Received: by 2002:a05:6358:d09b:b0:dc:cd0c:909e with SMTP id jc27csp1326258rwb; Wed, 16 Nov 2022 15:59:24 -0800 (PST) X-Google-Smtp-Source: AA0mqf7w1QyRX/WvOEdNQBkR3/mEDOkiSz12MXCqUvBUxEvN++Of0pI7VIEbzMsHTswdNBJsj3Yb X-Received: by 2002:a17:902:a503:b0:188:51c0:705 with SMTP id s3-20020a170902a50300b0018851c00705mr255873plq.62.1668643164108; Wed, 16 Nov 2022 15:59:24 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1668643164; cv=none; d=google.com; s=arc-20160816; b=EzdUeeebd0bF08D7xKrirPtMgJRD90YeRfBPgPrjLsGdXld3r371pxJrEICYqIyR+2 doQlvdKmBbBbwjmqiMgW9hQYJpyL94ATFZeBsppWoH+pOoPiUepymbshL/h7YeaOL7DM jKaMRfai1zE+ODX15kL7CipjVSXH6xSCS2uRraeqnQy73tzyH6TN4hpXc8aXhvQhBYXo mQqZe+0Zm5p7KTP7v3ycv0PUWA6qRPF0SlcN8F9HURQIpeA6SW1o186WezF43nuk5Yej dtolWLogg7KX3/e05/INylc0FKnghf9z7MTeeG5I6i5CmLnYiJu+RfaQVukPAxavLVxd RTJg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:in-reply-to:content-disposition:mime-version :references:message-id:subject:cc:to:from:date:dkim-signature; bh=sM9LBQZ376u5w/8PZH+YQVlUaxTNIyanmkjPT3FYfMU=; b=TrXIc9WZg6IDBW8NBj62hlju5cubwSeSSOlaJLjEbOV1RrDlgCHrgnG2QUZX3Ql6OQ ProxchiE/NEjJaSnUiLUyy7cKlPfLvzp/OS9TlBziIp+2rWstY2uvgbBV0zXhL8OysZk gRjperD3Ln1kj8W8LkuflxeBqNyoRMHY8emPM7ZcSet+74s9ol4mQ/h7VWXy+3AO6spu VqVfvDELXJ3DKxN7EyTD+WbSmjwTdO6JZs78wjvlYt7vQJ1Qproo4o5MuLA/NXXNZ5GZ CvF8m83rbx901z0vUuxmFN4eVv2pLecKFOb5RfbsnjA8qFEhI89iebRj7ApQYcoatr0s hReg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@zx2c4.com header.s=20210105 header.b=goQ4eWiA; spf=pass (google.com: domain of linux-crypto-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-crypto-owner@vger.kernel.org; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=zx2c4.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id t64-20020a638143000000b0047071cc77e9si3115984pgd.534.2022.11.16.15.59.06; Wed, 16 Nov 2022 15:59:24 -0800 (PST) Received-SPF: pass (google.com: domain of linux-crypto-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@zx2c4.com header.s=20210105 header.b=goQ4eWiA; spf=pass (google.com: domain of linux-crypto-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-crypto-owner@vger.kernel.org; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=zx2c4.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233980AbiKPX4C (ORCPT + 99 others); Wed, 16 Nov 2022 18:56:02 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:44636 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231871AbiKPX4B (ORCPT ); Wed, 16 Nov 2022 18:56:01 -0500 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 61175657E5; Wed, 16 Nov 2022 15:56:00 -0800 (PST) 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 F15466205D; Wed, 16 Nov 2022 23:55:59 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id AABDBC433D6; Wed, 16 Nov 2022 23:55:55 +0000 (UTC) Authentication-Results: smtp.kernel.org; dkim=pass (1024-bit key) header.d=zx2c4.com header.i=@zx2c4.com header.b="goQ4eWiA" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=zx2c4.com; s=20210105; t=1668642952; 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=sM9LBQZ376u5w/8PZH+YQVlUaxTNIyanmkjPT3FYfMU=; b=goQ4eWiAvkZqWkfss8gq1ex5AtlA/ahfxEBk6r2fheEdHDzxQqDvdaI7jHnRtKQZacSknN xALKJahBGIU2w4KpYkodsz6zSfQUgQjwtn/9XiLQzmbduxm8SAGcIyMTbNpEffPJ6eVtj8 fknlTQXlnEW5l9AjbJbPQ6U48mWYP8U= Received: by mail.zx2c4.com (ZX2C4 Mail Server) with ESMTPSA id 48c033fa (TLSv1.3:TLS_AES_256_GCM_SHA384:256:NO); Wed, 16 Nov 2022 23:55:51 +0000 (UTC) Date: Thu, 17 Nov 2022 00:55:47 +0100 From: "Jason A. Donenfeld" To: Kees Cook Cc: linux-kernel@vger.kernel.org, patches@lists.linux.dev, Greg Kroah-Hartman , Jakub Kicinski , Russell King , Catalin Marinas , Thomas Bogendoerfer , Heiko Carstens , Herbert Xu , Christoph =?utf-8?Q?B=C3=B6hmwalder?= , Jani Nikula , Jason Gunthorpe , Sakari Ailus , "Martin K . Petersen" , Theodore Ts'o , Andreas Dilger , Jaegeuk Kim , Richard Weinberger , "Darrick J . Wong" , SeongJae Park , Thomas Gleixner , Andrew Morton , Michael Ellerman , Helge Deller , netdev@vger.kernel.org, linux-crypto@vger.kernel.org, linux-block@vger.kernel.org, linux-fsdevel@vger.kernel.org, linux-media@vger.kernel.org, linux-arm-kernel@lists.infradead.org, loongarch@lists.linux.dev, linux-mips@vger.kernel.org, linuxppc-dev@lists.ozlabs.org, linux-mmc@vger.kernel.org, linux-parisc@vger.kernel.org, ydroneaud@opteya.com Subject: Re: [PATCH v2 3/3] treewide: use get_random_u32_between() when possible Message-ID: References: <20221114164558.1180362-1-Jason@zx2c4.com> <20221114164558.1180362-4-Jason@zx2c4.com> <202211161436.A45AD719A@keescook> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline In-Reply-To: <202211161436.A45AD719A@keescook> X-Spam-Status: No, score=-6.8 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, RCVD_IN_DNSWL_HI,SPF_HELO_NONE,SPF_PASS autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-crypto@vger.kernel.org On Wed, Nov 16, 2022 at 02:43:13PM -0800, Kees Cook wrote: > On Mon, Nov 14, 2022 at 05:45:58PM +0100, Jason A. Donenfeld wrote: > > - (get_random_u32_below(1024) + 1) * PAGE_SIZE; > > + get_random_u32_between(1, 1024 + 1) * PAGE_SIZE; > > I really don't like "between". Can't this be named "inclusive" (and > avoid adding 1 everywhere, which seems ugly), or at least named > something less ambiguous? > > > - n = get_random_u32_below(100) + 1; > > + n = get_random_u32_between(1, 101); > > Because I find this much less readable. "Below 100" is clear: 0-99 > inclusive, plus 1, so 1-100 inclusive. "Between 1 and 101" is not obvious > to me to mean: 1-100 inclusive. > > These seem so much nicer: > get_random_u32_inclusive(1, 1024) > get_random_u32_inclusive(1, 100) Yann pointed out something similar -- the half-closed interval being confusing -- and while I was initially dismissive, I've warmed up to doing this fully closed after sending a diff of that: https://lore.kernel.org/lkml/Y3Qt8HiXj8giOnZy@zx2c4.com/ So okay, let's say that I'll implement the inclusive version instead. We now have two problems to solve: 1) How/whether to make f(0, UR2_MAX) safe, - without additional 64-bit arithmetic, - minimizing the number of branches. I have a few ideas I'll code golf for a bit. 2) What to call it: - between I still like, because it mirrors "I'm thinking of a number between 1 and 10 and..." that everybody knows, - inclusive I guess works, but it's not a preposition, - bikeshed color #3? I think I can make progress with (1) alone by fiddling around with godbolt enough, like usual. I could use some more ideas for (2) though. Jason