From patchwork Sat Oct 22 01:44:01 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Jason A. Donenfeld" X-Patchwork-Id: 13015716 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 9C28EC433FE for ; Sat, 22 Oct 2022 01:45:53 +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=WqAc53coaXIGGXtGlCzzugKFgr4cXOk1GLR7JhAlvo0=; b=k1Ovp5VtMyiu7X ZH78mrNQGcrPwJe1e/c8zn24zSJMrL5rutPLDrpxVByBLg4W1qJtJbLTbxRMATskvGSjQ9t5K6HKm dw2S8UlgAdlf1a2xwBR1r9DNQ3xqnVixJ1Q5StBY2UKtCeUMJkjQKixAv/ifurO+W2OA2D23De2gG svKkfoN74N90F9ZOP2lrD1MasT3gYpp5KUbS1aqVVloskF1J60+clhV6KWrM4XvCoD7n7jnprM3Ph bcw5JdOcUFFNWclCl6wjrKGLf4SwmO3Bd5VSJKUxrFT9Qxyo1WpeucoVJSuryiH2cPrLCZZezfFi2 iO2hutrtAdaJJz7vXRlg==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.94.2 #2 (Red Hat Linux)) id 1om3ZH-00AiIN-79; Sat, 22 Oct 2022 01:44:55 +0000 Received: from ams.source.kernel.org ([2604:1380:4601:e00::1]) by bombadil.infradead.org with esmtps (Exim 4.94.2 #2 (Red Hat Linux)) id 1om3Yz-00AiA0-CP for linux-arm-kernel@lists.infradead.org; Sat, 22 Oct 2022 01:44:39 +0000 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 D47A4B82DB8; Sat, 22 Oct 2022 01:44:35 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 8FC1BC43143; Sat, 22 Oct 2022 01:44:31 +0000 (UTC) Authentication-Results: smtp.kernel.org; dkim=pass (1024-bit key) header.d=zx2c4.com header.i=@zx2c4.com header.b="GMIwnwq1" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=zx2c4.com; s=20210105; t=1666403063; 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=pgAGvtfWFBepSErNnmOiCogblep8pK7WSNMDWQtrNSg=; b=GMIwnwq1awcpUW/3bcqRLII9Ls9FFpGfdnmMIcvGbiEtdgTfYCtyHBxD9yKHQ7iLoU7MWe oedm6GH5CEx+hIfF88+NdonBLSB3Qlk4BllZXEvDgVRGj4TJT9CbyQxCoDz8OaY8tqxGTj zGaJgSvm0ym+4US4UBRtlbzQYNmOeTE= Received: by mail.zx2c4.com (ZX2C4 Mail Server) with ESMTPSA id e102b3b6 (TLSv1.3:TLS_AES_256_GCM_SHA384:256:NO); Sat, 22 Oct 2022 01:44:23 +0000 (UTC) From: "Jason A. Donenfeld" To: linux-kernel@vger.kernel.org Cc: "Jason A. Donenfeld" , Kees Cook , Greg Kroah-Hartman , Jakub Kicinski , Russell King , Catalin Marinas , Thomas Bogendoerfer , Heiko Carstens , Herbert Xu , =?utf-8?q?Christoph_B=C3=B6hmwalder?= , Jani Nikula , Jason Gunthorpe , Sakari Ailus , "Martin K . Petersen" , Theodore Ts'o , Andreas Dilger , Jaegeuk Kim , Richard Weinberger , "Darrick J . Wong" , SeongJae Park , Thomas Gleixner , Andrew Morton , Michael Ellerman , Helge Deller , netdev@vger.kernel.org, linux-crypto@vger.kernel.org, linux-block@vger.kernel.org, linux-fsdevel@vger.kernel.org, linux-media@vger.kernel.org, linux-arm-kernel@lists.infradead.org, loongarch@lists.linux.dev, linux-mips@vger.kernel.org, linuxppc-dev@lists.ozlabs.org, linux-mmc@vger.kernel.org, linux-parisc@vger.kernel.org Subject: [PATCH v1 3/5] random: add helpers for random numbers with given floor or range Date: Fri, 21 Oct 2022 21:44:01 -0400 Message-Id: <20221022014403.3881893-4-Jason@zx2c4.com> In-Reply-To: <20221022014403.3881893-1-Jason@zx2c4.com> References: <20221022014403.3881893-1-Jason@zx2c4.com> MIME-Version: 1.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20221021_184437_731803_4433AA48 X-CRM114-Status: GOOD ( 14.48 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org Now that we have get_random_u32_below(), it's trivial to make inline helpers to compute get_random_u32_above() and get_random_u32_between(), which will help clean up open coded loops and manual computations throughout the tree. Signed-off-by: Jason A. Donenfeld --- include/linux/random.h | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/include/linux/random.h b/include/linux/random.h index 3a82c0a8bc46..92188a74e50e 100644 --- a/include/linux/random.h +++ b/include/linux/random.h @@ -91,6 +91,30 @@ static inline u32 get_random_u32_below(u32 ceil) } } +/* + * Returns a random integer in the interval (floor, U32_MAX], with uniform + * distribution, suitable for all uses. Fastest when floor is a constant, but + * still fast for variable floor as well. + */ +static inline u32 get_random_u32_above(u32 floor) +{ + BUILD_BUG_ON_MSG(__builtin_constant_p(floor) && floor == U32_MAX, + "get_random_u32_above() must take floor < U32_MAX"); + return floor + 1 + get_random_u32_below(U32_MAX - floor); +} + +/* + * Returns a random integer in the interval [floor, ceil), with uniform + * distribution, suitable for all uses. Fastest when floor and ceil are + * constant, but still fast for variable floor and ceil as well. + */ +static inline u32 get_random_u32_between(u32 floor, u32 ceil) +{ + BUILD_BUG_ON_MSG(__builtin_constant_p(floor) && __builtin_constant_p(ceil) && + floor >= ceil, "get_random_u32_above() must take floor < ceil"); + return floor + get_random_u32_below(ceil - floor); +} + /* * On 64-bit architectures, protect against non-terminated C string overflows * by zeroing out the first byte of the canary; this leaves 56 bits of entropy.