From: "Michael S. Tsirkin" <mst@redhat.com>
To: zhenwei pi <pizhenwei@bytedance.com>
Cc: xuanzhuo@linux.alibaba.com, Joerg Roedel <jroedel@suse.de>,
linux-kernel@vger.kernel.org,
virtualization@lists.linux-foundation.org
Subject: Re: [PATCH] virtio_ring: use u32 for virtio_max_dma_size
Date: Tue, 4 Jul 2023 02:21:23 -0400 [thread overview]
Message-ID: <20230704020935-mutt-send-email-mst@kernel.org> (raw)
In-Reply-To: <20230510025437.377807-1-pizhenwei@bytedance.com>
On Wed, May 10, 2023 at 10:54:37AM +0800, zhenwei pi wrote:
> Both split ring and packed ring use 32bits to describe the length of
> a descriptor: see struct vring_desc and struct vring_packed_desc.
> This means the max segment size supported by virtio is U32_MAX.
>
> An example of virtio_max_dma_size in virtio_blk.c:
> u32 v, max_size;
>
> max_size = virtio_max_dma_size(vdev); -> implicit convert
> err = virtio_cread_feature(vdev, VIRTIO_BLK_F_SIZE_MAX,
> struct virtio_blk_config, size_max, &v);
> max_size = min(max_size, v);
>
> There is a risk during implicit convert here, once virtio_max_dma_size
> returns 4G, max_size becomes 0.
>
> Fixes: e6d6dd6c875e ("virtio: Introduce virtio_max_dma_size()")
> Cc: Joerg Roedel <jroedel@suse.de>
> Signed-off-by: zhenwei pi <pizhenwei@bytedance.com>
> ---
> drivers/virtio/virtio_ring.c | 12 ++++++++----
> include/linux/virtio.h | 2 +-
> 2 files changed, 9 insertions(+), 5 deletions(-)
>
> diff --git a/drivers/virtio/virtio_ring.c b/drivers/virtio/virtio_ring.c
> index c5310eaf8b46..55cfecf030a1 100644
> --- a/drivers/virtio/virtio_ring.c
> +++ b/drivers/virtio/virtio_ring.c
> @@ -289,12 +289,16 @@ static bool vring_use_dma_api(const struct virtio_device *vdev)
> return false;
> }
>
> -size_t virtio_max_dma_size(const struct virtio_device *vdev)
> +u32 virtio_max_dma_size(const struct virtio_device *vdev)
> {
> - size_t max_segment_size = SIZE_MAX;
> + u32 max_segment_size = U32_MAX;
>
> - if (vring_use_dma_api(vdev))
> - max_segment_size = dma_max_mapping_size(vdev->dev.parent);
> + if (vring_use_dma_api(vdev)) {
> + size_t max_dma_size = dma_max_mapping_size(vdev->dev.parent);
> +
> + if (max_dma_size < max_segment_size)
> + max_segment_size = max_dma_size;
> + }
>
> return max_segment_size;
> }
Took a while for me to get this, it's confusing. I think the issue is
really in virtio blk, so I would just change max_size there to size_t
and be done with it.
> diff --git a/include/linux/virtio.h b/include/linux/virtio.h
> index b93238db94e3..1a605f408329 100644
> --- a/include/linux/virtio.h
> +++ b/include/linux/virtio.h
> @@ -147,7 +147,7 @@ int virtio_device_restore(struct virtio_device *dev);
> #endif
> void virtio_reset_device(struct virtio_device *dev);
>
> -size_t virtio_max_dma_size(const struct virtio_device *vdev);
> +u32 virtio_max_dma_size(const struct virtio_device *vdev);
>
> #define virtio_device_for_each_vq(vdev, vq) \
> list_for_each_entry(vq, &vdev->vqs, list)
> --
> 2.20.1
_______________________________________________
Virtualization mailing list
Virtualization@lists.linux-foundation.org
https://lists.linuxfoundation.org/mailman/listinfo/virtualization
WARNING: multiple messages have this Message-ID (diff)
From: "Michael S. Tsirkin" <mst@redhat.com>
To: zhenwei pi <pizhenwei@bytedance.com>
Cc: jasowang@redhat.com, virtualization@lists.linux-foundation.org,
linux-kernel@vger.kernel.org, xuanzhuo@linux.alibaba.com,
Joerg Roedel <jroedel@suse.de>
Subject: Re: [PATCH] virtio_ring: use u32 for virtio_max_dma_size
Date: Tue, 4 Jul 2023 02:21:23 -0400 [thread overview]
Message-ID: <20230704020935-mutt-send-email-mst@kernel.org> (raw)
In-Reply-To: <20230510025437.377807-1-pizhenwei@bytedance.com>
On Wed, May 10, 2023 at 10:54:37AM +0800, zhenwei pi wrote:
> Both split ring and packed ring use 32bits to describe the length of
> a descriptor: see struct vring_desc and struct vring_packed_desc.
> This means the max segment size supported by virtio is U32_MAX.
>
> An example of virtio_max_dma_size in virtio_blk.c:
> u32 v, max_size;
>
> max_size = virtio_max_dma_size(vdev); -> implicit convert
> err = virtio_cread_feature(vdev, VIRTIO_BLK_F_SIZE_MAX,
> struct virtio_blk_config, size_max, &v);
> max_size = min(max_size, v);
>
> There is a risk during implicit convert here, once virtio_max_dma_size
> returns 4G, max_size becomes 0.
>
> Fixes: e6d6dd6c875e ("virtio: Introduce virtio_max_dma_size()")
> Cc: Joerg Roedel <jroedel@suse.de>
> Signed-off-by: zhenwei pi <pizhenwei@bytedance.com>
> ---
> drivers/virtio/virtio_ring.c | 12 ++++++++----
> include/linux/virtio.h | 2 +-
> 2 files changed, 9 insertions(+), 5 deletions(-)
>
> diff --git a/drivers/virtio/virtio_ring.c b/drivers/virtio/virtio_ring.c
> index c5310eaf8b46..55cfecf030a1 100644
> --- a/drivers/virtio/virtio_ring.c
> +++ b/drivers/virtio/virtio_ring.c
> @@ -289,12 +289,16 @@ static bool vring_use_dma_api(const struct virtio_device *vdev)
> return false;
> }
>
> -size_t virtio_max_dma_size(const struct virtio_device *vdev)
> +u32 virtio_max_dma_size(const struct virtio_device *vdev)
> {
> - size_t max_segment_size = SIZE_MAX;
> + u32 max_segment_size = U32_MAX;
>
> - if (vring_use_dma_api(vdev))
> - max_segment_size = dma_max_mapping_size(vdev->dev.parent);
> + if (vring_use_dma_api(vdev)) {
> + size_t max_dma_size = dma_max_mapping_size(vdev->dev.parent);
> +
> + if (max_dma_size < max_segment_size)
> + max_segment_size = max_dma_size;
> + }
>
> return max_segment_size;
> }
Took a while for me to get this, it's confusing. I think the issue is
really in virtio blk, so I would just change max_size there to size_t
and be done with it.
> diff --git a/include/linux/virtio.h b/include/linux/virtio.h
> index b93238db94e3..1a605f408329 100644
> --- a/include/linux/virtio.h
> +++ b/include/linux/virtio.h
> @@ -147,7 +147,7 @@ int virtio_device_restore(struct virtio_device *dev);
> #endif
> void virtio_reset_device(struct virtio_device *dev);
>
> -size_t virtio_max_dma_size(const struct virtio_device *vdev);
> +u32 virtio_max_dma_size(const struct virtio_device *vdev);
>
> #define virtio_device_for_each_vq(vdev, vq) \
> list_for_each_entry(vq, &vdev->vqs, list)
> --
> 2.20.1
next prev parent reply other threads:[~2023-07-04 6:21 UTC|newest]
Thread overview: 22+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-05-10 2:54 [PATCH] virtio_ring: use u32 for virtio_max_dma_size zhenwei pi via Virtualization
2023-05-10 2:54 ` zhenwei pi
2023-05-10 3:26 ` Xuan Zhuo
2023-05-10 3:26 ` Xuan Zhuo
2023-05-10 3:39 ` Michael S. Tsirkin
2023-05-10 3:39 ` Michael S. Tsirkin
2023-05-10 4:04 ` Jason Wang
2023-05-10 4:04 ` Jason Wang
2023-05-10 4:06 ` Michael S. Tsirkin
2023-05-10 4:06 ` Michael S. Tsirkin
2023-07-04 0:18 ` PING " zhenwei pi via Virtualization
2023-07-04 0:18 ` zhenwei pi
2023-05-10 7:52 ` zhenwei pi via Virtualization
2023-05-10 7:52 ` zhenwei pi
2023-05-10 3:39 ` Michael S. Tsirkin
2023-05-10 3:39 ` Michael S. Tsirkin
2023-05-10 3:41 ` zhenwei pi via Virtualization
2023-05-10 3:41 ` zhenwei pi
2023-07-04 6:21 ` Michael S. Tsirkin [this message]
2023-07-04 6:21 ` Michael S. Tsirkin
2023-07-04 7:46 ` zhenwei pi via Virtualization
2023-07-04 7:46 ` zhenwei pi
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=20230704020935-mutt-send-email-mst@kernel.org \
--to=mst@redhat.com \
--cc=jroedel@suse.de \
--cc=linux-kernel@vger.kernel.org \
--cc=pizhenwei@bytedance.com \
--cc=virtualization@lists.linux-foundation.org \
--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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.