From: Paolo Bonzini <pbonzini@redhat.com>
To: linux-kernel@vger.kernel.org, kvm@vger.kernel.org
Cc: Sean Christopherson <seanjc@google.com>,
Hillf Danton <hdanton@sina.com>,
Steven Rostedt <rostedt@goodmis.org>,
Harry Yoo <harry@kernel.org>
Subject: [PATCH 2/2] x86: KVM: remove the dummy allocation case from async page fault handling
Date: Tue, 3 Nov 2026 13:49:30 +0100 [thread overview]
Message-ID: <20261103124930.2278671-3-pbonzini@redhat.com> (raw)
In-Reply-To: <20261103124930.2278671-1-pbonzini@redhat.com>
The page ready notification for asynchronous page faults is delivered as
an interrupt to the same vCPU that received the not-present exception.
As long as interrupts are not enabled before the page fault is registered
in the sleepers' hash table, the page ready notification must happen
after a prior registration.
This is now the case indeed, so the dummy allocation case is dead and
can be dropped.
Note however that a write of the apf-enable MSR that goes from enabled
to enabled might trigger *both* a broadcast notification interrupt
and, later, individual interrupts for pending asynchronous page faults.
While Linux will never do such a write, the host might do a KVM_SET_MSRS
call that would result in the broadcast notification, so do not complain
too loudly if the wait does not find a token and only do a pr_once().
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
---
arch/x86/include/asm/kvm_para.h | 1 -
arch/x86/kernel/kvm.c | 66 ++++++++-------------------------
2 files changed, 15 insertions(+), 52 deletions(-)
diff --git a/arch/x86/include/asm/kvm_para.h b/arch/x86/include/asm/kvm_para.h
index 1e28c89b1405..b986edb1a225 100644
--- a/arch/x86/include/asm/kvm_para.h
+++ b/arch/x86/include/asm/kvm_para.h
@@ -123,7 +123,6 @@ struct kvm_task_sleep_node {
struct swait_queue_head wq;
u32 token;
int cpu;
- bool dummy;
};
#ifdef CONFIG_KVM_GUEST
diff --git a/arch/x86/kernel/kvm.c b/arch/x86/kernel/kvm.c
index ca5dcdc7ffde..ab57cb3ed5a0 100644
--- a/arch/x86/kernel/kvm.c
+++ b/arch/x86/kernel/kvm.c
@@ -111,26 +111,11 @@ bool kvm_async_pf_queue_task(u32 token, struct kvm_task_sleep_node *n)
raw_spin_lock(&b->lock);
e = _find_apf_task(b, token);
if (e) {
- struct kvm_task_sleep_node *dummy = NULL;
-
- /*
- * The entry can either be a 'dummy' entry (which is put on the
- * list when wake-up happens ahead of APF handling completion)
- * or a token from another task which should not be touched.
- */
- if (e->dummy) {
- hlist_del(&e->link);
- dummy = e;
- }
-
raw_spin_unlock(&b->lock);
- kfree(dummy);
return false;
}
-
n->token = token;
n->cpu = smp_processor_id();
- n->dummy = false;
init_swait_queue_head(&n->wq);
hlist_add_head(&n->link, &b->list);
raw_spin_unlock(&b->lock);
@@ -194,53 +179,32 @@ static void kvm_async_pf_task_wake(u32 token)
{
u32 key = hash_32(token, KVM_TASK_SLEEP_HASHBITS);
struct kvm_task_sleep_head *b = &async_pf_sleepers[key];
- struct kvm_task_sleep_node *n, *dummy = NULL;
+ struct kvm_task_sleep_node *n;
if (token == ~0) {
apf_task_wake_all();
return;
}
-again:
raw_spin_lock(&b->lock);
n = _find_apf_task(b, token);
- if (!n) {
- /*
- * Async #PF not yet handled, add a dummy entry for the token.
- * Allocating the token must be down outside of the raw lock
- * as the allocator is preemptible on PREEMPT_RT kernels.
- */
- if (!dummy) {
- raw_spin_unlock(&b->lock);
- dummy = kzalloc_obj(*dummy, GFP_ATOMIC);
- /*
- * Continue looping on allocation failure, eventually
- * the async #PF will be handled and allocating a new
- * node will be unnecessary.
- */
- if (!dummy)
- cpu_relax();
-
- /*
- * Recheck for async #PF completion before enqueueing
- * the dummy token to avoid duplicate list entries.
- */
- goto again;
- }
- dummy->token = token;
- dummy->cpu = smp_processor_id();
- dummy->dummy = true;
- init_swait_queue_head(&dummy->wq);
- hlist_add_head(&dummy->link, &b->list);
- dummy = NULL;
- } else {
+ /*
+ * A broadcast wakeup, which can happen for example if the VMM calls
+ * KVM_SET_MSRS, might still leave the individual wakeups queued.
+ * In that case _find_apf_task() will not find the token when the
+ * individual wakeup comes, so just ignore the case where n is NULL.
+ * Nevertheless it *should* not happen, so print a message for the
+ * user in case something bad actually happens later.
+ */
+ if (n)
apf_task_wake_one(n);
- }
- raw_spin_unlock(&b->lock);
+ else
+ pr_info_once("Async page fault waiter not found. This is probably triggered "
+ "by the host and harmless. However, if a userspace task hangs, "
+ "please include this message in your bug report.\n");
- /* A dummy token might be allocated and ultimately not used. */
- kfree(dummy);
+ raw_spin_unlock(&b->lock);
}
noinstr u32 kvm_read_and_reset_apf_flags(void)
--
2.56.0
prev parent reply other threads:[~2026-11-03 12:49 UTC|newest]
Thread overview: 4+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-03 12:49 [PATCH 0/2] " Paolo Bonzini
2026-11-03 12:49 ` [PATCH 1/2] KVM: register asynchronous page fault waits with interrupts disabled Paolo Bonzini
2026-11-03 14:43 ` Sean Christopherson
2026-11-03 12:49 ` Paolo Bonzini [this message]
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=20261103124930.2278671-3-pbonzini@redhat.com \
--to=pbonzini@redhat.com \
--cc=harry@kernel.org \
--cc=hdanton@sina.com \
--cc=kvm@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=rostedt@goodmis.org \
--cc=seanjc@google.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
all inboxes | Powered by JetHome®