From patchwork Mon Aug 23 19:50:31 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Adhemerval Zanella X-Patchwork-Id: 44747 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 4FAF33858C2C for ; Mon, 23 Aug 2021 19:52:05 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 4FAF33858C2C DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1629748325; bh=e1zMjL/j1U0YZzmOfTwEzCCdnewHjR6A1UUIJ1kzb4I=; 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=PZ16M8hbtsQGwGIgi/YIa/umhxAxv0ZHwAcmlN5EfyhlzGRpGsKATu7jiUQcpeq+c izRgOFcpVi+l1E0nXXh3ztDM/BD+yfVhWoYuN/pyJcEzCc56SMCBN1Tc24pntBCkjW EUinAbMFA6IYBCRBaNYB4jH1aAQJ7Fs0yn4nn098= X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail-qk1-x72b.google.com (mail-qk1-x72b.google.com [IPv6:2607:f8b0:4864:20::72b]) by sourceware.org (Postfix) with ESMTPS id 229763858010 for ; Mon, 23 Aug 2021 19:50:55 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 229763858010 Received: by mail-qk1-x72b.google.com with SMTP id bk29so14784929qkb.8 for ; Mon, 23 Aug 2021 12:50:55 -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:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=e1zMjL/j1U0YZzmOfTwEzCCdnewHjR6A1UUIJ1kzb4I=; b=gQDWIQ6NlTxwj+oS1VYoCEYdnGnI3aCHXmGH5JEFB8IKgEEdnuhUv237NsIu9f9vE9 CymCmHmKnduF0Wg4jfqoLlChYHoNtkVTl5QwaJDGpQzOjLaTR1ablpYZCsvZ/9xIroil /C6jpCsgKsWxqlV6uflZPpQRmG3n1UQ+aUzwlq8N+nOqaBgbZ2i/YSzu550R9qD1vnYL bh3JLGnmlMwq5xhUiY0N7Qrxu3rSq4P4P7hBLgtjA+hL4G7rxOFblchHx+9GfiRlEFYi vLZnVHfNV57K5bPBLiwZVbvnu8iK7fZ6p3Vd6WE7APhIlglRhpQi7wmiD6fIzMynnpC+ zDRA== X-Gm-Message-State: AOAM533HpRcg8WXMLusoBxj+113U2BxPheQHI7mcFaxv6IiLU21AQiLw 8VRm4L4Ac044srsBJJwEfnj/1Y1FW/EZDg== X-Google-Smtp-Source: ABdhPJxJewQ6FKa9jkj+dYQGDH3F/fpztA/G3u0AHrFVzL0HHtPfVo/KkJbk+CYWafCUY1BjOV97+A== X-Received: by 2002:a37:749:: with SMTP id 70mr22023239qkh.461.1629748254603; Mon, 23 Aug 2021 12:50:54 -0700 (PDT) Received: from birita.. ([2804:431:c7ca:cd83:c38b:b50d:5d9a:43d4]) by smtp.gmail.com with ESMTPSA id s10sm9210935qko.134.2021.08.23.12.50.53 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 23 Aug 2021 12:50:54 -0700 (PDT) To: libc-alpha@sourceware.org Subject: [PATCH v2 03/19] nptl: Handle robust PI mutexes for !__ASSUME_SET_ROBUST_LIST Date: Mon, 23 Aug 2021 16:50:31 -0300 Message-Id: <20210823195047.543237-4-adhemerval.zanella@linaro.org> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20210823195047.543237-1-adhemerval.zanella@linaro.org> References: <20210823195047.543237-1-adhemerval.zanella@linaro.org> MIME-Version: 1.0 X-Spam-Status: No, score=-11.9 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 Reply-To: Adhemerval Zanella Cc: Florian Weimer 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 | 53 ++++++++++++++++++++++--------------------- 1 file changed, 27 insertions(+), 26 deletions(-) diff --git a/nptl/pthread_create.c b/nptl/pthread_create.c index d8ec299cb1..08e5189ad6 100644 --- a/nptl/pthread_create.c +++ b/nptl/pthread_create.c @@ -486,35 +486,36 @@ start_thread (void *arg) exit (0); #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)); + unsigned int nusers = mtx->__nusers; + 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 with potential users. */ + if (lock > 1 || nusers != 0) { - 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