From patchwork Thu Sep 30 20:00:38 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: 45624 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 048713857C6D for ; Thu, 30 Sep 2021 20:02:42 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 048713857C6D DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1633032162; bh=hEmOLwFzs9CYBznEsiOpTfxj0Vk1yGyATmkja0uetZQ=; 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=B8PFUtLTKTGtD0VpEASv/uxiXP+XfuDzAng7pP1sqyFmyhEOLrtRZKbcNF/naAE95 0eiBhWAP+5EAvWP34B8XFVOIfVgQlVmpHdMtX+OS4neFvOEwn9SpLFG55RUA1DF2TP gvAyjrBlNU+K+Enlrr19l04rVvU28UcWd/IbEdqo= X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail-qt1-x82b.google.com (mail-qt1-x82b.google.com [IPv6:2607:f8b0:4864:20::82b]) by sourceware.org (Postfix) with ESMTPS id 4B8E23857C42 for ; Thu, 30 Sep 2021 20:00:59 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 4B8E23857C42 Received: by mail-qt1-x82b.google.com with SMTP id e16so6847172qte.13 for ; Thu, 30 Sep 2021 13:00:59 -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=hEmOLwFzs9CYBznEsiOpTfxj0Vk1yGyATmkja0uetZQ=; b=CcxW29qBxMBR60Fce0i7XDN6NSbXja1PHwn3ZsuHKJvB7QLfKhjxv3lDDIe0O7Q2fE ReZKGqaSTMvrVuVE3DEMoED7gE+ADRDzPMFAZJgKC9B31ZfOsFs4AZHzyf/VPjbX4OJu V24gJu+wLG7GGaIHJovpmicIQ3u2yKqvBYT5xw2kDJYk1s8rlWh3zW6jouU3LRW2uMhj mQPp76cB+djU1jKgr8r55W+QHaTIGwg9V0VjidiioXkvoMW5KCAItdoaxo1SOz7VZRIo 9lAE37ypkkWsQMp6W/qMUaQl+bFwX841EuYVTL90AgiEz8yE4MtLXajQWctuXG3nDGJ3 0KiQ== X-Gm-Message-State: AOAM530RXOFiLKbEsPs9sxz4DyjqoTOsD9Q9oz0kIBJK7qF3M04lfCof VJsCkw2isWWsO16KxN4fruQY2yBK/y6NOA== X-Google-Smtp-Source: ABdhPJweqop7o2yiiK6h4G9XkPIYIlhCAjG4NZeOf8sX9gtM4pg55dWBK5KHC3qGefrWjgXE1BfsDg== X-Received: by 2002:a05:622a:283:: with SMTP id z3mr8603698qtw.324.1633032058589; Thu, 30 Sep 2021 13:00:58 -0700 (PDT) Received: from birita.. ([2804:431:c7cb:b338:b20e:53f8:8b5:1bb2]) by smtp.gmail.com with ESMTPSA id r196sm1910203qka.119.2021.09.30.13.00.57 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 30 Sep 2021 13:00:58 -0700 (PDT) To: libc-alpha@sourceware.org Subject: [PATCH 02/15] nptl: Handle robust PI mutexes for !__ASSUME_SET_ROBUST_LIST (BZ #28268) Date: Thu, 30 Sep 2021 17:00:38 -0300 Message-Id: <20210930200051.1017457-3-adhemerval.zanella@linaro.org> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20210930200051.1017457-1-adhemerval.zanella@linaro.org> References: <20210930200051.1017457-1-adhemerval.zanella@linaro.org> MIME-Version: 1.0 X-Spam-Status: No, score=-12.2 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.4 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) 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 d6ea43a754..cd8401c878 100644 --- a/nptl/pthread_create.c +++ b/nptl/pthread_create.c @@ -507,35 +507,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