x86: Fix unused variable 'msr_val' warning
Subsystems (3)
PATCH 1/1 x86: Fix unused variable 'msr_val' warning
Fixes the following W=1 kernel build warning(s):arch/x86/hyperv/hv_spinlock.c:28:16: warning: variable ‘msr_val’ set but not used [-Wunused-but-set-variable]
unsigned long msr_val;
As Hypervisor Top-Level Functional Specification states in chapter 7.5 Virtual Processor Idle Sleep State, "A partition which possesses the AccessGuestIdleMsr privilege (refer to section 4.2.2) may trigger entry into the virtual processor idle sleep state through a read to the hypervisor-defined MSR HV_X64_MSR_GUEST_IDLE". That means only a read is necessary, msr_val is not uesed, so __maybe_unused should be added.
Reference:
https://docs.microsoft.com/en-us/virtualization/hyper-v-on-windows/reference/tlfs
--- arch/x86/hyperv/hv_spinlock.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
arch/x86/hyperv/hv_spinlock.c +1 -1
@@ -25,7 +25,7 @@ static void hv_qlock_kick(int cpu)static void hv_qlock_wait (u8 *byte,u8 val) { -unsigned long msr_val; +unsigned long msr_val__maybe_unused ;unsigned long flags;if (in_nmi ())
Signature · 1 line
2.17.1
Wei Liu
maintainerQuoted 11 lines · Xu Yihang
Fixes the following W=1 kernel build warning(s):
arch/x86/hyperv/hv_spinlock.c:28:16: warning: variable ‘msr_val’ set but not used [-Wunused-but-set-variable]
unsigned longmsr_val;As Hypervisor Top-Level Functional Specification states in chapter 7.5 Virtual Processor Idle Sleep State, "A partition which possesses the AccessGuestIdleMsr privilege (refer to section 4.2.2) may trigger entry into the virtual processor idle sleep state through a read to the hypervisor-defined MSR
HV_X64_MSR_GUEST_IDLE". That means only a read is necessary,msr_valis not uesed, so__maybe_unusedshould be added.Reference:
https://docs.microsoft.com/en-us/virtualization/hyper-v-on-windows/reference/tlfsReported-by Hulk Robot <[email protected]>Signed-off-by Xu Yihang <[email protected]>
I modified the commit message a bit and queued this up for hyperv-next. Thanks.
Wei.
Quoted 20 lines · Xu Yihang
Fixes the following W=1 kernel build warning(s):
arch/x86/hyperv/hv_spinlock.c:28:16: warning: variable ‘msr_val’ set but not used [-Wunused-but-set-variable]
unsigned longmsr_val;As Hypervisor Top-Level Functional Specification states in chapter 7.5 Virtual Processor Idle Sleep State, "A partition which possesses the AccessGuestIdleMsr privilege (refer to section 4.2.2) may trigger entry into the virtual processor idle sleep state through a read to the hypervisor-defined MSR
HV_X64_MSR_GUEST_IDLE". That means only a read is necessary,msr_valis not uesed, so__maybe_unusedshould be added.Reference:
https://docs.microsoft.com/en-us/virtualization/hyper-v-on-windows/reference/tlfsReported-by Hulk Robot <[email protected]>Signed-off-by Xu Yihang <[email protected]>--- arch/x86/hyperv/hv_spinlock.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)arch/x86/hyperv/hv_spinlock.c +0
-0
arch/x86/hyperv/hv_spinlock.c static void hv_qlock_kick(int cpu) · line 29
Quoted 6 lines
arch/x86/hyperv/hv_spinlock.c +1
-1@@ -25,7 +25,7 @@ static void hv_qlock_kick(int cpu)static void hv_qlock_wait (u8 *byte,u8 val) { -unsigned long msr_val; +unsigned long msr_val__maybe_unused ;unsigned long flags;
Please don't add new __maybe_unused annotations to the x86 tree - improve the flow instead to help GCC recognize the initialization sequence better.
Thanks,
Ingo
From: Ingo Molnar <[email protected]> Sent: Monday, March 22, 2021 2:08 PM
Quoted 40 lines · Xu Yihang
Quoted 3 lines
Fixes the following W=1 kernel build warning(s):
arch/x86/hyperv/hv_spinlock.c:28:16: warning: variable 'msr_val' set but not used [-Wunused-but-set-variable]
Quoted 3 lines
unsigned long
msr_val;As Hypervisor Top-Level Functional Specification states in chapter 7.5 Virtual Processor
Idle Sleep State, "A partition which possesses the AccessGuestIdleMsr privilege (refer to section 4.2.2) may trigger entry into the virtual processor idle sleep state through a read to the hypervisor-defined MSR
HV_X64_MSR_GUEST_IDLE". That means only a read is necessary,msr_valis not uesed, so__maybe_unusedshould be added.Quoted 16 lines
Reference:
https://docs.microsoft.com/en-us/virtualization/hyper-v-on-windows/reference/tlfs
Reported-by Hulk Robot <[email protected]>Signed-off-by Xu Yihang <[email protected]>--- arch/x86/hyperv/hv_spinlock.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)arch/x86/hyperv/hv_spinlock.c +0
-0arch/x86/hyperv/hv_spinlock.c static void hv_qlock_kick(int cpu) · line 29
Quoted 6 lines
arch/x86/hyperv/hv_spinlock.c +1
-1@@ -25,7 +25,7 @@ static void hv_qlock_kick(int cpu)static void hv_qlock_wait (u8 *byte,u8 val) { -unsigned long msr_val; +unsigned long msr_val__maybe_unused ;unsigned long flags;Please don't add new
__maybe_unusedannotations to the x86 tree - improve the flow instead to help GCC recognize the initialization sequence better.
Quoted 3 lines
Thanks,
Ingo
Could you elaborate on the thinking here, or point to some written
discussion? I'm just curious. In this particular case, it's not a problem
with the flow or gcc detection. This code really does read an MSR and ignore that value that is read, so it's not clear how gcc would ever figure out that's OK.
Michael
Quoted 43 lines · Michael Kelley
From: Ingo Molnar <[email protected]> Sent: Monday, March 22, 2021 2:08 PM
Quoted 42 lines · Xu Yihang
Quoted 3 lines
Fixes the following W=1 kernel build warning(s):
arch/x86/hyperv/hv_spinlock.c:28:16: warning: variable 'msr_val' set but not used [-Wunused-but-set-variable]
Quoted 3 lines
unsigned long
msr_val;As Hypervisor Top-Level Functional Specification states in chapter 7.5 Virtual Processor
Idle Sleep State, "A partition which possesses the AccessGuestIdleMsr privilege (refer to section 4.2.2) may trigger entry into the virtual processor idle sleep state through a read to the hypervisor-defined MSR
HV_X64_MSR_GUEST_IDLE". That means only a read is necessary,msr_valis not uesed, so__maybe_unusedshould be added.Quoted 16 lines
Reference:
https://docs.microsoft.com/en-us/virtualization/hyper-v-on-windows/reference/tlfs
Reported-by Hulk Robot <[email protected]>Signed-off-by Xu Yihang <[email protected]>--- arch/x86/hyperv/hv_spinlock.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)arch/x86/hyperv/hv_spinlock.c +0
-0arch/x86/hyperv/hv_spinlock.c static void hv_qlock_kick(int cpu) · line 29
Quoted 6 lines
arch/x86/hyperv/hv_spinlock.c +1
-1@@ -25,7 +25,7 @@ static void hv_qlock_kick(int cpu)static void hv_qlock_wait (u8 *byte,u8 val) { -unsigned long msr_val; +unsigned long msr_val__maybe_unused ;unsigned long flags;Please don't add new
__maybe_unusedannotations to the x86 tree - improve the flow instead to help GCC recognize the initialization sequence better.Thanks,
Quoted 7 lines
Quoted 1 line
Ingo
Could you elaborate on the thinking here, or point to some written
discussion? I'm just curious. In this particular case, it's not a problem
with the flow or gcc detection. This code really does read an MSR and ignore that value that is read, so it's not clear how gcc would ever figure out that's OK.
Yeah, so the canonical way to signal that the msr_val isn't used would be to rewrite this as:
if (READ_ONCE(*byte) == val) {
unsigned long msr_val;
rdmsrl(HV_X64_MSR_GUEST_IDLE, msr_val);
(void)msr_val;
}(Also see the patch below - untested.)
This makes it abundantly clear that the rdmsr() msr_val return value is not 'maybe' unused, but totally intentionally skipped.
Thanks,
Ingo
arch/x86/hyperv/hv_spinlock.c | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-)
arch/x86/hyperv/hv_spinlock.c +7 -2
@@ -25,7 +25,6 @@ static void hv_qlock_kick(int cpu)static void hv_qlock_wait (u8 *byte,u8 val) { -unsigned long msr_val;unsigned long flags;if (in_nmi ())
@@ -48,8 +47,14 @@ static void hv_qlock_wait(u8 *byte, u8 val)/* * Only issue the rdmsrl() when the lock state has not changed. */ -if (READ_ONCE (*byte) == val) +if (READ_ONCE (*byte) == val) { +unsigned long msr_val; +rdmsrl (HV_X64_MSR_GUEST_IDLE , msr_val); + + (void )msr_val; + } +local_irq_restore (flags); }
Wei Liu
maintainerQuoted 66 lines · Ingo Molnar
Quoted 43 lines · Michael Kelley
From: Ingo Molnar <[email protected]> Sent: Monday, March 22, 2021 2:08 PM
Quoted 42 lines · Xu Yihang
Quoted 3 lines
Fixes the following W=1 kernel build warning(s):
arch/x86/hyperv/hv_spinlock.c:28:16: warning: variable 'msr_val' set but not used [-Wunused-but-set-variable]
Quoted 3 lines
unsigned long
msr_val;As Hypervisor Top-Level Functional Specification states in chapter 7.5 Virtual Processor
Idle Sleep State, "A partition which possesses the AccessGuestIdleMsr privilege (refer to section 4.2.2) may trigger entry into the virtual processor idle sleep state through a read to the hypervisor-defined MSR
HV_X64_MSR_GUEST_IDLE". That means only a read is necessary,msr_valis not uesed, so__maybe_unusedshould be added.Quoted 16 lines
Reference:
https://docs.microsoft.com/en-us/virtualization/hyper-v-on-windows/reference/tlfs
Reported-by Hulk Robot <[email protected]>Signed-off-by Xu Yihang <[email protected]>--- arch/x86/hyperv/hv_spinlock.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)arch/x86/hyperv/hv_spinlock.c +0
-0arch/x86/hyperv/hv_spinlock.c static void hv_qlock_kick(int cpu) · line 29
Quoted 6 lines
arch/x86/hyperv/hv_spinlock.c +1
-1@@ -25,7 +25,7 @@ static void hv_qlock_kick(int cpu)static void hv_qlock_wait (u8 *byte,u8 val) { -unsigned long msr_val; +unsigned long msr_val__maybe_unused ;unsigned long flags;Please don't add new
__maybe_unusedannotations to the x86 tree - improve the flow instead to help GCC recognize the initialization sequence better.Thanks,
Quoted 7 lines
Quoted 1 line
Ingo
Could you elaborate on the thinking here, or point to some written
discussion? I'm just curious. In this particular case, it's not a problem
with the flow or gcc detection. This code really does read an MSR and ignore that value that is read, so it's not clear how gcc would ever figure out that's OK.Yeah, so the canonical way to signal that the
msr_valisn't used would be to rewrite this as:if (READ_ONCE(*byte) == val) { unsigned long msr_val; rdmsrl(HV_X64_MSR_GUEST_IDLE, msr_val); (void)msr_val; }
Quoted 9 lines
(Also see the patch below - untested.)
This makes it abundantly clear that the
rdmsr()msr_valreturn value is not 'maybe' unused, but totally intentionally skipped.Thanks,
Ingo
Thank you for the advice, Ingo.
Wei.
PATCH 1/1 x86: Fix unused variable 'msr_val' warning
In reply to Xu YihangFixes the following W=1 kernel build warning(s):arch/x86/hyperv/hv_spinlock.c:28:16: warning: variable ‘msr_val’ set but not used [-Wunused-but-set-variable]
unsigned long msr_val;
As Hypervisor Top-Level Functional Specification states in chapter 7.5 Virtual Processor Idle Sleep State, "A partition which possesses the AccessGuestIdleMsr privilege (refer to section 4.2.2) may trigger entry into the virtual processor idle sleep state through a read to the hypervisor-defined MSR HV_X64_MSR_GUEST_IDLE". That means only a read is necessary, msr_val is not uesed, so potentially cast to void in order to silent this warning.
Reference:
https://docs.microsoft.com/en-us/virtualization/hyper-v-on-windows/reference/tlfs
--- arch/x86/hyperv/hv_spinlock.c | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-)
arch/x86/hyperv/hv_spinlock.c +6 -2
@@ -25,7 +25,6 @@ static void hv_qlock_kick(int cpu)static void hv_qlock_wait (u8 *byte,u8 val) { -unsigned long msr_val;unsigned long flags;if (in_nmi ())
@@ -48,8 +47,13 @@ static void hv_qlock_wait(u8 *byte, u8 val)/* * Only issue the rdmsrl() when the lock state has not changed. */ -if (READ_ONCE (*byte) == val) +if (READ_ONCE (*byte) == val) { +unsigned long msr_val; +rdmsrl (HV_X64_MSR_GUEST_IDLE , msr_val); + + (void )msr_val; + }local_irq_restore (flags); }
Signature · 1 line
2.17.1
Quoted 11 lines · Xu Yihang
Fixes the following W=1 kernel build warning(s):
arch/x86/hyperv/hv_spinlock.c:28:16: warning: variable ‘msr_val’ set but not used [-Wunused-but-set-variable]
unsigned longmsr_val;As Hypervisor Top-Level Functional Specification states in chapter 7.5 Virtual Processor Idle Sleep State, "A partition which possesses the AccessGuestIdleMsr privilege (refer to section 4.2.2) may trigger entry into the virtual processor idle sleep state through a read to the hypervisor-defined MSR
HV_X64_MSR_GUEST_IDLE". That means only a read is necessary,msr_valis not uesed, so potentially cast to void in order to silent this warning.Reference:
https://docs.microsoft.com/en-us/virtualization/hyper-v-on-windows/reference/tlfsReported-by Hulk Robot <[email protected]>Signed-off-by Xu Yihang <[email protected]>
Applied to hyperv-next. Thanks.