Received: by 2002:a05:6a10:f347:0:0:0:0 with SMTP id d7csp352387pxu; Tue, 5 Jan 2021 12:50:41 -0800 (PST) X-Google-Smtp-Source: ABdhPJyloK+fXcKomKz5NfkBshcfdyD+ABXeUVv//HcX6HcW2nCvXCAxEgMVxl+xzta+Z22SWB6y X-Received: by 2002:a05:6402:b9a:: with SMTP id cf26mr1591488edb.372.1609879841044; Tue, 05 Jan 2021 12:50:41 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1609879841; cv=none; d=google.com; s=arc-20160816; b=Hrj02765ZaeVfkJPXudG6vo6FJOjW6w5iWnelDNPuF2m7AXWp7/wnKD84c+8n7axlm glVTf+BkitmWEYul1xeSMh8Gkv5cnrt0xurW9wlJTJY+m2FDP1af/Sy3jFQRQkUJcwZl Kg2VTX0RWVVvt01mcQHXGsx8X8cgcTCM2w3ZMVifUQ4UNgOfVRbHzr11m+uIMB/V/L/w PDBWvQUoPE/4TB7c1ZVVVLY+DQJ15+di/7NlmeTNkFE3/NOz7MAXS9cg6AZAQSPaBZQ+ ZaWDOgwGyiYHIl9eJGxiVidZRx+aPHbB+wGo10csq+2QS5FGWdas0dNECs2ak8VGFHCc qlWg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:user-agent:in-reply-to:content-disposition :mime-version:references:message-id:subject:cc:to:from:date :dkim-signature; bh=TJhrJj2VAeXA7ZN/JUT2ZjYuR8UYpUgimbrV6Rf6hs8=; b=Z+unfbgitSd24I+5FLxkqcIFhxH4YrfTlWGyMg+l3V+3A0Mj862q9u0uQN1ghrYdb/ vCLfjPykMUQscFbeo215i25JpKfXngkOXSuY1YY56jbkf9ZP1oiCoXfU+Bd01j8d+nzZ qnTx8lR/xtrRZCaiqld9gt+zHWakSgbXzKjRonOIok0OloAEtCPleBiby48GyhTzGiCS c0dw4lLnFZI4a+Z0PvLSfbzWl2GnXYdNr9IQQgtLCgUXl+42j3HIxvYwnzoO72xrcXLD +BIlqyf0/Mwpk3w0TqO624aeAyUzPA8KTq3+0gX/6FSQi5T089MHmFdfpxncwod0UO/p E29g== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=h6CBueej; 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 bu13si125796ejb.588.2021.01.05.12.50.17; Tue, 05 Jan 2021 12:50:41 -0800 (PST) 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=k20201202 header.b=h6CBueej; 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 S1729095AbhAEN1K (ORCPT + 99 others); Tue, 5 Jan 2021 08:27:10 -0500 Received: from mail.kernel.org ([198.145.29.99]:34932 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728949AbhAEN1J (ORCPT ); Tue, 5 Jan 2021 08:27:09 -0500 Received: by mail.kernel.org (Postfix) with ESMTPSA id F2194225AC; Tue, 5 Jan 2021 13:26:26 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1609853189; bh=TmYNnmKpWB2t2Q9JsIHgrp8V/YLclj6QFBBv76JxcGI=; h=Date:From:To:Cc:Subject:References:In-Reply-To:From; b=h6CBueejxaIU+YgJTNSnl5xkqm0WnsJZ3JQodk76eUGNSvSvFWbgqtFlerBNe46O7 KCfs6qTXHvoxl+KgOgnyYCRImBCjtoRl0Sv9bjlNM7osRSXL5kzyWRuDlBkuA2sEY2 QXO+02ZXf0Vjg2WEgjj4nZeO5cWIkYARBS9IHsx3rT4etAm5hg7/iHbpBjw4rbhuhK qIJDgYPl94MPe9+jLH00xJ+fEhNuG22bB4+WGxTTh4ZRaynB+XUSxJRtTlY+EJebkh /xRJUMzBCY5wKF+Ee1YMNjFs4ud+Tv7pSwnGv6QdEHPG7ED4APzW6rwyo2ugYIjNbk qXL4nmbqdI1PQ== Date: Tue, 5 Jan 2021 13:26:23 +0000 From: Will Deacon To: Andy Lutomirski Cc: Nicholas Piggin , Mathieu Desnoyers , X86 ML , Arnd Bergmann , Benjamin Herrenschmidt , Catalin Marinas , linux-arm-kernel , LKML , linuxppc-dev , Michael Ellerman , Paul Mackerras , stable Subject: Re: [RFC please help] membarrier: Rewrite sync_core_before_usermode() Message-ID: <20210105132623.GB11108@willie-the-truck> References: <1609199804.yrsu9vagzk.astroid@bobo.none> 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) Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Hi Andy, Sorry for the slow reply, I was socially distanced from my keyboard. On Mon, Dec 28, 2020 at 04:36:11PM -0800, Andy Lutomirski wrote: > On Mon, Dec 28, 2020 at 4:11 PM Nicholas Piggin wrote: > > > +static inline void membarrier_sync_core_before_usermode(void) > > > +{ > > > + /* > > > + * XXX: I know basically nothing about powerpc cache management. > > > + * Is this correct? > > > + */ > > > + isync(); > > > > This is not about memory ordering or cache management, it's about > > pipeline management. Powerpc's return to user mode serializes the > > CPU (aka the hardware thread, _not_ the core; another wrongness of > > the name, but AFAIKS the HW thread is what is required for > > membarrier). So this is wrong, powerpc needs nothing here. > > Fair enough. I'm happy to defer to you on the powerpc details. In > any case, this just illustrates that we need feedback from a person > who knows more about ARM64 than I do. I think we're in a very similar boat to PowerPC, fwiw. Roughly speaking: 1. SYNC_CORE does _not_ perform any cache management; that is the responsibility of userspace, either by executing the relevant maintenance instructions (arm64) or a system call (arm32). Crucially, the hardware will ensure that this cache maintenance is broadcast to all other CPUs. 2. Even with all the cache maintenance in the world, a CPU could have speculatively fetched stale instructions into its "pipeline" ahead of time, and these are _not_ flushed by the broadcast maintenance instructions in (1). SYNC_CORE provides a means for userspace to discard these stale instructions. 3. The context synchronization event on exception entry/exit is sufficient here. The Arm ARM isn't very good at describing what it does, because it's in denial about the existence of a pipeline, but it does have snippets such as: (s/PE/CPU/) | For all types of memory: | The PE might have fetched the instructions from memory at any time | since the last Context synchronization event on that PE. Interestingly, the architecture recently added a control bit to remove this synchronisation from exception return, so if we set that then we'd have a problem with SYNC_CORE and adding an ISB would be necessary (and we could probable then make kernel->kernel returns cheaper, but I suspect we're relying on this implicit synchronisation in other places too). Are you seeing a problem in practice, or did this come up while trying to decipher the semantics of SYNC_CORE? Will