umem_odp.c 20 KB

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