linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH v2] bitfield: fix *_encode_bits()
@ 2018-06-14 21:26 Johannes Berg
  2018-06-15  8:51 ` Andy Shevchenko
  0 siblings, 1 reply; 4+ messages in thread
From: Johannes Berg @ 2018-06-14 21:26 UTC (permalink / raw)
  To: linux-kernel; +Cc: Al Viro

There's a bug in *_encode_bits() in using ~field_multiplier() for
the check whether or not the constant value fits into the field,
this is wrong and clearly ~field_mask() was intended. This was
triggering for me for both constant and non-constant values.

Additionally, make this case actually into an compile error.
Declaring the extern function that will never exist with just a
warning is pointless as then later we'll just get a link error.

While at it, also fix the indentation in those lines I'm touching.

Fixes: 00b0c9b82663 ("Add primitives for manipulating bitfields both in host- and fixed-endian.")
Signed-off-by: Johannes Berg <johannes@sipsolutions.net>
---
v2: replace stray tab by space

If you don't mind, I'd like to take this through the networking
tree(s) as a fix since I have some pending code that depends on
it.
---
 include/linux/bitfield.h | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

diff --git a/include/linux/bitfield.h b/include/linux/bitfield.h
index cf2588d81148..147a7bb341dd 100644
--- a/include/linux/bitfield.h
+++ b/include/linux/bitfield.h
@@ -104,7 +104,7 @@
 		(typeof(_mask))(((_reg) & (_mask)) >> __bf_shf(_mask));	\
 	})
 
-extern void __compiletime_warning("value doesn't fit into mask")
+extern void __compiletime_error("value doesn't fit into mask")
 __field_overflow(void);
 extern void __compiletime_error("bad bitfield mask")
 __bad_mask(void);
@@ -121,8 +121,8 @@ static __always_inline u64 field_mask(u64 field)
 #define ____MAKE_OP(type,base,to,from)					\
 static __always_inline __##type type##_encode_bits(base v, base field)	\
 {									\
-        if (__builtin_constant_p(v) &&	(v & ~field_multiplier(field)))	\
-			    __field_overflow();				\
+	if (__builtin_constant_p(v) && (v & ~field_mask(field)))	\
+		__field_overflow();					\
 	return to((v & field_mask(field)) * field_multiplier(field));	\
 }									\
 static __always_inline __##type type##_replace_bits(__##type old,	\
-- 
2.14.4


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

* Re: [PATCH v2] bitfield: fix *_encode_bits()
  2018-06-14 21:26 [PATCH v2] bitfield: fix *_encode_bits() Johannes Berg
@ 2018-06-15  8:51 ` Andy Shevchenko
  2018-06-15  9:30   ` Johannes Berg
  0 siblings, 1 reply; 4+ messages in thread
From: Andy Shevchenko @ 2018-06-15  8:51 UTC (permalink / raw)
  To: Johannes Berg; +Cc: Linux Kernel Mailing List, Al Viro

On Fri, Jun 15, 2018 at 12:26 AM, Johannes Berg
<johannes@sipsolutions.net> wrote:
> There's a bug in *_encode_bits() in using ~field_multiplier() for
> the check whether or not the constant value fits into the field,
> this is wrong and clearly ~field_mask() was intended. This was
> triggering for me for both constant and non-constant values.
>
> Additionally, make this case actually into an compile error.
> Declaring the extern function that will never exist with just a
> warning is pointless as then later we'll just get a link error.
>
> While at it, also fix the indentation in those lines I'm touching.

I'm just wondering if we have test cases for that API.
If not, perhaps it's a good time to start (perhaps extend test_bitmap.c)?

Otherwise, the fix looks good!

>
> Fixes: 00b0c9b82663 ("Add primitives for manipulating bitfields both in host- and fixed-endian.")
> Signed-off-by: Johannes Berg <johannes@sipsolutions.net>
> ---
> v2: replace stray tab by space
>
> If you don't mind, I'd like to take this through the networking
> tree(s) as a fix since I have some pending code that depends on
> it.
> ---
>  include/linux/bitfield.h | 6 +++---
>  1 file changed, 3 insertions(+), 3 deletions(-)
>
> diff --git a/include/linux/bitfield.h b/include/linux/bitfield.h
> index cf2588d81148..147a7bb341dd 100644
> --- a/include/linux/bitfield.h
> +++ b/include/linux/bitfield.h
> @@ -104,7 +104,7 @@
>                 (typeof(_mask))(((_reg) & (_mask)) >> __bf_shf(_mask)); \
>         })
>
> -extern void __compiletime_warning("value doesn't fit into mask")
> +extern void __compiletime_error("value doesn't fit into mask")
>  __field_overflow(void);
>  extern void __compiletime_error("bad bitfield mask")
>  __bad_mask(void);
> @@ -121,8 +121,8 @@ static __always_inline u64 field_mask(u64 field)
>  #define ____MAKE_OP(type,base,to,from)                                 \
>  static __always_inline __##type type##_encode_bits(base v, base field) \
>  {                                                                      \
> -        if (__builtin_constant_p(v) && (v & ~field_multiplier(field))) \
> -                           __field_overflow();                         \
> +       if (__builtin_constant_p(v) && (v & ~field_mask(field)))        \
> +               __field_overflow();                                     \
>         return to((v & field_mask(field)) * field_multiplier(field));   \
>  }                                                                      \
>  static __always_inline __##type type##_replace_bits(__##type old,      \
> --
> 2.14.4
>



-- 
With Best Regards,
Andy Shevchenko

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

* Re: [PATCH v2] bitfield: fix *_encode_bits()
  2018-06-15  8:51 ` Andy Shevchenko
@ 2018-06-15  9:30   ` Johannes Berg
  2018-06-15 13:02     ` Andy Shevchenko
  0 siblings, 1 reply; 4+ messages in thread
From: Johannes Berg @ 2018-06-15  9:30 UTC (permalink / raw)
  To: Andy Shevchenko; +Cc: Linux Kernel Mailing List, Al Viro

On Fri, 2018-06-15 at 11:51 +0300, Andy Shevchenko wrote:
> On Fri, Jun 15, 2018 at 12:26 AM, Johannes Berg
> <johannes@sipsolutions.net> wrote:
> > There's a bug in *_encode_bits() in using ~field_multiplier() for
> > the check whether or not the constant value fits into the field,
> > this is wrong and clearly ~field_mask() was intended. This was
> > triggering for me for both constant and non-constant values.
> > 
> > Additionally, make this case actually into an compile error.
> > Declaring the extern function that will never exist with just a
> > warning is pointless as then later we'll just get a link error.
> > 
> > While at it, also fix the indentation in those lines I'm touching.
> 
> I'm just wondering if we have test cases for that API.
> If not, perhaps it's a good time to start (perhaps extend test_bitmap.c)?

I guess. Do we have infrastructure to provoke & check compile errors
though?

johannes

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

* Re: [PATCH v2] bitfield: fix *_encode_bits()
  2018-06-15  9:30   ` Johannes Berg
@ 2018-06-15 13:02     ` Andy Shevchenko
  0 siblings, 0 replies; 4+ messages in thread
From: Andy Shevchenko @ 2018-06-15 13:02 UTC (permalink / raw)
  To: Johannes Berg; +Cc: Linux Kernel Mailing List, Al Viro

On Fri, Jun 15, 2018 at 12:30 PM, Johannes Berg
<johannes@sipsolutions.net> wrote:
> On Fri, 2018-06-15 at 11:51 +0300, Andy Shevchenko wrote:
>> On Fri, Jun 15, 2018 at 12:26 AM, Johannes Berg
>> <johannes@sipsolutions.net> wrote:
>> > There's a bug in *_encode_bits() in using ~field_multiplier() for
>> > the check whether or not the constant value fits into the field,
>> > this is wrong and clearly ~field_mask() was intended. This was
>> > triggering for me for both constant and non-constant values.
>> >
>> > Additionally, make this case actually into an compile error.
>> > Declaring the extern function that will never exist with just a
>> > warning is pointless as then later we'll just get a link error.
>> >
>> > While at it, also fix the indentation in those lines I'm touching.
>>
>> I'm just wondering if we have test cases for that API.
>> If not, perhaps it's a good time to start (perhaps extend test_bitmap.c)?
>
> I guess. Do we have infrastructure to provoke & check compile errors
> though?

Not sure,. What I'm telling about is run-time testing.

-- 
With Best Regards,
Andy Shevchenko

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

end of thread, other threads:[~2018-06-15 13:02 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2018-06-14 21:26 [PATCH v2] bitfield: fix *_encode_bits() Johannes Berg
2018-06-15  8:51 ` Andy Shevchenko
2018-06-15  9:30   ` Johannes Berg
2018-06-15 13:02     ` Andy Shevchenko

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).