Skip to contentSkip to conversation

KVM: arm64: Ask the compiler to __always_inline functions used by KVM at HYP

PATCH In review James Morse 5 participants 13 messages Feb 20, 2020 – Feb 24, 2020

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

cover KVM: arm64: Ask the compiler to __always_inline functions used by KVM at HYP

Hello!

It turns out KVM relies on the inline hint being honoured by the compiler in quite a few more places than expected. Something about the Shadow Call Stack support[0] causes the compiler to avoid inline-ing and to place these functions outside the __hyp_text. This ruins KVM's day.

Add the simon-says __always_inline annotation to all the static inlines that KVM calls from HYP code.

This series based on v5.6-rc2.

[0] https://lore.kernel.org/linux-arm-kernel/[email protected]/

Thanks,

James Morse (3):
KVM: arm64: Ask the compiler to __always_inline functions used at HYP
KVM: arm64: define our own swab32() to avoid a uapi static inline
arm64: Ask the compiler to __always_inline functions used by KVM at
HYP

 arch/arm64/include/asm/arch_gicv3.h      |  2 +-
 arch/arm64/include/asm/cache.h           |  2 +-
 arch/arm64/include/asm/cacheflush.h      |  2 +-
 arch/arm64/include/asm/cpufeature.h      | 10 ++---
 arch/arm64/include/asm/io.h              |  4 +-
 arch/arm64/include/asm/kvm_emulate.h     | 48 ++++++++++++------------
 arch/arm64/include/asm/kvm_hyp.h         |  7 ++++
 arch/arm64/include/asm/kvm_mmu.h         |  3 +-
 arch/arm64/include/asm/virt.h            |  2 +-
 arch/arm64/kvm/hyp/vgic-v2-cpuif-proxy.c |  4 +-
 10 files changed, 46 insertions(+), 38 deletions(-)
Signature · 6 lines
2.24.1

_______________________________________________
kvmarm mailing list
[email protected]
https://lists.cs.columbia.edu/mailman/listinfo/kvmarm
#1 lore raw

PATCH 1/3 KVM: arm64: Ask the compiler to __always_inline functions used at HYP

On non VHE CPUs, KVM's __hyp_text contains code run at EL2 while the rest of the kernel runs at EL1. This code lives in its own section with start and end markers so we can map it to EL2.

The compiler may decide not to inline static-inline functions from the header file. It may also decide not to put these out-of-line functions in the same section, meaning they aren't mapped when called at EL2.

Clang-9 does exactly this with __kern_hyp_va() and a few others when x18 is reserved for the shadow call stack. Add the additional __always_ hint to all the static-inlines that are called from a hyp file.

----
kvm_get_hyp_vector() pulls in all the regular per-cpu accessors and this_cpu_has_cap(), fortunately its only called for VHE.

---
 arch/arm64/include/asm/arch_gicv3.h  |  2 +-
 arch/arm64/include/asm/cpufeature.h  |  2 +-
 arch/arm64/include/asm/kvm_emulate.h | 48 ++++++++++++++--------------
 arch/arm64/include/asm/kvm_mmu.h     |  3 +-
 arch/arm64/include/asm/virt.h        |  2 +-
 5 files changed, 29 insertions(+), 28 deletions(-)
arch/arm64/include/asm/arch_gicv3.h +1 -1
index 25fec4bde43a..a358e97572c1 100644
@@ -32,7 +32,7 @@ static inline void gic_write_eoir(u32 irq)
 	isb();
 }
 
-static inline void gic_write_dir(u32 irq)
+static __always_inline void gic_write_dir(u32 irq)
 {
 	write_sysreg_s(irq, SYS_ICC_DIR_EL1);
 	isb();
arch/arm64/include/asm/cpufeature.h +1 -1
index 92ef9539874a..42ce41eef274 100644
@@ -581,7 +581,7 @@ static inline bool system_supports_sve(void)
 		cpus_have_const_cap(ARM64_SVE);
 }
 
-static inline bool system_supports_cnp(void)
+static __always_inline bool system_supports_cnp(void)
 {
 	return IS_ENABLED(CONFIG_ARM64_CNP) &&
 		cpus_have_const_cap(ARM64_HAS_CNP);
arch/arm64/include/asm/kvm_emulate.h +24 -24

172 lines not on this page · raw message

arch/arm64/include/asm/kvm_mmu.h +2 -1
index 53d846f1bfe7..785762860c63 100644
@@ -93,7 +93,7 @@ void kvm_update_va_mask(struct alt_instr *alt,
 			__le32 *origptr, __le32 *updptr, int nr_inst);
 void kvm_compute_layout(void);
 
-static inline unsigned long __kern_hyp_va(unsigned long v)
+static __always_inline unsigned long __kern_hyp_va(unsigned long v)
 {
 	asm volatile(ALTERNATIVE_CB("and %0, %0, #1\n"
 				    "ror %0, %0, #1\n"
@@ -473,6 +473,7 @@ static inline int kvm_write_guest_lock(struct kvm *kvm, gpa_t gpa,
 extern void *__kvm_bp_vect_base;
 extern int __kvm_harden_el2_vector_slot;
 
+/*  This is only called on a VHE system */
 static inline void *kvm_get_hyp_vector(void)
 {
 	struct bp_hardening_data *data = arm64_get_bp_hardening_data();
arch/arm64/include/asm/virt.h +1 -1
index 0958ed6191aa..61fd26752adc 100644
@@ -83,7 +83,7 @@ static inline bool is_kernel_in_hyp_mode(void)
 	return read_sysreg(CurrentEL) == CurrentEL_EL2;
 }
 
-static inline bool has_vhe(void)
+static __always_inline bool has_vhe(void)
 {
 	if (cpus_have_const_cap(ARM64_HAS_VIRT_HOST_EXTN))
 		return true;
Signature · 6 lines
2.24.1

_______________________________________________
kvmarm mailing list
[email protected]
https://lists.cs.columbia.edu/mailman/listinfo/kvmarm
#2 lore raw

PATCH 2/3 KVM: arm64: define our own swab32() to avoid a uapi static inline

In reply to James Morse

KVM uses swab32() when mediating GIC MMIO accesses if the GICV is badly aligned, and the host and guest differ in endianness.

arm64 doesn't provide a __arch_swab32(), so __fswab32() is always backed by the macro implementation that the compiler reduces to a single instruction. But the static-inline causes problems for KVM if the compiler chooses not to inline this function, it may not be located in the __hyp_text where __vgic_v2_perform_cpuif_access() needs it.

Create our own __kvm_swab32() macro that calls ___constant_swab32() directly. This way we know it will always be inlined.

---
 arch/arm64/include/asm/kvm_hyp.h         | 7 +++++++
 arch/arm64/kvm/hyp/vgic-v2-cpuif-proxy.c | 4 ++--
 2 files changed, 9 insertions(+), 2 deletions(-)
arch/arm64/include/asm/kvm_hyp.h +7 -0
index a3a6a2ba9a63..fe57f60f06a8 100644
@@ -47,6 +47,13 @@
 #define read_sysreg_el2(r)	read_sysreg_elx(r, _EL2, _EL1)
 #define write_sysreg_el2(v,r)	write_sysreg_elx(v, r, _EL2, _EL1)
 
+/*
+ * Without an __arch_swab32(), we fall back to ___constant_swab32(), but the
+ * static inline can allow the compiler to out-of-line this. KVM always wants
+ * the macro version as its always inlined.
+ */
+#define __kvm_swab32(x)	___constant_swab32(x)
+
 int __vgic_v2_perform_cpuif_access(struct kvm_vcpu *vcpu);
 
 void __vgic_v3_save_state(struct kvm_vcpu *vcpu);
arch/arm64/kvm/hyp/vgic-v2-cpuif-proxy.c +2 -2
index 29ee1feba4eb..4f3a087e36d5 100644
@@ -69,14 +69,14 @@ int __hyp_text __vgic_v2_perform_cpuif_access(struct kvm_vcpu *vcpu)
 		u32 data = vcpu_get_reg(vcpu, rd);
 		if (__is_be(vcpu)) {
 			/* guest pre-swabbed data, undo this for writel() */
-			data = swab32(data);
+			data = __kvm_swab32(data);
 		}
 		writel_relaxed(data, addr);
 	} else {
 		u32 data = readl_relaxed(addr);
 		if (__is_be(vcpu)) {
 			/* guest expects swabbed data */
-			data = swab32(data);
+			data = __kvm_swab32(data);
 		}
 		vcpu_set_reg(vcpu, rd, data);
 	}
Signature · 6 lines
2.24.1

_______________________________________________
kvmarm mailing list
[email protected]
https://lists.cs.columbia.edu/mailman/listinfo/kvmarm
#3 lore raw

PATCH 3/3 arm64: Ask the compiler to __always_inline functions used by KVM at HYP

In reply to James Morse

KVM uses some of the static-inline helpers like icache_is_vipt() from its HYP code. This assumes the function is inlined so that the code is mapped to EL2. The compiler may decide not to inline these, and the out-of-line version may not be in the __hyp_text section.

Add the additional __always_ hint to these static-inlines that are used by KVM.

---
 arch/arm64/include/asm/cache.h      | 2 +-
 arch/arm64/include/asm/cacheflush.h | 2 +-
 arch/arm64/include/asm/cpufeature.h | 8 ++++----
 arch/arm64/include/asm/io.h         | 4 ++--
 4 files changed, 8 insertions(+), 8 deletions(-)
arch/arm64/include/asm/cache.h +1 -1
index 806e9dc2a852..a4d1b5f771f6 100644
@@ -69,7 +69,7 @@ static inline int icache_is_aliasing(void)
 	return test_bit(ICACHEF_ALIASING, &__icache_flags);
 }
 
-static inline int icache_is_vpipt(void)
+static __always_inline int icache_is_vpipt(void)
 {
 	return test_bit(ICACHEF_VPIPT, &__icache_flags);
 }
arch/arm64/include/asm/cacheflush.h +1 -1
index 665c78e0665a..e6cca3d4acf7 100644
@@ -145,7 +145,7 @@ extern void copy_to_user_page(struct vm_area_struct *, struct page *,
 #define ARCH_IMPLEMENTS_FLUSH_DCACHE_PAGE 1
 extern void flush_dcache_page(struct page *);
 
-static inline void __flush_icache_all(void)
+static __always_inline void __flush_icache_all(void)
 {
 	if (cpus_have_const_cap(ARM64_HAS_CACHE_DIC))
 		return;
arch/arm64/include/asm/cpufeature.h +4 -4
index 42ce41eef274..2a746b99e937 100644
@@ -435,13 +435,13 @@ cpuid_feature_extract_signed_field(u64 features, int field)
 	return cpuid_feature_extract_signed_field_width(features, field, 4);
 }
 
-static inline unsigned int __attribute_const__
+static __always_inline unsigned int __attribute_const__
 cpuid_feature_extract_unsigned_field_width(u64 features, int field, int width)
 {
 	return (u64)(features << (64 - width - field)) >> (64 - width);
 }
 
-static inline unsigned int __attribute_const__
+static __always_inline unsigned int __attribute_const__
 cpuid_feature_extract_unsigned_field(u64 features, int field)
 {
 	return cpuid_feature_extract_unsigned_field_width(features, field, 4);
@@ -564,7 +564,7 @@ static inline bool system_supports_mixed_endian(void)
 	return val == 0x1;
 }
 
-static inline bool system_supports_fpsimd(void)
+static __always_inline bool system_supports_fpsimd(void)
 {
 	return !cpus_have_const_cap(ARM64_HAS_NO_FPSIMD);
 }
@@ -575,7 +575,7 @@ static inline bool system_uses_ttbr0_pan(void)
 		!cpus_have_const_cap(ARM64_HAS_PAN);
 }
 
-static inline bool system_supports_sve(void)
+static __always_inline bool system_supports_sve(void)
 {
 	return IS_ENABLED(CONFIG_ARM64_SVE) &&
 		cpus_have_const_cap(ARM64_SVE);
arch/arm64/include/asm/io.h +2 -2
index 4e531f57147d..6facd1308e7c 100644
@@ -34,7 +34,7 @@ static inline void __raw_writew(u16 val, volatile void __iomem *addr)
 }
 
 #define __raw_writel __raw_writel
-static inline void __raw_writel(u32 val, volatile void __iomem *addr)
+static __always_inline void __raw_writel(u32 val, volatile void __iomem *addr)
 {
 	asm volatile("str %w0, [%1]" : : "rZ" (val), "r" (addr));
 }
@@ -69,7 +69,7 @@ static inline u16 __raw_readw(const volatile void __iomem *addr)
 }
 
 #define __raw_readl __raw_readl
-static inline u32 __raw_readl(const volatile void __iomem *addr)
+static __always_inline u32 __raw_readl(const volatile void __iomem *addr)
 {
 	u32 val;
 	asm volatile(ALTERNATIVE("ldr %w0, [%1]",
Signature · 6 lines
2.24.1

_______________________________________________
kvmarm mailing list
[email protected]
https://lists.cs.columbia.edu/mailman/listinfo/kvmarm
maintainer Acked-by on v1 3/3
#4 lore raw

Re: [PATCH 3/3] arm64: Ask the compiler to __always_inline functions used by KVM at HYP

Quoted 8 lines · James Morse

KVM uses some of the static-inline helpers like icache_is_vipt() from its HYP code. This assumes the function is inlined so that the code is mapped to EL2. The compiler may decide not to inline these, and the out-of-line version may not be in the __hyp_text section.

Add the additional __always_ hint to these static-inlines that are used by KVM.

Quoted 7 lines
---
 arch/arm64/include/asm/cache.h      | 2 +-
 arch/arm64/include/asm/cacheflush.h | 2 +-
 arch/arm64/include/asm/cpufeature.h | 8 ++++----
 arch/arm64/include/asm/io.h         | 4 ++--
 4 files changed, 8 insertions(+), 8 deletions(-)

It's the right thing to do, but if this stuff keeps trickling in then we should make CONFIG_OPTIMIZE_INLINING depend on !ARM64 because seeing "__always_inline" tells you nothing about /why/ it needs to be there and it's hard to know if/when you can remove those annotations in future.

Will
_______________________________________________
kvmarm mailing list
[email protected]
https://lists.cs.columbia.edu/mailman/listinfo/kvmarm

#5 lore raw

Re: [PATCH 3/3] arm64: Ask the compiler to __always_inline functions used by KVM at HYP

Quoted 18 lines · Will Deacon
Quoted 8 lines · James Morse

KVM uses some of the static-inline helpers like icache_is_vipt() from its HYP code. This assumes the function is inlined so that the code is mapped to EL2. The compiler may decide not to inline these, and the out-of-line version may not be in the __hyp_text section.

Add the additional __always_ hint to these static-inlines that are used by KVM.

Quoted 7 lines
---
 arch/arm64/include/asm/cache.h      | 2 +-
 arch/arm64/include/asm/cacheflush.h | 2 +-
 arch/arm64/include/asm/cpufeature.h | 8 ++++----
 arch/arm64/include/asm/io.h         | 4 ++--
 4 files changed, 8 insertions(+), 8 deletions(-)
Quoted 7 lines

It's the right thing to do, but if this stuff keeps trickling in then we should make CONFIG_OPTIMIZE_INLINING depend on !ARM64 because seeing "__always_inline" tells you nothing about /why/ it needs to be there and it's hard to know if/when you can remove those annotations in future.

We might need to follow the same approach as we took for the EFI stub, and create a special __kvm_hyp symbol namespace so that we can carefully control which routines from the kernel proper it has access to.
_______________________________________________
kvmarm mailing list
[email protected]
https://lists.cs.columbia.edu/mailman/listinfo/kvmarm

#6 lore raw
In reply to James Morse
Quoted 8 lines · James Morse

Hello!

It turns out KVM relies on the inline hint being honoured by the compiler in quite a few more places than expected. Something about the Shadow Call Stack support[0] causes the compiler to avoid inline-ing and to place these functions outside the __hyp_text. This ruins KVM's day.

Quoted 5 lines

Add the simon-says __always_inline annotation to all the static inlines that KVM calls from HYP code.

This series based on v5.6-rc2.

This isn't quite as yuck as I expected, fortunately, but it does beg the question whether we shouldn't simply map the entire kernel at EL2 instead?
_______________________________________________
kvmarm mailing list
[email protected]
https://lists.cs.columbia.edu/mailman/listinfo/kvmarm

#7 lore raw

Hi Ard,

Quoted 6 lines · Ard Biesheuvel
Quoted 5 lines · James Morse

It turns out KVM relies on the inline hint being honoured by the compiler in quite a few more places than expected. Something about the Shadow Call Stack support[0] causes the compiler to avoid inline-ing and to place these functions outside the __hyp_text. This ruins KVM's day.

Quoted 6 lines
Quoted 2 lines

Add the simon-says __always_inline annotation to all the static inlines that KVM calls from HYP code.

This isn't quite as yuck as I expected, fortunately, but it does beg the question whether we shouldn't simply map the entire kernel at EL2 instead?

If the kernel is big enough to need internal veneers (the 128M range?), these would certainly go horribly wrong because its running somewhere other than the relocation-time address. We would need a way of telling the linker to keep the bits of KVM close together...

Thanks,

James
_______________________________________________
kvmarm mailing list
[email protected]
https://lists.cs.columbia.edu/mailman/listinfo/kvmarm

#8 lore raw
Quoted 13 lines · James Morse

Hi Ard,

Quoted 8 lines · Ard Biesheuvel
Quoted 7 lines · James Morse

It turns out KVM relies on the inline hint being honoured by the compiler in quite a few more places than expected. Something about the Shadow Call Stack support[0] causes the compiler to avoid inline-ing and to place these functions outside the __hyp_text. This ruins KVM's day.

Add the simon-says __always_inline annotation to all the static inlines that KVM calls from HYP code.

Quoted 8 lines
Quoted 3 lines

This isn't quite as yuck as I expected, fortunately, but it does beg the question whether we shouldn't simply map the entire kernel at EL2 instead?

If the kernel is big enough to need internal veneers (the 128M range?), these would certainly go horribly wrong because its running somewhere other than the relocation-time address. We would need a way of telling the linker to keep the bits of KVM close together...

Ah, of course, there is that as well ...
_______________________________________________
kvmarm mailing list
[email protected]
https://lists.cs.columbia.edu/mailman/listinfo/kvmarm

maintainer Acked-by on the v1 series
#9 lore raw
In reply to James Morse

Hi James,

Quoted 11 lines · James Morse

Hello!

It turns out KVM relies on the inline hint being honoured by the compiler in quite a few more places than expected. Something about the Shadow Call Stack support[0] causes the compiler to avoid inline-ing and to place these functions outside the __hyp_text. This ruins KVM's day.

Add the simon-says __always_inline annotation to all the static inlines that KVM calls from HYP code.

This series based on v5.6-rc2.

Many thanks for going through all this.

I'm happy to take it if Catalin or Will ack the arm64 patches. It case we decide to go the other way around:

One thing I'd like to look into though is a compile-time check that nothing in the hyp_text section has a reference to a non-hyp_text symbol.

We already have checks around non-init symbols pointing to init symbols, and I was wondering if we could reuse this for fun and profit...

Thanks,

        M.
Signature · 5 lines
Jazz is not dead. It just smells funny...
_______________________________________________
kvmarm mailing list
[email protected]
https://lists.cs.columbia.edu/mailman/listinfo/kvmarm
#10 lore raw

Hi Marc,

Quoted 13 lines · Marc Zyngier
Quoted 9 lines · James Morse

It turns out KVM relies on the inline hint being honoured by the compiler in quite a few more places than expected. Something about the Shadow Call Stack support[0] causes the compiler to avoid inline-ing and to place these functions outside the __hyp_text. This ruins KVM's day.

Add the simon-says __always_inline annotation to all the static inlines that KVM calls from HYP code.

This series based on v5.6-rc2.

Many thanks for going through all this.

Quoted 8 lines

I'm happy to take it if Catalin or Will ack the arm64 patches. It case we decide to go the other way around:

One thing I'd like to look into though is a compile-time check that nothing in the hyp_text section has a reference to a non-hyp_text symbol.

Heh, that hypothetical tool would choke on things like arch/arm64/kvm/hyp/tlb.c:
| static void __hyp_text __tlb_switch_to_guest_vhe(...)
| {

[...]

| local_irq_save(cxt->flags);

which calls trace_hardirqs_off() ... which is absolutely fine because this only happens on VHE.

To do it purely with the section information, you'd need to separate all the VHE code... (maybe as a debug option that only runs when VHE is turned off?)

Quoted 2 lines

We already have checks around non-init symbols pointing to init symbols, and I was wondering if we could reuse this for fun and profit...

I think objtool is the tool-of-the-future that can do this. You need something that believes everything behind has_vhe() is unreachable...

Thanks,

James
_______________________________________________
kvmarm mailing list
[email protected]
https://lists.cs.columbia.edu/mailman/listinfo/kvmarm

maintainer
#11 lore raw

Hi James,

Quoted 29 lines · James Morse

Hi Marc,

Quoted 13 lines · Marc Zyngier
Quoted 9 lines · James Morse

It turns out KVM relies on the inline hint being honoured by the compiler in quite a few more places than expected. Something about the Shadow Call Stack support[0] causes the compiler to avoid inline-ing and to place these functions outside the __hyp_text. This ruins KVM's day.

Add the simon-says __always_inline annotation to all the static inlines that KVM calls from HYP code.

This series based on v5.6-rc2.

Many thanks for going through all this.

Quoted 8 lines

I'm happy to take it if Catalin or Will ack the arm64 patches. It case we decide to go the other way around:

One thing I'd like to look into though is a compile-time check that nothing in the hyp_text section has a reference to a non-hyp_text symbol.

Heh, that hypothetical tool would choke on things like arch/arm64/kvm/hyp/tlb.c:
| static void __hyp_text __tlb_switch_to_guest_vhe(...)
| {

Quoted 6 lines

[...]

| local_irq_save(cxt->flags);

which calls trace_hardirqs_off() ... which is absolutely fine because this only happens on VHE.

Duh, indeed.

Quoted 3 lines

To do it purely with the section information, you'd need to separate all the VHE code... (maybe as a debug option that only runs when VHE is turned off?)

We may have to to that anyway at some point. If the "KVM compartment" thing becomes real, we may have to end-up compiling both separately (and jettison the one we don't need at runtime).

Quoted 5 lines
Quoted 2 lines

We already have checks around non-init symbols pointing to init symbols, and I was wondering if we could reuse this for fun and profit...

I think objtool is the tool-of-the-future that can do this. You need something that believes everything behind has_vhe() is unreachable...

I need to educate myself about objtool. Seems to be the miracle cure for a lot of ailments! ;-)

Anyway, I've now queued the series for 5.6.

         M.
Signature · 6 lines
Jazz is not dead. It just smells funny...

_______________________________________________
linux-arm-kernel mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel
#12 lore raw
In reply to Marc Zyngier
Quoted 22 lines · Marc Zyngier

Hi James,

Quoted 9 lines · James Morse

Hello!

It turns out KVM relies on the inline hint being honoured by the compiler
in quite a few more places than expected. Something about the Shadow Call
Stack support[0] causes the compiler to avoid inline-ing and to place these functions outside the __hyp_text. This ruins KVM's day.

Quoted 4 lines

Add the simon-says __always_inline annotation to all the static inlines that KVM calls from HYP code.

This series based on v5.6-rc2.

Many thanks for going through all this.

I'm happy to take it if Catalin or Will ack the arm64 patches. It case we decide to go the other way around:

Quoted 8 lines

One thing I'd like to look into though is a compile-time check that nothing in the hyp_text section has a reference to a non-hyp_text symbol.

We already have checks around non-init symbols pointing to init symbols, and I was wondering if we could reuse this for fun and profit...

Hi Marc,

I recall that you've suggested that before, and I even tried it around that time [*]. I wasn't happy enough with it to post a proper patch though.

[*] https://lists.cs.columbia.edu/pipermail/kvmarm/2018-May/031629.html

Thanks,
drew

Quoted 11 lines

Thanks,

        M.
Signature · 5 lines
Jazz is not dead. It just smells funny...
_______________________________________________
kvmarm mailing list
[email protected]
https://lists.cs.columbia.edu/mailman/listinfo/kvmarm

_______________________________________________
kvmarm mailing list
[email protected]
https://lists.cs.columbia.edu/mailman/listinfo/kvmarm