Skip to contentSkip to conversation

Prevent inlining for asm blocks with labels

PATCH In review Bill Wendling 2 participants 18 messages Aug 25, 2021 – Sep 9, 2021

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

cover Prevent inlining for asm blocks with labels

Clang may decide to inline some functions that have inline asm with labels. Doing this duplicates the labels, causing the assembler to be complain. These patches add the "noinline" attribute to the functions to prevent this.

Bill Wendling (4):
x86: realmode: mark exec_in_big_real_mode as noinline
x86: svm: mark test_run as noinline
x86: umip: mark do_ring3 as noinline
x86: vmx: mark some test_* functions as noinline

 x86/realmode.c | 2 +-
 x86/svm.c      | 2 +-
 x86/umip.c     | 2 +-
 x86/vmx.c      | 6 +++---
 4 files changed, 6 insertions(+), 6 deletions(-)
Signature · 1 line
2.33.0.rc2.250.ged5fa647cd-goog
#1 lore raw

PATCH 1/4 x86: realmode: mark exec_in_big_real_mode as noinline

exec_in_big_real_mode() uses inline asm that has labels. Clang decides that it can inline this function, which causes the assembler to complain about duplicate symbols. Mark the function as "noinline" to prevent this.

---
 x86/realmode.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
x86/realmode.c +1 -1
index b4fa603..56bd238 100644
@@ -178,7 +178,7 @@ static inline void init_inregs(struct regs *regs)
 		inregs.esp = (unsigned long)&tmp_stack.top;
 }
 
-static void exec_in_big_real_mode(struct insn_desc *insn)
+static void __attribute__((noinline)) exec_in_big_real_mode(struct insn_desc *insn)
 {
 	unsigned long tmp;
 	static struct regs save;
Signature · 1 line
2.33.0.rc2.250.ged5fa647cd-goog
#2 lore raw

PATCH 2/4 x86: svm: mark test_run as noinline

In reply to Bill Wendling

test_run() uses inline asm that has labels. Clang decides that it can inline this function, which causes the assembler to complain about duplicate symbols. Mark the function as "noinline" to prevent this.

---
 x86/svm.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
x86/svm.c +1 -1
index beb40b7..68fd29b 100644
@@ -256,7 +256,7 @@ int svm_vmrun(void)
 
 extern u8 vmrun_rip;
 
-static void test_run(struct svm_test *test)
+static void __attribute__((noinline)) test_run(struct svm_test *test)
 {
 	u64 vmcb_phys = virt_to_phys(vmcb);
 
Signature · 1 line
2.33.0.rc2.250.ged5fa647cd-goog
#3 lore raw

PATCH 3/4 x86: umip: mark do_ring3 as noinline

In reply to Bill Wendling

do_ring3() uses inline asm that has labels. Clang decides that it can inline this function, which causes the assembler to complain about duplicate symbols. Mark the function as "noinline" to prevent this.

---
 x86/umip.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
x86/umip.c +1 -1
index c5700b3..5ae6f44 100644
@@ -114,7 +114,7 @@ static void test_umip_gp(const char *msg)
 
 /* The ugly mode switching code */
 
-static int do_ring3(void (*fn)(const char *), const char *arg)
+static int __attribute__((noinline)) do_ring3(void (*fn)(const char *), const char *arg)
 {
     static unsigned char user_stack[4096];
     int ret;
Signature · 1 line
2.33.0.rc2.250.ged5fa647cd-goog
#4 lore raw

PATCH 4/4 x86: vmx: mark some test_* functions as noinline

In reply to Bill Wendling

Some test_* functions use inline asm that has labels. Clang decides that it can inline these functions, which causes the assembler to complain about duplicate symbols. Mark the functions as "noinline" to prevent this.

---
 x86/vmx.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
x86/vmx.c +3 -3
index f0b853a..a264136 100644
@@ -426,7 +426,7 @@ static void prep_flags_test_env(void **vpage, struct vmcs **vmcs, handler *old)
 	*old = handle_exception(PF_VECTOR, &pf_handler);
 }
 
-static void test_read_sentinel(void)
+static void __attribute__((noinline)) test_read_sentinel(void)
 {
 	void *vpage;
 	struct vmcs *vmcs;
@@ -474,7 +474,7 @@ static void test_vmread_flags_touch(void)
 	test_read_sentinel();
 }
 
-static void test_write_sentinel(void)
+static void __attribute__((noinline)) test_write_sentinel(void)
 {
 	void *vpage;
 	struct vmcs *vmcs;
@@ -1786,7 +1786,7 @@ static int exit_handler(union exit_reason exit_reason)
  * Tries to enter the guest, populates @result with VM-Fail, VM-Exit, entered,
  * etc...
  */
-static void vmx_enter_guest(struct vmentry_result *result)
+static void __attribute__((noinline)) vmx_enter_guest(struct vmentry_result *result)
 {
 	memset(result, 0, sizeof(*result));
 
Signature · 1 line
2.33.0.rc2.250.ged5fa647cd-goog
#5 lore raw
In reply to Bill Wendling
Quoted 1 line · Bill Wendling

Clang may decide to inline some functions that have inline asm with labels.

For all changlogs, it's probably worth clarifying that they have _global_ labels, e.g. local labels within an asm block are perfectly ok for inlining, as are local labels in the function (but outside of the block) used by asm goto.

And maybe add a "#define noinline ..." to match the kernel and convert the two existing uses in pmu_lbr.c as a prep patch?

Quoted 18 lines

Doing this duplicates the labels, causing the assembler to be complain. These patches add the "noinline" attribute to the functions to prevent this.

Bill Wendling (4):
x86: realmode: mark exec_in_big_real_mode as noinline
x86: svm: mark test_run as noinline
x86: umip: mark do_ring3 as noinline
x86: vmx: mark some test_* functions as noinline

 x86/realmode.c | 2 +-
 x86/svm.c      | 2 +-
 x86/umip.c     | 2 +-
 x86/vmx.c      | 6 +++---
 4 files changed, 6 insertions(+), 6 deletions(-)
Signature · 1 line
2.33.0.rc2.250.ged5fa647cd-goog
#6 lore raw
Quoted 11 lines · Sean Christopherson
Quoted 1 line · Bill Wendling

Clang may decide to inline some functions that have inline asm with labels.

For all changlogs, it's probably worth clarifying that they have _global_ labels, e.g. local labels within an asm block are perfectly ok for inlining, as are local labels in the function (but outside of the block) used by asm goto.

And maybe add a "#define noinline ..." to match the kernel and convert the two existing uses in pmu_lbr.c as a prep patch?

I sent out a patch for this. Thanks!

As for the changelogs, I'll send an updated patch series once the noinline patch is resolved.

-bw

Quoted 18 lines
Quoted 18 lines

Doing this duplicates the labels, causing the assembler to be complain. These patches add the "noinline" attribute to the functions to prevent this.

Bill Wendling (4):
x86: realmode: mark exec_in_big_real_mode as noinline
x86: svm: mark test_run as noinline
x86: umip: mark do_ring3 as noinline
x86: vmx: mark some test_* functions as noinline

 x86/realmode.c | 2 +-
 x86/svm.c      | 2 +-
 x86/umip.c     | 2 +-
 x86/vmx.c      | 6 +++---
 4 files changed, 6 insertions(+), 6 deletions(-)

--
2.33.0.rc2.250.ged5fa647cd-goog

#7 lore raw

v2 cover Prevent inlining for asm blocks with labels

In reply to Bill Wendling

Clang may decide to inline some functions that have inline asm with labels. Doing this duplicates the labels, causing the assembler to be complain. These patches add the "noinline" attribute to the functions to prevent this.

v2: Combine "libcflat.h" change and use the new "noinline" macro.

Bill Wendling (5):
libcflag: define the "noinline" macro
x86: realmode: mark exec_in_big_real_mode as noinline
x86: svm: mark test_run as noinline
x86: umip: mark do_ring3 as noinline
x86: vmx: mark some test_* functions as noinline

 lib/libcflat.h | 1 +
 x86/pmu_lbr.c  | 4 ++--
 x86/realmode.c | 2 +-
 x86/svm.c      | 2 +-
 x86/umip.c     | 2 +-
 x86/vmx.c      | 6 +++---
 6 files changed, 9 insertions(+), 8 deletions(-)
Signature · 1 line
2.33.0.309.g3052b89438-goog
#8 lore raw

PATCH v2 1/5 libcflag: define the "noinline" macro

Define "noline" macro to reduce the amount of typing for functions using the "noinline" attribute.

---
v2: Combine separate change with this series.

---
 lib/libcflat.h | 1 +
 x86/pmu_lbr.c  | 4 ++--
 2 files changed, 3 insertions(+), 2 deletions(-)
lib/libcflat.h +1 -0
index 97db9e3..a652c76 100644
@@ -29,6 +29,7 @@
 #include <stdbool.h>
 
 #define __unused __attribute__((__unused__))
+#define noinline __attribute__((noinline))
 
 #define xstr(s...) xxstr(s)
 #define xxstr(s...) #s
x86/pmu_lbr.c +2 -2
index 3bd9e9f..5ff805a 100644
@@ -16,14 +16,14 @@
 
 volatile int count;
 
-static __attribute__((noinline)) int compute_flag(int i)
+static noinline int compute_flag(int i)
 {
 	if (i % 10 < 4)
 		return i + 1;
 	return 0;
 }
 
-static __attribute__((noinline)) int lbr_test(void)
+static noinline int lbr_test(void)
 {
 	int i;
 	int flag;
Signature · 1 line
2.33.0.309.g3052b89438-goog
#10 lore raw

PATCH v2 2/5 x86: realmode: mark exec_in_big_real_mode as noinline

In reply to Bill Wendling

exec_in_big_real_mode() uses inline asm that has labels. Clang decides that it can inline this function, which causes the assembler to complain about duplicate symbols. Mark the function as "noinline" to prevent this.

---
 x86/realmode.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
x86/realmode.c +1 -1
index b4fa603..07a477f 100644
@@ -178,7 +178,7 @@ static inline void init_inregs(struct regs *regs)
 		inregs.esp = (unsigned long)&tmp_stack.top;
 }
 
-static void exec_in_big_real_mode(struct insn_desc *insn)
+static __attribute__((noinline)) void exec_in_big_real_mode(struct insn_desc *insn)
 {
 	unsigned long tmp;
 	static struct regs save;
Signature · 1 line
2.33.0.309.g3052b89438-goog
#11 lore raw

Re: [kvm-unit-tests PATCH v2 2/5] x86: realmode: mark exec_in_big_real_mode as noinline

Quoted 1 line · Bill Wendling

exec_in_big_real_mode() uses inline asm that has labels. Clang decides

_global_ labels. Inlining functions with local labels, including asm goto labels, is not problematic, the issue is specific to labels that must be unique for a given compilation unit.

Quoted 14 lines

that it can inline this function, which causes the assembler to complain about duplicate symbols. Mark the function as "noinline" to prevent this.

---
 x86/realmode.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
x86/realmode.c +0 -0
index b4fa603..07a477f 100644

x86/realmode.c static inline void init_inregs(struct regs *regs) · line 181

Quoted 5 lines
x86/realmode.c +1 -1
@@ -178,7 +178,7 @@ static inline void init_inregs(struct regs *regs)
 		inregs.esp = (unsigned long)&tmp_stack.top;
 }
 
-static void exec_in_big_real_mode(struct insn_desc *insn)
+static __attribute__((noinline)) void exec_in_big_real_mode(struct insn_desc *insn)

Forgot to use the new define in this patch :-)

Quoted 6 lines
x86/realmode.c +0 -0
 {
 	unsigned long tmp;
 	static struct regs save;
Signature · 1 line
2.33.0.309.g3052b89438-goog
#12 lore raw

Re: [kvm-unit-tests PATCH v2 2/5] x86: realmode: mark exec_in_big_real_mode as noinline

Quoted 25 lines · Sean Christopherson
Quoted 1 line · Bill Wendling

exec_in_big_real_mode() uses inline asm that has labels. Clang decides

_global_ labels. Inlining functions with local labels, including asm goto labels, is not problematic, the issue is specific to labels that must be unique for a given compilation unit.

Quoted 17 lines

that it can inline this function, which causes the assembler to complain about duplicate symbols. Mark the function as "noinline" to prevent this.

---
 x86/realmode.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
x86/realmode.c +0 -0
index b4fa603..07a477f 100644
@@ -178,7 +178,7 @@ static inline void init_inregs(struct regs *regs)
              inregs.esp = (unsigned long)&tmp_stack.top;
 }
 
Quoted 5 lines

x86/realmode.c static inline void init_inregs(struct regs *regs) · line 181

Quoted 2 lines
x86/realmode.c +1 -1
-static void exec_in_big_real_mode(struct insn_desc *insn)
+static __attribute__((noinline)) void exec_in_big_real_mode(struct insn_desc *insn)

Forgot to use the new define in this patch :-)

This was intentional. realmode.c doesn't #include any header files, and adding '#include "libflat.h" causes a lot of warnings and errors. We could do that, but I feel it's beyond the scope of this series of patches.

-bw

#13 lore raw

Re: [kvm-unit-tests PATCH v2 2/5] x86: realmode: mark exec_in_big_real_mode as noinline

Quoted 31 lines · Bill Wendling
Quoted 25 lines · Sean Christopherson
Quoted 1 line · Bill Wendling

exec_in_big_real_mode() uses inline asm that has labels. Clang decides

_global_ labels. Inlining functions with local labels, including asm goto labels, is not problematic, the issue is specific to labels that must be unique for a given compilation unit.

Quoted 17 lines

that it can inline this function, which causes the assembler to complain about duplicate symbols. Mark the function as "noinline" to prevent this.

---
 x86/realmode.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
x86/realmode.c +0 -0
index b4fa603..07a477f 100644
@@ -178,7 +178,7 @@ static inline void init_inregs(struct regs *regs)
              inregs.esp = (unsigned long)&tmp_stack.top;
 }
 
Quoted 5 lines

x86/realmode.c static inline void init_inregs(struct regs *regs) · line 181

Quoted 2 lines
x86/realmode.c +1 -1
-static void exec_in_big_real_mode(struct insn_desc *insn)
+static __attribute__((noinline)) void exec_in_big_real_mode(struct insn_desc *insn)

Forgot to use the new define in this patch :-)

Quoted 4 lines

This was intentional. realmode.c doesn't #include any header files, and adding '#include "libflat.h" causes a lot of warnings and errors. We could do that, but I feel it's beyond the scope of this series of patches.

Ah, right, realmode is compiled for real mode and can't use any of the libcflat stuff.

A better option would be to put the #define in linux/compiler.h and include that in libcflat.h and directly in realmode.h. It only requires a small prep patch to avoid a duplicate barrier() definition.

From 6e6971ef22c335732a9597409a45fee8a3be6fb7 Mon Sep 17 00:00:00 2001
From: Sean Christopherson <[email protected]>
Date: Wed, 8 Sep 2021 15:41:12 -0700
Subject: [PATCH] lib: Drop x86/processor.h's barrier() in favor of compiler.h
version

Drop x86's duplicate version of barrier() in favor of the generic #define provided by linux/compiler.h. Include compiler.h in the all-encompassing libcflat.h to pick up barrier() and other future goodies, e.g. new attributes defines.

---
 lib/libcflat.h      | 1 +
 lib/x86/processor.h | 5 -----
 2 files changed, 1 insertion(+), 5 deletions(-)
lib/libcflat.h +1 -0
index 97db9e3..e619de1 100644
@@ -22,6 +22,7 @@
 
 #ifndef __ASSEMBLY__
 
+#include <linux/compiler.h>
 #include <stdarg.h>
 #include <stddef.h>
 #include <stdint.h>
lib/x86/processor.h +0 -5
index f380321..eaf24d4 100644
@@ -216,11 +216,6 @@ struct descriptor_table_ptr {
     ulong base;
 } __attribute__((packed));
 
-static inline void barrier(void)
-{
-    asm volatile ("" : : : "memory");
-}
-
 static inline void clac(void)
 {
     asm volatile (".byte 0x0f, 0x01, 0xca" : : : "memory");

--

and then your patch 1 can become:

lib/linux/compiler.h +1 -0
index 5d9552a..5937b7b 100644
@@ -46,6 +46,7 @@
 #define barrier()      asm volatile("" : : : "memory")
 
 #define __always_inline        inline __attribute__((always_inline))
+#define noinline __attribute__((noinline))
 
#14 lore raw

Re: [kvm-unit-tests PATCH v2 2/5] x86: realmode: mark exec_in_big_real_mode as noinline

Quoted 38 lines · Sean Christopherson
Quoted 31 lines · Bill Wendling
Quoted 25 lines · Sean Christopherson
Quoted 1 line · Bill Wendling

exec_in_big_real_mode() uses inline asm that has labels. Clang decides

_global_ labels. Inlining functions with local labels, including asm goto labels, is not problematic, the issue is specific to labels that must be unique for a given compilation unit.

Quoted 17 lines

that it can inline this function, which causes the assembler to complain about duplicate symbols. Mark the function as "noinline" to prevent this.

---
 x86/realmode.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
x86/realmode.c +0 -0
index b4fa603..07a477f 100644
@@ -178,7 +178,7 @@ static inline void init_inregs(struct regs *regs)
              inregs.esp = (unsigned long)&tmp_stack.top;
 }
 
Quoted 5 lines

x86/realmode.c static inline void init_inregs(struct regs *regs) · line 181

Quoted 2 lines
x86/realmode.c +1 -1
-static void exec_in_big_real_mode(struct insn_desc *insn)
+static __attribute__((noinline)) void exec_in_big_real_mode(struct insn_desc *insn)

Forgot to use the new define in this patch :-)

Quoted 4 lines

This was intentional. realmode.c doesn't #include any header files, and adding '#include "libflat.h" causes a lot of warnings and errors. We could do that, but I feel it's beyond the scope of this series of patches.

Quoted 7 lines

Ah, right, realmode is compiled for real mode and can't use any of the libcflat stuff.

A better option would be to put the #define in linux/compiler.h and include that in libcflat.h and directly in realmode.h. It only requires a small prep patch to avoid a duplicate barrier() definition.

Sounds good to me. Please go ahead and submit this for inclusion and I can revamp my stuff.

-bw

Quoted 59 lines

From 6e6971ef22c335732a9597409a45fee8a3be6fb7 Mon Sep 17 00:00:00 2001
From: Sean Christopherson <[email protected]>
Date: Wed, 8 Sep 2021 15:41:12 -0700
Subject: [PATCH] lib: Drop x86/processor.h's barrier() in favor of compiler.h
version

Drop x86's duplicate version of barrier() in favor of the generic #define provided by linux/compiler.h. Include compiler.h in the all-encompassing libcflat.h to pick up barrier() and other future goodies, e.g. new attributes defines.

---
 lib/libcflat.h      | 1 +
 lib/x86/processor.h | 5 -----
 2 files changed, 1 insertion(+), 5 deletions(-)
lib/libcflat.h +1 -0
index 97db9e3..e619de1 100644
@@ -22,6 +22,7 @@
 
 #ifndef __ASSEMBLY__
 
+#include <linux/compiler.h>
 #include <stdarg.h>
 #include <stddef.h>
 #include <stdint.h>
lib/x86/processor.h +0 -6
index f380321..eaf24d4 100644
@@ -216,11 +216,6 @@ struct descriptor_table_ptr {
     ulong base;
 } __attribute__((packed));
 
-static inline void barrier(void)
-{
-    asm volatile ("" : : : "memory");
-}
-
 static inline void clac(void)
 {
     asm volatile (".byte 0x0f, 0x01, 0xca" : : : "memory");
--

and then your patch 1 can become:

lib/linux/compiler.h +1 -0
index 5d9552a..5937b7b 100644
@@ -46,6 +46,7 @@
 #define barrier()      asm volatile("" : : : "memory")
 
 #define __always_inline        inline __attribute__((always_inline))
+#define noinline __attribute__((noinline))
 
#15 lore raw

PATCH v2 3/5 x86: svm: mark test_run as noinline

In reply to Bill Wendling

test_run uses inline asm that has labels. Clang decides that it can inline this function, which causes the assembler to complain about duplicate symbols. Mark the function as "noinline" to prevent this.

---
v2: Use "noinline" instead of "__attribute__((noinline)).

---
 x86/svm.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
x86/svm.c +1 -1
index beb40b7..3f94b2a 100644
@@ -256,7 +256,7 @@ int svm_vmrun(void)
 
 extern u8 vmrun_rip;
 
-static void test_run(struct svm_test *test)
+static noinline void test_run(struct svm_test *test)
 {
 	u64 vmcb_phys = virt_to_phys(vmcb);
 
Signature · 1 line
2.33.0.309.g3052b89438-goog
#16 lore raw

PATCH v2 4/5 x86: umip: mark do_ring3 as noinline

In reply to Bill Wendling

do_ring3 uses inline asm that has labels. Clang decides that it can inline this function, which causes the assembler to complain about duplicate symbols. Mark the function as "noinline" to prevent this.

---
v2: Use "noinline" instead of "__attribute__((noinline)).

---
 x86/umip.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
x86/umip.c +1 -1
index c5700b3..0fc1f65 100644
@@ -114,7 +114,7 @@ static void test_umip_gp(const char *msg)
 
 /* The ugly mode switching code */
 
-static int do_ring3(void (*fn)(const char *), const char *arg)
+static noinline int do_ring3(void (*fn)(const char *), const char *arg)
 {
     static unsigned char user_stack[4096];
     int ret;
Signature · 1 line
2.33.0.309.g3052b89438-goog
#17 lore raw

PATCH v2 5/5 x86: vmx: mark some test_* functions as noinline

In reply to Bill Wendling

Some test_* functions use inline asm that has labels. Clang decides that it can inline these functions, which causes the assembler to complain about duplicate symbols. Mark the functions as "noinline" to prevent this.

---
v2: Use "noinline" instead of "__attribute__((noinline)).

---
 x86/vmx.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
x86/vmx.c +3 -3
index f0b853a..2a32aa2 100644
@@ -426,7 +426,7 @@ static void prep_flags_test_env(void **vpage, struct vmcs **vmcs, handler *old)
 	*old = handle_exception(PF_VECTOR, &pf_handler);
 }
 
-static void test_read_sentinel(void)
+static noinline void test_read_sentinel(void)
 {
 	void *vpage;
 	struct vmcs *vmcs;
@@ -474,7 +474,7 @@ static void test_vmread_flags_touch(void)
 	test_read_sentinel();
 }
 
-static void test_write_sentinel(void)
+static noinline void test_write_sentinel(void)
 {
 	void *vpage;
 	struct vmcs *vmcs;
@@ -1786,7 +1786,7 @@ static int exit_handler(union exit_reason exit_reason)
  * Tries to enter the guest, populates @result with VM-Fail, VM-Exit, entered,
  * etc...
  */
-static void vmx_enter_guest(struct vmentry_result *result)
+static noinline void vmx_enter_guest(struct vmentry_result *result)
 {
 	memset(result, 0, sizeof(*result));
 
Signature · 1 line
2.33.0.309.g3052b89438-goog