Received: by 2002:a05:6902:102b:0:0:0:0 with SMTP id x11csp1391608ybt; Thu, 2 Jul 2020 04:22:08 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzc3JfhZepYLC6zgdIm+QKxNrdqWQp1tiu73Ppx1akFkvcL4c/rxHw6QAhTvJggjqZpExxM X-Received: by 2002:a17:906:27c9:: with SMTP id k9mr26504664ejc.74.1593688928812; Thu, 02 Jul 2020 04:22:08 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1593688928; cv=none; d=google.com; s=arc-20160816; b=QvMmUzkzRLhSNSRM5oHtadtOgGlIMk7P0mI1X+hibi6REhwIGFMKHYNjkmIX0dlLtS K/UY5MBadTTlEXNJ5EYb9GyswzF++BHXTJvII+75VtlHqyn44R9+ZauwAOn+xZe3LCC4 6+8P+iiWC5FWZfh+wq7hWuloXxbAbzvBCE8xKAqP4oX1Rmx2RIZLhDxySLAdVE+j4B/y ESTc+B/uUvWvzKwabZVL26v5IBVEzHb8Kftm5k0n9B9kEUIu57hhLgBJhIw9bGToTLOy Pia+qqiXs9QwSczf07/VS3wOTUPa4xG+YiG1CO5fgAJ06uOiWNMOQUoNi18M0IRpT67r e8IQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:user-agent:in-reply-to :content-disposition:mime-version:references:message-id:subject:cc :to:from:date:dkim-signature; bh=/GxLCtXepBGbNHwN0ZiI3jLy0UqAZU44PPszMcwlK9s=; b=hxKt4hCms7kSniNj1pb1mtVmrFweg6wBKJ4n7zOGtyhorzUUPx5aBxLx0gz2O+Lnej rTpztlNFNm9oI89giTWOfYbtA3NK9P39D2BlCqmZHAWvql5FMRbzKxtawoRG9JoY7VHW R5KRETDNgwLW7YDUTAgh06U42jF5M5NIjSgDWEn/bIaplMGZLgMOldjb0JwQzEwK8TrU nGOxPcS5+q1kfofwDdq4PyTy2NJzf45DFo6eE7jt5Wdbmxv6UG/Sl9CEs7ZOjZjQvVGT IZk5Pl/RbBMAplu6T2NXL33ag8HCDacObaOks0Y5I829lea0R9+FCKgPZxpHbgf2Y8Vy VSDw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=TEOri34U; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id h23si5307237ejj.573.2020.07.02.04.21.46; Thu, 02 Jul 2020 04:22:08 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=TEOri34U; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728666AbgGBLSl (ORCPT + 99 others); Thu, 2 Jul 2020 07:18:41 -0400 Received: from mail.kernel.org ([198.145.29.99]:46614 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726475AbgGBLSk (ORCPT ); Thu, 2 Jul 2020 07:18:40 -0400 Received: from willie-the-truck (236.31.169.217.in-addr.arpa [217.169.31.236]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id 70F432070C; Thu, 2 Jul 2020 11:18:36 +0000 (UTC) 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= Date: Thu, 2 Jul 2020 12:18:33 +0100 From: Will Deacon 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 , virtualization@lists.linux-foundation.org, Android Kernel Team Subject: Re: [PATCH 04/18] alpha: Override READ_ONCE() with barriered implementation 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 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: User-Agent: Mutt/1.10.1 (2018-07-13) Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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