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 149E2C43219 for ; Thu, 17 Nov 2022 10:33:09 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S239468AbiKQKdG (ORCPT ); Thu, 17 Nov 2022 05:33:06 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:60482 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229931AbiKQKdE (ORCPT ); Thu, 17 Nov 2022 05:33:04 -0500 Received: from pandora.armlinux.org.uk (pandora.armlinux.org.uk [IPv6:2001:4d48:ad52:32c8:5054:ff:fe00:142]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id A1C6259841; Thu, 17 Nov 2022 02:33:01 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=armlinux.org.uk; s=pandora-2019; h=Sender:In-Reply-To:Content-Type: MIME-Version:References:Message-ID:Subject:Cc:To:From:Date: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=2BgNwLdh+HBA4xrzyykRGd8e3nHfE6jHoZjPyIO7tQ8=; b=vf0GDy8lKzIs95KZT9hTDN9bLB KkkakBOovK/EhU72ZwXcJFd6ZXnYeh5hWZFAO+QwAUy+Iil38BRQKWQA/ovqtD2R3k/zIRWUVleNY mI6EufB0EHT9QlEKH1MxQI8dlYgzNxWxQ/KJJ0HUSUpP7NgTBBgzbRiK7voh4pixtxFai3XdS+4CH KUkR3sQ10UY1J1DLrBw2+xk4ToBWEQq3GT1Ou599VPaU5ywBw9TAIVKjU75NdNZTSPprpMUx5CQP1 Y51nu+1iINv9JXk8aPYDqbEhMoPEahldPraSq7QM6Racs8sRYiTunUTXpT5cPjddHYjb4UQdEZEgF MxQo2C5A==; Received: from shell.armlinux.org.uk ([fd8f:7570:feb6:1:5054:ff:fe00:4ec]:35306) by pandora.armlinux.org.uk with esmtpsa (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1ovcCQ-0004Iz-Pt; Thu, 17 Nov 2022 10:32:50 +0000 Received: from linux by shell.armlinux.org.uk with local (Exim 4.94.2) (envelope-from ) id 1ovcCG-0006jE-Em; Thu, 17 Nov 2022 10:32:40 +0000 Date: Thu, 17 Nov 2022 10:32:40 +0000 From: "Russell King (Oracle)" To: "Jason A. Donenfeld" Cc: Kees Cook , linux-kernel@vger.kernel.org, patches@lists.linux.dev, Greg Kroah-Hartman , Jakub Kicinski , Catalin Marinas , Thomas Bogendoerfer , Heiko Carstens , Herbert Xu , Christoph =?iso-8859-1?Q?B=F6hmwalder?= , 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=us-ascii Content-Disposition: inline In-Reply-To: Sender: Russell King (Oracle) Precedence: bulk List-ID: X-Mailing-List: linux-media@vger.kernel.org On Thu, Nov 17, 2022 at 03:05:14AM +0100, Jason A. Donenfeld wrote: > On Thu, Nov 17, 2022 at 12:55:47AM +0100, Jason A. Donenfeld wrote: > > 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. > > I think I can make progress with (1) alone by fiddling around with > > godbolt enough, like usual. > > The code gen is definitely worse. > > Original half-open interval: > > return floor + get_random_u32_below(ceil - floor); > > Suggested fully closed interval: > > ceil = ceil - floor + 1; > return likely(ceil) ? floor + get_random_u32_below(ceil) : get_random_u32(); How many of these uses are going to have ceil and floor as a variable? If they're constants (e.g. due to being in an inline function with constant arguments) then the compiler will optimise all of the above and the assembly code will just be either: 1. a call to get_random_u32() 2. a call to get_random_u32_below() and an addition. If one passes ceil or floor as a variable, then yes, the code gen is going to be as complicated as you suggest above. -- RMK's Patch system: https://www.armlinux.org.uk/developer/patches/ FTTP is here! 40Mbps down 10Mbps up. Decent connectivity at last!