umem_odp.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802
  1. /*
  2. * Copyright (c) 2014 Mellanox Technologies. All rights reserved.
  3. *
  4. * This software is available to you under a choice of one of two
  5. * licenses. You may choose to be licensed under the terms of the GNU
  6. * General Public License (GPL) Version 2, available from the file
  7. * COPYING in the main directory of this source tree, or the
  8. * OpenIB.org BSD license below:
  9. *
  10. * Redistribution and use in source and binary forms, with or
  11. * without modification, are permitted provided that the following
  12. * conditions are met:
  13. *
  14. * - Redistributions of source code must retain the above
  15. * copyright notice, this list of conditions and the following
  16. * disclaimer.
  17. *
  18. * - Redistributions in binary form must reproduce the above
  19. * copyright notice, this list of conditions and the following
  20. * disclaimer in the documentation and/or other materials
  21. * provided with the distribution.
  22. *
  23. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  24. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  25. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  26. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  27. * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  28. * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  29. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  30. * SOFTWARE.
  31. */
  32. #include <linux/types.h>
  33. #include <linux/sched.h>
  34. #include <linux/sched/mm.h>
  35. #include <linux/sched/task.h>
  36. #include <linux/pid.h>
  37. #include <linux/slab.h>
  38. #include <linux/export.h>
  39. #include <linux/vmalloc.h>
  40. #include <linux/hugetlb.h>
  41. #include <linux/interval_tree_generic.h>
  42. #include <rdma/ib_verbs.h>
  43. #include <rdma/ib_umem.h>
  44. #include <rdma/ib_umem_odp.h>
  45. /*
  46. * The ib_umem list keeps track of memory regions for which the HW
  47. * device request to receive notification when the related memory
  48. * mapping is changed.
  49. *
  50. * ib_umem_lock protects the list.
  51. */
  52. static u64 node_start(struct umem_odp_node *n)
  53. {
  54. struct ib_umem_odp *umem_odp =
  55. container_of(n, struct ib_umem_odp, interval_tree);
  56. return ib_umem_start(&umem_odp->umem);
  57. }
  58. /* Note that the representation of the intervals in the interval tree
  59. * considers the ending point as contained in the interval, while the
  60. * function ib_umem_end returns the first address which is not contained
  61. * in the umem.
  62. */
  63. static u64 node_last(struct umem_odp_node *n)
  64. {
  65. struct ib_umem_odp *umem_odp =
  66. container_of(n, struct ib_umem_odp, interval_tree);
  67. return ib_umem_end(&umem_odp->umem) - 1;
  68. }
  69. INTERVAL_TREE_DEFINE(struct umem_odp_node, rb, u64, __subtree_last,
  70. node_start, node_last, static, rbt_ib_umem)
  71. static void ib_umem_notifier_start_account(struct ib_umem_odp *umem_odp)
  72. {
  73. mutex_lock(&umem_odp->umem_mutex);
  74. if (umem_odp->notifiers_count++ == 0)
  75. /*
  76. * Initialize the completion object for waiting on
  77. * notifiers. Since notifier_count is zero, no one should be
  78. * waiting right now.
  79. */
  80. reinit_completion(&umem_odp->notifier_completion);
  81. mutex_unlock(&umem_odp->umem_mutex);
  82. }
  83. static void ib_umem_notifier_end_account(struct ib_umem_odp *umem_odp)
  84. {
  85. mutex_lock(&umem_odp->umem_mutex);
  86. /*
  87. * This sequence increase will notify the QP page fault that the page
  88. * that is going to be mapped in the spte could have been freed.
  89. */
  90. ++umem_odp->notifiers_seq;
  91. if (--umem_odp->notifiers_count == 0)
  92. complete_all(&umem_odp->notifier_completion);
  93. mutex_unlock(&umem_odp->umem_mutex);
  94. }
  95. static int ib_umem_notifier_release_trampoline(struct ib_umem_odp *umem_odp,
  96. u64 start, u64 end, void *cookie)
  97. {
  98. struct ib_umem *umem = &umem_odp->umem;
  99. /*
  100. * Increase the number of notifiers running, to
  101. * prevent any further fault handling on this MR.
  102. */
  103. ib_umem_notifier_start_account(umem_odp);
  104. umem_odp->dying = 1;
  105. /* Make sure that the fact the umem is dying is out before we release
  106. * all pending page faults. */
  107. smp_wmb();
  108. complete_all(&umem_odp->notifier_completion);
  109. umem->context->invalidate_range(umem_odp, ib_umem_start(umem),
  110. ib_umem_end(umem));
  111. return 0;
  112. }
  113. static void ib_umem_notifier_release(struct mmu_notifier *mn,
  114. struct mm_struct *mm)
  115. {
  116. struct ib_ucontext_per_mm *per_mm =
  117. container_of(mn, struct ib_ucontext_per_mm, mn);
  118. down_read(&per_mm->umem_rwsem);
  119. if (per_mm->active)
  120. rbt_ib_umem_for_each_in_range(
  121. &per_mm->umem_tree, 0, ULLONG_MAX,
  122. ib_umem_notifier_release_trampoline, true, NULL);
  123. up_read(&per_mm->umem_rwsem);
  124. }
  125. static int invalidate_page_trampoline(struct ib_umem_odp *item, u64 start,
  126. u64 end, void *cookie)
  127. {
  128. ib_umem_notifier_start_account(item);
  129. item->umem.context->invalidate_range(item, start, start + PAGE_SIZE);
  130. ib_umem_notifier_end_account(item);
  131. return 0;
  132. }
  133. static int invalidate_range_start_trampoline(struct ib_umem_odp *item,
  134. u64 start, u64 end, void *cookie)
  135. {
  136. ib_umem_notifier_start_account(item);
  137. item->umem.context->invalidate_range(item, start, end);
  138. return 0;
  139. }
  140. static int ib_umem_notifier_invalidate_range_start(struct mmu_notifier *mn,
  141. struct mm_struct *mm,
  142. unsigned long start,
  143. unsigned long end,
  144. bool blockable)
  145. {
  146. struct ib_ucontext_per_mm *per_mm =
  147. container_of(mn, struct ib_ucontext_per_mm, mn);
  148. if (blockable)
  149. down_read(&per_mm->umem_rwsem);
  150. else if (!down_read_trylock(&per_mm->umem_rwsem))
  151. return -EAGAIN;
  152. if (!per_mm->active) {
  153. up_read(&per_mm->umem_rwsem);
  154. /*
  155. * At this point active is permanently set and visible to this
  156. * CPU without a lock, that fact is relied on to skip the unlock
  157. * in range_end.
  158. */
  159. return 0;
  160. }
  161. return rbt_ib_umem_for_each_in_range(&per_mm->umem_tree, start, end,
  162. invalidate_range_start_trampoline,
  163. blockable, NULL);
  164. }
  165. static int invalidate_range_end_trampoline(struct ib_umem_odp *item, u64 start,
  166. u64 end, void *cookie)
  167. {
  168. ib_umem_notifier_end_account(item);
  169. return 0;
  170. }
  171. static void ib_umem_notifier_invalidate_range_end(struct mmu_notifier *mn,
  172. struct mm_struct *mm,
  173. unsigned long start,
  174. unsigned long end)
  175. {
  176. struct ib_ucontext_per_mm *per_mm =
  177. container_of(mn, struct ib_ucontext_per_mm, mn);
  178. if (unlikely(!per_mm->active))
  179. return;
  180. rbt_ib_umem_for_each_in_range(&per_mm->umem_tree, start,
  181. end,
  182. invalidate_range_end_trampoline, true, NULL);
  183. up_read(&per_mm->umem_rwsem);
  184. }
  185. static const struct mmu_notifier_ops ib_umem_notifiers = {
  186. .release = ib_umem_notifier_release,
  187. .invalidate_range_start = ib_umem_notifier_invalidate_range_start,
  188. .invalidate_range_end = ib_umem_notifier_invalidate_range_end,
  189. };
  190. static void add_umem_to_per_mm(struct ib_umem_odp *umem_odp)
  191. {
  192. struct ib_ucontext_per_mm *per_mm = umem_odp->per_mm;
  193. struct ib_umem *umem = &umem_odp->umem;
  194. down_write(&per_mm->umem_rwsem);
  195. if (likely(ib_umem_start(umem) != ib_umem_end(umem)))
  196. rbt_ib_umem_insert(&umem_odp->interval_tree,
  197. &per_mm->umem_tree);
  198. up_write(&per_mm->umem_rwsem);
  199. }
  200. static void remove_umem_from_per_mm(struct ib_umem_odp *umem_odp)
  201. {
  202. struct ib_ucontext_per_mm *per_mm = umem_odp->per_mm;
  203. struct ib_umem *umem = &umem_odp->umem;
  204. down_write(&per_mm->umem_rwsem);
  205. if (likely(ib_umem_start(umem) != ib_umem_end(umem)))
  206. rbt_ib_umem_remove(&umem_odp->interval_tree,
  207. &per_mm->umem_tree);
  208. complete_all(&umem_odp->notifier_completion);
  209. up_write(&per_mm->umem_rwsem);
  210. }
  211. static struct ib_ucontext_per_mm *alloc_per_mm(struct ib_ucontext *ctx,
  212. struct mm_struct *mm)
  213. {
  214. struct ib_ucontext_per_mm *per_mm;
  215. int ret;
  216. per_mm = kzalloc(sizeof(*per_mm), GFP_KERNEL);
  217. if (!per_mm)
  218. return ERR_PTR(-ENOMEM);
  219. per_mm->context = ctx;
  220. per_mm->mm = mm;
  221. per_mm->umem_tree = RB_ROOT_CACHED;
  222. init_rwsem(&per_mm->umem_rwsem);
  223. per_mm->active = ctx->invalidate_range;
  224. rcu_read_lock();
  225. per_mm->tgid = get_task_pid(current->group_leader, PIDTYPE_PID);
  226. rcu_read_unlock();
  227. WARN_ON(mm != current->mm);
  228. per_mm->mn.ops = &ib_umem_notifiers;
  229. ret = mmu_notifier_register(&per_mm->mn, per_mm->mm);
  230. if (ret) {
  231. dev_err(&ctx->device->dev,
  232. "Failed to register mmu_notifier %d\n", ret);
  233. goto out_pid;
  234. }
  235. list_add(&per_mm->ucontext_list, &ctx->per_mm_list);
  236. return per_mm;
  237. out_pid:
  238. put_pid(per_mm->tgid);
  239. kfree(per_mm);
  240. return ERR_PTR(ret);
  241. }
  242. static int get_per_mm(struct ib_umem_odp *umem_odp)
  243. {
  244. struct ib_ucontext *ctx = umem_odp->umem.context;
  245. struct ib_ucontext_per_mm *per_mm;
  246. /*
  247. * Generally speaking we expect only one or two per_mm in this list,
  248. * so no reason to optimize this search today.
  249. */
  250. mutex_lock(&ctx->per_mm_list_lock);
  251. list_for_each_entry(per_mm, &ctx->per_mm_list, ucontext_list) {
  252. if (per_mm->mm == umem_odp->umem.owning_mm)
  253. goto found;
  254. }
  255. per_mm = alloc_per_mm(ctx, umem_odp->umem.owning_mm);
  256. if (IS_ERR(per_mm)) {
  257. mutex_unlock(&ctx->per_mm_list_lock);
  258. return PTR_ERR(per_mm);
  259. }
  260. found:
  261. umem_odp->per_mm = per_mm;
  262. per_mm->odp_mrs_count++;
  263. mutex_unlock(&ctx->per_mm_list_lock);
  264. return 0;
  265. }
  266. static void free_per_mm(struct rcu_head *rcu)
  267. {
  268. kfree(container_of(rcu, struct ib_ucontext_per_mm, rcu));
  269. }
  270. void put_per_mm(struct ib_umem_odp *umem_odp)
  271. {
  272. struct ib_ucontext_per_mm *per_mm = umem_odp->per_mm;
  273. struct ib_ucontext *ctx = umem_odp->umem.context;
  274. bool need_free;
  275. mutex_lock(&ctx->per_mm_list_lock);
  276. umem_odp->per_mm = NULL;
  277. per_mm->odp_mrs_count--;
  278. need_free = per_mm->odp_mrs_count == 0;
  279. if (need_free)
  280. list_del(&per_mm->ucontext_list);
  281. mutex_unlock(&ctx->per_mm_list_lock);
  282. if (!need_free)
  283. return;
  284. /*
  285. * NOTE! mmu_notifier_unregister() can happen between a start/end
  286. * callback, resulting in an start/end, and thus an unbalanced
  287. * lock. This doesn't really matter to us since we are about to kfree
  288. * the memory that holds the lock, however LOCKDEP doesn't like this.
  289. */
  290. down_write(&per_mm->umem_rwsem);
  291. per_mm->active = false;
  292. up_write(&per_mm->umem_rwsem);
  293. WARN_ON(!RB_EMPTY_ROOT(&per_mm->umem_tree.rb_root));
  294. mmu_notifier_unregister_no_release(&per_mm->mn, per_mm->mm);
  295. put_pid(per_mm->tgid);
  296. mmu_notifier_call_srcu(&per_mm->rcu, free_per_mm);
  297. }
  298. struct ib_umem_odp *ib_alloc_odp_umem(struct ib_ucontext_per_mm *per_mm,
  299. unsigned long addr, size_t size)
  300. {
  301. struct ib_ucontext *ctx = per_mm->context;
  302. struct ib_umem_odp *odp_data;
  303. struct ib_umem *umem;
  304. int pages = size >> PAGE_SHIFT;
  305. int ret;
  306. odp_data = kzalloc(sizeof(*odp_data), GFP_KERNEL);
  307. if (!odp_data)
  308. return ERR_PTR(-ENOMEM);
  309. umem = &odp_data->umem;
  310. umem->context = ctx;
  311. umem->length = size;
  312. umem->address = addr;
  313. umem->page_shift = PAGE_SHIFT;
  314. umem->writable = 1;
  315. umem->is_odp = 1;
  316. odp_data->per_mm = per_mm;
  317. mutex_init(&odp_data->umem_mutex);
  318. init_completion(&odp_data->notifier_completion);
  319. odp_data->page_list =
  320. vzalloc(array_size(pages, sizeof(*odp_data->page_list)));
  321. if (!odp_data->page_list) {
  322. ret = -ENOMEM;
  323. goto out_odp_data;
  324. }
  325. odp_data->dma_list =
  326. vzalloc(array_size(pages, sizeof(*odp_data->dma_list)));
  327. if (!odp_data->dma_list) {
  328. ret = -ENOMEM;
  329. goto out_page_list;
  330. }
  331. /*
  332. * Caller must ensure that the umem_odp that the per_mm came from
  333. * cannot be freed during the call to ib_alloc_odp_umem.
  334. */
  335. mutex_lock(&ctx->per_mm_list_lock);
  336. per_mm->odp_mrs_count++;
  337. mutex_unlock(&ctx->per_mm_list_lock);
  338. add_umem_to_per_mm(odp_data);
  339. return odp_data;
  340. out_page_list:
  341. vfree(odp_data->page_list);
  342. out_odp_data:
  343. kfree(odp_data);
  344. return ERR_PTR(ret);
  345. }
  346. EXPORT_SYMBOL(ib_alloc_odp_umem);
  347. int ib_umem_odp_get(struct ib_umem_odp *umem_odp, int access)
  348. {
  349. struct ib_umem *umem = &umem_odp->umem;
  350. /*
  351. * NOTE: This must called in a process context where umem->owning_mm
  352. * == current->mm
  353. */
  354. struct mm_struct *mm = umem->owning_mm;
  355. int ret_val;
  356. if (access & IB_ACCESS_HUGETLB) {
  357. struct vm_area_struct *vma;
  358. struct hstate *h;
  359. down_read(&mm->mmap_sem);
  360. vma = find_vma(mm, ib_umem_start(umem));
  361. if (!vma || !is_vm_hugetlb_page(vma)) {
  362. up_read(&mm->mmap_sem);
  363. return -EINVAL;
  364. }
  365. h = hstate_vma(vma);
  366. umem->page_shift = huge_page_shift(h);
  367. up_read(&mm->mmap_sem);
  368. umem->hugetlb = 1;
  369. } else {
  370. umem->hugetlb = 0;
  371. }
  372. mutex_init(&umem_odp->umem_mutex);
  373. init_completion(&umem_odp->notifier_completion);
  374. if (ib_umem_num_pages(umem)) {
  375. umem_odp->page_list =
  376. vzalloc(array_size(sizeof(*umem_odp->page_list),
  377. ib_umem_num_pages(umem)));
  378. if (!umem_odp->page_list)
  379. return -ENOMEM;
  380. umem_odp->dma_list =
  381. vzalloc(array_size(sizeof(*umem_odp->dma_list),
  382. ib_umem_num_pages(umem)));
  383. if (!umem_odp->dma_list) {
  384. ret_val = -ENOMEM;
  385. goto out_page_list;
  386. }
  387. }
  388. ret_val = get_per_mm(umem_odp);
  389. if (ret_val)
  390. goto out_dma_list;
  391. add_umem_to_per_mm(umem_odp);
  392. return 0;
  393. out_dma_list:
  394. vfree(umem_odp->dma_list);
  395. out_page_list:
  396. vfree(umem_odp->page_list);
  397. return ret_val;
  398. }
  399. void ib_umem_odp_release(struct ib_umem_odp *umem_odp)
  400. {
  401. struct ib_umem *umem = &umem_odp->umem;
  402. /*
  403. * Ensure that no more pages are mapped in the umem.
  404. *
  405. * It is the driver's responsibility to ensure, before calling us,
  406. * that the hardware will not attempt to access the MR any more.
  407. */
  408. ib_umem_odp_unmap_dma_pages(umem_odp, ib_umem_start(umem),
  409. ib_umem_end(umem));
  410. remove_umem_from_per_mm(umem_odp);
  411. put_per_mm(umem_odp);
  412. vfree(umem_odp->dma_list);
  413. vfree(umem_odp->page_list);
  414. }
  415. /*
  416. * Map for DMA and insert a single page into the on-demand paging page tables.
  417. *
  418. * @umem: the umem to insert the page to.
  419. * @page_index: index in the umem to add the page to.
  420. * @page: the page struct to map and add.
  421. * @access_mask: access permissions needed for this page.
  422. * @current_seq: sequence number for synchronization with invalidations.
  423. * the sequence number is taken from
  424. * umem_odp->notifiers_seq.
  425. *
  426. * The function returns -EFAULT if the DMA mapping operation fails. It returns
  427. * -EAGAIN if a concurrent invalidation prevents us from updating the page.
  428. *
  429. * The page is released via put_page even if the operation failed. For
  430. * on-demand pinning, the page is released whenever it isn't stored in the
  431. * umem.
  432. */
  433. static int ib_umem_odp_map_dma_single_page(
  434. struct ib_umem_odp *umem_odp,
  435. int page_index,
  436. struct page *page,
  437. u64 access_mask,
  438. unsigned long current_seq)
  439. {
  440. struct ib_umem *umem = &umem_odp->umem;
  441. struct ib_device *dev = umem->context->device;
  442. dma_addr_t dma_addr;
  443. int stored_page = 0;
  444. int remove_existing_mapping = 0;
  445. int ret = 0;
  446. /*
  447. * Note: we avoid writing if seq is different from the initial seq, to
  448. * handle case of a racing notifier. This check also allows us to bail
  449. * early if we have a notifier running in parallel with us.
  450. */
  451. if (ib_umem_mmu_notifier_retry(umem_odp, current_seq)) {
  452. ret = -EAGAIN;
  453. goto out;
  454. }
  455. if (!(umem_odp->dma_list[page_index])) {
  456. dma_addr = ib_dma_map_page(dev,
  457. page,
  458. 0, BIT(umem->page_shift),
  459. DMA_BIDIRECTIONAL);
  460. if (ib_dma_mapping_error(dev, dma_addr)) {
  461. ret = -EFAULT;
  462. goto out;
  463. }
  464. umem_odp->dma_list[page_index] = dma_addr | access_mask;
  465. umem_odp->page_list[page_index] = page;
  466. umem->npages++;
  467. stored_page = 1;
  468. } else if (umem_odp->page_list[page_index] == page) {
  469. umem_odp->dma_list[page_index] |= access_mask;
  470. } else {
  471. pr_err("error: got different pages in IB device and from get_user_pages. IB device page: %p, gup page: %p\n",
  472. umem_odp->page_list[page_index], page);
  473. /* Better remove the mapping now, to prevent any further
  474. * damage. */
  475. remove_existing_mapping = 1;
  476. }
  477. out:
  478. /* On Demand Paging - avoid pinning the page */
  479. if (umem->context->invalidate_range || !stored_page)
  480. put_page(page);
  481. if (remove_existing_mapping && umem->context->invalidate_range) {
  482. invalidate_page_trampoline(
  483. umem_odp,
  484. ib_umem_start(umem) + (page_index >> umem->page_shift),
  485. ib_umem_start(umem) + ((page_index + 1) >>
  486. umem->page_shift),
  487. NULL);
  488. ret = -EAGAIN;
  489. }
  490. return ret;
  491. }
  492. /**
  493. * ib_umem_odp_map_dma_pages - Pin and DMA map userspace memory in an ODP MR.
  494. *
  495. * Pins the range of pages passed in the argument, and maps them to
  496. * DMA addresses. The DMA addresses of the mapped pages is updated in
  497. * umem_odp->dma_list.
  498. *
  499. * Returns the number of pages mapped in success, negative error code
  500. * for failure.
  501. * An -EAGAIN error code is returned when a concurrent mmu notifier prevents
  502. * the function from completing its task.
  503. * An -ENOENT error code indicates that userspace process is being terminated
  504. * and mm was already destroyed.
  505. * @umem_odp: the umem to map and pin
  506. * @user_virt: the address from which we need to map.
  507. * @bcnt: the minimal number of bytes to pin and map. The mapping might be
  508. * bigger due to alignment, and may also be smaller in case of an error
  509. * pinning or mapping a page. The actual pages mapped is returned in
  510. * the return value.
  511. * @access_mask: bit mask of the requested access permissions for the given
  512. * range.
  513. * @current_seq: the MMU notifiers sequance value for synchronization with
  514. * invalidations. the sequance number is read from
  515. * umem_odp->notifiers_seq before calling this function
  516. */
  517. int ib_umem_odp_map_dma_pages(struct ib_umem_odp *umem_odp, u64 user_virt,
  518. u64 bcnt, u64 access_mask,
  519. unsigned long current_seq)
  520. {
  521. struct ib_umem *umem = &umem_odp->umem;
  522. struct task_struct *owning_process = NULL;
  523. struct mm_struct *owning_mm = umem_odp->umem.owning_mm;
  524. struct page **local_page_list = NULL;
  525. u64 page_mask, off;
  526. int j, k, ret = 0, start_idx, npages = 0, page_shift;
  527. unsigned int flags = 0;
  528. phys_addr_t p = 0;
  529. if (access_mask == 0)
  530. return -EINVAL;
  531. if (user_virt < ib_umem_start(umem) ||
  532. user_virt + bcnt > ib_umem_end(umem))
  533. return -EFAULT;
  534. local_page_list = (struct page **)__get_free_page(GFP_KERNEL);
  535. if (!local_page_list)
  536. return -ENOMEM;
  537. page_shift = umem->page_shift;
  538. page_mask = ~(BIT(page_shift) - 1);
  539. off = user_virt & (~page_mask);
  540. user_virt = user_virt & page_mask;
  541. bcnt += off; /* Charge for the first page offset as well. */
  542. /*
  543. * owning_process is allowed to be NULL, this means somehow the mm is
  544. * existing beyond the lifetime of the originating process.. Presumably
  545. * mmget_not_zero will fail in this case.
  546. */
  547. owning_process = get_pid_task(umem_odp->per_mm->tgid, PIDTYPE_PID);
  548. if (WARN_ON(!mmget_not_zero(umem_odp->umem.owning_mm))) {
  549. ret = -EINVAL;
  550. goto out_put_task;
  551. }
  552. if (access_mask & ODP_WRITE_ALLOWED_BIT)
  553. flags |= FOLL_WRITE;
  554. start_idx = (user_virt - ib_umem_start(umem)) >> page_shift;
  555. k = start_idx;
  556. while (bcnt > 0) {
  557. const size_t gup_num_pages = min_t(size_t,
  558. (bcnt + BIT(page_shift) - 1) >> page_shift,
  559. PAGE_SIZE / sizeof(struct page *));
  560. down_read(&owning_mm->mmap_sem);
  561. /*
  562. * Note: this might result in redundent page getting. We can
  563. * avoid this by checking dma_list to be 0 before calling
  564. * get_user_pages. However, this make the code much more
  565. * complex (and doesn't gain us much performance in most use
  566. * cases).
  567. */
  568. npages = get_user_pages_remote(owning_process, owning_mm,
  569. user_virt, gup_num_pages,
  570. flags, local_page_list, NULL, NULL);
  571. up_read(&owning_mm->mmap_sem);
  572. if (npages < 0)
  573. break;
  574. bcnt -= min_t(size_t, npages << PAGE_SHIFT, bcnt);
  575. mutex_lock(&umem_odp->umem_mutex);
  576. for (j = 0; j < npages; j++, user_virt += PAGE_SIZE) {
  577. if (user_virt & ~page_mask) {
  578. p += PAGE_SIZE;
  579. if (page_to_phys(local_page_list[j]) != p) {
  580. ret = -EFAULT;
  581. break;
  582. }
  583. put_page(local_page_list[j]);
  584. continue;
  585. }
  586. ret = ib_umem_odp_map_dma_single_page(
  587. umem_odp, k, local_page_list[j],
  588. access_mask, current_seq);
  589. if (ret < 0)
  590. break;
  591. p = page_to_phys(local_page_list[j]);
  592. k++;
  593. }
  594. mutex_unlock(&umem_odp->umem_mutex);
  595. if (ret < 0) {
  596. /* Release left over pages when handling errors. */
  597. for (++j; j < npages; ++j)
  598. put_page(local_page_list[j]);
  599. break;
  600. }
  601. }
  602. if (ret >= 0) {
  603. if (npages < 0 && k == start_idx)
  604. ret = npages;
  605. else
  606. ret = k - start_idx;
  607. }
  608. mmput(owning_mm);
  609. out_put_task:
  610. if (owning_process)
  611. put_task_struct(owning_process);
  612. free_page((unsigned long)local_page_list);
  613. return ret;
  614. }
  615. EXPORT_SYMBOL(ib_umem_odp_map_dma_pages);
  616. void ib_umem_odp_unmap_dma_pages(struct ib_umem_odp *umem_odp, u64 virt,
  617. u64 bound)
  618. {
  619. struct ib_umem *umem = &umem_odp->umem;
  620. int idx;
  621. u64 addr;
  622. struct ib_device *dev = umem->context->device;
  623. virt = max_t(u64, virt, ib_umem_start(umem));
  624. bound = min_t(u64, bound, ib_umem_end(umem));
  625. /* Note that during the run of this function, the
  626. * notifiers_count of the MR is > 0, preventing any racing
  627. * faults from completion. We might be racing with other
  628. * invalidations, so we must make sure we free each page only
  629. * once. */
  630. mutex_lock(&umem_odp->umem_mutex);
  631. for (addr = virt; addr < bound; addr += BIT(umem->page_shift)) {
  632. idx = (addr - ib_umem_start(umem)) >> umem->page_shift;
  633. if (umem_odp->page_list[idx]) {
  634. struct page *page = umem_odp->page_list[idx];
  635. dma_addr_t dma = umem_odp->dma_list[idx];
  636. dma_addr_t dma_addr = dma & ODP_DMA_ADDR_MASK;
  637. WARN_ON(!dma_addr);
  638. ib_dma_unmap_page(dev, dma_addr, PAGE_SIZE,
  639. DMA_BIDIRECTIONAL);
  640. if (dma & ODP_WRITE_ALLOWED_BIT) {
  641. struct page *head_page = compound_head(page);
  642. /*
  643. * set_page_dirty prefers being called with
  644. * the page lock. However, MMU notifiers are
  645. * called sometimes with and sometimes without
  646. * the lock. We rely on the umem_mutex instead
  647. * to prevent other mmu notifiers from
  648. * continuing and allowing the page mapping to
  649. * be removed.
  650. */
  651. set_page_dirty(head_page);
  652. }
  653. /* on demand pinning support */
  654. if (!umem->context->invalidate_range)
  655. put_page(page);
  656. umem_odp->page_list[idx] = NULL;
  657. umem_odp->dma_list[idx] = 0;
  658. umem->npages--;
  659. }
  660. }
  661. mutex_unlock(&umem_odp->umem_mutex);
  662. }
  663. EXPORT_SYMBOL(ib_umem_odp_unmap_dma_pages);
  664. /* @last is not a part of the interval. See comment for function
  665. * node_last.
  666. */
  667. int rbt_ib_umem_for_each_in_range(struct rb_root_cached *root,
  668. u64 start, u64 last,
  669. umem_call_back cb,
  670. bool blockable,
  671. void *cookie)
  672. {
  673. int ret_val = 0;
  674. struct umem_odp_node *node, *next;
  675. struct ib_umem_odp *umem;
  676. if (unlikely(start == last))
  677. return ret_val;
  678. for (node = rbt_ib_umem_iter_first(root, start, last - 1);
  679. node; node = next) {
  680. /* TODO move the blockable decision up to the callback */
  681. if (!blockable)
  682. return -EAGAIN;
  683. next = rbt_ib_umem_iter_next(node, start, last - 1);
  684. umem = container_of(node, struct ib_umem_odp, interval_tree);
  685. ret_val = cb(umem, start, last, cookie) || ret_val;
  686. }
  687. return ret_val;
  688. }
  689. EXPORT_SYMBOL(rbt_ib_umem_for_each_in_range);
  690. struct ib_umem_odp *rbt_ib_umem_lookup(struct rb_root_cached *root,
  691. u64 addr, u64 length)
  692. {
  693. struct umem_odp_node *node;
  694. node = rbt_ib_umem_iter_first(root, addr, addr + length - 1);
  695. if (node)
  696. return container_of(node, struct ib_umem_odp, interval_tree);
  697. return NULL;
  698. }
  699. EXPORT_SYMBOL(rbt_ib_umem_lookup);