Received: by 2002:a05:6a10:f347:0:0:0:0 with SMTP id d7csp9461462pxu; Mon, 28 Dec 2020 17:23:22 -0800 (PST) X-Google-Smtp-Source: ABdhPJwU3+iXbDvY9u3SnGj+WKQvvXY6GxmF5jwaMb6y68xgVObDItwXqM1+hvi1aQpzpXv3jh6p X-Received: by 2002:aa7:d511:: with SMTP id y17mr44380655edq.249.1609205001761; Mon, 28 Dec 2020 17:23:21 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1609205001; cv=none; d=google.com; s=arc-20160816; b=clfRRt5Cvd2eaJxu2m5SehwTWUr3cB/jLTibKOssocBs4Gn0WaDlRiePL9Il+p6PDo 9JsXDUt0V2/Ttajq+bjfbGs3iJxtj+Gkm8VjrLs7NQsT3ak5x/R7t9RqmCQ66/eXB+oB wUda3NImD5Yv2DfWndCzVUV7PDjo+vAILR/+8jGXxD16Jpowp8fuChkL+FV3GqSUa1BS FW/TbcSbVRB18EiIAKjmVVwZ5v5ov7/e86uH6bzuMQL1wVM92M47mTrL4DGFUPNvnq0x lnPiZTOyz21+SOHyla+O3UwlgGMBjgGCw30kGHnhWcNIgDJMB27aYooDNSOVOAygl5HQ ugXw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:subject:message-id:date:from:in-reply-to :references:mime-version:dkim-signature; bh=PHk4GI9q32WqcIPn06pJ6Es3GNl1YPxHrrW5jztEleI=; b=PrEHLMmC4/UAX2UU2LUNNySE+F5ggqe4C668h3PhK656nmgxVPLRxLNVRJSis0Ycum hP9IM5tifsSFmQ7xiJd8sbTU5TKvlXQSdCyK1h21U9ZRYkLowi8P67wov6YeHewF/DQF uW7Csh2J27nFUU2Puy12p13nprWadJffqC6kwHXulfvvU0j2/7qOGoa8pLziCn3bYkC5 nUQ8doea8Cqrlv5X9TPUBUNal/RkoH5JndDbtcEX/JBnJW/RidiKjYgAeZc5m4p5j0gY CLlK9zD2ZY0fdIMzADoIWgv9orHYXx4XmeT45Xlew6DLOANjjh7VdDVcgzAYhkMsjRgH O3Fw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=AaTJQGDx; 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 k17si18904867ejc.181.2020.12.28.17.22.59; Mon, 28 Dec 2020 17:23:21 -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=AaTJQGDx; 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 S1727280AbgL1SvQ (ORCPT + 99 others); Mon, 28 Dec 2020 13:51:16 -0500 Received: from mail.kernel.org ([198.145.29.99]:37982 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726420AbgL1SvP (ORCPT ); Mon, 28 Dec 2020 13:51:15 -0500 Received: by mail.kernel.org (Postfix) with ESMTPSA id 4694222B2B for ; Mon, 28 Dec 2020 18:50:34 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1609181434; bh=6YqwLVWGZ7ybOF3Ak7Rutxzpv/0P68frB/ABbtNRx2A=; h=References:In-Reply-To:From:Date:Subject:To:Cc:From; b=AaTJQGDx58ZoPUwIflwplFjWLnIFD807YZxgy/wYiFKWT1laWnPttoNEf6fcvtHHU +1CZ1iOYgjTejeFWS++DiH29gBHSy8svPa+lVVWyGWKVVVrLK0b4deZrIMXHHkUZav Cbh7w6P49o2HymZPH6nKYAV5V/DIz4ohgdngS6R+6FETrskhWBi87AJduXHvoFDd99 f4+7QHkuPbqsmYqNWe6Q41r1iNa231xtvKSnlNbahIH/zwC+CDwxNnAeD2oXFPcWSf M9A+zrh7bnLWxYVHHSprHpra4oKbHl/w+xYPE2jSLv3Y/SWoptfm2qEu1lf6V4xPqh 2uJEMxqBjqKBw== Received: by mail-wm1-f42.google.com with SMTP id e25so271046wme.0 for ; Mon, 28 Dec 2020 10:50:34 -0800 (PST) X-Gm-Message-State: AOAM533BcUcxTCi1yN6c/YUR1VxWfH0gXeMSVGByfkdrwa9X78RguDtw ruwKQ/uDTodjdZVva6CeaYZUj7ryI/MEoAPOaIgHGA== X-Received: by 2002:a7b:c40b:: with SMTP id k11mr257558wmi.36.1609181432795; Mon, 28 Dec 2020 10:50:32 -0800 (PST) MIME-Version: 1.0 References: <1836294649.3345.1609100294833.JavaMail.zimbra@efficios.com> <20201228102537.GG1551@shell.armlinux.org.uk> In-Reply-To: From: Andy Lutomirski Date: Mon, 28 Dec 2020 10:50:20 -0800 X-Gmail-Original-Message-ID: Message-ID: Subject: Re: [RFC please help] membarrier: Rewrite sync_core_before_usermode() To: Jann Horn Cc: Andy Lutomirski , Will Deacon , Russell King - ARM Linux admin , Mathieu Desnoyers , x86 , linux-kernel , Nicholas Piggin , Arnd Bergmann , Michael Ellerman , Benjamin Herrenschmidt , Paul Mackerras , linuxppc-dev , Catalin Marinas , linux-arm-kernel , stable Content-Type: text/plain; charset="UTF-8" Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Mon, Dec 28, 2020 at 10:30 AM Jann Horn wrote: > > On Mon, Dec 28, 2020 at 6:14 PM Andy Lutomirski wrote: > > On Mon, Dec 28, 2020 at 2:25 AM Russell King - ARM Linux admin > > wrote: > > > > > > On Sun, Dec 27, 2020 at 01:36:13PM -0800, Andy Lutomirski wrote: > > > > On Sun, Dec 27, 2020 at 12:18 PM Mathieu Desnoyers > > > > wrote: > > > > > > > > > > ----- On Dec 27, 2020, at 1:28 PM, Andy Lutomirski luto@kernel.org wrote: > > > > > > > > > > > > > > > > > > > > > I admit that I'm rather surprised that the code worked at all on arm64, > > > > > > and I'm suspicious that it has never been very well tested. My apologies > > > > > > for not reviewing this more carefully in the first place. > > > > > > > > > > Please refer to Documentation/features/sched/membarrier-sync-core/arch-support.txt > > > > > > > > > > It clearly states that only arm, arm64, powerpc and x86 support the membarrier > > > > > sync core feature as of now: > > > > > > > > Sigh, I missed arm (32). Russell or ARM folks, what's the right > > > > incantation to make the CPU notice instruction changes initiated by > > > > other cores on 32-bit ARM? > > > > > > You need to call flush_icache_range(), since the changes need to be > > > flushed from the data cache to the point of unification (of the Harvard > > > I and D), and the instruction cache needs to be invalidated so it can > > > then see those updated instructions. This will also take care of the > > > necessary barriers that the CPU requires for you. > > > > With what parameters? From looking at the header, this is for the > > case in which the kernel writes some memory and then intends to > > execute it. That's not what membarrier() does at all. membarrier() > > works like this: > > > > User thread 1: > > > > write to RWX memory *or* write to an RW alias of an X region. > > membarrier(...); > > somehow tell thread 2 that we're ready (with a store release, perhaps, > > or even just a relaxed store.) > > > > User thread 2: > > > > wait for the indication from thread 1. > > barrier(); > > jump to the code. > > > > membarrier() is, for better or for worse, not given a range of addresses. > > > > On x86, the documentation is a bit weak, but a strict reading > > indicates that thread 2 must execute a serializing instruction at some > > point after thread 1 writes the code and before thread 2 runs it. > > membarrier() does this by sending an IPI and ensuring that a > > "serializing" instruction (thanks for great terminology, Intel) is > > executed. Note that flush_icache_range() is a no-op on x86, and I've > > asked Intel's architects to please clarify their precise rules. No > > response yet. > > > > On arm64, flush_icache_range() seems to send an IPI, and that's not > > what I want. membarrier() already does an IPI. > > 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? I haven't the faintest clue what "serializes cores" means. It seems to be a bit of a mishmash of x86 SDM terminology and Linux x86 "sync_core" terminology. The latter means very little to me, even as an x86 person. I'm moderately confident that the *intent* is that a multithreaded program can write some JIT code to memory, do this membarrier() operation, and then execute the code, and it will work. Maybe it's even intended to work cross-architecture without any additional help from the program. But maybe the existing API is simply incorrect for this. > > 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.) I have no idea what DC anything does. Based on my very cursory reading of the manual, ISB is the right approach, but I don't pretend I understand all the details.