Skip to contentSkip to conversation

IOASID extensions for guest SVA

PATCH In review Jacob Pan 15 participants 269 messages Feb 27, 2021 – Jun 8, 2021 1 message not in the archive Page 1 of 2

Subsystems (7)
lore
Share & tools
lore

Jacob Pan

author
#0 lore raw

v4 cover IOASID extensions for guest SVA

I/O Address Space ID (IOASID) core code was introduced in v5.5 as a generic kernel allocator service for both PCIe Process Address Space ID (PASID) and ARM SMMU's Substream ID. IOASIDs are used to associate DMA requests with virtual address spaces, including both host and guest.

In addition to providing basic ID allocation, ioasid_set was defined as a token that is shared by a group of IOASIDs. This set token can be used for permission checking, but lack some features to address the following needs by guest Shared Virtual Address (SVA).
- Manage IOASIDs by group, group ownership, quota, etc.
- State synchronization among IOASID users (e.g. IOMMU driver, KVM, device drivers)
- Non-identity guest-host IOASID mapping
- Lifecycle management

This patchset introduces the following extensions as solutions to the problems above.
- Redefine and extend IOASID set such that IOASIDs can be managed by groups/pools.
- Add notifications for IOASID state synchronization
- Extend reference counting for life cycle alignment among multiple users
- Support ioasid_set private IDs, which can be used as guest IOASIDs
- Add a new cgroup controller for resource distribution

Please refer to Documentation/admin-guide/cgroup-v1/ioasids.rst and Documentation/driver-api/ioasid.rst in the enclosed patches for more details.

Based on discussions on LKML[1], a direction change was made in v4 such that the user interfaces for IOASID allocation are extracted from VFIO subsystem. The proposed IOASID subsystem now consists of three components:
1. IOASID core[01-14]: provides APIs for allocation, pool management, notifications, and refcounting.
2. IOASID cgroup controller[RFC 15-17]: manage resource distribution[2].
3. IOASID user[RFC 18]: provides user allocation interface via /dev/ioasid

This patchset only included VT-d driver as users of some of the new APIs. VFIO and KVM patches are coming up to fully utilize the APIs introduced here.

[1] https://lore.kernel.org/linux-iommu/1599734733-6431-1-git-send-email-yi.l.liu-ral2JQCrhuEAvxtiuMwx3w@public.gmane.org/
[2] Note that ioasid quota management code can be removed once the IOASIDs
cgroup is ratified.

You can find this series, VFIO, KVM, and IOASID user at:
https://github.com/jacobpan/linux.git ioasid_v4
(VFIO and KVM patches will be available at this branch when published.)

This work is a result of collaboration with many people:
Liu, Yi L <[email protected]> Wu Hao <[email protected]> Ashok Raj <[email protected]> Kevin Tian <[email protected]>

Thanks,

Jacob

Changelog:

v4
- Introduced IOASIDs cgroup controller
- Introduced /dev/ioasid user API for allocation/free
- Added IOASID states and free function, aligned refcounting on v5.11 introduced by Jean.
- Support iommu-sva-lib (will converge VT-d code afterward)
- Added a shared ordered workqueue for notification work that requires thread context. Streamlined notification framework among multiple IOASID users.
- Added ioasid_set helper functions for taking per set operations

V3:
- Use consistent ioasid_set_ prefix for ioasid_set level APIs
- Make SPID and private detach/attach APIs symmetric
- Use the same ioasid_put semantics as Jean-Phillippe IOASID reference patch
- Take away the public ioasid_notify() function, notifications are now emitted by IOASID core as a result of certain IOASID APIs
- Partition into finer incremental patches
- Miscellaneous cleanup, locking, exception handling fixes based on v2 reviews

V2:
- Redesigned ioasid_set APIs, removed set ID
- Added set private ID (SPID) for guest PASID usage.
- Add per ioasid_set notification and priority support.
- Back to use spinlocks and atomic notifications.
- Added async work in VT-d driver to perform teardown outside atomic context

Jacob Pan (17):
docs: Document IO Address Space ID (IOASID) APIs iommu/ioasid: Rename ioasid_set_data() iommu/ioasid: Add a separate function for detach data iommu/ioasid: Support setting system-wide capacity iommu/ioasid: Redefine IOASID set and allocation APIs iommu/ioasid: Add free function and states iommu/ioasid: Add ioasid_set iterator helper functions iommu/ioasid: Introduce ioasid_set private ID iommu/ioasid: Introduce notification APIs iommu/ioasid: Support mm token type ioasid_set notifications iommu/ioasid: Add ownership check in guest bind iommu/vt-d: Remove mm reference for guest SVA iommu/ioasid: Add a workqueue for cleanup work iommu/vt-d: Listen to IOASID notifications
cgroup: Introduce ioasids controller
iommu/ioasid: Consult IOASIDs cgroup for allocation
docs: cgroup-v1: Add IOASIDs controller

Liu Yi L (1):
ioasid: Add /dev/ioasid for userspace

 Documentation/admin-guide/cgroup-v1/index.rst |   1 +
 .../admin-guide/cgroup-v1/ioasids.rst         | 107 ++
 Documentation/driver-api/index.rst            |   1 +
 Documentation/driver-api/ioasid.rst           | 510 +++++++++
 Documentation/userspace-api/index.rst         |   1 +
 Documentation/userspace-api/ioasid.rst        |  49 +
 drivers/iommu/Kconfig                         |   5 +
 drivers/iommu/Makefile                        |   1 +
 .../iommu/arm/arm-smmu-v3/arm-smmu-v3-sva.c   |   1 +
 drivers/iommu/intel/Kconfig                   |   1 +
 drivers/iommu/intel/iommu.c                   |  32 +-
 drivers/iommu/intel/pasid.h                   |   1 +
 drivers/iommu/intel/svm.c                     | 145 ++-
 drivers/iommu/ioasid.c                        | 983 +++++++++++++++++-
 drivers/iommu/ioasid_user.c                   | 297 ++++++
 drivers/iommu/iommu-sva-lib.c                 |  19 +-
 drivers/iommu/iommu.c                         |  16 +-
 include/linux/cgroup_subsys.h                 |   4 +
 include/linux/intel-iommu.h                   |   2 +
 include/linux/ioasid.h                        | 256 ++++-
 include/linux/miscdevice.h                    |   1 +
 include/uapi/linux/ioasid.h                   |  98 ++
 init/Kconfig                                  |   7 +
 kernel/cgroup/Makefile                        |   1 +
 kernel/cgroup/ioasids.c                       | 345 ++++++
 25 files changed, 2794 insertions(+), 90 deletions(-)
 create mode 100644 Documentation/admin-guide/cgroup-v1/ioasids.rst
 create mode 100644 Documentation/driver-api/ioasid.rst
 create mode 100644 Documentation/userspace-api/ioasid.rst
 create mode 100644 drivers/iommu/ioasid_user.c
 create mode 100644 include/uapi/linux/ioasid.h
 create mode 100644 kernel/cgroup/ioasids.c
Signature · 1 line
2.25.1
author
#1 lore raw

PATCH v4 1/18 docs: Document IO Address Space ID (IOASID) APIs

IOASID is used to identify address spaces that can be targeted by device DMA. It is a system-wide resource that is essential to its many users. This document is an attempt to help developers from all vendors navigate the APIs. At this time, ARM SMMU and Intel’s Scalable IO Virtualization (SIOV) enabled platforms are the primary users of IOASID. Examples of how SIOV components interact with the IOASID APIs are provided.

---
 Documentation/driver-api/index.rst  |   1 +
 Documentation/driver-api/ioasid.rst | 510 ++++++++++++++++++++++++++++
 2 files changed, 511 insertions(+)
 create mode 100644 Documentation/driver-api/ioasid.rst
Documentation/driver-api/index.rst +1 -0
index 2456d0a97ed8..baeec308cf2c 100644
@@ -37,6 +37,7 @@ available subsections can be seen below.
    pci/index
    spi
    i2c
+   ioasid
    ipmb
    ipmi
    i3c/index
Documentation/driver-api/ioasid.rst new file +510 -0

510 lines not on this page · raw message

Signature · 6 lines
2.25.1

_______________________________________________
iommu mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/iommu
author
#2 lore raw

PATCH v4 2/18 iommu/ioasid: Rename ioasid_set_data()

In reply to Jacob Pan

Rename ioasid_set_data() to ioasid_attach_data() to avoid confusion with struct ioasid_set. ioasid_set is a group of IOASIDs that share a common token.

---
 drivers/iommu/intel/svm.c | 6 +++---
 drivers/iommu/ioasid.c    | 6 +++---
 include/linux/ioasid.h    | 4 ++--
 3 files changed, 8 insertions(+), 8 deletions(-)
drivers/iommu/intel/svm.c +3 -3
index 18a9f05df407..0053df9edffc 100644
@@ -371,7 +371,7 @@ int intel_svm_bind_gpasid(struct iommu_domain *domain, struct device *dev,
 			svm->gpasid = data->gpasid;
 			svm->flags |= SVM_FLAG_GUEST_PASID;
 		}
-		ioasid_set_data(data->hpasid, svm);
+		ioasid_attach_data(data->hpasid, svm);
 		INIT_LIST_HEAD_RCU(&svm->devs);
 		mmput(svm->mm);
 	}
@@ -425,7 +425,7 @@ int intel_svm_bind_gpasid(struct iommu_domain *domain, struct device *dev,
 	list_add_rcu(&sdev->list, &svm->devs);
  out:
 	if (!IS_ERR_OR_NULL(svm) && list_empty(&svm->devs)) {
-		ioasid_set_data(data->hpasid, NULL);
+		ioasid_attach_data(data->hpasid, NULL);
 		kfree(svm);
 	}
 
@@ -468,7 +468,7 @@ int intel_svm_unbind_gpasid(struct device *dev, u32 pasid)
 				 * the unbind, IOMMU driver will get notified
 				 * and perform cleanup.
 				 */
-				ioasid_set_data(pasid, NULL);
+				ioasid_attach_data(pasid, NULL);
 				kfree(svm);
 			}
 		}
drivers/iommu/ioasid.c +3 -3
index 50ee27bbd04e..eeadf4586e0a 100644
@@ -259,14 +259,14 @@ void ioasid_unregister_allocator(struct ioasid_allocator_ops *ops)
 EXPORT_SYMBOL_GPL(ioasid_unregister_allocator);
 
 /**
- * ioasid_set_data - Set private data for an allocated ioasid
+ * ioasid_attach_data - Set private data for an allocated ioasid
  * @ioasid: the ID to set data
  * @data:   the private data
  *
  * For IOASID that is already allocated, private data can be set
  * via this API. Future lookup can be done via ioasid_find.
  */
-int ioasid_set_data(ioasid_t ioasid, void *data)
+int ioasid_attach_data(ioasid_t ioasid, void *data)
 {
 	struct ioasid_data *ioasid_data;
 	int ret = 0;
@@ -288,7 +288,7 @@ int ioasid_set_data(ioasid_t ioasid, void *data)
 
 	return ret;
 }
-EXPORT_SYMBOL_GPL(ioasid_set_data);
+EXPORT_SYMBOL_GPL(ioasid_attach_data);
 
 /**
  * ioasid_alloc - Allocate an IOASID
include/linux/ioasid.h +2 -2
index e9dacd4b9f6b..60ea279802b8 100644
@@ -40,7 +40,7 @@ void *ioasid_find(struct ioasid_set *set, ioasid_t ioasid,
 		  bool (*getter)(void *));
 int ioasid_register_allocator(struct ioasid_allocator_ops *allocator);
 void ioasid_unregister_allocator(struct ioasid_allocator_ops *allocator);
-int ioasid_set_data(ioasid_t ioasid, void *data);
+int ioasid_attach_data(ioasid_t ioasid, void *data);
 
 #else /* !CONFIG_IOASID */
 static inline ioasid_t ioasid_alloc(struct ioasid_set *set, ioasid_t min,
@@ -73,7 +73,7 @@ static inline void ioasid_unregister_allocator(struct ioasid_allocator_ops *allo
 {
 }
 
-static inline int ioasid_set_data(ioasid_t ioasid, void *data)
+static inline int ioasid_attach_data(ioasid_t ioasid, void *data)
 {
 	return -ENOTSUPP;
 }
Signature · 1 line
2.25.1
author
#3 lore raw

PATCH v4 3/18 iommu/ioasid: Add a separate function for detach data

In reply to Jacob Pan

IOASID private data can be cleared by ioasid_attach_data() with a NULL data pointer. A common use case is for a caller to free the data afterward. ioasid_attach_data() calls synchronize_rcu() before return such that free data can be sure without outstanding readers. However, since synchronize_rcu() may sleep, ioasid_attach_data() cannot be used under spinlocks.

This patch adds ioasid_detach_data() as a separate API where synchronize_rcu() is called only in this case. ioasid_attach_data() can then be used under spinlocks. In addition, this change makes the API symmetrical.

---
 drivers/iommu/intel/svm.c |  4 +--
 drivers/iommu/ioasid.c    | 54 +++++++++++++++++++++++++++++++--------
 include/linux/ioasid.h    |  5 +++-
 3 files changed, 50 insertions(+), 13 deletions(-)
drivers/iommu/intel/svm.c +2 -2
index 0053df9edffc..68372a7eb8b5 100644
@@ -425,7 +425,7 @@ int intel_svm_bind_gpasid(struct iommu_domain *domain, struct device *dev,
 	list_add_rcu(&sdev->list, &svm->devs);
  out:
 	if (!IS_ERR_OR_NULL(svm) && list_empty(&svm->devs)) {
-		ioasid_attach_data(data->hpasid, NULL);
+		ioasid_detach_data(data->hpasid);
 		kfree(svm);
 	}
 
@@ -468,7 +468,7 @@ int intel_svm_unbind_gpasid(struct device *dev, u32 pasid)
 				 * the unbind, IOMMU driver will get notified
 				 * and perform cleanup.
 				 */
-				ioasid_attach_data(pasid, NULL);
+				ioasid_detach_data(pasid);
 				kfree(svm);
 			}
 		}
drivers/iommu/ioasid.c +44 -10

67 lines not on this page · raw message

include/linux/ioasid.h +4 -1
index 60ea279802b8..f6e705f832f0 100644
@@ -41,7 +41,7 @@ void *ioasid_find(struct ioasid_set *set, ioasid_t ioasid,
 int ioasid_register_allocator(struct ioasid_allocator_ops *allocator);
 void ioasid_unregister_allocator(struct ioasid_allocator_ops *allocator);
 int ioasid_attach_data(ioasid_t ioasid, void *data);
-
+void ioasid_detach_data(ioasid_t ioasid);
 #else /* !CONFIG_IOASID */
 static inline ioasid_t ioasid_alloc(struct ioasid_set *set, ioasid_t min,
 				    ioasid_t max, void *private)
@@ -78,5 +78,8 @@ static inline int ioasid_attach_data(ioasid_t ioasid, void *data)
 	return -ENOTSUPP;
 }
 
+static inline void ioasid_detach_data(ioasid_t ioasid)
+{
+}
 #endif /* CONFIG_IOASID */
 #endif /* __LINUX_IOASID_H */
Signature · 1 line
2.25.1
author
#4 lore raw

PATCH v4 4/18 iommu/ioasid: Support setting system-wide capacity

In reply to Jacob Pan

IOASID is a system-wide resource that could vary on different systems. The default capacity is 20 bits as defined in the PCI-E specifications. This patch adds a function to allow adjusting system IOASID capacity. For VT-d this is set during boot as part of the Intel IOMMU initialization. APIs also added to support runtime capacity reservation, potentially by cgroups.

---
 drivers/iommu/intel/iommu.c |  5 +++
 drivers/iommu/ioasid.c      | 70 +++++++++++++++++++++++++++++++++++++
 include/linux/ioasid.h      | 18 ++++++++++
 3 files changed, 93 insertions(+)
drivers/iommu/intel/iommu.c +5 -0
index f665322a0991..6f42ff7d171d 100644
@@ -41,6 +41,7 @@
 #include <linux/dma-direct.h>
 #include <linux/crash_dump.h>
 #include <linux/numa.h>
+#include <linux/ioasid.h>
 #include <asm/irq_remapping.h>
 #include <asm/cacheflush.h>
 #include <asm/iommu.h>
@@ -3298,6 +3299,10 @@ static int __init init_dmars(void)
 	if (ret)
 		goto free_iommu;
 
+	/* PASID is needed for scalable mode irrespective to SVM */
+	if (intel_iommu_sm)
+		ioasid_install_capacity(intel_pasid_max_id);
+
 	/*
 	 * for each drhd
 	 *   enable fault log
drivers/iommu/ioasid.c +70 -0

82 lines not on this page · raw message

include/linux/ioasid.h +18 -0
index f6e705f832f0..2780bdc84b94 100644
@@ -32,6 +32,10 @@ struct ioasid_allocator_ops {
 #define DECLARE_IOASID_SET(name) struct ioasid_set name = { 0 }
 
 #if IS_ENABLED(CONFIG_IOASID)
+void ioasid_install_capacity(ioasid_t total);
+int ioasid_reserve_capacity(ioasid_t nr_ioasid);
+int ioasid_cancel_capacity(ioasid_t nr_ioasid);
+
 ioasid_t ioasid_alloc(struct ioasid_set *set, ioasid_t min, ioasid_t max,
 		      void *private);
 void ioasid_get(ioasid_t ioasid);
@@ -43,6 +47,20 @@ void ioasid_unregister_allocator(struct ioasid_allocator_ops *allocator);
 int ioasid_attach_data(ioasid_t ioasid, void *data);
 void ioasid_detach_data(ioasid_t ioasid);
 #else /* !CONFIG_IOASID */
+static inline void ioasid_install_capacity(ioasid_t total)
+{
+}
+
+static inline int ioasid_reserve_capacity(ioasid_t nr_ioasid)
+{
+	return -ENOSPC;
+}
+
+static inline int ioasid_cancel_capacity(ioasid_t nr_ioasid)
+{
+	return -EINVAL;
+}
+
 static inline ioasid_t ioasid_alloc(struct ioasid_set *set, ioasid_t min,
 				    ioasid_t max, void *private)
 {
Signature · 1 line
2.25.1
author
#5 lore raw

PATCH v4 5/18 iommu/ioasid: Redefine IOASID set and allocation APIs

In reply to Jacob Pan

ioasid_set was introduced as an arbitrary token that is shared by a group of IOASIDs. For example, two IOASIDs allocated via the same ioasid_set pointer belong to the same set.

For guest SVA usages, system-wide IOASID resources need to be partitioned such that each VM can have its own quota and being managed separately. ioasid_set is the perfect candidate for meeting such requirements. This patch redefines and extends ioasid_set with the following new fields:
- Quota
- Reference count
- Storage of its namespace
- The token is now stored in the ioasid_set with types

Basic ioasid_set level APIs are introduced that wire up these new data. Existing users of IOASID APIs are converted where a host IOASID set is allocated for bare-metal usages. Including VT-d driver and iommu-sva-lib.

---
 .../iommu/arm/arm-smmu-v3/arm-smmu-v3-sva.c   |   1 +
 drivers/iommu/intel/iommu.c                   |  27 +-
 drivers/iommu/intel/pasid.h                   |   1 +
 drivers/iommu/intel/svm.c                     |  25 +-
 drivers/iommu/ioasid.c                        | 288 +++++++++++++++---
 drivers/iommu/iommu-sva-lib.c                 |  19 +-
 include/linux/ioasid.h                        |  68 ++++-
 7 files changed, 361 insertions(+), 68 deletions(-)
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3-sva.c +1 -0
index e13b092e6004..588aa66ed5e4 100644
@@ -459,6 +459,7 @@ int arm_smmu_master_enable_sva(struct arm_smmu_master *master)
 {
 	mutex_lock(&sva_lock);
 	master->sva_enabled = true;
+	iommu_sva_init();
 	mutex_unlock(&sva_lock);
 
 	return 0;
drivers/iommu/intel/iommu.c +20 -7
index 6f42ff7d171d..eb9868061545 100644
@@ -103,6 +103,9 @@
  */
 #define INTEL_IOMMU_PGSIZES	(~0xFFFUL)
 
+/* PASIDs used by host SVM */
+struct ioasid_set *host_pasid_set;
+
 static inline int agaw_to_level(int agaw)
 {
 	return agaw + 2;
@@ -173,6 +176,7 @@ static struct intel_iommu **g_iommus;
 
 static void __init check_tylersburg_isoch(void);
 static int rwbf_quirk;
+static bool scalable_mode_support(void);
 
 /*
  * set to 1 to panic kernel if can't successfully enable VT-d
@@ -3114,8 +3118,8 @@ static void intel_vcmd_ioasid_free(ioasid_t ioasid, void *data)
 	 * Sanity check the ioasid owner is done at upper layer, e.g. VFIO
 	 * We can only free the PASID when all the devices are unbound.
 	 */
-	if (ioasid_find(NULL, ioasid, NULL)) {
-		pr_alert("Cannot free active IOASID %d\n", ioasid);
+	if (IS_ERR(ioasid_find(host_pasid_set, ioasid, NULL))) {
+		pr_err("IOASID %d to be freed but not in system set\n", ioasid);
 		return;
 	}
 	vcmd_free_pasid(iommu, ioasid);
@@ -3300,8 +3304,17 @@ static int __init init_dmars(void)
 		goto free_iommu;
 
 	/* PASID is needed for scalable mode irrespective to SVM */
-	if (intel_iommu_sm)
+	if (scalable_mode_support()) {
 		ioasid_install_capacity(intel_pasid_max_id);
+		/* We should not run out of IOASIDs at boot */
+		host_pasid_set = ioasid_set_alloc(NULL, PID_MAX_DEFAULT,
+						  IOASID_SET_TYPE_NULL);
+		if (IS_ERR_OR_NULL(host_pasid_set)) {
+			pr_err("Failed to allocate host PASID set %lu\n",
+				PTR_ERR(host_pasid_set));
+			intel_iommu_sm = 0;
+		}
+	}
 
 	/*
 	 * for each drhd
@@ -3348,7 +3361,7 @@ static int __init init_dmars(void)
 		disable_dmar_iommu(iommu);
 		free_dmar_iommu(iommu);
 	}
-
+	ioasid_set_free(host_pasid_set);
 	kfree(g_iommus);
 
 error:
@@ -4573,7 +4586,7 @@ static int aux_domain_add_dev(struct dmar_domain *domain,
 		u32 pasid;
 
 		/* No private data needed for the default pasid */
-		pasid = ioasid_alloc(NULL, PASID_MIN,
+		pasid = ioasid_alloc(host_pasid_set, PASID_MIN,
 				     pci_max_pasids(to_pci_dev(dev)) - 1,
 				     NULL);
 		if (pasid == INVALID_IOASID) {
@@ -4630,7 +4643,7 @@ static int aux_domain_add_dev(struct dmar_domain *domain,
 link_failed:
 	spin_unlock_irqrestore(&device_domain_lock, flags);
 	if (list_empty(&domain->subdevices) && domain->default_pasid > 0)
-		ioasid_put(domain->default_pasid);
+		ioasid_put(host_pasid_set, domain->default_pasid);
 
 	return ret;
 }
@@ -4660,7 +4673,7 @@ static void aux_domain_remove_dev(struct dmar_domain *domain,
 	spin_unlock_irqrestore(&device_domain_lock, flags);
 
 	if (list_empty(&domain->subdevices) && domain->default_pasid > 0)
-		ioasid_put(domain->default_pasid);
+		ioasid_put(host_pasid_set, domain->default_pasid);
 }
 
 static int prepare_domain_attach_device(struct iommu_domain *domain,
drivers/iommu/intel/pasid.h +1 -0
index 97dfcffbf495..12b5ca18de5d 100644
@@ -99,6 +99,7 @@ static inline bool pasid_pte_is_present(struct pasid_entry *pte)
 }
 
 extern unsigned int intel_pasid_max_id;
+extern struct ioasid_set *host_pasid_set;
 int intel_pasid_alloc_id(void *ptr, int start, int end, gfp_t gfp);
 void intel_pasid_free_id(u32 pasid);
 void *intel_pasid_lookup_id(u32 pasid);
drivers/iommu/intel/svm.c +15 -10
index 68372a7eb8b5..c469c24d23f5 100644
@@ -247,7 +247,9 @@ static LIST_HEAD(global_svm_list);
 	list_for_each_entry((sdev), &(svm)->devs, list)	\
 		if ((d) != (sdev)->dev) {} else
 
-static int pasid_to_svm_sdev(struct device *dev, unsigned int pasid,
+static int pasid_to_svm_sdev(struct device *dev,
+			     struct ioasid_set *set,
+			     unsigned int pasid,
 			     struct intel_svm **rsvm,
 			     struct intel_svm_dev **rsdev)
 {
@@ -261,7 +263,7 @@ static int pasid_to_svm_sdev(struct device *dev, unsigned int pasid,
 	if (pasid == INVALID_IOASID || pasid >= PASID_MAX)
 		return -EINVAL;
 
-	svm = ioasid_find(NULL, pasid, NULL);
+	svm = ioasid_find(set, pasid, NULL);
 	if (IS_ERR(svm))
 		return PTR_ERR(svm);
 
@@ -337,7 +339,8 @@ int intel_svm_bind_gpasid(struct iommu_domain *domain, struct device *dev,
 	dmar_domain = to_dmar_domain(domain);
 
 	mutex_lock(&pasid_mutex);
-	ret = pasid_to_svm_sdev(dev, data->hpasid, &svm, &sdev);
+	ret = pasid_to_svm_sdev(dev, NULL,
+				data->hpasid, &svm, &sdev);
 	if (ret)
 		goto out;
 
@@ -444,7 +447,7 @@ int intel_svm_unbind_gpasid(struct device *dev, u32 pasid)
 		return -EINVAL;
 
 	mutex_lock(&pasid_mutex);
-	ret = pasid_to_svm_sdev(dev, pasid, &svm, &sdev);
+	ret = pasid_to_svm_sdev(dev, NULL, pasid, &svm, &sdev);
 	if (ret)
 		goto out;
 
@@ -602,7 +605,7 @@ intel_svm_bind_mm(struct device *dev, unsigned int flags,
 			pasid_max = intel_pasid_max_id;
 
 		/* Do not use PASID 0, reserved for RID to PASID */
-		svm->pasid = ioasid_alloc(NULL, PASID_MIN,
+		svm->pasid = ioasid_alloc(host_pasid_set, PASID_MIN,
 					  pasid_max - 1, svm);
 		if (svm->pasid == INVALID_IOASID) {
 			kfree(svm);
@@ -619,7 +622,7 @@ intel_svm_bind_mm(struct device *dev, unsigned int flags,
 		if (mm) {
 			ret = mmu_notifier_register(&svm->notifier, mm);
 			if (ret) {
-				ioasid_put(svm->pasid);
+				ioasid_put(host_pasid_set, svm->pasid);
 				kfree(svm);
 				kfree(sdev);
 				goto out;
@@ -637,7 +640,7 @@ intel_svm_bind_mm(struct device *dev, unsigned int flags,
 		if (ret) {
 			if (mm)
 				mmu_notifier_unregister(&svm->notifier, mm);
-			ioasid_put(svm->pasid);
+			ioasid_put(host_pasid_set, svm->pasid);
 			kfree(svm);
 			kfree(sdev);
 			goto out;
@@ -689,7 +692,8 @@ static int intel_svm_unbind_mm(struct device *dev, u32 pasid)
 	if (!iommu)
 		goto out;
 
-	ret = pasid_to_svm_sdev(dev, pasid, &svm, &sdev);
+	ret = pasid_to_svm_sdev(dev, host_pasid_set,
+				pasid, &svm, &sdev);
 	if (ret)
 		goto out;
 
@@ -710,7 +714,7 @@ static int intel_svm_unbind_mm(struct device *dev, u32 pasid)
 			kfree_rcu(sdev, rcu);
 
 			if (list_empty(&svm->devs)) {
-				ioasid_put(svm->pasid);
+				ioasid_put(host_pasid_set, svm->pasid);
 				if (svm->mm) {
 					mmu_notifier_unregister(&svm->notifier, svm->mm);
 					/* Clear mm's pasid. */
@@ -1184,7 +1188,8 @@ int intel_svm_page_response(struct device *dev,
 		goto out;
 	}
 
-	ret = pasid_to_svm_sdev(dev, prm->pasid, &svm, &sdev);
+	ret = pasid_to_svm_sdev(dev, host_pasid_set,
+				prm->pasid, &svm, &sdev);
 	if (ret || !sdev) {
 		ret = -ENODEV;
 		goto out;
drivers/iommu/ioasid.c +249 -39

354 lines not on this page · raw message

drivers/iommu/iommu-sva-lib.c +14 -5
index bd41405d34e9..7f97a03a135b 100644
@@ -8,7 +8,16 @@
 #include "iommu-sva-lib.h"
 
 static DEFINE_MUTEX(iommu_sva_lock);
-static DECLARE_IOASID_SET(iommu_sva_pasid);
+static struct ioasid_set *iommu_sva_pasid;
+
+/* Must be called before PASID allocations can occur */
+void iommu_sva_init(void)
+{
+	if (iommu_sva_pasid)
+		return;
+	iommu_sva_pasid = ioasid_set_alloc(NULL, 0, IOASID_SET_TYPE_NULL);
+}
+EXPORT_SYMBOL_GPL(iommu_sva_init);
 
 /**
  * iommu_sva_alloc_pasid - Allocate a PASID for the mm
@@ -35,11 +44,11 @@ int iommu_sva_alloc_pasid(struct mm_struct *mm, ioasid_t min, ioasid_t max)
 	mutex_lock(&iommu_sva_lock);
 	if (mm->pasid) {
 		if (mm->pasid >= min && mm->pasid <= max)
-			ioasid_get(mm->pasid);
+			ioasid_get(iommu_sva_pasid, mm->pasid);
 		else
 			ret = -EOVERFLOW;
 	} else {
-		pasid = ioasid_alloc(&iommu_sva_pasid, min, max, mm);
+		pasid = ioasid_alloc(iommu_sva_pasid, min, max, mm);
 		if (pasid == INVALID_IOASID)
 			ret = -ENOMEM;
 		else
@@ -59,7 +68,7 @@ EXPORT_SYMBOL_GPL(iommu_sva_alloc_pasid);
 void iommu_sva_free_pasid(struct mm_struct *mm)
 {
 	mutex_lock(&iommu_sva_lock);
-	if (ioasid_put(mm->pasid))
+	if (ioasid_put(iommu_sva_pasid, mm->pasid))
 		mm->pasid = 0;
 	mutex_unlock(&iommu_sva_lock);
 }
@@ -81,6 +90,6 @@ static bool __mmget_not_zero(void *mm)
  */
 struct mm_struct *iommu_sva_find(ioasid_t pasid)
 {
-	return ioasid_find(&iommu_sva_pasid, pasid, __mmget_not_zero);
+	return ioasid_find(iommu_sva_pasid, pasid, __mmget_not_zero);
 }
 EXPORT_SYMBOL_GPL(iommu_sva_find);
include/linux/ioasid.h +61 -7

103 lines not on this page · raw message

Signature · 1 line
2.25.1
author
#240 lore raw

PATCH v4 6/18 iommu/ioasid: Add free function and states

In reply to Jacob Pan

When an actively used IOASID is freed due to exceptions, users must be notified to perform the cleanup. The IOASID shall be put in a pending state until all users completed their cleanup work.

This patch adds ioasid_free() function to let the caller initiate the freeing process. Both ioasid_free() and ioasid_put() decrements reference counts. Unlike ioasid_put(), the ioasid_free() function also transition the IOASID to the free pending state where further ioasid_get() is prohibited. This paves the way for FREE event notifications that will be introduced next.

---
 drivers/iommu/ioasid.c | 73 ++++++++++++++++++++++++++++++++++++++++++
 include/linux/ioasid.h |  5 +++
 2 files changed, 78 insertions(+)
drivers/iommu/ioasid.c +73 -0
index d7b476651027..a10f8154c680 100644
@@ -15,8 +15,26 @@
 static ioasid_t ioasid_capacity = PCI_PASID_MAX;
 static ioasid_t ioasid_capacity_avail = PCI_PASID_MAX;
 static DEFINE_XARRAY_ALLOC(ioasid_sets);
+
+enum ioasid_state {
+	IOASID_STATE_IDLE,
+	IOASID_STATE_ACTIVE,
+	IOASID_STATE_FREE_PENDING,
+};
+
+/**
+ * struct ioasid_data - Meta data about ioasid
+ *
+ * @id:		Unique ID
+ * @refs:	Number of active users
+ * @state:	Track state of the IOASID
+ * @set:	ioasid_set of the IOASID belongs to
+ * @private:	Private data associated with the IOASID
+ * @rcu:	For free after RCU grace period
+ */
 struct ioasid_data {
 	ioasid_t id;
+	enum ioasid_state state;
 	struct ioasid_set *set;
 	void *private;
 	struct rcu_head rcu;
@@ -597,6 +615,7 @@ ioasid_t ioasid_alloc(struct ioasid_set *set, ioasid_t min, ioasid_t max,
 		goto exit_free;
 	}
 	data->id = id;
+	data->state = IOASID_STATE_IDLE;
 
 	/* Store IOASID in the per set data */
 	if (xa_err(xa_store(&set->xa, id, data, GFP_ATOMIC))) {
@@ -631,6 +650,56 @@ static void ioasid_do_free_locked(struct ioasid_data *data)
 		ioasid_set_free_locked(data->set);
 }
 
+static void ioasid_free_locked(struct ioasid_set *set, ioasid_t ioasid)
+{
+	struct ioasid_data *data;
+
+	data = xa_load(&active_allocator->xa, ioasid);
+	if (!data) {
+		pr_err_ratelimited("Trying to free unknown IOASID %u\n", ioasid);
+		return;
+	}
+	if (data->set != set) {
+		pr_warn("Cannot free IOASID %u due to set ownership\n", ioasid);
+		return;
+	}
+	/* Check if the set exists */
+	if (WARN_ON(!xa_load(&ioasid_sets, data->set->id)))
+		return;
+
+	/* Free is already in progress */
+	if (data->state == IOASID_STATE_FREE_PENDING)
+		return;
+
+	data->state = IOASID_STATE_FREE_PENDING;
+	/*
+	 * If the refcount is 1, it means there is no other users of the IOASID
+	 * other than IOASID core itself. There is no need to notify anyone.
+	 */
+	if (!refcount_dec_and_test(&data->refs))
+		return;
+
+	ioasid_do_free_locked(data);
+}
+
+/**
+ * ioasid_free - Drop reference on an IOASID. Free if refcount drops to 0,
+ *               including free from its set and system-wide list.
+ * @set:	The ioasid_set to check permission with. If not NULL, IOASID
+ *		free will fail if the set does not match.
+ * @ioasid:	The IOASID to remove
+ *
+ * TODO: return true if all references dropped, false if async work is in
+ * progress, IOASID is in FREE_PENDING state. wait queue to be used for blocking
+ * free task.
+ */
+void ioasid_free(struct ioasid_set *set, ioasid_t ioasid)
+{
+	spin_lock(&ioasid_allocator_lock);
+	ioasid_free_locked(set, ioasid);
+	spin_unlock(&ioasid_allocator_lock);
+}
+EXPORT_SYMBOL_GPL(ioasid_free);
 int ioasid_get_locked(struct ioasid_set *set, ioasid_t ioasid)
 {
 	struct ioasid_data *data;
@@ -640,6 +709,10 @@ int ioasid_get_locked(struct ioasid_set *set, ioasid_t ioasid)
 		pr_err("Trying to get unknown IOASID %u\n", ioasid);
 		return -EINVAL;
 	}
+	if (data->state == IOASID_STATE_FREE_PENDING) {
+		pr_err("Trying to get IOASID being freed%u\n", ioasid);
+		return -EBUSY;
+	}
 
 	/* Check set ownership if the set is non-null */
 	if (set && data->set != set) {
include/linux/ioasid.h +5 -0
index 095f4e50dc58..cabaf0b0348f 100644
@@ -72,6 +72,7 @@ int ioasid_get(struct ioasid_set *set, ioasid_t ioasid);
 int ioasid_get_locked(struct ioasid_set *set, ioasid_t ioasid);
 bool ioasid_put(struct ioasid_set *set, ioasid_t ioasid);
 bool ioasid_put_locked(struct ioasid_set *set, ioasid_t ioasid);
+void ioasid_free(struct ioasid_set *set, ioasid_t ioasid);
 void *ioasid_find(struct ioasid_set *set, ioasid_t ioasid,
 		  bool (*getter)(void *));
 int ioasid_register_allocator(struct ioasid_allocator_ops *allocator);
@@ -105,6 +106,10 @@ static inline struct ioasid_set *ioasid_set_alloc(void *token, ioasid_t quota,
 	return ERR_PTR(-ENOTSUPP);
 }
 
+static inline void ioasid_free(struct ioasid_set *set, ioasid_t ioasid)
+{
+}
+
 static inline struct ioasid_set *ioasid_find_mm_set(struct mm_struct *token)
 {
 	return NULL;
Signature · 1 line
2.25.1
author
#241 lore raw

PATCH v4 7/18 iommu/ioasid: Add ioasid_set iterator helper functions

In reply to Jacob Pan

Users of an ioasid_set may not keep track of all the IOASIDs allocated under the set. When collective actions are needed for each IOASIDs, it is useful to iterate over all the IOASIDs within the set. For example, when the ioasid_set is freed, the user might perform the same cleanup operation on each IOASID.

This patch adds an API to iterate all the IOASIDs within the set.

---
 drivers/iommu/ioasid.c | 84 ++++++++++++++++++++++++++++++++++++++++++
 include/linux/ioasid.h | 20 ++++++++++
 2 files changed, 104 insertions(+)
drivers/iommu/ioasid.c +84 -0
index a10f8154c680..9a3ba157dec3 100644
@@ -700,6 +700,61 @@ void ioasid_free(struct ioasid_set *set, ioasid_t ioasid)
 	spin_unlock(&ioasid_allocator_lock);
 }
 EXPORT_SYMBOL_GPL(ioasid_free);
+
+/**
+ * ioasid_free_all_in_set
+ *
+ * @brief
+ * Free all PASIDs from system-wide IOASID pool, all subscribers gets
+ * notified and do cleanup of their own.
+ * Note that some references of the IOASIDs within the set can still
+ * be held after the free call. This is OK in that the IOASIDs will be
+ * marked inactive, the only operations can be done is ioasid_put.
+ * No need to track IOASID set states since there is no reclaim phase.
+ *
+ * @param
+ * struct ioasid_set where all IOASIDs within the set will be freed.
+ */
+void ioasid_free_all_in_set(struct ioasid_set *set)
+{
+	struct ioasid_data *entry;
+	unsigned long index;
+
+	if (!ioasid_set_is_valid(set))
+		return;
+
+	if (xa_empty(&set->xa))
+		return;
+
+	if (!atomic_read(&set->nr_ioasids))
+		return;
+	spin_lock(&ioasid_allocator_lock);
+	xa_for_each(&set->xa, index, entry) {
+		ioasid_free_locked(set, index);
+		/* Free from per set private pool */
+		xa_erase(&set->xa, index);
+	}
+	spin_unlock(&ioasid_allocator_lock);
+}
+EXPORT_SYMBOL_GPL(ioasid_free_all_in_set);
+
+/**
+ * ioasid_set_for_each_ioasid
+ * @brief
+ * Iterate over all the IOASIDs within the set
+ */
+void ioasid_set_for_each_ioasid(struct ioasid_set *set,
+				void (*fn)(ioasid_t id, void *data),
+				void *data)
+{
+	struct ioasid_data *entry;
+	unsigned long index;
+
+	xa_for_each(&set->xa, index, entry)
+		fn(index, data);
+}
+EXPORT_SYMBOL_GPL(ioasid_set_for_each_ioasid);
+
 int ioasid_get_locked(struct ioasid_set *set, ioasid_t ioasid)
 {
 	struct ioasid_data *data;
@@ -789,6 +844,35 @@ bool ioasid_put(struct ioasid_set *set, ioasid_t ioasid)
 }
 EXPORT_SYMBOL_GPL(ioasid_put);
 
+/**
+ * @brief
+ * Find the ioasid_set of an IOASID. As long as the IOASID is valid,
+ * the set must be valid since the refcounting is based on the number of IOASID
+ * in the set.
+ *
+ * @param ioasid
+ * @return struct ioasid_set*
+ */
+struct ioasid_set *ioasid_find_set(ioasid_t ioasid)
+{
+	struct ioasid_allocator_data *idata;
+	struct ioasid_data *ioasid_data;
+	struct ioasid_set *set = NULL;
+
+	rcu_read_lock();
+	idata = rcu_dereference(active_allocator);
+	ioasid_data = xa_load(&idata->xa, ioasid);
+	if (!ioasid_data) {
+		set = ERR_PTR(-ENOENT);
+		goto unlock;
+	}
+	set = ioasid_data->set;
+unlock:
+	rcu_read_unlock();
+	return set;
+}
+EXPORT_SYMBOL_GPL(ioasid_find_set);
+
 /**
  * ioasid_find - Find IOASID data
  * @set: the IOASID set
include/linux/ioasid.h +20 -0
index cabaf0b0348f..e7f3e6108724 100644
@@ -73,12 +73,17 @@ int ioasid_get_locked(struct ioasid_set *set, ioasid_t ioasid);
 bool ioasid_put(struct ioasid_set *set, ioasid_t ioasid);
 bool ioasid_put_locked(struct ioasid_set *set, ioasid_t ioasid);
 void ioasid_free(struct ioasid_set *set, ioasid_t ioasid);
+void ioasid_free_all_in_set(struct ioasid_set *set);
 void *ioasid_find(struct ioasid_set *set, ioasid_t ioasid,
 		  bool (*getter)(void *));
+struct ioasid_set *ioasid_find_set(ioasid_t ioasid);
 int ioasid_register_allocator(struct ioasid_allocator_ops *allocator);
 void ioasid_unregister_allocator(struct ioasid_allocator_ops *allocator);
 int ioasid_attach_data(ioasid_t ioasid, void *data);
 void ioasid_detach_data(ioasid_t ioasid);
+void ioasid_set_for_each_ioasid(struct ioasid_set *sdata,
+				void (*fn)(ioasid_t id, void *data),
+				void *data);
 #else /* !CONFIG_IOASID */
 static inline void ioasid_install_capacity(ioasid_t total)
 {
@@ -158,5 +163,20 @@ static inline int ioasid_attach_data(ioasid_t ioasid, void *data)
 static inline void ioasid_detach_data(ioasid_t ioasid)
 {
 }
+
+static inline void ioasid_free_all_in_set(struct ioasid_set *set)
+{
+}
+
+static inline struct ioasid_set *ioasid_find_set(ioasid_t ioasid)
+{
+	return ERR_PTR(-ENOTSUPP);
+}
+
+static inline void ioasid_set_for_each_ioasid(struct ioasid_set *sdata,
+					      void (*fn)(ioasid_t id, void *data),
+					      void *data)
+{
+}
 #endif /* CONFIG_IOASID */
 #endif /* __LINUX_IOASID_H */
Signature · 1 line
2.25.1
author
#242 lore raw

PATCH v4 8/18 iommu/ioasid: Introduce ioasid_set private ID

In reply to Jacob Pan

When an IOASID set is used for guest SVA, each VM will acquire its ioasid_set for IOASID allocations. IOASIDs within the VM must have a host/physical IOASID backing, mapping between guest and host IOASIDs can be non-identical. IOASID set private ID (SPID) is introduced in this patch to be used as guest IOASID. However, the concept of ioasid_set specific namespace is generic, thus named SPID.

As SPID namespace is within the IOASID set, the IOASID core can provide lookup services at both directions. SPIDs may not be available when its IOASID is allocated, the mapping between SPID and IOASID is usually established when a guest page table is bound to a host PASID.

---
 drivers/iommu/ioasid.c | 104 +++++++++++++++++++++++++++++++++++++++++
 include/linux/ioasid.h |  18 +++++++
 2 files changed, 122 insertions(+)
drivers/iommu/ioasid.c +104 -0
index 9a3ba157dec3..7707bb608bdd 100644
@@ -26,6 +26,7 @@ enum ioasid_state {
  * struct ioasid_data - Meta data about ioasid
  *
  * @id:		Unique ID
+ * @spid:	Private ID unique within a set
  * @refs:	Number of active users
  * @state:	Track state of the IOASID
  * @set:	ioasid_set of the IOASID belongs to
@@ -34,6 +35,7 @@ enum ioasid_state {
  */
 struct ioasid_data {
 	ioasid_t id;
+	ioasid_t spid;
 	enum ioasid_state state;
 	struct ioasid_set *set;
 	void *private;
@@ -413,6 +415,107 @@ void ioasid_detach_data(ioasid_t ioasid)
 }
 EXPORT_SYMBOL_GPL(ioasid_detach_data);
 
+static ioasid_t ioasid_find_by_spid_locked(struct ioasid_set *set, ioasid_t spid, bool get)
+{
+	ioasid_t ioasid = INVALID_IOASID;
+	struct ioasid_data *entry;
+	unsigned long index;
+
+	if (!xa_load(&ioasid_sets, set->id)) {
+		pr_warn("Invalid set\n");
+		goto done;
+	}
+
+	xa_for_each(&set->xa, index, entry) {
+		if (spid == entry->spid) {
+			if (get)
+				refcount_inc(&entry->refs);
+			ioasid = index;
+		}
+	}
+done:
+	return ioasid;
+}
+
+/**
+ * ioasid_attach_spid - Attach ioasid_set private ID to an IOASID
+ *
+ * @ioasid: the system-wide IOASID to attach
+ * @spid:   the ioasid_set private ID of @ioasid
+ *
+ * After attching SPID, future lookup can be done via ioasid_find_by_spid().
+ */
+int ioasid_attach_spid(ioasid_t ioasid, ioasid_t spid)
+{
+	struct ioasid_data *data;
+	int ret = 0;
+
+	if (spid == INVALID_IOASID)
+		return -EINVAL;
+
+	spin_lock(&ioasid_allocator_lock);
+	data = xa_load(&active_allocator->xa, ioasid);
+
+	if (!data) {
+		pr_err("No IOASID entry %d to attach SPID %d\n",
+			ioasid, spid);
+		ret = -ENOENT;
+		goto done_unlock;
+	}
+	/* Check if SPID is unique within the set */
+	if (ioasid_find_by_spid_locked(data->set, spid, false) != INVALID_IOASID) {
+		ret = -EINVAL;
+		goto done_unlock;
+	}
+	data->spid = spid;
+
+done_unlock:
+	spin_unlock(&ioasid_allocator_lock);
+	return ret;
+}
+EXPORT_SYMBOL_GPL(ioasid_attach_spid);
+
+void ioasid_detach_spid(ioasid_t ioasid)
+{
+	struct ioasid_data *data;
+
+	spin_lock(&ioasid_allocator_lock);
+	data = xa_load(&active_allocator->xa, ioasid);
+
+	if (!data || data->spid == INVALID_IOASID) {
+		pr_err("Invalid IOASID entry %d to detach\n", ioasid);
+		goto done_unlock;
+	}
+	data->spid = INVALID_IOASID;
+
+done_unlock:
+	spin_unlock(&ioasid_allocator_lock);
+}
+EXPORT_SYMBOL_GPL(ioasid_detach_spid);
+
+/**
+ * ioasid_find_by_spid - Find the system-wide IOASID by a set private ID and
+ * its set.
+ *
+ * @set:	the ioasid_set to search within
+ * @spid:	the set private ID
+ * @get:	flag indicates whether to take a reference once found
+ *
+ * Given a set private ID and its IOASID set, find the system-wide IOASID. Take
+ * a reference upon finding the matching IOASID if @get is true. Return
+ * INVALID_IOASID if the IOASID is not found in the set or the set is not valid.
+ */
+ioasid_t ioasid_find_by_spid(struct ioasid_set *set, ioasid_t spid, bool get)
+{
+	ioasid_t ioasid;
+
+	spin_lock(&ioasid_allocator_lock);
+	ioasid = ioasid_find_by_spid_locked(set, spid, get);
+	spin_unlock(&ioasid_allocator_lock);
+	return ioasid;
+}
+EXPORT_SYMBOL_GPL(ioasid_find_by_spid);
+
 static inline bool ioasid_set_is_valid(struct ioasid_set *set)
 {
 	return xa_load(&ioasid_sets, set->id) == set;
@@ -616,6 +719,7 @@ ioasid_t ioasid_alloc(struct ioasid_set *set, ioasid_t min, ioasid_t max,
 	}
 	data->id = id;
 	data->state = IOASID_STATE_IDLE;
+	data->spid = INVALID_IOASID;
 
 	/* Store IOASID in the per set data */
 	if (xa_err(xa_store(&set->xa, id, data, GFP_ATOMIC))) {
include/linux/ioasid.h +18 -0
index e7f3e6108724..dcab02886cb5 100644
@@ -81,6 +81,9 @@ int ioasid_register_allocator(struct ioasid_allocator_ops *allocator);
 void ioasid_unregister_allocator(struct ioasid_allocator_ops *allocator);
 int ioasid_attach_data(ioasid_t ioasid, void *data);
 void ioasid_detach_data(ioasid_t ioasid);
+int ioasid_attach_spid(ioasid_t ioasid, ioasid_t spid);
+void ioasid_detach_spid(ioasid_t ioasid);
+ioasid_t ioasid_find_by_spid(struct ioasid_set *set, ioasid_t spid, bool get);
 void ioasid_set_for_each_ioasid(struct ioasid_set *sdata,
 				void (*fn)(ioasid_t id, void *data),
 				void *data);
@@ -173,6 +176,21 @@ static inline struct ioasid_set *ioasid_find_set(ioasid_t ioasid)
 	return ERR_PTR(-ENOTSUPP);
 }
 
+static inline int ioasid_attach_spid(ioasid_t ioasid, ioasid_t spid)
+{
+	return -ENOTSUPP;
+}
+
+static inline void ioasid_detach_spid(ioasid_t ioasid)
+{
+}
+
+static inline ioasid_t ioasid_find_by_spid(struct ioasid_set *set,
+					   ioasid_t spid, bool get)
+{
+	return INVALID_IOASID;
+}
+
 static inline void ioasid_set_for_each_ioasid(struct ioasid_set *sdata,
 					      void (*fn)(ioasid_t id, void *data),
 					      void *data)
Signature · 1 line
2.25.1
author
#243 lore raw

PATCH v4 9/18 iommu/ioasid: Introduce notification APIs

In reply to Jacob Pan

Relations among IOASID users largely follow a publisher-subscriber pattern. E.g. to support guest SVA on Intel Scalable I/O Virtualization (SIOV) enabled platforms, VFIO, IOMMU, device drivers, KVM are all users of IOASIDs. When a state change occurs, VFIO publishes the change event that needs to be processed by other users/subscribers.

This patch introduced two types of notifications: global and per ioasid_set. The latter is intended for users who only needs to handle events related to the IOASID of a given set.
For more information, refer to the kernel documentation at Documentation/ioasid.rst.

---
 drivers/iommu/ioasid.c | 111 +++++++++++++++++++++++++++++++++++++++--
 include/linux/ioasid.h |  54 ++++++++++++++++++++
 2 files changed, 161 insertions(+), 4 deletions(-)
drivers/iommu/ioasid.c +107 -4
index 7707bb608bdd..56577e745c4b 100644
@@ -10,12 +10,33 @@
 #include <linux/spinlock.h>
 #include <linux/xarray.h>
 
+/*
+ * An IOASID can have multiple consumers where each consumer may have
+ * hardware contexts associated with the IOASID.
+ * When a status change occurs, like on IOASID deallocation, notifier chains
+ * are used to keep the consumers in sync.
+ * This is a publisher-subscriber pattern where publisher can change the
+ * state of each IOASID, e.g. alloc/free, bind IOASID to a device and mm.
+ * On the other hand, subscribers get notified for the state change and
+ * keep local states in sync.
+ */
+static ATOMIC_NOTIFIER_HEAD(ioasid_notifier);
+static DEFINE_SPINLOCK(ioasid_nb_lock);
+
 /* Default to PCIe standard 20 bit PASID */
 #define PCI_PASID_MAX 0x100000
 static ioasid_t ioasid_capacity = PCI_PASID_MAX;
 static ioasid_t ioasid_capacity_avail = PCI_PASID_MAX;
 static DEFINE_XARRAY_ALLOC(ioasid_sets);
 
+struct ioasid_set_nb {
+	struct list_head	list;
+	struct notifier_block	*nb;
+	void			*token;
+	struct ioasid_set	*set;
+	bool			active;
+};
+
 enum ioasid_state {
 	IOASID_STATE_IDLE,
 	IOASID_STATE_ACTIVE,
@@ -415,6 +436,38 @@ void ioasid_detach_data(ioasid_t ioasid)
 }
 EXPORT_SYMBOL_GPL(ioasid_detach_data);
 
+/**
+ * ioasid_notify - Send notification on a given IOASID for status change.
+ *
+ * @data:	The IOASID data to which the notification will send
+ * @cmd:	Notification event sent by IOASID external users, can be
+ *		IOASID_BIND or IOASID_UNBIND.
+ *
+ * @flags:	Special instructions, e.g. notify within a set or global by
+ *		IOASID_NOTIFY_FLAG_SET or IOASID_NOTIFY_FLAG_ALL flags
+ * Caller must hold ioasid_allocator_lock and reference to the IOASID
+ */
+static int ioasid_notify(struct ioasid_data *data,
+			 enum ioasid_notify_val cmd, unsigned int flags)
+{
+	struct ioasid_nb_args args = { 0 };
+	int ret = 0;
+
+	if (flags & ~(IOASID_NOTIFY_FLAG_ALL | IOASID_NOTIFY_FLAG_SET))
+		return -EINVAL;
+
+	args.id = data->id;
+	args.set = data->set;
+	args.pdata = data->private;
+	args.spid = data->spid;
+	if (flags & IOASID_NOTIFY_FLAG_ALL)
+		ret = atomic_notifier_call_chain(&ioasid_notifier, cmd, &args);
+	if (flags & IOASID_NOTIFY_FLAG_SET)
+		ret = atomic_notifier_call_chain(&data->set->nh, cmd, &args);
+
+	return ret;
+}
+
 static ioasid_t ioasid_find_by_spid_locked(struct ioasid_set *set, ioasid_t spid, bool get)
 {
 	ioasid_t ioasid = INVALID_IOASID;
@@ -468,7 +521,7 @@ int ioasid_attach_spid(ioasid_t ioasid, ioasid_t spid)
 		goto done_unlock;
 	}
 	data->spid = spid;
-
+	ioasid_notify(data, IOASID_NOTIFY_BIND, IOASID_NOTIFY_FLAG_SET);
 done_unlock:
 	spin_unlock(&ioasid_allocator_lock);
 	return ret;
@@ -486,8 +539,8 @@ void ioasid_detach_spid(ioasid_t ioasid)
 		pr_err("Invalid IOASID entry %d to detach\n", ioasid);
 		goto done_unlock;
 	}
+	ioasid_notify(data, IOASID_NOTIFY_UNBIND, IOASID_NOTIFY_FLAG_SET);
 	data->spid = INVALID_IOASID;
-
 done_unlock:
 	spin_unlock(&ioasid_allocator_lock);
 }
@@ -603,6 +656,8 @@ struct ioasid_set *ioasid_set_alloc(void *token, ioasid_t quota, int type)
 	set->quota = quota;
 	set->id = id;
 	atomic_set(&set->nr_ioasids, 0);
+	ATOMIC_INIT_NOTIFIER_HEAD(&set->nh);
+
 	/*
 	 * Per set XA is used to store private IDs within the set, get ready
 	 * for ioasid_set private ID and system-wide IOASID allocation
@@ -655,7 +710,9 @@ int ioasid_set_free(struct ioasid_set *set)
 	int ret = 0;
 
 	spin_lock(&ioasid_allocator_lock);
+	spin_lock(&ioasid_nb_lock);
 	ret = ioasid_set_free_locked(set);
+	spin_unlock(&ioasid_nb_lock);
 	spin_unlock(&ioasid_allocator_lock);
 	return ret;
 }
@@ -728,6 +785,7 @@ ioasid_t ioasid_alloc(struct ioasid_set *set, ioasid_t min, ioasid_t max,
 		goto exit_free;
 	}
 	atomic_inc(&set->nr_ioasids);
+	ioasid_notify(data, IOASID_NOTIFY_ALLOC, IOASID_NOTIFY_FLAG_SET);
 	goto done_unlock;
 exit_free:
 	kfree(data);
@@ -780,9 +838,11 @@ static void ioasid_free_locked(struct ioasid_set *set, ioasid_t ioasid)
 	 * If the refcount is 1, it means there is no other users of the IOASID
 	 * other than IOASID core itself. There is no need to notify anyone.
 	 */
-	if (!refcount_dec_and_test(&data->refs))
+	if (!refcount_dec_and_test(&data->refs)) {
+		ioasid_notify(data, IOASID_NOTIFY_FREE,
+			IOASID_NOTIFY_FLAG_SET | IOASID_NOTIFY_FLAG_ALL);
 		return;
-
+	}
 	ioasid_do_free_locked(data);
 }
 
@@ -833,15 +893,39 @@ void ioasid_free_all_in_set(struct ioasid_set *set)
 	if (!atomic_read(&set->nr_ioasids))
 		return;
 	spin_lock(&ioasid_allocator_lock);
+	spin_lock(&ioasid_nb_lock);
 	xa_for_each(&set->xa, index, entry) {
 		ioasid_free_locked(set, index);
 		/* Free from per set private pool */
 		xa_erase(&set->xa, index);
 	}
+	spin_unlock(&ioasid_nb_lock);
 	spin_unlock(&ioasid_allocator_lock);
 }
 EXPORT_SYMBOL_GPL(ioasid_free_all_in_set);
 
+/*
+ * ioasid_find_mm_set - Retrieve IOASID set with mm token
+ * Take a reference of the set if found.
+ */
+struct ioasid_set *ioasid_find_mm_set(struct mm_struct *token)
+{
+	struct ioasid_set *set;
+	unsigned long index;
+
+	spin_lock(&ioasid_allocator_lock);
+
+	xa_for_each(&ioasid_sets, index, set) {
+		if (set->type == IOASID_SET_TYPE_MM && set->token == token)
+			goto exit_unlock;
+	}
+	set = NULL;
+exit_unlock:
+	spin_unlock(&ioasid_allocator_lock);
+	return set;
+}
+EXPORT_SYMBOL_GPL(ioasid_find_mm_set);
+
 /**
  * ioasid_set_for_each_ioasid
  * @brief
@@ -1021,6 +1105,25 @@ void *ioasid_find(struct ioasid_set *set, ioasid_t ioasid,
 }
 EXPORT_SYMBOL_GPL(ioasid_find);
 
+int ioasid_register_notifier(struct ioasid_set *set, struct notifier_block *nb)
+{
+	if (set)
+		return atomic_notifier_chain_register(&set->nh, nb);
+	else
+		return atomic_notifier_chain_register(&ioasid_notifier, nb);
+}
+EXPORT_SYMBOL_GPL(ioasid_register_notifier);
+
+void ioasid_unregister_notifier(struct ioasid_set *set,
+				struct notifier_block *nb)
+{
+	if (set)
+		atomic_notifier_chain_unregister(&set->nh, nb);
+	else
+		atomic_notifier_chain_unregister(&ioasid_notifier, nb);
+}
+EXPORT_SYMBOL_GPL(ioasid_unregister_notifier);
+
 MODULE_AUTHOR("Jean-Philippe Brucker <[email protected]>");
 MODULE_AUTHOR("Jacob Pan <jacob.jun.pan-VuQAYsv1563Yd54FQh9/[email protected]>");
 MODULE_DESCRIPTION("IO Address Space ID (IOASID) allocator");
include/linux/ioasid.h +54 -0

72 lines not on this page · raw message

Signature · 1 line
2.25.1
author
#244 lore raw

PATCH v4 10/18 iommu/ioasid: Support mm token type ioasid_set notifications

In reply to Jacob Pan

As a system-wide resource, IOASID is often shared by multiple kernel subsystems that are independent of each other. However, at the ioasid_set level, these kernel subsystems must communicate with each other for ownership checking, event notifications, etc. For example, on Intel Scalable IO Virtualization (SIOV) enabled platforms, KVM and VFIO instances under the same process/guest must be aware of a shared IOASID set.
IOASID_SET_TYPE_MM token type was introduced to explicitly mark an IOASID set that belongs to a process, thus use the same mm_struct pointer as a token. Users of the same process can then identify with each other based on this token.

This patch introduces MM token specific event registration APIs. Event subscribers such as KVM instances can register IOASID event handler without the knowledge of its ioasid_set. Event handlers are registered based on its mm_struct pointer as a token. In case when subscribers register handler *prior* to the creation of the ioasid_set, the handler’s notification block is stored in a pending list within IOASID core. Once the ioasid_set of the MM token is created, the notification block will be registered by the IOASID core.

---
 drivers/iommu/ioasid.c | 142 +++++++++++++++++++++++++++++++++++++++++
 include/linux/ioasid.h |  18 ++++++
 2 files changed, 160 insertions(+)
drivers/iommu/ioasid.c +142 -0
index 56577e745c4b..96e941dfada7 100644
@@ -21,6 +21,8 @@
  * keep local states in sync.
  */
 static ATOMIC_NOTIFIER_HEAD(ioasid_notifier);
+/* List to hold pending notification block registrations */
+static LIST_HEAD(ioasid_nb_pending_list);
 static DEFINE_SPINLOCK(ioasid_nb_lock);
 
 /* Default to PCIe standard 20 bit PASID */
@@ -574,6 +576,27 @@ static inline bool ioasid_set_is_valid(struct ioasid_set *set)
 	return xa_load(&ioasid_sets, set->id) == set;
 }
 
+static void ioasid_add_pending_nb(struct ioasid_set *set)
+{
+	struct ioasid_set_nb *curr;
+
+	if (set->type != IOASID_SET_TYPE_MM)
+		return;
+	/*
+	 * Check if there are any pending nb requests for the given token, if so
+	 * add them to the notifier chain.
+	 */
+	spin_lock(&ioasid_nb_lock);
+	list_for_each_entry(curr, &ioasid_nb_pending_list, list) {
+		if (curr->token == set->token && !curr->active) {
+			atomic_notifier_chain_register(&set->nh, curr->nb);
+			curr->set = set;
+			curr->active = true;
+		}
+	}
+	spin_unlock(&ioasid_nb_lock);
+}
+
 /**
  * ioasid_set_alloc - Allocate a new IOASID set for a given token
  *
@@ -658,6 +681,11 @@ struct ioasid_set *ioasid_set_alloc(void *token, ioasid_t quota, int type)
 	atomic_set(&set->nr_ioasids, 0);
 	ATOMIC_INIT_NOTIFIER_HEAD(&set->nh);
 
+	/*
+	 * Check if there are any pending nb requests for the given token, if so
+	 * add them to the notifier chain.
+	 */
+	ioasid_add_pending_nb(set);
 	/*
 	 * Per set XA is used to store private IDs within the set, get ready
 	 * for ioasid_set private ID and system-wide IOASID allocation
@@ -675,6 +703,7 @@ EXPORT_SYMBOL_GPL(ioasid_set_alloc);
 
 static int ioasid_set_free_locked(struct ioasid_set *set)
 {
+	struct ioasid_set_nb *curr;
 	int ret = 0;
 
 	if (!ioasid_set_is_valid(set)) {
@@ -688,6 +717,16 @@ static int ioasid_set_free_locked(struct ioasid_set *set)
 	}
 
 	WARN_ON(!xa_empty(&set->xa));
+	/* Restore pending status of the set NBs */
+	list_for_each_entry(curr, &ioasid_nb_pending_list, list) {
+		if (curr->token == set->token) {
+			if (curr->active)
+				curr->active = false;
+			else
+				pr_warn("Set token exists but not active!\n");
+		}
+	}
+
 	/*
 	 * Token got released right away after the ioasid_set is freed.
 	 * If a new set is created immediately with the newly released token,
@@ -1117,6 +1156,22 @@ EXPORT_SYMBOL_GPL(ioasid_register_notifier);
 void ioasid_unregister_notifier(struct ioasid_set *set,
 				struct notifier_block *nb)
 {
+	struct ioasid_set_nb *curr;
+
+	spin_lock(&ioasid_nb_lock);
+	/*
+	 * Pending list is registered with a token without an ioasid_set,
+	 * therefore should not be unregistered directly.
+	 */
+	list_for_each_entry(curr, &ioasid_nb_pending_list, list) {
+		if (curr->nb == nb) {
+			pr_warn("Cannot unregister NB from pending list\n");
+			spin_unlock(&ioasid_nb_lock);
+			return;
+		}
+	}
+	spin_unlock(&ioasid_nb_lock);
+
 	if (set)
 		atomic_notifier_chain_unregister(&set->nh, nb);
 	else
@@ -1124,6 +1179,93 @@ void ioasid_unregister_notifier(struct ioasid_set *set,
 }
 EXPORT_SYMBOL_GPL(ioasid_unregister_notifier);
 
+/**
+ * ioasid_register_notifier_mm - Register a notifier block on the IOASID set
+ *                               created by the mm_struct pointer as the token
+ *
+ * @mm: the mm_struct token of the ioasid_set
+ * @nb: notfier block to be registered on the ioasid_set
+ *
+ * This a variant of ioasid_register_notifier() where the caller intends to
+ * listen to IOASID events belong the ioasid_set created under the same
+ * process. Caller is not aware of the ioasid_set, no need to hold reference
+ * of the ioasid_set.
+ */
+int ioasid_register_notifier_mm(struct mm_struct *mm, struct notifier_block *nb)
+{
+	struct ioasid_set_nb *curr;
+	struct ioasid_set *set;
+	int ret = 0;
+
+	spin_lock(&ioasid_nb_lock);
+	/* Check for duplicates, nb is unique per set */
+	list_for_each_entry(curr, &ioasid_nb_pending_list, list) {
+		if (curr->token == mm && curr->nb == nb) {
+			ret = -EBUSY;
+			goto exit_unlock;
+		}
+	}
+	curr = kzalloc(sizeof(*curr), GFP_ATOMIC);
+	if (!curr) {
+		ret = -ENOMEM;
+		goto exit_unlock;
+	}
+	/* Check if the token has an existing set */
+	set = ioasid_find_mm_set(mm);
+	if (!set) {
+		/* Add to the rsvd list as inactive */
+		curr->active = false;
+	} else {
+		/* REVISIT: Only register empty set for now. Can add an option
+		 * in the future to playback existing PASIDs.
+		 */
+		if (atomic_read(&set->nr_ioasids)) {
+			pr_warn("IOASID set %d not empty %d\n", set->id,
+				atomic_read(&set->nr_ioasids));
+			ret = -EBUSY;
+			goto exit_free;
+		}
+		curr->token = mm;
+		curr->nb = nb;
+		curr->active = true;
+		curr->set = set;
+
+		/* Set already created, add to the notifier chain */
+		atomic_notifier_chain_register(&set->nh, nb);
+	}
+
+	list_add(&curr->list, &ioasid_nb_pending_list);
+	goto exit_unlock;
+exit_free:
+	kfree(curr);
+exit_unlock:
+	spin_unlock(&ioasid_nb_lock);
+	return ret;
+}
+EXPORT_SYMBOL_GPL(ioasid_register_notifier_mm);
+
+void ioasid_unregister_notifier_mm(struct mm_struct *mm, struct notifier_block *nb)
+{
+	struct ioasid_set_nb *curr;
+
+	spin_lock(&ioasid_nb_lock);
+	list_for_each_entry(curr, &ioasid_nb_pending_list, list) {
+		if (curr->token == mm && curr->nb == nb) {
+			list_del(&curr->list);
+			spin_unlock(&ioasid_nb_lock);
+			if (curr->active) {
+				atomic_notifier_chain_unregister(&curr->set->nh,
+								 nb);
+			}
+			kfree(curr);
+			return;
+		}
+	}
+	pr_warn("No ioasid set found for mm token %llx\n",  (u64)mm);
+	spin_unlock(&ioasid_nb_lock);
+}
+EXPORT_SYMBOL_GPL(ioasid_unregister_notifier_mm);
+
 MODULE_AUTHOR("Jean-Philippe Brucker <[email protected]>");
 MODULE_AUTHOR("Jacob Pan <[email protected]>");
 MODULE_DESCRIPTION("IO Address Space ID (IOASID) allocator");
include/linux/ioasid.h +18 -0
index d8b85a04214f..c97e80ff65cc 100644
@@ -132,6 +132,8 @@ void ioasid_unregister_notifier(struct ioasid_set *set,
 void ioasid_set_for_each_ioasid(struct ioasid_set *sdata,
 				void (*fn)(ioasid_t id, void *data),
 				void *data);
+int ioasid_register_notifier_mm(struct mm_struct *mm, struct notifier_block *nb);
+void ioasid_unregister_notifier_mm(struct mm_struct *mm, struct notifier_block *nb);
 #else /* !CONFIG_IOASID */
 static inline void ioasid_install_capacity(ioasid_t total)
 {
@@ -250,5 +252,21 @@ static inline void ioasid_set_for_each_ioasid(struct ioasid_set *sdata,
 					      void *data)
 {
 }
+
+static inline int ioasid_register_notifier_mm(struct mm_struct *mm,
+					      struct notifier_block *nb)
+{
+	return -ENOTSUPP;
+}
+
+static inline void ioasid_unregister_notifier_mm(struct mm_struct *mm,
+						 struct notifier_block *nb)
+{
+}
+
+static inline bool ioasid_queue_work(struct work_struct *work)
+{
+	return false;
+}
 #endif /* CONFIG_IOASID */
 #endif /* __LINUX_IOASID_H */
Signature · 6 lines
2.25.1

_______________________________________________
iommu mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/iommu
author
#245 lore raw

PATCH v4 11/18 iommu/ioasid: Add ownership check in guest bind

In reply to Jacob Pan

Bind guest page table call comes with an IOASID provided by the userspace. To prevent attacks by malicious users, we must ensure the IOASID was allocated under the same process.

This patch adds a new API that will perform an ownership check that is based on whether the IOASID belongs to the ioasid_set allocated with the mm_struct pointer as a token.

---
 drivers/iommu/ioasid.c | 37 +++++++++++++++++++++++++++++++++++++
 drivers/iommu/iommu.c  | 16 ++++++++++++++--
 include/linux/ioasid.h |  6 ++++++
 3 files changed, 57 insertions(+), 2 deletions(-)
drivers/iommu/ioasid.c +37 -0
index 96e941dfada7..28a2e9b6594d 100644
@@ -9,6 +9,7 @@
 #include <linux/slab.h>
 #include <linux/spinlock.h>
 #include <linux/xarray.h>
+#include <linux/sched/mm.h>
 
 /*
  * An IOASID can have multiple consumers where each consumer may have
@@ -1028,6 +1029,42 @@ int ioasid_get(struct ioasid_set *set, ioasid_t ioasid)
 }
 EXPORT_SYMBOL_GPL(ioasid_get);
 
+/**
+ * ioasid_get_if_owned - obtain a reference to the IOASID if the IOASID belongs
+ * 		to the ioasid_set with the current mm as token
+ * @ioasid:	the IOASID to get reference
+ *
+ *
+ * Return: 0 on success, error if failed.
+ */
+int ioasid_get_if_owned(ioasid_t ioasid)
+{
+	struct ioasid_set *set;
+	int ret;
+
+	spin_lock(&ioasid_allocator_lock);
+	set = ioasid_find_set(ioasid);
+	if (IS_ERR_OR_NULL(set)) {
+		ret = -ENOENT;
+		goto done_unlock;
+	}
+	if (set->type != IOASID_SET_TYPE_MM) {
+		ret = -EINVAL;
+		goto done_unlock;
+	}
+	if (current->mm != set->token) {
+		ret = -EPERM;
+		goto done_unlock;
+	}
+
+	ret = ioasid_get_locked(set, ioasid);
+done_unlock:
+	spin_unlock(&ioasid_allocator_lock);
+
+	return ret;
+}
+EXPORT_SYMBOL_GPL(ioasid_get_if_owned);
+
 bool ioasid_put_locked(struct ioasid_set *set, ioasid_t ioasid)
 {
 	struct ioasid_data *data;
drivers/iommu/iommu.c +14 -2
index fd76e2f579fe..18716d856b02 100644
@@ -2169,7 +2169,13 @@ int iommu_uapi_sva_bind_gpasid(struct iommu_domain *domain, struct device *dev,
 	if (ret)
 		return ret;
 
-	return domain->ops->sva_bind_gpasid(domain, dev, &data);
+	ret = ioasid_get_if_owned(data.hpasid);
+	if (ret)
+		return ret;
+	ret = domain->ops->sva_bind_gpasid(domain, dev, &data);
+	ioasid_put(NULL, data.hpasid);
+
+	return ret;
 }
 EXPORT_SYMBOL_GPL(iommu_uapi_sva_bind_gpasid);
 
@@ -2196,7 +2202,13 @@ int iommu_uapi_sva_unbind_gpasid(struct iommu_domain *domain, struct device *dev
 	if (ret)
 		return ret;
 
-	return iommu_sva_unbind_gpasid(domain, dev, data.hpasid);
+	ret = ioasid_get_if_owned(data.hpasid);
+	if (ret)
+		return ret;
+	ret = iommu_sva_unbind_gpasid(domain, dev, data.hpasid);
+	ioasid_put(NULL, data.hpasid);
+
+	return ret;
 }
 EXPORT_SYMBOL_GPL(iommu_uapi_sva_unbind_gpasid);
 
include/linux/ioasid.h +6 -0
index c97e80ff65cc..9624b665f810 100644
@@ -111,6 +111,7 @@ ioasid_t ioasid_alloc(struct ioasid_set *set, ioasid_t min, ioasid_t max,
 		      void *private);
 int ioasid_get(struct ioasid_set *set, ioasid_t ioasid);
 int ioasid_get_locked(struct ioasid_set *set, ioasid_t ioasid);
+int ioasid_get_if_owned(ioasid_t ioasid);
 bool ioasid_put(struct ioasid_set *set, ioasid_t ioasid);
 bool ioasid_put_locked(struct ioasid_set *set, ioasid_t ioasid);
 void ioasid_free(struct ioasid_set *set, ioasid_t ioasid);
@@ -180,6 +181,11 @@ static inline int ioasid_get_locked(struct ioasid_set *set, ioasid_t ioasid)
 	return -ENOTSUPP;
 }
 
+static inline int ioasid_get_if_owned(ioasid_t ioasid)
+{
+	return -ENOTSUPP;
+}
+
 static inline bool ioasid_put(struct ioasid_set *set, ioasid_t ioasid)
 {
 	return false;
Signature · 1 line
2.25.1
author
#246 lore raw

PATCH v4 12/18 iommu/vt-d: Remove mm reference for guest SVA

In reply to Jacob Pan

Now that IOASID core keeps track of the IOASID to mm_struct ownership in the forms of ioasid_set with IOASID_SET_TYPE_MM token type, there is no need to keep the same mapping in VT-d driver specific data. Native SVM usage is not affected by the change.

---
 drivers/iommu/intel/svm.c | 7 -------
 1 file changed, 7 deletions(-)
drivers/iommu/intel/svm.c +0 -7
index c469c24d23f5..f75699ddb923 100644
@@ -363,12 +363,6 @@ int intel_svm_bind_gpasid(struct iommu_domain *domain, struct device *dev,
 			ret = -ENOMEM;
 			goto out;
 		}
-		/* REVISIT: upper layer/VFIO can track host process that bind
-		 * the PASID. ioasid_set = mm might be sufficient for vfio to
-		 * check pasid VMM ownership. We can drop the following line
-		 * once VFIO and IOASID set check is in place.
-		 */
-		svm->mm = get_task_mm(current);
 		svm->pasid = data->hpasid;
 		if (data->flags & IOMMU_SVA_GPASID_VAL) {
 			svm->gpasid = data->gpasid;
@@ -376,7 +370,6 @@ int intel_svm_bind_gpasid(struct iommu_domain *domain, struct device *dev,
 		}
 		ioasid_attach_data(data->hpasid, svm);
 		INIT_LIST_HEAD_RCU(&svm->devs);
-		mmput(svm->mm);
 	}
 	sdev = kzalloc(sizeof(*sdev), GFP_KERNEL);
 	if (!sdev) {
Signature · 1 line
2.25.1
author
#247 lore raw

PATCH v4 13/18 iommu/ioasid: Add a workqueue for cleanup work

In reply to Jacob Pan

An IOASID can have multiple users, such as IOMMU driver, KVM, and device
drivers. The atomic IOASID notifier is used to inform users of IOASID
state change. For example, the IOASID_NOTIFY_UNBIND event is issued when the IOASID is no longer bound to an address space. This requires ordered actions among users to tear down their contexts.

Not all work can be handled in the atomic notifier handler. This patch introduces a shared, ordered workqueue for all IOASID users who wish to perform work asynchronously upon notification.

---
 drivers/iommu/ioasid.c | 25 +++++++++++++++++++++++++
 include/linux/ioasid.h |  1 +
 2 files changed, 26 insertions(+)
drivers/iommu/ioasid.c +25 -0
index 28a2e9b6594d..d42b39ca2c8b 100644
@@ -32,6 +32,9 @@ static ioasid_t ioasid_capacity = PCI_PASID_MAX;
 static ioasid_t ioasid_capacity_avail = PCI_PASID_MAX;
 static DEFINE_XARRAY_ALLOC(ioasid_sets);
 
+/* Workqueue for IOASID users to do cleanup upon notification */
+static struct workqueue_struct *ioasid_wq;
+
 struct ioasid_set_nb {
 	struct list_head	list;
 	struct notifier_block	*nb;
@@ -1281,6 +1284,12 @@ int ioasid_register_notifier_mm(struct mm_struct *mm, struct notifier_block *nb)
 }
 EXPORT_SYMBOL_GPL(ioasid_register_notifier_mm);
 
+bool ioasid_queue_work(struct work_struct *work)
+{
+	return queue_work(ioasid_wq, work);
+}
+EXPORT_SYMBOL_GPL(ioasid_queue_work);
+
 void ioasid_unregister_notifier_mm(struct mm_struct *mm, struct notifier_block *nb)
 {
 	struct ioasid_set_nb *curr;
@@ -1303,7 +1312,23 @@ void ioasid_unregister_notifier_mm(struct mm_struct *mm, struct notifier_block *
 }
 EXPORT_SYMBOL_GPL(ioasid_unregister_notifier_mm);
 
+static int __init ioasid_init(void)
+{
+	ioasid_wq = alloc_ordered_workqueue("ioasid_wq", 0);
+	if (!ioasid_wq)
+		return -ENOMEM;
+
+	return 0;
+}
+
+static void __exit ioasid_cleanup(void)
+{
+	destroy_workqueue(ioasid_wq);
+}
+
 MODULE_AUTHOR("Jean-Philippe Brucker <[email protected]>");
 MODULE_AUTHOR("Jacob Pan <jacob.jun.pan-VuQAYsv1563Yd54FQh9/[email protected]>");
 MODULE_DESCRIPTION("IO Address Space ID (IOASID) allocator");
 MODULE_LICENSE("GPL");
+module_init(ioasid_init);
+module_exit(ioasid_cleanup);
include/linux/ioasid.h +1 -0
index 9624b665f810..4547086797df 100644
@@ -135,6 +135,7 @@ void ioasid_set_for_each_ioasid(struct ioasid_set *sdata,
 				void *data);
 int ioasid_register_notifier_mm(struct mm_struct *mm, struct notifier_block *nb);
 void ioasid_unregister_notifier_mm(struct mm_struct *mm, struct notifier_block *nb);
+bool ioasid_queue_work(struct work_struct *work);
 #else /* !CONFIG_IOASID */
 static inline void ioasid_install_capacity(ioasid_t total)
 {
Signature · 1 line
2.25.1
author
#248 lore raw

PATCH v4 14/18 iommu/vt-d: Listen to IOASID notifications

In reply to Jacob Pan

On Intel Scalable I/O Virtualization (SIOV) enabled platforms, IOMMU driver is one of the users of IOASIDs. In normal flow, callers will perform IOASID allocation, bind, unbind, and free in order. However, for guest SVA, IOASID free could come before unbind as guest is untrusted. This patch registers IOASID notification handler such that IOMMU driver can perform PASID teardown upon receiving an unexpected IOASID free event.

---
 drivers/iommu/intel/iommu.c |   2 +
 drivers/iommu/intel/svm.c   | 109 +++++++++++++++++++++++++++++++++++-
 include/linux/intel-iommu.h |   2 +
 3 files changed, 111 insertions(+), 2 deletions(-)
drivers/iommu/intel/iommu.c +2 -0
index eb9868061545..d602e89c40d2 100644
@@ -3313,6 +3313,8 @@ static int __init init_dmars(void)
 			pr_err("Failed to allocate host PASID set %lu\n",
 				PTR_ERR(host_pasid_set));
 			intel_iommu_sm = 0;
+		} else {
+			intel_svm_add_pasid_notifier();
 		}
 	}
 
drivers/iommu/intel/svm.c +107 -2

135 lines not on this page · raw message

include/linux/intel-iommu.h +2 -0
index 09c6a0bf3892..b1b8914e1564 100644
@@ -757,6 +757,7 @@ void intel_svm_unbind(struct iommu_sva *handle);
 u32 intel_svm_get_pasid(struct iommu_sva *handle);
 int intel_svm_page_response(struct device *dev, struct iommu_fault_event *evt,
 			    struct iommu_page_response *msg);
+void intel_svm_add_pasid_notifier(void);
 
 struct svm_dev_ops;
 
@@ -783,6 +784,7 @@ struct intel_svm {
 	int gpasid; /* In case that guest PASID is different from host PASID */
 	struct list_head devs;
 	struct list_head list;
+	struct work_struct work; /* For deferred clean up */
 };
 #else
 static inline void intel_svm_check(struct intel_iommu *iommu) {}
Signature · 1 line
2.25.1
author
#249 lore raw

RFC PATCH v4 15/18 cgroup: Introduce ioasids controller

In reply to Jacob Pan

IOASIDs are used to associate DMA requests with virtual address spaces. They are a system-wide limited resource made available to the userspace applications. Let it be VMs or user-space device drivers.

This RFC patch introduces a cgroup controller to address the following problems:
1. Some user applications exhaust all the available IOASIDs thus depriving others of the same host.
2. System admins need to provision VMs based on their needs for IOASIDs, e.g. the number of VMs with assigned devices that perform DMA requests with PASID.

This patch is nowhere near its completion, it merely provides the basic functionality for resource distribution and cgroup hierarchy organizational changes.

Since this is part of a greater effort to enable Shared Virtual Address (SVA) virtualization. We would like to have a direction check and collect feedback early. For details, please refer to the documentation:
Documentation/admin-guide/cgroup-v1/ioasids.rst

---
 include/linux/cgroup_subsys.h |   4 +
 include/linux/ioasid.h        |  17 ++
 init/Kconfig                  |   7 +
 kernel/cgroup/Makefile        |   1 +
 kernel/cgroup/ioasids.c       | 345 ++++++++++++++++++++++++++++++++++
 5 files changed, 374 insertions(+)
 create mode 100644 kernel/cgroup/ioasids.c
include/linux/cgroup_subsys.h +4 -0
index acb77dcff3b4..cda75ecdcdcb 100644
@@ -57,6 +57,10 @@ SUBSYS(hugetlb)
 SUBSYS(pids)
 #endif
 
+#if IS_ENABLED(CONFIG_CGROUP_IOASIDS)
+SUBSYS(ioasids)
+#endif
+
 #if IS_ENABLED(CONFIG_CGROUP_RDMA)
 SUBSYS(rdma)
 #endif
include/linux/ioasid.h +17 -0
index 4547086797df..5ea4710efb02 100644
@@ -135,8 +135,25 @@ void ioasid_set_for_each_ioasid(struct ioasid_set *sdata,
 				void *data);
 int ioasid_register_notifier_mm(struct mm_struct *mm, struct notifier_block *nb);
 void ioasid_unregister_notifier_mm(struct mm_struct *mm, struct notifier_block *nb);
+#ifdef CONFIG_CGROUP_IOASIDS
+int ioasid_cg_charge(struct ioasid_set *set);
+void ioasid_cg_uncharge(struct ioasid_set *set);
+#else
+/* No cgroup control, allocation will proceed until run out total pool */
+static inline int ioasid_cg_charge(struct ioasid_set *set)
+{
+	return 0;
+}
+
+static inline int ioasid_cg_uncharge(struct ioasid_set *set)
+{
+	return 0;
+}
+#endif /* CGROUP_IOASIDS */
 bool ioasid_queue_work(struct work_struct *work);
+
 #else /* !CONFIG_IOASID */
+
 static inline void ioasid_install_capacity(ioasid_t total)
 {
 }
init/Kconfig +7 -0
index b77c60f8b963..9a23683dad98 100644
@@ -1017,6 +1017,13 @@ config CGROUP_PIDS
 	  since the PIDs limit only affects a process's ability to fork, not to
 	  attach to a cgroup.
 
+config CGROUP_IOASIDS
+	bool "IOASIDs controller"
+	depends on IOASID
+	help
+	  Provides enforcement of IO Address Space ID limits in the scope of a
+	  cgroup.
+
 config CGROUP_RDMA
 	bool "RDMA controller"
 	help
kernel/cgroup/Makefile +1 -0
index 5d7a76bfbbb7..c5ad7c9a2305 100644
@@ -3,6 +3,7 @@ obj-y := cgroup.o rstat.o namespace.o cgroup-v1.o freezer.o
 
 obj-$(CONFIG_CGROUP_FREEZER) += legacy_freezer.o
 obj-$(CONFIG_CGROUP_PIDS) += pids.o
+obj-$(CONFIG_CGROUP_IOASIDS) += ioasids.o
 obj-$(CONFIG_CGROUP_RDMA) += rdma.o
 obj-$(CONFIG_CPUSETS) += cpuset.o
 obj-$(CONFIG_CGROUP_DEBUG) += debug.o
kernel/cgroup/ioasids.c new file +345 -0

345 lines not on this page · raw message

Signature · 1 line
2.25.1
author
#263 lore raw

RFC PATCH v4 16/18 iommu/ioasid: Consult IOASIDs cgroup for allocation

In reply to Jacob Pan

Once IOASIDs cgroup is active, we must consult the limitation set up by the cgroups during allocation. Freeing IOASIDs also need to return the quota back to the cgroup.

---
 drivers/iommu/ioasid.c | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)
drivers/iommu/ioasid.c +6 -1
index d42b39ca2c8b..fd3f5729c71d 100644
@@ -782,7 +782,10 @@ ioasid_t ioasid_alloc(struct ioasid_set *set, ioasid_t min, ioasid_t max,
 
 	spin_lock(&ioasid_allocator_lock);
 	/* Check if the IOASID set has been allocated and initialized */
-	if (!ioasid_set_is_valid(set))
+	if (!set || !ioasid_set_is_valid(set))
+		goto done_unlock;
+
+	if (set->type == IOASID_SET_TYPE_MM && ioasid_cg_charge(set))
 		goto done_unlock;
 
 	if (set->quota <= atomic_read(&set->nr_ioasids)) {
@@ -832,6 +835,7 @@ ioasid_t ioasid_alloc(struct ioasid_set *set, ioasid_t min, ioasid_t max,
 	goto done_unlock;
 exit_free:
 	kfree(data);
+	ioasid_cg_uncharge(set);
 done_unlock:
 	spin_unlock(&ioasid_allocator_lock);
 	return id;
@@ -849,6 +853,7 @@ static void ioasid_do_free_locked(struct ioasid_data *data)
 		kfree_rcu(ioasid_data, rcu);
 	}
 	atomic_dec(&data->set->nr_ioasids);
+	ioasid_cg_uncharge(data->set);
 	xa_erase(&data->set->xa, data->id);
 	/* Destroy the set if empty */
 	if (!atomic_read(&data->set->nr_ioasids))
Signature · 1 line
2.25.1
author
#264 lore raw

RFC PATCH v4 17/18 docs: cgroup-v1: Add IOASIDs controller

In reply to Jacob Pan
---
 Documentation/admin-guide/cgroup-v1/index.rst |   1 +
 .../admin-guide/cgroup-v1/ioasids.rst         | 110 ++++++++++++++++++
 2 files changed, 111 insertions(+)
 create mode 100644 Documentation/admin-guide/cgroup-v1/ioasids.rst
Documentation/admin-guide/cgroup-v1/index.rst +1 -0
index 226f64473e8e..f5e307dc4dbb 100644
@@ -15,6 +15,7 @@ Control Groups version 1
     devices
     freezer-subsystem
     hugetlb
+    ioasids
     memcg_test
     memory
     net_cls
Documentation/admin-guide/cgroup-v1/ioasids.rst new file +110 -0

110 lines not on this page · raw message

Signature · 6 lines
2.25.1

_______________________________________________
iommu mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/iommu
author
#265 lore raw

RFC PATCH v4 18/18 ioasid: Add /dev/ioasid for userspace

In reply to Jacob Pan

From: Liu Yi L <[email protected]>

I/O Address Space IDs (IOASIDs) is used to tag DMA requests to target multiple DMA address spaces for physical devices. Its PCI terminology is called PASID (Process Address Space ID). Platforms with PASID support can provide PASID granularity DMA isolation, which is very useful for efficient and secure device sharing (SVA, subdevice passthrough, etc.).

Today only kernel drivers are allowed to allocate IOASIDs [1]. This patch aims to extend this capability to userspace as required in device pass- through scenarios. For example, a userspace driver may want to create its own DMA address spaces besides the default IOVA address space established by the kernel on the assigned device (e.g. vDPA control vq [2] and guest SVA [3]), thus need to get IOASIDs from the kernel IOASID allocator for tagging. In concept, each device can have its own IOASID space, thus it's also possible for userspace driver to manage a private IOASID space itself, say, when PF/VF is assigned. However it doesn't work for subdevice pass- through, as multiple subdevices under the same parent device share a single IOASID space thus IOASIDs must be centrally managed by the kernel in such case.

This patch introduces a /dev/ioasid interface for this purpose (per discussion in [4]). An IOASID is just a number before it is tagged to a specific DMA address space. The actual IOASID tagging (to DMA requests) and association (with DMA address spaces) operations from userspace are scrutinized by specific device passthrough frameworks, which must ensure that a malicious driver cannot program arbitrary IOASIDs to its assigned device to access DMA address spaces that don't belong to it, this is out of the scope of this patch (a reference VFIO implementation will be posted soon).

Open:

PCIe PASID is 20bit implying a space with 1M IOASIDs. although it's plenty there was an open [4] on whether this user interface is open to all processes or only selective processes (e.g. with device assigned). In this patchseries, a cgroup controller is introduced to manage IOASID quota that a process is allowed to use. A cgroup-enabled system may by default set quota=0 to disallow IOASID allocation for most processes, and then having the virt management stack to adjust the quota for a process which gets device assigned. But yeah, we are also willing to hear more suggestions.

[1] https://lore.kernel.org/linux-iommu/1565900005-62508-8-git-send-email-jacob.jun.pan-VuQAYsv1563Yd54FQh9/[email protected]/
[2] https://lore.kernel.org/kvm/20201216064818.48239-1-jasowang-H+wXaHxf7aLQT0dZR+AlfA@public.gmane.org/
[3] https://lore.kernel.org/linux-iommu/1599734733-6431-1-git-send-email-yi.l.liu-ral2JQCrhuEAvxtiuMwx3w@public.gmane.org/
[4] https://lore.kernel.org/kvm/[email protected]/

---
 Documentation/userspace-api/index.rst  |   1 +
 Documentation/userspace-api/ioasid.rst |  49 ++++
 drivers/iommu/Kconfig                  |   5 +
 drivers/iommu/Makefile                 |   1 +
 drivers/iommu/intel/Kconfig            |   1 +
 drivers/iommu/ioasid_user.c            | 297 +++++++++++++++++++++++++
 include/linux/ioasid.h                 |  26 +++
 include/linux/miscdevice.h             |   1 +
 include/uapi/linux/ioasid.h            |  98 ++++++++
 9 files changed, 479 insertions(+)
 create mode 100644 Documentation/userspace-api/ioasid.rst
 create mode 100644 drivers/iommu/ioasid_user.c
 create mode 100644 include/uapi/linux/ioasid.h
Documentation/userspace-api/index.rst +1 -0
index acd2cc2a538d..69e1be7c67ee 100644
@@ -24,6 +24,7 @@ place where this information is gathered.
    ioctl/index
    iommu
    media/index
+   ioasid
 
 .. only::  subproject and html
 
Documentation/userspace-api/ioasid.rst new file +49 -0

49 lines not on this page · raw message

drivers/iommu/Kconfig +5 -0
index 192ef8f61310..830f4ec28a16 100644
@@ -7,6 +7,11 @@ config IOMMU_IOVA
 config IOASID
 	tristate
 
+config IOASID_USER
+	tristate
+	depends on IOASID
+	default n
+
 # IOMMU_API always gets selected by whoever wants it.
 config IOMMU_API
 	bool
drivers/iommu/Makefile +1 -0
index 61bd30cd8369..305dd019ff49 100644
@@ -9,6 +9,7 @@ obj-$(CONFIG_IOMMU_IO_PGTABLE) += io-pgtable.o
 obj-$(CONFIG_IOMMU_IO_PGTABLE_ARMV7S) += io-pgtable-arm-v7s.o
 obj-$(CONFIG_IOMMU_IO_PGTABLE_LPAE) += io-pgtable-arm.o
 obj-$(CONFIG_IOASID) += ioasid.o
+obj-$(CONFIG_IOASID_USER) += ioasid_user.o
 obj-$(CONFIG_IOMMU_IOVA) += iova.o
 obj-$(CONFIG_OF_IOMMU)	+= of_iommu.o
 obj-$(CONFIG_MSM_IOMMU) += msm_iommu.o
drivers/iommu/intel/Kconfig +1 -0
index 28a3d1596c76..a6d9dea61d58 100644
@@ -13,6 +13,7 @@ config INTEL_IOMMU
 	select DMAR_TABLE
 	select SWIOTLB
 	select IOASID
+	select IOASID_USER
 	select IOMMU_DMA
 	help
 	  DMA remapping (DMAR) devices support enables independent address
drivers/iommu/ioasid_user.c new file +297 -0

297 lines not on this page · raw message

include/linux/ioasid.h +26 -0
index 5ea4710efb02..b82abe6325f7 100644
@@ -6,6 +6,7 @@
 #include <linux/errno.h>
 #include <linux/xarray.h>
 #include <linux/refcount.h>
+#include <uapi/linux/ioasid.h>
 
 #define INVALID_IOASID ((ioasid_t)-1)
 typedef unsigned int ioasid_t;
@@ -152,6 +153,31 @@ static inline int ioasid_cg_uncharge(struct ioasid_set *set)
 #endif /* CGROUP_IOASIDS */
 bool ioasid_queue_work(struct work_struct *work);
 
+/* IOASID userspace support */
+struct ioasid_user;
+#if IS_ENABLED(CONFIG_IOASID_USER)
+extern struct ioasid_user *ioasid_user_get_from_task(struct task_struct *task);
+extern void ioasid_user_put(struct ioasid_user *iuser);
+extern void ioasid_user_for_each_id(struct ioasid_user *iuser, void *data,
+				   void (*fn)(ioasid_t id, void *data));
+
+#else /* CONFIG_IOASID_USER */
+static inline struct ioasid_user *
+ioasid_user_get_from_task(struct task_struct *task)
+{
+	return ERR_PTR(-ENOTTY);
+}
+
+static inline void ioasid_user_put(struct ioasid_user *iuser)
+{
+}
+
+static inline void ioasid_user_for_each_id(struct ioasid_user *iuser, void *data,
+					  void (*fn)(ioasid_t id, void *data))
+{
+}
+#endif /* CONFIG_IOASID_USER */
+
 #else /* !CONFIG_IOASID */
 
 static inline void ioasid_install_capacity(ioasid_t total)
include/linux/miscdevice.h +1 -0
index 0676f18093f9..9823901f11a4 100644
@@ -21,6 +21,7 @@
 #define APOLLO_MOUSE_MINOR	7	/* unused */
 #define PC110PAD_MINOR		9	/* unused */
 /*#define ADB_MOUSE_MINOR	10	FIXME OBSOLETE */
+#define IOASID_MINOR		129     /* /dev/ioasid     */
 #define WATCHDOG_MINOR		130	/* Watchdog timer     */
 #define TEMP_MINOR		131	/* Temperature Sensor */
 #define APM_MINOR_DEV		134
include/uapi/linux/ioasid.h new file +98 -0

98 lines not on this page · raw message

Signature · 1 line
2.25.1
#269 lore raw
In reply to Jacob Pan
Quoted 53 lines

From: Jacob Pan <jacob.jun.pan-VuQAYsv1563Yd54FQh9/[email protected]>
Sent: Sunday, February 28, 2021 6:01 AM

I/O Address Space ID (IOASID) core code was introduced in v5.5 as a generic kernel allocator service for both PCIe Process Address Space ID (PASID) and ARM SMMU's Substream ID. IOASIDs are used to associate DMA requests with
virtual address spaces, including both host and guest.

In addition to providing basic ID allocation, ioasid_set was defined as a token that is shared by a group of IOASIDs. This set token can be used for permission checking, but lack some features to address the following needs by guest Shared Virtual Address (SVA).
- Manage IOASIDs by group, group ownership, quota, etc.
- State synchronization among IOASID users (e.g. IOMMU driver, KVM, device
drivers)
- Non-identity guest-host IOASID mapping
- Lifecycle management

This patchset introduces the following extensions as solutions to the problems above.
- Redefine and extend IOASID set such that IOASIDs can be managed by groups/pools.
- Add notifications for IOASID state synchronization
- Extend reference counting for life cycle alignment among multiple users
- Support ioasid_set private IDs, which can be used as guest IOASIDs
- Add a new cgroup controller for resource distribution

Please refer to Documentation/admin-guide/cgroup-v1/ioasids.rst and Documentation/driver-api/ioasid.rst in the enclosed patches for more details.

Based on discussions on LKML[1], a direction change was made in v4 such that
the user interfaces for IOASID allocation are extracted from VFIO subsystem. The proposed IOASID subsystem now consists of three components:
1. IOASID core[01-14]: provides APIs for allocation, pool management, notifications, and refcounting.
2. IOASID cgroup controller[RFC 15-17]: manage resource distribution[2].
3. IOASID user[RFC 18]: provides user allocation interface via /dev/ioasid

This patchset only included VT-d driver as users of some of the new APIs. VFIO and KVM patches are coming up to fully utilize the APIs introduced here.

[1] https://lore.kernel.org/linux-iommu/1599734733-6431-1-git-send-email-
[email protected]/
[2] Note that ioasid quota management code can be removed once the IOASIDs
cgroup is ratified.

Quoted 3 lines

You can find this series, VFIO, KVM, and IOASID user at:
https://github.com/jacobpan/linux.git ioasid_v4
(VFIO and KVM patches will be available at this branch when published.)

VFIO and QEMU series are listed below:

VFIO: https://lore.kernel.org/linux-iommu/20210302203545.436623-1-yi.l.liu-ral2JQCrhuEAvxtiuMwx3w@public.gmane.org/
QEMU: https://lore.kernel.org/qemu-devel/20210302203827.437645-1-yi.l.liu-ral2JQCrhuEAvxtiuMwx3w@public.gmane.org/T/#t

Regards,
Yi Liu

maintainer
#250 lore raw

Re: [RFC PATCH 15/18] cgroup: Introduce ioasids controller

In reply to Jacob Pan
Quoted 11 lines · Jacob Pan

IOASIDs are used to associate DMA requests with virtual address spaces. They are a system-wide limited resource made available to the userspace applications. Let it be VMs or user-space device drivers.

This RFC patch introduces a cgroup controller to address the following problems:
1. Some user applications exhaust all the available IOASIDs thus depriving others of the same host.
2. System admins need to provision VMs based on their needs for IOASIDs, e.g. the number of VMs with assigned devices that perform DMA requests with PASID.

Please take a look at the proposed misc controller:

http://lkml.kernel.org/r/[email protected]

Would that fit your bill?

Thanks.

Signature · 1 line
tejun
author
#251 lore raw

Re: [RFC PATCH 15/18] cgroup: Introduce ioasids controller

Hi Tejun,

Quoted 13 lines · Tejun Heo
Quoted 11 lines · Jacob Pan

IOASIDs are used to associate DMA requests with virtual address spaces. They are a system-wide limited resource made available to the userspace applications. Let it be VMs or user-space device drivers.

This RFC patch introduces a cgroup controller to address the following problems:
1. Some user applications exhaust all the available IOASIDs thus depriving others of the same host.
2. System admins need to provision VMs based on their needs for IOASIDs, e.g. the number of VMs with assigned devices that perform DMA requests with PASID.

Quoted 5 lines

Please take a look at the proposed misc controller:

http://lkml.kernel.org/r/[email protected]

Would that fit your bill?

The interface definitely can be reused. But IOASID has a different behavior in terms of migration and ownership checking. I guess SEV key IDs are not tied to a process whereas IOASIDs are. Perhaps this can be solved by adding
+ .can_attach = ioasids_can_attach,
+ .cancel_attach = ioasids_cancel_attach,
Let me give it a try and come back.

Thanks for the pointer.

Jacob

Quoted 3 lines

Thanks.

Thanks,

Jacob
_______________________________________________
iommu mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/iommu

author
#252 lore raw

Re: [RFC PATCH 15/18] cgroup: Introduce ioasids controller

Hi Jacob,

Quoted 28 lines · Jacob Pan

Hi Tejun,

Quoted 13 lines · Tejun Heo
Quoted 11 lines · Jacob Pan

IOASIDs are used to associate DMA requests with virtual address spaces. They are a system-wide limited resource made available to the userspace applications. Let it be VMs or user-space device drivers.

This RFC patch introduces a cgroup controller to address the following problems:
1. Some user applications exhaust all the available IOASIDs thus depriving others of the same host.
2. System admins need to provision VMs based on their needs for IOASIDs, e.g. the number of VMs with assigned devices that perform DMA requests with PASID.

Quoted 5 lines

Please take a look at the proposed misc controller:

http://lkml.kernel.org/r/[email protected]

Would that fit your bill?

The interface definitely can be reused. But IOASID has a different behavior in terms of migration and ownership checking. I guess SEV key IDs are not tied to a process whereas IOASIDs are. Perhaps this can be solved by adding

kernel/cgroup/ioasids.c new file +2 -0
+	.can_attach	= ioasids_can_attach,
+	.cancel_attach	= ioasids_cancel_attach,
Quoted 2 lines

Let me give it a try and come back.

While I am trying to fit the IOASIDs cgroup in to the misc cgroup proposal. I'd like to have a direction check on whether this idea of using cgroup for IOASID/PASID resource management is viable.

Alex/Jason/Jean and everyone, your feedback is much appreciated.

Quoted 5 lines

Thanks for the pointer.

Jacob

Quoted 1 line
Quoted 7 lines
Quoted 2 lines

Thanks.

Thanks,

Jacob

Thanks,

Jacob
_______________________________________________
iommu mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/iommu

#253 lore raw

Re: [RFC PATCH 15/18] cgroup: Introduce ioasids controller

Quoted 12 lines · Jacob Pan
Quoted 8 lines

The interface definitely can be reused. But IOASID has a different behavior in terms of migration and ownership checking. I guess SEV key IDs are not tied to a process whereas IOASIDs are. Perhaps this can be solved by adding

kernel/cgroup/ioasids.c new file +2 -0
+	.can_attach	= ioasids_can_attach,
+	.cancel_attach	= ioasids_cancel_attach,

Let me give it a try and come back.

While I am trying to fit the IOASIDs cgroup in to the misc cgroup proposal. I'd like to have a direction check on whether this idea of using cgroup for IOASID/PASID resource management is viable.

Quoted 1 line

Alex/Jason/Jean and everyone, your feedback is much appreciated.

IMHO I can't think of anything else to enforce some limit on a HW scarce resource that unpriv userspace can consume.

Jason

#254 lore raw

Re: [RFC PATCH 15/18] cgroup: Introduce ioasids controller

In reply to Jacob Pan
Quoted 27 lines · Jacob Pan

Hi Jacob,

Quoted 22 lines · Jacob Pan

Hi Tejun,

Quoted 18 lines · Tejun Heo
Quoted 11 lines · Jacob Pan

IOASIDs are used to associate DMA requests with virtual address spaces. They are a system-wide limited resource made available to the userspace applications. Let it be VMs or user-space device drivers.

This RFC patch introduces a cgroup controller to address the following problems:
1. Some user applications exhaust all the available IOASIDs thus depriving others of the same host.
2. System admins need to provision VMs based on their needs for IOASIDs, e.g. the number of VMs with assigned devices that perform DMA requests with PASID.

Please take a look at the proposed misc controller:

http://lkml.kernel.org/r/20210302081705.1990283-2-vipinsh-hpIqsD4AKlfQT0dZR+AlfA@public.gmane.org

Would that fit your bill?

Quoted 11 lines
Quoted 8 lines

The interface definitely can be reused. But IOASID has a different behavior in terms of migration and ownership checking. I guess SEV key IDs are not tied to a process whereas IOASIDs are. Perhaps this can be solved by adding

kernel/cgroup/ioasids.c new file +2 -0
+	.can_attach	= ioasids_can_attach,
+	.cancel_attach	= ioasids_cancel_attach,

Let me give it a try and come back.

While I am trying to fit the IOASIDs cgroup in to the misc cgroup proposal. I'd like to have a direction check on whether this idea of using cgroup for IOASID/PASID resource management is viable.

Yes, even for host SVA it would be good to have a cgroup. Currently the number of shared address spaces is naturally limited by number of processes, which can be controlled with rlimit and cgroup. But on Arm the hardware limit on shared address spaces is 64k (number of ASIDs), easily exhausted with the default PASID and PID limits. So a cgroup for managing this resource is more than welcome.

It looks like your current implementation is very dependent on IOASID_SET_TYPE_MM? I'll need to do more reading about cgroup to see how easily it can be adapted to host SVA which uses IOASID_SET_TYPE_NULL.

Thanks,
Jean

author
#255 lore raw

Re: [RFC PATCH 15/18] cgroup: Introduce ioasids controller

Hi Jean-Philippe,

Quoted 48 lines · Jean-Philippe Brucker
Quoted 28 lines · Jacob Pan

Hi Jacob,

Quoted 23 lines · Jacob Pan

Hi Tejun,

Quoted 19 lines · Tejun Heo
Quoted 5 lines · Jacob Pan

IOASIDs are used to associate DMA requests with virtual address spaces. They are a system-wide limited resource made available to the userspace applications. Let it be VMs or user-space device drivers.

Quoted 7 lines

This RFC patch introduces a cgroup controller to address the following problems:
1. Some user applications exhaust all the available IOASIDs thus depriving others of the same host.
2. System admins need to provision VMs based on their needs for IOASIDs, e.g. the number of VMs with assigned devices that perform DMA requests with PASID.

Please take a look at the proposed misc controller:

http://lkml.kernel.org/r/[email protected]

Would that fit your bill?

Quoted 11 lines
Quoted 8 lines

The interface definitely can be reused. But IOASID has a different behavior in terms of migration and ownership checking. I guess SEV key IDs are not tied to a process whereas IOASIDs are. Perhaps this can be solved by adding

kernel/cgroup/ioasids.c new file +2 -0
+	.can_attach	= ioasids_can_attach,
+	.cancel_attach	= ioasids_cancel_attach,

Let me give it a try and come back.

While I am trying to fit the IOASIDs cgroup in to the misc cgroup proposal. I'd like to have a direction check on whether this idea of using cgroup for IOASID/PASID resource management is viable.

Yes, even for host SVA it would be good to have a cgroup. Currently the number of shared address spaces is naturally limited by number of processes, which can be controlled with rlimit and cgroup. But on Arm the hardware limit on shared address spaces is 64k (number of ASIDs), easily exhausted with the default PASID and PID limits. So a cgroup for managing this resource is more than welcome.

Quoted 4 lines

It looks like your current implementation is very dependent on IOASID_SET_TYPE_MM? I'll need to do more reading about cgroup to see how easily it can be adapted to host SVA which uses IOASID_SET_TYPE_NULL.

Right, I was assuming have three use cases of IOASIDs:
1. host supervisor SVA (not a concern, just one init_mm to bind)
2. host user SVA, either one IOASID per process or perhaps some private IOASID for private address space
3. VM use for guest SVA, each IOASID is bound to a guest process

My current cgroup proposal applies to #3 with IOASID_SET_TYPE_MM, which is allocated by the new /dev/ioasid interface.

For #2, I was thinking you can limit the host process via PIDs cgroup? i.e. limit fork. So the host IOASIDs are currently allocated from the system pool with quota of chosen by iommu_sva_init() in my patch, 0 means unlimited use whatever is available. https://lkml.org/lkml/2021/2/28/18

Quoted 2 lines

Thanks,
Jean

Thanks,

Jacob
_______________________________________________
iommu mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/iommu

#256 lore raw

Re: [RFC PATCH 15/18] cgroup: Introduce ioasids controller

Quoted 6 lines · Jacob Pan

Right, I was assuming have three use cases of IOASIDs:
1. host supervisor SVA (not a concern, just one init_mm to bind)
2. host user SVA, either one IOASID per process or perhaps some private IOASID for private address space
3. VM use for guest SVA, each IOASID is bound to a guest process

Quoted 7 lines

My current cgroup proposal applies to #3 with IOASID_SET_TYPE_MM, which is allocated by the new /dev/ioasid interface.

For #2, I was thinking you can limit the host process via PIDs cgroup? i.e. limit fork. So the host IOASIDs are currently allocated from the system pool with quota of chosen by iommu_sva_init() in my patch, 0 means unlimited use whatever is available. https://lkml.org/lkml/2021/2/28/18

Why do we need two pools?

If PASID's are limited then why does it matter how the PASID was allocated? Either the thing requesting it is below the limit, or it isn't.

For something like qemu I'd expect to put the qemu process in a cgroup with 1 PASID. Who cares what qemu uses the PASID for, or how it was allocated?

Jason

author
#257 lore raw

Re: [RFC PATCH 15/18] cgroup: Introduce ioasids controller

Hi Jason,

Quoted 17 lines · Jason Gunthorpe
Quoted 6 lines · Jacob Pan

Right, I was assuming have three use cases of IOASIDs:
1. host supervisor SVA (not a concern, just one init_mm to bind)
2. host user SVA, either one IOASID per process or perhaps some private IOASID for private address space
3. VM use for guest SVA, each IOASID is bound to a guest process

Quoted 8 lines

My current cgroup proposal applies to #3 with IOASID_SET_TYPE_MM, which is allocated by the new /dev/ioasid interface.

For #2, I was thinking you can limit the host process via PIDs cgroup? i.e. limit fork. So the host IOASIDs are currently allocated from the system pool with quota of chosen by iommu_sva_init() in my patch, 0 means unlimited use whatever is available.
https://lkml.org/lkml/2021/2/28/18

Quoted 6 lines

Why do we need two pools?

If PASID's are limited then why does it matter how the PASID was allocated? Either the thing requesting it is below the limit, or it isn't.

you are right. it should be tracked based on the process regardless it is allocated by the user (/dev/ioasid) or indirectly by kernel drivers during iommu_sva_bind_device(). Need to consolidate both 2 and 3 and decouple cgroup and IOASID set.

Quoted 4 lines

For something like qemu I'd expect to put the qemu process in a cgroup with 1 PASID. Who cares what qemu uses the PASID for, or how it was allocated?

For vSVA, we will need one PASID per guest process. But that is up to the admin based on whether or how many SVA capable devices are directly assigned.

Quoted 1 line

Jason

Thanks,

Jacob
_______________________________________________
iommu mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/iommu

#258 lore raw

Re: [RFC PATCH 15/18] cgroup: Introduce ioasids controller

Quoted 7 lines · Jacob Pan
Quoted 3 lines

For something like qemu I'd expect to put the qemu process in a cgroup with 1 PASID. Who cares what qemu uses the PASID for, or how it was allocated?

For vSVA, we will need one PASID per guest process. But that is up to the admin based on whether or how many SVA capable devices are directly assigned.

I hope the virtual IOMMU driver can communicate the PASID limit and the cgroup machinery in the guest can know what the actual limit is.

I was thinking of a case where qemu is using a single PASID to setup the guest kVA or similar

Jason

author
#259 lore raw

Re: [RFC PATCH 15/18] cgroup: Introduce ioasids controller

Hi Jason,

Quoted 6 lines · Jason Gunthorpe
Quoted 4 lines · Jacob Pan
Quoted 3 lines

For something like qemu I'd expect to put the qemu process in a cgroup with 1 PASID. Who cares what qemu uses the PASID for, or how it was allocated?

Quoted 7 lines
Quoted 3 lines

For vSVA, we will need one PASID per guest process. But that is up to the admin based on whether or how many SVA capable devices are directly assigned.

I hope the virtual IOMMU driver can communicate the PASID limit and the cgroup machinery in the guest can know what the actual limit is.

For VT-d, emulated vIOMMU can communicate with the guest IOMMU driver on how many PASID bits are supported (extended cap reg PASID size fields). But it cannot communicate how many PASIDs are in the pool(host cgroup capacity).

The QEMU process may not be the only one in a cgroup so it cannot give hard guarantees. I don't see a good way to communicate accurately at runtime as the process migrates or limit changes.

We were thinking to adopt the "Limits" model as defined in the cgroup-v2 doc.
"
Limits
------

A child can only consume upto the configured amount of the resource. Limits can be over-committed - the sum of the limits of children can exceed the amount of resource available to the parent.
"

So the guest cgroup would still think it has full 20 bits of PASID at its disposal. But PASID allocation may fail before reaching the full 20 bits (2M).
Similar on the host side, we only enforce the limit set by the cgroup but not guarantee it.

Quoted 3 lines

I was thinking of a case where qemu is using a single PASID to setup the guest kVA or similar

got it.

Quoted 1 line

Jason

Thanks,

Jacob
_______________________________________________
iommu mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/iommu

#260 lore raw

Re: [RFC PATCH 15/18] cgroup: Introduce ioasids controller

In reply to Jacob Pan
Quoted 63 lines · Jacob Pan

Hi Jean-Philippe,

Quoted 48 lines · Jean-Philippe Brucker
Quoted 28 lines · Jacob Pan

Hi Jacob,

Quoted 23 lines · Jacob Pan

Hi Tejun,

Quoted 19 lines · Tejun Heo
Quoted 5 lines · Jacob Pan

IOASIDs are used to associate DMA requests with virtual address spaces. They are a system-wide limited resource made available to the userspace applications. Let it be VMs or user-space device drivers.

Quoted 7 lines

This RFC patch introduces a cgroup controller to address the following problems:
1. Some user applications exhaust all the available IOASIDs thus depriving others of the same host.
2. System admins need to provision VMs based on their needs for IOASIDs, e.g. the number of VMs with assigned devices that perform DMA requests with PASID.

Please take a look at the proposed misc controller:

http://lkml.kernel.org/r/20210302081705.1990283-2-vipinsh-hpIqsD4AKlfQT0dZR+AlfA@public.gmane.org

Would that fit your bill?

Quoted 11 lines
Quoted 8 lines

The interface definitely can be reused. But IOASID has a different behavior in terms of migration and ownership checking. I guess SEV key IDs are not tied to a process whereas IOASIDs are. Perhaps this can be solved by adding

kernel/cgroup/ioasids.c new file +2 -0
+	.can_attach	= ioasids_can_attach,
+	.cancel_attach	= ioasids_cancel_attach,

Let me give it a try and come back.

While I am trying to fit the IOASIDs cgroup in to the misc cgroup proposal. I'd like to have a direction check on whether this idea of using cgroup for IOASID/PASID resource management is viable.

Yes, even for host SVA it would be good to have a cgroup. Currently the number of shared address spaces is naturally limited by number of processes, which can be controlled with rlimit and cgroup. But on Arm the hardware limit on shared address spaces is 64k (number of ASIDs), easily exhausted with the default PASID and PID limits. So a cgroup for managing this resource is more than welcome.

Quoted 4 lines

It looks like your current implementation is very dependent on IOASID_SET_TYPE_MM? I'll need to do more reading about cgroup to see how easily it can be adapted to host SVA which uses IOASID_SET_TYPE_NULL.

Right, I was assuming have three use cases of IOASIDs:
1. host supervisor SVA (not a concern, just one init_mm to bind)
2. host user SVA, either one IOASID per process or perhaps some private IOASID for private address space
3. VM use for guest SVA, each IOASID is bound to a guest process

Quoted 5 lines

My current cgroup proposal applies to #3 with IOASID_SET_TYPE_MM, which is allocated by the new /dev/ioasid interface.

For #2, I was thinking you can limit the host process via PIDs cgroup? i.e. limit fork.

That works but isn't perfect, because the hardware resource of shared address spaces can be much lower that PID limit - 16k ASIDs on Arm. To allow an admin to fairly distribute that resource we could introduce another cgroup just to limit the number of shared address spaces, but limiting the number of IOASIDs does the trick.

Quoted 3 lines

So the host IOASIDs are currently allocated from the system pool with quota of chosen by iommu_sva_init() in my patch, 0 means unlimited use whatever is available. https://lkml.org/lkml/2021/2/28/18

Yes that's sensible, but it would be good to plan the cgroup user interface to work for #2 as well, even if we don't implement it right away.

Thanks,
Jean

#261 lore raw

Re: [RFC PATCH 15/18] cgroup: Introduce ioasids controller

Quoted 2 lines · Jean-Philippe Brucker

That works but isn't perfect, because the hardware resource of shared address spaces can be much lower that PID limit - 16k ASIDs on Arm. To

Sorry I meant 16-bit here - 64k

Thanks,
Jean

author
#262 lore raw

Re: [RFC PATCH 15/18] cgroup: Introduce ioasids controller

In reply to Jean-Philippe Brucker

Hi Jean-Philippe,

Quoted 67 lines · Jean-Philippe Brucker
Quoted 66 lines · Jacob Pan

Hi Jean-Philippe,

Quoted 48 lines · Jean-Philippe Brucker
Quoted 28 lines · Jacob Pan

Hi Jacob,

Quoted 23 lines · Jacob Pan

Hi Tejun,

Quoted 19 lines · Tejun Heo
Quoted 5 lines · Jacob Pan

IOASIDs are used to associate DMA requests with virtual address spaces. They are a system-wide limited resource made available to the userspace applications. Let it be VMs or user-space device drivers.

Quoted 7 lines

This RFC patch introduces a cgroup controller to address the following problems:
1. Some user applications exhaust all the available IOASIDs thus depriving others of the same host.
2. System admins need to provision VMs based on their needs for IOASIDs, e.g. the number of VMs with assigned devices that perform DMA requests with PASID.

Please take a look at the proposed misc controller:

http://lkml.kernel.org/r/[email protected]

Would that fit your bill?

Quoted 11 lines
Quoted 8 lines

The interface definitely can be reused. But IOASID has a different behavior in terms of migration and ownership checking. I guess SEV key IDs are not tied to a process whereas IOASIDs are. Perhaps this can be solved by adding

(quoted diff) +2 -0
+	.can_attach	= ioasids_can_attach,
+	.cancel_attach	= ioasids_cancel_attach,

Let me give it a try and come back.

While I am trying to fit the IOASIDs cgroup in to the misc cgroup proposal. I'd like to have a direction check on whether this idea of using cgroup for IOASID/PASID resource management is viable.

Yes, even for host SVA it would be good to have a cgroup. Currently the number of shared address spaces is naturally limited by number of processes, which can be controlled with rlimit and cgroup. But on Arm the hardware limit on shared address spaces is 64k (number of ASIDs), easily exhausted with the default PASID and PID limits. So a cgroup for managing this resource is more than welcome.

Quoted 4 lines

It looks like your current implementation is very dependent on IOASID_SET_TYPE_MM? I'll need to do more reading about cgroup to see how easily it can be adapted to host SVA which uses IOASID_SET_TYPE_NULL.

Right, I was assuming have three use cases of IOASIDs:
1. host supervisor SVA (not a concern, just one init_mm to bind)
2. host user SVA, either one IOASID per process or perhaps some private IOASID for private address space
3. VM use for guest SVA, each IOASID is bound to a guest process

My current cgroup proposal applies to #3 with IOASID_SET_TYPE_MM, which is allocated by the new /dev/ioasid interface.

Quoted 9 lines
Quoted 2 lines

For #2, I was thinking you can limit the host process via PIDs cgroup? i.e. limit fork.

That works but isn't perfect, because the hardware resource of shared address spaces can be much lower that PID limit - 16k ASIDs on Arm. To allow an admin to fairly distribute that resource we could introduce another cgroup just to limit the number of shared address spaces, but limiting the number of IOASIDs does the trick.

make sense. it would be cleaner to have a single approach to limit IOASIDs (as Jason asked).

Quoted 8 lines
Quoted 3 lines

So the host IOASIDs are currently allocated from the system pool with quota of chosen by iommu_sva_init() in my patch, 0 means unlimited use whatever is available. https://lkml.org/lkml/2021/2/28/18

Yes that's sensible, but it would be good to plan the cgroup user interface to work for #2 as well, even if we don't implement it right away.

will do it in the next version.

Quoted 2 lines

Thanks,
Jean

Thanks,

Jacob
_______________________________________________
iommu mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/iommu

#266 lore raw

Re: [RFC PATCH 18/18] ioasid: Add /dev/ioasid for userspace

In reply to Jacob Pan

include/uapi/linux/ioasid.h line 42

Quoted 12 lines · Jacob Pan
include/uapi/linux/ioasid.h new file +12 -0
+/* -------- IOCTLs for IOASID file descriptor (/dev/ioasid) -------- */
+
+/**
+ * IOASID_GET_API_VERSION - _IO(IOASID_TYPE, IOASID_BASE + 0)
+ *
+ * Report the version of the IOASID API.  This allows us to bump the entire
+ * API version should we later need to add or change features in incompatible
+ * ways.
+ * Return: IOASID_API_VERSION
+ * Availability: Always
+ */
+#define IOASID_GET_API_VERSION		_IO(IOASID_TYPE, IOASID_BASE + 0)

I think this is generally a bad idea, if you change the API later then also change the ioctl numbers and everything should work out

eg use the 4th argument to IOC to specify something about the ABI

Jason

author
#267 lore raw

Re: [RFC PATCH 18/18] ioasid: Add /dev/ioasid for userspace

Hi Jason,

Thanks for the review.

Quoted 21 lines · Jason Gunthorpe
Quoted 7 lines · Jacob Pan
include/uapi/linux/ioasid.h new file +6 -0
+/* -------- IOCTLs for IOASID file descriptor (/dev/ioasid) -------- */
+
+/**
+ * IOASID_GET_API_VERSION - _IO(IOASID_TYPE, IOASID_BASE + 0)
+ *
+ * Report the version of the IOASID API.  This allows us to bump the

entire

Quoted 8 lines

+ * API version should we later need to add or change features in incompatible
+ * ways.
+ * Return: IOASID_API_VERSION
+ * Availability: Always
+ */
+#define IOASID_GET_API_VERSION _IO(IOASID_TYPE,
IOASID_BASE + 0)

I think this is generally a bad idea, if you change the API later then also change the ioctl numbers and everything should work out

Quoted 2 lines

eg use the 4th argument to IOC to specify something about the ABI

Let me try to understand the idea, do you mean something like this? #define IOASID_GET_INFO _IOC(_IOC_NONE, IOASID_TYPE, IOASID_BASE + 1, sizeof(struct ioasid_info))

If we later change the size of struct ioasid_info, IOASID_GET_INFO would be a different ioctl number. Then we will break the existing user space that uses the old number. So I am guessing you meant we need to have a different name also. i.e.

#define IOASID_GET_INFO_V2 _IOC(_IOC_NONE, IOASID_TYPE, IOASID_BASE + 1, sizeof(struct ioasid_info_v2))

We can get rid of the API version, just have individual IOCTL version. Is that right?

Quoted 1 line

Jason

Thanks,

Jacob
_______________________________________________
iommu mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/iommu

#268 lore raw

Re: [RFC PATCH 18/18] ioasid: Add /dev/ioasid for userspace

Quoted 29 lines · Jacob Pan

Hi Jason,

Thanks for the review.

Quoted 21 lines · Jason Gunthorpe
Quoted 7 lines · Jacob Pan
include/uapi/linux/ioasid.h new file +6 -0
+/* -------- IOCTLs for IOASID file descriptor (/dev/ioasid) -------- */
+
+/**
+ * IOASID_GET_API_VERSION - _IO(IOASID_TYPE, IOASID_BASE + 0)
+ *
+ * Report the version of the IOASID API.  This allows us to bump the

entire

Quoted 8 lines

+ * API version should we later need to add or change features in incompatible
+ * ways.
+ * Return: IOASID_API_VERSION
+ * Availability: Always
+ */
+#define IOASID_GET_API_VERSION _IO(IOASID_TYPE,
IOASID_BASE + 0)

I think this is generally a bad idea, if you change the API later then also change the ioctl numbers and everything should work out

Quoted 2 lines

eg use the 4th argument to IOC to specify something about the ABI

Quoted 8 lines

Let me try to understand the idea, do you mean something like this? #define IOASID_GET_INFO _IOC(_IOC_NONE, IOASID_TYPE, IOASID_BASE + 1, sizeof(struct ioasid_info))

If we later change the size of struct ioasid_info, IOASID_GET_INFO would be a different ioctl number. Then we will break the existing user space that uses the old number. So I am guessing you meant we need to have a different name also. i.e.

Something like that is more appropriate. Generally we should not be planning to 'remove' IOCTLs. The kernel must always have backwards compat, so any new format you introduce down the road has to have new IOCTL number so the old format can continue to be supported.

Negotiation of support can usually by done by probing for ENOIOCTLCMD or similar on the new ioctls, not an API version

Jason

author
#6 lore raw

Re: [PATCH V4 05/18] iommu/ioasid: Redefine IOASID set and allocation APIs

In reply to Jacob Pan

Hi Jean,

Slightly off the title. As we are moving to use cgroup to limit PASID allocations, it would be much simpler if we enforce on the current task.

However, iommu_sva_alloc_pasid() takes an mm_struct pointer as argument which implies it can be something other the the current task mm. So far all kernel callers use current task mm. Is there a use case for doing PASID allocation on behalf of another mm? If not, can we remove the mm argument?

Thanks,

Jacob

Quoted 5 lines

/**
* iommu_sva_alloc_pasid - Allocate a PASID for the mm

drivers/iommu/iommu-sva-lib.c +0 -0
@@ -35,11 +44,11 @@ int iommu_sva_alloc_pasid(struct mm_struct *mm,
 ioasid_t min, ioasid_t max) mutex_lock(&iommu_sva_lock);
 	if (mm->pasid) {

drivers/iommu/iommu-sva-lib.c int iommu_sva_alloc_pasid(struct mm_struct *mm, · line 53

Quoted 10 lines
drivers/iommu/iommu-sva-lib.c +2 -2
 		if (mm->pasid >= min && mm->pasid <= max)
-			ioasid_get(mm->pasid);
+			ioasid_get(iommu_sva_pasid, mm->pasid);
 		else
 			ret = -EOVERFLOW;
 	} else {
-		pasid = ioasid_alloc(&iommu_sva_pasid, min, max, mm);
+		pasid = ioasid_alloc(iommu_sva_pasid, min, max, mm);
 		if (pasid == INVALID_IOASID)
 			ret = -ENOMEM;

Thanks,

Jacob
_______________________________________________
iommu mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/iommu

#7 lore raw

Re: [PATCH V4 05/18] iommu/ioasid: Redefine IOASID set and allocation APIs

Hi Jacob,

Quoted 4 lines · Jacob Pan

Hi Jean,

Slightly off the title. As we are moving to use cgroup to limit PASID allocations, it would be much simpler if we enforce on the current task.

Yes I think we should do that. Is there a problem with charging the process that does the PASID allocation even if the PASID indexes some other mm?

Quoted 4 lines

However, iommu_sva_alloc_pasid() takes an mm_struct pointer as argument which implies it can be something other the the current task mm. So far all kernel callers use current task mm. Is there a use case for doing PASID allocation on behalf of another mm? If not, can we remove the mm argument?

This would effectively remove the mm parameter from iommu_sva_bind_device(). I'm not opposed to that, but reintroducing it later will be difficult if IOMMU drivers start assuming that the bound mm is from current.

Although there is no use for it at the moment (only two upstream users and it looks like amdkfd always uses current too), I quite like the client-server model where the privileged process does bind() and programs the hardware queue on behalf of the client process.

Thanks,
Jean

#8 lore raw

Re: [PATCH V4 05/18] iommu/ioasid: Redefine IOASID set and allocation APIs

Quoted 4 lines · Jean-Philippe Brucker

Although there is no use for it at the moment (only two upstream users and it looks like amdkfd always uses current too), I quite like the client-server model where the privileged process does bind() and programs the hardware queue on behalf of the client process.

This creates a lot complexity, how do does process A get a secure reference to B? How does it access the memory in B to setup the HW?

Why do we need separation anyhow? SVM devices are supposed to be secure or they shouldn't do SVM.

Jason

#9 lore raw

Re: [PATCH V4 05/18] iommu/ioasid: Redefine IOASID set and allocation APIs

Quoted 9 lines · Jason Gunthorpe
Quoted 4 lines · Jean-Philippe Brucker

Although there is no use for it at the moment (only two upstream users and it looks like amdkfd always uses current too), I quite like the client-server model where the privileged process does bind() and programs the hardware queue on behalf of the client process.

This creates a lot complexity, how do does process A get a secure reference to B? How does it access the memory in B to setup the HW?

mm_access() for example, and passing addresses via IPC

Quoted 2 lines

Why do we need separation anyhow? SVM devices are supposed to be secure or they shouldn't do SVM.

Right

Thanks,
Jean

#10 lore raw

Re: [PATCH V4 05/18] iommu/ioasid: Redefine IOASID set and allocation APIs

Quoted 8 lines · Jean-Philippe Brucker
Quoted 7 lines · Jason Gunthorpe
Quoted 4 lines · Jean-Philippe Brucker

Although there is no use for it at the moment (only two upstream users and it looks like amdkfd always uses current too), I quite like the client-server model where the privileged process does bind() and programs the hardware queue on behalf of the client process.

Quoted 4 lines
Quoted 2 lines

This creates a lot complexity, how do does process A get a secure reference to B? How does it access the memory in B to setup the HW?

mm_access() for example, and passing addresses via IPC

I'd rather the source process establish its own PASID and then pass the rights to use it to some other process via FD passing than try to go the other way. There are lots of security questions with something like mm_access.

Jason

author
#239 lore raw

Re: [PATCH V4 05/18] iommu/ioasid: Redefine IOASID set and allocation APIs

In reply to Jean-Philippe Brucker

Hi Jean-Philippe,

Quoted 7 lines · Jean-Philippe Brucker
Quoted 3 lines

Slightly off the title. As we are moving to use cgroup to limit PASID allocations, it would be much simpler if we enforce on the current task.

Yes I think we should do that. Is there a problem with charging the process that does the PASID allocation even if the PASID indexes some other mm?

Besides complexity, my second concern is that we are sharing the misc cgroup controller with other resources that do not have such behavior.

Cgroup v2 also has unified hierarchy which also requires coherent behavior among controllers.

Thanks,

Jacob
_______________________________________________
iommu mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/iommu

author
#11 lore raw

Re: [PATCH V4 05/18] iommu/ioasid: Redefine IOASID set and allocation APIs

In reply to Jason Gunthorpe

Hi Jason,

Quoted 13 lines · Jason Gunthorpe
Quoted 12 lines · Jean-Philippe Brucker
Quoted 10 lines · Jason Gunthorpe
Quoted 5 lines · Jean-Philippe Brucker

Although there is no use for it at the moment (only two upstream users and it looks like amdkfd always uses current too), I quite like the client-server model where the privileged process does bind() and programs the hardware queue on behalf of the client process.

This creates a lot complexity, how do does process A get a secure reference to B? How does it access the memory in B to setup the HW?

Quoted 7 lines
Quoted 1 line

mm_access() for example, and passing addresses via IPC

I'd rather the source process establish its own PASID and then pass the rights to use it to some other process via FD passing than try to go the other way. There are lots of security questions with something like mm_access.

Thank you all for the input, it sounds like we are OK to remove mm argument from iommu_sva_bind_device() and iommu_sva_alloc_pasid() for now?

Let me try to summarize PASID allocation as below:

Interfaces | Usage | Limit | bind¹ |User visible
--------------------------------------------------------------------
/dev/ioasid² | G-SVA/IOVA | cgroup | No |Yes
--------------------------------------------------------------------
char dev³ | SVA | cgroup | Yes |No
--------------------------------------------------------------------
iommu driver | default PASID| no | No |No
--------------------------------------------------------------------
kernel | super SVA | no | yes |No
--------------------------------------------------------------------

¹ Allocated during SVA bind
² PASIDs allocated via /dev/ioasid are not bound to any mm. But its
ownership is assigned to the process that does the allocation.
³ Include uacce, other private device driver char dev such as idxd

Currently, the proposed /dev/ioasid interface does not map individual PASID with an FD. The FD is at the ioasid_set granularity and bond to the current mm. We could extend the IOCTLs to cover individual PASID-FD passing case when use cases arise. Would this work?

Thanks,

Jacob
_______________________________________________
iommu mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/iommu

#12 lore raw

Re: [PATCH V4 05/18] iommu/ioasid: Redefine IOASID set and allocation APIs

Quoted 19 lines · Jacob Pan

Hi Jason,

Quoted 15 lines · Jason Gunthorpe
Quoted 13 lines · Jean-Philippe Brucker
Quoted 10 lines · Jason Gunthorpe
Quoted 5 lines · Jean-Philippe Brucker

Although there is no use for it at the moment (only two upstream users and it looks like amdkfd always uses current too), I quite like the client-server model where the privileged process does bind() and programs the hardware queue on behalf of the client process.

This creates a lot complexity, how do does process A get a secure reference to B? How does it access the memory in B to setup the HW?

mm_access() for example, and passing addresses via IPC

Quoted 8 lines
Quoted 5 lines

I'd rather the source process establish its own PASID and then pass the rights to use it to some other process via FD passing than try to go the other way. There are lots of security questions with something like mm_access.

Thank you all for the input, it sounds like we are OK to remove mm argument from iommu_sva_bind_device() and iommu_sva_alloc_pasid() for now?

Fine by me. By the way the IDXD currently missues the bind API for supervisor PASID, and the drvdata parameter isn't otherwise used. This would be a good occasion to clean both. The new bind prototype could be:

struct iommu_sva *iommu_sva_bind_device(struct device *dev, int flags)

And a flag IOMMU_SVA_BIND_SUPERVISOR (not that I plan to implement it in the SMMU, but I think we need to clean the current usage)

Quoted 10 lines

Let me try to summarize PASID allocation as below:

Interfaces | Usage | Limit | bind¹ |User visible
--------------------------------------------------------------------
/dev/ioasid² | G-SVA/IOVA | cgroup | No |Yes
--------------------------------------------------------------------
char dev³ | SVA | cgroup | Yes |No
--------------------------------------------------------------------
iommu driver | default PASID| no | No |No

Is this PASID #0?

Quoted 3 lines

--------------------------------------------------------------------
kernel | super SVA | no | yes |No
--------------------------------------------------------------------

Also wondering about device driver allocating auxiliary domains for their private use, to do iommu_map/unmap on private PASIDs (a clean replacement to super SVA, for example). Would that go through the same path as /dev/ioasid and use the cgroup of current task?

Thanks,
Jean

Quoted 6 lines

¹ Allocated during SVA bind
² PASIDs allocated via /dev/ioasid are not bound to any mm. But its
ownership is assigned to the process that does the allocation.
³ Include uacce, other private device driver char dev such as idxd

Quoted 8 lines

Currently, the proposed /dev/ioasid interface does not map individual PASID with an FD. The FD is at the ioasid_set granularity and bond to the current mm. We could extend the IOCTLs to cover individual PASID-FD passing case when use cases arise. Would this work?

Thanks,

Jacob

_______________________________________________
iommu mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/iommu

#25 lore raw

Re: [PATCH V4 05/18] iommu/ioasid: Redefine IOASID set and allocation APIs

In reply to Jacob Pan
Quoted 36 lines · Jacob Pan

Hi Jason,

Quoted 13 lines · Jason Gunthorpe
Quoted 12 lines · Jean-Philippe Brucker
Quoted 10 lines · Jason Gunthorpe
Quoted 5 lines · Jean-Philippe Brucker

Although there is no use for it at the moment (only two upstream users and it looks like amdkfd always uses current too), I quite like the client-server model where the privileged process does bind() and programs the hardware queue on behalf of the client process.

This creates a lot complexity, how do does process A get a secure reference to B? How does it access the memory in B to setup the HW?

Quoted 7 lines
Quoted 1 line

mm_access() for example, and passing addresses via IPC

I'd rather the source process establish its own PASID and then pass the rights to use it to some other process via FD passing than try to go the other way. There are lots of security questions with something like mm_access.

Thank you all for the input, it sounds like we are OK to remove mm argument from iommu_sva_bind_device() and iommu_sva_alloc_pasid() for now?

Let me try to summarize PASID allocation as below:

Interfaces | Usage | Limit | bind¹ |User visible
/dev/ioasid² | G-SVA/IOVA | cgroup | No |Yes
char dev³ | SVA | cgroup | Yes |No
iommu driver | default PASID| no | No |No
kernel | super SVA | no | yes |No

Quoted 3 lines

¹ Allocated during SVA bind
² PASIDs allocated via /dev/ioasid are not bound to any mm. But its
ownership is assigned to the process that does the allocation.

What does "not bound to a mm" mean?

IMHO a use created PASID is either bound to a mm (current) at creation time, or it will never be bound to a mm and its page table is under user control via /dev/ioasid.

I thought the whole point of something like a /dev/ioasid was to get away from each and every device creating its own PASID interface?

It maybe somewhat reasonable that some devices could have some easy 'make a SVA PASID on current' interface built in, but anything more complicated should use /dev/ioasid, and anything consuming PASID should also have an API to import and attach a PASID from /dev/ioasid.

Quoted 4 lines

Currently, the proposed /dev/ioasid interface does not map individual PASID with an FD. The FD is at the ioasid_set granularity and bond to the current mm. We could extend the IOCTLs to cover individual PASID-FD passing case when use cases arise. Would this work?

Is it a good idea that the FD is per ioasid_set ? What is the set used for?

Usually kernel interfaces work nicer with a one fd/one object model.

But even if it is a set, you could pass the set between co-operating processes and the PASID can be created in the correct 'current'. But there is all kinds of security questsions as soon as you start doing anything like this - is there really a use case?

Jason
_______________________________________________
iommu mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/iommu

author
#13 lore raw

Re: [PATCH V4 05/18] iommu/ioasid: Redefine IOASID set and allocation APIs

In reply to Jean-Philippe Brucker

Hi Jean-Philippe,

Quoted 32 lines · Jean-Philippe Brucker
Quoted 27 lines · Jacob Pan

Hi Jason,

Quoted 15 lines · Jason Gunthorpe
Quoted 13 lines · Jean-Philippe Brucker
Quoted 11 lines · Jason Gunthorpe
Quoted 5 lines · Jean-Philippe Brucker

Although there is no use for it at the moment (only two upstream users and it looks like amdkfd always uses current too), I quite like the client-server model where the privileged process does bind() and programs the hardware queue on behalf of the client process.

This creates a lot complexity, how do does process A get a secure reference to B? How does it access the memory in B to setup the HW?

Quoted 7 lines
Quoted 1 line

mm_access() for example, and passing addresses via IPC

I'd rather the source process establish its own PASID and then pass the rights to use it to some other process via FD passing than try to go the other way. There are lots of security questions with something like mm_access.

Quoted 3 lines

Thank you all for the input, it sounds like we are OK to remove mm argument from iommu_sva_bind_device() and iommu_sva_alloc_pasid() for now?

Quoted 6 lines

Fine by me. By the way the IDXD currently missues the bind API for supervisor PASID, and the drvdata parameter isn't otherwise used. This would be a good occasion to clean both. The new bind prototype could be:

struct iommu_sva *iommu_sva_bind_device(struct device *dev, int flags)

yes, we really just hijacked drvdata as flags, it would be cleaner to use flags explicitly.

Quoted 3 lines

And a flag IOMMU_SVA_BIND_SUPERVISOR (not that I plan to implement it in the SMMU, but I think we need to clean the current usage)

You mean move #define SVM_FLAG_SUPERVISOR_MODE out of Intel code to be a generic flag in iommu-sva-lib.h called IOMMU_SVA_BIND_SUPERVISOR?

I agree if that is the proposal.

Quoted 13 lines
Quoted 12 lines

Let me try to summarize PASID allocation as below:

Interfaces | Usage | Limit | bind¹ |User visible
--------------------------------------------------------------------
/dev/ioasid² | G-SVA/IOVA | cgroup | No
|Yes
--------------------------------------------------------------------
char dev³ | SVA | cgroup | Yes |No
--------------------------------------------------------------------
iommu driver | default PASID| no | No |No

Quoted 2 lines

Is this PASID #0?

True for native case but not limited to PASID#0 for guest case. E.g. for mdev assignment with guest IOVA, the guest PASID would #0, but the host aux domain default PASID can be non-zero. Here I meant to include both cases.

Quoted 9 lines
Quoted 3 lines

--------------------------------------------------------------------
kernel | super SVA | no | yes |No
--------------------------------------------------------------------

Also wondering about device driver allocating auxiliary domains for their private use, to do iommu_map/unmap on private PASIDs (a clean replacement to super SVA, for example). Would that go through the same path as /dev/ioasid and use the cgroup of current task?

For the in-kernel private use, I don't think we should restrict based on cgroup, since there is no affinity to user processes. I also think the PASID allocation should just use kernel API instead of /dev/ioasid. Why would user space need to know the actual PASID # for device private domains? Maybe I missed your idea?

Quoted 17 lines

Thanks,
Jean

Quoted 14 lines

¹ Allocated during SVA bind
² PASIDs allocated via /dev/ioasid are not bound to any mm. But its
ownership is assigned to the process that does the allocation.
³ Include uacce, other private device driver char dev such as idxd

Currently, the proposed /dev/ioasid interface does not map individual PASID with an FD. The FD is at the ioasid_set granularity and bond to the current mm. We could extend the IOCTLs to cover individual PASID-FD passing case when use cases arise. Would this work?

Thanks,

Jacob

Thanks,

Jacob
_______________________________________________
iommu mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/iommu

#14 lore raw

Re: [PATCH V4 05/18] iommu/ioasid: Redefine IOASID set and allocation APIs

Quoted 10 lines · Jacob Pan
Quoted 4 lines

Also wondering about device driver allocating auxiliary domains for their private use, to do iommu_map/unmap on private PASIDs (a clean replacement to super SVA, for example). Would that go through the same path as /dev/ioasid and use the cgroup of current task?

For the in-kernel private use, I don't think we should restrict based on cgroup, since there is no affinity to user processes. I also think the PASID allocation should just use kernel API instead of /dev/ioasid. Why would user space need to know the actual PASID # for device private domains? Maybe I missed your idea?

There is not much in the kernel that isn't triggered by a process, I would be careful about the idea that there is a class of users that can consume a cgroup controlled resource without being inside the cgroup.

We've got into trouble before overlooking this and with something greenfield like PASID it would be best built in to the API to prevent a mistake. eg accepting a cgroup or process input to the allocator.

Jason

author
#26 lore raw

Re: [PATCH V4 05/18] iommu/ioasid: Redefine IOASID set and allocation APIs

In reply to Jason Gunthorpe

Hi Jason,

Quoted 41 lines · Jason Gunthorpe
Quoted 40 lines · Jacob Pan

Hi Jason,

Quoted 15 lines · Jason Gunthorpe
Quoted 13 lines · Jean-Philippe Brucker
Quoted 11 lines · Jason Gunthorpe
Quoted 5 lines · Jean-Philippe Brucker

Although there is no use for it at the moment (only two upstream users and it looks like amdkfd always uses current too), I quite like the client-server model where the privileged process does bind() and programs the hardware queue on behalf of the client process.

This creates a lot complexity, how do does process A get a secure reference to B? How does it access the memory in B to setup the HW?

Quoted 7 lines
Quoted 1 line

mm_access() for example, and passing addresses via IPC

I'd rather the source process establish its own PASID and then pass the rights to use it to some other process via FD passing than try to go the other way. There are lots of security questions with something like mm_access.

Thank you all for the input, it sounds like we are OK to remove mm argument from iommu_sva_bind_device() and iommu_sva_alloc_pasid() for now?

Let me try to summarize PASID allocation as below:

Interfaces | Usage | Limit | bind¹ |User visible
/dev/ioasid² | G-SVA/IOVA | cgroup | No

|Yes char dev³	| SVA		|  cgroup	|
Yes	|No iommu driver	| default PASID|  no
| No	|No kernel		| super SVA	| no
	| yes   |No
Quoted 6 lines
Quoted 3 lines

¹ Allocated during SVA bind
² PASIDs allocated via /dev/ioasid are not bound to any mm. But its
ownership is assigned to the process that does the allocation.

What does "not bound to a mm" mean?

I meant, the IOASID allocated via /dev/ioasid is in a clean state (just a number). It's initial state is not bound to an mm. Unlike, sva_bind_device() where the IOASID is allocated during bind time.

The use case is to support guest SVA bind, where allocation and bind are in two separate steps.

Quoted 4 lines

IMHO a use created PASID is either bound to a mm (current) at creation time, or it will never be bound to a mm and its page table is under user control via /dev/ioasid.

True for PASID used in native SVA bind. But for binding with a guest mm, PASID is allocated first (VT-d virtual cmd interface Spec 10.4.44), the bind with the host IOMMU when vIOMMU PASID cache is invalidated.

Our intention is to have two separate interfaces:
1. /dev/ioasid (allocation/free only)
2. /dev/sva (handles all SVA related activities including page tables)

Quoted 3 lines

I thought the whole point of something like a /dev/ioasid was to get away from each and every device creating its own PASID interface?

yes, but only for the use cases that need to expose PASID to the userspace. AFAICT, the cases are:
1. guest SVA (bind guest mm)
2. full PF/VF assignment(not mediated) where guest driver want to program the actual PASID onto the device.

Quoted 2 lines

It maybe somewhat reasonable that some devices could have some easy 'make a SVA PASID on current' interface built in,

I agree, this is the case PASID is hidden from the userspace, right? e.g. uacce.

Quoted 4 lines

but anything more
complicated should use /dev/ioasid, and anything consuming PASID should also have an API to import and attach a PASID from /dev/ioasid.

Would the above two use cases constitute the "complicated" criteria? Or we should say anything that need the explicit PASID value has to through /dev/ioasid?

Could you give some highlevel hint on the APIs that hook up IOASID allocated from /dev/ioasid and use cases that combine device and domain information? Yi is working on /dev/sva RFC, it would be good to have a direction check.

Quoted 6 lines
Quoted 4 lines

Currently, the proposed /dev/ioasid interface does not map individual PASID with an FD. The FD is at the ioasid_set granularity and bond to the current mm. We could extend the IOCTLs to cover individual PASID-FD passing case when use cases arise. Would this work?

Is it a good idea that the FD is per ioasid_set ?

We were thinking the allocation IOCTL is on a per set basis, then we know the ownership of between PASIDs and its set. If per PASID FD is needed, we can extend.

Quoted 3 lines

What is the set used
for?

I tried to document the concept in
https://lore.kernel.org/lkml/[email protected]/

In terms of usage for guest SVA, an ioasid_set is mostly tied to a host mm, the use case is as the following:
1. Identify a pool of PASIDs for permission checking (below to the same VM), e.g. only allow SVA binding for PASIDs allocated from the same set.

2. Allow different PASID-aware kernel subsystems to associate, e.g. KVM, device drivers, and IOMMU driver. i.e. each KVM instance only cares about the ioasid_set associated with the VM. Events notifications are also within the ioasid_set to synchronize PASID states.

3. Guest-Host PASID look up (each set has its own XArray to store the mapping)

4. Quota control (going away once we have cgroup)

Quoted 7 lines

Usually kernel interfaces work nicer with a one fd/one object model.

But even if it is a set, you could pass the set between co-operating processes and the PASID can be created in the correct 'current'. But there is all kinds of security questsions as soon as you start doing anything like this - is there really a use case?

We don't see a use case for passing ioasid_set to another process. All the four use cases above are for the current process.

Quoted 1 line

Jason

Thanks,

Jacob
_______________________________________________
iommu mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/iommu

author
#15 lore raw

Re: [PATCH V4 05/18] iommu/ioasid: Redefine IOASID set and allocation APIs

In reply to Jason Gunthorpe

Hi Jason,

Quoted 12 lines · Jason Gunthorpe
Quoted 10 lines · Jacob Pan
Quoted 4 lines

Also wondering about device driver allocating auxiliary domains for their private use, to do iommu_map/unmap on private PASIDs (a clean replacement to super SVA, for example). Would that go through the same path as /dev/ioasid and use the cgroup of current task?

For the in-kernel private use, I don't think we should restrict based on cgroup, since there is no affinity to user processes. I also think the PASID allocation should just use kernel API instead of /dev/ioasid. Why would user space need to know the actual PASID # for device private domains? Maybe I missed your idea?

Quoted 9 lines

There is not much in the kernel that isn't triggered by a process, I would be careful about the idea that there is a class of users that can consume a cgroup controlled resource without being inside the cgroup.

We've got into trouble before overlooking this and with something greenfield like PASID it would be best built in to the API to prevent a mistake. eg accepting a cgroup or process input to the allocator.

Make sense. But I think we only allow charging the current cgroup, how about I add the following to ioasid_alloc():

	misc_cg = get_current_misc_cg();
	ret = misc_cg_try_charge(MISC_CG_RES_IOASID, misc_cg, 1);
	if (ret) {
		put_misc_cg(misc_cg);
		return ret;
	}

BTW, IOASID will be one of the resources under the proposed misc cgroup.

Thanks,

Jacob
_______________________________________________
iommu mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/iommu

#16 lore raw

Re: [PATCH V4 05/18] iommu/ioasid: Redefine IOASID set and allocation APIs

Quoted 28 lines · Jacob Pan

Hi Jason,

Quoted 12 lines · Jason Gunthorpe
Quoted 10 lines · Jacob Pan
Quoted 4 lines

Also wondering about device driver allocating auxiliary domains for their private use, to do iommu_map/unmap on private PASIDs (a clean replacement to super SVA, for example). Would that go through the same path as /dev/ioasid and use the cgroup of current task?

For the in-kernel private use, I don't think we should restrict based on cgroup, since there is no affinity to user processes. I also think the PASID allocation should just use kernel API instead of /dev/ioasid. Why would user space need to know the actual PASID # for device private domains? Maybe I missed your idea?

Quoted 9 lines

There is not much in the kernel that isn't triggered by a process, I would be careful about the idea that there is a class of users that can consume a cgroup controlled resource without being inside the cgroup.

We've got into trouble before overlooking this and with something greenfield like PASID it would be best built in to the API to prevent a mistake. eg accepting a cgroup or process input to the allocator.

Make sense. But I think we only allow charging the current cgroup, how about I add the following to ioasid_alloc():

Quoted 6 lines
	misc_cg = get_current_misc_cg();
	ret = misc_cg_try_charge(MISC_CG_RES_IOASID, misc_cg, 1);
	if (ret) {
		put_misc_cg(misc_cg);
		return ret;
	}

Does that allow PASID allocation during driver probe, in kernel_init or modprobe context?

Thanks,
Jean

#24 lore raw

Re: [PATCH V4 05/18] iommu/ioasid: Redefine IOASID set and allocation APIs

In reply to Jacob Pan
Quoted 5 lines · Jacob Pan
Quoted 3 lines

And a flag IOMMU_SVA_BIND_SUPERVISOR (not that I plan to implement it in the SMMU, but I think we need to clean the current usage)

You mean move #define SVM_FLAG_SUPERVISOR_MODE out of Intel code to be a generic flag in iommu-sva-lib.h called IOMMU_SVA_BIND_SUPERVISOR?

Yes, though it would need to be in iommu.h since it's used by device drivers

Quoted 10 lines
Quoted 5 lines

Also wondering about device driver allocating auxiliary domains for their private use, to do iommu_map/unmap on private PASIDs (a clean replacement to super SVA, for example). Would that go through the same path as /dev/ioasid and use the cgroup of current task?

For the in-kernel private use, I don't think we should restrict based on cgroup, since there is no affinity to user processes. I also think the PASID allocation should just use kernel API instead of /dev/ioasid. Why would user space need to know the actual PASID # for device private domains? Maybe I missed your idea?

No that's my bad, I didn't get the role of /dev/ioasid. Let me give the series a proper read.

Thanks,
Jean

author
#17 lore raw

Re: [PATCH V4 05/18] iommu/ioasid: Redefine IOASID set and allocation APIs

In reply to Jean-Philippe Brucker

Hi Jean-Philippe,

Quoted 37 lines · Jean-Philippe Brucker
Quoted 29 lines · Jacob Pan

Hi Jason,

Quoted 13 lines · Jason Gunthorpe
Quoted 11 lines · Jacob Pan
Quoted 5 lines

Also wondering about device driver allocating auxiliary domains for their private use, to do iommu_map/unmap on private PASIDs (a clean replacement to super SVA, for example). Would that go through the same path as /dev/ioasid and use the cgroup of current task?

For the in-kernel private use, I don't think we should restrict based on cgroup, since there is no affinity to user processes. I also think the PASID allocation should just use kernel API instead of /dev/ioasid. Why would user space need to know the actual PASID

# for device private domains? Maybe I missed your idea?    
Quoted 9 lines

There is not much in the kernel that isn't triggered by a process, I would be careful about the idea that there is a class of users that can consume a cgroup controlled resource without being inside the cgroup.

We've got into trouble before overlooking this and with something greenfield like PASID it would be best built in to the API to prevent a mistake. eg accepting a cgroup or process input to the allocator.

Make sense. But I think we only allow charging the current cgroup, how about I add the following to ioasid_alloc():

Quoted 6 lines
	misc_cg = get_current_misc_cg();
	ret = misc_cg_try_charge(MISC_CG_RES_IOASID, misc_cg, 1);
	if (ret) {
		put_misc_cg(misc_cg);
		return ret;
	}  
Quoted 3 lines

Does that allow PASID allocation during driver probe, in kernel_init or modprobe context?

Good point. Yes, you can get cgroup subsystem state in kernel_init for charging/uncharging. I would think module_init should work also since it is after kernel_init. I have tried the following:
static int __ref kernel_init(void *unused)

 {
        int ret;

+ struct cgroup_subsys_state *css;
+ css = task_get_css(current, pids_cgrp_id);

But that would imply:
1. IOASID has to be built-in, not as module
2. IOASIDs charged on PID1/init would not subject to cgroup limit since it will be in the root cgroup and we don't support migration nor will migrate.

Then it comes back to the question of why do we try to limit in-kernel users per cgroup if we can't enforce these cases.

Quoted 3 lines

Thanks,
Jean

Thanks,

Jacob
_______________________________________________
iommu mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/iommu

#18 lore raw

Re: [PATCH V4 05/18] iommu/ioasid: Redefine IOASID set and allocation APIs

Quoted 54 lines · Jacob Pan

Hi Jean-Philippe,

Quoted 37 lines · Jean-Philippe Brucker
Quoted 29 lines · Jacob Pan

Hi Jason,

Quoted 13 lines · Jason Gunthorpe
Quoted 11 lines · Jacob Pan
Quoted 5 lines

Also wondering about device driver allocating auxiliary domains for their private use, to do iommu_map/unmap on private PASIDs (a clean replacement to super SVA, for example). Would that go through the same path as /dev/ioasid and use the cgroup of current task?

For the in-kernel private use, I don't think we should restrict based on cgroup, since there is no affinity to user processes. I also think the PASID allocation should just use kernel API instead of /dev/ioasid. Why would user space need to know the actual PASID

# for device private domains? Maybe I missed your idea?    
Quoted 9 lines

There is not much in the kernel that isn't triggered by a process, I would be careful about the idea that there is a class of users that can consume a cgroup controlled resource without being inside the cgroup.

We've got into trouble before overlooking this and with something greenfield like PASID it would be best built in to the API to prevent a mistake. eg accepting a cgroup or process input to the allocator.

Make sense. But I think we only allow charging the current cgroup, how about I add the following to ioasid_alloc():

Quoted 6 lines
	misc_cg = get_current_misc_cg();
	ret = misc_cg_try_charge(MISC_CG_RES_IOASID, misc_cg, 1);
	if (ret) {
		put_misc_cg(misc_cg);
		return ret;
	}  
Quoted 3 lines

Does that allow PASID allocation during driver probe, in kernel_init or modprobe context?

Good point. Yes, you can get cgroup subsystem state in kernel_init for charging/uncharging. I would think module_init should work also since it is after kernel_init. I have tried the following:
static int __ref kernel_init(void *unused)

 {
        int ret;
(quoted diff) +2 -0
+       struct cgroup_subsys_state *css;
+       css = task_get_css(current, pids_cgrp_id);
Quoted 7 lines

But that would imply:
1. IOASID has to be built-in, not as module
2. IOASIDs charged on PID1/init would not subject to cgroup limit since it will be in the root cgroup and we don't support migration nor will migrate.

Then it comes back to the question of why do we try to limit in-kernel users per cgroup if we can't enforce these cases.

Are these real use cases? Why would a driver binding to a device create a single kernel pasid at bind time? Why wouldn't it use untagged DMA?

When someone needs it they can rework it and explain why they are doing something sane.

Jason

author
#19 lore raw

Re: [PATCH V4 05/18] iommu/ioasid: Redefine IOASID set and allocation APIs

Hi Jason,

Quoted 62 lines · Jason Gunthorpe
Quoted 61 lines · Jacob Pan

Hi Jean-Philippe,

Quoted 38 lines · Jean-Philippe Brucker
Quoted 30 lines · Jacob Pan

Hi Jason,

Quoted 19 lines · Jason Gunthorpe
Quoted 6 lines · Jacob Pan
Quoted 5 lines

Also wondering about device driver allocating auxiliary domains for their private use, to do iommu_map/unmap on private PASIDs (a clean replacement to super SVA, for example). Would that go through the same path as /dev/ioasid and use the cgroup of current task?

Quoted 6 lines

For the in-kernel private use, I don't think we should restrict based on cgroup, since there is no affinity to user processes. I also think the PASID allocation should just use kernel API instead of /dev/ioasid. Why would user space need to know the actual PASID # for device private domains? Maybe I missed your idea?

There is not much in the kernel that isn't triggered by a process, I would be careful about the idea that there is a class of users that can consume a cgroup controlled resource without being inside the cgroup.

Quoted 4 lines

We've got into trouble before overlooking this and with something greenfield like PASID it would be best built in to the API to prevent a mistake. eg accepting a cgroup or process input to the allocator.

Make sense. But I think we only allow charging the current cgroup, how about I add the following to ioasid_alloc():

Quoted 6 lines
	misc_cg = get_current_misc_cg();
	ret = misc_cg_try_charge(MISC_CG_RES_IOASID, misc_cg, 1);
	if (ret) {
		put_misc_cg(misc_cg);
		return ret;
	}    
Quoted 3 lines

Does that allow PASID allocation during driver probe, in kernel_init or modprobe context?

Good point. Yes, you can get cgroup subsystem state in kernel_init for charging/uncharging. I would think module_init should work also since it is after kernel_init. I have tried the following:
static int __ref kernel_init(void *unused)

 {
        int ret;
(quoted diff) +2 -0
+       struct cgroup_subsys_state *css;
+       css = task_get_css(current, pids_cgrp_id);

But that would imply:
1. IOASID has to be built-in, not as module
2. IOASIDs charged on PID1/init would not subject to cgroup limit since it will be in the root cgroup and we don't support migration nor will migrate.

Quoted 7 lines
Quoted 2 lines

Then it comes back to the question of why do we try to limit in-kernel users per cgroup if we can't enforce these cases.

Are these real use cases? Why would a driver binding to a device create a single kernel pasid at bind time? Why wouldn't it use untagged DMA?

For VT-d, I don't see such use cases. All PASID allocations by the kernel drivers has proper process context.

Quoted 3 lines

When someone needs it they can rework it and explain why they are doing something sane.

Agreed.

Quoted 1 line

Jason

Thanks,

Jacob
_______________________________________________
iommu mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/iommu

#20 lore raw

Re: [PATCH V4 05/18] iommu/ioasid: Redefine IOASID set and allocation APIs

In reply to Jason Gunthorpe
Quoted 55 lines · Jason Gunthorpe
Quoted 54 lines · Jacob Pan

Hi Jean-Philippe,

Quoted 37 lines · Jean-Philippe Brucker
Quoted 29 lines · Jacob Pan

Hi Jason,

Quoted 13 lines · Jason Gunthorpe
Quoted 11 lines · Jacob Pan
Quoted 5 lines

Also wondering about device driver allocating auxiliary domains for their private use, to do iommu_map/unmap on private PASIDs (a clean replacement to super SVA, for example). Would that go through the same path as /dev/ioasid and use the cgroup of current task?

For the in-kernel private use, I don't think we should restrict based on cgroup, since there is no affinity to user processes. I also think the PASID allocation should just use kernel API instead of /dev/ioasid. Why would user space need to know the actual PASID

# for device private domains? Maybe I missed your idea?    
Quoted 9 lines

There is not much in the kernel that isn't triggered by a process, I would be careful about the idea that there is a class of users that can consume a cgroup controlled resource without being inside the cgroup.

We've got into trouble before overlooking this and with something greenfield like PASID it would be best built in to the API to prevent a mistake. eg accepting a cgroup or process input to the allocator.

Make sense. But I think we only allow charging the current cgroup, how about I add the following to ioasid_alloc():

Quoted 6 lines
	misc_cg = get_current_misc_cg();
	ret = misc_cg_try_charge(MISC_CG_RES_IOASID, misc_cg, 1);
	if (ret) {
		put_misc_cg(misc_cg);
		return ret;
	}  
Quoted 3 lines

Does that allow PASID allocation during driver probe, in kernel_init or modprobe context?

Good point. Yes, you can get cgroup subsystem state in kernel_init for charging/uncharging. I would think module_init should work also since it is after kernel_init. I have tried the following:
static int __ref kernel_init(void *unused)

 {
        int ret;
(quoted diff) +2 -0
+       struct cgroup_subsys_state *css;
+       css = task_get_css(current, pids_cgrp_id);
Quoted 2 lines
Quoted 2 lines

But that would imply:
1. IOASID has to be built-in, not as module

If IOASID is a module, the device driver will probe once the IOMMU module is available, which I think always happens in probe deferral kworker.

Quoted 5 lines
Quoted 5 lines

2. IOASIDs charged on PID1/init would not subject to cgroup limit since it will be in the root cgroup and we don't support migration nor will migrate.

Then it comes back to the question of why do we try to limit in-kernel users per cgroup if we can't enforce these cases.

It may be better to explicitly pass a cgroup during allocation as Jason suggested. That way anyone using the API will have to be aware of this and pass the root cgroup if that's what they want.

Quoted 3 lines

Are these real use cases? Why would a driver binding to a device create a single kernel pasid at bind time? Why wouldn't it use untagged DMA?

It's not inconceivable to have a control queue doing DMA tagged with PASID. The devices I know either use untagged DMA, or have a choice to use a PASID. We're not outright forbidding PASID allocation at boot (I don't think we can or should) and we won't be able to check every use of the API, so I'm trying to figure out whether it will always default to root cgroup, or crash in some corner case.

Thanks,
Jean

#27 lore raw

Re: [PATCH V4 05/18] iommu/ioasid: Redefine IOASID set and allocation APIs

In reply to Jacob Pan
Quoted 11 lines · Jacob Pan
Quoted 4 lines

IMHO a use created PASID is either bound to a mm (current) at creation time, or it will never be bound to a mm and its page table is under user control via /dev/ioasid.

True for PASID used in native SVA bind. But for binding with a guest mm, PASID is allocated first (VT-d virtual cmd interface Spec 10.4.44), the bind with the host IOMMU when vIOMMU PASID cache is invalidated.

Our intention is to have two separate interfaces:
1. /dev/ioasid (allocation/free only)
2. /dev/sva (handles all SVA related activities including page tables)

I'm not sure I understand why you'd want to have two things. Doesn't that just complicate everything?

Manipulating the ioasid, including filling it with page tables, seems an integral inseperable part of the whole interface. Why have two ?

Quoted 5 lines
Quoted 3 lines

I thought the whole point of something like a /dev/ioasid was to get away from each and every device creating its own PASID interface?

yes, but only for the use cases that need to expose PASID to the userspace.

Why "but only"? This thing should reach for a higher generality, not just be contained to solve some problem within qemu.

Quoted 4 lines
Quoted 2 lines

It maybe somewhat reasonable that some devices could have some easy 'make a SVA PASID on current' interface built in,

I agree, this is the case PASID is hidden from the userspace, right? e.g. uacce.

"hidden", I guess, but does it matter so much?

The PASID would still consume a cgroup credit

Quoted 7 lines
Quoted 4 lines

but anything more
complicated should use /dev/ioasid, and anything consuming PASID should also have an API to import and attach a PASID from /dev/ioasid.

Would the above two use cases constitute the "complicated" criteria? Or we should say anything that need the explicit PASID value has to through /dev/ioasid?

Anything that needs more that creating a hidden PASID link'd to current should use the full interface.

Quoted 2 lines

In terms of usage for guest SVA, an ioasid_set is mostly tied to a host mm, the use case is as the following:

From that doc:

It is imperative to enforce
VM-IOASID ownership such that a malicious guest cannot target DMA traffic outside its own IOASIDs, or free an active IOASID that belongs to another VM.

Huh?

Security in a PASID world comes from the IOMMU blocking access to the PASID except from approved PCI-ID's. If a VF/PF is assigned to a guest then that guest can cause the device to issue any PASID by having complete control and the vIOMMU is supposed to tell the real IOMMU what PASID's the device is alowed to access.

If a device is sharing a single PCI function with different security contexts (eg vfio mdev) then the device itself is responsible to ensure that only the secure interface can program a PASID and a less secure context can never self-enroll.

Here the mdev driver would have to consule with the vIOMMU to ensure the mdev device is allowed to access the PASID - is that what this set stuff is about?

If yes, it is backwards. The MDEV is the thing doing the security, the MDEV should have the list of allowed PASID's and a single PASID created under /dev/ioasid should be loaded into MDEV with some 'Ok you can use PASID xyz from FD abc' command.

Because you absolutely don't want to have a generic 'set' that all the mdevs are sharing as that violates the basic security principle at the start - each and every device must have a unique list of what PASID's it can talk to.

Quoted 8 lines

1. Identify a pool of PASIDs for permission checking (below to the same VM), e.g. only allow SVA binding for PASIDs allocated from the same set.

2. Allow different PASID-aware kernel subsystems to associate, e.g. KVM, device drivers, and IOMMU driver. i.e. each KVM instance only cares about the ioasid_set associated with the VM. Events notifications are also within the ioasid_set to synchronize PASID states.

Quoted 4 lines

3. Guest-Host PASID look up (each set has its own XArray to store the mapping)

4. Quota control (going away once we have cgroup)

It sounds worrysome things have gone this way.

I'd say you shoul have a single /dev/ioasid per VM and KVM should attach to that - it should get all the global events/etc that are not device specific.

permission checking *must* be done on a per-device level, either inside the mdev driver, or inside the IOMMU at a per-PCI device level.

Not sure what guest-host PASID means, these have to be 1:1 for device assignment to work - why would use something else for mdev?

Jason

author
#28 lore raw

Re: [PATCH V4 05/18] iommu/ioasid: Redefine IOASID set and allocation APIs

Hi Jason,

Quoted 14 lines · Jason Gunthorpe
Quoted 11 lines · Jacob Pan
Quoted 4 lines

IMHO a use created PASID is either bound to a mm (current) at creation time, or it will never be bound to a mm and its page table is under user control via /dev/ioasid.

True for PASID used in native SVA bind. But for binding with a guest mm, PASID is allocated first (VT-d virtual cmd interface Spec 10.4.44), the bind with the host IOMMU when vIOMMU PASID cache is invalidated.

Our intention is to have two separate interfaces:
1. /dev/ioasid (allocation/free only)
2. /dev/sva (handles all SVA related activities including page tables)

Quoted 6 lines

I'm not sure I understand why you'd want to have two things. Doesn't that just complicate everything?

Manipulating the ioasid, including filling it with page tables, seems an integral inseperable part of the whole interface. Why have two ?

In one of the earlier discussions, I was made aware of some use cases (by AMD, iirc) where PASID can be used w/o IOMMU. That is why I tried to keep ioasid a separate subsystem. Other than that, I don't see an issue combining the two.

Quoted 9 lines
Quoted 5 lines
Quoted 3 lines

I thought the whole point of something like a /dev/ioasid was to get away from each and every device creating its own PASID interface?

yes, but only for the use cases that need to expose PASID to the userspace.

Why "but only"? This thing should reach for a higher generality, not just be contained to solve some problem within qemu.

I totally agree in terms of generality. I was just trying to point out existing framework or drivers such as uacce and idxd driver does not have a need to use /dev/ioasid.

Quoted 7 lines
Quoted 4 lines
Quoted 2 lines

It maybe somewhat reasonable that some devices could have some easy 'make a SVA PASID on current' interface built in,

I agree, this is the case PASID is hidden from the userspace, right? e.g. uacce.

"hidden", I guess, but does it matter so much?

it matters when it comes to which interface to choose. Use /dev/ioasid to allocate if PASID value cannot be hidden. Use some other interface for bind current and allocate if a PASID is not visible to the user.

Quoted 2 lines

The PASID would still consume a cgroup credit

yes, credit still consumed. Just the PASID value is hidden.

Quoted 11 lines
Quoted 7 lines
Quoted 4 lines

but anything more
complicated should use /dev/ioasid, and anything consuming PASID should also have an API to import and attach a PASID from /dev/ioasid.

Would the above two use cases constitute the "complicated" criteria? Or we should say anything that need the explicit PASID value has to through /dev/ioasid?

Anything that needs more that creating a hidden PASID link'd to current should use the full interface.

Yes, I think we are on the same page. For example, today's uacce or idxd driver creates a hidden PASID when user does open(), where a new WQ is provisioned and bound to current mm. This is the case where /dev/ioasid is not needed.

Quoted 12 lines
Quoted 2 lines

In terms of usage for guest SVA, an ioasid_set is mostly tied to a host mm, the use case is as the following:

From that doc:

It is imperative to enforce
VM-IOASID ownership such that a malicious guest cannot target DMA traffic outside its own IOASIDs, or free an active IOASID that belongs to another VM.

Huh?

Sorry, I am not following. In the doc, I have an example to show the ioasid_set to VM/mm mapping. We use mm as the ioasid_set token to identify who the owner of an IOASID is. i.e. who allocated the IOASID. Non-owner cannot perform bind page table or free operations.

Section: IOASID Set Private ID (SPID)

 .------------------.    .------------------.
 |   VM 1           |    |   VM 2           |
 |                  |    |                  |
 |------------------|    |------------------|
 | GPASID/SPID 101  |    | GPASID/SPID 101  |
 '------------------'    -------------------'     Guest
 __________|______________________|____________________
           |                      |               Host
           v                      v
 .------------------.    .------------------.
 | Host IOASID 201  |    | Host IOASID 202  |
 '------------------'    '------------------'
 |   IOASID set 1   |    |   IOASID set 2   |
 '------------------'    '------------------'
Quoted 6 lines

Security in a PASID world comes from the IOMMU blocking access to the PASID except from approved PCI-ID's. If a VF/PF is assigned to a guest then that guest can cause the device to issue any PASID by having complete control and the vIOMMU is supposed to tell the real IOMMU what PASID's the device is alowed to access.

Yes, each PF/VF has its own PASID table. The device can do whatever it wants as long as the PASID is present in the table. Programming of the pIOMMU PASID table entry, however, is controlled by the host.

IMHO, there are two levels of security here:
1. A PASID can only be used by a secure context
2. A device can only use allowed PASIDs (PASID namespace is system-wide but PASID table storage is per PF/VF)

IOASID set is designed for #1.

Quoted 5 lines

If a device is sharing a single PCI function with different security contexts (eg vfio mdev) then the device itself is responsible to ensure that only the secure interface can program a PASID and a less secure context can never self-enroll.

If two mdevs from the same PF dev are assigned to two VMs, the PASID table will be shared. IOASID set ensures one VM cannot program another VM's PASIDs. I assume 'secure context' is per VM when it comes to host PASID.

Quoted 4 lines

Here the mdev driver would have to consule with the vIOMMU to ensure the mdev device is allowed to access the PASID - is that what this set stuff is about?

No. the mdev driver consults with IOASID core When the guest programs a guest PASID on to he mdev. VDCM driver does a lookup:
host_pasid = ioasid_find_by_spid(ioasid_set, guest_pasid);

If the guest_pasid does not exist in the ioasid_set, the mdev programming fails; if the guest_pasid does exist but it maps to a wrong host PASID, the damage is limited to the guest itself.

Quoted 5 lines

If yes, it is backwards. The MDEV is the thing doing the security, the MDEV should have the list of allowed PASID's and a single PASID created under /dev/ioasid should be loaded into MDEV with some 'Ok you can use PASID xyz from FD abc' command.

I guess that is not the case. For VT-d dedicated WQ, there is only one PASID can be programmed onto the device. Programming the PASID with /dev/sva FD abc command will be checked against its mm where /dev/ioasid is used to do the allocation.

For a single shared WQ assigned to multiple VMs, there will be one mdev per VM. Again, FD commands is limited to the PASIDs allocated for the VM.

For a single share WQ assigned to one VM, it can be bound to multiple guest processes/PASIDs. Host IOMMU driver maintains a list of the PASIDs and ensures that they are only programmed on to the per device PASID table.

Quoted 5 lines

Because you absolutely don't want to have a generic 'set' that all the mdevs are sharing as that violates the basic security principle at the start - each and every device must have a unique list of what PASID's it can talk to.

I agree, I don't think this is the case. The ioasid_set is some what orthogonal to mdev collections.

Quoted 8 lines
Quoted 8 lines

1. Identify a pool of PASIDs for permission checking (below to the same VM), e.g. only allow SVA binding for PASIDs allocated from the same set.

2. Allow different PASID-aware kernel subsystems to associate, e.g. KVM, device drivers, and IOMMU driver. i.e. each KVM instance only cares about the ioasid_set associated with the VM. Events notifications are also within the ioasid_set to synchronize PASID states.

Quoted 7 lines
Quoted 4 lines

3. Guest-Host PASID look up (each set has its own XArray to store the mapping)

4. Quota control (going away once we have cgroup)

It sounds worrysome things have gone this way.

Could you expand on that? Guaranteeing quota is very difficult. cgroup limit model fits most scalar resources.

Quoted 4 lines

I'd say you shoul have a single /dev/ioasid per VM and KVM should attach to that - it should get all the global events/etc that are not device specific.

You mean a single /dev/ioasid FD per VM and KVM? I think that is what we are doing in this set. A VM process can only open /dev/ioasid once, then use the FD for allocation and pass the PASID for bind page table etc.

Quoted 3 lines

permission checking *must* be done on a per-device level, either inside the mdev driver, or inside the IOMMU at a per-PCI device level.

I think we are on the same page. For mdev, VDCM driver makes sure the guest PASID programmed is allocated by the same VM that also performed the bind SVA.

For PF/VF which is not mediated, the permission is implied by the IOMMU driver/HW since PASID table is per device.

Quoted 3 lines

Not sure what guest-host PASID means, these have to be 1:1 for device assignment to work - why would use something else for mdev?

We have G-H PASID translation. They don't have to be 1:1.
IOASID Set Private ID (SPID) is intended as a generic solution for guest PASID. Could you review the secion Section: IOASID Set Private ID (SPID) in the doc patch?

We also had some slides from last year. Slide 3s-6 mostly.
https://static.sched.com/hosted_files/kvmforum2020/9f/KVM_forum_2020_PASID_MGMT_Yi_Jacob_final.pdf

Really appreciated your time!

Jacob
_______________________________________________
iommu mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/iommu

#38 lore raw

RE: [PATCH V4 05/18] iommu/ioasid: Redefine IOASID set and allocation APIs

In reply to Jason Gunthorpe
Quoted 17 lines

From: Jason Gunthorpe <[email protected]>
Sent: Tuesday, March 30, 2021 12:32 AM

Quoted 11 lines · Jacob Pan
Quoted 4 lines

IMHO a use created PASID is either bound to a mm (current) at creation time, or it will never be bound to a mm and its page table is under user control via /dev/ioasid.

True for PASID used in native SVA bind. But for binding with a guest mm, PASID is allocated first (VT-d virtual cmd interface Spec 10.4.44), the bind with the host IOMMU when vIOMMU PASID cache is invalidated.

Our intention is to have two separate interfaces:
1. /dev/ioasid (allocation/free only)
2. /dev/sva (handles all SVA related activities including page tables)

Quoted 5 lines

I'm not sure I understand why you'd want to have two things. Doesn't that just complicate everything?

Manipulating the ioasid, including filling it with page tables, seems an integral inseperable part of the whole interface. Why have two ?

Hi, Jason,

Actually above is a major open while we are refactoring vSVA uAPI toward this direction. We have two concerns about merging /dev/ioasid with /dev/sva, and would like to hear your thought whether they are valid.

First, userspace may use ioasid in a non-SVA scenario where ioasid is bound to specific security context (e.g. a control vq in vDPA) instead of tying to mm. In this case there is no pgtable binding initiated from user space. Instead, ioasid is allocated from /dev/ioasid and then programmed to the intended security context through specific passthrough framework which manages that context.

Second, ioasid is managed per process/VM while pgtable binding is a device-wise operation. The userspace flow looks like below for an integral /dev/ioasid interface:

-----------initialization----------
- ioctl(container->fd, VFIO_SET_IOMMU, VFIO_TYPE1_NESTING_IOMMU)
- ioasid_fd = open(/dev/ioasid)
- ioctl(ioasid_fd, IOASID_GET_USVA_FD, &sva_fd) //an empty context
- ioctl(device->fd, VFIO_DEVICE_SET_SVA, &sva_fd); //sva_fd ties to device
- ioctl(sva_fd, USVA_GET_INFO, &sva_info); -----------runtime----------------
- ioctl(ioasid_fd, IOMMU_ALLOC_IOASID, &ioasid);
- ioctl(sva_fd, USVA_BIND_PGTBL, &bind_data);
- ioctl(sva_fd, USVA_FLUSH_CACHE, &inv_info);
- ioctl(sva_fd, USVA_UNBIND_PGTBL, &unbind_data); -----------destroy----------------
- ioctl(device->fd, VFIO_DEVICE_UNSET_SVA, &sva_fd);
- close(sva_fd)
- close(ioasid_fd)

Our hesitation here is based on one of your earlier comments that you are not a fan of constructing fd's through ioctl. Are you OK with above flow or have a better idea of handling it?

With separate interfaces then userspace just opens /dev/sva instead of getting it through ioasid_fd:

- ioasid_fd = open(/dev/ioasid)
- sva_fd = open(/dev/sva)

Thanks
Kevin

#229 lore raw

RE: [PATCH V4 05/18] iommu/ioasid: Redefine IOASID set and allocation APIs

In reply to Jason Gunthorpe
Quoted 25 lines

From: Jason Gunthorpe <[email protected]>
Sent: Tuesday, March 30, 2021 12:32 AM

Quoted 2 lines

In terms of usage for guest SVA, an ioasid_set is mostly tied to a host mm, the use case is as the following:

From that doc:

It is imperative to enforce
VM-IOASID ownership such that a malicious guest cannot target DMA traffic outside its own IOASIDs, or free an active IOASID that belongs to another VM.

Huh?

Security in a PASID world comes from the IOMMU blocking access to the PASID except from approved PCI-ID's. If a VF/PF is assigned to a guest then that guest can cause the device to issue any PASID by having complete control and the vIOMMU is supposed to tell the real IOMMU what PASID's the device is alowed to access.

If a device is sharing a single PCI function with different security contexts (eg vfio mdev) then the device itself is responsible to ensure that only the secure interface can program a PASID and a less secure context can never self-enroll.

Quoted 9 lines

Here the mdev driver would have to consule with the vIOMMU to ensure the mdev device is allowed to access the PASID - is that what this set stuff is about?

If yes, it is backwards. The MDEV is the thing doing the security, the MDEV should have the list of allowed PASID's and a single PASID created under /dev/ioasid should be loaded into MDEV with some 'Ok you can use PASID xyz from FD abc' command.

The 'set' is per-VM. Once the mdev is assigned to a VM, all valid PASID's in the set of that VM are considered legitimate on this mdev. The mdev driver will mediate guest operations which program PASID to the backend context and load the PASID only if it is within the 'set' (i.e. already allocated through /dev/ioasid). This prevents a malicious VM from attacking others. Though it's not mdev which directly maintaining the list of allowed PASID's, the effect is the same in concept.

Thanks
Kevin

#231 lore raw

RE: [PATCH V4 05/18] iommu/ioasid: Redefine IOASID set and allocation APIs

In reply to Jason Gunthorpe
Quoted 38 lines

From: Tian, Kevin
Sent: Tuesday, March 30, 2021 10:24 AM

Quoted 25 lines

From: Jason Gunthorpe <[email protected]>
Sent: Tuesday, March 30, 2021 12:32 AM

Quoted 2 lines

In terms of usage for guest SVA, an ioasid_set is mostly tied to a host mm, the use case is as the following:

From that doc:

It is imperative to enforce
VM-IOASID ownership such that a malicious guest cannot target DMA traffic outside its own IOASIDs, or free an active IOASID that belongs to another VM.

Huh?

Security in a PASID world comes from the IOMMU blocking access to the PASID except from approved PCI-ID's. If a VF/PF is assigned to a guest then that guest can cause the device to issue any PASID by having complete control and the vIOMMU is supposed to tell the real IOMMU what PASID's the device is alowed to access.

If a device is sharing a single PCI function with different security contexts (eg vfio mdev) then the device itself is responsible to ensure that only the secure interface can program a PASID and a less secure context can never self-enroll.

Quoted 9 lines

Here the mdev driver would have to consule with the vIOMMU to ensure the mdev device is allowed to access the PASID - is that what this set stuff is about?

If yes, it is backwards. The MDEV is the thing doing the security, the MDEV should have the list of allowed PASID's and a single PASID created under /dev/ioasid should be loaded into MDEV with some 'Ok you can use PASID xyz from FD abc' command.

Quoted 8 lines

The 'set' is per-VM. Once the mdev is assigned to a VM, all valid PASID's in the set of that VM are considered legitimate on this mdev. The mdev driver will mediate guest operations which program PASID to the backend context and load the PASID only if it is within the 'set' (i.e. already allocated through /dev/ioasid). This prevents a malicious VM from attacking others. Though it's not mdev which directly maintaining the list of allowed PASID's, the effect is the same in concept.

One correction. The mdev should still construct the list of allowed PASID's as you said (by listening to IOASID_BIND/UNBIND event), in addition to the ioasid set maintained per VM (updated when a PASID is allocated/freed). The per-VM set is required for inter-VM isolation (verified when a pgtable is bound to the mdev/PASID), while the mdev's own list is necessary for intra-VM isolation when multiple mdevs are assigned to the same VM (verified before loading a PASID to the mdev). This series just handles the general part i.e. per-VM ioasid set and leaves the mdev's own list to be managed by specific mdev driver which listens to various IOASID events).

Thanks
Kevin

#21 lore raw

Re: [PATCH V4 05/18] iommu/ioasid: Redefine IOASID set and allocation APIs

In reply to Jean-Philippe Brucker
Quoted 3 lines · Jean-Philippe Brucker

It's not inconceivable to have a control queue doing DMA tagged with PASID. The devices I know either use untagged DMA, or have a choice to use a PASID.

I don't think we should encourage that. A PASID and all the related is so expensive compared to just doing normal untagged kernel DMA.

I assume HW has these features because virtualization use cases might use them, eg by using mdev to assign a command queue - then it would need be be contained by a PASID.

Jason

#230 lore raw

Re: [PATCH V4 05/18] iommu/ioasid: Redefine IOASID set and allocation APIs

In reply to Tian, Kevin
Quoted 29 lines · Kevin
Quoted 29 lines

From: Jason Gunthorpe <[email protected]>
Sent: Tuesday, March 30, 2021 12:32 AM

Quoted 2 lines

In terms of usage for guest SVA, an ioasid_set is mostly tied to a host mm, the use case is as the following:

From that doc:

It is imperative to enforce
VM-IOASID ownership such that a malicious guest cannot target DMA traffic outside its own IOASIDs, or free an active IOASID that belongs to another VM.

Huh?

Security in a PASID world comes from the IOMMU blocking access to the PASID except from approved PCI-ID's. If a VF/PF is assigned to a guest then that guest can cause the device to issue any PASID by having complete control and the vIOMMU is supposed to tell the real IOMMU what PASID's the device is alowed to access.

If a device is sharing a single PCI function with different security contexts (eg vfio mdev) then the device itself is responsible to ensure that only the secure interface can program a PASID and a less secure context can never self-enroll.

Here the mdev driver would have to consule with the vIOMMU to ensure the mdev device is allowed to access the PASID - is that what this set stuff is about?

Quoted 8 lines
Quoted 5 lines

If yes, it is backwards. The MDEV is the thing doing the security, the MDEV should have the list of allowed PASID's and a single PASID created under /dev/ioasid should be loaded into MDEV with some 'Ok you can use PASID xyz from FD abc' command.

The 'set' is per-VM. Once the mdev is assigned to a VM, all valid PASID's in the set of that VM are considered legitimate on this mdev.

No! That is a major security problem!

PASID authorization is *PER DEVICE*.

If I map a device into VFIO in userspace with full control over the HW that device MUST ONLY have access to PASID's that have been registered with vfio.

This means each time you register a PASID vfio must tell the IOMMU driver to authorize the pci_device to access the PASID, the vIOMMU driver must tell the hypervisor and the mdev under the PCI device MUST have a per-device list of allowed PASIDs.

Otherwise userspace in a VM with vfio could tell the mdev driver to talk to a PASID in the same VM but *that process doesn't own*. This is absolutely not allowed.

Most likely the entire ioasid set and related need to be deleted as a kernel concept.

Jason

#232 lore raw

Re: [PATCH V4 05/18] iommu/ioasid: Redefine IOASID set and allocation APIs

In reply to Tian, Kevin
Quoted 9 lines · Kevin

One correction. The mdev should still construct the list of allowed PASID's as you said (by listening to IOASID_BIND/UNBIND event), in addition to the ioasid set maintained per VM (updated when a PASID is allocated/freed). The per-VM set is required for inter-VM isolation (verified when a pgtable is bound to the mdev/PASID), while the mdev's own list is necessary for intra-VM isolation when multiple mdevs are assigned to the same VM (verified before loading a PASID to the mdev). This series just handles the general part i.e. per-VM ioasid set and leaves the mdev's own list to be managed by specific mdev driver which listens to various IOASID events).

This is better, but I don't understand why we need such a convoluted design.

Get rid of the ioasid set.

Each driver has its own list of allowed ioasids.

Register a ioasid in the driver's list by passing the fd and ioasid #

No listening to events. A simple understandable security model.

Look - it took you three emails to even correctly explain the security model you are striving for here, it is *obviously* too complicated for anyone to understand or successfully implement. simplify smiplify simplify.

Jason

#39 lore raw

Re: [PATCH V4 05/18] iommu/ioasid: Redefine IOASID set and allocation APIs

In reply to Tian, Kevin
Quoted 29 lines · Kevin
Quoted 17 lines

From: Jason Gunthorpe <[email protected]>
Sent: Tuesday, March 30, 2021 12:32 AM

Quoted 11 lines · Jacob Pan
Quoted 4 lines

IMHO a use created PASID is either bound to a mm (current) at creation time, or it will never be bound to a mm and its page table is under user control via /dev/ioasid.

True for PASID used in native SVA bind. But for binding with a guest mm, PASID is allocated first (VT-d virtual cmd interface Spec 10.4.44), the bind with the host IOMMU when vIOMMU PASID cache is invalidated.

Our intention is to have two separate interfaces:
1. /dev/ioasid (allocation/free only)
2. /dev/sva (handles all SVA related activities including page tables)

Quoted 5 lines

I'm not sure I understand why you'd want to have two things. Doesn't that just complicate everything?

Manipulating the ioasid, including filling it with page tables, seems an integral inseperable part of the whole interface. Why have two ?

Hi, Jason,

Actually above is a major open while we are refactoring vSVA uAPI toward this direction. We have two concerns about merging /dev/ioasid with /dev/sva, and would like to hear your thought whether they are valid.

Quoted 6 lines

First, userspace may use ioasid in a non-SVA scenario where ioasid is bound to specific security context (e.g. a control vq in vDPA) instead of tying to mm. In this case there is no pgtable binding initiated from user space. Instead, ioasid is allocated from /dev/ioasid and then programmed to the intended security context through specific passthrough framework which manages that context.

This sounds like the exact opposite of what I'd like to see.

I do not want to see every subsystem gaining APIs to program a PASID. All of that should be consolidated in *one place*.

I do not want to see VDPA and VFIO have two nearly identical sets of APIs to control the PASID.

Drivers consuming a PASID, like VDPA, should consume the PASID and do nothing more than authorize the HW to use it.

quemu should have general code under the viommu driver that drives /dev/ioasid to create PASID's and manage the IO mapping according to the guest's needs.

Drivers like VDPA and VFIO should simply accept that PASID and configure/authorize their HW to do DMA's with its tag.

Quoted 17 lines

Second, ioasid is managed per process/VM while pgtable binding is a device-wise operation. The userspace flow looks like below for an integral /dev/ioasid interface:

- ioctl(container->fd, VFIO_SET_IOMMU, VFIO_TYPE1_NESTING_IOMMU)
- ioasid_fd = open(/dev/ioasid)
- ioctl(ioasid_fd, IOASID_GET_USVA_FD, &sva_fd) //an empty context
- ioctl(device->fd, VFIO_DEVICE_SET_SVA, &sva_fd); //sva_fd ties to device
- ioctl(sva_fd, USVA_GET_INFO, &sva_info);
- ioctl(ioasid_fd, IOMMU_ALLOC_IOASID, &ioasid);
- ioctl(sva_fd, USVA_BIND_PGTBL, &bind_data);
- ioctl(sva_fd, USVA_FLUSH_CACHE, &inv_info);
- ioctl(sva_fd, USVA_UNBIND_PGTBL, &unbind_data);
- ioctl(device->fd, VFIO_DEVICE_UNSET_SVA, &sva_fd);
- close(sva_fd)
- close(ioasid_fd)

Quoted 3 lines

Our hesitation here is based on one of your earlier comments that you are not a fan of constructing fd's through ioctl. Are you OK with above flow or have a better idea of handling it?

My reaction is to squash 'sva' and ioasid fds together, I can't see why you'd need two fds to manipulate a PASID.

DEVICE_SET_SVA seems like the wrong language too, it should be more like DEVICE_ALLOW_IOASID which only tells the iommu and driver to alow the pci_device to use the IOASID.

Jason

#22 lore raw

Re: [PATCH V4 05/18] iommu/ioasid: Redefine IOASID set and allocation APIs

In reply to Jason Gunthorpe
Quoted 8 lines · Jason Gunthorpe
Quoted 3 lines · Jean-Philippe Brucker

It's not inconceivable to have a control queue doing DMA tagged with PASID. The devices I know either use untagged DMA, or have a choice to use a PASID.

I don't think we should encourage that. A PASID and all the related is so expensive compared to just doing normal untagged kernel DMA.

How is it expensive? Low number of PASIDs, or slowing down DMA transactions? PASIDs aren't a scarce resource on Arm systems, they have almost 1M unused PASIDs per VM.

Thanks,
Jean

Quoted 5 lines

I assume HW has these features because virtualization use cases might use them, eg by using mdev to assign a command queue - then it would need be be contained by a PASID.

Jason

#29 lore raw

Re: [PATCH V4 05/18] iommu/ioasid: Redefine IOASID set and allocation APIs

In reply to Jacob Pan
Quoted 4 lines · Jacob Pan

In one of the earlier discussions, I was made aware of some use cases (by AMD, iirc) where PASID can be used w/o IOMMU. That is why I tried to keep ioasid a separate subsystem. Other than that, I don't see an issue combining the two.

That sounds like nonsense. A freshly created ioasid should have *NO DMA*. Every access to it should result in a PCI error until a mapping for the address space is defined. It is called IO *address space* for a reason.

So, what exactly do you do with a PASID without an IOMMU? You certainly can't expose it through this interface because you can't establish the first requirement of *NO DMA*.

While there may be an interesting use case, it looks to be kernel-only and not relavent here.

Quoted 3 lines

it matters when it comes to which interface to choose. Use /dev/ioasid to allocate if PASID value cannot be hidden. Use some other interface for bind current and allocate if a PASID is not visible to the user.

I just view it as a shortcut, it has less to do with "hidden" and more to do if the shortcut is a valuable savings. If you swap four ioctls with one ioctl I'd say that is not enough of a win <shrug>

Quoted 4 lines

Yes, I think we are on the same page. For example, today's uacce or idxd driver creates a hidden PASID when user does open(), where a new WQ is provisioned and bound to current mm. This is the case where /dev/ioasid is not needed.

So that is a probelm for uacce, they shouldn't have created PASIDs at open() time, there is no option to customize what is happening there.

Quoted 4 lines

Sorry, I am not following. In the doc, I have an example to show the ioasid_set to VM/mm mapping. We use mm as the ioasid_set token to identify who the owner of an IOASID is. i.e. who allocated the IOASID. Non-owner cannot perform bind page table or free operations.

As I said to Kevin this seems very over complicated.

Access to the /dev/ioasid FD is the only authorization the kernel needs.

Quoted 10 lines

Yes, each PF/VF has its own PASID table. The device can do whatever it wants as long as the PASID is present in the table. Programming of the pIOMMU PASID table entry, however, is controlled by the host.

IMHO, there are two levels of security here:
1. A PASID can only be used by a secure context
2. A device can only use allowed PASIDs (PASID namespace is system-wide but PASID table storage is per PF/VF)

IOASID set is designed for #1.

#1 sounds like the mdev case, and as I said to Kevin each and every mdev needs its own allow'd PASID list. There is no need for an ioasid set to implement that.

Quoted 8 lines
Quoted 4 lines

If a device is sharing a single PCI function with different security contexts (eg vfio mdev) then the device itself is responsible to ensure that only the secure interface can program a PASID and a less secure context can never self-enroll.

If two mdevs from the same PF dev are assigned to two VMs, the PASID table will be shared. IOASID set ensures one VM cannot program another VM's PASIDs. I assume 'secure context' is per VM when it comes to host PASID.

No, the mdev device driver must enforce this directly. It is the one that programms the physical shared HW, it is the one that needs a list of PASID's it is allowed to program *for each mdev*

ioasid_set doesn't seem to help at all, certainly not as a concept tied to /dev/ioasid.

Quoted 3 lines

No. the mdev driver consults with IOASID core When the guest programs a guest PASID on to he mdev. VDCM driver does a lookup:
host_pasid = ioasid_find_by_spid(ioasid_set, guest_pasid);

This is the wrong layering. Tell the mdev device directly what it is allowed to do. Do not pollute the ioasid core with security stuff.

Quoted 7 lines
Quoted 4 lines

I'd say you shoul have a single /dev/ioasid per VM and KVM should attach to that - it should get all the global events/etc that are not device specific.

You mean a single /dev/ioasid FD per VM and KVM? I think that is what we are doing in this set. A VM process can only open /dev/ioasid once, then use the FD for allocation and pass the PASID for bind page table etc.

Yes, I think that is reasonable.

Tag all the IOCTL's with the IOASID number.

Quoted 7 lines
Quoted 3 lines

Not sure what guest-host PASID means, these have to be 1:1 for device assignment to work - why would use something else for mdev?

We have G-H PASID translation. They don't have to be 1:1.
IOASID Set Private ID (SPID) is intended as a generic solution for guest PASID. Could you review the secion Section: IOASID Set Private ID (SPID) in the doc patch?

Again this only works for MDEV? How would you do translation for a real PF/VF?

So when you 'allow' a mdev to access a PASID you want to say:
Allow Guest PASID A, map it to host PASID B on this /dev/ioasid FD

?

That seems like a good helper library to provide for drivers to use, but it should be a construct entirely contained in the driver.

Quoted 2 lines

We also had some slides from last year. Slide 3s-6 mostly.
https://static.sched.com/hosted_files/kvmforum2020/9f/KVM_forum_2020_PASID_MGMT_Yi_Jacob_final.pdf

I think you are trying to put too much into a giant ioasid core. Responsibility needs to rest in more logical places, it will simplify everything.

Jason

#23 lore raw

Re: [PATCH V4 05/18] iommu/ioasid: Redefine IOASID set and allocation APIs

In reply to Jean-Philippe Brucker
Quoted 7 lines · Jean-Philippe Brucker
Quoted 6 lines · Jason Gunthorpe
Quoted 3 lines · Jean-Philippe Brucker

It's not inconceivable to have a control queue doing DMA tagged with PASID. The devices I know either use untagged DMA, or have a choice to use a PASID.

Quoted 6 lines
Quoted 2 lines

I don't think we should encourage that. A PASID and all the related is so expensive compared to just doing normal untagged kernel DMA.

How is it expensive? Low number of PASIDs, or slowing down DMA transactions? PASIDs aren't a scarce resource on Arm systems, they have almost 1M unused PASIDs per VM.

There may be lots of PASIDs, but they are not without cost. The page table behind them costs memory and cache occupancy, doing the lookups hurts DMA performance.

Compare to a physical addressed kernel DMA (like x86 often sets up) the runtime overheads from unnecessary PASID use is quite big.

Jason

author
#30 lore raw

Re: [PATCH V4 05/18] iommu/ioasid: Redefine IOASID set and allocation APIs

In reply to Jason Gunthorpe

Hi Jason,

Quoted 9 lines · Jason Gunthorpe
Quoted 4 lines

If two mdevs from the same PF dev are assigned to two VMs, the PASID table will be shared. IOASID set ensures one VM cannot program another VM's PASIDs. I assume 'secure context' is per VM when it comes to host PASID.

No, the mdev device driver must enforce this directly. It is the one that programms the physical shared HW, it is the one that needs a list of PASID's it is allowed to program *for each mdev*

This requires the mdev driver to obtain a list of allowed PASIDs(possibly during PASID bind time) prior to do enforcement. IMHO, the PASID enforcement points are:
1. During WQ configuration (e.g.program MSI)
2. During work submission

For VT-d shared workqueue, there is no way to enforce #2 in mdev driver in that the PASID is obtained from PASID MSR from the CPU and submitted w/o driver involvement. The enforcement for #2 is in the KVM PASID translation table, which is per VM.

For our current VFIO mdev model, bind guest page table does not involve mdev driver. So this is a gap we must fill, i.e. include a callback from mdev driver?

Quoted 3 lines

ioasid_set doesn't seem to help at all, certainly not as a concept tied to /dev/ioasid.

Yes, we can take the security role off ioasid_set once we have per mdev list. However, ioasid_set being a per VM/mm entity also bridge communications among kernel subsystems that don't have direct call path. e.g. KVM, VDCM and IOMMU.

Quoted 22 lines
Quoted 3 lines

No. the mdev driver consults with IOASID core When the guest programs a guest PASID on to he mdev. VDCM driver does a lookup:
host_pasid = ioasid_find_by_spid(ioasid_set, guest_pasid);

This is the wrong layering. Tell the mdev device directly what it is allowed to do. Do not pollute the ioasid core with security stuff.

Quoted 7 lines
Quoted 4 lines

I'd say you shoul have a single /dev/ioasid per VM and KVM should attach to that - it should get all the global events/etc that are not device specific.

You mean a single /dev/ioasid FD per VM and KVM? I think that is what we are doing in this set. A VM process can only open /dev/ioasid once, then use the FD for allocation and pass the PASID for bind page table etc.

Yes, I think that is reasonable.

Tag all the IOCTL's with the IOASID number.

Quoted 3 lines
Quoted 3 lines

Not sure what guest-host PASID means, these have to be 1:1 for device assignment to work - why would use something else for mdev?

Quoted 8 lines
Quoted 4 lines

We have G-H PASID translation. They don't have to be 1:1. IOASID Set Private ID (SPID) is intended as a generic solution for guest PASID. Could you review the secion Section: IOASID Set Private ID (SPID) in the doc patch?

Again this only works for MDEV? How would you do translation for a real PF/VF?

Right, we will need some mediation for PF/VF.

Quoted 5 lines

So when you 'allow' a mdev to access a PASID you want to say:
Allow Guest PASID A, map it to host PASID B on this /dev/ioasid FD

?

Host and guest PASID value, as well as device info are available through iommu_uapi_sva_bind_gpasid(), we just need to feed that info to mdev driver.

Quoted 2 lines

That seems like a good helper library to provide for drivers to use, but it should be a construct entirely contained in the driver.

why? would it be cleaner if it is in the common code?

Thanks,

Jacob
_______________________________________________
iommu mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/iommu

#40 lore raw

RE: [PATCH V4 05/18] iommu/ioasid: Redefine IOASID set and allocation APIs

In reply to Jason Gunthorpe

Hi Jason,

Quoted 4 lines

From: Jason Gunthorpe <[email protected]>
Sent: Tuesday, March 30, 2021 9:29 PM

On Tue, Mar 30, 2021 at 01:37:05AM +0000, Tian, Kevin wrote:

[...]

Quoted 49 lines
Quoted 6 lines

Hi, Jason,

Actually above is a major open while we are refactoring vSVA uAPI toward this direction. We have two concerns about merging /dev/ioasid with /dev/sva, and would like to hear your thought whether they are valid.

Quoted 6 lines

First, userspace may use ioasid in a non-SVA scenario where ioasid is bound to specific security context (e.g. a control vq in vDPA) instead of tying to mm. In this case there is no pgtable binding initiated from user space. Instead, ioasid is allocated from /dev/ioasid and then programmed to the intended security context through specific passthrough framework which manages that context.

This sounds like the exact opposite of what I'd like to see.

I do not want to see every subsystem gaining APIs to program a PASID. All of that should be consolidated in *one place*.

I do not want to see VDPA and VFIO have two nearly identical sets of APIs to control the PASID.

Drivers consuming a PASID, like VDPA, should consume the PASID and do nothing more than authorize the HW to use it.

quemu should have general code under the viommu driver that drives /dev/ioasid to create PASID's and manage the IO mapping according to the guest's needs.

Drivers like VDPA and VFIO should simply accept that PASID and configure/authorize their HW to do DMA's with its tag.

Quoted 8 lines

Second, ioasid is managed per process/VM while pgtable binding is a device-wise operation. The userspace flow looks like below for an integral /dev/ioasid interface:

- ioctl(container->fd, VFIO_SET_IOMMU, VFIO_TYPE1_NESTING_IOMMU)
- ioasid_fd = open(/dev/ioasid)
- ioctl(ioasid_fd, IOASID_GET_USVA_FD, &sva_fd) //an empty context
- ioctl(device->fd, VFIO_DEVICE_SET_SVA, &sva_fd); //sva_fd ties to

device

Quoted 9 lines

- ioctl(sva_fd, USVA_GET_INFO, &sva_info);
- ioctl(ioasid_fd, IOMMU_ALLOC_IOASID, &ioasid);
- ioctl(sva_fd, USVA_BIND_PGTBL, &bind_data);
- ioctl(sva_fd, USVA_FLUSH_CACHE, &inv_info);
- ioctl(sva_fd, USVA_UNBIND_PGTBL, &unbind_data);
- ioctl(device->fd, VFIO_DEVICE_UNSET_SVA, &sva_fd);
- close(sva_fd)
- close(ioasid_fd)

Quoted 6 lines
Quoted 3 lines

Our hesitation here is based on one of your earlier comments that you are not a fan of constructing fd's through ioctl. Are you OK with above flow or have a better idea of handling it?

My reaction is to squash 'sva' and ioasid fds together, I can't see why you'd need two fds to manipulate a PASID.

The reason is /dev/ioasid FD is per-VM since the ioasid allocated to the VM should be able to be shared by all assigned device for the VM. But the SVA operations (bind/unbind page table, cache_invalidate) should be per-device. If squashing the two fds to be one, then requires a device tag for each vSVA ioctl. I'm not sure if it is good. Per me, it looks better to have a SVA FD and associated with a device FD so that any ioctl on it will be in the device level. This also benefits ARM and AMD's vSVA support since they binds guest PASID table to host instead of binding guest page tables to specific PASIDs.

Regards,
Yi Liu

#233 lore raw

RE: [PATCH V4 05/18] iommu/ioasid: Redefine IOASID set and allocation APIs

In reply to Jason Gunthorpe
Quoted 21 lines

From: Jason Gunthorpe <[email protected]>
Sent: Tuesday, March 30, 2021 9:28 PM

Quoted 1 line · Kevin

One correction. The mdev should still construct the list of allowed PASID's

as

Quoted 1 line

you said (by listening to IOASID_BIND/UNBIND event), in addition to the

ioasid

Quoted 1 line

set maintained per VM (updated when a PASID is allocated/freed). The

per-VM

Quoted 1 line

set is required for inter-VM isolation (verified when a pgtable is bound to

the

Quoted 1 line

mdev/PASID), while the mdev's own list is necessary for intra-VM isolation

when

Quoted 1 line

multiple mdevs are assigned to the same VM (verified before loading a

PASID

Quoted 1 line

to the mdev). This series just handles the general part i.e. per-VM ioasid

set and

Quoted 1 line

leaves the mdev's own list to be managed by specific mdev driver which

listens

Quoted 8 lines
Quoted 1 line

to various IOASID events).

This is better, but I don't understand why we need such a convoluted design.

Get rid of the ioasid set.

Each driver has its own list of allowed ioasids.

First, I agree with you it's necessary to have a per-device allowed ioasid list. But besides it, I think we still need to ensure the ioasid used by a VM is really allocated to this VM. A VM should not use an ioasid allocated to another VM. right? Actually, this is the major intention for introducing ioasid_set.

Quoted 1 line

Register a ioasid in the driver's list by passing the fd and ioasid #

The fd here is a device fd. Am I right? If yes, your idea is ioasid is allocated via /dev/ioasid and associated with device fd via either VFIO or vDPA ioctl. right? sorry I may be asking silly questions but really need to ensure we are talking in the same page.

Quoted 1 line

No listening to events. A simple understandable security model.

For this suggestion, I have a little bit concern if we may have A-B/B-A lock sequence issue since it requires the /dev/ioasid (if it supports) to call back into VFIO/VDPA to check if the ioasid has been registered to device FD and record it in the per-device list. right? Let's have more discussion based on the skeleton sent by Kevin.

Regards,
Yi Liu

#37 lore raw

RE: [PATCH V4 05/18] iommu/ioasid: Redefine IOASID set and allocation APIs

In reply to Jason Gunthorpe
Quoted 2 lines

From: Jason Gunthorpe <[email protected]>
Sent: Tuesday, March 30, 2021 9:43 PM

[..]

Quoted 7 lines

No, the mdev device driver must enforce this directly. It is the one that programms the physical shared HW, it is the one that needs a list of PASID's it is allowed to program *for each mdev*

ioasid_set doesn't seem to help at all, certainly not as a concept tied to /dev/ioasid.

As replied in another thread. We introduced ioasid_set based on the motivation to have per-VM ioasid track, which is required when user space tries to bind an ioasid with a device. Should ensure the ioasid it is using was allocated to it. otherwise, we may suffer inter-VM ioasid problem. It may not necessaty to be ioasid_set but a per-VM ioasid track is necessary.

Regards,
Yi Liu

#31 lore raw

Re: [PATCH V4 05/18] iommu/ioasid: Redefine IOASID set and allocation APIs

In reply to Jacob Pan
Quoted 19 lines · Jacob Pan

Hi Jason,

Quoted 9 lines · Jason Gunthorpe
Quoted 4 lines

If two mdevs from the same PF dev are assigned to two VMs, the PASID table will be shared. IOASID set ensures one VM cannot program another VM's PASIDs. I assume 'secure context' is per VM when it comes to host PASID.

No, the mdev device driver must enforce this directly. It is the one that programms the physical shared HW, it is the one that needs a list of PASID's it is allowed to program *for each mdev*

This requires the mdev driver to obtain a list of allowed PASIDs(possibly during PASID bind time) prior to do enforcement. IMHO, the PASID enforcement points are:
1. During WQ configuration (e.g.program MSI)
2. During work submission

Quoted 3 lines

For VT-d shared workqueue, there is no way to enforce #2 in mdev driver in that the PASID is obtained from PASID MSR from the CPU and submitted w/o driver involvement.

I assume that the PASID MSR is privileged and only qemu can program it? Otherwise this seems like a security problem.

If qemu controls it then the idxd userspace driver in qemu must ensure it is only ever programmed to an authorized PASID.

Quoted 2 lines

The enforcement for #2 is in the KVM PASID translation table, which is per VM.

I don't understand why KVM gets involved in PASID??

Doesn't work submission go either to the mdev driver or through the secure PASID of #1?

Quoted 3 lines

For our current VFIO mdev model, bind guest page table does not involve mdev driver. So this is a gap we must fill, i.e. include a callback from mdev driver?

No not a callback, tell the mdev driver with a VFIO IOCTL that it is authorized to use a specific PASID because the vIOMMU was told to allow it by the guest kernel. Simple and straightforward.

Quoted 7 lines
Quoted 3 lines

ioasid_set doesn't seem to help at all, certainly not as a concept tied to /dev/ioasid.

Yes, we can take the security role off ioasid_set once we have per mdev list. However, ioasid_set being a per VM/mm entity also bridge communications among kernel subsystems that don't have direct call path. e.g. KVM, VDCM and IOMMU.

Everything should revolve around the /dev/ioasid FD. qemu should pass it to all places that need to know about PASID's in the VM.

We should try to avoid hidden behind the scenes kernel interconnections between subsystems.

Quoted 6 lines
Quoted 3 lines

So when you 'allow' a mdev to access a PASID you want to say:
Allow Guest PASID A, map it to host PASID B on this /dev/ioasid FD

Host and guest PASID value, as well as device info are available through iommu_uapi_sva_bind_gpasid(), we just need to feed that info to mdev driver.

You need that IOCTL to exist on the *mdev driver*. It is a VFIO ioctl, not a iommu or ioasid or sva IOCTL.

Quoted 3 lines
Quoted 2 lines

That seems like a good helper library to provide for drivers to use, but it should be a construct entirely contained in the driver.

why? would it be cleaner if it is in the common code?

No, it is the "mid layer" problematic design.

Having the iommu layer store driver-specific data on behalf of a driver will just make a mess. Use the natural layering we have and store driver specific data in the driver structs.

Add a library to help build the datastructure if it necessary.

Jason

#234 lore raw

Re: [PATCH V4 05/18] iommu/ioasid: Redefine IOASID set and allocation APIs

In reply to Liu, Yi L
Quoted 28 lines · Yi L
Quoted 28 lines

From: Jason Gunthorpe <[email protected]>
Sent: Tuesday, March 30, 2021 9:28 PM

Quoted 1 line · Kevin

One correction. The mdev should still construct the list of allowed PASID's

as

Quoted 1 line

you said (by listening to IOASID_BIND/UNBIND event), in addition to the

ioasid

Quoted 1 line

set maintained per VM (updated when a PASID is allocated/freed). The

per-VM

Quoted 1 line

set is required for inter-VM isolation (verified when a pgtable is bound to

the

Quoted 1 line

mdev/PASID), while the mdev's own list is necessary for intra-VM isolation

when

Quoted 1 line

multiple mdevs are assigned to the same VM (verified before loading a

PASID

Quoted 1 line

to the mdev). This series just handles the general part i.e. per-VM ioasid

set and

Quoted 1 line

leaves the mdev's own list to be managed by specific mdev driver which

listens

Quoted 1 line

to various IOASID events).

This is better, but I don't understand why we need such a convoluted design.

Get rid of the ioasid set.

Quoted 7 lines
Quoted 1 line

Each driver has its own list of allowed ioasids.

First, I agree with you it's necessary to have a per-device allowed ioasid list. But besides it, I think we still need to ensure the ioasid used by a VM is really allocated to this VM. A VM should not use an ioasid allocated to another VM. right? Actually, this is the major intention for introducing ioasid_set.

The /dev/ioasid FD replaces this security check. By becoming FD centric you don't need additional kernel security objects.

Any process with access to the /dev/ioasid FD is allowed to control those PASID. The seperation between VMs falls naturally from the seperation of FDs without creating additional, complicated, security infrastrucure in the kernel.

This is why all APIs must be FD focused, and you need to have a logical layering of responsibility.

Allocate a /dev/ioasid FD
Allocate PASIDs inside the FD
Assign memory to the PASIDS

Open a device FD, eg from VFIO or VDP
Instruct the device FD to authorize the device to access PASID A in an ioasid FD
* Prior to being authorized the device will have NO access to any PASID
* Presenting BOTH the device FD and the ioasid FD to the kernel is the security check. Any process with both FDs is allowed to make the connection. This is normal Unix FD centric thinking.

Quoted 3 lines
Quoted 1 line

Register a ioasid in the driver's list by passing the fd and ioasid #

The fd here is a device fd. Am I right?

It would be the vfio_device FD, for instance, and a VFIO IOCTL.

Quoted 4 lines

If yes, your idea is ioasid is allocated via /dev/ioasid and associated with device fd via either VFIO or vDPA ioctl. right? sorry I may be asking silly questions but really need to ensure we are talking in the same page.

Yes, this is right

Quoted 7 lines
Quoted 1 line

No listening to events. A simple understandable security model.

For this suggestion, I have a little bit concern if we may have A-B/B-A lock sequence issue since it requires the /dev/ioasid (if it supports) to call back into VFIO/VDPA to check if the ioasid has been registered to device FD and record it in the per-device list. right? Let's have more discussion based on the skeleton sent by Kevin.

Callbacks would be backwards.

User calls vfio with vfio_device fd and dev/ioasid fd

VFIO extracts some kernel representation of the ioasid from the ioasid fd using an API

VFIO does some kernel call to IOMMU/IOASID layer that says 'tell the IOMMU that this PCI device is allowed to use this PASID'

VFIO mdev drivers then record that the PASID is allowed in its own device specific struct for later checking during other system calls.

No lock inversions. No callbacks. Why do we need callbacks?? Simplify.

Jason

#41 lore raw

Re: [PATCH V4 05/18] iommu/ioasid: Redefine IOASID set and allocation APIs

In reply to Liu, Yi L
Quoted 4 lines · Yi L

The reason is /dev/ioasid FD is per-VM since the ioasid allocated to the VM should be able to be shared by all assigned device for the VM. But the SVA operations (bind/unbind page table, cache_invalidate) should be per-device.

It is not *per-device* it is *per-ioasid*

And as /dev/ioasid is an interface for controlling multiple ioasid's there is no issue to also multiplex the page table manipulation for multiple ioasids as well.

What you should do next is sketch out in some RFC the exactl ioctls each FD would have and show how the parts I outlined would work and point out any remaining gaps.

The device FD is something like the vfio_device FD from VFIO, it has *nothing* to do with PASID beyond having a single ioctl to authorize the device to use the PASID. All control of the PASID is in /dev/ioasid.

Jason

author
#32 lore raw

Re: [PATCH V4 05/18] iommu/ioasid: Redefine IOASID set and allocation APIs

In reply to Jason Gunthorpe

Hi Jason,

Quoted 10 lines · Jason Gunthorpe

On Tue, Mar 30, 2021 at 05:10:41PM -0700, Jacob Pan wrote:
[...]
[...]
[...]

Quoted 6 lines

This requires the mdev driver to obtain a list of allowed PASIDs(possibly during PASID bind time) prior to do enforcement. IMHO, the PASID enforcement points are:
1. During WQ configuration (e.g.program MSI)
2. During work submission

Quoted 7 lines
Quoted 3 lines

For VT-d shared workqueue, there is no way to enforce #2 in mdev driver in that the PASID is obtained from PASID MSR from the CPU and submitted w/o driver involvement.

I assume that the PASID MSR is privileged and only qemu can program it? Otherwise this seems like a security problem.

yes.

Quoted 3 lines

If qemu controls it then the idxd userspace driver in qemu must ensure it is only ever programmed to an authorized PASID.

it is ensured for #1.

Quoted 5 lines
Quoted 2 lines

The enforcement for #2 is in the KVM PASID translation table, which is per VM.

I don't understand why KVM gets involved in PASID??

Here is an excerpt from the SIOV spec.
https://software.intel.com/content/www/us/en/develop/download/intel-scalable-io-virtualization-technical-specification.html

"3.3 PASID translation
To support PASID isolation for Shared Work Queues used by VMs, the CPU must provide a way for the PASID to be communicated to the device in the DMWr transaction. On Intel CPUs, the CPU provides a PASID translation table in the vCPUs virtual machine control structures. During ENQCMD/ENQCMDS instruction execution in a VM, the PASID translation table is used by the CPU to replace the guest PASID in the work descriptor with a host PASID before the descriptor is sent to the device.3.3 PASID translation"

Quoted 3 lines

Doesn't work submission go either to the mdev driver or through the secure PASID of #1?

No, once a PASID is bound with IOMMU, KVM, and the mdev, work submission is all done in HW.
But I don't think this will change for either uAPI design.

Quoted 8 lines
Quoted 3 lines

For our current VFIO mdev model, bind guest page table does not involve mdev driver. So this is a gap we must fill, i.e. include a callback from mdev driver?

No not a callback, tell the mdev driver with a VFIO IOCTL that it is authorized to use a specific PASID because the vIOMMU was told to allow it by the guest kernel. Simple and straightforward.

Make sense.

Quoted 11 lines
Quoted 7 lines
Quoted 3 lines

ioasid_set doesn't seem to help at all, certainly not as a concept tied to /dev/ioasid.

Yes, we can take the security role off ioasid_set once we have per mdev list. However, ioasid_set being a per VM/mm entity also bridge communications among kernel subsystems that don't have direct call path. e.g. KVM, VDCM and IOMMU.

Everything should revolve around the /dev/ioasid FD. qemu should pass it to all places that need to know about PASID's in the VM.

I guess we need to extend KVM interface to support PASIDs. Our original intention was to avoid introducing new interfaces.

Quoted 3 lines

We should try to avoid hidden behind the scenes kernel interconnections between subsystems.

Can we? in case of exception. Since all these IOCTLs are coming from the unreliable user space, we must deal all exceptions.

For example, when user closes /dev/ioasid FD before (or w/o) unbind IOCTL for VFIO, KVM, kernel must do cleanup and coordinate among subsystems. In this patchset, we have a per mm(ioasid_set) notifier to inform mdev, KVM to clean up and drop its refcount. Do you have any suggestion on this?

Quoted 5 lines
Quoted 4 lines
Quoted 4 lines

So when you 'allow' a mdev to access a PASID you want to say:
Allow Guest PASID A, map it to host PASID B on this /dev/ioasid FD

Quoted 7 lines
Quoted 3 lines

Host and guest PASID value, as well as device info are available through iommu_uapi_sva_bind_gpasid(), we just need to feed that info to mdev driver.

You need that IOCTL to exist on the *mdev driver*. It is a VFIO ioctl, not a iommu or ioasid or sva IOCTL.

OK. A separate IOCTL and separate step.

Quoted 4 lines
Quoted 3 lines
Quoted 2 lines

That seems like a good helper library to provide for drivers to use, but it should be a construct entirely contained in the driver.

why? would it be cleaner if it is in the common code?

Quoted 8 lines

No, it is the "mid layer" problematic design.

Having the iommu layer store driver-specific data on behalf of a driver will just make a mess. Use the natural layering we have and store driver specific data in the driver structs.

Add a library to help build the datastructure if it necessary.

Let me try to paraphrase, you are suggesting common helper code and data format but still driver specific storage of the mapping, correct?

Will try this out, seems cleaner.

Quoted 1 line

Jason

Thanks,

Jacob
_______________________________________________
iommu mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/iommu

#33 lore raw

Re: [PATCH V4 05/18] iommu/ioasid: Redefine IOASID set and allocation APIs

Quoted 8 lines · Jacob Pan

"3.3 PASID translation
To support PASID isolation for Shared Work Queues used by VMs, the CPU must provide a way for the PASID to be communicated to the device in the DMWr transaction. On Intel CPUs, the CPU provides a PASID translation table in the vCPUs virtual machine control structures. During ENQCMD/ENQCMDS instruction execution in a VM, the PASID translation table is used by the CPU to replace the guest PASID in the work descriptor with a host PASID before the descriptor is sent to the device.3.3 PASID translation"

Yikes, a special ENQCMD table in the hypervisor!

Still, pass the /dev/ioasid into a KVM IOCTL and tell it to populate this table. KVM only adds to the table when userspace presents a /dev/ioasid FD.

Quoted 6 lines
Quoted 2 lines

Doesn't work submission go either to the mdev driver or through the secure PASID of #1?

No, once a PASID is bound with IOMMU, KVM, and the mdev, work submission is all done in HW. But I don't think this will change for either uAPI design.

The big note here is "only for things that use ENQCMD" and that is basically nothing these days.

Quoted 5 lines
Quoted 2 lines

Everything should revolve around the /dev/ioasid FD. qemu should pass it to all places that need to know about PASID's in the VM.

I guess we need to extend KVM interface to support PASIDs. Our original intention was to avoid introducing new interfaces.

New features need new interfaces, especially if there is a security sensitivity! KVM should *not* automatically opt into security sensitive stuff without being explicitly told what to do.

Here you'd need to authorized *two* things for IDXD:
- The mdev needs to be told it is allowed to use PASID, this tells the IOMMU driver to connect the pci device under the mdev
- KVM needs to be told to populate a vPASID to the 'ENQCMD' security table translated to a physical PASID.

If qemu doesn't explicitly enable the ENQCMD security table it should be *left disabled* by KVM - even if someone else is using PASID in the same process. And the API should be narrow like this just to the EQNCMD table as who knows what will come down the road, or how it will work.

Having a PASID wrongly leak out into the VM would be a security disaster. Be explicit.

Quoted 10 lines
Quoted 3 lines

We should try to avoid hidden behind the scenes kernel interconnections between subsystems.

Can we? in case of exception. Since all these IOCTLs are coming from the unreliable user space, we must deal all exceptions.

For example, when user closes /dev/ioasid FD before (or w/o) unbind IOCTL for VFIO, KVM, kernel must do cleanup and coordinate among subsystems. In this patchset, we have a per mm(ioasid_set) notifier to inform mdev, KVM to clean up and drop its refcount. Do you have any suggestion on this?

The ioasid should be a reference counted object.

When the FD is closed, or the ioasid is "destroyed" it just blocks DMA and parks the PASID until *all* places release it. Upon a zero refcount the PASID is recycled for future use.

The duration between unmapping the ioasid and releasing all HW access will have HW see PCIE TLP errors due to the blocked access. If userspace messes up the order it is fine to cause this. We already had this dicussion when talking about how to deal with process exit in the simple SVA case.

Quoted 2 lines

Let me try to paraphrase, you are suggesting common helper code and data format but still driver specific storage of the mapping, correct?

The driver just needs to hold the datastructure in its memory.

Like an xarray, the driver can have an xarray inside its struct device, but the xarray library provides all the implementation.

Jason

author
#34 lore raw

Re: [PATCH V4 05/18] iommu/ioasid: Redefine IOASID set and allocation APIs

Hi Jason,

Quoted 6 lines · Jason Gunthorpe
Quoted 6 lines
Quoted 3 lines

We should try to avoid hidden behind the scenes kernel interconnections between subsystems.

Can we? in case of exception. Since all these IOCTLs are coming from the unreliable user space, we must deal all exceptions.

Quoted 8 lines
Quoted 5 lines

For example, when user closes /dev/ioasid FD before (or w/o) unbind IOCTL for VFIO, KVM, kernel must do cleanup and coordinate among subsystems. In this patchset, we have a per mm(ioasid_set) notifier to inform mdev, KVM to clean up and drop its refcount. Do you have any suggestion on this?

The ioasid should be a reference counted object.

yes, this is done in this patchset.

Quoted 4 lines

When the FD is closed, or the ioasid is "destroyed" it just blocks DMA and parks the PASID until *all* places release it. Upon a zero refcount the PASID is recycled for future use.

Just to clarify, you are saying (when FREE happens before proper teardown) there is no need to proactively notify all users of the IOASID to drop their reference. Instead, just wait for the other parties to naturally close and drop their references. Am I understanding you correctly?

I feel having the notifications can add two values:
1. Shorten the duration of errors (as you mentioned below), FD close can take a long and unpredictable time. e.g. FD shared.
2. Provide teardown ordering among PASID users. i.e. vCPU, IOMMU, mdev.

Quoted 5 lines

The duration between unmapping the ioasid and releasing all HW access will have HW see PCIE TLP errors due to the blocked access. If userspace messes up the order it is fine to cause this. We already had this dicussion when talking about how to deal with process exit in the simple SVA case.

Yes, we have disabled fault reporting during this period. The slight differences vs. the simple SVA case is that KVM is also involved and there might be an ordering requirement to stop vCPU first.

Thanks,

Jacob
_______________________________________________
iommu mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/iommu

#35 lore raw

Re: [PATCH V4 05/18] iommu/ioasid: Redefine IOASID set and allocation APIs

Quoted 20 lines · Jacob Pan

Hi Jason,

Quoted 6 lines · Jason Gunthorpe
Quoted 6 lines
Quoted 3 lines

We should try to avoid hidden behind the scenes kernel interconnections between subsystems.

Can we? in case of exception. Since all these IOCTLs are coming from the unreliable user space, we must deal all exceptions.

Quoted 8 lines
Quoted 5 lines

For example, when user closes /dev/ioasid FD before (or w/o) unbind IOCTL for VFIO, KVM, kernel must do cleanup and coordinate among subsystems. In this patchset, we have a per mm(ioasid_set) notifier to inform mdev, KVM to clean up and drop its refcount. Do you have any suggestion on this?

The ioasid should be a reference counted object.

yes, this is done in this patchset.

Quoted 8 lines
Quoted 4 lines

When the FD is closed, or the ioasid is "destroyed" it just blocks DMA and parks the PASID until *all* places release it. Upon a zero refcount the PASID is recycled for future use.

Just to clarify, you are saying (when FREE happens before proper teardown) there is no need to proactively notify all users of the IOASID to drop their reference. Instead, just wait for the other parties to naturally close and drop their references. Am I understanding you correctly?

Yes. What are receivers going to do when you notify them anyhow? What will a mdev do? This is how you get into they crazy locking problems.

It is an error for userspace to shutdown like this, recover sensibly and don't crash the kernel. PCIe error TLPs are expected, supress them. That is what we decided on the mmu notifier discussion.

Quoted 3 lines

I feel having the notifications can add two values:
1. Shorten the duration of errors (as you mentioned below), FD close can take a long and unpredictable time. e.g. FD shared.

Only if userspace exits in some uncontrolled way. In a controlled exit it can close all the FDs in the right order.

It is OK if userspace does something weird and ends up with disabled IOASIDs. It shouldn't do that if it cares.

Quoted 1 line

2. Provide teardown ordering among PASID users. i.e. vCPU, IOMMU, mdev.

This is a hard ask too, there is no natural ordering here I can see, obviously we want vcpu, mdev, iommu for qemu but that doesn't seem to fall out unless we explicitly hard wire it into the kernel.

Doesn't kvm always kill the vCPU first based on the mmu notifier shooting down all the memory? IIRC this happens before FD close?

Quoted 8 lines
Quoted 5 lines

The duration between unmapping the ioasid and releasing all HW access will have HW see PCIE TLP errors due to the blocked access. If userspace messes up the order it is fine to cause this. We already had this dicussion when talking about how to deal with process exit in the simple SVA case.

Yes, we have disabled fault reporting during this period. The slight differences vs. the simple SVA case is that KVM is also involved and there might be an ordering requirement to stop vCPU first.

KVM can continue to use the PASIDs, they are parked and DMA is permanently blocked. When KVM reaches a natural point in its teardown it can release them.

If you have to stop the vcpu from a iommu notifier you are in the crazy locking world I mentioned. IMHO don't create exciting locking problems just to avoid PCI errors in uncontrolled shutdown.

Suppress the errors instead.

Jason

author
#36 lore raw

Re: [PATCH V4 05/18] iommu/ioasid: Redefine IOASID set and allocation APIs

Hi Jason,

Quoted 13 lines · Jason Gunthorpe
Quoted 7 lines · Jacob Pan

Hi Jason,

Quoted 3 lines · Jason Gunthorpe
Quoted 3 lines
Quoted 3 lines

We should try to avoid hidden behind the scenes kernel interconnections between subsystems.

[...]
[...]

Quoted 2 lines

yes, this is done in this patchset.

[...]

Quoted 9 lines
Quoted 5 lines

Just to clarify, you are saying (when FREE happens before proper teardown) there is no need to proactively notify all users of the IOASID to drop their reference. Instead, just wait for the other parties to naturally close and drop their references. Am I understanding you correctly?

Yes. What are receivers going to do when you notify them anyhow? What will a mdev do? This is how you get into they crazy locking problems.

The receivers perform cleanup work similar to normal unbind. Drain/Abort PASID. Locking is an issue in that the atomic notifier is under IOASID spinlock, so I provided a common ordered workqueue to let mdev drivers queue cleanup work that cannot be done in atomic context. Not ideal. Also need to prevent nested notifications for certain cases.

Quoted 8 lines

It is an error for userspace to shutdown like this, recover sensibly and don't crash the kernel. PCIe error TLPs are expected, supress them. That is what we decided on the mmu notifier discussion.

Quoted 3 lines

I feel having the notifications can add two values:
1. Shorten the duration of errors (as you mentioned below), FD close can take a long and unpredictable time. e.g. FD shared.

Quoted 6 lines

Only if userspace exits in some uncontrolled way. In a controlled exit it can close all the FDs in the right order.

It is OK if userspace does something weird and ends up with disabled IOASIDs. It shouldn't do that if it cares.

Agreed.

Quoted 7 lines
Quoted 2 lines

2. Provide teardown ordering among PASID users. i.e. vCPU, IOMMU, mdev.

This is a hard ask too, there is no natural ordering here I can see, obviously we want vcpu, mdev, iommu for qemu but that doesn't seem to fall out unless we explicitly hard wire it into the kernel.

The ordering problem as I understood is that it is difficult for KVM to rendezvous all vCPUs before updating PASID translation table. So there could be in-flight enqcmd with the stale PASID after the PASID table update and refcount drop.

If KVM is the last one to drop the PASID refcount, the PASID could be immediately reused and starts a new life. The in-flight enqcmd with the stale PASID could cause problems. The likelihood and window is very small.

If we ensure KVM does PASID table update before IOMMU and mdev driver, the stale PASID in the in-flight enqcmd would be be drained before starting a new life.

Perhaps Yi and Kevin can explain this better.

Quoted 3 lines

Doesn't kvm always kill the vCPU first based on the mmu notifier shooting down all the memory? IIRC this happens before FD close?

I don't know the answer, Kevin & Yi?

Quoted 13 lines
Quoted 8 lines
Quoted 5 lines

The duration between unmapping the ioasid and releasing all HW access will have HW see PCIE TLP errors due to the blocked access. If userspace messes up the order it is fine to cause this. We already had this dicussion when talking about how to deal with process exit in the simple SVA case.

Yes, we have disabled fault reporting during this period. The slight differences vs. the simple SVA case is that KVM is also involved and there might be an ordering requirement to stop vCPU first.

KVM can continue to use the PASIDs, they are parked and DMA is permanently blocked. When KVM reaches a natural point in its teardown it can release them.

Quoted 6 lines

If you have to stop the vcpu from a iommu notifier you are in the crazy locking world I mentioned. IMHO don't create exciting locking problems just to avoid PCI errors in uncontrolled shutdown.

Suppress the errors instead.

I agree, this simplify things a lot. Just need to clarify the in-flight enqcmd case.

Quoted 1 line

Jason

Thanks,

Jacob
_______________________________________________
iommu mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/iommu

author
#235 lore raw

Re: [PATCH V4 05/18] iommu/ioasid: Redefine IOASID set and allocation APIs

In reply to Jason Gunthorpe

Hi Jason,

Quoted 16 lines · Jason Gunthorpe
Quoted 3 lines
Quoted 3 lines

Get rid of the ioasid set.

Each driver has its own list of allowed ioasids.

[...]

The /dev/ioasid FD replaces this security check. By becoming FD centric you don't need additional kernel security objects.

Any process with access to the /dev/ioasid FD is allowed to control those PASID. The seperation between VMs falls naturally from the seperation of FDs without creating additional, complicated, security infrastrucure in the kernel.

This is why all APIs must be FD focused, and you need to have a logical layering of responsibility.

Quoted 7 lines

Allocate a /dev/ioasid FD
Allocate PASIDs inside the FD
Assign memory to the PASIDS

Open a device FD, eg from VFIO or VDP
Instruct the device FD to authorize the device to access PASID A in an ioasid FD

How do we know user provided PASID A was allocated by the ioasid FD? Shouldn't we validate user input by tracking which PASIDs are allocated by which ioasid FD? This is one reason why we have ioasid_set and its xarray.

Quoted 28 lines

* Prior to being authorized the device will have NO access to any PASID
* Presenting BOTH the device FD and the ioasid FD to the kernel is the security check. Any process with both FDs is allowed to make the connection. This is normal Unix FD centric thinking.

Quoted 4 lines
Quoted 2 lines

Register a ioasid in the driver's list by passing the fd and ioasid #

The fd here is a device fd. Am I right?

It would be the vfio_device FD, for instance, and a VFIO IOCTL.

Quoted 4 lines

If yes, your idea is ioasid is allocated via /dev/ioasid and associated with device fd via either VFIO or vDPA ioctl. right? sorry I may be asking silly questions but really need to ensure we are talking in the same page.

Yes, this is right

Quoted 7 lines
Quoted 1 line

No listening to events. A simple understandable security model.

For this suggestion, I have a little bit concern if we may have A-B/B-A lock sequence issue since it requires the /dev/ioasid (if it supports) to call back into VFIO/VDPA to check if the ioasid has been registered to device FD and record it in the per-device list. right? Let's have more discussion based on the skeleton sent by Kevin.

Quoted 7 lines

Callbacks would be backwards.

User calls vfio with vfio_device fd and dev/ioasid fd

VFIO extracts some kernel representation of the ioasid from the ioasid fd using an API

This lookup API seems to be asking for per ioasid FD storage array. Today, the ioasid_set is per mm and contains a Xarray. Since each VM, KVM can only open one ioasid FD, this per FD array would be equivalent to the per mm ioasid_set, right?

Quoted 3 lines

VFIO does some kernel call to IOMMU/IOASID layer that says 'tell the IOMMU that this PCI device is allowed to use this PASID'

Would it be redundant to what iommu_uapi_sva_bind_gpasid() does? I thought the idea is to use ioasid FD IOCTL to issue IOMMU uAPI calls. Or we can skip this step for now and wait for the user to do SVA bind.

Quoted 2 lines

VFIO mdev drivers then record that the PASID is allowed in its own device specific struct for later checking during other system calls.

Thanks,

Jacob
_______________________________________________
iommu mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/iommu

#236 lore raw

Re: [PATCH V4 05/18] iommu/ioasid: Redefine IOASID set and allocation APIs

Quoted 20 lines · Jacob Pan

Hi Jason,

Quoted 16 lines · Jason Gunthorpe
Quoted 3 lines
Quoted 3 lines

Get rid of the ioasid set.

Each driver has its own list of allowed ioasids.

[...]

The /dev/ioasid FD replaces this security check. By becoming FD centric you don't need additional kernel security objects.

Any process with access to the /dev/ioasid FD is allowed to control those PASID. The seperation between VMs falls naturally from the seperation of FDs without creating additional, complicated, security infrastrucure in the kernel.

This is why all APIs must be FD focused, and you need to have a logical layering of responsibility.

Quoted 8 lines
Quoted 7 lines

Allocate a /dev/ioasid FD
Allocate PASIDs inside the FD
Assign memory to the PASIDS

Open a device FD, eg from VFIO or VDP
Instruct the device FD to authorize the device to access PASID A in an ioasid FD

How do we know user provided PASID A was allocated by the ioasid FD?

You pass in the ioasid FD and use a 'get pasid from fdno' API to extract the required kernel structure.

Quoted 2 lines

Shouldn't we validate user input by tracking which PASIDs are allocated by which ioasid FD?

Yes, but it is integral to the ioasid FD, not something separated.

Quoted 5 lines
Quoted 3 lines

VFIO extracts some kernel representation of the ioasid from the ioasid fd using an API

This lookup API seems to be asking for per ioasid FD storage array. Today, the ioasid_set is per mm and contains a Xarray.

Right, put the xarray per FD. A set per mm is fairly nonsensical, we don't use the mm as that kind of security key.

Quoted 2 lines

Since each VM, KVM can only open one ioasid FD, this per FD array would be equivalent to the per mm ioasid_set, right?

Why only one? Each interaction with the other FDs should include the PASID/FD pair. There is no restriction to just one.

Quoted 6 lines
Quoted 2 lines

VFIO does some kernel call to IOMMU/IOASID layer that says 'tell the IOMMU that this PCI device is allowed to use this PASID'

Would it be redundant to what iommu_uapi_sva_bind_gpasid() does? I thought the idea is to use ioasid FD IOCTL to issue IOMMU uAPI calls. Or we can skip this step for now and wait for the user to do SVA bind.

I'm not sure what you are asking.

Possibly some of the IOMMU API will need a bit adjusting to make things split.

The act of programming the page tables and the act of authorizing a PCI BDF to use a PASID are distinct things with two different IOCTLs.

iommu_uapi_sva_bind_gpasid() is never called by anything, and it's uAPI is never implemented.

Joerg? Why did you merge dead uapi and dead code?

Jason

#42 lore raw

RE: [PATCH V4 05/18] iommu/ioasid: Redefine IOASID set and allocation APIs

In reply to Jason Gunthorpe
Quoted 16 lines

From: Jason Gunthorpe <[email protected]>
Sent: Wednesday, March 31, 2021 8:41 PM

Quoted 4 lines · Yi L

The reason is /dev/ioasid FD is per-VM since the ioasid allocated to the VM should be able to be shared by all assigned device for the VM. But the SVA operations (bind/unbind page table, cache_invalidate) should be per-device.

It is not *per-device* it is *per-ioasid*

And as /dev/ioasid is an interface for controlling multiple ioasid's there is no issue to also multiplex the page table manipulation for multiple ioasids as well.

Quoted 8 lines

What you should do next is sketch out in some RFC the exactl ioctls each FD would have and show how the parts I outlined would work and point out any remaining gaps.

The device FD is something like the vfio_device FD from VFIO, it has *nothing* to do with PASID beyond having a single ioctl to authorize the device to use the PASID. All control of the PASID is in /dev/ioasid.

good to see this reply. Your idea is much clearer to me now. If I'm getting you correctly. I think the skeleton is something like below:

1) userspace opens a /dev/ioasid, meanwhile there will be an ioasid allocated and a per-ioasid context which can be used to do bind page table and cache invalidate, an ioasid FD returned to userspace.
2) userspace passes the ioasid FD to VFIO, let it associated with a device FD (like vfio_device FD).
3) userspace binds page table on the ioasid FD with the page table info.
4) userspace unbinds the page table on the ioasid FD
5) userspace de-associates the ioasid FD and device FD

Does above suit your outline?

If yes, I still have below concern and wish to see your opinion.
- the ioasid FD and device association will happen at runtime instead of just happen in the setup phase.
- how about AMD and ARM's vSVA support? Their PASID allocation and page table happens within guest. They only need to bind the guest PASID table to host. Above model seems unable to fit them. (Jean, Eric, Jacob please feel free to correct me)
- this per-ioasid SVA operations is not aligned with the native SVA usage model. Native SVA bind is per-device.

Regards,
Yi Liu

#43 lore raw

RE: [PATCH V4 05/18] iommu/ioasid: Redefine IOASID set and allocation APIs

Hi Jason,

Quoted 43 lines

From: Liu, Yi L <[email protected]>
Sent: Thursday, April 1, 2021 12:39 PM

Quoted 8 lines

From: Jason Gunthorpe <[email protected]>
Sent: Wednesday, March 31, 2021 8:41 PM

Quoted 3 lines · Yi L

The reason is /dev/ioasid FD is per-VM since the ioasid allocated to the VM should be able to be shared by all assigned device for the VM. But the SVA operations (bind/unbind page table, cache_invalidate)

should

Quoted 8 lines
Quoted 1 line

be per-device.

It is not *per-device* it is *per-ioasid*

And as /dev/ioasid is an interface for controlling multiple ioasid's there is no issue to also multiplex the page table manipulation for multiple ioasids as well.

Quoted 8 lines

What you should do next is sketch out in some RFC the exactl ioctls each FD would have and show how the parts I outlined would work and point out any remaining gaps.

The device FD is something like the vfio_device FD from VFIO, it has *nothing* to do with PASID beyond having a single ioctl to authorize the device to use the PASID. All control of the PASID is in /dev/ioasid.

good to see this reply. Your idea is much clearer to me now. If I'm getting you correctly. I think the skeleton is something like below:
f
1) userspace opens a /dev/ioasid, meanwhile there will be an ioasid allocated and a per-ioasid context which can be used to do bind page table and cache invalidate, an ioasid FD returned to userspace.
2) userspace passes the ioasid FD to VFIO, let it associated with a device FD (like vfio_device FD).
3) userspace binds page table on the ioasid FD with the page table info.
4) userspace unbinds the page table on the ioasid FD
5) userspace de-associates the ioasid FD and device FD

Does above suit your outline?

Quoted 11 lines

If yes, I still have below concern and wish to see your opinion.
- the ioasid FD and device association will happen at runtime instead of just happen in the setup phase.
- how about AMD and ARM's vSVA support? Their PASID allocation and page table
happens within guest. They only need to bind the guest PASID table to
host.
Above model seems unable to fit them. (Jean, Eric, Jacob please feel free to correct me)
- this per-ioasid SVA operations is not aligned with the native SVA usage model. Native SVA bind is per-device.

After reading your reply in https://lore.kernel.org/linux-iommu/[email protected]/#t
So you mean /dev/ioasid FD is per-VM instead of per-ioasid, so above skeleton
doesn't suit your idea. I draft below skeleton to see if our mind is the
same. But I still believe there is an open on how to fit ARM and AMD's
vSVA support in this the per-ioasid SVA operation model. thoughts?

+-----------------------------+-----------------------------------------------+
|      userspace              |               kernel space                    |
+-----------------------------+-----------------------------------------------+
| ioasid_fd =                 | /dev/ioasid does below:                       |
| open("/dev/ioasid", O_RDWR);|   struct ioasid_fd_ctx {                      |
|                             |       struct list_head ioasid_list;           |
|                             |       ...                                     |
|                             |   } ifd_ctx; // ifd_ctx is per ioasid_fd      |
+-----------------------------+-----------------------------------------------+
| ioctl(ioasid_fd,            | /dev/ioasid does below:                       |
|       ALLOC, &ioasid);      |   struct ioasid_data {                        |
|                             |       ioasid_t ioasid;                        |
|                             |       struct list_head device_list;           |
|                             |       struct list_head next;                  |
|                             |       ...                                     |
|                             |   } id_data; // id_data is per ioasid         |
|                             |                                               |
|                             |   list_add(&id_data.next,                     |
|                             |            &ifd_ctx.ioasid_list);             |
+-----------------------------+-----------------------------------------------+
| ioctl(device_fd,            | VFIO does below:                              |
|       DEVICE_ALLOW_IOASID,  | 1) get ioasid_fd, check if ioasid_fd is valid |
|       ioasid_fd,            | 2) check if ioasid is allocated from ioasid_fd|
|       ioasid);              | 3) register device/domain info to /dev/ioasid |
|                             |    tracked in id_data.device_list             |
|                             | 4) record the ioasid in VFIO's per-device     |
|                             |    ioasid list for future security check      |
+-----------------------------+-----------------------------------------------+
| ioctl(ioasid_fd,            | /dev/ioasid does below:                       |
|       BIND_PGTBL,           | 1) find ioasid's id_data                      |
|       pgtbl_data,           | 2) loop the id_data.device_list and tell iommu|
|       ioasid);              |    give ioasid access to the devices          |
+-----------------------------+-----------------------------------------------+
| ioctl(ioasid_fd,            | /dev/ioasid does below:                       |
|       UNBIND_PGTBL,         | 1) find ioasid's id_data                      |
|       ioasid);              | 2) loop the id_data.device_list and tell iommu|
|                             |    clear ioasid access to the devices         |
+-----------------------------+-----------------------------------------------+
| ioctl(device_fd,            | VFIO does below:                              |
|      DEVICE_DISALLOW_IOASID,| 1) check if ioasid is associated in VFIO's    |
|       ioasid_fd,            |    device ioasid list.                        |
|       ioasid);              | 2) unregister device/domain info from         |
|                             |    /dev/ioasid, clear in id_data.device_list  |
+-----------------------------+-----------------------------------------------+
| ioctl(ioasid_fd,            | /dev/ioasid does below:                       |
|       FREE, ioasid);        |  list_del(&id_data.next);                     |
+-----------------------------+-----------------------------------------------+

Regards,
Yi Liu

#200 lore raw

Re: [PATCH V4 05/18] iommu/ioasid: Redefine IOASID set and allocation APIs

In reply to Liu, Yi L
Quoted 37 lines · Yi L
Quoted 16 lines

From: Jason Gunthorpe <[email protected]>
Sent: Wednesday, March 31, 2021 8:41 PM

Quoted 4 lines · Yi L

The reason is /dev/ioasid FD is per-VM since the ioasid allocated to the VM should be able to be shared by all assigned device for the VM. But the SVA operations (bind/unbind page table, cache_invalidate) should be per-device.

It is not *per-device* it is *per-ioasid*

And as /dev/ioasid is an interface for controlling multiple ioasid's there is no issue to also multiplex the page table manipulation for multiple ioasids as well.

Quoted 8 lines

What you should do next is sketch out in some RFC the exactl ioctls each FD would have and show how the parts I outlined would work and point out any remaining gaps.

The device FD is something like the vfio_device FD from VFIO, it has *nothing* to do with PASID beyond having a single ioctl to authorize the device to use the PASID. All control of the PASID is in /dev/ioasid.

good to see this reply. Your idea is much clearer to me now. If I'm getting you correctly. I think the skeleton is something like below:

1) userspace opens a /dev/ioasid, meanwhile there will be an ioasid allocated and a per-ioasid context which can be used to do bind page table and cache invalidate, an ioasid FD returned to userspace.
2) userspace passes the ioasid FD to VFIO, let it associated with a device FD (like vfio_device FD).
3) userspace binds page table on the ioasid FD with the page table info.
4) userspace unbinds the page table on the ioasid FD
5) userspace de-associates the ioasid FD and device FD

Quoted 1 line

Does above suit your outline?

Seems so

Quoted 3 lines

If yes, I still have below concern and wish to see your opinion.
- the ioasid FD and device association will happen at runtime instead of just happen in the setup phase.

Of course, this is required for security. The vIOMMU must perform the device association when the guest requires it. Otherwise a guest cannot isolate a PASID to a single process/device pair.

I'm worried Intel views the only use of PASID in a guest is with ENQCMD, but that is not consistent with the industry. We need to see normal nested PASID support with assigned PCI VFs.

Quoted 4 lines

- how about AMD and ARM's vSVA support? Their PASID allocation and page table happens within guest. They only need to bind the guest PASID table to host. Above model seems unable to fit them. (Jean, Eric, Jacob please feel free to correct me)

No, everything needs the device association step or it is not secure.

You can give a PASID to a guest and allow it to manipulate it's memory map directly, nested under the guest's CPU page tables.

However the guest cannot authorize a PCI BDF to utilize that PASID without going through some kind of step in the hypervisor. A Guest should not be able to authorize a PASID for a BDF it doesn't have access to - only the hypervisor can enforce this.

This all must also fit into the mdev model where only the device-specific mdev driver can do the device specific PASID authorization. A hypercall is essential, or we need to stop pretending mdev is a good idea.

I'm sure there will be some small differences, and you should clearly explain the entire uAPI surface so that soneone from AMD and ARM can review it.

Quoted 2 lines

- this per-ioasid SVA operations is not aligned with the native SVA usage model. Native SVA bind is per-device.

Seems like that is an error in native SVA.

SVA is a particular mode of the PASID's memory mapping table, it has nothing to do with a device.

Jason

#44 lore raw

Re: [PATCH V4 05/18] iommu/ioasid: Redefine IOASID set and allocation APIs

In reply to Liu, Yi L
Quoted 3 lines · Yi L

After reading your reply in https://lore.kernel.org/linux-iommu/[email protected]/#t
So you mean /dev/ioasid FD is per-VM instead of per-ioasid, so above skeleton
doesn't suit your idea.

You can do it one PASID per FD or multiple PASID's per FD. Most likely we will have high numbers of PASID's in a qemu process so I assume that number of FDs will start to be a contraining factor, thus multiplexing is reasonable.

It doesn't really change anything about the basic flow.

digging deeply into it either seems like a reasonable choice.

Quoted 8 lines
+-----------------------------+-----------------------------------------------+
|      userspace              |               kernel space                    |
+-----------------------------+-----------------------------------------------+
| ioasid_fd =                 | /dev/ioasid does below:                       |
| open("/dev/ioasid", O_RDWR);|   struct ioasid_fd_ctx {                      |
|                             |       struct list_head ioasid_list;           |
|                             |       ...                                     |
|                             |   } ifd_ctx; // ifd_ctx is per ioasid_fd      |

Sure, possibly an xarray not a list

Quoted 4 lines
+-----------------------------+-----------------------------------------------+
| ioctl(ioasid_fd,            | /dev/ioasid does below:                       |
|       ALLOC, &ioasid);      |   struct ioasid_data {                        |
|                             |       ioasid_t ioasid;                        |
Quoted 8 lines
|                             |       struct list_head device_list;           |
|                             |       struct list_head next;                  |
|                             |       ...                                     |
|                             |   } id_data; // id_data is per ioasid         |
|                             |                                               |
|                             |   list_add(&id_data.next,                     |
|                             |            &ifd_ctx.ioasid_list);
|

Yes, this should have a kref in it too

Quoted 8 lines
+-----------------------------+-----------------------------------------------+
| ioctl(device_fd,            | VFIO does below:                              |
|       DEVICE_ALLOW_IOASID,  | 1) get ioasid_fd, check if ioasid_fd is valid |
|       ioasid_fd,            | 2) check if ioasid is allocated from ioasid_fd|
|       ioasid);              | 3) register device/domain info to /dev/ioasid |
|                             |    tracked in id_data.device_list             |
|                             | 4) record the ioasid in VFIO's per-device     |
|                             |    ioasid list for future security check      |

You would provide a function that does steps 1&2 look at eventfd for instance.

I'm not sure we need to register the device with the ioasid. device should incr the kref on the ioasid_data at this point.

Quoted 6 lines
+-----------------------------+-----------------------------------------------+
| ioctl(ioasid_fd,            | /dev/ioasid does below:                       |
|       BIND_PGTBL,           | 1) find ioasid's id_data                      |
|       pgtbl_data,           | 2) loop the id_data.device_list and tell iommu|
|       ioasid);              |    give ioasid access to the devices
|

This seems backwards, DEVICE_ALLOW_IOASID should tell the iommu to give the ioasid to the device.

Here the ioctl should be about assigning a memory map from the the current mm_struct to the pasid

Quoted 5 lines
+-----------------------------+-----------------------------------------------+
| ioctl(ioasid_fd,            | /dev/ioasid does below:                       |
|       UNBIND_PGTBL,         | 1) find ioasid's id_data                      |
|       ioasid);              | 2) loop the id_data.device_list and tell iommu|
|                             |    clear ioasid access to the devices         |

Also seems backwards. The ioctl here should be 'destroy ioasid' which wipes out the page table, halts DMA access and parks the PASID until all users are done.

Quoted 6 lines
+-----------------------------+-----------------------------------------------+
| ioctl(device_fd,            | VFIO does below:                              |
|      DEVICE_DISALLOW_IOASID,| 1) check if ioasid is associated in VFIO's    |
|       ioasid_fd,            |    device ioasid list.                        |
|       ioasid);              | 2) unregister device/domain info from         |
|                             |    /dev/ioasid, clear in id_data.device_list  |

This should disconnect the iommu and kref_put the ioasid_data

Remember the layering, only the device_fd knows what the pci_device is that it is touching, it doesn't make alot of sense to leak that into the ioasid world that should only be dealing with the page table mapping.

Quoted 4 lines
+-----------------------------+-----------------------------------------------+
| ioctl(ioasid_fd,            | /dev/ioasid does below:                       |
|       FREE, ioasid);        |  list_del(&id_data.next);                     |
+-----------------------------+-----------------------------------------------+

Don't know if we need a free. The sequence above is backwards, the page table should be setup, the device authorized, device de-authorized then page table destroyed. PASID recycles once everyone is released.

Include a sequence showing how the kvm FD is used to program the vPASID to pPASID table that ENQCMD uses.

Show how dynamic authorization works based on requests from the guest's vIOMMU

Jason

#46 lore raw

Re: [PATCH V4 05/18] iommu/ioasid: Redefine IOASID set and allocation APIs

In reply to Liu, Yi L
Quoted 4 lines · Yi L
Quoted 4 lines

- how about AMD and ARM's vSVA support? Their PASID allocation and page table
happens within guest. They only need to bind the guest PASID table to
host.

In this case each VM has its own IOASID space, and the host IOASID allocator doesn't participate. Plus this only makes sense when assigning a whole VF to a guest, and VFIO is the tool for this. So I wouldn't shoehorn those ops into /dev/ioasid, though we do need a transport for invalidate commands.

Quoted 4 lines
Quoted 4 lines

Above model seems unable to fit them. (Jean, Eric, Jacob please feel free to correct me)
- this per-ioasid SVA operations is not aligned with the native SVA usage model. Native SVA bind is per-device.

Bare-metal SVA doesn't need /dev/ioasid either. A program uses a device handle to either ask whether SVA is enabled, or to enable it explicitly. With or without /dev/ioasid, that step is required. OpenCL uses the first method - automatically enable "fine-grain system SVM" if available, and provide a flag to userspace.

So userspace does not need to know about PASID. It's only one method for doing SVA (some GPUs are context-switching page tables instead).

Quoted 45 lines

After reading your reply in https://lore.kernel.org/linux-iommu/[email protected]/#t
So you mean /dev/ioasid FD is per-VM instead of per-ioasid, so above skeleton
doesn't suit your idea. I draft below skeleton to see if our mind is the
same. But I still believe there is an open on how to fit ARM and AMD's
vSVA support in this the per-ioasid SVA operation model. thoughts?

+-----------------------------+-----------------------------------------------+
|      userspace              |               kernel space                    |
+-----------------------------+-----------------------------------------------+
| ioasid_fd =                 | /dev/ioasid does below:                       |
| open("/dev/ioasid", O_RDWR);|   struct ioasid_fd_ctx {                      |
|                             |       struct list_head ioasid_list;           |
|                             |       ...                                     |
|                             |   } ifd_ctx; // ifd_ctx is per ioasid_fd      |
+-----------------------------+-----------------------------------------------+
| ioctl(ioasid_fd,            | /dev/ioasid does below:                       |
|       ALLOC, &ioasid);      |   struct ioasid_data {                        |
|                             |       ioasid_t ioasid;                        |
|                             |       struct list_head device_list;           |
|                             |       struct list_head next;                  |
|                             |       ...                                     |
|                             |   } id_data; // id_data is per ioasid         |
|                             |                                               |
|                             |   list_add(&id_data.next,                     |
|                             |            &ifd_ctx.ioasid_list);             |
+-----------------------------+-----------------------------------------------+
| ioctl(device_fd,            | VFIO does below:                              |
|       DEVICE_ALLOW_IOASID,  | 1) get ioasid_fd, check if ioasid_fd is valid |
|       ioasid_fd,            | 2) check if ioasid is allocated from ioasid_fd|
|       ioasid);              | 3) register device/domain info to /dev/ioasid |
|                             |    tracked in id_data.device_list             |
|                             | 4) record the ioasid in VFIO's per-device     |
|                             |    ioasid list for future security check      |
+-----------------------------+-----------------------------------------------+
| ioctl(ioasid_fd,            | /dev/ioasid does below:                       |
|       BIND_PGTBL,           | 1) find ioasid's id_data                      |
|       pgtbl_data,           | 2) loop the id_data.device_list and tell iommu|
|       ioasid);              |    give ioasid access to the devices          |
+-----------------------------+-----------------------------------------------+
| ioctl(ioasid_fd,            | /dev/ioasid does below:                       |
|       UNBIND_PGTBL,         | 1) find ioasid's id_data                      |
|       ioasid);              | 2) loop the id_data.device_list and tell iommu|
|                             |    clear ioasid access to the devices         |
+-----------------------------+-----------------------------------------------+
| ioctl(device_fd,            | VFIO does below:                              |
Quoted 8 lines
|      DEVICE_DISALLOW_IOASID,| 1) check if ioasid is associated in VFIO's    |
|       ioasid_fd,            |    device ioasid list.                        |
|       ioasid);              | 2) unregister device/domain info from         |
|                             |    /dev/ioasid, clear in id_data.device_list  |
+-----------------------------+-----------------------------------------------+
| ioctl(ioasid_fd,            | /dev/ioasid does below:                       |
|       FREE, ioasid);        |  list_del(&id_data.next);                     |
+-----------------------------+-----------------------------------------------+

Also wondering about:

* Querying IOMMU nesting capabilities before binding page tables (which page table formats are supported?). We were planning to have a VFIO cap, but I'm guessing we need to go back to the sysfs solution?

* Invalidation, probably an ioasid_fd ioctl?

* Page faults, page response. From and to devices, and don't necessarily have a PASID. But needed by vdpa as well, so that's also going through /dev/ioasid?

Thanks,
Jean

#47 lore raw

Re: [PATCH V4 05/18] iommu/ioasid: Redefine IOASID set and allocation APIs

Quoted 11 lines · Jean-Philippe Brucker
Quoted 4 lines · Yi L
Quoted 4 lines

- how about AMD and ARM's vSVA support? Their PASID allocation and page table
happens within guest. They only need to bind the guest PASID table to
host.

In this case each VM has its own IOASID space, and the host IOASID allocator doesn't participate. Plus this only makes sense when assigning a whole VF to a guest, and VFIO is the tool for this. So I wouldn't shoehorn those ops into /dev/ioasid, though we do need a transport for invalidate commands.

How does security work? Devices still have to be authorized to use the PASID and this approach seems like it completely excludes mdev/vdpa from ever using a PASID, and those are the most logical users.

Quoted 6 lines
Quoted 4 lines
Quoted 4 lines

Above model seems unable to fit them. (Jean, Eric, Jacob please feel free to correct me)
- this per-ioasid SVA operations is not aligned with the native SVA usage model. Native SVA bind is per-device.

Bare-metal SVA doesn't need /dev/ioasid either.

It depends what you are doing. /dev/ioasid would provide fine grained control over the memory mapping. It is not strict SVA, but I can see applications where using a GPU with a pre-configured optimized mapping could be interesting.

Quoted 5 lines

A program uses a device handle to either ask whether SVA is enabled, or to enable it explicitly. With or without /dev/ioasid, that step is required. OpenCL uses the first method - automatically enable "fine-grain system SVM" if available, and provide a flag to userspace.

SVA can be done with ioasid, we can decide if it makes sense to have shortcuts in every driver

Quoted 2 lines

So userspace does not need to know about PASID. It's only one method for doing SVA (some GPUs are context-switching page tables instead).

Sure, there are lots of approaches. Here we are only talking about PASID enablement. PASID has more options.

Quoted 3 lines

* Page faults, page response. From and to devices, and don't necessarily have a PASID. But needed by vdpa as well, so that's also going through /dev/ioasid?

Only real PASID's should use this interface. All the not-PASID stuff is on its own.

VPDA should accept a PASID from here and configure&authorize the real HW to attach the PASID to all DMA's connected to the virtio queues.

Jason

#201 lore raw

RE: [PATCH V4 05/18] iommu/ioasid: Redefine IOASID set and allocation APIs

In reply to Jason Gunthorpe
Quoted 2 lines

From: Jason Gunthorpe <[email protected]>
Sent: Thursday, April 1, 2021 7:47 PM

[...]

Quoted 3 lines

I'm worried Intel views the only use of PASID in a guest is with ENQCMD, but that is not consistent with the industry. We need to see normal nested PASID support with assigned PCI VFs.

I'm not quire flow here. Intel also allows PASID usage in guest without ENQCMD. e.g. Passthru a PF to guest, and use PASID on it without ENQCMD.

[...]

Quoted 3 lines

I'm sure there will be some small differences, and you should clearly explain the entire uAPI surface so that soneone from AMD and ARM can review it.

good suggestion, will do.

Quoted 7 lines
Quoted 2 lines

- this per-ioasid SVA operations is not aligned with the native SVA usage model. Native SVA bind is per-device.

Seems like that is an error in native SVA.

SVA is a particular mode of the PASID's memory mapping table, it has nothing to do with a device.

I think it still has relationship with device. This is determined by the DMA remapping hierarchy in hardware. e.g. Intel VT-d, the DMA isolation is enforced first in device granularity and then PASID granularity. SVA makes usage of both PASID and device granularity isolation.

Regards,
Yi Liu

Quoted 1 line

Jason

#202 lore raw

Re: [PATCH V4 05/18] iommu/ioasid: Redefine IOASID set and allocation APIs

Quoted 9 lines · Yi L
Quoted 2 lines

From: Jason Gunthorpe <[email protected]>
Sent: Thursday, April 1, 2021 7:47 PM

[...]

Quoted 3 lines

I'm worried Intel views the only use of PASID in a guest is with ENQCMD, but that is not consistent with the industry. We need to see normal nested PASID support with assigned PCI VFs.

I'm not quire flow here. Intel also allows PASID usage in guest without ENQCMD. e.g. Passthru a PF to guest, and use PASID on it without ENQCMD.

Then you need all the parts, the hypervisor calls from the vIOMMU, and you can't really use a vPASID.

I'm not sure how Intel intends to resolve all of this.

Quoted 5 lines
Quoted 5 lines
Quoted 2 lines

- this per-ioasid SVA operations is not aligned with the native SVA usage model. Native SVA bind is per-device.

Seems like that is an error in native SVA.

Quoted 7 lines
Quoted 2 lines

SVA is a particular mode of the PASID's memory mapping table, it has nothing to do with a device.

I think it still has relationship with device. This is determined by the DMA remapping hierarchy in hardware. e.g. Intel VT-d, the DMA isolation is enforced first in device granularity and then PASID granularity. SVA makes usage of both PASID and device granularity isolation.

When the device driver authorizes a PASID the VT-d stuff should setup the isolation parameters for the give pci_device and PASID.

Do not leak implementation details like this as uAPI. Authorization and memory map are distinct ideas with distinct interfaces. Do not mix them.

Jason

#48 lore raw

RE: [PATCH V4 05/18] iommu/ioasid: Redefine IOASID set and allocation APIs

In reply to Jean-Philippe Brucker
Quoted 2 lines

From: Jean-Philippe Brucker <[email protected]>
Sent: Thursday, April 1, 2021 8:05 PM

[...]

Quoted 6 lines

Also wondering about:

* Querying IOMMU nesting capabilities before binding page tables (which page table formats are supported?). We were planning to have a VFIO cap, but I'm guessing we need to go back to the sysfs solution?

I think it can also be with /dev/ioasid.

Quoted 2 lines

* Invalidation, probably an ioasid_fd ioctl?

yeah, if we are doing bind/unbind_pagtable via ioasid_fd, then yes, invalidation should go this way as well. This is why I worried it may fail to meet the requirement from you and Eric.

Quoted 3 lines

* Page faults, page response. From and to devices, and don't necessarily have a PASID. But needed by vdpa as well, so that's also going through /dev/ioasid?

page faults should still be per-device, but the fault event fd may be stored in /dev/ioasid. page response would be in /dev/ioasid just like invalidation.

Regards,
Yi Liu

Quoted 3 lines

Thanks,
Jean

#49 lore raw

Re: [PATCH V4 05/18] iommu/ioasid: Redefine IOASID set and allocation APIs

Quoted 11 lines · Yi L
Quoted 2 lines

From: Jean-Philippe Brucker <[email protected]>
Sent: Thursday, April 1, 2021 8:05 PM

[...]

Quoted 6 lines

Also wondering about:

* Querying IOMMU nesting capabilities before binding page tables (which page table formats are supported?). We were planning to have a VFIO cap, but I'm guessing we need to go back to the sysfs solution?

I think it can also be with /dev/ioasid.

Sure, anything to do with page table formats and setting page tables should go through ioasid.

Quoted 5 lines
Quoted 1 line

* Invalidation, probably an ioasid_fd ioctl?

yeah, if we are doing bind/unbind_pagtable via ioasid_fd, then yes, invalidation should go this way as well. This is why I worried it may fail to meet the requirement from you and Eric.

Yes, all manipulation of page tables, including removing memory ranges, or setting memory ranges to trigger a page fault behavior should go through here.

Quoted 6 lines
Quoted 3 lines

* Page faults, page response. From and to devices, and don't necessarily have a PASID. But needed by vdpa as well, so that's also going through /dev/ioasid?

page faults should still be per-device, but the fault event fd may be stored in /dev/ioasid. page response would be in /dev/ioasid just like invalidation.

Here you mean non-SVA page faults that are delegated to userspace to handle?

Why would that be per-device?

Can you show the flow you imagine?

Jason

#203 lore raw

RE: [PATCH V4 05/18] iommu/ioasid: Redefine IOASID set and allocation APIs

In reply to Jason Gunthorpe
Quoted 11 lines

From: Jason Gunthorpe <[email protected]>
Sent: Thursday, April 1, 2021 9:16 PM

Quoted 7 lines · Yi L
Quoted 2 lines

From: Jason Gunthorpe <[email protected]>
Sent: Thursday, April 1, 2021 7:47 PM

[...]

Quoted 3 lines

I'm worried Intel views the only use of PASID in a guest is with ENQCMD, but that is not consistent with the industry. We need to see normal nested PASID support with assigned PCI VFs.

Quoted 6 lines
Quoted 2 lines

I'm not quire flow here. Intel also allows PASID usage in guest without ENQCMD. e.g. Passthru a PF to guest, and use PASID on it without

ENQCMD.

Then you need all the parts, the hypervisor calls from the vIOMMU, and you can't really use a vPASID.

This is a diagram shows the vSVA setup.

    .-------------.  .---------------------------.
    |   vIOMMU    |  | Guest process CR3, FL only|
    |             |  '---------------------------'

.----------------/
| PASID Entry |--- PASID cache flush -
'-------------' |
| | V
| | CR3 in GPA
'-------------'
Guest

------| Shadow |--------------------------|--------
      v        v                          v
Host
    .-------------.  .----------------------.
    |   pIOMMU    |  | Bind FL for GVA-GPA  |
    |             |  '----------------------'
    .----------------/  |
    | PASID Entry |     V (Nested xlate)
    '----------------\.------------------------------.
    |             |   |SL for GPA-HPA, default domain|
    |             |   '------------------------------'
    '-------------'

Where:
- FL = First level/stage one page tables
- SL = Second level/stage two page tables

https://lore.kernel.org/linux-iommu/20210302203545.436623-1-yi.l.liu-ral2JQCrhuEAvxtiuMwx3w@public.gmane.org/

Quoted 12 lines

I'm not sure how Intel intends to resolve all of this.

Quoted 1 line
Quoted 1 line
Quoted 1 line

- this per-ioasid SVA operations is not aligned with the native SVA

usage

Quoted 7 lines
Quoted 6 lines
Quoted 1 line

model. Native SVA bind is per-device.

Seems like that is an error in native SVA.

SVA is a particular mode of the PASID's memory mapping table, it has nothing to do with a device.

Quoted 8 lines
Quoted 2 lines

I think it still has relationship with device. This is determined by the DMA remapping hierarchy in hardware. e.g. Intel VT-d, the DMA isolation

is

Quoted 2 lines

enforced first in device granularity and then PASID granularity. SVA makes usage of both PASID and device granularity isolation.

When the device driver authorizes a PASID the VT-d stuff should setup the isolation parameters for the give pci_device and PASID.

yes, both device and PASID is needed to setup VT-d stuff.

Quoted 3 lines

Do not leak implementation details like this as uAPI. Authorization and memory map are distinct ideas with distinct interfaces. Do not mix them.

got you. Let's focus on the uAPI things here and leave implementation details in RFC patches.

Thanks,
Yi Liu

Quoted 1 line

Jason

#204 lore raw

Re: [PATCH V4 05/18] iommu/ioasid: Redefine IOASID set and allocation APIs

Quoted 11 lines · Yi L
Quoted 11 lines

From: Jason Gunthorpe <[email protected]>
Sent: Thursday, April 1, 2021 9:16 PM

Quoted 7 lines · Yi L
Quoted 2 lines

From: Jason Gunthorpe <[email protected]>
Sent: Thursday, April 1, 2021 7:47 PM

[...]

Quoted 3 lines

I'm worried Intel views the only use of PASID in a guest is with ENQCMD, but that is not consistent with the industry. We need to see normal nested PASID support with assigned PCI VFs.

Quoted 8 lines
Quoted 6 lines
Quoted 2 lines

I'm not quire flow here. Intel also allows PASID usage in guest without ENQCMD. e.g. Passthru a PF to guest, and use PASID on it without

ENQCMD.

Then you need all the parts, the hypervisor calls from the vIOMMU, and you can't really use a vPASID.

This is a diagram shows the vSVA setup.

I'm not talking only about vSVA. Generic PASID support with arbitary mappings.

And how do you deal with the vPASID vs pPASID issue if the system has a mix of physical devices and mdevs?

Jason

#50 lore raw

RE: [PATCH V4 05/18] iommu/ioasid: Redefine IOASID set and allocation APIs

In reply to Jason Gunthorpe
Quoted 35 lines

From: Jason Gunthorpe <[email protected]>
Sent: Thursday, April 1, 2021 9:43 PM

Quoted 7 lines · Yi L
Quoted 2 lines

From: Jean-Philippe Brucker <[email protected]>
Sent: Thursday, April 1, 2021 8:05 PM

[...]

Quoted 4 lines

Also wondering about:

* Querying IOMMU nesting capabilities before binding page tables

(which

Quoted 1 line
Quoted 1 line

page table formats are supported?). We were planning to have a VFIO

cap,

Quoted 3 lines
Quoted 1 line

but I'm guessing we need to go back to the sysfs solution?

I think it can also be with /dev/ioasid.

Sure, anything to do with page table formats and setting page tables should go through ioasid.

Quoted 5 lines
Quoted 1 line

* Invalidation, probably an ioasid_fd ioctl?

yeah, if we are doing bind/unbind_pagtable via ioasid_fd, then yes, invalidation should go this way as well. This is why I worried it may fail to meet the requirement from you and Eric.

Yes, all manipulation of page tables, including removing memory ranges, or setting memory ranges to trigger a page fault behavior should go through here.

Quoted 4 lines
Quoted 3 lines

* Page faults, page response. From and to devices, and don't necessarily have a PASID. But needed by vdpa as well, so that's also going through /dev/ioasid?

Quoted 5 lines
Quoted 2 lines

page faults should still be per-device, but the fault event fd may be stored in /dev/ioasid. page response would be in /dev/ioasid just like invalidation.

Here you mean non-SVA page faults that are delegated to userspace to handle?

no, just SVA page faults. otherwise, no need to let userspace handle.

Quoted 4 lines

Why would that be per-device?

Can you show the flow you imagine?

DMA page faults are delivered to root-complex via page request message and it is per-device according to PCIe spec. Page request handling flow is:

1) iommu driver receives a page request from device
2) iommu driver parses the page request message. Get the RID,PASID, faulted page and requested permissions etc.
3) iommu driver triggers fault handler registered by device driver with iommu_report_device_fault()
4) device driver's fault handler signals an event FD to notify userspace to fetch the information about the page fault. If it's VM case, inject the page fault to VM and let guest to solve it.

Eric has sent below series for the page fault reporting for VM with passthru device.
https://lore.kernel.org/kvm/20210223210625.604517-5-eric.auger-H+wXaHxf7aLQT0dZR+AlfA@public.gmane.org/

Regards,
Yi Liu

Quoted 1 line

Jason

#51 lore raw

Re: [PATCH V4 05/18] iommu/ioasid: Redefine IOASID set and allocation APIs

Quoted 8 lines · Yi L

DMA page faults are delivered to root-complex via page request message and it is per-device according to PCIe spec. Page request handling flow is:

1) iommu driver receives a page request from device
2) iommu driver parses the page request message. Get the RID,PASID, faulted page and requested permissions etc.
3) iommu driver triggers fault handler registered by device driver with iommu_report_device_fault()

This seems confused.

The PASID should define how to handle the page fault, not the driver.

I don't remember any device specific actions in ATS, so what is the driver supposed to do?

Quoted 3 lines

4) device driver's fault handler signals an event FD to notify userspace to fetch the information about the page fault. If it's VM case, inject the page fault to VM and let guest to solve it.

If the PASID is set to 'report page fault to userspace' then some event should come out of /dev/ioasid, or be reported to a linked eventfd, or whatever.

If the PASID is set to 'SVM' then the fault should be passed to handle_mm_fault

And so on.

Userspace chooses what happens based on how they configure the PASID through /dev/ioasid.

Why would a device driver get involved here?

Quoted 3 lines

Eric has sent below series for the page fault reporting for VM with passthru device.
https://lore.kernel.org/kvm/20210223210625.604517-5-eric.auger-H+wXaHxf7aLQT0dZR+AlfA@public.gmane.org/

It certainly should not be in vfio pci. Everything using a PASID needs this infrastructure, VDPA, mdev, PCI, CXL, etc.

Jason

author
#237 lore raw

Re: [PATCH V4 05/18] iommu/ioasid: Redefine IOASID set and allocation APIs

In reply to Jason Gunthorpe

Hi Jason,

Quoted 18 lines · Jason Gunthorpe
Quoted 17 lines · Jacob Pan

Hi Jason,

Quoted 13 lines · Jason Gunthorpe
Quoted 3 lines
Quoted 3 lines

Get rid of the ioasid set.

Each driver has its own list of allowed ioasids.

[...]

The /dev/ioasid FD replaces this security check. By becoming FD centric you don't need additional kernel security objects.

Any process with access to the /dev/ioasid FD is allowed to control those PASID. The seperation between VMs falls naturally from the seperation of FDs without creating additional, complicated, security infrastrucure in the kernel.

Quoted 5 lines
Quoted 5 lines
Quoted 5 lines

This is why all APIs must be FD focused, and you need to have a logical layering of responsibility.

Allocate a /dev/ioasid FD
Allocate PASIDs inside the FD

Just to be super clear. Do we allocate a FD for each PASID and return the FD to the user? Or return the plain PASID number back to the user space?

Quoted 10 lines
Quoted 6 lines
Quoted 5 lines

Assign memory to the PASIDS

Open a device FD, eg from VFIO or VDP
Instruct the device FD to authorize the device to access PASID A in an ioasid FD

How do we know user provided PASID A was allocated by the ioasid FD?

You pass in the ioasid FD and use a 'get pasid from fdno' API to extract the required kernel structure.

Seems you are talking about two FDs:
- /dev/ioasid FD
- per IOASID FD
This API ioasid = get_pasid_from_fd(dev_ioasid_fd, ioasid_fd); dev_ioasid_fd will find the xarray for all the PASIDs allocated under it, ioasid_fd wil be the index into the xarray to retrieve the actual ioasid. Correct?

Quoted 5 lines
Quoted 2 lines

Shouldn't we validate user input by tracking which PASIDs are allocated by which ioasid FD?

Yes, but it is integral to the ioasid FD, not something separated.

OK, if we have per IOASID FD in addition to the /dev/ioasid FD we can validate user input.

Quoted 9 lines
Quoted 5 lines
Quoted 3 lines

VFIO extracts some kernel representation of the ioasid from the ioasid fd using an API

This lookup API seems to be asking for per ioasid FD storage array. Today, the ioasid_set is per mm and contains a Xarray.

Right, put the xarray per FD. A set per mm is fairly nonsensical, we don't use the mm as that kind of security key.

Sounds good, one xarray per /dev/ioasid FD.

Quoted 6 lines
Quoted 2 lines

Since each VM, KVM can only open one ioasid FD, this per FD array would be equivalent to the per mm ioasid_set, right?

Why only one? Each interaction with the other FDs should include the PASID/FD pair. There is no restriction to just one.

OK, one per subsystem-VM. For example, if a VM has a VFIO and a VDPA device, it should only two /dev/ioasid FDs respectively. Correct?

Quoted 7 lines
Quoted 6 lines
Quoted 2 lines

VFIO does some kernel call to IOMMU/IOASID layer that says 'tell the IOMMU that this PCI device is allowed to use this PASID'

Would it be redundant to what iommu_uapi_sva_bind_gpasid() does? I thought the idea is to use ioasid FD IOCTL to issue IOMMU uAPI calls. Or we can skip this step for now and wait for the user to do SVA bind.

Quoted 8 lines

I'm not sure what you are asking.

Possibly some of the IOMMU API will need a bit adjusting to make things split.

The act of programming the page tables and the act of authorizing a PCI BDF to use a PASID are distinct things with two different IOCTLs.

Why separate? I don't see a use case to just authorize a PASID but don't bind it with a page table. The very act of bind page table *is* the authorization.

Quoted 3 lines

iommu_uapi_sva_bind_gpasid() is never called by anything, and it's uAPI is never implemented.

Just a little background here. We have been working on the vSVA stack since 2017. At the time, VFIO was the de facto interface for IOMMU-aware driver framework. These uAPIs were always developed alone side with the accompanying VFIO patches served as consumers. By the time these IOMMU uAPIs were merged after reviews from most vendors, the VFIO patchset was approaching maturity in around v7. This is when we suddenly saw a new request to support VDPA, which attempted VFIO earlier but ultimately moved away.

For a complex stack like vSVA, I feel we have to reduce moving parts and do some divide and conquer.

Quoted 3 lines

Joerg? Why did you merge dead uapi and dead code?

Jason

Thanks,

Jacob
_______________________________________________
iommu mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/iommu

#238 lore raw

Re: [PATCH V4 05/18] iommu/ioasid: Redefine IOASID set and allocation APIs

Quoted 22 lines · Jacob Pan

Hi Jason,

Quoted 18 lines · Jason Gunthorpe
Quoted 17 lines · Jacob Pan

Hi Jason,

Quoted 13 lines · Jason Gunthorpe
Quoted 3 lines
Quoted 3 lines

Get rid of the ioasid set.

Each driver has its own list of allowed ioasids.

[...]

The /dev/ioasid FD replaces this security check. By becoming FD centric you don't need additional kernel security objects.

Any process with access to the /dev/ioasid FD is allowed to control those PASID. The seperation between VMs falls naturally from the seperation of FDs without creating additional, complicated, security infrastrucure in the kernel.

Quoted 7 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines

This is why all APIs must be FD focused, and you need to have a logical layering of responsibility.

Allocate a /dev/ioasid FD
Allocate PASIDs inside the FD

Just to be super clear. Do we allocate a FD for each PASID and return the FD to the user? Or return the plain PASID number back to the user space?

I would do multiple PASID's per /dev/ioasid FD because we expect alot of PASIDs to be in use and we'd run into FDno limits.

Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines

Assign memory to the PASIDS

Open a device FD, eg from VFIO or VDP
Instruct the device FD to authorize the device to access PASID A in an ioasid FD

Quoted 7 lines
Quoted 5 lines
Quoted 1 line

How do we know user provided PASID A was allocated by the ioasid FD?

You pass in the ioasid FD and use a 'get pasid from fdno' API to extract the required kernel structure.

Seems you are talking about two FDs:
- /dev/ioasid FD

No, just this one.

Quoted 5 lines

- per IOASID FD
This API ioasid = get_pasid_from_fd(dev_ioasid_fd, ioasid_fd); dev_ioasid_fd will find the xarray for all the PASIDs allocated under it, ioasid_fd wil be the index into the xarray to retrieve the actual ioasid. Correct?

'ioasid_fd' is just the ioasid number in whatever numberspace the /dev/ioasid FD's use.

Quoted 5 lines
Quoted 2 lines

Why only one? Each interaction with the other FDs should include the PASID/FD pair. There is no restriction to just one.

OK, one per subsystem-VM. For example, if a VM has a VFIO and a VDPA device, it should only two /dev/ioasid FDs respectively. Correct?

No, only one.

For something like qemu's use case I mostly expect the vIOMMU driver will open /dev/ioasid for each vIOMMU instance it creates (basically only one)

Quoted 4 lines
Quoted 3 lines

The act of programming the page tables and the act of authorizing a PCI BDF to use a PASID are distinct things with two different IOCTLs.

Why separate?

Because they have different owners and different layers in the software.

It is not about use case, it is about putting the control points where they naturally belong.

Quoted 2 lines

For a complex stack like vSVA, I feel we have to reduce moving parts and do some divide and conquer.

uAPI should have all come together with a user and user application.

uAPI is hardest and most important part.

Jason

#52 lore raw

RE: [PATCH V4 05/18] iommu/ioasid: Redefine IOASID set and allocation APIs

In reply to Jason Gunthorpe
Quoted 41 lines

From: Jason Gunthorpe <[email protected]>
Sent: Friday, April 2, 2021 12:04 AM

Quoted 1 line · Yi L

DMA page faults are delivered to root-complex via page request message

and

Quoted 4 lines

it is per-device according to PCIe spec. Page request handling flow is:

1) iommu driver receives a page request from device
2) iommu driver parses the page request message. Get the RID,PASID,

faulted

Quoted 3 lines

page and requested permissions etc.
3) iommu driver triggers fault handler registered by device driver with iommu_report_device_fault()

This seems confused.

The PASID should define how to handle the page fault, not the driver.

I don't remember any device specific actions in ATS, so what is the driver supposed to do?

Quoted 3 lines

4) device driver's fault handler signals an event FD to notify userspace to fetch the information about the page fault. If it's VM case, inject the page fault to VM and let guest to solve it.

If the PASID is set to 'report page fault to userspace' then some event should come out of /dev/ioasid, or be reported to a linked eventfd, or whatever.

If the PASID is set to 'SVM' then the fault should be passed to handle_mm_fault

And so on.

Userspace chooses what happens based on how they configure the PASID through /dev/ioasid.

Why would a device driver get involved here?

Quoted 8 lines
Quoted 3 lines

Eric has sent below series for the page fault reporting for VM with passthru device.
https://lore.kernel.org/kvm/20210223210625.604517-5-

[email protected]/

It certainly should not be in vfio pci. Everything using a PASID needs this infrastructure, VDPA, mdev, PCI, CXL, etc.

This touches an interesting fact:

The fault may be triggered in either 1st-level or 2nd-level page table, when nested translation is enabled (in vSVA case). The 1st-level is bound by the user space, which therefore needs to receive the fault event. The 2nd-level is managed by VFIO (or vDPA), which needs to fix the fault in kernel (e.g. find HVA per faulting GPA, call handle_mm_fault and map GPA->HPA to IOMMU). Yi's current proposal lets VFIO to register the device fault handler, which then forward the event through /dev/ioasid to userspace only if it is a 1st-level fault. Are you suggesting a pgtable- centric fault reporting mechanism to separate handlers in each level, i.e. letting VFIO register handler only for 2nd-level fault and then /dev/ ioasid register handler for 1st-level fault?

Thanks
Kevin

#205 lore raw

RE: [PATCH V4 05/18] iommu/ioasid: Redefine IOASID set and allocation APIs

In reply to Jason Gunthorpe
Quoted 22 lines

From: Jason Gunthorpe <[email protected]>
Sent: Thursday, April 1, 2021 9:47 PM

Quoted 18 lines · Yi L
Quoted 17 lines

From: Jason Gunthorpe <[email protected]>
Sent: Thursday, April 1, 2021 9:16 PM

Quoted 9 lines · Yi L
Quoted 2 lines

From: Jason Gunthorpe <[email protected]>
Sent: Thursday, April 1, 2021 7:47 PM

[...]

Quoted 3 lines

I'm worried Intel views the only use of PASID in a guest is with ENQCMD, but that is not consistent with the industry. We need to see normal nested PASID support with assigned PCI VFs.

I'm not quire flow here. Intel also allows PASID usage in guest without ENQCMD. e.g. Passthru a PF to guest, and use PASID on it without

ENQCMD.

Then you need all the parts, the hypervisor calls from the vIOMMU, and you can't really use a vPASID.

Quoted 8 lines
Quoted 1 line

This is a diagram shows the vSVA setup.

I'm not talking only about vSVA. Generic PASID support with arbitary mappings.

And how do you deal with the vPASID vs pPASID issue if the system has a mix of physical devices and mdevs?

We plan to support two schemes. One is vPASID identity-mapped to pPASID then the mixed scenario just works, with the limitation of lacking of live migration support. The other is non-identity-mapped scheme, where live migration is supported but physical devices and mdevs should not be mixed in one VM if both expose SVA capability (requires some filtering check in Qemu). Although we have some idea relaxing this restriction in the non-identity scheme, it requires more thinking given how the vSVA uAPI is being refactored.

In both cases the virtual VT-d will report a virtual capability to the guest, indicating that the guest must request PASID through a vcmd register instead of creating its own namespace. The vIOMMU returns a vPASID to the guest upon request. The vPASID could be directly mapped to a pPASID or allocated from a new namespace based on user configuration.

We hope the /dev/ioasid can support both schemes, with the minimal requirement of allowing userspace to tag a vPASID to a pPASID and allowing mdev to translate vPASID into pPASID, i.e. not assuming that the guest will always use pPASID.

Thanks
Kevin

#213 lore raw

RE: [PATCH V4 05/18] iommu/ioasid: Redefine IOASID set and allocation APIs

In reply to Jason Gunthorpe
Quoted 22 lines

From: Jason Gunthorpe <[email protected]>
Sent: Tuesday, March 30, 2021 9:29 PM

Quoted 7 lines

First, userspace may use ioasid in a non-SVA scenario where ioasid is bound to specific security context (e.g. a control vq in vDPA) instead of tying to mm. In this case there is no pgtable binding initiated from user space. Instead, ioasid is allocated from /dev/ioasid and then programmed to the intended security context through specific passthrough framework which manages that context.

This sounds like the exact opposite of what I'd like to see.

I do not want to see every subsystem gaining APIs to program a PASID. All of that should be consolidated in *one place*.

I do not want to see VDPA and VFIO have two nearly identical sets of APIs to control the PASID.

Drivers consuming a PASID, like VDPA, should consume the PASID and do nothing more than authorize the HW to use it.

Quoted 7 lines

quemu should have general code under the viommu driver that drives /dev/ioasid to create PASID's and manage the IO mapping according to the guest's needs.

Drivers like VDPA and VFIO should simply accept that PASID and configure/authorize their HW to do DMA's with its tag.

I agree with you on consolidating things in one place (especially for the general SVA support). But here I was referring to an usage without pgtable binding (Possibly Jason. W can say more here), where the userspace just wants to allocate PASIDs, program/accept PASIDs to various workqueues (device specific), and then use MAP/UNMAP interface to manage address spaces associated with each PASID. I just wanted to point out that the latter two steps are through VFIO/VDPA specific interfaces.

Thanks
Kevin

#212 lore raw

RE: [PATCH V4 05/18] iommu/ioasid: Redefine IOASID set and allocation APIs

In reply to Jason Gunthorpe
Quoted 50 lines

From: Tian, Kevin
Sent: Friday, April 2, 2021 3:58 PM

Quoted 13 lines

From: Jason Gunthorpe <[email protected]>
Sent: Thursday, April 1, 2021 9:47 PM

Quoted 9 lines · Yi L
Quoted 9 lines

From: Jason Gunthorpe <[email protected]>
Sent: Thursday, April 1, 2021 9:16 PM

Quoted 5 lines · Yi L
Quoted 2 lines

From: Jason Gunthorpe <[email protected]>
Sent: Thursday, April 1, 2021 7:47 PM

[...]

Quoted 2 lines

I'm worried Intel views the only use of PASID in a guest is with ENQCMD, but that is not consistent with the industry. We need to

see

Quoted 9 lines
Quoted 9 lines
Quoted 8 lines
Quoted 4 lines
Quoted 1 line

normal nested PASID support with assigned PCI VFs.

I'm not quire flow here. Intel also allows PASID usage in guest without ENQCMD. e.g. Passthru a PF to guest, and use PASID on it without

ENQCMD.

Then you need all the parts, the hypervisor calls from the vIOMMU, and you can't really use a vPASID.

Quoted 8 lines
Quoted 1 line

This is a diagram shows the vSVA setup.

I'm not talking only about vSVA. Generic PASID support with arbitary mappings.

And how do you deal with the vPASID vs pPASID issue if the system has a mix of physical devices and mdevs?

We plan to support two schemes. One is vPASID identity-mapped to pPASID then the mixed scenario just works, with the limitation of lacking of live migration support. The other is non-identity-mapped scheme, where live migration is supported but physical devices and mdevs should not be mixed in one VM if both expose SVA capability (requires some filtering check in Qemu). Although we have some idea relaxing this restriction in the non-identity scheme, it requires more thinking given how the vSVA uAPI is being refactored.

In both cases the virtual VT-d will report a virtual capability to the guest, indicating that the guest must request PASID through a vcmd register instead of creating its own namespace. The vIOMMU returns a vPASID to the guest upon request. The vPASID could be directly mapped to a pPASID or allocated from a new namespace based on user configuration.

Quoted 5 lines

We hope the /dev/ioasid can support both schemes, with the minimal requirement of allowing userspace to tag a vPASID to a pPASID and allowing mdev to translate vPASID into pPASID, i.e. not assuming that the guest will always use pPASID.

Per your comments in other threads I suppose this requirement should be implemented in VFIO_ALLOW_PASID command instead of going through /dev/ioasid which only needs to know pPASID and its pgtable management.

Thanks
Kevin

#45 lore raw

RE: [PATCH V4 05/18] iommu/ioasid: Redefine IOASID set and allocation APIs

In reply to Jason Gunthorpe

Hi Jason,

Quoted 85 lines

From: Jason Gunthorpe <[email protected]>
Sent: Thursday, April 1, 2021 7:54 PM

Quoted 1 line · Yi L

After reading your reply in https://lore.kernel.org/linux-

iommu/[email protected]/#t

Quoted 1 line

So you mean /dev/ioasid FD is per-VM instead of per-ioasid, so above

skeleton

Quoted 1 line

doesn't suit your idea.

You can do it one PASID per FD or multiple PASID's per FD. Most likely we will have high numbers of PASID's in a qemu process so I assume that number of FDs will start to be a contraining factor, thus multiplexing is reasonable.

It doesn't really change anything about the basic flow.

digging deeply into it either seems like a reasonable choice.

Quoted 8 lines
+-----------------------------+-----------------------------------------------+
|      userspace              |               kernel space                    |
+-----------------------------+-----------------------------------------------+
| ioasid_fd =                 | /dev/ioasid does below:                       |
| open("/dev/ioasid", O_RDWR);|   struct ioasid_fd_ctx {                      |
|                             |       struct list_head ioasid_list;           |
|                             |       ...                                     |
|                             |   } ifd_ctx; // ifd_ctx is per ioasid_fd      |

Sure, possibly an xarray not a list

Quoted 4 lines
+-----------------------------+-----------------------------------------------+
| ioctl(ioasid_fd,            | /dev/ioasid does below:                       |
|       ALLOC, &ioasid);      |   struct ioasid_data {                        |
|                             |       ioasid_t ioasid;                        |
Quoted 8 lines
|                             |       struct list_head device_list;           |
|                             |       struct list_head next;                  |
|                             |       ...                                     |
|                             |   } id_data; // id_data is per ioasid         |
|                             |                                               |
|                             |   list_add(&id_data.next,                     |
|                             |            &ifd_ctx.ioasid_list);
|

Yes, this should have a kref in it too

Quoted 8 lines
+-----------------------------+-----------------------------------------------+
| ioctl(device_fd,            | VFIO does below:                              |
|       DEVICE_ALLOW_IOASID,  | 1) get ioasid_fd, check if ioasid_fd is valid |
|       ioasid_fd,            | 2) check if ioasid is allocated from ioasid_fd|
|       ioasid);              | 3) register device/domain info to /dev/ioasid |
|                             |    tracked in id_data.device_list             |
|                             | 4) record the ioasid in VFIO's per-device     |
|                             |    ioasid list for future security check      |

You would provide a function that does steps 1&2 look at eventfd for instance.

I'm not sure we need to register the device with the ioasid. device should incr the kref on the ioasid_data at this point.

Quoted 6 lines
+-----------------------------+-----------------------------------------------+
| ioctl(ioasid_fd,            | /dev/ioasid does below:                       |
|       BIND_PGTBL,           | 1) find ioasid's id_data                      |
|       pgtbl_data,           | 2) loop the id_data.device_list and tell iommu|
|       ioasid);              |    give ioasid access to the devices
|

This seems backwards, DEVICE_ALLOW_IOASID should tell the iommu to give the ioasid to the device.

Here the ioctl should be about assigning a memory map from the the current
mm_struct to the pasid

Quoted 5 lines
+-----------------------------+-----------------------------------------------+
| ioctl(ioasid_fd,            | /dev/ioasid does below:                       |
|       UNBIND_PGTBL,         | 1) find ioasid's id_data                      |
|       ioasid);              | 2) loop the id_data.device_list and tell iommu|
|                             |    clear ioasid access to the devices         |

Also seems backwards. The ioctl here should be 'destroy ioasid' which wipes out the page table, halts DMA access and parks the PASID until all users are done.

Quoted 8 lines
Quoted 6 lines
+-----------------------------+-----------------------------------------------+
| ioctl(device_fd,            | VFIO does below:                              |
|      DEVICE_DISALLOW_IOASID,| 1) check if ioasid is associated in VFIO's    |
|       ioasid_fd,            |    device ioasid list.                        |
|       ioasid);              | 2) unregister device/domain info from         |
|                             |    /dev/ioasid, clear in id_data.device_list  |

This should disconnect the iommu and kref_put the ioasid_data

thanks for the comments, updated the skeleton a little bit, accepted your Xarray and kref suggestion.

+-----------------------------+------------------------------------------------+
|      userspace              |               kernel space                     |
+-----------------------------+------------------------------------------------+
| ioasid_fd =                 | /dev/ioasid does below:                        |
| open("/dev/ioasid", O_RDWR);|   struct ioasid_fd_ctx {                       |
|                             |        struct xarray xa;                       |
|                             |       ...                                      |
|                             |   } ifd_ctx; // ifd_ctx is per ioasid_fd       |
+-----------------------------+------------------------------------------------+
| ioctl(ioasid_fd,            | /dev/ioasid does below:                        |
|       ALLOC, &ioasid);      |   struct ioasid_data {                         |
|                             |       ioasid_t ioasid;                         |
|                             |       refcount_t refs;                         |
|                             |       ...                                      |
|                             |   } id_data; // id_data is per ioasid          |
|                             |                                                |
|                             |   refcount_set(&id_data->refs, 1);             |
+-----------------------------+------------------------------------------------+
| ioctl(device_fd,            | VFIO does below:                               |
|       DEVICE_ALLOW_IOASID,  | 1) get ioasid_fd, check if ioasid_fd is valid  |
|       ioasid_fd,            | 2) check if ioasid is allocated from ioasid_fd |
|       ioasid);              | 3) inr refcount on the ioasid                  |
|                             | 4) tell iommu to give the ioasid to the device |
|                             |    by an iommu API. iommu driver needs to      |
|                             |    store the ioasid/device info in a per       |
|                             |    ioasid allow device list                    |
|                             | 5) record the ioasid in VFIO's per-device      |
|                             |    ioasid list for future security check       |
+-----------------------------+------------------------------------------------+
| ioctl(ioasid_fd,            | /dev/ioasid does below:                        |
|       BIND_PGTBL,           | 1) find ioasid's id_data                       |
|       pgtbl_data,           | 2) call into iommu driver with ioasid, pgtbl   |
|       ioasid);              |    data, iommu driver setup the PASID entry[1] |
|                             |    with the ioasid and the pgtbl_data          |
+-----------------------------+------------------------------------------------+
| ioctl(ioasid_fd,            | /dev/ioasid does below:                        |
|       CAHCE_INVLD,          | 1) find ioasid's id_data                       |
|       inv_data,             | 2) call into iommu driver with ioasid, inv     |
|       ioasid);              |    data, iommu driver invalidates cache        |
+-----------------------------+------------------------------------------------+
| ioctl(ioasid_fd,            | /dev/ioasid does below:                        |
|       UNBIND_PGTBL,         | 1) find ioasid's id_data                       |
|       ioasid);              | 2) call into iommu driver with ioasid, iommu   |
|                             |    driver destroy the PASID entry to block DMA |
|                             |    with this ioasid from device                |
+-----------------------------+------------------------------------------------+
| ioctl(device_fd,            | VFIO does below:                               |
|      DEVICE_DISALLOW_IOASID,| 1) check if ioasid is associated in VFIO's     |
|       ioasid_fd,            |    device ioasid list                          |
|       ioasid);              | 2) tell iommu driver to clear the device from  |
|                             |    its per-ioasid device allow list            |
|                             | 3) put refcount on the ioasid                  |
+-----------------------------+------------------------------------------------+
| ioctl(ioasid_fd,            | /dev/ioasid does below:                        |
|       FREE, ioasid);        |  list_del(&id_data.next);                      |
+-----------------------------+------------------------------------------------+

[1] PASID entry is an entry in a per-device PASID table, this is where the page table pointer is stored. e.g. guest cr3 page table pointer. Setup PASID entry in a device's PASID table means the access is finally grant in IOMMU side.

I kept FREE as it seems to be more symmetric since there is an ALLOC exposed to userspace. But yeah, I'm open with removing it all the same if it's really unnecessary per your opinion.

Need your help again on an open.
The major purpose of this series is to support vSVA for guest based on nested translation. And there is another usage case which is also based on nested translation but don't have an ioasid. And still, it needs the bind/unbind_pgtbl, cache_invalidation uAPI. It is gIOVA support. In this usage, page table is a guest IOVA page table, VMM needs to bind this page table to host and enabled nested translation, also needs to do cache invalidation when guest IOVA page table has changes. It's very similar with the page table bind of vSVA. Only difference is there is no ioasid in the gIOVA case. Instead, gIOVA case requires device information. But with regards to the uAPI reusing, need to fit gIOVA to /dev/ioasid model. As of now, I think it may require user space passes a device FD to the BIND/UNBIND_PGTBL and CAHCE_INVLD ioctl, then iommu driver can bind the gIOVA page table to a correct device. Not sure if it looks good. Do you have any suggestion on it?

[...]

Quoted 5 lines

Include a sequence showing how the kvm FD is used to program the vPASID to pPASID table that ENQCMD uses.

Show how dynamic authorization works based on requests from the guest's vIOMMU

I'd like to see if the updated skeleton suits your idea first, then draw a more complete flow to show this.

Regards,
Yi Liu

Quoted 1 line

Jason

#53 lore raw

Re: [PATCH V4 05/18] iommu/ioasid: Redefine IOASID set and allocation APIs

In reply to Tian, Kevin
Quoted 52 lines · Kevin
Quoted 41 lines

From: Jason Gunthorpe <[email protected]>
Sent: Friday, April 2, 2021 12:04 AM

Quoted 1 line · Yi L

DMA page faults are delivered to root-complex via page request message

and

Quoted 4 lines

it is per-device according to PCIe spec. Page request handling flow is:

1) iommu driver receives a page request from device
2) iommu driver parses the page request message. Get the RID,PASID,

faulted

Quoted 3 lines

page and requested permissions etc.
3) iommu driver triggers fault handler registered by device driver with iommu_report_device_fault()

This seems confused.

The PASID should define how to handle the page fault, not the driver.

I don't remember any device specific actions in ATS, so what is the driver supposed to do?

Quoted 3 lines

4) device driver's fault handler signals an event FD to notify userspace to fetch the information about the page fault. If it's VM case, inject the page fault to VM and let guest to solve it.

If the PASID is set to 'report page fault to userspace' then some event should come out of /dev/ioasid, or be reported to a linked eventfd, or whatever.

If the PASID is set to 'SVM' then the fault should be passed to handle_mm_fault

And so on.

Userspace chooses what happens based on how they configure the PASID through /dev/ioasid.

Why would a device driver get involved here?

Quoted 8 lines
Quoted 3 lines

Eric has sent below series for the page fault reporting for VM with passthru device.
https://lore.kernel.org/kvm/20210223210625.604517-5-

[email protected]/

It certainly should not be in vfio pci. Everything using a PASID needs this infrastructure, VDPA, mdev, PCI, CXL, etc.

This touches an interesting fact:

Quoted 11 lines

The fault may be triggered in either 1st-level or 2nd-level page table, when nested translation is enabled (in vSVA case). The 1st-level is bound by the user space, which therefore needs to receive the fault event. The 2nd-level is managed by VFIO (or vDPA), which needs to fix the fault in kernel (e.g. find HVA per faulting GPA, call handle_mm_fault and map GPA->HPA to IOMMU). Yi's current proposal lets VFIO to register the device fault handler, which then forward the event through /dev/ioasid to userspace only if it is a 1st-level fault. Are you suggesting a pgtable- centric fault reporting mechanism to separate handlers in each level, i.e. letting VFIO register handler only for 2nd-level fault and then /dev/ ioasid register handler for 1st-level fault?

This I'm struggling to understand. /dev/ioasid should handle all the faults cases, why would VFIO ever get involved in a fault? What would it even do?

If the fault needs to be fixed in the hypervisor then it is a kernel fault and it does handle_mm_fault. This absolutely should not be in VFIO or VDPA

If the fault needs to be fixed in the guest, then it needs to be delivered over /dev/ioasid in some way and injected into the vIOMMU. VFIO and VDPA have nothing to do with vIOMMU driver in quemu.

You need to have an interface under /dev/ioasid to create both page table levels and part of that will be to tell the kernel what VA is mapped and how to handle faults.

VFIO/VDPA do *nothing* more than authorize the physical device to use the given PASID.

In the VDPA case you might need to have SW access to the PASID, but that should be provided by a generic iommu layer interface like 'copy_to/from_pasid()' not by involving VDPA in the address mapping.

Jason

#206 lore raw

Re: [PATCH V4 05/18] iommu/ioasid: Redefine IOASID set and allocation APIs

In reply to Tian, Kevin
Quoted 31 lines · Kevin
Quoted 22 lines

From: Jason Gunthorpe <[email protected]>
Sent: Thursday, April 1, 2021 9:47 PM

Quoted 18 lines · Yi L
Quoted 17 lines

From: Jason Gunthorpe <[email protected]>
Sent: Thursday, April 1, 2021 9:16 PM

Quoted 9 lines · Yi L
Quoted 2 lines

From: Jason Gunthorpe <[email protected]>
Sent: Thursday, April 1, 2021 7:47 PM

[...]

Quoted 3 lines

I'm worried Intel views the only use of PASID in a guest is with ENQCMD, but that is not consistent with the industry. We need to see normal nested PASID support with assigned PCI VFs.

I'm not quire flow here. Intel also allows PASID usage in guest without ENQCMD. e.g. Passthru a PF to guest, and use PASID on it without

ENQCMD.

Then you need all the parts, the hypervisor calls from the vIOMMU, and you can't really use a vPASID.

Quoted 8 lines
Quoted 1 line

This is a diagram shows the vSVA setup.

I'm not talking only about vSVA. Generic PASID support with arbitary mappings.

And how do you deal with the vPASID vs pPASID issue if the system has a mix of physical devices and mdevs?

Quoted 6 lines

We plan to support two schemes. One is vPASID identity-mapped to pPASID then the mixed scenario just works, with the limitation of lacking of live migration support. The other is non-identity-mapped scheme, where live migration is supported but physical devices and mdevs should not be mixed in one VM if both expose SVA capability (requires some filtering check in Qemu).

That just becomes "block vPASID support if any device that doesn't use ENQCMD is plugged into the guest"

Which needs a special VFIO capability of some kind so qemu knows to block it. This really needs to all be layed out together so someone can understand it :(

Why doesn't the siov cookbook explaining this stuff??

Quoted 4 lines

We hope the /dev/ioasid can support both schemes, with the minimal requirement of allowing userspace to tag a vPASID to a pPASID and allowing mdev to translate vPASID into pPASID, i.e. not assuming that the guest will always use pPASID.

What I'm a unclear of is if /dev/ioasid even needs to care about vPASID or if vPASID is just a hidden artifact of the KVM connection to setup the translation table and the vIOMMU driver in qemu.

Since the physical HW never sees the vPASID I'm inclined to think the latter.

Jason

#214 lore raw

Re: [PATCH V4 05/18] iommu/ioasid: Redefine IOASID set and allocation APIs

In reply to Tian, Kevin
Quoted 30 lines · Kevin
Quoted 22 lines

From: Jason Gunthorpe <[email protected]>
Sent: Tuesday, March 30, 2021 9:29 PM

Quoted 7 lines

First, userspace may use ioasid in a non-SVA scenario where ioasid is bound to specific security context (e.g. a control vq in vDPA) instead of tying to mm. In this case there is no pgtable binding initiated from user space. Instead, ioasid is allocated from /dev/ioasid and then programmed to the intended security context through specific passthrough framework which manages that context.

This sounds like the exact opposite of what I'd like to see.

I do not want to see every subsystem gaining APIs to program a PASID. All of that should be consolidated in *one place*.

I do not want to see VDPA and VFIO have two nearly identical sets of APIs to control the PASID.

Drivers consuming a PASID, like VDPA, should consume the PASID and do nothing more than authorize the HW to use it.

Quoted 7 lines

quemu should have general code under the viommu driver that drives /dev/ioasid to create PASID's and manage the IO mapping according to the guest's needs.

Drivers like VDPA and VFIO should simply accept that PASID and configure/authorize their HW to do DMA's with its tag.

Quoted 8 lines

I agree with you on consolidating things in one place (especially for the general SVA support). But here I was referring to an usage without pgtable binding (Possibly Jason. W can say more here), where the userspace just wants to allocate PASIDs, program/accept PASIDs to various workqueues (device specific), and then use MAP/UNMAP interface to manage address spaces associated with each PASID. I just wanted to point out that the latter two steps are through VFIO/VDPA specific interfaces.

No, don't do that.

VFIO and VDPA has no buisness having map/unmap interfaces once we have /dev/ioasid. That all belongs in the iosaid side.

I know they have those interfaces today, but that doesn't mean we have to keep using them for PASID use cases, they should be replaced with a 'do dma from this pasid on /dev/ioasid' interface certainly not a 'here is a pasid from /dev/ioasid, go ahead and configure it youself' interface

This is because PASID is *complicated* in the general case! For instance all the two level stuff you are talking about must not leak into every user!

Jason

#54 lore raw

RE: [PATCH V4 05/18] iommu/ioasid: Redefine IOASID set and allocation APIs

In reply to Jason Gunthorpe
Quoted 71 lines

From: Jason Gunthorpe <[email protected]>
Sent: Tuesday, April 6, 2021 7:35 AM

Quoted 6 lines · Kevin
Quoted 6 lines

From: Jason Gunthorpe <[email protected]>
Sent: Friday, April 2, 2021 12:04 AM

Quoted 1 line · Yi L

DMA page faults are delivered to root-complex via page request

message

Quoted 10 lines
Quoted 10 lines

and

Quoted 4 lines

it is per-device according to PCIe spec. Page request handling flow is:

1) iommu driver receives a page request from device
2) iommu driver parses the page request message. Get the RID,PASID,

faulted

Quoted 3 lines

page and requested permissions etc.
3) iommu driver triggers fault handler registered by device driver with iommu_report_device_fault()

Quoted 8 lines
Quoted 8 lines

This seems confused.

The PASID should define how to handle the page fault, not the driver.

I don't remember any device specific actions in ATS, so what is the driver supposed to do?

Quoted 1 line

4) device driver's fault handler signals an event FD to notify userspace

to

Quoted 10 lines
Quoted 10 lines
Quoted 2 lines

fetch the information about the page fault. If it's VM case, inject the page fault to VM and let guest to solve it.

If the PASID is set to 'report page fault to userspace' then some event should come out of /dev/ioasid, or be reported to a linked eventfd, or whatever.

If the PASID is set to 'SVM' then the fault should be passed to handle_mm_fault

Quoted 8 lines
Quoted 8 lines

And so on.

Userspace chooses what happens based on how they configure the PASID through /dev/ioasid.

Why would a device driver get involved here?

Quoted 1 line

Eric has sent below series for the page fault reporting for VM with

passthru

Quoted 10 lines
Quoted 7 lines
Quoted 2 lines

device.
https://lore.kernel.org/kvm/20210223210625.604517-5-

[email protected]/

It certainly should not be in vfio pci. Everything using a PASID needs this infrastructure, VDPA, mdev, PCI, CXL, etc.

This touches an interesting fact:

Quoted 11 lines

The fault may be triggered in either 1st-level or 2nd-level page table, when nested translation is enabled (in vSVA case). The 1st-level is bound by the user space, which therefore needs to receive the fault event. The 2nd-level is managed by VFIO (or vDPA), which needs to fix the fault in kernel (e.g. find HVA per faulting GPA, call handle_mm_fault and map GPA->HPA to IOMMU). Yi's current proposal lets VFIO to register the device fault handler, which then forward the event through /dev/ioasid to userspace only if it is a 1st-level fault. Are you suggesting a pgtable- centric fault reporting mechanism to separate handlers in each level, i.e. letting VFIO register handler only for 2nd-level fault and then /dev/ ioasid register handler for 1st-level fault?

Quoted 7 lines

This I'm struggling to understand. /dev/ioasid should handle all the faults cases, why would VFIO ever get involved in a fault? What would it even do?

If the fault needs to be fixed in the hypervisor then it is a kernel fault and it does handle_mm_fault. This absolutely should not be in VFIO or VDPA

With nested translation it is GVA->GPA->HPA. The kernel needs to fix fault related to GPA->HPA (managed by VFIO/VDPA) while handle_mm_fault only handles HVA->HPA. In this case, the 2nd-level page fault is expected to be delivered to VFIO/VDPA first which then find HVA related to GPA, call handle_mm_fault to fix HVA->HPA, and then call iommu_map to fix GPA->HPA in the IOMMU page table. This is exactly like how CPU EPT violation is handled.

Quoted 8 lines

If the fault needs to be fixed in the guest, then it needs to be delivered over /dev/ioasid in some way and injected into the vIOMMU. VFIO and VDPA have nothing to do with vIOMMU driver in quemu.

You need to have an interface under /dev/ioasid to create both page table levels and part of that will be to tell the kernel what VA is mapped and how to handle faults.

VFIO/VDPA already have their own interface to manage GPA->HPA mappings. Why do we want to duplicate it in /dev/ioasid?

Thanks
Kevin

#207 lore raw

RE: [PATCH V4 05/18] iommu/ioasid: Redefine IOASID set and allocation APIs

In reply to Jason Gunthorpe
Quoted 45 lines

From: Jason Gunthorpe <[email protected]>
Sent: Tuesday, April 6, 2021 7:40 AM

Quoted 7 lines · Kevin
Quoted 7 lines

From: Jason Gunthorpe <[email protected]>
Sent: Thursday, April 1, 2021 9:47 PM

Quoted 3 lines · Yi L
Quoted 3 lines

From: Jason Gunthorpe <[email protected]>
Sent: Thursday, April 1, 2021 9:16 PM

Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines · Yi L
Quoted 5 lines
Quoted 2 lines

From: Jason Gunthorpe <[email protected]>
Sent: Thursday, April 1, 2021 7:47 PM

[...]

Quoted 2 lines

I'm worried Intel views the only use of PASID in a guest is with ENQCMD, but that is not consistent with the industry. We need to

see

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 1 line

normal nested PASID support with assigned PCI VFs.

I'm not quire flow here. Intel also allows PASID usage in guest

without

Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 1 line

ENQCMD. e.g. Passthru a PF to guest, and use PASID on it without

ENQCMD.

Then you need all the parts, the hypervisor calls from the vIOMMU,

and

Quoted 11 lines
Quoted 10 lines
Quoted 3 lines
Quoted 1 line

you can't really use a vPASID.

This is a diagram shows the vSVA setup.

I'm not talking only about vSVA. Generic PASID support with arbitary mappings.

And how do you deal with the vPASID vs pPASID issue if the system has a mix of physical devices and mdevs?

Quoted 6 lines

We plan to support two schemes. One is vPASID identity-mapped to pPASID then the mixed scenario just works, with the limitation of lacking of live migration support. The other is non-identity-mapped scheme, where live migration is supported but physical devices and mdevs should not be mixed in one VM if both expose SVA capability (requires some filtering check in Qemu).

Quoted 2 lines

That just becomes "block vPASID support if any device that doesn't use ENQCMD is plugged into the guest"

The limitation is only for physical device. and in reality it is not that bad. To support live migration with physical device we anyway need additional work to migrate the device state (e.g. based on Max's work), then it's not unreasonable to also mediate guest programming of device specific PASID register to enable vPASID (need to translate in the whole VM lifespan but likely is not a hot path).

Quoted 4 lines

Which needs a special VFIO capability of some kind so qemu knows to block it. This really needs to all be layed out together so someone can understand it :(

Or could simply based on whether the VFIO device supports live migration.

Quoted 11 lines

Why doesn't the siov cookbook explaining this stuff??

Quoted 4 lines

We hope the /dev/ioasid can support both schemes, with the minimal requirement of allowing userspace to tag a vPASID to a pPASID and allowing mdev to translate vPASID into pPASID, i.e. not assuming that the guest will always use pPASID.

What I'm a unclear of is if /dev/ioasid even needs to care about vPASID or if vPASID is just a hidden artifact of the KVM connection to setup the translation table and the vIOMMU driver in qemu.

Not just for KVM. Also required by mdev, which needs to translate vPASID into pPASID when ENQCMD is not used. As I replied in another mail, possibly we don't need /dev/ioasid to know this fact, which should only care about the operations related to pPASID. VFIO could carry vPASID information to mdev. KVM should have its own interface to know this information, as you suggested earlier.

Thanks
Kevin

#215 lore raw

RE: [PATCH V4 05/18] iommu/ioasid: Redefine IOASID set and allocation APIs

In reply to Jason Gunthorpe
Quoted 56 lines

From: Jason Gunthorpe
Sent: Tuesday, April 6, 2021 7:43 AM

Quoted 8 lines · Kevin
Quoted 8 lines

From: Jason Gunthorpe <[email protected]>
Sent: Tuesday, March 30, 2021 9:29 PM

Quoted 5 lines

First, userspace may use ioasid in a non-SVA scenario where ioasid is bound to specific security context (e.g. a control vq in vDPA) instead of tying to mm. In this case there is no pgtable binding initiated from user space. Instead, ioasid is allocated from /dev/ioasid and then

programmed

Quoted 1 line
Quoted 1 line
Quoted 1 line

to the intended security context through specific passthrough

framework

Quoted 21 lines
Quoted 13 lines
Quoted 1 line

which manages that context.

This sounds like the exact opposite of what I'd like to see.

I do not want to see every subsystem gaining APIs to program a PASID. All of that should be consolidated in *one place*.

I do not want to see VDPA and VFIO have two nearly identical sets of APIs to control the PASID.

Drivers consuming a PASID, like VDPA, should consume the PASID and do nothing more than authorize the HW to use it.

Quoted 7 lines

quemu should have general code under the viommu driver that drives /dev/ioasid to create PASID's and manage the IO mapping according to the guest's needs.

Drivers like VDPA and VFIO should simply accept that PASID and configure/authorize their HW to do DMA's with its tag.

Quoted 8 lines

I agree with you on consolidating things in one place (especially for the general SVA support). But here I was referring to an usage without pgtable binding (Possibly Jason. W can say more here), where the userspace just wants to allocate PASIDs, program/accept PASIDs to various workqueues (device specific), and then use MAP/UNMAP interface to manage address spaces associated with each PASID. I just wanted to point out that the latter two steps are through VFIO/VDPA specific interfaces.

No, don't do that.

VFIO and VDPA has no buisness having map/unmap interfaces once we have /dev/ioasid. That all belongs in the iosaid side.

I know they have those interfaces today, but that doesn't mean we have to keep using them for PASID use cases, they should be replaced with a 'do dma from this pasid on /dev/ioasid' interface certainly not a 'here is a pasid from /dev/ioasid, go ahead and configure it youself' interface

Quoted 4 lines

This is because PASID is *complicated* in the general case! For instance all the two level stuff you are talking about must not leak into every user!

Hi, Jason,

I didn't get your last comment how the two level stuff is leaked into every user. Could you elaborate it a bit?

and here is one example why using existing VFIO/VDPA interface makes sense. say dev1 (w/ sva) and dev2 (w/o sva) are placed in a single VFIO container. The container is associated to an iommu domain which contains a single 2nd-level page table, shared by both devices (when attached to the domain). The VFIO MAP operation is applied to the 2nd-level page table thus naturally applied to both devices. Then userspace could use /dev/ioasid to further allocate IOASIDs and bind multiple 1st-level page tables for dev1, nested on the shared 2nd-level page table.

If following your suggestion then VFIO must deny VFIO MAP operations on sva1 (assume userspace should not mix sva1 and sva2 in the same container and instead use /dev/ioasid to map for sva1)? and even for a sva-capable device there is a window before the guest actually enables sva on that device then VFIO should still accept MAP in that window and then deny it after sva is enabled by the guest? This all sounds unnecessary complex while there is already a clean way to achieve it...

Thanks
Kevin

#221 lore raw

Re: [PATCH V4 05/18] iommu/ioasid: Redefine IOASID set and allocation APIs

In reply to Jason Gunthorpe

ÔÚ 2021/4/6 ÉÏÎç7:42, Jason Gunthorpe дµÀ:

Quoted 38 lines · Kevin
Quoted 27 lines
Quoted 20 lines

From: Jason Gunthorpe <[email protected]>
Sent: Tuesday, March 30, 2021 9:29 PM

Quoted 6 lines

First, userspace may use ioasid in a non-SVA scenario where ioasid is bound to specific security context (e.g. a control vq in vDPA) instead of tying to mm. In this case there is no pgtable binding initiated from user space. Instead, ioasid is allocated from /dev/ioasid and then programmed to the intended security context through specific passthrough framework which manages that context.

This sounds like the exact opposite of what I'd like to see.

I do not want to see every subsystem gaining APIs to program a PASID. All of that should be consolidated in *one place*.

I do not want to see VDPA and VFIO have two nearly identical sets of APIs to control the PASID.

Drivers consuming a PASID, like VDPA, should consume the PASID and do nothing more than authorize the HW to use it.

Quoted 7 lines

quemu should have general code under the viommu driver that drives /dev/ioasid to create PASID's and manage the IO mapping according to the guest's needs.

Drivers like VDPA and VFIO should simply accept that PASID and configure/authorize their HW to do DMA's with its tag.

Quoted 8 lines

I agree with you on consolidating things in one place (especially for the general SVA support). But here I was referring to an usage without pgtable binding (Possibly Jason. W can say more here), where the userspace just wants to allocate PASIDs, program/accept PASIDs to various workqueues (device specific), and then use MAP/UNMAP interface to manage address spaces associated with each PASID. I just wanted to point out that the latter two steps are through VFIO/VDPA specific interfaces.

No, don't do that.

Quoted 8 lines

VFIO and VDPA has no buisness having map/unmap interfaces once we have /dev/ioasid. That all belongs in the iosaid side.

I know they have those interfaces today, but that doesn't mean we have to keep using them for PASID use cases, they should be replaced with a 'do dma from this pasid on /dev/ioasid' interface certainly not a 'here is a pasid from /dev/ioasid, go ahead and configure it youself' interface

So it looks like the PASID was bound to SVA in this design. I think it's not necessairly the case:

1) PASID can be implemented without SVA, in this case a map/unmap interface is still required
2) For the case that hypervisor want to do some mediation in the middle for a virtqueue. e.g in the case of control vq that is implemented in the VF/ADI/SF itself, the hardware virtqueue needs to be controlled by Qemu, Though binding qemu's page table to cvq can work but it looks like a overkill, a small dedicated buffers that is mapped for this PASID seems more suitalbe.

Quoted 6 lines

This is because PASID is *complicated* in the general case! For instance all the two level stuff you are talking about must not leak into every user!

Jason

So do you mean the device should not expose the PASID confiugration API to guest? I think it could happen if we assign the whole device and let guest to configure it for nested VMs.

Thanks

Quoted 1 line
#211 lore raw

RE: [PATCH V4 05/18] iommu/ioasid: Redefine IOASID set and allocation APIs

In reply to a message not in the archive
Quoted 60 lines

From: Tian, Kevin
Sent: Tuesday, April 6, 2021 9:02 AM

Quoted 17 lines

From: Jason Gunthorpe <[email protected]>
Sent: Tuesday, April 6, 2021 7:40 AM

Quoted 13 lines · Kevin
Quoted 13 lines

From: Jason Gunthorpe <[email protected]>
Sent: Thursday, April 1, 2021 9:47 PM

Quoted 9 lines · Yi L
Quoted 9 lines

From: Jason Gunthorpe <[email protected]>
Sent: Thursday, April 1, 2021 9:16 PM

Quoted 5 lines · Yi L
Quoted 2 lines

From: Jason Gunthorpe <[email protected]>
Sent: Thursday, April 1, 2021 7:47 PM

[...]

Quoted 2 lines

I'm worried Intel views the only use of PASID in a guest is with ENQCMD, but that is not consistent with the industry. We need

to

Quoted 28 lines

see

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 1 line

normal nested PASID support with assigned PCI VFs.

I'm not quire flow here. Intel also allows PASID usage in guest

without

Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 1 line

ENQCMD. e.g. Passthru a PF to guest, and use PASID on it without

ENQCMD.

Then you need all the parts, the hypervisor calls from the vIOMMU,

and

Quoted 11 lines
Quoted 10 lines
Quoted 3 lines
Quoted 1 line

you can't really use a vPASID.

This is a diagram shows the vSVA setup.

I'm not talking only about vSVA. Generic PASID support with arbitary mappings.

And how do you deal with the vPASID vs pPASID issue if the system has a mix of physical devices and mdevs?

Quoted 6 lines

We plan to support two schemes. One is vPASID identity-mapped to pPASID then the mixed scenario just works, with the limitation of lacking of live migration support. The other is non-identity-mapped scheme, where live migration is supported but physical devices and mdevs should not be mixed in one VM if both expose SVA capability (requires some filtering check in Qemu).

Quoted 2 lines

That just becomes "block vPASID support if any device that doesn't use ENQCMD is plugged into the guest"

The limitation is only for physical device. and in reality it is not that bad. To support live migration with physical device we anyway need additional work to migrate the device state (e.g. based on Max's work), then it's not unreasonable to also mediate guest programming of device specific PASID register to enable vPASID (need to translate in the whole VM lifespan but likely is not a hot path).

Quoted 1 line
Quoted 5 lines
Quoted 3 lines

Which needs a special VFIO capability of some kind so qemu knows to block it. This really needs to all be layed out together so someone can understand it :(

Or could simply based on whether the VFIO device supports live migration.

Actually you are right on this point. VFIO should provide a per-device capability to indicate whether vPASID is allowed on this device. likely yes for mdev, by default no for pdev (unless explicitly opt in). Qemu should enable vPASID only if all assigned devices support it, and then provide vPASID information when using VFIO API to allow pPASIDs.

Quoted 22 lines
Quoted 4 lines

Why doesn't the siov cookbook explaining this stuff??

Quoted 8 lines
Quoted 4 lines

We hope the /dev/ioasid can support both schemes, with the minimal requirement of allowing userspace to tag a vPASID to a pPASID and allowing mdev to translate vPASID into pPASID, i.e. not assuming that the guest will always use pPASID.

What I'm a unclear of is if /dev/ioasid even needs to care about vPASID or if vPASID is just a hidden artifact of the KVM connection to setup the translation table and the vIOMMU driver in qemu.

Not just for KVM. Also required by mdev, which needs to translate vPASID into pPASID when ENQCMD is not used. As I replied in another mail, possibly we don't need /dev/ioasid to know this fact, which should only care about the operations related to pPASID. VFIO could carry vPASID information to mdev. KVM should have its own interface to know this information, as you suggested earlier.

Thanks
Kevin

#55 lore raw

Re: [PATCH V4 05/18] iommu/ioasid: Redefine IOASID set and allocation APIs

In reply to Tian, Kevin
Quoted 7 lines · Kevin

With nested translation it is GVA->GPA->HPA. The kernel needs to fix fault related to GPA->HPA (managed by VFIO/VDPA) while handle_mm_fault only handles HVA->HPA. In this case, the 2nd-level page fault is expected to be delivered to VFIO/VDPA first which then find HVA related to GPA, call handle_mm_fault to fix HVA->HPA, and then call iommu_map to fix GPA->HPA in the IOMMU page table. This is exactly like how CPU EPT violation is handled.

No, it should all be in the /dev/ioasid layer not duplicated into every user.

Quoted 10 lines
Quoted 7 lines

If the fault needs to be fixed in the guest, then it needs to be delivered over /dev/ioasid in some way and injected into the vIOMMU. VFIO and VDPA have nothing to do with vIOMMU driver in quemu.

You need to have an interface under /dev/ioasid to create both page table levels and part of that will be to tell the kernel what VA is mapped and how to handle faults.

VFIO/VDPA already have their own interface to manage GPA->HPA mappings. Why do we want to duplicate it in /dev/ioasid?

They have their own interface to manage other types of HW, we should not duplicate PASID programming into there too.

Jason

#208 lore raw

Re: [PATCH V4 05/18] iommu/ioasid: Redefine IOASID set and allocation APIs

In reply to Tian, Kevin
Quoted 48 lines · Kevin
Quoted 45 lines

From: Jason Gunthorpe <[email protected]>
Sent: Tuesday, April 6, 2021 7:40 AM

Quoted 7 lines · Kevin
Quoted 7 lines

From: Jason Gunthorpe <[email protected]>
Sent: Thursday, April 1, 2021 9:47 PM

Quoted 3 lines · Yi L
Quoted 3 lines

From: Jason Gunthorpe <[email protected]>
Sent: Thursday, April 1, 2021 9:16 PM

Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines · Yi L
Quoted 5 lines
Quoted 2 lines

From: Jason Gunthorpe <[email protected]>
Sent: Thursday, April 1, 2021 7:47 PM

[...]

Quoted 2 lines

I'm worried Intel views the only use of PASID in a guest is with ENQCMD, but that is not consistent with the industry. We need to

see

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 1 line

normal nested PASID support with assigned PCI VFs.

I'm not quire flow here. Intel also allows PASID usage in guest

without

Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 1 line

ENQCMD. e.g. Passthru a PF to guest, and use PASID on it without

ENQCMD.

Then you need all the parts, the hypervisor calls from the vIOMMU,

and

Quoted 11 lines
Quoted 10 lines
Quoted 3 lines
Quoted 1 line

you can't really use a vPASID.

This is a diagram shows the vSVA setup.

I'm not talking only about vSVA. Generic PASID support with arbitary mappings.

And how do you deal with the vPASID vs pPASID issue if the system has a mix of physical devices and mdevs?

Quoted 6 lines

We plan to support two schemes. One is vPASID identity-mapped to pPASID then the mixed scenario just works, with the limitation of lacking of live migration support. The other is non-identity-mapped scheme, where live migration is supported but physical devices and mdevs should not be mixed in one VM if both expose SVA capability (requires some filtering check in Qemu).

Quoted 2 lines

That just becomes "block vPASID support if any device that doesn't use ENQCMD is plugged into the guest"

Quoted 6 lines

The limitation is only for physical device. and in reality it is not that bad. To support live migration with physical device we anyway need additional work to migrate the device state (e.g. based on Max's work), then it's not unreasonable to also mediate guest programming of device specific PASID register to enable vPASID (need to translate in the whole VM lifespan but likely is not a hot path).

IMHO that is pretty unreasonable.. More likely we end up with vPASID tables in each migratable device like KVM has.

Quoted 5 lines
Quoted 3 lines

Which needs a special VFIO capability of some kind so qemu knows to block it. This really needs to all be layed out together so someone can understand it :(

Or could simply based on whether the VFIO device supports live migration.

You need to define affirmative caps that indicate that vPASID will be supported by the VFIO device.

Quoted 7 lines
Quoted 7 lines

Why doesn't the siov cookbook explaining this stuff??

Quoted 4 lines

We hope the /dev/ioasid can support both schemes, with the minimal requirement of allowing userspace to tag a vPASID to a pPASID and allowing mdev to translate vPASID into pPASID, i.e. not assuming that the guest will always use pPASID.

Quoted 6 lines
Quoted 3 lines

What I'm a unclear of is if /dev/ioasid even needs to care about vPASID or if vPASID is just a hidden artifact of the KVM connection to setup the translation table and the vIOMMU driver in qemu.

Not just for KVM. Also required by mdev, which needs to translate vPASID into pPASID when ENQCMD is not used.

Do we have any mdev's that will do this?

Quoted 2 lines

should only care about the operations related to pPASID. VFIO could carry vPASID information to mdev.

It depends how common this is, I suppose

Jason

#216 lore raw

Re: [PATCH V4 05/18] iommu/ioasid: Redefine IOASID set and allocation APIs

In reply to Tian, Kevin
Quoted 4 lines · Kevin

and here is one example why using existing VFIO/VDPA interface makes sense. say dev1 (w/ sva) and dev2 (w/o sva) are placed in a single VFIO container.

Forget about SVA, it is an irrelevant detail of how a PASID is configured.

Quoted 3 lines

The container is associated to an iommu domain which contains a single 2nd-level page table, shared by both devices (when attached to the domain).

This level should be described by an ioasid.

Quoted 5 lines

The VFIO MAP operation is applied to the 2nd-level
page table thus naturally applied to both devices. Then userspace could use /dev/ioasid to further allocate IOASIDs and bind multiple 1st-level page tables for dev1, nested on the shared 2nd-level page table.

Because if you don't then we enter insane world where a PASID is being created under /dev/ioasid but its translation path flows through setup done by VFIO and the whole user API becomes an incomprehensible mess.

How will you even associate the PASID with the other translation??

The entire translation path for any ioasid or PASID should be defined only by /dev/ioasid. Everything else is a legacy API.

Quoted 3 lines

If following your suggestion then VFIO must deny VFIO MAP operations on sva1 (assume userspace should not mix sva1 and sva2 in the same container and instead use /dev/ioasid to map for sva1)?

No, userspace creates an iosaid for the guest physical mapping and passes this ioasid to VFIO PCI which will assign it as the first layer mapping on the RID

When PASIDs are allocated the uAPI will be told to logically nested under the first ioasid. When VFIO authorizes a PASID for a RID it checks that all the HW rules are being followed.

If there are rules like groups of VFIO devices must always use the same IOASID then VFIO will check these too (and realistically qemu will have only one guest physical map ioasid anyhow)

There is no real difference between setting up an IOMMU table for a (RID,PASID) tuple or just a RID. We can do it universally with one interface for all consumers.

I wanted this when we were doing VDPA for the first time, now that we are doing pasid and more difficult stuff I view it as essential.

Jason

#222 lore raw

Re: [PATCH V4 05/18] iommu/ioasid: Redefine IOASID set and allocation APIs

In reply to Jason Wang
Quoted 11 lines · Jason Wang
Quoted 8 lines

VFIO and VDPA has no buisness having map/unmap interfaces once we have /dev/ioasid. That all belongs in the iosaid side.

I know they have those interfaces today, but that doesn't mean we have to keep using them for PASID use cases, they should be replaced with a 'do dma from this pasid on /dev/ioasid' interface certainly not a 'here is a pasid from /dev/ioasid, go ahead and configure it youself' interface

So it looks like the PASID was bound to SVA in this design. I think it's not necessairly the case:

No, I wish people would stop talking about SVA.

SVA and vSVA are a very special narrow configuration of a PASID. There are lots of other PASID configurations! That is the whole point, a PASID is complicated, there are many configuration scenarios, they need to be in one place with a very clearly defined uAPI

Quoted 2 lines

1) PASID can be implemented without SVA, in this case a map/unmap interface is still required

Any interface to manipulate a PASID should be under /dev/ioasid. We do not want to duplicate this into every subsystem.

Quoted 6 lines

2) For the case that hypervisor want to do some mediation in the middle for a virtqueue. e.g in the case of control vq that is implemented in the VF/ADI/SF itself, the hardware virtqueue needs to be controlled by Qemu, Though binding qemu's page table to cvq can work but it looks like a overkill, a small dedicated buffers that is mapped for this PASID seems more suitalbe.

/dev/ioasid should allow userspace to setup any PASID configuration it wants. There are many choices. That is the whole point, instead of copying&pasting all the PASID configuration option into every subsystem we have on place to configure it.

If you want a PASID (or generic ioasid) that has the guest physical map, which is probably all that VDPA would ever want, then /dev/ioasid should be able to prepare that.

If you just want to map a few buffers into a PASID then it should be able to do that too.

Quoted 3 lines

So do you mean the device should not expose the PASID confiugration API to guest? I think it could happen if we assign the whole device and let guest to configure it for nested VMs.

This always needs co-operating with the vIOMMU driver. We can't have nested PASID use without both parts working together.

The vIOMMU driver configures the PASID and assigns the mappings (however complicated that turns out to be)

The VDPA/mdev driver authorizes the HW to use the ioasid mapping, eg by authorizing a queue to issue PCIe TLPs with a specific PASID.

The authorization is triggered by the guest telling the vIOMMU to allow a vRID to talk to a PASID, which qemu would have to translate to telling something like the VDPA driver under the vRID that it can use a PASID from /dev/ioasid

For security a VDPA/mdev device MUST NOT issue PASIDs that the vIOMMU has not authorized its vRID to use. Otherwise the security model of something like VFIO in the guest becomes completely broken.

Jason

#223 lore raw

Re: [PATCH V4 05/18] iommu/ioasid: Redefine IOASID set and allocation APIs

ÔÚ 2021/4/6 ÏÂÎç8:42, Jason Gunthorpe дµÀ:

Quoted 11 lines · Jason Wang
Quoted 10 lines
Quoted 9 lines

VFIO and VDPA has no buisness having map/unmap interfaces once we have /dev/ioasid. That all belongs in the iosaid side.

I know they have those interfaces today, but that doesn't mean we have to keep using them for PASID use cases, they should be replaced with a 'do dma from this pasid on /dev/ioasid' interface certainly not a 'here is a pasid from /dev/ioasid, go ahead and configure it youself' interface

Quoted 8 lines
Quoted 2 lines

So it looks like the PASID was bound to SVA in this design. I think it's not necessairly the case:

No, I wish people would stop talking about SVA.

SVA and vSVA are a very special narrow configuration of a PASID. There are lots of other PASID configurations! That is the whole point, a PASID is complicated, there are many configuration scenarios, they need to be in one place with a very clearly defined uAPI

Right, that's my understanding as well.

Quoted 5 lines
Quoted 3 lines

1) PASID can be implemented without SVA, in this case a map/unmap interface is still required

Any interface to manipulate a PASID should be under /dev/ioasid. We do not want to duplicate this into every subsystem.

Yes.

Quoted 31 lines
Quoted 7 lines

2) For the case that hypervisor want to do some mediation in the middle for a virtqueue. e.g in the case of control vq that is implemented in the VF/ADI/SF itself, the hardware virtqueue needs to be controlled by Qemu, Though binding qemu's page table to cvq can work but it looks like a overkill, a small dedicated buffers that is mapped for this PASID seems more suitalbe.

/dev/ioasid should allow userspace to setup any PASID configuration it wants. There are many choices. That is the whole point, instead of copying&pasting all the PASID configuration option into every subsystem we have on place to configure it.

If you want a PASID (or generic ioasid) that has the guest physical map, which is probably all that VDPA would ever want, then /dev/ioasid should be able to prepare that.

If you just want to map a few buffers into a PASID then it should be able to do that too.

Quoted 3 lines

So do you mean the device should not expose the PASID confiugration API to guest? I think it could happen if we assign the whole device and let guest to configure it for nested VMs.

This always needs co-operating with the vIOMMU driver. We can't have nested PASID use without both parts working together.

The vIOMMU driver configures the PASID and assigns the mappings (however complicated that turns out to be)

The VDPA/mdev driver authorizes the HW to use the ioasid mapping, eg by authorizing a queue to issue PCIe TLPs with a specific PASID.

Quoted 8 lines

The authorization is triggered by the guest telling the vIOMMU to allow a vRID to talk to a PASID, which qemu would have to translate to telling something like the VDPA driver under the vRID that it can use a PASID from /dev/ioasid

For security a VDPA/mdev device MUST NOT issue PASIDs that the vIOMMU has not authorized its vRID to use. Otherwise the security model of something like VFIO in the guest becomes completely broken.

Yes, that's how it should work.

Thanks

Quoted 3 lines

Jason

#217 lore raw

RE: [PATCH V4 05/18] iommu/ioasid: Redefine IOASID set and allocation APIs

In reply to Jason Gunthorpe
Quoted 24 lines

From: Jason Gunthorpe <[email protected]>
Sent: Tuesday, April 6, 2021 8:35 PM

Quoted 4 lines · Kevin

and here is one example why using existing VFIO/VDPA interface makes sense. say dev1 (w/ sva) and dev2 (w/o sva) are placed in a single VFIO container.

Forget about SVA, it is an irrelevant detail of how a PASID is configured.

Quoted 3 lines

The container is associated to an iommu domain which contains a single 2nd-level page table, shared by both devices (when attached to the domain).

This level should be described by an ioasid.

Quoted 5 lines

The VFIO MAP operation is applied to the 2nd-level
page table thus naturally applied to both devices. Then userspace could use /dev/ioasid to further allocate IOASIDs and bind multiple 1st-level page tables for dev1, nested on the shared 2nd-level page table.

Quoted 5 lines

Because if you don't then we enter insane world where a PASID is being created under /dev/ioasid but its translation path flows through setup done by VFIO and the whole user API becomes an incomprehensible mess.

How will you even associate the PASID with the other translation??

PASID is attached to a specific iommu domain (created by VFIO/VDPA), which has GPA->HPA mappings already configured. If we view that mapping as an attribute of the iommu domain, it's reasonable to have the userspace-bound pgtable through /dev/ioasid to nest on it.

Quoted 11 lines

The entire translation path for any ioasid or PASID should be defined only by /dev/ioasid. Everything else is a legacy API.

Quoted 3 lines

If following your suggestion then VFIO must deny VFIO MAP operations on sva1 (assume userspace should not mix sva1 and sva2 in the same container and instead use /dev/ioasid to map for sva1)?

No, userspace creates an iosaid for the guest physical mapping and passes this ioasid to VFIO PCI which will assign it as the first layer mapping on the RID

Is it an dummy ioasid just for providing GPA mappings for nesting purpose of other IOASIDs? Then we waste one per VM?

Quoted 4 lines

When PASIDs are allocated the uAPI will be told to logically nested under the first ioasid. When VFIO authorizes a PASID for a RID it checks that all the HW rules are being followed.

As I explained above, why cannot we just use iommu domain to connect the dots? Every passthrough framework needs to create an iommu domain first. and It needs to support both devices w/ PASID and devices w/o PASID. For devices w/o PASID it needs to invent its own MAP interface anyway. Then why do we bother creating another MAP interface through /dev/ioasid which not only duplicates but also creating transition burden between two set of MAP interfaces when the guest turns on/off the pasid capability on the device?

Quoted 9 lines

If there are rules like groups of VFIO devices must always use the same IOASID then VFIO will check these too (and realistically qemu will have only one guest physical map ioasid anyhow)

There is no real difference between setting up an IOMMU table for a (RID,PASID) tuple or just a RID. We can do it universally with one interface for all consumers.

'universally' upon from which angle you look at this problem. From IOASID p.o.v possibly yes, but from device passthrough p.o.v. it's the opposite since the passthrough framework needs to handle devices w/o PASID anyway (or even for device w/ PASID it could send traffic w/o PASID) thus 'universally' makes more sense if the passthrough framework can use one interface of its own to manage GPA mappings for all consumers (apply to the case when a PASID is allowed/authorized).

Thanks
Kevin

#209 lore raw

RE: [PATCH V4 05/18] iommu/ioasid: Redefine IOASID set and allocation APIs

In reply to Jason Gunthorpe
Quoted 62 lines

From: Jason Gunthorpe <[email protected]>
Sent: Tuesday, April 6, 2021 8:21 PM

Quoted 11 lines · Kevin
Quoted 11 lines

From: Jason Gunthorpe <[email protected]>
Sent: Tuesday, April 6, 2021 7:40 AM

Quoted 7 lines · Kevin
Quoted 7 lines

From: Jason Gunthorpe <[email protected]>
Sent: Thursday, April 1, 2021 9:47 PM

Quoted 3 lines · Yi L
Quoted 3 lines

From: Jason Gunthorpe <[email protected]>
Sent: Thursday, April 1, 2021 9:16 PM

Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines · Yi L
Quoted 5 lines
Quoted 2 lines

From: Jason Gunthorpe <[email protected]>
Sent: Thursday, April 1, 2021 7:47 PM

[...]

Quoted 2 lines

I'm worried Intel views the only use of PASID in a guest is with ENQCMD, but that is not consistent with the industry. We need

to

Quoted 6 lines
Quoted 6 lines

see

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 1 line

normal nested PASID support with assigned PCI VFs.

I'm not quire flow here. Intel also allows PASID usage in guest

without

Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

ENQCMD. e.g. Passthru a PF to guest, and use PASID on it

without

Quoted 4 lines
Quoted 4 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

ENQCMD.

Then you need all the parts, the hypervisor calls from the vIOMMU,

and

Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 3 lines
Quoted 1 line

you can't really use a vPASID.

This is a diagram shows the vSVA setup.

I'm not talking only about vSVA. Generic PASID support with arbitary mappings.

And how do you deal with the vPASID vs pPASID issue if the system

has

Quoted 13 lines
Quoted 10 lines
Quoted 9 lines
Quoted 2 lines

a mix of physical devices and mdevs?

We plan to support two schemes. One is vPASID identity-mapped to pPASID then the mixed scenario just works, with the limitation of lacking of live migration support. The other is non-identity-mapped scheme, where live migration is supported but physical devices and mdevs should not be mixed in one VM if both expose SVA capability (requires some filtering check in Qemu).

Quoted 2 lines

That just becomes "block vPASID support if any device that doesn't use ENQCMD is plugged into the guest"

Quoted 6 lines

The limitation is only for physical device. and in reality it is not that bad. To support live migration with physical device we anyway need additional work to migrate the device state (e.g. based on Max's work), then it's not unreasonable to also mediate guest programming of device specific PASID register to enable vPASID (need to translate in the whole VM lifespan but likely is not a hot path).

Quoted 2 lines

IMHO that is pretty unreasonable.. More likely we end up with vPASID tables in each migratable device like KVM has.

just like mdev needs to maintain allowed PASID list, this extends it to all migratable devices.

Quoted 9 lines
Quoted 6 lines
Quoted 4 lines

Which needs a special VFIO capability of some kind so qemu knows to block it. This really needs to all be layed out together so someone can understand it :(

Or could simply based on whether the VFIO device supports live migration.

You need to define affirmative caps that indicate that vPASID will be supported by the VFIO device.

Yes, this is required as I acked in another mail.

Quoted 8 lines
Quoted 8 lines
Quoted 8 lines

Why doesn't the siov cookbook explaining this stuff??

Quoted 4 lines

We hope the /dev/ioasid can support both schemes, with the minimal requirement of allowing userspace to tag a vPASID to a pPASID and allowing mdev to translate vPASID into pPASID, i.e. not assuming that the guest will always use pPASID.

Quoted 8 lines
Quoted 6 lines
Quoted 3 lines

What I'm a unclear of is if /dev/ioasid even needs to care about vPASID or if vPASID is just a hidden artifact of the KVM connection to setup the translation table and the vIOMMU driver in qemu.

Not just for KVM. Also required by mdev, which needs to translate vPASID into pPASID when ENQCMD is not used.

Do we have any mdev's that will do this?

definitely. Actually any mdev which doesn't do ENQCMD needs to do this. In normal case, the PASID is programmed to a MMIO register (or in-memory context) associate with the backend resource of the mdev. The value programmed from the guest is vPASID, thus must be translated into pPASID before updating the physical register.

Quoted 6 lines
Quoted 3 lines

should only care about the operations related to pPASID. VFIO could carry vPASID information to mdev.

It depends how common this is, I suppose

based on above I think it's a common case.

Thanks
Kevin

#224 lore raw

RE: [PATCH V4 05/18] iommu/ioasid: Redefine IOASID set and allocation APIs

In reply to Jason Gunthorpe
Quoted 18 lines

From: Jason Gunthorpe
Sent: Tuesday, April 6, 2021 8:43 PM

Quoted 1 line · Jason Wang
Quoted 1 line

VFIO and VDPA has no buisness having map/unmap interfaces once we

have

Quoted 10 lines
Quoted 7 lines

/dev/ioasid. That all belongs in the iosaid side.

I know they have those interfaces today, but that doesn't mean we have to keep using them for PASID use cases, they should be replaced with a 'do dma from this pasid on /dev/ioasid' interface certainly not a 'here is a pasid from /dev/ioasid, go ahead and configure it youself' interface

So it looks like the PASID was bound to SVA in this design. I think it's not necessairly the case:

Quoted 7 lines

No, I wish people would stop talking about SVA.

SVA and vSVA are a very special narrow configuration of a PASID. There are lots of other PASID configurations! That is the whole point, a PASID is complicated, there are many configuration scenarios, they need to be in one place with a very clearly defined uAPI

I feel it also makes sense to allow a subsystem to specify which configurations are permitted when allowing a PASID on its device, e.g. excluding things like GPA mappings that existing subsystems (VFIO/VDPA) already handle well:

- Share GPA mappings between multiple devices (w/ or w/o PASID) for better IOTLB efficiency;

- Share GPA mappings between transactions w/ PASID and transactions w/o PASID from the same device (e.g. GPU) for better IOTLB efficiency;

- Use the same page table for GPA mappings before and after the guest turns on/off the PASID capability;

All above are given as long as we continue to let VFIO/VDPA manage the iommu domain and associated GPA mappings for PASID. The IOMMU driver already ensures a nested PASID entry linking to the established GPA paging structure of the domain when the 1st-level pgtable is bound through /dev/ioasid.

In contrast, above merits are lost if forcing a model where GPA mappings for PASID must be constructed through /dev/ioasid, as this will lead to multiple paging structures for the same GPA mappings implying worse IOTLB usage and unnecessary cost of invalidations.

Therefore, I envision a scheme where the subsystem could specify permitted PASID configurations when doing ALLOW_PASID, and then userspace queries per-PASID capability to learn which operations are allowed, e.g.:

1) To enable vSVA, VFIO/VDPA allows pgtable binding and related invalidation/ fault ops through /dev/ioasid;

2) for vDPA control vq usage, no configuration is allowed through /dev/ioasid;

3) for new subsystem which doesn't carry any legacy or similar usage as VFIO/VDPA, it could permit all configurations through /dev/ioasid including 1st-level binding and 2nd-level mapping ops;

This approach also allows us to grow the uAPI in a staging approach. Now focus on 1) and 2) as VFIO/VDPA are the only two users for now with good legacy to cover the GPA mappings. More ops can be introduced for 3) when there is a real example to show what exact ops are required for such a new subsystem.

Is this a good strategy to move forward?

btw this discussion was raised when discussing the I/O page fault handling process. Currently the IOMMU layer implements a per-device fault reporting mechanism, which requires VFIO to register a handler to receive all faults on its device and then forwards to ioasid if it's due to 1st-level. Possibly it makes more sense to convert it into a per-pgtable reporting scheme, and then the owner of each pgtable should register its own handler. It means for 1) VFIO will register a 2nd-level pgtable handler while /dev/ioasid will register a 1st-level pgtable handler, while for 3) /dev/ioasid will register handlers for both 1st-level and 2nd-level pgtable. Jean? also want to know your thoughts...

Thanks
Kevin

#225 lore raw

Re: [PATCH V4 05/18] iommu/ioasid: Redefine IOASID set and allocation APIs

Quoted 20 lines · Kevin
Quoted 20 lines

From: Jason Gunthorpe
Sent: Tuesday, April 6, 2021 8:43 PM

Quoted 1 line · Jason Wang
Quoted 1 line

VFIO and VDPA has no buisness having map/unmap interfaces once we

have

Quoted 10 lines
Quoted 7 lines

/dev/ioasid. That all belongs in the iosaid side.

I know they have those interfaces today, but that doesn't mean we have to keep using them for PASID use cases, they should be replaced with a 'do dma from this pasid on /dev/ioasid' interface certainly not a 'here is a pasid from /dev/ioasid, go ahead and configure it youself' interface

So it looks like the PASID was bound to SVA in this design. I think it's not necessairly the case:

No, I wish people would stop talking about SVA.

Quoted 8 lines
Quoted 5 lines

SVA and vSVA are a very special narrow configuration of a PASID. There are lots of other PASID configurations! That is the whole point, a PASID is complicated, there are many configuration scenarios, they need to be in one place with a very clearly defined uAPI

I feel it also makes sense to allow a subsystem to specify which configurations are permitted when allowing a PASID on its device

huh? why?

Quoted 2 lines

e.g. excluding things like
GPA mappings that existing subsystems (VFIO/VDPA) already handle well:

They don't "handle well", they have some historical baggage that is no longer suitable for the complexity this area has in the modern world.

Forget about the existing APIs and replace them in /dev/ioasid.

Quoted 8 lines

- Share GPA mappings between multiple devices (w/ or w/o PASID) for better IOTLB efficiency;

- Share GPA mappings between transactions w/ PASID and transactions w/o PASID from the same device (e.g. GPU) for better IOTLB efficiency;

- Use the same page table for GPA mappings before and after the guest turns on/off the PASID capability;

All of these are cases you need to design the /dev/ioasid to handle.

It is pretty clear to me that you'll need non-PASID IOASID's as well.

Ideally a generic IOASID would just be a page table and it doesn't crystalize into a RID or RID,PASID routing until devices are attached to it.

Since IOASID can be nested the only thing that makes any sense is for each level of the nest to be visible under /dev/ioasid.

What a complete mess it would be if vfio-pci owns the GPA table, /dev/ioasid has a nested PASID, and vfio-mdev is running a mdev on top of that PASID.

Quoted 2 lines

All above are given as long as we continue to let VFIO/VDPA manage the iommu domain and associated GPA mappings for PASID.

So don't do that. Don't I keep saying this weird split is making a horrible mess?

You can't reasonably build the complex PASID scenarios you talk about above unless the entire translation path is owned by one entity:
/dev/ioasid.

You need to focus on figuring out what that looks like then figure out how to move VDPA and VFIO to consume /dev/ioasid for all of their translation instead of open-coding half-baked internal versions.

Jason

#218 lore raw

Re: [PATCH V4 05/18] iommu/ioasid: Redefine IOASID set and allocation APIs

In reply to Tian, Kevin
Quoted 10 lines · Kevin
Quoted 5 lines

Because if you don't then we enter insane world where a PASID is being created under /dev/ioasid but its translation path flows through setup done by VFIO and the whole user API becomes an incomprehensible mess.

How will you even associate the PASID with the other translation??

PASID is attached to a specific iommu domain (created by VFIO/VDPA), which has GPA->HPA mappings already configured. If we view that mapping as an attribute of the iommu domain, it's reasonable to have the userspace-bound pgtable through /dev/ioasid to nest on it.

A user controlled page table should absolutely not be an attribute of a hidden kernel object, nor should two parts of the kernel silently connect to each other via a hidden internal objects like this.

Security is important - the kind of connection must use some explicit FD authorization to access shared objects, not be made implicit!

IMHO this direction is a dead end for this reason.

Quoted 7 lines
Quoted 7 lines

The entire translation path for any ioasid or PASID should be defined only by /dev/ioasid. Everything else is a legacy API.

Quoted 3 lines

If following your suggestion then VFIO must deny VFIO MAP operations on sva1 (assume userspace should not mix sva1 and sva2 in the same container and instead use /dev/ioasid to map for sva1)?

Quoted 6 lines
Quoted 3 lines

No, userspace creates an iosaid for the guest physical mapping and passes this ioasid to VFIO PCI which will assign it as the first layer mapping on the RID

Is it an dummy ioasid just for providing GPA mappings for nesting purpose of other IOASIDs? Then we waste one per VM?

Generic ioasid's are "free" they are just software constructs in the kernel.

Quoted 6 lines
Quoted 3 lines

When PASIDs are allocated the uAPI will be told to logically nested under the first ioasid. When VFIO authorizes a PASID for a RID it checks that all the HW rules are being followed.

As I explained above, why cannot we just use iommu domain to connect the dots?

Security.

Quoted 4 lines

Every passthrough framework needs to create an iommu domain first. and It needs to support both devices w/ PASID and devices w/o PASID. For devices w/o PASID it needs to invent its own MAP interface anyway.

No, it should consume a ioasid from /dev/ioasid, use a common ioasid map interface and assign that ioasid to a RID.

Don't get so fixated on PASID as a special case

Quoted 4 lines

Then why do we bother creating another MAP interface through /dev/ioasid which not only duplicates but also creating transition burden between two set of MAP interfaces when the guest turns on/off the pasid capability on the device?

Don't transition. Always use the new interface. qemu detects the kernel supports /dev/ioasid and *all iommu page table configuration* goes through there. VFIO and VDPA APIs become unused for iommu configuration.

Quoted 7 lines

'universally' upon from which angle you look at this problem. From IOASID p.o.v possibly yes, but from device passthrough p.o.v. it's the opposite since the passthrough framework needs to handle devices w/o PASID anyway (or even for device w/ PASID it could send traffic w/o PASID) thus 'universally' makes more sense if the passthrough framework can use one interface of its own to manage GPA mappings for all consumers (apply to the case when a PASID is allowed/authorized).

You correctly named it /dev/ioasid, it is a generic way to allocate, manage and assign IOMMU page tables, which when generalized, only some of which may consume a limited PASID.

RID and RID,PASID are the same thing, just a small difference in how they match TLPs.

Jason

#226 lore raw

Re: [PATCH V4 05/18] iommu/ioasid: Redefine IOASID set and allocation APIs

In reply to Tian, Kevin
Quoted 6 lines · Kevin

btw this discussion was raised when discussing the I/O page fault handling process. Currently the IOMMU layer implements a per-device fault reporting mechanism, which requires VFIO to register a handler to receive all faults on its device and then forwards to ioasid if it's due to 1st-level. Possibly it makes more sense to convert it into a per-pgtable reporting scheme, and then the owner of each pgtable should register its own handler.

Maybe, but you do need device information in there, since that's how the fault is reported to the guest and how the response is routed back to the faulting device (only PASID+PRGI would cause aliasing). And we need to report non-recoverable faults, as well as recoverable ones without PASID, once we hand control of level-1 page tables to guests.

Quoted 5 lines

It means
for 1) VFIO will register a 2nd-level pgtable handler while /dev/ioasid will register a 1st-level pgtable handler, while for 3) /dev/ioasid will register handlers for both 1st-level and 2nd-level pgtable. Jean? also want to know your thoughts...

Moving all IOMMU controls to /dev/ioasid rather that splitting them is probably better. Hopefully the implementation can reuse most of vfio_iommu_type1.

I'm trying to sketch what may work for Arm, if we have to reuse /dev/ioasid to avoid duplication of fault and inval queues:

* Get a container handle out of /dev/ioasid (or /dev/iommu, really.) No operation available since we don't know what the device and IOMMU capabilities are.

* Attach the handle to a VF. With VFIO that would be VFIO_GROUP_SET_CONTAINER. That causes the kernel to associate an IOMMU with the handle, and decide which operations are available.

* With a map/unmap vIOMMU (or shadow mappings), a single translation level is supported. With a nesting vIOMMU, we're populating the level-2 translation (some day maybe by binding the KVM page tables, but currently with map/unmap ioctl).

Single-level translation needs single VF per container. Two level would allow sharing stage-2 between multiple VFs, though it's a pain to define and implement.

* Without a vIOMMU or if the vIOMMU starts in bypass, populate the container page tables.

Start the guest.

* With a map/unmap vIOMMU, guest creates mappings, userspace populates the page tables with map/unmap ioctl.

It would be possible to add a PASID mode there: guest requests an address space with a specific PASID, userspace derives an IOASID handle from the container handle and populate that address space with map/unmap ioctl. That would enable PASID on sub-VF assignment, which requires the host to control which PASID is programmed into the VF (with DEVICE_ALLOW_IOASID, I guess). And either the host allocates the PASID in this case (which isn't supported by a vSMMU) or we have to do a vPASID -> pPASID. I don't know if it's worth the effort.

Or
* With a nesting vIOMMU, the guest attaches a PASID table to a VF, userspace issues a SET_PASID_TABLE ioctl on the container handle. If we support multiple VFs per container, we first need to derive a child container from the main one and the device, then attach the PASID table.

Guest programs the PASID table, sends invalidations when removing mappings which are relayed to the host on the child container. Page faults and response queue would be per container, so if multiple VF per container, we could have one queue for the parent (level-2 faults) and one for each child (level-1 faults).

Thanks,
Jean

#227 lore raw

Re: [PATCH V4 05/18] iommu/ioasid: Redefine IOASID set and allocation APIs

Quoted 7 lines · Jean-Philippe Brucker

* Get a container handle out of /dev/ioasid (or /dev/iommu, really.) No operation available since we don't know what the device and IOMMU capabilities are.

* Attach the handle to a VF. With VFIO that would be VFIO_GROUP_SET_CONTAINER. That causes the kernel to associate an IOMMU with the handle, and decide which operations are available.

Right, this is basically the point, - the VFIO container (/dev/vfio) and the /dev/ioasid we are talking about have a core of similarity. ioasid is the generalized, modernized, and cross-subsystem version of the same idea. Instead of calling it "vfio container" we call it something that evokes the idea of controlling the iommu.

The issue is to seperate /dev/vfio generic functionality from vfio and share it with every subsystem.

It may be that /dev/vfio and /dev/ioasid end up sharing a lot of code, with a different IOCTL interface around it. The vfio_iommu_driver_ops is not particularly VFIOy.

Creating /dev/ioasid may primarily start as a code reorganization exercise.

Quoted 6 lines

* With a map/unmap vIOMMU (or shadow mappings), a single translation level is supported. With a nesting vIOMMU, we're populating the level-2 translation (some day maybe by binding the KVM page tables, but currently with map/unmap ioctl).

Single-level translation needs single VF per container.

Really? Why?

Jason

#219 lore raw

RE: [PATCH V4 05/18] iommu/ioasid: Redefine IOASID set and allocation APIs

In reply to Jason Gunthorpe
Quoted 19 lines

From: Jason Gunthorpe <[email protected]>
Sent: Wednesday, April 7, 2021 8:21 PM

Quoted 3 lines · Kevin
Quoted 3 lines

Because if you don't then we enter insane world where a PASID is being created under /dev/ioasid but its translation path flows through setup done by VFIO and the whole user API becomes an incomprehensible

mess.

Quoted 4 lines
Quoted 2 lines

How will you even associate the PASID with the other translation??

PASID is attached to a specific iommu domain (created by VFIO/VDPA),

which

Quoted 2 lines

has GPA->HPA mappings already configured. If we view that mapping as an attribute of the iommu domain, it's reasonable to have the userspace-

bound

Quoted 1 line

pgtable through /dev/ioasid to nest on it.

Quoted 9 lines

A user controlled page table should absolutely not be an attribute of a hidden kernel object, nor should two parts of the kernel silently connect to each other via a hidden internal objects like this.

Security is important - the kind of connection must use some explicit FD authorization to access shared objects, not be made implicit!

IMHO this direction is a dead end for this reason.

Could you elaborate what exact security problem is brought with this approach? Isn't ALLOW_PASID the authorization interface for the connection?

Based on all your replies now I see what you actually want is generalizing all IOMMU related stuff through /dev/ioasid (sort of /dev/iommu), which requires factoring out the vfio_iommu_type1 into the general part. This is a huge work.

Is it really the only practice in Linux that any new feature has to be blocked as long as a refactoring work is identified? Don't people accept any balance between enabling new features and completing refactoring work through a staging approach, as long as we don't introduce an uAPI specifically for the staging purpose? ☹

Thanks
Kevin

#228 lore raw

Re: [PATCH V4 05/18] iommu/ioasid: Redefine IOASID set and allocation APIs

In reply to Jason Gunthorpe
Quoted 26 lines · Jason Gunthorpe
Quoted 7 lines · Jean-Philippe Brucker

* Get a container handle out of /dev/ioasid (or /dev/iommu, really.) No operation available since we don't know what the device and IOMMU capabilities are.

* Attach the handle to a VF. With VFIO that would be VFIO_GROUP_SET_CONTAINER. That causes the kernel to associate an IOMMU with the handle, and decide which operations are available.

Right, this is basically the point, - the VFIO container (/dev/vfio) and the /dev/ioasid we are talking about have a core of similarity. ioasid is the generalized, modernized, and cross-subsystem version of the same idea. Instead of calling it "vfio container" we call it something that evokes the idea of controlling the iommu.

The issue is to seperate /dev/vfio generic functionality from vfio and share it with every subsystem.

It may be that /dev/vfio and /dev/ioasid end up sharing a lot of code, with a different IOCTL interface around it. The vfio_iommu_driver_ops is not particularly VFIOy.

Creating /dev/ioasid may primarily start as a code reorganization exercise.

Quoted 8 lines
Quoted 6 lines

* With a map/unmap vIOMMU (or shadow mappings), a single translation level is supported. With a nesting vIOMMU, we're populating the level-2 translation (some day maybe by binding the KVM page tables, but currently with map/unmap ioctl).

Single-level translation needs single VF per container.

Really? Why?

The vIOMMU is started in bypass, so the device can do DMA to the GPA space until the guest configures the vIOMMU, at which point each VF is either kept in bypass or gets new DMA mappings, which requires the host to tear down the bypass mappings and set up the guest mappings on a per-VF basis (I'm not considering nesting translation in the host kernel for this, because it's not supported by all pIOMMUs and is expensive in terms of TLB and pinned memory). So keeping a single VF per container is simpler, but there are certainly other programming models possible.

Thanks,
Jean

#220 lore raw

Re: [PATCH V4 05/18] iommu/ioasid: Redefine IOASID set and allocation APIs

In reply to Tian, Kevin
Quoted 26 lines · Kevin
Quoted 26 lines

From: Jason Gunthorpe <[email protected]>
Sent: Wednesday, April 7, 2021 8:21 PM

Quoted 3 lines · Kevin
Quoted 3 lines

Because if you don't then we enter insane world where a PASID is being created under /dev/ioasid but its translation path flows through setup done by VFIO and the whole user API becomes an incomprehensible

mess.

Quoted 4 lines
Quoted 2 lines

How will you even associate the PASID with the other translation??

PASID is attached to a specific iommu domain (created by VFIO/VDPA),

which

Quoted 2 lines

has GPA->HPA mappings already configured. If we view that mapping as an attribute of the iommu domain, it's reasonable to have the userspace-

bound

Quoted 1 line

pgtable through /dev/ioasid to nest on it.

A user controlled page table should absolutely not be an attribute of a hidden kernel object, nor should two parts of the kernel silently connect to each other via a hidden internal objects like this.

Security is important - the kind of connection must use some explicit FD authorization to access shared objects, not be made implicit!

Quoted 6 lines
Quoted 2 lines

IMHO this direction is a dead end for this reason.

Could you elaborate what exact security problem is brought with this approach? Isn't ALLOW_PASID the authorization interface for the connection?

If the kernel objects don't come out of FDs then no.

Quoted 2 lines

Is it really the only practice in Linux that any new feature has to be blocked as long as a refactoring work is identified?

The practice is to define uAPIs that make sense and have a good chance to be supported over a long time period, as the software evolves, not to hacky hacky a gaint uAPI mess just to get some feature out the door.

This proposal as it was oringial shown is exactly the kind of hacky hacky uapi nobody wants to see. Tunneling an IOMMU uapi through a whole bunch of other FDs is completely nutz.

Intel should basically be investing most of its time building a robust and well designed uAPI here, and don't complain that the community is not doing Intel's job for free.

Quoted 3 lines

Don't people accept any balance between enabling new features and completing refactoring work through a staging approach, as long as we don't introduce an uAPI specifically for the staging purpose? ☹

Since this is all uapi I don't see it as applicable here.

Jason

#56 lore raw

Re: [PATCH V4 05/18] iommu/ioasid: Redefine IOASID set and allocation APIs

In reply to Jason Gunthorpe

Hi Jason,

Quoted 11 lines · Jason Gunthorpe
Quoted 8 lines · Yi L

DMA page faults are delivered to root-complex via page request message and it is per-device according to PCIe spec. Page request handling flow is:

1) iommu driver receives a page request from device
2) iommu driver parses the page request message. Get the RID,PASID, faulted page and requested permissions etc.
3) iommu driver triggers fault handler registered by device driver with iommu_report_device_fault()

Quoted 3 lines

This seems confused.

The PASID should define how to handle the page fault, not the driver.

In my series I don't use PASID at all. I am just enabling nested stage and the guest uses a single context. I don't allocate any user PASID at any point.

When there is a fault at physical level (a stage 1 fault that concerns the guest), this latter needs to be reported and injected into the guest. The vfio pci driver registers a fault handler to the iommu layer and in that fault handler it fills a circ bugger and triggers an eventfd that is listened to by the VFIO-PCI QEMU device. this latter retrives the faault from the mmapped circ buffer, it knowns which vIOMMU it is attached to, and passes the fault to the vIOMMU.
Then the vIOMMU triggers and IRQ in the guest.

We are reusing the existing concepts from VFIO, region, IRQ to do that.

For that use case, would you also use /dev/ioasid?

Thanks

Eric

Quoted 31 lines

I don't remember any device specific actions in ATS, so what is the driver supposed to do?

Quoted 3 lines

4) device driver's fault handler signals an event FD to notify userspace to fetch the information about the page fault. If it's VM case, inject the page fault to VM and let guest to solve it.

If the PASID is set to 'report page fault to userspace' then some event should come out of /dev/ioasid, or be reported to a linked eventfd, or whatever.

If the PASID is set to 'SVM' then the fault should be passed to handle_mm_fault

And so on.

Userspace chooses what happens based on how they configure the PASID through /dev/ioasid.

Why would a device driver get involved here?

Quoted 3 lines

Eric has sent below series for the page fault reporting for VM with passthru device.
https://lore.kernel.org/kvm/[email protected]/

It certainly should not be in vfio pci. Everything using a PASID needs this infrastructure, VDPA, mdev, PCI, CXL, etc.

Jason

#57 lore raw

Re: [PATCH V4 05/18] iommu/ioasid: Redefine IOASID set and allocation APIs

Quoted 31 lines · Auger Eric

Hi Jason,

Quoted 11 lines · Jason Gunthorpe
Quoted 8 lines · Yi L

DMA page faults are delivered to root-complex via page request message and it is per-device according to PCIe spec. Page request handling flow is:

1) iommu driver receives a page request from device
2) iommu driver parses the page request message. Get the RID,PASID, faulted page and requested permissions etc.
3) iommu driver triggers fault handler registered by device driver with iommu_report_device_fault()

Quoted 3 lines

This seems confused.

The PASID should define how to handle the page fault, not the driver.

In my series I don't use PASID at all. I am just enabling nested stage and the guest uses a single context. I don't allocate any user PASID at any point.

When there is a fault at physical level (a stage 1 fault that concerns the guest), this latter needs to be reported and injected into the guest. The vfio pci driver registers a fault handler to the iommu layer and in that fault handler it fills a circ bugger and triggers an eventfd that is listened to by the VFIO-PCI QEMU device. this latter retrives the faault from the mmapped circ buffer, it knowns which vIOMMU it is attached to, and passes the fault to the vIOMMU.
Then the vIOMMU triggers and IRQ in the guest.

Quoted 3 lines

We are reusing the existing concepts from VFIO, region, IRQ to do that.

For that use case, would you also use /dev/ioasid?

/dev/ioasid could do all the things you described vfio-pci as doing, it can even do them the same way you just described.

Stated another way, do you plan to duplicate all of this code someday for vfio-cxl? What about for vfio-platform? ARM SMMU can be hooked to platform devices, right?

I feel what you guys are struggling with is some choice in the iommu kernel APIs that cause the events to be delivered to the pci_device owner, not the PASID owner.

That feels solvable.

Jason

author
#58 lore raw

Re: [PATCH V4 05/18] iommu/ioasid: Redefine IOASID set and allocation APIs

Hi Jason,

Quoted 45 lines · Jason Gunthorpe
Quoted 33 lines · Auger Eric

Hi Jason,

Quoted 12 lines · Jason Gunthorpe
Quoted 9 lines · Yi L

DMA page faults are delivered to root-complex via page request message and it is per-device according to PCIe spec. Page request handling flow is:

1) iommu driver receives a page request from device
2) iommu driver parses the page request message. Get the RID,PASID, faulted page and requested permissions etc.
3) iommu driver triggers fault handler registered by device driver with iommu_report_device_fault()

Quoted 4 lines

This seems confused.

The PASID should define how to handle the page fault, not the driver.

In my series I don't use PASID at all. I am just enabling nested stage and the guest uses a single context. I don't allocate any user PASID at any point.

When there is a fault at physical level (a stage 1 fault that concerns the guest), this latter needs to be reported and injected into the guest. The vfio pci driver registers a fault handler to the iommu layer and in that fault handler it fills a circ bugger and triggers an eventfd that is listened to by the VFIO-PCI QEMU device. this latter retrives the faault from the mmapped circ buffer, it knowns which vIOMMU it is attached to, and passes the fault to the vIOMMU.
Then the vIOMMU triggers and IRQ in the guest.

Quoted 3 lines

We are reusing the existing concepts from VFIO, region, IRQ to do that.

For that use case, would you also use /dev/ioasid?

/dev/ioasid could do all the things you described vfio-pci as doing, it can even do them the same way you just described.

Stated another way, do you plan to duplicate all of this code someday for vfio-cxl? What about for vfio-platform? ARM SMMU can be hooked to platform devices, right?

Quoted 6 lines

I feel what you guys are struggling with is some choice in the iommu kernel APIs that cause the events to be delivered to the pci_device owner, not the PASID owner.

That feels solvable.

Perhaps more of a philosophical question for you and Alex. There is no doubt that the direction you guided for /dev/ioasid is a much cleaner one, especially after VDPA emerged as another IOMMU backed framework.

The question is what do we do with the nested translation features that have been targeting the existing VFIO-IOMMU for the last three years? That predates VDPA. Shall we put a stop marker *after* nested support and say no more extensions for VFIO-IOMMU, new features must be built on this new interface?

If we were to close a checkout line for some unforeseen reasons, should we honor the customers already in line for a long time?

This is not a tactic or excuse for not working on the new /dev/ioasid interface. In fact, I believe we can benefit from the lessons learned while completing the existing. This will give confidence to the new interface. Thoughts?

Quoted 1 line

Jason

Thanks,

Jacob

#195 lore raw

Re: [PATCH V4 05/18] iommu/ioasid: Redefine IOASID set and allocation APIs

In reply to Jason Gunthorpe

Hi Jason,

Quoted 34 lines · Jason Gunthorpe
Quoted 33 lines · Auger Eric

Hi Jason,

Quoted 11 lines · Jason Gunthorpe
Quoted 8 lines · Yi L

DMA page faults are delivered to root-complex via page request message and it is per-device according to PCIe spec. Page request handling flow is:

1) iommu driver receives a page request from device
2) iommu driver parses the page request message. Get the RID,PASID, faulted page and requested permissions etc.
3) iommu driver triggers fault handler registered by device driver with iommu_report_device_fault()

Quoted 3 lines

This seems confused.

The PASID should define how to handle the page fault, not the driver.

In my series I don't use PASID at all. I am just enabling nested stage and the guest uses a single context. I don't allocate any user PASID at any point.

When there is a fault at physical level (a stage 1 fault that concerns the guest), this latter needs to be reported and injected into the guest. The vfio pci driver registers a fault handler to the iommu layer and in that fault handler it fills a circ bugger and triggers an eventfd that is listened to by the VFIO-PCI QEMU device. this latter retrives the faault from the mmapped circ buffer, it knowns which vIOMMU it is attached to, and passes the fault to the vIOMMU.
Then the vIOMMU triggers and IRQ in the guest.

We are reusing the existing concepts from VFIO, region, IRQ to do that.

Quoted 8 lines
Quoted 1 line

For that use case, would you also use /dev/ioasid?

/dev/ioasid could do all the things you described vfio-pci as doing, it can even do them the same way you just described.

Stated another way, do you plan to duplicate all of this code someday for vfio-cxl? What about for vfio-platform? ARM SMMU can be hooked to platform devices, right?

vfio regions and IRQ related APIs are common user interfaces exposed by all vfio drivers, including platform. Then the actual circular buffer implementation details can be put in a common lib.

as for the thin vfio iommu wrappers, the ones you don't like, they are implemented in type1 code.

Maybe the need for /dev/ioasid is more crying for PASID management but for the nested use case, that's not obvious to me and in your different replies, it was not crystal clear where the use case belongs to.

The redesign requirement came pretty late in the development process. The iommu user API is upstream for a while, the VFIO interfaces have been submitted a long time ago and under review for a bunch of time. Redesigning everything with a different API, undefined at this point, is a major setback for our work and will have a large impact on the introduction of features companies are looking forward, hence our frustration.

Thanks

Eric

Quoted 9 lines

I feel what you guys are struggling with is some choice in the iommu kernel APIs that cause the events to be delivered to the pci_device owner, not the PASID owner.

That feels solvable.

Jason

#196 lore raw

Re: [PATCH V4 05/18] iommu/ioasid: Redefine IOASID set and allocation APIs

Quoted 7 lines · Auger Eric

The redesign requirement came pretty late in the development process. The iommu user API is upstream for a while, the VFIO interfaces have been submitted a long time ago and under review for a bunch of time. Redesigning everything with a different API, undefined at this point, is a major setback for our work and will have a large impact on the introduction of features companies are looking forward, hence our frustration.

I will answer both you and Jacob at once.

This is uAPI, once it is set it can never be changed.

The kernel process and philosophy is to invest heavily in uAPI development and review to converge on the best uAPI possible.

Many past submissions have take a long time to get this right, there are several high profile uAPI examples.

Do you think this case is so special, or the concerns so minor, that it should get to bypass all of the normal process?

Ask yourself, is anyone advocating for the current direction on technical merits alone?

Certainly the patches I last saw where completely disgusting from a uAPI design perspective.

It was against the development process to organize this work the way it was done. Merging a wack of dead code to the kernel to support a uAPI vision that was never clearly articulated was a big mistake.

Start from the beginning. Invest heavily in defining a high quality uAPI. Clearly describe the uAPI to all stake holders. Break up the implementation into patch series without dead code. Make the patches. Remove the dead code this group has already added.

None of this should be a surprise. The VDPA discussion and related "what is a mdev" over a year ago made it pretty clear VFIO is not the exclusive user of "IOMMU in userspace" and that places limits on what kind of uAPIs expansion it should experience going forward.

Jason

#197 lore raw

Re: [PATCH V4 05/18] iommu/ioasid: Redefine IOASID set and allocation APIs

Hi,

Quoted 14 lines · Jason Gunthorpe
Quoted 7 lines · Auger Eric

The redesign requirement came pretty late in the development process. The iommu user API is upstream for a while, the VFIO interfaces have been submitted a long time ago and under review for a bunch of time. Redesigning everything with a different API, undefined at this point, is a major setback for our work and will have a large impact on the introduction of features companies are looking forward, hence our frustration.

I will answer both you and Jacob at once.

This is uAPI, once it is set it can never be changed.

Quoted 8 lines

The kernel process and philosophy is to invest heavily in uAPI development and review to converge on the best uAPI possible.

Many past submissions have take a long time to get this right, there are several high profile uAPI examples.

Do you think this case is so special, or the concerns so minor, that it should get to bypass all of the normal process?

That's not my intent to bypass any process. I am just trying to understand what needs to be re-designed and for what use case.

Quoted 7 lines

Ask yourself, is anyone advocating for the current direction on technical merits alone?

Certainly the patches I last saw where completely disgusting from a uAPI design perspective.

Quoted 6 lines

It was against the development process to organize this work the way it was done. Merging a wack of dead code to the kernel to support a uAPI vision that was never clearly articulated was a big mistake.

Start from the beginning. Invest heavily in defining a high quality uAPI. Clearly describe the uAPI to all stake holders.

This was largely done during several confs including plumber, KVM forum, for several years. Also API docs were shared on the ML. I don't remember any voice was raised at those moments.

Break up the

Quoted 7 lines

implementation into patch series without dead code. Make the patches. Remove the dead code this group has already added.

None of this should be a surprise. The VDPA discussion and related "what is a mdev" over a year ago made it pretty clear VFIO is not the exclusive user of "IOMMU in userspace" and that places limits on what kind of uAPIs expansion it should experience going forward.

Maybe clear for you but most probably not for many other stakeholders.

Anyway I do not intend to further argue and I will be happy to learn from you and work with you, Jacob, Liu and all other stakeholders to define a better integration.

Thanks

Eric

Quoted 3 lines

Jason

#198 lore raw

Re: [PATCH V4 05/18] iommu/ioasid: Redefine IOASID set and allocation APIs

Quoted 3 lines · Auger Eric

This was largely done during several confs including plumber, KVM forum, for several years. Also API docs were shared on the ML. I don't remember any voice was raised at those moments.

I don't think anyone objects to the high level ideas, but implementation does matter. I don't think anyone presented "hey we will tunnel an uAPI through VFIO to the IOMMU subsystem" - did they?

Look at the fairly simple IMS situation, for example. This was presented at plumbers too, and the slides were great - but the implementation was too hacky. It required a major rework of the x86 interrupt handling before it was OK.

Jason

#199 lore raw

Re: [PATCH V4 05/18] iommu/ioasid: Redefine IOASID set and allocation APIs

Hi Jason,

Quoted 9 lines · Jason Gunthorpe
Quoted 3 lines · Auger Eric

This was largely done during several confs including plumber, KVM forum, for several years. Also API docs were shared on the ML. I don't remember any voice was raised at those moments.

I don't think anyone objects to the high level ideas, but implementation does matter. I don't think anyone presented "hey we will tunnel an uAPI through VFIO to the IOMMU subsystem" - did they?

At minimum
https://events19.linuxfoundation.cn/wp-content/uploads/2017/11/Shared-Virtual-Memory-in-KVM_Yi-Liu.pdf

But most obviously everything is documented in Documentation/userspace-api/iommu.rst where the VFIO tunneling is clearly stated ;-)

But well let's work together to design a better and more elegant solution then.

Thanks

Eric

Quoted 8 lines

Look at the fairly simple IMS situation, for example. This was presented at plumbers too, and the slides were great - but the implementation was too hacky. It required a major rework of the x86 interrupt handling before it was OK.

Jason

#59 lore raw

Re: [PATCH V4 05/18] iommu/ioasid: Redefine IOASID set and allocation APIs

In reply to Jacob Pan
Quoted 53 lines · On Fri, 16 Apr 2021 06:12:58 -0700 Jacob Pan <[email protected]>…

Hi Jason,

Quoted 49 lines · Jason Gunthorpe
Quoted 33 lines · Auger Eric

Hi Jason,

Quoted 12 lines · Jason Gunthorpe
Quoted 9 lines · Yi L

DMA page faults are delivered to root-complex via page request message and it is per-device according to PCIe spec. Page request handling flow is:

1) iommu driver receives a page request from device
2) iommu driver parses the page request message. Get the RID,PASID, faulted page and requested permissions etc.
3) iommu driver triggers fault handler registered by device driver with iommu_report_device_fault()

Quoted 4 lines

This seems confused.

The PASID should define how to handle the page fault, not the driver.

In my series I don't use PASID at all. I am just enabling nested stage and the guest uses a single context. I don't allocate any user PASID at any point.

When there is a fault at physical level (a stage 1 fault that concerns the guest), this latter needs to be reported and injected into the guest. The vfio pci driver registers a fault handler to the iommu layer and in that fault handler it fills a circ bugger and triggers an eventfd that is listened to by the VFIO-PCI QEMU device. this latter retrives the faault from the mmapped circ buffer, it knowns which vIOMMU it is attached to, and passes the fault to the vIOMMU.
Then the vIOMMU triggers and IRQ in the guest.

Quoted 3 lines

We are reusing the existing concepts from VFIO, region, IRQ to do that.

For that use case, would you also use /dev/ioasid?

/dev/ioasid could do all the things you described vfio-pci as doing, it can even do them the same way you just described.

Stated another way, do you plan to duplicate all of this code someday for vfio-cxl? What about for vfio-platform? ARM SMMU can be hooked to platform devices, right?

I feel what you guys are struggling with is some choice in the iommu kernel APIs that cause the events to be delivered to the pci_device owner, not the PASID owner.

Quoted 5 lines
Quoted 2 lines

That feels solvable.

Perhaps more of a philosophical question for you and Alex. There is no doubt that the direction you guided for /dev/ioasid is a much cleaner one, especially after VDPA emerged as another IOMMU backed framework.

I think this statement answers all your remaining questions ;)

Quoted 6 lines

The question is what do we do with the nested translation features that have been targeting the existing VFIO-IOMMU for the last three years? That predates VDPA. Shall we put a stop marker *after* nested support and say no more extensions for VFIO-IOMMU, new features must be built on this new interface?

Quoted 7 lines

If we were to close a checkout line for some unforeseen reasons, should we honor the customers already in line for a long time?

This is not a tactic or excuse for not working on the new /dev/ioasid interface. In fact, I believe we can benefit from the lessons learned while completing the existing. This will give confidence to the new interface. Thoughts?

I understand a big part of Jason's argument is that we shouldn't be in the habit of creating duplicate interfaces, we should create one, well designed interfaces to share among multiple subsystems. As new users have emerged, our solution needs to change to a common one rather than a VFIO specific one. The IOMMU uAPI provides an abstraction, but at the wrong level, requiring userspace interfaces for each subsystem.

Luckily the IOMMU uAPI is not really exposed as an actual uAPI, but that changes if we proceed to enable the interfaces to tunnel it through VFIO.

The logical answer would therefore be that we don't make that commitment to the IOMMU uAPI if we believe now that it's fundamentally flawed.

Ideally this new /dev/ioasid interface, and making use of it as a VFIO IOMMU backend, should replace type1. Type1 will live on until that interface gets to parity, at which point we may deprecate type1, but it wouldn't make sense to continue to expand type1 in the same direction as we intend /dev/ioasid to take over in the meantime, especially if it means maintaining an otherwise dead uAPI. Thanks,

Alex

author
#60 lore raw

Re: [PATCH V4 05/18] iommu/ioasid: Redefine IOASID set and allocation APIs

Hi Alex,

Quoted 86 lines · Alex Williamson
Quoted 54 lines · On Fri, 16 Apr 2021 06:12:58 -0700 Jacob Pan <[email protected]>…

Hi Jason,

Quoted 50 lines · Jason Gunthorpe
Quoted 33 lines · Auger Eric

Hi Jason,

Quoted 12 lines · Jason Gunthorpe
Quoted 9 lines · Yi L

DMA page faults are delivered to root-complex via page request message and it is per-device according to PCIe spec. Page request handling flow is:

1) iommu driver receives a page request from device
2) iommu driver parses the page request message. Get the RID,PASID, faulted page and requested permissions etc.
3) iommu driver triggers fault handler registered by device driver with iommu_report_device_fault()

Quoted 4 lines

This seems confused.

The PASID should define how to handle the page fault, not the driver.

In my series I don't use PASID at all. I am just enabling nested stage and the guest uses a single context. I don't allocate any user PASID at any point.

When there is a fault at physical level (a stage 1 fault that concerns the guest), this latter needs to be reported and injected into the guest. The vfio pci driver registers a fault handler to the iommu layer and in that fault handler it fills a circ bugger and triggers an eventfd that is listened to by the VFIO-PCI QEMU device. this latter retrives the faault from the mmapped circ buffer, it knowns which vIOMMU it is attached to, and passes the fault to the vIOMMU. Then the vIOMMU triggers and IRQ in the guest.

Quoted 4 lines

We are reusing the existing concepts from VFIO, region, IRQ to do that.

For that use case, would you also use /dev/ioasid?

/dev/ioasid could do all the things you described vfio-pci as doing, it can even do them the same way you just described.

Stated another way, do you plan to duplicate all of this code someday for vfio-cxl? What about for vfio-platform? ARM SMMU can be hooked to platform devices, right?

I feel what you guys are struggling with is some choice in the iommu kernel APIs that cause the events to be delivered to the pci_device owner, not the PASID owner.

Quoted 5 lines
Quoted 2 lines

That feels solvable.

Perhaps more of a philosophical question for you and Alex. There is no doubt that the direction you guided for /dev/ioasid is a much cleaner one, especially after VDPA emerged as another IOMMU backed framework.

I think this statement answers all your remaining questions ;)

Quoted 6 lines

The question is what do we do with the nested translation features that have been targeting the existing VFIO-IOMMU for the last three years? That predates VDPA. Shall we put a stop marker *after* nested support and say no more extensions for VFIO-IOMMU, new features must be built on this new interface?

Quoted 7 lines

If we were to close a checkout line for some unforeseen reasons, should we honor the customers already in line for a long time?

This is not a tactic or excuse for not working on the new /dev/ioasid interface. In fact, I believe we can benefit from the lessons learned while completing the existing. This will give confidence to the new interface. Thoughts?

I understand a big part of Jason's argument is that we shouldn't be in the habit of creating duplicate interfaces, we should create one, well designed interfaces to share among multiple subsystems. As new users have emerged, our solution needs to change to a common one rather than a VFIO specific one. The IOMMU uAPI provides an abstraction, but at the wrong level, requiring userspace interfaces for each subsystem.

Quoted 8 lines

Luckily the IOMMU uAPI is not really exposed as an actual uAPI, but that changes if we proceed to enable the interfaces to tunnel it through VFIO.

The logical answer would therefore be that we don't make that commitment to the IOMMU uAPI if we believe now that it's fundamentally flawed.

I agree the uAPI data tunneling is definitely flawed in terms of scalability.

I was just thinking it is still a small part of the overall picture. Considering there are other parts such as fault reporting, user space deployment, performance, and security. By completing the support on the existing VFIO framework, it would at least offer a clear landscape where the new /dev/ioasid can improve upon.

Perhaps similar to cgroup v1 vs v2, it took a long time and with known limitations in v1.

Anyway, I am glad we have a clear direction now.

Thanks,

Jacob

Quoted 10 lines

Ideally this new /dev/ioasid interface, and making use of it as a VFIO IOMMU backend, should replace type1. Type1 will live on until that interface gets to parity, at which point we may deprecate type1, but it wouldn't make sense to continue to expand type1 in the same direction as we intend /dev/ioasid to take over in the meantime, especially if it means maintaining an otherwise dead uAPI. Thanks,

Alex

Thanks,

Jacob

#61 lore raw

Re: [PATCH V4 05/18] iommu/ioasid: Redefine IOASID set and allocation APIs

Quoted 2 lines · Jacob Pan

Perhaps similar to cgroup v1 vs v2, it took a long time and with known limitations in v1.

cgroup v2 is still having transition problems, if anything it is a cautionary tale to think really hard about uAPI because transitioning can be really hard.

It might be very wise to make /dev/ioasid and /dev/vfio ioctl compatible in some way so existing software has a smoother upgrade path.

For instance by defining a default IOASID

Jason

#62 lore raw

RE: [PATCH V4 05/18] iommu/ioasid: Redefine IOASID set and allocation APIs

In reply to Alex Williamson

Hi Alex,

Quoted 2 lines

From: Alex Williamson <[email protected]>
Sent: Friday, April 16, 2021 11:46 PM

[...]

Quoted 16 lines
Quoted 4 lines

This is not a tactic or excuse for not working on the new /dev/ioasid interface. In fact, I believe we can benefit from the lessons learned while completing the existing. This will give confidence to the new interface. Thoughts?

I understand a big part of Jason's argument is that we shouldn't be in the habit of creating duplicate interfaces, we should create one, well designed interfaces to share among multiple subsystems. As new users have emerged, our solution needs to change to a common one rather than a VFIO specific one. The IOMMU uAPI provides an abstraction, but at the wrong level, requiring userspace interfaces for each subsystem.

Luckily the IOMMU uAPI is not really exposed as an actual uAPI, but that changes if we proceed to enable the interfaces to tunnel it through VFIO.

Quoted 6 lines

The logical answer would therefore be that we don't make that commitment to the IOMMU uAPI if we believe now that it's fundamentally flawed.

Ideally this new /dev/ioasid interface, and making use of it as a VFIO IOMMU backend, should replace type1.

yeah, just a double check, I think this also requires a new set of uAPIs (e.g. new MAP/UNMAP), which means the current VFIO IOMMU type1 related ioctls would be deprecated in future. right?

Quoted 5 lines

Type1 will live on until that
interface gets to parity, at which point we may deprecate type1, but it wouldn't make sense to continue to expand type1 in the same direction as we intend /dev/ioasid to take over in the meantime, especially if it means maintaining an otherwise dead uAPI. Thanks,

understood.

Regards,
Yi Liu

#63 lore raw

Re: [PATCH V4 05/18] iommu/ioasid: Redefine IOASID set and allocation APIs

Quoted 6 lines · Yi L
Quoted 2 lines

Ideally this new /dev/ioasid interface, and making use of it as a VFIO IOMMU backend, should replace type1.

yeah, just a double check, I think this also requires a new set of uAPIs (e.g. new MAP/UNMAP), which means the current VFIO IOMMU type1 related ioctls would be deprecated in future. right?

This is something to think about, it might make sense to run the current ioctls in some "compat" mode under /dev/ioasid just to make migration easier

In this sense /dev/ioasid would be a container that holds multiple IOASIDs and every new format ioctl specifies the IOASID to operate on. The legacy ioctls would use some default IOASID but otherwise act the same.

I'm assuming here there is nothing especially wrong with the /dev/vfio interface beyond being in the wrong place in the kernel and not supporting multiple IOASIDs?

Then there may be a fairly simple approch to just make /dev/vfio == /dev/ioasid, at least for type 1.

By this I mean we could have the new /dev/ioasid code take over the /dev/vfio char dev and present both interfaces, but with the same fops.

The VFIO code would have to remain somehow to support PPC until someone from ppc world migrates the SPAPR_TCE to use the kernel's new common IOMMU framework instead of the arch specialty thing it does now. But it can at least be compile disabled on everything except ppc.

Jason

#64 lore raw

Re: [PATCH V4 05/18] iommu/ioasid: Redefine IOASID set and allocation APIs

Quoted 11 lines · On Wed, 21 Apr 2021 13:23:07 -0300 Jason Gunthorpe <[email protected]> wrote:
Quoted 6 lines · Yi L
Quoted 2 lines

Ideally this new /dev/ioasid interface, and making use of it as a VFIO IOMMU backend, should replace type1.

yeah, just a double check, I think this also requires a new set of uAPIs (e.g. new MAP/UNMAP), which means the current VFIO IOMMU type1 related ioctls would be deprecated in future. right?

This is something to think about, it might make sense to run the current ioctls in some "compat" mode under /dev/ioasid just to make migration easier

Right, deprecating type1 doesn't necessarily mean deprecating the uAPI. We created a type1v2 with minor semantic differences in unmap behavior within the same uAPI. Userspace is able to query and select an IOMMU backend model and each model might have a different uAPI. The SPAPR IOMMU backend already takes advantage of this, using some ioctls consistent with type1, but also requiring some extra steps.

Also note that the simple MAP and UNMAP uAPI of type1 has its limitations, which we already struggle with. See for example the massive performance issues backing a vIOMMU with this uAPI. The /dev/ioasid approach should alleviate some of that, using a page table for the 1st level, but a more advanced uAPI for the 2nd level seems necessary at some point as well.

Quoted 9 lines

In this sense /dev/ioasid would be a container that holds multiple IOASIDs and every new format ioctl specifies the IOASID to operate on. The legacy ioctls would use some default IOASID but otherwise act the same.

I'm assuming here there is nothing especially wrong with the /dev/vfio interface beyond being in the wrong place in the kernel and not supporting multiple IOASIDs?

Quoted 6 lines

Then there may be a fairly simple approch to just make /dev/vfio == /dev/ioasid, at least for type 1.

By this I mean we could have the new /dev/ioasid code take over the /dev/vfio char dev and present both interfaces, but with the same fops.

That's essentially replacing vfio-core, where I think we're more talking about /dev/ioasid being an available IOMMU backend driver which a user can select when available. The interface of making that selection might change to accept an external /dev/ioasid file descriptor, of course. Maybe you can elaborate on how the vfio device and group uAPI live (or not) in this new scheme were /dev/ioasid is the primary interface. Thanks,

Alex

#65 lore raw

Re: [PATCH V4 05/18] iommu/ioasid: Redefine IOASID set and allocation APIs

Quoted 1 line · Alex Williamson

That's essentially replacing vfio-core, where I think we're more

I am only talking about /dev/vfio here which is basically the IOMMU interface part.

I still expect that VFIO_GROUP_SET_CONTAINER will be used to connect /dev/{ioasid,vfio} to the VFIO group and all the group and device logic stays inside VFIO.

The appeal of unifying /dev/{ioasid,vfio} to a single fops is that it cleans up vfio a lot - we don't have to have two different code paths where one handles a vfio_container and the other a ioasid_container and the all the related different iommu ops and so on.

Everything can be switched to ioasid_container all down the line. If it wasn't for PPC this looks fairly simple.

Since getting rid of PPC looks a bit hard, we'd be stuck with accepting a /dev/ioasid and then immediately wrappering it in a vfio_container an shimming it through a vfio_iommu_ops. It is not ideal at all, but in my look around I don't see a major problem if type1 implementation is moved to live under /dev/ioasid.

For concreteness if we look at the set container flow with ioasid I'd say something like:

vfio_group_fops_unl_ioctl()
VFIO_GROUP_SET_CONTAINER
vfio_group_set_container()

     if (f.file->f_op == &vfio_fops) {
          // Use a real vfio_container and vfio_iommu_driver
          driver->ops->attach_group()
             tce_iommu_attach_group()
     }

     if (ioasid_container = ioasid_get_from_fd(container_fd)) {
         // create a dummy vfio_container and use the ioasid driver
	 container = kzalloc()
         container->iommu_driver = ioasid_shim
         driver->ops->attach_group()
             ioasid_shim_attach_group(ioasid_container, ...)
                 ioasid_attach_group()
                     // What used to be vfio_iommu_attach_group()

Broadly all the ops vfio need go through the ioasid_shim which relays them to the generic ioasid API.

We end up with a ioasid.h that basically has the vfio_iommu_type1 code lightly recast into some 'struct iommu_container' and a set of ioasid_* function entry points that follow vfio_iommu_driver_ops_type1:
ioasid_attach_group
ioasid_detatch_group
ioasid_<something about user pages> ioasid_read/ioasid_write

If we have this, and /dev/ioasid implements the legacy IOCTLs, then /dev/vfio == /dev/ioasid and we can compile out vfio_fops and related from vfio.c and tell ioasid.c to create /dev/vfio instead using the ops it owns.

This is a very long winded way of saying ideally we'd do approximately:
git mv drivers/vfio/vfio_iommu_type1.c drivers/ioasid/ioasid.c

As the first step. Essentially we declare that what is type1 is really the user interface to the internal kernel IOMMU kAPI, which has been steadily evolving since type1 was created 10 years ago.

Quoted 4 lines

The interface of making that selection might change to accept an external /dev/ioasid file descriptor, of course. Maybe you can elaborate on how the vfio device and group uAPI live (or not) in this new scheme were /dev/ioasid is the primary interface. Thanks,

They say in vfio. You'd still open a group and you'd still pass in either /dev/vfio or /dev/ioasid to define the container

Though, completely as an unrelated aside, I admit to not entirely understanding why the group is the central element of the uAPI.

It is weird that the vfio "drivers" all work on the struct vfio_device (at least after my series), and it has a file_operations presence via vfio_device_fops, but instead of struct vfio_device directly having a 'struct device' and cdev to access the FD we get it through a group FD and agroup chardev via VFIO_GROUP_GET_DEVICE_FD

If we were to revise this, and I don't see a huge reason to do so, I would put a struct device and cdev in struct vfio_device, attach the vfio_device directly to the ioasid and then forget about the group, at least as uapi, completely.

Or at least I don't see where that gets into trouble, but I'm not too familiar with the multi-vfio in a process scenario..

Jason

#66 lore raw

Re: [PATCH V4 05/18] iommu/ioasid: Redefine IOASID set and allocation APIs

Quoted 10 lines · On Wed, 21 Apr 2021 14:52:03 -0300 Jason Gunthorpe…
Quoted 1 line · Alex Williamson

That's essentially replacing vfio-core, where I think we're more

I am only talking about /dev/vfio here which is basically the IOMMU interface part.

I still expect that VFIO_GROUP_SET_CONTAINER will be used to connect /dev/{ioasid,vfio} to the VFIO group and all the group and device logic stays inside VFIO.

But that group and device logic is also tied to the container, where the IOMMU backend is the interchangeable thing that provides the IOMMU manipulation for that container. If you're using VFIO_GROUP_SET_CONTAINER to associate a group to a /dev/ioasid, then you're really either taking that group outside of vfio or you're re-implementing group management in /dev/ioasid. I'd expect the transition point at VFIO_SET_IOMMU.

Quoted 4 lines

The appeal of unifying /dev/{ioasid,vfio} to a single fops is that it cleans up vfio a lot - we don't have to have two different code paths where one handles a vfio_container and the other a ioasid_container and the all the related different iommu ops and so on.

Currently vfio IOMMU backends don't know about containers either. Setting the vfio IOMMU for a container creates an object within the IOMMU backend representing that IOMMU context. IOMMU groups are then attached to that context, where the IOMMU backend can add to or create a new IOMMU domain to include that group, or if no compatible IOMMU context can be created, reject it.

Quoted 2 lines

Everything can be switched to ioasid_container all down the line. If it wasn't for PPC this looks fairly simple.

At what point is it no longer vfio? I'd venture to say that replacing the container rather than invoking a different IOMMU backend is that point.

Quoted 5 lines

Since getting rid of PPC looks a bit hard, we'd be stuck with accepting a /dev/ioasid and then immediately wrappering it in a vfio_container an shimming it through a vfio_iommu_ops. It is not ideal at all, but in my look around I don't see a major problem if type1 implementation is moved to live under /dev/ioasid.

But type1 is \just\ an IOMMU backend, not "/dev/vfio". Given that nobody flinched at removing NVLink support, maybe just deprecate SPAPR now and see if anyone objects ;)

Quoted 6 lines

For concreteness if we look at the set container flow with ioasid I'd say something like:

vfio_group_fops_unl_ioctl()
VFIO_GROUP_SET_CONTAINER
vfio_group_set_container()

Quoted 14 lines
     if (f.file->f_op == &vfio_fops) {
          // Use a real vfio_container and vfio_iommu_driver
          driver->ops->attach_group()
             tce_iommu_attach_group()
     }

     if (ioasid_container = ioasid_get_from_fd(container_fd)) {
         // create a dummy vfio_container and use the ioasid driver
	 container = kzalloc()
         container->iommu_driver = ioasid_shim
         driver->ops->attach_group()
             ioasid_shim_attach_group(ioasid_container, ...)
                 ioasid_attach_group()
                     // What used to be vfio_iommu_attach_group()

How do you handle multiple groups with the same container? Again, I'd expect some augmentation of VFIO_SET_IOMMU so that /dev/vfio continues to exist and manage group to container mapping and /dev/ioasid manages the IOMMU context of that container.

Quoted 3 lines

Broadly all the ops vfio need go through the ioasid_shim which relays them to the generic ioasid API.

/dev/vfio essentially already passes through all fops to the IOMMU backend once the VFIO_SET_IOMMU is established.

Quoted 7 lines

We end up with a ioasid.h that basically has the vfio_iommu_type1 code lightly recast into some 'struct iommu_container' and a set of ioasid_* function entry points that follow vfio_iommu_driver_ops_type1:
ioasid_attach_group
ioasid_detatch_group
ioasid_<something about user pages> ioasid_read/ioasid_write

Again, this looks like a vfio IOMMU backend. What are we accomplishing by replacing /dev/vfio with /dev/ioasid versus some manipulation of VFIO_SET_IOMMU accepting a /dev/ioasid fd?

Quoted 4 lines

If we have this, and /dev/ioasid implements the legacy IOCTLs, then /dev/vfio == /dev/ioasid and we can compile out vfio_fops and related from vfio.c and tell ioasid.c to create /dev/vfio instead using the ops it owns.

Why would we want /dev/ioasid to implement legacy ioctls instead of simply implementing an interface to allow /dev/ioasid to be used as a vfio IOMMU backend?

Quoted 7 lines

This is a very long winded way of saying ideally we'd do approximately:
git mv drivers/vfio/vfio_iommu_type1.c drivers/ioasid/ioasid.c

As the first step. Essentially we declare that what is type1 is really the user interface to the internal kernel IOMMU kAPI, which has been steadily evolving since type1 was created 10 years ago.

The pseudo code above really suggests you do want to remove /dev/vfio/vfio, but this is only one of the IOMMU backends for vfio, so I can't quite figure out if we're talking past each other.

As I expressed in another thread, type1 has a lot of shortcomings. The mapping interface leaves userspace trying desperately to use statically mapped buffers because the map/unmap latency is too high. We have horrible issues with duplicate locked page accounting across containers. It suffers pretty hard from feature creep in various areas. A new IOMMU backend is an opportunity to redesign some of these things.

Quoted 17 lines
Quoted 4 lines

The interface of making that selection might change to accept an external /dev/ioasid file descriptor, of course. Maybe you can elaborate on how the vfio device and group uAPI live (or not) in this new scheme were /dev/ioasid is the primary interface. Thanks,

They say in vfio. You'd still open a group and you'd still pass in either /dev/vfio or /dev/ioasid to define the container

Though, completely as an unrelated aside, I admit to not entirely understanding why the group is the central element of the uAPI.

It is weird that the vfio "drivers" all work on the struct vfio_device (at least after my series), and it has a file_operations presence via vfio_device_fops, but instead of struct vfio_device directly having a 'struct device' and cdev to access the FD we get it through a group FD and agroup chardev via VFIO_GROUP_GET_DEVICE_FD

Quoted 7 lines

If we were to revise this, and I don't see a huge reason to do so, I would put a struct device and cdev in struct vfio_device, attach the vfio_device directly to the ioasid and then forget about the group, at least as uapi, completely.

Or at least I don't see where that gets into trouble, but I'm not too familiar with the multi-vfio in a process scenario..

The vfio_group is the unit of userspace ownership as it reflects the IOMMU group as the unit of isolation. Ideally there's a 1:1 mapping between device and group, but that is of course not always the case.

The IOMMU group also abstracts isolation and visibility relative to DMA. For example, in a PCIe topology a multi-function device may not have isolation between functions, but each requester ID is visible to the IOMMU. This lacks isolation but not IOMMU granularity, or visibility. A conventional PCI topology however lacks both isolation and visibility, all devices downstream use either the PCIe-to-PCI bridge RID or a RID derived from the secondary bus. We can also have mixed topologies, for example PCIe-to-PCI<->PCI-to-PCIe, where the grouping code needs to search upstream for the highest level where we achieve both isolation and visibility.

To simplify this, we use the group as the unit of IOMMU context, again favoring singleton group behavior.

An individual vfio_device doesn't know about these isolation dependencies, thus while a vfio bus/device driver like vfio-pci can expose a device, it's vfio-core than manages whether the isolated set of devices which includes that device, ie. the group, meets the requirements for userspace access. Thanks,

Alex

#67 lore raw

Re: [PATCH V4 05/18] iommu/ioasid: Redefine IOASID set and allocation APIs

Quoted 7 lines · Alex Williamson
Quoted 3 lines

I still expect that VFIO_GROUP_SET_CONTAINER will be used to connect /dev/{ioasid,vfio} to the VFIO group and all the group and device logic stays inside VFIO.

But that group and device logic is also tied to the container, where the IOMMU backend is the interchangeable thing that provides the IOMMU manipulation for that container.

I think that is an area where the discussion would need to be focused.

I don't feel very prepared to have it in details, as I haven't dug into all the group and iommu micro-operation very much.

But, it does seem like the security concept that VFIO is creating with the group also has to be present in the lower iommu layer too.

With different subsystems joining devices to the same ioasid's we still have to enforce the security propery the vfio group is creating.

Quoted 3 lines

If you're using VFIO_GROUP_SET_CONTAINER to associate a group to a /dev/ioasid, then you're really either taking that group outside of vfio or you're re-implementing group management in /dev/ioasid.

This sounds right.

Quoted 6 lines
Quoted 2 lines

Everything can be switched to ioasid_container all down the line. If it wasn't for PPC this looks fairly simple.

At what point is it no longer vfio? I'd venture to say that replacing the container rather than invoking a different IOMMU backend is that point.

sorry, which is no longer vfio?

Quoted 9 lines
Quoted 5 lines

Since getting rid of PPC looks a bit hard, we'd be stuck with accepting a /dev/ioasid and then immediately wrappering it in a vfio_container an shimming it through a vfio_iommu_ops. It is not ideal at all, but in my look around I don't see a major problem if type1 implementation is moved to live under /dev/ioasid.

But type1 is \just\ an IOMMU backend, not "/dev/vfio". Given that nobody flinched at removing NVLink support, maybe just deprecate SPAPR now and see if anyone objects ;)

Would simplify this project, but I wonder :)

In any event, it does look like today we'd expect the SPAPR stuff would be done through the normal iommu APIs, perhaps enhanced a bit, which makes me suspect an enhanced type1 can implement SPAPR.

I say this because the SPAPR looks quite a lot like PASID when it has APIs for allocating multiple tables and other things. I would be interested to hear someone from IBM talk about what it is doing and how it doesn't fit into today's IOMMU API.

It is very old and the iommu world has advanced tremendously lately, maybe I'm too optimisitic?

Quoted 11 lines
Quoted 7 lines

We end up with a ioasid.h that basically has the vfio_iommu_type1 code lightly recast into some 'struct iommu_container' and a set of ioasid_* function entry points that follow vfio_iommu_driver_ops_type1:
ioasid_attach_group
ioasid_detatch_group
ioasid_<something about user pages> ioasid_read/ioasid_write

Again, this looks like a vfio IOMMU backend. What are we accomplishing by replacing /dev/vfio with /dev/ioasid versus some manipulation of VFIO_SET_IOMMU accepting a /dev/ioasid fd?

The point of all of this is to make the user api for the IOMMU cross-subsystem. It is not a vfio IOMMU backend, it is moving the IOMMU abstraction from VFIO into the iommu framework and giving the iommu framework a re-usable user API.

My ideal outcome would be for VFIO to use only the new iommu/ioasid API and have no iommu pluggability at all. The iommu subsystem provides everything needed to VFIO, and provides it equally to VDPA and everything else.

drivers/vfio/ becomes primarily about 'struct vfio_device' and everything related to its IOCTL interface.

drivers/iommu and ioasid.c become all about a pluggable IOMMU interface, including a uAPI for it.

IMHO it makes a high level sense, though it may be a pipe dream.

Quoted 8 lines
Quoted 4 lines

If we have this, and /dev/ioasid implements the legacy IOCTLs, then /dev/vfio == /dev/ioasid and we can compile out vfio_fops and related from vfio.c and tell ioasid.c to create /dev/vfio instead using the ops it owns.

Why would we want /dev/ioasid to implement legacy ioctls instead of simply implementing an interface to allow /dev/ioasid to be used as a vfio IOMMU backend?

Only to make our own migration easier. I'd imagine everyone would want to sit down and design this new clear ioasid API that can co-exist on /dev/ioasid with the legacy once.

Quoted 3 lines

The pseudo code above really suggests you do want to remove /dev/vfio/vfio, but this is only one of the IOMMU backends for vfio, so I can't quite figure out if we're talking past each other.

I'm not quite sure what you mean by "one of the IOMMU backends?" You mean type1, right?

Quoted 7 lines

As I expressed in another thread, type1 has a lot of shortcomings. The mapping interface leaves userspace trying desperately to use statically mapped buffers because the map/unmap latency is too high. We have horrible issues with duplicate locked page accounting across containers. It suffers pretty hard from feature creep in various areas. A new IOMMU backend is an opportunity to redesign some of these things.

Sure, but also those kinds of transformational things go alot better if you can smoothly go from the old to the new and have technical co-existance in side the kernel. Having a shim that maps the old APIs to new APIs internally to Linux helps keep the implementation from becoming too bogged down with compatibility.

Quoted 4 lines

The IOMMU group also abstracts isolation and visibility relative to DMA. For example, in a PCIe topology a multi-function device may not have isolation between functions, but each requester ID is visible to the IOMMU.

Okay, I'm glad I have this all right in my head, as I was pretty sure this was what the group was about.

My next question is why do we have three things as a FD: group, device and container (aka IOMMU interface)?

Do we have container because the /dev/vfio/vfio can hold only a single page table so we need to swap containers sometimes?

If we start from a clean sheet and make a sketch..

/dev/ioasid is the IOMMU control interface. It can create multiple IOASIDs that have page tables and it can manipulate those page tables. Each IOASID is identified by some number.

struct vfio_device/vdpa_device/etc are consumers of /dev/ioasid

When a device attaches to an ioasid userspace gives VFIO/VDPA the ioasid FD and the ioasid # in the FD.

The security rule for isolation is that once a device is attached to a /dev/ioasid fd then all other devices in that security group must be attached to the same ioasid FD or left unused.

Thus /dev/ioasid also becomes the unit of security and the IOMMU subsystem level becomes aware of and enforces the group security rules. Userspace does not need to "see" the group

In sketch it would be like
ioasid_fd = open("/dev/ioasid");
vfio_device_fd = open("/dev/vfio/device0")
vdpa_device_fd = open("/dev/vdpa/device0")
ioctl(vifo_device_fd, JOIN_IOASID_FD, ioasifd)
ioctl(vdpa_device_fd, JOIN_IOASID_FD, ioasifd)

gpa_ioasid_id = ioctl(ioasid_fd, CREATE_IOASID, ..) ioctl(ioasid_fd, SET_IOASID_PAGE_TABLES, ..)

ioctl(vfio_device, ATTACH_IOASID, gpa_ioasid_id) ioctl(vpda_device, ATTACH_IOASID, gpa_ioasid_id)

.. both VDPA and VFIO see the guest physical map and the kernel has
enough info that both could use the same IOMMU page table structure ..

// Guest viommu turns off bypass mode for the vfio device ioctl(vfio_device, DETATCH_IOASID)

// Guest viommu creates a new page table rid_ioasid_id = ioctl(ioasid_fd, CREATE_IOASID, ..) ioctl(ioasid_fd, SET_IOASID_PAGE_TABLES, ..)

// Guest viommu links the new page table to the RID ioctl(vfio_device, ATTACH_IOASID, rid_ioasid_id)

The group security concept becomes implicit and hidden from the uAPI. JOIN_IOASID_FD implicitly finds the device's group inside the kernel and requires that all members of the group be joined only to this ioasid_fd.

Essentially we discover the group from the device instead of the device from the group.

Where does it fall down compared to the three FD version we have today?

Jason

#68 lore raw

RE: [PATCH V4 05/18] iommu/ioasid: Redefine IOASID set and allocation APIs

Quoted 9 lines

From: Jason Gunthorpe <[email protected]>
Sent: Thursday, April 22, 2021 7:03 AM

Quoted 3 lines

The pseudo code above really suggests you do want to remove /dev/vfio/vfio, but this is only one of the IOMMU backends for vfio, so I can't quite figure out if we're talking past each other.

I'm not quite sure what you mean by "one of the IOMMU backends?" You mean type1, right?

I think Alex meant that type1 is one of the IOMMU backends in VFIO (type1, type1v2, tce, tce_v2, noiommu, etc.) which are all configured through /dev/vfio/vfio. If we are just moving type1 to /dev/ioasid, the justification is not sufficient by replacing /dev/vfio/vfio with /dev/ioasid, at least in this transition phase (before all iommu bits are consolidated in /dev/ioasid in your ideal outcome).

Quoted 9 lines
Quoted 8 lines

As I expressed in another thread, type1 has a lot of shortcomings. The mapping interface leaves userspace trying desperately to use statically mapped buffers because the map/unmap latency is too high. We have horrible issues with duplicate locked page accounting across containers. It suffers pretty hard from feature creep in various areas. A new IOMMU backend is an opportunity to redesign some of these things.

Quoted 5 lines

Sure, but also those kinds of transformational things go alot better if you can smoothly go from the old to the new and have technical co-existance in side the kernel. Having a shim that maps the old APIs to new APIs internally to Linux helps keep the implementation from becoming too bogged down with compatibility.

The shim layer could be considered as a new iommu backend in VFIO, which connects VFIO iommu ops to the internal helpers in drivers/ioasid. In this case then we don't need to replicate the VFIO uAPI through /dev/ioasid. Instead the new interface just supports new uAPI. An old VFIO userspace still opens /dev/vfio/vfio to conduct iommu operations which implicitly goes to drivers/ioasid. A new VFIO userspace uses /dev/vfio/vfio to join ioasid_fd and then use new uAPIs through /dev/ ioasid to manage iommu pgtables, as you described below.

Quoted 6 lines
Quoted 5 lines

The IOMMU group also abstracts isolation and visibility relative to DMA. For example, in a PCIe topology a multi-function device may not have isolation between functions, but each requester ID is visible to the IOMMU.

Quoted 8 lines

Okay, I'm glad I have this all right in my head, as I was pretty sure this was what the group was about.

My next question is why do we have three things as a FD: group, device and container (aka IOMMU interface)?

Do we have container because the /dev/vfio/vfio can hold only a single page table so we need to swap containers sometimes?

Yes, one container can hold only a single page table. When vIOMMU is exposed, VFIO requires each device/group in different containers to support per-device address space (before nested translation is supported), which is switched between GPA and gIOVA when bypass mode is turned on/off for a given device.

Another tricky thing is that a container may be linked to multiple iommu domains in VFIO, as devices in the container may locate behind different IOMMUs with inconsistent capability (commit 1ef3e2bc). In this case more accurately one container can hold a single address space, which could be replayed into multiple page tables (with exact same mappings). I'm not sure whether this is something that could be simplified (or not supported) in the new interface. In the end each pgtable operation is per iommu domain in the iommu layer. I wonder where we want to maintain the relationship between the ioasid_fd and associated iommu domains.

Quoted 40 lines

If we start from a clean sheet and make a sketch..

/dev/ioasid is the IOMMU control interface. It can create multiple IOASIDs that have page tables and it can manipulate those page tables. Each IOASID is identified by some number.

struct vfio_device/vdpa_device/etc are consumers of /dev/ioasid

When a device attaches to an ioasid userspace gives VFIO/VDPA the ioasid FD and the ioasid # in the FD.

The security rule for isolation is that once a device is attached to a /dev/ioasid fd then all other devices in that security group must be attached to the same ioasid FD or left unused.

Thus /dev/ioasid also becomes the unit of security and the IOMMU subsystem level becomes aware of and enforces the group security rules. Userspace does not need to "see" the group

In sketch it would be like
ioasid_fd = open("/dev/ioasid");
vfio_device_fd = open("/dev/vfio/device0")
vdpa_device_fd = open("/dev/vdpa/device0")
ioctl(vifo_device_fd, JOIN_IOASID_FD, ioasifd)
ioctl(vdpa_device_fd, JOIN_IOASID_FD, ioasifd)

gpa_ioasid_id = ioctl(ioasid_fd, CREATE_IOASID, ..) ioctl(ioasid_fd, SET_IOASID_PAGE_TABLES, ..)

ioctl(vfio_device, ATTACH_IOASID, gpa_ioasid_id) ioctl(vpda_device, ATTACH_IOASID, gpa_ioasid_id)

.. both VDPA and VFIO see the guest physical map and the kernel has
enough info that both could use the same IOMMU page table structure ..

// Guest viommu turns off bypass mode for the vfio device ioctl(vfio_device, DETATCH_IOASID)

Quoted 6 lines

// Guest viommu creates a new page table rid_ioasid_id = ioctl(ioasid_fd, CREATE_IOASID, ..) ioctl(ioasid_fd, SET_IOASID_PAGE_TABLES, ..)

// Guest viommu links the new page table to the RID ioctl(vfio_device, ATTACH_IOASID, rid_ioasid_id)

Just to confirm. Above flow is for current map/unmap flavor as what VFIO/vDPA do today. Later when nested translation is supported, there is no need to detach gpa_ioasid_fd. Instead, a new cmd will be introduced to nest rid_ioasid_fd on top of gpa_ioasid_fd:

ioctl(ioasid_fd, NEST_IOASIDS, rid_ioasid_id, gpa_ioasid_id); ioctl(ioasid_fd, BIND_PGTABLE, rid_ioasid_id, ...);

and vSVA will follow the same flow:

ioctl(ioasid_fd, NEST_IOASIDS, sva_ioasid_id, gpa_ioasid_id); ioctl(ioasid_fd, BIND_PGTABLE, sva_ioasid_id, ...);

Does it match your mind when expanding /dev/ioasid to support vSVA and other new usages?

Quoted 6 lines

The group security concept becomes implicit and hidden from the uAPI. JOIN_IOASID_FD implicitly finds the device's group inside the kernel and requires that all members of the group be joined only to this ioasid_fd.

Quoted 6 lines

Essentially we discover the group from the device instead of the device from the group.

Where does it fall down compared to the three FD version we have today?

I also feel hiding group from uAPI is a good thing and is interested in the rationale behind for explicitly managing group in vfio (which is essentially the same boundary as provided by iommu group), e.g. for better user experience when group security is broken?

Thanks
Kevin

#69 lore raw

Re: [PATCH V4 05/18] iommu/ioasid: Redefine IOASID set and allocation APIs

Quoted 3 lines · Kevin

The shim layer could be considered as a new iommu backend in VFIO, which connects VFIO iommu ops to the internal helpers in drivers/ioasid.

It may be the best we can do because of SPAPR, but the ideal outcome should be to remove the entire pluggable IOMMU stuff from vfio entirely and have it only use /dev/ioasid

We should never add another pluggable IOMMU type to vfio - everything should be done through drives/iommu now that it is much more capable.

Quoted 3 lines

Another tricky thing is that a container may be linked to multiple iommu domains in VFIO, as devices in the container may locate behind different IOMMUs with inconsistent capability (commit 1ef3e2bc).

Frankly this sounds over complicated. I would think /dev/ioasid should select the IOMMU when the first device is joined, and all future joins must be compatible with the original IOMMU - ie there is only one set of IOMMU capabilities in a /dev/ioasid.

This means qemue might have multiple /dev/ioasid's if the system has multiple incompatible IOMMUs (is this actually a thing?) The platform should design its IOMMU domains to minimize the number of /dev/ioasid's required.

Is there a reason we need to share IOASID'd between completely divergance IOMMU implementations? I don't expect the HW should be able to physically share page tables??

That decision point alone might be the thing that just says we can't ever have /dev/vfio/vfio == /dev/ioasid

Quoted 4 lines

Just to confirm. Above flow is for current map/unmap flavor as what VFIO/vDPA do today. Later when nested translation is supported, there is no need to detach gpa_ioasid_fd. Instead, a new cmd will be introduced to nest rid_ioasid_fd on top of gpa_ioasid_fd:

Sure.. The tricky bit will be to define both of the common nested operating modes.

  nested_ioasid = ioctl(ioasid_fd, CREATE_NESTED_IOASID,  gpa_ioasid_id);
  ioctl(ioasid_fd, SET_NESTED_IOASID_PAGE_TABLES, nested_ioasid, ..)

// IOMMU will match on the device RID, no PASID:
ioctl(vfio_device, ATTACH_IOASID, nested_ioasid);

// IOMMU will match on the device RID and PASID:
ioctl(vfio_device, ATTACH_IOASID_PASID, pasid, nested_ioasid);

Notice that ATTACH (or bind, whatever) is always done on the vfio_device FD. ATTACH tells the IOMMU HW to link the PCI BDF&PASID to a specific page table defined by an IOASID.

I expect we have many flavours of IOASID tables, eg we have normal, and 'nested with table controlled by hypervisor'. ARM has 'nested with table controlled by guest' right? So like this?

nested_ioasid = ioctl(ioasid_fd, CREATE_DELGATED_IOASID,
gpa_ioasid_id, <some kind of viommu_id>)
// PASID now goes to <viommu_id>
ioctl(vfio_device, ATTACH_IOASID_PASID, pasid, nested_ioasid);

Where <viommu_id> is some internal to the guest handle of the viommu page table scoped within gpa_ioasid_id? Like maybe it is GPA of the base of the page table?

The guest can't select its own PASIDs without telling the hypervisor, right?

Quoted 4 lines

I also feel hiding group from uAPI is a good thing and is interested in the rationale behind for explicitly managing group in vfio (which is essentially the same boundary as provided by iommu group), e.g. for better user experience when group security is broken?

Indeed, I can see how things might have just evolved into this, but if it has a purpose it seems pretty hidden.
we need it or not seems pretty hidden.

Jason

#194 lore raw

Re: [PATCH V4 05/18] iommu/ioasid: Redefine IOASID set and allocation APIs

In reply to Alex Williamson
Quoted 14 lines · Alex Williamson
Quoted 10 lines · On Wed, 21 Apr 2021 14:52:03 -0300 Jason Gunthorpe <[email protected]> wrote:
Quoted 1 line · Alex Williamson

That's essentially replacing vfio-core, where I think we're more

I am only talking about /dev/vfio here which is basically the IOMMU interface part.

I still expect that VFIO_GROUP_SET_CONTAINER will be used to connect /dev/{ioasid,vfio} to the VFIO group and all the group and device logic stays inside VFIO.

Quoted 7 lines

But that group and device logic is also tied to the container, where the IOMMU backend is the interchangeable thing that provides the IOMMU manipulation for that container. If you're using VFIO_GROUP_SET_CONTAINER to associate a group to a /dev/ioasid, then you're really either taking that group outside of vfio or you're re-implementing group management in /dev/ioasid. I'd expect the transition point at VFIO_SET_IOMMU.

per my understanding, transiting at the VFIO_SET_IOMMU point makes more sense as VFIO can still have the group and device logic, which is the key concept of group granularity isolation for userspace direct access.

Signature · 2 lines
Regards,
Yi Liu
#130 lore raw

Re: [PATCH V4 05/18] iommu/ioasid: Redefine IOASID set and allocation APIs

In reply to Jason Gunthorpe
Quoted 27 lines · On Wed, 21 Apr 2021 20:03:01 -0300 Jason Gunthorpe…
Quoted 7 lines · Alex Williamson
Quoted 3 lines

I still expect that VFIO_GROUP_SET_CONTAINER will be used to connect /dev/{ioasid,vfio} to the VFIO group and all the group and device logic stays inside VFIO.

But that group and device logic is also tied to the container, where the IOMMU backend is the interchangeable thing that provides the IOMMU manipulation for that container.

I think that is an area where the discussion would need to be focused.

I don't feel very prepared to have it in details, as I haven't dug into all the group and iommu micro-operation very much.

But, it does seem like the security concept that VFIO is creating with the group also has to be present in the lower iommu layer too.

With different subsystems joining devices to the same ioasid's we still have to enforce the security propery the vfio group is creating.

Quoted 3 lines

If you're using VFIO_GROUP_SET_CONTAINER to associate a group to a /dev/ioasid, then you're really either taking that group outside of vfio or you're re-implementing group management in /dev/ioasid.

This sounds right.

Quoted 8 lines
Quoted 6 lines
Quoted 2 lines

Everything can be switched to ioasid_container all down the line. If it wasn't for PPC this looks fairly simple.

At what point is it no longer vfio? I'd venture to say that replacing the container rather than invoking a different IOMMU backend is that point.

sorry, which is no longer vfio?

I'm suggesting that if we're replacing the container/group model with an ioasid then we're effectively creating a new thing that really only retains the vfio device uapi.

Quoted 10 lines
Quoted 9 lines
Quoted 5 lines

Since getting rid of PPC looks a bit hard, we'd be stuck with accepting a /dev/ioasid and then immediately wrappering it in a vfio_container an shimming it through a vfio_iommu_ops. It is not ideal at all, but in my look around I don't see a major problem if type1 implementation is moved to live under /dev/ioasid.

But type1 is \just\ an IOMMU backend, not "/dev/vfio". Given that nobody flinched at removing NVLink support, maybe just deprecate SPAPR now and see if anyone objects ;)

Quoted 5 lines

Would simplify this project, but I wonder :)

In any event, it does look like today we'd expect the SPAPR stuff would be done through the normal iommu APIs, perhaps enhanced a bit, which makes me suspect an enhanced type1 can implement SPAPR.

David Gibson has argued for some time that SPAPR could be handled via a converged type1 model. We has mapped that out at one point, essentially a "type2", but neither of us had any bandwidth to pursue it.

Quoted 4 lines

I say this because the SPAPR looks quite a lot like PASID when it has APIs for allocating multiple tables and other things. I would be interested to hear someone from IBM talk about what it is doing and how it doesn't fit into today's IOMMU API.

[Cc David, Alexey]

Quoted 11 lines

It is very old and the iommu world has advanced tremendously lately, maybe I'm too optimisitic?

Quoted 8 lines
Quoted 7 lines

We end up with a ioasid.h that basically has the vfio_iommu_type1 code lightly recast into some 'struct iommu_container' and a set of ioasid_* function entry points that follow vfio_iommu_driver_ops_type1:
ioasid_attach_group
ioasid_detatch_group
ioasid_<something about user pages> ioasid_read/ioasid_write

Quoted 8 lines
Quoted 3 lines

Again, this looks like a vfio IOMMU backend. What are we accomplishing by replacing /dev/vfio with /dev/ioasid versus some manipulation of VFIO_SET_IOMMU accepting a /dev/ioasid fd?

The point of all of this is to make the user api for the IOMMU cross-subsystem. It is not a vfio IOMMU backend, it is moving the IOMMU abstraction from VFIO into the iommu framework and giving the iommu framework a re-usable user API.

Right, but I don't see that implies it cannot work within the vfio IOMMU model. Currently when an IOMMU is set, the /dev/vfio/vfio container becomes a conduit for file ops from the container to be forwarded to the IOMMU. But that's in part because the user doesn't have another object to interact with the IOMMU. It's entirely possible that with an ioasid shim, the user would continue to interact directly with the /dev/ioasid fd for IOMMU manipulation and only use VFIO_SET_IOMMU to associate a vfio container to that ioasid.

Quoted 4 lines

My ideal outcome would be for VFIO to use only the new iommu/ioasid API and have no iommu pluggability at all. The iommu subsystem provides everything needed to VFIO, and provides it equally to VDPA and everything else.

As above, we don't necessarily need to have the vfio container be the access mechanism for the IOMMU, it can become just an means to association the container with an IOMMU. This has quite a few transitional benefits.

Quoted 7 lines

drivers/vfio/ becomes primarily about 'struct vfio_device' and everything related to its IOCTL interface.

drivers/iommu and ioasid.c become all about a pluggable IOMMU interface, including a uAPI for it.

IMHO it makes a high level sense, though it may be a pipe dream.

This is where we've dissolved all but the vfio device uapi, which suggests the group and container model were never necessary and I'm not sure exactly what that uapi looks like. We currently make use of an IOMMU api that is group aware, but that awareness extends out to the vfio uapi.

Quoted 5 lines
Quoted 5 lines
Quoted 4 lines

If we have this, and /dev/ioasid implements the legacy IOCTLs, then /dev/vfio == /dev/ioasid and we can compile out vfio_fops and related from vfio.c and tell ioasid.c to create /dev/vfio instead using the ops it owns.

Quoted 7 lines
Quoted 3 lines

Why would we want /dev/ioasid to implement legacy ioctls instead of simply implementing an interface to allow /dev/ioasid to be used as a vfio IOMMU backend?

Only to make our own migration easier. I'd imagine everyone would want to sit down and design this new clear ioasid API that can co-exist on /dev/ioasid with the legacy once.

vfio really just wants to be able to attach groups to an address space to consider them isolated, everything else about the IOMMU API could happen via a new ioasid file descriptor representing that context, ie. vfio handles the group ownership and device access, ioasid handles the actual mappings.

Quoted 15 lines
Quoted 3 lines

The pseudo code above really suggests you do want to remove /dev/vfio/vfio, but this is only one of the IOMMU backends for vfio, so I can't quite figure out if we're talking past each other.

I'm not quite sure what you mean by "one of the IOMMU backends?" You mean type1, right?

Quoted 7 lines

As I expressed in another thread, type1 has a lot of shortcomings. The mapping interface leaves userspace trying desperately to use statically mapped buffers because the map/unmap latency is too high. We have horrible issues with duplicate locked page accounting across containers. It suffers pretty hard from feature creep in various areas. A new IOMMU backend is an opportunity to redesign some of these things.

Quoted 5 lines

Sure, but also those kinds of transformational things go alot better if you can smoothly go from the old to the new and have technical co-existance in side the kernel. Having a shim that maps the old APIs to new APIs internally to Linux helps keep the implementation from becoming too bogged down with compatibility.

I'm afraid /dev/ioasid providing type1 compatibility would be just that.

Quoted 5 lines
Quoted 4 lines

The IOMMU group also abstracts isolation and visibility relative to DMA. For example, in a PCIe topology a multi-function device may not have isolation between functions, but each requester ID is visible to the IOMMU.

Quoted 8 lines

Okay, I'm glad I have this all right in my head, as I was pretty sure this was what the group was about.

My next question is why do we have three things as a FD: group, device and container (aka IOMMU interface)?

Do we have container because the /dev/vfio/vfio can hold only a single page table so we need to swap containers sometimes?

The container represents an IOMMU address space, which can be shared by multiple groups, where each group may contain one or more devices. Swapping a container would require releasing all the devices (the user cannot have access to a non-isolated device), then a group could be moved from one container to another.

Quoted 6 lines

If we start from a clean sheet and make a sketch..

/dev/ioasid is the IOMMU control interface. It can create multiple IOASIDs that have page tables and it can manipulate those page tables. Each IOASID is identified by some number.

Quoted 8 lines

struct vfio_device/vdpa_device/etc are consumers of /dev/ioasid

When a device attaches to an ioasid userspace gives VFIO/VDPA the ioasid FD and the ioasid # in the FD.

The security rule for isolation is that once a device is attached to a /dev/ioasid fd then all other devices in that security group must be attached to the same ioasid FD or left unused.

Sounds like a group... Note also that if those other devices are not isolated from the user's device, the user could manipulate "unused" devices via DMA. So even unused devices should be within the same IOMMU context... thus attaching groups to IOMMU domains.

Quoted 3 lines

Thus /dev/ioasid also becomes the unit of security and the IOMMU subsystem level becomes aware of and enforces the group security rules. Userspace does not need to "see" the group

What tools does userspace have to understand isolation of individual devices without groups?

Quoted 32 lines

In sketch it would be like
ioasid_fd = open("/dev/ioasid");
vfio_device_fd = open("/dev/vfio/device0")
vdpa_device_fd = open("/dev/vdpa/device0")
ioctl(vifo_device_fd, JOIN_IOASID_FD, ioasifd)
ioctl(vdpa_device_fd, JOIN_IOASID_FD, ioasifd)

gpa_ioasid_id = ioctl(ioasid_fd, CREATE_IOASID, ..) ioctl(ioasid_fd, SET_IOASID_PAGE_TABLES, ..)

ioctl(vfio_device, ATTACH_IOASID, gpa_ioasid_id) ioctl(vpda_device, ATTACH_IOASID, gpa_ioasid_id)

.. both VDPA and VFIO see the guest physical map and the kernel has
enough info that both could use the same IOMMU page table structure ..

// Guest viommu turns off bypass mode for the vfio device ioctl(vfio_device, DETATCH_IOASID)

// Guest viommu creates a new page table rid_ioasid_id = ioctl(ioasid_fd, CREATE_IOASID, ..) ioctl(ioasid_fd, SET_IOASID_PAGE_TABLES, ..)

// Guest viommu links the new page table to the RID ioctl(vfio_device, ATTACH_IOASID, rid_ioasid_id)

The group security concept becomes implicit and hidden from the uAPI. JOIN_IOASID_FD implicitly finds the device's group inside the kernel and requires that all members of the group be joined only to this ioasid_fd.

Quoted 5 lines

Essentially we discover the group from the device instead of the device from the group.

Where does it fall down compared to the three FD version we have today?

The group concept is explicit today because how does userspace learn about implicit dependencies between devices? For example, if the user has a conventional PCI bus with a couple devices on it, how do they understand that those devices cannot be assigned to separate userspace drivers? The group fd fills that gap. Thanks,

Alex

#131 lore raw

Re: [PATCH V4 05/18] iommu/ioasid: Redefine IOASID set and allocation APIs

Quoted 3 lines · Alex Williamson

I'm suggesting that if we're replacing the container/group model with an ioasid then we're effectively creating a new thing that really only retains the vfio device uapi.

Yes, I think that is a fair assessment, but not necessarily bad.

The VFIO device uAPI is really the thing that is unique to VFIO and cannot be re-used anyplace else, in my assesment this is what vfio *is*, and the series I've been working on make it more obvious how broad that statement really is.

Quoted 7 lines
Quoted 3 lines

In any event, it does look like today we'd expect the SPAPR stuff would be done through the normal iommu APIs, perhaps enhanced a bit, which makes me suspect an enhanced type1 can implement SPAPR.

David Gibson has argued for some time that SPAPR could be handled via a converged type1 model. We has mapped that out at one point, essentially a "type2", but neither of us had any bandwidth to pursue it.

Cool! Well, let's put a pin in it, I don't think revising SPAPR should be a pre-condition for anything here - but we can all agree than an ideal world would be able to access SPAPR functionality from devices/iommu and /dev/ioasid

And it would be nice to map this out enough to provide enough preperation in the new /dev/ioasid uAPI. For instance I saw the only SPAPR specific stuff in DPDK was to preset the IOVA range that the IOASID would support. This would be trivial to add and may have benifits to other IOMMUS by reducing the number of translation levels or somethign.

Quoted 8 lines

Right, but I don't see that implies it cannot work within the vfio IOMMU model. Currently when an IOMMU is set, the /dev/vfio/vfio container becomes a conduit for file ops from the container to be forwarded to the IOMMU. But that's in part because the user doesn't have another object to interact with the IOMMU. It's entirely possible that with an ioasid shim, the user would continue to interact directly with the /dev/ioasid fd for IOMMU manipulation and only use VFIO_SET_IOMMU to associate a vfio container to that ioasid.

I am looking at this in two directions, the first is if we have /dev/ioasid how do we connect it to VFIO? And here I aruge we need new device IOCTLs and ideally a VFIO world that does not have a vestigial container FD at all.

This is because /dev/ioasid will have to be multi-IOASID and it just does not fit well into the VFIO IOMMU pluggable model at all - or at least trying to make it fit will defeat the point of having it in the first place.

This does not seem to be a big deal - the required device IOCTLs should be small and having two code paths isn't going to be an exploding complexity.

The second direction is how do we keep /dev/vfio/vfio entire uAPI without duplicating a lot of code. There is where building a ioasid back end or making ioasid == vfio are areas to look at.

Quoted 5 lines

vfio really just wants to be able to attach groups to an address space to consider them isolated, everything else about the IOMMU API could happen via a new ioasid file descriptor representing that context, ie. vfio handles the group ownership and device access, ioasid handles the actual mappings.

Right, exactly.

Quoted 8 lines
Quoted 2 lines

Do we have container because the /dev/vfio/vfio can hold only a single page table so we need to swap containers sometimes?

The container represents an IOMMU address space, which can be shared by multiple groups, where each group may contain one or more devices. Swapping a container would require releasing all the devices (the user cannot have access to a non-isolated device), then a group could be moved from one container to another.

So, basically, the answer is yes.

Having the container FD hold a single IOASID forced the group FD to exist because we can't maintain the security object of a group in the container FD if the work flow is to swap the container FD.

Here what I suggest is to merge the group security and the multiple "IOMMU address space" concept into one FD. The /dev/ioasid would have multiple page tables objects within it called IOASID'd and each IOASID effectively represents what /dev/vfio/vfio does today.

We can assign any device joined to a /dev/ioasid FD to any IOASID inside that FD, dynamically.

Quoted 8 lines
Quoted 3 lines

The security rule for isolation is that once a device is attached to a /dev/ioasid fd then all other devices in that security group must be attached to the same ioasid FD or left unused.

Sounds like a group... Note also that if those other devices are not isolated from the user's device, the user could manipulate "unused" devices via DMA. So even unused devices should be within the same IOMMU context... thus attaching groups to IOMMU domains.

That is a very interesting point. So, say, in the classic PCI bus world if I have a NIC and HD on my PCI bus and both are in the group, I assign the NIC to a /dev/ioasid & VFIO then it is possible to use the NIC to access the HD via DMA

And here you want a more explicit statement that the HD is at risk by using the NIC?

Honestly, I'm not sure the current group FD is actually showing that very strongly - though I get the point it is modeled in the sysfs and kind of implicit in the API - we evolved things in a way where most actual applications are taking in a PCI BDF from the user, not a group reference. So the actual security impact seems lost on the user.

Along my sketch if we have:

ioctl(vifo_device_fd, JOIN_IOASID_FD, ioasifd) [..]
ioctl(vfio_device, ATTACH_IOASID, gpa_ioasid_id) == ENOPERM

I would feel comfortable if the ATTACH_IOASID fails by default if all devices in the group have not been joined to the same ioasidfd.

So in the NIC&HD example the application would need to do:

   ioasid_fd = open("/dev/ioasid");
   nic_device_fd = open("/dev/vfio/device0")
   hd_device_fd = open("/dev/vfio/device1")

ioctl(nic_device_fd, JOIN_IOASID_FD, ioasifd) ioctl(hd_device_fd, JOIN_IOASID_FD, ioasifd)
[..]
ioctl(nice_device, ATTACH_IOASID, gpa_ioasid_id) == SUCCESS

Now the security relation is forced by the kernel to be very explicit.

However to keep current semantics, I'd suggest a flag on JOIN_IOASID_FD called "IOASID_IMPLICIT_GROUP" which has the effect of allowing the ATTACH_IOASID to succeed without the user having to explicitly join all the group devices. This is analogous to the world we have today of opening the VFIO group FD but only instantiating one device FD.

In effect the ioasid FD becomes the group and the numbered IOASID's inside the FD become the /dev/vfio/vfio objects - we don't end up with fewer objects in the system, they just have different uAPI presentations.

I'd envision applications like DPDK that are BDF centric to use the first API with some '--allow-insecure-vfio' flag to switch on the IOASID_IMPLICIT_GROUP. Maybe good applications would also print:
"Danger Will Robinson these PCI BDFs [...] are also at risk"
When the switch is used by parsing the sysfs

Quoted 6 lines
Quoted 3 lines

Thus /dev/ioasid also becomes the unit of security and the IOMMU subsystem level becomes aware of and enforces the group security rules. Userspace does not need to "see" the group

What tools does userspace have to understand isolation of individual devices without groups?

I think we can continue to show all of this group information in sysfs files, it just doesn't require application code to open a group FD.

This becomes relavent the more I think about it - elmininating the group and container FD uAPI by directly creating the device FD also sidesteps questions about how to model these objects in a /dev/ioasid only world. We simply don't have them at all so the answer is pretty easy.

Jason

#132 lore raw

Re: [PATCH V4 05/18] iommu/ioasid: Redefine IOASID set and allocation APIs

Quoted 9 lines · On Thu, 22 Apr 2021 14:57:15 -0300 Jason Gunthorpe…
Quoted 8 lines
Quoted 3 lines

The security rule for isolation is that once a device is attached to a /dev/ioasid fd then all other devices in that security group must be attached to the same ioasid FD or left unused.

Sounds like a group... Note also that if those other devices are not isolated from the user's device, the user could manipulate "unused" devices via DMA. So even unused devices should be within the same IOMMU context... thus attaching groups to IOMMU domains.

Quoted 7 lines

That is a very interesting point. So, say, in the classic PCI bus world if I have a NIC and HD on my PCI bus and both are in the group, I assign the NIC to a /dev/ioasid & VFIO then it is possible to use the NIC to access the HD via DMA

And here you want a more explicit statement that the HD is at risk by using the NIC?

If by "classic" you mean conventional PCI bus, then this is much worse than simply "at risk". The IOMMU cannot differentiate devices behind a PCIe-to-PCI bridge, so the moment you turn on the IOMMU context for the NIC, the address space for your HBA is pulled out from under it. In the vfio world, the NIC and HBA are grouped and managed together, the user cannot change the IOMMU context of a group unless all of the devices in the group are "viable", ie. they are released from any host drivers.

Quoted 5 lines

Honestly, I'm not sure the current group FD is actually showing that very strongly - though I get the point it is modeled in the sysfs and kind of implicit in the API - we evolved things in a way where most actual applications are taking in a PCI BDF from the user, not a group reference. So the actual security impact seems lost on the user.

vfio users are extremely aware of grouping, they understand the model, if not always the reason for the grouping. You only need to look at r/VFIO to find various lsgroup scripts and kernel patches to manipulate grouping. The visibility to the user is valuable imo.

Quoted 8 lines

Along my sketch if we have:

   ioctl(vifo_device_fd, JOIN_IOASID_FD, ioasifd)
   [..]
   ioctl(vfio_device, ATTACH_IOASID, gpa_ioasid_id) == ENOPERM

I would feel comfortable if the ATTACH_IOASID fails by default if all devices in the group have not been joined to the same ioasidfd.

And without a group representation to userspace, how would a user know to resolve that?

Quoted 6 lines

So in the NIC&HD example the application would need to do:

   ioasid_fd = open("/dev/ioasid");
   nic_device_fd = open("/dev/vfio/device0")
   hd_device_fd = open("/dev/vfio/device1")
Quoted 6 lines

ioctl(nic_device_fd, JOIN_IOASID_FD, ioasifd) ioctl(hd_device_fd, JOIN_IOASID_FD, ioasifd)
[..]
ioctl(nice_device, ATTACH_IOASID, gpa_ioasid_id) == SUCCESS

Now the security relation is forced by the kernel to be very explicit.

But not discoverable to the user.

Quoted 12 lines

However to keep current semantics, I'd suggest a flag on JOIN_IOASID_FD called "IOASID_IMPLICIT_GROUP" which has the effect of allowing the ATTACH_IOASID to succeed without the user having to explicitly join all the group devices. This is analogous to the world we have today of opening the VFIO group FD but only instantiating one device FD.

In effect the ioasid FD becomes the group and the numbered IOASID's inside the FD become the /dev/vfio/vfio objects - we don't end up with fewer objects in the system, they just have different uAPI presentations.

Quoted 5 lines

I'd envision applications like DPDK that are BDF centric to use the first API with some '--allow-insecure-vfio' flag to switch on the IOASID_IMPLICIT_GROUP. Maybe good applications would also print:
"Danger Will Robinson these PCI BDFs [...] are also at risk"
When the switch is used by parsing the sysfs

So the group still exist in sysfs, they just don't have vfio representations? An implicit grouping does what, automatically unbind the devices, so an admin gives a user access to the NIC but their HBA device disappears because they were implicitly linked? That's why vfio basis ownership on the group, if a user owns the group but the group is not viable because a device is still bound to another kernel driver, the use can't do anything. Implicitly snarfing up subtly affected devices is bad.

Quoted 7 lines
Quoted 6 lines
Quoted 3 lines

Thus /dev/ioasid also becomes the unit of security and the IOMMU subsystem level becomes aware of and enforces the group security rules. Userspace does not need to "see" the group

What tools does userspace have to understand isolation of individual devices without groups?

Quoted 8 lines

I think we can continue to show all of this group information in sysfs files, it just doesn't require application code to open a group FD.

This becomes relavent the more I think about it - elmininating the group and container FD uAPI by directly creating the device FD also sidesteps questions about how to model these objects in a /dev/ioasid only world. We simply don't have them at all so the answer is pretty easy.

I'm not sold. Ideally each device would be fully isolated, then we could assume a 1:1 relation of group and device and collapse the model to work on devices. We don't live in that world and I see a benefit to making that explicit in the uapi, even if that group fd might seem superfluous at times. Thanks,

Alex

#133 lore raw

Re: [PATCH V4 05/18] iommu/ioasid: Redefine IOASID set and allocation APIs

Quoted 4 lines · Alex Williamson

If by "classic" you mean conventional PCI bus, then this is much worse than simply "at risk". The IOMMU cannot differentiate devices behind a PCIe-to-PCI bridge, so the moment you turn on the IOMMU context for the NIC, the address space for your HBA is pulled out from under it.

Yes, I understand this, but this is fine and not really surprising if the HD device is just forced to remain "unusued"

To my mind the bigger issue is the NIC now has access to the HD and nobody really raised an alarm unless the HD happened to have a kernel driver bound.

Quoted 4 lines

the vfio world, the NIC and HBA are grouped and managed together, the user cannot change the IOMMU context of a group unless all of the devices in the group are "viable", ie. they are released from any host drivers.

Yes, I don't propose to change any of that, I just suggest to make the 'change the IOMMU context" into "join a /dev/ioasid fd"

All devices in the group have to be joined to the same ioasid or, with the flag, left "unused" with no kernel driver.

This is the same viability test VFIO is doing now, just moved slightly in the programming flow.

Quoted 4 lines

vfio users are extremely aware of grouping, they understand the model, if not always the reason for the grouping. You only need to look at r/VFIO to find various lsgroup scripts and kernel patches to manipulate grouping. The visibility to the user is valuable imo.

I don't propose to remove visibility, sysfs and the lsgroup scripts should all still be there.

I'm just acknowledging reality that the user command line experiance we have is focused on single BDFs not on groups. The user only sees the group idea when things explode, so why do we have it as such an integral part of the programming model?

Quoted 9 lines
Quoted 6 lines
   ioctl(vifo_device_fd, JOIN_IOASID_FD, ioasifd)
   [..]
   ioctl(vfio_device, ATTACH_IOASID, gpa_ioasid_id) == ENOPERM

I would feel comfortable if the ATTACH_IOASID fails by default if all devices in the group have not been joined to the same ioasidfd.

And without a group representation to userspace, how would a user know to resolve that?

Userspace can continue to read sysfs files that show the group relation.

I'm only talking about the group char device and FD.

Quoted 4 lines

So the group still exist in sysfs, they just don't have vfio representations? An implicit grouping does what, automatically unbind the devices, so an admin gives a user access to the NIC but their HBA device disappears because they were implicitly linked?

It does exactly the same thing as opening the VFIO group FD and instantiating a single device FD does today.

Most software, like dpdk, automatically deduces the VFIO group from the commandline BDF, I'm mainly suggesting we move that deduction from userspace software to kernel software.

Quoted 4 lines

basis ownership on the group, if a user owns the group but the group is not viable because a device is still bound to another kernel driver, the use can't do anything. Implicitly snarfing up subtly affected devices is bad.

The user would get an /dev/ioasid join failure just like they get a failure from VFIO_GROUP_SET_CONTAINER (?) today that reflects the group is not viable.

Otherwise what is the alternative?

How do we model the VFIO group security concept to something like VDPA?

How do you reconcile the ioasid security model with the VFIO container and group FDs?

Jason

#134 lore raw

Re: [PATCH V4 05/18] iommu/ioasid: Redefine IOASID set and allocation APIs

Quoted 33 lines · On Thu, 22 Apr 2021 17:00:24 -0300 Jason Gunthorpe…
Quoted 4 lines · Alex Williamson

If by "classic" you mean conventional PCI bus, then this is much worse than simply "at risk". The IOMMU cannot differentiate devices behind a PCIe-to-PCI bridge, so the moment you turn on the IOMMU context for the NIC, the address space for your HBA is pulled out from under it.

Yes, I understand this, but this is fine and not really surprising if the HD device is just forced to remain "unusued"

To my mind the bigger issue is the NIC now has access to the HD and nobody really raised an alarm unless the HD happened to have a kernel driver bound.

Quoted 4 lines

the vfio world, the NIC and HBA are grouped and managed together, the user cannot change the IOMMU context of a group unless all of the devices in the group are "viable", ie. they are released from any host drivers.

Yes, I don't propose to change any of that, I just suggest to make the 'change the IOMMU context" into "join a /dev/ioasid fd"

All devices in the group have to be joined to the same ioasid or, with the flag, left "unused" with no kernel driver.

This is the same viability test VFIO is doing now, just moved slightly in the programming flow.

Quoted 4 lines

vfio users are extremely aware of grouping, they understand the model, if not always the reason for the grouping. You only need to look at r/VFIO to find various lsgroup scripts and kernel patches to manipulate grouping. The visibility to the user is valuable imo.

Quoted 7 lines

I don't propose to remove visibility, sysfs and the lsgroup scripts should all still be there.

I'm just acknowledging reality that the user command line experiance we have is focused on single BDFs not on groups. The user only sees the group idea when things explode, so why do we have it as such an integral part of the programming model?

Because it's fundamental to the isolation of the device? What you're proposing doesn't get around the group issue, it just makes it implicit rather than explicit in the uapi. For what? Some ideal notion that every device should be isolated at the expense of userspace drivers that then fail randomly because they didn't take into account groups because it's not part of the uapi?

Quoted 32 lines
Quoted 9 lines
Quoted 6 lines
   ioctl(vifo_device_fd, JOIN_IOASID_FD, ioasifd)
   [..]
   ioctl(vfio_device, ATTACH_IOASID, gpa_ioasid_id) == ENOPERM

I would feel comfortable if the ATTACH_IOASID fails by default if all devices in the group have not been joined to the same ioasidfd.

And without a group representation to userspace, how would a user know to resolve that?

Userspace can continue to read sysfs files that show the group relation.

I'm only talking about the group char device and FD.

Quoted 4 lines

So the group still exist in sysfs, they just don't have vfio representations? An implicit grouping does what, automatically unbind the devices, so an admin gives a user access to the NIC but their HBA device disappears because they were implicitly linked?

It does exactly the same thing as opening the VFIO group FD and instantiating a single device FD does today.

Most software, like dpdk, automatically deduces the VFIO group from the commandline BDF, I'm mainly suggesting we move that deduction from userspace software to kernel software.

Quoted 4 lines

basis ownership on the group, if a user owns the group but the group is not viable because a device is still bound to another kernel driver, the use can't do anything. Implicitly snarfing up subtly affected devices is bad.

Quoted 8 lines

The user would get an /dev/ioasid join failure just like they get a failure from VFIO_GROUP_SET_CONTAINER (?) today that reflects the group is not viable.

Otherwise what is the alternative?

How do we model the VFIO group security concept to something like VDPA?

Is it really a "VFIO group security concept"? We're reflecting the reality of the hardware, not all devices are fully isolated. An IOMMU group is the smallest set of devices we believe are isolated from all other sets of devices. VFIO groups simply reflect that notion of an IOMMU group. This is the reality that any userspace driver needs to play in, it doesn't magically go away because we drop the group file descriptor. It only makes the uapi more difficult to use correctly because userspace drivers need to go outside of the uapi to have any idea that this restriction exists. Thanks,

Alex

#135 lore raw

Re: [PATCH V4 05/18] iommu/ioasid: Redefine IOASID set and allocation APIs

Quoted 3 lines · Alex Williamson

Because it's fundamental to the isolation of the device? What you're proposing doesn't get around the group issue, it just makes it implicit rather than explicit in the uapi.

I'm not even sure it makes it explicit or implicit, it just takes away the FD.

There are four group IOCTLs, I see them mapping to /dev/ioasid follows:
VFIO_GROUP_GET_STATUS -
+ VFIO_GROUP_FLAGS_CONTAINER_SET is fairly redundant
+ VFIO_GROUP_FLAGS_VIABLE could be in a new sysfs under kernel/iomm_groups, or could be an IOCTL on /dev/ioasid IOASID_ALL_DEVICES_VIABLE

VFIO_GROUP_SET_CONTAINER -
+ This happens implicitly when the device joins the IOASID so it gets moved to the vfio_device FD:
ioctl(vifo_device_fd, JOIN_IOASID_FD, ioasifd)

VFIO_GROUP_UNSET_CONTAINER -
+ Also moved to the vfio_device FD, opposite of JOIN_IOASID_FD

VFIO_GROUP_GET_DEVICE_FD -
+ Replaced by opening /dev/vfio/deviceX
Learn the deviceX which will be the cdev sysfs shows as:
/sys/devices/pci0000:00/0000:00:01.0/0000:01:00.0/vfio/deviceX/dev
Open /dev/vfio/deviceX

Quoted 5 lines
Quoted 2 lines

How do we model the VFIO group security concept to something like VDPA?

Is it really a "VFIO group security concept"? We're reflecting the reality of the hardware, not all devices are fully isolated.

Well, exactly.

/dev/ioasid should understand the group concept somehow, otherwise it is incomplete and maybe even security broken.

So, how do I add groups to, say, VDPA in a way that makes sense? The only answer I come to is broadly what I outlined here - make /dev/ioasid do all the group operations, and do them when we enjoin the VDPA device to the ioasid.

Once I have solved all the groups problems with the non-VFIO users, then where does that leave VFIO? Why does VFIO need a group FD if everyone else doesn't?

Quoted 3 lines

IOMMU group. This is the reality that any userspace driver needs to play in, it doesn't magically go away because we drop the group file descriptor.

I'm not saying it does, I'm saying it makes the uAPI more regular and easier to fit into /dev/ioasid without the group FD.

Quoted 3 lines

It only makes the uapi more difficult to use correctly because userspace drivers need to go outside of the uapi to have any idea that this restriction exists.

I don't think it makes any substantive difference one way or the other.

With the group FD: the userspace has to read sysfs, find the list of devices in the group, open the group fd, create device FDs for each device using the name from sysfs.

Starting from a BDF the general pseudo code is
group_path = readlink("/sys/bus/pci/devices/BDF/iommu_group") group_name = basename(group_path)
group_fd = open("/dev/vfio/"+group_name)
device_fd = ioctl(VFIO_GROUP_GET_DEVICE_FD, BDF);

Without the group FD: the userspace has to read sysfs, find the list of devices in the group and then open the device-specific cdev (found via sysfs) and link them to a /dev/ioasid FD.

Starting from a BDF the general pseudo code is:
device_name = first_directory_of("/sys/bus/pci/devices/BDF/vfio/") device_fd = open("/dev/vfio/"+device_name)
ioasidfd = open("/dev/ioasid")
ioctl(device_fd, JOIN_IOASID_FD, ioasidfd)

These two routes can have identical outcomes and identical security checks.

In both cases if userspace wants a list of BDFs in the same group as the BDF it is interested in:
readdir("/sys/bus/pci/devices/BDF/iommu_group/devices")

It seems like a very small difference to me.

I still don't see how the group restriction gets surfaced to the application through the group FD. The applications I looked through just treat the group FD as a step on their way to get the device_fd.

Jason

#70 lore raw

RE: [PATCH V4 05/18] iommu/ioasid: Redefine IOASID set and allocation APIs

In reply to Jason Gunthorpe
Quoted 5 lines

From: Jason Gunthorpe <[email protected]>
Sent: Thursday, April 22, 2021 8:10 PM

On Thu, Apr 22, 2021 at 08:34:32AM +0000, Tian, Kevin wrote:

Quoted 8 lines
Quoted 3 lines

Another tricky thing is that a container may be linked to multiple iommu domains in VFIO, as devices in the container may locate behind different IOMMUs with inconsistent capability (commit 1ef3e2bc).

Frankly this sounds over complicated. I would think /dev/ioasid should select the IOMMU when the first device is joined, and all future joins must be compatible with the original IOMMU - ie there is only one set of IOMMU capabilities in a /dev/ioasid.

Or could we still have just one /dev/ioasid but allow userspace to create multiple gpa_ioasid_id's each associated to a different iommu domain? Then the compatibility check will be done at ATTACH_IOASID instead of JOIN_IOASID_FD.

This does impose one burden to userspace though, to understand the IOMMU compatibilities and figure out which incompatible features may affect the page table management (while such knowledge is IOMMU vendor specific) and then explicitly manage multiple /dev/ioasid's or multiple gpa_ioasid_id's.

Alternatively is it a good design by having the kernel return error at attach/join time to indicate that incompatibility is detected then the userspace should open a new /dev/ioasid or creates a new gpa_ioasid_id for the failing device upon such failure, w/o constructing its own compatibility knowledge?

Quoted 3 lines

This means qemue might have multiple /dev/ioasid's if the system has multiple incompatible IOMMUs (is this actually a thing?) The platform

One example is Intel platform with igd. Typically there is one IOMMU dedicated for igd and the other IOMMU serving all the remaining devices. The igd IOMMU may not support IOMMU_CACHE while the other one does.

Quoted 6 lines

should design its IOMMU domains to minimize the number of /dev/ioasid's required.

Is there a reason we need to share IOASID'd between completely divergance IOMMU implementations? I don't expect the HW should be able to physically share page tables??

yes, e.g. in vSVA both devices (behind divergence IOMMUs) are bound to a single guest process which has an unique PASID and 1st-level page table. Earlier incompatibility example is only for 2nd-level.

Quoted 3 lines

That decision point alone might be the thing that just says we can't ever have /dev/vfio/vfio == /dev/ioasid

yes, unless we adopt the vfio scheme, i.e. implicitly managing incompatible iommu domains in /dev/ioasid.

Quoted 9 lines
Quoted 5 lines

Just to confirm. Above flow is for current map/unmap flavor as what VFIO/vDPA do today. Later when nested translation is supported, there is no need to detach gpa_ioasid_fd. Instead, a new cmd will be introduced to nest rid_ioasid_fd on top of gpa_ioasid_fd:

Sure.. The tricky bit will be to define both of the common nested operating modes.

Quoted 8 lines
  nested_ioasid = ioctl(ioasid_fd, CREATE_NESTED_IOASID,  gpa_ioasid_id);
  ioctl(ioasid_fd, SET_NESTED_IOASID_PAGE_TABLES, nested_ioasid, ..)
   // IOMMU will match on the device RID, no PASID:

ioctl(vfio_device, ATTACH_IOASID, nested_ioasid);

   // IOMMU will match on the device RID and PASID:

ioctl(vfio_device, ATTACH_IOASID_PASID, pasid, nested_ioasid);

I'm a bit confused here why we have both pasid and ioasid notations together. Why not use nested_ioasid as pasid directly (i.e. every pasid in nested mode is created by CREATE_NESTED_IOASID)?

Below I list different scenarios for ATTACH_IOASID in my view. Here vfio_device could be a real PCI function (RID), or a subfunction device (RID+def_ioasid). The vfio_device could be attached to a gpa_ioasid (RID in guest view, no nesting), a nested_ioasid (RID in guest view, nesting) or a nested_ioasid (RID+PASID in guest view, nesting).

// IOMMU will match on the device RID, no nesting, no PASID:
ioctl(vfio_device, ATTACH_IOASID, gpa_ioasid);

// IOMMU will match on the device (RID+def_ioasid), no nesting, no PASID:
ioctl(vfio_subdevice, ATTACH_IOASID, gpa_ioasid);

// IOMMU will match on the device RID, nesting, no PASID:
ioctl(vfio_device, ATTACH_IOASID, nested_ioasid);

// IOMMU will match on the device (RID+def_ioasid), nesting, no PASID:
ioctl(vfio_subdevice, ATTACH_IOASID, nested_ioasid);

// IOMMU will match on the device (RID+nested_ioasid), nesting, PASID:
ioctl(vfio_device, ATTACH_IOASID_PASID, nested_ioasid);

// IOMMU will match on the device (RID+nested_ioasid), nesting, PASID:
ioctl(vfio_subdevice, ATTACH_IOASID_PASID, nested_ioasid);

Quoted 14 lines

Notice that ATTACH (or bind, whatever) is always done on the vfio_device FD. ATTACH tells the IOMMU HW to link the PCI BDF&PASID to a specific page table defined by an IOASID.

I expect we have many flavours of IOASID tables, eg we have normal, and 'nested with table controlled by hypervisor'. ARM has 'nested with table controlled by guest' right? So like this?

nested_ioasid = ioctl(ioasid_fd, CREATE_DELGATED_IOASID,
gpa_ioasid_id, <some kind of viommu_id>)
// PASID now goes to <viommu_id>
ioctl(vfio_device, ATTACH_IOASID_PASID, pasid, nested_ioasid);

Quoted 7 lines

Where <viommu_id> is some internal to the guest handle of the viommu page table scoped within gpa_ioasid_id? Like maybe it is GPA of the base of the page table?

The guest can't select its own PASIDs without telling the hypervisor, right?

If the whole PASID table is delegated to the guest in ARM case, the guest can select its own PASIDs w/o telling the hypervisor. I haven't thought carefully about a clean way to support this scheme, e.g. if mandating guest to always allocate PASIDs through hypervisor even in this case would it make the uAPI simpler...

Thanks
Kevin

#136 lore raw

RE: [PATCH V4 05/18] iommu/ioasid: Redefine IOASID set and allocation APIs

In reply to Jason Gunthorpe
Quoted 94 lines

From: Jason Gunthorpe <[email protected]>
Sent: Friday, April 23, 2021 7:40 AM

Quoted 3 lines · Alex Williamson

Because it's fundamental to the isolation of the device? What you're proposing doesn't get around the group issue, it just makes it implicit rather than explicit in the uapi.

I'm not even sure it makes it explicit or implicit, it just takes away the FD.

There are four group IOCTLs, I see them mapping to /dev/ioasid follows:
VFIO_GROUP_GET_STATUS -
+ VFIO_GROUP_FLAGS_CONTAINER_SET is fairly redundant
+ VFIO_GROUP_FLAGS_VIABLE could be in a new sysfs under kernel/iomm_groups, or could be an IOCTL on /dev/ioasid IOASID_ALL_DEVICES_VIABLE

VFIO_GROUP_SET_CONTAINER -
+ This happens implicitly when the device joins the IOASID so it gets moved to the vfio_device FD:
ioctl(vifo_device_fd, JOIN_IOASID_FD, ioasifd)

VFIO_GROUP_UNSET_CONTAINER -
+ Also moved to the vfio_device FD, opposite of JOIN_IOASID_FD

VFIO_GROUP_GET_DEVICE_FD -
+ Replaced by opening /dev/vfio/deviceX
Learn the deviceX which will be the cdev sysfs shows as:
/sys/devices/pci0000:00/0000:00:01.0/0000:01:00.0/vfio/deviceX/dev
Open /dev/vfio/deviceX

Quoted 5 lines
Quoted 2 lines

How do we model the VFIO group security concept to something like VDPA?

Is it really a "VFIO group security concept"? We're reflecting the reality of the hardware, not all devices are fully isolated.

Well, exactly.

/dev/ioasid should understand the group concept somehow, otherwise it is incomplete and maybe even security broken.

So, how do I add groups to, say, VDPA in a way that makes sense? The only answer I come to is broadly what I outlined here - make /dev/ioasid do all the group operations, and do them when we enjoin the VDPA device to the ioasid.

Once I have solved all the groups problems with the non-VFIO users, then where does that leave VFIO? Why does VFIO need a group FD if everyone else doesn't?

Quoted 3 lines

IOMMU group. This is the reality that any userspace driver needs to play in, it doesn't magically go away because we drop the group file descriptor.

I'm not saying it does, I'm saying it makes the uAPI more regular and easier to fit into /dev/ioasid without the group FD.

Quoted 3 lines

It only makes the uapi more difficult to use correctly because userspace drivers need to go outside of the uapi to have any idea that this restriction exists.

I don't think it makes any substantive difference one way or the other.

With the group FD: the userspace has to read sysfs, find the list of devices in the group, open the group fd, create device FDs for each device using the name from sysfs.

Starting from a BDF the general pseudo code is
group_path = readlink("/sys/bus/pci/devices/BDF/iommu_group") group_name = basename(group_path)
group_fd = open("/dev/vfio/"+group_name)
device_fd = ioctl(VFIO_GROUP_GET_DEVICE_FD, BDF);

Without the group FD: the userspace has to read sysfs, find the list of devices in the group and then open the device-specific cdev (found via sysfs) and link them to a /dev/ioasid FD.

Starting from a BDF the general pseudo code is:
device_name = first_directory_of("/sys/bus/pci/devices/BDF/vfio/") device_fd = open("/dev/vfio/"+device_name)
ioasidfd = open("/dev/ioasid")
ioctl(device_fd, JOIN_IOASID_FD, ioasidfd)

These two routes can have identical outcomes and identical security checks.

In both cases if userspace wants a list of BDFs in the same group as the BDF it is interested in:
readdir("/sys/bus/pci/devices/BDF/iommu_group/devices")

Quoted 6 lines

It seems like a very small difference to me.

I still don't see how the group restriction gets surfaced to the application through the group FD. The applications I looked through just treat the group FD as a step on their way to get the device_fd.

So your proposal sort of moves the entire container/group/domain managment into /dev/ioasid and then leaves vfio only provide device specific uAPI. An ioasid represents a page table (address space), thus is equivalent to the scope of VFIO container. Having the device join an ioasid is equivalent to attaching a device to VFIO container, and here the group integrity must be enforced. Then /dev/ioasid anyway needs to manage group objects and their association with ioasid and underlying iommu domain thus it's pointless to keep same logic within VFIO. Is this understanding correct?

btw one remaining open is whether you expect /dev/ioasid to be associated with a single iommu domain, or multiple. If only a single domain is allowed, the ioasid_fd is equivalent to the scope of VFIO container. It is supposed to have only one gpa_ioasid_id since one iommu domain can only have a single 2nd level pgtable. Then all other ioasids, once allocated, must be nested on this gpa_ioasid_id to fit in the same domain. if a legacy vIOMMU is exposed (which disallows nesting), the userspace has to open an ioasid_fd for every group. This is basically the VFIO way. On the other hand if multiple domains is allowed, there could be multiple ioasid_ids each holding a 2nd level pgtable and an iommu domain (or a list of pgtables and domains due to incompatibility issue as discussed in another thread), and can be nested by other ioasids respectively. The application only needs to open /dev/ioasid once regardless of whether vIOMMU allows nesting, and has a single interface for ioasid allocation. Which way do you prefer to?

Thanks
Kevin

#71 lore raw

Re: [PATCH V4 05/18] iommu/ioasid: Redefine IOASID set and allocation APIs

In reply to Tian, Kevin
Quoted 4 lines · Kevin

Or could we still have just one /dev/ioasid but allow userspace to create multiple gpa_ioasid_id's each associated to a different iommu domain? Then the compatibility check will be done at ATTACH_IOASID instead of JOIN_IOASID_FD.

To my mind what makes sense that that /dev/ioasid presents a single IOMMU behavior that is basically the same. This may ultimately not be what we call a domain today.

We may end up with a middle object which is a group of domains that all have the same capabilities, and we define capabilities in a way that most platforms have a single group of domains.

The key capability of a group of domains is they can all share the HW page table representation, so if an IOASID instantiates a page table it can be assigned to any device on any domain in the gruop of domains.

If you try to say that /dev/ioasid has many domains and they can't have their HW page tables shared then I think the implementation complexity will explode.

Quoted 5 lines

This does impose one burden to userspace though, to understand the IOMMU compatibilities and figure out which incompatible features may affect the page table management (while such knowledge is IOMMU vendor specific) and then explicitly manage multiple /dev/ioasid's or multiple gpa_ioasid_id's.

Right, this seems very hard in the general case..

Quoted 5 lines

Alternatively is it a good design by having the kernel return error at attach/join time to indicate that incompatibility is detected then the userspace should open a new /dev/ioasid or creates a new gpa_ioasid_id for the failing device upon such failure, w/o constructing its own compatibility knowledge?

Yes, this feels workable too

Quoted 7 lines
Quoted 2 lines

This means qemue might have multiple /dev/ioasid's if the system has multiple incompatible IOMMUs (is this actually a thing?) The platform

One example is Intel platform with igd. Typically there is one IOMMU dedicated for igd and the other IOMMU serving all the remaining devices. The igd IOMMU may not support IOMMU_CACHE while the other one does.

If we can do as above the two domains may be in the same group of domains and the IOMMU_CACHE is not exposed at the /dev/ioasid level.

For instance the API could specifiy IOMMU_CACHE during attach, not during IOASID creation.

Getting all the data model right in the API is going to be trickiest part of this.

Quoted 3 lines

yes, e.g. in vSVA both devices (behind divergence IOMMUs) are bound to a single guest process which has an unique PASID and 1st-level page table. Earlier incompatibility example is only for 2nd-level.

Because when we get to here, things become inscrutable as an API if you are trying to say two different IOMMU presentations can actually be nested.

Quoted 9 lines
Quoted 9 lines

Sure.. The tricky bit will be to define both of the common nested operating modes.

  nested_ioasid = ioctl(ioasid_fd, CREATE_NESTED_IOASID,  gpa_ioasid_id);
  ioctl(ioasid_fd, SET_NESTED_IOASID_PAGE_TABLES, nested_ioasid, ..)
   // IOMMU will match on the device RID, no PASID:

ioctl(vfio_device, ATTACH_IOASID, nested_ioasid);

Quoted 6 lines
Quoted 2 lines
   // IOMMU will match on the device RID and PASID:

ioctl(vfio_device, ATTACH_IOASID_PASID, pasid, nested_ioasid);

I'm a bit confused here why we have both pasid and ioasid notations together. Why not use nested_ioasid as pasid directly (i.e. every pasid in nested mode is created by CREATE_NESTED_IOASID)?

The IOASID is not a PASID, it is just a page table.

A generic IOMMU matches on either RID or (RID,PASID), so you should specify the PASID when establishing the match.

IOASID only specifies the page table.

So you read the above as configuring the path

PCI_DEVICE -> (RID,PASID) -> nested_ioasid -> gpa_ioasid_id -> physical

Where (RID,PASID) indicate values taken from the PCI packet.

In principle the IOMMU could also be commanded to reuse the same ioasid page table with a different PASID:

PCI_DEVICE_B -> (RID_B,PASID_B) -> nested_ioasid -> gpa_ioasid_id -> physical

This is impossible if the ioasid == PASID in the API.

Quoted 3 lines

Below I list different scenarios for ATTACH_IOASID in my view. Here vfio_device could be a real PCI function (RID), or a subfunction device (RID+def_ioasid).

What is RID+def_ioasid? The IOMMU does not match on IOASID's.

A subfunction device always need to use PASID, or an internal IOMMU, confused what you are trying to explain?

Quoted 2 lines

If the whole PASID table is delegated to the guest in ARM case, the guest can select its own PASIDs w/o telling the hypervisor.

The hypervisor has to route the PASID's to the guest at some point - a guest can't just claim a PASID unilaterally, that would not be secure.

If it is not done with per-PASID hypercalls then the hypervisor has to route all PASID's for a RID to the guest and /dev/ioasid needs to have a nested IOASID object that represents this connection - ie it points to the PASID table of the guest vIOMMU or something.

Remember this all has to be compatible with mdev's too and without hypercalls to create PASIDs that will be hard: mdev sharing a RID and slicing the physical PASIDs can't support a 'send all PASIDs to the guest' model, or even a 'the guest gets to pick the PASID' option.

Jason

#137 lore raw

Re: [PATCH V4 05/18] iommu/ioasid: Redefine IOASID set and allocation APIs

In reply to Tian, Kevin
Quoted 9 lines · Kevin

So your proposal sort of moves the entire container/group/domain managment into /dev/ioasid and then leaves vfio only provide device specific uAPI. An ioasid represents a page table (address space), thus is equivalent to the scope of VFIO container. Having the device join an ioasid is equivalent to attaching a device to VFIO container, and here the group integrity must be enforced. Then /dev/ioasid anyway needs to manage group objects and their association with ioasid and underlying iommu domain thus it's pointless to keep same logic within VFIO. Is this understanding correct?

Yes, I haven't thought of a way to define /dev/ioasid in a way that is useful to VDPA/etc without all these parts.. If you come up with a better idea do share.

Quoted 4 lines

btw one remaining open is whether you expect /dev/ioasid to be associated with a single iommu domain, or multiple. If only a single domain is allowed, the ioasid_fd is equivalent to the scope of VFIO container.

See the prior email for a more complete set of thoughts on this.

Quoted 5 lines

It is supposed to have only one gpa_ioasid_id since one iommu domain can only have a single 2nd level pgtable. Then all other ioasids, once allocated, must be nested on this gpa_ioasid_id to fit in the same domain. if a legacy vIOMMU is exposed (which disallows nesting), the userspace has to open an ioasid_fd for every group.

Quoted 8 lines

This is basically the VFIO way. On the other hand if multiple domains is allowed, there could be multiple ioasid_ids each holding a 2nd level pgtable and an iommu domain (or a list of pgtables and domains due to incompatibility issue as discussed in another thread), and can be nested by other ioasids respectively. The application only needs to open /dev/ioasid once regardless of whether vIOMMU allows nesting, and has a single interface for ioasid allocation. Which way do you prefer to?

I have a feeling we want to have a single IOASID be usable in as many contexts as possible - as many domains, devices and groups as we can get away with.

The IOASID is the expensive object, it is the pagetable, it is potentially a lot of memory. The API should be designed so we don't have to have multiple copies of the same pagetable.

For this reason I think having multiple IOASID's in a single /dev/ioasid container is the way to go.

To my mind the /dev/ioasid should be linked to a HW page table format and any device/domain/group that uses that same HW page table format can be joined to it. This implies we can have multiple domains under /dev/ioasid, but there is a limitation on what domains can be grouped together.

This probably does not match the exact IOMMU capability/domain model we have today, so I present it as an inspirational goal. The other tricky thing here will be to define small steps..

eg V1 may only allow one domain, but the uAPI will not reflect this as we expect V2 will allow multiple domains..

Jason

#141 lore raw

Re: [PATCH V4 05/18] iommu/ioasid: Redefine IOASID set and allocation APIs

In reply to Jason Gunthorpe
Quoted 41 lines · On Thu, 22 Apr 2021 20:39:50 -0300 Jason Gunthorpe…
Quoted 3 lines · Alex Williamson

Because it's fundamental to the isolation of the device? What you're proposing doesn't get around the group issue, it just makes it implicit rather than explicit in the uapi.

I'm not even sure it makes it explicit or implicit, it just takes away the FD.

There are four group IOCTLs, I see them mapping to /dev/ioasid follows:
VFIO_GROUP_GET_STATUS -
+ VFIO_GROUP_FLAGS_CONTAINER_SET is fairly redundant
+ VFIO_GROUP_FLAGS_VIABLE could be in a new sysfs under kernel/iomm_groups, or could be an IOCTL on /dev/ioasid IOASID_ALL_DEVICES_VIABLE

VFIO_GROUP_SET_CONTAINER -
+ This happens implicitly when the device joins the IOASID so it gets moved to the vfio_device FD:
ioctl(vifo_device_fd, JOIN_IOASID_FD, ioasifd)

VFIO_GROUP_UNSET_CONTAINER -
+ Also moved to the vfio_device FD, opposite of JOIN_IOASID_FD

VFIO_GROUP_GET_DEVICE_FD -
+ Replaced by opening /dev/vfio/deviceX
Learn the deviceX which will be the cdev sysfs shows as:
/sys/devices/pci0000:00/0000:00:01.0/0000:01:00.0/vfio/deviceX/dev
Open /dev/vfio/deviceX

Quoted 5 lines
Quoted 2 lines

How do we model the VFIO group security concept to something like VDPA?

Is it really a "VFIO group security concept"? We're reflecting the reality of the hardware, not all devices are fully isolated.

Well, exactly.

/dev/ioasid should understand the group concept somehow, otherwise it is incomplete and maybe even security broken.

Quoted 8 lines

So, how do I add groups to, say, VDPA in a way that makes sense? The only answer I come to is broadly what I outlined here - make /dev/ioasid do all the group operations, and do them when we enjoin the VDPA device to the ioasid.

Once I have solved all the groups problems with the non-VFIO users, then where does that leave VFIO? Why does VFIO need a group FD if everyone else doesn't?

This assumes there's a solution for vDPA that doesn't just ignore the problem and hope for the best. I can't speak to a vDPA solution.

Quoted 28 lines
Quoted 3 lines

IOMMU group. This is the reality that any userspace driver needs to play in, it doesn't magically go away because we drop the group file descriptor.

I'm not saying it does, I'm saying it makes the uAPI more regular and easier to fit into /dev/ioasid without the group FD.

Quoted 3 lines

It only makes the uapi more difficult to use correctly because userspace drivers need to go outside of the uapi to have any idea that this restriction exists.

I don't think it makes any substantive difference one way or the other.

With the group FD: the userspace has to read sysfs, find the list of devices in the group, open the group fd, create device FDs for each device using the name from sysfs.

Starting from a BDF the general pseudo code is
group_path = readlink("/sys/bus/pci/devices/BDF/iommu_group") group_name = basename(group_path)
group_fd = open("/dev/vfio/"+group_name)
device_fd = ioctl(VFIO_GROUP_GET_DEVICE_FD, BDF);

Without the group FD: the userspace has to read sysfs, find the list of devices in the group and then open the device-specific cdev (found via sysfs) and link them to a /dev/ioasid FD.

Quoted 5 lines

Starting from a BDF the general pseudo code is:
device_name = first_directory_of("/sys/bus/pci/devices/BDF/vfio/") device_fd = open("/dev/vfio/"+device_name)
ioasidfd = open("/dev/ioasid")
ioctl(device_fd, JOIN_IOASID_FD, ioasidfd)

This is exactly the implicit vs explicit semantics. In the existing vfio case, the user needs to explicitly interact with the group. In your proposal, the user interacts with the device, the group concept is an implicit restriction. You've listed a step in the description about a "list of devices in the group", but nothing in the pseudo code reflects that step. I expect it would be a subtly missed by any userspace driver developer unless they happen to work on a system where the grouping is not ideal. I think that makes the interface hard to use correctly.

Quoted 8 lines

These two routes can have identical outcomes and identical security checks.

In both cases if userspace wants a list of BDFs in the same group as the BDF it is interested in:
readdir("/sys/bus/pci/devices/BDF/iommu_group/devices")

It seems like a very small difference to me.

The difference is that the group becomes a nuance, that I expect would be ignored, rather than a first class concept in the API.

Quoted 3 lines

I still don't see how the group restriction gets surfaced to the application through the group FD. The applications I looked through just treat the group FD as a step on their way to get the device_fd.

A step where the developer hopefully recognizes that there might be other devices in a group, a group can't be opened more than once, the group has a flag indicating viability, they can't actually get the device fd until the group is attached to an IOMMU context, all devices in the group therefore have the same IOMMU context, and device fd isn't actually available until they've gone through a proper setup, which is an additional layer of protection. A userspace vfio developer may not understand why a group isn't viable, but they have that clue that it's something at the group level to investigate.

Most of the userspace vfio drivers that I haven't contributed myself have come about with little or no interaction from me, so I'd like to think that the vfio uapi is actually somewhat intuitive in its concepts and difficult to use incorrectly. Thanks,

Alex

#142 lore raw

Re: [PATCH V4 05/18] iommu/ioasid: Redefine IOASID set and allocation APIs

Quoted 10 lines · Alex Williamson

On Thu, 22 Apr 2021 20:39:50 -0300

Quoted 8 lines

/dev/ioasid should understand the group concept somehow, otherwise it is incomplete and maybe even security broken.

So, how do I add groups to, say, VDPA in a way that makes sense? The only answer I come to is broadly what I outlined here - make /dev/ioasid do all the group operations, and do them when we enjoin the VDPA device to the ioasid.

Quoted 6 lines
Quoted 3 lines

Once I have solved all the groups problems with the non-VFIO users, then where does that leave VFIO? Why does VFIO need a group FD if everyone else doesn't?

This assumes there's a solution for vDPA that doesn't just ignore the problem and hope for the best. I can't speak to a vDPA solution.

I don't think we can just ignore the question and succeed with /dev/ioasid.

Guess it should get answered as best it can for ioasid "in general" then we can decide if it makes sense for VFIO to use the group FD or not when working in ioasid mode.

Maybe a better idea will come up

Quoted 3 lines

an implicit restriction. You've listed a step in the description about a "list of devices in the group", but nothing in the pseudo code reflects that step.

I gave it below with the readdir() - it isn't in the pseudo code because the applications I looked through didn't use it, and wouldn't benefit from it. I tried to show what things were doing today.

Quoted 3 lines

I expect it would be a subtly missed by any userspace driver developer unless they happen to work on a system where the grouping is not ideal.

I'm still unclear - what are be the consequence if the application designer misses the group detail?

Jason

#72 lore raw

RE: [PATCH V4 05/18] iommu/ioasid: Redefine IOASID set and allocation APIs

In reply to Jason Gunthorpe
Quoted 14 lines

From: Jason Gunthorpe <[email protected]>
Sent: Friday, April 23, 2021 7:50 PM

Quoted 4 lines · Kevin

Or could we still have just one /dev/ioasid but allow userspace to create multiple gpa_ioasid_id's each associated to a different iommu domain? Then the compatibility check will be done at ATTACH_IOASID instead of JOIN_IOASID_FD.

To my mind what makes sense that that /dev/ioasid presents a single IOMMU behavior that is basically the same. This may ultimately not be what we call a domain today.

Quoted 7 lines

We may end up with a middle object which is a group of domains that all have the same capabilities, and we define capabilities in a way that most platforms have a single group of domains.

The key capability of a group of domains is they can all share the HW page table representation, so if an IOASID instantiates a page table it can be assigned to any device on any domain in the gruop of domains.

Sorry that I didn't quite get it. If a group of domains can share the same page table then why not just attaching all devices under those domains into a single domain? IMO the iommu domain is introduced to describe the HW page table. Ideally a new iommu domain should be created only when it's impossible to share an existing page table. Otherwise you'll get bad iotlb efficiency because each domain has its unique domain id (tagged in iotlb) then duplicated iotlb entries may exist even when a single page table is shared by those domains.

Then does it imply that you are actually suggesting /dev/ioasid associated with a single 2nd-level page table (which can be nested by multiple 1st-level page tables represented by other ioasids) thus a single iommu domain for all devices linked to compatible IOMMUs?

Or, can you elaborate what is the targeted usage by having a group of domains which all share the same page table?

Quoted 4 lines

If you try to say that /dev/ioasid has many domains and they can't have their HW page tables shared then I think the implementation complexity will explode.

Want to hear your opinion for one open here. There is no doubt that an ioasid represents a HW page table when the table is constructed by userspace and then linked to the IOMMU through the bind/unbind API. But I'm not very sure about whether an ioasid should represent the exact pgtable or the mapping metadata when the underlying pgtable is indirectly constructed through map/unmap API. VFIO does the latter way, which is why it allows multiple incompatible domains in a single container which all share the same mapping metadata.

Quoted 74 lines
Quoted 6 lines

This does impose one burden to userspace though, to understand the IOMMU compatibilities and figure out which incompatible features may affect the page table management (while such knowledge is IOMMU vendor specific) and then explicitly manage multiple /dev/ioasid's or multiple gpa_ioasid_id's.

Right, this seems very hard in the general case..

Quoted 5 lines

Alternatively is it a good design by having the kernel return error at attach/join time to indicate that incompatibility is detected then the userspace should open a new /dev/ioasid or creates a new gpa_ioasid_id for the failing device upon such failure, w/o constructing its own compatibility knowledge?

Yes, this feels workable too

Quoted 7 lines
Quoted 2 lines

This means qemue might have multiple /dev/ioasid's if the system has multiple incompatible IOMMUs (is this actually a thing?) The platform

One example is Intel platform with igd. Typically there is one IOMMU dedicated for igd and the other IOMMU serving all the remaining devices. The igd IOMMU may not support IOMMU_CACHE while the other one does.

If we can do as above the two domains may be in the same group of domains and the IOMMU_CACHE is not exposed at the /dev/ioasid level.

For instance the API could specifiy IOMMU_CACHE during attach, not during IOASID creation.

Getting all the data model right in the API is going to be trickiest part of this.

Quoted 3 lines

yes, e.g. in vSVA both devices (behind divergence IOMMUs) are bound to a single guest process which has an unique PASID and 1st-level page table. Earlier incompatibility example is only for 2nd-level.

Because when we get to here, things become inscrutable as an API if you are trying to say two different IOMMU presentations can actually be nested.

Quoted 4 lines
Quoted 4 lines

Sure.. The tricky bit will be to define both of the common nested operating modes.

nested_ioasid = ioctl(ioasid_fd, CREATE_NESTED_IOASID,

gpa_ioasid_id);

Quoted 9 lines
Quoted 7 lines

ioctl(ioasid_fd, SET_NESTED_IOASID_PAGE_TABLES, nested_ioasid, ..)

   // IOMMU will match on the device RID, no PASID:

ioctl(vfio_device, ATTACH_IOASID, nested_ioasid);

   // IOMMU will match on the device RID and PASID:

ioctl(vfio_device, ATTACH_IOASID_PASID, pasid, nested_ioasid);

I'm a bit confused here why we have both pasid and ioasid notations

together.

Quoted 1 line

Why not use nested_ioasid as pasid directly (i.e. every pasid in nested

mode

Quoted 1 line

is created by CREATE_NESTED_IOASID)?

The IOASID is not a PASID, it is just a page table.

A generic IOMMU matches on either RID or (RID,PASID), so you should specify the PASID when establishing the match.

IOASID only specifies the page table.

So you read the above as configuring the path

PCI_DEVICE -> (RID,PASID) -> nested_ioasid -> gpa_ioasid_id -> physical

Where (RID,PASID) indicate values taken from the PCI packet.

Quoted 7 lines

In principle the IOMMU could also be commanded to reuse the same ioasid page table with a different PASID:

PCI_DEVICE_B -> (RID_B,PASID_B) -> nested_ioasid -> gpa_ioasid_id ->
physical

This is impossible if the ioasid == PASID in the API.

OK, now I see where the disconnection comes from. In my context ioasid is the identifier that is actually used in the wire, but seems you treat it as a sw-defined namespace purely for representing page tables. We should clear this concept first before further discussing other details. 😊

Below is the description when the kernel ioasid allocator was introduced:

--
commit fa83433c92e340822a056a610a4fa2063a3db304

Author: Jean-Philippe Brucker <[email protected]>
Date:   Wed Oct 2 12:42:41 2019 -0700

iommu: Add I/O ASID allocator

Some devices might support multiple DMA address spaces, in particular those that have the PCI PASID feature. PASID (Process Address Space ID) allows to share process address spaces with devices (SVA), partition a device into VM-assignable entities (VFIO mdev) or simply provide multiple DMA address space to kernel drivers. Add a global PASID allocator usable by different drivers at the same time. Name it I/O ASID to avoid confusion with ASIDs allocated by arch code, which are usually a separate ID space.

The IOASID space is global. Each device can have its own PASID space, but by convention the IOMMU ended up having a global PASID space, so that with SVA, each mm_struct is associated to a single PASID.

The allocator is primarily used by IOMMU subsystem but in rare occasions drivers would like to allocate PASIDs for devices that aren't managed by an IOMMU, using the same ID space as IOMMU.
--

ioasid and pasid are used interchangeably within the kernel, and the ioasid value returned by the ioasid allocator is directly used as PASID when the driver programs IOMMU and device, respectively. My context is based on this understanding, which is why I thought nested_ioasid can be directly used as PASID in earlier reply. Do you see a problem with this approach?

Then following your proposal, does it mean that we need another interface for allocating PASID? and since ioasid means different thing in uAPI and in-kernel API, possibly a new name is required to avoid confusion?

Quoted 9 lines
Quoted 4 lines

Below I list different scenarios for ATTACH_IOASID in my view. Here vfio_device could be a real PCI function (RID), or a subfunction device (RID+def_ioasid).

What is RID+def_ioasid? The IOMMU does not match on IOASID's.

A subfunction device always need to use PASID, or an internal IOMMU, confused what you are trying to explain?

Here the def_ioasid is the PASID that is associated with the subfunction in my context with above explanation.

Quoted 11 lines
Quoted 3 lines

If the whole PASID table is delegated to the guest in ARM case, the guest can select its own PASIDs w/o telling the hypervisor.

The hypervisor has to route the PASID's to the guest at some point - a guest can't just claim a PASID unilaterally, that would not be secure.

If it is not done with per-PASID hypercalls then the hypervisor has to route all PASID's for a RID to the guest and /dev/ioasid needs to have a nested IOASID object that represents this connection - ie it points to the PASID table of the guest vIOMMU or something.

yes, this might be the model that will work for ARM case. In their architecture the PASID table locates in the GPA space thus naturally to be managed by the guest (though Jean ever mentioned some tricky method to allow the host managing it by stealing a GPA window).

Quoted 6 lines

Remember this all has to be compatible with mdev's too and without hypercalls to create PASIDs that will be hard: mdev sharing a RID and slicing the physical PASIDs can't support a 'send all PASIDs to the guest' model, or even a 'the guest gets to pick the PASID' option.

yes, with mdev above is inevitable. I guess ARM may need some similar extension in their SMMU as what VT-d provide for mdev usage, e.g. at least not mandating PASID table in GPA space. But before that they may still expect a way to delegate the whole PASID space per RID to the guest.

Really lots of subtle differences to be generalized...

Thanks
Kevin

#73 lore raw

Re: [PATCH V4 05/18] iommu/ioasid: Redefine IOASID set and allocation APIs

Quoted 18 lines · Kevin
Quoted 18 lines

From: Jason Gunthorpe <[email protected]>
Sent: Friday, April 23, 2021 7:50 PM

Quoted 4 lines · Kevin

Or could we still have just one /dev/ioasid but allow userspace to create multiple gpa_ioasid_id's each associated to a different iommu domain? Then the compatibility check will be done at ATTACH_IOASID instead of JOIN_IOASID_FD.

To my mind what makes sense that that /dev/ioasid presents a single IOMMU behavior that is basically the same. This may ultimately not be what we call a domain today.

We may end up with a middle object which is a group of domains that all have the same capabilities, and we define capabilities in a way that most platforms have a single group of domains.

Quoted 7 lines
Quoted 3 lines

The key capability of a group of domains is they can all share the HW page table representation, so if an IOASID instantiates a page table it can be assigned to any device on any domain in the gruop of domains.

Sorry that I didn't quite get it. If a group of domains can share the same page table then why not just attaching all devices under those domains into a single domain?

Sure, if that works. But you shouldn't have things like IOMMU_CACHE create different domains or trigger different /dev/ioasid's

Quoted 5 lines

to describe the HW page table. Ideally a new iommu domain should be created only when it's impossible to share an existing page table. Otherwise you'll get bad iotlb efficiency because each domain has its unique domain id (tagged in iotlb) then duplicated iotlb entries may exist even when a single page table is shared by those domains.

Right, fewer is better

Quoted 2 lines

Or, can you elaborate what is the targeted usage by having a group of domains which all share the same page table?

You just need to have clear rule what what requires a new /dev/ioasid FD - and if it maps to domains then great.

Quoted 8 lines

Want to hear your opinion for one open here. There is no doubt that an ioasid represents a HW page table when the table is constructed by userspace and then linked to the IOMMU through the bind/unbind API. But I'm not very sure about whether an ioasid should represent the exact pgtable or the mapping metadata when the underlying pgtable is indirectly constructed through map/unmap API. VFIO does the latter way, which is why it allows multiple incompatible domains in a single container which all share the same mapping metadata.

I think VFIO's map/unmap is way too complex and we know it has bad performance problems.

If /dev/ioasid is single HW page table only then I would focus on that implementation and leave it to userspace to span different /dev/ioasids if needed.

Quoted 4 lines

OK, now I see where the disconnection comes from. In my context ioasid is the identifier that is actually used in the wire, but seems you treat it as a sw-defined namespace purely for representing page tables. We should clear this concept first before further discussing other details. 😊

There is no general HW requirement that every IO page table be referred to by the same PASID and this API would have to support non-PASID IO page tables as well. So I'd keep the two things separated in the uAPI - even though the kernel today has a global PASID pool.

Quoted 4 lines

Then following your proposal, does it mean that we need another interface for allocating PASID? and since ioasid means different thing in uAPI and in-kernel API, possibly a new name is required to avoid confusion?

I would suggest have two ways to control the PASID

1) Over /dev/ioasid allocate a PASID for an IOASID. All future PASID based usages of the IOASID will use that global PASID

2) Over the device FD, when the IOASID is bound return the PASID that was selected. If the IOASID does not have a global PASID then the kernel is free to make something up. In this mode a single IOASID can have multiple PASIDs.

Simple things like DPDK can use #2 and potentially have better PASID limits. hypervisors will most likely have to use #1, but it depends on how their vIOMMU interface works.

I think the name IOASID is fine for the uAPI, the kernel version can be called ioasid_id or something.

(also looking at ioasid.c, why do we need such a thin and odd wrapper around xarray?)

Jason

#178 lore raw

Re: [PATCH V4 05/18] iommu/ioasid: Redefine IOASID set and allocation APIs

In reply to Alex Williamson
Quoted 55 lines · Alex Williamson
Quoted 27 lines · On Wed, 21 Apr 2021 20:03:01 -0300 Jason Gunthorpe…
Quoted 7 lines · Alex Williamson
Quoted 3 lines

I still expect that VFIO_GROUP_SET_CONTAINER will be used to connect /dev/{ioasid,vfio} to the VFIO group and all the group and device logic stays inside VFIO.

But that group and device logic is also tied to the container, where the IOMMU backend is the interchangeable thing that provides the IOMMU manipulation for that container.

I think that is an area where the discussion would need to be focused.

I don't feel very prepared to have it in details, as I haven't dug into all the group and iommu micro-operation very much.

But, it does seem like the security concept that VFIO is creating with the group also has to be present in the lower iommu layer too.

With different subsystems joining devices to the same ioasid's we still have to enforce the security propery the vfio group is creating.

Quoted 3 lines

If you're using VFIO_GROUP_SET_CONTAINER to associate a group to a /dev/ioasid, then you're really either taking that group outside of vfio or you're re-implementing group management in /dev/ioasid.

This sounds right.

Quoted 8 lines
Quoted 6 lines
Quoted 2 lines

Everything can be switched to ioasid_container all down the line. If it wasn't for PPC this looks fairly simple.

At what point is it no longer vfio? I'd venture to say that replacing the container rather than invoking a different IOMMU backend is that point.

sorry, which is no longer vfio?

I'm suggesting that if we're replacing the container/group model with an ioasid then we're effectively creating a new thing that really only retains the vfio device uapi.

Quoted 12 lines
Quoted 9 lines
Quoted 5 lines

Since getting rid of PPC looks a bit hard, we'd be stuck with accepting a /dev/ioasid and then immediately wrappering it in a vfio_container an shimming it through a vfio_iommu_ops. It is not ideal at all, but in my look around I don't see a major problem if type1 implementation is moved to live under /dev/ioasid.

But type1 is \just\ an IOMMU backend, not "/dev/vfio". Given that nobody flinched at removing NVLink support, maybe just deprecate SPAPR now and see if anyone objects ;)

Would simplify this project, but I wonder :)

Quoted 7 lines
Quoted 3 lines

In any event, it does look like today we'd expect the SPAPR stuff would be done through the normal iommu APIs, perhaps enhanced a bit, which makes me suspect an enhanced type1 can implement SPAPR.

David Gibson has argued for some time that SPAPR could be handled via a converged type1 model. We has mapped that out at one point, essentially a "type2", but neither of us had any bandwidth to pursue it.

Right. The sPAPR TCE backend is kind of an unfortunate accident of history. We absolutely could do a common interface, but no-one's had time to work on it.

Quoted 4 lines
Quoted 4 lines

I say this because the SPAPR looks quite a lot like PASID when it has APIs for allocating multiple tables and other things. I would be interested to hear someone from IBM talk about what it is doing and how it doesn't fit into today's IOMMU API.

Hm. I don't think it's really like PASID. Just like Type1, the TCE backend represents a single DMA address space which all devices in the container will see at all times. The difference is that there can be multiple (well, 2) "windows" of valid IOVAs within that address space. Each window can have a different TCE (page table) layout. For kernel drivers, a smallish translated window at IOVA 0 is used for 32-bit devices, and a large direct mapped (no page table) window is created at a high IOVA for better performance with 64-bit DMA capable devices.

With the VFIO backend we create (but don't populate) a similar smallish 32-bit window, userspace can create its own secondary window if it likes, though obvious for userspace use there will always be a page table. Userspace can choose the total size (but not address), page size and to an extent the page table format of the created window. Note that the TCE page table format is *not* the same as the POWER CPU core's page table format. Userspace can also remove the default small window and create its own.

The second wrinkle is pre-registration. That lets userspace register certain userspace VA ranges (*not* IOVA ranges) as being the only ones allowed to be mapped into the IOMMU. This is a performance optimization, because on pre-registration we also pre-account memory that will be effectively locked by DMA mappings, rather than doing it at DMA map and unmap time.

This came about because POWER guests always contain a vIOMMU. That (combined with the smallish default IOVA window) means that DMA maps and unmaps can become an important bottleneck, rather than being basically a small once-off cost when qemu maps all of guest memory into the IOMMU. That's optimized with a special interlink between KVM and VFIO that accelerates the guest-initiated maps/unmap operations. However, it's not feasible to do the accounting in that fast path, hence the need for the pre-registration.

Quoted 22 lines

[Cc David, Alexey]

Quoted 19 lines

It is very old and the iommu world has advanced tremendously lately, maybe I'm too optimisitic?

Quoted 11 lines
Quoted 7 lines

We end up with a ioasid.h that basically has the vfio_iommu_type1 code lightly recast into some 'struct iommu_container' and a set of ioasid_* function entry points that follow vfio_iommu_driver_ops_type1:
ioasid_attach_group
ioasid_detatch_group
ioasid_<something about user pages> ioasid_read/ioasid_write

Again, this looks like a vfio IOMMU backend. What are we accomplishing by replacing /dev/vfio with /dev/ioasid versus some manipulation of VFIO_SET_IOMMU accepting a /dev/ioasid fd?

The point of all of this is to make the user api for the IOMMU cross-subsystem. It is not a vfio IOMMU backend, it is moving the IOMMU abstraction from VFIO into the iommu framework and giving the iommu framework a re-usable user API.

I like the idea of a common DMA/IOMMU handling system across platforms. However in order to be efficiently usable for POWER it will need to include multiple windows, allowing the user to change those windows and something like pre-registration to amortize accounting costs for heavy vIOMMU load.

Well... possibly we can do without the pre-reg now that 32-bit DMA limited devics are less common, as are POWER8 systems. With modern devices and modern kernels a guest is likely to use a single large 64-bit secondary window mapping all guest RAM, so the vIOMMU bottleneck shouldn't be such an issue.

Quoted 168 lines

Right, but I don't see that implies it cannot work within the vfio IOMMU model. Currently when an IOMMU is set, the /dev/vfio/vfio container becomes a conduit for file ops from the container to be forwarded to the IOMMU. But that's in part because the user doesn't have another object to interact with the IOMMU. It's entirely possible that with an ioasid shim, the user would continue to interact directly with the /dev/ioasid fd for IOMMU manipulation and only use VFIO_SET_IOMMU to associate a vfio container to that ioasid.

Quoted 4 lines

My ideal outcome would be for VFIO to use only the new iommu/ioasid API and have no iommu pluggability at all. The iommu subsystem provides everything needed to VFIO, and provides it equally to VDPA and everything else.

As above, we don't necessarily need to have the vfio container be the access mechanism for the IOMMU, it can become just an means to association the container with an IOMMU. This has quite a few transitional benefits.

Quoted 7 lines

drivers/vfio/ becomes primarily about 'struct vfio_device' and everything related to its IOCTL interface.

drivers/iommu and ioasid.c become all about a pluggable IOMMU interface, including a uAPI for it.

IMHO it makes a high level sense, though it may be a pipe dream.

This is where we've dissolved all but the vfio device uapi, which suggests the group and container model were never necessary and I'm not sure exactly what that uapi looks like. We currently make use of an IOMMU api that is group aware, but that awareness extends out to the vfio uapi.

Quoted 5 lines
Quoted 5 lines
Quoted 4 lines

If we have this, and /dev/ioasid implements the legacy IOCTLs, then /dev/vfio == /dev/ioasid and we can compile out vfio_fops and related from vfio.c and tell ioasid.c to create /dev/vfio instead using the ops it owns.

Quoted 7 lines
Quoted 3 lines

Why would we want /dev/ioasid to implement legacy ioctls instead of simply implementing an interface to allow /dev/ioasid to be used as a vfio IOMMU backend?

Only to make our own migration easier. I'd imagine everyone would want to sit down and design this new clear ioasid API that can co-exist on /dev/ioasid with the legacy once.

vfio really just wants to be able to attach groups to an address space to consider them isolated, everything else about the IOMMU API could happen via a new ioasid file descriptor representing that context, ie. vfio handles the group ownership and device access, ioasid handles the actual mappings.

Quoted 15 lines
Quoted 3 lines

The pseudo code above really suggests you do want to remove /dev/vfio/vfio, but this is only one of the IOMMU backends for vfio, so I can't quite figure out if we're talking past each other.

I'm not quite sure what you mean by "one of the IOMMU backends?" You mean type1, right?

Quoted 7 lines

As I expressed in another thread, type1 has a lot of shortcomings. The mapping interface leaves userspace trying desperately to use statically mapped buffers because the map/unmap latency is too high. We have horrible issues with duplicate locked page accounting across containers. It suffers pretty hard from feature creep in various areas. A new IOMMU backend is an opportunity to redesign some of these things.

Quoted 5 lines

Sure, but also those kinds of transformational things go alot better if you can smoothly go from the old to the new and have technical co-existance in side the kernel. Having a shim that maps the old APIs to new APIs internally to Linux helps keep the implementation from becoming too bogged down with compatibility.

I'm afraid /dev/ioasid providing type1 compatibility would be just that.

Quoted 5 lines
Quoted 4 lines

The IOMMU group also abstracts isolation and visibility relative to DMA. For example, in a PCIe topology a multi-function device may not have isolation between functions, but each requester ID is visible to the IOMMU.

Quoted 8 lines

Okay, I'm glad I have this all right in my head, as I was pretty sure this was what the group was about.

My next question is why do we have three things as a FD: group, device and container (aka IOMMU interface)?

Do we have container because the /dev/vfio/vfio can hold only a single page table so we need to swap containers sometimes?

The container represents an IOMMU address space, which can be shared by multiple groups, where each group may contain one or more devices. Swapping a container would require releasing all the devices (the user cannot have access to a non-isolated device), then a group could be moved from one container to another.

Quoted 6 lines

If we start from a clean sheet and make a sketch..

/dev/ioasid is the IOMMU control interface. It can create multiple IOASIDs that have page tables and it can manipulate those page tables. Each IOASID is identified by some number.

Quoted 8 lines

struct vfio_device/vdpa_device/etc are consumers of /dev/ioasid

When a device attaches to an ioasid userspace gives VFIO/VDPA the ioasid FD and the ioasid # in the FD.

The security rule for isolation is that once a device is attached to a /dev/ioasid fd then all other devices in that security group must be attached to the same ioasid FD or left unused.

Sounds like a group... Note also that if those other devices are not isolated from the user's device, the user could manipulate "unused" devices via DMA. So even unused devices should be within the same IOMMU context... thus attaching groups to IOMMU domains.

Quoted 3 lines

Thus /dev/ioasid also becomes the unit of security and the IOMMU subsystem level becomes aware of and enforces the group security rules. Userspace does not need to "see" the group

What tools does userspace have to understand isolation of individual devices without groups?

Quoted 32 lines

In sketch it would be like
ioasid_fd = open("/dev/ioasid");
vfio_device_fd = open("/dev/vfio/device0")
vdpa_device_fd = open("/dev/vdpa/device0")
ioctl(vifo_device_fd, JOIN_IOASID_FD, ioasifd)
ioctl(vdpa_device_fd, JOIN_IOASID_FD, ioasifd)

gpa_ioasid_id = ioctl(ioasid_fd, CREATE_IOASID, ..) ioctl(ioasid_fd, SET_IOASID_PAGE_TABLES, ..)

ioctl(vfio_device, ATTACH_IOASID, gpa_ioasid_id) ioctl(vpda_device, ATTACH_IOASID, gpa_ioasid_id)

.. both VDPA and VFIO see the guest physical map and the kernel has
enough info that both could use the same IOMMU page table structure ..

// Guest viommu turns off bypass mode for the vfio device ioctl(vfio_device, DETATCH_IOASID)

// Guest viommu creates a new page table rid_ioasid_id = ioctl(ioasid_fd, CREATE_IOASID, ..) ioctl(ioasid_fd, SET_IOASID_PAGE_TABLES, ..)

// Guest viommu links the new page table to the RID ioctl(vfio_device, ATTACH_IOASID, rid_ioasid_id)

The group security concept becomes implicit and hidden from the uAPI. JOIN_IOASID_FD implicitly finds the device's group inside the kernel and requires that all members of the group be joined only to this ioasid_fd.

Quoted 5 lines

Essentially we discover the group from the device instead of the device from the group.

Where does it fall down compared to the three FD version we have today?

The group concept is explicit today because how does userspace learn about implicit dependencies between devices? For example, if the user has a conventional PCI bus with a couple devices on it, how do they understand that those devices cannot be assigned to separate userspace drivers? The group fd fills that gap. Thanks,

Alex

Signature · 4 lines
David Gibson			| I'll have my music baroque, and my code
david AT gibson.dropbear.id.au	| minimalist, thank you.  NOT _the_ _other_
				| _way_ _around_!
http://www.ozlabs.org/~dgibson
#144 lore raw

Re: [PATCH V4 05/18] iommu/ioasid: Redefine IOASID set and allocation APIs

In reply to Jason Gunthorpe
Quoted 78 lines · Jason Gunthorpe
Quoted 3 lines · Alex Williamson

Because it's fundamental to the isolation of the device? What you're proposing doesn't get around the group issue, it just makes it implicit rather than explicit in the uapi.

I'm not even sure it makes it explicit or implicit, it just takes away the FD.

There are four group IOCTLs, I see them mapping to /dev/ioasid follows:
VFIO_GROUP_GET_STATUS -
+ VFIO_GROUP_FLAGS_CONTAINER_SET is fairly redundant
+ VFIO_GROUP_FLAGS_VIABLE could be in a new sysfs under kernel/iomm_groups, or could be an IOCTL on /dev/ioasid IOASID_ALL_DEVICES_VIABLE

VFIO_GROUP_SET_CONTAINER -
+ This happens implicitly when the device joins the IOASID so it gets moved to the vfio_device FD:
ioctl(vifo_device_fd, JOIN_IOASID_FD, ioasifd)

VFIO_GROUP_UNSET_CONTAINER -
+ Also moved to the vfio_device FD, opposite of JOIN_IOASID_FD

VFIO_GROUP_GET_DEVICE_FD -
+ Replaced by opening /dev/vfio/deviceX
Learn the deviceX which will be the cdev sysfs shows as:
/sys/devices/pci0000:00/0000:00:01.0/0000:01:00.0/vfio/deviceX/dev
Open /dev/vfio/deviceX

Quoted 5 lines
Quoted 2 lines

How do we model the VFIO group security concept to something like VDPA?

Is it really a "VFIO group security concept"? We're reflecting the reality of the hardware, not all devices are fully isolated.

Well, exactly.

/dev/ioasid should understand the group concept somehow, otherwise it is incomplete and maybe even security broken.

So, how do I add groups to, say, VDPA in a way that makes sense? The only answer I come to is broadly what I outlined here - make /dev/ioasid do all the group operations, and do them when we enjoin the VDPA device to the ioasid.

Once I have solved all the groups problems with the non-VFIO users, then where does that leave VFIO? Why does VFIO need a group FD if everyone else doesn't?

Quoted 3 lines

IOMMU group. This is the reality that any userspace driver needs to play in, it doesn't magically go away because we drop the group file descriptor.

I'm not saying it does, I'm saying it makes the uAPI more regular and easier to fit into /dev/ioasid without the group FD.

Quoted 3 lines

It only makes the uapi more difficult to use correctly because userspace drivers need to go outside of the uapi to have any idea that this restriction exists.

I don't think it makes any substantive difference one way or the other.

With the group FD: the userspace has to read sysfs, find the list of devices in the group, open the group fd, create device FDs for each device using the name from sysfs.

Starting from a BDF the general pseudo code is
group_path = readlink("/sys/bus/pci/devices/BDF/iommu_group") group_name = basename(group_path)
group_fd = open("/dev/vfio/"+group_name)
device_fd = ioctl(VFIO_GROUP_GET_DEVICE_FD, BDF);

Without the group FD: the userspace has to read sysfs, find the list of devices in the group and then open the device-specific cdev (found via sysfs) and link them to a /dev/ioasid FD.

Quoted 5 lines

Starting from a BDF the general pseudo code is:
device_name = first_directory_of("/sys/bus/pci/devices/BDF/vfio/") device_fd = open("/dev/vfio/"+device_name)
ioasidfd = open("/dev/ioasid")
ioctl(device_fd, JOIN_IOASID_FD, ioasidfd)

This line is the problem.

[Historical aside: Alex's early drafts for the VFIO interface looked quite similar to this. Ben Herrenschmidt and myself persuaded him it was a bad idea, and groups were developed instead. I still think it's a bad idea, and not just for POWER]

As Alex says, if this line fails because of the group restrictions, that's not great because it's not very obvious what's gone wrong. But IMO, the success path on a multi-device group is kind of worse:
you've now made made a meaningful and visible change to the setup of devices which are not mentioned in this line *at all*. If you've changed the DMA address space of this device you've also changed it for everything else in the group - there's no getting around that.

For both those reasons, I absolutely agree with Alex that retaining the explicit group model is valuable.

Yes, it makes set up more of a pain, but it's necessary complexity to actually understand what's going on here.

Quoted 15 lines

These two routes can have identical outcomes and identical security checks.

In both cases if userspace wants a list of BDFs in the same group as the BDF it is interested in:
readdir("/sys/bus/pci/devices/BDF/iommu_group/devices")

It seems like a very small difference to me.

I still don't see how the group restriction gets surfaced to the application through the group FD. The applications I looked through just treat the group FD as a step on their way to get the device_fd.

Jason

Signature · 4 lines
David Gibson			| I'll have my music baroque, and my code
david AT gibson.dropbear.id.au	| minimalist, thank you.  NOT _the_ _other_
				| _way_ _around_!
http://www.ozlabs.org/~dgibson
#138 lore raw

Re: [PATCH V4 05/18] iommu/ioasid: Redefine IOASID set and allocation APIs

In reply to Tian, Kevin
Quoted 101 lines · Kevin
Quoted 94 lines

From: Jason Gunthorpe <[email protected]>
Sent: Friday, April 23, 2021 7:40 AM

Quoted 3 lines · Alex Williamson

Because it's fundamental to the isolation of the device? What you're proposing doesn't get around the group issue, it just makes it implicit rather than explicit in the uapi.

I'm not even sure it makes it explicit or implicit, it just takes away the FD.

There are four group IOCTLs, I see them mapping to /dev/ioasid follows:
VFIO_GROUP_GET_STATUS -
+ VFIO_GROUP_FLAGS_CONTAINER_SET is fairly redundant
+ VFIO_GROUP_FLAGS_VIABLE could be in a new sysfs under kernel/iomm_groups, or could be an IOCTL on /dev/ioasid IOASID_ALL_DEVICES_VIABLE

VFIO_GROUP_SET_CONTAINER -
+ This happens implicitly when the device joins the IOASID so it gets moved to the vfio_device FD:
ioctl(vifo_device_fd, JOIN_IOASID_FD, ioasifd)

VFIO_GROUP_UNSET_CONTAINER -
+ Also moved to the vfio_device FD, opposite of JOIN_IOASID_FD

VFIO_GROUP_GET_DEVICE_FD -
+ Replaced by opening /dev/vfio/deviceX
Learn the deviceX which will be the cdev sysfs shows as:
/sys/devices/pci0000:00/0000:00:01.0/0000:01:00.0/vfio/deviceX/dev
Open /dev/vfio/deviceX

Quoted 5 lines
Quoted 2 lines

How do we model the VFIO group security concept to something like VDPA?

Is it really a "VFIO group security concept"? We're reflecting the reality of the hardware, not all devices are fully isolated.

Well, exactly.

/dev/ioasid should understand the group concept somehow, otherwise it is incomplete and maybe even security broken.

So, how do I add groups to, say, VDPA in a way that makes sense? The only answer I come to is broadly what I outlined here - make /dev/ioasid do all the group operations, and do them when we enjoin the VDPA device to the ioasid.

Once I have solved all the groups problems with the non-VFIO users, then where does that leave VFIO? Why does VFIO need a group FD if everyone else doesn't?

Quoted 3 lines

IOMMU group. This is the reality that any userspace driver needs to play in, it doesn't magically go away because we drop the group file descriptor.

I'm not saying it does, I'm saying it makes the uAPI more regular and easier to fit into /dev/ioasid without the group FD.

Quoted 3 lines

It only makes the uapi more difficult to use correctly because userspace drivers need to go outside of the uapi to have any idea that this restriction exists.

I don't think it makes any substantive difference one way or the other.

With the group FD: the userspace has to read sysfs, find the list of devices in the group, open the group fd, create device FDs for each device using the name from sysfs.

Starting from a BDF the general pseudo code is
group_path = readlink("/sys/bus/pci/devices/BDF/iommu_group") group_name = basename(group_path)
group_fd = open("/dev/vfio/"+group_name)
device_fd = ioctl(VFIO_GROUP_GET_DEVICE_FD, BDF);

Without the group FD: the userspace has to read sysfs, find the list of devices in the group and then open the device-specific cdev (found via sysfs) and link them to a /dev/ioasid FD.

Starting from a BDF the general pseudo code is:
device_name = first_directory_of("/sys/bus/pci/devices/BDF/vfio/") device_fd = open("/dev/vfio/"+device_name)
ioasidfd = open("/dev/ioasid")
ioctl(device_fd, JOIN_IOASID_FD, ioasidfd)

These two routes can have identical outcomes and identical security checks.

In both cases if userspace wants a list of BDFs in the same group as the BDF it is interested in:
readdir("/sys/bus/pci/devices/BDF/iommu_group/devices")

Quoted 6 lines

It seems like a very small difference to me.

I still don't see how the group restriction gets surfaced to the application through the group FD. The applications I looked through just treat the group FD as a step on their way to get the device_fd.

Quoted 4 lines

So your proposal sort of moves the entire container/group/domain managment into /dev/ioasid and then leaves vfio only provide device specific uAPI. An ioasid represents a page table (address space), thus is equivalent to the scope of VFIO container.

Right. I don't really know how /dev/iosasid is supposed to work, and so far I don't see how it conceptually differs from a container. What is it adding?

Quoted 27 lines

Having the device join
an ioasid is equivalent to attaching a device to VFIO container, and here the group integrity must be enforced. Then /dev/ioasid anyway needs to manage group objects and their association with ioasid and underlying iommu domain thus it's pointless to keep same logic within VFIO. Is this understanding correct?

btw one remaining open is whether you expect /dev/ioasid to be associated with a single iommu domain, or multiple. If only a single domain is allowed, the ioasid_fd is equivalent to the scope of VFIO container. It is supposed to have only one gpa_ioasid_id since one iommu domain can only have a single 2nd level pgtable. Then all other ioasids, once allocated, must be nested on this gpa_ioasid_id to fit in the same domain. if a legacy vIOMMU is exposed (which disallows nesting), the userspace has to open an ioasid_fd for every group. This is basically the VFIO way. On the other hand if multiple domains is allowed, there could be multiple ioasid_ids each holding a 2nd level pgtable and an iommu domain (or a list of pgtables and domains due to incompatibility issue as discussed in another thread), and can be nested by other ioasids respectively. The application only needs to open /dev/ioasid once regardless of whether vIOMMU allows nesting, and has a single interface for ioasid allocation. Which way do you prefer to?

Thanks
Kevin

Signature · 4 lines
David Gibson			| I'll have my music baroque, and my code
david AT gibson.dropbear.id.au	| minimalist, thank you.  NOT _the_ _other_
				| _way_ _around_!
http://www.ozlabs.org/~dgibson