Received: by 2002:a05:6358:d09b:b0:dc:cd0c:909e with SMTP id jc27csp14215350rwb; Sun, 27 Nov 2022 20:05:33 -0800 (PST) X-Google-Smtp-Source: AA0mqf42/XEdsXzquOE9APhlb3bHrmAiA7TZzoLk6L0t2pM2915Esc+DmmR1RDl6kxLLW7n3t+Za X-Received: by 2002:a17:906:350e:b0:7ae:f441:6ade with SMTP id r14-20020a170906350e00b007aef4416ademr29710778eja.436.1669608333597; Sun, 27 Nov 2022 20:05:33 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1669608333; cv=none; d=google.com; s=arc-20160816; b=0dk7+jncmJIJFY1qcS1AkAK3z7KexMN6IKbo+iBHsn564xQqnJAp7UWrqiSUEHFN/5 KbcCdYcQXRAryRD9o+ra1ZJRZH6E15PLdXi75R/ShQu77YqnbIJzzDbPs9YVB0cNl4v6 6RBtHHLnXMT6X+C7U5VqA/H8urQUXYzqC+EpuRs0PJztz9QjDvvFllNmm+xcTkjDlxSJ L/17Mn70nfi+i8WBFjrPDXfFW5sgRqOHh+qGe5lIfVxZLgHGd1kRGHH+8PqOin4AczhN i5ZkvigLS3eoJWIEGe6nu1f1q93sJNejhS5ZWc7Fr/OOz7vAjv6LoZUQ3ek5YCRNo8GM jmcw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :message-id:references:in-reply-to:user-agent:subject:cc:to:from :date:dkim-signature:dkim-filter; bh=pyo7Px7PpQM2HufyWL1vMcwAPfvOwNDGVJHyx4pR0Eo=; b=MiioDTV9eej2SNPmOv/P5LABbrzbzGicBwO4YTa4TmAbPM8istg/OBGOihrh+3ZIV6 hDPIZSj62ZUyUDoRWLdvnQjLX6XDjvW/wmxMBj/PVO/sbbZU9NfhRtZIpCngJtu5bKvz SwAu69LveBXeNIZDN9Bu+lHktHh1ddvk0wAwqSBcH32PPJObqv2v3RVJKwtzaEj88bLV jdZc3S4hDazbrD0SprzKK3kJP+w69gn4gKSjj6a/31kpDt+1N7Mkykw7cHpZ6yoPMCQc bMjODqGFZ1qy7RztZowcT2/pTW+AloWDy10slSLPoYPpK90nS6tTFQU/SD17o4aN9rNz 8WcQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@zytor.com header.s=2022110601 header.b=cU7V1Hgw; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=zytor.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id d13-20020a170906c20d00b00797670441desi6929320ejz.911.2022.11.27.20.05.13; Sun, 27 Nov 2022 20:05:33 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@zytor.com header.s=2022110601 header.b=cU7V1Hgw; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=zytor.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229921AbiK1Df2 (ORCPT + 84 others); Sun, 27 Nov 2022 22:35:28 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:40594 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229675AbiK1DfY (ORCPT ); Sun, 27 Nov 2022 22:35:24 -0500 Received: from mail.zytor.com (unknown [IPv6:2607:7c80:54:3::138]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id D1CAD2715 for ; Sun, 27 Nov 2022 19:35:23 -0800 (PST) Received: from [127.0.0.1] ([73.223.250.219]) (authenticated bits=0) by mail.zytor.com (8.17.1/8.17.1) with ESMTPSA id 2AS3YgFa2228734 (version=TLSv1.3 cipher=TLS_AES_128_GCM_SHA256 bits=128 verify=NO); Sun, 27 Nov 2022 19:34:43 -0800 DKIM-Filter: OpenDKIM Filter v2.11.0 mail.zytor.com 2AS3YgFa2228734 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=zytor.com; s=2022110601; t=1669606485; bh=pyo7Px7PpQM2HufyWL1vMcwAPfvOwNDGVJHyx4pR0Eo=; h=Date:From:To:CC:Subject:In-Reply-To:References:From; b=cU7V1Hgw40yLX8VxRwQFYAeaAkAtFA9OKt0fQ+q5LZ3VAitRxt5Rqk+OK1Zo0YUYN sIW/WfDBgyRt/GCwJAozxfDXpHEYtLxHvq65lJMLHPyTd+6/GgsKP8Sjo7zBtXvfhj 0cs8ZGKS8WIpZsbQUr6nS5c4x1tXQbE27ZyBLpcMq43aO7jmrBAwJMH0mQjEDyP7T9 kyp+u70AKyLhCztSMIeH4BL3AmDdy/a463T4vIpIZlJ5cQ4V6W3s+a+w1AhilTzusn 1pDRgvNbmDKHyzpS6xS8rD8535TyXZhizQDweXYaZ3Sl9OmAHVS9FjXkrP3DUx3+PV trhAjHytoqEqw== Date: Sun, 27 Nov 2022 19:34:39 -0800 From: "H. Peter Anvin" To: Hou Wenlong CC: linux-kernel@vger.kernel.org, Juergen Gross , "Srivatsa S. Bhat (VMware)" , Alexey Makhalov , VMware PV-Drivers Reviewers , Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , x86@kernel.org, Peter Zijlstra , Josh Poimboeuf , Kees Cook , Song Liu , Nadav Amit , virtualization@lists.linux-foundation.org Subject: =?US-ASCII?Q?Re=3A_=5BPATCH_v2=5D_x86/paravirt=3A_Use_relat?= =?US-ASCII?Q?ive_reference_for_original_instruction?= User-Agent: K-9 Mail for Android In-Reply-To: <20221128030320.GA101008@k08j02272.eu95sqa> References: <73c9ffac157087da78af9fca59cf7d8db7f17226.1669290510.git.houwenlong.hwl@antgroup.com> <20221128030320.GA101008@k08j02272.eu95sqa> Message-ID: <169A82BE-E5A9-4DB6-9CBE-055699F00213@zytor.com> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable X-Spam-Status: No, score=-1.3 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RDNS_NONE,SPF_HELO_PASS, SPF_PASS autolearn=no autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On November 27, 2022 7:03:20 PM PST, Hou Wenlong wrote: >On Sun, Nov 27, 2022 at 09:24:34AM -0800, H=2E Peter Anvin wrote: >> On November 24, 2022 3:51:53 AM PST, Hou Wenlong wrote: >> >Similar to the alternative patching, use relative reference for origin= al >> >instruction rather than absolute one, which saves 8 bytes for one entr= y >> >on x86_64=2E And it could generate R_X86_64_PC32 relocation instead o= f >> >R_X86_64_64 relocation, which also reduces relocation metadata on >> >relocatable builds=2E And the alignment could be hard coded to be 4 no= w=2E >> > >> >Signed-off-by: Hou Wenlong >> >--- >> > arch/x86/include/asm/paravirt=2Eh | 10 +++++----- >> > arch/x86/include/asm/paravirt_types=2Eh | 8 ++++---- >> > arch/x86/kernel/alternative=2Ec | 8 +++++--- >> > 3 files changed, 14 insertions(+), 12 deletions(-) >> > >> >diff --git a/arch/x86/include/asm/paravirt=2Eh b/arch/x86/include/asm/= paravirt=2Eh >> >index 2851bc2339d5=2E=2Ee56065ea73f2 100644 >> >--- a/arch/x86/include/asm/paravirt=2Eh >> >+++ b/arch/x86/include/asm/paravirt=2Eh >> >@@ -735,16 +735,16 @@ extern void default_banner(void); >> >=20 >> > #else /* __ASSEMBLY__ */ >> >=20 >> >-#define _PVSITE(ptype, ops, word, algn) \ >> >+#define _PVSITE(ptype, ops) \ >> > 771:; \ >> > ops; \ >> > 772:; \ >> > =2Epushsection =2Eparainstructions,"a"; \ >> >- =2Ealign algn; \ >> >- word 771b; \ >> >+ =2Ealign 4; \ >> >+ =2Elong 771b-=2E; \ >> > =2Ebyte ptype; \ >> > =2Ebyte 772b-771b; \ >> >- _ASM_ALIGN; \ >> >+ =2Ealign 4; \ >> > =2Epopsection >> >=20 >> >=20 >> >@@ -752,7 +752,7 @@ extern void default_banner(void); >> > #ifdef CONFIG_PARAVIRT_XXL >> >=20 >> > #define PARA_PATCH(off) ((off) / 8) >> >-#define PARA_SITE(ptype, ops) _PVSITE(ptype, ops, =2Equad, 8) >> >+#define PARA_SITE(ptype, ops) _PVSITE(ptype, ops) >> > #define PARA_INDIRECT(addr) *addr(%rip) >> >=20 >> > #ifdef CONFIG_DEBUG_ENTRY >> >diff --git a/arch/x86/include/asm/paravirt_types=2Eh b/arch/x86/includ= e/asm/paravirt_types=2Eh >> >index 8c1da419260f=2E=2E68952ae07a3f 100644 >> >--- a/arch/x86/include/asm/paravirt_types=2Eh >> >+++ b/arch/x86/include/asm/paravirt_types=2Eh >> >@@ -5,7 +5,7 @@ >> > #ifndef __ASSEMBLY__ >> > /* These all sit in the =2Eparainstructions section to tell us what t= o patch=2E */ >> > struct paravirt_patch_site { >> >- u8 *instr; /* original instructions */ >> >+ s32 instr_offset; /* original instructions */ >> > u8 type; /* type of this instruction */ >> > u8 len; /* length of original instruction */ >> > }; >> >@@ -273,11 +273,11 @@ extern struct paravirt_patch_template pv_ops; >> > #define _paravirt_alt(insn_string, type) \ >> > "771:\n\t" insn_string "\n" "772:\n" \ >> > "=2Epushsection =2Eparainstructions,\"a\"\n" \ >> >- _ASM_ALIGN "\n" \ >> >- _ASM_PTR " 771b\n" \ >> >+ " =2Ealign 4\n" \ >> >+ " =2Elong 771b-=2E\n" \ >> > " =2Ebyte " type "\n" \ >> > " =2Ebyte 772b-771b\n" \ >> >- _ASM_ALIGN "\n" \ >> >+ " =2Ealign 4\n" \ >> > "=2Epopsection\n" >> >=20 >> > /* Generate patchable code, with the default asm parameters=2E */ >> >diff --git a/arch/x86/kernel/alternative=2Ec b/arch/x86/kernel/alterna= tive=2Ec >> >index 111b809f0ac2=2E=2E6eea563a098d 100644 >> >--- a/arch/x86/kernel/alternative=2Ec >> >+++ b/arch/x86/kernel/alternative=2Ec >> >@@ -1232,20 +1232,22 @@ void __init_or_module apply_paravirt(struct pa= ravirt_patch_site *start, >> > { >> > struct paravirt_patch_site *p; >> > char insn_buff[MAX_PATCH_LEN]; >> >+ u8 *instr; >> >=20 >> > for (p =3D start; p < end; p++) { >> > unsigned int used; >> >=20 >> >+ instr =3D (u8 *)&p->instr_offset + p->instr_offset; >> > BUG_ON(p->len > MAX_PATCH_LEN); >> > /* prep the buffer with the original instructions */ >> >- memcpy(insn_buff, p->instr, p->len); >> >- used =3D paravirt_patch(p->type, insn_buff, (unsigned long)p->instr= , p->len); >> >+ memcpy(insn_buff, instr, p->len); >> >+ used =3D paravirt_patch(p->type, insn_buff, (unsigned long)instr, p= ->len); >> >=20 >> > BUG_ON(used > p->len); >> >=20 >> > /* Pad the rest with nops */ >> > add_nops(insn_buff + used, p->len - used); >> >- text_poke_early(p->instr, insn_buff, p->len); >> >+ text_poke_early(instr, insn_buff, p->len); >> > } >> > } >> > extern struct paravirt_patch_site __start_parainstructions[], >>=20 >> Any reason that you couldn't use the same patching code? > >Sorry, what do you mean using the same patching code? Do you >mean that share some code between apply_alternatives() and >apply_paravirt()? Yes=2E Abstract the facility rather than duplicate=2E