Fixes for KVM HV around synchronization and initializations
Subsystems (4)
v3 cover Fixes for KVM HV around synchronization and initializations
This series contains the fixes for synchronization and initializations bugs in the KVM HV code.
Patch 1: Fix for concurrent calls to idr_alloc
Patch 2: Initialize the arch.irq_type correctly for cleanup to work
Patch 3: In the cleanup function, add a check to avoid NULL ptr dereference.
Patch 4: Add a srcu lock to fix "suspicious usage of
rcu_dereference_check" usage.
v2 -> v3:
1. Patch 1: Correct the patch description to reflect the code changes.
2. Patch 3: Add stable and fixes tags
3. Patch 4: Use srcu_read_{unlock,lock} instead of their kvm_vcpu* variants.
v1 -> v2:
1. Patch 2 : Avoid NULL pointer dereference when calling kvmppc_xive_cleanup_vcpu(). Split this in a separate patch
Gautam Menghani (4):
KVM: PPC: Book3S HV: Ensure that calls to idr_alloc are synchronized
KVM: PPC: Book3S HV: XICS: Update irq_type for cleanup to work
KVM: PPC: Book3S HV: XICS: Avoid masking VP IPI on init failure
KVM: PPC: Book3s HV: Take SRCU read lock around kvmppc_h_page_init()
call
arch/powerpc/kvm/book3s_hv.c | 2 ++ arch/powerpc/kvm/book3s_hv_nested.c | 34 +++++++++-------------------- arch/powerpc/kvm/book3s_xive.c | 5 +++-- 3 files changed, 15 insertions(+), 26 deletions(-)
Signature · 1 line
2.54.0
PATCH v3 1/4 KVM: PPC: Book3S HV: Ensure that calls to idr_alloc are synchronized
Calls to idr_alloc() in __prealloc_nested() are not currently synchronized. According to the documentation [1], the caller should provide their own locking to prevent concurrent modifications to the idr to prevent bugs.
__prealloc_nested() calls idr_alloc() with GFP_KERNEL which can sleep and mmu_lock is a spinlock. So idr_alloc() cannot be moved under mmu_lock. In such cases the idr Documentation says:
If you need to take a lock while allocating a new ID from the IDR, you may need to pass a restrictive set of GFP flags, which can lead to the IDR being unable to allocate memory. To work around this, you can call idr_preload() before taking the lock, and then idr_preload_end() after the allocation.
Hence preload the idr memory with idr_preload() before taking mmu_lock, which allows us to use idr_alloc(..., GFP_NOWAIT) under the spinlock.
This also allows to kill __prealloc_nested() and __add_nested(), as they are not needed anymore with idr_preload().
[1]: lib/idr.c
---
v3:
1. Fix the patch description to accurately describe the code change. (Ritesh)
v2:
1. None
arch/powerpc/kvm/book3s_hv_nested.c | 34 +++++++++-------------------- 1 file changed, 10 insertions(+), 24 deletions(-)
arch/powerpc/kvm/book3s_hv_nested.c +10 -24
@@ -702,20 +702,6 @@ static struct kvm_nested_guest *__find_nested(struct kvm *kvm, int lpid)return idr_find (&kvm->arch.kvm_nested_guest_idr, lpid); } -static bool __prealloc_nested (struct kvm *kvm,int lpid) -{ -if (idr_alloc (&kvm->arch.kvm_nested_guest_idr, -NULL , lpid, lpid +1 ,GFP_KERNEL ) != lpid) -return false; -return true; -} - -static void __add_nested (struct kvm *kvm,int lpid,struct kvm_nested_guest *gp) -{ -if (idr_replace (&kvm->arch.kvm_nested_guest_idr, gp, lpid)) -WARN_ON (1 ); -} -static void __remove_nested (struct kvm *kvm,int lpid) {idr_remove (&kvm->arch.kvm_nested_guest_idr, lpid);
@@ -862,21 +848,21 @@ struct kvm_nested_guest *kvmhv_get_nested(struct kvm *kvm, int l1_lpid,if (!newgp)return NULL ; -if (!__prealloc_nested (kvm, l1_lpid)) { -kvmhv_release_nested (newgp); -return NULL ; - } - +idr_preload (GFP_KERNEL );spin_lock (&kvm->mmu_lock); gp =__find_nested (kvm, l1_lpid);if (!gp) { -__add_nested (kvm, l1_lpid, newgp); - ++newgp->refcnt; - gp = newgp; - newgp =NULL ; +if (idr_alloc (&kvm->arch.kvm_nested_guest_idr, newgp, + l1_lpid, l1_lpid +1 ,GFP_NOWAIT ) >=0 ) { + ++newgp->refcnt; + gp = newgp; + newgp =NULL ; + } } - ++gp->refcnt; +if (gp) + ++gp->refcnt;spin_unlock (&kvm->mmu_lock); +idr_preload_end ();if (newgp)kvmhv_release_nested (newgp);
Signature · 1 line
2.54.0
PATCH v3 2/4 KVM: PPC: Book3S HV: XICS: Update irq_type for cleanup to work
In reply to Gautam MenghaniIn kvmppc_xive_connect_vcpu(), if any failures are encountered, the control flow jumps to the 'bail' label where kvmppc_xive_cleanup_vcpu() is called. This cleanup function exits prematurely since vcpu->arch.irq_type is set to KVMPPC_IRQ_DEFAULT at this point. This ends up resulting in wasted memory.
Fix this by setting irq_type to KVMPPC_IRQ_XICS before vcpu configuration starts. This makes XICS initialization symmetrical to XIVE initialization done in kvmppc_xive_native_connect_vcpu().
---
v3:
None
v2:
None
arch/powerpc/kvm/book3s_xive.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
arch/powerpc/kvm/book3s_xive.c +1 -1
@@ -1937,6 +1937,7 @@ int kvmppc_xive_connect_vcpu(struct kvm_device *dev, xc->vp_id = vp_id; xc->mfrr =0xff ; xc->valid = true; + vcpu->arch.irq_type =KVMPPC_IRQ_XICS ; r =xive_native_get_vp_info (xc->vp_id, &xc->vp_cam, &xc->vp_chip_id);if (r)
@@ -2025,7 +2026,6 @@ int kvmppc_xive_connect_vcpu(struct kvm_device *dev,return r; } - vcpu->arch.irq_type =KVMPPC_IRQ_XICS ;return 0 ; }
Signature · 1 line
2.54.0
Re: [PATCH v3 2/4] KVM: PPC: Book3S HV: XICS: Update irq_type for cleanup to work
Quoted 6 lines · Gautam Menghani
In
kvmppc_xive_connect_vcpu(), if any failures are encountered, the control flow jumps to the 'bail' label wherekvmppc_xive_cleanup_vcpu()is called. This cleanup function exits prematurely sincevcpu->arch.irq_typeis set toKVMPPC_IRQ_DEFAULTat this point. This ends up resulting in wasted memory.
Quoted 7 lines
Fix this by setting
irq_typetoKVMPPC_IRQ_XICSbefore vcpu configuration starts. This makes XICS initialization symmetrical to XIVE initialization done inkvmppc_xive_native_connect_vcpu().Fixes 5af50993850a ("KVM: PPC: Book3S HV: Native usage of the XIVE interrupt controller")Cc [email protected] # 4.12+Signed-off-by Gautam Menghani <[email protected]>
Mainly fixes the error handling / cleanup routine in kvmppc_xive_connect_cpu(). The fix looks good to me, feel free to add:
PATCH v3 3/4 KVM: PPC: Book3S HV: XICS: Avoid NULL ptr deref in kvmppc_xive_cleanup_vcpu()
In reply to Gautam MenghaniIn kvmppc_xive_connect_vcpu() if there is a failure before xc->vp_ipi_data is setup, it can result in a NULL pointer dereference in kvmppc_xive_cleanup_vcpu() when accessing the MMIO page in xive_vm_esb_load(). Fix this by adding a check before masking the VP IPI before accessing the MMIO pages.
---
v3:
1. Add some more info to the patch description.
v2:
1. New patch - Introduced to fix the scenario where NULL ptr deref is possible. (Sashiko)
arch/powerpc/kvm/book3s_xive.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)
arch/powerpc/kvm/book3s_xive.c +2 -1
@@ -1818,7 +1818,8 @@ void kvmppc_xive_cleanup_vcpu(struct kvm_vcpu *vcpu)kvmppc_xive_disable_vcpu_interrupts (vcpu);/* Mask the VP IPI */ -xive_vm_esb_load (&xc->vp_ipi_data,XIVE_ESB_SET_PQ_01 ); +if (xc->vp_ipi_data.eoi_mmio) +xive_vm_esb_load (&xc->vp_ipi_data,XIVE_ESB_SET_PQ_01 );/* Free escalations */ for (i =0 ; i <KVMPPC_XIVE_Q_COUNT ; i++) {
Signature · 1 line
2.54.0
Re: [PATCH v3 3/4] KVM: PPC: Book3S HV: XICS: Avoid NULL ptr deref in kvmppc_xive_cleanup_vcpu()
Quoted 9 lines · Gautam Menghani
In
kvmppc_xive_connect_vcpu()if there is a failure beforexc->vp_ipi_datais setup, it can result in a NULL pointer dereference inkvmppc_xive_cleanup_vcpu()when accessing the MMIO page inxive_vm_esb_load(). Fix this by adding a check before masking the VP IPI before accessing the MMIO pages.Fixes 5af50993850a ("KVM: PPC: Book3S HV: Native usage of the XIVE interrupt controller")Cc [email protected] # 4.12+Signed-off-by Gautam Menghani <[email protected]>
Quoted 8 lines
---
v3:
1. Add some more info to the patch description.v2:
1. New patch - Introduced to fix the scenario where NULL ptr deref is possible. (Sashiko)
Make sense. However, the order of patch-2 and patch-3 should be reversed. Otherwise LGTM.
PATCH v3 4/4 KVM: PPC: Book3S HV: Take SRCU read lock around kvmppc_h_page_init() call
In reply to Gautam MenghaniSince there is no srcu read lock around kvmppc_h_page_init(), a "suspicious rcu_dereference_check() usage" can be triggered by running host linux with CONFIG_PROVE_RCU=y and the following code in the guest:
plpar_hcall_norets(H_PAGE_INIT, H_ZERO_PAGE, phys_addr, 0);
=============================
WARNING: suspicious RCU usage
7.0.0-dirty #227 Not tainted
-----------------------------./include/linux/kvm_host.h:1074 suspicious rcu_dereference_check() usage!
other info that might help us debug this:
rcu_scheduler_active = 2, debug_locks = 1
1 lock held by qemu-system-ppc/2093:
#0: c000000354197ab0 (&vcpu->mutex){+.+.}-{3:3}, at: kvm_vcpu_ioctl+0x10c/0xaf0
stack backtrace: CPU: 30 UID: 0 PID: 2093 Comm: qemu-system-ppc Not tainted 7.0.0-dirty #227 PREEMPT(full) Hardware name: IBM,9080-HEX POWER10 (architected) 0x800200 0xf000006 of:IBM,FW1060.60 (NH1060_158) hv:phyp pSeries Call Trace: [c00000033e1bf430] [c000000001b3dbd4] dump_stack_lvl+0xc8/0x130 (unreliable) [c00000033e1bf470] [c0000000003da964] lockdep_rcu_suspicious+0x1f4/0x290 [c00000033e1bf520] [c000000000231424] gfn_to_memslot+0x1b4/0x1c0 [c00000033e1bf560] [c00000000023637c] kvm_clear_guest+0x9c/0x110 [c00000033e1bf5c0] [c0000000002714a4] kvmppc_h_page_init+0x160/0x1a0 [c00000033e1bf610] [c00000000026c040] kvmppc_pseries_do_hcall+0x1930/0x1940 [c00000033e1bf6d0] [c00000000026fb18] kvmppc_vcpu_run_hv+0x268/0x800 [c00000033e1bf7a0] [c000000000246b30] kvmppc_vcpu_run+0x30/0x50 [c00000033e1bf7c0] [c000000000241ed4] kvm_arch_vcpu_ioctl_run+0x364/0x530 [c00000033e1bf860] [c00000000022b870] kvm_vcpu_ioctl+0x1b0/0xaf0 [c00000033e1bfa50] [c0000000009b4514] sys_ioctl+0x144/0x190 [c00000033e1bfab0] [c000000000034330] system_call_exception+0x170/0x370 [c00000033e1bfe50] [c00000000000d05c] system_call_vectored_common+0x15c/0x2ec
Fix this by taking a srcu read lock around kvmppc_h_page_init().
---
v3:
1. Use srcu_read_{lock/unlock} instead of the vcpu_srcu* variants (Ritesh)
v2:
None
arch/powerpc/kvm/book3s_hv.c | 2 ++ 1 file changed, 2 insertions(+)
arch/powerpc/kvm/book3s_hv.c +2 -0
@@ -1386,9 +1386,11 @@ int kvmppc_pseries_do_hcall(struct kvm_vcpu *vcpu) ret =kvmhv_copy_tofrom_guest_nested (vcpu);break ;case H_PAGE_INIT : + idx =srcu_read_lock (&kvm->srcu); ret =kvmppc_h_page_init (vcpu,kvmppc_get_gpr (vcpu,4 ),kvmppc_get_gpr (vcpu,5 ),kvmppc_get_gpr (vcpu,6 )); +srcu_read_unlock (&kvm->srcu, idx);break ;case H_SVM_PAGE_IN : ret =H_UNSUPPORTED ;
Signature · 1 line
2.54.0
Re: [PATCH v3 4/4] KVM: PPC: Book3S HV: Take SRCU read lock around kvmppc_h_page_init() call
Quoted 36 lines · Gautam Menghani
Since there is no srcu read lock around
kvmppc_h_page_init(), a "suspiciousrcu_dereference_check()usage" can be triggered by running host linux withCONFIG_PROVE_RCU=yand the following code in the guest:
plpar_hcall_norets(H_PAGE_INIT, H_ZERO_PAGE, phys_addr, 0);=============================
WARNING: suspicious RCU usage
7.0.0-dirty #227 Not tainted
-----------------------------./include/linux/kvm_host.h:1074suspiciousrcu_dereference_check()usage!other info that might help us debug this:
rcu_scheduler_active= 2,debug_locks= 1
1 lock held by qemu-system-ppc/2093:
#0:c000000354197ab0(&vcpu->mutex){+.+.}-{3:3}, at:kvm_vcpu_ioctl+0x10c/0xaf0stack backtrace: CPU: 30 UID: 0 PID: 2093 Comm: qemu-system-ppc Not tainted 7.0.0-dirty #227 PREEMPT(full) Hardware name: IBM,9080-HEX POWER10 (architected) 0x800200 0xf000006 of:IBM,FW1060.60 (NH1060_158) hv:phyp pSeries Call Trace: [c00000033e1bf430] [c000000001b3dbd4] dump_stack_lvl+0xc8/0x130 (unreliable) [c00000033e1bf470] [c0000000003da964] lockdep_rcu_suspicious+0x1f4/0x290 [c00000033e1bf520] [c000000000231424] gfn_to_memslot+0x1b4/0x1c0 [c00000033e1bf560] [c00000000023637c] kvm_clear_guest+0x9c/0x110 [c00000033e1bf5c0] [c0000000002714a4] kvmppc_h_page_init+0x160/0x1a0 [c00000033e1bf610] [c00000000026c040] kvmppc_pseries_do_hcall+0x1930/0x1940 [c00000033e1bf6d0] [c00000000026fb18] kvmppc_vcpu_run_hv+0x268/0x800 [c00000033e1bf7a0] [c000000000246b30] kvmppc_vcpu_run+0x30/0x50 [c00000033e1bf7c0] [c000000000241ed4] kvm_arch_vcpu_ioctl_run+0x364/0x530 [c00000033e1bf860] [c00000000022b870] kvm_vcpu_ioctl+0x1b0/0xaf0 [c00000033e1bfa50] [c0000000009b4514] sys_ioctl+0x144/0x190 [c00000033e1bfab0] [c000000000034330] system_call_exception+0x170/0x370 [c00000033e1bfe50] [c00000000000d05c] system_call_vectored_common+0x15c/0x2ec
Quoted 1 line
Fix this by taking a srcu read lock around
kvmppc_h_page_init().
Looks good to me. Please feel free to add:
Quoted 8 lines · Gautam Menghani
This series contains the fixes for synchronization and initializations bugs in the KVM HV code.
Patch 1: Fix for concurrent calls to
idr_allocPatch 2: Initialize the arch.irq_type correctly for cleanup to work
[...]
Applied to powerpc/fixes.
[1/4] KVM: PPC: Book3S HV: Ensure that calls to idr_alloc are synchronized
https://git.kernel.org/powerpc/c/d8c6ab0aeeca7cc2adfcf0ce1f796c6a86314ec0 [2/4] KVM: PPC: Book3S HV: XICS: Update irq_type for cleanup to work https://git.kernel.org/powerpc/c/b9d9a8741752c5b42c65b1c8dffba867889b74e5 [3/4] KVM: PPC: Book3S HV: XICS: Avoid NULL ptr deref in kvmppc_xive_cleanup_vcpu() https://git.kernel.org/powerpc/c/7279692d70f3e399b3dd2d07ffa365e9f533761f [4/4] KVM: PPC: Book3S HV: Take SRCU read lock around kvmppc_h_page_init() call https://git.kernel.org/powerpc/c/b04be22bf6ea06b92bff7850328a8057b8d964ae
cheers