Skip to content

Commit 3f804f6

Browse files
KAGA-KOKObonzini
authored andcommitted
KVM: x86: Prevent deadlock against tk_core.seq
syzbot reported a possible deadlock in pvclock_gtod_notify(): CPU 0 CPU 1 write_seqcount_begin(&tk_core.seq); pvclock_gtod_notify() spin_lock(&pool->lock); queue_work(..., &pvclock_gtod_work) ktime_get() spin_lock(&pool->lock); do { seq = read_seqcount_begin(tk_core.seq) ... } while (read_seqcount_retry(&tk_core.seq, seq); While this is unlikely to happen, it's possible. Delegate queue_work() to irq_work() which postpones it until the tk_core.seq write held region is left and interrupts are reenabled. Fixes: 16e8d74 ("KVM: x86: notifier for clocksource changes") Reported-by: syzbot+6beae4000559d41d80f8@syzkaller.appspotmail.com Signed-off-by: Thomas Gleixner <tglx@linutronix.de> Message-Id: <87h7jgm1zy.ffs@nanos.tec.linutronix.de> Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
1 parent 594b27e commit 3f804f6

1 file changed

Lines changed: 18 additions & 4 deletions

File tree

arch/x86/kvm/x86.c

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8094,6 +8094,18 @@ static void pvclock_gtod_update_fn(struct work_struct *work)
80948094

80958095
static DECLARE_WORK(pvclock_gtod_work, pvclock_gtod_update_fn);
80968096

8097+
/*
8098+
* Indirection to move queue_work() out of the tk_core.seq write held
8099+
* region to prevent possible deadlocks against time accessors which
8100+
* are invoked with work related locks held.
8101+
*/
8102+
static void pvclock_irq_work_fn(struct irq_work *w)
8103+
{
8104+
queue_work(system_long_wq, &pvclock_gtod_work);
8105+
}
8106+
8107+
static DEFINE_IRQ_WORK(pvclock_irq_work, pvclock_irq_work_fn);
8108+
80978109
/*
80988110
* Notification about pvclock gtod data update.
80998111
*/
@@ -8105,13 +8117,14 @@ static int pvclock_gtod_notify(struct notifier_block *nb, unsigned long unused,
81058117

81068118
update_pvclock_gtod(tk);
81078119

8108-
/* disable master clock if host does not trust, or does not
8109-
* use, TSC based clocksource.
8120+
/*
8121+
* Disable master clock if host does not trust, or does not use,
8122+
* TSC based clocksource. Delegate queue_work() to irq_work as
8123+
* this is invoked with tk_core.seq write held.
81108124
*/
81118125
if (!gtod_is_based_on_tsc(gtod->clock.vclock_mode) &&
81128126
atomic_read(&kvm_guest_has_master_clock) != 0)
8113-
queue_work(system_long_wq, &pvclock_gtod_work);
8114-
8127+
irq_work_queue(&pvclock_irq_work);
81158128
return 0;
81168129
}
81178130

@@ -8224,6 +8237,7 @@ void kvm_arch_exit(void)
82248237
cpuhp_remove_state_nocalls(CPUHP_AP_X86_KVM_CLK_ONLINE);
82258238
#ifdef CONFIG_X86_64
82268239
pvclock_gtod_unregister_notifier(&pvclock_gtod_notifier);
8240+
irq_work_sync(&pvclock_irq_work);
82278241
cancel_work_sync(&pvclock_gtod_work);
82288242
#endif
82298243
kvm_x86_ops.hardware_enable = NULL;

0 commit comments

Comments
 (0)