xref: /openbmc/linux/drivers/iommu/iommufd/device.c (revision a72b9869)
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 
8 #include "io_pagetable.h"
9 #include "iommufd_private.h"
10 
11 static bool allow_unsafe_interrupts;
12 module_param(allow_unsafe_interrupts, bool, S_IRUGO | S_IWUSR);
13 MODULE_PARM_DESC(
14 	allow_unsafe_interrupts,
15 	"Allow IOMMUFD to bind to devices even if the platform cannot isolate "
16 	"the MSI interrupt window. Enabling this is a security weakness.");
17 
18 /*
19  * A iommufd_device object represents the binding relationship between a
20  * consuming driver and the iommufd. These objects are created/destroyed by
21  * external drivers, not by userspace.
22  */
23 struct iommufd_device {
24 	struct iommufd_object obj;
25 	struct iommufd_ctx *ictx;
26 	struct iommufd_hw_pagetable *hwpt;
27 	/* Head at iommufd_hw_pagetable::devices */
28 	struct list_head devices_item;
29 	/* always the physical device */
30 	struct device *dev;
31 	struct iommu_group *group;
32 	bool enforce_cache_coherency;
33 };
34 
35 void iommufd_device_destroy(struct iommufd_object *obj)
36 {
37 	struct iommufd_device *idev =
38 		container_of(obj, struct iommufd_device, obj);
39 
40 	iommu_device_release_dma_owner(idev->dev);
41 	iommu_group_put(idev->group);
42 	iommufd_ctx_put(idev->ictx);
43 }
44 
45 /**
46  * iommufd_device_bind - Bind a physical device to an iommu fd
47  * @ictx: iommufd file descriptor
48  * @dev: Pointer to a physical device struct
49  * @id: Output ID number to return to userspace for this device
50  *
51  * A successful bind establishes an ownership over the device and returns
52  * struct iommufd_device pointer, otherwise returns error pointer.
53  *
54  * A driver using this API must set driver_managed_dma and must not touch
55  * the device until this routine succeeds and establishes ownership.
56  *
57  * Binding a PCI device places the entire RID under iommufd control.
58  *
59  * The caller must undo this with iommufd_device_unbind()
60  */
61 struct iommufd_device *iommufd_device_bind(struct iommufd_ctx *ictx,
62 					   struct device *dev, u32 *id)
63 {
64 	struct iommufd_device *idev;
65 	struct iommu_group *group;
66 	int rc;
67 
68 	/*
69 	 * iommufd always sets IOMMU_CACHE because we offer no way for userspace
70 	 * to restore cache coherency.
71 	 */
72 	if (!device_iommu_capable(dev, IOMMU_CAP_CACHE_COHERENCY))
73 		return ERR_PTR(-EINVAL);
74 
75 	group = iommu_group_get(dev);
76 	if (!group)
77 		return ERR_PTR(-ENODEV);
78 
79 	rc = iommu_device_claim_dma_owner(dev, ictx);
80 	if (rc)
81 		goto out_group_put;
82 
83 	idev = iommufd_object_alloc(ictx, idev, IOMMUFD_OBJ_DEVICE);
84 	if (IS_ERR(idev)) {
85 		rc = PTR_ERR(idev);
86 		goto out_release_owner;
87 	}
88 	idev->ictx = ictx;
89 	iommufd_ctx_get(ictx);
90 	idev->dev = dev;
91 	idev->enforce_cache_coherency =
92 		device_iommu_capable(dev, IOMMU_CAP_ENFORCE_CACHE_COHERENCY);
93 	/* The calling driver is a user until iommufd_device_unbind() */
94 	refcount_inc(&idev->obj.users);
95 	/* group refcount moves into iommufd_device */
96 	idev->group = group;
97 
98 	/*
99 	 * If the caller fails after this success it must call
100 	 * iommufd_unbind_device() which is safe since we hold this refcount.
101 	 * This also means the device is a leaf in the graph and no other object
102 	 * can take a reference on it.
103 	 */
104 	iommufd_object_finalize(ictx, &idev->obj);
105 	*id = idev->obj.id;
106 	return idev;
107 
108 out_release_owner:
109 	iommu_device_release_dma_owner(dev);
110 out_group_put:
111 	iommu_group_put(group);
112 	return ERR_PTR(rc);
113 }
114 EXPORT_SYMBOL_NS_GPL(iommufd_device_bind, IOMMUFD);
115 
116 /**
117  * iommufd_device_unbind - Undo iommufd_device_bind()
118  * @idev: Device returned by iommufd_device_bind()
119  *
120  * Release the device from iommufd control. The DMA ownership will return back
121  * to unowned with DMA controlled by the DMA API. This invalidates the
122  * iommufd_device pointer, other APIs that consume it must not be called
123  * concurrently.
124  */
125 void iommufd_device_unbind(struct iommufd_device *idev)
126 {
127 	bool was_destroyed;
128 
129 	was_destroyed = iommufd_object_destroy_user(idev->ictx, &idev->obj);
130 	WARN_ON(!was_destroyed);
131 }
132 EXPORT_SYMBOL_NS_GPL(iommufd_device_unbind, IOMMUFD);
133 
134 static int iommufd_device_setup_msi(struct iommufd_device *idev,
135 				    struct iommufd_hw_pagetable *hwpt,
136 				    phys_addr_t sw_msi_start)
137 {
138 	int rc;
139 
140 	/*
141 	 * If the IOMMU driver gives a IOMMU_RESV_SW_MSI then it is asking us to
142 	 * call iommu_get_msi_cookie() on its behalf. This is necessary to setup
143 	 * the MSI window so iommu_dma_prepare_msi() can install pages into our
144 	 * domain after request_irq(). If it is not done interrupts will not
145 	 * work on this domain.
146 	 *
147 	 * FIXME: This is conceptually broken for iommufd since we want to allow
148 	 * userspace to change the domains, eg switch from an identity IOAS to a
149 	 * DMA IOAS. There is currently no way to create a MSI window that
150 	 * matches what the IRQ layer actually expects in a newly created
151 	 * domain.
152 	 */
153 	if (sw_msi_start != PHYS_ADDR_MAX && !hwpt->msi_cookie) {
154 		rc = iommu_get_msi_cookie(hwpt->domain, sw_msi_start);
155 		if (rc)
156 			return rc;
157 
158 		/*
159 		 * iommu_get_msi_cookie() can only be called once per domain,
160 		 * it returns -EBUSY on later calls.
161 		 */
162 		hwpt->msi_cookie = true;
163 	}
164 
165 	/*
166 	 * For historical compat with VFIO the insecure interrupt path is
167 	 * allowed if the module parameter is set. Insecure means that a MemWr
168 	 * operation from the device (eg a simple DMA) cannot trigger an
169 	 * interrupt outside this iommufd context.
170 	 */
171 	if (!iommu_group_has_isolated_msi(idev->group)) {
172 		if (!allow_unsafe_interrupts)
173 			return -EPERM;
174 
175 		dev_warn(
176 			idev->dev,
177 			"MSI interrupts are not secure, they cannot be isolated by the platform. "
178 			"Check that platform features like interrupt remapping are enabled. "
179 			"Use the \"allow_unsafe_interrupts\" module parameter to override\n");
180 	}
181 	return 0;
182 }
183 
184 static bool iommufd_hw_pagetable_has_group(struct iommufd_hw_pagetable *hwpt,
185 					   struct iommu_group *group)
186 {
187 	struct iommufd_device *cur_dev;
188 
189 	list_for_each_entry(cur_dev, &hwpt->devices, devices_item)
190 		if (cur_dev->group == group)
191 			return true;
192 	return false;
193 }
194 
195 static int iommufd_device_do_attach(struct iommufd_device *idev,
196 				    struct iommufd_hw_pagetable *hwpt)
197 {
198 	phys_addr_t sw_msi_start = PHYS_ADDR_MAX;
199 	int rc;
200 
201 	mutex_lock(&hwpt->devices_lock);
202 
203 	/*
204 	 * Try to upgrade the domain we have, it is an iommu driver bug to
205 	 * report IOMMU_CAP_ENFORCE_CACHE_COHERENCY but fail
206 	 * enforce_cache_coherency when there are no devices attached to the
207 	 * domain.
208 	 */
209 	if (idev->enforce_cache_coherency && !hwpt->enforce_cache_coherency) {
210 		if (hwpt->domain->ops->enforce_cache_coherency)
211 			hwpt->enforce_cache_coherency =
212 				hwpt->domain->ops->enforce_cache_coherency(
213 					hwpt->domain);
214 		if (!hwpt->enforce_cache_coherency) {
215 			WARN_ON(list_empty(&hwpt->devices));
216 			rc = -EINVAL;
217 			goto out_unlock;
218 		}
219 	}
220 
221 	rc = iopt_table_enforce_group_resv_regions(&hwpt->ioas->iopt, idev->dev,
222 						   idev->group, &sw_msi_start);
223 	if (rc)
224 		goto out_unlock;
225 
226 	rc = iommufd_device_setup_msi(idev, hwpt, sw_msi_start);
227 	if (rc)
228 		goto out_iova;
229 
230 	/*
231 	 * FIXME: Hack around missing a device-centric iommu api, only attach to
232 	 * the group once for the first device that is in the group.
233 	 */
234 	if (!iommufd_hw_pagetable_has_group(hwpt, idev->group)) {
235 		rc = iommu_attach_group(hwpt->domain, idev->group);
236 		if (rc)
237 			goto out_iova;
238 
239 		if (list_empty(&hwpt->devices)) {
240 			rc = iopt_table_add_domain(&hwpt->ioas->iopt,
241 						   hwpt->domain);
242 			if (rc)
243 				goto out_detach;
244 		}
245 	}
246 
247 	idev->hwpt = hwpt;
248 	refcount_inc(&hwpt->obj.users);
249 	list_add(&idev->devices_item, &hwpt->devices);
250 	mutex_unlock(&hwpt->devices_lock);
251 	return 0;
252 
253 out_detach:
254 	iommu_detach_group(hwpt->domain, idev->group);
255 out_iova:
256 	iopt_remove_reserved_iova(&hwpt->ioas->iopt, idev->dev);
257 out_unlock:
258 	mutex_unlock(&hwpt->devices_lock);
259 	return rc;
260 }
261 
262 /*
263  * When automatically managing the domains we search for a compatible domain in
264  * the iopt and if one is found use it, otherwise create a new domain.
265  * Automatic domain selection will never pick a manually created domain.
266  */
267 static int iommufd_device_auto_get_domain(struct iommufd_device *idev,
268 					  struct iommufd_ioas *ioas)
269 {
270 	struct iommufd_hw_pagetable *hwpt;
271 	int rc;
272 
273 	/*
274 	 * There is no differentiation when domains are allocated, so any domain
275 	 * that is willing to attach to the device is interchangeable with any
276 	 * other.
277 	 */
278 	mutex_lock(&ioas->mutex);
279 	list_for_each_entry(hwpt, &ioas->hwpt_list, hwpt_item) {
280 		if (!hwpt->auto_domain)
281 			continue;
282 
283 		rc = iommufd_device_do_attach(idev, hwpt);
284 
285 		/*
286 		 * -EINVAL means the domain is incompatible with the device.
287 		 * Other error codes should propagate to userspace as failure.
288 		 * Success means the domain is attached.
289 		 */
290 		if (rc == -EINVAL)
291 			continue;
292 		goto out_unlock;
293 	}
294 
295 	hwpt = iommufd_hw_pagetable_alloc(idev->ictx, ioas, idev->dev);
296 	if (IS_ERR(hwpt)) {
297 		rc = PTR_ERR(hwpt);
298 		goto out_unlock;
299 	}
300 	hwpt->auto_domain = true;
301 
302 	rc = iommufd_device_do_attach(idev, hwpt);
303 	if (rc)
304 		goto out_abort;
305 	list_add_tail(&hwpt->hwpt_item, &ioas->hwpt_list);
306 
307 	mutex_unlock(&ioas->mutex);
308 	iommufd_object_finalize(idev->ictx, &hwpt->obj);
309 	return 0;
310 
311 out_abort:
312 	iommufd_object_abort_and_destroy(idev->ictx, &hwpt->obj);
313 out_unlock:
314 	mutex_unlock(&ioas->mutex);
315 	return rc;
316 }
317 
318 /**
319  * iommufd_device_attach - Connect a device from an iommu_domain
320  * @idev: device to attach
321  * @pt_id: Input a IOMMUFD_OBJ_IOAS, or IOMMUFD_OBJ_HW_PAGETABLE
322  *         Output the IOMMUFD_OBJ_HW_PAGETABLE ID
323  *
324  * This connects the device to an iommu_domain, either automatically or manually
325  * selected. Once this completes the device could do DMA.
326  *
327  * The caller should return the resulting pt_id back to userspace.
328  * This function is undone by calling iommufd_device_detach().
329  */
330 int iommufd_device_attach(struct iommufd_device *idev, u32 *pt_id)
331 {
332 	struct iommufd_object *pt_obj;
333 	int rc;
334 
335 	pt_obj = iommufd_get_object(idev->ictx, *pt_id, IOMMUFD_OBJ_ANY);
336 	if (IS_ERR(pt_obj))
337 		return PTR_ERR(pt_obj);
338 
339 	switch (pt_obj->type) {
340 	case IOMMUFD_OBJ_HW_PAGETABLE: {
341 		struct iommufd_hw_pagetable *hwpt =
342 			container_of(pt_obj, struct iommufd_hw_pagetable, obj);
343 
344 		rc = iommufd_device_do_attach(idev, hwpt);
345 		if (rc)
346 			goto out_put_pt_obj;
347 		break;
348 	}
349 	case IOMMUFD_OBJ_IOAS: {
350 		struct iommufd_ioas *ioas =
351 			container_of(pt_obj, struct iommufd_ioas, obj);
352 
353 		rc = iommufd_device_auto_get_domain(idev, ioas);
354 		if (rc)
355 			goto out_put_pt_obj;
356 		break;
357 	}
358 	default:
359 		rc = -EINVAL;
360 		goto out_put_pt_obj;
361 	}
362 
363 	refcount_inc(&idev->obj.users);
364 	*pt_id = idev->hwpt->obj.id;
365 	rc = 0;
366 
367 out_put_pt_obj:
368 	iommufd_put_object(pt_obj);
369 	return rc;
370 }
371 EXPORT_SYMBOL_NS_GPL(iommufd_device_attach, IOMMUFD);
372 
373 /**
374  * iommufd_device_detach - Disconnect a device to an iommu_domain
375  * @idev: device to detach
376  *
377  * Undo iommufd_device_attach(). This disconnects the idev from the previously
378  * attached pt_id. The device returns back to a blocked DMA translation.
379  */
380 void iommufd_device_detach(struct iommufd_device *idev)
381 {
382 	struct iommufd_hw_pagetable *hwpt = idev->hwpt;
383 
384 	mutex_lock(&hwpt->ioas->mutex);
385 	mutex_lock(&hwpt->devices_lock);
386 	list_del(&idev->devices_item);
387 	if (!iommufd_hw_pagetable_has_group(hwpt, idev->group)) {
388 		if (list_empty(&hwpt->devices)) {
389 			iopt_table_remove_domain(&hwpt->ioas->iopt,
390 						 hwpt->domain);
391 			list_del(&hwpt->hwpt_item);
392 		}
393 		iommu_detach_group(hwpt->domain, idev->group);
394 	}
395 	iopt_remove_reserved_iova(&hwpt->ioas->iopt, idev->dev);
396 	mutex_unlock(&hwpt->devices_lock);
397 	mutex_unlock(&hwpt->ioas->mutex);
398 
399 	if (hwpt->auto_domain)
400 		iommufd_object_destroy_user(idev->ictx, &hwpt->obj);
401 	else
402 		refcount_dec(&hwpt->obj.users);
403 
404 	idev->hwpt = NULL;
405 
406 	refcount_dec(&idev->obj.users);
407 }
408 EXPORT_SYMBOL_NS_GPL(iommufd_device_detach, IOMMUFD);
409 
410 void iommufd_access_destroy_object(struct iommufd_object *obj)
411 {
412 	struct iommufd_access *access =
413 		container_of(obj, struct iommufd_access, obj);
414 
415 	iopt_remove_access(&access->ioas->iopt, access);
416 	iommufd_ctx_put(access->ictx);
417 	refcount_dec(&access->ioas->obj.users);
418 }
419 
420 /**
421  * iommufd_access_create - Create an iommufd_access
422  * @ictx: iommufd file descriptor
423  * @ioas_id: ID for a IOMMUFD_OBJ_IOAS
424  * @ops: Driver's ops to associate with the access
425  * @data: Opaque data to pass into ops functions
426  *
427  * An iommufd_access allows a driver to read/write to the IOAS without using
428  * DMA. The underlying CPU memory can be accessed using the
429  * iommufd_access_pin_pages() or iommufd_access_rw() functions.
430  *
431  * The provided ops are required to use iommufd_access_pin_pages().
432  */
433 struct iommufd_access *
434 iommufd_access_create(struct iommufd_ctx *ictx, u32 ioas_id,
435 		      const struct iommufd_access_ops *ops, void *data)
436 {
437 	struct iommufd_access *access;
438 	struct iommufd_object *obj;
439 	int rc;
440 
441 	/*
442 	 * There is no uAPI for the access object, but to keep things symmetric
443 	 * use the object infrastructure anyhow.
444 	 */
445 	access = iommufd_object_alloc(ictx, access, IOMMUFD_OBJ_ACCESS);
446 	if (IS_ERR(access))
447 		return access;
448 
449 	access->data = data;
450 	access->ops = ops;
451 
452 	obj = iommufd_get_object(ictx, ioas_id, IOMMUFD_OBJ_IOAS);
453 	if (IS_ERR(obj)) {
454 		rc = PTR_ERR(obj);
455 		goto out_abort;
456 	}
457 	access->ioas = container_of(obj, struct iommufd_ioas, obj);
458 	iommufd_ref_to_users(obj);
459 
460 	if (ops->needs_pin_pages)
461 		access->iova_alignment = PAGE_SIZE;
462 	else
463 		access->iova_alignment = 1;
464 	rc = iopt_add_access(&access->ioas->iopt, access);
465 	if (rc)
466 		goto out_put_ioas;
467 
468 	/* The calling driver is a user until iommufd_access_destroy() */
469 	refcount_inc(&access->obj.users);
470 	access->ictx = ictx;
471 	iommufd_ctx_get(ictx);
472 	iommufd_object_finalize(ictx, &access->obj);
473 	return access;
474 out_put_ioas:
475 	refcount_dec(&access->ioas->obj.users);
476 out_abort:
477 	iommufd_object_abort(ictx, &access->obj);
478 	return ERR_PTR(rc);
479 }
480 EXPORT_SYMBOL_NS_GPL(iommufd_access_create, IOMMUFD);
481 
482 /**
483  * iommufd_access_destroy - Destroy an iommufd_access
484  * @access: The access to destroy
485  *
486  * The caller must stop using the access before destroying it.
487  */
488 void iommufd_access_destroy(struct iommufd_access *access)
489 {
490 	bool was_destroyed;
491 
492 	was_destroyed = iommufd_object_destroy_user(access->ictx, &access->obj);
493 	WARN_ON(!was_destroyed);
494 }
495 EXPORT_SYMBOL_NS_GPL(iommufd_access_destroy, IOMMUFD);
496 
497 /**
498  * iommufd_access_notify_unmap - Notify users of an iopt to stop using it
499  * @iopt: iopt to work on
500  * @iova: Starting iova in the iopt
501  * @length: Number of bytes
502  *
503  * After this function returns there should be no users attached to the pages
504  * linked to this iopt that intersect with iova,length. Anyone that has attached
505  * a user through iopt_access_pages() needs to detach it through
506  * iommufd_access_unpin_pages() before this function returns.
507  *
508  * iommufd_access_destroy() will wait for any outstanding unmap callback to
509  * complete. Once iommufd_access_destroy() no unmap ops are running or will
510  * run in the future. Due to this a driver must not create locking that prevents
511  * unmap to complete while iommufd_access_destroy() is running.
512  */
513 void iommufd_access_notify_unmap(struct io_pagetable *iopt, unsigned long iova,
514 				 unsigned long length)
515 {
516 	struct iommufd_ioas *ioas =
517 		container_of(iopt, struct iommufd_ioas, iopt);
518 	struct iommufd_access *access;
519 	unsigned long index;
520 
521 	xa_lock(&ioas->iopt.access_list);
522 	xa_for_each(&ioas->iopt.access_list, index, access) {
523 		if (!iommufd_lock_obj(&access->obj))
524 			continue;
525 		xa_unlock(&ioas->iopt.access_list);
526 
527 		access->ops->unmap(access->data, iova, length);
528 
529 		iommufd_put_object(&access->obj);
530 		xa_lock(&ioas->iopt.access_list);
531 	}
532 	xa_unlock(&ioas->iopt.access_list);
533 }
534 
535 /**
536  * iommufd_access_unpin_pages() - Undo iommufd_access_pin_pages
537  * @access: IOAS access to act on
538  * @iova: Starting IOVA
539  * @length: Number of bytes to access
540  *
541  * Return the struct page's. The caller must stop accessing them before calling
542  * this. The iova/length must exactly match the one provided to access_pages.
543  */
544 void iommufd_access_unpin_pages(struct iommufd_access *access,
545 				unsigned long iova, unsigned long length)
546 {
547 	struct io_pagetable *iopt = &access->ioas->iopt;
548 	struct iopt_area_contig_iter iter;
549 	unsigned long last_iova;
550 	struct iopt_area *area;
551 
552 	if (WARN_ON(!length) ||
553 	    WARN_ON(check_add_overflow(iova, length - 1, &last_iova)))
554 		return;
555 
556 	down_read(&iopt->iova_rwsem);
557 	iopt_for_each_contig_area(&iter, area, iopt, iova, last_iova)
558 		iopt_area_remove_access(
559 			area, iopt_area_iova_to_index(area, iter.cur_iova),
560 			iopt_area_iova_to_index(
561 				area,
562 				min(last_iova, iopt_area_last_iova(area))));
563 	up_read(&iopt->iova_rwsem);
564 	WARN_ON(!iopt_area_contig_done(&iter));
565 }
566 EXPORT_SYMBOL_NS_GPL(iommufd_access_unpin_pages, IOMMUFD);
567 
568 static bool iopt_area_contig_is_aligned(struct iopt_area_contig_iter *iter)
569 {
570 	if (iopt_area_start_byte(iter->area, iter->cur_iova) % PAGE_SIZE)
571 		return false;
572 
573 	if (!iopt_area_contig_done(iter) &&
574 	    (iopt_area_start_byte(iter->area, iopt_area_last_iova(iter->area)) %
575 	     PAGE_SIZE) != (PAGE_SIZE - 1))
576 		return false;
577 	return true;
578 }
579 
580 static bool check_area_prot(struct iopt_area *area, unsigned int flags)
581 {
582 	if (flags & IOMMUFD_ACCESS_RW_WRITE)
583 		return area->iommu_prot & IOMMU_WRITE;
584 	return area->iommu_prot & IOMMU_READ;
585 }
586 
587 /**
588  * iommufd_access_pin_pages() - Return a list of pages under the iova
589  * @access: IOAS access to act on
590  * @iova: Starting IOVA
591  * @length: Number of bytes to access
592  * @out_pages: Output page list
593  * @flags: IOPMMUFD_ACCESS_RW_* flags
594  *
595  * Reads @length bytes starting at iova and returns the struct page * pointers.
596  * These can be kmap'd by the caller for CPU access.
597  *
598  * The caller must perform iommufd_access_unpin_pages() when done to balance
599  * this.
600  *
601  * This API always requires a page aligned iova. This happens naturally if the
602  * ioas alignment is >= PAGE_SIZE and the iova is PAGE_SIZE aligned. However
603  * smaller alignments have corner cases where this API can fail on otherwise
604  * aligned iova.
605  */
606 int iommufd_access_pin_pages(struct iommufd_access *access, unsigned long iova,
607 			     unsigned long length, struct page **out_pages,
608 			     unsigned int flags)
609 {
610 	struct io_pagetable *iopt = &access->ioas->iopt;
611 	struct iopt_area_contig_iter iter;
612 	unsigned long last_iova;
613 	struct iopt_area *area;
614 	int rc;
615 
616 	/* Driver's ops don't support pin_pages */
617 	if (IS_ENABLED(CONFIG_IOMMUFD_TEST) &&
618 	    WARN_ON(access->iova_alignment != PAGE_SIZE || !access->ops->unmap))
619 		return -EINVAL;
620 
621 	if (!length)
622 		return -EINVAL;
623 	if (check_add_overflow(iova, length - 1, &last_iova))
624 		return -EOVERFLOW;
625 
626 	down_read(&iopt->iova_rwsem);
627 	iopt_for_each_contig_area(&iter, area, iopt, iova, last_iova) {
628 		unsigned long last = min(last_iova, iopt_area_last_iova(area));
629 		unsigned long last_index = iopt_area_iova_to_index(area, last);
630 		unsigned long index =
631 			iopt_area_iova_to_index(area, iter.cur_iova);
632 
633 		if (area->prevent_access ||
634 		    !iopt_area_contig_is_aligned(&iter)) {
635 			rc = -EINVAL;
636 			goto err_remove;
637 		}
638 
639 		if (!check_area_prot(area, flags)) {
640 			rc = -EPERM;
641 			goto err_remove;
642 		}
643 
644 		rc = iopt_area_add_access(area, index, last_index, out_pages,
645 					  flags);
646 		if (rc)
647 			goto err_remove;
648 		out_pages += last_index - index + 1;
649 	}
650 	if (!iopt_area_contig_done(&iter)) {
651 		rc = -ENOENT;
652 		goto err_remove;
653 	}
654 
655 	up_read(&iopt->iova_rwsem);
656 	return 0;
657 
658 err_remove:
659 	if (iova < iter.cur_iova) {
660 		last_iova = iter.cur_iova - 1;
661 		iopt_for_each_contig_area(&iter, area, iopt, iova, last_iova)
662 			iopt_area_remove_access(
663 				area,
664 				iopt_area_iova_to_index(area, iter.cur_iova),
665 				iopt_area_iova_to_index(
666 					area, min(last_iova,
667 						  iopt_area_last_iova(area))));
668 	}
669 	up_read(&iopt->iova_rwsem);
670 	return rc;
671 }
672 EXPORT_SYMBOL_NS_GPL(iommufd_access_pin_pages, IOMMUFD);
673 
674 /**
675  * iommufd_access_rw - Read or write data under the iova
676  * @access: IOAS access to act on
677  * @iova: Starting IOVA
678  * @data: Kernel buffer to copy to/from
679  * @length: Number of bytes to access
680  * @flags: IOMMUFD_ACCESS_RW_* flags
681  *
682  * Copy kernel to/from data into the range given by IOVA/length. If flags
683  * indicates IOMMUFD_ACCESS_RW_KTHREAD then a large copy can be optimized
684  * by changing it into copy_to/from_user().
685  */
686 int iommufd_access_rw(struct iommufd_access *access, unsigned long iova,
687 		      void *data, size_t length, unsigned int flags)
688 {
689 	struct io_pagetable *iopt = &access->ioas->iopt;
690 	struct iopt_area_contig_iter iter;
691 	struct iopt_area *area;
692 	unsigned long last_iova;
693 	int rc;
694 
695 	if (!length)
696 		return -EINVAL;
697 	if (check_add_overflow(iova, length - 1, &last_iova))
698 		return -EOVERFLOW;
699 
700 	down_read(&iopt->iova_rwsem);
701 	iopt_for_each_contig_area(&iter, area, iopt, iova, last_iova) {
702 		unsigned long last = min(last_iova, iopt_area_last_iova(area));
703 		unsigned long bytes = (last - iter.cur_iova) + 1;
704 
705 		if (area->prevent_access) {
706 			rc = -EINVAL;
707 			goto err_out;
708 		}
709 
710 		if (!check_area_prot(area, flags)) {
711 			rc = -EPERM;
712 			goto err_out;
713 		}
714 
715 		rc = iopt_pages_rw_access(
716 			area->pages, iopt_area_start_byte(area, iter.cur_iova),
717 			data, bytes, flags);
718 		if (rc)
719 			goto err_out;
720 		data += bytes;
721 	}
722 	if (!iopt_area_contig_done(&iter))
723 		rc = -ENOENT;
724 err_out:
725 	up_read(&iopt->iova_rwsem);
726 	return rc;
727 }
728 EXPORT_SYMBOL_NS_GPL(iommufd_access_rw, IOMMUFD);
729 
730 #ifdef CONFIG_IOMMUFD_TEST
731 /*
732  * Creating a real iommufd_device is too hard, bypass creating a iommufd_device
733  * and go directly to attaching a domain.
734  */
735 struct iommufd_hw_pagetable *
736 iommufd_device_selftest_attach(struct iommufd_ctx *ictx,
737 			       struct iommufd_ioas *ioas,
738 			       struct device *mock_dev)
739 {
740 	struct iommufd_hw_pagetable *hwpt;
741 	int rc;
742 
743 	hwpt = iommufd_hw_pagetable_alloc(ictx, ioas, mock_dev);
744 	if (IS_ERR(hwpt))
745 		return hwpt;
746 
747 	rc = iopt_table_add_domain(&hwpt->ioas->iopt, hwpt->domain);
748 	if (rc)
749 		goto out_hwpt;
750 
751 	refcount_inc(&hwpt->obj.users);
752 	iommufd_object_finalize(ictx, &hwpt->obj);
753 	return hwpt;
754 
755 out_hwpt:
756 	iommufd_object_abort_and_destroy(ictx, &hwpt->obj);
757 	return ERR_PTR(rc);
758 }
759 
760 void iommufd_device_selftest_detach(struct iommufd_ctx *ictx,
761 				    struct iommufd_hw_pagetable *hwpt)
762 {
763 	iopt_table_remove_domain(&hwpt->ioas->iopt, hwpt->domain);
764 	refcount_dec(&hwpt->obj.users);
765 }
766 #endif
767