diff mbox series

[v3,13/35] KVM: x86: hyper-v: optimize and cleanup kvm_hv_process_stimers()

Message ID 20231212022749.625238-14-yury.norov@gmail.com (mailing list archive)
State New, archived
Headers show
Series bitops: add atomic find_bit() operations | expand

Commit Message

Yury Norov Dec. 12, 2023, 2:27 a.m. UTC
The function traverses stimer_pending_bitmap in a for-loop bit by bit.
Simplify it by using atomic find_and_set_bit().

While here, refactor the logic by decreasing indentation level.

CC: Sean Christopherson <seanjc@google.com>
Signed-off-by: Yury Norov <yury.norov@gmail.com>
Reviewed-by: Vitaly Kuznetsov <vkuznets@redhat.com>
---
 arch/x86/kvm/hyperv.c | 40 ++++++++++++++++++++--------------------
 1 file changed, 20 insertions(+), 20 deletions(-)

Comments

Sean Christopherson Dec. 12, 2023, 5:22 p.m. UTC | #1
On Mon, Dec 11, 2023, Yury Norov wrote:
> The function traverses stimer_pending_bitmap in a for-loop bit by bit.
> Simplify it by using atomic find_and_set_bit().

for_each_test_and_clear_bit(), not find_and_set_bit().

It might also be nice to call out that there are only 4 bits, i.e. that using
for_each_test_and_clear_bit() will still generate inline code.  Definitely not
mandatory though, just nice to have (I highly doubt this code would be sensitive
to using less optimal code).

> While here, refactor the logic by decreasing indentation level.
> 
> CC: Sean Christopherson <seanjc@google.com>
> Signed-off-by: Yury Norov <yury.norov@gmail.com>
> Reviewed-by: Vitaly Kuznetsov <vkuznets@redhat.com>
> ---
>  arch/x86/kvm/hyperv.c | 40 ++++++++++++++++++++--------------------

This doesn't conflict with any of the in-flight Hyper-V changes, so with a fixed
changelog, feel free to take this through the bitmap tree.

Acked-by: Sean Christopherson <seanjc@google.com>
Yury Norov Dec. 12, 2023, 5:35 p.m. UTC | #2
On Tue, Dec 12, 2023 at 09:22:36AM -0800, Sean Christopherson wrote:
> On Mon, Dec 11, 2023, Yury Norov wrote:
> > The function traverses stimer_pending_bitmap in a for-loop bit by bit.
> > Simplify it by using atomic find_and_set_bit().
> 
> for_each_test_and_clear_bit(), not find_and_set_bit().
> 
> It might also be nice to call out that there are only 4 bits, i.e. that using
> for_each_test_and_clear_bit() will still generate inline code.  Definitely not
> mandatory though, just nice to have (I highly doubt this code would be sensitive
> to using less optimal code).

Sure, will do.
 
> > While here, refactor the logic by decreasing indentation level.
> > 
> > CC: Sean Christopherson <seanjc@google.com>
> > Signed-off-by: Yury Norov <yury.norov@gmail.com>
> > Reviewed-by: Vitaly Kuznetsov <vkuznets@redhat.com>
> > ---
> >  arch/x86/kvm/hyperv.c | 40 ++++++++++++++++++++--------------------
> 
> This doesn't conflict with any of the in-flight Hyper-V changes, so with a fixed
> changelog, feel free to take this through the bitmap tree.
> 
> Acked-by: Sean Christopherson <seanjc@google.com>

Thank you!
diff mbox series

Patch

diff --git a/arch/x86/kvm/hyperv.c b/arch/x86/kvm/hyperv.c
index 238afd7335e4..d541524ca49f 100644
--- a/arch/x86/kvm/hyperv.c
+++ b/arch/x86/kvm/hyperv.c
@@ -870,27 +870,27 @@  void kvm_hv_process_stimers(struct kvm_vcpu *vcpu)
 	if (!hv_vcpu)
 		return;
 
-	for (i = 0; i < ARRAY_SIZE(hv_vcpu->stimer); i++)
-		if (test_and_clear_bit(i, hv_vcpu->stimer_pending_bitmap)) {
-			stimer = &hv_vcpu->stimer[i];
-			if (stimer->config.enable) {
-				exp_time = stimer->exp_time;
-
-				if (exp_time) {
-					time_now =
-						get_time_ref_counter(vcpu->kvm);
-					if (time_now >= exp_time)
-						stimer_expiration(stimer);
-				}
-
-				if ((stimer->config.enable) &&
-				    stimer->count) {
-					if (!stimer->msg_pending)
-						stimer_start(stimer);
-				} else
-					stimer_cleanup(stimer);
-			}
+	for_each_test_and_clear_bit(i, hv_vcpu->stimer_pending_bitmap,
+				    ARRAY_SIZE(hv_vcpu->stimer)) {
+		stimer = &hv_vcpu->stimer[i];
+		if (!stimer->config.enable)
+			continue;
+
+		exp_time = stimer->exp_time;
+
+		if (exp_time) {
+			time_now = get_time_ref_counter(vcpu->kvm);
+			if (time_now >= exp_time)
+				stimer_expiration(stimer);
 		}
+
+		if (stimer->config.enable && stimer->count) {
+			if (!stimer->msg_pending)
+				stimer_start(stimer);
+		} else {
+			stimer_cleanup(stimer);
+		}
+	}
 }
 
 void kvm_hv_vcpu_uninit(struct kvm_vcpu *vcpu)