[Drbd-dev] [PATCH v1 3/5] treewide: use get_random_u32() when possible

Jan Kara jack at suse.cz
Thu Oct 6 10:43:31 CEST 2022


On Wed 05-10-22 23:48:42, Jason A. Donenfeld wrote:
> The prandom_u32() function has been a deprecated inline wrapper around
> get_random_u32() for several releases now, and compiles down to the
> exact same code. Replace the deprecated wrapper with a direct call to
> the real function.
> 
> Signed-off-by: Jason A. Donenfeld <Jason at zx2c4.com>

...

> diff --git a/fs/ext2/ialloc.c b/fs/ext2/ialloc.c
> index 998dd2ac8008..e439a872c398 100644
> --- a/fs/ext2/ialloc.c
> +++ b/fs/ext2/ialloc.c
> @@ -277,7 +277,7 @@ static int find_group_orlov(struct super_block *sb, struct inode *parent)
>  		int best_ndir = inodes_per_group;
>  		int best_group = -1;
>  
> -		group = prandom_u32();
> +		group = get_random_u32();
>  		parent_group = (unsigned)group % ngroups;
>  		for (i = 0; i < ngroups; i++) {
>  			group = (parent_group + i) % ngroups;

The code here is effectively doing the

	parent_group = prandom_u32_max(ngroups);

> diff --git a/fs/ext4/ialloc.c b/fs/ext4/ialloc.c
> index f73e5eb43eae..954ec9736a8d 100644
> --- a/fs/ext4/ialloc.c
> +++ b/fs/ext4/ialloc.c
> @@ -465,7 +465,7 @@ static int find_group_orlov(struct super_block *sb, struct inode *parent,
>  			ext4fs_dirhash(parent, qstr->name, qstr->len, &hinfo);
>  			grp = hinfo.hash;
>  		} else
> -			grp = prandom_u32();
> +			grp = get_random_u32();

Similarly here we can use prandom_u32_max(ngroups) like:

		if (qstr) {
			...
			parent_group = hinfo.hash % ngroups;
		} else
			parent_group = prandom_u32_max(ngroups);

> diff --git a/fs/ext4/mmp.c b/fs/ext4/mmp.c
> index 9af68a7ecdcf..588cb09c5291 100644
> --- a/fs/ext4/mmp.c
> +++ b/fs/ext4/mmp.c
> @@ -265,7 +265,7 @@ static unsigned int mmp_new_seq(void)
>  	u32 new_seq;
>  
>  	do {
> -		new_seq = prandom_u32();
> +		new_seq = get_random_u32();
>  	} while (new_seq > EXT4_MMP_SEQ_MAX);

OK, here we again effectively implement prandom_u32_max(EXT4_MMP_SEQ_MAX + 1).
Just presumably we didn't want to use modulo here because EXT4_MMP_SEQ_MAX
is rather big and so the resulting 'new_seq' would be seriously
non-uniform.

								Honza
-- 
Jan Kara <jack at suse.com>
SUSE Labs, CR


More information about the drbd-dev mailing list