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 lists.ozlabs.org (lists.ozlabs.org [112.213.38.117]) (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 5DBF9FD063E for ; Wed, 11 Mar 2026 07:10:45 +0000 (UTC) Received: from boromir.ozlabs.org (localhost [127.0.0.1]) by lists.ozlabs.org (Postfix) with ESMTP id 4fW22q54cVz3dkm; Wed, 11 Mar 2026 18:10:43 +1100 (AEDT) Authentication-Results: lists.ozlabs.org; arc=none smtp.remote-ip="2607:7c80:54:3::133" ARC-Seal: i=1; a=rsa-sha256; d=lists.ozlabs.org; s=201707; t=1773213043; cv=none; b=Uz1qj+5JJJDmaRmHW49sNH+UPaEQWmRg2Vv63/RJUwl5hkXpaRDgcb1wJRhm1cWvMVukMdPT38fohnWviBUpYI3BsgYBqoIzmXYYzg/bPSv9xfRVorluZ+49zdTqlKafeiFhR+ls1+PzrdT+mHlmuxhk323VtovMNu0B8WV7wxWTdtdBS9bYSn6uqYnIt05SD3k/YILPbKbJ8T1qYoBm5st+Bpzctbl33WCLjEwbVW9LpFJo6ch+R6MivfRNm4JNp2Eo3tqXJE3H1hhuddkf4lcApNsmQuoL2Ewfc9GLzyp3Wd02yJQXeNYcEnaeGJI4P2mOrLANBmcxNJzXi6WGog== ARC-Message-Signature: i=1; a=rsa-sha256; d=lists.ozlabs.org; s=201707; t=1773213043; c=relaxed/relaxed; bh=EDWCo5cE2aEVpTui5bFdNy+9+SZiFj1BIITbfBYcjoU=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=oz18IxTkh+31b0HtlDlfqZjAB8sVuE+T0xJNsqnqn59DMbh18fBbQhQr3T0Z+8Ih4FLU+0ZC8+A3Ej9Qq94O+gqJjG0X4ko5wF80M5TNCzYYEI3D1makW6sOSXyKGFiZvm2SxTqh3F3Wi1di8F37zq3K1m/dOsJLesk8h1si+aHG9rgQQVA+uifMbUQ2ZpuxWCUwq8DIC0kNMGfuHBFeZpfInUO7+cWWGJgnRFahGVV1gBOEh3AS93CPfpkWyH1JUzplkTs9pY1iXJd43CeUWTLmFq/gt2VSzPsc6/PRCfXfvl+0Nd3KYwFODruSx54QytotLVlBJdZLQvtv9pi+Ng== ARC-Authentication-Results: i=1; lists.ozlabs.org; dmarc=fail (p=none dis=none) header.from=lst.de; dkim=pass (2048-bit key; secure) header.d=infradead.org header.i=@infradead.org header.a=rsa-sha256 header.s=bombadil.20210309 header.b=BiRPm2vK; dkim-atps=neutral; spf=none (client-ip=2607:7c80:54:3::133; helo=bombadil.infradead.org; envelope-from=batv+bcc67bb7b2d0717eac20+8235+infradead.org+hch@bombadil.srs.infradead.org; receiver=lists.ozlabs.org) smtp.mailfrom=bombadil.srs.infradead.org Authentication-Results: lists.ozlabs.org; dmarc=fail (p=none dis=none) header.from=lst.de Authentication-Results: lists.ozlabs.org; dkim=pass (2048-bit key; secure) header.d=infradead.org header.i=@infradead.org header.a=rsa-sha256 header.s=bombadil.20210309 header.b=BiRPm2vK; dkim-atps=neutral Authentication-Results: lists.ozlabs.org; spf=none (no SPF record) smtp.mailfrom=bombadil.srs.infradead.org (client-ip=2607:7c80:54:3::133; helo=bombadil.infradead.org; envelope-from=batv+bcc67bb7b2d0717eac20+8235+infradead.org+hch@bombadil.srs.infradead.org; receiver=lists.ozlabs.org) Received: from bombadil.infradead.org (bombadil.infradead.org [IPv6:2607:7c80:54:3::133]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange x25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by lists.ozlabs.org (Postfix) with ESMTPS id 4fW22q1K1Cz3dhk for ; Wed, 11 Mar 2026 18:10:43 +1100 (AEDT) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=bombadil.20210309; h=Content-Transfer-Encoding: MIME-Version:References:In-Reply-To:Message-ID:Date:Subject:Cc:To:From:Sender :Reply-To:Content-Type:Content-ID:Content-Description; bh=EDWCo5cE2aEVpTui5bFdNy+9+SZiFj1BIITbfBYcjoU=; b=BiRPm2vKL9hGHKNuSaxBRpxE2D mXgqkxQdMGMIlSKtxUiL1DX30AL1P/IRN2NatDer1YoAKfR1SamsIwlMzXU0NUGufK6L0AAlKLvKO nVYjyKBGLuqv4evC5XLG4cWcwJwJ8Co6CST0FYTLyD4EB0vuMcIszwlr4HyrULBj6+ePMyFjY/x/l 2510EhcWdl4KfDQc53Q5AKdxMuzlGPF8SpZuE2mwLh1zneewS0BbdJwg2jp/pkRl+acSv1UHcG9+f PKnjNpW0ifFJ4eLkkRXHDCfR/vTJU5S011cRoPjh00Ll/I7IxPZSkukKKbbsuASGypN10O3qaSBJy qG4MSjRA==; 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> X-Mailing-List: linuxppc-dev@lists.ozlabs.org List-Id: List-Help: List-Owner: List-Post: List-Archive: , List-Subscribe: , , List-Unsubscribe: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-SRS-Rewrite: SMTP reverse-path rewritten from by bombadil.infradead.org. See http://www.infradead.org/rpr.html 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