qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: Martin Wilck <mwilck@suse.com>
To: "Laurent Vivier" <lvivier@redhat.com>,
	"Philippe Mathieu-Daudé" <philmd@redhat.com>,
	"Michael S. Tsirkin" <mst@redhat.com>,
	"Jason Wang" <jasowang@redhat.com>
Cc: Amit Shah <amit@kernel.org>,
	qemu-devel@nongnu.org, virtualization@lists.linux-foundation.org
Subject: Re: [PATCH v2] virtio-rng: return available data with O_NONBLOCK
Date: Tue, 11 Aug 2020 14:53:41 +0200	[thread overview]
Message-ID: <b8b19be7fd9b3fab629506eb30d9f0c820aa57d2.camel@suse.com> (raw)
In-Reply-To: <b764a4bb-b81f-b14f-9b7f-d6d087a8b1ea@redhat.com>

On Tue, 2020-08-11 at 14:39 +0200, Laurent Vivier wrote:
> On 11/08/2020 14:22, Martin Wilck wrote:
> > On Tue, 2020-08-11 at 14:02 +0200, Laurent Vivier wrote:
> > > > >  drivers/char/hw_random/virtio-rng.c | 14 ++++++++++++++
> > > > >  1 file changed, 14 insertions(+)
> > > > > 
> > > > > diff --git a/drivers/char/hw_random/virtio-rng.c
> > > > > b/drivers/char/hw_random/virtio-rng.c
> > > > > index 79a6e47b5fbc..984713b35892 100644
> > > > > --- a/drivers/char/hw_random/virtio-rng.c
> > > > > +++ b/drivers/char/hw_random/virtio-rng.c
> > > > > @@ -59,6 +59,20 @@ static int virtio_read(struct hwrng *rng,
> > > > > void
> > > > > *buf, size_t size, bool wait)
> > > > >  	if (vi->hwrng_removed)
> > > > >  		return -ENODEV;
> > > > >  
> > > > > +	/*
> > > > > +	 * If the previous call was non-blocking, we may have
> > > > > got some
> > > > > +	 * randomness already.
> > > > > +	 */
> > > > > +	if (vi->busy && completion_done(&vi->have_data)) {
> > > > > +		unsigned int len;
> > > > > +
> > > > > +		vi->busy = false;
> > > > > +		len = vi->data_avail > size ? size : vi-
> > > > > >data_avail;
> > > > > +		vi->data_avail -= len;
> > > 
> > > You don't need to modify data_avail. As busy is set to false, the
> > > buffer
> > > will be reused. and it is always overwritten by
> > > virtqueue_get_buf().
> > > And moreover, if it was reused it would be always the beginning.
> > 
> > Ok.
> > 
> > > > > +		if (len)
> > > > > +			return len;
> > > > > +	}
> > > > > +
> > > > >  	if (!vi->busy) {
> > > > >  		vi->busy = true;
> > > > >  		reinit_completion(&vi->have_data);
> > > > > 
> > > 
> > > Why don't you modify only the wait case?
> > > 
> > > Something like:
> > > 
> > > 	if (!wait && !completion_done(&vi->have_data)) {
> > > 		return 0;
> > >         }
> > > 
> > > then at the end you can do "return min(size, vi->data_avail);".
> > 
> > Sorry, I don't understand what you mean. Where would you insert the
> > above "if" clause? Are you saying I should call
> > wait_for_completion_killable() also in the (!wait) case?
> 
> Yes, but only if a the completion is done, so it will not wait.
> 

Slowly getting there, thanks for your patience. Yes, I guess this would
work, too. I'll test and get back to you.

> > I must call check completion_done() before calling
> > reinit_completion().
> 
> Normally, the busy flag is here for that. If busy is true, a buffer
> is
> already registered. reinit_completion() must not be called if busy is
> true. busy becomes false when the before is ready to be reused.

My thinking was that once the completion is done, "busy" doesn't
reflect the actual state any more. But your idea is leaner and keeps
the semantics of "busy". I'll give it a try.

Thanks,
Martin




  reply	other threads:[~2020-08-11 12:54 UTC|newest]

Thread overview: 16+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-07-15 13:32 [PATCH v2] virtio-rng: return available data with O_NONBLOCK mwilck
2020-08-11 10:23 ` Reminder: " Martin Wilck
2020-08-11 10:37 ` Philippe Mathieu-Daudé
2020-08-11 12:02   ` Laurent Vivier
2020-08-11 12:22     ` Martin Wilck
2020-08-11 12:39       ` Laurent Vivier
2020-08-11 12:53         ` Martin Wilck [this message]
2020-08-11 13:00           ` Laurent Vivier
2020-08-11 13:14             ` Michael S. Tsirkin
2020-08-11 13:53               ` Laurent Vivier
2020-08-11 14:49                 ` Michael S. Tsirkin
2020-08-11 15:00                   ` Laurent Vivier
2020-08-11 14:12           ` Martin Wilck
2020-08-11 11:26 ` Michael S. Tsirkin
2020-08-11 12:07   ` Martin Wilck
2020-08-11 12:58     ` Michael S. Tsirkin

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=b8b19be7fd9b3fab629506eb30d9f0c820aa57d2.camel@suse.com \
    --to=mwilck@suse.com \
    --cc=amit@kernel.org \
    --cc=jasowang@redhat.com \
    --cc=lvivier@redhat.com \
    --cc=mst@redhat.com \
    --cc=philmd@redhat.com \
    --cc=qemu-devel@nongnu.org \
    --cc=virtualization@lists.linux-foundation.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).