7
7
8
8
#include <kunit/test.h>
9
9
#include <linux/atomic.h>
10
+ #include <linux/bitops.h>
10
11
#include <linux/random.h>
11
12
#include <linux/spinlock.h>
12
13
@@ -25,7 +26,7 @@ static void __init init_id(atomic64_t *const counter, const u32 random_32bits)
25
26
* Ensures sure 64-bit values are always used by user space (or may
26
27
* fail with -EOVERFLOW), and makes this testable.
27
28
*/
28
- init = 1ULL << 32 ;
29
+ init = BIT_ULL ( 32 ) ;
29
30
30
31
/*
31
32
* Makes a large (2^32) boot-time value to limit ID collision in logs
@@ -105,7 +106,7 @@ static u64 get_id_range(size_t number_of_ids, atomic64_t *const counter,
105
106
* to get a new ID (e.g. a full landlock_restrict_self() call), and the
106
107
* cost of draining all available IDs during the system's uptime.
107
108
*/
108
- random_4bits = random_4bits % ( 1 << 4 ) ;
109
+ random_4bits &= 0b1111 ;
109
110
step = number_of_ids + random_4bits ;
110
111
111
112
/* It is safe to cast a signed atomic to an unsigned value. */
@@ -144,6 +145,19 @@ static void test_range1_rand1(struct kunit *const test)
144
145
init + 2 );
145
146
}
146
147
148
+ static void test_range1_rand15 (struct kunit * const test )
149
+ {
150
+ atomic64_t counter ;
151
+ u64 init ;
152
+
153
+ init = get_random_u32 ();
154
+ atomic64_set (& counter , init );
155
+ KUNIT_EXPECT_EQ (test , get_id_range (1 , & counter , 15 ), init );
156
+ KUNIT_EXPECT_EQ (
157
+ test , get_id_range (get_random_u8 (), & counter , get_random_u8 ()),
158
+ init + 16 );
159
+ }
160
+
147
161
static void test_range1_rand16 (struct kunit * const test )
148
162
{
149
163
atomic64_t counter ;
@@ -196,6 +210,19 @@ static void test_range2_rand2(struct kunit *const test)
196
210
init + 4 );
197
211
}
198
212
213
+ static void test_range2_rand15 (struct kunit * const test )
214
+ {
215
+ atomic64_t counter ;
216
+ u64 init ;
217
+
218
+ init = get_random_u32 ();
219
+ atomic64_set (& counter , init );
220
+ KUNIT_EXPECT_EQ (test , get_id_range (2 , & counter , 15 ), init );
221
+ KUNIT_EXPECT_EQ (
222
+ test , get_id_range (get_random_u8 (), & counter , get_random_u8 ()),
223
+ init + 17 );
224
+ }
225
+
199
226
static void test_range2_rand16 (struct kunit * const test )
200
227
{
201
228
atomic64_t counter ;
@@ -232,10 +259,12 @@ static struct kunit_case __refdata test_cases[] = {
232
259
KUNIT_CASE (test_init_once ),
233
260
KUNIT_CASE (test_range1_rand0 ),
234
261
KUNIT_CASE (test_range1_rand1 ),
262
+ KUNIT_CASE (test_range1_rand15 ),
235
263
KUNIT_CASE (test_range1_rand16 ),
236
264
KUNIT_CASE (test_range2_rand0 ),
237
265
KUNIT_CASE (test_range2_rand1 ),
238
266
KUNIT_CASE (test_range2_rand2 ),
267
+ KUNIT_CASE (test_range2_rand15 ),
239
268
KUNIT_CASE (test_range2_rand16 ),
240
269
{}
241
270
/* clang-format on */
0 commit comments