Thread (31 messages) 31 messages, 8 authors, 2022-10-13

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

From: "Jason A. Donenfeld" <Jason@zx2c4.com>
Date: 2022-10-06 15:02:02
Also in: dri-devel, linux-f2fs-devel, linux-hams, linux-nvme, lvs-devel

On Thu, Oct 06, 2022 at 10:43:31AM +0200, Jan Kara wrote:
The code here is effectively doing the

	parent_group = prandom_u32_max(ngroups);

Similarly here we can use prandom_u32_max(ngroups) like:

		if (qstr) {
			...
			parent_group = hinfo.hash % ngroups;
		} else
			parent_group = prandom_u32_max(ngroups);
Nice catch. I'll move these to patch #1.

quoted
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.
I'm not handling this during this patch set, but if in the course of
review we find enough places that want actually uniformly bounded
integers, I'll implement efficient rejection sampling to clean up these
cases, with something faster and general, and add a new function for it.
So far this is the first case to come up, but we'll probably eventually
find others. So I'll make note of this.

Jason
Keyboard shortcuts
hback out one level
jnext message in thread
kprevious message in thread
ldrill in
Escclose help / fold thread tree
?toggle this help