Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1751785AbbD3Ven (ORCPT ); Thu, 30 Apr 2015 17:34:43 -0400 Received: from g4t3426.houston.hp.com ([15.201.208.54]:54014 "EHLO g4t3426.houston.hp.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751100AbbD3Vek (ORCPT ); Thu, 30 Apr 2015 17:34:40 -0400 Message-ID: <55429FED.5090207@hp.com> Date: Thu, 30 Apr 2015 17:34:37 -0400 From: Waiman Long User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:10.0.12) Gecko/20130109 Thunderbird/10.0.12 MIME-Version: 1.0 To: Peter Zijlstra CC: Ingo Molnar , linux-kernel@vger.kernel.org, Jason Low , Davidlohr Bueso , Scott J Norton , Douglas Hatch Subject: Re: [PATCH v3] locking/rwsem: reduce spinlock contention in wakeup after up_read/up_write References: <1429898069-28907-1-git-send-email-Waiman.Long@hp.com> <20150428171734.GH23123@twins.programming.kicks-ass.net> In-Reply-To: <20150428171734.GH23123@twins.programming.kicks-ass.net> Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Length: 2784 Lines: 74 On 04/28/2015 01:17 PM, Peter Zijlstra wrote: > On Fri, Apr 24, 2015 at 01:54:29PM -0400, Waiman Long wrote: >> @@ -478,7 +515,40 @@ struct rw_semaphore *rwsem_wake(struct rw_semaphore *sem) >> { >> unsigned long flags; >> >> - raw_spin_lock_irqsave(&sem->wait_lock, flags); >> + /* >> + * If a spinner is present, it is not necessary to do the wakeup. >> + * Try to do wakeup only if the trylock succeeds to minimize >> + * spinlock contention which may introduce too much delay in the >> + * unlock operation. >> + * >> + * spinning writer up_write/up_read caller >> + * --------------- ----------------------- >> + * [S] osq_unlock() [L] osq >> + * MB MB >> + * [RmW] rwsem_try_write_lock() [RmW] spin_trylock(wait_lock) >> + * >> + * Here, it is important to make sure that there won't be a missed >> + * wakeup while the rwsem is free and the only spinning writer goes >> + * to sleep without taking the rwsem. In case the spinning writer is >> + * just going to break out of the waiting loop, it will still do a >> + * trylock in rwsem_down_write_failed() before sleeping. IOW, if >> + * rwsem_has_spinner() is true, it will guarantee at least one >> + * trylock attempt on the rwsem. >> + */ >> + if (!rwsem_has_spinner(sem)) { >> + raw_spin_lock_irqsave(&sem->wait_lock, flags); >> + } else { >> + /* >> + * rwsem_has_spinner() is an atomic read while spin_trylock >> + * does not guarantee a full memory barrier. Insert a memory >> + * barrier here to make sure that wait_lock isn't read until >> + * after osq. >> + * Note: smp_rmb__after_atomic() should be used if available. >> + */ >> + smp_mb__after_atomic(); >> + if (!raw_spin_trylock_irqsave(&sem->wait_lock, flags)) >> + return sem; >> + } >> >> /* do nothing if list empty */ >> if (!list_empty(&sem->wait_list)) > To me it makes more sense to reverse these two branches (identical code > wise of course) and put the special case first. > > Alternatively we could also do something like the below, which to my > eyes looks a little better still, but I don't care too much. > > if (rwsem_has_spinner(sem)) { > /* > * comment ... > */ > smp_rmb(); > if (!raw_spin_trylock_irqsave(&sem->wait_lock, flags)) > return sem; > goto locked; > } > > raw_spin_lock_irqsave(&sem->wait_lock, flags); > locked: Thanks for the suggested. I have implemented that in the v4 patch. Also thanks for correcting my misconception on how to use the smp_mb__after_atomic() macro. Cheers, Longman -- To unsubscribe from this list: send the line "unsubscribe linux-kernel" in the body of a message to majordomo@vger.kernel.org More majordomo info at http://vger.kernel.org/majordomo-info.html Please read the FAQ at http://www.tux.org/lkml/