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 B80E5C433FE for ; Wed, 16 Nov 2022 23:57:16 +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:In-Reply-To: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=EuQetUqDaxVtQPnvrumbjCv7XgvizSx71nH/AnQTme8=; b=seTGdsLPvOnjbi ++QYHYDN3uYtI4YgsyYXE6H8v65T+LhHzmuHlBPT+uDMfp46rPiZZxW0MfAZ1FHHg1gDwb9el9Dqw C3Hgxxe1holXhQN/73Bs0GzABObfSMuCsOGZvP/r45tr9v8jrHeOkkVsigknOpzQ+dLSut4zua8gH XW2cAjyul8TIfmrmd3GINjt+q44b5Fr8Z3m1dcy+xVoYPf918Qk7OZTcUlEqoUIxZXQ1aq+HFOShL zYXh6v7SkVdhEWO+VK9nTMKFGUXdozEJrTMlV7ikmsK1Al+lYTfYoUADAR/S9DmhVjIiBDcQFia+8 2FS0bfccRknSA04r1Sow==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.94.2 #2 (Red Hat Linux)) id 1ovSGB-008mcR-VY; Wed, 16 Nov 2022 23:56:04 +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 1ovSG8-008mbf-39 for linux-arm-kernel@lists.infradead.org; Wed, 16 Nov 2022 23:56:01 +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 83BF56204E; 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-Disposition: inline In-Reply-To: <202211161436.A45AD719A@keescook> X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20221116_155600_445783_963B8A7F X-CRM114-Status: GOOD ( 20.34 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.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 _______________________________________________ linux-arm-kernel mailing list linux-arm-kernel@lists.infradead.org http://lists.infradead.org/mailman/listinfo/linux-arm-kernel