Skip to content

Commit ba470de

Browse files
Rik van Rieltorvalds
authored andcommitted
mmap: handle mlocked pages during map, remap, unmap
Originally by Nick Piggin <npiggin@suse.de> Remove mlocked pages from the LRU using "unevictable infrastructure" during mmap(), munmap(), mremap() and truncate(). Try to move back to normal LRU lists on munmap() when last mlocked mapping removed. Remove PageMlocked() status when page truncated from file. [akpm@linux-foundation.org: cleanup] [kamezawa.hiroyu@jp.fujitsu.com: fix double unlock_page()] [kosaki.motohiro@jp.fujitsu.com: split LRU: munlock rework] [lee.schermerhorn@hp.com: mlock: fix __mlock_vma_pages_range comment block] [akpm@linux-foundation.org: remove bogus kerneldoc token] Signed-off-by: Nick Piggin <npiggin@suse.de> Signed-off-by: Lee Schermerhorn <lee.schermerhorn@hp.com> Signed-off-by: Rik van Riel <riel@redhat.com> Signed-off-by: KOSAKI Motohiro <kosaki.motohiro@jp.fujitsu.com> Signed-off-by: KAMEZAWA Hiroyuki <kamewzawa.hiroyu@jp.fujitsu.com> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
1 parent 8edb08c commit ba470de

6 files changed

Lines changed: 180 additions & 160 deletions

File tree

‎mm/fremap.c‎

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121
#include <asm/cacheflush.h>
2222
#include <asm/tlbflush.h>
2323

24+
#include "internal.h"
25+
2426
static void zap_pte(struct mm_struct *mm, struct vm_area_struct *vma,
2527
unsigned long addr, pte_t *ptep)
2628
{
@@ -215,15 +217,31 @@ asmlinkage long sys_remap_file_pages(unsigned long start, unsigned long size,
215217
spin_unlock(&mapping->i_mmap_lock);
216218
}
217219

220+
if (vma->vm_flags & VM_LOCKED) {
221+
/*
222+
* drop PG_Mlocked flag for over-mapped range
223+
*/
224+
unsigned int saved_flags = vma->vm_flags;
225+
munlock_vma_pages_range(vma, start, start + size);
226+
vma->vm_flags = saved_flags;
227+
}
228+
218229
mmu_notifier_invalidate_range_start(mm, start, start + size);
219230
err = populate_range(mm, vma, start, size, pgoff);
220231
mmu_notifier_invalidate_range_end(mm, start, start + size);
221232
if (!err && !(flags & MAP_NONBLOCK)) {
222-
if (unlikely(has_write_lock)) {
223-
downgrade_write(&mm->mmap_sem);
224-
has_write_lock = 0;
233+
if (vma->vm_flags & VM_LOCKED) {
234+
/*
235+
* might be mapping previously unmapped range of file
236+
*/
237+
mlock_vma_pages_range(vma, start, start + size);
238+
} else {
239+
if (unlikely(has_write_lock)) {
240+
downgrade_write(&mm->mmap_sem);
241+
has_write_lock = 0;
242+
}
243+
make_pages_present(start, start+size);
225244
}
226-
make_pages_present(start, start+size);
227245
}
228246

229247
/*
@@ -240,4 +258,3 @@ asmlinkage long sys_remap_file_pages(unsigned long start, unsigned long size,
240258

241259
return err;
242260
}
243-

‎mm/internal.h‎

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,9 +61,14 @@ static inline unsigned long page_order(struct page *page)
6161
return page_private(page);
6262
}
6363

64-
extern int mlock_vma_pages_range(struct vm_area_struct *vma,
64+
extern long mlock_vma_pages_range(struct vm_area_struct *vma,
6565
unsigned long start, unsigned long end);
66-
extern void munlock_vma_pages_all(struct vm_area_struct *vma);
66+
extern void munlock_vma_pages_range(struct vm_area_struct *vma,
67+
unsigned long start, unsigned long end);
68+
static inline void munlock_vma_pages_all(struct vm_area_struct *vma)
69+
{
70+
munlock_vma_pages_range(vma, vma->vm_start, vma->vm_end);
71+
}
6772

6873
#ifdef CONFIG_UNEVICTABLE_LRU
6974
/*

‎mm/mlock.c‎

Lines changed: 90 additions & 131 deletions
Original file line numberDiff line numberDiff line change
@@ -112,26 +112,49 @@ static void munlock_vma_page(struct page *page)
112112
}
113113
}
114114

115-
/*
116-
* mlock a range of pages in the vma.
115+
/**
116+
* __mlock_vma_pages_range() - mlock/munlock a range of pages in the vma.
117+
* @vma: target vma
118+
* @start: start address
119+
* @end: end address
120+
* @mlock: 0 indicate munlock, otherwise mlock.
121+
*
122+
* If @mlock == 0, unlock an mlocked range;
123+
* else mlock the range of pages. This takes care of making the pages present ,
124+
* too.
117125
*
118-
* This takes care of making the pages present too.
126+
* return 0 on success, negative error code on error.
119127
*
120-
* vma->vm_mm->mmap_sem must be held for write.
128+
* vma->vm_mm->mmap_sem must be held for at least read.
121129
*/
122-
static int __mlock_vma_pages_range(struct vm_area_struct *vma,
123-
unsigned long start, unsigned long end)
130+
static long __mlock_vma_pages_range(struct vm_area_struct *vma,
131+
unsigned long start, unsigned long end,
132+
int mlock)
124133
{
125134
struct mm_struct *mm = vma->vm_mm;
126135
unsigned long addr = start;
127136
struct page *pages[16]; /* 16 gives a reasonable batch */
128-
int write = !!(vma->vm_flags & VM_WRITE);
129137
int nr_pages = (end - start) / PAGE_SIZE;
130138
int ret;
139+
int gup_flags = 0;
131140

132-
VM_BUG_ON(start & ~PAGE_MASK || end & ~PAGE_MASK);
133-
VM_BUG_ON(start < vma->vm_start || end > vma->vm_end);
134-
VM_BUG_ON(!rwsem_is_locked(&vma->vm_mm->mmap_sem));
141+
VM_BUG_ON(start & ~PAGE_MASK);
142+
VM_BUG_ON(end & ~PAGE_MASK);
143+
VM_BUG_ON(start < vma->vm_start);
144+
VM_BUG_ON(end > vma->vm_end);
145+
VM_BUG_ON((!rwsem_is_locked(&mm->mmap_sem)) &&
146+
(atomic_read(&mm->mm_users) != 0));
147+
148+
/*
149+
* mlock: don't page populate if page has PROT_NONE permission.
150+
* munlock: the pages always do munlock althrough
151+
* its has PROT_NONE permission.
152+
*/
153+
if (!mlock)
154+
gup_flags |= GUP_FLAGS_IGNORE_VMA_PERMISSIONS;
155+
156+
if (vma->vm_flags & VM_WRITE)
157+
gup_flags |= GUP_FLAGS_WRITE;
135158

136159
lru_add_drain_all(); /* push cached pages to LRU */
137160

@@ -146,9 +169,9 @@ static int __mlock_vma_pages_range(struct vm_area_struct *vma,
146169
* disable migration of this page. However, page may
147170
* still be truncated out from under us.
148171
*/
149-
ret = get_user_pages(current, mm, addr,
172+
ret = __get_user_pages(current, mm, addr,
150173
min_t(int, nr_pages, ARRAY_SIZE(pages)),
151-
write, 0, pages, NULL);
174+
gup_flags, pages, NULL);
152175
/*
153176
* This can happen for, e.g., VM_NONLINEAR regions before
154177
* a page has been allocated and mapped at a given offset,
@@ -178,8 +201,12 @@ static int __mlock_vma_pages_range(struct vm_area_struct *vma,
178201
* by the elevated reference, we need only check for
179202
* page truncation (file-cache only).
180203
*/
181-
if (page->mapping)
182-
mlock_vma_page(page);
204+
if (page->mapping) {
205+
if (mlock)
206+
mlock_vma_page(page);
207+
else
208+
munlock_vma_page(page);
209+
}
183210
unlock_page(page);
184211
put_page(page); /* ref from get_user_pages() */
185212

@@ -197,125 +224,38 @@ static int __mlock_vma_pages_range(struct vm_area_struct *vma,
197224
return 0; /* count entire vma as locked_vm */
198225
}
199226

200-
/*
201-
* private structure for munlock page table walk
202-
*/
203-
struct munlock_page_walk {
204-
struct vm_area_struct *vma;
205-
pmd_t *pmd; /* for migration_entry_wait() */
206-
};
207-
208-
/*
209-
* munlock normal pages for present ptes
210-
*/
211-
static int __munlock_pte_handler(pte_t *ptep, unsigned long addr,
212-
unsigned long end, struct mm_walk *walk)
213-
{
214-
struct munlock_page_walk *mpw = walk->private;
215-
swp_entry_t entry;
216-
struct page *page;
217-
pte_t pte;
218-
219-
retry:
220-
pte = *ptep;
221-
/*
222-
* If it's a swap pte, we might be racing with page migration.
223-
*/
224-
if (unlikely(!pte_present(pte))) {
225-
if (!is_swap_pte(pte))
226-
goto out;
227-
entry = pte_to_swp_entry(pte);
228-
if (is_migration_entry(entry)) {
229-
migration_entry_wait(mpw->vma->vm_mm, mpw->pmd, addr);
230-
goto retry;
231-
}
232-
goto out;
233-
}
234-
235-
page = vm_normal_page(mpw->vma, addr, pte);
236-
if (!page)
237-
goto out;
238-
239-
lock_page(page);
240-
if (!page->mapping) {
241-
unlock_page(page);
242-
goto retry;
243-
}
244-
munlock_vma_page(page);
245-
unlock_page(page);
246-
247-
out:
248-
return 0;
249-
}
250-
251-
/*
252-
* Save pmd for pte handler for waiting on migration entries
253-
*/
254-
static int __munlock_pmd_handler(pmd_t *pmd, unsigned long addr,
255-
unsigned long end, struct mm_walk *walk)
256-
{
257-
struct munlock_page_walk *mpw = walk->private;
258-
259-
mpw->pmd = pmd;
260-
return 0;
261-
}
262-
263-
264-
/*
265-
* munlock a range of pages in the vma using standard page table walk.
266-
*
267-
* vma->vm_mm->mmap_sem must be held for write.
268-
*/
269-
static void __munlock_vma_pages_range(struct vm_area_struct *vma,
270-
unsigned long start, unsigned long end)
271-
{
272-
struct mm_struct *mm = vma->vm_mm;
273-
struct munlock_page_walk mpw = {
274-
.vma = vma,
275-
};
276-
struct mm_walk munlock_page_walk = {
277-
.pmd_entry = __munlock_pmd_handler,
278-
.pte_entry = __munlock_pte_handler,
279-
.private = &mpw,
280-
.mm = mm,
281-
};
282-
283-
VM_BUG_ON(start & ~PAGE_MASK || end & ~PAGE_MASK);
284-
VM_BUG_ON(!rwsem_is_locked(&vma->vm_mm->mmap_sem));
285-
VM_BUG_ON(start < vma->vm_start);
286-
VM_BUG_ON(end > vma->vm_end);
287-
288-
lru_add_drain_all(); /* push cached pages to LRU */
289-
walk_page_range(start, end, &munlock_page_walk);
290-
lru_add_drain_all(); /* to update stats */
291-
}
292-
293227
#else /* CONFIG_UNEVICTABLE_LRU */
294228

295229
/*
296230
* Just make pages present if VM_LOCKED. No-op if unlocking.
297231
*/
298-
static int __mlock_vma_pages_range(struct vm_area_struct *vma,
299-
unsigned long start, unsigned long end)
232+
static long __mlock_vma_pages_range(struct vm_area_struct *vma,
233+
unsigned long start, unsigned long end,
234+
int mlock)
300235
{
301-
if (vma->vm_flags & VM_LOCKED)
236+
if (mlock && (vma->vm_flags & VM_LOCKED))
302237
make_pages_present(start, end);
303238
return 0;
304239
}
305-
306-
/*
307-
* munlock a range of pages in the vma -- no-op.
308-
*/
309-
static void __munlock_vma_pages_range(struct vm_area_struct *vma,
310-
unsigned long start, unsigned long end)
311-
{
312-
}
313240
#endif /* CONFIG_UNEVICTABLE_LRU */
314241

315-
/*
316-
* mlock all pages in this vma range. For mmap()/mremap()/...
242+
/**
243+
* mlock_vma_pages_range() - mlock pages in specified vma range.
244+
* @vma - the vma containing the specfied address range
245+
* @start - starting address in @vma to mlock
246+
* @end - end address [+1] in @vma to mlock
247+
*
248+
* For mmap()/mremap()/expansion of mlocked vma.
249+
*
250+
* return 0 on success for "normal" vmas.
251+
*
252+
* return number of pages [> 0] to be removed from locked_vm on success
253+
* of "special" vmas.
254+
*
255+
* return negative error if vma spanning @start-@range disappears while
256+
* mmap semaphore is dropped. Unlikely?
317257
*/
318-
int mlock_vma_pages_range(struct vm_area_struct *vma,
258+
long mlock_vma_pages_range(struct vm_area_struct *vma,
319259
unsigned long start, unsigned long end)
320260
{
321261
struct mm_struct *mm = vma->vm_mm;
@@ -331,17 +271,20 @@ int mlock_vma_pages_range(struct vm_area_struct *vma,
331271
if (!((vma->vm_flags & (VM_DONTEXPAND | VM_RESERVED)) ||
332272
is_vm_hugetlb_page(vma) ||
333273
vma == get_gate_vma(current))) {
274+
long error;
334275
downgrade_write(&mm->mmap_sem);
335-
nr_pages = __mlock_vma_pages_range(vma, start, end);
276+
277+
error = __mlock_vma_pages_range(vma, start, end, 1);
336278

337279
up_read(&mm->mmap_sem);
338280
/* vma can change or disappear */
339281
down_write(&mm->mmap_sem);
340282
vma = find_vma(mm, start);
341283
/* non-NULL vma must contain @start, but need to check @end */
342284
if (!vma || end > vma->vm_end)
343-
return -EAGAIN;
344-
return nr_pages;
285+
return -ENOMEM;
286+
287+
return 0; /* hide other errors from mmap(), et al */
345288
}
346289

347290
/*
@@ -356,17 +299,33 @@ int mlock_vma_pages_range(struct vm_area_struct *vma,
356299

357300
no_mlock:
358301
vma->vm_flags &= ~VM_LOCKED; /* and don't come back! */
359-
return nr_pages; /* pages NOT mlocked */
302+
return nr_pages; /* error or pages NOT mlocked */
360303
}
361304

362305

363306
/*
364-
* munlock all pages in vma. For munmap() and exit().
307+
* munlock_vma_pages_range() - munlock all pages in the vma range.'
308+
* @vma - vma containing range to be munlock()ed.
309+
* @start - start address in @vma of the range
310+
* @end - end of range in @vma.
311+
*
312+
* For mremap(), munmap() and exit().
313+
*
314+
* Called with @vma VM_LOCKED.
315+
*
316+
* Returns with VM_LOCKED cleared. Callers must be prepared to
317+
* deal with this.
318+
*
319+
* We don't save and restore VM_LOCKED here because pages are
320+
* still on lru. In unmap path, pages might be scanned by reclaim
321+
* and re-mlocked by try_to_{munlock|unmap} before we unmap and
322+
* free them. This will result in freeing mlocked pages.
365323
*/
366-
void munlock_vma_pages_all(struct vm_area_struct *vma)
324+
void munlock_vma_pages_range(struct vm_area_struct *vma,
325+
unsigned long start, unsigned long end)
367326
{
368327
vma->vm_flags &= ~VM_LOCKED;
369-
__munlock_vma_pages_range(vma, vma->vm_start, vma->vm_end);
328+
__mlock_vma_pages_range(vma, start, end, 0);
370329
}
371330

372331
/*
@@ -443,7 +402,7 @@ static int mlock_fixup(struct vm_area_struct *vma, struct vm_area_struct **prev,
443402
*/
444403
downgrade_write(&mm->mmap_sem);
445404

446-
ret = __mlock_vma_pages_range(vma, start, end);
405+
ret = __mlock_vma_pages_range(vma, start, end, 1);
447406
if (ret > 0) {
448407
mm->locked_vm -= ret;
449408
ret = 0;
@@ -460,7 +419,7 @@ static int mlock_fixup(struct vm_area_struct *vma, struct vm_area_struct **prev,
460419
*prev = find_vma(mm, start);
461420
/* non-NULL *prev must contain @start, but need to check @end */
462421
if (!(*prev) || end > (*prev)->vm_end)
463-
ret = -EAGAIN;
422+
ret = -ENOMEM;
464423
} else {
465424
/*
466425
* TODO: for unlocking, pages will already be resident, so
@@ -469,7 +428,7 @@ static int mlock_fixup(struct vm_area_struct *vma, struct vm_area_struct **prev,
469428
* while. Should we downgrade the semaphore for both lock
470429
* AND unlock ?
471430
*/
472-
__munlock_vma_pages_range(vma, start, end);
431+
__mlock_vma_pages_range(vma, start, end, 0);
473432
}
474433

475434
out:

0 commit comments

Comments
 (0)