KVM: arm64: vgic-v3: Fix yet another lock ordering turd
cover KVM: arm64: vgic-v3: Fix yet another lock ordering turd
syzkaller found yet another locking bug in the VGIC [*], this time due to nesting a 'plain' spinlock (xa_lock) inside of a raw spinlock (ap_list_lock). Given the way we do refcounts on LPIs it is possible for this exact sort of issue to crop up where the last reference may be dropped in unexpected places.
Small series to fix the issue by deferring xarray modifications outside of the ap_list_lock critical section along with some slight lockdep hinting to make these rare bugs a bit more obvious.
Applies to 6.17-rc4.
Oliver Upton (5):
KVM: arm64: vgic-v3: Use bare refcount for VGIC LPIs
KVM: arm64: Spin off release helper from vgic_put_irq()
KVM: arm64: vgic-v3: Erase LPIs from xarray outside of raw spinlocks
KVM: arm64: vgic-v3: Don't require IRQs be disabled for LPI xarray
lock
KVM: arm64: vgic-v3: Indicate vgic_put_irq() may take LPI xarray lock
arch/arm64/kvm/vgic/vgic-debug.c | 2 +- arch/arm64/kvm/vgic/vgic-init.c | 6 +-- arch/arm64/kvm/vgic/vgic-its.c | 15 +++--- arch/arm64/kvm/vgic/vgic-v4.c | 2 +- arch/arm64/kvm/vgic/vgic.c | 78 +++++++++++++++++++++++--------- arch/arm64/kvm/vgic/vgic.h | 8 ++-- include/kvm/arm_vgic.h | 10 ++-- 7 files changed, 80 insertions(+), 41 deletions(-)
base-commit: b320789d6883cc00ac78ce83bccbfe7ed58afcf0
Signature · 1 line
2.39.5
PATCH 1/5 KVM: arm64: vgic-v3: Use bare refcount for VGIC LPIs
KVM's use of krefs to manage LPIs isn't adding much, especially considering vgic_irq_release() is a noop due to the lack of sufficient context.
Switch to using a regular refcount in anticipation of adding a meaningful release concept for LPIs.
--- arch/arm64/kvm/vgic/vgic-debug.c | 2 +- arch/arm64/kvm/vgic/vgic-init.c | 4 ++-- arch/arm64/kvm/vgic/vgic-its.c | 8 ++++---- arch/arm64/kvm/vgic/vgic-v4.c | 2 +- arch/arm64/kvm/vgic/vgic.c | 17 ++++------------- arch/arm64/kvm/vgic/vgic.h | 8 ++++---- include/kvm/arm_vgic.h | 4 ++-- 7 files changed, 18 insertions(+), 27 deletions(-)
arch/arm64/kvm/vgic/vgic-debug.c +1 -1
@@ -69,7 +69,7 @@ static int iter_mark_lpis(struct kvm *kvm)int nr_lpis =0 ;xa_for_each (&dist->lpi_xa, intid, irq) { -if (!vgic_try_get_irq_kref (irq)) +if (!vgic_try_get_irq_ref (irq))continue ;xa_set_mark (&dist->lpi_xa, intid,LPI_XA_MARK_DEBUG_ITER );
arch/arm64/kvm/vgic/vgic-init.c +2 -2
@@ -208,7 +208,7 @@ static int kvm_vgic_dist_init(struct kvm *kvm, unsigned int nr_spis)raw_spin_lock_init (&irq->irq_lock); irq->vcpu =NULL ; irq->target_vcpu = vcpu0; -kref_init (&irq->refcount); +refcount_set (&irq->refcount,0 );switch (dist->vgic_model) {case KVM_DEV_TYPE_ARM_VGIC_V2 : irq->targets =0 ;
@@ -277,7 +277,7 @@ static int vgic_allocate_private_irqs_locked(struct kvm_vcpu *vcpu, u32 type) irq->intid = i; irq->vcpu =NULL ; irq->target_vcpu = vcpu; -kref_init (&irq->refcount); +refcount_set (&irq->refcount,0 );if (vgic_irq_is_sgi (i)) {/* SGIs */ irq->enabled =1 ;
arch/arm64/kvm/vgic/vgic-its.c +4 -4
@@ -99,7 +99,7 @@ static struct vgic_irq *vgic_add_lpi(struct kvm *kvm, u32 intid,raw_spin_lock_init (&irq->irq_lock); irq->config =VGIC_CONFIG_EDGE ; -kref_init (&irq->refcount); +refcount_set (&irq->refcount,1 ); irq->intid = intid; irq->target_vcpu = vcpu; irq->group =1 ;
@@ -111,7 +111,7 @@ static struct vgic_irq *vgic_add_lpi(struct kvm *kvm, u32 intid,* check that we don't add a second list entry with the same LPI. */ oldirq =xa_load (&dist->lpi_xa, intid); -if (vgic_try_get_irq_kref (oldirq)) { +if (vgic_try_get_irq_ref (oldirq)) {/* Someone was faster with adding this LPI, lets use that. */ kfree (irq); irq = oldirq;
@@ -547,7 +547,7 @@ static struct vgic_irq *vgic_its_check_cache(struct kvm *kvm, phys_addr_t db,rcu_read_lock (); irq =xa_load (&its->translation_cache, cache_key); -if (!vgic_try_get_irq_kref (irq)) +if (!vgic_try_get_irq_ref (irq)) irq =NULL ;rcu_read_unlock ();
@@ -571,7 +571,7 @@ static void vgic_its_cache_translation(struct kvm *kvm, struct vgic_its *its,* its_lock, as the ITE (and the reference it holds) cannot be freed. */ lockdep_assert_held (&its->its_lock); -vgic_get_irq_kref (irq); +vgic_get_irq_ref (irq); old =xa_store (&its->translation_cache, cache_key, irq,GFP_KERNEL_ACCOUNT );
arch/arm64/kvm/vgic/vgic-v4.c +1 -1
@@ -518,7 +518,7 @@ static struct vgic_irq *__vgic_host_irq_get_vlpi(struct kvm *kvm, int host_irq)if (!irq->hw || irq->host_irq != host_irq)continue ; -if (!vgic_try_get_irq_kref (irq)) +if (!vgic_try_get_irq_ref (irq))return NULL ;return irq;
arch/arm64/kvm/vgic/vgic.c +4 -13
@@ -71,7 +71,7 @@ static struct vgic_irq *vgic_get_lpi(struct kvm *kvm, u32 intid)rcu_read_lock (); irq =xa_load (&dist->lpi_xa, intid); -if (!vgic_try_get_irq_kref (irq)) +if (!vgic_try_get_irq_ref (irq)) irq =NULL ;rcu_read_unlock ();
@@ -114,15 +114,6 @@ struct vgic_irq *vgic_get_vcpu_irq(struct kvm_vcpu *vcpu, u32 intid)return vgic_get_irq (vcpu->kvm, intid); } -/* -* We can't do anything in here, because we lack the kvm pointer to -* lock and remove the item from the lpi_list. So we keep this function -* empty and use the return value of kref_put() to trigger the freeing. -*/ -static void vgic_irq_release (struct kref *ref) -{ -} -void vgic_put_irq (struct kvm *kvm,struct vgic_irq *irq) {struct vgic_dist *dist = &kvm->arch.vgic;
@@ -131,7 +122,7 @@ void vgic_put_irq(struct kvm *kvm, struct vgic_irq *irq)if (irq->intid <VGIC_MIN_LPI )return ; -if (!kref_put (&irq->refcount, vgic_irq_release)) +if (!refcount_dec_and_test (&irq->refcount))return ;xa_lock_irqsave (&dist->lpi_xa, flags);
@@ -399,7 +390,7 @@ bool vgic_queue_irq_unlock(struct kvm *kvm, struct vgic_irq *irq,* now in the ap_list. This is safe as the caller must already hold a * reference on the irq. */ -vgic_get_irq_kref (irq); +vgic_get_irq_ref (irq);list_add_tail (&irq->ap_list, &vcpu->arch.vgic_cpu.ap_list_head); irq->vcpu = vcpu;
@@ -657,7 +648,7 @@ static void vgic_prune_ap_list(struct kvm_vcpu *vcpu)/* * This vgic_put_irq call matches the -* vgic_get_irq_kref in vgic_queue_irq_unlock, +* vgic_get_irq_ref in vgic_queue_irq_unlock, * where we added the LPI to the ap_list. As * we remove the irq from the list, we drop * also drop the refcount.
arch/arm64/kvm/vgic/vgic.h +4 -4
@@ -267,7 +267,7 @@ void vgic_v2_put(struct kvm_vcpu *vcpu);void vgic_v2_save_state (struct kvm_vcpu *vcpu);void vgic_v2_restore_state (struct kvm_vcpu *vcpu); -static inline bool vgic_try_get_irq_kref (struct vgic_irq *irq) +static inline bool vgic_try_get_irq_ref (struct vgic_irq *irq) {if (!irq)return false;
@@ -275,12 +275,12 @@ static inline bool vgic_try_get_irq_kref(struct vgic_irq *irq)if (irq->intid <VGIC_MIN_LPI )return true; -return kref_get_unless_zero (&irq->refcount); +return refcount_inc_not_zero (&irq->refcount); } -static inline void vgic_get_irq_kref (struct vgic_irq *irq) +static inline void vgic_get_irq_ref (struct vgic_irq *irq) { -WARN_ON_ONCE (!vgic_try_get_irq_kref (irq)); +WARN_ON_ONCE (!vgic_try_get_irq_ref (irq)); }void vgic_v3_fold_lr_state (struct kvm_vcpu *vcpu);
include/kvm/arm_vgic.h +2 -2
@@ -8,8 +8,8 @@#include <linux/bits.h> #include <linux/kvm.h> #include <linux/irqreturn.h> -#include <linux/kref.h> #include <linux/mutex.h> +#include <linux/refcount.h> #include <linux/spinlock.h> #include <linux/static_key.h> #include <linux/types.h>
@@ -142,7 +142,7 @@ struct vgic_irq {bool active;/* not used for LPIs */ bool enabled;bool hw;/* Tied to HW IRQ */ -struct kref refcount;/* Used for LPIs */ +refcount_t refcount;/* Used for LPIs */ u32 hwintid;/* HW INTID number */ unsigned int host_irq;/* linux irq corresponding to hwintid */ union {
Signature · 1 line
2.39.5
PATCH 2/5 KVM: arm64: Spin off release helper from vgic_put_irq()
In reply to Oliver UptonSpin off the release implementation from vgic_put_irq() to prepare for a more involved fix for lock ordering such that it may be unnested from raw spinlocks. This has the minor functional change of doing call_rcu() behind the xa_lock although it shouldn't be consequential.
--- arch/arm64/kvm/vgic/vgic.c | 24 +++++++++++++++++------- 1 file changed, 17 insertions(+), 7 deletions(-)
arch/arm64/kvm/vgic/vgic.c +17 -7
@@ -114,22 +114,32 @@ struct vgic_irq *vgic_get_vcpu_irq(struct kvm_vcpu *vcpu, u32 intid)return vgic_get_irq (vcpu->kvm, intid); } +static void vgic_release_lpi_locked (struct vgic_dist *dist,struct vgic_irq *irq) +{ +lockdep_assert_held (&dist->lpi_xa.xa_lock); +__xa_erase (&dist->lpi_xa, irq->intid); +kfree_rcu (irq, rcu); +} + +static __must_check bool __vgic_put_irq (struct kvm *kvm,struct vgic_irq *irq) +{ +if (irq->intid <VGIC_MIN_LPI ) +return false; + +return refcount_dec_and_test (&irq->refcount); +} +void vgic_put_irq (struct kvm *kvm,struct vgic_irq *irq) {struct vgic_dist *dist = &kvm->arch.vgic;unsigned long flags; -if (irq->intid <VGIC_MIN_LPI ) -return ; - -if (!refcount_dec_and_test (&irq->refcount)) +if (!__vgic_put_irq (kvm, irq))return ;xa_lock_irqsave (&dist->lpi_xa, flags); -__xa_erase (&dist->lpi_xa, irq->intid); +vgic_release_lpi_locked (dist, irq);xa_unlock_irqrestore (&dist->lpi_xa, flags); - -kfree_rcu (irq, rcu); }void vgic_flush_pending_lpis (struct kvm_vcpu *vcpu)
Signature · 1 line
2.39.5
PATCH 3/5 KVM: arm64: vgic-v3: Erase LPIs from xarray outside of raw spinlocks
In reply to Oliver Uptonsyzkaller has caught us red-handed once more, this time nesting regular spinlocks behind raw spinlocks:
=============================
[ BUG: Invalid wait context ]
6.16.0-rc3-syzkaller-g7b8346bd9fce #0 Not tainted
-----------------------------
syz.0.29/3743 is trying to lock:
a3ff80008e2e9e18 (&xa->xa_lock#20){....}-{3:3}, at: vgic_put_irq+0xb4/0x190 arch/arm64/kvm/vgic/vgic.c:137 other info that might help us debug this:
context-{5:5}
3 locks held by syz.0.29/3743:
#0: a3ff80008e2e90a8 (&kvm->slots_lock){+.+.}-{4:4}, at: kvm_vgic_destroy+0x50/0x624 arch/arm64/kvm/vgic/vgic-init.c:499 #1: a3ff80008e2e9fa0 (&kvm->arch.config_lock){+.+.}-{4:4}, at: kvm_vgic_destroy+0x5c/0x624 arch/arm64/kvm/vgic/vgic-init.c:500 #2: 58f0000021be1428 (&vgic_cpu->ap_list_lock){....}-{2:2}, at: vgic_flush_pending_lpis+0x3c/0x31c arch/arm64/kvm/vgic/vgic.c:150
stack backtrace: CPU: 0 UID: 0 PID: 3743 Comm: syz.0.29 Not tainted 6.16.0-rc3-syzkaller-g7b8346bd9fce #0 PREEMPT Hardware name: linux,dummy-virt (DT) Call trace: show_stack+0x2c/0x3c arch/arm64/kernel/stacktrace.c:466 (C) __dump_stack+0x30/0x40 lib/dump_stack.c:94 dump_stack_lvl+0xd8/0x12c lib/dump_stack.c:120 dump_stack+0x1c/0x28 lib/dump_stack.c:129 print_lock_invalid_wait_context kernel/locking/lockdep.c:4833 [inline] check_wait_context kernel/locking/lockdep.c:4905 [inline] __lock_acquire+0x978/0x299c kernel/locking/lockdep.c:5190 lock_acquire+0x14c/0x2e0 kernel/locking/lockdep.c:5871 __raw_spin_lock_irqsave include/linux/spinlock_api_smp.h:110 [inline] _raw_spin_lock_irqsave+0x5c/0x7c kernel/locking/spinlock.c:162 vgic_put_irq+0xb4/0x190 arch/arm64/kvm/vgic/vgic.c:137 vgic_flush_pending_lpis+0x24c/0x31c arch/arm64/kvm/vgic/vgic.c:158 __kvm_vgic_vcpu_destroy+0x44/0x500 arch/arm64/kvm/vgic/vgic-init.c:455 kvm_vgic_destroy+0x100/0x624 arch/arm64/kvm/vgic/vgic-init.c:505 kvm_arch_destroy_vm+0x80/0x138 arch/arm64/kvm/arm.c:244 kvm_destroy_vm virt/kvm/kvm_main.c:1308 [inline] kvm_put_kvm+0x800/0xff8 virt/kvm/kvm_main.c:1344 kvm_vm_release+0x58/0x78 virt/kvm/kvm_main.c:1367 __fput+0x4ac/0x980 fs/file_table.c:465 ____fput+0x20/0x58 fs/file_table.c:493 task_work_run+0x1bc/0x254 kernel/task_work.c:227 resume_user_mode_work include/linux/resume_user_mode.h:50 [inline] do_notify_resume+0x1b4/0x270 arch/arm64/kernel/entry-common.c:151 exit_to_user_mode_prepare arch/arm64/kernel/entry-common.c:169 [inline] exit_to_user_mode arch/arm64/kernel/entry-common.c:178 [inline] el0_svc+0xb4/0x160 arch/arm64/kernel/entry-common.c:768 el0t_64_sync_handler+0x78/0x108 arch/arm64/kernel/entry-common.c:786 el0t_64_sync+0x198/0x19c arch/arm64/kernel/entry.S:600
This is of course no good, but is at odds with how LPI refcounts are managed. Solve the locking mess by deferring the release of unreferenced LPIs after the ap_list_lock is released. Mark these to-be-released LPIs specially to avoid racing with vgic_put_irq() and causing a double-free.
Since references can only be taken on LPIs with a nonzero refcount, extending the lifetime of freed LPIs is still safe.
--- arch/arm64/kvm/vgic/vgic.c | 41 ++++++++++++++++++++++++++++++++++---- include/kvm/arm_vgic.h | 6 +++++- 2 files changed, 42 insertions(+), 5 deletions(-)
arch/arm64/kvm/vgic/vgic.c +37 -4
@@ -28,8 +28,8 @@ struct vgic_global kvm_vgic_global_state __ro_after_init = {* kvm->arch.config_lock (mutex) * its->cmd_lock (mutex) * its->its_lock (mutex) -* vgic_cpu->ap_list_lock must be taken with IRQs disabled -* vgic_dist->lpi_xa.xa_lock must be taken with IRQs disabled +* vgic_dist->lpi_xa.xa_lock must be taken with IRQs disabled +* vgic_cpu->ap_list_lock must be taken with IRQs disabled * vgic_irq->irq_lock must be taken with IRQs disabled * * As the ap_list_lock might be taken from the timer interrupt handler,
@@ -129,6 +129,15 @@ static __must_check bool __vgic_put_irq(struct kvm *kvm, struct vgic_irq *irq)return refcount_dec_and_test (&irq->refcount); } +static __must_check bool vgic_put_irq_norelease (struct kvm *kvm,struct vgic_irq *irq) +{ +if (!__vgic_put_irq (kvm, irq)) +return false; + + irq->pending_release = true; +return true; +} +void vgic_put_irq (struct kvm *kvm,struct vgic_irq *irq) {struct vgic_dist *dist = &kvm->arch.vgic;
@@ -142,10 +151,27 @@ void vgic_put_irq(struct kvm *kvm, struct vgic_irq *irq)xa_unlock_irqrestore (&dist->lpi_xa, flags); } +static void vgic_release_deleted_lpis (struct kvm *kvm) +{ +struct vgic_dist *dist = &kvm->arch.vgic; +unsigned long flags, intid; +struct vgic_irq *irq; + +xa_lock_irqsave (&dist->lpi_xa, flags); + +xa_for_each (&dist->lpi_xa, intid, irq) { +if (irq->pending_release) +vgic_release_lpi_locked (dist, irq); + } + +xa_unlock_irqrestore (&dist->lpi_xa, flags); +} +void vgic_flush_pending_lpis (struct kvm_vcpu *vcpu) {struct vgic_cpu *vgic_cpu = &vcpu->arch.vgic_cpu;struct vgic_irq *irq, *tmp; +bool deleted = false;unsigned long flags;raw_spin_lock_irqsave (&vgic_cpu->ap_list_lock, flags);
@@ -156,11 +182,14 @@ void vgic_flush_pending_lpis(struct kvm_vcpu *vcpu)list_del (&irq->ap_list); irq->vcpu =NULL ;raw_spin_unlock (&irq->irq_lock); -vgic_put_irq (vcpu->kvm, irq); + deleted |=vgic_put_irq_norelease (vcpu->kvm, irq); } }raw_spin_unlock_irqrestore (&vgic_cpu->ap_list_lock, flags); + +if (deleted) +vgic_release_deleted_lpis (vcpu->kvm); }void vgic_irq_set_phys_pending (struct vgic_irq *irq,bool pending)
@@ -631,6 +660,7 @@ static void vgic_prune_ap_list(struct kvm_vcpu *vcpu) {struct vgic_cpu *vgic_cpu = &vcpu->arch.vgic_cpu;struct vgic_irq *irq, *tmp; +bool deleted_lpis = false;DEBUG_SPINLOCK_BUG_ON (!irqs_disabled ());
@@ -663,7 +693,7 @@ static void vgic_prune_ap_list(struct kvm_vcpu *vcpu)* we remove the irq from the list, we drop * also drop the refcount. */ -vgic_put_irq (vcpu->kvm, irq); + deleted_lpis |=vgic_put_irq_norelease (vcpu->kvm, irq);continue ; }
@@ -726,6 +756,9 @@ static void vgic_prune_ap_list(struct kvm_vcpu *vcpu) }raw_spin_unlock (&vgic_cpu->ap_list_lock); + +if (unlikely (deleted_lpis)) +vgic_release_deleted_lpis (vcpu->kvm); }static inline void vgic_fold_lr_state (struct kvm_vcpu *vcpu)
include/kvm/arm_vgic.h +5 -1
@@ -139,7 +139,11 @@ struct vgic_irq {bool pending_latch;/* The pending latch state used to calculate * the pending state for both level * and edge triggered IRQs. */ -bool active;/* not used for LPIs */ +union { +bool active;/* not used for LPIs */ +bool pending_release;/* LPI pending a release */ + }; +bool enabled;bool hw;/* Tied to HW IRQ */ refcount_t refcount;/* Used for LPIs */
Signature · 1 line
2.39.5
Marc Zyngier
maintainerRe: [PATCH 3/5] KVM: arm64: vgic-v3: Erase LPIs from xarray outside of raw spinlocks
Quoted 6 lines · Oliver Upton
include/kvm/arm_vgic.h +0
-0
include/kvm/arm_vgic.h struct vgic_irq { · line 146
Quoted 9 lines
include/kvm/arm_vgic.h +5
-1@@ -139,7 +139,11 @@ struct vgic_irq {bool pending_latch;/* The pending latch state used to calculate * the pending state for both level * and edge triggered IRQs. */ -bool active;/* not used for LPIs */ +union { +bool active;/* not used for LPIs */ +bool pending_release;/* LPI pending a release */ + }; +
Err... no. Please don't do that. An activated LPI that hasn't been EOI'd yet does have the active state in the LR (yes, the original comment is totally broken, please remove it).
Imagine a case where you'd preempt the guest between the read of IAR and the write to EOI: pending_release is now set, and that could result in some funky stuff...
Just add it as a separate field, I don't think anyone will cry over that.
M.
Signature · 1 line
Jazz isn't dead. It just smells funny.
Re: [PATCH 3/5] KVM: arm64: vgic-v3: Erase LPIs from xarray outside of raw spinlocks
Quoted 13 lines · Marc Zyngier
Quoted 11 lines · Oliver Upton
include/kvm/arm_vgic.h +1
-1@@ -139,7 +139,11 @@ struct vgic_irq {bool pending_latch;/* The pending latch state used to calculate * the pending state for both level * and edge triggered IRQs. */ -bool active;/* not used for LPIs */ +union {
Quoted 8 lines
include/kvm/arm_vgic.h struct vgic_irq { · line 146
Quoted 4 lines
include/kvm/arm_vgic.h +4
-0+bool active;/* not used for LPIs */ +bool pending_release;/* LPI pending a release */ + }; +Err... no. Please don't do that. An activated LPI that hasn't been EOI'd yet does have the active state in the LR (yes, the original comment is totally broken, please remove it).
Yeah, this obviously wasn't thought about too carefully. I'll squash in a fix unless there's something more ugly that needs addressing.
Thanks,
Oliver
PATCH 4/5 KVM: arm64: vgic-v3: Don't require IRQs be disabled for LPI xarray lock
In reply to Oliver UptonNow that the LPI xarray is only read with IRQs disabled, relax the locking on the xarray to a non-IRQ disabling spinlock.
--- arch/arm64/kvm/vgic/vgic-init.c | 2 +- arch/arm64/kvm/vgic/vgic-its.c | 7 +++---- arch/arm64/kvm/vgic/vgic.c | 13 ++++++------- 3 files changed, 10 insertions(+), 12 deletions(-)
arch/arm64/kvm/vgic/vgic-init.c +1 -1
@@ -53,7 +53,7 @@ void kvm_vgic_early_init(struct kvm *kvm) {struct vgic_dist *dist = &kvm->arch.vgic; -xa_init_flags (&dist->lpi_xa,XA_FLAGS_LOCK_IRQ ); +xa_init (&dist->lpi_xa); }/* CREATION */
arch/arm64/kvm/vgic/vgic-its.c +3 -4
@@ -78,7 +78,6 @@ static struct vgic_irq *vgic_add_lpi(struct kvm *kvm, u32 intid, {struct vgic_dist *dist = &kvm->arch.vgic;struct vgic_irq *irq =vgic_get_irq (kvm, intid), *oldirq; -unsigned long flags;int ret;/* In this case there is no put, since we keep the reference. */
@@ -89,7 +88,7 @@ static struct vgic_irq *vgic_add_lpi(struct kvm *kvm, u32 intid,if (!irq)return ERR_PTR (-ENOMEM ); - ret =xa_reserve_irq (&dist->lpi_xa, intid,GFP_KERNEL_ACCOUNT ); + ret =xa_reserve (&dist->lpi_xa, intid,GFP_KERNEL_ACCOUNT );if (ret) {kfree (irq);return ERR_PTR (ret);
@@ -104,7 +103,7 @@ static struct vgic_irq *vgic_add_lpi(struct kvm *kvm, u32 intid, irq->target_vcpu = vcpu; irq->group =1 ; -xa_lock_irqsave (&dist->lpi_xa, flags); +xa_lock (&dist->lpi_xa);/* * There could be a race with another vgic_add_lpi(), so we need to
@@ -126,7 +125,7 @@ static struct vgic_irq *vgic_add_lpi(struct kvm *kvm, u32 intid, } out_unlock: -xa_unlock_irqrestore (&dist->lpi_xa, flags); +xa_unlock (&dist->lpi_xa);if (ret)return ERR_PTR (ret);
arch/arm64/kvm/vgic/vgic.c +6 -7
@@ -28,7 +28,7 @@ struct vgic_global kvm_vgic_global_state __ro_after_init = {* kvm->arch.config_lock (mutex) * its->cmd_lock (mutex) * its->its_lock (mutex) -* vgic_dist->lpi_xa.xa_lock must be taken with IRQs disabled +* vgic_dist->lpi_xa.xa_lock * vgic_cpu->ap_list_lock must be taken with IRQs disabled * vgic_irq->irq_lock must be taken with IRQs disabled *
@@ -141,30 +141,29 @@ static __must_check bool vgic_put_irq_norelease(struct kvm *kvm, struct vgic_irqvoid vgic_put_irq (struct kvm *kvm,struct vgic_irq *irq) {struct vgic_dist *dist = &kvm->arch.vgic; -unsigned long flags;if (!__vgic_put_irq (kvm, irq))return ; -xa_lock_irqsave (&dist->lpi_xa, flags); +xa_lock (&dist->lpi_xa);vgic_release_lpi_locked (dist, irq); -xa_unlock_irqrestore (&dist->lpi_xa, flags); +xa_unlock (&dist->lpi_xa); }static void vgic_release_deleted_lpis (struct kvm *kvm) {struct vgic_dist *dist = &kvm->arch.vgic; -unsigned long flags, intid; +unsigned long intid;struct vgic_irq *irq; -xa_lock_irqsave (&dist->lpi_xa, flags); +xa_lock (&dist->lpi_xa);xa_for_each (&dist->lpi_xa, intid, irq) {if (irq->pending_release)vgic_release_lpi_locked (dist, irq); } -xa_unlock_irqrestore (&dist->lpi_xa, flags); +xa_unlock (&dist->lpi_xa); }void vgic_flush_pending_lpis (struct kvm_vcpu *vcpu)
Signature · 1 line
2.39.5
Marc Zyngier
maintainerRe: [PATCH 4/5] KVM: arm64: vgic-v3: Don't require IRQs be disabled for LPI xarray lock
Quoted 68 lines · Oliver Upton
Now that the LPI xarray is only read with IRQs disabled, relax the locking on the xarray to a non-IRQ disabling spinlock.
Signed-off-by Oliver Upton <[email protected]>--- arch/arm64/kvm/vgic/vgic-init.c | 2 +- arch/arm64/kvm/vgic/vgic-its.c | 7 +++---- arch/arm64/kvm/vgic/vgic.c | 13 ++++++------- 3 files changed, 10 insertions(+), 12 deletions(-)arch/arm64/kvm/vgic/vgic-init.c +1
-1@@ -53,7 +53,7 @@ void kvm_vgic_early_init(struct kvm *kvm) {struct vgic_dist *dist = &kvm->arch.vgic; -xa_init_flags (&dist->lpi_xa,XA_FLAGS_LOCK_IRQ ); +xa_init (&dist->lpi_xa); }/* CREATION */ arch/arm64/kvm/vgic/vgic-its.c +3
-4@@ -78,7 +78,6 @@ static struct vgic_irq *vgic_add_lpi(struct kvm *kvm, u32 intid, {struct vgic_dist *dist = &kvm->arch.vgic;struct vgic_irq *irq =vgic_get_irq (kvm, intid), *oldirq; -unsigned long flags;int ret;/* In this case there is no put, since we keep the reference. */ @@ -89,7 +88,7 @@ static struct vgic_irq *vgic_add_lpi(struct kvm *kvm, u32 intid,if (!irq)return ERR_PTR (-ENOMEM ); - ret =xa_reserve_irq (&dist->lpi_xa, intid,GFP_KERNEL_ACCOUNT ); + ret =xa_reserve (&dist->lpi_xa, intid,GFP_KERNEL_ACCOUNT );if (ret) {kfree (irq);return ERR_PTR (ret);@@ -104,7 +103,7 @@ static struct vgic_irq *vgic_add_lpi(struct kvm *kvm, u32 intid, irq->target_vcpu = vcpu; irq->group =1 ; -xa_lock_irqsave (&dist->lpi_xa, flags); +xa_lock (&dist->lpi_xa);/* * There could be a race with another vgic_add_lpi(), so we need to @@ -126,7 +125,7 @@ static struct vgic_irq *vgic_add_lpi(struct kvm *kvm, u32 intid, } out_unlock: -xa_unlock_irqrestore (&dist->lpi_xa, flags); +xa_unlock (&dist->lpi_xa);if (ret)return ERR_PTR (ret);arch/arm64/kvm/vgic/vgic.c +0
-0
arch/arm64/kvm/vgic/vgic.c struct vgic_global kvm_vgic_global_state __ro_after_init = { · line 31
Quoted 5 lines
arch/arm64/kvm/vgic/vgic.c +1
-1@@ -28,7 +28,7 @@ struct vgic_global kvm_vgic_global_state __ro_after_init = {* kvm->arch.config_lock (mutex) * its->cmd_lock (mutex) * its->its_lock (mutex) -* vgic_dist->lpi_xa.xa_lock must be taken with IRQs disabled +* vgic_dist->lpi_xa.xa_lock
Doesn't this contradict the commit message statement that we always take this lock with interrupts disabled?
Thanks,
M.
Signature · 1 line
Jazz isn't dead. It just smells funny.
Re: [PATCH 4/5] KVM: arm64: vgic-v3: Don't require IRQs be disabled for LPI xarray lock
Quoted 5 lines · Marc Zyngier
Quoted 5 lines
arch/arm64/kvm/vgic/vgic.c +0
-0@@ -28,7 +28,7 @@ struct vgic_global kvm_vgic_global_state __ro_after_init = {
Quoted 8 lines
arch/arm64/kvm/vgic/vgic.c struct vgic_global kvm_vgic_global_state __ro_after_init = { · line 31
Quoted 5 lines
arch/arm64/kvm/vgic/vgic.c +1
-1* kvm->arch.config_lock (mutex) * its->cmd_lock (mutex) * its->its_lock (mutex) -* vgic_dist->lpi_xa.xa_lock must be taken with IRQs disabled +* vgic_dist->lpi_xa.xa_lock Doesn't this contradict the commit message statement that we always take this lock with interrupts disabled?
[ relaying offline conversation ]
The changelog sucks hard on this one, what I was trying to say is there are no longer any writers that acquire the xa_lock with IRQs disabled. What remains is readers that have interrupts disabled, although readers are RCU-protected and don't take the lock.
Thanks,
Oliver
PATCH 5/5 KVM: arm64: vgic-v3: Indicate vgic_put_irq() may take LPI xarray lock
In reply to Oliver UptonThe release path on LPIs is quite rare, meaning it can be difficult to find lock ordering bugs on the LPI xarray's spinlock. Tell lockdep that vgic_put_irq() might acquire the xa_lock to make unsafe patterns more obvious.
--- arch/arm64/kvm/vgic/vgic.c | 3 +++ 1 file changed, 3 insertions(+)
arch/arm64/kvm/vgic/vgic.c +3 -0
@@ -142,6 +142,9 @@ void vgic_put_irq(struct kvm *kvm, struct vgic_irq *irq) {struct vgic_dist *dist = &kvm->arch.vgic; +if (irq->intid >VGIC_MIN_LPI ) +might_lock (&dist->lpi_xa.xa_lock); +if (!__vgic_put_irq (kvm, irq))return ;
Signature · 1 line
2.39.5
Re: [PATCH 5/5] KVM: arm64: vgic-v3: Indicate vgic_put_irq() may take LPI xarray lock
Hi Oliver,
Quoted 15 lines · Oliver Upton
The release path on LPIs is quite rare, meaning it can be difficult to find lock ordering bugs on the LPI xarray's spinlock. Tell lockdep that
vgic_put_irq()might acquire thexa_lockto make unsafe patterns more obvious.Signed-off-by Oliver Upton <[email protected]>--- arch/arm64/kvm/vgic/vgic.c | 3 +++ 1 file changed, 3 insertions(+)arch/arm64/kvm/vgic/vgic.c +0
-0
arch/arm64/kvm/vgic/vgic.c void vgic_put_irq(struct kvm *kvm, struct vgic_irq *irq) · line 147
Quoted 6 lines
arch/arm64/kvm/vgic/vgic.c +3
-0@@ -142,6 +142,9 @@ void vgic_put_irq(struct kvm *kvm, struct vgic_irq *irq) {struct vgic_dist *dist = &kvm->arch.vgic; +if (irq->intid >VGIC_MIN_LPI ) +might_lock (&dist->lpi_xa.xa_lock); +
nit: Use >= rather than > to include all LPI.
arch/arm64/kvm/vgic/vgic.c void vgic_put_irq(struct kvm *kvm, struct vgic_irq *irq) · line 150
Quoted 3 lines
arch/arm64/kvm/vgic/vgic.c +0
-0if (!__vgic_put_irq (kvm, irq))return ;
Thanks,
Ben
Re: [PATCH 5/5] KVM: arm64: vgic-v3: Indicate vgic_put_irq() may take LPI xarray lock
Quoted 21 lines · Ben Horgan
Hi Oliver,
Quoted 18 lines · Oliver Upton
The release path on LPIs is quite rare, meaning it can be difficult to find lock ordering bugs on the LPI xarray's spinlock. Tell lockdep that
vgic_put_irq()might acquire thexa_lockto make unsafe patterns more obvious.Signed-off-by Oliver Upton <[email protected]>--- arch/arm64/kvm/vgic/vgic.c | 3 +++ 1 file changed, 3 insertions(+)arch/arm64/kvm/vgic/vgic.c +0
-0@@ -142,6 +142,9 @@ void vgic_put_irq(struct kvm *kvm, struct vgic_irq *irq) {struct vgic_dist *dist = &kvm->arch.vgic;
Quoted 5 lines
arch/arm64/kvm/vgic/vgic.c void vgic_put_irq(struct kvm *kvm, struct vgic_irq *irq) · line 147
Quoted 3 lines
arch/arm64/kvm/vgic/vgic.c +3
-0+if (irq->intid >VGIC_MIN_LPI ) +might_lock (&dist->lpi_xa.xa_lock); +nit: Use >= rather than > to include all LPI.
Of course, silly typo.
Thanks!
Oliver
Quoted 10 lines · Oliver Upton
syzkaller found yet another locking bug in the VGIC [*], this time due to nesting a 'plain' spinlock (
xa_lock) inside of a raw spinlock (ap_list_lock). Given the way we do refcounts on LPIs it is possible for this exact sort of issue to crop up where the last reference may be dropped in unexpected places.Small series to fix the issue by deferring xarray modifications outside of the
ap_list_lockcritical section along with some slight lockdep hinting to make these rare bugs a bit more obvious.
I quite like that we get a release operation, rather than the slightly counter-intuitive construct we have today.
With the couple of issues that Ben and I raised fixed:
M.
Signature · 1 line
Jazz isn't dead. It just smells funny.