From: Waiman Long <longman-H+wXaHxf7aLQT0dZR+AlfA@public.gmane.org>
To: Johannes Weiner <hannes-druUgvl0LCNAfugRpC6u6w@public.gmane.org>,
Michal Hocko <mhocko-DgEjT+Ai2ygdnm+yROfE0A@public.gmane.org>,
Vladimir Davydov
<vdavydov.dev-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>,
Andrew Morton
<akpm-de/tnXTf+JLsfHDXvbKv3WD2FQJk+8+b@public.gmane.org>,
Vlastimil Babka <vbabka-AlSwsSmVLrQ@public.gmane.org>,
Roman Gushchin <guro-b10kYP2dOMg@public.gmane.org>
Cc: linux-kernel-u79uwXL29TY76Z2rM5mHXA@public.gmane.org,
cgroups-u79uwXL29TY76Z2rM5mHXA@public.gmane.org,
linux-mm-Bw31MaZKKs3YtjvyW6yDsg@public.gmane.org,
Shakeel Butt <shakeelb-hpIqsD4AKlfQT0dZR+AlfA@public.gmane.org>,
Muchun Song <songmuchun-EC8Uxl6Npydl57MIdRCFDg@public.gmane.org>,
Waiman Long <longman-H+wXaHxf7aLQT0dZR+AlfA@public.gmane.org>
Subject: [PATCH 3/3] mm, memcg: Ensure valid memcg from objcg within a RCU critical section
Date: Fri, 1 Oct 2021 15:09:38 -0400 [thread overview]
Message-ID: <20211001190938.14050-4-longman@redhat.com> (raw)
In-Reply-To: <20211001190938.14050-1-longman-H+wXaHxf7aLQT0dZR+AlfA@public.gmane.org>
To ensure that a to-be-offlined memcg fetched from objcg remains
valid (has non-zero reference count) within a RCU critical section,
a synchronize_rcu() call is inserted at the end of memcg_offline_kmem().
With that change, we no longer need to use css_tryget()
in get_mem_cgroup_from_objcg() as the final css_put() in
css_killed_work_fn() would not have been called yet.
The obj_cgroup_uncharge_pages() function is simplifed to perform
the whole uncharge operation within a RCU critical section saving a
css_get()/css_put() pair.
Signed-off-by: Waiman Long <longman-H+wXaHxf7aLQT0dZR+AlfA@public.gmane.org>
---
mm/memcontrol.c | 16 +++++++++++-----
1 file changed, 11 insertions(+), 5 deletions(-)
diff --git a/mm/memcontrol.c b/mm/memcontrol.c
index 8177f253a127..1dbb37d96e49 100644
--- a/mm/memcontrol.c
+++ b/mm/memcontrol.c
@@ -2769,10 +2769,8 @@ static struct mem_cgroup *get_mem_cgroup_from_objcg(struct obj_cgroup *objcg)
struct mem_cgroup *memcg;
rcu_read_lock();
-retry:
memcg = obj_cgroup_memcg(objcg);
- if (unlikely(!css_tryget(&memcg->css)))
- goto retry;
+ css_get(&memcg->css);
rcu_read_unlock();
return memcg;
@@ -2947,13 +2945,14 @@ static void obj_cgroup_uncharge_pages(struct obj_cgroup *objcg,
{
struct mem_cgroup *memcg;
- memcg = get_mem_cgroup_from_objcg(objcg);
+ rcu_read_lock();
+ memcg = obj_cgroup_memcg(objcg);
if (!cgroup_subsys_on_dfl(memory_cgrp_subsys))
page_counter_uncharge(&memcg->kmem, nr_pages);
refill_stock(memcg, nr_pages);
- css_put(&memcg->css);
+ rcu_read_unlock();
}
/*
@@ -3672,6 +3671,13 @@ static void memcg_offline_kmem(struct mem_cgroup *memcg)
memcg_drain_all_list_lrus(kmemcg_id, parent);
memcg_free_cache_id(kmemcg_id);
+
+ /*
+ * To ensure that a to-be-offlined memcg fetched from objcg remains
+ * valid within a RCU critical section, we need to wait here until
+ * the a grace period has elapsed.
+ */
+ synchronize_rcu();
}
#else
static int memcg_online_kmem(struct mem_cgroup *memcg)
--
2.18.1
next prev parent reply other threads:[~2021-10-01 19:09 UTC|newest]
Thread overview: 20+ messages / expand[flat|nested] mbox.gz Atom feed top
2021-10-01 19:09 [PATCH 0/3] mm, memcg: Miscellaneous cleanups Waiman Long
[not found] ` <20211001190938.14050-1-longman-H+wXaHxf7aLQT0dZR+AlfA@public.gmane.org>
2021-10-01 19:09 ` [PATCH 1/3] mm, memcg: Don't put offlined memcg into local stock Waiman Long
[not found] ` <20211001190938.14050-2-longman-H+wXaHxf7aLQT0dZR+AlfA@public.gmane.org>
2021-10-01 21:17 ` kernel test robot
2021-10-01 23:06 ` kernel test robot
2021-10-01 23:51 ` Roman Gushchin
[not found] ` <YVefHLo1+6lgw3aB-cx5fftMpWqeCjSd+JxjunQ2O0Ztt9esIQQ4Iyu8u01E@public.gmane.org>
2021-10-02 1:54 ` Waiman Long
2022-01-31 3:55 ` Waiman Long
2022-01-31 17:01 ` Roman Gushchin
2022-01-31 17:09 ` Waiman Long
2022-01-31 17:15 ` Waiman Long
[not found] ` <c950a93d-bb70-9a97-dabe-a1d58d755dad-H+wXaHxf7aLQT0dZR+AlfA@public.gmane.org>
2022-01-31 17:19 ` Roman Gushchin
[not found] ` <YfgaIcSJhqEOnh65-cx5fftMpWqeCjSd+JxjunQ2O0Ztt9esIQQ4Iyu8u01E@public.gmane.org>
2022-01-31 17:25 ` Waiman Long
[not found] ` <9abc9cb8-8734-90b0-c495-8df89c7bb579-H+wXaHxf7aLQT0dZR+AlfA@public.gmane.org>
2022-01-31 18:00 ` Shakeel Butt
[not found] ` <95ba1931-e9c9-45c3-b080-d28f2ad368a7-H+wXaHxf7aLQT0dZR+AlfA@public.gmane.org>
2022-01-31 17:15 ` Roman Gushchin
2021-10-01 19:09 ` [PATCH 2/3] mm, memcg: Remove obsolete memcg_free_kmem() Waiman Long
[not found] ` <20211001190938.14050-3-longman-H+wXaHxf7aLQT0dZR+AlfA@public.gmane.org>
2021-10-02 0:01 ` Roman Gushchin
2021-10-02 2:03 ` Waiman Long
2021-10-01 19:09 ` Waiman Long [this message]
[not found] ` <20211001190938.14050-4-longman-H+wXaHxf7aLQT0dZR+AlfA@public.gmane.org>
2021-10-01 20:24 ` [PATCH 3/3] mm, memcg: Ensure valid memcg from objcg within a RCU critical section Shakeel Butt
2021-10-01 20:34 ` Waiman Long
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20211001190938.14050-4-longman@redhat.com \
--to=longman-h+wxahxf7alqt0dzr+alfa@public.gmane.org \
--cc=akpm-de/tnXTf+JLsfHDXvbKv3WD2FQJk+8+b@public.gmane.org \
--cc=cgroups-u79uwXL29TY76Z2rM5mHXA@public.gmane.org \
--cc=guro-b10kYP2dOMg@public.gmane.org \
--cc=hannes-druUgvl0LCNAfugRpC6u6w@public.gmane.org \
--cc=linux-kernel-u79uwXL29TY76Z2rM5mHXA@public.gmane.org \
--cc=linux-mm-Bw31MaZKKs3YtjvyW6yDsg@public.gmane.org \
--cc=mhocko-DgEjT+Ai2ygdnm+yROfE0A@public.gmane.org \
--cc=shakeelb-hpIqsD4AKlfQT0dZR+AlfA@public.gmane.org \
--cc=songmuchun-EC8Uxl6Npydl57MIdRCFDg@public.gmane.org \
--cc=vbabka-AlSwsSmVLrQ@public.gmane.org \
--cc=vdavydov.dev-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox