From mboxrd@z Thu Jan 1 00:00:00 1970 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 bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 1711EFD063E for ; Wed, 11 Mar 2026 07:10:36 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:MIME-Version:References:In-Reply-To: Message-ID:Date:Subject:Cc:To:From:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=7FEOSEIFOst9igVIKxIlmGbgsCIFwMNu4akyv7Bnh8Y=; b=FV4Gy6pQN576GX qkq5PQ5DdW61pmw0bYD0+h6C5iY2zvgsM+foA1t9NvZyNBcXkFEm2vJz9LPcxFZQCl5urM8EOsBZp BrBVkwTJnbd2b7lvwmKCHMans/n/Ukf+1WEapaiFCWJdx1Z+v0JdXvUjCGJ2ojFzDlFxr7V0hHY9N 8Nnfrx6TOzxNm3aYaH/Wq1HVUBAnOnn3vupjoa825rRUaZpxR98Q17tX7CU94pnT8xVjfN9M7YPlN NPQv6a7G0xLw3Jc3KEYh9zsW/0yJ35BsCCbErhc2gXyjidiUYc8jDFEzRknztHY4NL1kElcTkvCqU DQV7Or0GUTYOMTJNWX9g==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.98.2 #2 (Red Hat Linux)) id 1w0Di9-0000000B1nJ-3yiS; Wed, 11 Mar 2026 07:10:29 +0000 Received: from [212.243.42.10] (helo=localhost) by bombadil.infradead.org with esmtpsa (Exim 4.98.2 #2 (Red Hat Linux)) id 1w0Di7-0000000B1bE-3GZS; Wed, 11 Mar 2026 07:10:28 +0000 From: Christoph Hellwig To: Andrew Morton Cc: Richard Henderson , Matt Turner , Magnus Lindholm , Russell King , Catalin Marinas , Will Deacon , Huacai Chen , WANG Xuerui , Madhavan Srinivasan , Michael Ellerman , Nicholas Piggin , "Christophe Leroy (CS GROUP)" , Paul Walmsley , Palmer Dabbelt , Albert Ou , Alexandre Ghiti , Heiko Carstens , Vasily Gorbik , Alexander Gordeev , Christian Borntraeger , Sven Schnelle , "David S. Miller" , Andreas Larsson , Richard Weinberger , Anton Ivanov , Johannes Berg , Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , x86@kernel.org, "H. Peter Anvin" , Herbert Xu , Dan Williams , Chris Mason , David Sterba , Arnd Bergmann , Song Liu , Yu Kuai , Li Nan , "Theodore Ts'o" , "Jason A. Donenfeld" , linux-alpha@vger.kernel.org, linux-kernel@vger.kernel.org, linux-arm-kernel@lists.infradead.org, loongarch@lists.linux.dev, linuxppc-dev@lists.ozlabs.org, linux-riscv@lists.infradead.org, linux-s390@vger.kernel.org, sparclinux@vger.kernel.org, linux-um@lists.infradead.org, linux-crypto@vger.kernel.org, linux-btrfs@vger.kernel.org, linux-arch@vger.kernel.org, linux-raid@vger.kernel.org Subject: [PATCH 26/27] random: factor out a __limit_random_u32_below helper Date: Wed, 11 Mar 2026 08:03:58 +0100 Message-ID: <20260311070416.972667-27-hch@lst.de> X-Mailer: git-send-email 2.47.3 In-Reply-To: <20260311070416.972667-1-hch@lst.de> References: <20260311070416.972667-1-hch@lst.de> MIME-Version: 1.0 X-BeenThere: linux-riscv@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Sender: "linux-riscv" Errors-To: linux-riscv-bounces+linux-riscv=archiver.kernel.org@lists.infradead.org Factor out the guts of __get_random_u32_below into a new helper, so that callers with their own prng state can reuse this code. Signed-off-by: Christoph Hellwig --- drivers/char/random.c | 26 +++++++++++++++----------- include/linux/random.h | 1 + 2 files changed, 16 insertions(+), 11 deletions(-) diff --git a/drivers/char/random.c b/drivers/char/random.c index 7ff4d29911fd..23b5addf02fb 100644 --- a/drivers/char/random.c +++ b/drivers/char/random.c @@ -544,18 +544,16 @@ DEFINE_BATCHED_ENTROPY(u16) DEFINE_BATCHED_ENTROPY(u32) DEFINE_BATCHED_ENTROPY(u64) -u32 __get_random_u32_below(u32 ceil) +/* + * This is the slow path for variable ceil. It is still fast, most of the time, + * by doing traditional reciprocal multiplication and opportunistically + * comparing the lower half to ceil itself, before falling back to computing a + * larger bound, and then rejecting samples whose lower half would indicate a + * range indivisible by ceil. The use of `-ceil % ceil` is analogous to `2^32 % + * ceil`, but is computable in 32-bits. + */ +u32 __limit_random_u32_below(u32 ceil, u32 rand) { - /* - * This is the slow path for variable ceil. It is still fast, most of - * the time, by doing traditional reciprocal multiplication and - * opportunistically comparing the lower half to ceil itself, before - * falling back to computing a larger bound, and then rejecting samples - * whose lower half would indicate a range indivisible by ceil. The use - * of `-ceil % ceil` is analogous to `2^32 % ceil`, but is computable - * in 32-bits. - */ - u32 rand = get_random_u32(); u64 mult; /* @@ -577,6 +575,12 @@ u32 __get_random_u32_below(u32 ceil) } return mult >> 32; } +EXPORT_SYMBOL_GPL(__limit_random_u32_below); + +u32 __get_random_u32_below(u32 ceil) +{ + return __limit_random_u32_below(ceil, get_random_u32()); +} EXPORT_SYMBOL(__get_random_u32_below); #ifdef CONFIG_SMP diff --git a/include/linux/random.h b/include/linux/random.h index 8a8064dc3970..54401dd53f68 100644 --- a/include/linux/random.h +++ b/include/linux/random.h @@ -50,6 +50,7 @@ static inline unsigned long get_random_long(void) #endif } +u32 __limit_random_u32_below(u32 ceil, u32 rand); u32 __get_random_u32_below(u32 ceil); /* -- 2.47.3 _______________________________________________ linux-riscv mailing list linux-riscv@lists.infradead.org http://lists.infradead.org/mailman/listinfo/linux-riscv