1 // SPDX-License-Identifier: GPL-2.0-only 2 /* Copyright (c) 2021-2022, NVIDIA CORPORATION & AFFILIATES 3 */ 4 #include <linux/iommu.h> 5 #include <linux/iommufd.h> 6 #include <linux/pci-ats.h> 7 #include <linux/slab.h> 8 #include <uapi/linux/iommufd.h> 9 10 #include "../iommu-priv.h" 11 #include "io_pagetable.h" 12 #include "iommufd_private.h" 13 14 static bool allow_unsafe_interrupts; 15 module_param(allow_unsafe_interrupts, bool, S_IRUGO | S_IWUSR); 16 MODULE_PARM_DESC( 17 allow_unsafe_interrupts, 18 "Allow IOMMUFD to bind to devices even if the platform cannot isolate " 19 "the MSI interrupt window. Enabling this is a security weakness."); 20 21 struct iommufd_attach { 22 struct iommufd_hw_pagetable *hwpt; 23 struct xarray device_array; 24 }; 25 26 /* 27 * Detect a noiommu device for the cdev path. We check dev->iommu rather than 28 * using device_iommu_mapped() (which checks dev->iommu_group) because when 29 * both group and cdev interfaces coexist, the group path assigns a fake 30 * noiommu iommu_group to the device. That would cause device_iommu_mapped() 31 * to return true and hide the noiommu case from the cdev path. dev->iommu is 32 * reliably NULL when no IOMMU driver is managing the device. 33 */ 34 static bool iommufd_device_is_noiommu(struct iommufd_device *idev) 35 { 36 return IS_ENABLED(CONFIG_IOMMUFD_NOIOMMU) && !idev->dev->iommu; 37 } 38 39 static void iommufd_group_release(struct kref *kref) 40 { 41 struct iommufd_group *igroup = 42 container_of(kref, struct iommufd_group, ref); 43 44 WARN_ON(!xa_empty(&igroup->pasid_attach)); 45 46 if (igroup->group) { 47 xa_cmpxchg(&igroup->ictx->groups, iommu_group_id(igroup->group), 48 igroup, NULL, GFP_KERNEL); 49 iommu_group_put(igroup->group); 50 } 51 mutex_destroy(&igroup->lock); 52 kfree(igroup); 53 } 54 55 static void iommufd_put_group(struct iommufd_group *group) 56 { 57 kref_put(&group->ref, iommufd_group_release); 58 } 59 60 static bool iommufd_group_try_get(struct iommufd_group *igroup, 61 struct iommu_group *group) 62 { 63 if (!igroup) 64 return false; 65 /* 66 * group ID's cannot be re-used until the group is put back which does 67 * not happen if we could get an igroup pointer under the xa_lock. 68 */ 69 if (WARN_ON(igroup->group != group)) 70 return false; 71 return kref_get_unless_zero(&igroup->ref); 72 } 73 74 static struct iommufd_group *iommufd_alloc_group(struct iommufd_ctx *ictx, 75 struct iommu_group *group) 76 { 77 struct iommufd_group *new_igroup; 78 79 new_igroup = kzalloc_obj(*new_igroup, GFP_KERNEL); 80 if (!new_igroup) 81 return ERR_PTR(-ENOMEM); 82 83 kref_init(&new_igroup->ref); 84 mutex_init(&new_igroup->lock); 85 xa_init(&new_igroup->pasid_attach); 86 new_igroup->sw_msi_start = PHYS_ADDR_MAX; 87 /* group reference moves into new_igroup */ 88 new_igroup->group = group; 89 90 /* 91 * The ictx is not additionally refcounted here because all objects using 92 * an igroup must put it before their destroy completes. 93 */ 94 new_igroup->ictx = ictx; 95 return new_igroup; 96 } 97 98 /* 99 * iommufd needs to store some more data for each iommu_group, we keep a 100 * parallel xarray indexed by iommu_group id to hold this instead of putting it 101 * in the core structure. To keep things simple the iommufd_group memory is 102 * unique within the iommufd_ctx. This makes it easy to check there are no 103 * memory leaks. 104 */ 105 static struct iommufd_group *iommufd_get_group(struct iommufd_ctx *ictx, 106 struct device *dev) 107 { 108 struct iommufd_group *new_igroup; 109 struct iommufd_group *cur_igroup; 110 struct iommufd_group *igroup; 111 struct iommu_group *group; 112 unsigned int id; 113 114 group = iommu_group_get(dev); 115 if (!group) 116 return ERR_PTR(-ENODEV); 117 118 id = iommu_group_id(group); 119 120 xa_lock(&ictx->groups); 121 igroup = xa_load(&ictx->groups, id); 122 if (iommufd_group_try_get(igroup, group)) { 123 xa_unlock(&ictx->groups); 124 iommu_group_put(group); 125 return igroup; 126 } 127 xa_unlock(&ictx->groups); 128 129 new_igroup = iommufd_alloc_group(ictx, group); 130 if (IS_ERR(new_igroup)) { 131 iommu_group_put(group); 132 return new_igroup; 133 } 134 135 /* 136 * We dropped the lock so igroup is invalid. NULL is a safe and likely 137 * value to assume for the xa_cmpxchg algorithm. 138 */ 139 cur_igroup = NULL; 140 xa_lock(&ictx->groups); 141 while (true) { 142 igroup = __xa_cmpxchg(&ictx->groups, id, cur_igroup, new_igroup, 143 GFP_KERNEL); 144 if (xa_is_err(igroup)) { 145 xa_unlock(&ictx->groups); 146 iommufd_put_group(new_igroup); 147 return ERR_PTR(xa_err(igroup)); 148 } 149 150 /* new_group was successfully installed */ 151 if (cur_igroup == igroup) { 152 xa_unlock(&ictx->groups); 153 return new_igroup; 154 } 155 156 /* Check again if the current group is any good */ 157 if (iommufd_group_try_get(igroup, group)) { 158 xa_unlock(&ictx->groups); 159 iommufd_put_group(new_igroup); 160 return igroup; 161 } 162 cur_igroup = igroup; 163 } 164 } 165 166 static void iommufd_device_remove_vdev(struct iommufd_device *idev) 167 { 168 struct iommufd_vdevice *vdev; 169 170 mutex_lock(&idev->igroup->lock); 171 /* prevent new references from vdev */ 172 idev->destroying = true; 173 /* vdev has been completely destroyed by userspace */ 174 if (!idev->vdev) 175 goto out_unlock; 176 177 vdev = idev->vdev; 178 179 /* 180 * An ongoing vdev destroy ioctl has removed the vdev from the object 181 * xarray, but has not finished iommufd_vdevice_destroy() yet as it 182 * needs the same mutex. We exit the locking then wait on wait_cnt 183 * reference for the vdev destruction. 184 */ 185 if (iommufd_try_inc_users(idev->ictx, &vdev->obj)) 186 goto out_unlock; 187 188 /* 189 * vdev is still alive. Hold a users refcount to prevent racing with 190 * userspace destruction, then use iommufd_object_tombstone_user() to 191 * destroy it and leave a tombstone. 192 */ 193 mutex_unlock(&idev->igroup->lock); 194 iommufd_object_tombstone_user(idev->ictx, &vdev->obj); 195 return; 196 197 out_unlock: 198 mutex_unlock(&idev->igroup->lock); 199 } 200 201 void iommufd_device_pre_destroy(struct iommufd_object *obj) 202 { 203 struct iommufd_device *idev = 204 container_of(obj, struct iommufd_device, obj); 205 206 /* Release the wait_cnt reference on this */ 207 iommufd_device_remove_vdev(idev); 208 } 209 210 void iommufd_device_destroy(struct iommufd_object *obj) 211 { 212 struct iommufd_device *idev = 213 container_of(obj, struct iommufd_device, obj); 214 215 /* igroup is NULL when destroy called during bind error cleanup */ 216 if (!idev->igroup) 217 return; 218 if (!iommufd_device_is_noiommu(idev)) 219 iommu_device_release_dma_owner(idev->dev); 220 iommufd_put_group(idev->igroup); 221 if (!iommufd_selftest_is_mock_dev(idev->dev)) 222 iommufd_ctx_put(idev->ictx); 223 } 224 225 static int iommufd_bind_iommu(struct iommufd_device *idev) 226 { 227 struct iommufd_ctx *ictx = idev->ictx; 228 struct device *dev = idev->dev; 229 struct iommufd_group *igroup; 230 int rc; 231 232 /* 233 * iommufd always sets IOMMU_CACHE because we offer no way for userspace 234 * to restore cache coherency. 235 */ 236 if (!device_iommu_capable(dev, IOMMU_CAP_CACHE_COHERENCY)) 237 return -EINVAL; 238 239 igroup = iommufd_get_group(ictx, dev); 240 if (IS_ERR(igroup)) 241 return PTR_ERR(igroup); 242 243 /* 244 * For historical compat with VFIO the insecure interrupt path is 245 * allowed if the module parameter is set. Secure/Isolated means that a 246 * MemWr operation from the device (eg a simple DMA) cannot trigger an 247 * interrupt outside this iommufd context. 248 */ 249 if (!iommufd_selftest_is_mock_dev(dev) && 250 !iommu_group_has_isolated_msi(igroup->group)) { 251 if (!allow_unsafe_interrupts) { 252 rc = -EPERM; 253 goto out_group_put; 254 } 255 256 dev_warn( 257 dev, 258 "MSI interrupts are not secure, they cannot be isolated by the platform. " 259 "Check that platform features like interrupt remapping are enabled. " 260 "Use the \"allow_unsafe_interrupts\" module parameter to override\n"); 261 } 262 263 rc = iommu_device_claim_dma_owner(dev, ictx); 264 if (rc) 265 goto out_group_put; 266 267 /* igroup refcount moves into iommufd_device */ 268 idev->igroup = igroup; 269 idev->enforce_cache_coherency = 270 device_iommu_capable(dev, IOMMU_CAP_ENFORCE_CACHE_COHERENCY); 271 return 0; 272 273 out_group_put: 274 iommufd_put_group(igroup); 275 return rc; 276 } 277 278 /* 279 * Noiommu devices have no real IOMMU group. Create a dummy igroup so that 280 * internal code paths that expect idev->igroup to be present still work. 281 * A NULL igroup->group distinguishes this from a real IOMMU-backed group. 282 */ 283 static int iommufd_bind_noiommu(struct iommufd_device *idev) 284 { 285 struct iommufd_group *igroup; 286 287 igroup = iommufd_alloc_group(idev->ictx, NULL); 288 if (IS_ERR(igroup)) 289 return PTR_ERR(igroup); 290 idev->igroup = igroup; 291 return 0; 292 } 293 294 /** 295 * iommufd_device_bind - Bind a physical device to an iommu fd 296 * @ictx: iommufd file descriptor 297 * @dev: Pointer to a physical device struct 298 * @id: Output ID number to return to userspace for this device 299 * 300 * A successful bind establishes an ownership over the device and returns 301 * struct iommufd_device pointer, otherwise returns error pointer. 302 * 303 * A driver using this API must set driver_managed_dma and must not touch 304 * the device until this routine succeeds and establishes ownership. 305 * 306 * Binding a PCI device places the entire RID under iommufd control. 307 * 308 * The caller must undo this with iommufd_device_unbind() 309 */ 310 struct iommufd_device *iommufd_device_bind(struct iommufd_ctx *ictx, 311 struct device *dev, u32 *id) 312 { 313 struct iommufd_device *idev; 314 int rc; 315 316 idev = iommufd_object_alloc(ictx, idev, IOMMUFD_OBJ_DEVICE); 317 if (IS_ERR(idev)) 318 return idev; 319 320 idev->ictx = ictx; 321 idev->dev = dev; 322 323 if (!iommufd_device_is_noiommu(idev)) 324 rc = iommufd_bind_iommu(idev); 325 else 326 rc = iommufd_bind_noiommu(idev); 327 if (rc) 328 goto err_out; 329 330 /* 331 * Take a ctx reference after bind succeeds. This must happen here 332 * so that iommufd_device_destroy() can handle partial initialization 333 */ 334 if (!iommufd_selftest_is_mock_dev(dev)) 335 iommufd_ctx_get(ictx); 336 /* The calling driver is a user until iommufd_device_unbind() */ 337 refcount_inc(&idev->obj.users); 338 339 /* 340 * If the caller fails after this success it must call 341 * iommufd_unbind_device() which is safe since we hold this refcount. 342 * This also means the device is a leaf in the graph and no other object 343 * can take a reference on it. 344 */ 345 iommufd_object_finalize(ictx, &idev->obj); 346 *id = idev->obj.id; 347 return idev; 348 349 err_out: 350 /* 351 * iommufd_device_destroy() handles partially initialized idev, 352 * so iommufd_object_abort_and_destroy() is safe to call here. 353 */ 354 iommufd_object_abort_and_destroy(ictx, &idev->obj); 355 return ERR_PTR(rc); 356 357 } 358 EXPORT_SYMBOL_NS_GPL(iommufd_device_bind, "IOMMUFD"); 359 360 /** 361 * iommufd_ctx_has_group - True if any device within the group is bound 362 * to the ictx 363 * @ictx: iommufd file descriptor 364 * @group: Pointer to a physical iommu_group struct 365 * 366 * True if any device within the group has been bound to this ictx, ex. via 367 * iommufd_device_bind(), therefore implying ictx ownership of the group. 368 */ 369 bool iommufd_ctx_has_group(struct iommufd_ctx *ictx, struct iommu_group *group) 370 { 371 struct iommufd_object *obj; 372 unsigned long index; 373 374 if (!ictx || !group) 375 return false; 376 377 xa_lock(&ictx->objects); 378 xa_for_each(&ictx->objects, index, obj) { 379 if (obj->type == IOMMUFD_OBJ_DEVICE && 380 container_of(obj, struct iommufd_device, obj) 381 ->igroup->group == group) { 382 xa_unlock(&ictx->objects); 383 return true; 384 } 385 } 386 xa_unlock(&ictx->objects); 387 return false; 388 } 389 EXPORT_SYMBOL_NS_GPL(iommufd_ctx_has_group, "IOMMUFD"); 390 391 /** 392 * iommufd_device_unbind - Undo iommufd_device_bind() 393 * @idev: Device returned by iommufd_device_bind() 394 * 395 * Release the device from iommufd control. The DMA ownership will return back 396 * to unowned with DMA controlled by the DMA API. This invalidates the 397 * iommufd_device pointer, other APIs that consume it must not be called 398 * concurrently. 399 */ 400 void iommufd_device_unbind(struct iommufd_device *idev) 401 { 402 iommufd_object_destroy_user(idev->ictx, &idev->obj); 403 } 404 EXPORT_SYMBOL_NS_GPL(iommufd_device_unbind, "IOMMUFD"); 405 406 struct iommufd_ctx *iommufd_device_to_ictx(struct iommufd_device *idev) 407 { 408 return idev->ictx; 409 } 410 EXPORT_SYMBOL_NS_GPL(iommufd_device_to_ictx, "IOMMUFD"); 411 412 u32 iommufd_device_to_id(struct iommufd_device *idev) 413 { 414 return idev->obj.id; 415 } 416 EXPORT_SYMBOL_NS_GPL(iommufd_device_to_id, "IOMMUFD"); 417 418 static unsigned int iommufd_group_device_num(struct iommufd_group *igroup, 419 ioasid_t pasid) 420 { 421 struct iommufd_attach *attach; 422 struct iommufd_device *idev; 423 unsigned int count = 0; 424 unsigned long index; 425 426 lockdep_assert_held(&igroup->lock); 427 428 attach = xa_load(&igroup->pasid_attach, pasid); 429 if (attach) 430 xa_for_each(&attach->device_array, index, idev) 431 count++; 432 return count; 433 } 434 435 #ifdef CONFIG_IRQ_MSI_IOMMU 436 static int iommufd_group_setup_msi(struct iommufd_group *igroup, 437 struct iommufd_hwpt_paging *hwpt_paging) 438 { 439 struct iommufd_ctx *ictx = igroup->ictx; 440 struct iommufd_sw_msi_map *cur; 441 442 if (igroup->sw_msi_start == PHYS_ADDR_MAX) 443 return 0; 444 445 /* 446 * Install all the MSI pages the device has been using into the domain 447 */ 448 guard(mutex)(&ictx->sw_msi_lock); 449 list_for_each_entry(cur, &ictx->sw_msi_list, sw_msi_item) { 450 int rc; 451 452 if (cur->sw_msi_start != igroup->sw_msi_start || 453 !test_bit(cur->id, igroup->required_sw_msi.bitmap)) 454 continue; 455 456 rc = iommufd_sw_msi_install(ictx, hwpt_paging, cur); 457 if (rc) 458 return rc; 459 } 460 return 0; 461 } 462 #else 463 static inline int 464 iommufd_group_setup_msi(struct iommufd_group *igroup, 465 struct iommufd_hwpt_paging *hwpt_paging) 466 { 467 return 0; 468 } 469 #endif 470 471 static bool 472 iommufd_group_first_attach(struct iommufd_group *igroup, ioasid_t pasid) 473 { 474 lockdep_assert_held(&igroup->lock); 475 return !xa_load(&igroup->pasid_attach, pasid); 476 } 477 478 static int 479 iommufd_device_attach_reserved_iova(struct iommufd_device *idev, 480 struct iommufd_hwpt_paging *hwpt_paging) 481 { 482 struct iommufd_group *igroup = idev->igroup; 483 int rc; 484 485 lockdep_assert_held(&igroup->lock); 486 487 rc = iopt_table_enforce_dev_resv_regions(&hwpt_paging->ioas->iopt, 488 idev->dev, 489 &igroup->sw_msi_start); 490 if (rc) 491 return rc; 492 493 if (iommufd_group_first_attach(igroup, IOMMU_NO_PASID)) { 494 rc = iommufd_group_setup_msi(igroup, hwpt_paging); 495 if (rc) { 496 iopt_remove_reserved_iova(&hwpt_paging->ioas->iopt, 497 idev->dev); 498 return rc; 499 } 500 } 501 return 0; 502 } 503 504 /* The device attach/detach/replace helpers for attach_handle */ 505 506 static bool iommufd_device_is_attached(struct iommufd_device *idev, 507 ioasid_t pasid) 508 { 509 struct iommufd_attach *attach; 510 511 attach = xa_load(&idev->igroup->pasid_attach, pasid); 512 return xa_load(&attach->device_array, idev->obj.id); 513 } 514 515 static int iommufd_hwpt_pasid_compat(struct iommufd_hw_pagetable *hwpt, 516 struct iommufd_device *idev, 517 ioasid_t pasid) 518 { 519 struct iommufd_group *igroup = idev->igroup; 520 521 lockdep_assert_held(&igroup->lock); 522 523 if (pasid == IOMMU_NO_PASID) { 524 unsigned long start = IOMMU_NO_PASID; 525 526 if (!hwpt->pasid_compat && 527 xa_find_after(&igroup->pasid_attach, 528 &start, UINT_MAX, XA_PRESENT)) 529 return -EINVAL; 530 } else { 531 struct iommufd_attach *attach; 532 533 if (!hwpt->pasid_compat) 534 return -EINVAL; 535 536 attach = xa_load(&igroup->pasid_attach, IOMMU_NO_PASID); 537 if (attach && attach->hwpt && !attach->hwpt->pasid_compat) 538 return -EINVAL; 539 } 540 541 return 0; 542 } 543 544 static bool iommufd_hwpt_compatible_device(struct iommufd_hw_pagetable *hwpt, 545 struct iommufd_device *idev) 546 { 547 struct pci_dev *pdev; 548 549 if (!hwpt->fault || !dev_is_pci(idev->dev)) 550 return true; 551 552 /* 553 * Once we turn on PCI/PRI support for VF, the response failure code 554 * should not be forwarded to the hardware due to PRI being a shared 555 * resource between PF and VFs. There is no coordination for this 556 * shared capability. This waits for a vPRI reset to recover. 557 */ 558 pdev = to_pci_dev(idev->dev); 559 560 return (!pdev->is_virtfn || !pci_pri_supported(pdev)); 561 } 562 563 static int iommufd_hwpt_attach_device(struct iommufd_hw_pagetable *hwpt, 564 struct iommufd_device *idev, 565 ioasid_t pasid) 566 { 567 struct iommufd_attach_handle *handle; 568 int rc; 569 570 if (!iommufd_hwpt_compatible_device(hwpt, idev)) 571 return -EINVAL; 572 573 rc = iommufd_hwpt_pasid_compat(hwpt, idev, pasid); 574 if (rc) 575 return rc; 576 577 if (iommufd_device_is_noiommu(idev)) 578 return 0; 579 580 handle = kzalloc_obj(*handle); 581 if (!handle) 582 return -ENOMEM; 583 584 handle->idev = idev; 585 if (pasid == IOMMU_NO_PASID) 586 rc = iommu_attach_group_handle(hwpt->domain, idev->igroup->group, 587 &handle->handle); 588 else 589 rc = iommu_attach_device_pasid(hwpt->domain, idev->dev, pasid, 590 &handle->handle); 591 if (rc) 592 goto out_free_handle; 593 594 return 0; 595 596 out_free_handle: 597 kfree(handle); 598 return rc; 599 } 600 601 static struct iommufd_attach_handle * 602 iommufd_device_get_attach_handle(struct iommufd_device *idev, ioasid_t pasid) 603 { 604 struct iommu_attach_handle *handle; 605 606 lockdep_assert_held(&idev->igroup->lock); 607 608 handle = iommu_attach_handle_get(idev->igroup->group, pasid, 0); 609 if (IS_ERR(handle)) 610 return NULL; 611 return to_iommufd_handle(handle); 612 } 613 614 static void iommufd_hwpt_detach_device(struct iommufd_hw_pagetable *hwpt, 615 struct iommufd_device *idev, 616 ioasid_t pasid) 617 { 618 struct iommufd_attach_handle *handle; 619 620 if (iommufd_device_is_noiommu(idev)) 621 return; 622 623 handle = iommufd_device_get_attach_handle(idev, pasid); 624 if (pasid == IOMMU_NO_PASID) 625 iommu_detach_group_handle(hwpt->domain, idev->igroup->group); 626 else 627 iommu_detach_device_pasid(hwpt->domain, idev->dev, pasid); 628 629 iommufd_auto_response_faults(hwpt, handle); 630 kfree(handle); 631 } 632 633 static int iommufd_hwpt_replace_device(struct iommufd_device *idev, 634 ioasid_t pasid, 635 struct iommufd_hw_pagetable *hwpt, 636 struct iommufd_hw_pagetable *old) 637 { 638 struct iommufd_attach_handle *handle, *old_handle; 639 int rc; 640 641 if (!iommufd_hwpt_compatible_device(hwpt, idev)) 642 return -EINVAL; 643 644 rc = iommufd_hwpt_pasid_compat(hwpt, idev, pasid); 645 if (rc) 646 return rc; 647 648 if (iommufd_device_is_noiommu(idev)) 649 return 0; 650 651 old_handle = iommufd_device_get_attach_handle(idev, pasid); 652 653 handle = kzalloc_obj(*handle); 654 if (!handle) 655 return -ENOMEM; 656 657 handle->idev = idev; 658 if (pasid == IOMMU_NO_PASID) 659 rc = iommu_replace_group_handle(idev->igroup->group, 660 hwpt->domain, &handle->handle); 661 else 662 rc = iommu_replace_device_pasid(hwpt->domain, idev->dev, 663 pasid, &handle->handle); 664 if (rc) 665 goto out_free_handle; 666 667 iommufd_auto_response_faults(old, old_handle); 668 kfree(old_handle); 669 670 return 0; 671 672 out_free_handle: 673 kfree(handle); 674 return rc; 675 } 676 677 int iommufd_hw_pagetable_attach(struct iommufd_hw_pagetable *hwpt, 678 struct iommufd_device *idev, ioasid_t pasid) 679 { 680 struct iommufd_hwpt_paging *hwpt_paging = find_hwpt_paging(hwpt); 681 bool attach_resv = hwpt_paging && pasid == IOMMU_NO_PASID && 682 !iommufd_device_is_noiommu(idev); 683 struct iommufd_group *igroup = idev->igroup; 684 struct iommufd_hw_pagetable *old_hwpt; 685 struct iommufd_attach *attach; 686 int rc; 687 688 mutex_lock(&igroup->lock); 689 690 attach = xa_cmpxchg(&igroup->pasid_attach, pasid, NULL, 691 XA_ZERO_ENTRY, GFP_KERNEL); 692 if (xa_is_err(attach)) { 693 rc = xa_err(attach); 694 goto err_unlock; 695 } 696 697 if (!attach) { 698 attach = kzalloc_obj(*attach); 699 if (!attach) { 700 rc = -ENOMEM; 701 goto err_release_pasid; 702 } 703 xa_init(&attach->device_array); 704 } 705 706 old_hwpt = attach->hwpt; 707 708 rc = xa_insert(&attach->device_array, idev->obj.id, XA_ZERO_ENTRY, 709 GFP_KERNEL); 710 if (rc) { 711 WARN_ON(rc == -EBUSY && !old_hwpt); 712 goto err_free_attach; 713 } 714 715 if (old_hwpt && old_hwpt != hwpt) { 716 rc = -EINVAL; 717 goto err_release_devid; 718 } 719 720 if (attach_resv) { 721 rc = iommufd_device_attach_reserved_iova(idev, hwpt_paging); 722 if (rc) 723 goto err_release_devid; 724 } 725 726 /* 727 * Only attach to the group once for the first device that is in the 728 * group. All the other devices will follow this attachment. The user 729 * should attach every device individually to the hwpt as the per-device 730 * reserved regions are only updated during individual device 731 * attachment. 732 */ 733 if (iommufd_group_first_attach(igroup, pasid)) { 734 rc = iommufd_hwpt_attach_device(hwpt, idev, pasid); 735 if (rc) 736 goto err_unresv; 737 attach->hwpt = hwpt; 738 WARN_ON(xa_is_err(xa_store(&igroup->pasid_attach, pasid, attach, 739 GFP_KERNEL))); 740 } 741 refcount_inc(&hwpt->obj.users); 742 WARN_ON(xa_is_err(xa_store(&attach->device_array, idev->obj.id, 743 idev, GFP_KERNEL))); 744 mutex_unlock(&igroup->lock); 745 return 0; 746 err_unresv: 747 if (attach_resv) 748 iopt_remove_reserved_iova(&hwpt_paging->ioas->iopt, idev->dev); 749 err_release_devid: 750 xa_release(&attach->device_array, idev->obj.id); 751 err_free_attach: 752 if (iommufd_group_first_attach(igroup, pasid)) 753 kfree(attach); 754 err_release_pasid: 755 if (iommufd_group_first_attach(igroup, pasid)) 756 xa_release(&igroup->pasid_attach, pasid); 757 err_unlock: 758 mutex_unlock(&igroup->lock); 759 return rc; 760 } 761 762 struct iommufd_hw_pagetable * 763 iommufd_hw_pagetable_detach(struct iommufd_device *idev, ioasid_t pasid) 764 { 765 struct iommufd_group *igroup = idev->igroup; 766 struct iommufd_hwpt_paging *hwpt_paging; 767 struct iommufd_hw_pagetable *hwpt; 768 struct iommufd_attach *attach; 769 770 mutex_lock(&igroup->lock); 771 attach = xa_load(&igroup->pasid_attach, pasid); 772 if (!attach) { 773 mutex_unlock(&igroup->lock); 774 return NULL; 775 } 776 777 hwpt = attach->hwpt; 778 hwpt_paging = find_hwpt_paging(hwpt); 779 780 xa_erase(&attach->device_array, idev->obj.id); 781 if (xa_empty(&attach->device_array)) { 782 iommufd_hwpt_detach_device(hwpt, idev, pasid); 783 xa_erase(&igroup->pasid_attach, pasid); 784 kfree(attach); 785 } 786 if (hwpt_paging && pasid == IOMMU_NO_PASID) 787 iopt_remove_reserved_iova(&hwpt_paging->ioas->iopt, idev->dev); 788 mutex_unlock(&igroup->lock); 789 790 iommufd_hw_pagetable_put(idev->ictx, hwpt); 791 792 /* Caller must destroy hwpt */ 793 return hwpt; 794 } 795 796 static struct iommufd_hw_pagetable * 797 iommufd_device_do_attach(struct iommufd_device *idev, ioasid_t pasid, 798 struct iommufd_hw_pagetable *hwpt) 799 { 800 int rc; 801 802 rc = iommufd_hw_pagetable_attach(hwpt, idev, pasid); 803 if (rc) 804 return ERR_PTR(rc); 805 return NULL; 806 } 807 808 static void 809 iommufd_group_remove_reserved_iova(struct iommufd_group *igroup, 810 struct iommufd_hwpt_paging *hwpt_paging) 811 { 812 struct iommufd_attach *attach; 813 struct iommufd_device *cur; 814 unsigned long index; 815 816 lockdep_assert_held(&igroup->lock); 817 818 attach = xa_load(&igroup->pasid_attach, IOMMU_NO_PASID); 819 xa_for_each(&attach->device_array, index, cur) 820 iopt_remove_reserved_iova(&hwpt_paging->ioas->iopt, cur->dev); 821 } 822 823 static int 824 iommufd_group_do_replace_reserved_iova(struct iommufd_group *igroup, 825 struct iommufd_hwpt_paging *hwpt_paging) 826 { 827 struct iommufd_hwpt_paging *old_hwpt_paging; 828 struct iommufd_attach *attach; 829 struct iommufd_device *cur; 830 unsigned long index; 831 int rc; 832 833 lockdep_assert_held(&igroup->lock); 834 835 attach = xa_load(&igroup->pasid_attach, IOMMU_NO_PASID); 836 old_hwpt_paging = find_hwpt_paging(attach->hwpt); 837 if (!old_hwpt_paging || hwpt_paging->ioas != old_hwpt_paging->ioas) { 838 xa_for_each(&attach->device_array, index, cur) { 839 rc = iopt_table_enforce_dev_resv_regions( 840 &hwpt_paging->ioas->iopt, cur->dev, NULL); 841 if (rc) 842 goto err_unresv; 843 } 844 } 845 846 rc = iommufd_group_setup_msi(igroup, hwpt_paging); 847 if (rc) 848 goto err_unresv; 849 return 0; 850 851 err_unresv: 852 iommufd_group_remove_reserved_iova(igroup, hwpt_paging); 853 return rc; 854 } 855 856 static struct iommufd_hw_pagetable * 857 iommufd_device_do_replace(struct iommufd_device *idev, ioasid_t pasid, 858 struct iommufd_hw_pagetable *hwpt) 859 { 860 struct iommufd_hwpt_paging *hwpt_paging = find_hwpt_paging(hwpt); 861 bool attach_resv = hwpt_paging && pasid == IOMMU_NO_PASID && 862 !iommufd_device_is_noiommu(idev); 863 struct iommufd_hwpt_paging *old_hwpt_paging; 864 struct iommufd_group *igroup = idev->igroup; 865 struct iommufd_hw_pagetable *old_hwpt; 866 struct iommufd_attach *attach; 867 unsigned int num_devices; 868 int rc; 869 870 mutex_lock(&igroup->lock); 871 872 attach = xa_load(&igroup->pasid_attach, pasid); 873 if (!attach) { 874 rc = -EINVAL; 875 goto err_unlock; 876 } 877 878 old_hwpt = attach->hwpt; 879 880 WARN_ON(!old_hwpt || xa_empty(&attach->device_array)); 881 882 if (!iommufd_device_is_attached(idev, pasid)) { 883 rc = -EINVAL; 884 goto err_unlock; 885 } 886 887 if (hwpt == old_hwpt) { 888 mutex_unlock(&igroup->lock); 889 return NULL; 890 } 891 892 if (attach_resv) { 893 rc = iommufd_group_do_replace_reserved_iova(igroup, hwpt_paging); 894 if (rc) 895 goto err_unlock; 896 } 897 898 rc = iommufd_hwpt_replace_device(idev, pasid, hwpt, old_hwpt); 899 if (rc) 900 goto err_unresv; 901 902 old_hwpt_paging = find_hwpt_paging(old_hwpt); 903 if (old_hwpt_paging && pasid == IOMMU_NO_PASID && 904 (!hwpt_paging || hwpt_paging->ioas != old_hwpt_paging->ioas)) 905 iommufd_group_remove_reserved_iova(igroup, old_hwpt_paging); 906 907 attach->hwpt = hwpt; 908 909 num_devices = iommufd_group_device_num(igroup, pasid); 910 /* 911 * Move the refcounts held by the device_array to the new hwpt. Retain a 912 * refcount for this thread as the caller will free it. 913 */ 914 refcount_add(num_devices, &hwpt->obj.users); 915 if (num_devices > 1) 916 WARN_ON(refcount_sub_and_test(num_devices - 1, 917 &old_hwpt->obj.users)); 918 mutex_unlock(&igroup->lock); 919 920 /* Caller must destroy old_hwpt */ 921 return old_hwpt; 922 err_unresv: 923 if (attach_resv) 924 iommufd_group_remove_reserved_iova(igroup, hwpt_paging); 925 err_unlock: 926 mutex_unlock(&igroup->lock); 927 return ERR_PTR(rc); 928 } 929 930 typedef struct iommufd_hw_pagetable *(*attach_fn)( 931 struct iommufd_device *idev, ioasid_t pasid, 932 struct iommufd_hw_pagetable *hwpt); 933 934 /* 935 * When automatically managing the domains we search for a compatible domain in 936 * the iopt and if one is found use it, otherwise create a new domain. 937 * Automatic domain selection will never pick a manually created domain. 938 */ 939 static struct iommufd_hw_pagetable * 940 iommufd_device_auto_get_domain(struct iommufd_device *idev, ioasid_t pasid, 941 struct iommufd_ioas *ioas, u32 *pt_id, 942 attach_fn do_attach) 943 { 944 /* 945 * iommufd_hw_pagetable_attach() is called by 946 * iommufd_hwpt_paging_alloc() in immediate attachment mode, same as 947 * iommufd_device_do_attach(). So if we are in this mode then we prefer 948 * to use the immediate_attach path as it supports drivers that can't 949 * directly allocate a domain. 950 */ 951 bool immediate_attach = do_attach == iommufd_device_do_attach; 952 struct iommufd_hw_pagetable *destroy_hwpt; 953 struct iommufd_hwpt_paging *hwpt_paging; 954 struct iommufd_hw_pagetable *hwpt; 955 956 /* 957 * There is no differentiation when domains are allocated, so any domain 958 * that is willing to attach to the device is interchangeable with any 959 * other. 960 */ 961 mutex_lock(&ioas->mutex); 962 list_for_each_entry(hwpt_paging, &ioas->hwpt_list, hwpt_item) { 963 if (!hwpt_paging->auto_domain) 964 continue; 965 966 hwpt = &hwpt_paging->common; 967 if (!iommufd_lock_obj(&hwpt->obj)) 968 continue; 969 destroy_hwpt = (*do_attach)(idev, pasid, hwpt); 970 if (IS_ERR(destroy_hwpt)) { 971 iommufd_put_object(idev->ictx, &hwpt->obj); 972 /* 973 * -EINVAL means the domain is incompatible with the 974 * device. Other error codes should propagate to 975 * userspace as failure. Success means the domain is 976 * attached. 977 */ 978 if (PTR_ERR(destroy_hwpt) == -EINVAL) 979 continue; 980 goto out_unlock; 981 } 982 *pt_id = hwpt->obj.id; 983 iommufd_put_object(idev->ictx, &hwpt->obj); 984 goto out_unlock; 985 } 986 987 hwpt_paging = iommufd_hwpt_paging_alloc(idev->ictx, ioas, idev, pasid, 988 0, immediate_attach, NULL); 989 if (IS_ERR(hwpt_paging)) { 990 destroy_hwpt = ERR_CAST(hwpt_paging); 991 goto out_unlock; 992 } 993 hwpt = &hwpt_paging->common; 994 995 if (!immediate_attach) { 996 destroy_hwpt = (*do_attach)(idev, pasid, hwpt); 997 if (IS_ERR(destroy_hwpt)) 998 goto out_abort; 999 } else { 1000 destroy_hwpt = NULL; 1001 } 1002 1003 hwpt_paging->auto_domain = true; 1004 *pt_id = hwpt->obj.id; 1005 1006 iommufd_object_finalize(idev->ictx, &hwpt->obj); 1007 mutex_unlock(&ioas->mutex); 1008 return destroy_hwpt; 1009 1010 out_abort: 1011 iommufd_object_abort_and_destroy(idev->ictx, &hwpt->obj); 1012 out_unlock: 1013 mutex_unlock(&ioas->mutex); 1014 return destroy_hwpt; 1015 } 1016 1017 static int iommufd_device_change_pt(struct iommufd_device *idev, 1018 ioasid_t pasid, 1019 u32 *pt_id, attach_fn do_attach) 1020 { 1021 struct iommufd_hw_pagetable *destroy_hwpt; 1022 struct iommufd_object *pt_obj; 1023 1024 pt_obj = iommufd_get_object(idev->ictx, *pt_id, IOMMUFD_OBJ_ANY); 1025 if (IS_ERR(pt_obj)) 1026 return PTR_ERR(pt_obj); 1027 1028 switch (pt_obj->type) { 1029 case IOMMUFD_OBJ_HWPT_NESTED: 1030 case IOMMUFD_OBJ_HWPT_PAGING: { 1031 struct iommufd_hw_pagetable *hwpt = 1032 container_of(pt_obj, struct iommufd_hw_pagetable, obj); 1033 1034 destroy_hwpt = (*do_attach)(idev, pasid, hwpt); 1035 if (IS_ERR(destroy_hwpt)) 1036 goto out_put_pt_obj; 1037 break; 1038 } 1039 case IOMMUFD_OBJ_IOAS: { 1040 struct iommufd_ioas *ioas = 1041 container_of(pt_obj, struct iommufd_ioas, obj); 1042 1043 destroy_hwpt = iommufd_device_auto_get_domain(idev, pasid, ioas, 1044 pt_id, do_attach); 1045 if (IS_ERR(destroy_hwpt)) 1046 goto out_put_pt_obj; 1047 break; 1048 } 1049 default: 1050 destroy_hwpt = ERR_PTR(-EINVAL); 1051 goto out_put_pt_obj; 1052 } 1053 iommufd_put_object(idev->ictx, pt_obj); 1054 1055 /* This destruction has to be after we unlock everything */ 1056 if (destroy_hwpt) 1057 iommufd_hw_pagetable_put(idev->ictx, destroy_hwpt); 1058 return 0; 1059 1060 out_put_pt_obj: 1061 iommufd_put_object(idev->ictx, pt_obj); 1062 return PTR_ERR(destroy_hwpt); 1063 } 1064 1065 /** 1066 * iommufd_device_attach - Connect a device/pasid to an iommu_domain 1067 * @idev: device to attach 1068 * @pasid: pasid to attach 1069 * @pt_id: Input an IOMMUFD_OBJ_IOAS, or IOMMUFD_OBJ_HWPT_PAGING 1070 * Output the IOMMUFD_OBJ_HWPT_PAGING ID 1071 * 1072 * This connects the device/pasid to an iommu_domain, either automatically 1073 * or manually selected. Once this completes the device could do DMA with 1074 * @pasid. @pasid is IOMMU_NO_PASID if this attach is for no pasid usage. 1075 * 1076 * The caller should return the resulting pt_id back to userspace. 1077 * This function is undone by calling iommufd_device_detach(). 1078 */ 1079 int iommufd_device_attach(struct iommufd_device *idev, ioasid_t pasid, 1080 u32 *pt_id) 1081 { 1082 int rc; 1083 1084 rc = iommufd_device_change_pt(idev, pasid, pt_id, 1085 &iommufd_device_do_attach); 1086 if (rc) 1087 return rc; 1088 1089 /* 1090 * Pairs with iommufd_device_detach() - catches caller bugs attempting 1091 * to destroy a device with an attachment. 1092 */ 1093 refcount_inc(&idev->obj.users); 1094 return 0; 1095 } 1096 EXPORT_SYMBOL_NS_GPL(iommufd_device_attach, "IOMMUFD"); 1097 1098 /** 1099 * iommufd_device_replace - Change the device/pasid's iommu_domain 1100 * @idev: device to change 1101 * @pasid: pasid to change 1102 * @pt_id: Input an IOMMUFD_OBJ_IOAS, or IOMMUFD_OBJ_HWPT_PAGING 1103 * Output the IOMMUFD_OBJ_HWPT_PAGING ID 1104 * 1105 * This is the same as:: 1106 * 1107 * iommufd_device_detach(); 1108 * iommufd_device_attach(); 1109 * 1110 * If it fails then no change is made to the attachment. The iommu driver may 1111 * implement this so there is no disruption in translation. This can only be 1112 * called if iommufd_device_attach() has already succeeded. @pasid is 1113 * IOMMU_NO_PASID for no pasid usage. 1114 */ 1115 int iommufd_device_replace(struct iommufd_device *idev, ioasid_t pasid, 1116 u32 *pt_id) 1117 { 1118 return iommufd_device_change_pt(idev, pasid, pt_id, 1119 &iommufd_device_do_replace); 1120 } 1121 EXPORT_SYMBOL_NS_GPL(iommufd_device_replace, "IOMMUFD"); 1122 1123 /** 1124 * iommufd_device_detach - Disconnect a device/pasid from an iommu_domain 1125 * @idev: device to detach 1126 * @pasid: pasid to detach 1127 * 1128 * Undo iommufd_device_attach(). This disconnects the idev from the previously 1129 * attached pt_id. The device returns back to a blocked DMA translation. 1130 * @pasid is IOMMU_NO_PASID for no pasid usage. 1131 */ 1132 void iommufd_device_detach(struct iommufd_device *idev, ioasid_t pasid) 1133 { 1134 struct iommufd_hw_pagetable *hwpt; 1135 1136 hwpt = iommufd_hw_pagetable_detach(idev, pasid); 1137 if (!hwpt) 1138 return; 1139 refcount_dec(&idev->obj.users); 1140 } 1141 EXPORT_SYMBOL_NS_GPL(iommufd_device_detach, "IOMMUFD"); 1142 1143 /* 1144 * On success, it will refcount_inc() at a valid new_ioas and refcount_dec() at 1145 * a valid cur_ioas (access->ioas). A caller passing in a valid new_ioas should 1146 * call iommufd_put_object() if it does an iommufd_get_object() for a new_ioas. 1147 */ 1148 static int iommufd_access_change_ioas(struct iommufd_access *access, 1149 struct iommufd_ioas *new_ioas) 1150 { 1151 u32 iopt_access_list_id = access->iopt_access_list_id; 1152 struct iommufd_ioas *cur_ioas = access->ioas; 1153 int rc; 1154 1155 lockdep_assert_held(&access->ioas_lock); 1156 1157 /* We are racing with a concurrent detach, bail */ 1158 if (cur_ioas != access->ioas_unpin) 1159 return -EBUSY; 1160 1161 if (cur_ioas == new_ioas) 1162 return 0; 1163 1164 /* 1165 * Set ioas to NULL to block any further iommufd_access_pin_pages(). 1166 * iommufd_access_unpin_pages() can continue using access->ioas_unpin. 1167 */ 1168 access->ioas = NULL; 1169 1170 if (new_ioas) { 1171 rc = iopt_add_access(&new_ioas->iopt, access); 1172 if (rc) { 1173 access->ioas = cur_ioas; 1174 return rc; 1175 } 1176 refcount_inc(&new_ioas->obj.users); 1177 } 1178 1179 if (cur_ioas) { 1180 if (!iommufd_access_is_internal(access) && access->ops->unmap) { 1181 mutex_unlock(&access->ioas_lock); 1182 access->ops->unmap(access->data, 0, ULONG_MAX); 1183 mutex_lock(&access->ioas_lock); 1184 } 1185 iopt_remove_access(&cur_ioas->iopt, access, iopt_access_list_id); 1186 refcount_dec(&cur_ioas->obj.users); 1187 } 1188 1189 access->ioas = new_ioas; 1190 access->ioas_unpin = new_ioas; 1191 1192 return 0; 1193 } 1194 1195 static int iommufd_access_change_ioas_id(struct iommufd_access *access, u32 id) 1196 { 1197 struct iommufd_ioas *ioas = iommufd_get_ioas(access->ictx, id); 1198 int rc; 1199 1200 if (IS_ERR(ioas)) 1201 return PTR_ERR(ioas); 1202 rc = iommufd_access_change_ioas(access, ioas); 1203 iommufd_put_object(access->ictx, &ioas->obj); 1204 return rc; 1205 } 1206 1207 void iommufd_access_destroy_object(struct iommufd_object *obj) 1208 { 1209 struct iommufd_access *access = 1210 container_of(obj, struct iommufd_access, obj); 1211 1212 mutex_lock(&access->ioas_lock); 1213 if (access->ioas) 1214 WARN_ON(iommufd_access_change_ioas(access, NULL)); 1215 mutex_unlock(&access->ioas_lock); 1216 if (!iommufd_access_is_internal(access)) 1217 iommufd_ctx_put(access->ictx); 1218 } 1219 1220 static struct iommufd_access *__iommufd_access_create(struct iommufd_ctx *ictx) 1221 { 1222 struct iommufd_access *access; 1223 1224 /* 1225 * There is no uAPI for the access object, but to keep things symmetric 1226 * use the object infrastructure anyhow. 1227 */ 1228 access = iommufd_object_alloc(ictx, access, IOMMUFD_OBJ_ACCESS); 1229 if (IS_ERR(access)) 1230 return access; 1231 1232 /* The calling driver is a user until iommufd_access_destroy() */ 1233 refcount_inc(&access->obj.users); 1234 mutex_init(&access->ioas_lock); 1235 return access; 1236 } 1237 1238 struct iommufd_access *iommufd_access_create_internal(struct iommufd_ctx *ictx) 1239 { 1240 struct iommufd_access *access; 1241 1242 access = __iommufd_access_create(ictx); 1243 if (IS_ERR(access)) 1244 return access; 1245 access->iova_alignment = PAGE_SIZE; 1246 1247 iommufd_object_finalize(ictx, &access->obj); 1248 return access; 1249 } 1250 1251 /** 1252 * iommufd_access_create - Create an iommufd_access 1253 * @ictx: iommufd file descriptor 1254 * @ops: Driver's ops to associate with the access 1255 * @data: Opaque data to pass into ops functions 1256 * @id: Output ID number to return to userspace for this access 1257 * 1258 * An iommufd_access allows a driver to read/write to the IOAS without using 1259 * DMA. The underlying CPU memory can be accessed using the 1260 * iommufd_access_pin_pages() or iommufd_access_rw() functions. 1261 * 1262 * The provided ops are required to use iommufd_access_pin_pages(). 1263 */ 1264 struct iommufd_access * 1265 iommufd_access_create(struct iommufd_ctx *ictx, 1266 const struct iommufd_access_ops *ops, void *data, u32 *id) 1267 { 1268 struct iommufd_access *access; 1269 1270 access = __iommufd_access_create(ictx); 1271 if (IS_ERR(access)) 1272 return access; 1273 1274 access->data = data; 1275 access->ops = ops; 1276 1277 if (ops->needs_pin_pages) 1278 access->iova_alignment = PAGE_SIZE; 1279 else 1280 access->iova_alignment = 1; 1281 1282 access->ictx = ictx; 1283 iommufd_ctx_get(ictx); 1284 iommufd_object_finalize(ictx, &access->obj); 1285 *id = access->obj.id; 1286 return access; 1287 } 1288 EXPORT_SYMBOL_NS_GPL(iommufd_access_create, "IOMMUFD"); 1289 1290 /** 1291 * iommufd_access_destroy - Destroy an iommufd_access 1292 * @access: The access to destroy 1293 * 1294 * The caller must stop using the access before destroying it. 1295 */ 1296 void iommufd_access_destroy(struct iommufd_access *access) 1297 { 1298 iommufd_object_destroy_user(access->ictx, &access->obj); 1299 } 1300 EXPORT_SYMBOL_NS_GPL(iommufd_access_destroy, "IOMMUFD"); 1301 1302 void iommufd_access_detach(struct iommufd_access *access) 1303 { 1304 mutex_lock(&access->ioas_lock); 1305 if (WARN_ON(!access->ioas)) { 1306 mutex_unlock(&access->ioas_lock); 1307 return; 1308 } 1309 WARN_ON(iommufd_access_change_ioas(access, NULL)); 1310 mutex_unlock(&access->ioas_lock); 1311 } 1312 EXPORT_SYMBOL_NS_GPL(iommufd_access_detach, "IOMMUFD"); 1313 1314 int iommufd_access_attach(struct iommufd_access *access, u32 ioas_id) 1315 { 1316 int rc; 1317 1318 mutex_lock(&access->ioas_lock); 1319 if (WARN_ON(access->ioas)) { 1320 mutex_unlock(&access->ioas_lock); 1321 return -EINVAL; 1322 } 1323 1324 rc = iommufd_access_change_ioas_id(access, ioas_id); 1325 mutex_unlock(&access->ioas_lock); 1326 return rc; 1327 } 1328 EXPORT_SYMBOL_NS_GPL(iommufd_access_attach, "IOMMUFD"); 1329 1330 int iommufd_access_attach_internal(struct iommufd_access *access, 1331 struct iommufd_ioas *ioas) 1332 { 1333 int rc; 1334 1335 mutex_lock(&access->ioas_lock); 1336 if (WARN_ON(access->ioas)) { 1337 mutex_unlock(&access->ioas_lock); 1338 return -EINVAL; 1339 } 1340 1341 rc = iommufd_access_change_ioas(access, ioas); 1342 mutex_unlock(&access->ioas_lock); 1343 return rc; 1344 } 1345 1346 int iommufd_access_replace(struct iommufd_access *access, u32 ioas_id) 1347 { 1348 int rc; 1349 1350 mutex_lock(&access->ioas_lock); 1351 if (!access->ioas) { 1352 mutex_unlock(&access->ioas_lock); 1353 return -ENOENT; 1354 } 1355 rc = iommufd_access_change_ioas_id(access, ioas_id); 1356 mutex_unlock(&access->ioas_lock); 1357 return rc; 1358 } 1359 EXPORT_SYMBOL_NS_GPL(iommufd_access_replace, "IOMMUFD"); 1360 1361 /** 1362 * iommufd_access_notify_unmap - Notify users of an iopt to stop using it 1363 * @iopt: iopt to work on 1364 * @iova: Starting iova in the iopt 1365 * @length: Number of bytes 1366 * 1367 * After this function returns there should be no users attached to the pages 1368 * linked to this iopt that intersect with iova,length. Anyone that has attached 1369 * a user through iopt_access_pages() needs to detach it through 1370 * iommufd_access_unpin_pages() before this function returns. 1371 * 1372 * iommufd_access_destroy() will wait for any outstanding unmap callback to 1373 * complete. Once iommufd_access_destroy() no unmap ops are running or will 1374 * run in the future. Due to this a driver must not create locking that prevents 1375 * unmap to complete while iommufd_access_destroy() is running. 1376 */ 1377 void iommufd_access_notify_unmap(struct io_pagetable *iopt, unsigned long iova, 1378 unsigned long length) 1379 { 1380 struct iommufd_ioas *ioas = 1381 container_of(iopt, struct iommufd_ioas, iopt); 1382 struct iommufd_access *access; 1383 unsigned long index; 1384 1385 xa_lock(&ioas->iopt.access_list); 1386 xa_for_each(&ioas->iopt.access_list, index, access) { 1387 if (iommufd_access_is_internal(access) || 1388 !iommufd_lock_obj(&access->obj)) 1389 continue; 1390 xa_unlock(&ioas->iopt.access_list); 1391 1392 access->ops->unmap(access->data, iova, length); 1393 1394 iommufd_put_object(access->ictx, &access->obj); 1395 xa_lock(&ioas->iopt.access_list); 1396 } 1397 xa_unlock(&ioas->iopt.access_list); 1398 } 1399 1400 /** 1401 * iommufd_access_unpin_pages() - Undo iommufd_access_pin_pages 1402 * @access: IOAS access to act on 1403 * @iova: Starting IOVA 1404 * @length: Number of bytes to access 1405 * 1406 * Return the struct page's. The caller must stop accessing them before calling 1407 * this. The iova/length must exactly match the one provided to access_pages. 1408 */ 1409 void iommufd_access_unpin_pages(struct iommufd_access *access, 1410 unsigned long iova, unsigned long length) 1411 { 1412 bool internal = iommufd_access_is_internal(access); 1413 struct iopt_area_contig_iter iter; 1414 struct io_pagetable *iopt; 1415 unsigned long last_iova; 1416 struct iopt_area *area; 1417 1418 if (WARN_ON(!length) || 1419 WARN_ON(check_add_overflow(iova, length - 1, &last_iova))) 1420 return; 1421 1422 mutex_lock(&access->ioas_lock); 1423 /* 1424 * The driver must be doing something wrong if it calls this before an 1425 * iommufd_access_attach() or after an iommufd_access_detach(). 1426 */ 1427 if (WARN_ON(!access->ioas_unpin)) { 1428 mutex_unlock(&access->ioas_lock); 1429 return; 1430 } 1431 iopt = &access->ioas_unpin->iopt; 1432 1433 down_read(&iopt->iova_rwsem); 1434 iopt_for_each_contig_area(&iter, area, iopt, iova, last_iova) 1435 iopt_area_remove_access( 1436 area, iopt_area_iova_to_index(area, iter.cur_iova), 1437 iopt_area_iova_to_index( 1438 area, 1439 min(last_iova, iopt_area_last_iova(area))), 1440 internal); 1441 WARN_ON(!iopt_area_contig_done(&iter)); 1442 up_read(&iopt->iova_rwsem); 1443 mutex_unlock(&access->ioas_lock); 1444 } 1445 EXPORT_SYMBOL_NS_GPL(iommufd_access_unpin_pages, "IOMMUFD"); 1446 1447 static bool iopt_area_contig_is_aligned(struct iopt_area_contig_iter *iter) 1448 { 1449 if (iopt_area_start_byte(iter->area, iter->cur_iova) % PAGE_SIZE) 1450 return false; 1451 1452 if (!iopt_area_contig_done(iter) && 1453 (iopt_area_start_byte(iter->area, iopt_area_last_iova(iter->area)) % 1454 PAGE_SIZE) != (PAGE_SIZE - 1)) 1455 return false; 1456 return true; 1457 } 1458 1459 static bool check_area_prot(struct iopt_area *area, unsigned int flags) 1460 { 1461 if (flags & IOMMUFD_ACCESS_RW_WRITE) 1462 return area->iommu_prot & IOMMU_WRITE; 1463 return area->iommu_prot & IOMMU_READ; 1464 } 1465 1466 /** 1467 * iommufd_access_pin_pages() - Return a list of pages under the iova 1468 * @access: IOAS access to act on 1469 * @iova: Starting IOVA 1470 * @length: Number of bytes to access 1471 * @out_pages: Output page list 1472 * @flags: IOPMMUFD_ACCESS_RW_* flags 1473 * 1474 * Reads @length bytes starting at iova and returns the struct page * pointers. 1475 * These can be kmap'd by the caller for CPU access. 1476 * 1477 * The caller must perform iommufd_access_unpin_pages() when done to balance 1478 * this. 1479 * 1480 * This API always requires a page aligned iova. This happens naturally if the 1481 * ioas alignment is >= PAGE_SIZE and the iova is PAGE_SIZE aligned. However 1482 * smaller alignments have corner cases where this API can fail on otherwise 1483 * aligned iova. 1484 */ 1485 int iommufd_access_pin_pages(struct iommufd_access *access, unsigned long iova, 1486 unsigned long length, struct page **out_pages, 1487 unsigned int flags) 1488 { 1489 bool internal = iommufd_access_is_internal(access); 1490 struct iopt_area_contig_iter iter; 1491 struct io_pagetable *iopt; 1492 unsigned long last_iova; 1493 struct iopt_area *area; 1494 int rc; 1495 1496 /* Driver's ops don't support pin_pages */ 1497 if (IS_ENABLED(CONFIG_IOMMUFD_TEST) && 1498 WARN_ON(access->iova_alignment != PAGE_SIZE || 1499 (!internal && !access->ops->unmap))) 1500 return -EINVAL; 1501 1502 if (!length) 1503 return -EINVAL; 1504 if (check_add_overflow(iova, length - 1, &last_iova)) 1505 return -EOVERFLOW; 1506 1507 mutex_lock(&access->ioas_lock); 1508 if (!access->ioas) { 1509 mutex_unlock(&access->ioas_lock); 1510 return -ENOENT; 1511 } 1512 iopt = &access->ioas->iopt; 1513 1514 down_read(&iopt->iova_rwsem); 1515 iopt_for_each_contig_area(&iter, area, iopt, iova, last_iova) { 1516 unsigned long last = min(last_iova, iopt_area_last_iova(area)); 1517 unsigned long last_index = iopt_area_iova_to_index(area, last); 1518 unsigned long index = 1519 iopt_area_iova_to_index(area, iter.cur_iova); 1520 1521 if (area->prevent_access || 1522 !iopt_area_contig_is_aligned(&iter)) { 1523 rc = -EINVAL; 1524 goto err_remove; 1525 } 1526 1527 if (!check_area_prot(area, flags)) { 1528 rc = -EPERM; 1529 goto err_remove; 1530 } 1531 1532 rc = iopt_area_add_access(area, index, last_index, out_pages, 1533 flags, internal); 1534 if (rc) 1535 goto err_remove; 1536 out_pages += last_index - index + 1; 1537 } 1538 if (!iopt_area_contig_done(&iter)) { 1539 rc = -ENOENT; 1540 goto err_remove; 1541 } 1542 1543 up_read(&iopt->iova_rwsem); 1544 mutex_unlock(&access->ioas_lock); 1545 return 0; 1546 1547 err_remove: 1548 if (iova < iter.cur_iova) { 1549 last_iova = iter.cur_iova - 1; 1550 iopt_for_each_contig_area(&iter, area, iopt, iova, last_iova) 1551 iopt_area_remove_access( 1552 area, 1553 iopt_area_iova_to_index(area, iter.cur_iova), 1554 iopt_area_iova_to_index( 1555 area, min(last_iova, 1556 iopt_area_last_iova(area))), 1557 internal); 1558 } 1559 up_read(&iopt->iova_rwsem); 1560 mutex_unlock(&access->ioas_lock); 1561 return rc; 1562 } 1563 EXPORT_SYMBOL_NS_GPL(iommufd_access_pin_pages, "IOMMUFD"); 1564 1565 /** 1566 * iommufd_access_rw - Read or write data under the iova 1567 * @access: IOAS access to act on 1568 * @iova: Starting IOVA 1569 * @data: Kernel buffer to copy to/from 1570 * @length: Number of bytes to access 1571 * @flags: IOMMUFD_ACCESS_RW_* flags 1572 * 1573 * Copy kernel to/from data into the range given by IOVA/length. If flags 1574 * indicates IOMMUFD_ACCESS_RW_KTHREAD then a large copy can be optimized 1575 * by changing it into copy_to/from_user(). 1576 */ 1577 int iommufd_access_rw(struct iommufd_access *access, unsigned long iova, 1578 void *data, size_t length, unsigned int flags) 1579 { 1580 struct iopt_area_contig_iter iter; 1581 struct io_pagetable *iopt; 1582 struct iopt_area *area; 1583 unsigned long last_iova; 1584 int rc = -EINVAL; 1585 1586 if (!length) 1587 return -EINVAL; 1588 if (check_add_overflow(iova, length - 1, &last_iova)) 1589 return -EOVERFLOW; 1590 1591 mutex_lock(&access->ioas_lock); 1592 if (!access->ioas) { 1593 mutex_unlock(&access->ioas_lock); 1594 return -ENOENT; 1595 } 1596 iopt = &access->ioas->iopt; 1597 1598 down_read(&iopt->iova_rwsem); 1599 iopt_for_each_contig_area(&iter, area, iopt, iova, last_iova) { 1600 unsigned long last = min(last_iova, iopt_area_last_iova(area)); 1601 unsigned long bytes = (last - iter.cur_iova) + 1; 1602 1603 if (area->prevent_access) { 1604 rc = -EINVAL; 1605 goto err_out; 1606 } 1607 1608 if (!check_area_prot(area, flags)) { 1609 rc = -EPERM; 1610 goto err_out; 1611 } 1612 1613 rc = iopt_pages_rw_access( 1614 area->pages, iopt_area_start_byte(area, iter.cur_iova), 1615 data, bytes, flags); 1616 if (rc) 1617 goto err_out; 1618 data += bytes; 1619 } 1620 if (!iopt_area_contig_done(&iter)) 1621 rc = -ENOENT; 1622 err_out: 1623 up_read(&iopt->iova_rwsem); 1624 mutex_unlock(&access->ioas_lock); 1625 return rc; 1626 } 1627 EXPORT_SYMBOL_NS_GPL(iommufd_access_rw, "IOMMUFD"); 1628 1629 int iommufd_get_hw_info(struct iommufd_ucmd *ucmd) 1630 { 1631 const u32 SUPPORTED_FLAGS = IOMMU_HW_INFO_FLAG_INPUT_TYPE; 1632 struct iommu_hw_info *cmd = ucmd->cmd; 1633 void __user *user_ptr = u64_to_user_ptr(cmd->data_uptr); 1634 const struct iommu_ops *ops; 1635 struct iommufd_device *idev; 1636 unsigned int data_len; 1637 unsigned int copy_len; 1638 void *data; 1639 int rc; 1640 1641 if (cmd->flags & ~SUPPORTED_FLAGS) 1642 return -EOPNOTSUPP; 1643 if (cmd->__reserved[0] || cmd->__reserved[1] || cmd->__reserved[2]) 1644 return -EOPNOTSUPP; 1645 1646 /* Clear the type field since drivers don't support a random input */ 1647 if (!(cmd->flags & IOMMU_HW_INFO_FLAG_INPUT_TYPE)) 1648 cmd->in_data_type = IOMMU_HW_INFO_TYPE_DEFAULT; 1649 1650 idev = iommufd_get_device(ucmd, cmd->dev_id); 1651 if (IS_ERR(idev)) 1652 return PTR_ERR(idev); 1653 1654 if (iommufd_device_is_noiommu(idev)) { 1655 rc = -EOPNOTSUPP; 1656 goto out_put; 1657 } 1658 1659 ops = dev_iommu_ops(idev->dev); 1660 if (ops->hw_info) { 1661 data = ops->hw_info(idev->dev, &data_len, &cmd->out_data_type); 1662 if (IS_ERR(data)) { 1663 rc = PTR_ERR(data); 1664 goto out_put; 1665 } 1666 1667 /* 1668 * drivers that have hw_info callback should have a unique 1669 * iommu_hw_info_type. 1670 */ 1671 if (WARN_ON_ONCE(cmd->out_data_type == 1672 IOMMU_HW_INFO_TYPE_NONE)) { 1673 rc = -EOPNOTSUPP; 1674 goto out_free; 1675 } 1676 } else { 1677 cmd->out_data_type = IOMMU_HW_INFO_TYPE_NONE; 1678 data_len = 0; 1679 data = NULL; 1680 } 1681 1682 copy_len = min(cmd->data_len, data_len); 1683 if (copy_to_user(user_ptr, data, copy_len)) { 1684 rc = -EFAULT; 1685 goto out_free; 1686 } 1687 1688 /* 1689 * Zero the trailing bytes if the user buffer is bigger than the 1690 * data size kernel actually has. 1691 */ 1692 if (copy_len < cmd->data_len) { 1693 if (clear_user(user_ptr + copy_len, cmd->data_len - copy_len)) { 1694 rc = -EFAULT; 1695 goto out_free; 1696 } 1697 } 1698 1699 /* 1700 * We return the length the kernel supports so userspace may know what 1701 * the kernel capability is. It could be larger than the input buffer. 1702 */ 1703 cmd->data_len = data_len; 1704 1705 cmd->out_capabilities = 0; 1706 if (device_iommu_capable(idev->dev, IOMMU_CAP_DIRTY_TRACKING)) 1707 cmd->out_capabilities |= IOMMU_HW_CAP_DIRTY_TRACKING; 1708 1709 /* Report when ATS cannot be used for this device */ 1710 if (!device_iommu_capable(idev->dev, IOMMU_CAP_PCI_ATS_SUPPORTED)) 1711 cmd->out_capabilities |= IOMMU_HW_CAP_PCI_ATS_NOT_SUPPORTED; 1712 1713 cmd->out_max_pasid_log2 = 0; 1714 /* 1715 * Currently, all iommu drivers enable PASID in the probe_device() 1716 * op if iommu and device supports it. So the max_pasids stored in 1717 * dev->iommu indicates both PASID support and enable status. A 1718 * non-zero dev->iommu->max_pasids means PASID is supported and 1719 * enabled. The iommufd only reports PASID capability to userspace 1720 * if it's enabled. 1721 */ 1722 if (idev->dev->iommu->max_pasids) { 1723 cmd->out_max_pasid_log2 = ilog2(idev->dev->iommu->max_pasids); 1724 1725 if (dev_is_pci(idev->dev)) { 1726 struct pci_dev *pdev = to_pci_dev(idev->dev); 1727 int ctrl; 1728 1729 ctrl = pci_pasid_status(pdev); 1730 1731 WARN_ON_ONCE(ctrl < 0 || 1732 !(ctrl & PCI_PASID_CTRL_ENABLE)); 1733 1734 if (ctrl & PCI_PASID_CTRL_EXEC) 1735 cmd->out_capabilities |= 1736 IOMMU_HW_CAP_PCI_PASID_EXEC; 1737 if (ctrl & PCI_PASID_CTRL_PRIV) 1738 cmd->out_capabilities |= 1739 IOMMU_HW_CAP_PCI_PASID_PRIV; 1740 } 1741 } 1742 1743 rc = iommufd_ucmd_respond(ucmd, sizeof(*cmd)); 1744 out_free: 1745 kfree(data); 1746 out_put: 1747 iommufd_put_object(ucmd->ictx, &idev->obj); 1748 return rc; 1749 } 1750