From: "Huang, Kai" <kai.huang@intel.com>
To: "pbonzini@redhat.com" <pbonzini@redhat.com>,
"seanjc@google.com" <seanjc@google.com>,
"bestswngs@gmail.com" <bestswngs@gmail.com>
Cc: "Wang, Zhong" <wangzhong.c0ss4ck@bytedance.com>,
"kvm@vger.kernel.org" <kvm@vger.kernel.org>,
"shixuanqing.11@bytedance.com" <shixuanqing.11@bytedance.com>
Subject: Re: [PATCH] KVM: x86/ioapic: Cancel eoi_inject work before destroying vCPUs
Date: Mon, 6 Jul 2026 09:09:42 +0000 [thread overview]
Message-ID: <f654b630538d27d05ebf09a5b7ff07cee1fed773.camel@intel.com> (raw)
In-Reply-To: <20260705050443.1331662-1-bestswngs@gmail.com>
On Sun, 2026-07-05 at 13:04 +0800, Weiming Shi wrote:
> kvm_ioapic_eoi_inject_work() re-delivers a throttled level-triggered
> interrupt via kvm_irq_delivery_to_apic(), which walks kvm->arch.apic_map
> and dereferences the destination vCPU's APIC. The work is cancelled only
> in kvm_ioapic_destroy(), which runs after kvm_destroy_vcpus() has freed
> the vCPUs and their APICs. kvm_free_lapic() does not rebuild apic_map, so
> the map is left with dangling pointers, and a work item that fires during
> that window reads freed memory:
>
> BUG: KASAN: slab-use-after-free in __kvm_irq_delivery_to_apic_fast (arch/x86/kvm/lapic.c:1248)
> Read of size 8 by task kworker/3:1
> Workqueue: events kvm_ioapic_eoi_inject_work
> __kvm_irq_delivery_to_apic_fast (arch/x86/kvm/lapic.c:1248)
> __kvm_irq_delivery_to_apic (arch/x86/kvm/lapic.c:1343)
> ioapic_service (arch/x86/kvm/ioapic.c:492)
> kvm_ioapic_eoi_inject_work (arch/x86/kvm/ioapic.c:525)
> process_one_work
>
> Freed by task 153:
> kvm_arch_vcpu_destroy (arch/x86/kvm/x86.c:12871)
> kvm_destroy_vcpus (virt/kvm/kvm_main.c:489)
> kvm_arch_destroy_vm (arch/x86/kvm/x86.c:13402)
> kvm_destroy_vm (virt/kvm/kvm_main.c:1302)
> kvm_vm_release (virt/kvm/kvm_main.c:1363)
>
> A guest arms the work by EOIing a level-triggered pin 10000 times in a
> row, so the window is reachable from guest ring 0 whenever its VM is torn
> down soon after.
>
> Add kvm_ioapic_pre_destroy() and call it from kvm_arch_pre_destroy_vm(),
> which already stops the PIT and the kvmclock/MMU workers before vCPUs are
> freed for the same reason.
I am wondering whether we can just move kvm_ioapic_destroy() (and
kvm_pic_destroy()) to kvm_arch_pre_destroy_vm(). I guess the concern is we want
to make sure vIOAPIC is destroyed _after_ kvm_free_irq_routing() is done?
Btw, one odd thing I saw is all KVM IO buses are actually destroyed right after
kvm_free_irq_routing(), but yet kvm_ioapic_destroy() tries to unregister it from
the KVM_MMIO_BUS (ditto for kvm_pic_destroy).
next prev parent reply other threads:[~2026-07-06 9:09 UTC|newest]
Thread overview: 4+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-07-05 5:04 [PATCH] KVM: x86/ioapic: Cancel eoi_inject work before destroying vCPUs Weiming Shi
2026-07-05 5:18 ` 王众
2026-07-06 9:09 ` Huang, Kai [this message]
2026-07-06 17:50 ` Weiming Shi
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=f654b630538d27d05ebf09a5b7ff07cee1fed773.camel@intel.com \
--to=kai.huang@intel.com \
--cc=bestswngs@gmail.com \
--cc=kvm@vger.kernel.org \
--cc=pbonzini@redhat.com \
--cc=seanjc@google.com \
--cc=shixuanqing.11@bytedance.com \
--cc=wangzhong.c0ss4ck@bytedance.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox