public inbox for kvm@vger.kernel.org
 help / color / mirror / Atom feed
* [PATCH] KVM: VMX: Eliminate warnings by sparse in vmcs12.c and hyperv_evmcs.c
@ 2026-03-11  1:59 isaku.yamahata
  2026-03-12 17:49 ` Sean Christopherson
  0 siblings, 1 reply; 3+ messages in thread
From: isaku.yamahata @ 2026-03-11  1:59 UTC (permalink / raw)
  To: kvm
  Cc: isaku.yamahata, isaku.yamahata, Paolo Bonzini,
	Sean Christopherson, kernel test robot

From: Isaku Yamahata <isaku.yamahata@intel.com>

Make ROL16() explicitly mask instead of the truncation by u16 cast.
Sparse complains as follows for vmcs12.c and hyperv_evmcs.c.
  > vmcs12.c:17:9: warning: cast truncates bits from constant value
  > (20002 becomes 2)

It is because ROL16(val, n) includes "(u16)(val) << (n)" whose type is int,
not u16, due to integer promotion even with a u16 cast.  The outermost u16
cast in ROL16() may truncate it.  Explicitly mask it to avoid truncation by
the u16 cast.

Reported-by: kernel test robot <lkp@intel.com>
Closes: https://lore.kernel.org/oe-kbuild-all/202603061942.wdSsERnL-lkp@intel.com/
Signed-off-by: Isaku Yamahata <isaku.yamahata@intel.com>
---
 arch/x86/kvm/vmx/vmcs.h | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/arch/x86/kvm/vmx/vmcs.h b/arch/x86/kvm/vmx/vmcs.h
index 1f16ddeae9cb..b5b5f4870655 100644
--- a/arch/x86/kvm/vmx/vmcs.h
+++ b/arch/x86/kvm/vmx/vmcs.h
@@ -18,7 +18,8 @@
  * wasting too much memory, while the "algorithm" is fast enough to be used to
  * lookup vmcs12 fields on-demand, e.g. for emulation.
  */
-#define ROL16(val, n) ((u16)(((u16)(val) << (n)) | ((u16)(val) >> (16 - (n)))))
+#define ROL16(val, n) ((u16)((((u16)(val) << (n)) & GENMASK_U16(15, 0)) | \
+			     ((u16)(val) >> (16 - (n)))))
 #define VMCS12_IDX_TO_ENC(idx) ROL16(idx, 10)
 #define ENC_TO_VMCS12_IDX(enc) ROL16(enc, 6)
 

base-commit: 5128b972fb2801ad9aca54d990a75611ab5283a9
-- 
2.45.2


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

* Re: [PATCH] KVM: VMX: Eliminate warnings by sparse in vmcs12.c and hyperv_evmcs.c
  2026-03-11  1:59 [PATCH] KVM: VMX: Eliminate warnings by sparse in vmcs12.c and hyperv_evmcs.c isaku.yamahata
@ 2026-03-12 17:49 ` Sean Christopherson
  2026-03-12 19:09   ` Isaku Yamahata
  0 siblings, 1 reply; 3+ messages in thread
From: Sean Christopherson @ 2026-03-12 17:49 UTC (permalink / raw)
  To: isaku.yamahata; +Cc: kvm, isaku.yamahata, Paolo Bonzini, kernel test robot

On Tue, Mar 10, 2026, isaku.yamahata@intel.com wrote:
> From: Isaku Yamahata <isaku.yamahata@intel.com>
> 
> Make ROL16() explicitly mask instead of the truncation by u16 cast.
> Sparse complains as follows for vmcs12.c and hyperv_evmcs.c.
>   > vmcs12.c:17:9: warning: cast truncates bits from constant value
>   > (20002 becomes 2)
> 
> It is because ROL16(val, n) includes "(u16)(val) << (n)" whose type is int,
> not u16, due to integer promotion even with a u16 cast.  The outermost u16
> cast in ROL16() may truncate it.  Explicitly mask it to avoid truncation by
> the u16 cast.

Honestly, I'm inclined to ignore sparse.  The code is correct as-is, and adding
the GENMASK just makes it less readable IMO.

> Reported-by: kernel test robot <lkp@intel.com>
> Closes: https://lore.kernel.org/oe-kbuild-all/202603061942.wdSsERnL-lkp@intel.com/
> Signed-off-by: Isaku Yamahata <isaku.yamahata@intel.com>
> ---
>  arch/x86/kvm/vmx/vmcs.h | 3 ++-
>  1 file changed, 2 insertions(+), 1 deletion(-)
> 
> diff --git a/arch/x86/kvm/vmx/vmcs.h b/arch/x86/kvm/vmx/vmcs.h
> index 1f16ddeae9cb..b5b5f4870655 100644
> --- a/arch/x86/kvm/vmx/vmcs.h
> +++ b/arch/x86/kvm/vmx/vmcs.h
> @@ -18,7 +18,8 @@
>   * wasting too much memory, while the "algorithm" is fast enough to be used to
>   * lookup vmcs12 fields on-demand, e.g. for emulation.
>   */
> -#define ROL16(val, n) ((u16)(((u16)(val) << (n)) | ((u16)(val) >> (16 - (n)))))
> +#define ROL16(val, n) ((u16)((((u16)(val) << (n)) & GENMASK_U16(15, 0)) | \
> +			     ((u16)(val) >> (16 - (n)))))
>  #define VMCS12_IDX_TO_ENC(idx) ROL16(idx, 10)
>  #define ENC_TO_VMCS12_IDX(enc) ROL16(enc, 6)
>  
> 
> base-commit: 5128b972fb2801ad9aca54d990a75611ab5283a9
> -- 
> 2.45.2
> 

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

* Re: [PATCH] KVM: VMX: Eliminate warnings by sparse in vmcs12.c and hyperv_evmcs.c
  2026-03-12 17:49 ` Sean Christopherson
@ 2026-03-12 19:09   ` Isaku Yamahata
  0 siblings, 0 replies; 3+ messages in thread
From: Isaku Yamahata @ 2026-03-12 19:09 UTC (permalink / raw)
  To: Sean Christopherson
  Cc: isaku.yamahata, kvm, isaku.yamahata, Paolo Bonzini,
	kernel test robot, isaku.yamahata

On Thu, Mar 12, 2026 at 10:49:16AM -0700,
Sean Christopherson <seanjc@google.com> wrote:

> On Tue, Mar 10, 2026, isaku.yamahata@intel.com wrote:
> > From: Isaku Yamahata <isaku.yamahata@intel.com>
> > 
> > Make ROL16() explicitly mask instead of the truncation by u16 cast.
> > Sparse complains as follows for vmcs12.c and hyperv_evmcs.c.
> >   > vmcs12.c:17:9: warning: cast truncates bits from constant value
> >   > (20002 becomes 2)
> > 
> > It is because ROL16(val, n) includes "(u16)(val) << (n)" whose type is int,
> > not u16, due to integer promotion even with a u16 cast.  The outermost u16
> > cast in ROL16() may truncate it.  Explicitly mask it to avoid truncation by
> > the u16 cast.
> 
> Honestly, I'm inclined to ignore sparse.  The code is correct as-is, and adding
> the GENMASK just makes it less readable IMO.

Ok, I'm fine with ignoring those warnings. I just wanted to silence
kernel test robot and avoid sparse "too many warnings" to stop more
warnings.  (No other warning found with this change.)
-- 
Isaku Yamahata <isaku.yamahata@intel.com>

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

end of thread, other threads:[~2026-03-12 19:09 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-03-11  1:59 [PATCH] KVM: VMX: Eliminate warnings by sparse in vmcs12.c and hyperv_evmcs.c isaku.yamahata
2026-03-12 17:49 ` Sean Christopherson
2026-03-12 19:09   ` Isaku Yamahata

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox