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 lists.sourceforge.net (lists.sourceforge.net [216.105.38.7]) (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 AF909C433FE for ; Thu, 6 Oct 2022 04:23:02 +0000 (UTC) Received: from [127.0.0.1] (helo=sfs-ml-1.v29.lw.sourceforge.com) by sfs-ml-1.v29.lw.sourceforge.com with esmtp (Exim 4.95) (envelope-from ) id 1ogIPT-0001IQ-My; Thu, 06 Oct 2022 04:22:59 +0000 Received: from [172.30.20.202] (helo=mx.sourceforge.net) by sfs-ml-1.v29.lw.sourceforge.com with esmtps (TLS1.2) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.95) (envelope-from ) id 1ogIPR-0001IK-4W for linux-f2fs-devel@lists.sourceforge.net; Thu, 06 Oct 2022 04:22:57 +0000 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=sourceforge.net; s=x; h=Content-Type:Cc:To:Subject:Message-ID:Date:From: In-Reply-To:References:MIME-Version:Sender:Reply-To:Content-Transfer-Encoding :Content-ID:Content-Description:Resent-Date:Resent-From:Resent-Sender: Resent-To:Resent-Cc:Resent-Message-ID:List-Id:List-Help:List-Unsubscribe: List-Subscribe:List-Post:List-Owner:List-Archive; bh=79D/+k/bRZXhGBAaempdgng3l68Zp7pbnLAWamJJEQU=; b=WGfYxA9/nmrVAss6aZJBbVO+jc 7EFgdl51lzyuU+88/vDWeQ/yKL5PRdu2R9qVQ3whO9DttAtbbSn42q9byMizGU3ZBmPKp37aRQbEP iVMJOke/TyEOn3r8Z++PkMi0oLcs/eeD+XZioaNoED1t/u9OmCa5ewPWiUdmsW9eLZ0c=; DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=sf.net; s=x ; h=Content-Type:Cc:To:Subject:Message-ID:Date:From:In-Reply-To:References: MIME-Version:Sender:Reply-To:Content-Transfer-Encoding:Content-ID: Content-Description:Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc :Resent-Message-ID:List-Id:List-Help:List-Unsubscribe:List-Subscribe: List-Post:List-Owner:List-Archive; bh=79D/+k/bRZXhGBAaempdgng3l68Zp7pbnLAWamJJEQU=; b=WT/5yR/BPSi1bA1AIqVBTOgs2g VOqERgCNjEzYRAj6xE96b+rOqswolwoaQvHTJmEhUwrTd3upB8bVo++nV3u3b7cZ0YzuUPNqK8y6X ztqSJeguIJQg8fG9idx55CkA59B+1LH+1VKXtgh3EtNPFfgG62MyTq/ZIEeBAeTtXppY=; Received: from ams.source.kernel.org ([145.40.68.75]) by sfi-mx-1.v28.lw.sourceforge.com with esmtps (TLS1.2:ECDHE-RSA-AES256-GCM-SHA384:256) (Exim 4.95) id 1ogIPQ-00EI7K-39 for linux-f2fs-devel@lists.sourceforge.net; Thu, 06 Oct 2022 04:22:57 +0000 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 F107EB82002 for ; Thu, 6 Oct 2022 04:22:44 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id A7630C4FF6F for ; Thu, 6 Oct 2022 04:22:42 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1665030162; bh=HB4K7E86nsfwh8pjE5w4FJpZB/dXbKFz0OtI2RFCH+s=; h=References:In-Reply-To:From:Date:Subject:To:Cc:From; b=U243XDpeQtdzP8OLbjBO8J34oe/T3rthPl2gsNKiT9xCqeUnUTBzfbdZR0oEbv8Mp lPUf13U9HZtldKkHu3hieQy1ooxgGr3je2Iyuov29SulKcYwRSQg8aT8ndxjKkrBm9 yYZ1Uq8szapDDqaY77n9nTr7uQ/9X9WvUHPII0+TEdYCoMzEYZVnUy1SDg92Dq0uG2 Ju8aaYT1AhIsd8qi8oF6wIzJ677pb+rbuaKAcxpTVzA4yhtL+83XMe6/9hKeCfKjTB mL7xYFl5DTW4GpUcX5w0ZJgJfMNDJ8SLOXyfnOzTwGam4VqrQ51ift8aGqYZ18csob aXa7zsdW2FHNQ== Received: by mail-lf1-f44.google.com with SMTP id g1so915657lfu.12 for ; Wed, 05 Oct 2022 21:22:42 -0700 (PDT) X-Gm-Message-State: ACrzQf2Htpnemga9BGGzle2bDmGkUphyPmS5pG0YrQMJfJDKM8858XQ6 Tlv3xsl4uY/pFKGkKbUX3lOQlJx1hJl18ceWzNwGWA== X-Google-Smtp-Source: AMsMyM4buFniz46ZSiVKh/+EZ3E6CrEv7WC+W0XWrOZd99wlz7Q68ThxnzcjqPbpKa49LlF82wYrc2S5syRYJ4jy1zU= X-Received: by 2002:a2e:7318:0:b0:26d:fdd9:6b2 with SMTP id o24-20020a2e7318000000b0026dfdd906b2mr1076019ljc.156.1665030148829; Wed, 05 Oct 2022 21:22:28 -0700 (PDT) MIME-Version: 1.0 References: <20221005214844.2699-1-Jason@zx2c4.com> <20221005214844.2699-2-Jason@zx2c4.com> <202210052035.A1020E3@keescook> In-Reply-To: <202210052035.A1020E3@keescook> From: KP Singh Date: Wed, 5 Oct 2022 21:22:17 -0700 X-Gmail-Original-Message-ID: Message-ID: To: Kees Cook X-Headers-End: 1ogIPQ-00EI7K-39 Subject: Re: [f2fs-dev] [PATCH v1 1/5] treewide: use prandom_u32_max() when possible X-BeenThere: linux-f2fs-devel@lists.sourceforge.net X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Andrew Lunn , "Darrick J . Wong" , Ulf Hansson , dri-devel@lists.freedesktop.org, Andrii Nakryiko , Hans Verkuil , linux-sctp@vger.kernel.org, "Md . Haris Iqbal" , Miquel Raynal , Christoph Hellwig , Andy Gospodarek , Sergey Matyukevich , Rohit Maheshwari , Michael Ellerman , ceph-devel@vger.kernel.org, Christophe Leroy , Jozsef Kadlecsik , Nilesh Javali , Jean-Paul Roubelat , Dick Kennedy , Jay Vosburgh , Potnuri Bharat Teja , Vinay Kumar Yadav , linux-nfs@vger.kernel.org, Nicholas Piggin , Igor Mitsyanko , Andy Lutomirski , linux-hams@vger.kernel.org, Thomas Gleixner , Trond Myklebust , linux-raid@vger.kernel.org, Neil Horman , Hante Meuleman , Greg Kroah-Hartman , linux-usb@vger.kernel.org, Michael Chan , linux-kernel@vger.kernel.org, Varun Prakash , Chuck Lever , netfilter-devel@vger.kernel.org, Masami Hiramatsu , Jiri Olsa , Jan Kara , linux-fsdevel@vger.kernel.org, Lars Ellenberg , linux-media@vger.kernel.org, Claudiu Beznea , Sharvari Harisangam , "Jason A. Donenfeld" , linux-doc@vger.kernel.org, linux-mmc@vger.kernel.org, Dave Hansen , Song Liu , Eric Dumazet , target-devel@vger.kernel.org, John Stultz , Stanislav Fomichev , Gregory Greenman , drbd-dev@lists.linbit.com, dev@openvswitch.org, Leon Romanovsky , Helge Deller , Hugh Dickins , James Smart , Anil S Keshavamurthy , Pravin B Shelar , Julian Anastasov , coreteam@netfilter.org, Veaceslav Falico , Yonghong Song , Namjae Jeon , linux-crypto@vger.kernel.org, Santosh Shilimkar , Ganapathi Bhat , linux-actions@lists.infradead.org, Simon Horman , Jaegeuk Kim , Mika Westerberg , Andrew Morton , OGAWA Hirofumi , Hao Luo , Theodore Ts'o , Stephen Boyd , Dennis Dalessandro , Florian Westphal , =?UTF-8?Q?Andreas_F=C3=A4rber?= , Jon Maloy , Vlad Yasevich , Anna Schumaker , Yehezkel Bernat , Haoyue Xu , Heiner Kallweit , linux-wireless@vger.kernel.org, Marcelo Ricardo Leitner , Rasmus Villemoes , linux-fbdev@vger.kernel.org, linux-nvme@lists.infradead.org, Michal Januszewski , linux-mtd@lists.infradead.org, kasan-dev@googlegroups.com, Cong Wang , Thomas Sailer , Ajay Singh , Xiubo Li , Sagi Grimberg , Daniel Borkmann , Jonathan Corbet , linux-rdma@vger.kernel.org, lvs-devel@vger.kernel.org, linux-arm-kernel@lists.infradead.org, "Naveen N . Rao" , Ilya Dryomov , Paolo Abeni , Pablo Neira Ayuso , Marco Elver , Yury Norov , "James E . J . Bottomley" , Jamal Hadi Salim , Borislav Petkov , Keith Busch , Dan Williams , Mauro Carvalho Chehab , Franky Lin , Arend van Spriel , linux-ext4@vger.kernel.org, Wenpeng Liang , "Martin K . Petersen" , Xinming Hu , linux-stm32@st-md-mailman.stormreply.com, Jeff Layton , linux-xfs@vger.kernel.org, netdev@vger.kernel.org, Ying Xue , Manish Rangankar , "David S . Miller" , =?UTF-8?B?VG9rZSBIw7hpbGFuZC1Kw7hyZ2Vuc2Vu?= , Vignesh Raghavendra , Peter Zijlstra , "H . Peter Anvin" , Alexandre Torgue , Amitkumar Karwar , linux-mm@kvack.org, Andreas Dilger , Ayush Sawal , Andreas Noever , Jiri Pirko , linux-f2fs-devel@lists.sourceforge.net, Jack Wang , Steffen Klassert , rds-devel@oss.oracle.com, Herbert Xu , linux-scsi@vger.kernel.org, dccp@vger.kernel.org, Richard Weinberger , Russell King , Jason Gunthorpe , SHA-cyfmac-dev-list@infineon.com, Ingo Molnar , Jakub Kicinski , John Fastabend , Maxime Coquelin , Manivannan Sadhasivam , Michael Jamet , Kalle Valo , Akinobu Mita , linux-block@vger.kernel.org, dmaengine@vger.kernel.org, Hannes Reinecke , Andy Shevchenko , Dmitry Vyukov , Jens Axboe , cake@lists.bufferbloat.net, brcm80211-dev-list.pdl@broadcom.com, Yishai Hadas , Hideaki YOSHIFUJI , linuxppc-dev@lists.ozlabs.org, David Ahern , Philipp Reisner , Stephen Hemminger , =?UTF-8?Q?Christoph_B=C3=B6hmwalder?= , Vinod Koul , tipc-discussion@lists.sourceforge.net, Thomas Graf , Johannes Berg , Sungjong Seo , Martin KaFai Lau Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Errors-To: linux-f2fs-devel-bounces@lists.sourceforge.net On Wed, Oct 5, 2022 at 9:16 PM Kees Cook wrote: > > On Wed, Oct 05, 2022 at 11:48:40PM +0200, 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. > > Yes please! > > Since this is a treewide patch, it's helpful for (me at least) doing > reviews to detail the mechanism of the transformation. > > e.g. I imagine this could be done with something like Coccinelle and > > @no_modulo@ > expression E; > @@ > > - (prandom_u32() % (E)) > + prandom_u32_max(E) > > > diff --git a/drivers/mtd/ubi/debug.h b/drivers/mtd/ubi/debug.h > > index 118248a5d7d4..4236c799a47c 100644 > > --- a/drivers/mtd/ubi/debug.h > > +++ b/drivers/mtd/ubi/debug.h > > @@ -73,7 +73,7 @@ static inline int ubi_dbg_is_bgt_disabled(const struct ubi_device *ubi) > > static inline int ubi_dbg_is_bitflip(const struct ubi_device *ubi) > > { > > if (ubi->dbg.emulate_bitflips) > > - return !(prandom_u32() % 200); > > + return !(prandom_u32_max(200)); > > return 0; > > } > > > > Because some looks automated (why the parens?) > > > @@ -393,14 +387,11 @@ static struct test_driver { > > > > static void shuffle_array(int *arr, int n) > > { > > - unsigned int rnd; > > int i, j; > > > > for (i = n - 1; i > 0; i--) { > > - rnd = prandom_u32(); > > - > > /* Cut the range. */ > > - j = rnd % i; > > + j = prandom_u32_max(i); > > > > /* Swap indexes. */ > > swap(arr[i], arr[j]); > > And some by hand. :) > > Reviewed-by: Kees Cook Thanks! Reviewed-by: KP Singh > > -- > Kees Cook _______________________________________________ Linux-f2fs-devel mailing list Linux-f2fs-devel@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/linux-f2fs-devel 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 391ACC433FE for ; Thu, 6 Oct 2022 04:22:57 +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:Cc:To:Subject:Message-ID:Date:From: In-Reply-To:References:MIME-Version:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=v/fEenHRIPT3xpm2JgbP/932hPJe/VcWb9FsmAwkNlc=; b=rakdKVAgv8BWKX 5jRZp555RJ+yCTq5mYlNAIi3KKxmVborrCjSInsRJ538Ma6zhuGeyaka4lhy8poZ8zjGBZuoPHNAE 6DovhnN2cca9m3BSO9d4RBAnlw9IrtQF+hGO1LP10OzBlUhFrtr3Ly/+J1NS2ynmj0K/P68Nksdg8 4dHD0GiGjrYt05AzMv8y36G1aSBKa9vFegH1yMD+0Jj/tvFTT2mXBmzv6UlKhmyjBuv6WcLVOPLfn 5e2Ph4jmoZDfDHmrW/6dOt7ObsF9b4fyXDJQqJkvF176/OrflUC3LrhOHpTZQIL81CL07OlJ6vDqh rvKrG0lgibNIizk0DVZg==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.94.2 #2 (Red Hat Linux)) id 1ogIPI-00HLyg-OC; Thu, 06 Oct 2022 04:22:48 +0000 Received: from sin.source.kernel.org ([145.40.73.55]) by bombadil.infradead.org with esmtps (Exim 4.94.2 #2 (Red Hat Linux)) id 1ogIPF-00HLwU-0D for linux-mtd@lists.infradead.org; Thu, 06 Oct 2022 04:22:46 +0000 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 55C7ACE138A for ; Thu, 6 Oct 2022 04:22:42 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id C7978C4347C for ; Thu, 6 Oct 2022 04:22:40 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1665030160; bh=HB4K7E86nsfwh8pjE5w4FJpZB/dXbKFz0OtI2RFCH+s=; h=References:In-Reply-To:From:Date:Subject:To:Cc:From; b=rz0KgRVgcRWERBFB0+m+Y6CztoFmNTgnw9GG43ySTARE7wZXz3UC6ihy350JUmeF7 xtBJdIPB17keeDilDNDhV2M0pmLYoJAIW11OKFth9HaBexsSobS9UP3RlvUSth5qdn qBNDZ+lGFzgdIOME1WHf1r+XrGOnHOdvAgyY/JPuOyhlxU7e81fEw2YHOSIfbdPfvw X5nM4c6zIOslK7Ru1b6C3PyBvVVK+sngsg5M8KPYZcB1XV7mUjOIUa4EhIQhsCIdV0 OAofcEL44SvP+vqKX1lSPld3V1Jpl27QrV0gaHSIZJwQsj67WyvOturwfw1hnRsxk9 EaUs1J6jBgc2Q== Received: by mail-ed1-f42.google.com with SMTP id g27so1085913edf.11 for ; Wed, 05 Oct 2022 21:22:40 -0700 (PDT) X-Gm-Message-State: ACrzQf0QQ98h1ryjim1AZlv5cTRc2DBjchtkQRh4MOHEAoTie2QGT2of As121zjWZQpmkFA3w5MnVFuliHAghyKYPYd0UvuL7g== X-Google-Smtp-Source: AMsMyM4buFniz46ZSiVKh/+EZ3E6CrEv7WC+W0XWrOZd99wlz7Q68ThxnzcjqPbpKa49LlF82wYrc2S5syRYJ4jy1zU= X-Received: by 2002:a2e:7318:0:b0:26d:fdd9:6b2 with SMTP id o24-20020a2e7318000000b0026dfdd906b2mr1076019ljc.156.1665030148829; Wed, 05 Oct 2022 21:22:28 -0700 (PDT) MIME-Version: 1.0 References: <20221005214844.2699-1-Jason@zx2c4.com> <20221005214844.2699-2-Jason@zx2c4.com> <202210052035.A1020E3@keescook> In-Reply-To: <202210052035.A1020E3@keescook> From: KP Singh Date: Wed, 5 Oct 2022 21:22:17 -0700 X-Gmail-Original-Message-ID: Message-ID: Subject: Re: [PATCH v1 1/5] treewide: use prandom_u32_max() when possible To: Kees Cook Cc: "Jason A. Donenfeld" , linux-kernel@vger.kernel.org, Ajay Singh , Akinobu Mita , Alexandre Torgue , Amitkumar Karwar , Andreas Dilger , =?UTF-8?Q?Andreas_F=C3=A4rber?= , 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 , =?UTF-8?Q?Christoph_B=C3=B6hmwalder?= , 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 , 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 , =?UTF-8?B?VG9rZSBIw7hpbGFuZC1Kw7hyZ2Vuc2Vu?= , 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 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20221005_212245_448801_610D3510 X-CRM114-Status: GOOD ( 30.46 ) X-BeenThere: linux-mtd@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: Linux MTD discussion mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Sender: "linux-mtd" Errors-To: linux-mtd-bounces+linux-mtd=archiver.kernel.org@lists.infradead.org On Wed, Oct 5, 2022 at 9:16 PM Kees Cook wrote: > > On Wed, Oct 05, 2022 at 11:48:40PM +0200, 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. > > Yes please! > > Since this is a treewide patch, it's helpful for (me at least) doing > reviews to detail the mechanism of the transformation. > > e.g. I imagine this could be done with something like Coccinelle and > > @no_modulo@ > expression E; > @@ > > - (prandom_u32() % (E)) > + prandom_u32_max(E) > > > diff --git a/drivers/mtd/ubi/debug.h b/drivers/mtd/ubi/debug.h > > index 118248a5d7d4..4236c799a47c 100644 > > --- a/drivers/mtd/ubi/debug.h > > +++ b/drivers/mtd/ubi/debug.h > > @@ -73,7 +73,7 @@ static inline int ubi_dbg_is_bgt_disabled(const struct ubi_device *ubi) > > static inline int ubi_dbg_is_bitflip(const struct ubi_device *ubi) > > { > > if (ubi->dbg.emulate_bitflips) > > - return !(prandom_u32() % 200); > > + return !(prandom_u32_max(200)); > > return 0; > > } > > > > Because some looks automated (why the parens?) > > > @@ -393,14 +387,11 @@ static struct test_driver { > > > > static void shuffle_array(int *arr, int n) > > { > > - unsigned int rnd; > > int i, j; > > > > for (i = n - 1; i > 0; i--) { > > - rnd = prandom_u32(); > > - > > /* Cut the range. */ > > - j = rnd % i; > > + j = prandom_u32_max(i); > > > > /* Swap indexes. */ > > swap(arr[i], arr[j]); > > And some by hand. :) > > Reviewed-by: Kees Cook Thanks! Reviewed-by: KP Singh > > -- > Kees Cook ______________________________________________________ Linux MTD discussion mailing list http://lists.infradead.org/mailman/listinfo/linux-mtd/ 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 gabe.freedesktop.org (gabe.freedesktop.org [131.252.210.177]) (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 B3E22C433F5 for ; Thu, 6 Oct 2022 05:33:26 +0000 (UTC) Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id B4AEC10E7DB; Thu, 6 Oct 2022 05:33:11 +0000 (UTC) Received: from dfw.source.kernel.org (dfw.source.kernel.org [IPv6:2604:1380:4641:c500::1]) by gabe.freedesktop.org (Postfix) with ESMTPS id B4F3210E1F4 for ; Thu, 6 Oct 2022 04:22:43 +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 dfw.source.kernel.org (Postfix) with ESMTPS id CAD0E61825 for ; Thu, 6 Oct 2022 04:22:42 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 1057CC43152 for ; Thu, 6 Oct 2022 04:22:41 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1665030161; bh=HB4K7E86nsfwh8pjE5w4FJpZB/dXbKFz0OtI2RFCH+s=; h=References:In-Reply-To:From:Date:Subject:To:Cc:From; b=sovw+cMG1PqLu3BzWeR5UstVr29Ayo36LHZjAiDcfG20hbLu76k5LKE18tIq5krU9 2IlB4duW8vX1hOnpq03B41LTEc7Wa5d0AGgAs71PAR/M4iDr/bTeeDH1FloO1irXn2 qyxMF4VVts5BCRediQoi+Safq01SOpOrcKX0H3I19tPcIWSCoK5Dyb0UKru6HoTuR1 vlK/vQjlJBmjjMMWBLASnCnLuMi6HFCToTu24EHP4xItpTqpQuqDPRook7owBtQJZ9 IPIHGEfoZ/tk2tw3ErCHQAkeB5/XSwS75qAaOVQYfkwmkURMi6cycxAciQq4oGw2vv f2vjWVjZrIETg== Received: by mail-ed1-f45.google.com with SMTP id u21so1098433edi.9 for ; Wed, 05 Oct 2022 21:22:40 -0700 (PDT) X-Gm-Message-State: ACrzQf2YHuEaCAEgIlOyrcvvqNhjSZXMBN64Y54b5XHxfN2+Qrjf5wGI +CaGiV2CIecC6uIaoQ/9Kx2Yp8mAJxFK4nccL17oCg== X-Google-Smtp-Source: AMsMyM4buFniz46ZSiVKh/+EZ3E6CrEv7WC+W0XWrOZd99wlz7Q68ThxnzcjqPbpKa49LlF82wYrc2S5syRYJ4jy1zU= X-Received: by 2002:a2e:7318:0:b0:26d:fdd9:6b2 with SMTP id o24-20020a2e7318000000b0026dfdd906b2mr1076019ljc.156.1665030148829; Wed, 05 Oct 2022 21:22:28 -0700 (PDT) MIME-Version: 1.0 References: <20221005214844.2699-1-Jason@zx2c4.com> <20221005214844.2699-2-Jason@zx2c4.com> <202210052035.A1020E3@keescook> In-Reply-To: <202210052035.A1020E3@keescook> From: KP Singh Date: Wed, 5 Oct 2022 21:22:17 -0700 X-Gmail-Original-Message-ID: Message-ID: Subject: Re: [PATCH v1 1/5] treewide: use prandom_u32_max() when possible To: Kees Cook Content-Type: text/plain; charset="UTF-8" X-Mailman-Approved-At: Thu, 06 Oct 2022 05:33:08 +0000 X-BeenThere: dri-devel@lists.freedesktop.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Direct Rendering Infrastructure - Development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Andrew Lunn , "Darrick J . Wong" , Ulf Hansson , dri-devel@lists.freedesktop.org, Andrii Nakryiko , Hans Verkuil , linux-sctp@vger.kernel.org, "Md . Haris Iqbal" , Miquel Raynal , Christoph Hellwig , Andy Gospodarek , Sergey Matyukevich , Rohit Maheshwari , Michael Ellerman , ceph-devel@vger.kernel.org, Christophe Leroy , Jozsef Kadlecsik , Nilesh Javali , Jean-Paul Roubelat , Dick Kennedy , Jay Vosburgh , Potnuri Bharat Teja , Vinay Kumar Yadav , linux-nfs@vger.kernel.org, Nicholas Piggin , Igor Mitsyanko , Andy Lutomirski , linux-hams@vger.kernel.org, Thomas Gleixner , Trond Myklebust , linux-raid@vger.kernel.org, Neil Horman , Hante Meuleman , Greg Kroah-Hartman , linux-usb@vger.kernel.org, Michael Chan , linux-kernel@vger.kernel.org, Varun Prakash , Chuck Lever , netfilter-devel@vger.kernel.org, Masami Hiramatsu , Jiri Olsa , Jan Kara , linux-fsdevel@vger.kernel.org, Lars Ellenberg , linux-media@vger.kernel.org, Claudiu Beznea , Sharvari Harisangam , "Jason A. Donenfeld" , linux-doc@vger.kernel.org, linux-mmc@vger.kernel.org, Dave Hansen , Song Liu , Eric Dumazet , target-devel@vger.kernel.org, John Stultz , Stanislav Fomichev , Gregory Greenman , drbd-dev@lists.linbit.com, dev@openvswitch.org, Leon Romanovsky , Helge Deller , Hugh Dickins , James Smart , Anil S Keshavamurthy , Pravin B Shelar , Julian Anastasov , coreteam@netfilter.org, Veaceslav Falico , Yonghong Song , Namjae Jeon , linux-crypto@vger.kernel.org, Santosh Shilimkar , Ganapathi Bhat , linux-actions@lists.infradead.org, Simon Horman , Jaegeuk Kim , Mika Westerberg , Andrew Morton , OGAWA Hirofumi , Hao Luo , Theodore Ts'o , Stephen Boyd , Dennis Dalessandro , Florian Westphal , =?UTF-8?Q?Andreas_F=C3=A4rber?= , Jon Maloy , Vlad Yasevich , Anna Schumaker , Yehezkel Bernat , Haoyue Xu , Heiner Kallweit , linux-wireless@vger.kernel.org, Marcelo Ricardo Leitner , Rasmus Villemoes , linux-fbdev@vger.kernel.org, linux-nvme@lists.infradead.org, Michal Januszewski , linux-mtd@lists.infradead.org, kasan-dev@googlegroups.com, Cong Wang , Thomas Sailer , Ajay Singh , Xiubo Li , Sagi Grimberg , Daniel Borkmann , Jonathan Corbet , linux-rdma@vger.kernel.org, lvs-devel@vger.kernel.org, linux-arm-kernel@lists.infradead.org, "Naveen N . Rao" , Ilya Dryomov , Paolo Abeni , Pablo Neira Ayuso , Marco Elver , Yury Norov , "James E . J . Bottomley" , Jamal Hadi Salim , Borislav Petkov , Keith Busch , Dan Williams , Mauro Carvalho Chehab , Franky Lin , Arend van Spriel , linux-ext4@vger.kernel.org, Wenpeng Liang , "Martin K . Petersen" , Xinming Hu , linux-stm32@st-md-mailman.stormreply.com, Jeff Layton , linux-xfs@vger.kernel.org, netdev@vger.kernel.org, Ying Xue , Manish Rangankar , "David S . Miller" , =?UTF-8?B?VG9rZSBIw7hpbGFuZC1Kw7hyZ2Vuc2Vu?= , Vignesh Raghavendra , Peter Zijlstra , "H . Peter Anvin" , Alexandre Torgue , Amitkumar Karwar , linux-mm@kvack.org, Andreas Dilger , Ayush Sawal , Andreas Noever , Jiri Pirko , linux-f2fs-devel@lists.sourceforge.net, Jack Wang , Steffen Klassert , rds-devel@oss.oracle.com, Herbert Xu , linux-scsi@vger.kernel.org, dccp@vger.kernel.org, Richard Weinberger , Russell King , Jaehoon Chung , Jason Gunthorpe , SHA-cyfmac-dev-list@infineon.com, Ingo Molnar , Jakub Kicinski , John Fastabend , Maxime Coquelin , Manivannan Sadhasivam , Michael Jamet , Kalle Valo , Chao Yu , Akinobu Mita , linux-block@vger.kernel.org, dmaengine@vger.kernel.org, Hannes Reinecke , Andy Shevchenko , Dmitry Vyukov , Jens Axboe , cake@lists.bufferbloat.net, brcm80211-dev-list.pdl@broadcom.com, Yishai Hadas , Hideaki YOSHIFUJI , linuxppc-dev@lists.ozlabs.org, David Ahern , Philipp Reisner , Stephen Hemminger , =?UTF-8?Q?Christoph_B=C3=B6hmwalder?= , Vinod Koul , tipc-discussion@lists.sourceforge.net, Thomas Graf , Johannes Berg , Sungjong Seo , Martin KaFai Lau Errors-To: dri-devel-bounces@lists.freedesktop.org Sender: "dri-devel" On Wed, Oct 5, 2022 at 9:16 PM Kees Cook wrote: > > On Wed, Oct 05, 2022 at 11:48:40PM +0200, 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. > > Yes please! > > Since this is a treewide patch, it's helpful for (me at least) doing > reviews to detail the mechanism of the transformation. > > e.g. I imagine this could be done with something like Coccinelle and > > @no_modulo@ > expression E; > @@ > > - (prandom_u32() % (E)) > + prandom_u32_max(E) > > > diff --git a/drivers/mtd/ubi/debug.h b/drivers/mtd/ubi/debug.h > > index 118248a5d7d4..4236c799a47c 100644 > > --- a/drivers/mtd/ubi/debug.h > > +++ b/drivers/mtd/ubi/debug.h > > @@ -73,7 +73,7 @@ static inline int ubi_dbg_is_bgt_disabled(const struct ubi_device *ubi) > > static inline int ubi_dbg_is_bitflip(const struct ubi_device *ubi) > > { > > if (ubi->dbg.emulate_bitflips) > > - return !(prandom_u32() % 200); > > + return !(prandom_u32_max(200)); > > return 0; > > } > > > > Because some looks automated (why the parens?) > > > @@ -393,14 +387,11 @@ static struct test_driver { > > > > static void shuffle_array(int *arr, int n) > > { > > - unsigned int rnd; > > int i, j; > > > > for (i = n - 1; i > 0; i--) { > > - rnd = prandom_u32(); > > - > > /* Cut the range. */ > > - j = rnd % i; > > + j = prandom_u32_max(i); > > > > /* Swap indexes. */ > > swap(arr[i], arr[j]); > > And some by hand. :) > > Reviewed-by: Kees Cook Thanks! Reviewed-by: KP Singh > > -- > Kees Cook 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 71BEBC433FE for ; Thu, 6 Oct 2022 06:28:43 +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:Content-Type:Cc:To:Subject: Message-ID:Date:From:In-Reply-To:References:MIME-Version:Reply-To: Content-Transfer-Encoding:Content-ID:Content-Description:Resent-Date: Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Owner; bh=79D/+k/bRZXhGBAaempdgng3l68Zp7pbnLAWamJJEQU=; b=lRK0trYw6BRPufWKwZyI7kNqxU 2dJlGEMNAOMGOvopzoc1ovJ9BRdgf9VVo+hdOliOWQf4gpqU0gg5oLI+KGStXeshvY5qpswbDP2c+ d+02UY+0IZbEQNuQyddb4CHfN05syolPR53XKPWMiECjPICaZcjsdin7lsNs02Cq9q0s7YIlYcyeK Uk6VNKmxu+22+FtgM/shZsHbWR/jepMmX4FR5kUT7w6Hba2Co4jyEmmmrvLDa4tFrY4gijxsflOQr u64wR7yBUvJ433nHU+iJdN+SCwcLcd6y0DI7QLCChW7k3b0K9ePdbGPtIoEm7OZzmWKYFGw1g01Xm 6pUUQS2g==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.94.2 #2 (Red Hat Linux)) id 1ogKN6-0009Qj-97; Thu, 06 Oct 2022 06:28:40 +0000 Received: from dfw.source.kernel.org ([2604:1380:4641:c500::1]) by bombadil.infradead.org with esmtps (Exim 4.94.2 #2 (Red Hat Linux)) id 1ogIPF-00HLwg-DJ for linux-nvme@lists.infradead.org; Thu, 06 Oct 2022 04:22:48 +0000 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 dfw.source.kernel.org (Postfix) with ESMTPS id 11CCB61876 for ; Thu, 6 Oct 2022 04:22:44 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id DE112C4FEBF for ; Thu, 6 Oct 2022 04:22:41 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1665030161; bh=HB4K7E86nsfwh8pjE5w4FJpZB/dXbKFz0OtI2RFCH+s=; h=References:In-Reply-To:From:Date:Subject:To:Cc:From; b=sovw+cMG1PqLu3BzWeR5UstVr29Ayo36LHZjAiDcfG20hbLu76k5LKE18tIq5krU9 2IlB4duW8vX1hOnpq03B41LTEc7Wa5d0AGgAs71PAR/M4iDr/bTeeDH1FloO1irXn2 qyxMF4VVts5BCRediQoi+Safq01SOpOrcKX0H3I19tPcIWSCoK5Dyb0UKru6HoTuR1 vlK/vQjlJBmjjMMWBLASnCnLuMi6HFCToTu24EHP4xItpTqpQuqDPRook7owBtQJZ9 IPIHGEfoZ/tk2tw3ErCHQAkeB5/XSwS75qAaOVQYfkwmkURMi6cycxAciQq4oGw2vv f2vjWVjZrIETg== Received: by mail-lf1-f43.google.com with SMTP id r14so970099lfm.2 for ; Wed, 05 Oct 2022 21:22:41 -0700 (PDT) X-Gm-Message-State: ACrzQf0oa09eob5jNk+ie+m24NR4cph/TpnmB3EkPv3IMqq+Yq8MwvH8 FXYkaaozgY8G3NHPq/7xF9E4ptgv2UzzICRq2mpaAQ== X-Google-Smtp-Source: AMsMyM4buFniz46ZSiVKh/+EZ3E6CrEv7WC+W0XWrOZd99wlz7Q68ThxnzcjqPbpKa49LlF82wYrc2S5syRYJ4jy1zU= X-Received: by 2002:a2e:7318:0:b0:26d:fdd9:6b2 with SMTP id o24-20020a2e7318000000b0026dfdd906b2mr1076019ljc.156.1665030148829; Wed, 05 Oct 2022 21:22:28 -0700 (PDT) MIME-Version: 1.0 References: <20221005214844.2699-1-Jason@zx2c4.com> <20221005214844.2699-2-Jason@zx2c4.com> <202210052035.A1020E3@keescook> In-Reply-To: <202210052035.A1020E3@keescook> From: KP Singh Date: Wed, 5 Oct 2022 21:22:17 -0700 X-Gmail-Original-Message-ID: Message-ID: Subject: Re: [PATCH v1 1/5] treewide: use prandom_u32_max() when possible To: Kees Cook Cc: "Jason A. Donenfeld" , linux-kernel@vger.kernel.org, Ajay Singh , Akinobu Mita , Alexandre Torgue , Amitkumar Karwar , Andreas Dilger , =?UTF-8?Q?Andreas_F=C3=A4rber?= , 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 , =?UTF-8?Q?Christoph_B=C3=B6hmwalder?= , 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 , 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 , =?UTF-8?B?VG9rZSBIw7hpbGFuZC1Kw7hyZ2Vuc2Vu?= , 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 Content-Type: text/plain; charset="UTF-8" X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20221005_212245_542934_5291EFB8 X-CRM114-Status: GOOD ( 30.89 ) 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 5, 2022 at 9:16 PM Kees Cook wrote: > > On Wed, Oct 05, 2022 at 11:48:40PM +0200, 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. > > Yes please! > > Since this is a treewide patch, it's helpful for (me at least) doing > reviews to detail the mechanism of the transformation. > > e.g. I imagine this could be done with something like Coccinelle and > > @no_modulo@ > expression E; > @@ > > - (prandom_u32() % (E)) > + prandom_u32_max(E) > > > diff --git a/drivers/mtd/ubi/debug.h b/drivers/mtd/ubi/debug.h > > index 118248a5d7d4..4236c799a47c 100644 > > --- a/drivers/mtd/ubi/debug.h > > +++ b/drivers/mtd/ubi/debug.h > > @@ -73,7 +73,7 @@ static inline int ubi_dbg_is_bgt_disabled(const struct ubi_device *ubi) > > static inline int ubi_dbg_is_bitflip(const struct ubi_device *ubi) > > { > > if (ubi->dbg.emulate_bitflips) > > - return !(prandom_u32() % 200); > > + return !(prandom_u32_max(200)); > > return 0; > > } > > > > Because some looks automated (why the parens?) > > > @@ -393,14 +387,11 @@ static struct test_driver { > > > > static void shuffle_array(int *arr, int n) > > { > > - unsigned int rnd; > > int i, j; > > > > for (i = n - 1; i > 0; i--) { > > - rnd = prandom_u32(); > > - > > /* Cut the range. */ > > - j = rnd % i; > > + j = prandom_u32_max(i); > > > > /* Swap indexes. */ > > swap(arr[i], arr[j]); > > And some by hand. :) > > Reviewed-by: Kees Cook Thanks! Reviewed-by: KP Singh > > -- > Kees Cook 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 lists.ozlabs.org (lists.ozlabs.org [112.213.38.117]) (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 18BBAC433FE for ; Thu, 6 Oct 2022 04:53:49 +0000 (UTC) Received: from boromir.ozlabs.org (localhost [IPv6:::1]) by lists.ozlabs.org (Postfix) with ESMTP id 4MjfGf6P9gz3c7K for ; Thu, 6 Oct 2022 15:53:46 +1100 (AEDT) Authentication-Results: lists.ozlabs.org; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20201202 header.b=ocUSwvib; dkim-atps=neutral Authentication-Results: lists.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=kernel.org (client-ip=2604:1380:4641:c500::1; helo=dfw.source.kernel.org; envelope-from=kpsingh@kernel.org; receiver=) Authentication-Results: lists.ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20201202 header.b=ocUSwvib; dkim-atps=neutral Received: from dfw.source.kernel.org (dfw.source.kernel.org [IPv6:2604:1380:4641:c500::1]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by lists.ozlabs.org (Postfix) with ESMTPS id 4MjdZx6g2Fz2xJN for ; Thu, 6 Oct 2022 15:22:49 +1100 (AEDT) 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 dfw.source.kernel.org (Postfix) with ESMTPS id 703496188A for ; Thu, 6 Oct 2022 04:22:45 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 76C84C43162 for ; Thu, 6 Oct 2022 04:22:43 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1665030163; bh=HB4K7E86nsfwh8pjE5w4FJpZB/dXbKFz0OtI2RFCH+s=; h=References:In-Reply-To:From:Date:Subject:To:Cc:From; b=ocUSwvibzgYcenYn4FG8vsRWFQGQjsvyOiPqTQva+2n19RDrsOdloFZIc76SKznVq zyhJxrRREbOmdlYpZgj/7UuvKU4VixWLe4UHGqKCMGP/uTt3hljIka6snp6wM0iwTF yXBAKJidiDjEPEC5TyMeglXl/dCOqqAIGC+nY834YhHqknMig/zv36I2xjhUXF1POV yMf4o11GMY9EiS6s9NIJkYm3tUaVmNVE0c9+SJTjpexgeqY8gGTCeopwCiRKe6LjkV WynnOMlYlHemnVYtLniOaU0AU1o/iBDSJLzLbGYCDlCIwvEioxdRicFxEwgg7w41uR 45XHCgy5/Yg+A== Received: by mail-lf1-f52.google.com with SMTP id bu25so962287lfb.3 for ; Wed, 05 Oct 2022 21:22:43 -0700 (PDT) X-Gm-Message-State: ACrzQf30tuNYrTw9DCgGWb83P44ucf2b8NzwNLpP+NBNTFGE8pmWk5xe A3MfQ+qhrqfMuy3IjJuVwRu4x91WgY0t/Wz4NSwVMQ== X-Google-Smtp-Source: AMsMyM4buFniz46ZSiVKh/+EZ3E6CrEv7WC+W0XWrOZd99wlz7Q68ThxnzcjqPbpKa49LlF82wYrc2S5syRYJ4jy1zU= X-Received: by 2002:a2e:7318:0:b0:26d:fdd9:6b2 with SMTP id o24-20020a2e7318000000b0026dfdd906b2mr1076019ljc.156.1665030148829; Wed, 05 Oct 2022 21:22:28 -0700 (PDT) MIME-Version: 1.0 References: <20221005214844.2699-1-Jason@zx2c4.com> <20221005214844.2699-2-Jason@zx2c4.com> <202210052035.A1020E3@keescook> In-Reply-To: <202210052035.A1020E3@keescook> From: KP Singh Date: Wed, 5 Oct 2022 21:22:17 -0700 X-Gmail-Original-Message-ID: Message-ID: Subject: Re: [PATCH v1 1/5] treewide: use prandom_u32_max() when possible To: Kees Cook Content-Type: text/plain; charset="UTF-8" X-Mailman-Approved-At: Thu, 06 Oct 2022 15:52:03 +1100 X-BeenThere: linuxppc-dev@lists.ozlabs.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Linux on PowerPC Developers Mail List List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Andrew Lunn , "Darrick J . Wong" , Ulf Hansson , dri-devel@lists.freedesktop.org, Andrii Nakryiko , Hans Verkuil , linux-sctp@vger.kernel.org, "Md . Haris Iqbal" , Miquel Raynal , Christoph Hellwig , Andy Gospodarek , Sergey Matyukevich , Rohit Maheshwari , ceph-devel@vger.kernel.org, Jozsef Kadlecsik , Nilesh Javali , Jean-Paul Roubelat , Dick Kennedy , Jay Vosburgh , Potnuri Bharat Teja , Vinay Kumar Yadav , linux-nfs@vger.kernel.org, Nicholas Piggin , Igor Mitsyanko , Andy Lutomirski , linux-hams@vger.kernel.org, Thomas Gleixner , Trond Myklebust , linux-raid@vger.kernel.org, Neil Horman , Hante Meuleman , Greg Kroah-Hartman , linux-usb@vger.kernel.org, Michael Chan , linux-kernel@vger.kernel.org, Varun Prakash , Chuck Lever , netfilter-devel@vger.kernel.org, Masami Hiramatsu , Jiri Olsa , Jan Kara , linux-fsdevel@vger.kernel.org, Lars Ellenberg , linux-media@vger.kernel.org, Claudiu Beznea , Sharvari Harisangam , "Jason A. Donenfeld" , linux-doc@vger.kernel.org, linux-mmc@vger.kernel.org, Dave Hansen , Song Liu , Eric Dumazet , target-devel@vger.kernel.org, John Stultz , Stanislav Fomichev , Gregory Greenman , drbd-dev@lists.linbit.com, dev@openvswitch.org, Leon Romanovsky , Helge Deller , Hugh Dickins , James Smart , Anil S Keshavamurthy , Pravin B Shelar , Julian Anastasov , coreteam@netfilter.org, Veaceslav Falico , Yonghong Song , Namjae Jeon , linux-crypto@vger.kernel.org, Santosh Shilimkar , Ganapathi Bhat , linux-actions@lists.infradead.org, Simon Horman , Jaegeuk Kim , Mika Westerberg , Andrew Morton , OGAWA Hirofumi , Hao Luo , Theodore Ts'o , Stephen Boyd , Dennis Dalessandro , Florian Westphal , =?UTF-8?Q?Andreas_F=C3=A4rber?= , Jon Maloy , Vlad Yasevich , Anna Schumaker , Yehezkel Bernat , Haoyue Xu , Heiner Kallweit , linux-wireless@vger.kernel.org, Marcelo Ricardo Leitner , Rasmus Villemoes , linux-fbdev@vger.kernel.org, linux-nvme@lists.infradead.org, Michal Januszewski , linux-mtd@lists.infradead.org, kasan-dev@googlegroups.com, Cong Wang , Thomas Sailer , Ajay Singh , Xiubo Li , Sagi Grimberg , Daniel Borkmann , Jonathan Corbet , linux-rdma@vger.kernel.org, lvs-devel@vger.kernel.org, linux-arm-kernel@lists.infradead.org, "Naveen N . Rao" , Ilya Dryomov , Paolo Abeni , Pablo Neira Ayuso , Marco Elver , Yury Norov , "James E . J . Bottomley" , Jamal Hadi Salim , Borislav Petkov , Keith Busch , Dan Williams , Mauro Carvalho Chehab , Franky Lin , Arend van Spriel , linux-ext4@vger.kernel.org, Wenpeng Liang , "Martin K . Petersen" , Xinming Hu , linux-stm32@st-md-mailman.stormreply.com, Jeff Layton , linux-xfs@vger.kernel.org, netdev@vger.kernel.org, Ying Xue , Manish Rangankar , "David S . Miller" , =?UTF-8?B?VG9rZSBIw7hpbGFuZC1Kw7hyZ2Vuc2Vu?= , Vignesh Raghavendra , Peter Zijlstra , "H . Peter Anvin" , Alexandre Torgue , Amitkumar Karwar , linux-mm@kvack.org, Andreas Dilger , Ayush Sawal , Andreas Noever , Jiri Pirko , linux-f2fs-devel@lists.sourceforge.net, Jack Wang , Steffen Klassert , rds-devel@oss.oracle.com, Herbert Xu , linux-scsi@vger.kernel.org, dccp@vger.kernel.org, Richard Weinberger , Russell King , Jaehoon Chung , Jason Gunthorpe , SHA-cyfmac-dev-list@infineon.com, Ingo Molnar , Jakub Kicinski , John Fastabend , Maxime Coquelin , Manivannan Sadhasivam , Michael Jamet , Kalle Valo , Chao Yu , Akinobu Mita , linux-block@vger.kernel.org, dmaengine@vger.kernel.org, Hannes Reinecke , Andy Shevchenko , Dmitry Vyukov , Jens Axboe , cake@lists.bufferbloat.net, brcm80211-dev-list.pdl@broadcom.com, Yishai Hadas , Hideaki YOSHIFUJI , linuxppc-dev@lists.ozlabs.org, David Ahern , Philipp Reisner , Stephen Hemminger , =?UTF-8?Q?Christoph_B=C3=B6hmwalder?= , Vinod Koul , tipc-discussion@lists.sourceforge.net, Thomas Graf , Johannes Berg , Sungjong Seo , Martin KaFai Lau Errors-To: linuxppc-dev-bounces+linuxppc-dev=archiver.kernel.org@lists.ozlabs.org Sender: "Linuxppc-dev" On Wed, Oct 5, 2022 at 9:16 PM Kees Cook wrote: > > On Wed, Oct 05, 2022 at 11:48:40PM +0200, 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. > > Yes please! > > Since this is a treewide patch, it's helpful for (me at least) doing > reviews to detail the mechanism of the transformation. > > e.g. I imagine this could be done with something like Coccinelle and > > @no_modulo@ > expression E; > @@ > > - (prandom_u32() % (E)) > + prandom_u32_max(E) > > > diff --git a/drivers/mtd/ubi/debug.h b/drivers/mtd/ubi/debug.h > > index 118248a5d7d4..4236c799a47c 100644 > > --- a/drivers/mtd/ubi/debug.h > > +++ b/drivers/mtd/ubi/debug.h > > @@ -73,7 +73,7 @@ static inline int ubi_dbg_is_bgt_disabled(const struct ubi_device *ubi) > > static inline int ubi_dbg_is_bitflip(const struct ubi_device *ubi) > > { > > if (ubi->dbg.emulate_bitflips) > > - return !(prandom_u32() % 200); > > + return !(prandom_u32_max(200)); > > return 0; > > } > > > > Because some looks automated (why the parens?) > > > @@ -393,14 +387,11 @@ static struct test_driver { > > > > static void shuffle_array(int *arr, int n) > > { > > - unsigned int rnd; > > int i, j; > > > > for (i = n - 1; i > 0; i--) { > > - rnd = prandom_u32(); > > - > > /* Cut the range. */ > > - j = rnd % i; > > + j = prandom_u32_max(i); > > > > /* Swap indexes. */ > > swap(arr[i], arr[j]); > > And some by hand. :) > > Reviewed-by: Kees Cook Thanks! Reviewed-by: KP Singh > > -- > Kees Cook From mboxrd@z Thu Jan 1 00:00:00 1970 From: KP Singh Subject: Re: [PATCH v1 1/5] treewide: use prandom_u32_max() when possible Date: Wed, 5 Oct 2022 21:22:17 -0700 Message-ID: References: <20221005214844.2699-1-Jason@zx2c4.com> <20221005214844.2699-2-Jason@zx2c4.com> <202210052035.A1020E3@keescook> Mime-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Return-path: DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=sourceforge.net; s=x; h=Content-Type:Cc:To:Subject:Message-ID:Date:From: In-Reply-To:References:MIME-Version:Sender:Reply-To:Content-Transfer-Encoding :Content-ID:Content-Description:Resent-Date:Resent-From:Resent-Sender: Resent-To:Resent-Cc:Resent-Message-ID:List-Id:List-Help:List-Unsubscribe: List-Subscribe:List-Post:List-Owner:List-Archive; bh=79D/+k/bRZXhGBAaempdgng3l68Zp7pbnLAWamJJEQU=; b=WGfYxA9/nmrVAss6aZJBbVO+jc 7EFgdl51lzyuU+88/vDWeQ/yKL5PRdu2R9qVQ3whO9DttAtbbSn42q9byMizGU3ZBmPKp37aRQbEP iVMJOke/TyEOn3r8Z++PkMi0oLcs/eeD+XZioaNoED1t/u9OmCa5ewPWiUdmsW9eLZ0c=; DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=sf.net; s=x ; h=Content-Type:Cc:To:Subject:Message-ID:Date:From:In-Reply-To:References: MIME-Version:Sender:Reply-To:Content-Transfer-Encoding:Content-ID: Content-Description:Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc :Resent-Message-ID:List-Id:List-Help:List-Unsubscribe:List-Subscribe: List-Post:List-Owner:List-Archive; bh=79D/+k/bRZXhGBAaempdgng3l68Zp7pbnLAWamJJEQU=; b=WT/5yR/BPSi1bA1AIqVBTOgs2g VOqERgCNjEzYRAj6xE96b+rOqswolwoaQvHTJmEhUwrTd3upB8bVo++nV3u3b7cZ0YzuUPNqK8y6X ztqSJeguIJQg8fG9idx55CkA59B+1LH+1VKXtgh3EtNPFfgG62MyTq/ZIEeBAeTtXppY=; DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1665030162; bh=HB4K7E86nsfwh8pjE5w4FJpZB/dXbKFz0OtI2RFCH+s=; h=References:In-Reply-To:From:Date:Subject:To:Cc:From; b=U243XDpeQtdzP8OLbjBO8J34oe/T3rthPl2gsNKiT9xCqeUnUTBzfbdZR0oEbv8Mp lPUf13U9HZtldKkHu3hieQy1ooxgGr3je2Iyuov29SulKcYwRSQg8aT8ndxjKkrBm9 yYZ1Uq8szapDDqaY77n9nTr7uQ/9X9WvUHPII0+TEdYCoMzEYZVnUy1SDg92Dq0uG2 Ju8aaYT1AhIsd8qi8oF6wIzJ677pb+rbuaKAcxpTVzA4yhtL+83XMe6/9hKeCfKjTB mL7xYFl5DTW4GpUcX5w0ZJgJfMNDJ8SLOXyfnOzTwGam4VqrQ51ift8aGqYZ18csob aXa7zsdW2FHNQ== In-Reply-To: <202210052035.A1020E3@keescook> List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: linux-f2fs-devel-bounces@lists.sourceforge.net To: Kees Cook Cc: Andrew Lunn , "Darrick J . Wong" , Ulf Hansson , dri-devel@lists.freedesktop.org, Andrii Nakryiko , Hans Verkuil , linux-sctp@vger.kernel.org, "Md . Haris Iqbal" , Miquel Raynal , Christoph Hellwig , Andy Gospodarek , Sergey Matyukevich , Rohit Maheshwari , Michael Ellerman , ceph-devel@vger.kernel.org, Christophe Leroy , Jozsef Kadlecsik , Nilesh Javali , Jean-Paul Roubelat , Dick Kennedy , Jay Vosburgh , Potnuri Bharat Teja , Vinay Kumar Yadav , linux-nfs@vg On Wed, Oct 5, 2022 at 9:16 PM Kees Cook wrote: > > On Wed, Oct 05, 2022 at 11:48:40PM +0200, 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. > > Yes please! > > Since this is a treewide patch, it's helpful for (me at least) doing > reviews to detail the mechanism of the transformation. > > e.g. I imagine this could be done with something like Coccinelle and > > @no_modulo@ > expression E; > @@ > > - (prandom_u32() % (E)) > + prandom_u32_max(E) > > > diff --git a/drivers/mtd/ubi/debug.h b/drivers/mtd/ubi/debug.h > > index 118248a5d7d4..4236c799a47c 100644 > > --- a/drivers/mtd/ubi/debug.h > > +++ b/drivers/mtd/ubi/debug.h > > @@ -73,7 +73,7 @@ static inline int ubi_dbg_is_bgt_disabled(const struct ubi_device *ubi) > > static inline int ubi_dbg_is_bitflip(const struct ubi_device *ubi) > > { > > if (ubi->dbg.emulate_bitflips) > > - return !(prandom_u32() % 200); > > + return !(prandom_u32_max(200)); > > return 0; > > } > > > > Because some looks automated (why the parens?) > > > @@ -393,14 +387,11 @@ static struct test_driver { > > > > static void shuffle_array(int *arr, int n) > > { > > - unsigned int rnd; > > int i, j; > > > > for (i = n - 1; i > 0; i--) { > > - rnd = prandom_u32(); > > - > > /* Cut the range. */ > > - j = rnd % i; > > + j = prandom_u32_max(i); > > > > /* Swap indexes. */ > > swap(arr[i], arr[j]); > > And some by hand. :) > > Reviewed-by: Kees Cook Thanks! Reviewed-by: KP Singh > > -- > Kees Cook