Received: by 2002:ac0:a594:0:0:0:0:0 with SMTP id m20-v6csp3858761imm; Thu, 17 May 2018 16:29:31 -0700 (PDT) X-Google-Smtp-Source: AB8JxZodOtZf1mo11zyrahzGfbsStjVxwdaFG8sRRyo8W0PR4HwuE+OXGoZrvmA6aWu7o1qW0Iml X-Received: by 2002:a17:902:968d:: with SMTP id n13-v6mr6885225plp.168.1526599771753; Thu, 17 May 2018 16:29:31 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1526599771; cv=none; d=google.com; s=arc-20160816; b=n98aXmv1RKqKpa/Xrrf1hF2GxQk6L6EAk0SwrtYU+LNVJjIBqa50r8kTDIfAxFRWET pOjXsV5de+C3MPHQKi2/DaV3ymcBGbWgJg1ESYc2PAcWqJaTbxWV3+575HZCs530m7gS kHjUsK5rYYkGT7VGxAsI4jJ8l15WYURvcXXXO7/+he21VzDSzxZ1pQ40pwrOib7oitoP qUto/AD8wb2Uap4lCeMWFQEklRIQpvkQVGglodwXXTiAqOiGO9q/p1dI0kLco5XIPPwI +D/n24r1k9hMFmhcHFycMvazY0yVjsbD+5j4+2fqgcGeB3N3+Xh5rn4GrlvWjUmb22MJ 9agw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:arc-authentication-results; bh=IQZAxxAAvqYxBj+3ovRcbMVt/o1T/UPGaLL5iGSOYxc=; b=JPid/z9bbapsuXogBWNvDfh30KTj1cGkRtkrhUs5Ruc4ZodGXp92zD8sCtRmESL1di J51OTk1g5sT7d1NgvcLVDYtViMOxs9D+0VFS2GRSxLHjqi43rkMBSbmTmDiVYjz/RzDX UIAjEBMZ9SUtsg8G7qFOepIUBPPPVlA4bNuifrN6D0hzimGxMEs9lgC+n2p59t2l+9ka 488Z0m3XgrHqTlmNYgUyWLnERcGkhtebOfXmoxlX612R8ynvJeRfdc+nIzzepqFlkO6L c8z+o2PuhFt/qYlJGgQ4DbGQX6Wa0GmBz0fwh69gZUkk5k6r43WI36IzentRyH35JxNc trzQ== 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 v8-v6si6003823plo.306.2018.05.17.16.29.17; Thu, 17 May 2018 16:29:31 -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 S1752257AbeEQX2q (ORCPT + 99 others); Thu, 17 May 2018 19:28:46 -0400 Received: from ex13-edg-ou-002.vmware.com ([208.91.0.190]:57881 "EHLO EX13-EDG-OU-002.vmware.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751543AbeEQX2l (ORCPT ); Thu, 17 May 2018 19:28:41 -0400 Received: from sc9-mailhost3.vmware.com (10.113.161.73) by EX13-EDG-OU-002.vmware.com (10.113.208.156) with Microsoft SMTP Server id 15.0.1156.6; Thu, 17 May 2018 16:28:39 -0700 Received: from sc2-haas01-esx0118.eng.vmware.com (sc2-haas01-esx0118.eng.vmware.com [10.172.44.118]) by sc9-mailhost3.vmware.com (Postfix) with ESMTP id 65D214075D; Thu, 17 May 2018 16:28:40 -0700 (PDT) From: Nadav Amit To: , CC: , Nadav Amit , Juergen Gross , Alok Kataria , Thomas Gleixner , Ingo Molnar , "H. Peter Anvin" , Subject: [PATCH 4/6] x86: prevent inline distortion by paravirt ops Date: Thu, 17 May 2018 09:14:00 -0700 Message-ID: <20180517161402.78089-5-namit@vmware.com> X-Mailer: git-send-email 2.17.0 In-Reply-To: <20180517161402.78089-1-namit@vmware.com> References: <20180517161402.78089-1-namit@vmware.com> MIME-Version: 1.0 Content-Type: text/plain Received-SPF: None (EX13-EDG-OU-002.vmware.com: namit@vmware.com does not designate permitted sender hosts) Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org GCC considers the number of statements in inlined assembly blocks, according to new-lines and semicolons, as an indication to the cost of the block in time and space. This data is distorted by the kernel code, which puts information in alternative sections. As a result, the compiler may perform incorrect inlining and branch optimizations. The solution is to set an assembly macro and call it from the inlined assembly block. As a result GCC considers the inline assembly block as a single instruction. The effect of the patch is a more aggressive inlining, which also causes a size increase of kernel. text data bss dec hex filename 18131468 10068488 2936832 31136788 1db1c14 ./vmlinux before 18146418 10064100 2936832 31147350 1db4556 ./vmlinux after (+10562) Static text symbols: Before: 39788 After: 39673 (-115) Cc: Juergen Gross Cc: Alok Kataria Cc: Thomas Gleixner Cc: Ingo Molnar Cc: "H. Peter Anvin" Cc: x86@kernel.org Cc: virtualization@lists.linux-foundation.org Signed-off-by: Nadav Amit --- arch/x86/include/asm/paravirt_types.h | 63 +++++++++++++++++---------- 1 file changed, 39 insertions(+), 24 deletions(-) diff --git a/arch/x86/include/asm/paravirt_types.h b/arch/x86/include/asm/paravirt_types.h index 180bc0bff0fb..ea62204c2ee6 100644 --- a/arch/x86/include/asm/paravirt_types.h +++ b/arch/x86/include/asm/paravirt_types.h @@ -346,20 +346,45 @@ extern struct pv_lock_ops pv_lock_ops; /* * Generate some code, and mark it as patchable by the * apply_paravirt() alternate instruction patcher. + * + * This generates an indirect call based on the operation type number. + * The type number, computed in PARAVIRT_PATCH, is derived from the + * offset into the paravirt_patch_template structure, and can therefore be + * freely converted back into a structure offset. + * + * The paravirtual alternative logic and data are encapsulated within an + * assembly macro, which is then called on each use. This hack is necessary to + * prevent GCC from considering the inline assembly blocks as costly in time and + * space, which can prevent function inlining and lead to other bad compilation + * decisions. GCC computes inline assembly cost according to the number of + * perceived number of assembly instruction, based on the number of new-lines + * and semicolons in the assembly block. The macro will eventually be compiled + * into a single instruction (and some data). This scheme allows GCC to better + * understand the inline asm cost. */ -#define _paravirt_alt(insn_string, type, clobber) \ - "771:\n\t" insn_string "\n" "772:\n" \ - ".pushsection .parainstructions,\"a\"\n" \ - _ASM_ALIGN "\n" \ - _ASM_PTR " 771b\n" \ - " .byte " type "\n" \ - " .byte 772b-771b\n" \ - " .short " clobber "\n" \ - ".popsection\n" +asm(".macro __paravirt_alt type:req clobber:req pv_opptr:req\n" + "771:\n\t" + ANNOTATE_RETPOLINE_SAFE "\n\t" + "call *\\pv_opptr\n" + "772:\n\t" + ".pushsection .parainstructions,\"a\"\n\t" + _ASM_ALIGN "\n\t" + _ASM_PTR " 771b\n\t" + ".byte \\type\n\t" + ".byte 772b-771b\n\t" + ".short \\clobber\n\t" + ".popsection\n\t" + ".endm"); + +#define _paravirt_alt(type, clobber, pv_opptr) \ + "__paravirt_alt type=" __stringify(type) \ + " clobber=" __stringify(clobber) \ + " pv_opptr=" __stringify(pv_opptr) "\n\t" /* Generate patchable code, with the default asm parameters. */ -#define paravirt_alt(insn_string) \ - _paravirt_alt(insn_string, "%c[paravirt_typenum]", "%c[paravirt_clobber]") +#define paravirt_alt \ + _paravirt_alt("%c[paravirt_typenum]", "%c[paravirt_clobber]", \ + "%c[paravirt_opptr]") /* Simple instruction patching code. */ #define NATIVE_LABEL(a,x,b) "\n\t.globl " a #x "_" #b "\n" a #x "_" #b ":\n\t" @@ -387,16 +412,6 @@ unsigned native_patch(u8 type, u16 clobbers, void *ibuf, int paravirt_disable_iospace(void); -/* - * This generates an indirect call based on the operation type number. - * The type number, computed in PARAVIRT_PATCH, is derived from the - * offset into the paravirt_patch_template structure, and can therefore be - * freely converted back into a structure offset. - */ -#define PARAVIRT_CALL \ - ANNOTATE_RETPOLINE_SAFE \ - "call *%c[paravirt_opptr];" - /* * These macros are intended to wrap calls through one of the paravirt * ops structs, so that they can be later identified and patched at @@ -534,7 +549,7 @@ int paravirt_disable_iospace(void); /* since this condition will never hold */ \ if (sizeof(rettype) > sizeof(unsigned long)) { \ asm volatile(pre \ - paravirt_alt(PARAVIRT_CALL) \ + paravirt_alt \ post \ : call_clbr, ASM_CALL_CONSTRAINT \ : paravirt_type(op), \ @@ -544,7 +559,7 @@ int paravirt_disable_iospace(void); __ret = (rettype)((((u64)__edx) << 32) | __eax); \ } else { \ asm volatile(pre \ - paravirt_alt(PARAVIRT_CALL) \ + paravirt_alt \ post \ : call_clbr, ASM_CALL_CONSTRAINT \ : paravirt_type(op), \ @@ -571,7 +586,7 @@ int paravirt_disable_iospace(void); PVOP_VCALL_ARGS; \ PVOP_TEST_NULL(op); \ asm volatile(pre \ - paravirt_alt(PARAVIRT_CALL) \ + paravirt_alt \ post \ : call_clbr, ASM_CALL_CONSTRAINT \ : paravirt_type(op), \ -- 2.17.0