Received: by 2002:a05:6a10:a0d1:0:0:0:0 with SMTP id j17csp1374687pxa; Sat, 22 Aug 2020 23:58:54 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyOiWQ8jyEOy6M0aqNONXksoRULeeu6gr2RXxwH+hHV5+aZdFvnuZY/Y24kCBx4qTw6IBb7 X-Received: by 2002:a17:906:76c7:: with SMTP id q7mr338515ejn.541.1598165934564; Sat, 22 Aug 2020 23:58:54 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1598165934; cv=none; d=google.com; s=arc-20160816; b=fW0f194BuhUsSdQviyubhnSMwjHJ+aq7ltv1F1RmSfkKthnh6fkqlAch8G6p2BzUek fiznoLDYV32JAT09J8VzbJ/cj7HqRCBgD6Qo0hjOuJ1BRDTwfVJsBcl5uWbGPffdyDly kWYZOP5p4NwuxmO+N051REBDNiBnUhAzNDYnJE2mHt5ib+81uwzU1bm2Ww5p1D15Rtd5 kNVi+4hqXu7CAn+QGZrKNLTxFy5APvQcAUsHkNmO2k+YBOm3BRrevpwUFrJAIcdmWt6x BIyQqfkGIBIttZQWxgp9SkTRl5nU+2k29+NcpYcdjtyLD6X5fZ4cdmx+X0jBbtQZYSnx vw6w== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:dkim-signature:user-agent:in-reply-to :content-disposition:mime-version:references:message-id:subject:cc :to:from:date; bh=UiD8dJXe+GsLHU8pN9btsFs9/XV7cIHrvGbnOBAFGPU=; b=uRGNUdK46fAfhhNekq/bhFaH5cUjzDy2eElgPEgPOnE8yUB0St9z+D1kqTAfoo89Vp F0oB1OUILlLozdVyDFwgf1u2tKSn22VFqFvuHWJ6sf80Wejc98J9NyxaKnfDz1l5Lg6g RU84mqs/FliA0ghVxcx+IXzWgCNwr84jAwF1eY7hiW4LMz2hAiwq1aph3z5aBtAIdqb+ +shwX/VbzgxEOg+ZTYQF6QEchRYLkhWiy8NSEOM4WB9/nKLRqFvKdXoqExHv+1X2n8H8 wdL8fQ7zmB3QEQc13AZ0se6bIL11qPn5pCglZhw8Ttra7V8cQGBAGpSVT85Fk6nj7gwJ P9hA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@nvidia.com header.s=n1 header.b=pnz8g2ef; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=nvidia.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id e15si4693354ejz.137.2020.08.22.23.58.31; Sat, 22 Aug 2020 23:58:54 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@nvidia.com header.s=n1 header.b=pnz8g2ef; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=nvidia.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726075AbgHWGlK (ORCPT + 99 others); Sun, 23 Aug 2020 02:41:10 -0400 Received: from hqnvemgate26.nvidia.com ([216.228.121.65]:18685 "EHLO hqnvemgate26.nvidia.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1725372AbgHWGlH (ORCPT ); Sun, 23 Aug 2020 02:41:07 -0400 Received: from hqpgpgate101.nvidia.com (Not Verified[216.228.121.13]) by hqnvemgate26.nvidia.com (using TLS: TLSv1.2, DES-CBC3-SHA) id ; Sat, 22 Aug 2020 23:40:53 -0700 Received: from hqmail.nvidia.com ([172.20.161.6]) by hqpgpgate101.nvidia.com (PGP Universal service); Sat, 22 Aug 2020 23:41:07 -0700 X-PGP-Universal: processed; by hqpgpgate101.nvidia.com on Sat, 22 Aug 2020 23:41:07 -0700 Received: from mtl-vdi-166.wap.labs.mlnx (10.124.1.5) by HQMAIL107.nvidia.com (172.20.187.13) with Microsoft SMTP Server (TLS) id 15.0.1473.3; Sun, 23 Aug 2020 06:40:39 +0000 Date: Sun, 23 Aug 2020 09:40:36 +0300 From: Eli Cohen To: Jason Wang CC: , , , , , , , , , , , , , , Subject: Re: [PATCH V2 2/3] vhost: vdpa: report iova range Message-ID: <20200823064035.GB147797@mtl-vdi-166.wap.labs.mlnx> References: <20200821092813.8952-1-jasowang@redhat.com> <20200821092813.8952-3-jasowang@redhat.com> MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-Disposition: inline In-Reply-To: <20200821092813.8952-3-jasowang@redhat.com> User-Agent: Mutt/1.9.5 (bf161cf53efb) (2018-04-13) X-Originating-IP: [10.124.1.5] X-ClientProxiedBy: HQMAIL105.nvidia.com (172.20.187.12) To HQMAIL107.nvidia.com (172.20.187.13) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=nvidia.com; s=n1; t=1598164853; bh=UiD8dJXe+GsLHU8pN9btsFs9/XV7cIHrvGbnOBAFGPU=; h=X-PGP-Universal:Date:From:To:CC:Subject:Message-ID:References: MIME-Version:Content-Type:Content-Disposition:In-Reply-To: User-Agent:X-Originating-IP:X-ClientProxiedBy; b=pnz8g2efLGRimNp8lVrxsQ2m5FibE80h8lYBFZALVSm1w7qPUUdqcHZfTJWkJ9/i2 +5UC22KO67n5pzyOgiwRwSMWLIFUoRYGceulSVqJ3MlkkCAW+TjNd6VDB69iBPLHWg Kf9BqO2G0WeJEp3Yu/MTK+5z8wjztuWJbEtrZjpZQpfnj8sBNt9x9P1c6ISUuPhILh xxovu8NL2v5w1za0BrZddeqyuV9kMV+VdC6f2yTlIoXeLq5ETXvF/x1wxOF+KIcnrg atnjdiKMsel6dxUdn412VZXLePhEi0S+aiiInd2pq4MWSUWh79/r/Wd8/nbZHzLhiK DR4gmNQ8+oUWg== Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Fri, Aug 21, 2020 at 05:28:12AM -0400, Jason Wang wrote: > This patch introduces a new ioctl for vhost-vdpa device that can > report the iova range by the device. > > For device that implements get_iova_range() method, we fetch it from > the vDPA device. If device doesn't implement get_iova_range() but > depends on platform IOMMU, we will query via DOMAIN_ATTR_GEOMETRY, > otherwise [0, ULLONG_MAX] is assumed. > > For safety, this patch also rules out the map request which is not in > the valid range. > > Signed-off-by: Jason Wang > --- > drivers/vhost/vdpa.c | 41 ++++++++++++++++++++++++++++++++ > include/uapi/linux/vhost.h | 4 ++++ > include/uapi/linux/vhost_types.h | 9 +++++++ > 3 files changed, 54 insertions(+) > > diff --git a/drivers/vhost/vdpa.c b/drivers/vhost/vdpa.c > index 3fab94f88894..1adb4adb0345 100644 > --- a/drivers/vhost/vdpa.c > +++ b/drivers/vhost/vdpa.c > @@ -48,6 +48,7 @@ struct vhost_vdpa { > int minor; > struct eventfd_ctx *config_ctx; > int in_batch; > + struct vdpa_iova_range range; > }; > > static DEFINE_IDA(vhost_vdpa_ida); > @@ -344,6 +345,16 @@ static long vhost_vdpa_set_config_call(struct vhost_vdpa *v, u32 __user *argp) > return 0; > } > > +static long vhost_vdpa_get_iova_range(struct vhost_vdpa *v, u32 __user *argp) > +{ > + struct vhost_vdpa_iova_range range = { > + .first = v->range.first, > + .last = v->range.last, > + }; > + > + return copy_to_user(argp, &range, sizeof(range)); > +} > + > static long vhost_vdpa_vring_ioctl(struct vhost_vdpa *v, unsigned int cmd, > void __user *argp) > { > @@ -476,6 +487,9 @@ static long vhost_vdpa_unlocked_ioctl(struct file *filep, > case VHOST_VDPA_SET_CONFIG_CALL: > r = vhost_vdpa_set_config_call(v, argp); > break; > + case VHOST_VDPA_GET_IOVA_RANGE: > + r = vhost_vdpa_get_iova_range(v, argp); > + break; > default: > r = vhost_dev_ioctl(&v->vdev, cmd, argp); > if (r == -ENOIOCTLCMD) > @@ -597,6 +611,10 @@ static int vhost_vdpa_process_iotlb_update(struct vhost_vdpa *v, > u64 iova = msg->iova; > int ret = 0; > > + if (msg->iova < v->range.first || > + msg->iova + msg->size - 1 > v->range.last) > + return -EINVAL; > + > if (vhost_iotlb_itree_first(iotlb, msg->iova, > msg->iova + msg->size - 1)) > return -EEXIST; > @@ -762,6 +780,27 @@ static void vhost_vdpa_free_domain(struct vhost_vdpa *v) > v->domain = NULL; > } > > +static void vhost_vdpa_set_iova_range(struct vhost_vdpa *v) > +{ > + struct vdpa_iova_range *range = &v->range; > + struct iommu_domain_geometry geo; > + struct vdpa_device *vdpa = v->vdpa; > + const struct vdpa_config_ops *ops = vdpa->config; > + > + if (ops->get_iova_range) { > + *range = ops->get_iova_range(vdpa); > + } else if (v->domain && > + !iommu_domain_get_attr(v->domain, > + DOMAIN_ATTR_GEOMETRY, &geo) && > + geo.force_aperture) { > + range->first = geo.aperture_start; > + range->last = geo.aperture_end; > + } else { > + range->first = 0; > + range->last = ULLONG_MAX; > + } Shouldn't we require drivers that publish VIRTIO_F_ACCESS_PLATFORM to implement get_iova_range? > +} > + > static int vhost_vdpa_open(struct inode *inode, struct file *filep) > { > struct vhost_vdpa *v; > @@ -802,6 +841,8 @@ static int vhost_vdpa_open(struct inode *inode, struct file *filep) > if (r) > goto err_init_iotlb; > > + vhost_vdpa_set_iova_range(v); > + > filep->private_data = v; > > return 0; > diff --git a/include/uapi/linux/vhost.h b/include/uapi/linux/vhost.h > index 75232185324a..c998860d7bbc 100644 > --- a/include/uapi/linux/vhost.h > +++ b/include/uapi/linux/vhost.h > @@ -146,4 +146,8 @@ > > /* Set event fd for config interrupt*/ > #define VHOST_VDPA_SET_CONFIG_CALL _IOW(VHOST_VIRTIO, 0x77, int) > + > +/* Get the valid iova range */ > +#define VHOST_VDPA_GET_IOVA_RANGE _IOR(VHOST_VIRTIO, 0x78, \ > + struct vhost_vdpa_iova_range) > #endif > diff --git a/include/uapi/linux/vhost_types.h b/include/uapi/linux/vhost_types.h > index 9a269a88a6ff..f7f6a3a28977 100644 > --- a/include/uapi/linux/vhost_types.h > +++ b/include/uapi/linux/vhost_types.h > @@ -138,6 +138,15 @@ struct vhost_vdpa_config { > __u8 buf[0]; > }; > > +/* vhost vdpa IOVA range > + * @first: First address that can be mapped by vhost-vDPA > + * @last: Last address that can be mapped by vhost-vDPA > + */ > +struct vhost_vdpa_iova_range { > + __u64 first; > + __u64 last; > +}; > + > /* Feature bits */ > /* Log all write descriptors. Can be changed while device is active. */ > #define VHOST_F_LOG_ALL 26 > -- > 2.18.1 >