Received: by 2002:a25:6193:0:0:0:0:0 with SMTP id v141csp743613ybb; Wed, 1 Apr 2020 08:50:51 -0700 (PDT) X-Google-Smtp-Source: ADFU+vtOm8ZHGYtkD+EaF83Jq7FlLxX7lRXwZW2NIYklp+dhO9AR10rPsSyUVTmvrl37RQD8FGrL X-Received: by 2002:a9d:7c94:: with SMTP id q20mr15131120otn.150.1585756250979; Wed, 01 Apr 2020 08:50:50 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1585756250; cv=none; d=google.com; s=arc-20160816; b=zz4UmLJndk9t5ynUsZxbShX/FlCIO81vdxLrK8czpYUGakhGxphoq9Nw6lt22P5gsD XKcMDQOZK99q32Lk6jv2/nn9dBqXc2iUakJvcATruJNltVY5MqFZGJLCHrd658oEdnVm s3SVxxf9WxrABla2pXSJnDm8Y9UQpNzWVk+MOcsWX3DsjVF9y2TEX2u6M2M87MPFGO/U 1MJXaJyfQWacY0g+8j1G1JQHl+bVfLWPI9yqSq+YYxOpLUSK23vf3vPuVk+IyzUwtoJO NOwG8lHG5qOW1Iuvrd3Jx+D3A81TSDeUoq9RVcdffQS+dzRH7stQC3tRcT+w6s/5RNra 1xfQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:in-reply-to:content-disposition :mime-version:references:message-id:subject:cc:to:from:date :dkim-signature; bh=C/6o2vAlNYHnEoJMtN8wwi/oRzeStDRZUTB/Nd2Hv7M=; b=XDnarE8xC9d6zsULgeKPk+GjJrj7Vqn4O2IUi1mcYIkEyKvpzWXzfxlVunyIV+Sl78 VdQhVjO0mLKfASUik/PtZB8/Ang/QZ+6RPEnpiaob7B6XW4dILDEmxltEFlhGZ9NFhvx fnpd/JvPLcCJ2/5jXyXzope6pKCiuvtJ8d2KvcAD/6nOg6GoINFCVrlkbo02EbRXUn3j CcYmFSspT1ybbex4Df2RYOH10VaiMoWnbG1tTY1n9xFueiFBHMgiSpmG8APjTcFpYH+/ uJJDbp4LuOqWDHXmPGnc7xlmTKcKoc+XvYyritjPkV5xIzAxMPPIXjIGAr/+a78p/6xs /sOQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=V1X71f5r; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id e8si1092403oif.218.2020.04.01.08.50.37; Wed, 01 Apr 2020 08:50:50 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=V1X71f5r; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1733146AbgDAPto (ORCPT + 99 others); Wed, 1 Apr 2020 11:49:44 -0400 Received: from mail-wr1-f68.google.com ([209.85.221.68]:37057 "EHLO mail-wr1-f68.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1732683AbgDAPto (ORCPT ); Wed, 1 Apr 2020 11:49:44 -0400 Received: by mail-wr1-f68.google.com with SMTP id w10so653709wrm.4 for ; Wed, 01 Apr 2020 08:49:42 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=date:from:to:cc:subject:message-id:references:mime-version :content-disposition:in-reply-to; bh=C/6o2vAlNYHnEoJMtN8wwi/oRzeStDRZUTB/Nd2Hv7M=; b=V1X71f5rZiDGmnBfh3sBWCp7T9Jp+5xgMJtds1xfZSu8D+O/hvzseXEbwd0hbE/ic5 x2TkT+43IaifgxbqN3WD5qF0jkxiEJn/vxu7PJRb8DLrWPo6nyc2sc0xJTT/Dgr5U/Ks I+dljP0laNt/dwhmWSN9NgnGnsQ5TIJIRWzDiczColatMsLZ4AxN0FV2nGqCjw0hr5vA Zoy0T/CjYuH6DlYK8xB91i5btiy69jf9MTCkULGVja5Tvk+K0iLwEzJSa0OqQVJ5Ulf2 xfg3KFSYDoo0qFQSaOx3myBfeqcG6DhAd2eTiiX5P4BHoNaXawbCHD7VO9oOVywNrhMn kYog== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:from:to:cc:subject:message-id:references :mime-version:content-disposition:in-reply-to; bh=C/6o2vAlNYHnEoJMtN8wwi/oRzeStDRZUTB/Nd2Hv7M=; b=Ccovi9ArqJL2KgUyTb/XHqZljoAQcVwazEEJyRattmDWsoiRLcpa9vir6hhUBvNRUW dmD1l8stVBJfzqaTaaJG0hHq+C5V1ev8xfj1zpYCpVsz6ati4ymdaWvnUjNj3qnhAXnl fgGdSm3QCyd5v2eCfn+ha/KJPBwoVzJVXclbwc02zf21YBysxYR+aJtqaLUEpaTS/6s8 8NFEBA4CE/571mJEiXdFGGeBYIJrOqCVe595v/88vsMSMqL2CzyNbDqdMOH0KSddO6mG UhukZzQVRZVwqj5ERdBdyY6kwP6PhywKEVVmRyWNDJt/07336eY6BAQ5weeuupurdkmu BZxw== X-Gm-Message-State: ANhLgQ1uLOuEm0LLmHu+ulcAOBGkrAPB3CW/Eh7L0Eq7sUpSqVuQNfXw psVmfD/FzhNvKruDai0pX7h3zg== X-Received: by 2002:adf:84a3:: with SMTP id 32mr25563249wrg.378.1585756181494; Wed, 01 Apr 2020 08:49:41 -0700 (PDT) Received: from myrica ([2001:171b:226b:54a0:6097:1406:6470:33b5]) by smtp.gmail.com with ESMTPSA id s131sm3258684wmf.35.2020.04.01.08.49.40 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 01 Apr 2020 08:49:40 -0700 (PDT) Date: Wed, 1 Apr 2020 17:49:32 +0200 From: Jean-Philippe Brucker To: Robin Murphy Cc: Bharat Bhushan , joro@8bytes.org, mst@redhat.com, jasowang@redhat.com, virtualization@lists.linux-foundation.org, iommu@lists.linux-foundation.org, linux-kernel@vger.kernel.org, eric.auger.pro@gmail.com, eric.auger@redhat.com Subject: Re: [RFC PATCH v2] iommu/virtio: Use page size bitmap supported by endpoint Message-ID: <20200401154932.GA1124215@myrica> References: <20200401113804.21616-1-bbhushan2@marvell.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Wed, Apr 01, 2020 at 02:00:13PM +0100, Robin Murphy wrote: > On 2020-04-01 12:38 pm, Bharat Bhushan wrote: > > Different endpoint can support different page size, probe > > endpoint if it supports specific page size otherwise use > > global page sizes. > > > > Signed-off-by: Bharat Bhushan > > --- > > drivers/iommu/virtio-iommu.c | 33 +++++++++++++++++++++++++++---- > > include/uapi/linux/virtio_iommu.h | 7 +++++++ > > 2 files changed, 36 insertions(+), 4 deletions(-) > > > > diff --git a/drivers/iommu/virtio-iommu.c b/drivers/iommu/virtio-iommu.c > > index cce329d71fba..c794cb5b7b3e 100644 > > --- a/drivers/iommu/virtio-iommu.c > > +++ b/drivers/iommu/virtio-iommu.c > > @@ -78,6 +78,7 @@ struct viommu_endpoint { > > struct viommu_dev *viommu; > > struct viommu_domain *vdomain; > > struct list_head resv_regions; > > + u64 pgsize_bitmap; > > }; > > struct viommu_request { > > @@ -415,6 +416,20 @@ static int viommu_replay_mappings(struct viommu_domain *vdomain) > > return ret; > > } > > +static int viommu_set_pgsize_bitmap(struct viommu_endpoint *vdev, > > + struct virtio_iommu_probe_pgsize_mask *mask, > > + size_t len) > > + > > +{ > > + u64 pgsize_bitmap = le64_to_cpu(mask->pgsize_bitmap); > > + > > + if (len < sizeof(*mask)) > > + return -EINVAL; > > + > > + vdev->pgsize_bitmap = pgsize_bitmap; > > + return 0; > > +} > > + > > static int viommu_add_resv_mem(struct viommu_endpoint *vdev, > > struct virtio_iommu_probe_resv_mem *mem, > > size_t len) > > @@ -494,11 +509,13 @@ static int viommu_probe_endpoint(struct viommu_dev *viommu, struct device *dev) > > while (type != VIRTIO_IOMMU_PROBE_T_NONE && > > cur < viommu->probe_size) { > > len = le16_to_cpu(prop->length) + sizeof(*prop); > > - Whitespace change > > switch (type) { > > case VIRTIO_IOMMU_PROBE_T_RESV_MEM: > > ret = viommu_add_resv_mem(vdev, (void *)prop, len); > > break; > > + case VIRTIO_IOMMU_PROBE_T_PAGE_SIZE_MASK: > > + ret = viommu_set_pgsize_bitmap(vdev, (void *)prop, len); > > + break; > > default: > > dev_err(dev, "unknown viommu prop 0x%x\n", type); > > } > > @@ -607,16 +624,23 @@ static struct iommu_domain *viommu_domain_alloc(unsigned type) > > return &vdomain->domain; > > } > > -static int viommu_domain_finalise(struct viommu_dev *viommu, > > +static int viommu_domain_finalise(struct viommu_endpoint *vdev, > > struct iommu_domain *domain) > > { > > int ret; > > struct viommu_domain *vdomain = to_viommu_domain(domain); > > + struct viommu_dev *viommu = vdev->viommu; > > vdomain->viommu = viommu; > > vdomain->map_flags = viommu->map_flags; > > - domain->pgsize_bitmap = viommu->pgsize_bitmap; > > + /* Devices in same domain must support same size pages */ > > AFAICS what the code appears to do is enforce that the first endpoint > attached to any domain has the same pgsize_bitmap as the most recently > probed viommu_dev instance, then ignore any subsequent endpoints attached to > the same domain. Thus I'm not sure that comment is accurate. > Yes viommu_domain_finalise() is only called once. What I had in mind is something like: ---- 8< ---- diff --git a/drivers/iommu/virtio-iommu.c b/drivers/iommu/virtio-iommu.c index 750f69c49b95..8303b7b513ff 100644 --- a/drivers/iommu/virtio-iommu.c +++ b/drivers/iommu/virtio-iommu.c @@ -639,6 +639,29 @@ static int viommu_domain_finalise(struct viommu_endpoint *vdev, return 0; } +/* + * Check whether the endpoint's capabilities are compatible with other endpoints + * in the domain. Report any inconsistency. + */ +static bool viommu_endpoint_is_compatible(struct viommu_endpoint *vdev, + struct viommu_domain *vdomain) +{ + struct device *dev = vdev->dev; + + if (vdomain->viommu != vdev->viommu) { + dev_err(dev, "cannot attach to foreign vIOMMU\n"); + return false; + } + + if (vdomain->domain.pgsize_bitmap != vdev->pgsize_bitmap) { + dev_err(dev, "incompatible domain bitmap 0x%lx != 0x%lx\n", + vdomain->domain.pgsize_bitmap, vdev->pgsize_bitmap); + return false; + } + + return true; +} + static void viommu_domain_free(struct iommu_domain *domain) { struct viommu_domain *vdomain = to_viommu_domain(domain); @@ -670,9 +693,8 @@ static int viommu_attach_dev(struct iommu_domain *domain, struct device *dev) * owns it. */ ret = viommu_domain_finalise(vdev, domain); - } else if (vdomain->viommu != vdev->viommu) { - dev_err(dev, "cannot attach to foreign vIOMMU\n"); - ret = -EXDEV; + } else if (!viommu_endpoint_is_compatible(vdev, vdomain)) { + ret = -EINVAL; } mutex_unlock(&vdomain->mutex); ---- >8 ---- > > > + if ((domain->pgsize_bitmap != viommu->pgsize_bitmap) && > > + (domain->pgsize_bitmap != vdev->pgsize_bitmap)) > > + return -EINVAL; > > + > > + domain->pgsize_bitmap = vdev->pgsize_bitmap; > > + > > domain->geometry = viommu->geometry; > > ret = ida_alloc_range(&viommu->domain_ids, viommu->first_domain, > > @@ -657,7 +681,7 @@ static int viommu_attach_dev(struct iommu_domain *domain, struct device *dev) > > * Properly initialize the domain now that we know which viommu > > * owns it. > > */ > > - ret = viommu_domain_finalise(vdev->viommu, domain); > > + ret = viommu_domain_finalise(vdev, domain); > > } else if (vdomain->viommu != vdev->viommu) { > > dev_err(dev, "cannot attach to foreign vIOMMU\n"); > > ret = -EXDEV; > > @@ -875,6 +899,7 @@ static int viommu_add_device(struct device *dev) > > vdev->dev = dev; > > vdev->viommu = viommu; > > + vdev->pgsize_bitmap = viommu->pgsize_bitmap; > > INIT_LIST_HEAD(&vdev->resv_regions); > > fwspec->iommu_priv = vdev; > > diff --git a/include/uapi/linux/virtio_iommu.h b/include/uapi/linux/virtio_iommu.h > > index 237e36a280cb..dc9d3f40bcd8 100644 > > --- a/include/uapi/linux/virtio_iommu.h > > +++ b/include/uapi/linux/virtio_iommu.h > > @@ -111,6 +111,7 @@ struct virtio_iommu_req_unmap { > > #define VIRTIO_IOMMU_PROBE_T_NONE 0 > > #define VIRTIO_IOMMU_PROBE_T_RESV_MEM 1 > > +#define VIRTIO_IOMMU_PROBE_T_PAGE_SIZE_MASK 2 > > #define VIRTIO_IOMMU_PROBE_T_MASK 0xfff > > @@ -119,6 +120,12 @@ struct virtio_iommu_probe_property { > > __le16 length; > > }; > > +struct virtio_iommu_probe_pgsize_mask { > > + struct virtio_iommu_probe_property head; > > + __u8 reserved[4]; > > + __u64 pgsize_bitmap; Should be __le64 Thanks, Jean > > +}; > > + > > #define VIRTIO_IOMMU_RESV_MEM_T_RESERVED 0 > > #define VIRTIO_IOMMU_RESV_MEM_T_MSI 1 > >