umem_odp.c 24 KB

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