mm/vmscan: __isolate_lru_page_prepare clean up
Subsystems (5)
PATCH 1/1 mm/vmscan: __isolate_lru_page_prepare clean up
The function just return 2 results, so use a 'switch' to deal with its result is unnecessary, and simplify it to a bool func as Vlastimil suggested.
Also removed 'goto' in using by reusing list_move().
--- mm/compaction.c | 2 +- mm/vmscan.c | 53 ++++++++++++++++++++++++------------------------- 2 files changed, 27 insertions(+), 28 deletions(-)
mm/compaction.c +1 -1
@@ -988,7 +988,7 @@ isolate_migratepages_block(struct compact_control *cc, unsigned long low_pfn,if (unlikely (!get_page_unless_zero (page))) goto isolate_fail; -if (__isolate_lru_page_prepare (page, isolate_mode) !=0 ) +if (!__isolate_lru_page_prepare (page, isolate_mode)) goto isolate_fail_put;/* Try isolate the page */
mm/vmscan.c +26 -27
75 lines not on this page · raw message
Signature · 1 line
2.29.GIT
Andrew Morton
maintainerQuoted 20 lines · On Fri, 20 Nov 2020 16:03:33 +0800 Alex Shi <[email protected]> wrote:
The function just return 2 results, so use a 'switch' to deal with its result is unnecessary, and simplify it to a bool func as Vlastimil suggested.
Also removed 'goto' in using by reusing
list_move()....
mm/vmscan.c +1
-1@@ -1540,7 +1540,7 @@ unsigned int reclaim_clean_pages_from_list(struct zone *zone,*/ int __isolate_lru_page_prepare (struct page *page,isolate_mode_t mode) { -int ret = -EBUSY ; +int ret = false;/* Only take pages on the LRU. */ if (!PageLRU (page))
mm/vmscan.c int __isolate_lru_page_prepare(struct page *page, isolate_mode_t mode) · line 1594
Quoted 6 lines
mm/vmscan.c +1
-1@@ -1590,7 +1590,7 @@ int __isolate_lru_page_prepare(struct page *page, isolate_mode_t mode)if ((mode &ISOLATE_UNMAPPED ) &&page_mapped (page))return ret; -return 0 ; +return true; }
The resulting __isolate_lru_page_prepare() is rather unpleasing.
- Why return an int and not a bool?
- int ret = false' is a big hint that ret' should have bool type!
- Why not just remove ret' and do return false' in all those `return ret' places?
- The __isolate_lru_page_prepare() kerneldoc still says "returns 0 on success, -ve errno on failure".
Quoted 33 lines · Andrew Morton
Quoted 20 lines · On Fri, 20 Nov 2020 16:03:33 +0800 Alex Shi <[email protected]> wrote:
The function just return 2 results, so use a 'switch' to deal with its result is unnecessary, and simplify it to a bool func as Vlastimil suggested.
Also removed 'goto' in using by reusing
list_move()....
mm/vmscan.c +1
-1@@ -1540,7 +1540,7 @@ unsigned int reclaim_clean_pages_from_list(struct zone *zone,*/ int __isolate_lru_page_prepare (struct page *page,isolate_mode_t mode) { -int ret = -EBUSY ; +int ret = false;/* Only take pages on the LRU. */ if (!PageLRU (page))mm/vmscan.c int __isolate_lru_page_prepare(struct page *page, isolate_mode_t mode) · line 1594
Quoted 6 lines
mm/vmscan.c +1
-1@@ -1590,7 +1590,7 @@ int __isolate_lru_page_prepare(struct page *page, isolate_mode_t mode)if ((mode &ISOLATE_UNMAPPED ) &&page_mapped (page))return ret; -return 0 ; +return true; }The resulting
__isolate_lru_page_prepare()is rather unpleasing.- Why return an int and not a bool?
Quoted 8 lines
-
int ret = false' is a big hint thatret' should have bool type!- Why not just remove
ret' and doreturn false' in all those `return ret' places?- The
__isolate_lru_page_prepare()kerneldoc still says "returns 0 on success, -ve errno on failure".
Hi Andrew,
Thanks a lot for caching and sorry for the bad patch.
It initially a 'int' version, and change it to bool in a hurry weekend. I am sorry.
From 36c4fbda2d55633d3c1a3e79f045cd9877453ab7 Mon Sep 17 00:00:00 2001
From: Alex Shi <[email protected]>
Date: Fri, 20 Nov 2020 14:49:16 +0800
Subject: [PATCH v2 next] mm/vmscan: __isolate_lru_page_prepare clean up
The function just return 2 results, so use a 'switch' to deal with its result is unnecessary, and simplify it to a bool func as Vlastimil suggested.
Also remove 'goto' by reusing list_move().
--- mm/compaction.c | 2 +- mm/vmscan.c | 69 +++++++++++++++++++++++-------------------------- 2 files changed, 34 insertions(+), 37 deletions(-)
mm/compaction.c +1 -1
@@ -988,7 +988,7 @@ isolate_migratepages_block(struct compact_control *cc, unsigned long low_pfn,if (unlikely (!get_page_unless_zero (page))) goto isolate_fail; -if (__isolate_lru_page_prepare (page, isolate_mode) !=0 ) +if (!__isolate_lru_page_prepare (page, isolate_mode)) goto isolate_fail_put;/* Try isolate the page */
mm/vmscan.c +33 -36
114 lines not on this page · raw message
Signature · 1 line
2.29.GIT
Quoted 3 lines · Alex Shi
mm/compaction.c | 2 +- mm/vmscan.c | 69 +++++++++++++++++++++++-------------------------- 2 files changed, 34 insertions(+), 37 deletions(-)
How is it possible you're changing the signature of a function without touching a header file? Surely __isolate_lru_page_prepare() must be declared in mm/internal.h ?
mm/vmscan.c unsigned int reclaim_clean_pages_from_list(struct zone *zone, · line 1539
Quoted 7 lines
+++
b/mm/vmscan.cmm/vmscan.c +1
-1@@ -1536,19 +1536,17 @@ unsigned int reclaim_clean_pages_from_list(struct zone *zone,* page: page to consider * mode: one of the LRU isolation modes defined above * -* returns 0 on success, -ve errno on failure. +* returns ture on success, false on failure.
"true".
mm/vmscan.c static unsigned long isolate_lru_pages(unsigned long nr_to_scan, · line 1676
Quoted 8 lines
mm/vmscan.c +2
-2@@ -1674,35 +1672,34 @@ static unsigned long isolate_lru_pages(unsigned long nr_to_scan,* only when the page is being freed somewhere else. */ scan += nr_pages; -switch (__isolate_lru_page_prepare (page, mode)) { -case 0 : +if (!__isolate_lru_page_prepare (page, mode)) { +/* else it is being freed elsewhere */
I don't think the word "else" helps here. Just
/* It is being freed elsewhere */
mm/vmscan.c static unsigned long isolate_lru_pages(unsigned long nr_to_scan, · line 1694
Quoted 5 lines
mm/vmscan.c +3
-0+if (!TestClearPageLRU (page)) {/* +* This page may in other isolation path, +* but we still hold lru_lock. */
I don't think this comment helps me understand what's going on here. Maybe:
/* Another thread is already isolating this page */
Quoted 4 lines
mm/vmscan.c +2
-0+put_page (page);list_move (&page->lru, src); +continue ; }
Quoted 40 lines · Matthew Wilcox
Quoted 3 lines · Alex Shi
mm/compaction.c | 2 +- mm/vmscan.c | 69 +++++++++++++++++++++++-------------------------- 2 files changed, 34 insertions(+), 37 deletions(-)How is it possible you're changing the signature of a function without touching a header file? Surely
__isolate_lru_page_prepare()must be declared inmm/internal.h?Review unsigned int reclaim_clean_pages_from_list(struct zone *zone, · line 1539
Quoted 7 lines
+++
b/mm/vmscan.c(quoted diff) +1
-1@@ -1536,19 +1536,17 @@ unsigned int reclaim_clean_pages_from_list(struct zone *zone, * page: page to consider * mode: one of the LRU isolation modes defined above * - * returns 0 on success, -ve errno on failure. + * returns ture on success, false on failure."true".
Review static unsigned long isolate_lru_pages(unsigned long nr_to_scan, · line 1676
Quoted 8 lines
(quoted diff) +2
-2@@ -1674,35 +1672,34 @@ static unsigned long isolate_lru_pages(unsigned long nr_to_scan, * only when the page is being freed somewhere else. */ scan += nr_pages; - switch (__isolate_lru_page_prepare(page, mode)) { - case 0: + if (!__isolate_lru_page_prepare(page, mode)) { + /* else it is being freed elsewhere */I don't think the word "else" helps here. Just
/* It is being freed elsewhere */Review static unsigned long isolate_lru_pages(unsigned long nr_to_scan, · line 1681
Quoted 5 lines
(quoted diff) +3
-0+ if (!TestClearPageLRU(page)) { /* + * This page may in other isolation path, + * but we still hold lru_lock. */I don't think this comment helps me understand what's going on here. Maybe:
Quoted 6 lines
/* Another thread is already isolating this page */Review static unsigned long isolate_lru_pages(unsigned long nr_to_scan, · line 1685
Quoted 4 lines
(quoted diff) +2
-0+ put_page(page); list_move(&page->lru, src); + continue; }
Hi Matthew,
Thanks a lot for all comments, I picked all up and here is the v3:
From 167131dd106a96fd08af725df850e0da6ec899af Mon Sep 17 00:00:00 2001
From: Alex Shi <[email protected]>
Date: Fri, 20 Nov 2020 14:49:16 +0800
Subject: [PATCH v3 next] mm/vmscan: __isolate_lru_page_prepare clean up
The function just return 2 results, so use a 'switch' to deal with its result is unnecessary, and simplify it to a bool func as Vlastimil suggested.
Also remove 'goto' by reusing list_move(), and take Matthew Wilcox's suggestion to update comments in function.
--- include/linux/swap.h | 2 +- mm/compaction.c | 2 +- mm/vmscan.c | 68 ++++++++++++++++++++------------------------ 3 files changed, 33 insertions(+), 39 deletions(-)
include/linux/swap.h +1 -1
@@ -356,7 +356,7 @@ extern void lru_cache_add_inactive_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_prepare (struct page *page,isolate_mode_t mode); +extern bool __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 +1 -1
@@ -988,7 +988,7 @@ isolate_migratepages_block(struct compact_control *cc, unsigned long low_pfn,if (unlikely (!get_page_unless_zero (page))) goto isolate_fail; -if (__isolate_lru_page_prepare (page, isolate_mode) !=0 ) +if (!__isolate_lru_page_prepare (page, isolate_mode)) goto isolate_fail_put;/* Try isolate the page */
mm/vmscan.c +31 -37
112 lines not on this page · raw message
Signature · 1 line
2.29.GIT
Vlastimil Babka
Acked-byQuoted 7 lines · Alex Shi
Thanks a lot for all comments, I picked all up and here is the v3:
From
167131dd106a96fd08af725df850e0da6ec899afMon Sep 17 00:00:00 2001
From: Alex Shi <[email protected]>
Date: Fri, 20 Nov 2020 14:49:16 +0800
Subject: [PATCH v3 next] mm/vmscan:__isolate_lru_page_prepareclean up
Quoted 6 lines
The function just return 2 results, so use a 'switch' to deal with its result is unnecessary, and simplify it to a bool func as Vlastimil suggested.
Also remove 'goto' by reusing
list_move(), and take Matthew Wilcox's suggestion to update comments in function.
I wouldn't mind if the goto stayed, but it's not repeating that much without it (list_move() + continue, 3 times) so...
Quoted 162 lines
Signed-off-by Alex Shi <[email protected]>Cc Andrew Morton <[email protected]>Cc Matthew Wilcox <[email protected]>Cc Hugh Dickins <[email protected]>Cc Yu Zhao <[email protected]>Cc Vlastimil Babka <[email protected]>Cc Michal Hocko <[email protected]>--- include/linux/swap.h | 2 +- mm/compaction.c | 2 +- mm/vmscan.c | 68 ++++++++++++++++++++------------------------ 3 files changed, 33 insertions(+), 39 deletions(-)include/linux/swap.h +1
-1@@ -356,7 +356,7 @@ extern void lru_cache_add_inactive_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_prepare (struct page *page,isolate_mode_t mode); +extern bool __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 +1
-1@@ -988,7 +988,7 @@ isolate_migratepages_block(struct compact_control *cc, unsigned long low_pfn,if (unlikely (!get_page_unless_zero (page))) goto isolate_fail; -if (__isolate_lru_page_prepare (page, isolate_mode) !=0 ) +if (!__isolate_lru_page_prepare (page, isolate_mode)) goto isolate_fail_put;/* Try isolate the page */ mm/vmscan.c +31
-37@@ -1536,19 +1536,17 @@ unsigned int reclaim_clean_pages_from_list(struct zone *zone,* page: page to consider * mode: one of the LRU isolation modes defined above * -* returns 0 on success, -ve errno on failure. +* returns true on success, false on failure. */ -int __isolate_lru_page_prepare (struct page *page,isolate_mode_t mode) +bool __isolate_lru_page_prepare (struct page *page,isolate_mode_t mode) { -int ret = -EBUSY ; -/* Only take pages on the LRU. */ if (!PageLRU (page)) -return ret; +return false;/* Compaction should not handle unevictable pages but CMA can do so */ if (PageUnevictable (page) && !(mode &ISOLATE_UNEVICTABLE )) -return ret; +return false;/* * To minimise LRU disruption, the caller can indicate that it only @@ -1561,7 +1559,7 @@ int __isolate_lru_page_prepare(struct page *page, isolate_mode_t mode)if (mode &ISOLATE_ASYNC_MIGRATE ) {/* All the caller can do on PageWriteback is block */ if (PageWriteback (page)) -return ret; +return false;if (PageDirty (page)) {struct address_space *mapping;@@ -1577,20 +1575,20 @@ int __isolate_lru_page_prepare(struct page *page, isolate_mode_t mode)* from the page cache. */ if (!trylock_page (page)) -return ret; +return false; mapping =page_mapping (page); migrate_dirty = !mapping || mapping->a_ops->migratepage;unlock_page (page);if (!migrate_dirty) -return ret; +return false; } }if ((mode &ISOLATE_UNMAPPED ) &&page_mapped (page)) -return ret; +return false; -return 0 ; +return true; }/* @@ -1674,35 +1672,31 @@ static unsigned long isolate_lru_pages(unsigned long nr_to_scan,* only when the page is being freed somewhere else. */ scan += nr_pages; -switch (__isolate_lru_page_prepare (page, mode)) { -case 0 : -/* -* 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 busy; - -if (!TestClearPageLRU (page)) { -/* -* This page may in other isolation path, -* but we still hold lru_lock. -*/ -put_page (page); - goto busy; - } - - nr_taken += nr_pages; - nr_zone_taken[page_zonenum (page)] += nr_pages; -list_move (&page->lru, dst); -break ; +if (!__isolate_lru_page_prepare (page, mode)) { +/* It is being freed elsewhere */ +list_move (&page->lru, src); +continue ; + } +/* +* 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))) { +list_move (&page->lru, src); +continue ; + } -default : -busy: -/* else it is being freed elsewhere */ +if (!TestClearPageLRU (page)) { +/* Another thread is already isolating this page */ +put_page (page);list_move (&page->lru, src); +continue ; } + + nr_taken += nr_pages; + nr_zone_taken[page_zonenum (page)] += nr_pages; +list_move (&page->lru, dst); }/*
Andrew Morton
maintainerQuoted 12 lines · On Tue, 24 Nov 2020 12:21:28 +0100 Vlastimil Babka <[email protected]> wrote:
Quoted 11 lines · Alex Shi
Thanks a lot for all comments, I picked all up and here is the v3:
From
167131dd106a96fd08af725df850e0da6ec899afMon Sep 17 00:00:00 2001
From: Alex Shi <[email protected]>
Date: Fri, 20 Nov 2020 14:49:16 +0800
Subject: [PATCH v3 next] mm/vmscan:__isolate_lru_page_prepareclean upThe function just return 2 results, so use a 'switch' to deal with its result is unnecessary, and simplify it to a bool func as Vlastimil suggested.
Quoted 5 lines
Quoted 2 lines
Also remove 'goto' by reusing
list_move(), and take Matthew Wilcox's suggestion to update comments in function.I wouldn't mind if the goto stayed, but it's not repeating that much without it (
list_move()+ continue, 3 times) so...
I tried that, and .text became significantly larger, for reasons which I didn't investigate ;)
Quoted 14 lines · Andrew Morton
Quoted 12 lines · On Tue, 24 Nov 2020 12:21:28 +0100 Vlastimil Babka <[email protected]> wrote:
Quoted 11 lines · Alex Shi
Thanks a lot for all comments, I picked all up and here is the v3:
From
167131dd106a96fd08af725df850e0da6ec899afMon Sep 17 00:00:00 2001
From: Alex Shi <[email protected]>
Date: Fri, 20 Nov 2020 14:49:16 +0800
Subject: [PATCH v3 next] mm/vmscan:__isolate_lru_page_prepareclean upThe function just return 2 results, so use a 'switch' to deal with its result is unnecessary, and simplify it to a bool func as Vlastimil suggested.
Quoted 9 lines
Quoted 5 lines
Quoted 2 lines
Also remove 'goto' by reusing
list_move(), and take Matthew Wilcox's suggestion to update comments in function.I wouldn't mind if the goto stayed, but it's not repeating that much without it (
list_move()+ continue, 3 times) so...I tried that, and .text became significantly larger, for reasons which I didn't investigate ;)
Uh, BTW, with the gcc 8.3.1 and centos 7, goto or continue version has same size on my side with or w/o DEBUG_LIST. But actually, this clean up patch could add 10 bytes also with or w/o DEDBUG_LIST.
Maybe related with different compiler?
Thanks
Alex
Quoted 17 lines · Alex Shi
Quoted 14 lines · Andrew Morton
Quoted 12 lines · On Tue, 24 Nov 2020 12:21:28 +0100 Vlastimil Babka <[email protected]> wrote:
Quoted 11 lines · Alex Shi
Thanks a lot for all comments, I picked all up and here is the v3:
From
167131dd106a96fd08af725df850e0da6ec899afMon Sep 17 00:00:00 2001
From: Alex Shi <[email protected]>
Date: Fri, 20 Nov 2020 14:49:16 +0800
Subject: [PATCH v3 next] mm/vmscan:__isolate_lru_page_prepareclean upThe function just return 2 results, so use a 'switch' to deal with its result is unnecessary, and simplify it to a bool func as Vlastimil suggested.
Quoted 7 lines
Quoted 7 lines
Quoted 4 lines
Quoted 2 lines
Also remove 'goto' by reusing
list_move(), and take Matthew Wilcox's suggestion to update comments in function.I wouldn't mind if the goto stayed, but it's not repeating that much without it (
list_move()+ continue, 3 times) so...I tried that, and .text became significantly larger, for reasons which I didn't investigate ;)
I found out that comparing whole .text doesn't often work as changes might be lost in alignment, or once in a while cross the alignment boundary and become exagerated. bloat-o-meter works nice though.
Quoted 5 lines
Uh, BTW, with the gcc 8.3.1 and centos 7, goto or continue version has same size on my side with or w/o
DEBUG_LIST. But actually, this clean up patch could add 10 bytes also with or w/oDEDBUG_LIST.Maybe related with different compiler?
gcc (SUSE Linux) 10.2.1 20201117 [revision 98ba03ffe0b9f37b4916ce6238fad754e00d720b]
./scripts/bloat-o-meter vmscan.o.before mm/vmscan.o add/remove: 0/0 grow/shrink: 0/1 up/down: 0/-1 (-1)
Function old new deltaisolate_lru_pages 1125 1124 -1
Total: Before=57283, After=57282, chg -0.00%
Not surprising, as I'd expect the compiler to figure out by itself that list_move + continue repeats and can be unified. The reason for goto to stay would be rather readability (subjective).
Quoted 3 lines
Thanks
Alex
Quoted 15 lines · Vlastimil Babka
Quoted 3 lines
Quoted 3 lines
I tried that, and .text became significantly larger, for reasons which I didn't investigate ;)
I found out that comparing whole .text doesn't often work as changes might be lost in alignment, or once in a while cross the alignment boundary and become exagerated. bloat-o-meter works nice though.
Quoted 5 lines
Uh, BTW, with the gcc 8.3.1 and centos 7, goto or continue version has same size on my side with or w/o
DEBUG_LIST. But actually, this clean up patch could add 10 bytes also with or w/oDEDBUG_LIST.Maybe related with different compiler?
gcc (SUSE Linux) 10.2.1 20201117 [revision
98ba03ffe0b9f37b4916ce6238fad754e00d720b]
Quoted 8 lines
./scripts/bloat-o-metervmscan.o.beforemm/vmscan.oadd/remove: 0/0 grow/shrink: 0/1 up/down: 0/-1 (-1) Function old new delta isolate_lru_pages 1125 1124 -1
Total: Before=57283, After=57282, chg -0.00%Not surprising, as I'd expect the compiler to figure out by itself that
list_move+ continue repeats and can be unified. The reason for goto to stay would be rather readability (subjective).
Hi Vlastimil,
Thanks for tool sharing! The gcc do give different.
My data is read from 'size' tool and isolate_lru_pages text size from 'objdump -d'. Maybe a same way like bloat-o-meter. :)
Thanks
Alex