Received: by 2002:a05:6a10:f347:0:0:0:0 with SMTP id d7csp9462806pxu; Mon, 28 Dec 2020 17:26:30 -0800 (PST) X-Google-Smtp-Source: ABdhPJzI4vdnu1PT7qgUzsCPAJk5x1u46E45208U/pvlNexo3vTUNTvZH5WT7jKzbEiPTIot2b45 X-Received: by 2002:aa7:d94e:: with SMTP id l14mr44027019eds.98.1609205190328; Mon, 28 Dec 2020 17:26:30 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1609205190; cv=none; d=google.com; s=arc-20160816; b=ozC0yaWIWxwRw9L6XS4Qm9GeavRO8dhsCEttuUnLstHeGDjodd9e9Gt2WIdf6gKp/Y 41zlWWsX/FrMuBxKrRhVXHVaE1ENCryS9qCssBjLp2AKrh37bdBVlHAIiBPliPjrNLTj o2jlEeANRwpCO7vvT2WpzQlmPquRlmPGvJnZcq4itEa8LIxBGT4Y8epMHTtVM8wQroqv 7UU50jg36N2WM2adoZplcCpPPqFk76Uxa7HtTRmIkDKktTv+HpfDv167LXkDGCW0l4fN LsABsFvrcarE2EPpeg7CdAU0vFyZO3sI6KrnRxdG9up6WSwr5tDPhKAaQqc/NTK5iKDM Y4pQ== 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=eSd/w17xiPPnLAKIvbc4UG/p09zbwuPGzXqzRTsYu+8=; b=kABbAgMXZn+GMRsXH0hVKu0yXsZ/cMZvcpmtRjG15YivCbxmfFSt3AHaTTeT6wpGsS l4OoDeQDuS7h9hdqFRNKPh7xKbKpx0DOYENhnzmd00NVmdRwtk0Vs6PNIUgJeJX03fjN RZ3vepYKOjRSe0g3K6MD77xnzS7dXCnts6HKxvBFyqaqVRoCWs89AybMlxvPTsyWcfBW nyyegMdrQwh2fw8FKHBurHzEVbZEdj6SuEAq9p0gPdv+oLqwXoTBJthJAbyvjAE52hhN D/aQRHIVekXNuqB5hGh2T23gOn6RBXVSdy/PhBKRk7GoQtSIsUu/rLRYqJWMNdm96DTQ 4oiQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=fail (test mode) header.i=@armlinux.org.uk header.s=pandora-2019 header.b=CByjhLjV; 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=fail (p=NONE sp=NONE dis=NONE) header.from=armlinux.org.uk Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id jz8si7194074ejb.237.2020.12.28.17.26.08; Mon, 28 Dec 2020 17:26:30 -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=fail (test mode) header.i=@armlinux.org.uk header.s=pandora-2019 header.b=CByjhLjV; 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=fail (p=NONE sp=NONE dis=NONE) header.from=armlinux.org.uk Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728882AbgL1TJn (ORCPT + 99 others); Mon, 28 Dec 2020 14:09:43 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:49532 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726420AbgL1TJm (ORCPT ); Mon, 28 Dec 2020 14:09:42 -0500 Received: from pandora.armlinux.org.uk (pandora.armlinux.org.uk [IPv6:2001:4d48:ad52:32c8:5054:ff:fe00:142]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C8678C0613D6; Mon, 28 Dec 2020 11:09:01 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=armlinux.org.uk; s=pandora-2019; h=Sender:In-Reply-To:Content-Type: MIME-Version:References:Message-ID:Subject:Cc:To:From:Date:Reply-To: Content-Transfer-Encoding:Content-ID:Content-Description:Resent-Date: Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Id: List-Help:List-Unsubscribe:List-Subscribe:List-Post:List-Owner:List-Archive; bh=eSd/w17xiPPnLAKIvbc4UG/p09zbwuPGzXqzRTsYu+8=; b=CByjhLjVZIVi+YbXCKUmIlvUn r9hy7wCEq46kHeYDMPFAGAI7FXlkBBapUVKxYRx7kWbb9YrwumQRz865RcDVTtAFVon7wtSGRGE7R bxvEACxEKGRZi1UWoB9e0/9CRVqkkbPuc0f/9lTNa4UvyfcOgs1mpT8kXXf4DCuwRIb7oTWQyQ2V1 18WSXm8IqVlwYe6UbFjuSD+Iy36OFYfYjqOIFPtcsjij1zrIWpgKFl6K1D9tlZfILFXw4Ply0IKb7 +3ire6hjbCpuwRcO+z4jjkFpxsC+lsJxoMuhoCpgZiTdlbotqQjKyX2Y0mv1faZ3ntUChS/zz4UsZ jI0iOd7eg==; Received: from shell.armlinux.org.uk ([fd8f:7570:feb6:1:5054:ff:fe00:4ec]:44612) by pandora.armlinux.org.uk with esmtpsa (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1ktxt1-0004df-Ab; Mon, 28 Dec 2020 19:08:55 +0000 Received: from linux by shell.armlinux.org.uk with local (Exim 4.92) (envelope-from ) id 1ktxsy-0000VF-Nz; Mon, 28 Dec 2020 19:08:52 +0000 Date: Mon, 28 Dec 2020 19:08:52 +0000 From: Russell King - ARM Linux admin To: Jann Horn Cc: Andy Lutomirski , Will Deacon , Mathieu Desnoyers , x86 , linux-kernel , Nicholas Piggin , Arnd Bergmann , Michael Ellerman , Benjamin Herrenschmidt , Paul Mackerras , linuxppc-dev , Catalin Marinas , linux-arm-kernel , stable Subject: Re: [RFC please help] membarrier: Rewrite sync_core_before_usermode() Message-ID: <20201228190852.GI1551@shell.armlinux.org.uk> References: <1836294649.3345.1609100294833.JavaMail.zimbra@efficios.com> <20201228102537.GG1551@shell.armlinux.org.uk> 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: Russell King - ARM Linux admin Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Mon, Dec 28, 2020 at 07:29:34PM +0100, Jann Horn wrote: > After chatting with rmk about this (but without claiming that any of > this is his opinion), based on the manpage, I think membarrier() > currently doesn't really claim to be synchronizing caches? It just > serializes cores. So arguably if userspace wants to use membarrier() > to synchronize code changes, userspace should first do the code > change, then flush icache as appropriate for the architecture, and > then do the membarrier() to ensure that the old code is unused? > > For 32-bit arm, rmk pointed out that that would be the cacheflush() > syscall. That might cause you to end up with two IPIs instead of one > in total, but we probably don't care _that_ much about extra IPIs on > 32-bit arm? > > For arm64, I believe userspace can flush icache across the entire > system with some instructions from userspace - "DC CVAU" followed by > "DSB ISH", or something like that, I think? (See e.g. > compat_arm_syscall(), the arm64 compat code that implements the 32-bit > arm cacheflush() syscall.) Note that the ARM cacheflush syscall calls flush_icache_user_range() over the range of addresses that userspace has passed - it's intention since day one is to support cases where userspace wants to change executable code. It will issue the appropriate write-backs to the data cache (DCCMVAU), the invalidates to the instruction cache (ICIMVAU), invalidate the branch target buffer (BPIALLIS or BPIALL as appropriate), and issue the appropriate barriers (DSB ISHST, ISB). Note that neither flush_icache_user_range() nor flush_icache_range() result in IPIs; cache operations are broadcast across all CPUs (which is one of the minimums we require for SMP systems.) Now, that all said, I think the question that has to be asked is... What is the basic purpose of membarrier? Is the purpose of it to provide memory barriers, or is it to provide memory coherence? If it's the former and not the latter, then cache flushes are out of scope, and expecting memory written to be visible to the instruction stream is totally out of scope of the membarrier interface, whether or not the writes happen on the same or a different CPU to the one executing the rewritten code. The documentation in the kernel does not seem to describe what it's supposed to be doing - the only thing I could find is this: Documentation/features/sched/membarrier-sync-core/arch-support.txt which describes it as "arch supports core serializing membarrier" whatever that means. Seems to be the standard and usual case of utterly poor to non-existent documentation within the kernel tree, or even a pointer to where any useful documentation can be found. Reading the membarrier(2) man page, I find nothing in there that talks about any kind of cache coherency for self-modifying code - it only seems to be about _barriers_ and nothing more, and barriers alone do precisely nothing to save you from non-coherent Harvard caches. So, either Andy has a misunderstanding, or the man page is wrong, or my rudimentary understanding of what membarrier is supposed to be doing is wrong... -- RMK's Patch system: https://www.armlinux.org.uk/developer/patches/ FTTP is here! 40Mbps down 10Mbps up. Decent connectivity at last!