Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1751828Ab3FXUs5 (ORCPT ); Mon, 24 Jun 2013 16:48:57 -0400 Received: from mailout39.mail01.mtsvc.net ([216.70.64.83]:38718 "EHLO n12.mail01.mtsvc.net" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1751072Ab3FXUs4 (ORCPT ); Mon, 24 Jun 2013 16:48:56 -0400 Message-ID: <51C8B0AA.4070204@hurleysoftware.com> Date: Mon, 24 Jun 2013 16:48:42 -0400 From: Peter Hurley User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:17.0) Gecko/20130510 Thunderbird/17.0.6 MIME-Version: 1.0 To: Tim Chen CC: Davidlohr Bueso , Alex Shi , Michel Lespinasse , Ingo Molnar , Andrew Morton , Andrea Arcangeli , Andi Kleen , Matthew R Wilcox , Dave Hansen , Peter Zijlstra , Rik van Riel , linux-kernel@vger.kernel.org, linux-mm Subject: Re: [PATCH 2/2] rwsem: do optimistic spinning for writer lock acquisition References: <1371858700.22432.5.camel@schen9-DESK> <51C558E2.1040108@hurleysoftware.com> <1372017836.1797.14.camel@buesod1.americas.hpqcorp.net> <1372093876.22432.34.camel@schen9-DESK> <51C894C3.4040407@hurleysoftware.com> <1372105065.22432.65.camel@schen9-DESK> In-Reply-To: <1372105065.22432.65.camel@schen9-DESK> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-Authenticated-User: 990527 peter@hurleysoftware.com X-MT-INTERNAL-ID: 8fa290c2a27252aacf65dbc4a42f3ce3735fb2a4 Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Length: 1956 Lines: 67 On 06/24/2013 04:17 PM, Tim Chen wrote: > On Mon, 2013-06-24 at 14:49 -0400, Peter Hurley wrote: >> On 06/24/2013 01:11 PM, Tim Chen wrote: >>> On Sun, 2013-06-23 at 13:03 -0700, Davidlohr Bueso wrote: >>>> On Sat, 2013-06-22 at 03:57 -0400, Peter Hurley wrote: >>>>> On 06/21/2013 07:51 PM, Tim Chen wrote: >>>>>> >>>>>> +static inline bool rwsem_can_spin_on_owner(struct rw_semaphore *sem) >>>>>> +{ >>>>>> + int retval = true; >>>>>> + >>>>>> + /* Spin only if active writer running */ >>>>>> + if (!sem->owner) >>>>>> + return false; >>>>>> + >>>>>> + rcu_read_lock(); >>>>>> + if (sem->owner) >>>>>> + retval = sem->owner->on_cpu; >>>>> ^^^^^^^^^^^^^^^^^^ >>>>> >>>>> Why is this a safe dereference? Could not another cpu have just >>>>> dropped the sem (and thus set sem->owner to NULL and oops)? >>>>> >>> >>> The rcu read lock should protect against sem->owner being NULL. >> >> It doesn't. >> >> Here's the comment from mutex_spin_on_owner(): >> >> /* >> * Look out! "owner" is an entirely speculative pointer >> * access and not reliable. >> */ > > On second thought, I agree with you. I should change this to > something like > > int retval = true; > task_struct *sem_owner; > > /* Spin only if active writer running */ > if (!sem->owner) > return false; > > rcu_read_lock(); > sem_owner = sem->owner; > if (sem_owner) > retval = sem_owner->on_cpu; > Our emails passed each other. Also, I haven't given a lot of thought to if preemption must be disabled before calling rwsem_can_spin_on_owner(). If so, wouldn't you just drop rwsem_can_spin_on_owner() (because the conditions tested in the loop are equivalent)? Regards, Peter Hurley -- 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/