linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH v2] vringh: fix __vringh_iov() when riov and wiov are different
@ 2020-10-08 20:42 Stefano Garzarella
  2020-10-09  4:05 ` Jason Wang
  0 siblings, 1 reply; 3+ messages in thread
From: Stefano Garzarella @ 2020-10-08 20:42 UTC (permalink / raw)
  To: mst
  Cc: kvm, netdev, virtualization, linux-kernel, stable, Rusty Russell,
	Jason Wang

If riov and wiov are both defined and they point to different
objects, only riov is initialized. If the wiov is not initialized
by the caller, the function fails returning -EINVAL and printing
"Readable desc 0x... after writable" error message.

This issue happens when descriptors have both readable and writable
buffers (eg. virtio-blk devices has virtio_blk_outhdr in the readable
buffer and status as last byte of writable buffer) and we call
__vringh_iov() to get both type of buffers in two different iovecs.

Let's replace the 'else if' clause with 'if' to initialize both
riov and wiov if they are not NULL.

As checkpatch pointed out, we also avoid crashing the kernel
when riov and wiov are both NULL, replacing BUG() with WARN_ON()
and returning -EINVAL.

Fixes: f87d0fbb5798 ("vringh: host-side implementation of virtio rings.")
Cc: stable@vger.kernel.org
Signed-off-by: Stefano Garzarella <sgarzare@redhat.com>
---
 drivers/vhost/vringh.c | 9 +++++----
 1 file changed, 5 insertions(+), 4 deletions(-)

diff --git a/drivers/vhost/vringh.c b/drivers/vhost/vringh.c
index e059a9a47cdf..8bd8b403f087 100644
--- a/drivers/vhost/vringh.c
+++ b/drivers/vhost/vringh.c
@@ -284,13 +284,14 @@ __vringh_iov(struct vringh *vrh, u16 i,
 	desc_max = vrh->vring.num;
 	up_next = -1;
 
+	/* You must want something! */
+	if (WARN_ON(!riov && !wiov))
+		return -EINVAL;
+
 	if (riov)
 		riov->i = riov->used = 0;
-	else if (wiov)
+	if (wiov)
 		wiov->i = wiov->used = 0;
-	else
-		/* You must want something! */
-		BUG();
 
 	for (;;) {
 		void *addr;
-- 
2.26.2


^ permalink raw reply related	[flat|nested] 3+ messages in thread

* Re: [PATCH v2] vringh: fix __vringh_iov() when riov and wiov are different
  2020-10-08 20:42 [PATCH v2] vringh: fix __vringh_iov() when riov and wiov are different Stefano Garzarella
@ 2020-10-09  4:05 ` Jason Wang
  2020-10-09  7:00   ` Stefano Garzarella
  0 siblings, 1 reply; 3+ messages in thread
From: Jason Wang @ 2020-10-09  4:05 UTC (permalink / raw)
  To: Stefano Garzarella, mst
  Cc: kvm, netdev, virtualization, linux-kernel, stable, Rusty Russell


On 2020/10/9 上午4:42, Stefano Garzarella wrote:
> If riov and wiov are both defined and they point to different
> objects, only riov is initialized. If the wiov is not initialized
> by the caller, the function fails returning -EINVAL and printing
> "Readable desc 0x... after writable" error message.
>
> This issue happens when descriptors have both readable and writable
> buffers (eg. virtio-blk devices has virtio_blk_outhdr in the readable
> buffer and status as last byte of writable buffer) and we call
> __vringh_iov() to get both type of buffers in two different iovecs.
>
> Let's replace the 'else if' clause with 'if' to initialize both
> riov and wiov if they are not NULL.
>
> As checkpatch pointed out, we also avoid crashing the kernel
> when riov and wiov are both NULL, replacing BUG() with WARN_ON()
> and returning -EINVAL.


It looks like I met the exact similar issue when developing ctrl vq 
support (which requires both READ and WRITE descriptor).

While I was trying to fix the issue I found the following comment:

  * Note that you may need to clean up riov and wiov, even on error!
  */
int vringh_getdesc_iotlb(struct vringh *vrh,

I saw some driver call vringh_kiov_cleanup().

So I just follow to use that.

I'm not quite sure which one is better.

Thanks


>
> Fixes: f87d0fbb5798 ("vringh: host-side implementation of virtio rings.")
> Cc: stable@vger.kernel.org
> Signed-off-by: Stefano Garzarella <sgarzare@redhat.com>
> ---
>   drivers/vhost/vringh.c | 9 +++++----
>   1 file changed, 5 insertions(+), 4 deletions(-)
>
> diff --git a/drivers/vhost/vringh.c b/drivers/vhost/vringh.c
> index e059a9a47cdf..8bd8b403f087 100644
> --- a/drivers/vhost/vringh.c
> +++ b/drivers/vhost/vringh.c
> @@ -284,13 +284,14 @@ __vringh_iov(struct vringh *vrh, u16 i,
>   	desc_max = vrh->vring.num;
>   	up_next = -1;
>   
> +	/* You must want something! */
> +	if (WARN_ON(!riov && !wiov))
> +		return -EINVAL;
> +
>   	if (riov)
>   		riov->i = riov->used = 0;
> -	else if (wiov)
> +	if (wiov)
>   		wiov->i = wiov->used = 0;
> -	else
> -		/* You must want something! */
> -		BUG();
>   
>   	for (;;) {
>   		void *addr;


^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH v2] vringh: fix __vringh_iov() when riov and wiov are different
  2020-10-09  4:05 ` Jason Wang
@ 2020-10-09  7:00   ` Stefano Garzarella
  0 siblings, 0 replies; 3+ messages in thread
From: Stefano Garzarella @ 2020-10-09  7:00 UTC (permalink / raw)
  To: Jason Wang
  Cc: mst, kvm, netdev, virtualization, linux-kernel, stable, Rusty Russell

On Fri, Oct 09, 2020 at 12:05:15PM +0800, Jason Wang wrote:
> 
> On 2020/10/9 上午4:42, Stefano Garzarella wrote:
> > If riov and wiov are both defined and they point to different
> > objects, only riov is initialized. If the wiov is not initialized
> > by the caller, the function fails returning -EINVAL and printing
> > "Readable desc 0x... after writable" error message.
> > 
> > This issue happens when descriptors have both readable and writable
> > buffers (eg. virtio-blk devices has virtio_blk_outhdr in the readable
> > buffer and status as last byte of writable buffer) and we call
> > __vringh_iov() to get both type of buffers in two different iovecs.
> > 
> > Let's replace the 'else if' clause with 'if' to initialize both
> > riov and wiov if they are not NULL.
> > 
> > As checkpatch pointed out, we also avoid crashing the kernel
> > when riov and wiov are both NULL, replacing BUG() with WARN_ON()
> > and returning -EINVAL.
> 
> 
> It looks like I met the exact similar issue when developing ctrl vq support
> (which requires both READ and WRITE descriptor).
> 
> While I was trying to fix the issue I found the following comment:
> 
>  * Note that you may need to clean up riov and wiov, even on error!
>  */
> int vringh_getdesc_iotlb(struct vringh *vrh,

Thank you for pointing that out, I didn't see it!

> 
> I saw some driver call vringh_kiov_cleanup().
> 
> So I just follow to use that.
> 
> I'm not quite sure which one is better.

Me too, but in both cases the 'else if' is wrong.

Either we completely remove the reset in the function or we merge this patch.
In the first case, we should also fix drivers that don't call
vringh_kiov_cleanup() (e.g. vdpa_sim).

I'm fine with both.

Thanks,
Stefano

> 
> Thanks
> 
> 
> > 
> > Fixes: f87d0fbb5798 ("vringh: host-side implementation of virtio rings.")
> > Cc: stable@vger.kernel.org
> > Signed-off-by: Stefano Garzarella <sgarzare@redhat.com>
> > ---
> >   drivers/vhost/vringh.c | 9 +++++----
> >   1 file changed, 5 insertions(+), 4 deletions(-)
> > 
> > diff --git a/drivers/vhost/vringh.c b/drivers/vhost/vringh.c
> > index e059a9a47cdf..8bd8b403f087 100644
> > --- a/drivers/vhost/vringh.c
> > +++ b/drivers/vhost/vringh.c
> > @@ -284,13 +284,14 @@ __vringh_iov(struct vringh *vrh, u16 i,
> >   	desc_max = vrh->vring.num;
> >   	up_next = -1;
> > +	/* You must want something! */
> > +	if (WARN_ON(!riov && !wiov))
> > +		return -EINVAL;
> > +
> >   	if (riov)
> >   		riov->i = riov->used = 0;
> > -	else if (wiov)
> > +	if (wiov)
> >   		wiov->i = wiov->used = 0;
> > -	else
> > -		/* You must want something! */
> > -		BUG();
> >   	for (;;) {
> >   		void *addr;
> 


^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2020-10-09  7:00 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-10-08 20:42 [PATCH v2] vringh: fix __vringh_iov() when riov and wiov are different Stefano Garzarella
2020-10-09  4:05 ` Jason Wang
2020-10-09  7:00   ` Stefano Garzarella

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).