From patchwork Tue May 5 19:06:50 2026 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Adhemerval Zanella Netto X-Patchwork-Id: 134507 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from vm01.sourceware.org (localhost [127.0.0.1]) by sourceware.org (Postfix) with ESMTP id 9E8274BAD14D for ; Tue, 5 May 2026 19:10:22 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 9E8274BAD14D Authentication-Results: sourceware.org; dkim=pass (2048-bit key, unprotected) header.d=linaro.org header.i=@linaro.org header.a=rsa-sha256 header.s=google header.b=GdU7/K9k X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail-vs1-xe33.google.com (mail-vs1-xe33.google.com [IPv6:2607:f8b0:4864:20::e33]) by sourceware.org (Postfix) with ESMTPS id E2C1F4BA799C for ; Tue, 5 May 2026 19:08:18 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org E2C1F4BA799C Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=linaro.org Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=linaro.org ARC-Filter: OpenARC Filter v1.0.0 sourceware.org E2C1F4BA799C Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2607:f8b0:4864:20::e33 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1778008099; cv=none; b=a38UC3NfE+uO+bjiyU7iFn9Pm7vJv8E4R0Lxz/K1jJ3xJYKbefCEB2J7Q8um9x3hIeBAZWoS84Qb2/GSSPx0dGuIBbkIQ5CRgmt/atgsKos7cq80MFHrGs7k5R6BtevjTi6iXTTAGNXNByl5KDdk85r5wB/Gwvl4nHyB1cBayrw= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1778008099; c=relaxed/simple; bh=+SSVsmE6kVKSB8EXvdJTwTvgbRtC55Je9txgMQ+gqcQ=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=kmdURJXjP/ObQGFVVznfqt/gGrG31C5tZRovK+OhKdYYD+kJqkvSD08joLMuASWxZ93LPik0GxDebLMNSECcAam2KWumGnzFMySnh6Qbo/DqSM9Jr3dEiXlyZewi+PBGg3L4S/QJCu4wOzF6atWOuW6b5Hdi/Bi1gh34QO31lEw= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org E2C1F4BA799C Received: by mail-vs1-xe33.google.com with SMTP id ada2fe7eead31-610f4cf6ddcso159285137.1 for ; Tue, 05 May 2026 12:08:18 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1778008098; x=1778612898; darn=sourceware.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=dDn68h72jIjI8U4hEHDseM+LUdAGP1zBrJx+1FVvZfc=; b=GdU7/K9kQKHlC9xOnaZfDfOKm0K3kKFoBsQCFk/skGV46vAf80wJNOlzhYNAAq09bH +mis5ETv+Z4MuOtjpwSP0WEDW5vh2y56Yr+JDbXCBpHc0JNolFsZKQmxTQWvDiIop+UV +nspth1uAOYMwx+dIF6Bz/8XHmu9hjYPOM6TZ4LayTao4Op7PP5LLuUZ+oIz/Fvgoogi wqvCipfxVZCdwWjvKJrzEHDDR6xAsWFqFIrJTsB9voSq1i6d7NYJigJ2JC5BBtF0ijKg P4XxG+XPaR2JsbNrziviKpXz6gkDgJMz9Kg4Il2ZupnFLgaRJksWXJtsrU6C1qbt8uyA VQLQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1778008098; x=1778612898; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to; bh=dDn68h72jIjI8U4hEHDseM+LUdAGP1zBrJx+1FVvZfc=; b=L4mW+Zt4zF4bcHtaBkbjoG/WcBy8hHQdXWUvdreKpAxGxQqVNObR7zmpsUQJAO32rZ TcsgeY/FBxZkO7aELoo7MQ9MVeeARPbxACM1+Dmp9S52NXaV0XBMNeyVblABrEM87qp5 WMakA7R8k/EXOuw608zPGGlscINWT19nfCA4LvZ+K+0WxZLhohEa+bX/N0Y6JTEqyWy2 43s+M4JIIDlp1qRL0zk8UzxzL1Oi6hAeqKu0URAfeLha2jot/h5aQJgq57c2xIc9f90q g+FwAmky4AzNcHnJks+n7kUHzYZqT6G2RU91b+LfPJ/OtwNQupghGRiJJQIkmriVprkj 4hxA== X-Gm-Message-State: AOJu0YxzC3XmyEwgmM7Ls4ozct1qBFq1qe8H7Q/2cljOLHQtKGT4rIOP xjlSfd0lc07m1TcEgbGgjgu/6yqzWixdP+6Hi3bwBsh3bgaiVR93a6EVwSnTFOIVmYq+bSnvezX jIrLD X-Gm-Gg: AeBDiet58B+5aHrIhafRt8T7QnVJZ/S0ULR/gexSSEeNS9DeCrZJGPP4moDxjeNzZrk SScgaRMysyQxUyFI71codnm/O+uzpjvCdFRiIO9OSc/yvRSMA9e1n15wkbAYZzlbMwiiL5m0OWH +0g2yYYvQgSJecM69S668CLUOaO1J3dvJK9Sjmj1+zGpZ+ivhnrUn3ic8TOht1HsVuMsr0P4u+S JzXNvmseyPtCQUu1cXFxDqEvN9Ecy3kB6xexBxV9qzCnMVtVVEFvYjiN7tTRwvezYk3elXYc5fj HZzWxxQ6/C+k0jx043QZo+tx+99lAxlO9vOe8J8EdR+57xC6/GEbXKXfrU54GwRLYYAkuR1Rpp6 FmlUQV0TpW4P/9ItHJAGb8rensTmmRKrutWwMnLajJO98k3euFw9x7cYGwlhJ3iNvG1rl7Z1zUm Oi4uDJbSEgbTfF3X2etpgBz9LELQ3/UmwBJZd+pRD074M= X-Received: by 2002:a05:6102:d8a:b0:607:d68f:d0f6 with SMTP id ada2fe7eead31-62f55a492fdmr2718782137.20.1778008097810; Tue, 05 May 2026 12:08:17 -0700 (PDT) Received: from mandiga.. ([2804:1b3:a7c1:364:abee:44ff:b045:5a7]) by smtp.gmail.com with ESMTPSA id a1e0cc1a2514c-95ce08f4a7asm7504046241.5.2026.05.05.12.08.16 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 05 May 2026 12:08:17 -0700 (PDT) From: Adhemerval Zanella To: libc-alpha@sourceware.org Cc: Florian Weimer Subject: [PATCH v2 1/3] support: Add support_process_shared_robust_mutex Date: Tue, 5 May 2026 16:06:50 -0300 Message-ID: <20260505190809.3898686-2-adhemerval.zanella@linaro.org> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20260505190809.3898686-1-adhemerval.zanella@linaro.org> References: <20260505190809.3898686-1-adhemerval.zanella@linaro.org> MIME-Version: 1.0 X-Spam-Status: No, score=-12.3 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, KAM_SHORT, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_NONE, TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on sourceware.org X-BeenThere: libc-alpha@sourceware.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Libc-alpha mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: libc-alpha-bounces~patchwork=sourceware.org@sourceware.org Returns whether process-shared robust mutexes are supported. --- support/Makefile | 1 + support/support_mutex_robust.c | 34 ++++++++++++++++++++++++++++++++++ support/xthread.h | 3 +++ 3 files changed, 38 insertions(+) create mode 100644 support/support_mutex_robust.c diff --git a/support/Makefile b/support/Makefile index 3f19a98bdc4..4facaaead6b 100644 --- a/support/Makefile +++ b/support/Makefile @@ -87,6 +87,7 @@ libsupport-routines = \ support_isolate_in_subprocess \ support_mem_access \ support_mutex_pi_monotonic \ + support_mutex_robust \ support_need_proc \ support_nscd \ support_open_and_compare_file_bytes \ diff --git a/support/support_mutex_robust.c b/support/support_mutex_robust.c new file mode 100644 index 00000000000..2957aca230d --- /dev/null +++ b/support/support_mutex_robust.c @@ -0,0 +1,34 @@ +/* Returns whether process-shared robust mutexes are supported. + Copyright (C) 2025 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + The GNU C Library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + The GNU C Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with the GNU C Library; if not, see + . */ + +#include +#include +#include + +bool +support_process_shared_robust_mutex (void) +{ +#ifdef __linux__ + void *head; + size_t len; + int r = syscall (__NR_get_robust_list, 0, &head, &len); + return r == 0; +#else + return true; +#endif +} diff --git a/support/xthread.h b/support/xthread.h index d585bd0c3c6..7afd96dd3b9 100644 --- a/support/xthread.h +++ b/support/xthread.h @@ -36,6 +36,9 @@ void delayed__exit (int seconds); /* Returns true if Priority Inheritance support CLOCK_MONOTONIC. */ bool support_mutex_pi_monotonic (void); +/* Returns true if process-shared robust mutexes are supported. */ +bool support_process_shared_robust_mutex (void); + /* Terminate the process (with exit status 1) if VALUE is not zero. In that case, print a failure message to standard output mentioning FUNCTION. The process is terminated with the exit function, so From patchwork Tue May 5 19:06:51 2026 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Adhemerval Zanella Netto X-Patchwork-Id: 134506 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from vm01.sourceware.org (localhost [127.0.0.1]) by sourceware.org (Postfix) with ESMTP id 431864BAD15E for ; Tue, 5 May 2026 19:09:21 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 431864BAD15E Authentication-Results: sourceware.org; dkim=pass (2048-bit key, unprotected) header.d=linaro.org header.i=@linaro.org header.a=rsa-sha256 header.s=google header.b=UNw46paz X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail-ua1-x936.google.com (mail-ua1-x936.google.com [IPv6:2607:f8b0:4864:20::936]) by sourceware.org (Postfix) with ESMTPS id 852514BA799E for ; Tue, 5 May 2026 19:08:23 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 852514BA799E Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=linaro.org Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=linaro.org ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 852514BA799E Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2607:f8b0:4864:20::936 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1778008103; cv=none; b=RWxn1FB4yEAtvepu/Zip84zDLTOu8dxGRCT8Xt0BM7bXDL+m5RblrExAC6hcvvzgtB4u1smtMHBC5ReNFFkNbKGOKOa4+m5YMOghf77Lv2GU5C2U2tXVP7pdPw+FhCd53AgW7IWQQ/UKMX0sZFNejXlpiPrDW0kWMvoiGtIlVFg= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1778008103; c=relaxed/simple; bh=RRPwl7yHx3wHsqDSeSqBvaKNLXjSxxuB/4/iEMdcH5c=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=jOSOmvsXH2wkpAszjL9L5RXGVKuKobhkvhsc0YTS1Yr4kgozDS+W2+o4gzcwauATfOHlQbBa4vbqNQZd8rTHE0OslzSoYiqrvDrHDVFV/oyRV44oeiC/ikRGgpZtftEDwOw96Htkpe+lsFI2uBZX9e6sPw/z3w2TZsSUJfxRbQ8= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 852514BA799E Received: by mail-ua1-x936.google.com with SMTP id a1e0cc1a2514c-95695190911so1326245241.2 for ; Tue, 05 May 2026 12:08:23 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1778008102; x=1778612902; darn=sourceware.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=e1tQo/fmJU5NcynXJf61cqSsDtN5zrfMYr0mp/kgIo8=; b=UNw46pazt0QcWAUoccybo3Df9RR0nK4QoUgi1h7X9vnajUFjbQ4nEZrEEfIIhtr4OC WqJ5JZ8iMuHQ4QxXbsxZ/vMxtVVISh61lvdPO18URIQ6HGIFJOiA/H15u3BkxrZLT7bK i8B2TSyNOXthrilPfo3TaycLjxiONC5vSxm6FtPngBrslbH0GD8cl9QV6Irl1EScoOH7 g0/5ob0jD6o/FOeRzDROKDz1Tft8NZ18DQdz9KKqMFI8/dWItrVht3j8Q53qoz80ELsG swCEXKFGiFU32u61dJyu+Yv1yKNbTEYmnquS9pwnGE9ygYbULN70EL84DVPz+YzNy2bK Fw4w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1778008102; x=1778612902; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to; bh=e1tQo/fmJU5NcynXJf61cqSsDtN5zrfMYr0mp/kgIo8=; b=HEfGoiphvn7TsgHS0DJJ327Rx7XFMYy+68p1ldCgrGH8+4rIAAv7seXZi0osF0KpmI hQyg+aUR18svi7ChQVhi59u5tfIeMgmzY3e8F/nFIg31w+RHbe2UIEl+ffpBH41CX8ST g0AkR1NTvHg/fkK5h57S51VO80IWJ9sb1ph0pownJ5ljJwIGjo6XCwbtezIQFquNbdQo 0h0e9EFX1HF+FhFYH8VWdCZC7peOdhaNU4aVMbARTViSM+EO4lMeeP+Bm5zkL38CjYek nNQM0lhVyDUz3ucgLE5SjJZzlindhRHSDlzIv5w8apyvybc6gjgu8yjckaTO64ebS89P drkg== X-Gm-Message-State: AOJu0YxXtFaMgs3q4UPn6IkDRT/aiKERPvaCYUZ3RfZUyYGFDp5FgSe9 W0iqctS3f3iVbxCtl8rutU5w7gQ0VAHdj0DQ0TEqp2RrbyOFE/9ZPcVhVFK5xvdkStti+2mpDcT QIEVz X-Gm-Gg: AeBDievU+XQ37d28Px3qzghUojgQLmOvWS2dtDdo4GrrN+IKucWY6RMr86W1oWf6ms7 gj1OG07ZqmeswFOhAre7oAxV02Xl+Gp7hGH6NAsCCbmmaZXNXNgW4v+FoPiovxjoX7PKYrbw0A3 1r6rq0dCRiKHztISiuYb43wAbfEEKBjPFA603JevHxqDmj0uCKbogVM7nO8LzIGm37nCIhqYs9U jAh7uqcuQQErPIpjDUU/HxMFir8G4Jiv/ZnAbBB0hfPsoCHQv5DP3oEX2jJfhfORH2RmevUtrpI oyug2LeEoBPisouTneOP/GB3MOupzRHyZZwGRKCruPzNX3vhPTEZyd+NOmhGAcEye9N0vTLnqUh SSynvM5qCNprXxWGYKZy7fjB1VfM14ctetfC6kE4fKTT2ei6WNA6z+cENe/nCJzqFOLXWymr96J jWjVi+sagJXdmJiwVAd1MGZhCKqNe/IkriBKhtv3NKMlI= X-Received: by 2002:a05:6102:6a94:b0:62f:3ba3:3039 with SMTP id ada2fe7eead31-630f8c252cemr15423137.0.1778008102176; Tue, 05 May 2026 12:08:22 -0700 (PDT) Received: from mandiga.. ([2804:1b3:a7c1:364:abee:44ff:b045:5a7]) by smtp.gmail.com with ESMTPSA id a1e0cc1a2514c-95ce08f4a7asm7504046241.5.2026.05.05.12.08.20 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 05 May 2026 12:08:21 -0700 (PDT) From: Adhemerval Zanella To: libc-alpha@sourceware.org Cc: Florian Weimer , Daniel Henrique Barboza Subject: [PATCH v2 2/3] nptl: Do not always assume set_robust_list availability (BZ 33225) Date: Tue, 5 May 2026 16:06:51 -0300 Message-ID: <20260505190809.3898686-3-adhemerval.zanella@linaro.org> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20260505190809.3898686-1-adhemerval.zanella@linaro.org> References: <20260505190809.3898686-1-adhemerval.zanella@linaro.org> MIME-Version: 1.0 X-Spam-Status: No, score=-12.6 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_NONE, TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on sourceware.org X-BeenThere: libc-alpha@sourceware.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Libc-alpha mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: libc-alpha-bounces~patchwork=sourceware.org@sourceware.org The __ASSUME_SET_ROBUST_LIST is used to define and set __nptl_set_robust_list_avail to advertise process shared robust mutex support, done at __tls_init_tp (during pthread initialization). Some specific kernel configurations and the qemu-user (for all ABIs) do not implement the set_robust_list syscal. Therefore, for the default case (__ASSUME_SET_ROBUST_LIST being true), the missing support is not detected, and pthread_mutex_init succeeds where it should fail. For instance, the sequence succeeds on qemu-user even when set_robust_list fails with ENOSYS. pthread_mutexattr_init(&attr); pthread_mutexattr_setpshared(&attr, PTHREAD_PROCESS_SHARED); pthread_mutexattr_setrobust(&attr, PTHREAD_MUTEX_ROBUST); pthread_mutex_init(&mutex, &attr); This patch removes __ASSUME_SET_ROBUST_LIST and always enables __nptl_set_robust_list_avail detection. Checked on x86_64-linux-gnu. Reported-by: Daniel Henrique Barboza --- nptl/pthread_create.c | 4 ---- nptl/pthread_mutex_init.c | 3 --- nptl/tst-mutexpi10.c | 4 +++- nptl/tst-robust-fork.c | 21 +++++++++++++++++++ sysdeps/nptl/dl-tls_init_tp.c | 8 +------ sysdeps/nptl/pthreadP.h | 2 -- sysdeps/pthread/tst-robust7.c | 1 - sysdeps/pthread/tst-robust8.c | 6 ++++++ sysdeps/pthread/tst-robust9.c | 1 - sysdeps/unix/sysv/linux/arm/kernel-features.h | 7 ------- .../unix/sysv/linux/hppa/kernel-features.h | 3 --- sysdeps/unix/sysv/linux/kernel-features.h | 5 ----- .../unix/sysv/linux/m68k/kernel-features.h | 5 ----- .../unix/sysv/linux/mips/kernel-features.h | 6 ------ .../unix/sysv/linux/riscv/kernel-features.h | 5 ----- .../unix/sysv/linux/sparc/kernel-features.h | 6 ------ 16 files changed, 31 insertions(+), 56 deletions(-) diff --git a/nptl/pthread_create.c b/nptl/pthread_create.c index 9a0cefb0f5a..72f77d1914e 100644 --- a/nptl/pthread_create.c +++ b/nptl/pthread_create.c @@ -386,9 +386,7 @@ start_thread (void *arg) __libc_fatal ("Fatal glibc error: rseq registration failed\n"); } -#ifndef __ASSUME_SET_ROBUST_LIST if (__nptl_set_robust_list_avail) -#endif { /* This call should never fail because the initial call in init.c succeeded. */ @@ -536,7 +534,6 @@ start_thread (void *arg) pd->exiting = true; __libc_lock_unlock (pd->exit_lock); -#ifndef __ASSUME_SET_ROBUST_LIST /* If this thread has any robust mutexes locked, handle them now. */ # if __PTHREAD_MUTEX_HAVE_PREV void *robust = pd->robust_head.list; @@ -567,7 +564,6 @@ start_thread (void *arg) } while (robust != (void *) &pd->robust_head); } -#endif /* Release the vDSO getrandom per-thread buffer with all signal blocked, to avoid creating a new free-state block during thread release. */ diff --git a/nptl/pthread_mutex_init.c b/nptl/pthread_mutex_init.c index 8a5450bcb08..9be08332f1f 100644 --- a/nptl/pthread_mutex_init.c +++ b/nptl/pthread_mutex_init.c @@ -93,12 +93,9 @@ ___pthread_mutex_init (pthread_mutex_t *mutex, if ((imutexattr->mutexkind & PTHREAD_MUTEXATTR_FLAG_ROBUST) != 0) { -#ifndef __ASSUME_SET_ROBUST_LIST if ((imutexattr->mutexkind & PTHREAD_MUTEXATTR_FLAG_PSHARED) != 0 && !__nptl_set_robust_list_avail) return ENOTSUP; -#endif - mutex_kind |= PTHREAD_MUTEX_ROBUST_NORMAL_NP; } diff --git a/nptl/tst-mutexpi10.c b/nptl/tst-mutexpi10.c index 3978fb25fdf..379627e4b02 100644 --- a/nptl/tst-mutexpi10.c +++ b/nptl/tst-mutexpi10.c @@ -58,7 +58,9 @@ do_test (void) xpthread_mutexattr_setrobust (&attr, robust[r]); pthread_mutex_t mtx; - xpthread_mutex_init (&mtx, &attr); + if (pthread_mutex_init (&mtx, &attr) == ENOTSUP + && robust[r] == PTHREAD_MUTEX_ROBUST) + continue; /* Uncontended case does not trigger any futex call. */ struct timespec tmo = timespec_add (xclock_now (clocks[c].clk), diff --git a/nptl/tst-robust-fork.c b/nptl/tst-robust-fork.c index be9c6d3cc41..80d76f96c14 100644 --- a/nptl/tst-robust-fork.c +++ b/nptl/tst-robust-fork.c @@ -159,9 +159,20 @@ one_test (int parent_bits, int child_bits, int nonshared_bits, xmunmap (shared, sizeof (*shared)); } +static inline bool +is_robust_pshared (int bits) +{ + return (bits & (mutex_robust | mutex_pshared)) + == (mutex_robust | mutex_pshared); +} + static int do_test (void) { + bool robust_support = support_process_shared_robust_mutex (); + if (test_verbose) + printf ("info: robust_support=%d\n", robust_support); + for (int parent_bits = 0; parent_bits <= mutex_all_bits; ++parent_bits) for (int child_bits = 0; child_bits <= mutex_all_bits; ++child_bits) for (int nonshared_bits = 0; nonshared_bits <= mutex_all_bits; @@ -175,6 +186,16 @@ do_test (void) parent_bits, child_bits, nonshared_bits, lock_nonshared ? " lock_nonshared" : "", lock_child ? " lock_child" : ""); + if ((is_robust_pshared (parent_bits) + || is_robust_pshared (child_bits) + || is_robust_pshared (nonshared_bits)) + && !robust_support) + { + if (test_verbose) + printf ("info: skipping tests due missing robust mutex" + " support\n"); + continue; + } one_test (parent_bits, child_bits, nonshared_bits, lock_nonshared, lock_child); } diff --git a/sysdeps/nptl/dl-tls_init_tp.c b/sysdeps/nptl/dl-tls_init_tp.c index 72cc4087c91..75e3712a6d8 100644 --- a/sysdeps/nptl/dl-tls_init_tp.c +++ b/sysdeps/nptl/dl-tls_init_tp.c @@ -28,10 +28,8 @@ #define TUNABLE_NAMESPACE pthread #include -#ifndef __ASSUME_SET_ROBUST_LIST bool __nptl_set_robust_list_avail; rtld_hidden_data_def (__nptl_set_robust_list_avail) -#endif bool __nptl_initial_report_events; rtld_hidden_def (__nptl_initial_report_events) @@ -95,11 +93,7 @@ __tls_init_tp (void) int res = INTERNAL_SYSCALL_CALL (set_robust_list, &pd->robust_head, sizeof (struct robust_list_head)); if (!INTERNAL_SYSCALL_ERROR_P (res)) - { -#ifndef __ASSUME_SET_ROBUST_LIST - __nptl_set_robust_list_avail = true; -#endif - } + __nptl_set_robust_list_avail = true; } { diff --git a/sysdeps/nptl/pthreadP.h b/sysdeps/nptl/pthreadP.h index de432d40324..c62c8982905 100644 --- a/sysdeps/nptl/pthreadP.h +++ b/sysdeps/nptl/pthreadP.h @@ -192,12 +192,10 @@ libc_hidden_proto (__pthread_keys) extern unsigned int __nptl_nthreads; libc_hidden_proto (__nptl_nthreads) -#ifndef __ASSUME_SET_ROBUST_LIST /* True if the set_robust_list system call works. Initialized in __tls_init_tp. */ extern bool __nptl_set_robust_list_avail; rtld_hidden_proto (__nptl_set_robust_list_avail) -#endif /* Thread Priority Protection. */ extern int __sched_fifo_min_prio; diff --git a/sysdeps/pthread/tst-robust7.c b/sysdeps/pthread/tst-robust7.c index d062d3d6582..a2567d7fe08 100644 --- a/sysdeps/pthread/tst-robust7.c +++ b/sysdeps/pthread/tst-robust7.c @@ -21,7 +21,6 @@ #include #include - static pthread_barrier_t b; static pthread_cond_t c = PTHREAD_COND_INITIALIZER; static pthread_mutex_t m; diff --git a/sysdeps/pthread/tst-robust8.c b/sysdeps/pthread/tst-robust8.c index 65f37feddfa..3b3ca161905 100644 --- a/sysdeps/pthread/tst-robust8.c +++ b/sysdeps/pthread/tst-robust8.c @@ -10,6 +10,8 @@ #include +#include +#include static void prepare (void); @@ -143,6 +145,10 @@ child (int round) static int do_test (void) { + /* Process shared robust mutexes requires kernel support. */ + if (!support_process_shared_robust_mutex ()) + FAIL_UNSUPPORTED ("process-shared robust mutexes not supported"); + if (ftruncate (fd, N * sizeof (pthread_mutex_t)) != 0) { puts ("cannot size new file"); diff --git a/sysdeps/pthread/tst-robust9.c b/sysdeps/pthread/tst-robust9.c index ca24f24282f..99b6abbc86b 100644 --- a/sysdeps/pthread/tst-robust9.c +++ b/sysdeps/pthread/tst-robust9.c @@ -5,7 +5,6 @@ #include #include - static pthread_mutex_t m; static void * diff --git a/sysdeps/unix/sysv/linux/arm/kernel-features.h b/sysdeps/unix/sysv/linux/arm/kernel-features.h index d169bf58946..1511de1c843 100644 --- a/sysdeps/unix/sysv/linux/arm/kernel-features.h +++ b/sysdeps/unix/sysv/linux/arm/kernel-features.h @@ -20,13 +20,6 @@ #include #include_next -/* The ARM kernel before 3.14.3 may or may not support - futex_atomic_cmpxchg_inatomic, depending on kernel - configuration. */ -#if __LINUX_KERNEL_VERSION < 0x030E03 -# undef __ASSUME_SET_ROBUST_LIST -#endif - /* ARM fadvise64_64 reorganize the syscall arguments. */ #define __ASSUME_FADVISE64_64_6ARG 1 diff --git a/sysdeps/unix/sysv/linux/hppa/kernel-features.h b/sysdeps/unix/sysv/linux/hppa/kernel-features.h index 1b46336e4a0..45ef64bb1f9 100644 --- a/sysdeps/unix/sysv/linux/hppa/kernel-features.h +++ b/sysdeps/unix/sysv/linux/hppa/kernel-features.h @@ -30,6 +30,3 @@ #undef __ASSUME_CLONE_DEFAULT #define __ASSUME_CLONE_BACKWARDS 1 - -/* QEMU does not support set_robust_list. */ -#undef __ASSUME_SET_ROBUST_LIST diff --git a/sysdeps/unix/sysv/linux/kernel-features.h b/sysdeps/unix/sysv/linux/kernel-features.h index 85f6e888dba..33f3c2d2c58 100644 --- a/sysdeps/unix/sysv/linux/kernel-features.h +++ b/sysdeps/unix/sysv/linux/kernel-features.h @@ -49,11 +49,6 @@ SH this appeared first in 2.6.19-rc1. */ #define __ASSUME_PSELECT 1 -/* Support for inter-process robust mutexes was added in 2.6.17 (but - some architectures lack futex_atomic_cmpxchg_inatomic in some - configurations). */ -#define __ASSUME_SET_ROBUST_LIST 1 - /* The termios2 interface was introduced across all architectures except Alpha in kernel 2.6.22. */ #define __ASSUME_TERMIOS2 1 diff --git a/sysdeps/unix/sysv/linux/m68k/kernel-features.h b/sysdeps/unix/sysv/linux/m68k/kernel-features.h index d66fe16fa8d..db90d8a8c07 100644 --- a/sysdeps/unix/sysv/linux/m68k/kernel-features.h +++ b/sysdeps/unix/sysv/linux/m68k/kernel-features.h @@ -42,11 +42,6 @@ # undef __ASSUME_GETPEERNAME_SYSCALL #endif -/* No support for PI futexes or robust mutexes before 3.10 for m68k. */ -#if __LINUX_KERNEL_VERSION < 0x030a00 -# undef __ASSUME_SET_ROBUST_LIST -#endif - /* m68k only supports ipc syscall before 5.1. */ #if __LINUX_KERNEL_VERSION < 0x050100 # undef __ASSUME_DIRECT_SYSVIPC_SYSCALLS diff --git a/sysdeps/unix/sysv/linux/mips/kernel-features.h b/sysdeps/unix/sysv/linux/mips/kernel-features.h index 7790f0d14b5..0d30d4d8284 100644 --- a/sysdeps/unix/sysv/linux/mips/kernel-features.h +++ b/sysdeps/unix/sysv/linux/mips/kernel-features.h @@ -21,12 +21,6 @@ #include_next -/* The MIPS kernel does not support futex_atomic_cmpxchg_inatomic if - emulating LL/SC. */ -#if __mips == 1 || defined _MIPS_ARCH_R5900 -# undef __ASSUME_SET_ROBUST_LIST -#endif - /* Define this if your 32-bit syscall API requires 64-bit register pairs to start with an even-number register. */ #if _MIPS_SIM == _ABIO32 diff --git a/sysdeps/unix/sysv/linux/riscv/kernel-features.h b/sysdeps/unix/sysv/linux/riscv/kernel-features.h index 32087c0602c..9172373e989 100644 --- a/sysdeps/unix/sysv/linux/riscv/kernel-features.h +++ b/sysdeps/unix/sysv/linux/riscv/kernel-features.h @@ -21,8 +21,3 @@ #undef __ASSUME_CLONE_DEFAULT #define __ASSUME_CLONE_BACKWARDS 1 - -/* No support for PI mutexes or robust futexes before 4.20. */ -#if __LINUX_KERNEL_VERSION < 0x041400 -# undef __ASSUME_SET_ROBUST_LIST -#endif diff --git a/sysdeps/unix/sysv/linux/sparc/kernel-features.h b/sysdeps/unix/sysv/linux/sparc/kernel-features.h index eb293411135..dad512cec35 100644 --- a/sysdeps/unix/sysv/linux/sparc/kernel-features.h +++ b/sysdeps/unix/sysv/linux/sparc/kernel-features.h @@ -19,12 +19,6 @@ #include_next -/* 32-bit SPARC kernels do not support - futex_atomic_cmpxchg_inatomic. */ -#if !defined __arch64__ && !defined __sparc_v9__ -# undef __ASSUME_SET_ROBUST_LIST -#endif - /* These syscalls were added for 32-bit in 4.4 (but present for 64-bit in all supported kernel versions); the architecture-independent kernel-features.h assumes some of them to be present by default. From patchwork Tue May 5 19:06:52 2026 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Adhemerval Zanella Netto X-Patchwork-Id: 134505 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from vm01.sourceware.org (localhost [127.0.0.1]) by sourceware.org (Postfix) with ESMTP id EAC684B9DB59 for ; Tue, 5 May 2026 19:09:18 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org EAC684B9DB59 Authentication-Results: sourceware.org; dkim=pass (2048-bit key, unprotected) header.d=linaro.org header.i=@linaro.org header.a=rsa-sha256 header.s=google header.b=AMFu/y49 X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail-vs1-xe2b.google.com (mail-vs1-xe2b.google.com [IPv6:2607:f8b0:4864:20::e2b]) by sourceware.org (Postfix) with ESMTPS id 5E9E74BA79A2 for ; Tue, 5 May 2026 19:08:26 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 5E9E74BA79A2 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=linaro.org Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=linaro.org ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 5E9E74BA79A2 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2607:f8b0:4864:20::e2b ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1778008106; cv=none; b=dVJYK8BKXSkPK4kyQb0jnarin35M0V6cKwpFmsBdsTgrWhwhDnlape9LCqdCezbauGgwj9A1sAE3p3X73QOAhzo6OQbwYodUZLygwrG5I1AeTd5UW6rlqWZgcl2z9YOPbegnuMGKAKB3d19mRLmrYhZkxJMWJ+tn+xZCN99zI+4= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1778008106; c=relaxed/simple; bh=L3pZ5rrC4x2yTQdRKpOevEtZJizWu1R5GAEmoIUUrFo=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=FTR99biuxUvJ/NtiCe0L2zitXtKzC8nloRIpOkxHxyR4af3pT5a6+McCC1D/h0LhC7f0u56DHM+LGFpF4aJYruUriCSzwms/H1Xlwp7O9JZKjjkzXF51JZhuu7g+zKuWQvcIsAhudjT768q5l1BU1HQMWjPHaQ2a99HO6F36Xc8= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 5E9E74BA79A2 Received: by mail-vs1-xe2b.google.com with SMTP id ada2fe7eead31-62f390b8df7so1606684137.1 for ; Tue, 05 May 2026 12:08:26 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1778008105; x=1778612905; darn=sourceware.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=jxFmbu9+XBXAKdKF0W4t5VOJl29THVzXXkcLcNbj4Ss=; b=AMFu/y49PzX1z6nfB0l9IE/2A1aAOeRDyV7pz3vReF0/0DyjDUIzgOUPohw8DYvPBL mqdgEf0DQN1kteKhUTE/kaoDYYJSAkyGBBK3GTxXqbNhNvcNAGhwiNHBQNZgvTJcpJKM RmeY0JF3GlAvnF8FpPgZ45oS4e31kGmqlDtX+MTVZn6q5E+EgI9vqkcAczKGPGjF22SI T8Zi9+pjmc4/g+cTUJ9UiSb8+xeMVufPBGLEE9UmVaZQanTAxQA/k2eV6K2wpWlXUAYg klRvp9DO4b/IPlveJxzb79Jka2uTQq41a/9Dn9rAg5FF7QtIz7AGMMt5dG1LsT4MqIhq FWdw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1778008105; x=1778612905; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to; bh=jxFmbu9+XBXAKdKF0W4t5VOJl29THVzXXkcLcNbj4Ss=; b=s9/rG3HV4P1l1k3pwzdQjOV/9+ZLybuz8iWJoc/rLRLzIJN7adLzQ6Q9XzutjcMvX2 vRiwKoAFGhhlGzBF3qz0gQI7FWc7JecFX1ZZ6tgEaMoxnBHhJTlm/549UPrtZsD9XCJR yq+tS5JV4BJabVuw8I2OciJwyn1ipTJmSyDEHV80sEsO3eBdt9Zrof15IiZbwt8IoT+N nnXehISBFsrn6/aYQ+fo9OlB7y91Y2twLSSeD4oDPVgBabg4Vc9Xi6YMwF9u22gjsCTw IS33ulvhLeYwwtYPCQAW+GH68SZhPyweYb5w7OZYiQ6gk1agQZPjjn/MwH/Tk2tI5Ez5 xwXg== X-Gm-Message-State: AOJu0Yw6xPaXNtiMlREfhD6FANXVTnb7KKSTpQrUyC14mhHRJIhkdcQ/ NN5RAy+utO+cSJRMKtFMCmRAcmcLsSobAx08zH7KIF22gETv0cIBcS2Di+XHGL60ZFZV1dT4cMA DAGEc X-Gm-Gg: AeBDieseoYIbU1romLIXF+HLbw/G4iBsMnIWGCKlg/EsHeiEACSXnAd0f7Q7hfj2bnV e7Olw8bItrzFd9GWtBQi0DOM6gmxvNIH2gr0fXY7/TdJqYfOktL4zg2ASSaFArit+3sxKX31uyh xr2MfM7kj4xJGZQ9P6B/css43Ld43zaMQYhlUb1tPSoJ1X/ugpCP79Kzv60PH9L0R0/ZF/Cyc9K Ywg4mKtvW967O/vWATeVx0ne+sF3gGtESiQ4LEFGC1zP3jGBaE3qLwngJVTxchdrXNqBXhtnaW6 isZWqkBFZB1DtIcre9WoNm5T6WX6BoXl/z0ghxjI/K9DaCDDG9qA8MugVvHxqVOCGVeMhWoupeq 9K+BAOx9bcP115tseE4iqJyW9RDdVjCzhLEpHz9j0Kk8JhoVAh3ieVN52vQw5UFSgbdST7WDh3T AQ1sG31ZD/jPsiHPrnj+zVLeHYDSKlAfp8MmYozPNP8Ac= X-Received: by 2002:a05:6102:802a:b0:618:3503:5659 with SMTP id ada2fe7eead31-62f591dbcfemr3622245137.14.1778008104395; Tue, 05 May 2026 12:08:24 -0700 (PDT) Received: from mandiga.. ([2804:1b3:a7c1:364:abee:44ff:b045:5a7]) by smtp.gmail.com with ESMTPSA id a1e0cc1a2514c-95ce08f4a7asm7504046241.5.2026.05.05.12.08.22 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 05 May 2026 12:08:23 -0700 (PDT) From: Adhemerval Zanella To: libc-alpha@sourceware.org Cc: Florian Weimer Subject: [PATCH v2 3/3] nptl: Only initialize robust list at mutex usage Date: Tue, 5 May 2026 16:06:52 -0300 Message-ID: <20260505190809.3898686-4-adhemerval.zanella@linaro.org> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20260505190809.3898686-1-adhemerval.zanella@linaro.org> References: <20260505190809.3898686-1-adhemerval.zanella@linaro.org> MIME-Version: 1.0 X-Spam-Status: No, score=-12.4 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, KAM_SHORT, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_NONE, TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on sourceware.org X-BeenThere: libc-alpha@sourceware.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Libc-alpha mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: libc-alpha-bounces~patchwork=sourceware.org@sourceware.org The set_robust_list syscall is currently called unconditionally at process startup (__tls_init_tp) and on every thread creation (pthread_create), even in programs that never use robust mutexes. This patch defers the call to the first time a robust mutex is actually locked or initialized with PTHREAD_PROCESS_SHARED. The new helper robust_list_setup() performs the deferred registration and is called from pthread_mutex_init (for pshared+robust), pthread_mutex_lock, pthread_mutex_trylock, and pthread_mutex_timedlock. The field robust_head.futex_offset serves as a sentinel: zero means the list has not been registered with the kernel; non-zero means it has. robust_list_setup() sets futex_offset to its correct value before calling set_robust_list, so the kernel always reads a valid offset when it walks the list on thread death. On syscall failure futex_offset is reset to zero. _Fork is updated to re-register the list with the kernel only when the parent had already initialized it (futex_offset != 0). If the parent never used robust mutexes, the child's first robust mutex lock calls robust_list_setup() itself. The __nptl_set_robust_list_avail variable moves from ld.so to libc.so, since it no longer needs to be set during pthread startup. Checked on x86_64-linux-gnu and aarch64-linux-gnu. --- nptl/Makefile | 2 + nptl/Versions | 1 - nptl/allocatestack.c | 9 +--- nptl/descr.h | 31 ++++++++++++ nptl/nptl_robust_setup.c | 38 +++++++++++++++ nptl/pthread_create.c | 8 --- nptl/pthread_mutex_init.c | 2 +- nptl/pthread_mutex_lock.c | 2 + nptl/pthread_mutex_timedlock.c | 2 + nptl/pthread_mutex_trylock.c | 2 + nptl/tst-robust-pshared.c | 89 ++++++++++++++++++++++++++++++++++ sysdeps/nptl/_Fork.c | 24 ++++----- sysdeps/nptl/dl-tls_init_tp.c | 17 +------ sysdeps/nptl/pthreadP.h | 6 +-- 14 files changed, 183 insertions(+), 50 deletions(-) create mode 100644 nptl/nptl_robust_setup.c create mode 100644 nptl/tst-robust-pshared.c diff --git a/nptl/Makefile b/nptl/Makefile index 02862d1c04b..c9c7fc00f55 100644 --- a/nptl/Makefile +++ b/nptl/Makefile @@ -50,6 +50,7 @@ routines = \ nptl_deallocate_tsd \ nptl_free_tcb \ nptl_nthreads \ + nptl_robust_setup \ nptl_setxid \ nptlfreeres \ old_pthread_cond_broadcast \ @@ -326,6 +327,7 @@ tests = \ tst-pthread_exit-nothreads-static \ tst-pthread_gettid_np \ tst-robust-fork \ + tst-robust-pshared \ tst-robustpi1 \ tst-robustpi2 \ tst-robustpi3 \ diff --git a/nptl/Versions b/nptl/Versions index b813b675b91..94a567bd609 100644 --- a/nptl/Versions +++ b/nptl/Versions @@ -535,6 +535,5 @@ libpthread { ld { GLIBC_PRIVATE { __nptl_initial_report_events; - __nptl_set_robust_list_avail; } } diff --git a/nptl/allocatestack.c b/nptl/allocatestack.c index b2ecb001136..fcccc75ddb8 100644 --- a/nptl/allocatestack.c +++ b/nptl/allocatestack.c @@ -631,14 +631,7 @@ allocate_stack (const struct pthread_attr *attr, struct pthread **pdp, /* The robust mutex lists also need to be initialized unconditionally because the cleanup for the previous stack owner might have happened in the kernel. */ - pd->robust_head.futex_offset = (offsetof (pthread_mutex_t, __data.__lock) - - offsetof (pthread_mutex_t, - __data.__list.__next)); - pd->robust_head.list_op_pending = NULL; -#if __PTHREAD_MUTEX_HAVE_PREV - pd->robust_prev = &pd->robust_head; -#endif - pd->robust_head.list = &pd->robust_head; + robust_list_init (pd); /* We place the thread descriptor at the end of the stack. */ *pdp = pd; diff --git a/nptl/descr.h b/nptl/descr.h index 627cc3980f0..308100c0c30 100644 --- a/nptl/descr.h +++ b/nptl/descr.h @@ -458,6 +458,37 @@ cancel_enabled_and_canceled_and_async (int value) == (CANCELTYPE_BITMASK | CANCELED_BITMASK); } +static inline void +robust_list_init (struct pthread *pd) +{ + pd->robust_head.list_op_pending = NULL; +#if __PTHREAD_MUTEX_HAVE_PREV + pd->robust_prev = &pd->robust_head; +#endif + pd->robust_head.list = &pd->robust_head; + pd->robust_head.futex_offset = 0; +} + +extern bool __nptl_robust_setup (struct robust_list_head *robust_head) + attribute_hidden; + +static inline bool +robust_list_setup (struct pthread *pd) +{ + /* The current thread was already initialized. */ + if (pd->robust_head.futex_offset != 0) + return true; + + pd->robust_head.futex_offset = (offsetof (pthread_mutex_t, __data.__lock) + - offsetof (pthread_mutex_t, + __data.__list.__next)); + if (__nptl_robust_setup (&pd->robust_head)) + return true; + + pd->robust_head.futex_offset = 0; + return false; +} + /* This yields the pointer that TLS support code calls the thread pointer. */ #if TLS_TCB_AT_TP # define TLS_TPADJ(pd) (pd) diff --git a/nptl/nptl_robust_setup.c b/nptl/nptl_robust_setup.c new file mode 100644 index 00000000000..0b95de22009 --- /dev/null +++ b/nptl/nptl_robust_setup.c @@ -0,0 +1,38 @@ +/* Linux robust mutex setup. + Copyright (C) 2025 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + The GNU C Library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + The GNU C Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with the GNU C Library; if not, see + . */ + +#include +#include +#include + +int __nptl_set_robust_list_avail = 1; + +bool +__nptl_robust_setup (struct robust_list_head *robust_head) +{ + if (atomic_load_relaxed (&__nptl_set_robust_list_avail)) + { + int res = INTERNAL_SYSCALL_CALL (set_robust_list, robust_head, + sizeof (struct robust_list_head)); + if (!INTERNAL_SYSCALL_ERROR_P (res)) + return true; + + atomic_store_relaxed (&__nptl_set_robust_list_avail, 0); + } + return false; +} diff --git a/nptl/pthread_create.c b/nptl/pthread_create.c index 72f77d1914e..d7aed06efda 100644 --- a/nptl/pthread_create.c +++ b/nptl/pthread_create.c @@ -386,14 +386,6 @@ start_thread (void *arg) __libc_fatal ("Fatal glibc error: rseq registration failed\n"); } - if (__nptl_set_robust_list_avail) - { - /* This call should never fail because the initial call in init.c - succeeded. */ - INTERNAL_SYSCALL_CALL (set_robust_list, &pd->robust_head, - sizeof (struct robust_list_head)); - } - /* This is where the try/finally block should be created. For compilers without that support we do use setjmp. */ struct pthread_unwind_buf unwind_buf; diff --git a/nptl/pthread_mutex_init.c b/nptl/pthread_mutex_init.c index 9be08332f1f..2f0bb84c557 100644 --- a/nptl/pthread_mutex_init.c +++ b/nptl/pthread_mutex_init.c @@ -94,7 +94,7 @@ ___pthread_mutex_init (pthread_mutex_t *mutex, if ((imutexattr->mutexkind & PTHREAD_MUTEXATTR_FLAG_ROBUST) != 0) { if ((imutexattr->mutexkind & PTHREAD_MUTEXATTR_FLAG_PSHARED) != 0 - && !__nptl_set_robust_list_avail) + && !robust_list_setup (THREAD_SELF)) return ENOTSUP; mutex_kind |= PTHREAD_MUTEX_ROBUST_NORMAL_NP; } diff --git a/nptl/pthread_mutex_lock.c b/nptl/pthread_mutex_lock.c index a697f2b6ca8..f649036d954 100644 --- a/nptl/pthread_mutex_lock.c +++ b/nptl/pthread_mutex_lock.c @@ -177,6 +177,7 @@ __pthread_mutex_lock_full (pthread_mutex_t *mutex) case PTHREAD_MUTEX_ROBUST_ERRORCHECK_NP: case PTHREAD_MUTEX_ROBUST_NORMAL_NP: case PTHREAD_MUTEX_ROBUST_ADAPTIVE_NP: + robust_list_setup (THREAD_SELF); THREAD_SETMEM (THREAD_SELF, robust_head.list_op_pending, &mutex->__data.__list.__next); /* We need to set op_pending before starting the operation. Also @@ -361,6 +362,7 @@ __pthread_mutex_lock_full (pthread_mutex_t *mutex) if (robust) { + robust_list_setup (THREAD_SELF); /* Note: robust PI futexes are signaled by setting bit 0. */ THREAD_SETMEM (THREAD_SELF, robust_head.list_op_pending, (void *) (((uintptr_t) &mutex->__data.__list.__next) diff --git a/nptl/pthread_mutex_timedlock.c b/nptl/pthread_mutex_timedlock.c index 9efca2c7791..a52ed795df0 100644 --- a/nptl/pthread_mutex_timedlock.c +++ b/nptl/pthread_mutex_timedlock.c @@ -111,6 +111,7 @@ __pthread_mutex_clocklock_common (pthread_mutex_t *mutex, case PTHREAD_MUTEX_ROBUST_ERRORCHECK_NP: case PTHREAD_MUTEX_ROBUST_NORMAL_NP: case PTHREAD_MUTEX_ROBUST_ADAPTIVE_NP: + robust_list_setup (THREAD_SELF); THREAD_SETMEM (THREAD_SELF, robust_head.list_op_pending, &mutex->__data.__list.__next); /* We need to set op_pending before starting the operation. Also @@ -295,6 +296,7 @@ __pthread_mutex_clocklock_common (pthread_mutex_t *mutex, if (robust) { + robust_list_setup (THREAD_SELF); /* Note: robust PI futexes are signaled by setting bit 0. */ THREAD_SETMEM (THREAD_SELF, robust_head.list_op_pending, (void *) (((uintptr_t) &mutex->__data.__list.__next) diff --git a/nptl/pthread_mutex_trylock.c b/nptl/pthread_mutex_trylock.c index 236b3228ddb..09c5dc983b2 100644 --- a/nptl/pthread_mutex_trylock.c +++ b/nptl/pthread_mutex_trylock.c @@ -77,6 +77,7 @@ ___pthread_mutex_trylock (pthread_mutex_t *mutex) case PTHREAD_MUTEX_ROBUST_ERRORCHECK_NP: case PTHREAD_MUTEX_ROBUST_NORMAL_NP: case PTHREAD_MUTEX_ROBUST_ADAPTIVE_NP: + robust_list_setup (THREAD_SELF); THREAD_SETMEM (THREAD_SELF, robust_head.list_op_pending, &mutex->__data.__list.__next); /* We need to set op_pending before starting the operation. Also @@ -219,6 +220,7 @@ ___pthread_mutex_trylock (pthread_mutex_t *mutex) if (robust) { + robust_list_setup (THREAD_SELF); /* Note: robust PI futexes are signaled by setting bit 0. */ THREAD_SETMEM (THREAD_SELF, robust_head.list_op_pending, (void *) (((uintptr_t) &mutex->__data.__list.__next) diff --git a/nptl/tst-robust-pshared.c b/nptl/tst-robust-pshared.c new file mode 100644 index 00000000000..4074ffa494b --- /dev/null +++ b/nptl/tst-robust-pshared.c @@ -0,0 +1,89 @@ +/* Test process-shared robust mutex support and lazy initialization (BZ 33225). + Copyright (C) 2025 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + The GNU C Library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + The GNU C Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with the GNU C Library; if not, see + . */ + +/* Verify that pthread_mutex_init returns ENOTSUP for a process-shared robust + mutex when the set_robust_list syscall is not available (e.g., qemu-user), + and that the lazy robust list initialization works correctly when the + syscall is available. */ + +#include +#include +#include +#include +#include +#include +#include + +/* Lock the mutex and exit without unlocking to trigger EOWNERDEAD. */ +static void * +owner_thread (void *arg) +{ + pthread_mutex_t *mutex = arg; + TEST_COMPARE (pthread_mutex_lock (mutex), 0); + /* Thread exits here without unlocking. The kernel walks the robust + list registered via set_robust_list and marks the mutex owner-dead. + This verifies that lazy robust_list_setup correctly set futex_offset + and called set_robust_list before the first lock. */ + return NULL; +} + +static int +do_test (void) +{ + bool robust_support = support_process_shared_robust_mutex (); + if (test_verbose) + printf ("info: process-shared robust mutex support: %d\n", robust_support); + + pthread_mutexattr_t attr; + TEST_COMPARE (pthread_mutexattr_init (&attr), 0); + TEST_COMPARE (pthread_mutexattr_setpshared (&attr, PTHREAD_PROCESS_SHARED), + 0); + TEST_COMPARE (pthread_mutexattr_setrobust (&attr, PTHREAD_MUTEX_ROBUST), 0); + + pthread_mutex_t mutex; + int ret = pthread_mutex_init (&mutex, &attr); + TEST_COMPARE (pthread_mutexattr_destroy (&attr), 0); + + if (!robust_support) + { + /* When set_robust_list is unavailable, pshared+robust mutex init + must fail with ENOTSUP rather than silently succeeding. */ + TEST_COMPARE (ret, ENOTSUP); + return 0; + } + + TEST_COMPARE (ret, 0); + + /* Have a thread lock the mutex and exit without unlocking. + This exercises the lazy robust_list_setup path in + pthread_mutex_lock: futex_offset must be set before set_robust_list + is called so the kernel can correctly compute the lock address on + thread death. */ + pthread_t thread; + TEST_COMPARE (pthread_create (&thread, NULL, owner_thread, &mutex), 0); + TEST_COMPARE (pthread_join (thread, NULL), 0); + + TEST_COMPARE (pthread_mutex_lock (&mutex), EOWNERDEAD); + TEST_COMPARE (pthread_mutex_consistent (&mutex), 0); + TEST_COMPARE (pthread_mutex_unlock (&mutex), 0); + TEST_COMPARE (pthread_mutex_destroy (&mutex), 0); + + return 0; +} + +#include diff --git a/sysdeps/nptl/_Fork.c b/sysdeps/nptl/_Fork.c index 907b3fef876..ba6912aa346 100644 --- a/sysdeps/nptl/_Fork.c +++ b/sysdeps/nptl/_Fork.c @@ -35,22 +35,22 @@ _Fork (void) { struct pthread *self = THREAD_SELF; - /* Initialize the robust mutex list setting in the kernel which has - been reset during the fork. We do not check for errors because if - it fails here, it must have failed at process startup as well and - nobody could have used robust mutexes. - Before we do that, we have to clear the list of robust mutexes - because we do not inherit ownership of mutexes from the parent. - We do not have to set self->robust_head.futex_offset since we do - inherit the correct value from the parent. We do not need to clear - the pending operation because it must have been zero when fork was - called. */ + /* Clear the list of robust mutexes because we do not inherit ownership + of mutexes from the parent. We do not need to clear the pending + operation because it must have been zero when fork was called. + futex_offset is inherited from the parent unchanged. */ #if __PTHREAD_MUTEX_HAVE_PREV self->robust_prev = &self->robust_head; #endif self->robust_head.list = &self->robust_head; - INTERNAL_SYSCALL_CALL (set_robust_list, &self->robust_head, - sizeof (struct robust_list_head)); + /* Re-register the robust list with the kernel only if the parent had + already initialized it. futex_offset is the sentinel: zero means + lazy initialization has not happened yet, so there is nothing to + re-register and the first robust mutex lock in the child will call + set_robust_list itself. */ + if (self->robust_head.futex_offset != 0) + INTERNAL_SYSCALL_CALL (set_robust_list, &self->robust_head, + sizeof (struct robust_list_head)); call_function_static_weak (__getrandom_fork_subprocess); } diff --git a/sysdeps/nptl/dl-tls_init_tp.c b/sysdeps/nptl/dl-tls_init_tp.c index 75e3712a6d8..77f2e341822 100644 --- a/sysdeps/nptl/dl-tls_init_tp.c +++ b/sysdeps/nptl/dl-tls_init_tp.c @@ -28,9 +28,6 @@ #define TUNABLE_NAMESPACE pthread #include -bool __nptl_set_robust_list_avail; -rtld_hidden_data_def (__nptl_set_robust_list_avail) - bool __nptl_initial_report_events; rtld_hidden_def (__nptl_initial_report_events) @@ -82,19 +79,7 @@ __tls_init_tp (void) THREAD_SETMEM (pd, report_events, __nptl_initial_report_events); /* Initialize the robust mutex data. */ - { -#if __PTHREAD_MUTEX_HAVE_PREV - pd->robust_prev = &pd->robust_head; -#endif - pd->robust_head.list = &pd->robust_head; - pd->robust_head.futex_offset = (offsetof (pthread_mutex_t, __data.__lock) - - offsetof (pthread_mutex_t, - __data.__list.__next)); - int res = INTERNAL_SYSCALL_CALL (set_robust_list, &pd->robust_head, - sizeof (struct robust_list_head)); - if (!INTERNAL_SYSCALL_ERROR_P (res)) - __nptl_set_robust_list_avail = true; - } + robust_list_init (pd); { /* If the registration fails or is disabled by tunable, the public diff --git a/sysdeps/nptl/pthreadP.h b/sysdeps/nptl/pthreadP.h index c62c8982905..38a1383c3fe 100644 --- a/sysdeps/nptl/pthreadP.h +++ b/sysdeps/nptl/pthreadP.h @@ -192,10 +192,8 @@ libc_hidden_proto (__pthread_keys) extern unsigned int __nptl_nthreads; libc_hidden_proto (__nptl_nthreads) -/* True if the set_robust_list system call works. Initialized in - __tls_init_tp. */ -extern bool __nptl_set_robust_list_avail; -rtld_hidden_proto (__nptl_set_robust_list_avail) +/* Set if the set_robust_list system call works. */ +extern int __nptl_set_robust_list_avail attribute_hidden; /* Thread Priority Protection. */ extern int __sched_fifo_min_prio;