From: Sean Christopherson <seanjc@google.com>
To: Paolo Bonzini <pbonzini@redhat.com>
Cc: Sean Christopherson <seanjc@google.com>,
kvm@vger.kernel.org, linux-kernel@vger.kernel.org,
Satish Kharat <satishkh@cisco.com>,
Sashiko Bot <sashiko-bot@kernel.org>,
Oliver Upton <oupton@kernel.org>, Marc Zyngier <maz@kernel.org>
Subject: [PATCH v2 2/6] KVM: Add struct and helpers to handle kicking multiple (v)CPUs
Date: Thu, 19 Nov 2026 15:56:52 -0800 [thread overview]
Message-ID: <20261119235656.70315-3-seanjc@google.com> (raw)
In-Reply-To: <20261119235656.70315-1-seanjc@google.com>
Add a "kvm_cpu_kick" structure along with init()+finish() helpers to dedup
the core logic for kicking multiple vCPUs, and to make it easier to handle
non-waiting kicks via smp_send_reschedule() instead of
smp_call_function_many().
No functional change intended.
Signed-off-by: Sean Christopherson <seanjc@google.com>
---
virt/kvm/kvm_main.c | 77 +++++++++++++++++++++++++--------------------
1 file changed, 43 insertions(+), 34 deletions(-)
diff --git a/virt/kvm/kvm_main.c b/virt/kvm/kvm_main.c
index cf4cb586f140..c2231a130c4c 100644
--- a/virt/kvm/kvm_main.c
+++ b/virt/kvm/kvm_main.c
@@ -222,15 +222,43 @@ static inline bool kvm_kick_many_cpus(struct cpumask *cpus, bool wait)
return true;
}
-static void kvm_make_vcpu_request(struct kvm_vcpu *vcpu, unsigned int req,
- struct cpumask *tmp, int current_cpu)
+struct kvm_cpu_kick {
+ int me;
+ unsigned int req;
+ struct cpumask *cpus;
+};
+
+static __always_inline struct kvm_cpu_kick kvm_init_cpu_kick(unsigned int req)
+{
+ struct kvm_cpu_kick kick = { .req = req };
+
+ kick.me = get_cpu();
+
+ kick.cpus = this_cpu_cpumask_var_ptr(cpu_kick_mask);
+ cpumask_clear(kick.cpus);
+
+ return kick;
+}
+
+static bool kvm_finish_cpu_kick(struct kvm_cpu_kick *kick)
+{
+ bool kicked;
+
+ kicked = kvm_kick_many_cpus(kick->cpus, !!(kick->req & KVM_REQUEST_WAIT));
+
+ put_cpu();
+ return kicked;
+}
+
+static void kvm_make_vcpu_request(struct kvm_vcpu *vcpu,
+ struct kvm_cpu_kick *kick)
{
int cpu;
- if (likely(!(req & KVM_REQUEST_NO_ACTION)))
- __kvm_make_request(req, vcpu);
+ if (likely(!(kick->req & KVM_REQUEST_NO_ACTION)))
+ __kvm_make_request(kick->req, vcpu);
- if (!(req & KVM_REQUEST_NO_WAKEUP) && kvm_vcpu_wake_up(vcpu))
+ if (!(kick->req & KVM_REQUEST_NO_WAKEUP) && kvm_vcpu_wake_up(vcpu))
return;
/*
@@ -243,59 +271,40 @@ static void kvm_make_vcpu_request(struct kvm_vcpu *vcpu, unsigned int req,
* for vCPUs that were reading SPTEs _before_ any changes were
* finalized. See kvm_vcpu_kick() for more details on handling requests.
*/
- if (kvm_request_needs_ipi(vcpu, req)) {
+ if (kvm_request_needs_ipi(vcpu, kick->req)) {
cpu = READ_ONCE(vcpu->cpu);
- if (cpu != -1 && cpu != current_cpu)
- __cpumask_set_cpu(cpu, tmp);
+ if (cpu != -1 && cpu != kick->me)
+ __cpumask_set_cpu(cpu, kick->cpus);
}
}
bool kvm_make_vcpus_request_mask(struct kvm *kvm, unsigned int req,
unsigned long *vcpu_bitmap)
{
+ struct kvm_cpu_kick kick = kvm_init_cpu_kick(req);
struct kvm_vcpu *vcpu;
- struct cpumask *cpus;
- int i, me;
- bool called;
-
- me = get_cpu();
-
- cpus = this_cpu_cpumask_var_ptr(cpu_kick_mask);
- cpumask_clear(cpus);
+ int i;
for_each_set_bit(i, vcpu_bitmap, KVM_MAX_VCPUS) {
vcpu = kvm_get_vcpu(kvm, i);
if (!vcpu)
continue;
- kvm_make_vcpu_request(vcpu, req, cpus, me);
+ kvm_make_vcpu_request(vcpu, &kick);
}
- called = kvm_kick_many_cpus(cpus, !!(req & KVM_REQUEST_WAIT));
- put_cpu();
-
- return called;
+ return kvm_finish_cpu_kick(&kick);
}
bool kvm_make_all_cpus_request(struct kvm *kvm, unsigned int req)
{
+ struct kvm_cpu_kick kick = kvm_init_cpu_kick(req);
struct kvm_vcpu *vcpu;
- struct cpumask *cpus;
unsigned long i;
- bool called;
- int me;
-
- me = get_cpu();
-
- cpus = this_cpu_cpumask_var_ptr(cpu_kick_mask);
- cpumask_clear(cpus);
kvm_for_each_vcpu(i, vcpu, kvm)
- kvm_make_vcpu_request(vcpu, req, cpus, me);
+ kvm_make_vcpu_request(vcpu, &kick);
- called = kvm_kick_many_cpus(cpus, !!(req & KVM_REQUEST_WAIT));
- put_cpu();
-
- return called;
+ return kvm_finish_cpu_kick(&kick);
}
EXPORT_SYMBOL_FOR_KVM_INTERNAL(kvm_make_all_cpus_request);
--
2.56.1.708.g77570a6a1d-goog
next prev parent reply other threads:[~2026-11-19 23:57 UTC|newest]
Thread overview: 7+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-19 23:56 [PATCH v2 0/6] KVM: Send reschedule IPIs for non-waiting requests Sean Christopherson
2026-11-19 23:56 ` [PATCH v2 1/6] s390/smp: Export arch_smp_send_reschedule() for KVM Sean Christopherson
2026-11-19 23:56 ` Sean Christopherson [this message]
2026-11-19 23:56 ` [PATCH v2 3/6] KVM: Add dedicated flag to track if multi-vCPU request needs at least one kick Sean Christopherson
2026-11-19 23:56 ` [PATCH v2 4/6] KVM: Don't kick offline CPUs when making a multi-vCPU request Sean Christopherson
2026-11-19 23:56 ` [PATCH v2 5/6] KVM: Send reschedule IPIs instead of SMP function calls for non-waiting requests Sean Christopherson
2026-11-19 23:56 ` [PATCH v2 6/6] KVM: Extract and dedup the kick code to check if a CPU needs an IPI Sean Christopherson
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=20261119235656.70315-3-seanjc@google.com \
--to=seanjc@google.com \
--cc=kvm@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=maz@kernel.org \
--cc=oupton@kernel.org \
--cc=pbonzini@redhat.com \
--cc=sashiko-bot@kernel.org \
--cc=satishkh@cisco.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®