Received: by 2002:a6b:fb09:0:0:0:0:0 with SMTP id h9csp6128698iog; Thu, 23 Jun 2022 11:58:07 -0700 (PDT) X-Google-Smtp-Source: AGRyM1s/EszbxkRFPIMx1om/wWqxQKhmroDatwqUH8b7jOQAfn4RryvL1l0dA8pTlXPVQZ7FYMiY X-Received: by 2002:aa7:c9c9:0:b0:431:962f:f61e with SMTP id i9-20020aa7c9c9000000b00431962ff61emr12704709edt.189.1656010687086; Thu, 23 Jun 2022 11:58:07 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1656010687; cv=none; d=google.com; s=arc-20160816; b=LK26k7QTPZ6wOyz3mNLgTB+JVkB589/0GxkAtLdqQtDKwX5AjALiOyc4pl/u7xREKN ySIyRt6E/8mgCnYUZCiD+sdBSuAQsRqOf9Jw+ujrvBsuX9TqiWjAeTn5eV75ravR4Ui7 +bhIb0M3xuZswGqtRqenl0z0xOZ5HLofUIMwnDnCkbVW1+nyv7+4slxFlO5pIw350VPo 2gJx/u/KDy+oUhBlbLeHtK+bCB82jyc4765pO3MYMlPSlOa19h8bgG60yIV17VhaYS5B bI1C6SLV6/KDNT0Ej7tlYlqGLttpbnqc8uR0KGooLzRrgahPO33EBiKlxRFkiefejG3F NmDA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=uCoDZE01mPPrdwUvmJuJbacE/z8nr4jQGekZKRxb3v0=; b=aWI1hCpcHYTwCOUuXH2034lfNr4/MiumaSA73/f6cQDlKB1KxpJjZ0yqnYJnQ7LoSY IRsKzfW+WugtG/Xshm0D58bPRLsnI4VnxgaoC4+TfHMnYO5KVlZrJQBDgrkB/6vKlYLk cc6Ow6kNVSEEVNUZGvoMt5F4y4rtvLjO2/CAKTjV57F+ZyzeYBBeMeTLgoKfl45D25xl GihTX99aqkgx2IwCEB5MSXYKn4vm3Y6I56A3SJ7Qr0DtsV/6Cy5BKaN87/jioSGRDGV8 Kidw89q4SENggJXGLC/GU+w0LLqUwrs/l9OzDO/wMtTNyJeWwPlAnKO2We1gg4fpULZe xJDg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=QIQ9wMxC; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id ej27-20020a056402369b00b00434fff6fe37si405807edb.227.2022.06.23.11.57.42; Thu, 23 Jun 2022 11:58:07 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-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=@linuxfoundation.org header.s=korg header.b=QIQ9wMxC; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233871AbiFWR7n (ORCPT + 99 others); Thu, 23 Jun 2022 13:59:43 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:58620 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235871AbiFWR5r (ORCPT ); Thu, 23 Jun 2022 13:57:47 -0400 Received: from ams.source.kernel.org (ams.source.kernel.org [145.40.68.75]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id AD6136F4A9; Thu, 23 Jun 2022 10:15:49 -0700 (PDT) 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 ams.source.kernel.org (Postfix) with ESMTPS id 93602B82498; Thu, 23 Jun 2022 17:15:48 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 0B93BC3411B; Thu, 23 Jun 2022 17:15:46 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1656004547; bh=RG6VjW3XhSd7agEEKPH/P8cAoVf6or+rlbFXWJgkVVg=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=QIQ9wMxCDIQHuz/SFH2R19SJn0Rsg60k6mP7qxrxveaw7jbnq4NUTBEE2Xp7snuec Hlv4ggGYeYT5gT79CtXILGGnU34zyuLI/DGw3/53MZu1T22PnfHwuh13Ihyo15r0cD MHdru/BW+k3WC9vI/I+wo8/IdLMOUkEDZFFa1z9s= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Dominik Brodowski , "Jason A. Donenfeld" Subject: [PATCH 4.19 071/234] random: simplify arithmetic function flow in account() Date: Thu, 23 Jun 2022 18:42:18 +0200 Message-Id: <20220623164345.070066155@linuxfoundation.org> X-Mailer: git-send-email 2.36.1 In-Reply-To: <20220623164343.042598055@linuxfoundation.org> References: <20220623164343.042598055@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-7.7 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_HI, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE 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-kernel@vger.kernel.org From: "Jason A. Donenfeld" commit a254a0e4093fce8c832414a83940736067eed515 upstream. Now that have_bytes is never modified, we can simplify this function. First, we move the check for negative entropy_count to be first. That ensures that subsequent reads of this will be non-negative. Then, have_bytes and ibytes can be folded into their one use site in the min_t() function. Suggested-by: Dominik Brodowski Reviewed-by: Dominik Brodowski Signed-off-by: Jason A. Donenfeld Signed-off-by: Greg Kroah-Hartman --- drivers/char/random.c | 17 ++++++----------- 1 file changed, 6 insertions(+), 11 deletions(-) --- a/drivers/char/random.c +++ b/drivers/char/random.c @@ -1293,7 +1293,7 @@ EXPORT_SYMBOL_GPL(add_disk_randomness); */ static size_t account(size_t nbytes, int min) { - int entropy_count, orig, have_bytes; + int entropy_count, orig; size_t ibytes, nfrac; BUG_ON(input_pool.entropy_count > POOL_FRACBITS); @@ -1301,20 +1301,15 @@ static size_t account(size_t nbytes, int /* Can we pull enough? */ retry: entropy_count = orig = READ_ONCE(input_pool.entropy_count); - ibytes = nbytes; - /* never pull more than available */ - have_bytes = entropy_count >> (POOL_ENTROPY_SHIFT + 3); - - if (have_bytes < 0) - have_bytes = 0; - ibytes = min_t(size_t, ibytes, have_bytes); - if (ibytes < min) - ibytes = 0; - if (WARN_ON(entropy_count < 0)) { pr_warn("negative entropy count: count %d\n", entropy_count); entropy_count = 0; } + + /* never pull more than available */ + ibytes = min_t(size_t, nbytes, entropy_count >> (POOL_ENTROPY_SHIFT + 3)); + if (ibytes < min) + ibytes = 0; nfrac = ibytes << (POOL_ENTROPY_SHIFT + 3); if ((size_t)entropy_count > nfrac) entropy_count -= nfrac;