Message ID | 20230510025437.377807-1-pizhenwei@bytedance.com |
---|---|
State | New |
Headers |
Return-Path: <linux-kernel-owner@vger.kernel.org> Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:b0ea:0:b0:3b6:4342:cba0 with SMTP id b10csp3333546vqo; Tue, 9 May 2023 20:21:10 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ6qLkfhYuxuXe++zq26KBfmM5AcIEaqO9Nda0DgjQBVmsa1UQQ53gIE9CBkC+bfN0u21tq/ X-Received: by 2002:a05:6a20:3ca9:b0:100:1044:9ce2 with SMTP id b41-20020a056a203ca900b0010010449ce2mr13840263pzj.56.1683688870485; Tue, 09 May 2023 20:21:10 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1683688870; cv=none; d=google.com; s=arc-20160816; b=bnqF+P+JbZWKUQJKgIvKYdrdkNh7M/+K+Pj4BQ90blCrIC4Vk4FOnAOcqjkMtSV/if 3e7LAe0oJ5QmSTH0S//5qBQjoqofqIpCCZsvoK0pO/2uG2h56Ct8XNXjFUNHRbon9q1U sSKVDVLlSewZCDpnOxSz5Q7IfiWMyk3p/4Q8Tt+0UTJKW+b+BhYcEiD+GOM0WRcabztr j5NeDQ9wL/ZS4KuUZ3zKakbj2qkC3X2E8tyemLoUe/Im0l8ojYMXyjTXK1nRxvRIjRGE K/JLvNAp9BcRZG3XE0JhrdXyEnJM1d0PKrECovPJT60qBUbI/E1F9HxXALHVM6aHUmu8 dweg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :message-id:date:subject:cc:to:from:dkim-signature; bh=hTSesuixdo5vZ8I9+v28R7iSHBf2PWhulvCTlSJ6srA=; b=ETwLXHYZMcfu6tb+iBPTF0M1xwTvNNLQUi+0sYXzdCyTiRYcLUU4TW/EJZmZh+K90J JcgAWAFWco0JOyDmFiFRA7ZFdxYZwSiZ3XrMW7GnIsKHMIeI+xadrxoN+u9/YAopRBsP QXVqJ75IrKczBU59lmvpDFRGye2I8Qs+cIsqrwyke933dxNfuLGOGOED4lzJxHPTtW3o m6H7ITN0acqGCFPGK/bO31BX6jVzpsklw3TN2HXjW6nKupWafRXVIo0bIg1FdbDNKZTF zqtWdNKNWA3btNYhX7Bonj3I0ttnQM3kow2WVTXsW+53eocQmKtEwkRgrxYy20mQa3Um aZLQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@bytedance.com header.s=google header.b=AJvosUCn; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=bytedance.com Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id i30-20020a63585e000000b0052856cec94asi3004308pgm.884.2023.05.09.20.20.54; Tue, 09 May 2023 20:21:10 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@bytedance.com header.s=google header.b=AJvosUCn; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=bytedance.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S235177AbjEJC5J (ORCPT <rfc822;baris.duru.linux@gmail.com> + 99 others); Tue, 9 May 2023 22:57:09 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:50036 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229536AbjEJC5I (ORCPT <rfc822;linux-kernel@vger.kernel.org>); Tue, 9 May 2023 22:57:08 -0400 Received: from mail-pf1-x432.google.com (mail-pf1-x432.google.com [IPv6:2607:f8b0:4864:20::432]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 2533030FF for <linux-kernel@vger.kernel.org>; Tue, 9 May 2023 19:56:42 -0700 (PDT) Received: by mail-pf1-x432.google.com with SMTP id d2e1a72fcca58-643b7b8f8ceso3042328b3a.1 for <linux-kernel@vger.kernel.org>; Tue, 09 May 2023 19:56:42 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bytedance.com; s=google; t=1683687401; x=1686279401; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=hTSesuixdo5vZ8I9+v28R7iSHBf2PWhulvCTlSJ6srA=; b=AJvosUCnaL7DVrTLt0wEHVdYm9nezT1Xn8WsRYZ5yIuyP3499YqOnLadpQLSAn9yt2 05U+onyUhUO0mNusq+LU34DX+jo3WaPD3xduRVgLNMAF77mNjHIkNOYFHbwMi2ZH2trk WPZ9SWFvn+9Oms0zV++Xge+Rah4vggZDy/1Uo3NnXa+zOnJLgi0MH0+/ob20TT8QWfu1 +J9vUcfhAUXHBr43YbyXF3BjQRTXP98x9z9WWJpyRvW/zsSk21J5ZhzoJQPAtU2vN7IN xvHx8OiV3wiV9PenK6h+w2quENNK8h/hjJOEtp1BW6HfgIuiAPKYOvDkMTW2ow/8lFG0 T2vQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1683687401; x=1686279401; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=hTSesuixdo5vZ8I9+v28R7iSHBf2PWhulvCTlSJ6srA=; b=j4NY28Z1HqdLcqcacwLzargonPtf9TE/AHTbEwNWSJyEqJa5a8I501bojnrmYxiiLb ajkn7UQA+Mx1HGcztfcoJa3YZS1fNMRrxDJudStERpZHTYWJsGJBtx9Ioossp65X0/dm aGxisfeSGCwEJEZjrZeFwEsCVmH+ehUVBLOovetEIXvypyB7bz0BzsdFhqVR/QvqoXR5 MkhdM/xp3vSKy7Zn6s5ZfBfbhP0zmj9Xj/HhDiW4nIWkME6Jmj6eWMWgM1FTXP7KOteV t00bWpDNVSVloxS54eS72lXiuadH/eFyLXfkr/hG8Cy0pV83m4+Mk3JMZdZPtmqP6xNP DSnA== X-Gm-Message-State: AC+VfDxo9+Yql+hBblAXc9cxVn6M8XZQ41151RmpTVx/g4LIvi8tlkgt Akzhht9kEjv92CZffqnMr2K8Vw== X-Received: by 2002:a05:6a20:4421:b0:101:914e:da75 with SMTP id ce33-20020a056a20442100b00101914eda75mr3401063pzb.25.1683687401584; Tue, 09 May 2023 19:56:41 -0700 (PDT) Received: from always-x1.bytedance.net ([61.213.176.13]) by smtp.gmail.com with ESMTPSA id h13-20020a63e14d000000b00524cfd635cesm2083080pgk.69.2023.05.09.19.56.38 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 09 May 2023 19:56:41 -0700 (PDT) From: zhenwei pi <pizhenwei@bytedance.com> To: mst@redhat.com, jasowang@redhat.com Cc: virtualization@lists.linux-foundation.org, linux-kernel@vger.kernel.org, xuanzhuo@linux.alibaba.com, zhenwei pi <pizhenwei@bytedance.com>, Joerg Roedel <jroedel@suse.de> Subject: [PATCH] virtio_ring: use u32 for virtio_max_dma_size Date: Wed, 10 May 2023 10:54:37 +0800 Message-Id: <20230510025437.377807-1-pizhenwei@bytedance.com> X-Mailer: git-send-email 2.25.1 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE,URIBL_BLOCKED autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: <linux-kernel.vger.kernel.org> X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1765475741418976836?= X-GMAIL-MSGID: =?utf-8?q?1765475741418976836?= |
Series |
virtio_ring: use u32 for virtio_max_dma_size
|
|
Commit Message
zhenwei pi
May 10, 2023, 2:54 a.m. UTC
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(-)
Comments
On Wed, 10 May 2023 10:54:37 +0800, zhenwei pi <pizhenwei@bytedance.com> 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) LGTM But, should we change the parameter to vq, then use the dma_dev? @Jason Thanks. > { > - 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; > } > 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 >
On Wed, May 10, 2023 at 11:26:54AM +0800, Xuan Zhuo wrote: > On Wed, 10 May 2023 10:54:37 +0800, zhenwei pi <pizhenwei@bytedance.com> 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) > > > LGTM > > But, should we change the parameter to vq, then use the dma_dev? > > @Jason > > Thanks. > that would be an unrelated rework. > > { > > - 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; > > } > > 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 > >
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> is this a theoretical concern or do you manage to trigger this somehow? > --- > 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; > } > 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
On 5/10/23 11:39, Michael S. Tsirkin wrote: > 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> > > > is this a theoretical concern or do you manage to trigger this > somehow? > I never hit any issue about this, I notice here during diving into the symbols exported by virtio_ring.ko. >> --- >> 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; >> } >> 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 >
On Wed, May 10, 2023 at 11:44 AM Michael S. Tsirkin <mst@redhat.com> wrote: > > On Wed, May 10, 2023 at 11:26:54AM +0800, Xuan Zhuo wrote: > > On Wed, 10 May 2023 10:54:37 +0800, zhenwei pi <pizhenwei@bytedance.com> 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) > > > > > > LGTM > > > > But, should we change the parameter to vq, then use the dma_dev? > > > > @Jason > > > > Thanks. > > > > > that would be an unrelated rework. Probably, but I think it's better to be done on top otherwise we may forget. Thanks > > > > { > > > - 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; > > > } > > > 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 > > > >
On Wed, May 10, 2023 at 12:04:50PM +0800, Jason Wang wrote: > On Wed, May 10, 2023 at 11:44 AM Michael S. Tsirkin <mst@redhat.com> wrote: > > > > On Wed, May 10, 2023 at 11:26:54AM +0800, Xuan Zhuo wrote: > > > On Wed, 10 May 2023 10:54:37 +0800, zhenwei pi <pizhenwei@bytedance.com> 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) > > > > > > > > > LGTM > > > > > > But, should we change the parameter to vq, then use the dma_dev? > > > > > > @Jason > > > > > > Thanks. > > > > > > > > > that would be an unrelated rework. > > Probably, but I think it's better to be done on top otherwise we may forget. > > Thanks Just to make things clear I'm merging fixes for this release but cleanups belong in the next one. > > > > > > { > > > > - 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; > > > > } > > > > 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 > > > > > >
On 5/10/23 11:26, Xuan Zhuo wrote: > On Wed, 10 May 2023 10:54:37 +0800, zhenwei pi <pizhenwei@bytedance.com> 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) > > > LGTM > > But, should we change the parameter to vq, then use the dma_dev? > > @Jason > > Thanks. > The max DMA size is a attribute of a virtio device rather than any VQ, so I guess virtio_max_dma_size(const struct virtio_device *vdev) is clear. On the other hand, if changing the parameter to vq, we need select a VQ, then the question is: 1, which VQ to select? VQ0 or a random one? this leads confusing. 2, The virtio spec defines: Each device can have zero or more virtqueues > >> { >> - 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; >> } >> 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 >>
Hi Michael, This seems to be ignored... On 5/10/23 12:06, Michael S. Tsirkin wrote: > On Wed, May 10, 2023 at 12:04:50PM +0800, Jason Wang wrote: >> On Wed, May 10, 2023 at 11:44 AM Michael S. Tsirkin <mst@redhat.com> wrote: >>> >>> On Wed, May 10, 2023 at 11:26:54AM +0800, Xuan Zhuo wrote: >>>> On Wed, 10 May 2023 10:54:37 +0800, zhenwei pi <pizhenwei@bytedance.com> 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) >>>> >>>> >>>> LGTM >>>> >>>> But, should we change the parameter to vq, then use the dma_dev? >>>> >>>> @Jason >>>> >>>> Thanks. >>>> >>> >>> >>> that would be an unrelated rework. >> >> Probably, but I think it's better to be done on top otherwise we may forget. >> >> Thanks > > Just to make things clear I'm merging fixes for this > release but cleanups belong in the next one. > >>> >>>>> { >>>>> - 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; >>>>> } >>>>> 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 >>>>> >>> >
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
On 7/4/23 14:21, Michael S. Tsirkin wrote: > 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. > > Fine. > > >> 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 >
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; } 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)