Received: by 2002:a25:6193:0:0:0:0:0 with SMTP id v141csp117524ybb; Tue, 7 Apr 2020 18:19:09 -0700 (PDT) X-Google-Smtp-Source: APiQypLWk85SmRbxJyr5egwTL+679v7/fbeFZqZkW1DXQ8QsVMRLgF09Eh2MfqY4C0lKUad9kKHn X-Received: by 2002:aca:d68a:: with SMTP id n132mr775840oig.40.1586308749182; Tue, 07 Apr 2020 18:19:09 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1586308749; cv=none; d=google.com; s=arc-20160816; b=Vba6XVRDAU8abpBpSyosL4qgHFAannL3L6IUik23tFMVSK+le6re3j9f8f0ABFAZZw JjTzUulCH5C9rGj46Rn5nIlG4RZ6XlDCyVCsvfcHVhEb9FnUAmbWBNlAd7cXOeiFeFpo W3qgscw9e0jS2znyIcLYt239fknxpMVFS7Vl8MgFyWA7BeSqhcoPFYofUookpnSiPpp8 vdOl/SVa/qKGftw23nAZR/oSDWGPa0ZWh70vzjKPS3KrxlaiMsCrOXyoF6+C58IU9ptk UOYZd5gx8yjf6Q7706Biebw+It2/rf86GtgBQEsQ/II2eIs3mdZwn87C+V0/h7IJhEOc ws5Q== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-language :content-transfer-encoding:in-reply-to:mime-version:user-agent:date :message-id:from:references:cc:to:subject; bh=EE0o2Eq7ntjAsSOSDbU7J5Q9HqQ1uPHqp4eV2jS7vhs=; b=SI6voc7/nJiakE7oYy6nSC2uQPwUYVP3W17MS3StTETBxVTihpk2TF9k8lHzQDlATQ zTawGGtj24ixKxWL39Ww2PWx9QG8Y+mn1FvavDo8c8DfjC4HpABSyqJggkD2ETDtX6FQ Dxpu+qHiW7/cElfHZ57+NoqB82P27V/+/CZYHGWELI5RXJ5erozUesHjG3gSBG8zRXMT RlhUXt8gc0pbNLSzRKvhs+5E1f3YBH+eDqO2Cp3Jb69PrWgIu6CsSMEExj7PSKqTWBuR gpPjiOmpeTk0/3RhRx+FDNEz2jNxlTSIY45+BqDj6h8ij1ALNNLdmSVOGdHW0IDs8Qq3 cEoA== ARC-Authentication-Results: i=1; mx.google.com; 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 Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id l5si1707276oic.3.2020.04.07.18.18.28; Tue, 07 Apr 2020 18:19:09 -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; 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 Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726428AbgDHBRa (ORCPT + 99 others); Tue, 7 Apr 2020 21:17:30 -0400 Received: from szxga05-in.huawei.com ([45.249.212.191]:12693 "EHLO huawei.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1726406AbgDHBR3 (ORCPT ); Tue, 7 Apr 2020 21:17:29 -0400 Received: from DGGEMS401-HUB.china.huawei.com (unknown [172.30.72.60]) by Forcepoint Email with ESMTP id CE0E864582508C10A993; Wed, 8 Apr 2020 09:17:26 +0800 (CST) Received: from [127.0.0.1] (10.133.217.205) by DGGEMS401-HUB.china.huawei.com (10.3.19.201) with Microsoft SMTP Server id 14.3.487.0; Wed, 8 Apr 2020 09:17:15 +0800 Subject: Re: Why is text_mutex used in jump_label_transform for x86_64 To: Will Deacon , Peter Zijlstra CC: , , , Kees Cook , "x86@kernel.org" , "linux-kernel@vger.kernel.org" , , "Xiexiuqi (Xie XiuQi)" , Li Bin , , "chengjian (D)" References: <20200320102709.GC20696@hirez.programming.kicks-ass.net> <28edc3d5-83a3-43cb-3e64-7d0525d430f3@huawei.com> <20200406091551.GG20730@hirez.programming.kicks-ass.net> <20200406141020.GB3178@willie-the-truck> From: "chengjian (D)" Message-ID: Date: Wed, 8 Apr 2020 09:17:15 +0800 User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:68.0) Gecko/20100101 Thunderbird/68.6.0 MIME-Version: 1.0 In-Reply-To: <20200406141020.GB3178@willie-the-truck> Content-Type: text/plain; charset="utf-8"; format=flowed Content-Transfer-Encoding: 8bit Content-Language: en-US X-Originating-IP: [10.133.217.205] X-CFilter-Loop: Reflected Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On 2020/4/6 22:10, Will Deacon wrote: > 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 > > . Hi, Peter and Will     I have learned.     I truly appreciate your timely help.     Thanks a lot.     -- Cheng Jian