From patchwork Fri Jun 9 09:45:31 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Hou Wenlong X-Patchwork-Id: 105514 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:994d:0:b0:3d9:f83d:47d9 with SMTP id k13csp840982vqr; Fri, 9 Jun 2023 03:18:48 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ6peAtowCe5WpzRGhey1XpcL9i9dsNntWKL3OO12zTuHsPBP5RdlxXcqhuzJXsVyowqOe2N X-Received: by 2002:a05:6808:10d3:b0:39a:26e4:f455 with SMTP id s19-20020a05680810d300b0039a26e4f455mr1729887ois.0.1686305928401; Fri, 09 Jun 2023 03:18:48 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1686305928; cv=none; d=google.com; s=arc-20160816; b=N7u6IQPIMMbl2QsuPaOwGClMZ7h4pI2CdAsti25gEBoCTARnhkhAz+KeNtcgPdI8pZ HLdWIKL7SFOnBszoUXWtkWNFgoP0Wr2xtpjAcgSuuDPlab4wd3plnVzsbdT1h8294XdW BKjoA9l4Ym9z+2U7ZNkMl43JiZIwP60FEnPHt/dmA2Q1BFcFLn7JoeZ9Eda2dSuGX8BB Ap2U5m5M314rUqgbfoygVok3rQgSFO2UE2OQihKEa2rS9IQecfGrU3SfpfK4AQDXU2k5 9JZ/F7SgT9XilJR6slp3rIipByzIaTKaUd68MHeWmXXuwd0Z1byoBxnVMXNUnbX1ZHfJ /2EQ== 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:date:subject:cc:to:from; bh=29xRUd95UCxYhdCIa3g03wOiTIqDNoyEghbvpCmdLg8=; b=eQGrwEBq5jR7kAo08W4lsvIxIXmMs8kYrv9zBTi2/RGvBxrJkqF/PdBTyazcGBaNqI LARud2ezmCB3RJPDmpZJvY8FT2oPuwDC25YDQURD2SvThnybfFCF7iEWJ1yr2ePiNGQc EpYUXLqqoo8DNA2YqkppYNqfl83W4x5pW3jrg+cs5lG5pUV6C/8xMaDCK0VjxesYs+DY mG+7gQ/e5kMkLHiI1eCGKEp9ETbQnu0kbHgObEP5+eG6GnvzHPcggDpB9Kz7GuEPBA/E 9PtookejldMK8j96UhgJ3uLXSmrD7PPYbJ5pGXtAS8SpI9IlDyLRri0Aq5NacpBuilmG 3h5w== ARC-Authentication-Results: i=1; mx.google.com; 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=fail (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=antgroup.com Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id q24-20020a637518000000b0053fc2857c09si2538119pgc.652.2023.06.09.03.18.35; Fri, 09 Jun 2023 03:18:48 -0700 (PDT) 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; 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=fail (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=antgroup.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S241924AbjFIJz0 (ORCPT + 99 others); Fri, 9 Jun 2023 05:55:26 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45818 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S241968AbjFIJyt (ORCPT ); Fri, 9 Jun 2023 05:54:49 -0400 Received: from out0-199.mail.aliyun.com (out0-199.mail.aliyun.com [140.205.0.199]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 0272146AB for ; Fri, 9 Jun 2023 02:46:31 -0700 (PDT) X-Alimail-AntiSpam: AC=PASS;BC=-1|-1;BR=01201311R201e4;CH=green;DM=||false|;DS=||;FP=0|-1|-1|-1|0|-1|-1|-1;HT=ay29a033018047194;MF=houwenlong.hwl@antgroup.com;NM=1;PH=DS;RN=21;SR=0;TI=SMTPD_---.TPYicRa_1686303982; Received: from localhost(mailfrom:houwenlong.hwl@antgroup.com fp:SMTPD_---.TPYicRa_1686303982) by smtp.aliyun-inc.com; Fri, 09 Jun 2023 17:46:23 +0800 From: "Hou Wenlong" To: linux-kernel@vger.kernel.org Cc: "Lai Jiangshan" , "Hou Wenlong" , "Juergen Gross" , "Ajay Kaher" , "Alexey Makhalov" , "VMware PV-Drivers Reviewers" , "Thomas Gleixner" , "Ingo Molnar" , "Borislav Petkov" , "Dave Hansen" , , "H. Peter Anvin" , "Peter Zijlstra" , "Kees Cook" , "Nadav Amit" , "Luis Chamberlain" , "Song Liu" , "Christophe Leroy" , "Arnd Bergmann" , Subject: [PATCH 1/2] x86/paravirt: Use relative reference for original instruction Date: Fri, 09 Jun 2023 17:45:31 +0800 Message-Id: <9e6053107fbaabc0d33e5d2865c5af2c67ec9925.1686301237.git.houwenlong.hwl@antgroup.com> X-Mailer: git-send-email 2.31.1 MIME-Version: 1.0 X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00,SPF_HELO_NONE, SPF_PASS,T_SCC_BODY_TEXT_LINE,UNPARSEABLE_RELAY autolearn=ham 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 X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1768219925229246544?= X-GMAIL-MSGID: =?utf-8?q?1768219925229246544?= Similar to the alternative patching, use relative reference for original instruction rather than absolute one, which saves 8 bytes for one entry on x86_64. And it could generate R_X86_64_PC32 relocation instead of R_X86_64_64 relocation, which also reduces relocation metadata on relocatable builds. And the alignment could be hard coded to be 4 now. Signed-off-by: Hou Wenlong Reviewed-by: Juergen Gross --- arch/x86/include/asm/paravirt.h | 10 +++++----- arch/x86/include/asm/paravirt_types.h | 8 ++++---- arch/x86/kernel/alternative.c | 8 +++++--- arch/x86/kernel/callthunks.c | 2 +- 4 files changed, 15 insertions(+), 13 deletions(-) diff --git a/arch/x86/include/asm/paravirt.h b/arch/x86/include/asm/paravirt.h index b49778664d2b..2350ceb43db0 100644 --- a/arch/x86/include/asm/paravirt.h +++ b/arch/x86/include/asm/paravirt.h @@ -742,16 +742,16 @@ extern void default_banner(void); #else /* __ASSEMBLY__ */ -#define _PVSITE(ptype, ops, word, algn) \ +#define _PVSITE(ptype, ops) \ 771:; \ ops; \ 772:; \ .pushsection .parainstructions,"a"; \ - .align algn; \ - word 771b; \ + .align 4; \ + .long 771b-.; \ .byte ptype; \ .byte 772b-771b; \ - _ASM_ALIGN; \ + .align 4; \ .popsection @@ -759,7 +759,7 @@ extern void default_banner(void); #ifdef CONFIG_PARAVIRT_XXL #define PARA_PATCH(off) ((off) / 8) -#define PARA_SITE(ptype, ops) _PVSITE(ptype, ops, .quad, 8) +#define PARA_SITE(ptype, ops) _PVSITE(ptype, ops) #define PARA_INDIRECT(addr) *addr(%rip) #ifdef CONFIG_DEBUG_ENTRY diff --git a/arch/x86/include/asm/paravirt_types.h b/arch/x86/include/asm/paravirt_types.h index 4acbcddddc29..982a234f5a06 100644 --- a/arch/x86/include/asm/paravirt_types.h +++ b/arch/x86/include/asm/paravirt_types.h @@ -5,7 +5,7 @@ #ifndef __ASSEMBLY__ /* These all sit in the .parainstructions section to tell us what to patch. */ 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 */ }; @@ -270,11 +270,11 @@ extern struct paravirt_patch_template pv_ops; #define _paravirt_alt(insn_string, type) \ "771:\n\t" insn_string "\n" "772:\n" \ ".pushsection .parainstructions,\"a\"\n" \ - _ASM_ALIGN "\n" \ - _ASM_PTR " 771b\n" \ + " .align 4\n" \ + " .long 771b-.\n" \ " .byte " type "\n" \ " .byte 772b-771b\n" \ - _ASM_ALIGN "\n" \ + " .align 4\n" \ ".popsection\n" /* Generate patchable code, with the default asm parameters. */ diff --git a/arch/x86/kernel/alternative.c b/arch/x86/kernel/alternative.c index d9a0e28ff62b..191fda1b17f1 100644 --- a/arch/x86/kernel/alternative.c +++ b/arch/x86/kernel/alternative.c @@ -1364,20 +1364,22 @@ void __init_or_module apply_paravirt(struct paravirt_patch_site *start, { struct paravirt_patch_site *p; char insn_buff[MAX_PATCH_LEN]; + u8 *instr; for (p = start; p < end; p++) { unsigned int used; + instr = (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 = paravirt_patch(p->type, insn_buff, (unsigned long)p->instr, p->len); + memcpy(insn_buff, instr, p->len); + used = paravirt_patch(p->type, insn_buff, (unsigned long)instr, p->len); BUG_ON(used > p->len); /* 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[], diff --git a/arch/x86/kernel/callthunks.c b/arch/x86/kernel/callthunks.c index 8bb937331acb..6f5e2447d5a6 100644 --- a/arch/x86/kernel/callthunks.c +++ b/arch/x86/kernel/callthunks.c @@ -245,7 +245,7 @@ patch_paravirt_call_sites(struct paravirt_patch_site *start, struct paravirt_patch_site *p; for (p = start; p < end; p++) - patch_call(p->instr, ct); + patch_call((void *)&p->instr_offset + p->instr_offset, ct); } static __init_or_module void