[RFC PATCH 08/15] mm: memcontrol: make lruvec lock safe when the LRU pages reparented

From: Muchun Song
Date: Tue Mar 30 2021 - 06:23:00 EST


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@xxxxxxxxxxxxx>
---
include/linux/memcontrol.h | 16 +++----------
mm/compaction.c | 13 ++++++++++-
mm/memcontrol.c | 56 +++++++++++++++++++++++++++++-----------------
mm/swap.c | 5 +++++
4 files changed, 56 insertions(+), 34 deletions(-)

diff --git a/include/linux/memcontrol.h b/include/linux/memcontrol.h
index 463fc7b78396..5d7c8a060843 100644
--- a/include/linux/memcontrol.h
+++ b/include/linux/memcontrol.h
@@ -735,7 +735,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)
{
@@ -771,14 +773,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;
@@ -1500,10 +1494,6 @@ static inline
void count_memcg_event_mm(struct mm_struct *mm, enum vm_event_item idx)
{
}
-
-static inline void lruvec_memcg_debug(struct lruvec *lruvec, struct page *page)
-{
-}
#endif /* CONFIG_MEMCG */

static inline void __inc_lruvec_kmem_state(void *p, enum node_stat_item idx)
diff --git a/mm/compaction.c b/mm/compaction.c
index d6b7d5f90fce..b0ad635dd576 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,16 @@ 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;
+ }
+
+ /*
+ * Preemption is disabled in the internal of spin_lock, which can serve
+ * as RCU read-side critical sections.
+ */
+ rcu_read_unlock();

return lruvec;
}
diff --git a/mm/memcontrol.c b/mm/memcontrol.c
index 0107f23e7035..2592e2b072ef 100644
--- a/mm/memcontrol.c
+++ b/mm/memcontrol.c
@@ -1314,23 +1314,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
@@ -1345,10 +1328,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;
}
@@ -1357,10 +1351,21 @@ 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;
+ }
+
+ /*
+ * Preemption is disabled in the internal of spin_lock, which can serve
+ * as RCU read-side critical sections.
+ */
+ rcu_read_unlock();

return lruvec;
}
@@ -1369,10 +1374,21 @@ 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;
+ }
+
+ /*
+ * Preemption is disabled in the internal of spin_lock, which can serve
+ * as RCU read-side critical sections.
+ */
+ rcu_read_unlock();

return lruvec;
}
diff --git a/mm/swap.c b/mm/swap.c
index af695acb7413..044c240d8873 100644
--- a/mm/swap.c
+++ b/mm/swap.c
@@ -300,6 +300,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