All of lore.kernel.org
 help / color / mirror / Atom feed
From: Muchun Song <songmuchun@bytedance.com>
To: guro@fb.com, hannes@cmpxchg.org, mhocko@kernel.org,
	akpm@linux-foundation.org, shakeelb@google.com,
	vdavydov.dev@gmail.com
Cc: linux-kernel@vger.kernel.org, linux-mm@kvack.org,
	duanxiongchun@bytedance.com, fam.zheng@bytedance.com,
	Muchun Song <songmuchun@bytedance.com>
Subject: [PATCH 5/7] mm: memcontrol: simplify the logic of objcg pinning memcg
Date: Tue, 13 Apr 2021 14:51:51 +0800	[thread overview]
Message-ID: <20210413065153.63431-6-songmuchun@bytedance.com> (raw)
In-Reply-To: <20210413065153.63431-1-songmuchun@bytedance.com>

The obj_cgroup_release() and memcg_reparent_objcgs() are serialized by
the css_set_lock. We do not need to care about objcg->memcg being
released in the process of obj_cgroup_release(). So there is no need
to pin memcg before releasing objcg. Remove those pinning logic to
simplfy the code.

There are only two places that modifies the objcg->memcg. One is the
initialization to objcg->memcg in the memcg_online_kmem(), another
is objcgs reparenting in the memcg_reparent_objcgs(). It is also
impossible for the two to run in parallel. So xchg() is unnecessary
and it is enough to use WRITE_ONCE().

Signed-off-by: Muchun Song <songmuchun@bytedance.com>
Acked-by: Johannes Weiner <hannes@cmpxchg.org>
---
 mm/memcontrol.c | 20 ++++++--------------
 1 file changed, 6 insertions(+), 14 deletions(-)

diff --git a/mm/memcontrol.c b/mm/memcontrol.c
index 1f807448233e..42d8c0f4ab1d 100644
--- a/mm/memcontrol.c
+++ b/mm/memcontrol.c
@@ -261,7 +261,6 @@ static void obj_cgroup_uncharge_pages(struct obj_cgroup *objcg,
 static void obj_cgroup_release(struct percpu_ref *ref)
 {
 	struct obj_cgroup *objcg = container_of(ref, struct obj_cgroup, refcnt);
-	struct mem_cgroup *memcg;
 	unsigned int nr_bytes;
 	unsigned int nr_pages;
 	unsigned long flags;
@@ -291,11 +290,9 @@ static void obj_cgroup_release(struct percpu_ref *ref)
 	nr_pages = nr_bytes >> PAGE_SHIFT;
 
 	spin_lock_irqsave(&css_set_lock, flags);
-	memcg = obj_cgroup_memcg(objcg);
 	if (nr_pages)
 		obj_cgroup_uncharge_pages(objcg, nr_pages);
 	list_del(&objcg->list);
-	mem_cgroup_put(memcg);
 	spin_unlock_irqrestore(&css_set_lock, flags);
 
 	percpu_ref_exit(ref);
@@ -330,17 +327,12 @@ static void memcg_reparent_objcgs(struct mem_cgroup *memcg,
 
 	spin_lock_irq(&css_set_lock);
 
-	/* Move active objcg to the parent's list */
-	xchg(&objcg->memcg, parent);
-	css_get(&parent->css);
-	list_add(&objcg->list, &parent->objcg_list);
-
-	/* Move already reparented objcgs to the parent's list */
-	list_for_each_entry(iter, &memcg->objcg_list, list) {
-		css_get(&parent->css);
-		xchg(&iter->memcg, parent);
-		css_put(&memcg->css);
-	}
+	/* 1) Ready to reparent active objcg. */
+	list_add(&objcg->list, &memcg->objcg_list);
+	/* 2) Reparent active objcg and already reparented objcgs to parent. */
+	list_for_each_entry(iter, &memcg->objcg_list, list)
+		WRITE_ONCE(iter->memcg, parent);
+	/* 3) Move already reparented objcgs to the parent's list */
 	list_splice(&memcg->objcg_list, &parent->objcg_list);
 
 	spin_unlock_irq(&css_set_lock);
-- 
2.11.0


  parent reply	other threads:[~2021-04-13  6:59 UTC|newest]

Thread overview: 40+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-04-13  6:51 [PATCH 0/7] memcontrol code cleanup and simplification Muchun Song
2021-04-13  6:51 ` [PATCH 1/7] mm: memcontrol: fix page charging in page replacement Muchun Song
2021-04-13 17:45   ` Roman Gushchin
2021-04-14  9:20   ` Michal Hocko
2021-04-13  6:51 ` [PATCH 2/7] mm: memcontrol: bail out early when !mm in get_mem_cgroup_from_mm Muchun Song
2021-04-13 17:47   ` Roman Gushchin
2021-04-14  9:24   ` Michal Hocko
2021-04-14 10:04     ` [External] " Muchun Song
2021-04-14 10:04       ` Muchun Song
2021-04-14 10:15       ` Michal Hocko
2021-04-15  3:13         ` Muchun Song
2021-04-15  3:13           ` Muchun Song
2021-04-15  7:09           ` Michal Hocko
2021-04-13  6:51 ` [PATCH 3/7] mm: memcontrol: remove the pgdata parameter of mem_cgroup_page_lruvec Muchun Song
2021-04-13 13:12   ` Shakeel Butt
2021-04-13 13:12     ` Shakeel Butt
2021-04-13 17:48   ` Roman Gushchin
2021-04-14  9:27   ` Michal Hocko
2021-04-13  6:51 ` [PATCH 4/7] mm: memcontrol: simplify lruvec_holds_page_lru_lock Muchun Song
2021-04-13 13:28   ` Shakeel Butt
2021-04-13 13:28     ` Shakeel Butt
2021-04-13 17:57   ` Roman Gushchin
2021-04-14  9:44   ` Michal Hocko
2021-04-14 10:00     ` [External] " Muchun Song
2021-04-14 10:00       ` Muchun Song
2021-04-14 17:49       ` Johannes Weiner
2021-04-15  7:08         ` Michal Hocko
2021-04-15 10:01         ` Muchun Song
2021-04-15 10:01           ` Muchun Song
2021-04-13  6:51 ` Muchun Song [this message]
2021-04-13 14:16   ` [PATCH 5/7] mm: memcontrol: simplify the logic of objcg pinning memcg Shakeel Butt
2021-04-13 14:16     ` Shakeel Butt
2021-04-13 17:51   ` Roman Gushchin
2021-04-13  6:51 ` [PATCH 6/7] mm: memcontrol: move obj_cgroup_uncharge_pages() out of css_set_lock Muchun Song
2021-04-13 14:18   ` Shakeel Butt
2021-04-13 14:18     ` Shakeel Butt
2021-04-13 17:54   ` Roman Gushchin
2021-04-13 18:03   ` Johannes Weiner
2021-04-13  6:51 ` [PATCH 7/7] mm: vmscan: remove noinline_for_stack Muchun Song
2021-04-14  9:46   ` Michal Hocko

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=20210413065153.63431-6-songmuchun@bytedance.com \
    --to=songmuchun@bytedance.com \
    --cc=akpm@linux-foundation.org \
    --cc=duanxiongchun@bytedance.com \
    --cc=fam.zheng@bytedance.com \
    --cc=guro@fb.com \
    --cc=hannes@cmpxchg.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-mm@kvack.org \
    --cc=mhocko@kernel.org \
    --cc=shakeelb@google.com \
    --cc=vdavydov.dev@gmail.com \
    /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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.