Received: by 2002:ac0:a5a6:0:0:0:0:0 with SMTP id m35-v6csp277079imm; Wed, 29 Aug 2018 21:15:07 -0700 (PDT) X-Google-Smtp-Source: ANB0VdbtcL75v/g6DKuHGHRQNl6YppDd1fQgXR5mKw+3VKBNYUFd+QwcNbwvtSaqwqmE4pV+OL1X X-Received: by 2002:a17:902:7488:: with SMTP id h8-v6mr8519095pll.16.1535602507401; Wed, 29 Aug 2018 21:15:07 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1535602507; cv=none; d=google.com; s=arc-20160816; b=chkIGPaK7KoSnUWxMqF/MPWYWGrTWRMD0gcPHPpRRuCBHHUwdrQ+18oYYlqe07ZEuw J0XRxsZ7UGKg5hBZ3EKGCxAkbF8xf/br2C8413V5dkusMau4+9DYyhxiayAl0z6Qd4+7 jrY3ymQihoP4o1vRm4ib11whUWVrhVWV0o4PAL+QffMBpYD3Vu/WDUFjh/2IHodk6j1X xfnJdAK9bNv37c1WIcFpnfQWgfozDutnwiBXHmIrfy7xVvm1immBDwv6h5gAEAeQgEit 53y0lse/dPXIY2N/L9fW+FvpBGJPzs3w7amc8rMJl2ZNyUMG0v34s3SBDMeEYs7ZI+F7 ho/w== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:references:in-reply-to:message-id:date :subject:cc:to:from:arc-authentication-results; bh=0gFhPArPmRYqwO0sIcGk/eXL4zfHj0xoNl7ompa+rPM=; b=Kuk12uteW6WSuMVumXIYyENql2GZrCu0yNlBGQRt7uY0hTV8jJkknL8lmI2iUHiKMU Zbfo6Fj1IQVhs0qHMKNTg4L1dBSQ1c2fqe3dlULk48Omohn2alsTZf108t2+OU2gYudc tsI8ujJDpp/862ztQHE7iQ+j4o+6p9SwMHENHG68tgghCJ0jBYqURVu+ZkqDlQtc77Rr XqLaJb9Up+8u6jlTzx7oo44uK28dDsgiYJtvH+UILvkZzwPmvC5qXQG8vqmsDzyr513A bCjjMHDwYuQDrWlYMPi9a1+nx8hIpWXA8nVIA4dn/2FB/Ks01W7Y07ga2pk5IqdTmqBe Cwsg== 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 u11-v6si5585106pgg.683.2018.08.29.21.14.29; Wed, 29 Aug 2018 21:15:07 -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 S1727823AbeH3ILj (ORCPT + 99 others); Thu, 30 Aug 2018 04:11:39 -0400 Received: from mga02.intel.com ([134.134.136.20]:31598 "EHLO mga02.intel.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727720AbeH3ILY (ORCPT ); Thu, 30 Aug 2018 04:11:24 -0400 X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False Received: from orsmga007.jf.intel.com ([10.7.209.58]) by orsmga101.jf.intel.com with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 29 Aug 2018 21:11:13 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.53,306,1531810800"; d="scan'208";a="68959983" Received: from allen-box.sh.intel.com ([10.239.161.122]) by orsmga007.jf.intel.com with ESMTP; 29 Aug 2018 21:11:07 -0700 From: Lu Baolu To: Joerg Roedel , David Woodhouse , Alex Williamson , Kirti Wankhede Cc: ashok.raj@intel.com, sanjay.k.kumar@intel.com, jacob.jun.pan@intel.com, kevin.tian@intel.com, Jean-Philippe Brucker , yi.l.liu@intel.com, yi.y.sun@intel.com, peterx@redhat.com, tiwei.bie@intel.com, iommu@lists.linux-foundation.org, kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Lu Baolu , Jacob Pan Subject: [RFC PATCH v2 08/10] vfio/type1: Add domain at(de)taching group helpers Date: Thu, 30 Aug 2018 12:09:20 +0800 Message-Id: <20180830040922.30426-9-baolu.lu@linux.intel.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20180830040922.30426-1-baolu.lu@linux.intel.com> References: <20180830040922.30426-1-baolu.lu@linux.intel.com> Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org If a domain is attaching to a group which includes the mediated devices, it should attach to the mdev parent of each mdev. This adds a helper for attaching domain to group, no matter a PCI physical device or mediated devices which are derived from a PCI physical device. Cc: Ashok Raj Cc: Jacob Pan Cc: Kevin Tian Cc: Liu Yi L Signed-off-by: Lu Baolu --- drivers/vfio/vfio_iommu_type1.c | 77 ++++++++++++++++++++++++++++++--- 1 file changed, 70 insertions(+), 7 deletions(-) diff --git a/drivers/vfio/vfio_iommu_type1.c b/drivers/vfio/vfio_iommu_type1.c index d9fd3188615d..89e2e6123223 100644 --- a/drivers/vfio/vfio_iommu_type1.c +++ b/drivers/vfio/vfio_iommu_type1.c @@ -91,6 +91,9 @@ struct vfio_dma { struct vfio_group { struct iommu_group *iommu_group; struct list_head next; + bool attach_parent; /* An mdev group with domain + * attached to parent + */ }; /* @@ -1327,6 +1330,66 @@ static bool vfio_iommu_has_sw_msi(struct iommu_group *group, phys_addr_t *base) return ret; } +static int vfio_mdev_set_aux_domain(struct device *dev, + struct iommu_domain *domain) +{ + int (*fn)(struct device *dev, void *domain); + int ret; + + fn = symbol_get(mdev_set_domain); + if (fn) { + ret = fn(dev, domain); + symbol_put(mdev_set_domain); + + return ret; + } + + return -EINVAL; +} + +static int vfio_attach_aux_domain(struct device *dev, void *data) +{ + struct iommu_domain *domain = data; + int ret; + + ret = vfio_mdev_set_aux_domain(dev, domain); + if (ret) + return ret; + + return iommu_attach_device(domain, dev->parent); +} + +static int vfio_detach_aux_domain(struct device *dev, void *data) +{ + struct iommu_domain *domain = data; + + vfio_mdev_set_aux_domain(dev, NULL); + iommu_detach_device(domain, dev->parent); + + return 0; +} + +static int vfio_iommu_attach_group(struct vfio_domain *domain, + struct vfio_group *group) +{ + if (group->attach_parent) + return iommu_group_for_each_dev(group->iommu_group, + domain->domain, + vfio_attach_aux_domain); + else + return iommu_attach_group(domain->domain, group->iommu_group); +} + +static void vfio_iommu_detach_group(struct vfio_domain *domain, + struct vfio_group *group) +{ + if (group->attach_parent) + iommu_group_for_each_dev(group->iommu_group, domain->domain, + vfio_detach_aux_domain); + else + iommu_detach_group(domain->domain, group->iommu_group); +} + static int vfio_iommu_type1_attach_group(void *iommu_data, struct iommu_group *iommu_group) { @@ -1402,7 +1465,7 @@ static int vfio_iommu_type1_attach_group(void *iommu_data, goto out_domain; } - ret = iommu_attach_group(domain->domain, iommu_group); + ret = vfio_iommu_attach_group(domain, group); if (ret) goto out_domain; @@ -1434,8 +1497,8 @@ static int vfio_iommu_type1_attach_group(void *iommu_data, list_for_each_entry(d, &iommu->domain_list, next) { if (d->domain->ops == domain->domain->ops && d->prot == domain->prot) { - iommu_detach_group(domain->domain, iommu_group); - if (!iommu_attach_group(d->domain, iommu_group)) { + vfio_iommu_detach_group(domain, group); + if (!vfio_iommu_attach_group(d, group)) { list_add(&group->next, &d->group_list); iommu_domain_free(domain->domain); kfree(domain); @@ -1443,7 +1506,7 @@ static int vfio_iommu_type1_attach_group(void *iommu_data, return 0; } - ret = iommu_attach_group(domain->domain, iommu_group); + ret = vfio_iommu_attach_group(domain, group); if (ret) goto out_domain; } @@ -1469,7 +1532,7 @@ static int vfio_iommu_type1_attach_group(void *iommu_data, return 0; out_detach: - iommu_detach_group(domain->domain, iommu_group); + vfio_iommu_detach_group(domain, group); out_domain: iommu_domain_free(domain->domain); out_free: @@ -1560,7 +1623,7 @@ static void vfio_iommu_type1_detach_group(void *iommu_data, if (!group) continue; - iommu_detach_group(domain->domain, iommu_group); + vfio_iommu_detach_group(domain, group); list_del(&group->next); kfree(group); /* @@ -1625,7 +1688,7 @@ static void vfio_release_domain(struct vfio_domain *domain, bool external) list_for_each_entry_safe(group, group_tmp, &domain->group_list, next) { if (!external) - iommu_detach_group(domain->domain, group->iommu_group); + vfio_iommu_detach_group(domain, group); list_del(&group->next); kfree(group); } -- 2.17.1