Received: by 2002:a6b:fb09:0:0:0:0:0 with SMTP id h9csp6032238iog; Thu, 23 Jun 2022 09:57:46 -0700 (PDT) X-Google-Smtp-Source: AGRyM1ve6dcf1jW/wGZoRot/eANaKyYFEm4TjLZQYr6YmRWgmsAzQHZ4xV7TKiqVOaackGJ4HGkZ X-Received: by 2002:a17:906:77c9:b0:722:ea38:9979 with SMTP id m9-20020a17090677c900b00722ea389979mr9128156ejn.95.1656003466327; Thu, 23 Jun 2022 09:57:46 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1656003466; cv=none; d=google.com; s=arc-20160816; b=ZxpdDeK3n7Ml/Ss+Ysn2Q+HBes+5I1P8gXj/velxMPvhHQy9L1LY3W0xnhQ66oLvSU uNvIyg7Fjfkc2wGVjGAe6hjjryO3H5CUJu2gql+juMajLx/pjQGcK+FGx7hDGh+xiSyr tCtahPu5yiqPwmpNereUnuV3U1yuabdAw8qyM50Rpv6vqULRawY78h+UaPa4/p3QiR0z rfWh8EETDyVeBayZEqtc7ttmYTV57QlhF4GlXP64FgEPW8vlVEMdk8iGJFKh1qw/D7gn A5RHJWhvDoC7QMjG+Y/zqNI2GX+17VRwF9AwD04L6xtKSCdTgtUNVN+h4/b0/D7VGBvZ RBeA== 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=2TobyCax5GlFDdq1WKzpAk1XVCShRZg1y9FBodUDLB0=; b=SS8jzgZGAlIZf+c8PehXBy4nXUeihNHHwXuBAeKUL+4CIoM7vISYWNmJCWe1BMXb3M Ppd+706Da5ijXMQypcji26pQ0awPEBfkXARilG05ECGo84xCT6M9Msr7vYDA5dtCq1hQ C2Cm5zXPeOhqoQCJTX7Zw/fOSXnF1p1Aimn4o9hXKkA36+dOkxa2U08CEE5v70CHkj5P h+N6hFzA7zQ10oQYQHAJ7xEw6g87uZaNpLWgTn/6b347RGw9+TFvnlSF7775RH2efr2A EaJbZfmNUo+YM9MrcLkgGJ44/HpZIHYYPpyhP3NgXr1Kb54mExjfKTdIn0/7B+0/X4Ap 1jMA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=M6DGAINY; 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 30-20020a170906025e00b00722e163c698si3273897ejl.398.2022.06.23.09.57.20; Thu, 23 Jun 2022 09:57:46 -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=M6DGAINY; 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 S233232AbiFWQy4 (ORCPT + 99 others); Thu, 23 Jun 2022 12:54:56 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:51626 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233877AbiFWQvs (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 3B640EE07; Thu, 23 Jun 2022 09:50:26 -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 C56EAB82490; Thu, 23 Jun 2022 16:50:24 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 35202C3411B; Thu, 23 Jun 2022 16:50:23 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1656003023; bh=t1sQBqOkiTHsARiRLmonyqAeVDU+sFjt7joITwh4v3c=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=M6DGAINYazMIhrkiLph1dSJNWqNnltInZMloGYVK0aZSX4U0GRW5wkyaltfKk/k78 0qEnq0xKtxqM3QsE3gJTlUCVqnxczE3gVowzCMc6IQynj+SYoVOSYnbJuEG+ZQBdi4 6YKeSnmTx0EUyvzAwM11H+k2p9Ut8dzC1utEAcdY= 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 104/264] random: cleanup fractional entropy shift constants Date: Thu, 23 Jun 2022 18:41:37 +0200 Message-Id: <20220623164347.012838506@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 18263c4e8e62f7329f38f5eadc568751242ca89c upstream. The entropy estimator is calculated in terms of 1/8 bits, which means there are various constants where things are shifted by 3. Move these into our pool info enum with the other relevant constants. While we're at it, move an English assertion about sizes into a proper BUILD_BUG_ON so that the compiler can ensure this invariant. Reviewed-by: Dominik Brodowski Signed-off-by: Jason A. Donenfeld Signed-off-by: Greg Kroah-Hartman --- drivers/char/random.c | 28 +++++++++++++--------------- 1 file changed, 13 insertions(+), 15 deletions(-) --- a/drivers/char/random.c +++ b/drivers/char/random.c @@ -360,16 +360,6 @@ /* #define ADD_INTERRUPT_BENCH */ /* - * To allow fractional bits to be tracked, the entropy_count field is - * denominated in units of 1/8th bits. - * - * 2*(POOL_ENTROPY_SHIFT + poolbitshift) must <= 31, or the multiply in - * credit_entropy_bits() needs to be 64 bits wide. - */ -#define POOL_ENTROPY_SHIFT 3 -#define POOL_ENTROPY_BITS() (input_pool.entropy_count >> POOL_ENTROPY_SHIFT) - -/* * If the entropy count falls under this number of bits, then we * should wake up processes which are selecting or polling on write * access to /dev/random. @@ -426,8 +416,13 @@ enum poolinfo { POOL_WORDMASK = POOL_WORDS - 1, POOL_BYTES = POOL_WORDS * sizeof(u32), POOL_BITS = POOL_BYTES * 8, - POOL_BITSHIFT = ilog2(POOL_WORDS) + 5, - POOL_FRACBITS = POOL_WORDS << (POOL_ENTROPY_SHIFT + 5), + POOL_BITSHIFT = ilog2(POOL_BITS), + + /* To allow fractional bits to be tracked, the entropy_count field is + * denominated in units of 1/8th bits. */ + POOL_ENTROPY_SHIFT = 3, +#define POOL_ENTROPY_BITS() (input_pool.entropy_count >> POOL_ENTROPY_SHIFT) + POOL_FRACBITS = POOL_BITS << POOL_ENTROPY_SHIFT, /* x^128 + x^104 + x^76 + x^51 +x^25 + x + 1 */ POOL_TAP1 = 104, @@ -653,6 +648,9 @@ static void credit_entropy_bits(int nbit int entropy_count, entropy_bits, orig; int nfrac = nbits << POOL_ENTROPY_SHIFT; + /* Ensure that the multiplication can avoid being 64 bits wide. */ + BUILD_BUG_ON(2 * (POOL_ENTROPY_SHIFT + POOL_BITSHIFT) > 31); + if (!nbits) return; @@ -688,13 +686,13 @@ retry: /* The +2 corresponds to the /4 in the denominator */ do { - unsigned int anfrac = min(pnfrac, POOL_FRACBITS/2); + unsigned int anfrac = min(pnfrac, POOL_FRACBITS / 2); unsigned int add = - ((POOL_FRACBITS - entropy_count)*anfrac*3) >> s; + ((POOL_FRACBITS - entropy_count) * anfrac * 3) >> s; entropy_count += add; pnfrac -= anfrac; - } while (unlikely(entropy_count < POOL_FRACBITS-2 && pnfrac)); + } while (unlikely(entropy_count < POOL_FRACBITS - 2 && pnfrac)); } if (WARN_ON(entropy_count < 0)) {