From patchwork Tue May 31 17:52:45 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Adhemerval Zanella Netto X-Patchwork-Id: 54581 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 B0BD6395B44B for ; Tue, 31 May 2022 17:54:44 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org B0BD6395B44B DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1654019684; bh=Qxj/Gch70FdGO9Vr9OCSyjyWWNkg+hHdoKUJgYtqBd0=; 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=INTWoExznanaLh4LEYlCtXlLHgrJYLwS/y5VUOLpSFQpjfiqaZApd9cjQ7aIP9QpV 00MWKIncFaw8OKwhwL6wQY0pUTDtiYgvRyRnFrnyeyHKjUKvis9eNg4OLWPoXOlAbx NkX0sOVxIBT0KNaOZlxcq8r6RLfbvIq4R9NGv7Z0= X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail-oi1-x234.google.com (mail-oi1-x234.google.com [IPv6:2607:f8b0:4864:20::234]) by sourceware.org (Postfix) with ESMTPS id 19559383A605 for ; Tue, 31 May 2022 17:53:02 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 19559383A605 Received: by mail-oi1-x234.google.com with SMTP id k187so14203495oif.1 for ; Tue, 31 May 2022 10:53:02 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=Qxj/Gch70FdGO9Vr9OCSyjyWWNkg+hHdoKUJgYtqBd0=; b=E6pzxmyO3c65Q3wN+xASK/thdN4Dq2eqFa5HTMn1fwXAMbss1s2K2WD4EHGRumZ8NZ t84ESNEauYVj/qC9BK/Y55g9Y/rNYvANkn2HVIcIYJAx7dnUPe7owT0dVSnV9CmMnzfs e2v9OUG+er8euGGlXEdf5b29vXkgJAVGx5t3iCmbOZRmehDJ594ZTDzwQNB47B2iMTnk tlcTLe8qww64kl3qZ1CEkCx8QhBoECP+2zD2pU0NHzPTy9IsXC8eyROe1D+OfYpRBhvF w20eWre3ms/2zrTKF6pwtlaGwsmsmCp1GvgIl8tlwM3QqJHJLUZAiYWhIRy+GHn3F1qf Puwg== X-Gm-Message-State: AOAM531CO8bd/nfoc+yld2WozmO4sJFaN+aLyPAy0EqrRuJPCCXieLuF LmCIyIquDwM+lX1uDfGHCl7QIswp7BHlfQ== X-Google-Smtp-Source: ABdhPJzSz2RXMfWamtYnyQcCGn+8tFWhI5zhPL+iqdUX5NPE5h/CmdG+UY0gy+T8IBdH2/H8n8tstw== X-Received: by 2002:a05:6808:1510:b0:32a:ef6a:99b with SMTP id u16-20020a056808151000b0032aef6a099bmr13168725oiw.120.1654019581254; Tue, 31 May 2022 10:53:01 -0700 (PDT) Received: from birita.. ([2804:431:c7ca:e39c:980f:eaf1:7a30:10ef]) by smtp.gmail.com with ESMTPSA id f30-20020a4a8921000000b0035f627ac898sm6402626ooi.10.2022.05.31.10.53.00 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 31 May 2022 10:53:01 -0700 (PDT) To: libc-alpha@sourceware.org Subject: [PATCH v3 02/12] nptl: Handle robust PI mutexes for !__ASSUME_SET_ROBUST_LIST (BZ #28268) Date: Tue, 31 May 2022 14:52:45 -0300 Message-Id: <20220531175255.1513396-3-adhemerval.zanella@linaro.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20220531175255.1513396-1-adhemerval.zanella@linaro.org> References: <20220531175255.1513396-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, T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) 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+patchwork=sourceware.org@sourceware.org Sender: "Libc-alpha" The robust PI mutexes are signaled by setting the LSB bit to 1, so the code requires to take this consideration before access the __pthread_mutex_s. The code is also simplified: the initialization code is not really required, PD->robust_head.list and PD->robust_list.__next are essentially the same regardless of __PTHREAD_MUTEX_HAVE_PREV, the futex wake is optimized to be issued only when required, and the futex shared bit is set only when required. Checked on a build for m68k-linux-gnu. I also checked on x86_64-linux-gnu by removing the check for !__ASSUME_SET_ROBUST_LIST. --- nptl/pthread_create.c | 52 +++++++++++++++++++++---------------------- 1 file changed, 26 insertions(+), 26 deletions(-) diff --git a/nptl/pthread_create.c b/nptl/pthread_create.c index e7a099acb7..a6200cf9e5 100644 --- a/nptl/pthread_create.c +++ b/nptl/pthread_create.c @@ -515,35 +515,35 @@ start_thread (void *arg) __libc_lock_unlock (pd->exit_lock); #ifndef __ASSUME_SET_ROBUST_LIST - /* If this thread has any robust mutexes locked, handle them now. */ -# if __PTHREAD_MUTEX_HAVE_PREV - void *robust = pd->robust_head.list; -# else - __pthread_slist_t *robust = pd->robust_list.__next; -# endif - /* We let the kernel do the notification if it is able to do so. - If we have to do it here there for sure are no PI mutexes involved - since the kernel support for them is even more recent. */ - if (!__nptl_set_robust_list_avail - && __builtin_expect (robust != (void *) &pd->robust_head, 0)) + /* We let the kernel do the notification if it is able to do so on the exit + syscall. Otherwise we need to handle before the thread terminates. */ + void **robust; + while ((robust = pd->robust_head.list) + && robust != (void *) &pd->robust_head) { - do + /* Note: robust PI futexes are signaled by setting bit 0. */ + void **robustp = (void **) ((uintptr_t) robust & ~1UL); + + struct __pthread_mutex_s *mtx = (struct __pthread_mutex_s *) + ((char *) robustp - offsetof (struct __pthread_mutex_s, + __list.__next)); + int shared = mtx->__kind & 128; + + pd->robust_head.list_op_pending = robust; + pd->robust_head.list = *robustp; + /* Although the list will not be changed at this point, it follows the + expected kernel ABI. */ + __asm ("" ::: "memory"); + + int lock = atomic_exchange_relaxed (&mtx->__lock, FUTEX_OWNER_DIED); + /* Wake any users if mutex is acquired. */ + if (lock > 1) { - struct __pthread_mutex_s *this = (struct __pthread_mutex_s *) - ((char *) robust - offsetof (struct __pthread_mutex_s, - __list.__next)); - robust = *((void **) robust); - -# if __PTHREAD_MUTEX_HAVE_PREV - this->__list.__prev = NULL; -# endif - this->__list.__next = NULL; - - atomic_or (&this->__lock, FUTEX_OWNER_DIED); - futex_wake ((unsigned int *) &this->__lock, 1, - /* XYZ */ FUTEX_SHARED); + if ((uintptr_t) robust & 1) + futex_unlock_pi ((unsigned int *) &mtx->__lock, shared); + else + futex_wake ((unsigned int *) &mtx->__lock, 1, shared); } - while (robust != (void *) &pd->robust_head); } #endif