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 299D7C433FE for ; Thu, 6 Oct 2022 06:30:21 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender:List-Subscribe:List-Help :List-Post:List-Archive:List-Unsubscribe:List-Id:In-Reply-To: Content-Transfer-Encoding:Content-Type:MIME-Version:References: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=51dSYIsgEywaJmdEPyI7vbp82hVQfJ6jQKHayIqsWH8=; b=jESyTQQ9iL7R19USmQVjF7B1PU I2bnNr/L8m9McJFIaL+shXPc0RKHa7OODbCOY/HVibS1myx0TpInVHoqcwJVU4vCkyTCXImN8sN3q Isub9GAd1rM9yhDXJZAjJMwXQunrk/Y8PYA5gXiQ1fUoMraCH20xRaDjaJxZc8ydfDIB6P+y1KDM/ FVTMYxEsNQ50CS2BeVpP9ht7+kEAbyZffZ/GxdkCEyI/rAq6JtEpyVOxmcnln8ajG5x3MzKXv9Kcv KaZ0HjhB+u+oYfq/VZfKQPPuvYrQhVViZpEJc22xNi8kvBqQpOwi/IOfyI0+AAMAamMiJCIUIBcrz OTLO6NqQ==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.94.2 #2 (Red Hat Linux)) id 1ogKOf-0009wV-QN; Thu, 06 Oct 2022 06:30:17 +0000 Received: from mail-pf1-x432.google.com ([2607:f8b0:4864:20::432]) by bombadil.infradead.org with esmtps (Exim 4.94.2 #2 (Red Hat Linux)) id 1ogIvD-00HUNZ-PB for linux-nvme@lists.infradead.org; Thu, 06 Oct 2022 04:55:49 +0000 Received: by mail-pf1-x432.google.com with SMTP id g28so1020631pfk.8 for ; Wed, 05 Oct 2022 21:55:45 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; h=in-reply-to:content-transfer-encoding:content-disposition :mime-version:references:message-id:subject:cc:to:from:date:from:to :cc:subject:date; bh=51dSYIsgEywaJmdEPyI7vbp82hVQfJ6jQKHayIqsWH8=; b=e0wWc8f7UVTw3oXSVMh5SbOC8/ybqXORh0RfOsp37foz3bEHC+sZdUS2Cb48q2Rals cLwchuP5t4lVTMm+j0edXnLdA6eiOkpTvFVeI+b/+yanhEnnWG77EYRopMXRVrLoop3K nYwrVibqoZRWrTFw38o2fgxxWy4pLnB/kciNo= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=in-reply-to:content-transfer-encoding:content-disposition :mime-version:references:message-id:subject:cc:to:from:date :x-gm-message-state:from:to:cc:subject:date; bh=51dSYIsgEywaJmdEPyI7vbp82hVQfJ6jQKHayIqsWH8=; b=Jq2HVNU0Q8ND9B5zekJrGAnWDNeSKe3HPLYQn1iNQzumjk0bCA6THRLvVmrdCKzySu OqD1etXv5Q9W0o4kmpjc5kP51dKT6P3yLcJVGDtXhbZT3hiQ+TggSLhceyLr7SYwytKE IujxitfK2lblVsokgHTXXxKKOhIVcky+ZmIXkzn/2Bx6/7kGr8La9G0eeG8+2w8QW5DD kDabWaJT3RHC1xIzi/H8Ji94mXstckUDXa1O6B6lTGMv+XHYOKT395Wzl6HWKflT1hVd oiMmENG9xve36enlnH4k50mvUCq9k9dCx9gjHTg48RqBZvMe9PWaVojs9hxqcmLgMLVR xZ2g== X-Gm-Message-State: ACrzQf1DbhA0Kwdzs+WXMTF7q6zA5pvttHZUU831bj7OOADruCX/HtBM p+g7sjI0C5/PfB1jADHAVZ6zgg== X-Google-Smtp-Source: AMsMyM48HYcC17YfWxdcqN47yJn5oQWwbAuWOEmTE9E3JPhKRJg4eKNbmI4Bm6DfbK3tY9AIwV8yDw== X-Received: by 2002:a63:d613:0:b0:45a:654:cf16 with SMTP id q19-20020a63d613000000b0045a0654cf16mr2835844pgg.611.1665032145428; Wed, 05 Oct 2022 21:55:45 -0700 (PDT) Received: from www.outflux.net (smtp.outflux.net. [198.145.64.163]) by smtp.gmail.com with ESMTPSA id s14-20020a17090302ce00b0017a09ebd1e2sm11252393plk.237.2022.10.05.21.55.44 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 05 Oct 2022 21:55:44 -0700 (PDT) Date: Wed, 5 Oct 2022 21:55:43 -0700 From: Kees Cook To: "Jason A. Donenfeld" Cc: linux-kernel@vger.kernel.org, Ajay Singh , Akinobu Mita , Alexandre Torgue , Amitkumar Karwar , Andreas Dilger , Andreas =?iso-8859-1?Q?F=E4rber?= , Andreas Noever , Andrew Lunn , Andrew Morton , Andrii Nakryiko , Andy Gospodarek , Andy Lutomirski , Andy Shevchenko , Anil S Keshavamurthy , Anna Schumaker , Arend van Spriel , Ayush Sawal , Borislav Petkov , Chao Yu , Christoph =?iso-8859-1?Q?B=F6hmwalder?= , Christoph Hellwig , Christophe Leroy , Chuck Lever , Claudiu Beznea , Cong Wang , Dan Williams , Daniel Borkmann , "Darrick J . Wong" , Dave Hansen , David Ahern , "David S . Miller" , Dennis Dalessandro , Dick Kennedy , Dmitry Vyukov , Eric Dumazet , Florian Westphal , Franky Lin , Ganapathi Bhat , Greg Kroah-Hartman , Gregory Greenman , "H . Peter Anvin" , Hannes Reinecke , Hans Verkuil , Hante Meuleman , Hao Luo , Haoyue Xu , Heiner Kallweit , Helge Deller , Herbert Xu , Hideaki YOSHIFUJI , Hugh Dickins , Igor Mitsyanko , Ilya Dryomov , Ingo Molnar , Jack Wang , Jaegeuk Kim , Jaehoon Chung , Jakub Kicinski , Jamal Hadi Salim , "James E . J . Bottomley" , James Smart , Jan Kara , Jason Gunthorpe , Jay Vosburgh , Jean-Paul Roubelat , Jeff Layton , Jens Axboe , Jiri Olsa , Jiri Pirko , Johannes Berg , John Fastabend , John Stultz , Jon Maloy , Jonathan Corbet , Jozsef Kadlecsik , Julian Anastasov , KP Singh , Kalle Valo , Keith Busch , Lars Ellenberg , Leon Romanovsky , Manish Rangankar , Manivannan Sadhasivam , Marcelo Ricardo Leitner , Marco Elver , "Martin K . Petersen" , Martin KaFai Lau , Masami Hiramatsu , Mauro Carvalho Chehab , Maxime Coquelin , "Md . Haris Iqbal" , Michael Chan , Michael Ellerman , Michael Jamet , Michal Januszewski , Mika Westerberg , Miquel Raynal , Namjae Jeon , "Naveen N . Rao" , Neil Horman , Nicholas Piggin , Nilesh Javali , OGAWA Hirofumi , Pablo Neira Ayuso , Paolo Abeni , Peter Zijlstra , Philipp Reisner , Potnuri Bharat Teja , Pravin B Shelar , Rasmus Villemoes , Richard Weinberger , Rohit Maheshwari , Russell King , Sagi Grimberg , Santosh Shilimkar , Sergey Matyukevich , Sharvari Harisangam , Simon Horman , Song Liu , Stanislav Fomichev , Steffen Klassert , Stephen Boyd , Stephen Hemminger , Sungjong Seo , Theodore Ts'o , Thomas Gleixner , Thomas Graf , Thomas Sailer , Toke =?iso-8859-1?Q?H=F8iland-J=F8rgensen?= , Trond Myklebust , Ulf Hansson , Varun Prakash , Veaceslav Falico , Vignesh Raghavendra , Vinay Kumar Yadav , Vinod Koul , Vlad Yasevich , Wenpeng Liang , Xinming Hu , Xiubo Li , Yehezkel Bernat , Ying Xue , Yishai Hadas , Yonghong Song , Yury Norov , brcm80211-dev-list.pdl@broadcom.com, cake@lists.bufferbloat.net, ceph-devel@vger.kernel.org, coreteam@netfilter.org, dccp@vger.kernel.org, dev@openvswitch.org, dmaengine@vger.kernel.org, drbd-dev@lists.linbit.com, dri-devel@lists.freedesktop.org, kasan-dev@googlegroups.com, linux-actions@lists.infradead.org, linux-arm-kernel@lists.infradead.org, linux-block@vger.kernel.org, linux-crypto@vger.kernel.org, linux-doc@vger.kernel.org, linux-ext4@vger.kernel.org, linux-f2fs-devel@lists.sourceforge.net, linux-fbdev@vger.kernel.org, linux-fsdevel@vger.kernel.org, linux-hams@vger.kernel.org, linux-media@vger.kernel.org, linux-mm@kvack.org, linux-mmc@vger.kernel.org, linux-mtd@lists.infradead.org, linux-nfs@vger.kernel.org, linux-nvme@lists.infradead.org, linux-raid@vger.kernel.org, linux-rdma@vger.kernel.org, linux-scsi@vger.kernel.org, linux-sctp@vger.kernel.org, linux-stm32@st-md-mailman.stormreply.com, linux-usb@vger.kernel.org, linux-wireless@vger.kernel.org, linux-xfs@vger.kernel.org, linuxppc-dev@lists.ozlabs.org, lvs-devel@vger.kernel.org, netdev@vger.kernel.org, netfilter-devel@vger.kernel.org, rds-devel@oss.oracle.com, SHA-cyfmac-dev-list@infineon.com, target-devel@vger.kernel.org, tipc-discussion@lists.sourceforge.net Subject: Re: [PATCH v1 0/5] treewide cleanup of random integer usage Message-ID: <202210052148.B11CBC60@keescook> References: <20221005214844.2699-1-Jason@zx2c4.com> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: <20221005214844.2699-1-Jason@zx2c4.com> X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20221005_215547_829759_D5443315 X-CRM114-Status: GOOD ( 37.08 ) X-Mailman-Approved-At: Wed, 05 Oct 2022 23:27:55 -0700 X-BeenThere: linux-nvme@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "Linux-nvme" Errors-To: linux-nvme-bounces+linux-nvme=archiver.kernel.org@lists.infradead.org On Wed, Oct 05, 2022 at 11:48:39PM +0200, Jason A. Donenfeld wrote: > Hi folks, > > This is a five part treewide cleanup of random integer handling. The > rules for random integers are: > > - If you want a secure or an insecure random u64, use get_random_u64(). > - If you want a secure or an insecure random u32, use get_random_u32(). > * The old function prandom_u32() has been deprecated for a while now > and is just a wrapper around get_random_u32(). > - If you want a secure or an insecure random u16, use get_random_u16(). > - If you want a secure or an insecure random u8, use get_random_u8(). > - If you want secure or insecure random bytes, use get_random_bytes(). > * The old function prandom_bytes() has been deprecated for a while now > and has long been a wrapper around get_random_bytes(). > - If you want a non-uniform random u32, u16, or u8 bounded by a certain > open interval maximum, use prandom_u32_max(). > * I say "non-uniform", because it doesn't do any rejection sampling or > divisions. Hence, it stays within the prandom_* namespace. > > These rules ought to be applied uniformly, so that we can clean up the > deprecated functions, and earn the benefits of using the modern > functions. In particular, in addition to the boring substitutions, this > patchset accomplishes a few nice effects: > > - By using prandom_u32_max() with an upper-bound that the compiler can > prove at compile-time is ≤65536 or ≤256, internally get_random_u16() > or get_random_u8() is used, which wastes fewer batched random bytes, > and hence has higher throughput. > > - By using prandom_u32_max() instead of %, when the upper-bound is not a > constant, division is still avoided, because prandom_u32_max() uses > a faster multiplication-based trick instead. > > - By using get_random_u16() or get_random_u8() in cases where the return > value is intended to indeed be a u16 or a u8, we waste fewer batched > random bytes, and hence have higher throughput. > > So, based on those rules and benefits from following them, this patchset > breaks down into the following five steps: > > 1) Replace `prandom_u32() % max` and variants thereof with > prandom_u32_max(max). > > 2) Replace `(type)get_random_u32()` and variants thereof with > get_random_u16() or get_random_u8(). I took the pains to actually > look and see what every lvalue type was across the entire tree. > > 3) Replace remaining deprecated uses of prandom_u32() with > get_random_u32(). > > 4) Replace remaining deprecated uses of prandom_bytes() with > get_random_bytes(). > > 5) Remove the deprecated and now-unused prandom_u32() and > prandom_bytes() inline wrapper functions. > > I was thinking of taking this through my random.git tree (on which this > series is currently based) and submitting it near the end of the merge > window, or waiting for the very end of the 6.1 cycle when there will be > the fewest new patches brewing. If somebody with some treewide-cleanup > experience might share some wisdom about what the best timing usually > winds up being, I'm all ears. It'd be nice to capture some (all?) of the above somewhere. Perhaps just a massive comment in the header? > I've CC'd get_maintainers.pl, which is a pretty big list. Probably some > portion of those are going to bounce, too, and everytime you reply to > this thread, you'll have to deal with a bunch of bounces coming > immediately after. And a recipient list this big will probably dock my > email domain's spam reputation, at least temporarily. Sigh. I think > that's just how it goes with treewide cleanups though. Again, let me > know if I'm doing it wrong. I usually stick to just mailing lists and subsystem maintainers. If any of the subsystems ask you to break this up (I hope not), I've got this[1], which does a reasonable job of splitting a commit up into separate commits for each matching subsystem. Showing that a treewide change can be reproduced mechanically helps with keeping it together as one bit treewide patch, too, I've found. :) Thank you for the cleanup! The "u8 rnd = get_random_u32()" in the tree has bothered me for a loooong time. -Kees -- Kees Cook