linux-arm-kernel.lists.infradead.org archive mirror
 help / color / mirror / Atom feed
* [PATCH 0/2] Fix and add warning of misuse of type##_replace_bits()
@ 2025-07-03 13:57 Ben Horgan
  2025-07-03 13:57 ` [PATCH 1/2] KVM: arm64: Fix enforcement of upper bound on MDCR_EL2.HPMN Ben Horgan
  2025-07-03 13:57 ` [PATCH 2/2] bitfield: Ensure the return value of type##_replace_bits() is checked Ben Horgan
  0 siblings, 2 replies; 8+ messages in thread
From: Ben Horgan @ 2025-07-03 13:57 UTC (permalink / raw)
  To: catalin.marinas, will, maz, oliver.upton, joey.gouly,
	suzuki.poulose, yuzenghui, linux-arm-kernel, kvmarm, yury.norov,
	linux, linux-kernel
  Cc: james.morse, Ben Horgan

By inspection there is one mistake in the use of u64_replace_bits(). Fix
this and while I'm here add a __must_check annotation to help avoid the
same mistake happening again.

Ben Horgan (2):
  KVM: arm64: Fix enforcement of upper bound on MDCR_EL2.HPMN
  bitfield: Ensure the return value of type##_replace_bits() is checked

 arch/arm64/kvm/sys_regs.c | 2 +-
 include/linux/bitfield.h  | 4 ++--
 2 files changed, 3 insertions(+), 3 deletions(-)

-- 
2.43.0



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

* [PATCH 1/2] KVM: arm64: Fix enforcement of upper bound on MDCR_EL2.HPMN
  2025-07-03 13:57 [PATCH 0/2] Fix and add warning of misuse of type##_replace_bits() Ben Horgan
@ 2025-07-03 13:57 ` Ben Horgan
  2025-07-04  6:44   ` Zenghui Yu
  2025-07-03 13:57 ` [PATCH 2/2] bitfield: Ensure the return value of type##_replace_bits() is checked Ben Horgan
  1 sibling, 1 reply; 8+ messages in thread
From: Ben Horgan @ 2025-07-03 13:57 UTC (permalink / raw)
  To: catalin.marinas, will, maz, oliver.upton, joey.gouly,
	suzuki.poulose, yuzenghui, linux-arm-kernel, kvmarm, yury.norov,
	linux, linux-kernel
  Cc: james.morse, Ben Horgan, stable

Previously, u64_replace_bits() was used to no effect as the return value
was ignored. Convert to u64p_replace_bits() so the value is updated in
place.

Signed-off-by: Ben Horgan <ben.horgan@arm.com>
Fixes: efff9dd2fee7 ("KVM: arm64: Handle out-of-bound write to MDCR_EL2.HPMN")
Cc: Marc Zyngier <maz@kernel.org>
Cc: stable@vger.kernel.org
---
 arch/arm64/kvm/sys_regs.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/arch/arm64/kvm/sys_regs.c b/arch/arm64/kvm/sys_regs.c
index 76c2f0da821f..c20bd6f21e60 100644
--- a/arch/arm64/kvm/sys_regs.c
+++ b/arch/arm64/kvm/sys_regs.c
@@ -2624,7 +2624,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);
+		u64p_replace_bits(&val, hpmn, MDCR_EL2_HPMN);
 	}
 
 	__vcpu_assign_sys_reg(vcpu, MDCR_EL2, val);
-- 
2.43.0



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

* [PATCH 2/2] bitfield: Ensure the return value of type##_replace_bits() is checked
  2025-07-03 13:57 [PATCH 0/2] Fix and add warning of misuse of type##_replace_bits() Ben Horgan
  2025-07-03 13:57 ` [PATCH 1/2] KVM: arm64: Fix enforcement of upper bound on MDCR_EL2.HPMN Ben Horgan
@ 2025-07-03 13:57 ` Ben Horgan
  2025-07-07 16:31   ` Yury Norov
  1 sibling, 1 reply; 8+ messages in thread
From: Ben Horgan @ 2025-07-03 13:57 UTC (permalink / raw)
  To: catalin.marinas, will, maz, oliver.upton, joey.gouly,
	suzuki.poulose, yuzenghui, linux-arm-kernel, kvmarm, yury.norov,
	linux, linux-kernel
  Cc: james.morse, Ben Horgan

As type##_replace_bits() has no side effects it is only useful if its
return value is checked. Add __must_check to enforce this usage. To have
the bits replaced in-place typep##_replace_bits() can be used instead.

Signed-off-by: Ben Horgan <ben.horgan@arm.com>
---
 include/linux/bitfield.h | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/include/linux/bitfield.h b/include/linux/bitfield.h
index 6d9a53db54b6..39333b80d22b 100644
--- a/include/linux/bitfield.h
+++ b/include/linux/bitfield.h
@@ -195,8 +195,8 @@ static __always_inline __##type type##_encode_bits(base v, base field)	\
 		__field_overflow();					\
 	return to((v & field_mask(field)) * field_multiplier(field));	\
 }									\
-static __always_inline __##type type##_replace_bits(__##type old,	\
-					base val, base field)		\
+static __always_inline __##type __must_check type##_replace_bits(__##type old,	\
+							base val, base field)	\
 {									\
 	return (old & ~to(field)) | type##_encode_bits(val, field);	\
 }									\
-- 
2.43.0



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

* Re: [PATCH 1/2] KVM: arm64: Fix enforcement of upper bound on MDCR_EL2.HPMN
  2025-07-03 13:57 ` [PATCH 1/2] KVM: arm64: Fix enforcement of upper bound on MDCR_EL2.HPMN Ben Horgan
@ 2025-07-04  6:44   ` Zenghui Yu
  0 siblings, 0 replies; 8+ messages in thread
From: Zenghui Yu @ 2025-07-04  6:44 UTC (permalink / raw)
  To: Ben Horgan
  Cc: catalin.marinas, will, maz, oliver.upton, joey.gouly,
	suzuki.poulose, linux-arm-kernel, kvmarm, yury.norov, linux,
	linux-kernel, james.morse, stable

On 2025/7/3 21:57, Ben Horgan wrote:
> Previously, u64_replace_bits() was used to no effect as the return value
> was ignored. Convert to u64p_replace_bits() so the value is updated in
> place.
> 
> Signed-off-by: Ben Horgan <ben.horgan@arm.com>
> Fixes: efff9dd2fee7 ("KVM: arm64: Handle out-of-bound write to MDCR_EL2.HPMN")
> Cc: Marc Zyngier <maz@kernel.org>
> Cc: stable@vger.kernel.org
> ---
>  arch/arm64/kvm/sys_regs.c | 2 +-
>  1 file changed, 1 insertion(+), 1 deletion(-)
> 
> diff --git a/arch/arm64/kvm/sys_regs.c b/arch/arm64/kvm/sys_regs.c
> index 76c2f0da821f..c20bd6f21e60 100644
> --- a/arch/arm64/kvm/sys_regs.c
> +++ b/arch/arm64/kvm/sys_regs.c
> @@ -2624,7 +2624,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);
> +		u64p_replace_bits(&val, hpmn, MDCR_EL2_HPMN);
>  	}
>  
>  	__vcpu_assign_sys_reg(vcpu, MDCR_EL2, val);

Reviewed-by: Zenghui Yu <yuzenghui@huawei.com>

Thanks!


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

* Re: [PATCH 2/2] bitfield: Ensure the return value of type##_replace_bits() is checked
  2025-07-03 13:57 ` [PATCH 2/2] bitfield: Ensure the return value of type##_replace_bits() is checked Ben Horgan
@ 2025-07-07 16:31   ` Yury Norov
  2025-07-08  9:42     ` Ben Horgan
  0 siblings, 1 reply; 8+ messages in thread
From: Yury Norov @ 2025-07-07 16:31 UTC (permalink / raw)
  To: Ben Horgan
  Cc: catalin.marinas, will, maz, oliver.upton, joey.gouly,
	suzuki.poulose, yuzenghui, linux-arm-kernel, kvmarm, linux,
	linux-kernel, james.morse

Hi Ben,

On Thu, Jul 03, 2025 at 02:57:29PM +0100, Ben Horgan wrote:
> As type##_replace_bits() has no side effects it is only useful if its
> return value is checked. Add __must_check to enforce this usage. To have
> the bits replaced in-place typep##_replace_bits() can be used instead.
> 
> Signed-off-by: Ben Horgan <ben.horgan@arm.com>
> ---
>  include/linux/bitfield.h | 4 ++--
>  1 file changed, 2 insertions(+), 2 deletions(-)
> 
> diff --git a/include/linux/bitfield.h b/include/linux/bitfield.h
> index 6d9a53db54b6..39333b80d22b 100644
> --- a/include/linux/bitfield.h
> +++ b/include/linux/bitfield.h
> @@ -195,8 +195,8 @@ static __always_inline __##type type##_encode_bits(base v, base field)	\
>  		__field_overflow();					\
>  	return to((v & field_mask(field)) * field_multiplier(field));	\
>  }									\
> -static __always_inline __##type type##_replace_bits(__##type old,	\
> -					base val, base field)		\
> +static __always_inline __##type __must_check type##_replace_bits(__##type old,	\
> +							base val, base field)	\
>  {									\
>  	return (old & ~to(field)) | type##_encode_bits(val, field);	\
>  }									\

So, would it make sense to mark _encode_bits() and _get_bits() as
__must_check as well? At least from the point of unification, it
would.

How would we move this - with my bitmap-for next or with arm branch?

Thanks,
Yury


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

* Re: [PATCH 2/2] bitfield: Ensure the return value of type##_replace_bits() is checked
  2025-07-07 16:31   ` Yury Norov
@ 2025-07-08  9:42     ` Ben Horgan
  2025-07-08  9:45       ` Marc Zyngier
  0 siblings, 1 reply; 8+ messages in thread
From: Ben Horgan @ 2025-07-08  9:42 UTC (permalink / raw)
  To: Yury Norov
  Cc: catalin.marinas, will, maz, oliver.upton, joey.gouly,
	suzuki.poulose, yuzenghui, linux-arm-kernel, kvmarm, linux,
	linux-kernel, james.morse

Hi Yury,

On 7/7/25 17:31, Yury Norov wrote:
> Hi Ben,
> 
> On Thu, Jul 03, 2025 at 02:57:29PM +0100, Ben Horgan wrote:
>> As type##_replace_bits() has no side effects it is only useful if its
>> return value is checked. Add __must_check to enforce this usage. To have
>> the bits replaced in-place typep##_replace_bits() can be used instead.
>>
>> Signed-off-by: Ben Horgan <ben.horgan@arm.com>
>> ---
>>   include/linux/bitfield.h | 4 ++--
>>   1 file changed, 2 insertions(+), 2 deletions(-)
>>
>> diff --git a/include/linux/bitfield.h b/include/linux/bitfield.h
>> index 6d9a53db54b6..39333b80d22b 100644
>> --- a/include/linux/bitfield.h
>> +++ b/include/linux/bitfield.h
>> @@ -195,8 +195,8 @@ static __always_inline __##type type##_encode_bits(base v, base field)	\
>>   		__field_overflow();					\
>>   	return to((v & field_mask(field)) * field_multiplier(field));	\
>>   }									\
>> -static __always_inline __##type type##_replace_bits(__##type old,	\
>> -					base val, base field)		\
>> +static __always_inline __##type __must_check type##_replace_bits(__##type old,	\
>> +							base val, base field)	\
>>   {									\
>>   	return (old & ~to(field)) | type##_encode_bits(val, field);	\
>>   }									\
> 
> So, would it make sense to mark _encode_bits() and _get_bits() as
> __must_check as well? At least from the point of unification, it
> would.
Could do. It seems less important as there are no obvious foot-guns that 
these would guards against. Would you like me to add this in a v2?
> 
> How would we move this - with my bitmap-for next or with arm branch?

I'm not familiar with the branch machinery so can't comment on this.
> 
> Thanks,
> Yury
> 

Thanks,

Ben



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

* Re: [PATCH 2/2] bitfield: Ensure the return value of type##_replace_bits() is checked
  2025-07-08  9:42     ` Ben Horgan
@ 2025-07-08  9:45       ` Marc Zyngier
  2025-07-08 14:46         ` Yury Norov
  0 siblings, 1 reply; 8+ messages in thread
From: Marc Zyngier @ 2025-07-08  9:45 UTC (permalink / raw)
  To: Ben Horgan
  Cc: Yury Norov, catalin.marinas, will, oliver.upton, joey.gouly,
	suzuki.poulose, yuzenghui, linux-arm-kernel, kvmarm, linux,
	linux-kernel, james.morse

On Tue, 08 Jul 2025 10:42:06 +0100,
Ben Horgan <ben.horgan@arm.com> wrote:
> 
> Hi Yury,
> 
> On 7/7/25 17:31, Yury Norov wrote:
> > Hi Ben,
> > 
> > On Thu, Jul 03, 2025 at 02:57:29PM +0100, Ben Horgan wrote:
> >> As type##_replace_bits() has no side effects it is only useful if its
> >> return value is checked. Add __must_check to enforce this usage. To have
> >> the bits replaced in-place typep##_replace_bits() can be used instead.
> >> 
> >> Signed-off-by: Ben Horgan <ben.horgan@arm.com>
> >> ---
> >>   include/linux/bitfield.h | 4 ++--
> >>   1 file changed, 2 insertions(+), 2 deletions(-)
> >> 
> >> diff --git a/include/linux/bitfield.h b/include/linux/bitfield.h
> >> index 6d9a53db54b6..39333b80d22b 100644
> >> --- a/include/linux/bitfield.h
> >> +++ b/include/linux/bitfield.h
> >> @@ -195,8 +195,8 @@ static __always_inline __##type type##_encode_bits(base v, base field)	\
> >>   		__field_overflow();					\
> >>   	return to((v & field_mask(field)) * field_multiplier(field));	\
> >>   }									\
> >> -static __always_inline __##type type##_replace_bits(__##type old,	\
> >> -					base val, base field)		\
> >> +static __always_inline __##type __must_check type##_replace_bits(__##type old,	\
> >> +							base val, base field)	\
> >>   {									\
> >>   	return (old & ~to(field)) | type##_encode_bits(val, field);	\
> >>   }									\
> > 
> > So, would it make sense to mark _encode_bits() and _get_bits() as
> > __must_check as well? At least from the point of unification, it
> > would.
> Could do. It seems less important as there are no obvious foot-guns
> that these would guards against. Would you like me to add this in a
> v2?
> > 
> > How would we move this - with my bitmap-for next or with arm branch?
> 
> I'm not familiar with the branch machinery so can't comment on this.

The first patch will definitely go in via the KVM/arm64 tree, probably
as a fix for 6.16.

Thanks,

	M.

-- 
Without deviation from the norm, progress is not possible.


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

* Re: [PATCH 2/2] bitfield: Ensure the return value of type##_replace_bits() is checked
  2025-07-08  9:45       ` Marc Zyngier
@ 2025-07-08 14:46         ` Yury Norov
  0 siblings, 0 replies; 8+ messages in thread
From: Yury Norov @ 2025-07-08 14:46 UTC (permalink / raw)
  To: Marc Zyngier
  Cc: Ben Horgan, catalin.marinas, will, oliver.upton, joey.gouly,
	suzuki.poulose, yuzenghui, linux-arm-kernel, kvmarm, linux,
	linux-kernel, james.morse

On Tue, Jul 08, 2025 at 10:45:50AM +0100, Marc Zyngier wrote:
> On Tue, 08 Jul 2025 10:42:06 +0100,
> Ben Horgan <ben.horgan@arm.com> wrote:
> > 
> > Hi Yury,
> > 
> > On 7/7/25 17:31, Yury Norov wrote:
> > > Hi Ben,
> > > 
> > > On Thu, Jul 03, 2025 at 02:57:29PM +0100, Ben Horgan wrote:
> > >> As type##_replace_bits() has no side effects it is only useful if its
> > >> return value is checked. Add __must_check to enforce this usage. To have
> > >> the bits replaced in-place typep##_replace_bits() can be used instead.
> > >> 
> > >> Signed-off-by: Ben Horgan <ben.horgan@arm.com>
> > >> ---
> > >>   include/linux/bitfield.h | 4 ++--
> > >>   1 file changed, 2 insertions(+), 2 deletions(-)
> > >> 
> > >> diff --git a/include/linux/bitfield.h b/include/linux/bitfield.h
> > >> index 6d9a53db54b6..39333b80d22b 100644
> > >> --- a/include/linux/bitfield.h
> > >> +++ b/include/linux/bitfield.h
> > >> @@ -195,8 +195,8 @@ static __always_inline __##type type##_encode_bits(base v, base field)	\
> > >>   		__field_overflow();					\
> > >>   	return to((v & field_mask(field)) * field_multiplier(field));	\
> > >>   }									\
> > >> -static __always_inline __##type type##_replace_bits(__##type old,	\
> > >> -					base val, base field)		\
> > >> +static __always_inline __##type __must_check type##_replace_bits(__##type old,	\
> > >> +							base val, base field)	\
> > >>   {									\
> > >>   	return (old & ~to(field)) | type##_encode_bits(val, field);	\
> > >>   }									\
> > > 
> > > So, would it make sense to mark _encode_bits() and _get_bits() as
> > > __must_check as well? At least from the point of unification, it
> > > would.
> > Could do. It seems less important as there are no obvious foot-guns
> > that these would guards against. Would you like me to add this in a
> > v2?

Yes please.
 
> > > How would we move this - with my bitmap-for next or with arm branch?
> > 
> > I'm not familiar with the branch machinery so can't comment on this.
> 
> The first patch will definitely go in via the KVM/arm64 tree, probably
> as a fix for 6.16.

OK. Then I'll take patch #2 v2 by myself.

Thanks,
Yury


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

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

Thread overview: 8+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-07-03 13:57 [PATCH 0/2] Fix and add warning of misuse of type##_replace_bits() Ben Horgan
2025-07-03 13:57 ` [PATCH 1/2] KVM: arm64: Fix enforcement of upper bound on MDCR_EL2.HPMN Ben Horgan
2025-07-04  6:44   ` Zenghui Yu
2025-07-03 13:57 ` [PATCH 2/2] bitfield: Ensure the return value of type##_replace_bits() is checked Ben Horgan
2025-07-07 16:31   ` Yury Norov
2025-07-08  9:42     ` Ben Horgan
2025-07-08  9:45       ` Marc Zyngier
2025-07-08 14:46         ` Yury Norov

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).