umem_odp.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673
  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 const 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. context->odp_mrs_count == 1)
  260. umem->odp_data->mn_counters_active = true;
  261. else
  262. list_add(&umem->odp_data->no_private_counters,
  263. &context->no_private_counters);
  264. downgrade_write(&context->umem_rwsem);
  265. if (context->odp_mrs_count == 1) {
  266. /*
  267. * Note that at this point, no MMU notifier is running
  268. * for this context!
  269. */
  270. atomic_set(&context->notifier_count, 0);
  271. INIT_HLIST_NODE(&context->mn.hlist);
  272. context->mn.ops = &ib_umem_notifiers;
  273. /*
  274. * Lock-dep detects a false positive for mmap_sem vs.
  275. * umem_rwsem, due to not grasping downgrade_write correctly.
  276. */
  277. lockdep_off();
  278. ret_val = mmu_notifier_register(&context->mn, mm);
  279. lockdep_on();
  280. if (ret_val) {
  281. pr_err("Failed to register mmu_notifier %d\n", ret_val);
  282. ret_val = -EBUSY;
  283. goto out_mutex;
  284. }
  285. }
  286. up_read(&context->umem_rwsem);
  287. /*
  288. * Note that doing an mmput can cause a notifier for the relevant mm.
  289. * If the notifier is called while we hold the umem_rwsem, this will
  290. * cause a deadlock. Therefore, we release the reference only after we
  291. * released the semaphore.
  292. */
  293. mmput(mm);
  294. return 0;
  295. out_mutex:
  296. up_read(&context->umem_rwsem);
  297. vfree(umem->odp_data->dma_list);
  298. out_page_list:
  299. vfree(umem->odp_data->page_list);
  300. out_odp_data:
  301. kfree(umem->odp_data);
  302. out_mm:
  303. mmput(mm);
  304. return ret_val;
  305. }
  306. void ib_umem_odp_release(struct ib_umem *umem)
  307. {
  308. struct ib_ucontext *context = umem->context;
  309. /*
  310. * Ensure that no more pages are mapped in the umem.
  311. *
  312. * It is the driver's responsibility to ensure, before calling us,
  313. * that the hardware will not attempt to access the MR any more.
  314. */
  315. ib_umem_odp_unmap_dma_pages(umem, ib_umem_start(umem),
  316. ib_umem_end(umem));
  317. down_write(&context->umem_rwsem);
  318. if (likely(ib_umem_start(umem) != ib_umem_end(umem)))
  319. rbt_ib_umem_remove(&umem->odp_data->interval_tree,
  320. &context->umem_tree);
  321. context->odp_mrs_count--;
  322. if (!umem->odp_data->mn_counters_active) {
  323. list_del(&umem->odp_data->no_private_counters);
  324. complete_all(&umem->odp_data->notifier_completion);
  325. }
  326. /*
  327. * Downgrade the lock to a read lock. This ensures that the notifiers
  328. * (who lock the mutex for reading) will be able to finish, and we
  329. * will be able to enventually obtain the mmu notifiers SRCU. Note
  330. * that since we are doing it atomically, no other user could register
  331. * and unregister while we do the check.
  332. */
  333. downgrade_write(&context->umem_rwsem);
  334. if (!context->odp_mrs_count) {
  335. struct task_struct *owning_process = NULL;
  336. struct mm_struct *owning_mm = NULL;
  337. owning_process = get_pid_task(context->tgid,
  338. PIDTYPE_PID);
  339. if (owning_process == NULL)
  340. /*
  341. * The process is already dead, notifier were removed
  342. * already.
  343. */
  344. goto out;
  345. owning_mm = get_task_mm(owning_process);
  346. if (owning_mm == NULL)
  347. /*
  348. * The process' mm is already dead, notifier were
  349. * removed already.
  350. */
  351. goto out_put_task;
  352. mmu_notifier_unregister(&context->mn, owning_mm);
  353. mmput(owning_mm);
  354. out_put_task:
  355. put_task_struct(owning_process);
  356. }
  357. out:
  358. up_read(&context->umem_rwsem);
  359. vfree(umem->odp_data->dma_list);
  360. vfree(umem->odp_data->page_list);
  361. kfree(umem->odp_data);
  362. kfree(umem);
  363. }
  364. /*
  365. * Map for DMA and insert a single page into the on-demand paging page tables.
  366. *
  367. * @umem: the umem to insert the page to.
  368. * @page_index: index in the umem to add the page to.
  369. * @page: the page struct to map and add.
  370. * @access_mask: access permissions needed for this page.
  371. * @current_seq: sequence number for synchronization with invalidations.
  372. * the sequence number is taken from
  373. * umem->odp_data->notifiers_seq.
  374. *
  375. * The function returns -EFAULT if the DMA mapping operation fails. It returns
  376. * -EAGAIN if a concurrent invalidation prevents us from updating the page.
  377. *
  378. * The page is released via put_page even if the operation failed. For
  379. * on-demand pinning, the page is released whenever it isn't stored in the
  380. * umem.
  381. */
  382. static int ib_umem_odp_map_dma_single_page(
  383. struct ib_umem *umem,
  384. int page_index,
  385. u64 base_virt_addr,
  386. struct page *page,
  387. u64 access_mask,
  388. unsigned long current_seq)
  389. {
  390. struct ib_device *dev = umem->context->device;
  391. dma_addr_t dma_addr;
  392. int stored_page = 0;
  393. int remove_existing_mapping = 0;
  394. int ret = 0;
  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. /* On Demand Paging - avoid pinning the page */
  427. if (umem->context->invalidate_range || !stored_page)
  428. put_page(page);
  429. if (remove_existing_mapping && umem->context->invalidate_range) {
  430. invalidate_page_trampoline(
  431. umem,
  432. base_virt_addr + (page_index * PAGE_SIZE),
  433. base_virt_addr + ((page_index+1)*PAGE_SIZE),
  434. NULL);
  435. ret = -EAGAIN;
  436. }
  437. return ret;
  438. }
  439. /**
  440. * ib_umem_odp_map_dma_pages - Pin and DMA map userspace memory in an ODP MR.
  441. *
  442. * Pins the range of pages passed in the argument, and maps them to
  443. * DMA addresses. The DMA addresses of the mapped pages is updated in
  444. * umem->odp_data->dma_list.
  445. *
  446. * Returns the number of pages mapped in success, negative error code
  447. * for failure.
  448. * An -EAGAIN error code is returned when a concurrent mmu notifier prevents
  449. * the function from completing its task.
  450. *
  451. * @umem: the umem to map and pin
  452. * @user_virt: the address from which we need to map.
  453. * @bcnt: the minimal number of bytes to pin and map. The mapping might be
  454. * bigger due to alignment, and may also be smaller in case of an error
  455. * pinning or mapping a page. The actual pages mapped is returned in
  456. * the return value.
  457. * @access_mask: bit mask of the requested access permissions for the given
  458. * range.
  459. * @current_seq: the MMU notifiers sequance value for synchronization with
  460. * invalidations. the sequance number is read from
  461. * umem->odp_data->notifiers_seq before calling this function
  462. */
  463. int ib_umem_odp_map_dma_pages(struct ib_umem *umem, u64 user_virt, u64 bcnt,
  464. u64 access_mask, unsigned long current_seq)
  465. {
  466. struct task_struct *owning_process = NULL;
  467. struct mm_struct *owning_mm = NULL;
  468. struct page **local_page_list = NULL;
  469. u64 off;
  470. int j, k, ret = 0, start_idx, npages = 0;
  471. u64 base_virt_addr;
  472. unsigned int flags = 0;
  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. if (access_mask & ODP_WRITE_ALLOWED_BIT)
  496. flags |= FOLL_WRITE;
  497. start_idx = (user_virt - ib_umem_start(umem)) >> PAGE_SHIFT;
  498. k = start_idx;
  499. while (bcnt > 0) {
  500. const size_t gup_num_pages =
  501. min_t(size_t, ALIGN(bcnt, PAGE_SIZE) / PAGE_SIZE,
  502. PAGE_SIZE / sizeof(struct page *));
  503. down_read(&owning_mm->mmap_sem);
  504. /*
  505. * Note: this might result in redundent page getting. We can
  506. * avoid this by checking dma_list to be 0 before calling
  507. * get_user_pages. However, this make the code much more
  508. * complex (and doesn't gain us much performance in most use
  509. * cases).
  510. */
  511. npages = get_user_pages_remote(owning_process, owning_mm,
  512. user_virt, gup_num_pages,
  513. flags, local_page_list, NULL);
  514. up_read(&owning_mm->mmap_sem);
  515. if (npages < 0)
  516. break;
  517. bcnt -= min_t(size_t, npages << PAGE_SHIFT, bcnt);
  518. user_virt += npages << PAGE_SHIFT;
  519. mutex_lock(&umem->odp_data->umem_mutex);
  520. for (j = 0; j < npages; ++j) {
  521. ret = ib_umem_odp_map_dma_single_page(
  522. umem, k, base_virt_addr, local_page_list[j],
  523. access_mask, current_seq);
  524. if (ret < 0)
  525. break;
  526. k++;
  527. }
  528. mutex_unlock(&umem->odp_data->umem_mutex);
  529. if (ret < 0) {
  530. /* Release left over pages when handling errors. */
  531. for (++j; j < npages; ++j)
  532. put_page(local_page_list[j]);
  533. break;
  534. }
  535. }
  536. if (ret >= 0) {
  537. if (npages < 0 && k == start_idx)
  538. ret = npages;
  539. else
  540. ret = k - start_idx;
  541. }
  542. mmput(owning_mm);
  543. out_put_task:
  544. put_task_struct(owning_process);
  545. out_no_task:
  546. free_page((unsigned long)local_page_list);
  547. return ret;
  548. }
  549. EXPORT_SYMBOL(ib_umem_odp_map_dma_pages);
  550. void ib_umem_odp_unmap_dma_pages(struct ib_umem *umem, u64 virt,
  551. u64 bound)
  552. {
  553. int idx;
  554. u64 addr;
  555. struct ib_device *dev = umem->context->device;
  556. virt = max_t(u64, virt, ib_umem_start(umem));
  557. bound = min_t(u64, bound, ib_umem_end(umem));
  558. /* Note that during the run of this function, the
  559. * notifiers_count of the MR is > 0, preventing any racing
  560. * faults from completion. We might be racing with other
  561. * invalidations, so we must make sure we free each page only
  562. * once. */
  563. mutex_lock(&umem->odp_data->umem_mutex);
  564. for (addr = virt; addr < bound; addr += (u64)umem->page_size) {
  565. idx = (addr - ib_umem_start(umem)) / PAGE_SIZE;
  566. if (umem->odp_data->page_list[idx]) {
  567. struct page *page = umem->odp_data->page_list[idx];
  568. dma_addr_t dma = umem->odp_data->dma_list[idx];
  569. dma_addr_t dma_addr = dma & ODP_DMA_ADDR_MASK;
  570. WARN_ON(!dma_addr);
  571. ib_dma_unmap_page(dev, dma_addr, PAGE_SIZE,
  572. DMA_BIDIRECTIONAL);
  573. if (dma & ODP_WRITE_ALLOWED_BIT) {
  574. struct page *head_page = compound_head(page);
  575. /*
  576. * set_page_dirty prefers being called with
  577. * the page lock. However, MMU notifiers are
  578. * called sometimes with and sometimes without
  579. * the lock. We rely on the umem_mutex instead
  580. * to prevent other mmu notifiers from
  581. * continuing and allowing the page mapping to
  582. * be removed.
  583. */
  584. set_page_dirty(head_page);
  585. }
  586. /* on demand pinning support */
  587. if (!umem->context->invalidate_range)
  588. put_page(page);
  589. umem->odp_data->page_list[idx] = NULL;
  590. umem->odp_data->dma_list[idx] = 0;
  591. }
  592. }
  593. mutex_unlock(&umem->odp_data->umem_mutex);
  594. }
  595. EXPORT_SYMBOL(ib_umem_odp_unmap_dma_pages);