Received: by 2002:a25:e74b:0:0:0:0:0 with SMTP id e72csp221680ybh; Sun, 12 Jul 2020 04:16:40 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxExR5S1kgZE9Qy0f1oTpeF+fPLiRVMo7/8OjkqsOehJEmqM6xCITyXJrgNku5ypQjDTIyR X-Received: by 2002:aa7:c442:: with SMTP id n2mr75295288edr.309.1594552600381; Sun, 12 Jul 2020 04:16:40 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1594552600; cv=none; d=google.com; s=arc-20160816; b=K6DhAqJzKauc0+SRW+XO1s7CVX0qNG4rM2TdLbswUVefmowL/2+wuPawNzb+NB0AfO 0WhuyyyFYbHQMOECPbEAO08B8odhN1/KjMVtwaIpb+xoJ5wLlwGFHh2s0LC9YJdaA79C QS44M9MOAVcEjXGwg7kLo5yG7TmG1yvT2lAt9zD7iJgOS8AO5ao6WMR7teBsAfC7so3m nSxk7o8W9aVvQh8q0+I6Y3OUWW5RydEWmXdq4Mos9r50aGMyvciBoCbh3aDHpucW44rC D6J0WXhOz9Yupc9BtSdGtkdEPp8Ux/Ox7e7PNX/HWuit4kWUqma+Q7rZTPz0wzUUipyt eZ4g== 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:ironport-sdr:ironport-sdr; bh=GWy/u2qvaFLE3iOTTpEDb7O7LnPLVz1SGlpz47UIE3M=; b=aCYAtdkb636NIEIjsHMynaEWKrZ6Tnrc75SAoSAarZM4KNI8K9SPkMLOhFZLcy8tbk OoNyfDGg9l4aGaCj4U5p/HVpy7XteQBtlg2wF9XWO58xnu8QvNcjum5BSlCEvB7Ao2t7 rAXOWe6rAh8ORXZMAefjWW4kgTZDPF7GTH2y7RmzxcS+NBXmWO+lh0Jnh0BA/n9zDtPX vsUm06JxKsVZ77rHvSrKA5lUNEKoj2BSJLTYbBgst/6rw3nYRbJuN70aW5EQi1wM8imc hkyNOS2OCAqk2is7aQ9UdrUBZ1qPAEw0Nz9okb71L1nAdoqV7CGfkxMDXN1zy3gf9beg Sfow== ARC-Authentication-Results: i=1; mx.google.com; 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=fail (p=NONE sp=NONE dis=NONE) header.from=intel.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id dh2si7093790edb.55.2020.07.12.04.16.18; Sun, 12 Jul 2020 04:16:40 -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; 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=fail (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728943AbgGLLP2 (ORCPT + 99 others); Sun, 12 Jul 2020 07:15:28 -0400 Received: from mga09.intel.com ([134.134.136.24]:45847 "EHLO mga09.intel.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728799AbgGLLOk (ORCPT ); Sun, 12 Jul 2020 07:14:40 -0400 IronPort-SDR: unmfbl6Jt5ju6I9rrVPeqJ8fvYfKCeUQEauYhHQxOoRGIPlj1ehMWFhCDki4eMu5JG9A3TlI52 Ph73HgCabaHw== X-IronPort-AV: E=McAfee;i="6000,8403,9679"; a="149952701" X-IronPort-AV: E=Sophos;i="5.75,343,1589266800"; d="scan'208";a="149952701" X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False Received: from orsmga008.jf.intel.com ([10.7.209.65]) by orsmga102.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 12 Jul 2020 04:14:30 -0700 IronPort-SDR: bm2S/HgQTwV7jE3XzEnrOmUe0qjoc/XvlTDWUT9414eUWiYKQlkSbXqforE8Zvsu5g6RzPIdCL YIZ2S2JT+OjQ== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.75,343,1589266800"; d="scan'208";a="315788584" Received: from jacob-builder.jf.intel.com ([10.7.199.155]) by orsmga008.jf.intel.com with ESMTP; 12 Jul 2020 04:14:30 -0700 From: Liu Yi L To: alex.williamson@redhat.com, eric.auger@redhat.com, baolu.lu@linux.intel.com, joro@8bytes.org Cc: kevin.tian@intel.com, jacob.jun.pan@linux.intel.com, ashok.raj@intel.com, yi.l.liu@intel.com, jun.j.tian@intel.com, yi.y.sun@intel.com, jean-philippe@linaro.org, peterx@redhat.com, hao.wu@intel.com, stefanha@gmail.com, iommu@lists.linux-foundation.org, kvm@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH v5 12/15] vfio/type1: Add vSVA support for IOMMU-backed mdevs Date: Sun, 12 Jul 2020 04:21:07 -0700 Message-Id: <1594552870-55687-13-git-send-email-yi.l.liu@intel.com> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1594552870-55687-1-git-send-email-yi.l.liu@intel.com> References: <1594552870-55687-1-git-send-email-yi.l.liu@intel.com> Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Recent years, mediated device pass-through framework (e.g. vfio-mdev) is used to achieve flexible device sharing across domains (e.g. VMs). Also there are hardware assisted mediated pass-through solutions from platform vendors. e.g. Intel VT-d scalable mode which supports Intel Scalable I/O Virtualization technology. Such mdevs are called IOMMU- backed mdevs as there are IOMMU enforced DMA isolation for such mdevs. In kernel, IOMMU-backed mdevs are exposed to IOMMU layer by aux-domain concept, which means mdevs are protected by an iommu domain which is auxiliary to the domain that the kernel driver primarily uses for DMA API. Details can be found in the KVM presentation as below: https://events19.linuxfoundation.org/wp-content/uploads/2017/12/\ Hardware-Assisted-Mediated-Pass-Through-with-VFIO-Kevin-Tian-Intel.pdf This patch extends NESTING_IOMMU ops to IOMMU-backed mdev devices. The main requirement is to use the auxiliary domain associated with mdev. Cc: Kevin Tian CC: Jacob Pan CC: Jun Tian Cc: Alex Williamson Cc: Eric Auger Cc: Jean-Philippe Brucker Cc: Joerg Roedel Cc: Lu Baolu Signed-off-by: Liu Yi L --- v1 -> v2: *) check the iommu_device to ensure the handling mdev is IOMMU-backed --- drivers/vfio/vfio_iommu_type1.c | 39 +++++++++++++++++++++++++++++++++++---- 1 file changed, 35 insertions(+), 4 deletions(-) diff --git a/drivers/vfio/vfio_iommu_type1.c b/drivers/vfio/vfio_iommu_type1.c index 960cc59..f1f1ae2 100644 --- a/drivers/vfio/vfio_iommu_type1.c +++ b/drivers/vfio/vfio_iommu_type1.c @@ -2373,20 +2373,41 @@ static int vfio_iommu_resv_refresh(struct vfio_iommu *iommu, return ret; } +static struct device *vfio_get_iommu_device(struct vfio_group *group, + struct device *dev) +{ + if (group->mdev_group) + return vfio_mdev_get_iommu_device(dev); + else + return dev; +} + static int vfio_dev_bind_gpasid_fn(struct device *dev, void *data) { struct domain_capsule *dc = (struct domain_capsule *)data; unsigned long arg = *(unsigned long *)dc->data; + struct device *iommu_device; + + iommu_device = vfio_get_iommu_device(dc->group, dev); + if (!iommu_device) + return -EINVAL; - return iommu_sva_bind_gpasid(dc->domain, dev, (void __user *)arg); + return iommu_sva_bind_gpasid(dc->domain, iommu_device, + (void __user *)arg); } static int vfio_dev_unbind_gpasid_fn(struct device *dev, void *data) { struct domain_capsule *dc = (struct domain_capsule *)data; unsigned long arg = *(unsigned long *)dc->data; + struct device *iommu_device; - iommu_sva_unbind_gpasid(dc->domain, dev, (void __user *)arg); + iommu_device = vfio_get_iommu_device(dc->group, dev); + if (!iommu_device) + return -EINVAL; + + iommu_sva_unbind_gpasid(dc->domain, iommu_device, + (void __user *)arg); return 0; } @@ -2395,8 +2416,13 @@ static int __vfio_dev_unbind_gpasid_fn(struct device *dev, void *data) struct domain_capsule *dc = (struct domain_capsule *)data; struct iommu_gpasid_bind_data *unbind_data = (struct iommu_gpasid_bind_data *)dc->data; + struct device *iommu_device; + + iommu_device = vfio_get_iommu_device(dc->group, dev); + if (!iommu_device) + return -EINVAL; - __iommu_sva_unbind_gpasid(dc->domain, dev, unbind_data); + __iommu_sva_unbind_gpasid(dc->domain, iommu_device, unbind_data); return 0; } @@ -3077,8 +3103,13 @@ static int vfio_dev_cache_invalidate_fn(struct device *dev, void *data) { struct domain_capsule *dc = (struct domain_capsule *)data; unsigned long arg = *(unsigned long *)dc->data; + struct device *iommu_device; + + iommu_device = vfio_get_iommu_device(dc->group, dev); + if (!iommu_device) + return -EINVAL; - iommu_cache_invalidate(dc->domain, dev, (void __user *)arg); + iommu_cache_invalidate(dc->domain, iommu_device, (void __user *)arg); return 0; } -- 2.7.4