From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from EUR01-HE1-obe.outbound.protection.outlook.com (mail-eopbgr130041.outbound.protection.outlook.com [40.107.13.41]) by sourceware.org (Postfix) with ESMTPS id 7E5BF3858413 for ; Wed, 8 Dec 2021 17:34:28 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 7E5BF3858413 Received: from AM6PR08CA0041.eurprd08.prod.outlook.com (2603:10a6:20b:c0::29) by HE1PR0802MB2618.eurprd08.prod.outlook.com (2603:10a6:3:d4::13) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4755.21; Wed, 8 Dec 2021 17:34:25 +0000 Received: from AM5EUR03FT039.eop-EUR03.prod.protection.outlook.com (2603:10a6:20b:c0:cafe::cd) by AM6PR08CA0041.outlook.office365.com (2603:10a6:20b:c0::29) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4778.12 via Frontend Transport; Wed, 8 Dec 2021 17:34:25 +0000 X-MS-Exchange-Authentication-Results: spf=pass (sender IP is 63.35.35.123) smtp.mailfrom=arm.com; dkim=pass (signature was verified) header.d=armh.onmicrosoft.com;dmarc=pass action=none header.from=arm.com; Received-SPF: Pass (protection.outlook.com: domain of arm.com designates 63.35.35.123 as permitted sender) receiver=protection.outlook.com; client-ip=63.35.35.123; helo=64aa7808-outbound-1.mta.getcheckrecipient.com; Received: from 64aa7808-outbound-1.mta.getcheckrecipient.com (63.35.35.123) by AM5EUR03FT039.mail.protection.outlook.com (10.152.17.185) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4755.13 via Frontend Transport; Wed, 8 Dec 2021 17:34:25 +0000 Received: ("Tessian outbound dbb52aec1fa6:v110"); Wed, 08 Dec 2021 17:34:25 +0000 X-CheckRecipientChecked: true X-CR-MTA-CID: f6e5b7209c8e1e98 X-CR-MTA-TID: 64aa7808 Received: from 24c0e2c5a2ca.1 by 64aa7808-outbound-1.mta.getcheckrecipient.com id 2FFC1AF9-60E8-4617-A334-BD18D07512BF.1; Wed, 08 Dec 2021 17:34:18 +0000 Received: from EUR01-DB5-obe.outbound.protection.outlook.com by 64aa7808-outbound-1.mta.getcheckrecipient.com with ESMTPS id 24c0e2c5a2ca.1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384); Wed, 08 Dec 2021 17:34:18 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=Nbr7+xdjtC+zaCRfky2NXU1/h6S0V4Xh7F+XUkYoRmgLyHKD+ORHcHgJinKgsMKVzCW0At1NRev+I156gvRFk9qsMzMQ4zX9+eFHsQcWjSBO8jVQv/l6wauuxagKU0DpbmFparRpK3x7OdiLmuhU0k/aXNjbAdzJWndCUF+kHcGj9CciIv2jYKcEOIKBuY6cLyhVy6eU1KoFjFk0TqT4X+joOz5Ftg+1+e3GY/ciseESccAJQ9NBvjeRIOi8vRJmlUwZM4d3Y1W55ybtTdrq4qUjpc7ww7DSYkoP0Ohr9GAb2Aq8T5nJmrDjC1SaLB4m+i+bm1788HMhZpSqvjfkUg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=nuI4VVd0rxZypoQQY3yT4JYDXve6vzRoKjzG7jZJRYE=; b=KqtN5COepvAIoGgJX6ZNoUBcqDUOVFIW+Rax+xmL1DbkEdednP/TgJj/EwMqfpOjzkEYm6MqEr2DXHe90xrRXvi+OuYPOJztL5SnQvETj8fHotpnAaoNwBNmpoQQvMr2jGkrctM/Kg/Axs6/xNiaVAqmu/xEL/WtdwZFY5wrKkEIY/YLkYkYfTteXnZ+w7LSIssG7p7MEE3vQBrcc8Q4lgzOuCTxXfrERL1Y2a1DqxS4BR5lgttT5SRpxFBi3UwfOZ0vn9kQkMWH6FUKrXGLV/VwRX7DsYNNp0G8TgWOF17XMz5tBV5EbPXA+QPByfEn2FSkSxoYdG+83X+0dxsdLA== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=arm.com; dmarc=pass action=none header.from=arm.com; dkim=pass header.d=arm.com; arc=none Authentication-Results-Original: dkim=none (message not signed) header.d=none;dmarc=none action=none header.from=arm.com; Received: from DB9PR08MB7179.eurprd08.prod.outlook.com (2603:10a6:10:2cc::19) by DB9PR08MB7082.eurprd08.prod.outlook.com (2603:10a6:10:2c2::22) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4755.22; Wed, 8 Dec 2021 17:34:15 +0000 Received: from DB9PR08MB7179.eurprd08.prod.outlook.com ([fe80::25f9:a7e6:422a:da43]) by DB9PR08MB7179.eurprd08.prod.outlook.com ([fe80::25f9:a7e6:422a:da43%6]) with mapi id 15.20.4755.022; Wed, 8 Dec 2021 17:34:15 +0000 Date: Wed, 8 Dec 2021 17:34:08 +0000 From: Szabolcs Nagy To: Florian Weimer Cc: libc-alpha@sourceware.org Subject: Re: [PATCH 7/8] nptl: Add public rseq symbols and Message-ID: <20211208173408.GK3294453@arm.com> References: <67665bb0d00cd540def5bfe758c94c98458536df.1638880889.git.fweimer@redhat.com> Content-Type: text/plain; charset=utf-8 Content-Disposition: inline In-Reply-To: <67665bb0d00cd540def5bfe758c94c98458536df.1638880889.git.fweimer@redhat.com> X-ClientProxiedBy: SN6PR05CA0001.namprd05.prod.outlook.com (2603:10b6:805:de::14) To DB9PR08MB7179.eurprd08.prod.outlook.com (2603:10a6:10:2cc::19) MIME-Version: 1.0 Received: from arm.com (217.140.106.51) by SN6PR05CA0001.namprd05.prod.outlook.com (2603:10b6:805:de::14) with Microsoft SMTP Server (version=TLS1_2, cipher=) via Frontend Transport; Wed, 8 Dec 2021 17:34:14 +0000 X-MS-Office365-Filtering-Correlation-Id: 845f8ca7-114c-4d2b-15ae-08d9ba70fabe X-MS-TrafficTypeDiagnostic: DB9PR08MB7082:EE_|AM5EUR03FT039:EE_|HE1PR0802MB2618:EE_ X-Microsoft-Antispam-PRVS: x-checkrecipientrouted: true NoDisclaimer: true X-MS-Oob-TLC-OOBClassifiers: OLM:7691;OLM:7691; X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam-Untrusted: BCL:0; X-Microsoft-Antispam-Message-Info-Original: 95oN89Mq+Z+3iiZE/wcBlkFLrBN+ae6vyWVdxHJE2Lo50MaQ/x5IQ4FrWb+be5JtEVsndxSuxgvfRR2hNXRDT/Q+vuVNJbHEsPSpYaGHA0N4gJ5yesiEWW77ehOemesGPNucznCnBA+pyaNrWWZsNIdSEipzpmMo9w/v2BJzltQ2ZLqm4A8OctjgvvhINIk2h/oFyktjXD1MABdQ0xbUSX3+0F45tYKEw8ZkvSALOQTAu1uTDVddfszsIxolwDacRQ+8DjOtLQBezO/38QuOveZG5+iqms2+/gXtG4iSoXlEV3z7AgOjT9GNiu19lD4LqWjdHSupzeGJoOaIoKukCn4XXTWdXOOZQRbxKgi7BXg3yLfsjF4Ttb+WLd/iYUUVDBqy0co8a3J3h6HfGA5UEq14qzrEF1qsBhJNUWQz4c3A17qcNC0K+6UbEQXup/aPEFPLEDbZlir4HWC3EBRGbnl8rFnsZDulnUtGRQxpui99Dap4Rye/zR2WkB+AS4kDjAD/Tie4CWBCxUazqkvUldKAVF4a6pBlPw+eRESkjq90U9KPwUP0wVUhDnJL24Eq7nMXKiyCKAK175SoVKQyquOvMbqqyiWEnDygWups1FFiWtOjqBIlTHZGhji13KFQOefvNDxmNQ5Mw9njR99ptNYMmi1jK1bfVoUCtDJJHOpvz5Eq0BLa8XGs3F0UJfxouQxRFYzmyA1kWdAw8P0m9JHZQg+TJgFXFHK7+wcNHum/W6SmoKQyo714yL3esXGjNCK+D0OBvWqvxHUFA28Rhzoz6gnVKh6BbTMrP9Znxx4= X-Forefront-Antispam-Report-Untrusted: CIP:255.255.255.255; CTRY:; LANG:en; SCL:1; SRV:; IPV:NLI; SFV:NSPM; H:DB9PR08MB7179.eurprd08.prod.outlook.com; PTR:; CAT:NONE; SFS:(4636009)(366004)(36756003)(8936002)(38350700002)(38100700002)(6916009)(6666004)(5660300002)(508600001)(316002)(8886007)(1076003)(2906002)(26005)(7696005)(52116002)(186003)(4326008)(83380400001)(2616005)(8676002)(956004)(66556008)(86362001)(33656002)(66476007)(55016003)(44832011)(66946007); DIR:OUT; SFP:1101; X-MS-Exchange-Transport-CrossTenantHeadersStamped: DB9PR08MB7082 Original-Authentication-Results: dkim=none (message not signed) header.d=none;dmarc=none action=none header.from=arm.com; X-EOPAttributedMessage: 0 X-MS-Exchange-Transport-CrossTenantHeadersStripped: AM5EUR03FT039.eop-EUR03.prod.protection.outlook.com X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id-Prvs: 0b145639-3815-4e78-7898-08d9ba70f4e1 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: fqzgzFzU2DcSUmvp+ppxD0E898ZvC0tH3ixhsdnCO88gha6i4r89HehenpFLSYphIeE/NJTGvq/ULLtX82PwbQ+MmhQ9YkPr8kvWlHl1kkockRdbL+HMiNjIR/ikot+r9UX4f7m4y4+hDOG+GXNwyFcTXFOfnWw7pkayBr3VI22EM8TektTTHWbv5TJrKnJIwW80BOzFCWUUFVfxLINjNzR+fNcnaCmKa+znsAgj9+EqJuW5HquROkrxFHCIk8HfGT9LCH9DOPra4OBAsyFKJvfxJnfQcGx0r7dXrqFIQebulQt2jzEI4yAdqRftg6eb3aNyA+qj5YJ4gA0BYz90ZjwTv9B7d9txzOVJFfIUVJb6Mftd7KaLOtM0l+i25DsMwq00nkkLiPvsmsO84cqYlNb9iTVv8SOfyBXmTHEkIXEDuWL1H1ZlcDroVuHpi+7zy7Pyrd8q3CnDNVUnfZMbgpce+w32I228wU/tdS6VAFBwvZa/g+cwFt7X+pX4eLiAHf3GmZRmzUpgoOkjkpf7BEljazMallhrSJspCxKShtkVLqGLvgonR+xEpQZ5jjZ+LW62N6FP1MJi8ZZDTz5pFjPLAQZc5VwaWwZrOVqWH9CYG002kGUuPoY5F81jgBAP8517i8FXIPfDjG4F8RxYLpPNmcjCvC9yd4dpzJA3s2gAewmqugdrk4ZfsK39upmmq7K4yzbscrUSEJKN6fkz0PkPp1+gDULSfysu+1cXG1nYQal58WyEGh/R13sMyMhNNyZzVD9FWuNBqZekHUSpt4CiB+bpxMKRZ3uqfJjxBHZJg5RHjk8+9QdnKfgf4D4AnAUPsCPzUbGzUzBEjcTkvHIULUinT7OzwLKKa/uC90E= X-Forefront-Antispam-Report: CIP:63.35.35.123; CTRY:IE; LANG:en; SCL:1; SRV:; IPV:CAL; SFV:NSPM; H:64aa7808-outbound-1.mta.getcheckrecipient.com; PTR:ec2-63-35-35-123.eu-west-1.compute.amazonaws.com; CAT:NONE; SFS:(4636009)(46966006)(36840700001)(40470700001)(316002)(44832011)(2906002)(2616005)(8886007)(36756003)(956004)(7696005)(6666004)(26005)(508600001)(356005)(40460700001)(36860700001)(47076005)(336012)(55016003)(8676002)(82310400004)(6862004)(70206006)(33656002)(186003)(81166007)(70586007)(5660300002)(1076003)(83380400001)(86362001)(4326008)(8936002); DIR:OUT; SFP:1101; X-OriginatorOrg: arm.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 08 Dec 2021 17:34:25.4719 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 845f8ca7-114c-4d2b-15ae-08d9ba70fabe X-MS-Exchange-CrossTenant-Id: f34e5979-57d9-4aaa-ad4d-b122a662184d X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=f34e5979-57d9-4aaa-ad4d-b122a662184d; Ip=[63.35.35.123]; Helo=[64aa7808-outbound-1.mta.getcheckrecipient.com] X-MS-Exchange-CrossTenant-AuthSource: AM5EUR03FT039.eop-EUR03.prod.protection.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: HE1PR0802MB2618 X-Spam-Status: No, score=-13.1 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, GIT_PATCH_0, MSGID_FROM_MTA_HEADER, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H2, SPF_HELO_PASS, SPF_PASS, TXREP, UNPARSEABLE_RELAY autolearn=ham autolearn_force=no version=3.4.4 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on server2.sourceware.org X-BeenThere: libc-alpha@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Libc-alpha mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Wed, 08 Dec 2021 17:34:31 -0000 The 12/07/2021 14:03, Florian Weimer via Libc-alpha wrote: > The relationship between the thread pointer and the rseq area > is made explicit. The constant offset can be used by JIT compilers > to optimize rseq access (e.g., for really fast sched_getcpu). > > Extensibility is provided through __rseq_size and __rseq_flags. > (In the future, the kernel could request a different rseq size > via the auxiliary vector.) > > Co-Authored-By: Mathieu Desnoyers > --- > v2: Fix manual typos. Other than the typos noted below this looks ok. Reviewed-by: Szabolcs Nagy > --- a/manual/threads.texi > +++ b/manual/threads.texi > @@ -629,6 +629,8 @@ the standard. > * Waiting with Explicit Clocks:: Functions for waiting with an > explicit clock specification. > * Single-Threaded:: Detecting single-threaded execution. > +* Restartable Sequences:: Linux-specific restartable sequences > + integration. > @end menu > > @node Default Thread Attributes > @@ -958,6 +960,85 @@ application-created thread because future versions of @theglibc{} may > create background threads after the first thread has been created, and > the application has no way of knowning that these threads are present. > > +@node Restartable Sequences > +@subsubsection Restartable Sequences > + > +This section describes restartable sequences integration for > +@theglibc{}. This functionality is only available on Linux. > + > +@deftp {Data Type} {struct rseq} > +@standards{Linux, sys/rseq.h} > +The type of the restartable sequences area. Future versions > +of Linux may add additional fields to the end of this structure. > + > + > +Users need to obtain the address of the restartable sequences area using > +the thread pointer and the @code{__rseq_offset} variable, described > +below. > + > +One use of the restartable sequences area is to read the current CPU > +number from its @code{cpu_id} field, as an inline version of > +@code{sched_getcpu}. @Theglibc{} sets the @code{cpu_id} field to > +@code{RSEQ_CPU_ID_REGISTRATION_FAILED} if registration failed or was > +explicitly disabled. > + > +Furthermore, users can store the address of a @code{struct rseq_cs} > +object into the @code{rseq_cs} field of @code{struct rseq}, thus > +informing the kernel that the thread enters a restartable sequence > +critical section. This pointer and the code areas it itself points to > +must not be left pointing to memory areas which are freed or re-used. > +Several approaches can guarantee this. If the application or library > +can guarantee that the memory used to hold the @code{struct rseq_cs} and > +the code areas it refers to are never freed or re-used, no special > +action must be taken. Else, before that memory is re-used of freed, the > +application is responsible for setting the @code{rseq_cs} field to > +@code{NULL} in each thread's restartable sequence area to guarantee that > +it does not leak dangling references. Because the application does not > +typically have knowledge of libraries' use of restartable sequences, it > +is recommended that libraries using restartable sequences which may end > +up freeing or re-using their memory set the @code{rseq_cs} field to > +@code{NULL} before returning from library functions which use > +restartable sequences. > + > +The manual for the @code{rseq} system call can be found > +at @uref{https://git.kernel.org/pub/scm/libs/librseq/librseq.git/tree/doc/man/rseq.2}. > +@end deftp > + > +@deftypevar {int} __rseq_offset > +@standards{Linux, sys/rseq.h} > +This variable contains the offset between the thread pointer (as defined > +by @code{__builtin_thread_pointer} or the thread pointer register for > +the architecture) and the restartable sequences area. This value is the > +same for all threads in the process. If the restartable sequences area > +is located at a lower address than the location to which the thread > +pointer points, the value is negative. > +@end deftypevar > + > +@deftypevar {unsigned int} __rseq_size > +@standards{Linux, sys/rseq.h} > +This variable is either zero (if restartable sequence registration > +failed or has been disabled) or the size of the restartable sequence > +registration. This can be less can be different from the size of can be can be > +@code{struct rseq} if the kernel has extended the size of the > +registration. If registration is successful, @code{__rseq_size} is at > +least 32 (the initial size of @code{struct rseq}). > +@end deftypevar > + > +@deftypevar {unsigned int} __rseq_flags > +@standards{Linux, sys/rseq.h} > +The flags used during restartable sequence registration with the kernel. > +Currently zero. > +@end deftypevar > + > +@deftypevr Macro int RSEQ_SIG > +@standards{Linux, sys/rseq.h} > +Each supported architecture provides a @code{RSEQ_SIG} macro in > +@file{sys/rseq.h} which contains a signature. That signature is > +expected to be present in the code before each restartable sequences > +abort handler. Failure to provide the expected signature may terminate > +the process with a segmentation fault. > +@end deftypevr > + > @c FIXME these are undocumented: > @c pthread_atfork > @c pthread_attr_destroy > diff --git a/sysdeps/nptl/dl-tls_init_tp.c b/sysdeps/nptl/dl-tls_init_tp.c > index b39dfbff2c..77443cc330 100644 > --- a/sysdeps/nptl/dl-tls_init_tp.c > +++ b/sysdeps/nptl/dl-tls_init_tp.c > @@ -22,6 +22,7 @@ > #include > #include > #include > +#include > > #define TUNABLE_NAMESPACE pthread > #include > @@ -43,6 +44,10 @@ rtld_mutex_dummy (pthread_mutex_t *lock) > } > #endif > > +const unsigned int __rseq_flags; > +const unsigned int __rseq_size attribute_relro; > +const int __rseq_offset attribute_relro; > + > void > __tls_pre_init_tp (void) > { > @@ -100,7 +105,23 @@ __tls_init_tp (void) > #if HAVE_TUNABLES > do_rseq = TUNABLE_GET (rseq, int, NULL); > #endif > - rseq_register_current_thread (pd, do_rseq); > + if (rseq_register_current_thread (pd, do_rseq)) > + { > + /* We need a writable view of the variables. They are in > + .data.relro and are not yet write-protected. */ > + extern unsigned int size __asm__ ("__rseq_size"); > + size = sizeof (pd->rseq_area); > + } > + > +#ifdef RSEQ_SIG > + /* This should be a compile-time constant, but the current > + infrastructure makes it difficult to determine its value. Not > + all targets support __thread_pointer, so set set __rseq_offset > + only if thre rseq registration may have happened because set set thre > + RSEQ_SIG is defined. */ > + extern int offset __asm__ ("__rseq_offset"); > + offset = (char *) &pd->rseq_area - (char *) __thread_pointer (); > +#endif > }