From nobody Tue Jun 30 11:58:17 2026 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 75477C433EF for ; Mon, 17 Jan 2022 17:52:55 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S241871AbiAQRwy (ORCPT ); Mon, 17 Jan 2022 12:52:54 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:57950 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233445AbiAQRwx (ORCPT ); Mon, 17 Jan 2022 12:52:53 -0500 Received: from ams.source.kernel.org (ams.source.kernel.org [IPv6:2604:1380:4601:e00::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 3BAF5C061574 for ; Mon, 17 Jan 2022 09:52:53 -0800 (PST) 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 0FE7BB80F00 for ; Mon, 17 Jan 2022 17:52:52 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 7A2D3C36AE3; Mon, 17 Jan 2022 17:52:50 +0000 (UTC) Authentication-Results: smtp.kernel.org; dkim=pass (1024-bit key) header.d=zx2c4.com header.i=@zx2c4.com header.b="Qz4RErlu" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=zx2c4.com; s=20210105; t=1642441968; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=SR77sVBJKfk/enxOsWX3Wu8wGdXDiwHLJ+nNzLvRppY=; b=Qz4RErlu17jmLsEsoT3sgMJ0fbY4w0qRPvCCU5ko7oohvLb1KKb0Gcu5pJcCPZWete8wF+ gi02PYUjfAQT8Z+NUEwvyJbSkgkKdP1UoEu9AOnCa4ztcyCRdUidgKI50KidhKQrYHqBFW zfhxx+ln8ECTseOpwlnFlzui4lBVdTU= Received: by mail.zx2c4.com (ZX2C4 Mail Server) with ESMTPSA id 937c7668 (TLSv1.3:AEAD-AES256-GCM-SHA384:256:NO); Mon, 17 Jan 2022 17:52:48 +0000 (UTC) From: "Jason A. Donenfeld" To: linux-kernel@vger.kernel.org Cc: "Jason A. Donenfeld" , Dominik Brodowski Subject: [PATCH] random: simplify arithmetic function flow in account() Date: Mon, 17 Jan 2022 18:52:37 +0100 Message-Id: <20220117175237.361518-1-Jason@zx2c4.com> In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" 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 --- drivers/char/random.c | 19 +++++++------------ 1 file changed, 7 insertions(+), 12 deletions(-) diff --git a/drivers/char/random.c b/drivers/char/random.c index a1720a48f583..ecb45be9535f 100644 --- a/drivers/char/random.c +++ b/drivers/char/random.c @@ -1291,9 +1291,9 @@ EXPORT_SYMBOL_GPL(add_disk_randomness); * This function decides how many bytes to actually take from the * given pool, and also debits the entropy count accordingly. */ -static size_t account(size_t nbytes, int min) +static noinline size_t account(size_t nbytes, int min) { - int entropy_count, orig, have_bytes; + int entropy_count, orig; size_t ibytes, nfrac; =20 BUG_ON(input_pool.entropy_count > POOL_FRACBITS); @@ -1301,20 +1301,15 @@ static size_t account(size_t nbytes, int min) /* Can we pull enough? */ retry: entropy_count =3D orig =3D READ_ONCE(input_pool.entropy_count); - ibytes =3D nbytes; - /* never pull more than available */ - have_bytes =3D entropy_count >> (POOL_ENTROPY_SHIFT + 3); - - if (have_bytes < 0) - have_bytes =3D 0; - ibytes =3D min_t(size_t, ibytes, have_bytes); - if (ibytes < min) - ibytes =3D 0; - if (WARN_ON(entropy_count < 0)) { pr_warn("negative entropy count: count %d\n", entropy_count); entropy_count =3D 0; } + + /* never pull more than available */ + ibytes =3D min_t(size_t, nbytes, entropy_count >> (POOL_ENTROPY_SHIFT + 3= )); + if (ibytes < min) + ibytes =3D 0; nfrac =3D ibytes << (POOL_ENTROPY_SHIFT + 3); if ((size_t)entropy_count > nfrac) entropy_count -=3D nfrac; --=20 2.34.1