All of lore.kernel.org
 help / color / mirror / Atom feed
From: Andi Kleen <andi@firstfloor.org>
To: x86@kernel.org
Cc: linux-kernel@vger.kernel.org, Andi Kleen <ak@linux.intel.com>,
	Thomas Gleixner <tglx@linutronix.de>
Subject: [PATCH v2 3/9] x86/paravirt: Replace paravirt patches with data
Date: Fri, 29 Mar 2019 17:47:37 -0700	[thread overview]
Message-ID: <20190330004743.29541-3-andi@firstfloor.org> (raw)
In-Reply-To: <20190330004743.29541-1-andi@firstfloor.org>

From: Andi Kleen <ak@linux.intel.com>

For LTO all top level assembler statements need to be global because
LTO might put it into a different assembler file than the referencing
C code.

To avoid making all the paravirt patch snippets global replace them
with data containing the patch instructions. Since these are unlikely
to change this shouldn't be a significant maintenance burden.

Suggested-by: Thomas Gleixner <tglx@linutronix.de>
Signed-off-by: Andi Kleen <ak@linux.intel.com>
---
 arch/x86/include/asm/paravirt_types.h |  6 +---
 arch/x86/kernel/paravirt_patch_32.c   | 33 +++++++++++----------
 arch/x86/kernel/paravirt_patch_64.c   | 42 +++++++++++++++------------
 3 files changed, 42 insertions(+), 39 deletions(-)

diff --git a/arch/x86/include/asm/paravirt_types.h b/arch/x86/include/asm/paravirt_types.h
index 2474e434a6f7..bb13e79d4344 100644
--- a/arch/x86/include/asm/paravirt_types.h
+++ b/arch/x86/include/asm/paravirt_types.h
@@ -367,12 +367,8 @@ extern struct paravirt_patch_template pv_ops;
 #define paravirt_alt(insn_string)					\
 	_paravirt_alt(insn_string, "%c[paravirt_typenum]", "%c[paravirt_clobber]")
 
-/* Simple instruction patching code. */
-#define NATIVE_LABEL(a,x,b) "\n\t.globl " a #x "_" #b "\n" a #x "_" #b ":\n\t"
-
 #define DEF_NATIVE(ops, name, code)					\
-	__visible extern const char start_##ops##_##name[], end_##ops##_##name[];	\
-	asm(NATIVE_LABEL("start_", ops, name) code NATIVE_LABEL("end_", ops, name))
+	const char start_##ops##_##name[] = code
 
 unsigned paravirt_patch_ident_64(void *insnbuf, unsigned len);
 unsigned paravirt_patch_default(u8 type, void *insnbuf,
diff --git a/arch/x86/kernel/paravirt_patch_32.c b/arch/x86/kernel/paravirt_patch_32.c
index de138d3912e4..9a649026d74c 100644
--- a/arch/x86/kernel/paravirt_patch_32.c
+++ b/arch/x86/kernel/paravirt_patch_32.c
@@ -2,14 +2,14 @@
 #include <asm/paravirt.h>
 
 #ifdef CONFIG_PARAVIRT_XXL
-DEF_NATIVE(irq, irq_disable, "cli");
-DEF_NATIVE(irq, irq_enable, "sti");
-DEF_NATIVE(irq, restore_fl, "push %eax; popf");
-DEF_NATIVE(irq, save_fl, "pushf; pop %eax");
-DEF_NATIVE(cpu, iret, "iret");
-DEF_NATIVE(mmu, read_cr2, "mov %cr2, %eax");
-DEF_NATIVE(mmu, write_cr3, "mov %eax, %cr3");
-DEF_NATIVE(mmu, read_cr3, "mov %cr3, %eax");
+static const unsigned char patch_irq_irq_disable[] = { 0xfa }; 	  	/* cli */
+static const unsigned char patch_irq_irq_enable[] = { 0xfb };  	  	/* sti */
+static const unsigned char patch_irq_restore_fl[] =  { 0x50, 0x9d }; 	/* push %eax; popf */
+static const unsigned char patch_irq_save_fl[] = { 0x9c, 0x58 };  	/* pushf; pop %eax */
+static const unsigned char patch_cpu_iret[] = { 0xcf };		  	/* iret */
+static const unsigned char patch_mmu_read_cr2[] = { 0x0f, 0x20, 0xd0 }; /* mov %cr2, %eax */
+static const unsigned char patch_mmu_write_cr3[] = { 0x0f, 0x22, 0xd8 };/* mov %eax, %cr3 */
+static const unsigned char patch_mmu_read_cr3[] = { 0x0f, 0x20, 0xd8 }; /* mov %cr3, %eax */
 
 unsigned paravirt_patch_ident_64(void *insnbuf, unsigned len)
 {
@@ -19,8 +19,8 @@ unsigned paravirt_patch_ident_64(void *insnbuf, unsigned len)
 #endif
 
 #if defined(CONFIG_PARAVIRT_SPINLOCKS)
-DEF_NATIVE(lock, queued_spin_unlock, "movb $0, (%eax)");
-DEF_NATIVE(lock, vcpu_is_preempted, "xor %eax, %eax");
+static const unsigned char patch_lock_queued_spin_unlock[] = { 0xc6, 0x00, 0x00 }; /* movb $0, (%eax) */
+static const unsigned char patch_lock_vcpu_is_preempted[] =  { 0x31, 0xc0 }; 	 /* xor %eax, %eax */
 #endif
 
 extern bool pv_is_native_spin_unlock(void);
@@ -30,7 +30,8 @@ unsigned native_patch(u8 type, void *ibuf, unsigned long addr, unsigned len)
 {
 #define PATCH_SITE(ops, x)					\
 	case PARAVIRT_PATCH(ops.x):				\
-		return paravirt_patch_insns(ibuf, len, start_##ops##_##x, end_##ops##_##x)
+		return paravirt_patch_insns(ibuf, len, 		\
+				patch_##ops##_##x, patch_##ops##_##x+sizeof(patch_##ops##_x));
 
 	switch (type) {
 #ifdef CONFIG_PARAVIRT_XXL
@@ -47,15 +48,17 @@ unsigned native_patch(u8 type, void *ibuf, unsigned long addr, unsigned len)
 	case PARAVIRT_PATCH(lock.queued_spin_unlock):
 		if (pv_is_native_spin_unlock())
 			return paravirt_patch_insns(ibuf, len,
-						    start_lock_queued_spin_unlock,
-						    end_lock_queued_spin_unlock);
+						    patch_lock_queued_spin_unlock,
+						    patch_lock_queued_spin_unlock +
+						    sizeof(patch_lock_queued_spin_unlock));
 		break;
 
 	case PARAVIRT_PATCH(lock.vcpu_is_preempted):
 		if (pv_is_native_vcpu_is_preempted())
 			return paravirt_patch_insns(ibuf, len,
-						    start_lock_vcpu_is_preempted,
-						    end_lock_vcpu_is_preempted);
+						    patch_lock_vcpu_is_preempted,
+						    patch_lock_vcpu_is_preempted +
+						    sizeof(patch_lock_vcpu_is_preempted));
 		break;
 #endif
 
diff --git a/arch/x86/kernel/paravirt_patch_64.c b/arch/x86/kernel/paravirt_patch_64.c
index 9d9e04b31077..fce6f54665d3 100644
--- a/arch/x86/kernel/paravirt_patch_64.c
+++ b/arch/x86/kernel/paravirt_patch_64.c
@@ -4,29 +4,30 @@
 #include <linux/stringify.h>
 
 #ifdef CONFIG_PARAVIRT_XXL
-DEF_NATIVE(irq, irq_disable, "cli");
-DEF_NATIVE(irq, irq_enable, "sti");
-DEF_NATIVE(irq, restore_fl, "pushq %rdi; popfq");
-DEF_NATIVE(irq, save_fl, "pushfq; popq %rax");
-DEF_NATIVE(mmu, read_cr2, "movq %cr2, %rax");
-DEF_NATIVE(mmu, read_cr3, "movq %cr3, %rax");
-DEF_NATIVE(mmu, write_cr3, "movq %rdi, %cr3");
-DEF_NATIVE(cpu, wbinvd, "wbinvd");
+static const unsigned char patch_irq_irq_disable[] = { 0xfa }; 			/* cli */
+static const unsigned char patch_irq_irq_enable[] =  { 0xfb }; 			/* sti */
+static const unsigned char patch_irq_restore_fl[] =  { 0x50, 0x9d};		/* pushq %rdi; popfq */
+static const unsigned char patch_irq_save_fl[] =     { 0x9c, 0x58 };		/* pushfq; popq %rax */
+static const unsigned char patch_mmu_read_cr2[] =    { 0x0f, 0x20, 0xd0 };	/* movq %cr2, %rax */
+static const unsigned char patch_mmu_read_cr3[] =    { 0x0f, 0x22, 0xd8 };	/* movq %cr3, %rax */
+static const unsigned char patch_mmu_write_cr3[] =   { 0x0f, 0x22, 0xdf };	/* movq %rdi, %cr3 */
+static const unsigned char patch_cpu_wbinvd[] =      { 0x0f, 0x09 };		/* wbinvd */
 
-DEF_NATIVE(cpu, usergs_sysret64, "swapgs; sysretq");
-DEF_NATIVE(cpu, swapgs, "swapgs");
-DEF_NATIVE(, mov64, "mov %rdi, %rax");
+static const unsigned char patch_cpu_usergs_sysret64[] = { 0x0f, 0x01, 0xf8, 0x48, 0x0f, 0x07 };
+										/* swapgs; sysretq */
+static const unsigned char patch_cpu_swapgs[] = { 0x0f, 0x01, 0xf8 };		/* swapgs */
+static const unsigned char patch_mov64[] = { 0x48, 0x89, 0xf8 };		/* mov %rdi, %rax */
 
 unsigned paravirt_patch_ident_64(void *insnbuf, unsigned len)
 {
 	return paravirt_patch_insns(insnbuf, len,
-				    start__mov64, end__mov64);
+				    start_mov64, start_mov64 + sizeof(start_mov64));
 }
 #endif
 
 #if defined(CONFIG_PARAVIRT_SPINLOCKS)
-DEF_NATIVE(lock, queued_spin_unlock, "movb $0, (%rdi)");
-DEF_NATIVE(lock, vcpu_is_preempted, "xor %eax, %eax");
+static const unsigned char patch_lock_queued_spin_unlock[] = { 0xc6, 0x07, 0x00}; /* movb $0, (%rdi) */
+static const unsigned char patch_lock_vcpu_is_preempted[] = { 0x31, 0xc0 };	  /* xor %eax, %eax */
 #endif
 
 extern bool pv_is_native_spin_unlock(void);
@@ -36,7 +37,8 @@ unsigned native_patch(u8 type, void *ibuf, unsigned long addr, unsigned len)
 {
 #define PATCH_SITE(ops, x)					\
 	case PARAVIRT_PATCH(ops.x):				\
-		return paravirt_patch_insns(ibuf, len, start_##ops##_##x, end_##ops##_##x)
+		return paravirt_patch_insns(ibuf, len, start_##ops##_##x, \
+				patch_##ops##_##x + sizeof(patch_##ops##_##x));
 
 	switch (type) {
 #ifdef CONFIG_PARAVIRT_XXL
@@ -55,15 +57,17 @@ unsigned native_patch(u8 type, void *ibuf, unsigned long addr, unsigned len)
 	case PARAVIRT_PATCH(lock.queued_spin_unlock):
 		if (pv_is_native_spin_unlock())
 			return paravirt_patch_insns(ibuf, len,
-						    start_lock_queued_spin_unlock,
-						    end_lock_queued_spin_unlock);
+						    patch_lock_queued_spin_unlock,
+						    patch_lock_queued_spin_unlock +
+						    sizeof(patch_lock_queued_spin_unlock));
 		break;
 
 	case PARAVIRT_PATCH(lock.vcpu_is_preempted):
 		if (pv_is_native_vcpu_is_preempted())
 			return paravirt_patch_insns(ibuf, len,
-						    start_lock_vcpu_is_preempted,
-						    end_lock_vcpu_is_preempted);
+						    patch_lock_vcpu_is_preempted,
+						    patch_lock_vcpu_is_preempted +
+						    sizeof(patch_lock_vcpu_is_preempted));
 		break;
 #endif
 
-- 
2.20.1


  parent reply	other threads:[~2019-03-30  0:48 UTC|newest]

Thread overview: 20+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-03-30  0:47 [PATCH v2 1/9] x86/asm: Mark all top level asm statements as .text Andi Kleen
2019-03-30  0:47 ` [PATCH v2 2/9] x86/cpu/amd: Ifdef 32bit only assembler for 32bit Andi Kleen
2019-04-19 15:52   ` [tip:x86/asm] x86/cpu/amd: Exclude 32bit only assembler from 64bit build tip-bot for Andi Kleen
2019-03-30  0:47 ` Andi Kleen [this message]
2019-04-19 21:26   ` [PATCH v2 3/9] x86/paravirt: Replace paravirt patches with data Thomas Gleixner
2019-03-30  0:47 ` [PATCH v2 4/9] x86/timer: Don't inline __const_udelay Andi Kleen
2019-04-19 15:55   ` [tip:x86/timers] x86/timer: Don't inline __const_udelay() tip-bot for Andi Kleen
2019-03-30  0:47 ` [PATCH v2 5/9] x86/xen: Mark xen_vcpu_stolen as __visible Andi Kleen
2019-03-30 10:40   ` Juergen Gross
2019-03-30  0:47 ` [PATCH v2 6/9] x86/hyperv: Make hv_vcpu_is_preempted visible Andi Kleen
2019-04-19 15:58   ` [tip:x86/hyperv] x86/hyperv: Make hv_vcpu_is_preempted() visible tip-bot for Andi Kleen
2019-04-19 16:04   ` [tip:x86/platform] " tip-bot for Andi Kleen
2019-03-30  0:47 ` [PATCH v2 7/9] x86/kprobes: Make trampoline_handler global and visible Andi Kleen
2019-04-19 15:58   ` [tip:x86/core] x86/kprobes: Make trampoline_handler() " tip-bot for Andi Kleen
2019-05-08 11:20   ` [tip:x86/urgent] " tip-bot for Andi Kleen
2019-03-30  0:47 ` [PATCH v2 8/9] x86/kvm: Make steal_time visible Andi Kleen
2019-04-19 16:04   ` [tip:x86/platform] " tip-bot for Andi Kleen
2019-03-30  0:47 ` [PATCH v2 9/9] x86/cpu/bugs: Fix __initconst usage in bugs.c Andi Kleen
2019-04-19 15:16   ` [tip:x86/urgent] x86/cpu/bugs: Use __initconst for 'const' init data tip-bot for Andi Kleen
2019-04-19 15:52 ` [tip:x86/asm] x86/asm: Mark all top level asm statements as .text tip-bot for Andi Kleen

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20190330004743.29541-3-andi@firstfloor.org \
    --to=andi@firstfloor.org \
    --cc=ak@linux.intel.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=tglx@linutronix.de \
    --cc=x86@kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.