From nobody Sat Nov 16 00:57:41 2024 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 991DAC433F5 for ; Mon, 10 Oct 2022 23:08:37 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230047AbiJJXIg (ORCPT ); Mon, 10 Oct 2022 19:08:36 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:52138 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229945AbiJJXIa (ORCPT ); Mon, 10 Oct 2022 19:08:30 -0400 Received: from ams.source.kernel.org (ams.source.kernel.org [145.40.68.75]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 0C21D7B1C9; Mon, 10 Oct 2022 16:07:23 -0700 (PDT) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ams.source.kernel.org (Postfix) with ESMTPS id AC6D1B80EB7; Mon, 10 Oct 2022 23:07:21 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 725E9C433C1; Mon, 10 Oct 2022 23:07:14 +0000 (UTC) Authentication-Results: smtp.kernel.org; dkim=pass (1024-bit key) header.d=zx2c4.com header.i=@zx2c4.com header.b="iCV0PUgs" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=zx2c4.com; s=20210105; t=1665443233; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=/gaEwlFkPoVgdZQ7oNDn7xk98mXWF7hGEdKqEVicSg8=; b=iCV0PUgsFgGEFTuCnybE0WXe7GIB5vQWZ71Z2u87DeC3sS1LTUZg7W7Rl2Q/9sH4JzLWV0 YOjN9YvNjsRp0bwMNv7NLhHz2Vvw1HXZxeh83nfFsemOB8tKSiO+njBpu91HEPpJR5v23j 60zHpp6WqDTFZjKz3UsVT0FoqweKAcs= Received: by mail.zx2c4.com (ZX2C4 Mail Server) with ESMTPSA id e346dd3e (TLSv1.3:TLS_AES_256_GCM_SHA384:256:NO); Mon, 10 Oct 2022 23:07:12 +0000 (UTC) From: "Jason A. Donenfeld" To: linux-kernel@vger.kernel.org, patches@lists.linux.dev Cc: "Jason A. Donenfeld" , Andreas Noever , Andrew Morton , Andy Shevchenko , Borislav Petkov , Catalin Marinas , =?UTF-8?q?Christoph=20B=C3=B6hmwalder?= , Christoph Hellwig , Christophe Leroy , Daniel Borkmann , Dave Airlie , Dave Hansen , "David S . Miller" , Eric Dumazet , Florian Westphal , Greg Kroah-Hartman , "H . Peter Anvin" , Heiko Carstens , Helge Deller , Herbert Xu , Huacai Chen , Hugh Dickins , Jakub Kicinski , "James E . J . Bottomley" , Jan Kara , Jason Gunthorpe , Jens Axboe , Johannes Berg , Jonathan Corbet , Jozsef Kadlecsik , KP Singh , Kees Cook , Marco Elver , Mauro Carvalho Chehab , Michael Ellerman , Pablo Neira Ayuso , Paolo Abeni , Peter Zijlstra , Richard Weinberger , Russell King , Theodore Ts'o , Thomas Bogendoerfer , Thomas Gleixner , Thomas Graf , Ulf Hansson , Vignesh Raghavendra , WANG Xuerui , Will Deacon , Yury Norov , dri-devel@lists.freedesktop.org, kasan-dev@googlegroups.com, kernel-janitors@vger.kernel.org, linux-arm-kernel@lists.infradead.org, linux-block@vger.kernel.org, linux-crypto@vger.kernel.org, linux-doc@vger.kernel.org, linux-fsdevel@vger.kernel.org, linux-media@vger.kernel.org, linux-mips@vger.kernel.org, linux-mm@kvack.org, linux-mmc@vger.kernel.org, linux-mtd@lists.infradead.org, linux-nvme@lists.infradead.org, linux-parisc@vger.kernel.org, linux-rdma@vger.kernel.org, linux-s390@vger.kernel.org, linux-um@lists.infradead.org, linux-usb@vger.kernel.org, linux-wireless@vger.kernel.org, linuxppc-dev@lists.ozlabs.org, loongarch@lists.linux.dev, netdev@vger.kernel.org, sparclinux@vger.kernel.org, x86@kernel.org, Jan Kara Subject: [PATCH v6 2/7] treewide: use prandom_u32_max() when possible, part 2 Date: Mon, 10 Oct 2022 17:06:08 -0600 Message-Id: <20221010230613.1076905-3-Jason@zx2c4.com> In-Reply-To: <20221010230613.1076905-1-Jason@zx2c4.com> References: <20221010230613.1076905-1-Jason@zx2c4.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" 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 Reviewed-by: Kees Cook Reviewed-by: Yury Norov Reviewed-by: Jan Kara # for ext2, ext4, and sbitmap Signed-off-by: Jason A. Donenfeld --- fs/ext2/ialloc.c | 3 +-- fs/ext4/ialloc.c | 5 ++--- lib/sbitmap.c | 2 +- lib/test_vmalloc.c | 17 ++++------------- 4 files changed, 8 insertions(+), 19 deletions(-) diff --git a/fs/ext2/ialloc.c b/fs/ext2/ialloc.c index 998dd2ac8008..f4944c4dee60 100644 --- a/fs/ext2/ialloc.c +++ b/fs/ext2/ialloc.c @@ -277,8 +277,7 @@ static int find_group_orlov(struct super_block *sb, str= uct inode *parent) int best_ndir =3D inodes_per_group; int best_group =3D -1; =20 - group =3D prandom_u32(); - parent_group =3D (unsigned)group % ngroups; + parent_group =3D prandom_u32_max(ngroups); for (i =3D 0; i < ngroups; i++) { group =3D (parent_group + i) % ngroups; desc =3D ext2_get_group_desc (sb, group, NULL); diff --git a/fs/ext4/ialloc.c b/fs/ext4/ialloc.c index 208b87ce8858..7575aa359675 100644 --- a/fs/ext4/ialloc.c +++ b/fs/ext4/ialloc.c @@ -463,10 +463,9 @@ static int find_group_orlov(struct super_block *sb, st= ruct inode *parent, hinfo.hash_version =3D DX_HASH_HALF_MD4; hinfo.seed =3D sbi->s_hash_seed; ext4fs_dirhash(parent, qstr->name, qstr->len, &hinfo); - grp =3D hinfo.hash; + parent_group =3D hinfo.hash % ngroups; } else - grp =3D prandom_u32(); - parent_group =3D (unsigned)grp % ngroups; + parent_group =3D prandom_u32_max(ngroups); for (i =3D 0; i < ngroups; i++) { g =3D (parent_group + i) % ngroups; get_orlov_stats(sb, g, flex_size, &stats); diff --git a/lib/sbitmap.c b/lib/sbitmap.c index 055dac069afb..7280ae8ca88c 100644 --- a/lib/sbitmap.c +++ b/lib/sbitmap.c @@ -21,7 +21,7 @@ static int init_alloc_hint(struct sbitmap *sb, gfp_t flag= s) int i; =20 for_each_possible_cpu(i) - *per_cpu_ptr(sb->alloc_hint, i) =3D prandom_u32() % depth; + *per_cpu_ptr(sb->alloc_hint, i) =3D prandom_u32_max(depth); } return 0; } diff --git a/lib/test_vmalloc.c b/lib/test_vmalloc.c index 4f2f2d1bac56..a26bbbf20e62 100644 --- a/lib/test_vmalloc.c +++ b/lib/test_vmalloc.c @@ -151,9 +151,7 @@ static int random_size_alloc_test(void) int i; =20 for (i =3D 0; i < test_loop_count; i++) { - n =3D prandom_u32(); - n =3D (n % 100) + 1; - + n =3D prandom_u32_max(100) + 1; p =3D vmalloc(n * PAGE_SIZE); =20 if (!p) @@ -293,16 +291,12 @@ pcpu_alloc_test(void) return -1; =20 for (i =3D 0; i < 35000; i++) { - unsigned int r; - - r =3D prandom_u32(); - size =3D (r % (PAGE_SIZE / 4)) + 1; + size =3D prandom_u32_max(PAGE_SIZE / 4) + 1; =20 /* * Maximum PAGE_SIZE */ - r =3D prandom_u32(); - align =3D 1 << ((r % 11) + 1); + align =3D 1 << (prandom_u32_max(11) + 1); =20 pcpu[i] =3D __alloc_percpu(size, align); if (!pcpu[i]) @@ -393,14 +387,11 @@ static struct test_driver { =20 static void shuffle_array(int *arr, int n) { - unsigned int rnd; int i, j; =20 for (i =3D n - 1; i > 0; i--) { - rnd =3D prandom_u32(); - /* Cut the range. */ - j =3D rnd % i; + j =3D prandom_u32_max(i); =20 /* Swap indexes. */ swap(arr[i], arr[j]); --=20 2.37.3