From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-wm1-f48.google.com (mail-wm1-f48.google.com [209.85.128.48]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id ADEF3387358 for ; Thu, 12 Mar 2026 08:38:58 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.48 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1773304745; cv=none; b=gihiUF4yu9qZL22y8Ee5i5XoTV2T6RFfBickkdHcdiwhKHHHfxbuM0lM/t9kaBixm2UC/3FB/BjsBnVJ8+JTvcScLRsULW11jDG3UKdKKPRr4yTtZBgHzSbWduy+hF//R/xGdCeMJgbevv//3u+su7/4kiUklmoV7WX/zcbk+54= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1773304745; c=relaxed/simple; bh=2rASbAMEw5ZRlubXhkcWPIGmcIGEBmzjA5ymOU3qhqE=; h=Date:From:To:Cc:Subject:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=jII+XQg0gGQU6XLI2QXgiQZwsIa7FZjczccBduyV4iVvAJ+fAd5jspzE0gtsqltoGIIP9mSSK2teWhXwMM8lQAjbR4KJrSVDBjIZJkG2SyPXudC2FLjRxbphSTCChzFybTUIh07P+4bwakwz95s2sQaKZH2oJbw0AUohfZi7e/E= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=Wj+rWY6m; arc=none smtp.client-ip=209.85.128.48 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="Wj+rWY6m" Received: by mail-wm1-f48.google.com with SMTP id 5b1f17b1804b1-4852f8ac7e9so8188385e9.1 for ; Thu, 12 Mar 2026 01:38:58 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1773304736; x=1773909536; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:subject:cc:to:from:date:from:to:cc:subject:date :message-id:reply-to; bh=tXrAFYR5bIhSumOFjuc1G0swOeqj6K0Pq1d0oWdhjWU=; b=Wj+rWY6m6+UCfo2x6AIjTqdw9g4ImHxiulaT1RQNuBOH7Wi+or6HuP175SMxDPofb/ 0376Rddavr2FkBzhUM+dzwRfAtr2UHVpIHWK9TPE6PQsJVgW4HCASac/7ZOSZ9Ou5ano 16E3zcEXnQ0/A2y27KVdOIADss3J/fB3GsjHarBVmVx9BCxOh1aWg5dMH5Enxd/Nb46W 9AglMk8NL68i2W2cAncjuexUGaJ3jPDvt4O4XdErAcXqflOZ16c6cxTL3c3uCUoxDSXb 971dm2O+mnDzK7t4q2fa1HFxJZ+rEQUnpR9uFoZKu5H1hiA6V6UPe1KtLFSnN3N5tGah XJZg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1773304736; x=1773909536; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:subject:cc:to:from:date:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to; bh=tXrAFYR5bIhSumOFjuc1G0swOeqj6K0Pq1d0oWdhjWU=; b=ZwXD4VjpNTOkn4uiUNXkixTU8qUFso4Penh5Gl5iU2pxptxCUYllF06zOvwVighIXh EnKJICJYghNkPp8QeagTk7DNQt/6Am+GQa6G25LD/O1BYKNPlelSH83kS5BMcF6PA24A hX4vkbZU6VbIuuL/0nb2y9HZJJI1JlQS5QG5ylMme5dZeRXuGLK1XzcyqYlg8S+U5GhB V2CBMJKTzMSlLE0t3z19OiqpkFdfAYkDPMfj182YenNz6K4xLNFktHRim/nUD9bvHuWC BWqL/QdYxcp4oqysog8uPUs7SXj1GFkBOnJzklpb6FBEyFYm4RLrx6EhhRZzTTUM+oxZ Ac6g== X-Forwarded-Encrypted: i=1; AJvYcCXNUTW9F1xm6fgGPoL+OpYJlua1675mz0GbJzxXf3hdIFJ31FhT5Cp8igqcwWq+mtlTI47JnU9e0tjIBg==@vger.kernel.org X-Gm-Message-State: AOJu0Yz2N2m5ezNFGFYpfrstVzPZMIy831mYWLcMTIoWIEb19vQzkx8C u4coJNhzVuH6YjddsvzrcOKKXiTLTFCfyPPzCnm1VuEmFxw6QXcHYGO1 X-Gm-Gg: ATEYQzwz7P3V4xOi4+FvL4hKXpVQPuAeQOA2+16ICJ2k+h78PDJUv6eyZIOgYtEZzb0 Yx8U2AcROvkp+ZfXwcWrm9LfQ82IjCcHYpQyoQupBfJPQG2ob/z6IwGy0I9YHEKufkXuFze6n49 pwZ5XmVRXYq8n9WmgOXKR8a/7Uy19MTNog0x8zNw6QO8e5yTBseiadVJ4ZTbnNGyOVL97kMJWWn PIyUKbwP2CqBA1dV+01foyKDK1mDg0Q1EUQ8szdRlu6y63KMpOs/+gW67m9wZg1N5RKcF2WQPpu q95cw3P6wOqIsnq1s2JPOL9J+rCFDgVju6CNR8/TXz2IkRS2jp//tzi4uDSmuZYBvL+5CkI5zMH vpscGLI2FR/4JG59e3JDiNlT13OsL6buazn9HKOa8JLl3Q0fB1m4ob1worvhcOUtynOt0tXtxDK U3TWB2QMrO1oZBG249D3pGGYuNk1wJeqBHrWMFOeY7rZNpIE+PT/9fTF9/YLzMixQ5 X-Received: by 2002:a05:600c:3551:b0:477:7b16:5fb1 with SMTP id 5b1f17b1804b1-4854b0cbe81mr96719315e9.7.1773304735965; Thu, 12 Mar 2026 01:38:55 -0700 (PDT) Received: from pumpkin (82-69-66-36.dsl.in-addr.zen.co.uk. [82.69.66.36]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-4854b5f6bb4sm111774365e9.4.2026.03.12.01.38.54 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 12 Mar 2026 01:38:55 -0700 (PDT) Date: Thu, 12 Mar 2026 08:38:53 +0000 From: David Laight To: Eric Biggers Cc: Christoph Hellwig , Andrew Morton , 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: Re: [PATCH 26/27] random: factor out a __limit_random_u32_below helper Message-ID: <20260312083853.01dc18d3@pumpkin> In-Reply-To: <20260311222935.GA3161@quark> References: <20260311070416.972667-1-hch@lst.de> <20260311070416.972667-27-hch@lst.de> <20260311222935.GA3161@quark> X-Mailer: Claws Mail 4.1.1 (GTK 3.24.38; arm-unknown-linux-gnueabihf) Precedence: bulk X-Mailing-List: linux-alpha@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit On Wed, 11 Mar 2026 15:29:35 -0700 Eric Biggers wrote: > On Wed, Mar 11, 2026 at 08:03:58AM +0100, Christoph Hellwig wrote: > > 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 > > I think I'd prefer that the test just uses the mod operation instead, > like many of the existing tests do: > > prandom_u32_state(&rng) % ceil Or possibly what the old code used: (prandom_u32_state(&rnd) * (u64)ceil) >> 32 Which distributes the values evenly across the range although some values happen 1 more time than others. I suspect that is good enough for a lot of the users of the cryptographic random number generator as well. David > > Yes, when ceil isn't a power of 2 the result isn't uniformly > distributed. But that's perfectly fine for these tests, especially with > the values of ceil being used being far smaller than U32_MAX. > > There's been an effort to keep the cryptographic random number generator > (drivers/char/random.c and include/linux/random.h) separate from the > non-cryptographic random number generator (lib/random32.c and > include/linux/prandom.h). This patch feels like it's going in a > slightly wrong direction, where random.c gains a function that's used > with both cryptographic and non-cryptographic random numbers. > > And if someone actually needs a fully unform distribution, then they'd > probably want cryptographic random numbers as well. > > So I'm not sure the proposed combination of "fully uniform > non-cryptographic random numbers" makes much sense. > > Plus the '% ceil' implementation is much easier to understand. > > - Eric > 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 00F81FED2EB for ; Thu, 12 Mar 2026 08:39:14 +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:Subject:Cc:To:From:Date:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=Eiv/b+YO4n7W2fauJsb/jz9kiRhCMZuvr5KhL0kfq/o=; b=T20npVjm3bQ+OQ 4a9X6J9QiSVkhbZbo61tQ7kqoA3j+mDWLdckm+Lb92PrGTvEzuTl/QinLWM/lQbSuLMvdY2ZfTFj0 lSELlD7yKAcSYpIcpfn4p34osW3q/G2Nm4RET4UkBqCAMbxLiYkuZ3V9B4amfYZityONXCZlO5oBk WecM6DSBOKa7d4AxruF4RUPcw/JuZxCAnX7DrGTq2TX2Bhby8xG9sRZ5EgUpr1DnBX5ok3+Zw9yFb S9EJmzh89HTEH9baILqp8wtR9OBnySkGDsBof7c7xhmPcE9YxUJkxBRIaDnjBQYOtmoFbEoQOTU5W VjBS1UKdkchkpsANHsIA==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.98.2 #2 (Red Hat Linux)) id 1w0bZM-0000000DeSP-3ate; Thu, 12 Mar 2026 08:39:00 +0000 Received: from mail-wm1-x335.google.com ([2a00:1450:4864:20::335]) by bombadil.infradead.org with esmtps (Exim 4.98.2 #2 (Red Hat Linux)) id 1w0bZL-0000000DeRN-12Y4 for linux-riscv@lists.infradead.org; Thu, 12 Mar 2026 08:39:00 +0000 Received: by mail-wm1-x335.google.com with SMTP id 5b1f17b1804b1-48374014a77so7748155e9.3 for ; Thu, 12 Mar 2026 01:38:57 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1773304736; x=1773909536; darn=lists.infradead.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:subject:cc:to:from:date:from:to:cc:subject:date :message-id:reply-to; bh=tXrAFYR5bIhSumOFjuc1G0swOeqj6K0Pq1d0oWdhjWU=; b=kWXVmDGOrEI/c0dfGsKm/XXJFuYc1x/lnyO+3N/5VLpeCHtXRjuwJRmyfiL3pOzOHF xmBeLEkngRGDzecE4mkaac6NrrilKw51bHgItokf3gHXUFAf/56RyB5aTlJPsHMzDCUJ 8eze2Dt8k46mcEnC9YEdiAirVgSZHKMEnVpaP6mCZD4KVILxeX3MgsWjTqdr00w26HMY 0Q7qH9M0BX7562RTR+7lq0KjhPsQQ8nhFmo8nl0ZWRL+N1le2Tc3jJr0JdX7J6WirTso SpHkC2ZCPA4miolQWr8BTE1hUiHd78K9eHAl7OykfckSkOXGEm8OhN9fdYNgxRwwltFS MMew== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1773304736; x=1773909536; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:subject:cc:to:from:date:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to; bh=tXrAFYR5bIhSumOFjuc1G0swOeqj6K0Pq1d0oWdhjWU=; b=NCftxXtKZz9mN8vRWFJZpzQes9HAQkLvhPfwwIFdLH4BA60sYuhyf6epec/VopD7ZS aL+0Z4+loA+F52vao4A3Kxy024Ku7WSVbs9BNn0VUB1kg5NDQHBJYSxrPgnvJYxL3JxF k18jvxVgj+kPbT/AGAObBEDZr7jV6ASkTIWT1slQGUoHhpMyJV6PrVdstaaW6dhaJKtB DNloDQ6J9bEkSU0mM5mhyaPCDqQuIxxp2rlXbnQKagmSYOWfU5hWu9tGP05RrYDxDHnT 3KrRfJfd5NOaPREvGSzaYiFyXNJqdw530Gch0/Mrv77kEcGUP8v6Fr0AfIuxPBNLAmyL janw== X-Forwarded-Encrypted: i=1; AJvYcCWVpzHukoY6urQgq5WQaBx8TRQ9pGng7t4lLUhij325U7QtLNu3P2VnVtgfanDsTwMFB7IKqBE5L2F5oQ==@lists.infradead.org X-Gm-Message-State: AOJu0Yx3SGan4PECCej0paS2EquAEIuhJT7v42S66QRMfrXNCE+icZ9+ lAYV8oyZnK5bHfirKiouS8zrNLz35xq/X3VGy6zDWLmj9ebuL4jOovvU X-Gm-Gg: ATEYQzx6cGGy8deF2F7VXMgMkG28FJbflwVFehRuMCa0r0O7mpXZ8boKyl1PutQvCln Ag08fY1t/kGQ2kTGp6KvZiajKN5Use6JpLbKrL6GltGsvdkLztwTkoao11sDakB8VtUSlw0hlnj pdT8S3a2XumdTJwVOYxKcnGUUwC5V8TkNfdV9WgYReWcMniPYxHXAE83vELhV0QH6iqctN35OM9 9PDeByszTpQ7iz8c29S1/rXF8GWyRBehq65ffDSteoiQUNzYaI/1d4hwXL8dGj6KnaNPM5UsWBK IixeCSjahPHoF6rxkdJmTQmc08DJ3rg3HSKi2BCopKo1nmd74GKTSU8YgOlt5kqvaVapQClKWa5 j41BWseViM0R9LjUNODmfnuQGTLnTTvmL+6IxmWj/i9aKo89PF58xnXbUr+W8+BXMikribrCtUF NMHSsjzONfmkeMKfDdC300heT3sUKs08mpspDJoPZInmrNED1qlCCmsplAu69ohP9J X-Received: by 2002:a05:600c:3551:b0:477:7b16:5fb1 with SMTP id 5b1f17b1804b1-4854b0cbe81mr96719315e9.7.1773304735965; Thu, 12 Mar 2026 01:38:55 -0700 (PDT) Received: from pumpkin (82-69-66-36.dsl.in-addr.zen.co.uk. [82.69.66.36]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-4854b5f6bb4sm111774365e9.4.2026.03.12.01.38.54 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 12 Mar 2026 01:38:55 -0700 (PDT) Date: Thu, 12 Mar 2026 08:38:53 +0000 From: David Laight To: Eric Biggers Cc: Christoph Hellwig , Andrew Morton , 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: Re: [PATCH 26/27] random: factor out a __limit_random_u32_below helper Message-ID: <20260312083853.01dc18d3@pumpkin> In-Reply-To: <20260311222935.GA3161@quark> References: <20260311070416.972667-1-hch@lst.de> <20260311070416.972667-27-hch@lst.de> <20260311222935.GA3161@quark> X-Mailer: Claws Mail 4.1.1 (GTK 3.24.38; arm-unknown-linux-gnueabihf) MIME-Version: 1.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20260312_013859_335679_B37D8817 X-CRM114-Status: GOOD ( 25.35 ) 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 On Wed, 11 Mar 2026 15:29:35 -0700 Eric Biggers wrote: > On Wed, Mar 11, 2026 at 08:03:58AM +0100, Christoph Hellwig wrote: > > 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 > > I think I'd prefer that the test just uses the mod operation instead, > like many of the existing tests do: > > prandom_u32_state(&rng) % ceil Or possibly what the old code used: (prandom_u32_state(&rnd) * (u64)ceil) >> 32 Which distributes the values evenly across the range although some values happen 1 more time than others. I suspect that is good enough for a lot of the users of the cryptographic random number generator as well. David > > Yes, when ceil isn't a power of 2 the result isn't uniformly > distributed. But that's perfectly fine for these tests, especially with > the values of ceil being used being far smaller than U32_MAX. > > There's been an effort to keep the cryptographic random number generator > (drivers/char/random.c and include/linux/random.h) separate from the > non-cryptographic random number generator (lib/random32.c and > include/linux/prandom.h). This patch feels like it's going in a > slightly wrong direction, where random.c gains a function that's used > with both cryptographic and non-cryptographic random numbers. > > And if someone actually needs a fully unform distribution, then they'd > probably want cryptographic random numbers as well. > > So I'm not sure the proposed combination of "fully uniform > non-cryptographic random numbers" makes much sense. > > Plus the '% ceil' implementation is much easier to understand. > > - Eric > _______________________________________________ linux-riscv mailing list linux-riscv@lists.infradead.org http://lists.infradead.org/mailman/listinfo/linux-riscv