Received: by 2002:a25:6193:0:0:0:0:0 with SMTP id v141csp3345409ybb; Mon, 6 Apr 2020 07:12:13 -0700 (PDT) X-Google-Smtp-Source: APiQypKfeRl3eae+s9PlaMDmKX0r9iWHcgv2aDr92o7Yzmpm8ii/8CFJwRReLXZg+wmjVzlh3Esi X-Received: by 2002:a05:6830:3151:: with SMTP id c17mr18655471ots.310.1586182333519; Mon, 06 Apr 2020 07:12:13 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1586182333; cv=none; d=google.com; s=arc-20160816; b=XANbF5F1lMEBeBRv5vXX3K0HUToa7HxJFLHgGfsiuQLlVAYikiu15LtgtFXFr7Jweg qdh9nKnl43V6/nXFQB4r029ZJd0fZHsf2GgcDb0c5b++O4UJDktuTYIV03ME4nMjOPOT NfRK16HzaTPPWFg0oqoquHNxEKoeo2DOYe645mKLV4ek9S1Dn6bXMfTSRkt+NwMNiYdG EwggB05T7J5PlVNJkwX/X68QRt2nEKWdCzum7Vyo/XlZznUGCUu9aXI+33u3tY0LTThL CBBU1CDjX0W8KfDKuGXOpSYIhrPHbHlrTm029G5wHSpYqLgmEeyN6VADAe2Alh2UTqwj iDlg== 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-transfer-encoding:content-disposition:mime-version :references:message-id:subject:cc:to:from:date:dkim-signature; bh=iA59WaxbaQZNnQdZ4ggRxM5e3TycEx9BpRx5DxORHJk=; b=vwcLLZ0e0EeMjNIS/JYgif2dSVIZS70S1RWlGqjaHbYoTVmJFJ7TXGW8/PDSZ3Sa6y fRvT21IfsdmpaX3PV7VhcG+/fyCFGu1rwWHaYaGjhAabMnYs01Nqdc5YMO8XyP9SdDwS M+XMnfTVqt8T86LD5EkXb27CDcfXFzIjKPH1fvpuGd5Sw/vhZjPDawqpa92v4Zkp+M28 Vg8wQjykeGtE2Sqd55PqcYFIkYaxw2crWc17qjmg23Py62ry5i9jIr9aKIJOG7Dqc4jn PaTB+JmPxR12ONMDCBdGFb8XNrFbFSsGqig8DLAdN9C4vHHzhatphw4GsawlCBErASwu YoJg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=CON5JSBs; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 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. [209.132.180.67]) by mx.google.com with ESMTP id g18si7201291otq.133.2020.04.06.07.11.59; Mon, 06 Apr 2020 07:12:13 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=CON5JSBs; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 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 S1728578AbgDFOK1 (ORCPT + 99 others); Mon, 6 Apr 2020 10:10:27 -0400 Received: from mail.kernel.org ([198.145.29.99]:37468 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728405AbgDFOK1 (ORCPT ); Mon, 6 Apr 2020 10:10:27 -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 E12912395B; Mon, 6 Apr 2020 14:10:23 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1586182225; bh=OgOGUCitRRP/T8mKYZLXW0xweJNWsGNMLQglNR9Ce8c=; h=Date:From:To:Cc:Subject:References:In-Reply-To:From; b=CON5JSBs/tPWIZV3Nd9bYqXiekdGMRmbI5kIIE99ckLSpocYPhdy/06x90OACGYdO zPiEJLs8YCmx4ZHTayBxPyVjceHWp/QeglX+Oudhf97+w8INuAN8RTgdRClz6zIYBO wctIMM7IHW30WaN9QJtXvQiHGcK1LNpfV2locpwg= Date: Mon, 6 Apr 2020 15:10:20 +0100 From: Will Deacon To: Peter Zijlstra Cc: "chengjian (D)" , andrew.murray@arm.com, bristot@redhat.com, jakub.kicinski@netronome.com, Kees Cook , "x86@kernel.org" , "linux-kernel@vger.kernel.org" , linux-arm-kernel@lists.infradead.org, "Xiexiuqi (Xie XiuQi)" , Li Bin , bobo.shaobowang@huawei.com Subject: Re: Why is text_mutex used in jump_label_transform for x86_64 Message-ID: <20200406141020.GB3178@willie-the-truck> References: <20200320102709.GC20696@hirez.programming.kicks-ass.net> <28edc3d5-83a3-43cb-3e64-7d0525d430f3@huawei.com> <20200406091551.GG20730@hirez.programming.kicks-ass.net> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: <20200406091551.GG20730@hirez.programming.kicks-ass.net> 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 Mon, Apr 06, 2020 at 11:15:51AM +0200, Peter Zijlstra wrote: > On Mon, Apr 06, 2020 at 04:39:11PM +0800, chengjian (D) wrote: > > > > On 2020/3/20 18:27, Peter Zijlstra wrote: > > > It depends on the architecture details of how self-modifying code works. > > > In particular, x86 is a variable instruction length architecture and > > > needs extreme care -- it's implementation requires there only be a > > > single text modifier at any one time, hence the use of text_mutex. > > > > > > ARM64 OTOH is, like most RISC based architectures, a fixed width > > > instruction architecture. And in particular it can re-write certain > > > (branch) instructions with impunity (see their > > > aarch64_insn_patch_text_nosync()). Which is why they don't need > > > additional serialization. > > > > Hi, Peter > > > > Thank you very much for your reply. > > > > X86 is a variable-length instruction, only one byte modification of the > > instruction > > can be regarded as atomic. so we must be very careful when modifying > > instructions > > concurrently. > > Close enough. > > > For other architectures such as ARM64, the modification of some instructions > > can be > > considered atomic, (Eg. nop -> jmp/b). The set of instructions that can be > > executed > > by one thread of execution as they are being modified by another thread of > > execution > > without requiring explicit synchronization. > > > > In ARM64 Architecture Reference Manual, I find that: > >     Concurrent modification and execution of instructions can lead to the > > resulting instruction performing any behavior > >     that can be achieved by executing any sequence of instructions that can > > be executed from the same Exception level, > >     except where each of the instruction before modification and the > > instruction after modification is one of a B, BL, BRK, > >     HVC, ISB, NOP, SMC, or SVC instruction. > >     For the B, BL, BRK, HVC, ISB, NOP, SMC, and SVC instructions the > > architecture guarantees that, after modification of the > >     instruction, behavior is consistent with execution of either: > >     • The instruction originally fetched. > >     • A fetch of the modified instruction > > > > So we can safely modify jump_label for ARM64(from NOP to b or form b to > > NOP). > > > > Is my understanding correct? > > I think so; but I'm really not much of an ARM64 person. FWIW I think I > remember Will saying the same is true of ARM (32bit) and they could > implement the same optimization, but so far nobody has bothered doing > so. But please, ask an ARM64 maintainer and don't take my word for this. On 32-bit there are complications with Thumb-2 instructions where you can have a mixture of 16-bit and 32-bit encodings, so you have to be pretty careful there. For arm64, we have aarch64_insn_patch_text_nosync() which we use to toggle jump labels. Will