[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-Id: <20210421070059.69361-4-songmuchun@bytedance.com>
Date: Wed, 21 Apr 2021 15:00:50 +0800
From: Muchun Song <songmuchun@...edance.com>
To: guro@...com, hannes@...xchg.org, mhocko@...nel.org,
akpm@...ux-foundation.org, shakeelb@...gle.com,
vdavydov.dev@...il.com
Cc: linux-kernel@...r.kernel.org, linux-mm@...ck.org,
duanxiongchun@...edance.com, fam.zheng@...edance.com,
bsingharora@...il.com, shy828301@...il.com,
alex.shi@...ux.alibaba.com, Muchun Song <songmuchun@...edance.com>
Subject: [RFC PATCH v3 03/12] mm: memcontrol: make lruvec lock safe when the LRU pages reparented
The diagram below shows how to make the page lruvec lock safe when the
LRU pages reparented.
lock_page_lruvec(page)
retry:
lruvec = mem_cgroup_page_lruvec(page);
// The page is reparented at this time.
spin_lock(&lruvec->lru_lock);
if (unlikely(lruvec_memcg(lruvec) != page_memcg(page)))
// Acquired the wrong lruvec lock and need to retry.
// Because this page is on the parent memcg lruvec list.
goto retry;
// If we reach here, it means that page_memcg(page) is stable.
memcg_reparent_objcgs(memcg)
// lruvec belongs to memcg and lruvec_parent belongs to parent memcg.
spin_lock(&lruvec->lru_lock);
spin_lock(&lruvec_parent->lru_lock);
// Move all the pages from the lruvec list to the parent lruvec list.
spin_unlock(&lruvec_parent->lru_lock);
spin_unlock(&lruvec->lru_lock);
After we acquire the lruvec lock, we need to check whether the page is
reparented. If so, we need to reacquire the new lruvec lock. On the
routine of the LRU pages reparenting, we will also acquire the lruvec
lock (Will be implemented in the later patch). So page_memcg() cannot
be changed when we hold the lruvec lock.
Since lruvec_memcg(lruvec) is always equal to page_memcg(page) after
we hold the lruvec lock, lruvec_memcg_debug() check is pointless. So
remove it.
This is a preparation for reparenting the LRU pages.
Signed-off-by: Muchun Song <songmuchun@...edance.com>
---
include/linux/memcontrol.h | 16 +++------------
mm/compaction.c | 10 +++++++++-
mm/memcontrol.c | 50 +++++++++++++++++++++++++++-------------------
mm/swap.c | 5 +++++
4 files changed, 47 insertions(+), 34 deletions(-)
diff --git a/include/linux/memcontrol.h b/include/linux/memcontrol.h
index 53a19db9f8eb..cb0d99583f77 100644
--- a/include/linux/memcontrol.h
+++ b/include/linux/memcontrol.h
@@ -746,7 +746,9 @@ 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
*
- * This function relies on page->mem_cgroup being stable.
+ * The lruvec can be changed to its parent lruvec when the page reparented.
+ * The caller need to recheck if it cares about this change (just like
+ * lock_page_lruvec() does).
*/
static inline struct lruvec *mem_cgroup_page_lruvec(struct page *page)
{
@@ -766,14 +768,6 @@ struct lruvec *lock_page_lruvec_irq(struct page *page);
struct lruvec *lock_page_lruvec_irqsave(struct page *page,
unsigned long *flags);
-#ifdef CONFIG_DEBUG_VM
-void lruvec_memcg_debug(struct lruvec *lruvec, struct page *page);
-#else
-static inline void lruvec_memcg_debug(struct lruvec *lruvec, struct page *page)
-{
-}
-#endif
-
static inline
struct mem_cgroup *mem_cgroup_from_css(struct cgroup_subsys_state *css){
return css ? container_of(css, struct mem_cgroup, css) : NULL;
@@ -1213,10 +1207,6 @@ static inline struct lruvec *mem_cgroup_page_lruvec(struct page *page)
return &pgdat->__lruvec;
}
-static inline void lruvec_memcg_debug(struct lruvec *lruvec, struct page *page)
-{
-}
-
static inline struct mem_cgroup *parent_mem_cgroup(struct mem_cgroup *memcg)
{
return NULL;
diff --git a/mm/compaction.c b/mm/compaction.c
index 082293587cc6..f88e2731d27c 100644
--- a/mm/compaction.c
+++ b/mm/compaction.c
@@ -517,6 +517,8 @@ compact_lock_page_lruvec_irqsave(struct page *page, unsigned long *flags,
{
struct lruvec *lruvec;
+ rcu_read_lock();
+retry:
lruvec = mem_cgroup_page_lruvec(page);
/* Track if the lock is contended in async mode */
@@ -529,7 +531,13 @@ compact_lock_page_lruvec_irqsave(struct page *page, unsigned long *flags,
spin_lock_irqsave(&lruvec->lru_lock, *flags);
out:
- lruvec_memcg_debug(lruvec, page);
+ if (unlikely(lruvec_memcg(lruvec) != page_memcg(page))) {
+ spin_unlock_irqrestore(&lruvec->lru_lock, *flags);
+ goto retry;
+ }
+
+ /* See the comments in lock_page_lruvec(). */
+ rcu_read_unlock();
return lruvec;
}
diff --git a/mm/memcontrol.c b/mm/memcontrol.c
index dae7ab2b1406..fd8e2c242726 100644
--- a/mm/memcontrol.c
+++ b/mm/memcontrol.c
@@ -1160,23 +1160,6 @@ int mem_cgroup_scan_tasks(struct mem_cgroup *memcg,
return ret;
}
-#ifdef CONFIG_DEBUG_VM
-void lruvec_memcg_debug(struct lruvec *lruvec, struct page *page)
-{
- struct mem_cgroup *memcg;
-
- if (mem_cgroup_disabled())
- return;
-
- memcg = page_memcg(page);
-
- if (!memcg)
- VM_BUG_ON_PAGE(lruvec_memcg(lruvec) != root_mem_cgroup, page);
- else
- VM_BUG_ON_PAGE(lruvec_memcg(lruvec) != memcg, page);
-}
-#endif
-
/**
* lock_page_lruvec - lock and return lruvec for a given page.
* @page: the page
@@ -1191,10 +1174,21 @@ struct lruvec *lock_page_lruvec(struct page *page)
{
struct lruvec *lruvec;
+ rcu_read_lock();
+retry:
lruvec = mem_cgroup_page_lruvec(page);
spin_lock(&lruvec->lru_lock);
- lruvec_memcg_debug(lruvec, page);
+ if (unlikely(lruvec_memcg(lruvec) != page_memcg(page))) {
+ spin_unlock(&lruvec->lru_lock);
+ goto retry;
+ }
+
+ /*
+ * Preemption is disabled in the internal of spin_lock, which can serve
+ * as RCU read-side critical sections.
+ */
+ rcu_read_unlock();
return lruvec;
}
@@ -1203,10 +1197,18 @@ struct lruvec *lock_page_lruvec_irq(struct page *page)
{
struct lruvec *lruvec;
+ rcu_read_lock();
+retry:
lruvec = mem_cgroup_page_lruvec(page);
spin_lock_irq(&lruvec->lru_lock);
- lruvec_memcg_debug(lruvec, page);
+ if (unlikely(lruvec_memcg(lruvec) != page_memcg(page))) {
+ spin_unlock_irq(&lruvec->lru_lock);
+ goto retry;
+ }
+
+ /* See the comments in lock_page_lruvec(). */
+ rcu_read_unlock();
return lruvec;
}
@@ -1215,10 +1217,18 @@ struct lruvec *lock_page_lruvec_irqsave(struct page *page, unsigned long *flags)
{
struct lruvec *lruvec;
+ rcu_read_lock();
+retry:
lruvec = mem_cgroup_page_lruvec(page);
spin_lock_irqsave(&lruvec->lru_lock, *flags);
- lruvec_memcg_debug(lruvec, page);
+ if (unlikely(lruvec_memcg(lruvec) != page_memcg(page))) {
+ spin_unlock_irqrestore(&lruvec->lru_lock, *flags);
+ goto retry;
+ }
+
+ /* See the comments in lock_page_lruvec(). */
+ rcu_read_unlock();
return lruvec;
}
diff --git a/mm/swap.c b/mm/swap.c
index e0d5699213cc..f3ce307d09fa 100644
--- a/mm/swap.c
+++ b/mm/swap.c
@@ -313,6 +313,11 @@ void lru_note_cost(struct lruvec *lruvec, bool file, unsigned int nr_pages)
void lru_note_cost_page(struct page *page)
{
+ /*
+ * The rcu read lock is held by the caller, so we do not need to
+ * care about the lruvec returned by mem_cgroup_page_lruvec() being
+ * released.
+ */
lru_note_cost(mem_cgroup_page_lruvec(page),
page_is_file_lru(page), thp_nr_pages(page));
}
--
2.11.0
Powered by blists - more mailing lists