Skip to contentSkip to conversation

KVM: arm64: fix u64_replace_bits() usage

PATCH In review Arnd Bergmann 5 participants 6 messages Jul 11, 2025

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 KVM: arm64: fix u64_replace_bits() usage

From: Arnd Bergmann <[email protected]>

u64_replace_bits() returns a modified word but does not actually modify its argument, as pointed out by this new warning:

arch/arm64/kvm/sys_regs.c: In function 'access_mdcr':
arch/arm64/kvm/sys_regs.c:2654:17: error: ignoring return value of 'u64_replace_bits' declared with attribute 'warn_unused_result' [-Werror=unused-result]
2654 | u64_replace_bits(val, hpmn, MDCR_EL2_HPMN);

The intention here must have been to update 'val', so do that instead.

Fixes efff9dd2fee7 ("KVM: arm64: Handle out-of-bound write to MDCR_EL2.HPMN")
---
 arch/arm64/kvm/sys_regs.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
arch/arm64/kvm/sys_regs.c +1 -1
index 33aa4f5071b8..793fb19bebd6 100644
@@ -2651,7 +2651,7 @@ static bool access_mdcr(struct kvm_vcpu *vcpu,
 	 */
 	if (hpmn > vcpu->kvm->arch.nr_pmu_counters) {
 		hpmn = vcpu->kvm->arch.nr_pmu_counters;
-		u64_replace_bits(val, hpmn, MDCR_EL2_HPMN);
+		val = u64_replace_bits(val, hpmn, MDCR_EL2_HPMN);
 	}
 
 	__vcpu_assign_sys_reg(vcpu, MDCR_EL2, val);
Signature · 1 line
2.39.5
reviewer
#1 lore raw
Quoted 22 lines · Arnd Bergmann

From: Arnd Bergmann <[email protected]>

u64_replace_bits() returns a modified word but does not actually modify its argument, as pointed out by this new warning:

arch/arm64/kvm/sys_regs.c: In function 'access_mdcr':
arch/arm64/kvm/sys_regs.c:2654:17: error: ignoring return value of 'u64_replace_bits' declared with attribute 'warn_unused_result' [-Werror=unused-result]
2654 | u64_replace_bits(val, hpmn, MDCR_EL2_HPMN);

The intention here must have been to update 'val', so do that instead.

Fixes efff9dd2fee7 ("KVM: arm64: Handle out-of-bound write to MDCR_EL2.HPMN")
---
 arch/arm64/kvm/sys_regs.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
arch/arm64/kvm/sys_regs.c +0 -0
index 33aa4f5071b8..793fb19bebd6 100644

arch/arm64/kvm/sys_regs.c static bool access_mdcr(struct kvm_vcpu *vcpu, · line 2657

Quoted 8 lines
arch/arm64/kvm/sys_regs.c +1 -1
@@ -2651,7 +2651,7 @@ static bool access_mdcr(struct kvm_vcpu *vcpu,
 	 */
 	if (hpmn > vcpu->kvm->arch.nr_pmu_counters) {
 		hpmn = vcpu->kvm->arch.nr_pmu_counters;
-		u64_replace_bits(val, hpmn, MDCR_EL2_HPMN);
+		val = u64_replace_bits(val, hpmn, MDCR_EL2_HPMN);
 	}
 
 	__vcpu_assign_sys_reg(vcpu, MDCR_EL2, val);

Already fixed by Ben's patch [*], thanks!

[*]
https://git.kernel.org/pub/scm/linux/kernel/git/kvmarm/kvmarm.git/commit/?h=fixes&id=2265c08ec393ef1f5ef5019add0ab1e3a7ee0b79

Zenghui

maintainer
#2 lore raw
In reply to Arnd Bergmann
Quoted 23 lines · Arnd Bergmann

From: Arnd Bergmann <[email protected]>

u64_replace_bits() returns a modified word but does not actually modify its argument, as pointed out by this new warning:

arch/arm64/kvm/sys_regs.c: In function 'access_mdcr':
arch/arm64/kvm/sys_regs.c:2654:17: error: ignoring return value of 'u64_replace_bits' declared with attribute 'warn_unused_result' [-Werror=unused-result]
2654 | u64_replace_bits(val, hpmn, MDCR_EL2_HPMN);

The intention here must have been to update 'val', so do that instead.

Fixes efff9dd2fee7 ("KVM: arm64: Handle out-of-bound write to MDCR_EL2.HPMN")
---
 arch/arm64/kvm/sys_regs.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
arch/arm64/kvm/sys_regs.c +0 -0
index 33aa4f5071b8..793fb19bebd6 100644

arch/arm64/kvm/sys_regs.c static bool access_mdcr(struct kvm_vcpu *vcpu, · line 2657

Quoted 8 lines
arch/arm64/kvm/sys_regs.c +1 -1
@@ -2651,7 +2651,7 @@ static bool access_mdcr(struct kvm_vcpu *vcpu,
 	 */
 	if (hpmn > vcpu->kvm->arch.nr_pmu_counters) {
 		hpmn = vcpu->kvm->arch.nr_pmu_counters;
-		u64_replace_bits(val, hpmn, MDCR_EL2_HPMN);
+		val = u64_replace_bits(val, hpmn, MDCR_EL2_HPMN);
 	}
 
 	__vcpu_assign_sys_reg(vcpu, MDCR_EL2, val);

This is only in -next, right? Because I have a fix for this already queued for 6.16, as per [1].

Thanks,

	M.

[1] https://lore.kernel.org/r/[email protected]

Signature · 1 line
Without deviation from the norm, progress is not possible.
#3 lore raw
Quoted 11 lines · Marc Zyngier

arch/arm64/kvm/sys_regs.c static bool access_mdcr(struct kvm_vcpu *vcpu, · line 2657

Quoted 7 lines · Arnd Bergmann
arch/arm64/kvm/sys_regs.c +1 -1
 	if (hpmn > vcpu->kvm->arch.nr_pmu_counters) {
 		hpmn = vcpu->kvm->arch.nr_pmu_counters;
-		u64_replace_bits(val, hpmn, MDCR_EL2_HPMN);
+		val = u64_replace_bits(val, hpmn, MDCR_EL2_HPMN);
 	}
 
 	__vcpu_assign_sys_reg(vcpu, MDCR_EL2, val);

This is only in -next, right? Because I have a fix for this already queued for 6.16, as per [1].

Yes, as far as I can tell, the warning only showed up in linux-next after f66f9c3d09c1 ("bitfield: Ensure the return values of helper functions are checked").

As far as I can tell, Ben added the check in linux/bitfield.h when he sent you his version of the fix, they just ended up in linux-next in the wrong order, so I ended up recreating his original fix slightly differently.

Arnd

maintainer
#4 lore raw
Quoted 14 lines · Arnd Bergmann
Quoted 11 lines · Marc Zyngier

arch/arm64/kvm/sys_regs.c static bool access_mdcr(struct kvm_vcpu *vcpu, · line 2657

Quoted 7 lines · Arnd Bergmann
arch/arm64/kvm/sys_regs.c +1 -1
 	if (hpmn > vcpu->kvm->arch.nr_pmu_counters) {
 		hpmn = vcpu->kvm->arch.nr_pmu_counters;
-		u64_replace_bits(val, hpmn, MDCR_EL2_HPMN);
+		val = u64_replace_bits(val, hpmn, MDCR_EL2_HPMN);
 	}
 
 	__vcpu_assign_sys_reg(vcpu, MDCR_EL2, val);

This is only in -next, right? Because I have a fix for this already queued for 6.16, as per [1].

Quoted 8 lines

Yes, as far as I can tell, the warning only showed up in linux-next after f66f9c3d09c1 ("bitfield: Ensure the return values of helper functions are checked").

As far as I can tell, Ben added the check in linux/bitfield.h when he sent you his version of the fix, they just ended up in linux-next in the wrong order, so I ended up recreating his original fix slightly differently.

I don't think Ben's fix is in -next, as I queued it in the kvmarm fixes branch, which isn't pulled by -next.

Hopefully Paolo will send this to Linus shortly (pull request here[1]), and -next will be clean again.

Thanks,

	M.

[1] https://lore.kernel.org/r/[email protected]

Signature · 1 line
Without deviation from the norm, progress is not possible.
#5 lore raw
Quoted 6 lines · Marc Zyngier
Quoted 3 lines · Arnd Bergmann

Yes, as far as I can tell, the warning only showed up in linux-next after f66f9c3d09c1 ("bitfield: Ensure the return values of helper functions are checked").

Quoted 7 lines
Quoted 4 lines

As far as I can tell, Ben added the check in linux/bitfield.h when he sent you his version of the fix, they just ended up in linux-next in the wrong order, so I ended up recreating his original fix slightly differently.

I don't think Ben's fix is in -next, as I queued it in the kvmarm fixes branch, which isn't pulled by -next.

We should really get that added to -next, that'll ensure it gets covered in the pending-fixes testing coverage and avoid issues like this with the main -next tree.