iommu_api.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * IOMMU helpers in MMU context.
  4. *
  5. * Copyright (C) 2015 IBM Corp. <[email protected]>
  6. */
  7. #include <linux/sched/signal.h>
  8. #include <linux/slab.h>
  9. #include <linux/rculist.h>
  10. #include <linux/vmalloc.h>
  11. #include <linux/mutex.h>
  12. #include <linux/migrate.h>
  13. #include <linux/hugetlb.h>
  14. #include <linux/swap.h>
  15. #include <linux/sizes.h>
  16. #include <linux/mm.h>
  17. #include <asm/mmu_context.h>
  18. #include <asm/pte-walk.h>
  19. #include <linux/mm_inline.h>
  20. static DEFINE_MUTEX(mem_list_mutex);
  21. #define MM_IOMMU_TABLE_GROUP_PAGE_DIRTY 0x1
  22. #define MM_IOMMU_TABLE_GROUP_PAGE_MASK ~(SZ_4K - 1)
  23. struct mm_iommu_table_group_mem_t {
  24. struct list_head next;
  25. struct rcu_head rcu;
  26. unsigned long used;
  27. atomic64_t mapped;
  28. unsigned int pageshift;
  29. u64 ua; /* userspace address */
  30. u64 entries; /* number of entries in hpas/hpages[] */
  31. /*
  32. * in mm_iommu_get we temporarily use this to store
  33. * struct page address.
  34. *
  35. * We need to convert ua to hpa in real mode. Make it
  36. * simpler by storing physical address.
  37. */
  38. union {
  39. struct page **hpages; /* vmalloc'ed */
  40. phys_addr_t *hpas;
  41. };
  42. #define MM_IOMMU_TABLE_INVALID_HPA ((uint64_t)-1)
  43. u64 dev_hpa; /* Device memory base address */
  44. };
  45. bool mm_iommu_preregistered(struct mm_struct *mm)
  46. {
  47. return !list_empty(&mm->context.iommu_group_mem_list);
  48. }
  49. EXPORT_SYMBOL_GPL(mm_iommu_preregistered);
  50. static long mm_iommu_do_alloc(struct mm_struct *mm, unsigned long ua,
  51. unsigned long entries, unsigned long dev_hpa,
  52. struct mm_iommu_table_group_mem_t **pmem)
  53. {
  54. struct mm_iommu_table_group_mem_t *mem, *mem2;
  55. long i, ret, locked_entries = 0, pinned = 0;
  56. unsigned int pageshift;
  57. unsigned long entry, chunk;
  58. if (dev_hpa == MM_IOMMU_TABLE_INVALID_HPA) {
  59. ret = account_locked_vm(mm, entries, true);
  60. if (ret)
  61. return ret;
  62. locked_entries = entries;
  63. }
  64. mem = kzalloc(sizeof(*mem), GFP_KERNEL);
  65. if (!mem) {
  66. ret = -ENOMEM;
  67. goto unlock_exit;
  68. }
  69. if (dev_hpa != MM_IOMMU_TABLE_INVALID_HPA) {
  70. mem->pageshift = __ffs(dev_hpa | (entries << PAGE_SHIFT));
  71. mem->dev_hpa = dev_hpa;
  72. goto good_exit;
  73. }
  74. mem->dev_hpa = MM_IOMMU_TABLE_INVALID_HPA;
  75. /*
  76. * For a starting point for a maximum page size calculation
  77. * we use @ua and @entries natural alignment to allow IOMMU pages
  78. * smaller than huge pages but still bigger than PAGE_SIZE.
  79. */
  80. mem->pageshift = __ffs(ua | (entries << PAGE_SHIFT));
  81. mem->hpas = vzalloc(array_size(entries, sizeof(mem->hpas[0])));
  82. if (!mem->hpas) {
  83. kfree(mem);
  84. ret = -ENOMEM;
  85. goto unlock_exit;
  86. }
  87. mmap_read_lock(mm);
  88. chunk = (1UL << (PAGE_SHIFT + MAX_ORDER - 1)) /
  89. sizeof(struct vm_area_struct *);
  90. chunk = min(chunk, entries);
  91. for (entry = 0; entry < entries; entry += chunk) {
  92. unsigned long n = min(entries - entry, chunk);
  93. ret = pin_user_pages(ua + (entry << PAGE_SHIFT), n,
  94. FOLL_WRITE | FOLL_LONGTERM,
  95. mem->hpages + entry, NULL);
  96. if (ret == n) {
  97. pinned += n;
  98. continue;
  99. }
  100. if (ret > 0)
  101. pinned += ret;
  102. break;
  103. }
  104. mmap_read_unlock(mm);
  105. if (pinned != entries) {
  106. if (!ret)
  107. ret = -EFAULT;
  108. goto free_exit;
  109. }
  110. good_exit:
  111. atomic64_set(&mem->mapped, 1);
  112. mem->used = 1;
  113. mem->ua = ua;
  114. mem->entries = entries;
  115. mutex_lock(&mem_list_mutex);
  116. list_for_each_entry_rcu(mem2, &mm->context.iommu_group_mem_list, next,
  117. lockdep_is_held(&mem_list_mutex)) {
  118. /* Overlap? */
  119. if ((mem2->ua < (ua + (entries << PAGE_SHIFT))) &&
  120. (ua < (mem2->ua +
  121. (mem2->entries << PAGE_SHIFT)))) {
  122. ret = -EINVAL;
  123. mutex_unlock(&mem_list_mutex);
  124. goto free_exit;
  125. }
  126. }
  127. if (mem->dev_hpa == MM_IOMMU_TABLE_INVALID_HPA) {
  128. /*
  129. * Allow to use larger than 64k IOMMU pages. Only do that
  130. * if we are backed by hugetlb. Skip device memory as it is not
  131. * backed with page structs.
  132. */
  133. pageshift = PAGE_SHIFT;
  134. for (i = 0; i < entries; ++i) {
  135. struct page *page = mem->hpages[i];
  136. if ((mem->pageshift > PAGE_SHIFT) && PageHuge(page))
  137. pageshift = page_shift(compound_head(page));
  138. mem->pageshift = min(mem->pageshift, pageshift);
  139. /*
  140. * We don't need struct page reference any more, switch
  141. * to physical address.
  142. */
  143. mem->hpas[i] = page_to_pfn(page) << PAGE_SHIFT;
  144. }
  145. }
  146. list_add_rcu(&mem->next, &mm->context.iommu_group_mem_list);
  147. mutex_unlock(&mem_list_mutex);
  148. *pmem = mem;
  149. return 0;
  150. free_exit:
  151. /* free the references taken */
  152. unpin_user_pages(mem->hpages, pinned);
  153. vfree(mem->hpas);
  154. kfree(mem);
  155. unlock_exit:
  156. account_locked_vm(mm, locked_entries, false);
  157. return ret;
  158. }
  159. long mm_iommu_new(struct mm_struct *mm, unsigned long ua, unsigned long entries,
  160. struct mm_iommu_table_group_mem_t **pmem)
  161. {
  162. return mm_iommu_do_alloc(mm, ua, entries, MM_IOMMU_TABLE_INVALID_HPA,
  163. pmem);
  164. }
  165. EXPORT_SYMBOL_GPL(mm_iommu_new);
  166. long mm_iommu_newdev(struct mm_struct *mm, unsigned long ua,
  167. unsigned long entries, unsigned long dev_hpa,
  168. struct mm_iommu_table_group_mem_t **pmem)
  169. {
  170. return mm_iommu_do_alloc(mm, ua, entries, dev_hpa, pmem);
  171. }
  172. EXPORT_SYMBOL_GPL(mm_iommu_newdev);
  173. static void mm_iommu_unpin(struct mm_iommu_table_group_mem_t *mem)
  174. {
  175. long i;
  176. struct page *page = NULL;
  177. if (!mem->hpas)
  178. return;
  179. for (i = 0; i < mem->entries; ++i) {
  180. if (!mem->hpas[i])
  181. continue;
  182. page = pfn_to_page(mem->hpas[i] >> PAGE_SHIFT);
  183. if (!page)
  184. continue;
  185. if (mem->hpas[i] & MM_IOMMU_TABLE_GROUP_PAGE_DIRTY)
  186. SetPageDirty(page);
  187. unpin_user_page(page);
  188. mem->hpas[i] = 0;
  189. }
  190. }
  191. static void mm_iommu_do_free(struct mm_iommu_table_group_mem_t *mem)
  192. {
  193. mm_iommu_unpin(mem);
  194. vfree(mem->hpas);
  195. kfree(mem);
  196. }
  197. static void mm_iommu_free(struct rcu_head *head)
  198. {
  199. struct mm_iommu_table_group_mem_t *mem = container_of(head,
  200. struct mm_iommu_table_group_mem_t, rcu);
  201. mm_iommu_do_free(mem);
  202. }
  203. static void mm_iommu_release(struct mm_iommu_table_group_mem_t *mem)
  204. {
  205. list_del_rcu(&mem->next);
  206. call_rcu(&mem->rcu, mm_iommu_free);
  207. }
  208. long mm_iommu_put(struct mm_struct *mm, struct mm_iommu_table_group_mem_t *mem)
  209. {
  210. long ret = 0;
  211. unsigned long unlock_entries = 0;
  212. mutex_lock(&mem_list_mutex);
  213. if (mem->used == 0) {
  214. ret = -ENOENT;
  215. goto unlock_exit;
  216. }
  217. --mem->used;
  218. /* There are still users, exit */
  219. if (mem->used)
  220. goto unlock_exit;
  221. /* Are there still mappings? */
  222. if (atomic64_cmpxchg(&mem->mapped, 1, 0) != 1) {
  223. ++mem->used;
  224. ret = -EBUSY;
  225. goto unlock_exit;
  226. }
  227. if (mem->dev_hpa == MM_IOMMU_TABLE_INVALID_HPA)
  228. unlock_entries = mem->entries;
  229. /* @mapped became 0 so now mappings are disabled, release the region */
  230. mm_iommu_release(mem);
  231. unlock_exit:
  232. mutex_unlock(&mem_list_mutex);
  233. account_locked_vm(mm, unlock_entries, false);
  234. return ret;
  235. }
  236. EXPORT_SYMBOL_GPL(mm_iommu_put);
  237. struct mm_iommu_table_group_mem_t *mm_iommu_lookup(struct mm_struct *mm,
  238. unsigned long ua, unsigned long size)
  239. {
  240. struct mm_iommu_table_group_mem_t *mem, *ret = NULL;
  241. rcu_read_lock();
  242. list_for_each_entry_rcu(mem, &mm->context.iommu_group_mem_list, next) {
  243. if ((mem->ua <= ua) &&
  244. (ua + size <= mem->ua +
  245. (mem->entries << PAGE_SHIFT))) {
  246. ret = mem;
  247. break;
  248. }
  249. }
  250. rcu_read_unlock();
  251. return ret;
  252. }
  253. EXPORT_SYMBOL_GPL(mm_iommu_lookup);
  254. struct mm_iommu_table_group_mem_t *mm_iommu_get(struct mm_struct *mm,
  255. unsigned long ua, unsigned long entries)
  256. {
  257. struct mm_iommu_table_group_mem_t *mem, *ret = NULL;
  258. mutex_lock(&mem_list_mutex);
  259. list_for_each_entry_rcu(mem, &mm->context.iommu_group_mem_list, next,
  260. lockdep_is_held(&mem_list_mutex)) {
  261. if ((mem->ua == ua) && (mem->entries == entries)) {
  262. ret = mem;
  263. ++mem->used;
  264. break;
  265. }
  266. }
  267. mutex_unlock(&mem_list_mutex);
  268. return ret;
  269. }
  270. EXPORT_SYMBOL_GPL(mm_iommu_get);
  271. long mm_iommu_ua_to_hpa(struct mm_iommu_table_group_mem_t *mem,
  272. unsigned long ua, unsigned int pageshift, unsigned long *hpa)
  273. {
  274. const long entry = (ua - mem->ua) >> PAGE_SHIFT;
  275. u64 *va;
  276. if (entry >= mem->entries)
  277. return -EFAULT;
  278. if (pageshift > mem->pageshift)
  279. return -EFAULT;
  280. if (!mem->hpas) {
  281. *hpa = mem->dev_hpa + (ua - mem->ua);
  282. return 0;
  283. }
  284. va = &mem->hpas[entry];
  285. *hpa = (*va & MM_IOMMU_TABLE_GROUP_PAGE_MASK) | (ua & ~PAGE_MASK);
  286. return 0;
  287. }
  288. EXPORT_SYMBOL_GPL(mm_iommu_ua_to_hpa);
  289. bool mm_iommu_is_devmem(struct mm_struct *mm, unsigned long hpa,
  290. unsigned int pageshift, unsigned long *size)
  291. {
  292. struct mm_iommu_table_group_mem_t *mem;
  293. unsigned long end;
  294. rcu_read_lock();
  295. list_for_each_entry_rcu(mem, &mm->context.iommu_group_mem_list, next) {
  296. if (mem->dev_hpa == MM_IOMMU_TABLE_INVALID_HPA)
  297. continue;
  298. end = mem->dev_hpa + (mem->entries << PAGE_SHIFT);
  299. if ((mem->dev_hpa <= hpa) && (hpa < end)) {
  300. /*
  301. * Since the IOMMU page size might be bigger than
  302. * PAGE_SIZE, the amount of preregistered memory
  303. * starting from @hpa might be smaller than 1<<pageshift
  304. * and the caller needs to distinguish this situation.
  305. */
  306. *size = min(1UL << pageshift, end - hpa);
  307. return true;
  308. }
  309. }
  310. rcu_read_unlock();
  311. return false;
  312. }
  313. EXPORT_SYMBOL_GPL(mm_iommu_is_devmem);
  314. long mm_iommu_mapped_inc(struct mm_iommu_table_group_mem_t *mem)
  315. {
  316. if (atomic64_inc_not_zero(&mem->mapped))
  317. return 0;
  318. /* Last mm_iommu_put() has been called, no more mappings allowed() */
  319. return -ENXIO;
  320. }
  321. EXPORT_SYMBOL_GPL(mm_iommu_mapped_inc);
  322. void mm_iommu_mapped_dec(struct mm_iommu_table_group_mem_t *mem)
  323. {
  324. atomic64_add_unless(&mem->mapped, -1, 1);
  325. }
  326. EXPORT_SYMBOL_GPL(mm_iommu_mapped_dec);
  327. void mm_iommu_init(struct mm_struct *mm)
  328. {
  329. INIT_LIST_HEAD_RCU(&mm->context.iommu_group_mem_list);
  330. }