Skip to contentSkip to conversation

KVM: arm64: Make vcpu flag updates non-preemptible

PATCH Superseded Marc Zyngier 2 participants 3 messages Apr 17, 2023

Subsystems (4)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Marc Zyngier

author maintainer
#0 lore raw

PATCH 1/1 KVM: arm64: Make vcpu flag updates non-preemptible

Per-vcpu flags are updated using a non-atomic RMW operation. Which means it is possible to get preempted between the read and write operations.

Another interesting thing to note is that preemption also updates flags, as we have some flag manipulation in both the load and put operations.

It is thus possible to lose information communicated by either load or put, as the preempted flag update will overwrite the flags when the thread is resumed. This is specially critical if either load or put has stored information which depends on the physical CPU the vcpu runs on.

This results in really elusive bugs, and kudos must be given to Mostafa for the long hours of debugging, and finally spotting the problem.

Fixes e87abb73e594 ("KVM: arm64: Add helpers to manipulate vcpu flags among a set")
---
 arch/arm64/include/asm/kvm_host.h | 17 +++++++++++++++++
 1 file changed, 17 insertions(+)
arch/arm64/include/asm/kvm_host.h +17 -0
index bcd774d74f34..d716cfd806e8 100644
@@ -579,6 +579,19 @@ struct kvm_vcpu_arch {
 		v->arch.flagset & (m);				\
 	})
 
+/*
+ * Note that the set/clear accessors must be preempt-safe in order to
+ * avoid nesting them with load/put which also manipulate flags...
+ */
+#ifdef __KVM_NVHE_HYPERVISOR__
+/* the nVHE hypervisor is always non-preemptible */
+#define __vcpu_flags_preempt_disable()
+#define __vcpu_flags_preempt_enable()
+#else
+#define __vcpu_flags_preempt_disable()	preempt_disable()
+#define __vcpu_flags_preempt_enable()	preempt_enable()
+#endif
+
 #define __vcpu_set_flag(v, flagset, f, m)			\
 	do {							\
 		typeof(v->arch.flagset) *fset;			\
@@ -586,9 +599,11 @@ struct kvm_vcpu_arch {
 		__build_check_flag(v, flagset, f, m);		\
 								\
 		fset = &v->arch.flagset;			\
+		__vcpu_flags_preempt_disable();			\
 		if (HWEIGHT(m) > 1)				\
 			*fset &= ~(m);				\
 		*fset |= (f);					\
+		__vcpu_flags_preempt_enable();			\
 	} while (0)
 
 #define __vcpu_clear_flag(v, flagset, f, m)			\
@@ -598,7 +613,9 @@ struct kvm_vcpu_arch {
 		__build_check_flag(v, flagset, f, m);		\
 								\
 		fset = &v->arch.flagset;			\
+		__vcpu_flags_preempt_disable();			\
 		*fset &= ~(m);					\
+		__vcpu_flags_preempt_enable();			\
 	} while (0)
 
 #define vcpu_get_flag(v, ...)	__vcpu_get_flag((v), __VA_ARGS__)
Signature · 1 line
2.34.1
maintainer
#1 lore raw
Quoted 36 lines · Marc Zyngier

Per-vcpu flags are updated using a non-atomic RMW operation. Which means it is possible to get preempted between the read and write operations.

Another interesting thing to note is that preemption also updates flags, as we have some flag manipulation in both the load and put operations.

It is thus possible to lose information communicated by either load or put, as the preempted flag update will overwrite the flags when the thread is resumed. This is specially critical if either load or put has stored information which depends on the physical CPU the vcpu runs on.

This results in really elusive bugs, and kudos must be given to Mostafa for the long hours of debugging, and finally spotting the problem.

Fixes e87abb73e594 ("KVM: arm64: Add helpers to manipulate vcpu flags among a set")
---
 arch/arm64/include/asm/kvm_host.h | 17 +++++++++++++++++
 1 file changed, 17 insertions(+)
arch/arm64/include/asm/kvm_host.h +2 -0
index bcd774d74f34..d716cfd806e8 100644
@@ -579,6 +579,19 @@ struct kvm_vcpu_arch {
 		v->arch.flagset & (m);				\
 	})
 
+/*
+ * Note that the set/clear accessors must be preempt-safe in order to

arch/arm64/include/asm/kvm_host.h struct kvm_vcpu_arch { · line 593

Quoted 10 lines
arch/arm64/include/asm/kvm_host.h +10 -0
+ * avoid nesting them with load/put which also manipulate flags...
+ */
+#ifdef __KVM_NVHE_HYPERVISOR__
+/* the nVHE hypervisor is always non-preemptible */
+#define __vcpu_flags_preempt_disable()
+#define __vcpu_flags_preempt_enable()
+#else
+#define __vcpu_flags_preempt_disable()	preempt_disable()
+#define __vcpu_flags_preempt_enable()	preempt_enable()
+#endif

If it makes things cleaner, we could define local (empty) copies of these preempt_*() macros at EL2 to save you having to wrap them here. Up to you.

Quoted 16 lines
arch/arm64/include/asm/kvm_host.h +2 -0
 #define __vcpu_set_flag(v, flagset, f, m)			\
 	do {							\
 		typeof(v->arch.flagset) *fset;			\
@@ -586,9 +599,11 @@ struct kvm_vcpu_arch {
 		__build_check_flag(v, flagset, f, m);		\
 								\
 		fset = &v->arch.flagset;			\
+		__vcpu_flags_preempt_disable();			\
 		if (HWEIGHT(m) > 1)				\
 			*fset &= ~(m);				\
 		*fset |= (f);					\
+		__vcpu_flags_preempt_enable();			\
 	} while (0)
 
 #define __vcpu_clear_flag(v, flagset, f, m)			\

arch/arm64/include/asm/kvm_host.h struct kvm_vcpu_arch { · line 621

Quoted 9 lines
arch/arm64/include/asm/kvm_host.h +2 -0
@@ -598,7 +613,9 @@ struct kvm_vcpu_arch {
 		__build_check_flag(v, flagset, f, m);		\
 								\
 		fset = &v->arch.flagset;			\
+		__vcpu_flags_preempt_disable();			\
 		*fset &= ~(m);					\
+		__vcpu_flags_preempt_enable();			\
 	} while (0)
 
 #define vcpu_get_flag(v, ...)	__vcpu_get_flag((v), __VA_ARGS__)

Given that __vcpu_get_flag() is still preemptible, we should probably add a READ_ONCE() in there when we access the relevant flags field. In practice, they're all single-byte fields so it should be ok, but I think the READ_ONCE() is still worthwhile.

Will

author maintainer
#2 lore raw
Quoted 36 lines · Will Deacon
Quoted 34 lines · Marc Zyngier

Per-vcpu flags are updated using a non-atomic RMW operation. Which means it is possible to get preempted between the read and write operations.

Another interesting thing to note is that preemption also updates flags, as we have some flag manipulation in both the load and put operations.

It is thus possible to lose information communicated by either load or put, as the preempted flag update will overwrite the flags when the thread is resumed. This is specially critical if either load or put has stored information which depends on the physical CPU the vcpu runs on.

This results in really elusive bugs, and kudos must be given to Mostafa for the long hours of debugging, and finally spotting the problem.

Fixes e87abb73e594 ("KVM: arm64: Add helpers to manipulate vcpu flags among a set")
---
 arch/arm64/include/asm/kvm_host.h | 17 +++++++++++++++++
 1 file changed, 17 insertions(+)
arch/arm64/include/asm/kvm_host.h +0 -0
index bcd774d74f34..d716cfd806e8 100644
@@ -579,6 +579,19 @@ struct kvm_vcpu_arch {
 		v->arch.flagset & (m);				\
 	})
 
Quoted 15 lines

arch/arm64/include/asm/kvm_host.h struct kvm_vcpu_arch { · line 593

Quoted 12 lines
arch/arm64/include/asm/kvm_host.h +12 -0
+/*
+ * Note that the set/clear accessors must be preempt-safe in order to
+ * avoid nesting them with load/put which also manipulate flags...
+ */
+#ifdef __KVM_NVHE_HYPERVISOR__
+/* the nVHE hypervisor is always non-preemptible */
+#define __vcpu_flags_preempt_disable()
+#define __vcpu_flags_preempt_enable()
+#else
+#define __vcpu_flags_preempt_disable()	preempt_disable()
+#define __vcpu_flags_preempt_enable()	preempt_enable()
+#endif

If it makes things cleaner, we could define local (empty) copies of these preempt_*() macros at EL2 to save you having to wrap them here. Up to you.

Nah, that's fine. This is subtle enough stuff that I'm happy to see it all exposed in the same location.

Quoted 24 lines
Quoted 24 lines
arch/arm64/include/asm/kvm_host.h +4 -0
 #define __vcpu_set_flag(v, flagset, f, m)			\
 	do {							\
 		typeof(v->arch.flagset) *fset;			\
@@ -586,9 +599,11 @@ struct kvm_vcpu_arch {
 		__build_check_flag(v, flagset, f, m);		\
 								\
 		fset = &v->arch.flagset;			\
+		__vcpu_flags_preempt_disable();			\
 		if (HWEIGHT(m) > 1)				\
 			*fset &= ~(m);				\
 		*fset |= (f);					\
+		__vcpu_flags_preempt_enable();			\
 	} while (0)
 
 #define __vcpu_clear_flag(v, flagset, f, m)			\
@@ -598,7 +613,9 @@ struct kvm_vcpu_arch {
 		__build_check_flag(v, flagset, f, m);		\
 								\
 		fset = &v->arch.flagset;			\
+		__vcpu_flags_preempt_disable();			\
 		*fset &= ~(m);					\
+		__vcpu_flags_preempt_enable();			\
 	} while (0)
 
Quoted 6 lines

arch/arm64/include/asm/kvm_host.h struct kvm_vcpu_arch { · line 621

Quoted 1 line
arch/arm64/include/asm/kvm_host.h +0 -0
 #define vcpu_get_flag(v, ...)	__vcpu_get_flag((v), __VA_ARGS__)

Given that __vcpu_get_flag() is still preemptible, we should probably add a READ_ONCE() in there when we access the relevant flags field. In practice, they're all single-byte fields so it should be ok, but I think the READ_ONCE() is still worthwhile.

Yup, good point. People are already talking about expanding some of the fields for $REASON, so they may become larger than a single byte. And READ_ONCE() makes it clear that there is some level of atomicity required here as well.

I'll respin this shortly.

Thanks,

	M.
Signature · 1 line
Without deviation from the norm, progress is not possible.