From patchwork Mon Dec 7 21:27:52 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Adhemerval Zanella X-Patchwork-Id: 41331 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 4E08E393D02B; Mon, 7 Dec 2020 21:28:09 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 4E08E393D02B DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1607376489; bh=T7p/TJ6gknrumM+IJ6j8r2cqInKQmGcgFTq+RBXfryY=; h=To:Subject:Date:In-Reply-To:References:List-Id:List-Unsubscribe: List-Archive:List-Post:List-Help:List-Subscribe:From:Reply-To:Cc: From; b=Fqz8jlozcT4NexF5xbnFzn/rWc2FKNXm0IlVPcJDy5G09FRZesK48Xt0GSHk2PA96 Lc2nx4Z5hgA7L2dM1KnFSUHjPkzMq1Dmp/8fRIUvEtEvZ7RBwmqnLj5MegpIycpVSb hZc9iEvK3ajY5CgIthB/ljreZAUsLuW7zuX1WyMQ= X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail-qk1-x741.google.com (mail-qk1-x741.google.com [IPv6:2607:f8b0:4864:20::741]) by sourceware.org (Postfix) with ESMTPS id 6FF3B393BC09 for ; Mon, 7 Dec 2020 21:28:07 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.3.2 sourceware.org 6FF3B393BC09 Received: by mail-qk1-x741.google.com with SMTP id q5so14125069qkc.12 for ; Mon, 07 Dec 2020 13:28:07 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=T7p/TJ6gknrumM+IJ6j8r2cqInKQmGcgFTq+RBXfryY=; b=hOA+3H40V8eU2lZf48PNqJtS0PPErNtrM0AoM+u4dFgG9sEnvj4bCkPdP/1/WiTXIw x4qOFR/QNJRKwrhf9hL3ZAgzEGYj/i/CW1R+P6QP24zhDiJI+l6WtZjF2oeWqN93DaX2 wRdh2NnzI8Y9gG2U3M6dW5a/lAJNg+oE5zCjUto+IdsP+zCeqtnXqM8yKuWLIeTFBZej 4vFEElQ6M54kNXYnYp+gMzi3bdmpImd9vJbqXOsToczai3avC7wuR7UiLL/+wLR5Vs12 6DFRFXyWI9JQzLlA3sV0HUuS8/yegRFfI7kgHGC0z1n2njN7SX1MOWOA8H4/hKktWD1C EyhA== X-Gm-Message-State: AOAM5304KXEKvwkl4L5opQVM+bi4w19VTA559zRA2hhUagAx4aNC1OAI lPkyi47cZrhbVGu6GiXBB1BDgB5UCMTBiw== X-Google-Smtp-Source: ABdhPJxq4/tjBlt+317+3RL8WSClgU2cbdo3eZDwaAl2h27Of1XI0Ysd3DWVuur8cZLIyELv/LLPUg== X-Received: by 2002:a05:620a:1526:: with SMTP id n6mr4706530qkk.334.1607376486779; Mon, 07 Dec 2020 13:28:06 -0800 (PST) Received: from localhost.localdomain ([177.194.48.209]) by smtp.googlemail.com with ESMTPSA id o29sm12832317qtl.7.2020.12.07.13.28.05 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 07 Dec 2020 13:28:06 -0800 (PST) To: libc-alpha@sourceware.org Subject: [PATCH v2 3/8] nptl: Make pthread_kill async-signal-safe Date: Mon, 7 Dec 2020 18:27:52 -0300 Message-Id: <20201207212757.3948164-3-adhemerval.zanella@linaro.org> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20201207212757.3948164-1-adhemerval.zanella@linaro.org> References: <20201207212757.3948164-1-adhemerval.zanella@linaro.org> MIME-Version: 1.0 X-Spam-Status: No, score=-13.7 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 Reply-To: Adhemerval Zanella Cc: Florian Weimer Errors-To: libc-alpha-bounces@sourceware.org Sender: "Libc-alpha" Changes from previous version: * 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 7ef68d1572..006322ea9f 100644 --- a/nptl/pthread_kill.c +++ b/nptl/pthread_kill.c @@ -27,6 +27,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. */ @@ -34,13 +39,19 @@ __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; } strong_alias (__pthread_kill, pthread_kill)