virtio: rng: gracefully handle 0 byte returns
authorAndre Przywara <andre.przywara@arm.com>
Tue, 7 Nov 2023 16:09:00 +0000 (16:09 +0000)
committerTom Rini <trini@konsulko.com>
Fri, 17 Nov 2023 16:58:26 +0000 (11:58 -0500)
commit45c4b276f0a4d67e06c94de3d0a5dadf4bee530a
tree5378af6bbb11115d1eb807df683ba0efac866043
parent741d1e9d3f368908e3cd1861ddd707e81e1fd576
virtio: rng: gracefully handle 0 byte returns

According to the virtio v1.x "entropy device" specification, a virtio-rng
device is supposed to always return at least one byte of entropy.
However the virtio v0.9 spec does not mention such a requirement.

The Arm Fixed Virtual Platform (FVP) implementation of virtio-rng always
returns 8 bytes less of entropy than requested. If 8 bytes or less are
requested, it will return 0 bytes.
This behaviour makes U-Boot's virtio_rng_read() implementation go into an
endless loop, hanging the system.

Work around this problem by always requesting 8 bytes more than needed,
but only if a previous call to virtqueue_get_buf() returned 0 bytes.

This should never trigger on a v1.x spec compliant implementation, but
fixes the hang on the Arm FVP.

Signed-off-by: Andre Przywara <andre.przywara@arm.com>
Reported-by: Peter Hoyes <peter.hoyes@arm.com>
drivers/virtio/virtio_rng.c