From patchwork Fri Jan 22 12:19:58 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ladi Prosek X-Patchwork-Id: 8090841 Return-Path: X-Original-To: patchwork-qemu-devel@patchwork.kernel.org Delivered-To: patchwork-parsemail@patchwork2.web.kernel.org Received: from mail.kernel.org (mail.kernel.org [198.145.29.136]) by patchwork2.web.kernel.org (Postfix) with ESMTP id DEE6FBEEE5 for ; Fri, 22 Jan 2016 14:50:11 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id 39835202F2 for ; Fri, 22 Jan 2016 14:50:11 +0000 (UTC) Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id 9255B20274 for ; Fri, 22 Jan 2016 14:50:05 +0000 (UTC) Received: from localhost ([::1]:54233 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1aMd2K-0005oK-Nw for patchwork-qemu-devel@patchwork.kernel.org; Fri, 22 Jan 2016 09:50:04 -0500 Received: from eggs.gnu.org ([2001:4830:134:3::10]:55881) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1aMahQ-0000EE-QM for qemu-devel@nongnu.org; Fri, 22 Jan 2016 07:20:21 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1aMahL-00017P-QK for qemu-devel@nongnu.org; Fri, 22 Jan 2016 07:20:20 -0500 Received: from mx1.redhat.com ([209.132.183.28]:50502) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1aMahL-00017L-Id for qemu-devel@nongnu.org; Fri, 22 Jan 2016 07:20:15 -0500 Received: from int-mx14.intmail.prod.int.phx2.redhat.com (int-mx14.intmail.prod.int.phx2.redhat.com [10.5.11.27]) by mx1.redhat.com (Postfix) with ESMTPS id BD0BC38883E for ; Fri, 22 Jan 2016 12:20:14 +0000 (UTC) Received: from dhcp-1-107.brq.redhat.com (dhcp-1-127.brq.redhat.com [10.34.1.127]) by int-mx14.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with ESMTP id u0MCKDrI017690; Fri, 22 Jan 2016 07:20:13 -0500 From: Ladi Prosek To: qemu-devel@nongnu.org Date: Fri, 22 Jan 2016 13:19:58 +0100 Message-Id: <1453465198-11000-1-git-send-email-lprosek@redhat.com> X-Scanned-By: MIMEDefang 2.68 on 10.5.11.27 X-detected-operating-system: by eggs.gnu.org: GNU/Linux 3.x X-Received-From: 209.132.183.28 X-Mailman-Approved-At: Fri, 22 Jan 2016 09:49:58 -0500 Cc: amit.shah@redhat.com, Ladi Prosek Subject: [Qemu-devel] [PATCH] rng-random: implement request queue X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+patchwork-qemu-devel=patchwork.kernel.org@nongnu.org Sender: qemu-devel-bounces+patchwork-qemu-devel=patchwork.kernel.org@nongnu.org X-Spam-Status: No, score=-6.9 required=5.0 tests=BAYES_00, RCVD_IN_DNSWL_HI, UNPARSEABLE_RELAY autolearn=unavailable version=3.3.1 X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on mail.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP If the guest adds a buffer to the virtio queue while another buffer is still pending and hasn't been filled and returned by the rng device, rng-random internally discards the pending request, which leads to the second buffer getting stuck in the queue. For the guest this manifests as delayed completion of reads from virtio-rng, i.e. a read is completed only after another read is issued. This patch adds an internal queue of requests, analogous to what rng-egd uses, to make sure that requests and responses are balanced and correctly ordered. Signed-off-by: Ladi Prosek --- backends/rng-random.c | 71 +++++++++++++++++++++++++++++++++++++++------------ 1 file changed, 54 insertions(+), 17 deletions(-) diff --git a/backends/rng-random.c b/backends/rng-random.c index 4e51f46..3f1fd21 100644 --- a/backends/rng-random.c +++ b/backends/rng-random.c @@ -22,10 +22,16 @@ struct RndRandom int fd; char *filename; + GSList *requests; +}; + +typedef struct RngRequest +{ EntropyReceiveFunc *receive_func; void *opaque; size_t size; -}; +} RngRequest; + /** * A simple and incomplete backend to request entropy from /dev/random. @@ -37,19 +43,27 @@ struct RndRandom static void entropy_available(void *opaque) { RndRandom *s = RNG_RANDOM(opaque); - uint8_t buffer[s->size]; - ssize_t len; - len = read(s->fd, buffer, s->size); - if (len < 0 && errno == EAGAIN) { - return; - } - g_assert(len != -1); + while (s->requests != NULL) { + RngRequest *req = s->requests->data; + uint8_t buffer[req->size]; + ssize_t len; + + len = read(s->fd, buffer, req->size); + if (len < 0 && errno == EAGAIN) { + return; + } + g_assert(len != -1); - s->receive_func(s->opaque, buffer, len); - s->receive_func = NULL; + req->receive_func(req->opaque, buffer, len); - qemu_set_fd_handler(s->fd, NULL, NULL, NULL); + s->requests = g_slist_remove_link(s->requests, s->requests); + g_free(req); + } + + if (s->requests == NULL) { + qemu_set_fd_handler(s->fd, NULL, NULL, NULL); + } } static void rng_random_request_entropy(RngBackend *b, size_t size, @@ -57,16 +71,36 @@ static void rng_random_request_entropy(RngBackend *b, size_t size, void *opaque) { RndRandom *s = RNG_RANDOM(b); + RngRequest *req; - if (s->receive_func) { - s->receive_func(s->opaque, NULL, 0); + req = g_malloc(sizeof(*req)); + + req->size = size; + req->receive_func = receive_entropy; + req->opaque = opaque; + + s->requests = g_slist_append(s->requests, req); + if (s->requests->data == req) { + qemu_set_fd_handler(s->fd, entropy_available, NULL, s); + } +} + +static void rng_random_free_requests(RndRandom *s) +{ + GSList *i; + + for (i = s->requests; i; i = i->next) { + g_free(i->data); } - s->receive_func = receive_entropy; - s->opaque = opaque; - s->size = size; + g_slist_free(s->requests); + s->requests = NULL; +} - qemu_set_fd_handler(s->fd, entropy_available, NULL, s); +static void rng_random_cancel_requests(RngBackend *b) +{ + RndRandom *s = RNG_RANDOM(b); + rng_random_free_requests(s); } static void rng_random_opened(RngBackend *b, Error **errp) @@ -129,6 +163,8 @@ static void rng_random_finalize(Object *obj) } g_free(s->filename); + + rng_random_free_requests(s); } static void rng_random_class_init(ObjectClass *klass, void *data) @@ -136,6 +172,7 @@ static void rng_random_class_init(ObjectClass *klass, void *data) RngBackendClass *rbc = RNG_BACKEND_CLASS(klass); rbc->request_entropy = rng_random_request_entropy; + rbc->cancel_requests = rng_random_cancel_requests; rbc->opened = rng_random_opened; }