Skip to content

Commit f144367

Browse files
committed
Merge tag 'bitmap-for-6.20' of https://github.com/norov/linux
Pull bitmap updates from Yury Norov: - more rust helpers (Alice) - more bitops tests (Ryota) - FIND_NTH_BIT() uninitialized variable fix (Lee Yongjun) - random cleanups (Andy, H. Peter) * tag 'bitmap-for-6.20' of https://github.com/norov/linux: lib/tests: extend KUnit test for bitops with more cases bitops: Add more files to the MAINTAINERS lib/find_bit: fix uninitialized variable use in FIND_NTH_BIT lib/tests: add KUnit test for bitops rust: cpumask: add __rust_helper to helpers rust: bitops: add __rust_helper to helpers rust: bitmap: add __rust_helper to helpers linux/bitfield.h: replace __auto_type with auto
2 parents f17b474 + 6711069 commit f144367

9 files changed

Lines changed: 243 additions & 3 deletions

File tree

MAINTAINERS

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4472,8 +4472,10 @@ F: arch/*/lib/bitops.c
44724472
F: include/asm-generic/bitops
44734473
F: include/asm-generic/bitops.h
44744474
F: include/linux/bitops.h
4475+
F: include/linux/count_zeros.h
44754476
F: lib/hweight.c
44764477
F: lib/test_bitops.c
4478+
F: lib/tests/bitops_kunit.c
44774479
F: tools/*/bitops*
44784480

44794481
BITOPS API BINDINGS [RUST]

include/linux/bitfield.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#define _LINUX_BITFIELD_H
99

1010
#include <linux/build_bug.h>
11+
#include <linux/compiler.h>
1112
#include <linux/typecheck.h>
1213
#include <asm/byteorder.h>
1314

@@ -243,7 +244,7 @@ __MAKE_OP(64)
243244

244245
#define __field_prep(mask, val) \
245246
({ \
246-
__auto_type __mask = (mask); \
247+
auto __mask = (mask); \
247248
typeof(__mask) __val = (val); \
248249
unsigned int __shift = BITS_PER_TYPE(__mask) <= 32 ? \
249250
__ffs(__mask) : __ffs64(__mask); \
@@ -252,7 +253,7 @@ __MAKE_OP(64)
252253

253254
#define __field_get(mask, reg) \
254255
({ \
255-
__auto_type __mask = (mask); \
256+
auto __mask = (mask); \
256257
typeof(__mask) __reg = (reg); \
257258
unsigned int __shift = BITS_PER_TYPE(__mask) <= 32 ? \
258259
__ffs(__mask) : __ffs64(__mask); \

lib/Kconfig.debug

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2647,6 +2647,19 @@ config TEST_SYSCTL
26472647

26482648
If unsure, say N.
26492649

2650+
config BITOPS_KUNIT
2651+
tristate "KUnit test for bitops" if !KUNIT_ALL_TESTS
2652+
depends on KUNIT
2653+
default KUNIT_ALL_TESTS
2654+
help
2655+
This option enables the KUnit test for the bitops library
2656+
which provides functions for bit operations.
2657+
2658+
Note that this is derived from the original test_bitops module.
2659+
For micro-benchmarks and compiler warning checks, enable TEST_BITOPS.
2660+
2661+
If unsure, say N.
2662+
26502663
config BITFIELD_KUNIT
26512664
tristate "KUnit test bitfield functions at runtime" if !KUNIT_ALL_TESTS
26522665
depends on KUNIT

lib/find_bit.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ out: \
7171

7272
#define FIND_NTH_BIT(FETCH, size, num) \
7373
({ \
74-
unsigned long sz = (size), nr = (num), idx, w, tmp; \
74+
unsigned long sz = (size), nr = (num), idx, w, tmp = 0; \
7575
\
7676
for (idx = 0; (idx + 1) * BITS_PER_LONG <= sz; idx++) { \
7777
if (idx * BITS_PER_LONG + nr >= sz) \

lib/tests/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
# KUnit tests
66
CFLAGS_bitfield_kunit.o := $(DISABLE_STRUCTLEAK_PLUGIN)
77
obj-$(CONFIG_BASE64_KUNIT) += base64_kunit.o
8+
obj-$(CONFIG_BITOPS_KUNIT) += bitops_kunit.o
89
obj-$(CONFIG_BITFIELD_KUNIT) += bitfield_kunit.o
910
obj-$(CONFIG_BITS_TEST) += test_bits.o
1011
obj-$(CONFIG_BLACKHOLE_DEV_KUNIT_TEST) += blackhole_dev_kunit.o

lib/tests/bitops_kunit.c

Lines changed: 205 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,205 @@
1+
// SPDX-License-Identifier: GPL-2.0-only
2+
/*
3+
* Copyright (C) 2020 Intel Corporation
4+
* Copyright (C) 2026 Ryota Sakamoto <sakamo.ryota@gmail.com>
5+
*/
6+
7+
#include <linux/bitops.h>
8+
#include <linux/module.h>
9+
#include <kunit/test.h>
10+
11+
/* use an enum because that's the most common BITMAP usage */
12+
enum bitops_fun {
13+
BITOPS_4 = 4,
14+
BITOPS_7 = 7,
15+
BITOPS_11 = 11,
16+
BITOPS_31 = 31,
17+
BITOPS_88 = 88,
18+
BITOPS_LENGTH = 256
19+
};
20+
21+
struct bitops_test_case {
22+
const char *str;
23+
const long nr;
24+
};
25+
26+
static struct bitops_test_case bitops_cases[] = {
27+
{
28+
.str = "BITOPS_4",
29+
.nr = BITOPS_4,
30+
},
31+
{
32+
.str = "BITOPS_7",
33+
.nr = BITOPS_7,
34+
},
35+
{
36+
.str = "BITOPS_11",
37+
.nr = BITOPS_11,
38+
},
39+
{
40+
.str = "BITOPS_31",
41+
.nr = BITOPS_31,
42+
},
43+
{
44+
.str = "BITOPS_88",
45+
.nr = BITOPS_88,
46+
},
47+
};
48+
49+
KUNIT_ARRAY_PARAM_DESC(bitops, bitops_cases, str);
50+
51+
static void test_set_bit_clear_bit(struct kunit *test)
52+
{
53+
const struct bitops_test_case *params = test->param_value;
54+
DECLARE_BITMAP(bitmap, BITOPS_LENGTH);
55+
int bit_set;
56+
57+
bitmap_zero(bitmap, BITOPS_LENGTH);
58+
59+
set_bit(params->nr, bitmap);
60+
KUNIT_EXPECT_TRUE(test, test_bit(params->nr, bitmap));
61+
62+
clear_bit(params->nr, bitmap);
63+
KUNIT_EXPECT_FALSE(test, test_bit(params->nr, bitmap));
64+
65+
bit_set = find_first_bit(bitmap, BITOPS_LENGTH);
66+
KUNIT_EXPECT_EQ(test, bit_set, BITOPS_LENGTH);
67+
}
68+
69+
static void test_change_bit(struct kunit *test)
70+
{
71+
const struct bitops_test_case *params = test->param_value;
72+
DECLARE_BITMAP(bitmap, BITOPS_LENGTH);
73+
int bit_set;
74+
75+
bitmap_zero(bitmap, BITOPS_LENGTH);
76+
77+
change_bit(params->nr, bitmap);
78+
KUNIT_EXPECT_TRUE(test, test_bit(params->nr, bitmap));
79+
80+
change_bit(params->nr, bitmap);
81+
KUNIT_EXPECT_FALSE(test, test_bit(params->nr, bitmap));
82+
83+
bit_set = find_first_bit(bitmap, BITOPS_LENGTH);
84+
KUNIT_EXPECT_EQ(test, bit_set, BITOPS_LENGTH);
85+
}
86+
87+
static void test_test_and_set_bit_test_and_clear_bit(struct kunit *test)
88+
{
89+
const struct bitops_test_case *params = test->param_value;
90+
DECLARE_BITMAP(bitmap, BITOPS_LENGTH);
91+
int bit_set;
92+
93+
bitmap_zero(bitmap, BITOPS_LENGTH);
94+
95+
KUNIT_EXPECT_FALSE(test, test_and_set_bit(params->nr, bitmap));
96+
KUNIT_EXPECT_TRUE(test, test_bit(params->nr, bitmap));
97+
98+
KUNIT_EXPECT_TRUE(test, test_and_set_bit(params->nr, bitmap));
99+
KUNIT_EXPECT_TRUE(test, test_bit(params->nr, bitmap));
100+
101+
KUNIT_EXPECT_TRUE(test, test_and_clear_bit(params->nr, bitmap));
102+
KUNIT_EXPECT_FALSE(test, test_bit(params->nr, bitmap));
103+
104+
KUNIT_EXPECT_FALSE(test, test_and_clear_bit(params->nr, bitmap));
105+
KUNIT_EXPECT_FALSE(test, test_bit(params->nr, bitmap));
106+
107+
bit_set = find_first_bit(bitmap, BITOPS_LENGTH);
108+
KUNIT_EXPECT_EQ(test, bit_set, BITOPS_LENGTH);
109+
}
110+
111+
static void test_test_and_change_bit(struct kunit *test)
112+
{
113+
const struct bitops_test_case *params = test->param_value;
114+
DECLARE_BITMAP(bitmap, BITOPS_LENGTH);
115+
int bit_set;
116+
117+
bitmap_zero(bitmap, BITOPS_LENGTH);
118+
119+
KUNIT_EXPECT_FALSE(test, test_and_change_bit(params->nr, bitmap));
120+
KUNIT_EXPECT_TRUE(test, test_bit(params->nr, bitmap));
121+
122+
KUNIT_EXPECT_TRUE(test, test_and_change_bit(params->nr, bitmap));
123+
KUNIT_EXPECT_FALSE(test, test_bit(params->nr, bitmap));
124+
125+
bit_set = find_first_bit(bitmap, BITOPS_LENGTH);
126+
KUNIT_EXPECT_EQ(test, bit_set, BITOPS_LENGTH);
127+
}
128+
129+
struct order_test_case {
130+
const char *str;
131+
const unsigned int count;
132+
const int expected;
133+
};
134+
135+
static struct order_test_case order_test_cases[] = {
136+
{"0x00000003", 0x00000003, 2},
137+
{"0x00000004", 0x00000004, 2},
138+
{"0x00001fff", 0x00001fff, 13},
139+
{"0x00002000", 0x00002000, 13},
140+
{"0x50000000", 0x50000000, 31},
141+
{"0x80000000", 0x80000000, 31},
142+
{"0x80003000", 0x80003000, 32},
143+
};
144+
145+
KUNIT_ARRAY_PARAM_DESC(order, order_test_cases, str);
146+
147+
static void test_get_count_order(struct kunit *test)
148+
{
149+
const struct order_test_case *params = test->param_value;
150+
151+
KUNIT_EXPECT_EQ(test, get_count_order(params->count), params->expected);
152+
KUNIT_EXPECT_EQ(test, get_count_order_long(params->count), params->expected);
153+
}
154+
155+
#ifdef CONFIG_64BIT
156+
struct order_long_test_case {
157+
const char *str;
158+
const unsigned long count;
159+
const int expected;
160+
};
161+
162+
static struct order_long_test_case order_long_test_cases[] = {
163+
{"0x0000000300000000", 0x0000000300000000, 34},
164+
{"0x0000000400000000", 0x0000000400000000, 34},
165+
{"0x00001fff00000000", 0x00001fff00000000, 45},
166+
{"0x0000200000000000", 0x0000200000000000, 45},
167+
{"0x5000000000000000", 0x5000000000000000, 63},
168+
{"0x8000000000000000", 0x8000000000000000, 63},
169+
{"0x8000300000000000", 0x8000300000000000, 64},
170+
};
171+
172+
KUNIT_ARRAY_PARAM_DESC(order_long, order_long_test_cases, str);
173+
174+
static void test_get_count_order_long(struct kunit *test)
175+
{
176+
const struct order_long_test_case *params = test->param_value;
177+
178+
KUNIT_EXPECT_EQ(test, get_count_order_long(params->count), params->expected);
179+
}
180+
#endif
181+
182+
static struct kunit_case bitops_test_cases[] = {
183+
KUNIT_CASE_PARAM(test_set_bit_clear_bit, bitops_gen_params),
184+
KUNIT_CASE_PARAM(test_change_bit, bitops_gen_params),
185+
KUNIT_CASE_PARAM(test_test_and_set_bit_test_and_clear_bit, bitops_gen_params),
186+
KUNIT_CASE_PARAM(test_test_and_change_bit, bitops_gen_params),
187+
KUNIT_CASE_PARAM(test_get_count_order, order_gen_params),
188+
#ifdef CONFIG_64BIT
189+
KUNIT_CASE_PARAM(test_get_count_order_long, order_long_gen_params),
190+
#endif
191+
{},
192+
};
193+
194+
static struct kunit_suite bitops_test_suite = {
195+
.name = "bitops",
196+
.test_cases = bitops_test_cases,
197+
};
198+
199+
kunit_test_suite(bitops_test_suite);
200+
201+
MODULE_AUTHOR("Jesse Brandeburg <jesse.brandeburg@intel.com>");
202+
MODULE_AUTHOR("Wei Yang <richard.weiyang@gmail.com>");
203+
MODULE_AUTHOR("Ryota Sakamoto <sakamo.ryota@gmail.com>");
204+
MODULE_LICENSE("GPL");
205+
MODULE_DESCRIPTION("Bit testing module");

rust/helpers/bitmap.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
#include <linux/bitmap.h>
44

5+
__rust_helper
56
void rust_helper_bitmap_copy_and_extend(unsigned long *to, const unsigned long *from,
67
unsigned int count, unsigned int size)
78
{

rust/helpers/bitops.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,25 @@
33
#include <linux/bitops.h>
44
#include <linux/find.h>
55

6+
__rust_helper
67
void rust_helper___set_bit(unsigned long nr, unsigned long *addr)
78
{
89
__set_bit(nr, addr);
910
}
1011

12+
__rust_helper
1113
void rust_helper___clear_bit(unsigned long nr, unsigned long *addr)
1214
{
1315
__clear_bit(nr, addr);
1416
}
1517

18+
__rust_helper
1619
void rust_helper_set_bit(unsigned long nr, volatile unsigned long *addr)
1720
{
1821
set_bit(nr, addr);
1922
}
2023

24+
__rust_helper
2125
void rust_helper_clear_bit(unsigned long nr, volatile unsigned long *addr)
2226
{
2327
clear_bit(nr, addr);

rust/helpers/cpumask.c

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,67 +2,80 @@
22

33
#include <linux/cpumask.h>
44

5+
__rust_helper
56
void rust_helper_cpumask_set_cpu(unsigned int cpu, struct cpumask *dstp)
67
{
78
cpumask_set_cpu(cpu, dstp);
89
}
910

11+
__rust_helper
1012
void rust_helper___cpumask_set_cpu(unsigned int cpu, struct cpumask *dstp)
1113
{
1214
__cpumask_set_cpu(cpu, dstp);
1315
}
1416

17+
__rust_helper
1518
void rust_helper_cpumask_clear_cpu(int cpu, struct cpumask *dstp)
1619
{
1720
cpumask_clear_cpu(cpu, dstp);
1821
}
1922

23+
__rust_helper
2024
void rust_helper___cpumask_clear_cpu(int cpu, struct cpumask *dstp)
2125
{
2226
__cpumask_clear_cpu(cpu, dstp);
2327
}
2428

29+
__rust_helper
2530
bool rust_helper_cpumask_test_cpu(int cpu, struct cpumask *srcp)
2631
{
2732
return cpumask_test_cpu(cpu, srcp);
2833
}
2934

35+
__rust_helper
3036
void rust_helper_cpumask_setall(struct cpumask *dstp)
3137
{
3238
cpumask_setall(dstp);
3339
}
3440

41+
__rust_helper
3542
bool rust_helper_cpumask_empty(struct cpumask *srcp)
3643
{
3744
return cpumask_empty(srcp);
3845
}
3946

47+
__rust_helper
4048
bool rust_helper_cpumask_full(struct cpumask *srcp)
4149
{
4250
return cpumask_full(srcp);
4351
}
4452

53+
__rust_helper
4554
unsigned int rust_helper_cpumask_weight(struct cpumask *srcp)
4655
{
4756
return cpumask_weight(srcp);
4857
}
4958

59+
__rust_helper
5060
void rust_helper_cpumask_copy(struct cpumask *dstp, const struct cpumask *srcp)
5161
{
5262
cpumask_copy(dstp, srcp);
5363
}
5464

65+
__rust_helper
5566
bool rust_helper_alloc_cpumask_var(cpumask_var_t *mask, gfp_t flags)
5667
{
5768
return alloc_cpumask_var(mask, flags);
5869
}
5970

71+
__rust_helper
6072
bool rust_helper_zalloc_cpumask_var(cpumask_var_t *mask, gfp_t flags)
6173
{
6274
return zalloc_cpumask_var(mask, flags);
6375
}
6476

6577
#ifndef CONFIG_CPUMASK_OFFSTACK
78+
__rust_helper
6679
void rust_helper_free_cpumask_var(cpumask_var_t mask)
6780
{
6881
free_cpumask_var(mask);

0 commit comments

Comments
 (0)