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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id C2DB4C4332F for ; Wed, 16 Nov 2022 23:56:04 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233438AbiKPX4C (ORCPT ); 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> Precedence: bulk List-ID: X-Mailing-List: linux-block@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 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 0D84CC4332F for ; Wed, 16 Nov 2022 23:57:01 +0000 (UTC) Received: from boromir.ozlabs.org (localhost [IPv6:::1]) by lists.ozlabs.org (Postfix) with ESMTP id 4NCKhr1P8gz3dtx for ; Thu, 17 Nov 2022 10:57:00 +1100 (AEDT) Authentication-Results: lists.ozlabs.org; dkim=fail reason="signature verification failed" (1024-bit key; unprotected) header.d=zx2c4.com header.i=@zx2c4.com header.a=rsa-sha256 header.s=20210105 header.b=goQ4eWiA; dkim-atps=neutral Authentication-Results: lists.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=kernel.org (client-ip=139.178.84.217; helo=dfw.source.kernel.org; envelope-from=srs0=7/5/=3q=zx2c4.com=jason@kernel.org; receiver=) Authentication-Results: lists.ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=zx2c4.com header.i=@zx2c4.com header.a=rsa-sha256 header.s=20210105 header.b=goQ4eWiA; dkim-atps=neutral Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) (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 4NCKgk69n0z3bm9 for ; Thu, 17 Nov 2022 10:56:02 +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 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 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-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: "Darrick J . Wong" , patches@lists.linux.dev, netdev@vger.kernel.org, Andreas Dilger , ydroneaud@opteya.com, Herbert Xu , Richard Weinberger , Helge Deller , Russell King , Jason Gunthorpe , Catalin Marinas , Jakub Kicinski , linux-mips@vger.kernel.org, linux-media@vger.kernel.org, Heiko Carstens , Jani Nikula , linux-block@vger.kernel.org, SeongJae Park , loongarch@lists.linux.dev, Jaegeuk Kim , Thomas Gleixner , linux-arm-kernel@lists.infradead.org, Thomas Bogendoerfer , Theodore Ts'o , linux-parisc@vger.kernel.org, "Martin K . Petersen" , Greg Kroah-Hartman , linux-mmc@vger.kernel.org, linux-kerne l@vger.kernel.org, Christoph =?utf-8?Q?B=C3=B6hmwalder?= , linux-crypto@vger.kernel.org, Sakari Ailus , linux-fsdevel@vger.kernel.org, Andrew Morton , linuxppc-dev@lists.ozlabs.org Errors-To: linuxppc-dev-bounces+linuxppc-dev=archiver.kernel.org@lists.ozlabs.org Sender: "Linuxppc-dev" 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 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