Re: [PATCH v3 3/4] KVM: Optimize kvm_make_vcpus_request_mask() a bit

From: Sean Christopherson
Date: Thu Aug 26 2021 - 11:25:20 EST


On Thu, Aug 26, 2021, Vitaly Kuznetsov wrote:
> Iterating over set bits in 'vcpu_bitmap' should be faster than going
> through all vCPUs, especially when just a few bits are set.
>
> Signed-off-by: Vitaly Kuznetsov <vkuznets@xxxxxxxxxx>
> ---

...

> + if (vcpu_bitmap) {
> + for_each_set_bit(i, vcpu_bitmap, KVM_MAX_VCPUS) {
> + vcpu = kvm_get_vcpu(kvm, i);
> + if (!vcpu || vcpu == except)
> + continue;
> + kvm_make_vcpu_request(kvm, vcpu, req, tmp, me);
> + }
> + } else {
> + kvm_for_each_vcpu(i, vcpu, kvm) {
> + if (vcpu == except)
> + continue;
> + kvm_make_vcpu_request(kvm, vcpu, req, tmp, me);
> }
> }

Rather than feed kvm_make_all_cpus_request_except() into kvm_make_vcpus_request_mask(),
I think it would be better to move the kvm_for_each_vcpu() path into
kvm_make_all_cpus_request_except() (see bottom of the mail). That eliminates the
silliness of calling a "mask" function without a mask, and also allows a follow-up
patch to drop @except from kvm_make_vcpus_request_mask(), which is truly nonsensical
as the caller can and should simply not set that vCPU in the mask. E.g.

---
arch/x86/kvm/hyperv.c | 2 +-
arch/x86/kvm/x86.c | 2 +-
include/linux/kvm_host.h | 1 -
virt/kvm/kvm_main.c | 3 +--
4 files changed, 3 insertions(+), 5 deletions(-)

diff --git a/arch/x86/kvm/hyperv.c b/arch/x86/kvm/hyperv.c
index fe4a02715266..5787becdcfe4 100644
--- a/arch/x86/kvm/hyperv.c
+++ b/arch/x86/kvm/hyperv.c
@@ -1848,7 +1848,7 @@ static u64 kvm_hv_flush_tlb(struct kvm_vcpu *vcpu, struct kvm_hv_hcall *hc, bool
* analyze it here, flush TLB regardless of the specified address space.
*/
kvm_make_vcpus_request_mask(kvm, KVM_REQ_TLB_FLUSH_GUEST,
- NULL, vcpu_mask, &hv_vcpu->tlb_flush);
+ vcpu_mask, &hv_vcpu->tlb_flush);

ret_success:
/* We always do full TLB flush, set 'Reps completed' = 'Rep Count' */
diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c
index 11c7a02f839c..cf8fb6eb676a 100644
--- a/arch/x86/kvm/x86.c
+++ b/arch/x86/kvm/x86.c
@@ -9182,7 +9182,7 @@ void kvm_make_scan_ioapic_request_mask(struct kvm *kvm,
zalloc_cpumask_var(&cpus, GFP_ATOMIC);

kvm_make_vcpus_request_mask(kvm, KVM_REQ_SCAN_IOAPIC,
- NULL, vcpu_bitmap, cpus);
+ vcpu_bitmap, cpus);

free_cpumask_var(cpus);
}
diff --git a/include/linux/kvm_host.h b/include/linux/kvm_host.h
index 70700d4d5410..742f3b2f7c73 100644
--- a/include/linux/kvm_host.h
+++ b/include/linux/kvm_host.h
@@ -160,7 +160,6 @@ static inline bool is_error_page(struct page *page)
#define KVM_ARCH_REQ(nr) KVM_ARCH_REQ_FLAGS(nr, 0)

bool kvm_make_vcpus_request_mask(struct kvm *kvm, unsigned int req,
- struct kvm_vcpu *except,
unsigned long *vcpu_bitmap, cpumask_var_t tmp);
bool kvm_make_all_cpus_request(struct kvm *kvm, unsigned int req);
bool kvm_make_all_cpus_request_except(struct kvm *kvm, unsigned int req,
diff --git a/virt/kvm/kvm_main.c b/virt/kvm/kvm_main.c
index d0d2b7fbfe43..56b524365f69 100644
--- a/virt/kvm/kvm_main.c
+++ b/virt/kvm/kvm_main.c
@@ -298,7 +298,6 @@ static void kvm_make_vcpu_request(struct kvm *kvm, struct kvm_vcpu *vcpu,
}

bool kvm_make_vcpus_request_mask(struct kvm *kvm, unsigned int req,
- struct kvm_vcpu *except,
unsigned long *vcpu_bitmap, cpumask_var_t tmp)
{
struct kvm_vcpu *vcpu;
@@ -309,7 +308,7 @@ bool kvm_make_vcpus_request_mask(struct kvm *kvm, unsigned int req,

for_each_set_bit(i, vcpu_bitmap, KVM_MAX_VCPUS) {
vcpu = kvm_get_vcpu(kvm, i);
- if (!vcpu || vcpu == except)
+ if (!vcpu)
continue;
kvm_make_vcpu_request(kvm, vcpu, req, tmp, me);
}
--



on top of...

---
virt/kvm/kvm_main.c | 33 ++++++++++++++++++---------------
1 file changed, 18 insertions(+), 15 deletions(-)

diff --git a/virt/kvm/kvm_main.c b/virt/kvm/kvm_main.c
index a800a9f89806..d0d2b7fbfe43 100644
--- a/virt/kvm/kvm_main.c
+++ b/virt/kvm/kvm_main.c
@@ -301,25 +301,17 @@ bool kvm_make_vcpus_request_mask(struct kvm *kvm, unsigned int req,
struct kvm_vcpu *except,
unsigned long *vcpu_bitmap, cpumask_var_t tmp)
{
- int i, me;
struct kvm_vcpu *vcpu;
+ int i, me;
bool called;

me = get_cpu();

- if (vcpu_bitmap) {
- for_each_set_bit(i, vcpu_bitmap, KVM_MAX_VCPUS) {
- vcpu = kvm_get_vcpu(kvm, i);
- if (!vcpu || vcpu == except)
- continue;
- kvm_make_vcpu_request(kvm, vcpu, req, tmp, me);
- }
- } else {
- kvm_for_each_vcpu(i, vcpu, kvm) {
- if (vcpu == except)
- continue;
- kvm_make_vcpu_request(kvm, vcpu, req, tmp, me);
- }
+ for_each_set_bit(i, vcpu_bitmap, KVM_MAX_VCPUS) {
+ vcpu = kvm_get_vcpu(kvm, i);
+ if (!vcpu || vcpu == except)
+ continue;
+ kvm_make_vcpu_request(kvm, vcpu, req, tmp, me);
}

called = kvm_kick_many_cpus(tmp, !!(req & KVM_REQUEST_WAIT));
@@ -331,12 +323,23 @@ bool kvm_make_vcpus_request_mask(struct kvm *kvm, unsigned int req,
bool kvm_make_all_cpus_request_except(struct kvm *kvm, unsigned int req,
struct kvm_vcpu *except)
{
+ struct kvm_vcpu *vcpu;
cpumask_var_t cpus;
bool called;
+ int i, me;

zalloc_cpumask_var(&cpus, GFP_ATOMIC);

- called = kvm_make_vcpus_request_mask(kvm, req, except, NULL, cpus);
+ me = get_cpu();
+
+ kvm_for_each_vcpu(i, vcpu, kvm) {
+ if (vcpu == except)
+ continue;
+ kvm_make_vcpu_request(kvm, vcpu, req, cpus, me);
+ }
+
+ called = kvm_kick_many_cpus(cpus, !!(req & KVM_REQUEST_WAIT));
+ put_cpu();

free_cpumask_var(cpus);
return called;
--