Commit 3e92041d68b40c47faa34c7dc08fc650a6c36adc
Committed by
Linus Torvalds
1 parent
d38144b7a5
Exists in
master
and in
38 other branches
memcg: add mem_cgroup_same_or_subtree() helper
We are checking whether a given two groups are same or at least in the same subtree of a hierarchy at several places. Let's make a helper for it to make code easier to read. Signed-off-by: Michal Hocko <mhocko@suse.cz> Acked-by: KAMEZAWA Hiroyuki <kamezawa.hiroyu@jp.fujitsu.com> Cc: Balbir Singh <bsingharora@gmail.com> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
Showing 1 changed file with 26 additions and 25 deletions Side-by-side Diff
mm/memcontrol.c
... | ... | @@ -1108,6 +1108,21 @@ |
1108 | 1108 | mem_cgroup_add_lru_list(page, to); |
1109 | 1109 | } |
1110 | 1110 | |
1111 | +/* | |
1112 | + * Checks whether given mem is same or in the root_mem's | |
1113 | + * hierarchy subtree | |
1114 | + */ | |
1115 | +static bool mem_cgroup_same_or_subtree(const struct mem_cgroup *root_mem, | |
1116 | + struct mem_cgroup *mem) | |
1117 | +{ | |
1118 | + if (root_mem != mem) { | |
1119 | + return (root_mem->use_hierarchy && | |
1120 | + css_is_ancestor(&mem->css, &root_mem->css)); | |
1121 | + } | |
1122 | + | |
1123 | + return true; | |
1124 | +} | |
1125 | + | |
1111 | 1126 | int task_in_mem_cgroup(struct task_struct *task, const struct mem_cgroup *mem) |
1112 | 1127 | { |
1113 | 1128 | int ret; |
... | ... | @@ -1127,10 +1142,7 @@ |
1127 | 1142 | * enabled in "curr" and "curr" is a child of "mem" in *cgroup* |
1128 | 1143 | * hierarchy(even if use_hierarchy is disabled in "mem"). |
1129 | 1144 | */ |
1130 | - if (mem->use_hierarchy) | |
1131 | - ret = css_is_ancestor(&curr->css, &mem->css); | |
1132 | - else | |
1133 | - ret = (curr == mem); | |
1145 | + ret = mem_cgroup_same_or_subtree(mem, curr); | |
1134 | 1146 | css_put(&curr->css); |
1135 | 1147 | return ret; |
1136 | 1148 | } |
... | ... | @@ -1369,10 +1381,9 @@ |
1369 | 1381 | to = mc.to; |
1370 | 1382 | if (!from) |
1371 | 1383 | goto unlock; |
1372 | - if (from == mem || to == mem | |
1373 | - || (mem->use_hierarchy && css_is_ancestor(&from->css, &mem->css)) | |
1374 | - || (mem->use_hierarchy && css_is_ancestor(&to->css, &mem->css))) | |
1375 | - ret = true; | |
1384 | + | |
1385 | + ret = mem_cgroup_same_or_subtree(mem, from) | |
1386 | + || mem_cgroup_same_or_subtree(mem, to); | |
1376 | 1387 | unlock: |
1377 | 1388 | spin_unlock(&mc.lock); |
1378 | 1389 | return ret; |
1379 | 1390 | |
1380 | 1391 | |
1381 | 1392 | |
1382 | 1393 | |
... | ... | @@ -1915,25 +1926,20 @@ |
1915 | 1926 | static int memcg_oom_wake_function(wait_queue_t *wait, |
1916 | 1927 | unsigned mode, int sync, void *arg) |
1917 | 1928 | { |
1918 | - struct mem_cgroup *wake_mem = (struct mem_cgroup *)arg; | |
1929 | + struct mem_cgroup *wake_mem = (struct mem_cgroup *)arg, | |
1930 | + *oom_wait_mem; | |
1919 | 1931 | struct oom_wait_info *oom_wait_info; |
1920 | 1932 | |
1921 | 1933 | oom_wait_info = container_of(wait, struct oom_wait_info, wait); |
1934 | + oom_wait_mem = oom_wait_info->mem; | |
1922 | 1935 | |
1923 | - if (oom_wait_info->mem == wake_mem) | |
1924 | - goto wakeup; | |
1925 | - /* if no hierarchy, no match */ | |
1926 | - if (!oom_wait_info->mem->use_hierarchy || !wake_mem->use_hierarchy) | |
1927 | - return 0; | |
1928 | 1936 | /* |
1929 | 1937 | * Both of oom_wait_info->mem and wake_mem are stable under us. |
1930 | 1938 | * Then we can use css_is_ancestor without taking care of RCU. |
1931 | 1939 | */ |
1932 | - if (!css_is_ancestor(&oom_wait_info->mem->css, &wake_mem->css) && | |
1933 | - !css_is_ancestor(&wake_mem->css, &oom_wait_info->mem->css)) | |
1940 | + if (!mem_cgroup_same_or_subtree(oom_wait_mem, wake_mem) | |
1941 | + && !mem_cgroup_same_or_subtree(wake_mem, oom_wait_mem)) | |
1934 | 1942 | return 0; |
1935 | - | |
1936 | -wakeup: | |
1937 | 1943 | return autoremove_wake_function(wait, mode, sync, arg); |
1938 | 1944 | } |
1939 | 1945 | |
... | ... | @@ -2178,13 +2184,8 @@ |
2178 | 2184 | mem = stock->cached; |
2179 | 2185 | if (!mem || !stock->nr_pages) |
2180 | 2186 | continue; |
2181 | - if (mem != root_mem) { | |
2182 | - if (!root_mem->use_hierarchy) | |
2183 | - continue; | |
2184 | - /* check whether "mem" is under tree of "root_mem" */ | |
2185 | - if (!css_is_ancestor(&mem->css, &root_mem->css)) | |
2186 | - continue; | |
2187 | - } | |
2187 | + if (!mem_cgroup_same_or_subtree(root_mem, mem)) | |
2188 | + continue; | |
2188 | 2189 | if (!test_and_set_bit(FLUSHING_CACHED_CHARGE, &stock->flags)) { |
2189 | 2190 | if (cpu == curcpu) |
2190 | 2191 | drain_local_stock(&stock->work); |