All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] kvm: x86: simplify kvm_vector_to_index()
@ 2025-07-20  1:58 Yury Norov
  2025-08-19 23:12 ` Sean Christopherson
  0 siblings, 1 reply; 6+ messages in thread
From: Yury Norov @ 2025-07-20  1:58 UTC (permalink / raw)
  To: Sean Christopherson, Paolo Bonzini, Thomas Gleixner, Ingo Molnar,
	Borislav Petkov, Dave Hansen, H. Peter Anvin, x86, kvm,
	linux-kernel
  Cc: Yury Norov

Use find_nth_bit() and make the function almost a one-liner.

Signed-off-by: Yury Norov <yury.norov@gmail.com>
---
 arch/x86/kvm/lapic.c | 12 +++---------
 1 file changed, 3 insertions(+), 9 deletions(-)

diff --git a/arch/x86/kvm/lapic.c b/arch/x86/kvm/lapic.c
index 73418dc0ebb2..6c4ec016de6a 100644
--- a/arch/x86/kvm/lapic.c
+++ b/arch/x86/kvm/lapic.c
@@ -1106,16 +1106,10 @@ EXPORT_SYMBOL_GPL(kvm_apic_match_dest);
 int kvm_vector_to_index(u32 vector, u32 dest_vcpus,
 		       const unsigned long *bitmap, u32 bitmap_size)
 {
-	u32 mod;
-	int i, idx = -1;
-
-	mod = vector % dest_vcpus;
-
-	for (i = 0; i <= mod; i++) {
-		idx = find_next_bit(bitmap, bitmap_size, idx + 1);
-		BUG_ON(idx == bitmap_size);
-	}
+	u32 mod = vector % dest_vcpus;
+	int idx = find_nth_bit(bitmap, bitmap_size, mod);
 
+	BUG_ON(idx >= bitmap_size);
 	return idx;
 }
 
-- 
2.43.0


^ permalink raw reply related	[flat|nested] 6+ messages in thread

* Re: [PATCH] kvm: x86: simplify kvm_vector_to_index()
  2025-07-20  1:58 [PATCH] kvm: x86: simplify kvm_vector_to_index() Yury Norov
@ 2025-08-19 23:12 ` Sean Christopherson
  2025-08-20 13:42   ` Yury Norov
  0 siblings, 1 reply; 6+ messages in thread
From: Sean Christopherson @ 2025-08-19 23:12 UTC (permalink / raw)
  To: Sean Christopherson, Paolo Bonzini, Thomas Gleixner, Ingo Molnar,
	Borislav Petkov, Dave Hansen, H. Peter Anvin, x86, kvm,
	linux-kernel, Yury Norov

On Sat, 19 Jul 2025 21:58:45 -0400, Yury Norov wrote:
> Use find_nth_bit() and make the function almost a one-liner.

Applied to kvm-x86 misc, thanks!

P.S. I'm amazed you could decipher the intent of the code.  Even with your
     patch, it took me 10+ minutes to understand the "logic".

[1/1] kvm: x86: simplify kvm_vector_to_index()
      https://github.com/kvm-x86/linux/commit/cc63f918a215

--
https://github.com/kvm-x86/linux/tree/next

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH] kvm: x86: simplify kvm_vector_to_index()
  2025-08-19 23:12 ` Sean Christopherson
@ 2025-08-20 13:42   ` Yury Norov
  2025-08-20 14:01     ` Juergen Gross
  0 siblings, 1 reply; 6+ messages in thread
From: Yury Norov @ 2025-08-20 13:42 UTC (permalink / raw)
  To: Sean Christopherson
  Cc: Paolo Bonzini, Thomas Gleixner, Ingo Molnar, Borislav Petkov,
	Dave Hansen, H. Peter Anvin, x86, kvm, linux-kernel

On Tue, Aug 19, 2025 at 04:12:11PM -0700, Sean Christopherson wrote:
> On Sat, 19 Jul 2025 21:58:45 -0400, Yury Norov wrote:
> > Use find_nth_bit() and make the function almost a one-liner.
> 
> Applied to kvm-x86 misc, thanks!
> 
> P.S. I'm amazed you could decipher the intent of the code.  Even with your
>      patch, it took me 10+ minutes to understand the "logic".

Thanks Sean. :)

> [1/1] kvm: x86: simplify kvm_vector_to_index()
>       https://github.com/kvm-x86/linux/commit/cc63f918a215
> 
> --
> https://github.com/kvm-x86/linux/tree/next

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH] kvm: x86: simplify kvm_vector_to_index()
  2025-08-20 13:42   ` Yury Norov
@ 2025-08-20 14:01     ` Juergen Gross
  2025-08-20 14:12       ` Yury Norov
  0 siblings, 1 reply; 6+ messages in thread
From: Juergen Gross @ 2025-08-20 14:01 UTC (permalink / raw)
  To: Yury Norov, Sean Christopherson
  Cc: Paolo Bonzini, Thomas Gleixner, Ingo Molnar, Borislav Petkov,
	Dave Hansen, H. Peter Anvin, x86, kvm, linux-kernel


[-- Attachment #1.1.1: Type: text/plain, Size: 963 bytes --]

On 20.08.25 15:42, Yury Norov wrote:
> On Tue, Aug 19, 2025 at 04:12:11PM -0700, Sean Christopherson wrote:
>> On Sat, 19 Jul 2025 21:58:45 -0400, Yury Norov wrote:
>>> Use find_nth_bit() and make the function almost a one-liner.
>>
>> Applied to kvm-x86 misc, thanks!
>>
>> P.S. I'm amazed you could decipher the intent of the code.  Even with your
>>       patch, it took me 10+ minutes to understand the "logic".
> 
> Thanks Sean. :)
> 
>> [1/1] kvm: x86: simplify kvm_vector_to_index()
>>        https://github.com/kvm-x86/linux/commit/cc63f918a215

Is this really correct?

The original code has:

	for (i = 0; i <= mod; i++)

(note the "<=").

So it will find the (mod + 1)th bit set, so shouldn't it use

	idx = find_nth_bit(bitmap, bitmap_size, (vector % dest_vcpus) + 1);

instead?

My remark assumes that find_nth_bit(bitmap, bitmap_size, 1) will return the
same value as find_first_bit(bitmap, bitmap_size).


Juergen

[-- Attachment #1.1.2: OpenPGP public key --]
[-- Type: application/pgp-keys, Size: 3743 bytes --]

[-- Attachment #2: OpenPGP digital signature --]
[-- Type: application/pgp-signature, Size: 495 bytes --]

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH] kvm: x86: simplify kvm_vector_to_index()
  2025-08-20 14:01     ` Juergen Gross
@ 2025-08-20 14:12       ` Yury Norov
  2025-08-20 14:19         ` Jürgen Groß
  0 siblings, 1 reply; 6+ messages in thread
From: Yury Norov @ 2025-08-20 14:12 UTC (permalink / raw)
  To: Juergen Gross
  Cc: Sean Christopherson, Paolo Bonzini, Thomas Gleixner, Ingo Molnar,
	Borislav Petkov, Dave Hansen, H. Peter Anvin, x86, kvm,
	linux-kernel

On Wed, Aug 20, 2025 at 04:01:22PM +0200, Juergen Gross wrote:
> On 20.08.25 15:42, Yury Norov wrote:
> > On Tue, Aug 19, 2025 at 04:12:11PM -0700, Sean Christopherson wrote:
> > > On Sat, 19 Jul 2025 21:58:45 -0400, Yury Norov wrote:
> > > > Use find_nth_bit() and make the function almost a one-liner.
> > > 
> > > Applied to kvm-x86 misc, thanks!
> > > 
> > > P.S. I'm amazed you could decipher the intent of the code.  Even with your
> > >       patch, it took me 10+ minutes to understand the "logic".
> > 
> > Thanks Sean. :)
> > 
> > > [1/1] kvm: x86: simplify kvm_vector_to_index()
> > >        https://github.com/kvm-x86/linux/commit/cc63f918a215
> 
> Is this really correct?
> 
> The original code has:
> 
> 	for (i = 0; i <= mod; i++)
> 
> (note the "<=").
> 
> So it will find the (mod + 1)th bit set, so shouldn't it use
> 
> 	idx = find_nth_bit(bitmap, bitmap_size, (vector % dest_vcpus) + 1);
> 
> instead?
> 
> My remark assumes that find_nth_bit(bitmap, bitmap_size, 1) will return the
> same value as find_first_bit(bitmap, bitmap_size).

find_nth_bit indexes those bits starting from 0, so 

find_nth_bit(bitmap, nbits, 0) == find_first_bit(bitmap, nbits)
find_nth_bit(bitmap, nbits, 1) == find_next_bit(bitmap, nbits,
                                        find_first_bit(bitmap, nbits))

And so on. Check test_find_nth_bit() for the examples.

Also, bitmap_size has a different meaning, so let's refer 'nbits'
instead.

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH] kvm: x86: simplify kvm_vector_to_index()
  2025-08-20 14:12       ` Yury Norov
@ 2025-08-20 14:19         ` Jürgen Groß
  0 siblings, 0 replies; 6+ messages in thread
From: Jürgen Groß @ 2025-08-20 14:19 UTC (permalink / raw)
  To: Yury Norov
  Cc: Sean Christopherson, Paolo Bonzini, Thomas Gleixner, Ingo Molnar,
	Borislav Petkov, Dave Hansen, H. Peter Anvin, x86, kvm,
	linux-kernel


[-- Attachment #1.1.1: Type: text/plain, Size: 1436 bytes --]

On 20.08.25 16:12, Yury Norov wrote:
> On Wed, Aug 20, 2025 at 04:01:22PM +0200, Juergen Gross wrote:
>> On 20.08.25 15:42, Yury Norov wrote:
>>> On Tue, Aug 19, 2025 at 04:12:11PM -0700, Sean Christopherson wrote:
>>>> On Sat, 19 Jul 2025 21:58:45 -0400, Yury Norov wrote:
>>>>> Use find_nth_bit() and make the function almost a one-liner.
>>>>
>>>> Applied to kvm-x86 misc, thanks!
>>>>
>>>> P.S. I'm amazed you could decipher the intent of the code.  Even with your
>>>>        patch, it took me 10+ minutes to understand the "logic".
>>>
>>> Thanks Sean. :)
>>>
>>>> [1/1] kvm: x86: simplify kvm_vector_to_index()
>>>>         https://github.com/kvm-x86/linux/commit/cc63f918a215
>>
>> Is this really correct?
>>
>> The original code has:
>>
>> 	for (i = 0; i <= mod; i++)
>>
>> (note the "<=").
>>
>> So it will find the (mod + 1)th bit set, so shouldn't it use
>>
>> 	idx = find_nth_bit(bitmap, bitmap_size, (vector % dest_vcpus) + 1);
>>
>> instead?
>>
>> My remark assumes that find_nth_bit(bitmap, bitmap_size, 1) will return the
>> same value as find_first_bit(bitmap, bitmap_size).
> 
> find_nth_bit indexes those bits starting from 0, so
Ah, okay. That was the part I wasn't sure about, hence the addition of
my assumption regarding the semantics of find_nth_bit() (looking into the
__find_nth_bit() code didn't make that obvious at once).

Thanks for the clarification.


Juergen

[-- Attachment #1.1.2: OpenPGP public key --]
[-- Type: application/pgp-keys, Size: 3743 bytes --]

[-- Attachment #2: OpenPGP digital signature --]
[-- Type: application/pgp-signature, Size: 495 bytes --]

^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2025-08-20 14:19 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-07-20  1:58 [PATCH] kvm: x86: simplify kvm_vector_to_index() Yury Norov
2025-08-19 23:12 ` Sean Christopherson
2025-08-20 13:42   ` Yury Norov
2025-08-20 14:01     ` Juergen Gross
2025-08-20 14:12       ` Yury Norov
2025-08-20 14:19         ` Jürgen Groß

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.