umem_odp.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852
  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 *item)
  72. {
  73. mutex_lock(&item->odp_data->umem_mutex);
  74. /* Only update private counters for this umem if it has them.
  75. * Otherwise skip it. All page faults will be delayed for this umem. */
  76. if (item->odp_data->mn_counters_active) {
  77. int notifiers_count = item->odp_data->notifiers_count++;
  78. if (notifiers_count == 0)
  79. /* Initialize the completion object for waiting on
  80. * notifiers. Since notifier_count is zero, no one
  81. * should be waiting right now. */
  82. reinit_completion(&item->odp_data->notifier_completion);
  83. }
  84. mutex_unlock(&item->odp_data->umem_mutex);
  85. }
  86. static void ib_umem_notifier_end_account(struct ib_umem *item)
  87. {
  88. mutex_lock(&item->odp_data->umem_mutex);
  89. /* Only update private counters for this umem if it has them.
  90. * Otherwise skip it. All page faults will be delayed for this umem. */
  91. if (item->odp_data->mn_counters_active) {
  92. /*
  93. * This sequence increase will notify the QP page fault that
  94. * the page that is going to be mapped in the spte could have
  95. * been freed.
  96. */
  97. ++item->odp_data->notifiers_seq;
  98. if (--item->odp_data->notifiers_count == 0)
  99. complete_all(&item->odp_data->notifier_completion);
  100. }
  101. mutex_unlock(&item->odp_data->umem_mutex);
  102. }
  103. /* Account for a new mmu notifier in an ib_ucontext. */
  104. static void ib_ucontext_notifier_start_account(struct ib_ucontext *context)
  105. {
  106. atomic_inc(&context->notifier_count);
  107. }
  108. /* Account for a terminating mmu notifier in an ib_ucontext.
  109. *
  110. * Must be called with the ib_ucontext->umem_rwsem semaphore unlocked, since
  111. * the function takes the semaphore itself. */
  112. static void ib_ucontext_notifier_end_account(struct ib_ucontext *context)
  113. {
  114. int zero_notifiers = atomic_dec_and_test(&context->notifier_count);
  115. if (zero_notifiers &&
  116. !list_empty(&context->no_private_counters)) {
  117. /* No currently running mmu notifiers. Now is the chance to
  118. * add private accounting to all previously added umems. */
  119. struct ib_umem_odp *odp_data, *next;
  120. /* Prevent concurrent mmu notifiers from working on the
  121. * no_private_counters list. */
  122. down_write(&context->umem_rwsem);
  123. /* Read the notifier_count again, with the umem_rwsem
  124. * semaphore taken for write. */
  125. if (!atomic_read(&context->notifier_count)) {
  126. list_for_each_entry_safe(odp_data, next,
  127. &context->no_private_counters,
  128. no_private_counters) {
  129. mutex_lock(&odp_data->umem_mutex);
  130. odp_data->mn_counters_active = true;
  131. list_del(&odp_data->no_private_counters);
  132. complete_all(&odp_data->notifier_completion);
  133. mutex_unlock(&odp_data->umem_mutex);
  134. }
  135. }
  136. up_write(&context->umem_rwsem);
  137. }
  138. }
  139. static int ib_umem_notifier_release_trampoline(struct ib_umem *item, u64 start,
  140. u64 end, void *cookie) {
  141. /*
  142. * Increase the number of notifiers running, to
  143. * prevent any further fault handling on this MR.
  144. */
  145. ib_umem_notifier_start_account(item);
  146. item->odp_data->dying = 1;
  147. /* Make sure that the fact the umem is dying is out before we release
  148. * all pending page faults. */
  149. smp_wmb();
  150. complete_all(&item->odp_data->notifier_completion);
  151. item->context->invalidate_range(item, ib_umem_start(item),
  152. ib_umem_end(item));
  153. return 0;
  154. }
  155. static void ib_umem_notifier_release(struct mmu_notifier *mn,
  156. struct mm_struct *mm)
  157. {
  158. struct ib_ucontext *context = container_of(mn, struct ib_ucontext, mn);
  159. if (!context->invalidate_range)
  160. return;
  161. ib_ucontext_notifier_start_account(context);
  162. down_read(&context->umem_rwsem);
  163. rbt_ib_umem_for_each_in_range(&context->umem_tree, 0,
  164. ULLONG_MAX,
  165. ib_umem_notifier_release_trampoline,
  166. true,
  167. NULL);
  168. up_read(&context->umem_rwsem);
  169. }
  170. static int invalidate_page_trampoline(struct ib_umem *item, u64 start,
  171. u64 end, void *cookie)
  172. {
  173. ib_umem_notifier_start_account(item);
  174. item->context->invalidate_range(item, start, start + PAGE_SIZE);
  175. ib_umem_notifier_end_account(item);
  176. return 0;
  177. }
  178. static int invalidate_range_start_trampoline(struct ib_umem *item, u64 start,
  179. u64 end, void *cookie)
  180. {
  181. ib_umem_notifier_start_account(item);
  182. item->context->invalidate_range(item, start, end);
  183. return 0;
  184. }
  185. static int ib_umem_notifier_invalidate_range_start(struct mmu_notifier *mn,
  186. struct mm_struct *mm,
  187. unsigned long start,
  188. unsigned long end,
  189. bool blockable)
  190. {
  191. struct ib_ucontext *context = container_of(mn, struct ib_ucontext, mn);
  192. int ret;
  193. if (!context->invalidate_range)
  194. return 0;
  195. if (blockable)
  196. down_read(&context->umem_rwsem);
  197. else if (!down_read_trylock(&context->umem_rwsem))
  198. return -EAGAIN;
  199. ib_ucontext_notifier_start_account(context);
  200. ret = rbt_ib_umem_for_each_in_range(&context->umem_tree, start,
  201. end,
  202. invalidate_range_start_trampoline,
  203. blockable, NULL);
  204. up_read(&context->umem_rwsem);
  205. return ret;
  206. }
  207. static int invalidate_range_end_trampoline(struct ib_umem *item, u64 start,
  208. u64 end, void *cookie)
  209. {
  210. ib_umem_notifier_end_account(item);
  211. return 0;
  212. }
  213. static void ib_umem_notifier_invalidate_range_end(struct mmu_notifier *mn,
  214. struct mm_struct *mm,
  215. unsigned long start,
  216. unsigned long end)
  217. {
  218. struct ib_ucontext *context = container_of(mn, struct ib_ucontext, mn);
  219. if (!context->invalidate_range)
  220. return;
  221. /*
  222. * TODO: we currently bail out if there is any sleepable work to be done
  223. * in ib_umem_notifier_invalidate_range_start so we shouldn't really block
  224. * here. But this is ugly and fragile.
  225. */
  226. down_read(&context->umem_rwsem);
  227. rbt_ib_umem_for_each_in_range(&context->umem_tree, start,
  228. end,
  229. invalidate_range_end_trampoline, true, NULL);
  230. up_read(&context->umem_rwsem);
  231. ib_ucontext_notifier_end_account(context);
  232. }
  233. static const struct mmu_notifier_ops ib_umem_notifiers = {
  234. .release = ib_umem_notifier_release,
  235. .invalidate_range_start = ib_umem_notifier_invalidate_range_start,
  236. .invalidate_range_end = ib_umem_notifier_invalidate_range_end,
  237. };
  238. struct ib_umem *ib_alloc_odp_umem(struct ib_ucontext *context,
  239. unsigned long addr,
  240. size_t size)
  241. {
  242. struct ib_umem *umem;
  243. struct ib_umem_odp *odp_data;
  244. int pages = size >> PAGE_SHIFT;
  245. int ret;
  246. umem = kzalloc(sizeof(*umem), GFP_KERNEL);
  247. if (!umem)
  248. return ERR_PTR(-ENOMEM);
  249. umem->context = context;
  250. umem->length = size;
  251. umem->address = addr;
  252. umem->page_shift = PAGE_SHIFT;
  253. umem->writable = 1;
  254. odp_data = kzalloc(sizeof(*odp_data), GFP_KERNEL);
  255. if (!odp_data) {
  256. ret = -ENOMEM;
  257. goto out_umem;
  258. }
  259. odp_data->umem = umem;
  260. mutex_init(&odp_data->umem_mutex);
  261. init_completion(&odp_data->notifier_completion);
  262. odp_data->page_list =
  263. vzalloc(array_size(pages, sizeof(*odp_data->page_list)));
  264. if (!odp_data->page_list) {
  265. ret = -ENOMEM;
  266. goto out_odp_data;
  267. }
  268. odp_data->dma_list =
  269. vzalloc(array_size(pages, sizeof(*odp_data->dma_list)));
  270. if (!odp_data->dma_list) {
  271. ret = -ENOMEM;
  272. goto out_page_list;
  273. }
  274. down_write(&context->umem_rwsem);
  275. context->odp_mrs_count++;
  276. rbt_ib_umem_insert(&odp_data->interval_tree, &context->umem_tree);
  277. if (likely(!atomic_read(&context->notifier_count)))
  278. odp_data->mn_counters_active = true;
  279. else
  280. list_add(&odp_data->no_private_counters,
  281. &context->no_private_counters);
  282. up_write(&context->umem_rwsem);
  283. umem->odp_data = odp_data;
  284. return umem;
  285. out_page_list:
  286. vfree(odp_data->page_list);
  287. out_odp_data:
  288. kfree(odp_data);
  289. out_umem:
  290. kfree(umem);
  291. return ERR_PTR(ret);
  292. }
  293. EXPORT_SYMBOL(ib_alloc_odp_umem);
  294. int ib_umem_odp_get(struct ib_ucontext *context, struct ib_umem *umem,
  295. int access)
  296. {
  297. int ret_val;
  298. struct pid *our_pid;
  299. struct mm_struct *mm = get_task_mm(current);
  300. if (!mm)
  301. return -EINVAL;
  302. if (access & IB_ACCESS_HUGETLB) {
  303. struct vm_area_struct *vma;
  304. struct hstate *h;
  305. down_read(&mm->mmap_sem);
  306. vma = find_vma(mm, ib_umem_start(umem));
  307. if (!vma || !is_vm_hugetlb_page(vma)) {
  308. up_read(&mm->mmap_sem);
  309. return -EINVAL;
  310. }
  311. h = hstate_vma(vma);
  312. umem->page_shift = huge_page_shift(h);
  313. up_read(&mm->mmap_sem);
  314. umem->hugetlb = 1;
  315. } else {
  316. umem->hugetlb = 0;
  317. }
  318. /* Prevent creating ODP MRs in child processes */
  319. rcu_read_lock();
  320. our_pid = get_task_pid(current->group_leader, PIDTYPE_PID);
  321. rcu_read_unlock();
  322. put_pid(our_pid);
  323. if (context->tgid != our_pid) {
  324. ret_val = -EINVAL;
  325. goto out_mm;
  326. }
  327. umem->odp_data = kzalloc(sizeof(*umem->odp_data), GFP_KERNEL);
  328. if (!umem->odp_data) {
  329. ret_val = -ENOMEM;
  330. goto out_mm;
  331. }
  332. umem->odp_data->umem = umem;
  333. mutex_init(&umem->odp_data->umem_mutex);
  334. init_completion(&umem->odp_data->notifier_completion);
  335. if (ib_umem_num_pages(umem)) {
  336. umem->odp_data->page_list =
  337. vzalloc(array_size(sizeof(*umem->odp_data->page_list),
  338. ib_umem_num_pages(umem)));
  339. if (!umem->odp_data->page_list) {
  340. ret_val = -ENOMEM;
  341. goto out_odp_data;
  342. }
  343. umem->odp_data->dma_list =
  344. vzalloc(array_size(sizeof(*umem->odp_data->dma_list),
  345. ib_umem_num_pages(umem)));
  346. if (!umem->odp_data->dma_list) {
  347. ret_val = -ENOMEM;
  348. goto out_page_list;
  349. }
  350. }
  351. /*
  352. * When using MMU notifiers, we will get a
  353. * notification before the "current" task (and MM) is
  354. * destroyed. We use the umem_rwsem semaphore to synchronize.
  355. */
  356. down_write(&context->umem_rwsem);
  357. context->odp_mrs_count++;
  358. if (likely(ib_umem_start(umem) != ib_umem_end(umem)))
  359. rbt_ib_umem_insert(&umem->odp_data->interval_tree,
  360. &context->umem_tree);
  361. if (likely(!atomic_read(&context->notifier_count)) ||
  362. context->odp_mrs_count == 1)
  363. umem->odp_data->mn_counters_active = true;
  364. else
  365. list_add(&umem->odp_data->no_private_counters,
  366. &context->no_private_counters);
  367. downgrade_write(&context->umem_rwsem);
  368. if (context->odp_mrs_count == 1) {
  369. /*
  370. * Note that at this point, no MMU notifier is running
  371. * for this context!
  372. */
  373. atomic_set(&context->notifier_count, 0);
  374. INIT_HLIST_NODE(&context->mn.hlist);
  375. context->mn.ops = &ib_umem_notifiers;
  376. /*
  377. * Lock-dep detects a false positive for mmap_sem vs.
  378. * umem_rwsem, due to not grasping downgrade_write correctly.
  379. */
  380. lockdep_off();
  381. ret_val = mmu_notifier_register(&context->mn, mm);
  382. lockdep_on();
  383. if (ret_val) {
  384. pr_err("Failed to register mmu_notifier %d\n", ret_val);
  385. ret_val = -EBUSY;
  386. goto out_mutex;
  387. }
  388. }
  389. up_read(&context->umem_rwsem);
  390. /*
  391. * Note that doing an mmput can cause a notifier for the relevant mm.
  392. * If the notifier is called while we hold the umem_rwsem, this will
  393. * cause a deadlock. Therefore, we release the reference only after we
  394. * released the semaphore.
  395. */
  396. mmput(mm);
  397. return 0;
  398. out_mutex:
  399. up_read(&context->umem_rwsem);
  400. vfree(umem->odp_data->dma_list);
  401. out_page_list:
  402. vfree(umem->odp_data->page_list);
  403. out_odp_data:
  404. kfree(umem->odp_data);
  405. out_mm:
  406. mmput(mm);
  407. return ret_val;
  408. }
  409. void ib_umem_odp_release(struct ib_umem *umem)
  410. {
  411. struct ib_ucontext *context = umem->context;
  412. /*
  413. * Ensure that no more pages are mapped in the umem.
  414. *
  415. * It is the driver's responsibility to ensure, before calling us,
  416. * that the hardware will not attempt to access the MR any more.
  417. */
  418. ib_umem_odp_unmap_dma_pages(umem, ib_umem_start(umem),
  419. ib_umem_end(umem));
  420. down_write(&context->umem_rwsem);
  421. if (likely(ib_umem_start(umem) != ib_umem_end(umem)))
  422. rbt_ib_umem_remove(&umem->odp_data->interval_tree,
  423. &context->umem_tree);
  424. context->odp_mrs_count--;
  425. if (!umem->odp_data->mn_counters_active) {
  426. list_del(&umem->odp_data->no_private_counters);
  427. complete_all(&umem->odp_data->notifier_completion);
  428. }
  429. /*
  430. * Downgrade the lock to a read lock. This ensures that the notifiers
  431. * (who lock the mutex for reading) will be able to finish, and we
  432. * will be able to enventually obtain the mmu notifiers SRCU. Note
  433. * that since we are doing it atomically, no other user could register
  434. * and unregister while we do the check.
  435. */
  436. downgrade_write(&context->umem_rwsem);
  437. if (!context->odp_mrs_count) {
  438. struct task_struct *owning_process = NULL;
  439. struct mm_struct *owning_mm = NULL;
  440. owning_process = get_pid_task(context->tgid,
  441. PIDTYPE_PID);
  442. if (owning_process == NULL)
  443. /*
  444. * The process is already dead, notifier were removed
  445. * already.
  446. */
  447. goto out;
  448. owning_mm = get_task_mm(owning_process);
  449. if (owning_mm == NULL)
  450. /*
  451. * The process' mm is already dead, notifier were
  452. * removed already.
  453. */
  454. goto out_put_task;
  455. mmu_notifier_unregister(&context->mn, owning_mm);
  456. mmput(owning_mm);
  457. out_put_task:
  458. put_task_struct(owning_process);
  459. }
  460. out:
  461. up_read(&context->umem_rwsem);
  462. vfree(umem->odp_data->dma_list);
  463. vfree(umem->odp_data->page_list);
  464. kfree(umem->odp_data);
  465. kfree(umem);
  466. }
  467. /*
  468. * Map for DMA and insert a single page into the on-demand paging page tables.
  469. *
  470. * @umem: the umem to insert the page to.
  471. * @page_index: index in the umem to add the page to.
  472. * @page: the page struct to map and add.
  473. * @access_mask: access permissions needed for this page.
  474. * @current_seq: sequence number for synchronization with invalidations.
  475. * the sequence number is taken from
  476. * umem->odp_data->notifiers_seq.
  477. *
  478. * The function returns -EFAULT if the DMA mapping operation fails. It returns
  479. * -EAGAIN if a concurrent invalidation prevents us from updating the page.
  480. *
  481. * The page is released via put_page even if the operation failed. For
  482. * on-demand pinning, the page is released whenever it isn't stored in the
  483. * umem.
  484. */
  485. static int ib_umem_odp_map_dma_single_page(
  486. struct ib_umem *umem,
  487. int page_index,
  488. struct page *page,
  489. u64 access_mask,
  490. unsigned long current_seq)
  491. {
  492. struct ib_device *dev = umem->context->device;
  493. dma_addr_t dma_addr;
  494. int stored_page = 0;
  495. int remove_existing_mapping = 0;
  496. int ret = 0;
  497. /*
  498. * Note: we avoid writing if seq is different from the initial seq, to
  499. * handle case of a racing notifier. This check also allows us to bail
  500. * early if we have a notifier running in parallel with us.
  501. */
  502. if (ib_umem_mmu_notifier_retry(umem, current_seq)) {
  503. ret = -EAGAIN;
  504. goto out;
  505. }
  506. if (!(umem->odp_data->dma_list[page_index])) {
  507. dma_addr = ib_dma_map_page(dev,
  508. page,
  509. 0, BIT(umem->page_shift),
  510. DMA_BIDIRECTIONAL);
  511. if (ib_dma_mapping_error(dev, dma_addr)) {
  512. ret = -EFAULT;
  513. goto out;
  514. }
  515. umem->odp_data->dma_list[page_index] = dma_addr | access_mask;
  516. umem->odp_data->page_list[page_index] = page;
  517. umem->npages++;
  518. stored_page = 1;
  519. } else if (umem->odp_data->page_list[page_index] == page) {
  520. umem->odp_data->dma_list[page_index] |= access_mask;
  521. } else {
  522. pr_err("error: got different pages in IB device and from get_user_pages. IB device page: %p, gup page: %p\n",
  523. umem->odp_data->page_list[page_index], page);
  524. /* Better remove the mapping now, to prevent any further
  525. * damage. */
  526. remove_existing_mapping = 1;
  527. }
  528. out:
  529. /* On Demand Paging - avoid pinning the page */
  530. if (umem->context->invalidate_range || !stored_page)
  531. put_page(page);
  532. if (remove_existing_mapping && umem->context->invalidate_range) {
  533. invalidate_page_trampoline(
  534. umem,
  535. ib_umem_start(umem) + (page_index >> umem->page_shift),
  536. ib_umem_start(umem) + ((page_index + 1) >>
  537. umem->page_shift),
  538. NULL);
  539. ret = -EAGAIN;
  540. }
  541. return ret;
  542. }
  543. /**
  544. * ib_umem_odp_map_dma_pages - Pin and DMA map userspace memory in an ODP MR.
  545. *
  546. * Pins the range of pages passed in the argument, and maps them to
  547. * DMA addresses. The DMA addresses of the mapped pages is updated in
  548. * umem->odp_data->dma_list.
  549. *
  550. * Returns the number of pages mapped in success, negative error code
  551. * for failure.
  552. * An -EAGAIN error code is returned when a concurrent mmu notifier prevents
  553. * the function from completing its task.
  554. * An -ENOENT error code indicates that userspace process is being terminated
  555. * and mm was already destroyed.
  556. * @umem: the umem to map and pin
  557. * @user_virt: the address from which we need to map.
  558. * @bcnt: the minimal number of bytes to pin and map. The mapping might be
  559. * bigger due to alignment, and may also be smaller in case of an error
  560. * pinning or mapping a page. The actual pages mapped is returned in
  561. * the return value.
  562. * @access_mask: bit mask of the requested access permissions for the given
  563. * range.
  564. * @current_seq: the MMU notifiers sequance value for synchronization with
  565. * invalidations. the sequance number is read from
  566. * umem->odp_data->notifiers_seq before calling this function
  567. */
  568. int ib_umem_odp_map_dma_pages(struct ib_umem *umem, u64 user_virt, u64 bcnt,
  569. u64 access_mask, unsigned long current_seq)
  570. {
  571. struct task_struct *owning_process = NULL;
  572. struct mm_struct *owning_mm = NULL;
  573. struct page **local_page_list = NULL;
  574. u64 page_mask, off;
  575. int j, k, ret = 0, start_idx, npages = 0, page_shift;
  576. unsigned int flags = 0;
  577. phys_addr_t p = 0;
  578. if (access_mask == 0)
  579. return -EINVAL;
  580. if (user_virt < ib_umem_start(umem) ||
  581. user_virt + bcnt > ib_umem_end(umem))
  582. return -EFAULT;
  583. local_page_list = (struct page **)__get_free_page(GFP_KERNEL);
  584. if (!local_page_list)
  585. return -ENOMEM;
  586. page_shift = umem->page_shift;
  587. page_mask = ~(BIT(page_shift) - 1);
  588. off = user_virt & (~page_mask);
  589. user_virt = user_virt & page_mask;
  590. bcnt += off; /* Charge for the first page offset as well. */
  591. owning_process = get_pid_task(umem->context->tgid, PIDTYPE_PID);
  592. if (owning_process == NULL) {
  593. ret = -EINVAL;
  594. goto out_no_task;
  595. }
  596. owning_mm = get_task_mm(owning_process);
  597. if (owning_mm == NULL) {
  598. ret = -ENOENT;
  599. goto out_put_task;
  600. }
  601. if (access_mask & ODP_WRITE_ALLOWED_BIT)
  602. flags |= FOLL_WRITE;
  603. start_idx = (user_virt - ib_umem_start(umem)) >> page_shift;
  604. k = start_idx;
  605. while (bcnt > 0) {
  606. const size_t gup_num_pages = min_t(size_t,
  607. ALIGN(bcnt, PAGE_SIZE) / PAGE_SIZE,
  608. PAGE_SIZE / sizeof(struct page *));
  609. down_read(&owning_mm->mmap_sem);
  610. /*
  611. * Note: this might result in redundent page getting. We can
  612. * avoid this by checking dma_list to be 0 before calling
  613. * get_user_pages. However, this make the code much more
  614. * complex (and doesn't gain us much performance in most use
  615. * cases).
  616. */
  617. npages = get_user_pages_remote(owning_process, owning_mm,
  618. user_virt, gup_num_pages,
  619. flags, local_page_list, NULL, NULL);
  620. up_read(&owning_mm->mmap_sem);
  621. if (npages < 0)
  622. break;
  623. bcnt -= min_t(size_t, npages << PAGE_SHIFT, bcnt);
  624. mutex_lock(&umem->odp_data->umem_mutex);
  625. for (j = 0; j < npages; j++, user_virt += PAGE_SIZE) {
  626. if (user_virt & ~page_mask) {
  627. p += PAGE_SIZE;
  628. if (page_to_phys(local_page_list[j]) != p) {
  629. ret = -EFAULT;
  630. break;
  631. }
  632. put_page(local_page_list[j]);
  633. continue;
  634. }
  635. ret = ib_umem_odp_map_dma_single_page(
  636. umem, k, local_page_list[j],
  637. access_mask, current_seq);
  638. if (ret < 0)
  639. break;
  640. p = page_to_phys(local_page_list[j]);
  641. k++;
  642. }
  643. mutex_unlock(&umem->odp_data->umem_mutex);
  644. if (ret < 0) {
  645. /* Release left over pages when handling errors. */
  646. for (++j; j < npages; ++j)
  647. put_page(local_page_list[j]);
  648. break;
  649. }
  650. }
  651. if (ret >= 0) {
  652. if (npages < 0 && k == start_idx)
  653. ret = npages;
  654. else
  655. ret = k - start_idx;
  656. }
  657. mmput(owning_mm);
  658. out_put_task:
  659. put_task_struct(owning_process);
  660. out_no_task:
  661. free_page((unsigned long)local_page_list);
  662. return ret;
  663. }
  664. EXPORT_SYMBOL(ib_umem_odp_map_dma_pages);
  665. void ib_umem_odp_unmap_dma_pages(struct ib_umem *umem, u64 virt,
  666. u64 bound)
  667. {
  668. int idx;
  669. u64 addr;
  670. struct ib_device *dev = umem->context->device;
  671. virt = max_t(u64, virt, ib_umem_start(umem));
  672. bound = min_t(u64, bound, ib_umem_end(umem));
  673. /* Note that during the run of this function, the
  674. * notifiers_count of the MR is > 0, preventing any racing
  675. * faults from completion. We might be racing with other
  676. * invalidations, so we must make sure we free each page only
  677. * once. */
  678. mutex_lock(&umem->odp_data->umem_mutex);
  679. for (addr = virt; addr < bound; addr += BIT(umem->page_shift)) {
  680. idx = (addr - ib_umem_start(umem)) >> umem->page_shift;
  681. if (umem->odp_data->page_list[idx]) {
  682. struct page *page = umem->odp_data->page_list[idx];
  683. dma_addr_t dma = umem->odp_data->dma_list[idx];
  684. dma_addr_t dma_addr = dma & ODP_DMA_ADDR_MASK;
  685. WARN_ON(!dma_addr);
  686. ib_dma_unmap_page(dev, dma_addr, PAGE_SIZE,
  687. DMA_BIDIRECTIONAL);
  688. if (dma & ODP_WRITE_ALLOWED_BIT) {
  689. struct page *head_page = compound_head(page);
  690. /*
  691. * set_page_dirty prefers being called with
  692. * the page lock. However, MMU notifiers are
  693. * called sometimes with and sometimes without
  694. * the lock. We rely on the umem_mutex instead
  695. * to prevent other mmu notifiers from
  696. * continuing and allowing the page mapping to
  697. * be removed.
  698. */
  699. set_page_dirty(head_page);
  700. }
  701. /* on demand pinning support */
  702. if (!umem->context->invalidate_range)
  703. put_page(page);
  704. umem->odp_data->page_list[idx] = NULL;
  705. umem->odp_data->dma_list[idx] = 0;
  706. umem->npages--;
  707. }
  708. }
  709. mutex_unlock(&umem->odp_data->umem_mutex);
  710. }
  711. EXPORT_SYMBOL(ib_umem_odp_unmap_dma_pages);
  712. /* @last is not a part of the interval. See comment for function
  713. * node_last.
  714. */
  715. int rbt_ib_umem_for_each_in_range(struct rb_root_cached *root,
  716. u64 start, u64 last,
  717. umem_call_back cb,
  718. bool blockable,
  719. void *cookie)
  720. {
  721. int ret_val = 0;
  722. struct umem_odp_node *node, *next;
  723. struct ib_umem_odp *umem;
  724. if (unlikely(start == last))
  725. return ret_val;
  726. for (node = rbt_ib_umem_iter_first(root, start, last - 1);
  727. node; node = next) {
  728. /* TODO move the blockable decision up to the callback */
  729. if (!blockable)
  730. return -EAGAIN;
  731. next = rbt_ib_umem_iter_next(node, start, last - 1);
  732. umem = container_of(node, struct ib_umem_odp, interval_tree);
  733. ret_val = cb(umem->umem, start, last, cookie) || ret_val;
  734. }
  735. return ret_val;
  736. }
  737. EXPORT_SYMBOL(rbt_ib_umem_for_each_in_range);
  738. struct ib_umem_odp *rbt_ib_umem_lookup(struct rb_root_cached *root,
  739. u64 addr, u64 length)
  740. {
  741. struct umem_odp_node *node;
  742. node = rbt_ib_umem_iter_first(root, addr, addr + length - 1);
  743. if (node)
  744. return container_of(node, struct ib_umem_odp, interval_tree);
  745. return NULL;
  746. }
  747. EXPORT_SYMBOL(rbt_ib_umem_lookup);