Received: by 2002:a25:ca44:0:0:0:0:0 with SMTP id a65csp58768ybg; Mon, 27 Jul 2020 23:22:29 -0700 (PDT) X-Google-Smtp-Source: ABdhPJz4adW5jc6DbsVjRJMPbnewX8FyqreElAIbprrM1w3JADqj+nN0sowuX+r0fHEW0Se8yqKA X-Received: by 2002:a17:906:22c1:: with SMTP id q1mr15125742eja.443.1595917348987; Mon, 27 Jul 2020 23:22:28 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1595917348; cv=none; d=google.com; s=arc-20160816; b=Uvz+Ss0wWbrfzbcDS/AJ9X7KwTMhNQqPHc0ickvjurRt7n1fHTCPmsMxL5b5MHddjO aSQC3K9gF0XlwR0DEIQWPFxjRAdQLlr+sUCY+GFulxwRuiU3ADoxMrQV8hOvFaMKTi7e pQxtZfZtNlNGcHphZmBtmctOto84NoZYNEVLW/MD9Pl18cWITct0oQAMevCNH0xQ2nzE wqNw2y4pdn15JJDo7J6FQwAOgss5EVw1aUEKsQ9gMu2nijBs2rYcRTQ/uz0HSuWLncKv DuJ37rE4oT1QAIaUi1UxgzRJHXwTgLzddrfrxAl3u3i5HAQr6w5mO6UQzv2b2ZVya4nA yT+g== 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=1MWMj4l0sAiWeJkPSFHBHuFxDEoY4xzG/CWe6CCv9y8=; b=hdkOhNdFfSZfwXWTDnoLYPQKeQXAS/hsK0w8DqZ7l/plMwdbxR0rGAvGz2eM73vRpl 0PPuh5otBpmrqYR2ScJakDs12j++OKkf1ki2r1xdco1r0w0yxvptiS4UXCfGT1PXS12+ +5fdk+Bu8WlphTXDfvb1e9fm3iDQcjj9P9mTQ3KtobXzMLst8WZh6kUlziVt2V9Gjvpt V8ETiSWGEhM48Cj7ywsnnUtyE0sl4YNGtCHGhQi0dBF7x68bn/ra/S06A6qzI1VXPWtZ AW50qYeKcX9m89dquOj5eCdCuKYuQWA2yJGqsCXfMDGmq8onRY4VLnXLKEV1p+nZBLVE GBGw== 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 v18si7237719edy.249.2020.07.27.23.22.06; Mon, 27 Jul 2020 23:22:28 -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 S1727929AbgG1GVF (ORCPT + 99 others); Tue, 28 Jul 2020 02:21:05 -0400 Received: from mga06.intel.com ([134.134.136.31]:26366 "EHLO mga06.intel.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727860AbgG1GVB (ORCPT ); Tue, 28 Jul 2020 02:21:01 -0400 IronPort-SDR: icqlQwHEHGbNddpNc6CN4XRLQtgWILYn7qw8UOJOJ/iE762x9CV1Fknosq9EjjhX7GhXJMqF33 2IurGesQOlEQ== X-IronPort-AV: E=McAfee;i="6000,8403,9695"; a="212681245" X-IronPort-AV: E=Sophos;i="5.75,405,1589266800"; d="scan'208";a="212681245" X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False Received: from orsmga008.jf.intel.com ([10.7.209.65]) by orsmga104.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 27 Jul 2020 23:20:56 -0700 IronPort-SDR: VlRe0kE4IZ+SAp93BNM4mHHkG26qEW/bt1adkGZP7Nv8ca8WydhAw0wdEthVR85g4/0dvk00qi 850q/82Zvx4Q== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.75,405,1589266800"; d="scan'208";a="320274417" Received: from jacob-builder.jf.intel.com ([10.7.199.155]) by orsmga008.jf.intel.com with ESMTP; 27 Jul 2020 23:20:56 -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 v6 11/15] vfio/type1: Allow invalidating first-level/stage IOMMU cache Date: Mon, 27 Jul 2020 23:27:40 -0700 Message-Id: <1595917664-33276-12-git-send-email-yi.l.liu@intel.com> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1595917664-33276-1-git-send-email-yi.l.liu@intel.com> References: <1595917664-33276-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 This patch provides an interface allowing the userspace to invalidate IOMMU cache for first-level page table. It is required when the first level IOMMU page table is not managed by the host kernel in the nested translation setup. Cc: Kevin Tian CC: Jacob Pan Cc: Alex Williamson Cc: Eric Auger Cc: Jean-Philippe Brucker Cc: Joerg Roedel Cc: Lu Baolu Signed-off-by: Liu Yi L Signed-off-by: Eric Auger Signed-off-by: Jacob Pan --- v1 -> v2: *) rename from "vfio/type1: Flush stage-1 IOMMU cache for nesting type" *) rename vfio_cache_inv_fn() to vfio_dev_cache_invalidate_fn() *) vfio_dev_cache_inv_fn() always successful *) remove VFIO_IOMMU_CACHE_INVALIDATE, and reuse VFIO_IOMMU_NESTING_OP --- drivers/vfio/vfio_iommu_type1.c | 42 +++++++++++++++++++++++++++++++++++++++++ include/uapi/linux/vfio.h | 3 +++ 2 files changed, 45 insertions(+) diff --git a/drivers/vfio/vfio_iommu_type1.c b/drivers/vfio/vfio_iommu_type1.c index 245436e..bf95a0f 100644 --- a/drivers/vfio/vfio_iommu_type1.c +++ b/drivers/vfio/vfio_iommu_type1.c @@ -3056,6 +3056,45 @@ static long vfio_iommu_handle_pgtbl_op(struct vfio_iommu *iommu, return ret; } +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; + + iommu_uapi_cache_invalidate(dc->domain, dev, (void __user *)arg); + return 0; +} + +static long vfio_iommu_invalidate_cache(struct vfio_iommu *iommu, + unsigned long arg) +{ + struct domain_capsule dc = { .data = &arg }; + struct iommu_nesting_info *info; + int ret; + + mutex_lock(&iommu->lock); + /* + * Cache invalidation is required for any nesting IOMMU, + * so no need to check system-wide PASID support. + */ + info = iommu->nesting_info; + if (!info || !(info->features & IOMMU_NESTING_FEAT_CACHE_INVLD)) { + ret = -EOPNOTSUPP; + goto out_unlock; + } + + ret = vfio_get_nesting_domain_capsule(iommu, &dc); + if (ret) + goto out_unlock; + + iommu_group_for_each_dev(dc.group->iommu_group, &dc, + vfio_dev_cache_invalidate_fn); + +out_unlock: + mutex_unlock(&iommu->lock); + return ret; +} + static long vfio_iommu_type1_nesting_op(struct vfio_iommu *iommu, unsigned long arg) { @@ -3078,6 +3117,9 @@ static long vfio_iommu_type1_nesting_op(struct vfio_iommu *iommu, case VFIO_IOMMU_NESTING_OP_UNBIND_PGTBL: ret = vfio_iommu_handle_pgtbl_op(iommu, false, arg + minsz); break; + case VFIO_IOMMU_NESTING_OP_CACHE_INVLD: + ret = vfio_iommu_invalidate_cache(iommu, arg + minsz); + break; default: ret = -EINVAL; } diff --git a/include/uapi/linux/vfio.h b/include/uapi/linux/vfio.h index 9501cfb..48e2fb5 100644 --- a/include/uapi/linux/vfio.h +++ b/include/uapi/linux/vfio.h @@ -1225,6 +1225,8 @@ struct vfio_iommu_type1_pasid_request { * +-----------------+-----------------------------------------------+ * | UNBIND_PGTBL | struct iommu_gpasid_bind_data | * +-----------------+-----------------------------------------------+ + * | CACHE_INVLD | struct iommu_cache_invalidate_info | + * +-----------------+-----------------------------------------------+ * * returns: 0 on success, -errno on failure. */ @@ -1237,6 +1239,7 @@ struct vfio_iommu_type1_nesting_op { #define VFIO_IOMMU_NESTING_OP_BIND_PGTBL (0) #define VFIO_IOMMU_NESTING_OP_UNBIND_PGTBL (1) +#define VFIO_IOMMU_NESTING_OP_CACHE_INVLD (2) #define VFIO_IOMMU_NESTING_OP _IO(VFIO_TYPE, VFIO_BASE + 19) -- 2.7.4