1 // SPDX-License-Identifier: GPL-2.0
3 * Handle device page faults
5 * Copyright (C) 2020 ARM Ltd.
8 #include <linux/iommu.h>
9 #include <linux/list.h>
10 #include <linux/sched/mm.h>
11 #include <linux/slab.h>
12 #include <linux/workqueue.h>
14 #include "iommu-sva.h"
17 * struct iopf_queue - IO Page Fault queue
18 * @wq: the fault workqueue
19 * @devices: devices attached to this queue
20 * @lock: protects the device list
23 struct workqueue_struct *wq;
24 struct list_head devices;
29 * struct iopf_device_param - IO Page Fault data attached to a device
30 * @dev: the device that owns this param
32 * @queue_list: index into queue->devices
33 * @partial: faults that are part of a Page Request Group for which the last
34 * request hasn't been submitted yet.
36 struct iopf_device_param {
38 struct iopf_queue *queue;
39 struct list_head queue_list;
40 struct list_head partial;
44 struct iommu_fault fault;
45 struct list_head list;
49 struct iopf_fault last_fault;
50 struct list_head faults;
51 struct work_struct work;
55 static int iopf_complete_group(struct device *dev, struct iopf_fault *iopf,
56 enum iommu_page_response_code status)
58 struct iommu_page_response resp = {
59 .version = IOMMU_PAGE_RESP_VERSION_1,
60 .pasid = iopf->fault.prm.pasid,
61 .grpid = iopf->fault.prm.grpid,
65 if ((iopf->fault.prm.flags & IOMMU_FAULT_PAGE_REQUEST_PASID_VALID) &&
66 (iopf->fault.prm.flags & IOMMU_FAULT_PAGE_RESPONSE_NEEDS_PASID))
67 resp.flags = IOMMU_PAGE_RESP_PASID_VALID;
69 return iommu_page_response(dev, &resp);
72 static void iopf_handler(struct work_struct *work)
74 struct iopf_group *group;
75 struct iommu_domain *domain;
76 struct iopf_fault *iopf, *next;
77 enum iommu_page_response_code status = IOMMU_PAGE_RESP_SUCCESS;
79 group = container_of(work, struct iopf_group, work);
80 domain = iommu_get_domain_for_dev_pasid(group->dev,
81 group->last_fault.fault.prm.pasid, 0);
82 if (!domain || !domain->iopf_handler)
83 status = IOMMU_PAGE_RESP_INVALID;
85 list_for_each_entry_safe(iopf, next, &group->faults, list) {
87 * For the moment, errors are sticky: don't handle subsequent
88 * faults in the group if there is an error.
90 if (status == IOMMU_PAGE_RESP_SUCCESS)
91 status = domain->iopf_handler(&iopf->fault,
94 if (!(iopf->fault.prm.flags &
95 IOMMU_FAULT_PAGE_REQUEST_LAST_PAGE))
99 iopf_complete_group(group->dev, &group->last_fault, status);
104 * iommu_queue_iopf - IO Page Fault handler
105 * @fault: fault event
106 * @cookie: struct device, passed to iommu_register_device_fault_handler.
108 * Add a fault to the device workqueue, to be handled by mm.
110 * This module doesn't handle PCI PASID Stop Marker; IOMMU drivers must discard
111 * them before reporting faults. A PASID Stop Marker (LRW = 0b100) doesn't
112 * expect a response. It may be generated when disabling a PASID (issuing a
113 * PASID stop request) by some PCI devices.
115 * The PASID stop request is issued by the device driver before unbind(). Once
116 * it completes, no page request is generated for this PASID anymore and
117 * outstanding ones have been pushed to the IOMMU (as per PCIe 4.0r1.0 - 6.20.1
118 * and 10.4.1.2 - Managing PASID TLP Prefix Usage). Some PCI devices will wait
119 * for all outstanding page requests to come back with a response before
120 * completing the PASID stop request. Others do not wait for page responses, and
121 * instead issue this Stop Marker that tells us when the PASID can be
124 * It is safe to discard the Stop Marker because it is an optimization.
125 * a. Page requests, which are posted requests, have been flushed to the IOMMU
126 * when the stop request completes.
127 * b. The IOMMU driver flushes all fault queues on unbind() before freeing the
130 * So even though the Stop Marker might be issued by the device *after* the stop
131 * request completes, outstanding faults will have been dealt with by the time
132 * the PASID is freed.
134 * Any valid page fault will be eventually routed to an iommu domain and the
135 * page fault handler installed there will get called. The users of this
136 * handling framework should guarantee that the iommu domain could only be
137 * freed after the device has stopped generating page faults (or the iommu
138 * hardware has been set to block the page faults) and the pending page faults
141 * Return: 0 on success and <0 on error.
143 int iommu_queue_iopf(struct iommu_fault *fault, void *cookie)
146 struct iopf_group *group;
147 struct iopf_fault *iopf, *next;
148 struct iopf_device_param *iopf_param;
150 struct device *dev = cookie;
151 struct dev_iommu *param = dev->iommu;
153 lockdep_assert_held(¶m->lock);
155 if (fault->type != IOMMU_FAULT_PAGE_REQ)
156 /* Not a recoverable page fault */
160 * As long as we're holding param->lock, the queue can't be unlinked
161 * from the device and therefore cannot disappear.
163 iopf_param = param->iopf_param;
167 if (!(fault->prm.flags & IOMMU_FAULT_PAGE_REQUEST_LAST_PAGE)) {
168 iopf = kzalloc(sizeof(*iopf), GFP_KERNEL);
172 iopf->fault = *fault;
174 /* Non-last request of a group. Postpone until the last one */
175 list_add(&iopf->list, &iopf_param->partial);
180 group = kzalloc(sizeof(*group), GFP_KERNEL);
183 * The caller will send a response to the hardware. But we do
184 * need to clean up before leaving, otherwise partial faults
188 goto cleanup_partial;
192 group->last_fault.fault = *fault;
193 INIT_LIST_HEAD(&group->faults);
194 list_add(&group->last_fault.list, &group->faults);
195 INIT_WORK(&group->work, iopf_handler);
197 /* See if we have partial faults for this group */
198 list_for_each_entry_safe(iopf, next, &iopf_param->partial, list) {
199 if (iopf->fault.prm.grpid == fault->prm.grpid)
200 /* Insert *before* the last fault */
201 list_move(&iopf->list, &group->faults);
204 queue_work(iopf_param->queue->wq, &group->work);
208 list_for_each_entry_safe(iopf, next, &iopf_param->partial, list) {
209 if (iopf->fault.prm.grpid == fault->prm.grpid) {
210 list_del(&iopf->list);
216 EXPORT_SYMBOL_GPL(iommu_queue_iopf);
219 * iopf_queue_flush_dev - Ensure that all queued faults have been processed
220 * @dev: the endpoint whose faults need to be flushed.
222 * The IOMMU driver calls this before releasing a PASID, to ensure that all
223 * pending faults for this PASID have been handled, and won't hit the address
224 * space of the next process that uses this PASID. The driver must make sure
225 * that no new fault is added to the queue. In particular it must flush its
226 * low-level queue before calling this function.
228 * Return: 0 on success and <0 on error.
230 int iopf_queue_flush_dev(struct device *dev)
233 struct iopf_device_param *iopf_param;
234 struct dev_iommu *param = dev->iommu;
239 mutex_lock(¶m->lock);
240 iopf_param = param->iopf_param;
242 flush_workqueue(iopf_param->queue->wq);
245 mutex_unlock(¶m->lock);
249 EXPORT_SYMBOL_GPL(iopf_queue_flush_dev);
252 * iopf_queue_discard_partial - Remove all pending partial fault
253 * @queue: the queue whose partial faults need to be discarded
255 * When the hardware queue overflows, last page faults in a group may have been
256 * lost and the IOMMU driver calls this to discard all partial faults. The
257 * driver shouldn't be adding new faults to this queue concurrently.
259 * Return: 0 on success and <0 on error.
261 int iopf_queue_discard_partial(struct iopf_queue *queue)
263 struct iopf_fault *iopf, *next;
264 struct iopf_device_param *iopf_param;
269 mutex_lock(&queue->lock);
270 list_for_each_entry(iopf_param, &queue->devices, queue_list) {
271 list_for_each_entry_safe(iopf, next, &iopf_param->partial,
273 list_del(&iopf->list);
277 mutex_unlock(&queue->lock);
280 EXPORT_SYMBOL_GPL(iopf_queue_discard_partial);
283 * iopf_queue_add_device - Add producer to the fault queue
285 * @dev: device to add
287 * Return: 0 on success and <0 on error.
289 int iopf_queue_add_device(struct iopf_queue *queue, struct device *dev)
292 struct iopf_device_param *iopf_param;
293 struct dev_iommu *param = dev->iommu;
298 iopf_param = kzalloc(sizeof(*iopf_param), GFP_KERNEL);
302 INIT_LIST_HEAD(&iopf_param->partial);
303 iopf_param->queue = queue;
304 iopf_param->dev = dev;
306 mutex_lock(&queue->lock);
307 mutex_lock(¶m->lock);
308 if (!param->iopf_param) {
309 list_add(&iopf_param->queue_list, &queue->devices);
310 param->iopf_param = iopf_param;
313 mutex_unlock(¶m->lock);
314 mutex_unlock(&queue->lock);
321 EXPORT_SYMBOL_GPL(iopf_queue_add_device);
324 * iopf_queue_remove_device - Remove producer from fault queue
326 * @dev: device to remove
328 * Caller makes sure that no more faults are reported for this device.
330 * Return: 0 on success and <0 on error.
332 int iopf_queue_remove_device(struct iopf_queue *queue, struct device *dev)
335 struct iopf_fault *iopf, *next;
336 struct iopf_device_param *iopf_param;
337 struct dev_iommu *param = dev->iommu;
339 if (!param || !queue)
342 mutex_lock(&queue->lock);
343 mutex_lock(¶m->lock);
344 iopf_param = param->iopf_param;
345 if (iopf_param && iopf_param->queue == queue) {
346 list_del(&iopf_param->queue_list);
347 param->iopf_param = NULL;
350 mutex_unlock(¶m->lock);
351 mutex_unlock(&queue->lock);
355 /* Just in case some faults are still stuck */
356 list_for_each_entry_safe(iopf, next, &iopf_param->partial, list)
363 EXPORT_SYMBOL_GPL(iopf_queue_remove_device);
366 * iopf_queue_alloc - Allocate and initialize a fault queue
367 * @name: a unique string identifying the queue (for workqueue)
369 * Return: the queue on success and NULL on error.
371 struct iopf_queue *iopf_queue_alloc(const char *name)
373 struct iopf_queue *queue;
375 queue = kzalloc(sizeof(*queue), GFP_KERNEL);
380 * The WQ is unordered because the low-level handler enqueues faults by
381 * group. PRI requests within a group have to be ordered, but once
382 * that's dealt with, the high-level function can handle groups out of
385 queue->wq = alloc_workqueue("iopf_queue/%s", WQ_UNBOUND, 0, name);
391 INIT_LIST_HEAD(&queue->devices);
392 mutex_init(&queue->lock);
396 EXPORT_SYMBOL_GPL(iopf_queue_alloc);
399 * iopf_queue_free - Free IOPF queue
400 * @queue: queue to free
402 * Counterpart to iopf_queue_alloc(). The driver must not be queuing faults or
403 * adding/removing devices on this queue anymore.
405 void iopf_queue_free(struct iopf_queue *queue)
407 struct iopf_device_param *iopf_param, *next;
412 list_for_each_entry_safe(iopf_param, next, &queue->devices, queue_list)
413 iopf_queue_remove_device(queue, iopf_param->dev);
415 destroy_workqueue(queue->wq);
418 EXPORT_SYMBOL_GPL(iopf_queue_free);