Skip to content

Commit 87773dd

Browse files
Shawn Bohrerrolandd
authored andcommitted
IB: ib_umem_release() should decrement mm->pinned_vm from ib_umem_get
In debugging an application that receives -ENOMEM from ib_reg_mr(), I found that ib_umem_get() can fail because the pinned_vm count has wrapped causing it to always be larger than the lock limit even with RLIMIT_MEMLOCK set to RLIM_INFINITY. The wrapping of pinned_vm occurs because the process that calls ib_reg_mr() will have its mm->pinned_vm count incremented. Later a different process with a different mm_struct than the one that allocated the ib_umem struct ends up releasing it which results in decrementing the new processes mm->pinned_vm count past zero and wrapping. I'm not entirely sure what circumstances cause a different process to release the ib_umem than the one that allocated it but the kernel stack trace of the freeing process from my situation looks like the following: Call Trace: [<ffffffff814d64b1>] dump_stack+0x19/0x1b [<ffffffffa0b522a5>] ib_umem_release+0x1f5/0x200 [ib_core] [<ffffffffa0b90681>] mlx4_ib_destroy_qp+0x241/0x440 [mlx4_ib] [<ffffffffa0b4d93c>] ib_destroy_qp+0x12c/0x170 [ib_core] [<ffffffffa0cc7129>] ib_uverbs_close+0x259/0x4e0 [ib_uverbs] [<ffffffff81141cba>] __fput+0xba/0x240 [<ffffffff81141e4e>] ____fput+0xe/0x10 [<ffffffff81060894>] task_work_run+0xc4/0xe0 [<ffffffff810029e5>] do_notify_resume+0x95/0xa0 [<ffffffff814e3dd0>] int_signal+0x12/0x17 The following patch fixes the issue by storing the pid struct of the process that calls ib_umem_get() so that ib_umem_release and/or ib_umem_account() can properly decrement the pinned_vm count of the correct mm_struct. Signed-off-by: Shawn Bohrer <[email protected]> Reviewed-by: Shachar Raindel <[email protected]> Signed-off-by: Roland Dreier <[email protected]>
1 parent 52addcf commit 87773dd

File tree

2 files changed

+14
-6
lines changed

2 files changed

+14
-6
lines changed

drivers/infiniband/core/umem.c

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@ struct ib_umem *ib_umem_get(struct ib_ucontext *context, unsigned long addr,
105105
umem->length = size;
106106
umem->offset = addr & ~PAGE_MASK;
107107
umem->page_size = PAGE_SIZE;
108+
umem->pid = get_task_pid(current, PIDTYPE_PID);
108109
/*
109110
* We ask for writable memory if any access flags other than
110111
* "remote read" are set. "Local write" and "remote write"
@@ -198,6 +199,7 @@ struct ib_umem *ib_umem_get(struct ib_ucontext *context, unsigned long addr,
198199
if (ret < 0) {
199200
if (need_release)
200201
__ib_umem_release(context->device, umem, 0);
202+
put_pid(umem->pid);
201203
kfree(umem);
202204
} else
203205
current->mm->pinned_vm = locked;
@@ -230,15 +232,19 @@ void ib_umem_release(struct ib_umem *umem)
230232
{
231233
struct ib_ucontext *context = umem->context;
232234
struct mm_struct *mm;
235+
struct task_struct *task;
233236
unsigned long diff;
234237

235238
__ib_umem_release(umem->context->device, umem, 1);
236239

237-
mm = get_task_mm(current);
238-
if (!mm) {
239-
kfree(umem);
240-
return;
241-
}
240+
task = get_pid_task(umem->pid, PIDTYPE_PID);
241+
put_pid(umem->pid);
242+
if (!task)
243+
goto out;
244+
mm = get_task_mm(task);
245+
put_task_struct(task);
246+
if (!mm)
247+
goto out;
242248

243249
diff = PAGE_ALIGN(umem->length + umem->offset) >> PAGE_SHIFT;
244250

@@ -262,9 +268,10 @@ void ib_umem_release(struct ib_umem *umem)
262268
} else
263269
down_write(&mm->mmap_sem);
264270

265-
current->mm->pinned_vm -= diff;
271+
mm->pinned_vm -= diff;
266272
up_write(&mm->mmap_sem);
267273
mmput(mm);
274+
out:
268275
kfree(umem);
269276
}
270277
EXPORT_SYMBOL(ib_umem_release);

include/rdma/ib_umem.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ struct ib_umem {
4747
int writable;
4848
int hugetlb;
4949
struct work_struct work;
50+
struct pid *pid;
5051
struct mm_struct *mm;
5152
unsigned long diff;
5253
struct sg_table sg_head;

0 commit comments

Comments
 (0)