mm: per-process reclaim

These day, there are many platforms available in the embedded market
and they are smarter than kernel which has very limited information
about working set so they want to involve memory management more heavily
like android's lowmemory killer and ashmem or recent many lowmemory
notifier.

One of the simple imagine scenario about userspace's intelligence is that
platform can manage tasks as forground and background so it would be
better to reclaim background's task pages for end-user's *responsibility*
although it has frequent referenced pages.

This patch adds new knob "reclaim under proc/<pid>/" so task manager
can reclaim any target process anytime, anywhere. It could give another
method to platform for using memory efficiently.

It can avoid process killing for getting free memory, which was really
terrible experience because I lost my best score of game I had ever
after I switch the phone call while I enjoyed the game.

Reclaim file-backed pages only.
	echo file > /proc/PID/reclaim
Reclaim anonymous pages only.
	echo anon > /proc/PID/reclaim
Reclaim all pages
	echo all > /proc/PID/reclaim

Bug: 131016077
Bug: 153444106
Test: boot
(cherry picked from 18c2af05a553f17d354b88b3a45dadc114c8c72c)
Change-Id: I99b51544f79202c097214d3856678cac4449a743
Signed-off-by: Tim Murray <timmurray@google.com>
Signed-off-by: Minchan Kim <minchan@kernel.org>
Signed-off-by: Martin Liu <liumartin@google.com>
Signed-off-by: UtsavBalar1231 <utsavbalar1231@gmail.com>
This commit is contained in:
Minchan Kim 2015-01-09 18:36:55 +05:30 committed by spakkkk
parent f4eed96283
commit db83b128b3
6 changed files with 210 additions and 2 deletions

View File

@ -3416,6 +3416,9 @@ static const struct pid_entry tgid_base_stuff[] = {
REG("mounts", S_IRUGO, proc_mounts_operations),
REG("mountinfo", S_IRUGO, proc_mountinfo_operations),
REG("mountstats", S_IRUSR, proc_mountstats_operations),
#ifdef CONFIG_PROCESS_RECLAIM
REG("reclaim", S_IWUGO, proc_reclaim_operations),
#endif
#ifdef CONFIG_PROC_PAGE_MONITOR
REG("clear_refs", S_IWUSR, proc_clear_refs_operations),
REG("smaps", S_IRUGO, proc_pid_smaps_operations),

View File

@ -208,6 +208,7 @@ struct pde_opener {
extern const struct inode_operations proc_link_inode_operations;
extern const struct inode_operations proc_pid_link_inode_operations;
extern const struct file_operations proc_reclaim_operations;
void proc_init_kmemcache(void);
void set_proc_pid_nlink(void);

View File

@ -19,6 +19,7 @@
#include <linux/shmem_fs.h>
#include <linux/uaccess.h>
#include <linux/pkeys.h>
#include <linux/mm_inline.h>
#include <asm/elf.h>
#include <asm/tlb.h>
@ -1662,6 +1663,144 @@ const struct file_operations proc_pagemap_operations = {
};
#endif /* CONFIG_PROC_PAGE_MONITOR */
#ifdef CONFIG_PROCESS_RECLAIM
enum reclaim_type {
RECLAIM_FILE,
RECLAIM_ANON,
RECLAIM_ALL,
};
static int reclaim_pmd_range(pmd_t *pmd, unsigned long addr,
unsigned long end, struct mm_walk *walk)
{
pte_t *orig_pte, *pte, ptent;
spinlock_t *ptl;
LIST_HEAD(page_list);
struct page *page;
int isolated = 0;
struct vm_area_struct *vma = walk->vma;
orig_pte = pte_offset_map_lock(vma->vm_mm, pmd, addr, &ptl);
for (pte = orig_pte; addr < end; pte++, addr += PAGE_SIZE) {
ptent = *pte;
if (!pte_present(ptent))
continue;
page = vm_normal_page(vma, addr, ptent);
if (!page)
continue;
/*
* XXX: we don't handle compound page at this moment but
* it should revisit for THP page before upstream.
*/
if (PageCompound(page)) {
unsigned int order = compound_order(page);
unsigned int nr_pages = (1 << order) - 1;
addr += (nr_pages * PAGE_SIZE);
pte += nr_pages;
continue;
}
if (!PageLRU(page))
continue;
if (page_mapcount(page) > 1)
continue;
if (isolate_lru_page(page))
continue;
isolated++;
list_add(&page->lru, &page_list);
if (isolated >= SWAP_CLUSTER_MAX) {
pte_unmap_unlock(orig_pte, ptl);
reclaim_pages(&page_list);
isolated = 0;
pte = pte_offset_map_lock(vma->vm_mm, pmd, addr, &ptl);
orig_pte = pte;
}
}
pte_unmap_unlock(orig_pte, ptl);
reclaim_pages(&page_list);
cond_resched();
return 0;
}
static ssize_t reclaim_write(struct file *file, const char __user *buf,
size_t count, loff_t *ppos)
{
struct task_struct *task;
char buffer[PROC_NUMBUF];
struct mm_struct *mm;
struct vm_area_struct *vma;
enum reclaim_type type;
char *type_buf;
if (!capable(CAP_SYS_NICE))
return -EPERM;
memset(buffer, 0, sizeof(buffer));
if (count > sizeof(buffer) - 1)
count = sizeof(buffer) - 1;
if (copy_from_user(buffer, buf, count))
return -EFAULT;
type_buf = strstrip(buffer);
if (!strcmp(type_buf, "file"))
type = RECLAIM_FILE;
else if (!strcmp(type_buf, "anon"))
type = RECLAIM_ANON;
else if (!strcmp(type_buf, "all"))
type = RECLAIM_ALL;
else
return -EINVAL;
task = get_proc_task(file->f_path.dentry->d_inode);
if (!task)
return -ESRCH;
mm = get_task_mm(task);
if (mm) {
struct mm_walk reclaim_walk = {
.pmd_entry = reclaim_pmd_range,
.mm = mm,
};
down_read(&mm->mmap_sem);
for (vma = mm->mmap; vma; vma = vma->vm_next) {
if (is_vm_hugetlb_page(vma))
continue;
if (vma->vm_flags & VM_LOCKED)
continue;
if (type == RECLAIM_ANON && !vma_is_anonymous(vma))
continue;
if (type == RECLAIM_FILE && vma_is_anonymous(vma))
continue;
walk_page_range(vma->vm_start, vma->vm_end,
&reclaim_walk);
}
flush_tlb_mm(mm);
up_read(&mm->mmap_sem);
mmput(mm);
}
put_task_struct(task);
return count;
}
const struct file_operations proc_reclaim_operations = {
.write = reclaim_write,
.llseek = noop_llseek,
};
#endif
#ifdef CONFIG_NUMA
struct numa_maps {

View File

@ -12,6 +12,10 @@
#include <linux/memcontrol.h>
#include <linux/highmem.h>
extern int isolate_lru_page(struct page *page);
extern void putback_lru_page(struct page *page);
extern unsigned long reclaim_pages(struct list_head *page_list);
/*
* The anon_vma heads a list of private "related" vmas, to scan if
* an anonymous page pointing to this anon_vma needs to be unmapped:

View File

@ -263,6 +263,18 @@ config COMPACTION
it and then we would be really interested to hear about that at
linux-mm@kvack.org.
config PROCESS_RECLAIM
bool "Enable process reclaim"
depends on PROC_FS
default n
help
It allows to reclaim pages of the process by /proc/pid/reclaim.
(echo file > /proc/PID/reclaim) reclaims file-backed pages only.
(echo anon > /proc/PID/reclaim) reclaims anonymous pages only.
(echo all > /proc/PID/reclaim) reclaims all pages.
Any other value is ignored.
#
# support for page migration
#

View File

@ -1101,7 +1101,7 @@ static unsigned long shrink_page_list(struct list_head *page_list,
struct scan_control *sc,
enum ttu_flags ttu_flags,
struct reclaim_stat *stat,
bool force_reclaim)
bool skip_reference_check)
{
LIST_HEAD(ret_pages);
LIST_HEAD(free_pages);
@ -1253,7 +1253,7 @@ static unsigned long shrink_page_list(struct list_head *page_list,
}
}
if (!force_reclaim)
if (!skip_reference_check)
references = page_check_references(page, sc);
switch (references) {
@ -1532,6 +1532,55 @@ unsigned long reclaim_clean_pages_from_list(struct zone *zone,
return ret;
}
#ifdef CONFIG_PROCESS_RECLAIM
unsigned long reclaim_pages(struct list_head *page_list)
{
unsigned long nr_reclaimed;
struct page *page;
unsigned long nr_isolated[2] = {0, };
struct pglist_data *pgdat = NULL;
struct scan_control sc = {
.gfp_mask = GFP_KERNEL,
.priority = DEF_PRIORITY,
.may_writepage = 1,
.may_unmap = 1,
.may_swap = 1,
};
if (list_empty(page_list))
return 0;
list_for_each_entry(page, page_list, lru) {
ClearPageActive(page);
if (pgdat == NULL)
pgdat = page_pgdat(page);
/* XXX: It could be multiple node in other config */
WARN_ON_ONCE(pgdat != page_pgdat(page));
if (!page_is_file_cache(page))
nr_isolated[0]++;
else
nr_isolated[1]++;
}
mod_node_page_state(pgdat, NR_ISOLATED_ANON, nr_isolated[0]);
mod_node_page_state(pgdat, NR_ISOLATED_FILE, nr_isolated[1]);
nr_reclaimed = shrink_page_list(page_list, pgdat, &sc,
TTU_IGNORE_ACCESS, NULL, true);
while (!list_empty(page_list)) {
page = lru_to_page(page_list);
list_del(&page->lru);
putback_lru_page(page);
}
mod_node_page_state(pgdat, NR_ISOLATED_ANON, -nr_isolated[0]);
mod_node_page_state(pgdat, NR_ISOLATED_FILE, -nr_isolated[1]);
return nr_reclaimed;
}
#endif
/*
* Attempt to remove the specified page from its LRU. Only take this page
* if it is of the appropriate PageActive status. Pages which are being