virtualization.lists.linux-foundation.org archive mirror
 help / color / mirror / Atom feed
From: "Michael S. Tsirkin" <mst@redhat.com>
To: Xuan Zhuo <xuanzhuo@linux.alibaba.com>
Cc: virtualization@lists.linux.dev, Jason Wang <jasowang@redhat.com>
Subject: Re: [PATCH] virtio: packed: fix unmap leak for indirect desc table
Date: Thu, 22 Feb 2024 15:06:01 -0500	[thread overview]
Message-ID: <20240222150550-mutt-send-email-mst@kernel.org> (raw)
In-Reply-To: <20240201095618.2288-1-xuanzhuo@linux.alibaba.com>

On Thu, Feb 01, 2024 at 05:56:18PM +0800, Xuan Zhuo wrote:
> When use_dma_api and premapped are true, then the do_unmap is false.
> 
> Because the do_unmap is false, vring_unmap_extra_packed is not called by
> detach_buf_packed.
> 
>  if (unlikely(vq->do_unmap)) {
>                curr = id;
>                for (i = 0; i < state->num; i++) {
>                        vring_unmap_extra_packed(vq,
>                                                 &vq->packed.desc_extra[curr]);
>                        curr = vq->packed.desc_extra[curr].next;
>                }
>   }
> 
> So the indirect desc table is not unmapped. This causes the unmap leak.
> 
> On the other side, the dma addr and the flags is not updated. That
> will be used by vring_unmap_extra_packed.
> 
> For safety in this patch, the flags of desc without indirect flag is
> also updated.
> 
> This bug does not occur, because no driver use the premapped with
> indirect.
> 
> Fixes: b319940f83c2 ("virtio_ring: skip unmap for premapped")
> Signed-off-by: Xuan Zhuo <xuanzhuo@linux.alibaba.com>

were you going to post v2?

> ---
>  drivers/virtio/virtio_ring.c | 24 ++++++++++++------------
>  1 file changed, 12 insertions(+), 12 deletions(-)
> 
> diff --git a/drivers/virtio/virtio_ring.c b/drivers/virtio/virtio_ring.c
> index 49299b1f9ec7..097258a10819 100644
> --- a/drivers/virtio/virtio_ring.c
> +++ b/drivers/virtio/virtio_ring.c
> @@ -1340,14 +1340,15 @@ static int virtqueue_add_indirect_packed(struct vring_virtqueue *vq,
>  				sizeof(struct vring_packed_desc));
>  	vq->packed.vring.desc[head].id = cpu_to_le16(id);
>  
> -	if (vq->do_unmap) {
> +	if (vq->use_dma_api) {
>  		vq->packed.desc_extra[id].addr = addr;
>  		vq->packed.desc_extra[id].len = total_sg *
>  				sizeof(struct vring_packed_desc);
> -		vq->packed.desc_extra[id].flags = VRING_DESC_F_INDIRECT |
> -						  vq->packed.avail_used_flags;
>  	}
>  
> +	vq->packed.desc_extra[id].flags = VRING_DESC_F_INDIRECT |
> +		vq->packed.avail_used_flags;
> +
>  	/*
>  	 * A driver MUST NOT make the first descriptor in the list
>  	 * available before all subsequent descriptors comprising
> @@ -1484,9 +1485,10 @@ static inline int virtqueue_add_packed(struct virtqueue *_vq,
>  			if (unlikely(vq->do_unmap)) {
>  				vq->packed.desc_extra[curr].addr = addr;
>  				vq->packed.desc_extra[curr].len = sg->length;
> -				vq->packed.desc_extra[curr].flags =
> -					le16_to_cpu(flags);
>  			}
> +
> +			vq->packed.desc_extra[curr].flags = le16_to_cpu(flags);
> +
>  			prev = curr;
>  			curr = vq->packed.desc_extra[curr].next;
>  
> @@ -1615,13 +1617,11 @@ static void detach_buf_packed(struct vring_virtqueue *vq,
>  	vq->free_head = id;
>  	vq->vq.num_free += state->num;
>  
> -	if (unlikely(vq->do_unmap)) {
> -		curr = id;
> -		for (i = 0; i < state->num; i++) {
> -			vring_unmap_extra_packed(vq,
> -						 &vq->packed.desc_extra[curr]);
> -			curr = vq->packed.desc_extra[curr].next;
> -		}
> +	curr = id;
> +	for (i = 0; i < state->num; i++) {
> +		vring_unmap_extra_packed(vq,
> +					 &vq->packed.desc_extra[curr]);
> +		curr = vq->packed.desc_extra[curr].next;
>  	}
>  
>  	if (vq->indirect) {
> -- 
> 2.32.0.3.g01195cf9f


      parent reply	other threads:[~2024-02-22 20:06 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-02-01  9:56 [PATCH] virtio: packed: fix unmap leak for indirect desc table Xuan Zhuo
2024-02-02  3:46 ` Jason Wang
2024-02-02  5:56   ` Xuan Zhuo
2024-02-22 20:06 ` Michael S. Tsirkin [this message]

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=20240222150550-mutt-send-email-mst@kernel.org \
    --to=mst@redhat.com \
    --cc=jasowang@redhat.com \
    --cc=virtualization@lists.linux.dev \
    --cc=xuanzhuo@linux.alibaba.com \
    /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).