Skip to contentSkip to conversation

bitfield fixes and tests

PATCH In review Johannes Berg 2 participants 5 messages Jun 20, 2018 – Jun 27, 2018

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

v6 cover bitfield fixes and tests

This is just a resend of v5, to Cc linux-wireless - we decided that since we have the user coming in through the wireless-drivers tree, it'll avoid all the usual problems if we'll take these also through that tree.

johannes

#1 lore raw

PATCH v6 1/3 bitfield: fix *_encode_bits()

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.

Finally, as suggested by Andy Shevchenko, add some tests and for that introduce also u8 helpers. The tests don't compile without the fix, showing that it's necessary.

Fixes 00b0c9b82663 ("Add primitives for manipulating bitfields both in host- and fixed-endian.")

---
v2: replace stray tab by space
v3: u8 helpers, tests
v4: minor cleanup (Andy)
split into two patches (Andy)

---
 include/linux/bitfield.h | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
include/linux/bitfield.h +3 -3
index cf2588d81148..147a7bb341dd 100644
@@ -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,	\
Signature · 1 line
2.14.4
#2 lore raw

Re: [v6,(resend),1/3] bitfield: fix *_encode_bits()

Quoted 11 lines · Johannes Berg

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.

Quoted 7 lines

Finally, as suggested by Andy Shevchenko, add some tests and for that introduce also u8 helpers. The tests don't compile without the fix, showing that it's necessary.

Fixes 00b0c9b82663 ("Add primitives for manipulating bitfields both in host- and fixed-endian.")

3 patches applied to wireless-drivers-next.git, thanks.

e7d4a95da86e bitfield: fix *_encode_bits()
37a3862e1238 bitfield: add u8 helpers
0e2dc70e3d0d bitfield: add tests

Signature · 3 lines
https://patchwork.kernel.org/patch/10476545/

https://wireless.wiki.kernel.org/en/developers/documentation/submittingpatches
#3 lore raw

PATCH v6 2/3 bitfield: add u8 helpers

In reply to Johannes Berg

There's no reason why we shouldn't pack/unpack bits into/from u8 values/registers/etc., so add u8 helpers.

Use the ____MAKE_OP() macro directly to avoid having nonsense le8_encode_bits() and similar functions.

---
 include/linux/bitfield.h | 1 +
 1 file changed, 1 insertion(+)
include/linux/bitfield.h +1 -0
index 147a7bb341dd..65a6981eef7b 100644
@@ -143,6 +143,7 @@ static __always_inline base type##_get_bits(__##type v, base field)	\
 	____MAKE_OP(le##size,u##size,cpu_to_le##size,le##size##_to_cpu)	\
 	____MAKE_OP(be##size,u##size,cpu_to_be##size,be##size##_to_cpu)	\
 	____MAKE_OP(u##size,u##size,,)
+____MAKE_OP(u8,u8,,)
 __MAKE_OP(16)
 __MAKE_OP(32)
 __MAKE_OP(64)
Signature · 1 line
2.14.4
#4 lore raw

PATCH v6 3/3 bitfield: add tests

In reply to Johannes Berg

Add tests for the bitfield helpers. The constant ones will all be folded to nothing by the compiler (if everything is correct in the header file), and the variable ones do some tests against open-coding the necessary shifts.

A few test cases that should fail/warn compilation are provided under ifdef.

---
 lib/Kconfig.debug   |   7 +++
 lib/Makefile        |   1 +
 lib/test_bitfield.c | 168 ++++++++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 176 insertions(+)
 create mode 100644 lib/test_bitfield.c
lib/Kconfig.debug +7 -0
index eb885942eb0f..b0870377b4d1 100644
@@ -1799,6 +1799,13 @@ config TEST_BITMAP
 
 	  If unsure, say N.
 
+config TEST_BITFIELD
+	tristate "Test bitfield functions at runtime"
+	help
+	  Enable this option to test the bitfield functions at boot.
+
+	  If unsure, say N.
+
 config TEST_UUID
 	tristate "Test functions located in the uuid module at runtime"
 
lib/Makefile +1 -0
index 84c6dcb31fbb..02ab4c1a64d1 100644
@@ -68,6 +68,7 @@ obj-$(CONFIG_TEST_STATIC_KEYS) += test_static_keys.o
 obj-$(CONFIG_TEST_STATIC_KEYS) += test_static_key_base.o
 obj-$(CONFIG_TEST_PRINTF) += test_printf.o
 obj-$(CONFIG_TEST_BITMAP) += test_bitmap.o
+obj-$(CONFIG_TEST_BITFIELD) += test_bitfield.o
 obj-$(CONFIG_TEST_UUID) += test_uuid.o
 obj-$(CONFIG_TEST_PARMAN) += test_parman.o
 obj-$(CONFIG_TEST_KMOD) += test_kmod.o
lib/test_bitfield.c new file +168 -0

168 lines not on this page · raw message

Signature · 1 line
2.14.4