From patchwork Thu May 27 17:28:22 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: 43615 X-Patchwork-Delegate: fweimer@redhat.com 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 3002E395A00A; Thu, 27 May 2021 17:28:42 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 3002E395A00A DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1622136522; bh=1DeyDCWUQ+3Y4HyRrOjhBvzTZqYpLVc25zluV7cBJhE=; 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=ANgxolDWwELo2Zr+o2/Lazf8sGBjyg8mKq0GuXFOooqoKT+hfwKqP502Q4pbs8PVy TkrMlkVpumyEZVnE8aWJhtAYOs7fkhuAlpJBlzw2S9VlNVWf5jML6a01KjK4Av8TtL S1/G3Br6Wazm+gAlPAeTAJIpVmTV0snCfG3F6yfY= X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail-qt1-x82f.google.com (mail-qt1-x82f.google.com [IPv6:2607:f8b0:4864:20::82f]) by sourceware.org (Postfix) with ESMTPS id CA586395B41A for ; Thu, 27 May 2021 17:28:38 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.3.2 sourceware.org CA586395B41A Received: by mail-qt1-x82f.google.com with SMTP id k19so800422qta.2 for ; Thu, 27 May 2021 10:28:38 -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=1DeyDCWUQ+3Y4HyRrOjhBvzTZqYpLVc25zluV7cBJhE=; b=ENHo59hEO1YFlm7nnh7Ief091PM0ZutAgOCxegDu3vwrTxbAe5ICY3czKCTn7kL3+9 ZfDvbfntlfQ5S9/K7tTmKNKbcUKOvd13/7xaMAy755Cq74GEzniL7LgsU4lYSR0YGJBA wEr7zMP5WM9hRg007DSntUjktyFqbgxFVBYnoISW3h7ANmzaCWFnRPJalqfWr+AbGlYk hOqVOPoIDvEsgjlT+fa4LgmqX4EFjQs+NNepj/Xv0hO5KtV+ZTIU+t1SKXpqFDYm7G94 3Be8D6Jvf0vFSlF9bCZ8CDgHwkl86swXgy3zkwIUMOhCfu223mVaPql/HPrJhNPMi1cc Fl/A== X-Gm-Message-State: AOAM533ehBGy38saHKR2gEqJtY2kcamjs8S7UrGBbgnTG8qS/ZDo/Phx iePy4pue+j1ol6lmlG79m2Z2CYweWV2Hxg== X-Google-Smtp-Source: ABdhPJxzqeS9wkBXEOgL34P+uDEc7uIE23H1G3wxTrw1DXIDmHQmJTSrwKObDWyljdYoLrtAxF7AJA== X-Received: by 2002:ac8:65d8:: with SMTP id t24mr4210026qto.332.1622136518205; Thu, 27 May 2021 10:28:38 -0700 (PDT) Received: from birita.. ([177.194.37.86]) by smtp.googlemail.com with ESMTPSA id 25sm1767913qtd.51.2021.05.27.10.28.37 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 27 May 2021 10:28:37 -0700 (PDT) To: libc-alpha@sourceware.org Subject: [PATCH v2 8/9] nptl: Use pthread_kill on pthread_cancel Date: Thu, 27 May 2021 14:28:22 -0300 Message-Id: <20210527172823.3461314-9-adhemerval.zanella@linaro.org> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20210527172823.3461314-1-adhemerval.zanella@linaro.org> References: <20210527172823.3461314-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 c6086b6c3c..5fae763861 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)