From: Christoph Lameter Opportunistically move mlocked pages off the LRU Add a new function try_to_mlock() that attempts to move a page off the LRU and marks it mlocked. This function can then be used in various code paths to move pages off the LRU immediately. Early discovery will make NR_MLOCK track the actual number of mlocked pages in the system more closely. Signed-off-by: Christoph Lameter Signed-off-by: Andrew Morton --- mm/memory.c | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff -puN mm/memory.c~opportunistically-move-mlocked-pages-off-the-lru mm/memory.c --- a/mm/memory.c~opportunistically-move-mlocked-pages-off-the-lru +++ a/mm/memory.c @@ -59,6 +59,7 @@ #include #include +#include #ifndef CONFIG_NEED_MULTIPLE_NODES /* use the per-pgdat data instead for discontigmem - mbligh */ @@ -920,6 +921,34 @@ static void add_anon_page(struct vm_area } /* + * Opportunistically move the page off the LRU + * if possible. If we do not succeed then the LRU + * scans will take the page off. + */ +static void try_to_set_mlocked(struct page *page) +{ + struct zone *zone; + unsigned long flags; + + if (!PageLRU(page) || PageMlocked(page)) + return; + + zone = page_zone(page); + if (spin_trylock_irqsave(&zone->lru_lock, flags)) { + if (PageLRU(page) && !PageMlocked(page)) { + ClearPageLRU(page); + if (PageActive(page)) + del_page_from_active_list(zone, page); + else + del_page_from_inactive_list(zone, page); + ClearPageActive(page); + SetPageMlocked(page); + __inc_zone_page_state(page, NR_MLOCK); + } + spin_unlock_irqrestore(&zone->lru_lock, flags); + } +} +/* * Do a quick page-table lookup for a single page. */ struct page *follow_page(struct vm_area_struct *vma, unsigned long address, @@ -979,6 +1008,8 @@ struct page *follow_page(struct vm_area_ set_page_dirty(page); mark_page_accessed(page); } + if (vma->vm_flags & VM_LOCKED) + try_to_set_mlocked(page); unlock: pte_unmap_unlock(ptep, ptl); out: @@ -2317,6 +2348,8 @@ retry: else { inc_mm_counter(mm, file_rss); page_add_file_rmap(new_page); + if (vma->vm_flags & VM_LOCKED) + try_to_set_mlocked(new_page); if (write_access) { dirty_page = new_page; get_page(dirty_page); _