Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

powerpc: Fix is_kvm_guest() / kvm_para_available()

Commit a21d1becaa3f ("powerpc: Reintroduce is_kvm_guest() as a fast-path
check") added is_kvm_guest() and changed kvm_para_available() to use it.

is_kvm_guest() checks a static key, kvm_guest, and that static key is
set in check_kvm_guest().

The problem is check_kvm_guest() is only called on pseries, and even
then only in some configurations. That means is_kvm_guest() always
returns false on all non-pseries and some pseries depending on
configuration. That's a bug.

For PR KVM guests this is noticable because they no longer do live
patching of themselves, which can be detected by the omission of a
message in dmesg such as:

KVM: Live patching for a fast VM worked

To fix it make check_kvm_guest() an initcall, to ensure it's always
called at boot. It needs to be core so that it runs before
kvm_guest_init() which is postcore. To be an initcall it needs to return
int, where 0 means success, so update that.

We still call it manually in pSeries_smp_probe(), because that runs
before init calls are run.

Fixes: a21d1becaa3f ("powerpc: Reintroduce is_kvm_guest() as a fast-path check")
Signed-off-by: Michael Ellerman <mpe@ellerman.id.au>
Link: https://lore.kernel.org/r/20210623130514.2543232-1-mpe@ellerman.id.au

+11 -7
+2 -2
arch/powerpc/include/asm/kvm_guest.h
··· 16 16 return static_branch_unlikely(&kvm_guest); 17 17 } 18 18 19 - bool check_kvm_guest(void); 19 + int check_kvm_guest(void); 20 20 #else 21 21 static inline bool is_kvm_guest(void) { return false; } 22 - static inline bool check_kvm_guest(void) { return false; } 22 + static inline int check_kvm_guest(void) { return 0; } 23 23 #endif 24 24 25 25 #endif /* _ASM_POWERPC_KVM_GUEST_H_ */
+6 -4
arch/powerpc/kernel/firmware.c
··· 23 23 24 24 #if defined(CONFIG_PPC_PSERIES) || defined(CONFIG_KVM_GUEST) 25 25 DEFINE_STATIC_KEY_FALSE(kvm_guest); 26 - bool check_kvm_guest(void) 26 + int __init check_kvm_guest(void) 27 27 { 28 28 struct device_node *hyper_node; 29 29 30 30 hyper_node = of_find_node_by_path("/hypervisor"); 31 31 if (!hyper_node) 32 - return false; 32 + return 0; 33 33 34 34 if (!of_device_is_compatible(hyper_node, "linux,kvm")) 35 - return false; 35 + return 0; 36 36 37 37 static_branch_enable(&kvm_guest); 38 - return true; 38 + 39 + return 0; 39 40 } 41 + core_initcall(check_kvm_guest); // before kvm_guest_init() 40 42 #endif
+3 -1
arch/powerpc/platforms/pseries/smp.c
··· 211 211 if (!cpu_has_feature(CPU_FTR_SMT)) 212 212 return; 213 213 214 - if (check_kvm_guest()) { 214 + check_kvm_guest(); 215 + 216 + if (is_kvm_guest()) { 215 217 /* 216 218 * KVM emulates doorbells by disabling FSCR[MSGP] so msgsndp 217 219 * faults to the hypervisor which then reads the instruction