Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit fdb14c28 authored by Minchan Kim's avatar Minchan Kim Committed by TARKZiM
Browse files

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: 121158602
Bug: 122047783
Change-Id: I2f629f7a43289af114df27044b1d2af4a6e785bc
Signed-off-by: default avatarTim Murray <timmurray@google.com>
Signed-off-by: default avatarMinchan Kim <minchan@kernel.org>
(cherry picked from commit e48b0dd9cc1fa572455434357f5678018b3458b9)
modification for zone-lru list from 4.9
parent 6a49615d
Loading
Loading
Loading
Loading
+3 −0
Original line number Diff line number Diff line
@@ -2878,6 +2878,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),
+1 −0
Original line number Diff line number Diff line
@@ -205,6 +205,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;

extern void proc_init_inodecache(void);
extern struct inode *proc_get_inode(struct super_block *, struct proc_dir_entry *);
+139 −0
Original line number Diff line number Diff line
@@ -11,6 +11,7 @@
#include <linux/rmap.h>
#include <linux/swap.h>
#include <linux/swapops.h>
#include <linux/mm_inline.h>

#include <asm/elf.h>
#include <asm/uaccess.h>
@@ -1205,6 +1206,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->vm_ops)
				continue;
			if (type == RECLAIM_FILE && !vma->vm_ops)
				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 {
+4 −0
Original line number Diff line number Diff line
@@ -10,6 +10,10 @@
#include <linux/rwsem.h>
#include <linux/memcontrol.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:
+12 −0
Original line number Diff line number Diff line
@@ -239,6 +239,18 @@ config COMPACTION
	help
	  Allows the compaction of memory for the allocation of huge pages.

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
#
Loading