From patchwork Thu Nov 24 08:51:20 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Hou Wenlong X-Patchwork-Id: 25383 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:adf:f944:0:0:0:0:0 with SMTP id q4csp3273294wrr; Thu, 24 Nov 2022 00:57:46 -0800 (PST) X-Google-Smtp-Source: AA0mqf4BoDZ1ifhiRnkiJ6sZMNAvFLmyUoxXayO4y53/oHRzS69a4sxLv1qW1Wwl4EZyNbyB+zZf X-Received: by 2002:a63:f962:0:b0:477:1bb8:bbf4 with SMTP id q34-20020a63f962000000b004771bb8bbf4mr11164683pgk.19.1669280265692; Thu, 24 Nov 2022 00:57:45 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1669280265; cv=none; d=google.com; s=arc-20160816; b=ecA9fT5KwVwDpOhN24oGPKtXo+xH4QAwF/xgBCUWbHP50vJnkqh15hnED/r+yTPhiv 0ksZto1/a08LexcOZ0N4KfTv8YFDekOjQNdachh5D+irISq0r6/DkX5ZKXKaNARGKsv8 p4kCERXHYrUbds8yUpHwCG6uIuQS3q7UG0Ve1yKl91ty6xrC9iEEqFrJ+fI+Bvf2xXWM vH55tMH738U10IuV2avWnxYEEbbWVMCYOdIy7ISvpxjord3JhHcxGEefVdQEWLseLjiv 12zri14QKtTGDuUv/HeunXJFglP41RSrbHFkYXBU85ax7S2ZIkwaeXvLlCzS9mt+SeiQ Wiiw== 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=K1P9Gz9HNmMlu6Os3FsalcVHS2mNzvxT55lAP9vFvsE=; b=eVFGF4jRKqmVUs0ld8KEjXwb/g1Kp0wXwGSXEqF8gsB4RFOLx+61ABaikYtXLtzrO4 PnkqV6QIDtiANe/4/NM81foi3nV6F42E8Ixn8w1R0qDqX9y7TNqe9JOc0+aq6jgE8NDD 12C+NCQenK4Qr26wmOB2ga/e1JmecS/l4WE7pmBC0SnkIPbLFIStYTogP6hBOBu1fe/q fyAhuKIHO7X3GBJwK567LdVLktJmbXShmpVTwuEP9zWr+0CVBsYNceLdCyyKGp7wGN3Q 7DC25WlZ09uM4ZUYqY6ST15pDnRTGVebFedjF6U2fsC36w1PbcrEezFZZDbV0YwKdn9I 8Rxg== 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 s18-20020a056a00195200b0056238741ba7si567566pfk.174.2022.11.24.00.57.33; Thu, 24 Nov 2022 00:57:45 -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; 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 S229814AbiKXIxN (ORCPT + 99 others); Thu, 24 Nov 2022 03:53:13 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:55162 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229790AbiKXIxL (ORCPT ); Thu, 24 Nov 2022 03:53:11 -0500 Received: from out0-147.mail.aliyun.com (out0-147.mail.aliyun.com [140.205.0.147]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 1493885A35 for ; Thu, 24 Nov 2022 00:53:08 -0800 (PST) X-Alimail-AntiSpam: AC=PASS;BC=-1|-1;BR=01201311R101e4;CH=green;DM=||false|;DS=||;FP=0|-1|-1|-1|0|-1|-1|-1;HT=ay29a033018047203;MF=houwenlong.hwl@antgroup.com;NM=1;PH=DS;RN=18;SR=0;TI=SMTPD_---.QFkkAyF_1669279984; Received: from localhost(mailfrom:houwenlong.hwl@antgroup.com fp:SMTPD_---.QFkkAyF_1669279984) by smtp.aliyun-inc.com; Thu, 24 Nov 2022 16:53:05 +0800 From: "Hou Wenlong" To: linux-kernel@vger.kernel.org Cc: "Hou Wenlong" , "Juergen Gross" , " =?utf-8?q?Srivatsa_S=2E_Bhat_=28VMware?= =?utf-8?q?=29?= " , "Alexey Makhalov" , "VMware PV-Drivers Reviewers" , "Thomas Gleixner" , "Ingo Molnar" , "Borislav Petkov" , "Dave Hansen" , , "H. Peter Anvin" , "Peter Zijlstra" , "Josh Poimboeuf" , "Kees Cook" , "Song Liu" , "Nadav Amit" , Subject: [PATCH] x86/paravirt: Use relative reference for original instruction Date: Thu, 24 Nov 2022 16:51:20 +0800 Message-Id: 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,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?1750367223535384236?= X-GMAIL-MSGID: =?utf-8?q?1750367223535384236?= 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. Signed-off-by: Hou Wenlong --- arch/x86/include/asm/paravirt.h | 6 +++--- arch/x86/include/asm/paravirt_types.h | 4 ++-- arch/x86/kernel/alternative.c | 8 +++++--- 3 files changed, 10 insertions(+), 8 deletions(-) diff --git a/arch/x86/include/asm/paravirt.h b/arch/x86/include/asm/paravirt.h index 2851bc2339d5..2cbe9b64e103 100644 --- a/arch/x86/include/asm/paravirt.h +++ b/arch/x86/include/asm/paravirt.h @@ -735,13 +735,13 @@ extern void default_banner(void); #else /* __ASSEMBLY__ */ -#define _PVSITE(ptype, ops, word, algn) \ +#define _PVSITE(ptype, ops, algn) \ 771:; \ ops; \ 772:; \ .pushsection .parainstructions,"a"; \ .align algn; \ - word 771b; \ + .long 771b-.; \ .byte ptype; \ .byte 772b-771b; \ _ASM_ALIGN; \ @@ -752,7 +752,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, 8) #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 8c1da419260f..19f709cf7df9 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 */ }; @@ -274,7 +274,7 @@ extern struct paravirt_patch_template pv_ops; "771:\n\t" insn_string "\n" "772:\n" \ ".pushsection .parainstructions,\"a\"\n" \ _ASM_ALIGN "\n" \ - _ASM_PTR " 771b\n" \ + " .long 771b-.\n" \ " .byte " type "\n" \ " .byte 772b-771b\n" \ _ASM_ALIGN "\n" \ diff --git a/arch/x86/kernel/alternative.c b/arch/x86/kernel/alternative.c index 111b809f0ac2..6eea563a098d 100644 --- a/arch/x86/kernel/alternative.c +++ b/arch/x86/kernel/alternative.c @@ -1232,20 +1232,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[],