Skip to contentSkip to conversation

iommu: Trace I/O page fault response latency

PATCH Superseded Zhang Yuwei 3 participants 4 messages Nov 25

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 iommu: Trace I/O page fault response latency

From: Chen Jun <[email protected]>

Carry an optional monotonic timestamp with each IOPF and preserve it across group construction, including allocation error paths. Sample it at the common report entry point and record latency after the driver's page_response callback returns.

Cover asynchronous group responses and synchronous error responses. For grouped requests, measure from reporting the last fault in the group. Use existing tracing configuration and record fault identity, response code and elapsed nanoseconds without command submission return values. Print PASID validity inline as pasid(v) or pasid(i), retaining the raw validity field for programmatic consumers.

Document the software timing boundaries and tracefs interface.

The implementation was validated with x86_64 builds with tracing enabled and tracing disabled.

---
 Documentation/trace/index.rst      |  1 +
 Documentation/trace/iommu-iopf.rst | 52 ++++++++++++++++++++++++++++++
 MAINTAINERS                        |  2 ++
 drivers/iommu/io-pgfault.c         | 17 ++++++++++
 drivers/iommu/iommu-traces.c       |  1 +
 include/linux/iommu.h              |  2 ++
 include/trace/events/iommu.h       | 36 +++++++++++++++++++++
 7 files changed, 111 insertions(+)
 create mode 100644 Documentation/trace/iommu-iopf.rst
Documentation/trace/index.rst +1 -0
index f4058e8e92e3..e61e428ecd68 100644
@@ -57,6 +57,7 @@ applications.
    events-landlock
    events-pci
    events-pci-controller
+   iommu-iopf
    boottime-trace
    histogram
    histogram-design
Documentation/trace/iommu-iopf.rst new file +52 -0

52 lines not on this page · raw message

MAINTAINERS +2 -0
index 782389bd5cd3..82217afd67e2 100644
@@ -13704,10 +13704,12 @@ L:	[email protected]
 S:	Maintained
 T:	git git://git.kernel.org/pub/scm/linux/kernel/git/iommu/linux.git
 F:	Documentation/devicetree/bindings/iommu/
+F:	Documentation/trace/iommu-iopf.rst
 F:	drivers/iommu/
 F:	include/linux/iommu.h
 F:	include/linux/iova.h
 F:	include/linux/of_iommu.h
+F:	include/trace/events/iommu.h
 F:	rust/kernel/iommu/
 
 IOMMUFD
drivers/iommu/io-pgfault.c +17 -0
index c16ff1fc4b95..72e654ea5ffe 100644
@@ -6,11 +6,14 @@
  */
 
 #include <linux/iommu.h>
+#include <linux/ktime.h>
 #include <linux/list.h>
 #include <linux/sched/mm.h>
 #include <linux/slab.h>
 #include <linux/workqueue.h>
 
+#include <trace/events/iommu.h>
+
 #include "iommu-priv.h"
 
 /*
@@ -96,6 +99,7 @@ static struct iopf_group *iopf_group_alloc(struct iommu_fault_param *iopf_param,
 
 	group->fault_param = iopf_param;
 	group->last_fault.fault = evt->fault;
+	group->last_fault.report_time_ns = evt->report_time_ns;
 	INIT_LIST_HEAD(&group->faults);
 	INIT_LIST_HEAD(&group->pending_node);
 	list_add(&group->last_fault.list, &group->faults);
@@ -155,6 +159,14 @@ static struct iommu_attach_handle *find_fault_handler(struct device *dev,
 	return attach_handle;
 }
 
+static void iopf_trace_response(struct device *dev, const struct iopf_fault *evt,
+				const struct iommu_page_response *resp)
+{
+	if (trace_iopf_latency_enabled() && evt->report_time_ns)
+		trace_iopf_latency(dev, evt, resp,
+				   ktime_get_ns() - evt->report_time_ns);
+}
+
 static void iopf_error_response(struct device *dev, struct iopf_fault *evt)
 {
 	const struct iommu_ops *ops = dev_iommu_ops(dev);
@@ -166,6 +178,7 @@ static void iopf_error_response(struct device *dev, struct iopf_fault *evt)
 	};
 
 	ops->page_response(dev, evt, &resp);
+	iopf_trace_response(dev, evt, &resp);
 }
 
 /**
@@ -219,6 +232,9 @@ int iommu_report_device_fault(struct device *dev, struct iopf_fault *evt)
 	struct iopf_group abort_group = {};
 	struct iopf_group *group;
 
+	/* Error paths may respond synchronously before reporting returns. */
+	evt->report_time_ns = trace_iopf_latency_enabled() ? ktime_get_ns() : 0;
+
 	attach_handle = find_fault_handler(dev, evt);
 	if (!attach_handle)
 		goto err_bad_iopf;
@@ -334,6 +350,7 @@ void iopf_group_response(struct iopf_group *group,
 
 	mutex_lock(&fault_param->lock);
 	ops->page_response(dev, &group->last_fault, &resp);
+	iopf_trace_response(dev, iopf, &resp);
 	list_del_init(&group->pending_node);
 	mutex_unlock(&fault_param->lock);
 }
drivers/iommu/iommu-traces.c +1 -0
index 23416bf76df9..ce2ab3277b5f 100644
@@ -25,3 +25,4 @@ EXPORT_TRACEPOINT_SYMBOL_GPL(unmap);
 
 /* iommu_error */
 EXPORT_TRACEPOINT_SYMBOL_GPL(io_page_fault);
+EXPORT_TRACEPOINT_SYMBOL_GPL(iopf_latency);
include/linux/iommu.h +2 -0
index ac43b8b93f14..d797b42fdb20 100644
@@ -125,6 +125,8 @@ struct iopf_fault {
 	struct iommu_fault fault;
 	/* node for pending lists */
 	struct list_head list;
+	/* Fault report timestamp in ns, or zero if not sampled. */
+	u64 report_time_ns;
 };
 
 struct iopf_group {
include/trace/events/iommu.h +36 -0
index 373007e567cb..cfcc29a792b2 100644
@@ -11,6 +11,8 @@
 #if !defined(_TRACE_IOMMU_H) || defined(TRACE_HEADER_MULTI_READ)
 #define _TRACE_IOMMU_H
 
+#include <linux/device.h>
+#include <linux/iommu.h>
 #include <linux/tracepoint.h>
 
 struct device;
@@ -156,6 +158,40 @@ DEFINE_EVENT(iommu_error, io_page_fault,
 
 	TP_ARGS(dev, iova, flags)
 );
+
+TRACE_EVENT(iopf_latency,
+
+	TP_PROTO(struct device *dev, const struct iopf_fault *evt,
+		 const struct iommu_page_response *resp, u64 duration_ns),
+
+	TP_ARGS(dev, evt, resp, duration_ns),
+
+	TP_STRUCT__entry(
+		__string(device, dev_name(dev))
+		__field(u32, pasid)
+		__field(bool, pasid_valid)
+		__field(u32, grpid)
+		__field(u64, addr)
+		__field(u32, response)
+		__field(u64, duration_ns)
+	),
+
+	TP_fast_assign(
+		__assign_str(device);
+		__entry->pasid = evt->fault.prm.pasid;
+		__entry->pasid_valid = evt->fault.prm.flags &
+			IOMMU_FAULT_PAGE_REQUEST_PASID_VALID;
+		__entry->grpid = resp->grpid;
+		__entry->addr = evt->fault.prm.addr;
+		__entry->response = resp->code;
+		__entry->duration_ns = duration_ns;
+	),
+
+	TP_printk("device=%s pasid(%c)=%u grpid=%u addr=0x%llx response=%u duration_ns=%llu",
+		  __get_str(device), __entry->pasid_valid ? 'v' : 'i', __entry->pasid,
+		  __entry->grpid, __entry->addr, __entry->response,
+		  __entry->duration_ns)
+);
 #endif /* _TRACE_IOMMU_H */
 
 /* This part must be outside protection */
Signature · 1 line
2.22.0
#1 lore raw
Thank you for your contribution! Sashiko AI review found 2 potential issues to consider: - [Low] docs: iommu: fix short title underline in Documentation/trace/iommu-iopf.rst - [Low] iommu: missing…

Thank you for your contribution! Sashiko AI review found 2 potential issues to consider:
- [Low] docs: iommu: fix short title underline in Documentation/trace/iommu-iopf.rst
- [Low] iommu: missing iopf_trace_response() call in iopf_queue_remove_device()
--

commit 98dd7f9efb7c0639fc5bdb2729afb3491dcaf701
Author: Chen Jun <[email protected]>

iommu: Trace I/O page fault response latency

This commit adds the iommu:iopf_latency tracepoint to measure software I/O page fault response latency from iommu_report_device_fault() until the driver's page_response() callback returns, along with tracefs documentation.

[ ... ]

Documentation/trace/iommu-iopf.rst line 4

Quoted 10 lines
Documentation/trace/iommu-iopf.rst new file +4 -0
new file mode 100644
index 0000000000000..259cf23c68992
@@ -0,0 +1,52 @@
+.. SPDX-License-Identifier: GPL-2.0
+
+IOMMU I/O page fault response latency
+====================================

[Severity: Low]
The title underline is 36 "=" characters for a 37-character heading, which causes Sphinx to emit a "Title underline too short" warning during htmldocs builds.

[ ... ]

Quoted 4 lines
drivers/iommu/io-pgfault.c +0 -0
index c16ff1fc4b95b..72e654ea5ffe9 100644

[ ... ]

drivers/iommu/io-pgfault.c void iopf_group_response(struct iopf_group *group, · line 356

Quoted 8 lines
drivers/iommu/io-pgfault.c +1 -0
@@ -334,6 +350,7 @@ void iopf_group_response(struct iopf_group *group,
 
 	mutex_lock(&fault_param->lock);
 	ops->page_response(dev, &group->last_fault, &resp);
+	iopf_trace_response(dev, iopf, &resp);
 	list_del_init(&group->pending_node);
 	mutex_unlock(&fault_param->lock);
 }

[Severity: Low]
Should iopf_queue_remove_device() also call iopf_trace_response() after ops->page_response()?

drivers/iommu/io-pgfault.c:iopf_queue_remove_device() {
    ...
	list_for_each_entry_safe(group, temp, &fault_param->faults, pending_node) {
		struct iopf_fault *iopf = &group->last_fault;
		struct iommu_page_response resp = {
			.pasid = iopf->fault.prm.pasid,
			.grpid = iopf->fault.prm.grpid,
			.code = IOMMU_PAGE_RESP_INVALID
		};

		ops->page_response(dev, iopf, &resp);
		list_del_init(&group->pending_node);
		iopf_free_group(group);
	}
    ...
}

When a device is removed from an IOPF queue while fault groups are still pending on fault_param->faults (which already had group->last_fault.report_time_ns populated by iopf_group_alloc()), those groups are completed with IOMMU_PAGE_RESP_INVALID via ops->page_response() without emitting the iopf_latency event.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
maintainer
#2 lore raw
In reply to Zhang Yuwei

drivers/iommu/io-pgfault.c static struct iommu_attach_handle *find_fault_handler(struct device *dev, · line 167

Quoted 10 lines · On Wed, 25 Nov 2026 09:34:30 +0800 Zhang Yuwei <[email protected]> wrote:
drivers/iommu/io-pgfault.c +6 -0
@@ -155,6 +159,14 @@ static struct iommu_attach_handle *find_fault_handler(struct device *dev,
 	return attach_handle;
 }
 
+static void iopf_trace_response(struct device *dev, const struct iopf_fault *evt,
+				const struct iommu_page_response *resp)
+{
+	if (trace_iopf_latency_enabled() && evt->report_time_ns)
+		trace_iopf_latency(dev, evt, resp,
+				   ktime_get_ns() - evt->report_time_ns);

Please use:

		trace_call__iopf_latency(dev, evt, resp,
					 ktime_get_ns() - evt->report_time_ns);

As the trace_iopf_latency_enabled() already does check if the event is enabled. Using the trace_call__ variant will always call the hooks without adding another static branch.

-- Steve

Quoted 2 lines
drivers/iommu/io-pgfault.c +2 -0
+}
+
#3 lore raw

PATCH v2 1/1 iommu: Trace I/O page fault response latency

In reply to Zhang Yuwei

From: Chen Jun <[email protected]>

Carry an optional monotonic timestamp with each IOPF and preserve it across group construction, including allocation error paths. Sample it at the common report entry point and record latency after the driver's page_response callback returns.

Cover asynchronous group responses, synchronous error responses, and pending groups completed by iopf_queue_remove_device(). For grouped requests, measure from reporting the last fault in the group. Use existing tracing configuration and record fault identity, response code and elapsed nanoseconds without command submission return values. Print PASID validity inline as pasid(v) or pasid(i), retaining the raw validity field for programmatic consumers.

Use the trace_call__ variant after the enabled check to avoid a second static branch. Document the software timing boundaries and tracefs interface.

The implementation was validated with x86_64 builds with tracepoints enabled and disabled.

---
 Documentation/trace/index.rst      |  1 +
 Documentation/trace/iommu-iopf.rst | 54 ++++++++++++++++++++++++++++++
 MAINTAINERS                        |  2 ++
 drivers/iommu/io-pgfault.c         | 18 ++++++++++
 drivers/iommu/iommu-traces.c       |  1 +
 include/linux/iommu.h              |  2 ++
 include/trace/events/iommu.h       | 36 ++++++++++++++++++++
 7 files changed, 114 insertions(+)
 create mode 100644 Documentation/trace/iommu-iopf.rst
Documentation/trace/index.rst +1 -0
index f4058e8e92e3..e61e428ecd68 100644
@@ -57,6 +57,7 @@ applications.
    events-landlock
    events-pci
    events-pci-controller
+   iommu-iopf
    boottime-trace
    histogram
    histogram-design
Documentation/trace/iommu-iopf.rst new file +54 -0

54 lines not on this page · raw message

MAINTAINERS +2 -0
index 742c4e6b291f..8eed908d6c24 100644
@@ -14019,10 +14019,12 @@ L:	[email protected]
 S:	Maintained
 T:	git git://git.kernel.org/pub/scm/linux/kernel/git/iommu/linux.git
 F:	Documentation/devicetree/bindings/iommu/
+F:	Documentation/trace/iommu-iopf.rst
 F:	drivers/iommu/
 F:	include/linux/iommu.h
 F:	include/linux/iova.h
 F:	include/linux/of_iommu.h
+F:	include/trace/events/iommu.h
 F:	rust/kernel/iommu/
 
 IOMMUFD
drivers/iommu/io-pgfault.c +18 -0
index c16ff1fc4b95..ccf806528f6b 100644
@@ -6,11 +6,14 @@
  */
 
 #include <linux/iommu.h>
+#include <linux/ktime.h>
 #include <linux/list.h>
 #include <linux/sched/mm.h>
 #include <linux/slab.h>
 #include <linux/workqueue.h>
 
+#include <trace/events/iommu.h>
+
 #include "iommu-priv.h"
 
 /*
@@ -96,6 +99,7 @@ static struct iopf_group *iopf_group_alloc(struct iommu_fault_param *iopf_param,
 
 	group->fault_param = iopf_param;
 	group->last_fault.fault = evt->fault;
+	group->last_fault.report_time_ns = evt->report_time_ns;
 	INIT_LIST_HEAD(&group->faults);
 	INIT_LIST_HEAD(&group->pending_node);
 	list_add(&group->last_fault.list, &group->faults);
@@ -155,6 +159,14 @@ static struct iommu_attach_handle *find_fault_handler(struct device *dev,
 	return attach_handle;
 }
 
+static void iopf_trace_response(struct device *dev, const struct iopf_fault *evt,
+				const struct iommu_page_response *resp)
+{
+	if (trace_iopf_latency_enabled() && evt->report_time_ns)
+		trace_call__iopf_latency(dev, evt, resp,
+					 ktime_get_ns() - evt->report_time_ns);
+}
+
 static void iopf_error_response(struct device *dev, struct iopf_fault *evt)
 {
 	const struct iommu_ops *ops = dev_iommu_ops(dev);
@@ -166,6 +178,7 @@ static void iopf_error_response(struct device *dev, struct iopf_fault *evt)
 	};
 
 	ops->page_response(dev, evt, &resp);
+	iopf_trace_response(dev, evt, &resp);
 }
 
 /**
@@ -219,6 +232,9 @@ int iommu_report_device_fault(struct device *dev, struct iopf_fault *evt)
 	struct iopf_group abort_group = {};
 	struct iopf_group *group;
 
+	/* Error paths may respond synchronously before reporting returns. */
+	evt->report_time_ns = trace_iopf_latency_enabled() ? ktime_get_ns() : 0;
+
 	attach_handle = find_fault_handler(dev, evt);
 	if (!attach_handle)
 		goto err_bad_iopf;
@@ -334,6 +350,7 @@ void iopf_group_response(struct iopf_group *group,
 
 	mutex_lock(&fault_param->lock);
 	ops->page_response(dev, &group->last_fault, &resp);
+	iopf_trace_response(dev, iopf, &resp);
 	list_del_init(&group->pending_node);
 	mutex_unlock(&fault_param->lock);
 }
@@ -491,6 +508,7 @@ void iopf_queue_remove_device(struct iopf_queue *queue, struct device *dev)
 		};
 
 		ops->page_response(dev, iopf, &resp);
+		iopf_trace_response(dev, iopf, &resp);
 		list_del_init(&group->pending_node);
 		iopf_free_group(group);
 	}
drivers/iommu/iommu-traces.c +1 -0
index 23416bf76df9..ce2ab3277b5f 100644
@@ -25,3 +25,4 @@ EXPORT_TRACEPOINT_SYMBOL_GPL(unmap);
 
 /* iommu_error */
 EXPORT_TRACEPOINT_SYMBOL_GPL(io_page_fault);
+EXPORT_TRACEPOINT_SYMBOL_GPL(iopf_latency);
include/linux/iommu.h +2 -0
index ef5043752a36..199681a8841c 100644
@@ -125,6 +125,8 @@ struct iopf_fault {
 	struct iommu_fault fault;
 	/* node for pending lists */
 	struct list_head list;
+	/* Fault report timestamp in ns, or zero if not sampled. */
+	u64 report_time_ns;
 };
 
 struct iopf_group {
include/trace/events/iommu.h +36 -0
index 373007e567cb..cfcc29a792b2 100644
@@ -11,6 +11,8 @@
 #if !defined(_TRACE_IOMMU_H) || defined(TRACE_HEADER_MULTI_READ)
 #define _TRACE_IOMMU_H
 
+#include <linux/device.h>
+#include <linux/iommu.h>
 #include <linux/tracepoint.h>
 
 struct device;
@@ -156,6 +158,40 @@ DEFINE_EVENT(iommu_error, io_page_fault,
 
 	TP_ARGS(dev, iova, flags)
 );
+
+TRACE_EVENT(iopf_latency,
+
+	TP_PROTO(struct device *dev, const struct iopf_fault *evt,
+		 const struct iommu_page_response *resp, u64 duration_ns),
+
+	TP_ARGS(dev, evt, resp, duration_ns),
+
+	TP_STRUCT__entry(
+		__string(device, dev_name(dev))
+		__field(u32, pasid)
+		__field(bool, pasid_valid)
+		__field(u32, grpid)
+		__field(u64, addr)
+		__field(u32, response)
+		__field(u64, duration_ns)
+	),
+
+	TP_fast_assign(
+		__assign_str(device);
+		__entry->pasid = evt->fault.prm.pasid;
+		__entry->pasid_valid = evt->fault.prm.flags &
+			IOMMU_FAULT_PAGE_REQUEST_PASID_VALID;
+		__entry->grpid = resp->grpid;
+		__entry->addr = evt->fault.prm.addr;
+		__entry->response = resp->code;
+		__entry->duration_ns = duration_ns;
+	),
+
+	TP_printk("device=%s pasid(%c)=%u grpid=%u addr=0x%llx response=%u duration_ns=%llu",
+		  __get_str(device), __entry->pasid_valid ? 'v' : 'i', __entry->pasid,
+		  __entry->grpid, __entry->addr, __entry->response,
+		  __entry->duration_ns)
+);
 #endif /* _TRACE_IOMMU_H */
 
 /* This part must be outside protection */
Signature · 1 line
2.22.0