From patchwork Sun Feb 13 20:46:29 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Dominik Brodowski X-Patchwork-Id: 542619 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 8D9CAC433F5 for ; Sun, 13 Feb 2022 20:50:20 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S238285AbiBMUuY (ORCPT ); Sun, 13 Feb 2022 15:50:24 -0500 Received: from mxb-00190b01.gslb.pphosted.com ([23.128.96.19]:55676 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S238214AbiBMUuX (ORCPT ); Sun, 13 Feb 2022 15:50:23 -0500 Received: from isilmar-4.linta.de (isilmar-4.linta.de [136.243.71.142]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 6BD0B53723; Sun, 13 Feb 2022 12:50:17 -0800 (PST) X-isilmar-external: YES X-isilmar-external: YES X-isilmar-external: YES X-isilmar-external: YES Received: from owl.dominikbrodowski.net (owl.brodo.linta [10.2.0.111]) by isilmar-4.linta.de (Postfix) with ESMTPSA id 58EC220133D; Sun, 13 Feb 2022 20:50:15 +0000 (UTC) Received: by owl.dominikbrodowski.net (Postfix, from userid 1000) id 58AF3808C5; Sun, 13 Feb 2022 21:48:21 +0100 (CET) From: Dominik Brodowski To: Herbert Xu Cc: linux-kernel@vger.kernel.org, linux-crypto@vger.kernel.org, "Jason A . Donenfeld" Subject: [PATCH 2/4] hw_random: start and stop in-kernel rngd in separate function Date: Sun, 13 Feb 2022 21:46:29 +0100 Message-Id: <20220213204631.354247-3-linux@dominikbrodowski.net> X-Mailer: git-send-email 2.35.1 In-Reply-To: <20220213204631.354247-1-linux@dominikbrodowski.net> References: <20220213204631.354247-1-linux@dominikbrodowski.net> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-crypto@vger.kernel.org Extract the start/stop logic for the in-kernel rngd thread to a separate function. Cc: Herbert Xu Cc: Jason A. Donenfeld Signed-off-by: Dominik Brodowski --- drivers/char/hw_random/core.c | 24 ++++++++++++++---------- 1 file changed, 14 insertions(+), 10 deletions(-) diff --git a/drivers/char/hw_random/core.c b/drivers/char/hw_random/core.c index 6f09f4e5af20..29febf55b0d4 100644 --- a/drivers/char/hw_random/core.c +++ b/drivers/char/hw_random/core.c @@ -51,7 +51,7 @@ MODULE_PARM_DESC(default_quality, static void drop_current_rng(void); static int hwrng_init(struct hwrng *rng); -static void start_khwrngd(void); +static void hwrng_manage_rngd(void); static inline int rng_get_data(struct hwrng *rng, u8 *buffer, size_t size, int wait); @@ -164,10 +164,7 @@ static int hwrng_init(struct hwrng *rng) if (current_quality > 1024) current_quality = 1024; - if (current_quality == 0 && hwrng_fill) - kthread_stop(hwrng_fill); - if (current_quality > 0 && !hwrng_fill) - start_khwrngd(); + hwrng_manage_rngd(); return 0; } @@ -466,12 +463,19 @@ static int hwrng_fillfn(void *unused) return 0; } -static void start_khwrngd(void) +static void hwrng_manage_rngd(void) { - hwrng_fill = kthread_run(hwrng_fillfn, NULL, "hwrng"); - if (IS_ERR(hwrng_fill)) { - pr_err("hwrng_fill thread creation failed\n"); - hwrng_fill = NULL; + if (WARN_ON(!mutex_is_locked(&rng_mutex))) + return; + + if (current_quality == 0 && hwrng_fill) + kthread_stop(hwrng_fill); + if (current_quality > 0 && !hwrng_fill) { + hwrng_fill = kthread_run(hwrng_fillfn, NULL, "hwrng"); + if (IS_ERR(hwrng_fill)) { + pr_err("hwrng_fill thread creation failed\n"); + hwrng_fill = NULL; + } } }