From patchwork Mon Jan 7 23:44:38 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Tolnay X-Patchwork-Id: 10751297 X-Patchwork-Delegate: herbert@gondor.apana.org.au Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 926F091E for ; Mon, 7 Jan 2019 23:44:52 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 7EC6D28A80 for ; Mon, 7 Jan 2019 23:44:52 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 7072928ADA; Mon, 7 Jan 2019 23:44:52 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-8.0 required=2.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FROM,MAILING_LIST_MULTI,RCVD_IN_DNSWL_HI autolearn=ham version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 0D7A928A80 for ; Mon, 7 Jan 2019 23:44:52 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727106AbfAGXov (ORCPT ); Mon, 7 Jan 2019 18:44:51 -0500 Received: from mail-it1-f193.google.com ([209.85.166.193]:37052 "EHLO mail-it1-f193.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726667AbfAGXov (ORCPT ); Mon, 7 Jan 2019 18:44:51 -0500 Received: by mail-it1-f193.google.com with SMTP id b5so3681741iti.2 for ; Mon, 07 Jan 2019 15:44:50 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:from:date:message-id:subject:to:cc; bh=/FycSehYAb+FdR9Qxb2TjG9eWJL08/xJeXh9JFQUBGs=; b=ramtKVoPdTYlReahoaB5dIUfd0nhnS27kkBaEM8MIBdaAzS3IcuC1Gr35/eJeRhSRm 58Di67eSEfgk7DGUQTGgfKXoTqLCm1Eiflzgv2XelbC5z7meO6pP+4+L/Dj6so9wHdUo bZ0ZgiBabNV5vPywOsLyJ48sezvolrX9qvee9OJt7vu42a7UrVxy5lD2UwlGISeD6hoW UfIHnPadjcwBp/KOmlPBG9+3K/2mxocn4ARfNAqeDq7Oyver5HtVVukM3nYAIMHBnqEu EDM2ImoH3eGIsl7Bs5v0b/O2jz4Rr1GjfMOI3BBFTqS0388wt+ZuH3bBIm3/xlcMMCX0 1iXA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:from:date:message-id:subject:to:cc; bh=/FycSehYAb+FdR9Qxb2TjG9eWJL08/xJeXh9JFQUBGs=; b=MBjV2Ogxoe/IFWPjj6rZh8hpeNhFVX1WiH7+RZOTaUYEThOvIb1rjiOuBk/SDyOtJw r12LyIfEeHe6C0rpcyxdJIOwpht9YbVHaFkERJwgok/Z2xu9zkL27rq42Gwb6trdeog1 4iAtHQLWruKnRztH0zNhgPglfLJMEsDhpL/ER/oUa9WH5WHscwLlqtYCgqzoLL3XCxR6 iYg5r95yQ7UhgD5S73iDGgo3z7w+vm9fArvhABKCb9207NxMtjbhEktr2IYCylXj/t9b 5Dchj1gI4mCJC1cO+K+Kw76EoOaHxAVc4m0HxK/0nT7Uwsznsd7KXlvqDDLq2LoZ+oES gG1Q== X-Gm-Message-State: AJcUukfl7Px7ZfRLVX+6l/9WyUDOyeDjk+PoVrfrfPsEguGyCMOLtkP5 9oyHAQT9KylzrA8q2ntdmHNxOfd+84tDo+tlPqI= X-Google-Smtp-Source: ALg8bN6qlcbxuXb1Iv1LnNZCIR/QRoMbJ0538aV9PUmF+1o3PJjhBT3i9QqFHfQj01kNyfM8oY34EE1jWaAXvj0zL8I= X-Received: by 2002:a24:4242:: with SMTP id i63mr9135893itb.139.1546904689899; Mon, 07 Jan 2019 15:44:49 -0800 (PST) MIME-Version: 1.0 From: David Tolnay Date: Mon, 7 Jan 2019 15:44:38 -0800 Message-ID: Subject: [PATCH] virtio-rng: Avoid repeated init of completion To: Matt Mackall , Herbert Xu Cc: linux-crypto@vger.kernel.org Sender: linux-crypto-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-crypto@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP From: David Tolnay Date: Mon, 7 Jan 2019 14:36:11 -0800 Subject: [PATCH] virtio-rng: Avoid repeated init of completion 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 f7f510ec1957 ("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 08e53fbdb85c ("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: David Tolnay Tested-by: David Tolnay --- drivers/char/hw_random/virtio-rng.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/drivers/char/hw_random/virtio-rng.c b/drivers/char/hw_random/virtio-rng.c index b89df66ea1ae..7abd604e938c 100644 --- a/drivers/char/hw_random/virtio-rng.c +++ b/drivers/char/hw_random/virtio-rng.c @@ -73,7 +73,7 @@ static int virtio_read(struct hwrng *rng, void *buf, size_t size, bool wait) if (!vi->busy) { vi->busy = true; - init_completion(&vi->have_data); + reinit_completion(&vi->have_data); register_buffer(vi, buf, size); }