From patchwork Thu Mar 3 12:37:10 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Amit Shah X-Patchwork-Id: 8492411 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 50FFB9F9A0 for ; Thu, 3 Mar 2016 12:39:54 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id 8CF6120270 for ; Thu, 3 Mar 2016 12:39:52 +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 8300720295 for ; Thu, 3 Mar 2016 12:39:51 +0000 (UTC) Received: from localhost ([::1]:34659 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1abSXm-0000ra-OR for patchwork-qemu-devel@patchwork.kernel.org; Thu, 03 Mar 2016 07:39:50 -0500 Received: from eggs.gnu.org ([2001:4830:134:3::10]:39045) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1abSVj-0005wW-Bd for qemu-devel@nongnu.org; Thu, 03 Mar 2016 07:37:49 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1abSVg-0004VF-KF for qemu-devel@nongnu.org; Thu, 03 Mar 2016 07:37:43 -0500 Received: from mx1.redhat.com ([209.132.183.28]:42849) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1abSVg-0004Un-8B for qemu-devel@nongnu.org; Thu, 03 Mar 2016 07:37:40 -0500 Received: from int-mx13.intmail.prod.int.phx2.redhat.com (int-mx13.intmail.prod.int.phx2.redhat.com [10.5.11.26]) by mx1.redhat.com (Postfix) with ESMTPS id D5A2DC0005CF; Thu, 3 Mar 2016 12:37:39 +0000 (UTC) Received: from localhost (ovpn-113-82.phx2.redhat.com [10.3.113.82]) by int-mx13.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with ESMTP id u23Cbc0t021780; Thu, 3 Mar 2016 07:37:39 -0500 From: Amit Shah To: Peter Maydell Date: Thu, 3 Mar 2016 18:07:10 +0530 Message-Id: <60253ed1e6ec6d8e5ef2efe7bf755f475dce9956.1457008497.git.amit.shah@redhat.com> In-Reply-To: References: In-Reply-To: References: X-Scanned-By: MIMEDefang 2.68 on 10.5.11.26 X-detected-operating-system: by eggs.gnu.org: GNU/Linux 3.x X-Received-From: 209.132.183.28 Cc: Amit Shah , thuth@redhat.com, lprosek@redhat.com, qemu list Subject: [Qemu-devel] [PULL 5/6] 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 From: Ladi Prosek 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 processed only one request at a time and simply discarded all but the most recent one. In the guest this manifested as delayed completion of reads from virtio-rng, i.e. a read was completed only after another read was issued. By switching rng-random to use the same request queue as rng-egd, the unsafe stack-based allocation of the entropy buffer is eliminated and replaced with g_malloc. Signed-off-by: Ladi Prosek Reviewed-by: Amit Shah Message-Id: <1456994238-9585-5-git-send-email-lprosek@redhat.com> Signed-off-by: Amit Shah --- 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; - } - g_assert(len != -1); + 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); - s->receive_func(s->opaque, buffer, len); - s->receive_func = NULL; + req->receive_entropy(req->opaque, req->data, len); + rng_backend_finalize_request(&s->parent, req); + } + + /* 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 08a2eda..4fffd68 100644 --- a/include/sysemu/rng.h +++ b/include/sysemu/rng.h @@ -45,8 +45,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); };