diff options
Diffstat (limited to 'mm/mlock.c')
| -rw-r--r-- | mm/mlock.c | 644 | 
1 files changed, 444 insertions, 200 deletions
diff --git a/mm/mlock.c b/mm/mlock.c index b70919ce4f7..b1eb5363400 100644 --- a/mm/mlock.c +++ b/mm/mlock.c @@ -11,13 +11,16 @@  #include <linux/swap.h>  #include <linux/swapops.h>  #include <linux/pagemap.h> +#include <linux/pagevec.h>  #include <linux/mempolicy.h>  #include <linux/syscalls.h>  #include <linux/sched.h> -#include <linux/module.h> +#include <linux/export.h>  #include <linux/rmap.h>  #include <linux/mmzone.h>  #include <linux/hugetlb.h> +#include <linux/memcontrol.h> +#include <linux/mm_inline.h>  #include "internal.h" @@ -51,15 +54,13 @@ EXPORT_SYMBOL(can_do_mlock);  /*   *  LRU accounting for clear_page_mlock()   */ -void __clear_page_mlock(struct page *page) +void clear_page_mlock(struct page *page)  { -	VM_BUG_ON(!PageLocked(page)); - -	if (!page->mapping) {	/* truncated ? */ +	if (!TestClearPageMlocked(page))  		return; -	} -	dec_zone_page_state(page, NR_MLOCK); +	mod_zone_page_state(page_zone(page), NR_MLOCK, +			    -hpage_nr_pages(page));  	count_vm_event(UNEVICTABLE_PGCLEARED);  	if (!isolate_lru_page(page)) {  		putback_lru_page(page); @@ -78,19 +79,85 @@ void __clear_page_mlock(struct page *page)   */  void mlock_vma_page(struct page *page)  { +	/* Serialize with page migration */  	BUG_ON(!PageLocked(page));  	if (!TestSetPageMlocked(page)) { -		inc_zone_page_state(page, NR_MLOCK); +		mod_zone_page_state(page_zone(page), NR_MLOCK, +				    hpage_nr_pages(page));  		count_vm_event(UNEVICTABLE_PGMLOCKED);  		if (!isolate_lru_page(page))  			putback_lru_page(page);  	}  } +/* + * Isolate a page from LRU with optional get_page() pin. + * Assumes lru_lock already held and page already pinned. + */ +static bool __munlock_isolate_lru_page(struct page *page, bool getpage) +{ +	if (PageLRU(page)) { +		struct lruvec *lruvec; + +		lruvec = mem_cgroup_page_lruvec(page, page_zone(page)); +		if (getpage) +			get_page(page); +		ClearPageLRU(page); +		del_page_from_lru_list(page, lruvec, page_lru(page)); +		return true; +	} + +	return false; +} + +/* + * Finish munlock after successful page isolation + * + * Page must be locked. This is a wrapper for try_to_munlock() + * and putback_lru_page() with munlock accounting. + */ +static void __munlock_isolated_page(struct page *page) +{ +	int ret = SWAP_AGAIN; + +	/* +	 * Optimization: if the page was mapped just once, that's our mapping +	 * and we don't need to check all the other vmas. +	 */ +	if (page_mapcount(page) > 1) +		ret = try_to_munlock(page); + +	/* Did try_to_unlock() succeed or punt? */ +	if (ret != SWAP_MLOCK) +		count_vm_event(UNEVICTABLE_PGMUNLOCKED); + +	putback_lru_page(page); +} + +/* + * Accounting for page isolation fail during munlock + * + * Performs accounting when page isolation fails in munlock. There is nothing + * else to do because it means some other task has already removed the page + * from the LRU. putback_lru_page() will take care of removing the page from + * the unevictable list, if necessary. vmscan [page_referenced()] will move + * the page back to the unevictable list if some other vma has it mlocked. + */ +static void __munlock_isolation_failed(struct page *page) +{ +	if (PageUnevictable(page)) +		__count_vm_event(UNEVICTABLE_PGSTRANDED); +	else +		__count_vm_event(UNEVICTABLE_PGMUNLOCKED); +} +  /**   * munlock_vma_page - munlock a vma page - * @page - page to be unlocked + * @page - page to be unlocked, either a normal page or THP page head + * + * returns the size of the page as a page mask (0 for normal page, + *         HPAGE_PMD_NR - 1 for THP head page)   *   * called from munlock()/munmap() path with page supposedly on the LRU.   * When we munlock a page, because the vma where we found the page is being @@ -103,43 +170,39 @@ void mlock_vma_page(struct page *page)   * can't isolate the page, we leave it for putback_lru_page() and vmscan   * [page_referenced()/try_to_unmap()] to deal with.   */ -void munlock_vma_page(struct page *page) +unsigned int munlock_vma_page(struct page *page)  { +	unsigned int nr_pages; +	struct zone *zone = page_zone(page); + +	/* For try_to_munlock() and to serialize with page migration */  	BUG_ON(!PageLocked(page)); -	if (TestClearPageMlocked(page)) { -		dec_zone_page_state(page, NR_MLOCK); -		if (!isolate_lru_page(page)) { -			int ret = try_to_munlock(page); -			/* -			 * did try_to_unlock() succeed or punt? -			 */ -			if (ret != SWAP_MLOCK) -				count_vm_event(UNEVICTABLE_PGMUNLOCKED); +	/* +	 * Serialize with any parallel __split_huge_page_refcount() which +	 * might otherwise copy PageMlocked to part of the tail pages before +	 * we clear it in the head page. It also stabilizes hpage_nr_pages(). +	 */ +	spin_lock_irq(&zone->lru_lock); -			putback_lru_page(page); -		} else { -			/* -			 * Some other task has removed the page from the LRU. -			 * putback_lru_page() will take care of removing the -			 * page from the unevictable list, if necessary. -			 * vmscan [page_referenced()] will move the page back -			 * to the unevictable list if some other vma has it -			 * mlocked. -			 */ -			if (PageUnevictable(page)) -				count_vm_event(UNEVICTABLE_PGSTRANDED); -			else -				count_vm_event(UNEVICTABLE_PGMUNLOCKED); -		} +	nr_pages = hpage_nr_pages(page); +	if (!TestClearPageMlocked(page)) +		goto unlock_out; + +	__mod_zone_page_state(zone, NR_MLOCK, -nr_pages); + +	if (__munlock_isolate_lru_page(page, true)) { +		spin_unlock_irq(&zone->lru_lock); +		__munlock_isolated_page(page); +		goto out;  	} -} +	__munlock_isolation_failed(page); -static inline int stack_guard_page(struct vm_area_struct *vma, unsigned long addr) -{ -	return (vma->vm_flags & VM_GROWSDOWN) && -		(vma->vm_start == addr) && -		!vma_stack_continue(vma->vm_prev, addr); +unlock_out: +	spin_unlock_irq(&zone->lru_lock); + +out: +	return nr_pages - 1;  }  /** @@ -154,14 +217,11 @@ static inline int stack_guard_page(struct vm_area_struct *vma, unsigned long add   *   * vma->vm_mm->mmap_sem must be held for at least read.   */ -static long __mlock_vma_pages_range(struct vm_area_struct *vma, -				    unsigned long start, unsigned long end) +long __mlock_vma_pages_range(struct vm_area_struct *vma, +		unsigned long start, unsigned long end, int *nonblocking)  {  	struct mm_struct *mm = vma->vm_mm; -	unsigned long addr = start; -	struct page *pages[16]; /* 16 gives a reasonable batch */ -	int nr_pages = (end - start) / PAGE_SIZE; -	int ret = 0; +	unsigned long nr_pages = (end - start) / PAGE_SIZE;  	int gup_flags;  	VM_BUG_ON(start & ~PAGE_MASK); @@ -170,73 +230,28 @@ static long __mlock_vma_pages_range(struct vm_area_struct *vma,  	VM_BUG_ON(end   > vma->vm_end);  	VM_BUG_ON(!rwsem_is_locked(&mm->mmap_sem)); -	gup_flags = FOLL_TOUCH | FOLL_GET; -	if (vma->vm_flags & VM_WRITE) +	gup_flags = FOLL_TOUCH | FOLL_MLOCK; +	/* +	 * We want to touch writable mappings with a write fault in order +	 * to break COW, except for shared mappings because these don't COW +	 * and we would not want to dirty them for nothing. +	 */ +	if ((vma->vm_flags & (VM_WRITE | VM_SHARED)) == VM_WRITE)  		gup_flags |= FOLL_WRITE; -	/* We don't try to access the guard page of a stack vma */ -	if (stack_guard_page(vma, start)) { -		addr += PAGE_SIZE; -		nr_pages--; -	} - -	while (nr_pages > 0) { -		int i; - -		cond_resched(); - -		/* -		 * get_user_pages makes pages present if we are -		 * setting mlock. and this extra reference count will -		 * disable migration of this page.  However, page may -		 * still be truncated out from under us. -		 */ -		ret = __get_user_pages(current, mm, addr, -				min_t(int, nr_pages, ARRAY_SIZE(pages)), -				gup_flags, pages, NULL); -		/* -		 * This can happen for, e.g., VM_NONLINEAR regions before -		 * a page has been allocated and mapped at a given offset, -		 * or for addresses that map beyond end of a file. -		 * We'll mlock the pages if/when they get faulted in. -		 */ -		if (ret < 0) -			break; - -		lru_add_drain();	/* push cached pages to LRU */ - -		for (i = 0; i < ret; i++) { -			struct page *page = pages[i]; - -			if (page->mapping) { -				/* -				 * That preliminary check is mainly to avoid -				 * the pointless overhead of lock_page on the -				 * ZERO_PAGE: which might bounce very badly if -				 * there is contention.  However, we're still -				 * dirtying its cacheline with get/put_page: -				 * we'll add another __get_user_pages flag to -				 * avoid it if that case turns out to matter. -				 */ -				lock_page(page); -				/* -				 * Because we lock page here and migration is -				 * blocked by the elevated reference, we need -				 * only check for file-cache page truncation. -				 */ -				if (page->mapping) -					mlock_vma_page(page); -				unlock_page(page); -			} -			put_page(page);	/* ref from get_user_pages() */ -		} - -		addr += ret * PAGE_SIZE; -		nr_pages -= ret; -		ret = 0; -	} +	/* +	 * We want mlock to succeed for regions that have any permissions +	 * other than PROT_NONE. +	 */ +	if (vma->vm_flags & (VM_READ | VM_WRITE | VM_EXEC)) +		gup_flags |= FOLL_FORCE; -	return ret;	/* 0 or negative error code */ +	/* +	 * We made sure addr is within a VMA, so the following will +	 * not result in a stack expansion that recurses back here. +	 */ +	return __get_user_pages(current, mm, start, nr_pages, gup_flags, +				NULL, NULL, nonblocking);  }  /* @@ -251,54 +266,188 @@ static int __mlock_posix_error_return(long retval)  	return retval;  } -/** - * mlock_vma_pages_range() - mlock pages in specified vma range. - * @vma - the vma containing the specfied address range - * @start - starting address in @vma to mlock - * @end   - end address [+1] in @vma to mlock - * - * For mmap()/mremap()/expansion of mlocked vma. +/* + * Prepare page for fast batched LRU putback via putback_lru_evictable_pagevec()   * - * return 0 on success for "normal" vmas. + * The fast path is available only for evictable pages with single mapping. + * Then we can bypass the per-cpu pvec and get better performance. + * when mapcount > 1 we need try_to_munlock() which can fail. + * when !page_evictable(), we need the full redo logic of putback_lru_page to + * avoid leaving evictable page in unevictable list.   * - * return number of pages [> 0] to be removed from locked_vm on success - * of "special" vmas. + * In case of success, @page is added to @pvec and @pgrescued is incremented + * in case that the page was previously unevictable. @page is also unlocked.   */ -long mlock_vma_pages_range(struct vm_area_struct *vma, -			unsigned long start, unsigned long end) +static bool __putback_lru_fast_prepare(struct page *page, struct pagevec *pvec, +		int *pgrescued)  { -	int nr_pages = (end - start) / PAGE_SIZE; -	BUG_ON(!(vma->vm_flags & VM_LOCKED)); +	VM_BUG_ON_PAGE(PageLRU(page), page); +	VM_BUG_ON_PAGE(!PageLocked(page), page); + +	if (page_mapcount(page) <= 1 && page_evictable(page)) { +		pagevec_add(pvec, page); +		if (TestClearPageUnevictable(page)) +			(*pgrescued)++; +		unlock_page(page); +		return true; +	} +	return false; +} + +/* + * Putback multiple evictable pages to the LRU + * + * Batched putback of evictable pages that bypasses the per-cpu pvec. Some of + * the pages might have meanwhile become unevictable but that is OK. + */ +static void __putback_lru_fast(struct pagevec *pvec, int pgrescued) +{ +	count_vm_events(UNEVICTABLE_PGMUNLOCKED, pagevec_count(pvec));  	/* -	 * filter unlockable vmas +	 *__pagevec_lru_add() calls release_pages() so we don't call +	 * put_page() explicitly  	 */ -	if (vma->vm_flags & (VM_IO | VM_PFNMAP)) -		goto no_mlock; +	__pagevec_lru_add(pvec); +	count_vm_events(UNEVICTABLE_PGRESCUED, pgrescued); +} + +/* + * Munlock a batch of pages from the same zone + * + * The work is split to two main phases. First phase clears the Mlocked flag + * and attempts to isolate the pages, all under a single zone lru lock. + * The second phase finishes the munlock only for pages where isolation + * succeeded. + * + * Note that the pagevec may be modified during the process. + */ +static void __munlock_pagevec(struct pagevec *pvec, struct zone *zone) +{ +	int i; +	int nr = pagevec_count(pvec); +	int delta_munlocked; +	struct pagevec pvec_putback; +	int pgrescued = 0; -	if (!((vma->vm_flags & (VM_DONTEXPAND | VM_RESERVED)) || -			is_vm_hugetlb_page(vma) || -			vma == get_gate_vma(current))) { +	pagevec_init(&pvec_putback, 0); -		__mlock_vma_pages_range(vma, start, end); +	/* Phase 1: page isolation */ +	spin_lock_irq(&zone->lru_lock); +	for (i = 0; i < nr; i++) { +		struct page *page = pvec->pages[i]; -		/* Hide errors from mmap() and other callers */ -		return 0; +		if (TestClearPageMlocked(page)) { +			/* +			 * We already have pin from follow_page_mask() +			 * so we can spare the get_page() here. +			 */ +			if (__munlock_isolate_lru_page(page, false)) +				continue; +			else +				__munlock_isolation_failed(page); +		} + +		/* +		 * We won't be munlocking this page in the next phase +		 * but we still need to release the follow_page_mask() +		 * pin. We cannot do it under lru_lock however. If it's +		 * the last pin, __page_cache_release() would deadlock. +		 */ +		pagevec_add(&pvec_putback, pvec->pages[i]); +		pvec->pages[i] = NULL; +	} +	delta_munlocked = -nr + pagevec_count(&pvec_putback); +	__mod_zone_page_state(zone, NR_MLOCK, delta_munlocked); +	spin_unlock_irq(&zone->lru_lock); + +	/* Now we can release pins of pages that we are not munlocking */ +	pagevec_release(&pvec_putback); + +	/* Phase 2: page munlock */ +	for (i = 0; i < nr; i++) { +		struct page *page = pvec->pages[i]; + +		if (page) { +			lock_page(page); +			if (!__putback_lru_fast_prepare(page, &pvec_putback, +					&pgrescued)) { +				/* +				 * Slow path. We don't want to lose the last +				 * pin before unlock_page() +				 */ +				get_page(page); /* for putback_lru_page() */ +				__munlock_isolated_page(page); +				unlock_page(page); +				put_page(page); /* from follow_page_mask() */ +			} +		}  	}  	/* -	 * User mapped kernel pages or huge pages: -	 * make these pages present to populate the ptes, but -	 * fall thru' to reset VM_LOCKED--no need to unlock, and -	 * return nr_pages so these don't get counted against task's -	 * locked limit.  huge pages are already counted against -	 * locked vm limit. +	 * Phase 3: page putback for pages that qualified for the fast path +	 * This will also call put_page() to return pin from follow_page_mask()  	 */ -	make_pages_present(start, end); +	if (pagevec_count(&pvec_putback)) +		__putback_lru_fast(&pvec_putback, pgrescued); +} + +/* + * Fill up pagevec for __munlock_pagevec using pte walk + * + * The function expects that the struct page corresponding to @start address is + * a non-TPH page already pinned and in the @pvec, and that it belongs to @zone. + * + * The rest of @pvec is filled by subsequent pages within the same pmd and same + * zone, as long as the pte's are present and vm_normal_page() succeeds. These + * pages also get pinned. + * + * Returns the address of the next page that should be scanned. This equals + * @start + PAGE_SIZE when no page could be added by the pte walk. + */ +static unsigned long __munlock_pagevec_fill(struct pagevec *pvec, +		struct vm_area_struct *vma, int zoneid,	unsigned long start, +		unsigned long end) +{ +	pte_t *pte; +	spinlock_t *ptl; -no_mlock: -	vma->vm_flags &= ~VM_LOCKED;	/* and don't come back! */ -	return nr_pages;		/* error or pages NOT mlocked */ +	/* +	 * Initialize pte walk starting at the already pinned page where we +	 * are sure that there is a pte, as it was pinned under the same +	 * mmap_sem write op. +	 */ +	pte = get_locked_pte(vma->vm_mm, start,	&ptl); +	/* Make sure we do not cross the page table boundary */ +	end = pgd_addr_end(start, end); +	end = pud_addr_end(start, end); +	end = pmd_addr_end(start, end); + +	/* The page next to the pinned page is the first we will try to get */ +	start += PAGE_SIZE; +	while (start < end) { +		struct page *page = NULL; +		pte++; +		if (pte_present(*pte)) +			page = vm_normal_page(vma, start, *pte); +		/* +		 * Break if page could not be obtained or the page's node+zone does not +		 * match +		 */ +		if (!page || page_zone_id(page) != zoneid) +			break; + +		get_page(page); +		/* +		 * Increase the address that will be returned *before* the +		 * eventual break due to pvec becoming full by adding the page +		 */ +		start += PAGE_SIZE; +		if (pagevec_add(pvec, page) == 0) +			break; +	} +	pte_unmap_unlock(pte, ptl); +	return start;  }  /* @@ -322,13 +471,17 @@ no_mlock:  void munlock_vma_pages_range(struct vm_area_struct *vma,  			     unsigned long start, unsigned long end)  { -	unsigned long addr; - -	lru_add_drain();  	vma->vm_flags &= ~VM_LOCKED; -	for (addr = start; addr < end; addr += PAGE_SIZE) { -		struct page *page; +	while (start < end) { +		struct page *page = NULL; +		unsigned int page_mask; +		unsigned long page_increm; +		struct pagevec pvec; +		struct zone *zone; +		int zoneid; + +		pagevec_init(&pvec, 0);  		/*  		 * Although FOLL_DUMP is intended for get_dump_page(),  		 * it just so happens that its special treatment of the @@ -336,20 +489,48 @@ void munlock_vma_pages_range(struct vm_area_struct *vma,  		 * suits munlock very well (and if somehow an abnormal page  		 * has sneaked into the range, we won't oops here: great).  		 */ -		page = follow_page(vma, addr, FOLL_GET | FOLL_DUMP); +		page = follow_page_mask(vma, start, FOLL_GET | FOLL_DUMP, +				&page_mask); +  		if (page && !IS_ERR(page)) { -			lock_page(page); -			/* -			 * Like in __mlock_vma_pages_range(), -			 * because we lock page here and migration is -			 * blocked by the elevated reference, we need -			 * only check for file-cache page truncation. -			 */ -			if (page->mapping) -				munlock_vma_page(page); -			unlock_page(page); -			put_page(page); +			if (PageTransHuge(page)) { +				lock_page(page); +				/* +				 * Any THP page found by follow_page_mask() may +				 * have gotten split before reaching +				 * munlock_vma_page(), so we need to recompute +				 * the page_mask here. +				 */ +				page_mask = munlock_vma_page(page); +				unlock_page(page); +				put_page(page); /* follow_page_mask() */ +			} else { +				/* +				 * Non-huge pages are handled in batches via +				 * pagevec. The pin from follow_page_mask() +				 * prevents them from collapsing by THP. +				 */ +				pagevec_add(&pvec, page); +				zone = page_zone(page); +				zoneid = page_zone_id(page); + +				/* +				 * Try to fill the rest of pagevec using fast +				 * pte walk. This will also update start to +				 * the next page to process. Then munlock the +				 * pagevec. +				 */ +				start = __munlock_pagevec_fill(&pvec, vma, +						zoneid, start, end); +				__munlock_pagevec(&pvec, zone); +				goto next; +			}  		} +		/* It's a bug to munlock in the middle of a THP page */ +		VM_BUG_ON((start >> PAGE_SHIFT) & page_mask); +		page_increm = 1 + page_mask; +		start += page_increm * PAGE_SIZE; +next:  		cond_resched();  	}  } @@ -359,30 +540,22 @@ void munlock_vma_pages_range(struct vm_area_struct *vma,   *   * Filters out "special" vmas -- VM_LOCKED never gets set for these, and   * munlock is a no-op.  However, for some special vmas, we go ahead and - * populate the ptes via make_pages_present(). + * populate the ptes.   *   * For vmas that pass the filters, merge/split as appropriate.   */  static int mlock_fixup(struct vm_area_struct *vma, struct vm_area_struct **prev, -	unsigned long start, unsigned long end, unsigned int newflags) +	unsigned long start, unsigned long end, vm_flags_t newflags)  {  	struct mm_struct *mm = vma->vm_mm;  	pgoff_t pgoff;  	int nr_pages;  	int ret = 0; -	int lock = newflags & VM_LOCKED; - -	if (newflags == vma->vm_flags || -			(vma->vm_flags & (VM_IO | VM_PFNMAP))) -		goto out;	/* don't set VM_LOCKED,  don't count */ +	int lock = !!(newflags & VM_LOCKED); -	if ((vma->vm_flags & (VM_DONTEXPAND | VM_RESERVED)) || -			is_vm_hugetlb_page(vma) || -			vma == get_gate_vma(current)) { -		if (lock) -			make_pages_present(start, end); +	if (newflags == vma->vm_flags || (vma->vm_flags & VM_SPECIAL) || +	    is_vm_hugetlb_page(vma) || vma == get_gate_vma(current->mm))  		goto out;	/* don't set VM_LOCKED,  don't count */ -	}  	pgoff = vma->vm_pgoff + ((start - vma->vm_start) >> PAGE_SHIFT);  	*prev = vma_merge(mm, *prev, start, end, newflags, vma->anon_vma, @@ -419,14 +592,10 @@ success:  	 * set VM_LOCKED, __mlock_vma_pages_range will bring it back.  	 */ -	if (lock) { +	if (lock)  		vma->vm_flags = newflags; -		ret = __mlock_vma_pages_range(vma, start, end); -		if (ret < 0) -			ret = __mlock_posix_error_return(ret); -	} else { +	else  		munlock_vma_pages_range(vma, start, end); -	}  out:  	*prev = vma; @@ -439,27 +608,29 @@ static int do_mlock(unsigned long start, size_t len, int on)  	struct vm_area_struct * vma, * prev;  	int error; -	len = PAGE_ALIGN(len); +	VM_BUG_ON(start & ~PAGE_MASK); +	VM_BUG_ON(len != PAGE_ALIGN(len));  	end = start + len;  	if (end < start)  		return -EINVAL;  	if (end == start)  		return 0; -	vma = find_vma_prev(current->mm, start, &prev); +	vma = find_vma(current->mm, start);  	if (!vma || vma->vm_start > start)  		return -ENOMEM; +	prev = vma->vm_prev;  	if (start > vma->vm_start)  		prev = vma;  	for (nstart = start ; ; ) { -		unsigned int newflags; +		vm_flags_t newflags;  		/* Here we know that  vma->vm_start <= nstart < vma->vm_end. */ -		newflags = vma->vm_flags | VM_LOCKED; -		if (!on) -			newflags &= ~VM_LOCKED; +		newflags = vma->vm_flags & ~VM_LOCKED; +		if (on) +			newflags |= VM_LOCKED;  		tmp = vma->vm_end;  		if (tmp > end) @@ -482,6 +653,69 @@ static int do_mlock(unsigned long start, size_t len, int on)  	return error;  } +/* + * __mm_populate - populate and/or mlock pages within a range of address space. + * + * This is used to implement mlock() and the MAP_POPULATE / MAP_LOCKED mmap + * flags. VMAs must be already marked with the desired vm_flags, and + * mmap_sem must not be held. + */ +int __mm_populate(unsigned long start, unsigned long len, int ignore_errors) +{ +	struct mm_struct *mm = current->mm; +	unsigned long end, nstart, nend; +	struct vm_area_struct *vma = NULL; +	int locked = 0; +	long ret = 0; + +	VM_BUG_ON(start & ~PAGE_MASK); +	VM_BUG_ON(len != PAGE_ALIGN(len)); +	end = start + len; + +	for (nstart = start; nstart < end; nstart = nend) { +		/* +		 * We want to fault in pages for [nstart; end) address range. +		 * Find first corresponding VMA. +		 */ +		if (!locked) { +			locked = 1; +			down_read(&mm->mmap_sem); +			vma = find_vma(mm, nstart); +		} else if (nstart >= vma->vm_end) +			vma = vma->vm_next; +		if (!vma || vma->vm_start >= end) +			break; +		/* +		 * Set [nstart; nend) to intersection of desired address +		 * range with the first VMA. Also, skip undesirable VMA types. +		 */ +		nend = min(end, vma->vm_end); +		if (vma->vm_flags & (VM_IO | VM_PFNMAP)) +			continue; +		if (nstart < vma->vm_start) +			nstart = vma->vm_start; +		/* +		 * Now fault in a range of pages. __mlock_vma_pages_range() +		 * double checks the vma flags, so that it won't mlock pages +		 * if the vma was already munlocked. +		 */ +		ret = __mlock_vma_pages_range(vma, nstart, nend, &locked); +		if (ret < 0) { +			if (ignore_errors) { +				ret = 0; +				continue;	/* continue at next VMA */ +			} +			ret = __mlock_posix_error_return(ret); +			break; +		} +		nend = nstart + ret * PAGE_SIZE; +		ret = 0; +	} +	if (locked) +		up_read(&mm->mmap_sem); +	return ret;	/* 0 or negative error code */ +} +  SYSCALL_DEFINE2(mlock, unsigned long, start, size_t, len)  {  	unsigned long locked; @@ -493,20 +727,24 @@ SYSCALL_DEFINE2(mlock, unsigned long, start, size_t, len)  	lru_add_drain_all();	/* flush pagevec */ -	down_write(¤t->mm->mmap_sem);  	len = PAGE_ALIGN(len + (start & ~PAGE_MASK));  	start &= PAGE_MASK; -	locked = len >> PAGE_SHIFT; -	locked += current->mm->locked_vm; -  	lock_limit = rlimit(RLIMIT_MEMLOCK);  	lock_limit >>= PAGE_SHIFT; +	locked = len >> PAGE_SHIFT; + +	down_write(¤t->mm->mmap_sem); + +	locked += current->mm->locked_vm;  	/* check against resource limits */  	if ((locked <= lock_limit) || capable(CAP_IPC_LOCK))  		error = do_mlock(start, len, 1); +  	up_write(¤t->mm->mmap_sem); +	if (!error) +		error = __mm_populate(start, len, 0);  	return error;  } @@ -514,34 +752,37 @@ SYSCALL_DEFINE2(munlock, unsigned long, start, size_t, len)  {  	int ret; -	down_write(¤t->mm->mmap_sem);  	len = PAGE_ALIGN(len + (start & ~PAGE_MASK));  	start &= PAGE_MASK; + +	down_write(¤t->mm->mmap_sem);  	ret = do_mlock(start, len, 0);  	up_write(¤t->mm->mmap_sem); +  	return ret;  }  static int do_mlockall(int flags)  {  	struct vm_area_struct * vma, * prev = NULL; -	unsigned int def_flags = 0;  	if (flags & MCL_FUTURE) -		def_flags = VM_LOCKED; -	current->mm->def_flags = def_flags; +		current->mm->def_flags |= VM_LOCKED; +	else +		current->mm->def_flags &= ~VM_LOCKED;  	if (flags == MCL_FUTURE)  		goto out;  	for (vma = current->mm->mmap; vma ; vma = prev->vm_next) { -		unsigned int newflags; +		vm_flags_t newflags; -		newflags = vma->vm_flags | VM_LOCKED; -		if (!(flags & MCL_CURRENT)) -			newflags &= ~VM_LOCKED; +		newflags = vma->vm_flags & ~VM_LOCKED; +		if (flags & MCL_CURRENT) +			newflags |= VM_LOCKED;  		/* Ignore errors */  		mlock_fixup(vma, &prev, vma->vm_start, vma->vm_end, newflags); +		cond_resched();  	}  out:  	return 0; @@ -559,18 +800,21 @@ SYSCALL_DEFINE1(mlockall, int, flags)  	if (!can_do_mlock())  		goto out; -	lru_add_drain_all();	/* flush pagevec */ - -	down_write(¤t->mm->mmap_sem); +	if (flags & MCL_CURRENT) +		lru_add_drain_all();	/* flush pagevec */  	lock_limit = rlimit(RLIMIT_MEMLOCK);  	lock_limit >>= PAGE_SHIFT;  	ret = -ENOMEM; +	down_write(¤t->mm->mmap_sem); +  	if (!(flags & MCL_CURRENT) || (current->mm->total_vm <= lock_limit) ||  	    capable(CAP_IPC_LOCK))  		ret = do_mlockall(flags);  	up_write(¤t->mm->mmap_sem); +	if (!ret && (flags & MCL_CURRENT)) +		mm_populate(0, TASK_SIZE);  out:  	return ret;  }  | 
