Received: by 2002:a05:6a11:4021:0:0:0:0 with SMTP id ky33csp669526pxb; Wed, 22 Sep 2021 10:11:26 -0700 (PDT) X-Google-Smtp-Source: ABdhPJx+FW5v2WjodlC8MpNxyatNH/+GZOGbN0R5rzfqjQL1+DiOU1vs73Jk770TxM83ZS+HpQ4N X-Received: by 2002:a05:6e02:1cab:: with SMTP id x11mr110207ill.254.1632330685949; Wed, 22 Sep 2021 10:11:25 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1632330685; cv=none; d=google.com; s=arc-20160816; b=qtANqKPjjd61hmeAMqD0UoNOz0kJghvrdt8oVVDOOaHkA0CK5jL9kG9MPOf4xXiuR/ w0sRZ/w9CdNAEEiHD1h8g7CXwZbk+SHYpvoA23B/bNUrcHMIisv5KlHopKuvzWsE/3Og aXW3lNJYZQXNBQ8TLVHC1HPanxTkrviZE9iqWMdO+CoQHL94d6v2KNeCVxho/5cOdP/d dAUBSyM3raJW6Bicu0ae6xtgM22nklJ0WzDqxeKTR90L1qFgRcgOX4wAr9UfjzHnAlpY tjxZiWjLDCb5yUp1Cv3ZZys4M/W+G14bcls8AN8UOcdFcEf5b0hmXAs/51Bo+toge2ti +MWA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=BQsej0EuCL2beLZAb1gxjyy0OhJfKtFa7H9euSdki+w=; b=wBykQX2Dx9ycni1ArD+KmiJ2XOj0qRcyG5BxrMgv9K8js5Ivdel1WDK703wZZBputl W03npDJsJFzB7O+y24YWkh2s5uXjARlMb2QJWCjV+5nYtVHiJb/eS0Ouulsm7jAzH2Q0 hPnQEgGEPYxqUNSiSSqEynVsof8qU8ejvHa16az9fVma8fNogyk3tVc8x0fa6c1RJrAk QD/uOkC07esd+v+IutEN90bG0JwaYUdx1axEIaNEdwGn2C4hcgy1nX1TqLfeD+rCJnnA 8+AupLrcpfqSv0EVSELN+XMhPpr2vU2HIe213tloRnUHccj7iataCrHY5+9ifTq8gOM1 AjCQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b="KGGY/25b"; spf=pass (google.com: domain of linux-crypto-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-crypto-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id g6si1445009ila.86.2021.09.22.10.11.13; Wed, 22 Sep 2021 10:11:25 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-crypto-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b="KGGY/25b"; spf=pass (google.com: domain of linux-crypto-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-crypto-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S236747AbhIVRLD (ORCPT + 99 others); Wed, 22 Sep 2021 13:11:03 -0400 Received: from us-smtp-delivery-124.mimecast.com ([216.205.24.124]:49815 "EHLO us-smtp-delivery-124.mimecast.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S236838AbhIVRKy (ORCPT ); Wed, 22 Sep 2021 13:10:54 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1632330563; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=BQsej0EuCL2beLZAb1gxjyy0OhJfKtFa7H9euSdki+w=; b=KGGY/25bJPYLsK+sXDzgT4q+vDcxgUyHLOHELBcClcH9oanypt+dlzwGi9KtAKF4RnpNUs JHN3jOhyvnS9bl9/P6oxY+3xWczonaHY3YdWNtPePWupmvAcywsWhfoNaIr5F5t1MmjkdN 8eSs4xMrY6a6zK1aXUgWYElfeLqgoK0= Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-495-LNokeKBaN3iaxMNmJKsS3g-1; Wed, 22 Sep 2021 13:09:22 -0400 X-MC-Unique: LNokeKBaN3iaxMNmJKsS3g-1 Received: from smtp.corp.redhat.com (int-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.11]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id D266E79EDC; Wed, 22 Sep 2021 17:09:20 +0000 (UTC) Received: from thinkpad.redhat.com (unknown [10.39.192.105]) by smtp.corp.redhat.com (Postfix) with ESMTP id 50BFA652A1; Wed, 22 Sep 2021 17:09:18 +0000 (UTC) From: Laurent Vivier To: linux-kernel@vger.kernel.org Cc: Alexander Potapenko , linux-crypto@vger.kernel.org, Dmitriy Vyukov , rusty@rustcorp.com.au, amit@kernel.org, akong@redhat.com, Herbert Xu , "Michael S . Tsirkin" , Matt Mackall , virtualization@lists.linux-foundation.org, Laurent Vivier Subject: [PATCH 4/4] hwrng: virtio - always add a pending request Date: Wed, 22 Sep 2021 19:09:03 +0200 Message-Id: <20210922170903.577801-5-lvivier@redhat.com> In-Reply-To: <20210922170903.577801-1-lvivier@redhat.com> References: <20210922170903.577801-1-lvivier@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Scanned-By: MIMEDefang 2.79 on 10.5.11.11 Precedence: bulk List-ID: X-Mailing-List: linux-crypto@vger.kernel.org If we ensure we have already some data available by enqueuing again the buffer once data are exhausted, we can return what we have without waiting for the device answer. Signed-off-by: Laurent Vivier --- drivers/char/hw_random/virtio-rng.c | 24 ++++++++++-------------- 1 file changed, 10 insertions(+), 14 deletions(-) diff --git a/drivers/char/hw_random/virtio-rng.c b/drivers/char/hw_random/virtio-rng.c index 8ba97cf4ca8f..0b3c9b643495 100644 --- a/drivers/char/hw_random/virtio-rng.c +++ b/drivers/char/hw_random/virtio-rng.c @@ -20,7 +20,6 @@ struct virtrng_info { struct virtqueue *vq; char name[25]; int index; - bool busy; bool hwrng_register_done; bool hwrng_removed; /* data transfer */ @@ -44,16 +43,16 @@ static void random_recv_done(struct virtqueue *vq) return; vi->data_idx = 0; - vi->busy = false; complete(&vi->have_data); } -/* The host will fill any buffer we give it with sweet, sweet randomness. */ -static void register_buffer(struct virtrng_info *vi) +static void request_entropy(struct virtrng_info *vi) { struct scatterlist sg; + reinit_completion(&vi->have_data); + sg_init_one(&sg, vi->data, sizeof(vi->data)); /* There should always be room for one buffer. */ @@ -69,6 +68,8 @@ static unsigned int copy_data(struct virtrng_info *vi, void *buf, memcpy(buf, vi->data + vi->data_idx, size); vi->data_idx += size; vi->data_avail -= size; + if (vi->data_avail == 0) + request_entropy(vi); return size; } @@ -98,13 +99,7 @@ static int virtio_read(struct hwrng *rng, void *buf, size_t size, bool wait) * so either size is 0 or data_avail is 0 */ while (size != 0) { - /* data_avail is 0 */ - if (!vi->busy) { - /* no pending request, ask for more */ - vi->busy = true; - reinit_completion(&vi->have_data); - register_buffer(vi); - } + /* data_avail is 0 but a request is pending */ ret = wait_for_completion_killable(&vi->have_data); if (ret < 0) return ret; @@ -126,8 +121,7 @@ static void virtio_cleanup(struct hwrng *rng) { struct virtrng_info *vi = (struct virtrng_info *)rng->priv; - if (vi->busy) - complete(&vi->have_data); + complete(&vi->have_data); } static int probe_common(struct virtio_device *vdev) @@ -163,6 +157,9 @@ static int probe_common(struct virtio_device *vdev) goto err_find; } + /* we always have a pending entropy request */ + request_entropy(vi); + return 0; err_find: @@ -181,7 +178,6 @@ static void remove_common(struct virtio_device *vdev) vi->data_idx = 0; complete(&vi->have_data); vdev->config->reset(vdev); - vi->busy = false; if (vi->hwrng_register_done) hwrng_unregister(&vi->hwrng); vdev->config->del_vqs(vdev); -- 2.31.1