memcontrol code cleanup and simplification
Subsystems (5)
v2 cover memcontrol code cleanup and simplification
This patch series is part of [1] patch series. Because those patches are code cleanup or simplification. I gather those patches into a separate series to make it easier to review.
[1] https://lore.kernel.org/linux-mm/[email protected]/
Changlogs in v2:
1. Collect Acked-by and Review-by tags.
2. Add a new patch to rename lruvec_holds_page_lru_lock to page_matches_lruvec.
3. Add a comment to patch 2.
Thanks to Roman, Johannes, Shakeel and Michal's review.
Muchun Song (8):
mm: memcontrol: fix page charging in page replacement
mm: memcontrol: bail out early when !mm in get_mem_cgroup_from_mm
mm: memcontrol: remove the pgdata parameter of mem_cgroup_page_lruvec
mm: memcontrol: simplify lruvec_holds_page_lru_lock
mm: memcontrol: rename lruvec_holds_page_lru_lock to
page_matches_lruvec
mm: memcontrol: simplify the logic of objcg pinning memcg
mm: memcontrol: move obj_cgroup_uncharge_pages() out of css_set_lock
mm: vmscan: remove noinline_for_stack
include/linux/memcontrol.h | 42 +++++++++--------------------- mm/compaction.c | 2 +- mm/memcontrol.c | 65 +++++++++++++++++++++------------------------- mm/swap.c | 2 +- mm/vmscan.c | 8 +++--- mm/workingset.c | 2 +- 6 files changed, 49 insertions(+), 72 deletions(-)
Signature · 1 line
2.11.0
PATCH v2 1/8 mm: memcontrol: fix page charging in page replacement
The pages aren't accounted at the root level, so do not charge the page to the root memcg in page replacement. Although we do not display the value (mem_cgroup_usage) so there shouldn't be any actual problem, but there is a WARN_ON_ONCE in the page_counter_cancel(). Who knows if it will trigger? So it is better to fix it.
--- mm/memcontrol.c | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-)
mm/memcontrol.c +5 -3
@@ -6806,9 +6806,11 @@ void mem_cgroup_migrate(struct page *oldpage, struct page *newpage)/* Force-charge the new page. The old one will be freed soon */ nr_pages =thp_nr_pages (newpage); -page_counter_charge (&memcg->memory, nr_pages); -if (do_memsw_account ()) -page_counter_charge (&memcg->memsw, nr_pages); +if (!mem_cgroup_is_root (memcg)) { +page_counter_charge (&memcg->memory, nr_pages); +if (do_memsw_account ()) +page_counter_charge (&memcg->memsw, nr_pages); + }css_get (&memcg->css);commit_charge (newpage, memcg);
Signature · 1 line
2.11.0
PATCH v2 2/8 mm: memcontrol: bail out early when !mm in get_mem_cgroup_from_mm
In reply to Muchun SongWhen 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.
--- mm/memcontrol.c | 25 ++++++++++++++----------- 1 file changed, 14 insertions(+), 11 deletions(-)
mm/memcontrol.c +14 -11
@@ -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;
Signature · 1 line
2.11.0
PATCH v2 3/8 mm: memcontrol: remove the pgdata parameter of mem_cgroup_page_lruvec
In reply to Muchun SongAll the callers of mem_cgroup_page_lruvec() just pass page_pgdat(page) as the 2nd parameter to it (except isolate_migratepages_block()). But for isolate_migratepages_block(), the page_pgdat(page) is also equal to the local variable of @pgdat. So mem_cgroup_page_lruvec() do not need the pgdat parameter. Just remove it to simplify the code.
--- include/linux/memcontrol.h | 10 +++++----- mm/compaction.c | 2 +- mm/memcontrol.c | 9 +++------ mm/swap.c | 2 +- mm/workingset.c | 2 +- 5 files changed, 11 insertions(+), 14 deletions(-)
include/linux/memcontrol.h +5 -5
@@ -743,13 +743,12 @@ static inline struct lruvec *mem_cgroup_lruvec(struct mem_cgroup *memcg,/** * mem_cgroup_page_lruvec - return lruvec for isolating/putting an LRU page * @page: the page -* @pgdat: pgdat of the page * * This function relies on page->mem_cgroup being stable. */ -static inline struct lruvec *mem_cgroup_page_lruvec (struct page *page, -struct pglist_data *pgdat) +static inline struct lruvec *mem_cgroup_page_lruvec (struct page *page) { +pg_data_t *pgdat =page_pgdat (page);struct mem_cgroup *memcg =page_memcg (page);VM_WARN_ON_ONCE_PAGE (!memcg && !mem_cgroup_disabled (), page);
@@ -1221,9 +1220,10 @@ static inline struct lruvec *mem_cgroup_lruvec(struct mem_cgroup *memcg,return &pgdat->__lruvec; } -static inline struct lruvec *mem_cgroup_page_lruvec (struct page *page, -struct pglist_data *pgdat) +static inline struct lruvec *mem_cgroup_page_lruvec (struct page *page) { +pg_data_t *pgdat =page_pgdat (page); +return &pgdat->__lruvec; }
mm/compaction.c +1 -1
@@ -994,7 +994,7 @@ isolate_migratepages_block(struct compact_control *cc, unsigned long low_pfn,if (!TestClearPageLRU (page)) goto isolate_fail_put; - lruvec =mem_cgroup_page_lruvec (page, pgdat); + lruvec =mem_cgroup_page_lruvec (page);/* If we already hold the lock, we can skip some rechecking */ if (lruvec != locked) {
mm/memcontrol.c +3 -6
@@ -1181,9 +1181,8 @@ void lruvec_memcg_debug(struct lruvec *lruvec, struct page *page)struct lruvec *lock_page_lruvec (struct page *page) {struct lruvec *lruvec; -struct pglist_data *pgdat =page_pgdat (page); - lruvec =mem_cgroup_page_lruvec (page, pgdat); + lruvec =mem_cgroup_page_lruvec (page);spin_lock (&lruvec->lru_lock);lruvec_memcg_debug (lruvec, page);
@@ -1194,9 +1193,8 @@ struct lruvec *lock_page_lruvec(struct page *page)struct lruvec *lock_page_lruvec_irq (struct page *page) {struct lruvec *lruvec; -struct pglist_data *pgdat =page_pgdat (page); - lruvec =mem_cgroup_page_lruvec (page, pgdat); + lruvec =mem_cgroup_page_lruvec (page);spin_lock_irq (&lruvec->lru_lock);lruvec_memcg_debug (lruvec, page);
@@ -1207,9 +1205,8 @@ struct lruvec *lock_page_lruvec_irq(struct page *page)struct lruvec *lock_page_lruvec_irqsave (struct page *page,unsigned long *flags) {struct lruvec *lruvec; -struct pglist_data *pgdat =page_pgdat (page); - lruvec =mem_cgroup_page_lruvec (page, pgdat); + lruvec =mem_cgroup_page_lruvec (page);spin_lock_irqsave (&lruvec->lru_lock, *flags);lruvec_memcg_debug (lruvec, page);
mm/swap.c +1 -1
@@ -313,7 +313,7 @@ void lru_note_cost(struct lruvec *lruvec, bool file, unsigned int nr_pages)void lru_note_cost_page (struct page *page) { -lru_note_cost (mem_cgroup_page_lruvec (page,page_pgdat (page)), +lru_note_cost (mem_cgroup_page_lruvec (page),page_is_file_lru (page),thp_nr_pages (page)); }
mm/workingset.c +1 -1
@@ -408,7 +408,7 @@ void workingset_activation(struct page *page) memcg =page_memcg_rcu (page);if (!mem_cgroup_disabled () && !memcg) goto out; - lruvec =mem_cgroup_page_lruvec (page,page_pgdat (page)); + lruvec =mem_cgroup_page_lruvec (page);workingset_age_nonresident (lruvec,thp_nr_pages (page)); out:rcu_read_unlock ();
Signature · 1 line
2.11.0
PATCH v2 4/8 mm: memcontrol: simplify lruvec_holds_page_lru_lock
In reply to Muchun SongWe already have a helper lruvec_memcg() to get the memcg from lruvec, we do not need to do it ourselves in the lruvec_holds_page_lru_lock(). So use lruvec_memcg() instead. And if mem_cgroup_disabled() returns false, the page_memcg(page) (the LRU pages) cannot be NULL. So remove the odd logic of "memcg = page_memcg(page) ? : root_mem_cgroup". And use lruvec_pgdat to simplify the code. We can have a single definition for this function that works for !CONFIG_MEMCG, CONFIG_MEMCG + mem_cgroup_disabled() and CONFIG_MEMCG.
--- include/linux/memcontrol.h | 31 +++++++------------------------ 1 file changed, 7 insertions(+), 24 deletions(-)
include/linux/memcontrol.h +7 -24
@@ -755,22 +755,6 @@ static inline struct lruvec *mem_cgroup_page_lruvec(struct page *page)return mem_cgroup_lruvec (memcg, pgdat); } -static inline bool lruvec_holds_page_lru_lock (struct page *page, -struct lruvec *lruvec) -{ -pg_data_t *pgdat =page_pgdat (page); -const struct mem_cgroup *memcg; -struct mem_cgroup_per_node *mz; - -if (mem_cgroup_disabled ()) -return lruvec == &pgdat->__lruvec; - - mz = container_of(lruvec, struct mem_cgroup_per_node, lruvec); - memcg =page_memcg (page) ? : root_mem_cgroup; - -return lruvec->pgdat == pgdat && mz->memcg == memcg; -} -struct mem_cgroup *mem_cgroup_from_task (struct task_struct *p);struct mem_cgroup *get_mem_cgroup_from_mm (struct mm_struct *mm);
@@ -1227,14 +1211,6 @@ static inline struct lruvec *mem_cgroup_page_lruvec(struct page *page)return &pgdat->__lruvec; } -static inline bool lruvec_holds_page_lru_lock (struct page *page, -struct lruvec *lruvec) -{ -pg_data_t *pgdat =page_pgdat (page); - -return lruvec == &pgdat->__lruvec; -} -static inline void lruvec_memcg_debug (struct lruvec *lruvec,struct page *page) { }
@@ -1516,6 +1492,13 @@ static inline void unlock_page_lruvec_irqrestore(struct lruvec *lruvec,spin_unlock_irqrestore (&lruvec->lru_lock, flags); } +static inline bool lruvec_holds_page_lru_lock (struct page *page, +struct lruvec *lruvec) +{ +return lruvec_pgdat (lruvec) ==page_pgdat (page) && +lruvec_memcg (lruvec) ==page_memcg (page); +} +/* Don't lock again iff page's lruvec locked */ static inline struct lruvec *relock_page_lruvec_irq (struct page *page,struct lruvec *locked_lruvec)
Signature · 1 line
2.11.0
PATCH v2 5/8 mm: memcontrol: rename lruvec_holds_page_lru_lock to page_matches_lruvec
In reply to Muchun Songlruvec_holds_page_lru_lock() doesn't check anything about locking and is used to check whether the page belongs to the lruvec. So rename it to page_matches_lruvec().
--- include/linux/memcontrol.h | 7 +++---- mm/vmscan.c | 2 +- 2 files changed, 4 insertions(+), 5 deletions(-)
include/linux/memcontrol.h +3 -4
@@ -1492,8 +1492,7 @@ static inline void unlock_page_lruvec_irqrestore(struct lruvec *lruvec,spin_unlock_irqrestore (&lruvec->lru_lock, flags); } -static inline bool lruvec_holds_page_lru_lock (struct page *page, -struct lruvec *lruvec) +static inline bool page_matches_lruvec (struct page *page,struct lruvec *lruvec) {return lruvec_pgdat (lruvec) ==page_pgdat (page) &&lruvec_memcg (lruvec) ==page_memcg (page);
@@ -1504,7 +1503,7 @@ static inline struct lruvec *relock_page_lruvec_irq(struct page *page, struct lruvec *locked_lruvec) {if (locked_lruvec) { -if (lruvec_holds_page_lru_lock (page, locked_lruvec)) +if (page_matches_lruvec (page, locked_lruvec))return locked_lruvec;unlock_page_lruvec_irq (locked_lruvec);
@@ -1518,7 +1517,7 @@ static inline struct lruvec *relock_page_lruvec_irqsave(struct page *page, struct lruvec *locked_lruvec, unsigned long *flags) {if (locked_lruvec) { -if (lruvec_holds_page_lru_lock (page, locked_lruvec)) +if (page_matches_lruvec (page, locked_lruvec))return locked_lruvec;unlock_page_lruvec_irqrestore (locked_lruvec, *flags);
mm/vmscan.c +1 -1
@@ -2062,7 +2062,7 @@ static unsigned noinline_for_stack move_pages_to_lru(struct lruvec *lruvec,* All pages were isolated from the same lruvec (and isolation * inhibits memcg migration). */ -VM_BUG_ON_PAGE (!lruvec_holds_page_lru_lock (page, lruvec), page); +VM_BUG_ON_PAGE (!page_matches_lruvec (page, lruvec), page);add_page_to_lru_list (page, lruvec); nr_pages =thp_nr_pages (page); nr_moved += nr_pages;
Signature · 1 line
2.11.0
Re: [PATCH v2 5/8] mm: memcontrol: rename lruvec_holds_page_lru_lock to page_matches_lruvec
Quoted 5 lines · Muchun Song
lruvec_holds_page_lru_lock()doesn't check anything about locking and is used to check whether the page belongs to the lruvec. So rename it topage_matches_lruvec().Signed-off-by Muchun Song <[email protected]>
Thanks
Quoted 52 lines
--- include/linux/memcontrol.h | 7 +++---- mm/vmscan.c | 2 +- 2 files changed, 4 insertions(+), 5 deletions(-)include/linux/memcontrol.h +3
-4@@ -1492,8 +1492,7 @@ static inline void unlock_page_lruvec_irqrestore(struct lruvec *lruvec,spin_unlock_irqrestore (&lruvec->lru_lock, flags); } -static inline bool lruvec_holds_page_lru_lock (struct page *page, -struct lruvec *lruvec) +static inline bool page_matches_lruvec (struct page *page,struct lruvec *lruvec) {return lruvec_pgdat (lruvec) ==page_pgdat (page) &&lruvec_memcg (lruvec) ==page_memcg (page);@@ -1504,7 +1503,7 @@ static inline struct lruvec *relock_page_lruvec_irq(struct page *page, struct lruvec *locked_lruvec) {if (locked_lruvec) { -if (lruvec_holds_page_lru_lock (page, locked_lruvec)) +if (page_matches_lruvec (page, locked_lruvec))return locked_lruvec;unlock_page_lruvec_irq (locked_lruvec);@@ -1518,7 +1517,7 @@ static inline struct lruvec *relock_page_lruvec_irqsave(struct page *page, struct lruvec *locked_lruvec, unsigned long *flags) {if (locked_lruvec) { -if (lruvec_holds_page_lru_lock (page, locked_lruvec)) +if (page_matches_lruvec (page, locked_lruvec))return locked_lruvec;unlock_page_lruvec_irqrestore (locked_lruvec, *flags);mm/vmscan.c +1
-1@@ -2062,7 +2062,7 @@ static unsigned noinline_for_stack move_pages_to_lru(struct lruvec *lruvec,* All pages were isolated from the same lruvec (and isolation * inhibits memcg migration). */ -VM_BUG_ON_PAGE (!lruvec_holds_page_lru_lock (page, lruvec), page); +VM_BUG_ON_PAGE (!page_matches_lruvec (page, lruvec), page);add_page_to_lru_list (page, lruvec); nr_pages =thp_nr_pages (page); nr_moved += nr_pages;Signature · 1 line
2.11.0
Signature · 2 lines
Michal Hocko SUSE Labs
Re: [PATCH v2 5/8] mm: memcontrol: rename lruvec_holds_page_lru_lock to page_matches_lruvec
In reply to Muchun SongQuoted 5 lines · Muchun Song
lruvec_holds_page_lru_lock()doesn't check anything about locking and is used to check whether the page belongs to the lruvec. So rename it topage_matches_lruvec().Signed-off-by Muchun Song <[email protected]>
The rename makes sense, since the previous name was defined by a specific use case rather than what it does. That said, it did imply a lock context that makes the test result stable. Without that the function could use a short comment, IMO. How about:
/* Test requires a stable page->memcg binding, see page_memcg() */
With that,
Re: [External] Re: [PATCH v2 5/8] mm: memcontrol: rename lruvec_holds_page_lru_lock to page_matches_lruvec
Quoted 6 lines · Johannes Weiner
Quoted 4 lines · Muchun Song
lruvec_holds_page_lru_lock()doesn't check anything about locking and is used to check whether the page belongs to the lruvec. So rename it topage_matches_lruvec().
Quoted 8 lines
Quoted 1 line
Signed-off-by Muchun Song <[email protected]>The rename makes sense, since the previous name was defined by a specific use case rather than what it does. That said, it did imply a lock context that makes the test result stable. Without that the function could use a short comment, IMO. How about:
/* Test requires a stable
page->memcgbinding, seepage_memcg()*/
Make sense. I will add this comment.
Quoted 3 lines
With that,
Acked-by Johannes Weiner <[email protected]>
Thanks.
Re: [PATCH v2 5/8] mm: memcontrol: rename lruvec_holds_page_lru_lock to page_matches_lruvec
In reply to Muchun SongQuoted 6 lines · Muchun Song
lruvec_holds_page_lru_lock()doesn't check anything about locking and is used to check whether the page belongs to the lruvec. So rename it topage_matches_lruvec().Signed-off-by Muchun Song <[email protected]>
PATCH v2 6/8 mm: memcontrol: simplify the logic of objcg pinning memcg
In reply to Muchun SongThe 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().
--- mm/memcontrol.c | 20 ++++++-------------- 1 file changed, 6 insertions(+), 14 deletions(-)
mm/memcontrol.c +6 -14
@@ -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);
Signature · 1 line
2.11.0
PATCH v2 7/8 mm: memcontrol: move obj_cgroup_uncharge_pages() out of css_set_lock
In reply to Muchun SongThe css_set_lock is used to guard the list of inherited objcgs. So there is no need to uncharge kernel memory under css_set_lock. Just move it out of the lock.
--- mm/memcontrol.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)
mm/memcontrol.c +2 -1
@@ -289,9 +289,10 @@ static void obj_cgroup_release(struct percpu_ref *ref)WARN_ON_ONCE (nr_bytes & (PAGE_SIZE -1 )); nr_pages = nr_bytes >>PAGE_SHIFT ; -spin_lock_irqsave (&css_set_lock, flags);if (nr_pages)obj_cgroup_uncharge_pages (objcg, nr_pages); + +spin_lock_irqsave (&css_set_lock, flags);list_del (&objcg->list);spin_unlock_irqrestore (&css_set_lock, flags);
Signature · 1 line
2.11.0
PATCH v2 8/8 mm: vmscan: remove noinline_for_stack
In reply to Muchun SongThe noinline_for_stack is introduced by commit 666356297ec4 ("vmscan:
set up pagevec as late as possible in shrink_inactive_list()"), its purpose is to delay the allocation of pagevec as late as possible to save stack memory. But the commit 2bcf88796381 ("mm: take pagevecs off reclaim stack") replace pagevecs by lists of pages_to_free. So we do not need noinline_for_stack, just remove it (let the compiler decide whether to inline).
--- mm/vmscan.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-)
mm/vmscan.c +3 -3
@@ -2014,8 +2014,8 @@ static int too_many_isolated(struct pglist_data *pgdat, int file,* * Returns the number of pages moved to the given lruvec. */ -static unsigned noinline_for_stack move_pages_to_lru (struct lruvec *lruvec, -struct list_head *list) +static unsigned int move_pages_to_lru (struct lruvec *lruvec, +struct list_head *list) {int nr_pages, nr_moved =0 ;LIST_HEAD (pages_to_free);
@@ -2095,7 +2095,7 @@ static int current_may_throttle(void)* shrink_inactive_list() is a helper for shrink_node(). It returns the number * of reclaimed pages */ -static noinline_for_stack unsigned long +static unsigned long shrink_inactive_list (unsigned long nr_to_scan,struct lruvec *lruvec,struct scan_control *sc,enum lru_list lru) {
Signature · 1 line
2.11.0