All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] kvm: change KVM_REQUEST_MASK to reflect vcpu.requests size
@ 2019-05-21 17:22 Rik van Riel
  2019-05-21 18:28 ` Paolo Bonzini
  0 siblings, 1 reply; 2+ messages in thread
From: Rik van Riel @ 2019-05-21 17:22 UTC (permalink / raw)
  To: Paolo Bonzini; +Cc: kernel-team, kvm, linux-kernel, Radim Krčmář

The code using KVM_REQUEST_MASK uses a pattern reminiscent of a bitmask:

	set_bit(req & KVM_REQUEST_MASK, &vcpu->requests);

However, the first argument passed to set_bit, test_bit, and clear_bit
is a bit number, not a bitmask. That means the current definition would
allow users of kvm_make_request to overflow the vcpu.requests bitmask,
and is confusing to developers examining the code.

Redefine KVM_REQUEST_MASK to reflect the number of bits that actually
fit inside an unsigned long, and add a comment explaining set_bit and
friends take bit numbers, not a bitmask.

Signed-off-by: Rik van Riel <riel@surriel.com>
---
 include/linux/kvm_host.h | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/include/linux/kvm_host.h b/include/linux/kvm_host.h
index 79fa4426509c..d15fb43d7796 100644
--- a/include/linux/kvm_host.h
+++ b/include/linux/kvm_host.h
@@ -138,7 +138,8 @@ static inline bool is_error_page(struct page *page)
 	return IS_ERR(page);
 }
 
-#define KVM_REQUEST_MASK           GENMASK(7,0)
+/* Limit the bit numbers for set_bit etc to fit unsigned long vcpu.requests. */
+#define KVM_REQUEST_MASK           (BITS_PER_LONG-1)
 #define KVM_REQUEST_NO_WAKEUP      BIT(8)
 #define KVM_REQUEST_WAIT           BIT(9)
 /*


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

* Re: [PATCH] kvm: change KVM_REQUEST_MASK to reflect vcpu.requests size
  2019-05-21 17:22 [PATCH] kvm: change KVM_REQUEST_MASK to reflect vcpu.requests size Rik van Riel
@ 2019-05-21 18:28 ` Paolo Bonzini
  0 siblings, 0 replies; 2+ messages in thread
From: Paolo Bonzini @ 2019-05-21 18:28 UTC (permalink / raw)
  To: Rik van Riel; +Cc: kernel-team, kvm, linux-kernel, Radim Krčmář

On 21/05/19 19:22, Rik van Riel wrote:
> The code using KVM_REQUEST_MASK uses a pattern reminiscent of a bitmask:
> 
> 	set_bit(req & KVM_REQUEST_MASK, &vcpu->requests);
> 
> However, the first argument passed to set_bit, test_bit, and clear_bit
> is a bit number, not a bitmask. That means the current definition would
> allow users of kvm_make_request to overflow the vcpu.requests bitmask,
> and is confusing to developers examining the code.

This is true, but the meaning of the masking is that bits above 7 define
extra things to do when sending a request (wait for acknowledge, kick
the recipient CPU).  The fact that the "request number" field is 8 bits
rather than 5 or 6 is just an implementation detail.

If you change it to BITS_PER_LONG-1, the obvious way to read the code
would be that requests 0, 64, 128 are all valid and map to the same request.

Paolo

> Redefine KVM_REQUEST_MASK to reflect the number of bits that actually
> fit inside an unsigned long, and add a comment explaining set_bit and
> friends take bit numbers, not a bitmask.


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

end of thread, other threads:[~2019-05-21 18:28 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2019-05-21 17:22 [PATCH] kvm: change KVM_REQUEST_MASK to reflect vcpu.requests size Rik van Riel
2019-05-21 18:28 ` Paolo Bonzini

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.