From patchwork Wed Mar 12 14:05:14 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Adhemerval Zanella X-Patchwork-Id: 872833 Delivered-To: patch@linaro.org Received: by 2002:a5d:64e7:0:b0:38f:210b:807b with SMTP id g7csp301999wri; Wed, 12 Mar 2025 07:06:07 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCVATnC0OluH2ExPyzbKqdNvuEuNuirnp29OcEpuyQbhTb4M1mqnCjCcW46efA6+NV8SJ/McPg==@linaro.org X-Google-Smtp-Source: AGHT+IG4waA4dOa2MORwtb4KyXrQ+xbQ3vaqZUl2l+5UPRnaRGMISuj+0KleO37T0OnOGoUY4M1i X-Received: by 2002:a05:6122:1d07:b0:50d:39aa:7881 with SMTP id 71dfb90a1353d-524194aa47cmr6531879e0c.0.1741788367071; Wed, 12 Mar 2025 07:06:07 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1741788367; cv=pass; d=google.com; s=arc-20240605; b=fALJgLWJGRun9TJqsFb0wcumNCGTO2wQQDi/dItuhd6ey3dx+58tuw525BakMipE7I tb0vWEXtOfIWu+ogSjjOfzcfFiOrPwtoEfw68RGleOpCLxk33557EdW165bG5JAvsZbn p1I7KGceKR7kJIXKVkhe9Aepvf54HM3tWqSVdhXNp61MYOnbHPAUFxoCVQoKn9dtlpDN 12UjtnCfSp7cB6C6ZwwXerW/jhkk0jBdtn8acfX5d3ptpSxPoIQrPxdLOJkWjXR40vTY sJ7MI6Ljfz6Hs+TvPl7I7hHdcIPKy75t3710DOv4nXyXNY5CkSfnm8wq5SCYyzsOFLyR b6/g== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20240605; h=errors-to:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:content-transfer-encoding :mime-version:message-id:date:subject:cc:to:from:dkim-signature :dkim-filter:arc-filter:dmarc-filter:delivered-to:dkim-filter; bh=+EYtJ3nrkJ0cjTOMHUFch1PakJ+CAx5U3r1xFNGlspk=; fh=UCVZ7S1lBcEAg62ZuvyIjHIDpE1rX0WJm+RYA5LwILk=; b=NHCFOWKJ1Ld0YzgkdCcF74r3CHRVlI+At/d/iiA+9rspNMSuCQnc0G/lavIHcGKWA8 ukx5/8TbPlNAl2v3vKH/cfWS0BNSrQR+NJiyW3ggDVG9k5+vDq9Eg0gQJyADDx07PRw0 n9VD5rMlOjPv2gctCAiOdkpaQHJDp1FRx/7GhL/Ea10mr5xdPHVf1QZAALSMVhOOcD0u W3pyNPBbSDk/MeMffSploH7NB9uKP4oKoUtEI7Z/rmHd9/ZsD+ao2M9wgO/vHtyO3AwE QNiayNxfnBw6vpUbw7atbj29ql5Vdi+SADFu/MGBy8QpHHevmJLKQNFoWC1133qn0uUP BVDw==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=LC89yzsM; arc=pass (i=1); spf=pass (google.com: domain of libc-alpha-bounces~patch=linaro.org@sourceware.org designates 8.43.85.97 as permitted sender) smtp.mailfrom="libc-alpha-bounces~patch=linaro.org@sourceware.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from server2.sourceware.org (server2.sourceware.org. [8.43.85.97]) by mx.google.com with ESMTPS id 71dfb90a1353d-523d8a85298si2907126e0c.16.2025.03.12.07.06.06 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 12 Mar 2025 07:06:07 -0700 (PDT) Received-SPF: pass (google.com: domain of libc-alpha-bounces~patch=linaro.org@sourceware.org designates 8.43.85.97 as permitted sender) client-ip=8.43.85.97; Authentication-Results: mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=LC89yzsM; arc=pass (i=1); spf=pass (google.com: domain of libc-alpha-bounces~patch=linaro.org@sourceware.org designates 8.43.85.97 as permitted sender) smtp.mailfrom="libc-alpha-bounces~patch=linaro.org@sourceware.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 5D6393858430 for ; Wed, 12 Mar 2025 14:06:06 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 5D6393858430 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=LC89yzsM X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail-pl1-x62a.google.com (mail-pl1-x62a.google.com [IPv6:2607:f8b0:4864:20::62a]) by sourceware.org (Postfix) with ESMTPS id 6E8653858C5F for ; Wed, 12 Mar 2025 14:05:50 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 6E8653858C5F 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 6E8653858C5F Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2607:f8b0:4864:20::62a ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1741788350; cv=none; b=JkIQWJWOqbFT45Z/toKhISMI2/2m+K6t+vSLWMJ4PM0+MoK5XUgkcINZfUExKo5WKN5gLVEQKEkiPdcmbKDmlSKUImV6tkvdx8jsQ9t63bnhpKt90C7TtJ3enQsc6GiJxCc637+yHUcQBAr1y0OtZkw1w4zBj7DL3TGQRbTMwbE= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1741788350; c=relaxed/simple; bh=bZWTOkeV0asaHtk6B+0XSY3SQtRmMHymvaANVGD8J9U=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=UC2GznEUlWYwQc5jnYBij+0Ip5HVWlAcWnBtwT/dxEgEGxBGP+ulmOdmpXUrW8BKPl9lQsCAZT56muQmAJ2PRLI3lEei6HR9whg2PGKYV/z1CfAaRAQUCO5s70slKu1iv/gEO+BypRN4dB+pabLFagQd76avD0NXEKv0icuegKU= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 6E8653858C5F Received: by mail-pl1-x62a.google.com with SMTP id d9443c01a7336-223f4c06e9fso15796935ad.1 for ; Wed, 12 Mar 2025 07:05:50 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1741788349; x=1742393149; darn=sourceware.org; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=+EYtJ3nrkJ0cjTOMHUFch1PakJ+CAx5U3r1xFNGlspk=; b=LC89yzsMYUZh570OFUp64i9aNtMSqudX/LQZ/jvmoJTveQJ02FpXI82VdM42bSetuC VRpFSM3KEdS11HY3y/HCZIQLJqld5Th9iT4SsL6uCYnyZnv7Kly10+pdlironAfGd5GX cj3VM7U6KocGuxpExJCBjUKlsQ2CCzLHmvRlR6Zw1ozwz8odgtnvDOsVvn7uj/9SeqW/ M0OK4sNUVAwrlEZiLjg5bMFb09OorMRZvGVy7MXdQtanDxbb0u8cKLCechklfzxotMMw EKAdDnug9sy+h6BKLjiliG3mgDSdO93BL0vPJhVIUn5fxFjB6p3HAxwDrtmXLqJbsTvC gi3w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1741788349; x=1742393149; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=+EYtJ3nrkJ0cjTOMHUFch1PakJ+CAx5U3r1xFNGlspk=; b=knKXzrCVbBCdl3BJWVjzd3f/qGSfWQ3gYOgY1wdmXrLC1tTNFyXemgzeZEZqNeQ/Xt Bi7VWAlJFee6uqZeF/E4LwlROlqnIOCgS2D0K1c+PV60CnmlK+ZA/4d0zHbqtJ5ixJwG Gca32t37gPPCuA2tVDKLzGijcmJh3P4Ef1qJnGpbBUyyvmCE922XKxKx71JxtRuhf8GN cGjypDD1DXlTJL/l+yeuMpfXFhfaC3Xosz78zUAExd9mJHzpIlVOPUX8f9ZtbjSDlO6j NUeJcdpmcHHTuwD+Sm29tz0c2bQ264kyEVEfLZH6a4QtVwRw0f8LuRVJqEF0IA+Im86M h3jw== X-Gm-Message-State: AOJu0YySAhD8bHGvxUzSkgtkKNMuAII7znYq/7+Tx7Kw1KcUNfbpNHs1 J3bc70n8nXUWlB3Ow8x+F9WT/wn7fYW/yiDL85MHcOnd4dIPwPBFCOERz5D4oluhzPucxeNNpVc o X-Gm-Gg: ASbGncsimNm/qGrustTKAldtJbVe2v4V9fCJhtHmS5qunw2RvfBT5g8fIcU04Ko/EHx qxAgJvuDYvchqWibhhN3f2z60oeX/aZWC+e08N5W52SHocU4PHJzapq7Z5aW+pKwuTY1JpmJHTq dN8ifB8Fhbso4VPOEd4wFNN1ztxeX0gn9pm/0+7o6ePtL+/S3TWqTzjV+vx9yCCWhMpwIrWJCUL nSMFFvJGCi1Ix4MuFpJzh+Tx6XAvym0J6kTRSMCehAdXx0VYIyWyC7KDkvCmPoaFlDh1qzd4O+G BtQs43NIAAgtidh8mLFgmtRCXEcocjQ7oXavzQXqXR2UCxNC8ZGhlcWGXg== X-Received: by 2002:a17:902:d502:b0:224:7a4:b31 with SMTP id d9443c01a7336-22593d6ad30mr89722705ad.6.1741788349002; Wed, 12 Mar 2025 07:05:49 -0700 (PDT) Received: from mandiga.. ([2804:1b3:a7c1:1ebf:78b3:c5aa:cdc8:55cd]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-224109ddfbcsm116269455ad.21.2025.03.12.07.05.47 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 12 Mar 2025 07:05:48 -0700 (PDT) From: Adhemerval Zanella To: libc-alpha@sourceware.org Cc: Aurelien Jarno Subject: [PATCH] nptl: Disable asynchronous cancellation on __do_cancel (BZ 32782) Date: Wed, 12 Mar 2025 11:05:14 -0300 Message-ID: <20250312140544.3569550-1-adhemerval.zanella@linaro.org> X-Mailer: git-send-email 2.43.0 MIME-Version: 1.0 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~patch=linaro.org@sourceware.org Similar to __pthread_unwind, called from pthread_exit, once cancellation starts the cancellation signal handler (sigcancel_handler) should not restart the cancellation process (and libgcc unwind is not reentrant). So also disables asynchronous cancellation on __do_cancel, any cancellation signal received after it is ignored (cancel_async_enabled will return false). Checked on x86_64-linux-gnu and aarch64-linux-gnu. --- sysdeps/nptl/pthreadP.h | 12 +++++- sysdeps/pthread/Makefile | 1 + sysdeps/pthread/tst-cancel32.c | 73 ++++++++++++++++++++++++++++++++++ 3 files changed, 84 insertions(+), 2 deletions(-) create mode 100644 sysdeps/pthread/tst-cancel32.c diff --git a/sysdeps/nptl/pthreadP.h b/sysdeps/nptl/pthreadP.h index 2d620ed20d..63de8904f4 100644 --- a/sysdeps/nptl/pthreadP.h +++ b/sysdeps/nptl/pthreadP.h @@ -267,8 +267,16 @@ __do_cancel (void *result) self->result = result; - /* Make sure we get no more cancellations. */ - atomic_fetch_or_relaxed (&self->cancelhandling, EXITING_BITMASK); + /* Disable asynchronous cancellation and signal that thread is exiting. */ + int cancelhandling = atomic_load_relaxed (&self->cancelhandling); + int newval; + do + { + newval = (cancelhandling & ~CANCELTYPE_BITMASK) | EXITING_BITMASK; + } + while (!atomic_compare_exchange_weak_acquire (&self->cancelhandling, + &cancelhandling, + newval)); __pthread_unwind ((__pthread_unwind_buf_t *) THREAD_GETMEM (self, cleanup_jmp_buf)); diff --git a/sysdeps/pthread/Makefile b/sysdeps/pthread/Makefile index 70e62b2e1b..d4869c624b 100644 --- a/sysdeps/pthread/Makefile +++ b/sysdeps/pthread/Makefile @@ -106,6 +106,7 @@ tests += \ tst-cancel28 \ tst-cancel29 \ tst-cancel30 \ + tst-cancel32 \ tst-cleanup0 \ tst-cleanup1 \ tst-cleanup2 \ diff --git a/sysdeps/pthread/tst-cancel32.c b/sysdeps/pthread/tst-cancel32.c new file mode 100644 index 0000000000..1c6a4d8f47 --- /dev/null +++ b/sysdeps/pthread/tst-cancel32.c @@ -0,0 +1,73 @@ +/* Check if pthread_setcanceltype disables asynchronous cancellation + once cancellation happens (BZ 32782) + + 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 + . */ + +/* The pthread_setcanceltype is a cancellation entrypoint, and if + asynchronous is enabled and the cancellation starts (on the second + pthread_setcanceltype call), the asynchronous should not restart + the process. */ + +#include + +#define NITER 1000 +#define NTHREADS 8 + +static void +tf_cleanup (void *arg) +{ +} + +static void * +tf (void *closure) +{ + pthread_cleanup_push (tf_cleanup, NULL); + for (;;) + { + /* The only failure possible for pthread_setcanceltype is and + invalid state type. */ + pthread_setcanceltype (PTHREAD_CANCEL_ASYNCHRONOUS, NULL); + pthread_setcanceltype (PTHREAD_CANCEL_DEFERRED, NULL); + } + pthread_cleanup_pop (1); + + return NULL; +} + +static void +poll_threads (int nthreads) +{ + pthread_t thr[nthreads]; + for (int i = 0; i < nthreads; i++) + thr[i] = xpthread_create (NULL, tf, NULL); + for (int i = 0; i < nthreads; i++) + xpthread_cancel (thr[i]); + for (int i = 0; i < nthreads; i++) + xpthread_join (thr[i]); +} + +static int +do_test (void) +{ + for (int k = 0; k < NITER; k++) + poll_threads (NTHREADS); + + return 0; +} + +#include