Skip to content

Commit adbe427

Browse files
Vladimir Davydovtorvalds
authored andcommitted
memcg: zap mem_cgroup_lookup()
mem_cgroup_lookup() is a wrapper around mem_cgroup_from_id(), which checks that id != 0 before issuing the function call. Today, there is no point in this additional check apart from optimization, because there is no css with id <= 0, so that css_from_id, called by mem_cgroup_from_id, will return NULL for any id <= 0. Since mem_cgroup_from_id is only called from mem_cgroup_lookup, let us zap mem_cgroup_lookup, substituting calls to it with mem_cgroup_from_id and moving the check if id > 0 to css_from_id. Signed-off-by: Vladimir Davydov <[email protected]> Acked-by: Michal Hocko <[email protected]> Cc: Johannes Weiner <[email protected]> Signed-off-by: Andrew Morton <[email protected]> Signed-off-by: Linus Torvalds <[email protected]>
1 parent d7e4a2e commit adbe427

File tree

2 files changed

+9
-17
lines changed

2 files changed

+9
-17
lines changed

kernel/cgroup.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5451,7 +5451,7 @@ struct cgroup_subsys_state *css_tryget_online_from_dir(struct dentry *dentry,
54515451
struct cgroup_subsys_state *css_from_id(int id, struct cgroup_subsys *ss)
54525452
{
54535453
WARN_ON_ONCE(!rcu_read_lock_held());
5454-
return idr_find(&ss->css_idr, id);
5454+
return id > 0 ? idr_find(&ss->css_idr, id) : NULL;
54555455
}
54565456

54575457
#ifdef CONFIG_CGROUP_DEBUG

mm/memcontrol.c

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -460,6 +460,12 @@ static inline unsigned short mem_cgroup_id(struct mem_cgroup *memcg)
460460
return memcg->css.id;
461461
}
462462

463+
/*
464+
* A helper function to get mem_cgroup from ID. must be called under
465+
* rcu_read_lock(). The caller is responsible for calling
466+
* css_tryget_online() if the mem_cgroup is used for charging. (dropping
467+
* refcnt from swap can be called against removed memcg.)
468+
*/
463469
static inline struct mem_cgroup *mem_cgroup_from_id(unsigned short id)
464470
{
465471
struct cgroup_subsys_state *css;
@@ -2348,20 +2354,6 @@ static void cancel_charge(struct mem_cgroup *memcg, unsigned int nr_pages)
23482354
css_put_many(&memcg->css, nr_pages);
23492355
}
23502356

2351-
/*
2352-
* A helper function to get mem_cgroup from ID. must be called under
2353-
* rcu_read_lock(). The caller is responsible for calling
2354-
* css_tryget_online() if the mem_cgroup is used for charging. (dropping
2355-
* refcnt from swap can be called against removed memcg.)
2356-
*/
2357-
static struct mem_cgroup *mem_cgroup_lookup(unsigned short id)
2358-
{
2359-
/* ID 0 is unused ID */
2360-
if (!id)
2361-
return NULL;
2362-
return mem_cgroup_from_id(id);
2363-
}
2364-
23652357
/*
23662358
* try_get_mem_cgroup_from_page - look up page's memcg association
23672359
* @page: the page
@@ -2388,7 +2380,7 @@ struct mem_cgroup *try_get_mem_cgroup_from_page(struct page *page)
23882380
ent.val = page_private(page);
23892381
id = lookup_swap_cgroup_id(ent);
23902382
rcu_read_lock();
2391-
memcg = mem_cgroup_lookup(id);
2383+
memcg = mem_cgroup_from_id(id);
23922384
if (memcg && !css_tryget_online(&memcg->css))
23932385
memcg = NULL;
23942386
rcu_read_unlock();
@@ -5869,7 +5861,7 @@ void mem_cgroup_uncharge_swap(swp_entry_t entry)
58695861

58705862
id = swap_cgroup_record(entry, 0);
58715863
rcu_read_lock();
5872-
memcg = mem_cgroup_lookup(id);
5864+
memcg = mem_cgroup_from_id(id);
58735865
if (memcg) {
58745866
if (!mem_cgroup_is_root(memcg))
58755867
page_counter_uncharge(&memcg->memsw, 1);

0 commit comments

Comments
 (0)