Skip to contentSkip to conversation

Introduce /dev/iommu for userspace I/O address space management

RFC In review Liu Yi L 12 participants 279 messages Sep 19, 2021 – Oct 29, 2021 2 messages not in the archive Page 1 of 2

Subsystems (10)
Documentation KVM PCI Driver core +6 more subsystems
lore
Share & tools
lore

Liu Yi L

author
#0 lore raw

cover Introduce /dev/iommu for userspace I/O address space management

Linux now includes multiple device-passthrough frameworks (e.g. VFIO and vDPA) to manage secure device access from the userspace. One critical task of those frameworks is to put the assigned device in a secure, IOMMU- protected context so user-initiated DMAs are prevented from doing harm to the rest of the system.

Currently those frameworks implement their own logic for managing I/O page tables to isolate user-initiated DMAs. This doesn't scale to support many new IOMMU features, such as PASID-granular DMA remapping, nested translation, I/O page fault, IOMMU dirty bit, etc.

/dev/iommu is introduced as an unified interface for managing I/O address spaces and DMA isolation for passthrough devices. It's originated from the upstream discussion for the vSVA enabling work[1].

This RFC aims to provide a basic skeleton for above proposal, w/o adding any new feature beyond what vfio type1 provides today. For an overview of future extensions, please refer to the full design proposal [2].

The core concepts in /dev/iommu are iommufd and ioasid. iommufd (by opening /dev/iommu) is the container holding multiple I/O address spaces, while ioasid is the fd-local software handle representing an I/O address space and associated with a single I/O page table. User manages those address spaces through fd operations, e.g. by using vfio type1v2 mapping semantics to manage respective I/O page tables.

An I/O address space takes effect in the iommu only after it is attached by a device. One I/O address space can be attached by multiple devices. One device can be only attached to a single I/O address space in this RFC, to match vfio type1 behavior as the starting point.

Device must be bound to an iommufd before attach operation can be conducted. The binding operation builds the connection between the devicefd (opened via device-passthrough framework) and iommufd. Most importantly, the entire /dev/iommu framework adopts a device-centric model w/o carrying any container/ group legacy as current vfio does. This requires the binding operation also establishes a security context which prevents the bound device from accessing the rest of the system, as the contract for vfio to grant user access to the assigned device. Detail explanation of this aspect can be found in patch 06.

Last, the format of an I/O page table must be compatible to the attached devices (or more specifically to the IOMMU which serves the DMA from the attached devices). User is responsible for specifying the format when allocating an IOASID, according to one or multiple devices which will be attached right after. The device IOMMU format can be queried via iommufd once a device is successfully bound to the iommufd. Attaching a device to an IOASID with incompatible format is simply rejected.

The skeleton is mostly implemented in iommufd, except that bind_iommufd/ ioasid_attach operations are initiated via device-passthrough framework specific uAPIs. This RFC only changes vfio to work with iommufd. vdpa support can be added in a later stage.

Basically iommufd provides following uAPIs and helper functions:

- IOMMU_DEVICE_GET_INFO, for querying per-device iommu capability/format;
- IOMMU_IOASID_ALLOC/FREE, as the name stands;
- IOMMU_[UN]MAP_DMA, providing vfio type1v2 semantics for managing a specific I/O page table;
- helper functions for vfio to bind_iommufd/attach_ioasid with devices;

vfio extensions include:
- A new interface for user to open a device w/o using container/group uAPI;
- VFIO_DEVICE_BIND_IOMMUFD, for binding a vfio device to an iommufd;
* unbind is automatically done when devicefd is closed;
- VFIO_DEVICE_[DE]ATTACH_IOASID, for attaching/detaching a vfio device to/from an ioasid in the specified iommufd;

[TODO in RFC v2]

We did one temporary hack in v1 by reusing vfio_iommu_type1.c to implement IOMMU_[UN]MAP_DMA. This leads to some dirty code in patch 16/17/18. We estimated almost 80% of the current type1 code are related to map/unmap. It needs non-trivial effort for either duplicating it in iommufd or making it shared by both vfio and iommufd. We hope this hack doesn't affect the review of the overall skeleton, since the role of this part is very clear. Based on the received feedback we will make a clean implementation in v2.

For userspace our time doesn't afford a clean implementation in Qemu. Instead, we just wrote a simple application (similar to the example in iommufd.rst) and verified the basic work flow (bind/unbind, alloc/free ioasid, attach/detach, map/unmap, multi-devices group, etc.). We did verify the I/O page table mappings established as expected, though no DMA is conducted. We plan to have a clean implementation in Qemu and provide a public link for reference when v2 is sending out.

[TODO out of this RFC]

The entire /dev/iommu project involves lots of tasks. It has to grow in a staging approach. Below is a rough list of TODO features. Most of them can be developed in parallel after this skeleton is accepted. For more detail please refer to the design proposal [2]:

1. Move more vfio device types to iommufd:
* device which does no-snoop DMA
* software mdev
* PPC device
* platform device

2. New vfio device type
* hardware mdev/subdev (with PASID)

3. vDPA adoption

4. User-managed I/O page table
* ioasid nesting (hardware)
* ioasid nesting (software)
* pasid virtualization
o pdev (arm/amd)
o pdev/mdev which doesn't support enqcmd (intel)
o pdev/mdev which supports enqcmd (intel)
* I/O page fault (stage-1)

5. Miscellaneous
* I/O page fault (stage-2), for on-demand paging
* IOMMU dirty bit, for hardware-assisted dirty page tracking
* shared I/O page table (mm, ept, etc.)
* vfio/vdpa shim to avoid code duplication for legacy uAPI
* hardware-assisted vIOMMU

[1] https://lore.kernel.org/linux-iommu/[email protected]/
[2] https://lore.kernel.org/kvm/BN9PR11MB5433B1E4AE5B0480369F97178C189@BN9PR11MB5433.namprd11.prod.outlook.com/

[Series Overview]
* Basic skeleton:
0001-iommu-iommufd-Add-dev-iommu-core.patch

* VFIO PCI creates device-centric interface:
0002-vfio-Add-vfio-device-class-for-device-nodes.patch 0003-vfio-Add-vfio_-un-register_device.patch 0004-iommu-Add-iommu_device_get_info-interface.patch 0005-vfio-pci-Register-device-centric-interface.patch

* Bind device fd with iommufd:
0006-iommu-Add-iommu_device_init-exit-_user_dma-interface.patch 0007-iommu-iommufd-Add-iommufd_-un-bind_device.patch 0008-vfio-pci-Add-VFIO_DEVICE_BIND_IOMMUFD.patch

* IOASID allocation:
0009-iommu-iommufd-Add-IOMMU_DEVICE_GET_INFO.patch 0010-iommu-iommufd-Add-IOMMU_IOASID_ALLOC-FREE.patch

* IOASID [de]attach:
0011-iommu-Extend-iommu_at-de-tach_device-for-multiple-de.patch 0012-iommu-iommufd-Add-iommufd_device_-de-attach_ioasid.patch 0013-vfio-pci-Add-VFIO_DEVICE_-DE-ATTACH_IOASID.patch

* /dev/iommu DMA (un)map:
0014-vfio-type1-Export-symbols-for-dma-un-map-code-sharin.patch 0015-iommu-iommufd-Report-iova-range-to-userspace.patch 0016-iommu-iommufd-Add-IOMMU_-UN-MAP_DMA-on-IOASID.patch

* Report the device info:
0017-iommu-vt-d-Implement-device_info-iommu_ops-callback.patch

* Add doc:
0018-Doc-Add-documentation-for-dev-iommu.patch

* Basic skeleton:
0001-iommu-iommufd-Add-dev-iommu-core.patch

* VFIO PCI creates device-centric interface:
0002-vfio-Add-device-class-for-dev-vfio-devices.patch 0003-vfio-Add-vfio_-un-register_device.patch 0004-iommu-Add-iommu_device_get_info-interface.patch 0005-vfio-pci-Register-device-to-dev-vfio-devices.patch

* Bind device fd with iommufd:
0006-iommu-Add-iommu_device_init-exit-_user_dma-interface.patch 0007-iommu-iommufd-Add-iommufd_-un-bind_device.patch 0008-vfio-pci-Add-VFIO_DEVICE_BIND_IOMMUFD.patch

* IOASID allocation:
0009-iommu-Add-page-size-and-address-width-attributes.patch 0010-iommu-iommufd-Add-IOMMU_DEVICE_GET_INFO.patch 0011-iommu-iommufd-Add-IOMMU_IOASID_ALLOC-FREE.patch 0012-iommu-iommufd-Add-IOMMU_CHECK_EXTENSION.patch

* IOASID [de]attach:
0013-iommu-Extend-iommu_at-de-tach_device-for-multiple-de.patch 0014-iommu-iommufd-Add-iommufd_device_-de-attach_ioasid.patch 0015-vfio-pci-Add-VFIO_DEVICE_-DE-ATTACH_IOASID.patch

* DMA (un)map:
0016-vfio-type1-Export-symbols-for-dma-un-map-code-sharin.patch 0017-iommu-iommufd-Report-iova-range-to-userspace.patch 0018-iommu-iommufd-Add-IOMMU_-UN-MAP_DMA-on-IOASID.patch

* Report the device info in vt-d driver to enable whole series:
0019-iommu-vt-d-Implement-device_info-iommu_ops-callback.patch

* Add doc:
0020-Doc-Add-documentation-for-dev-iommu.patch

Complete code can be found in:
https://github.com/luxis1999/dev-iommu/commits/dev-iommu-5.14-rfcv1

Thanks for your time!

Regards,
Yi Liu
---

Liu Yi L (15):
iommu/iommufd: Add /dev/iommu core
vfio: Add device class for /dev/vfio/devices
vfio: Add vfio_[un]register_device()
vfio/pci: Register device to /dev/vfio/devices iommu/iommufd: Add iommufd_[un]bind_device() vfio/pci: Add VFIO_DEVICE_BIND_IOMMUFD iommu/iommufd: Add IOMMU_DEVICE_GET_INFO iommu/iommufd: Add IOMMU_IOASID_ALLOC/FREE iommu/iommufd: Add IOMMU_CHECK_EXTENSION iommu/iommufd: Add iommufd_device_[de]attach_ioasid() vfio/pci: Add VFIO_DEVICE_[DE]ATTACH_IOASID vfio/type1: Export symbols for dma [un]map code sharing iommu/iommufd: Report iova range to userspace iommu/iommufd: Add IOMMU_[UN]MAP_DMA on IOASID
Doc: Add documentation for /dev/iommu

Lu Baolu (5):
iommu: Add iommu_device_get_info interface
iommu: Add iommu_device_init[exit]_user_dma interfaces
iommu: Add page size and address width attributes
iommu: Extend iommu_at[de]tach_device() for multiple devices group iommu/vt-d: Implement device_info iommu_ops callback

 Documentation/userspace-api/index.rst   |   1 +
 Documentation/userspace-api/iommufd.rst | 183 ++++++
 drivers/iommu/Kconfig                   |   1 +
 drivers/iommu/Makefile                  |   1 +
 drivers/iommu/intel/iommu.c             |  35 +
 drivers/iommu/iommu.c                   | 188 +++++-
 drivers/iommu/iommufd/Kconfig           |  11 +
 drivers/iommu/iommufd/Makefile          |   2 +
 drivers/iommu/iommufd/iommufd.c         | 832 ++++++++++++++++++++++++
 drivers/vfio/pci/Kconfig                |   1 +
 drivers/vfio/pci/vfio_pci.c             | 179 ++++-
 drivers/vfio/pci/vfio_pci_private.h     |  10 +
 drivers/vfio/vfio.c                     | 366 ++++++++++-
 drivers/vfio/vfio_iommu_type1.c         | 246 ++++++-
 include/linux/iommu.h                   |  35 +
 include/linux/iommufd.h                 |  71 ++
 include/linux/vfio.h                    |  27 +
 include/uapi/linux/iommu.h              | 162 +++++
 include/uapi/linux/vfio.h               |  56 ++
 19 files changed, 2358 insertions(+), 49 deletions(-)
 create mode 100644 Documentation/userspace-api/iommufd.rst
 create mode 100644 drivers/iommu/iommufd/Kconfig
 create mode 100644 drivers/iommu/iommufd/Makefile
 create mode 100644 drivers/iommu/iommufd/iommufd.c
 create mode 100644 include/linux/iommufd.h
Signature · 6 lines
2.25.1

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

RFC 1/20 iommu/iommufd: Add /dev/iommu core

/dev/iommu aims to provide a unified interface for managing I/O address spaces for devices assigned to userspace. This patch adds the initial framework to create a /dev/iommu node. Each open of this node returns an iommufd. And this fd is the handle for userspace to initiate its I/O address space management.

One open:
- We call this feature as IOMMUFD in Kconfig in this RFC. However this name is not clear enough to indicate its purpose to user. Back to 2010 vfio even introduced a /dev/uiommu [1] as the predecessor of its container concept. Is that a better name? Appreciate opinions here.

[1] https://lore.kernel.org/kvm/4c0eb470.1HMjondO00NIvFM6%[email protected]/

---
 drivers/iommu/Kconfig           |   1 +
 drivers/iommu/Makefile          |   1 +
 drivers/iommu/iommufd/Kconfig   |  11 ++++
 drivers/iommu/iommufd/Makefile  |   2 +
 drivers/iommu/iommufd/iommufd.c | 112 ++++++++++++++++++++++++++++++++
 5 files changed, 127 insertions(+)
 create mode 100644 drivers/iommu/iommufd/Kconfig
 create mode 100644 drivers/iommu/iommufd/Makefile
 create mode 100644 drivers/iommu/iommufd/iommufd.c
drivers/iommu/Kconfig +1 -0
index 07b7c25cbed8..a83ce0acd09d 100644
@@ -136,6 +136,7 @@ config MSM_IOMMU
 
 source "drivers/iommu/amd/Kconfig"
 source "drivers/iommu/intel/Kconfig"
+source "drivers/iommu/iommufd/Kconfig"
 
 config IRQ_REMAP
 	bool "Support for Interrupt Remapping"
drivers/iommu/Makefile +1 -0
index c0fb0ba88143..719c799f23ad 100644
@@ -29,3 +29,4 @@ obj-$(CONFIG_HYPERV_IOMMU) += hyperv-iommu.o
 obj-$(CONFIG_VIRTIO_IOMMU) += virtio-iommu.o
 obj-$(CONFIG_IOMMU_SVA_LIB) += iommu-sva-lib.o io-pgfault.o
 obj-$(CONFIG_SPRD_IOMMU) += sprd-iommu.o
+obj-$(CONFIG_IOMMUFD) += iommufd/
drivers/iommu/iommufd/Kconfig new file +11 -0
new file mode 100644
index 000000000000..9fb7769a815d
@@ -0,0 +1,11 @@
+# SPDX-License-Identifier: GPL-2.0-only
+config IOMMUFD
+	tristate "I/O Address Space management framework for passthrough devices"
+	select IOMMU_API
+	default n
+	help
+	  provides unified I/O address space management framework for
+	  isolating untrusted DMAs via devices which are passed through
+	  to userspace drivers.
+
+	  If you don't know what to do here, say N.
drivers/iommu/iommufd/Makefile new file +2 -0
new file mode 100644
index 000000000000..54381a01d003
@@ -0,0 +1,2 @@
+# SPDX-License-Identifier: GPL-2.0-only
+obj-$(CONFIG_IOMMUFD) += iommufd.o
drivers/iommu/iommufd/iommufd.c new file +112 -0

112 lines not on this page · raw message

Signature · 1 line
2.25.1
maintainer
#2 lore raw

Re: [RFC 01/20] iommu/iommufd: Add /dev/iommu core

Quoted 85 lines · Liu Yi L

/dev/iommu aims to provide a unified interface for managing I/O address spaces for devices assigned to userspace. This patch adds the initial framework to create a /dev/iommu node. Each open of this node returns an iommufd. And this fd is the handle for userspace to initiate its I/O address space management.

One open:
- We call this feature as IOMMUFD in Kconfig in this RFC. However this name is not clear enough to indicate its purpose to user. Back to 2010 vfio even introduced a /dev/uiommu [1] as the predecessor of its container concept. Is that a better name? Appreciate opinions here.

[1] https://lore.kernel.org/kvm/4c0eb470.1HMjondO00NIvFM6%[email protected]/

 drivers/iommu/Kconfig           |   1 +
 drivers/iommu/Makefile          |   1 +
 drivers/iommu/iommufd/Kconfig   |  11 ++++
 drivers/iommu/iommufd/Makefile  |   2 +
 drivers/iommu/iommufd/iommufd.c | 112 ++++++++++++++++++++++++++++++++
 5 files changed, 127 insertions(+)
 create mode 100644 drivers/iommu/iommufd/Kconfig
 create mode 100644 drivers/iommu/iommufd/Makefile
 create mode 100644 drivers/iommu/iommufd/iommufd.c
drivers/iommu/Kconfig +2 -0
index 07b7c25cbed8..a83ce0acd09d 100644
+++ b/drivers/iommu/Kconfig
@@ -136,6 +136,7 @@ config MSM_IOMMU
 
 source "drivers/iommu/amd/Kconfig"
 source "drivers/iommu/intel/Kconfig"
+source "drivers/iommu/iommufd/Kconfig"
 
 config IRQ_REMAP
 	bool "Support for Interrupt Remapping"
drivers/iommu/Makefile +2 -0
index c0fb0ba88143..719c799f23ad 100644
+++ b/drivers/iommu/Makefile
@@ -29,3 +29,4 @@ obj-$(CONFIG_HYPERV_IOMMU) += hyperv-iommu.o
 obj-$(CONFIG_VIRTIO_IOMMU) += virtio-iommu.o
 obj-$(CONFIG_IOMMU_SVA_LIB) += iommu-sva-lib.o io-pgfault.o
 obj-$(CONFIG_SPRD_IOMMU) += sprd-iommu.o
+obj-$(CONFIG_IOMMUFD) += iommufd/
drivers/iommu/iommufd/Kconfig new file +12 -0
new file mode 100644
index 000000000000..9fb7769a815d
+++ b/drivers/iommu/iommufd/Kconfig
@@ -0,0 +1,11 @@
+# SPDX-License-Identifier: GPL-2.0-only
+config IOMMUFD
+	tristate "I/O Address Space management framework for passthrough devices"
+	select IOMMU_API
+	default n
+	help
+	  provides unified I/O address space management framework for
+	  isolating untrusted DMAs via devices which are passed through
+	  to userspace drivers.
+
+	  If you don't know what to do here, say N.
drivers/iommu/iommufd/Makefile new file +3 -0
new file mode 100644
index 000000000000..54381a01d003
+++ b/drivers/iommu/iommufd/Makefile
@@ -0,0 +1,2 @@
+# SPDX-License-Identifier: GPL-2.0-only
+obj-$(CONFIG_IOMMUFD) += iommufd.o
drivers/iommu/iommufd/iommufd.c new file +14 -0
new file mode 100644
index 000000000000..710b7e62988b
+++ b/drivers/iommu/iommufd/iommufd.c
@@ -0,0 +1,112 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/*
+ * I/O Address Space Management for passthrough devices
+ *
+ * Copyright (C) 2021 Intel Corporation
+ *
+ * Author: Liu Yi L <[email protected]>
+ */
+
+#define pr_fmt(fmt)    "iommufd: " fmt
+
+#include <linux/file.h>
+#include <linux/fs.h>

drivers/iommu/iommufd/iommufd.c line 23

Quoted 10 lines
drivers/iommu/iommufd/iommufd.c new file +10 -0
+#include <linux/module.h>
+#include <linux/slab.h>
+#include <linux/miscdevice.h>
+#include <linux/mutex.h>
+#include <linux/iommu.h>
+
+/* Per iommufd */
+struct iommufd_ctx {
+	refcount_t refs;
+};

A private_data of a struct file should avoid having a refcount (and this should have been a kref anyhow)

Use the refcount on the struct file instead.

In general the lifetime models look overly convoluted to me with refcounts being used as locks and going in all manner of directions.

- No refcount on iommufd_ctx, this should use the fget on the fd. The driver facing version of the API has the driver holds a fget inside the iommufd_device.

- Put a rwlock inside the iommufd_ioas that is a 'destroying_lock'. The rwlock starts out unlocked.

Acquire from the xarray is
rcu_lock()
ioas = xa_load()
if (ioas)

      if (down_read_trylock(&ioas->destroying_lock))
           // success

Unacquire is just up_read()

Do down_write when the ioas is to be destroyed, do not return ebusy.

- Delete the iommufd_ctx->lock. Use RCU to protect load, erase/alloc does not need locking (order it properly too, it is in the wrong order), and don't check for duplicate devices or dev_cookie duplication, that is user error and is harmless to the kernel.

drivers/iommu/iommufd/iommufd.c line 50

Quoted 5 lines
drivers/iommu/iommufd/iommufd.c new file +5 -0
+static int iommufd_fops_release(struct inode *inode, struct file *filep)
+{
+	struct iommufd_ctx *ictx = filep->private_data;
+
+	filep->private_data = NULL;

unnecessary

drivers/iommu/iommufd/iommufd.c line 64

Quoted 13 lines
drivers/iommu/iommufd/iommufd.c new file +13 -0
+	iommufd_ctx_put(ictx);
+
+	return 0;
+}
+
+static long iommufd_fops_unl_ioctl(struct file *filep,
+				   unsigned int cmd, unsigned long arg)
+{
+	struct iommufd_ctx *ictx = filep->private_data;
+	long ret = -EINVAL;
+
+	if (!ictx)
+		return ret;

impossible

drivers/iommu/iommufd/iommufd.c line 68

Quoted 4 lines
drivers/iommu/iommufd/iommufd.c new file +4 -0
+
+	switch (cmd) {
+	default:
+		pr_err_ratelimited("unsupported cmd %u\n", cmd);

don't log user triggerable events

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

maintainer
#3 lore raw

RE: [RFC 01/20] iommu/iommufd: Add /dev/iommu core

Quoted 8 lines

From: Jason Gunthorpe <[email protected]>
Sent: Tuesday, September 21, 2021 11:42 PM

- Delete the iommufd_ctx->lock. Use RCU to protect load, erase/alloc does not need locking (order it properly too, it is in the wrong order), and don't check for duplicate devices or dev_cookie duplication, that is user error and is harmless to the kernel.

I'm confused here. yes it's user error, but we check so many user errors and then return -EINVAL, -EBUSY, etc. Why is this one special?

maintainer
#4 lore raw

Re: [RFC 01/20] iommu/iommufd: Add /dev/iommu core

Quoted 11 lines · Kevin
Quoted 8 lines

From: Jason Gunthorpe <[email protected]>
Sent: Tuesday, September 21, 2021 11:42 PM

- Delete the iommufd_ctx->lock. Use RCU to protect load, erase/alloc does not need locking (order it properly too, it is in the wrong order), and don't check for duplicate devices or dev_cookie duplication, that is user error and is harmless to the kernel.

I'm confused here. yes it's user error, but we check so many user errors and then return -EINVAL, -EBUSY, etc. Why is this one special?

Because it is expensive to calculate and forces a complicated locking scheme into the kernel. Without this check you don't need the locking that spans so much code, and simple RCU becomes acceptable.

Jason

maintainer
#5 lore raw

RE: [RFC 01/20] iommu/iommufd: Add /dev/iommu core

Quoted 14 lines

From: Jason Gunthorpe <[email protected]>
Sent: Wednesday, September 22, 2021 8:41 PM

Quoted 4 lines · Kevin
Quoted 4 lines

From: Jason Gunthorpe <[email protected]>
Sent: Tuesday, September 21, 2021 11:42 PM

- Delete the iommufd_ctx->lock. Use RCU to protect load, erase/alloc

does

Quoted 5 lines
Quoted 4 lines

not need locking (order it properly too, it is in the wrong order), and don't check for duplicate devices or dev_cookie duplication, that is user error and is harmless to the kernel.

Quoted 7 lines
Quoted 2 lines

I'm confused here. yes it's user error, but we check so many user errors and then return -EINVAL, -EBUSY, etc. Why is this one special?

Because it is expensive to calculate and forces a complicated locking scheme into the kernel. Without this check you don't need the locking that spans so much code, and simple RCU becomes acceptable.

In case of duplication the kernel just uses the first entry which matches the device when sending an event to userspace?
_______________________________________________
iommu mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/iommu

maintainer
#6 lore raw

Re: [RFC 01/20] iommu/iommufd: Add /dev/iommu core

Quoted 17 lines · Kevin
Quoted 17 lines

From: Jason Gunthorpe <[email protected]>
Sent: Wednesday, September 22, 2021 8:41 PM

Quoted 4 lines · Kevin
Quoted 4 lines

From: Jason Gunthorpe <[email protected]>
Sent: Tuesday, September 21, 2021 11:42 PM

- Delete the iommufd_ctx->lock. Use RCU to protect load, erase/alloc

does

Quoted 7 lines
Quoted 4 lines

not need locking (order it properly too, it is in the wrong order), and don't check for duplicate devices or dev_cookie duplication, that is user error and is harmless to the kernel.

I'm confused here. yes it's user error, but we check so many user errors and then return -EINVAL, -EBUSY, etc. Why is this one special?

Quoted 6 lines
Quoted 3 lines

Because it is expensive to calculate and forces a complicated locking scheme into the kernel. Without this check you don't need the locking that spans so much code, and simple RCU becomes acceptable.

In case of duplication the kernel just uses the first entry which matches the device when sending an event to userspace?

Sure

Jason

author
#7 lore raw

RE: [RFC 01/20] iommu/iommufd: Add /dev/iommu core

In reply to Jason Gunthorpe
Quoted 119 lines

From: Jason Gunthorpe <[email protected]>
Sent: Tuesday, September 21, 2021 11:42 PM

Quoted 6 lines · Liu Yi L

/dev/iommu aims to provide a unified interface for managing I/O address spaces for devices assigned to userspace. This patch adds the initial framework to create a /dev/iommu node. Each open of this node returns an iommufd. And this fd is the handle for userspace to initiate its I/O address space management.

Quoted 7 lines

One open:
- We call this feature as IOMMUFD in Kconfig in this RFC. However this name is not clear enough to indicate its purpose to user. Back to 2010 vfio even introduced a /dev/uiommu [1] as the predecessor of its container concept. Is that a better name? Appreciate opinions here.

[1]

https://lore.kernel.org/kvm/4c0eb470.1HMjondO00NIvFM6%[email protected]
m/

Quoted 7 lines
 drivers/iommu/Kconfig           |   1 +
 drivers/iommu/Makefile          |   1 +
 drivers/iommu/iommufd/Kconfig   |  11 ++++
 drivers/iommu/iommufd/Makefile  |   2 +
 drivers/iommu/iommufd/iommufd.c | 112

++++++++++++++++++++++++++++++++

Review

Quoted 25 lines
 5 files changed, 127 insertions(+)
 create mode 100644 drivers/iommu/iommufd/Kconfig
 create mode 100644 drivers/iommu/iommufd/Makefile
 create mode 100644 drivers/iommu/iommufd/iommufd.c
drivers/iommu/Kconfig +2 -0
index 07b7c25cbed8..a83ce0acd09d 100644
+++ b/drivers/iommu/Kconfig
@@ -136,6 +136,7 @@ config MSM_IOMMU
 
 source "drivers/iommu/amd/Kconfig"
 source "drivers/iommu/intel/Kconfig"
+source "drivers/iommu/iommufd/Kconfig"
 
 config IRQ_REMAP
 	bool "Support for Interrupt Remapping"
drivers/iommu/Makefile +2 -0
index c0fb0ba88143..719c799f23ad 100644
+++ b/drivers/iommu/Makefile
@@ -29,3 +29,4 @@ obj-$(CONFIG_HYPERV_IOMMU) += hyperv-iommu.o
 obj-$(CONFIG_VIRTIO_IOMMU) += virtio-iommu.o
 obj-$(CONFIG_IOMMU_SVA_LIB) += iommu-sva-lib.o io-pgfault.o
 obj-$(CONFIG_SPRD_IOMMU) += sprd-iommu.o
+obj-$(CONFIG_IOMMUFD) += iommufd/
(quoted diff) +0 -0

b/drivers/iommu/iommufd/Kconfig

drivers/iommu/iommufd/Kconfig line 3

Quoted 7 lines

new file mode 100644
index 000000000000..9fb7769a815d
+++ b/drivers/iommu/iommufd/Kconfig

drivers/iommu/iommufd/Kconfig new file +3 -0
@@ -0,0 +1,11 @@
+# SPDX-License-Identifier: GPL-2.0-only
+config IOMMUFD
+	tristate "I/O Address Space management framework for passthrough

devices"

Review

Quoted 9 lines
drivers/iommu/iommufd/Kconfig new file +8 -0
+	select IOMMU_API
+	default n
+	help
+	  provides unified I/O address space management framework for
+	  isolating untrusted DMAs via devices which are passed through
+	  to userspace drivers.
+
+	  If you don't know what to do here, say N.
(quoted diff) +0 -0

b/drivers/iommu/iommufd/Makefile

Review

Quoted 7 lines

new file mode 100644
index 000000000000..54381a01d003
+++ b/drivers/iommu/iommufd/Makefile

drivers/iommu/iommufd/Makefile new file +2 -0
@@ -0,0 +1,2 @@
+# SPDX-License-Identifier: GPL-2.0-only
+obj-$(CONFIG_IOMMUFD) += iommufd.o
(quoted diff) +0 -0

b/drivers/iommu/iommufd/iommufd.c

Quoted 17 lines

new file mode 100644
index 000000000000..710b7e62988b
+++ b/drivers/iommu/iommufd/iommufd.c

drivers/iommu/iommufd/iommufd.c new file +13 -0
@@ -0,0 +1,112 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/*
+ * I/O Address Space Management for passthrough devices
+ *
+ * Copyright (C) 2021 Intel Corporation
+ *
+ * Author: Liu Yi L <[email protected]>
+ */
+
+#define pr_fmt(fmt)    "iommufd: " fmt
+
+#include <linux/file.h>
+#include <linux/fs.h>

drivers/iommu/iommufd/iommufd.c line 23

Quoted 10 lines
drivers/iommu/iommufd/iommufd.c new file +10 -0
+#include <linux/module.h>
+#include <linux/slab.h>
+#include <linux/miscdevice.h>
+#include <linux/mutex.h>
+#include <linux/iommu.h>
+
+/* Per iommufd */
+struct iommufd_ctx {
+	refcount_t refs;
+};

A private_data of a struct file should avoid having a refcount (and this should have been a kref anyhow)

Use the refcount on the struct file instead.

In general the lifetime models look overly convoluted to me with refcounts being used as locks and going in all manner of directions.

- No refcount on iommufd_ctx, this should use the fget on the fd. The driver facing version of the API has the driver holds a fget inside the iommufd_device.

Quoted 8 lines

- Put a rwlock inside the iommufd_ioas that is a 'destroying_lock'. The rwlock starts out unlocked.

Acquire from the xarray is
rcu_lock()
ioas = xa_load()
if (ioas)

      if (down_read_trylock(&ioas->destroying_lock))

all good suggestions, will refine accordingly. Here destroying_lock is a rw_semaphore. right? Since down_read_trylock() accepts a rwsem.

Thanks,
Yi Liu

Quoted 34 lines
           // success

Unacquire is just up_read()

Do down_write when the ioas is to be destroyed, do not return ebusy.

- Delete the iommufd_ctx->lock. Use RCU to protect load, erase/alloc does not need locking (order it properly too, it is in the wrong order), and don't check for duplicate devices or dev_cookie duplication, that is user error and is harmless to the kernel.

drivers/iommu/iommufd/iommufd.c line 50

Quoted 5 lines
drivers/iommu/iommufd/iommufd.c new file +5 -0
+static int iommufd_fops_release(struct inode *inode, struct file *filep)
+{
+	struct iommufd_ctx *ictx = filep->private_data;
+
+	filep->private_data = NULL;

unnecessary

drivers/iommu/iommufd/iommufd.c line 64

Quoted 13 lines
drivers/iommu/iommufd/iommufd.c new file +13 -0
+	iommufd_ctx_put(ictx);
+
+	return 0;
+}
+
+static long iommufd_fops_unl_ioctl(struct file *filep,
+				   unsigned int cmd, unsigned long arg)
+{
+	struct iommufd_ctx *ictx = filep->private_data;
+	long ret = -EINVAL;
+
+	if (!ictx)
+		return ret;

impossible

Quoted 8 lines

drivers/iommu/iommufd/iommufd.c line 68

Quoted 4 lines
drivers/iommu/iommufd/iommufd.c new file +4 -0
+
+	switch (cmd) {
+	default:
+		pr_err_ratelimited("unsupported cmd %u\n", cmd);

don't log user triggerable events

Jason

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

maintainer
#8 lore raw

Re: [RFC 01/20] iommu/iommufd: Add /dev/iommu core

Quoted 8 lines · Yi L
Quoted 5 lines

Acquire from the xarray is
rcu_lock()
ioas = xa_load()
if (ioas)

      if (down_read_trylock(&ioas->destroying_lock))

all good suggestions, will refine accordingly. Here destroying_lock is a rw_semaphore. right? Since down_read_trylock() accepts a rwsem.

Yes, you probably need a sleeping lock

Jason

author
#9 lore raw

RE: [RFC 01/20] iommu/iommufd: Add /dev/iommu core

Quoted 11 lines

From: Jason Gunthorpe <[email protected]>
Sent: Friday, October 15, 2021 7:18 PM

Quoted 6 lines · Yi L
Quoted 5 lines

Acquire from the xarray is
rcu_lock()
ioas = xa_load()
if (ioas)

      if (down_read_trylock(&ioas->destroying_lock))
Quoted 4 lines
Quoted 2 lines

all good suggestions, will refine accordingly. Here destroying_lock is a rw_semaphore. right? Since down_read_trylock() accepts a rwsem.

Yes, you probably need a sleeping lock

got it. thanks,

Regards,
Yi Liu

#10 lore raw

Re: [RFC 01/20] iommu/iommufd: Add /dev/iommu core

In reply to Jason Gunthorpe

Hi Jason,

Quoted 8 lines · Jason Gunthorpe
Quoted 6 lines · Yi L
Quoted 5 lines

Acquire from the xarray is
rcu_lock()
ioas = xa_load()
if (ioas)

      if (down_read_trylock(&ioas->destroying_lock))  
Quoted 5 lines
Quoted 2 lines

all good suggestions, will refine accordingly. Here destroying_lock is a rw_semaphore. right? Since down_read_trylock() accepts a rwsem.

Yes, you probably need a sleeping lock

I am not following why we want a sleeping lock inside RCU protected section?

For ioas, do we really care about the stale data to choose rw_lock vs RCU? Destroying can be kfree_rcu?

Quoted 1 line

Jason

Thanks,

Jacob

maintainer
#11 lore raw

Re: [RFC 01/20] iommu/iommufd: Add /dev/iommu core

Quoted 12 lines · Jacob Pan

Hi Jason,

Quoted 8 lines · Jason Gunthorpe
Quoted 6 lines · Yi L
Quoted 5 lines

Acquire from the xarray is
rcu_lock()
ioas = xa_load()
if (ioas)

      if (down_read_trylock(&ioas->destroying_lock))  
Quoted 7 lines
Quoted 5 lines
Quoted 2 lines

all good suggestions, will refine accordingly. Here destroying_lock is a rw_semaphore. right? Since down_read_trylock() accepts a rwsem.

Yes, you probably need a sleeping lock

I am not following why we want a sleeping lock inside RCU protected section?

trylock is not sleeping

Quoted 2 lines

For ioas, do we really care about the stale data to choose rw_lock vs RCU? Destroying can be kfree_rcu?

It needs a hard fence so things don't continue to use the IOS once it is destroyed.

Jason

#12 lore raw

Re: [RFC 01/20] iommu/iommufd: Add /dev/iommu core

Hi Jason,

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

Hi Jason,

Quoted 12 lines · Jason Gunthorpe
Quoted 9 lines · Yi L
Quoted 5 lines

Acquire from the xarray is
rcu_lock()
ioas = xa_load()
if (ioas)

      if (down_read_trylock(&ioas->destroying_lock))    

all good suggestions, will refine accordingly. Here destroying_lock is a rw_semaphore. right? Since down_read_trylock() accepts a rwsem.

Quoted 6 lines
Quoted 4 lines
Quoted 2 lines

Yes, you probably need a sleeping lock

I am not following why we want a sleeping lock inside RCU protected section?

trylock is not sleeping

Of course, thanks for clarifying.

Quoted 5 lines
Quoted 2 lines

For ioas, do we really care about the stale data to choose rw_lock vs RCU? Destroying can be kfree_rcu?

It needs a hard fence so things don't continue to use the IOS once it is destroyed.

I guess RCU can do that also perhaps can scale better?

Quoted 1 line

Jason

Thanks,

Jacob

maintainer
#13 lore raw

Re: [RFC 01/20] iommu/iommufd: Add /dev/iommu core

Quoted 27 lines · Jacob Pan

Hi Jason,

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

Hi Jason,

Quoted 12 lines · Jason Gunthorpe
Quoted 9 lines · Yi L
Quoted 5 lines

Acquire from the xarray is
rcu_lock()
ioas = xa_load()
if (ioas)

      if (down_read_trylock(&ioas->destroying_lock))    

all good suggestions, will refine accordingly. Here destroying_lock is a rw_semaphore. right? Since down_read_trylock() accepts a rwsem.

Quoted 6 lines
Quoted 4 lines
Quoted 2 lines

Yes, you probably need a sleeping lock

I am not following why we want a sleeping lock inside RCU protected section?

trylock is not sleeping

Quoted 8 lines

Of course, thanks for clarifying.

Quoted 5 lines
Quoted 2 lines

For ioas, do we really care about the stale data to choose rw_lock vs RCU? Destroying can be kfree_rcu?

It needs a hard fence so things don't continue to use the IOS once it is destroyed.

I guess RCU can do that also perhaps can scale better?

RCU is not a fence, it is an eventual consistency mechanism and has very bad scaling if you don't use things like kfree_rcu

Jason

author
#14 lore raw

RFC 2/20 vfio: Add device class for /dev/vfio/devices

In reply to Liu Yi L

This patch introduces a new interface (/dev/vfio/devices/$DEVICE) for userspace to directly open a vfio device w/o relying on container/group (/dev/vfio/$GROUP). Anything related to group is now hidden behind iommufd (more specifically in iommu core by this RFC) in a device-centric manner.

In case a device is exposed in both legacy and new interfaces (see next patch for how to decide it), this patch also ensures that when the device is already opened via one interface then the other one must be blocked.

---
 drivers/vfio/vfio.c  | 228 +++++++++++++++++++++++++++++++++++++++----
 include/linux/vfio.h |   2 +
 2 files changed, 213 insertions(+), 17 deletions(-)
drivers/vfio/vfio.c +211 -17
index 02cc51ce6891..84436d7abedd 100644
@@ -46,6 +46,12 @@ static struct vfio {
 	struct mutex			group_lock;
 	struct cdev			group_cdev;
 	dev_t				group_devt;
+	/* Fields for /dev/vfio/devices interface */
+	struct class			*device_class;
+	struct cdev			device_cdev;
+	dev_t				device_devt;
+	struct mutex			device_lock;
+	struct idr			device_idr;
 } vfio;
 
 struct vfio_iommu_driver {
@@ -81,9 +87,11 @@ struct vfio_group {
 	struct list_head		container_next;
 	struct list_head		unbound_list;
 	struct mutex			unbound_lock;
-	atomic_t			opened;
-	wait_queue_head_t		container_q;
+	struct mutex			opened_lock;
+	u32				opened;
+	bool				opened_by_nongroup_dev;
 	bool				noiommu;
+	wait_queue_head_t		container_q;
 	unsigned int			dev_counter;
 	struct kvm			*kvm;
 	struct blocking_notifier_head	notifier;
@@ -327,7 +335,7 @@ static struct vfio_group *vfio_create_group(struct iommu_group *iommu_group)
 	INIT_LIST_HEAD(&group->unbound_list);
 	mutex_init(&group->unbound_lock);
 	atomic_set(&group->container_users, 0);
-	atomic_set(&group->opened, 0);
+	mutex_init(&group->opened_lock);
 	init_waitqueue_head(&group->container_q);
 	group->iommu_group = iommu_group;
 #ifdef CONFIG_VFIO_NOIOMMU
@@ -1489,10 +1497,53 @@ static long vfio_group_fops_unl_ioctl(struct file *filep,
 	return ret;
 }
 
+/*
+ * group->opened is used to ensure that the group can be opened only via
+ * one of the two interfaces (/dev/vfio/$GROUP and /dev/vfio/devices/
+ * $DEVICE) instead of both.
+ *
+ * We also introduce a new group flag to indicate whether this group is
+ * opened via /dev/vfio/devices/$DEVICE. For multi-devices group,
+ * group->opened also tracks how many devices have been opened in the
+ * group if the new flag is true.
+ *
+ * Also add a new lock since two flags are operated here.
+ */
+static int vfio_group_try_open(struct vfio_group *group, bool nongroup_dev)
+{
+	int ret = 0;
+
+	mutex_lock(&group->opened_lock);
+	if (group->opened) {
+		if (nongroup_dev && group->opened_by_nongroup_dev)
+			group->opened++;
+		else
+			ret = -EBUSY;
+		goto out;
+	}
+
+	/*
+	 * Is something still in use from a previous open? Should
+	 * not allow new open if it is such case.
+	 */
+	if (group->container) {
+		ret = -EBUSY;
+		goto out;
+	}
+
+	group->opened = 1;
+	group->opened_by_nongroup_dev = nongroup_dev;
+
+out:
+	mutex_unlock(&group->opened_lock);
+
+	return ret;
+}
+
 static int vfio_group_fops_open(struct inode *inode, struct file *filep)
 {
 	struct vfio_group *group;
-	int opened;
+	int ret;
 
 	group = vfio_group_get_from_minor(iminor(inode));
 	if (!group)
@@ -1503,18 +1554,10 @@ static int vfio_group_fops_open(struct inode *inode, struct file *filep)
 		return -EPERM;
 	}
 
-	/* Do we need multiple instances of the group open?  Seems not. */
-	opened = atomic_cmpxchg(&group->opened, 0, 1);
-	if (opened) {
-		vfio_group_put(group);
-		return -EBUSY;
-	}
-
-	/* Is something still in use from a previous open? */
-	if (group->container) {
-		atomic_dec(&group->opened);
+	ret = vfio_group_try_open(group, false);
+	if (ret) {
 		vfio_group_put(group);
-		return -EBUSY;
+		return ret;
 	}
 
 	/* Warn if previous user didn't cleanup and re-init to drop them */
@@ -1534,7 +1577,9 @@ static int vfio_group_fops_release(struct inode *inode, struct file *filep)
 
 	vfio_group_try_dissolve_container(group);
 
-	atomic_dec(&group->opened);
+	mutex_lock(&group->opened_lock);
+	group->opened--;
+	mutex_unlock(&group->opened_lock);
 
 	vfio_group_put(group);
 
@@ -1552,6 +1597,92 @@ static const struct file_operations vfio_group_fops = {
 /**
  * VFIO Device fd
  */
+static struct vfio_device *vfio_device_get_from_minor(int minor)
+{
+	struct vfio_device *device;
+
+	mutex_lock(&vfio.device_lock);
+	device = idr_find(&vfio.device_idr, minor);
+	if (!device || !vfio_device_try_get(device)) {
+		mutex_unlock(&vfio.device_lock);
+		return NULL;
+	}
+	mutex_unlock(&vfio.device_lock);
+
+	return device;
+}
+
+static int vfio_device_fops_open(struct inode *inode, struct file *filep)
+{
+	struct vfio_device *device;
+	struct vfio_group *group;
+	int ret, opened;
+
+	device = vfio_device_get_from_minor(iminor(inode));
+	if (!device)
+		return -ENODEV;
+
+	/*
+	 * Check whether the user has opened this device via the legacy
+	 * container/group interface. If yes, then prevent the user from
+	 * opening it via device node in /dev/vfio/devices. Otherwise,
+	 * mark the group as opened to block the group interface. either
+	 * way, we must ensure only one interface is used to open the
+	 * device when it supports both legacy and new interfaces.
+	 */
+	group = vfio_group_try_get(device->group);
+	if (group) {
+		ret = vfio_group_try_open(group, true);
+		if (ret)
+			goto err_group_try_open;
+	}
+
+	/*
+	 * No support of multiple instances of the device open, similar to
+	 * the policy on the group open.
+	 */
+	opened = atomic_cmpxchg(&device->opened, 0, 1);
+	if (opened) {
+		ret = -EBUSY;
+		goto err_device_try_open;
+	}
+
+	if (!try_module_get(device->dev->driver->owner)) {
+		ret = -ENODEV;
+		goto err_module_get;
+	}
+
+	ret = device->ops->open(device);
+	if (ret)
+		goto err_device_open;
+
+	filep->private_data = device;
+
+	if (group)
+		vfio_group_put(group);
+	return 0;
+err_device_open:
+	module_put(device->dev->driver->owner);
+err_module_get:
+	atomic_dec(&device->opened);
+err_device_try_open:
+	if (group) {
+		mutex_lock(&group->opened_lock);
+		group->opened--;
+		mutex_unlock(&group->opened_lock);
+	}
+err_group_try_open:
+	if (group)
+		vfio_group_put(group);
+	vfio_device_put(device);
+	return ret;
+}
+
+static bool vfio_device_in_container(struct vfio_device *device)
+{
+	return !!(device->group && device->group->container);
+}
+
 static int vfio_device_fops_release(struct inode *inode, struct file *filep)
 {
 	struct vfio_device *device = filep->private_data;
@@ -1560,7 +1691,16 @@ static int vfio_device_fops_release(struct inode *inode, struct file *filep)
 
 	module_put(device->dev->driver->owner);
 
-	vfio_group_try_dissolve_container(device->group);
+	if (vfio_device_in_container(device)) {
+		vfio_group_try_dissolve_container(device->group);
+	} else {
+		atomic_dec(&device->opened);
+		if (device->group) {
+			mutex_lock(&device->group->opened_lock);
+			device->group->opened--;
+			mutex_unlock(&device->group->opened_lock);
+		}
+	}
 
 	vfio_device_put(device);
 
@@ -1613,6 +1753,7 @@ static int vfio_device_fops_mmap(struct file *filep, struct vm_area_struct *vma)
 
 static const struct file_operations vfio_device_fops = {
 	.owner		= THIS_MODULE,
+	.open		= vfio_device_fops_open,
 	.release	= vfio_device_fops_release,
 	.read		= vfio_device_fops_read,
 	.write		= vfio_device_fops_write,
@@ -2295,6 +2436,52 @@ static struct miscdevice vfio_dev = {
 	.mode = S_IRUGO | S_IWUGO,
 };
 
+static char *vfio_device_devnode(struct device *dev, umode_t *mode)
+{
+	return kasprintf(GFP_KERNEL, "vfio/devices/%s", dev_name(dev));
+}
+
+static int vfio_init_device_class(void)
+{
+	int ret;
+
+	mutex_init(&vfio.device_lock);
+	idr_init(&vfio.device_idr);
+
+	/* /dev/vfio/devices/$DEVICE */
+	vfio.device_class = class_create(THIS_MODULE, "vfio-device");
+	if (IS_ERR(vfio.device_class))
+		return PTR_ERR(vfio.device_class);
+
+	vfio.device_class->devnode = vfio_device_devnode;
+
+	ret = alloc_chrdev_region(&vfio.device_devt, 0, MINORMASK + 1, "vfio-device");
+	if (ret)
+		goto err_alloc_chrdev;
+
+	cdev_init(&vfio.device_cdev, &vfio_device_fops);
+	ret = cdev_add(&vfio.device_cdev, vfio.device_devt, MINORMASK + 1);
+	if (ret)
+		goto err_cdev_add;
+	return 0;
+
+err_cdev_add:
+	unregister_chrdev_region(vfio.device_devt, MINORMASK + 1);
+err_alloc_chrdev:
+	class_destroy(vfio.device_class);
+	vfio.device_class = NULL;
+	return ret;
+}
+
+static void vfio_destroy_device_class(void)
+{
+	cdev_del(&vfio.device_cdev);
+	unregister_chrdev_region(vfio.device_devt, MINORMASK + 1);
+	class_destroy(vfio.device_class);
+	vfio.device_class = NULL;
+	idr_destroy(&vfio.device_idr);
+}
+
 static int __init vfio_init(void)
 {
 	int ret;
@@ -2329,6 +2516,10 @@ static int __init vfio_init(void)
 	if (ret)
 		goto err_cdev_add;
 
+	ret = vfio_init_device_class();
+	if (ret)
+		goto err_init_device_class;
+
 	pr_info(DRIVER_DESC " version: " DRIVER_VERSION "\n");
 
 #ifdef CONFIG_VFIO_NOIOMMU
@@ -2336,6 +2527,8 @@ static int __init vfio_init(void)
 #endif
 	return 0;
 
+err_init_device_class:
+	cdev_del(&vfio.group_cdev);
 err_cdev_add:
 	unregister_chrdev_region(vfio.group_devt, MINORMASK + 1);
 err_alloc_chrdev:
@@ -2358,6 +2551,7 @@ static void __exit vfio_cleanup(void)
 	unregister_chrdev_region(vfio.group_devt, MINORMASK + 1);
 	class_destroy(vfio.class);
 	vfio.class = NULL;
+	vfio_destroy_device_class();
 	misc_deregister(&vfio_dev);
 }
 
include/linux/vfio.h +2 -0
index a2c5b30e1763..4a5f3f99eab2 100644
@@ -24,6 +24,8 @@ struct vfio_device {
 	refcount_t refcount;
 	struct completion comp;
 	struct list_head group_next;
+	int minor;
+	atomic_t opened;
 };
 
 /**
Signature · 1 line
2.25.1
maintainer
#15 lore raw

Re: [RFC 02/20] vfio: Add device class for /dev/vfio/devices

Quoted 28 lines · Liu Yi L

This patch introduces a new interface (/dev/vfio/devices/$DEVICE) for userspace to directly open a vfio device w/o relying on container/group (/dev/vfio/$GROUP). Anything related to group is now hidden behind iommufd (more specifically in iommu core by this RFC) in a device-centric manner.

In case a device is exposed in both legacy and new interfaces (see next patch for how to decide it), this patch also ensures that when the device is already opened via one interface then the other one must be blocked.

---
 drivers/vfio/vfio.c  | 228 +++++++++++++++++++++++++++++++++++++++----
 include/linux/vfio.h |   2 +
 2 files changed, 213 insertions(+), 17 deletions(-)
drivers/vfio/vfio.c +12 -0
+static int vfio_init_device_class(void)
+{
+	int ret;
+
+	mutex_init(&vfio.device_lock);
+	idr_init(&vfio.device_idr);
+
+	/* /dev/vfio/devices/$DEVICE */
+	vfio.device_class = class_create(THIS_MODULE, "vfio-device");
+	if (IS_ERR(vfio.device_class))
+		return PTR_ERR(vfio.device_class);
+

drivers/vfio/vfio.c static struct miscdevice vfio_dev = { · line 2465

Quoted 10 lines
drivers/vfio/vfio.c +10 -0
+	vfio.device_class->devnode = vfio_device_devnode;
+
+	ret = alloc_chrdev_region(&vfio.device_devt, 0, MINORMASK + 1, "vfio-device");
+	if (ret)
+		goto err_alloc_chrdev;
+
+	cdev_init(&vfio.device_cdev, &vfio_device_fops);
+	ret = cdev_add(&vfio.device_cdev, vfio.device_devt, MINORMASK + 1);
+	if (ret)
+		goto err_cdev_add;

Huh? This is not how cdevs are used. This patch needs rewriting.

The struct vfio_device should gain a 'struct device' and 'struct cdev' as non-pointer members

vfio register path should end up doing cdev_device_add() for each vfio_device

vfio_unregister path should do cdev_device_del()

No idr should be needed, an ida is used to allocate minor numbers

The struct device release function should trigger a kfree which requires some reworking of the callers

vfio_init_group_dev() should do a device_initialize() vfio_uninit_group_dev() should do a device_put()

The opened atomic is aweful. A newly created fd should start in a state where it has a disabled fops

The only thing the disabled fops can do is register the device to the iommu fd. When successfully registered the device gets the normal fops.

The registration steps should be done under a normal lock inside the vfio_device. If a vfio_device is already registered then further registration should fail.

Getting the device fd via the group fd triggers the same sequence as above.

Jason

maintainer
#16 lore raw

RE: [RFC 02/20] vfio: Add device class for /dev/vfio/devices

Quoted 55 lines

From: Jason Gunthorpe <[email protected]>
Sent: Tuesday, September 21, 2021 11:57 PM

Quoted 21 lines · Liu Yi L

This patch introduces a new interface (/dev/vfio/devices/$DEVICE) for userspace to directly open a vfio device w/o relying on container/group (/dev/vfio/$GROUP). Anything related to group is now hidden behind iommufd (more specifically in iommu core by this RFC) in a device-centric manner.

In case a device is exposed in both legacy and new interfaces (see next patch for how to decide it), this patch also ensures that when the device is already opened via one interface then the other one must be blocked.

---
 drivers/vfio/vfio.c  | 228 +++++++++++++++++++++++++++++++++++++++----
 include/linux/vfio.h |   2 +
 2 files changed, 213 insertions(+), 17 deletions(-)
drivers/vfio/vfio.c +5 -0
+static int vfio_init_device_class(void)
+{
+	int ret;
+
+	mutex_init(&vfio.device_lock);

drivers/vfio/vfio.c static struct miscdevice vfio_dev = {

Quoted 10 lines
drivers/vfio/vfio.c +10 -0
+	idr_init(&vfio.device_idr);
+
+	/* /dev/vfio/devices/$DEVICE */
+	vfio.device_class = class_create(THIS_MODULE, "vfio-device");
+	if (IS_ERR(vfio.device_class))
+		return PTR_ERR(vfio.device_class);
+
+	vfio.device_class->devnode = vfio_device_devnode;
+
+	ret = alloc_chrdev_region(&vfio.device_devt, 0, MINORMASK + 1,

"vfio-device");

drivers/vfio/vfio.c static struct miscdevice vfio_dev = {

Quoted 5 lines
drivers/vfio/vfio.c +5 -0
+	if (ret)
+		goto err_alloc_chrdev;
+
+	cdev_init(&vfio.device_cdev, &vfio_device_fops);
+	ret = cdev_add(&vfio.device_cdev, vfio.device_devt, MINORMASK +

1);

Review

Quoted 2 lines
(quoted diff) +2 -0
+	if (ret)
+		goto err_cdev_add;

Huh? This is not how cdevs are used. This patch needs rewriting.

The struct vfio_device should gain a 'struct device' and 'struct cdev' as non-pointer members

vfio register path should end up doing cdev_device_add() for each vfio_device

vfio_unregister path should do cdev_device_del()

Quoted 7 lines

No idr should be needed, an ida is used to allocate minor numbers

The struct device release function should trigger a kfree which requires some reworking of the callers

vfio_init_group_dev() should do a device_initialize() vfio_uninit_group_dev() should do a device_put()

All above are good suggestions!

Quoted 7 lines

The opened atomic is aweful. A newly created fd should start in a state where it has a disabled fops

The only thing the disabled fops can do is register the device to the iommu fd. When successfully registered the device gets the normal fops.

Quoted 7 lines

The registration steps should be done under a normal lock inside the vfio_device. If a vfio_device is already registered then further registration should fail.

Getting the device fd via the group fd triggers the same sequence as above.

Above works if the group interface is also connected to iommufd, i.e. making vfio type1 as a shim. In this case we can use the registration status as the exclusive switch. But if we keep vfio type1 separate as today, then a new atomic is still necessary. This all depends on how we want to deal with vfio type1 and iommufd, and possibly what's discussed here just adds another pound to the shim option...

Thanks
Kevin

maintainer
#17 lore raw

Re: [RFC 02/20] vfio: Add device class for /dev/vfio/devices

Quoted 14 lines · Kevin
Quoted 6 lines

The opened atomic is aweful. A newly created fd should start in a state where it has a disabled fops

The only thing the disabled fops can do is register the device to the iommu fd. When successfully registered the device gets the normal fops.

Quoted 7 lines

The registration steps should be done under a normal lock inside the vfio_device. If a vfio_device is already registered then further registration should fail.

Getting the device fd via the group fd triggers the same sequence as above.

Quoted 6 lines

Above works if the group interface is also connected to iommufd, i.e. making vfio type1 as a shim. In this case we can use the registration status as the exclusive switch. But if we keep vfio type1 separate as today, then a new atomic is still necessary. This all depends on how we want to deal with vfio type1 and iommufd, and possibly what's discussed here just adds another pound to the shim option...

No, it works the same either way, the group FD path is identical to the normal FD path, it just triggers some of the state transitions automatically internally instead of requiring external ioctls.

The device FDs starts disabled, an internal API binds it to the iommu via open coding with the group API, and then the rest of the APIs can be enabled. Same as today.

Jason

maintainer
#18 lore raw

RE: [RFC 02/20] vfio: Add device class for /dev/vfio/devices

Quoted 25 lines

From: Jason Gunthorpe <[email protected]>
Sent: Wednesday, September 22, 2021 8:55 AM

Quoted 14 lines · Kevin
Quoted 6 lines

The opened atomic is aweful. A newly created fd should start in a state where it has a disabled fops

The only thing the disabled fops can do is register the device to the iommu fd. When successfully registered the device gets the normal fops.

Quoted 7 lines

The registration steps should be done under a normal lock inside the vfio_device. If a vfio_device is already registered then further registration should fail.

Getting the device fd via the group fd triggers the same sequence as above.

Quoted 6 lines

Above works if the group interface is also connected to iommufd, i.e. making vfio type1 as a shim. In this case we can use the registration status as the exclusive switch. But if we keep vfio type1 separate as today, then a new atomic is still necessary. This all depends on how we want to deal with vfio type1 and iommufd, and possibly what's discussed here just adds another pound to the shim option...

Quoted 8 lines

No, it works the same either way, the group FD path is identical to the normal FD path, it just triggers some of the state transitions automatically internally instead of requiring external ioctls.

The device FDs starts disabled, an internal API binds it to the iommu via open coding with the group API, and then the rest of the APIs can be enabled. Same as today.

Still a bit confused. if vfio type1 also connects to iommufd, whether the device is registered can be centrally checked based on whether an iommu_ctx is recorded. But if type1 doesn't talk to iommufd at all, don't we still need introduce a new state (calling it 'opened' or 'registered') to protect the two interfaces? In this case what is the point of keeping device FD disabled even for the group path?

maintainer
#19 lore raw

Re: [RFC 02/20] vfio: Add device class for /dev/vfio/devices

Quoted 34 lines · Kevin
Quoted 25 lines

From: Jason Gunthorpe <[email protected]>
Sent: Wednesday, September 22, 2021 8:55 AM

Quoted 14 lines · Kevin
Quoted 6 lines

The opened atomic is aweful. A newly created fd should start in a state where it has a disabled fops

The only thing the disabled fops can do is register the device to the iommu fd. When successfully registered the device gets the normal fops.

Quoted 7 lines

The registration steps should be done under a normal lock inside the vfio_device. If a vfio_device is already registered then further registration should fail.

Getting the device fd via the group fd triggers the same sequence as above.

Quoted 6 lines

Above works if the group interface is also connected to iommufd, i.e. making vfio type1 as a shim. In this case we can use the registration status as the exclusive switch. But if we keep vfio type1 separate as today, then a new atomic is still necessary. This all depends on how we want to deal with vfio type1 and iommufd, and possibly what's discussed here just adds another pound to the shim option...

Quoted 8 lines

No, it works the same either way, the group FD path is identical to the normal FD path, it just triggers some of the state transitions automatically internally instead of requiring external ioctls.

The device FDs starts disabled, an internal API binds it to the iommu via open coding with the group API, and then the rest of the APIs can be enabled. Same as today.

Quoted 5 lines

Still a bit confused. if vfio type1 also connects to iommufd, whether the device is registered can be centrally checked based on whether an iommu_ctx is recorded. But if type1 doesn't talk to iommufd at all, don't we still need introduce a new state (calling it 'opened' or 'registered') to protect the two interfaces?

The "new state" is if the fops are pointing at the real fops or the pre-fops, which in turn protects everything. You could imagine this as some state in front of every fop call if you want.

Quoted 2 lines

In this case what is the point of keeping device FD disabled even for the group path?

I have a feeling when you go through the APIs it will make sense to have some symmetry here.

eg creating a device FD should have basically the same flow no matter what triggers it, not confusing special cases where the group code skips steps

Jason

maintainer
#20 lore raw

RE: [RFC 02/20] vfio: Add device class for /dev/vfio/devices

In reply to Jason Gunthorpe
Quoted 36 lines

From: Tian, Kevin
Sent: Wednesday, September 22, 2021 9:07 AM

Quoted 33 lines

From: Jason Gunthorpe <[email protected]>
Sent: Wednesday, September 22, 2021 8:55 AM

Quoted 14 lines · Kevin
Quoted 6 lines

The opened atomic is aweful. A newly created fd should start in a state where it has a disabled fops

The only thing the disabled fops can do is register the device to the iommu fd. When successfully registered the device gets the normal fops.

Quoted 7 lines

The registration steps should be done under a normal lock inside the vfio_device. If a vfio_device is already registered then further registration should fail.

Getting the device fd via the group fd triggers the same sequence as above.

Quoted 6 lines

Above works if the group interface is also connected to iommufd, i.e. making vfio type1 as a shim. In this case we can use the registration status as the exclusive switch. But if we keep vfio type1 separate as today, then a new atomic is still necessary. This all depends on how we want to deal with vfio type1 and iommufd, and possibly what's discussed here just adds another pound to the shim option...

No, it works the same either way, the group FD path is identical to the normal FD path, it just triggers some of the state transitions automatically internally instead of requiring external ioctls.

The device FDs starts disabled, an internal API binds it to the iommu via open coding with the group API, and then the rest of the APIs can be enabled. Same as today.

After reading your comments on patch08, I may have a clearer picture on your suggestion. The key is to handle exclusive access at the binding time (based on vdev->iommu_dev). Please see whether below makes sense:

Shared sequence:

1)  initialize the device with a parked fops;
2)  need binding (explicit or implicit) to move away from parked fops;
3)  switch to normal fops after successful binding;

1) happens at device probe.

for nongroup 2) and 3) are done together in VFIO_DEVICE_GET_IOMMUFD:

- 2) is done by calling .bind_iommufd() callback;
- 3) could be done within .bind_iommufd(), or via a new callback e.g.

    .finalize_device(). The latter may be preferred for the group interface;
  - Two threads may open the same device simultaneously, with exclusive 
    access guaranteed by iommufd_bind_device();
  - Open() after successful binding is rejected, since normal fops has been
    activated. This is checked upon vdev->iommu_dev;

for group 2/3) are done together in VFIO_GROUP_GET_DEVICE_FD:

- 2) is done by open coding bind_iommufd + attach_ioas. Create an

    iommufd_device object and record it to vdev->iommu_dev

- 3) is done by calling .finalize_device();
- open() after a valid vdev->iommu_dev is rejected. this also ensures exclusive ownership with the nongroup path.

If Alex also agrees with it, this might be another mini-series to be merged (just for group path) before this one. Doing so sort of nullifies the existing group/container attaching process, where attach_ioas will be skipped and now the security context is established when the device is opened.

Thanks
Kevin

maintainer
#21 lore raw

Re: [RFC 02/20] vfio: Add device class for /dev/vfio/devices

Quoted 42 lines · Kevin
Quoted 36 lines

From: Tian, Kevin
Sent: Wednesday, September 22, 2021 9:07 AM

Quoted 33 lines

From: Jason Gunthorpe <[email protected]>
Sent: Wednesday, September 22, 2021 8:55 AM

Quoted 14 lines · Kevin
Quoted 6 lines

The opened atomic is aweful. A newly created fd should start in a state where it has a disabled fops

The only thing the disabled fops can do is register the device to the iommu fd. When successfully registered the device gets the normal fops.

Quoted 7 lines

The registration steps should be done under a normal lock inside the vfio_device. If a vfio_device is already registered then further registration should fail.

Getting the device fd via the group fd triggers the same sequence as above.

Quoted 6 lines

Above works if the group interface is also connected to iommufd, i.e. making vfio type1 as a shim. In this case we can use the registration status as the exclusive switch. But if we keep vfio type1 separate as today, then a new atomic is still necessary. This all depends on how we want to deal with vfio type1 and iommufd, and possibly what's discussed here just adds another pound to the shim option...

No, it works the same either way, the group FD path is identical to the normal FD path, it just triggers some of the state transitions automatically internally instead of requiring external ioctls.

The device FDs starts disabled, an internal API binds it to the iommu via open coding with the group API, and then the rest of the APIs can be enabled. Same as today.

After reading your comments on patch08, I may have a clearer picture on your suggestion. The key is to handle exclusive access at the binding time (based on vdev->iommu_dev). Please see whether below makes sense:

Quoted 7 lines

Shared sequence:

1)  initialize the device with a parked fops;
2)  need binding (explicit or implicit) to move away from parked fops;
3)  switch to normal fops after successful binding;

1) happens at device probe.

1 happens when the cdev is setup with the parked fops, yes. I'd say it happens at fd open time.

Quoted 9 lines

for nongroup 2) and 3) are done together in VFIO_DEVICE_GET_IOMMUFD:

- 2) is done by calling .bind_iommufd() callback;
- 3) could be done within .bind_iommufd(), or via a new callback e.g.

    .finalize_device(). The latter may be preferred for the group interface;
  - Two threads may open the same device simultaneously, with exclusive 
    access guaranteed by iommufd_bind_device();
  - Open() after successful binding is rejected, since normal fops has been
    activated. This is checked upon vdev->iommu_dev;

Almost, open is always successful, what fails is VFIO_DEVICE_GET_IOMMUFD (or the group equivilant). The user ends up with a FD that is useless, cannot reach the ops and thus cannot impact the device it doesn't own in any way.

It is similar to opening a group FD

Quoted 7 lines

for group 2/3) are done together in VFIO_GROUP_GET_DEVICE_FD:

- 2) is done by open coding bind_iommufd + attach_ioas. Create an

    iommufd_device object and record it to vdev->iommu_dev

- 3) is done by calling .finalize_device();
- open() after a valid vdev->iommu_dev is rejected. this also ensures exclusive ownership with the nongroup path.

Same comment as above, groups should go through the same sequence of steps, create a FD, attempt to bind, if successuful make the FD operational.

The only difference is that failure in these steps does not call fd_install(). For this reason alone the FD could start out with operational fops, but it feels like a needless optimization.

Quoted 4 lines

If Alex also agrees with it, this might be another mini-series to be merged (just for group path) before this one. Doing so sort of nullifies the existing group/container attaching process, where attach_ioas will be skipped and now the security context is established when the device is opened.

I think it is really important to unify DMA exclusion model and lower to the core iommu code. If there is a reason the exclusion must be triggered on group fd open then the iommu core code should provide an API to do that which interworks with the device API iommufd will work.

But I would start here because it is much simpler to understand..

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

maintainer
#22 lore raw

RE: [RFC 02/20] vfio: Add device class for /dev/vfio/devices

Quoted 69 lines

From: Jason Gunthorpe <[email protected]>
Sent: Wednesday, September 22, 2021 8:51 PM

Quoted 6 lines · Kevin
Quoted 6 lines

From: Tian, Kevin
Sent: Wednesday, September 22, 2021 9:07 AM

Quoted 3 lines

From: Jason Gunthorpe <[email protected]>
Sent: Wednesday, September 22, 2021 8:55 AM

Quoted 6 lines
Quoted 6 lines
Quoted 6 lines · Kevin
Quoted 5 lines
Quoted 5 lines

The opened atomic is aweful. A newly created fd should start in a state where it has a disabled fops

The only thing the disabled fops can do is register the device to the iommu fd. When successfully registered the device gets the normal

fops.

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

The registration steps should be done under a normal lock inside

the

Quoted 28 lines
Quoted 14 lines
Quoted 14 lines
Quoted 13 lines
Quoted 6 lines

vfio_device. If a vfio_device is already registered then further registration should fail.

Getting the device fd via the group fd triggers the same sequence as above.

Above works if the group interface is also connected to iommufd, i.e. making vfio type1 as a shim. In this case we can use the registration status as the exclusive switch. But if we keep vfio type1 separate as today, then a new atomic is still necessary. This all depends on how we want to deal with vfio type1 and iommufd, and possibly what's discussed here just adds another pound to the shim option...

Quoted 8 lines
Quoted 8 lines

No, it works the same either way, the group FD path is identical to the normal FD path, it just triggers some of the state transitions automatically internally instead of requiring external ioctls.

The device FDs starts disabled, an internal API binds it to the iommu via open coding with the group API, and then the rest of the APIs can be enabled. Same as today.

After reading your comments on patch08, I may have a clearer picture on your suggestion. The key is to handle exclusive access at the binding time (based on vdev->iommu_dev). Please see whether below makes sense:

Quoted 7 lines

Shared sequence:

1)  initialize the device with a parked fops;
2)  need binding (explicit or implicit) to move away from parked fops;
3)  switch to normal fops after successful binding;

1) happens at device probe.

1 happens when the cdev is setup with the parked fops, yes. I'd say it happens at fd open time.

Quoted 9 lines

for nongroup 2) and 3) are done together in VFIO_DEVICE_GET_IOMMUFD:

- 2) is done by calling .bind_iommufd() callback;
- 3) could be done within .bind_iommufd(), or via a new callback e.g.

    .finalize_device(). The latter may be preferred for the group interface;
  - Two threads may open the same device simultaneously, with exclusive
    access guaranteed by iommufd_bind_device();
  - Open() after successful binding is rejected, since normal fops has been
    activated. This is checked upon vdev->iommu_dev;
Quoted 4 lines

Almost, open is always successful, what fails is VFIO_DEVICE_GET_IOMMUFD (or the group equivilant). The user ends up with a FD that is useless, cannot reach the ops and thus cannot impact the device it doesn't own in any way.

make sense. I had an wrong impression that once a normal fops is activated it is also visible to other threads. But in concept this fops replacement should be local to each thread thus another thread opening the device always gets a parked fops.

Quoted 24 lines

It is similar to opening a group FD

Quoted 7 lines

for group 2/3) are done together in VFIO_GROUP_GET_DEVICE_FD:

- 2) is done by open coding bind_iommufd + attach_ioas. Create an

    iommufd_device object and record it to vdev->iommu_dev

- 3) is done by calling .finalize_device();
- open() after a valid vdev->iommu_dev is rejected. this also ensures exclusive ownership with the nongroup path.

Same comment as above, groups should go through the same sequence of steps, create a FD, attempt to bind, if successuful make the FD operational.

The only difference is that failure in these steps does not call fd_install(). For this reason alone the FD could start out with operational fops, but it feels like a needless optimization.

Quoted 4 lines

If Alex also agrees with it, this might be another mini-series to be merged (just for group path) before this one. Doing so sort of nullifies the existing group/container attaching process, where attach_ioas will be skipped and now the security context is established when the device is opened.

Quoted 7 lines

I think it is really important to unify DMA exclusion model and lower to the core iommu code. If there is a reason the exclusion must be triggered on group fd open then the iommu core code should provide an API to do that which interworks with the device API iommufd will work.

But I would start here because it is much simpler to understand..

Let's work on this task first and figure out what's the cleaner way to unify it. My current impression is that having an iommu api for group fd open might be simpler. Currently vfio iommu drivers are coupled with container with group-granular operations. Adapting them to device fd open will require more changes to handle device<->group. anyway we'll see...
_______________________________________________
iommu mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/iommu

#23 lore raw

Re: [RFC 02/20] vfio: Add device class for /dev/vfio/devices

In reply to Liu Yi L
Quoted 20 lines · On Sun, 19 Sep 2021 14:38:30 +0800 Liu Yi L <[email protected]> wrote:

This patch introduces a new interface (/dev/vfio/devices/$DEVICE) for userspace to directly open a vfio device w/o relying on container/group (/dev/vfio/$GROUP). Anything related to group is now hidden behind iommufd (more specifically in iommu core by this RFC) in a device-centric manner.

In case a device is exposed in both legacy and new interfaces (see next patch for how to decide it), this patch also ensures that when the device is already opened via one interface then the other one must be blocked.

---
 drivers/vfio/vfio.c  | 228 +++++++++++++++++++++++++++++++++++++++----
 include/linux/vfio.h |   2 +
 2 files changed, 213 insertions(+), 17 deletions(-)
drivers/vfio/vfio.c +0 -0
index 02cc51ce6891..84436d7abedd 100644

...

drivers/vfio/vfio.c static struct miscdevice vfio_dev = { · line 2442

Quoted 8 lines
drivers/vfio/vfio.c +4 -0
@@ -2295,6 +2436,52 @@ static struct miscdevice vfio_dev = {
 	.mode = S_IRUGO | S_IWUGO,
 };
 
+static char *vfio_device_devnode(struct device *dev, umode_t *mode)
+{
+	return kasprintf(GFP_KERNEL, "vfio/devices/%s", dev_name(dev));
+}

dev_name() doesn't provide us with any uniqueness guarantees, so this could potentially generate naming conflicts. The similar scheme for devices within an iommu group appends an instance number if a conflict occurs, but that solution doesn't work here where the name isn't just a link to the actual device. Devices within an iommu group are also likely associated within a bus_type, so the potential for conflict is pretty negligible, that's not the case as vfio is adopted for new device types. Thanks,

Alex

maintainer
#24 lore raw

RE: [RFC 02/20] vfio: Add device class for /dev/vfio/devices

Quoted 31 lines

From: Alex Williamson <[email protected]>
Sent: Wednesday, September 22, 2021 3:56 AM

Quoted 20 lines · On Sun, 19 Sep 2021 14:38:30 +0800 Liu Yi L <[email protected]> wrote:

This patch introduces a new interface (/dev/vfio/devices/$DEVICE) for userspace to directly open a vfio device w/o relying on container/group (/dev/vfio/$GROUP). Anything related to group is now hidden behind iommufd (more specifically in iommu core by this RFC) in a device-centric manner.

In case a device is exposed in both legacy and new interfaces (see next patch for how to decide it), this patch also ensures that when the device is already opened via one interface then the other one must be blocked.

---
 drivers/vfio/vfio.c  | 228 +++++++++++++++++++++++++++++++++++++++----
 include/linux/vfio.h |   2 +
 2 files changed, 213 insertions(+), 17 deletions(-)
drivers/vfio/vfio.c +0 -0
index 02cc51ce6891..84436d7abedd 100644

...

Quoted 4 lines
drivers/vfio/vfio.c +0 -0
@@ -2295,6 +2436,52 @@ static struct miscdevice vfio_dev = {
 	.mode = S_IRUGO | S_IWUGO,
 };
 
Quoted 14 lines

drivers/vfio/vfio.c static struct miscdevice vfio_dev = { · line 2442

Quoted 4 lines
drivers/vfio/vfio.c +4 -0
+static char *vfio_device_devnode(struct device *dev, umode_t *mode)
+{
+	return kasprintf(GFP_KERNEL, "vfio/devices/%s", dev_name(dev));
+}

dev_name() doesn't provide us with any uniqueness guarantees, so this could potentially generate naming conflicts. The similar scheme for devices within an iommu group appends an instance number if a conflict occurs, but that solution doesn't work here where the name isn't just a link to the actual device. Devices within an iommu group are also likely associated within a bus_type, so the potential for conflict is pretty negligible, that's not the case as vfio is adopted for new device types. Thanks,

This is also our concern. Thanks for confirming it. Appreciate if you can help think out some better alternative to deal with it.

Thanks
Kevin

#25 lore raw

Re: [RFC 02/20] vfio: Add device class for /dev/vfio/devices

In reply to Liu Yi L
Quoted 11 lines · Liu Yi L

This patch introduces a new interface (/dev/vfio/devices/$DEVICE) for userspace to directly open a vfio device w/o relying on container/group (/dev/vfio/$GROUP). Anything related to group is now hidden behind iommufd (more specifically in iommu core by this RFC) in a device-centric manner.

In case a device is exposed in both legacy and new interfaces (see next patch for how to decide it), this patch also ensures that when the device is already opened via one interface then the other one must be blocked.

[snip]

drivers/vfio/vfio.c static const struct file_operations vfio_group_fops = { · line 1683

Quoted 3 lines
drivers/vfio/vfio.c +3 -0
+static bool vfio_device_in_container(struct vfio_device *device)
+{
+	return !!(device->group && device->group->container);

You don't need !! here. && is already a logical operation, so returns a valid bool.

Quoted 32 lines
drivers/vfio/vfio.c +13 -1
+}
+
 static int vfio_device_fops_release(struct inode *inode, struct file *filep)
 {
 	struct vfio_device *device = filep->private_data;
@@ -1560,7 +1691,16 @@ static int vfio_device_fops_release(struct inode *inode, struct file *filep)
 
 	module_put(device->dev->driver->owner);
 
-	vfio_group_try_dissolve_container(device->group);
+	if (vfio_device_in_container(device)) {
+		vfio_group_try_dissolve_container(device->group);
+	} else {
+		atomic_dec(&device->opened);
+		if (device->group) {
+			mutex_lock(&device->group->opened_lock);
+			device->group->opened--;
+			mutex_unlock(&device->group->opened_lock);
+		}
+	}
 
 	vfio_device_put(device);
 
@@ -1613,6 +1753,7 @@ static int vfio_device_fops_mmap(struct file *filep, struct vm_area_struct *vma)
 
 static const struct file_operations vfio_device_fops = {
 	.owner		= THIS_MODULE,
+	.open		= vfio_device_fops_open,
 	.release	= vfio_device_fops_release,
 	.read		= vfio_device_fops_read,
 	.write		= vfio_device_fops_write,

drivers/vfio/vfio.c static struct miscdevice vfio_dev = { · line 2441

Quoted 6 lines
drivers/vfio/vfio.c +3 -0
@@ -2295,6 +2436,52 @@ static struct miscdevice vfio_dev = {
 	.mode = S_IRUGO | S_IWUGO,
 };
 
+static char *vfio_device_devnode(struct device *dev, umode_t *mode)
+{
+	return kasprintf(GFP_KERNEL, "vfio/devices/%s", dev_name(dev));

Others have pointed out some problems with the use of dev_name() here. I'll add that I think you'll make things much easier if instead of using one huge "devices" subdir, you use a separate subdir for each vfio sub-driver (so, one for PCI, one for each type of mdev, one for platform, etc.). That should make avoiding name conflicts a lot simpler.

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
#26 lore raw

Re: [RFC 02/20] vfio: Add device class for /dev/vfio/devices

Quoted 56 lines · On Wed, 29 Sep 2021 12:08:59 +1000 David Gibson <[email protected]>…
Quoted 11 lines · Liu Yi L

This patch introduces a new interface (/dev/vfio/devices/$DEVICE) for userspace to directly open a vfio device w/o relying on container/group (/dev/vfio/$GROUP). Anything related to group is now hidden behind iommufd (more specifically in iommu core by this RFC) in a device-centric manner.

In case a device is exposed in both legacy and new interfaces (see next patch for how to decide it), this patch also ensures that when the device is already opened via one interface then the other one must be blocked.

[snip]

drivers/vfio/vfio.c static const struct file_operations vfio_group_fops = { · line 1683

Quoted 3 lines
drivers/vfio/vfio.c +3 -0
+static bool vfio_device_in_container(struct vfio_device *device)
+{
+	return !!(device->group && device->group->container);  

You don't need !! here. && is already a logical operation, so returns a valid bool.

Quoted 35 lines
drivers/vfio/vfio.c +13 -1
+}
+
 static int vfio_device_fops_release(struct inode *inode, struct file *filep)
 {
 	struct vfio_device *device = filep->private_data;
@@ -1560,7 +1691,16 @@ static int vfio_device_fops_release(struct inode *inode, struct file *filep)
 
 	module_put(device->dev->driver->owner);
 
-	vfio_group_try_dissolve_container(device->group);
+	if (vfio_device_in_container(device)) {
+		vfio_group_try_dissolve_container(device->group);
+	} else {
+		atomic_dec(&device->opened);
+		if (device->group) {
+			mutex_lock(&device->group->opened_lock);
+			device->group->opened--;
+			mutex_unlock(&device->group->opened_lock);
+		}
+	}
 
 	vfio_device_put(device);
 
@@ -1613,6 +1753,7 @@ static int vfio_device_fops_mmap(struct file *filep, struct vm_area_struct *vma)
 
 static const struct file_operations vfio_device_fops = {
 	.owner		= THIS_MODULE,
+	.open		= vfio_device_fops_open,
 	.release	= vfio_device_fops_release,
 	.read		= vfio_device_fops_read,
 	.write		= vfio_device_fops_write,
@@ -2295,6 +2436,52 @@ static struct miscdevice vfio_dev = {
 	.mode = S_IRUGO | S_IWUGO,
 };
 
Quoted 9 lines

drivers/vfio/vfio.c static struct miscdevice vfio_dev = { · line 2441

Quoted 3 lines
drivers/vfio/vfio.c +3 -0
+static char *vfio_device_devnode(struct device *dev, umode_t *mode)
+{
+	return kasprintf(GFP_KERNEL, "vfio/devices/%s", dev_name(dev));  

Others have pointed out some problems with the use of dev_name() here. I'll add that I think you'll make things much easier if instead of using one huge "devices" subdir, you use a separate subdir for each vfio sub-driver (so, one for PCI, one for each type of mdev, one for platform, etc.). That should make avoiding name conflicts a lot simpler.

It seems like this is unnecessary if we use the vfioX naming approach. Conflicts are trivial to ignore if we don't involve dev_name() and looking for the correct major:minor chardev in the correct subdirectory seems like a hassle for userspace. Thanks,

Alex

#27 lore raw

Re: [RFC 02/20] vfio: Add device class for /dev/vfio/devices

Quoted 69 lines · Alex Williamson
Quoted 56 lines · On Wed, 29 Sep 2021 12:08:59 +1000 David Gibson <[email protected]>…
Quoted 11 lines · Liu Yi L

This patch introduces a new interface (/dev/vfio/devices/$DEVICE) for userspace to directly open a vfio device w/o relying on container/group (/dev/vfio/$GROUP). Anything related to group is now hidden behind iommufd (more specifically in iommu core by this RFC) in a device-centric manner.

In case a device is exposed in both legacy and new interfaces (see next patch for how to decide it), this patch also ensures that when the device is already opened via one interface then the other one must be blocked.

[snip]

drivers/vfio/vfio.c static const struct file_operations vfio_group_fops = { · line 1683

Quoted 3 lines
drivers/vfio/vfio.c +3 -0
+static bool vfio_device_in_container(struct vfio_device *device)
+{
+	return !!(device->group && device->group->container);  

You don't need !! here. && is already a logical operation, so returns a valid bool.

Quoted 35 lines
drivers/vfio/vfio.c +13 -1
+}
+
 static int vfio_device_fops_release(struct inode *inode, struct file *filep)
 {
 	struct vfio_device *device = filep->private_data;
@@ -1560,7 +1691,16 @@ static int vfio_device_fops_release(struct inode *inode, struct file *filep)
 
 	module_put(device->dev->driver->owner);
 
-	vfio_group_try_dissolve_container(device->group);
+	if (vfio_device_in_container(device)) {
+		vfio_group_try_dissolve_container(device->group);
+	} else {
+		atomic_dec(&device->opened);
+		if (device->group) {
+			mutex_lock(&device->group->opened_lock);
+			device->group->opened--;
+			mutex_unlock(&device->group->opened_lock);
+		}
+	}
 
 	vfio_device_put(device);
 
@@ -1613,6 +1753,7 @@ static int vfio_device_fops_mmap(struct file *filep, struct vm_area_struct *vma)
 
 static const struct file_operations vfio_device_fops = {
 	.owner		= THIS_MODULE,
+	.open		= vfio_device_fops_open,
 	.release	= vfio_device_fops_release,
 	.read		= vfio_device_fops_read,
 	.write		= vfio_device_fops_write,
@@ -2295,6 +2436,52 @@ static struct miscdevice vfio_dev = {
 	.mode = S_IRUGO | S_IWUGO,
 };
 
Quoted 9 lines

drivers/vfio/vfio.c static struct miscdevice vfio_dev = { · line 2441

Quoted 3 lines
drivers/vfio/vfio.c +3 -0
+static char *vfio_device_devnode(struct device *dev, umode_t *mode)
+{
+	return kasprintf(GFP_KERNEL, "vfio/devices/%s", dev_name(dev));  

Others have pointed out some problems with the use of dev_name() here. I'll add that I think you'll make things much easier if instead of using one huge "devices" subdir, you use a separate subdir for each vfio sub-driver (so, one for PCI, one for each type of mdev, one for platform, etc.). That should make avoiding name conflicts a lot simpler.

Quoted 4 lines

It seems like this is unnecessary if we use the vfioX naming approach. Conflicts are trivial to ignore if we don't involve dev_name() and looking for the correct major:minor chardev in the correct subdirectory seems like a hassle for userspace. Thanks,

Right.. it does sound like a hassle, but AFAICT that's *more* necessary with /dev/vfio/vfioXX than with /dev/vfio/pci/DDDD:BB:SS.F, since you have to look up a meaningful name in sysfs to find the right devnode.

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
author
#28 lore raw

RE: [RFC 02/20] vfio: Add device class for /dev/vfio/devices

In reply to David Gibson
Quoted 15 lines

From: David Gibson <[email protected]>
Sent: Wednesday, September 29, 2021 10:09 AM

Quoted 11 lines · Liu Yi L

This patch introduces a new interface (/dev/vfio/devices/$DEVICE) for userspace to directly open a vfio device w/o relying on container/group (/dev/vfio/$GROUP). Anything related to group is now hidden behind iommufd (more specifically in iommu core by this RFC) in a device-centric manner.

In case a device is exposed in both legacy and new interfaces (see next patch for how to decide it), this patch also ensures that when the device is already opened via one interface then the other one must be blocked.

Quoted 8 lines

[snip]

drivers/vfio/vfio.c static const struct file_operations vfio_group_fops = { · line 1683

Quoted 3 lines
drivers/vfio/vfio.c +3 -0
+static bool vfio_device_in_container(struct vfio_device *device)
+{
+	return !!(device->group && device->group->container);

You don't need !! here. && is already a logical operation, so returns a valid bool.

got it. thanks.

Regards,
Yi Liu

author
#29 lore raw

RFC 3/20 vfio: Add vfio_[un]register_device()

In reply to Liu Yi L

With /dev/vfio/devices introduced, now a vfio device driver has three options to expose its device to userspace:

a) only legacy group interface, for devices which haven't been moved to

    iommufd (e.g. platform devices, sw mdev, etc.);

b) both legacy group interface and new device-centric interface, for devices which supports iommufd but also wants to keep backward compatibility (e.g. pci devices in this RFC);

c) only new device-centric interface, for new devices which don't carry

    backward compatibility burden (e.g. hw mdev/subdev with pasid);

This patch introduces vfio_[un]register_device() helpers for the device drivers to specify the device exposure policy to vfio core. Hence the existing vfio_[un]register_group_dev() become the wrapper of the new helper functions. The new device-centric interface is described as 'nongroup' to differentiate from existing 'group' stuff.

TBD: this patch needs to rebase on top of below series from Christoph in next version.

"cleanup vfio iommu_group creation"

Legacy userspace continues to follow the legacy group interface.

Newer userspace can first try the new device-centric interface if the device is present under /dev/vfio/devices. Otherwise fall back to the group interface.

One open about how to organize the device nodes under /dev/vfio/devices/. This RFC adopts a simple policy by keeping a flat layout with mixed devname from all kinds of devices. The prerequisite of this model is that devnames from different bus types are unique formats:

	/dev/vfio/devices/0000:00:14.2 (pci)
	/dev/vfio/devices/PNP0103:00 (platform)
	/dev/vfio/devices/83b8f4f2-509f-382f-3c1e-e6bfe0fa1001 (mdev)

One alternative option is to arrange device nodes in sub-directories based on the device type. But doing so also adds one trouble to userspace. The current vfio uAPI is designed to have the user query device type via VFIO_DEVICE_GET_INFO after opening the device. With this option the user instead needs to figure out the device type before opening the device, to identify the sub-directory. Another tricky thing is that "pdev. vs. mdev" and "pci vs. platform vs. ccw,..." are orthogonal categorizations. Need more thoughts on whether both or just one category should be used to define the sub-directories.

---
 drivers/vfio/vfio.c  | 137 +++++++++++++++++++++++++++++++++++++++----
 include/linux/vfio.h |   9 +++
 2 files changed, 134 insertions(+), 12 deletions(-)
drivers/vfio/vfio.c +125 -12
index 84436d7abedd..1e87b25962f1 100644
@@ -51,6 +51,7 @@ static struct vfio {
 	struct cdev			device_cdev;
 	dev_t				device_devt;
 	struct mutex			device_lock;
+	struct list_head		device_list;
 	struct idr			device_idr;
 } vfio;
 
@@ -757,7 +758,7 @@ void vfio_init_group_dev(struct vfio_device *device, struct device *dev,
 }
 EXPORT_SYMBOL_GPL(vfio_init_group_dev);
 
-int vfio_register_group_dev(struct vfio_device *device)
+static int __vfio_register_group_dev(struct vfio_device *device)
 {
 	struct vfio_device *existing_device;
 	struct iommu_group *iommu_group;
@@ -794,8 +795,13 @@ int vfio_register_group_dev(struct vfio_device *device)
 	/* Our reference on group is moved to the device */
 	device->group = group;
 
-	/* Refcounting can't start until the driver calls register */
-	refcount_set(&device->refcount, 1);
+	/*
+	 * Refcounting can't start until the driver call register. Don’t
+	 * start twice when the device is exposed in both group and nongroup
+	 * interfaces.
+	 */
+	if (!refcount_read(&device->refcount))
+		refcount_set(&device->refcount, 1);
 
 	mutex_lock(&group->device_lock);
 	list_add(&device->group_next, &group->device_list);
@@ -804,7 +810,78 @@ int vfio_register_group_dev(struct vfio_device *device)
 
 	return 0;
 }
-EXPORT_SYMBOL_GPL(vfio_register_group_dev);
+
+static int __vfio_register_nongroup_dev(struct vfio_device *device)
+{
+	struct vfio_device *existing_device;
+	struct device *dev;
+	int ret = 0, minor;
+
+	mutex_lock(&vfio.device_lock);
+	list_for_each_entry(existing_device, &vfio.device_list, vfio_next) {
+		if (existing_device == device) {
+			ret = -EBUSY;
+			goto out_unlock;
+		}
+	}
+
+	minor = idr_alloc(&vfio.device_idr, device, 0, MINORMASK + 1, GFP_KERNEL);
+	pr_debug("%s - mnior: %d\n", __func__, minor);
+	if (minor < 0) {
+		ret = minor;
+		goto out_unlock;
+	}
+
+	dev = device_create(vfio.device_class, NULL,
+			    MKDEV(MAJOR(vfio.device_devt), minor),
+			    device, "%s", dev_name(device->dev));
+	if (IS_ERR(dev)) {
+		idr_remove(&vfio.device_idr, minor);
+		ret = PTR_ERR(dev);
+		goto out_unlock;
+	}
+
+	/*
+	 * Refcounting can't start until the driver call register. Don’t
+	 * start twice when the device is exposed in both group and nongroup
+	 * interfaces.
+	 */
+	if (!refcount_read(&device->refcount))
+		refcount_set(&device->refcount, 1);
+
+	device->minor = minor;
+	list_add(&device->vfio_next, &vfio.device_list);
+	dev_info(device->dev, "Creates Device interface successfully!\n");
+out_unlock:
+	mutex_unlock(&vfio.device_lock);
+	return ret;
+}
+
+int vfio_register_device(struct vfio_device *device, u32 flags)
+{
+	int ret = -EINVAL;
+
+	device->minor = -1;
+	device->group = NULL;
+	atomic_set(&device->opened, 0);
+
+	if (flags & ~(VFIO_DEVNODE_GROUP | VFIO_DEVNODE_NONGROUP))
+		return ret;
+
+	if (flags & VFIO_DEVNODE_GROUP) {
+		ret = __vfio_register_group_dev(device);
+		if (ret)
+			return ret;
+	}
+
+	if (flags & VFIO_DEVNODE_NONGROUP) {
+		ret = __vfio_register_nongroup_dev(device);
+		if (ret && device->group)
+			vfio_unregister_device(device);
+	}
+	return ret;
+}
+EXPORT_SYMBOL_GPL(vfio_register_device);
 
 /**
  * Get a reference to the vfio_device for a device.  Even if the
@@ -861,13 +938,14 @@ static struct vfio_device *vfio_device_get_from_name(struct vfio_group *group,
 /*
  * Decrement the device reference count and wait for the device to be
  * removed.  Open file descriptors for the device... */
-void vfio_unregister_group_dev(struct vfio_device *device)
+void vfio_unregister_device(struct vfio_device *device)
 {
 	struct vfio_group *group = device->group;
 	struct vfio_unbound_dev *unbound;
 	unsigned int i = 0;
 	bool interrupted = false;
 	long rc;
+	int minor = device->minor;
 
 	/*
 	 * When the device is removed from the group, the group suddenly
@@ -878,14 +956,20 @@ void vfio_unregister_group_dev(struct vfio_device *device)
 	 * solve this, we track such devices on the unbound_list to bridge
 	 * the gap until they're fully unbound.
 	 */
-	unbound = kzalloc(sizeof(*unbound), GFP_KERNEL);
-	if (unbound) {
-		unbound->dev = device->dev;
-		mutex_lock(&group->unbound_lock);
-		list_add(&unbound->unbound_next, &group->unbound_list);
-		mutex_unlock(&group->unbound_lock);
+	if (group) {
+		/*
+		 * If caller hasn't called vfio_register_group_dev(), this
+		 * branch is not necessary.
+		 */
+		unbound = kzalloc(sizeof(*unbound), GFP_KERNEL);
+		if (unbound) {
+			unbound->dev = device->dev;
+			mutex_lock(&group->unbound_lock);
+			list_add(&unbound->unbound_next, &group->unbound_list);
+			mutex_unlock(&group->unbound_lock);
+		}
+		WARN_ON(!unbound);
 	}
-	WARN_ON(!unbound);
 
 	vfio_device_put(device);
 	rc = try_wait_for_completion(&device->comp);
@@ -910,6 +994,21 @@ void vfio_unregister_group_dev(struct vfio_device *device)
 		}
 	}
 
+	/* nongroup interface related cleanup */
+	if (minor >= 0) {
+		mutex_lock(&vfio.device_lock);
+		list_del(&device->vfio_next);
+		device->minor = -1;
+		device_destroy(vfio.device_class,
+			       MKDEV(MAJOR(vfio.device_devt), minor));
+		idr_remove(&vfio.device_idr, minor);
+		mutex_unlock(&vfio.device_lock);
+	}
+
+	/* No need go further if no group. */
+	if (!group)
+		return;
+
 	mutex_lock(&group->device_lock);
 	list_del(&device->group_next);
 	group->dev_counter--;
@@ -935,6 +1034,18 @@ void vfio_unregister_group_dev(struct vfio_device *device)
 	/* Matches the get in vfio_register_group_dev() */
 	vfio_group_put(group);
 }
+EXPORT_SYMBOL_GPL(vfio_unregister_device);
+
+int vfio_register_group_dev(struct vfio_device *device)
+{
+	return vfio_register_device(device, VFIO_DEVNODE_GROUP);
+}
+EXPORT_SYMBOL_GPL(vfio_register_group_dev);
+
+void vfio_unregister_group_dev(struct vfio_device *device)
+{
+	vfio_unregister_device(device);
+}
 EXPORT_SYMBOL_GPL(vfio_unregister_group_dev);
 
 /**
@@ -2447,6 +2558,7 @@ static int vfio_init_device_class(void)
 
 	mutex_init(&vfio.device_lock);
 	idr_init(&vfio.device_idr);
+	INIT_LIST_HEAD(&vfio.device_list);
 
 	/* /dev/vfio/devices/$DEVICE */
 	vfio.device_class = class_create(THIS_MODULE, "vfio-device");
@@ -2542,6 +2654,7 @@ static int __init vfio_init(void)
 static void __exit vfio_cleanup(void)
 {
 	WARN_ON(!list_empty(&vfio.group_list));
+	WARN_ON(!list_empty(&vfio.device_list));
 
 #ifdef CONFIG_VFIO_NOIOMMU
 	vfio_unregister_iommu_driver(&vfio_noiommu_ops);
include/linux/vfio.h +9 -0
index 4a5f3f99eab2..9448b751b663 100644
@@ -26,6 +26,7 @@ struct vfio_device {
 	struct list_head group_next;
 	int minor;
 	atomic_t opened;
+	struct list_head vfio_next;
 };
 
 /**
@@ -73,6 +74,14 @@ enum vfio_iommu_notify_type {
 	VFIO_IOMMU_CONTAINER_CLOSE = 0,
 };
 
+/* The device can be opened via VFIO_GROUP_GET_DEVICE_FD */
+#define VFIO_DEVNODE_GROUP	BIT(0)
+/* The device can be opened via /dev/sys/devices/${DEVICE} */
+#define VFIO_DEVNODE_NONGROUP	BIT(1)
+
+extern int vfio_register_device(struct vfio_device *device, u32 flags);
+extern void vfio_unregister_device(struct vfio_device *device);
+
 /**
  * struct vfio_iommu_driver_ops - VFIO IOMMU driver callbacks
  */
Signature · 6 lines
2.25.1

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

Re: [RFC 03/20] vfio: Add vfio_[un]register_device()

Quoted 4 lines · Liu Yi L

With /dev/vfio/devices introduced, now a vfio device driver has three options to expose its device to userspace:

a) only legacy group interface, for devices which haven't been moved to

Quoted 8 lines
    iommufd (e.g. platform devices, sw mdev, etc.);

b) both legacy group interface and new device-centric interface, for devices which supports iommufd but also wants to keep backward compatibility (e.g. pci devices in this RFC);

c) only new device-centric interface, for new devices which don't carry

    backward compatibility burden (e.g. hw mdev/subdev with pasid);

We shouldn't have 'b'? Where does it come from?

Quoted 5 lines

This patch introduces vfio_[un]register_device() helpers for the device drivers to specify the device exposure policy to vfio core. Hence the existing vfio_[un]register_group_dev() become the wrapper of the new helper functions. The new device-centric interface is described as 'nongroup' to differentiate from existing 'group' stuff.

Detect what the driver supports based on the ops it declares. There should be a function provided through the ops for the driver to bind to the iommufd.

Quoted 4 lines

One open about how to organize the device nodes under /dev/vfio/devices/.
This RFC adopts a simple policy by keeping a flat layout with mixed devname from all kinds of devices. The prerequisite of this model is that devnames from different bus types are unique formats:

This isn't reliable, the devname should just be vfio0, vfio1, etc

The userspace can learn the correct major/minor by inspecting the sysfs.

This whole concept should disappear into the prior patch that adds the struct device in the first place, and I think most of the code here can be deleted once the struct device is used properly.

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

maintainer
#31 lore raw

RE: [RFC 03/20] vfio: Add vfio_[un]register_device()

Quoted 10 lines

From: Jason Gunthorpe <[email protected]>
Sent: Wednesday, September 22, 2021 12:01 AM

Quoted 6 lines · Liu Yi L

With /dev/vfio/devices introduced, now a vfio device driver has three options to expose its device to userspace:

a) only legacy group interface, for devices which haven't been moved to

    iommufd (e.g. platform devices, sw mdev, etc.);
Quoted 8 lines
Quoted 6 lines

b) both legacy group interface and new device-centric interface, for devices which supports iommufd but also wants to keep backward compatibility (e.g. pci devices in this RFC);

c) only new device-centric interface, for new devices which don't carry

    backward compatibility burden (e.g. hw mdev/subdev with pasid);

We shouldn't have 'b'? Where does it come from?

a vfio-pci device can be opened via the existing group interface. if no b) it means legacy vfio userspace can never use vfio-pci device any more once the latter is moved to iommufd.

Thanks
Kevin

maintainer
#32 lore raw

Re: [RFC 03/20] vfio: Add vfio_[un]register_device()

Quoted 14 lines · Kevin
Quoted 14 lines

From: Jason Gunthorpe <[email protected]>
Sent: Wednesday, September 22, 2021 12:01 AM

Quoted 10 lines · Liu Yi L

With /dev/vfio/devices introduced, now a vfio device driver has three options to expose its device to userspace:

a) only legacy group interface, for devices which haven't been moved to

    iommufd (e.g. platform devices, sw mdev, etc.);

b) both legacy group interface and new device-centric interface, for devices which supports iommufd but also wants to keep backward compatibility (e.g. pci devices in this RFC);

Quoted 8 lines
Quoted 4 lines
Quoted 2 lines

c) only new device-centric interface, for new devices which don't carry

    backward compatibility burden (e.g. hw mdev/subdev with pasid);

We shouldn't have 'b'? Where does it come from?

a vfio-pci device can be opened via the existing group interface. if no b) it means legacy vfio userspace can never use vfio-pci device any more once the latter is moved to iommufd.

Sorry, I think I ment a, which I guess you will say is SW mdev devices

But even so, I think the way forward here is to still always expose the device /dev/vfio/devices/X and some devices may not allow iommufd usage initially.

Providing an ioctl to bind to a normal VFIO container or group might allow a reasonable fallback in userspace..

Jason

maintainer
#33 lore raw

RE: [RFC 03/20] vfio: Add vfio_[un]register_device()

Quoted 22 lines

From: Jason Gunthorpe <[email protected]>
Sent: Wednesday, September 22, 2021 8:54 AM

Quoted 8 lines · Kevin
Quoted 8 lines

From: Jason Gunthorpe <[email protected]>
Sent: Wednesday, September 22, 2021 12:01 AM

Quoted 4 lines · Liu Yi L

With /dev/vfio/devices introduced, now a vfio device driver has three options to expose its device to userspace:

a) only legacy group interface, for devices which haven't been moved

to

Quoted 9 lines
Quoted 9 lines
Quoted 8 lines
    iommufd (e.g. platform devices, sw mdev, etc.);

b) both legacy group interface and new device-centric interface, for devices which supports iommufd but also wants to keep backward compatibility (e.g. pci devices in this RFC);

c) only new device-centric interface, for new devices which don't carry

    backward compatibility burden (e.g. hw mdev/subdev with pasid);
Quoted 7 lines
Quoted 5 lines
Quoted 1 line

We shouldn't have 'b'? Where does it come from?

a vfio-pci device can be opened via the existing group interface. if no b) it means legacy vfio userspace can never use vfio-pci device any more once the latter is moved to iommufd.

Sorry, I think I ment a, which I guess you will say is SW mdev devices

We listed a) here in case we don't want to move all vfio device types to use iommufd in one breath. It's supposed to be a type valid only in this transition phase. In the end only b) and c) are valid.

Quoted 8 lines

But even so, I think the way forward here is to still always expose the device /dev/vfio/devices/X and some devices may not allow iommufd usage initially.

Providing an ioctl to bind to a normal VFIO container or group might allow a reasonable fallback in userspace..

but doesn't a new ioctl still imply breaking existing vfio userspace?

Thanks
Kevin

maintainer
#34 lore raw

RE: [RFC 03/20] vfio: Add vfio_[un]register_device()

In reply to Jason Gunthorpe
Quoted 28 lines

From: Jason Gunthorpe <[email protected]>
Sent: Wednesday, September 22, 2021 8:54 AM

Quoted 8 lines · Kevin
Quoted 8 lines

From: Jason Gunthorpe <[email protected]>
Sent: Wednesday, September 22, 2021 12:01 AM

Quoted 4 lines · Liu Yi L

With /dev/vfio/devices introduced, now a vfio device driver has three options to expose its device to userspace:

a) only legacy group interface, for devices which haven't been moved

to

Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
    iommufd (e.g. platform devices, sw mdev, etc.);

b) both legacy group interface and new device-centric interface, for devices which supports iommufd but also wants to keep backward compatibility (e.g. pci devices in this RFC);

Quoted 8 lines
Quoted 4 lines
Quoted 2 lines

c) only new device-centric interface, for new devices which don't carry

    backward compatibility burden (e.g. hw mdev/subdev with pasid);

We shouldn't have 'b'? Where does it come from?

a vfio-pci device can be opened via the existing group interface. if no b) it means legacy vfio userspace can never use vfio-pci device any more once the latter is moved to iommufd.

Quoted 5 lines

Sorry, I think I ment a, which I guess you will say is SW mdev devices

But even so, I think the way forward here is to still always expose the device /dev/vfio/devices/X and some devices may not allow iommufd usage initially.

After another thought this should work. Following your comments in other places, we'll move the handling of BIND_IOMMUFD to vfio core which then invoke .bind_iommufd() from the driver. For devices which don't allow iommufd now, the callback is null thus an error is returned.

This leaves the userspace in a try-and-fail mode. It first opens the device fd and iommufd, and then try to connect the two together. If failed then fallback to the legacy group interface.

Then we don't need a) at all. and we can even avoid introducing new vfio_[un]register_device() at this point. Just leverage existing vfio_[un]register_group_dev() to cover b). new helpers can be introduced later when c) is supported.

Quoted 4 lines

Providing an ioctl to bind to a normal VFIO container or group might allow a reasonable fallback in userspace..

I didn't get this point though. An error in binding already allows the user to fall back to the group path. Why do we need introduce another ioctl to explicitly bind to container via the nongroup interface?

Thanks
Kevin

maintainer
#35 lore raw

Re: [RFC 03/20] vfio: Add vfio_[un]register_device()

Quoted 6 lines · Kevin
Quoted 2 lines

Providing an ioctl to bind to a normal VFIO container or group might allow a reasonable fallback in userspace..

I didn't get this point though. An error in binding already allows the user to fall back to the group path. Why do we need introduce another ioctl to explicitly bind to container via the nongroup interface?

New userspace still needs a fallback path if it hits the 'try and fail'. Keeping the device FD open and just using a different ioctl to bind to a container/group FD, which new userspace can then obtain as a fallback, might be OK.

Hard to see without going through the qemu parts, so maybe just keep it in mind

Jason

maintainer
#36 lore raw

RE: [RFC 03/20] vfio: Add vfio_[un]register_device()

Quoted 12 lines

From: Jason Gunthorpe <[email protected]>
Sent: Wednesday, September 22, 2021 8:23 PM

Quoted 6 lines · Kevin
Quoted 2 lines

Providing an ioctl to bind to a normal VFIO container or group might allow a reasonable fallback in userspace..

I didn't get this point though. An error in binding already allows the user to fall back to the group path. Why do we need introduce another ioctl to explicitly bind to container via the nongroup interface?

Quoted 8 lines

New userspace still needs a fallback path if it hits the 'try and fail'. Keeping the device FD open and just using a different ioctl to bind to a container/group FD, which new userspace can then obtain as a fallback, might be OK.

Hard to see without going through the qemu parts, so maybe just keep it in mind

sure. will figure it out when working on the qemu part.
_______________________________________________
iommu mailing list [email protected]
https://lists.linuxfoundation.org/mailman/listinfo/iommu

#37 lore raw

Re: [RFC 03/20] vfio: Add vfio_[un]register_device()

In reply to Jason Gunthorpe
Quoted 9 lines · On Wed, 22 Sep 2021 09:22:52 -0300 Jason Gunthorpe <[email protected]> wrote:
Quoted 6 lines · Kevin
Quoted 2 lines

Providing an ioctl to bind to a normal VFIO container or group might allow a reasonable fallback in userspace..

I didn't get this point though. An error in binding already allows the user to fall back to the group path. Why do we need introduce another ioctl to explicitly bind to container via the nongroup interface?

Quoted 7 lines

New userspace still needs a fallback path if it hits the 'try and fail'. Keeping the device FD open and just using a different ioctl to bind to a container/group FD, which new userspace can then obtain as a fallback, might be OK.

Hard to see without going through the qemu parts, so maybe just keep it in mind

If we assume that the container/group/device interface is essentially deprecated once we have iommufd, it doesn't make a lot of sense to me to tack on a container/device interface just so userspace can avoid reverting to the fully legacy interface.

But why would we create vfio device interface files at all if they can't work? I'm not really on board with creating a try-and-fail interface for a mechanism that cannot work for a given device. The existence of the device interface should indicate that it's supported. Thanks,

Alex

maintainer
#38 lore raw

RE: [RFC 03/20] vfio: Add vfio_[un]register_device()

Quoted 28 lines

From: Alex Williamson <[email protected]>
Sent: Thursday, September 23, 2021 4:11 AM

Quoted 9 lines · On Wed, 22 Sep 2021 09:22:52 -0300 Jason Gunthorpe <[email protected]> wrote:
Quoted 6 lines · Kevin
Quoted 2 lines

Providing an ioctl to bind to a normal VFIO container or group might allow a reasonable fallback in userspace..

I didn't get this point though. An error in binding already allows the user to fall back to the group path. Why do we need introduce another ioctl to explicitly bind to container via the nongroup interface?

Quoted 7 lines

New userspace still needs a fallback path if it hits the 'try and fail'. Keeping the device FD open and just using a different ioctl to bind to a container/group FD, which new userspace can then obtain as a fallback, might be OK.

Hard to see without going through the qemu parts, so maybe just keep it in mind

If we assume that the container/group/device interface is essentially deprecated once we have iommufd, it doesn't make a lot of sense to me to tack on a container/device interface just so userspace can avoid reverting to the fully legacy interface.

Quoted 6 lines

But why would we create vfio device interface files at all if they can't work? I'm not really on board with creating a try-and-fail interface for a mechanism that cannot work for a given device. The existence of the device interface should indicate that it's supported. Thanks,

Now it's a try-and-fail model even for devices which support iommufd. Per Jason's suggestion, a device is always opened with a parked fops which supports only bind. Binding serves as the contract for handling exclusive ownership on a device and switching to normal fops if succeed. So the user has to try-and-fail in case multiple threads attempt to open a same device. Device which doesn't support iommufd is not different, except binding request 100% fails (due to missing .bind_iommufd in kernel driver).

Thanks
Kevin

#39 lore raw

Re: [RFC 03/20] vfio: Add vfio_[un]register_device()

Quoted 35 lines · Kevin
Quoted 28 lines

From: Alex Williamson <[email protected]>
Sent: Thursday, September 23, 2021 4:11 AM

Quoted 9 lines · On Wed, 22 Sep 2021 09:22:52 -0300 Jason Gunthorpe <[email protected]> wrote:
Quoted 6 lines · Kevin
Quoted 2 lines

Providing an ioctl to bind to a normal VFIO container or group might allow a reasonable fallback in userspace..

I didn't get this point though. An error in binding already allows the user to fall back to the group path. Why do we need introduce another ioctl to explicitly bind to container via the nongroup interface?

Quoted 7 lines

New userspace still needs a fallback path if it hits the 'try and fail'. Keeping the device FD open and just using a different ioctl to bind to a container/group FD, which new userspace can then obtain as a fallback, might be OK.

Hard to see without going through the qemu parts, so maybe just keep it in mind

If we assume that the container/group/device interface is essentially deprecated once we have iommufd, it doesn't make a lot of sense to me to tack on a container/device interface just so userspace can avoid reverting to the fully legacy interface.

Quoted 6 lines

But why would we create vfio device interface files at all if they can't work? I'm not really on board with creating a try-and-fail interface for a mechanism that cannot work for a given device. The existence of the device interface should indicate that it's supported. Thanks,

Quoted 8 lines

Now it's a try-and-fail model even for devices which support iommufd. Per Jason's suggestion, a device is always opened with a parked fops which supports only bind. Binding serves as the contract for handling exclusive ownership on a device and switching to normal fops if succeed. So the user has to try-and-fail in case multiple threads attempt to open a same device. Device which doesn't support iommufd is not different, except binding request 100% fails (due to missing .bind_iommufd in kernel driver).

That's a rather important difference. I don't really see how that's comparable to the mutually exclusive nature of the legacy vs device interface. We're not going to present a vfio device interface for SW mdevs that can't participate in iommufd, right? Thanks,

Alex

maintainer
#40 lore raw

RE: [RFC 03/20] vfio: Add vfio_[un]register_device()

Quoted 52 lines

From: Alex Williamson <[email protected]>
Sent: Thursday, September 23, 2021 6:45 AM

Quoted 6 lines · Kevin
Quoted 6 lines

From: Alex Williamson <[email protected]>
Sent: Thursday, September 23, 2021 4:11 AM

On Wed, 22 Sep 2021 09:22:52 -0300
Jason Gunthorpe <[email protected]> wrote:

Quoted 7 lines
Quoted 7 lines
Quoted 7 lines · Kevin
Quoted 6 lines
Quoted 3 lines

Providing an ioctl to bind to a normal VFIO container or group might allow a reasonable fallback in userspace..

I didn't get this point though. An error in binding already allows the user to fall back to the group path. Why do we need introduce

another

Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 1 line

ioctl to explicitly bind to container via the nongroup interface?

New userspace still needs a fallback path if it hits the 'try and fail'. Keeping the device FD open and just using a different ioctl to bind to a container/group FD, which new userspace can then obtain as

a

Quoted 17 lines
Quoted 10 lines
Quoted 4 lines

fallback, might be OK.

Hard to see without going through the qemu parts, so maybe just keep it in mind

If we assume that the container/group/device interface is essentially deprecated once we have iommufd, it doesn't make a lot of sense to me to tack on a container/device interface just so userspace can avoid reverting to the fully legacy interface.

Quoted 6 lines

But why would we create vfio device interface files at all if they can't work? I'm not really on board with creating a try-and-fail interface for a mechanism that cannot work for a given device. The existence of the device interface should indicate that it's supported. Thanks,

Quoted 8 lines

Now it's a try-and-fail model even for devices which support iommufd. Per Jason's suggestion, a device is always opened with a parked fops which supports only bind. Binding serves as the contract for handling exclusive ownership on a device and switching to normal fops if succeed. So the user has to try-and-fail in case multiple threads attempt to open a same device. Device which doesn't support iommufd is not different, except binding request 100% fails (due to missing .bind_iommufd in kernel driver).

Quoted 2 lines

That's a rather important difference. I don't really see how that's comparable to the mutually exclusive nature of the legacy vs device

I didn't get the 'comparable' part. Can you elaborate?

Quoted 3 lines

interface. We're not going to present a vfio device interface for SW mdevs that can't participate in iommufd, right? Thanks,

Did you see any problem if exposing sw mdev now? Following above explanation the try-and-fail model should still work...

btw I realized another related piece regarding to the new layout that Jason suggested, which have sys device node include a link to the vfio devnode:

/sys/bus/pci/devices/DDDD:BB:DD.F/vfio/vfioX/dev

This for sure requires specific vfio driver support to get the link established. if we only do it for vfio-pci in the start, then for other devices which don't support iommufd there is no way for the user to identify the corresponding vfio devnode even it's still exposed. Then try-and-fail model may not even been reached for those devices.

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

maintainer
#41 lore raw

Re: [RFC 03/20] vfio: Add vfio_[un]register_device()

Quoted 10 lines · Kevin
Quoted 1 line

From: Alex Williamson <[email protected]>

btw I realized another related piece regarding to the new layout that Jason suggested, which have sys device node include a link to the vfio devnode:

/sys/bus/pci/devices/DDDD:BB:DD.F/vfio/vfioX/dev

This for sure requires specific vfio driver support to get the link established.

It doesn't. Just set the parent device of the vfio_device's struct device to the physical struct device that vfio is already tracking - ie the struct device providing the IOMMU. The driver core takes care of everything else.

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

maintainer
#42 lore raw

RE: [RFC 03/20] vfio: Add vfio_[un]register_device()

Quoted 12 lines

From: Jason Gunthorpe <[email protected]>
Sent: Thursday, September 23, 2021 7:52 AM

Quoted 8 lines · Kevin
Quoted 1 line

From: Alex Williamson <[email protected]>

btw I realized another related piece regarding to the new layout that Jason suggested, which have sys device node include a link to the vfio devnode:

/sys/bus/pci/devices/DDDD:BB:DD.F/vfio/vfioX/dev

Quoted 8 lines
Quoted 2 lines

This for sure requires specific vfio driver support to get the link established.

It doesn't. Just set the parent device of the vfio_device's struct device to the physical struct device that vfio is already tracking - ie the struct device providing the IOMMU. The driver core takes care of everything else.

Thanks for correction. So it's still the same try-and-fail model for both devices which support iommufd and which do not.

maintainer
#43 lore raw

Re: [RFC 03/20] vfio: Add vfio_[un]register_device()

In reply to Alex Williamson
Quoted 4 lines · Alex Williamson

But why would we create vfio device interface files at all if they can't work? I'm not really on board with creating a try-and-fail interface for a mechanism that cannot work for a given device. The existence of the device interface should indicate that it's supported.

I'm a little worried about adding a struct device to vfio_device and then making it optional.. That is a really weird situation.

I suppose you could create the sysfs presence in the struct device but not create a cdev.

However, if we ever want to use the device fd for something else, like querying the device driver capabilities or mode, (ie clean the driver_api thing wrongly placed in mdev sysfs for instance), we are blocked as the uAPI will be cdev == must support iommufd..

Jason

maintainer
#44 lore raw

RE: [RFC 03/20] vfio: Add vfio_[un]register_device()

In reply to Jason Gunthorpe
Quoted 10 lines

From: Jason Gunthorpe <[email protected]>
Sent: Wednesday, September 22, 2021 12:01 AM

Quoted 1 line

One open about how to organize the device nodes under

/dev/vfio/devices/.

Quoted 1 line

This RFC adopts a simple policy by keeping a flat layout with mixed

devname

Quoted 2 lines

from all kinds of devices. The prerequisite of this model is that devnames from different bus types are unique formats:

Quoted 9 lines

This isn't reliable, the devname should just be vfio0, vfio1, etc

The userspace can learn the correct major/minor by inspecting the sysfs.

This whole concept should disappear into the prior patch that adds the struct device in the first place, and I think most of the code here can be deleted once the struct device is used properly.

Can you help elaborate above flow? This is one area where we need more guidance.

When Qemu accepts an option "-device vfio-pci,host=DDDD:BB:DD.F", how does Qemu identify which vifo0/1/... is associated with the specified DDDD:BB:DD.F?

Thanks
Kevin

maintainer
#45 lore raw

Re: [RFC 03/20] vfio: Add vfio_[un]register_device()

Quoted 20 lines · Kevin
Quoted 10 lines

From: Jason Gunthorpe <[email protected]>
Sent: Wednesday, September 22, 2021 12:01 AM

Quoted 1 line

One open about how to organize the device nodes under

/dev/vfio/devices/.

Quoted 1 line

This RFC adopts a simple policy by keeping a flat layout with mixed

devname

Quoted 2 lines

from all kinds of devices. The prerequisite of this model is that devnames from different bus types are unique formats:

Quoted 9 lines

This isn't reliable, the devname should just be vfio0, vfio1, etc

The userspace can learn the correct major/minor by inspecting the sysfs.

This whole concept should disappear into the prior patch that adds the struct device in the first place, and I think most of the code here can be deleted once the struct device is used properly.

Quoted 6 lines

Can you help elaborate above flow? This is one area where we need more guidance.

When Qemu accepts an option "-device vfio-pci,host=DDDD:BB:DD.F", how does Qemu identify which vifo0/1/... is associated with the specified DDDD:BB:DD.F?

When done properly in the kernel the file:

/sys/bus/pci/devices/DDDD:BB:DD.F/vfio/vfioX/dev

Will contain the major:minor of the VFIO device.

Userspace then opens the /dev/vfio/devices/vfioX and checks with fstat that the major:minor matches.

in the above pattern "pci" and "DDDD:BB:DD.FF" are the arguments passed to qemu.

You can look at this for some general over engineered code to handle opening from a sysfs handle like above:

https://github.com/linux-rdma/rdma-core/blob/master/util/open_cdev.c

Jason

maintainer
#46 lore raw

RE: [RFC 03/20] vfio: Add vfio_[un]register_device()

Quoted 32 lines

From: Jason Gunthorpe <[email protected]>
Sent: Wednesday, September 22, 2021 9:00 AM

Quoted 8 lines · Kevin
Quoted 8 lines

From: Jason Gunthorpe <[email protected]>
Sent: Wednesday, September 22, 2021 12:01 AM

Quoted 1 line

One open about how to organize the device nodes under

/dev/vfio/devices/.

Quoted 1 line

This RFC adopts a simple policy by keeping a flat layout with mixed

devname

Quoted 1 line

from all kinds of devices. The prerequisite of this model is that

devnames

Quoted 12 lines
Quoted 11 lines
Quoted 1 line

from different bus types are unique formats:

This isn't reliable, the devname should just be vfio0, vfio1, etc

The userspace can learn the correct major/minor by inspecting the sysfs.

This whole concept should disappear into the prior patch that adds the struct device in the first place, and I think most of the code here can be deleted once the struct device is used properly.

Quoted 6 lines

Can you help elaborate above flow? This is one area where we need more guidance.

When Qemu accepts an option "-device vfio-pci,host=DDDD:BB:DD.F", how does Qemu identify which vifo0/1/... is associated with the specified DDDD:BB:DD.F?

Quoted 8 lines

When done properly in the kernel the file:

/sys/bus/pci/devices/DDDD:BB:DD.F/vfio/vfioX/dev

Will contain the major:minor of the VFIO device.

Userspace then opens the /dev/vfio/devices/vfioX and checks with fstat that the major:minor matches.

ah, that's the trick.

Quoted 9 lines

in the above pattern "pci" and "DDDD:BB:DD.FF" are the arguments passed to qemu.

You can look at this for some general over engineered code to handle opening from a sysfs handle like above:

https://github.com/linux-rdma/rdma-core/blob/master/util/open_cdev.c

will check. Thanks for suggestion.

#47 lore raw

Re: [RFC 03/20] vfio: Add vfio_[un]register_device()

In reply to Jason Gunthorpe

Hi,

Quoted 29 lines · Jason Gunthorpe
Quoted 18 lines · Kevin
Quoted 9 lines

From: Jason Gunthorpe <[email protected]>
Sent: Wednesday, September 22, 2021 12:01 AM

Quoted 1 line

One open about how to organize the device nodes under

/dev/vfio/devices/.

Quoted 1 line

This RFC adopts a simple policy by keeping a flat layout with mixed

devname

Quoted 2 lines

from all kinds of devices. The prerequisite of this model is that devnames from different bus types are unique formats:

Quoted 9 lines

This isn't reliable, the devname should just be vfio0, vfio1, etc

The userspace can learn the correct major/minor by inspecting the sysfs.

This whole concept should disappear into the prior patch that adds the struct device in the first place, and I think most of the code here can be deleted once the struct device is used properly.

Quoted 6 lines

Can you help elaborate above flow? This is one area where we need more guidance.

When Qemu accepts an option "-device vfio-pci,host=DDDD:BB:DD.F", how does Qemu identify which vifo0/1/... is associated with the specified DDDD:BB:DD.F?

When done properly in the kernel the file:

/sys/bus/pci/devices/DDDD:BB:DD.F/vfio/vfioX/dev

Quoted 7 lines

Will contain the major:minor of the VFIO device.

Userspace then opens the /dev/vfio/devices/vfioX and checks with fstat that the major:minor matches.

in the above pattern "pci" and "DDDD:BB:DD.FF" are the arguments passed to qemu.

I guess this would be the same for platform devices, for instance /sys/bus/platform/devices/AMDI8001:01/vfio/vfioX/dev, right?

Thanks

Eric

Quoted 8 lines

You can look at this for some general over engineered code to handle opening from a sysfs handle like above:

https://github.com/linux-rdma/rdma-core/blob/master/util/open_cdev.c

Jason

maintainer
#48 lore raw

Re: [RFC 03/20] vfio: Add vfio_[un]register_device()

Quoted 34 lines · Eric Auger

Hi,

Quoted 31 lines · Jason Gunthorpe
Quoted 18 lines · Kevin
Quoted 9 lines

From: Jason Gunthorpe <[email protected]>
Sent: Wednesday, September 22, 2021 12:01 AM

Quoted 1 line

One open about how to organize the device nodes under

/dev/vfio/devices/.

Quoted 1 line

This RFC adopts a simple policy by keeping a flat layout with mixed

devname

Quoted 2 lines

from all kinds of devices. The prerequisite of this model is that devnames from different bus types are unique formats:

Quoted 9 lines

This isn't reliable, the devname should just be vfio0, vfio1, etc

The userspace can learn the correct major/minor by inspecting the sysfs.

This whole concept should disappear into the prior patch that adds the struct device in the first place, and I think most of the code here can be deleted once the struct device is used properly.

Quoted 6 lines

Can you help elaborate above flow? This is one area where we need more guidance.

When Qemu accepts an option "-device vfio-pci,host=DDDD:BB:DD.F", how does Qemu identify which vifo0/1/... is associated with the specified DDDD:BB:DD.F?

When done properly in the kernel the file:

/sys/bus/pci/devices/DDDD:BB:DD.F/vfio/vfioX/dev

Will contain the major:minor of the VFIO device.

Quoted 7 lines
Quoted 5 lines

Userspace then opens the /dev/vfio/devices/vfioX and checks with fstat that the major:minor matches.

in the above pattern "pci" and "DDDD:BB:DD.FF" are the arguments passed to qemu.

I guess this would be the same for platform devices, for instance /sys/bus/platform/devices/AMDI8001:01/vfio/vfioX/dev, right?

Yes, it is the general driver core pattern for creating cdevs below a parent device

Jason

#49 lore raw

Re: [RFC 03/20] vfio: Add vfio_[un]register_device()

In reply to Jason Gunthorpe
Quoted 32 lines · Jason Gunthorpe
Quoted 20 lines · Kevin
Quoted 10 lines

From: Jason Gunthorpe <[email protected]>
Sent: Wednesday, September 22, 2021 12:01 AM

Quoted 1 line

One open about how to organize the device nodes under

/dev/vfio/devices/.

Quoted 1 line

This RFC adopts a simple policy by keeping a flat layout with mixed

devname

Quoted 2 lines

from all kinds of devices. The prerequisite of this model is that devnames from different bus types are unique formats:

Quoted 9 lines

This isn't reliable, the devname should just be vfio0, vfio1, etc

The userspace can learn the correct major/minor by inspecting the sysfs.

This whole concept should disappear into the prior patch that adds the struct device in the first place, and I think most of the code here can be deleted once the struct device is used properly.

Quoted 6 lines

Can you help elaborate above flow? This is one area where we need more guidance.

When Qemu accepts an option "-device vfio-pci,host=DDDD:BB:DD.F", how does Qemu identify which vifo0/1/... is associated with the specified DDDD:BB:DD.F?

When done properly in the kernel the file:

/sys/bus/pci/devices/DDDD:BB:DD.F/vfio/vfioX/dev

Quoted 7 lines

Will contain the major:minor of the VFIO device.

Userspace then opens the /dev/vfio/devices/vfioX and checks with fstat that the major:minor matches.

in the above pattern "pci" and "DDDD:BB:DD.FF" are the arguments passed to qemu.

I thought part of the appeal of the device centric model was less grovelling around in sysfs for information. Using type/address directly in /dev seems simpler than having to dig around matching things here.

Note that this doesn't have to be done in kernel: you could have the kernel just call them /dev/vfio/devices/vfio0, ... but add udev rules that create symlinks from say /dev/vfio/pci/DDDD:BB:SS.F - > ../devices/vfioXX based on the sysfs information.

Quoted 8 lines

You can look at this for some general over engineered code to handle opening from a sysfs handle like above:

https://github.com/linux-rdma/rdma-core/blob/master/util/open_cdev.c

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
maintainer
#50 lore raw

Re: [RFC 03/20] vfio: Add vfio_[un]register_device()

Quoted 38 lines · On Wed, Sep 29, 2021 at 12:46:14PM +1000, [email protected] wrote:
Quoted 37 lines · Jason Gunthorpe
Quoted 20 lines · Kevin
Quoted 10 lines

From: Jason Gunthorpe <[email protected]>
Sent: Wednesday, September 22, 2021 12:01 AM

Quoted 1 line

One open about how to organize the device nodes under

/dev/vfio/devices/.

Quoted 1 line

This RFC adopts a simple policy by keeping a flat layout with mixed

devname

Quoted 2 lines

from all kinds of devices. The prerequisite of this model is that devnames from different bus types are unique formats:

Quoted 9 lines

This isn't reliable, the devname should just be vfio0, vfio1, etc

The userspace can learn the correct major/minor by inspecting the sysfs.

This whole concept should disappear into the prior patch that adds the struct device in the first place, and I think most of the code here can be deleted once the struct device is used properly.

Quoted 6 lines

Can you help elaborate above flow? This is one area where we need more guidance.

When Qemu accepts an option "-device vfio-pci,host=DDDD:BB:DD.F", how does Qemu identify which vifo0/1/... is associated with the specified DDDD:BB:DD.F?

When done properly in the kernel the file:

/sys/bus/pci/devices/DDDD:BB:DD.F/vfio/vfioX/dev

Will contain the major:minor of the VFIO device.

Userspace then opens the /dev/vfio/devices/vfioX and checks with fstat that the major:minor matches.

Quoted 7 lines
Quoted 2 lines

in the above pattern "pci" and "DDDD:BB:DD.FF" are the arguments passed to qemu.

I thought part of the appeal of the device centric model was less grovelling around in sysfs for information. Using type/address directly in /dev seems simpler than having to dig around matching things here.

I would say more regular grovelling. Starting from a sysfs device directory and querying the VFIO cdev associated with it is much more normal than what happens today, which also includes passing sysfs information into an ioctl :\

Quoted 4 lines

Note that this doesn't have to be done in kernel: you could have the kernel just call them /dev/vfio/devices/vfio0, ... but add udev rules that create symlinks from say /dev/vfio/pci/DDDD:BB:SS.F - > ../devices/vfioXX based on the sysfs information.

This is the right approach if people want to do this, but I'm not sure it is worth it given backwards compat requires the sysfs path as input. We may as well stick with sysfs as the command line interface for userspace tools.

And I certainly don't want to see userspace tools trying to reverse a sysfs path into a /dev/ symlink name when they can directly and reliably learn the correct cdev from the sysfspath.

Jason

#51 lore raw

Re: [RFC 03/20] vfio: Add vfio_[un]register_device()

Quoted 47 lines · Jason Gunthorpe
Quoted 38 lines · On Wed, Sep 29, 2021 at 12:46:14PM +1000, [email protected] wrote:
Quoted 37 lines · Jason Gunthorpe
Quoted 20 lines · Kevin
Quoted 10 lines

From: Jason Gunthorpe <[email protected]>
Sent: Wednesday, September 22, 2021 12:01 AM

Quoted 1 line

One open about how to organize the device nodes under

/dev/vfio/devices/.

Quoted 1 line

This RFC adopts a simple policy by keeping a flat layout with mixed

devname

Quoted 2 lines

from all kinds of devices. The prerequisite of this model is that devnames from different bus types are unique formats:

Quoted 9 lines

This isn't reliable, the devname should just be vfio0, vfio1, etc

The userspace can learn the correct major/minor by inspecting the sysfs.

This whole concept should disappear into the prior patch that adds the struct device in the first place, and I think most of the code here can be deleted once the struct device is used properly.

Quoted 6 lines

Can you help elaborate above flow? This is one area where we need more guidance.

When Qemu accepts an option "-device vfio-pci,host=DDDD:BB:DD.F", how does Qemu identify which vifo0/1/... is associated with the specified DDDD:BB:DD.F?

When done properly in the kernel the file:

/sys/bus/pci/devices/DDDD:BB:DD.F/vfio/vfioX/dev

Will contain the major:minor of the VFIO device.

Userspace then opens the /dev/vfio/devices/vfioX and checks with fstat that the major:minor matches.

Quoted 7 lines
Quoted 2 lines

in the above pattern "pci" and "DDDD:BB:DD.FF" are the arguments passed to qemu.

I thought part of the appeal of the device centric model was less grovelling around in sysfs for information. Using type/address directly in /dev seems simpler than having to dig around matching things here.

Quoted 4 lines

I would say more regular grovelling. Starting from a sysfs device directory and querying the VFIO cdev associated with it is much more normal than what happens today, which also includes passing sysfs information into an ioctl :\

Hm.. ok. Clearly I'm unfamiliar with the things that do that. Other than current VFIO, the only model I've really seen is where you just point your program at a device node.

Quoted 8 lines
Quoted 4 lines

Note that this doesn't have to be done in kernel: you could have the kernel just call them /dev/vfio/devices/vfio0, ... but add udev rules that create symlinks from say /dev/vfio/pci/DDDD:BB:SS.F - > ../devices/vfioXX based on the sysfs information.

This is the right approach if people want to do this, but I'm not sure it is worth it given backwards compat requires the sysfs path as input.

You mean for userspace that needs to be able to go back to the old VFIO interface as well? It seems silly to force this sysfs mucking about on new programs that depend on the new interface.

Quoted 6 lines

We may as well stick with sysfs as the command line interface for userspace tools.

And I certainly don't want to see userspace tools trying to reverse a sysfs path into a /dev/ symlink name when they can directly and reliably learn the correct cdev from the sysfspath.

Um.. sure.. but they can get the correct cdev from the sysfspath no matter how we name the cdevs.

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
#52 lore raw

Re: [RFC 03/20] vfio: Add vfio_[un]register_device()

In reply to Liu Yi L
Quoted 30 lines · Liu Yi L

With /dev/vfio/devices introduced, now a vfio device driver has three options to expose its device to userspace:

a) only legacy group interface, for devices which haven't been moved to

    iommufd (e.g. platform devices, sw mdev, etc.);

b) both legacy group interface and new device-centric interface, for devices which supports iommufd but also wants to keep backward compatibility (e.g. pci devices in this RFC);

c) only new device-centric interface, for new devices which don't carry

    backward compatibility burden (e.g. hw mdev/subdev with pasid);

This patch introduces vfio_[un]register_device() helpers for the device drivers to specify the device exposure policy to vfio core. Hence the existing vfio_[un]register_group_dev() become the wrapper of the new helper functions. The new device-centric interface is described as 'nongroup' to differentiate from existing 'group' stuff.

TBD: this patch needs to rebase on top of below series from Christoph in next version.

"cleanup vfio iommu_group creation"

Legacy userspace continues to follow the legacy group interface.

Newer userspace can first try the new device-centric interface if the device is present under /dev/vfio/devices. Otherwise fall back to the group interface.

Quoted 8 lines

One open about how to organize the device nodes under /dev/vfio/devices/. This RFC adopts a simple policy by keeping a flat layout with mixed devname from all kinds of devices. The prerequisite of this model is that devnames from different bus types are unique formats:

	/dev/vfio/devices/0000:00:14.2 (pci)
	/dev/vfio/devices/PNP0103:00 (platform)
	/dev/vfio/devices/83b8f4f2-509f-382f-3c1e-e6bfe0fa1001 (mdev)

Oof. I really don't think this is a good idea. Ensuring that a format is "unique" in the sense that it can't collide with any of the other formats, for *every* value of the parameters on both sides is actually pretty complicated in general.

I think per-type sub-directories would be helpful here, Jason's suggestion of just sequential numbers would work as well.

Quoted 6 lines

One alternative option is to arrange device nodes in sub-directories based on the device type. But doing so also adds one trouble to userspace. The current vfio uAPI is designed to have the user query device type via VFIO_DEVICE_GET_INFO after opening the device. With this option the user instead needs to figure out the device type before opening the device, to identify the sub-directory.

Wouldn't this be up to the operator / configuration, rather than the actual software though? I would assume that typically the VFIO program would be pointed at a specific vfio device node file to use, e.g.

	my-vfio-prog -d /dev/vfio/pci/0000:0a:03.1

Or more generally, if you're expecting userspace to know a name in a uniqu pattern, they can equally well know a "type/name" pair.

Quoted 34 lines

Another tricky thing is that "pdev. vs. mdev"
and "pci vs. platform vs. ccw,..." are orthogonal categorizations. Need more thoughts on whether both or just one category should be used to define the sub-directories.

---
 drivers/vfio/vfio.c  | 137 +++++++++++++++++++++++++++++++++++++++----
 include/linux/vfio.h |   9 +++
 2 files changed, 134 insertions(+), 12 deletions(-)
drivers/vfio/vfio.c +2 -1
index 84436d7abedd..1e87b25962f1 100644
@@ -51,6 +51,7 @@ static struct vfio {
 	struct cdev			device_cdev;
 	dev_t				device_devt;
 	struct mutex			device_lock;
+	struct list_head		device_list;
 	struct idr			device_idr;
 } vfio;
 
@@ -757,7 +758,7 @@ void vfio_init_group_dev(struct vfio_device *device, struct device *dev,
 }
 EXPORT_SYMBOL_GPL(vfio_init_group_dev);
 
-int vfio_register_group_dev(struct vfio_device *device)
+static int __vfio_register_group_dev(struct vfio_device *device)
 {
 	struct vfio_device *existing_device;
 	struct iommu_group *iommu_group;
@@ -794,8 +795,13 @@ int vfio_register_group_dev(struct vfio_device *device)
 	/* Our reference on group is moved to the device */

drivers/vfio/vfio.c int vfio_register_group_dev(struct vfio_device *device) · line 803

Quoted 10 lines
drivers/vfio/vfio.c +6 -2
 	device->group = group;
 
-	/* Refcounting can't start until the driver calls register */
-	refcount_set(&device->refcount, 1);
+	/*
+	 * Refcounting can't start until the driver call register. Don’t
+	 * start twice when the device is exposed in both group and nongroup
+	 * interfaces.
+	 */
+	if (!refcount_read(&device->refcount))

Is there a possible race here with something getting in and incrementing the refcount between the read and set?

Quoted 11 lines
drivers/vfio/vfio.c +3 -1
+		refcount_set(&device->refcount, 1);
 
 	mutex_lock(&group->device_lock);
 	list_add(&device->group_next, &group->device_list);
@@ -804,7 +810,78 @@ int vfio_register_group_dev(struct vfio_device *device)
 
 	return 0;
 }
-EXPORT_SYMBOL_GPL(vfio_register_group_dev);
+
+static int __vfio_register_nongroup_dev(struct vfio_device *device)

drivers/vfio/vfio.c int vfio_register_group_dev(struct vfio_device *device) · line 824

Quoted 10 lines
drivers/vfio/vfio.c +10 -0
+{
+	struct vfio_device *existing_device;
+	struct device *dev;
+	int ret = 0, minor;
+
+	mutex_lock(&vfio.device_lock);
+	list_for_each_entry(existing_device, &vfio.device_list, vfio_next) {
+		if (existing_device == device) {
+			ret = -EBUSY;
+			goto out_unlock;

This indicates a bug in the caller, doesn't it? Should it be a BUG or WARN instead?

Quoted 191 lines
drivers/vfio/vfio.c +104 -8
+		}
+	}
+
+	minor = idr_alloc(&vfio.device_idr, device, 0, MINORMASK + 1, GFP_KERNEL);
+	pr_debug("%s - mnior: %d\n", __func__, minor);
+	if (minor < 0) {
+		ret = minor;
+		goto out_unlock;
+	}
+
+	dev = device_create(vfio.device_class, NULL,
+			    MKDEV(MAJOR(vfio.device_devt), minor),
+			    device, "%s", dev_name(device->dev));
+	if (IS_ERR(dev)) {
+		idr_remove(&vfio.device_idr, minor);
+		ret = PTR_ERR(dev);
+		goto out_unlock;
+	}
+
+	/*
+	 * Refcounting can't start until the driver call register. Don’t
+	 * start twice when the device is exposed in both group and nongroup
+	 * interfaces.
+	 */
+	if (!refcount_read(&device->refcount))
+		refcount_set(&device->refcount, 1);
+
+	device->minor = minor;
+	list_add(&device->vfio_next, &vfio.device_list);
+	dev_info(device->dev, "Creates Device interface successfully!\n");
+out_unlock:
+	mutex_unlock(&vfio.device_lock);
+	return ret;
+}
+
+int vfio_register_device(struct vfio_device *device, u32 flags)
+{
+	int ret = -EINVAL;
+
+	device->minor = -1;
+	device->group = NULL;
+	atomic_set(&device->opened, 0);
+
+	if (flags & ~(VFIO_DEVNODE_GROUP | VFIO_DEVNODE_NONGROUP))
+		return ret;
+
+	if (flags & VFIO_DEVNODE_GROUP) {
+		ret = __vfio_register_group_dev(device);
+		if (ret)
+			return ret;
+	}
+
+	if (flags & VFIO_DEVNODE_NONGROUP) {
+		ret = __vfio_register_nongroup_dev(device);
+		if (ret && device->group)
+			vfio_unregister_device(device);
+	}
+	return ret;
+}
+EXPORT_SYMBOL_GPL(vfio_register_device);
 
 /**
  * Get a reference to the vfio_device for a device.  Even if the
@@ -861,13 +938,14 @@ static struct vfio_device *vfio_device_get_from_name(struct vfio_group *group,
 /*
  * Decrement the device reference count and wait for the device to be
  * removed.  Open file descriptors for the device... */
-void vfio_unregister_group_dev(struct vfio_device *device)
+void vfio_unregister_device(struct vfio_device *device)
 {
 	struct vfio_group *group = device->group;
 	struct vfio_unbound_dev *unbound;
 	unsigned int i = 0;
 	bool interrupted = false;
 	long rc;
+	int minor = device->minor;
 
 	/*
 	 * When the device is removed from the group, the group suddenly
@@ -878,14 +956,20 @@ void vfio_unregister_group_dev(struct vfio_device *device)
 	 * solve this, we track such devices on the unbound_list to bridge
 	 * the gap until they're fully unbound.
 	 */
-	unbound = kzalloc(sizeof(*unbound), GFP_KERNEL);
-	if (unbound) {
-		unbound->dev = device->dev;
-		mutex_lock(&group->unbound_lock);
-		list_add(&unbound->unbound_next, &group->unbound_list);
-		mutex_unlock(&group->unbound_lock);
+	if (group) {
+		/*
+		 * If caller hasn't called vfio_register_group_dev(), this
+		 * branch is not necessary.
+		 */
+		unbound = kzalloc(sizeof(*unbound), GFP_KERNEL);
+		if (unbound) {
+			unbound->dev = device->dev;
+			mutex_lock(&group->unbound_lock);
+			list_add(&unbound->unbound_next, &group->unbound_list);
+			mutex_unlock(&group->unbound_lock);
+		}
+		WARN_ON(!unbound);
 	}
-	WARN_ON(!unbound);
 
 	vfio_device_put(device);
 	rc = try_wait_for_completion(&device->comp);
@@ -910,6 +994,21 @@ void vfio_unregister_group_dev(struct vfio_device *device)
 		}
 	}
 
+	/* nongroup interface related cleanup */
+	if (minor >= 0) {
+		mutex_lock(&vfio.device_lock);
+		list_del(&device->vfio_next);
+		device->minor = -1;
+		device_destroy(vfio.device_class,
+			       MKDEV(MAJOR(vfio.device_devt), minor));
+		idr_remove(&vfio.device_idr, minor);
+		mutex_unlock(&vfio.device_lock);
+	}
+
+	/* No need go further if no group. */
+	if (!group)
+		return;
+
 	mutex_lock(&group->device_lock);
 	list_del(&device->group_next);
 	group->dev_counter--;
@@ -935,6 +1034,18 @@ void vfio_unregister_group_dev(struct vfio_device *device)
 	/* Matches the get in vfio_register_group_dev() */
 	vfio_group_put(group);
 }
+EXPORT_SYMBOL_GPL(vfio_unregister_device);
+
+int vfio_register_group_dev(struct vfio_device *device)
+{
+	return vfio_register_device(device, VFIO_DEVNODE_GROUP);
+}
+EXPORT_SYMBOL_GPL(vfio_register_group_dev);
+
+void vfio_unregister_group_dev(struct vfio_device *device)
+{
+	vfio_unregister_device(device);
+}
 EXPORT_SYMBOL_GPL(vfio_unregister_group_dev);
 
 /**
@@ -2447,6 +2558,7 @@ static int vfio_init_device_class(void)
 
 	mutex_init(&vfio.device_lock);
 	idr_init(&vfio.device_idr);
+	INIT_LIST_HEAD(&vfio.device_list);
 
 	/* /dev/vfio/devices/$DEVICE */
 	vfio.device_class = class_create(THIS_MODULE, "vfio-device");
@@ -2542,6 +2654,7 @@ static int __init vfio_init(void)
 static void __exit vfio_cleanup(void)
 {
 	WARN_ON(!list_empty(&vfio.group_list));
+	WARN_ON(!list_empty(&vfio.device_list));
 
 #ifdef CONFIG_VFIO_NOIOMMU
 	vfio_unregister_iommu_driver(&vfio_noiommu_ops);
include/linux/vfio.h +9 -0
index 4a5f3f99eab2..9448b751b663 100644
@@ -26,6 +26,7 @@ struct vfio_device {
 	struct list_head group_next;
 	int minor;
 	atomic_t opened;
+	struct list_head vfio_next;
 };
 
 /**
@@ -73,6 +74,14 @@ enum vfio_iommu_notify_type {
 	VFIO_IOMMU_CONTAINER_CLOSE = 0,
 };
 
+/* The device can be opened via VFIO_GROUP_GET_DEVICE_FD */
+#define VFIO_DEVNODE_GROUP	BIT(0)
+/* The device can be opened via /dev/sys/devices/${DEVICE} */
+#define VFIO_DEVNODE_NONGROUP	BIT(1)
+
+extern int vfio_register_device(struct vfio_device *device, u32 flags);
+extern void vfio_unregister_device(struct vfio_device *device);
+
 /**
  * struct vfio_iommu_driver_ops - VFIO IOMMU driver callbacks
  */
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
maintainer
#53 lore raw

RE: [RFC 03/20] vfio: Add vfio_[un]register_device()

Quoted 15 lines

From: David Gibson <[email protected]>
Sent: Wednesday, September 29, 2021 10:44 AM

Quoted 2 lines

One open about how to organize the device nodes under

/dev/vfio/devices/.

Quoted 1 line

This RFC adopts a simple policy by keeping a flat layout with mixed

devname

Quoted 6 lines

from all kinds of devices. The prerequisite of this model is that devnames from different bus types are unique formats:

	/dev/vfio/devices/0000:00:14.2 (pci)
	/dev/vfio/devices/PNP0103:00 (platform)
	/dev/vfio/devices/83b8f4f2-509f-382f-3c1e-e6bfe0fa1001 (mdev)
Quoted 7 lines

Oof. I really don't think this is a good idea. Ensuring that a format is "unique" in the sense that it can't collide with any of the other formats, for *every* value of the parameters on both sides is actually pretty complicated in general.

I think per-type sub-directories would be helpful here, Jason's suggestion of just sequential numbers would work as well.

we'll follow Jason's suggestion in next version.

Quoted 10 lines

drivers/vfio/vfio.c int vfio_register_group_dev(struct vfio_device *device)

Quoted 3 lines
drivers/vfio/vfio.c +3 -0
+	/*
+	 * Refcounting can't start until the driver call register. Don’t
+	 * start twice when the device is exposed in both group and

nongroup

drivers/vfio/vfio.c int vfio_register_group_dev(struct vfio_device *device) · line 803

Quoted 3 lines
drivers/vfio/vfio.c +3 -0
+	 * interfaces.
+	 */
+	if (!refcount_read(&device->refcount))

Is there a possible race here with something getting in and incrementing the refcount between the read and set?

this will not be required in next version, which will always create both group and nongroup interfaces for every device (then let driver providing .bind_iommufd() callback for whether nongroup interface is functional). It will be centrally processed within existing vfio_[un]register_group_dev(), thus above race is not a concern any more.

Quoted 18 lines

drivers/vfio/vfio.c int vfio_register_group_dev(struct vfio_device

Quoted 6 lines
drivers/vfio/vfio.c +1 -0
+		refcount_set(&device->refcount, 1);
 
 	mutex_lock(&group->device_lock);
 	list_add(&device->group_next, &group->device_list);
@@ -804,7 +810,78 @@ int vfio_register_group_dev(struct vfio_device

*device)

Quoted 11 lines
drivers/vfio/vfio.c +7 -1
 	return 0;
 }
-EXPORT_SYMBOL_GPL(vfio_register_group_dev);
+
+static int __vfio_register_nongroup_dev(struct vfio_device *device)
+{
+	struct vfio_device *existing_device;
+	struct device *dev;
+	int ret = 0, minor;
+
Quoted 8 lines

drivers/vfio/vfio.c int vfio_register_group_dev(struct vfio_device *device) · line 824

Quoted 5 lines
drivers/vfio/vfio.c +5 -0
+	mutex_lock(&vfio.device_lock);
+	list_for_each_entry(existing_device, &vfio.device_list, vfio_next) {
+		if (existing_device == device) {
+			ret = -EBUSY;
+			goto out_unlock;

This indicates a bug in the caller, doesn't it? Should it be a BUG or WARN instead?

this call is initiated by userspace. Per Jason's suggestion we don't even need to check it then no lock is required.

Thanks
Kevin

maintainer
#54 lore raw

RE: [RFC 03/20] vfio: Add vfio_[un]register_device()

In reply to David Gibson
Quoted 10 lines

From: David Gibson <[email protected]>
Sent: Wednesday, September 29, 2021 10:44 AM

Quoted 6 lines

One alternative option is to arrange device nodes in sub-directories based on the device type. But doing so also adds one trouble to userspace. The current vfio uAPI is designed to have the user query device type via VFIO_DEVICE_GET_INFO after opening the device. With this option the user instead needs to figure out the device type before opening the device, to identify the sub-directory.

Quoted 9 lines

Wouldn't this be up to the operator / configuration, rather than the actual software though? I would assume that typically the VFIO program would be pointed at a specific vfio device node file to use, e.g.

	my-vfio-prog -d /dev/vfio/pci/0000:0a:03.1

Or more generally, if you're expecting userspace to know a name in a uniqu pattern, they can equally well know a "type/name" pair.

You are correct. Currently:

-device, vfio-pci,host=DDDD:BB:DD.F
-device, vfio-pci,sysfdev=/sys/bus/pci/devices/ DDDD:BB:DD.F -device, vfio-platform,sysdev=/sys/bus/platform/devices/PNP0103:00

above is definitely type/name information to find the related node.

Actually even for Jason's proposal we still need such information to identify the sysfs path.

Then I feel type-based sub-directory does work. Adding another link to sysfs sounds unnecessary now. But I'm not sure whether we still want to create /dev/vfio/devices/vfio0 thing and related udev rule thing that you pointed out in another mail.

Jason?

Thanks
Kevin

#55 lore raw

RE: [RFC 03/20] vfio: Add vfio_[un]register_device()

Quoted 28 lines · Kevin
Quoted 10 lines

From: David Gibson <[email protected]>
Sent: Wednesday, September 29, 2021 10:44 AM

Quoted 6 lines

One alternative option is to arrange device nodes in sub-directories based on the device type. But doing so also adds one trouble to userspace. The current vfio uAPI is designed to have the user query device type via VFIO_DEVICE_GET_INFO after opening the device. With this option the user instead needs to figure out the device type before opening the device, to identify the sub-directory.

Quoted 9 lines

Wouldn't this be up to the operator / configuration, rather than the actual software though? I would assume that typically the VFIO program would be pointed at a specific vfio device node file to use, e.g.

	my-vfio-prog -d /dev/vfio/pci/0000:0a:03.1

Or more generally, if you're expecting userspace to know a name in a uniqu pattern, they can equally well know a "type/name" pair.

You are correct. Currently:

(quoted diff) +0 -3
-device, vfio-pci,host=DDDD:BB:DD.F
-device, vfio-pci,sysfdev=/sys/bus/pci/devices/ DDDD:BB:DD.F
-device, vfio-platform,sysdev=/sys/bus/platform/devices/PNP0103:00

above is definitely type/name information to find the related node.

Quoted 7 lines

Actually even for Jason's proposal we still need such information to identify the sysfs path.

Then I feel type-based sub-directory does work. Adding another link to sysfs sounds unnecessary now. But I'm not sure whether we still want to create /dev/vfio/devices/vfio0 thing and related udev rule thing that you pointed out in another mail.

Still reading through this whole thread, but type-based subdirectories also make the most sense to me. I don't really see userspace wanting to grab just any device and then figure out whether it is the device it was looking for, but rather immediately go to a specific device or at least a device of a specific type.

Sequentially-numbered devices tend to become really unwieldy in my experience if you are working on a system with loads of devices.

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

maintainer
#56 lore raw

Re: [RFC 03/20] vfio: Add vfio_[un]register_device()

Quoted 33 lines · Cornelia Huck
Quoted 31 lines · Kevin
Quoted 10 lines

From: David Gibson <[email protected]>
Sent: Wednesday, September 29, 2021 10:44 AM

Quoted 6 lines

One alternative option is to arrange device nodes in sub-directories based on the device type. But doing so also adds one trouble to userspace. The current vfio uAPI is designed to have the user query device type via VFIO_DEVICE_GET_INFO after opening the device. With this option the user instead needs to figure out the device type before opening the device, to identify the sub-directory.

Quoted 9 lines

Wouldn't this be up to the operator / configuration, rather than the actual software though? I would assume that typically the VFIO program would be pointed at a specific vfio device node file to use, e.g.

	my-vfio-prog -d /dev/vfio/pci/0000:0a:03.1

Or more generally, if you're expecting userspace to know a name in a uniqu pattern, they can equally well know a "type/name" pair.

You are correct. Currently:

(quoted diff) +0 -3
-device, vfio-pci,host=DDDD:BB:DD.F
-device, vfio-pci,sysfdev=/sys/bus/pci/devices/ DDDD:BB:DD.F
-device, vfio-platform,sysdev=/sys/bus/platform/devices/PNP0103:00

above is definitely type/name information to find the related node.

Actually even for Jason's proposal we still need such information to identify the sysfs path.

Quoted 10 lines
Quoted 4 lines

Then I feel type-based sub-directory does work. Adding another link to sysfs sounds unnecessary now. But I'm not sure whether we still want to create /dev/vfio/devices/vfio0 thing and related udev rule thing that you pointed out in another mail.

Still reading through this whole thread, but type-based subdirectories also make the most sense to me. I don't really see userspace wanting to grab just any device and then figure out whether it is the device it was looking for, but rather immediately go to a specific device or at least a device of a specific type.

Even so the kernel should not be creating this, that is a job for udev and some symlinks

Quoted 2 lines

Sequentially-numbered devices tend to become really unwieldy in my experience if you are working on a system with loads of devices.

If the user experiance is always to refer to the sysfs node as Kevin shows above then the user never sees the integer.

It is very much like how the group number works already, programs always start at the sysfs, do the readlink thing on iommu_group and then get the group number to go to /dev/vfio/X

So it is already the case that every piece of software can construct a sysfs path to the device, we are just changing from readlink(iommu_group) to readdir(vfio/vfio_device_XX)

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

author
#57 lore raw

RFC 4/20 iommu: Add iommu_device_get_info interface

In reply to Liu Yi L

From: Lu Baolu <[email protected]>

This provides an interface for upper layers to get the per-device iommu attributes.

int iommu_device_get_info(struct device *dev,
enum iommu_devattr attr, void *data);

The first attribute (IOMMU_DEV_INFO_FORCE_SNOOP) is added. It tells if the iommu can force DMA to snoop cache. At this stage, only PCI devices which have this attribute set could use the iommufd, this is due to supporting no-snoop DMA requires additional refactoring work on the current kvm-vfio contract. The following patch will have vfio check this attribute to decide whether a pci device can be exposed through /dev/vfio/devices.

---
 drivers/iommu/iommu.c | 16 ++++++++++++++++
 include/linux/iommu.h | 19 +++++++++++++++++++
 2 files changed, 35 insertions(+)
drivers/iommu/iommu.c +16 -0
index 63f0af10c403..5ea3a007fd7c 100644
@@ -3260,3 +3260,19 @@ static ssize_t iommu_group_store_type(struct iommu_group *group,
 
 	return ret;
 }
+
+/* Expose per-device iommu attributes. */
+int iommu_device_get_info(struct device *dev, enum iommu_devattr attr, void *data)
+{
+	const struct iommu_ops *ops;
+
+	if (!dev->bus || !dev->bus->iommu_ops)
+		return -EINVAL;
+
+	ops = dev->bus->iommu_ops;
+	if (unlikely(!ops->device_info))
+		return -ENODEV;
+
+	return ops->device_info(dev, attr, data);
+}
+EXPORT_SYMBOL_GPL(iommu_device_get_info);
include/linux/iommu.h +19 -0
index 32d448050bf7..52a6d33c82dc 100644
@@ -150,6 +150,14 @@ enum iommu_dev_features {
 	IOMMU_DEV_FEAT_IOPF,
 };
 
+/**
+ * enum iommu_devattr - Per device IOMMU attributes
+ * @IOMMU_DEV_INFO_FORCE_SNOOP [bool]: IOMMU can force DMA to be snooped.
+ */
+enum iommu_devattr {
+	IOMMU_DEV_INFO_FORCE_SNOOP,
+};
+
 #define IOMMU_PASID_INVALID	(-1U)
 
 #ifdef CONFIG_IOMMU_API
@@ -215,6 +223,7 @@ struct iommu_iotlb_gather {
  *		- IOMMU_DOMAIN_IDENTITY: must use an identity domain
  *		- IOMMU_DOMAIN_DMA: must use a dma domain
  *		- 0: use the default setting
+ * @device_info: query per-device iommu attributes
  * @pgsize_bitmap: bitmap of all possible supported page sizes
  * @owner: Driver module providing these ops
  */
@@ -283,6 +292,8 @@ struct iommu_ops {
 
 	int (*def_domain_type)(struct device *dev);
 
+	int (*device_info)(struct device *dev, enum iommu_devattr attr, void *data);
+
 	unsigned long pgsize_bitmap;
 	struct module *owner;
 };
@@ -604,6 +615,8 @@ struct iommu_sva *iommu_sva_bind_device(struct device *dev,
 void iommu_sva_unbind_device(struct iommu_sva *handle);
 u32 iommu_sva_get_pasid(struct iommu_sva *handle);
 
+int iommu_device_get_info(struct device *dev, enum iommu_devattr attr, void *data);
+
 #else /* CONFIG_IOMMU_API */
 
 struct iommu_ops {};
@@ -999,6 +1012,12 @@ static inline struct iommu_fwspec *dev_iommu_fwspec_get(struct device *dev)
 {
 	return NULL;
 }
+
+static inline int iommu_device_get_info(struct device *dev,
+					enum iommu_devattr type, void *data)
+{
+	return -ENODEV;
+}
 #endif /* CONFIG_IOMMU_API */
 
 /**
Signature · 6 lines
2.25.1

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

Re: [RFC 04/20] iommu: Add iommu_device_get_info interface

Quoted 7 lines · Liu Yi L

From: Lu Baolu <[email protected]>

This provides an interface for upper layers to get the per-device iommu attributes.

int iommu_device_get_info(struct device *dev,
enum iommu_devattr attr, void *data);

Can't we use properly typed ops and functions here instead of a void *data?

get_snoop()
get_page_size()
get_addr_width()

?

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

maintainer
#59 lore raw

Re: [RFC 04/20] iommu: Add iommu_device_get_info interface

Hi Jason,

Quoted 9 lines · Jason Gunthorpe
Quoted 7 lines · Liu Yi L

From: Lu Baolu <[email protected]>

This provides an interface for upper layers to get the per-device iommu attributes.

int iommu_device_get_info(struct device *dev,
enum iommu_devattr attr, void *data);

Quoted 6 lines

Can't we use properly typed ops and functions here instead of a void *data?

get_snoop()
get_page_size()
get_addr_width()

Yeah! Above are more friendly to the upper layer callers.

Quoted 5 lines

?

Jason

Best regards,
baolu

#60 lore raw

Re: [RFC 04/20] iommu: Add iommu_device_get_info interface

Quoted 12 lines · Lu Baolu

Hi Jason,

Quoted 9 lines · Jason Gunthorpe
Quoted 7 lines · Liu Yi L

From: Lu Baolu <[email protected]>

This provides an interface for upper layers to get the per-device iommu attributes.

int iommu_device_get_info(struct device *dev,
enum iommu_devattr attr, void *data);

Quoted 8 lines
Quoted 6 lines

Can't we use properly typed ops and functions here instead of a void *data?

get_snoop()
get_page_size()
get_addr_width()

Yeah! Above are more friendly to the upper layer callers.

The other option would be a struct with all the attributes. Still type safe, but not as many methods. It'll require a little boilerplate in the callers, though.

#61 lore raw

Re: [RFC 04/20] iommu: Add iommu_device_get_info interface

In reply to Liu Yi L
Quoted 7 lines · Liu Yi L

From: Lu Baolu <[email protected]>

This provides an interface for upper layers to get the per-device iommu attributes.

int iommu_device_get_info(struct device *dev,
enum iommu_devattr attr, void *data);

That fact that this interface doesn't let you know how to size the data buffer, other than by just knowing the right size for each attr concerns me.

Quoted 97 lines

The first attribute (IOMMU_DEV_INFO_FORCE_SNOOP) is added. It tells if the iommu can force DMA to snoop cache. At this stage, only PCI devices which have this attribute set could use the iommufd, this is due to supporting no-snoop DMA requires additional refactoring work on the current kvm-vfio contract. The following patch will have vfio check this attribute to decide whether a pci device can be exposed through /dev/vfio/devices.

---
 drivers/iommu/iommu.c | 16 ++++++++++++++++
 include/linux/iommu.h | 19 +++++++++++++++++++
 2 files changed, 35 insertions(+)
drivers/iommu/iommu.c +16 -0
index 63f0af10c403..5ea3a007fd7c 100644
@@ -3260,3 +3260,19 @@ static ssize_t iommu_group_store_type(struct iommu_group *group,
 
 	return ret;
 }
+
+/* Expose per-device iommu attributes. */
+int iommu_device_get_info(struct device *dev, enum iommu_devattr attr, void *data)
+{
+	const struct iommu_ops *ops;
+
+	if (!dev->bus || !dev->bus->iommu_ops)
+		return -EINVAL;
+
+	ops = dev->bus->iommu_ops;
+	if (unlikely(!ops->device_info))
+		return -ENODEV;
+
+	return ops->device_info(dev, attr, data);
+}
+EXPORT_SYMBOL_GPL(iommu_device_get_info);
include/linux/iommu.h +19 -0
index 32d448050bf7..52a6d33c82dc 100644
@@ -150,6 +150,14 @@ enum iommu_dev_features {
 	IOMMU_DEV_FEAT_IOPF,
 };
 
+/**
+ * enum iommu_devattr - Per device IOMMU attributes
+ * @IOMMU_DEV_INFO_FORCE_SNOOP [bool]: IOMMU can force DMA to be snooped.
+ */
+enum iommu_devattr {
+	IOMMU_DEV_INFO_FORCE_SNOOP,
+};
+
 #define IOMMU_PASID_INVALID	(-1U)
 
 #ifdef CONFIG_IOMMU_API
@@ -215,6 +223,7 @@ struct iommu_iotlb_gather {
  *		- IOMMU_DOMAIN_IDENTITY: must use an identity domain
  *		- IOMMU_DOMAIN_DMA: must use a dma domain
  *		- 0: use the default setting
+ * @device_info: query per-device iommu attributes
  * @pgsize_bitmap: bitmap of all possible supported page sizes
  * @owner: Driver module providing these ops
  */
@@ -283,6 +292,8 @@ struct iommu_ops {
 
 	int (*def_domain_type)(struct device *dev);
 
+	int (*device_info)(struct device *dev, enum iommu_devattr attr, void *data);
+
 	unsigned long pgsize_bitmap;
 	struct module *owner;
 };
@@ -604,6 +615,8 @@ struct iommu_sva *iommu_sva_bind_device(struct device *dev,
 void iommu_sva_unbind_device(struct iommu_sva *handle);
 u32 iommu_sva_get_pasid(struct iommu_sva *handle);
 
+int iommu_device_get_info(struct device *dev, enum iommu_devattr attr, void *data);
+
 #else /* CONFIG_IOMMU_API */
 
 struct iommu_ops {};
@@ -999,6 +1012,12 @@ static inline struct iommu_fwspec *dev_iommu_fwspec_get(struct device *dev)
 {
 	return NULL;
 }
+
+static inline int iommu_device_get_info(struct device *dev,
+					enum iommu_devattr type, void *data)
+{
+	return -ENODEV;
+}
 #endif /* CONFIG_IOMMU_API */
 
 /**
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
maintainer
#62 lore raw

Re: [RFC 04/20] iommu: Add iommu_device_get_info interface

Hi David,

Quoted 12 lines · David Gibson
Quoted 7 lines · Liu Yi L

From: Lu Baolu<[email protected]>

This provides an interface for upper layers to get the per-device iommu attributes.

int iommu_device_get_info(struct device *dev,
enum iommu_devattr attr, void *data);

That fact that this interface doesn't let you know how to size the data buffer, other than by just knowing the right size for each attr concerns me.

We plan to address this by following the comments here.

https://lore.kernel.org/linux-iommu/[email protected]/

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

maintainer
#63 lore raw

Re: [RFC 04/20] iommu: Add iommu_device_get_info interface

Quoted 11 lines · Lu Baolu

Hi David,

Quoted 8 lines · David Gibson
Quoted 7 lines · Liu Yi L

From: Lu Baolu<[email protected]>

This provides an interface for upper layers to get the per-device iommu attributes.

     int iommu_device_get_info(struct device *dev,
                               enum iommu_devattr attr, void *data);

Quoted 8 lines
Quoted 4 lines

That fact that this interface doesn't let you know how to size the data buffer, other than by just knowing the right size for each attr concerns me.

We plan to address this by following the comments here.

https://lore.kernel.org/linux-iommu/[email protected]/

And Christoph gave another option as well.

https://lore.kernel.org/linux-iommu/[email protected]/

Best regards,
baolu

author
#64 lore raw

RFC 5/20 vfio/pci: Register device to /dev/vfio/devices

In reply to Liu Yi L

This patch exposes the device-centric interface for vfio-pci devices. To be compatiable with existing users, vfio-pci exposes both legacy group interface and device-centric interface.

As explained in last patch, this change doesn't apply to devices which cannot be forced to snoop cache by their upstream iommu. Such devices are still expected to be opened via the legacy group interface.

When the device is opened via /dev/vfio/devices, vfio-pci should prevent the user from accessing the assigned device because the device is still attached to the default domain which may allow user-initiated DMAs to touch arbitrary place. The user access must be blocked until the device is later bound to an iommufd (see patch 08). The binding acts as the contract for putting the device in a security context which ensures user- initiated DMAs via this device cannot harm the rest of the system.

This patch introduces a vdev->block_access flag for this purpose. It's set when the device is opened via /dev/vfio/devices and cleared after binding to iommufd succeeds. mmap and r/w handlers check this flag to decide whether user access should be blocked or not.

An alternative option is to use a dummy fops when the device is opened and then switch to the real fops (replace_fops()) after binding. Appreciate inputs on which option is better.

The legacy group interface doesn't have this problem. Its uAPI requires the user to first put the device into a security context via container/group attaching process, before opening the device through the groupfd.

---
 drivers/vfio/pci/vfio_pci.c         | 25 +++++++++++++++++++++++--
 drivers/vfio/pci/vfio_pci_private.h |  1 +
 drivers/vfio/vfio.c                 |  3 ++-
 include/linux/vfio.h                |  1 +
 4 files changed, 27 insertions(+), 3 deletions(-)
drivers/vfio/pci/vfio_pci.c +23 -2
index 318864d52837..145addde983b 100644
@@ -572,6 +572,10 @@ static int vfio_pci_open(struct vfio_device *core_vdev)
 
 		vfio_spapr_pci_eeh_open(vdev->pdev);
 		vfio_pci_vf_token_user_add(vdev, 1);
+		if (!vfio_device_in_container(core_vdev))
+			atomic_set(&vdev->block_access, 1);
+		else
+			atomic_set(&vdev->block_access, 0);
 	}
 	vdev->refcnt++;
 error:
@@ -1374,6 +1378,9 @@ static ssize_t vfio_pci_rw(struct vfio_pci_device *vdev, char __user *buf,
 {
 	unsigned int index = VFIO_PCI_OFFSET_TO_INDEX(*ppos);
 
+	if (atomic_read(&vdev->block_access))
+		return -ENODEV;
+
 	if (index >= VFIO_PCI_NUM_REGIONS + vdev->num_regions)
 		return -EINVAL;
 
@@ -1640,6 +1647,9 @@ static int vfio_pci_mmap(struct vfio_device *core_vdev, struct vm_area_struct *v
 	u64 phys_len, req_len, pgoff, req_start;
 	int ret;
 
+	if (atomic_read(&vdev->block_access))
+		return -ENODEV;
+
 	index = vma->vm_pgoff >> (VFIO_PCI_OFFSET_SHIFT - PAGE_SHIFT);
 
 	if (index >= VFIO_PCI_NUM_REGIONS + vdev->num_regions)
@@ -1978,6 +1988,8 @@ static int vfio_pci_probe(struct pci_dev *pdev, const struct pci_device_id *id)
 	struct vfio_pci_device *vdev;
 	struct iommu_group *group;
 	int ret;
+	u32 flags;
+	bool snoop = false;
 
 	if (vfio_pci_is_denylisted(pdev))
 		return -EINVAL;
@@ -2046,9 +2058,18 @@ static int vfio_pci_probe(struct pci_dev *pdev, const struct pci_device_id *id)
 		vfio_pci_set_power_state(vdev, PCI_D3hot);
 	}
 
-	ret = vfio_register_group_dev(&vdev->vdev);
-	if (ret)
+	flags = VFIO_DEVNODE_GROUP;
+	ret = iommu_device_get_info(&pdev->dev,
+				    IOMMU_DEV_INFO_FORCE_SNOOP, &snoop);
+	if (!ret && snoop)
+		flags |= VFIO_DEVNODE_NONGROUP;
+
+	ret = vfio_register_device(&vdev->vdev, flags);
+	if (ret) {
+		pr_debug("Failed to register device interface\n");
 		goto out_power;
+	}
+
 	dev_set_drvdata(&pdev->dev, vdev);
 	return 0;
 
drivers/vfio/pci/vfio_pci_private.h +1 -0
index 5a36272cecbf..f12012e30b53 100644
@@ -143,6 +143,7 @@ struct vfio_pci_device {
 	struct mutex		vma_lock;
 	struct list_head	vma_list;
 	struct rw_semaphore	memory_lock;
+	atomic_t		block_access;
 };
 
 #define is_intx(vdev) (vdev->irq_type == VFIO_PCI_INTX_IRQ_INDEX)
drivers/vfio/vfio.c +2 -1
index 1e87b25962f1..22851747e92c 100644
@@ -1789,10 +1789,11 @@ static int vfio_device_fops_open(struct inode *inode, struct file *filep)
 	return ret;
 }
 
-static bool vfio_device_in_container(struct vfio_device *device)
+bool vfio_device_in_container(struct vfio_device *device)
 {
 	return !!(device->group && device->group->container);
 }
+EXPORT_SYMBOL_GPL(vfio_device_in_container);
 
 static int vfio_device_fops_release(struct inode *inode, struct file *filep)
 {
include/linux/vfio.h +1 -0
index 9448b751b663..fd0629acb948 100644
@@ -81,6 +81,7 @@ enum vfio_iommu_notify_type {
 
 extern int vfio_register_device(struct vfio_device *device, u32 flags);
 extern void vfio_unregister_device(struct vfio_device *device);
+extern bool vfio_device_in_container(struct vfio_device *device);
 
 /**
  * struct vfio_iommu_driver_ops - VFIO IOMMU driver callbacks
Signature · 6 lines
2.25.1

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

Re: [RFC 05/20] vfio/pci: Register device to /dev/vfio/devices

Quoted 16 lines · Liu Yi L

This patch exposes the device-centric interface for vfio-pci devices. To be compatiable with existing users, vfio-pci exposes both legacy group interface and device-centric interface.

As explained in last patch, this change doesn't apply to devices which cannot be forced to snoop cache by their upstream iommu. Such devices are still expected to be opened via the legacy group interface.

When the device is opened via /dev/vfio/devices, vfio-pci should prevent the user from accessing the assigned device because the device is still attached to the default domain which may allow user-initiated DMAs to touch arbitrary place. The user access must be blocked until the device is later bound to an iommufd (see patch 08). The binding acts as the contract for putting the device in a security context which ensures user- initiated DMAs via this device cannot harm the rest of the system.

Quoted 4 lines

This patch introduces a vdev->block_access flag for this purpose. It's set when the device is opened via /dev/vfio/devices and cleared after binding to iommufd succeeds. mmap and r/w handlers check this flag to decide whether user access should be blocked or not.

This should not be in vfio_pci.

AFAIK there is no condition where a vfio driver can work without being connected to some kind of iommu back end, so the core code should handle this interlock globally. A vfio driver's ops should not be callable until the iommu is connected.

The only vfio_pci patch in this series should be adding a new callback op to take in an iommufd and register the pci_device as a iommufd device.

Jason

#66 lore raw

Re: [RFC 05/20] vfio/pci: Register device to /dev/vfio/devices

Quoted 8 lines · On Tue, 21 Sep 2021 13:40:01 -0300 Jason Gunthorpe <[email protected]> wrote:
Quoted 7 lines · Liu Yi L

This patch exposes the device-centric interface for vfio-pci devices. To be compatiable with existing users, vfio-pci exposes both legacy group interface and device-centric interface.

As explained in last patch, this change doesn't apply to devices which cannot be forced to snoop cache by their upstream iommu. Such devices are still expected to be opened via the legacy group interface.

This doesn't make much sense to me. The previous patch indicates there's work to be done in updating the kvm-vfio contract to understand DMA coherency, so you're trying to limit use cases to those where the IOMMU enforces coherency, but there's QEMU work to be done to support the iommufd uAPI at all. Isn't part of that work to understand how KVM will be told about non-coherent devices rather than "meh, skip it in the kernel"? Also let's not forget that vfio is not only for KVM.

Quoted 15 lines
Quoted 8 lines

When the device is opened via /dev/vfio/devices, vfio-pci should prevent the user from accessing the assigned device because the device is still attached to the default domain which may allow user-initiated DMAs to touch arbitrary place. The user access must be blocked until the device is later bound to an iommufd (see patch 08). The binding acts as the contract for putting the device in a security context which ensures user- initiated DMAs via this device cannot harm the rest of the system.

Quoted 4 lines

This patch introduces a vdev->block_access flag for this purpose. It's set when the device is opened via /dev/vfio/devices and cleared after binding to iommufd succeeds. mmap and r/w handlers check this flag to decide whether user access should be blocked or not.

This should not be in vfio_pci.

Quoted 8 lines

AFAIK there is no condition where a vfio driver can work without being connected to some kind of iommu back end, so the core code should handle this interlock globally. A vfio driver's ops should not be callable until the iommu is connected.

The only vfio_pci patch in this series should be adding a new callback op to take in an iommufd and register the pci_device as a iommufd device.

Couldn't the same argument be made that registering a $bus device as an iommufd device is a common interface that shouldn't be the responsibility of the vfio device driver? Is userspace opening the non-group device anything more than a reservation of that device if access is withheld until iommu isolation? I also don't really want to predict how ioctls might evolve to guess whether only blocking .read, .write, and .mmap callbacks are sufficient. Thanks,

Alex

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

maintainer
#67 lore raw

Re: [RFC 05/20] vfio/pci: Register device to /dev/vfio/devices

Quoted 3 lines · Alex Williamson

the iommufd uAPI at all. Isn't part of that work to understand how KVM will be told about non-coherent devices rather than "meh, skip it in the kernel"? Also let's not forget that vfio is not only for KVM.

vfio is not only for KVM, but AFIACT the wbinv stuff is only for KVM... But yes, I agree this should be sorted out at this stage

Quoted 20 lines
Quoted 20 lines
Quoted 8 lines

When the device is opened via /dev/vfio/devices, vfio-pci should prevent the user from accessing the assigned device because the device is still attached to the default domain which may allow user-initiated DMAs to touch arbitrary place. The user access must be blocked until the device is later bound to an iommufd (see patch 08). The binding acts as the contract for putting the device in a security context which ensures user- initiated DMAs via this device cannot harm the rest of the system.

Quoted 4 lines

This patch introduces a vdev->block_access flag for this purpose. It's set when the device is opened via /dev/vfio/devices and cleared after binding to iommufd succeeds. mmap and r/w handlers check this flag to decide whether user access should be blocked or not.

This should not be in vfio_pci.

AFAIK there is no condition where a vfio driver can work without being connected to some kind of iommu back end, so the core code should handle this interlock globally. A vfio driver's ops should not be callable until the iommu is connected.

Quoted 7 lines
Quoted 3 lines

The only vfio_pci patch in this series should be adding a new callback op to take in an iommufd and register the pci_device as a iommufd device.

Couldn't the same argument be made that registering a $bus device as an iommufd device is a common interface that shouldn't be the responsibility of the vfio device driver?

The driver needs enough involvment to signal what kind of IOMMU connection it wants, eg attaching to a physical device will use the iofd_attach_device() path, but attaching to a SW page table should use a different API call. PASID should also be different.

Possibly a good arrangement is to have the core provide some generic ioctl ops functions 'vfio_all_device_iommufd_bind' that everything except mdev drivers can use so the code is all duplicated.

Quoted 4 lines

non-group device anything more than a reservation of that device if access is withheld until iommu isolation? I also don't really want to predict how ioctls might evolve to guess whether only blocking .read, .write, and .mmap callbacks are sufficient. Thanks,

This is why I said the entire fops should be blocked in a dummy fops so the core code the vfio_device FD parked and userspace unable to access the ops until device attachment and thus IOMMU ioslation is completed.

Simple and easy to reason about, a parked FD is very similar to a closed FD.

Jason

maintainer
#68 lore raw

RE: [RFC 05/20] vfio/pci: Register device to /dev/vfio/devices

Quoted 11 lines

From: Jason Gunthorpe <[email protected]>
Sent: Wednesday, September 22, 2021 5:59 AM

Quoted 3 lines · Alex Williamson

the iommufd uAPI at all. Isn't part of that work to understand how KVM will be told about non-coherent devices rather than "meh, skip it in the kernel"? Also let's not forget that vfio is not only for KVM.

vfio is not only for KVM, but AFIACT the wbinv stuff is only for KVM... But yes, I agree this should be sorted out at this stage

If such devices are even not exposed in the new hierarchy at this stage, suppose sorting it out later should be fine?

Quoted 28 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

When the device is opened via /dev/vfio/devices, vfio-pci should

prevent

Quoted 9 lines
Quoted 9 lines
Quoted 9 lines

the user from accessing the assigned device because the device is still attached to the default domain which may allow user-initiated DMAs to touch arbitrary place. The user access must be blocked until the device is later bound to an iommufd (see patch 08). The binding acts as the contract for putting the device in a security context which ensures user- initiated DMAs via this device cannot harm the rest of the system.

This patch introduces a vdev->block_access flag for this purpose. It's set when the device is opened via /dev/vfio/devices and cleared after

binding

Quoted 1 line
Quoted 1 line
Quoted 1 line

to iommufd succeeds. mmap and r/w handlers check this flag to decide

whether

Quoted 13 lines
Quoted 12 lines
Quoted 1 line

user access should be blocked or not.

This should not be in vfio_pci.

AFAIK there is no condition where a vfio driver can work without being connected to some kind of iommu back end, so the core code should handle this interlock globally. A vfio driver's ops should not be callable until the iommu is connected.

The only vfio_pci patch in this series should be adding a new callback op to take in an iommufd and register the pci_device as a iommufd device.

Quoted 8 lines
Quoted 3 lines

Couldn't the same argument be made that registering a $bus device as an iommufd device is a common interface that shouldn't be the responsibility of the vfio device driver?

The driver needs enough involvment to signal what kind of IOMMU connection it wants, eg attaching to a physical device will use the iofd_attach_device() path, but attaching to a SW page table should use a different API call. PASID should also be different.

Exactly

Quoted 4 lines

Possibly a good arrangement is to have the core provide some generic ioctl ops functions 'vfio_all_device_iommufd_bind' that everything except mdev drivers can use so the code is all duplicated.

Could this be an future enhancement when we have multiple device types supporting iommufd?

Quoted 6 lines
Quoted 5 lines

non-group device anything more than a reservation of that device if access is withheld until iommu isolation? I also don't really want to predict how ioctls might evolve to guess whether only blocking .read, .write, and .mmap callbacks are sufficient. Thanks,

Quoted 8 lines

This is why I said the entire fops should be blocked in a dummy fops so the core code the vfio_device FD parked and userspace unable to access the ops until device attachment and thus IOMMU ioslation is completed.

Simple and easy to reason about, a parked FD is very similar to a closed FD.

This rationale makes sense. Just the open how to handle exclusive open between group and nongroup interfaces still needs some more clarification here, especially about what a parked FD means for the group interface (where parking is unnecessary since the security context is already established before the device is opened)

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

maintainer
#69 lore raw

RE: [RFC 05/20] vfio/pci: Register device to /dev/vfio/devices

In reply to Alex Williamson
Quoted 15 lines

From: Alex Williamson <[email protected]>
Sent: Wednesday, September 22, 2021 5:09 AM

Quoted 8 lines · On Tue, 21 Sep 2021 13:40:01 -0300 Jason Gunthorpe <[email protected]> wrote:
Quoted 7 lines · Liu Yi L

This patch exposes the device-centric interface for vfio-pci devices. To be compatiable with existing users, vfio-pci exposes both legacy group interface and device-centric interface.

As explained in last patch, this change doesn't apply to devices which cannot be forced to snoop cache by their upstream iommu. Such devices are still expected to be opened via the legacy group interface.

Quoted 7 lines

This doesn't make much sense to me. The previous patch indicates there's work to be done in updating the kvm-vfio contract to understand DMA coherency, so you're trying to limit use cases to those where the IOMMU enforces coherency, but there's QEMU work to be done to support the iommufd uAPI at all. Isn't part of that work to understand how KVM will be told about non-coherent devices rather than "meh, skip it in the kernel"? Also let's not forget that vfio is not only for KVM.

The policy here is that VFIO will not expose such devices (no enforce-snoop) in the new device hierarchy at all. In this case QEMU will fall back to the group interface automatically and then rely on the existing contract to connect vfio and QEMU. It doesn't need to care about the whatever new contract until such devices are exposed in the new interface.

yes, vfio is not only for KVM. But here it's more a task split based on staging consideration. imo it's not necessary to further split task into supporting non-snoop device for userspace driver and then for kvm.

#70 lore raw

Re: [RFC 05/20] vfio/pci: Register device to /dev/vfio/devices

Quoted 29 lines · Kevin
Quoted 15 lines

From: Alex Williamson <[email protected]>
Sent: Wednesday, September 22, 2021 5:09 AM

Quoted 8 lines · On Tue, 21 Sep 2021 13:40:01 -0300 Jason Gunthorpe <[email protected]> wrote:
Quoted 7 lines · Liu Yi L

This patch exposes the device-centric interface for vfio-pci devices. To be compatiable with existing users, vfio-pci exposes both legacy group interface and device-centric interface.

As explained in last patch, this change doesn't apply to devices which cannot be forced to snoop cache by their upstream iommu. Such devices are still expected to be opened via the legacy group interface.

Quoted 7 lines

This doesn't make much sense to me. The previous patch indicates there's work to be done in updating the kvm-vfio contract to understand DMA coherency, so you're trying to limit use cases to those where the IOMMU enforces coherency, but there's QEMU work to be done to support the iommufd uAPI at all. Isn't part of that work to understand how KVM will be told about non-coherent devices rather than "meh, skip it in the kernel"? Also let's not forget that vfio is not only for KVM.

The policy here is that VFIO will not expose such devices (no enforce-snoop) in the new device hierarchy at all. In this case QEMU will fall back to the group interface automatically and then rely on the existing contract to connect vfio and QEMU. It doesn't need to care about the whatever new contract until such devices are exposed in the new interface.

Quoted 3 lines

yes, vfio is not only for KVM. But here it's more a task split based on staging consideration. imo it's not necessary to further split task into supporting non-snoop device for userspace driver and then for kvm.

Patch 10 introduces an iommufd interface for QEMU to learn whether the IOMMU enforces DMA coherency, at that point QEMU could revert to the legacy interface, or register the iommufd with KVM, or otherwise establish non-coherent DMA with KVM as necessary. We're adding cruft to the kernel here to enforce an unnecessary limitation.

If there are reasons the kernel can't support the device interface, that's a valid reason not to present the interface, but this seems like picking a specific gap that userspace is already able to detect from this series at the expense of other use cases. Thanks,

Alex

maintainer
#71 lore raw

RE: [RFC 05/20] vfio/pci: Register device to /dev/vfio/devices

Quoted 48 lines

From: Alex Williamson <[email protected]>
Sent: Thursday, September 23, 2021 5:17 AM

Quoted 6 lines · Kevin
Quoted 6 lines

From: Alex Williamson <[email protected]>
Sent: Wednesday, September 22, 2021 5:09 AM

On Tue, 21 Sep 2021 13:40:01 -0300
Jason Gunthorpe <[email protected]> wrote:

Quoted 7 lines
Quoted 7 lines
Quoted 7 lines · Liu Yi L
Quoted 6 lines

This patch exposes the device-centric interface for vfio-pci devices. To be compatiable with existing users, vfio-pci exposes both legacy group interface and device-centric interface.

As explained in last patch, this change doesn't apply to devices which cannot be forced to snoop cache by their upstream iommu. Such

devices

Quoted 6 lines
Quoted 6 lines
Quoted 1 line
Quoted 1 line

are still expected to be opened via the legacy group interface.

This doesn't make much sense to me. The previous patch indicates there's work to be done in updating the kvm-vfio contract to understand DMA coherency, so you're trying to limit use cases to those where the IOMMU enforces coherency, but there's QEMU work to be done to

support

Quoted 7 lines
Quoted 3 lines

the iommufd uAPI at all. Isn't part of that work to understand how KVM will be told about non-coherent devices rather than "meh, skip it in the kernel"? Also let's not forget that vfio is not only for KVM.

The policy here is that VFIO will not expose such devices (no enforce-snoop) in the new device hierarchy at all. In this case QEMU will fall back to the group interface automatically and then rely on the existing contract to

connect

Quoted 6 lines

vfio and QEMU. It doesn't need to care about the whatever new contract until such devices are exposed in the new interface.

yes, vfio is not only for KVM. But here it's more a task split based on staging consideration. imo it's not necessary to further split task into supporting non-snoop device for userspace driver and then for kvm.

Patch 10 introduces an iommufd interface for QEMU to learn whether the IOMMU enforces DMA coherency, at that point QEMU could revert to the legacy interface, or register the iommufd with KVM, or otherwise establish non-coherent DMA with KVM as necessary. We're adding cruft to the kernel here to enforce an unnecessary limitation.

Quoted 5 lines

If there are reasons the kernel can't support the device interface, that's a valid reason not to present the interface, but this seems like picking a specific gap that userspace is already able to detect from this series at the expense of other use cases. Thanks,

I see your point now. Yes I agree that the kernel cruft is unnecessary limitation here. The user should rely on the device/iommufd capability to decide whether non-coherent DMA should go through legacy or new interface.

Thanks
Kevin

author
#72 lore raw

RFC 6/20 iommu: Add iommu_device_init[exit]_user_dma interfaces

In reply to Liu Yi L

From: Lu Baolu <[email protected]>

This extends iommu core to manage security context for passthrough devices. Please bear a long explanation for how we reach this design instead of managing it solely in iommufd like what vfio does today.

Devices which cannot be isolated from each other are organized into an iommu group. When a device is assigned to the user space, the entire group must be put in a security context so that user-initiated DMAs via the assigned device cannot harm the rest of the system. No user access should be granted on a device before the security context is established for the group which the device belongs to.

Managing the security context must meet below criteria:

1) The group is viable for user-initiated DMAs. This implies that the devices in the group must be either bound to a device-passthrough framework, or driver-less, or bound to a driver which is known safe (not do DMA).

2) The security context should only allow DMA to the user's memory and

    devices in this group;

3) After the security context is established for the group, the group viability must be continuously monitored before the user relinquishes all devices belonging to the group. The viability might be broken e.g. when a driver-less device is later bound to a driver which does DMA.

4) The security context should not be destroyed before user access permission is withdrawn.

Existing vfio introduces explicit container/group semantics in its uAPI to meet above requirements. A single security context (iommu domain) is created per container. Attaching group to container moves the entire group into the associated security context, and vice versa. The user can open the device only after group attach. A group can be detached only after all devices in the group are closed. Group viability is monitored by listening to iommu group events.

Unlike vfio, iommufd adopts a device-centric design with all group logistics hidden behind the fd. Binding a device to iommufd serves as the contract to get security context established (and vice versa for unbinding). One additional requirement in iommufd is to manage the switch between multiple security contexts due to decoupled bind/attach:

1) Open a device in "/dev/vfio/devices" with user access blocked;

2) Bind the device to an iommufd with an initial security context (an empty iommu domain which blocks dma) established for its group, with user access unblocked;

3) Attach the device to a user-specified ioasid (shared by all devices attached to this ioasid). Before attaching, the device should be first detached from the initial context;

4) Detach the device from the ioasid and switch it back to the initial

    security context;

5) Unbind the device from the iommufd, back to access blocked state and move its group out of the initial security context if it's the last unbound device in the group;

(multiple attach/detach could happen between 2 and 5).

However existing iommu core has problem with above transition. Detach in step 3/4 makes the device/group re-attached to the default domain automatically, which opens the door for user-initiated DMAs to attack the rest of the system. The existing vfio doesn't have this problem as it combines 2/3 in one step (so does 4/5).

Fixing this problem requires the iommu core to also participate in the security context management. Following this direction we also move group viability check into the iommu core, which allows iommufd to stay fully device-centric w/o keeping any group knowledge (combining with the extension to iommu_at[de]tach_device() in a latter patch).

Basically two new interfaces are provided:

        int iommu_device_init_user_dma(struct device *dev,
                        unsigned long owner);
        void iommu_device_exit_user_dma(struct device *dev);

iommufd calls them respectively when handling device binding/unbinding requests.

The init_user_dma() for the 1st device in a group marks the entire group for user-dma and establishes the initial security context (dma blocked) according to aforementioned criteria. As long as the group is marked for user-dma, auto-reattaching to default domain is disabled. Instead, upon detaching the group is moved back to the initial security context.

The caller also provides an owner id to mark the ownership so inadvertent attempt from another caller on the same device can be captured. In this RFC iommufd will use the fd context pointer as the owner id.

The exit_user_dma() for the last device in the group clears the user-dma mark and moves the group back to the default domain.

---
 drivers/iommu/iommu.c | 145 +++++++++++++++++++++++++++++++++++++++++-
 include/linux/iommu.h |  12 ++++
 2 files changed, 154 insertions(+), 3 deletions(-)
drivers/iommu/iommu.c +142 -3
index 5ea3a007fd7c..bffd84e978fb 100644
@@ -45,6 +45,8 @@ struct iommu_group {
 	struct iommu_domain *default_domain;
 	struct iommu_domain *domain;
 	struct list_head entry;
+	unsigned long user_dma_owner_id;
+	refcount_t owner_cnt;
 };
 
 struct group_device {
@@ -86,6 +88,7 @@ static int iommu_create_device_direct_mappings(struct iommu_group *group,
 static struct iommu_group *iommu_group_get_for_dev(struct device *dev);
 static ssize_t iommu_group_store_type(struct iommu_group *group,
 				      const char *buf, size_t count);
+static bool iommu_group_user_dma_viable(struct iommu_group *group);
 
 #define IOMMU_GROUP_ATTR(_name, _mode, _show, _store)		\
 struct iommu_group_attribute iommu_group_attr_##_name =		\
@@ -275,7 +278,11 @@ int iommu_probe_device(struct device *dev)
 	 */
 	iommu_alloc_default_domain(group, dev);
 
-	if (group->default_domain) {
+	/*
+	 * If any device in the group has been initialized for user dma,
+	 * avoid attaching the default domain.
+	 */
+	if (group->default_domain && !group->user_dma_owner_id) {
 		ret = __iommu_attach_device(group->default_domain, dev);
 		if (ret) {
 			iommu_group_put(group);
@@ -1664,6 +1671,17 @@ static int iommu_bus_notifier(struct notifier_block *nb,
 		group_action = IOMMU_GROUP_NOTIFY_BIND_DRIVER;
 		break;
 	case BUS_NOTIFY_BOUND_DRIVER:
+		/*
+		 * FIXME: Alternatively the attached drivers could generically
+		 * indicate to the iommu layer that they are safe for keeping
+		 * the iommu group user viable by calling some function around
+		 * probe(). We could eliminate this gross BUG_ON() by denying
+		 * probe to non-iommu-safe driver.
+		 */
+		mutex_lock(&group->mutex);
+		if (group->user_dma_owner_id)
+			BUG_ON(!iommu_group_user_dma_viable(group));
+		mutex_unlock(&group->mutex);
 		group_action = IOMMU_GROUP_NOTIFY_BOUND_DRIVER;
 		break;
 	case BUS_NOTIFY_UNBIND_DRIVER:
@@ -2304,7 +2322,11 @@ static int __iommu_attach_group(struct iommu_domain *domain,
 {
 	int ret;
 
-	if (group->default_domain && group->domain != group->default_domain)
+	/*
+	 * group->domain could be NULL when a domain is detached from the
+	 * group but the default_domain is not re-attached.
+	 */
+	if (group->domain && group->domain != group->default_domain)
 		return -EBUSY;
 
 	ret = __iommu_group_for_each_dev(group, domain,
@@ -2341,7 +2363,11 @@ static void __iommu_detach_group(struct iommu_domain *domain,
 {
 	int ret;
 
-	if (!group->default_domain) {
+	/*
+	 * If any device in the group has been initialized for user dma,
+	 * avoid re-attaching the default domain.
+	 */
+	if (!group->default_domain || group->user_dma_owner_id) {
 		__iommu_group_for_each_dev(group, domain,
 					   iommu_group_do_detach_device);
 		group->domain = NULL;
@@ -3276,3 +3302,116 @@ int iommu_device_get_info(struct device *dev, enum iommu_devattr attr, void *dat
 	return ops->device_info(dev, attr, data);
 }
 EXPORT_SYMBOL_GPL(iommu_device_get_info);
+
+/*
+ * IOMMU core interfaces for iommufd.
+ */
+
+/*
+ * FIXME: We currently simply follow vifo policy to mantain the group's
+ * viability to user. Eventually, we should avoid below hard-coded list
+ * by letting drivers indicate to the iommu layer that they are safe for
+ * keeping the iommu group's user aviability.
+ */
+static const char * const iommu_driver_allowed[] = {
+	"vfio-pci",
+	"pci-stub"
+};
+
+/*
+ * An iommu group is viable for use by userspace if all devices are in
+ * one of the following states:
+ *  - driver-less
+ *  - bound to an allowed driver
+ *  - a PCI interconnect device
+ */
+static int device_user_dma_viable(struct device *dev, void *data)
+{
+	struct device_driver *drv = READ_ONCE(dev->driver);
+
+	if (!drv)
+		return 0;
+
+	if (dev_is_pci(dev)) {
+		struct pci_dev *pdev = to_pci_dev(dev);
+
+		if (pdev->hdr_type != PCI_HEADER_TYPE_NORMAL)
+			return 0;
+	}
+
+	return match_string(iommu_driver_allowed,
+			    ARRAY_SIZE(iommu_driver_allowed),
+			    drv->name) < 0;
+}
+
+static bool iommu_group_user_dma_viable(struct iommu_group *group)
+{
+	return !__iommu_group_for_each_dev(group, NULL, device_user_dma_viable);
+}
+
+static int iommu_group_init_user_dma(struct iommu_group *group,
+				     unsigned long owner)
+{
+	if (group->user_dma_owner_id) {
+		if (group->user_dma_owner_id != owner)
+			return -EBUSY;
+
+		refcount_inc(&group->owner_cnt);
+		return 0;
+	}
+
+	if (group->domain && group->domain != group->default_domain)
+		return -EBUSY;
+
+	if (!iommu_group_user_dma_viable(group))
+		return -EINVAL;
+
+	group->user_dma_owner_id = owner;
+	refcount_set(&group->owner_cnt, 1);
+
+	/* default domain is unsafe for user-initiated dma */
+	if (group->domain == group->default_domain)
+		__iommu_detach_group(group->default_domain, group);
+
+	return 0;
+}
+
+int iommu_device_init_user_dma(struct device *dev, unsigned long owner)
+{
+	struct iommu_group *group = iommu_group_get(dev);
+	int ret;
+
+	if (!group || !owner)
+		return -ENODEV;
+
+	mutex_lock(&group->mutex);
+	ret = iommu_group_init_user_dma(group, owner);
+	mutex_unlock(&group->mutex);
+	iommu_group_put(group);
+
+	return ret;
+}
+EXPORT_SYMBOL_GPL(iommu_device_init_user_dma);
+
+static void iommu_group_exit_user_dma(struct iommu_group *group)
+{
+	if (refcount_dec_and_test(&group->owner_cnt)) {
+		group->user_dma_owner_id = 0;
+		if (group->default_domain)
+			__iommu_attach_group(group->default_domain, group);
+	}
+}
+
+void iommu_device_exit_user_dma(struct device *dev)
+{
+	struct iommu_group *group = iommu_group_get(dev);
+
+	if (WARN_ON(!group || !group->user_dma_owner_id))
+		return;
+
+	mutex_lock(&group->mutex);
+	iommu_group_exit_user_dma(group);
+	mutex_unlock(&group->mutex);
+	iommu_group_put(group);
+}
+EXPORT_SYMBOL_GPL(iommu_device_exit_user_dma);
include/linux/iommu.h +12 -0
index 52a6d33c82dc..943de6897f56 100644
@@ -617,6 +617,9 @@ u32 iommu_sva_get_pasid(struct iommu_sva *handle);
 
 int iommu_device_get_info(struct device *dev, enum iommu_devattr attr, void *data);
 
+int iommu_device_init_user_dma(struct device *dev, unsigned long owner);
+void iommu_device_exit_user_dma(struct device *dev);
+
 #else /* CONFIG_IOMMU_API */
 
 struct iommu_ops {};
@@ -1018,6 +1021,15 @@ static inline int iommu_device_get_info(struct device *dev,
 {
 	return -ENODEV;
 }
+
+static inline int iommu_device_init_user_dma(struct device *dev, unsigned long owner)
+{
+	return -ENODEV;
+}
+
+static inline void iommu_device_exit_user_dma(struct device *dev)
+{
+}
 #endif /* CONFIG_IOMMU_API */
 
 /**
Signature · 6 lines
2.25.1

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

Re: [RFC 06/20] iommu: Add iommu_device_init[exit]_user_dma interfaces

Quoted 13 lines · Liu Yi L

From: Lu Baolu <[email protected]>

This extends iommu core to manage security context for passthrough devices. Please bear a long explanation for how we reach this design instead of managing it solely in iommufd like what vfio does today.

Devices which cannot be isolated from each other are organized into an iommu group. When a device is assigned to the user space, the entire group must be put in a security context so that user-initiated DMAs via the assigned device cannot harm the rest of the system. No user access should be granted on a device before the security context is established for the group which the device belongs to.

Quoted 4 lines

Managing the security context must meet below criteria:

1) The group is viable for user-initiated DMAs. This implies that the devices in the group must be either bound to a device-passthrough

s/a/the same/

Quoted 22 lines

framework, or driver-less, or bound to a driver which is known safe (not do DMA).

2) The security context should only allow DMA to the user's memory and

    devices in this group;

3) After the security context is established for the group, the group viability must be continuously monitored before the user relinquishes all devices belonging to the group. The viability might be broken e.g. when a driver-less device is later bound to a driver which does DMA.

4) The security context should not be destroyed before user access permission is withdrawn.

Existing vfio introduces explicit container/group semantics in its uAPI to meet above requirements. A single security context (iommu domain) is created per container. Attaching group to container moves the entire group into the associated security context, and vice versa. The user can open the device only after group attach. A group can be detached only after all devices in the group are closed. Group viability is monitored by listening to iommu group events.

Quoted 5 lines

Unlike vfio, iommufd adopts a device-centric design with all group logistics hidden behind the fd. Binding a device to iommufd serves as the contract to get security context established (and vice versa for unbinding). One additional requirement in iommufd is to manage the switch between multiple security contexts due to decoupled bind/attach:

This should be a precursor series that actually does clean things up properly. There is no reason for vfio and iommufd to differ here, if we are implementing this logic into the iommu layer then it should be deleted from the VFIO layer, not left duplicated like this.

IIRC in VFIO the container is the IOAS and when the group goes to create the device fd it should simply do the iommu_device_init_user_dma() followed immediately by a call to bind the container IOAS as your #3.

Then delete all the group viability stuff from vfio, relying on the iommu to do it.

It should have full symmetry with the iommufd.

Quoted 5 lines
drivers/iommu/iommu.c +1 -0
@@ -1664,6 +1671,17 @@ static int iommu_bus_notifier(struct notifier_block *nb,
 		group_action = IOMMU_GROUP_NOTIFY_BIND_DRIVER;
 		break;
 	case BUS_NOTIFY_BOUND_DRIVER:
+		/*

drivers/iommu/iommu.c static int iommu_bus_notifier(struct notifier_block *nb, · line 1684

Quoted 10 lines
drivers/iommu/iommu.c +10 -0
+		 * FIXME: Alternatively the attached drivers could generically
+		 * indicate to the iommu layer that they are safe for keeping
+		 * the iommu group user viable by calling some function around
+		 * probe(). We could eliminate this gross BUG_ON() by denying
+		 * probe to non-iommu-safe driver.
+		 */
+		mutex_lock(&group->mutex);
+		if (group->user_dma_owner_id)
+			BUG_ON(!iommu_group_user_dma_viable(group));
+		mutex_unlock(&group->mutex);

And the mini-series should fix this BUG_ON properly by interlocking with the driver core to simply refuse to bind a driver under these conditions instead of allowing userspace to crash the kernel.

That alone would be justification enough to merge this work.

Quoted 5 lines
drivers/iommu/iommu.c +5 -0
+
+/*
+ * IOMMU core interfaces for iommufd.
+ */
+

drivers/iommu/iommu.c int iommu_device_get_info(struct device *dev, enum iommu_devattr attr, void *dat · line 3319

Quoted 10 lines
drivers/iommu/iommu.c +10 -0
+/*
+ * FIXME: We currently simply follow vifo policy to mantain the group's
+ * viability to user. Eventually, we should avoid below hard-coded list
+ * by letting drivers indicate to the iommu layer that they are safe for
+ * keeping the iommu group's user aviability.
+ */
+static const char * const iommu_driver_allowed[] = {
+	"vfio-pci",
+	"pci-stub"
+};

Yuk. This should be done with some callback in those drivers 'iomm_allow_user_dma()"

Ie the basic flow would see the driver core doing some:

ret = iommu_doing_kernel_dma()
if (ret) do not bind
driver_bind
pci_stub_probe()
iommu_allow_user_dma()

And the various functions are manipulating some atomic.
0 = nothing happening
1 = kernel DMA
2 = user DMA

No BUG_ON.

Jason

maintainer
#74 lore raw

RE: [RFC 06/20] iommu: Add iommu_device_init[exit]_user_dma interfaces

Quoted 52 lines

From: Jason Gunthorpe <[email protected]>
Sent: Wednesday, September 22, 2021 1:10 AM

Quoted 13 lines · Liu Yi L

From: Lu Baolu <[email protected]>

This extends iommu core to manage security context for passthrough devices. Please bear a long explanation for how we reach this design instead of managing it solely in iommufd like what vfio does today.

Devices which cannot be isolated from each other are organized into an iommu group. When a device is assigned to the user space, the entire group must be put in a security context so that user-initiated DMAs via the assigned device cannot harm the rest of the system. No user access should be granted on a device before the security context is established for the group which the device belongs to.

Quoted 4 lines

Managing the security context must meet below criteria:

1) The group is viable for user-initiated DMAs. This implies that the devices in the group must be either bound to a device-passthrough

s/a/the same/

Quoted 22 lines

framework, or driver-less, or bound to a driver which is known safe (not do DMA).

2) The security context should only allow DMA to the user's memory and

    devices in this group;

3) After the security context is established for the group, the group viability must be continuously monitored before the user relinquishes all devices belonging to the group. The viability might be broken e.g. when a driver-less device is later bound to a driver which does DMA.

4) The security context should not be destroyed before user access permission is withdrawn.

Existing vfio introduces explicit container/group semantics in its uAPI to meet above requirements. A single security context (iommu domain) is created per container. Attaching group to container moves the entire group into the associated security context, and vice versa. The user can open the device only after group attach. A group can be detached only after all devices in the group are closed. Group viability is monitored by listening to iommu group events.

Quoted 5 lines

Unlike vfio, iommufd adopts a device-centric design with all group logistics hidden behind the fd. Binding a device to iommufd serves as the contract to get security context established (and vice versa for unbinding). One additional requirement in iommufd is to manage the switch between multiple security contexts due to decoupled bind/attach:

Quoted 4 lines

This should be a precursor series that actually does clean things up properly. There is no reason for vfio and iommufd to differ here, if we are implementing this logic into the iommu layer then it should be deleted from the VFIO layer, not left duplicated like this.

make sense

Quoted 5 lines

IIRC in VFIO the container is the IOAS and when the group goes to create the device fd it should simply do the iommu_device_init_user_dma() followed immediately by a call to bind the container IOAS as your #3.

a slight correction.

to meet vfio semantics we could do init_user_dma() at group attach time and then call binding to container IOAS when the device fd is created. This is because vfio requires the group in a security context before the device is opened.

Quoted 5 lines

Then delete all the group viability stuff from vfio, relying on the iommu to do it.

It should have full symmetry with the iommufd.

agree

Quoted 24 lines

Review static int iommu_bus_notifier(struct

Quoted 2 lines
(quoted diff) +0 -0
@@ -1664,6 +1671,17 @@ static int iommu_bus_notifier(struct

notifier_block *nb,

drivers/iommu/iommu.c static int iommu_bus_notifier(struct notifier_block *nb,

Quoted 7 lines
drivers/iommu/iommu.c +4 -0
 		group_action = IOMMU_GROUP_NOTIFY_BIND_DRIVER;
 		break;
 	case BUS_NOTIFY_BOUND_DRIVER:
+		/*
+		 * FIXME: Alternatively the attached drivers could generically
+		 * indicate to the iommu layer that they are safe for keeping
+		 * the iommu group user viable by calling some function

around

Review static int iommu_bus_notifier(struct · line 1678

Quoted 1 line
(quoted diff) +1 -0
+		 * probe(). We could eliminate this gross BUG_ON() by

denying

drivers/iommu/iommu.c static int iommu_bus_notifier(struct notifier_block *nb, · line 1684

Quoted 6 lines
drivers/iommu/iommu.c +6 -0
+		 * probe to non-iommu-safe driver.
+		 */
+		mutex_lock(&group->mutex);
+		if (group->user_dma_owner_id)
+			BUG_ON(!iommu_group_user_dma_viable(group));
+		mutex_unlock(&group->mutex);

And the mini-series should fix this BUG_ON properly by interlocking with the driver core to simply refuse to bind a driver under these conditions instead of allowing userspace to crash the kernel.

Quoted 1 line

That alone would be justification enough to merge this work.

yes

Quoted 20 lines
Quoted 6 lines
drivers/iommu/iommu.c +5 -0
+
+/*
+ * IOMMU core interfaces for iommufd.
+ */
+

drivers/iommu/iommu.c int iommu_device_get_info(struct device *dev, enum iommu_devattr attr, void *dat · line 3319

Quoted 10 lines
drivers/iommu/iommu.c +10 -0
+/*
+ * FIXME: We currently simply follow vifo policy to mantain the group's
+ * viability to user. Eventually, we should avoid below hard-coded list
+ * by letting drivers indicate to the iommu layer that they are safe for
+ * keeping the iommu group's user aviability.
+ */
+static const char * const iommu_driver_allowed[] = {
+	"vfio-pci",
+	"pci-stub"
+};

Yuk. This should be done with some callback in those drivers 'iomm_allow_user_dma()"

Quoted 1 line

Ie the basic flow would see the driver core doing some:

Just double confirm. Is there concern on having the driver core to call iommu functions?

Quoted 15 lines

ret = iommu_doing_kernel_dma()
if (ret) do not bind
driver_bind
pci_stub_probe()
iommu_allow_user_dma()

And the various functions are manipulating some atomic.
0 = nothing happening
1 = kernel DMA
2 = user DMA

No BUG_ON.

Jason

maintainer
#75 lore raw

Re: [RFC 06/20] iommu: Add iommu_device_init[exit]_user_dma interfaces

Quoted 11 lines · Kevin
Quoted 4 lines

IIRC in VFIO the container is the IOAS and when the group goes to create the device fd it should simply do the iommu_device_init_user_dma() followed immediately by a call to bind the container IOAS as your #3.

a slight correction.

to meet vfio semantics we could do init_user_dma() at group attach time and then call binding to container IOAS when the device fd is created. This is because vfio requires the group in a security context before the device is opened.

Is it? Until a device FD is opened the group fd is kind of idle, right?

Quoted 4 lines
Quoted 1 line

Ie the basic flow would see the driver core doing some:

Just double confirm. Is there concern on having the driver core to call iommu functions?

It is always an interesting question, but I'd say iommu is foundantional to Linux and if it needs driver core help it shouldn't be any different from PM, pinctl, or other subsystems that have inserted themselves into the driver core.

Something kind of like the below.

If I recall, once it is done like this then the entire iommu notifier infrastructure can be ripped out which is a lot of code.

drivers/base/dd.c +6 -0
index 68ea1f949daa90..e39612c99c6123 100644
@@ -566,6 +566,10 @@ static int really_probe(struct device *dev, struct device_driver *drv)
                goto done;
        }
 
+       ret = iommu_set_kernel_ownership(dev);
+       if (ret)
+               return ret;
+
 re_probe:
        dev->driver = drv;
 
@@ -673,6 +677,7 @@ static int really_probe(struct device *dev, struct device_driver *drv)
                dev->pm_domain->dismiss(dev);
        pm_runtime_reinit(dev);
        dev_pm_set_driver_flags(dev, 0);
+       iommu_release_kernel_ownership(dev);
 done:
        return ret;
 }
@@ -1214,6 +1219,7 @@ static void __device_release_driver(struct device *dev, struct device *parent)
                        dev->pm_domain->dismiss(dev);
                pm_runtime_reinit(dev);
                dev_pm_set_driver_flags(dev, 0);
+               iommu_release_kernel_ownership(dev);
 
                klist_remove(&dev->p->knode_driver);
                device_pm_check_callbacks(dev);
maintainer
#76 lore raw

RE: [RFC 06/20] iommu: Add iommu_device_init[exit]_user_dma interfaces

Quoted 10 lines

From: Jason Gunthorpe <[email protected]>
Sent: Wednesday, September 22, 2021 8:40 PM

Quoted 5 lines · Kevin
Quoted 4 lines

IIRC in VFIO the container is the IOAS and when the group goes to create the device fd it should simply do the iommu_device_init_user_dma() followed immediately by a call to bind the container IOAS as your #3.

Quoted 8 lines
Quoted 6 lines

a slight correction.

to meet vfio semantics we could do init_user_dma() at group attach time and then call binding to container IOAS when the device fd is created. This is because vfio requires the group in a security context before the device is opened.

Is it? Until a device FD is opened the group fd is kind of idle, right?

yes, then there is no user-tangible difference between init_user_dma() at group attach time vs. doing it when opening fd(). But the latter does require more change than the former, as it also needs the vfio iommu driver to provide a .device_attach callback.

What's in my mind now is to keep existing group attach sequence which further calls a group-version init_user_dma(). Then when device fd is created, just create a iommu_dev object and switch to normal fops.

Quoted 11 lines
Quoted 5 lines
Quoted 2 lines

Ie the basic flow would see the driver core doing some:

Just double confirm. Is there concern on having the driver core to call iommu functions?

It is always an interesting question, but I'd say iommu is foundantional to Linux and if it needs driver core help it shouldn't be any different from PM, pinctl, or other subsystems that have inserted themselves into the driver core.

Quoted 4 lines

Something kind of like the below.

If I recall, once it is done like this then the entire iommu notifier infrastructure can be ripped out which is a lot of code.

thanks for the guidance. will think more along this direction...

Quoted 36 lines
drivers/base/dd.c +5 -0
index 68ea1f949daa90..e39612c99c6123 100644
@@ -566,6 +566,10 @@ static int really_probe(struct device *dev, struct
 device_driver *drv)
                goto done;
        }
 
+       ret = iommu_set_kernel_ownership(dev);
+       if (ret)
+               return ret;
+
 re_probe:
        dev->driver = drv;
@@ -673,6 +677,7 @@ static int really_probe(struct device *dev, struct
 device_driver *drv)
                dev->pm_domain->dismiss(dev);
        pm_runtime_reinit(dev);
        dev_pm_set_driver_flags(dev, 0);
+       iommu_release_kernel_ownership(dev);
 done:
        return ret;

}

drivers/base/dd.c +1 -0
@@ -1214,6 +1219,7 @@ static void __device_release_driver(struct device
 *dev, struct device *parent)
                        dev->pm_domain->dismiss(dev);
                pm_runtime_reinit(dev);
                dev_pm_set_driver_flags(dev, 0);
+               iommu_release_kernel_ownership(dev);
 
                klist_remove(&dev->p->knode_driver);
                device_pm_check_callbacks(dev);
maintainer
#77 lore raw

RE: [RFC 06/20] iommu: Add iommu_device_init[exit]_user_dma interfaces

In reply to Jason Gunthorpe
Quoted 13 lines

From: Jason Gunthorpe <[email protected]>
Sent: Wednesday, September 22, 2021 8:40 PM

Quoted 4 lines
Quoted 1 line

Ie the basic flow would see the driver core doing some:

Just double confirm. Is there concern on having the driver core to call iommu functions?

It is always an interesting question, but I'd say iommu is foundantional to Linux and if it needs driver core help it shouldn't be any different from PM, pinctl, or other subsystems that have inserted themselves into the driver core.

Quoted 4 lines

Something kind of like the below.

If I recall, once it is done like this then the entire iommu notifier infrastructure can be ripped out which is a lot of code.

Currently vfio is the only user of this notifier mechanism. Now three events are handled in vfio_iommu_group_notifier():

NOTIFY_ADD_DEVICE: this is basically for some sanity check. suppose not required once we handle it cleanly in the iommu/driver core.

NOTIFY_BOUND_DRIVER: the BUG_ON() logic to be fixed by this change.

NOTIFY_UNBOUND_DRIVER: still needs some thoughts. Based on the comments the group->unbound_list is used to avoid breaking group viability check between vfio_unregister_group_dev() and final dev/drv teardown. within that small window the device is not tracked by vfio group but is still bound to a driver (e.g. vfio-pci itself), while an external group user may hold a reference to the group. Possibly it's not required now with the new mechanism as we rely on init/exit_user_dma() as the single switch to claim/ withdraw the group ownership. As long as exit_user_dma() is not called until vfio_group_release(), above small window is covered thus no need to maintain a unbound_list.

But anyway since this corner case is tricky, will think more in case of any oversight.

Quoted 35 lines
drivers/base/dd.c +5 -0
index 68ea1f949daa90..e39612c99c6123 100644
@@ -566,6 +566,10 @@ static int really_probe(struct device *dev, struct
 device_driver *drv)
                goto done;
        }
 
+       ret = iommu_set_kernel_ownership(dev);
+       if (ret)
+               return ret;
+
 re_probe:
        dev->driver = drv;
@@ -673,6 +677,7 @@ static int really_probe(struct device *dev, struct
 device_driver *drv)
                dev->pm_domain->dismiss(dev);
        pm_runtime_reinit(dev);
        dev_pm_set_driver_flags(dev, 0);
+       iommu_release_kernel_ownership(dev);
 done:
        return ret;

}

drivers/base/dd.c +1 -0
@@ -1214,6 +1219,7 @@ static void __device_release_driver(struct device
 *dev, struct device *parent)
                        dev->pm_domain->dismiss(dev);
                pm_runtime_reinit(dev);
                dev_pm_set_driver_flags(dev, 0);
+               iommu_release_kernel_ownership(dev);
 
                klist_remove(&dev->p->knode_driver);
Quoted 1 line
                device_pm_check_callbacks(dev);

I expanded above into below conceptual draft. Please help check whether it matches your thought:

drivers/base/dd.c +6 -0
index 68ea1f9..826a651 100644
@@ -566,6 +566,10 @@ static int really_probe(struct device *dev, struct device_driver *drv)
 		goto done;
 	}
 
+	ret = iommu_device_set_dma_hint(dev, drv->dma_hint);
+	if (ret)
+		return ret;
+
 re_probe:
 	dev->driver = drv;
 
@@ -673,6 +677,7 @@ static int really_probe(struct device *dev, struct device_driver *drv)
 		dev->pm_domain->dismiss(dev);
 	pm_runtime_reinit(dev);
 	dev_pm_set_driver_flags(dev, 0);
+	iommu_device_clear_dma_hint(dev);
 done:
 	return ret;
 }
@@ -1214,6 +1219,7 @@ static void __device_release_driver(struct device *dev, struct device *parent)
 			dev->pm_domain->dismiss(dev);
 		pm_runtime_reinit(dev);
 		dev_pm_set_driver_flags(dev, 0);
+		iommu_device_clear_dma_hint(dev);
 
 		klist_remove(&dev->p->knode_driver);
 		device_pm_check_callbacks(dev);
drivers/iommu/iommu.c +98 -0

104 lines not on this page · raw message

drivers/pci/pci-stub.c +3 -0
index e408099..4568811 100644
@@ -36,6 +36,9 @@ static int pci_stub_probe(struct pci_dev *dev, const struct pci_device_id *id)
 	.name		= "pci-stub",
 	.id_table	= NULL,	/* only dynamic id's */
 	.probe		= pci_stub_probe,
+	.driver = {
+		.dma_hint	= DMA_FOR_NONE,
+	},
 };
 
 static int __init pci_stub_init(void)
drivers/vdpa/ifcvf/ifcvf_main.c +3 -0
index dcd648e..a613b78 100644
@@ -678,6 +678,9 @@ static void ifcvf_remove(struct pci_dev *pdev)
 	.id_table = ifcvf_pci_ids,
 	.probe    = ifcvf_probe,
 	.remove   = ifcvf_remove,
+	.driver = {
+		.dma_hint	= DMA_FOR_USER,
+	},
 };
 
 module_pci_driver(ifcvf_driver);
drivers/vfio/pci/vfio_pci.c +3 -0
index a5ce92b..61c422d 100644
@@ -193,6 +193,9 @@ static int vfio_pci_sriov_configure(struct pci_dev *pdev, int nr_virtfn)
 	.remove			= vfio_pci_remove,
 	.sriov_configure	= vfio_pci_sriov_configure,
 	.err_handler		= &vfio_pci_core_err_handlers,
+	.driver = {
+		.dma_hint	= DMA_FOR_USER,
+	},
 };
 
 static void __init vfio_pci_fill_ids(void)
include/linux/device/driver.h +12 -0
index a498ebc..6bddfd2 100644
@@ -48,6 +48,17 @@ enum probe_type {
 };
 
 /**
+ * enum dma_hint - device driver dma hint
+ *	Device drivers may provide hints for whether dma is
+ *	intended for kernel driver, user driver, not not required.
+ */
+enum dma_hint {
+	DMA_FOR_KERNEL,
+	DMA_FOR_USER,
+	DMA_FOR_NONE,
+};
+
+/**
  * struct device_driver - The basic device driver structure
  * @name:	Name of the device driver.
  * @bus:	The bus which the device of this driver belongs to.
@@ -101,6 +112,7 @@ struct device_driver {
 
 	bool suppress_bind_attrs;	/* disables bind/unbind via sysfs */
 	enum probe_type probe_type;
+	enum dma_type dma_type;
 
 	const struct of_device_id	*of_match_table;
 	const struct acpi_device_id	*acpi_match_table;

Thanks
Kevin

maintainer
#78 lore raw

Re: [RFC 06/20] iommu: Add iommu_device_init[exit]_user_dma interfaces

Quoted 7 lines · Kevin
drivers/iommu/iommu.c +7 -0
+int iommu_device_set_dma_hint(struct device *dev, enum dma_hint hint)
+{
+	struct iommu_group *group;
+	int ret;
+
+	group = iommu_group_get(dev);
+	/* not an iommu-probed device */

drivers/iommu/iommu.c void iommu_group_put(struct iommu_group *group) · line 1118

Quoted 10 lines
drivers/iommu/iommu.c +10 -0
+	if (!group)
+		return 0;
+
+	mutex_lock(&group->mutex);
+	ret = __iommu_group_viable(group, hint);
+	mutex_unlock(&group->mutex);
+
+	iommu_group_put(group);
+	return ret;
+}

Conceptually, we could also move iommu_deferred_attach() from iommu_dma_ops here to save unnecessary checks in the hot DMA API paths?

Best regards,
baolu

maintainer
#79 lore raw

RE: [RFC 06/20] iommu: Add iommu_device_init[exit]_user_dma interfaces

Quoted 17 lines

From: Lu Baolu <[email protected]>
Sent: Monday, September 27, 2021 7:34 PM

Quoted 13 lines · Kevin
drivers/iommu/iommu.c +13 -0
+int iommu_device_set_dma_hint(struct device *dev, enum dma_hint hint)
+{
+	struct iommu_group *group;
+	int ret;
+
+	group = iommu_group_get(dev);
+	/* not an iommu-probed device */
+	if (!group)
+		return 0;
+
+	mutex_lock(&group->mutex);
+	ret = __iommu_group_viable(group, hint);
+	mutex_unlock(&group->mutex);
Quoted 9 lines

drivers/iommu/iommu.c void iommu_group_put(struct iommu_group *group) · line 1118

Quoted 4 lines
drivers/iommu/iommu.c +4 -0
+
+	iommu_group_put(group);
+	return ret;
+}

Conceptually, we could also move iommu_deferred_attach() from iommu_dma_ops here to save unnecessary checks in the hot DMA API paths?

Yes, it's possible. But just be curious, why doesn't iommu core manage deferred_attach when receiving BOUND_DRIVER event? Is there other implication that deferred attach cannot be done at driver binding time?

Thanks
Kevin

maintainer
#80 lore raw

Re: [RFC 06/20] iommu: Add iommu_device_init[exit]_user_dma interfaces

In reply to Tian, Kevin
Quoted 21 lines · Kevin
Quoted 13 lines

From: Jason Gunthorpe <[email protected]>
Sent: Wednesday, September 22, 2021 8:40 PM

Quoted 4 lines
Quoted 1 line

Ie the basic flow would see the driver core doing some:

Just double confirm. Is there concern on having the driver core to call iommu functions?

It is always an interesting question, but I'd say iommu is foundantional to Linux and if it needs driver core help it shouldn't be any different from PM, pinctl, or other subsystems that have inserted themselves into the driver core.

Quoted 4 lines

Something kind of like the below.

If I recall, once it is done like this then the entire iommu notifier infrastructure can be ripped out which is a lot of code.

Currently vfio is the only user of this notifier mechanism. Now three events are handled in vfio_iommu_group_notifier():

Quoted 7 lines

NOTIFY_ADD_DEVICE: this is basically for some sanity check. suppose not required once we handle it cleanly in the iommu/driver core.

NOTIFY_BOUND_DRIVER: the BUG_ON() logic to be fixed by this change.

NOTIFY_UNBOUND_DRIVER: still needs some thoughts. Based on the comments the group->unbound_list is used to avoid breaking

I have a patch series to delete the unbound_list, the scenario you describe is handled by the device_lock()

drivers/base/dd.c static int really_probe(struct device *dev, struct device_driver *drv) · line 571

Quoted 10 lines
drivers/base/dd.c +4 -0
index 68ea1f9..826a651 100644
+++ b/drivers/base/dd.c
@@ -566,6 +566,10 @@ static int really_probe(struct device *dev, struct device_driver *drv)
 		goto done;
 	}
 
+	ret = iommu_device_set_dma_hint(dev, drv->dma_hint);
+	if (ret)
+		return ret;

I think for such a narrow usage you should not change the struct device_driver. Just have pci_stub call a function to flip back to user mode.

drivers/base/dd.c static int really_probe(struct device *dev, struct device_driver *drv) · line 575

Quoted 4 lines
drivers/base/dd.c +4 -0
+static int iommu_dev_viable(struct device *dev, void *data)
+{
+	enum dma_hint hint = *data;
+	struct device_driver *drv = READ_ONCE(dev->driver);

Especially since this isn't locked properly or safe.

Jason

maintainer
#81 lore raw

RE: [RFC 06/20] iommu: Add iommu_device_init[exit]_user_dma interfaces

Quoted 28 lines

From: Jason Gunthorpe <[email protected]>
Sent: Monday, September 27, 2021 7:54 PM

Quoted 24 lines · Kevin
Quoted 13 lines

From: Jason Gunthorpe <[email protected]>
Sent: Wednesday, September 22, 2021 8:40 PM

Quoted 4 lines
Quoted 1 line

Ie the basic flow would see the driver core doing some:

Just double confirm. Is there concern on having the driver core to call iommu functions?

It is always an interesting question, but I'd say iommu is foundantional to Linux and if it needs driver core help it shouldn't be any different from PM, pinctl, or other subsystems that have inserted themselves into the driver core.

Quoted 4 lines

Something kind of like the below.

If I recall, once it is done like this then the entire iommu notifier infrastructure can be ripped out which is a lot of code.

Currently vfio is the only user of this notifier mechanism. Now three events are handled in vfio_iommu_group_notifier():

NOTIFY_ADD_DEVICE: this is basically for some sanity check. suppose not required once we handle it cleanly in the iommu/driver core.

Quoted 7 lines
Quoted 4 lines

NOTIFY_BOUND_DRIVER: the BUG_ON() logic to be fixed by this change.

NOTIFY_UNBOUND_DRIVER: still needs some thoughts. Based on the comments the group->unbound_list is used to avoid breaking

I have a patch series to delete the unbound_list, the scenario you describe is handled by the device_lock()

that's great!

Quoted 9 lines

drivers/base/dd.c static int really_probe(struct device *dev, struct

Quoted 5 lines
drivers/base/dd.c +1 -0
index 68ea1f9..826a651 100644
+++ b/drivers/base/dd.c
@@ -566,6 +566,10 @@ static int really_probe(struct device *dev, struct

device_driver *drv)

Quoted 3 lines
drivers/base/dd.c +0 -0
 		goto done;
 	}
 
Quoted 7 lines

drivers/base/dd.c static int really_probe(struct device *dev, struct device_driver *drv) · line 571

Quoted 3 lines
drivers/base/dd.c +3 -0
+	ret = iommu_device_set_dma_hint(dev, drv->dma_hint);
+	if (ret)
+		return ret;

I think for such a narrow usage you should not change the struct device_driver. Just have pci_stub call a function to flip back to user mode.

Here we want to ensure that kernel dma should be blocked if the group is already marked for user-dma. If we just blindly do it for any driver at this point (as you commented earlier):

+ ret = iommu_set_kernel_ownership(dev);
+ if (ret)
+ return ret;

how would pci-stub reach its function to indicate that it doesn't do dma and flip back?

Do you envision a simpler policy that no driver can be bound to the group if it's already set for user-dma? what about vfio-pci itself?

Quoted 7 lines

drivers/base/dd.c static int really_probe(struct device *dev, struct · line 575

Quoted 5 lines
drivers/base/dd.c +4 -0
+static int iommu_dev_viable(struct device *dev, void *data)
+{
+	enum dma_hint hint = *data;
+	struct device_driver *drv = READ_ONCE(dev->driver);

Especially since this isn't locked properly or safe.

I have the same worry when copying from vfio. Not sure how vfio gets safe with this approach...

Thanks
Kevin

maintainer
#82 lore raw

Re: [RFC 06/20] iommu: Add iommu_device_init[exit]_user_dma interfaces

Quoted 12 lines · Kevin
Quoted 3 lines

I think for such a narrow usage you should not change the struct device_driver. Just have pci_stub call a function to flip back to user mode.

Here we want to ensure that kernel dma should be blocked if the group is already marked for user-dma. If we just blindly do it for any driver at this point (as you commented earlier):

drivers/base/dd.c +3 -0
+       ret = iommu_set_kernel_ownership(dev);
+       if (ret)
+               return ret;
Quoted 6 lines

how would pci-stub reach its function to indicate that it doesn't do dma and flip back?

Do you envision a simpler policy that no driver can be bound to the group if it's already set for user-dma? what about vfio-pci itself?

Yes.. I'm not sure there is a good use case to allow the stub drivers to load/unload while a VFIO is running. At least, not a strong enough one to justify a global change to the driver core..

Quoted 9 lines
Quoted 6 lines

drivers/iommu/iommu.c void iommu_group_put(struct iommu_group *group) · line 1070

Quoted 4 lines
drivers/iommu/iommu.c +4 -0
+static int iommu_dev_viable(struct device *dev, void *data)
+{
+	enum dma_hint hint = *data;
+	struct device_driver *drv = READ_ONCE(dev->driver);

Especially since this isn't locked properly or safe.

I have the same worry when copying from vfio. Not sure how vfio gets safe with this approach...

Fixing the locking in vfio_dev_viable is part of deleting the unbound list. Once it properly uses the device_lock and doesn't race with the driver core like this things are much better. Don't copy this stuff into the iommu core without fixing it.

https://github.com/jgunthorpe/linux/commit/fa6abb318ccca114da12c0b5b123c99131ace926
https://github.com/jgunthorpe/linux/commit/45980bd90b023d1eea56df70d1c395bdf4cc7cf1

I can't remember if the above is contingent on some of the mdev cleanups or not.. Have to get back to it.

Jason

maintainer
#83 lore raw

RE: [RFC 06/20] iommu: Add iommu_device_init[exit]_user_dma interfaces

Quoted 20 lines

From: Jason Gunthorpe
Sent: Monday, September 27, 2021 9:10 PM

Quoted 14 lines · Kevin
Quoted 3 lines

I think for such a narrow usage you should not change the struct device_driver. Just have pci_stub call a function to flip back to user mode.

Here we want to ensure that kernel dma should be blocked if the group is already marked for user-dma. If we just blindly do it for any driver at this point (as you commented earlier):

drivers/base/dd.c +3 -0
+       ret = iommu_set_kernel_ownership(dev);
+       if (ret)
+               return ret;

how would pci-stub reach its function to indicate that it doesn't do dma and flip back?

Quoted 7 lines
Quoted 3 lines

Do you envision a simpler policy that no driver can be bound to the group if it's already set for user-dma? what about vfio-pci itself?

Yes.. I'm not sure there is a good use case to allow the stub drivers to load/unload while a VFIO is running. At least, not a strong enough one to justify a global change to the driver core..

I'm fine with not loading pci-stub. From the very 1st commit msg looks pci-stub was introduced before vfio to prevent host driver loading when doing device assignment with KVM. I'm not sure whether other usages are built on pci-stub later, but in general it's not good to position devices in a same group into different usages.

but I'm little worried that even vfio-pci itself cannot be bound now, which implies that all devices in a group which are intended to be used by the user must be bound to vfio-pci in a breath before the user attempts to open any of them, i.e. late-binding and device- hotplug is disallowed after the initial open. I'm not sure how important such an usage would be, but it does cause user-tangible semantics change.

Alex?

Quoted 8 lines
Quoted 8 lines
Quoted 7 lines

drivers/iommu/iommu.c void iommu_group_put(struct iommu_group *group) · line 1070

Quoted 5 lines
drivers/iommu/iommu.c +4 -0
+static int iommu_dev_viable(struct device *dev, void *data)
+{
+	enum dma_hint hint = *data;
+	struct device_driver *drv = READ_ONCE(dev->driver);

Especially since this isn't locked properly or safe.

Quoted 7 lines
Quoted 2 lines

I have the same worry when copying from vfio. Not sure how vfio gets safe with this approach...

Fixing the locking in vfio_dev_viable is part of deleting the unbound list. Once it properly uses the device_lock and doesn't race with the driver core like this things are much better. Don't copy this stuff into the iommu core without fixing it.

sure. Above was just a quickly-baked sample code to match your thought.

Quoted 9 lines

https://github.com/jgunthorpe/linux/commit/fa6abb318ccca114da12c0b5b1
23c99131ace926
https://github.com/jgunthorpe/linux/commit/45980bd90b023d1eea56df70d
1c395bdf4cc7cf1

I can't remember if the above is contingent on some of the mdev cleanups or not.. Have to get back to it.

my home network has some problem to access above links. Will check it tomorrow and follow the fix when working on the formal change in iommu core.

Thanks
Kevin

maintainer
#84 lore raw

Re: [RFC 06/20] iommu: Add iommu_device_init[exit]_user_dma interfaces

Quoted 7 lines · Kevin

but I'm little worried that even vfio-pci itself cannot be bound now, which implies that all devices in a group which are intended to be used by the user must be bound to vfio-pci in a breath before the user attempts to open any of them, i.e. late-binding and device- hotplug is disallowed after the initial open. I'm not sure how important such an usage would be, but it does cause user-tangible semantics change.

Oh, that's bad..

I guess your approach is the only way forward, it will have to be extensively justified in the commit message for Greg et al.

Jason

maintainer
#85 lore raw

RE: [RFC 06/20] iommu: Add iommu_device_init[exit]_user_dma interfaces

Quoted 13 lines

From: Jason Gunthorpe <[email protected]>
Sent: Monday, September 27, 2021 10:40 PM

Quoted 7 lines · Kevin

but I'm little worried that even vfio-pci itself cannot be bound now, which implies that all devices in a group which are intended to be used by the user must be bound to vfio-pci in a breath before the user attempts to open any of them, i.e. late-binding and device- hotplug is disallowed after the initial open. I'm not sure how important such an usage would be, but it does cause user-tangible semantics change.

Quoted 5 lines

Oh, that's bad..

I guess your approach is the only way forward, it will have to be extensively justified in the commit message for Greg et al.

Just thought about another alternative. What about having driver core to call iommu after call_driver_probe()?

call_driver_probe()

	pci_stub_probe()
		iommu_set_dma_mode(dev, DMA_NONE);

iommu_check_dma_mode(dev);

The default dma mode is DMA_KERNEL. Above allows driver to opt for DMA_NONE or DMA_USER w/o changing the device_driver structure. Right after probe() is completed, we check whether dma mode of this device is allowed by the iommu core (based on recorded dma mode info of sibling devices in the same group). If not, then fail the binding.

Thanks
Kevin

maintainer
#86 lore raw

Re: [RFC 06/20] iommu: Add iommu_device_init[exit]_user_dma interfaces

Quoted 13 lines · Kevin
Quoted 13 lines

From: Jason Gunthorpe <[email protected]>
Sent: Monday, September 27, 2021 10:40 PM

Quoted 7 lines · Kevin

but I'm little worried that even vfio-pci itself cannot be bound now, which implies that all devices in a group which are intended to be used by the user must be bound to vfio-pci in a breath before the user attempts to open any of them, i.e. late-binding and device- hotplug is disallowed after the initial open. I'm not sure how important such an usage would be, but it does cause user-tangible semantics change.

Quoted 8 lines
Quoted 5 lines

Oh, that's bad..

I guess your approach is the only way forward, it will have to be extensively justified in the commit message for Greg et al.

Just thought about another alternative. What about having driver core to call iommu after call_driver_probe()?

Then the kernel is now already exposed to an insecure scenario, we must not do probe if any user device is attached at all.

Jason

maintainer
#87 lore raw

RE: [RFC 06/20] iommu: Add iommu_device_init[exit]_user_dma interfaces

Quoted 23 lines

From: Jason Gunthorpe <[email protected]>
Sent: Tuesday, September 28, 2021 7:55 PM

Quoted 19 lines · Kevin
Quoted 18 lines

From: Jason Gunthorpe <[email protected]>
Sent: Monday, September 27, 2021 10:40 PM

Quoted 7 lines · Kevin

but I'm little worried that even vfio-pci itself cannot be bound now, which implies that all devices in a group which are intended to be used by the user must be bound to vfio-pci in a breath before the user attempts to open any of them, i.e. late-binding and device- hotplug is disallowed after the initial open. I'm not sure how important such an usage would be, but it does cause user-tangible semantics change.

Oh, that's bad..

I guess your approach is the only way forward, it will have to be extensively justified in the commit message for Greg et al.

Quoted 6 lines
Quoted 2 lines

Just thought about another alternative. What about having driver core to call iommu after call_driver_probe()?

Then the kernel is now already exposed to an insecure scenario, we must not do probe if any user device is attached at all.

Originally I thought it's fine as long as the entire probe process is not completed. Based on your comment I feel your concern is that no guarantee that the driver won't do any iommu related work in its probe function thus it's insecure?

Thanks
Kevin

#88 lore raw

Re: [RFC 06/20] iommu: Add iommu_device_init[exit]_user_dma interfaces

In reply to Tian, Kevin
Quoted 28 lines · Kevin
Quoted 20 lines

From: Jason Gunthorpe
Sent: Monday, September 27, 2021 9:10 PM

Quoted 14 lines · Kevin
Quoted 3 lines

I think for such a narrow usage you should not change the struct device_driver. Just have pci_stub call a function to flip back to user mode.

Here we want to ensure that kernel dma should be blocked if the group is already marked for user-dma. If we just blindly do it for any driver at this point (as you commented earlier):

drivers/base/dd.c +3 -0
+       ret = iommu_set_kernel_ownership(dev);
+       if (ret)
+               return ret;

how would pci-stub reach its function to indicate that it doesn't do dma and flip back?

Quoted 7 lines
Quoted 3 lines

Do you envision a simpler policy that no driver can be bound to the group if it's already set for user-dma? what about vfio-pci itself?

Yes.. I'm not sure there is a good use case to allow the stub drivers to load/unload while a VFIO is running. At least, not a strong enough one to justify a global change to the driver core..

Quoted 5 lines

I'm fine with not loading pci-stub. From the very 1st commit msg looks pci-stub was introduced before vfio to prevent host driver loading when doing device assignment with KVM. I'm not sure whether other usages are built on pci-stub later, but in general it's not good to position devices in a same group into different usages.

IIRC, pci-stub was invented for legacy KVM device assignment because KVM was never an actual device driver, it just latched onto and started using the device. If there was an existing driver for the device then KVM would fail to get device resources. Therefore the device needed to be unbound from its standard host driver, but that left it susceptible to driver loads usurping the device. Therefore pci-stub came along to essentially claim the device on behalf of KVM.

With vfio, there are a couple use cases of pci-stub that can be interesting. The first is that pci-stub is generally built into the kernel, not as a module, which provides users the ability to specify a list of ids for pci-stub to claim on the kernel command line with higher priority than loadable modules. This can prevent default driver bindings to devices until tools like driverctl or boot time scripting gets a shot to load the user designated driver for a device.

The other use case, is that if a group is composed of multiple devices and all those devices are bound to vfio drivers, then the user can gain direct access to each of those devices. If we wanted to insert a barrier to restrict user access to certain devices within a group, we'd suggest binding those devices to pci-stub. Obviously within a group, it may still be possible to manipulate the device via p2p DMA, but the barrier is much higher and device, if not platform, specific to manipulate such devices. An example use case might be a chipset Ethernet controller grouped among system management function in a multi-function root complex integrated endpoint.

Quoted 7 lines

but I'm little worried that even vfio-pci itself cannot be bound now, which implies that all devices in a group which are intended to be used by the user must be bound to vfio-pci in a breath before the user attempts to open any of them, i.e. late-binding and device- hotplug is disallowed after the initial open. I'm not sure how important such an usage would be, but it does cause user-tangible semantics change.

Yep, a high potential to break userspace, especially as pci-stub has been recommended for the cases noted above. I don't expect that tools like libvirt manage unassigned devices within a group, but that probably means that there are all sorts of ad-hoc user mechanisms beyond simply assigning all the devices. Thanks,

Alex

maintainer
#89 lore raw

RE: [RFC 06/20] iommu: Add iommu_device_init[exit]_user_dma interfaces

Quoted 56 lines

From: Alex Williamson <[email protected]>
Sent: Tuesday, September 28, 2021 3:20 AM

Quoted 28 lines · Kevin
Quoted 20 lines

From: Jason Gunthorpe
Sent: Monday, September 27, 2021 9:10 PM

Quoted 14 lines · Kevin
Quoted 3 lines

I think for such a narrow usage you should not change the struct device_driver. Just have pci_stub call a function to flip back to user mode.

Here we want to ensure that kernel dma should be blocked if the group is already marked for user-dma. If we just blindly do it for any driver at this point (as you commented earlier):

drivers/base/dd.c +3 -0
+       ret = iommu_set_kernel_ownership(dev);
+       if (ret)
+               return ret;

how would pci-stub reach its function to indicate that it doesn't do dma and flip back?

Quoted 7 lines
Quoted 3 lines

Do you envision a simpler policy that no driver can be bound to the group if it's already set for user-dma? what about vfio-pci itself?

Yes.. I'm not sure there is a good use case to allow the stub drivers to load/unload while a VFIO is running. At least, not a strong enough one to justify a global change to the driver core..

Quoted 5 lines

I'm fine with not loading pci-stub. From the very 1st commit msg looks pci-stub was introduced before vfio to prevent host driver loading when doing device assignment with KVM. I'm not sure whether other usages are built on pci-stub later, but in general it's not good to position devices in a same group into different usages.

IIRC, pci-stub was invented for legacy KVM device assignment because KVM was never an actual device driver, it just latched onto and started using the device. If there was an existing driver for the device then KVM would fail to get device resources. Therefore the device needed to be unbound from its standard host driver, but that left it susceptible to driver loads usurping the device. Therefore pci-stub came along to essentially claim the device on behalf of KVM.

With vfio, there are a couple use cases of pci-stub that can be interesting. The first is that pci-stub is generally built into the kernel, not as a module, which provides users the ability to specify a list of ids for pci-stub to claim on the kernel command line with higher priority than loadable modules. This can prevent default driver bindings to devices until tools like driverctl or boot time scripting gets a shot to load the user designated driver for a device.

Quoted 10 lines

The other use case, is that if a group is composed of multiple devices and all those devices are bound to vfio drivers, then the user can gain direct access to each of those devices. If we wanted to insert a barrier to restrict user access to certain devices within a group, we'd suggest binding those devices to pci-stub. Obviously within a group, it may still be possible to manipulate the device via p2p DMA, but the barrier is much higher and device, if not platform, specific to manipulate such devices. An example use case might be a chipset Ethernet controller grouped among system management function in a multi-function root complex integrated endpoint.

Thanks for the background. It perfectly reflects how many tricky things that vfio has evolved to deal with and we'll dig them out again in this refactoring process with your help. 😊

just a nit on the last example. If a system management function is in such group, isn't the right policy is to disallow assigning any device in this group? Even the barrier is high, any chance of allowing the guest to control a system management function is dangerous...

Quoted 9 lines
Quoted 8 lines

but I'm little worried that even vfio-pci itself cannot be bound now, which implies that all devices in a group which are intended to be used by the user must be bound to vfio-pci in a breath before the user attempts to open any of them, i.e. late-binding and device- hotplug is disallowed after the initial open. I'm not sure how important such an usage would be, but it does cause user-tangible semantics change.

Quoted 6 lines

Yep, a high potential to break userspace, especially as pci-stub has been recommended for the cases noted above. I don't expect that tools like libvirt manage unassigned devices within a group, but that probably means that there are all sorts of ad-hoc user mechanisms beyond simply assigning all the devices. Thanks,

Thanks
Kevin

#90 lore raw

Re: [RFC 06/20] iommu: Add iommu_device_init[exit]_user_dma interfaces

Quoted 67 lines · Kevin
Quoted 56 lines

From: Alex Williamson <[email protected]>
Sent: Tuesday, September 28, 2021 3:20 AM

Quoted 28 lines · Kevin
Quoted 20 lines

From: Jason Gunthorpe
Sent: Monday, September 27, 2021 9:10 PM

Quoted 14 lines · Kevin
Quoted 3 lines

I think for such a narrow usage you should not change the struct device_driver. Just have pci_stub call a function to flip back to user mode.

Here we want to ensure that kernel dma should be blocked if the group is already marked for user-dma. If we just blindly do it for any driver at this point (as you commented earlier):

(quoted diff) +3 -0
+       ret = iommu_set_kernel_ownership(dev);
+       if (ret)
+               return ret;

how would pci-stub reach its function to indicate that it doesn't do dma and flip back?

Quoted 7 lines
Quoted 3 lines

Do you envision a simpler policy that no driver can be bound to the group if it's already set for user-dma? what about vfio-pci itself?

Yes.. I'm not sure there is a good use case to allow the stub drivers to load/unload while a VFIO is running. At least, not a strong enough one to justify a global change to the driver core..

Quoted 5 lines

I'm fine with not loading pci-stub. From the very 1st commit msg looks pci-stub was introduced before vfio to prevent host driver loading when doing device assignment with KVM. I'm not sure whether other usages are built on pci-stub later, but in general it's not good to position devices in a same group into different usages.

IIRC, pci-stub was invented for legacy KVM device assignment because KVM was never an actual device driver, it just latched onto and started using the device. If there was an existing driver for the device then KVM would fail to get device resources. Therefore the device needed to be unbound from its standard host driver, but that left it susceptible to driver loads usurping the device. Therefore pci-stub came along to essentially claim the device on behalf of KVM.

With vfio, there are a couple use cases of pci-stub that can be interesting. The first is that pci-stub is generally built into the kernel, not as a module, which provides users the ability to specify a list of ids for pci-stub to claim on the kernel command line with higher priority than loadable modules. This can prevent default driver bindings to devices until tools like driverctl or boot time scripting gets a shot to load the user designated driver for a device.

Quoted 10 lines

The other use case, is that if a group is composed of multiple devices and all those devices are bound to vfio drivers, then the user can gain direct access to each of those devices. If we wanted to insert a barrier to restrict user access to certain devices within a group, we'd suggest binding those devices to pci-stub. Obviously within a group, it may still be possible to manipulate the device via p2p DMA, but the barrier is much higher and device, if not platform, specific to manipulate such devices. An example use case might be a chipset Ethernet controller grouped among system management function in a multi-function root complex integrated endpoint.

Quoted 8 lines

Thanks for the background. It perfectly reflects how many tricky things that vfio has evolved to deal with and we'll dig them out again in this refactoring process with your help. 😊

just a nit on the last example. If a system management function is in such group, isn't the right policy is to disallow assigning any device in this group? Even the barrier is high, any chance of allowing the guest to control a system management function is dangerous...

We can advise that it's a risk, but we generally refrain from making such policy decisions. Ideally the chipset vendor avoids configurations that require their users to choose between functionality and security ;) Thanks,

Alex

maintainer
#91 lore raw

Re: [RFC 06/20] iommu: Add iommu_device_init[exit]_user_dma interfaces

In reply to Tian, Kevin

drivers/iommu/iommu.c void iommu_group_put(struct iommu_group *group) · line 1078

Quoted 12 lines · Kevin
drivers/iommu/iommu.c +12 -0
+static int iommu_dev_viable(struct device *dev, void *data)
+{
+	enum dma_hint hint = *data;
+	struct device_driver *drv = READ_ONCE(dev->driver);
+
+	/* no conflict if the new device doesn't do DMA */
+	if (hint == DMA_FOR_NONE)
+		return 0;
+
+	/* no conflict if this device is driver-less, or doesn't do DMA */
+	if (!drv || (drv->dma_hint == DMA_FOR_NONE))
+		return 0;

While it is kind of clever to fetch this in the drv like this, the locking just doesn't work right.

The group itself needs to have an atomic that encodes what state it is in. You can read the initial state from the drv, under the device_lock, and update the atomic state

Also, don't call it "hint", there is nothing hinty about this, it has definitive functional impacts.

Greg will want to see a definiate benefit from this extra global code, so be sure to explain about why the BUG_ON is bad, and how driver core involvement is needed to fix it properly.

Jason

maintainer
#92 lore raw

RE: [RFC 06/20] iommu: Add iommu_device_init[exit]_user_dma interfaces

Quoted 21 lines

From: Jason Gunthorpe <[email protected]>
Sent: Monday, September 27, 2021 11:09 PM

drivers/iommu/iommu.c void iommu_group_put(struct iommu_group *group) · line 1078

Quoted 12 lines · Kevin
drivers/iommu/iommu.c +12 -0
+static int iommu_dev_viable(struct device *dev, void *data)
+{
+	enum dma_hint hint = *data;
+	struct device_driver *drv = READ_ONCE(dev->driver);
+
+	/* no conflict if the new device doesn't do DMA */
+	if (hint == DMA_FOR_NONE)
+		return 0;
+
+	/* no conflict if this device is driver-less, or doesn't do DMA */
+	if (!drv || (drv->dma_hint == DMA_FOR_NONE))
+		return 0;

While it is kind of clever to fetch this in the drv like this, the locking just doesn't work right.

Quoted 3 lines

The group itself needs to have an atomic that encodes what state it is in. You can read the initial state from the drv, under the device_lock, and update the atomic state

will do.

Quoted 3 lines

Also, don't call it "hint", there is nothing hinty about this, it has definitive functional impacts.

possibly dma_mode (too broad?) or dma_usage

Quoted 5 lines

Greg will want to see a definiate benefit from this extra global code, so be sure to explain about why the BUG_ON is bad, and how driver core involvement is needed to fix it properly.

Sure. and I plan to at least have the patches aligned in this loop first, before rolling up to Greg. Better we all confirm it's the right approach with all corner cases covered and then involve Greg to help judge a clean driver core change. 😊

Thanks
Kevin

maintainer
#93 lore raw

Re: [RFC 06/20] iommu: Add iommu_device_init[exit]_user_dma interfaces

Quoted 4 lines · Kevin
Quoted 2 lines

Also, don't call it "hint", there is nothing hinty about this, it has definitive functional impacts.

possibly dma_mode (too broad?) or dma_usage

You just need a flag to specify if the driver manages DMA ownership itself, or if it requires the driver core to setup kernel ownership

DMA_OWNER_KERNEL
DMA_OWNER_DRIVER_CONTROLLED

?

There is a bool 'suprress_bind_attrs' already so it could be done like this:

bool suppress_bind_attrs:1;

/* If set the driver must call iommu_XX as the first action in probe() */ bool suppress_dma_owner:1;

Which is pretty low cost.

Jason

maintainer
#94 lore raw

Re: [RFC 06/20] iommu: Add iommu_device_init[exit]_user_dma interfaces

Hi Jason,

Quoted 18 lines · Jason Gunthorpe
Quoted 4 lines · Kevin
Quoted 2 lines

Also, don't call it "hint", there is nothing hinty about this, it has definitive functional impacts.

possibly dma_mode (too broad?) or dma_usage

You just need a flag to specify if the driver manages DMA ownership itself, or if it requires the driver core to setup kernel ownership

DMA_OWNER_KERNEL
DMA_OWNER_DRIVER_CONTROLLED

?

There is a bool 'suprress_bind_attrs' already so it could be done like this:

Quoted 6 lines

bool suppress_bind_attrs:1;

/* If set the driver must call iommu_XX as the first action in probe() */ bool suppress_dma_owner:1;

Which is pretty low cost.

Yes. Pretty low cost to fix the BUG_ON() issue. Any kernel-DMA driver binding is blocked if the device's iommu group has been put into user- dma mode.

Another issue is, when putting a device into user-dma mode, all devices belonging to the same iommu group shouldn't be bound with a kernel-dma driver. Kevin's prototype checks this by READ_ONCE(dev->driver). This is not lock safe as discussed below,

https://lore.kernel.org/linux-iommu/[email protected]/

Any guidance on this?

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

maintainer
#95 lore raw

Re: [RFC 06/20] iommu: Add iommu_device_init[exit]_user_dma interfaces

Quoted 8 lines · Lu Baolu

Another issue is, when putting a device into user-dma mode, all devices belonging to the same iommu group shouldn't be bound with a kernel-dma driver. Kevin's prototype checks this by READ_ONCE(dev->driver). This is not lock safe as discussed below,

https://lore.kernel.org/linux-iommu/[email protected]/

Any guidance on this?

Something like this?

int iommu_set_device_dma_owner(struct device *dev, enum device_dma_owner mode,
struct file *user_owner)

{
	struct iommu_group *group = group_from_dev(dev);

	spin_lock(&iommu_group->dma_owner_lock);
	switch (mode) {
		case DMA_OWNER_KERNEL:
			if (iommu_group->dma_users[DMA_OWNER_USERSPACE])
				return -EBUSY;
			break;
		case DMA_OWNER_SHARED:
			break;
		case DMA_OWNER_USERSPACE:
			if (iommu_group->dma_users[DMA_OWNER_KERNEL])
				return -EBUSY;
			if (iommu_group->dma_owner_file != user_owner) {
				if (iommu_group->dma_users[DMA_OWNER_USERSPACE])
					return -EPERM;
				get_file(user_owner);
				iommu_group->dma_owner_file = user_owner;
			}
			break;
		default:
			spin_unlock(&iommu_group->dma_owner_lock);
			return -EINVAL;
	}
	iommu_group->dma_users[mode]++;
	spin_unlock(&iommu_group->dma_owner_lock);
	return 0;
}

int iommu_release_device_dma_owner(struct device *dev,
enum device_dma_owner mode)

{
	struct iommu_group *group = group_from_dev(dev);

	spin_lock(&iommu_group->dma_owner_lock);
	if (WARN_ON(!iommu_group->dma_users[mode]))
		goto err_unlock;
	if (!iommu_group->dma_users[mode]--) {
		if (mode == DMA_OWNER_USERSPACE) {
			fput(iommu_group->dma_owner_file);
			iommu_group->dma_owner_file = NULL;
		}
	}
err_unlock:
	spin_unlock(&iommu_group->dma_owner_lock);
}

Where, the driver core does before probe:

iommu_set_device_dma_owner(dev, DMA_OWNER_KERNEL, NULL)

pci_stub/etc does in their probe func:

iommu_set_device_dma_owner(dev, DMA_OWNER_SHARED, NULL)

And vfio/iommfd does when a struct vfio_device FD is attached:

iommu_set_device_dma_owner(dev, DMA_OWNER_USERSPACE, group_file/iommu_file)

Jason

maintainer
#96 lore raw

RE: [RFC 06/20] iommu: Add iommu_device_init[exit]_user_dma interfaces

Quoted 9 lines

From: Jason Gunthorpe <[email protected]>
Sent: Tuesday, September 28, 2021 10:07 PM

Quoted 5 lines · Lu Baolu

Another issue is, when putting a device into user-dma mode, all devices belonging to the same iommu group shouldn't be bound with a kernel-dma driver. Kevin's prototype checks this by READ_ONCE(dev->driver). This is not lock safe as discussed below,

Quoted 8 lines
Quoted 1 line

https://lore.kernel.org/linux-

iommu/[email protected]/

Quoted 2 lines

Any guidance on this?

Something like this?

yes, with this group level atomics we don't need loop every dev->driver respectively.

Quoted 60 lines

int iommu_set_device_dma_owner(struct device *dev, enum device_dma_owner mode,
struct file *user_owner)

{
	struct iommu_group *group = group_from_dev(dev);

	spin_lock(&iommu_group->dma_owner_lock);
	switch (mode) {
		case DMA_OWNER_KERNEL:
			if (iommu_group-
Quoted 1 line

dma_users[DMA_OWNER_USERSPACE])

				return -EBUSY;
			break;
		case DMA_OWNER_SHARED:
			break;
		case DMA_OWNER_USERSPACE:
			if (iommu_group-
Quoted 1 line

dma_users[DMA_OWNER_KERNEL])

				return -EBUSY;
			if (iommu_group->dma_owner_file != user_owner) {
				if (iommu_group-
Quoted 1 line

dma_users[DMA_OWNER_USERSPACE])

					return -EPERM;
				get_file(user_owner);
				iommu_group->dma_owner_file =
user_owner;
			}
			break;
		default:
			spin_unlock(&iommu_group->dma_owner_lock);
			return -EINVAL;
	}
	iommu_group->dma_users[mode]++;
	spin_unlock(&iommu_group->dma_owner_lock);
	return 0;
}

int iommu_release_device_dma_owner(struct device *dev,
enum device_dma_owner mode)

{
	struct iommu_group *group = group_from_dev(dev);

	spin_lock(&iommu_group->dma_owner_lock);
	if (WARN_ON(!iommu_group->dma_users[mode]))
		goto err_unlock;
	if (!iommu_group->dma_users[mode]--) {
		if (mode == DMA_OWNER_USERSPACE) {
			fput(iommu_group->dma_owner_file);
			iommu_group->dma_owner_file = NULL;
		}
	}
err_unlock:
	spin_unlock(&iommu_group->dma_owner_lock);
}

Where, the driver core does before probe:

iommu_set_device_dma_owner(dev, DMA_OWNER_KERNEL, NULL)

Quoted 9 lines

pci_stub/etc does in their probe func:

iommu_set_device_dma_owner(dev, DMA_OWNER_SHARED, NULL)

And vfio/iommfd does when a struct vfio_device FD is attached:

iommu_set_device_dma_owner(dev, DMA_OWNER_USERSPACE,
group_file/iommu_file)

Just a nit. Per your comment in previous mail:

/* If set the driver must call iommu_XX as the first action in probe() */
bool suppress_dma_owner:1;

Following above logic userspace drivers won't call iommu_XX in probe(). Just want to double confirm whether you see any issue here with this relaxed behavior. If no problem:

/* If set the driver must call iommu_XX as the first action in probe() or
* before it attempts to do DMA
*/
bool suppress_dma_owner:1;

Thanks
Kevin

maintainer
#97 lore raw

Re: [RFC 06/20] iommu: Add iommu_device_init[exit]_user_dma interfaces

Quoted 4 lines · Kevin

/* If set the driver must call iommu_XX as the first action in probe() or
* before it attempts to do DMA
*/
bool suppress_dma_owner:1;

It is not "attempts to do DMA" but more "operates the physical device in any away"

Not having ownership means another entity could be using user space DMA to manipulate the device state and attack the integrity of the kernel's programming of the device.

Jason

maintainer
#98 lore raw

RE: [RFC 06/20] iommu: Add iommu_device_init[exit]_user_dma interfaces

Hi, Jason,

Quoted 10 lines

From: Jason Gunthorpe <[email protected]>
Sent: Wednesday, September 29, 2021 8:59 PM

Quoted 4 lines · Kevin

/* If set the driver must call iommu_XX as the first action in probe() or
* before it attempts to do DMA
*/
bool suppress_dma_owner:1;

Quoted 7 lines

It is not "attempts to do DMA" but more "operates the physical device in any away"

Not having ownership means another entity could be using user space DMA to manipulate the device state and attack the integrity of the kernel's programming of the device.

Does suppress_kernel_dma sounds better than suppress_dma_owner? We found the latter causing some confusion when doing internal code review. Somehow this flag represents "don't claim the kernel dma ownership during driver binding". suppress_dma_owner sounds the entire ownership is disabled...

Another thing is about DMA_OWNER_SHARED, which is set to indicate no dma at all. Thinking more we feel that this flag is meaningless. Its sole purpose is to show compatibility to any USER/KERNEL ownership, and essentially the same semantics as a device which is not bound to any driver. So we plan to remove it then pci-stub just needs one line change to set the suppress flag. But want to check with you first in case any oversight.

Thanks
Kevin

maintainer
#99 lore raw

Re: [RFC 06/20] iommu: Add iommu_device_init[exit]_user_dma interfaces

Quoted 20 lines · Kevin

Hi, Jason,

Quoted 10 lines

From: Jason Gunthorpe <[email protected]>
Sent: Wednesday, September 29, 2021 8:59 PM

Quoted 4 lines · Kevin

/* If set the driver must call iommu_XX as the first action in probe() or
* before it attempts to do DMA
*/
bool suppress_dma_owner:1;

Quoted 7 lines

It is not "attempts to do DMA" but more "operates the physical device in any away"

Not having ownership means another entity could be using user space DMA to manipulate the device state and attack the integrity of the kernel's programming of the device.

Quoted 5 lines

Does suppress_kernel_dma sounds better than suppress_dma_owner? We found the latter causing some confusion when doing internal code review. Somehow this flag represents "don't claim the kernel dma ownership during driver binding". suppress_dma_owner sounds the entire ownership is disabled...

If in doubt make it

suppress_iommu_whatever_the_api_is_that_isn't_called

Quoted 7 lines

Another thing is about DMA_OWNER_SHARED, which is set to indicate no dma at all. Thinking more we feel that this flag is meaningless. Its sole purpose is to show compatibility to any USER/KERNEL ownership, and essentially the same semantics as a device which is not bound to any driver. So we plan to remove it then pci-stub just needs one line change to set the suppress flag. But want to check with you first in case any oversight.

It sounds reasonable, but also makes it much harder to find the few places that have this special relationship - ie we can't grep for DMA_OWNER_SHARED anymore.

Jason

maintainer
#100 lore raw

RE: [RFC 06/20] iommu: Add iommu_device_init[exit]_user_dma interfaces

Quoted 30 lines

From: Jason Gunthorpe <[email protected]>
Sent: Friday, October 15, 2021 7:10 PM

Quoted 20 lines · Kevin

Hi, Jason,

Quoted 10 lines

From: Jason Gunthorpe <[email protected]>
Sent: Wednesday, September 29, 2021 8:59 PM

Quoted 4 lines · Kevin

/* If set the driver must call iommu_XX as the first action in probe() or
* before it attempts to do DMA
*/
bool suppress_dma_owner:1;

Quoted 7 lines

It is not "attempts to do DMA" but more "operates the physical device in any away"

Not having ownership means another entity could be using user space DMA to manipulate the device state and attack the integrity of the kernel's programming of the device.

Quoted 5 lines

Does suppress_kernel_dma sounds better than suppress_dma_owner? We found the latter causing some confusion when doing internal code review. Somehow this flag represents "don't claim the kernel dma ownership during driver binding". suppress_dma_owner sounds the entire ownership is disabled...

Quoted 3 lines

If in doubt make it

suppress_iommu_whatever_the_api_is_that_isn't_called

ok

Quoted 9 lines
Quoted 8 lines

Another thing is about DMA_OWNER_SHARED, which is set to indicate no dma at all. Thinking more we feel that this flag is meaningless. Its sole purpose is to show compatibility to any USER/KERNEL ownership, and essentially the same semantics as a device which is not bound to any driver. So we plan to remove it then pci-stub just needs one line change to set the suppress flag. But want to check with you first in case any oversight.

Quoted 4 lines

It sounds reasonable, but also makes it much harder to find the few places that have this special relationship - ie we can't grep for DMA_OWNER_SHARED anymore.

It's probably fine. People can just search the suppress flag and filter out drivers with DMA_OWNER_USER. Then the remaining set is about drivers in SHARED category. Less straightforward but should be fine for a relatively small set of drivers.

Thanks
Kevin

maintainer
#101 lore raw

Re: [RFC 06/20] iommu: Add iommu_device_init[exit]_user_dma interfaces

In reply to Jason Gunthorpe
Quoted 68 lines · Jason Gunthorpe
Quoted 8 lines · Lu Baolu

Another issue is, when putting a device into user-dma mode, all devices belonging to the same iommu group shouldn't be bound with a kernel-dma driver. Kevin's prototype checks this by READ_ONCE(dev->driver). This is not lock safe as discussed below,

https://lore.kernel.org/linux-iommu/[email protected]/

Any guidance on this?

Something like this?

int iommu_set_device_dma_owner(struct device *dev, enum device_dma_owner mode,
struct file *user_owner)

{
	struct iommu_group *group = group_from_dev(dev);

	spin_lock(&iommu_group->dma_owner_lock);
	switch (mode) {
		case DMA_OWNER_KERNEL:
			if (iommu_group->dma_users[DMA_OWNER_USERSPACE])
				return -EBUSY;
			break;
		case DMA_OWNER_SHARED:
			break;
		case DMA_OWNER_USERSPACE:
			if (iommu_group->dma_users[DMA_OWNER_KERNEL])
				return -EBUSY;
			if (iommu_group->dma_owner_file != user_owner) {
				if (iommu_group->dma_users[DMA_OWNER_USERSPACE])
					return -EPERM;
				get_file(user_owner);
				iommu_group->dma_owner_file = user_owner;
			}
			break;
		default:
			spin_unlock(&iommu_group->dma_owner_lock);
			return -EINVAL;
	}
	iommu_group->dma_users[mode]++;
	spin_unlock(&iommu_group->dma_owner_lock);
	return 0;
}

int iommu_release_device_dma_owner(struct device *dev,
enum device_dma_owner mode)

{
	struct iommu_group *group = group_from_dev(dev);

	spin_lock(&iommu_group->dma_owner_lock);
	if (WARN_ON(!iommu_group->dma_users[mode]))
		goto err_unlock;
	if (!iommu_group->dma_users[mode]--) {
		if (mode == DMA_OWNER_USERSPACE) {
			fput(iommu_group->dma_owner_file);
			iommu_group->dma_owner_file = NULL;
		}
	}
err_unlock:
	spin_unlock(&iommu_group->dma_owner_lock);
}

Where, the driver core does before probe:

iommu_set_device_dma_owner(dev, DMA_OWNER_KERNEL, NULL)

Quoted 7 lines

pci_stub/etc does in their probe func:

iommu_set_device_dma_owner(dev, DMA_OWNER_SHARED, NULL)

And vfio/iommfd does when a struct vfio_device FD is attached:

iommu_set_device_dma_owner(dev, DMA_OWNER_USERSPACE, group_file/iommu_file)

Really good design. It also helps alleviating some pains elsewhere in the iommu core.

Just a nit comment, we also need DMA_OWNER_NONE which will be set when the driver core unbinds the driver from the device.

Quoted 3 lines

Jason

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

maintainer
#102 lore raw

RE: [RFC 06/20] iommu: Add iommu_device_init[exit]_user_dma interfaces

Quoted 86 lines

From: Lu Baolu <[email protected]>
Sent: Wednesday, September 29, 2021 10:22 AM

Quoted 3 lines · Jason Gunthorpe
Quoted 2 lines · Lu Baolu

Another issue is, when putting a device into user-dma mode, all devices belonging to the same iommu group shouldn't be bound with a kernel-

dma

Quoted 4 lines
Quoted 4 lines

driver. Kevin's prototype checks this by READ_ONCE(dev->driver). This is not lock safe as discussed below,

https://lore.kernel.org/linux-

iommu/[email protected]/

Quoted 7 lines
Quoted 2 lines

Any guidance on this?

Something like this?

int iommu_set_device_dma_owner(struct device *dev, enum

device_dma_owner mode,

Quoted 15 lines

struct file *user_owner)

{
	struct iommu_group *group = group_from_dev(dev);

	spin_lock(&iommu_group->dma_owner_lock);
	switch (mode) {
		case DMA_OWNER_KERNEL:
			if (iommu_group-
dma_users[DMA_OWNER_USERSPACE])
				return -EBUSY;
			break;
		case DMA_OWNER_SHARED:
			break;
		case DMA_OWNER_USERSPACE:
			if (iommu_group-
Quoted 8 lines

dma_users[DMA_OWNER_KERNEL])

				return -EBUSY;
			if (iommu_group->dma_owner_file != user_owner) {
				if (iommu_group-
dma_users[DMA_OWNER_USERSPACE])
					return -EPERM;
				get_file(user_owner);
				iommu_group->dma_owner_file =

user_owner;

Quoted 34 lines
			}
			break;
		default:
			spin_unlock(&iommu_group->dma_owner_lock);
			return -EINVAL;
	}
	iommu_group->dma_users[mode]++;
	spin_unlock(&iommu_group->dma_owner_lock);
	return 0;
}

int iommu_release_device_dma_owner(struct device *dev,
enum device_dma_owner mode)

{
	struct iommu_group *group = group_from_dev(dev);

	spin_lock(&iommu_group->dma_owner_lock);
	if (WARN_ON(!iommu_group->dma_users[mode]))
		goto err_unlock;
	if (!iommu_group->dma_users[mode]--) {
		if (mode == DMA_OWNER_USERSPACE) {
			fput(iommu_group->dma_owner_file);
			iommu_group->dma_owner_file = NULL;
		}
	}
err_unlock:
	spin_unlock(&iommu_group->dma_owner_lock);
}

Where, the driver core does before probe:

iommu_set_device_dma_owner(dev, DMA_OWNER_KERNEL, NULL)

Quoted 7 lines

pci_stub/etc does in their probe func:

iommu_set_device_dma_owner(dev, DMA_OWNER_SHARED, NULL)

And vfio/iommfd does when a struct vfio_device FD is attached:

iommu_set_device_dma_owner(dev, DMA_OWNER_USERSPACE,

Quoted 9 lines

group_file/iommu_file)

Really good design. It also helps alleviating some pains elsewhere in the iommu core.

Just a nit comment, we also need DMA_OWNER_NONE which will be set when
the driver core unbinds the driver from the device.

Not necessarily. NONE is represented by none of dma_user[mode] is valid.

maintainer
#103 lore raw

Re: [RFC 06/20] iommu: Add iommu_device_init[exit]_user_dma interfaces

Quoted 91 lines · Kevin
Quoted 91 lines

From: Lu Baolu <[email protected]>
Sent: Wednesday, September 29, 2021 10:22 AM

Quoted 3 lines · Jason Gunthorpe
Quoted 2 lines · Lu Baolu

Another issue is, when putting a device into user-dma mode, all devices belonging to the same iommu group shouldn't be bound with a kernel-

dma

Quoted 4 lines
Quoted 4 lines

driver. Kevin's prototype checks this by READ_ONCE(dev->driver). This is not lock safe as discussed below,

https://lore.kernel.org/linux-

iommu/[email protected]/

Quoted 7 lines
Quoted 2 lines

Any guidance on this?

Something like this?

int iommu_set_device_dma_owner(struct device *dev, enum

device_dma_owner mode,

Quoted 15 lines

struct file *user_owner)

{
	struct iommu_group *group = group_from_dev(dev);

	spin_lock(&iommu_group->dma_owner_lock);
	switch (mode) {
		case DMA_OWNER_KERNEL:
			if (iommu_group-
dma_users[DMA_OWNER_USERSPACE])
				return -EBUSY;
			break;
		case DMA_OWNER_SHARED:
			break;
		case DMA_OWNER_USERSPACE:
			if (iommu_group-
Quoted 8 lines

dma_users[DMA_OWNER_KERNEL])

				return -EBUSY;
			if (iommu_group->dma_owner_file != user_owner) {
				if (iommu_group-
dma_users[DMA_OWNER_USERSPACE])
					return -EPERM;
				get_file(user_owner);
				iommu_group->dma_owner_file =

user_owner;

Quoted 34 lines
			}
			break;
		default:
			spin_unlock(&iommu_group->dma_owner_lock);
			return -EINVAL;
	}
	iommu_group->dma_users[mode]++;
	spin_unlock(&iommu_group->dma_owner_lock);
	return 0;
}

int iommu_release_device_dma_owner(struct device *dev,
enum device_dma_owner mode)

{
	struct iommu_group *group = group_from_dev(dev);

	spin_lock(&iommu_group->dma_owner_lock);
	if (WARN_ON(!iommu_group->dma_users[mode]))
		goto err_unlock;
	if (!iommu_group->dma_users[mode]--) {
		if (mode == DMA_OWNER_USERSPACE) {
			fput(iommu_group->dma_owner_file);
			iommu_group->dma_owner_file = NULL;
		}
	}
err_unlock:
	spin_unlock(&iommu_group->dma_owner_lock);
}

Where, the driver core does before probe:

iommu_set_device_dma_owner(dev, DMA_OWNER_KERNEL, NULL)

Quoted 7 lines

pci_stub/etc does in their probe func:

iommu_set_device_dma_owner(dev, DMA_OWNER_SHARED, NULL)

And vfio/iommfd does when a struct vfio_device FD is attached:

iommu_set_device_dma_owner(dev, DMA_OWNER_USERSPACE,

group_file/iommu_file)

Really good design. It also helps alleviating some pains elsewhere in the iommu core.

Quoted 8 lines
Quoted 4 lines

Just a nit comment, we also need DMA_OWNER_NONE which will be set when
the driver core unbinds the driver from the device.

Not necessarily. NONE is represented by none of dma_user[mode] is valid.

Fair enough.

Best regards,
baolu

#104 lore raw

Re: [RFC 06/20] iommu: Add iommu_device_init[exit]_user_dma interfaces

In reply to Liu Yi L
Quoted 39 lines · Liu Yi L

From: Lu Baolu <[email protected]>

This extends iommu core to manage security context for passthrough devices. Please bear a long explanation for how we reach this design instead of managing it solely in iommufd like what vfio does today.

Devices which cannot be isolated from each other are organized into an iommu group. When a device is assigned to the user space, the entire group must be put in a security context so that user-initiated DMAs via the assigned device cannot harm the rest of the system. No user access should be granted on a device before the security context is established for the group which the device belongs to.

Managing the security context must meet below criteria:

1) The group is viable for user-initiated DMAs. This implies that the devices in the group must be either bound to a device-passthrough framework, or driver-less, or bound to a driver which is known safe (not do DMA).

2) The security context should only allow DMA to the user's memory and

    devices in this group;

3) After the security context is established for the group, the group viability must be continuously monitored before the user relinquishes all devices belonging to the group. The viability might be broken e.g. when a driver-less device is later bound to a driver which does DMA.

4) The security context should not be destroyed before user access permission is withdrawn.

Existing vfio introduces explicit container/group semantics in its uAPI to meet above requirements. A single security context (iommu domain) is created per container. Attaching group to container moves the entire group into the associated security context, and vice versa. The user can open the device only after group attach. A group can be detached only after all devices in the group are closed. Group viability is monitored by listening to iommu group events.

Quoted 7 lines

Unlike vfio, iommufd adopts a device-centric design with all group logistics hidden behind the fd. Binding a device to iommufd serves as the contract to get security context established (and vice versa for unbinding). One additional requirement in iommufd is to manage the switch between multiple security contexts due to decoupled bind/attach:

1) Open a device in "/dev/vfio/devices" with user access blocked;

Probably worth clarifying that (1) must happen for *all* devices in the group before (2) happens for any device in the group.

Quoted 7 lines

2) Bind the device to an iommufd with an initial security context (an empty iommu domain which blocks dma) established for its group, with user access unblocked;

3) Attach the device to a user-specified ioasid (shared by all devices attached to this ioasid). Before attaching, the device should be first detached from the initial context;

So, this step can implicitly but observably change the behaviour for other devices in the group as well. I don't love that kind of difficult to predict side effect, which is why I'm *still* not totally convinced by the device-centric model.

Quoted 2 lines

4) Detach the device from the ioasid and switch it back to the initial

    security context;

Same non-local side effect at this step, of course.

Btw, explicitly naming the "no DMA" context is probably a good idea, rather than referring to the "initial security context" (it's "initial" from the PoV of the iommufd, but not from the PoV of the device fd which was likely bound to the default kernel context before (2)).

Quoted 3 lines

5) Unbind the device from the iommufd, back to access blocked state and move its group out of the initial security context if it's the last unbound device in the group;

Maybe worth clarifying that again (5) must happen for all devices in the group before rebiding any devices to regular kernel drivers.

Quoted 49 lines

(multiple attach/detach could happen between 2 and 5).

However existing iommu core has problem with above transition. Detach in step 3/4 makes the device/group re-attached to the default domain automatically, which opens the door for user-initiated DMAs to attack the rest of the system. The existing vfio doesn't have this problem as it combines 2/3 in one step (so does 4/5).

Fixing this problem requires the iommu core to also participate in the security context management. Following this direction we also move group viability check into the iommu core, which allows iommufd to stay fully device-centric w/o keeping any group knowledge (combining with the extension to iommu_at[de]tach_device() in a latter patch).

Basically two new interfaces are provided:

        int iommu_device_init_user_dma(struct device *dev,
                        unsigned long owner);
        void iommu_device_exit_user_dma(struct device *dev);

iommufd calls them respectively when handling device binding/unbinding requests.

The init_user_dma() for the 1st device in a group marks the entire group for user-dma and establishes the initial security context (dma blocked) according to aforementioned criteria. As long as the group is marked for user-dma, auto-reattaching to default domain is disabled. Instead, upon detaching the group is moved back to the initial security context.

The caller also provides an owner id to mark the ownership so inadvertent attempt from another caller on the same device can be captured. In this RFC iommufd will use the fd context pointer as the owner id.

The exit_user_dma() for the last device in the group clears the user-dma mark and moves the group back to the default domain.

---
 drivers/iommu/iommu.c | 145 +++++++++++++++++++++++++++++++++++++++++-
 include/linux/iommu.h |  12 ++++
 2 files changed, 154 insertions(+), 3 deletions(-)
drivers/iommu/iommu.c +0 -0
index 5ea3a007fd7c..bffd84e978fb 100644

drivers/iommu/iommu.c struct iommu_group { · line 48

Quoted 4 lines
drivers/iommu/iommu.c +1 -0
@@ -45,6 +45,8 @@ struct iommu_group {
 	struct iommu_domain *default_domain;
 	struct iommu_domain *domain;
 	struct list_head entry;
+	unsigned long user_dma_owner_id;

Using an opaque integer doesn't seem like a good idea. I think you probably want a pointer to a suitable struct dma_owner or the like (you could have one embedded in each iommufd struct, plus a global static kernel_default_owner).

Quoted 87 lines
drivers/iommu/iommu.c +42 -3
+	refcount_t owner_cnt;
 };
 
 struct group_device {
@@ -86,6 +88,7 @@ static int iommu_create_device_direct_mappings(struct iommu_group *group,
 static struct iommu_group *iommu_group_get_for_dev(struct device *dev);
 static ssize_t iommu_group_store_type(struct iommu_group *group,
 				      const char *buf, size_t count);
+static bool iommu_group_user_dma_viable(struct iommu_group *group);
 
 #define IOMMU_GROUP_ATTR(_name, _mode, _show, _store)		\
 struct iommu_group_attribute iommu_group_attr_##_name =		\
@@ -275,7 +278,11 @@ int iommu_probe_device(struct device *dev)
 	 */
 	iommu_alloc_default_domain(group, dev);
 
-	if (group->default_domain) {
+	/*
+	 * If any device in the group has been initialized for user dma,
+	 * avoid attaching the default domain.
+	 */
+	if (group->default_domain && !group->user_dma_owner_id) {
 		ret = __iommu_attach_device(group->default_domain, dev);
 		if (ret) {
 			iommu_group_put(group);
@@ -1664,6 +1671,17 @@ static int iommu_bus_notifier(struct notifier_block *nb,
 		group_action = IOMMU_GROUP_NOTIFY_BIND_DRIVER;
 		break;
 	case BUS_NOTIFY_BOUND_DRIVER:
+		/*
+		 * FIXME: Alternatively the attached drivers could generically
+		 * indicate to the iommu layer that they are safe for keeping
+		 * the iommu group user viable by calling some function around
+		 * probe(). We could eliminate this gross BUG_ON() by denying
+		 * probe to non-iommu-safe driver.
+		 */
+		mutex_lock(&group->mutex);
+		if (group->user_dma_owner_id)
+			BUG_ON(!iommu_group_user_dma_viable(group));
+		mutex_unlock(&group->mutex);
 		group_action = IOMMU_GROUP_NOTIFY_BOUND_DRIVER;
 		break;
 	case BUS_NOTIFY_UNBIND_DRIVER:
@@ -2304,7 +2322,11 @@ static int __iommu_attach_group(struct iommu_domain *domain,
 {
 	int ret;
 
-	if (group->default_domain && group->domain != group->default_domain)
+	/*
+	 * group->domain could be NULL when a domain is detached from the
+	 * group but the default_domain is not re-attached.
+	 */
+	if (group->domain && group->domain != group->default_domain)
 		return -EBUSY;
 
 	ret = __iommu_group_for_each_dev(group, domain,
@@ -2341,7 +2363,11 @@ static void __iommu_detach_group(struct iommu_domain *domain,
 {
 	int ret;
 
-	if (!group->default_domain) {
+	/*
+	 * If any device in the group has been initialized for user dma,
+	 * avoid re-attaching the default domain.
+	 */
+	if (!group->default_domain || group->user_dma_owner_id) {
 		__iommu_group_for_each_dev(group, domain,
 					   iommu_group_do_detach_device);
 		group->domain = NULL;
@@ -3276,3 +3302,116 @@ int iommu_device_get_info(struct device *dev, enum iommu_devattr attr, void *dat
 	return ops->device_info(dev, attr, data);
 }
 EXPORT_SYMBOL_GPL(iommu_device_get_info);
+
+/*
+ * IOMMU core interfaces for iommufd.
+ */
+
+/*
+ * FIXME: We currently simply follow vifo policy to mantain the group's
+ * viability to user. Eventually, we should avoid below hard-coded list
+ * by letting drivers indicate to the iommu layer that they are safe for
+ * keeping the iommu group's user aviability.
+ */
+static const char * const iommu_driver_allowed[] = {
+	"vfio-pci",
+	"pci-stub"

drivers/iommu/iommu.c int iommu_device_get_info(struct device *dev, enum iommu_devattr attr, void *dat · line 3328

Quoted 10 lines
drivers/iommu/iommu.c +10 -0
+};
+
+/*
+ * An iommu group is viable for use by userspace if all devices are in
+ * one of the following states:
+ *  - driver-less
+ *  - bound to an allowed driver
+ *  - a PCI interconnect device
+ */
+static int device_user_dma_viable(struct device *dev, void *data)

I think this wants a "less friendly" more obviously local name. Really the only safe way to call this is via iommu_group_user_dma_viable(), which isn't obvious from this name.

Quoted 17 lines
drivers/iommu/iommu.c +17 -0
+{
+	struct device_driver *drv = READ_ONCE(dev->driver);
+
+	if (!drv)
+		return 0;
+
+	if (dev_is_pci(dev)) {
+		struct pci_dev *pdev = to_pci_dev(dev);
+
+		if (pdev->hdr_type != PCI_HEADER_TYPE_NORMAL)
+			return 0;
+	}
+
+	return match_string(iommu_driver_allowed,
+			    ARRAY_SIZE(iommu_driver_allowed),
+			    drv->name) < 0;
+}
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
maintainer
#105 lore raw

RE: [RFC 06/20] iommu: Add iommu_device_init[exit]_user_dma interfaces

Quoted 10 lines

From: David Gibson <[email protected]>
Sent: Wednesday, September 29, 2021 12:56 PM

Quoted 7 lines

Unlike vfio, iommufd adopts a device-centric design with all group logistics hidden behind the fd. Binding a device to iommufd serves as the contract to get security context established (and vice versa for unbinding). One additional requirement in iommufd is to manage the switch between multiple security contexts due to decoupled bind/attach:

Quoted 4 lines
Quoted 1 line

1) Open a device in "/dev/vfio/devices" with user access blocked;

Probably worth clarifying that (1) must happen for *all* devices in the group before (2) happens for any device in the group.

No. User access is naturally blocked for other devices as long as they are not opened yet.

Quoted 5 lines
Quoted 5 lines

2) Bind the device to an iommufd with an initial security context (an empty iommu domain which blocks dma) established for its group, with user access unblocked;

Quoted 8 lines
Quoted 3 lines

3) Attach the device to a user-specified ioasid (shared by all devices attached to this ioasid). Before attaching, the device should be first detached from the initial context;

So, this step can implicitly but observably change the behaviour for other devices in the group as well. I don't love that kind of difficult to predict side effect, which is why I'm *still* not totally convinced by the device-centric model.

which side-effect is predicted here? The user anyway needs to be aware of such group restriction regardless whether it uses group or nongroup interface.

Quoted 16 lines

drivers/iommu/iommu.c struct iommu_group { · line 48

Quoted 10 lines
drivers/iommu/iommu.c +1 -0
index 5ea3a007fd7c..bffd84e978fb 100644
@@ -45,6 +45,8 @@ struct iommu_group {
 	struct iommu_domain *default_domain;
 	struct iommu_domain *domain;
 	struct list_head entry;
+	unsigned long user_dma_owner_id;

Using an opaque integer doesn't seem like a good idea. I think you probably want a pointer to a suitable struct dma_owner or the like (you could have one embedded in each iommufd struct, plus a global static kernel_default_owner).

For remaining comments you may want to look at the latest discussion here:

https://lore.kernel.org/kvm/[email protected]/

It relies on driver core change to manage group ownership gracefully. No BUG_ON() is triggered any more for driver binding. There a fd will be passed in to mark the ownership.

Thanks
Kevin

#106 lore raw

Re: [RFC 06/20] iommu: Add iommu_device_init[exit]_user_dma interfaces

Quoted 10 lines · Kevin
Quoted 10 lines

From: David Gibson <[email protected]>
Sent: Wednesday, September 29, 2021 12:56 PM

Quoted 7 lines

Unlike vfio, iommufd adopts a device-centric design with all group logistics hidden behind the fd. Binding a device to iommufd serves as the contract to get security context established (and vice versa for unbinding). One additional requirement in iommufd is to manage the switch between multiple security contexts due to decoupled bind/attach:

Quoted 7 lines
Quoted 4 lines
Quoted 1 line

1) Open a device in "/dev/vfio/devices" with user access blocked;

Probably worth clarifying that (1) must happen for *all* devices in the group before (2) happens for any device in the group.

No. User access is naturally blocked for other devices as long as they are not opened yet.

Uh... my point is that everything in the group has to be removed from regular kernel drivers before we reach step (2). Is the plan that you must do that before you can even open them? That's a reasonable choice, but then I think you should show that step in this description as well.

Quoted 8 lines
Quoted 8 lines
Quoted 7 lines

2) Bind the device to an iommufd with an initial security context (an empty iommu domain which blocks dma) established for its group, with user access unblocked;

3) Attach the device to a user-specified ioasid (shared by all devices attached to this ioasid). Before attaching, the device should be first detached from the initial context;

Quoted 8 lines
Quoted 4 lines

So, this step can implicitly but observably change the behaviour for other devices in the group as well. I don't love that kind of difficult to predict side effect, which is why I'm *still* not totally convinced by the device-centric model.

which side-effect is predicted here? The user anyway needs to be aware of such group restriction regardless whether it uses group or nongroup interface.

Yes, exactly. And with a group interface it's obvious it has to understand it. With the non-group interface, you can get to this stage in ignorance of groups. It will even work as long as you are lucky enough only to try with singleton-group devices. Then you try it with two devices in the one group and doing (3) on device A will implicitly change the DMA environment of device B.

(or at least, it will if they share a group because they don't have distinguishable RIDs. That's not the only multi-device group case, but it's one of them).

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
maintainer
#107 lore raw

RE: [RFC 06/20] iommu: Add iommu_device_init[exit]_user_dma interfaces

Quoted 21 lines

From: David Gibson
Sent: Wednesday, September 29, 2021 2:35 PM

Quoted 8 lines · Kevin
Quoted 8 lines

From: David Gibson <[email protected]>
Sent: Wednesday, September 29, 2021 12:56 PM

Quoted 5 lines

Unlike vfio, iommufd adopts a device-centric design with all group logistics hidden behind the fd. Binding a device to iommufd serves as the contract to get security context established (and vice versa for unbinding). One additional requirement in iommufd is to manage

the

Quoted 1 line
Quoted 1 line
Quoted 1 line

switch between multiple security contexts due to decoupled

bind/attach:

Quoted 6 lines
Quoted 5 lines
Quoted 2 lines

1) Open a device in "/dev/vfio/devices" with user access blocked;

Probably worth clarifying that (1) must happen for *all* devices in the group before (2) happens for any device in the group.

Quoted 8 lines
Quoted 2 lines

No. User access is naturally blocked for other devices as long as they are not opened yet.

Uh... my point is that everything in the group has to be removed from regular kernel drivers before we reach step (2). Is the plan that you must do that before you can even open them? That's a reasonable choice, but then I think you should show that step in this description as well.

Agree. I think below proposal can meet above requirement and ensure it's not broken in the whole process when the group is operated by the userspace:

https://lore.kernel.org/kvm/[email protected]/

and definitely an updated description will be provided when sending out the new proposal.

Quoted 18 lines
Quoted 9 lines
Quoted 9 lines
Quoted 8 lines

2) Bind the device to an iommufd with an initial security context (an empty iommu domain which blocks dma) established for its group, with user access unblocked;

3) Attach the device to a user-specified ioasid (shared by all devices attached to this ioasid). Before attaching, the device should be first detached from the initial context;

Quoted 8 lines
Quoted 4 lines

So, this step can implicitly but observably change the behaviour for other devices in the group as well. I don't love that kind of difficult to predict side effect, which is why I'm *still* not totally convinced by the device-centric model.

which side-effect is predicted here? The user anyway needs to be aware of such group restriction regardless whether it uses group or nongroup interface.

Quoted 6 lines

Yes, exactly. And with a group interface it's obvious it has to understand it. With the non-group interface, you can get to this stage in ignorance of groups. It will even work as long as you are lucky enough only to try with singleton-group devices. Then you try it with two devices in the one group and doing (3) on device A will implicitly change the DMA environment of device B.

for non-group we can also document it obviously in uAPI that the user must understand group restriction and violating it will get failure when attaching to different IOAS's for devices in the same group.

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

#108 lore raw

Re: [RFC 06/20] iommu: Add iommu_device_init[exit]_user_dma interfaces

Quoted 64 lines · Kevin
Quoted 21 lines

From: David Gibson
Sent: Wednesday, September 29, 2021 2:35 PM

Quoted 8 lines · Kevin
Quoted 8 lines

From: David Gibson <[email protected]>
Sent: Wednesday, September 29, 2021 12:56 PM

Quoted 5 lines

Unlike vfio, iommufd adopts a device-centric design with all group logistics hidden behind the fd. Binding a device to iommufd serves as the contract to get security context established (and vice versa for unbinding). One additional requirement in iommufd is to manage

the

Quoted 1 line
Quoted 1 line
Quoted 1 line

switch between multiple security contexts due to decoupled

bind/attach:

Quoted 6 lines
Quoted 5 lines
Quoted 2 lines

1) Open a device in "/dev/vfio/devices" with user access blocked;

Probably worth clarifying that (1) must happen for *all* devices in the group before (2) happens for any device in the group.

Quoted 8 lines
Quoted 2 lines

No. User access is naturally blocked for other devices as long as they are not opened yet.

Uh... my point is that everything in the group has to be removed from regular kernel drivers before we reach step (2). Is the plan that you must do that before you can even open them? That's a reasonable choice, but then I think you should show that step in this description as well.

Agree. I think below proposal can meet above requirement and ensure it's not broken in the whole process when the group is operated by the userspace:

https://lore.kernel.org/kvm/[email protected]/

and definitely an updated description will be provided when sending out the new proposal.

Quoted 18 lines
Quoted 9 lines
Quoted 9 lines
Quoted 8 lines

2) Bind the device to an iommufd with an initial security context (an empty iommu domain which blocks dma) established for its group, with user access unblocked;

3) Attach the device to a user-specified ioasid (shared by all devices attached to this ioasid). Before attaching, the device should be first detached from the initial context;

Quoted 8 lines
Quoted 4 lines

So, this step can implicitly but observably change the behaviour for other devices in the group as well. I don't love that kind of difficult to predict side effect, which is why I'm *still* not totally convinced by the device-centric model.

which side-effect is predicted here? The user anyway needs to be aware of such group restriction regardless whether it uses group or nongroup interface.

Quoted 6 lines

Yes, exactly. And with a group interface it's obvious it has to understand it. With the non-group interface, you can get to this stage in ignorance of groups. It will even work as long as you are lucky enough only to try with singleton-group devices. Then you try it with two devices in the one group and doing (3) on device A will implicitly change the DMA environment of device B.

Quoted 3 lines

for non-group we can also document it obviously in uAPI that the user must understand group restriction and violating it will get failure when attaching to different IOAS's for devices in the same group.

Documenting limitations is always inferior to building them into the actual API signatures. Sometimes its the only option, but people frequently don't read the docs, whereas they kind of have to look at the API itself.

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

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

maintainer
#109 lore raw

Re: [RFC 06/20] iommu: Add iommu_device_init[exit]_user_dma interfaces

In reply to David Gibson
Quoted 6 lines · David Gibson

Yes, exactly. And with a group interface it's obvious it has to understand it. With the non-group interface, you can get to this stage in ignorance of groups. It will even work as long as you are lucky enough only to try with singleton-group devices. Then you try it with two devices in the one group and doing (3) on device A will implicitly change the DMA environment of device B.

The security model here says this is fine.

This idea to put the iommu code in charge of security is quite clean, as I said in the other mail drivers attached to 'struct devices *' tell the iommu layer what they are are doing:

iommu_set_device_dma_owner(dev, DMA_OWNER_KERNEL, NULL) iommu_set_device_dma_owner(dev, DMA_OWNER_SHARED, NULL) iommu_set_device_dma_owner(dev, DMA_OWNER_USERSPACE, group_file/iommu_file)

And it decides if it is allowed.

If device A is allowed to go to userspace then security wise it is deemed fine that B is impacted. That is what we have defined already today.

This proposal does not free userpace from having to understand this! The iommu_group sysfs is still there and still must be understood.

The *admin* the one responsible to understand the groups, not the applications. The admin has no idea what a group FD is - they should be looking at the sysfs and seeing the iommu_group directories.

Jason

#110 lore raw

Re: [RFC 06/20] iommu: Add iommu_device_init[exit]_user_dma interfaces

Quoted 10 lines · Jason Gunthorpe
Quoted 6 lines · David Gibson

Yes, exactly. And with a group interface it's obvious it has to understand it. With the non-group interface, you can get to this stage in ignorance of groups. It will even work as long as you are lucky enough only to try with singleton-group devices. Then you try it with two devices in the one group and doing (3) on device A will implicitly change the DMA environment of device B.

The security model here says this is fine.

I'm not making a statement about the security model, I'm making a statement about surprisingness of the programming interface. In your program you have devices A & B, you perform an operation that specifies only device A and device B changes behaviour.

Quoted 14 lines

This idea to put the iommu code in charge of security is quite clean, as I said in the other mail drivers attached to 'struct devices *' tell the iommu layer what they are are doing:

iommu_set_device_dma_owner(dev, DMA_OWNER_KERNEL, NULL) iommu_set_device_dma_owner(dev, DMA_OWNER_SHARED, NULL) iommu_set_device_dma_owner(dev, DMA_OWNER_USERSPACE, group_file/iommu_file)

And it decides if it is allowed.

If device A is allowed to go to userspace then security wise it is deemed fine that B is impacted. That is what we have defined already today.

Quoted 6 lines

This proposal does not free userpace from having to understand this! The iommu_group sysfs is still there and still must be understood.

The *admin* the one responsible to understand the groups, not the applications. The admin has no idea what a group FD is - they should be looking at the sysfs and seeing the iommu_group directories.

Not just the admin. If an app is given two devices in the same group to use *both* it must understand that and act accordingly.

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
maintainer
#111 lore raw

Re: [RFC 06/20] iommu: Add iommu_device_init[exit]_user_dma interfaces

Quoted 6 lines · David Gibson
Quoted 3 lines

The *admin* the one responsible to understand the groups, not the applications. The admin has no idea what a group FD is - they should be looking at the sysfs and seeing the iommu_group directories.

Not just the admin. If an app is given two devices in the same group to use *both* it must understand that and act accordingly.

Yes, but this is true regardless of what the uAPI is, and for common app cases where we have a single IO Page table for all devices the app still doesn't need to care about groups since it can just assign all devices to the same IO page table and everything works out just fine.

For instance qemu without a vIOMMU does not need to care about groups. It opens a single iommufd, creates a single IO page table that maps the guest physical space and assigns every device to that IO page table. No issue.

Only if qemu is creating a vIOMMU does it need to start to look at the groups and ensure that the group becomes visible to the guest OS. Here the group fd doesn't really help anything

Jason

#112 lore raw

Re: [RFC 06/20] iommu: Add iommu_device_init[exit]_user_dma interfaces

Quoted 10 lines · Jason Gunthorpe
Quoted 6 lines · David Gibson
Quoted 3 lines

The *admin* the one responsible to understand the groups, not the applications. The admin has no idea what a group FD is - they should be looking at the sysfs and seeing the iommu_group directories.

Not just the admin. If an app is given two devices in the same group to use *both* it must understand that and act accordingly.

Yes, but this is true regardless of what the uAPI is,

Yes, but formerly it was explicit and now it is implicit. Before we said "attach this group to this container", which can reasonably be expected to affect the whole group. Now we say "attach this device to this IOAS" and it silently also affects other devices.

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
author
#113 lore raw

RFC 7/20 iommu/iommufd: Add iommufd_[un]bind_device()

In reply to Liu Yi L

Under the /dev/iommu model, iommufd provides the interface for I/O page tables management such as dma map/unmap. However, it cannot work independently since the device is still owned by the device-passthrough frameworks (VFIO, vDPA, etc.) and vice versa. Device-passthrough frameworks should build a connection between its device and the iommufd to delegate the I/O page table management affairs to iommufd.

This patch introduces iommufd_[un]bind_device() helpers for the device- passthrough framework to build such connection. The helper functions then invoke iommu core (iommu_device_init/exit_user_dma()) to establish/exit security context for the bound device. Each successfully bound device is internally tracked by an iommufd_device object. This object is returned to the caller for subsequent attaching operations on the device as well.

The caller should pass a user-provided cookie to mark the device in the iommufd. Later this cookie will be used to represent the device in iommufd uAPI, e.g. when querying device capabilities or handling per-device I/O page faults. One alternative is to have iommufd allocate a device label and return to the user. Either way works, but cookie is slightly preferred per earlier discussion as it may allow the user to inject faults slightly faster without ID->vRID lookup.

iommu_[un]bind_device() functions are only used for physical devices. Other variants will be introduced in the future, e.g.:

- iommu_[un]bind_device_pasid() for mdev/subdev which requires pasid granular DMA isolation;
- iommu_[un]bind_sw_mdev() for sw mdev which relies on software measures instead of iommu to isolate DMA;

---
 drivers/iommu/iommufd/iommufd.c | 160 +++++++++++++++++++++++++++++++-
 include/linux/iommufd.h         |  38 ++++++++
 2 files changed, 196 insertions(+), 2 deletions(-)
 create mode 100644 include/linux/iommufd.h
drivers/iommu/iommufd/iommufd.c +158 -2
index 710b7e62988b..e16ca21e4534 100644
@@ -16,10 +16,30 @@
 #include <linux/miscdevice.h>
 #include <linux/mutex.h>
 #include <linux/iommu.h>
+#include <linux/iommufd.h>
+#include <linux/xarray.h>
+#include <asm-generic/bug.h>
 
 /* Per iommufd */
 struct iommufd_ctx {
 	refcount_t refs;
+	struct mutex lock;
+	struct xarray device_xa; /* xarray of bound devices */
+};
+
+/*
+ * A iommufd_device object represents the binding relationship
+ * between iommufd and device. It is created per a successful
+ * binding request from device driver. The bound device must be
+ * a physical device so far. Subdevice will be supported later
+ * (with additional PASID information). An user-assigned cookie
+ * is also recorded to mark the device in the /dev/iommu uAPI.
+ */
+struct iommufd_device {
+	unsigned int id;
+	struct iommufd_ctx *ictx;
+	struct device *dev; /* always be the physical device */
+	u64 dev_cookie;
 };
 
 static int iommufd_fops_open(struct inode *inode, struct file *filep)
@@ -32,15 +52,58 @@ static int iommufd_fops_open(struct inode *inode, struct file *filep)
 		return -ENOMEM;
 
 	refcount_set(&ictx->refs, 1);
+	mutex_init(&ictx->lock);
+	xa_init_flags(&ictx->device_xa, XA_FLAGS_ALLOC);
 	filep->private_data = ictx;
 
 	return ret;
 }
 
+static void iommufd_ctx_get(struct iommufd_ctx *ictx)
+{
+	refcount_inc(&ictx->refs);
+}
+
+static const struct file_operations iommufd_fops;
+
+/**
+ * iommufd_ctx_fdget - Acquires a reference to the internal iommufd context.
+ * @fd: [in] iommufd file descriptor.
+ *
+ * Returns a pointer to the iommufd context, otherwise NULL;
+ *
+ */
+static struct iommufd_ctx *iommufd_ctx_fdget(int fd)
+{
+	struct fd f = fdget(fd);
+	struct file *file = f.file;
+	struct iommufd_ctx *ictx;
+
+	if (!file)
+		return NULL;
+
+	if (file->f_op != &iommufd_fops)
+		return NULL;
+
+	ictx = file->private_data;
+	if (ictx)
+		iommufd_ctx_get(ictx);
+	fdput(f);
+	return ictx;
+}
+
+/**
+ * iommufd_ctx_put - Releases a reference to the internal iommufd context.
+ * @ictx: [in] Pointer to iommufd context.
+ *
+ */
 static void iommufd_ctx_put(struct iommufd_ctx *ictx)
 {
-	if (refcount_dec_and_test(&ictx->refs))
-		kfree(ictx);
+	if (!refcount_dec_and_test(&ictx->refs))
+		return;
+
+	WARN_ON(!xa_empty(&ictx->device_xa));
+	kfree(ictx);
 }
 
 static int iommufd_fops_release(struct inode *inode, struct file *filep)
@@ -86,6 +149,99 @@ static struct miscdevice iommu_misc_dev = {
 	.mode = 0666,
 };
 
+/**
+ * iommufd_bind_device - Bind a physical device marked by a device
+ *			 cookie to an iommu fd.
+ * @fd:		[in] iommufd file descriptor.
+ * @dev:	[in] Pointer to a physical device struct.
+ * @dev_cookie:	[in] A cookie to mark the device in /dev/iommu uAPI.
+ *
+ * A successful bind establishes a security context for the device
+ * and returns struct iommufd_device pointer. Otherwise returns
+ * error pointer.
+ *
+ */
+struct iommufd_device *iommufd_bind_device(int fd, struct device *dev,
+					   u64 dev_cookie)
+{
+	struct iommufd_ctx *ictx;
+	struct iommufd_device *idev;
+	unsigned long index;
+	unsigned int id;
+	int ret;
+
+	ictx = iommufd_ctx_fdget(fd);
+	if (!ictx)
+		return ERR_PTR(-EINVAL);
+
+	mutex_lock(&ictx->lock);
+
+	/* check duplicate registration */
+	xa_for_each(&ictx->device_xa, index, idev) {
+		if (idev->dev == dev || idev->dev_cookie == dev_cookie) {
+			idev = ERR_PTR(-EBUSY);
+			goto out_unlock;
+		}
+	}
+
+	idev = kzalloc(sizeof(*idev), GFP_KERNEL);
+	if (!idev) {
+		ret = -ENOMEM;
+		goto out_unlock;
+	}
+
+	/* Establish the security context */
+	ret = iommu_device_init_user_dma(dev, (unsigned long)ictx);
+	if (ret)
+		goto out_free;
+
+	ret = xa_alloc(&ictx->device_xa, &id, idev,
+		       XA_LIMIT(IOMMUFD_DEVID_MIN, IOMMUFD_DEVID_MAX),
+		       GFP_KERNEL);
+	if (ret) {
+		idev = ERR_PTR(ret);
+		goto out_user_dma;
+	}
+
+	idev->ictx = ictx;
+	idev->dev = dev;
+	idev->dev_cookie = dev_cookie;
+	idev->id = id;
+	mutex_unlock(&ictx->lock);
+
+	return idev;
+out_user_dma:
+	iommu_device_exit_user_dma(idev->dev);
+out_free:
+	kfree(idev);
+out_unlock:
+	mutex_unlock(&ictx->lock);
+	iommufd_ctx_put(ictx);
+
+	return ERR_PTR(ret);
+}
+EXPORT_SYMBOL_GPL(iommufd_bind_device);
+
+/**
+ * iommufd_unbind_device - Unbind a physical device from iommufd
+ *
+ * @idev: [in] Pointer to the internal iommufd_device struct.
+ *
+ */
+void iommufd_unbind_device(struct iommufd_device *idev)
+{
+	struct iommufd_ctx *ictx = idev->ictx;
+
+	mutex_lock(&ictx->lock);
+	xa_erase(&ictx->device_xa, idev->id);
+	mutex_unlock(&ictx->lock);
+	/* Exit the security context */
+	iommu_device_exit_user_dma(idev->dev);
+	kfree(idev);
+	iommufd_ctx_put(ictx);
+}
+EXPORT_SYMBOL_GPL(iommufd_unbind_device);
+
 static int __init iommufd_init(void)
 {
 	int ret;
include/linux/iommufd.h new file +38 -0
new file mode 100644
index 000000000000..1603a13937e9
@@ -0,0 +1,38 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+/*
+ * IOMMUFD API definition
+ *
+ * Copyright (C) 2021 Intel Corporation
+ *
+ * Author: Liu Yi L <[email protected]>
+ */
+#ifndef __LINUX_IOMMUFD_H
+#define __LINUX_IOMMUFD_H
+
+#include <linux/types.h>
+#include <linux/errno.h>
+#include <linux/err.h>
+#include <linux/device.h>
+
+#define IOMMUFD_DEVID_MAX	((unsigned int)(0x7FFFFFFF))
+#define IOMMUFD_DEVID_MIN	0
+
+struct iommufd_device;
+
+#if IS_ENABLED(CONFIG_IOMMUFD)
+struct iommufd_device *
+iommufd_bind_device(int fd, struct device *dev, u64 dev_cookie);
+void iommufd_unbind_device(struct iommufd_device *idev);
+
+#else /* !CONFIG_IOMMUFD */
+static inline struct iommufd_device *
+iommufd_bind_device(int fd, struct device *dev, u64 dev_cookie)
+{
+	return ERR_PTR(-ENODEV);
+}
+
+static inline void iommufd_unbind_device(struct iommufd_device *idev)
+{
+}
+#endif /* CONFIG_IOMMUFD */
+#endif /* __LINUX_IOMMUFD_H */
Signature · 6 lines
2.25.1

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

Re: [RFC 07/20] iommu/iommufd: Add iommufd_[un]bind_device()

Quoted 21 lines · Liu Yi L
drivers/iommu/iommufd/iommufd.c +14 -0
+/*
+ * A iommufd_device object represents the binding relationship
+ * between iommufd and device. It is created per a successful
+ * binding request from device driver. The bound device must be
+ * a physical device so far. Subdevice will be supported later
+ * (with additional PASID information). An user-assigned cookie
+ * is also recorded to mark the device in the /dev/iommu uAPI.
+ */
+struct iommufd_device {
+	unsigned int id;
+	struct iommufd_ctx *ictx;
+	struct device *dev; /* always be the physical device */
+	u64 dev_cookie;
 };
 
 static int iommufd_fops_open(struct inode *inode, struct file *filep)
@@ -32,15 +52,58 @@ static int iommufd_fops_open(struct inode *inode, struct file *filep)
 		return -ENOMEM;
 
 	refcount_set(&ictx->refs, 1);
+	mutex_init(&ictx->lock);

drivers/iommu/iommufd/iommufd.c static int iommufd_fops_open(struct inode *inode, struct file *filep) · line 65

Quoted 10 lines
drivers/iommu/iommufd/iommufd.c +5 -0
+	xa_init_flags(&ictx->device_xa, XA_FLAGS_ALLOC);
 	filep->private_data = ictx;
 
 	return ret;
 }
 
+static void iommufd_ctx_get(struct iommufd_ctx *ictx)
+{
+	refcount_inc(&ictx->refs);
+}

See my earlier remarks about how to structure the lifetime logic, this ref isn't necessary.

Quoted 10 lines
drivers/iommu/iommufd/iommufd.c +10 -0
+static const struct file_operations iommufd_fops;
+
+/**
+ * iommufd_ctx_fdget - Acquires a reference to the internal iommufd context.
+ * @fd: [in] iommufd file descriptor.
+ *
+ * Returns a pointer to the iommufd context, otherwise NULL;
+ *
+ */
+static struct iommufd_ctx *iommufd_ctx_fdget(int fd)

drivers/iommu/iommufd/iommufd.c static int iommufd_fops_open(struct inode *inode, struct file *filep) · line 86

Quoted 10 lines
drivers/iommu/iommufd/iommufd.c +10 -0
+{
+	struct fd f = fdget(fd);
+	struct file *file = f.file;
+	struct iommufd_ctx *ictx;
+
+	if (!file)
+		return NULL;
+
+	if (file->f_op != &iommufd_fops)
+		return NULL;

Leaks the fdget

drivers/iommu/iommufd/iommufd.c static int iommufd_fops_open(struct inode *inode, struct file *filep) · line 90

Quoted 4 lines
drivers/iommu/iommufd/iommufd.c +4 -0
+
+	ictx = file->private_data;
+	if (ictx)
+		iommufd_ctx_get(ictx);

Use success oriented flow

Quoted 16 lines
drivers/iommu/iommufd/iommufd.c +15 -0
+	fdput(f);
+	return ictx;
+}
 
+ */
+struct iommufd_device *iommufd_bind_device(int fd, struct device *dev,
+					   u64 dev_cookie)
+{
+	struct iommufd_ctx *ictx;
+	struct iommufd_device *idev;
+	unsigned long index;
+	unsigned int id;
+	int ret;
+
+	ictx = iommufd_ctx_fdget(fd);
+	if (!ictx)

drivers/iommu/iommufd/iommufd.c static struct miscdevice iommu_misc_dev = { · line 184

Quoted 10 lines
drivers/iommu/iommufd/iommufd.c +10 -0
+		return ERR_PTR(-EINVAL);
+
+	mutex_lock(&ictx->lock);
+
+	/* check duplicate registration */
+	xa_for_each(&ictx->device_xa, index, idev) {
+		if (idev->dev == dev || idev->dev_cookie == dev_cookie) {
+			idev = ERR_PTR(-EBUSY);
+			goto out_unlock;
+		}

I can't think of a reason why this expensive check is needed.

Quoted 6 lines
drivers/iommu/iommufd/iommufd.c +6 -0
+	}
+
+	idev = kzalloc(sizeof(*idev), GFP_KERNEL);
+	if (!idev) {
+		ret = -ENOMEM;
+		goto out_unlock;

drivers/iommu/iommufd/iommufd.c static struct miscdevice iommu_misc_dev = { · line 200

Quoted 10 lines
drivers/iommu/iommufd/iommufd.c +10 -0
+	}
+
+	/* Establish the security context */
+	ret = iommu_device_init_user_dma(dev, (unsigned long)ictx);
+	if (ret)
+		goto out_free;
+
+	ret = xa_alloc(&ictx->device_xa, &id, idev,
+		       XA_LIMIT(IOMMUFD_DEVID_MIN, IOMMUFD_DEVID_MAX),
+		       GFP_KERNEL);

idev should be fully initialized before being placed in the xarray, so this should be the last thing done.

Why not just use the standard xa_limit_32b instead of special single use constants?

Jason

author
#115 lore raw

RE: [RFC 07/20] iommu/iommufd: Add iommufd_[un]bind_device()

Quoted 117 lines

From: Jason Gunthorpe <[email protected]>
Sent: Wednesday, September 22, 2021 1:15 AM

drivers/iommu/iommufd/iommufd.c static int iommufd_fops_open(struct inode *inode,

Quoted 17 lines · Liu Yi L
drivers/iommu/iommufd/iommufd.c +13 -0
+/*
+ * A iommufd_device object represents the binding relationship
+ * between iommufd and device. It is created per a successful
+ * binding request from device driver. The bound device must be
+ * a physical device so far. Subdevice will be supported later
+ * (with additional PASID information). An user-assigned cookie
+ * is also recorded to mark the device in the /dev/iommu uAPI.
+ */
+struct iommufd_device {
+	unsigned int id;
+	struct iommufd_ctx *ictx;
+	struct device *dev; /* always be the physical device */
+	u64 dev_cookie;
 };
 
 static int iommufd_fops_open(struct inode *inode, struct file *filep)
@@ -32,15 +52,58 @@ static int iommufd_fops_open(struct inode *inode,

struct file *filep)

Quoted 4 lines
drivers/iommu/iommufd/iommufd.c +1 -0
 		return -ENOMEM;
 
 	refcount_set(&ictx->refs, 1);
+	mutex_init(&ictx->lock);

drivers/iommu/iommufd/iommufd.c static int iommufd_fops_open(struct inode *inode, struct file *filep) · line 65

Quoted 10 lines
drivers/iommu/iommufd/iommufd.c +5 -0
+	xa_init_flags(&ictx->device_xa, XA_FLAGS_ALLOC);
 	filep->private_data = ictx;
 
 	return ret;
 }
 
+static void iommufd_ctx_get(struct iommufd_ctx *ictx)
+{
+	refcount_inc(&ictx->refs);
+}

See my earlier remarks about how to structure the lifetime logic, this ref isn't necessary.

drivers/iommu/iommufd/iommufd.c static int iommufd_fops_open(struct inode *inode, struct file *filep)

Quoted 4 lines
drivers/iommu/iommufd/iommufd.c +4 -0
+static const struct file_operations iommufd_fops;
+
+/**
+ * iommufd_ctx_fdget - Acquires a reference to the internal iommufd

context.

Quoted 6 lines
drivers/iommu/iommufd/iommufd.c +6 -0
+ * @fd: [in] iommufd file descriptor.
+ *
+ * Returns a pointer to the iommufd context, otherwise NULL;
+ *
+ */
+static struct iommufd_ctx *iommufd_ctx_fdget(int fd)

drivers/iommu/iommufd/iommufd.c static int iommufd_fops_open(struct inode *inode, struct file *filep) · line 86

Quoted 10 lines
drivers/iommu/iommufd/iommufd.c +10 -0
+{
+	struct fd f = fdget(fd);
+	struct file *file = f.file;
+	struct iommufd_ctx *ictx;
+
+	if (!file)
+		return NULL;
+
+	if (file->f_op != &iommufd_fops)
+		return NULL;

Leaks the fdget

drivers/iommu/iommufd/iommufd.c static int iommufd_fops_open(struct inode *inode, struct file *filep) · line 90

Quoted 4 lines
drivers/iommu/iommufd/iommufd.c +4 -0
+
+	ictx = file->private_data;
+	if (ictx)
+		iommufd_ctx_get(ictx);

Use success oriented flow

Quoted 16 lines
drivers/iommu/iommufd/iommufd.c +15 -0
+	fdput(f);
+	return ictx;
+}
 
+ */
+struct iommufd_device *iommufd_bind_device(int fd, struct device *dev,
+					   u64 dev_cookie)
+{
+	struct iommufd_ctx *ictx;
+	struct iommufd_device *idev;
+	unsigned long index;
+	unsigned int id;
+	int ret;
+
+	ictx = iommufd_ctx_fdget(fd);
+	if (!ictx)

drivers/iommu/iommufd/iommufd.c static struct miscdevice iommu_misc_dev = { · line 184

Quoted 10 lines
drivers/iommu/iommufd/iommufd.c +10 -0
+		return ERR_PTR(-EINVAL);
+
+	mutex_lock(&ictx->lock);
+
+	/* check duplicate registration */
+	xa_for_each(&ictx->device_xa, index, idev) {
+		if (idev->dev == dev || idev->dev_cookie == dev_cookie) {
+			idev = ERR_PTR(-EBUSY);
+			goto out_unlock;
+		}

I can't think of a reason why this expensive check is needed.

Quoted 5 lines
drivers/iommu/iommufd/iommufd.c +5 -0
+	}
+
+	idev = kzalloc(sizeof(*idev), GFP_KERNEL);
+	if (!idev) {
+		ret = -ENOMEM;

drivers/iommu/iommufd/iommufd.c static struct miscdevice iommu_misc_dev = {

Quoted 10 lines
drivers/iommu/iommufd/iommufd.c +10 -0
+		goto out_unlock;
+	}
+
+	/* Establish the security context */
+	ret = iommu_device_init_user_dma(dev, (unsigned long)ictx);
+	if (ret)
+		goto out_free;
+
+	ret = xa_alloc(&ictx->device_xa, &id, idev,
+		       XA_LIMIT(IOMMUFD_DEVID_MIN,

IOMMUFD_DEVID_MAX),

Quoted 4 lines

drivers/iommu/iommufd/iommufd.c static struct miscdevice iommu_misc_dev = { · line 200

Quoted 1 line
drivers/iommu/iommufd/iommufd.c +1 -0
+		       GFP_KERNEL);

idev should be fully initialized before being placed in the xarray, so this should be the last thing done.

all good suggestions above. thanks for catching them.

Quoted 2 lines

Why not just use the standard xa_limit_32b instead of special single use constants?

yeah. should use xa_limit_32b.

Regards,
Yi Liu

Quoted 1 line

Jason

#116 lore raw

Re: [RFC 07/20] iommu/iommufd: Add iommufd_[un]bind_device()

In reply to Liu Yi L
Quoted 59 lines · Liu Yi L

Under the /dev/iommu model, iommufd provides the interface for I/O page tables management such as dma map/unmap. However, it cannot work independently since the device is still owned by the device-passthrough frameworks (VFIO, vDPA, etc.) and vice versa. Device-passthrough frameworks should build a connection between its device and the iommufd to delegate the I/O page table management affairs to iommufd.

This patch introduces iommufd_[un]bind_device() helpers for the device- passthrough framework to build such connection. The helper functions then invoke iommu core (iommu_device_init/exit_user_dma()) to establish/exit security context for the bound device. Each successfully bound device is internally tracked by an iommufd_device object. This object is returned to the caller for subsequent attaching operations on the device as well.

The caller should pass a user-provided cookie to mark the device in the iommufd. Later this cookie will be used to represent the device in iommufd uAPI, e.g. when querying device capabilities or handling per-device I/O page faults. One alternative is to have iommufd allocate a device label and return to the user. Either way works, but cookie is slightly preferred per earlier discussion as it may allow the user to inject faults slightly faster without ID->vRID lookup.

iommu_[un]bind_device() functions are only used for physical devices. Other variants will be introduced in the future, e.g.:

(quoted diff) +0 -2
-  iommu_[un]bind_device_pasid() for mdev/subdev which requires pasid granular
   DMA isolation;
-  iommu_[un]bind_sw_mdev() for sw mdev which relies on software measures
   instead of iommu to isolate DMA;
---
 drivers/iommu/iommufd/iommufd.c | 160 +++++++++++++++++++++++++++++++-
 include/linux/iommufd.h         |  38 ++++++++
 2 files changed, 196 insertions(+), 2 deletions(-)
 create mode 100644 include/linux/iommufd.h
drivers/iommu/iommufd/iommufd.c +10 -0
index 710b7e62988b..e16ca21e4534 100644
@@ -16,10 +16,30 @@
 #include <linux/miscdevice.h>
 #include <linux/mutex.h>
 #include <linux/iommu.h>
+#include <linux/iommufd.h>
+#include <linux/xarray.h>
+#include <asm-generic/bug.h>
 
 /* Per iommufd */
 struct iommufd_ctx {
 	refcount_t refs;
+	struct mutex lock;
+	struct xarray device_xa; /* xarray of bound devices */
+};
+
+/*
+ * A iommufd_device object represents the binding relationship
+ * between iommufd and device. It is created per a successful

drivers/iommu/iommufd/iommufd.c line 42

Quoted 10 lines
drivers/iommu/iommufd/iommufd.c +10 -0
+ * binding request from device driver. The bound device must be
+ * a physical device so far. Subdevice will be supported later
+ * (with additional PASID information). An user-assigned cookie
+ * is also recorded to mark the device in the /dev/iommu uAPI.
+ */
+struct iommufd_device {
+	unsigned int id;
+	struct iommufd_ctx *ictx;
+	struct device *dev; /* always be the physical device */
+	u64 dev_cookie;

Why do you need both an 'id' and a 'dev_cookie'? Since they're both unique, couldn't you just use the cookie directly as the index into the xarray?

Quoted 208 lines
drivers/iommu/iommufd/iommufd.c +138 -2
 };
 
 static int iommufd_fops_open(struct inode *inode, struct file *filep)
@@ -32,15 +52,58 @@ static int iommufd_fops_open(struct inode *inode, struct file *filep)
 		return -ENOMEM;
 
 	refcount_set(&ictx->refs, 1);
+	mutex_init(&ictx->lock);
+	xa_init_flags(&ictx->device_xa, XA_FLAGS_ALLOC);
 	filep->private_data = ictx;
 
 	return ret;
 }
 
+static void iommufd_ctx_get(struct iommufd_ctx *ictx)
+{
+	refcount_inc(&ictx->refs);
+}
+
+static const struct file_operations iommufd_fops;
+
+/**
+ * iommufd_ctx_fdget - Acquires a reference to the internal iommufd context.
+ * @fd: [in] iommufd file descriptor.
+ *
+ * Returns a pointer to the iommufd context, otherwise NULL;
+ *
+ */
+static struct iommufd_ctx *iommufd_ctx_fdget(int fd)
+{
+	struct fd f = fdget(fd);
+	struct file *file = f.file;
+	struct iommufd_ctx *ictx;
+
+	if (!file)
+		return NULL;
+
+	if (file->f_op != &iommufd_fops)
+		return NULL;
+
+	ictx = file->private_data;
+	if (ictx)
+		iommufd_ctx_get(ictx);
+	fdput(f);
+	return ictx;
+}
+
+/**
+ * iommufd_ctx_put - Releases a reference to the internal iommufd context.
+ * @ictx: [in] Pointer to iommufd context.
+ *
+ */
 static void iommufd_ctx_put(struct iommufd_ctx *ictx)
 {
-	if (refcount_dec_and_test(&ictx->refs))
-		kfree(ictx);
+	if (!refcount_dec_and_test(&ictx->refs))
+		return;
+
+	WARN_ON(!xa_empty(&ictx->device_xa));
+	kfree(ictx);
 }
 
 static int iommufd_fops_release(struct inode *inode, struct file *filep)
@@ -86,6 +149,99 @@ static struct miscdevice iommu_misc_dev = {
 	.mode = 0666,
 };
 
+/**
+ * iommufd_bind_device - Bind a physical device marked by a device
+ *			 cookie to an iommu fd.
+ * @fd:		[in] iommufd file descriptor.
+ * @dev:	[in] Pointer to a physical device struct.
+ * @dev_cookie:	[in] A cookie to mark the device in /dev/iommu uAPI.
+ *
+ * A successful bind establishes a security context for the device
+ * and returns struct iommufd_device pointer. Otherwise returns
+ * error pointer.
+ *
+ */
+struct iommufd_device *iommufd_bind_device(int fd, struct device *dev,
+					   u64 dev_cookie)
+{
+	struct iommufd_ctx *ictx;
+	struct iommufd_device *idev;
+	unsigned long index;
+	unsigned int id;
+	int ret;
+
+	ictx = iommufd_ctx_fdget(fd);
+	if (!ictx)
+		return ERR_PTR(-EINVAL);
+
+	mutex_lock(&ictx->lock);
+
+	/* check duplicate registration */
+	xa_for_each(&ictx->device_xa, index, idev) {
+		if (idev->dev == dev || idev->dev_cookie == dev_cookie) {
+			idev = ERR_PTR(-EBUSY);
+			goto out_unlock;
+		}
+	}
+
+	idev = kzalloc(sizeof(*idev), GFP_KERNEL);
+	if (!idev) {
+		ret = -ENOMEM;
+		goto out_unlock;
+	}
+
+	/* Establish the security context */
+	ret = iommu_device_init_user_dma(dev, (unsigned long)ictx);
+	if (ret)
+		goto out_free;
+
+	ret = xa_alloc(&ictx->device_xa, &id, idev,
+		       XA_LIMIT(IOMMUFD_DEVID_MIN, IOMMUFD_DEVID_MAX),
+		       GFP_KERNEL);
+	if (ret) {
+		idev = ERR_PTR(ret);
+		goto out_user_dma;
+	}
+
+	idev->ictx = ictx;
+	idev->dev = dev;
+	idev->dev_cookie = dev_cookie;
+	idev->id = id;
+	mutex_unlock(&ictx->lock);
+
+	return idev;
+out_user_dma:
+	iommu_device_exit_user_dma(idev->dev);
+out_free:
+	kfree(idev);
+out_unlock:
+	mutex_unlock(&ictx->lock);
+	iommufd_ctx_put(ictx);
+
+	return ERR_PTR(ret);
+}
+EXPORT_SYMBOL_GPL(iommufd_bind_device);
+
+/**
+ * iommufd_unbind_device - Unbind a physical device from iommufd
+ *
+ * @idev: [in] Pointer to the internal iommufd_device struct.
+ *
+ */
+void iommufd_unbind_device(struct iommufd_device *idev)
+{
+	struct iommufd_ctx *ictx = idev->ictx;
+
+	mutex_lock(&ictx->lock);
+	xa_erase(&ictx->device_xa, idev->id);
+	mutex_unlock(&ictx->lock);
+	/* Exit the security context */
+	iommu_device_exit_user_dma(idev->dev);
+	kfree(idev);
+	iommufd_ctx_put(ictx);
+}
+EXPORT_SYMBOL_GPL(iommufd_unbind_device);
+
 static int __init iommufd_init(void)
 {
 	int ret;
include/linux/iommufd.h new file +38 -0
new file mode 100644
index 000000000000..1603a13937e9
@@ -0,0 +1,38 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+/*
+ * IOMMUFD API definition
+ *
+ * Copyright (C) 2021 Intel Corporation
+ *
+ * Author: Liu Yi L <[email protected]>
+ */
+#ifndef __LINUX_IOMMUFD_H
+#define __LINUX_IOMMUFD_H
+
+#include <linux/types.h>
+#include <linux/errno.h>
+#include <linux/err.h>
+#include <linux/device.h>
+
+#define IOMMUFD_DEVID_MAX	((unsigned int)(0x7FFFFFFF))
+#define IOMMUFD_DEVID_MIN	0
+
+struct iommufd_device;
+
+#if IS_ENABLED(CONFIG_IOMMUFD)
+struct iommufd_device *
+iommufd_bind_device(int fd, struct device *dev, u64 dev_cookie);
+void iommufd_unbind_device(struct iommufd_device *idev);
+
+#else /* !CONFIG_IOMMUFD */
+static inline struct iommufd_device *
+iommufd_bind_device(int fd, struct device *dev, u64 dev_cookie)
+{
+	return ERR_PTR(-ENODEV);
+}
+
+static inline void iommufd_unbind_device(struct iommufd_device *idev)
+{
+}
+#endif /* CONFIG_IOMMUFD */
+#endif /* __LINUX_IOMMUFD_H */
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
maintainer
#117 lore raw

Re: [RFC 07/20] iommu/iommufd: Add iommufd_[un]bind_device()

Quoted 9 lines · David Gibson

drivers/iommu/iommufd/iommufd.c line 42

Quoted 5 lines
drivers/iommu/iommufd/iommufd.c +5 -0
+struct iommufd_device {
+	unsigned int id;
+	struct iommufd_ctx *ictx;
+	struct device *dev; /* always be the physical device */
+	u64 dev_cookie;

Why do you need both an 'id' and a 'dev_cookie'? Since they're both unique, couldn't you just use the cookie directly as the index into the xarray?

ID is the kernel value in the xarray - xarray is much more efficient & safe with small kernel controlled values.

dev_cookie is a user assigned value that may not be unique. It's purpose is to allow userspace to receive and event and go back to its structure. Most likely userspace will store a pointer here, but it is also possible userspace could not use it.

It is a pretty normal pattern

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

#118 lore raw

Re: [RFC 07/20] iommu/iommufd: Add iommufd_[un]bind_device()

Quoted 14 lines · David Gibson
Quoted 10 lines

drivers/iommu/iommufd/iommufd.c line 42

Quoted 6 lines
drivers/iommu/iommufd/iommufd.c +5 -0
+struct iommufd_device {
+	unsigned int id;
+	struct iommufd_ctx *ictx;
+	struct device *dev; /* always be the physical device */
+	u64 dev_cookie;

Why do you need both an 'id' and a 'dev_cookie'? Since they're both unique, couldn't you just use the cookie directly as the index into the xarray?

ID is the kernel value in the xarray - xarray is much more efficient & safe with small kernel controlled values.

Quoted 6 lines

dev_cookie is a user assigned value that may not be unique. It's purpose is to allow userspace to receive and event and go back to its structure. Most likely userspace will store a pointer here, but it is also possible userspace could not use it.

It is a pretty normal pattern

Hm, ok. Could you point me at an example?

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

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

maintainer
#119 lore raw

Re: [RFC 07/20] iommu/iommufd: Add iommufd_[un]bind_device()

Quoted 16 lines · David Gibson
Quoted 14 lines · David Gibson
Quoted 10 lines

drivers/iommu/iommufd/iommufd.c line 42

Quoted 6 lines
drivers/iommu/iommufd/iommufd.c +5 -0
+struct iommufd_device {
+	unsigned int id;
+	struct iommufd_ctx *ictx;
+	struct device *dev; /* always be the physical device */
+	u64 dev_cookie;

Why do you need both an 'id' and a 'dev_cookie'? Since they're both unique, couldn't you just use the cookie directly as the index into the xarray?

ID is the kernel value in the xarray - xarray is much more efficient & safe with small kernel controlled values.

Quoted 8 lines
Quoted 6 lines

dev_cookie is a user assigned value that may not be unique. It's purpose is to allow userspace to receive and event and go back to its structure. Most likely userspace will store a pointer here, but it is also possible userspace could not use it.

It is a pretty normal pattern

Hm, ok. Could you point me at an example?

For instance user_data vs fd in io_uring

RDMA has many similar examples.

More or less anytime you want to allow the kernel to async retun some information providing a 64 bit user_data lets userspace have an easier time to deal with it.

Jason

#120 lore raw

Re: [RFC 07/20] iommu/iommufd: Add iommufd_[un]bind_device()

Quoted 22 lines · Jason Gunthorpe
Quoted 21 lines · David Gibson
Quoted 20 lines · Jason Gunthorpe
Quoted 9 lines · David Gibson

drivers/iommu/iommufd/iommufd.c line 42

Quoted 5 lines
drivers/iommu/iommufd/iommufd.c +5 -0
+struct iommufd_device {
+	unsigned int id;
+	struct iommufd_ctx *ictx;
+	struct device *dev; /* always be the physical device */
+	u64 dev_cookie;

Why do you need both an 'id' and a 'dev_cookie'? Since they're both unique, couldn't you just use the cookie directly as the index into the xarray?

ID is the kernel value in the xarray - xarray is much more efficient & safe with small kernel controlled values.

dev_cookie is a user assigned value that may not be unique. It's purpose is to allow userspace to receive and event and go back to its structure. Most likely userspace will store a pointer here, but it is also possible userspace could not use it.

Quoted 5 lines
Quoted 3 lines
Quoted 1 line

It is a pretty normal pattern

Hm, ok. Could you point me at an example?

For instance user_data vs fd in io_uring

Ok, but one of those is an fd, which is an existing type of handle. Here we're introducing two different unique handles that aren't an existing kernel concept.

Quoted 5 lines

RDMA has many similar examples.

More or less anytime you want to allow the kernel to async retun some information providing a 64 bit user_data lets userspace have an easier time to deal with it.

I absolutely see the need for user_data. What I'm questioning is having two different, user-visible unique handles, neither of which is an fd.

That said... is there any strong reason why user_data needs to be unique? I can imagine userspace applications where you don't care which device the notification is coming from - or at least don't care down to the same granularity that /dev/iommu is using. In which case having the kernel provided unique handle and the not-necessarily-unique user_data would make perfect sense.

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
maintainer
#121 lore raw

Re: [RFC 07/20] iommu/iommufd: Add iommufd_[un]bind_device()

Quoted 25 lines · David Gibson
Quoted 24 lines · Jason Gunthorpe
Quoted 23 lines · David Gibson
Quoted 21 lines · Jason Gunthorpe
Quoted 9 lines · David Gibson

drivers/iommu/iommufd/iommufd.c line 42

Quoted 5 lines
drivers/iommu/iommufd/iommufd.c +5 -0
+struct iommufd_device {
+	unsigned int id;
+	struct iommufd_ctx *ictx;
+	struct device *dev; /* always be the physical device */
+	u64 dev_cookie;

Why do you need both an 'id' and a 'dev_cookie'? Since they're both unique, couldn't you just use the cookie directly as the index into the xarray?

ID is the kernel value in the xarray - xarray is much more efficient & safe with small kernel controlled values.

dev_cookie is a user assigned value that may not be unique. It's purpose is to allow userspace to receive and event and go back to its structure. Most likely userspace will store a pointer here, but it is also possible userspace could not use it.

It is a pretty normal pattern

Quoted 7 lines
Quoted 3 lines
Quoted 1 line

Hm, ok. Could you point me at an example?

For instance user_data vs fd in io_uring

Ok, but one of those is an fd, which is an existing type of handle. Here we're introducing two different unique handles that aren't an existing kernel concept.

I'm not sure how that matters, the kernel has many handles - and we get to make more of them.. Look at xarray/idr users in the kernel, many of those are making userspace handles.

Quoted 6 lines

That said... is there any strong reason why user_data needs to be unique? I can imagine userspace applications where you don't care which device the notification is coming from - or at least don't care down to the same granularity that /dev/iommu is using. In which case having the kernel provided unique handle and the not-necessarily-unique user_data would make perfect sense.

I don't think the user_data 64 bit value should be unique, it is just transported from user to kernal and back again. It is *not* a handle, it is a cookie.

Handles for the kernel/user boundary should come from xarrays that have nice lookup properties - not from user provided 64 bit values that have to be stored in red black trees..

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

#122 lore raw

Re: [RFC 07/20] iommu/iommufd: Add iommufd_[un]bind_device()

Quoted 30 lines · Jason Gunthorpe
Quoted 29 lines · David Gibson
Quoted 27 lines · Jason Gunthorpe
Quoted 16 lines · David Gibson
Quoted 15 lines · Jason Gunthorpe
Quoted 9 lines · David Gibson

Review

Quoted 5 lines
(quoted diff) +5 -0
+struct iommufd_device {
+	unsigned int id;
+	struct iommufd_ctx *ictx;
+	struct device *dev; /* always be the physical device */
+	u64 dev_cookie;

Why do you need both an 'id' and a 'dev_cookie'? Since they're both unique, couldn't you just use the cookie directly as the index into the xarray?

ID is the kernel value in the xarray - xarray is much more efficient & safe with small kernel controlled values.

Quoted 8 lines
Quoted 6 lines

dev_cookie is a user assigned value that may not be unique. It's purpose is to allow userspace to receive and event and go back to its structure. Most likely userspace will store a pointer here, but it is also possible userspace could not use it.

It is a pretty normal pattern

Hm, ok. Could you point me at an example?

For instance user_data vs fd in io_uring

Quoted 7 lines
Quoted 3 lines

Ok, but one of those is an fd, which is an existing type of handle. Here we're introducing two different unique handles that aren't an existing kernel concept.

I'm not sure how that matters, the kernel has many handles - and we get to make more of them.. Look at xarray/idr users in the kernel, many of those are making userspace handles.

Again, I'm commenting *just* on the fact that the current draft introduce *two* handles for the same object. I have no objection to either of the handles in isoation.

Quoted 7 lines
Quoted 6 lines

That said... is there any strong reason why user_data needs to be unique? I can imagine userspace applications where you don't care which device the notification is coming from - or at least don't care down to the same granularity that /dev/iommu is using. In which case having the kernel provided unique handle and the not-necessarily-unique user_data would make perfect sense.

Quoted 7 lines

I don't think the user_data 64 bit value should be unique, it is just transported from user to kernal and back again. It is *not* a handle, it is a cookie.

Handles for the kernel/user boundary should come from xarrays that have nice lookup properties - not from user provided 64 bit values that have to be stored in red black trees..

Yes, I think that would make more sense.

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
author
#123 lore raw

RFC 8/20 vfio/pci: Add VFIO_DEVICE_BIND_IOMMUFD

In reply to Liu Yi L

This patch adds VFIO_DEVICE_BIND_IOMMUFD for userspace to bind the vfio device to an iommufd. No VFIO_DEVICE_UNBIND_IOMMUFD interface is provided because it's implicitly done when the device fd is closed.

In concept a vfio device can be bound to multiple iommufds, each hosting a subset of I/O address spaces attached by this device. However as a starting point (matching current vfio), only one I/O address space is supported per vfio device. It implies one device can only be attached to one iommufd at this point.

---
 drivers/vfio/pci/Kconfig            |  1 +
 drivers/vfio/pci/vfio_pci.c         | 72 ++++++++++++++++++++++++++++-
 drivers/vfio/pci/vfio_pci_private.h |  8 ++++
 include/uapi/linux/vfio.h           | 30 ++++++++++++
 4 files changed, 110 insertions(+), 1 deletion(-)
drivers/vfio/pci/Kconfig +1 -0
index 5e2e1b9a9fd3..3abfb098b4dc 100644
@@ -5,6 +5,7 @@ config VFIO_PCI
 	depends on MMU
 	select VFIO_VIRQFD
 	select IRQ_BYPASS_MANAGER
+	select IOMMUFD
 	help
 	  Support for the PCI VFIO bus driver.  This is required to make
 	  use of PCI drivers using the VFIO framework.
drivers/vfio/pci/vfio_pci.c +71 -1

90 lines not on this page · raw message

drivers/vfio/pci/vfio_pci_private.h +8 -0
index f12012e30b53..bd784accac35 100644
@@ -14,6 +14,7 @@
 #include <linux/types.h>
 #include <linux/uuid.h>
 #include <linux/notifier.h>
+#include <linux/iommufd.h>
 
 #ifndef VFIO_PCI_PRIVATE_H
 #define VFIO_PCI_PRIVATE_H
@@ -99,6 +100,11 @@ struct vfio_pci_mmap_vma {
 	struct list_head	vma_next;
 };
 
+struct vfio_iommufd_device {
+	struct iommufd_device *idev;
+	int iommu_fd;
+};
+
 struct vfio_pci_device {
 	struct vfio_device	vdev;
 	struct pci_dev		*pdev;
@@ -144,6 +150,8 @@ struct vfio_pci_device {
 	struct list_head	vma_list;
 	struct rw_semaphore	memory_lock;
 	atomic_t		block_access;
+	struct mutex		videv_lock;
+	struct vfio_iommufd_device *videv;
 };
 
 #define is_intx(vdev) (vdev->irq_type == VFIO_PCI_INTX_IRQ_INDEX)
include/uapi/linux/vfio.h +30 -0
index ef33ea002b0b..c902abd60339 100644
@@ -190,6 +190,36 @@ struct vfio_group_status {
 
 /* --------------- IOCTLs for DEVICE file descriptors --------------- */
 
+/*
+ * VFIO_DEVICE_BIND_IOMMUFD - _IOR(VFIO_TYPE, VFIO_BASE + 19,
+ *				struct vfio_device_iommu_bind_data)
+ *
+ * Bind a vfio_device to the specified iommufd
+ *
+ * The user should provide a device cookie when calling this ioctl. The
+ * cookie is later used in iommufd for capability query, iotlb invalidation
+ * and I/O fault handling.
+ *
+ * User is not allowed to access the device before the binding operation
+ * is completed.
+ *
+ * Unbind is automatically conducted when device fd is closed.
+ *
+ * Input parameters:
+ *	- iommu_fd;
+ *	- dev_cookie;
+ *
+ * Return: 0 on success, -errno on failure.
+ */
+struct vfio_device_iommu_bind_data {
+	__u32	argsz;
+	__u32	flags;
+	__s32	iommu_fd;
+	__u64	dev_cookie;
+};
+
+#define VFIO_DEVICE_BIND_IOMMUFD	_IO(VFIO_TYPE, VFIO_BASE + 19)
+
 /**
  * VFIO_DEVICE_GET_INFO - _IOR(VFIO_TYPE, VFIO_BASE + 7,
  *						struct vfio_device_info)
Signature · 6 lines
2.25.1

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

Re: [RFC 08/20] vfio/pci: Add VFIO_DEVICE_BIND_IOMMUFD

[If your patch is applied to the wrong git tree, kindly drop us a note. And when submitting patch, we suggest to use '--base' as documented in https://git-scm.com/docs/git-format-patch]

Hi Liu,

[FYI, it's a private test report for your RFC patch.]
[auto build test WARNING on hch-configfs/for-next]
[cannot apply to joro-iommu/next awilliam-vfio/next linus/master v5.15-rc1 next-20210917] [If your patch is applied to the wrong git tree, kindly drop us a note.
And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch]

url:    https://github.com/0day-ci/linux/commits/Liu-Yi-L/Introduce-dev-iommu-for-userspace-I-O-address-space-management/20210919-144631
base:   git://git.infradead.org/users/hch/configfs.git for-next
config: i386-randconfig-a016-20210919 (attached as .config)
compiler: clang version 14.0.0 (https://github.com/llvm/llvm-project c8b3d7d6d6de37af68b2f379d0e37304f78e115f)

reproduce (this is a W=1 build):
wget https://raw.githubusercontent.com/intel/lkp-tests/master/sbin/make.cross -O ~/bin/make.cross chmod +x ~/bin/make.cross
# https://github.com/0day-ci/linux/commit/597d2cdca2b63c05ae9cb0c8bb8e3b9f53b82685
git remote add linux-review https://github.com/0day-ci/linux
git fetch --no-tags linux-review Liu-Yi-L/Introduce-dev-iommu-for-userspace-I-O-address-space-management/20210919-144631 git checkout 597d2cdca2b63c05ae9cb0c8bb8e3b9f53b82685
# save the attached .config to linux build tree
COMPILER_INSTALL_PATH=$HOME/0day COMPILER=clang make.cross W=1 ARCH=i386

If you fix the issue, kindly add following tag as appropriate

All warnings (new ones prefixed by >>):

>> drivers/iommu/iommufd/iommufd.c:181:7: warning: variable 'ret' is used uninitialized whenever 'if' condition is true [-Wsometimes-uninitialized]
                   if (idev->dev == dev || idev->dev_cookie == dev_cookie) {
                       ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
   drivers/iommu/iommufd/iommufd.c:221:17: note: uninitialized use occurs here
           return ERR_PTR(ret);
                          ^~~
   drivers/iommu/iommufd/iommufd.c:181:3: note: remove the 'if' if its condition is always false
                   if (idev->dev == dev || idev->dev_cookie == dev_cookie) {
                   ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
>> drivers/iommu/iommufd/iommufd.c:181:7: warning: variable 'ret' is used uninitialized whenever '||' condition is true [-Wsometimes-uninitialized]
                   if (idev->dev == dev || idev->dev_cookie == dev_cookie) {
                       ^~~~~~~~~~~~~~~~
   drivers/iommu/iommufd/iommufd.c:221:17: note: uninitialized use occurs here
           return ERR_PTR(ret);
                          ^~~
   drivers/iommu/iommufd/iommufd.c:181:7: note: remove the '||' if its condition is always false
                   if (idev->dev == dev || idev->dev_cookie == dev_cookie) {
                       ^~~~~~~~~~~~~~~~~~~
   drivers/iommu/iommufd/iommufd.c:171:9: note: initialize the variable 'ret' to silence this warning
           int ret;
                  ^
                   = 0

2 warnings generated.

Kconfig warnings: (for reference only)
WARNING: unmet direct dependencies detected for IOMMUFD Depends on IOMMU_SUPPORT
Selected by
- VFIO_PCI && VFIO && PCI && EVENTFD && MMU

vim +181 drivers/iommu/iommufd/iommufd.c

6a3d96f3199688 Liu Yi L 2021-09-19  151  
09ba2a1b554c58 Liu Yi L 2021-09-19  152  /**
09ba2a1b554c58 Liu Yi L 2021-09-19  153   * iommufd_bind_device - Bind a physical device marked by a device
09ba2a1b554c58 Liu Yi L 2021-09-19  154   *			 cookie to an iommu fd.
09ba2a1b554c58 Liu Yi L 2021-09-19  155   * @fd:		[in] iommufd file descriptor.
09ba2a1b554c58 Liu Yi L 2021-09-19  156   * @dev:	[in] Pointer to a physical device struct.
09ba2a1b554c58 Liu Yi L 2021-09-19  157   * @dev_cookie:	[in] A cookie to mark the device in /dev/iommu uAPI.
09ba2a1b554c58 Liu Yi L 2021-09-19  158   *
09ba2a1b554c58 Liu Yi L 2021-09-19  159   * A successful bind establishes a security context for the device
09ba2a1b554c58 Liu Yi L 2021-09-19  160   * and returns struct iommufd_device pointer. Otherwise returns
09ba2a1b554c58 Liu Yi L 2021-09-19  161   * error pointer.
09ba2a1b554c58 Liu Yi L 2021-09-19  162   *
09ba2a1b554c58 Liu Yi L 2021-09-19  163   */
09ba2a1b554c58 Liu Yi L 2021-09-19  164  struct iommufd_device *iommufd_bind_device(int fd, struct device *dev,
09ba2a1b554c58 Liu Yi L 2021-09-19  165  					   u64 dev_cookie)
09ba2a1b554c58 Liu Yi L 2021-09-19  166  {
09ba2a1b554c58 Liu Yi L 2021-09-19  167  	struct iommufd_ctx *ictx;
09ba2a1b554c58 Liu Yi L 2021-09-19  168  	struct iommufd_device *idev;
09ba2a1b554c58 Liu Yi L 2021-09-19  169  	unsigned long index;
09ba2a1b554c58 Liu Yi L 2021-09-19  170  	unsigned int id;
09ba2a1b554c58 Liu Yi L 2021-09-19  171  	int ret;
09ba2a1b554c58 Liu Yi L 2021-09-19  172  
09ba2a1b554c58 Liu Yi L 2021-09-19  173  	ictx = iommufd_ctx_fdget(fd);
09ba2a1b554c58 Liu Yi L 2021-09-19  174  	if (!ictx)
09ba2a1b554c58 Liu Yi L 2021-09-19  175  		return ERR_PTR(-EINVAL);
09ba2a1b554c58 Liu Yi L 2021-09-19  176  
09ba2a1b554c58 Liu Yi L 2021-09-19  177  	mutex_lock(&ictx->lock);
09ba2a1b554c58 Liu Yi L 2021-09-19  178  
09ba2a1b554c58 Liu Yi L 2021-09-19  179  	/* check duplicate registration */
09ba2a1b554c58 Liu Yi L 2021-09-19  180  	xa_for_each(&ictx->device_xa, index, idev) {
09ba2a1b554c58 Liu Yi L 2021-09-19 @181  		if (idev->dev == dev || idev->dev_cookie == dev_cookie) {
09ba2a1b554c58 Liu Yi L 2021-09-19  182  			idev = ERR_PTR(-EBUSY);
09ba2a1b554c58 Liu Yi L 2021-09-19  183  			goto out_unlock;
09ba2a1b554c58 Liu Yi L 2021-09-19  184  		}
09ba2a1b554c58 Liu Yi L 2021-09-19  185  	}
09ba2a1b554c58 Liu Yi L 2021-09-19  186  
09ba2a1b554c58 Liu Yi L 2021-09-19  187  	idev = kzalloc(sizeof(*idev), GFP_KERNEL);
09ba2a1b554c58 Liu Yi L 2021-09-19  188  	if (!idev) {
09ba2a1b554c58 Liu Yi L 2021-09-19  189  		ret = -ENOMEM;
09ba2a1b554c58 Liu Yi L 2021-09-19  190  		goto out_unlock;
09ba2a1b554c58 Liu Yi L 2021-09-19  191  	}
09ba2a1b554c58 Liu Yi L 2021-09-19  192  
09ba2a1b554c58 Liu Yi L 2021-09-19  193  	/* Establish the security context */
09ba2a1b554c58 Liu Yi L 2021-09-19  194  	ret = iommu_device_init_user_dma(dev, (unsigned long)ictx);
09ba2a1b554c58 Liu Yi L 2021-09-19  195  	if (ret)
09ba2a1b554c58 Liu Yi L 2021-09-19  196  		goto out_free;
09ba2a1b554c58 Liu Yi L 2021-09-19  197  
09ba2a1b554c58 Liu Yi L 2021-09-19  198  	ret = xa_alloc(&ictx->device_xa, &id, idev,
09ba2a1b554c58 Liu Yi L 2021-09-19  199  		       XA_LIMIT(IOMMUFD_DEVID_MIN, IOMMUFD_DEVID_MAX),
09ba2a1b554c58 Liu Yi L 2021-09-19  200  		       GFP_KERNEL);
09ba2a1b554c58 Liu Yi L 2021-09-19  201  	if (ret) {
09ba2a1b554c58 Liu Yi L 2021-09-19  202  		idev = ERR_PTR(ret);
09ba2a1b554c58 Liu Yi L 2021-09-19  203  		goto out_user_dma;
09ba2a1b554c58 Liu Yi L 2021-09-19  204  	}
09ba2a1b554c58 Liu Yi L 2021-09-19  205  
09ba2a1b554c58 Liu Yi L 2021-09-19  206  	idev->ictx = ictx;
09ba2a1b554c58 Liu Yi L 2021-09-19  207  	idev->dev = dev;
09ba2a1b554c58 Liu Yi L 2021-09-19  208  	idev->dev_cookie = dev_cookie;
09ba2a1b554c58 Liu Yi L 2021-09-19  209  	idev->id = id;
09ba2a1b554c58 Liu Yi L 2021-09-19  210  	mutex_unlock(&ictx->lock);
09ba2a1b554c58 Liu Yi L 2021-09-19  211  
09ba2a1b554c58 Liu Yi L 2021-09-19  212  	return idev;
09ba2a1b554c58 Liu Yi L 2021-09-19  213  out_user_dma:
09ba2a1b554c58 Liu Yi L 2021-09-19  214  	iommu_device_exit_user_dma(idev->dev);
09ba2a1b554c58 Liu Yi L 2021-09-19  215  out_free:
09ba2a1b554c58 Liu Yi L 2021-09-19  216  	kfree(idev);
09ba2a1b554c58 Liu Yi L 2021-09-19  217  out_unlock:
09ba2a1b554c58 Liu Yi L 2021-09-19  218  	mutex_unlock(&ictx->lock);
09ba2a1b554c58 Liu Yi L 2021-09-19  219  	iommufd_ctx_put(ictx);
09ba2a1b554c58 Liu Yi L 2021-09-19  220  
09ba2a1b554c58 Liu Yi L 2021-09-19  221  	return ERR_PTR(ret);
09ba2a1b554c58 Liu Yi L 2021-09-19  222  }
09ba2a1b554c58 Liu Yi L 2021-09-19  223  EXPORT_SYMBOL_GPL(iommufd_bind_device);
09ba2a1b554c58 Liu Yi L 2021-09-19  224  

---
0-DAY CI Kernel Test Service, Intel Corporation
https://lists.01.org/hyperkitty/list/kbuild-all(a)lists.01.org

maintainer
#125 lore raw

Re: [RFC 08/20] vfio/pci: Add VFIO_DEVICE_BIND_IOMMUFD

In reply to Liu Yi L
Quoted 49 lines · Liu Yi L

This patch adds VFIO_DEVICE_BIND_IOMMUFD for userspace to bind the vfio device to an iommufd. No VFIO_DEVICE_UNBIND_IOMMUFD interface is provided because it's implicitly done when the device fd is closed.

In concept a vfio device can be bound to multiple iommufds, each hosting a subset of I/O address spaces attached by this device. However as a starting point (matching current vfio), only one I/O address space is supported per vfio device. It implies one device can only be attached to one iommufd at this point.

 drivers/vfio/pci/Kconfig            |  1 +
 drivers/vfio/pci/vfio_pci.c         | 72 ++++++++++++++++++++++++++++-
 drivers/vfio/pci/vfio_pci_private.h |  8 ++++
 include/uapi/linux/vfio.h           | 30 ++++++++++++
 4 files changed, 110 insertions(+), 1 deletion(-)
drivers/vfio/pci/Kconfig +2 -0
index 5e2e1b9a9fd3..3abfb098b4dc 100644
+++ b/drivers/vfio/pci/Kconfig
@@ -5,6 +5,7 @@ config VFIO_PCI
 	depends on MMU
 	select VFIO_VIRQFD
 	select IRQ_BYPASS_MANAGER
+	select IOMMUFD
 	help
 	  Support for the PCI VFIO bus driver.  This is required to make
 	  use of PCI drivers using the VFIO framework.
drivers/vfio/pci/vfio_pci.c +11 -0
index 145addde983b..20006bb66430 100644
+++ b/drivers/vfio/pci/vfio_pci.c
@@ -552,6 +552,16 @@ static void vfio_pci_release(struct vfio_device *core_vdev)
 			vdev->req_trigger = NULL;
 		}
 		mutex_unlock(&vdev->igate);
+
+		mutex_lock(&vdev->videv_lock);
+		if (vdev->videv) {
+			struct vfio_iommufd_device *videv = vdev->videv;
+
+			vdev->videv = NULL;
+			iommufd_unbind_device(videv->idev);
+			kfree(videv);
+		}
+		mutex_unlock(&vdev->videv_lock);
 	}
 
 	mutex_unlock(&vdev->reflck->lock);

drivers/vfio/pci/vfio_pci.c static long vfio_pci_ioctl(struct vfio_device *core_vdev, · line 793

Quoted 5 lines
drivers/vfio/pci/vfio_pci.c +1 -1
@@ -780,7 +790,66 @@ static long vfio_pci_ioctl(struct vfio_device *core_vdev,
 		container_of(core_vdev, struct vfio_pci_device, vdev);
 	unsigned long minsz;
 
-	if (cmd == VFIO_DEVICE_GET_INFO) {
+	if (cmd == VFIO_DEVICE_BIND_IOMMUFD) {

Choosing to implement this through the ioctl multiplexor is what is causing so much ugly gyration in the previous patches

This should be a straightforward new function and ops:

struct iommufd_device *vfio_pci_bind_iommufd(struct vfio_device *)

{
		iommu_dev = iommufd_bind_device(bind_data.iommu_fd,
					   &vdev->pdev->dev,
					   bind_data.dev_cookie);
                if (!iommu_dev) return ERR
                vdev->iommu_dev = iommu_dev;
}

static const struct vfio_device_ops vfio_pci_ops = {
.bind_iommufd = &*vfio_pci_bind_iommufd

If you do the other stuff I said then you'll notice that the iommufd_bind_device() will provide automatic exclusivity.

The thread that sees ops->bind_device succeed will know it is the only thread that can see that (by definition, the iommu enable user stuff has to be exclusive and race free) thus it can go ahead and store the iommu pointer.

The other half of the problem '&vdev->block_access' is solved by manipulating the filp->f_ops. Start with a fops that can ONLY call the above op. When the above op succeeds switch the fops to the normal full ops. .

The same flow happens when the group fd spawns the device fd, just parts of iommfd_bind_device are open coded into the vfio code, but the whole flow and sequence should be the same.

drivers/vfio/pci/vfio_pci.c static long vfio_pci_ioctl(struct vfio_device *core_vdev, · line 804

Quoted 6 lines
drivers/vfio/pci/vfio_pci.c +6 -0
+		/*
+		 * Reject the request if the device is already opened and
+		 * attached to a container.
+		 */
+		if (vfio_device_in_container(core_vdev))
+			return -ENOTTY;

This is wrongly locked

Quoted 27 lines
drivers/vfio/pci/vfio_pci.c +27 -0
+
+		minsz = offsetofend(struct vfio_device_iommu_bind_data, dev_cookie);
+
+		if (copy_from_user(&bind_data, (void __user *)arg, minsz))
+			return -EFAULT;
+
+		if (bind_data.argsz < minsz ||
+		    bind_data.flags || bind_data.iommu_fd < 0)
+			return -EINVAL;
+
+		mutex_lock(&vdev->videv_lock);
+		/*
+		 * Allow only one iommufd per device until multiple
+		 * address spaces (e.g. vSVA) support is introduced
+		 * in the future.
+		 */
+		if (vdev->videv) {
+			mutex_unlock(&vdev->videv_lock);
+			return -EBUSY;
+		}
+
+		idev = iommufd_bind_device(bind_data.iommu_fd,
+					   &vdev->pdev->dev,
+					   bind_data.dev_cookie);
+		if (IS_ERR(idev)) {
+			mutex_unlock(&vdev->videv_lock);
+			return PTR_ERR(idev);

drivers/vfio/pci/vfio_pci.c static long vfio_pci_ioctl(struct vfio_device *core_vdev, · line 841

Quoted 10 lines
drivers/vfio/pci/vfio_pci.c +10 -0
+		}
+
+		videv = kzalloc(sizeof(*videv), GFP_KERNEL);
+		if (!videv) {
+			iommufd_unbind_device(idev);
+			mutex_unlock(&vdev->videv_lock);
+			return -ENOMEM;
+		}
+		videv->idev = idev;
+		videv->iommu_fd = bind_data.iommu_fd;

No need for more memory, a struct vfio_device can be attached to a single iommu context. If idev then the context and all the other information is valid.

drivers/vfio/pci/vfio_pci.c static long vfio_pci_ioctl(struct vfio_device *core_vdev, · line 847

Quoted 2 lines
drivers/vfio/pci/vfio_pci.c +2 -0
+		if (atomic_read(&vdev->block_access))
+			atomic_set(&vdev->block_access, 0);

I'm sure I'll tell you this is all wrongly locked too if I look closely.

Quoted 16 lines
drivers/vfio/pci/vfio_pci.c +16 -0
+/*
+ * VFIO_DEVICE_BIND_IOMMUFD - _IOR(VFIO_TYPE, VFIO_BASE + 19,
+ *				struct vfio_device_iommu_bind_data)
+ *
+ * Bind a vfio_device to the specified iommufd
+ *
+ * The user should provide a device cookie when calling this ioctl. The
+ * cookie is later used in iommufd for capability query, iotlb invalidation
+ * and I/O fault handling.
+ *
+ * User is not allowed to access the device before the binding operation
+ * is completed.
+ *
+ * Unbind is automatically conducted when device fd is closed.
+ *
+ * Input parameters:

drivers/vfio/pci/vfio_pci.c static long vfio_pci_ioctl(struct vfio_device *core_vdev,

Quoted 10 lines
drivers/vfio/pci/vfio_pci.c +10 -0
+ *	- iommu_fd;
+ *	- dev_cookie;
+ *
+ * Return: 0 on success, -errno on failure.
+ */
+struct vfio_device_iommu_bind_data {
+	__u32	argsz;
+	__u32	flags;
+	__s32	iommu_fd;
+	__u64	dev_cookie;

Missing explicit padding

Always use __aligned_u64 in uapi headers, fix all the patches.

Jason

#126 lore raw

Re: [RFC 08/20] vfio/pci: Add VFIO_DEVICE_BIND_IOMMUFD

Quoted 10 lines · On Tue, 21 Sep 2021 14:29:39 -0300 Jason Gunthorpe <[email protected]> wrote:

include/uapi/linux/vfio.h struct vfio_group_status { · line 218

Quoted 5 lines · Liu Yi L
include/uapi/linux/vfio.h +5 -0
+struct vfio_device_iommu_bind_data {
+	__u32	argsz;
+	__u32	flags;
+	__s32	iommu_fd;
+	__u64	dev_cookie;  

Missing explicit padding

Always use __aligned_u64 in uapi headers, fix all the patches.

We don't need padding or explicit alignment if we just swap the order of iommu_fd and dev_cookie. Thanks,

Alex

maintainer
#127 lore raw

Re: [RFC 08/20] vfio/pci: Add VFIO_DEVICE_BIND_IOMMUFD

Quoted 12 lines · Alex Williamson
Quoted 9 lines · On Tue, 21 Sep 2021 14:29:39 -0300 Jason Gunthorpe <[email protected]> wrote:

include/uapi/linux/vfio.h struct vfio_group_status { · line 218

Quoted 5 lines · Liu Yi L
include/uapi/linux/vfio.h +5 -0
+struct vfio_device_iommu_bind_data {
+	__u32	argsz;
+	__u32	flags;
+	__s32	iommu_fd;
+	__u64	dev_cookie;  

Missing explicit padding

Quoted 4 lines
Quoted 1 line

Always use __aligned_u64 in uapi headers, fix all the patches.

We don't need padding or explicit alignment if we just swap the order of iommu_fd and dev_cookie. Thanks,

Yes, the padding should all be checked and minimized

But it is always good practice to always use __aligned_u64 in the uapi headers just in case someone messes it up someday - it prevents small mistakes from becoming an ABI mess.

Jason

#128 lore raw

Re: [RFC 08/20] vfio/pci: Add VFIO_DEVICE_BIND_IOMMUFD

In reply to Liu Yi L
Quoted 6 lines · Liu Yi L

This patch adds VFIO_DEVICE_BIND_IOMMUFD for userspace to bind the vfio device to an iommufd. No VFIO_DEVICE_UNBIND_IOMMUFD interface is provided because it's implicitly done when the device fd is closed.

In concept a vfio device can be bound to multiple iommufds, each hosting a subset of I/O address spaces attached by this device.

I really feel like this many<->many mapping between devices is going to be super-confusing, and therefore make it really hard to be confident we have all the rules right for proper isolation.

That's why I was suggesting a concept like endpoints, to break this into two many<->one relationships. I'm ok if that isn't visible in the user API, but I think this is going to be really hard to keep track of if it isn't explicit somewhere in the internals.

Quoted 52 lines

However as a
starting point (matching current vfio), only one I/O address space is supported per vfio device. It implies one device can only be attached to one iommufd at this point.

---
 drivers/vfio/pci/Kconfig            |  1 +
 drivers/vfio/pci/vfio_pci.c         | 72 ++++++++++++++++++++++++++++-
 drivers/vfio/pci/vfio_pci_private.h |  8 ++++
 include/uapi/linux/vfio.h           | 30 ++++++++++++
 4 files changed, 110 insertions(+), 1 deletion(-)
drivers/vfio/pci/Kconfig +1 -0
index 5e2e1b9a9fd3..3abfb098b4dc 100644
@@ -5,6 +5,7 @@ config VFIO_PCI
 	depends on MMU
 	select VFIO_VIRQFD
 	select IRQ_BYPASS_MANAGER
+	select IOMMUFD
 	help
 	  Support for the PCI VFIO bus driver.  This is required to make
 	  use of PCI drivers using the VFIO framework.
drivers/vfio/pci/vfio_pci.c +11 -1
index 145addde983b..20006bb66430 100644
@@ -552,6 +552,16 @@ static void vfio_pci_release(struct vfio_device *core_vdev)
 			vdev->req_trigger = NULL;
 		}
 		mutex_unlock(&vdev->igate);
+
+		mutex_lock(&vdev->videv_lock);
+		if (vdev->videv) {
+			struct vfio_iommufd_device *videv = vdev->videv;
+
+			vdev->videv = NULL;
+			iommufd_unbind_device(videv->idev);
+			kfree(videv);
+		}
+		mutex_unlock(&vdev->videv_lock);
 	}
 
 	mutex_unlock(&vdev->reflck->lock);
@@ -780,7 +790,66 @@ static long vfio_pci_ioctl(struct vfio_device *core_vdev,
 		container_of(core_vdev, struct vfio_pci_device, vdev);
 	unsigned long minsz;
 
-	if (cmd == VFIO_DEVICE_GET_INFO) {
+	if (cmd == VFIO_DEVICE_BIND_IOMMUFD) {

drivers/vfio/pci/vfio_pci.c static long vfio_pci_ioctl(struct vfio_device *core_vdev, · line 803

Quoted 10 lines
drivers/vfio/pci/vfio_pci.c +10 -0
+		struct vfio_device_iommu_bind_data bind_data;
+		unsigned long minsz;
+		struct iommufd_device *idev;
+		struct vfio_iommufd_device *videv;
+
+		/*
+		 * Reject the request if the device is already opened and
+		 * attached to a container.
+		 */
+		if (vfio_device_in_container(core_vdev))

Usually one would do argument sanity checks before checks that actually depend on machine state.

drivers/vfio/pci/vfio_pci.c static long vfio_pci_ioctl(struct vfio_device *core_vdev, · line 804

Quoted 1 line
drivers/vfio/pci/vfio_pci.c +1 -0
+			return -ENOTTY;

This doesn't seem like the right error code. It's a perfectly valid operation for this device - just not available right now.

Quoted 72 lines
drivers/vfio/pci/vfio_pci.c +49 -0
+
+		minsz = offsetofend(struct vfio_device_iommu_bind_data, dev_cookie);
+
+		if (copy_from_user(&bind_data, (void __user *)arg, minsz))
+			return -EFAULT;
+
+		if (bind_data.argsz < minsz ||
+		    bind_data.flags || bind_data.iommu_fd < 0)
+			return -EINVAL;
+
+		mutex_lock(&vdev->videv_lock);
+		/*
+		 * Allow only one iommufd per device until multiple
+		 * address spaces (e.g. vSVA) support is introduced
+		 * in the future.
+		 */
+		if (vdev->videv) {
+			mutex_unlock(&vdev->videv_lock);
+			return -EBUSY;
+		}
+
+		idev = iommufd_bind_device(bind_data.iommu_fd,
+					   &vdev->pdev->dev,
+					   bind_data.dev_cookie);
+		if (IS_ERR(idev)) {
+			mutex_unlock(&vdev->videv_lock);
+			return PTR_ERR(idev);
+		}
+
+		videv = kzalloc(sizeof(*videv), GFP_KERNEL);
+		if (!videv) {
+			iommufd_unbind_device(idev);
+			mutex_unlock(&vdev->videv_lock);
+			return -ENOMEM;
+		}
+		videv->idev = idev;
+		videv->iommu_fd = bind_data.iommu_fd;
+		/*
+		 * A security context has been established. Unblock
+		 * user access.
+		 */
+		if (atomic_read(&vdev->block_access))
+			atomic_set(&vdev->block_access, 0);
+		vdev->videv = videv;
+		mutex_unlock(&vdev->videv_lock);
+
+		return 0;
+	} else if (cmd == VFIO_DEVICE_GET_INFO) {
 		struct vfio_device_info info;
 		struct vfio_info_cap caps = { .buf = NULL, .size = 0 };
 		unsigned long capsz;
@@ -2031,6 +2100,7 @@ static int vfio_pci_probe(struct pci_dev *pdev, const struct pci_device_id *id)
 	mutex_init(&vdev->vma_lock);
 	INIT_LIST_HEAD(&vdev->vma_list);
 	init_rwsem(&vdev->memory_lock);
+	mutex_init(&vdev->videv_lock);
 
 	ret = vfio_pci_reflck_attach(vdev);
 	if (ret)
drivers/vfio/pci/vfio_pci_private.h +1 -0
index f12012e30b53..bd784accac35 100644
@@ -14,6 +14,7 @@
 #include <linux/types.h>
 #include <linux/uuid.h>
 #include <linux/notifier.h>
+#include <linux/iommufd.h>
 
 #ifndef VFIO_PCI_PRIVATE_H
 #define VFIO_PCI_PRIVATE_H

drivers/vfio/pci/vfio_pci_private.h struct vfio_pci_mmap_vma { · line 104

Quoted 5 lines
drivers/vfio/pci/vfio_pci_private.h +2 -0
@@ -99,6 +100,11 @@ struct vfio_pci_mmap_vma {
 	struct list_head	vma_next;
 };
 
+struct vfio_iommufd_device {
+	struct iommufd_device *idev;

Could this be embedded to avoid multiple layers of pointers?

Quoted 56 lines
drivers/vfio/pci/vfio_pci_private.h +5 -0
+	int iommu_fd;
+};
+
 struct vfio_pci_device {
 	struct vfio_device	vdev;
 	struct pci_dev		*pdev;
@@ -144,6 +150,8 @@ struct vfio_pci_device {
 	struct list_head	vma_list;
 	struct rw_semaphore	memory_lock;
 	atomic_t		block_access;
+	struct mutex		videv_lock;
+	struct vfio_iommufd_device *videv;
 };
 
 #define is_intx(vdev) (vdev->irq_type == VFIO_PCI_INTX_IRQ_INDEX)
include/uapi/linux/vfio.h +30 -0
index ef33ea002b0b..c902abd60339 100644
@@ -190,6 +190,36 @@ struct vfio_group_status {
 
 /* --------------- IOCTLs for DEVICE file descriptors --------------- */
 
+/*
+ * VFIO_DEVICE_BIND_IOMMUFD - _IOR(VFIO_TYPE, VFIO_BASE + 19,
+ *				struct vfio_device_iommu_bind_data)
+ *
+ * Bind a vfio_device to the specified iommufd
+ *
+ * The user should provide a device cookie when calling this ioctl. The
+ * cookie is later used in iommufd for capability query, iotlb invalidation
+ * and I/O fault handling.
+ *
+ * User is not allowed to access the device before the binding operation
+ * is completed.
+ *
+ * Unbind is automatically conducted when device fd is closed.
+ *
+ * Input parameters:
+ *	- iommu_fd;
+ *	- dev_cookie;
+ *
+ * Return: 0 on success, -errno on failure.
+ */
+struct vfio_device_iommu_bind_data {
+	__u32	argsz;
+	__u32	flags;
+	__s32	iommu_fd;
+	__u64	dev_cookie;
+};
+
+#define VFIO_DEVICE_BIND_IOMMUFD	_IO(VFIO_TYPE, VFIO_BASE + 19)
+
 /**
  * VFIO_DEVICE_GET_INFO - _IOR(VFIO_TYPE, VFIO_BASE + 7,
  *						struct vfio_device_info)
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
maintainer
#129 lore raw

RE: [RFC 08/20] vfio/pci: Add VFIO_DEVICE_BIND_IOMMUFD

Quoted 8 lines

From: David Gibson <[email protected]>
Sent: Wednesday, September 29, 2021 2:01 PM

Quoted 1 line · Liu Yi L

This patch adds VFIO_DEVICE_BIND_IOMMUFD for userspace to bind the

vfio

Quoted 1 line

device to an iommufd. No VFIO_DEVICE_UNBIND_IOMMUFD interface is

provided

Quoted 8 lines
Quoted 4 lines

because it's implicitly done when the device fd is closed.

In concept a vfio device can be bound to multiple iommufds, each hosting a subset of I/O address spaces attached by this device.

I really feel like this many<->many mapping between devices is going to be super-confusing, and therefore make it really hard to be confident we have all the rules right for proper isolation.

Based on new discussion on group ownership part (patch06), I feel this many<->many relationship will disappear. The context fd (either container or iommufd) will uniquely mark the ownership on a physical device and its group. With this design it's impractical to have one device bound to multiple iommufds. Actually I don't think this is a compelling usage in reality. The previous rationale was that no need to impose such restriction if no special reason... and now we have a reason. 😊

Jason, are you OK with this simplification?

Quoted 6 lines

That's why I was suggesting a concept like endpoints, to break this into two many<->one relationships. I'm ok if that isn't visible in the user API, but I think this is going to be really hard to keep track of if it isn't explicit somewhere in the internals.

I think this endpoint concept is represented by ioas_device_info in patch14:

+/*
+ * An ioas_device_info object is created per each successful attaching
+ * request. A list of objects are maintained per ioas when the address
+ * space is shared by multiple devices.
+ */
+struct ioas_device_info {
+ struct iommufd_device *idev;
+ struct list_head next;
};

currently it's 1:1 mapping before this object and iommufd_device, because no pasid support yet.

We can rename it to struct ioas_endpoint if it makes you feel better.

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

maintainer
#130 lore raw

Re: [RFC 08/20] vfio/pci: Add VFIO_DEVICE_BIND_IOMMUFD

Quoted 17 lines · Kevin
Quoted 8 lines

From: David Gibson <[email protected]>
Sent: Wednesday, September 29, 2021 2:01 PM

Quoted 1 line · Liu Yi L

This patch adds VFIO_DEVICE_BIND_IOMMUFD for userspace to bind the

vfio

Quoted 1 line

device to an iommufd. No VFIO_DEVICE_UNBIND_IOMMUFD interface is

provided

Quoted 8 lines
Quoted 4 lines

because it's implicitly done when the device fd is closed.

In concept a vfio device can be bound to multiple iommufds, each hosting a subset of I/O address spaces attached by this device.

I really feel like this many<->many mapping between devices is going to be super-confusing, and therefore make it really hard to be confident we have all the rules right for proper isolation.

Quoted 5 lines

Based on new discussion on group ownership part (patch06), I feel this many<->many relationship will disappear. The context fd (either container or iommufd) will uniquely mark the ownership on a physical device and its group. With this design it's impractical to have one device bound to multiple iommufds.

That should be a requirement! We have no way to prove that two iommufds are the same security domain, so devices/groups cannot be shared.

That is why the API I suggested takes in a struct file to ID the user security context. A group is accessible only from that single struct file and no more.

If the first series goes the way I outlined then I think David's concern about security is strongly solved as the IOMMU layer is directly managing it with a very clear responsiblity and semantic.

Jason

maintainer
#131 lore raw

RE: [RFC 08/20] vfio/pci: Add VFIO_DEVICE_BIND_IOMMUFD

Quoted 34 lines

From: Jason Gunthorpe <[email protected]>
Sent: Wednesday, September 29, 2021 8:28 PM

Quoted 5 lines · Kevin
Quoted 5 lines

From: David Gibson <[email protected]>
Sent: Wednesday, September 29, 2021 2:01 PM

Quoted 1 line · Liu Yi L

This patch adds VFIO_DEVICE_BIND_IOMMUFD for userspace to bind

the

Quoted 2 lines
Quoted 2 lines

vfio

Quoted 1 line

device to an iommufd. No VFIO_DEVICE_UNBIND_IOMMUFD interface

is

Quoted 4 lines
Quoted 4 lines

provided

Quoted 3 lines

because it's implicitly done when the device fd is closed.

In concept a vfio device can be bound to multiple iommufds, each

hosting

Quoted 11 lines
Quoted 5 lines
Quoted 1 line

a subset of I/O address spaces attached by this device.

I really feel like this many<->many mapping between devices is going to be super-confusing, and therefore make it really hard to be confident we have all the rules right for proper isolation.

Based on new discussion on group ownership part (patch06), I feel this many<->many relationship will disappear. The context fd (either container or iommufd) will uniquely mark the ownership on a physical device and its group. With this design it's impractical to have one device bound to multiple iommufds.

That should be a requirement! We have no way to prove that two iommufds are the same security domain, so devices/groups cannot be shared.

Quoted 8 lines

That is why the API I suggested takes in a struct file to ID the user security context. A group is accessible only from that single struct file and no more.

If the first series goes the way I outlined then I think David's concern about security is strongly solved as the IOMMU layer is directly managing it with a very clear responsiblity and semantic.

Yes, this is also my understanding now.

#132 lore raw

Re: [RFC 08/20] vfio/pci: Add VFIO_DEVICE_BIND_IOMMUFD

In reply to Tian, Kevin
Quoted 47 lines · Kevin
Quoted 8 lines

From: David Gibson <[email protected]>
Sent: Wednesday, September 29, 2021 2:01 PM

Quoted 1 line · Liu Yi L

This patch adds VFIO_DEVICE_BIND_IOMMUFD for userspace to bind the

vfio

Quoted 1 line

device to an iommufd. No VFIO_DEVICE_UNBIND_IOMMUFD interface is

provided

Quoted 8 lines
Quoted 4 lines

because it's implicitly done when the device fd is closed.

In concept a vfio device can be bound to multiple iommufds, each hosting a subset of I/O address spaces attached by this device.

I really feel like this many<->many mapping between devices is going to be super-confusing, and therefore make it really hard to be confident we have all the rules right for proper isolation.

Based on new discussion on group ownership part (patch06), I feel this many<->many relationship will disappear. The context fd (either container or iommufd) will uniquely mark the ownership on a physical device and its group. With this design it's impractical to have one device bound to multiple iommufds. Actually I don't think this is a compelling usage in reality. The previous rationale was that no need to impose such restriction if no special reason... and now we have a reason. 😊

Jason, are you OK with this simplification?

Quoted 6 lines

That's why I was suggesting a concept like endpoints, to break this into two many<->one relationships. I'm ok if that isn't visible in the user API, but I think this is going to be really hard to keep track of if it isn't explicit somewhere in the internals.

I think this endpoint concept is represented by ioas_device_info in patch14:

(quoted diff) +8 -0
+/*
+ * An ioas_device_info object is created per each successful attaching
+ * request. A list of objects are maintained per ioas when the address
+ * space is shared by multiple devices.
+ */
+struct ioas_device_info {
+	struct iommufd_device *idev;
+	struct list_head next;
 };
Quoted 2 lines

currently it's 1:1 mapping before this object and iommufd_device, because no pasid support yet.

Ok, I haven't read that far in the series yet.

Quoted 2 lines

We can rename it to struct ioas_endpoint if it makes you feel better.

Meh. The concept is much more important than the name.

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
author
#133 lore raw

RFC 9/20 iommu: Add page size and address width attributes

In reply to Liu Yi L

From: Lu Baolu <[email protected]>

This exposes PAGE_SIZE and ADDR_WIDTH attributes. The iommufd could use them to define the IOAS.

---
 include/linux/iommu.h | 4 ++++
 1 file changed, 4 insertions(+)
include/linux/iommu.h +4 -0
index 943de6897f56..86d34e4ce05e 100644
@@ -153,9 +153,13 @@ enum iommu_dev_features {
 /**
  * enum iommu_devattr - Per device IOMMU attributes
  * @IOMMU_DEV_INFO_FORCE_SNOOP [bool]: IOMMU can force DMA to be snooped.
+ * @IOMMU_DEV_INFO_PAGE_SIZE [u64]: Page sizes that iommu supports.
+ * @IOMMU_DEV_INFO_ADDR_WIDTH [u32]: Address width supported.
  */
 enum iommu_devattr {
 	IOMMU_DEV_INFO_FORCE_SNOOP,
+	IOMMU_DEV_INFO_PAGE_SIZE,
+	IOMMU_DEV_INFO_ADDR_WIDTH,
 };
 
 #define IOMMU_PASID_INVALID	(-1U)
Signature · 6 lines
2.25.1

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

Re: [RFC 09/20] iommu: Add page size and address width attributes

Hi,

Quoted 15 lines · Liu Yi L

From: Lu Baolu <[email protected]>

This exposes PAGE_SIZE and ADDR_WIDTH attributes. The iommufd could use them to define the IOAS.

---
 include/linux/iommu.h | 4 ++++
 1 file changed, 4 insertions(+)
include/linux/iommu.h +0 -0
index 943de6897f56..86d34e4ce05e 100644

include/linux/iommu.h enum iommu_dev_features { · line 157

Quoted 5 lines
include/linux/iommu.h +2 -0
@@ -153,9 +153,13 @@ enum iommu_dev_features {
 /**
  * enum iommu_devattr - Per device IOMMU attributes
  * @IOMMU_DEV_INFO_FORCE_SNOOP [bool]: IOMMU can force DMA to be snooped.
+ * @IOMMU_DEV_INFO_PAGE_SIZE [u64]: Page sizes that iommu supports.
+ * @IOMMU_DEV_INFO_ADDR_WIDTH [u32]: Address width supported.

I think this deserves additional info. What address width do we talk about, input, output, what stage if the IOMMU does support multiple stages

Thanks

Eric

Quoted 8 lines
include/linux/iommu.h +2 -0
  */
 enum iommu_devattr {
 	IOMMU_DEV_INFO_FORCE_SNOOP,
+	IOMMU_DEV_INFO_PAGE_SIZE,
+	IOMMU_DEV_INFO_ADDR_WIDTH,
 };
 
 #define IOMMU_PASID_INVALID	(-1U)
maintainer
#135 lore raw

RE: [RFC 09/20] iommu: Add page size and address width attributes

Quoted 26 lines

From: Eric Auger <[email protected]>
Sent: Wednesday, September 22, 2021 9:43 PM

Hi,

Quoted 3 lines · Liu Yi L

From: Lu Baolu <[email protected]>

This exposes PAGE_SIZE and ADDR_WIDTH attributes. The iommufd could

use

Quoted 12 lines

them to define the IOAS.

---
 include/linux/iommu.h | 4 ++++
 1 file changed, 4 insertions(+)
include/linux/iommu.h +0 -0
index 943de6897f56..86d34e4ce05e 100644

include/linux/iommu.h enum iommu_dev_features { · line 155

Quoted 3 lines
include/linux/iommu.h +0 -0
@@ -153,9 +153,13 @@ enum iommu_dev_features {
 /**
  * enum iommu_devattr - Per device IOMMU attributes
  * @IOMMU_DEV_INFO_FORCE_SNOOP [bool]: IOMMU can force DMA to

be snooped.

Quoted 6 lines

include/linux/iommu.h enum iommu_dev_features { · line 156

Quoted 1 line
include/linux/iommu.h +1 -0
+ * @IOMMU_DEV_INFO_PAGE_SIZE [u64]: Page sizes that iommu

supports.

include/linux/iommu.h enum iommu_dev_features { · line 157

Quoted 1 line
include/linux/iommu.h +1 -0
+ * @IOMMU_DEV_INFO_ADDR_WIDTH [u32]: Address width supported.

I think this deserves additional info. What address width do we talk about, input, output, what stage if the IOMMU does support multiple stages

it describes the address space width, thus is about input.

when multiple stages are supported, each stage is represented by a separate ioasid, each with its own addr_width

author
#136 lore raw

RFC 10/20 iommu/iommufd: Add IOMMU_DEVICE_GET_INFO

In reply to Liu Yi L

After a device is bound to the iommufd, userspace can use this interface to query the underlying iommu capability and format info for this device. Based on this information the user then creates I/O address space in a compatible format with the to-be-attached devices.

Device cookie which is registered at binding time is used to mark the device which is being queried here.

---
 drivers/iommu/iommufd/iommufd.c | 68 +++++++++++++++++++++++++++++++++
 include/uapi/linux/iommu.h      | 49 ++++++++++++++++++++++++
 2 files changed, 117 insertions(+)
drivers/iommu/iommufd/iommufd.c +68 -0
index e16ca21e4534..641f199f2d41 100644
@@ -117,6 +117,71 @@ static int iommufd_fops_release(struct inode *inode, struct file *filep)
 	return 0;
 }
 
+static struct device *
+iommu_find_device_from_cookie(struct iommufd_ctx *ictx, u64 dev_cookie)
+{
+	struct iommufd_device *idev;
+	struct device *dev = NULL;
+	unsigned long index;
+
+	mutex_lock(&ictx->lock);
+	xa_for_each(&ictx->device_xa, index, idev) {
+		if (idev->dev_cookie == dev_cookie) {
+			dev = idev->dev;
+			break;
+		}
+	}
+	mutex_unlock(&ictx->lock);
+
+	return dev;
+}
+
+static void iommu_device_build_info(struct device *dev,
+				    struct iommu_device_info *info)
+{
+	bool snoop;
+	u64 awidth, pgsizes;
+
+	if (!iommu_device_get_info(dev, IOMMU_DEV_INFO_FORCE_SNOOP, &snoop))
+		info->flags |= snoop ? IOMMU_DEVICE_INFO_ENFORCE_SNOOP : 0;
+
+	if (!iommu_device_get_info(dev, IOMMU_DEV_INFO_PAGE_SIZE, &pgsizes)) {
+		info->pgsize_bitmap = pgsizes;
+		info->flags |= IOMMU_DEVICE_INFO_PGSIZES;
+	}
+
+	if (!iommu_device_get_info(dev, IOMMU_DEV_INFO_ADDR_WIDTH, &awidth)) {
+		info->addr_width = awidth;
+		info->flags |= IOMMU_DEVICE_INFO_ADDR_WIDTH;
+	}
+}
+
+static int iommufd_get_device_info(struct iommufd_ctx *ictx,
+				   unsigned long arg)
+{
+	struct iommu_device_info info;
+	unsigned long minsz;
+	struct device *dev;
+
+	minsz = offsetofend(struct iommu_device_info, addr_width);
+
+	if (copy_from_user(&info, (void __user *)arg, minsz))
+		return -EFAULT;
+
+	if (info.argsz < minsz)
+		return -EINVAL;
+
+	info.flags = 0;
+
+	dev = iommu_find_device_from_cookie(ictx, info.dev_cookie);
+	if (!dev)
+		return -EINVAL;
+
+	iommu_device_build_info(dev, &info);
+
+	return copy_to_user((void __user *)arg, &info, minsz) ? -EFAULT : 0;
+}
+
 static long iommufd_fops_unl_ioctl(struct file *filep,
 				   unsigned int cmd, unsigned long arg)
 {
@@ -127,6 +192,9 @@ static long iommufd_fops_unl_ioctl(struct file *filep,
 		return ret;
 
 	switch (cmd) {
+	case IOMMU_DEVICE_GET_INFO:
+		ret = iommufd_get_device_info(ictx, arg);
+		break;
 	default:
 		pr_err_ratelimited("unsupported cmd %u\n", cmd);
 		break;
include/uapi/linux/iommu.h +49 -0

55 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
maintainer
#137 lore raw

Re: [RFC 10/20] iommu/iommufd: Add IOMMU_DEVICE_GET_INFO

Quoted 17 lines · Liu Yi L

After a device is bound to the iommufd, userspace can use this interface to query the underlying iommu capability and format info for this device. Based on this information the user then creates I/O address space in a compatible format with the to-be-attached devices.

Device cookie which is registered at binding time is used to mark the device which is being queried here.

 drivers/iommu/iommufd/iommufd.c | 68 +++++++++++++++++++++++++++++++++
 include/uapi/linux/iommu.h      | 49 ++++++++++++++++++++++++
 2 files changed, 117 insertions(+)
drivers/iommu/iommufd/iommufd.c +1 -0
index e16ca21e4534..641f199f2d41 100644
+++ b/drivers/iommu/iommufd/iommufd.c

drivers/iommu/iommufd/iommufd.c static int iommufd_fops_release(struct inode *inode, struct file *filep) · line 122

Quoted 6 lines
drivers/iommu/iommufd/iommufd.c +3 -0
@@ -117,6 +117,71 @@ static int iommufd_fops_release(struct inode *inode, struct file *filep)
 	return 0;
 }
 
+static struct device *
+iommu_find_device_from_cookie(struct iommufd_ctx *ictx, u64 dev_cookie)
+{

We have an xarray ID for the device, why are we allowing userspace to use the dev_cookie as input?

Userspace should always pass in the ID. The only place dev_cookie should appear is if the kernel generates an event back to userspace. Then the kernel should return both the ID and the dev_cookie in the event to allow userspace to correlate it.

Quoted 8 lines
drivers/iommu/iommufd/iommufd.c +8 -0
+static void iommu_device_build_info(struct device *dev,
+				    struct iommu_device_info *info)
+{
+	bool snoop;
+	u64 awidth, pgsizes;
+
+	if (!iommu_device_get_info(dev, IOMMU_DEV_INFO_FORCE_SNOOP, &snoop))
+		info->flags |= snoop ? IOMMU_DEVICE_INFO_ENFORCE_SNOOP : 0;

drivers/iommu/iommufd/iommufd.c static int iommufd_fops_release(struct inode *inode, struct file *filep) · line 156

Quoted 10 lines
drivers/iommu/iommufd/iommufd.c +10 -0
+
+	if (!iommu_device_get_info(dev, IOMMU_DEV_INFO_PAGE_SIZE, &pgsizes)) {
+		info->pgsize_bitmap = pgsizes;
+		info->flags |= IOMMU_DEVICE_INFO_PGSIZES;
+	}
+
+	if (!iommu_device_get_info(dev, IOMMU_DEV_INFO_ADDR_WIDTH, &awidth)) {
+		info->addr_width = awidth;
+		info->flags |= IOMMU_DEVICE_INFO_ADDR_WIDTH;
+	}

Another good option is to push the iommu_device_info uAPI struct down through to the iommu driver to fill it in and forget about the crazy enum.

A big part of thinking of this iommu interface is a way to bind the HW IOMMU driver to a uAPI and allow the HW driver to expose its unique functionalities.

Quoted 4 lines
drivers/iommu/iommufd/iommufd.c +4 -0
+static int iommufd_get_device_info(struct iommufd_ctx *ictx,
+				   unsigned long arg)
+{
+	struct iommu_device_info info;

drivers/iommu/iommufd/iommufd.c static int iommufd_fops_release(struct inode *inode, struct file *filep) · line 172

Quoted 10 lines
drivers/iommu/iommufd/iommufd.c +10 -0
+	unsigned long minsz;
+	struct device *dev;
+
+	minsz = offsetofend(struct iommu_device_info, addr_width);
+
+	if (copy_from_user(&info, (void __user *)arg, minsz))
+		return -EFAULT;
+
+	if (info.argsz < minsz)
+		return -EINVAL;

All of these patterns everywhere are wrongly coded for forward/back compatibility.

static int iommufd_get_device_info(struct iommufd_ctx *ictx,
struct iommu_device_info __user *arg, size_t usize)

{
	struct iommu_device_info info;
	int ret;

	if (usize < offsetofend(struct iommu_device_info, addr_flags))
           return -EINVAL;

        ret = copy_struct_from_user(&info, sizeof(info), arg, usize);
        if (ret)
	      return ret;

'usize' should be in a 'common' header extracted by the main ioctl handler.

drivers/iommu/iommufd/iommufd.c static int iommufd_fops_release(struct inode *inode, struct file *filep) · line 164

Quoted 10 lines
drivers/iommu/iommufd/iommufd.c +10 -0
+struct iommu_device_info {
+	__u32	argsz;
+	__u32	flags;
+#define IOMMU_DEVICE_INFO_ENFORCE_SNOOP	(1 << 0) /* IOMMU enforced snoop */
+#define IOMMU_DEVICE_INFO_PGSIZES	(1 << 1) /* supported page sizes */
+#define IOMMU_DEVICE_INFO_ADDR_WIDTH	(1 << 2) /* addr_wdith field valid */
+	__u64	dev_cookie;
+	__u64   pgsize_bitmap;
+	__u32	addr_width;
+};

Be explicit with padding here too.

Jason

maintainer
#138 lore raw

RE: [RFC 10/20] iommu/iommufd: Add IOMMU_DEVICE_GET_INFO

Quoted 32 lines

From: Jason Gunthorpe <[email protected]>
Sent: Wednesday, September 22, 2021 1:41 AM

Quoted 10 lines · Liu Yi L

After a device is bound to the iommufd, userspace can use this interface to query the underlying iommu capability and format info for this device. Based on this information the user then creates I/O address space in a compatible format with the to-be-attached devices.

Device cookie which is registered at binding time is used to mark the device which is being queried here.

 drivers/iommu/iommufd/iommufd.c | 68

+++++++++++++++++++++++++++++++++

Review

Quoted 4 lines
 include/uapi/linux/iommu.h      | 49 ++++++++++++++++++++++++
 2 files changed, 117 insertions(+)
(quoted diff) +0 -0

b/drivers/iommu/iommufd/iommufd.c

Review static int iommufd_fops_release(struct inode

Quoted 3 lines

index e16ca21e4534..641f199f2d41 100644
+++ b/drivers/iommu/iommufd/iommufd.c

(quoted diff) +0 -0
@@ -117,6 +117,71 @@ static int iommufd_fops_release(struct inode

*inode, struct file *filep)

drivers/iommu/iommufd/iommufd.c static int iommufd_fops_release(struct inode *inode, struct file *filep)

Quoted 5 lines
drivers/iommu/iommufd/iommufd.c +2 -0
 	return 0;
 }
 
+static struct device *
+iommu_find_device_from_cookie(struct iommufd_ctx *ictx, u64

dev_cookie)

Review static int iommufd_fops_release(struct inode · line 122

Quoted 1 line
(quoted diff) +1 -0
+{
Quoted 8 lines

We have an xarray ID for the device, why are we allowing userspace to use the dev_cookie as input?

Userspace should always pass in the ID. The only place dev_cookie should appear is if the kernel generates an event back to userspace. Then the kernel should return both the ID and the dev_cookie in the event to allow userspace to correlate it.

A little background.

In earlier design proposal we discussed two options. One is to return an kernel-allocated ID (label) to userspace. The other is to have user register a cookie and use it in iommufd uAPI. At that time the two options were discussed exclusively and the cookie one is preferred.

Now you instead recommended a mixed option. We can follow it for sure if nobody objects.

Thanks
Kevin

maintainer
#139 lore raw

Re: [RFC 10/20] iommu/iommufd: Add IOMMU_DEVICE_GET_INFO

Quoted 43 lines · Kevin
Quoted 32 lines

From: Jason Gunthorpe <[email protected]>
Sent: Wednesday, September 22, 2021 1:41 AM

Quoted 10 lines · Liu Yi L

After a device is bound to the iommufd, userspace can use this interface to query the underlying iommu capability and format info for this device. Based on this information the user then creates I/O address space in a compatible format with the to-be-attached devices.

Device cookie which is registered at binding time is used to mark the device which is being queried here.

 drivers/iommu/iommufd/iommufd.c | 68

+++++++++++++++++++++++++++++++++

Review

Quoted 4 lines
 include/uapi/linux/iommu.h      | 49 ++++++++++++++++++++++++
 2 files changed, 117 insertions(+)
(quoted diff) +0 -0

b/drivers/iommu/iommufd/iommufd.c

Review static int iommufd_fops_release(struct inode

Quoted 3 lines

index e16ca21e4534..641f199f2d41 100644
+++ b/drivers/iommu/iommufd/iommufd.c

(quoted diff) +0 -0
@@ -117,6 +117,71 @@ static int iommufd_fops_release(struct inode

*inode, struct file *filep)

drivers/iommu/iommufd/iommufd.c static int iommufd_fops_release(struct inode *inode, struct file *filep)

Quoted 5 lines
drivers/iommu/iommufd/iommufd.c +2 -0
 	return 0;
 }
 
+static struct device *
+iommu_find_device_from_cookie(struct iommufd_ctx *ictx, u64

dev_cookie)

Review static int iommufd_fops_release(struct inode · line 122

Quoted 1 line
(quoted diff) +1 -0
+{
Quoted 8 lines

We have an xarray ID for the device, why are we allowing userspace to use the dev_cookie as input?

Userspace should always pass in the ID. The only place dev_cookie should appear is if the kernel generates an event back to userspace. Then the kernel should return both the ID and the dev_cookie in the event to allow userspace to correlate it.

A little background.

Quoted 7 lines

In earlier design proposal we discussed two options. One is to return an kernel-allocated ID (label) to userspace. The other is to have user register a cookie and use it in iommufd uAPI. At that time the two options were discussed exclusively and the cookie one is preferred.

Now you instead recommended a mixed option. We can follow it for sure if nobody objects.

Either or for the return is fine, I'd return both just because it is more flexable

But the cookie should never be an input from userspace, and the kernel should never search for it. Locating the kernel object is what the ID and xarray is for.

Jason

#140 lore raw

Re: [RFC 10/20] iommu/iommufd: Add IOMMU_DEVICE_GET_INFO

Quoted 52 lines · Jason Gunthorpe
Quoted 43 lines · Kevin
Quoted 32 lines

From: Jason Gunthorpe <[email protected]>
Sent: Wednesday, September 22, 2021 1:41 AM

Quoted 10 lines · Liu Yi L

After a device is bound to the iommufd, userspace can use this interface to query the underlying iommu capability and format info for this device. Based on this information the user then creates I/O address space in a compatible format with the to-be-attached devices.

Device cookie which is registered at binding time is used to mark the device which is being queried here.

 drivers/iommu/iommufd/iommufd.c | 68

+++++++++++++++++++++++++++++++++

Review

Quoted 4 lines
 include/uapi/linux/iommu.h      | 49 ++++++++++++++++++++++++
 2 files changed, 117 insertions(+)
(quoted diff) +0 -0

b/drivers/iommu/iommufd/iommufd.c

Review static int iommufd_fops_release(struct inode

Quoted 3 lines

index e16ca21e4534..641f199f2d41 100644
+++ b/drivers/iommu/iommufd/iommufd.c

(quoted diff) +0 -0
@@ -117,6 +117,71 @@ static int iommufd_fops_release(struct inode

*inode, struct file *filep)

drivers/iommu/iommufd/iommufd.c static int iommufd_fops_release(struct inode *inode, struct file *filep)

Quoted 5 lines
drivers/iommu/iommufd/iommufd.c +2 -0
 	return 0;
 }
 
+static struct device *
+iommu_find_device_from_cookie(struct iommufd_ctx *ictx, u64

dev_cookie)

Review static int iommufd_fops_release(struct inode · line 122

Quoted 1 line
(quoted diff) +1 -0
+{
Quoted 8 lines

We have an xarray ID for the device, why are we allowing userspace to use the dev_cookie as input?

Userspace should always pass in the ID. The only place dev_cookie should appear is if the kernel generates an event back to userspace. Then the kernel should return both the ID and the dev_cookie in the event to allow userspace to correlate it.

A little background.

Quoted 7 lines

In earlier design proposal we discussed two options. One is to return an kernel-allocated ID (label) to userspace. The other is to have user register a cookie and use it in iommufd uAPI. At that time the two options were discussed exclusively and the cookie one is preferred.

Now you instead recommended a mixed option. We can follow it for sure if nobody objects.

Quoted 6 lines

Either or for the return is fine, I'd return both just because it is more flexable

But the cookie should never be an input from userspace, and the kernel should never search for it. Locating the kernel object is what the ID and xarray is for.

Why do we need two IDs at all? Can't we just use the cookie as the sole ID?

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
#141 lore raw

Re: [RFC 10/20] iommu/iommufd: Add IOMMU_DEVICE_GET_INFO

In reply to Liu Yi L

include/uapi/linux/iommu.h line 50

Quoted 4 lines · On Sun, 19 Sep 2021 14:38:38 +0800 Liu Yi L <[email protected]> wrote:
include/uapi/linux/iommu.h +4 -0
+struct iommu_device_info {
+	__u32	argsz;
+	__u32	flags;
+#define IOMMU_DEVICE_INFO_ENFORCE_SNOOP	(1 << 0) /* IOMMU enforced snoop */

Is this too PCI specific, or perhaps too much of the mechanism rather than the result? ie. should we just indicate if the IOMMU guarantees coherent DMA? Thanks,

Alex

Quoted 11 lines
include/uapi/linux/iommu.h +8 -0
+#define IOMMU_DEVICE_INFO_PGSIZES	(1 << 1) /* supported page sizes */
+#define IOMMU_DEVICE_INFO_ADDR_WIDTH	(1 << 2) /* addr_wdith field valid */
+	__u64	dev_cookie;
+	__u64   pgsize_bitmap;
+	__u32	addr_width;
+};
+
+#define IOMMU_DEVICE_GET_INFO	_IO(IOMMU_TYPE, IOMMU_BASE + 1)
 
 #define IOMMU_FAULT_PERM_READ	(1 << 0) /* read */
 #define IOMMU_FAULT_PERM_WRITE	(1 << 1) /* write */
maintainer
#142 lore raw

Re: [RFC 10/20] iommu/iommufd: Add IOMMU_DEVICE_GET_INFO

Quoted 11 lines · Alex Williamson

include/uapi/linux/iommu.h line 50

Quoted 4 lines · On Sun, 19 Sep 2021 14:38:38 +0800 Liu Yi L <[email protected]> wrote:
include/uapi/linux/iommu.h +4 -0
+struct iommu_device_info {
+	__u32	argsz;
+	__u32	flags;
+#define IOMMU_DEVICE_INFO_ENFORCE_SNOOP	(1 << 0) /* IOMMU enforced snoop */

Is this too PCI specific, or perhaps too much of the mechanism rather than the result? ie. should we just indicate if the IOMMU guarantees coherent DMA? Thanks,

I think the name of "coherent DMA" for this feature inside the kernel is very, very confusing. We already have something called coherent dma and this usage on Intel has nothing at all to do with that.

In fact it looks like this confusing name has already caused implementation problems as I see dma-iommu, is connecting dev->dma_coherent to IOMMU_CACHE! eg in dma_info_to_prot(). This is completely wrong if IOMMU_CACHE is linked to no_snoop.

And ARM seems to have fallen out of step with x86 as the ARM IOMMU drivers are mapping IOMMU_CACHE to ARM_LPAE_PTE_MEMATTR_OIWB, ARM_LPAE_MAIR_ATTR_IDX_CACHE

The SMMU spec for ARMv8 is pretty clear:

13.6.1.1 No_snoop

Support for No_snoop is system-dependent and, if implemented, No_snoop transforms a final access attribute of a Normal cacheable type to Normal-iNC-oNC-OSH downstream of (or appearing to be performed downstream of) the SMMU. No_snoop does not transform a final access attribute of any-Device.

Meaning setting ARM_LPAE_MAIR_ATTR_IDX_CACHE from IOMMU_CACHE does NOT block non-snoop, in fact it *enables* it - the reverse of what Intel is doing!

So this is all a mess.

Better to start clear and unambiguous names in the uAPI and someone can try to clean up the kernel eventually.

The required behavior for iommufd is to have the IOMMU ignore the no-snoop bit so that Intel HW can disable wbinvd. This bit should be clearly documented for its exact purpose and if other arches also have instructions that need to be disabled if snoop TLPs are allowed then they can re-use this bit. It appears ARM does not have this issue and does not need the bit.

What ARM is doing with IOMMU_CACHE is unclear to me, and I'm unclear if/how iommufd should expose it as a controllable PTE flag. The ARM

Jason

maintainer
#143 lore raw

RE: [RFC 10/20] iommu/iommufd: Add IOMMU_DEVICE_GET_INFO

Quoted 4 lines

From: Jason Gunthorpe <[email protected]>
Sent: Thursday, September 23, 2021 7:50 AM

On Wed, Sep 22, 2021 at 03:24:07PM -0600, Alex Williamson wrote:

Quoted 10 lines
Quoted 7 lines

include/uapi/linux/iommu.h

Quoted 4 lines · On Sun, 19 Sep 2021 14:38:38 +0800 Liu Yi L <[email protected]> wrote:
include/uapi/linux/iommu.h +4 -0
+struct iommu_device_info {
+	__u32	argsz;
+	__u32	flags;
+#define IOMMU_DEVICE_INFO_ENFORCE_SNOOP	(1 << 0) /* IOMMU

enforced snoop */

Quoted 2 lines

Is this too PCI specific, or perhaps too much of the mechanism rather

Isn't snoop vs. !snoop a general concept not pci specific?

Quoted 26 lines
Quoted 2 lines

than the result? ie. should we just indicate if the IOMMU guarantees coherent DMA? Thanks,

I think the name of "coherent DMA" for this feature inside the kernel is very, very confusing. We already have something called coherent dma and this usage on Intel has nothing at all to do with that.

In fact it looks like this confusing name has already caused implementation problems as I see dma-iommu, is connecting dev->dma_coherent to IOMMU_CACHE! eg in dma_info_to_prot(). This is completely wrong if IOMMU_CACHE is linked to no_snoop.

And ARM seems to have fallen out of step with x86 as the ARM IOMMU drivers are mapping IOMMU_CACHE to ARM_LPAE_PTE_MEMATTR_OIWB, ARM_LPAE_MAIR_ATTR_IDX_CACHE

The SMMU spec for ARMv8 is pretty clear:

13.6.1.1 No_snoop

Support for No_snoop is system-dependent and, if implemented, No_snoop transforms a final access attribute of a Normal cacheable type to Normal-iNC-oNC-OSH downstream of (or appearing to be performed downstream of) the SMMU. No_snoop does not transform a final access attribute of any-Device.

Quoted 4 lines

Meaning setting ARM_LPAE_MAIR_ATTR_IDX_CACHE from IOMMU_CACHE does NOT
block non-snoop, in fact it *enables* it - the reverse of what Intel is doing!

Checking the code:

        if (data->iop.fmt == ARM_64_LPAE_S2 ||
            data->iop.fmt == ARM_32_LPAE_S2) {
                if (prot & IOMMU_MMIO)
                        pte |= ARM_LPAE_PTE_MEMATTR_DEV;
                else if (prot & IOMMU_CACHE)
                        pte |= ARM_LPAE_PTE_MEMATTR_OIWB;
                else
                        pte |= ARM_LPAE_PTE_MEMATTR_NC;

It does set attribute to WB for IOMMU_CACHE and then NC (Non-cacheable) for !IOMMU_CACHE. The main difference between Intel and ARM is that Intel by default allows both snoop and non-snoop traffic with one additional bit to enforce snoop, while ARM requires explicit SMMU configuration for snoop and non-snoop respectively.

        } else {
                if (prot & IOMMU_MMIO)
                        pte |= (ARM_LPAE_MAIR_ATTR_IDX_DEV
                                << ARM_LPAE_PTE_ATTRINDX_SHIFT);
                else if (prot & IOMMU_CACHE)
                        pte |= (ARM_LPAE_MAIR_ATTR_IDX_CACHE
                                << ARM_LPAE_PTE_ATTRINDX_SHIFT);
        }

same for this one. MAIR_ELx register is programmed to ARM_LPAE_MAIR_ ATTR_WBRWA for IDX_CACHE bit. I'm not sure why it doesn't use IDX_NC though, when !IOMMU_CACHE.

Quoted 6 lines

So this is all a mess.

Better to start clear and unambiguous names in the uAPI and someone can try to clean up the kernel eventually.

Quoted 6 lines

The required behavior for iommufd is to have the IOMMU ignore the no-snoop bit so that Intel HW can disable wbinvd. This bit should be clearly documented for its exact purpose and if other arches also have instructions that need to be disabled if snoop TLPs are allowed then they can re-use this bit. It appears ARM does not have this issue and does not need the bit.

Disabling wbinvd is one purpose. imo the more important intention is that iommu vendor uses different PTE formats between snoop and !snoop. As long as we want allow userspace to opt in case of isoch performance requirement (unlike current vfio which always choose snoop format if available), such mechanism is required for all vendors.

When creating an ioas there could be three snoop modes:

1) snoop for all attached devices;
2) non-snoop for all attached devices;
3) device-selected snoop;

Intel supports 1) <enforce-snoop on> and 3) <enforce-snoop off>. snoop and nonsnoop devices can be attached to a same ioas in 3).

ARM supports 1) <snoop format> and 2) <nonsnoop format>. snoop devices and nonsnoop devices must be attached to different ioas's in 1) and 2) respectively.

Then the device info should reports:

/* iommu enforced snoop */
+#define IOMMU_DEVICE_INFO_ENFORCE_SNOOP (1 << 0)
/* iommu enforced nonsnoop */
+#define IOMMU_DEVICE_INFO_ENFORCE_NONSNOOP (1 << 1)
/* device selected snoop */
+#define IOMMU_DEVICE_INFO_DEVICE_SNOOP (1 << 2)

Quoted 4 lines

What ARM is doing with IOMMU_CACHE is unclear to me, and I'm unclear if/how iommufd should expose it as a controllable PTE flag. The ARM

Based on above analysis I think the ARM usage with IOMMU_CACHE doesn't change.

Thanks
Kevin

#144 lore raw

Re: [RFC 10/20] iommu/iommufd: Add IOMMU_DEVICE_GET_INFO

Quoted 74 lines · Kevin
Quoted 4 lines

From: Jason Gunthorpe <[email protected]>
Sent: Thursday, September 23, 2021 7:50 AM

On Wed, Sep 22, 2021 at 03:24:07PM -0600, Alex Williamson wrote:

Quoted 10 lines
Quoted 7 lines

include/uapi/linux/iommu.h

Quoted 4 lines · On Sun, 19 Sep 2021 14:38:38 +0800 Liu Yi L <[email protected]> wrote:
include/uapi/linux/iommu.h +4 -0
+struct iommu_device_info {
+	__u32	argsz;
+	__u32	flags;
+#define IOMMU_DEVICE_INFO_ENFORCE_SNOOP	(1 << 0) /* IOMMU

enforced snoop */

Quoted 2 lines

Is this too PCI specific, or perhaps too much of the mechanism rather

Isn't snoop vs. !snoop a general concept not pci specific?

Quoted 26 lines
Quoted 2 lines

than the result? ie. should we just indicate if the IOMMU guarantees coherent DMA? Thanks,

I think the name of "coherent DMA" for this feature inside the kernel is very, very confusing. We already have something called coherent dma and this usage on Intel has nothing at all to do with that.

In fact it looks like this confusing name has already caused implementation problems as I see dma-iommu, is connecting dev->dma_coherent to IOMMU_CACHE! eg in dma_info_to_prot(). This is completely wrong if IOMMU_CACHE is linked to no_snoop.

And ARM seems to have fallen out of step with x86 as the ARM IOMMU drivers are mapping IOMMU_CACHE to ARM_LPAE_PTE_MEMATTR_OIWB, ARM_LPAE_MAIR_ATTR_IDX_CACHE

The SMMU spec for ARMv8 is pretty clear:

13.6.1.1 No_snoop

Support for No_snoop is system-dependent and, if implemented, No_snoop transforms a final access attribute of a Normal cacheable type to Normal-iNC-oNC-OSH downstream of (or appearing to be performed downstream of) the SMMU. No_snoop does not transform a final access attribute of any-Device.

Quoted 4 lines

Meaning setting ARM_LPAE_MAIR_ATTR_IDX_CACHE from IOMMU_CACHE does NOT
block non-snoop, in fact it *enables* it - the reverse of what Intel is doing!

Checking the code:

        if (data->iop.fmt == ARM_64_LPAE_S2 ||
            data->iop.fmt == ARM_32_LPAE_S2) {
                if (prot & IOMMU_MMIO)
                        pte |= ARM_LPAE_PTE_MEMATTR_DEV;
                else if (prot & IOMMU_CACHE)
                        pte |= ARM_LPAE_PTE_MEMATTR_OIWB;
                else
                        pte |= ARM_LPAE_PTE_MEMATTR_NC;

It does set attribute to WB for IOMMU_CACHE and then NC (Non-cacheable) for !IOMMU_CACHE. The main difference between Intel and ARM is that Intel by default allows both snoop and non-snoop traffic with one additional bit to enforce snoop, while ARM requires explicit SMMU configuration for snoop and non-snoop respectively.

        } else {
                if (prot & IOMMU_MMIO)
                        pte |= (ARM_LPAE_MAIR_ATTR_IDX_DEV
                                << ARM_LPAE_PTE_ATTRINDX_SHIFT);
                else if (prot & IOMMU_CACHE)
                        pte |= (ARM_LPAE_MAIR_ATTR_IDX_CACHE
                                << ARM_LPAE_PTE_ATTRINDX_SHIFT);
        }
Quoted 3 lines

same for this one. MAIR_ELx register is programmed to ARM_LPAE_MAIR_ ATTR_WBRWA for IDX_CACHE bit. I'm not sure why it doesn't use IDX_NC though, when !IOMMU_CACHE.

It is in effect since IDX_NC == 0

Quoted 26 lines
Quoted 7 lines

So this is all a mess.

Better to start clear and unambiguous names in the uAPI and someone can try to clean up the kernel eventually.

Quoted 6 lines

The required behavior for iommufd is to have the IOMMU ignore the no-snoop bit so that Intel HW can disable wbinvd. This bit should be clearly documented for its exact purpose and if other arches also have instructions that need to be disabled if snoop TLPs are allowed then they can re-use this bit. It appears ARM does not have this issue and does not need the bit.

Disabling wbinvd is one purpose. imo the more important intention is that iommu vendor uses different PTE formats between snoop and !snoop. As long as we want allow userspace to opt in case of isoch performance requirement (unlike current vfio which always choose snoop format if available), such mechanism is required for all vendors.

When creating an ioas there could be three snoop modes:

1) snoop for all attached devices;
2) non-snoop for all attached devices;
3) device-selected snoop;

Quoted 6 lines

Intel supports 1) <enforce-snoop on> and 3) <enforce-snoop off>. snoop and nonsnoop devices can be attached to a same ioas in 3).

ARM supports 1) <snoop format> and 2) <nonsnoop format>. snoop devices and nonsnoop devices must be attached to different ioas's in 1) and 2) respectively.

I think Arm mainly supports 3), ie. No_snoop PCI transactions on pages mapped cacheable become non-cacheable memory accesses.

But the Arm Base System Architecture 1.0
(https://developer.arm.com/documentation/den0094/a) states that it's implementation dependent whether the system supports No_snoop.

In the case where the system has a System MMU translating and attributing the transactions from the root complex, the PCI Express transactions must keep the memory attributes assigned by the System MMU. If the System MMU-assigned attribute is cacheable then it is IMPLEMENTATION DEFINED if No_snoop transactions replace the attribute with non-cached.

So we can only tell userspace "No_snoop is not supported" (provided we even want to allow them to enable No_snoop). Users in control of stage-1 tables can create non-cacheable mappings through MAIR attributes.

Thanks,
Jean

Quoted 20 lines

Then the device info should reports:

/* iommu enforced snoop */
+#define IOMMU_DEVICE_INFO_ENFORCE_SNOOP (1 << 0)
/* iommu enforced nonsnoop */
+#define IOMMU_DEVICE_INFO_ENFORCE_NONSNOOP (1 << 1)
/* device selected snoop */
+#define IOMMU_DEVICE_INFO_DEVICE_SNOOP (1 << 2)

Quoted 4 lines

What ARM is doing with IOMMU_CACHE is unclear to me, and I'm unclear if/how iommufd should expose it as a controllable PTE flag. The ARM

Based on above analysis I think the ARM usage with IOMMU_CACHE doesn't change.

Thanks
Kevin

maintainer
#145 lore raw

Re: [RFC 10/20] iommu/iommufd: Add IOMMU_DEVICE_GET_INFO

Quoted 3 lines · Jean-Philippe Brucker

So we can only tell userspace "No_snoop is not supported" (provided we even want to allow them to enable No_snoop). Users in control of stage-1 tables can create non-cacheable mappings through MAIR attributes.

My point is that ARM is using IOMMU_CACHE to control the overall cachability of the DMA

ie not specifying IOMMU_CACHE requires using the arch specific DMA cache flushers.

Intel never uses arch specifc DMA cache flushers, and instead is abusing IOMMU_CACHE to mean IOMMU_BLOCK_NO_SNOOP on DMA that is always cachable.

These are different things and need different bits. Since the ARM path has a lot more code supporting it, I'd suggest Intel should change their code to use IOMMU_BLOCK_NO_SNOOP and abandon IOMMU_CACHE.

Which clarifies what to do here as uAPI - these things need to have different bits and Intel's should still have NO SNOOP in the name. What the no-snoop bit is called on other busses can be clarified in comments if that case ever arises.

Jason

maintainer
#146 lore raw

RE: [RFC 10/20] iommu/iommufd: Add IOMMU_DEVICE_GET_INFO

Quoted 12 lines

From: Jason Gunthorpe <[email protected]>
Sent: Thursday, September 23, 2021 7:27 PM

Quoted 3 lines · Jean-Philippe Brucker

So we can only tell userspace "No_snoop is not supported" (provided we even want to allow them to enable No_snoop). Users in control of stage-1 tables can create non-cacheable mappings through MAIR attributes.

My point is that ARM is using IOMMU_CACHE to control the overall cachability of the DMA

Quoted 7 lines

ie not specifying IOMMU_CACHE requires using the arch specific DMA cache flushers.

Intel never uses arch specifc DMA cache flushers, and instead is abusing IOMMU_CACHE to mean IOMMU_BLOCK_NO_SNOOP on DMA that is always
cachable.

it uses IOMMU_CACHE to force all DMAs to snoop, including those which has non_snoop flag and wouldn't snoop cache if iommu is disabled. Nothing is blocked.

but why do you call it abuse? IOMMU_CACHE was first introduced for Intel platform:

commit 9cf0669746be19a4906a6c48920060bcf54c708b

Author: Sheng Yang <[email protected]>
Date:   Wed Mar 18 15:33:07 2009 +0800

intel-iommu: VT-d page table to support snooping control bit

The user can request to enable snooping control through VT-d page table.

Quoted 4 lines

These are different things and need different bits. Since the ARM path has a lot more code supporting it, I'd suggest Intel should change their code to use IOMMU_BLOCK_NO_SNOOP and abandon IOMMU_CACHE.

I didn't fully get this point. The end result is same, i.e. making the DMA cache-coherent when IOMMU_CACHE is set. Or if you help define the behavior of IOMMU_CACHE, what will you define now?

Quoted 7 lines

Which clarifies what to do here as uAPI - these things need to have different bits and Intel's should still have NO SNOOP in the name. What the no-snoop bit is called on other busses can be clarified in comments if that case ever arises.

Jason

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

maintainer
#147 lore raw

Re: [RFC 10/20] iommu/iommufd: Add IOMMU_DEVICE_GET_INFO

Quoted 15 lines · Kevin
Quoted 15 lines

From: Jason Gunthorpe <[email protected]>
Sent: Thursday, September 23, 2021 7:27 PM

Quoted 3 lines · Jean-Philippe Brucker

So we can only tell userspace "No_snoop is not supported" (provided we even want to allow them to enable No_snoop). Users in control of stage-1 tables can create non-cacheable mappings through MAIR attributes.

My point is that ARM is using IOMMU_CACHE to control the overall cachability of the DMA

ie not specifying IOMMU_CACHE requires using the arch specific DMA cache flushers.

Quoted 8 lines
Quoted 4 lines

Intel never uses arch specifc DMA cache flushers, and instead is abusing IOMMU_CACHE to mean IOMMU_BLOCK_NO_SNOOP on DMA that is always
cachable.

it uses IOMMU_CACHE to force all DMAs to snoop, including those which has non_snoop flag and wouldn't snoop cache if iommu is disabled. Nothing is blocked.

I see it differently, on Intel the only way to bypass the cache with DMA is to specify the no-snoop bit in the TLP. The IOMMU PTE flag we are talking about tells the IOMMU to ignore the no snoop bit.

Again, Intel arch in the kernel does not support the DMA cache flush arch API and *DOES NOT* support incoherent DMA at all.

ARM *does* implement the DMA cache flush arch API and is using IOMMU_CACHE to control if the caller will, or will not call the cache flushes.

This is fundamentally different from what Intel is using it for.

Quoted 2 lines

but why do you call it abuse? IOMMU_CACHE was first introduced for Intel platform:

IMHO ARM changed the meaning when Robin linked IOMMU_CACHE to dma_is_coherent stuff. At that point it became linked to 'do I need to call arch cache flushers or not'.

Quoted 7 lines
Quoted 3 lines

These are different things and need different bits. Since the ARM path has a lot more code supporting it, I'd suggest Intel should change their code to use IOMMU_BLOCK_NO_SNOOP and abandon IOMMU_CACHE.

I didn't fully get this point. The end result is same, i.e. making the DMA cache-coherent when IOMMU_CACHE is set. Or if you help define the behavior of IOMMU_CACHE, what will you define now?

It is clearly specifying how the kernel API works:

!IOMMU_CACHE
must call arch cache flushers
IOMMU_CACHE -
do not call arch cache flushers
IOMMU_CACHE|IOMMU_BLOCK_NO_SNOOP -
dot not arch cache flushers, and ignore the no snoop bit.

On Intel it should refuse to create a !IOMMU_CACHE since the HW can't do that. All IOMMU formats can support IOMMU_CACHE. Only the special no-snoop IOPTE format can support the final one, and it is only useful for iommufd/vfio users that are interacting with VMs and wbvind.

Jason

maintainer
#148 lore raw

RE: [RFC 10/20] iommu/iommufd: Add IOMMU_DEVICE_GET_INFO

+Robin.

Quoted 36 lines

From: Jason Gunthorpe <[email protected]>
Sent: Thursday, September 23, 2021 8:22 PM

Quoted 6 lines · Kevin
Quoted 6 lines

From: Jason Gunthorpe <[email protected]>
Sent: Thursday, September 23, 2021 7:27 PM

Quoted 1 line · Jean-Philippe Brucker

So we can only tell userspace "No_snoop is not supported" (provided

we

Quoted 1 line
Quoted 1 line
Quoted 1 line

even want to allow them to enable No_snoop). Users in control of

stage-1

Quoted 10 lines
Quoted 10 lines
Quoted 1 line

tables can create non-cacheable mappings through MAIR attributes.

My point is that ARM is using IOMMU_CACHE to control the overall cachability of the DMA

ie not specifying IOMMU_CACHE requires using the arch specific DMA cache flushers.

Intel never uses arch specifc DMA cache flushers, and instead is abusing IOMMU_CACHE to mean IOMMU_BLOCK_NO_SNOOP on DMA

that

Quoted 5 lines
Quoted 2 lines

is always
cachable.

it uses IOMMU_CACHE to force all DMAs to snoop, including those which has non_snoop flag and wouldn't snoop cache if iommu is disabled.

Nothing

Quoted 1 line

is blocked.

I see it differently, on Intel the only way to bypass the cache with DMA is to specify the no-snoop bit in the TLP. The IOMMU PTE flag we are talking about tells the IOMMU to ignore the no snoop bit.

Quoted 6 lines

Again, Intel arch in the kernel does not support the DMA cache flush arch API and *DOES NOT* support incoherent DMA at all.

ARM *does* implement the DMA cache flush arch API and is using IOMMU_CACHE to control if the caller will, or will not call the cache flushes.

I still didn't fully understand this point after reading the code. Looking at dma-iommu its cache flush functions are all coded with below as the first check:

        if (dev_is_dma_coherent(dev) && !dev_is_untrusted(dev))
                return;

dev->dma_coherent is initialized upon firmware info, not decided by IOMMU_CACHE.

i.e. it's not IOMMU_CACHE to decide whether cache flushes should be called.

Probably the confusion comes from __iommu_dma_alloc_noncontiguous:

        if (!(ioprot & IOMMU_CACHE)) {
                struct scatterlist *sg;
                int i;

                for_each_sg(sgt->sgl, sg, sgt->orig_nents, i)
                        arch_dma_prep_coherent(sg_page(sg), sg->length);
        }

Here it makes more sense to be if (!coherent) {}.

with above being corrected, I think all iommu drivers do associate IOMMU_CACHE to the snoop aspect:

Intel:
- either force snooping by ignoring snoop bit in TLP (IOMMU_CACHE)
- or has snoop decided by TLP (!IOMMU_CACHE)

ARM:
- set to snoop format if IOMMU_CACHE
- set to nonsnoop format if !IOMMU_CACHE
(in both cases TLP snoop bit is ignored?)

Other archs
- ignore IOMMU_CACHE as cache is always snooped via their IOMMUs

Quoted 10 lines

This is fundamentally different from what Intel is using it for.

Quoted 2 lines

but why do you call it abuse? IOMMU_CACHE was first introduced for Intel platform:

IMHO ARM changed the meaning when Robin linked IOMMU_CACHE to dma_is_coherent stuff. At that point it became linked to 'do I need to call arch cache flushers or not'.

I didn't identify the exact commit for above meaning change.

Robin, could you help share some thoughts here?

Thanks
Kevin

maintainer
#149 lore raw

Re: [RFC 10/20] iommu/iommufd: Add IOMMU_DEVICE_GET_INFO

Quoted 4 lines · Kevin

ARM:
- set to snoop format if IOMMU_CACHE
- set to nonsnoop format if !IOMMU_CACHE
(in both cases TLP snoop bit is ignored?)

Where do you see this? I couldn't even find this functionality in the ARM HW manual??

What I saw is ARM linking the IOMMU_CACHE to a IO PTE bit that causes the cache coherence to be disabled, which is not ignoring no snoop.

Quoted 3 lines

I didn't identify the exact commit for above meaning change.

Robin, could you help share some thoughts here?

It is this:

static int dma_info_to_prot(enum dma_data_direction dir, bool coherent,
unsigned long attrs)

{
	int prot = coherent ? IOMMU_CACHE : 0;

Which sets IOMMU_CACHE based on:

static void *iommu_dma_alloc(struct device *dev, size_t size,
dma_addr_t *handle, gfp_t gfp, unsigned long attrs)

{
	bool coherent = dev_is_dma_coherent(dev);
	int ioprot = dma_info_to_prot(DMA_BIDIRECTIONAL, coherent, attrs); 

Driving IOMMU_CACHE from dev_is_dma_coherent() has *NOTHING* to do with no-snoop TLPs and everything to do with the arch cache maintenance API

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

maintainer
#150 lore raw

RE: [RFC 10/20] iommu/iommufd: Add IOMMU_DEVICE_GET_INFO

Quoted 5 lines

From: Jason Gunthorpe
Sent: Wednesday, September 29, 2021 8:37 PM

On Wed, Sep 29, 2021 at 08:48:28AM +0000, Tian, Kevin wrote:

Quoted 7 lines
Quoted 4 lines

ARM:
- set to snoop format if IOMMU_CACHE
- set to nonsnoop format if !IOMMU_CACHE
(in both cases TLP snoop bit is ignored?)

Where do you see this? I couldn't even find this functionality in the ARM HW manual??

Honestly speaking I'm getting confused by the complex attribute transformation control (default, replace, combine, input, output, etc.) in SMMU manual. Above was my impression after last check, but now I cannot find necessary info to build the same picture (except below code). :/

Quoted 3 lines

What I saw is ARM linking the IOMMU_CACHE to a IO PTE bit that causes the cache coherence to be disabled, which is not ignoring no snoop.

My impression was that snoop is one way of implementing cache coherency and now since the PTE can explicitly specify cache coherency like below:

                else if (prot & IOMMU_CACHE)
                        pte |= ARM_LPAE_PTE_MEMATTR_OIWB;
                else
                        pte |= ARM_LPAE_PTE_MEMATTR_NC;

This setting in concept overrides the snoop attribute from the device thus make it sort of ignored?

But I did see the manual says that:
--
Note: To achieve this 'pull-down' behavior, the No_snoop flag might be carried through the SMMU and used to transform the SMMU output downstream.
--

So again, just got confused here...

Quoted 16 lines
Quoted 4 lines

I didn't identify the exact commit for above meaning change.

Robin, could you help share some thoughts here?

It is this:

static int dma_info_to_prot(enum dma_data_direction dir, bool coherent,
unsigned long attrs)

{
	int prot = coherent ? IOMMU_CACHE : 0;

Which sets IOMMU_CACHE based on:

static void *iommu_dma_alloc(struct device *dev, size_t size,
dma_addr_t *handle, gfp_t gfp, unsigned long attrs)

Quoted 7 lines
{
	bool coherent = dev_is_dma_coherent(dev);
	int ioprot = dma_info_to_prot(DMA_BIDIRECTIONAL, coherent, attrs);

Driving IOMMU_CACHE from dev_is_dma_coherent() has *NOTHING* to do with no-snoop TLPs and everything to do with the arch cache maintenance API

Maybe I'll get a clearer picture on this after understanding the difference between cache coherency and snoop on ARM. They are sort of inter- changeable on Intel (or possibly on x86 since I just found that AMD completely ignores IOMMU_CACHE).

Thanks
Kevin

#151 lore raw

Re: [RFC 10/20] iommu/iommufd: Add IOMMU_DEVICE_GET_INFO

Quoted 27 lines · Kevin
Quoted 5 lines

From: Jason Gunthorpe
Sent: Wednesday, September 29, 2021 8:37 PM

On Wed, Sep 29, 2021 at 08:48:28AM +0000, Tian, Kevin wrote:

Quoted 7 lines
Quoted 4 lines

ARM:
- set to snoop format if IOMMU_CACHE
- set to nonsnoop format if !IOMMU_CACHE
(in both cases TLP snoop bit is ignored?)

Where do you see this? I couldn't even find this functionality in the ARM HW manual??

Honestly speaking I'm getting confused by the complex attribute transformation control (default, replace, combine, input, output, etc.) in SMMU manual. Above was my impression after last check, but now I cannot find necessary info to build the same picture (except below code). :/

Quoted 3 lines

What I saw is ARM linking the IOMMU_CACHE to a IO PTE bit that causes the cache coherence to be disabled, which is not ignoring no snoop.

My impression was that snoop is one way of implementing cache coherency and now since the PTE can explicitly specify cache coherency like below:

Quoted 7 lines
                else if (prot & IOMMU_CACHE)
                        pte |= ARM_LPAE_PTE_MEMATTR_OIWB;
                else
                        pte |= ARM_LPAE_PTE_MEMATTR_NC;

This setting in concept overrides the snoop attribute from the device thus make it sort of ignored?

To make sure we're talking about the same thing: "the snoop attribute from the device" is the "No snoop" attribute in the PCI TLP, right?

The PTE flags define whether the memory access is cache-coherent or not.
* WB is cacheable (short for write-back cacheable. Doesn't matter here what OI or RWA mean.)
* NC is non-cacheable.

| Normal PCI access | No_snoop PCI access
-------+-------------------+-------------------

  PTE WB | Cacheable         | Non-cacheable
  PTE NC | Non-cacheable     | Non-cacheable

Cacheable memory access participate in cache coherency. Non-cacheable accesses go directly to memory, do not cause cache allocation.

On Arm cache coherency is configured through PTE attributes. I don't think PCI No_snoop should be used because it's not necessarily supported throughout the system and, as far as I understand, software can't discover whether it is.

[...]

Quoted 2 lines

Maybe I'll get a clearer picture on this after understanding the difference between cache coherency and snoop on ARM.

The architecture uses terms "cacheable" and "coherent". The term "snoop" is used when referring specifically to the PCI "No snoop" attribute. It is also used within the interconnect coherency protocols, which are invisible to software.

Thanks,
Jean

maintainer
#152 lore raw

Re: [RFC 10/20] iommu/iommufd: Add IOMMU_DEVICE_GET_INFO

Quoted 44 lines · Jean-Philippe Brucker
Quoted 27 lines · Kevin
Quoted 5 lines

From: Jason Gunthorpe
Sent: Wednesday, September 29, 2021 8:37 PM

On Wed, Sep 29, 2021 at 08:48:28AM +0000, Tian, Kevin wrote:

Quoted 7 lines
Quoted 4 lines

ARM:
- set to snoop format if IOMMU_CACHE
- set to nonsnoop format if !IOMMU_CACHE
(in both cases TLP snoop bit is ignored?)

Where do you see this? I couldn't even find this functionality in the ARM HW manual??

Honestly speaking I'm getting confused by the complex attribute transformation control (default, replace, combine, input, output, etc.) in SMMU manual. Above was my impression after last check, but now I cannot find necessary info to build the same picture (except below code). :/

Quoted 3 lines

What I saw is ARM linking the IOMMU_CACHE to a IO PTE bit that causes the cache coherence to be disabled, which is not ignoring no snoop.

My impression was that snoop is one way of implementing cache coherency and now since the PTE can explicitly specify cache coherency like below:

Quoted 7 lines
                else if (prot & IOMMU_CACHE)
                        pte |= ARM_LPAE_PTE_MEMATTR_OIWB;
                else
                        pte |= ARM_LPAE_PTE_MEMATTR_NC;

This setting in concept overrides the snoop attribute from the device thus make it sort of ignored?

To make sure we're talking about the same thing: "the snoop attribute from the device" is the "No snoop" attribute in the PCI TLP, right?

The PTE flags define whether the memory access is cache-coherent or not.
* WB is cacheable (short for write-back cacheable. Doesn't matter here what OI or RWA mean.)
* NC is non-cacheable.

Quoted 6 lines
         | Normal PCI access | No_snoop PCI access
  PTE WB | Cacheable         | Non-cacheable
  PTE NC | Non-cacheable     | Non-cacheable

Cacheable memory access participate in cache coherency. Non-cacheable accesses go directly to memory, do not cause cache allocation.

This table is what I was thinking after reading through the ARM docs.

Quoted 4 lines

On Arm cache coherency is configured through PTE attributes. I don't think PCI No_snoop should be used because it's not necessarily supported throughout the system and, as far as I understand, software can't discover whether it is.

The usage of no-snoop is a behavior of a device. A generic PCI driver should be able to program the device to generate no-snoop TLPs and ideally rely on an arch specific API in the OS to trigger the required cache maintenance.

It doesn't make much sense for a portable driver to rely on a non-portable IO PTE flag to control coherency, since that is not a standards based approach.

That said, Linux doesn't have a generic DMA API to support no-snoop. The few GPUs drivers that use this stuff just hardwired wbsync on Intel..

What I don't really understand is why ARM, with an IOMMU that supports PTE WB, has devices where dev_is_dma_coherent() == false ?

Is it the case that DMA from those devices ignores the IO PTE's cachable mode?

Jason

#153 lore raw

Re: [RFC 10/20] iommu/iommufd: Add IOMMU_DEVICE_GET_INFO

Quoted 9 lines · Jason Gunthorpe
Quoted 4 lines

On Arm cache coherency is configured through PTE attributes. I don't think PCI No_snoop should be used because it's not necessarily supported throughout the system and, as far as I understand, software can't discover whether it is.

The usage of no-snoop is a behavior of a device. A generic PCI driver should be able to program the device to generate no-snoop TLPs and ideally rely on an arch specific API in the OS to trigger the required cache maintenance.

Well, it is a combination of the device, the root port and the driver which all need to be in line to use this.

Quoted 7 lines

It doesn't make much sense for a portable driver to rely on a non-portable IO PTE flag to control coherency, since that is not a standards based approach.

That said, Linux doesn't have a generic DMA API to support no-snoop. The few GPUs drivers that use this stuff just hardwired wbsync on Intel..

Yes, as usual the GPU folks come up with nasty hacks instead of providing generic helper. Basically all we'd need to support it in a generic way is:

- a DMA_ATTR_NO_SNOOP (or DMA_ATTR_FORCE_NONCOHERENT to fit the Linux terminology) which treats the current dma_map/unmap/sync calls as if dev_is_dma_coherent was false
- a way for the driver to discover that a given architecture / running system actually supports this

Quoted 2 lines

What I don't really understand is why ARM, with an IOMMU that supports PTE WB, has devices where dev_is_dma_coherent() == false ?

Because no IOMMU in the world can help that fact that a periphal on the SOC is not part of the cache coherency protocol.

maintainer
#154 lore raw

RE: [RFC 10/20] iommu/iommufd: Add IOMMU_DEVICE_GET_INFO

Quoted 31 lines

From: [email protected] <[email protected]>
Sent: Friday, October 1, 2021 11:28 AM

Quoted 1 line · Jason Gunthorpe
Quoted 1 line

On Arm cache coherency is configured through PTE attributes. I don't

think

Quoted 2 lines
Quoted 2 lines

PCI No_snoop should be used because it's not necessarily supported throughout the system and, as far as I understand, software can't

discover

Quoted 6 lines
Quoted 1 line

whether it is.

The usage of no-snoop is a behavior of a device. A generic PCI driver should be able to program the device to generate no-snoop TLPs and ideally rely on an arch specific API in the OS to trigger the required cache maintenance.

Well, it is a combination of the device, the root port and the driver which all need to be in line to use this.

Quoted 7 lines

It doesn't make much sense for a portable driver to rely on a non-portable IO PTE flag to control coherency, since that is not a standards based approach.

That said, Linux doesn't have a generic DMA API to support no-snoop. The few GPUs drivers that use this stuff just hardwired wbsync on Intel..

Yes, as usual the GPU folks come up with nasty hacks instead of providing generic helper. Basically all we'd need to support it in a generic way is:

Quoted 6 lines

- a DMA_ATTR_NO_SNOOP (or DMA_ATTR_FORCE_NONCOHERENT to fit the
Linux
terminology) which treats the current dma_map/unmap/sync calls as if dev_is_dma_coherent was false
- a way for the driver to discover that a given architecture / running system actually supports this

Based on above information my interpretation is that existing DMA API manages coherency per device and It's not designed for devices which are coherent in nature but also set PCI no-snoop for selective traffic. Then the new DMA_ATTR_NO_SNOOP, once set in dma_map, allows the driver to follow non-coherent semantics even when the device itself is considered coherent.

Does it capture the whole story correct?

Quoted 6 lines
Quoted 3 lines

What I don't really understand is why ARM, with an IOMMU that supports PTE WB, has devices where dev_is_dma_coherent() == false ?

Because no IOMMU in the world can help that fact that a periphal on the SOC is not part of the cache coherency protocol.

but since DMA goes through IOMMU then isn't IOMMU the one who should decide the final cache coherency? What would be the case if the IOMMU sets WB while the peripheral doesn't want it?

Thanks
Kevin

#155 lore raw

Re: [RFC 10/20] iommu/iommufd: Add IOMMU_DEVICE_GET_INFO

Quoted 8 lines · Kevin

Based on above information my interpretation is that existing DMA API manages coherency per device and It's not designed for devices which are coherent in nature but also set PCI no-snoop for selective traffic. Then the new DMA_ATTR_NO_SNOOP, once set in dma_map, allows the driver to follow non-coherent semantics even when the device itself is considered coherent.

Does it capture the whole story correct?

Yes.

Quoted 9 lines
Quoted 5 lines
Quoted 2 lines

What I don't really understand is why ARM, with an IOMMU that supports PTE WB, has devices where dev_is_dma_coherent() == false ?

Because no IOMMU in the world can help that fact that a periphal on the SOC is not part of the cache coherency protocol.

but since DMA goes through IOMMU then isn't IOMMU the one who should decide the final cache coherency? What would be the case if the IOMMU sets WB while the peripheral doesn't want it?

No. And IOMMU deal with address translation, it can't paper over a fact that there is no coherency possible.

maintainer
#156 lore raw

RE: [RFC 10/20] iommu/iommufd: Add IOMMU_DEVICE_GET_INFO

Quoted 10 lines

From: [email protected] <[email protected]>
Sent: Thursday, October 14, 2021 4:22 PM

Quoted 1 line
Quoted 1 line
Quoted 1 line

What I don't really understand is why ARM, with an IOMMU that

supports

Quoted 5 lines
Quoted 4 lines
Quoted 1 line

PTE WB, has devices where dev_is_dma_coherent() == false ?

Because no IOMMU in the world can help that fact that a periphal on the SOC is not part of the cache coherency protocol.

Quoted 6 lines
Quoted 3 lines

but since DMA goes through IOMMU then isn't IOMMU the one who should decide the final cache coherency? What would be the case if the IOMMU sets WB while the peripheral doesn't want it?

No. And IOMMU deal with address translation, it can't paper over a fact that there is no coherency possible.

Does it relate to the ATS story where the device gets translated address from IOMMU and then directly sends request to memory controller? In this way if the device is not in cache coherency domain then nothing can change it.

Then if ATS is disabled, suppose the untranslated request from the device is translated and forwarded by IOMMU to the memory controller. In this case IOMMU should be able to join the coherency protocol even when the originating device itself cannot.

Is above understanding correct?

Thanks
Kevin

maintainer
#157 lore raw

RE: [RFC 10/20] iommu/iommufd: Add IOMMU_DEVICE_GET_INFO

In reply to Jean-Philippe Brucker
Quoted 8 lines

From: Jean-Philippe Brucker <[email protected]>
Sent: Thursday, September 30, 2021 6:33 PM

The PTE flags define whether the memory access is cache-coherent or not.
* WB is cacheable (short for write-back cacheable. Doesn't matter here what OI or RWA mean.)
* NC is non-cacheable.

Quoted 4 lines

| Normal PCI access | No_snoop PCI access
-------+-------------------+-------------------

  PTE WB | Cacheable         | Non-cacheable
  PTE NC | Non-cacheable     | Non-cacheable

This implies that PCI no-snoop supersedes PTE flags when it's supported by the system?

#158 lore raw

Re: [RFC 10/20] iommu/iommufd: Add IOMMU_DEVICE_GET_INFO

Quoted 8 lines · Kevin
Quoted 8 lines

From: Jean-Philippe Brucker <[email protected]>
Sent: Thursday, September 30, 2021 6:33 PM

The PTE flags define whether the memory access is cache-coherent or not.
* WB is cacheable (short for write-back cacheable. Doesn't matter here what OI or RWA mean.)
* NC is non-cacheable.

Quoted 8 lines
Quoted 4 lines

| Normal PCI access | No_snoop PCI access
-------+-------------------+-------------------

  PTE WB | Cacheable         | Non-cacheable
  PTE NC | Non-cacheable     | Non-cacheable

This implies that PCI no-snoop supersedes PTE flags when it's supported by the system?

Yes, no way for the SMMU to ignore no-snoop, as far as I can see

Thanks,
Jean

maintainer
#159 lore raw

RE: [RFC 10/20] iommu/iommufd: Add IOMMU_DEVICE_GET_INFO

In reply to Jason Gunthorpe
Quoted 12 lines

From: Jason Gunthorpe <[email protected]>
Sent: Thursday, September 23, 2021 8:22 PM

Quoted 3 lines
Quoted 3 lines

These are different things and need different bits. Since the ARM path has a lot more code supporting it, I'd suggest Intel should change their code to use IOMMU_BLOCK_NO_SNOOP and abandon

IOMMU_CACHE.

Quoted 4 lines

I didn't fully get this point. The end result is same, i.e. making the DMA cache-coherent when IOMMU_CACHE is set. Or if you help define the behavior of IOMMU_CACHE, what will you define now?

Quoted 8 lines

It is clearly specifying how the kernel API works:

!IOMMU_CACHE
must call arch cache flushers
IOMMU_CACHE -
do not call arch cache flushers
IOMMU_CACHE|IOMMU_BLOCK_NO_SNOOP -
dot not arch cache flushers, and ignore the no snoop bit.

Who will set IOMMU_BLOCK_NO_SNOOP? I feel this is arch specific knowledge about how cache coherency is implemented, i.e. when IOMMU_CACHE is set intel-iommu driver just maps it to blocking no-snoop. It's not necessarily to be an attribute in the same level as IOMMU_CACHE?

Quoted 3 lines

On Intel it should refuse to create a !IOMMU_CACHE since the HW can't do that.

Agree. In reality I guess this is not hit because all devices are marked coherent on Intel platforms...

Baolu, any insight here?

Thanks
Kevin

maintainer
#160 lore raw

Re: [RFC 10/20] iommu/iommufd: Add IOMMU_DEVICE_GET_INFO

Quoted 28 lines · Kevin
Quoted 12 lines

From: Jason Gunthorpe <[email protected]>
Sent: Thursday, September 23, 2021 8:22 PM

Quoted 3 lines
Quoted 3 lines

These are different things and need different bits. Since the ARM path has a lot more code supporting it, I'd suggest Intel should change their code to use IOMMU_BLOCK_NO_SNOOP and abandon

IOMMU_CACHE.

Quoted 4 lines

I didn't fully get this point. The end result is same, i.e. making the DMA cache-coherent when IOMMU_CACHE is set. Or if you help define the behavior of IOMMU_CACHE, what will you define now?

Quoted 8 lines

It is clearly specifying how the kernel API works:

!IOMMU_CACHE
must call arch cache flushers
IOMMU_CACHE -
do not call arch cache flushers
IOMMU_CACHE|IOMMU_BLOCK_NO_SNOOP -
dot not arch cache flushers, and ignore the no snoop bit.

Who will set IOMMU_BLOCK_NO_SNOOP? I feel this is arch specific knowledge about how cache coherency is implemented, i.e. when IOMMU_CACHE is set intel-iommu driver just maps it to blocking no-snoop. It's not necessarily to be an attribute in the same level as IOMMU_CACHE?

Quoted 1 line
Quoted 7 lines
Quoted 2 lines

On Intel it should refuse to create a !IOMMU_CACHE since the HW can't do that.

Agree. In reality I guess this is not hit because all devices are marked coherent on Intel platforms...

Baolu, any insight here?

I am trying to follow the discussion here. Please guide me if I didn't get the right context.

Here, we are discussing arch_sync_dma_for_cpu() and arch_sync_dma_for_device(). The x86 arch has clflush to sync dma buffer for device, but I can't see any instruction to sync dma buffer for cpu if the device is not cache coherent. Is that the reason why x86 can't have an implementation for arch_sync_dma_for_cpu(), hence all devices are marked coherent?

Quoted 3 lines

Thanks
Kevin

Best regards,
baolu

#161 lore raw

Re: [RFC 10/20] iommu/iommufd: Add IOMMU_DEVICE_GET_INFO

Quoted 6 lines · Lu Baolu

Here, we are discussing arch_sync_dma_for_cpu() and arch_sync_dma_for_device(). The x86 arch has clflush to sync dma buffer for device, but I can't see any instruction to sync dma buffer for cpu if the device is not cache coherent. Is that the reason why x86 can't have an implementation for arch_sync_dma_for_cpu(), hence all devices are marked coherent?

arch_sync_dma_for_cpu and arch_sync_dma_for_device are only used if the device is marked non-coherent, that is if Linux knows the device can't be part of the cache coherency protocol. There are no known x86 systems with entirely not cache coherent devices so these helpers won't be useful as-is.

maintainer
#162 lore raw

Re: [RFC 10/20] iommu/iommufd: Add IOMMU_DEVICE_GET_INFO

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

From: Jason Gunthorpe <[email protected]>
Sent: Thursday, September 23, 2021 8:22 PM

Quoted 3 lines
Quoted 3 lines

These are different things and need different bits. Since the ARM path has a lot more code supporting it, I'd suggest Intel should change their code to use IOMMU_BLOCK_NO_SNOOP and abandon

IOMMU_CACHE.

Quoted 4 lines

I didn't fully get this point. The end result is same, i.e. making the DMA cache-coherent when IOMMU_CACHE is set. Or if you help define the behavior of IOMMU_CACHE, what will you define now?

It is clearly specifying how the kernel API works:

Quoted 8 lines
Quoted 6 lines

!IOMMU_CACHE
must call arch cache flushers
IOMMU_CACHE -
do not call arch cache flushers
IOMMU_CACHE|IOMMU_BLOCK_NO_SNOOP -
dot not arch cache flushers, and ignore the no snoop bit.

Who will set IOMMU_BLOCK_NO_SNOOP?

Basically only qemu due to specialized x86 hypervisor knowledge.

The only purpose of this attribute is to support a specific virtualization use case where a whole bunch of stuff is broken together:
- the cache maintenance instructions are not available to a guest
- the guest isn't aware that the instructions don't work and tells the device to issue no-snoop TLPs
- The device ignores the 'disable no-snoop' flag in the PCIe config space

Thus things become broken.

Jason

maintainer
#163 lore raw

Re: [RFC 10/20] iommu/iommufd: Add IOMMU_DEVICE_GET_INFO

In reply to Tian, Kevin
Quoted 3 lines · Kevin

Disabling wbinvd is one purpose. imo the more important intention is that iommu vendor uses different PTE formats between snoop and !snoop.

The PTE format for userspace is communicated through the format input, not through random flags. If Intel has two different PTE formats then userspace must negotiate which to use via the format input.

If the kernel controls the PTE then the format doesn't matter and the kernel should configure things to match the requested behavior

Quoted 5 lines

When creating an ioas there could be three snoop modes:

1) snoop for all attached devices;
2) non-snoop for all attached devices;
3) device-selected snoop;

I'd express the three cases like this:

0
ARM can avoid cache shooping, must use arch cache flush helpers
IOMMU_CACHE
Normal DMAs get cache coherence, do not need arch cache flush helpers
IOMMU_CACHE | IOMMU_BLOCK_NO_SNOOP
All DMAs get cache coherence, not supported on ARM

Jason

A message that is not in the archive (sent off-list or not yet received): BN9PR11MB5433409DF766AAEF1BB2CF258CA39@BN9PR11MB5433.namprd11.prod.outlook.com

maintainer
#165 lore raw

RE: [RFC 10/20] iommu/iommufd: Add IOMMU_DEVICE_GET_INFO

Quoted 11 lines

From: Tian, Kevin
Sent: Thursday, September 23, 2021 11:11 AM

Quoted 7 lines

The required behavior for iommufd is to have the IOMMU ignore the no-snoop bit so that Intel HW can disable wbinvd. This bit should be clearly documented for its exact purpose and if other arches also have instructions that need to be disabled if snoop TLPs are allowed then they can re-use this bit. It appears ARM does not have this issue and does not need the bit.

Quoted 6 lines

Disabling wbinvd is one purpose. imo the more important intention is that iommu vendor uses different PTE formats between snoop and !snoop. As long as we want allow userspace to opt in case of isoch performance requirement (unlike current vfio which always choose snoop format if available), such mechanism is required for all vendors.

btw I'm not sure whether the wbinvd trick is Intel specific. All other platforms (amd, arm, s390, etc.) currently always claim OMMU_CAP_ CACHE_COHERENCY (the source of IOMMU_CACHE). They didn't hit this problem because vfio always sets IOMMU_CACHE to force every DMA to snoop. Will they need to handle similar wbinvd-like trick (plus necessary memory type virtualization) when non-snoop format is enabled? Or are their architectures highly optimized to afford isoch traffic even with snoop (then fine to not support user opt-in)?

Thanks
Kevin

maintainer
#166 lore raw

Re: [RFC 10/20] iommu/iommufd: Add IOMMU_DEVICE_GET_INFO

Quoted 18 lines · Kevin
Quoted 11 lines

From: Tian, Kevin
Sent: Thursday, September 23, 2021 11:11 AM

Quoted 7 lines

The required behavior for iommufd is to have the IOMMU ignore the no-snoop bit so that Intel HW can disable wbinvd. This bit should be clearly documented for its exact purpose and if other arches also have instructions that need to be disabled if snoop TLPs are allowed then they can re-use this bit. It appears ARM does not have this issue and does not need the bit.

Quoted 6 lines

Disabling wbinvd is one purpose. imo the more important intention is that iommu vendor uses different PTE formats between snoop and !snoop. As long as we want allow userspace to opt in case of isoch performance requirement (unlike current vfio which always choose snoop format if available), such mechanism is required for all vendors.

Quoted 3 lines

btw I'm not sure whether the wbinvd trick is Intel specific. All other platforms (amd, arm, s390, etc.) currently always claim OMMU_CAP_ CACHE_COHERENCY (the source of IOMMU_CACHE).

This only means they don't need to use the arch cache flush helpers. It has nothing to do with no-snoop on those platforms.

Quoted 6 lines

They didn't hit this problem because vfio always sets IOMMU_CACHE to force every DMA to snoop. Will they need to handle similar wbinvd-like trick (plus necessary memory type virtualization) when non-snoop format is enabled? Or are their architectures highly optimized to afford isoch traffic even with snoop (then fine to not support user opt-in)?

In other arches the question is:
- Do they allow non-coherent DMA to exist in a VM?
- Can the VM issue cache maintaince ops to fix the decoherence?

The Intel functional issue is that Intel blocks the cache maintaince ops from the VM and the VM has no way to self-discover that the cache maintaince ops don't work.

Other arches don't seem to have this specific problem...

The other warped part of this is that Linux doesn't actually support no-snoop DMA through the DMA API. The users in Intel GPU drivers are all hacking it up, so it may well be that on other arches Linux never ask devices to issue no-snoop DMA because there is no portable way for the driver to restore coherence on a DMA by DMA basis..

Jason

maintainer
#167 lore raw

RE: [RFC 10/20] iommu/iommufd: Add IOMMU_DEVICE_GET_INFO

Quoted 36 lines

From: Jason Gunthorpe <[email protected]>
Sent: Thursday, September 23, 2021 7:42 PM

Quoted 18 lines · Kevin
Quoted 11 lines

From: Tian, Kevin
Sent: Thursday, September 23, 2021 11:11 AM

Quoted 7 lines

The required behavior for iommufd is to have the IOMMU ignore the no-snoop bit so that Intel HW can disable wbinvd. This bit should be clearly documented for its exact purpose and if other arches also have instructions that need to be disabled if snoop TLPs are allowed then they can re-use this bit. It appears ARM does not have this issue and does not need the bit.

Quoted 6 lines

Disabling wbinvd is one purpose. imo the more important intention is that iommu vendor uses different PTE formats between snoop and !snoop. As long as we want allow userspace to opt in case of isoch performance requirement (unlike current vfio which always choose snoop format if available), such mechanism is required for all vendors.

Quoted 3 lines

btw I'm not sure whether the wbinvd trick is Intel specific. All other platforms (amd, arm, s390, etc.) currently always claim OMMU_CAP_ CACHE_COHERENCY (the source of IOMMU_CACHE).

This only means they don't need to use the arch cache flush helpers. It has nothing to do with no-snoop on those platforms.

Quoted 6 lines

They didn't hit this problem because vfio always sets IOMMU_CACHE to force every DMA to snoop. Will they need to handle similar wbinvd-like trick (plus necessary memory type virtualization) when non-snoop format is enabled? Or are their architectures highly optimized to afford isoch traffic even with snoop (then fine to not support user opt-in)?

Quoted 2 lines

In other arches the question is:
- Do they allow non-coherent DMA to exist in a VM?

And is coherency a static attribute per device or could be opted by driver on such arch? If the latter, then the same opt path from userspace sounds also reasonable, since driver is in userspace now.

Quoted 1 line

- Can the VM issue cache maintaince ops to fix the decoherence?

As you listed the questions are all about non-coherent DMA, not how non-coherent DMAs are implemented underlyingly. From this angle focusing on coherent part as Alex suggested is more forward looking than tying the uAPI to a specific coherency implementation using snoop?

Quoted 4 lines

The Intel functional issue is that Intel blocks the cache maintaince ops from the VM and the VM has no way to self-discover that the cache maintaince ops don't work.

the VM doesn't need to know whether the maintenance ops actually works. It just treats the device as if those ops are always required. The hypervisor will figure out whether those ops should be blocked based on whether coherency is guaranteed by iommu based on iommufd/vfio.

Quoted 2 lines

Other arches don't seem to have this specific problem...

I think the key is whether other archs allow driver to decide DMA coherency and indirectly the underlying I/O page table format. If yes, then I don't see a reason why such decision should not be given to userspace for passthrough case.

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

maintainer
#168 lore raw

Re: [RFC 10/20] iommu/iommufd: Add IOMMU_DEVICE_GET_INFO

Quoted 6 lines · Kevin
Quoted 3 lines

The Intel functional issue is that Intel blocks the cache maintaince ops from the VM and the VM has no way to self-discover that the cache maintaince ops don't work.

the VM doesn't need to know whether the maintenance ops actually works.

Which is the whole problem.

Intel has a design where the device driver tells the device to issue non-cachable TLPs.

The driver is supposed to know if it can issue the cache maintaince instructions - if it can then it should ask the device to issue no-snoop TLPs.

For instance the same PCI driver on non-x86 should never ask the device to issue no-snoop TLPs because it has no idea how to restore cache coherence on eg ARM.

Do you see the issue? This configuration where the hypervisor silently make wbsync a NOP breaks the x86 architecture because the guest has no idea it can no longer use no-snoop features.

Using the IOMMU to forcibly prevent the device from issuing no-snoop makes this whole issue of the broken wbsync moot.

It is important to be really clear on what this is about - this is not some idealized nice iommu feature - it is working around alot of backwards compatability baggage that is probably completely unique to x86.

Quoted 6 lines
Quoted 1 line

Other arches don't seem to have this specific problem...

I think the key is whether other archs allow driver to decide DMA coherency and indirectly the underlying I/O page table format. If yes, then I don't see a reason why such decision should not be given to userspace for passthrough case.

The choice all comes down to if the other arches have cache maintenance instructions in the VM that *don't work*

Jason

#169 lore raw

Re: [RFC 10/20] iommu/iommufd: Add IOMMU_DEVICE_GET_INFO

Quoted 7 lines · Jason Gunthorpe
Quoted 6 lines
Quoted 3 lines

The Intel functional issue is that Intel blocks the cache maintaince ops from the VM and the VM has no way to self-discover that the cache maintaince ops don't work.

the VM doesn't need to know whether the maintenance ops actually works.

Quoted 8 lines

Which is the whole problem.

Intel has a design where the device driver tells the device to issue non-cachable TLPs.

The driver is supposed to know if it can issue the cache maintaince instructions - if it can then it should ask the device to issue no-snoop TLPs.

The driver should never issue them. This whole idea that a driver can just magically poke the cache directly is just one of these horrible short cuts that seems to happen in GPU land all the time but nowhere else.

Quoted 6 lines
Quoted 3 lines

coherency and indirectly the underlying I/O page table format. If yes, then I don't see a reason why such decision should not be given to userspace for passthrough case.

The choice all comes down to if the other arches have cache maintenance instructions in the VM that *don't work*

Or have them at all.

maintainer
#170 lore raw

RE: [RFC 10/20] iommu/iommufd: Add IOMMU_DEVICE_GET_INFO

In reply to Jason Gunthorpe
Quoted 21 lines

From: Jason Gunthorpe <[email protected]>
Sent: Friday, October 1, 2021 6:24 AM

Quoted 6 lines · Kevin
Quoted 3 lines

The Intel functional issue is that Intel blocks the cache maintaince ops from the VM and the VM has no way to self-discover that the cache maintaince ops don't work.

the VM doesn't need to know whether the maintenance ops actually works.

Which is the whole problem.

Intel has a design where the device driver tells the device to issue non-cachable TLPs.

The driver is supposed to know if it can issue the cache maintaince instructions - if it can then it should ask the device to issue no-snoop TLPs.

Quoted 7 lines

For instance the same PCI driver on non-x86 should never ask the device to issue no-snoop TLPs because it has no idea how to restore cache coherence on eg ARM.

Do you see the issue? This configuration where the hypervisor silently make wbsync a NOP breaks the x86 architecture because the guest has no idea it can no longer use no-snoop features.

Thanks for explanation. But I still have one puzzle about the 'break' part.

If hypervisor makes wbinvd a NOP then it will also set enforce_snoop bit in PTE to convert non-snoop packet to snoop. No function in the guest is broken, just the performance may lag.

If performance matters then hypervisor configures IOMMU to allow non-snoop packet and then emulate wbinvd properly.

The contract between vfio and kvm is to convey above requirement on how wbinvd is handled.

But in both cases cache maintenance instructions are available from guest p.o.v and no coherency semantics would be violated.

Quoted 3 lines

Using the IOMMU to forcibly prevent the device from issuing no-snoop makes this whole issue of the broken wbsync moot.

it's not prevent issuing. Instead, IOMMU converts non-snoop request to snoop.

Quoted 8 lines

It is important to be really clear on what this is about - this is not some idealized nice iommu feature - it is working around alot of backwards compatability baggage that is probably completely unique to x86.

Quoted 2 lines
Quoted 1 line

Other arches don't seem to have this specific problem...

Quoted 8 lines
Quoted 4 lines

I think the key is whether other archs allow driver to decide DMA coherency and indirectly the underlying I/O page table format. If yes, then I don't see a reason why such decision should not be given to userspace for passthrough case.

The choice all comes down to if the other arches have cache maintenance instructions in the VM that *don't work*

Looks vfio always sets IOMMU_CACHE on all platforms as long as iommu supports it (true on all platforms except intel iommu which is dedicated for GPU):

vfio_iommu_type1_attach_group()

{
	...
	if (iommu_capable(bus, IOMMU_CAP_CACHE_COHERENCY))
		domain->prot |= IOMMU_CACHE;
	...
}

Should above be set according to whether a device is coherent?

Thanks
Kevin

maintainer
#171 lore raw

Re: [RFC 10/20] iommu/iommufd: Add IOMMU_DEVICE_GET_INFO

Quoted 2 lines · Kevin

But in both cases cache maintenance instructions are available from guest p.o.v and no coherency semantics would be violated.

You've described how Intel's solution papers over the problem.

In part wbinvd is defined to restore CPU cache coherence after a no-snoop DMA. Having wbinvd NOP breaks this contract.

To counter-act the broken wbinvd the IOMMU completely prevents the use of no-snoop DMA. It converts them to snoop instead.

The driver thinks it has no-snoop. The platform appears to support no-snoop. The driver issues wbinvd - but all of it does nothing.

Don't think any of this is even remotely related to what ARM is doing here. ARM has neither the broken VM cache ops, nor the IOMMU ability to suppress no-snoop.

Quoted 13 lines
Quoted 7 lines
Quoted 4 lines

I think the key is whether other archs allow driver to decide DMA coherency and indirectly the underlying I/O page table format. If yes, then I don't see a reason why such decision should not be given to userspace for passthrough case.

The choice all comes down to if the other arches have cache maintenance instructions in the VM that *don't work*

Looks vfio always sets IOMMU_CACHE on all platforms as long as iommu supports it (true on all platforms except intel iommu which is dedicated for GPU):

vfio_iommu_type1_attach_group()

Quoted 8 lines
{
	...
	if (iommu_capable(bus, IOMMU_CAP_CACHE_COHERENCY))
		domain->prot |= IOMMU_CACHE;
	...
}

Should above be set according to whether a device is coherent?

For IOMMU_CACHE there are two questions related to the overloaded meaning:

- Should VFIO ask the IOMMU to use non-coherent DMA (ARM meaning) This depends on how the VFIO user expects to operate the DMA. If the VFIO user can issue cache maintenance ops then IOMMU_CACHE should be controlled by the user. I have no idea what platforms support user space cache maintenance ops.

- Should VFIO ask the IOMMU to suppress no-snoop (Intel meaning) This depends if the VFIO user has access to wbinvd or not.

wbinvd is a privileged instruction so normally userspace will not be able to access it.

Per Paolo recommendation there should be a uAPI someplace that allows userspace to issue wbinvd - basically the suppress no-snoop is also user controllable.

The two things are very similar and ultimately are a choice userspace should be making.

From something like a qemu perspective things are more murkey - eg on ARM qemu needs to co-ordinate with the guest. Whatever IOMMU_CACHE mode VFIO is using must match the device coherent flag in the Linux guest. I'm guessing all Linux guest VMs only use coherent DMA for all devices today. I don't know if the cache maintaince ops are even permitted in an ARM VM.

Jason

maintainer
#172 lore raw

RE: [RFC 10/20] iommu/iommufd: Add IOMMU_DEVICE_GET_INFO

Quoted 25 lines

From: Jason Gunthorpe <[email protected]>
Sent: Thursday, October 14, 2021 11:43 PM

Quoted 13 lines
Quoted 7 lines
Quoted 4 lines

I think the key is whether other archs allow driver to decide DMA coherency and indirectly the underlying I/O page table format. If yes, then I don't see a reason why such decision should not be given to userspace for passthrough case.

The choice all comes down to if the other arches have cache maintenance instructions in the VM that *don't work*

Looks vfio always sets IOMMU_CACHE on all platforms as long as iommu supports it (true on all platforms except intel iommu which is dedicated for GPU):

vfio_iommu_type1_attach_group()

Quoted 8 lines
{
	...
	if (iommu_capable(bus, IOMMU_CAP_CACHE_COHERENCY))
		domain->prot |= IOMMU_CACHE;
	...
}

Should above be set according to whether a device is coherent?

Quoted 8 lines

For IOMMU_CACHE there are two questions related to the overloaded meaning:

- Should VFIO ask the IOMMU to use non-coherent DMA (ARM meaning) This depends on how the VFIO user expects to operate the DMA. If the VFIO user can issue cache maintenance ops then IOMMU_CACHE should be controlled by the user. I have no idea what platforms support user space cache maintenance ops.

But just like you said for intel meaning below, even if those ops are privileged a uAPI can be provided to support such usage if necessary.

Quoted 7 lines

- Should VFIO ask the IOMMU to suppress no-snoop (Intel meaning) This depends if the VFIO user has access to wbinvd or not.

wbinvd is a privileged instruction so normally userspace will not be able to access it.

Quoted 6 lines

Per Paolo recommendation there should be a uAPI someplace that allows userspace to issue wbinvd - basically the suppress no-snoop is also user controllable.

The two things are very similar and ultimately are a choice userspace should be making.

yes

Quoted 8 lines

From something like a qemu perspective things are more murkey - eg on ARM qemu needs to co-ordinate with the guest. Whatever IOMMU_CACHE mode VFIO is using must match the device coherent flag in the Linux guest. I'm guessing all Linux guest VMs only use coherent DMA for all devices today. I don't know if the cache maintaince ops are even permitted in an ARM VM.

I'll leave it to Jean to confirm. If only coherent DMA can be used in the guest on other platforms, suppose VFIO should not blindly set IOMMU_CACHE and in concept it should deny assigning a non-coherent device since no co-ordination with guest exists today.

So the bottomline is that we'll keep this no-snoop thing Intel-specific. For the basic skeleton we'll not support no-snoop thus the user needs to set enforce-snoop flag when creating an IOAS like this RFC v1 does. Also need to introduce a new flag instead of abusing IOMMU_CACHE in the kernel. For other platforms it may need a fix to deny non-coherent device (based on above open) for now.

Thanks
Kevin

A message that is not in the archive (sent off-list or not yet received): BN9PR11MB543327BB6D58AEF91AD2C9D18CB99@BN9PR11MB5433.namprd11.prod.outlook.com

maintainer
#174 lore raw

RE: [RFC 10/20] iommu/iommufd: Add IOMMU_DEVICE_GET_INFO

Quoted 65 lines

From: Tian, Kevin
Sent: Friday, October 15, 2021 9:02 AM

Quoted 25 lines

From: Jason Gunthorpe <[email protected]>
Sent: Thursday, October 14, 2021 11:43 PM

Quoted 13 lines
Quoted 7 lines
Quoted 4 lines

I think the key is whether other archs allow driver to decide DMA coherency and indirectly the underlying I/O page table format. If yes, then I don't see a reason why such decision should not be given to userspace for passthrough case.

The choice all comes down to if the other arches have cache maintenance instructions in the VM that *don't work*

Looks vfio always sets IOMMU_CACHE on all platforms as long as iommu supports it (true on all platforms except intel iommu which is dedicated for GPU):

vfio_iommu_type1_attach_group()

Quoted 8 lines
{
	...
	if (iommu_capable(bus, IOMMU_CAP_CACHE_COHERENCY))
		domain->prot |= IOMMU_CACHE;
	...
}

Should above be set according to whether a device is coherent?

Quoted 8 lines

For IOMMU_CACHE there are two questions related to the overloaded meaning:

- Should VFIO ask the IOMMU to use non-coherent DMA (ARM meaning) This depends on how the VFIO user expects to operate the DMA. If the VFIO user can issue cache maintenance ops then IOMMU_CACHE should be controlled by the user. I have no idea what platforms support user space cache maintenance ops.

But just like you said for intel meaning below, even if those ops are privileged a uAPI can be provided to support such usage if necessary.

Quoted 7 lines

- Should VFIO ask the IOMMU to suppress no-snoop (Intel meaning) This depends if the VFIO user has access to wbinvd or not.

wbinvd is a privileged instruction so normally userspace will not be able to access it.

Quoted 6 lines

Per Paolo recommendation there should be a uAPI someplace that allows userspace to issue wbinvd - basically the suppress no-snoop is also user controllable.

The two things are very similar and ultimately are a choice userspace should be making.

yes

Quoted 8 lines

From something like a qemu perspective things are more murkey - eg on ARM qemu needs to co-ordinate with the guest. Whatever IOMMU_CACHE mode VFIO is using must match the device coherent flag in the Linux guest. I'm guessing all Linux guest VMs only use coherent DMA for all devices today. I don't know if the cache maintaince ops are even permitted in an ARM VM.

Quoted 4 lines

I'll leave it to Jean to confirm. If only coherent DMA can be used in the guest on other platforms, suppose VFIO should not blindly set IOMMU_CACHE and in concept it should deny assigning a non-coherent device since no co-ordination with guest exists today.

Jean, what's your opinion?

Quoted 8 lines

So the bottomline is that we'll keep this no-snoop thing Intel-specific. For the basic skeleton we'll not support no-snoop thus the user needs to set enforce-snoop flag when creating an IOAS like this RFC v1 does. Also need to introduce a new flag instead of abusing IOMMU_CACHE in the kernel. For other platforms it may need a fix to deny non-coherent device (based on above open) for now.

Jason, want to check whether another option works here.

The current proposal lets the user to choose whether the I/O page table should be put in an enforced-snoop format, with the assumption that the user may have better knowledge than the kernel to know the no-snoop requirement. This leads to the current design which exposes whether an IOMMU behind a device supports enforce-snoop via IOMMU_DEVICE_GET_INFO to the user and then have the user to set/clear the enforce-snoop flag in IOMMU_IOASID_ALLOC.

This makes sense if there are no-snoop devices behind an IOMMU supporting enforce-snoop.

But in reality only Intel integrated GPUs have this special no-snoop trick (fixed knowledge), with a dedicated IOMMU which doesn't support enforce-snoop format at all. In this case there is no choice that the user can further make.

Also per Christoph's comment no-snoop is not an encouraged usage overall.

Given that I wonder whether the current vfio model better suites for this corner case, i.e. just let the kernel to handle instead of exposing it in uAPI. The simple policy (as vfio does) is to automatically set enforce-snoop when the target IOMMU supports it, otherwise enable vfio/kvm contract to handle no-snoop requirement.

I don't see any interest in implementing an Intel GPU driver fully in userspace. If just talking about possibility, a separate uAPI can be still introduced to allow the userspace to issue wbinvd as Paolo suggested.

One side-effect of doing so is that then we may have to support multiple domains per IOAS when Intel GPU and other devices are attached to the same IOAS. But this doesn't have to be implemented in the basic skeleton now. Can be extended later when we start working on Intel GPU support. And overall it also improves performance otherwise the user has to create two duplicated IOAS's (one for GPU, one for other devices) if assuming one domain per IOAS then every map request must be done twice in both IOAS's.

Does this option make sense?

btw fixing the abuse of IOMMU_CACHE is orthogonal to this uAPI open anyway.

Thanks
Kevin

#175 lore raw

Re: [RFC 10/20] iommu/iommufd: Add IOMMU_DEVICE_GET_INFO

Quoted 6 lines · Kevin
Quoted 4 lines

I'll leave it to Jean to confirm. If only coherent DMA can be used in the guest on other platforms, suppose VFIO should not blindly set IOMMU_CACHE and in concept it should deny assigning a non-coherent device since no co-ordination with guest exists today.

Jean, what's your opinion?

Yes a sanity check to prevent assigning non-coherent devices would be good, though I'm not particularly worried about non-coherent devices. PCIe on Arm should be coherent (according to the Base System Architecture). So vfio-pci devices should be coherent, but vfio-platform and mdev are case-by-case (hopefully all coherent since it concerns newer platforms).

More worrying, I thought we disabled No-Snoop for VFIO but I was wrong, it's left enabled. On Arm I don't think userspace can perform the right cache maintenance operations to maintain coherency with a device that issues No-Snoop writes. Userspace can issue clean+invalidate but not invalidate alone, so there is no equivalent to arch_sync_dma_for_cpu(). I think the worse that can happen is the device owner shooting itself in the foot by using No-Snoop, but would it hurt to disable it?

Thanks,
Jean

maintainer
#176 lore raw

Re: [RFC 10/20] iommu/iommufd: Add IOMMU_DEVICE_GET_INFO

Quoted 14 lines · Jean-Philippe Brucker
Quoted 6 lines · Kevin
Quoted 4 lines

I'll leave it to Jean to confirm. If only coherent DMA can be used in the guest on other platforms, suppose VFIO should not blindly set IOMMU_CACHE and in concept it should deny assigning a non-coherent device since no co-ordination with guest exists today.

Jean, what's your opinion?

Yes a sanity check to prevent assigning non-coherent devices would be good, though I'm not particularly worried about non-coherent devices. PCIe on Arm should be coherent (according to the Base System Architecture). So vfio-pci devices should be coherent, but vfio-platform and mdev are case-by-case (hopefully all coherent since it concerns newer platforms).

Quoted 6 lines

More worrying, I thought we disabled No-Snoop for VFIO but I was wrong, it's left enabled. On Arm I don't think userspace can perform the right cache maintenance operations to maintain coherency with a device that issues No-Snoop writes. Userspace can issue clean+invalidate but not invalidate alone, so there is no equivalent to arch_sync_dma_for_cpu().

So what happens in a VM? Does a VM know that arch_sync_dma_for_cpu() is not available?

And how does this work with the nested IOMMU translation? I thought I read in the SMMU spec that the io page table entries could control cachability including in nesting cases?

Quoted 2 lines

I think the worse that can happen is the device owner shooting itself in the foot by using No-Snoop, but would it hurt to disable it?

No, the worst is the same as Intel - a driver running in the guest VM assumes it can use arch_sync_dma_for_cpu() and acts accordingly, resulting in a broken VM.

Jason

#177 lore raw

Re: [RFC 10/20] iommu/iommufd: Add IOMMU_DEVICE_GET_INFO

Quoted 22 lines · Jason Gunthorpe
Quoted 14 lines · Jean-Philippe Brucker
Quoted 6 lines · Kevin
Quoted 4 lines

I'll leave it to Jean to confirm. If only coherent DMA can be used in the guest on other platforms, suppose VFIO should not blindly set IOMMU_CACHE and in concept it should deny assigning a non-coherent device since no co-ordination with guest exists today.

Jean, what's your opinion?

Yes a sanity check to prevent assigning non-coherent devices would be good, though I'm not particularly worried about non-coherent devices. PCIe on Arm should be coherent (according to the Base System Architecture). So vfio-pci devices should be coherent, but vfio-platform and mdev are case-by-case (hopefully all coherent since it concerns newer platforms).

Quoted 6 lines

More worrying, I thought we disabled No-Snoop for VFIO but I was wrong, it's left enabled. On Arm I don't think userspace can perform the right cache maintenance operations to maintain coherency with a device that issues No-Snoop writes. Userspace can issue clean+invalidate but not invalidate alone, so there is no equivalent to arch_sync_dma_for_cpu().

Quoted 2 lines

So what happens in a VM? Does a VM know that arch_sync_dma_for_cpu() is not available?

This would only affect userspace drivers, it's only host or guest userspace that cannot issue the maintenance operations. The VM can do arch_sync_dma_for_cpu()

Thanks,
Jean

Quoted 13 lines

And how does this work with the nested IOMMU translation? I thought I read in the SMMU spec that the io page table entries could control cachability including in nesting cases?

Quoted 2 lines

I think the worse that can happen is the device owner shooting itself in the foot by using No-Snoop, but would it hurt to disable it?

No, the worst is the same as Intel - a driver running in the guest VM assumes it can use arch_sync_dma_for_cpu() and acts accordingly, resulting in a broken VM.

Jason

maintainer
#178 lore raw

Re: [RFC 10/20] iommu/iommufd: Add IOMMU_DEVICE_GET_INFO

Quoted 23 lines · Jean-Philippe Brucker
Quoted 22 lines · Jason Gunthorpe
Quoted 20 lines · Jean-Philippe Brucker
Quoted 6 lines · Kevin
Quoted 4 lines

I'll leave it to Jean to confirm. If only coherent DMA can be used in the guest on other platforms, suppose VFIO should not blindly set IOMMU_CACHE and in concept it should deny assigning a non-coherent device since no co-ordination with guest exists today.

Jean, what's your opinion?

Yes a sanity check to prevent assigning non-coherent devices would be good, though I'm not particularly worried about non-coherent devices. PCIe on Arm should be coherent (according to the Base System Architecture). So vfio-pci devices should be coherent, but vfio-platform and mdev are case-by-case (hopefully all coherent since it concerns newer platforms).

More worrying, I thought we disabled No-Snoop for VFIO but I was wrong, it's left enabled. On Arm I don't think userspace can perform the right cache maintenance operations to maintain coherency with a device that issues No-Snoop writes. Userspace can issue clean+invalidate but not invalidate alone, so there is no equivalent to arch_sync_dma_for_cpu().

Quoted 6 lines
Quoted 2 lines

So what happens in a VM? Does a VM know that arch_sync_dma_for_cpu() is not available?

This would only affect userspace drivers, it's only host or guest userspace that cannot issue the maintenance operations. The VM can do arch_sync_dma_for_cpu()

This seems out of sync with what the KVM people asked for - any operation a VM can do should be doable inside a normal process as well?

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

maintainer
#179 lore raw

Re: [RFC 10/20] iommu/iommufd: Add IOMMU_DEVICE_GET_INFO

In reply to Tian, Kevin
Quoted 4 lines · Kevin

But in reality only Intel integrated GPUs have this special no-snoop trick (fixed knowledge), with a dedicated IOMMU which doesn't support enforce-snoop format at all. In this case there is no choice that the user can further make.

huh? That is not true at all. no-snoop is a PCIe spec behavior, any device can trigger it

What is true today is that only Intel GPU drivers are crazy enough to use it on Linux without platform support.

Quoted 2 lines

Also per Christoph's comment no-snoop is not an encouraged usage overall.

I wouldn't say that, I think Christoph said using it without API support through the DMA layer is very wrong.

DMA layer support could be added if there was interest, all the pieces are there to do it.

Quoted 5 lines

Given that I wonder whether the current vfio model better suites for this corner case, i.e. just let the kernel to handle instead of exposing it in uAPI. The simple policy (as vfio does) is to automatically set enforce-snoop when the target IOMMU supports it, otherwise enable vfio/kvm contract to handle no-snoop requirement.

IMHO you need to model it as the KVM people said - if KVM can execute a real wbinvd in a VM then an ioctl shoudl be available to normal userspace to run the same instruction.

So, figure out some rules to add a wbinvd ioctl to iommufd that makes some kind of sense and logically kvm is just triggering that ioctl, including whatever security model protects it.

I have no idea what security model makes sense for wbinvd, that is the major question you have to answer.

And obviously none of this should be hidden behind a private API to KVM.

Quoted 8 lines

I don't see any interest in implementing an Intel GPU driver fully in userspace. If just talking about possibility, a separate uAPI can be still introduced to allow the userspace to issue wbinvd as Paolo suggested.

One side-effect of doing so is that then we may have to support multiple domains per IOAS when Intel GPU and other devices are attached to the same IOAS.

I think we already said the IOAS should represent a single IO page table layout?

So if there is a new for incompatible layouts then the IOAS should be duplicated.

Otherwise, I also think the iommu core code should eventually learn to share the io page table across HW instances. Eg ARM has a similar efficiency issue if there are multiple SMMU HW blocks.

Jason

maintainer
#180 lore raw

RE: [RFC 10/20] iommu/iommufd: Add IOMMU_DEVICE_GET_INFO

Quoted 5 lines

From: Jason Gunthorpe <[email protected]>
Sent: Friday, October 22, 2021 7:31 AM

On Thu, Oct 21, 2021 at 02:26:00AM +0000, Tian, Kevin wrote:

Quoted 7 lines
Quoted 4 lines

But in reality only Intel integrated GPUs have this special no-snoop trick (fixed knowledge), with a dedicated IOMMU which doesn't support enforce-snoop format at all. In this case there is no choice that the user can further make.

huh? That is not true at all. no-snoop is a PCIe spec behavior, any device can trigger it

yes, I should say Intel GPU 'drivers'.

Quoted 9 lines

What is true today is that only Intel GPU drivers are crazy enough to use it on Linux without platform support.

Quoted 2 lines

Also per Christoph's comment no-snoop is not an encouraged usage overall.

I wouldn't say that, I think Christoph said using it without API support through the DMA layer is very wrong.

ok, sounds like I drew out a wrong impression from previous discussion.

Quoted 10 lines

DMA layer support could be added if there was interest, all the pieces are there to do it.

Quoted 5 lines

Given that I wonder whether the current vfio model better suites for this corner case, i.e. just let the kernel to handle instead of exposing it in uAPI. The simple policy (as vfio does) is to automatically set enforce-snoop when the target IOMMU supports it, otherwise enable vfio/kvm contract to handle no-snoop requirement.

Quoted 7 lines

IMHO you need to model it as the KVM people said - if KVM can execute a real wbinvd in a VM then an ioctl shoudl be available to normal userspace to run the same instruction.

So, figure out some rules to add a wbinvd ioctl to iommufd that makes some kind of sense and logically kvm is just triggering that ioctl, including whatever security model protects it.

wbinvd instruction is x86 specific. Here we'd want a generic cache invalidation ioctl and then need some form of arch callbacks though x86 is the only concerned platform for now.

Quoted 3 lines

I have no idea what security model makes sense for wbinvd, that is the major question you have to answer.

wbinvd flushes the entire cache in local cpu. It's more a performance isolation problem but nothing can prevent it once the user is allowed to call this ioctl. This is the main reason why wbinvd is a privileged instruction and is emulated by kvm as a nop unless an assigned device has no-snoop requirement. alternatively the user may call clflush which is unprivileged and can invalidate a specific cache line, though not efficient for flushing a big buffer.

One tricky thing is that the process might be scheduled to different cpus between writing buffers and calling wbinvd ioctl. Since wbvind only has local behavior, it requires the ioctl to call wbinvd on all cpus that this process has previously been scheduled on.

kvm maintains a dirty cpu mask in its preempt notifier (see kvm_sched_in/out).

Is there any concern if iommufd also follows the same mechanism? Currently looks preempt notifier is only used by kvm. Not sure whether there is strong criteria around using it. and this local behavior may not apply to all platforms (then better hidden behind arch callback?)

Quoted 9 lines

And obviously none of this should be hidden behind a private API to KVM.

Quoted 5 lines

I don't see any interest in implementing an Intel GPU driver fully in userspace. If just talking about possibility, a separate uAPI can be still introduced to allow the userspace to issue wbinvd as Paolo suggested.

Quoted 6 lines
Quoted 3 lines

One side-effect of doing so is that then we may have to support multiple domains per IOAS when Intel GPU and other devices are attached to the same IOAS.

I think we already said the IOAS should represent a single IO page table layout?

yes. I was just talking about tradeoff possibility if the aforementioned option is feasible. Now based on above discussion then we will resume back to this one-ioas-one-layout model.

Quoted 8 lines

So if there is a new for incompatible layouts then the IOAS should be duplicated.

Otherwise, I also think the iommu core code should eventually learn to share the io page table across HW instances. Eg ARM has a similar efficiency issue if there are multiple SMMU HW blocks.

or we may introduce an alias ioas concept that any change on one ioas is automatically replayed on the alias ioas if two ioas's are created just due to incompatible layout.

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

maintainer
#181 lore raw

Re: [RFC 10/20] iommu/iommufd: Add IOMMU_DEVICE_GET_INFO

Quoted 11 lines · Kevin
Quoted 2 lines

I have no idea what security model makes sense for wbinvd, that is the major question you have to answer.

wbinvd flushes the entire cache in local cpu. It's more a performance isolation problem but nothing can prevent it once the user is allowed to call this ioctl. This is the main reason why wbinvd is a privileged instruction and is emulated by kvm as a nop unless an assigned device has no-snoop requirement. alternatively the user may call clflush which is unprivileged and can invalidate a specific cache line, though not efficient for flushing a big buffer.

Quoted 4 lines

One tricky thing is that the process might be scheduled to different cpus between writing buffers and calling wbinvd ioctl. Since wbvind only has local behavior, it requires the ioctl to call wbinvd on all cpus that this process has previously been scheduled on.

That is such a hassle, you may want to re-open this with the kvm people as it seems ARM also has different behavior between VM and process here.

The ideal is already not being met, so maybe we can keep special casing cache ops?

Quoted 4 lines

Is there any concern if iommufd also follows the same mechanism? Currently looks preempt notifier is only used by kvm. Not sure whether there is strong criteria around using it. and this local behavior may not apply to all platforms (then better hidden behind arch callback?)

I don't have any desire to see a performance cost to implement an ioctl that nothing will ever call just to satisify a idealized target from the kvm folks..

Jason

maintainer
#182 lore raw

RE: [RFC 10/20] iommu/iommufd: Add IOMMU_DEVICE_GET_INFO

Quoted 21 lines

From: Jason Gunthorpe <[email protected]>
Sent: Tuesday, October 26, 2021 7:35 AM

Quoted 11 lines · Kevin
Quoted 2 lines

I have no idea what security model makes sense for wbinvd, that is the major question you have to answer.

wbinvd flushes the entire cache in local cpu. It's more a performance isolation problem but nothing can prevent it once the user is allowed to call this ioctl. This is the main reason why wbinvd is a privileged instruction and is emulated by kvm as a nop unless an assigned device has no-snoop requirement. alternatively the user may call clflush which is unprivileged and can invalidate a specific cache line, though not efficient for flushing a big buffer.

Quoted 4 lines

One tricky thing is that the process might be scheduled to different cpus between writing buffers and calling wbinvd ioctl. Since wbvind only has local behavior, it requires the ioctl to call wbinvd on all cpus that this process has previously been scheduled on.

Quoted 7 lines

That is such a hassle, you may want to re-open this with the kvm people as it seems ARM also has different behavior between VM and process here.

The ideal is already not being met, so maybe we can keep special casing cache ops?

will do.

maintainer
#183 lore raw

RE: [RFC 10/20] iommu/iommufd: Add IOMMU_DEVICE_GET_INFO

In reply to Jason Gunthorpe
Quoted 21 lines

From: Jason Gunthorpe <[email protected]>
Sent: Tuesday, October 26, 2021 7:35 AM

Quoted 11 lines · Kevin
Quoted 2 lines

I have no idea what security model makes sense for wbinvd, that is the major question you have to answer.

wbinvd flushes the entire cache in local cpu. It's more a performance isolation problem but nothing can prevent it once the user is allowed to call this ioctl. This is the main reason why wbinvd is a privileged instruction and is emulated by kvm as a nop unless an assigned device has no-snoop requirement. alternatively the user may call clflush which is unprivileged and can invalidate a specific cache line, though not efficient for flushing a big buffer.

Quoted 4 lines

One tricky thing is that the process might be scheduled to different cpus between writing buffers and calling wbinvd ioctl. Since wbvind only has local behavior, it requires the ioctl to call wbinvd on all cpus that this process has previously been scheduled on.

Quoted 7 lines

That is such a hassle, you may want to re-open this with the kvm people as it seems ARM also has different behavior between VM and process here.

The ideal is already not being met, so maybe we can keep special casing cache ops?

Now Paolo confirmed wbinvd ioctl is just a thought experiment.

Then Jason, want to have a clarification on 'keep special casing' here.

Did you mean adopting the vfio model which neither allows the user to decide no-snoop format nor provides a wbinvd ioctl for the user to manage buffers used for no-snoop traffic, or still wanting the user to decide no-snoop format but not implementing a wbinvd ioctl?

The latter option sounds a bit incomplete from uAPI p.o.v. but it allows us to stay with one-format-one-ioas policy. And anyway the userspace can still call clflush to do cacheline-based invalidation, if necessary.

The former option would force us to support multi-formats-one-ioas.

either case it's iommufd which decides and tells kvm whether wbinvd is allowed for a process.

Thanks
Kevin

maintainer
#184 lore raw

Re: [RFC 10/20] iommu/iommufd: Add IOMMU_DEVICE_GET_INFO

Quoted 29 lines · Kevin
Quoted 21 lines

From: Jason Gunthorpe <[email protected]>
Sent: Tuesday, October 26, 2021 7:35 AM

Quoted 11 lines · Kevin
Quoted 2 lines

I have no idea what security model makes sense for wbinvd, that is the major question you have to answer.

wbinvd flushes the entire cache in local cpu. It's more a performance isolation problem but nothing can prevent it once the user is allowed to call this ioctl. This is the main reason why wbinvd is a privileged instruction and is emulated by kvm as a nop unless an assigned device has no-snoop requirement. alternatively the user may call clflush which is unprivileged and can invalidate a specific cache line, though not efficient for flushing a big buffer.

Quoted 4 lines

One tricky thing is that the process might be scheduled to different cpus between writing buffers and calling wbinvd ioctl. Since wbvind only has local behavior, it requires the ioctl to call wbinvd on all cpus that this process has previously been scheduled on.

Quoted 7 lines

That is such a hassle, you may want to re-open this with the kvm people as it seems ARM also has different behavior between VM and process here.

The ideal is already not being met, so maybe we can keep special casing cache ops?

Quoted 8 lines

Now Paolo confirmed wbinvd ioctl is just a thought experiment.

Then Jason, want to have a clarification on 'keep special casing' here.

Did you mean adopting the vfio model which neither allows the user to decide no-snoop format nor provides a wbinvd ioctl for the user to manage buffers used for no-snoop traffic, or still wanting the user to decide no-snoop format but not implementing a wbinvd ioctl?

IMHO if the wbinvd is just a thought experiment then userspace should directly control the wbinvd enable and present the iommufd as 'proof' to enable it.

The thought experiment tells us that iommufd should have a wbinvd ioctl, even if we don't implement it today. So access to iommufd is also access to wbinvd.

iommufd should control/report via intel sepecific areas if the IOS is no-snoop blocking or not so userspace can decide what it wants to do.

Jason