X-Received: by 10.180.37.198 with SMTP id a6mr14305298wik.7.1433802614024;
        Mon, 08 Jun 2015 15:30:14 -0700 (PDT)
X-BeenThere: linux.kernel@googlegroups.com
Received: by 10.152.36.65 with SMTP id o1ls1762laj.108.gmail; Mon, 08 Jun 2015
 15:30:04 -0700 (PDT)
X-Received: by 10.112.53.102 with SMTP id a6mr18928452lbp.16.1433802604123;
        Mon, 08 Jun 2015 15:30:04 -0700 (PDT)
MIME-Version: 1.0
Path: ul5ni3600lbb.0!nntp.google.com!goblin2!goblin.stu.neva.ru!aioe.org!bofh.it!news.nic.it!robomod
From: Waiman Long <Waima...@hp.com>
Newsgroups: linux.kernel
Subject: [PATCH 2/2] locking/qrwlock: Don't contend with readers when setting _QW_WAITING
Date: Tue, 09 Jun 2015 00:30:03 +0200
Message-ID: <pzdIn-3Yc-51@gated-at.bofh.it>
References: <pzdIn-3Yc-31@gated-at.bofh.it>
X-Original-To: Peter Zijlstra <pet...@infradead.org>,
	Ingo Molnar <mi...@redhat.com>, Arnd Bergmann <ar...@arndb.de>
X-Mailer: git-send-email 1.7.1
Sender: rob...@news.nic.it
List-ID: <linux-kernel.vger.kernel.org>
X-Mailing-List: linux-...@vger.kernel.org
Approved: rob...@news.nic.it
Lines: 65
Organization: linux.* mail to news gateway
X-Original-Cc: linux...@vger.kernel.org, linux-...@vger.kernel.org,
	Scott J Norton <scott....@hp.com>,
	Douglas Hatch <doug....@hp.com>,
	Waiman Long <Waima...@hp.com>
X-Original-Date: Mon,  8 Jun 2015 18:20:44 -0400
X-Original-Message-ID: <1433802045-21298-3-git...@hp.com>
X-Original-References: <1433802045-21298-1-git...@hp.com>
X-Original-Sender: linux-ker...@vger.kernel.org

The current cmpxchg() loop in setting the _QW_WAITING flag for writers
in queue_write_lock_slowpath() will contend with incoming readers
causing possibly extra cmpxchg() operations that are wasteful. This
patch changes the code to do a byte cmpxchg() to eliminate contention
with new readers.

Signed-off-by: Waiman Long <Waima...@hp.com>
---
 kernel/locking/qrwlock.c |   28 ++++++++++++++++++++++++----
 1 files changed, 24 insertions(+), 4 deletions(-)

diff --git a/kernel/locking/qrwlock.c b/kernel/locking/qrwlock.c
index d7d7557..559198a 100644
--- a/kernel/locking/qrwlock.c
+++ b/kernel/locking/qrwlock.c
@@ -22,6 +22,26 @@
 #include <linux/hardirq.h>
 #include <asm/qrwlock.h>
 
+/*
+ * This internal data structure is used for optimizing access to some of
+ * the subfields within the atomic_t cnts.
+ */
+struct __qrwlock {
+	union {
+		atomic_t cnts;
+		struct {
+#ifdef __LITTLE_ENDIAN
+			u8 wmode;	/* Writer mode   */
+			u8 rcnts[3];	/* Reader counts */
+#else
+			u8 rcnts[3];	/* Reader counts */
+			u8 wmode;	/* Writer mode   */
+#endif
+		};
+	};
+	arch_spinlock_t	lock;
+};
+
 /**
  * rspin_until_writer_unlock - inc reader count & spin until writer is gone
  * @lock  : Pointer to queue rwlock structure
@@ -109,10 +129,10 @@ void queue_write_lock_slowpath(struct qrwlock *lock)
 	 * or wait for a previous writer to go away.
 	 */
 	for (;;) {
-		cnts = atomic_read(&lock->cnts);
-		if (!(cnts & _QW_WMASK) &&
-		    (atomic_cmpxchg(&lock->cnts, cnts,
-				    cnts | _QW_WAITING) == cnts))
+		struct __qrwlock *l = (struct __qrwlock *)lock;
+
+		if (!READ_ONCE(l->wmode) &&
+		   (cmpxchg(&l->wmode, 0, _QW_WAITING) == 0))
 			break;
 
 		cpu_relax_lowlatency();
-- 
1.7.1

--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majo...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Please read the FAQ at  http://www.tux.org/lkml/

