Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

mm: fix mlocked page counter mismatch

When I tested following program, I found that the mlocked counter
is strange. It cannot free some mlocked pages.

It is because try_to_unmap_file() doesn't check real
page mappings in vmas.

That is because the goal of an address_space for a file is to find all
processes into which the file's specific interval is mapped. It is
related to the file's interval, not to pages.

Even if the page isn't really mapped by the vma, it returns SWAP_MLOCK
since the vma has VM_LOCKED, then calls try_to_mlock_page. After this the
mlocked counter is increased again.

COWed anon page in a file-backed vma could be a such case. This patch
resolves it.

-- my test program --

int main()
{
mlockall(MCL_CURRENT);
return 0;
}

-- before --

root@barrios-target-linux:~# cat /proc/meminfo | egrep 'Mlo|Unev'
Unevictable: 0 kB
Mlocked: 0 kB

-- after --

root@barrios-target-linux:~# cat /proc/meminfo | egrep 'Mlo|Unev'
Unevictable: 8 kB
Mlocked: 8 kB

Signed-off-by: MinChan Kim <minchan.kim@gmail.com>
Acked-by: Lee Schermerhorn <lee.schermerhorn@hp.com>
Acked-by: KOSAKI Motohiro <kosaki.motohiro@jp.fujitsu.com>
Tested-by: Lee Schermerhorn <lee.schermerhorn@hp.com>
Cc: Hugh Dickins <hugh@veritas.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>

authored by

MinChan Kim and committed by
Linus Torvalds
508b9f8e 02ac597c

+2 -1
+2 -1
mm/rmap.c
··· 1072 1072 spin_lock(&mapping->i_mmap_lock); 1073 1073 vma_prio_tree_foreach(vma, &iter, &mapping->i_mmap, pgoff, pgoff) { 1074 1074 if (MLOCK_PAGES && unlikely(unlock)) { 1075 - if (!(vma->vm_flags & VM_LOCKED)) 1075 + if (!((vma->vm_flags & VM_LOCKED) && 1076 + page_mapped_in_vma(page, vma))) 1076 1077 continue; /* must visit all vmas */ 1077 1078 ret = SWAP_MLOCK; 1078 1079 } else {