linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH v2 RESEND] sched/fair: Do not scan non-movable tasks several times
@ 2024-01-15 10:50 Konstantin Khorenko
  2024-01-19 15:49 ` Vincent Guittot
  0 siblings, 1 reply; 2+ messages in thread
From: Konstantin Khorenko @ 2024-01-15 10:50 UTC (permalink / raw)
  To: Vincent Guittot
  Cc: Ingo Molnar, Peter Zijlstra, Juri Lelli, Dietmar Eggemann,
	Steven Rostedt, Ben Segall, Mel Gorman,
	Daniel Bristot de Oliveira, Valentin Schneider,
	Alexander Atanasov, linux-kernel, Konstantin Khorenko

If busiest rq is small, nr_running < SCHED_NR_MIGRATE_BREAK and all
tasks are not movable, detach_tasks() should not iterate more than tasks
available in the busiest rq.

Before commit: b0defa7ae03e ("sched/fair: Make sure to try to detach at
least one movable task"), the (env->loop > env->loop_max) condition
prevented us from scanning non-movable tasks more than rq size times,
but after we start checking the LBF_ALL_PINNED flag, the "all tasks are
not movable" case is under threat.

Note: in case all tasks in the rq could not be moved in detach_tasks()
we always increase loop_break by SCHED_NR_MIGRATE_BREAK, so we can step
over loop_max, but i think it's a rare case and does not worth adding
here extra check for rq->nr_running overlimit.

Fixes: b0defa7ae03e ("sched/fair: Make sure to try to detach at least
one movable task")

Signed-off-by: Konstantin Khorenko <khorenko@virtuozzo.com>
---
 kernel/sched/fair.c | 13 +++++++++----
 1 file changed, 9 insertions(+), 4 deletions(-)

diff --git a/kernel/sched/fair.c b/kernel/sched/fair.c
index 533547e3c90a..920fb16e6e2f 100644
--- a/kernel/sched/fair.c
+++ b/kernel/sched/fair.c
@@ -11277,7 +11277,6 @@ static int load_balance(int this_cpu, struct rq *this_rq,
 		.dst_rq		= this_rq,
 		.dst_grpmask    = group_balance_mask(sd->groups),
 		.idle		= idle,
-		.loop_break	= SCHED_NR_MIGRATE_BREAK,
 		.cpus		= cpus,
 		.fbq_type	= all,
 		.tasks		= LIST_HEAD_INIT(env.tasks),
@@ -11324,6 +11323,14 @@ static int load_balance(int this_cpu, struct rq *this_rq,
 		 */
 		env.loop_max  = min(sysctl_sched_nr_migrate, busiest->nr_running);
 
+more_balance_reset_break:
+		/*
+		 * If busiest rq is small, nr_running < SCHED_NR_MIGRATE_BREAK
+		 * and all tasks are not movable, detach_tasks() should not
+		 * iterate more than tasks available in rq.
+		 */
+		env.loop_break = min(SCHED_NR_MIGRATE_BREAK, busiest->nr_running);
+
 more_balance:
 		rq_lock_irqsave(busiest, &rf);
 		update_rq_clock(busiest);
@@ -11386,13 +11393,12 @@ static int load_balance(int this_cpu, struct rq *this_rq,
 			env.dst_cpu	 = env.new_dst_cpu;
 			env.flags	&= ~LBF_DST_PINNED;
 			env.loop	 = 0;
-			env.loop_break	 = SCHED_NR_MIGRATE_BREAK;
 
 			/*
 			 * Go back to "more_balance" rather than "redo" since we
 			 * need to continue with same src_cpu.
 			 */
-			goto more_balance;
+			goto more_balance_reset_break;
 		}
 
 		/*
@@ -11418,7 +11424,6 @@ static int load_balance(int this_cpu, struct rq *this_rq,
 			 */
 			if (!cpumask_subset(cpus, env.dst_grpmask)) {
 				env.loop = 0;
-				env.loop_break = SCHED_NR_MIGRATE_BREAK;
 				goto redo;
 			}
 			goto out_all_pinned;
-- 
2.39.3


^ permalink raw reply related	[flat|nested] 2+ messages in thread

* Re: [PATCH v2 RESEND] sched/fair: Do not scan non-movable tasks several times
  2024-01-15 10:50 [PATCH v2 RESEND] sched/fair: Do not scan non-movable tasks several times Konstantin Khorenko
@ 2024-01-19 15:49 ` Vincent Guittot
  0 siblings, 0 replies; 2+ messages in thread
From: Vincent Guittot @ 2024-01-19 15:49 UTC (permalink / raw)
  To: Konstantin Khorenko
  Cc: Ingo Molnar, Peter Zijlstra, Juri Lelli, Dietmar Eggemann,
	Steven Rostedt, Ben Segall, Mel Gorman,
	Daniel Bristot de Oliveira, Valentin Schneider,
	Alexander Atanasov, linux-kernel

Le lundi 15 janv. 2024 à 13:50:52 (+0300), Konstantin Khorenko a écrit :
> If busiest rq is small, nr_running < SCHED_NR_MIGRATE_BREAK and all
> tasks are not movable, detach_tasks() should not iterate more than tasks
> available in the busiest rq.
> 
> Before commit: b0defa7ae03e ("sched/fair: Make sure to try to detach at
> least one movable task"), the (env->loop > env->loop_max) condition
> prevented us from scanning non-movable tasks more than rq size times,
> but after we start checking the LBF_ALL_PINNED flag, the "all tasks are
> not movable" case is under threat.
> 
> Note: in case all tasks in the rq could not be moved in detach_tasks()
> we always increase loop_break by SCHED_NR_MIGRATE_BREAK, so we can step
> over loop_max, but i think it's a rare case and does not worth adding
> here extra check for rq->nr_running overlimit.


In this case why not doing the below ? Close to your 1st version 

---
 kernel/sched/fair.c | 7 +++++++
 1 file changed, 7 insertions(+)

diff --git a/kernel/sched/fair.c b/kernel/sched/fair.c
index fce22b4462bb..1dae6cdf8561 100644
--- a/kernel/sched/fair.c
+++ b/kernel/sched/fair.c
@@ -11344,6 +11344,13 @@ static int load_balance(int this_cpu, struct rq *this_rq,
 		env.loop_max  = min(sysctl_sched_nr_migrate, busiest->nr_running);

 more_balance:
+		/*
+		 * If busiest rq is small, nr_running < SCHED_NR_MIGRATE_BREAK
+		 * and all tasks are not movable, detach_tasks() should not
+		 * iterate more than tasks available in rq.
+		 */
+		env.loop_break = min(env.loop_break, busiest->nr_running);
+
 		rq_lock_irqsave(busiest, &rf);
 		update_rq_clock(busiest);

--
2.34.1


> 
> Fixes: b0defa7ae03e ("sched/fair: Make sure to try to detach at least
> one movable task")
> 
> Signed-off-by: Konstantin Khorenko <khorenko@virtuozzo.com>
> ---
>  kernel/sched/fair.c | 13 +++++++++----
>  1 file changed, 9 insertions(+), 4 deletions(-)
> 
> diff --git a/kernel/sched/fair.c b/kernel/sched/fair.c
> index 533547e3c90a..920fb16e6e2f 100644
> --- a/kernel/sched/fair.c
> +++ b/kernel/sched/fair.c
> @@ -11277,7 +11277,6 @@ static int load_balance(int this_cpu, struct rq *this_rq,
>  		.dst_rq		= this_rq,
>  		.dst_grpmask    = group_balance_mask(sd->groups),
>  		.idle		= idle,
> -		.loop_break	= SCHED_NR_MIGRATE_BREAK,
>  		.cpus		= cpus,
>  		.fbq_type	= all,
>  		.tasks		= LIST_HEAD_INIT(env.tasks),
> @@ -11324,6 +11323,14 @@ static int load_balance(int this_cpu, struct rq *this_rq,
>  		 */
>  		env.loop_max  = min(sysctl_sched_nr_migrate, busiest->nr_running);
>  
> +more_balance_reset_break:
> +		/*
> +		 * If busiest rq is small, nr_running < SCHED_NR_MIGRATE_BREAK
> +		 * and all tasks are not movable, detach_tasks() should not
> +		 * iterate more than tasks available in rq.
> +		 */
> +		env.loop_break = min(SCHED_NR_MIGRATE_BREAK, busiest->nr_running);
> +
>  more_balance:
>  		rq_lock_irqsave(busiest, &rf);
>  		update_rq_clock(busiest);
> @@ -11386,13 +11393,12 @@ static int load_balance(int this_cpu, struct rq *this_rq,
>  			env.dst_cpu	 = env.new_dst_cpu;
>  			env.flags	&= ~LBF_DST_PINNED;
>  			env.loop	 = 0;
> -			env.loop_break	 = SCHED_NR_MIGRATE_BREAK;
>  
>  			/*
>  			 * Go back to "more_balance" rather than "redo" since we
>  			 * need to continue with same src_cpu.
>  			 */
> -			goto more_balance;
> +			goto more_balance_reset_break;
>  		}
>  
>  		/*
> @@ -11418,7 +11424,6 @@ static int load_balance(int this_cpu, struct rq *this_rq,
>  			 */
>  			if (!cpumask_subset(cpus, env.dst_grpmask)) {
>  				env.loop = 0;
> -				env.loop_break = SCHED_NR_MIGRATE_BREAK;
>  				goto redo;
>  			}
>  			goto out_all_pinned;
> -- 
> 2.39.3
> 

^ permalink raw reply related	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2024-01-19 15:49 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-01-15 10:50 [PATCH v2 RESEND] sched/fair: Do not scan non-movable tasks several times Konstantin Khorenko
2024-01-19 15:49 ` Vincent Guittot

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).