1 /*-
2 * SPDX-License-Identifier: BSD-2-Clause OR GPL-2.0
3 *
4 * Copyright (c) 2014 Mellanox Technologies. All rights reserved.
5 *
6 * This software is available to you under a choice of one of two
7 * licenses. You may choose to be licensed under the terms of the GNU
8 * General Public License (GPL) Version 2, available from the file
9 * COPYING in the main directory of this source tree, or the
10 * OpenIB.org BSD license below:
11 *
12 * Redistribution and use in source and binary forms, with or
13 * without modification, are permitted provided that the following
14 * conditions are met:
15 *
16 * - Redistributions of source code must retain the above
17 * copyright notice, this list of conditions and the following
18 * disclaimer.
19 *
20 * - Redistributions in binary form must reproduce the above
21 * copyright notice, this list of conditions and the following
22 * disclaimer in the documentation and/or other materials
23 * provided with the distribution.
24 *
25 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
26 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
27 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
28 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
29 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
30 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
31 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
32 * SOFTWARE.
33 */
34
35 #include <sys/cdefs.h>
36 #include <linux/types.h>
37 #include <linux/sched.h>
38 #include <linux/slab.h>
39 #include <linux/vmalloc.h>
40 #include <linux/hugetlb.h>
41 #include <linux/interval_tree_generic.h>
42
43 #include <rdma/ib_verbs.h>
44 #include <rdma/ib_umem.h>
45 #include <rdma/ib_umem_odp.h>
46
47 /*
48 * The ib_umem list keeps track of memory regions for which the HW
49 * device request to receive notification when the related memory
50 * mapping is changed.
51 *
52 * ib_umem_lock protects the list.
53 */
54
node_start(struct umem_odp_node * n)55 static u64 node_start(struct umem_odp_node *n)
56 {
57 struct ib_umem_odp *umem_odp =
58 container_of(n, struct ib_umem_odp, interval_tree);
59
60 return ib_umem_start(&umem_odp->umem);
61 }
62
63 /* Note that the representation of the intervals in the interval tree
64 * considers the ending point as contained in the interval, while the
65 * function ib_umem_end returns the first address which is not contained
66 * in the umem.
67 */
node_last(struct umem_odp_node * n)68 static u64 node_last(struct umem_odp_node *n)
69 {
70 struct ib_umem_odp *umem_odp =
71 container_of(n, struct ib_umem_odp, interval_tree);
72
73 return ib_umem_end(&umem_odp->umem) - 1;
74 }
75
INTERVAL_TREE_DEFINE(struct umem_odp_node,rb,u64,__subtree_last,node_start,node_last,static,rbt_ib_umem)76 INTERVAL_TREE_DEFINE(struct umem_odp_node, rb, u64, __subtree_last,
77 node_start, node_last, static, rbt_ib_umem)
78
79 static void ib_umem_notifier_start_account(struct ib_umem_odp *umem_odp)
80 {
81 mutex_lock(&umem_odp->umem_mutex);
82 if (umem_odp->notifiers_count++ == 0)
83 /*
84 * Initialize the completion object for waiting on
85 * notifiers. Since notifier_count is zero, no one should be
86 * waiting right now.
87 */
88 reinit_completion(&umem_odp->notifier_completion);
89 mutex_unlock(&umem_odp->umem_mutex);
90 }
91
ib_umem_notifier_end_account(struct ib_umem_odp * umem_odp)92 static void ib_umem_notifier_end_account(struct ib_umem_odp *umem_odp)
93 {
94 mutex_lock(&umem_odp->umem_mutex);
95 /*
96 * This sequence increase will notify the QP page fault that the page
97 * that is going to be mapped in the spte could have been freed.
98 */
99 ++umem_odp->notifiers_seq;
100 if (--umem_odp->notifiers_count == 0)
101 complete_all(&umem_odp->notifier_completion);
102 mutex_unlock(&umem_odp->umem_mutex);
103 }
104
ib_umem_notifier_release_trampoline(struct ib_umem_odp * umem_odp,u64 start,u64 end,void * cookie)105 static int ib_umem_notifier_release_trampoline(struct ib_umem_odp *umem_odp,
106 u64 start, u64 end, void *cookie)
107 {
108 struct ib_umem *umem = &umem_odp->umem;
109
110 /*
111 * Increase the number of notifiers running, to
112 * prevent any further fault handling on this MR.
113 */
114 ib_umem_notifier_start_account(umem_odp);
115 umem_odp->dying = 1;
116 /* Make sure that the fact the umem is dying is out before we release
117 * all pending page faults. */
118 smp_wmb();
119 complete_all(&umem_odp->notifier_completion);
120 umem->context->invalidate_range(umem_odp, ib_umem_start(umem),
121 ib_umem_end(umem));
122 return 0;
123 }
124
ib_umem_notifier_release(struct mmu_notifier * mn,struct mm_struct * mm)125 static void ib_umem_notifier_release(struct mmu_notifier *mn,
126 struct mm_struct *mm)
127 {
128 struct ib_ucontext_per_mm *per_mm =
129 container_of(mn, struct ib_ucontext_per_mm, mn);
130
131 down_read(&per_mm->umem_rwsem);
132 if (per_mm->active)
133 rbt_ib_umem_for_each_in_range(
134 &per_mm->umem_tree, 0, ULLONG_MAX,
135 ib_umem_notifier_release_trampoline, true, NULL);
136 up_read(&per_mm->umem_rwsem);
137 }
138
invalidate_page_trampoline(struct ib_umem_odp * item,u64 start,u64 end,void * cookie)139 static int invalidate_page_trampoline(struct ib_umem_odp *item, u64 start,
140 u64 end, void *cookie)
141 {
142 ib_umem_notifier_start_account(item);
143 item->umem.context->invalidate_range(item, start, start + PAGE_SIZE);
144 ib_umem_notifier_end_account(item);
145 return 0;
146 }
147
invalidate_range_start_trampoline(struct ib_umem_odp * item,u64 start,u64 end,void * cookie)148 static int invalidate_range_start_trampoline(struct ib_umem_odp *item,
149 u64 start, u64 end, void *cookie)
150 {
151 ib_umem_notifier_start_account(item);
152 item->umem.context->invalidate_range(item, start, end);
153 return 0;
154 }
155
ib_umem_notifier_invalidate_range_start(struct mmu_notifier * mn,struct mm_struct * mm,unsigned long start,unsigned long end,bool blockable)156 static int ib_umem_notifier_invalidate_range_start(struct mmu_notifier *mn,
157 struct mm_struct *mm,
158 unsigned long start,
159 unsigned long end,
160 bool blockable)
161 {
162 struct ib_ucontext_per_mm *per_mm =
163 container_of(mn, struct ib_ucontext_per_mm, mn);
164
165 if (blockable)
166 down_read(&per_mm->umem_rwsem);
167 else if (!down_read_trylock(&per_mm->umem_rwsem))
168 return -EAGAIN;
169
170 if (!per_mm->active) {
171 up_read(&per_mm->umem_rwsem);
172 /*
173 * At this point active is permanently set and visible to this
174 * CPU without a lock, that fact is relied on to skip the unlock
175 * in range_end.
176 */
177 return 0;
178 }
179
180 return rbt_ib_umem_for_each_in_range(&per_mm->umem_tree, start, end,
181 invalidate_range_start_trampoline,
182 blockable, NULL);
183 }
184
invalidate_range_end_trampoline(struct ib_umem_odp * item,u64 start,u64 end,void * cookie)185 static int invalidate_range_end_trampoline(struct ib_umem_odp *item, u64 start,
186 u64 end, void *cookie)
187 {
188 ib_umem_notifier_end_account(item);
189 return 0;
190 }
191
ib_umem_notifier_invalidate_range_end(struct mmu_notifier * mn,struct mm_struct * mm,unsigned long start,unsigned long end)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_per_mm *per_mm =
198 container_of(mn, struct ib_ucontext_per_mm, mn);
199
200 if (unlikely(!per_mm->active))
201 return;
202
203 rbt_ib_umem_for_each_in_range(&per_mm->umem_tree, start,
204 end,
205 invalidate_range_end_trampoline, true, NULL);
206 up_read(&per_mm->umem_rwsem);
207 }
208
209 static const struct mmu_notifier_ops ib_umem_notifiers = {
210 .release = ib_umem_notifier_release,
211 .invalidate_range_start = ib_umem_notifier_invalidate_range_start,
212 .invalidate_range_end = ib_umem_notifier_invalidate_range_end,
213 };
214
add_umem_to_per_mm(struct ib_umem_odp * umem_odp)215 static void add_umem_to_per_mm(struct ib_umem_odp *umem_odp)
216 {
217 struct ib_ucontext_per_mm *per_mm = umem_odp->per_mm;
218 struct ib_umem *umem = &umem_odp->umem;
219
220 down_write(&per_mm->umem_rwsem);
221 if (likely(ib_umem_start(umem) != ib_umem_end(umem)))
222 rbt_ib_umem_insert(&umem_odp->interval_tree,
223 &per_mm->umem_tree);
224 up_write(&per_mm->umem_rwsem);
225 }
226
remove_umem_from_per_mm(struct ib_umem_odp * umem_odp)227 static void remove_umem_from_per_mm(struct ib_umem_odp *umem_odp)
228 {
229 struct ib_ucontext_per_mm *per_mm = umem_odp->per_mm;
230 struct ib_umem *umem = &umem_odp->umem;
231
232 down_write(&per_mm->umem_rwsem);
233 if (likely(ib_umem_start(umem) != ib_umem_end(umem)))
234 rbt_ib_umem_remove(&umem_odp->interval_tree,
235 &per_mm->umem_tree);
236 complete_all(&umem_odp->notifier_completion);
237
238 up_write(&per_mm->umem_rwsem);
239 }
240
alloc_per_mm(struct ib_ucontext * ctx,struct mm_struct * mm)241 static struct ib_ucontext_per_mm *alloc_per_mm(struct ib_ucontext *ctx,
242 struct mm_struct *mm)
243 {
244 struct ib_ucontext_per_mm *per_mm;
245 int ret;
246
247 per_mm = kzalloc(sizeof(*per_mm), GFP_KERNEL);
248 if (!per_mm)
249 return ERR_PTR(-ENOMEM);
250
251 per_mm->context = ctx;
252 per_mm->mm = mm;
253 per_mm->umem_tree = RB_ROOT_CACHED;
254 init_rwsem(&per_mm->umem_rwsem);
255 per_mm->active = ctx->invalidate_range;
256
257 rcu_read_lock();
258 per_mm->tgid = get_task_pid(current->group_leader, PIDTYPE_PID);
259 rcu_read_unlock();
260
261 WARN_ON(mm != current->mm);
262
263 per_mm->mn.ops = &ib_umem_notifiers;
264 ret = mmu_notifier_register(&per_mm->mn, per_mm->mm);
265 if (ret) {
266 dev_err(&ctx->device->dev,
267 "Failed to register mmu_notifier %d\n", ret);
268 goto out_pid;
269 }
270
271 list_add(&per_mm->ucontext_list, &ctx->per_mm_list);
272 return per_mm;
273
274 out_pid:
275 put_pid(per_mm->tgid);
276 kfree(per_mm);
277 return ERR_PTR(ret);
278 }
279
get_per_mm(struct ib_umem_odp * umem_odp)280 static int get_per_mm(struct ib_umem_odp *umem_odp)
281 {
282 struct ib_ucontext *ctx = umem_odp->umem.context;
283 struct ib_ucontext_per_mm *per_mm;
284
285 /*
286 * Generally speaking we expect only one or two per_mm in this list,
287 * so no reason to optimize this search today.
288 */
289 mutex_lock(&ctx->per_mm_list_lock);
290 list_for_each_entry(per_mm, &ctx->per_mm_list, ucontext_list) {
291 if (per_mm->mm == umem_odp->umem.owning_mm)
292 goto found;
293 }
294
295 per_mm = alloc_per_mm(ctx, umem_odp->umem.owning_mm);
296 if (IS_ERR(per_mm)) {
297 mutex_unlock(&ctx->per_mm_list_lock);
298 return PTR_ERR(per_mm);
299 }
300
301 found:
302 umem_odp->per_mm = per_mm;
303 per_mm->odp_mrs_count++;
304 mutex_unlock(&ctx->per_mm_list_lock);
305
306 return 0;
307 }
308
free_per_mm(struct rcu_head * rcu)309 static void free_per_mm(struct rcu_head *rcu)
310 {
311 kfree(container_of(rcu, struct ib_ucontext_per_mm, rcu));
312 }
313
put_per_mm(struct ib_umem_odp * umem_odp)314 void put_per_mm(struct ib_umem_odp *umem_odp)
315 {
316 struct ib_ucontext_per_mm *per_mm = umem_odp->per_mm;
317 struct ib_ucontext *ctx = umem_odp->umem.context;
318 bool need_free;
319
320 mutex_lock(&ctx->per_mm_list_lock);
321 umem_odp->per_mm = NULL;
322 per_mm->odp_mrs_count--;
323 need_free = per_mm->odp_mrs_count == 0;
324 if (need_free)
325 list_del(&per_mm->ucontext_list);
326 mutex_unlock(&ctx->per_mm_list_lock);
327
328 if (!need_free)
329 return;
330
331 /*
332 * NOTE! mmu_notifier_unregister() can happen between a start/end
333 * callback, resulting in an start/end, and thus an unbalanced
334 * lock. This doesn't really matter to us since we are about to kfree
335 * the memory that holds the lock, however LOCKDEP doesn't like this.
336 */
337 down_write(&per_mm->umem_rwsem);
338 per_mm->active = false;
339 up_write(&per_mm->umem_rwsem);
340
341 WARN_ON(!RB_EMPTY_ROOT(&per_mm->umem_tree.rb_root));
342 mmu_notifier_unregister_no_release(&per_mm->mn, per_mm->mm);
343 put_pid(per_mm->tgid);
344 mmu_notifier_call_srcu(&per_mm->rcu, free_per_mm);
345 }
346
ib_alloc_odp_umem(struct ib_ucontext_per_mm * per_mm,unsigned long addr,size_t size)347 struct ib_umem_odp *ib_alloc_odp_umem(struct ib_ucontext_per_mm *per_mm,
348 unsigned long addr, size_t size)
349 {
350 struct ib_ucontext *ctx = per_mm->context;
351 struct ib_umem_odp *odp_data;
352 struct ib_umem *umem;
353 int pages = size >> PAGE_SHIFT;
354 int ret;
355
356 odp_data = kzalloc(sizeof(*odp_data), GFP_KERNEL);
357 if (!odp_data)
358 return ERR_PTR(-ENOMEM);
359 umem = &odp_data->umem;
360 umem->context = ctx;
361 umem->length = size;
362 umem->address = addr;
363 umem->page_shift = PAGE_SHIFT;
364 umem->writable = 1;
365 umem->is_odp = 1;
366 odp_data->per_mm = per_mm;
367
368 mutex_init(&odp_data->umem_mutex);
369 init_completion(&odp_data->notifier_completion);
370
371 odp_data->page_list =
372 vzalloc(array_size(pages, sizeof(*odp_data->page_list)));
373 if (!odp_data->page_list) {
374 ret = -ENOMEM;
375 goto out_odp_data;
376 }
377
378 odp_data->dma_list =
379 vzalloc(array_size(pages, sizeof(*odp_data->dma_list)));
380 if (!odp_data->dma_list) {
381 ret = -ENOMEM;
382 goto out_page_list;
383 }
384
385 /*
386 * Caller must ensure that the umem_odp that the per_mm came from
387 * cannot be freed during the call to ib_alloc_odp_umem.
388 */
389 mutex_lock(&ctx->per_mm_list_lock);
390 per_mm->odp_mrs_count++;
391 mutex_unlock(&ctx->per_mm_list_lock);
392 add_umem_to_per_mm(odp_data);
393
394 return odp_data;
395
396 out_page_list:
397 vfree(odp_data->page_list);
398 out_odp_data:
399 kfree(odp_data);
400 return ERR_PTR(ret);
401 }
402 EXPORT_SYMBOL(ib_alloc_odp_umem);
403
ib_umem_odp_get(struct ib_umem_odp * umem_odp,int access)404 int ib_umem_odp_get(struct ib_umem_odp *umem_odp, int access)
405 {
406 struct ib_umem *umem = &umem_odp->umem;
407 /*
408 * NOTE: This must called in a process context where umem->owning_mm
409 * == current->mm
410 */
411 struct mm_struct *mm = umem->owning_mm;
412 int ret_val;
413
414 if (access & IB_ACCESS_HUGETLB) {
415 struct vm_area_struct *vma;
416 struct hstate *h;
417
418 vma = find_vma(mm, ib_umem_start(umem));
419 if (!vma || !is_vm_hugetlb_page(vma))
420 return -EINVAL;
421 h = hstate_vma(vma);
422 umem->page_shift = huge_page_shift(h);
423 umem->hugetlb = 1;
424 } else {
425 umem->hugetlb = 0;
426 }
427
428 mutex_init(&umem_odp->umem_mutex);
429
430 init_completion(&umem_odp->notifier_completion);
431
432 if (ib_umem_num_pages(umem)) {
433 umem_odp->page_list =
434 vzalloc(array_size(sizeof(*umem_odp->page_list),
435 ib_umem_num_pages(umem)));
436 if (!umem_odp->page_list)
437 return -ENOMEM;
438
439 umem_odp->dma_list =
440 vzalloc(array_size(sizeof(*umem_odp->dma_list),
441 ib_umem_num_pages(umem)));
442 if (!umem_odp->dma_list) {
443 ret_val = -ENOMEM;
444 goto out_page_list;
445 }
446 }
447
448 ret_val = get_per_mm(umem_odp);
449 if (ret_val)
450 goto out_dma_list;
451 add_umem_to_per_mm(umem_odp);
452
453 return 0;
454
455 out_dma_list:
456 vfree(umem_odp->dma_list);
457 out_page_list:
458 vfree(umem_odp->page_list);
459 return ret_val;
460 }
461
ib_umem_odp_release(struct ib_umem_odp * umem_odp)462 void ib_umem_odp_release(struct ib_umem_odp *umem_odp)
463 {
464 struct ib_umem *umem = &umem_odp->umem;
465
466 /*
467 * Ensure that no more pages are mapped in the umem.
468 *
469 * It is the driver's responsibility to ensure, before calling us,
470 * that the hardware will not attempt to access the MR any more.
471 */
472 ib_umem_odp_unmap_dma_pages(umem_odp, ib_umem_start(umem),
473 ib_umem_end(umem));
474
475 remove_umem_from_per_mm(umem_odp);
476 put_per_mm(umem_odp);
477 vfree(umem_odp->dma_list);
478 vfree(umem_odp->page_list);
479 }
480
481 /*
482 * Map for DMA and insert a single page into the on-demand paging page tables.
483 *
484 * @umem: the umem to insert the page to.
485 * @page_index: index in the umem to add the page to.
486 * @page: the page struct to map and add.
487 * @access_mask: access permissions needed for this page.
488 * @current_seq: sequence number for synchronization with invalidations.
489 * the sequence number is taken from
490 * umem_odp->notifiers_seq.
491 *
492 * The function returns -EFAULT if the DMA mapping operation fails. It returns
493 * -EAGAIN if a concurrent invalidation prevents us from updating the page.
494 *
495 * The page is released via put_page even if the operation failed. For
496 * on-demand pinning, the page is released whenever it isn't stored in the
497 * umem.
498 */
ib_umem_odp_map_dma_single_page(struct ib_umem_odp * umem_odp,int page_index,struct page * page,u64 access_mask,unsigned long current_seq)499 static int ib_umem_odp_map_dma_single_page(
500 struct ib_umem_odp *umem_odp,
501 int page_index,
502 struct page *page,
503 u64 access_mask,
504 unsigned long current_seq)
505 {
506 struct ib_umem *umem = &umem_odp->umem;
507 struct ib_device *dev = umem->context->device;
508 dma_addr_t dma_addr;
509 int stored_page = 0;
510 int remove_existing_mapping = 0;
511 int ret = 0;
512
513 /*
514 * Note: we avoid writing if seq is different from the initial seq, to
515 * handle case of a racing notifier. This check also allows us to bail
516 * early if we have a notifier running in parallel with us.
517 */
518 if (ib_umem_mmu_notifier_retry(umem_odp, current_seq)) {
519 ret = -EAGAIN;
520 goto out;
521 }
522 if (!(umem_odp->dma_list[page_index])) {
523 dma_addr = ib_dma_map_page(dev,
524 page,
525 0, BIT(umem->page_shift),
526 DMA_BIDIRECTIONAL);
527 if (ib_dma_mapping_error(dev, dma_addr)) {
528 ret = -EFAULT;
529 goto out;
530 }
531 umem_odp->dma_list[page_index] = dma_addr | access_mask;
532 umem_odp->page_list[page_index] = page;
533 umem->npages++;
534 stored_page = 1;
535 } else if (umem_odp->page_list[page_index] == page) {
536 umem_odp->dma_list[page_index] |= access_mask;
537 } else {
538 pr_err("error: got different pages in IB device and from get_user_pages. IB device page: %p, gup page: %p\n",
539 umem_odp->page_list[page_index], page);
540 /* Better remove the mapping now, to prevent any further
541 * damage. */
542 remove_existing_mapping = 1;
543 }
544
545 out:
546 /* On Demand Paging - avoid pinning the page */
547 if (umem->context->invalidate_range || !stored_page)
548 put_page(page);
549
550 if (remove_existing_mapping && umem->context->invalidate_range) {
551 invalidate_page_trampoline(
552 umem_odp,
553 ib_umem_start(umem) + (page_index >> umem->page_shift),
554 ib_umem_start(umem) + ((page_index + 1) >>
555 umem->page_shift),
556 NULL);
557 ret = -EAGAIN;
558 }
559
560 return ret;
561 }
562
563 /**
564 * ib_umem_odp_map_dma_pages - Pin and DMA map userspace memory in an ODP MR.
565 *
566 * Pins the range of pages passed in the argument, and maps them to
567 * DMA addresses. The DMA addresses of the mapped pages is updated in
568 * umem_odp->dma_list.
569 *
570 * Returns the number of pages mapped in success, negative error code
571 * for failure.
572 * An -EAGAIN error code is returned when a concurrent mmu notifier prevents
573 * the function from completing its task.
574 * An -ENOENT error code indicates that userspace process is being terminated
575 * and mm was already destroyed.
576 * @umem_odp: the umem to map and pin
577 * @user_virt: the address from which we need to map.
578 * @bcnt: the minimal number of bytes to pin and map. The mapping might be
579 * bigger due to alignment, and may also be smaller in case of an error
580 * pinning or mapping a page. The actual pages mapped is returned in
581 * the return value.
582 * @access_mask: bit mask of the requested access permissions for the given
583 * range.
584 * @current_seq: the MMU notifiers sequance value for synchronization with
585 * invalidations. the sequance number is read from
586 * umem_odp->notifiers_seq before calling this function
587 */
ib_umem_odp_map_dma_pages(struct ib_umem_odp * umem_odp,u64 user_virt,u64 bcnt,u64 access_mask,unsigned long current_seq)588 int ib_umem_odp_map_dma_pages(struct ib_umem_odp *umem_odp, u64 user_virt,
589 u64 bcnt, u64 access_mask,
590 unsigned long current_seq)
591 {
592 struct ib_umem *umem = &umem_odp->umem;
593 struct task_struct *owning_process = NULL;
594 struct mm_struct *owning_mm = umem_odp->umem.owning_mm;
595 struct page **local_page_list = NULL;
596 u64 page_mask, off;
597 int j, k, ret = 0, start_idx, npages = 0, page_shift;
598 unsigned int flags = 0;
599 phys_addr_t p = 0;
600
601 if (access_mask == 0)
602 return -EINVAL;
603
604 if (user_virt < ib_umem_start(umem) ||
605 user_virt + bcnt > ib_umem_end(umem))
606 return -EFAULT;
607
608 local_page_list = (struct page **)__get_free_page(GFP_KERNEL);
609 if (!local_page_list)
610 return -ENOMEM;
611
612 page_shift = umem->page_shift;
613 page_mask = ~(BIT(page_shift) - 1);
614 off = user_virt & (~page_mask);
615 user_virt = user_virt & page_mask;
616 bcnt += off; /* Charge for the first page offset as well. */
617
618 /*
619 * owning_process is allowed to be NULL, this means somehow the mm is
620 * existing beyond the lifetime of the originating process.. Presumably
621 * mmget_not_zero will fail in this case.
622 */
623 owning_process = get_pid_task(umem_odp->per_mm->tgid, PIDTYPE_PID);
624 if (WARN_ON(!mmget_not_zero(umem_odp->umem.owning_mm))) {
625 ret = -EINVAL;
626 goto out_put_task;
627 }
628
629 if (access_mask & ODP_WRITE_ALLOWED_BIT)
630 flags |= FOLL_WRITE;
631
632 start_idx = (user_virt - ib_umem_start(umem)) >> page_shift;
633 k = start_idx;
634
635 while (bcnt > 0) {
636 const size_t gup_num_pages = min_t(size_t,
637 (bcnt + BIT(page_shift) - 1) >> page_shift,
638 PAGE_SIZE / sizeof(struct page *));
639
640 down_read(&owning_mm->mmap_sem);
641 /*
642 * Note: this might result in redundent page getting. We can
643 * avoid this by checking dma_list to be 0 before calling
644 * get_user_pages. However, this make the code much more
645 * complex (and doesn't gain us much performance in most use
646 * cases).
647 */
648 npages = get_user_pages_remote(owning_process, owning_mm,
649 user_virt, gup_num_pages,
650 flags, local_page_list, NULL);
651 up_read(&owning_mm->mmap_sem);
652
653 if (npages < 0)
654 break;
655
656 bcnt -= min_t(size_t, npages << PAGE_SHIFT, bcnt);
657 mutex_lock(&umem_odp->umem_mutex);
658 for (j = 0; j < npages; j++, user_virt += PAGE_SIZE) {
659 if (user_virt & ~page_mask) {
660 p += PAGE_SIZE;
661 if (page_to_phys(local_page_list[j]) != p) {
662 ret = -EFAULT;
663 break;
664 }
665 put_page(local_page_list[j]);
666 continue;
667 }
668
669 ret = ib_umem_odp_map_dma_single_page(
670 umem_odp, k, local_page_list[j],
671 access_mask, current_seq);
672 if (ret < 0)
673 break;
674
675 p = page_to_phys(local_page_list[j]);
676 k++;
677 }
678 mutex_unlock(&umem_odp->umem_mutex);
679
680 if (ret < 0) {
681 /* Release left over pages when handling errors. */
682 for (++j; j < npages; ++j)
683 put_page(local_page_list[j]);
684 break;
685 }
686 }
687
688 if (ret >= 0) {
689 if (npages < 0 && k == start_idx)
690 ret = npages;
691 else
692 ret = k - start_idx;
693 }
694
695 mmput(owning_mm);
696 out_put_task:
697 if (owning_process)
698 put_task_struct(owning_process);
699 free_page((unsigned long)local_page_list);
700 return ret;
701 }
702 EXPORT_SYMBOL(ib_umem_odp_map_dma_pages);
703
ib_umem_odp_unmap_dma_pages(struct ib_umem_odp * umem_odp,u64 virt,u64 bound)704 void ib_umem_odp_unmap_dma_pages(struct ib_umem_odp *umem_odp, u64 virt,
705 u64 bound)
706 {
707 struct ib_umem *umem = &umem_odp->umem;
708 int idx;
709 u64 addr;
710 struct ib_device *dev = umem->context->device;
711
712 virt = max_t(u64, virt, ib_umem_start(umem));
713 bound = min_t(u64, bound, ib_umem_end(umem));
714 /* Note that during the run of this function, the
715 * notifiers_count of the MR is > 0, preventing any racing
716 * faults from completion. We might be racing with other
717 * invalidations, so we must make sure we free each page only
718 * once. */
719 mutex_lock(&umem_odp->umem_mutex);
720 for (addr = virt; addr < bound; addr += BIT(umem->page_shift)) {
721 idx = (addr - ib_umem_start(umem)) >> umem->page_shift;
722 if (umem_odp->page_list[idx]) {
723 struct page *page = umem_odp->page_list[idx];
724 dma_addr_t dma = umem_odp->dma_list[idx];
725 dma_addr_t dma_addr = dma & ODP_DMA_ADDR_MASK;
726
727 WARN_ON(!dma_addr);
728
729 ib_dma_unmap_page(dev, dma_addr, PAGE_SIZE,
730 DMA_BIDIRECTIONAL);
731 if (dma & ODP_WRITE_ALLOWED_BIT) {
732 struct page *head_page = compound_head(page);
733 /*
734 * set_page_dirty prefers being called with
735 * the page lock. However, MMU notifiers are
736 * called sometimes with and sometimes without
737 * the lock. We rely on the umem_mutex instead
738 * to prevent other mmu notifiers from
739 * continuing and allowing the page mapping to
740 * be removed.
741 */
742 set_page_dirty(head_page);
743 }
744 /* on demand pinning support */
745 if (!umem->context->invalidate_range)
746 put_page(page);
747 umem_odp->page_list[idx] = NULL;
748 umem_odp->dma_list[idx] = 0;
749 umem->npages--;
750 }
751 }
752 mutex_unlock(&umem_odp->umem_mutex);
753 }
754 EXPORT_SYMBOL(ib_umem_odp_unmap_dma_pages);
755
756 /* @last is not a part of the interval. See comment for function
757 * node_last.
758 */
rbt_ib_umem_for_each_in_range(struct rb_root_cached * root,u64 start,u64 last,umem_call_back cb,bool blockable,void * cookie)759 int rbt_ib_umem_for_each_in_range(struct rb_root_cached *root,
760 u64 start, u64 last,
761 umem_call_back cb,
762 bool blockable,
763 void *cookie)
764 {
765 int ret_val = 0;
766 struct umem_odp_node *node, *next;
767 struct ib_umem_odp *umem;
768
769 if (unlikely(start == last))
770 return ret_val;
771
772 for (node = rbt_ib_umem_iter_first(root, start, last - 1);
773 node; node = next) {
774 /* TODO move the blockable decision up to the callback */
775 if (!blockable)
776 return -EAGAIN;
777 next = rbt_ib_umem_iter_next(node, start, last - 1);
778 umem = container_of(node, struct ib_umem_odp, interval_tree);
779 ret_val = cb(umem, start, last, cookie) || ret_val;
780 }
781
782 return ret_val;
783 }
784 EXPORT_SYMBOL(rbt_ib_umem_for_each_in_range);
785
rbt_ib_umem_lookup(struct rb_root_cached * root,u64 addr,u64 length)786 struct ib_umem_odp *rbt_ib_umem_lookup(struct rb_root_cached *root,
787 u64 addr, u64 length)
788 {
789 struct umem_odp_node *node;
790
791 node = rbt_ib_umem_iter_first(root, addr, addr + length - 1);
792 if (node)
793 return container_of(node, struct ib_umem_odp, interval_tree);
794 return NULL;
795
796 }
797 EXPORT_SYMBOL(rbt_ib_umem_lookup);
798