From patchwork Wed May 18 21:57:18 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Adhemerval Zanella Netto X-Patchwork-Id: 68059 Delivered-To: patch@linaro.org Received: by 10.140.92.199 with SMTP id b65csp2869231qge; Wed, 18 May 2016 14:57:48 -0700 (PDT) X-Received: by 10.66.254.1 with SMTP id ae1mr14326038pad.32.1463608668733; Wed, 18 May 2016 14:57:48 -0700 (PDT) Return-Path: Received: from sourceware.org (server1.sourceware.org. [209.132.180.131]) by mx.google.com with ESMTPS id d15si14688427pfb.137.2016.05.18.14.57.48 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Wed, 18 May 2016 14:57:48 -0700 (PDT) Received-SPF: pass (google.com: domain of libc-alpha-return-69691-patch=linaro.org@sourceware.org designates 209.132.180.131 as permitted sender) client-ip=209.132.180.131; Authentication-Results: mx.google.com; dkim=pass header.i=@sourceware.org; spf=pass (google.com: domain of libc-alpha-return-69691-patch=linaro.org@sourceware.org designates 209.132.180.131 as permitted sender) smtp.mailfrom=libc-alpha-return-69691-patch=linaro.org@sourceware.org; dmarc=fail (p=NONE dis=NONE) header.from=linaro.org DomainKey-Signature: a=rsa-sha1; c=nofws; d=sourceware.org; h=list-id :list-unsubscribe:list-subscribe:list-archive:list-post :list-help:sender:from:to:subject:date:message-id:in-reply-to :references; q=dns; s=default; b=FWHod/tXxVTCA+T3mMm1+y9h0czOmMw psfEAPQO6/B5Eb+bMoJ5dYaCawKLVR3dy/RSG5Jyf2dkZGoH4+7v+p3cvY72kE9/ d87B1CVj5VSp7JR9AZL3e4fyTcovcL/1AOm+i4m/ZE1jjTTmRPMapb64q4U8IM7W AMFUib3f+MJw= DKIM-Signature: v=1; a=rsa-sha1; c=relaxed; d=sourceware.org; h=list-id :list-unsubscribe:list-subscribe:list-archive:list-post :list-help:sender:from:to:subject:date:message-id:in-reply-to :references; s=default; bh=ps4jjTCJwIPvDp7ASUMGD7PoXNU=; b=RQWQz 3ZVeSjpkpWBG1+1Dj8yiuDsg9sPtzDRbznPULk/S46Dj34CqciEqUr8a0ZjxOXbk gBcVJEv6wqDMdEHu0RvjObAfQv1MoaAXC/Ak76cwU4mhsvo9glEkMDYORSo2rutg aSedLmxS0jvSF1dwoMcbU/EEqSC0epjKtmrCrs= Received: (qmail 13688 invoked by alias); 18 May 2016 21:57:39 -0000 Mailing-List: contact libc-alpha-help@sourceware.org; run by ezmlm Precedence: bulk List-Id: List-Unsubscribe: List-Subscribe: List-Archive: List-Post: List-Help: , Sender: libc-alpha-owner@sourceware.org Delivered-To: mailing list libc-alpha@sourceware.org Received: (qmail 13661 invoked by uid 89); 18 May 2016 21:57:38 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-1.9 required=5.0 tests=BAYES_00, RCVD_IN_DNSWL_NONE, SPF_PASS autolearn=ham version=3.3.2 spammy=2197, 1597, policy X-HELO: mail-yw0-f182.google.com X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:from:to:subject:date:message-id:in-reply-to :references; bh=4rPOnqTeBia3Lm7PVWb8IYLHNU/IDXgYynW03itFOR0=; b=bsQvW7nLVUMAU4k3JUdcyU9s4UTPRyXbiwftmVWtKcTltE++PjTNJOoKovC4cn8S4s b5cyfh+EDb6oKVwIGqkv3dEeqPlOslszdK+Asu8tgLZq5pBs1CQYTbHtIY9nStNIUw+3 9eD/YYQL/6dV5sw1hkcgiHwM5blrkOch0aOcvZDbryy5huv0+swbZMQl30avxDMRIhpO ni+vSc1vFezHqLtrcU66tXVSVjiScSKHAF0EfI/KwQtaNJApzFl4Bgvy1rdMxLSvjpYi a4rQ4/+GtZQIpMWwV2Sx4NtbTeSyy+ieaE0tGqKHMmZvlsdVxvHK0xcqCSh3dsomJffe sZyw== X-Gm-Message-State: AOPr4FUhgpIfmkh7xplNjEUqtEgYm2yvi4IMbTZN6oYPK/zi9NgcoJSvStfBKYWtoFeMgUJs X-Received: by 10.37.207.146 with SMTP id f140mr5291226ybg.131.1463608647983; Wed, 18 May 2016 14:57:27 -0700 (PDT) From: Adhemerval Zanella To: libc-alpha@sourceware.org Subject: [PATCH 3/3] Add runtime check for __ASSUME_REQUEUE_PI (BZ#18463) Date: Wed, 18 May 2016 18:57:18 -0300 Message-Id: <1463608638-7215-3-git-send-email-adhemerval.zanella@linaro.org> In-Reply-To: <1463608638-7215-1-git-send-email-adhemerval.zanella@linaro.org> References: <1463608638-7215-1-git-send-email-adhemerval.zanella@linaro.org> This patch adds a runtime check for requeue priority futexes (FUTEX_{WAIT,CMP}_REQUEUE_PI) for configurations that do not define __ASSUME_REQUEUE_PI. It uses the same check uses for priority lock/unlock support, where issuing a futex operation with FUTEX_UNLOCK_PI returns if kernel has or not internal 'futex_cmpxchg_enabled' support (which is also used on requeue priority futexes operations). For architectures that already have __ASSUME_REQUEUE_PI the code flow does not change, 'use_requeue_pi' returns the previous chec logic. Also, if 'futex_cmpxchg_enabled' is not supported by the kernel, the previous logic will be used (by issuing a default futex operation). It fixes BZ#18463 since now requeue pi operation are queried on runtime. Tested on ARM (v3.8 kernel) and x86_64. * nptl/pthreadP.h (prio_inherit_missing): Add prototype. (USE_REQUEUE_PI): Remove define. (use_requeue_pi): New function. * nptl/pthread_cond_broadcast.c [__ASSUME_REQUEUE_PI] (__pthread_cond_broadcast): Remove ifdef. (USE_REQUEUE_PI): Change to use_requeue_pi. * nptl/pthread_cond_signal.c [__ASSUME_REQUEUE_PI] (__pthread_cond_signal): Remove ifdef. (USE_REQUEUE_PI): Change to use_requeue_pi. * nptl/pthread_cond_timedwait.c [__ASSUME_REQUEUE_PI] (__pthread_cond_timedwait): Remove ifdef. (USE_REQUEUE_PI): Change to use_requeue_pi. ( nptl/pthread_cond_wait.c [__ASSUME_REQUEUE_PI] (__pthread_cond_wait): Remove ifdef. (USE_REQUEUE_PI): Change to use_requeue_pi. * nptl/pthread_mutex_init.c (prio_inherit_missing): Remove 'static' qualifier. --- ChangeLog | 18 ++++++++++++++++++ nptl/pthreadP.h | 23 ++++++++++++++--------- nptl/pthread_cond_broadcast.c | 5 ++--- nptl/pthread_cond_signal.c | 5 ++--- nptl/pthread_cond_timedwait.c | 11 ++++------- nptl/pthread_cond_wait.c | 13 +++++-------- nptl/pthread_mutex_init.c | 2 +- 7 files changed, 46 insertions(+), 31 deletions(-) -- 2.7.4 diff --git a/nptl/pthreadP.h b/nptl/pthreadP.h index d479a3e..4eac061 100644 --- a/nptl/pthreadP.h +++ b/nptl/pthreadP.h @@ -585,17 +585,22 @@ extern void __wait_lookup_done (void) attribute_hidden; # define PTHREAD_STATIC_FN_REQUIRE(name) __asm (".globl " #name); #endif +extern bool prio_inherit_missing (void); + /* Test if the mutex is suitable for the FUTEX_WAIT_REQUEUE_PI operation. */ -#if (defined lll_futex_wait_requeue_pi \ - && defined __ASSUME_REQUEUE_PI) -# define USE_REQUEUE_PI(mut) \ - ((mut) && (mut) != (void *) ~0l \ - && (((mut)->__data.__kind \ - & (PTHREAD_MUTEX_PRIO_INHERIT_NP | PTHREAD_MUTEX_ROBUST_NORMAL_NP)) \ - == PTHREAD_MUTEX_PRIO_INHERIT_NP)) -#else -# define USE_REQUEUE_PI(mut) 0 +static inline +bool use_requeue_pi (pthread_mutex_t *mut) +{ +#ifndef __ASSUME_REQUEUE_PI + if (prio_inherit_missing ()) + return false; #endif + return (mut) && + (mut) != (void *) ~0l && + (((mut)->__data.__kind + & (PTHREAD_MUTEX_PRIO_INHERIT_NP | PTHREAD_MUTEX_ROBUST_NORMAL_NP)) == + PTHREAD_MUTEX_PRIO_INHERIT_NP); +} /* Returns 0 if POL is a valid scheduling policy. */ static inline int diff --git a/nptl/pthread_cond_broadcast.c b/nptl/pthread_cond_broadcast.c index 552fd42..ef81bcd 100644 --- a/nptl/pthread_cond_broadcast.c +++ b/nptl/pthread_cond_broadcast.c @@ -60,9 +60,8 @@ __pthread_cond_broadcast (pthread_cond_t *cond) || PTHREAD_MUTEX_PSHARED (mut) & PTHREAD_MUTEX_PSHARED_BIT) goto wake_all; -#if (defined lll_futex_cmp_requeue_pi \ - && defined __ASSUME_REQUEUE_PI) - if (USE_REQUEUE_PI (mut)) +#ifdef lll_futex_cmp_requeue_pi + if (use_requeue_pi (mut)) { if (lll_futex_cmp_requeue_pi (&cond->__data.__futex, 1, INT_MAX, &mut->__data.__lock, futex_val, diff --git a/nptl/pthread_cond_signal.c b/nptl/pthread_cond_signal.c index b3a6d3d..9c8f883 100644 --- a/nptl/pthread_cond_signal.c +++ b/nptl/pthread_cond_signal.c @@ -46,11 +46,10 @@ __pthread_cond_signal (pthread_cond_t *cond) ++cond->__data.__wakeup_seq; ++cond->__data.__futex; -#if (defined lll_futex_cmp_requeue_pi \ - && defined __ASSUME_REQUEUE_PI) +#ifdef lll_futex_cmp_requeue_pi pthread_mutex_t *mut = cond->__data.__mutex; - if (USE_REQUEUE_PI (mut) + if (use_requeue_pi (mut) /* This can only really fail with a ENOSYS, since nobody can modify futex while we have the cond_lock. */ && lll_futex_cmp_requeue_pi (&cond->__data.__futex, 1, 0, diff --git a/nptl/pthread_cond_timedwait.c b/nptl/pthread_cond_timedwait.c index 711a51d..61a651c 100644 --- a/nptl/pthread_cond_timedwait.c +++ b/nptl/pthread_cond_timedwait.c @@ -63,8 +63,7 @@ __pthread_cond_timedwait (pthread_cond_t *cond, pthread_mutex_t *mutex, int pshared = (cond->__data.__mutex == (void *) ~0l) ? LLL_SHARED : LLL_PRIVATE; -#if (defined lll_futex_timed_wait_requeue_pi \ - && defined __ASSUME_REQUEUE_PI) +#ifdef lll_futex_timed_wait_requeue_pi int pi_flag = 0; #endif @@ -161,8 +160,7 @@ __pthread_cond_timedwait (pthread_cond_t *cond, pthread_mutex_t *mutex, /* REQUEUE_PI was implemented after FUTEX_CLOCK_REALTIME, so it is sufficient to check just the former. */ -#if (defined lll_futex_timed_wait_requeue_pi \ - && defined __ASSUME_REQUEUE_PI) +#ifdef lll_futex_timed_wait_requeue_pi /* If pi_flag remained 1 then it means that we had the lock and the mutex but a spurious waker raced ahead of us. Give back the mutex before going into wait again. */ @@ -171,7 +169,7 @@ __pthread_cond_timedwait (pthread_cond_t *cond, pthread_mutex_t *mutex, __pthread_mutex_cond_lock_adjust (mutex); __pthread_mutex_unlock_usercnt (mutex, 0); } - pi_flag = USE_REQUEUE_PI (mutex); + pi_flag = use_requeue_pi (mutex); if (pi_flag) { @@ -250,8 +248,7 @@ __pthread_cond_timedwait (pthread_cond_t *cond, pthread_mutex_t *mutex, __pthread_cleanup_pop (&buffer, 0); /* Get the mutex before returning. */ -#if (defined lll_futex_timed_wait_requeue_pi \ - && defined __ASSUME_REQUEUE_PI) +#ifdef lll_futex_timed_wait_requeue_pi if (pi_flag) { __pthread_mutex_cond_lock_adjust (mutex); diff --git a/nptl/pthread_cond_wait.c b/nptl/pthread_cond_wait.c index 3f62acc..f79bf67 100644 --- a/nptl/pthread_cond_wait.c +++ b/nptl/pthread_cond_wait.c @@ -86,7 +86,7 @@ __condvar_cleanup (void *arg) /* Get the mutex before returning unless asynchronous cancellation is in effect. We don't try to get the mutex if we already own it. */ - if (!(USE_REQUEUE_PI (cbuffer->mutex)) + if (!(use_requeue_pi (cbuffer->mutex)) || ((cbuffer->mutex->__data.__lock & FUTEX_TID_MASK) != THREAD_GETMEM (THREAD_SELF, tid))) { @@ -106,8 +106,7 @@ __pthread_cond_wait (pthread_cond_t *cond, pthread_mutex_t *mutex) int pshared = (cond->__data.__mutex == (void *) ~0l) ? LLL_SHARED : LLL_PRIVATE; -#if (defined lll_futex_wait_requeue_pi \ - && defined __ASSUME_REQUEUE_PI) +#ifdef lll_futex_wait_requeue_pi int pi_flag = 0; #endif @@ -160,8 +159,7 @@ __pthread_cond_wait (pthread_cond_t *cond, pthread_mutex_t *mutex) /* Enable asynchronous cancellation. Required by the standard. */ cbuffer.oldtype = __pthread_enable_asynccancel (); -#if (defined lll_futex_wait_requeue_pi \ - && defined __ASSUME_REQUEUE_PI) +#ifdef lll_futex_wait_requeue_pi /* If pi_flag remained 1 then it means that we had the lock and the mutex but a spurious waker raced ahead of us. Give back the mutex before going into wait again. */ @@ -170,7 +168,7 @@ __pthread_cond_wait (pthread_cond_t *cond, pthread_mutex_t *mutex) __pthread_mutex_cond_lock_adjust (mutex); __pthread_mutex_unlock_usercnt (mutex, 0); } - pi_flag = USE_REQUEUE_PI (mutex); + pi_flag = use_requeue_pi (mutex); if (pi_flag) { @@ -221,8 +219,7 @@ __pthread_cond_wait (pthread_cond_t *cond, pthread_mutex_t *mutex) __pthread_cleanup_pop (&buffer, 0); /* Get the mutex before returning. Not needed for PI. */ -#if (defined lll_futex_wait_requeue_pi \ - && defined __ASSUME_REQUEUE_PI) +#ifdef lll_futex_wait_requeue_pi if (pi_flag) { __pthread_mutex_cond_lock_adjust (mutex); diff --git a/nptl/pthread_mutex_init.c b/nptl/pthread_mutex_init.c index 6aef890..c4f45f2 100644 --- a/nptl/pthread_mutex_init.c +++ b/nptl/pthread_mutex_init.c @@ -33,7 +33,7 @@ static const struct pthread_mutexattr default_mutexattr = }; -static bool +bool prio_inherit_missing (void) { #ifdef __NR_futex