Received: by 2002:a05:6a11:4021:0:0:0:0 with SMTP id ky33csp296408pxb; Wed, 22 Sep 2021 23:26:38 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzaXJIZY2R1/qk+tmzbJTj2mLN9MQ1DpFYkacGSjsaJMYmcCwDDBRuFusm7aA5pz7ZVAbZU X-Received: by 2002:a05:6e02:1111:: with SMTP id u17mr2317576ilk.262.1632378398100; Wed, 22 Sep 2021 23:26:38 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1632378398; cv=none; d=google.com; s=arc-20160816; b=waJI8Q0QUT04b0sEEPEyn7KCCBVwRp6O48vyW6gNpoWdIXaiWkcb/aHIYHDbH+duWE zuTwHK9v8wQA8xwrPmpagsY4dMrkM9Bjc3tuSX4Jv8X/tzPLvytK92BZtLLAO66qim/Z nckUdAuLoRRbsO6SP7v3lUScfAHvw6k53pOaDbUcICwvuNN0ej3PwWxrkCyKtj/+Igzb B4gGJpVdqUFKQdYcLzBi7wehDGfi3hKoxFcGQCj1v+96ClNiHYrkaZT2NvzIx6C3KG7G GeiXY0hqavWEP9ELZSXAYs2yhUZh0UXDpKK2aOsCYtT5gGeKbztGKG/nE5wgxWeBqLmm EWjA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:in-reply-to:from :references:cc:to:content-language:subject:user-agent:mime-version :date:message-id:dkim-signature; bh=71fUOImGUODDyJCOfZ+X9xPf/a4pfZ4Afxj/CEjfaLk=; b=y29nXyJbSU+hjD6fWx+DqRVOXOo4GxwLnkEgIrJhxkF/OvUxLAHLkygXeCG7x9lNic uRn4p8wugIwaMecOp5oCzoHhetoJPAFGlnxo07HKcG/QnVZ4z9Yj+uzMn5xlYoREanRA WRan2CT/vpbyGKPWP9JQB/fIF+sD1f2CsIfxT94C3I8xWl0jJj4S8W2JbJbOgg5NFCbV omPAdBTAKrDRYhplwG4WxCMB7sgIX8zJHj6DHacZjXlIBYiMVA1P3aiiuS/zLdfTyazx RrNZdMT2QtG3uQfOc3nqVckVWnm4NnoUgWYsRw4N8K+uUYgU9bItjnm/nCkFWZvf1UY4 0J0g== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=f6E8+MZI; 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 p8si6027127ilo.5.2021.09.22.23.26.15; Wed, 22 Sep 2021 23:26:38 -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=f6E8+MZI; 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 S239299AbhIWG1o (ORCPT + 99 others); Thu, 23 Sep 2021 02:27:44 -0400 Received: from us-smtp-delivery-124.mimecast.com ([170.10.129.124]:24394 "EHLO us-smtp-delivery-124.mimecast.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S239284AbhIWG1o (ORCPT ); Thu, 23 Sep 2021 02:27:44 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1632378372; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=71fUOImGUODDyJCOfZ+X9xPf/a4pfZ4Afxj/CEjfaLk=; b=f6E8+MZIipTbD+3Ktb52dY80XXvlvWG3EkANabixyf8aF0Pw2vU4Ql0vuRGTWyIlsVyaK/ jxyUObYbJ7+ZVMZN52by4bFVokJZgJd08fDTGrE4T60nl02I/a645T6JircL2I/I9xHRQN gafOgpmeSiSmEKexlUUSiMRhQk78otE= Received: from mail-wr1-f71.google.com (mail-wr1-f71.google.com [209.85.221.71]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-35-AWU2ZR3VPbySghOIUnuHUg-1; Thu, 23 Sep 2021 02:26:11 -0400 X-MC-Unique: AWU2ZR3VPbySghOIUnuHUg-1 Received: by mail-wr1-f71.google.com with SMTP id x2-20020a5d54c2000000b0015dfd2b4e34so4248049wrv.6 for ; Wed, 22 Sep 2021 23:26:10 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:message-id:date:mime-version:user-agent:subject :content-language:to:cc:references:from:in-reply-to :content-transfer-encoding; bh=71fUOImGUODDyJCOfZ+X9xPf/a4pfZ4Afxj/CEjfaLk=; b=0bBDqwVPAtGyNSYWdK46yqrKVazUU2JMaAzSFOpT1bve7rcL2gRhkNABalPOWSVPCA h1NAbpz39x5qvxhvXYC9yDytla9Nb2txUj26ITcJRJjN4sYdarXU7xIAtgCarB3ptPWR Fv1zVvda5NZk8//lWkphnKN7GbT4Yt7o/AZlNHtdRWgxEgsE9eCIdbXBFIA2BxWLEa6f 71iq9HZxpajPQBFIkea5XzfmENXD1ZSofgpixbrHkSOvF5GXfxjZ6e0424m0uMqNL8Py sDS8NQu3y4KukuiSWx41GKJREBMK7HC9/xWGrI024kXbGgr/khNjgKtHrSB0QXxb9m5R hpLQ== X-Gm-Message-State: AOAM533id5HEVc7moHu6pLZvCVYkw/r/l3HPlXIu4XCJJwjlOnNJh7Wq PnmahGYc27o1tMOCuRCFdAMh7vxwgV/qAkxGgLcra4yQiDjIMr+akt48tCZ3rvSTnV+wciYjb5C Y6/7tuaMg8HgK7kk59u9jKOUf X-Received: by 2002:a05:600c:35d2:: with SMTP id r18mr14004169wmq.97.1632378369831; Wed, 22 Sep 2021 23:26:09 -0700 (PDT) X-Received: by 2002:a05:600c:35d2:: with SMTP id r18mr14004142wmq.97.1632378369633; Wed, 22 Sep 2021 23:26:09 -0700 (PDT) Received: from [192.168.100.42] ([82.142.21.142]) by smtp.gmail.com with ESMTPSA id v20sm4386301wra.73.2021.09.22.23.26.07 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Wed, 22 Sep 2021 23:26:08 -0700 (PDT) Message-ID: <0dd338bb-0fbe-b9d5-0962-d47ac2de4c4e@redhat.com> Date: Thu, 23 Sep 2021 08:26:06 +0200 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:91.0) Gecko/20100101 Thunderbird/91.1.0 Subject: Re: [PATCH 1/4] hwrng: virtio - add an internal buffer Content-Language: en-US To: "Michael S. Tsirkin" Cc: linux-kernel@vger.kernel.org, Alexander Potapenko , linux-crypto@vger.kernel.org, Dmitriy Vyukov , rusty@rustcorp.com.au, amit@kernel.org, akong@redhat.com, Herbert Xu , Matt Mackall , virtualization@lists.linux-foundation.org References: <20210922170903.577801-1-lvivier@redhat.com> <20210922170903.577801-2-lvivier@redhat.com> <20210922145651-mutt-send-email-mst@kernel.org> From: Laurent Vivier In-Reply-To: <20210922145651-mutt-send-email-mst@kernel.org> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit Precedence: bulk List-ID: X-Mailing-List: linux-crypto@vger.kernel.org On 22/09/2021 21:02, Michael S. Tsirkin wrote: > On Wed, Sep 22, 2021 at 07:09:00PM +0200, Laurent Vivier wrote: >> hwrng core uses two buffers that can be mixed in the >> virtio-rng queue. >> >> If the buffer is provided with wait=0 it is enqueued in the >> virtio-rng queue but unused by the caller. >> On the next call, core provides another buffer but the >> first one is filled instead and the new one queued. >> And the caller reads the data from the new one that is not >> updated, and the data in the first one are lost. >> >> To avoid this mix, virtio-rng needs to use its own unique >> internal buffer at a cost of a data copy to the caller buffer. >> >> Signed-off-by: Laurent Vivier >> --- >> drivers/char/hw_random/virtio-rng.c | 43 ++++++++++++++++++++++------- >> 1 file changed, 33 insertions(+), 10 deletions(-) >> >> diff --git a/drivers/char/hw_random/virtio-rng.c b/drivers/char/hw_random/virtio-rng.c >> index a90001e02bf7..208c547dcac1 100644 >> --- a/drivers/char/hw_random/virtio-rng.c >> +++ b/drivers/char/hw_random/virtio-rng.c >> @@ -18,13 +18,20 @@ static DEFINE_IDA(rng_index_ida); >> struct virtrng_info { >> struct hwrng hwrng; >> struct virtqueue *vq; >> - struct completion have_data; >> char name[25]; >> - unsigned int data_avail; >> int index; >> bool busy; >> bool hwrng_register_done; >> bool hwrng_removed; >> + /* data transfer */ >> + struct completion have_data; >> + unsigned int data_avail; >> + /* minimal size returned by rng_buffer_size() */ >> +#if SMP_CACHE_BYTES < 32 >> + u8 data[32]; >> +#else >> + u8 data[SMP_CACHE_BYTES]; >> +#endif > > Let's move this logic to a macro in hw_random.h ? > >> }; >> >> static void random_recv_done(struct virtqueue *vq) >> @@ -39,14 +46,14 @@ static void random_recv_done(struct virtqueue *vq) >> } >> >> /* The host will fill any buffer we give it with sweet, sweet randomness. */ >> -static void register_buffer(struct virtrng_info *vi, u8 *buf, size_t size) >> +static void register_buffer(struct virtrng_info *vi) >> { >> struct scatterlist sg; >> >> - sg_init_one(&sg, buf, size); >> + sg_init_one(&sg, vi->data, sizeof(vi->data)); > > Note that add_early_randomness requests less: > size_t size = min_t(size_t, 16, rng_buffer_size()); > > maybe track how much was requested and grow up to sizeof(data)? I think this problem is managed by PATCH 3/4 as we reuse unused data of the buffer. > >> >> /* There should always be room for one buffer. */ >> - virtqueue_add_inbuf(vi->vq, &sg, 1, buf, GFP_KERNEL); >> + virtqueue_add_inbuf(vi->vq, &sg, 1, vi->data, GFP_KERNEL); > > > BTW no longer true if DMA API is in use ... not easy to fix, > I think some changes to virtio API to allow pre-mapping > s/g for DMA might be needed ... Is there something I can do here? Thanks, Laurent