From c4a234c3b4f7becec70936000623c62898e2092b Mon Sep 17 00:00:00 2001 From: Dongwoo Lee Date: Mon, 3 Aug 2020 14:44:43 +0900 Subject: [PATCH] mm: memcontrol: Add force_reclaim to reclaim tasks' memory in memcg These days, platforms tend to manage memory on low memory state like andloid's lowmemory killer. These platforms might want to reclaim memory from background tasks as well as kill victims to guarantee free memory at use space level. This patch provides an interface to reclaim a given memcg. After platform's low memory handler moves tasks that the platform wants to reclaim to a memcg and decides how many pages should be reclaimed, it can reclaim the pages from the tasks by writing the number of pages at memory.force_reclaim. Signed-off-by: Hyunhee Kim Signed-off-by: Kyungmin Park [dwoo08.lee: ported from mailing list https://www.spinics.net/lists/cgroups/msg07874.html] Signed-off-by: Dongwoo Lee [mszyprow: adapted for the changes from e55b9f96860f ("mm: memcontrol: drop dead CONFIG_MEMCG_SWAP config symbol") in Linux v6.1] Signed-off-by: Marek Szyprowski Change-Id: I928bf48e9715ed64135030dea44102d54d5f16f4 --- mm/memcontrol.c | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/mm/memcontrol.c b/mm/memcontrol.c index 8a881ab..52ddfe8 100644 --- a/mm/memcontrol.c +++ b/mm/memcontrol.c @@ -3643,6 +3643,36 @@ static ssize_t mem_cgroup_force_empty_write(struct kernfs_open_file *of, return mem_cgroup_force_empty(memcg) ?: nbytes; } +#if defined(CONFIG_MEMCG) && defined(CONFIG_SWAP) +static int mem_cgroup_force_reclaim(struct cgroup_subsys_state *css, + struct cftype *cft, u64 val) +{ + struct mem_cgroup *memcg = mem_cgroup_from_css(css); + unsigned long nr_to_reclaim = val; + unsigned long total = 0; + int loop; + + for (loop = 0; loop < MEM_CGROUP_MAX_RECLAIM_LOOPS; loop++) { + total += try_to_free_mem_cgroup_pages(memcg, nr_to_reclaim, + GFP_KERNEL, true); + + /* + * If nothing was reclaimed after two attempts, there + * may be no reclaimable pages in this hierarchy. + * If more than nr_to_reclaim pages were already reclaimed, + * finish force reclaim. + */ + if (loop && (!total || total > nr_to_reclaim)) + break; + } + + pr_info("%s: [Mem_reclaim] Loop: %d - Total_reclaimed: %lu - nr_to_reclaim: %lu\n", + __func__, loop, total, nr_to_reclaim); + + return total; +} +#endif + static u64 mem_cgroup_hierarchy_read(struct cgroup_subsys_state *css, struct cftype *cft) { @@ -7752,6 +7782,12 @@ static struct cftype memsw_files[] = { .write = mem_cgroup_reset, .read_u64 = mem_cgroup_read_u64, }, +#if defined(CONFIG_MEMCG) && defined(CONFIG_SWAP) + { + .name = "force_reclaim", + .write_u64 = mem_cgroup_force_reclaim, + }, +#endif { }, /* terminate */ }; -- 2.7.4