Skip to contentSkip to conversation

hwrng: core - don't block in add_early_randomness()

PATCH In review Laurent Vivier 2 participants 3 messages Mar 29, 2019 – Apr 8, 2019

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 hwrng: core - don't block in add_early_randomness()

If the device is not ready to provide data the kernel will be stuck indefinitely in the init function.

This is not a problem if the device is driven using a module, but if the driver is linked directly into the kernel then the kernel boot sequence hangs.

This can happen with virtio-rng device with rng-egd backend with no data provider, for instance with QEMU command line parameters:

...

    -chardev socket,id=charrng0,host=localhost,port=2345,server,nowait \
    -object rng-egd,id=objrng0,chardev=charrng0 \
    -device virtio-rng-pci,rng=objrng0,id=rng0

To avoid that, we can call rng_get_data() in non blocking mode because the function already manages the case where byte_read is
0 (if the device is not already initialized).

See also commit d3cc7996473a
("hwrng: fetch randomness only after device init")

---
 drivers/char/hw_random/core.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/char/hw_random/core.c +1 -1
index 95be7228f327..3866d6b8017c 100644
@@ -67,7 +67,7 @@ static void add_early_randomness(struct hwrng *rng)
 	size_t size = min_t(size_t, 16, rng_buffer_size());
 
 	mutex_lock(&reading_mutex);
-	bytes_read = rng_get_data(rng, rng_buffer, size, 1);
+	bytes_read = rng_get_data(rng, rng_buffer, size, 0);
 	mutex_unlock(&reading_mutex);
 	if (bytes_read > 0)
 		add_device_randomness(rng_buffer, bytes_read);
Signature · 1 line
2.20.1
maintainer
#1 lore raw
Quoted 15 lines · Laurent Vivier

If the device is not ready to provide data the kernel will be stuck indefinitely in the init function.

This is not a problem if the device is driven using a module, but if the driver is linked directly into the kernel then the kernel boot sequence hangs.

This can happen with virtio-rng device with rng-egd backend with no data provider, for instance with QEMU command line parameters:

...

    -chardev socket,id=charrng0,host=localhost,port=2345,server,nowait \
    -object rng-egd,id=objrng0,chardev=charrng0 \
    -device virtio-rng-pci,rng=objrng0,id=rng0
Quoted 8 lines

To avoid that, we can call rng_get_data() in non blocking mode because the function already manages the case where byte_read is
0 (if the device is not already initialized).

See also commit d3cc7996473a
("hwrng: fetch randomness only after device init")

I think this is either not a bug (make sure that the user supplies a RNG source) or it should be addressed in virtio-rng.

Cheers,

Signature · 3 lines
Email: Herbert Xu <[email protected]>
Home Page: http://gondor.apana.org.au/~herbert/
PGP Key: http://gondor.apana.org.au/~herbert/pubkey.txt
#2 lore raw
Quoted 20 lines · Herbert Xu
Quoted 19 lines · Laurent Vivier

If the device is not ready to provide data the kernel will be stuck indefinitely in the init function.

This is not a problem if the device is driven using a module, but if the driver is linked directly into the kernel then the kernel boot sequence hangs.

This can happen with virtio-rng device with rng-egd backend with no data provider, for instance with QEMU command line parameters:

...

    -chardev socket,id=charrng0,host=localhost,port=2345,server,nowait \
    -object rng-egd,id=objrng0,chardev=charrng0 \
    -device virtio-rng-pci,rng=objrng0,id=rng0

To avoid that, we can call rng_get_data() in non blocking mode because the function already manages the case where byte_read is
0 (if the device is not already initialized).

Quoted 7 lines
Quoted 4 lines

See also commit d3cc7996473a
("hwrng: fetch randomness only after device init")

I think this is either not a bug (make sure that the user supplies a RNG source) or it should be addressed in virtio-rng.

Yes, I did more investigation and I agree.

Thanks,
Laurent