From patchwork Wed Feb 10 15:53:25 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ladi Prosek X-Patchwork-Id: 8273081 Return-Path: X-Original-To: patchwork-qemu-devel@patchwork.kernel.org Delivered-To: patchwork-parsemail@patchwork1.web.kernel.org Received: from mail.kernel.org (mail.kernel.org [198.145.29.136]) by patchwork1.web.kernel.org (Postfix) with ESMTP id 4FCE19F6DA for ; Wed, 10 Feb 2016 15:56:40 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id 96E6820394 for ; Wed, 10 Feb 2016 15:56:39 +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 B485920398 for ; Wed, 10 Feb 2016 15:56:38 +0000 (UTC) Received: from localhost ([::1]:40478 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1aTX8A-0001D3-60 for patchwork-qemu-devel@patchwork.kernel.org; Wed, 10 Feb 2016 10:56:38 -0500 Received: from eggs.gnu.org ([2001:4830:134:3::10]:40505) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1aTX6K-0006pQ-Gr for qemu-devel@nongnu.org; Wed, 10 Feb 2016 10:54:46 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1aTX6I-0001MF-Iz for qemu-devel@nongnu.org; Wed, 10 Feb 2016 10:54:44 -0500 Received: from mx1.redhat.com ([209.132.183.28]:54929) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1aTX6H-0001Le-SM for qemu-devel@nongnu.org; Wed, 10 Feb 2016 10:54:42 -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 72B78C0A8493 for ; Wed, 10 Feb 2016 15:54:41 +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 u1AFsVYu031596; Wed, 10 Feb 2016 10:54:40 -0500 From: Ladi Prosek To: qemu-devel@nongnu.org Date: Wed, 10 Feb 2016 16:53:25 +0100 Message-Id: <1455119605-31261-5-git-send-email-lprosek@redhat.com> In-Reply-To: <1455119605-31261-1-git-send-email-lprosek@redhat.com> References: <1455119605-31261-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 Cc: amit.shah@redhat.com, pbonzini@redhat.com, Ladi Prosek , pagupta@redhat.com Subject: [Qemu-devel] [PATCH v2 4/4] rng: add request queue support to rng-random 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 Requests are now created in the RngBackend parent class and the code path is shared by both rng-egd and rng-random. This commit fixes the rng-random implementation which currently processes only one request at a time and simply discards all but the most recent one. In the guest this manifests as delayed completion of reads from virtio-rng, i.e. a read is completed only after another read is issued. Signed-off-by: Ladi Prosek --- backends/rng-egd.c | 16 ++-------------- backends/rng-random.c | 43 +++++++++++++++++++------------------------ backends/rng.c | 13 ++++++++++++- include/sysemu/rng.h | 3 +-- 4 files changed, 34 insertions(+), 41 deletions(-) diff --git a/backends/rng-egd.c b/backends/rng-egd.c index 8f2bd16..30332ed 100644 --- a/backends/rng-egd.c +++ b/backends/rng-egd.c @@ -27,20 +27,10 @@ typedef struct RngEgd char *chr_name; } RngEgd; -static void rng_egd_request_entropy(RngBackend *b, size_t size, - EntropyReceiveFunc *receive_entropy, - void *opaque) +static void rng_egd_request_entropy(RngBackend *b, RngRequest *req) { RngEgd *s = RNG_EGD(b); - RngRequest *req; - - req = g_malloc(sizeof(*req)); - - req->offset = 0; - req->size = size; - req->receive_entropy = receive_entropy; - req->opaque = opaque; - req->data = g_malloc(req->size); + size_t size = req->size; while (size > 0) { uint8_t header[2]; @@ -54,8 +44,6 @@ static void rng_egd_request_entropy(RngBackend *b, size_t size, size -= len; } - - s->parent.requests = g_slist_append(s->parent.requests, req); } static int rng_egd_chr_can_read(void *opaque) diff --git a/backends/rng-random.c b/backends/rng-random.c index 8cdad6a..a6cb385 100644 --- a/backends/rng-random.c +++ b/backends/rng-random.c @@ -22,10 +22,6 @@ struct RndRandom int fd; char *filename; - - EntropyReceiveFunc *receive_func; - void *opaque; - size_t size; }; /** @@ -38,36 +34,35 @@ 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; + while (s->parent.requests != NULL) { + RngRequest *req = s->parent.requests->data; + ssize_t len; + + len = read(s->fd, req->data, req->size); + if (len < 0 && errno == EAGAIN) { + return; + } + g_assert(len != -1); + + req->receive_entropy(req->opaque, req->data, len); + + rng_backend_finalize_request(&s->parent, req); } - g_assert(len != -1); - - s->receive_func(s->opaque, buffer, len); - s->receive_func = NULL; + /* We've drained all requests, the fd handler can be reset. */ qemu_set_fd_handler(s->fd, NULL, NULL, NULL); } -static void rng_random_request_entropy(RngBackend *b, size_t size, - EntropyReceiveFunc *receive_entropy, - void *opaque) +static void rng_random_request_entropy(RngBackend *b, RngRequest *req) { RndRandom *s = RNG_RANDOM(b); - if (s->receive_func) { - s->receive_func(s->opaque, NULL, 0); + if (s->parent.requests == NULL) { + /* If there are no pending requests yet, we need to + * install our fd handler. */ + qemu_set_fd_handler(s->fd, entropy_available, NULL, s); } - - s->receive_func = receive_entropy; - s->opaque = opaque; - s->size = size; - - qemu_set_fd_handler(s->fd, entropy_available, NULL, s); } static void rng_random_opened(RngBackend *b, Error **errp) diff --git a/backends/rng.c b/backends/rng.c index 014cb9d..277a41b 100644 --- a/backends/rng.c +++ b/backends/rng.c @@ -20,9 +20,20 @@ void rng_backend_request_entropy(RngBackend *s, size_t size, void *opaque) { RngBackendClass *k = RNG_BACKEND_GET_CLASS(s); + RngRequest *req; if (k->request_entropy) { - k->request_entropy(s, size, receive_entropy, opaque); + req = g_malloc(sizeof(*req)); + + req->offset = 0; + req->size = size; + req->receive_entropy = receive_entropy; + req->opaque = opaque; + req->data = g_malloc(req->size); + + k->request_entropy(s, req); + + s->requests = g_slist_append(s->requests, req); } } diff --git a/include/sysemu/rng.h b/include/sysemu/rng.h index c2c9035..a7ed580 100644 --- a/include/sysemu/rng.h +++ b/include/sysemu/rng.h @@ -46,8 +46,7 @@ struct RngBackendClass { ObjectClass parent_class; - void (*request_entropy)(RngBackend *s, size_t size, - EntropyReceiveFunc *receive_entropy, void *opaque); + void (*request_entropy)(RngBackend *s, RngRequest *req); void (*opened)(RngBackend *s, Error **errp); };