uapi: Add support for GENMASK_U128()
Subsystems (3)
v4 cover uapi: Add support for GENMASK_U128()
This adds support for GENMASK_U128() and some corresponding tests as well. GENMASK_U128() generated 128 bit masks will be required later on the arm64 platform for enabling FEAT_SYSREG128 and FEAT_D128 features.
Because GENMAKS_U128() depends on __int128 data type being supported in the compiler, its usage needs to be protected with CONFIG_ARCH_SUPPORTS_INT128.
This series is being used for an work in progress on arm64 platform.
https://lore.kernel.org/all/[email protected]/
Changes in V4:
- Folded in the below patch which guards against assembly code
- Fixed __GENMASK_U128() for the corner case (128 bit left shift)
- Improved genmask_u128_test() and genmask_input_check_test()
https://lore.kernel.org/lkml/[email protected]/
Changes in V3:
https://lore.kernel.org/lkml/[email protected]/
- Dropped unused __BITS_PER_U128
- Moved #ifdef CONFIG_ARCH_SUPPORTS_INT128 inside genmask_u128_test()
- Added asm unsupported comments for GENMASK_U128() and __BIT128()
- Added reviewed tag from Arnd
Changes in V2:
https://lore.kernel.org/all/[email protected]/
- Wrapped genmask_u128_test() with CONFIG_ARCH_SUPPORTS_INT128
- Defined __BITS_PER_U128 unconditionally as 128
- Defined __GENMASK_U128() via new _BIT128()
- Dropped _U128() and _AC128()
Changes in V1:
https://lore.kernel.org/lkml/[email protected]/
Anshuman Khandual (2):
uapi: Define GENMASK_U128
lib/test_bits.c: Add tests for GENMASK_U128()
include/linux/bits.h | 15 +++++++++++++++ include/uapi/linux/bits.h | 3 +++ include/uapi/linux/const.h | 17 +++++++++++++++++ lib/test_bits.c | 34 ++++++++++++++++++++++++++++++++++ 4 files changed, 69 insertions(+)
Signature · 1 line
2.30.2
PATCH v4 1/2 uapi: Define GENMASK_U128
This adds GENMASK_U128() and __GENMASK_U128() macros using __BITS_PER_U128 and __int128 data types. These macros will be used in providing support for generating 128 bit masks.
The macros wouldn't work in all assembler flavors for reasons described in the comments on top of declarations. Enforce it for more by adding !__ASSEMBLY__ guard.
--- include/linux/bits.h | 15 +++++++++++++++ include/uapi/linux/bits.h | 3 +++ include/uapi/linux/const.h | 17 +++++++++++++++++ 3 files changed, 35 insertions(+)
include/linux/bits.h +15 -0
@@ -36,4 +36,19 @@#define GENMASK_ULL (h, l) \ (GENMASK_INPUT_CHECK(h, l) + __GENMASK_ULL(h, l)) +#if !defined(__ASSEMBLY__) +/* +* Missing asm support +* +* __GENMASK_U128() depends on _BIT128() which would not work +* in the asm code, as it shifts an 'unsigned __init128' data +* type instead of direct representation of 128 bit constants +* such as long and unsigned long. The fundamental problem is +* that a 128 bit constant will get silently truncated by the +* gcc compiler. +*/ +#define GENMASK_U128 (h, l) \ + (GENMASK_INPUT_CHECK(h, l) + __GENMASK_U128(h, l)) +#endif +#endif /* __LINUX_BITS_H */
include/uapi/linux/bits.h +3 -0
@@ -12,4 +12,7 @@ (((~_ULL (0 )) - (_ULL (1 ) << (l)) +1 ) & \ (~_ULL(0) >> (__BITS_PER_LONG_LONG - 1 - (h)))) +#define __GENMASK_U128 (h, l) \ + ((_BIT128((h)) << 1) - (_BIT128(l))) +#endif /* _UAPI_LINUX_BITS_H */
include/uapi/linux/const.h +17 -0
@@ -28,6 +28,23 @@#define _BITUL (x) (_UL(1) << (x))#define _BITULL (x) (_ULL(1) << (x)) +#if !defined(__ASSEMBLY__) +/* +* Missing asm support +* +* __BIT128() would not work in the asm code, as it shifts an +* 'unsigned __init128' data type as direct representation of +* 128 bit constants is not supported in the gcc compiler, as +* they get silently truncated. +* +* TODO: Please revisit this implementation when gcc compiler +* starts representing 128 bit constants directly like long +* and unsigned long etc. Subsequently drop the comment for +* GENMASK_U128() which would then start supporting asm code. +*/ +#define _BIT128 (x) ((unsigned __int128)(1) << (x)) +#endif +#define __ALIGN_KERNEL (x, a) __ALIGN_KERNEL_MASK(x, (__typeof__(x))(a) - 1)#define __ALIGN_KERNEL_MASK (x, mask) (((x) + (mask)) & ~(mask))
Signature · 1 line
2.30.2
PATCH v4 2/2 lib/test_bits.c: Add tests for GENMASK_U128()
In reply to Anshuman KhandualThis adds GENMASK_U128() tests although currently only 64 bit wide masks are being tested.
--- lib/test_bits.c | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+)
lib/test_bits.c +34 -0
@@ -39,6 +39,36 @@ static void genmask_ull_test(struct kunit *test)#endif } +static void genmask_u128_test (struct kunit *test) +{ +#ifdef CONFIG_ARCH_SUPPORTS_INT128 +/* Below 64 bit masks */ +KUNIT_EXPECT_EQ (test,0x0000000000000001ull ,GENMASK_U128 (0 ,0 )); +KUNIT_EXPECT_EQ (test,0x0000000000000003ull ,GENMASK_U128 (1 ,0 )); +KUNIT_EXPECT_EQ (test,0x0000000000000006ull ,GENMASK_U128 (2 ,1 )); +KUNIT_EXPECT_EQ (test,0x00000000ffffffffull ,GENMASK_U128 (31 ,0 )); +KUNIT_EXPECT_EQ (test,0x000000ffffe00000ull ,GENMASK_U128 (39 ,21 )); +KUNIT_EXPECT_EQ (test,0xffffffffffffffffull ,GENMASK_U128 (63 ,0 )); + +/* Above 64 bit masks - only 64 bit portion can be validated once */ +KUNIT_EXPECT_EQ (test,0xffffffffffffffffull ,GENMASK_U128 (64 ,0 ) >>1 ); +KUNIT_EXPECT_EQ (test,0x00000000ffffffffull ,GENMASK_U128 (81 ,50 ) >>50 ); +KUNIT_EXPECT_EQ (test,0x0000000000ffffffull ,GENMASK_U128 (87 ,64 ) >>64 ); +KUNIT_EXPECT_EQ (test,0x0000000000ff0000ull ,GENMASK_U128 (87 ,80 ) >>64 ); + +KUNIT_EXPECT_EQ (test,0xffffffffffffffffull ,GENMASK_U128 (127 ,0 ) >>64 ); +KUNIT_EXPECT_EQ (test,0xffffffffffffffffull , (u64 )GENMASK_U128 (127 ,0 )); +KUNIT_EXPECT_EQ (test,0x0000000000000003ull ,GENMASK_U128 (127 ,126 ) >>126 ); +KUNIT_EXPECT_EQ (test,0x0000000000000001ull ,GENMASK_U128 (127 ,127 ) >>127 ); +#ifdef TEST_GENMASK_FAILURES +/* these should fail compilation */ +GENMASK_U128 (0 ,1 ); +GENMASK_U128 (0 ,10 ); +GENMASK_U128 (9 ,10 ); +#endif /* TEST_GENMASK_FAILURES */ +#endif /* CONFIG_ARCH_SUPPORTS_INT128 */ +} +static void genmask_input_check_test (struct kunit *test) {unsigned int x, y;
@@ -56,12 +86,16 @@ static void genmask_input_check_test(struct kunit *test)/* Valid input */ KUNIT_EXPECT_EQ (test,0 ,GENMASK_INPUT_CHECK (1 ,1 ));KUNIT_EXPECT_EQ (test,0 ,GENMASK_INPUT_CHECK (39 ,21 )); +KUNIT_EXPECT_EQ (test,0 ,GENMASK_INPUT_CHECK (100 ,80 )); +KUNIT_EXPECT_EQ (test,0 ,GENMASK_INPUT_CHECK (110 ,65 )); +KUNIT_EXPECT_EQ (test,0 ,GENMASK_INPUT_CHECK (127 ,0 )); }static struct kunit_case bits_test_cases[] = {KUNIT_CASE (genmask_test),KUNIT_CASE (genmask_ull_test), +KUNIT_CASE (genmask_u128_test),KUNIT_CASE (genmask_input_check_test), {} };
Signature · 1 line
2.30.2