Received: by 2002:a05:6358:bb9e:b0:b9:5105:a5b4 with SMTP id df30csp5282371rwb; Tue, 6 Sep 2022 23:35:45 -0700 (PDT) X-Google-Smtp-Source: AA6agR4XdyPPo2J5zO/86fjm4VUp77Q7BfjttIqTiNNXpDbuUlWqSanBa+NqUwCFy8JvIq6rJXWq X-Received: by 2002:a63:cc51:0:b0:41f:12f5:675b with SMTP id q17-20020a63cc51000000b0041f12f5675bmr2066600pgi.69.1662532544838; Tue, 06 Sep 2022 23:35:44 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1662532544; cv=none; d=google.com; s=arc-20160816; b=lBAVqt3EJ0KopZHV9uW74J/11YAa7F4oklnS5T82PwZlC+YO/34bAMEl4eTNhRiI9y XGpMEVYw7fZOwrLI7NQJ9CPdvsmlz0ne5OGvaHPez1KZBgRzYwj/iVIrnZdV/wcIwgFb CpeuoZG7qjfuS/Kf4EfNLoouxmzlsckKJoD/Uh8BnmBtNm7Hjyzn4XBJ1x8em1mySdEB fsjvFv0ByjLrBWCMKteBxUf3bkcMCVYBzBI/WM1CWUhRRjFL9LgJIaaN0cDqc66/hSGp yzypYHaeKrN7IpTg5wYCYi6kTVwmsVaAhvRvF6wtqMejLb6AOH4S2ioEzXGfPM/19FZB 91xQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:in-reply-to:content-disposition:mime-version :references:message-id:subject:cc:to:from:date; bh=MPfKgHqxs+Ljb0nztkLMcSuh0jF0SWTJ2jxxd1ph5NA=; b=MUCwDU35mwFPetXedNN6zjyZmFYurvOGQ4cdvv4U9W4in9iHvFQa15WiRPZcoY/y/a Vnq5jC/mtnLf9+NKbUo1T6NLhv4BmcaOF087C6QUTuRyg3tm6FLW4rHsZlqxXH53FO6d lbrPs40XgcL3mbhULye1SzDUk8RtXg7rRy14Bt8cWsRHnhlyflNij/bH8ClaeAVaAamw d1zPXAy8hUGV68hirD391F2IEC2cV8fTcFTWKM7Fk1Js/qdS9RmeqW0QGYjwSRidGaGU mWa30JtcyKSWMgWlQdDjUrch6JbE+apE0ouWhOdrjzSMVWtVYLkWIpj1pB7CgMnkrvpg 3Hwg== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-crypto-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-crypto-owner@vger.kernel.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id z36-20020a630a64000000b004085af5007bsi15131950pgk.428.2022.09.06.23.35.28; Tue, 06 Sep 2022 23:35:44 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-crypto-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; spf=pass (google.com: domain of linux-crypto-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-crypto-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229985AbiIGGaJ (ORCPT + 99 others); Wed, 7 Sep 2022 02:30:09 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:59386 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229987AbiIGG3y (ORCPT ); Wed, 7 Sep 2022 02:29:54 -0400 Received: from fornost.hmeau.com (helcar.hmeau.com [216.24.177.18]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 20C81183BC; Tue, 6 Sep 2022 23:29:37 -0700 (PDT) Received: from gwarestrin.arnor.me.apana.org.au ([192.168.103.7]) by fornost.hmeau.com with smtp (Exim 4.94.2 #2 (Debian)) id 1oVoYj-001uKt-VX; Wed, 07 Sep 2022 16:29:15 +1000 Received: by gwarestrin.arnor.me.apana.org.au (sSMTP sendmail emulation); Wed, 07 Sep 2022 14:29:13 +0800 Date: Wed, 7 Sep 2022 14:29:13 +0800 From: Herbert Xu To: Sven van Ashbrook Cc: LKML , Alex Levin , Rajat Jain , Andrey Pronin , Stephen Boyd , Dominik Brodowski , Eric Biggers , "Jason A. Donenfeld" , Olivia Mackall , linux-crypto@vger.kernel.org Subject: Re: [PATCH v1 2/2] hwrng: core: fix potential suspend/resume race condition Message-ID: References: <20220831172024.1613208-1-svenva@chromium.org> <20220831172024.1613208-2-svenva@chromium.org> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20220831172024.1613208-2-svenva@chromium.org> X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00,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-crypto@vger.kernel.org On Wed, Aug 31, 2022 at 05:20:24PM +0000, Sven van Ashbrook wrote: > The hwrng fill function runs as a normal kthread. This thread > doesn't get frozen by the PM, i.e. it will keep running during, > or in, system suspend. It may call the client driver's > data_present()/data_read() functions during, or in, suspend; > which may generate errors or warnings. For example, if the > client driver uses an i2c bus, the following warning may be > intermittently generated: > > i2c: Transfer while suspended > > Fix by converting the delay polled kthread into an ordered work > queue running a single, self-rearming delayed_work. Make the > workqueue WQ_FREEZABLE, so the PM will drain any work items > before going into suspend. This prevents client drivers from > being accessed during, or in, suspend. > > Tested on a Chromebook containing an cr50 tpm over i2c. The test > consists of 31000 suspend/resume cycles. Occasional > "i2c: Transfer while suspended" warnings are seen. After applying > this patch, these warnings disappear. > > This patch also does not appear to cause any regressions on the > ChromeOS test queues. > > Signed-off-by: Sven van Ashbrook The general concept seems to be fine. > - if (rc <= 0) { > - pr_warn("hwrng: no data available\n"); > - msleep_interruptible(10000); > - continue; > - } > + if (!quality) > + return; > > + if (rc > 0) { > /* If we cannot credit at least one bit of entropy, > * keep track of the remainder for the next iteration > */ You need to refresh your patch-set against the latest tree. This function has changed quite a bit. > @@ -541,14 +536,12 @@ static void hwrng_manage_rngd(struct hwrng *rng) > if (WARN_ON(!mutex_is_locked(&rng_mutex))) > return; > > - if (rng->quality == 0 && hwrng_fill) > - kthread_stop(hwrng_fill); > - if (rng->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; > - } > + if (rng->quality == 0 && is_hwrng_wq_running) { > + cancel_delayed_work(&hwrng_fill_dwork); > + is_hwrng_wq_running = false; > + } else if (rng->quality > 0 && !is_hwrng_wq_running) { > + mod_delayed_work(hwrng_wq, &hwrng_fill_dwork, 0); > + is_hwrng_wq_running = true; > } > } > > @@ -631,8 +624,7 @@ void hwrng_unregister(struct hwrng *rng) > new_rng = get_current_rng_nolock(); > if (list_empty(&rng_list)) { > mutex_unlock(&rng_mutex); > - if (hwrng_fill) > - kthread_stop(hwrng_fill); > + cancel_delayed_work_sync(&hwrng_fill_dwork); What if hwrng_manage_rngd races against hwrng_unregister? Thanks, -- Email: Herbert Xu Home Page: http://gondor.apana.org.au/~herbert/ PGP Key: http://gondor.apana.org.au/~herbert/pubkey.txt