From: rkrcmar@redhat.com (Radim Krčmář)
To: linux-arm-kernel@lists.infradead.org
Subject: [PATCH RFC v3 6/9] KVM: rework kvm_vcpu_on_spin loop
Date: Mon, 21 Aug 2017 22:35:27 +0200 [thread overview]
Message-ID: <20170821203530.9266-7-rkrcmar@redhat.com> (raw)
In-Reply-To: <20170821203530.9266-1-rkrcmar@redhat.com>
The original code managed to obfuscate a straightforward idea:
start iterating from the selected index and reset the index to 0 when
reaching the end of online vcpus, then iterate until reaching the index
that we started at.
The resulting code is a bit better, IMO. (Still horrible, though.)
Signed-off-by: Radim Kr?m?? <rkrcmar@redhat.com>
---
include/linux/kvm_host.h | 13 +++++++++++++
virt/kvm/kvm_main.c | 47 ++++++++++++++++++-----------------------------
2 files changed, 31 insertions(+), 29 deletions(-)
diff --git a/include/linux/kvm_host.h b/include/linux/kvm_host.h
index abd5cb1feb9e..cfb3c0efdd51 100644
--- a/include/linux/kvm_host.h
+++ b/include/linux/kvm_host.h
@@ -498,6 +498,19 @@ static inline struct kvm_vcpu *kvm_get_vcpu(struct kvm *kvm, int i)
(vcpup = kvm_get_vcpu(kvm, idx)) != NULL; \
idx++)
+#define kvm_for_each_vcpu_from(idx, vcpup, from, kvm) \
+ for (idx = from, vcpup = kvm_get_vcpu(kvm, idx); \
+ vcpup; \
+ ({ \
+ idx++; \
+ if (idx >= atomic_read(&kvm->online_vcpus)) \
+ idx = 0; \
+ if (idx == from) \
+ vcpup = NULL; \
+ else \
+ vcpup = kvm_get_vcpu(kvm, idx); \
+ }))
+
static inline struct kvm_vcpu *kvm_get_vcpu_by_id(struct kvm *kvm, int id)
{
struct kvm_vcpu *vcpu = NULL;
diff --git a/virt/kvm/kvm_main.c b/virt/kvm/kvm_main.c
index d89261d0d8c6..33a15e176927 100644
--- a/virt/kvm/kvm_main.c
+++ b/virt/kvm/kvm_main.c
@@ -2333,8 +2333,7 @@ void kvm_vcpu_on_spin(struct kvm_vcpu *me, bool yield_to_kernel_mode)
struct kvm_vcpu *vcpu;
int last_boosted_vcpu = me->kvm->last_boosted_vcpu;
int yielded = 0;
- int try = 3;
- int pass;
+ int try = 2;
int i;
kvm_vcpu_set_in_spin_loop(me, true);
@@ -2345,34 +2344,24 @@ void kvm_vcpu_on_spin(struct kvm_vcpu *me, bool yield_to_kernel_mode)
* VCPU is holding the lock that we need and will release it.
* We approximate round-robin by starting at the last boosted VCPU.
*/
- for (pass = 0; pass < 2 && !yielded && try; pass++) {
- kvm_for_each_vcpu(i, vcpu, kvm) {
- if (!pass && i <= last_boosted_vcpu) {
- i = last_boosted_vcpu;
- continue;
- } else if (pass && i > last_boosted_vcpu)
- break;
- if (!ACCESS_ONCE(vcpu->preempted))
- continue;
- if (vcpu == me)
- continue;
- if (swait_active(&vcpu->wq) && !kvm_arch_vcpu_runnable(vcpu))
- continue;
- if (yield_to_kernel_mode && !kvm_arch_vcpu_in_kernel(vcpu))
- continue;
- if (!kvm_vcpu_eligible_for_directed_yield(vcpu))
- continue;
+ kvm_for_each_vcpu_from(i, vcpu, last_boosted_vcpu, kvm) {
+ if (!ACCESS_ONCE(vcpu->preempted))
+ continue;
+ if (vcpu == me)
+ continue;
+ if (swait_active(&vcpu->wq) && !kvm_arch_vcpu_runnable(vcpu))
+ continue;
+ if (yield_to_kernel_mode && !kvm_arch_vcpu_in_kernel(vcpu))
+ continue;
+ if (!kvm_vcpu_eligible_for_directed_yield(vcpu))
+ continue;
- yielded = kvm_vcpu_yield_to(vcpu);
- if (yielded > 0) {
- kvm->last_boosted_vcpu = i;
- break;
- } else if (yielded < 0) {
- try--;
- if (!try)
- break;
- }
- }
+ yielded = kvm_vcpu_yield_to(vcpu);
+ if (yielded > 0) {
+ kvm->last_boosted_vcpu = i;
+ break;
+ } else if (yielded < 0 && !try--)
+ break;
}
kvm_vcpu_set_in_spin_loop(me, false);
--
2.13.3
next prev parent reply other threads:[~2017-08-21 20:35 UTC|newest]
Thread overview: 27+ messages / expand[flat|nested] mbox.gz Atom feed top
2017-08-21 20:35 [PATCH RFC v3 0/9] KVM: allow dynamic kvm->vcpus array Radim Krčmář
2017-08-21 20:35 ` [PATCH RFC v3 1/9] KVM: s390: optimize detection of started vcpus Radim Krčmář
2017-08-22 7:26 ` Christian Borntraeger
2017-08-22 11:31 ` David Hildenbrand
2017-08-29 11:23 ` Cornelia Huck
2017-08-29 12:05 ` David Hildenbrand
2017-08-29 12:42 ` Cornelia Huck
2017-08-29 11:26 ` Cornelia Huck
2017-08-21 20:35 ` [PATCH RFC v3 2/9] KVM: arm/arm64: fix vcpu self-detection in vgic_v3_dispatch_sgi() Radim Krčmář
2017-08-22 11:43 ` David Hildenbrand
2017-08-29 10:00 ` Christoffer Dall
2017-08-21 20:35 ` [PATCH RFC v3 3/9] KVM: remember position in kvm->vcpus array Radim Krčmář
2017-08-22 11:44 ` David Hildenbrand
2017-08-29 11:30 ` Cornelia Huck
2017-08-21 20:35 ` [PATCH RFC v3 4/9] KVM: arm/arm64: use locking helpers in kvm_vgic_create() Radim Krčmář
2017-08-22 11:51 ` David Hildenbrand
2017-08-29 10:00 ` Christoffer Dall
2017-08-21 20:35 ` [PATCH RFC v3 5/9] KVM: remove unused __KVM_HAVE_ARCH_VM_ALLOC Radim Krčmář
2017-08-21 20:35 ` Radim Krčmář [this message]
2017-08-22 14:06 ` [PATCH RFC v3 6/9] KVM: rework kvm_vcpu_on_spin loop David Hildenbrand
2017-08-29 15:24 ` Cornelia Huck
2017-08-21 20:35 ` [PATCH RFC v3 7/9] KVM: add kvm_free_vcpus and kvm_arch_free_vcpus Radim Krčmář
2017-08-22 14:18 ` David Hildenbrand
2017-08-29 13:00 ` Cornelia Huck
2017-08-21 20:35 ` [PATCH RFC v3 8/9] KVM: implement kvm_for_each_vcpu with a list Radim Krčmář
2017-08-29 9:58 ` Christoffer Dall
2017-08-21 20:35 ` [PATCH RFC v3 9/9] KVM: split kvm->vcpus into chunks Radim Krčmář
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=20170821203530.9266-7-rkrcmar@redhat.com \
--to=rkrcmar@redhat.com \
--cc=linux-arm-kernel@lists.infradead.org \
/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;
as well as URLs for NNTP newsgroup(s).