Skip to content

Commit 261dc53

Browse files
committed
RDMA/odp: Split creating a umem_odp from ib_umem_get
This is the last creation API that is overloaded for both, there is very little code sharing and a driver has to be specifically ready for a umem_odp to be created to use the odp version. Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Leon Romanovsky <[email protected]> Signed-off-by: Jason Gunthorpe <[email protected]>
1 parent f20bef6 commit 261dc53

File tree

5 files changed

+86
-67
lines changed

5 files changed

+86
-67
lines changed

drivers/infiniband/core/umem.c

Lines changed: 5 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -184,9 +184,6 @@ EXPORT_SYMBOL(ib_umem_find_best_pgsz);
184184
/**
185185
* ib_umem_get - Pin and DMA map userspace memory.
186186
*
187-
* If access flags indicate ODP memory, avoid pinning. Instead, stores
188-
* the mm for future page fault handling in conjunction with MMU notifiers.
189-
*
190187
* @udata: userspace context to pin memory for
191188
* @addr: userspace virtual address to start at
192189
* @size: length of region to pin
@@ -231,36 +228,19 @@ struct ib_umem *ib_umem_get(struct ib_udata *udata, unsigned long addr,
231228
if (!can_do_mlock())
232229
return ERR_PTR(-EPERM);
233230

234-
if (access & IB_ACCESS_ON_DEMAND) {
235-
umem = kzalloc(sizeof(struct ib_umem_odp), GFP_KERNEL);
236-
if (!umem)
237-
return ERR_PTR(-ENOMEM);
238-
umem->is_odp = 1;
239-
} else {
240-
umem = kzalloc(sizeof(*umem), GFP_KERNEL);
241-
if (!umem)
242-
return ERR_PTR(-ENOMEM);
243-
}
231+
if (access & IB_ACCESS_ON_DEMAND)
232+
return ERR_PTR(-EOPNOTSUPP);
244233

234+
umem = kzalloc(sizeof(*umem), GFP_KERNEL);
235+
if (!umem)
236+
return ERR_PTR(-ENOMEM);
245237
umem->context = context;
246238
umem->length = size;
247239
umem->address = addr;
248240
umem->writable = ib_access_writable(access);
249241
umem->owning_mm = mm = current->mm;
250242
mmgrab(mm);
251243

252-
if (access & IB_ACCESS_ON_DEMAND) {
253-
if (WARN_ON_ONCE(!context->invalidate_range)) {
254-
ret = -EINVAL;
255-
goto umem_kfree;
256-
}
257-
258-
ret = ib_umem_odp_get(to_ib_umem_odp(umem), access);
259-
if (ret)
260-
goto umem_kfree;
261-
return umem;
262-
}
263-
264244
page_list = (struct page **) __get_free_page(GFP_KERNEL);
265245
if (!page_list) {
266246
ret = -ENOMEM;

drivers/infiniband/core/umem_odp.c

Lines changed: 49 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -335,6 +335,7 @@ static inline int ib_init_umem_odp(struct ib_umem_odp *umem_odp,
335335
&per_mm->umem_tree);
336336
up_write(&per_mm->umem_rwsem);
337337
}
338+
mmgrab(umem_odp->umem.owning_mm);
338339

339340
return 0;
340341

@@ -389,9 +390,6 @@ struct ib_umem_odp *ib_umem_odp_alloc_implicit(struct ib_udata *udata,
389390
kfree(umem_odp);
390391
return ERR_PTR(ret);
391392
}
392-
393-
mmgrab(umem->owning_mm);
394-
395393
return umem_odp;
396394
}
397395
EXPORT_SYMBOL(ib_umem_odp_alloc_implicit);
@@ -435,27 +433,51 @@ struct ib_umem_odp *ib_umem_odp_alloc_child(struct ib_umem_odp *root,
435433
kfree(odp_data);
436434
return ERR_PTR(ret);
437435
}
438-
439-
mmgrab(umem->owning_mm);
440-
441436
return odp_data;
442437
}
443438
EXPORT_SYMBOL(ib_umem_odp_alloc_child);
444439

445440
/**
446-
* ib_umem_odp_get - Complete ib_umem_get()
441+
* ib_umem_odp_get - Create a umem_odp for a userspace va
447442
*
448-
* @umem_odp: The partially configured umem from ib_umem_get()
449-
* @addr: The starting userspace VA
450-
* @access: ib_reg_mr access flags
443+
* @udata: userspace context to pin memory for
444+
* @addr: userspace virtual address to start at
445+
* @size: length of region to pin
446+
* @access: IB_ACCESS_xxx flags for memory being pinned
447+
*
448+
* The driver should use when the access flags indicate ODP memory. It avoids
449+
* pinning, instead, stores the mm for future page fault handling in
450+
* conjunction with MMU notifiers.
451451
*/
452-
int ib_umem_odp_get(struct ib_umem_odp *umem_odp, int access)
452+
struct ib_umem_odp *ib_umem_odp_get(struct ib_udata *udata, unsigned long addr,
453+
size_t size, int access)
453454
{
454-
/*
455-
* NOTE: This must called in a process context where umem->owning_mm
456-
* == current->mm
457-
*/
458-
struct mm_struct *mm = umem_odp->umem.owning_mm;
455+
struct ib_umem_odp *umem_odp;
456+
struct ib_ucontext *context;
457+
struct mm_struct *mm;
458+
int ret;
459+
460+
if (!udata)
461+
return ERR_PTR(-EIO);
462+
463+
context = container_of(udata, struct uverbs_attr_bundle, driver_udata)
464+
->context;
465+
if (!context)
466+
return ERR_PTR(-EIO);
467+
468+
if (WARN_ON_ONCE(!(access & IB_ACCESS_ON_DEMAND)) ||
469+
WARN_ON_ONCE(!context->invalidate_range))
470+
return ERR_PTR(-EINVAL);
471+
472+
umem_odp = kzalloc(sizeof(struct ib_umem_odp), GFP_KERNEL);
473+
if (!umem_odp)
474+
return ERR_PTR(-ENOMEM);
475+
476+
umem_odp->umem.context = context;
477+
umem_odp->umem.length = size;
478+
umem_odp->umem.address = addr;
479+
umem_odp->umem.writable = ib_access_writable(access);
480+
umem_odp->umem.owning_mm = mm = current->mm;
459481

460482
umem_odp->page_shift = PAGE_SHIFT;
461483
if (access & IB_ACCESS_HUGETLB) {
@@ -466,15 +488,24 @@ int ib_umem_odp_get(struct ib_umem_odp *umem_odp, int access)
466488
vma = find_vma(mm, ib_umem_start(umem_odp));
467489
if (!vma || !is_vm_hugetlb_page(vma)) {
468490
up_read(&mm->mmap_sem);
469-
return -EINVAL;
491+
ret = -EINVAL;
492+
goto err_free;
470493
}
471494
h = hstate_vma(vma);
472495
umem_odp->page_shift = huge_page_shift(h);
473496
up_read(&mm->mmap_sem);
474497
}
475498

476-
return ib_init_umem_odp(umem_odp, NULL);
499+
ret = ib_init_umem_odp(umem_odp, NULL);
500+
if (ret)
501+
goto err_free;
502+
return umem_odp;
503+
504+
err_free:
505+
kfree(umem_odp);
506+
return ERR_PTR(ret);
477507
}
508+
EXPORT_SYMBOL(ib_umem_odp_get);
478509

479510
void ib_umem_odp_release(struct ib_umem_odp *umem_odp)
480511
{

drivers/infiniband/hw/mlx5/mem.c

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -56,19 +56,6 @@ void mlx5_ib_cont_pages(struct ib_umem *umem, u64 addr,
5656
struct scatterlist *sg;
5757
int entry;
5858

59-
if (umem->is_odp) {
60-
struct ib_umem_odp *odp = to_ib_umem_odp(umem);
61-
unsigned int page_shift = odp->page_shift;
62-
63-
*ncont = ib_umem_odp_num_pages(odp);
64-
*count = *ncont << (page_shift - PAGE_SHIFT);
65-
*shift = page_shift;
66-
if (order)
67-
*order = ilog2(roundup_pow_of_two(*ncont));
68-
69-
return;
70-
}
71-
7259
addr = addr >> PAGE_SHIFT;
7360
tmp = (unsigned long)addr;
7461
m = find_first_bit(&tmp, BITS_PER_LONG);

drivers/infiniband/hw/mlx5/mr.c

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -784,19 +784,37 @@ static int mr_umem_get(struct mlx5_ib_dev *dev, struct ib_udata *udata,
784784
int *ncont, int *order)
785785
{
786786
struct ib_umem *u;
787-
int err;
788787

789788
*umem = NULL;
790789

791-
u = ib_umem_get(udata, start, length, access_flags, 0);
792-
err = PTR_ERR_OR_ZERO(u);
793-
if (err) {
794-
mlx5_ib_dbg(dev, "umem get failed (%d)\n", err);
795-
return err;
790+
if (access_flags & IB_ACCESS_ON_DEMAND) {
791+
struct ib_umem_odp *odp;
792+
793+
odp = ib_umem_odp_get(udata, start, length, access_flags);
794+
if (IS_ERR(odp)) {
795+
mlx5_ib_dbg(dev, "umem get failed (%ld)\n",
796+
PTR_ERR(odp));
797+
return PTR_ERR(odp);
798+
}
799+
800+
u = &odp->umem;
801+
802+
*page_shift = odp->page_shift;
803+
*ncont = ib_umem_odp_num_pages(odp);
804+
*npages = *ncont << (*page_shift - PAGE_SHIFT);
805+
if (order)
806+
*order = ilog2(roundup_pow_of_two(*ncont));
807+
} else {
808+
u = ib_umem_get(udata, start, length, access_flags, 0);
809+
if (IS_ERR(u)) {
810+
mlx5_ib_dbg(dev, "umem get failed (%ld)\n", PTR_ERR(u));
811+
return PTR_ERR(u);
812+
}
813+
814+
mlx5_ib_cont_pages(u, start, MLX5_MKEY_PAGE_SHIFT_MASK, npages,
815+
page_shift, ncont, order);
796816
}
797817

798-
mlx5_ib_cont_pages(u, start, MLX5_MKEY_PAGE_SHIFT_MASK, npages,
799-
page_shift, ncont, order);
800818
if (!*npages) {
801819
mlx5_ib_warn(dev, "avoid zero region\n");
802820
ib_umem_release(u);

include/rdma/ib_umem_odp.h

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,8 @@ struct ib_ucontext_per_mm {
139139
struct rcu_head rcu;
140140
};
141141

142-
int ib_umem_odp_get(struct ib_umem_odp *umem_odp, int access);
142+
struct ib_umem_odp *ib_umem_odp_get(struct ib_udata *udata, unsigned long addr,
143+
size_t size, int access);
143144
struct ib_umem_odp *ib_umem_odp_alloc_implicit(struct ib_udata *udata,
144145
int access);
145146
struct ib_umem_odp *ib_umem_odp_alloc_child(struct ib_umem_odp *root_umem,
@@ -199,9 +200,11 @@ static inline int ib_umem_mmu_notifier_retry(struct ib_umem_odp *umem_odp,
199200

200201
#else /* CONFIG_INFINIBAND_ON_DEMAND_PAGING */
201202

202-
static inline int ib_umem_odp_get(struct ib_umem_odp *umem_odp, int access)
203+
static inline struct ib_umem_odp *ib_umem_odp_get(struct ib_udata *udata,
204+
unsigned long addr,
205+
size_t size, int access)
203206
{
204-
return -EINVAL;
207+
return ERR_PTR(-EINVAL);
205208
}
206209

207210
static inline void ib_umem_odp_release(struct ib_umem_odp *umem_odp) {}

0 commit comments

Comments
 (0)