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 kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 1AA73C4167B for ; Sun, 9 Oct 2022 02:58:16 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 1D05D6B0071; Sat, 8 Oct 2022 22:58:16 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 1590D6B0073; Sat, 8 Oct 2022 22:58:16 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id EC5AC6B0074; Sat, 8 Oct 2022 22:58:15 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0010.hostedemail.com [216.40.44.10]) by kanga.kvack.org (Postfix) with ESMTP id D34E56B0071 for ; Sat, 8 Oct 2022 22:58:15 -0400 (EDT) Received: from smtpin07.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay05.hostedemail.com (Postfix) with ESMTP id 181C640245 for ; Sun, 9 Oct 2022 02:58:15 +0000 (UTC) X-FDA: 79999902150.07.489A2C9 Received: from ams.source.kernel.org (ams.source.kernel.org [145.40.68.75]) by imf07.hostedemail.com (Postfix) with ESMTP id 4BCB540003 for ; Sun, 9 Oct 2022 02:58:14 +0000 (UTC) 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 80CD3B80B91; Sun, 9 Oct 2022 02:58:12 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id E6F02C433D6; Sun, 9 Oct 2022 02:58:04 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=zx2c4.com; s=20210105; t=1665284282; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=PTr9nvXhCIMgKxMMIHmKX/lEwOczzX2VojqQXIdEQXM=; b=BCnShyLwlwmwAtXh10NutIMIlSIhkA3vRZVyN65BKsRj3tXHWyoCFyOeejJMpbUNI3Ekb8 49UE+EpPryrYxycNRVtDAFnXtv9x21EZGJQQn9h5YttxW9h0hZ/Z324YkU/7vcz00VHicb bpNHNFK7pnOsAXwDr3Q1vkS9s34eCwg= Received: by mail.zx2c4.com (ZX2C4 Mail Server) with ESMTPSA id b38280f4 (TLSv1.3:TLS_AES_256_GCM_SHA384:256:NO); Sun, 9 Oct 2022 02:58:02 +0000 (UTC) Date: Sat, 8 Oct 2022 20:57:54 -0600 From: "Jason A. Donenfeld" To: David Laight Cc: Kees Cook , Christophe Leroy , "linux-kernel@vger.kernel.org" , "patches@lists.linux.dev" , Andreas Noever , Andrew Morton , Andy Shevchenko , Borislav Petkov , Catalin Marinas , Christoph =?utf-8?Q?B=C3=B6hmwalder?= , Christoph Hellwig , Daniel Borkmann , Dave Airlie , Dave Hansen , "David S . Miller" , Eric Dumazet , Florian Westphal , Greg Kroah-Hartman , "H . Peter Anvin" , Heiko Carstens , Helge Deller , Herbert Xu , Huacai Chen , Hugh Dickins , Jakub Kicinski , "James E . J . Bottomley" , Jan Kara , Jason Gunthorpe , Jens Axboe , Johannes Berg , Jonathan Corbet , Jozsef Kadlecsik , KP Singh , Marco Elver , Mauro Carvalho Chehab , Michael Ellerman , Pablo Neira Ayuso , Paolo Abeni , Peter Zijlstra , Richard Weinberger , Russell King , Theodore Ts'o , Thomas Bogendoerfer , Thomas Gleixner , Thomas Graf , Ulf Hansson , Vignesh Raghavendra , WANG Xuerui , Will Deacon , Yury Norov , "dri-devel@lists.freedesktop.org" , "kasan-dev@googlegroups.com" , "kernel-janitors@vger.kernel.org" , "linux-arm-kernel@lists.infradead.org" , "linux-block@vger.kernel.org" , "linux-crypto@vger.kernel.org" , "linux-doc@vger.kernel.org" , "linux-fsdevel@vger.kernel.org" , "linux-media@vger.kernel.org" , "linux-mips@vger.kernel.org" , "linux-mm@kvack.org" , "linux-mmc@vger.kernel.org" , "linux-mtd@lists.infradead.org" , "linux-nvme@lists.infradead.org" , "linux-parisc@vger.kernel.org" , "linux-rdma@vger.kernel.org" , "linux-s390@vger.kernel.org" , "linux-um@lists.infradead.org" , "linux-usb@vger.kernel.org" , "linux-wireless@vger.kernel.org" , "linuxppc-dev@lists.ozlabs.org" , "loongarch@lists.linux.dev" , "netdev@vger.kernel.org" , "sparclinux@vger.kernel.org" , "x86@kernel.org" , Toke =?utf-8?Q?H=C3=B8iland-J=C3=B8rgensen?= , Chuck Lever , Jan Kara Subject: Re: [PATCH v3 3/5] treewide: use get_random_u32() when possible Message-ID: References: <848ed24c-13ef-6c38-fd13-639b33809194@csgroup.eu> <6396875c-146a-acf5-dd9e-7f93ba1b4bc3@csgroup.eu> <501b0fc3-6c67-657f-781e-25ee0283bc2e@csgroup.eu> <202210071010.52C672FA9@keescook> <69080fb8cace486db4e28e2e90f1d550@AcuMS.aculab.com> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline In-Reply-To: <69080fb8cace486db4e28e2e90f1d550@AcuMS.aculab.com> ARC-Authentication-Results: i=1; imf07.hostedemail.com; dkim=pass header.d=zx2c4.com header.s=20210105 header.b=BCnShyLw; spf=pass (imf07.hostedemail.com: domain of "SRS0=aaEw=2K=zx2c4.com=Jason@kernel.org" designates 145.40.68.75 as permitted sender) smtp.mailfrom="SRS0=aaEw=2K=zx2c4.com=Jason@kernel.org"; dmarc=pass (policy=quarantine) header.from=zx2c4.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1665284294; a=rsa-sha256; cv=none; b=T3ZaRENs/6jjMTsEMF4bfB2rCVsdZYTR26vuxP6hWT+K1Q2u9CWAHvgSGxuICwARQFtxbb Jjxr/WJnt+m5/pZ3yNhV00ZVqYC3q3db7Gje7DDc9gQNPkMTgFDJtwSHU0x357SfH3WBzz xSEWze4z1HMJ9i6Dxyvmx1i9nWsTL0k= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1665284294; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=PTr9nvXhCIMgKxMMIHmKX/lEwOczzX2VojqQXIdEQXM=; b=PoEIGeLWyZJ469WJ1uTdaIDJXM6ZXrYSMub+y+kDmpumc2w+RKSbygo7ccaf3Fn+1U2rrH AUW8tzsvpIfYhk5N5TbDzAdaiSY85L1X6GjGTJzeoHrRp7mm/HEFEMT930SUB7YOksBrkm pSitJe4TohP4XlRy5yh2xTGE6kIgw8g= X-Rspamd-Queue-Id: 4BCB540003 X-Rspam-User: Authentication-Results: imf07.hostedemail.com; dkim=pass header.d=zx2c4.com header.s=20210105 header.b=BCnShyLw; spf=pass (imf07.hostedemail.com: domain of "SRS0=aaEw=2K=zx2c4.com=Jason@kernel.org" designates 145.40.68.75 as permitted sender) smtp.mailfrom="SRS0=aaEw=2K=zx2c4.com=Jason@kernel.org"; dmarc=pass (policy=quarantine) header.from=zx2c4.com X-Rspamd-Server: rspam10 X-Stat-Signature: xcjebueuatq7gqtg898m3whxazsdsqk5 X-HE-Tag: 1665284294-51516 X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: On Sat, Oct 08, 2022 at 09:53:33PM +0000, David Laight wrote: > From: Jason A. Donenfeld > > Sent: 07 October 2022 18:56 > ... > > > Given these kinds of less mechanical changes, it may make sense to split > > > these from the "trivial" conversions in a treewide patch. The chance of > > > needing a revert from the simple 1:1 conversions is much lower than the > > > need to revert by-hand changes. > > > > > > The Cocci script I suggested in my v1 review gets 80% of the first > > > patch, for example. > > > > I'll split things up into a mechanical step and a non-mechanical step. > > Good idea. > > I'd also do something about the 'get_random_int() & 3' cases. > (ie remainder by 2^n-1) > These can be converted to 'get_random_u8() & 3' (etc). > So they only need one random byte (not 4) and no multiply. > > Possibly something based on (the quickly typed, and not C): > #define get_random_below(val) [ > if (builtin_constant(val)) > BUILD_BUG_ON(!val || val > 0x100000000ull) > if (!(val & (val - 1)) { > if (val <= 0x100) > return get_random_u8() & (val - 1); > if (val <= 0x10000) > return get_random_u16() & (val - 1); > return get_random_u32() & (val - 1); > } > } > BUILD_BUG_ON(sizeof (val) > 4); > return ((u64)get_random_u32() * val) >> 32; This is already how the prandom_u32_max() implementation works, as suggested in the cover letter. The multiplication by constants in it reduces to bit shifts and you already get all the manual masking possible. > get_random_below() is a much better name than prandom_u32_max(). Yes, but that name is reserved for when I succeed at making a function that bounds with a uniform distribution. prandom_u32_max()'s distribution is non-uniform since it doesn't do rejection sampling. Work in progress is on https://git.zx2c4.com/linux-rng/commit/?h=jd/get_random_u32_below . But out of common respect for this already huge thread with a massive CC list, if you want to bikeshed my WIP stuff, please start a new thread for that and not bog this one down. IOW, no need to reply here directly. That'd annoy me. Jason