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