Received: by 2002:a25:4158:0:0:0:0:0 with SMTP id o85csp573435yba; Fri, 5 Apr 2019 12:23:40 -0700 (PDT) X-Google-Smtp-Source: APXvYqzA6k4sHYQe/jlso2Gy8nvMyF7hi9ekdHPG2wN+vhuMZASNMdBKMG83QoR7kYm3vUXKe8QD X-Received: by 2002:a17:902:5a45:: with SMTP id f5mr813052plm.23.1554492219797; Fri, 05 Apr 2019 12:23:39 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1554492219; cv=none; d=google.com; s=arc-20160816; b=W5yXkQsbgWTYkqX1+SRj0umMmQ3rswcfs9aSFpS3L7+NWBUwHh+/Q1oHYBDc1R9NT1 PQwr7njp1hduN+iNDQYzNfCb9VHLyNkZP4jLSjYxWBaRkh4oOuoZOrfg7PHl3MOZUeoz llZcabZQtjvYYVLWHM2eGAkAj2Q+fxDITbfJugYkTwg39NFzgr6P+s+aQCEk3PYzKGM7 hFmnlDb0i88p2GhEx+lERP1+52nJBL8vPlx61G1qAT7bJkeiMzXg7yU0c47xZ2MHOuxN TWCAOsYMTlNNP/gB8lwXkdsxyJ48RnYcxsupJpDxxRLMGWsym3R+Mz0sC3y9P3+BhqRq PUtg== 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=nnurSx+zuMwUiXcjXH169HAwB1njWLniPOKxrwz2v2A=; b=n2D5pL8vV5o70qfn9NXpob9DO5/uKXVheYX+LbyKF7jFWofHS+ahVHK2d7U64ZwRuK 8Z5bGcqTiMjQdlyJYljh9cQdhBMkp/Y7TkHO5GqvkwN1ZkITFzCQYYysEBgXrh5vNWCe 9Ac4p5BNHMj7ohAXbgqf9KOqHXIHbPwwwkuHPJa2ivzzBDOpFc/NFmsy3AHrVXsX28yo ozejD4dXk9k8Hau2myCP3GjY43pLSrvEu+lS7AIq6jdifsqBoghkN70k6Q8rNdXIktXW PHSsFgJoljH15+cQRc6Q2cuoUmbV0Z9dCx+8V34PlWP+zw4hhqcf3St4Aecfh0HkfILd ZFhA== 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 y18si21258135pfc.71.2019.04.05.12.23.25; Fri, 05 Apr 2019 12:23:39 -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 S1731984AbfDETW3 (ORCPT + 99 others); Fri, 5 Apr 2019 15:22:29 -0400 Received: from mx1.redhat.com ([209.132.183.28]:41358 "EHLO mx1.redhat.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1731950AbfDETW1 (ORCPT ); Fri, 5 Apr 2019 15:22:27 -0400 Received: from smtp.corp.redhat.com (int-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.11]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 2538BC05B017; Fri, 5 Apr 2019 19:22:27 +0000 (UTC) Received: from llong.com (dhcp-17-47.bos.redhat.com [10.18.17.47]) by smtp.corp.redhat.com (Postfix) with ESMTP id 569816058F; Fri, 5 Apr 2019 19:22:24 +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 , Waiman Long Subject: [PATCH-tip v2 12/12] locking/rwsem: Remove redundant computation of writer lock word Date: Fri, 5 Apr 2019 15:21:15 -0400 Message-Id: <20190405192115.17416-13-longman@redhat.com> In-Reply-To: <20190405192115.17416-1-longman@redhat.com> References: <20190405192115.17416-1-longman@redhat.com> X-Scanned-By: MIMEDefang 2.79 on 10.5.11.11 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.31]); Fri, 05 Apr 2019 19:22:27 +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-xadd.c | 25 ++++++++++++++----------- 1 file changed, 14 insertions(+), 11 deletions(-) diff --git a/kernel/locking/rwsem-xadd.c b/kernel/locking/rwsem-xadd.c index ebdf21fcd75e..24ce7e2d4716 100644 --- a/kernel/locking/rwsem-xadd.c +++ b/kernel/locking/rwsem-xadd.c @@ -230,8 +230,8 @@ static void __rwsem_mark_wake(struct rw_semaphore *sem, * race conditions between checking the rwsem wait list and setting the * sem->count accordingly. */ -static inline bool -rwsem_try_write_lock(long count, struct rw_semaphore *sem, bool first) +static inline bool rwsem_try_write_lock(long count, struct rw_semaphore *sem, + const long wlock, bool first) { long new; @@ -241,7 +241,7 @@ rwsem_try_write_lock(long count, struct rw_semaphore *sem, bool first) if (!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)) { @@ -280,13 +280,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; @@ -436,7 +437,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); @@ -465,7 +466,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) @@ -544,7 +545,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; } @@ -601,7 +603,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; /* @@ -717,10 +719,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; /* @@ -779,7 +782,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, first)) + if (rwsem_try_write_lock(count, sem, wlock, first)) break; raw_spin_unlock_irq(&sem->wait_lock); -- 2.18.1