* [PATCH] [backport for 4.19/5.4 stable] KVM: remember position in kvm->vcpus array
@ 2021-09-21 13:48 Christian Borntraeger
2021-09-21 14:31 ` Paolo Bonzini
0 siblings, 1 reply; 3+ messages in thread
From: Christian Borntraeger @ 2021-09-21 13:48 UTC (permalink / raw)
To: stable
Cc: gregkh, KVM, Cornelia Huck, Christian Borntraeger, Janosch Frank,
David Hildenbrand, linux-s390, Thomas Huth, Claudio Imbrenda,
Paolo Bonzini
From: Radim Krčmář <rkrcmar@redhat.com>
Fetching an index for any vcpu in kvm->vcpus array by traversing
the entire array everytime is costly.
This patch remembers the position of each vcpu in kvm->vcpus array
by storing it in vcpus_idx under kvm_vcpu structure.
Signed-off-by: Radim Krčmář <rkrcmar@redhat.com>
Signed-off-by: Nitesh Narayan Lal <nitesh@redhat.com>
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
[borntraeger@de.ibm.com]: backport to 4.19 (also fits for 5.4)
Signed-off-by: Christian Borntraeger <borntraeger@de.ibm.com>
---
include/linux/kvm_host.h | 11 +++--------
virt/kvm/kvm_main.c | 5 +++--
2 files changed, 6 insertions(+), 10 deletions(-)
diff --git a/include/linux/kvm_host.h b/include/linux/kvm_host.h
index 8dd4ebb58e97..827f70ce0b49 100644
--- a/include/linux/kvm_host.h
+++ b/include/linux/kvm_host.h
@@ -248,7 +248,8 @@ struct kvm_vcpu {
struct preempt_notifier preempt_notifier;
#endif
int cpu;
- int vcpu_id;
+ int vcpu_id; /* id given by userspace at creation */
+ int vcpu_idx; /* index in kvm->vcpus array */
int srcu_idx;
int mode;
u64 requests;
@@ -551,13 +552,7 @@ static inline struct kvm_vcpu *kvm_get_vcpu_by_id(struct kvm *kvm, int id)
static inline int kvm_vcpu_get_idx(struct kvm_vcpu *vcpu)
{
- struct kvm_vcpu *tmp;
- int idx;
-
- kvm_for_each_vcpu(idx, tmp, vcpu->kvm)
- if (tmp == vcpu)
- return idx;
- BUG();
+ return vcpu->vcpu_idx;
}
#define kvm_for_each_memslot(memslot, slots) \
diff --git a/virt/kvm/kvm_main.c b/virt/kvm/kvm_main.c
index a3d82113ae1c..86ef740763b5 100644
--- a/virt/kvm/kvm_main.c
+++ b/virt/kvm/kvm_main.c
@@ -2751,7 +2751,8 @@ static int kvm_vm_ioctl_create_vcpu(struct kvm *kvm, u32 id)
goto unlock_vcpu_destroy;
}
- BUG_ON(kvm->vcpus[atomic_read(&kvm->online_vcpus)]);
+ vcpu->vcpu_idx = atomic_read(&kvm->online_vcpus);
+ BUG_ON(kvm->vcpus[vcpu->vcpu_idx]);
/* Now it's all set up, let userspace reach it */
kvm_get_kvm(kvm);
@@ -2761,7 +2762,7 @@ static int kvm_vm_ioctl_create_vcpu(struct kvm *kvm, u32 id)
goto unlock_vcpu_destroy;
}
- kvm->vcpus[atomic_read(&kvm->online_vcpus)] = vcpu;
+ kvm->vcpus[vcpu->vcpu_idx] = vcpu;
/*
* Pairs with smp_rmb() in kvm_get_vcpu. Write kvm->vcpus
--
2.31.1
^ permalink raw reply related [flat|nested] 3+ messages in thread
* Re: [PATCH] [backport for 4.19/5.4 stable] KVM: remember position in kvm->vcpus array
2021-09-21 13:48 [PATCH] [backport for 4.19/5.4 stable] KVM: remember position in kvm->vcpus array Christian Borntraeger
@ 2021-09-21 14:31 ` Paolo Bonzini
2021-09-23 7:49 ` Greg KH
0 siblings, 1 reply; 3+ messages in thread
From: Paolo Bonzini @ 2021-09-21 14:31 UTC (permalink / raw)
To: Christian Borntraeger, stable
Cc: gregkh, KVM, Cornelia Huck, Janosch Frank, David Hildenbrand,
linux-s390, Thomas Huth, Claudio Imbrenda
On 21/09/21 15:48, Christian Borntraeger wrote:
> From: Radim Krčmář <rkrcmar@redhat.com>
>
> Fetching an index for any vcpu in kvm->vcpus array by traversing
> the entire array everytime is costly.
> This patch remembers the position of each vcpu in kvm->vcpus array
> by storing it in vcpus_idx under kvm_vcpu structure.
>
> Signed-off-by: Radim Krčmář <rkrcmar@redhat.com>
> Signed-off-by: Nitesh Narayan Lal <nitesh@redhat.com>
> Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
> [borntraeger@de.ibm.com]: backport to 4.19 (also fits for 5.4)
> Signed-off-by: Christian Borntraeger <borntraeger@de.ibm.com>
> ---
> include/linux/kvm_host.h | 11 +++--------
> virt/kvm/kvm_main.c | 5 +++--
> 2 files changed, 6 insertions(+), 10 deletions(-)
>
> diff --git a/include/linux/kvm_host.h b/include/linux/kvm_host.h
> index 8dd4ebb58e97..827f70ce0b49 100644
> --- a/include/linux/kvm_host.h
> +++ b/include/linux/kvm_host.h
> @@ -248,7 +248,8 @@ struct kvm_vcpu {
> struct preempt_notifier preempt_notifier;
> #endif
> int cpu;
> - int vcpu_id;
> + int vcpu_id; /* id given by userspace at creation */
> + int vcpu_idx; /* index in kvm->vcpus array */
> int srcu_idx;
> int mode;
> u64 requests;
> @@ -551,13 +552,7 @@ static inline struct kvm_vcpu *kvm_get_vcpu_by_id(struct kvm *kvm, int id)
>
> static inline int kvm_vcpu_get_idx(struct kvm_vcpu *vcpu)
> {
> - struct kvm_vcpu *tmp;
> - int idx;
> -
> - kvm_for_each_vcpu(idx, tmp, vcpu->kvm)
> - if (tmp == vcpu)
> - return idx;
> - BUG();
> + return vcpu->vcpu_idx;
> }
>
> #define kvm_for_each_memslot(memslot, slots) \
> diff --git a/virt/kvm/kvm_main.c b/virt/kvm/kvm_main.c
> index a3d82113ae1c..86ef740763b5 100644
> --- a/virt/kvm/kvm_main.c
> +++ b/virt/kvm/kvm_main.c
> @@ -2751,7 +2751,8 @@ static int kvm_vm_ioctl_create_vcpu(struct kvm *kvm, u32 id)
> goto unlock_vcpu_destroy;
> }
>
> - BUG_ON(kvm->vcpus[atomic_read(&kvm->online_vcpus)]);
> + vcpu->vcpu_idx = atomic_read(&kvm->online_vcpus);
> + BUG_ON(kvm->vcpus[vcpu->vcpu_idx]);
>
> /* Now it's all set up, let userspace reach it */
> kvm_get_kvm(kvm);
> @@ -2761,7 +2762,7 @@ static int kvm_vm_ioctl_create_vcpu(struct kvm *kvm, u32 id)
> goto unlock_vcpu_destroy;
> }
>
> - kvm->vcpus[atomic_read(&kvm->online_vcpus)] = vcpu;
> + kvm->vcpus[vcpu->vcpu_idx] = vcpu;
>
> /*
> * Pairs with smp_rmb() in kvm_get_vcpu. Write kvm->vcpus
>
Acked-by: Paolo Bonzini <pbonzini@redhat.com>
The backport makes sense given the code in the stable branch now calls
kvm_vcpu_get_idx more than it used to.
Paolo
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: [PATCH] [backport for 4.19/5.4 stable] KVM: remember position in kvm->vcpus array
2021-09-21 14:31 ` Paolo Bonzini
@ 2021-09-23 7:49 ` Greg KH
0 siblings, 0 replies; 3+ messages in thread
From: Greg KH @ 2021-09-23 7:49 UTC (permalink / raw)
To: Paolo Bonzini
Cc: Christian Borntraeger, stable, KVM, Cornelia Huck, Janosch Frank,
David Hildenbrand, linux-s390, Thomas Huth, Claudio Imbrenda
On Tue, Sep 21, 2021 at 04:31:03PM +0200, Paolo Bonzini wrote:
> On 21/09/21 15:48, Christian Borntraeger wrote:
> > From: Radim Krčmář <rkrcmar@redhat.com>
> >
> > Fetching an index for any vcpu in kvm->vcpus array by traversing
> > the entire array everytime is costly.
> > This patch remembers the position of each vcpu in kvm->vcpus array
> > by storing it in vcpus_idx under kvm_vcpu structure.
> >
> > Signed-off-by: Radim Krčmář <rkrcmar@redhat.com>
> > Signed-off-by: Nitesh Narayan Lal <nitesh@redhat.com>
> > Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
> > [borntraeger@de.ibm.com]: backport to 4.19 (also fits for 5.4)
> > Signed-off-by: Christian Borntraeger <borntraeger@de.ibm.com>
> > ---
> > include/linux/kvm_host.h | 11 +++--------
> > virt/kvm/kvm_main.c | 5 +++--
> > 2 files changed, 6 insertions(+), 10 deletions(-)
> >
> > diff --git a/include/linux/kvm_host.h b/include/linux/kvm_host.h
> > index 8dd4ebb58e97..827f70ce0b49 100644
> > --- a/include/linux/kvm_host.h
> > +++ b/include/linux/kvm_host.h
> > @@ -248,7 +248,8 @@ struct kvm_vcpu {
> > struct preempt_notifier preempt_notifier;
> > #endif
> > int cpu;
> > - int vcpu_id;
> > + int vcpu_id; /* id given by userspace at creation */
> > + int vcpu_idx; /* index in kvm->vcpus array */
> > int srcu_idx;
> > int mode;
> > u64 requests;
> > @@ -551,13 +552,7 @@ static inline struct kvm_vcpu *kvm_get_vcpu_by_id(struct kvm *kvm, int id)
> > static inline int kvm_vcpu_get_idx(struct kvm_vcpu *vcpu)
> > {
> > - struct kvm_vcpu *tmp;
> > - int idx;
> > -
> > - kvm_for_each_vcpu(idx, tmp, vcpu->kvm)
> > - if (tmp == vcpu)
> > - return idx;
> > - BUG();
> > + return vcpu->vcpu_idx;
> > }
> > #define kvm_for_each_memslot(memslot, slots) \
> > diff --git a/virt/kvm/kvm_main.c b/virt/kvm/kvm_main.c
> > index a3d82113ae1c..86ef740763b5 100644
> > --- a/virt/kvm/kvm_main.c
> > +++ b/virt/kvm/kvm_main.c
> > @@ -2751,7 +2751,8 @@ static int kvm_vm_ioctl_create_vcpu(struct kvm *kvm, u32 id)
> > goto unlock_vcpu_destroy;
> > }
> > - BUG_ON(kvm->vcpus[atomic_read(&kvm->online_vcpus)]);
> > + vcpu->vcpu_idx = atomic_read(&kvm->online_vcpus);
> > + BUG_ON(kvm->vcpus[vcpu->vcpu_idx]);
> > /* Now it's all set up, let userspace reach it */
> > kvm_get_kvm(kvm);
> > @@ -2761,7 +2762,7 @@ static int kvm_vm_ioctl_create_vcpu(struct kvm *kvm, u32 id)
> > goto unlock_vcpu_destroy;
> > }
> > - kvm->vcpus[atomic_read(&kvm->online_vcpus)] = vcpu;
> > + kvm->vcpus[vcpu->vcpu_idx] = vcpu;
> > /*
> > * Pairs with smp_rmb() in kvm_get_vcpu. Write kvm->vcpus
> >
>
> Acked-by: Paolo Bonzini <pbonzini@redhat.com>
>
> The backport makes sense given the code in the stable branch now calls
> kvm_vcpu_get_idx more than it used to.
Now queued up, thanks.
greg k-h
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2021-09-23 7:49 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-09-21 13:48 [PATCH] [backport for 4.19/5.4 stable] KVM: remember position in kvm->vcpus array Christian Borntraeger
2021-09-21 14:31 ` Paolo Bonzini
2021-09-23 7:49 ` Greg KH
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.