Received: by 2002:ac0:a5a6:0:0:0:0:0 with SMTP id m35-v6csp1353656imm; Sun, 2 Sep 2018 20:22:13 -0700 (PDT) X-Google-Smtp-Source: ANB0VdZpsK93iHaiEmAhcjIZtjKf+sDWVjKVr1R4anJ0DnVxDPOmPVkomJdR9pIyf5/bj3CpzMHF X-Received: by 2002:a17:902:8c84:: with SMTP id t4-v6mr26728302plo.100.1535944933691; Sun, 02 Sep 2018 20:22:13 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1535944933; cv=none; d=google.com; s=arc-20160816; b=zgRAye89l30WZlNCgYgTK9cEad3UAGSFrXsOi8uxHFYqa6cRUH0H/8V+wz1Jfky/YD ZPzHocF8XSTcnw6HFYMgILr/njHBBWVugd5Zw4x/gDcS3tsOn3ubEXsRZjNexNc38n/A mliVl03CIY0FbMdHy93gRRZNNxT/vp8FIpN5w+N6RySy8++d9gBtw11wK3bwNT0uSquU ULdmMCUO2+FXKZRhYPRXT9BALpx5HfZSKPx3NYsEwYh1FYYU1BVkcTyOqKx+IbQkKzoS F02V5UbSwo7txul4mm86e/AXu69wU2nG9EZuFx/F369LxBcPx/PvJhYXenuNI0l3tzM/ 3FWA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding :content-language:in-reply-to:mime-version:user-agent:date :message-id:from:references:to:subject:cc:arc-authentication-results; bh=dx5rOlLyAC+YbzAFfWu0SJhw1xpv8C4vBrWgAyLnSgU=; b=abq3cvEGR+fO/Go8FtcPriNWihptiuDot2r+B4sD8zdowKxRam+rjmPvtkkr7Qnhrs MitvryObErBThAe/AYxv7AftzbFgZg8QC6AEDCasFZ/e1WGviYHa7kOTE/qoCGlhC8HJ 36mp7Yuu72Au4Y1QOkafmKOxVKGyd4JP0K5wna7Ab0HlXN2D5JLbBwrO/unWUezvYrtx 3AlJo1KSooBarKs7FiI7lZrQ7RpYlPR/kvcERWFb3im2htZue0MC+yKEXWAbC5Pbiwpf 5Ezed1w7ciIUWHvBZN5rYi4AEVa/S18LOv1qh3k2pqjRGzQVaYKcfU/eU3jT/E3bju8b n5Rw== ARC-Authentication-Results: i=1; mx.google.com; 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=fail (p=NONE sp=NONE dis=NONE) header.from=intel.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id x18-v6si16890780pll.88.2018.09.02.20.21.59; Sun, 02 Sep 2018 20:22:13 -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; 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=fail (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727334AbeICHPG (ORCPT + 99 others); Mon, 3 Sep 2018 03:15:06 -0400 Received: from mga04.intel.com ([192.55.52.120]:32637 "EHLO mga04.intel.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1725903AbeICHPF (ORCPT ); Mon, 3 Sep 2018 03:15:05 -0400 X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False Received: from fmsmga008.fm.intel.com ([10.253.24.58]) by fmsmga104.fm.intel.com with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 02 Sep 2018 19:57:00 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.53,323,1531810800"; d="scan'208";a="67902651" Received: from allen-box.sh.intel.com (HELO [10.239.161.122]) ([10.239.161.122]) by fmsmga008.fm.intel.com with ESMTP; 02 Sep 2018 19:56:57 -0700 Cc: baolu.lu@linux.intel.com, linuxarm@huawei.com Subject: Re: [PATCH 3/7] vfio: add sdmdev support To: Kenneth Lee , Jonathan Corbet , Herbert Xu , "David S . Miller" , Joerg Roedel , Alex Williamson , Kenneth Lee , Hao Fang , Zhou Wang , Zaibo Xu , Philippe Ombredanne , Greg Kroah-Hartman , Thomas Gleixner , linux-doc@vger.kernel.org, linux-kernel@vger.kernel.org, linux-crypto@vger.kernel.org, iommu@lists.linux-foundation.org, kvm@vger.kernel.org, linux-accelerators@lists.ozlabs.org, Sanjay Kumar References: <20180903005204.26041-1-nek.in.cn@gmail.com> <20180903005204.26041-4-nek.in.cn@gmail.com> From: Lu Baolu Message-ID: <4ea51b20-dcc1-db32-18eb-24a004ab9085@linux.intel.com> Date: Mon, 3 Sep 2018 10:55:57 +0800 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:52.0) Gecko/20100101 Thunderbird/52.9.1 MIME-Version: 1.0 In-Reply-To: <20180903005204.26041-4-nek.in.cn@gmail.com> Content-Type: text/plain; charset=utf-8; format=flowed Content-Language: en-US Content-Transfer-Encoding: 7bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Hi, On 09/03/2018 08:52 AM, Kenneth Lee wrote: > From: Kenneth Lee > > SDMDEV is "Share Domain Mdev". It is a vfio-mdev. But differ from > the general vfio-mdev, it shares its parent's IOMMU. If Multi-PASID > support is enabled in the IOMMU (not yet in the current kernel HEAD), > multiple process can share the IOMMU by different PASID. If it is not > support, only one process can share the IOMMU with the kernel driver. > If only for share domain purpose, I don't think it's necessary to create a new device type. > Currently only the vfio type-1 driver is updated to make it to be aware > of. > > Signed-off-by: Kenneth Lee > Signed-off-by: Zaibo Xu > Signed-off-by: Zhou Wang > --- > drivers/vfio/Kconfig | 1 + > drivers/vfio/Makefile | 1 + > drivers/vfio/sdmdev/Kconfig | 10 + > drivers/vfio/sdmdev/Makefile | 3 + > drivers/vfio/sdmdev/vfio_sdmdev.c | 363 ++++++++++++++++++++++++++++++ > drivers/vfio/vfio_iommu_type1.c | 151 ++++++++++++- > include/linux/vfio_sdmdev.h | 96 ++++++++ > include/uapi/linux/vfio_sdmdev.h | 29 +++ > 8 files changed, 648 insertions(+), 6 deletions(-) > create mode 100644 drivers/vfio/sdmdev/Kconfig > create mode 100644 drivers/vfio/sdmdev/Makefile > create mode 100644 drivers/vfio/sdmdev/vfio_sdmdev.c > create mode 100644 include/linux/vfio_sdmdev.h > create mode 100644 include/uapi/linux/vfio_sdmdev.h > [--cut for short --] > diff --git a/drivers/vfio/vfio_iommu_type1.c b/drivers/vfio/vfio_iommu_type1.c > index d9fd3188615d..ba73231d8692 100644 > --- a/drivers/vfio/vfio_iommu_type1.c > +++ b/drivers/vfio/vfio_iommu_type1.c > @@ -41,6 +41,7 @@ > #include > #include > #include > +#include > > #define DRIVER_VERSION "0.2" > #define DRIVER_AUTHOR "Alex Williamson " > @@ -89,6 +90,8 @@ struct vfio_dma { > }; > > struct vfio_group { > + /* iommu_group of mdev's parent device */ > + struct iommu_group *parent_group; > struct iommu_group *iommu_group; > struct list_head next; > }; > @@ -1327,6 +1330,109 @@ static bool vfio_iommu_has_sw_msi(struct iommu_group *group, phys_addr_t *base) > return ret; > } > > +/* return 0 if the device is not sdmdev. > + * return 1 if the device is sdmdev, the data will be updated with parent > + * device's group. > + * return -errno if other error. > + */ > +static int vfio_sdmdev_type(struct device *dev, void *data) > +{ > + struct iommu_group **group = data; > + struct iommu_group *pgroup; > + int (*_is_sdmdev)(struct device *dev); > + struct device *pdev; > + int ret = 1; > + > + /* vfio_sdmdev module is not configurated */ > + _is_sdmdev = symbol_get(vfio_sdmdev_is_sdmdev); > + if (!_is_sdmdev) > + return 0; > + > + /* check if it belongs to vfio_sdmdev device */ > + if (!_is_sdmdev(dev)) { > + ret = 0; > + goto out; > + } > + > + pdev = dev->parent; > + pgroup = iommu_group_get(pdev); > + if (!pgroup) { > + ret = -ENODEV; > + goto out; > + } > + > + if (group) { > + /* check if all parent devices is the same */ > + if (*group && *group != pgroup) > + ret = -ENODEV; > + else > + *group = pgroup; > + } > + > + iommu_group_put(pgroup); > + > +out: > + symbol_put(vfio_sdmdev_is_sdmdev); > + > + return ret; > +} > + > +/* return 0 or -errno */ > +static int vfio_sdmdev_bus(struct device *dev, void *data) > +{ > + struct bus_type **bus = data; > + > + if (!dev->bus) > + return -ENODEV; > + > + /* ensure all devices has the same bus_type */ > + if (*bus && *bus != dev->bus) > + return -EINVAL; > + > + *bus = dev->bus; > + return 0; > +} > + > +/* return 0 means it is not sd group, 1 means it is, or -EXXX for error */ > +static int vfio_iommu_type1_attach_sdgroup(struct vfio_domain *domain, > + struct vfio_group *group, > + struct iommu_group *iommu_group) > +{ > + int ret; > + struct bus_type *pbus = NULL; > + struct iommu_group *pgroup = NULL; > + > + ret = iommu_group_for_each_dev(iommu_group, &pgroup, > + vfio_sdmdev_type); > + if (ret < 0) > + goto out; > + else if (ret > 0) { > + domain->domain = iommu_group_share_domain(pgroup); > + if (IS_ERR(domain->domain)) > + goto out; > + ret = iommu_group_for_each_dev(pgroup, &pbus, > + vfio_sdmdev_bus); > + if (ret < 0) > + goto err_with_share_domain; > + > + if (pbus && iommu_capable(pbus, IOMMU_CAP_CACHE_COHERENCY)) > + domain->prot |= IOMMU_CACHE; > + > + group->parent_group = pgroup; > + INIT_LIST_HEAD(&domain->group_list); > + list_add(&group->next, &domain->group_list); > + > + return 1; > + } This doesn't match the function name. It only gets the domain from the parent device. It hasn't been really attached. > + > + return 0; > + > +err_with_share_domain: > + iommu_group_unshare_domain(pgroup); > +out: > + return ret; > +} > + > static int vfio_iommu_type1_attach_group(void *iommu_data, > struct iommu_group *iommu_group) > { > @@ -1335,8 +1441,8 @@ static int vfio_iommu_type1_attach_group(void *iommu_data, > struct vfio_domain *domain, *d; > struct bus_type *bus = NULL, *mdev_bus; > int ret; > - bool resv_msi, msi_remap; > - phys_addr_t resv_msi_base; > + bool resv_msi = false, msi_remap; > + phys_addr_t resv_msi_base = 0; > > mutex_lock(&iommu->lock); > > @@ -1373,6 +1479,14 @@ static int vfio_iommu_type1_attach_group(void *iommu_data, > if (mdev_bus) { > if ((bus == mdev_bus) && !iommu_present(bus)) { > symbol_put(mdev_bus_type); > + > + ret = vfio_iommu_type1_attach_sdgroup(domain, group, > + iommu_group); > + if (ret < 0) > + goto out_free; > + else if (ret > 0) > + goto replay_check; Here you get the domain from the parent device and save it for later use. The actual attaching is ignored. I don't think this follows the philosophy of this function. It actually make all devices in the group with the same bus type to share a single domain. Further more, the parent domain might be a domain of type IOMMU_DOMAIN_DMA. That will not be able to use as an IOMMU_DOMAIN_UNMANAGED domain for iommu APIs. Best regards, Lu Baolu