Received: by 2002:a05:6358:9144:b0:117:f937:c515 with SMTP id r4csp6703338rwr; Tue, 9 May 2023 20:31:55 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ4R6jobTV7/KTp4y88a5TQyMUhtx0iEOZq5cLUXXXo0ezO7GTMwGSUWQ4j7cnTyIHpQ4Ih0 X-Received: by 2002:a17:903:22c6:b0:1ac:939b:abdb with SMTP id y6-20020a17090322c600b001ac939babdbmr5373168plg.29.1683689515238; Tue, 09 May 2023 20:31:55 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1683689515; cv=none; d=google.com; s=arc-20160816; b=D9Dj/pRZnbfeqm7GiEup2KQNqoVP0RZBEbK+n/fHEKv706Ne4k0x8qgKiyIE9tcyCc p/FY4IAugRrn5M8id4WTluUu23x57rKmPQOzLQ92sZ3W4j5AuYr8FCbNd+tpI0d4AZr9 oO7gPFgt54fG1gF+O+c3cCxeVTk67SFLhCF09w0w5g1Lc42WVBqTnVNpYG/damk07sc1 7ZLR4MB02JlB8q8OMPlfzAjZ6vbVEp6ntB08ym5KQKDcgOmAmbNKhz1RGOdgspaXkYLL E9oI59d1FogZQGFhfkPEWZdaN1WXbscZhYtL6VbFrZdkDhugtvc5Dxorsnu2A8b0nbzY ASsg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:in-reply-to:references:cc:to:from:date:subject :message-id; bh=PpvkltYgH0QjacUJZC1MMPxqjgF59QPPm2t7DYGG9dw=; b=kq+NnmuQ0gShEdB5ROsX14oBxRlDDoTfjoTaQZDWXH1nQP6MOlfiCAGQ1HWG5cOJYt IWv2x9Xo60HaPJadkal+OEI+36pxjj64hj4nxMvPPk19oNvrrD/cbZP7jGurQdIjH93S 1YvV3kiI0+d8xJqdBdkbNqG6VZ2BYxHNo6XYqdsDHPnrtcEdD8XKy9JABg/gjFGS8Yxf O5nFTX5KhxUgKqPOzDyKsreHKDPKOJApOcDQ6tyLMGEvMb7KhsYWC++0HAIbp1Xkg9X2 lAPPmjW7+ebmZNd4J2QWCtTR/NZCUrYMATKSaaxqsoQPd/NaahaJLa0P1jAlSC3dLiWp aPdA== ARC-Authentication-Results: i=1; mx.google.com; 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=fail (p=NONE sp=NONE dis=NONE) header.from=alibaba.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id 28-20020a17090a1a1c00b002299b06dca9si15386937pjk.83.2023.05.09.20.31.40; Tue, 09 May 2023 20:31:55 -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; 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=fail (p=NONE sp=NONE dis=NONE) header.from=alibaba.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229556AbjEJDaB (ORCPT + 99 others); Tue, 9 May 2023 23:30:01 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:37254 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235121AbjEJD37 (ORCPT ); Tue, 9 May 2023 23:29:59 -0400 Received: from out30-113.freemail.mail.aliyun.com (out30-113.freemail.mail.aliyun.com [115.124.30.113]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 38B0391 for ; Tue, 9 May 2023 20:29:56 -0700 (PDT) X-Alimail-AntiSpam: AC=PASS;BC=-1|-1;BR=01201311R131e4;CH=green;DM=||false|;DS=||;FP=0|-1|-1|-1|0|-1|-1|-1;HT=ay29a033018046050;MF=xuanzhuo@linux.alibaba.com;NM=1;PH=DS;RN=6;SR=0;TI=SMTPD_---0ViDxAvy_1683689393; Received: from localhost(mailfrom:xuanzhuo@linux.alibaba.com fp:SMTPD_---0ViDxAvy_1683689393) by smtp.aliyun-inc.com; Wed, 10 May 2023 11:29:54 +0800 Message-ID: <1683689214.9647853-1-xuanzhuo@linux.alibaba.com> Subject: Re: [PATCH] virtio_ring: use u32 for virtio_max_dma_size Date: Wed, 10 May 2023 11:26:54 +0800 From: Xuan Zhuo To: zhenwei pi Cc: virtualization@lists.linux-foundation.org, linux-kernel@vger.kernel.org, zhenwei pi , Joerg Roedel , mst@redhat.com, jasowang@redhat.com References: <20230510025437.377807-1-pizhenwei@bytedance.com> In-Reply-To: <20230510025437.377807-1-pizhenwei@bytedance.com> X-Spam-Status: No, score=-9.9 required=5.0 tests=BAYES_00, ENV_AND_HDR_SPF_MATCH,RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H2, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE,UNPARSEABLE_RELAY, USER_IN_DEF_SPF_WL 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: X-Mailing-List: linux-kernel@vger.kernel.org On Wed, 10 May 2023 10:54:37 +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 > Signed-off-by: zhenwei pi > --- > 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 >