Skip to content

Commit bc3e53f

Browse files
Christoph Lametertorvalds
authored andcommitted
mm: distinguish between mlocked and pinned pages
Some kernel components pin user space memory (infiniband and perf) (by increasing the page count) and account that memory as "mlocked". The difference between mlocking and pinning is: A. mlocked pages are marked with PG_mlocked and are exempt from swapping. Page migration may move them around though. They are kept on a special LRU list. B. Pinned pages cannot be moved because something needs to directly access physical memory. They may not be on any LRU list. I recently saw an mlockalled process where mm->locked_vm became bigger than the virtual size of the process (!) because some memory was accounted for twice: Once when the page was mlocked and once when the Infiniband layer increased the refcount because it needt to pin the RDMA memory. This patch introduces a separate counter for pinned pages and accounts them seperately. Signed-off-by: Christoph Lameter <cl@linux.com> Cc: Mike Marciniszyn <infinipath@qlogic.com> Cc: Roland Dreier <roland@kernel.org> Cc: Sean Hefty <sean.hefty@intel.com> Cc: Hugh Dickins <hughd@google.com> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
1 parent f11c0ca commit bc3e53f

6 files changed

Lines changed: 14 additions & 12 deletions

File tree

‎drivers/infiniband/core/umem.c‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ struct ib_umem *ib_umem_get(struct ib_ucontext *context, unsigned long addr,
136136

137137
down_write(&current->mm->mmap_sem);
138138

139-
locked = npages + current->mm->locked_vm;
139+
locked = npages + current->mm->pinned_vm;
140140
lock_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
141141

142142
if ((locked > lock_limit) && !capable(CAP_IPC_LOCK)) {
@@ -206,7 +206,7 @@ struct ib_umem *ib_umem_get(struct ib_ucontext *context, unsigned long addr,
206206
__ib_umem_release(context->device, umem, 0);
207207
kfree(umem);
208208
} else
209-
current->mm->locked_vm = locked;
209+
current->mm->pinned_vm = locked;
210210

211211
up_write(&current->mm->mmap_sem);
212212
if (vma_list)
@@ -222,7 +222,7 @@ static void ib_umem_account(struct work_struct *work)
222222
struct ib_umem *umem = container_of(work, struct ib_umem, work);
223223

224224
down_write(&umem->mm->mmap_sem);
225-
umem->mm->locked_vm -= umem->diff;
225+
umem->mm->pinned_vm -= umem->diff;
226226
up_write(&umem->mm->mmap_sem);
227227
mmput(umem->mm);
228228
kfree(umem);

‎drivers/infiniband/hw/ipath/ipath_user_pages.c‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ static int __ipath_get_user_pages(unsigned long start_page, size_t num_pages,
7979
goto bail_release;
8080
}
8181

82-
current->mm->locked_vm += num_pages;
82+
current->mm->pinned_vm += num_pages;
8383

8484
ret = 0;
8585
goto bail;
@@ -178,7 +178,7 @@ void ipath_release_user_pages(struct page **p, size_t num_pages)
178178

179179
__ipath_release_user_pages(p, num_pages, 1);
180180

181-
current->mm->locked_vm -= num_pages;
181+
current->mm->pinned_vm -= num_pages;
182182

183183
up_write(&current->mm->mmap_sem);
184184
}
@@ -195,7 +195,7 @@ static void user_pages_account(struct work_struct *_work)
195195
container_of(_work, struct ipath_user_pages_work, work);
196196

197197
down_write(&work->mm->mmap_sem);
198-
work->mm->locked_vm -= work->num_pages;
198+
work->mm->pinned_vm -= work->num_pages;
199199
up_write(&work->mm->mmap_sem);
200200
mmput(work->mm);
201201
kfree(work);

‎drivers/infiniband/hw/qib/qib_user_pages.c‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ static int __qib_get_user_pages(unsigned long start_page, size_t num_pages,
7474
goto bail_release;
7575
}
7676

77-
current->mm->locked_vm += num_pages;
77+
current->mm->pinned_vm += num_pages;
7878

7979
ret = 0;
8080
goto bail;
@@ -151,7 +151,7 @@ void qib_release_user_pages(struct page **p, size_t num_pages)
151151
__qib_release_user_pages(p, num_pages, 1);
152152

153153
if (current->mm) {
154-
current->mm->locked_vm -= num_pages;
154+
current->mm->pinned_vm -= num_pages;
155155
up_write(&current->mm->mmap_sem);
156156
}
157157
}

‎fs/proc/task_mmu.c‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ void task_mem(struct seq_file *m, struct mm_struct *mm)
4444
"VmPeak:\t%8lu kB\n"
4545
"VmSize:\t%8lu kB\n"
4646
"VmLck:\t%8lu kB\n"
47+
"VmPin:\t%8lu kB\n"
4748
"VmHWM:\t%8lu kB\n"
4849
"VmRSS:\t%8lu kB\n"
4950
"VmData:\t%8lu kB\n"
@@ -55,6 +56,7 @@ void task_mem(struct seq_file *m, struct mm_struct *mm)
5556
hiwater_vm << (PAGE_SHIFT-10),
5657
(total_vm - mm->reserved_vm) << (PAGE_SHIFT-10),
5758
mm->locked_vm << (PAGE_SHIFT-10),
59+
mm->pinned_vm << (PAGE_SHIFT-10),
5860
hiwater_rss << (PAGE_SHIFT-10),
5961
total_rss << (PAGE_SHIFT-10),
6062
data << (PAGE_SHIFT-10),

‎include/linux/mm_types.h‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -304,7 +304,7 @@ struct mm_struct {
304304
unsigned long hiwater_rss; /* High-watermark of RSS usage */
305305
unsigned long hiwater_vm; /* High-water virtual memory usage */
306306

307-
unsigned long total_vm, locked_vm, shared_vm, exec_vm;
307+
unsigned long total_vm, locked_vm, pinned_vm, shared_vm, exec_vm;
308308
unsigned long stack_vm, reserved_vm, def_flags, nr_ptes;
309309
unsigned long start_code, end_code, start_data, end_data;
310310
unsigned long start_brk, brk, start_stack;

‎kernel/events/core.c‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3544,7 +3544,7 @@ static void perf_mmap_close(struct vm_area_struct *vma)
35443544
struct ring_buffer *rb = event->rb;
35453545

35463546
atomic_long_sub((size >> PAGE_SHIFT) + 1, &user->locked_vm);
3547-
vma->vm_mm->locked_vm -= event->mmap_locked;
3547+
vma->vm_mm->pinned_vm -= event->mmap_locked;
35483548
rcu_assign_pointer(event->rb, NULL);
35493549
mutex_unlock(&event->mmap_mutex);
35503550

@@ -3625,7 +3625,7 @@ static int perf_mmap(struct file *file, struct vm_area_struct *vma)
36253625

36263626
lock_limit = rlimit(RLIMIT_MEMLOCK);
36273627
lock_limit >>= PAGE_SHIFT;
3628-
locked = vma->vm_mm->locked_vm + extra;
3628+
locked = vma->vm_mm->pinned_vm + extra;
36293629

36303630
if ((locked > lock_limit) && perf_paranoid_tracepoint_raw() &&
36313631
!capable(CAP_IPC_LOCK)) {
@@ -3651,7 +3651,7 @@ static int perf_mmap(struct file *file, struct vm_area_struct *vma)
36513651
atomic_long_add(user_extra, &user->locked_vm);
36523652
event->mmap_locked = extra;
36533653
event->mmap_user = get_current_user();
3654-
vma->vm_mm->locked_vm += event->mmap_locked;
3654+
vma->vm_mm->pinned_vm += event->mmap_locked;
36553655

36563656
unlock:
36573657
if (!ret)

0 commit comments

Comments
 (0)