2 * Copyright (c) 2014 Mellanox Technologies. All rights reserved.
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:
10 * Redistribution and use in source and binary forms, with or
11 * without modification, are permitted provided that the following
14 * - Redistributions of source code must retain the above
15 * copyright notice, this list of conditions and the following
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.
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
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 #include <linux/interval_tree.h>
43 #include <linux/hmm.h>
44 #include <linux/pagemap.h>
46 #include <rdma/ib_umem_odp.h>
50 static inline int ib_init_umem_odp(struct ib_umem_odp
*umem_odp
,
51 const struct mmu_interval_notifier_ops
*ops
)
55 umem_odp
->umem
.is_odp
= 1;
56 mutex_init(&umem_odp
->umem_mutex
);
58 if (!umem_odp
->is_implicit_odp
) {
59 size_t page_size
= 1UL << umem_odp
->page_shift
;
64 start
= ALIGN_DOWN(umem_odp
->umem
.address
, page_size
);
65 if (check_add_overflow(umem_odp
->umem
.address
,
66 (unsigned long)umem_odp
->umem
.length
,
69 end
= ALIGN(end
, page_size
);
70 if (unlikely(end
< page_size
))
73 ndmas
= (end
- start
) >> umem_odp
->page_shift
;
77 npfns
= (end
- start
) >> PAGE_SHIFT
;
78 umem_odp
->pfn_list
= kvcalloc(
79 npfns
, sizeof(*umem_odp
->pfn_list
), GFP_KERNEL
);
80 if (!umem_odp
->pfn_list
)
83 umem_odp
->dma_list
= kvcalloc(
84 ndmas
, sizeof(*umem_odp
->dma_list
), GFP_KERNEL
);
85 if (!umem_odp
->dma_list
) {
90 ret
= mmu_interval_notifier_insert(&umem_odp
->notifier
,
91 umem_odp
->umem
.owning_mm
,
92 start
, end
- start
, ops
);
100 kvfree(umem_odp
->dma_list
);
102 kvfree(umem_odp
->pfn_list
);
107 * ib_umem_odp_alloc_implicit - Allocate a parent implicit ODP umem
109 * Implicit ODP umems do not have a VA range and do not have any page lists.
110 * They exist only to hold the per_mm reference to help the driver create
113 * @device: IB device to create UMEM
114 * @access: ib_reg_mr access flags
116 struct ib_umem_odp
*ib_umem_odp_alloc_implicit(struct ib_device
*device
,
119 struct ib_umem
*umem
;
120 struct ib_umem_odp
*umem_odp
;
123 if (access
& IB_ACCESS_HUGETLB
)
124 return ERR_PTR(-EINVAL
);
126 umem_odp
= kzalloc(sizeof(*umem_odp
), GFP_KERNEL
);
128 return ERR_PTR(-ENOMEM
);
129 umem
= &umem_odp
->umem
;
130 umem
->ibdev
= device
;
131 umem
->writable
= ib_access_writable(access
);
132 umem
->owning_mm
= current
->mm
;
133 umem_odp
->is_implicit_odp
= 1;
134 umem_odp
->page_shift
= PAGE_SHIFT
;
136 umem_odp
->tgid
= get_task_pid(current
->group_leader
, PIDTYPE_PID
);
137 ret
= ib_init_umem_odp(umem_odp
, NULL
);
139 put_pid(umem_odp
->tgid
);
145 EXPORT_SYMBOL(ib_umem_odp_alloc_implicit
);
148 * ib_umem_odp_alloc_child - Allocate a child ODP umem under an implicit
151 * @root: The parent umem enclosing the child. This must be allocated using
152 * ib_alloc_implicit_odp_umem()
153 * @addr: The starting userspace VA
154 * @size: The length of the userspace VA
155 * @ops: MMU interval ops, currently only @invalidate
158 ib_umem_odp_alloc_child(struct ib_umem_odp
*root
, unsigned long addr
,
160 const struct mmu_interval_notifier_ops
*ops
)
163 * Caller must ensure that root cannot be freed during the call to
166 struct ib_umem_odp
*odp_data
;
167 struct ib_umem
*umem
;
170 if (WARN_ON(!root
->is_implicit_odp
))
171 return ERR_PTR(-EINVAL
);
173 odp_data
= kzalloc(sizeof(*odp_data
), GFP_KERNEL
);
175 return ERR_PTR(-ENOMEM
);
176 umem
= &odp_data
->umem
;
177 umem
->ibdev
= root
->umem
.ibdev
;
179 umem
->address
= addr
;
180 umem
->writable
= root
->umem
.writable
;
181 umem
->owning_mm
= root
->umem
.owning_mm
;
182 odp_data
->page_shift
= PAGE_SHIFT
;
183 odp_data
->notifier
.ops
= ops
;
186 * A mmget must be held when registering a notifier, the owming_mm only
187 * has a mm_grab at this point.
189 if (!mmget_not_zero(umem
->owning_mm
)) {
194 odp_data
->tgid
= get_pid(root
->tgid
);
195 ret
= ib_init_umem_odp(odp_data
, ops
);
198 mmput(umem
->owning_mm
);
202 put_pid(odp_data
->tgid
);
203 mmput(umem
->owning_mm
);
208 EXPORT_SYMBOL(ib_umem_odp_alloc_child
);
211 * ib_umem_odp_get - Create a umem_odp for a userspace va
213 * @device: IB device struct to get UMEM
214 * @addr: userspace virtual address to start at
215 * @size: length of region to pin
216 * @access: IB_ACCESS_xxx flags for memory being pinned
217 * @ops: MMU interval ops, currently only @invalidate
219 * The driver should use when the access flags indicate ODP memory. It avoids
220 * pinning, instead, stores the mm for future page fault handling in
221 * conjunction with MMU notifiers.
223 struct ib_umem_odp
*ib_umem_odp_get(struct ib_device
*device
,
224 unsigned long addr
, size_t size
, int access
,
225 const struct mmu_interval_notifier_ops
*ops
)
227 struct ib_umem_odp
*umem_odp
;
230 if (WARN_ON_ONCE(!(access
& IB_ACCESS_ON_DEMAND
)))
231 return ERR_PTR(-EINVAL
);
233 umem_odp
= kzalloc(sizeof(struct ib_umem_odp
), GFP_KERNEL
);
235 return ERR_PTR(-ENOMEM
);
237 umem_odp
->umem
.ibdev
= device
;
238 umem_odp
->umem
.length
= size
;
239 umem_odp
->umem
.address
= addr
;
240 umem_odp
->umem
.writable
= ib_access_writable(access
);
241 umem_odp
->umem
.owning_mm
= current
->mm
;
242 umem_odp
->notifier
.ops
= ops
;
244 umem_odp
->page_shift
= PAGE_SHIFT
;
245 #ifdef CONFIG_HUGETLB_PAGE
246 if (access
& IB_ACCESS_HUGETLB
)
247 umem_odp
->page_shift
= HPAGE_SHIFT
;
250 umem_odp
->tgid
= get_task_pid(current
->group_leader
, PIDTYPE_PID
);
251 ret
= ib_init_umem_odp(umem_odp
, ops
);
257 put_pid(umem_odp
->tgid
);
261 EXPORT_SYMBOL(ib_umem_odp_get
);
263 void ib_umem_odp_release(struct ib_umem_odp
*umem_odp
)
266 * Ensure that no more pages are mapped in the umem.
268 * It is the driver's responsibility to ensure, before calling us,
269 * that the hardware will not attempt to access the MR any more.
271 if (!umem_odp
->is_implicit_odp
) {
272 mutex_lock(&umem_odp
->umem_mutex
);
273 ib_umem_odp_unmap_dma_pages(umem_odp
, ib_umem_start(umem_odp
),
274 ib_umem_end(umem_odp
));
275 mutex_unlock(&umem_odp
->umem_mutex
);
276 mmu_interval_notifier_remove(&umem_odp
->notifier
);
277 kvfree(umem_odp
->dma_list
);
278 kvfree(umem_odp
->pfn_list
);
280 put_pid(umem_odp
->tgid
);
283 EXPORT_SYMBOL(ib_umem_odp_release
);
286 * Map for DMA and insert a single page into the on-demand paging page tables.
288 * @umem: the umem to insert the page to.
289 * @dma_index: index in the umem to add the dma to.
290 * @page: the page struct to map and add.
291 * @access_mask: access permissions needed for this page.
293 * The function returns -EFAULT if the DMA mapping operation fails.
296 static int ib_umem_odp_map_dma_single_page(
297 struct ib_umem_odp
*umem_odp
,
298 unsigned int dma_index
,
302 struct ib_device
*dev
= umem_odp
->umem
.ibdev
;
303 dma_addr_t
*dma_addr
= &umem_odp
->dma_list
[dma_index
];
307 * If the page is already dma mapped it means it went through
308 * a non-invalidating trasition, like read-only to writable.
311 *dma_addr
= (*dma_addr
& ODP_DMA_ADDR_MASK
) | access_mask
;
315 *dma_addr
= ib_dma_map_page(dev
, page
, 0, 1 << umem_odp
->page_shift
,
317 if (ib_dma_mapping_error(dev
, *dma_addr
)) {
322 *dma_addr
|= access_mask
;
327 * ib_umem_odp_map_dma_and_lock - DMA map userspace memory in an ODP MR and lock it.
329 * Maps the range passed in the argument to DMA addresses.
330 * The DMA addresses of the mapped pages is updated in umem_odp->dma_list.
331 * Upon success the ODP MR will be locked to let caller complete its device
334 * Returns the number of pages mapped in success, negative error code
336 * @umem_odp: the umem to map and pin
337 * @user_virt: the address from which we need to map.
338 * @bcnt: the minimal number of bytes to pin and map. The mapping might be
339 * bigger due to alignment, and may also be smaller in case of an error
340 * pinning or mapping a page. The actual pages mapped is returned in
342 * @access_mask: bit mask of the requested access permissions for the given
344 * @fault: is faulting required for the given range
346 int ib_umem_odp_map_dma_and_lock(struct ib_umem_odp
*umem_odp
, u64 user_virt
,
347 u64 bcnt
, u64 access_mask
, bool fault
)
348 __acquires(&umem_odp
->umem_mutex
)
350 struct task_struct
*owning_process
= NULL
;
351 struct mm_struct
*owning_mm
= umem_odp
->umem
.owning_mm
;
352 int pfn_index
, dma_index
, ret
= 0, start_idx
;
353 unsigned int page_shift
, hmm_order
, pfn_start_idx
;
354 unsigned long num_pfns
, current_seq
;
355 struct hmm_range range
= {};
356 unsigned long timeout
;
358 if (access_mask
== 0)
361 if (user_virt
< ib_umem_start(umem_odp
) ||
362 user_virt
+ bcnt
> ib_umem_end(umem_odp
))
365 page_shift
= umem_odp
->page_shift
;
368 * owning_process is allowed to be NULL, this means somehow the mm is
369 * existing beyond the lifetime of the originating process.. Presumably
370 * mmget_not_zero will fail in this case.
372 owning_process
= get_pid_task(umem_odp
->tgid
, PIDTYPE_PID
);
373 if (!owning_process
|| !mmget_not_zero(owning_mm
)) {
378 range
.notifier
= &umem_odp
->notifier
;
379 range
.start
= ALIGN_DOWN(user_virt
, 1UL << page_shift
);
380 range
.end
= ALIGN(user_virt
+ bcnt
, 1UL << page_shift
);
381 pfn_start_idx
= (range
.start
- ib_umem_start(umem_odp
)) >> PAGE_SHIFT
;
382 num_pfns
= (range
.end
- range
.start
) >> PAGE_SHIFT
;
384 range
.default_flags
= HMM_PFN_REQ_FAULT
;
386 if (access_mask
& ODP_WRITE_ALLOWED_BIT
)
387 range
.default_flags
|= HMM_PFN_REQ_WRITE
;
390 range
.hmm_pfns
= &(umem_odp
->pfn_list
[pfn_start_idx
]);
391 timeout
= jiffies
+ msecs_to_jiffies(HMM_RANGE_DEFAULT_TIMEOUT
);
394 current_seq
= range
.notifier_seq
=
395 mmu_interval_read_begin(&umem_odp
->notifier
);
397 mmap_read_lock(owning_mm
);
398 ret
= hmm_range_fault(&range
);
399 mmap_read_unlock(owning_mm
);
401 if (ret
== -EBUSY
&& !time_after(jiffies
, timeout
))
406 start_idx
= (range
.start
- ib_umem_start(umem_odp
)) >> page_shift
;
407 dma_index
= start_idx
;
409 mutex_lock(&umem_odp
->umem_mutex
);
410 if (mmu_interval_read_retry(&umem_odp
->notifier
, current_seq
)) {
411 mutex_unlock(&umem_odp
->umem_mutex
);
415 for (pfn_index
= 0; pfn_index
< num_pfns
;
416 pfn_index
+= 1 << (page_shift
- PAGE_SHIFT
), dma_index
++) {
420 * Since we asked for hmm_range_fault() to populate
421 * pages it shouldn't return an error entry on success.
423 WARN_ON(range
.hmm_pfns
[pfn_index
] & HMM_PFN_ERROR
);
424 WARN_ON(!(range
.hmm_pfns
[pfn_index
] & HMM_PFN_VALID
));
426 if (!(range
.hmm_pfns
[pfn_index
] & HMM_PFN_VALID
)) {
427 WARN_ON(umem_odp
->dma_list
[dma_index
]);
430 access_mask
= ODP_READ_ALLOWED_BIT
;
431 if (range
.hmm_pfns
[pfn_index
] & HMM_PFN_WRITE
)
432 access_mask
|= ODP_WRITE_ALLOWED_BIT
;
435 hmm_order
= hmm_pfn_to_map_order(range
.hmm_pfns
[pfn_index
]);
436 /* If a hugepage was detected and ODP wasn't set for, the umem
437 * page_shift will be used, the opposite case is an error.
439 if (hmm_order
+ PAGE_SHIFT
< page_shift
) {
441 ibdev_dbg(umem_odp
->umem
.ibdev
,
442 "%s: un-expected hmm_order %u, page_shift %u\n",
443 __func__
, hmm_order
, page_shift
);
447 ret
= ib_umem_odp_map_dma_single_page(
448 umem_odp
, dma_index
, hmm_pfn_to_page(range
.hmm_pfns
[pfn_index
]),
451 ibdev_dbg(umem_odp
->umem
.ibdev
,
452 "ib_umem_odp_map_dma_single_page failed with error %d\n", ret
);
456 /* upon success lock should stay on hold for the callee */
458 ret
= dma_index
- start_idx
;
460 mutex_unlock(&umem_odp
->umem_mutex
);
463 mmput_async(owning_mm
);
466 put_task_struct(owning_process
);
469 EXPORT_SYMBOL(ib_umem_odp_map_dma_and_lock
);
471 void ib_umem_odp_unmap_dma_pages(struct ib_umem_odp
*umem_odp
, u64 virt
,
478 struct ib_device
*dev
= umem_odp
->umem
.ibdev
;
480 lockdep_assert_held(&umem_odp
->umem_mutex
);
482 virt
= max_t(u64
, virt
, ib_umem_start(umem_odp
));
483 bound
= min_t(u64
, bound
, ib_umem_end(umem_odp
));
484 for (addr
= virt
; addr
< bound
; addr
+= BIT(umem_odp
->page_shift
)) {
485 idx
= (addr
- ib_umem_start(umem_odp
)) >> umem_odp
->page_shift
;
486 dma
= umem_odp
->dma_list
[idx
];
488 /* The access flags guaranteed a valid DMA address in case was NULL */
490 unsigned long pfn_idx
= (addr
- ib_umem_start(umem_odp
)) >> PAGE_SHIFT
;
491 struct page
*page
= hmm_pfn_to_page(umem_odp
->pfn_list
[pfn_idx
]);
493 dma_addr
= dma
& ODP_DMA_ADDR_MASK
;
494 ib_dma_unmap_page(dev
, dma_addr
,
495 BIT(umem_odp
->page_shift
),
497 if (dma
& ODP_WRITE_ALLOWED_BIT
) {
498 struct page
*head_page
= compound_head(page
);
500 * set_page_dirty prefers being called with
501 * the page lock. However, MMU notifiers are
502 * called sometimes with and sometimes without
503 * the lock. We rely on the umem_mutex instead
504 * to prevent other mmu notifiers from
505 * continuing and allowing the page mapping to
508 set_page_dirty(head_page
);
510 umem_odp
->dma_list
[idx
] = 0;
515 EXPORT_SYMBOL(ib_umem_odp_unmap_dma_pages
);