mm/vmscan.c: no need to double-check if free pages are under high-watermark
Subsystems (3)
PATCH 1/1 mm/vmscan.c: no need to double-check if free pages are under high-watermark
From: Daero Lee <[email protected]>
In kswapd_try_to_sleep function, to check whether kswapd can sleep, the prepare_kswapd_sleep function is called twice.
If free pages are below high-watermark in the first call, the @remaining variable is not updated at 0 and the prepare_kswapd_sleep function is called for the second time.
I think it is necessary to set the initial value of the @remaining to a non-zero value to prevent consecutive calls to the same function.
--- mm/vmscan.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
mm/vmscan.c +2 -2
@@ -4331,7 +4331,7 @@ static int balance_pgdat(pg_data_t *pgdat, int order, int highest_zoneidx)/* * Return the order kswapd stopped reclaiming at as * prepare_kswapd_sleep() takes it into account. If another caller -* entered the allocator slow path while kswapd was awake, order will +* entered the allqocator slow path while kswapd was awake, order will * remain at the higher level. */ return sc.order;
@@ -4355,7 +4355,7 @@ static enum zone_type kswapd_highest_zoneidx(pg_data_t *pgdat,static void kswapd_try_to_sleep (pg_data_t *pgdat,int alloc_order,int reclaim_order,unsigned int highest_zoneidx) { -long remaining =0 ; +long remaining = ~0 ;DEFINE_WAIT (wait);if (freezing (current) ||kthread_should_stop ())
Signature · 1 line
2.25.1
Andrew Morton
maintainerQuoted 5 lines · On Sun, 2 Jan 2022 12:31:29 +0900 [email protected] wrote:
From: Daero Lee <[email protected]>
In
kswapd_try_to_sleepfunction, to check whether kswapd can sleep, theprepare_kswapd_sleepfunction is called twice.
Quoted 7 lines
If free pages are below high-watermark in the first call, the @remaining variable is not updated at 0 and the
prepare_kswapd_sleepfunction is called for the second time.I think it is necessary to set the initial value of the @remaining to a non-zero value to prevent consecutive calls to the same function.
Seems right. Mel & Joonsoo, could you please check?
mm/vmscan.c static int balance_pgdat(pg_data_t *pgdat, int order, int highest_zoneidx) · line 4334
Quoted 8 lines
mm/vmscan.c +1
-1@@ -4331,7 +4331,7 @@ static int balance_pgdat(pg_data_t *pgdat, int order, int highest_zoneidx)/* * Return the order kswapd stopped reclaiming at as * prepare_kswapd_sleep() takes it into account. If another caller -* entered the allocator slow path while kswapd was awake, order will +* entered the allqocator slow path while kswapd was awake, order will
whoops.
Quoted 12 lines
mm/vmscan.c +1
-1* remain at the higher level. */ return sc.order;@@ -4355,7 +4355,7 @@ static enum zone_type kswapd_highest_zoneidx(pg_data_t *pgdat,static void kswapd_try_to_sleep (pg_data_t *pgdat,int alloc_order,int reclaim_order,unsigned int highest_zoneidx) { -long remaining =0 ; +long remaining = ~0 ;DEFINE_WAIT (wait);if (freezing (current) ||kthread_should_stop ())
Quoted 23 lines · On Sun, Jan 02, 2022 at 12:31:29PM +0900, [email protected] wrote:
From: Daero Lee <[email protected]>
In
kswapd_try_to_sleepfunction, to check whether kswapd can sleep, theprepare_kswapd_sleepfunction is called twice.If free pages are below high-watermark in the first call, the @remaining variable is not updated at 0 and the
prepare_kswapd_sleepfunction is called for the second time.I think it is necessary to set the initial value of the @remaining to a non-zero value to prevent consecutive calls to the same function.
Signed-off-by Daero Lee <[email protected]>--- mm/vmscan.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)mm/vmscan.c +0
-0
mm/vmscan.c static int balance_pgdat(pg_data_t *pgdat, int order, int highest_zoneidx) · line 4337
Quoted 8 lines
mm/vmscan.c +1
-1@@ -4331,7 +4331,7 @@ static int balance_pgdat(pg_data_t *pgdat, int order, int highest_zoneidx)/* * Return the order kswapd stopped reclaiming at as * prepare_kswapd_sleep() takes it into account. If another caller -* entered the allocator slow path while kswapd was awake, order will +* entered the allqocator slow path while kswapd was awake, order will * remain at the higher level. */ return sc.order;
This hunk just adds a typo, drop it.
mm/vmscan.c static enum zone_type kswapd_highest_zoneidx(pg_data_t *pgdat, · line 4361
Quoted 9 lines
mm/vmscan.c +1
-1@@ -4355,7 +4355,7 @@ static enum zone_type kswapd_highest_zoneidx(pg_data_t *pgdat,static void kswapd_try_to_sleep (pg_data_t *pgdat,int alloc_order,int reclaim_order,unsigned int highest_zoneidx) { -long remaining =0 ; +long remaining = ~0 ;DEFINE_WAIT (wait);if (freezing (current) ||kthread_should_stop ())
While this does avoid calling prepare_kswapd_sleep() twice if the pgdat is balanced on the first try, it then does not restore the vmstat thresholds and doesn't call schedul() for kswapd to go to sleep.
I think you did spot a problem but I suspect you want something like the following untested patch
mm/vmscan.c +12 -6
@@ -4355,7 +4355,8 @@ static enum zone_type kswapd_highest_zoneidx(pg_data_t *pgdat,static void kswapd_try_to_sleep (pg_data_t *pgdat,int alloc_order,int reclaim_order,unsigned int highest_zoneidx) { -long remaining =0 ; +long remaining; +bool balanced;DEFINE_WAIT (wait);if (freezing (current) ||kthread_should_stop ())
@@ -4370,7 +4371,8 @@ static void kswapd_try_to_sleep(pg_data_t *pgdat, int alloc_order, int reclaim_o* eligible zone balanced that it's also unlikely that compaction will * succeed. */ -if (prepare_kswapd_sleep (pgdat, reclaim_order, highest_zoneidx)) { + balanced =prepare_kswapd_sleep (pgdat, reclaim_order, highest_zoneidx); +if (balanced) {/* * Compaction records what page blocks it recently failed to * isolate pages from and skips them in the future scanning.
@@ -4387,6 +4389,10 @@ static void kswapd_try_to_sleep(pg_data_t *pgdat, int alloc_order, int reclaim_o remaining =schedule_timeout (HZ /10 ); +/* Is pgdat balanced after a short sleep? */ + balanced =prepare_kswapd_sleep (pgdat, reclaim_order, + highest_zoneidx); +/* * If woken prematurely then reset kswapd_highest_zoneidx and * order. The values will either be from a wakeup request or
@@ -4406,11 +4412,11 @@ static void kswapd_try_to_sleep(pg_data_t *pgdat, int alloc_order, int reclaim_o }/* -* After a short sleep, check if it was a premature sleep. If not, then -* go fully to sleep until explicitly woken up. +* If balanced to the high watermark, restore vmstat thresholds and +* kswapd goes to sleep. If kswapd remains awake, account whether +* the low or high watermark was hit quickly. */ -if (!remaining && -prepare_kswapd_sleep (pgdat, reclaim_order, highest_zoneidx)) { +if (balanced) {trace_mm_vmscan_kswapd_sleep (pgdat->node_id);/*
Quoted 8 lines
Quoted 8 lines
mm/vmscan.c +1
-1@@ -4355,7 +4355,7 @@ static enum zone_type kswapd_highest_zoneidx(pg_data_t *pgdat,static void kswapd_try_to_sleep (pg_data_t *pgdat,int alloc_order,int reclaim_order,unsigned int highest_zoneidx) { -long remaining =0 ; +long remaining = ~0 ;DEFINE_WAIT (wait);
Quoted 5 lines
mm/vmscan.c static enum zone_type kswapd_highest_zoneidx(pg_data_t *pgdat, · line 4362
Quoted 1 line
mm/vmscan.c +0
-0if (freezing (current) ||kthread_should_stop ())While this does avoid calling
prepare_kswapd_sleep()twice if the pgdat is balanced on the first try, it then does not restore the vmstat thresholds and doesn't callschedul()for kswapd to go to sleep.
I intended not to call prepare_kswapd_sleep() twice when the pgdat is NOT balanced on the first try:)
Quoted 41 lines
mm/vmscan.c +8
-4@@ -4355,7 +4355,8 @@ static enum zone_type kswapd_highest_zoneidx(pg_data_t *pgdat,static void kswapd_try_to_sleep (pg_data_t *pgdat,int alloc_order,int reclaim_order,unsigned int highest_zoneidx) { -long remaining =0 ; +long remaining; +bool balanced;DEFINE_WAIT (wait);if (freezing (current) ||kthread_should_stop ())@@ -4370,7 +4371,8 @@ static void kswapd_try_to_sleep(pg_data_t *pgdat, int alloc_order, int reclaim_o* eligible zone balanced that it's also unlikely that compaction will * succeed. */ -if (prepare_kswapd_sleep (pgdat, reclaim_order, highest_zoneidx)) { + balanced =prepare_kswapd_sleep (pgdat, reclaim_order, highest_zoneidx); +if (balanced) {/* * Compaction records what page blocks it recently failed to * isolate pages from and skips them in the future scanning. @@ -4387,6 +4389,10 @@ static void kswapd_try_to_sleep(pg_data_t *pgdat, int alloc_order, int reclaim_o remaining =schedule_timeout (HZ /10 ); +/* Is pgdat balanced after a short sleep? */ + balanced =prepare_kswapd_sleep (pgdat, reclaim_order, + highest_zoneidx); +/* * If woken prematurely then reset kswapd_highest_zoneidx and * order. The values will either be from a wakeup request or @@ -4406,11 +4412,11 @@ static void kswapd_try_to_sleep(pg_data_t *pgdat, int alloc_order, int reclaim_o }/* -* After a short sleep, check if it was a premature sleep. If not, then -* go fully to sleep until explicitly woken up.
mm/vmscan.c static void kswapd_try_to_sleep(pg_data_t *pgdat, int alloc_order, int reclaim_o · line 4422
Quoted 10 lines
mm/vmscan.c +4
-2+* If balanced to the high watermark, restore vmstat thresholds and +* kswapd goes to sleep. If kswapd remains awake, account whether +* the low or high watermark was hit quickly. */ -if (!remaining && -prepare_kswapd_sleep (pgdat, reclaim_order, highest_zoneidx)) { +if (balanced) {trace_mm_vmscan_kswapd_sleep (pgdat->node_id);/*
But, I think what you did is more readable and nice. Thanks!
Quoted 7 lines
Quoted 7 lines
mm/vmscan.c +0
-0@@ -4331,7 +4331,7 @@ static int balance_pgdat(pg_data_t *pgdat, int order, int highest_zoneidx)/* * Return the order kswapd stopped reclaiming at as
Quoted 8 lines
mm/vmscan.c static int balance_pgdat(pg_data_t *pgdat, int order, int highest_zoneidx) · line 4337
Quoted 6 lines
mm/vmscan.c +1
-1* prepare_kswapd_sleep() takes it into account. If another caller -* entered the allocator slow path while kswapd was awake, order will +* entered the allqocator slow path while kswapd was awake, order will * remain at the higher level. */ return sc.order;This hunk just adds a typo, drop it.
Sorry about that;;
Quoted 8 lines · DaeRo Lee
Quoted 8 lines
Quoted 8 lines
mm/vmscan.c +1
-1@@ -4355,7 +4355,7 @@ static enum zone_type kswapd_highest_zoneidx(pg_data_t *pgdat,static void kswapd_try_to_sleep (pg_data_t *pgdat,int alloc_order,int reclaim_order,unsigned int highest_zoneidx) { -long remaining =0 ; +long remaining = ~0 ;DEFINE_WAIT (wait);
Quoted 9 lines
Quoted 5 lines
mm/vmscan.c static enum zone_type kswapd_highest_zoneidx(pg_data_t *pgdat, · line 4362
Quoted 1 line
mm/vmscan.c +0
-0if (freezing (current) ||kthread_should_stop ())While this does avoid calling
prepare_kswapd_sleep()twice if the pgdat is balanced on the first try, it then does not restore the vmstat thresholds and doesn't callschedul()for kswapd to go to sleep.I intended not to call
prepare_kswapd_sleep()twice when the pgdat is NOT balanced on the first try:)
Stupid typo on my part.
Quoted 15 lines
Quoted 15 lines
mm/vmscan.c +4
-4@@ -4406,11 +4412,11 @@ static void kswapd_try_to_sleep(pg_data_t *pgdat, int alloc_order, int reclaim_o }/* -* After a short sleep, check if it was a premature sleep. If not, then -* go fully to sleep until explicitly woken up. +* If balanced to the high watermark, restore vmstat thresholds and +* kswapd goes to sleep. If kswapd remains awake, account whether +* the low or high watermark was hit quickly. */ -if (!remaining && -prepare_kswapd_sleep (pgdat, reclaim_order, highest_zoneidx)) { +if (balanced) {trace_mm_vmscan_kswapd_sleep (pgdat->node_id);
Quoted 5 lines
Review static void kswapd_try_to_sleep(pg_data_t *pgdat, int alloc_order, int reclaim_o · line 4422
Quoted 1 line
(quoted diff) +0
-0/*But, I think what you did is more readable and nice. Thanks!
Feel free to pick it up, rerun your tests to ensure it's behaving as expected and resend! Include something in the changelog about user-visible effects if any (or a note saying that it reduces unnecssary overhead) and resend with me added to the cc.
Quoted 12 lines
Quoted 11 lines
Review static int balance_pgdat(pg_data_t *pgdat, int order, int highest_zoneidx) · line 4337
Quoted 9 lines
(quoted diff) +1
-1@@ -4331,7 +4331,7 @@ static int balance_pgdat(pg_data_t *pgdat, int order, int highest_zoneidx) /* * Return the order kswapd stopped reclaiming at as * prepare_kswapd_sleep() takes it into account. If another caller - * entered the allocator slow path while kswapd was awake, order will + * entered the allqocator slow path while kswapd was awake, order will * remain at the higher level. */ return sc.order;This hunk just adds a typo, drop it.
Quoted 1 line
Sorry about that;;
No need to be sorry, it happens :)
Thanks!
Signature · 2 lines
Mel Gorman SUSE Labs
Quoted 42 lines · Mel Gorman
Quoted 8 lines · DaeRo Lee
Quoted 8 lines
Quoted 8 lines
mm/vmscan.c +1
-1@@ -4355,7 +4355,7 @@ static enum zone_type kswapd_highest_zoneidx(pg_data_t *pgdat,static void kswapd_try_to_sleep (pg_data_t *pgdat,int alloc_order,int reclaim_order,unsigned int highest_zoneidx) { -long remaining =0 ; +long remaining = ~0 ;DEFINE_WAIT (wait);Quoted 9 lines
Quoted 5 lines
mm/vmscan.c static enum zone_type kswapd_highest_zoneidx(pg_data_t *pgdat, · line 4362
Quoted 1 line
mm/vmscan.c +0
-0if (freezing (current) ||kthread_should_stop ())While this does avoid calling
prepare_kswapd_sleep()twice if the pgdat is balanced on the first try, it then does not restore the vmstat thresholds and doesn't callschedul()for kswapd to go to sleep.I intended not to call
prepare_kswapd_sleep()twice when the pgdat is NOT balanced on the first try:)Stupid typo on my part.
Quoted 15 lines
Quoted 15 lines
mm/vmscan.c +4
-4@@ -4406,11 +4412,11 @@ static void kswapd_try_to_sleep(pg_data_t *pgdat, int alloc_order, int reclaim_o }/* -* After a short sleep, check if it was a premature sleep. If not, then -* go fully to sleep until explicitly woken up. +* If balanced to the high watermark, restore vmstat thresholds and +* kswapd goes to sleep. If kswapd remains awake, account whether +* the low or high watermark was hit quickly. */ -if (!remaining && -prepare_kswapd_sleep (pgdat, reclaim_order, highest_zoneidx)) { +if (balanced) {trace_mm_vmscan_kswapd_sleep (pgdat->node_id);Quoted 5 lines
Review static void kswapd_try_to_sleep(pg_data_t *pgdat, int alloc_order, int reclaim_o · line 4422
Quoted 1 line
(quoted diff) +0
-0/*But, I think what you did is more readable and nice. Thanks!
Quoted 5 lines
Feel free to pick it up, rerun your tests to ensure it's behaving as expected and resend! Include something in the changelog about user-visible effects if any (or a note saying that it reduces unnecssary overhead) and resend with me added to the cc.
Hi, All
Seems this thread stops here. I don't see following patch and current upstream doesn't include this change.
May I continue this? Of course, with author-ship from DaeRo Lee <[email protected]>.
Mel,
Would you mind suggesting some cases that I could do to see the effects from this change? Such as the overhead or throughput? Or what cases you expect?
Signature · 2 lines
Wei Yang Help you, Help me
Quoted 49 lines · Wei Yang
Quoted 42 lines · Mel Gorman
Quoted 8 lines · DaeRo Lee
Quoted 8 lines
Quoted 8 lines
mm/vmscan.c +1
-1@@ -4355,7 +4355,7 @@ static enum zone_type kswapd_highest_zoneidx(pg_data_t *pgdat,static void kswapd_try_to_sleep (pg_data_t *pgdat,int alloc_order,int reclaim_order,unsigned int highest_zoneidx) { -long remaining =0 ; +long remaining = ~0 ;DEFINE_WAIT (wait);Quoted 9 lines
Quoted 5 lines
mm/vmscan.c static enum zone_type kswapd_highest_zoneidx(pg_data_t *pgdat, · line 4362
Quoted 1 line
mm/vmscan.c +0
-0if (freezing (current) ||kthread_should_stop ())While this does avoid calling
prepare_kswapd_sleep()twice if the pgdat is balanced on the first try, it then does not restore the vmstat thresholds and doesn't callschedul()for kswapd to go to sleep.I intended not to call
prepare_kswapd_sleep()twice when the pgdat is NOT balanced on the first try:)Stupid typo on my part.
Quoted 15 lines
Quoted 15 lines
mm/vmscan.c +4
-4@@ -4406,11 +4412,11 @@ static void kswapd_try_to_sleep(pg_data_t *pgdat, int alloc_order, int reclaim_o }/* -* After a short sleep, check if it was a premature sleep. If not, then -* go fully to sleep until explicitly woken up. +* If balanced to the high watermark, restore vmstat thresholds and +* kswapd goes to sleep. If kswapd remains awake, account whether +* the low or high watermark was hit quickly. */ -if (!remaining && -prepare_kswapd_sleep (pgdat, reclaim_order, highest_zoneidx)) { +if (balanced) {trace_mm_vmscan_kswapd_sleep (pgdat->node_id);Quoted 5 lines
Review static void kswapd_try_to_sleep(pg_data_t *pgdat, int alloc_order, int reclaim_o · line 4422
Quoted 1 line
(quoted diff) +0
-0/*But, I think what you did is more readable and nice. Thanks!
Quoted 5 lines
Feel free to pick it up, rerun your tests to ensure it's behaving as expected and resend! Include something in the changelog about user-visible effects if any (or a note saying that it reduces unnecssary overhead) and resend with me added to the cc.
Quoted 7 lines
Hi, All
Seems this thread stops here. I don't see following patch and current upstream doesn't include this change.
May I continue this? Of course, with author-ship from DaeRo Lee <[email protected]>.
I've no objections. When I said "Feel free to pick it up", I meant that I was ok with you taking the patch and putting your team on it.
Quoted 5 lines
Mel,
Would you mind suggesting some cases that I could do to see the effects from this change? Such as the overhead or throughput? Or what cases you expect?
I don't have any suggestions on artificially triggering it. I had assumed you had encountered the bug in practice and had a test case but it would be ok to note that the patch is a theoretical fix based on code review.
Signature · 2 lines
Mel Gorman SUSE Labs