umem_odp.c 22 KB

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