Skip to content

Commit 22a668d

Browse files
hkamezawatorvalds
authored andcommitted
memcg: fix behavior under memory.limit equals to memsw.limit
A user can set memcg.limit_in_bytes == memcg.memsw.limit_in_bytes when the user just want to limit the total size of applications, in other words, not very interested in memory usage itself. In this case, swap-out will be done only by global-LRU. But, under current implementation, memory.limit_in_bytes is checked at first and try_to_free_page() may do swap-out. But, that swap-out is useless for memsw.limit_in_bytes and the thread may hit limit again. This patch tries to fix the current behavior at memory.limit == memsw.limit case. And documentation is updated to explain the behavior of this special case. Signed-off-by: KAMEZAWA Hiroyuki <kamezawa.hiroyu@jp.fujitsu.com> Cc: Daisuke Nishimura <nishimura@mxp.nes.nec.co.jp> Cc: Balbir Singh <balbir@in.ibm.com> Cc: Li Zefan <lizf@cn.fujitsu.com> Cc: Dhaval Giani <dhaval@linux.vnet.ibm.com> Cc: YAMAMOTO Takashi <yamamoto@valinux.co.jp> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
1 parent 8a9478c commit 22a668d

2 files changed

Lines changed: 30 additions & 6 deletions

File tree

‎Documentation/cgroups/memory.txt‎

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -152,14 +152,19 @@ When swap is accounted, following files are added.
152152

153153
usage of mem+swap is limited by memsw.limit_in_bytes.
154154

155-
Note: why 'mem+swap' rather than swap.
155+
* why 'mem+swap' rather than swap.
156156
The global LRU(kswapd) can swap out arbitrary pages. Swap-out means
157157
to move account from memory to swap...there is no change in usage of
158-
mem+swap.
159-
160-
In other words, when we want to limit the usage of swap without affecting
161-
global LRU, mem+swap limit is better than just limiting swap from OS point
162-
of view.
158+
mem+swap. In other words, when we want to limit the usage of swap without
159+
affecting global LRU, mem+swap limit is better than just limiting swap from
160+
OS point of view.
161+
162+
* What happens when a cgroup hits memory.memsw.limit_in_bytes
163+
When a cgroup his memory.memsw.limit_in_bytes, it's useless to do swap-out
164+
in this cgroup. Then, swap-out will not be done by cgroup routine and file
165+
caches are dropped. But as mentioned above, global LRU can do swapout memory
166+
from it for sanity of the system's memory management state. You can't forbid
167+
it by cgroup.
163168

164169
2.5 Reclaim
165170

‎mm/memcontrol.c‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,9 @@ struct mem_cgroup {
177177

178178
unsigned int swappiness;
179179

180+
/* set when res.limit == memsw.limit */
181+
bool memsw_is_minimum;
182+
180183
/*
181184
* statistics. This must be placed at the end of memcg.
182185
*/
@@ -847,6 +850,10 @@ static int mem_cgroup_hierarchical_reclaim(struct mem_cgroup *root_mem,
847850
int ret, total = 0;
848851
int loop = 0;
849852

853+
/* If memsw_is_minimum==1, swap-out is of-no-use. */
854+
if (root_mem->memsw_is_minimum)
855+
noswap = true;
856+
850857
while (loop < 2) {
851858
victim = mem_cgroup_select_victim(root_mem);
852859
if (victim == root_mem)
@@ -1752,6 +1759,12 @@ static int mem_cgroup_resize_limit(struct mem_cgroup *memcg,
17521759
break;
17531760
}
17541761
ret = res_counter_set_limit(&memcg->res, val);
1762+
if (!ret) {
1763+
if (memswlimit == val)
1764+
memcg->memsw_is_minimum = true;
1765+
else
1766+
memcg->memsw_is_minimum = false;
1767+
}
17551768
mutex_unlock(&set_limit_mutex);
17561769

17571770
if (!ret)
@@ -1799,6 +1812,12 @@ static int mem_cgroup_resize_memsw_limit(struct mem_cgroup *memcg,
17991812
break;
18001813
}
18011814
ret = res_counter_set_limit(&memcg->memsw, val);
1815+
if (!ret) {
1816+
if (memlimit == val)
1817+
memcg->memsw_is_minimum = true;
1818+
else
1819+
memcg->memsw_is_minimum = false;
1820+
}
18021821
mutex_unlock(&set_limit_mutex);
18031822

18041823
if (!ret)

0 commit comments

Comments
 (0)