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 7070BC4167B for ; Sat, 8 Oct 2022 01:29:28 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 6733D6B0072; Fri, 7 Oct 2022 21:29:27 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 623416B0073; Fri, 7 Oct 2022 21:29:27 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 4C38B6B0074; Fri, 7 Oct 2022 21:29:27 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0014.hostedemail.com [216.40.44.14]) by kanga.kvack.org (Postfix) with ESMTP id 36FDF6B0072 for ; Fri, 7 Oct 2022 21:29:27 -0400 (EDT) Received: from smtpin12.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id 05D4DABC2A for ; Sat, 8 Oct 2022 01:29:27 +0000 (UTC) X-FDA: 79996049574.12.8B2C5FB Received: from sin.source.kernel.org (sin.source.kernel.org [145.40.73.55]) by imf19.hostedemail.com (Postfix) with ESMTP id E2C0E1A001A for ; Sat, 8 Oct 2022 01:29:24 +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 sin.source.kernel.org (Postfix) with ESMTPS id 880ACCE1926; Sat, 8 Oct 2022 01:29:20 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id B6C01C433C1; Sat, 8 Oct 2022 01:29:12 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=zx2c4.com; s=20210105; t=1665192550; 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: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=2a2X1YlfkZ0pGgKe1BT3qPauqYJUuDonzfwdbODwUj0=; b=F0WFV8408bfDyb840yWHYb2sS5Uszq0PMkkmtmrVRBUNL2FY49DLuZ3A8VZX01cjH92PRw 963/yLDTfSbPjK9v/cSx6ClQtvwOaP31xlyhs5+LtAOzyMXtRqv5atZ8HOk8wnFIOFKYJl weXSG5WPTsM48QIVGdJf4b19sbZPAzo= Received: by mail.zx2c4.com (ZX2C4 Mail Server) with ESMTPSA id 08b122db (TLSv1.3:TLS_AES_256_GCM_SHA384:256:NO); Sat, 8 Oct 2022 01:29:09 +0000 (UTC) Date: Fri, 7 Oct 2022 19:28:59 -0600 From: "Jason A. Donenfeld" To: "Darrick J. Wong" Cc: 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 , Christophe Leroy , 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 , Kees Cook , 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, Jan Kara Subject: Re: [PATCH v4 2/6] treewide: use prandom_u32_max() when possible Message-ID: References: <20221007180107.216067-1-Jason@zx2c4.com> <20221007180107.216067-3-Jason@zx2c4.com> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1665192565; 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:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=2a2X1YlfkZ0pGgKe1BT3qPauqYJUuDonzfwdbODwUj0=; b=cpNXjNVBQu8QnyKuvca6/aEtKy0+9EMFzhCi1FHOcD1+FQ2g+xOAB+/Z3fW9EQZCPAqr3t loPnvKsf0f9GeM14uPUYPK4FMUPl1fQ2awaR1NULX5snhLt5zCfZUE3Uoew2IWOv7VLm/x +bADqFKDDg7Ci4gYmwiEQ4QAnAUrJBo= ARC-Authentication-Results: i=1; imf19.hostedemail.com; dkim=pass header.d=zx2c4.com header.s=20210105 header.b=F0WFV840; spf=pass (imf19.hostedemail.com: domain of "SRS0=1Ou5=2J=zx2c4.com=Jason@kernel.org" designates 145.40.73.55 as permitted sender) smtp.mailfrom="SRS0=1Ou5=2J=zx2c4.com=Jason@kernel.org"; dmarc=pass (policy=quarantine) header.from=zx2c4.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1665192565; a=rsa-sha256; cv=none; b=GNwWc7T/hMfr3bo75+HLm8jl6SM8Br6h69+VoaEz9gvUSnR/oSEiVHlrPJdjphaYfNw3Wr Vq05bN6UTAXf8xMYdf0hmk8lrB9ITtVQQD9qGeBNEawRbVaWfm61yDQXmfmN4nAczIkQic OqBhHVmFMizKUVUDXLuBVAowdTpov6Q= X-Rspam-User: X-Rspamd-Server: rspam11 X-Rspamd-Queue-Id: E2C0E1A001A X-Stat-Signature: ff1jq5cgus14qwiaz4cr7czqiin8nn8g Authentication-Results: imf19.hostedemail.com; dkim=pass header.d=zx2c4.com header.s=20210105 header.b=F0WFV840; spf=pass (imf19.hostedemail.com: domain of "SRS0=1Ou5=2J=zx2c4.com=Jason@kernel.org" designates 145.40.73.55 as permitted sender) smtp.mailfrom="SRS0=1Ou5=2J=zx2c4.com=Jason@kernel.org"; dmarc=pass (policy=quarantine) header.from=zx2c4.com X-HE-Tag: 1665192564-51170 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 Fri, Oct 07, 2022 at 02:17:22PM -0700, Darrick J. Wong wrote: > On Fri, Oct 07, 2022 at 12:01:03PM -0600, Jason A. Donenfeld wrote: > > Rather than incurring a division or requesting too many random bytes for > > the given range, use the prandom_u32_max() function, which only takes > > the minimum required bytes from the RNG and avoids divisions. > > > > Reviewed-by: Kees Cook > > Reviewed-by: KP Singh > > Reviewed-by: Christoph Böhmwalder # for drbd > > Reviewed-by: Jan Kara # for ext2, ext4, and sbitmap > > Signed-off-by: Jason A. Donenfeld > > --- > > > > > diff --git a/fs/xfs/libxfs/xfs_alloc.c b/fs/xfs/libxfs/xfs_alloc.c > > index e2bdf089c0a3..6261599bb389 100644 > > --- a/fs/xfs/libxfs/xfs_alloc.c > > +++ b/fs/xfs/libxfs/xfs_alloc.c > > @@ -1520,7 +1520,7 @@ xfs_alloc_ag_vextent_lastblock( > > > > #ifdef DEBUG > > /* Randomly don't execute the first algorithm. */ > > - if (prandom_u32() & 1) > > + if (prandom_u32_max(2)) > > I wonder if these usecases (picking 0 or 1 randomly) ought to have a > trivial wrapper to make it more obvious that we want boolean semantics: > > static inline bool prandom_bool(void) > { > return prandom_u32_max(2); > } > > if (prandom_bool()) > use_crazy_algorithm(...); > Yea, I've had a lot of similar ideas there. Part of doing this (initial) patchset is to get an intuitive sense of what's actually used and how often. On my list for investigation are a get_random_u32_max() to return uniform numbers by rejection sampling (prandom_u32_max() doesn't do that uniformly) and adding a function for booleans or bits < 8. Possible ideas for the latter include: bool get_random_bool(void); bool get_random_bool(unsigned int probability); bool get_random_bits(u8 bits_less_than_eight); With the core of all of those involving the same batching as the current get_random_u{8,16,32,64}() functions, but also buffering the latest byte and managing how many bits are left in it that haven't been shifted out yet. So API-wise, there are a few ways to go, so hopefully this series will start to give a good picture of what's needed. One thing I've noticed is that most of the prandom_u32_max(2) invocations are in debug or test code, so that doesn't need to be optimized. But kfence does that too in its hot path, so a get_random_bool() function there would in theory lead to an 8x speed-up. But I guess I just have to try some things and see. Anyway, that is a long way to say, I share you curiosity on the matter and I'm looking into it. Jason