Received: by 2002:a6b:fb09:0:0:0:0:0 with SMTP id h9csp6135384iog; Thu, 23 Jun 2022 12:05:18 -0700 (PDT) X-Google-Smtp-Source: AGRyM1u2NlwLxk1ml7m3PLlIsdXf9Mn0L5Pcr9QwJDXMp0RuM4sV+w8T9geLUTk+MchI06eMtNNS X-Received: by 2002:a63:5304:0:b0:3db:2e57:6f34 with SMTP id h4-20020a635304000000b003db2e576f34mr8589193pgb.88.1656011118126; Thu, 23 Jun 2022 12:05:18 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1656011118; cv=none; d=google.com; s=arc-20160816; b=sl+aoa67KTUsLpUXiaaXBGgYKPEgfOwGnXuzqhxgwCAZ1oSiua4UvkyIpL0DElkfoN ARqmDxSKatf5vYOBWIg+YZotlZ6h+Aykfj2U3180zB4amP/ABrZUFTBetNqDiQcGBUaK FgHi0icln2WT+SKa2XTKMXfuh4/eqaBxHi+/r5/O1OS+FN/iB8AfymgOBudt4YOG+QxG eSF7Kpj49a1ekS61N+wqGzWbkfZi0xTHRO/0nl+6IvzRAlSbyxMMg67sT1L073G+kaEJ /Z/lys/CXdUrfZgL0iSimI2z+qXB+T9Sspl5JOz77VJpr9CH8VJKBXUT+zVM7IOwDvXj f09g== 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=96fnVUnOwoSeOpvNfXF+pcDUYmj8V3zINu5MJH+ROEw=; b=0AP+go0HQbDy2YaMYT9DHdw1+Ouqaf8s7aQs/h4xLag+xN+V3Z0eOiqZka1HyIAGEw UyXVdHCMI7WkOLCnUrDu0k7YbqTaPl69+IchgP+NVV3COgxmySe+3ZbBQL8HNAKNVdZo +RFp3KAHjFEwmAiNg617xccYE8DyKToYpo6ICGFf+DIGcAunFVJniKB4bZUGjLS2NMzL 6WbjXQTJR1iR7SLr1myeDI2V83QsyWuCM1HoQXUXk4U9rpKsK3LKteKNPQr4+7QWoWyP RkEA0JiCqp2GJ7/hsHgnDwu/uutmK/Q5/uNcf0plBXTATaEgA/dwh78ypLVfP8tuqDyI KE2A== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=dTCggIM6; 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 s18-20020aa78bd2000000b00525215ea340si6439pfd.70.2022.06.23.12.05.05; Thu, 23 Jun 2022 12:05:18 -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=dTCggIM6; 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 S233141AbiFWSBU (ORCPT + 99 others); Thu, 23 Jun 2022 14:01:20 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:54316 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235804AbiFWR5m (ORCPT ); Thu, 23 Jun 2022 13:57:42 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C82FE6F488; Thu, 23 Jun 2022 10:15:42 -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 59FBE61DE5; Thu, 23 Jun 2022 17:15:42 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 25E1DC341C5; Thu, 23 Jun 2022 17:15:40 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1656004541; bh=1gYXjOiQ/z90nRzb8U321OxJ4EeK2vYVDyO9CPVnJcY=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=dTCggIM6+Zm0+pYB6JSVvOWu40emA780F5P8kNKjxJlS/cp8ueF8rBua9diu2+Mqr NEdrREjj27vbPOCwLvO5sOyuDpBX4Cltgp3ZJSklyO8KXgU0vFKTtTYzAXV6k1aRs/ TDue3HPcQ8Nm/wpRbXS8yuLCNLHbKdhIM7vqlg44= 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 069/234] random: cleanup fractional entropy shift constants Date: Thu, 23 Jun 2022 18:42:16 +0200 Message-Id: <20220623164345.014453017@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 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 @@ -359,16 +359,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. @@ -425,8 +415,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, @@ -652,6 +647,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; @@ -687,13 +685,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)) {