umem_odp.c 22 KB

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