Skip to contentSkip to conversation

per lruvec lru_lock for memcg

PATCH In review Alex Shi 8 participants 36 messages Mar 2, 2020 – Mar 6, 2020 3 messages not in the archive

Subsystems (15)
lore
Share & tools
lore

Alex Shi

author
#0 lore raw

v9 cover per lruvec lru_lock for memcg

Hi all,

This patchset mainly includes 3 parts:
1, some code cleanup and minimum optimization as a preparation. 2, use TestCleanPageLRU as page isolation's precondition 3, replace per node lru_lock with per memcg per node lru_lock

The keypoint of this patchset is moving lru_lock into lruvec, give a lru_lock for each of lruvec, thus bring a lru_lock for each of memcg per node. So on a large node machine, each of memcg don't suffer from per node pgdat->lru_lock waiting. They could go fast with their self lru_lock now.

Since lruvec belongs to each memcg, the critical point is to keep page's memcg stable, so we take PageLRU as its isolation's precondition. Thanks for Johannes Weiner help and suggestion!

Following Daniel Jordan's suggestion, I have run 208 'dd' with on 104 containers on a 2s * 26cores * HT box with a modefied case:
https://git.kernel.org/pub/scm/linux/kernel/git/wfg/vm-scalability.git/tree/case-lru-file-readtwice

With this patchset, the readtwice performance increased about 80% in concurrent containers.

Thanks Hugh Dickins and Konstantin Khlebnikov, they both brought this idea 8 years ago, and others who give comments as well: Daniel Jordan, Mel Gorman, Shakeel Butt, Matthew Wilcox etc.

Thanks for Testing support from Intel 0day and Rong Chen, Fengguang Wu, and Yun Wang.

v9,
a, use TestCleanPageLRU as page isolation's precondition, that
resolved the page's memcg stable problem, Thanks Johannes Weiner!
b, few more code clean up patches

v8,
a, redo lock_page_lru cleanup as Konstantin Khlebnikov suggested. b, fix a bug in lruvec_memcg_debug, reported by Hugh Dickins

...

v1: initial version, aim testing show 5% performance increase on a 16 threads box.

Alex Shi (19):
mm/vmscan: remove unnecessary lruvec adding
mm/memcg: fold lock_page_lru into commit_charge mm/page_idle: no unlikely double check for idle page counting mm/thp: move lru_add_page_tail func to huge_memory.c mm/thp: clean up lru_add_page_tail
mm/thp: narrow lru locking
mm/lru: introduce TestClearPageLRU
mm/lru: add page isolation precondition in __isolate_lru_page mm/mlock: ClearPageLRU before get lru lock in munlock page isolation mm/lru: take PageLRU first in moving page between lru lists mm/memcg: move SetPageLRU out of lru_lock in commit_charge mm/mlock: clean up __munlock_isolate_lru_page
mm/lru: replace pgdat lru_lock with lruvec lock
mm/lru: introduce the relock_page_lruvec function mm/mlock: optimize munlock_pagevec by relocking
mm/swap: only change the lru_lock iff page's lruvec is different mm/pgdat: remove pgdat lru_lock
mm/lru: add debug checking for page memcg moving mm/memcg: add debug checking in lock_page_memcg

Hugh Dickins (1):
mm/lru: revise the comments of lru_lock

 Documentation/admin-guide/cgroup-v1/memcg_test.rst |  15 +-
 Documentation/admin-guide/cgroup-v1/memory.rst     |   8 +-
 Documentation/trace/events-kmem.rst                |   2 +-
 Documentation/vm/unevictable-lru.rst               |  22 +--
 include/linux/memcontrol.h                         |  77 ++++++++++
 include/linux/mm_types.h                           |   2 +-
 include/linux/mmzone.h                             |   5 +-
 include/linux/page-flags.h                         |   1 +
 include/linux/swap.h                               |   6 +-
 mm/compaction.c                                    |  78 ++++++----
 mm/filemap.c                                       |   4 +-
 mm/huge_memory.c                                   |  54 +++++--
 mm/memcontrol.c                                    | 110 +++++++++-----
 mm/mlock.c                                         |  88 ++++++-----
 mm/mmzone.c                                        |   1 +
 mm/page_alloc.c                                    |   1 -
 mm/page_idle.c                                     |   8 -
 mm/rmap.c                                          |   2 +-
 mm/swap.c                                          | 160 +++++++-------------
 mm/vmscan.c                                        | 163 +++++++++++----------
 20 files changed, 446 insertions(+), 361 deletions(-)
Signature · 1 line
1.8.3.1
#1 lore raw

PATCH v9 1/20 mm/vmscan: remove unnecessary lruvec adding

We don't have to add a freeable page into lru and then remove from it. This change saves a couple of actions and makes the moving more clear.

The SetPageLRU needs to be kept here for list intergrity. Otherwise:
#0 mave_pages_to_lru #1 release_pages
if (put_page_testzero())
if !put_page_testzero
!PageLRU //skip lru_lock
list_add(&page->lru,)
list_add(&page->lru,) //corrupt

Cc Andrew Morton <[email protected]>
Cc Johannes Weiner <[email protected]>
Cc Tejun Heo <[email protected]>
Cc Matthew Wilcox <[email protected]>
Cc Hugh Dickins <[email protected]>
---
 mm/vmscan.c | 32 +++++++++++++++++++++-----------
 1 file changed, 21 insertions(+), 11 deletions(-)
mm/vmscan.c +21 -11
index 876370565455..dcdd33f65f43 100644
@@ -1838,26 +1838,29 @@ static unsigned noinline_for_stack move_pages_to_lru(struct lruvec *lruvec,
 	while (!list_empty(list)) {
 		page = lru_to_page(list);
 		VM_BUG_ON_PAGE(PageLRU(page), page);
+		list_del(&page->lru);
 		if (unlikely(!page_evictable(page))) {
-			list_del(&page->lru);
 			spin_unlock_irq(&pgdat->lru_lock);
 			putback_lru_page(page);
 			spin_lock_irq(&pgdat->lru_lock);
 			continue;
 		}
-		lruvec = mem_cgroup_page_lruvec(page, pgdat);
 
+		/*
+		 * The SetPageLRU needs to be kept here for list intergrity.
+		 * Otherwise:
+		 *   #0 mave_pages_to_lru              #1 release_pages
+		 *				       if (put_page_testzero())
+		 *   if !put_page_testzero
+		 *				         !PageLRU //skip lru_lock
+		 *                                         list_add(&page->lru,)
+		 *     list_add(&page->lru,) //corrupt
+		 */
 		SetPageLRU(page);
-		lru = page_lru(page);
-
-		nr_pages = hpage_nr_pages(page);
-		update_lru_size(lruvec, lru, page_zonenum(page), nr_pages);
-		list_move(&page->lru, &lruvec->lists[lru]);
 
-		if (put_page_testzero(page)) {
+		if (unlikely(put_page_testzero(page))) {
 			__ClearPageLRU(page);
 			__ClearPageActive(page);
-			del_page_from_lru_list(page, lruvec, lru);
 
 			if (unlikely(PageCompound(page))) {
 				spin_unlock_irq(&pgdat->lru_lock);
@@ -1865,9 +1868,16 @@ static unsigned noinline_for_stack move_pages_to_lru(struct lruvec *lruvec,
 				spin_lock_irq(&pgdat->lru_lock);
 			} else
 				list_add(&page->lru, &pages_to_free);
-		} else {
-			nr_moved += nr_pages;
+			continue;
 		}
+
+		lruvec = mem_cgroup_page_lruvec(page, pgdat);
+		lru = page_lru(page);
+		nr_pages = hpage_nr_pages(page);
+
+		update_lru_size(lruvec, lru, page_zonenum(page), nr_pages);
+		list_add(&page->lru, &lruvec->lists[lru]);
+		nr_moved += nr_pages;
 	}
 
 	/*
Signature · 1 line
1.8.3.1
#2 lore raw

PATCH v9 2/20 mm/memcg: fold lock_page_lru into commit_charge

In reply to Alex Shi

As Konstantin Khlebnikov mentioned:

Also I don't like these functions:
- called lock/unlock but actually also isolates
- used just once
- pgdat evaluated twice

Cleanup and fold these functions into commit_charge. It also reduces lock time while lrucare && !PageLRU.

Cc Johannes Weiner <[email protected]>
Cc Michal Hocko <[email protected]>
Cc Konstantin Khlebnikov <[email protected]>
Cc Vladimir Davydov <[email protected]>
Cc Andrew Morton <[email protected]>
---
 mm/memcontrol.c | 57 ++++++++++++++++++++-------------------------------------
 1 file changed, 20 insertions(+), 37 deletions(-)
mm/memcontrol.c +20 -37
index d09776cd6e10..875e2aebcde7 100644
@@ -2572,41 +2572,11 @@ static void cancel_charge(struct mem_cgroup *memcg, unsigned int nr_pages)
 	css_put_many(&memcg->css, nr_pages);
 }
 
-static void lock_page_lru(struct page *page, int *isolated)
-{
-	pg_data_t *pgdat = page_pgdat(page);
-
-	spin_lock_irq(&pgdat->lru_lock);
-	if (PageLRU(page)) {
-		struct lruvec *lruvec;
-
-		lruvec = mem_cgroup_page_lruvec(page, pgdat);
-		ClearPageLRU(page);
-		del_page_from_lru_list(page, lruvec, page_lru(page));
-		*isolated = 1;
-	} else
-		*isolated = 0;
-}
-
-static void unlock_page_lru(struct page *page, int isolated)
-{
-	pg_data_t *pgdat = page_pgdat(page);
-
-	if (isolated) {
-		struct lruvec *lruvec;
-
-		lruvec = mem_cgroup_page_lruvec(page, pgdat);
-		VM_BUG_ON_PAGE(PageLRU(page), page);
-		SetPageLRU(page);
-		add_page_to_lru_list(page, lruvec, page_lru(page));
-	}
-	spin_unlock_irq(&pgdat->lru_lock);
-}
-
 static void commit_charge(struct page *page, struct mem_cgroup *memcg,
 			  bool lrucare)
 {
-	int isolated;
+	struct lruvec *lruvec = NULL;
+	pg_data_t *pgdat;
 
 	VM_BUG_ON_PAGE(page->mem_cgroup, page);
 
@@ -2614,9 +2584,17 @@ static void commit_charge(struct page *page, struct mem_cgroup *memcg,
 	 * In some cases, SwapCache and FUSE(splice_buf->radixtree), the page
 	 * may already be on some other mem_cgroup's LRU.  Take care of it.
 	 */
-	if (lrucare)
-		lock_page_lru(page, &isolated);
-
+	if (lrucare) {
+		pgdat = page_pgdat(page);
+		spin_lock_irq(&pgdat->lru_lock);
+
+		if (PageLRU(page)) {
+			lruvec = mem_cgroup_page_lruvec(page, pgdat);
+			ClearPageLRU(page);
+			del_page_from_lru_list(page, lruvec, page_lru(page));
+		} else
+			spin_unlock_irq(&pgdat->lru_lock);
+	}
 	/*
 	 * Nobody should be changing or seriously looking at
 	 * page->mem_cgroup at this point:
@@ -2633,8 +2611,13 @@ static void commit_charge(struct page *page, struct mem_cgroup *memcg,
 	 */
 	page->mem_cgroup = memcg;
 
-	if (lrucare)
-		unlock_page_lru(page, isolated);
+	if (lrucare && lruvec) {
+		lruvec = mem_cgroup_page_lruvec(page, pgdat);
+		VM_BUG_ON_PAGE(PageLRU(page), page);
+		SetPageLRU(page);
+		add_page_to_lru_list(page, lruvec, page_lru(page));
+		spin_unlock_irq(&pgdat->lru_lock);
+	}
 }
 
 #ifdef CONFIG_MEMCG_KMEM
Signature · 1 line
1.8.3.1
#3 lore raw

PATCH v9 3/20 mm/page_idle: no unlikely double check for idle page counting

In reply to Alex Shi

As func comments mentioned, few isolated page missing be tolerated. So why not do further to drop the unlikely double check. That won't cause more idle pages, but reduce a lock contention.

This is also a preparation for later new page isolation feature.

---
 mm/page_idle.c | 8 --------
 1 file changed, 8 deletions(-)
mm/page_idle.c +0 -8
index 295512465065..914df63948b1 100644
@@ -31,7 +31,6 @@
 static struct page *page_idle_get_page(unsigned long pfn)
 {
 	struct page *page;
-	pg_data_t *pgdat;
 
 	if (!pfn_valid(pfn))
 		return NULL;
@@ -41,13 +40,6 @@ static struct page *page_idle_get_page(unsigned long pfn)
 	    !get_page_unless_zero(page))
 		return NULL;
 
-	pgdat = page_pgdat(page);
-	spin_lock_irq(&pgdat->lru_lock);
-	if (unlikely(!PageLRU(page))) {
-		put_page(page);
-		page = NULL;
-	}
-	spin_unlock_irq(&pgdat->lru_lock);
 	return page;
 }
 
Signature · 1 line
1.8.3.1
#4 lore raw

PATCH v9 4/20 mm/thp: move lru_add_page_tail func to huge_memory.c

In reply to Alex Shi

The func is only used in huge_memory.c, defining it in other file with a CONFIG_TRANSPARENT_HUGEPAGE macro restrict just looks weird.

Let's move it close user.

Cc Andrew Morton <[email protected]>
Cc Johannes Weiner <[email protected]>
Cc Matthew Wilcox <[email protected]>
Cc Hugh Dickins <[email protected]>
---
 include/linux/swap.h |  4 ++--
 mm/huge_memory.c     | 35 +++++++++++++++++++++++++++++++++++
 mm/swap.c            | 41 +----------------------------------------
 3 files changed, 38 insertions(+), 42 deletions(-)
include/linux/swap.h +2 -2
index 1e99f7ac1d7e..c555e8f161ad 100644
@@ -328,11 +328,11 @@ struct vma_swap_readahead {
 
 
 /* linux/mm/swap.c */
+extern void update_page_reclaim_stat(struct lruvec *lruvec,
+						int file, int rotated);
 extern void lru_cache_add(struct page *);
 extern void lru_cache_add_anon(struct page *page);
 extern void lru_cache_add_file(struct page *page);
-extern void lru_add_page_tail(struct page *page, struct page *page_tail,
-			 struct lruvec *lruvec, struct list_head *head);
 extern void activate_page(struct page *);
 extern void mark_page_accessed(struct page *);
 extern void lru_add_drain(void);
mm/huge_memory.c +35 -0
index b08b199f9a11..acef164a8981 100644
@@ -2445,6 +2445,41 @@ static void remap_page(struct page *page)
 	}
 }
 
+void lru_add_page_tail(struct page *page, struct page *page_tail,
+		       struct lruvec *lruvec, struct list_head *list)
+{
+	const int file = 0;
+
+	VM_BUG_ON_PAGE(!PageHead(page), page);
+	VM_BUG_ON_PAGE(PageCompound(page_tail), page);
+	VM_BUG_ON_PAGE(PageLRU(page_tail), page);
+	lockdep_assert_held(&lruvec_pgdat(lruvec)->lru_lock);
+
+	if (!list)
+		SetPageLRU(page_tail);
+
+	if (likely(PageLRU(page)))
+		list_add_tail(&page_tail->lru, &page->lru);
+	else if (list) {
+		/* page reclaim is reclaiming a huge page */
+		get_page(page_tail);
+		list_add_tail(&page_tail->lru, list);
+	} else {
+		/*
+		 * Head page has not yet been counted, as an hpage,
+		 * so we must account for each subpage individually.
+		 *
+		 * Put page_tail on the list at the correct position
+		 * so they all end up in order.
+		 */
+		add_page_to_lru_list_tail(page_tail, lruvec,
+					  page_lru(page_tail));
+	}
+
+	if (!PageUnevictable(page))
+		update_page_reclaim_stat(lruvec, file, PageActive(page_tail));
+}
+
 static void __split_huge_page_tail(struct page *head, int tail,
 		struct lruvec *lruvec, struct list_head *list)
 {
mm/swap.c +1 -40

53 lines not on this page · raw message

Signature · 1 line
1.8.3.1
#5 lore raw

Re: [PATCH v9 04/20] mm/thp: move lru_add_page_tail func to huge_memory.c

Quoted 4 lines · Alex Shi

The func is only used in huge_memory.c, defining it in other file with a CONFIG_TRANSPARENT_HUGEPAGE macro restrict just looks weird.

Let's move it close user.

I don't think it's strong enough justification. I would rather keep all lru helpers in one place.

Signature · 1 line
 Kirill A. Shutemov
#6 lore raw

Re: [PATCH v9 04/20] mm/thp: move lru_add_page_tail func to huge_memory.c

ÔÚ 2020/3/4 ÏÂÎç3:47, Kirill A. Shutemov дµÀ:

Quoted 9 lines · Alex Shi
Quoted 4 lines

The func is only used in huge_memory.c, defining it in other file with a CONFIG_TRANSPARENT_HUGEPAGE macro restrict just looks weird.

Let's move it close user.

I don't think it's strong enough justification. I would rather keep all lru helpers in one place.

Thanks for comments, Kirill,

If it's a common used func, yes.

But if you look into details of this func, it's thp used only func.

Thanks
Alex

#7 lore raw

PATCH v9 5/20 mm/thp: clean up lru_add_page_tail

In reply to Alex Shi

Since the first parameter is only used by head page, it's better to make it stright. And no needs to keep head checking:

	VM_BUG_ON_PAGE(!PageHead(page), page);
---
 mm/huge_memory.c | 13 ++++++-------
 1 file changed, 6 insertions(+), 7 deletions(-)
mm/huge_memory.c +6 -7
index acef164a8981..599367d25fca 100644
@@ -2445,21 +2445,20 @@ static void remap_page(struct page *page)
 	}
 }
 
-void lru_add_page_tail(struct page *page, struct page *page_tail,
+void lru_add_page_tail(struct page *head, struct page *page_tail,
 		       struct lruvec *lruvec, struct list_head *list)
 {
 	const int file = 0;
 
-	VM_BUG_ON_PAGE(!PageHead(page), page);
-	VM_BUG_ON_PAGE(PageCompound(page_tail), page);
-	VM_BUG_ON_PAGE(PageLRU(page_tail), page);
+	VM_BUG_ON_PAGE(PageCompound(page_tail), head);
+	VM_BUG_ON_PAGE(PageLRU(page_tail), head);
 	lockdep_assert_held(&lruvec_pgdat(lruvec)->lru_lock);
 
 	if (!list)
 		SetPageLRU(page_tail);
 
-	if (likely(PageLRU(page)))
-		list_add_tail(&page_tail->lru, &page->lru);
+	if (likely(PageLRU(head)))
+		list_add_tail(&page_tail->lru, &head->lru);
 	else if (list) {
 		/* page reclaim is reclaiming a huge page */
 		get_page(page_tail);
@@ -2476,7 +2475,7 @@ void lru_add_page_tail(struct page *page, struct page *page_tail,
 					  page_lru(page_tail));
 	}
 
-	if (!PageUnevictable(page))
+	if (!PageUnevictable(head))
 		update_page_reclaim_stat(lruvec, file, PageActive(page_tail));
 }
 
Signature · 1 line
1.8.3.1
#8 lore raw

PATCH v9 6/20 mm/thp: narrow lru locking

In reply to Alex Shi

Lru locking just guard the lru list and subpage's Mlocked. Including other things can't give help just delay the locking release. So narrow the locking for early lock release and better code meaning.

Cc Kirill A. Shutemov <[email protected]>
Cc Andrea Arcangeli <[email protected]>
Cc Johannes Weiner <[email protected]>
Cc Andrew Morton <[email protected]>
---
 mm/huge_memory.c | 17 +++++++----------
 1 file changed, 7 insertions(+), 10 deletions(-)
mm/huge_memory.c +7 -10
index 599367d25fca..3835f87d03fd 100644
@@ -2542,13 +2542,14 @@ static void __split_huge_page_tail(struct page *head, int tail,
 }
 
 static void __split_huge_page(struct page *page, struct list_head *list,
-		pgoff_t end, unsigned long flags)
+				pgoff_t end)
 {
 	struct page *head = compound_head(page);
 	pg_data_t *pgdat = page_pgdat(head);
 	struct lruvec *lruvec;
 	struct address_space *swap_cache = NULL;
 	unsigned long offset = 0;
+	unsigned long flags;
 	int i;
 
 	lruvec = mem_cgroup_page_lruvec(head, pgdat);
@@ -2564,6 +2565,9 @@ static void __split_huge_page(struct page *page, struct list_head *list,
 		xa_lock(&swap_cache->i_pages);
 	}
 
+	/* Lru list would be changed, don't care head's LRU bit. */
+	spin_lock_irqsave(&pgdat->lru_lock, flags);
+
 	for (i = HPAGE_PMD_NR - 1; i >= 1; i--) {
 		__split_huge_page_tail(head, i, lruvec, list);
 		/* Some pages can be beyond i_size: drop them from page cache */
@@ -2581,6 +2585,7 @@ static void __split_huge_page(struct page *page, struct list_head *list,
 					head + i, 0);
 		}
 	}
+	spin_unlock_irqrestore(&pgdat->lru_lock, flags);
 
 	ClearPageCompound(head);
 
@@ -2601,8 +2606,6 @@ static void __split_huge_page(struct page *page, struct list_head *list,
 		xa_unlock(&head->mapping->i_pages);
 	}
 
-	spin_unlock_irqrestore(&pgdat->lru_lock, flags);
-
 	remap_page(head);
 
 	for (i = 0; i < HPAGE_PMD_NR; i++) {
@@ -2740,13 +2743,11 @@ bool can_split_huge_page(struct page *page, int *pextra_pins)
 int split_huge_page_to_list(struct page *page, struct list_head *list)
 {
 	struct page *head = compound_head(page);
-	struct pglist_data *pgdata = NODE_DATA(page_to_nid(head));
 	struct deferred_split *ds_queue = get_deferred_split_queue(head);
 	struct anon_vma *anon_vma = NULL;
 	struct address_space *mapping = NULL;
 	int count, mapcount, extra_pins, ret;
 	bool mlocked;
-	unsigned long flags;
 	pgoff_t end;
 
 	VM_BUG_ON_PAGE(is_huge_zero_page(head), head);
@@ -2812,9 +2813,6 @@ int split_huge_page_to_list(struct page *page, struct list_head *list)
 	if (mlocked)
 		lru_add_drain();
 
-	/* prevent PageLRU to go away from under us, and freeze lru stats */
-	spin_lock_irqsave(&pgdata->lru_lock, flags);
-
 	if (mapping) {
 		XA_STATE(xas, &mapping->i_pages, page_index(head));
 
@@ -2844,7 +2842,7 @@ int split_huge_page_to_list(struct page *page, struct list_head *list)
 				__dec_node_page_state(head, NR_FILE_THPS);
 		}
 
-		__split_huge_page(page, list, end, flags);
+		__split_huge_page(page, list, end);
 		if (PageSwapCache(head)) {
 			swp_entry_t entry = { .val = page_private(head) };
 
@@ -2863,7 +2861,6 @@ int split_huge_page_to_list(struct page *page, struct list_head *list)
 		spin_unlock(&ds_queue->split_queue_lock);
 fail:		if (mapping)
 			xa_unlock(&mapping->i_pages);
-		spin_unlock_irqrestore(&pgdata->lru_lock, flags);
 		remap_page(head);
 		ret = -EBUSY;
 	}
Signature · 1 line
1.8.3.1
#9 lore raw

Re: [PATCH v9 06/20] mm/thp: narrow lru locking

mm/huge_memory.c static void __split_huge_page(struct page *page, struct list_head *list, · line 2573

Quoted 10 lines · Alex Shi
mm/huge_memory.c +3 -0
@@ -2564,6 +2565,9 @@ static void __split_huge_page(struct page *page, struct list_head *list,
 		xa_lock(&swap_cache->i_pages);
 	}
 
+	/* Lru list would be changed, don't care head's LRU bit. */
+	spin_lock_irqsave(&pgdat->lru_lock, flags);
+
 	for (i = HPAGE_PMD_NR - 1; i >= 1; i--) {
 		__split_huge_page_tail(head, i, lruvec, list);
 		/* Some pages can be beyond i_size: drop them from page cache */

You change locking order WRT i_pages lock. Is it safe?

Signature · 1 line
 Kirill A. Shutemov
#10 lore raw

Re: [PATCH v9 06/20] mm/thp: narrow lru locking

ÔÚ 2020/3/4 ÏÂÎç4:02, Kirill A. Shutemov дµÀ:

Quoted 5 lines · Alex Shi
Quoted 4 lines
mm/huge_memory.c +0 -0
@@ -2564,6 +2565,9 @@ static void __split_huge_page(struct page *page, struct list_head *list,
 		xa_lock(&swap_cache->i_pages);
 	}
 
Quoted 9 lines

mm/huge_memory.c static void __split_huge_page(struct page *page, struct list_head *list, · line 2573

Quoted 6 lines
mm/huge_memory.c +3 -0
+	/* Lru list would be changed, don't care head's LRU bit. */
+	spin_lock_irqsave(&pgdat->lru_lock, flags);
+
 	for (i = HPAGE_PMD_NR - 1; i >= 1; i--) {
 		__split_huge_page_tail(head, i, lruvec, list);
 		/* Some pages can be beyond i_size: drop them from page cache */

You change locking order WRT i_pages lock. Is it safe?

Thanks Kirill,

I think so. and lock_dep/proving has no complain.

Any problem addressed?

Alex

#11 lore raw

PATCH v9 7/20 mm/lru: introduce TestClearPageLRU

In reply to Alex Shi

Combined PageLRU check and ClearPageLRU into one function by new introduced func TestClearPageLRU. This function will be used as page isolation precondition.

No functional change yet.

---
 include/linux/page-flags.h |  1 +
 mm/memcontrol.c            |  4 ++--
 mm/mlock.c                 |  3 +--
 mm/swap.c                  |  8 ++------
 mm/vmscan.c                | 19 +++++++++----------
 5 files changed, 15 insertions(+), 20 deletions(-)
include/linux/page-flags.h +1 -0
index 1bf83c8fcaa7..5cb155f3191e 100644
@@ -318,6 +318,7 @@ static inline void page_init_poison(struct page *page, size_t size)
 PAGEFLAG(Dirty, dirty, PF_HEAD) TESTSCFLAG(Dirty, dirty, PF_HEAD)
 	__CLEARPAGEFLAG(Dirty, dirty, PF_HEAD)
 PAGEFLAG(LRU, lru, PF_HEAD) __CLEARPAGEFLAG(LRU, lru, PF_HEAD)
+	TESTCLEARFLAG(LRU, lru, PF_HEAD)
 PAGEFLAG(Active, active, PF_HEAD) __CLEARPAGEFLAG(Active, active, PF_HEAD)
 	TESTCLEARFLAG(Active, active, PF_HEAD)
 PAGEFLAG(Workingset, workingset, PF_HEAD)
mm/memcontrol.c +2 -2
index 875e2aebcde7..f8419f3436a8 100644
@@ -2588,9 +2588,8 @@ static void commit_charge(struct page *page, struct mem_cgroup *memcg,
 		pgdat = page_pgdat(page);
 		spin_lock_irq(&pgdat->lru_lock);
 
-		if (PageLRU(page)) {
+		if (TestClearPageLRU(page)) {
 			lruvec = mem_cgroup_page_lruvec(page, pgdat);
-			ClearPageLRU(page);
 			del_page_from_lru_list(page, lruvec, page_lru(page));
 		} else
 			spin_unlock_irq(&pgdat->lru_lock);
@@ -2613,6 +2612,7 @@ static void commit_charge(struct page *page, struct mem_cgroup *memcg,
 
 	if (lrucare && lruvec) {
 		lruvec = mem_cgroup_page_lruvec(page, pgdat);
+
 		VM_BUG_ON_PAGE(PageLRU(page), page);
 		SetPageLRU(page);
 		add_page_to_lru_list(page, lruvec, page_lru(page));
mm/mlock.c +1 -2
index a72c1eeded77..03b3a5d99ad7 100644
@@ -108,13 +108,12 @@ void mlock_vma_page(struct page *page)
  */
 static bool __munlock_isolate_lru_page(struct page *page, bool getpage)
 {
-	if (PageLRU(page)) {
+	if (TestClearPageLRU(page)) {
 		struct lruvec *lruvec;
 
 		lruvec = mem_cgroup_page_lruvec(page, page_pgdat(page));
 		if (getpage)
 			get_page(page);
-		ClearPageLRU(page);
 		del_page_from_lru_list(page, lruvec, page_lru(page));
 		return true;
 	}
mm/swap.c +2 -6
index 1ac24fc35d6b..8e71bdd04a1a 100644
@@ -59,15 +59,13 @@
  */
 static void __page_cache_release(struct page *page)
 {
-	if (PageLRU(page)) {
+	if (TestClearPageLRU(page)) {
 		pg_data_t *pgdat = page_pgdat(page);
 		struct lruvec *lruvec;
 		unsigned long flags;
 
 		spin_lock_irqsave(&pgdat->lru_lock, flags);
 		lruvec = mem_cgroup_page_lruvec(page, pgdat);
-		VM_BUG_ON_PAGE(!PageLRU(page), page);
-		__ClearPageLRU(page);
 		del_page_from_lru_list(page, lruvec, page_off_lru(page));
 		spin_unlock_irqrestore(&pgdat->lru_lock, flags);
 	}
@@ -831,7 +829,7 @@ void release_pages(struct page **pages, int nr)
 			continue;
 		}
 
-		if (PageLRU(page)) {
+		if (TestClearPageLRU(page)) {
 			struct pglist_data *pgdat = page_pgdat(page);
 
 			if (pgdat != locked_pgdat) {
@@ -844,8 +842,6 @@ void release_pages(struct page **pages, int nr)
 			}
 
 			lruvec = mem_cgroup_page_lruvec(page, locked_pgdat);
-			VM_BUG_ON_PAGE(!PageLRU(page), page);
-			__ClearPageLRU(page);
 			del_page_from_lru_list(page, lruvec, page_off_lru(page));
 		}
 
mm/vmscan.c +9 -10
index dcdd33f65f43..8958454d50fe 100644
@@ -1751,21 +1751,20 @@ int isolate_lru_page(struct page *page)
 	VM_BUG_ON_PAGE(!page_count(page), page);
 	WARN_RATELIMIT(PageTail(page), "trying to isolate tail page");
 
-	if (PageLRU(page)) {
+	get_page(page);
+	if (TestClearPageLRU(page)) {
 		pg_data_t *pgdat = page_pgdat(page);
 		struct lruvec *lruvec;
+		int lru = page_lru(page);
 
-		spin_lock_irq(&pgdat->lru_lock);
 		lruvec = mem_cgroup_page_lruvec(page, pgdat);
-		if (PageLRU(page)) {
-			int lru = page_lru(page);
-			get_page(page);
-			ClearPageLRU(page);
-			del_page_from_lru_list(page, lruvec, lru);
-			ret = 0;
-		}
+		spin_lock_irq(&pgdat->lru_lock);
+		del_page_from_lru_list(page, lruvec, lru);
 		spin_unlock_irq(&pgdat->lru_lock);
-	}
+		ret = 0;
+	} else
+		put_page(page);
+
 	return ret;
 }
 
Signature · 1 line
1.8.3.1
maintainer
#12 lore raw

Re: [PATCH v9 07/20] mm/lru: introduce TestClearPageLRU

Quoted 11 lines · On Mon, 2 Mar 2020 19:00:17 +0800 Alex Shi <[email protected]> wrote:

Combined PageLRU check and ClearPageLRU into one function by new introduced func TestClearPageLRU. This function will be used as page isolation precondition.

No functional change yet.

...

mm/memcontrol.c static void commit_charge(struct page *page, struct mem_cgroup *memcg, · line 2594

Quoted 9 lines
mm/memcontrol.c +1 -2
@@ -2588,9 +2588,8 @@ static void commit_charge(struct page *page, struct mem_cgroup *memcg,
 		pgdat = page_pgdat(page);
 		spin_lock_irq(&pgdat->lru_lock);
 
-		if (PageLRU(page)) {
+		if (TestClearPageLRU(page)) {
 			lruvec = mem_cgroup_page_lruvec(page, pgdat);
-			ClearPageLRU(page);
 			del_page_from_lru_list(page, lruvec, page_lru(page));
 		} else

The code will now get exclusive access of the page->flags cacheline and will dirty that cacheline, even for !PageLRU() pages. What is the performance impact of this?

#13 lore raw

Re: [PATCH v9 07/20] mm/lru: introduce TestClearPageLRU

ÔÚ 2020/3/3 ÉÏÎç6:11, Andrew Morton дµÀ:

Quoted 11 lines

mm/memcontrol.c static void commit_charge(struct page *page, struct mem_cgroup *memcg, · line 2594

Quoted 6 lines
mm/memcontrol.c +1 -2
-		if (PageLRU(page)) {
+		if (TestClearPageLRU(page)) {
 			lruvec = mem_cgroup_page_lruvec(page, pgdat);
-			ClearPageLRU(page);
 			del_page_from_lru_list(page, lruvec, page_lru(page));
 		} else

The code will now get exclusive access of the page->flags cacheline and will dirty that cacheline, even for !PageLRU() pages. What is the performance impact of this?

Hi Andrew,

Thanks a lot for comments!

I was tested the whole patchset with fengguang's case-lru-file-readtwice
https://git.kernel.org/pub/scm/linux/kernel/git/wfg/vm-scalability.git/
which is most sensitive case on PageLRU I found. There are no clear performance drop.

On this single patch, I just test the same case again, there is still no perf drop. some data is here on my 96 threads machine:

no lock_dep w lock_dep and few other debug option
w this patch, 50.96MB/s 32.93MB/s
w/o this patch, 50.50MB/s 33.53MB/s

And also no any warning from Intel 0day yet.

Thanks a lot!
Alex

maintainer
#14 lore raw

Re: [PATCH v9 07/20] mm/lru: introduce TestClearPageLRU

Quoted 24 lines · On Tue, 3 Mar 2020 12:11:34 +0800 Alex Shi…
Quoted 11 lines · Andrew Morton

mm/memcontrol.c static void commit_charge(struct page *page, struct mem_cgroup *memcg, · line 2594

Quoted 6 lines
mm/memcontrol.c +1 -2
-		if (PageLRU(page)) {
+		if (TestClearPageLRU(page)) {
 			lruvec = mem_cgroup_page_lruvec(page, pgdat);
-			ClearPageLRU(page);
 			del_page_from_lru_list(page, lruvec, page_lru(page));
 		} else

The code will now get exclusive access of the page->flags cacheline and will dirty that cacheline, even for !PageLRU() pages. What is the performance impact of this?

Hi Andrew,

Thanks a lot for comments!

I was tested the whole patchset with fengguang's case-lru-file-readtwice
https://git.kernel.org/pub/scm/linux/kernel/git/wfg/vm-scalability.git/
which is most sensitive case on PageLRU I found. There are no clear performance drop.

Quoted 8 lines

On this single patch, I just test the same case again, there is still no perf drop. some data is here on my 96 threads machine:

no lock_dep w lock_dep and few other debug option
w this patch, 50.96MB/s 32.93MB/s
w/o this patch, 50.50MB/s 33.53MB/s

Well, any difference would be small and the numbers did get a bit lower, albeit probably within the margin of error.

But you know, if someone were to send a patch which micro-optimized some code by replacing 'TestClearXXX()' with `if PageXXX() ClearPageXXX()', I would happily merge it!

Is this change essential to the overall patchset? If not, I'd be inclined to skip it?

#15 lore raw

Re: [PATCH v9 07/20] mm/lru: introduce TestClearPageLRU

Quoted 11 lines · Andrew Morton

Well, any difference would be small and the numbers did get a bit lower, albeit probably within the margin of error.

But you know, if someone were to send a patch which micro-optimized some code by replacing 'TestClearXXX()' with `if PageXXX() ClearPageXXX()', I would happily merge it!

Is this change essential to the overall patchset? If not, I'd be inclined to skip it?

Hi Andrew,

Thanks a lot for comments!
Yes, this patch is essential for all next.

Consider the normal memory testing would focus on user page, that probabably with PageLRU.

Fengguang's vm-scalibicase-small-allocs which used a lots vm_area_struct slab, which may got some impact. 0day/Cheng Rong is working on the test. In my roughly testing, it may drop 5% on my 96threads/394GB machine.

Thanks
Alex

#16 lore raw

Re: [PATCH v9 07/20] mm/lru: introduce TestClearPageLRU

Quoted 20 lines · Alex Shi
Quoted 11 lines · Andrew Morton

Well, any difference would be small and the numbers did get a bit lower, albeit probably within the margin of error.

But you know, if someone were to send a patch which micro-optimized some code by replacing 'TestClearXXX()' with `if PageXXX() ClearPageXXX()', I would happily merge it!

Is this change essential to the overall patchset? If not, I'd be inclined to skip it?

Hi Andrew,

Thanks a lot for comments!
Yes, this patch is essential for all next.

Quoted 8 lines

Consider the normal memory testing would focus on user page, that probabably with PageLRU.

Fengguang's vm-scalibicase-small-allocs which used a lots vm_area_struct slab, which may got some impact. 0day/Cheng Rong is working on the test. In my roughly testing, it may drop 5% on my 96threads/394GB machine.

Thanks
Alex

Hi,

We only tested one case and found a slight regression of vm-scalability.median from this patch set:

Test case: small allocs
=========================================================================================
compiler/cpufreq_governor/kconfig/rootfs/runtime/tbox_group/test/testcase/ucode:
gcc-7/performance/x86_64-rhel-7.6/debian-x86_64-20191114.cgz/300s/lkp-ivb-d02/small-allocs/vm-scalability/0x21

commit:
v5.6-rc4
f71ed0f653e9dbd57347f6321e36556004a17b52

        v5.6-rc4 f71ed0f653e9dbd57347f6321e3 
---------------- --------------------------- 
         %stddev     %change         %stddev
             \          |                \  
    998930            -1.4%     984729        vm-scalability.median

=========================================================================================
compiler/cpufreq_governor/kconfig/rootfs/runtime/tbox_group/test/testcase/ucode:
gcc-7/performance/x86_64-rhel-7.6/debian-x86_64-20191114.cgz/300s/lkp-csl-2ap4/small-allocs/vm-scalability/0x500002c

commit:
v5.6-rc4
f71ed0f653e9dbd57347f6321e36556004a17b52

        v5.6-rc4 f71ed0f653e9dbd57347f6321e3 
---------------- --------------------------- 
         %stddev     %change         %stddev
             \          |                \  
     64040            -2.2%      62641        vm-scalability.median
  12294118            -2.2%   12027483        vm-scalability.throughput
 3.695e+09            -2.2%  3.614e+09        vm-scalability.workload
$ git log --oneline v5.6-rc4..f71ed0f653e9dbd57347f6321e36556004a17b52
f71ed0f653e9d mm/memcg: add debug checking in lock_page_memcg
c56d782a737a5 mm/lru: add debug checking for page memcg moving
40f9438e4f7a9 mm/lru: revise the comments of lru_lock
cf4d433ab1f59 mm/pgdat: remove pgdat lru_lock
8b45216bf602c mm/swap: only change the lru_lock iff page's lruvec is different
9aeff27f856c4 mm/mlock: optimize munlock_pagevec by relocking
0fd16f50f4260 mm/lru: introduce the relock_page_lruvec function
e8bcc2440b133 mm/lru: replace pgdat lru_lock with lruvec lock
88013de2d9cfa mm/mlock: clean up __munlock_isolate_lru_page
037f0e01cc3a3 mm/memcg: move SetPageLRU out of lru_lock in commit_charge
5f889edacd91d mm/lru: take PageLRU first in moving page between lru lists
06998f054a82b mm/mlock: ClearPageLRU before get lru lock in munlock page isolation
5212e3636eed6 mm/lru: add page isolation precondition in __isolate_lru_page
a7b8b29f36b13 mm/lru: introduce TestClearPageLRU
f27e8da1e2fa1 mm/thp: narrow lru locking
2deca0177d843 mm/thp: clean up lru_add_page_tail
afbe030c47e06 mm/thp: move lru_add_page_tail func to huge_memory.c
9bee24913b538 mm/page_idle: no unlikely double check for idle page counting
40def76b96d7b mm/memcg: fold lock_page_lru into commit_charge
c1199696673c2 mm/vmscan: remove unnecessary lruvec adding

Best Regards,
Rong Chen

#17 lore raw

Re: [PATCH v9 07/20] mm/lru: introduce TestClearPageLRU

Quoted 13 lines · Rong Chen
Quoted 5 lines

Hi Andrew,

Thanks a lot for comments!
Yes, this patch is essential for all next.

Quoted 8 lines

Consider the normal memory testing would focus on user page, that probabably with PageLRU.

Fengguang's vm-scalibicase-small-allocs which used a lots vm_area_struct slab, which may got some impact. 0day/Cheng Rong is working on the test. In my roughly testing, it may drop 5% on my 96threads/394GB machine.

Thanks
Alex

Quoted 8 lines

Hi,

We only tested one case and found a slight regression of vm-scalability.median from this patch set:

Test case: small allocs
=========================================================================================
compiler/cpufreq_governor/kconfig/rootfs/runtime/tbox_group/test/testcase/ucode:
gcc-7/performance/x86_64-rhel-7.6/debian-x86_64-20191114.cgz/300s/lkp-ivb-d02/small-allocs/vm-scalability/0x21

It's a very acceptable result!

Thanks a lot for so quick testing! I believe your results would be far more stable than me. :)

(My testing show quit different result in 2 reboot(1.3% or 6.6% drop). Maybe sth wrong for me in this case.)

Thanks for your report!
Alex

#18 lore raw

PATCH v9 8/20 mm/lru: add page isolation precondition in __isolate_lru_page

In reply to Alex Shi

Johannes Weiner has suggested:
"
So here is a crazy idea that may be worth exploring:

Right now, pgdat->lru_lock protects both PageLRU *and* the lruvec's linked list.

Can we make PageLRU atomic and use it to stabilize the lru_lock instead, and then use the lru_lock only serialize list operations?
...
"

Yes, this patch is doing so on __isolate_lru_page which is the core page isolation func in compaction and shrinking path.

This patch move clear page lru action before compaction getting lru_lock, makes it as a necessary condition for page isolation. Hence, PageLRU may be cleared druing shrink_inactive_list path for isolation reason. If so, we can skip that page's in reclaim.

It's a preparation for later per memcg lru_lock change.

---
 include/linux/swap.h |  2 +-
 mm/compaction.c      | 25 +++++++++++++++++--------
 mm/vmscan.c          | 48 ++++++++++++++++++++++++++----------------------
 3 files changed, 44 insertions(+), 31 deletions(-)
include/linux/swap.h +1 -1
index c555e8f161ad..69f0794f1da3 100644
@@ -351,7 +351,7 @@ extern void lru_cache_add_active_or_unevictable(struct page *page,
 extern unsigned long zone_reclaimable_pages(struct zone *zone);
 extern unsigned long try_to_free_pages(struct zonelist *zonelist, int order,
 					gfp_t gfp_mask, nodemask_t *mask);
-extern int __isolate_lru_page(struct page *page, isolate_mode_t mode);
+extern int __isolate_lru_page_prepare(struct page *page, isolate_mode_t mode);
 extern unsigned long try_to_free_mem_cgroup_pages(struct mem_cgroup *memcg,
 						  unsigned long nr_pages,
 						  gfp_t gfp_mask,
mm/compaction.c +17 -8
index 672d3c78c6ab..1baba328d089 100644
@@ -948,6 +948,23 @@ static bool too_many_isolated(pg_data_t *pgdat)
 		if (!(cc->gfp_mask & __GFP_FS) && page_mapping(page))
 			goto isolate_fail;
 
+		if (__isolate_lru_page_prepare(page, isolate_mode) != 0)
+			goto isolate_fail;
+
+		/*
+		 * Be careful not to clear PageLRU until after we're
+		 * sure the page is not being freed elsewhere -- the
+		 * page release code relies on it.
+		 */
+		if (unlikely(!get_page_unless_zero(page)))
+			goto isolate_fail;
+
+		/* Try isolate the page */
+		if (!TestClearPageLRU(page)) {
+			put_page(page);
+			goto isolate_fail;
+		}
+
 		/* If we already hold the lock, we can skip some rechecking */
 		if (!locked) {
 			locked = compact_lock_irqsave(&pgdat->lru_lock,
@@ -960,10 +977,6 @@ static bool too_many_isolated(pg_data_t *pgdat)
 					goto isolate_abort;
 			}
 
-			/* Recheck PageLRU and PageCompound under lock */
-			if (!PageLRU(page))
-				goto isolate_fail;
-
 			/*
 			 * Page become compound since the non-locked check,
 			 * and it's on LRU. It can only be a THP so the order
@@ -977,10 +990,6 @@ static bool too_many_isolated(pg_data_t *pgdat)
 
 		lruvec = mem_cgroup_page_lruvec(page, pgdat);
 
-		/* Try isolate the page */
-		if (__isolate_lru_page(page, isolate_mode) != 0)
-			goto isolate_fail;
-
 		VM_BUG_ON_PAGE(PageCompound(page), page);
 
 		/* Successfully isolated */
mm/vmscan.c +26 -22

91 lines not on this page · raw message

Signature · 1 line
1.8.3.1
#19 lore raw

PATCH v9 9/20 mm/mlock: ClearPageLRU before get lru lock in munlock page isolation

In reply to Alex Shi

This is one of effort to split the PageLRU clear from old page isolation.

This patch move the lru_lock after TestClearPageLRU, which takes holding PageLRU as precondition for page isolation, as a preparation for later lru_lock replacment. So we have to unfold __munlock_isolate_lru_page.

__split_huge_page_refcount doesn't exist, but we still have to guard PageMlocked in __split_huge_page_tail. That make code ugly. Anyway we still remove 2 gotos.

[[email protected]: found a sleeping function bug ... at mm/rmap.c:1861]
Cc Kirill A. Shutemov <[email protected]>
Cc Andrew Morton <akpm-de/[email protected]>
Cc Johannes Weiner <[email protected]>
Cc Matthew Wilcox <[email protected]>
Cc Hugh Dickins <[email protected]>
---
 mm/mlock.c | 35 +++++++++++++++++++++++------------
 1 file changed, 23 insertions(+), 12 deletions(-)
mm/mlock.c +23 -12
index 03b3a5d99ad7..7ddc52ca14b1 100644
@@ -181,6 +181,7 @@ static void __munlock_isolation_failed(struct page *page)
 unsigned int munlock_vma_page(struct page *page)
 {
 	int nr_pages;
+	bool clearlru = false;
 	pg_data_t *pgdat = page_pgdat(page);
 
 	/* For try_to_munlock() and to serialize with page migration */
@@ -189,32 +190,42 @@ unsigned int munlock_vma_page(struct page *page)
 	VM_BUG_ON_PAGE(PageTail(page), page);
 
 	/*
-	 * Serialize with any parallel __split_huge_page_refcount() which
+	 * Serialize with any parallel __split_huge_page_tail() which
 	 * might otherwise copy PageMlocked to part of the tail pages before
 	 * we clear it in the head page. It also stabilizes hpage_nr_pages().
 	 */
+	get_page(page);
+	clearlru = TestClearPageLRU(page);
 	spin_lock_irq(&pgdat->lru_lock);
 
 	if (!TestClearPageMlocked(page)) {
-		/* Potentially, PTE-mapped THP: do not skip the rest PTEs */
-		nr_pages = 1;
-		goto unlock_out;
+		if (clearlru)
+			SetPageLRU(page);
+		/*
+		 * Potentially, PTE-mapped THP: do not skip the rest PTEs
+		 * Reuse lock as memory barrier for release_pages racing.
+		 */
+		spin_unlock_irq(&pgdat->lru_lock);
+		put_page(page);
+		return 0;
 	}
 
 	nr_pages = hpage_nr_pages(page);
 	__mod_zone_page_state(page_zone(page), NR_MLOCK, -nr_pages);
 
-	if (__munlock_isolate_lru_page(page, true)) {
+	if (clearlru) {
+		struct lruvec *lruvec;
+
+		lruvec = mem_cgroup_page_lruvec(page, page_pgdat(page));
+		del_page_from_lru_list(page, lruvec, page_lru(page));
 		spin_unlock_irq(&pgdat->lru_lock);
 		__munlock_isolated_page(page);
-		goto out;
+	} else {
+		spin_unlock_irq(&pgdat->lru_lock);
+		put_page(page);
+		__munlock_isolation_failed(page);
 	}
-	__munlock_isolation_failed(page);
-
-unlock_out:
-	spin_unlock_irq(&pgdat->lru_lock);
 
-out:
 	return nr_pages - 1;
 }
 
@@ -323,8 +334,8 @@ static void __munlock_pagevec(struct pagevec *pvec, struct zone *zone)
 		pagevec_add(&pvec_putback, pvec->pages[i]);
 		pvec->pages[i] = NULL;
 	}
-	__mod_zone_page_state(zone, NR_MLOCK, delta_munlocked);
 	spin_unlock_irq(&zone->zone_pgdat->lru_lock);
+	__mod_zone_page_state(zone, NR_MLOCK, delta_munlocked);
 
 	/* Now we can release pins of pages that we are not munlocking */
 	pagevec_release(&pvec_putback);
Signature · 1 line
1.8.3.1
#20 lore raw

PATCH v9 10/20 mm/lru: take PageLRU first in moving page between lru lists

In reply to Alex Shi

Current move_fn do moving with PageLRU in lru_lock protection. Moving include a lru isolation and a lru adding. As to the isolation part, we need take PageLRU before move_fn, that add a extra PageLRU guard to block other isolations. and set lru bit back after page settled on lru list.

This makes TestClearPageLRU as isolation's necessary condition in page moving between lru lists.

Another page moving between lru lists is check_move_unevictable_pages func, we need to take PageLRu temporarilly same as move_fn.

Cc Andrew Morton <[email protected]>
Cc Johannes Weiner <[email protected]>
Cc Matthew Wilcox <[email protected]>
Cc Hugh Dickins <[email protected]>
---
 mm/swap.c   | 42 ++++++++++++++++++++++++------------------
 mm/vmscan.c |  3 ++-
 2 files changed, 26 insertions(+), 19 deletions(-)
mm/swap.c +24 -18
index 8e71bdd04a1a..16af7c8369fe 100644
@@ -187,7 +187,7 @@ int get_kernel_page(unsigned long start, int write, struct page **pages)
 
 static void pagevec_lru_move_fn(struct pagevec *pvec,
 	void (*move_fn)(struct page *page, struct lruvec *lruvec, void *arg),
-	void *arg)
+	void *arg, bool isolation)
 {
 	int i;
 	struct pglist_data *pgdat = NULL;
@@ -198,6 +198,10 @@ static void pagevec_lru_move_fn(struct pagevec *pvec,
 		struct page *page = pvec->pages[i];
 		struct pglist_data *pagepgdat = page_pgdat(page);
 
+		if (isolation && !TestClearPageLRU(page))
+			continue;
+
+		/* every page should be isolated from lru */
 		if (pagepgdat != pgdat) {
 			if (pgdat)
 				spin_unlock_irqrestore(&pgdat->lru_lock, flags);
@@ -207,6 +211,9 @@ static void pagevec_lru_move_fn(struct pagevec *pvec,
 
 		lruvec = mem_cgroup_page_lruvec(page, pgdat);
 		(*move_fn)(page, lruvec, arg);
+
+		if (isolation)
+			SetPageLRU(page);
 	}
 	if (pgdat)
 		spin_unlock_irqrestore(&pgdat->lru_lock, flags);
@@ -219,7 +226,7 @@ static void pagevec_move_tail_fn(struct page *page, struct lruvec *lruvec,
 {
 	int *pgmoved = arg;
 
-	if (PageLRU(page) && !PageUnevictable(page)) {
+	if (!PageUnevictable(page)) {
 		del_page_from_lru_list(page, lruvec, page_lru(page));
 		ClearPageActive(page);
 		add_page_to_lru_list_tail(page, lruvec, page_lru(page));
@@ -235,7 +242,7 @@ static void pagevec_move_tail(struct pagevec *pvec)
 {
 	int pgmoved = 0;
 
-	pagevec_lru_move_fn(pvec, pagevec_move_tail_fn, &pgmoved);
+	pagevec_lru_move_fn(pvec, pagevec_move_tail_fn, &pgmoved, true);
 	__count_vm_events(PGROTATED, pgmoved);
 }
 
@@ -272,7 +279,7 @@ void update_page_reclaim_stat(struct lruvec *lruvec, int file, int rotated)
 static void __activate_page(struct page *page, struct lruvec *lruvec,
 			    void *arg)
 {
-	if (PageLRU(page) && !PageActive(page) && !PageUnevictable(page)) {
+	if (!PageActive(page) && !PageUnevictable(page)) {
 		int file = page_is_file_cache(page);
 		int lru = page_lru_base_type(page);
 
@@ -293,7 +300,7 @@ static void activate_page_drain(int cpu)
 	struct pagevec *pvec = &per_cpu(activate_page_pvecs, cpu);
 
 	if (pagevec_count(pvec))
-		pagevec_lru_move_fn(pvec, __activate_page, NULL);
+		pagevec_lru_move_fn(pvec, __activate_page, NULL, true);
 }
 
 static bool need_activate_page_drain(int cpu)
@@ -309,7 +316,7 @@ void activate_page(struct page *page)
 
 		get_page(page);
 		if (!pagevec_add(pvec, page) || PageCompound(page))
-			pagevec_lru_move_fn(pvec, __activate_page, NULL);
+			pagevec_lru_move_fn(pvec, __activate_page, NULL, true);
 		put_cpu_var(activate_page_pvecs);
 	}
 }
@@ -501,9 +508,6 @@ static void lru_deactivate_file_fn(struct page *page, struct lruvec *lruvec,
 	int lru, file;
 	bool active;
 
-	if (!PageLRU(page))
-		return;
-
 	if (PageUnevictable(page))
 		return;
 
@@ -544,7 +548,7 @@ static void lru_deactivate_file_fn(struct page *page, struct lruvec *lruvec,
 static void lru_deactivate_fn(struct page *page, struct lruvec *lruvec,
 			    void *arg)
 {
-	if (PageLRU(page) && PageActive(page) && !PageUnevictable(page)) {
+	if (PageActive(page) && !PageUnevictable(page)) {
 		int file = page_is_file_cache(page);
 		int lru = page_lru_base_type(page);
 
@@ -561,7 +565,7 @@ static void lru_deactivate_fn(struct page *page, struct lruvec *lruvec,
 static void lru_lazyfree_fn(struct page *page, struct lruvec *lruvec,
 			    void *arg)
 {
-	if (PageLRU(page) && PageAnon(page) && PageSwapBacked(page) &&
+	if (PageAnon(page) && PageSwapBacked(page) &&
 	    !PageSwapCache(page) && !PageUnevictable(page)) {
 		bool active = PageActive(page);
 
@@ -607,15 +611,15 @@ void lru_add_drain_cpu(int cpu)
 
 	pvec = &per_cpu(lru_deactivate_file_pvecs, cpu);
 	if (pagevec_count(pvec))
-		pagevec_lru_move_fn(pvec, lru_deactivate_file_fn, NULL);
+		pagevec_lru_move_fn(pvec, lru_deactivate_file_fn, NULL, true);
 
 	pvec = &per_cpu(lru_deactivate_pvecs, cpu);
 	if (pagevec_count(pvec))
-		pagevec_lru_move_fn(pvec, lru_deactivate_fn, NULL);
+		pagevec_lru_move_fn(pvec, lru_deactivate_fn, NULL, true);
 
 	pvec = &per_cpu(lru_lazyfree_pvecs, cpu);
 	if (pagevec_count(pvec))
-		pagevec_lru_move_fn(pvec, lru_lazyfree_fn, NULL);
+		pagevec_lru_move_fn(pvec, lru_lazyfree_fn, NULL, true);
 
 	activate_page_drain(cpu);
 }
@@ -641,7 +645,8 @@ void deactivate_file_page(struct page *page)
 		struct pagevec *pvec = &get_cpu_var(lru_deactivate_file_pvecs);
 
 		if (!pagevec_add(pvec, page) || PageCompound(page))
-			pagevec_lru_move_fn(pvec, lru_deactivate_file_fn, NULL);
+			pagevec_lru_move_fn(pvec,
+					lru_deactivate_file_fn, NULL, true);
 		put_cpu_var(lru_deactivate_file_pvecs);
 	}
 }
@@ -661,7 +666,8 @@ void deactivate_page(struct page *page)
 
 		get_page(page);
 		if (!pagevec_add(pvec, page) || PageCompound(page))
-			pagevec_lru_move_fn(pvec, lru_deactivate_fn, NULL);
+			pagevec_lru_move_fn(pvec,
+					lru_deactivate_fn, NULL, true);
 		put_cpu_var(lru_deactivate_pvecs);
 	}
 }
@@ -681,7 +687,7 @@ void mark_page_lazyfree(struct page *page)
 
 		get_page(page);
 		if (!pagevec_add(pvec, page) || PageCompound(page))
-			pagevec_lru_move_fn(pvec, lru_lazyfree_fn, NULL);
+			pagevec_lru_move_fn(pvec, lru_lazyfree_fn, NULL, true);
 		put_cpu_var(lru_lazyfree_pvecs);
 	}
 }
@@ -941,7 +947,7 @@ static void __pagevec_lru_add_fn(struct page *page, struct lruvec *lruvec,
  */
 void __pagevec_lru_add(struct pagevec *pvec)
 {
-	pagevec_lru_move_fn(pvec, __pagevec_lru_add_fn, NULL);
+	pagevec_lru_move_fn(pvec, __pagevec_lru_add_fn, NULL, false);
 }
 EXPORT_SYMBOL(__pagevec_lru_add);
 
mm/vmscan.c +2 -1
index bc2ec3fe4f48..efaa4f41044e 100644
@@ -4343,7 +4343,7 @@ void check_move_unevictable_pages(struct pagevec *pvec)
 		}
 		lruvec = mem_cgroup_page_lruvec(page, pgdat);
 
-		if (!PageLRU(page) || !PageUnevictable(page))
+		if (!TestClearPageLRU(page) || !PageUnevictable(page))
 			continue;
 
 		if (page_evictable(page)) {
@@ -4354,6 +4354,7 @@ void check_move_unevictable_pages(struct pagevec *pvec)
 			del_page_from_lru_list(page, lruvec, LRU_UNEVICTABLE);
 			add_page_to_lru_list(page, lruvec, lru);
 			pgrescued++;
+			SetPageLRU(page);
 		}
 	}
 
Signature · 1 line
1.8.3.1
#21 lore raw

PATCH v9 11/20 mm/memcg: move SetPageLRU out of lru_lock in commit_charge

In reply to Alex Shi

Since lru lock doesn't defense PageLRU anymore, move the setting out of lock may save a bit lock contention time.

---
 mm/memcontrol.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
mm/memcontrol.c +1 -1
index f8419f3436a8..7d7b861a948c 100644
@@ -2614,9 +2614,9 @@ static void commit_charge(struct page *page, struct mem_cgroup *memcg,
 		lruvec = mem_cgroup_page_lruvec(page, pgdat);
 
 		VM_BUG_ON_PAGE(PageLRU(page), page);
-		SetPageLRU(page);
 		add_page_to_lru_list(page, lruvec, page_lru(page));
 		spin_unlock_irq(&pgdat->lru_lock);
+		SetPageLRU(page);
 	}
 }
 
Signature · 1 line
1.8.3.1
#22 lore raw

PATCH v9 12/20 mm/mlock: clean up __munlock_isolate_lru_page

In reply to Alex Shi

clean up __munlock_isolate_lru_page func for later lru lock change. No functional change.

---
 mm/mlock.c | 47 ++++++++++++++++++-----------------------------
 1 file changed, 18 insertions(+), 29 deletions(-)
mm/mlock.c +18 -29
index 7ddc52ca14b1..a43b3da78541 100644
@@ -103,25 +103,6 @@ void mlock_vma_page(struct page *page)
 }
 
 /*
- * Isolate a page from LRU with optional get_page() pin.
- * Assumes lru_lock already held and page already pinned.
- */
-static bool __munlock_isolate_lru_page(struct page *page, bool getpage)
-{
-	if (TestClearPageLRU(page)) {
-		struct lruvec *lruvec;
-
-		lruvec = mem_cgroup_page_lruvec(page, page_pgdat(page));
-		if (getpage)
-			get_page(page);
-		del_page_from_lru_list(page, lruvec, page_lru(page));
-		return true;
-	}
-
-	return false;
-}
-
-/*
  * Finish munlock after successful page isolation
  *
  * Page must be locked. This is a wrapper for try_to_munlock()
@@ -311,26 +292,34 @@ static void __munlock_pagevec(struct pagevec *pvec, struct zone *zone)
 	spin_lock_irq(&zone->zone_pgdat->lru_lock);
 	for (i = 0; i < nr; i++) {
 		struct page *page = pvec->pages[i];
+		struct lruvec *lruvec;
 
-		if (TestClearPageMlocked(page)) {
-			/*
-			 * We already have pin from follow_page_mask()
-			 * so we can spare the get_page() here.
-			 */
-			if (__munlock_isolate_lru_page(page, false))
-				continue;
-			else
-				__munlock_isolation_failed(page);
-		} else {
+		if (!TestClearPageMlocked(page)) {
 			delta_munlocked++;
+			goto putback;
+		}
+
+		if (!TestClearPageLRU(page)) {
+			__munlock_isolation_failed(page);
+			goto putback;
 		}
 
 		/*
+		 * Isolate this page.
+		 * We already have pin from follow_page_mask()
+		 * so we can spare the get_page() here.
+		 */
+		lruvec = mem_cgroup_page_lruvec(page, page_pgdat(page));
+		del_page_from_lru_list(page, lruvec, page_lru(page));
+		continue;
+
+		/*
 		 * We won't be munlocking this page in the next phase
 		 * but we still need to release the follow_page_mask()
 		 * pin. We cannot do it under lru_lock however. If it's
 		 * the last pin, __page_cache_release() would deadlock.
 		 */
+putback:
 		pagevec_add(&pvec_putback, pvec->pages[i]);
 		pvec->pages[i] = NULL;
 	}
Signature · 1 line
1.8.3.1
#23 lore raw

PATCH v9 13/20 mm/lru: replace pgdat lru_lock with lruvec lock

In reply to Alex Shi

This patch moves lru_lock into lruvec, give a lru_lock for each of lruvec, thus bring a lru_lock for each of memcg per node. So on a large node machine, each of memcg don't need suffer from per node pgdat->lru_lock waiting. They could go fast with their self lru_lock.

We introduces function lock_page_lruvec, which will lock the page's memcg's lruvec->lru_lock. (Thanks Johannes Weiner, Hugh Dickins and Konstantin Khlebnikov suggestion/reminder during patch writting.)

This is the key patch to replace per node lru_lock with per memcg lruvec lock, with few palce full back more frequency lru locking. But the later patch will use relock_page_lruvec_xxx to fill up the gap.

According to Daniel Jordan's suggestion, I run 208 'dd' with on 104 containers on a 2s * 26cores * HT box with a modefied case:
https://git.kernel.org/pub/scm/linux/kernel/git/wfg/vm-scalability.git/tree/case-lru-file-readtwice

With this and later patches, the readtwice performance increases about 80% within concurrent containers.

Cc Andrew Morton <akpm-de/[email protected]>
Cc Johannes Weiner <[email protected]>
Cc Michal Hocko <[email protected]>
Cc Vladimir Davydov <[email protected]>
Cc Yang Shi <yang.shi-KPsoFbNs7GizrGE5bRqYAgC/[email protected]>
Cc Matthew Wilcox <[email protected]>
Cc Konstantin Khlebnikov <[email protected]>
Cc Hugh Dickins <[email protected]>
Cc Tejun Heo <[email protected]>
---
 include/linux/memcontrol.h | 35 ++++++++++++++++++++++
 include/linux/mmzone.h     |  2 ++
 mm/compaction.c            | 51 ++++++++++++++++++++------------
 mm/huge_memory.c           |  9 ++----
 mm/memcontrol.c            | 50 +++++++++++++++++++++++++-------
 mm/mlock.c                 | 18 +++++-------
 mm/mmzone.c                |  1 +
 mm/swap.c                  | 72 +++++++++++++++++-----------------------------
 mm/vmscan.c                | 57 ++++++++++++++++--------------------
 9 files changed, 170 insertions(+), 125 deletions(-)
include/linux/memcontrol.h +35 -0
index a7a0a1a5c8d5..b8a04f0a2ab8 100644
@@ -424,6 +424,13 @@ static inline struct lruvec *mem_cgroup_lruvec(struct mem_cgroup *memcg,
 
 struct mem_cgroup *get_mem_cgroup_from_page(struct page *page);
 
+struct lruvec *lock_page_lruvec_irq(struct page *page);
+struct lruvec *lock_page_lruvec_irqsave(struct page *page,
+						unsigned long *flags);
+
+void unlock_page_lruvec_irq(struct lruvec *lruvec);
+void unlock_page_lruvec_irqrestore(struct lruvec *lruvec, unsigned long flags);
+
 static inline
 struct mem_cgroup *mem_cgroup_from_css(struct cgroup_subsys_state *css){
 	return css ? container_of(css, struct mem_cgroup, css) : NULL;
@@ -926,6 +933,34 @@ static inline void mem_cgroup_put(struct mem_cgroup *memcg)
 {
 }
 
+static inline struct lruvec *lock_page_lruvec_irq(struct page *page)
+{
+	struct pglist_data *pgdat = page_pgdat(page);
+
+	spin_lock_irq(&pgdat->__lruvec.lru_lock);
+	return &pgdat->__lruvec;
+}
+
+static inline struct lruvec *lock_page_lruvec_irqsave(struct page *page,
+		unsigned long *flagsp)
+{
+	struct pglist_data *pgdat = page_pgdat(page);
+
+	spin_lock_irqsave(&pgdat->__lruvec.lru_lock, *flagsp);
+	return &pgdat->__lruvec;
+}
+
+static inline void unlock_page_lruvec_irq(struct lruvec *lruvec)
+{
+	spin_unlock_irq(&lruvec->lru_lock);
+}
+
+static inline void unlock_page_lruvec_irqrestore(struct lruvec *lruvec,
+		unsigned long flags)
+{
+	spin_unlock_irqrestore(&lruvec->lru_lock, flags);
+}
+
 static inline struct mem_cgroup *
 mem_cgroup_iter(struct mem_cgroup *root,
 		struct mem_cgroup *prev,
include/linux/mmzone.h +2 -0
index 462f6873905a..a7bdff94990d 100644
@@ -308,6 +308,8 @@ struct lruvec {
 	atomic_long_t			inactive_age;
 	/* Refaults at the time of last reclaim cycle */
 	unsigned long			refaults;
+	/* per lruvec lru_lock for memcg */
+	spinlock_t			lru_lock;
 	/* Various lruvec state flags (enum lruvec_flags) */
 	unsigned long			flags;
 #ifdef CONFIG_MEMCG
mm/compaction.c +32 -19

94 lines not on this page · raw message

mm/huge_memory.c +3 -6
index 3835f87d03fd..7af56d0fb044 100644
@@ -2452,7 +2452,7 @@ void lru_add_page_tail(struct page *head, struct page *page_tail,
 
 	VM_BUG_ON_PAGE(PageCompound(page_tail), head);
 	VM_BUG_ON_PAGE(PageLRU(page_tail), head);
-	lockdep_assert_held(&lruvec_pgdat(lruvec)->lru_lock);
+	lockdep_assert_held(&lruvec->lru_lock);
 
 	if (!list)
 		SetPageLRU(page_tail);
@@ -2545,15 +2545,12 @@ static void __split_huge_page(struct page *page, struct list_head *list,
 				pgoff_t end)
 {
 	struct page *head = compound_head(page);
-	pg_data_t *pgdat = page_pgdat(head);
 	struct lruvec *lruvec;
 	struct address_space *swap_cache = NULL;
 	unsigned long offset = 0;
 	unsigned long flags;
 	int i;
 
-	lruvec = mem_cgroup_page_lruvec(head, pgdat);
-
 	/* complete memcg works before add pages to LRU */
 	mem_cgroup_split_huge_fixup(head);
 
@@ -2566,7 +2563,7 @@ static void __split_huge_page(struct page *page, struct list_head *list,
 	}
 
 	/* Lru list would be changed, don't care head's LRU bit. */
-	spin_lock_irqsave(&pgdat->lru_lock, flags);
+	lruvec = lock_page_lruvec_irqsave(head, &flags);
 
 	for (i = HPAGE_PMD_NR - 1; i >= 1; i--) {
 		__split_huge_page_tail(head, i, lruvec, list);
@@ -2585,7 +2582,7 @@ static void __split_huge_page(struct page *page, struct list_head *list,
 					head + i, 0);
 		}
 	}
-	spin_unlock_irqrestore(&pgdat->lru_lock, flags);
+	unlock_page_lruvec_irqrestore(lruvec, flags);
 
 	ClearPageCompound(head);
 
mm/memcontrol.c +39 -11

91 lines not on this page · raw message

mm/mlock.c +7 -11
index a43b3da78541..d285348b147e 100644
@@ -163,7 +163,7 @@ unsigned int munlock_vma_page(struct page *page)
 {
 	int nr_pages;
 	bool clearlru = false;
-	pg_data_t *pgdat = page_pgdat(page);
+	struct lruvec *lruvec;
 
 	/* For try_to_munlock() and to serialize with page migration */
 	BUG_ON(!PageLocked(page));
@@ -177,7 +177,7 @@ unsigned int munlock_vma_page(struct page *page)
 	 */
 	get_page(page);
 	clearlru = TestClearPageLRU(page);
-	spin_lock_irq(&pgdat->lru_lock);
+	lruvec = lock_page_lruvec_irq(page);
 
 	if (!TestClearPageMlocked(page)) {
 		if (clearlru)
@@ -186,7 +186,7 @@ unsigned int munlock_vma_page(struct page *page)
 		 * Potentially, PTE-mapped THP: do not skip the rest PTEs
 		 * Reuse lock as memory barrier for release_pages racing.
 		 */
-		spin_unlock_irq(&pgdat->lru_lock);
+		unlock_page_lruvec_irq(lruvec);
 		put_page(page);
 		return 0;
 	}
@@ -195,14 +195,11 @@ unsigned int munlock_vma_page(struct page *page)
 	__mod_zone_page_state(page_zone(page), NR_MLOCK, -nr_pages);
 
 	if (clearlru) {
-		struct lruvec *lruvec;
-
-		lruvec = mem_cgroup_page_lruvec(page, page_pgdat(page));
 		del_page_from_lru_list(page, lruvec, page_lru(page));
-		spin_unlock_irq(&pgdat->lru_lock);
+		unlock_page_lruvec_irq(lruvec);
 		__munlock_isolated_page(page);
 	} else {
-		spin_unlock_irq(&pgdat->lru_lock);
+		unlock_page_lruvec_irq(lruvec);
 		put_page(page);
 		__munlock_isolation_failed(page);
 	}
@@ -289,7 +286,6 @@ static void __munlock_pagevec(struct pagevec *pvec, struct zone *zone)
 	pagevec_init(&pvec_putback);
 
 	/* Phase 1: page isolation */
-	spin_lock_irq(&zone->zone_pgdat->lru_lock);
 	for (i = 0; i < nr; i++) {
 		struct page *page = pvec->pages[i];
 		struct lruvec *lruvec;
@@ -309,8 +305,9 @@ static void __munlock_pagevec(struct pagevec *pvec, struct zone *zone)
 		 * We already have pin from follow_page_mask()
 		 * so we can spare the get_page() here.
 		 */
-		lruvec = mem_cgroup_page_lruvec(page, page_pgdat(page));
+		lruvec = lock_page_lruvec_irq(page);
 		del_page_from_lru_list(page, lruvec, page_lru(page));
+		unlock_page_lruvec_irq(lruvec);
 		continue;
 
 		/*
@@ -323,7 +320,6 @@ static void __munlock_pagevec(struct pagevec *pvec, struct zone *zone)
 		pagevec_add(&pvec_putback, pvec->pages[i]);
 		pvec->pages[i] = NULL;
 	}
-	spin_unlock_irq(&zone->zone_pgdat->lru_lock);
 	__mod_zone_page_state(zone, NR_MLOCK, delta_munlocked);
 
 	/* Now we can release pins of pages that we are not munlocking */
mm/mmzone.c +1 -0
index 4686fdc23bb9..3750a90ed4a0 100644
@@ -91,6 +91,7 @@ void lruvec_init(struct lruvec *lruvec)
 	enum lru_list lru;
 
 	memset(lruvec, 0, sizeof(struct lruvec));
+	spin_lock_init(&lruvec->lru_lock);
 
 	for_each_lru(lru)
 		INIT_LIST_HEAD(&lruvec->lists[lru]);
mm/swap.c +27 -45

150 lines not on this page · raw message

mm/vmscan.c +24 -33

183 lines not on this page · raw message

Signature · 1 line
1.8.3.1
#24 lore raw

PATCH v9 14/20 mm/lru: introduce the relock_page_lruvec function

In reply to Alex Shi

During the lruvec locking, a new page's lruvec is may same as previous one. Thus we could save a re-locking, and only change lock iff lruvec is new.

Function named relock_page_lruvec following Hugh Dickins' patch.

Cc Johannes Weiner <[email protected]>
Cc Andrew Morton <akpm-de/[email protected]>
Cc Thomas Gleixner <[email protected]>
Cc Andrey Ryabinin <[email protected]>
Cc Matthew Wilcox <[email protected]>
Cc Mel Gorman <[email protected]>
Cc Konstantin Khlebnikov <[email protected]>
Cc Hugh Dickins <[email protected]>
Cc Tejun Heo <[email protected]>
---
 include/linux/memcontrol.h | 36 ++++++++++++++++++++++++++++++++++++
 mm/vmscan.c                |  8 ++------
 2 files changed, 38 insertions(+), 6 deletions(-)
include/linux/memcontrol.h +36 -0
index b8a04f0a2ab8..f60009580d2a 100644
@@ -1307,6 +1307,42 @@ static inline void dec_lruvec_page_state(struct page *page,
 	mod_lruvec_page_state(page, idx, -1);
 }
 
+/* Don't lock again iff page's lruvec locked */
+static inline struct lruvec *relock_page_lruvec_irq(struct page *page,
+		struct lruvec *locked_lruvec)
+{
+	struct pglist_data *pgdat = page_pgdat(page);
+	struct lruvec *lruvec;
+
+	lruvec = mem_cgroup_page_lruvec(page, pgdat);
+
+	if (likely(locked_lruvec == lruvec))
+		return lruvec;
+
+	if (unlikely(locked_lruvec))
+		unlock_page_lruvec_irq(locked_lruvec);
+
+	return lock_page_lruvec_irq(page);
+}
+
+/* Don't lock again iff page's lruvec locked */
+static inline struct lruvec *relock_page_lruvec_irqsave(struct page *page,
+		struct lruvec *locked_lruvec, unsigned long *flags)
+{
+	struct pglist_data *pgdat = page_pgdat(page);
+	struct lruvec *lruvec;
+
+	lruvec = mem_cgroup_page_lruvec(page, pgdat);
+
+	if (likely(locked_lruvec == lruvec))
+		return lruvec;
+
+	if (unlikely(locked_lruvec))
+		unlock_page_lruvec_irqrestore(locked_lruvec, *flags);
+
+	return lock_page_lruvec_irqsave(page, flags);
+}
+
 #ifdef CONFIG_CGROUP_WRITEBACK
 
 struct wb_domain *mem_cgroup_wb_domain(struct bdi_writeback *wb);
mm/vmscan.c +2 -6
index a58cd5ee9ea1..de925bd629eb 100644
@@ -4325,14 +4325,10 @@ void check_move_unevictable_pages(struct pagevec *pvec)
 
 	for (i = 0; i < pvec->nr; i++) {
 		struct page *page = pvec->pages[i];
-		struct lruvec *new_lruvec = mem_cgroup_page_lruvec(page, page_pgdat(page));
 
 		pgscanned++;
-		if (lruvec != new_lruvec) {
-			if (lruvec)
-				unlock_page_lruvec_irq(lruvec);
-			lruvec = lock_page_lruvec_irq(page);
-		}
+
+		lruvec = relock_page_lruvec_irq(page, lruvec);
 
 		if (!TestClearPageLRU(page) || !PageUnevictable(page))
 			continue;
Signature · 1 line
1.8.3.1
#25 lore raw

PATCH v9 15/20 mm/mlock: optimize munlock_pagevec by relocking

In reply to Alex Shi

During the pagevec locking, a new page's lruvec is may same as previous one. Thus we could save a re-locking, and only change lock iff lruvec is newer.

---
 mm/mlock.c | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)
mm/mlock.c +4 -3
index d285348b147e..236a29b791f4 100644
@@ -281,6 +281,7 @@ static void __munlock_pagevec(struct pagevec *pvec, struct zone *zone)
 	int nr = pagevec_count(pvec);
 	int delta_munlocked = -nr;
 	struct pagevec pvec_putback;
+	struct lruvec *lruvec = NULL;
 	int pgrescued = 0;
 
 	pagevec_init(&pvec_putback);
@@ -288,7 +289,6 @@ static void __munlock_pagevec(struct pagevec *pvec, struct zone *zone)
 	/* Phase 1: page isolation */
 	for (i = 0; i < nr; i++) {
 		struct page *page = pvec->pages[i];
-		struct lruvec *lruvec;
 
 		if (!TestClearPageMlocked(page)) {
 			delta_munlocked++;
@@ -305,9 +305,8 @@ static void __munlock_pagevec(struct pagevec *pvec, struct zone *zone)
 		 * We already have pin from follow_page_mask()
 		 * so we can spare the get_page() here.
 		 */
-		lruvec = lock_page_lruvec_irq(page);
+		lruvec = relock_page_lruvec_irq(page, lruvec);
 		del_page_from_lru_list(page, lruvec, page_lru(page));
-		unlock_page_lruvec_irq(lruvec);
 		continue;
 
 		/*
@@ -320,6 +319,8 @@ static void __munlock_pagevec(struct pagevec *pvec, struct zone *zone)
 		pagevec_add(&pvec_putback, pvec->pages[i]);
 		pvec->pages[i] = NULL;
 	}
+	if (lruvec)
+		unlock_page_lruvec_irq(lruvec);
 	__mod_zone_page_state(zone, NR_MLOCK, delta_munlocked);
 
 	/* Now we can release pins of pages that we are not munlocking */
Signature · 1 line
1.8.3.1
#26 lore raw

PATCH v9 16/20 mm/swap: only change the lru_lock iff page's lruvec is different

In reply to Alex Shi

Since we introduced relock_page_lruvec, we could use it in more place to reduce spin_locks.

---
 mm/swap.c | 15 +++++++--------
 1 file changed, 7 insertions(+), 8 deletions(-)
mm/swap.c +7 -8
index 50c856246f84..74e03589adde 100644
@@ -197,13 +197,15 @@ static void pagevec_lru_move_fn(struct pagevec *pvec,
 		if (isolation && !TestClearPageLRU(page))
 			continue;
 
-		lruvec = lock_page_lruvec_irqsave(page, &flags);
+		lruvec = relock_page_lruvec_irqsave(page, lruvec, &flags);
 		(*move_fn)(page, lruvec, arg);
-		unlock_page_lruvec_irqrestore(lruvec, flags);
 
 		if (isolation)
 			SetPageLRU(page);
 	}
+	if (lruvec)
+		unlock_page_lruvec_irqrestore(lruvec, flags);
+
 	release_pages(pvec->pages, pvec->nr);
 	pagevec_reinit(pvec);
 }
@@ -821,14 +823,11 @@ void release_pages(struct page **pages, int nr)
 		}
 
 		if (TestClearPageLRU(page)) {
-			struct lruvec *new_lruvec = mem_cgroup_page_lruvec(page, page_pgdat(page));
+			struct lruvec *pre_lruvec = lruvec;
 
-			if (new_lruvec != lruvec) {
-				if (lruvec)
-					unlock_page_lruvec_irqrestore(lruvec, flags);
+			lruvec = relock_page_lruvec_irqsave(page, lruvec, &flags);
+			if (pre_lruvec != lruvec)
 				lock_batch = 0;
-				lruvec = lock_page_lruvec_irqsave(page, &flags);
-			}
 
 			del_page_from_lru_list(page, lruvec, page_off_lru(page));
 		}
Signature · 1 line
1.8.3.1
#27 lore raw

PATCH v9 17/20 mm/pgdat: remove pgdat lru_lock

In reply to Alex Shi

Now pgdat.lru_lock was replaced by lruvec lock. It's not used anymore.

Cc Andrew Morton <[email protected]>
Cc Konstantin Khlebnikov <[email protected]>
Cc Hugh Dickins <[email protected]>
Cc Johannes Weiner <[email protected]>
---
 include/linux/mmzone.h | 1 -
 mm/page_alloc.c        | 1 -
 2 files changed, 2 deletions(-)
include/linux/mmzone.h +0 -1
index a7bdff94990d..f1ff6713ac06 100644
@@ -768,7 +768,6 @@ struct deferred_split {
 
 	/* Write-intensive fields used by page reclaim */
 	ZONE_PADDING(_pad1_)
-	spinlock_t		lru_lock;
 
 #ifdef CONFIG_DEFERRED_STRUCT_PAGE_INIT
 	/*
mm/page_alloc.c +0 -1
index 3c4eb750a199..8c7df304bcd1 100644
@@ -6709,7 +6709,6 @@ static void __meminit pgdat_init_internals(struct pglist_data *pgdat)
 	init_waitqueue_head(&pgdat->pfmemalloc_wait);
 
 	pgdat_page_ext_init(pgdat);
-	spin_lock_init(&pgdat->lru_lock);
 	lruvec_init(&pgdat->__lruvec);
 }
 
Signature · 1 line
1.8.3.1
#28 lore raw

PATCH v9 18/20 mm/lru: revise the comments of lru_lock

In reply to Alex Shi

From: Hugh Dickins <[email protected]>

Since we changed the pgdat->lru_lock to lruvec->lru_lock, it's time to fix the incorrect comments in code. Also fixed some zone->lru_lock comment error from ancient time. etc.

---
 Documentation/admin-guide/cgroup-v1/memcg_test.rst | 15 +++------------
 Documentation/admin-guide/cgroup-v1/memory.rst     |  8 ++++----
 Documentation/trace/events-kmem.rst                |  2 +-
 Documentation/vm/unevictable-lru.rst               | 22 ++++++++--------------
 include/linux/mm_types.h                           |  2 +-
 include/linux/mmzone.h                             |  2 +-
 mm/filemap.c                                       |  4 ++--
 mm/memcontrol.c                                    |  2 +-
 mm/rmap.c                                          |  2 +-
 mm/vmscan.c                                        | 12 ++++++++----
 10 files changed, 30 insertions(+), 41 deletions(-)
Documentation/admin-guide/cgroup-v1/memcg_test.rst +3 -12
index 3f7115e07b5d..0b9f91589d3d 100644
@@ -133,18 +133,9 @@ Under below explanation, we assume CONFIG_MEM_RES_CTRL_SWAP=y.
 
 8. LRU
 ======
-        Each memcg has its own private LRU. Now, its handling is under global
-	VM's control (means that it's handled under global pgdat->lru_lock).
-	Almost all routines around memcg's LRU is called by global LRU's
-	list management functions under pgdat->lru_lock.
-
-	A special function is mem_cgroup_isolate_pages(). This scans
-	memcg's private LRU and call __isolate_lru_page() to extract a page
-	from LRU.
-
-	(By __isolate_lru_page(), the page is removed from both of global and
-	private LRU.)
-
+	Each memcg has its own vector of LRUs (inactive anon, active anon,
+	inactive file, active file, unevictable) of pages from each node,
+	each LRU handled under a single lru_lock for that memcg and node.
 
 9. Typical Tests.
 =================
Documentation/admin-guide/cgroup-v1/memory.rst +4 -4
index 0ae4f564c2d6..5a68ecfdb835 100644
@@ -297,13 +297,13 @@ When oom event notifier is registered, event will be delivered.
 
    PG_locked.
      mm->page_table_lock
-         pgdat->lru_lock
-	   lock_page_cgroup.
+       lruvec->lru_lock
+	 lock_page_cgroup.
 
   In many cases, just lock_page_cgroup() is called.
 
-  per-zone-per-cgroup LRU (cgroup's private LRU) is just guarded by
-  pgdat->lru_lock, it has no lock of its own.
+  per-node-per-cgroup LRU (cgroup's private LRU) is just guarded by
+  lruvec->lru_lock, it has no lock of its own.
 
 2.7 Kernel Memory Extension (CONFIG_MEMCG_KMEM)
 -----------------------------------------------
Documentation/trace/events-kmem.rst +1 -1
index 555484110e36..68fa75247488 100644
@@ -69,7 +69,7 @@ When pages are freed in batch, the also mm_page_free_batched is triggered.
 Broadly speaking, pages are taken off the LRU lock in bulk and
 freed in batch with a page list. Significant amounts of activity here could
 indicate that the system is under memory pressure and can also indicate
-contention on the zone->lru_lock.
+contention on the lruvec->lru_lock.
 
 4. Per-CPU Allocator Activity
 =============================
Documentation/vm/unevictable-lru.rst +8 -14
index 17d0861b0f1d..0e1490524f53 100644
@@ -33,7 +33,7 @@ reclaim in Linux.  The problems have been observed at customer sites on large
 memory x86_64 systems.
 
 To illustrate this with an example, a non-NUMA x86_64 platform with 128GB of
-main memory will have over 32 million 4k pages in a single zone.  When a large
+main memory will have over 32 million 4k pages in a single node.  When a large
 fraction of these pages are not evictable for any reason [see below], vmscan
 will spend a lot of time scanning the LRU lists looking for the small fraction
 of pages that are evictable.  This can result in a situation where all CPUs are
@@ -55,7 +55,7 @@ unevictable, either by definition or by circumstance, in the future.
 The Unevictable Page List
 -------------------------
 
-The Unevictable LRU infrastructure consists of an additional, per-zone, LRU list
+The Unevictable LRU infrastructure consists of an additional, per-node, LRU list
 called the "unevictable" list and an associated page flag, PG_unevictable, to
 indicate that the page is being managed on the unevictable list.
 
@@ -84,15 +84,9 @@ The unevictable list does not differentiate between file-backed and anonymous,
 swap-backed pages.  This differentiation is only important while the pages are,
 in fact, evictable.
 
-The unevictable list benefits from the "arrayification" of the per-zone LRU
+The unevictable list benefits from the "arrayification" of the per-node LRU
 lists and statistics originally proposed and posted by Christoph Lameter.
 
-The unevictable list does not use the LRU pagevec mechanism. Rather,
-unevictable pages are placed directly on the page's zone's unevictable list
-under the zone lru_lock.  This allows us to prevent the stranding of pages on
-the unevictable list when one task has the page isolated from the LRU and other
-tasks are changing the "evictability" state of the page.
-
 
 Memory Control Group Interaction
 --------------------------------
@@ -101,8 +95,8 @@ The unevictable LRU facility interacts with the memory control group [aka
 memory controller; see Documentation/admin-guide/cgroup-v1/memory.rst] by extending the
 lru_list enum.
 
-The memory controller data structure automatically gets a per-zone unevictable
-list as a result of the "arrayification" of the per-zone LRU lists (one per
+The memory controller data structure automatically gets a per-node unevictable
+list as a result of the "arrayification" of the per-node LRU lists (one per
 lru_list enum element).  The memory controller tracks the movement of pages to
 and from the unevictable list.
 
@@ -196,7 +190,7 @@ for the sake of expediency, to leave a unevictable page on one of the regular
 active/inactive LRU lists for vmscan to deal with.  vmscan checks for such
 pages in all of the shrink_{active|inactive|page}_list() functions and will
 "cull" such pages that it encounters: that is, it diverts those pages to the
-unevictable list for the zone being scanned.
+unevictable list for the node being scanned.
 
 There may be situations where a page is mapped into a VM_LOCKED VMA, but the
 page is not marked as PG_mlocked.  Such pages will make it all the way to
@@ -328,7 +322,7 @@ If the page was NOT already mlocked, mlock_vma_page() attempts to isolate the
 page from the LRU, as it is likely on the appropriate active or inactive list
 at that time.  If the isolate_lru_page() succeeds, mlock_vma_page() will put
 back the page - by calling putback_lru_page() - which will notice that the page
-is now mlocked and divert the page to the zone's unevictable list.  If
+is now mlocked and divert the page to the node's unevictable list.  If
 mlock_vma_page() is unable to isolate the page from the LRU, vmscan will handle
 it later if and when it attempts to reclaim the page.
 
@@ -603,7 +597,7 @@ Some examples of these unevictable pages on the LRU lists are:
      unevictable list in mlock_vma_page().
 
 shrink_inactive_list() also diverts any unevictable pages that it finds on the
-inactive lists to the appropriate zone's unevictable list.
+inactive lists to the appropriate node's unevictable list.
 
 shrink_inactive_list() should only see SHM_LOCK'd pages that became SHM_LOCK'd
 after shrink_active_list() had moved them to the inactive list, or pages mapped
include/linux/mm_types.h +1 -1
index c28911c3afa8..852b8ee0da5d 100644
@@ -78,7 +78,7 @@ struct page {
 		struct {	/* Page cache and anonymous pages */
 			/**
 			 * @lru: Pageout list, eg. active_list protected by
-			 * pgdat->lru_lock.  Sometimes used as a generic list
+			 * lruvec->lru_lock.  Sometimes used as a generic list
 			 * by the page owner.
 			 */
 			struct list_head lru;
include/linux/mmzone.h +1 -1
index f1ff6713ac06..bc91d4a43960 100644
@@ -159,7 +159,7 @@ static inline bool free_area_empty(struct free_area *area, int migratetype)
 struct pglist_data;
 
 /*
- * zone->lock and the zone lru_lock are two of the hottest locks in the kernel.
+ * zone->lock and the lru_lock are two of the hottest locks in the kernel.
  * So add a wild amount of padding here to ensure that they fall into separate
  * cachelines.  There are very few zone structures in the machine, so space
  * consumption is not a concern here.
mm/filemap.c +2 -2
index 1784478270e1..28fff98f4459 100644
@@ -101,8 +101,8 @@
  *    ->swap_lock		(try_to_unmap_one)
  *    ->private_lock		(try_to_unmap_one)
  *    ->i_pages lock		(try_to_unmap_one)
- *    ->pgdat->lru_lock		(follow_page->mark_page_accessed)
- *    ->pgdat->lru_lock		(check_pte_range->isolate_lru_page)
+ *    ->lruvec->lru_lock	(follow_page->mark_page_accessed)
+ *    ->lruvec->lru_lock	(check_pte_range->isolate_lru_page)
  *    ->private_lock		(page_remove_rmap->set_page_dirty)
  *    ->i_pages lock		(page_remove_rmap->set_page_dirty)
  *    bdi.wb->list_lock		(page_remove_rmap->set_page_dirty)
mm/memcontrol.c +1 -1
index b0c156da60fe..099926efbb48 100644
@@ -2940,7 +2940,7 @@ void __memcg_kmem_uncharge(struct page *page, int order)
 #ifdef CONFIG_TRANSPARENT_HUGEPAGE
 
 /*
- * Because tail pages are not marked as "used", set it. We're under
+ * Because tail pages are not marked as "used", set it. Don't need
  * lruvec->lru_lock and migration entries setup in all page mappings.
  */
 void mem_cgroup_split_huge_fixup(struct page *head)
mm/rmap.c +1 -1
index b3e381919835..39052794cb46 100644
@@ -27,7 +27,7 @@
  *         mapping->i_mmap_rwsem
  *           anon_vma->rwsem
  *             mm->page_table_lock or pte_lock
- *               pgdat->lru_lock (in mark_page_accessed, isolate_lru_page)
+ *               lruvec->lru_lock (in mark_page_accessed, isolate_lru_page)
  *               swap_lock (in swap_duplicate, swap_info_get)
  *                 mmlist_lock (in mmput, drain_mmlist and others)
  *                 mapping->private_lock (in __set_page_dirty_buffers)
mm/vmscan.c +8 -4
index de925bd629eb..2c98352c95cc 100644
@@ -1601,14 +1601,16 @@ static __always_inline void update_lru_sizes(struct lruvec *lruvec,
 }
 
 /**
- * pgdat->lru_lock is heavily contended.  Some of the functions that
+ * Isolating page from the lruvec to fill in @dst list by nr_to_scan times.
+ *
+ * lruvec->lru_lock is heavily contended.  Some of the functions that
  * shrink the lists perform better by taking out a batch of pages
  * and working on them outside the LRU lock.
  *
  * For pagecache intensive workloads, this function is the hottest
  * spot in the kernel (apart from copy_*_user functions).
  *
- * Appropriate locks must be held before calling this function.
+ * Lru_lock must be held before calling this function.
  *
  * @nr_to_scan:	The number of eligible pages to look through on the list.
  * @lruvec:	The LRU vector to pull pages from.
@@ -1809,14 +1811,16 @@ static int too_many_isolated(struct pglist_data *pgdat, int file,
 
 /*
  * This moves pages from @list to corresponding LRU list.
+ * The pages from @list is out of any lruvec, and in the end list reuses as
+ * pages_to_free list.
  *
  * We move them the other way if the page is referenced by one or more
  * processes, from rmap.
  *
  * If the pages are mostly unmapped, the processing is fast and it is
- * appropriate to hold zone_lru_lock across the whole operation.  But if
+ * appropriate to hold lru_lock across the whole operation.  But if
  * the pages are mapped, the processing is slow (page_referenced()) so we
- * should drop zone_lru_lock around each page.  It's impossible to balance
+ * should drop lru_lock around each page.  It's impossible to balance
  * this, so instead we remove the pages from the LRU while processing them.
  * It is safe to rely on PG_active against the non-LRU pages in here because
  * nobody will play with that bit on a non-LRU page.
Signature · 1 line
1.8.3.1
#29 lore raw

PATCH v9 19/20 mm/lru: add debug checking for page memcg moving

In reply to Alex Shi

This debug patch could give some clues if there are sth out of consideration.

Hugh Dickins reported a bug of this patch, Thanks!

---
 include/linux/memcontrol.h |  6 ++++++
 mm/compaction.c            |  2 ++
 mm/memcontrol.c            | 15 +++++++++++++++
 3 files changed, 23 insertions(+)
include/linux/memcontrol.h +6 -0
index f60009580d2a..27c7bbe82125 100644
@@ -431,6 +431,8 @@ struct lruvec *lock_page_lruvec_irqsave(struct page *page,
 void unlock_page_lruvec_irq(struct lruvec *lruvec);
 void unlock_page_lruvec_irqrestore(struct lruvec *lruvec, unsigned long flags);
 
+void lruvec_memcg_debug(struct lruvec *lruvec, struct page *page);
+
 static inline
 struct mem_cgroup *mem_cgroup_from_css(struct cgroup_subsys_state *css){
 	return css ? container_of(css, struct mem_cgroup, css) : NULL;
@@ -1191,6 +1193,10 @@ static inline void count_memcg_page_event(struct page *page,
 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 */
 
 /* idx can be of type enum memcg_stat_item or node_stat_item */
mm/compaction.c +2 -0
index 4a773f0ffedf..40c270bd5092 100644
@@ -984,6 +984,8 @@ static bool too_many_isolated(pg_data_t *pgdat)
 			compact_lock_irqsave(&lruvec->lru_lock, &flags, cc);
 			locked_lruvec = lruvec;
 
+			lruvec_memcg_debug(lruvec, page);
+
 			/* Try get exclusive access under lock */
 			if (!skip_updated) {
 				skip_updated = true;
mm/memcontrol.c +15 -0
index 099926efbb48..2d71e53ead88 100644
@@ -1240,6 +1240,19 @@ struct lruvec *mem_cgroup_page_lruvec(struct page *page, struct pglist_data *pgd
 	return lruvec;
 }
 
+void lruvec_memcg_debug(struct lruvec *lruvec, struct page *page)
+{
+#ifdef CONFIG_DEBUG_VM
+	if (mem_cgroup_disabled())
+		return;
+
+	if (!page->mem_cgroup)
+		VM_BUG_ON_PAGE(lruvec_memcg(lruvec) != root_mem_cgroup, page);
+	else
+		VM_BUG_ON_PAGE(lruvec_memcg(lruvec) != page->mem_cgroup, page);
+#endif
+}
+
 /* page must be isolated */
 struct lruvec *lock_page_lruvec_irq(struct page *page)
 {
@@ -1248,6 +1261,7 @@ struct lruvec *lock_page_lruvec_irq(struct page *page)
 	lruvec = mem_cgroup_page_lruvec(page, page_pgdat(page));
 	spin_lock_irq(&lruvec->lru_lock);
 
+	lruvec_memcg_debug(lruvec, page);
 	return lruvec;
 }
 
@@ -1258,6 +1272,7 @@ struct lruvec *lock_page_lruvec_irqsave(struct page *page, unsigned long *flags)
 	lruvec = mem_cgroup_page_lruvec(page, page_pgdat(page));
 	spin_lock_irqsave(&lruvec->lru_lock, *flags);
 
+	lruvec_memcg_debug(lruvec, page);
 	return lruvec;
 }
 
Signature · 1 line
1.8.3.1
#30 lore raw

PATCH v9 20/20 mm/memcg: add debug checking in lock_page_memcg

In reply to Alex Shi

This extra irq disable/enable and BUG_ON checking costs 5% readtwice performance on a 2 socket * 26 cores * HT box. So put it into CONFIG_PROVE_LOCKING.

---
 mm/memcontrol.c | 6 ++++++
 1 file changed, 6 insertions(+)
mm/memcontrol.c +6 -0
index 2d71e53ead88..8d7f6336f15c 100644
@@ -2026,6 +2026,12 @@ struct mem_cgroup *lock_page_memcg(struct page *page)
 	if (unlikely(!memcg))
 		return NULL;
 
+#ifdef CONFIG_PROVE_LOCKING
+	local_irq_save(flags);
+	might_lock(&memcg->move_lock);
+	local_irq_restore(flags);
+#endif
+
 	if (atomic_read(&memcg->moving_account) <= 0)
 		return memcg;
 
Signature · 1 line
1.8.3.1

A message that is not in the archive (sent off-list or not yet received): 1583146830-169516-1-git-send-email-alex.shi-KPsoFbNs7GizrGE5bRqYAgC/[email protected]

#32 lore raw
Quoted 24 lines · On Mon, 2 Mar 2020 19:00:10 +0800 Alex Shi…

Hi all,

This patchset mainly includes 3 parts:
1, some code cleanup and minimum optimization as a preparation. 2, use TestCleanPageLRU as page isolation's precondition 3, replace per node lru_lock with per memcg per node lru_lock

The keypoint of this patchset is moving lru_lock into lruvec, give a lru_lock for each of lruvec, thus bring a lru_lock for each of memcg per node. So on a large node machine, each of memcg don't suffer from per node pgdat->lru_lock waiting. They could go fast with their self lru_lock now.

Since lruvec belongs to each memcg, the critical point is to keep page's memcg stable, so we take PageLRU as its isolation's precondition. Thanks for Johannes Weiner help and suggestion!

Following Daniel Jordan's suggestion, I have run 208 'dd' with on 104 containers on a 2s * 26cores * HT box with a modefied case:
https://git.kernel.org/pub/scm/linux/kernel/git/wfg/vm-scalability.git/tree/case-lru-file-readtwice

With this patchset, the readtwice performance increased about 80% in concurrent containers.

Quoted 6 lines

Thanks Hugh Dickins and Konstantin Khlebnikov, they both brought this idea 8 years ago, and others who give comments as well: Daniel Jordan, Mel Gorman, Shakeel Butt, Matthew Wilcox etc.

Thanks for Testing support from Intel 0day and Rong Chen, Fengguang Wu, and Yun Wang.

I'm not seeing a lot of evidence of review and test activity yet. But I think I'll grab patches 01-06 as they look like fairly straightforward improvements.

A message that is not in the archive (sent off-list or not yet received): 20200302141202.91d88e8b730b194a8bd8fa7d-de/[email protected]

author
#34 lore raw

ÔÚ 2020/3/3 ÉÏÎç6:12, Andrew Morton дµÀ:

Quoted 5 lines
Quoted 2 lines

Thanks for Testing support from Intel 0day and Rong Chen, Fengguang Wu, and Yun Wang.

I'm not seeing a lot of evidence of review and test activity yet. But I think I'll grab patches 01-06 as they look like fairly straightforward improvements.

cc Fengguang and Rong Chen

I did some local functional testing and kselftest, they all look fine. 0day only warn me if some case failed. Is it no news is good news? :)

Thanks
Alex

A message that is not in the archive (sent off-list or not yet received): 59634b5f-b1b2-3b1d-d845-fd15565fcad4-KPsoFbNs7GizrGE5bRqYAgC/[email protected]

#36 lore raw
Quoted 11 lines · Alex Shi
Quoted 5 lines · Andrew Morton
Quoted 2 lines

Thanks for Testing support from Intel 0day and Rong Chen, Fengguang Wu, and Yun Wang.

I'm not seeing a lot of evidence of review and test activity yet. But I think I'll grab patches 01-06 as they look like fairly straightforward improvements.

cc Fengguang and Rong Chen

I did some local functional testing and kselftest, they all look fine. 0day only warn me if some case failed. Is it no news is good news? :)

And now the bad news.

Andrew, please revert those six (or seven as they ended up in mmotm). 5.6-rc4-mm1 without them runs my tmpfs+loop+swapping+memcg+ksm kernel build loads fine (did four hours just now), but 5.6-rc4-mm1 itself crashed just after starting - seconds or minutes I didn't see, but it did not complete an iteration.

I thought maybe those six would be harmless (though I've not looked at them at all); but knew already that the full series is not good yet:
I gave it a try over 5.6-rc4 on Monday, and crashed very soon on simpler testing, in different ways from what hits mmotm.

The first thing wrong with the full set was when I tried tmpfs+loop+ swapping kernel builds in "mem=700M cgroup_disabled=memory", of course with CONFIG_DEBUG_LIST=y. That soon collapsed in a splurge of OOM kills and list_del corruption messages: __list_del_entry_valid < list_del < __page_cache_release < __put_page < put_page < __try_to_reclaim_swap < free_swap_and_cache < shmem_free_swap < shmem_undo_range.

When I next tried with "mem=1G" and memcg enabled (but not being used), that managed some iterations, no OOM kills, no list_del warnings (was it swapping? perhaps, perhaps not, I was trying to go easy on it just to see if "cgroup_disabled=memory" had been the problem); but when rebooting after that, again list_del corruption messages and crash (I didn't note them down).

So I didn't take much notice of what the mmotm crash backtrace showed (but IIRC shmem and swap were in it).

Alex, I'm afraid you're focusing too much on performance results, without doing the basic testing needed - I thought we had given you some hints on the challenging areas (swapping, move_charge_at_immigrate, page migration) when we attached a *correctly working* 5.3 version back on 23rd August:

https://lore.kernel.org/linux-mm/[email protected]/

(Correctly working, except missing two patches I'd mistakenly dropped as unnecessary in earlier rebases: but our discussions with Johannes later showed to be very necessary, though their races rarely seen.)

I have not had the time (and do not expect to have the time) to review your series: maybe it's one or two small fixes away from being complete, or maybe it's still fundamentally flawed, I do not know. I had naively hoped that you would help with a patchset that worked, rather than cutting it down into something which does not.

Submitting your series to routine testing is much easier for me than reviewing it: but then, yes, it's a pity that I don't find the time to report the results on intervening versions, which also crashed.

What I have to do now, is set aside time today and tomorrow, to package up the old scripts I use, describe them and their environment, and send them to you (cc akpm in case I fall under a bus): so that you can reproduce the crashes for yourself, and get to work on them.

Hugh

#37 lore raw

Re: [PATCH v9 02/20] mm/memcg: fold lock_page_lru into commit_charge

In reply to Alex Shi
Quoted 94 lines · On Mon, 2 Mar 2020 19:00:12 +0800 Alex Shi wrote:

As Konstantin Khlebnikov mentioned:

Also I don't like these functions:
- called lock/unlock but actually also isolates
- used just once
- pgdat evaluated twice

Cleanup and fold these functions into commit_charge. It also reduces lock time while lrucare && !PageLRU.

Cc Johannes Weiner <[email protected]>
Cc Michal Hocko <[email protected]>
Cc Konstantin Khlebnikov <[email protected]>
Cc Vladimir Davydov <[email protected]>
Cc Andrew Morton <[email protected]>
---
 mm/memcontrol.c | 57 ++++++++++++++++++++-------------------------------------
 1 file changed, 20 insertions(+), 37 deletions(-)
mm/memcontrol.c +13 -35
index d09776cd6e10..875e2aebcde7 100644
@@ -2572,41 +2572,11 @@ static void cancel_charge(struct mem_cgroup *memcg, unsigned int nr_pages)
 	css_put_many(&memcg->css, nr_pages);
 }
 
-static void lock_page_lru(struct page *page, int *isolated)
-{
-	pg_data_t *pgdat = page_pgdat(page);
-
-	spin_lock_irq(&pgdat->lru_lock);
-	if (PageLRU(page)) {
-		struct lruvec *lruvec;
-
-		lruvec = mem_cgroup_page_lruvec(page, pgdat);
-		ClearPageLRU(page);
-		del_page_from_lru_list(page, lruvec, page_lru(page));
-		*isolated = 1;
-	} else
-		*isolated = 0;
-}
-
-static void unlock_page_lru(struct page *page, int isolated)
-{
-	pg_data_t *pgdat = page_pgdat(page);
-
-	if (isolated) {
-		struct lruvec *lruvec;
-
-		lruvec = mem_cgroup_page_lruvec(page, pgdat);
-		VM_BUG_ON_PAGE(PageLRU(page), page);
-		SetPageLRU(page);
-		add_page_to_lru_list(page, lruvec, page_lru(page));
-	}
-	spin_unlock_irq(&pgdat->lru_lock);
-}
-
 static void commit_charge(struct page *page, struct mem_cgroup *memcg,
 			  bool lrucare)
 {
-	int isolated;
+	struct lruvec *lruvec = NULL;
+	pg_data_t *pgdat;
 
 	VM_BUG_ON_PAGE(page->mem_cgroup, page);
 
@@ -2614,9 +2584,17 @@ static void commit_charge(struct page *page, struct mem_cgroup *memcg,
 	 * In some cases, SwapCache and FUSE(splice_buf->radixtree), the page
 	 * may already be on some other mem_cgroup's LRU.  Take care of it.
 	 */
-	if (lrucare)
-		lock_page_lru(page, &isolated);
-
+	if (lrucare) {
+		pgdat = page_pgdat(page);
+		spin_lock_irq(&pgdat->lru_lock);
+
+		if (PageLRU(page)) {
+			lruvec = mem_cgroup_page_lruvec(page, pgdat);
+			ClearPageLRU(page);
+			del_page_from_lru_list(page, lruvec, page_lru(page));
+		} else
+			spin_unlock_irq(&pgdat->lru_lock);
+	}
 	/*
 	 * Nobody should be changing or seriously looking at
 	 * page->mem_cgroup at this point:

mm/memcontrol.c static void commit_charge(struct page *page, struct mem_cgroup *memcg, · line 2613

Quoted 3 lines
mm/memcontrol.c +0 -0
@@ -2633,8 +2611,13 @@ static void commit_charge(struct page *page, struct mem_cgroup *memcg,
 	 */
 	page->mem_cgroup = memcg;
 

Well it is likely to update memcg for page without lru_lock held even if more care is required, which is a change added in the current semantic and worth a line of words in log.

Quoted 16 lines
mm/memcontrol.c +7 -2
-	if (lrucare)
-		unlock_page_lru(page, isolated);
+	if (lrucare && lruvec) {
+		lruvec = mem_cgroup_page_lruvec(page, pgdat);
+		VM_BUG_ON_PAGE(PageLRU(page), page);
+		SetPageLRU(page);
+		add_page_to_lru_list(page, lruvec, page_lru(page));
+		spin_unlock_irq(&pgdat->lru_lock);
+	}
 }
 
 #ifdef CONFIG_MEMCG_KMEM
Signature · 1 line
1.8.3.1
#38 lore raw

Re: [PATCH v9 02/20] mm/memcg: fold lock_page_lru into commit_charge

ÔÚ 2020/3/4 ÉÏÎç11:13, Hillf Danton дµÀ:

Quoted 10 lines

Review static void commit_charge(struct page *page, struct mem_cgroup *memcg, · line 2613

Quoted 6 lines
(quoted diff) +0 -0
 	 * Nobody should be changing or seriously looking at
 	 * page->mem_cgroup at this point:
@@ -2633,8 +2611,13 @@ static void commit_charge(struct page *page, struct mem_cgroup *memcg,
 	 */
 	page->mem_cgroup = memcg;
 

Well it is likely to update memcg for page without lru_lock held even if more care is required, which is a change added in the current semantic and worth a line of words in log.

the lru_lock is guard for lru list, not for page->mem_cgroup, seem no need to highlight this point. Do we?

Thanks
Alex