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-lib.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 enum iommu_page_response_code
73 iopf_handle_single(struct iopf_fault *iopf)
77 struct vm_area_struct *vma;
78 unsigned int access_flags = 0;
79 unsigned int fault_flags = FAULT_FLAG_REMOTE;
80 struct iommu_fault_page_request *prm = &iopf->fault.prm;
81 enum iommu_page_response_code status = IOMMU_PAGE_RESP_INVALID;
83 if (!(prm->flags & IOMMU_FAULT_PAGE_REQUEST_PASID_VALID))
86 mm = iommu_sva_find(prm->pasid);
87 if (IS_ERR_OR_NULL(mm))
92 vma = find_extend_vma(mm, prm->addr);
97 if (prm->perm & IOMMU_FAULT_PERM_READ)
98 access_flags |= VM_READ;
100 if (prm->perm & IOMMU_FAULT_PERM_WRITE) {
101 access_flags |= VM_WRITE;
102 fault_flags |= FAULT_FLAG_WRITE;
105 if (prm->perm & IOMMU_FAULT_PERM_EXEC) {
106 access_flags |= VM_EXEC;
107 fault_flags |= FAULT_FLAG_INSTRUCTION;
110 if (!(prm->perm & IOMMU_FAULT_PERM_PRIV))
111 fault_flags |= FAULT_FLAG_USER;
113 if (access_flags & ~vma->vm_flags)
117 ret = handle_mm_fault(vma, prm->addr, fault_flags, NULL);
118 status = ret & VM_FAULT_ERROR ? IOMMU_PAGE_RESP_INVALID :
119 IOMMU_PAGE_RESP_SUCCESS;
122 mmap_read_unlock(mm);
128 static void iopf_handle_group(struct work_struct *work)
130 struct iopf_group *group;
131 struct iopf_fault *iopf, *next;
132 enum iommu_page_response_code status = IOMMU_PAGE_RESP_SUCCESS;
134 group = container_of(work, struct iopf_group, work);
136 list_for_each_entry_safe(iopf, next, &group->faults, list) {
138 * For the moment, errors are sticky: don't handle subsequent
139 * faults in the group if there is an error.
141 if (status == IOMMU_PAGE_RESP_SUCCESS)
142 status = iopf_handle_single(iopf);
144 if (!(iopf->fault.prm.flags &
145 IOMMU_FAULT_PAGE_REQUEST_LAST_PAGE))
149 iopf_complete_group(group->dev, &group->last_fault, status);
154 * iommu_queue_iopf - IO Page Fault handler
155 * @fault: fault event
156 * @cookie: struct device, passed to iommu_register_device_fault_handler.
158 * Add a fault to the device workqueue, to be handled by mm.
160 * This module doesn't handle PCI PASID Stop Marker; IOMMU drivers must discard
161 * them before reporting faults. A PASID Stop Marker (LRW = 0b100) doesn't
162 * expect a response. It may be generated when disabling a PASID (issuing a
163 * PASID stop request) by some PCI devices.
165 * The PASID stop request is issued by the device driver before unbind(). Once
166 * it completes, no page request is generated for this PASID anymore and
167 * outstanding ones have been pushed to the IOMMU (as per PCIe 4.0r1.0 - 6.20.1
168 * and 10.4.1.2 - Managing PASID TLP Prefix Usage). Some PCI devices will wait
169 * for all outstanding page requests to come back with a response before
170 * completing the PASID stop request. Others do not wait for page responses, and
171 * instead issue this Stop Marker that tells us when the PASID can be
174 * It is safe to discard the Stop Marker because it is an optimization.
175 * a. Page requests, which are posted requests, have been flushed to the IOMMU
176 * when the stop request completes.
177 * b. The IOMMU driver flushes all fault queues on unbind() before freeing the
180 * So even though the Stop Marker might be issued by the device *after* the stop
181 * request completes, outstanding faults will have been dealt with by the time
182 * the PASID is freed.
184 * Return: 0 on success and <0 on error.
186 int iommu_queue_iopf(struct iommu_fault *fault, void *cookie)
189 struct iopf_group *group;
190 struct iopf_fault *iopf, *next;
191 struct iopf_device_param *iopf_param;
193 struct device *dev = cookie;
194 struct dev_iommu *param = dev->iommu;
196 lockdep_assert_held(¶m->lock);
198 if (fault->type != IOMMU_FAULT_PAGE_REQ)
199 /* Not a recoverable page fault */
203 * As long as we're holding param->lock, the queue can't be unlinked
204 * from the device and therefore cannot disappear.
206 iopf_param = param->iopf_param;
210 if (!(fault->prm.flags & IOMMU_FAULT_PAGE_REQUEST_LAST_PAGE)) {
211 iopf = kzalloc(sizeof(*iopf), GFP_KERNEL);
215 iopf->fault = *fault;
217 /* Non-last request of a group. Postpone until the last one */
218 list_add(&iopf->list, &iopf_param->partial);
223 group = kzalloc(sizeof(*group), GFP_KERNEL);
226 * The caller will send a response to the hardware. But we do
227 * need to clean up before leaving, otherwise partial faults
231 goto cleanup_partial;
235 group->last_fault.fault = *fault;
236 INIT_LIST_HEAD(&group->faults);
237 list_add(&group->last_fault.list, &group->faults);
238 INIT_WORK(&group->work, iopf_handle_group);
240 /* See if we have partial faults for this group */
241 list_for_each_entry_safe(iopf, next, &iopf_param->partial, list) {
242 if (iopf->fault.prm.grpid == fault->prm.grpid)
243 /* Insert *before* the last fault */
244 list_move(&iopf->list, &group->faults);
247 queue_work(iopf_param->queue->wq, &group->work);
251 list_for_each_entry_safe(iopf, next, &iopf_param->partial, list) {
252 if (iopf->fault.prm.grpid == fault->prm.grpid) {
253 list_del(&iopf->list);
259 EXPORT_SYMBOL_GPL(iommu_queue_iopf);
262 * iopf_queue_flush_dev - Ensure that all queued faults have been processed
263 * @dev: the endpoint whose faults need to be flushed.
265 * The IOMMU driver calls this before releasing a PASID, to ensure that all
266 * pending faults for this PASID have been handled, and won't hit the address
267 * space of the next process that uses this PASID. The driver must make sure
268 * that no new fault is added to the queue. In particular it must flush its
269 * low-level queue before calling this function.
271 * Return: 0 on success and <0 on error.
273 int iopf_queue_flush_dev(struct device *dev)
276 struct iopf_device_param *iopf_param;
277 struct dev_iommu *param = dev->iommu;
282 mutex_lock(¶m->lock);
283 iopf_param = param->iopf_param;
285 flush_workqueue(iopf_param->queue->wq);
288 mutex_unlock(¶m->lock);
292 EXPORT_SYMBOL_GPL(iopf_queue_flush_dev);
295 * iopf_queue_discard_partial - Remove all pending partial fault
296 * @queue: the queue whose partial faults need to be discarded
298 * When the hardware queue overflows, last page faults in a group may have been
299 * lost and the IOMMU driver calls this to discard all partial faults. The
300 * driver shouldn't be adding new faults to this queue concurrently.
302 * Return: 0 on success and <0 on error.
304 int iopf_queue_discard_partial(struct iopf_queue *queue)
306 struct iopf_fault *iopf, *next;
307 struct iopf_device_param *iopf_param;
312 mutex_lock(&queue->lock);
313 list_for_each_entry(iopf_param, &queue->devices, queue_list) {
314 list_for_each_entry_safe(iopf, next, &iopf_param->partial,
316 list_del(&iopf->list);
320 mutex_unlock(&queue->lock);
323 EXPORT_SYMBOL_GPL(iopf_queue_discard_partial);
326 * iopf_queue_add_device - Add producer to the fault queue
328 * @dev: device to add
330 * Return: 0 on success and <0 on error.
332 int iopf_queue_add_device(struct iopf_queue *queue, struct device *dev)
335 struct iopf_device_param *iopf_param;
336 struct dev_iommu *param = dev->iommu;
341 iopf_param = kzalloc(sizeof(*iopf_param), GFP_KERNEL);
345 INIT_LIST_HEAD(&iopf_param->partial);
346 iopf_param->queue = queue;
347 iopf_param->dev = dev;
349 mutex_lock(&queue->lock);
350 mutex_lock(¶m->lock);
351 if (!param->iopf_param) {
352 list_add(&iopf_param->queue_list, &queue->devices);
353 param->iopf_param = iopf_param;
356 mutex_unlock(¶m->lock);
357 mutex_unlock(&queue->lock);
364 EXPORT_SYMBOL_GPL(iopf_queue_add_device);
367 * iopf_queue_remove_device - Remove producer from fault queue
369 * @dev: device to remove
371 * Caller makes sure that no more faults are reported for this device.
373 * Return: 0 on success and <0 on error.
375 int iopf_queue_remove_device(struct iopf_queue *queue, struct device *dev)
378 struct iopf_fault *iopf, *next;
379 struct iopf_device_param *iopf_param;
380 struct dev_iommu *param = dev->iommu;
382 if (!param || !queue)
385 mutex_lock(&queue->lock);
386 mutex_lock(¶m->lock);
387 iopf_param = param->iopf_param;
388 if (iopf_param && iopf_param->queue == queue) {
389 list_del(&iopf_param->queue_list);
390 param->iopf_param = NULL;
393 mutex_unlock(¶m->lock);
394 mutex_unlock(&queue->lock);
398 /* Just in case some faults are still stuck */
399 list_for_each_entry_safe(iopf, next, &iopf_param->partial, list)
406 EXPORT_SYMBOL_GPL(iopf_queue_remove_device);
409 * iopf_queue_alloc - Allocate and initialize a fault queue
410 * @name: a unique string identifying the queue (for workqueue)
412 * Return: the queue on success and NULL on error.
414 struct iopf_queue *iopf_queue_alloc(const char *name)
416 struct iopf_queue *queue;
418 queue = kzalloc(sizeof(*queue), GFP_KERNEL);
423 * The WQ is unordered because the low-level handler enqueues faults by
424 * group. PRI requests within a group have to be ordered, but once
425 * that's dealt with, the high-level function can handle groups out of
428 queue->wq = alloc_workqueue("iopf_queue/%s", WQ_UNBOUND, 0, name);
434 INIT_LIST_HEAD(&queue->devices);
435 mutex_init(&queue->lock);
439 EXPORT_SYMBOL_GPL(iopf_queue_alloc);
442 * iopf_queue_free - Free IOPF queue
443 * @queue: queue to free
445 * Counterpart to iopf_queue_alloc(). The driver must not be queuing faults or
446 * adding/removing devices on this queue anymore.
448 void iopf_queue_free(struct iopf_queue *queue)
450 struct iopf_device_param *iopf_param, *next;
455 list_for_each_entry_safe(iopf_param, next, &queue->devices, queue_list)
456 iopf_queue_remove_device(queue, iopf_param->dev);
458 destroy_workqueue(queue->wq);
461 EXPORT_SYMBOL_GPL(iopf_queue_free);