Skip to contentSkip to conversation

iommu/amd: Introduce AMD Hardware-accelerated Virtualized IOMMU (vIOMMU) Support

PATCH In review Suravee Suthikulpanit 5 participants 51 messages Sep 30 – Oct 22

Subsystems (3)
lore
Share & tools
lore

Suravee Suthikulpanit

author reviewer
#0 lore raw

v6 cover iommu/amd: Introduce AMD Hardware-accelerated Virtualized IOMMU (vIOMMU) Support

OVERVIEW
========

AMD IOMMU introduces the vIOMMU feature, which provides partial hardware acceleration when implementing Guest IOMMUs. This feature provides acceleration for guest Command Buffer, Event Log, and PPR Log. This eliminates the CPU overhead needed for the supporting HV intercepts and reduces the latency of these operations.

When a guest attempts to access guest IOMMU MMIO registers with offsets between 8KB and 12KB (i.e. the 3rd 4K region) such as the Command Buffer, Event Log and PPR Log head and tail pointer registers, this is serviced directly by the IOMMU. When the IOMMU accesses a Command Buffer, PPR Log or a COMPLETION_WAIT store location in memory, it directly accesses guest physical memory. The HV/VMM continues to trap and emulate the IOMMU configuration MMIO registers between 0KB and 4KB (i.e. the 1st 4K region), which are primarily used during initialization.

Additionally, the HV must initialize the vIOMMU feature, map MMIO resources between the VMs and the IOMMU, manage additional supporting data structures in memory (e.g. GPA->SPA translation DTE, Device ID and Domain ID mapping tables), and allocate/map vIOMMU Private Address region used as backing storage memory for the IOMMU. Support for new IOMMU command and events specifically for vIOMMU are also added.

Guest IOMMUs are IOMMUs exposed to VMs with additional support from VMM (QEMU) to generate guest ACPI IVRS table and define guest PCI topology for IOMMU and pass-through VFIO devices, which are not covered by this series.

For more detail, please see the vIOMMU section of the AMD IOMMU Specification[1].

ABOUT SERIES V6
===============

This is version 6 of the AMD HW-vIOMMU series [2]. It is implemented on top of the IOMMUFD vIOMMU, vDevice, and nested-domain framework in Linux v7.3.0-rc3 (base 704340f1cd0d).

Note: This series is a partial implementation of AMD hardware-accelerated vIOMMU. Subsequent series will add hardware-queue and extended interrupt remapping support, which are needed to fully support AMD IOMMU virtualization in the guest VM. The fully supported version is available in a GitHub repository [3].

The series is organized into the following subsets:

  Patch 1-3   : Events and vIOMMU feature detect/init
  Patch 4-8   : Introduce IOMMUFD vIOMMU support and VF MMIO setup
  Patch 9-14  : Introduce and map vIOMMU Private Address (IPA) region

Patch 15-18 : IOMMUFD vDevice, DevID/DomID maps, and nested attach Patch 19-24 : Translate-device-ID pool, per-vIOMMU translation DTE, and
PCI-reserve relocation

CHANGES FROM V5
===============

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

IOMMUFD vIOMMU and VF MMIO (patches 4-8):
* Patch 6: In iommu_unmap_mmio_space(), release the host MMIO reservation only when mmio_base is set. iommu_map_mmio_space() already drops the reservation when ioremap() fails, so a failed map is not released twice.

IPA / DTE infrastructure (patches 9-14):
* Patch 13: Clear the self DTE only while CONTROL_CMDBUF_EN is set. disable_iommus() clears that bit before teardown, and INV_DTE would time out. dev_data is still freed.

IOMMUFD vDevice and DevID/DomID maps (patches 15-18):
* Patch 16: Program QueueID (bits 45:30) in the DevID table entry. Callers pass 0. QueueID is not supported yet.
* Patch 17: Drop amd_viommu_domain_id_update(). Callers hold vfctrl_lock and use amd_viommu_domain_id_update_locked().
* Patch 18: Prefill each gdev_id with hdev_id 0 and QueueID 0.

Translate device ID (patches 19-24):
* Patch 19: Reserve the IOMMU function devid from init_iommu_one_late(). Clearing rlookup keeps that function off probe_device(), while the vIOMMU self DTE still occupies the slot.
* Patch 23: Destroy trans_devid_lock on init error and on vIOMMU destroy, after the last unlock, the same way as vfctrl_lock.

[1] IOMMU Specification: https://docs.amd.com/v/u/en-US/48882_3.11_IOMMU_PUB
[2] Series v6 tree: https://github.com/AMDESE/linux-iommu/tree/linux-7.3.0-rc3-amd-viommu_upstream_v6
[3] Fully supported tree (work-in-progress): https://github.com/AMDESE/linux-iommu/tree/wip/v7.3.0-rc3-viommu_20260930

Thank you,
Suravee

Suravee Suthikulpanit (24):
iommu/amd: Introduce vIOMMU-specific events and event iommu/amd: Introduce EVENT_TYPE_GUEST_EVENT_FAULT iommu/amd: Detect and initialize AMD vIOMMU feature iommu/amd: Introduce IOMMUFD vIOMMU support for AMD iommu/amd: Allocate Guest IDs for IOMMUFD vIOMMU instances iommu/amd: Map vIOMMU VF and VF Control MMIO BARs iommu/amd: Add support for AMD vIOMMU VF MMIO region iommu/amd: Introduce Reset vMMIO Command
iommu/amd: Introduce and map vIOMMU private IPA region iommu/amd: Pass iommu to device_flush_dte()
iommu/amd: Pass iommu and devid to amd_iommu_make_clear_dte() iommu/amd: Store per-segment iommu_dev_data in an xarray iommu/amd: Program IOMMU DTE with the private IPA domain iommu/amd: Add per-VM private IPA alloc/map helpers iommu/amd: Add helper functions to manage DevID / DomID mapping tables iommu/amd: Add IOMMUFD vDevice and DevID mapping iommu/amd: Program nested DTE and DomID map on attach iommu/amd: Init and clear vIOMMU DevID and DomID maps iommu/amd: Add per-segment translate device ID pool iommu/amd: Reserve translate-device-id for PCI requestor aliases iommu/amd: Add translation DTE and VFctrl TransDevID helpers iommu/amd: Add translate-device-id alloc/free with vIOMMU owner iommu/amd: Assign per-vIOMMU translate device ID iommu/amd: Relocate vIOMMU translate-device-id on PCI reserve

 drivers/iommu/amd/Makefile          |   2 +-
 drivers/iommu/amd/amd_iommu.h       |  34 +-
 drivers/iommu/amd/amd_iommu_types.h | 125 ++++-
 drivers/iommu/amd/amd_viommu.h      |  90 ++++
 drivers/iommu/amd/init.c            |  84 +++-
 drivers/iommu/amd/iommu.c           | 496 +++++++++++++++++---
 drivers/iommu/amd/iommufd.c         | 185 +++++++-
 drivers/iommu/amd/nested.c          |  76 ++-
 drivers/iommu/amd/trans_devid.c     | 376 +++++++++++++++
 drivers/iommu/amd/viommu.c          | 698 ++++++++++++++++++++++++++++
 include/uapi/linux/iommufd.h        |  10 +
 11 files changed, 2090 insertions(+), 86 deletions(-)
 create mode 100644 drivers/iommu/amd/amd_viommu.h
 create mode 100644 drivers/iommu/amd/trans_devid.c
 create mode 100644 drivers/iommu/amd/viommu.c
Signature · 1 line
2.34.1
author reviewer
#1 lore raw

PATCH v6 1/24 iommu/amd: Introduce vIOMMU-specific events and event

Adding support for new vIOMMU events:
* vIOMMU Hardware Error event

Also, adding support for the additional vIOMMU related flags in existing events.

---
 drivers/iommu/amd/amd_iommu_types.h |  5 ++++
 drivers/iommu/amd/iommu.c           | 37 +++++++++++++++++++----------
 2 files changed, 29 insertions(+), 13 deletions(-)
drivers/iommu/amd/amd_iommu_types.h +5 -0
index 8241ef922519..f26a9059a356 100644
@@ -138,6 +138,8 @@
 #define EVENT_TYPE_IOTLB_INV_TO	0x7
 #define EVENT_TYPE_INV_DEV_REQ	0x8
 #define EVENT_TYPE_INV_PPR_REQ	0x9
+#define EVENT_TYPE_VIOMMU_HW_ERR	0xc
+
 #define EVENT_TYPE_RMP_FAULT	0xd
 #define EVENT_TYPE_RMP_HW_ERR	0xe
 #define EVENT_DEVID_MASK	0xffff
@@ -251,6 +253,9 @@
 #define EVTLOG_SIZE_MAX		SZ_512K /* 32K entries */
 #define EVTLOG_LEN_MASK_MAX	(0xFULL << EVTLOG_SIZE_SHIFT)
 
+/* Constants for vIOMMU event fields */
+#define EVENT_VFLAGS_MASK		GENMASK_ULL(31, 27)
+
 /* Constants for PPR Log handling */
 #define PPRLOG_ENTRY_SIZE	0x10
 #define PPRLOG_SIZE_SHIFT	56
drivers/iommu/amd/iommu.c +24 -13
index 56262f6b1f70..43a5690721af 100644
@@ -859,7 +859,7 @@ static void amd_iommu_report_rmp_fault(struct amd_iommu *iommu, volatile u32 *ev
 
 static void amd_iommu_report_page_fault(struct amd_iommu *iommu,
 					u16 devid, u16 domain_id,
-					u64 address, int flags)
+					u64 address, int flags, u8 vflags)
 {
 	struct iommu_dev_data *dev_data = NULL;
 	struct pci_dev *pdev;
@@ -894,13 +894,13 @@ static void amd_iommu_report_page_fault(struct amd_iommu *iommu,
 		}
 
 		if (__ratelimit(&dev_data->rs)) {
-			pci_err(pdev, "Event logged [IO_PAGE_FAULT domain=0x%04x address=0x%llx flags=0x%04x]\n",
-				domain_id, address, flags);
+			pci_err(pdev, "Event logged [IO_PAGE_FAULT domain=0x%04x address=%#llx flags=0x%04x vflags=%#x]\n",
+				domain_id, address, flags, vflags);
 		}
 	} else {
-		pr_err_ratelimited("Event logged [IO_PAGE_FAULT device=%04x:%02x:%02x.%x domain=0x%04x address=0x%llx flags=0x%04x]\n",
+		pr_err_ratelimited("Event logged [IO_PAGE_FAULT device=%04x:%02x:%02x.%x domain=0x%04x address=%#llx flags=0x%04x vflags=%#x]\n",
 			iommu->pci_seg->id, PCI_BUS_NUM(devid), PCI_SLOT(devid), PCI_FUNC(devid),
-			domain_id, address, flags);
+			domain_id, address, flags, vflags);
 	}
 
 out:
@@ -950,6 +950,7 @@ static void iommu_print_event(struct amd_iommu *iommu, void *__evt)
 	int count = 0;
 	u64 address, ctrl;
 	u32 pasid;
+	u8 vflags;
 
 retry:
 	type    = (event[1] >> EVENT_TYPE_SHIFT)  & EVENT_TYPE_MASK;
@@ -970,29 +971,30 @@ static void iommu_print_event(struct amd_iommu *iommu, void *__evt)
 		goto retry;
 	}
 
+	vflags = FIELD_GET(EVENT_VFLAGS_MASK, event[0]);
+
 	if (type == EVENT_TYPE_IO_FAULT) {
-		amd_iommu_report_page_fault(iommu, devid, pasid, address, flags);
+		amd_iommu_report_page_fault(iommu, devid, pasid, address, flags, vflags);
 		return;
 	}
 
 	switch (type) {
 	case EVENT_TYPE_ILL_DEV:
-		dev_err(dev, "Event logged [ILLEGAL_DEV_TABLE_ENTRY device=%04x:%02x:%02x.%x pasid=0x%05x address=0x%llx flags=0x%04x]\n",
+		dev_err(dev, "Event logged [ILLEGAL_DEV_TABLE_ENTRY device=%04x:%02x:%02x.%x pasid=0x%05x address=%#llx flags=0x%04x vflags=%#x]\n",
 			iommu->pci_seg->id, PCI_BUS_NUM(devid), PCI_SLOT(devid), PCI_FUNC(devid),
-			pasid, address, flags);
+			pasid, address, flags, vflags);
 		dev_err(dev, "Control Reg : 0x%llx\n", ctrl);
 		dump_dte_entry(iommu, devid);
 		break;
 	case EVENT_TYPE_DEV_TAB_ERR:
-		dev_err(dev, "Event logged [DEV_TAB_HARDWARE_ERROR device=%04x:%02x:%02x.%x "
-			"address=0x%llx flags=0x%04x]\n",
+		dev_err(dev, "Event logged [DEV_TAB_HARDWARE_ERROR device=%04x:%02x:%02x.%x address=%#llx flags=0x%04x vflags=%#x]\n",
 			iommu->pci_seg->id, PCI_BUS_NUM(devid), PCI_SLOT(devid), PCI_FUNC(devid),
-			address, flags);
+			address, flags, vflags);
 		break;
 	case EVENT_TYPE_PAGE_TAB_ERR:
-		dev_err(dev, "Event logged [PAGE_TAB_HARDWARE_ERROR device=%04x:%02x:%02x.%x pasid=0x%04x address=0x%llx flags=0x%04x]\n",
+		dev_err(dev, "Event logged [PAGE_TAB_HARDWARE_ERROR device=%04x:%02x:%02x.%x pasid=0x%04x address=%#llx flags=0x%04x vflags=%#x]\n",
 			iommu->pci_seg->id, PCI_BUS_NUM(devid), PCI_SLOT(devid), PCI_FUNC(devid),
-			pasid, address, flags);
+			pasid, address, flags, vflags);
 		break;
 	case EVENT_TYPE_ILL_CMD:
 		dev_err(dev, "Event logged [ILLEGAL_COMMAND_ERROR address=0x%llx]\n", address);
@@ -1021,6 +1023,15 @@ static void iommu_print_event(struct amd_iommu *iommu, void *__evt)
 	case EVENT_TYPE_INV_PPR_REQ:
 		amd_iommu_report_ppr_err(iommu, event, devid, address, flags);
 		break;
+	case EVENT_TYPE_VIOMMU_HW_ERR:
+	{
+		u16 gid = event[0] & 0xFFFF;
+		u8 src = (event[0] >> 16) & 0x3;
+
+		dev_err(dev, "Event logged [VIOMMU_HW_ERR gid=%#x address=%#llx src=%#x flags=0x%04x vflags=%#x]\n",
+			gid, address, src, flags, vflags);
+		break;
+	}
 	default:
 		dev_err(dev, "Event logged [UNKNOWN event[0]=0x%08x event[1]=0x%08x event[2]=0x%08x event[3]=0x%08x\n",
 			event[0], event[1], event[2], event[3]);
Signature · 1 line
2.34.1
author reviewer
#2 lore raw

PATCH v6 2/24 iommu/amd: Introduce EVENT_TYPE_GUEST_EVENT_FAULT

In reply to Suravee Suthikulpanit

INSERT_GUEST_EVENT with non-zero reserved bits logs GUEST_EVENT_FAULT followed by the original guest event. Consume both event-log slots, wrapping via evt_buf+(head+EVTLOG_ENTRY_SIZE)%size, and dump the guest payload raw so it is not decoded as a host event.

Wait until occupancy is at least two entries, not merely until live tail is not the pair offset. Re-read the event-log tail each poll iteration so a return of 2 cannot advance head past a stale cached tail and walk empty slots with the type-0 erratum retry.

Retry a type-0 guest slot up to LOOP_TIMEOUT, the same way erratum 732 waits for the primary slot. Return 2 only when that slot is already in [head, live_tail). Return 1 if the pair never appears in tail. Rate-limit the guest-triggered logs.

Log an invalid guest-event when the pair slot is still missing or type 0 after that wait, instead of stalling the event log.

---
 drivers/iommu/amd/amd_iommu_types.h |   2 +
 drivers/iommu/amd/iommu.c           | 116 +++++++++++++++++++++++++---
 2 files changed, 107 insertions(+), 11 deletions(-)
drivers/iommu/amd/amd_iommu_types.h +2 -0
index f26a9059a356..704b4c8acdc5 100644
@@ -138,6 +138,7 @@
 #define EVENT_TYPE_IOTLB_INV_TO	0x7
 #define EVENT_TYPE_INV_DEV_REQ	0x8
 #define EVENT_TYPE_INV_PPR_REQ	0x9
+#define EVENT_TYPE_GUEST_EVENT_FAULT	0xb
 #define EVENT_TYPE_VIOMMU_HW_ERR	0xc
 
 #define EVENT_TYPE_RMP_FAULT	0xd
@@ -255,6 +256,7 @@
 
 /* Constants for vIOMMU event fields */
 #define EVENT_VFLAGS_MASK		GENMASK_ULL(31, 27)
+#define EVENT_GID_MASK			GENMASK(15, 0)
 
 /* Constants for PPR Log handling */
 #define PPRLOG_ENTRY_SIZE	0x10
drivers/iommu/amd/iommu.c +105 -11

160 lines not on this page · raw message

Signature · 1 line
2.34.1
maintainer
#3 lore raw

Re: [PATCH v6 02/24] iommu/amd: Introduce EVENT_TYPE_GUEST_EVENT_FAULT

Quoted 18 lines · Suravee Suthikulpanit

INSERT_GUEST_EVENT with non-zero reserved bits logs GUEST_EVENT_FAULT followed by the original guest event. Consume both event-log slots, wrapping via evt_buf+(head+EVTLOG_ENTRY_SIZE)%size, and dump the guest payload raw so it is not decoded as a host event.

Wait until occupancy is at least two entries, not merely until live tail is not the pair offset. Re-read the event-log tail each poll iteration so a return of 2 cannot advance head past a stale cached tail and walk empty slots with the type-0 erratum retry.

Retry a type-0 guest slot up to LOOP_TIMEOUT, the same way erratum 732 waits for the primary slot. Return 2 only when that slot is already in [head, live_tail). Return 1 if the pair never appears in tail. Rate-limit the guest-triggered logs.

Quoted 3 lines

Log an invalid guest-event when the pair slot is still missing or type 0 after that wait, instead of stalling the event log.

As per spec:
GUEST_EVENT_FAULT event is always written in pairs with a guest event entry
immediately after it into the event log.

Why not add one additional check in iommu_poll_events() such that:
if event at head is GUEST_EVENT_FAULT and actual fault event is
not-yet-written by HW just return.
If subsequent event is also written then handle the event.

and increase head pointer accordingly.

Quoted 52 lines
---
 drivers/iommu/amd/amd_iommu_types.h |   2 +
 drivers/iommu/amd/iommu.c           | 116 +++++++++++++++++++++++++---
 2 files changed, 107 insertions(+), 11 deletions(-)
drivers/iommu/amd/amd_iommu_types.h +2 -0
index f26a9059a356..704b4c8acdc5 100644
@@ -138,6 +138,7 @@
 #define EVENT_TYPE_IOTLB_INV_TO	0x7
 #define EVENT_TYPE_INV_DEV_REQ	0x8
 #define EVENT_TYPE_INV_PPR_REQ	0x9
+#define EVENT_TYPE_GUEST_EVENT_FAULT	0xb
 #define EVENT_TYPE_VIOMMU_HW_ERR	0xc
 
 #define EVENT_TYPE_RMP_FAULT	0xd
@@ -255,6 +256,7 @@
 
 /* Constants for vIOMMU event fields */
 #define EVENT_VFLAGS_MASK		GENMASK_ULL(31, 27)
+#define EVENT_GID_MASK			GENMASK(15, 0)
 
 /* Constants for PPR Log handling */
 #define PPRLOG_ENTRY_SIZE	0x10
drivers/iommu/amd/iommu.c +11 -1
index 43a5690721af..2df81e29519e 100644
@@ -942,8 +942,18 @@ static void amd_iommu_report_ppr_err(struct amd_iommu *iommu, volatile u32 *even
 	pci_dev_put(pdev);
 }
 
-static void iommu_print_event(struct amd_iommu *iommu, void *__evt)
+static u32 evtlog_used(u32 head, u32 tail)
 {
+	return (tail + amd_iommu_evtlog_size - head) % amd_iommu_evtlog_size;
+}
+
+/*
+ * Decode one event-log slot (or a GUEST_EVENT_FAULT pair).
+ * Returns the number of slots consumed.
+ */
+static unsigned int iommu_print_event(struct amd_iommu *iommu, u32 head)
+{
+	void *__evt = iommu->evt_buf + head;
 	struct device *dev = iommu->iommu.dev;
 	int type, devid, flags;
 	volatile u32 *event = __evt;

drivers/iommu/amd/iommu.c static void iommu_print_event(struct amd_iommu *iommu, void *__evt) · line 978

Quoted 5 lines
drivers/iommu/amd/iommu.c +1 -1
@@ -965,7 +975,7 @@ static void iommu_print_event(struct amd_iommu *iommu, void *__evt)
 		/* Did we hit the erratum? */
 		if (++count == LOOP_TIMEOUT) {
 			pr_err("No event written to event log\n");
-			return;
+			return 1;

This will cause head to progress event though there is no event.

Quoted 35 lines
drivers/iommu/amd/iommu.c +20 -1
 		}
 		udelay(1);
 		goto retry;
@@ -975,7 +985,7 @@ static void iommu_print_event(struct amd_iommu *iommu, void *__evt)
 
 	if (type == EVENT_TYPE_IO_FAULT) {
 		amd_iommu_report_page_fault(iommu, devid, pasid, address, flags, vflags);
-		return;
+		return 1;
 	}
 
 	switch (type) {
@@ -1023,9 +1033,90 @@ static void iommu_print_event(struct amd_iommu *iommu, void *__evt)
 	case EVENT_TYPE_INV_PPR_REQ:
 		amd_iommu_report_ppr_err(iommu, event, devid, address, flags);
 		break;
+	case EVENT_TYPE_GUEST_EVENT_FAULT:
+	{
+		u16 gid = FIELD_GET(EVENT_GID_MASK, event[1]);
+		u32 next = (head + EVTLOG_ENTRY_SIZE) % amd_iommu_evtlog_size;
+		u32 *guest;
+		u32 live_tail, w0, w1, w2, w3;
+		int guest_type, wait;
+
+		dev_err_ratelimited(dev, "Event logged [GUEST_EVENT_FAULT gid=%#x flags=0x%04x vflags=%#x]\n",
+				    gid, flags, vflags);
+
+		/*
+		 * GUEST_EVENT_FAULT is paired with the original guest
+		 * event in the next log slot (possibly wrapped). The
+		 * poll-time tail can predate that slot. Wait until
+		 * occupancy is at least two entries, then retry a
+		 * type-0 pair slot the same way erratum 732 waits
+		 * for DMA visibility. Return 2 only when that slot
+		 * is already in [head, live_tail). iommu_poll_events()

drivers/iommu/amd/iommu.c static void iommu_print_event(struct amd_iommu *iommu, void *__evt) · line 1064

Quoted 10 lines
drivers/iommu/amd/iommu.c +10 -0
+		 * re-reads tail each iteration so head cannot skip
+		 * past tail.
+		 */
+		wait = 0;
+		for (;;) {
+			live_tail = readl(iommu->mmio_base + MMIO_EVT_TAIL_OFFSET);
+			if (evtlog_used(head, live_tail) >=
+			    2 * EVTLOG_ENTRY_SIZE)
+				break;
+			if (++wait == LOOP_TIMEOUT) {

I think we should avoid avoid this loop in interrupt handler path.

Quoted 71 lines
drivers/iommu/amd/iommu.c +56 -2
+				dev_err_ratelimited(dev, "Event logged [GUEST_EVENT_FAULT invalid guest-event (does not exist)]\n");
+				if (!amd_iommu_snp_en)
+					memset(__evt, 0, EVTLOG_ENTRY_SIZE);
+				return 1;
+			}
+			udelay(1);
+		}
+
+		guest = (u32 *)(iommu->evt_buf + next);
+		wait = 0;
+		for (;;) {
+			w1 = READ_ONCE(guest[1]);
+			guest_type = (w1 >> EVENT_TYPE_SHIFT) & EVENT_TYPE_MASK;
+			if (guest_type)
+				break;
+			if (++wait == LOOP_TIMEOUT) {
+				w0 = READ_ONCE(guest[0]);
+				w2 = READ_ONCE(guest[2]);
+				w3 = READ_ONCE(guest[3]);
+				dev_err_ratelimited(dev, "Event logged [GUEST_EVENT_FAULT invalid guest-event %08x %08x %08x %08x]\n",
+						    w0, w1, w2, w3);
+				if (!amd_iommu_snp_en) {
+					memset(__evt, 0, EVTLOG_ENTRY_SIZE);
+					memset(guest, 0, EVTLOG_ENTRY_SIZE);
+				}
+				/* Occupancy already includes this slot.
+				 * Skip it so a late DMA fill is not
+				 * decoded as a host event.
+				 */
+				return 2;
+			}
+			udelay(1);
+		}
+
+		w0 = READ_ONCE(guest[0]);
+		w2 = READ_ONCE(guest[2]);
+		w3 = READ_ONCE(guest[3]);
+		dev_err_ratelimited(dev, "Event logged [GUEST_EVENT_FAULT guest-event %08x %08x %08x %08x]\n",
+				    w0, w1, w2, w3);
+
+		/*
+		 * Erratum 732: clear both pair slots so a stale guest
+		 * event is not decoded on the next poll. The second
+		 * slot may wrap to the start of the ring. Skip on SNP;
+		 * that buffer is not writable.
+		 */
+		if (!amd_iommu_snp_en) {
+			memset(__evt, 0, EVTLOG_ENTRY_SIZE);
+			memset(guest, 0, EVTLOG_ENTRY_SIZE);
+		}
+		return 2;
+	}
 	case EVENT_TYPE_VIOMMU_HW_ERR:
 	{
-		u16 gid = event[0] & 0xFFFF;
+		u16 gid = FIELD_GET(EVENT_GID_MASK, event[0]);
 		u8 src = (event[0] >> 16) & 0x3;
 
 		dev_err(dev, "Event logged [VIOMMU_HW_ERR gid=%#x address=%#llx src=%#x flags=0x%04x vflags=%#x]\n",
@@ -1044,24 +1135,27 @@ static void iommu_print_event(struct amd_iommu *iommu, void *__evt)
 	 * SNP enabled system.
 	 */
 	if (!amd_iommu_snp_en)
-		memset(__evt, 0, 4 * sizeof(u32));
+		memset(__evt, 0, EVTLOG_ENTRY_SIZE);
+
+	return 1;
 }
 
 static void iommu_poll_events(struct amd_iommu *iommu)
 {

drivers/iommu/amd/iommu.c static void iommu_print_event(struct amd_iommu *iommu, void *__evt) · line 1150

Quoted 10 lines
drivers/iommu/amd/iommu.c +3 -4
 	u32 head, tail;
+	unsigned int n;
 
 	head = readl(iommu->mmio_base + MMIO_EVT_HEAD_OFFSET);
-	tail = readl(iommu->mmio_base + MMIO_EVT_TAIL_OFFSET);
-
-	while (head != tail) {
-		iommu_print_event(iommu, iommu->evt_buf + head);
+	for (;;) {
+		tail = readl(iommu->mmio_base + MMIO_EVT_TAIL_OFFSET);

We should read tail outside the loop. Otherwise we may stuck in interrupt handler context for long time and cause softlockups.

-Vasant

Quoted 13 lines
drivers/iommu/amd/iommu.c +4 -2
+		if (head == tail)
+			break;
+		n = iommu_print_event(iommu, head);
 
 		/* Update head pointer of hardware ring-buffer */
-		head = (head + EVTLOG_ENTRY_SIZE) % amd_iommu_evtlog_size;
+		head = (head + n * EVTLOG_ENTRY_SIZE) % amd_iommu_evtlog_size;
 		writel(head, iommu->mmio_base + MMIO_EVT_HEAD_OFFSET);
 	}
-
 }
 
 #ifdef CONFIG_IRQ_REMAP
author reviewer
#4 lore raw

Re: [PATCH v6 02/24] iommu/amd: Introduce EVENT_TYPE_GUEST_EVENT_FAULT

Vasant,

Quoted 30 lines · Vasant Hegde
Quoted 18 lines · Suravee Suthikulpanit

INSERT_GUEST_EVENT with non-zero reserved bits logs GUEST_EVENT_FAULT followed by the original guest event. Consume both event-log slots, wrapping via evt_buf+(head+EVTLOG_ENTRY_SIZE)%size, and dump the guest payload raw so it is not decoded as a host event.

Wait until occupancy is at least two entries, not merely until live tail is not the pair offset. Re-read the event-log tail each poll iteration so a return of 2 cannot advance head past a stale cached tail and walk empty slots with the type-0 erratum retry.

Retry a type-0 guest slot up to LOOP_TIMEOUT, the same way erratum 732 waits for the primary slot. Return 2 only when that slot is already in [head, live_tail). Return 1 if the pair never appears in tail. Rate-limit the guest-triggered logs.

Quoted 3 lines

Log an invalid guest-event when the pair slot is still missing or type 0 after that wait, instead of stalling the event log.

As per spec:
GUEST_EVENT_FAULT event is always written in pairs with a guest event entry
immediately after it into the event log.

Quoted 5 lines

Why not add one additional check in iommu_poll_events() such that:> if event at head is GUEST_EVENT_FAULT and actual fault event is
not-yet-written by HW just return.
If subsequent event is also written then handle the event.

and increase head pointer accordingly.

I would like to keep the code for handling GUEST_EVENT_FAULT (including the logic for checking guest event pair entry) in iommu_print_event() to simplify the code readability.

Originally, I try to handle the case where the guest event pair may arrive late, but that adds complexity to the tail polling logic. For v7, I can simplify the logic to just report GUEST_EVENT_FAULT event with missing guest eventinstead then.

Quoted 25 lines
Quoted 25 lines

....

drivers/iommu/amd/iommu.c +11 -1
index 43a5690721af..2df81e29519e 100644
@@ -942,8 +942,18 @@ static void amd_iommu_report_ppr_err(struct amd_iommu *iommu, volatile u32 *even
  	pci_dev_put(pdev);
  }
 
-static void iommu_print_event(struct amd_iommu *iommu, void *__evt)
+static u32 evtlog_used(u32 head, u32 tail)
  {
+	return (tail + amd_iommu_evtlog_size - head) % amd_iommu_evtlog_size;
+}
+
+/*
+ * Decode one event-log slot (or a GUEST_EVENT_FAULT pair).
+ * Returns the number of slots consumed.
+ */
+static unsigned int iommu_print_event(struct amd_iommu *iommu, u32 head)
+{
+	void *__evt = iommu->evt_buf + head;
  	struct device *dev = iommu->iommu.dev;
  	int type, devid, flags;
  	volatile u32 *event = __evt;
Quoted 8 lines

drivers/iommu/amd/iommu.c static void iommu_print_event(struct amd_iommu *iommu, void *__evt) · line 978

Quoted 6 lines
drivers/iommu/amd/iommu.c +1 -1
@@ -965,7 +975,7 @@ static void iommu_print_event(struct amd_iommu *iommu, void *__evt)
  		/* Did we hit the erratum? */
  		if (++count == LOOP_TIMEOUT) {
  			pr_err("No event written to event log\n");
-			return;
+			return 1;

This will cause head to progress event though there is no event.

Actually, there is an event (since the head != tail) but with (type == 0 : invalid event). So, we need to discard the entry and increment head.

Quoted 39 lines
Quoted 39 lines
drivers/iommu/amd/iommu.c +24 -1
  		}
  		udelay(1);
  		goto retry;
@@ -975,7 +985,7 @@ static void iommu_print_event(struct amd_iommu *iommu, void *__evt)
 
  	if (type == EVENT_TYPE_IO_FAULT) {
  		amd_iommu_report_page_fault(iommu, devid, pasid, address, flags, vflags);
-		return;
+		return 1;
  	}
 
  	switch (type) {
@@ -1023,9 +1033,90 @@ static void iommu_print_event(struct amd_iommu *iommu, void *__evt)
  	case EVENT_TYPE_INV_PPR_REQ:
  		amd_iommu_report_ppr_err(iommu, event, devid, address, flags);
  		break;
+	case EVENT_TYPE_GUEST_EVENT_FAULT:
+	{
+		u16 gid = FIELD_GET(EVENT_GID_MASK, event[1]);
+		u32 next = (head + EVTLOG_ENTRY_SIZE) % amd_iommu_evtlog_size;
+		u32 *guest;
+		u32 live_tail, w0, w1, w2, w3;
+		int guest_type, wait;
+
+		dev_err_ratelimited(dev, "Event logged [GUEST_EVENT_FAULT gid=%#x flags=0x%04x vflags=%#x]\n",
+				    gid, flags, vflags);
+
+		/*
+		 * GUEST_EVENT_FAULT is paired with the original guest
+		 * event in the next log slot (possibly wrapped). The
+		 * poll-time tail can predate that slot. Wait until
+		 * occupancy is at least two entries, then retry a
+		 * type-0 pair slot the same way erratum 732 waits
+		 * for DMA visibility. Return 2 only when that slot
+		 * is already in [head, live_tail). iommu_poll_events()
+		 * re-reads tail each iteration so head cannot skip
+		 * past tail.
+		 */
+		wait = 0;
Quoted 8 lines

drivers/iommu/amd/iommu.c static void iommu_print_event(struct amd_iommu *iommu, void *__evt) · line 1064

Quoted 6 lines
drivers/iommu/amd/iommu.c +6 -0
+		for (;;) {
+			live_tail = readl(iommu->mmio_base + MMIO_EVT_TAIL_OFFSET);
+			if (evtlog_used(head, live_tail) >=
+			    2 * EVTLOG_ENTRY_SIZE)
+				break;
+			if (++wait == LOOP_TIMEOUT) {

I think we should avoid avoid this loop in interrupt handler path.

I am removing this loop.

Quoted 74 lines
Quoted 74 lines
drivers/iommu/amd/iommu.c +57 -2
+				dev_err_ratelimited(dev, "Event logged [GUEST_EVENT_FAULT invalid guest-event (does not exist)]\n");
+				if (!amd_iommu_snp_en)
+					memset(__evt, 0, EVTLOG_ENTRY_SIZE);
+				return 1;
+			}
+			udelay(1);
+		}
+
+		guest = (u32 *)(iommu->evt_buf + next);
+		wait = 0;
+		for (;;) {
+			w1 = READ_ONCE(guest[1]);
+			guest_type = (w1 >> EVENT_TYPE_SHIFT) & EVENT_TYPE_MASK;
+			if (guest_type)
+				break;
+			if (++wait == LOOP_TIMEOUT) {
+				w0 = READ_ONCE(guest[0]);
+				w2 = READ_ONCE(guest[2]);
+				w3 = READ_ONCE(guest[3]);
+				dev_err_ratelimited(dev, "Event logged [GUEST_EVENT_FAULT invalid guest-event %08x %08x %08x %08x]\n",
+						    w0, w1, w2, w3);
+				if (!amd_iommu_snp_en) {
+					memset(__evt, 0, EVTLOG_ENTRY_SIZE);
+					memset(guest, 0, EVTLOG_ENTRY_SIZE);
+				}
+				/* Occupancy already includes this slot.
+				 * Skip it so a late DMA fill is not
+				 * decoded as a host event.
+				 */
+				return 2;
+			}
+			udelay(1);
+		}
+
+		w0 = READ_ONCE(guest[0]);
+		w2 = READ_ONCE(guest[2]);
+		w3 = READ_ONCE(guest[3]);
+		dev_err_ratelimited(dev, "Event logged [GUEST_EVENT_FAULT guest-event %08x %08x %08x %08x]\n",
+				    w0, w1, w2, w3);
+
+		/*
+		 * Erratum 732: clear both pair slots so a stale guest
+		 * event is not decoded on the next poll. The second
+		 * slot may wrap to the start of the ring. Skip on SNP;
+		 * that buffer is not writable.
+		 */
+		if (!amd_iommu_snp_en) {
+			memset(__evt, 0, EVTLOG_ENTRY_SIZE);
+			memset(guest, 0, EVTLOG_ENTRY_SIZE);
+		}
+		return 2;
+	}
  	case EVENT_TYPE_VIOMMU_HW_ERR:
  	{
-		u16 gid = event[0] & 0xFFFF;
+		u16 gid = FIELD_GET(EVENT_GID_MASK, event[0]);
  		u8 src = (event[0] >> 16) & 0x3;
 
  		dev_err(dev, "Event logged [VIOMMU_HW_ERR gid=%#x address=%#llx src=%#x flags=0x%04x vflags=%#x]\n",
@@ -1044,24 +1135,27 @@ static void iommu_print_event(struct amd_iommu *iommu, void *__evt)
  	 * SNP enabled system.
  	 */
  	if (!amd_iommu_snp_en)
-		memset(__evt, 0, 4 * sizeof(u32));
+		memset(__evt, 0, EVTLOG_ENTRY_SIZE);
+
+	return 1;
  }
 
  static void iommu_poll_events(struct amd_iommu *iommu)
  {
  	u32 head, tail;
+	unsigned int n;
 
Quoted 10 lines

drivers/iommu/amd/iommu.c static void iommu_print_event(struct amd_iommu *iommu, void *__evt) · line 1150

Quoted 7 lines
drivers/iommu/amd/iommu.c +2 -4
  	head = readl(iommu->mmio_base + MMIO_EVT_HEAD_OFFSET);
-	tail = readl(iommu->mmio_base + MMIO_EVT_TAIL_OFFSET);
-
-	while (head != tail) {
-		iommu_print_event(iommu, iommu->evt_buf + head);
+	for (;;) {
+		tail = readl(iommu->mmio_base + MMIO_EVT_TAIL_OFFSET);

We should read tail outside the loop. Otherwise we may stuck in interrupt handler context for long time and cause softlockups.

I am simplifying this logic.

Thanks,
Suravee

author reviewer
#5 lore raw

PATCH v6 3/24 iommu/amd: Detect and initialize AMD vIOMMU feature

In reply to Suravee Suthikulpanit

The feature is advertised w/ EFR[VIOMMUSup]. Please see the AMD IOMMU specification[1] for more detail.

Gate amd_viommu_init() on this IOMMU's EFR[VIOMMUSup] (iommu->features) at the PCI init call site. Do not use check_feature()/amd_iommu_efr; that global mask is the AND of every IOMMU. Do not fail other IOMMUs when one instance lacks the bit or init fails.

Set AMD_IOMMU_FLAG_VIOMMU_EN when amd_viommu_init() succeeds. Log PCI names of IOMMUs that have that flag set.

Forward-declare struct amd_iommu in amd_viommu.h so the header is self-contained.

When CONFIG_AMD_IOMMU_IOMMUFD is off, the amd_viommu_init() stub returns 0 so a missing build is not logged as an init failure on EFR[VIOMMUSup] hardware.

[1] https://docs.amd.com/v/u/en-US/48882_3.11_IOMMU_PUB

---
 drivers/iommu/amd/Makefile          |  2 +-
 drivers/iommu/amd/amd_iommu.h       |  5 +++++
 drivers/iommu/amd/amd_iommu_types.h |  2 ++
 drivers/iommu/amd/amd_viommu.h      | 29 ++++++++++++++++++++++++
 drivers/iommu/amd/init.c            | 34 +++++++++++++++++++++++++++++
 drivers/iommu/amd/viommu.c          | 25 +++++++++++++++++++++
 6 files changed, 96 insertions(+), 1 deletion(-)
 create mode 100644 drivers/iommu/amd/amd_viommu.h
 create mode 100644 drivers/iommu/amd/viommu.c
drivers/iommu/amd/Makefile +1 -1
index 94b8ef2acb18..e1e824b9c7b0 100644
@@ -1,4 +1,4 @@
 # SPDX-License-Identifier: GPL-2.0-only
 obj-y += iommu.o init.o quirks.o ppr.o pasid.o
-obj-$(CONFIG_AMD_IOMMU_IOMMUFD) += iommufd.o nested.o
+obj-$(CONFIG_AMD_IOMMU_IOMMUFD) += iommufd.o nested.o viommu.o
 obj-$(CONFIG_AMD_IOMMU_DEBUGFS) += debugfs.o
drivers/iommu/amd/amd_iommu.h +5 -0
index 71113e860859..b2ae17e3df34 100644
@@ -112,6 +112,11 @@ static inline bool check_feature(u64 mask)
 	return (amd_iommu_efr & mask);
 }
 
+static inline bool amd_iommu_viommu_enabled(struct amd_iommu *iommu)
+{
+	return iommu && (iommu->flags & AMD_IOMMU_FLAG_VIOMMU_EN);
+}
+
 static inline bool check_feature2(u64 mask)
 {
 	return (amd_iommu_efr2 & mask);
drivers/iommu/amd/amd_iommu_types.h +2 -0
index 704b4c8acdc5..688cd9240790 100644
@@ -91,6 +91,7 @@
 #define FEATURE_HASUP		BIT_ULL(49)
 #define FEATURE_EPHSUP		BIT_ULL(50)
 #define FEATURE_HDSUP		BIT_ULL(52)
+#define FEATURE_VIOMMU		BIT_ULL(55)
 #define FEATURE_SNP		BIT_ULL(63)
 
 
@@ -465,6 +466,7 @@ struct irq_domain;
 struct amd_irte_ops;
 
 #define AMD_IOMMU_FLAG_TRANS_PRE_ENABLED      (1 << 0)
+#define AMD_IOMMU_FLAG_VIOMMU_EN              (1 << 1)
 
 struct gcr3_tbl_info {
 	u64	*gcr3_tbl;	/* Guest CR3 table */
drivers/iommu/amd/amd_viommu.h new file +29 -0
new file mode 100644
index 000000000000..864e7b283cff
@@ -0,0 +1,29 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+/*
+ * Copyright (C) 2026 Advanced Micro Devices, Inc.
+ */
+
+#ifndef AMD_VIOMMU_H
+#define AMD_VIOMMU_H
+
+struct amd_iommu;
+
+#if IS_ENABLED(CONFIG_AMD_IOMMU_IOMMUFD)
+
+int amd_viommu_init(struct amd_iommu *iommu);
+
+#else
+
+/*
+ * CONFIG_AMD_IOMMU_IOMMUFD is off: host vIOMMU is not compiled in.
+ * Succeed as a no-op so iommu_init_pci() does not pr_err on
+ * EFR[VIOMMUSup] hardware.
+ */
+static inline int amd_viommu_init(struct amd_iommu *iommu)
+{
+	return 0;
+}
+
+#endif /* CONFIG_AMD_IOMMU_IOMMUFD */
+
+#endif /* AMD_VIOMMU_H */
drivers/iommu/amd/init.c +34 -0
index 8a410d4aa370..c80872ce0b60 100644
@@ -34,6 +34,7 @@
 #include <linux/crash_dump.h>
 
 #include "amd_iommu.h"
+#include "amd_viommu.h"
 #include "../irq_remapping.h"
 #include "../iommu-pages.h"
 
@@ -2196,6 +2197,19 @@ static int __init iommu_init_pci(struct amd_iommu *iommu)
 	if (check_feature(FEATURE_PPR) && amd_iommu_alloc_ppr_log(iommu))
 		return -ENOMEM;
 
+	/*
+	 * vIOMMU is advertised in this IOMMU's EFR[VIOMMUSup]
+	 * (iommu->features). Do not use check_feature()/amd_iommu_efr;
+	 * that global mask is the AND of every IOMMU and can hide the
+	 * bit on mixed systems.
+	 */
+	if (iommu->features & FEATURE_VIOMMU) {
+		ret = amd_viommu_init(iommu);
+		if (ret)
+			pr_err("Failed to initialize vIOMMU on %s\n",
+			       pci_name(iommu->dev));
+	}
+
 	if (iommu->cap & (1UL << IOMMU_CAP_NPCACHE)) {
 		pr_info("Using strict mode due to virtualization\n");
 		iommu_set_dma_strict();
@@ -2264,6 +2278,25 @@ static int __init iommu_init_pci(struct amd_iommu *iommu)
 	return pci_enable_device(iommu->dev);
 }
 
+static void print_viommu_info(void)
+{
+	struct amd_iommu *iommu;
+	const char *sep = "";
+
+	for_each_iommu(iommu) {
+		if (!amd_iommu_viommu_enabled(iommu))
+			continue;
+		if (!sep[0])
+			pr_info("vIOMMU enabled: ");
+		else
+			pr_cont("%s", sep);
+		pr_cont("%s", pci_name(iommu->dev));
+		sep = ", ";
+	}
+	if (sep[0])
+		pr_cont("\n");
+}
+
 static void print_iommu_info(void)
 {
 	int i;
@@ -2301,6 +2334,7 @@ static void print_iommu_info(void)
 		pr_info("V2 page table enabled (Paging mode : %d level)\n",
 			amd_iommu_gpt_level);
 	}
+	print_viommu_info();
 }
 
 static int __init amd_iommu_init_pci(void)
drivers/iommu/amd/viommu.c new file +25 -0
new file mode 100644
index 000000000000..53f3302f7cc6
@@ -0,0 +1,25 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/*
+ * Copyright (C) 2026 Advanced Micro Devices, Inc.
+ */
+
+#define pr_fmt(fmt)     "AMD-Vi: " fmt
+#define dev_fmt(fmt)    pr_fmt(fmt)
+
+#include <linux/iommu.h>
+#include <linux/iommufd.h>
+#include <uapi/linux/iommufd.h>
+
+#include <asm/iommu.h>
+#include <asm/set_memory.h>
+
+#include "iommufd.h"
+#include "amd_iommu.h"
+#include "amd_iommu_types.h"
+#include "amd_viommu.h"
+
+int __init amd_viommu_init(struct amd_iommu *iommu)
+{
+	iommu->flags |= AMD_IOMMU_FLAG_VIOMMU_EN;
+	return 0;
+}
Signature · 1 line
2.34.1
maintainer
#6 lore raw

Re: [PATCH v6 03/24] iommu/amd: Detect and initialize AMD vIOMMU feature

Quoted 93 lines · Suravee Suthikulpanit

The feature is advertised w/ EFR[VIOMMUSup]. Please see the AMD IOMMU specification[1] for more detail.

Gate amd_viommu_init() on this IOMMU's EFR[VIOMMUSup] (iommu->features) at the PCI init call site. Do not use check_feature()/amd_iommu_efr; that global mask is the AND of every IOMMU. Do not fail other IOMMUs when one instance lacks the bit or init fails.

Set AMD_IOMMU_FLAG_VIOMMU_EN when amd_viommu_init() succeeds. Log PCI names of IOMMUs that have that flag set.

Forward-declare struct amd_iommu in amd_viommu.h so the header is self-contained.

When CONFIG_AMD_IOMMU_IOMMUFD is off, the amd_viommu_init() stub returns 0 so a missing build is not logged as an init failure on EFR[VIOMMUSup] hardware.

[1] https://docs.amd.com/v/u/en-US/48882_3.11_IOMMU_PUB

---
 drivers/iommu/amd/Makefile          |  2 +-
 drivers/iommu/amd/amd_iommu.h       |  5 +++++
 drivers/iommu/amd/amd_iommu_types.h |  2 ++
 drivers/iommu/amd/amd_viommu.h      | 29 ++++++++++++++++++++++++
 drivers/iommu/amd/init.c            | 34 +++++++++++++++++++++++++++++
 drivers/iommu/amd/viommu.c          | 25 +++++++++++++++++++++
 6 files changed, 96 insertions(+), 1 deletion(-)
 create mode 100644 drivers/iommu/amd/amd_viommu.h
 create mode 100644 drivers/iommu/amd/viommu.c
drivers/iommu/amd/Makefile +1 -1
index 94b8ef2acb18..e1e824b9c7b0 100644
@@ -1,4 +1,4 @@
 # SPDX-License-Identifier: GPL-2.0-only
 obj-y += iommu.o init.o quirks.o ppr.o pasid.o
-obj-$(CONFIG_AMD_IOMMU_IOMMUFD) += iommufd.o nested.o
+obj-$(CONFIG_AMD_IOMMU_IOMMUFD) += iommufd.o nested.o viommu.o
 obj-$(CONFIG_AMD_IOMMU_DEBUGFS) += debugfs.o
drivers/iommu/amd/amd_iommu.h +5 -0
index 71113e860859..b2ae17e3df34 100644
@@ -112,6 +112,11 @@ static inline bool check_feature(u64 mask)
 	return (amd_iommu_efr & mask);
 }
 
+static inline bool amd_iommu_viommu_enabled(struct amd_iommu *iommu)
+{
+	return iommu && (iommu->flags & AMD_IOMMU_FLAG_VIOMMU_EN);
+}
+
 static inline bool check_feature2(u64 mask)
 {
 	return (amd_iommu_efr2 & mask);
drivers/iommu/amd/amd_iommu_types.h +2 -0
index 704b4c8acdc5..688cd9240790 100644
@@ -91,6 +91,7 @@
 #define FEATURE_HASUP		BIT_ULL(49)
 #define FEATURE_EPHSUP		BIT_ULL(50)
 #define FEATURE_HDSUP		BIT_ULL(52)
+#define FEATURE_VIOMMU		BIT_ULL(55)
 #define FEATURE_SNP		BIT_ULL(63)
 
 
@@ -465,6 +466,7 @@ struct irq_domain;
 struct amd_irte_ops;
 
 #define AMD_IOMMU_FLAG_TRANS_PRE_ENABLED      (1 << 0)
+#define AMD_IOMMU_FLAG_VIOMMU_EN              (1 << 1)
 
 struct gcr3_tbl_info {
 	u64	*gcr3_tbl;	/* Guest CR3 table */
drivers/iommu/amd/amd_viommu.h new file +8 -0
new file mode 100644
index 000000000000..864e7b283cff
@@ -0,0 +1,29 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+/*
+ * Copyright (C) 2026 Advanced Micro Devices, Inc.
+ */
+
+#ifndef AMD_VIOMMU_H
+#define AMD_VIOMMU_H
+

drivers/iommu/amd/amd_viommu.h line 18

Quoted 10 lines
drivers/iommu/amd/amd_viommu.h new file +10 -0
+struct amd_iommu;
+
+#if IS_ENABLED(CONFIG_AMD_IOMMU_IOMMUFD)
+
+int amd_viommu_init(struct amd_iommu *iommu);
+
+#else
+
+/*
+ * CONFIG_AMD_IOMMU_IOMMUFD is off: host vIOMMU is not compiled in.

Nit: This is redundant statement. No need to have above line.

Quoted 24 lines
drivers/iommu/amd/amd_viommu.h new file +11 -0
+ * Succeed as a no-op so iommu_init_pci() does not pr_err on
+ * EFR[VIOMMUSup] hardware.
+ */
+static inline int amd_viommu_init(struct amd_iommu *iommu)
+{
+	return 0;
+}
+
+#endif /* CONFIG_AMD_IOMMU_IOMMUFD */
+
+#endif /* AMD_VIOMMU_H */
drivers/iommu/amd/init.c +1 -0
index 8a410d4aa370..c80872ce0b60 100644
@@ -34,6 +34,7 @@
 #include <linux/crash_dump.h>
 
 #include "amd_iommu.h"
+#include "amd_viommu.h"
 #include "../irq_remapping.h"
 #include "../iommu-pages.h"
 

drivers/iommu/amd/init.c static int __init iommu_init_pci(struct amd_iommu *iommu) · line 2205

Quoted 9 lines
drivers/iommu/amd/init.c +6 -0
@@ -2196,6 +2197,19 @@ static int __init iommu_init_pci(struct amd_iommu *iommu)
 	if (check_feature(FEATURE_PPR) && amd_iommu_alloc_ppr_log(iommu))
 		return -ENOMEM;
 
+	/*
+	 * vIOMMU is advertised in this IOMMU's EFR[VIOMMUSup]
+	 * (iommu->features). Do not use check_feature()/amd_iommu_efr;
+	 * that global mask is the AND of every IOMMU and can hide the
+	 * bit on mixed systems.
+	 */> +	if (iommu->features & FEATURE_VIOMMU) {

Please add a additional check to skip vIOMMU if amd_iommu_snp_en is true. Support w/ SNP needs PSP plumbing and not part of this series.

-Vasant

author reviewer
#7 lore raw

PATCH v6 4/24 iommu/amd: Introduce IOMMUFD vIOMMU support for AMD

In reply to Suravee Suthikulpanit

Introduce a new enum iommu_viommu_type (IOMMU_VIOMMU_TYPE_AMD) for AMD vIOMMU along with the struct iommu_viommu_amd, which is used to initialize IOMMUFD vIOMMU instance when calling struct iommu_ops.viommu_init().

Also, hook up struct iomufd_viomu_ops.alloc_domain_nested to connect nested domain allocation with AMD vIOMMU implementation.

Only report a non-zero viommu size when the type is AMD and amd_iommu_viommu_enabled() is true for the device's IOMMU.

Additional initialization will be added in subsequent patches.

---
 drivers/iommu/amd/iommufd.c  | 10 ++++++++++
 include/uapi/linux/iommufd.h | 10 ++++++++++
 2 files changed, 20 insertions(+)
drivers/iommu/amd/iommufd.c +10 -0
index 52300b867c1f..e8e7807f2a33 100644
@@ -34,6 +34,15 @@ void *amd_iommufd_hw_info(struct device *dev, u32 *length, enum iommu_hw_info_ty
 
 size_t amd_iommufd_get_viommu_size(struct device *dev, enum iommu_viommu_type viommu_type)
 {
+	struct amd_iommu *iommu;
+
+	if (viommu_type != IOMMU_VIOMMU_TYPE_AMD)
+		return 0;
+
+	iommu = get_amd_iommu_from_dev(dev);
+	if (!amd_iommu_viommu_enabled(iommu))
+		return 0;
+
 	return VIOMMU_STRUCT_SIZE(struct amd_iommu_viommu, core);
 }
 
@@ -73,5 +82,6 @@ static void amd_iommufd_viommu_destroy(struct iommufd_viommu *viommu)
  * struct iommufd_viommu_ops - vIOMMU specific operations
  */
 static const struct iommufd_viommu_ops amd_viommu_ops = {
+	.alloc_domain_nested = amd_iommu_alloc_domain_nested,
 	.destroy = amd_iommufd_viommu_destroy,
 };
include/uapi/linux/iommufd.h +10 -0
index 206fa667c782..e1080b997e8f 100644
@@ -1095,6 +1095,7 @@ struct iommu_fault_alloc {
  * @IOMMU_VIOMMU_TYPE_ARM_SMMUV3: ARM SMMUv3 driver specific type
  * @IOMMU_VIOMMU_TYPE_TEGRA241_CMDQV: NVIDIA Tegra241 CMDQV (extension for ARM
  *                                    SMMUv3) enabled ARM SMMUv3 type
+ * @IOMMU_VIOMMU_TYPE_AMD: AMD HW-vIOMMU type
  */
 enum iommu_viommu_type {
 	IOMMU_VIOMMU_TYPE_DEFAULT = 0,
@@ -1105,6 +1106,7 @@ enum iommu_viommu_type {
 	 *   VMM must wire the HYP_OWN bit to 0 in guest VINTF_CONFIG register
 	 */
 	IOMMU_VIOMMU_TYPE_TEGRA241_CMDQV = 2,
+	IOMMU_VIOMMU_TYPE_AMD = 3,
 };
 
 /**
@@ -1123,6 +1125,14 @@ struct iommu_viommu_tegra241_cmdqv {
 	__aligned_u64 out_vintf_mmap_length;
 };
 
+/**
+ * struct iommu_viommu_amd - AMD vIOMMU Interface (IOMMU_VIOMMU_TYPE_AMD)
+ * @out_vfmmio_mmap_offset: (out) mmap offset for vIOMMU VF-MMIO
+ */
+struct iommu_viommu_amd {
+	__aligned_u64 out_vfmmio_mmap_offset;
+};
+
 /**
  * struct iommu_viommu_alloc - ioctl(IOMMU_VIOMMU_ALLOC)
  * @size: sizeof(struct iommu_viommu_alloc)
Signature · 1 line
2.34.1
Reviewed-by on v6 4/24
#8 lore raw

Re: [PATCH v6 04/24] iommu/amd: Introduce IOMMUFD vIOMMU support for AMD

Quoted 7 lines · Suravee Suthikulpanit

Introduce a new enum iommu_viommu_type (IOMMU_VIOMMU_TYPE_AMD) for AMD vIOMMU along with the struct iommu_viommu_amd, which is used to initialize IOMMUFD vIOMMU instance when calling struct iommu_ops.viommu_init().

Also, hook up struct iomufd_viomu_ops.alloc_domain_nested to connect nested domain allocation with AMD vIOMMU implementation.

Quoted 7 lines

Only report a non-zero viommu size when the type is AMD and amd_iommu_viommu_enabled() is true for the device's IOMMU.

Additional initialization will be added in subsequent patches.

author reviewer
#9 lore raw

PATCH v6 5/24 iommu/amd: Allocate Guest IDs for IOMMUFD vIOMMU instances

In reply to Suravee Suthikulpanit

Hardware vIOMMU uses a 16-bit Guest ID (GID) per guest IOMMU to index driver and hardware state. Allocate one GID per IOMMUFD vIOMMU from a per-amd_iommu IDA (unique within that IOMMU; a VM behind multiple IOMMUs may hold more than one GID).

Add amd_iommu_gid_alloc() and amd_iommu_gid_free(), store the ID in amd_iommu_viommu::gid, and call them from amd_iommufd_viommu_init() and destroy. Initialize gid_ida in init_iommu_one() and destroy it in free_iommu_one() with the IOMMU object.

Reject amd_iommufd_viommu_init() with -EOPNOTSUPP when the IOMMU does not have AMD_IOMMU_FLAG_VIOMMU_EN.

---
 drivers/iommu/amd/amd_iommu.h       |  4 ++++
 drivers/iommu/amd/amd_iommu_types.h |  7 +++++++
 drivers/iommu/amd/init.c            |  2 ++
 drivers/iommu/amd/iommu.c           | 16 ++++++++++++++++
 drivers/iommu/amd/iommufd.c         | 14 ++++++++++++++
 5 files changed, 43 insertions(+)
drivers/iommu/amd/amd_iommu.h +4 -0
index b2ae17e3df34..f6cf412a9820 100644
@@ -227,4 +227,8 @@ amd_iommu_make_clear_dte(struct iommu_dev_data *dev_data, struct dev_table_entry
 struct iommu_domain *
 amd_iommu_alloc_domain_nested(struct iommufd_viommu *viommu, u32 flags,
 			      const struct iommu_user_data *user_data);
+
+/* Guest ID for vIOMMU */
+int amd_iommu_gid_alloc(struct amd_iommu *iommu);
+void amd_iommu_gid_free(struct amd_iommu *iommu, int gid);
 #endif /* AMD_IOMMU_H */
drivers/iommu/amd/amd_iommu_types.h +7 -0
index 688cd9240790..8c38775ff5c1 100644
@@ -21,6 +21,7 @@
 #include <linux/iommufd.h>
 #include <linux/irqreturn.h>
 #include <linux/generic_pt/iommu.h>
+#include <linux/idr.h>
 
 #include <uapi/linux/iommufd.h>
 
@@ -404,6 +405,9 @@
 
 #define MAX_DOMAIN_ID 65536
 
+/* Guest ID bits 0-14; bit 15 is reserved for secure vIOMMU. */
+#define VIOMMU_MAX_GID		0x7FFF
+
 /* Timeout stuff */
 #define LOOP_TIMEOUT		100000
 #define MMIO_STATUS_TIMEOUT	2000000
@@ -501,6 +505,7 @@ struct amd_iommu_viommu {
 	struct iommufd_viommu core;
 	struct protection_domain *parent; /* nest parent domain for this viommu */
 	struct list_head pdom_list;	  /* For protection_domain->viommu_list */
+	u16 gid;			  /* Guest ID for the vIOMMU */
 
 	/*
 	 * Per-vIOMMU guest domain ID to host domain ID mapping.
@@ -760,6 +765,8 @@ struct amd_iommu {
 	/* IOPF support */
 	struct iopf_queue *iopf_queue;
 	unsigned char iopfq_name[32];
+
+	struct ida gid_ida;		 /* guest IDs for this IOMMU */
 };
 
 static inline struct amd_iommu *dev_to_amd_iommu(struct device *dev)
drivers/iommu/amd/init.c +2 -0
index c80872ce0b60..7b4cb473019d 100644
@@ -1797,6 +1797,7 @@ static void __init free_iommu_one(struct amd_iommu *iommu)
 	free_ga_log(iommu);
 	iommu_unmap_mmio_space(iommu);
 	amd_iommu_iopf_uninit(iommu);
+	ida_destroy(&iommu->gid_ida);
 }
 
 static void __init free_iommu_all(void)
@@ -1885,6 +1886,7 @@ static int __init init_iommu_one(struct amd_iommu *iommu, struct ivhd_header *h,
 
 	raw_spin_lock_init(&iommu->lock);
 	iommu->cmd_sem_val = 0;
+	ida_init(&iommu->gid_ida);
 
 	/* Add IOMMU to internal data structures */
 	list_add_tail(&iommu->list, &amd_iommu_list);
drivers/iommu/amd/iommu.c +16 -0
index 2df81e29519e..d3378754cf87 100644
@@ -257,6 +257,22 @@ static inline bool pdom_is_sva_capable(struct protection_domain *pdom)
 	return pdom_is_v2_pgtbl_mode(pdom) || pdom_is_in_pt_mode(pdom);
 }
 
+int amd_iommu_gid_alloc(struct amd_iommu *iommu)
+{
+	int ret = ida_alloc_range(&iommu->gid_ida, 1, VIOMMU_MAX_GID, GFP_KERNEL);
+
+	if (ret >= 0)
+		pr_debug("%s: iommu devid=%#x, gid=%u\n", __func__, iommu->devid, ret);
+
+	return ret;
+}
+
+void amd_iommu_gid_free(struct amd_iommu *iommu, int gid)
+{
+	pr_debug("%s: iommu devid=%#x, gid=%u\n", __func__, iommu->devid, gid);
+	ida_free(&iommu->gid_ida, gid);
+}
+
 static inline int get_acpihid_device_id(struct device *dev,
 					struct acpihid_map_entry **entry)
 {
drivers/iommu/amd/iommufd.c +14 -0
index e8e7807f2a33..69a0e61bf83d 100644
@@ -49,13 +49,23 @@ size_t amd_iommufd_get_viommu_size(struct device *dev, enum iommu_viommu_type vi
 int amd_iommufd_viommu_init(struct iommufd_viommu *viommu, struct iommu_domain *parent,
 			    const struct iommu_user_data *user_data)
 {
+	int ret;
 	unsigned long flags;
 	struct protection_domain *pdom = to_pdomain(parent);
 	struct amd_iommu_viommu *aviommu = container_of(viommu, struct amd_iommu_viommu, core);
+	struct amd_iommu *iommu = container_of(viommu->iommu_dev, struct amd_iommu, iommu);
+
+	if (!amd_iommu_viommu_enabled(iommu))
+		return -EOPNOTSUPP;
 
 	xa_init_flags(&aviommu->gdomid_array, XA_FLAGS_ALLOC1);
 	aviommu->parent = pdom;
 
+	ret = amd_iommu_gid_alloc(iommu);
+	if (ret < 0)
+		return ret;
+	aviommu->gid = ret;
+
 	viommu->ops = &amd_viommu_ops;
 
 	spin_lock_irqsave(&pdom->lock, flags);
@@ -70,11 +80,15 @@ static void amd_iommufd_viommu_destroy(struct iommufd_viommu *viommu)
 	unsigned long flags;
 	struct amd_iommu_viommu *aviommu = container_of(viommu, struct amd_iommu_viommu, core);
 	struct protection_domain *pdom = aviommu->parent;
+	struct amd_iommu *iommu = container_of(viommu->iommu_dev, struct amd_iommu, iommu);
+
+	pr_debug("%s: gid=%#x, iommu devid=%#x\n", __func__, aviommu->gid, iommu->devid);
 
 	spin_lock_irqsave(&pdom->lock, flags);
 	list_del(&aviommu->pdom_list);
 	spin_unlock_irqrestore(&pdom->lock, flags);
 	xa_destroy(&aviommu->gdomid_array);
+	amd_iommu_gid_free(iommu, aviommu->gid);
 }
 
 /*
Signature · 1 line
2.34.1
maintainer
#10 lore raw

Re: [PATCH v6 05/24] iommu/amd: Allocate Guest IDs for IOMMUFD vIOMMU instances

Quoted 28 lines · Suravee Suthikulpanit

Hardware vIOMMU uses a 16-bit Guest ID (GID) per guest IOMMU to index driver and hardware state. Allocate one GID per IOMMUFD vIOMMU from a per-amd_iommu IDA (unique within that IOMMU; a VM behind multiple IOMMUs may hold more than one GID).

Add amd_iommu_gid_alloc() and amd_iommu_gid_free(), store the ID in amd_iommu_viommu::gid, and call them from amd_iommufd_viommu_init() and destroy. Initialize gid_ida in init_iommu_one() and destroy it in free_iommu_one() with the IOMMU object.

Reject amd_iommufd_viommu_init() with -EOPNOTSUPP when the IOMMU does not have AMD_IOMMU_FLAG_VIOMMU_EN.

---
 drivers/iommu/amd/amd_iommu.h       |  4 ++++
 drivers/iommu/amd/amd_iommu_types.h |  7 +++++++
 drivers/iommu/amd/init.c            |  2 ++
 drivers/iommu/amd/iommu.c           | 16 ++++++++++++++++
 drivers/iommu/amd/iommufd.c         | 14 ++++++++++++++
 5 files changed, 43 insertions(+)
drivers/iommu/amd/amd_iommu.h +0 -0
index b2ae17e3df34..f6cf412a9820 100644

drivers/iommu/amd/amd_iommu.h amd_iommu_make_clear_dte(struct iommu_dev_data *dev_data, struct dev_table_entry · line 233

Quoted 7 lines
drivers/iommu/amd/amd_iommu.h +4 -0
@@ -227,4 +227,8 @@ amd_iommu_make_clear_dte(struct iommu_dev_data *dev_data, struct dev_table_entry
 struct iommu_domain *
 amd_iommu_alloc_domain_nested(struct iommufd_viommu *viommu, u32 flags,
 			      const struct iommu_user_data *user_data);
+
+/* Guest ID for vIOMMU */
+int amd_iommu_gid_alloc(struct amd_iommu *iommu);
+void amd_iommu_gid_free(struct amd_iommu *iommu, int gid);

How about moving these two functions to iommufd.c as this is vIOMMU specific functions and not called outside iommufd.c ?

-Vasant

author reviewer
#11 lore raw

PATCH v6 6/24 iommu/amd: Map vIOMMU VF and VF Control MMIO BARs

In reply to Suravee Suthikulpanit

Enable hardware vIOMMU on an IOMMU by locating its PCI vendor-specific capability (VSC), reading the VF and VF Control BAR addresses, and mapping them for host access (256MB VF, 4MB VF Control).

VF Control covers the first 4K of guest IOMMU MMIO (control registers, trapped by QEMU). VF MMIO covers the third 4K (virtualized by the IOMMU). Per-guest bases use the Guest ID from the previous patch.

Reject a VSC VF or VF Control BAR that is disabled or decodes to physical address zero before mapping MMIO.

Set AMD_IOMMU_FLAG_VIOMMU_EN after the VF BARs are mapped. Export MMIO map helpers. Call amd_viommu_uninit() from IOMMU teardown before free_iommu_buffers() so MMIO and the command buffer remain.

Release the reserved MMIO region if ioremap() fails so VF/VFCTRL map errors do not leak request_mem_region() for the life of the machine.

Skip the host release in iommu_unmap_mmio_space() when mmio_base is NULL, so a failed ioremap() is not released twice.

---
 drivers/iommu/amd/amd_iommu.h       |  2 +
 drivers/iommu/amd/amd_iommu_types.h | 31 +++++++++
 drivers/iommu/amd/amd_viommu.h      |  6 ++
 drivers/iommu/amd/init.c            | 25 ++++++--
 drivers/iommu/amd/viommu.c          | 98 +++++++++++++++++++++++++++++
 5 files changed, 157 insertions(+), 5 deletions(-)
drivers/iommu/amd/amd_iommu.h +2 -0
index f6cf412a9820..51e4364e8564 100644
@@ -28,6 +28,8 @@ void amd_iommu_set_rlookup_table(struct amd_iommu *iommu, u16 devid);
 void iommu_feature_enable(struct amd_iommu *iommu, u8 bit);
 void *__init iommu_alloc_4k_pages(struct amd_iommu *iommu,
 				  gfp_t gfp, size_t size);
+u8 __iomem * __init iommu_map_mmio_space(u64 address, u64 end);
+void __init iommu_unmap_mmio_space(struct amd_iommu *iommu);
 
 #ifdef CONFIG_AMD_IOMMU_DEBUGFS
 void amd_iommu_debugfs_setup(void);
drivers/iommu/amd/amd_iommu_types.h +31 -0
index 8c38775ff5c1..c2257d48e222 100644
@@ -40,6 +40,12 @@
 #define MMIO_RANGE_OFFSET	0x0c
 #define MMIO_MISC_OFFSET	0x10
 
+/* vIOMMU Capability offsets (from IOMMU Capability Header) */
+#define MMIO_VSC_VF_BAR_LO_OFFSET	0x08
+#define MMIO_VSC_VF_BAR_HI_OFFSET	0x0c
+#define MMIO_VSC_VF_CNTL_BAR_LO_OFFSET	0x10
+#define MMIO_VSC_VF_CNTL_BAR_HI_OFFSET	0x14
+
 /* Used offsets into the MMIO space */
 #define MMIO_DEV_TABLE_OFFSET   0x0000
 #define MMIO_CMD_BUF_OFFSET     0x0008
@@ -464,6 +470,20 @@ extern bool amdr_ivrs_remap_support;
 #define for_each_ivhd_dte_flags(entry) \
 	list_for_each_entry((entry), &amd_ivhd_dev_flags_list, list)
 
+/* VIOMMU stuff */
+#define VIOMMU_VF_MMIO_ENTRY_SIZE		4096
+#define VIOMMU_VFCTRL_MMIO_ENTRY_SIZE		64
+
+/* Host ioremap/request_mem_region sizes for VF / VF_CNTL BARs */
+#define VIOMMU_VF_MMIO_MAP_SIZE		0x10000000UL
+#define VIOMMU_VF_CNTL_MMIO_MAP_SIZE	0x400000UL
+
+#define VIOMMU_VF_MMIO_BASE(iommu, guestId) \
+	(iommu->vf_base + (guestId * VIOMMU_VF_MMIO_ENTRY_SIZE))
+
+#define VIOMMU_VFCTRL_MMIO_BASE(iommu, guestId) \
+	(iommu->vfctrl_base + (guestId * VIOMMU_VFCTRL_MMIO_ENTRY_SIZE))
+
 struct amd_iommu;
 struct iommu_domain;
 struct irq_domain;
@@ -678,6 +698,17 @@ struct amd_iommu {
 	 */
 	u16 cap_ptr;
 
+	/*
+	 * VF MMIO base physical address. This is needed to calculate/pass
+	 * per guest VF MMIO address (3rd 4K of IOMMU MMIO space)
+	 */
+	u64 vf_base_phys;
+	u64 vf_cntl_phys;
+
+	/* virtual addresses of vIOMMU VF/VF_CNTL BAR */
+	u8 __iomem *vf_base;
+	u8 __iomem *vfctrl_base;
+
 	/* pci domain of this IOMMU */
 	struct amd_iommu_pci_seg *pci_seg;
 
drivers/iommu/amd/amd_viommu.h +6 -0
index 864e7b283cff..e5fbea666e01 100644
@@ -12,6 +12,8 @@ struct amd_iommu;
 
 int amd_viommu_init(struct amd_iommu *iommu);
 
+void __init amd_viommu_uninit(struct amd_iommu *iommu);
+
 #else
 
 /*
@@ -24,6 +26,10 @@ static inline int amd_viommu_init(struct amd_iommu *iommu)
 	return 0;
 }
 
+static inline void amd_viommu_uninit(struct amd_iommu *iommu)
+{
+}
+
 #endif /* CONFIG_AMD_IOMMU_IOMMUFD */
 
 #endif /* AMD_VIOMMU_H */
drivers/iommu/amd/init.c +20 -5
index 7b4cb473019d..5be422591bac 100644
@@ -459,8 +459,10 @@ static void iommu_disable(struct amd_iommu *iommu)
  * mapping and unmapping functions for the IOMMU MMIO space. Each AMD IOMMU in
  * the system has one.
  */
-static u8 __iomem * __init iommu_map_mmio_space(u64 address, u64 end)
+u8 __iomem * __init iommu_map_mmio_space(u64 address, u64 end)
 {
+	u8 __iomem *base;
+
 	if (!request_mem_region(address, end, "amd_iommu")) {
 		pr_err("Can not reserve memory region %llx-%llx for mmio\n",
 			address, end);
@@ -468,13 +470,24 @@ static u8 __iomem * __init iommu_map_mmio_space(u64 address, u64 end)
 		return NULL;
 	}
 
-	return (u8 __iomem *)ioremap(address, end);
+	base = ioremap(address, end);
+	if (!base)
+		release_mem_region(address, end);
+
+	return base;
 }
 
-static void __init iommu_unmap_mmio_space(struct amd_iommu *iommu)
+void __init iommu_unmap_mmio_space(struct amd_iommu *iommu)
 {
-	if (iommu->mmio_base)
-		iounmap(iommu->mmio_base);
+	/*
+	 * iommu_map_mmio_space() drops the reservation when ioremap()
+	 * fails, so release only a mapping that was installed.
+	 */
+	if (!iommu->mmio_base)
+		return;
+
+	iounmap(iommu->mmio_base);
+	iommu->mmio_base = NULL;
 	release_mem_region(iommu->mmio_phys, iommu->mmio_phys_end);
 }
 
@@ -1792,6 +1805,8 @@ static void __init free_sysfs(struct amd_iommu *iommu)
 static void __init free_iommu_one(struct amd_iommu *iommu)
 {
 	free_sysfs(iommu);
+	/* Tear down vIOMMU before IOMMU buffers and MMIO are released. */
+	amd_viommu_uninit(iommu);
 	free_iommu_buffers(iommu);
 	amd_iommu_free_ppr_log(iommu);
 	free_ga_log(iommu);
drivers/iommu/amd/viommu.c +98 -0

106 lines not on this page · raw message

Signature · 1 line
2.34.1
maintainer
#12 lore raw

Re: [PATCH v6 06/24] iommu/amd: Map vIOMMU VF and VF Control MMIO BARs

Quoted 18 lines · Suravee Suthikulpanit

Enable hardware vIOMMU on an IOMMU by locating its PCI vendor-specific capability (VSC), reading the VF and VF Control BAR addresses, and mapping them for host access (256MB VF, 4MB VF Control).

VF Control covers the first 4K of guest IOMMU MMIO (control registers, trapped by QEMU). VF MMIO covers the third 4K (virtualized by the IOMMU). Per-guest bases use the Guest ID from the previous patch.

Reject a VSC VF or VF Control BAR that is disabled or decodes to physical address zero before mapping MMIO.

Set AMD_IOMMU_FLAG_VIOMMU_EN after the VF BARs are mapped. Export MMIO map helpers. Call amd_viommu_uninit() from IOMMU teardown before free_iommu_buffers() so MMIO and the command buffer remain.

Release the reserved MMIO region if ioremap() fails so VF/VFCTRL map errors do not leak request_mem_region() for the life of the machine.

Quoted 6 lines

Skip the host release in iommu_unmap_mmio_space() when mmio_base is NULL, so a failed ioremap() is not released twice.

---

.../...

Quoted 17 lines
drivers/iommu/amd/viommu.c +17 -0
+
+	pci_read_config_dword(iommu->dev, vsc + MMIO_VSC_VF_CNTL_BAR_LO_OFFSET, &lo);
+	pci_read_config_dword(iommu->dev, vsc + MMIO_VSC_VF_CNTL_BAR_HI_OFFSET, &hi);
+	vf_cntl_phys = hi;
+	vf_cntl_phys = (vf_cntl_phys << 32) | lo;
+	if (!(vf_cntl_phys & 1)) {
+		pr_err(FW_BUG "vf_cntl_phys disabled\n");
+		return -EINVAL;
+	}
+	vf_cntl_phys &= ~1ULL;
+	if (!vf_cntl_phys) {
+		pr_err(FW_BUG "vf_cntl_phys address is zero\n");
+		return -EINVAL;
+	}
+
+	/* Mapping 256MB of VF and 4MB of VF_CNTL BARs */
+	iommu->vf_base = iommu_map_mmio_space(vf_phys, VIOMMU_VF_MMIO_MAP_SIZE);

drivers/iommu/amd/viommu.c line 101

Quoted 10 lines
drivers/iommu/amd/viommu.c +10 -0
+	if (!iommu->vf_base) {
+		pr_err("Can't reserve vf_base\n");
+		goto err_out;
+	}
+	iommu->vf_base_phys = vf_phys;
+
+	iommu->vfctrl_base = iommu_map_mmio_space(vf_cntl_phys, VIOMMU_VF_CNTL_MMIO_MAP_SIZE);
+	if (!iommu->vfctrl_base) {
+		pr_err("Can't reserve vfctrl_base\n");
+		goto err_out;

Nothing to cleanup here. You can replace goto with return.

-Vasant

maintainer
#13 lore raw

Re: [PATCH v6 06/24] iommu/amd: Map vIOMMU VF and VF Control MMIO BARs

In reply to Suravee Suthikulpanit

drivers/iommu/amd/amd_iommu.h

Quoted 32 lines · Suravee Suthikulpanit

Enable hardware vIOMMU on an IOMMU by locating its PCI vendor-specific capability (VSC), reading the VF and VF Control BAR addresses, and mapping them for host access (256MB VF, 4MB VF Control).

VF Control covers the first 4K of guest IOMMU MMIO (control registers, trapped by QEMU). VF MMIO covers the third 4K (virtualized by the IOMMU). Per-guest bases use the Guest ID from the previous patch.

Reject a VSC VF or VF Control BAR that is disabled or decodes to physical address zero before mapping MMIO.

Set AMD_IOMMU_FLAG_VIOMMU_EN after the VF BARs are mapped. Export MMIO map helpers. Call amd_viommu_uninit() from IOMMU teardown before free_iommu_buffers() so MMIO and the command buffer remain.

Release the reserved MMIO region if ioremap() fails so VF/VFCTRL map errors do not leak request_mem_region() for the life of the machine.

Skip the host release in iommu_unmap_mmio_space() when mmio_base is NULL, so a failed ioremap() is not released twice.

---
 drivers/iommu/amd/amd_iommu.h       |  2 +
 drivers/iommu/amd/amd_iommu_types.h | 31 +++++++++
 drivers/iommu/amd/amd_viommu.h      |  6 ++
 drivers/iommu/amd/init.c            | 25 ++++++--
 drivers/iommu/amd/viommu.c          | 98 +++++++++++++++++++++++++++++
 5 files changed, 157 insertions(+), 5 deletions(-)
drivers/iommu/amd/amd_iommu.h +0 -0

.../...

Quoted 13 lines
drivers/iommu/amd/amd_iommu.h +13 -0
+
+	pr_debug("device:%s, vsc offset:%04x\n",
+		 pci_name(iommu->dev), vsc);
+
+	/* Setting up VF and VF_CNTL MMIOs */
+	pci_read_config_dword(iommu->dev, vsc + MMIO_VSC_VF_BAR_LO_OFFSET, &lo);
+	pci_read_config_dword(iommu->dev, vsc + MMIO_VSC_VF_BAR_HI_OFFSET, &hi);
+	vf_phys = hi;
+	vf_phys = (vf_phys << 32) | lo;
+	if (!(vf_phys & 1)) {
+		pr_err(FW_BUG "vf_phys disabled\n");
+		return -EINVAL;
+	}

drivers/iommu/amd/amd_iommu.h

Quoted 10 lines
drivers/iommu/amd/amd_iommu.h +10 -0
+	vf_phys &= ~1ULL;
+	if (!vf_phys) {
+		pr_err(FW_BUG "vf_phys address is zero\n");
+		return -EINVAL;
+	}
+
+	pci_read_config_dword(iommu->dev, vsc + MMIO_VSC_VF_CNTL_BAR_LO_OFFSET, &lo);
+	pci_read_config_dword(iommu->dev, vsc + MMIO_VSC_VF_CNTL_BAR_HI_OFFSET, &hi);
+	vf_cntl_phys = hi;
+	vf_cntl_phys = (vf_cntl_phys << 32) | lo;

AI suggests, w should use mask with GENMASK_ULL(63, 28) and GENMASK_ULL(63, 22) as other bits are reserved in these two registers.

-Vasant

author reviewer
#14 lore raw

PATCH v6 7/24 iommu/amd: Add support for AMD vIOMMU VF MMIO region

In reply to Suravee Suthikulpanit

The AMD vIOMMU virtualizes guest MMIO registers at the 3rd 4K region. This is achieved using the iommufd_viommu_alloc_mmap().

Copy struct iommu_viommu_amd from and to userspace so the VF-MMIO mmap offset is validated and returned on vIOMMU alloc.

Return -EINVAL if the VF-MMIO page_base is zero so mmap is not registered at physical address 0.

---
 drivers/iommu/amd/amd_iommu_types.h |  3 +++
 drivers/iommu/amd/amd_viommu.h      |  7 +++++
 drivers/iommu/amd/iommufd.c         | 41 ++++++++++++++++++++++++++++-
 drivers/iommu/amd/viommu.c          | 12 +++++++++
 4 files changed, 62 insertions(+), 1 deletion(-)
drivers/iommu/amd/amd_iommu_types.h +3 -0
index c2257d48e222..3aaaf69a1e7d 100644
@@ -532,6 +532,9 @@ struct amd_iommu_viommu {
 	 * Indexed by guest domain ID.
 	 */
 	struct xarray gdomid_array;
+
+	/* Offset for mmap() of guest VF MMIO; set after iommufd_viommu_alloc_mmap(). */
+	unsigned long vfmmio_mmap_offset;
 };
 
 /*
drivers/iommu/amd/amd_viommu.h +7 -0
index e5fbea666e01..276f2126290f 100644
@@ -14,6 +14,8 @@ int amd_viommu_init(struct amd_iommu *iommu);
 
 void __init amd_viommu_uninit(struct amd_iommu *iommu);
 
+u64 amd_viommu_get_vfmmio_addr(struct amd_iommu *iommu, u16 gid);
+
 #else
 
 /*
@@ -30,6 +32,11 @@ static inline void amd_viommu_uninit(struct amd_iommu *iommu)
 {
 }
 
+static inline u64 amd_viommu_get_vfmmio_addr(struct amd_iommu *iommu, u16 gid)
+{
+	return 0;
+}
+
 #endif /* CONFIG_AMD_IOMMU_IOMMUFD */
 
 #endif /* AMD_VIOMMU_H */
drivers/iommu/amd/iommufd.c +40 -1

78 lines not on this page · raw message

drivers/iommu/amd/viommu.c +12 -0
index 7e6c4412ff20..c9207ed47fb6 100644
@@ -110,6 +110,18 @@ static int __init viommu_vf_vfcntl_init(struct amd_iommu *iommu)
 	return -ENOMEM;
 }
 
+/*
+ * Returns VF MMIO BAR offset for the given guest ID which will be
+ * mapped to guest vIOMMU 3rd 4K MMIO address
+ */
+u64 amd_viommu_get_vfmmio_addr(struct amd_iommu *iommu, u16 gid)
+{
+	if (WARN_ON(!iommu->vf_base_phys || gid > VIOMMU_MAX_GID))
+		return 0;
+
+	return iommu->vf_base_phys + gid * VIOMMU_VF_MMIO_ENTRY_SIZE;
+}
+
 int __init amd_viommu_init(struct amd_iommu *iommu)
 {
 	int ret;
Signature · 1 line
2.34.1
maintainer
#15 lore raw

Re: [PATCH v6 07/24] iommu/amd: Add support for AMD vIOMMU VF MMIO region

Quoted 6 lines · Suravee Suthikulpanit

The AMD vIOMMU virtualizes guest MMIO registers at the 3rd 4K region. This is achieved using the iommufd_viommu_alloc_mmap().

Copy struct iommu_viommu_amd from and to userspace so the VF-MMIO mmap offset is validated and returned on vIOMMU alloc.

Quoted 8 lines

Return -EINVAL if the VF-MMIO page_base is zero so mmap is not registered at physical address 0.

---

.../...

Quoted 15 lines

/*

drivers/iommu/amd/amd_viommu.h +2 -0
index e5fbea666e01..276f2126290f 100644
@@ -14,6 +14,8 @@ int amd_viommu_init(struct amd_iommu *iommu);
 
 void __init amd_viommu_uninit(struct amd_iommu *iommu);
 
+u64 amd_viommu_get_vfmmio_addr(struct amd_iommu *iommu, u16 gid);
+
 #else
 
 /*

drivers/iommu/amd/amd_viommu.h static inline void amd_viommu_uninit(struct amd_iommu *iommu) · line 39

Quoted 8 lines
drivers/iommu/amd/amd_viommu.h +5 -0
@@ -30,6 +32,11 @@ static inline void amd_viommu_uninit(struct amd_iommu *iommu)
 {
 }
 
+static inline u64 amd_viommu_get_vfmmio_addr(struct amd_iommu *iommu, u16 gid)
+{
+	return 0;
+}
+

Do we really needs this? amd_viommu_get_vfmmio_addr() will be called from vIOMMU code path only.

-Vasant

author reviewer
#16 lore raw

PATCH v6 8/24 iommu/amd: Introduce Reset vMMIO Command

In reply to Suravee Suthikulpanit

Introduce new IOMMU commands for vIOMMU to reset virtualized MMIO registers of a particular guest.

Always program RESET_MMIO_ALL_FLAG and RESET_MMIO_VCMD_FLAG. Propagate iommu_completion_wait() errors to the caller.

---
 drivers/iommu/amd/amd_iommu.h       |  1 +
 drivers/iommu/amd/amd_iommu_types.h |  3 +++
 drivers/iommu/amd/iommu.c           | 21 +++++++++++++++++++++
 drivers/iommu/amd/iommufd.c         |  5 +++++
 4 files changed, 30 insertions(+)
drivers/iommu/amd/amd_iommu.h +1 -0
index 51e4364e8564..8c298e090889 100644
@@ -233,4 +233,5 @@ amd_iommu_alloc_domain_nested(struct iommufd_viommu *viommu, u32 flags,
 /* Guest ID for vIOMMU */
 int amd_iommu_gid_alloc(struct amd_iommu *iommu);
 void amd_iommu_gid_free(struct amd_iommu *iommu, int gid);
+int amd_iommu_reset_vmmio(struct amd_iommu *iommu, u16 gid);
 #endif /* AMD_IOMMU_H */
drivers/iommu/amd/amd_iommu_types.h +3 -0
index 3aaaf69a1e7d..9eb9a0c190de 100644
@@ -209,6 +209,9 @@
 #define CMD_INV_IRT		0x05
 #define CMD_COMPLETE_PPR	0x07
 #define CMD_INV_ALL		0x08
+#define CMD_RESET_VMMIO		0x0A
+#define RESET_MMIO_ALL_FLAG	BIT(28)
+#define RESET_MMIO_VCMD_FLAG	BIT(31)
 
 #define CMD_COMPL_WAIT_STORE_MASK	0x01
 #define CMD_COMPL_WAIT_INT_MASK		0x02
drivers/iommu/amd/iommu.c +21 -0
index d3378754cf87..ae44050fe86a 100644
@@ -1524,6 +1524,13 @@ static void build_inv_irt(struct iommu_cmd *cmd, u16 devid)
 	CMD_SET_TYPE(cmd, CMD_INV_IRT);
 }
 
+static void build_reset_vmmio(struct iommu_cmd *cmd, u16 gid)
+{
+	memset(cmd, 0, sizeof(*cmd));
+	cmd->data[0] = gid | RESET_MMIO_ALL_FLAG | RESET_MMIO_VCMD_FLAG;
+	CMD_SET_TYPE(cmd, CMD_RESET_VMMIO);
+}
+
 /*
  * Writes the command to the IOMMUs command buffer and informs the
  * hardware about the new command.
@@ -1777,6 +1784,20 @@ void amd_iommu_flush_all_caches(struct amd_iommu *iommu)
 	}
 }
 
+int amd_iommu_reset_vmmio(struct amd_iommu *iommu, u16 gid)
+{
+	int ret;
+	struct iommu_cmd cmd;
+
+	build_reset_vmmio(&cmd, gid);
+
+	ret = iommu_queue_command(iommu, &cmd);
+	if (ret)
+		return ret;
+
+	return iommu_completion_wait(iommu);
+}
+
 /*
  * Command send function for flushing on-device TLB
  */
drivers/iommu/amd/iommufd.c +5 -0
index 7f358e57efde..2e34491e3cf7 100644
@@ -92,6 +92,11 @@ int amd_iommufd_viommu_init(struct iommufd_viommu *viommu, struct iommu_domain *
 
 	data.out_vfmmio_mmap_offset = aviommu->vfmmio_mmap_offset;
 
+	/* Reset vIOMMU MMIOs to initialize the vIOMMU */
+	ret = amd_iommu_reset_vmmio(iommu, aviommu->gid);
+	if (ret)
+		goto err_init;
+
 	ret = iommu_copy_struct_to_user(user_data, &data,
 					IOMMU_VIOMMU_TYPE_AMD,
 					out_vfmmio_mmap_offset);
Signature · 1 line
2.34.1
author reviewer
#17 lore raw

PATCH v6 9/24 iommu/amd: Introduce and map vIOMMU private IPA region

In reply to Suravee Suthikulpanit

AMD vIOMMU introduces the IOMMU Private Address (IPA) region for guest-side IOMMU virtualization data structures.

Introduce a per-IOMMU v1 paging domain in viommu_pdom, allocate 8MB of backing memory as four 2MB subregions, map them into the domain, and add viommu_private_space_init() / viommu_private_space_uninit() as a matched pair.

Register the owning IOMMU in viommu_pdom->iommu_array via amd_iommu_pdom_bind_iommu() so unmap uses the standard domain_flush_pages_v1() IOTLB invalidation path. On bare metal iommu_map() does not flush (amd_iommu_iotlb_sync_map() returns early unless amd_iommu_np_cache is set).

Call viommu_private_space_uninit() from amd_viommu_uninit() so boot failure and free_iommu_one() release the 8MB IPA mapping. Failed amd_viommu_init() after VF mapping goes through uninit.

viommu_priv_alloc_map() takes a gfp for backing pages and iommu_map() page tables. Boot uses GFP_KERNEL. iommu_alloc_pages_node_sz() zeros them. On set_memory_uc() or iommu_map() failure, restore WB or leak rather than returning UC-mapped memory to the allocator. iommu_map() already unmaps any partial IOVA.

Skip IOTLB invalidation when CONTROL_CMDBUF_EN is clear so boot failure after disable_iommus() does not wait on a dead command buffer.

set_memory_uc() on each 2MB-aligned subregion splits the x86 direct map to 4KB PTEs. set_memory_wb() on free does not re-coalesce them, so the direct map stays fragmented (8MB per vIOMMU-capable IOMMU) for the life of the machine.

The private IPA domain skips iommu_domain_init(), so set IOMMU_DOMAIN_UNMANAGED. set_dte_entry() only programs a v1 table when type has __IOMMU_DOMAIN_PAGING; type 0 would WARN and install a cleared DTE when increase_top() rewrites the self DTE.

For more info, see section vIOMMU Private Address Space of the IOMMU specification [1].

[1] https://docs.amd.com/v/u/en-US/48882_3.10_PUB

---
 drivers/iommu/amd/amd_iommu.h       |   5 +
 drivers/iommu/amd/amd_iommu_types.h |  11 ++
 drivers/iommu/amd/init.c            |   7 +-
 drivers/iommu/amd/iommu.c           |  13 +-
 drivers/iommu/amd/viommu.c          | 192 ++++++++++++++++++++++++++++
 5 files changed, 225 insertions(+), 3 deletions(-)
drivers/iommu/amd/amd_iommu.h +5 -0
index 8c298e090889..a0d5d7e34020 100644
@@ -53,6 +53,8 @@ extern bool amd_iommu_hatdis;
 /* Protection domain ops */
 void amd_iommu_init_identity_domain(void);
 struct protection_domain *protection_domain_alloc(void);
+struct iommu_domain *amd_iommu_domain_alloc_paging_v1(struct device *dev,
+						      u32 flags);
 struct iommu_domain *amd_iommu_domain_alloc_sva(struct device *dev,
 						struct mm_struct *mm);
 void amd_iommu_domain_free(struct iommu_domain *dom);
@@ -94,6 +96,9 @@ void amd_iommu_domain_flush_pages(struct protection_domain *domain,
 void amd_iommu_dev_flush_pasid_pages(struct iommu_dev_data *dev_data,
 				     ioasid_t pasid, u64 address, u64 last);
 
+int amd_iommu_pdom_bind_iommu(struct amd_iommu *iommu, struct protection_domain *pdom);
+void amd_iommu_pdom_unbind_iommu(struct amd_iommu *iommu, struct protection_domain *pdom);
+
 #ifdef CONFIG_IRQ_REMAP
 int amd_iommu_create_irq_domain(struct amd_iommu *iommu);
 #else
drivers/iommu/amd/amd_iommu_types.h +11 -0
index 9eb9a0c190de..9a90b8fe0fe3 100644
@@ -417,6 +417,13 @@
 /* Guest ID bits 0-14; bit 15 is reserved for secure vIOMMU. */
 #define VIOMMU_MAX_GID		0x7FFF
 
+/*
+ * Total IOMMU private region is 8MB (4 x 2MB-subregion)
+ */
+#define VIOMMU_PRIV_REGION_BASE		(0)
+#define VIOMMU_PRIV_SUBREGION_CNT	(4)
+#define VIOMMU_PRIV_SUBREGION_SIZE	(0x200000)  /* 2MB */
+
 /* Timeout stuff */
 #define LOOP_TIMEOUT		100000
 #define MMIO_STATUS_TIMEOUT	2000000
@@ -804,6 +811,10 @@ struct amd_iommu {
 	unsigned char iopfq_name[32];
 
 	struct ida gid_ida;		 /* guest IDs for this IOMMU */
+
+	/* HW vIOMMU support */
+	struct protection_domain *viommu_pdom;
+	void *viommu_priv_region[VIOMMU_PRIV_SUBREGION_CNT];
 };
 
 static inline struct amd_iommu *dev_to_amd_iommu(struct device *dev)
drivers/iommu/amd/init.c +6 -1
index 5be422591bac..d34f991dee24 100644
@@ -1805,7 +1805,12 @@ static void __init free_sysfs(struct amd_iommu *iommu)
 static void __init free_iommu_one(struct amd_iommu *iommu)
 {
 	free_sysfs(iommu);
-	/* Tear down vIOMMU before IOMMU buffers and MMIO are released. */
+	/*
+	 * Tear down vIOMMU before buffers and MMIO are released.
+	 * IOTLB invalidation runs only while CONTROL_CMDBUF_EN is
+	 * set (amd_viommu_init() unwind). disable_iommus() clears
+	 * that bit first on the boot-failure path.
+	 */
 	amd_viommu_uninit(iommu);
 	free_iommu_buffers(iommu);
 	amd_iommu_free_ppr_log(iommu);
drivers/iommu/amd/iommu.c +11 -2
index ae44050fe86a..dfb9268f3c4e 100644
@@ -2509,6 +2509,16 @@ static void pdom_detach_iommu(struct amd_iommu *iommu,
 	spin_unlock_irqrestore(&pdom->lock, flags);
 }
 
+int amd_iommu_pdom_bind_iommu(struct amd_iommu *iommu, struct protection_domain *pdom)
+{
+	return pdom_attach_iommu(iommu, pdom);
+}
+
+void amd_iommu_pdom_unbind_iommu(struct amd_iommu *iommu, struct protection_domain *pdom)
+{
+	pdom_detach_iommu(iommu, pdom);
+}
+
 /*
  * If a device is not yet associated with a domain, this function makes the
  * device visible in the domain
@@ -2858,8 +2868,7 @@ static const struct iommu_dirty_ops amdv1_dirty_ops = {
 	.set_dirty_tracking = amd_iommu_set_dirty_tracking,
 };
 
-static struct iommu_domain *amd_iommu_domain_alloc_paging_v1(struct device *dev,
-							     u32 flags)
+struct iommu_domain *amd_iommu_domain_alloc_paging_v1(struct device *dev, u32 flags)
 {
 	struct pt_iommu_amdv1_cfg cfg = {};
 	struct protection_domain *domain;
drivers/iommu/amd/viommu.c +192 -0

219 lines not on this page · raw message

Signature · 1 line
2.34.1
maintainer
#18 lore raw

Re: [PATCH v6 09/24] iommu/amd: Introduce and map vIOMMU private IPA region

Quoted 128 lines · Suravee Suthikulpanit

AMD vIOMMU introduces the IOMMU Private Address (IPA) region for guest-side IOMMU virtualization data structures.

Introduce a per-IOMMU v1 paging domain in viommu_pdom, allocate 8MB of backing memory as four 2MB subregions, map them into the domain, and add viommu_private_space_init() / viommu_private_space_uninit() as a matched pair.

Register the owning IOMMU in viommu_pdom->iommu_array via amd_iommu_pdom_bind_iommu() so unmap uses the standard domain_flush_pages_v1() IOTLB invalidation path. On bare metal iommu_map() does not flush (amd_iommu_iotlb_sync_map() returns early unless amd_iommu_np_cache is set).

Call viommu_private_space_uninit() from amd_viommu_uninit() so boot failure and free_iommu_one() release the 8MB IPA mapping. Failed amd_viommu_init() after VF mapping goes through uninit.

viommu_priv_alloc_map() takes a gfp for backing pages and iommu_map() page tables. Boot uses GFP_KERNEL. iommu_alloc_pages_node_sz() zeros them. On set_memory_uc() or iommu_map() failure, restore WB or leak rather than returning UC-mapped memory to the allocator. iommu_map() already unmaps any partial IOVA.

Skip IOTLB invalidation when CONTROL_CMDBUF_EN is clear so boot failure after disable_iommus() does not wait on a dead command buffer.

set_memory_uc() on each 2MB-aligned subregion splits the x86 direct map to 4KB PTEs. set_memory_wb() on free does not re-coalesce them, so the direct map stays fragmented (8MB per vIOMMU-capable IOMMU) for the life of the machine.

The private IPA domain skips iommu_domain_init(), so set IOMMU_DOMAIN_UNMANAGED. set_dte_entry() only programs a v1 table when type has __IOMMU_DOMAIN_PAGING; type 0 would WARN and install a cleared DTE when increase_top() rewrites the self DTE.

Quoted 1 line

For more info, see section vIOMMU Private Address Space of the

IOMMU specification [1].

[1] https://docs.amd.com/v/u/en-US/48882_3.10_PUB

---
 drivers/iommu/amd/amd_iommu.h       |   5 +
 drivers/iommu/amd/amd_iommu_types.h |  11 ++
 drivers/iommu/amd/init.c            |   7 +-
 drivers/iommu/amd/iommu.c           |  13 +-
 drivers/iommu/amd/viommu.c          | 192 ++++++++++++++++++++++++++++
 5 files changed, 225 insertions(+), 3 deletions(-)
drivers/iommu/amd/amd_iommu.h +5 -0
index 8c298e090889..a0d5d7e34020 100644
@@ -53,6 +53,8 @@ extern bool amd_iommu_hatdis;
 /* Protection domain ops */
 void amd_iommu_init_identity_domain(void);
 struct protection_domain *protection_domain_alloc(void);
+struct iommu_domain *amd_iommu_domain_alloc_paging_v1(struct device *dev,
+						      u32 flags);
 struct iommu_domain *amd_iommu_domain_alloc_sva(struct device *dev,
 						struct mm_struct *mm);
 void amd_iommu_domain_free(struct iommu_domain *dom);
@@ -94,6 +96,9 @@ void amd_iommu_domain_flush_pages(struct protection_domain *domain,
 void amd_iommu_dev_flush_pasid_pages(struct iommu_dev_data *dev_data,
 				     ioasid_t pasid, u64 address, u64 last);
 
+int amd_iommu_pdom_bind_iommu(struct amd_iommu *iommu, struct protection_domain *pdom);
+void amd_iommu_pdom_unbind_iommu(struct amd_iommu *iommu, struct protection_domain *pdom);
+
 #ifdef CONFIG_IRQ_REMAP
 int amd_iommu_create_irq_domain(struct amd_iommu *iommu);
 #else
drivers/iommu/amd/amd_iommu_types.h +11 -0
index 9eb9a0c190de..9a90b8fe0fe3 100644
@@ -417,6 +417,13 @@
 /* Guest ID bits 0-14; bit 15 is reserved for secure vIOMMU. */
 #define VIOMMU_MAX_GID		0x7FFF
 
+/*
+ * Total IOMMU private region is 8MB (4 x 2MB-subregion)
+ */
+#define VIOMMU_PRIV_REGION_BASE		(0)
+#define VIOMMU_PRIV_SUBREGION_CNT	(4)
+#define VIOMMU_PRIV_SUBREGION_SIZE	(0x200000)  /* 2MB */
+
 /* Timeout stuff */
 #define LOOP_TIMEOUT		100000
 #define MMIO_STATUS_TIMEOUT	2000000
@@ -804,6 +811,10 @@ struct amd_iommu {
 	unsigned char iopfq_name[32];
 
 	struct ida gid_ida;		 /* guest IDs for this IOMMU */
+
+	/* HW vIOMMU support */
+	struct protection_domain *viommu_pdom;
+	void *viommu_priv_region[VIOMMU_PRIV_SUBREGION_CNT];
 };
 
 static inline struct amd_iommu *dev_to_amd_iommu(struct device *dev)
drivers/iommu/amd/init.c +6 -1
index 5be422591bac..d34f991dee24 100644
@@ -1805,7 +1805,12 @@ static void __init free_sysfs(struct amd_iommu *iommu)
 static void __init free_iommu_one(struct amd_iommu *iommu)
 {
 	free_sysfs(iommu);
-	/* Tear down vIOMMU before IOMMU buffers and MMIO are released. */
+	/*
+	 * Tear down vIOMMU before buffers and MMIO are released.
+	 * IOTLB invalidation runs only while CONTROL_CMDBUF_EN is
+	 * set (amd_viommu_init() unwind). disable_iommus() clears
+	 * that bit first on the boot-failure path.
+	 */
 	amd_viommu_uninit(iommu);
 	free_iommu_buffers(iommu);
 	amd_iommu_free_ppr_log(iommu);
drivers/iommu/amd/iommu.c +0 -0
index ae44050fe86a..dfb9268f3c4e 100644

drivers/iommu/amd/iommu.c static void pdom_detach_iommu(struct amd_iommu *iommu, · line 2512

Quoted 4 lines
drivers/iommu/amd/iommu.c +1 -0
@@ -2509,6 +2509,16 @@ static void pdom_detach_iommu(struct amd_iommu *iommu,
 	spin_unlock_irqrestore(&pdom->lock, flags);
 }
 
+int amd_iommu_pdom_bind_iommu(struct amd_iommu *iommu, struct protection_domain *pdom)

Why not just rename pdom_attach_iommu / pdom_detach_iommu ?

Quoted 48 lines
drivers/iommu/amd/iommu.c +10 -2
+{
+	return pdom_attach_iommu(iommu, pdom);
+}
+
+void amd_iommu_pdom_unbind_iommu(struct amd_iommu *iommu, struct protection_domain *pdom)
+{
+	pdom_detach_iommu(iommu, pdom);
+}
+
 /*
  * If a device is not yet associated with a domain, this function makes the
  * device visible in the domain
@@ -2858,8 +2868,7 @@ static const struct iommu_dirty_ops amdv1_dirty_ops = {
 	.set_dirty_tracking = amd_iommu_set_dirty_tracking,
 };
 
-static struct iommu_domain *amd_iommu_domain_alloc_paging_v1(struct device *dev,
-							     u32 flags)
+struct iommu_domain *amd_iommu_domain_alloc_paging_v1(struct device *dev, u32 flags)
 {
 	struct pt_iommu_amdv1_cfg cfg = {};
 	struct protection_domain *domain;
drivers/iommu/amd/viommu.c +4 -0
index c9207ed47fb6..89d6dc520b2c 100644
@@ -17,6 +17,7 @@
 #include "amd_iommu.h"
 #include "amd_iommu_types.h"
 #include "amd_viommu.h"
+#include "../iommu-pages.h"
 
 static void __init amd_viommu_vf_vfcntl_unmap(struct amd_iommu *iommu)
 {
@@ -39,9 +40,12 @@ static void __init amd_viommu_vf_vfcntl_unmap(struct amd_iommu *iommu)
 	}
 }
 
+static void viommu_private_space_uninit(struct amd_iommu *iommu);
+
 void __init amd_viommu_uninit(struct amd_iommu *iommu)
 {
 	iommu->flags &= ~AMD_IOMMU_FLAG_VIOMMU_EN;
+	viommu_private_space_uninit(iommu);
 	amd_viommu_vf_vfcntl_unmap(iommu);
 }
 

drivers/iommu/amd/viommu.c static int __init viommu_vf_vfcntl_init(struct amd_iommu *iommu) · line 122

Quoted 9 lines
drivers/iommu/amd/viommu.c +6 -0
@@ -110,6 +114,187 @@ static int __init viommu_vf_vfcntl_init(struct amd_iommu *iommu)
 	return -ENOMEM;
 }
 
+/*
+ * Restore WB and free @va. If WB restore fails, leak the pages so
+ * UC-MINUS direct-map PTEs and the PAT memtype reservation are not
+ * returned to the buddy allocator.
+ */
+static void viommu_priv_wb_free(void *va, size_t size)

Better viommu_priv_free() so that it matches the alloc function?

Quoted 13 lines
drivers/iommu/amd/viommu.c +13 -0
+{
+	if (WARN_ON_ONCE(set_memory_wb((unsigned long)va,
+				       size >> PAGE_SHIFT)))
+		return;
+	iommu_free_pages(va);
+}
+
+/*
+ * Allocate backing pages, mark UC, and map at @iova in viommu_pdom.
+ * *@out_va is NULL on any failure. @gfp is used for the backing
+ * folio and iommu_map() page tables.
+ */
+static int viommu_priv_alloc_map(struct amd_iommu *iommu, u64 iova, size_t size,

drivers/iommu/amd/viommu.c static int __init viommu_vf_vfcntl_init(struct amd_iommu *iommu) · line 145

Quoted 10 lines
drivers/iommu/amd/viommu.c +10 -0
+				 gfp_t gfp, void **out_va)
+{
+	int ret;
+	void *va;
+	int nid = iommu && iommu->dev ? dev_to_node(&iommu->dev->dev) : NUMA_NO_NODE;
+
+	*out_va = NULL;
+
+	if (!iommu || !iommu->viommu_pdom)
+		return -EINVAL;

Redundant check as its static function and know we it gets called only if iommu and viommu_pdom is valid.

Quoted 41 lines
drivers/iommu/amd/viommu.c +41 -0
+
+	va = iommu_alloc_pages_node_sz(nid, gfp, size);
+	if (!va)
+		return -ENOMEM;
+
+	/*
+	 * IOMMU spec: backing storage must be UC. set_memory_uc()
+	 * splits covering 2MB direct-map pages to 4K; set_memory_wb()
+	 * does not re-coalesce them.
+	 */
+	ret = set_memory_uc((unsigned long)va, size >> PAGE_SHIFT);
+	if (ret)
+		goto err_uc;
+
+	ret = iommu_map(&iommu->viommu_pdom->domain, iova,
+			iommu_virt_to_phys(va), size,
+			IOMMU_READ | IOMMU_WRITE, gfp);
+	if (ret)
+		goto err_uc;
+
+	*out_va = va;
+	return 0;
+
+err_uc:
+	viommu_priv_wb_free(va, size);
+	return ret;
+}
+
+static bool viommu_cmdbuf_enabled(struct amd_iommu *iommu)
+{
+	if (!iommu->mmio_base)
+		return false;
+	return readq(iommu->mmio_base + MMIO_CONTROL_OFFSET) &
+	       BIT_ULL(CONTROL_CMDBUF_EN);
+}
+
+/*
+ * Unmap @iova if the command buffer is still enabled, restore WB, and
+ * free @cpu_va. Skip IOTLB invalidation after disable_iommus().
+ */
+static void viommu_priv_unmap_free(struct amd_iommu *iommu, u64 iova, size_t size,

drivers/iommu/amd/viommu.c static int __init viommu_vf_vfcntl_init(struct amd_iommu *iommu) · line 196

Quoted 10 lines
drivers/iommu/amd/viommu.c +10 -0
+				   void *cpu_va)
+{
+	size_t unmapped;
+
+	if (!cpu_va)
+		return;
+	if (!iommu || !iommu->viommu_pdom)
+		return;
+
+	if (viommu_cmdbuf_enabled(iommu)) {

Hmmm. Why do we need this check? We already have AMD_IOMMU_FLAG_VIOMMU_EN to track whether vIOMMU is enabled or not. So in amd_viommu_uninit() path, if required we can check that flag so that we are sure command buffer was enabled.

-Vasant

author reviewer
#19 lore raw

PATCH v6 10/24 iommu/amd: Pass iommu to device_flush_dte()

In reply to Suravee Suthikulpanit

Take struct amd_iommu explicitly so DTE invalidation is issued on the same IOMMU instance used to program the DTE. Also, flush each device in amd_iommu_change_top() immediately after updating its DTE.

---
 drivers/iommu/amd/iommu.c | 13 +++++--------
 1 file changed, 5 insertions(+), 8 deletions(-)
drivers/iommu/amd/iommu.c +5 -8
index dfb9268f3c4e..a45044e1d423 100644
@@ -76,7 +76,7 @@ static void set_dte_entry(struct amd_iommu *iommu,
 			  struct iommu_dev_data *dev_data,
 			  phys_addr_t top_paddr, unsigned int top_level);
 
-static int device_flush_dte(struct iommu_dev_data *dev_data);
+static int device_flush_dte(struct amd_iommu *iommu, struct iommu_dev_data *dev_data);
 
 static void amd_iommu_change_top(struct pt_iommu *iommu_table,
 				 phys_addr_t top_paddr, unsigned int top_level);
@@ -218,7 +218,7 @@ void amd_iommu_update_dte(struct amd_iommu *iommu,
 {
 	update_dte256(iommu, dev_data, new);
 	clone_aliases(iommu, dev_data->dev);
-	device_flush_dte(dev_data);
+	device_flush_dte(iommu, dev_data);
 	iommu_completion_wait(iommu);
 }
 
@@ -1824,9 +1824,8 @@ static int device_flush_dte_alias(struct pci_dev *pdev, u16 alias, void *data)
 /*
  * Command send function for invalidating a device table entry
  */
-static int device_flush_dte(struct iommu_dev_data *dev_data)
+static int device_flush_dte(struct amd_iommu *iommu, struct iommu_dev_data *dev_data)
 {
-	struct amd_iommu *iommu = get_amd_iommu_from_dev_data(dev_data);
 	struct pci_dev *pdev = NULL;
 	struct amd_iommu_pci_seg *pci_seg;
 	u16 alias;
@@ -2795,11 +2794,9 @@ static void amd_iommu_change_top(struct pt_iommu *iommu_table,
 		/* Update the HW references with the new level and top ptr */
 		set_dte_entry(iommu, dev_data, top_paddr, top_level);
 		clone_aliases(iommu, dev_data->dev);
+		device_flush_dte(iommu, dev_data);
 	}
 
-	list_for_each_entry(dev_data, &pdom->dev_list, list)
-		device_flush_dte(dev_data);
-
 	domain_flush_complete(pdom);
 }
 
@@ -3233,7 +3230,7 @@ static int amd_iommu_set_dirty_tracking(struct iommu_domain *domain,
 		spin_unlock(&dev_data->dte_lock);
 
 		/* Flush device DTE */
-		device_flush_dte(dev_data);
+		device_flush_dte(iommu, dev_data);
 		domain_flush = true;
 	}
 
Signature · 1 line
2.34.1
maintainer
#20 lore raw

Re: [PATCH v6 10/24] iommu/amd: Pass iommu to device_flush_dte()

Quoted 44 lines · Suravee Suthikulpanit

Take struct amd_iommu explicitly so DTE invalidation is issued on the same IOMMU instance used to program the DTE. Also, flush each device in amd_iommu_change_top() immediately after updating its DTE.

---
 drivers/iommu/amd/iommu.c | 13 +++++--------
 1 file changed, 5 insertions(+), 8 deletions(-)
drivers/iommu/amd/iommu.c +3 -4
index dfb9268f3c4e..a45044e1d423 100644
@@ -76,7 +76,7 @@ static void set_dte_entry(struct amd_iommu *iommu,
 			  struct iommu_dev_data *dev_data,
 			  phys_addr_t top_paddr, unsigned int top_level);
 
-static int device_flush_dte(struct iommu_dev_data *dev_data);
+static int device_flush_dte(struct amd_iommu *iommu, struct iommu_dev_data *dev_data);
 
 static void amd_iommu_change_top(struct pt_iommu *iommu_table,
 				 phys_addr_t top_paddr, unsigned int top_level);
@@ -218,7 +218,7 @@ void amd_iommu_update_dte(struct amd_iommu *iommu,
 {
 	update_dte256(iommu, dev_data, new);
 	clone_aliases(iommu, dev_data->dev);
-	device_flush_dte(dev_data);
+	device_flush_dte(iommu, dev_data);
 	iommu_completion_wait(iommu);
 }
 
@@ -1824,9 +1824,8 @@ static int device_flush_dte_alias(struct pci_dev *pdev, u16 alias, void *data)
 /*
  * Command send function for invalidating a device table entry
  */
-static int device_flush_dte(struct iommu_dev_data *dev_data)
+static int device_flush_dte(struct amd_iommu *iommu, struct iommu_dev_data *dev_data)
 {
-	struct amd_iommu *iommu = get_amd_iommu_from_dev_data(dev_data);
 	struct pci_dev *pdev = NULL;
 	struct amd_iommu_pci_seg *pci_seg;
 	u16 alias;

drivers/iommu/amd/iommu.c static void amd_iommu_change_top(struct pt_iommu *iommu_table, · line 2797

Quoted 4 lines
drivers/iommu/amd/iommu.c +1 -0
@@ -2795,11 +2794,9 @@ static void amd_iommu_change_top(struct pt_iommu *iommu_table,
 		/* Update the HW references with the new level and top ptr */
 		set_dte_entry(iommu, dev_data, top_paddr, top_level);
 		clone_aliases(iommu, dev_data->dev);
+		device_flush_dte(iommu, dev_data);

May be an existing issue.

set_dte_entry -> amd_iommu_update_dte() already handles clone_aliases and dte_flush.

-Vasant

author reviewer
#21 lore raw

PATCH v6 11/24 iommu/amd: Pass iommu and devid to amd_iommu_make_clear_dte()

In reply to Suravee Suthikulpanit

Take struct amd_iommu and device ID explicitly instead of deriving the IOMMU from struct iommu_dev_data.

---
 drivers/iommu/amd/amd_iommu.h | 6 +++---
 drivers/iommu/amd/iommu.c     | 4 ++--
 drivers/iommu/amd/nested.c    | 2 +-
 3 files changed, 6 insertions(+), 6 deletions(-)
drivers/iommu/amd/amd_iommu.h +3 -3
index a0d5d7e34020..9cc5da745a73 100644
@@ -210,10 +210,10 @@ void amd_iommu_update_dte(struct amd_iommu *iommu,
 			  struct dev_table_entry *new);
 
 static inline void
-amd_iommu_make_clear_dte(struct iommu_dev_data *dev_data, struct dev_table_entry *new)
+amd_iommu_make_clear_dte(struct amd_iommu *iommu, u16 devid,
+			 struct dev_table_entry *new)
 {
 	struct dev_table_entry *initial_dte;
-	struct amd_iommu *iommu = get_amd_iommu_from_dev(dev_data->dev);
 
 	/* All existing DTE must have V bit set */
 	new->data128[0] = DTE_FLAG_V;
@@ -223,7 +223,7 @@ amd_iommu_make_clear_dte(struct iommu_dev_data *dev_data, struct dev_table_entry
 	 * Restore cached persistent DTE bits, which can be set by information
 	 * in IVRS table. See set_dev_entry_from_acpi().
 	 */
-	initial_dte = amd_iommu_get_ivhd_dte_flags(iommu->pci_seg->id, dev_data->devid);
+	initial_dte = amd_iommu_get_ivhd_dte_flags(iommu->pci_seg->id, devid);
 	if (initial_dte) {
 		new->data128[0] |= initial_dte->data128[0];
 		new->data128[1] |= initial_dte->data128[1];
drivers/iommu/amd/iommu.c +2 -2
index a45044e1d423..d4de87ded457 100644
@@ -2347,7 +2347,7 @@ static void set_dte_entry(struct amd_iommu *iommu,
 	struct gcr3_tbl_info *gcr3_info = &dev_data->gcr3_info;
 	struct dev_table_entry *dte = &get_dev_table(iommu)[dev_data->devid];
 
-	amd_iommu_make_clear_dte(dev_data, &new);
+	amd_iommu_make_clear_dte(iommu, dev_data->devid, &new);
 
 	old_domid = READ_ONCE(dte->data[1]) & DTE_DOMID_MASK;
 	if (gcr3_info->gcr3_tbl)
@@ -2379,7 +2379,7 @@ static void clear_dte_entry(struct amd_iommu *iommu, struct iommu_dev_data *dev_
 {
 	struct dev_table_entry new = {};
 
-	amd_iommu_make_clear_dte(dev_data, &new);
+	amd_iommu_make_clear_dte(iommu, dev_data->devid, &new);
 	amd_iommu_update_dte(iommu, dev_data, &new);
 }
 
drivers/iommu/amd/nested.c +1 -1
index f1c7987fc585..8bb0d1596c6c 100644
@@ -205,7 +205,7 @@ static void set_dte_nested(struct amd_iommu *iommu, struct iommu_domain *dom,
 		return;
 
 	parent = ndom->viommu->parent;
-	amd_iommu_make_clear_dte(dev_data, new);
+	amd_iommu_make_clear_dte(iommu, dev_data->devid, new);
 
 	/* Retrieve the current pagetable info via the IOMMU PT API. */
 	pt_iommu_amdv1_hw_info(&parent->amdv1, &pt_info);
Signature · 1 line
2.34.1
author reviewer
#22 lore raw

PATCH v6 12/24 iommu/amd: Store per-segment iommu_dev_data in an xarray

In reply to Suravee Suthikulpanit

Replace the per-segment llist of iommu_dev_data with an xarray keyed by devid so lookup is O(1). These objects stay immortal for the life of the PCI segment: amd_iommu_release_device() keeps them for replug, and IRQ/DTE paths look them up locklessly via xa_load().

Same-devid intern on xa_cmpxchg() covers replug and racing find_dev_data(). Aliases still get separate keys. Intern remains local to iommu.c; only the lookup table changes.

---
 drivers/iommu/amd/amd_iommu_types.h |  6 ++---
 drivers/iommu/amd/init.c            |  4 +++-
 drivers/iommu/amd/iommu.c           | 34 +++++++++++++++--------------
 3 files changed, 24 insertions(+), 20 deletions(-)
drivers/iommu/amd/amd_iommu_types.h +3 -3
index 9a90b8fe0fe3..d02f49a39a90 100644
@@ -22,6 +22,7 @@
 #include <linux/irqreturn.h>
 #include <linux/generic_pt/iommu.h>
 #include <linux/idr.h>
+#include <linux/xarray.h>
 
 #include <uapi/linux/iommufd.h>
 
@@ -605,8 +606,8 @@ struct amd_iommu_pci_seg {
 	/* List with all PCI segments in the system */
 	struct list_head list;
 
-	/* List of all available dev_data structures */
-	struct llist_head dev_data_list;
+	/* Immortal iommu_dev_data objects keyed by devid */
+	struct xarray dev_data_xa;
 
 	/* PCI segment number */
 	u16 id;
@@ -861,7 +862,6 @@ struct iommu_dev_data {
 	spinlock_t dte_lock;              /* DTE lock for 256-bit access */
 
 	struct list_head list;		  /* For domain->dev_list */
-	struct llist_node dev_data_list;  /* For global dev_data_list */
 	struct protection_domain *domain; /* Domain the device is bound to */
 	struct gcr3_tbl_info gcr3_info;   /* Per-device GCR3 table */
 	struct device *dev;
drivers/iommu/amd/init.c +3 -1
index d34f991dee24..e73139ba7d91 100644
@@ -1744,7 +1744,7 @@ static struct amd_iommu_pci_seg *__init alloc_pci_segment(u16 id,
 		    SZ_4K);
 
 	pci_seg->id = id;
-	init_llist_head(&pci_seg->dev_data_list);
+	xa_init(&pci_seg->dev_data_xa);
 	INIT_LIST_HEAD(&pci_seg->unity_map);
 	list_add_tail(&pci_seg->list, &amd_iommu_pci_seg_list);
 
@@ -1763,6 +1763,7 @@ static struct amd_iommu_pci_seg *__init alloc_pci_segment(u16 id,
 	free_dev_table(pci_seg);
 err_free_pci_seg:
 	list_del(&pci_seg->list);
+	xa_destroy(&pci_seg->dev_data_xa);
 	kfree(pci_seg);
 	return NULL;
 }
@@ -1786,6 +1787,7 @@ static void __init free_pci_segments(void)
 
 	for_each_pci_segment_safe(pci_seg, next) {
 		list_del(&pci_seg->list);
+		xa_destroy(&pci_seg->dev_data_xa);
 		free_irq_lookup_table(pci_seg);
 		free_rlookup_table(pci_seg);
 		free_alias_table(pci_seg);
drivers/iommu/amd/iommu.c +18 -16
index d4de87ded457..f9b49df4b90c 100644
@@ -390,9 +390,14 @@ static struct amd_iommu *rlookup_amd_iommu(struct device *dev)
 	return __rlookup_amd_iommu(seg, PCI_SBDF_TO_DEVID(devid));
 }
 
+/*
+ * Allocate an immortal per-devid object stored in pci_seg->dev_data_xa.
+ * These are never erased: amd_iommu_release_device() keeps them for
+ * replug, and IRQ/DTE paths look them up locklessly via xa_load().
+ */
 static struct iommu_dev_data *alloc_dev_data(struct amd_iommu *iommu, u16 devid)
 {
-	struct iommu_dev_data *dev_data;
+	struct iommu_dev_data *dev_data, *old;
 	struct amd_iommu_pci_seg *pci_seg = iommu->pci_seg;
 
 	dev_data = kzalloc_obj(*dev_data);
@@ -404,26 +409,23 @@ static struct iommu_dev_data *alloc_dev_data(struct amd_iommu *iommu, u16 devid)
 	dev_data->devid = devid;
 	ratelimit_default_init(&dev_data->rs);
 
-	llist_add(&dev_data->dev_data_list, &pci_seg->dev_data_list);
+	old = xa_cmpxchg(&pci_seg->dev_data_xa, devid, NULL, dev_data,
+			 GFP_KERNEL);
+	if (xa_is_err(old)) {
+		kfree(dev_data);
+		return NULL;
+	}
+	if (old) {
+		kfree(dev_data);
+		return old;
+	}
+
 	return dev_data;
 }
 
 struct iommu_dev_data *search_dev_data(struct amd_iommu *iommu, u16 devid)
 {
-	struct iommu_dev_data *dev_data;
-	struct llist_node *node;
-	struct amd_iommu_pci_seg *pci_seg = iommu->pci_seg;
-
-	if (llist_empty(&pci_seg->dev_data_list))
-		return NULL;
-
-	node = pci_seg->dev_data_list.first;
-	llist_for_each_entry(dev_data, node, dev_data_list) {
-		if (dev_data->devid == devid)
-			return dev_data;
-	}
-
-	return NULL;
+	return xa_load(&iommu->pci_seg->dev_data_xa, devid);
 }
 
 static int clone_alias(struct pci_dev *pdev_origin, u16 alias, void *data)
Signature · 1 line
2.34.1
maintainer
#23 lore raw

Re: [PATCH v6 12/24] iommu/amd: Store per-segment iommu_dev_data in an xarray

Quoted 9 lines · Suravee Suthikulpanit

Replace the per-segment llist of iommu_dev_data with an xarray keyed by devid so lookup is O(1). These objects stay immortal for the life of the PCI segment: amd_iommu_release_device() keeps them for replug, and IRQ/DTE paths look them up locklessly via xa_load().

Same-devid intern on xa_cmpxchg() covers replug and racing find_dev_data(). Aliases still get separate keys. Intern remains local to iommu.c; only the lookup table changes.

Quoted 6 lines
---
 drivers/iommu/amd/amd_iommu_types.h |  6 ++---
 drivers/iommu/amd/init.c            |  4 +++-
 drivers/iommu/amd/iommu.c           | 34 +++++++++++++++--------------
 3 files changed, 24 insertions(+), 20 deletions(-)

.../...

Quoted 12 lines

+ * Allocate an immortal per-devid object stored in pci_seg->dev_data_xa.
+ * These are never erased: amd_iommu_release_device() keeps them for
+ * replug, and IRQ/DTE paths look them up locklessly via xa_load().
+ */
static struct iommu_dev_data *alloc_dev_data(struct amd_iommu *iommu, u16 devid) {

drivers/iommu/amd/iommu.c +1 -1
-	struct iommu_dev_data *dev_data;
+	struct iommu_dev_data *dev_data, *old;
 	struct amd_iommu_pci_seg *pci_seg = iommu->pci_seg;
 
 	dev_data = kzalloc_obj(*dev_data);

drivers/iommu/amd/iommu.c static struct iommu_dev_data *alloc_dev_data(struct amd_iommu *iommu, u16 devid) · line 417

Quoted 10 lines
drivers/iommu/amd/iommu.c +6 -1
@@ -404,26 +409,23 @@ static struct iommu_dev_data *alloc_dev_data(struct amd_iommu *iommu, u16 devid)
 	dev_data->devid = devid;
 	ratelimit_default_init(&dev_data->rs);
 
-	llist_add(&dev_data->dev_data_list, &pci_seg->dev_data_list);
+	old = xa_cmpxchg(&pci_seg->dev_data_xa, devid, NULL, dev_data,
+			 GFP_KERNEL);
+	if (xa_is_err(old)) {> +		kfree(dev_data);
+		return NULL;
+	}
+	if (old) {

If old is valid means someone is already using devid. Allocation should fails if xa_array is not empty.

-Vasant

author reviewer
#24 lore raw

PATCH v6 13/24 iommu/amd: Program IOMMU DTE with the private IPA domain

In reply to Suravee Suthikulpanit

The IOMMU PCI function does not go through the normal device attach path, so it has no iommu_dev_data. Add amd_iommu_alloc_dev_data() and amd_iommu_free_dev_data() for a synthetic object that is not interned in pci_seg->dev_data_xa. Program that DTE with the private IPA domain's v1 page table so the IOMMU can DMA to its own vIOMMU backing store.

Leave the synthetic DTE off pdom->dev_list and with no struct device so clone_aliases() is skipped; it is only iommu->devid. Rewrite it from amd_iommu_change_top() when the IPA table grows so later high maps remain reachable.

Do this after viommu_private_space_init() so viommu_pdom exists. On teardown, clear the DTE only while CONTROL_CMDBUF_EN is set, then free the object. After disable_iommus() the INV_DTE flush would time out, so skip it and still free the object. Failed self DTE allocation unwinds through amd_viommu_uninit().

---
 drivers/iommu/amd/amd_iommu.h       |   4 ++
 drivers/iommu/amd/amd_iommu_types.h |   2 +
 drivers/iommu/amd/init.c            |   7 +-
 drivers/iommu/amd/iommu.c           | 102 +++++++++++++++++++++++++---
 drivers/iommu/amd/viommu.c          |  57 ++++++++++++++++
 5 files changed, 161 insertions(+), 11 deletions(-)
drivers/iommu/amd/amd_iommu.h +4 -0
index 9cc5da745a73..6ac64524208b 100644
@@ -50,6 +50,10 @@ extern u8 amd_iommu_hpt_vasize;
 extern unsigned long amd_iommu_pgsize_bitmap;
 extern bool amd_iommu_hatdis;
 
+struct iommu_dev_data *amd_iommu_alloc_dev_data(u16 devid);
+void amd_iommu_free_dev_data(struct amd_iommu *iommu,
+			     struct iommu_dev_data *dev_data);
+
 /* Protection domain ops */
 void amd_iommu_init_identity_domain(void);
 struct protection_domain *protection_domain_alloc(void);
drivers/iommu/amd/amd_iommu_types.h +2 -0
index d02f49a39a90..4b48ec8bacd6 100644
@@ -814,6 +814,8 @@ struct amd_iommu {
 	struct ida gid_ida;		 /* guest IDs for this IOMMU */
 
 	/* HW vIOMMU support */
+	/* Synthetic IOMMU-self DTE; not in pci_seg->dev_data_xa or pdom->dev_list */
+	struct iommu_dev_data *viommu_dev_data;
 	struct protection_domain *viommu_pdom;
 	void *viommu_priv_region[VIOMMU_PRIV_SUBREGION_CNT];
 };
drivers/iommu/amd/init.c +4 -3
index e73139ba7d91..96299f10b2e8 100644
@@ -1809,9 +1809,10 @@ static void __init free_iommu_one(struct amd_iommu *iommu)
 	free_sysfs(iommu);
 	/*
 	 * Tear down vIOMMU before buffers and MMIO are released.
-	 * IOTLB invalidation runs only while CONTROL_CMDBUF_EN is
-	 * set (amd_viommu_init() unwind). disable_iommus() clears
-	 * that bit first on the boot-failure path.
+	 * IOTLB invalidation and the self-DTE clear run only while
+	 * CONTROL_CMDBUF_EN is set (amd_viommu_init() unwind).
+	 * disable_iommus() clears that bit first on the boot-failure
+	 * path.
 	 */
 	amd_viommu_uninit(iommu);
 	free_iommu_buffers(iommu);
drivers/iommu/amd/iommu.c +94 -8

146 lines not on this page · raw message

drivers/iommu/amd/viommu.c +57 -0

74 lines not on this page · raw message

Signature · 1 line
2.34.1
maintainer
#25 lore raw

Re: [PATCH v6 13/24] iommu/amd: Program IOMMU DTE with the private IPA domain

Quoted 18 lines · Suravee Suthikulpanit

The IOMMU PCI function does not go through the normal device attach path, so it has no iommu_dev_data. Add amd_iommu_alloc_dev_data() and amd_iommu_free_dev_data() for a synthetic object that is not interned in pci_seg->dev_data_xa. Program that DTE with the private IPA domain's v1 page table so the IOMMU can DMA to its own vIOMMU backing store.

Leave the synthetic DTE off pdom->dev_list and with no struct device so clone_aliases() is skipped; it is only iommu->devid. Rewrite it from amd_iommu_change_top() when the IPA table grows so later high maps remain reachable.

Do this after viommu_private_space_init() so viommu_pdom exists. On teardown, clear the DTE only while CONTROL_CMDBUF_EN is set, then free the object. After disable_iommus() the INV_DTE flush would time out, so skip it and still free the object. Failed self DTE allocation unwinds through amd_viommu_uninit().

Quoted 9 lines
---
 drivers/iommu/amd/amd_iommu.h       |   4 ++
 drivers/iommu/amd/amd_iommu_types.h |   2 +
 drivers/iommu/amd/init.c            |   7 +-
 drivers/iommu/amd/iommu.c           | 102 +++++++++++++++++++++++++---
 drivers/iommu/amd/viommu.c          |  57 ++++++++++++++++
 5 files changed, 161 insertions(+), 11 deletions(-)

.../...

Quoted 7 lines
drivers/iommu/amd/viommu.c +7 -0
+
+static bool viommu_cmdbuf_enabled(struct amd_iommu *iommu);
+
+static void viommu_free_self_dev_data(struct amd_iommu *iommu)
+{
+	struct iommu_dev_data *dev_data = iommu->viommu_dev_data;
+	struct dev_table_entry new = {};

drivers/iommu/amd/viommu.c static void __init amd_viommu_vf_vfcntl_unmap(struct amd_iommu *iommu) · line 59

Quoted 10 lines
drivers/iommu/amd/viommu.c +10 -0
+
+	if (!dev_data)
+		return;
+
+	/*
+	 * INV_DTE needs the command buffer. Skip it after
+	 * disable_iommus() clears CONTROL_CMDBUF_EN. The init
+	 * unwind still flushes while the buffer is enabled.
+	 */
+	if (viommu_cmdbuf_enabled(iommu)) {

As mentioned in previous patch, do we really need this check?

Quoted 5 lines
drivers/iommu/amd/viommu.c +5 -0
+		amd_iommu_make_clear_dte(iommu, dev_data->devid, &new);
+		amd_iommu_update_dte(iommu, dev_data, &new);
+	}
+	amd_iommu_free_dev_data(iommu, dev_data);
+	iommu->viommu_dev_data = NULL;

drivers/iommu/amd/viommu.c static void __init amd_viommu_vf_vfcntl_unmap(struct amd_iommu *iommu) · line 74

Quoted 10 lines
drivers/iommu/amd/viommu.c +10 -0
+}
+
+static int viommu_alloc_self_dev_data(struct amd_iommu *iommu)
+{
+	struct protection_domain *pdom = iommu->viommu_pdom;
+	struct pt_iommu_amdv1_hw_info pt_info;
+	struct iommu_dev_data *dev_data;
+	struct dev_table_entry new = {};
+
+	dev_data = amd_iommu_alloc_dev_data(iommu->devid);

I am not getting why we can't call allc_dev_data? So that we can track these devid (including translation devid) in dev_data_xa. So that we can catch duplicate allocations?

drivers/iommu/amd/viommu.c static void __init amd_viommu_vf_vfcntl_unmap(struct amd_iommu *iommu) · line 84

Quoted 10 lines
drivers/iommu/amd/viommu.c +10 -0
+	if (!dev_data) {
+		pr_err("%s: Failed to allocate dev_data\n", __func__);
+		return -ENOMEM;
+	}
+	/*
+	 * Synthetic DTE for iommu->devid only: no struct device, so
+	 * amd_iommu_update_dte() skips clone_aliases(), and not on
+	 * pdom->dev_list (not an IOMMU-API attach).
+	 */
+	dev_data->dev = NULL;

I am not able to figureout why we cannot track iommu device structure here. Does it mess up anything? if we hook this to pdom->dev_list then we don't need special update_viommu_self_dte().

-Vasant

author reviewer
#26 lore raw

PATCH v6 14/24 iommu/amd: Add per-VM private IPA alloc/map helpers

In reply to Suravee Suthikulpanit

Guest device ID and guest domain ID tables use dedicated slots in the vIOMMU private address (IPA) region, indexed by guest ID (GID).

Add alloc_private_vm_region() and free_private_vm_region() to allocate backing pages, map them through viommu_pdom, and tear down with an unmap flush on VM destroy.

Charge GFP_KERNEL_ACCOUNT so the DevID/DomID pages count against the caller's memcg. iommufd already charges the vIOMMU object.

---
 drivers/iommu/amd/viommu.c | 39 ++++++++++++++++++++++++++++++++++++++
 1 file changed, 39 insertions(+)
drivers/iommu/amd/viommu.c +39 -0
index e9554a3e9591..d80f545c0648 100644
@@ -382,3 +382,42 @@ int __init amd_viommu_init(struct amd_iommu *iommu)
 	amd_viommu_uninit(iommu);
 	return ret;
 }
+
+static int __maybe_unused alloc_private_vm_region(struct amd_iommu *iommu, u64 **entry,
+						 u64 base, size_t size, u16 gid)
+{
+	int ret;
+	void *va = NULL;
+	u64 addr = base + (gid * size);
+
+	/* Charge the caller; boot IPA stays GFP_KERNEL. */
+	ret = viommu_priv_alloc_map(iommu, addr, size, GFP_KERNEL_ACCOUNT, &va);
+	if (ret) {
+		*entry = NULL;
+		return ret;
+	}
+
+	*entry = (u64 *)va;
+
+	pr_debug("%s: entry=%#llx(%#llx), addr=%#llx, size=%#lx\n", __func__,
+		 (unsigned long long)*entry, iommu_virt_to_phys(*entry), addr, size);
+
+	return 0;
+}
+
+static void __maybe_unused free_private_vm_region(struct amd_iommu *iommu, u64 **entry,
+						  u64 base, size_t size, u16 gid)
+{
+	u64 addr = base + (gid * size);
+
+	if (!iommu || !iommu->viommu_pdom || !*entry)
+		return;
+
+	pr_debug("%s: entry=%#llx(%#llx), base=%#llx, addr=%#llx, size=%#lx\n",
+		 __func__, (unsigned long long)*entry,
+		 iommu_virt_to_phys(*entry), base, addr, size);
+
+	viommu_priv_unmap_free(iommu, addr, size, *entry);
+
+	*entry = NULL;
+}
Signature · 1 line
2.34.1
author reviewer
#27 lore raw

PATCH v6 15/24 iommu/amd: Add helper functions to manage DevID / DomID mapping tables

In reply to Suravee Suthikulpanit

Introduce amd_viommu_init_one() and amd_viommu_uninit_one(). These functions are called during IOMMUFD vIOMMU initialize and destroy. Currently, it manages the IPA mapping for Device ID and Domain ID mapping tables.

---
 drivers/iommu/amd/amd_iommu_types.h |  3 ++
 drivers/iommu/amd/amd_viommu.h      |  4 ++
 drivers/iommu/amd/iommu.c           |  1 +
 drivers/iommu/amd/iommufd.c         |  9 +++-
 drivers/iommu/amd/viommu.c          | 64 +++++++++++++++++++++++++++++
 5 files changed, 80 insertions(+), 1 deletion(-)
drivers/iommu/amd/amd_iommu_types.h +3 -0
index 4b48ec8bacd6..fc8e1378c7ed 100644
@@ -544,6 +544,9 @@ struct amd_iommu_viommu {
 	 */
 	struct xarray gdomid_array;
 
+	u64 *devid_table;
+	u64 *domid_table;
+
 	/* Offset for mmap() of guest VF MMIO; set after iommufd_viommu_alloc_mmap(). */
 	unsigned long vfmmio_mmap_offset;
 };
drivers/iommu/amd/amd_viommu.h +4 -0
index 276f2126290f..580cad0f9f5a 100644
@@ -16,6 +16,10 @@ void __init amd_viommu_uninit(struct amd_iommu *iommu);
 
 u64 amd_viommu_get_vfmmio_addr(struct amd_iommu *iommu, u16 gid);
 
+int amd_viommu_init_one(struct amd_iommu *iommu, struct amd_iommu_viommu *viommu);
+
+void amd_viommu_uninit_one(struct amd_iommu *iommu, struct amd_iommu_viommu *viommu);
+
 #else
 
 /*
drivers/iommu/amd/iommu.c +1 -0
index edeaea48d434..b7073b0ca8b0 100644
@@ -43,6 +43,7 @@
 #include <linux/generic_pt/iommu.h>
 
 #include "amd_iommu.h"
+#include "amd_viommu.h"
 #include "iommufd.h"
 #include "../irq_remapping.h"
 #include "../iommu-pages.h"
drivers/iommu/amd/iommufd.c +8 -1
index 2e34491e3cf7..97de07d3ad10 100644
@@ -97,11 +97,15 @@ int amd_iommufd_viommu_init(struct iommufd_viommu *viommu, struct iommu_domain *
 	if (ret)
 		goto err_init;
 
+	ret = amd_viommu_init_one(iommu, aviommu);
+	if (ret)
+		goto err_init;
+
 	ret = iommu_copy_struct_to_user(user_data, &data,
 					IOMMU_VIOMMU_TYPE_AMD,
 					out_vfmmio_mmap_offset);
 	if (ret)
-		goto err_init;
+		goto err_init_one;
 
 	viommu->ops = &amd_viommu_ops;
 
@@ -110,6 +114,8 @@ int amd_iommufd_viommu_init(struct iommufd_viommu *viommu, struct iommu_domain *
 	spin_unlock_irqrestore(&pdom->lock, flags);
 
 	return 0;
+err_init_one:
+	amd_viommu_uninit_one(iommu, aviommu);
 err_init:
 	iommufd_viommu_destroy_mmap(&aviommu->core, aviommu->vfmmio_mmap_offset);
 err_mmap:
@@ -132,6 +138,7 @@ static void amd_iommufd_viommu_destroy(struct iommufd_viommu *viommu)
 	spin_unlock_irqrestore(&pdom->lock, flags);
 	xa_destroy(&aviommu->gdomid_array);
 	iommufd_viommu_destroy_mmap(&aviommu->core, aviommu->vfmmio_mmap_offset);
+	amd_viommu_uninit_one(iommu, aviommu);
 	amd_iommu_gid_free(iommu, aviommu->gid);
 }
 
drivers/iommu/amd/viommu.c +64 -0

73 lines not on this page · raw message

Signature · 1 line
2.34.1
author reviewer
#28 lore raw

PATCH v6 16/24 iommu/amd: Add IOMMUFD vDevice and DevID mapping

In reply to Suravee Suthikulpanit

IOMMUFD vDevice allocation supplies the guest device ID (virt_id). The AMD vIOMMU Device ID mapping table translates that gdev_id to the host device ID for virtualised IOMMU commands.

Add vdevice_init, which validates a PCI device and virt_id, then programs the mapping through VFCTRL via amd_viommu_set_device_mapping(). Get the host struct device via iommufd_vdevice_to_device(). Set vdev->destroy to restore an idle mapping when the vDevice is removed while the vIOMMU is still alive.

---
 drivers/iommu/amd/amd_iommu_types.h | 12 +++++
 drivers/iommu/amd/amd_viommu.h      | 10 ++++
 drivers/iommu/amd/iommufd.c         | 52 +++++++++++++++++++
 drivers/iommu/amd/viommu.c          | 79 ++++++++++++++++++++++++++++-
 4 files changed, 152 insertions(+), 1 deletion(-)
drivers/iommu/amd/amd_iommu_types.h +12 -0
index fc8e1378c7ed..07eaeeb72e38 100644
@@ -547,6 +547,14 @@ struct amd_iommu_viommu {
 	u64 *devid_table;
 	u64 *domid_table;
 
+	/*
+	 * Serializes VFCTRL Guest DID map CONTROL0/CONTROL1 doorbells
+	 * (DevID and DomID table updates) for this gid. Those registers
+	 * are shared by every vDevice on this vIOMMU; igroup locks are
+	 * not. Do not nest with trans_devid_lock.
+	 */
+	struct mutex vfctrl_lock;
+
 	/* Offset for mmap() of guest VF MMIO; set after iommufd_viommu_alloc_mmap(). */
 	unsigned long vfmmio_mmap_offset;
 };
@@ -1113,6 +1121,10 @@ struct amd_irte_ops {
 	void (*clear_allocated)(struct irq_remap_table *, int);
 };
 
+struct amd_iommu_vdevice {
+	struct iommufd_vdevice core;
+};
+
 #ifdef CONFIG_IRQ_REMAP
 extern struct amd_irte_ops irte_32_ops;
 extern struct amd_irte_ops irte_128_ops;
drivers/iommu/amd/amd_viommu.h +10 -0
index 580cad0f9f5a..1022ae20c170 100644
@@ -8,6 +8,9 @@
 
 struct amd_iommu;
 
+/* 16-bit guest DevID index; not VIOMMU_MAX_GID. */
+#define VIOMMU_MAX_GDEVID	0xFFFF
+
 #if IS_ENABLED(CONFIG_AMD_IOMMU_IOMMUFD)
 
 int amd_viommu_init(struct amd_iommu *iommu);
@@ -20,6 +23,8 @@ int amd_viommu_init_one(struct amd_iommu *iommu, struct amd_iommu_viommu *viommu
 
 void amd_viommu_uninit_one(struct amd_iommu *iommu, struct amd_iommu_viommu *viommu);
 
+void amd_viommu_set_device_mapping(struct amd_iommu_viommu *aviommu,
+				   u16 hdev_id, u16 gdev_id);
 #else
 
 /*
@@ -41,6 +46,11 @@ static inline u64 amd_viommu_get_vfmmio_addr(struct amd_iommu *iommu, u16 gid)
 	return 0;
 }
 
+static inline void amd_viommu_set_device_mapping(struct amd_iommu_viommu *aviommu,
+						 u16 hdev_id, u16 gdev_id)
+{
+}
+
 #endif /* CONFIG_AMD_IOMMU_IOMMUFD */
 
 #endif /* AMD_VIOMMU_H */
drivers/iommu/amd/iommufd.c +52 -0

74 lines not on this page · raw message

drivers/iommu/amd/viommu.c +78 -1

103 lines not on this page · raw message

Signature · 1 line
2.34.1
maintainer Reviewed-by on v6 16/24
#29 lore raw

Re: [PATCH v6 16/24] iommu/amd: Add IOMMUFD vDevice and DevID mapping

Quoted 8 lines

[ ... 128 lines skipped ... ]

drivers/iommu/amd/iommufd.c +7 -0
+/*
+ * Called from drivers/iommu/iommufd/viommu.c: iommufd_vdevice_alloc_ioctl()
+ */
+static int _amd_viommu_vdevice_init(struct iommufd_vdevice *vdev)
+{
+	struct device *dev = iommufd_vdevice_to_device(vdev);
+	struct pci_dev *pdev;

drivers/iommu/amd/iommufd.c static void amd_iommufd_viommu_destroy(struct iommufd_viommu *viommu) · line 182

Quoted 10 lines
drivers/iommu/amd/iommufd.c +10 -0
+	struct iommufd_viommu *viommu = vdev->viommu;
+	struct amd_iommu_viommu *aviommu = container_of(viommu, struct amd_iommu_viommu, core);
+
+	if (!dev_is_pci(dev))
+		return -EINVAL;
+	pdev = to_pci_dev(dev);
+
+	if (vdev->virt_id > VIOMMU_MAX_GDEVID) {
+		pr_err("%s: Invalid virtual ID (virt_id=%#llx)\n", __func__, vdev->virt_id);
+		return -EINVAL;

Do not print on user triggerable flows

drivers/iommu/amd/iommufd.c static void amd_iommufd_viommu_destroy(struct iommufd_viommu *viommu) · line 189

Quoted 7 lines
drivers/iommu/amd/iommufd.c +7 -0
+	}
+
+	pr_debug("%s: gid=%#x, hdev_id=%#x, gdev_id=%#llx\n", __func__,
+		 aviommu->gid, pci_dev_id(pdev), vdev->virt_id);
+
+	amd_viommu_set_device_mapping(aviommu, pci_dev_id(pdev),
+				      (u16)vdev->virt_id);

I spotted several pointless casts like this

Anyhow this looks like the right use of a vdevice to me and broadly matches what arm vcmdq is doing

Signature · 1 line
Jason
maintainer
#30 lore raw

Re: [PATCH v6 16/24] iommu/amd: Add IOMMUFD vDevice and DevID mapping

In reply to Suravee Suthikulpanit
Quoted 10 lines · Suravee Suthikulpanit

IOMMUFD vDevice allocation supplies the guest device ID (virt_id). The AMD vIOMMU Device ID mapping table translates that gdev_id to the host device ID for virtualised IOMMU commands.

Add vdevice_init, which validates a PCI device and virt_id, then programs the mapping through VFCTRL via amd_viommu_set_device_mapping(). Get the host struct device via iommufd_vdevice_to_device(). Set vdev->destroy to restore an idle mapping when the vDevice is removed while the vIOMMU is still alive.

Quoted 8 lines
---
 drivers/iommu/amd/amd_iommu_types.h | 12 +++++
 drivers/iommu/amd/amd_viommu.h      | 10 ++++
 drivers/iommu/amd/iommufd.c         | 52 +++++++++++++++++++
 drivers/iommu/amd/viommu.c          | 79 ++++++++++++++++++++++++++++-
 4 files changed, 152 insertions(+), 1 deletion(-)

.../...

drivers/iommu/amd/iommufd.c static void amd_iommufd_viommu_destroy(struct iommufd_viommu *viommu) · line 154

Quoted 9 lines
drivers/iommu/amd/iommufd.c +9 -0
+}
+
+/*
+ * Called from drivers/iommu/iommufd/viommu.c: iommufd_vdevice_abort()
+ *
+ * Restore an idle DevID map entry for this virt_id. Every DevID mapping
+ * table slot must have V=1 for both active and non-active devices. A
+ * non-valid entry raises INVALID_DEVICE_REQUEST when the IOMMU processes
+ * INVALIDATE_IOMMU_ALL. Use host device ID 0: that RID is the IOMMU PCI

We don't support INVALIDATE_IOMMU_ALL command inside guest. But with current kernel guest will endup walking entire devid/dom Id range and hitting INVALID_DEVICE_REQUEST.

Quoted 99 lines

+ * function and does not normally host an endpoint.
+ */

drivers/iommu/amd/iommufd.c +38 -0
+static void _amd_viommu_vdevice_destroy(struct iommufd_vdevice *vdev)
+{
+	struct iommufd_viommu *viommu = vdev->viommu;
+	struct amd_iommu_viommu *aviommu =
+		container_of(viommu, struct amd_iommu_viommu, core);
+
+	amd_viommu_set_device_mapping(aviommu, 0, (u16)vdev->virt_id);
+}
+
+/*
+ * Called from drivers/iommu/iommufd/viommu.c: iommufd_vdevice_alloc_ioctl()
+ */
+static int _amd_viommu_vdevice_init(struct iommufd_vdevice *vdev)
+{
+	struct device *dev = iommufd_vdevice_to_device(vdev);
+	struct pci_dev *pdev;
+	struct iommufd_viommu *viommu = vdev->viommu;
+	struct amd_iommu_viommu *aviommu = container_of(viommu, struct amd_iommu_viommu, core);
+
+	if (!dev_is_pci(dev))
+		return -EINVAL;
+	pdev = to_pci_dev(dev);
+
+	if (vdev->virt_id > VIOMMU_MAX_GDEVID) {
+		pr_err("%s: Invalid virtual ID (virt_id=%#llx)\n", __func__, vdev->virt_id);
+		return -EINVAL;
+	}
+
+	pr_debug("%s: gid=%#x, hdev_id=%#x, gdev_id=%#llx\n", __func__,
+		 aviommu->gid, pci_dev_id(pdev), vdev->virt_id);
+
+	amd_viommu_set_device_mapping(aviommu, pci_dev_id(pdev),
+				      (u16)vdev->virt_id);
+	vdev->destroy = _amd_viommu_vdevice_destroy;
+
+	return 0;
 }
 
 /*
@@ -149,4 +199,6 @@ static void amd_iommufd_viommu_destroy(struct iommufd_viommu *viommu)
 static const struct iommufd_viommu_ops amd_viommu_ops = {
 	.alloc_domain_nested = amd_iommu_alloc_domain_nested,
 	.destroy = amd_iommufd_viommu_destroy,
+	.vdevice_size = VDEVICE_STRUCT_SIZE(struct amd_iommu_vdevice, core),
+	.vdevice_init = _amd_viommu_vdevice_init,
 };
drivers/iommu/amd/viommu.c +21 -1
index b160c70f3804..e263d63afe3d 100644
@@ -6,6 +6,7 @@
 #define pr_fmt(fmt)     "AMD-Vi: " fmt
 #define dev_fmt(fmt)    pr_fmt(fmt)
 
+#include <linux/delay.h>
 #include <linux/iommu.h>
 #include <linux/iommufd.h>
 #include <uapi/linux/iommufd.h>
@@ -29,7 +30,6 @@
  * VIOMMU_MAX_GDEVID / VIOMMU_MAX_GDOMID are 16-bit guest DevID
  * and DomID table indices (hardware mapping-table width), not GID.
  */
-#define VIOMMU_MAX_GDEVID		0xFFFF
 #define VIOMMU_DEVID_MAPPING_BASE	0x1000000000ULL
 #define VIOMMU_DEVID_MAPPING_ENTRY_SIZE	(1 << 20)
 
@@ -45,6 +45,8 @@ static_assert(VIOMMU_DEVID_MAPPING_ENTRY_SIZE >=
 static_assert(VIOMMU_DOMID_MAPPING_ENTRY_SIZE >=
 	      (VIOMMU_MAX_GDOMID + 1ULL) * sizeof(u64));
 
+#define VIOMMU_VFCTRL_GUEST_DID_MAP_CONTROL0_OFFSET	0x00
+
 static void __init amd_viommu_vf_vfcntl_unmap(struct amd_iommu *iommu)
 {
 	if (iommu->vfctrl_base) {
@@ -448,6 +450,81 @@ static void __maybe_unused free_private_vm_region(struct amd_iommu *iommu, u64 *
 	*entry = NULL;
 }
 
+#define DEVID_ENTRY_GDEVID_MASK		GENMASK_ULL(61, 46)
+#define DEVID_ENTRY_QUEUEID_MASK	GENMASK_ULL(45, 30)
+#define DEVID_ENTRY_HDEVID_MASK		GENMASK_ULL(29, 14)
+#define DEVID_ENTRY_WRITE		BIT_ULL(63)
+#define DEVID_ENTRY_VALID		BIT_ULL(0)
+
+/*
+ * CONTROL0/CONTROL1 bit 63 (WRITE) is a doorbell: hardware copies one
+ * entry into the IPA-backed table and clears WRITE when that DMA write
+ * retires. This is not a command-buffer operation;
+ * iommu_completion_wait() does not drain it. A readq() of CONTROL_EN
+ * is also unrelated. Poll WRITE=0 before ringing a new doorbell and
+ * after each write so the table update is retired before the caller
+ * proceeds (DTE commit, unmap/free).
+ */
+static void vfctrl_wait_write_clear(struct amd_iommu *iommu, u16 gid,
+				    unsigned int offset)
+{

drivers/iommu/amd/viommu.c static void __maybe_unused free_private_vm_region(struct amd_iommu *iommu, u64 * · line 480

Quoted 10 lines
drivers/iommu/amd/viommu.c +10 -0
+	u8 __iomem *reg = VIOMMU_VFCTRL_MMIO_BASE(iommu, gid) + offset;
+	int i;
+
+	for (i = 0; i < LOOP_TIMEOUT; i++) {
+		if (!(readq(reg) & DEVID_ENTRY_WRITE))
+			return;
+		udelay(1);
+	}
+	pr_err("%s: WRITE timeout gid=%#x offset=%#x val=%#llx\n",
+	       __func__, gid, offset, readq(reg));

Shouldn't -ETIMEDOUT propagate so that vDevice allocation fails when the mapping was never committed?

drivers/iommu/amd/viommu.c static void __maybe_unused free_private_vm_region(struct amd_iommu *iommu, u64 * · line 490

Quoted 10 lines
drivers/iommu/amd/viommu.c +10 -0
+}
+
+/*
+ * Guest DevID table update via VFCTRL CONTROL0.
+ *
+ * TODO: The driver does not support QueueID yet,
+ *       so current callers pass 0. This will be
+ *       enhanced in the future.
+ */
+static void set_device_mapping_unlocked(struct amd_iommu *iommu, u16 gid,

You mean mapping_locked?

-Vasant

author reviewer
#31 lore raw

PATCH v6 17/24 iommu/amd: Program nested DTE and DomID map on attach

In reply to Suravee Suthikulpanit

On nested attach, the host DTE must enable vIOMMU and carry the guest device and guest IDs from the vDevice. Look up gdev_id with iommufd_viommu_get_vdev_id() and fail the attach if that lookup fails.

Program the Domain ID mapping table through VFCTRL so hardware can translate gdom_id to hdom_id. Write CONTROL1 before the DTE is live with DTE_VIOMMU_EN so guest DMA cannot resolve against the idle nest-parent map.

DomID CONTROL1 uses the same per-vIOMMU vfctrl_lock as DevID CONTROL0. Hold that lock across CONTROL1 and the DTE commit, and on last-ref nested_domain_free() take it before gdomid_array so idle restore cannot clobber a concurrent alloc+attach.

CONTROL1 WRITE uses the same doorbell as CONTROL0. Poll WRITE until it clears; iommu_completion_wait() does not drain the table DMA.

On the last nested_domain_free() for a gdom_id, restore the idle DomID map (nest parent, V=1) before returning hdom_id to the IDA.

---
 drivers/iommu/amd/amd_iommu_types.h | 10 ++++
 drivers/iommu/amd/amd_viommu.h      |  8 ++++
 drivers/iommu/amd/nested.c          | 74 +++++++++++++++++++++++++----
 drivers/iommu/amd/viommu.c          | 41 ++++++++++++++++
 4 files changed, 125 insertions(+), 8 deletions(-)
drivers/iommu/amd/amd_iommu_types.h +10 -0
index 07eaeeb72e38..b5d1b23791da 100644
@@ -382,6 +382,11 @@
 #define DTE_GPT_LEVEL_SHIFT	54
 #define DTE_GPT_LEVEL_MASK	GENMASK_ULL(55, 54)
 
+/* vIOMMU bit fields */
+#define DTE_VIOMMU_EN_SHIFT		15
+#define DTE_VIOMMU_GDEVICEID_MASK	GENMASK_ULL(31, 16)
+#define DTE_VIOMMU_GUESTID_MASK		GENMASK_ULL(47, 32)
+
 #define GCR3_VALID		0x01ULL
 
 /* DTE[128:179] | DTE[184:191] */
@@ -552,6 +557,11 @@ struct amd_iommu_viommu {
 	 * (DevID and DomID table updates) for this gid. Those registers
 	 * are shared by every vDevice on this vIOMMU; igroup locks are
 	 * not. Do not nest with trans_devid_lock.
+	 *
+	 * nested_domain_free() last-ref takes this before
+	 * gdomid_array's xa_lock so CONTROL1 idle restore cannot race
+	 * a concurrent alloc+attach. Attach holds it across CONTROL1
+	 * and the DTE commit.
 	 */
 	struct mutex vfctrl_lock;
 
drivers/iommu/amd/amd_viommu.h +8 -0
index 1022ae20c170..30227a5e4bfa 100644
@@ -23,6 +23,9 @@ int amd_viommu_init_one(struct amd_iommu *iommu, struct amd_iommu_viommu *viommu
 
 void amd_viommu_uninit_one(struct amd_iommu *iommu, struct amd_iommu_viommu *viommu);
 
+void amd_viommu_domain_id_update_locked(struct amd_iommu_viommu *aviommu,
+					u16 hdom_id, u16 gdom_id);
+
 void amd_viommu_set_device_mapping(struct amd_iommu_viommu *aviommu,
 				   u16 hdev_id, u16 gdev_id);
 #else
@@ -51,6 +54,11 @@ static inline void amd_viommu_set_device_mapping(struct amd_iommu_viommu *aviomm
 {
 }
 
+static inline void amd_viommu_domain_id_update_locked(struct amd_iommu_viommu *aviommu,
+						      u16 hdom_id, u16 gdom_id)
+{
+}
+
 #endif /* CONFIG_AMD_IOMMU_IOMMUFD */
 
 #endif /* AMD_VIOMMU_H */
drivers/iommu/amd/nested.c +66 -8

140 lines not on this page · raw message

drivers/iommu/amd/viommu.c +41 -0

53 lines not on this page · raw message

Signature · 1 line
2.34.1
maintainer
#32 lore raw

Re: [PATCH v6 17/24] iommu/amd: Program nested DTE and DomID map on attach

Quoted 11 lines

[ ... 150 lines skipped ... ]

drivers/iommu/amd/nested.c +4 -1
@@ -251,10 +268,23 @@ static int nested_attach_device(struct iommu_domain *dom, struct device *dev,
 
 	mutex_lock(&dev_data->mutex);
 
-	set_dte_nested(iommu, dom, dev_data, &new);
+	ret = set_dte_nested(iommu, dom, dev_data, &new);
+	if (ret)
+		goto out_err;
 
+	/*

drivers/iommu/amd/nested.c static int nested_attach_device(struct iommu_domain *dom, struct device *dev, · line 285

Quoted 10 lines
drivers/iommu/amd/nested.c +9 -0
+	 * Program gdom_id -> hdom_id before the DTE is live with
+	 * DTE_VIOMMU_EN. Hold vfctrl_lock across both so a concurrent
+	 * last-ref free cannot restore the idle map in between.
+	 */
+	mutex_lock(&ndom->viommu->vfctrl_lock);
+	amd_viommu_domain_id_update_locked(ndom->viommu,
+					   ndom->gdom_info->hdom_id,
+					   ndom->gdom_id);
 	amd_iommu_update_dte(iommu, dev_data, &new);
+	mutex_unlock(&ndom->viommu->vfctrl_lock);

I didn't find an explanation for why this is asymetric, or what the overall lifecycle of the mapping is.

I would have expected the maintaince of the HW table to be aligned to the gdom_info lifecycle. Establish a gdom/hdom mapping when the gdom_info is allocated, free it when it is freed.

Instead we set it on the first device to attach and then free it when the first sharing nested domain is freed?

That doesn't seem right?

Quoted 27 lines

[ ... 24 lines skipped ... ]

(quoted diff) +14 -4
@@ -282,14 +320,34 @@ static void nested_domain_free(struct iommu_domain *dom)
 			    ndom->gdom_info, NULL, GFP_ATOMIC);
 
 	xa_unlock_irqrestore(&aviommu->gdomid_array, irqflags);
-	if (WARN_ON(!curr || xa_err(curr)))
+	if (WARN_ON(!curr || xa_err(curr))) {
+		mutex_unlock(&aviommu->vfctrl_lock);
 		return;
+	}
 
-	/* success */
 	pr_debug("%s: Free gdom_id=%#x, hdom_id=%#x\n",
-		__func__, ndom->gdom_id, curr->hdom_id);
+		 __func__, ndom->gdom_id, curr->hdom_id);
 
-	amd_iommu_pdom_id_free(ndom->gdom_info->hdom_id);
+	/*
+	 * Restore the idle DomID map before releasing hdom_id. Every
+	 * slot stays V=1; unknown mapping raises an event. Idle is the
+	 * nest parent CONTROL1 map, same as vIOMMU init prefill.
+	 * amd_iommu_reset_vmmio() only resets guest MMIO.
+	 *
+	 * Guest INVALIDATE_IOMMU_ALL walks the HW table, not
+	 * gdomid_array. The pdom_ids IDA is global, so a stale V=1
+	 * entry can invalidate a recycled hdom_id that now belongs to
+	 * another domain.

Review static void nested_domain_free(struct iommu_domain *dom) · line 350

Quoted 10 lines
(quoted diff) +10 -0
+	 *
+	 * Detach does not clear this slot: hdom_id stays allocated
+	 * until this last free, so a lingering gdom_id -> hdom_id
+	 * map cannot point at a recycled ID.
+	 */
+	amd_viommu_domain_id_update_locked(aviommu, aviommu->parent->id,
+					   ndom->gdom_id);
+	mutex_unlock(&aviommu->vfctrl_lock);
+
+	amd_iommu_pdom_id_free(curr->hdom_id);

What is the protocol for flushing the hdom_id from the IOTLB?

I can't remember how AMD works, but generally it is a good idea for IDs in the IDR to be known to be cleared out from the IOTLB so they are always clean. I don't see something like that here. Is it OK?

drivers/iommu/amd/viommu.c void amd_viommu_set_device_mapping(struct amd_iommu_viommu *aviommu, · line 566

Quoted 9 lines

[ ... 49 lines skipped ... ]

drivers/iommu/amd/viommu.c +8 -0
+void amd_viommu_domain_id_update_locked(struct amd_iommu_viommu *aviommu,
+					u16 hdom_id, u16 gdom_id)
+{
+	struct amd_iommu *iommu =
+		container_of(aviommu->core.iommu_dev, struct amd_iommu, iommu);
+
+	lockdep_assert_held(&aviommu->vfctrl_lock);
+	domain_id_update_unlocked(iommu, aviommu->gid, hdom_id, gdom_id);

A _locked function calls an _unlocked function? That's confusing..

Signature · 1 line
Jason
maintainer
#33 lore raw

Re: [PATCH v6 17/24] iommu/amd: Program nested DTE and DomID map on attach

In reply to Suravee Suthikulpanit

drivers/iommu/amd/amd_iommu_types.h

Quoted 29 lines · Suravee Suthikulpanit

On nested attach, the host DTE must enable vIOMMU and carry the guest device and guest IDs from the vDevice. Look up gdev_id with iommufd_viommu_get_vdev_id() and fail the attach if that lookup fails.

Program the Domain ID mapping table through VFCTRL so hardware can translate gdom_id to hdom_id. Write CONTROL1 before the DTE is live with DTE_VIOMMU_EN so guest DMA cannot resolve against the idle nest-parent map.

DomID CONTROL1 uses the same per-vIOMMU vfctrl_lock as DevID CONTROL0. Hold that lock across CONTROL1 and the DTE commit, and on last-ref nested_domain_free() take it before gdomid_array so idle restore cannot clobber a concurrent alloc+attach.

CONTROL1 WRITE uses the same doorbell as CONTROL0. Poll WRITE until it clears; iommu_completion_wait() does not drain the table DMA.

On the last nested_domain_free() for a gdom_id, restore the idle DomID map (nest parent, V=1) before returning hdom_id to the IDA.

---
 drivers/iommu/amd/amd_iommu_types.h | 10 ++++
 drivers/iommu/amd/amd_viommu.h      |  8 ++++
 drivers/iommu/amd/nested.c          | 74 +++++++++++++++++++++++++----
 drivers/iommu/amd/viommu.c          | 41 ++++++++++++++++
 4 files changed, 125 insertions(+), 8 deletions(-)
drivers/iommu/amd/amd_iommu_types.h +0 -0

.../...

Quoted 15 lines
drivers/iommu/amd/amd_viommu.h +3 -0
index 1022ae20c170..30227a5e4bfa 100644
@@ -23,6 +23,9 @@ int amd_viommu_init_one(struct amd_iommu *iommu, struct amd_iommu_viommu *viommu
 
 void amd_viommu_uninit_one(struct amd_iommu *iommu, struct amd_iommu_viommu *viommu);
 
+void amd_viommu_domain_id_update_locked(struct amd_iommu_viommu *aviommu,
+					u16 hdom_id, u16 gdom_id);
+
 void amd_viommu_set_device_mapping(struct amd_iommu_viommu *aviommu,
 				   u16 hdev_id, u16 gdev_id);
 #else

drivers/iommu/amd/amd_viommu.h static inline void amd_viommu_set_device_mapping(struct amd_iommu_viommu *aviomm · line 61

Quoted 8 lines
drivers/iommu/amd/amd_viommu.h +5 -0
@@ -51,6 +54,11 @@ static inline void amd_viommu_set_device_mapping(struct amd_iommu_viommu *aviomm
 {
 }
 
+static inline void amd_viommu_domain_id_update_locked(struct amd_iommu_viommu *aviommu,
+						      u16 hdom_id, u16 gdom_id)
+{
+}
+

Do we need this ? Because this func gets called only when IOMMUFD is enabled.

drivers/iommu/amd/nested.c

Quoted 8 lines
drivers/iommu/amd/amd_viommu.h +0 -0
 #endif /* CONFIG_AMD_IOMMU_IOMMUFD */
 
 #endif /* AMD_VIOMMU_H */
drivers/iommu/amd/nested.c +0 -0
index 8bb0d1596c6c..ecdcecdc0bb7 100644
@@ -10,6 +10,7 @@

.../...

Quoted 19 lines
drivers/iommu/amd/nested.c +1 -0
 }
 
 static int nested_attach_device(struct iommu_domain *dom, struct device *dev,
 				struct iommu_domain *old)
 {
 	struct dev_table_entry new = {0};
+	struct nested_domain *ndom = to_ndomain(dom);
drivers/iommu/amd/nested.c +3 -1
 	struct iommu_dev_data *dev_data = dev_iommu_priv_get(dev);
 	struct amd_iommu *iommu = get_amd_iommu_from_dev_data(dev_data);
 	int ret = 0;
@@ -251,10 +268,23 @@ static int nested_attach_device(struct iommu_domain *dom, struct device *dev,
 
 	mutex_lock(&dev_data->mutex);
 
-	set_dte_nested(iommu, dom, dev_data, &new);
+	ret = set_dte_nested(iommu, dom, dev_data, &new);
+	if (ret)
+		goto out_err;
 

drivers/iommu/amd/nested.c static int nested_attach_device(struct iommu_domain *dom, struct device *dev, · line 284

Quoted 10 lines
drivers/iommu/amd/nested.c +9 -0
+	/*
+	 * Program gdom_id -> hdom_id before the DTE is live with
+	 * DTE_VIOMMU_EN. Hold vfctrl_lock across both so a concurrent
+	 * last-ref free cannot restore the idle map in between.
+	 */
+	mutex_lock(&ndom->viommu->vfctrl_lock);
+	amd_viommu_domain_id_update_locked(ndom->viommu,
+					   ndom->gdom_info->hdom_id,
+					   ndom->gdom_id);
 	amd_iommu_update_dte(iommu, dev_data, &new);

Shouldn't we keep update_dte outside vfctl_lock? We have dev_lock to cover dte update.

Also as Jason commented we should probably move vfctrl update to hdom_id allocation path.

Quoted 7 lines
drivers/iommu/amd/nested.c +2 -0
+	mutex_unlock(&ndom->viommu->vfctrl_lock);
 
+out_err:
 	mutex_unlock(&dev_data->mutex);
 
 	return ret;

drivers/iommu/amd/nested.c static void nested_domain_free(struct iommu_domain *dom) · line 303

Quoted 7 lines
drivers/iommu/amd/nested.c +4 -0
@@ -267,10 +297,18 @@ static void nested_domain_free(struct iommu_domain *dom)
 	struct nested_domain *ndom __free(kfree) = to_ndomain(dom);
 	struct amd_iommu_viommu *aviommu = ndom->viommu;
 
+	/*
+	 * vfctrl_lock then gdomid_array: keep the slot empty in the
+	 * xarray until CONTROL1 is idle so a concurrent alloc+attach
+	 * for this gdom_id cannot publish H2 and then lose it to this

In an ideal scenario guest/VMM will not re-use same gdom_id until control goes from back free path right?

-Vasant

author reviewer
#34 lore raw

PATCH v6 18/24 iommu/amd: Init and clear vIOMMU DevID and DomID maps

In reply to Suravee Suthikulpanit

Before using a vIOMMU instance, prefill the mapping tables so hardware never sees an unknown guest ID. Map every gdom_id to the nest parent domain ID, and write every gdev_id with hdev_id 0. Init cannot be scoped to IDs in use; INVALIDATE_IOMMU_ALL walks the HW table.

Yield vfctrl_lock every 256 doorbells so the 128K MMIO loop can resched. Attach cannot run until viommu->ops is set after init_one().

On destroy, drain CONTROL0/CONTROL1 WRITE and unmap. Do not rewrite the full tables: that would start 128K DMA writes into pages about to be freed. vDevice destroy and nested_domain_free already restored idle entries.

Skip VFCTRL access when CONTROL_CMDBUF_EN is already clear, matching viommu_priv_unmap_free() on the same uninit_one() path.

---
 drivers/iommu/amd/viommu.c | 90 ++++++++++++++++++++++++++++++++++++++
 1 file changed, 90 insertions(+)
drivers/iommu/amd/viommu.c +90 -0
index e2275d6b7442..2f299a16d113 100644
@@ -7,6 +7,7 @@
 #define dev_fmt(fmt)    pr_fmt(fmt)
 
 #include <linux/delay.h>
+#include <linux/sched.h>
 #include <linux/iommu.h>
 #include <linux/iommufd.h>
 #include <uapi/linux/iommufd.h>
@@ -457,6 +458,8 @@ static void __maybe_unused free_private_vm_region(struct amd_iommu *iommu, u64 *
 #define DEVID_ENTRY_WRITE		BIT_ULL(63)
 #define DEVID_ENTRY_VALID		BIT_ULL(0)
 
+#define VIOMMU_VFCTRL_PREFILL_RESCHED	256
+
 /*
  * CONTROL0/CONTROL1 bit 63 (WRITE) is a doorbell: hardware copies one
  * entry into the IPA-backed table and clears WRITE when that DMA write
@@ -566,10 +569,95 @@ void amd_viommu_domain_id_update_locked(struct amd_iommu_viommu *aviommu,
 	domain_id_update_unlocked(iommu, aviommu->gid, hdom_id, gdom_id);
 }
 
+static bool viommu_vfctrl_ready(struct amd_iommu *iommu)
+{
+	return iommu->vfctrl_base && viommu_cmdbuf_enabled(iommu);
+}
+
+/*
+ * Drain outstanding CONTROL0/CONTROL1 table DMA before the IPA pages
+ * are unmapped and freed. Do not rewrite 0..0xFFFF: vDevice destroy
+ * and nested_domain_free already restored idle entries, and a full
+ * walk would start 128K new DMA writes into pages about to be freed.
+ *
+ * Skip after disable_iommus(): CMDBUF_EN is already clear, matching
+ * viommu_priv_unmap_free() which skips IOTLB invalidation.
+ */
+static void viommu_drain_mapping(struct amd_iommu *iommu,
+				 struct amd_iommu_viommu *aviommu)
+{
+	u16 gid = aviommu->gid;
+
+	if (!viommu_vfctrl_ready(iommu))
+		return;
+
+	mutex_lock(&aviommu->vfctrl_lock);
+	vfctrl_wait_write_clear(iommu, gid,
+			VIOMMU_VFCTRL_GUEST_DID_MAP_CONTROL0_OFFSET);
+	vfctrl_wait_write_clear(iommu, gid,
+			VIOMMU_VFCTRL_GUEST_DID_MAP_CONTROL1_OFFSET);
+	mutex_unlock(&aviommu->vfctrl_lock);
+}
+
+static void viommu_prefill_resched(struct amd_iommu_viommu *aviommu, int i)
+{
+	lockdep_assert_held(&aviommu->vfctrl_lock);
+
+	if ((i & (VIOMMU_VFCTRL_PREFILL_RESCHED - 1)) !=
+	    VIOMMU_VFCTRL_PREFILL_RESCHED - 1)
+		return;
+
+	mutex_unlock(&aviommu->vfctrl_lock);
+	cond_resched();
+	mutex_lock(&aviommu->vfctrl_lock);
+}
+
+/*
+ * Prefill every guest ID so hardware never sees an unknown mapping.
+ * Map all gdom_id to the nest parent domain ID, and all gdev_id with
+ * hdev_id 0 and QueueID 0. Cannot be scoped to IDs in use: none exist
+ * yet, and INVALIDATE_IOMMU_ALL walks the HW table.
+ *
+ * Drop vfctrl_lock every VIOMMU_VFCTRL_PREFILL_RESCHED doorbells so
+ * the 128K MMIO loop can yield. Safe on init: vDevice/nested attach
+ * cannot run until viommu->ops is published after init_one().
+ */
+static void viommu_prefill_mapping(struct amd_iommu *iommu,
+				   struct amd_iommu_viommu *aviommu)
+{
+	int i;
+	u16 gid = aviommu->gid;
+
+	if (!viommu_vfctrl_ready(iommu))
+		return;
+
+	mutex_lock(&aviommu->vfctrl_lock);
+	if (aviommu->domid_table) {
+		for (i = 0; i <= VIOMMU_MAX_GDOMID; i++) {
+			domain_id_update_unlocked(iommu, gid,
+						  aviommu->parent->id, i);
+			viommu_prefill_resched(aviommu, i);
+		}
+	}
+
+	if (aviommu->devid_table) {
+		for (i = 0; i <= VIOMMU_MAX_GDEVID; i++) {
+			set_device_mapping_unlocked(iommu, gid, 0, i, 0);
+			viommu_prefill_resched(aviommu, i);
+		}
+	}
+	mutex_unlock(&aviommu->vfctrl_lock);
+}
+
 void amd_viommu_uninit_one(struct amd_iommu *iommu, struct amd_iommu_viommu *aviommu)
 {
 	pr_debug("%s: gid=%u\n", __func__, aviommu->gid);
 
+	/*
+	 * Wait until CONTROL0/CONTROL1 table DMA has retired before
+	 * the backing pages are unmapped and freed.
+	 */
+	viommu_drain_mapping(iommu, aviommu);
 	free_private_vm_region(iommu, &aviommu->devid_table,
 			       VIOMMU_DEVID_MAPPING_BASE,
 			       VIOMMU_DEVID_MAPPING_ENTRY_SIZE,
@@ -598,6 +686,8 @@ int amd_viommu_init_one(struct amd_iommu *iommu, struct amd_iommu_viommu *viommu
 	if (ret)
 		goto err_out;
 
+	viommu_prefill_mapping(iommu, viommu);
+
 	return 0;
 err_out:
 	amd_viommu_uninit_one(iommu, viommu);
Signature · 1 line
2.34.1
maintainer
#35 lore raw

Re: [PATCH v6 18/24] iommu/amd: Init and clear vIOMMU DevID and DomID maps

Quoted 8 lines

Before using a vIOMMU instance, prefill the mapping tables so hardware never sees an unknown guest ID. Map every gdom_id to the nest parent domain ID, and write every gdev_id with hdev_id 0. Init cannot be scoped to IDs in use; INVALIDATE_IOMMU_ALL walks the HW table.

Yield vfctrl_lock every 256 doorbells so the 128K MMIO loop can resched. Attach cannot run until viommu->ops is set after init_one().

Quoted 7 lines

On destroy, drain CONTROL0/CONTROL1 WRITE and unmap. Do not rewrite the full tables: that would start 128K DMA writes into pages about to be freed. vDevice destroy and nested_domain_free already restored idle entries.

Skip VFCTRL access when CONTROL_CMDBUF_EN is already clear, matching viommu_priv_unmap_free() on the same uninit_one() path.

I didn't see any explanation what happens if the guest issues an invalidate to a gdomid without a host domid?

This is related to my questions about when the IOTLB is cleaned. Given this patch I would expect that once the mapping is removed the hdomid is cleaned and any invalidations for the gdomid are NOPd

If the gdomis is used again it is assigned to a clean hdomid and those NOP'd invalidations don't matter.

Is this how it works?

I have to say the commit messages are no good in this series, do not summarize what the patch is doing, explain why this patch is necessary in the first place.

Signature · 1 line
Jason
author reviewer
#36 lore raw

Re: [PATCH v6 18/24] iommu/amd: Init and clear vIOMMU DevID and DomID maps

Quoted 18 lines · Jason Gunthorpe
Quoted 8 lines

Before using a vIOMMU instance, prefill the mapping tables so hardware never sees an unknown guest ID. Map every gdom_id to the nest parent domain ID, and write every gdev_id with hdev_id 0. Init cannot be scoped to IDs in use; INVALIDATE_IOMMU_ALL walks the HW table.

Yield vfctrl_lock every 256 doorbells so the 128K MMIO loop can resched. Attach cannot run until viommu->ops is set after init_one().

Quoted 7 lines

On destroy, drain CONTROL0/CONTROL1 WRITE and unmap. Do not rewrite the full tables: that would start 128K DMA writes into pages about to be freed. vDevice destroy and nested_domain_free already restored idle entries.

Skip VFCTRL access when CONTROL_CMDBUF_EN is already clear, matching viommu_priv_unmap_free() on the same uninit_one() path.

I didn't see any explanation what happens if the guest issues an invalidate to a gdomid without a host domid?

Quoted 8 lines

This is related to my questions about when the IOTLB is cleaned. Given this patch I would expect that once the mapping is removed the hdomid is cleaned and any invalidations for the gdomid are NOPd

If the gdomis is used again it is assigned to a clean hdomid and those NOP'd invalidations don't matter.

Is this how it works?

When guest sends INVALIDATE_IOMMU_PAGES command with gDomID to vCmd buffer, IOMMU hardware uses the gDomID to index the DomainID mapping table to get the hDomID (from the entry w/ v=1). Then it uses the hDomID
for actual IOMMU TLB invalidation. If the mapping entry has v=0, it
would generate hardware error in the host Event log.

If the hDomID does not match anything in the IOMMU TLB, it would be a NOP. This is the same as when host IOMMU driver submit INVALIDATE_IOMMU_PAGES command w/ an unused domainID.

For current implementation, the IOMMU driver prefills the DomainID mapping table with nest parent domain ID (hDomID), and restore the same ID when mapping is removed. This is to make sure that each mapping entry has valid hDomID and not just some random domain ID.

Quoted 3 lines

I have to say the commit messages are no good in this series, do not summarize what the patch is doing, explain why this patch is necessary in the first place.

I'll update the commit message.

Regards,
Suravee

maintainer
#37 lore raw

Re: [PATCH v6 18/24] iommu/amd: Init and clear vIOMMU DevID and DomID maps

Quoted 36 lines · Suravee
Quoted 18 lines · Jason Gunthorpe
Quoted 8 lines

Before using a vIOMMU instance, prefill the mapping tables so hardware never sees an unknown guest ID. Map every gdom_id to the nest parent domain ID, and write every gdev_id with hdev_id 0. Init cannot be scoped to IDs in use; INVALIDATE_IOMMU_ALL walks the HW table.

Yield vfctrl_lock every 256 doorbells so the 128K MMIO loop can resched. Attach cannot run until viommu->ops is set after init_one().

Quoted 7 lines

On destroy, drain CONTROL0/CONTROL1 WRITE and unmap. Do not rewrite the full tables: that would start 128K DMA writes into pages about to be freed. vDevice destroy and nested_domain_free already restored idle entries.

Skip VFCTRL access when CONTROL_CMDBUF_EN is already clear, matching viommu_priv_unmap_free() on the same uninit_one() path.

I didn't see any explanation what happens if the guest issues an invalidate to a gdomid without a host domid?

Quoted 8 lines

This is related to my questions about when the IOTLB is cleaned. Given this patch I would expect that once the mapping is removed the hdomid is cleaned and any invalidations for the gdomid are NOPd

If the gdomis is used again it is assigned to a clean hdomid and those NOP'd invalidations don't matter.

Is this how it works?

When guest sends INVALIDATE_IOMMU_PAGES command with gDomID to vCmd buffer, IOMMU hardware uses the gDomID to index the DomainID mapping table to get the hDomID (from the entry w/ v=1). Then it uses the hDomID for actual IOMMU TLB invalidation. If the mapping entry has v=0, it would generate hardware error in the host Event log.

Quoted 8 lines

If the hDomID does not match anything in the IOMMU TLB, it would be a NOP. This is the same as when host IOMMU driver submit INVALIDATE_IOMMU_PAGES command w/ an unused domainID.

For current implementation, the IOMMU driver prefills the DomainID mapping table with nest parent domain ID (hDomID), and restore the same ID when mapping is removed. This is to make sure that each mapping entry has valid hDomID and not just some random domain ID.

I asked what happens if the gDomID hits an entry with V=0 ?

Presumably something bad to motivate all this extra work.

Jason

author reviewer
#38 lore raw

Re: [PATCH v6 18/24] iommu/amd: Init and clear vIOMMU DevID and DomID maps

Quoted 41 lines · Jason Gunthorpe
Quoted 40 lines · Suravee
Quoted 18 lines · Jason Gunthorpe
Quoted 8 lines

Before using a vIOMMU instance, prefill the mapping tables so hardware never sees an unknown guest ID. Map every gdom_id to the nest parent domain ID, and write every gdev_id with hdev_id 0. Init cannot be scoped to IDs in use; INVALIDATE_IOMMU_ALL walks the HW table.

Yield vfctrl_lock every 256 doorbells so the 128K MMIO loop can resched. Attach cannot run until viommu->ops is set after init_one().

Quoted 7 lines

On destroy, drain CONTROL0/CONTROL1 WRITE and unmap. Do not rewrite the full tables: that would start 128K DMA writes into pages about to be freed. vDevice destroy and nested_domain_free already restored idle entries.

Skip VFCTRL access when CONTROL_CMDBUF_EN is already clear, matching viommu_priv_unmap_free() on the same uninit_one() path.

I didn't see any explanation what happens if the guest issues an invalidate to a gdomid without a host domid?

Quoted 8 lines

This is related to my questions about when the IOTLB is cleaned. Given this patch I would expect that once the mapping is removed the hdomid is cleaned and any invalidations for the gdomid are NOPd

If the gdomis is used again it is assigned to a clean hdomid and those NOP'd invalidations don't matter.

Is this how it works?

When guest sends INVALIDATE_IOMMU_PAGES command with gDomID to vCmd buffer, IOMMU hardware uses the gDomID to index the DomainID mapping table to get the hDomID (from the entry w/ v=1). Then it uses the hDomID for actual IOMMU TLB invalidation. If the mapping entry has v=0, it would generate hardware error in the host Event log.

If the hDomID does not match anything in the IOMMU TLB, it would be a NOP. This is the same as when host IOMMU driver submit INVALIDATE_IOMMU_PAGES command w/ an unused domainID.

Quoted 8 lines
Quoted 4 lines

For current implementation, the IOMMU driver prefills the DomainID mapping table with nest parent domain ID (hDomID), and restore the same ID when mapping is removed. This is to make sure that each mapping entry has valid hDomID and not just some random domain ID.

I asked what happens if the gDomID hits an entry with V=0 ?

Presumably something bad to motivate all this extra work.

As mentioned above, if the mapping entry has V=0, the IOMMU hardware generate hardware error event in the host event log. So, we try to prevent by pre-fill / restore with the parent domain ID (a.k.a this extra work).

Suravee

Quoted 2 lines

Jason

maintainer
#39 lore raw

Re: [PATCH v6 18/24] iommu/amd: Init and clear vIOMMU DevID and DomID maps

In reply to Suravee Suthikulpanit
Quoted 26 lines · Suravee Suthikulpanit

Before using a vIOMMU instance, prefill the mapping tables so hardware never sees an unknown guest ID. Map every gdom_id to the nest parent domain ID, and write every gdev_id with hdev_id 0. Init cannot be scoped to IDs in use; INVALIDATE_IOMMU_ALL walks the HW table.

Yield vfctrl_lock every 256 doorbells so the 128K MMIO loop can resched. Attach cannot run until viommu->ops is set after init_one().

On destroy, drain CONTROL0/CONTROL1 WRITE and unmap. Do not rewrite the full tables: that would start 128K DMA writes into pages about to be freed. vDevice destroy and nested_domain_free already restored idle entries.

Skip VFCTRL access when CONTROL_CMDBUF_EN is already clear, matching viommu_priv_unmap_free() on the same uninit_one() path.

---
 drivers/iommu/amd/viommu.c | 90 ++++++++++++++++++++++++++++++++++++++
 1 file changed, 90 insertions(+)
drivers/iommu/amd/viommu.c +0 -0
index e2275d6b7442..2f299a16d113 100644

drivers/iommu/amd/viommu.c line 8

Quoted 2 lines
drivers/iommu/amd/viommu.c +0 -0
@@ -7,6 +7,7 @@
 #define dev_fmt(fmt)    pr_fmt(fmt)
 

.../...

drivers/iommu/amd/viommu.c void amd_viommu_domain_id_update_locked(struct amd_iommu_viommu *aviommu, · line 619

Quoted 6 lines
drivers/iommu/amd/viommu.c +6 -0
+
+/*
+ * Prefill every guest ID so hardware never sees an unknown mapping.
+ * Map all gdom_id to the nest parent domain ID, and all gdev_id with
+ * hdev_id 0 and QueueID 0. Cannot be scoped to IDs in use: none exist
+ * yet, and INVALIDATE_IOMMU_ALL walks the HW table.

We don't support INVALIDATE_IOMMU_ALL inside guest.

drivers/iommu/amd/viommu.c void amd_viommu_domain_id_update_locked(struct amd_iommu_viommu *aviommu, · line 631

Quoted 12 lines

+ *
+ * Drop vfctrl_lock every VIOMMU_VFCTRL_PREFILL_RESCHED doorbells so
+ * the 128K MMIO loop can yield. Safe on init: vDevice/nested attach
+ * cannot run until viommu->ops is published after init_one().
+ */

drivers/iommu/amd/viommu.c +7 -0
+static void viommu_prefill_mapping(struct amd_iommu *iommu,
+				   struct amd_iommu_viommu *aviommu)
+{
+	int i;
+	u16 gid = aviommu->gid;
+
+	if (!viommu_vfctrl_ready(iommu))

Why? We will not hit this path if vfctl_base is not setup or command buffer is not enabled.

drivers/iommu/amd/viommu.c void amd_viommu_domain_id_update_locked(struct amd_iommu_viommu *aviommu, · line 594

Quoted 3 lines
drivers/iommu/amd/viommu.c +3 -0
+		return;
+
+	mutex_lock(&aviommu->vfctrl_lock);

May be without lock as no one else will call this until viommu_init is completed?

-Vasant

author reviewer
#40 lore raw

PATCH v6 19/24 iommu/amd: Add per-segment translate device ID pool

In reply to Suravee Suthikulpanit

A translate device ID (TransDevID) is a host device-table index used by hardware vIOMMU. VFCTRL guest-misc names the slot; that DTE holds the nest-parent v1 table for GPA->SPA walks of guest IOMMU traffic.

Reserve PCI requestor IDs in a per-segment pool so they are not used as vIOMMU translation DTEs. The device table is per-segment, so the pool is too.

Add trans_devid.c with pci_seg init/fini and amd_iommu_trans_devid_reserve() from probe_device(). Keep reservations for the pci_seg lifetime, including after amd_iommu_release_device().

The IOMMU function never reaches probe_device(): rlookup is cleared at iommu->devid, while the self DTE still occupies that slot. Reserve it in init_iommu_one_late() for the same pci_seg lifetime.

---
 drivers/iommu/amd/Makefile          |  2 +-
 drivers/iommu/amd/amd_iommu_types.h | 10 ++++
 drivers/iommu/amd/amd_viommu.h      | 20 +++++++
 drivers/iommu/amd/init.c            | 13 +++++
 drivers/iommu/amd/iommu.c           | 22 +++++++
 drivers/iommu/amd/trans_devid.c     | 90 +++++++++++++++++++++++++++++
 drivers/iommu/amd/viommu.c          |  3 +
 7 files changed, 159 insertions(+), 1 deletion(-)
 create mode 100644 drivers/iommu/amd/trans_devid.c
drivers/iommu/amd/Makefile +1 -1
index e1e824b9c7b0..12c3fe83e4ce 100644
@@ -1,4 +1,4 @@
 # SPDX-License-Identifier: GPL-2.0-only
 obj-y += iommu.o init.o quirks.o ppr.o pasid.o
-obj-$(CONFIG_AMD_IOMMU_IOMMUFD) += iommufd.o nested.o viommu.o
+obj-$(CONFIG_AMD_IOMMU_IOMMUFD) += iommufd.o nested.o viommu.o trans_devid.o
 obj-$(CONFIG_AMD_IOMMU_DEBUGFS) += debugfs.o
drivers/iommu/amd/amd_iommu_types.h +10 -0
index b5d1b23791da..6b3a43952797 100644
@@ -681,6 +681,16 @@ struct amd_iommu_pci_seg {
 	 * parsing time.
 	 */
 	struct list_head unity_map;
+
+#ifdef CONFIG_AMD_IOMMU_IOMMUFD
+	/*
+	 * Per-segment translate-device-id pool indexed by id.  Entries are:
+	 * absent (FREE), xa value TRANS_DEVID_RESERVED, or a pointer to the
+	 * owning struct amd_iommu_viommu (ALLOCATED).
+	 */
+	struct mutex trans_devid_mutex;
+	struct xarray trans_devid_xa;
+#endif
 };
 
 /*
drivers/iommu/amd/amd_viommu.h +20 -0
index 30227a5e4bfa..772d99391b6c 100644
@@ -28,6 +28,10 @@ void amd_viommu_domain_id_update_locked(struct amd_iommu_viommu *aviommu,
 
 void amd_viommu_set_device_mapping(struct amd_iommu_viommu *aviommu,
 				   u16 hdev_id, u16 gdev_id);
+
+void amd_iommu_pci_seg_trans_devid_init(struct amd_iommu_pci_seg *pci_seg);
+void amd_iommu_pci_seg_trans_devid_fini(struct amd_iommu_pci_seg *pci_seg);
+int amd_iommu_trans_devid_reserve(struct amd_iommu_pci_seg *pci_seg, u16 id);
 #else
 
 /*
@@ -59,6 +63,22 @@ static inline void amd_viommu_domain_id_update_locked(struct amd_iommu_viommu *a
 {
 }
 
+static inline void
+amd_iommu_pci_seg_trans_devid_init(struct amd_iommu_pci_seg *pci_seg)
+{
+}
+
+static inline int
+amd_iommu_trans_devid_reserve(struct amd_iommu_pci_seg *pci_seg, u16 id)
+{
+	return 0;
+}
+
+static inline void
+amd_iommu_pci_seg_trans_devid_fini(struct amd_iommu_pci_seg *pci_seg)
+{
+}
+
 #endif /* CONFIG_AMD_IOMMU_IOMMUFD */
 
 #endif /* AMD_VIOMMU_H */
drivers/iommu/amd/init.c +13 -0
index 96299f10b2e8..59a06c3014fe 100644
@@ -1747,6 +1747,7 @@ static struct amd_iommu_pci_seg *__init alloc_pci_segment(u16 id,
 	xa_init(&pci_seg->dev_data_xa);
 	INIT_LIST_HEAD(&pci_seg->unity_map);
 	list_add_tail(&pci_seg->list, &amd_iommu_pci_seg_list);
+	amd_iommu_pci_seg_trans_devid_init(pci_seg);
 
 	if (alloc_dev_table(pci_seg))
 		goto err_free_pci_seg;
@@ -1763,6 +1764,7 @@ static struct amd_iommu_pci_seg *__init alloc_pci_segment(u16 id,
 	free_dev_table(pci_seg);
 err_free_pci_seg:
 	list_del(&pci_seg->list);
+	amd_iommu_pci_seg_trans_devid_fini(pci_seg);
 	xa_destroy(&pci_seg->dev_data_xa);
 	kfree(pci_seg);
 	return NULL;
@@ -1787,6 +1789,7 @@ static void __init free_pci_segments(void)
 
 	for_each_pci_segment_safe(pci_seg, next) {
 		list_del(&pci_seg->list);
+		amd_iommu_pci_seg_trans_devid_fini(pci_seg);
 		xa_destroy(&pci_seg->dev_data_xa);
 		free_irq_lookup_table(pci_seg);
 		free_rlookup_table(pci_seg);
@@ -2006,8 +2009,18 @@ static int __init init_iommu_one_late(struct amd_iommu *iommu)
 	/*
 	 * Make sure IOMMU is not considered to translate itself. The IVRS
 	 * table tells us so, but this is a lie!
+	 *
+	 * Clearing rlookup keeps this function off amd_iommu_probe_device(),
+	 * so its devid is never reserved there. The vIOMMU self DTE is
+	 * still programmed at iommu->devid. Reserve it here so translate-id
+	 * alloc does not treat the empty xarray slot as free.
 	 */
 	iommu->pci_seg->rlookup_table[iommu->devid] = NULL;
+	ret = amd_iommu_trans_devid_reserve(iommu->pci_seg, iommu->devid);
+	if (ret) {
+		pr_err("Failed to reserve IOMMU devid %#x\n", iommu->devid);
+		return ret;
+	}
 
 	return 0;
 }
drivers/iommu/amd/iommu.c +22 -0
index b7073b0ca8b0..17716a506f93 100644
@@ -2738,6 +2738,25 @@ static struct iommu_device *amd_iommu_probe_device(struct device *dev)
 	if (dev_is_pci(dev))
 		pci_prepare_ats(to_pci_dev(dev), PAGE_SHIFT);
 
+#if IS_ENABLED(CONFIG_AMD_IOMMU_IOMMUFD)
+	/*
+	 * Occupy this RID in the per-segment translate-device-id pool
+	 * so vIOMMU alloc cannot reuse it. Probe is the RID lifetime
+	 * for devices that reach it. The IOMMU function is reserved
+	 * from init_iommu_one_late() instead: rlookup is cleared, so
+	 * probe never runs, while the self DTE still uses iommu->devid.
+	 * The reservation is not released on unplug. If reserve fails,
+	 * the IOMMU core frees dev->iommu and a later re-probe retries.
+	 */
+	ret = amd_iommu_trans_devid_reserve(iommu->pci_seg, dev_data->devid);
+	if (ret) {
+		pr_err("%s: Failed to reserve device id %#x\n", __func__,
+		       dev_data->devid);
+		iommu_dev = ERR_PTR(ret);
+		goto out_err;
+	}
+#endif
+
 out_err:
 	return iommu_dev;
 }
@@ -2751,6 +2770,9 @@ static void amd_iommu_release_device(struct device *dev)
 	/*
 	 * We keep dev_data around for unplugged devices and reuse it when the
 	 * device is re-plugged - not doing so would introduce a ton of races.
+	 * Translate-device-id reservations stay as well: the RID remains
+	 * occupied for the pci_seg lifetime so later alloc cannot reuse a
+	 * DTE index that still belongs to this function.
 	 */
 }
 
drivers/iommu/amd/trans_devid.c new file +90 -0

90 lines not on this page · raw message

drivers/iommu/amd/viommu.c +3 -0
index 2f299a16d113..ff5394e6a4c7 100644
@@ -110,6 +110,9 @@ static int viommu_alloc_self_dev_data(struct amd_iommu *iommu)
 	 * Synthetic DTE for iommu->devid only: no struct device, so
 	 * amd_iommu_update_dte() skips clone_aliases(), and not on
 	 * pdom->dev_list (not an IOMMU-API attach).
+	 *
+	 * init_iommu_one_late() reserves this devid. rlookup is NULL, so
+	 * probe never runs, and alloc must not reuse the self DTE slot.
 	 */
 	dev_data->dev = NULL;
 	dev_data->domain = pdom;
Signature · 1 line
2.34.1
author reviewer
#41 lore raw

PATCH v6 20/24 iommu/amd: Reserve translate-device-id for PCI requestor aliases

In reply to Suravee Suthikulpanit

The per-segment translate-device-id pool tracks which device-table indices are reserved for real PCI functions versus available for dynamic allocation to vIOMMU translation DTEs. amd_iommu_probe_device() already calls amd_iommu_trans_devid_reserve() for the struct device's own PCI BDF so that id cannot later be returned by the allocator.

That is not sufficient on its own. The AMD IOMMU driver programs identical DMA translation device-table entries (DTEs) for every requestor ID that can issue DMA on behalf of the same PCI function: the IVRS alias from alias_table[] when it is not covered by the PCI DMA-alias walk (different bus than the device), and every alias visited by pci_for_each_dma_alias(). Those alternate BDFs are not separate struct device probe targets, so they never received a trans_devid reservation and could in principle collide with a dynamically allocated trans_devid.

Introduce amd_iommu_trans_devid_reserve_pci_aliases() in trans_devid.c and invoke it from amd_iommu_probe_device() immediately after the primary amd_iommu_trans_devid_reserve() succeeds. For PCI devices the helper reserves the IVRS alias when it differs from the device BDF, then walks pci_for_each_dma_alias() and reserves each alias BDF. Repeated probe and overlap with the primary BDF in the PCI walk are handled by the existing idempotency of amd_iommu_trans_devid_reserve() (a second reserve of an already-reserved id succeeds).

---
 drivers/iommu/amd/amd_viommu.h  |  2 ++
 drivers/iommu/amd/iommu.c       |  8 ++++++
 drivers/iommu/amd/trans_devid.c | 45 +++++++++++++++++++++++++++++++++
 3 files changed, 55 insertions(+)
drivers/iommu/amd/amd_viommu.h +2 -0
index 772d99391b6c..44a342608d43 100644
@@ -32,6 +32,8 @@ void amd_viommu_set_device_mapping(struct amd_iommu_viommu *aviommu,
 void amd_iommu_pci_seg_trans_devid_init(struct amd_iommu_pci_seg *pci_seg);
 void amd_iommu_pci_seg_trans_devid_fini(struct amd_iommu_pci_seg *pci_seg);
 int amd_iommu_trans_devid_reserve(struct amd_iommu_pci_seg *pci_seg, u16 id);
+int amd_iommu_trans_devid_reserve_pci_aliases(struct amd_iommu *iommu,
+					      struct device *dev);
 #else
 
 /*
drivers/iommu/amd/iommu.c +8 -0
index 17716a506f93..65550b070a12 100644
@@ -2755,6 +2755,14 @@ static struct iommu_device *amd_iommu_probe_device(struct device *dev)
 		iommu_dev = ERR_PTR(ret);
 		goto out_err;
 	}
+
+	ret = amd_iommu_trans_devid_reserve_pci_aliases(iommu, dev);
+	if (ret) {
+		pr_err("%s: Failed to reserve translate devid for alias of %#x (err %d)\n",
+		       __func__, dev_data->devid, ret);
+		iommu_dev = ERR_PTR(ret);
+		goto out_err;
+	}
 #endif
 
 out_err:
drivers/iommu/amd/trans_devid.c +45 -0

54 lines not on this page · raw message

Signature · 1 line
2.34.1
author reviewer
#42 lore raw

PATCH v6 21/24 iommu/amd: Add translation DTE and VFctrl TransDevID helpers

In reply to Suravee Suthikulpanit

vIOMMU GPA->SPA walks use a per-vIOMMU TransDevID: VFCTRL guest-misc names the slot, and that host DTE points at the nest-parent v1 table.

Add amd_iommu_set_translate_dte() and amd_iommu_clear_translate_dte() to program that DTE, owned on amd_iommu_viommu.trans_dev_data rather than pci_seg->dev_data_xa. Rewrite it from amd_iommu_change_top() via pdom->viommu_list when the nest-parent table grows.

Add amd_iommu_update_vfctrl_mmio_translate_devid() to publish the TransDevID in VFCTRL guest-misc MMIO.

---
 drivers/iommu/amd/amd_iommu.h       |   7 ++
 drivers/iommu/amd/amd_iommu_types.h |   4 +
 drivers/iommu/amd/iommu.c           | 153 ++++++++++++++++++++++++----
 3 files changed, 142 insertions(+), 22 deletions(-)
drivers/iommu/amd/amd_iommu.h +7 -0
index 6ac64524208b..4aede6f6d489 100644
@@ -213,6 +213,13 @@ void amd_iommu_update_dte(struct amd_iommu *iommu,
 			  struct iommu_dev_data *dev_data,
 			  struct dev_table_entry *new);
 
+int amd_iommu_set_translate_dte(struct iommufd_viommu *viommu);
+void amd_iommu_clear_translate_dte(struct amd_iommu *iommu,
+				   struct iommu_dev_data *dev_data,
+				   u16 trans_devid);
+void amd_iommu_update_vfctrl_mmio_translate_devid(struct amd_iommu *iommu,
+						  u16 gid, u32 trans_devid);
+
 static inline void
 amd_iommu_make_clear_dte(struct amd_iommu *iommu, u16 devid,
 			 struct dev_table_entry *new)
drivers/iommu/amd/amd_iommu_types.h +4 -0
index 6b3a43952797..5f84f25e3f37 100644
@@ -489,6 +489,7 @@ extern bool amdr_ivrs_remap_support;
 /* VIOMMU stuff */
 #define VIOMMU_VF_MMIO_ENTRY_SIZE		4096
 #define VIOMMU_VFCTRL_MMIO_ENTRY_SIZE		64
+#define VIOMMU_VFCTRL_GUEST_MISC_CONTROL_OFFSET	0x10
 
 /* Host ioremap/request_mem_region sizes for VF / VF_CNTL BARs */
 #define VIOMMU_VF_MMIO_MAP_SIZE		0x10000000UL
@@ -551,6 +552,9 @@ struct amd_iommu_viommu {
 
 	u64 *devid_table;
 	u64 *domid_table;
+	u16 trans_devid;
+	/* Synthetic DTE; not stored in pci_seg->dev_data_xa */
+	struct iommu_dev_data *trans_dev_data;
 
 	/*
 	 * Serializes VFCTRL Guest DID map CONTROL0/CONTROL1 doorbells
drivers/iommu/amd/iommu.c +131 -22

234 lines not on this page · raw message

Signature · 1 line
2.34.1
maintainer
#43 lore raw

Re: [PATCH v6 21/24] iommu/amd: Add translation DTE and VFctrl TransDevID helpers

Quoted 63 lines · Suravee Suthikulpanit

vIOMMU GPA->SPA walks use a per-vIOMMU TransDevID: VFCTRL guest-misc names the slot, and that host DTE points at the nest-parent v1 table.

Add amd_iommu_set_translate_dte() and amd_iommu_clear_translate_dte() to program that DTE, owned on amd_iommu_viommu.trans_dev_data rather than pci_seg->dev_data_xa. Rewrite it from amd_iommu_change_top() via pdom->viommu_list when the nest-parent table grows.

Add amd_iommu_update_vfctrl_mmio_translate_devid() to publish the TransDevID in VFCTRL guest-misc MMIO.

---
 drivers/iommu/amd/amd_iommu.h       |   7 ++
 drivers/iommu/amd/amd_iommu_types.h |   4 +
 drivers/iommu/amd/iommu.c           | 153 ++++++++++++++++++++++++----
 3 files changed, 142 insertions(+), 22 deletions(-)
drivers/iommu/amd/amd_iommu.h +7 -0
index 6ac64524208b..4aede6f6d489 100644
@@ -213,6 +213,13 @@ void amd_iommu_update_dte(struct amd_iommu *iommu,
 			  struct iommu_dev_data *dev_data,
 			  struct dev_table_entry *new);
 
+int amd_iommu_set_translate_dte(struct iommufd_viommu *viommu);
+void amd_iommu_clear_translate_dte(struct amd_iommu *iommu,
+				   struct iommu_dev_data *dev_data,
+				   u16 trans_devid);
+void amd_iommu_update_vfctrl_mmio_translate_devid(struct amd_iommu *iommu,
+						  u16 gid, u32 trans_devid);
+
 static inline void
 amd_iommu_make_clear_dte(struct amd_iommu *iommu, u16 devid,
 			 struct dev_table_entry *new)
drivers/iommu/amd/amd_iommu_types.h +4 -0
index 6b3a43952797..5f84f25e3f37 100644
@@ -489,6 +489,7 @@ extern bool amdr_ivrs_remap_support;
 /* VIOMMU stuff */
 #define VIOMMU_VF_MMIO_ENTRY_SIZE		4096
 #define VIOMMU_VFCTRL_MMIO_ENTRY_SIZE		64
+#define VIOMMU_VFCTRL_GUEST_MISC_CONTROL_OFFSET	0x10
 
 /* Host ioremap/request_mem_region sizes for VF / VF_CNTL BARs */
 #define VIOMMU_VF_MMIO_MAP_SIZE		0x10000000UL
@@ -551,6 +552,9 @@ struct amd_iommu_viommu {
 
 	u64 *devid_table;
 	u64 *domid_table;
+	u16 trans_devid;
+	/* Synthetic DTE; not stored in pci_seg->dev_data_xa */
+	struct iommu_dev_data *trans_dev_data;
 
 	/*
 	 * Serializes VFCTRL Guest DID map CONTROL0/CONTROL1 doorbells
drivers/iommu/amd/iommu.c +0 -0
index 65550b070a12..8943863105b6 100644

drivers/iommu/amd/iommu.c static void write_dte_lower128(struct dev_table_entry *ptr, struct dev_table_ent · line 151

Quoted 5 lines
drivers/iommu/amd/iommu.c +1 -1
@@ -148,11 +148,11 @@ static void write_dte_lower128(struct dev_table_entry *ptr, struct dev_table_ent
  * So, only consider control bits related to DMA when updating the entry.
  */
 static void update_dte256(struct amd_iommu *iommu, struct iommu_dev_data *dev_data,
-			  struct dev_table_entry *new)
+			  u16 devid, struct dev_table_entry *new)

Why this change? dev_data always contains devid right?

drivers/iommu/amd/iommu.c static void write_dte_lower128(struct dev_table_entry *ptr, struct dev_table_ent · line 157

Quoted 7 lines
drivers/iommu/amd/iommu.c +1 -1
 {
 	unsigned long flags;
 	struct dev_table_entry *dev_table = get_dev_table(iommu);
-	struct dev_table_entry *ptr = &dev_table[dev_data->devid];
+	struct dev_table_entry *ptr = &dev_table[devid];
 
 	spin_lock_irqsave(&dev_data->dte_lock, flags);

.../...

Quoted 15 lines
drivers/iommu/amd/iommu.c +0 -0
  */

static void update_viommu_self_dte(struct protection_domain *pdom,

drivers/iommu/amd/iommu.c +2 -0
 				   phys_addr_t top_paddr,
 				   unsigned int top_level)
 {
 	struct pdom_iommu_info *pdom_iommu_info;
+	struct amd_iommu_viommu *aviommu;
 	unsigned long i;
 
 	lockdep_assert_held(&pdom->lock);
@@ -2884,6 +2893,17 @@ static void update_viommu_self_dte(struct protection_domain *pdom,
 		set_dte_entry(iommu, iommu->viommu_dev_data, top_paddr,
 			      top_level);
 	}
+

drivers/iommu/amd/iommu.c static void update_viommu_self_dte(struct protection_domain *pdom, · line 2906

Quoted 10 lines
drivers/iommu/amd/iommu.c +10 -0
+	list_for_each_entry(aviommu, &pdom->viommu_list, pdom_list) {
+		struct amd_iommu *iommu;
+
+		if (!aviommu->trans_dev_data)
+			continue;
+		iommu = container_of(aviommu->core.iommu_dev,
+				     struct amd_iommu, iommu);
+		set_dte_entry(iommu, aviommu->trans_dev_data, top_paddr,
+			      top_level);
+	}

These two are operating on different page table. So its better to have it in separate function.

drivers/iommu/amd/iommu.c static bool amd_iommu_enforce_cache_coherency(struct iommu_domain *domain) · line 3486

Quoted 8 lines
drivers/iommu/amd/iommu.c +1 -0
 }
 #else
 static inline void update_viommu_self_dte(struct protection_domain *pdom,
@@ -3463,6 +3483,95 @@ static bool amd_iommu_enforce_cache_coherency(struct iommu_domain *domain)
 	return true;
 }
 
+#if IS_ENABLED(CONFIG_AMD_IOMMU_IOMMUFD)

why not move this functions to trans_devid.c ?

Quoted 39 lines
drivers/iommu/amd/iommu.c +39 -0
+
+void amd_iommu_update_vfctrl_mmio_translate_devid(struct amd_iommu *iommu,
+						  u16 gid, u32 devid)
+{
+	writeq((devid & 0xFFFFULL) << 16,
+	       VIOMMU_VFCTRL_MMIO_BASE(iommu, gid) +
+	       VIOMMU_VFCTRL_GUEST_MISC_CONTROL_OFFSET);
+}
+
+int amd_iommu_set_translate_dte(struct iommufd_viommu *viommu)
+{
+	struct amd_iommu_viommu *aviommu =
+		container_of(viommu, struct amd_iommu_viommu, core);
+	struct amd_iommu *iommu =
+		container_of(viommu->iommu_dev, struct amd_iommu, iommu);
+	struct protection_domain *pdom = aviommu->parent;
+	u16 gid = aviommu->gid;
+	u16 trans_devid = aviommu->trans_devid;
+	struct iommu_dev_data *trans_dev_data;
+	unsigned long flags;
+
+	if (!aviommu->trans_dev_data) {
+		aviommu->trans_dev_data =
+			amd_iommu_alloc_dev_data(trans_devid);
+		if (!aviommu->trans_dev_data) {
+			pr_err("%s: Failed to allocate dev_data for translate-device-id %#x\n",
+			       __func__, trans_devid);
+			return -ENOMEM;
+		}
+	}
+
+	trans_dev_data = aviommu->trans_dev_data;
+	trans_dev_data->dev = NULL;
+	trans_dev_data->domain = pdom;
+	/*
+	 * Leave off pdom->dev_list: this is not an IOMMU-API attach.
+	 * Hold pdom->lock so change_top() cannot grow the nest-parent
+	 * top between the sample and the DTE commit. Publish devid
+	 * under that lock; it is the live TransDevID for

drivers/iommu/amd/iommu.c static bool amd_iommu_enforce_cache_coherency(struct iommu_domain *domain) · line 3535

Quoted 10 lines
drivers/iommu/amd/iommu.c +10 -0
+	 * update_viommu_self_dte(). If this vIOMMU is not yet on
+	 * viommu_list, add it before dropping the lock so a concurrent
+	 * increase_top() cannot miss the new DTE.
+	 */
+
+	spin_lock_irqsave(&pdom->lock, flags);
+	trans_dev_data->devid = trans_devid;
+	set_dte_entry(iommu, trans_dev_data, 0, 0);
+	if (list_empty(&aviommu->pdom_list))
+		list_add(&aviommu->pdom_list, &pdom->viommu_list);

Already updated in amd_iommufd_viommu_init() ?

drivers/iommu/amd/iommu.c static bool amd_iommu_enforce_cache_coherency(struct iommu_domain *domain) · line 3549

Quoted 13 lines
drivers/iommu/amd/iommu.c +13 -0
+	spin_unlock_irqrestore(&pdom->lock, flags);
+
+	pr_debug("%s: gid=%#x, iommu_devid=%#x, devid=%#x\n",
+		 __func__, gid, iommu->devid, trans_devid);
+	return 0;
+}
+
+static void amd_iommu_update_dte_slot(struct amd_iommu *iommu,
+				      struct iommu_dev_data *dev_data,
+				      u16 devid, struct dev_table_entry *new)
+{> +	update_dte256(iommu, dev_data, devid, new);
+	if (dev_data->dev)
+		clone_aliases(iommu, dev_data->dev);

We know there is no alias device for translation dte right?

Actually why not call amd_iommu_update_dte() . It calls device_flush_dte(), but for translation dte we are not setting ats_enabled flag.

-Vasant

author reviewer
#44 lore raw

PATCH v6 22/24 iommu/amd: Add translate-device-id alloc/free with vIOMMU owner

In reply to Suravee Suthikulpanit

Extend the per-segment translate-device-id pool to record the owning struct amd_iommu_viommu in the xarray. Add amd_iommu_trans_devid_alloc() and amd_iommu_trans_devid_free() to allocate ids from last_bdf down to 0 so each id indexes the device and alias tables, then return them on teardown. Free loads the xarray slot and erases it only when the owner matches @aviommu.

---
 drivers/iommu/amd/amd_viommu.h  |   4 ++
 drivers/iommu/amd/trans_devid.c | 105 ++++++++++++++++++++++++++++++--
 2 files changed, 105 insertions(+), 4 deletions(-)
drivers/iommu/amd/amd_viommu.h +4 -0
index 44a342608d43..3cf140d6439a 100644
@@ -34,6 +34,10 @@ void amd_iommu_pci_seg_trans_devid_fini(struct amd_iommu_pci_seg *pci_seg);
 int amd_iommu_trans_devid_reserve(struct amd_iommu_pci_seg *pci_seg, u16 id);
 int amd_iommu_trans_devid_reserve_pci_aliases(struct amd_iommu *iommu,
 					      struct device *dev);
+int amd_iommu_trans_devid_alloc(struct amd_iommu_pci_seg *pci_seg,
+				struct amd_iommu_viommu *aviommu);
+void amd_iommu_trans_devid_free(struct amd_iommu_pci_seg *pci_seg, u16 id,
+				struct amd_iommu_viommu *aviommu);
 #else
 
 /*
drivers/iommu/amd/trans_devid.c +101 -4

128 lines not on this page · raw message

Signature · 1 line
2.34.1
maintainer
#45 lore raw

Re: [PATCH v6 22/24] iommu/amd: Add translate-device-id alloc/free with vIOMMU owner

Quoted 6 lines · Suravee Suthikulpanit

Extend the per-segment translate-device-id pool to record the owning struct amd_iommu_viommu in the xarray. Add amd_iommu_trans_devid_alloc() and amd_iommu_trans_devid_free() to allocate ids from last_bdf down to 0 so each id indexes the device and alias tables, then return them on teardown. Free loads the xarray slot and erases it only when the owner matches @aviommu.

AI (Claude Opus 5.5) generated comment (seems correct, copy-pasting as is)

The pool marks a RID as used only if it went through amd_iommu_probe_device() (plus its PCI DMA aliases) or is the IOMMU itself. Are there RIDs the hardware uses that never reach probe_device()? For example:

- IOAPIC/HPET RIDs from IVRS special device entries

With this patch there are two per-segment xarrays indexed by the same thing, a device table slot: pci_seg->dev_data_xa and pci_seg->trans_devid_xa. The translate dev_data and the IOMMU's own dev_data are kept out of dev_data_xa, and that breaks the DTE locking. The interrupt remapping code finds the DTE lock by devid:

        static void set_dte_irq_entry(struct amd_iommu *iommu, u16 devid,
                                      struct irq_remap_table *table)
        {
                ...
                struct iommu_dev_data *dev_data = search_dev_data(iommu, devid);

                if (dev_data)
                        spin_lock(&dev_data->dte_lock);

                new = READ_ONCE(dte->data[2]);
                ...
                WRITE_ONCE(dte->data[2], new);

For a translate slot search_dev_data() returns NULL, so this read-modify-write of data[2] runs without a lock while write_dte_upper128() may be updating the same word under trans_dev_data->dte_lock. That can happen as soon as the pool hands out a RID that also has an IRQ remapping table, such as an IOAPIC or HPET RID that is not reserved today.

Could the two xarrays be merged into a single per-segment "DTE slot owner" table? Something like:

empty free, the translate allocator may use it
xa_mk_value(RESERVED) used by hardware but has no dev_data
(IVRS IOAPIC/HPET/ACPIHID, IVHD entries not yet enumerated)
struct iommu_dev_data * the driver programs this DTE; an owner
field says host device, alias, IOMMU self, or vIOMMU translate (with the aviommu pointer)

My comment:

I did some analysis and may be having single xa_array makes it easy to maintain. Any free slot in dev_data_xa is free for translation DTE. We can have additional flag in dev_data to track the translation devid vs actual devid. So that we can add checks to catch allocaton/free bugs. What do you think?

-Vasant

author reviewer
#46 lore raw

PATCH v6 23/24 iommu/amd: Assign per-vIOMMU translate device ID

In reply to Suravee Suthikulpanit

Allocate one translate-device-id per IOMMUFD vIOMMU instance from the per-segment pool on init. Reset MMIO first, then program translation DTE and VFctrl TransDevID under trans_devid_lock so PCI-probe relocate cannot interleave. Clear both on init error and destroy, and drop the id before amd_viommu_uninit_one().

Initialize pdom_list before set_translate_dte() so list_empty() is valid. Drop the late list_add; set_translate_dte() already publishes the vIOMMU on viommu_list under pdom->lock. list_del_init() on init error if that add ran.

Free the synthetic translate-device-id DTE object on init error and destroy; it is not stored in the per-segment xarray.

Add per-vIOMMU trans_devid_lock to serialize DTE and VFctrl TransDevID updates on init and teardown. VFctrl TransDevID is not written from amd_viommu_uninit_one().

Destroy trans_devid_lock on init error and on vIOMMU destroy, after the last unlock, the same way as vfctrl_lock.

---
 drivers/iommu/amd/amd_iommu_types.h | 10 +++++
 drivers/iommu/amd/iommufd.c         | 68 +++++++++++++++++++++++++----
 2 files changed, 69 insertions(+), 9 deletions(-)
drivers/iommu/amd/amd_iommu_types.h +10 -0
index 5f84f25e3f37..c748bab27509 100644
@@ -556,6 +556,16 @@ struct amd_iommu_viommu {
 	/* Synthetic DTE; not stored in pci_seg->dev_data_xa */
 	struct iommu_dev_data *trans_dev_data;
 
+	/*
+	 * Serializes this vIOMMU's translation DTE and VFctrl TransDevID
+	 * updates on init and teardown. VFctrl TransDevID is not written
+	 * from amd_viommu_uninit_one().
+	 *
+	 * Nested lock order: trans_devid_lock, then
+	 * pci_seg->trans_devid_mutex.
+	 */
+	struct mutex trans_devid_lock;
+
 	/*
 	 * Serializes VFCTRL Guest DID map CONTROL0/CONTROL1 doorbells
 	 * (DevID and DomID table updates) for this gid. Those registers
drivers/iommu/amd/iommufd.c +59 -9

115 lines not on this page · raw message

Signature · 1 line
2.34.1
maintainer
#47 lore raw

Re: [PATCH v6 23/24] iommu/amd: Assign per-vIOMMU translate device ID

Quoted 34 lines · Suravee Suthikulpanit

Allocate one translate-device-id per IOMMUFD vIOMMU instance from the per-segment pool on init. Reset MMIO first, then program translation DTE and VFctrl TransDevID under trans_devid_lock so PCI-probe relocate cannot interleave. Clear both on init error and destroy, and drop the id before amd_viommu_uninit_one().

Initialize pdom_list before set_translate_dte() so list_empty() is valid. Drop the late list_add; set_translate_dte() already publishes the vIOMMU on viommu_list under pdom->lock. list_del_init() on init error if that add ran.

Free the synthetic translate-device-id DTE object on init error and destroy; it is not stored in the per-segment xarray.

Add per-vIOMMU trans_devid_lock to serialize DTE and VFctrl TransDevID updates on init and teardown. VFctrl TransDevID is not written from amd_viommu_uninit_one().

Destroy trans_devid_lock on init error and on vIOMMU destroy, after the last unlock, the same way as vfctrl_lock.

---
 drivers/iommu/amd/amd_iommu_types.h | 10 +++++
 drivers/iommu/amd/iommufd.c         | 68 +++++++++++++++++++++++++----
 2 files changed, 69 insertions(+), 9 deletions(-)
drivers/iommu/amd/amd_iommu_types.h +0 -0
index 5f84f25e3f37..c748bab27509 100644
@@ -556,6 +556,16 @@ struct amd_iommu_viommu {
 	/* Synthetic DTE; not stored in pci_seg->dev_data_xa */
 	struct iommu_dev_data *trans_dev_data;

drivers/iommu/amd/amd_iommu_types.h struct amd_iommu_viommu { · line 567

Quoted 10 lines
drivers/iommu/amd/amd_iommu_types.h +9 -0
 
+	/*
+	 * Serializes this vIOMMU's translation DTE and VFctrl TransDevID
+	 * updates on init and teardown. VFctrl TransDevID is not written
+	 * from amd_viommu_uninit_one().
+	 *
+	 * Nested lock order: trans_devid_lock, then
+	 * pci_seg->trans_devid_mutex.
+	 */
+	struct mutex trans_devid_lock;

Do we really need another lock? We already have lock for vfctl update and allocating traslation device id.

-Vasant

author reviewer
#48 lore raw

PATCH v6 24/24 iommu/amd: Relocate vIOMMU translate-device-id on PCI reserve

In reply to Suravee Suthikulpanit

When PCI probe reserves a BDF already allocated to a vIOMMU, relocate that vIOMMU to a newly allocated translate-device-id before marking the BDF reserved for the device. Reserve holds the segment mutex and trylocks trans_devid_lock so the vIOMMU pointer stays valid without reversing the nested lock order. Reprogram translation DTE and VFctrl under trans_devid_lock; roll back the pool on failure.

Mark from_id reserved with a raw xa_store so ALLOCATED->RESERVED does not WARN on the non-NULL owner. On MMIO failure, restore from_id the same way (RESERVED->owner) instead of install_allocated.

Reuse the vIOMMU's trans_dev_data object. After programming the new slot, clear the old DTE without freeing the object.

---
 drivers/iommu/amd/amd_iommu_types.h |  10 +-
 drivers/iommu/amd/trans_devid.c     | 146 +++++++++++++++++++++++++++-
 2 files changed, 152 insertions(+), 4 deletions(-)
drivers/iommu/amd/amd_iommu_types.h +7 -3
index c748bab27509..347c6954439d 100644
@@ -558,11 +558,15 @@ struct amd_iommu_viommu {
 
 	/*
 	 * Serializes this vIOMMU's translation DTE and VFctrl TransDevID
-	 * updates on init and teardown. VFctrl TransDevID is not written
-	 * from amd_viommu_uninit_one().
+	 * updates on init and teardown against PCI-probe relocation, which
+	 * may reprogram the same registers to a newly allocated id.
+	 * VFctrl TransDevID is not written from amd_viommu_uninit_one().
 	 *
 	 * Nested lock order: trans_devid_lock, then
-	 * pci_seg->trans_devid_mutex.
+	 * pci_seg->trans_devid_mutex (same as destroy). Relocate drops
+	 * the segment mutex around the MMIO updates. Reserve holds the
+	 * segment mutex and mutex_trylock()s trans_devid_lock so the
+	 * aviommu pointer stays valid without reversing lock order.
 	 */
 	struct mutex trans_devid_lock;
 
drivers/iommu/amd/trans_devid.c +145 -1

175 lines not on this page · raw message

Signature · 1 line
2.34.1
maintainer
#49 lore raw

Re: [PATCH v6 24/24] iommu/amd: Relocate vIOMMU translate-device-id on PCI reserve

Quoted 25 lines · Suravee Suthikulpanit

When PCI probe reserves a BDF already allocated to a vIOMMU, relocate that vIOMMU to a newly allocated translate-device-id before marking the BDF reserved for the device. Reserve holds the segment mutex and trylocks trans_devid_lock so the vIOMMU pointer stays valid without reversing the nested lock order. Reprogram translation DTE and VFctrl under trans_devid_lock; roll back the pool on failure.

Mark from_id reserved with a raw xa_store so ALLOCATED->RESERVED does not WARN on the non-NULL owner. On MMIO failure, restore from_id the same way (RESERVED->owner) instead of install_allocated.

Reuse the vIOMMU's trans_dev_data object. After programming the new slot, clear the old DTE without freeing the object.

---
 drivers/iommu/amd/amd_iommu_types.h |  10 +-
 drivers/iommu/amd/trans_devid.c     | 146 +++++++++++++++++++++++++++-
 2 files changed, 152 insertions(+), 4 deletions(-)
drivers/iommu/amd/amd_iommu_types.h +0 -0
index c748bab27509..347c6954439d 100644

drivers/iommu/amd/amd_iommu_types.h struct amd_iommu_viommu { · line 558

Quoted 1 line
drivers/iommu/amd/amd_iommu_types.h +0 -0
@@ -558,11 +558,15 @@ struct amd_iommu_viommu {
 

.../...

Quoted 8 lines
drivers/iommu/amd/amd_iommu_types.h +7 -0
+	aviommu = trans_devid_xa_owner(entry);
+	if (aviommu) {
+		/*
+		 * Pin aviommu with trylock while the mutex still covers
+		 * the xarray pointer. A blocking lock here would invert
+		 * the nested order (ABBA with destroy). Dropping the
+		 * mutex first would UAF if destroy frees aviommu.

drivers/iommu/amd/amd_iommu_types.h struct amd_iommu_viommu {

Quoted 10 lines
drivers/iommu/amd/amd_iommu_types.h +10 -0
+		 *
+		 * No retry bound. cond_resched() avoids a soft lockup;
+		 * progress is this owner dropping trans_devid_lock or
+		 * freeing the slot.
+		 */
+		if (!mutex_trylock(&aviommu->trans_devid_lock)) {
+			mutex_unlock(&pci_seg->trans_devid_mutex);
+			cond_resched();
+			goto retry;
+		}

AI comment (looks genuine, hence copy-pasting as is)
This is an unbounded trylock/retry loop in amd_iommu_probe_device(), which runs under iommu_probe_device_lock. Would a single per-segment mutex covering both the pool and TransDevID/DTE programming remove the lock ordering problem, and with it the trylock? vIOMMU create and destroy are not hot paths.

The reservation also runs too late in probe. amd_iommu_probe_device() calls iommu_init_device() first, which goes through setup_aliases() -> clone_aliases() -> clone_alias(). clone_alias() reads the device's own DTE and copies it into every alias slot. Only afterwards does amd_iommu_trans_devid_reserve() run and relocate a vIOMMU that owns one of these RIDs. Two cases look wrong:

- The device's own RID is a live translate slot. The VM's translate DTE (GPA->SPA) is cloned into the alias slots, and the aliases keep pointing at the VM's address space until the default domain is attached.

- An alias RID is a live translate slot. find_dev_data() creates a host dev_data for it and clone_alias() overwrites the VM's translate DTE under the new dev_data's dte_lock, not trans_dev_data->dte_lock. Relocation then clears old_id, which wipes the alias DTE the host just wrote.

Shouldn't the reservation (and any relocation) complete before iommu_init_device()?

-Vasant