From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1754779Ab3IZGxs (ORCPT ); Thu, 26 Sep 2013 02:53:48 -0400 Received: from mail-ea0-f176.google.com ([209.85.215.176]:44801 "EHLO mail-ea0-f176.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751574Ab3IZGxr (ORCPT ); Thu, 26 Sep 2013 02:53:47 -0400 Date: Thu, 26 Sep 2013 08:53:35 +0200 From: Ingo Molnar To: Tim Chen Cc: Ingo Molnar , Andrew Morton , Andrea Arcangeli , Alex Shi , Andi Kleen , Michel Lespinasse , Davidlohr Bueso , Matthew R Wilcox , Dave Hansen , Peter Zijlstra , Rik van Riel , Peter Hurley , linux-kernel@vger.kernel.org, linux-mm , Linus Torvalds Subject: Re: [PATCH v6 6/6] rwsem: do optimistic spinning for writer lock acquisition Message-ID: <20130926065335.GC19090@gmail.com> References: <1380147051.3467.68.camel@schen9-DESK> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <1380147051.3467.68.camel@schen9-DESK> User-Agent: Mutt/1.5.21 (2010-09-15) Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org * Tim Chen wrote: > We want to add optimistic spinning to rwsems because > the writer rwsem does not perform as well as mutexes. Tim noticed that > for exim (mail server) workloads, when reverting commit 4fc3f1d6 and > Davidlohr noticed it when converting the i_mmap_mutex to a rwsem in some > aim7 workloads. We've noticed that the biggest difference > is when we fail to acquire a mutex in the fastpath, optimistic spinning > comes in to play and we can avoid a large amount of unnecessary sleeping > and overhead of moving tasks in and out of wait queue. > > Allowing optimistic spinning before putting the writer on the wait queue > reduces wait queue contention and provided greater chance for the rwsem > to get acquired. With these changes, rwsem is on par with mutex. > > Reviewed-by: Ingo Molnar > Reviewed-by: Peter Zijlstra > Reviewed-by: Peter Hurley > Signed-off-by: Tim Chen > Signed-off-by: Davidlohr Bueso > --- > include/linux/rwsem.h | 6 +- > kernel/rwsem.c | 19 +++++- > lib/rwsem.c | 203 ++++++++++++++++++++++++++++++++++++++++++++----- > 3 files changed, 207 insertions(+), 21 deletions(-) > > diff --git a/include/linux/rwsem.h b/include/linux/rwsem.h > index 0616ffe..ef5a83a 100644 > --- a/include/linux/rwsem.h > +++ b/include/linux/rwsem.h > @@ -26,6 +26,8 @@ struct rw_semaphore { > long count; > raw_spinlock_t wait_lock; > struct list_head wait_list; > + struct task_struct *owner; /* write owner */ > + void *spin_mlock; > +#define MLOCK(rwsem) ((struct mcs_spin_node **)&((rwsem)->spin_mlock)) > + mcs_spin_lock(MLOCK(sem), &node); > + mcs_spin_unlock(MLOCK(sem), &node); > + mcs_spin_unlock(MLOCK(sem), &node); > + mcs_spin_unlock(MLOCK(sem), &node); That forced type casting is ugly and fragile. To avoid having to include mcslock.h into rwsem.h just add a forward struct declaration, before the struct rw_semaphore definition: struct mcs_spin_node; Then define spin_mlock with the right type: struct mcs_spin_node *spin_mlock; I'd also suggest renaming 'spin_mlock', to reduce unnecessary variants. If the lock type name is 'struct mcs_spin_node' then 'mcs_lock' would be a perfect field name, right? While at it, renaming mcs_spin_node to mcs_spinlock might be wise as well, and the include file would be named mcs_spinlock.h. Thanks, Ingo