From patchwork Wed May 26 16:57:28 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Adhemerval Zanella Netto X-Patchwork-Id: 43599 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 2ACA6394740E; Wed, 26 May 2021 16:57:51 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 2ACA6394740E DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1622048271; bh=XKzL+yIVHNKhmPjjXm5ei1c+LgrwxqUSmVEWOme9dss=; h=To:Subject:Date:In-Reply-To:References:List-Id:List-Unsubscribe: List-Archive:List-Post:List-Help:List-Subscribe:From:Reply-To: From; b=uS7wkrJu/Ixb5LxJprT7YlBSepNiU1eKwVtH6CnU4D1FhrE4EETKFsimDyraEUEUz oDAY/0mPnnpxZIvEI6AzpIDFaKGbXNWeGq4oWdg2H4IAg48XIBpiO5KLgAaC8Ait43 BFkthnVBovO4VcN4o3iPPW9zmoNxuehnCAKg0i0A= X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail-qt1-x830.google.com (mail-qt1-x830.google.com [IPv6:2607:f8b0:4864:20::830]) by sourceware.org (Postfix) with ESMTPS id EF7633947433 for ; Wed, 26 May 2021 16:57:47 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.3.2 sourceware.org EF7633947433 Received: by mail-qt1-x830.google.com with SMTP id a15so1303045qta.0 for ; Wed, 26 May 2021 09:57:47 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=XKzL+yIVHNKhmPjjXm5ei1c+LgrwxqUSmVEWOme9dss=; b=s+sPlM/7FkaL7qX4qc0STtXCsUE2NFPitzJ7X2XCap6fer4LMR5xW2U/K+MJVD+FZa siTYKX7Yixu9j6BdMsFkHd1QD/HxqHKxP/lNxP4ii1uXvKZEPyy1szs1MdKmTAYrNOwR 71ASaiJ1YSjbEwZYMy+ObD6Tf49LlNuOSnsiobeYwPuWuL9jUGXIBcrt0qz1iYzCVYji jUvcFJ0pKbww5mU+WxScL0t+DrGHYsgmBTTQWFm2Opi9MFq8hc4KgTsqLKtV6wimYDpn JKcbK4osfyPoTftq1ffMaf2lVc0fMoM7J5492Yb8ByFQ7nHnbRTqzvRYApt30q4EjNKZ o1uA== X-Gm-Message-State: AOAM531IhLIgLUPz8JTk9H0eW0UXHf9Kv0t433t6/GO+W7KKS6X1+SmW T1HzqN11t8jCM9YOCnuM44NVUmSawKsPMA== X-Google-Smtp-Source: ABdhPJxxN1knVpvSZF5c7xYVk7oYhl70OQ9LWuIGuAZKVsVd2mQDhPpQ9/NRupe2FBNOCCUrfWzOLQ== X-Received: by 2002:a05:622a:210:: with SMTP id b16mr38168916qtx.51.1622048267325; Wed, 26 May 2021 09:57:47 -0700 (PDT) Received: from birita.. ([177.194.37.86]) by smtp.googlemail.com with ESMTPSA id k125sm1859761qkf.53.2021.05.26.09.57.46 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 26 May 2021 09:57:47 -0700 (PDT) To: libc-alpha@sourceware.org Subject: [PATCH 11/11] nptl: Use pthread_kill on pthread_cancel Date: Wed, 26 May 2021 13:57:28 -0300 Message-Id: <20210526165728.1772546-12-adhemerval.zanella@linaro.org> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20210526165728.1772546-1-adhemerval.zanella@linaro.org> References: <20210526165728.1772546-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_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.2 X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) 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-Patchwork-Original-From: Adhemerval Zanella via Libc-alpha From: Adhemerval Zanella Netto Reply-To: Adhemerval Zanella Errors-To: libc-alpha-bounces@sourceware.org Sender: "Libc-alpha" It consolidates the tgkill call and it is the first step of making pthread_cancel async-signal-safe. It also fix a possible issue where the 'struct pthread' tid is not read atomically, which might send an invalid cancellation signal (similar to what db988e50a87f613cb6b9e98a2fc66a4848bc3546 fixed for pthread_join). Checked on x86_64-linux-gnu and aarch64-linux-gnu. --- nptl/pthreadP.h | 2 ++ nptl/pthread_cancel.c | 6 +----- nptl/pthread_kill.c | 18 ++++++++++++------ 3 files changed, 15 insertions(+), 11 deletions(-) diff --git a/nptl/pthreadP.h b/nptl/pthreadP.h index 618922f47a..675d1de753 100644 --- a/nptl/pthreadP.h +++ b/nptl/pthreadP.h @@ -515,6 +515,8 @@ libc_hidden_proto (__pthread_detach) extern int __pthread_kill (pthread_t threadid, int signo); libc_hidden_proto (__pthread_kill) extern int __pthread_cancel (pthread_t th); +extern int __pthread_kill_internal (pthread_t threadid, int signo) + attribute_hidden; extern void __pthread_exit (void *value) __attribute__ ((__noreturn__)); libc_hidden_proto (__pthread_exit) extern int __pthread_join (pthread_t threadid, void **thread_return); diff --git a/nptl/pthread_cancel.c b/nptl/pthread_cancel.c index 3be3d0fce7..e6dc135e9b 100644 --- a/nptl/pthread_cancel.c +++ b/nptl/pthread_cancel.c @@ -109,11 +109,7 @@ __pthread_cancel (pthread_t th) return 0; } - pid_t pid = __getpid (); - int val = INTERNAL_SYSCALL_CALL (tgkill, pid, pd->tid, SIGCANCEL); - return INTERNAL_SYSCALL_ERROR_P (val) - ? INTERNAL_SYSCALL_ERRNO (val) - : 0; + return __pthread_kill_internal (th, SIGCANCEL); } versioned_symbol (libc, __pthread_cancel, pthread_cancel, GLIBC_2_34); diff --git a/nptl/pthread_kill.c b/nptl/pthread_kill.c index 0392c3f9e5..93387dd60a 100644 --- a/nptl/pthread_kill.c +++ b/nptl/pthread_kill.c @@ -21,13 +21,8 @@ #include int -__pthread_kill (pthread_t threadid, int signo) +__pthread_kill_internal (pthread_t threadid, int signo) { - /* Disallow sending the signal we use for cancellation, timers, - for the setxid implementation. */ - if (__is_internal_signal (signo)) - return EINVAL; - pid_t tid; struct pthread *pd = (struct pthread *) threadid; @@ -53,6 +48,17 @@ __pthread_kill (pthread_t threadid, int signo) return val; } + +int +__pthread_kill (pthread_t threadid, int signo) +{ + /* Disallow sending the signal we use for cancellation, timers, + for the setxid implementation. */ + if (__is_internal_signal (signo)) + return EINVAL; + + return __pthread_kill_internal (threadid, signo); +} /* Some architectures (for instance arm) might pull raise through libgcc, so avoid the symbol version if it ends up being used on ld.so. */ #if !IS_IN(rtld)