From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1757951AbdJKSCe (ORCPT ); Wed, 11 Oct 2017 14:02:34 -0400 Received: from mx1.redhat.com ([209.132.183.28]:33442 "EHLO mx1.redhat.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1757938AbdJKSCa (ORCPT ); Wed, 11 Oct 2017 14:02:30 -0400 DMARC-Filter: OpenDMARC Filter v1.3.2 mx1.redhat.com 75E36C04AC6B Authentication-Results: ext-mx07.extmail.prod.ext.phx2.redhat.com; dmarc=none (p=none dis=none) header.from=redhat.com Authentication-Results: ext-mx07.extmail.prod.ext.phx2.redhat.com; spf=fail smtp.mailfrom=longman@redhat.com From: Waiman Long To: Peter Zijlstra , Ingo Molnar Cc: linux-kernel@vger.kernel.org, x86@kernel.org, linux-alpha@vger.kernel.org, linux-ia64@vger.kernel.org, linux-s390@vger.kernel.org, linux-arch@vger.kernel.org, Davidlohr Bueso , Dave Chinner , Waiman Long Subject: [PATCH v6 10/11] locking/rwsem: Make rwsem_spin_on_owner() return a tri-state value Date: Wed, 11 Oct 2017 14:02:01 -0400 Message-Id: <1507744922-15196-11-git-send-email-longman@redhat.com> In-Reply-To: <1507744922-15196-1-git-send-email-longman@redhat.com> References: <1507744922-15196-1-git-send-email-longman@redhat.com> X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.31]); Wed, 11 Oct 2017 18:02:30 +0000 (UTC) Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org This patch modifies rwsem_spin_on_owner() to return a tri-state value to better reflect the state of lock holder which enables us to make a better decision of what to do next. Signed-off-by: Waiman Long --- kernel/locking/rwsem-xadd.c | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/kernel/locking/rwsem-xadd.c b/kernel/locking/rwsem-xadd.c index 38a6c32..d0f3778 100644 --- a/kernel/locking/rwsem-xadd.c +++ b/kernel/locking/rwsem-xadd.c @@ -301,9 +301,13 @@ static inline bool rwsem_can_spin_on_owner(struct rw_semaphore *sem) } /* - * Return true only if we can still spin on the owner field of the rwsem. + * Return the folowing three values depending on the lock owner state. + * 1 when owner has changed and no reader is detected yet. + * 0 when owner has change and/or owner is a reader. + * -1 when optimistic spinning has to stop because either the owner stops + * running or its timeslice has been used up. */ -static noinline bool rwsem_spin_on_owner(struct rw_semaphore *sem) +static noinline int rwsem_spin_on_owner(struct rw_semaphore *sem) { struct task_struct *owner = READ_ONCE(sem->owner); @@ -327,7 +331,7 @@ static noinline bool rwsem_spin_on_owner(struct rw_semaphore *sem) if (!owner->on_cpu || need_resched() || vcpu_is_preempted(task_cpu(owner))) { rcu_read_unlock(); - return false; + return -1; } cpu_relax(); @@ -338,7 +342,7 @@ static noinline bool rwsem_spin_on_owner(struct rw_semaphore *sem) * If there is a new owner or the owner is not set, we continue * spinning. */ - return !rwsem_owner_is_reader(READ_ONCE(sem->owner)); + return rwsem_owner_is_reader(READ_ONCE(sem->owner)) ? 0 : 1; } static bool @@ -359,7 +363,7 @@ static noinline bool rwsem_spin_on_owner(struct rw_semaphore *sem) * 2) readers own the lock as we can't determine if they are * actively running or not. */ - while (rwsem_spin_on_owner(sem)) { + while (rwsem_spin_on_owner(sem) > 0) { /* * Try to acquire the lock */ -- 1.8.3.1