linux-wireless.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: "Jason A. Donenfeld" <Jason@zx2c4.com>
To: linux-wireless@vger.kernel.org, linux-kernel@vger.kernel.org,
	"Toke Høiland-Jørgensen" <toke@redhat.com>
Cc: "Jason A. Donenfeld" <Jason@zx2c4.com>,
	Gregory Erwin <gregerwin256@gmail.com>,
	Kalle Valo <kvalo@kernel.org>,
	Rui Salvaterra <rsalvaterra@gmail.com>,
	Herbert Xu <herbert@gondor.apana.org.au>,
	stable@vger.kernel.org
Subject: [PATCH v3] ath9k: sleep for less time when unregistering hwrng
Date: Mon, 27 Jun 2022 11:29:27 +0200	[thread overview]
Message-ID: <20220627092927.513709-1-Jason@zx2c4.com> (raw)
In-Reply-To: <YrlR7O4roipJt4Nc@gondor.apana.org.au>

Even though hwrng provides a `wait` parameter, it doesn't work very well
when waiting for a long time. There are numerous deadlocks that emerge
related to shutdown. Work around this API limitation by waiting for a
shorter amount of time and erroring more frequently. This commit also
prevents hwrng from splatting messages to dmesg when there's a timeout
and prevents calling msleep_interruptible() for tons of time when a
thread is supposed to be shutting down, since msleep_interruptible()
isn't actually interrupted by kthread_stop().

Reported-by: Gregory Erwin <gregerwin256@gmail.com>
Tested-by: Gregory Erwin <gregerwin256@gmail.com>
Cc: Toke Høiland-Jørgensen <toke@redhat.com>
Cc: Kalle Valo <kvalo@kernel.org>
Cc: Rui Salvaterra <rsalvaterra@gmail.com>
Cc: Herbert Xu <herbert@gondor.apana.org.au>
Cc: stable@vger.kernel.org
Fixes: fcd09c90c3c5 ("ath9k: use hw_random API instead of directly dumping into random.c")
Link: https://lore.kernel.org/all/CAO+Okf6ZJC5-nTE_EJUGQtd8JiCkiEHytGgDsFGTEjs0c00giw@mail.gmail.com/
Link: https://lore.kernel.org/lkml/CAO+Okf5k+C+SE6pMVfPf-d8MfVPVq4PO7EY8Hys_DVXtent3HA@mail.gmail.com/
Link: https://bugs.archlinux.org/task/75138
Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
---
 drivers/char/hw_random/core.c        | 10 ++++++++--
 drivers/net/wireless/ath/ath9k/rng.c | 20 +++-----------------
 2 files changed, 11 insertions(+), 19 deletions(-)

diff --git a/drivers/char/hw_random/core.c b/drivers/char/hw_random/core.c
index 16f227b995e8..a15273271d87 100644
--- a/drivers/char/hw_random/core.c
+++ b/drivers/char/hw_random/core.c
@@ -513,8 +513,13 @@ static int hwrng_fillfn(void *unused)
 			break;
 
 		if (rc <= 0) {
-			pr_warn("hwrng: no data available\n");
-			msleep_interruptible(10000);
+			int i;
+
+			for (i = 0; i < 100; ++i) {
+				if (kthread_should_stop() ||
+				    schedule_timeout_interruptible(HZ / 20))
+					goto out;
+			}
 			continue;
 		}
 
@@ -529,6 +534,7 @@ static int hwrng_fillfn(void *unused)
 		add_hwgenerator_randomness((void *)rng_fillbuf, rc,
 					   entropy >> 10);
 	}
+out:
 	hwrng_fill = NULL;
 	return 0;
 }
diff --git a/drivers/net/wireless/ath/ath9k/rng.c b/drivers/net/wireless/ath/ath9k/rng.c
index cb5414265a9b..39195f89ea85 100644
--- a/drivers/net/wireless/ath/ath9k/rng.c
+++ b/drivers/net/wireless/ath/ath9k/rng.c
@@ -52,20 +52,6 @@ static int ath9k_rng_data_read(struct ath_softc *sc, u32 *buf, u32 buf_size)
 	return j << 2;
 }
 
-static u32 ath9k_rng_delay_get(u32 fail_stats)
-{
-	u32 delay;
-
-	if (fail_stats < 100)
-		delay = 10;
-	else if (fail_stats < 105)
-		delay = 1000;
-	else
-		delay = 10000;
-
-	return delay;
-}
-
 static int ath9k_rng_read(struct hwrng *rng, void *buf, size_t max, bool wait)
 {
 	struct ath_softc *sc = container_of(rng, struct ath_softc, rng_ops);
@@ -80,10 +66,10 @@ static int ath9k_rng_read(struct hwrng *rng, void *buf, size_t max, bool wait)
 			bytes_read += max & 3UL;
 			memzero_explicit(&word, sizeof(word));
 		}
-		if (!wait || !max || likely(bytes_read) || fail_stats > 110)
+		if (!wait || !max || likely(bytes_read) || ++fail_stats >= 100 ||
+		    schedule_timeout_interruptible(HZ / 20) ||
+		    ((current->flags & PF_KTHREAD) && kthread_should_stop()))
 			break;
-
-		msleep_interruptible(ath9k_rng_delay_get(++fail_stats));
 	}
 
 	if (wait && !bytes_read && max)
-- 
2.35.1


  reply	other threads:[~2022-06-27  9:29 UTC|newest]

Thread overview: 28+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-06-23  5:08 ath9k: hwrng blocks for several minutes when phy is un-associated Gregory Erwin
2022-06-23 12:14 ` Jason A. Donenfeld
2022-06-23 12:16   ` Jason A. Donenfeld
2022-06-23 23:36     ` Gregory Erwin
2022-06-23 23:47       ` Jason A. Donenfeld
2022-06-24  0:01         ` Jason A. Donenfeld
2022-06-24  0:50           ` Jason A. Donenfeld
2022-06-24  1:14             ` [PATCH] ath9k: rng: escape sleep loop when unregistering Jason A. Donenfeld
2022-06-24  5:25               ` Gregory Erwin
2022-06-24 19:12                 ` Jason A. Donenfeld
2022-06-24 20:44                   ` [PATCH v2] ath9k: sleep for less time when unregistering hwrng Jason A. Donenfeld
2022-06-25  0:13                     ` Gregory Erwin
2022-06-25  0:40                       ` Jason A. Donenfeld
2022-06-27  6:45                     ` Herbert Xu
2022-06-27  9:29                       ` Jason A. Donenfeld [this message]
2022-06-27 10:49                         ` [PATCH v4] " Jason A. Donenfeld
2022-06-27 11:37                           ` [PATCH v5] " Jason A. Donenfeld
2022-06-27 12:07                             ` [PATCH v6] " Jason A. Donenfeld
2022-06-27 12:18                               ` Toke Høiland-Jørgensen
2022-06-28  1:39                                 ` Gregory Erwin
2022-06-28 10:46                                   ` Jason A. Donenfeld
2022-06-28 10:48                                     ` Jason A. Donenfeld
2022-06-28 10:51                                       ` Herbert Xu
2022-06-28 10:55                                         ` Jason A. Donenfeld
2022-06-28 12:05                                           ` Jason A. Donenfeld
2022-06-28 15:14                                             ` Jason A. Donenfeld
2022-06-28  1:53                               ` Herbert Xu
2022-06-28 10:45                               ` Toke Høiland-Jørgensen

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20220627092927.513709-1-Jason@zx2c4.com \
    --to=jason@zx2c4.com \
    --cc=gregerwin256@gmail.com \
    --cc=herbert@gondor.apana.org.au \
    --cc=kvalo@kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-wireless@vger.kernel.org \
    --cc=rsalvaterra@gmail.com \
    --cc=stable@vger.kernel.org \
    --cc=toke@redhat.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).