Received: by 2002:a05:6a10:6744:0:0:0:0 with SMTP id w4csp731462pxu; Sun, 25 Oct 2020 15:21:15 -0700 (PDT) X-Google-Smtp-Source: ABdhPJx+C03Ns0s6vDhSKW3yluS8A2CqdHGeltQvQfupClNSIbEv5f1lSNut5hZ/RNDC3bh65Tcn X-Received: by 2002:a17:906:934d:: with SMTP id p13mr13667658ejw.532.1603664475525; Sun, 25 Oct 2020 15:21:15 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1603664475; cv=none; d=google.com; s=arc-20160816; b=n9iGCcNXbbwUMaTrgGgCVZeuykR+zURbF+HTQQA6oGc2Oy6MHtnXtzXvdSILu6b+mK VWcsNSjrUnZ7UU5hSZEJUxeKmBSXA/OTYM4+3PDmDEGwPDWx9kyEkeW/3FBVPADcy0bU pdAuuZ1q7kE4QLOmHX8QSYfQWhbgVm1CqFLCqr+Sv5MMZNN+e0TGqiJ8ElF0YF789mQ+ KkPfCXUxNoVbxmXb9g5Uo37TIVq6vKUSq3XS7rVPNpn+QSxhMPfPVN6GycfAgLu+fS1E 26jQ9/4gTTbCLth/O3b0Xo/MZc2gTGjjSOkmF1hxCOCdaUwmxvIdcwH5Za6gxoS6AeIY 0Bew== 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=H5U5b0c60OiFhVBev3K4PZ1W16Gukohw1HLrLU313w4=; b=KGDnEMNWRbChNVJ4qTwgLgYV0dRg7NpAVApuVfgw6h6+kuvTPUD9XhJ0CWfBqEwh9z 3jtBxT3SNYrW9wMIMPWl+FFZXCiBJfcRXKumIy6wjEC3g9eCAHOktkdTrNJja6BFar0d DviVAFyyG0WpR8T3/RCSEd24F+hePNkn2ueassi8w4uzn7CEHnwTfLiynyZbn1IxdcMc vPeYPcvBoOSsT1M872tcNHaRylhYuCPowFJOHlFgq8s4pwv6sBqJjdmDGAJ+jxQFnqp2 wlIzt2BsXOMvsyXk/bgdWzBpBbPU24CZy7wrQcWDvr9Zbus8hAofaNT5dv8/xhHxMyzA 6rCg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@rasmusvillemoes.dk header.s=google header.b=RW8QN6Bs; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id o22si7468368edr.480.2020.10.25.15.20.53; Sun, 25 Oct 2020 15:21:15 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@rasmusvillemoes.dk header.s=google header.b=RW8QN6Bs; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1418973AbgJYVsu (ORCPT + 99 others); Sun, 25 Oct 2020 17:48:50 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:58730 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1418967AbgJYVst (ORCPT ); Sun, 25 Oct 2020 17:48:49 -0400 Received: from mail-ej1-x643.google.com (mail-ej1-x643.google.com [IPv6:2a00:1450:4864:20::643]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 40633C0613D1 for ; Sun, 25 Oct 2020 14:48:49 -0700 (PDT) Received: by mail-ej1-x643.google.com with SMTP id p5so10672217ejj.2 for ; Sun, 25 Oct 2020 14:48:49 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rasmusvillemoes.dk; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=H5U5b0c60OiFhVBev3K4PZ1W16Gukohw1HLrLU313w4=; b=RW8QN6BsYUCXyComtBNVPU+Wuq7tGZce9IKJCqRFMeJFb4UOTB0H+p7OtuKYDOxloX SGy4VJSUpLgwmepKy5TB3bPmvKpAf0UdY80LU3u6PbEWPvsBAHGld5F1Tl6wDU1/WBZA ieEFEZbBWeNSZo246dtVlZPew5w9JoD84iN9Q= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=H5U5b0c60OiFhVBev3K4PZ1W16Gukohw1HLrLU313w4=; b=qC+v/xKqKkrAbgeCsIdJlgdOnByRKlfQyt50IZc9DB1S0nHnRHdGfBke3R9ZnXSupI btbMvUHr6j5f84tJFT9OVnFEYkLS6Z7BaYeCihpVoIwWkN9rY27cgi93IyOrof7y6pYu 72mffKzKv2tFykr49hUEKqCvaFUCoUc/k4pZkZ+6q7lXrmVKRP/fsWngo3jM1shnZNri snXD1MhaV3FQndaazVo3tkvbcIIcF0s7+dH642P/u3BnHo/uSnCxOZM0X7UFtPgMWWny C6Zad8L/A2Nh4j5d5/ZwlFe+X4of6dYiNo4p58ThadOLlOI/Onf/KIFR+QQ2by3W7zZz 5XXQ== X-Gm-Message-State: AOAM5308kE6dbu4aiCOOnsXQyK8TvUa73XgUN8lmA4PHXYVLrKUXuMsE mJJ5CgCQI2HjMdEb6uSWVAp6IA== X-Received: by 2002:a17:906:2987:: with SMTP id x7mr12651220eje.301.1603662527946; Sun, 25 Oct 2020 14:48:47 -0700 (PDT) Received: from prevas-ravi.prevas.se (5.186.115.188.cgn.fibianet.dk. [5.186.115.188]) by smtp.gmail.com with ESMTPSA id k18sm4115867eds.93.2020.10.25.14.48.46 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 25 Oct 2020 14:48:47 -0700 (PDT) From: Rasmus Villemoes To: Shuah Khan , Kees Cook Cc: Petr Mladek , Willy Tarreau , linux-kselftest@vger.kernel.org, linux-kernel@vger.kernel.org, Arpitha Raghunandan <98.arpi@gmail.com>, Andy Shevchenko , Brendan Higgins , Rasmus Villemoes Subject: [PATCH 1/4] prandom.h: add *_state variant of prandom_u32_max Date: Sun, 25 Oct 2020 22:48:39 +0100 Message-Id: <20201025214842.5924-2-linux@rasmusvillemoes.dk> X-Mailer: git-send-email 2.23.0 In-Reply-To: <20201025214842.5924-1-linux@rasmusvillemoes.dk> References: <20201025214842.5924-1-linux@rasmusvillemoes.dk> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org It is useful for test modules that make use of random numbers to allow the exact same series of test cases to be repeated (e.g., after fixing a bug in the code being tested). For that, the test module needs to obtain its random numbers from a private state that can be seeded by a known seed, e.g. given as a module parameter (and using a random seed when that parameter is not given). There's a few test modules I'm going to modify to follow that scheme. As preparation, add a _state variant of the existing prandom_u32_max(), and for convenience, also add a variant that produces a value in a given range. Signed-off-by: Rasmus Villemoes --- include/linux/prandom.h | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/include/linux/prandom.h b/include/linux/prandom.h index aa16e6468f91e79e1f31..58ffcd56c705be34fb98 100644 --- a/include/linux/prandom.h +++ b/include/linux/prandom.h @@ -46,6 +46,35 @@ static inline u32 prandom_u32_max(u32 ep_ro) return (u32)(((u64) prandom_u32() * ep_ro) >> 32); } +/** + * prandom_u32_max_state - get pseudo-random number in internal [0, hi) + * + * Like prandom_u32_max, but use the given state structure. + * @state: pointer to state structure + * @hi: (exclusive) upper bound + * + * Exception: If @hi == 0, this returns 0. + */ +static inline u32 prandom_u32_max_state(struct rnd_state *state, u32 hi) +{ + return ((u64)prandom_u32_state(state) * hi) >> 32; +} + +/** + * prandom_u32_range_state - get pseudo-random number in internal [lo, hi) + * + * @state: pointer to state structure + * @lo: (inclusive) lower bound + * @hi: (exclusive) upper bound + * + * Exception: If @lo == @hi, this returns @lo. Results are unspecified + * for @lo > @hi. + */ +static inline u32 prandom_u32_range_state(struct rnd_state *state, u32 lo, u32 hi) +{ + return lo + prandom_u32_max_state(state, hi - lo); +} + /* * Handle minimum values for seeds */ -- 2.23.0