Received: by 2002:a6b:fb09:0:0:0:0:0 with SMTP id h9csp2933652iog; Mon, 20 Jun 2022 07:47:07 -0700 (PDT) X-Google-Smtp-Source: AGRyM1uE+Wv3xKcp8IvEIm0/cvDKQUcO+yIRo41l9ZqaJCVOlJ6IVb/iW6hBstoIuumVnwQy7r5Y X-Received: by 2002:a17:902:b193:b0:16a:2c23:37d0 with SMTP id s19-20020a170902b19300b0016a2c2337d0mr3125736plr.35.1655736427341; Mon, 20 Jun 2022 07:47:07 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1655736427; cv=none; d=google.com; s=arc-20160816; b=jNNGtY65wOLyv5YlqiL/hAiTfa9EUCAi4uTZ8jNUYAVs2s4g9cRqiasZkQMH5ONGT5 JwARFLIMoD6el6xfKZy5wM+qnxlSJXd6dZuphPnGtUkB9ektGDxl/9+/qvsQRpMEv8V+ I8SrNoMGVFEv1knDn1hqwBa2VPQSAu/6e14+sTQPZPdzUs5mzkrno5fx/q1D0jc/3nCC eLSLRrC/kAzaOtSnRELXjPXn4OfbUW6bzLBCszwsUiXp+pCN/o6m1b34K+w4Ol063Go6 0fG756cqynJLfbvqymLdeZzyMZkqTRz+/7GUSVmhnxoh1BjQs4QGhAm3bB5ITnZNmUCY rueg== 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=Zhvl4uHxs5sh/e9RNYYRlE/9SxMxLbgOGTis/xFbdAJmujlp0ZW1+0vVOYeoItjcIs wscEkpb+kJWF0KMH2Uc6iZNbJZJfVLAsBHbt4VCUKsoKhAf40tu8L4c7x901kg2vP6So qF0P8dQCp6lPDaFubcIP1dGhtpxr28uwYdy0X7s52zVl1bAicCsez0O5n0/Sko/9cGLd 6OezMK7cLmK5gQufDvfBBBZzbUkCz2a0KxcB+t0ipdI1pebZdPevnRzV7SAn5WemBGmt 8wzJqeHwJ2X7PhXCBqcMF/ziXrMZybKH5uVXs8Pt6OcWKW5AfXFdn/fQtapWao9F4zSA 3DSw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=q0FhOVP4; 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 h6-20020a170902f70600b001619fb0b872si11952339plo.618.2022.06.20.07.46.54; Mon, 20 Jun 2022 07:47: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=q0FhOVP4; 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 S1347160AbiFTNh5 (ORCPT + 99 others); Mon, 20 Jun 2022 09:37:57 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:39780 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1346645AbiFTNg5 (ORCPT ); Mon, 20 Jun 2022 09:36:57 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id AF72B193E0; Mon, 20 Jun 2022 06:14:00 -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 dfw.source.kernel.org (Postfix) with ESMTPS id 2683F60ED5; Mon, 20 Jun 2022 13:13:32 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 36428C3411C; Mon, 20 Jun 2022 13:13:31 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1655730811; bh=RG6VjW3XhSd7agEEKPH/P8cAoVf6or+rlbFXWJgkVVg=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=q0FhOVP4nshTmvZ5jx2uzbM2q1TNcnwwtX9ZCsYNC4arWmLqDdgkrogI4vRMEgQIj fDKiqlJlyx2WoXrFMNu++/T8H3bLPjUOdqYV0Hp0VsUJ79LMw1Jp8jzRko1hkSEHkj G/sfFgKS2pBCZ+QCzW56qSMm+tT2+6rIyNrDK/MY= 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 5.4 061/240] random: simplify arithmetic function flow in account() Date: Mon, 20 Jun 2022 14:49:22 +0200 Message-Id: <20220620124740.087011903@linuxfoundation.org> X-Mailer: git-send-email 2.36.1 In-Reply-To: <20220620124737.799371052@linuxfoundation.org> References: <20220620124737.799371052@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;