From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-pb0-f46.google.com (mail-pb0-f46.google.com [209.85.160.46]) by kanga.kvack.org (Postfix) with ESMTP id 6C3D86B0037 for ; Thu, 26 Sep 2013 02:53:50 -0400 (EDT) Received: by mail-pb0-f46.google.com with SMTP id rq2so719905pbb.19 for ; Wed, 25 Sep 2013 23:53:50 -0700 (PDT) Received: by mail-ee0-f51.google.com with SMTP id c1so301945eek.10 for ; Wed, 25 Sep 2013 23:53:46 -0700 (PDT) Date: Thu, 26 Sep 2013 08:53:35 +0200 From: Ingo Molnar 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> Sender: owner-linux-mm@kvack.org List-ID: 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 * 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 -- To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to majordomo@kvack.org. For more info on Linux MM, see: http://www.linux-mm.org/ . Don't email: email@kvack.org