Received: by 2002:a05:6358:d09b:b0:dc:cd0c:909e with SMTP id jc27csp1826602rwb; Thu, 17 Nov 2022 02:35:16 -0800 (PST) X-Google-Smtp-Source: AA0mqf6aiEsdYCPqXStWw4B42bxZaGLv4iAh36lmxymSoX/0XZzEgSJzDOxtWgaRlgn9k7WitQ4v X-Received: by 2002:a17:906:844:b0:78c:2c03:804c with SMTP id f4-20020a170906084400b0078c2c03804cmr1675740ejd.107.1668681315878; Thu, 17 Nov 2022 02:35:15 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1668681315; cv=none; d=google.com; s=arc-20160816; b=HzVRQ92XmOiuDpu2AW9aRcbrG0anD0vjcTNTfB1XpgxEm1pbrgJ15vCw3gzKBni1tS HU8+CX5HI9QE9wegTmM/7KYe9eFYkL3QWTehSoDX2nxODjlg44R2ma60ZY1NdMREXSsd 7A9sGo6kFmchh20lrQC0z7ceU12GjboKIkLHBZsqN8EFcLDHrVkxJi4sf9a8rI2evfRq X4FLaftyOltMOZxsSQxBsDpSObMOKunhXGFVF3wv3jSjF2ipD/r0ULwUiiiWhAwx2LOn +v1g46KCeXNO8sebXFfQ2UAMupEMpVdYrFP1rjG8V/YzOfakluc/SzntGcp0/qT2C3Wz YnEw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:in-reply-to:content-disposition :mime-version:references:message-id:subject:cc:to:from:date :dkim-signature; bh=2BgNwLdh+HBA4xrzyykRGd8e3nHfE6jHoZjPyIO7tQ8=; b=K62hAin/vcON+G4luy/wP61uVGg2OXA5OPXYx+kZZDdD4J7tm1Rhu0Y50DHO/qoJGB UNhCEuBvPKSkFKXeb80F0Wvqva6hp2yziA3UTCV1S6asKGWwk1hT1a8jryb/ngSzFFgb OsYes8V9vyUFe8Ts8ugtuu84xWwMNrX8aBrJmg2Y87RoMRjQAyUsMQO0GMhe8kmsZGLN ptyzlC6RwhILtg/blCj2/RBDCXyb/0SQmG1CBQJ9vMs1SkiLOgp+NIkxUCG8YyPrDPzz IVxYh24dWlVST6fK4CP+43P3d3L70bQviJOpe0aCGpQ1S+vhQckPLk6vusNAIkj4YxwR uZlg== ARC-Authentication-Results: i=1; mx.google.com; dkim=fail (test mode) header.i=@armlinux.org.uk header.s=pandora-2019 header.b=vf0GDy8l; 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=fail (p=NONE sp=NONE dis=NONE) header.from=armlinux.org.uk Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id w2-20020a056402268200b00468eee7250csi505284edd.510.2022.11.17.02.34.42; Thu, 17 Nov 2022 02:35:15 -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=fail (test mode) header.i=@armlinux.org.uk header.s=pandora-2019 header.b=vf0GDy8l; 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=fail (p=NONE sp=NONE dis=NONE) header.from=armlinux.org.uk Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234768AbiKQKdF (ORCPT + 99 others); Thu, 17 Nov 2022 05:33:05 -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) X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,SPF_HELO_NONE,SPF_NONE 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 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!