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. ret_val = -EINVAL;
  310. goto out_mm;
  311. }
  312. h = hstate_vma(vma);
  313. umem->page_shift = huge_page_shift(h);
  314. up_read(&mm->mmap_sem);
  315. umem->hugetlb = 1;
  316. } else {
  317. umem->hugetlb = 0;
  318. }
  319. /* Prevent creating ODP MRs in child processes */
  320. rcu_read_lock();
  321. our_pid = get_task_pid(current->group_leader, PIDTYPE_PID);
  322. rcu_read_unlock();
  323. put_pid(our_pid);
  324. if (context->tgid != our_pid) {
  325. ret_val = -EINVAL;
  326. goto out_mm;
  327. }
  328. umem->odp_data = kzalloc(sizeof(*umem->odp_data), GFP_KERNEL);
  329. if (!umem->odp_data) {
  330. ret_val = -ENOMEM;
  331. goto out_mm;
  332. }
  333. umem->odp_data->umem = umem;
  334. mutex_init(&umem->odp_data->umem_mutex);
  335. init_completion(&umem->odp_data->notifier_completion);
  336. if (ib_umem_num_pages(umem)) {
  337. umem->odp_data->page_list =
  338. vzalloc(array_size(sizeof(*umem->odp_data->page_list),
  339. ib_umem_num_pages(umem)));
  340. if (!umem->odp_data->page_list) {
  341. ret_val = -ENOMEM;
  342. goto out_odp_data;
  343. }
  344. umem->odp_data->dma_list =
  345. vzalloc(array_size(sizeof(*umem->odp_data->dma_list),
  346. ib_umem_num_pages(umem)));
  347. if (!umem->odp_data->dma_list) {
  348. ret_val = -ENOMEM;
  349. goto out_page_list;
  350. }
  351. }
  352. /*
  353. * When using MMU notifiers, we will get a
  354. * notification before the "current" task (and MM) is
  355. * destroyed. We use the umem_rwsem semaphore to synchronize.
  356. */
  357. down_write(&context->umem_rwsem);
  358. context->odp_mrs_count++;
  359. if (likely(ib_umem_start(umem) != ib_umem_end(umem)))
  360. rbt_ib_umem_insert(&umem->odp_data->interval_tree,
  361. &context->umem_tree);
  362. if (likely(!atomic_read(&context->notifier_count)) ||
  363. context->odp_mrs_count == 1)
  364. umem->odp_data->mn_counters_active = true;
  365. else
  366. list_add(&umem->odp_data->no_private_counters,
  367. &context->no_private_counters);
  368. downgrade_write(&context->umem_rwsem);
  369. if (context->odp_mrs_count == 1) {
  370. /*
  371. * Note that at this point, no MMU notifier is running
  372. * for this context!
  373. */
  374. atomic_set(&context->notifier_count, 0);
  375. INIT_HLIST_NODE(&context->mn.hlist);
  376. context->mn.ops = &ib_umem_notifiers;
  377. /*
  378. * Lock-dep detects a false positive for mmap_sem vs.
  379. * umem_rwsem, due to not grasping downgrade_write correctly.
  380. */
  381. lockdep_off();
  382. ret_val = mmu_notifier_register(&context->mn, mm);
  383. lockdep_on();
  384. if (ret_val) {
  385. pr_err("Failed to register mmu_notifier %d\n", ret_val);
  386. ret_val = -EBUSY;
  387. goto out_mutex;
  388. }
  389. }
  390. up_read(&context->umem_rwsem);
  391. /*
  392. * Note that doing an mmput can cause a notifier for the relevant mm.
  393. * If the notifier is called while we hold the umem_rwsem, this will
  394. * cause a deadlock. Therefore, we release the reference only after we
  395. * released the semaphore.
  396. */
  397. mmput(mm);
  398. return 0;
  399. out_mutex:
  400. up_read(&context->umem_rwsem);
  401. vfree(umem->odp_data->dma_list);
  402. out_page_list:
  403. vfree(umem->odp_data->page_list);
  404. out_odp_data:
  405. kfree(umem->odp_data);
  406. out_mm:
  407. mmput(mm);
  408. return ret_val;
  409. }
  410. void ib_umem_odp_release(struct ib_umem *umem)
  411. {
  412. struct ib_ucontext *context = umem->context;
  413. /*
  414. * Ensure that no more pages are mapped in the umem.
  415. *
  416. * It is the driver's responsibility to ensure, before calling us,
  417. * that the hardware will not attempt to access the MR any more.
  418. */
  419. ib_umem_odp_unmap_dma_pages(umem, ib_umem_start(umem),
  420. ib_umem_end(umem));
  421. down_write(&context->umem_rwsem);
  422. if (likely(ib_umem_start(umem) != ib_umem_end(umem)))
  423. rbt_ib_umem_remove(&umem->odp_data->interval_tree,
  424. &context->umem_tree);
  425. context->odp_mrs_count--;
  426. if (!umem->odp_data->mn_counters_active) {
  427. list_del(&umem->odp_data->no_private_counters);
  428. complete_all(&umem->odp_data->notifier_completion);
  429. }
  430. /*
  431. * Downgrade the lock to a read lock. This ensures that the notifiers
  432. * (who lock the mutex for reading) will be able to finish, and we
  433. * will be able to enventually obtain the mmu notifiers SRCU. Note
  434. * that since we are doing it atomically, no other user could register
  435. * and unregister while we do the check.
  436. */
  437. downgrade_write(&context->umem_rwsem);
  438. if (!context->odp_mrs_count) {
  439. struct task_struct *owning_process = NULL;
  440. struct mm_struct *owning_mm = NULL;
  441. owning_process = get_pid_task(context->tgid,
  442. PIDTYPE_PID);
  443. if (owning_process == NULL)
  444. /*
  445. * The process is already dead, notifier were removed
  446. * already.
  447. */
  448. goto out;
  449. owning_mm = get_task_mm(owning_process);
  450. if (owning_mm == NULL)
  451. /*
  452. * The process' mm is already dead, notifier were
  453. * removed already.
  454. */
  455. goto out_put_task;
  456. mmu_notifier_unregister(&context->mn, owning_mm);
  457. mmput(owning_mm);
  458. out_put_task:
  459. put_task_struct(owning_process);
  460. }
  461. out:
  462. up_read(&context->umem_rwsem);
  463. vfree(umem->odp_data->dma_list);
  464. vfree(umem->odp_data->page_list);
  465. kfree(umem->odp_data);
  466. kfree(umem);
  467. }
  468. /*
  469. * Map for DMA and insert a single page into the on-demand paging page tables.
  470. *
  471. * @umem: the umem to insert the page to.
  472. * @page_index: index in the umem to add the page to.
  473. * @page: the page struct to map and add.
  474. * @access_mask: access permissions needed for this page.
  475. * @current_seq: sequence number for synchronization with invalidations.
  476. * the sequence number is taken from
  477. * umem->odp_data->notifiers_seq.
  478. *
  479. * The function returns -EFAULT if the DMA mapping operation fails. It returns
  480. * -EAGAIN if a concurrent invalidation prevents us from updating the page.
  481. *
  482. * The page is released via put_page even if the operation failed. For
  483. * on-demand pinning, the page is released whenever it isn't stored in the
  484. * umem.
  485. */
  486. static int ib_umem_odp_map_dma_single_page(
  487. struct ib_umem *umem,
  488. int page_index,
  489. struct page *page,
  490. u64 access_mask,
  491. unsigned long current_seq)
  492. {
  493. struct ib_device *dev = umem->context->device;
  494. dma_addr_t dma_addr;
  495. int stored_page = 0;
  496. int remove_existing_mapping = 0;
  497. int ret = 0;
  498. /*
  499. * Note: we avoid writing if seq is different from the initial seq, to
  500. * handle case of a racing notifier. This check also allows us to bail
  501. * early if we have a notifier running in parallel with us.
  502. */
  503. if (ib_umem_mmu_notifier_retry(umem, current_seq)) {
  504. ret = -EAGAIN;
  505. goto out;
  506. }
  507. if (!(umem->odp_data->dma_list[page_index])) {
  508. dma_addr = ib_dma_map_page(dev,
  509. page,
  510. 0, BIT(umem->page_shift),
  511. DMA_BIDIRECTIONAL);
  512. if (ib_dma_mapping_error(dev, dma_addr)) {
  513. ret = -EFAULT;
  514. goto out;
  515. }
  516. umem->odp_data->dma_list[page_index] = dma_addr | access_mask;
  517. umem->odp_data->page_list[page_index] = page;
  518. umem->npages++;
  519. stored_page = 1;
  520. } else if (umem->odp_data->page_list[page_index] == page) {
  521. umem->odp_data->dma_list[page_index] |= access_mask;
  522. } else {
  523. pr_err("error: got different pages in IB device and from get_user_pages. IB device page: %p, gup page: %p\n",
  524. umem->odp_data->page_list[page_index], page);
  525. /* Better remove the mapping now, to prevent any further
  526. * damage. */
  527. remove_existing_mapping = 1;
  528. }
  529. out:
  530. /* On Demand Paging - avoid pinning the page */
  531. if (umem->context->invalidate_range || !stored_page)
  532. put_page(page);
  533. if (remove_existing_mapping && umem->context->invalidate_range) {
  534. invalidate_page_trampoline(
  535. umem,
  536. ib_umem_start(umem) + (page_index >> umem->page_shift),
  537. ib_umem_start(umem) + ((page_index + 1) >>
  538. umem->page_shift),
  539. NULL);
  540. ret = -EAGAIN;
  541. }
  542. return ret;
  543. }
  544. /**
  545. * ib_umem_odp_map_dma_pages - Pin and DMA map userspace memory in an ODP MR.
  546. *
  547. * Pins the range of pages passed in the argument, and maps them to
  548. * DMA addresses. The DMA addresses of the mapped pages is updated in
  549. * umem->odp_data->dma_list.
  550. *
  551. * Returns the number of pages mapped in success, negative error code
  552. * for failure.
  553. * An -EAGAIN error code is returned when a concurrent mmu notifier prevents
  554. * the function from completing its task.
  555. * An -ENOENT error code indicates that userspace process is being terminated
  556. * and mm was already destroyed.
  557. * @umem: the umem to map and pin
  558. * @user_virt: the address from which we need to map.
  559. * @bcnt: the minimal number of bytes to pin and map. The mapping might be
  560. * bigger due to alignment, and may also be smaller in case of an error
  561. * pinning or mapping a page. The actual pages mapped is returned in
  562. * the return value.
  563. * @access_mask: bit mask of the requested access permissions for the given
  564. * range.
  565. * @current_seq: the MMU notifiers sequance value for synchronization with
  566. * invalidations. the sequance number is read from
  567. * umem->odp_data->notifiers_seq before calling this function
  568. */
  569. int ib_umem_odp_map_dma_pages(struct ib_umem *umem, u64 user_virt, u64 bcnt,
  570. u64 access_mask, unsigned long current_seq)
  571. {
  572. struct task_struct *owning_process = NULL;
  573. struct mm_struct *owning_mm = NULL;
  574. struct page **local_page_list = NULL;
  575. u64 page_mask, off;
  576. int j, k, ret = 0, start_idx, npages = 0, page_shift;
  577. unsigned int flags = 0;
  578. phys_addr_t p = 0;
  579. if (access_mask == 0)
  580. return -EINVAL;
  581. if (user_virt < ib_umem_start(umem) ||
  582. user_virt + bcnt > ib_umem_end(umem))
  583. return -EFAULT;
  584. local_page_list = (struct page **)__get_free_page(GFP_KERNEL);
  585. if (!local_page_list)
  586. return -ENOMEM;
  587. page_shift = umem->page_shift;
  588. page_mask = ~(BIT(page_shift) - 1);
  589. off = user_virt & (~page_mask);
  590. user_virt = user_virt & page_mask;
  591. bcnt += off; /* Charge for the first page offset as well. */
  592. owning_process = get_pid_task(umem->context->tgid, PIDTYPE_PID);
  593. if (owning_process == NULL) {
  594. ret = -EINVAL;
  595. goto out_no_task;
  596. }
  597. owning_mm = get_task_mm(owning_process);
  598. if (owning_mm == NULL) {
  599. ret = -ENOENT;
  600. goto out_put_task;
  601. }
  602. if (access_mask & ODP_WRITE_ALLOWED_BIT)
  603. flags |= FOLL_WRITE;
  604. start_idx = (user_virt - ib_umem_start(umem)) >> page_shift;
  605. k = start_idx;
  606. while (bcnt > 0) {
  607. const size_t gup_num_pages = min_t(size_t,
  608. ALIGN(bcnt, PAGE_SIZE) / PAGE_SIZE,
  609. PAGE_SIZE / sizeof(struct page *));
  610. down_read(&owning_mm->mmap_sem);
  611. /*
  612. * Note: this might result in redundent page getting. We can
  613. * avoid this by checking dma_list to be 0 before calling
  614. * get_user_pages. However, this make the code much more
  615. * complex (and doesn't gain us much performance in most use
  616. * cases).
  617. */
  618. npages = get_user_pages_remote(owning_process, owning_mm,
  619. user_virt, gup_num_pages,
  620. flags, local_page_list, NULL, NULL);
  621. up_read(&owning_mm->mmap_sem);
  622. if (npages < 0)
  623. break;
  624. bcnt -= min_t(size_t, npages << PAGE_SHIFT, bcnt);
  625. mutex_lock(&umem->odp_data->umem_mutex);
  626. for (j = 0; j < npages; j++, user_virt += PAGE_SIZE) {
  627. if (user_virt & ~page_mask) {
  628. p += PAGE_SIZE;
  629. if (page_to_phys(local_page_list[j]) != p) {
  630. ret = -EFAULT;
  631. break;
  632. }
  633. put_page(local_page_list[j]);
  634. continue;
  635. }
  636. ret = ib_umem_odp_map_dma_single_page(
  637. umem, k, local_page_list[j],
  638. access_mask, current_seq);
  639. if (ret < 0)
  640. break;
  641. p = page_to_phys(local_page_list[j]);
  642. k++;
  643. }
  644. mutex_unlock(&umem->odp_data->umem_mutex);
  645. if (ret < 0) {
  646. /* Release left over pages when handling errors. */
  647. for (++j; j < npages; ++j)
  648. put_page(local_page_list[j]);
  649. break;
  650. }
  651. }
  652. if (ret >= 0) {
  653. if (npages < 0 && k == start_idx)
  654. ret = npages;
  655. else
  656. ret = k - start_idx;
  657. }
  658. mmput(owning_mm);
  659. out_put_task:
  660. put_task_struct(owning_process);
  661. out_no_task:
  662. free_page((unsigned long)local_page_list);
  663. return ret;
  664. }
  665. EXPORT_SYMBOL(ib_umem_odp_map_dma_pages);
  666. void ib_umem_odp_unmap_dma_pages(struct ib_umem *umem, u64 virt,
  667. u64 bound)
  668. {
  669. int idx;
  670. u64 addr;
  671. struct ib_device *dev = umem->context->device;
  672. virt = max_t(u64, virt, ib_umem_start(umem));
  673. bound = min_t(u64, bound, ib_umem_end(umem));
  674. /* Note that during the run of this function, the
  675. * notifiers_count of the MR is > 0, preventing any racing
  676. * faults from completion. We might be racing with other
  677. * invalidations, so we must make sure we free each page only
  678. * once. */
  679. mutex_lock(&umem->odp_data->umem_mutex);
  680. for (addr = virt; addr < bound; addr += BIT(umem->page_shift)) {
  681. idx = (addr - ib_umem_start(umem)) >> umem->page_shift;
  682. if (umem->odp_data->page_list[idx]) {
  683. struct page *page = umem->odp_data->page_list[idx];
  684. dma_addr_t dma = umem->odp_data->dma_list[idx];
  685. dma_addr_t dma_addr = dma & ODP_DMA_ADDR_MASK;
  686. WARN_ON(!dma_addr);
  687. ib_dma_unmap_page(dev, dma_addr, PAGE_SIZE,
  688. DMA_BIDIRECTIONAL);
  689. if (dma & ODP_WRITE_ALLOWED_BIT) {
  690. struct page *head_page = compound_head(page);
  691. /*
  692. * set_page_dirty prefers being called with
  693. * the page lock. However, MMU notifiers are
  694. * called sometimes with and sometimes without
  695. * the lock. We rely on the umem_mutex instead
  696. * to prevent other mmu notifiers from
  697. * continuing and allowing the page mapping to
  698. * be removed.
  699. */
  700. set_page_dirty(head_page);
  701. }
  702. /* on demand pinning support */
  703. if (!umem->context->invalidate_range)
  704. put_page(page);
  705. umem->odp_data->page_list[idx] = NULL;
  706. umem->odp_data->dma_list[idx] = 0;
  707. umem->npages--;
  708. }
  709. }
  710. mutex_unlock(&umem->odp_data->umem_mutex);
  711. }
  712. EXPORT_SYMBOL(ib_umem_odp_unmap_dma_pages);
  713. /* @last is not a part of the interval. See comment for function
  714. * node_last.
  715. */
  716. int rbt_ib_umem_for_each_in_range(struct rb_root_cached *root,
  717. u64 start, u64 last,
  718. umem_call_back cb,
  719. bool blockable,
  720. void *cookie)
  721. {
  722. int ret_val = 0;
  723. struct umem_odp_node *node, *next;
  724. struct ib_umem_odp *umem;
  725. if (unlikely(start == last))
  726. return ret_val;
  727. for (node = rbt_ib_umem_iter_first(root, start, last - 1);
  728. node; node = next) {
  729. /* TODO move the blockable decision up to the callback */
  730. if (!blockable)
  731. return -EAGAIN;
  732. next = rbt_ib_umem_iter_next(node, start, last - 1);
  733. umem = container_of(node, struct ib_umem_odp, interval_tree);
  734. ret_val = cb(umem->umem, start, last, cookie) || ret_val;
  735. }
  736. return ret_val;
  737. }
  738. EXPORT_SYMBOL(rbt_ib_umem_for_each_in_range);
  739. struct ib_umem_odp *rbt_ib_umem_lookup(struct rb_root_cached *root,
  740. u64 addr, u64 length)
  741. {
  742. struct umem_odp_node *node;
  743. node = rbt_ib_umem_iter_first(root, addr, addr + length - 1);
  744. if (node)
  745. return container_of(node, struct ib_umem_odp, interval_tree);
  746. return NULL;
  747. }
  748. EXPORT_SYMBOL(rbt_ib_umem_lookup);