Received: by 2002:a25:4158:0:0:0:0:0 with SMTP id o85csp670138yba; Sat, 13 Apr 2019 10:26:19 -0700 (PDT) X-Google-Smtp-Source: APXvYqzpjBLi4Nt/qrahNhJMytexY+ltmWlhW47naClbrc1ttZJiqddGqQXqxWqBFw6UxHtZHhIy X-Received: by 2002:a63:c046:: with SMTP id z6mr56989816pgi.81.1555176379234; Sat, 13 Apr 2019 10:26:19 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1555176379; cv=none; d=google.com; s=arc-20160816; b=X5//Pb5u4gw8t+9shETM3j42exphDNd5WHOjHkzcMRqmtkfJRKfDvieSXV6K5aiWKb zYxPBcLsw5isyDeEX2JC1bRsxDgNyPhVpkG9enpBx9XfRQcMGTfTcsFYw6XVG1Co8E0s qCdGQtkeYa+1nou2Bob9bY75dwkw/dT1mpD1if0lle2Cz9zIO/l01BM+FH/wQqJa1fMy jY17qyDEdtUqwF8GuumlZipBidP6n5PJ2+PV/ofLGdS/dCOzaTGyqgsFCC0Jw7a0EEOI RVxD3C1z5a6i8rd7wBJnkn9QNkn8BceiUeayYOO+NyANBTmggquv/kHCjcZekNNB1QP4 CvEA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:references:in-reply-to:message-id:date :subject:cc:to:from; bh=zMkGMezQ62o6N4VkxEItQ7pbTxV3Wesizx+16v1cf50=; b=U53qMEfgHfmCqItvNcEa8uDgspRZGNiFTUEa/3hHivNY6wMeWh47707VEo5E2QVUCU BJJo6YuNt0JOqUvIawbsR4W8O+olWQR6pOdzvpiYTaS9zaKr9xZPbES4O3f6FKneh8YY mUgTyBf/xQAnUDpf3eGSf8iZlXRvR+07Z1cTSJDjQ4NIipF0gVsr+DMf9reA/rZuN+NV NOOK9QjCOLYBiwwQAS/M497i5yofgC4QAOYlIQxO3Qb1r0OVViTSIA2MGRb9/pihM4ce WqRsZ3WmFspxOajLuxhEFstaUaMAmLCvNh/WgFjTWTW20FFcNd+pSqTO0uwgvw7ojcHP 1tpg== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=redhat.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id j16si40795457pgl.335.2019.04.13.10.26.03; Sat, 13 Apr 2019 10:26:19 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728255AbfDMRYI (ORCPT + 99 others); Sat, 13 Apr 2019 13:24:08 -0400 Received: from mx1.redhat.com ([209.132.183.28]:39170 "EHLO mx1.redhat.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727997AbfDMRYH (ORCPT ); Sat, 13 Apr 2019 13:24:07 -0400 Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.phx2.redhat.com [10.5.11.14]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 731523082E5F; Sat, 13 Apr 2019 17:24:06 +0000 (UTC) Received: from llong.com (ovpn-120-133.rdu2.redhat.com [10.10.120.133]) by smtp.corp.redhat.com (Postfix) with ESMTP id 449A75D9C6; Sat, 13 Apr 2019 17:24:05 +0000 (UTC) From: Waiman Long To: Peter Zijlstra , Ingo Molnar , Will Deacon , Thomas Gleixner Cc: linux-kernel@vger.kernel.org, x86@kernel.org, Davidlohr Bueso , Linus Torvalds , Tim Chen , huang ying , Waiman Long Subject: [PATCH v4 16/16] locking/rwsem: Remove redundant computation of writer lock word Date: Sat, 13 Apr 2019 13:22:59 -0400 Message-Id: <20190413172259.2740-17-longman@redhat.com> In-Reply-To: <20190413172259.2740-1-longman@redhat.com> References: <20190413172259.2740-1-longman@redhat.com> X-Scanned-By: MIMEDefang 2.79 on 10.5.11.14 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.46]); Sat, 13 Apr 2019 17:24:06 +0000 (UTC) Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On 64-bit architectures, each rwsem writer will have its unique lock word for acquiring the lock. Right now, the writer code recomputes the lock word every time it tries to acquire the lock. This is a waste of time. The lock word is now cached and reused when it is needed. On 32-bit architectures, the extra constant argument to rwsem_try_write_lock() and rwsem_try_write_lock_unqueued() should be optimized out by the compiler. Signed-off-by: Waiman Long --- kernel/locking/rwsem.c | 22 +++++++++++++--------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/kernel/locking/rwsem.c b/kernel/locking/rwsem.c index 27219abb8bb6..2c8187690c7c 100644 --- a/kernel/locking/rwsem.c +++ b/kernel/locking/rwsem.c @@ -561,6 +561,7 @@ static void __rwsem_mark_wake(struct rw_semaphore *sem, * bit is set or the lock is acquired. */ static inline bool rwsem_try_write_lock(long count, struct rw_semaphore *sem, + const long wlock, enum writer_wait_state wstate) { long new; @@ -581,7 +582,7 @@ static inline bool rwsem_try_write_lock(long count, struct rw_semaphore *sem, if ((wstate == WRITER_NOT_FIRST) && RWSEM_COUNT_HANDOFF(count)) return false; - new = (count & ~RWSEM_FLAG_HANDOFF) + RWSEM_WRITER_LOCKED - + new = (count & ~RWSEM_FLAG_HANDOFF) + wlock - (list_is_singular(&sem->wait_list) ? RWSEM_FLAG_WAITERS : 0); if (atomic_long_try_cmpxchg_acquire(&sem->count, &count, new)) { @@ -623,13 +624,14 @@ static inline bool rwsem_try_read_lock_unqueued(struct rw_semaphore *sem) /* * Try to acquire write lock before the writer has been put on wait queue. */ -static inline bool rwsem_try_write_lock_unqueued(struct rw_semaphore *sem) +static inline bool rwsem_try_write_lock_unqueued(struct rw_semaphore *sem, + const long wlock) { long count = atomic_long_read(&sem->count); while (!RWSEM_COUNT_LOCKED_OR_HANDOFF(count)) { if (atomic_long_try_cmpxchg_acquire(&sem->count, &count, - count + RWSEM_WRITER_LOCKED)) { + count + wlock)) { rwsem_set_owner(sem); lockevent_inc(rwsem_opt_wlock); return true; @@ -779,7 +781,7 @@ static inline u64 rwsem_rspin_threshold(struct rw_semaphore *sem) : 25 * NSEC_PER_USEC); } -static bool rwsem_optimistic_spin(struct rw_semaphore *sem, bool wlock) +static bool rwsem_optimistic_spin(struct rw_semaphore *sem, const long wlock) { bool taken = false; bool is_rt_task = rt_task(current); @@ -808,7 +810,7 @@ static bool rwsem_optimistic_spin(struct rw_semaphore *sem, bool wlock) /* * Try to acquire the lock */ - taken = wlock ? rwsem_try_write_lock_unqueued(sem) + taken = wlock ? rwsem_try_write_lock_unqueued(sem, wlock) : rwsem_try_read_lock_unqueued(sem); if (taken) @@ -887,7 +889,8 @@ static inline bool rwsem_can_spin_on_owner(struct rw_semaphore *sem) return false; } -static inline bool rwsem_optimistic_spin(struct rw_semaphore *sem, bool wlock) +static inline bool rwsem_optimistic_spin(struct rw_semaphore *sem, + const long wlock) { return false; } @@ -944,7 +947,7 @@ __rwsem_down_read_failed_common(struct rw_semaphore *sem, int state, long count) */ atomic_long_add(-RWSEM_READER_BIAS, &sem->count); adjustment = 0; - if (rwsem_optimistic_spin(sem, false)) { + if (rwsem_optimistic_spin(sem, 0)) { unsigned long flags; /* @@ -1058,10 +1061,11 @@ __rwsem_down_write_failed_common(struct rw_semaphore *sem, int state) struct rwsem_waiter waiter; struct rw_semaphore *ret = sem; DEFINE_WAKE_Q(wake_q); + const long wlock = RWSEM_WRITER_LOCKED; /* do optimistic spinning and steal lock if possible */ if (rwsem_can_spin_on_owner(sem) && - rwsem_optimistic_spin(sem, true)) + rwsem_optimistic_spin(sem, wlock)) return sem; /* @@ -1120,7 +1124,7 @@ __rwsem_down_write_failed_common(struct rw_semaphore *sem, int state) /* wait until we successfully acquire the lock */ set_current_state(state); while (true) { - if (rwsem_try_write_lock(count, sem, wstate)) + if (rwsem_try_write_lock(count, sem, wlock, wstate)) break; raw_spin_unlock_irq(&sem->wait_lock); -- 2.18.1