umem_odp.c 22 KB

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