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: 7061 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:4242:0:0:0:0:0 with SMTP id s2csp990379wrr; Fri, 21 Oct 2022 18:47:02 -0700 (PDT) X-Google-Smtp-Source: AMsMyM41eJb9jbIgpLcJWxSMoMUB6oMCqvhV8wOOE6u7SWJkBG9LZ5PRsYJOfwleinP3G4KqLVeW X-Received: by 2002:a05:6402:50cf:b0:45c:dfce:66ae with SMTP id h15-20020a05640250cf00b0045cdfce66aemr19325660edb.370.1666403222234; Fri, 21 Oct 2022 18:47:02 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1666403222; cv=none; d=google.com; s=arc-20160816; b=Yd9leGe35hmVEoYz6WNS3YPRbsMlVQXAOjEDLEh4Nw8/Z+hoUWJYN2+JjvmhYgHKOJ 488Z86ascVY5Z+JPYxOvQliUYDgkbdzDhzyCv+adqHdlRJsF3qiyoLwjJSjcgUPuyt7A gu/OH4ZLI1hf52Rurv2jADXCmxq6nm9bzlY50pKLIRYqX7ItGGMfl/NCs4M3RuHyPJ+v Zo5gfbqr89yECFKY2Oo6Lk/RgAnUlFbf4156lJMUV3onbioyrP3hjkVdpVIVCclDSjHD 32UPT4Wr3Zrc6XZGW9kCMqArSweBkXsn+lYrH2D9/7e7WfP0odrLL8aDNRBGjOwaNgoW yCwg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=pgAGvtfWFBepSErNnmOiCogblep8pK7WSNMDWQtrNSg=; b=SBJAdH/FkL275Avlk0P8HPh5Zq9HI0OGGYPBa2kZeh4wiDiOfE+meStPRviqbSxK11 SiLYMWYqBYLM8dvFlTlab3Gnu4D0i/oK4RWfrF2O34eDtGuwMPYkmQyRpS9I870gjaPD 4Clfvc1H6aGZw+izIXd7pxDuAzOah4UdDjlSkvxwJ75fjyWTjSGNqLTjizErJSWIYivT tR8/7u9hSYvU8Et+7BdF1o2DCYY9tLhpqq4UdnTignEp0WgjjZG9iPbtq9DuwAzFd3XF GNDbilb/yDuoX2n996e/pR8rJ8jKBg6NcEzU5DZFb01peYnerbMXh3e5hJGV226qTw3+ HcVg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@zx2c4.com header.s=20210105 header.b=GMIwnwq1; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=zx2c4.com Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id g12-20020aa7c84c000000b00459c2ac54e2si19341878edt.255.2022.10.21.18.46.37; Fri, 21 Oct 2022 18:47:02 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@zx2c4.com header.s=20210105 header.b=GMIwnwq1; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=zx2c4.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229716AbiJVBpA (ORCPT + 99 others); Fri, 21 Oct 2022 21:45:00 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:56776 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229945AbiJVBoh (ORCPT ); Fri, 21 Oct 2022 21:44:37 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [IPv6:2604:1380:4641:c500::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 86A933204F; Fri, 21 Oct 2022 18:44:35 -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 dfw.source.kernel.org (Postfix) with ESMTPS id 227AE61FB1; 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-Spam-Status: No, score=-6.8 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, RCVD_IN_DNSWL_HI,SPF_HELO_NONE,SPF_PASS autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1747350425157574321?= X-GMAIL-MSGID: =?utf-8?q?1747350425157574321?= 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.