umem_odp.c 22 KB

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