1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
|
// SPDX-License-Identifier: GPL-2.0-only
/*
* KUnit tests for the scalable bitmap (sbitmap) implementation.
*
* sbitmap is the tag allocator used by blk-mq and the I/O schedulers built on
* top of it, but it has no tests. These tests pin down the observable
* behaviour of the plain, synchronous parts of the API.
*
* Copyright (C) 2026 Wang Yibo <szwyb123@gmail.com>
*/
#include <linux/bitmap.h>
#include <linux/gfp.h>
#include <linux/sbitmap.h>
#include <kunit/test.h>
/*
* Big enough to span several words for any plausible BITS_PER_LONG, and not a
* power of two so that the last word is partially used.
*/
#define SBITMAP_TEST_DEPTH (3 * BITS_PER_LONG + 5)
static void sbitmap_test_init(struct kunit *test)
{
const unsigned int depth = 97;
struct sbitmap sb;
int ret;
ret = sbitmap_init_node(&sb, depth, -1, GFP_KERNEL, NUMA_NO_NODE,
false, true);
KUNIT_ASSERT_EQ(test, ret, 0);
KUNIT_EXPECT_EQ(test, sb.depth, depth);
KUNIT_EXPECT_EQ(test, sb.shift,
(unsigned int)sbitmap_calculate_shift(depth));
KUNIT_EXPECT_EQ(test, sb.map_nr, DIV_ROUND_UP(depth, 1U << sb.shift));
KUNIT_EXPECT_FALSE(test, sb.round_robin);
/*
* alloc_hint is a __percpu pointer, which the generic pointer
* assertions cannot take: on x86, where percpu data lives in
* __seg_gs, passing it through a const void * does not compile.
* Test it as a plain boolean instead.
*/
KUNIT_EXPECT_TRUE(test, sb.alloc_hint);
KUNIT_EXPECT_FALSE(test, sbitmap_any_bit_set(&sb));
KUNIT_EXPECT_EQ(test, sbitmap_weight(&sb), 0U);
sbitmap_free(&sb);
/* An explicitly requested shift is honoured. */
ret = sbitmap_init_node(&sb, depth, 3, GFP_KERNEL, NUMA_NO_NODE,
false, true);
KUNIT_ASSERT_EQ(test, ret, 0);
KUNIT_EXPECT_EQ(test, sb.shift, 3U);
KUNIT_EXPECT_EQ(test, sb.map_nr, DIV_ROUND_UP(depth, 1U << 3));
sbitmap_free(&sb);
/* BITS_PER_LONG bits per word is the largest representable granularity. */
ret = sbitmap_init_node(&sb, depth, ilog2(BITS_PER_LONG), GFP_KERNEL,
NUMA_NO_NODE, false, true);
KUNIT_ASSERT_EQ(test, ret, 0);
KUNIT_EXPECT_EQ(test, sb.shift, (unsigned int)ilog2(BITS_PER_LONG));
KUNIT_EXPECT_EQ(test, sb.map_nr, DIV_ROUND_UP(depth, BITS_PER_LONG));
sbitmap_free(&sb);
/* Anything above that cannot be represented and must be rejected. */
ret = sbitmap_init_node(&sb, depth, ilog2(BITS_PER_LONG) + 1,
GFP_KERNEL, NUMA_NO_NODE, false, true);
KUNIT_EXPECT_EQ(test, ret, -EINVAL);
}
static void sbitmap_test_calculate_shift(struct kunit *test)
{
/* Maps too small to spread over several words keep the default. */
KUNIT_EXPECT_EQ(test, sbitmap_calculate_shift(0), ilog2(BITS_PER_LONG));
KUNIT_EXPECT_EQ(test, sbitmap_calculate_shift(1), ilog2(BITS_PER_LONG));
KUNIT_EXPECT_EQ(test, sbitmap_calculate_shift(3), ilog2(BITS_PER_LONG));
/* Four bits map to one bit per word. */
KUNIT_EXPECT_EQ(test, sbitmap_calculate_shift(4), 0);
/* The shift grows back as the depth increases. */
KUNIT_EXPECT_EQ(test, sbitmap_calculate_shift(4 * BITS_PER_LONG - 1),
ilog2(BITS_PER_LONG) - 1);
KUNIT_EXPECT_EQ(test, sbitmap_calculate_shift(4 * BITS_PER_LONG),
ilog2(BITS_PER_LONG));
}
static void sbitmap_test_get_exhaust(struct kunit *test)
{
DECLARE_BITMAP(seen, SBITMAP_TEST_DEPTH) = { 0 };
struct sbitmap sb;
unsigned int i;
int bit;
KUNIT_ASSERT_EQ(test, sbitmap_init_node(&sb, SBITMAP_TEST_DEPTH, -1,
GFP_KERNEL, NUMA_NO_NODE,
false, true), 0);
/* Every bit is handed out exactly once, then the map is empty. */
for (i = 0; i < SBITMAP_TEST_DEPTH; i++) {
bit = sbitmap_get(&sb);
KUNIT_ASSERT_GE(test, bit, 0);
KUNIT_ASSERT_LT(test, bit, (int)SBITMAP_TEST_DEPTH);
KUNIT_ASSERT_FALSE(test, test_bit(bit, seen));
__set_bit(bit, seen);
}
KUNIT_EXPECT_TRUE(test, sbitmap_any_bit_set(&sb));
KUNIT_EXPECT_EQ(test, sbitmap_weight(&sb),
(unsigned int)SBITMAP_TEST_DEPTH);
KUNIT_EXPECT_EQ(test, sbitmap_get(&sb), -1);
sbitmap_free(&sb);
}
static void sbitmap_test_put_reuse(struct kunit *test)
{
struct sbitmap sb;
unsigned int i;
int bit;
KUNIT_ASSERT_EQ(test, sbitmap_init_node(&sb, BITS_PER_LONG, -1,
GFP_KERNEL, NUMA_NO_NODE,
true, true), 0);
for (i = 0; i < BITS_PER_LONG; i++)
KUNIT_ASSERT_GE(test, sbitmap_get(&sb), 0);
KUNIT_EXPECT_EQ(test, sbitmap_weight(&sb), (unsigned int)BITS_PER_LONG);
KUNIT_EXPECT_EQ(test, sbitmap_get(&sb), -1);
/*
* Freeing a single bit must make exactly that bit available again, no
* matter which bit it was.
*/
for (i = 0; i < BITS_PER_LONG; i++) {
sbitmap_put(&sb, i);
KUNIT_EXPECT_EQ(test, sbitmap_weight(&sb),
(unsigned int)BITS_PER_LONG - 1);
bit = sbitmap_get(&sb);
KUNIT_EXPECT_EQ(test, bit, (int)i);
KUNIT_EXPECT_EQ(test, sbitmap_weight(&sb),
(unsigned int)BITS_PER_LONG);
KUNIT_EXPECT_EQ(test, sbitmap_get(&sb), -1);
}
sbitmap_free(&sb);
}
static void sbitmap_test_round_robin(struct kunit *test)
{
struct sbitmap sb;
unsigned int i;
int bit, prev = -1;
KUNIT_ASSERT_EQ(test, sbitmap_init_node(&sb, SBITMAP_TEST_DEPTH, -1,
GFP_KERNEL, NUMA_NO_NODE,
true, true), 0);
/*
* round_robin must hand out strictly increasing bit numbers while the
* map fills up from empty.
*/
for (i = 0; i < SBITMAP_TEST_DEPTH; i++) {
bit = sbitmap_get(&sb);
KUNIT_ASSERT_GE(test, bit, 0);
KUNIT_ASSERT_LT(test, bit, (int)SBITMAP_TEST_DEPTH);
if (prev != -1)
KUNIT_ASSERT_GT(test, bit, prev);
prev = bit;
}
KUNIT_EXPECT_EQ(test, sbitmap_weight(&sb),
(unsigned int)SBITMAP_TEST_DEPTH);
KUNIT_EXPECT_EQ(test, sbitmap_get(&sb), -1);
sbitmap_free(&sb);
}
static void sbitmap_test_resize(struct kunit *test)
{
const unsigned int depth = 4 * BITS_PER_LONG;
struct sbitmap sb;
unsigned int i;
KUNIT_ASSERT_EQ(test, sbitmap_init_node(&sb, depth, -1, GFP_KERNEL,
NUMA_NO_NODE, true, true), 0);
for (i = 0; i < depth; i++)
KUNIT_ASSERT_GE(test, sbitmap_get(&sb), 0);
KUNIT_EXPECT_EQ(test, sbitmap_weight(&sb), depth);
/* Shrinking hides the bits past the new depth. */
sbitmap_resize(&sb, depth / 2);
KUNIT_EXPECT_EQ(test, sb.depth, depth / 2);
KUNIT_EXPECT_EQ(test, sb.map_nr,
DIV_ROUND_UP(depth / 2, 1U << sb.shift));
KUNIT_EXPECT_EQ(test, sbitmap_weight(&sb), depth / 2);
KUNIT_EXPECT_EQ(test, sbitmap_get(&sb), -1);
/* Growing makes them visible again; they are still allocated. */
sbitmap_resize(&sb, depth);
KUNIT_EXPECT_EQ(test, sb.depth, depth);
KUNIT_EXPECT_EQ(test, sbitmap_weight(&sb), depth);
KUNIT_EXPECT_EQ(test, sbitmap_get(&sb), -1);
sbitmap_free(&sb);
}
static void sbitmap_test_set_clear_bit(struct kunit *test)
{
static const unsigned int bits[] = {
0, 1, BITS_PER_LONG - 1, BITS_PER_LONG,
2 * BITS_PER_LONG + 3, SBITMAP_TEST_DEPTH - 1
};
struct sbitmap sb;
unsigned int i;
KUNIT_ASSERT_EQ(test, sbitmap_init_node(&sb, SBITMAP_TEST_DEPTH, -1,
GFP_KERNEL, NUMA_NO_NODE,
false, true), 0);
KUNIT_EXPECT_FALSE(test, sbitmap_any_bit_set(&sb));
for (i = 0; i < ARRAY_SIZE(bits); i++) {
sbitmap_set_bit(&sb, bits[i]);
KUNIT_EXPECT_TRUE(test, sbitmap_test_bit(&sb, bits[i]));
}
KUNIT_EXPECT_TRUE(test, sbitmap_any_bit_set(&sb));
KUNIT_EXPECT_EQ(test, sbitmap_weight(&sb), (unsigned int)ARRAY_SIZE(bits));
for (i = 0; i < ARRAY_SIZE(bits); i++) {
sbitmap_clear_bit(&sb, bits[i]);
KUNIT_EXPECT_FALSE(test, sbitmap_test_bit(&sb, bits[i]));
}
KUNIT_EXPECT_FALSE(test, sbitmap_any_bit_set(&sb));
KUNIT_EXPECT_EQ(test, sbitmap_weight(&sb), 0U);
sbitmap_free(&sb);
}
struct sbitmap_iter_ctx {
struct kunit *test;
DECLARE_BITMAP(visited, SBITMAP_TEST_DEPTH);
unsigned int nr_visited;
};
static bool sbitmap_iter_collect(struct sbitmap *sb, unsigned int bitnr,
void *data)
{
struct sbitmap_iter_ctx *ctx = data;
KUNIT_ASSERT_LT(ctx->test, bitnr, SBITMAP_TEST_DEPTH);
__set_bit(bitnr, ctx->visited);
ctx->nr_visited++;
return true;
}
static void sbitmap_test_iterate_set(struct kunit *test)
{
static const unsigned int bits[] = {
0, 5, BITS_PER_LONG - 1, BITS_PER_LONG, 2 * BITS_PER_LONG + 3,
SBITMAP_TEST_DEPTH - 1
};
struct sbitmap_iter_ctx ctx = { .test = test };
struct sbitmap sb;
unsigned int i;
KUNIT_ASSERT_EQ(test, sbitmap_init_node(&sb, SBITMAP_TEST_DEPTH, -1,
GFP_KERNEL, NUMA_NO_NODE,
false, true), 0);
for (i = 0; i < ARRAY_SIZE(bits); i++) {
KUNIT_ASSERT_LT(test, bits[i], SBITMAP_TEST_DEPTH);
sbitmap_set_bit(&sb, bits[i]);
}
sbitmap_for_each_set(&sb, sbitmap_iter_collect, &ctx);
KUNIT_EXPECT_EQ(test, ctx.nr_visited, (unsigned int)ARRAY_SIZE(bits));
for (i = 0; i < ARRAY_SIZE(bits); i++)
KUNIT_EXPECT_TRUE(test, test_bit(bits[i], ctx.visited));
sbitmap_free(&sb);
}
static struct kunit_case sbitmap_test_cases[] = {
KUNIT_CASE(sbitmap_test_init),
KUNIT_CASE(sbitmap_test_calculate_shift),
KUNIT_CASE(sbitmap_test_get_exhaust),
KUNIT_CASE(sbitmap_test_put_reuse),
KUNIT_CASE(sbitmap_test_round_robin),
KUNIT_CASE(sbitmap_test_resize),
KUNIT_CASE(sbitmap_test_set_clear_bit),
KUNIT_CASE(sbitmap_test_iterate_set),
{}
};
static struct kunit_suite sbitmap_test_suite = {
.name = "sbitmap",
.test_cases = sbitmap_test_cases,
};
kunit_test_suite(sbitmap_test_suite);
MODULE_DESCRIPTION("KUnit test for sbitmap");
MODULE_LICENSE("GPL");
|