Skip to content

Commit 9838354

Browse files
Muchun Songtorvalds
authored andcommitted
mm: memcontrol: simplify the logic of objcg pinning memcg
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(). Link: https://lkml.kernel.org/r/20210417043538.9793-7-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> Cc: 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>
1 parent 7467c39 commit 9838354

File tree

1 file changed

+6
-14
lines changed

1 file changed

+6
-14
lines changed

mm/memcontrol.c

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -261,7 +261,6 @@ static void obj_cgroup_uncharge_pages(struct obj_cgroup *objcg,
261261
static void obj_cgroup_release(struct percpu_ref *ref)
262262
{
263263
struct obj_cgroup *objcg = container_of(ref, struct obj_cgroup, refcnt);
264-
struct mem_cgroup *memcg;
265264
unsigned int nr_bytes;
266265
unsigned int nr_pages;
267266
unsigned long flags;
@@ -291,11 +290,9 @@ static void obj_cgroup_release(struct percpu_ref *ref)
291290
nr_pages = nr_bytes >> PAGE_SHIFT;
292291

293292
spin_lock_irqsave(&css_set_lock, flags);
294-
memcg = obj_cgroup_memcg(objcg);
295293
if (nr_pages)
296294
obj_cgroup_uncharge_pages(objcg, nr_pages);
297295
list_del(&objcg->list);
298-
mem_cgroup_put(memcg);
299296
spin_unlock_irqrestore(&css_set_lock, flags);
300297

301298
percpu_ref_exit(ref);
@@ -330,17 +327,12 @@ static void memcg_reparent_objcgs(struct mem_cgroup *memcg,
330327

331328
spin_lock_irq(&css_set_lock);
332329

333-
/* Move active objcg to the parent's list */
334-
xchg(&objcg->memcg, parent);
335-
css_get(&parent->css);
336-
list_add(&objcg->list, &parent->objcg_list);
337-
338-
/* Move already reparented objcgs to the parent's list */
339-
list_for_each_entry(iter, &memcg->objcg_list, list) {
340-
css_get(&parent->css);
341-
xchg(&iter->memcg, parent);
342-
css_put(&memcg->css);
343-
}
330+
/* 1) Ready to reparent active objcg. */
331+
list_add(&objcg->list, &memcg->objcg_list);
332+
/* 2) Reparent active objcg and already reparented objcgs to parent. */
333+
list_for_each_entry(iter, &memcg->objcg_list, list)
334+
WRITE_ONCE(iter->memcg, parent);
335+
/* 3) Move already reparented objcgs to the parent's list */
344336
list_splice(&memcg->objcg_list, &parent->objcg_list);
345337

346338
spin_unlock_irq(&css_set_lock);

0 commit comments

Comments
 (0)