Received: by 2002:a6b:fb09:0:0:0:0:0 with SMTP id h9csp6030864iog; Thu, 23 Jun 2022 09:55:39 -0700 (PDT) X-Google-Smtp-Source: AGRyM1uQLTkPtiZegdnahfBAvmBNHUE81kGJydPLBeg/z1Dv27Dj4x3A1f5YH/cn4MWygCIoChaa X-Received: by 2002:a17:906:64d0:b0:722:e8c6:9169 with SMTP id p16-20020a17090664d000b00722e8c69169mr9509995ejn.206.1656003339387; Thu, 23 Jun 2022 09:55:39 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1656003339; cv=none; d=google.com; s=arc-20160816; b=h0T6QRV7Trkz/Quf4aX02mmqXMoPNcWzspEBArAT1KZVnAnvrxKSUp/lqvCVS6ej1n MWbbsFjs0Dh0rqEylTP1fnKdaMqydQh4Qwoq70MArSLSLdzxzBW3z5kgPWWsy/uQ7tEw 0+IzRM6fqMQDabQ0ZD/ssTzUTv9A/dFth+Gg2jHb/nCHzOa7UojfvlFbWix6/bkEnnpM VuMHnQ7FNoWTsey4b1vwFqK6kQhafSAp3aYuQfnThE30YlglQUYm/tEdXvIzY3NVuvEG g4DT/yBYuBHIN23WNKDzihHTUKN52aw9xDs732GT91eA2MLxXecq0TxbZVQMdsG9302s kb9g== 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=oOn/D5TBaFcIdicbB36BwMYa0VYoUH48AhEySK6aBrE=; b=kwmJ6xVRzYyGgWeQkrtj0oeLUSai3fYL4IuYGqJ3QDML2JmDfXPCq21HtJyUmVUYwm XNHRAcW1BX7Iwl0JvSEfvvdtPpif7t2Um5D15ASJ8oeLiKZCn3oKY4MsVzrHHXIDBuHB fD0FRmdhvDMWv7As3pYHde1keAu2Bk9tVwTpcbb66/+2DA9X4kKhKzTa8uZv0hnYcgwX gHRgGuoF/X08RU8w8Gw/RetzNHJm09I03K66O65dPJIdBP2vyUvYrTcrqcxlW2okIKDV H3FvlfCjqcf4fYrsr/vybRYi4u/2lgyxGzLaIUToHHTaVJ2/Jy5f/YcWP+mKBdN2wUEe W5dA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=y4m2HKle; 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 nb22-20020a1709071c9600b00717917b7d9csi9516808ejc.438.2022.06.23.09.55.14; Thu, 23 Jun 2022 09:55:39 -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=y4m2HKle; 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 S232118AbiFWQwi (ORCPT + 99 others); Thu, 23 Jun 2022 12:52:38 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:49014 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233879AbiFWQvs (ORCPT ); Thu, 23 Jun 2022 12:51:48 -0400 Received: from ams.source.kernel.org (ams.source.kernel.org [IPv6:2604:1380:4601:e00::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id CDC22FD11; Thu, 23 Jun 2022 09:50:31 -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 89A33B82493; Thu, 23 Jun 2022 16:50:30 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id E33A5C341C5; Thu, 23 Jun 2022 16:50:28 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1656003029; bh=x9iWFYc6ywzpL8ONHopaPPY7f/VQ7Xo4jKXCi7xFPqw=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=y4m2HKleWBRVCEkj5MAJWOepIpjO1xVera/PWHeFMrNwJfmsZFyc+zj9qXXLW5n9L yN4wslkWc7NxZ22H5wg4vzXYLNo1pGUaYrih5nncrjO9v0pDmK3iUbPW5vPqbVOy3+ 4iX1XI2D/EPgBirDfyUMtn8cY6Fq/B5b2H0mRCgk= 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.9 106/264] random: simplify arithmetic function flow in account() Date: Thu, 23 Jun 2022 18:41:39 +0200 Message-Id: <20220623164347.069392524@linuxfoundation.org> X-Mailer: git-send-email 2.36.1 In-Reply-To: <20220623164344.053938039@linuxfoundation.org> References: <20220623164344.053938039@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 @@ -1344,7 +1344,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); @@ -1352,20 +1352,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;