mm: memcontrol: bail out early when !mm in get_mem_cgroup_from_mm
authorMuchun Song <songmuchun@bytedance.com>
Tue, 29 Jun 2021 02:37:50 +0000 (19:37 -0700)
committerLinus Torvalds <torvalds@linux-foundation.org>
Tue, 29 Jun 2021 17:53:50 +0000 (10:53 -0700)
When mm is NULL, we do not need to hold rcu lock and call css_tryget for
the root memcg.  And we also do not need to check !mm in every loop of
while.  So bail out early when !mm.

Link: https://lkml.kernel.org/r/20210417043538.9793-3-songmuchun@bytedance.com
Signed-off-by: Muchun Song <songmuchun@bytedance.com>
Acked-by: Johannes Weiner <hannes@cmpxchg.org>
Reviewed-by: Shakeel Butt <shakeelb@google.com>
Acked-by: Roman Gushchin <guro@fb.com>
Acked-by: Michal Hocko <mhocko@suse.com>
Cc: Vladimir Davydov <vdavydov.dev@gmail.com>
Cc: Xiongchun Duan <duanxiongchun@bytedance.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
mm/memcontrol.c

index 239f69e..babbaf4 100644 (file)
@@ -919,20 +919,23 @@ struct mem_cgroup *get_mem_cgroup_from_mm(struct mm_struct *mm)
        if (mem_cgroup_disabled())
                return NULL;
 
+       /*
+        * Page cache insertions can happen without an
+        * actual mm context, e.g. during disk probing
+        * on boot, loopback IO, acct() writes etc.
+        *
+        * No need to css_get on root memcg as the reference
+        * counting is disabled on the root level in the
+        * cgroup core. See CSS_NO_REF.
+        */
+       if (unlikely(!mm))
+               return root_mem_cgroup;
+
        rcu_read_lock();
        do {
-               /*
-                * Page cache insertions can happen without an
-                * actual mm context, e.g. during disk probing
-                * on boot, loopback IO, acct() writes etc.
-                */
-               if (unlikely(!mm))
+               memcg = mem_cgroup_from_task(rcu_dereference(mm->owner));
+               if (unlikely(!memcg))
                        memcg = root_mem_cgroup;
-               else {
-                       memcg = mem_cgroup_from_task(rcu_dereference(mm->owner));
-                       if (unlikely(!memcg))
-                               memcg = root_mem_cgroup;
-               }
        } while (!css_tryget(&memcg->css));
        rcu_read_unlock();
        return memcg;