From patchwork Mon May 3 21:00:00 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: 43237 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 5E315395445E; Mon, 3 May 2021 21:00:15 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 5E315395445E DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1620075615; bh=Pkb8B8Czkg7MrwAzKza8XDEeC6ugcqBU+GerD4WiuE8=; h=To:Subject:Date:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=L7ndReijC0MGrRvdt6BWyzDmDYm/JT0qnLQAjll0yaVJUwra0zhdPZsd32G6Gqyjb pCwJBI4ZynQNU/A5RXg0IAib3KbKotV548z0hTHXd27aks9eOv5kYh292CtyV+gxQP aq7+HgLmIXxVRCGqO82Pw4S1X3wZGPvyP6j5/cAY= X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail-qk1-x732.google.com (mail-qk1-x732.google.com [IPv6:2607:f8b0:4864:20::732]) by sourceware.org (Postfix) with ESMTPS id 0FA76393BC37 for ; Mon, 3 May 2021 21:00:11 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.3.2 sourceware.org 0FA76393BC37 Received: by mail-qk1-x732.google.com with SMTP id 8so6544226qkv.8 for ; Mon, 03 May 2021 14:00:11 -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:mime-version :content-transfer-encoding; bh=Pkb8B8Czkg7MrwAzKza8XDEeC6ugcqBU+GerD4WiuE8=; b=GqkKgpxGvwmxNpLGni719/xY8nza0c+QN4mnKMYpbOZwSKXvTjh0LQsRECefI+bk0b 4kc0oiH14LED6VoIz2ixpInRhJlCZtXsl55pGmLakM73JKdmw135E7ijpDw/WeAWMHQZ z34ZIu6qkDpQKIPzaKCccWkwOXv2OHo/Ls3ZQpuqH0e+r2FQZzOBuvs9c76ihXiaMExu sR7cj2n77xvNJAuPvdjwl9sWT0VZPPtycxYIsUfHihHTzRpJOaJEyun433E70kpx7SHb J/q1dlnYyh4sR7WHWfau6HBCAurJYji5fD/HOVqxUyXwfJHGw1yfac83IRMcYvB1VaSY SlrA== X-Gm-Message-State: AOAM532Hij5BCwbsV5nzaPPlVMxXkdYiA2NW+lf+9EbUNN79sgbxxZ2t seD4cFf6fDnZIMeCh7wsPdlpMU7/n6jbHg== X-Google-Smtp-Source: ABdhPJzkxzSC9KkbNk47KlApPmpTwBSo6+SJ7dFeiF5rg+tk2IgqivzRrCV2RIlVMNSMNZnPiiVWmQ== X-Received: by 2002:a05:620a:2ee:: with SMTP id a14mr20886636qko.229.1620075610445; Mon, 03 May 2021 14:00:10 -0700 (PDT) Received: from birita.. ([177.194.41.149]) by smtp.googlemail.com with ESMTPSA id w196sm4578090qkb.90.2021.05.03.14.00.09 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 03 May 2021 14:00:10 -0700 (PDT) To: libc-alpha@sourceware.org Subject: [PATCH v3 1/6] nptl: Make pthread_kill async-signal-safe Date: Mon, 3 May 2021 18:00:00 -0300 Message-Id: <20210503210005.2891859-1-adhemerval.zanella@linaro.org> X-Mailer: git-send-email 2.30.2 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" Changes from v2: * Rebase against master. Changes from v1: * Move __libc_signal_block_all prior TID read. --- Simiar to raise (BZ #15368), pthread_kill is also defined as async-signal-safe (POSIX.1-2008 TC1). However, similar to raise it has the same issues relating to signal handling. Different than raise, all the signal are blocked so it would be possible to implement pthread_cancel (which also should be async-cancel safe). Checked on x86_64-linux-gnu. --- nptl/pthread_kill.c | 23 +++++++++++++++++------ 1 file changed, 17 insertions(+), 6 deletions(-) diff --git a/nptl/pthread_kill.c b/nptl/pthread_kill.c index ad7e011779..71c362adce 100644 --- a/nptl/pthread_kill.c +++ b/nptl/pthread_kill.c @@ -28,6 +28,11 @@ __pthread_kill (pthread_t threadid, int signo) if (__is_internal_signal (signo)) return EINVAL; + sigset_t set; + __libc_signal_block_all (&set); + + int val; + /* Force load of pd->tid into local variable or register. Otherwise if a thread exits between ESRCH test and tgkill, we might return EINVAL, because pd->tid would be cleared by the kernel. */ @@ -35,14 +40,20 @@ __pthread_kill (pthread_t threadid, int signo) pid_t tid = atomic_forced_read (pd->tid); if (__glibc_unlikely (tid <= 0)) /* Not a valid thread handle. */ - return ESRCH; + val = ESRCH; + else + { + /* We have a special syscall to do the work. */ + pid_t pid = __getpid (); + + val = INTERNAL_SYSCALL_CALL (tgkill, pid, tid, signo); + val = (INTERNAL_SYSCALL_ERROR_P (val) + ? INTERNAL_SYSCALL_ERRNO (val) : 0); + } - /* We have a special syscall to do the work. */ - pid_t pid = __getpid (); + __libc_signal_restore_set (&set); - int val = INTERNAL_SYSCALL_CALL (tgkill, pid, tid, signo); - return (INTERNAL_SYSCALL_ERROR_P (val) - ? INTERNAL_SYSCALL_ERRNO (val) : 0); + return val; } versioned_symbol (libc, __pthread_kill, pthread_kill, GLIBC_2_34);