]> git.ipfire.org Git - thirdparty/kernel/stable-queue.git/blame - releases/4.19.34/hwrng-virtio-avoid-repeated-init-of-completion.patch
Linux 4.19.34
[thirdparty/kernel/stable-queue.git] / releases / 4.19.34 / hwrng-virtio-avoid-repeated-init-of-completion.patch
CommitLineData
ba172962
SL
1From 186d10d5c15b4148016730cecfc32db82d79ccf3 Mon Sep 17 00:00:00 2001
2From: David Tolnay <dtolnay@gmail.com>
3Date: Mon, 7 Jan 2019 14:36:11 -0800
4Subject: hwrng: virtio - Avoid repeated init of completion
5
6[ Upstream commit aef027db48da56b6f25d0e54c07c8401ada6ce21 ]
7
8The virtio-rng driver uses a completion called have_data to wait for a
9virtio read to be fulfilled by the hypervisor. The completion is reset
10before placing a buffer on the virtio queue and completed by the virtio
11callback once data has been written into the buffer.
12
13Prior to this commit, the driver called init_completion on this
14completion both during probe as well as when registering virtio buffers
15as part of a hwrng read operation. The second of these init_completion
16calls should instead be reinit_completion because the have_data
17completion has already been inited by probe. As described in
18Documentation/scheduler/completion.txt, "Calling init_completion() twice
19on the same completion object is most likely a bug".
20
21This bug was present in the initial implementation of virtio-rng in
22f7f510ec1957 ("virtio: An entropy device, as suggested by hpa"). Back
23then the have_data completion was a single static completion rather than
24a member of one of potentially multiple virtrng_info structs as
25implemented later by 08e53fbdb85c ("virtio-rng: support multiple
26virtio-rng devices"). The original driver incorrectly used
27init_completion rather than INIT_COMPLETION to reset have_data during
28read.
29
30Tested by running `head -c48 /dev/random | hexdump` within crosvm, the
31Chrome OS virtual machine monitor, and confirming that the virtio-rng
32driver successfully produces random bytes from the host.
33
34Signed-off-by: David Tolnay <dtolnay@gmail.com>
35Tested-by: David Tolnay <dtolnay@gmail.com>
36Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
37Signed-off-by: Sasha Levin <sashal@kernel.org>
38---
39 drivers/char/hw_random/virtio-rng.c | 2 +-
40 1 file changed, 1 insertion(+), 1 deletion(-)
41
42diff --git a/drivers/char/hw_random/virtio-rng.c b/drivers/char/hw_random/virtio-rng.c
43index b89df66ea1ae..7abd604e938c 100644
44--- a/drivers/char/hw_random/virtio-rng.c
45+++ b/drivers/char/hw_random/virtio-rng.c
46@@ -73,7 +73,7 @@ static int virtio_read(struct hwrng *rng, void *buf, size_t size, bool wait)
47
48 if (!vi->busy) {
49 vi->busy = true;
50- init_completion(&vi->have_data);
51+ reinit_completion(&vi->have_data);
52 register_buffer(vi, buf, size);
53 }
54
55--
562.19.1
57