1 // SPDX-License-Identifier: GPL-2.0-only
2 /* Copyright (c) 2021-2022, NVIDIA CORPORATION & AFFILIATES
3 */
4 #include <linux/iommufd.h>
5 #include <linux/slab.h>
6 #include <linux/iommu.h>
7 #include <uapi/linux/iommufd.h>
8 #include "../iommu-priv.h"
9
10 #include "io_pagetable.h"
11 #include "iommufd_private.h"
12
13 static bool allow_unsafe_interrupts;
14 module_param(allow_unsafe_interrupts, bool, S_IRUGO | S_IWUSR);
15 MODULE_PARM_DESC(
16 allow_unsafe_interrupts,
17 "Allow IOMMUFD to bind to devices even if the platform cannot isolate "
18 "the MSI interrupt window. Enabling this is a security weakness.");
19
iommufd_group_release(struct kref * kref)20 static void iommufd_group_release(struct kref *kref)
21 {
22 struct iommufd_group *igroup =
23 container_of(kref, struct iommufd_group, ref);
24
25 WARN_ON(igroup->hwpt || !list_empty(&igroup->device_list));
26
27 xa_cmpxchg(&igroup->ictx->groups, iommu_group_id(igroup->group), igroup,
28 NULL, GFP_KERNEL);
29 iommu_group_put(igroup->group);
30 mutex_destroy(&igroup->lock);
31 kfree(igroup);
32 }
33
iommufd_put_group(struct iommufd_group * group)34 static void iommufd_put_group(struct iommufd_group *group)
35 {
36 kref_put(&group->ref, iommufd_group_release);
37 }
38
iommufd_group_try_get(struct iommufd_group * igroup,struct iommu_group * group)39 static bool iommufd_group_try_get(struct iommufd_group *igroup,
40 struct iommu_group *group)
41 {
42 if (!igroup)
43 return false;
44 /*
45 * group ID's cannot be re-used until the group is put back which does
46 * not happen if we could get an igroup pointer under the xa_lock.
47 */
48 if (WARN_ON(igroup->group != group))
49 return false;
50 return kref_get_unless_zero(&igroup->ref);
51 }
52
53 /*
54 * iommufd needs to store some more data for each iommu_group, we keep a
55 * parallel xarray indexed by iommu_group id to hold this instead of putting it
56 * in the core structure. To keep things simple the iommufd_group memory is
57 * unique within the iommufd_ctx. This makes it easy to check there are no
58 * memory leaks.
59 */
iommufd_get_group(struct iommufd_ctx * ictx,struct device * dev)60 static struct iommufd_group *iommufd_get_group(struct iommufd_ctx *ictx,
61 struct device *dev)
62 {
63 struct iommufd_group *new_igroup;
64 struct iommufd_group *cur_igroup;
65 struct iommufd_group *igroup;
66 struct iommu_group *group;
67 unsigned int id;
68
69 group = iommu_group_get(dev);
70 if (!group)
71 return ERR_PTR(-ENODEV);
72
73 id = iommu_group_id(group);
74
75 xa_lock(&ictx->groups);
76 igroup = xa_load(&ictx->groups, id);
77 if (iommufd_group_try_get(igroup, group)) {
78 xa_unlock(&ictx->groups);
79 iommu_group_put(group);
80 return igroup;
81 }
82 xa_unlock(&ictx->groups);
83
84 new_igroup = kzalloc(sizeof(*new_igroup), GFP_KERNEL);
85 if (!new_igroup) {
86 iommu_group_put(group);
87 return ERR_PTR(-ENOMEM);
88 }
89
90 kref_init(&new_igroup->ref);
91 mutex_init(&new_igroup->lock);
92 INIT_LIST_HEAD(&new_igroup->device_list);
93 new_igroup->sw_msi_start = PHYS_ADDR_MAX;
94 /* group reference moves into new_igroup */
95 new_igroup->group = group;
96
97 /*
98 * The ictx is not additionally refcounted here becase all objects using
99 * an igroup must put it before their destroy completes.
100 */
101 new_igroup->ictx = ictx;
102
103 /*
104 * We dropped the lock so igroup is invalid. NULL is a safe and likely
105 * value to assume for the xa_cmpxchg algorithm.
106 */
107 cur_igroup = NULL;
108 xa_lock(&ictx->groups);
109 while (true) {
110 igroup = __xa_cmpxchg(&ictx->groups, id, cur_igroup, new_igroup,
111 GFP_KERNEL);
112 if (xa_is_err(igroup)) {
113 xa_unlock(&ictx->groups);
114 iommufd_put_group(new_igroup);
115 return ERR_PTR(xa_err(igroup));
116 }
117
118 /* new_group was successfully installed */
119 if (cur_igroup == igroup) {
120 xa_unlock(&ictx->groups);
121 return new_igroup;
122 }
123
124 /* Check again if the current group is any good */
125 if (iommufd_group_try_get(igroup, group)) {
126 xa_unlock(&ictx->groups);
127 iommufd_put_group(new_igroup);
128 return igroup;
129 }
130 cur_igroup = igroup;
131 }
132 }
133
iommufd_device_destroy(struct iommufd_object * obj)134 void iommufd_device_destroy(struct iommufd_object *obj)
135 {
136 struct iommufd_device *idev =
137 container_of(obj, struct iommufd_device, obj);
138
139 iommu_device_release_dma_owner(idev->dev);
140 iommufd_put_group(idev->igroup);
141 if (!iommufd_selftest_is_mock_dev(idev->dev))
142 iommufd_ctx_put(idev->ictx);
143 }
144
145 /**
146 * iommufd_device_bind - Bind a physical device to an iommu fd
147 * @ictx: iommufd file descriptor
148 * @dev: Pointer to a physical device struct
149 * @id: Output ID number to return to userspace for this device
150 *
151 * A successful bind establishes an ownership over the device and returns
152 * struct iommufd_device pointer, otherwise returns error pointer.
153 *
154 * A driver using this API must set driver_managed_dma and must not touch
155 * the device until this routine succeeds and establishes ownership.
156 *
157 * Binding a PCI device places the entire RID under iommufd control.
158 *
159 * The caller must undo this with iommufd_device_unbind()
160 */
iommufd_device_bind(struct iommufd_ctx * ictx,struct device * dev,u32 * id)161 struct iommufd_device *iommufd_device_bind(struct iommufd_ctx *ictx,
162 struct device *dev, u32 *id)
163 {
164 struct iommufd_device *idev;
165 struct iommufd_group *igroup;
166 int rc;
167
168 /*
169 * iommufd always sets IOMMU_CACHE because we offer no way for userspace
170 * to restore cache coherency.
171 */
172 if (!device_iommu_capable(dev, IOMMU_CAP_CACHE_COHERENCY))
173 return ERR_PTR(-EINVAL);
174
175 igroup = iommufd_get_group(ictx, dev);
176 if (IS_ERR(igroup))
177 return ERR_CAST(igroup);
178
179 /*
180 * For historical compat with VFIO the insecure interrupt path is
181 * allowed if the module parameter is set. Secure/Isolated means that a
182 * MemWr operation from the device (eg a simple DMA) cannot trigger an
183 * interrupt outside this iommufd context.
184 */
185 if (!iommufd_selftest_is_mock_dev(dev) &&
186 !iommu_group_has_isolated_msi(igroup->group)) {
187 if (!allow_unsafe_interrupts) {
188 rc = -EPERM;
189 goto out_group_put;
190 }
191
192 dev_warn(
193 dev,
194 "MSI interrupts are not secure, they cannot be isolated by the platform. "
195 "Check that platform features like interrupt remapping are enabled. "
196 "Use the \"allow_unsafe_interrupts\" module parameter to override\n");
197 }
198
199 rc = iommu_device_claim_dma_owner(dev, ictx);
200 if (rc)
201 goto out_group_put;
202
203 idev = iommufd_object_alloc(ictx, idev, IOMMUFD_OBJ_DEVICE);
204 if (IS_ERR(idev)) {
205 rc = PTR_ERR(idev);
206 goto out_release_owner;
207 }
208 idev->ictx = ictx;
209 if (!iommufd_selftest_is_mock_dev(dev))
210 iommufd_ctx_get(ictx);
211 idev->dev = dev;
212 idev->enforce_cache_coherency =
213 device_iommu_capable(dev, IOMMU_CAP_ENFORCE_CACHE_COHERENCY);
214 /* The calling driver is a user until iommufd_device_unbind() */
215 refcount_inc(&idev->obj.users);
216 /* igroup refcount moves into iommufd_device */
217 idev->igroup = igroup;
218
219 /*
220 * If the caller fails after this success it must call
221 * iommufd_unbind_device() which is safe since we hold this refcount.
222 * This also means the device is a leaf in the graph and no other object
223 * can take a reference on it.
224 */
225 iommufd_object_finalize(ictx, &idev->obj);
226 *id = idev->obj.id;
227 return idev;
228
229 out_release_owner:
230 iommu_device_release_dma_owner(dev);
231 out_group_put:
232 iommufd_put_group(igroup);
233 return ERR_PTR(rc);
234 }
235 EXPORT_SYMBOL_NS_GPL(iommufd_device_bind, IOMMUFD);
236
237 /**
238 * iommufd_ctx_has_group - True if any device within the group is bound
239 * to the ictx
240 * @ictx: iommufd file descriptor
241 * @group: Pointer to a physical iommu_group struct
242 *
243 * True if any device within the group has been bound to this ictx, ex. via
244 * iommufd_device_bind(), therefore implying ictx ownership of the group.
245 */
iommufd_ctx_has_group(struct iommufd_ctx * ictx,struct iommu_group * group)246 bool iommufd_ctx_has_group(struct iommufd_ctx *ictx, struct iommu_group *group)
247 {
248 struct iommufd_object *obj;
249 unsigned long index;
250
251 if (!ictx || !group)
252 return false;
253
254 xa_lock(&ictx->objects);
255 xa_for_each(&ictx->objects, index, obj) {
256 if (obj->type == IOMMUFD_OBJ_DEVICE &&
257 container_of(obj, struct iommufd_device, obj)
258 ->igroup->group == group) {
259 xa_unlock(&ictx->objects);
260 return true;
261 }
262 }
263 xa_unlock(&ictx->objects);
264 return false;
265 }
266 EXPORT_SYMBOL_NS_GPL(iommufd_ctx_has_group, IOMMUFD);
267
268 /**
269 * iommufd_device_unbind - Undo iommufd_device_bind()
270 * @idev: Device returned by iommufd_device_bind()
271 *
272 * Release the device from iommufd control. The DMA ownership will return back
273 * to unowned with DMA controlled by the DMA API. This invalidates the
274 * iommufd_device pointer, other APIs that consume it must not be called
275 * concurrently.
276 */
iommufd_device_unbind(struct iommufd_device * idev)277 void iommufd_device_unbind(struct iommufd_device *idev)
278 {
279 iommufd_object_destroy_user(idev->ictx, &idev->obj);
280 }
281 EXPORT_SYMBOL_NS_GPL(iommufd_device_unbind, IOMMUFD);
282
iommufd_device_to_ictx(struct iommufd_device * idev)283 struct iommufd_ctx *iommufd_device_to_ictx(struct iommufd_device *idev)
284 {
285 return idev->ictx;
286 }
287 EXPORT_SYMBOL_NS_GPL(iommufd_device_to_ictx, IOMMUFD);
288
iommufd_device_to_id(struct iommufd_device * idev)289 u32 iommufd_device_to_id(struct iommufd_device *idev)
290 {
291 return idev->obj.id;
292 }
293 EXPORT_SYMBOL_NS_GPL(iommufd_device_to_id, IOMMUFD);
294
iommufd_group_setup_msi(struct iommufd_group * igroup,struct iommufd_hw_pagetable * hwpt)295 static int iommufd_group_setup_msi(struct iommufd_group *igroup,
296 struct iommufd_hw_pagetable *hwpt)
297 {
298 phys_addr_t sw_msi_start = igroup->sw_msi_start;
299 int rc;
300
301 /*
302 * If the IOMMU driver gives a IOMMU_RESV_SW_MSI then it is asking us to
303 * call iommu_get_msi_cookie() on its behalf. This is necessary to setup
304 * the MSI window so iommu_dma_prepare_msi() can install pages into our
305 * domain after request_irq(). If it is not done interrupts will not
306 * work on this domain.
307 *
308 * FIXME: This is conceptually broken for iommufd since we want to allow
309 * userspace to change the domains, eg switch from an identity IOAS to a
310 * DMA IOAS. There is currently no way to create a MSI window that
311 * matches what the IRQ layer actually expects in a newly created
312 * domain.
313 */
314 if (sw_msi_start != PHYS_ADDR_MAX && !hwpt->msi_cookie) {
315 rc = iommu_get_msi_cookie(hwpt->domain, sw_msi_start);
316 if (rc)
317 return rc;
318
319 /*
320 * iommu_get_msi_cookie() can only be called once per domain,
321 * it returns -EBUSY on later calls.
322 */
323 hwpt->msi_cookie = true;
324 }
325 return 0;
326 }
327
iommufd_hw_pagetable_attach(struct iommufd_hw_pagetable * hwpt,struct iommufd_device * idev)328 int iommufd_hw_pagetable_attach(struct iommufd_hw_pagetable *hwpt,
329 struct iommufd_device *idev)
330 {
331 int rc;
332
333 mutex_lock(&idev->igroup->lock);
334
335 if (idev->igroup->hwpt != NULL && idev->igroup->hwpt != hwpt) {
336 rc = -EINVAL;
337 goto err_unlock;
338 }
339
340 /* Try to upgrade the domain we have */
341 if (idev->enforce_cache_coherency) {
342 rc = iommufd_hw_pagetable_enforce_cc(hwpt);
343 if (rc)
344 goto err_unlock;
345 }
346
347 rc = iopt_table_enforce_dev_resv_regions(&hwpt->ioas->iopt, idev->dev,
348 &idev->igroup->sw_msi_start);
349 if (rc)
350 goto err_unlock;
351
352 /*
353 * Only attach to the group once for the first device that is in the
354 * group. All the other devices will follow this attachment. The user
355 * should attach every device individually to the hwpt as the per-device
356 * reserved regions are only updated during individual device
357 * attachment.
358 */
359 if (list_empty(&idev->igroup->device_list)) {
360 rc = iommufd_group_setup_msi(idev->igroup, hwpt);
361 if (rc)
362 goto err_unresv;
363
364 rc = iommu_attach_group(hwpt->domain, idev->igroup->group);
365 if (rc)
366 goto err_unresv;
367 idev->igroup->hwpt = hwpt;
368 }
369 refcount_inc(&hwpt->obj.users);
370 list_add_tail(&idev->group_item, &idev->igroup->device_list);
371 mutex_unlock(&idev->igroup->lock);
372 return 0;
373 err_unresv:
374 iopt_remove_reserved_iova(&hwpt->ioas->iopt, idev->dev);
375 err_unlock:
376 mutex_unlock(&idev->igroup->lock);
377 return rc;
378 }
379
380 struct iommufd_hw_pagetable *
iommufd_hw_pagetable_detach(struct iommufd_device * idev)381 iommufd_hw_pagetable_detach(struct iommufd_device *idev)
382 {
383 struct iommufd_hw_pagetable *hwpt = idev->igroup->hwpt;
384
385 mutex_lock(&idev->igroup->lock);
386 list_del(&idev->group_item);
387 if (list_empty(&idev->igroup->device_list)) {
388 iommu_detach_group(hwpt->domain, idev->igroup->group);
389 idev->igroup->hwpt = NULL;
390 }
391 iopt_remove_reserved_iova(&hwpt->ioas->iopt, idev->dev);
392 mutex_unlock(&idev->igroup->lock);
393
394 /* Caller must destroy hwpt */
395 return hwpt;
396 }
397
398 static struct iommufd_hw_pagetable *
iommufd_device_do_attach(struct iommufd_device * idev,struct iommufd_hw_pagetable * hwpt)399 iommufd_device_do_attach(struct iommufd_device *idev,
400 struct iommufd_hw_pagetable *hwpt)
401 {
402 int rc;
403
404 rc = iommufd_hw_pagetable_attach(hwpt, idev);
405 if (rc)
406 return ERR_PTR(rc);
407 return NULL;
408 }
409
410 /* Check if idev is attached to igroup->hwpt */
iommufd_device_is_attached(struct iommufd_device * idev)411 static bool iommufd_device_is_attached(struct iommufd_device *idev)
412 {
413 struct iommufd_device *cur;
414
415 list_for_each_entry(cur, &idev->igroup->device_list, group_item)
416 if (cur == idev)
417 return true;
418 return false;
419 }
420
421 static struct iommufd_hw_pagetable *
iommufd_device_do_replace(struct iommufd_device * idev,struct iommufd_hw_pagetable * hwpt)422 iommufd_device_do_replace(struct iommufd_device *idev,
423 struct iommufd_hw_pagetable *hwpt)
424 {
425 struct iommufd_group *igroup = idev->igroup;
426 struct iommufd_hw_pagetable *old_hwpt;
427 unsigned int num_devices = 0;
428 struct iommufd_device *cur;
429 int rc;
430
431 mutex_lock(&idev->igroup->lock);
432
433 if (igroup->hwpt == NULL) {
434 rc = -EINVAL;
435 goto err_unlock;
436 }
437
438 if (!iommufd_device_is_attached(idev)) {
439 rc = -EINVAL;
440 goto err_unlock;
441 }
442
443 if (hwpt == igroup->hwpt) {
444 mutex_unlock(&idev->igroup->lock);
445 return NULL;
446 }
447
448 /* Try to upgrade the domain we have */
449 list_for_each_entry(cur, &igroup->device_list, group_item) {
450 num_devices++;
451 if (cur->enforce_cache_coherency) {
452 rc = iommufd_hw_pagetable_enforce_cc(hwpt);
453 if (rc)
454 goto err_unlock;
455 }
456 }
457
458 old_hwpt = igroup->hwpt;
459 if (hwpt->ioas != old_hwpt->ioas) {
460 list_for_each_entry(cur, &igroup->device_list, group_item) {
461 rc = iopt_table_enforce_dev_resv_regions(
462 &hwpt->ioas->iopt, cur->dev, NULL);
463 if (rc)
464 goto err_unresv;
465 }
466 }
467
468 rc = iommufd_group_setup_msi(idev->igroup, hwpt);
469 if (rc)
470 goto err_unresv;
471
472 rc = iommu_group_replace_domain(igroup->group, hwpt->domain);
473 if (rc)
474 goto err_unresv;
475
476 if (hwpt->ioas != old_hwpt->ioas) {
477 list_for_each_entry(cur, &igroup->device_list, group_item)
478 iopt_remove_reserved_iova(&old_hwpt->ioas->iopt,
479 cur->dev);
480 }
481
482 igroup->hwpt = hwpt;
483
484 /*
485 * Move the refcounts held by the device_list to the new hwpt. Retain a
486 * refcount for this thread as the caller will free it.
487 */
488 refcount_add(num_devices, &hwpt->obj.users);
489 if (num_devices > 1)
490 WARN_ON(refcount_sub_and_test(num_devices - 1,
491 &old_hwpt->obj.users));
492 mutex_unlock(&idev->igroup->lock);
493
494 /* Caller must destroy old_hwpt */
495 return old_hwpt;
496 err_unresv:
497 list_for_each_entry(cur, &igroup->device_list, group_item)
498 iopt_remove_reserved_iova(&hwpt->ioas->iopt, cur->dev);
499 err_unlock:
500 mutex_unlock(&idev->igroup->lock);
501 return ERR_PTR(rc);
502 }
503
504 typedef struct iommufd_hw_pagetable *(*attach_fn)(
505 struct iommufd_device *idev, struct iommufd_hw_pagetable *hwpt);
506
507 /*
508 * When automatically managing the domains we search for a compatible domain in
509 * the iopt and if one is found use it, otherwise create a new domain.
510 * Automatic domain selection will never pick a manually created domain.
511 */
512 static struct iommufd_hw_pagetable *
513 iommufd_device_auto_get_domain(struct iommufd_device *idev,
514 struct iommufd_ioas *ioas, u32 *pt_id,
515 attach_fn do_attach)
516 {
517 /*
518 * iommufd_hw_pagetable_attach() is called by
519 * iommufd_hw_pagetable_alloc() in immediate attachment mode, same as
520 * iommufd_device_do_attach(). So if we are in this mode then we prefer
521 * to use the immediate_attach path as it supports drivers that can't
522 * directly allocate a domain.
523 */
524 bool immediate_attach = do_attach == iommufd_device_do_attach;
525 struct iommufd_hw_pagetable *destroy_hwpt;
526 struct iommufd_hw_pagetable *hwpt;
527
528 /*
529 * There is no differentiation when domains are allocated, so any domain
530 * that is willing to attach to the device is interchangeable with any
531 * other.
532 */
533 mutex_lock(&ioas->mutex);
534 list_for_each_entry(hwpt, &ioas->hwpt_list, hwpt_item) {
535 if (!hwpt->auto_domain)
536 continue;
537
538 if (!iommufd_lock_obj(&hwpt->obj))
539 continue;
540 destroy_hwpt = (*do_attach)(idev, hwpt);
541 if (IS_ERR(destroy_hwpt)) {
542 iommufd_put_object(&hwpt->obj);
543 /*
544 * -EINVAL means the domain is incompatible with the
545 * device. Other error codes should propagate to
546 * userspace as failure. Success means the domain is
547 * attached.
548 */
549 if (PTR_ERR(destroy_hwpt) == -EINVAL)
550 continue;
551 goto out_unlock;
552 }
553 *pt_id = hwpt->obj.id;
554 iommufd_put_object(&hwpt->obj);
555 goto out_unlock;
556 }
557
558 hwpt = iommufd_hw_pagetable_alloc(idev->ictx, ioas, idev,
559 immediate_attach);
560 if (IS_ERR(hwpt)) {
561 destroy_hwpt = ERR_CAST(hwpt);
562 goto out_unlock;
563 }
564
565 if (!immediate_attach) {
566 destroy_hwpt = (*do_attach)(idev, hwpt);
567 if (IS_ERR(destroy_hwpt))
568 goto out_abort;
569 } else {
570 destroy_hwpt = NULL;
571 }
572
573 hwpt->auto_domain = true;
574 *pt_id = hwpt->obj.id;
575
576 iommufd_object_finalize(idev->ictx, &hwpt->obj);
577 mutex_unlock(&ioas->mutex);
578 return destroy_hwpt;
579
580 out_abort:
581 iommufd_object_abort_and_destroy(idev->ictx, &hwpt->obj);
582 out_unlock:
583 mutex_unlock(&ioas->mutex);
584 return destroy_hwpt;
585 }
586
587 static int iommufd_device_change_pt(struct iommufd_device *idev, u32 *pt_id,
588 attach_fn do_attach)
589 {
590 struct iommufd_hw_pagetable *destroy_hwpt;
591 struct iommufd_object *pt_obj;
592
593 pt_obj = iommufd_get_object(idev->ictx, *pt_id, IOMMUFD_OBJ_ANY);
594 if (IS_ERR(pt_obj))
595 return PTR_ERR(pt_obj);
596
597 switch (pt_obj->type) {
598 case IOMMUFD_OBJ_HW_PAGETABLE: {
599 struct iommufd_hw_pagetable *hwpt =
600 container_of(pt_obj, struct iommufd_hw_pagetable, obj);
601
602 destroy_hwpt = (*do_attach)(idev, hwpt);
603 if (IS_ERR(destroy_hwpt))
604 goto out_put_pt_obj;
605 break;
606 }
607 case IOMMUFD_OBJ_IOAS: {
608 struct iommufd_ioas *ioas =
609 container_of(pt_obj, struct iommufd_ioas, obj);
610
611 destroy_hwpt = iommufd_device_auto_get_domain(idev, ioas, pt_id,
612 do_attach);
613 if (IS_ERR(destroy_hwpt))
614 goto out_put_pt_obj;
615 break;
616 }
617 default:
618 destroy_hwpt = ERR_PTR(-EINVAL);
619 goto out_put_pt_obj;
620 }
621 iommufd_put_object(pt_obj);
622
623 /* This destruction has to be after we unlock everything */
624 if (destroy_hwpt)
625 iommufd_hw_pagetable_put(idev->ictx, destroy_hwpt);
626 return 0;
627
628 out_put_pt_obj:
629 iommufd_put_object(pt_obj);
630 return PTR_ERR(destroy_hwpt);
631 }
632
633 /**
634 * iommufd_device_attach - Connect a device to an iommu_domain
635 * @idev: device to attach
636 * @pt_id: Input a IOMMUFD_OBJ_IOAS, or IOMMUFD_OBJ_HW_PAGETABLE
637 * Output the IOMMUFD_OBJ_HW_PAGETABLE ID
638 *
639 * This connects the device to an iommu_domain, either automatically or manually
640 * selected. Once this completes the device could do DMA.
641 *
642 * The caller should return the resulting pt_id back to userspace.
643 * This function is undone by calling iommufd_device_detach().
644 */
645 int iommufd_device_attach(struct iommufd_device *idev, u32 *pt_id)
646 {
647 int rc;
648
649 rc = iommufd_device_change_pt(idev, pt_id, &iommufd_device_do_attach);
650 if (rc)
651 return rc;
652
653 /*
654 * Pairs with iommufd_device_detach() - catches caller bugs attempting
655 * to destroy a device with an attachment.
656 */
657 refcount_inc(&idev->obj.users);
658 return 0;
659 }
660 EXPORT_SYMBOL_NS_GPL(iommufd_device_attach, IOMMUFD);
661
662 /**
663 * iommufd_device_replace - Change the device's iommu_domain
664 * @idev: device to change
665 * @pt_id: Input a IOMMUFD_OBJ_IOAS, or IOMMUFD_OBJ_HW_PAGETABLE
666 * Output the IOMMUFD_OBJ_HW_PAGETABLE ID
667 *
668 * This is the same as::
669 *
670 * iommufd_device_detach();
671 * iommufd_device_attach();
672 *
673 * If it fails then no change is made to the attachment. The iommu driver may
674 * implement this so there is no disruption in translation. This can only be
675 * called if iommufd_device_attach() has already succeeded.
676 */
iommufd_device_replace(struct iommufd_device * idev,u32 * pt_id)677 int iommufd_device_replace(struct iommufd_device *idev, u32 *pt_id)
678 {
679 return iommufd_device_change_pt(idev, pt_id,
680 &iommufd_device_do_replace);
681 }
682 EXPORT_SYMBOL_NS_GPL(iommufd_device_replace, IOMMUFD);
683
684 /**
685 * iommufd_device_detach - Disconnect a device to an iommu_domain
686 * @idev: device to detach
687 *
688 * Undo iommufd_device_attach(). This disconnects the idev from the previously
689 * attached pt_id. The device returns back to a blocked DMA translation.
690 */
iommufd_device_detach(struct iommufd_device * idev)691 void iommufd_device_detach(struct iommufd_device *idev)
692 {
693 struct iommufd_hw_pagetable *hwpt;
694
695 hwpt = iommufd_hw_pagetable_detach(idev);
696 iommufd_hw_pagetable_put(idev->ictx, hwpt);
697 refcount_dec(&idev->obj.users);
698 }
699 EXPORT_SYMBOL_NS_GPL(iommufd_device_detach, IOMMUFD);
700
701 /*
702 * On success, it will refcount_inc() at a valid new_ioas and refcount_dec() at
703 * a valid cur_ioas (access->ioas). A caller passing in a valid new_ioas should
704 * call iommufd_put_object() if it does an iommufd_get_object() for a new_ioas.
705 */
iommufd_access_change_ioas(struct iommufd_access * access,struct iommufd_ioas * new_ioas)706 static int iommufd_access_change_ioas(struct iommufd_access *access,
707 struct iommufd_ioas *new_ioas)
708 {
709 u32 iopt_access_list_id = access->iopt_access_list_id;
710 struct iommufd_ioas *cur_ioas = access->ioas;
711 int rc;
712
713 lockdep_assert_held(&access->ioas_lock);
714
715 /* We are racing with a concurrent detach, bail */
716 if (cur_ioas != access->ioas_unpin)
717 return -EBUSY;
718
719 if (cur_ioas == new_ioas)
720 return 0;
721
722 /*
723 * Set ioas to NULL to block any further iommufd_access_pin_pages().
724 * iommufd_access_unpin_pages() can continue using access->ioas_unpin.
725 */
726 access->ioas = NULL;
727
728 if (new_ioas) {
729 rc = iopt_add_access(&new_ioas->iopt, access);
730 if (rc) {
731 access->ioas = cur_ioas;
732 return rc;
733 }
734 refcount_inc(&new_ioas->obj.users);
735 }
736
737 if (cur_ioas) {
738 if (access->ops->unmap) {
739 mutex_unlock(&access->ioas_lock);
740 access->ops->unmap(access->data, 0, ULONG_MAX);
741 mutex_lock(&access->ioas_lock);
742 }
743 iopt_remove_access(&cur_ioas->iopt, access, iopt_access_list_id);
744 refcount_dec(&cur_ioas->obj.users);
745 }
746
747 access->ioas = new_ioas;
748 access->ioas_unpin = new_ioas;
749
750 return 0;
751 }
752
iommufd_access_change_ioas_id(struct iommufd_access * access,u32 id)753 static int iommufd_access_change_ioas_id(struct iommufd_access *access, u32 id)
754 {
755 struct iommufd_ioas *ioas = iommufd_get_ioas(access->ictx, id);
756 int rc;
757
758 if (IS_ERR(ioas))
759 return PTR_ERR(ioas);
760 rc = iommufd_access_change_ioas(access, ioas);
761 iommufd_put_object(&ioas->obj);
762 return rc;
763 }
764
iommufd_access_destroy_object(struct iommufd_object * obj)765 void iommufd_access_destroy_object(struct iommufd_object *obj)
766 {
767 struct iommufd_access *access =
768 container_of(obj, struct iommufd_access, obj);
769
770 mutex_lock(&access->ioas_lock);
771 if (access->ioas)
772 WARN_ON(iommufd_access_change_ioas(access, NULL));
773 mutex_unlock(&access->ioas_lock);
774 iommufd_ctx_put(access->ictx);
775 }
776
777 /**
778 * iommufd_access_create - Create an iommufd_access
779 * @ictx: iommufd file descriptor
780 * @ops: Driver's ops to associate with the access
781 * @data: Opaque data to pass into ops functions
782 * @id: Output ID number to return to userspace for this access
783 *
784 * An iommufd_access allows a driver to read/write to the IOAS without using
785 * DMA. The underlying CPU memory can be accessed using the
786 * iommufd_access_pin_pages() or iommufd_access_rw() functions.
787 *
788 * The provided ops are required to use iommufd_access_pin_pages().
789 */
790 struct iommufd_access *
iommufd_access_create(struct iommufd_ctx * ictx,const struct iommufd_access_ops * ops,void * data,u32 * id)791 iommufd_access_create(struct iommufd_ctx *ictx,
792 const struct iommufd_access_ops *ops, void *data, u32 *id)
793 {
794 struct iommufd_access *access;
795
796 /*
797 * There is no uAPI for the access object, but to keep things symmetric
798 * use the object infrastructure anyhow.
799 */
800 access = iommufd_object_alloc(ictx, access, IOMMUFD_OBJ_ACCESS);
801 if (IS_ERR(access))
802 return access;
803
804 access->data = data;
805 access->ops = ops;
806
807 if (ops->needs_pin_pages)
808 access->iova_alignment = PAGE_SIZE;
809 else
810 access->iova_alignment = 1;
811
812 /* The calling driver is a user until iommufd_access_destroy() */
813 refcount_inc(&access->obj.users);
814 access->ictx = ictx;
815 iommufd_ctx_get(ictx);
816 iommufd_object_finalize(ictx, &access->obj);
817 *id = access->obj.id;
818 mutex_init(&access->ioas_lock);
819 return access;
820 }
821 EXPORT_SYMBOL_NS_GPL(iommufd_access_create, IOMMUFD);
822
823 /**
824 * iommufd_access_destroy - Destroy an iommufd_access
825 * @access: The access to destroy
826 *
827 * The caller must stop using the access before destroying it.
828 */
iommufd_access_destroy(struct iommufd_access * access)829 void iommufd_access_destroy(struct iommufd_access *access)
830 {
831 iommufd_object_destroy_user(access->ictx, &access->obj);
832 }
833 EXPORT_SYMBOL_NS_GPL(iommufd_access_destroy, IOMMUFD);
834
iommufd_access_detach(struct iommufd_access * access)835 void iommufd_access_detach(struct iommufd_access *access)
836 {
837 mutex_lock(&access->ioas_lock);
838 if (WARN_ON(!access->ioas)) {
839 mutex_unlock(&access->ioas_lock);
840 return;
841 }
842 WARN_ON(iommufd_access_change_ioas(access, NULL));
843 mutex_unlock(&access->ioas_lock);
844 }
845 EXPORT_SYMBOL_NS_GPL(iommufd_access_detach, IOMMUFD);
846
iommufd_access_attach(struct iommufd_access * access,u32 ioas_id)847 int iommufd_access_attach(struct iommufd_access *access, u32 ioas_id)
848 {
849 int rc;
850
851 mutex_lock(&access->ioas_lock);
852 if (WARN_ON(access->ioas)) {
853 mutex_unlock(&access->ioas_lock);
854 return -EINVAL;
855 }
856
857 rc = iommufd_access_change_ioas_id(access, ioas_id);
858 mutex_unlock(&access->ioas_lock);
859 return rc;
860 }
861 EXPORT_SYMBOL_NS_GPL(iommufd_access_attach, IOMMUFD);
862
iommufd_access_replace(struct iommufd_access * access,u32 ioas_id)863 int iommufd_access_replace(struct iommufd_access *access, u32 ioas_id)
864 {
865 int rc;
866
867 mutex_lock(&access->ioas_lock);
868 if (!access->ioas) {
869 mutex_unlock(&access->ioas_lock);
870 return -ENOENT;
871 }
872 rc = iommufd_access_change_ioas_id(access, ioas_id);
873 mutex_unlock(&access->ioas_lock);
874 return rc;
875 }
876 EXPORT_SYMBOL_NS_GPL(iommufd_access_replace, IOMMUFD);
877
878 /**
879 * iommufd_access_notify_unmap - Notify users of an iopt to stop using it
880 * @iopt: iopt to work on
881 * @iova: Starting iova in the iopt
882 * @length: Number of bytes
883 *
884 * After this function returns there should be no users attached to the pages
885 * linked to this iopt that intersect with iova,length. Anyone that has attached
886 * a user through iopt_access_pages() needs to detach it through
887 * iommufd_access_unpin_pages() before this function returns.
888 *
889 * iommufd_access_destroy() will wait for any outstanding unmap callback to
890 * complete. Once iommufd_access_destroy() no unmap ops are running or will
891 * run in the future. Due to this a driver must not create locking that prevents
892 * unmap to complete while iommufd_access_destroy() is running.
893 */
iommufd_access_notify_unmap(struct io_pagetable * iopt,unsigned long iova,unsigned long length)894 void iommufd_access_notify_unmap(struct io_pagetable *iopt, unsigned long iova,
895 unsigned long length)
896 {
897 struct iommufd_ioas *ioas =
898 container_of(iopt, struct iommufd_ioas, iopt);
899 struct iommufd_access *access;
900 unsigned long index;
901
902 xa_lock(&ioas->iopt.access_list);
903 xa_for_each(&ioas->iopt.access_list, index, access) {
904 if (!iommufd_lock_obj(&access->obj))
905 continue;
906 xa_unlock(&ioas->iopt.access_list);
907
908 access->ops->unmap(access->data, iova, length);
909
910 iommufd_put_object(&access->obj);
911 xa_lock(&ioas->iopt.access_list);
912 }
913 xa_unlock(&ioas->iopt.access_list);
914 }
915
916 /**
917 * iommufd_access_unpin_pages() - Undo iommufd_access_pin_pages
918 * @access: IOAS access to act on
919 * @iova: Starting IOVA
920 * @length: Number of bytes to access
921 *
922 * Return the struct page's. The caller must stop accessing them before calling
923 * this. The iova/length must exactly match the one provided to access_pages.
924 */
iommufd_access_unpin_pages(struct iommufd_access * access,unsigned long iova,unsigned long length)925 void iommufd_access_unpin_pages(struct iommufd_access *access,
926 unsigned long iova, unsigned long length)
927 {
928 struct iopt_area_contig_iter iter;
929 struct io_pagetable *iopt;
930 unsigned long last_iova;
931 struct iopt_area *area;
932
933 if (WARN_ON(!length) ||
934 WARN_ON(check_add_overflow(iova, length - 1, &last_iova)))
935 return;
936
937 mutex_lock(&access->ioas_lock);
938 /*
939 * The driver must be doing something wrong if it calls this before an
940 * iommufd_access_attach() or after an iommufd_access_detach().
941 */
942 if (WARN_ON(!access->ioas_unpin)) {
943 mutex_unlock(&access->ioas_lock);
944 return;
945 }
946 iopt = &access->ioas_unpin->iopt;
947
948 down_read(&iopt->iova_rwsem);
949 iopt_for_each_contig_area(&iter, area, iopt, iova, last_iova)
950 iopt_area_remove_access(
951 area, iopt_area_iova_to_index(area, iter.cur_iova),
952 iopt_area_iova_to_index(
953 area,
954 min(last_iova, iopt_area_last_iova(area))));
955 WARN_ON(!iopt_area_contig_done(&iter));
956 up_read(&iopt->iova_rwsem);
957 mutex_unlock(&access->ioas_lock);
958 }
959 EXPORT_SYMBOL_NS_GPL(iommufd_access_unpin_pages, IOMMUFD);
960
iopt_area_contig_is_aligned(struct iopt_area_contig_iter * iter)961 static bool iopt_area_contig_is_aligned(struct iopt_area_contig_iter *iter)
962 {
963 if (iopt_area_start_byte(iter->area, iter->cur_iova) % PAGE_SIZE)
964 return false;
965
966 if (!iopt_area_contig_done(iter) &&
967 (iopt_area_start_byte(iter->area, iopt_area_last_iova(iter->area)) %
968 PAGE_SIZE) != (PAGE_SIZE - 1))
969 return false;
970 return true;
971 }
972
check_area_prot(struct iopt_area * area,unsigned int flags)973 static bool check_area_prot(struct iopt_area *area, unsigned int flags)
974 {
975 if (flags & IOMMUFD_ACCESS_RW_WRITE)
976 return area->iommu_prot & IOMMU_WRITE;
977 return area->iommu_prot & IOMMU_READ;
978 }
979
980 /**
981 * iommufd_access_pin_pages() - Return a list of pages under the iova
982 * @access: IOAS access to act on
983 * @iova: Starting IOVA
984 * @length: Number of bytes to access
985 * @out_pages: Output page list
986 * @flags: IOPMMUFD_ACCESS_RW_* flags
987 *
988 * Reads @length bytes starting at iova and returns the struct page * pointers.
989 * These can be kmap'd by the caller for CPU access.
990 *
991 * The caller must perform iommufd_access_unpin_pages() when done to balance
992 * this.
993 *
994 * This API always requires a page aligned iova. This happens naturally if the
995 * ioas alignment is >= PAGE_SIZE and the iova is PAGE_SIZE aligned. However
996 * smaller alignments have corner cases where this API can fail on otherwise
997 * aligned iova.
998 */
iommufd_access_pin_pages(struct iommufd_access * access,unsigned long iova,unsigned long length,struct page ** out_pages,unsigned int flags)999 int iommufd_access_pin_pages(struct iommufd_access *access, unsigned long iova,
1000 unsigned long length, struct page **out_pages,
1001 unsigned int flags)
1002 {
1003 struct iopt_area_contig_iter iter;
1004 struct io_pagetable *iopt;
1005 unsigned long last_iova;
1006 struct iopt_area *area;
1007 int rc;
1008
1009 /* Driver's ops don't support pin_pages */
1010 if (IS_ENABLED(CONFIG_IOMMUFD_TEST) &&
1011 WARN_ON(access->iova_alignment != PAGE_SIZE || !access->ops->unmap))
1012 return -EINVAL;
1013
1014 if (!length)
1015 return -EINVAL;
1016 if (check_add_overflow(iova, length - 1, &last_iova))
1017 return -EOVERFLOW;
1018
1019 mutex_lock(&access->ioas_lock);
1020 if (!access->ioas) {
1021 mutex_unlock(&access->ioas_lock);
1022 return -ENOENT;
1023 }
1024 iopt = &access->ioas->iopt;
1025
1026 down_read(&iopt->iova_rwsem);
1027 iopt_for_each_contig_area(&iter, area, iopt, iova, last_iova) {
1028 unsigned long last = min(last_iova, iopt_area_last_iova(area));
1029 unsigned long last_index = iopt_area_iova_to_index(area, last);
1030 unsigned long index =
1031 iopt_area_iova_to_index(area, iter.cur_iova);
1032
1033 if (area->prevent_access ||
1034 !iopt_area_contig_is_aligned(&iter)) {
1035 rc = -EINVAL;
1036 goto err_remove;
1037 }
1038
1039 if (!check_area_prot(area, flags)) {
1040 rc = -EPERM;
1041 goto err_remove;
1042 }
1043
1044 rc = iopt_area_add_access(area, index, last_index, out_pages,
1045 flags);
1046 if (rc)
1047 goto err_remove;
1048 out_pages += last_index - index + 1;
1049 }
1050 if (!iopt_area_contig_done(&iter)) {
1051 rc = -ENOENT;
1052 goto err_remove;
1053 }
1054
1055 up_read(&iopt->iova_rwsem);
1056 mutex_unlock(&access->ioas_lock);
1057 return 0;
1058
1059 err_remove:
1060 if (iova < iter.cur_iova) {
1061 last_iova = iter.cur_iova - 1;
1062 iopt_for_each_contig_area(&iter, area, iopt, iova, last_iova)
1063 iopt_area_remove_access(
1064 area,
1065 iopt_area_iova_to_index(area, iter.cur_iova),
1066 iopt_area_iova_to_index(
1067 area, min(last_iova,
1068 iopt_area_last_iova(area))));
1069 }
1070 up_read(&iopt->iova_rwsem);
1071 mutex_unlock(&access->ioas_lock);
1072 return rc;
1073 }
1074 EXPORT_SYMBOL_NS_GPL(iommufd_access_pin_pages, IOMMUFD);
1075
1076 /**
1077 * iommufd_access_rw - Read or write data under the iova
1078 * @access: IOAS access to act on
1079 * @iova: Starting IOVA
1080 * @data: Kernel buffer to copy to/from
1081 * @length: Number of bytes to access
1082 * @flags: IOMMUFD_ACCESS_RW_* flags
1083 *
1084 * Copy kernel to/from data into the range given by IOVA/length. If flags
1085 * indicates IOMMUFD_ACCESS_RW_KTHREAD then a large copy can be optimized
1086 * by changing it into copy_to/from_user().
1087 */
iommufd_access_rw(struct iommufd_access * access,unsigned long iova,void * data,size_t length,unsigned int flags)1088 int iommufd_access_rw(struct iommufd_access *access, unsigned long iova,
1089 void *data, size_t length, unsigned int flags)
1090 {
1091 struct iopt_area_contig_iter iter;
1092 struct io_pagetable *iopt;
1093 struct iopt_area *area;
1094 unsigned long last_iova;
1095 int rc = -EINVAL;
1096
1097 if (!length)
1098 return -EINVAL;
1099 if (check_add_overflow(iova, length - 1, &last_iova))
1100 return -EOVERFLOW;
1101
1102 mutex_lock(&access->ioas_lock);
1103 if (!access->ioas) {
1104 mutex_unlock(&access->ioas_lock);
1105 return -ENOENT;
1106 }
1107 iopt = &access->ioas->iopt;
1108
1109 down_read(&iopt->iova_rwsem);
1110 iopt_for_each_contig_area(&iter, area, iopt, iova, last_iova) {
1111 unsigned long last = min(last_iova, iopt_area_last_iova(area));
1112 unsigned long bytes = (last - iter.cur_iova) + 1;
1113
1114 if (area->prevent_access) {
1115 rc = -EINVAL;
1116 goto err_out;
1117 }
1118
1119 if (!check_area_prot(area, flags)) {
1120 rc = -EPERM;
1121 goto err_out;
1122 }
1123
1124 rc = iopt_pages_rw_access(
1125 area->pages, iopt_area_start_byte(area, iter.cur_iova),
1126 data, bytes, flags);
1127 if (rc)
1128 goto err_out;
1129 data += bytes;
1130 }
1131 if (!iopt_area_contig_done(&iter))
1132 rc = -ENOENT;
1133 err_out:
1134 up_read(&iopt->iova_rwsem);
1135 mutex_unlock(&access->ioas_lock);
1136 return rc;
1137 }
1138 EXPORT_SYMBOL_NS_GPL(iommufd_access_rw, IOMMUFD);
1139
iommufd_get_hw_info(struct iommufd_ucmd * ucmd)1140 int iommufd_get_hw_info(struct iommufd_ucmd *ucmd)
1141 {
1142 struct iommu_hw_info *cmd = ucmd->cmd;
1143 void __user *user_ptr = u64_to_user_ptr(cmd->data_uptr);
1144 const struct iommu_ops *ops;
1145 struct iommufd_device *idev;
1146 unsigned int data_len;
1147 unsigned int copy_len;
1148 void *data;
1149 int rc;
1150
1151 if (cmd->flags || cmd->__reserved)
1152 return -EOPNOTSUPP;
1153
1154 idev = iommufd_get_device(ucmd, cmd->dev_id);
1155 if (IS_ERR(idev))
1156 return PTR_ERR(idev);
1157
1158 ops = dev_iommu_ops(idev->dev);
1159 if (ops->hw_info) {
1160 data = ops->hw_info(idev->dev, &data_len, &cmd->out_data_type);
1161 if (IS_ERR(data)) {
1162 rc = PTR_ERR(data);
1163 goto out_put;
1164 }
1165
1166 /*
1167 * drivers that have hw_info callback should have a unique
1168 * iommu_hw_info_type.
1169 */
1170 if (WARN_ON_ONCE(cmd->out_data_type ==
1171 IOMMU_HW_INFO_TYPE_NONE)) {
1172 rc = -ENODEV;
1173 goto out_free;
1174 }
1175 } else {
1176 cmd->out_data_type = IOMMU_HW_INFO_TYPE_NONE;
1177 data_len = 0;
1178 data = NULL;
1179 }
1180
1181 copy_len = min(cmd->data_len, data_len);
1182 if (copy_to_user(user_ptr, data, copy_len)) {
1183 rc = -EFAULT;
1184 goto out_free;
1185 }
1186
1187 /*
1188 * Zero the trailing bytes if the user buffer is bigger than the
1189 * data size kernel actually has.
1190 */
1191 if (copy_len < cmd->data_len) {
1192 if (clear_user(user_ptr + copy_len, cmd->data_len - copy_len)) {
1193 rc = -EFAULT;
1194 goto out_free;
1195 }
1196 }
1197
1198 /*
1199 * We return the length the kernel supports so userspace may know what
1200 * the kernel capability is. It could be larger than the input buffer.
1201 */
1202 cmd->data_len = data_len;
1203
1204 rc = iommufd_ucmd_respond(ucmd, sizeof(*cmd));
1205 out_free:
1206 kfree(data);
1207 out_put:
1208 iommufd_put_object(&idev->obj);
1209 return rc;
1210 }
1211