Skip to content

Commit 8b3ccbc

Browse files
committed
treewide: use prandom_u32_max() when possible, part 2
Rather than incurring a division or requesting too many random bytes for the given range, use the prandom_u32_max() function, which only takes the minimum required bytes from the RNG and avoids divisions. This was done by hand, covering things that coccinelle could not do on its own. Reviewed-by: Greg Kroah-Hartman <[email protected]> Reviewed-by: Kees Cook <[email protected]> Reviewed-by: Yury Norov <[email protected]> Reviewed-by: Jan Kara <[email protected]> # for ext2, ext4, and sbitmap Acked-by: Jakub Kicinski <[email protected]> Signed-off-by: Jason A. Donenfeld <[email protected]>
1 parent 81895a6 commit 8b3ccbc

File tree

4 files changed

+8
-19
lines changed

4 files changed

+8
-19
lines changed

fs/ext2/ialloc.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -277,8 +277,7 @@ static int find_group_orlov(struct super_block *sb, struct inode *parent)
277277
int best_ndir = inodes_per_group;
278278
int best_group = -1;
279279

280-
group = prandom_u32();
281-
parent_group = (unsigned)group % ngroups;
280+
parent_group = prandom_u32_max(ngroups);
282281
for (i = 0; i < ngroups; i++) {
283282
group = (parent_group + i) % ngroups;
284283
desc = ext2_get_group_desc (sb, group, NULL);

fs/ext4/ialloc.c

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -463,10 +463,9 @@ static int find_group_orlov(struct super_block *sb, struct inode *parent,
463463
hinfo.hash_version = DX_HASH_HALF_MD4;
464464
hinfo.seed = sbi->s_hash_seed;
465465
ext4fs_dirhash(parent, qstr->name, qstr->len, &hinfo);
466-
grp = hinfo.hash;
466+
parent_group = hinfo.hash % ngroups;
467467
} else
468-
grp = prandom_u32();
469-
parent_group = (unsigned)grp % ngroups;
468+
parent_group = prandom_u32_max(ngroups);
470469
for (i = 0; i < ngroups; i++) {
471470
g = (parent_group + i) % ngroups;
472471
get_orlov_stats(sb, g, flex_size, &stats);

lib/sbitmap.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ static int init_alloc_hint(struct sbitmap *sb, gfp_t flags)
2121
int i;
2222

2323
for_each_possible_cpu(i)
24-
*per_cpu_ptr(sb->alloc_hint, i) = prandom_u32() % depth;
24+
*per_cpu_ptr(sb->alloc_hint, i) = prandom_u32_max(depth);
2525
}
2626
return 0;
2727
}

lib/test_vmalloc.c

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -151,9 +151,7 @@ static int random_size_alloc_test(void)
151151
int i;
152152

153153
for (i = 0; i < test_loop_count; i++) {
154-
n = prandom_u32();
155-
n = (n % 100) + 1;
156-
154+
n = prandom_u32_max(100) + 1;
157155
p = vmalloc(n * PAGE_SIZE);
158156

159157
if (!p)
@@ -293,16 +291,12 @@ pcpu_alloc_test(void)
293291
return -1;
294292

295293
for (i = 0; i < 35000; i++) {
296-
unsigned int r;
297-
298-
r = prandom_u32();
299-
size = (r % (PAGE_SIZE / 4)) + 1;
294+
size = prandom_u32_max(PAGE_SIZE / 4) + 1;
300295

301296
/*
302297
* Maximum PAGE_SIZE
303298
*/
304-
r = prandom_u32();
305-
align = 1 << ((r % 11) + 1);
299+
align = 1 << (prandom_u32_max(11) + 1);
306300

307301
pcpu[i] = __alloc_percpu(size, align);
308302
if (!pcpu[i])
@@ -393,14 +387,11 @@ static struct test_driver {
393387

394388
static void shuffle_array(int *arr, int n)
395389
{
396-
unsigned int rnd;
397390
int i, j;
398391

399392
for (i = n - 1; i > 0; i--) {
400-
rnd = prandom_u32();
401-
402393
/* Cut the range. */
403-
j = rnd % i;
394+
j = prandom_u32_max(i);
404395

405396
/* Swap indexes. */
406397
swap(arr[i], arr[j]);

0 commit comments

Comments
 (0)