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.

Signed-off-by: Muchun Song <songmuc...@bytedance.com>
Acked-by: Johannes Weiner <han...@cmpxchg.org>
Reviewed-by: Shakeel Butt <shake...@google.com>
Acked-by: Roman Gushchin <g...@fb.com>
Acked-by: Michal Hocko <mho...@suse.com>
---
 mm/memcontrol.c | 25 ++++++++++++++-----------
 1 file changed, 14 insertions(+), 11 deletions(-)

diff --git a/mm/memcontrol.c b/mm/memcontrol.c
index f229de925aa5..50e3cf1e263e 100644
--- a/mm/memcontrol.c
+++ b/mm/memcontrol.c
@@ -901,20 +901,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;
-- 
2.11.0

Reply via email to