iommu-sva.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Helpers for IOMMU drivers implementing SVA
  4. */
  5. #include <linux/mmu_context.h>
  6. #include <linux/mmu_notifier.h>
  7. #include <linux/mutex.h>
  8. #include <linux/sched/mm.h>
  9. #include <linux/iommu.h>
  10. #include "iommu-priv.h"
  11. static DEFINE_MUTEX(iommu_sva_lock);
  12. static bool iommu_sva_present;
  13. static LIST_HEAD(iommu_sva_mms);
  14. static struct iommu_domain *iommu_sva_domain_alloc(struct device *dev,
  15. struct mm_struct *mm);
  16. /* Allocate a PASID for the mm within range (inclusive) */
  17. static struct iommu_mm_data *iommu_alloc_mm_data(struct mm_struct *mm, struct device *dev)
  18. {
  19. struct iommu_mm_data *iommu_mm;
  20. ioasid_t pasid;
  21. lockdep_assert_held(&iommu_sva_lock);
  22. if (!arch_pgtable_dma_compat(mm))
  23. return ERR_PTR(-EBUSY);
  24. iommu_mm = mm->iommu_mm;
  25. /* Is a PASID already associated with this mm? */
  26. if (iommu_mm) {
  27. if (iommu_mm->pasid >= dev->iommu->max_pasids)
  28. return ERR_PTR(-EOVERFLOW);
  29. return iommu_mm;
  30. }
  31. iommu_mm = kzalloc_obj(struct iommu_mm_data);
  32. if (!iommu_mm)
  33. return ERR_PTR(-ENOMEM);
  34. pasid = iommu_alloc_global_pasid(dev);
  35. if (pasid == IOMMU_PASID_INVALID) {
  36. kfree(iommu_mm);
  37. return ERR_PTR(-ENOSPC);
  38. }
  39. iommu_mm->pasid = pasid;
  40. iommu_mm->mm = mm;
  41. INIT_LIST_HEAD(&iommu_mm->sva_domains);
  42. /*
  43. * Make sure the write to mm->iommu_mm is not reordered in front of
  44. * initialization to iommu_mm fields. If it does, readers may see a
  45. * valid iommu_mm with uninitialized values.
  46. */
  47. smp_store_release(&mm->iommu_mm, iommu_mm);
  48. return iommu_mm;
  49. }
  50. /**
  51. * iommu_sva_bind_device() - Bind a process address space to a device
  52. * @dev: the device
  53. * @mm: the mm to bind, caller must hold a reference to mm_users
  54. *
  55. * Create a bond between device and address space, allowing the device to
  56. * access the mm using the PASID returned by iommu_sva_get_pasid(). If a
  57. * bond already exists between @device and @mm, an additional internal
  58. * reference is taken. Caller must call iommu_sva_unbind_device()
  59. * to release each reference.
  60. *
  61. * On error, returns an ERR_PTR value.
  62. */
  63. struct iommu_sva *iommu_sva_bind_device(struct device *dev, struct mm_struct *mm)
  64. {
  65. struct iommu_group *group = dev->iommu_group;
  66. struct iommu_attach_handle *attach_handle;
  67. struct iommu_mm_data *iommu_mm;
  68. struct iommu_domain *domain;
  69. struct iommu_sva *handle;
  70. int ret;
  71. if (!group)
  72. return ERR_PTR(-ENODEV);
  73. mutex_lock(&iommu_sva_lock);
  74. /* Allocate mm->pasid if necessary. */
  75. iommu_mm = iommu_alloc_mm_data(mm, dev);
  76. if (IS_ERR(iommu_mm)) {
  77. ret = PTR_ERR(iommu_mm);
  78. goto out_unlock;
  79. }
  80. /* A bond already exists, just take a reference`. */
  81. attach_handle = iommu_attach_handle_get(group, iommu_mm->pasid, IOMMU_DOMAIN_SVA);
  82. if (!IS_ERR(attach_handle)) {
  83. handle = container_of(attach_handle, struct iommu_sva, handle);
  84. if (attach_handle->domain->mm != mm) {
  85. ret = -EBUSY;
  86. goto out_unlock;
  87. }
  88. refcount_inc(&handle->users);
  89. mutex_unlock(&iommu_sva_lock);
  90. return handle;
  91. }
  92. if (PTR_ERR(attach_handle) != -ENOENT) {
  93. ret = PTR_ERR(attach_handle);
  94. goto out_unlock;
  95. }
  96. handle = kzalloc_obj(*handle);
  97. if (!handle) {
  98. ret = -ENOMEM;
  99. goto out_unlock;
  100. }
  101. /* Search for an existing domain. */
  102. list_for_each_entry(domain, &mm->iommu_mm->sva_domains, next) {
  103. ret = iommu_attach_device_pasid(domain, dev, iommu_mm->pasid,
  104. &handle->handle);
  105. if (!ret) {
  106. domain->users++;
  107. goto out;
  108. }
  109. }
  110. /* Allocate a new domain and set it on device pasid. */
  111. domain = iommu_sva_domain_alloc(dev, mm);
  112. if (IS_ERR(domain)) {
  113. ret = PTR_ERR(domain);
  114. goto out_free_handle;
  115. }
  116. ret = iommu_attach_device_pasid(domain, dev, iommu_mm->pasid,
  117. &handle->handle);
  118. if (ret)
  119. goto out_free_domain;
  120. domain->users = 1;
  121. if (list_empty(&iommu_mm->sva_domains)) {
  122. if (list_empty(&iommu_sva_mms))
  123. iommu_sva_present = true;
  124. list_add(&iommu_mm->mm_list_elm, &iommu_sva_mms);
  125. }
  126. list_add(&domain->next, &iommu_mm->sva_domains);
  127. out:
  128. refcount_set(&handle->users, 1);
  129. mutex_unlock(&iommu_sva_lock);
  130. handle->dev = dev;
  131. return handle;
  132. out_free_domain:
  133. iommu_domain_free(domain);
  134. out_free_handle:
  135. kfree(handle);
  136. out_unlock:
  137. mutex_unlock(&iommu_sva_lock);
  138. return ERR_PTR(ret);
  139. }
  140. EXPORT_SYMBOL_GPL(iommu_sva_bind_device);
  141. /**
  142. * iommu_sva_unbind_device() - Remove a bond created with iommu_sva_bind_device
  143. * @handle: the handle returned by iommu_sva_bind_device()
  144. *
  145. * Put reference to a bond between device and address space. The device should
  146. * not be issuing any more transaction for this PASID. All outstanding page
  147. * requests for this PASID must have been flushed to the IOMMU.
  148. */
  149. void iommu_sva_unbind_device(struct iommu_sva *handle)
  150. {
  151. struct iommu_domain *domain = handle->handle.domain;
  152. struct iommu_mm_data *iommu_mm = domain->mm->iommu_mm;
  153. struct device *dev = handle->dev;
  154. mutex_lock(&iommu_sva_lock);
  155. if (!refcount_dec_and_test(&handle->users)) {
  156. mutex_unlock(&iommu_sva_lock);
  157. return;
  158. }
  159. iommu_detach_device_pasid(domain, dev, iommu_mm->pasid);
  160. if (--domain->users == 0) {
  161. list_del(&domain->next);
  162. if (list_empty(&iommu_mm->sva_domains)) {
  163. list_del(&iommu_mm->mm_list_elm);
  164. if (list_empty(&iommu_sva_mms))
  165. iommu_sva_present = false;
  166. }
  167. iommu_domain_free(domain);
  168. }
  169. mutex_unlock(&iommu_sva_lock);
  170. kfree(handle);
  171. }
  172. EXPORT_SYMBOL_GPL(iommu_sva_unbind_device);
  173. u32 iommu_sva_get_pasid(struct iommu_sva *handle)
  174. {
  175. struct iommu_domain *domain = handle->handle.domain;
  176. return mm_get_enqcmd_pasid(domain->mm);
  177. }
  178. EXPORT_SYMBOL_GPL(iommu_sva_get_pasid);
  179. void mm_pasid_drop(struct mm_struct *mm)
  180. {
  181. struct iommu_mm_data *iommu_mm = mm->iommu_mm;
  182. if (!iommu_mm)
  183. return;
  184. iommu_free_global_pasid(iommu_mm->pasid);
  185. kfree(iommu_mm);
  186. }
  187. /*
  188. * I/O page fault handler for SVA
  189. */
  190. static enum iommu_page_response_code
  191. iommu_sva_handle_mm(struct iommu_fault *fault, struct mm_struct *mm)
  192. {
  193. vm_fault_t ret;
  194. struct vm_area_struct *vma;
  195. unsigned int access_flags = 0;
  196. unsigned int fault_flags = FAULT_FLAG_REMOTE;
  197. struct iommu_fault_page_request *prm = &fault->prm;
  198. enum iommu_page_response_code status = IOMMU_PAGE_RESP_INVALID;
  199. if (!(prm->flags & IOMMU_FAULT_PAGE_REQUEST_PASID_VALID))
  200. return status;
  201. if (!mmget_not_zero(mm))
  202. return status;
  203. mmap_read_lock(mm);
  204. vma = vma_lookup(mm, prm->addr);
  205. if (!vma)
  206. /* Unmapped area */
  207. goto out_put_mm;
  208. if (prm->perm & IOMMU_FAULT_PERM_READ)
  209. access_flags |= VM_READ;
  210. if (prm->perm & IOMMU_FAULT_PERM_WRITE) {
  211. access_flags |= VM_WRITE;
  212. fault_flags |= FAULT_FLAG_WRITE;
  213. }
  214. if (prm->perm & IOMMU_FAULT_PERM_EXEC) {
  215. access_flags |= VM_EXEC;
  216. fault_flags |= FAULT_FLAG_INSTRUCTION;
  217. }
  218. if (!(prm->perm & IOMMU_FAULT_PERM_PRIV))
  219. fault_flags |= FAULT_FLAG_USER;
  220. if (access_flags & ~vma->vm_flags)
  221. /* Access fault */
  222. goto out_put_mm;
  223. ret = handle_mm_fault(vma, prm->addr, fault_flags, NULL);
  224. status = ret & VM_FAULT_ERROR ? IOMMU_PAGE_RESP_INVALID :
  225. IOMMU_PAGE_RESP_SUCCESS;
  226. out_put_mm:
  227. mmap_read_unlock(mm);
  228. mmput(mm);
  229. return status;
  230. }
  231. static void iommu_sva_handle_iopf(struct work_struct *work)
  232. {
  233. struct iopf_fault *iopf;
  234. struct iopf_group *group;
  235. enum iommu_page_response_code status = IOMMU_PAGE_RESP_SUCCESS;
  236. group = container_of(work, struct iopf_group, work);
  237. list_for_each_entry(iopf, &group->faults, list) {
  238. /*
  239. * For the moment, errors are sticky: don't handle subsequent
  240. * faults in the group if there is an error.
  241. */
  242. if (status != IOMMU_PAGE_RESP_SUCCESS)
  243. break;
  244. status = iommu_sva_handle_mm(&iopf->fault,
  245. group->attach_handle->domain->mm);
  246. }
  247. iopf_group_response(group, status);
  248. iopf_free_group(group);
  249. }
  250. static int iommu_sva_iopf_handler(struct iopf_group *group)
  251. {
  252. struct iommu_fault_param *fault_param = group->fault_param;
  253. INIT_WORK(&group->work, iommu_sva_handle_iopf);
  254. if (!queue_work(fault_param->queue->wq, &group->work))
  255. return -EBUSY;
  256. return 0;
  257. }
  258. static struct iommu_domain *iommu_sva_domain_alloc(struct device *dev,
  259. struct mm_struct *mm)
  260. {
  261. const struct iommu_ops *ops = dev_iommu_ops(dev);
  262. struct iommu_domain *domain;
  263. if (!ops->domain_alloc_sva)
  264. return ERR_PTR(-EOPNOTSUPP);
  265. domain = ops->domain_alloc_sva(dev, mm);
  266. if (IS_ERR(domain))
  267. return domain;
  268. domain->type = IOMMU_DOMAIN_SVA;
  269. domain->cookie_type = IOMMU_COOKIE_SVA;
  270. mmgrab(mm);
  271. domain->mm = mm;
  272. domain->owner = ops;
  273. domain->iopf_handler = iommu_sva_iopf_handler;
  274. return domain;
  275. }
  276. void iommu_sva_invalidate_kva_range(unsigned long start, unsigned long end)
  277. {
  278. struct iommu_mm_data *iommu_mm;
  279. guard(mutex)(&iommu_sva_lock);
  280. if (!iommu_sva_present)
  281. return;
  282. list_for_each_entry(iommu_mm, &iommu_sva_mms, mm_list_elm)
  283. mmu_notifier_arch_invalidate_secondary_tlbs(iommu_mm->mm, start, end);
  284. }