Skip to contentSkip to conversation

mm: use add_page_to_lru_list()/page_lru()/page_off_lru()

PATCH Superseded Yu Zhao 2 participants 6 messages Aug 27, 2020 – Aug 31, 2020

Subsystems (4)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Yu Zhao

author
#0 lore raw

PATCH 1/2 mm: use add_page_to_lru_list()/page_lru()/page_off_lru()

This is a trivial but worth having clean-up patch. There should be no side effects except page->lru is temporarily poisoned after it's deleted but before it's added to the new list in move_pages_to_lru() (which is not a problem).

---
 mm/swap.c   |  4 +---
 mm/vmscan.c | 14 ++++----------
 2 files changed, 5 insertions(+), 13 deletions(-)
mm/swap.c +1 -3
index 40bf20a75278..2735ecf0f566 100644
@@ -597,11 +597,9 @@ static void lru_lazyfree_fn(struct page *page, struct lruvec *lruvec,
 {
 	if (PageLRU(page) && PageAnon(page) && PageSwapBacked(page) &&
 	    !PageSwapCache(page) && !PageUnevictable(page)) {
-		bool active = PageActive(page);
 		int nr_pages = thp_nr_pages(page);
 
-		del_page_from_lru_list(page, lruvec,
-				       LRU_INACTIVE_ANON + active);
+		del_page_from_lru_list(page, lruvec, page_lru(page));
 		ClearPageActive(page);
 		ClearPageReferenced(page);
 		/*
mm/vmscan.c +4 -10
index 99e1796eb833..b479ced26cd3 100644
@@ -1845,13 +1845,12 @@ static unsigned noinline_for_stack move_pages_to_lru(struct lruvec *lruvec,
 	int nr_pages, nr_moved = 0;
 	LIST_HEAD(pages_to_free);
 	struct page *page;
-	enum lru_list lru;
 
 	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);
@@ -1860,16 +1859,10 @@ static unsigned noinline_for_stack move_pages_to_lru(struct lruvec *lruvec,
 		lruvec = mem_cgroup_page_lruvec(page, pgdat);
 
 		SetPageLRU(page);
-		lru = page_lru(page);
-
-		nr_pages = thp_nr_pages(page);
-		update_lru_size(lruvec, lru, page_zonenum(page), nr_pages);
-		list_move(&page->lru, &lruvec->lists[lru]);
+		add_page_to_lru_list(page, lruvec, page_lru(page));
 
 		if (put_page_testzero(page)) {
-			__ClearPageLRU(page);
-			__ClearPageActive(page);
-			del_page_from_lru_list(page, lruvec, lru);
+			del_page_from_lru_list(page, lruvec, page_off_lru(page));
 
 			if (unlikely(PageCompound(page))) {
 				spin_unlock_irq(&pgdat->lru_lock);
@@ -1878,6 +1871,7 @@ static unsigned noinline_for_stack move_pages_to_lru(struct lruvec *lruvec,
 			} else
 				list_add(&page->lru, &pages_to_free);
 		} else {
+			nr_pages = thp_nr_pages(page);
 			nr_moved += nr_pages;
 			if (PageActive(page))
 				workingset_age_nonresident(lruvec, nr_pages);
Signature · 1 line
2.28.0.402.g5ffc5be6b7-goog
author
#1 lore raw

PATCH 2/2 mm: use self-explanatory macros rather than "2"

This is a trivial clean-up patch. Take it or leave it.

---
 include/linux/mmzone.h | 12 ++++++++----
 include/linux/vmstat.h |  2 +-
 mm/vmscan.c            |  2 +-
 3 files changed, 10 insertions(+), 6 deletions(-)
include/linux/mmzone.h +8 -4
index 8379432f4f2f..be1cdbf8317f 100644
@@ -266,6 +266,8 @@ static inline bool is_active_lru(enum lru_list lru)
 	return (lru == LRU_ACTIVE_ANON || lru == LRU_ACTIVE_FILE);
 }
 
+#define ANON_AND_FILE 2
+
 enum lruvec_flags {
 	LRUVEC_CONGESTED,		/* lruvec has many dirty pages
 					 * backed by a congested BDI
@@ -283,8 +285,8 @@ struct lruvec {
 	unsigned long			file_cost;
 	/* Non-resident age, driven by LRU movement */
 	atomic_long_t			nonresident_age;
-	/* Refaults at the time of last reclaim cycle, anon=0, file=1 */
-	unsigned long			refaults[2];
+	/* Refaults at the time of last reclaim cycle */
+	unsigned long			refaults[ANON_AND_FILE];
 	/* Various lruvec state flags (enum lruvec_flags) */
 	unsigned long			flags;
 #ifdef CONFIG_MEMCG
@@ -406,6 +408,8 @@ enum zone_type {
 
 #ifndef __GENERATING_BOUNDS_H
 
+#define ASYNC_AND_SYNC 2
+
 struct zone {
 	/* Read-mostly fields */
 
@@ -525,8 +529,8 @@ struct zone {
 #if defined CONFIG_COMPACTION || defined CONFIG_CMA
 	/* pfn where compaction free scanner should start */
 	unsigned long		compact_cached_free_pfn;
-	/* pfn where async and sync compaction migration scanner should start */
-	unsigned long		compact_cached_migrate_pfn[2];
+	/* pfn where compaction migration scanner should start */
+	unsigned long		compact_cached_migrate_pfn[ASYNC_AND_SYNC];
 	unsigned long		compact_init_migrate_pfn;
 	unsigned long		compact_init_free_pfn;
 #endif
include/linux/vmstat.h +1 -1
index 91220ace31da..d5431c1bf6e5 100644
@@ -28,7 +28,7 @@ struct reclaim_stat {
 	unsigned nr_writeback;
 	unsigned nr_immediate;
 	unsigned nr_pageout;
-	unsigned nr_activate[2];
+	unsigned nr_activate[ANON_AND_FILE];
 	unsigned nr_ref_keep;
 	unsigned nr_unmap_fail;
 	unsigned nr_lazyfree_fail;
mm/vmscan.c +1 -1
index b479ced26cd3..c4ddb2a88b07 100644
@@ -2231,7 +2231,7 @@ static void get_scan_count(struct lruvec *lruvec, struct scan_control *sc,
 	struct mem_cgroup *memcg = lruvec_memcg(lruvec);
 	unsigned long anon_cost, file_cost, total_cost;
 	int swappiness = mem_cgroup_swappiness(memcg);
-	u64 fraction[2];
+	u64 fraction[ANON_AND_FILE];
 	u64 denominator = 0;	/* gcc */
 	enum scan_balance scan_balance;
 	unsigned long ap, fp;
Signature · 1 line
2.28.0.402.g5ffc5be6b7-goog
author
#2 lore raw
In reply to Yu Zhao
Quoted 6 lines · Yu Zhao

This is a trivial but worth having clean-up patch. There should be no side effects except page->lru is temporarily poisoned after it's deleted but before it's added to the new list in move_pages_to_lru() (which is not a problem).

Hi Alex, I just realized your
[v18,08/32] mm/vmscan: remove unnecessary lruvec adding
at
https://patchwork.kernel.org/patch/11733123/
also touches move_pages_to_lru(). I agree it's better not to add a page we are going to free to the list in the first place. The rest in this patch would be too trivial to be a separate one (on top of yours).

So would you mind taking of the clean-up too in your series? I'll drop this one then. Thanks.

Quoted 71 lines
---
 mm/swap.c   |  4 +---
 mm/vmscan.c | 14 ++++----------
 2 files changed, 5 insertions(+), 13 deletions(-)
mm/swap.c +1 -3
index 40bf20a75278..2735ecf0f566 100644
@@ -597,11 +597,9 @@ static void lru_lazyfree_fn(struct page *page, struct lruvec *lruvec,
 {
 	if (PageLRU(page) && PageAnon(page) && PageSwapBacked(page) &&
 	    !PageSwapCache(page) && !PageUnevictable(page)) {
-		bool active = PageActive(page);
 		int nr_pages = thp_nr_pages(page);
 
-		del_page_from_lru_list(page, lruvec,
-				       LRU_INACTIVE_ANON + active);
+		del_page_from_lru_list(page, lruvec, page_lru(page));
 		ClearPageActive(page);
 		ClearPageReferenced(page);
 		/*
mm/vmscan.c +4 -10
index 99e1796eb833..b479ced26cd3 100644
@@ -1845,13 +1845,12 @@ static unsigned noinline_for_stack move_pages_to_lru(struct lruvec *lruvec,
 	int nr_pages, nr_moved = 0;
 	LIST_HEAD(pages_to_free);
 	struct page *page;
-	enum lru_list lru;
 
 	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);
@@ -1860,16 +1859,10 @@ static unsigned noinline_for_stack move_pages_to_lru(struct lruvec *lruvec,
 		lruvec = mem_cgroup_page_lruvec(page, pgdat);
 
 		SetPageLRU(page);
-		lru = page_lru(page);
-
-		nr_pages = thp_nr_pages(page);
-		update_lru_size(lruvec, lru, page_zonenum(page), nr_pages);
-		list_move(&page->lru, &lruvec->lists[lru]);
+		add_page_to_lru_list(page, lruvec, page_lru(page));
 
 		if (put_page_testzero(page)) {
-			__ClearPageLRU(page);
-			__ClearPageActive(page);
-			del_page_from_lru_list(page, lruvec, lru);
+			del_page_from_lru_list(page, lruvec, page_off_lru(page));
 
 			if (unlikely(PageCompound(page))) {
 				spin_unlock_irq(&pgdat->lru_lock);
@@ -1878,6 +1871,7 @@ static unsigned noinline_for_stack move_pages_to_lru(struct lruvec *lruvec,
 			} else
 				list_add(&page->lru, &pages_to_free);
 		} else {
+			nr_pages = thp_nr_pages(page);
 			nr_moved += nr_pages;
 			if (PageActive(page))
 				workingset_age_nonresident(lruvec, nr_pages);
Signature · 1 line
2.28.0.402.g5ffc5be6b7-goog
#3 lore raw
Quoted 72 lines · Yu Zhao
Quoted 6 lines · Yu Zhao

This is a trivial but worth having clean-up patch. There should be no side effects except page->lru is temporarily poisoned after it's deleted but before it's added to the new list in move_pages_to_lru() (which is not a problem).

Hi Alex, I just realized your
[v18,08/32] mm/vmscan: remove unnecessary lruvec adding
at
https://patchwork.kernel.org/patch/11733123/
also touches move_pages_to_lru(). I agree it's better not to add a page we are going to free to the list in the first place. The rest in this patch would be too trivial to be a separate one (on top of yours).

So would you mind taking of the clean-up too in your series? I'll drop this one then. Thanks.

Quoted 52 lines
---
 mm/swap.c   |  4 +---
 mm/vmscan.c | 14 ++++----------
 2 files changed, 5 insertions(+), 13 deletions(-)
mm/swap.c +1 -3
index 40bf20a75278..2735ecf0f566 100644
@@ -597,11 +597,9 @@ static void lru_lazyfree_fn(struct page *page, struct lruvec *lruvec,
 {
 	if (PageLRU(page) && PageAnon(page) && PageSwapBacked(page) &&
 	    !PageSwapCache(page) && !PageUnevictable(page)) {
-		bool active = PageActive(page);
 		int nr_pages = thp_nr_pages(page);
 
-		del_page_from_lru_list(page, lruvec,
-				       LRU_INACTIVE_ANON + active);
+		del_page_from_lru_list(page, lruvec, page_lru(page));
 		ClearPageActive(page);
 		ClearPageReferenced(page);
 		/*
mm/vmscan.c +2 -7
index 99e1796eb833..b479ced26cd3 100644
@@ -1845,13 +1845,12 @@ static unsigned noinline_for_stack move_pages_to_lru(struct lruvec *lruvec,
 	int nr_pages, nr_moved = 0;
 	LIST_HEAD(pages_to_free);
 	struct page *page;
-	enum lru_list lru;
 
 	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);
@@ -1860,16 +1859,10 @@ static unsigned noinline_for_stack move_pages_to_lru(struct lruvec *lruvec,
 		lruvec = mem_cgroup_page_lruvec(page, pgdat);
 
 		SetPageLRU(page);
-		lru = page_lru(page);
-
-		nr_pages = thp_nr_pages(page);
-		update_lru_size(lruvec, lru, page_zonenum(page), nr_pages);
-		list_move(&page->lru, &lruvec->lists[lru]);
+		add_page_to_lru_list(page, lruvec, page_lru(page));
 
Quoted 2 lines

mm/vmscan.c static unsigned noinline_for_stack move_pages_to_lru(struct lruvec *lruvec, · line 1870

Quoted 2 lines
mm/vmscan.c +0 -1
 		if (put_page_testzero(page)) {
-			__ClearPageLRU(page);

it's interesting to know the PageLRU left has no bad impact in real life. it justs seems a path confliction with my that patch.

Quoted 17 lines
Quoted 17 lines
mm/vmscan.c +2 -2
-			__ClearPageActive(page);
-			del_page_from_lru_list(page, lruvec, lru);
+			del_page_from_lru_list(page, lruvec, page_off_lru(page));
 
 			if (unlikely(PageCompound(page))) {
 				spin_unlock_irq(&pgdat->lru_lock);
@@ -1878,6 +1871,7 @@ static unsigned noinline_for_stack move_pages_to_lru(struct lruvec *lruvec,
 			} else
 				list_add(&page->lru, &pages_to_free);
 		} else {
+			nr_pages = thp_nr_pages(page);
 			nr_moved += nr_pages;
 			if (PageActive(page))
 				workingset_age_nonresident(lruvec, nr_pages);
Signature · 1 line
2.28.0.402.g5ffc5be6b7-goog
author
#4 lore raw
Quoted 20 lines · Alex Shi
Quoted 17 lines · Yu Zhao
Quoted 6 lines · Yu Zhao

This is a trivial but worth having clean-up patch. There should be no side effects except page->lru is temporarily poisoned after it's deleted but before it's added to the new list in move_pages_to_lru() (which is not a problem).

Hi Alex, I just realized your
[v18,08/32] mm/vmscan: remove unnecessary lruvec adding
at
https://patchwork.kernel.org/patch/11733123/
also touches move_pages_to_lru(). I agree it's better not to add a page we are going to free to the list in the first place. The rest in this patch would be too trivial to be a separate one (on top of yours).

Quoted 2 lines
Quoted 2 lines

So would you mind taking of the clean-up too in your series? I'll drop this one then. Thanks.

Ok, maybe I wasn't clear before. Please see what exactly I'm asking you to consider below.

Quoted 11 lines
Quoted 11 lines
Quoted 11 lines
mm/swap.c +0 -1
index 40bf20a75278..2735ecf0f566 100644
@@ -597,11 +597,9 @@ static void lru_lazyfree_fn(struct page *page, struct lruvec *lruvec,
 {
 	if (PageLRU(page) && PageAnon(page) && PageSwapBacked(page) &&
 	    !PageSwapCache(page) && !PageUnevictable(page)) {
-		bool active = PageActive(page);
 		int nr_pages = thp_nr_pages(page);
 
Quoted 6 lines
Quoted 6 lines

mm/swap.c static void lru_lazyfree_fn(struct page *page, struct lruvec *lruvec, · line 605

Quoted 6 lines
mm/swap.c +1 -2
-		del_page_from_lru_list(page, lruvec,
-				       LRU_INACTIVE_ANON + active);
+		del_page_from_lru_list(page, lruvec, page_lru(page));
 		ClearPageActive(page);
 		ClearPageReferenced(page);
 		/*

1) The above has no conflict with your series and therefore can go separately. Feel free to include it.

Quoted 10 lines
Quoted 10 lines
Quoted 10 lines
mm/vmscan.c +0 -1
index 99e1796eb833..b479ced26cd3 100644
@@ -1845,13 +1845,12 @@ static unsigned noinline_for_stack move_pages_to_lru(struct lruvec *lruvec,
 	int nr_pages, nr_moved = 0;
 	LIST_HEAD(pages_to_free);
 	struct page *page;
-	enum lru_list lru;
 
Quoted 9 lines
Quoted 9 lines

mm/vmscan.c static unsigned noinline_for_stack move_pages_to_lru(struct lruvec *lruvec, · line 1856

Quoted 9 lines
mm/vmscan.c +1 -1
 	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);

2) The above is the same change you've made.

Quoted 11 lines
Quoted 11 lines
Quoted 11 lines
mm/vmscan.c +1 -5
@@ -1860,16 +1859,10 @@ static unsigned noinline_for_stack move_pages_to_lru(struct lruvec *lruvec,
 		lruvec = mem_cgroup_page_lruvec(page, pgdat);
 
 		SetPageLRU(page);
-		lru = page_lru(page);
-
-		nr_pages = thp_nr_pages(page);
-		update_lru_size(lruvec, lru, page_zonenum(page), nr_pages);
-		list_move(&page->lru, &lruvec->lists[lru]);
+		add_page_to_lru_list(page, lruvec, page_lru(page));
 
Quoted 5 lines
Quoted 2 lines

mm/vmscan.c static unsigned noinline_for_stack move_pages_to_lru(struct lruvec *lruvec, · line 1870

Quoted 2 lines
mm/vmscan.c +0 -1
 		if (put_page_testzero(page)) {
-			__ClearPageLRU(page);

it's interesting to know the PageLRU left has no bad impact in real life. it justs seems a path confliction with my that patch.

(No, we can't leave PG_lru uncleared. It's done by the page_off_lru() right below).

Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
mm/vmscan.c +1 -2
-			__ClearPageActive(page);
-			del_page_from_lru_list(page, lruvec, lru);
+			del_page_from_lru_list(page, lruvec, page_off_lru(page));
 
Quoted 10 lines
Quoted 10 lines

mm/vmscan.c static unsigned noinline_for_stack move_pages_to_lru(struct lruvec *lruvec, · line 1877

Quoted 10 lines
mm/vmscan.c +1 -0
 			if (unlikely(PageCompound(page))) {
 				spin_unlock_irq(&pgdat->lru_lock);
@@ -1878,6 +1871,7 @@ static unsigned noinline_for_stack move_pages_to_lru(struct lruvec *lruvec,
 			} else
 				list_add(&page->lru, &pages_to_free);
 		} else {
+			nr_pages = thp_nr_pages(page);
 			nr_moved += nr_pages;
 			if (PageActive(page))
 				workingset_age_nonresident(lruvec, nr_pages);

3) The above is the conflict. Since we won't add and then delete a page after your patch, we won't need it. But I do want this (you've snipped from your reply, so I take that you already figured it out):

- lru = page_lru(page);
-
- nr_pages = thp_nr_pages(page);
- update_lru_size(lruvec, lru, page_zonenum(page), nr_pages);
- list_move(&page->lru, &lruvec->lists[lru]);
+ add_page_to_lru_list(page, lruvec, page_lru(page));

No worries if you prefer this to go separately too.

author
#5 lore raw
Quoted 81 lines · Yu Zhao
Quoted 20 lines · Alex Shi
Quoted 17 lines · Yu Zhao
Quoted 6 lines · Yu Zhao

This is a trivial but worth having clean-up patch. There should be no side effects except page->lru is temporarily poisoned after it's deleted but before it's added to the new list in move_pages_to_lru() (which is not a problem).

Hi Alex, I just realized your
[v18,08/32] mm/vmscan: remove unnecessary lruvec adding
at
https://patchwork.kernel.org/patch/11733123/
also touches move_pages_to_lru(). I agree it's better not to add a page we are going to free to the list in the first place. The rest in this patch would be too trivial to be a separate one (on top of yours).

Quoted 2 lines
Quoted 2 lines

So would you mind taking of the clean-up too in your series? I'll drop this one then. Thanks.

Ok, maybe I wasn't clear before. Please see what exactly I'm asking you to consider below.

Quoted 11 lines
Quoted 11 lines
Quoted 11 lines
mm/swap.c +0 -1
index 40bf20a75278..2735ecf0f566 100644
@@ -597,11 +597,9 @@ static void lru_lazyfree_fn(struct page *page, struct lruvec *lruvec,
 {
 	if (PageLRU(page) && PageAnon(page) && PageSwapBacked(page) &&
 	    !PageSwapCache(page) && !PageUnevictable(page)) {
-		bool active = PageActive(page);
 		int nr_pages = thp_nr_pages(page);
 
Quoted 6 lines
Quoted 6 lines

mm/swap.c static void lru_lazyfree_fn(struct page *page, struct lruvec *lruvec, · line 605

Quoted 6 lines
mm/swap.c +1 -2
-		del_page_from_lru_list(page, lruvec,
-				       LRU_INACTIVE_ANON + active);
+		del_page_from_lru_list(page, lruvec, page_lru(page));
 		ClearPageActive(page);
 		ClearPageReferenced(page);
 		/*

1) The above has no conflict with your series and therefore can go separately. Feel free to include it.

Quoted 10 lines
Quoted 10 lines
Quoted 10 lines
mm/vmscan.c +0 -1
index 99e1796eb833..b479ced26cd3 100644
@@ -1845,13 +1845,12 @@ static unsigned noinline_for_stack move_pages_to_lru(struct lruvec *lruvec,
 	int nr_pages, nr_moved = 0;
 	LIST_HEAD(pages_to_free);
 	struct page *page;
-	enum lru_list lru;
 
Quoted 9 lines
Quoted 9 lines

mm/vmscan.c static unsigned noinline_for_stack move_pages_to_lru(struct lruvec *lruvec, · line 1856

Quoted 9 lines
mm/vmscan.c +1 -1
 	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);

2) The above is the same change you've made.

Quoted 11 lines
Quoted 11 lines
Quoted 11 lines
mm/vmscan.c +1 -5
@@ -1860,16 +1859,10 @@ static unsigned noinline_for_stack move_pages_to_lru(struct lruvec *lruvec,
 		lruvec = mem_cgroup_page_lruvec(page, pgdat);
 
 		SetPageLRU(page);
-		lru = page_lru(page);
-
-		nr_pages = thp_nr_pages(page);
-		update_lru_size(lruvec, lru, page_zonenum(page), nr_pages);
-		list_move(&page->lru, &lruvec->lists[lru]);
+		add_page_to_lru_list(page, lruvec, page_lru(page));
 
Quoted 8 lines
Quoted 5 lines
Quoted 2 lines

mm/vmscan.c static unsigned noinline_for_stack move_pages_to_lru(struct lruvec *lruvec, · line 1870

Quoted 2 lines
mm/vmscan.c +0 -1
 		if (put_page_testzero(page)) {
-			__ClearPageLRU(page);

it's interesting to know the PageLRU left has no bad impact in real life. it justs seems a path confliction with my that patch.

(No, we can't leave PG_lru uncleared. It's done by the page_off_lru() right below).

Sorry about this false claim. You are right: we need to keep __ClearPageLRU() here. page_off_lru() doesn't do it for us.

I'll fix this in v2. Thanks.