device.c 34 KB

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