mmu_context_iommu.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389
  1. /*
  2. * IOMMU helpers in MMU context.
  3. *
  4. * Copyright (C) 2015 IBM Corp. <aik@ozlabs.ru>
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License
  8. * as published by the Free Software Foundation; either version
  9. * 2 of the License, or (at your option) any later version.
  10. *
  11. */
  12. #include <linux/sched.h>
  13. #include <linux/slab.h>
  14. #include <linux/rculist.h>
  15. #include <linux/vmalloc.h>
  16. #include <linux/mutex.h>
  17. #include <linux/migrate.h>
  18. #include <linux/hugetlb.h>
  19. #include <linux/swap.h>
  20. #include <asm/mmu_context.h>
  21. static DEFINE_MUTEX(mem_list_mutex);
  22. struct mm_iommu_table_group_mem_t {
  23. struct list_head next;
  24. struct rcu_head rcu;
  25. unsigned long used;
  26. atomic64_t mapped;
  27. u64 ua; /* userspace address */
  28. u64 entries; /* number of entries in hpas[] */
  29. u64 *hpas; /* vmalloc'ed */
  30. };
  31. static long mm_iommu_adjust_locked_vm(struct mm_struct *mm,
  32. unsigned long npages, bool incr)
  33. {
  34. long ret = 0, locked, lock_limit;
  35. if (!npages)
  36. return 0;
  37. down_write(&mm->mmap_sem);
  38. if (incr) {
  39. locked = mm->locked_vm + npages;
  40. lock_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
  41. if (locked > lock_limit && !capable(CAP_IPC_LOCK))
  42. ret = -ENOMEM;
  43. else
  44. mm->locked_vm += npages;
  45. } else {
  46. if (WARN_ON_ONCE(npages > mm->locked_vm))
  47. npages = mm->locked_vm;
  48. mm->locked_vm -= npages;
  49. }
  50. pr_debug("[%d] RLIMIT_MEMLOCK HASH64 %c%ld %ld/%ld\n",
  51. current->pid,
  52. incr ? '+' : '-',
  53. npages << PAGE_SHIFT,
  54. mm->locked_vm << PAGE_SHIFT,
  55. rlimit(RLIMIT_MEMLOCK));
  56. up_write(&mm->mmap_sem);
  57. return ret;
  58. }
  59. bool mm_iommu_preregistered(void)
  60. {
  61. if (!current || !current->mm)
  62. return false;
  63. return !list_empty(&current->mm->context.iommu_group_mem_list);
  64. }
  65. EXPORT_SYMBOL_GPL(mm_iommu_preregistered);
  66. /*
  67. * Taken from alloc_migrate_target with changes to remove CMA allocations
  68. */
  69. struct page *new_iommu_non_cma_page(struct page *page, unsigned long private,
  70. int **resultp)
  71. {
  72. gfp_t gfp_mask = GFP_USER;
  73. struct page *new_page;
  74. if (PageHuge(page) || PageTransHuge(page) || PageCompound(page))
  75. return NULL;
  76. if (PageHighMem(page))
  77. gfp_mask |= __GFP_HIGHMEM;
  78. /*
  79. * We don't want the allocation to force an OOM if possibe
  80. */
  81. new_page = alloc_page(gfp_mask | __GFP_NORETRY | __GFP_NOWARN);
  82. return new_page;
  83. }
  84. static int mm_iommu_move_page_from_cma(struct page *page)
  85. {
  86. int ret = 0;
  87. LIST_HEAD(cma_migrate_pages);
  88. /* Ignore huge pages for now */
  89. if (PageHuge(page) || PageTransHuge(page) || PageCompound(page))
  90. return -EBUSY;
  91. lru_add_drain();
  92. ret = isolate_lru_page(page);
  93. if (ret)
  94. return ret;
  95. list_add(&page->lru, &cma_migrate_pages);
  96. put_page(page); /* Drop the gup reference */
  97. ret = migrate_pages(&cma_migrate_pages, new_iommu_non_cma_page,
  98. NULL, 0, MIGRATE_SYNC, MR_CMA);
  99. if (ret) {
  100. if (!list_empty(&cma_migrate_pages))
  101. putback_movable_pages(&cma_migrate_pages);
  102. }
  103. return 0;
  104. }
  105. long mm_iommu_get(unsigned long ua, unsigned long entries,
  106. struct mm_iommu_table_group_mem_t **pmem)
  107. {
  108. struct mm_iommu_table_group_mem_t *mem;
  109. long i, j, ret = 0, locked_entries = 0;
  110. struct page *page = NULL;
  111. if (!current || !current->mm)
  112. return -ESRCH; /* process exited */
  113. mutex_lock(&mem_list_mutex);
  114. list_for_each_entry_rcu(mem, &current->mm->context.iommu_group_mem_list,
  115. next) {
  116. if ((mem->ua == ua) && (mem->entries == entries)) {
  117. ++mem->used;
  118. *pmem = mem;
  119. goto unlock_exit;
  120. }
  121. /* Overlap? */
  122. if ((mem->ua < (ua + (entries << PAGE_SHIFT))) &&
  123. (ua < (mem->ua +
  124. (mem->entries << PAGE_SHIFT)))) {
  125. ret = -EINVAL;
  126. goto unlock_exit;
  127. }
  128. }
  129. ret = mm_iommu_adjust_locked_vm(current->mm, entries, true);
  130. if (ret)
  131. goto unlock_exit;
  132. locked_entries = entries;
  133. mem = kzalloc(sizeof(*mem), GFP_KERNEL);
  134. if (!mem) {
  135. ret = -ENOMEM;
  136. goto unlock_exit;
  137. }
  138. mem->hpas = vzalloc(entries * sizeof(mem->hpas[0]));
  139. if (!mem->hpas) {
  140. kfree(mem);
  141. ret = -ENOMEM;
  142. goto unlock_exit;
  143. }
  144. for (i = 0; i < entries; ++i) {
  145. if (1 != get_user_pages_fast(ua + (i << PAGE_SHIFT),
  146. 1/* pages */, 1/* iswrite */, &page)) {
  147. ret = -EFAULT;
  148. for (j = 0; j < i; ++j)
  149. put_page(pfn_to_page(mem->hpas[j] >>
  150. PAGE_SHIFT));
  151. vfree(mem->hpas);
  152. kfree(mem);
  153. goto unlock_exit;
  154. }
  155. /*
  156. * If we get a page from the CMA zone, since we are going to
  157. * be pinning these entries, we might as well move them out
  158. * of the CMA zone if possible. NOTE: faulting in + migration
  159. * can be expensive. Batching can be considered later
  160. */
  161. if (get_pageblock_migratetype(page) == MIGRATE_CMA) {
  162. if (mm_iommu_move_page_from_cma(page))
  163. goto populate;
  164. if (1 != get_user_pages_fast(ua + (i << PAGE_SHIFT),
  165. 1/* pages */, 1/* iswrite */,
  166. &page)) {
  167. ret = -EFAULT;
  168. for (j = 0; j < i; ++j)
  169. put_page(pfn_to_page(mem->hpas[j] >>
  170. PAGE_SHIFT));
  171. vfree(mem->hpas);
  172. kfree(mem);
  173. goto unlock_exit;
  174. }
  175. }
  176. populate:
  177. mem->hpas[i] = page_to_pfn(page) << PAGE_SHIFT;
  178. }
  179. atomic64_set(&mem->mapped, 1);
  180. mem->used = 1;
  181. mem->ua = ua;
  182. mem->entries = entries;
  183. *pmem = mem;
  184. list_add_rcu(&mem->next, &current->mm->context.iommu_group_mem_list);
  185. unlock_exit:
  186. if (locked_entries && ret)
  187. mm_iommu_adjust_locked_vm(current->mm, locked_entries, false);
  188. mutex_unlock(&mem_list_mutex);
  189. return ret;
  190. }
  191. EXPORT_SYMBOL_GPL(mm_iommu_get);
  192. static void mm_iommu_unpin(struct mm_iommu_table_group_mem_t *mem)
  193. {
  194. long i;
  195. struct page *page = NULL;
  196. for (i = 0; i < mem->entries; ++i) {
  197. if (!mem->hpas[i])
  198. continue;
  199. page = pfn_to_page(mem->hpas[i] >> PAGE_SHIFT);
  200. if (!page)
  201. continue;
  202. put_page(page);
  203. mem->hpas[i] = 0;
  204. }
  205. }
  206. static void mm_iommu_do_free(struct mm_iommu_table_group_mem_t *mem)
  207. {
  208. mm_iommu_unpin(mem);
  209. vfree(mem->hpas);
  210. kfree(mem);
  211. }
  212. static void mm_iommu_free(struct rcu_head *head)
  213. {
  214. struct mm_iommu_table_group_mem_t *mem = container_of(head,
  215. struct mm_iommu_table_group_mem_t, rcu);
  216. mm_iommu_do_free(mem);
  217. }
  218. static void mm_iommu_release(struct mm_iommu_table_group_mem_t *mem)
  219. {
  220. list_del_rcu(&mem->next);
  221. mm_iommu_adjust_locked_vm(current->mm, mem->entries, false);
  222. call_rcu(&mem->rcu, mm_iommu_free);
  223. }
  224. long mm_iommu_put(struct mm_iommu_table_group_mem_t *mem)
  225. {
  226. long ret = 0;
  227. if (!current || !current->mm)
  228. return -ESRCH; /* process exited */
  229. mutex_lock(&mem_list_mutex);
  230. if (mem->used == 0) {
  231. ret = -ENOENT;
  232. goto unlock_exit;
  233. }
  234. --mem->used;
  235. /* There are still users, exit */
  236. if (mem->used)
  237. goto unlock_exit;
  238. /* Are there still mappings? */
  239. if (atomic_cmpxchg(&mem->mapped, 1, 0) != 1) {
  240. ++mem->used;
  241. ret = -EBUSY;
  242. goto unlock_exit;
  243. }
  244. /* @mapped became 0 so now mappings are disabled, release the region */
  245. mm_iommu_release(mem);
  246. unlock_exit:
  247. mutex_unlock(&mem_list_mutex);
  248. return ret;
  249. }
  250. EXPORT_SYMBOL_GPL(mm_iommu_put);
  251. struct mm_iommu_table_group_mem_t *mm_iommu_lookup(unsigned long ua,
  252. unsigned long size)
  253. {
  254. struct mm_iommu_table_group_mem_t *mem, *ret = NULL;
  255. list_for_each_entry_rcu(mem,
  256. &current->mm->context.iommu_group_mem_list,
  257. next) {
  258. if ((mem->ua <= ua) &&
  259. (ua + size <= mem->ua +
  260. (mem->entries << PAGE_SHIFT))) {
  261. ret = mem;
  262. break;
  263. }
  264. }
  265. return ret;
  266. }
  267. EXPORT_SYMBOL_GPL(mm_iommu_lookup);
  268. struct mm_iommu_table_group_mem_t *mm_iommu_find(unsigned long ua,
  269. unsigned long entries)
  270. {
  271. struct mm_iommu_table_group_mem_t *mem, *ret = NULL;
  272. list_for_each_entry_rcu(mem,
  273. &current->mm->context.iommu_group_mem_list,
  274. next) {
  275. if ((mem->ua == ua) && (mem->entries == entries)) {
  276. ret = mem;
  277. break;
  278. }
  279. }
  280. return ret;
  281. }
  282. EXPORT_SYMBOL_GPL(mm_iommu_find);
  283. long mm_iommu_ua_to_hpa(struct mm_iommu_table_group_mem_t *mem,
  284. unsigned long ua, unsigned long *hpa)
  285. {
  286. const long entry = (ua - mem->ua) >> PAGE_SHIFT;
  287. u64 *va = &mem->hpas[entry];
  288. if (entry >= mem->entries)
  289. return -EFAULT;
  290. *hpa = *va | (ua & ~PAGE_MASK);
  291. return 0;
  292. }
  293. EXPORT_SYMBOL_GPL(mm_iommu_ua_to_hpa);
  294. long mm_iommu_mapped_inc(struct mm_iommu_table_group_mem_t *mem)
  295. {
  296. if (atomic64_inc_not_zero(&mem->mapped))
  297. return 0;
  298. /* Last mm_iommu_put() has been called, no more mappings allowed() */
  299. return -ENXIO;
  300. }
  301. EXPORT_SYMBOL_GPL(mm_iommu_mapped_inc);
  302. void mm_iommu_mapped_dec(struct mm_iommu_table_group_mem_t *mem)
  303. {
  304. atomic64_add_unless(&mem->mapped, -1, 1);
  305. }
  306. EXPORT_SYMBOL_GPL(mm_iommu_mapped_dec);
  307. void mm_iommu_init(mm_context_t *ctx)
  308. {
  309. INIT_LIST_HEAD_RCU(&ctx->iommu_group_mem_list);
  310. }
  311. void mm_iommu_cleanup(mm_context_t *ctx)
  312. {
  313. struct mm_iommu_table_group_mem_t *mem, *tmp;
  314. list_for_each_entry_safe(mem, tmp, &ctx->iommu_group_mem_list, next) {
  315. list_del_rcu(&mem->next);
  316. mm_iommu_do_free(mem);
  317. }
  318. }