1 // SPDX-License-Identifier: GPL-2.0-only
2 /* Copyright (C) 2024 Intel Corporation
4 #define pr_fmt(fmt) "iommufd: " fmt
6 #include <linux/anon_inodes.h>
7 #include <linux/file.h>
9 #include <linux/iommufd.h>
10 #include <linux/module.h>
11 #include <linux/mutex.h>
12 #include <linux/pci.h>
13 #include <linux/pci-ats.h>
14 #include <linux/poll.h>
15 #include <uapi/linux/iommufd.h>
17 #include "../iommu-priv.h"
18 #include "iommufd_private.h"
20 static int iommufd_fault_iopf_enable(struct iommufd_device
*idev
)
22 struct device
*dev
= idev
->dev
;
26 * Once we turn on PCI/PRI support for VF, the response failure code
27 * should not be forwarded to the hardware due to PRI being a shared
28 * resource between PF and VFs. There is no coordination for this
29 * shared capability. This waits for a vPRI reset to recover.
31 if (dev_is_pci(dev
)) {
32 struct pci_dev
*pdev
= to_pci_dev(dev
);
34 if (pdev
->is_virtfn
&& pci_pri_supported(pdev
))
38 mutex_lock(&idev
->iopf_lock
);
39 /* Device iopf has already been on. */
40 if (++idev
->iopf_enabled
> 1) {
41 mutex_unlock(&idev
->iopf_lock
);
45 ret
= iommu_dev_enable_feature(dev
, IOMMU_DEV_FEAT_IOPF
);
48 mutex_unlock(&idev
->iopf_lock
);
53 static void iommufd_fault_iopf_disable(struct iommufd_device
*idev
)
55 mutex_lock(&idev
->iopf_lock
);
56 if (!WARN_ON(idev
->iopf_enabled
== 0)) {
57 if (--idev
->iopf_enabled
== 0)
58 iommu_dev_disable_feature(idev
->dev
, IOMMU_DEV_FEAT_IOPF
);
60 mutex_unlock(&idev
->iopf_lock
);
63 static int __fault_domain_attach_dev(struct iommufd_hw_pagetable
*hwpt
,
64 struct iommufd_device
*idev
)
66 struct iommufd_attach_handle
*handle
;
69 handle
= kzalloc(sizeof(*handle
), GFP_KERNEL
);
74 ret
= iommu_attach_group_handle(hwpt
->domain
, idev
->igroup
->group
,
82 int iommufd_fault_domain_attach_dev(struct iommufd_hw_pagetable
*hwpt
,
83 struct iommufd_device
*idev
)
90 ret
= iommufd_fault_iopf_enable(idev
);
94 ret
= __fault_domain_attach_dev(hwpt
, idev
);
96 iommufd_fault_iopf_disable(idev
);
101 static void iommufd_auto_response_faults(struct iommufd_hw_pagetable
*hwpt
,
102 struct iommufd_attach_handle
*handle
)
104 struct iommufd_fault
*fault
= hwpt
->fault
;
105 struct iopf_group
*group
, *next
;
111 mutex_lock(&fault
->mutex
);
112 list_for_each_entry_safe(group
, next
, &fault
->deliver
, node
) {
113 if (group
->attach_handle
!= &handle
->handle
)
115 list_del(&group
->node
);
116 iopf_group_response(group
, IOMMU_PAGE_RESP_INVALID
);
117 iopf_free_group(group
);
120 xa_for_each(&fault
->response
, index
, group
) {
121 if (group
->attach_handle
!= &handle
->handle
)
123 xa_erase(&fault
->response
, index
);
124 iopf_group_response(group
, IOMMU_PAGE_RESP_INVALID
);
125 iopf_free_group(group
);
127 mutex_unlock(&fault
->mutex
);
130 static struct iommufd_attach_handle
*
131 iommufd_device_get_attach_handle(struct iommufd_device
*idev
)
133 struct iommu_attach_handle
*handle
;
135 handle
= iommu_attach_handle_get(idev
->igroup
->group
, IOMMU_NO_PASID
, 0);
139 return to_iommufd_handle(handle
);
142 void iommufd_fault_domain_detach_dev(struct iommufd_hw_pagetable
*hwpt
,
143 struct iommufd_device
*idev
)
145 struct iommufd_attach_handle
*handle
;
147 handle
= iommufd_device_get_attach_handle(idev
);
148 iommu_detach_group_handle(hwpt
->domain
, idev
->igroup
->group
);
149 iommufd_auto_response_faults(hwpt
, handle
);
150 iommufd_fault_iopf_disable(idev
);
154 static int __fault_domain_replace_dev(struct iommufd_device
*idev
,
155 struct iommufd_hw_pagetable
*hwpt
,
156 struct iommufd_hw_pagetable
*old
)
158 struct iommufd_attach_handle
*handle
, *curr
= NULL
;
162 curr
= iommufd_device_get_attach_handle(idev
);
165 handle
= kzalloc(sizeof(*handle
), GFP_KERNEL
);
170 ret
= iommu_replace_group_handle(idev
->igroup
->group
,
171 hwpt
->domain
, &handle
->handle
);
173 ret
= iommu_replace_group_handle(idev
->igroup
->group
,
178 iommufd_auto_response_faults(old
, curr
);
185 int iommufd_fault_domain_replace_dev(struct iommufd_device
*idev
,
186 struct iommufd_hw_pagetable
*hwpt
,
187 struct iommufd_hw_pagetable
*old
)
189 bool iopf_off
= !hwpt
->fault
&& old
->fault
;
190 bool iopf_on
= hwpt
->fault
&& !old
->fault
;
194 ret
= iommufd_fault_iopf_enable(idev
);
199 ret
= __fault_domain_replace_dev(idev
, hwpt
, old
);
202 iommufd_fault_iopf_disable(idev
);
207 iommufd_fault_iopf_disable(idev
);
212 void iommufd_fault_destroy(struct iommufd_object
*obj
)
214 struct iommufd_fault
*fault
= container_of(obj
, struct iommufd_fault
, obj
);
215 struct iopf_group
*group
, *next
;
218 * The iommufd object's reference count is zero at this point.
219 * We can be confident that no other threads are currently
220 * accessing this pointer. Therefore, acquiring the mutex here
223 list_for_each_entry_safe(group
, next
, &fault
->deliver
, node
) {
224 list_del(&group
->node
);
225 iopf_group_response(group
, IOMMU_PAGE_RESP_INVALID
);
226 iopf_free_group(group
);
230 static void iommufd_compose_fault_message(struct iommu_fault
*fault
,
231 struct iommu_hwpt_pgfault
*hwpt_fault
,
232 struct iommufd_device
*idev
,
235 hwpt_fault
->flags
= fault
->prm
.flags
;
236 hwpt_fault
->dev_id
= idev
->obj
.id
;
237 hwpt_fault
->pasid
= fault
->prm
.pasid
;
238 hwpt_fault
->grpid
= fault
->prm
.grpid
;
239 hwpt_fault
->perm
= fault
->prm
.perm
;
240 hwpt_fault
->addr
= fault
->prm
.addr
;
241 hwpt_fault
->length
= 0;
242 hwpt_fault
->cookie
= cookie
;
245 static ssize_t
iommufd_fault_fops_read(struct file
*filep
, char __user
*buf
,
246 size_t count
, loff_t
*ppos
)
248 size_t fault_size
= sizeof(struct iommu_hwpt_pgfault
);
249 struct iommufd_fault
*fault
= filep
->private_data
;
250 struct iommu_hwpt_pgfault data
;
251 struct iommufd_device
*idev
;
252 struct iopf_group
*group
;
253 struct iopf_fault
*iopf
;
257 if (*ppos
|| count
% fault_size
)
260 mutex_lock(&fault
->mutex
);
261 while (!list_empty(&fault
->deliver
) && count
> done
) {
262 group
= list_first_entry(&fault
->deliver
,
263 struct iopf_group
, node
);
265 if (group
->fault_count
* fault_size
> count
- done
)
268 rc
= xa_alloc(&fault
->response
, &group
->cookie
, group
,
269 xa_limit_32b
, GFP_KERNEL
);
273 idev
= to_iommufd_handle(group
->attach_handle
)->idev
;
274 list_for_each_entry(iopf
, &group
->faults
, list
) {
275 iommufd_compose_fault_message(&iopf
->fault
,
278 if (copy_to_user(buf
+ done
, &data
, fault_size
)) {
279 xa_erase(&fault
->response
, group
->cookie
);
286 list_del(&group
->node
);
288 mutex_unlock(&fault
->mutex
);
290 return done
== 0 ? rc
: done
;
293 static ssize_t
iommufd_fault_fops_write(struct file
*filep
, const char __user
*buf
,
294 size_t count
, loff_t
*ppos
)
296 size_t response_size
= sizeof(struct iommu_hwpt_page_response
);
297 struct iommufd_fault
*fault
= filep
->private_data
;
298 struct iommu_hwpt_page_response response
;
299 struct iopf_group
*group
;
303 if (*ppos
|| count
% response_size
)
306 mutex_lock(&fault
->mutex
);
307 while (count
> done
) {
308 rc
= copy_from_user(&response
, buf
+ done
, response_size
);
312 static_assert((int)IOMMUFD_PAGE_RESP_SUCCESS
==
313 (int)IOMMU_PAGE_RESP_SUCCESS
);
314 static_assert((int)IOMMUFD_PAGE_RESP_INVALID
==
315 (int)IOMMU_PAGE_RESP_INVALID
);
316 if (response
.code
!= IOMMUFD_PAGE_RESP_SUCCESS
&&
317 response
.code
!= IOMMUFD_PAGE_RESP_INVALID
) {
322 group
= xa_erase(&fault
->response
, response
.cookie
);
328 iopf_group_response(group
, response
.code
);
329 iopf_free_group(group
);
330 done
+= response_size
;
332 mutex_unlock(&fault
->mutex
);
334 return done
== 0 ? rc
: done
;
337 static __poll_t
iommufd_fault_fops_poll(struct file
*filep
,
338 struct poll_table_struct
*wait
)
340 struct iommufd_fault
*fault
= filep
->private_data
;
341 __poll_t pollflags
= EPOLLOUT
;
343 poll_wait(filep
, &fault
->wait_queue
, wait
);
344 mutex_lock(&fault
->mutex
);
345 if (!list_empty(&fault
->deliver
))
346 pollflags
|= EPOLLIN
| EPOLLRDNORM
;
347 mutex_unlock(&fault
->mutex
);
352 static int iommufd_fault_fops_release(struct inode
*inode
, struct file
*filep
)
354 struct iommufd_fault
*fault
= filep
->private_data
;
356 refcount_dec(&fault
->obj
.users
);
357 iommufd_ctx_put(fault
->ictx
);
361 static const struct file_operations iommufd_fault_fops
= {
362 .owner
= THIS_MODULE
,
363 .open
= nonseekable_open
,
364 .read
= iommufd_fault_fops_read
,
365 .write
= iommufd_fault_fops_write
,
366 .poll
= iommufd_fault_fops_poll
,
367 .release
= iommufd_fault_fops_release
,
370 int iommufd_fault_alloc(struct iommufd_ucmd
*ucmd
)
372 struct iommu_fault_alloc
*cmd
= ucmd
->cmd
;
373 struct iommufd_fault
*fault
;
381 fault
= iommufd_object_alloc(ucmd
->ictx
, fault
, IOMMUFD_OBJ_FAULT
);
383 return PTR_ERR(fault
);
385 fault
->ictx
= ucmd
->ictx
;
386 INIT_LIST_HEAD(&fault
->deliver
);
387 xa_init_flags(&fault
->response
, XA_FLAGS_ALLOC1
);
388 mutex_init(&fault
->mutex
);
389 init_waitqueue_head(&fault
->wait_queue
);
391 filep
= anon_inode_getfile("[iommufd-pgfault]", &iommufd_fault_fops
,
398 refcount_inc(&fault
->obj
.users
);
399 iommufd_ctx_get(fault
->ictx
);
400 fault
->filep
= filep
;
402 fdno
= get_unused_fd_flags(O_CLOEXEC
);
408 cmd
->out_fault_id
= fault
->obj
.id
;
409 cmd
->out_fault_fd
= fdno
;
411 rc
= iommufd_ucmd_respond(ucmd
, sizeof(*cmd
));
414 iommufd_object_finalize(ucmd
->ictx
, &fault
->obj
);
416 fd_install(fdno
, fault
->filep
);
423 refcount_dec(&fault
->obj
.users
);
424 iommufd_ctx_put(fault
->ictx
);
426 iommufd_object_abort_and_destroy(ucmd
->ictx
, &fault
->obj
);
431 int iommufd_fault_iopf_handler(struct iopf_group
*group
)
433 struct iommufd_hw_pagetable
*hwpt
;
434 struct iommufd_fault
*fault
;
436 hwpt
= group
->attach_handle
->domain
->fault_data
;
439 mutex_lock(&fault
->mutex
);
440 list_add_tail(&group
->node
, &fault
->deliver
);
441 mutex_unlock(&fault
->mutex
);
443 wake_up_interruptible(&fault
->wait_queue
);