mmu_context_iommu.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  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 ? current->pid : 0,
  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(struct mm_struct *mm)
  60. {
  61. return !list_empty(&mm->context.iommu_group_mem_list);
  62. }
  63. EXPORT_SYMBOL_GPL(mm_iommu_preregistered);
  64. /*
  65. * Taken from alloc_migrate_target with changes to remove CMA allocations
  66. */
  67. struct page *new_iommu_non_cma_page(struct page *page, unsigned long private,
  68. int **resultp)
  69. {
  70. gfp_t gfp_mask = GFP_USER;
  71. struct page *new_page;
  72. if (PageHuge(page) || PageTransHuge(page) || PageCompound(page))
  73. return NULL;
  74. if (PageHighMem(page))
  75. gfp_mask |= __GFP_HIGHMEM;
  76. /*
  77. * We don't want the allocation to force an OOM if possibe
  78. */
  79. new_page = alloc_page(gfp_mask | __GFP_NORETRY | __GFP_NOWARN);
  80. return new_page;
  81. }
  82. static int mm_iommu_move_page_from_cma(struct page *page)
  83. {
  84. int ret = 0;
  85. LIST_HEAD(cma_migrate_pages);
  86. /* Ignore huge pages for now */
  87. if (PageHuge(page) || PageTransHuge(page) || PageCompound(page))
  88. return -EBUSY;
  89. lru_add_drain();
  90. ret = isolate_lru_page(page);
  91. if (ret)
  92. return ret;
  93. list_add(&page->lru, &cma_migrate_pages);
  94. put_page(page); /* Drop the gup reference */
  95. ret = migrate_pages(&cma_migrate_pages, new_iommu_non_cma_page,
  96. NULL, 0, MIGRATE_SYNC, MR_CMA);
  97. if (ret) {
  98. if (!list_empty(&cma_migrate_pages))
  99. putback_movable_pages(&cma_migrate_pages);
  100. }
  101. return 0;
  102. }
  103. long mm_iommu_get(struct mm_struct *mm, unsigned long ua, unsigned long entries,
  104. struct mm_iommu_table_group_mem_t **pmem)
  105. {
  106. struct mm_iommu_table_group_mem_t *mem;
  107. long i, j, ret = 0, locked_entries = 0;
  108. struct page *page = NULL;
  109. mutex_lock(&mem_list_mutex);
  110. list_for_each_entry_rcu(mem, &mm->context.iommu_group_mem_list,
  111. next) {
  112. if ((mem->ua == ua) && (mem->entries == entries)) {
  113. ++mem->used;
  114. *pmem = mem;
  115. goto unlock_exit;
  116. }
  117. /* Overlap? */
  118. if ((mem->ua < (ua + (entries << PAGE_SHIFT))) &&
  119. (ua < (mem->ua +
  120. (mem->entries << PAGE_SHIFT)))) {
  121. ret = -EINVAL;
  122. goto unlock_exit;
  123. }
  124. }
  125. ret = mm_iommu_adjust_locked_vm(mm, entries, true);
  126. if (ret)
  127. goto unlock_exit;
  128. locked_entries = entries;
  129. mem = kzalloc(sizeof(*mem), GFP_KERNEL);
  130. if (!mem) {
  131. ret = -ENOMEM;
  132. goto unlock_exit;
  133. }
  134. mem->hpas = vzalloc(entries * sizeof(mem->hpas[0]));
  135. if (!mem->hpas) {
  136. kfree(mem);
  137. ret = -ENOMEM;
  138. goto unlock_exit;
  139. }
  140. for (i = 0; i < entries; ++i) {
  141. if (1 != get_user_pages_fast(ua + (i << PAGE_SHIFT),
  142. 1/* pages */, 1/* iswrite */, &page)) {
  143. ret = -EFAULT;
  144. for (j = 0; j < i; ++j)
  145. put_page(pfn_to_page(mem->hpas[j] >>
  146. PAGE_SHIFT));
  147. vfree(mem->hpas);
  148. kfree(mem);
  149. goto unlock_exit;
  150. }
  151. /*
  152. * If we get a page from the CMA zone, since we are going to
  153. * be pinning these entries, we might as well move them out
  154. * of the CMA zone if possible. NOTE: faulting in + migration
  155. * can be expensive. Batching can be considered later
  156. */
  157. if (get_pageblock_migratetype(page) == MIGRATE_CMA) {
  158. if (mm_iommu_move_page_from_cma(page))
  159. goto populate;
  160. if (1 != get_user_pages_fast(ua + (i << PAGE_SHIFT),
  161. 1/* pages */, 1/* iswrite */,
  162. &page)) {
  163. ret = -EFAULT;
  164. for (j = 0; j < i; ++j)
  165. put_page(pfn_to_page(mem->hpas[j] >>
  166. PAGE_SHIFT));
  167. vfree(mem->hpas);
  168. kfree(mem);
  169. goto unlock_exit;
  170. }
  171. }
  172. populate:
  173. mem->hpas[i] = page_to_pfn(page) << PAGE_SHIFT;
  174. }
  175. atomic64_set(&mem->mapped, 1);
  176. mem->used = 1;
  177. mem->ua = ua;
  178. mem->entries = entries;
  179. *pmem = mem;
  180. list_add_rcu(&mem->next, &mm->context.iommu_group_mem_list);
  181. unlock_exit:
  182. if (locked_entries && ret)
  183. mm_iommu_adjust_locked_vm(mm, locked_entries, false);
  184. mutex_unlock(&mem_list_mutex);
  185. return ret;
  186. }
  187. EXPORT_SYMBOL_GPL(mm_iommu_get);
  188. static void mm_iommu_unpin(struct mm_iommu_table_group_mem_t *mem)
  189. {
  190. long i;
  191. struct page *page = NULL;
  192. for (i = 0; i < mem->entries; ++i) {
  193. if (!mem->hpas[i])
  194. continue;
  195. page = pfn_to_page(mem->hpas[i] >> PAGE_SHIFT);
  196. if (!page)
  197. continue;
  198. put_page(page);
  199. mem->hpas[i] = 0;
  200. }
  201. }
  202. static void mm_iommu_do_free(struct mm_iommu_table_group_mem_t *mem)
  203. {
  204. mm_iommu_unpin(mem);
  205. vfree(mem->hpas);
  206. kfree(mem);
  207. }
  208. static void mm_iommu_free(struct rcu_head *head)
  209. {
  210. struct mm_iommu_table_group_mem_t *mem = container_of(head,
  211. struct mm_iommu_table_group_mem_t, rcu);
  212. mm_iommu_do_free(mem);
  213. }
  214. static void mm_iommu_release(struct mm_iommu_table_group_mem_t *mem)
  215. {
  216. list_del_rcu(&mem->next);
  217. call_rcu(&mem->rcu, mm_iommu_free);
  218. }
  219. long mm_iommu_put(struct mm_struct *mm, struct mm_iommu_table_group_mem_t *mem)
  220. {
  221. long ret = 0;
  222. mutex_lock(&mem_list_mutex);
  223. if (mem->used == 0) {
  224. ret = -ENOENT;
  225. goto unlock_exit;
  226. }
  227. --mem->used;
  228. /* There are still users, exit */
  229. if (mem->used)
  230. goto unlock_exit;
  231. /* Are there still mappings? */
  232. if (atomic_cmpxchg(&mem->mapped, 1, 0) != 1) {
  233. ++mem->used;
  234. ret = -EBUSY;
  235. goto unlock_exit;
  236. }
  237. /* @mapped became 0 so now mappings are disabled, release the region */
  238. mm_iommu_release(mem);
  239. mm_iommu_adjust_locked_vm(mm, mem->entries, false);
  240. unlock_exit:
  241. mutex_unlock(&mem_list_mutex);
  242. return ret;
  243. }
  244. EXPORT_SYMBOL_GPL(mm_iommu_put);
  245. struct mm_iommu_table_group_mem_t *mm_iommu_lookup(struct mm_struct *mm,
  246. unsigned long ua, unsigned long size)
  247. {
  248. struct mm_iommu_table_group_mem_t *mem, *ret = NULL;
  249. list_for_each_entry_rcu(mem, &mm->context.iommu_group_mem_list, next) {
  250. if ((mem->ua <= ua) &&
  251. (ua + size <= mem->ua +
  252. (mem->entries << PAGE_SHIFT))) {
  253. ret = mem;
  254. break;
  255. }
  256. }
  257. return ret;
  258. }
  259. EXPORT_SYMBOL_GPL(mm_iommu_lookup);
  260. struct mm_iommu_table_group_mem_t *mm_iommu_find(struct mm_struct *mm,
  261. unsigned long ua, unsigned long entries)
  262. {
  263. struct mm_iommu_table_group_mem_t *mem, *ret = NULL;
  264. list_for_each_entry_rcu(mem, &mm->context.iommu_group_mem_list, next) {
  265. if ((mem->ua == ua) && (mem->entries == entries)) {
  266. ret = mem;
  267. break;
  268. }
  269. }
  270. return ret;
  271. }
  272. EXPORT_SYMBOL_GPL(mm_iommu_find);
  273. long mm_iommu_ua_to_hpa(struct mm_iommu_table_group_mem_t *mem,
  274. unsigned long ua, unsigned long *hpa)
  275. {
  276. const long entry = (ua - mem->ua) >> PAGE_SHIFT;
  277. u64 *va = &mem->hpas[entry];
  278. if (entry >= mem->entries)
  279. return -EFAULT;
  280. *hpa = *va | (ua & ~PAGE_MASK);
  281. return 0;
  282. }
  283. EXPORT_SYMBOL_GPL(mm_iommu_ua_to_hpa);
  284. long mm_iommu_mapped_inc(struct mm_iommu_table_group_mem_t *mem)
  285. {
  286. if (atomic64_inc_not_zero(&mem->mapped))
  287. return 0;
  288. /* Last mm_iommu_put() has been called, no more mappings allowed() */
  289. return -ENXIO;
  290. }
  291. EXPORT_SYMBOL_GPL(mm_iommu_mapped_inc);
  292. void mm_iommu_mapped_dec(struct mm_iommu_table_group_mem_t *mem)
  293. {
  294. atomic64_add_unless(&mem->mapped, -1, 1);
  295. }
  296. EXPORT_SYMBOL_GPL(mm_iommu_mapped_dec);
  297. void mm_iommu_init(struct mm_struct *mm)
  298. {
  299. INIT_LIST_HEAD_RCU(&mm->context.iommu_group_mem_list);
  300. }