Received: by 2002:a6b:fb09:0:0:0:0:0 with SMTP id h9csp6034116iog; Thu, 23 Jun 2022 10:00:13 -0700 (PDT) X-Google-Smtp-Source: AGRyM1tqG9bgewufUo1cOWbI4zLPYmj+amcbqoSuZTxiZBF5wXRFIPDxLx1JFjPFJ8TUCgAxm1TA X-Received: by 2002:a17:907:9606:b0:70a:e140:6329 with SMTP id gb6-20020a170907960600b0070ae1406329mr8995764ejc.471.1656003613381; Thu, 23 Jun 2022 10:00:13 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1656003613; cv=none; d=google.com; s=arc-20160816; b=wAo8viSau3ww3gv0ONtdT3SePKXbqx9gPzSmWIwv4/xVrhNwWzPpF+UGuYY8mFeOFT EcGwd40w5D2wy4qsR10zvhKIzvhjdMdDKCWF7j6kO9zt+SVfxGGxGVojGw0cBxS5yqii SHu7fFF2QPyd4phO5tdgHJVJmjKqumUb7l70THSASq9tlipFaagg5XRhY2qYO6R5iDhf XD0Zawf1Oljwio9MWXQo1HeWpfvv1Y49dIYHBiLfkt+SXzLkXYixZ+JPEnFO9LkKGpBR Jgp/Nv+t65ULV/u+N8R3sWhcfUzdA6cNdzly39eQ8S6ntI2MBxlZBCCIxvElLRZvLagX YtuQ== 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=oCZdkv4sBLhaEzSRngHfgQDLuvIhqp180wHYHuBLYXE=; b=HOMwjVgKnt+ggmyr6oZtQIY1w1UGyIpiOqR4ecTCRyYJ8ZSNF/kZgktjCwD+Vhs+cY lzmAp8/2WYdj0A7OF0l0xzhWNnjliSl8/cN2AdC8wkL+ML4kpe3rztgV3hx0qJ/1cBOy yXndAqYcj3HSYz+DMCc5TBu/3SY3VeNZSZw3tXt2wgdGQP3kRSW+dSDmtTaBugYHaK8u Mh40CNu/staN8nS2LaFHnNwzSd7FNNN3mzVelfafgkPWudJh7W0/Uypdzqmrgct8wCAf nx9OvoO6geiULR01Fwj2ekRd6bKVqcY3bzEg4cIYgPcOe3FBJb0BT01pmWCdSm6p5Xsq 1lhQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=WSD1k1No; 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 o3-20020a056402438300b004359a5259e5si42760edc.561.2022.06.23.09.59.44; Thu, 23 Jun 2022 10:00:13 -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=WSD1k1No; 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 S233195AbiFWQ5h (ORCPT + 99 others); Thu, 23 Jun 2022 12:57:37 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:57198 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232813AbiFWQxY (ORCPT ); Thu, 23 Jun 2022 12:53:24 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id B724517E3D; Thu, 23 Jun 2022 09:52:43 -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 D8F6361FD0; Thu, 23 Jun 2022 16:52:42 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 9F94CC341C5; Thu, 23 Jun 2022 16:52:41 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1656003162; bh=VKfMwkHrv82AyMtOxpd93K9+egkcIRRe1Z42u3TfWZI=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=WSD1k1NonNrgXelY2H3z6qdi5QyUlZ9ckDs91DArk3JLBdTJUfCv9iX1mu82y3kRp vWJST1dxjyw6dr/4H0f7SqReNpl19EUbk/A0fndSZuI//fNueBq9KqNVuPB6Cy4r7g nI0COjzBH1ednbBnKJZfhGKo4nTv65RKpbGisBAY= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Harald Freudenberger , PrasannaKumar Muralidharan , Herbert Xu , "Jason A. Donenfeld" Subject: [PATCH 4.9 150/264] hwrng: use rng source with best quality Date: Thu, 23 Jun 2022 18:42:23 +0200 Message-Id: <20220623164348.309034313@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: Harald Freudenberger commit 2bbb6983887fefc8026beab01198d30f47b7bd22 upstream. This patch rewoks the hwrng to always use the rng source with best entropy quality. On registation and unregistration the hwrng now tries to choose the best (= highest quality value) rng source. The handling of the internal list of registered rng sources is now always sorted by quality and the top most rng chosen. Signed-off-by: Harald Freudenberger Reviewed-by: PrasannaKumar Muralidharan Signed-off-by: Herbert Xu Signed-off-by: Jason A. Donenfeld Signed-off-by: Greg Kroah-Hartman --- drivers/char/hw_random/core.c | 25 +++++++++++++++++++------ 1 file changed, 19 insertions(+), 6 deletions(-) --- a/drivers/char/hw_random/core.c +++ b/drivers/char/hw_random/core.c @@ -28,6 +28,7 @@ static struct hwrng *current_rng; static struct task_struct *hwrng_fill; +/* list of registered rngs, sorted decending by quality */ static LIST_HEAD(rng_list); /* Protects rng_list and current_rng */ static DEFINE_MUTEX(rng_mutex); @@ -416,6 +417,7 @@ int hwrng_register(struct hwrng *rng) { int err = -EINVAL; struct hwrng *old_rng, *tmp; + struct list_head *rng_list_ptr; if (!rng->name || (!rng->data_read && !rng->read)) goto out; @@ -431,14 +433,25 @@ int hwrng_register(struct hwrng *rng) init_completion(&rng->cleanup_done); complete(&rng->cleanup_done); + /* rng_list is sorted by decreasing quality */ + list_for_each(rng_list_ptr, &rng_list) { + tmp = list_entry(rng_list_ptr, struct hwrng, list); + if (tmp->quality < rng->quality) + break; + } + list_add_tail(&rng->list, rng_list_ptr); + old_rng = current_rng; err = 0; - if (!old_rng) { + if (!old_rng || (rng->quality > old_rng->quality)) { + /* + * Set new rng as current as the new rng source + * provides better entropy quality. + */ err = set_current_rng(rng); if (err) goto out_unlock; } - list_add_tail(&rng->list, &rng_list); if (old_rng && !rng->init) { /* @@ -465,12 +478,12 @@ void hwrng_unregister(struct hwrng *rng) list_del(&rng->list); if (current_rng == rng) { drop_current_rng(); + /* rng_list is sorted by quality, use the best (=first) one */ if (!list_empty(&rng_list)) { - struct hwrng *tail; - - tail = list_entry(rng_list.prev, struct hwrng, list); + struct hwrng *new_rng; - set_current_rng(tail); + new_rng = list_entry(rng_list.next, struct hwrng, list); + set_current_rng(new_rng); } }