Linux-Crypto Archive on lore.kernel.org
 help / color / Atom feed
* [PATCH] random: Don't freeze in add_hwgenerator_randomness() if stopping kthread
@ 2019-11-10 13:55 Maciej S. Szmigiero
  2019-11-15  6:08 ` Herbert Xu
       [not found] ` <5dcee409.1c69fb81.f5027.48ad@mx.google.com>
  0 siblings, 2 replies; 5+ messages in thread
From: Maciej S. Szmigiero @ 2019-11-10 13:55 UTC (permalink / raw)
  To: Theodore Ts'o
  Cc: Herbert Xu, Arnd Bergmann, Greg Kroah-Hartman,
	Sebastian Andrzej Siewior, Keerthy, Stephen Boyd, linux-crypto,
	linux-kernel

Since commit 59b569480dc8
("random: Use wait_event_freezable() in add_hwgenerator_randomness()")
there is a race in add_hwgenerator_randomness() between freezing and
stopping the calling kthread.

This commit changed wait_event_interruptible() call with
kthread_freezable_should_stop() as a condition into wait_event_freezable()
with just kthread_should_stop() as a condition to fix a warning that
kthread_freezable_should_stop() might sleep inside the wait.

wait_event_freezable() ultimately calls __refrigerator() with its
check_kthr_stop argument set to false, which causes it to keep the kthread
frozen even if somebody calls kthread_stop() on it.

Calling wait_event_freezable() with kthread_should_stop() as a condition
is racy because it doesn't take into account the situation where this
condition becomes true on a kthread marked for freezing only after this
condition has already been checked.

Calling freezing() should avoid the issue that the commit 59b569480dc8 has
fixed, as it is only a checking function, it doesn't actually do the
freezing.

add_hwgenerator_randomness() has two post-boot users: in khwrng the
kthread will be frozen anyway by call to kthread_freezable_should_stop()
in its main loop, while its second user (ath9k-hwrng) is not freezable at
all.

This change allows a VM with virtio-rng loaded to write s2disk image
successfully.

Fixes: 59b569480dc8 ("random: Use wait_event_freezable() in add_hwgenerator_randomness()")
Signed-off-by: Maciej S. Szmigiero <mail@maciej.szmigiero.name>
---
 drivers/char/random.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/drivers/char/random.c b/drivers/char/random.c
index de434feb873a..2f87910dd498 100644
--- a/drivers/char/random.c
+++ b/drivers/char/random.c
@@ -2500,8 +2500,8 @@ void add_hwgenerator_randomness(const char *buffer, size_t count,
 	 * We'll be woken up again once below random_write_wakeup_thresh,
 	 * or when the calling thread is about to terminate.
 	 */
-	wait_event_freezable(random_write_wait,
-			kthread_should_stop() ||
+	wait_event_interruptible(random_write_wait,
+			kthread_should_stop() || freezing(current) ||
 			ENTROPY_BITS(&input_pool) <= random_write_wakeup_bits);
 	mix_pool_bytes(poolp, buffer, count);
 	credit_entropy_bits(poolp, entropy);

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, back to index

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2019-11-10 13:55 [PATCH] random: Don't freeze in add_hwgenerator_randomness() if stopping kthread Maciej S. Szmigiero
2019-11-15  6:08 ` Herbert Xu
     [not found] ` <5dcee409.1c69fb81.f5027.48ad@mx.google.com>
2019-11-16 23:01   ` Maciej S. Szmigiero
2019-11-17  0:56     ` Herbert Xu
2019-11-17  4:09       ` Theodore Y. Ts'o

Linux-Crypto Archive on lore.kernel.org

Archives are clonable:
	git clone --mirror https://lore.kernel.org/linux-crypto/0 linux-crypto/git/0.git

	# If you have public-inbox 1.1+ installed, you may
	# initialize and index your mirror using the following commands:
	public-inbox-init -V2 linux-crypto linux-crypto/ https://lore.kernel.org/linux-crypto \
		linux-crypto@vger.kernel.org
	public-inbox-index linux-crypto

Example config snippet for mirrors

Newsgroup available over NNTP:
	nntp://nntp.lore.kernel.org/org.kernel.vger.linux-crypto


AGPL code for this site: git clone https://public-inbox.org/public-inbox.git