Commit b2145145 authored by Wanpeng Li's avatar Wanpeng Li Committed by Linus Torvalds

memcg: add mem_cgroup_from_css() helper

Add a mem_cgroup_from_css() helper to replace open-coded invokations of
container_of().  To clarify the code and to add a little more type safety.

[akpm@linux-foundation.org: fix extensive breakage]
Signed-off-by: default avatarWanpeng Li <liwanp@linux.vnet.ibm.com>
Acked-by: default avatarMichal Hocko <mhocko@suse.cz>
Cc: Johannes Weiner <hannes@cmpxchg.org>
Cc: KAMEZAWA Hiroyuki <kamezawa.hiroyu@jp.fujitsu.com>
Cc: Gavin Shan <shangw@linux.vnet.ibm.com>
Cc: Wanpeng Li <liwanp@linux.vnet.ibm.com>
Cc: Gavin Shan <shangw@linux.vnet.ibm.com>
Cc: Johannes Weiner <hannes@cmpxchg.org>
Cc: KAMEZAWA Hiroyuki <kamezawa.hiroyu@jp.fujitsu.com>
Cc: Michal Hocko <mhocko@suse.cz>
Signed-off-by: default avatarAndrew Morton <akpm@linux-foundation.org>
Signed-off-by: default avatarLinus Torvalds <torvalds@linux-foundation.org>
parent c3b94f44
...@@ -405,6 +405,12 @@ enum charge_type { ...@@ -405,6 +405,12 @@ enum charge_type {
static void mem_cgroup_get(struct mem_cgroup *memcg); static void mem_cgroup_get(struct mem_cgroup *memcg);
static void mem_cgroup_put(struct mem_cgroup *memcg); static void mem_cgroup_put(struct mem_cgroup *memcg);
static inline
struct mem_cgroup *mem_cgroup_from_css(struct cgroup_subsys_state *s)
{
return container_of(s, struct mem_cgroup, css);
}
/* Writing them here to avoid exposing memcg's inner layout */ /* Writing them here to avoid exposing memcg's inner layout */
#ifdef CONFIG_MEMCG_KMEM #ifdef CONFIG_MEMCG_KMEM
#include <net/sock.h> #include <net/sock.h>
...@@ -862,9 +868,8 @@ static void memcg_check_events(struct mem_cgroup *memcg, struct page *page) ...@@ -862,9 +868,8 @@ static void memcg_check_events(struct mem_cgroup *memcg, struct page *page)
struct mem_cgroup *mem_cgroup_from_cont(struct cgroup *cont) struct mem_cgroup *mem_cgroup_from_cont(struct cgroup *cont)
{ {
return container_of(cgroup_subsys_state(cont, return mem_cgroup_from_css(
mem_cgroup_subsys_id), struct mem_cgroup, cgroup_subsys_state(cont, mem_cgroup_subsys_id));
css);
} }
struct mem_cgroup *mem_cgroup_from_task(struct task_struct *p) struct mem_cgroup *mem_cgroup_from_task(struct task_struct *p)
...@@ -877,8 +882,7 @@ struct mem_cgroup *mem_cgroup_from_task(struct task_struct *p) ...@@ -877,8 +882,7 @@ struct mem_cgroup *mem_cgroup_from_task(struct task_struct *p)
if (unlikely(!p)) if (unlikely(!p))
return NULL; return NULL;
return container_of(task_subsys_state(p, mem_cgroup_subsys_id), return mem_cgroup_from_css(task_subsys_state(p, mem_cgroup_subsys_id));
struct mem_cgroup, css);
} }
struct mem_cgroup *try_get_mem_cgroup_from_mm(struct mm_struct *mm) struct mem_cgroup *try_get_mem_cgroup_from_mm(struct mm_struct *mm)
...@@ -964,8 +968,7 @@ struct mem_cgroup *mem_cgroup_iter(struct mem_cgroup *root, ...@@ -964,8 +968,7 @@ struct mem_cgroup *mem_cgroup_iter(struct mem_cgroup *root,
css = css_get_next(&mem_cgroup_subsys, id + 1, &root->css, &id); css = css_get_next(&mem_cgroup_subsys, id + 1, &root->css, &id);
if (css) { if (css) {
if (css == &root->css || css_tryget(css)) if (css == &root->css || css_tryget(css))
memcg = container_of(css, memcg = mem_cgroup_from_css(css);
struct mem_cgroup, css);
} else } else
id = 0; id = 0;
rcu_read_unlock(); rcu_read_unlock();
...@@ -2494,7 +2497,7 @@ static struct mem_cgroup *mem_cgroup_lookup(unsigned short id) ...@@ -2494,7 +2497,7 @@ static struct mem_cgroup *mem_cgroup_lookup(unsigned short id)
css = css_lookup(&mem_cgroup_subsys, id); css = css_lookup(&mem_cgroup_subsys, id);
if (!css) if (!css)
return NULL; return NULL;
return container_of(css, struct mem_cgroup, css); return mem_cgroup_from_css(css);
} }
struct mem_cgroup *try_get_mem_cgroup_from_page(struct page *page) struct mem_cgroup *try_get_mem_cgroup_from_page(struct page *page)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment