• David Tolnay's avatar
    hwrng: virtio - Avoid repeated init of completion · aef027db
    David Tolnay authored
    The virtio-rng driver uses a completion called have_data to wait for a
    virtio read to be fulfilled by the hypervisor. The completion is reset
    before placing a buffer on the virtio queue and completed by the virtio
    callback once data has been written into the buffer.
    
    Prior to this commit, the driver called init_completion on this
    completion both during probe as well as when registering virtio buffers
    as part of a hwrng read operation. The second of these init_completion
    calls should instead be reinit_completion because the have_data
    completion has already been inited by probe. As described in
    Documentation/scheduler/completion.txt, "Calling init_completion() twice
    on the same completion object is most likely a bug".
    
    This bug was present in the initial implementation of virtio-rng in
    f7f510ec ("virtio: An entropy device, as suggested by hpa"). Back
    then the have_data completion was a single static completion rather than
    a member of one of potentially multiple virtrng_info structs as
    implemented later by 08e53fbd ("virtio-rng: support multiple
    virtio-rng devices"). The original driver incorrectly used
    init_completion rather than INIT_COMPLETION to reset have_data during
    read.
    
    Tested by running `head -c48 /dev/random | hexdump` within crosvm, the
    Chrome OS virtual machine monitor, and confirming that the virtio-rng
    driver successfully produces random bytes from the host.
    Signed-off-by: default avatarDavid Tolnay <dtolnay@gmail.com>
    Tested-by: default avatarDavid Tolnay <dtolnay@gmail.com>
    Signed-off-by: default avatarHerbert Xu <herbert@gondor.apana.org.au>
    aef027db
virtio-rng.c 5.08 KB