From mboxrd@z Thu Jan 1 00:00:00 1970 From: Will Deacon Subject: Re: [PATCH 04/18] alpha: Override READ_ONCE() with barriered implementation Date: Thu, 2 Jul 2020 12:18:33 +0100 Message-ID: <20200702111832.GC16418@willie-the-truck> References: <20200630173734.14057-1-will@kernel.org> <20200630173734.14057-5-will@kernel.org> <20200702093239.GA15391@C02TD0UTHF1T.local> <20200702094833.GA16248@willie-the-truck> Mime-Version: 1.0 Return-path: DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1593688719; bh=h84qaL3tEGgL2zFfFCmV5wFzP3FSpcWjx8fMB+KEr/4=; h=Date:From:To:Cc:Subject:References:In-Reply-To:From; b=TEOri34UXEVBKgcj4rLGB/bJmudCDMyJL+tLvCgKpyoj8RzaK4APPbgu5RGOTP3p5 5oXOCWMbf39NFuDmH+wlYd4Dc7VUwtHetvygZjh2VEHqUUXg8xiVgAGsUh4qOeP1r9 KRGIfHHvKVj65yixb+M3JOkgh2dE76YFoeN4R2m4= Content-Disposition: inline In-Reply-To: Sender: linux-alpha-owner@vger.kernel.org List-ID: Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit To: Arnd Bergmann Cc: Mark Rutland , "linux-kernel@vger.kernel.org" , Sami Tolvanen , Nick Desaulniers , Kees Cook , Marco Elver , "Paul E. McKenney" , Josh Triplett , Matt Turner , Ivan Kokshaysky , Richard Henderson , Peter Zijlstra , Alan Stern , "Michael S. Tsirkin" , Jason Wang , Boqun Feng , Catalin Marinas , Linux ARM , alpha On Thu, Jul 02, 2020 at 12:08:41PM +0200, Arnd Bergmann wrote: > On Thu, Jul 2, 2020 at 11:48 AM Will Deacon wrote: > > On Thu, Jul 02, 2020 at 10:32:39AM +0100, Mark Rutland wrote: > > > On Tue, Jun 30, 2020 at 06:37:20PM +0100, Will Deacon wrote: > > > > -#define read_barrier_depends() __asm__ __volatile__("mb": : :"memory") > > > > +#define __smp_load_acquire(p) \ > > > > +({ \ > > > > + __unqual_scalar_typeof(*p) ___p1 = \ > > > > + (*(volatile typeof(___p1) *)(p)); \ > > > > + compiletime_assert_atomic_type(*p); \ > > > > + ___p1; \ > > > > +}) > > > > > > Sorry if I'm being thick, but doesn't this need a barrier after the > > > volatile access to provide the acquire semantic? > > > > > > IIUC prior to this commit alpha would have used the asm-generic > > > __smp_load_acquire, i.e. > > > > > > | #ifndef __smp_load_acquire > > > | #define __smp_load_acquire(p) \ > > > | ({ \ > > > | __unqual_scalar_typeof(*p) ___p1 = READ_ONCE(*p); \ > > > | compiletime_assert_atomic_type(*p); \ > > > | __smp_mb(); \ > > > | (typeof(*p))___p1; \ > > > | }) > > > | #endif > > I also have a question that I didn't dare ask when the same > code came up before (I guess it's also what's in the kernel today): > > With the cast to 'typeof(*p)' at the end, doesn't that mean we > lose the effect of __unqual_scalar_typeof() again, so any "volatile" > pointer passed into __READ_ONCE_SCALAR() or > __smp_load_acquire() still leads to a volatile load of the original > variable, plus another volatile access to ___p1 after > spilling it to the stack as a non-volatile variable? Not sure I follow you here, but I can confirm that what you're worried about doesn't happen for the usual case of a pointer-to-volatile scalar. For example, ignoring dependency ordering: unsigned long foo(volatile unsigned long *p) { return smp_load_acquire(p) + 1; } Ends up looking like: unsigned long ___p1 = *(const volatile unsigned long *)p; smp_mb(); (volatile unsigned long)___p1; My understanding is that casting a non-pointer type to volatile doesn't do anything, so we're good. On the other hand, you can still cause the stack reload if you use volatile pointers to volatile: volatile unsigned long *bar(volatile unsigned long * volatile *ptr) { return READ_ONCE(*ptr); } but this is pretty weird code, I think. Will