Skip to contentSkip to conversation

migrate disable fixes and performance

PATCH Superseded Scott Wood 4 participants 32 messages Jul 27, 2019 – Oct 10, 2019

Subsystems (4)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Scott Wood

author
#0 lore raw

cover migrate disable fixes and performance

With these patches, a kernel build on a 104-cpu machine took around 75% less wall time and 85% less system time. Note that there is a difference in v5.2-rt compared to v5.0-rt. The performance with these patches is similar in both cases, but without these patches v5.2-rt is substantially slower. In v5.0-rt with a previous version of these patches, lazy migrate disable reduced kernel build time by around 15-20% wall and 70-75% system.

Scott Wood (8):
sched: migrate_enable: Use sleeping_lock to indicate involuntary sleep
sched: __set_cpus_allowed_ptr: Check cpus_mask, not cpus_ptr
sched: Remove dead __migrate_disabled() check
sched: migrate disable: Protect cpus_ptr with lock sched/deadline: Reclaim cpuset bandwidth in .migrate_task_rq()
sched: migrate_enable: Set state to TASK_RUNNING
sched: migrate_enable: Use select_fallback_rq()
sched: Lazy migrate_disable processing

 include/linux/cpu.h      |   4 -
 include/linux/sched.h    |  15 ++--
 init/init_task.c         |   4 +
 kernel/cpu.c             |  97 ++++++++---------------
 kernel/rcu/tree_plugin.h |   2 +-
 kernel/sched/core.c      | 200 +++++++++++++++++++----------------------------
 kernel/sched/deadline.c  |  67 ++++++++--------
 kernel/sched/sched.h     |   4 +
 lib/smp_processor_id.c   |   3 +
 9 files changed, 166 insertions(+), 230 deletions(-)
Signature · 1 line
1.8.3.1
author
#1 lore raw

PATCH 1/8 sched: migrate_enable: Use sleeping_lock to indicate involuntary sleep

Without this, rcu_note_context_switch() will complain if an RCU read lock is held when migrate_enable() calls stop_one_cpu().

---
 include/linux/sched.h    | 4 ++--
 kernel/rcu/tree_plugin.h | 2 +-
 kernel/sched/core.c      | 2 ++
 3 files changed, 5 insertions(+), 3 deletions(-)
include/linux/sched.h +2 -2
index 4e218f8d8048..ad23ab939b35 100644
@@ -673,7 +673,7 @@ struct task_struct {
 	int				migrate_disable_atomic;
 # endif
 #endif
-#ifdef CONFIG_PREEMPT_RT_FULL
+#ifdef CONFIG_PREEMPT_RT_BASE
 	int				sleeping_lock;
 #endif
 
@@ -1873,7 +1873,7 @@ static __always_inline bool need_resched(void)
 	return unlikely(tif_need_resched());
 }
 
-#ifdef CONFIG_PREEMPT_RT_FULL
+#ifdef CONFIG_PREEMPT_RT_BASE
 static inline void sleeping_lock_inc(void)
 {
 	current->sleeping_lock++;
kernel/rcu/tree_plugin.h +1 -1
index 23a54e4b649c..7a3aa085ce2c 100644
@@ -292,7 +292,7 @@ void rcu_note_context_switch(bool preempt)
 	barrier(); /* Avoid RCU read-side critical sections leaking down. */
 	trace_rcu_utilization(TPS("Start context switch"));
 	lockdep_assert_irqs_disabled();
-#if defined(CONFIG_PREEMPT_RT_FULL)
+#if defined(CONFIG_PREEMPT_RT_BASE)
 	sleeping_l = t->sleeping_lock;
 #endif
 	WARN_ON_ONCE(!preempt && t->rcu_read_lock_nesting > 0 && !sleeping_l);
kernel/sched/core.c +2 -0
index d3c6542b306f..c3407707e367 100644
@@ -7405,7 +7405,9 @@ void migrate_enable(void)
 			unpin_current_cpu();
 			preempt_lazy_enable();
 			preempt_enable();
+			sleeping_lock_inc();
 			stop_one_cpu(task_cpu(p), migration_cpu_stop, &arg);
+			sleeping_lock_dec();
 			return;
 		}
 	}
Signature · 1 line
1.8.3.1
#2 lore raw

Re: [PATCH RT 1/8] sched: migrate_enable: Use sleeping_lock to indicate involuntary sleep

kernel/sched/core.c void migrate_enable(void) · line 7407

Quoted 13 lines · On Sat, 27 Jul 2019 00:56:31 -0500 Scott Wood <[email protected]> wrote:

Without this, rcu_note_context_switch() will complain if an RCU read lock is held when migrate_enable() calls stop_one_cpu().

index d3c6542b306f..c3407707e367 100644

kernel/sched/core.c +0 -0
@@ -7405,7 +7405,9 @@ void migrate_enable(void)
 			unpin_current_cpu();
 			preempt_lazy_enable();
 			preempt_enable();

This looks like it needs a comment to explain why this is.

-- Steve

Quoted 6 lines
kernel/sched/core.c +2 -0
+			sleeping_lock_inc();
 			stop_one_cpu(task_cpu(p), migration_cpu_stop, &arg);
+			sleeping_lock_dec();
 			return;
 		}
 	}
author
#3 lore raw

PATCH 2/8 sched: __set_cpus_allowed_ptr: Check cpus_mask, not cpus_ptr

In reply to Scott Wood

This function is concerned with the long-term cpu mask, not the transitory mask the task might have while migrate disabled. Before this patch, if a task was migrate disabled at the time __set_cpus_allowed_ptr() was called, and the new mask happened to be equal to the cpu that the task was running on, then the mask update would be lost.

---
 kernel/sched/core.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
kernel/sched/core.c +1 -1
index c3407707e367..6e643d656d71 100644
@@ -1218,7 +1218,7 @@ static int __set_cpus_allowed_ptr(struct task_struct *p,
 		goto out;
 	}
 
-	if (cpumask_equal(p->cpus_ptr, new_mask))
+	if (cpumask_equal(&p->cpus_mask, new_mask))
 		goto out;
 
 	if (!cpumask_intersects(new_mask, cpu_valid_mask)) {
Signature · 1 line
1.8.3.1
#4 lore raw

Re: [PATCH RT 2/8] sched: __set_cpus_allowed_ptr: Check cpus_mask, not cpus_ptr

Quoted 6 lines · Scott Wood

This function is concerned with the long-term cpu mask, not the transitory mask the task might have while migrate disabled. Before this patch, if a task was migrate disabled at the time __set_cpus_allowed_ptr() was called, and the new mask happened to be equal to the cpu that the task was running on, then the mask update would be lost.

lost as in "would not be carried out" I assume.

Quoted 10 lines
---
 kernel/sched/core.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
kernel/sched/core.c +0 -0
index c3407707e367..6e643d656d71 100644

kernel/sched/core.c static int __set_cpus_allowed_ptr(struct task_struct *p, · line 1224

Quoted 8 lines
kernel/sched/core.c +1 -1
@@ -1218,7 +1218,7 @@ static int __set_cpus_allowed_ptr(struct task_struct *p,
 		goto out;
 	}
 
-	if (cpumask_equal(p->cpus_ptr, new_mask))
+	if (cpumask_equal(&p->cpus_mask, new_mask))
 		goto out;
 
 	if (!cpumask_intersects(new_mask, cpu_valid_mask)) {

Sebastian

author
#5 lore raw

Re: [PATCH RT 2/8] sched: __set_cpus_allowed_ptr: Check cpus_mask, not cpus_ptr

Quoted 9 lines · Sebastian Andrzej Siewior
Quoted 6 lines · Scott Wood

This function is concerned with the long-term cpu mask, not the transitory mask the task might have while migrate disabled. Before this patch, if a task was migrate disabled at the time __set_cpus_allowed_ptr() was called, and the new mask happened to be equal to the cpu that the task was running on, then the mask update would be lost.

lost as in "would not be carried out" I assume.

Right. The old mask would be restored upon migrate_enable() even though that's no longer the policy that was requested.

-Scott

author
#6 lore raw

PATCH 3/8 sched: Remove dead __migrate_disabled() check

In reply to Scott Wood

This code was unreachable given the __migrate_disabled() branch to "out" immediately beforehand.

---
 kernel/sched/core.c | 7 -------
 1 file changed, 7 deletions(-)
kernel/sched/core.c +0 -7
index 6e643d656d71..99a3cfccf4d3 100644
@@ -1242,13 +1242,6 @@ static int __set_cpus_allowed_ptr(struct task_struct *p,
 	if (cpumask_test_cpu(task_cpu(p), new_mask) || __migrate_disabled(p))
 		goto out;
 
-#if defined(CONFIG_SMP) && defined(CONFIG_PREEMPT_RT_BASE)
-	if (__migrate_disabled(p)) {
-		p->migrate_disable_update = 1;
-		goto out;
-	}
-#endif
-
 	dest_cpu = cpumask_any_and(cpu_valid_mask, new_mask);
 	if (task_running(rq, p) || p->state == TASK_WAKING) {
 		struct migration_arg arg = { p, dest_cpu };
Signature · 1 line
1.8.3.1
author
#7 lore raw

PATCH 4/8 sched: migrate disable: Protect cpus_ptr with lock

In reply to Scott Wood

Various places assume that cpus_ptr is protected by rq/pi locks, so don't change it before grabbing those locks.

---
 kernel/sched/core.c | 6 ++----
 1 file changed, 2 insertions(+), 4 deletions(-)
kernel/sched/core.c +2 -4
index 99a3cfccf4d3..38a9a9df5638 100644
@@ -7283,9 +7283,8 @@ void dump_cpu_task(int cpu)
 	struct rq *rq;
 	struct rq_flags rf;
 
-	p->cpus_ptr = cpumask_of(smp_processor_id());
-
 	rq = task_rq_lock(p, &rf);
+	p->cpus_ptr = cpumask_of(smp_processor_id());
 	update_nr_migratory(p, -1);
 	p->nr_cpus_allowed = 1;
 	task_rq_unlock(rq, p, &rf);
@@ -7297,9 +7296,8 @@ void dump_cpu_task(int cpu)
 	struct rq *rq;
 	struct rq_flags rf;
 
-	p->cpus_ptr = &p->cpus_mask;
-
 	rq = task_rq_lock(p, &rf);
+	p->cpus_ptr = &p->cpus_mask;
 	p->nr_cpus_allowed = cpumask_weight(&p->cpus_mask);
 	update_nr_migratory(p, 1);
 	task_rq_unlock(rq, p, &rf);
Signature · 1 line
1.8.3.1
#8 lore raw

Re: [PATCH RT 4/8] sched: migrate disable: Protect cpus_ptr with lock

Quoted 4 lines · Scott Wood

Various places assume that cpus_ptr is protected by rq/pi locks, so don't change it before grabbing those locks.

I applied now everything until here and you can take a look at
https://git.kernel.org/pub/scm/linux/kernel/git/rt/linux-rt-devel.git/log/?h=linux-5.2.y-rt-RC

If there are no objections then I would make this a -rt9 and then go further.

Sebastian

author
#9 lore raw

Re: [PATCH RT 4/8] sched: migrate disable: Protect cpus_ptr with lock

Quoted 6 lines · Sebastian Andrzej Siewior
Quoted 4 lines · Scott Wood

Various places assume that cpus_ptr is protected by rq/pi locks, so don't change it before grabbing those locks.

Quoted 8 lines

I applied now everything until here and you can take a look at

https://git.kernel.org/pub/scm/linux/kernel/git/rt/linux-rt-devel.git/log/?h=linux-5.2.y-rt-RC

If there are no objections then I would make this a -rt9 and then go further.

Sebastian

Looks good, thanks!

-Scott

#10 lore raw

Re: [PATCH RT 4/8] sched: migrate disable: Protect cpus_ptr with lock

Quoted 1 line · Scott Wood

Looks good, thanks!

Thanks, just released.
Moving forward. It would be nice to have some DL-dev feedback on DL patch. For the remaining once, could please throw Steven's stress-test-hostplug-cpu-script? If that one does not complain I don't see a reason why not apply the patches (since they improve performance and do not break anything while doing so).

Quoted 1 line

-Scott

Sebastian

author
#11 lore raw

Re: [PATCH RT 4/8] sched: migrate disable: Protect cpus_ptr with lock

Quoted 9 lines · Sebastian Andrzej Siewior
Quoted 1 line · Scott Wood

Looks good, thanks!

Thanks, just released.
Moving forward. It would be nice to have some DL-dev feedback on DL patch. For the remaining once, could please throw Steven's stress-test-hostplug-cpu-script? If that one does not complain I don't see a reason why not apply the patches (since they improve performance and do not break anything while doing so).

I'd been using a quick-and-dirty script that does something similar, and ran it along with rcutorture, a kernel build, and something that randomly changes affinities -- though with these loads I have to ignore occasional RCU forward progress complaints that Paul said were expected with this version of the RCU code, XFS warnings that happen even on unmodified non-RT, and sometimes a transitory netdev timeout that is not that surprising given the heavy load and affinity stress (I get it without these patches as well, though it takes longer).

I just ran Steven's script (both alone and with the other stuff above) and saw no difference.

-Scott

author
#12 lore raw

PATCH 5/8 sched/deadline: Reclaim cpuset bandwidth in .migrate_task_rq()

In reply to Scott Wood

With the changes to migrate disabling, ->set_cpus_allowed() no longer gets deferred until migrate_enable(). To avoid releasing the bandwidth while the task may still be executing on the old CPU, move the subtraction to ->migrate_task_rq().

---
 kernel/sched/deadline.c | 67 +++++++++++++++++++++++--------------------------
 1 file changed, 31 insertions(+), 36 deletions(-)
kernel/sched/deadline.c +31 -36
index c18be51f7608..2f18d0cf1b56 100644
@@ -1606,14 +1606,42 @@ static void yield_task_dl(struct rq *rq)
 	return cpu;
 }
 
+static void free_old_cpuset_bw_dl(struct rq *rq, struct task_struct *p)
+{
+	struct root_domain *src_rd = rq->rd;
+
+	/*
+	 * Migrating a SCHED_DEADLINE task between exclusive
+	 * cpusets (different root_domains) entails a bandwidth
+	 * update. We already made space for us in the destination
+	 * domain (see cpuset_can_attach()).
+	 */
+	if (!cpumask_intersects(src_rd->span, p->cpus_ptr)) {
+		struct dl_bw *src_dl_b;
+
+		src_dl_b = dl_bw_of(cpu_of(rq));
+		/*
+		 * We now free resources of the root_domain we are migrating
+		 * off. In the worst case, sched_setattr() may temporary fail
+		 * until we complete the update.
+		 */
+		raw_spin_lock(&src_dl_b->lock);
+		__dl_sub(src_dl_b, p->dl.dl_bw, dl_bw_cpus(task_cpu(p)));
+		raw_spin_unlock(&src_dl_b->lock);
+	}
+}
+
 static void migrate_task_rq_dl(struct task_struct *p, int new_cpu __maybe_unused)
 {
 	struct rq *rq;
 
-	if (p->state != TASK_WAKING)
+	rq = task_rq(p);
+
+	if (p->state != TASK_WAKING) {
+		free_old_cpuset_bw_dl(rq, p);
 		return;
+	}
 
-	rq = task_rq(p);
 	/*
 	 * Since p->state == TASK_WAKING, set_task_cpu() has been called
 	 * from try_to_wake_up(). Hence, p->pi_lock is locked, but
@@ -2220,39 +2248,6 @@ static void task_woken_dl(struct rq *rq, struct task_struct *p)
 	}
 }
 
-static void set_cpus_allowed_dl(struct task_struct *p,
-				const struct cpumask *new_mask)
-{
-	struct root_domain *src_rd;
-	struct rq *rq;
-
-	BUG_ON(!dl_task(p));
-
-	rq = task_rq(p);
-	src_rd = rq->rd;
-	/*
-	 * Migrating a SCHED_DEADLINE task between exclusive
-	 * cpusets (different root_domains) entails a bandwidth
-	 * update. We already made space for us in the destination
-	 * domain (see cpuset_can_attach()).
-	 */
-	if (!cpumask_intersects(src_rd->span, new_mask)) {
-		struct dl_bw *src_dl_b;
-
-		src_dl_b = dl_bw_of(cpu_of(rq));
-		/*
-		 * We now free resources of the root_domain we are migrating
-		 * off. In the worst case, sched_setattr() may temporary fail
-		 * until we complete the update.
-		 */
-		raw_spin_lock(&src_dl_b->lock);
-		__dl_sub(src_dl_b, p->dl.dl_bw, dl_bw_cpus(task_cpu(p)));
-		raw_spin_unlock(&src_dl_b->lock);
-	}
-
-	set_cpus_allowed_common(p, new_mask);
-}
-
 /* Assumes rq->lock is held */
 static void rq_online_dl(struct rq *rq)
 {
@@ -2407,7 +2402,7 @@ static void prio_changed_dl(struct rq *rq, struct task_struct *p,
 #ifdef CONFIG_SMP
 	.select_task_rq		= select_task_rq_dl,
 	.migrate_task_rq	= migrate_task_rq_dl,
-	.set_cpus_allowed       = set_cpus_allowed_dl,
+	.set_cpus_allowed       = set_cpus_allowed_common,
 	.rq_online              = rq_online_dl,
 	.rq_offline             = rq_offline_dl,
 	.task_woken		= task_woken_dl,
Signature · 1 line
1.8.3.1
#13 lore raw

Re: [PATCH RT 5/8] sched/deadline: Reclaim cpuset bandwidth in .migrate_task_rq()

Quoted 4 lines · Scott Wood

With the changes to migrate disabling, ->set_cpus_allowed() no longer gets deferred until migrate_enable(). To avoid releasing the bandwidth while the task may still be executing on the old CPU, move the subtraction to ->migrate_task_rq().

I assume this is still valid for the !RT case.
Could someone from the scheduler/DL department please comment on this one?

Quoted 106 lines
---
 kernel/sched/deadline.c | 67 +++++++++++++++++++++++--------------------------
 1 file changed, 31 insertions(+), 36 deletions(-)
kernel/sched/deadline.c +31 -36
index c18be51f7608..2f18d0cf1b56 100644
@@ -1606,14 +1606,42 @@ static void yield_task_dl(struct rq *rq)
 	return cpu;
 }
 
+static void free_old_cpuset_bw_dl(struct rq *rq, struct task_struct *p)
+{
+	struct root_domain *src_rd = rq->rd;
+
+	/*
+	 * Migrating a SCHED_DEADLINE task between exclusive
+	 * cpusets (different root_domains) entails a bandwidth
+	 * update. We already made space for us in the destination
+	 * domain (see cpuset_can_attach()).
+	 */
+	if (!cpumask_intersects(src_rd->span, p->cpus_ptr)) {
+		struct dl_bw *src_dl_b;
+
+		src_dl_b = dl_bw_of(cpu_of(rq));
+		/*
+		 * We now free resources of the root_domain we are migrating
+		 * off. In the worst case, sched_setattr() may temporary fail
+		 * until we complete the update.
+		 */
+		raw_spin_lock(&src_dl_b->lock);
+		__dl_sub(src_dl_b, p->dl.dl_bw, dl_bw_cpus(task_cpu(p)));
+		raw_spin_unlock(&src_dl_b->lock);
+	}
+}
+
 static void migrate_task_rq_dl(struct task_struct *p, int new_cpu __maybe_unused)
 {
 	struct rq *rq;
 
-	if (p->state != TASK_WAKING)
+	rq = task_rq(p);
+
+	if (p->state != TASK_WAKING) {
+		free_old_cpuset_bw_dl(rq, p);
 		return;
+	}
 
-	rq = task_rq(p);
 	/*
 	 * Since p->state == TASK_WAKING, set_task_cpu() has been called
 	 * from try_to_wake_up(). Hence, p->pi_lock is locked, but
@@ -2220,39 +2248,6 @@ static void task_woken_dl(struct rq *rq, struct task_struct *p)
 	}
 }
 
-static void set_cpus_allowed_dl(struct task_struct *p,
-				const struct cpumask *new_mask)
-{
-	struct root_domain *src_rd;
-	struct rq *rq;
-
-	BUG_ON(!dl_task(p));
-
-	rq = task_rq(p);
-	src_rd = rq->rd;
-	/*
-	 * Migrating a SCHED_DEADLINE task between exclusive
-	 * cpusets (different root_domains) entails a bandwidth
-	 * update. We already made space for us in the destination
-	 * domain (see cpuset_can_attach()).
-	 */
-	if (!cpumask_intersects(src_rd->span, new_mask)) {
-		struct dl_bw *src_dl_b;
-
-		src_dl_b = dl_bw_of(cpu_of(rq));
-		/*
-		 * We now free resources of the root_domain we are migrating
-		 * off. In the worst case, sched_setattr() may temporary fail
-		 * until we complete the update.
-		 */
-		raw_spin_lock(&src_dl_b->lock);
-		__dl_sub(src_dl_b, p->dl.dl_bw, dl_bw_cpus(task_cpu(p)));
-		raw_spin_unlock(&src_dl_b->lock);
-	}
-
-	set_cpus_allowed_common(p, new_mask);
-}
-
 /* Assumes rq->lock is held */
 static void rq_online_dl(struct rq *rq)
 {
@@ -2407,7 +2402,7 @@ static void prio_changed_dl(struct rq *rq, struct task_struct *p,
 #ifdef CONFIG_SMP
 	.select_task_rq		= select_task_rq_dl,
 	.migrate_task_rq	= migrate_task_rq_dl,
-	.set_cpus_allowed       = set_cpus_allowed_dl,
+	.set_cpus_allowed       = set_cpus_allowed_common,
 	.rq_online              = rq_online_dl,
 	.rq_offline             = rq_offline_dl,
 	.task_woken		= task_woken_dl,
Signature · 1 line
1.8.3.1

Sebastian

maintainer
#14 lore raw

Re: [PATCH RT 5/8] sched/deadline: Reclaim cpuset bandwidth in .migrate_task_rq()

In reply to Scott Wood

Hi Scott,

Quoted 42 lines · Scott Wood

With the changes to migrate disabling, ->set_cpus_allowed() no longer gets deferred until migrate_enable(). To avoid releasing the bandwidth while the task may still be executing on the old CPU, move the subtraction to ->migrate_task_rq().

---
 kernel/sched/deadline.c | 67 +++++++++++++++++++++++--------------------------
 1 file changed, 31 insertions(+), 36 deletions(-)
kernel/sched/deadline.c +24 -0
index c18be51f7608..2f18d0cf1b56 100644
@@ -1606,14 +1606,42 @@ static void yield_task_dl(struct rq *rq)
 	return cpu;
 }
 
+static void free_old_cpuset_bw_dl(struct rq *rq, struct task_struct *p)
+{
+	struct root_domain *src_rd = rq->rd;
+
+	/*
+	 * Migrating a SCHED_DEADLINE task between exclusive
+	 * cpusets (different root_domains) entails a bandwidth
+	 * update. We already made space for us in the destination
+	 * domain (see cpuset_can_attach()).
+	 */
+	if (!cpumask_intersects(src_rd->span, p->cpus_ptr)) {
+		struct dl_bw *src_dl_b;
+
+		src_dl_b = dl_bw_of(cpu_of(rq));
+		/*
+		 * We now free resources of the root_domain we are migrating
+		 * off. In the worst case, sched_setattr() may temporary fail
+		 * until we complete the update.
+		 */
+		raw_spin_lock(&src_dl_b->lock);
+		__dl_sub(src_dl_b, p->dl.dl_bw, dl_bw_cpus(task_cpu(p)));
+		raw_spin_unlock(&src_dl_b->lock);
+	}
+}

kernel/sched/deadline.c static void yield_task_dl(struct rq *rq) · line 1641

Quoted 10 lines
kernel/sched/deadline.c +5 -1
+
 static void migrate_task_rq_dl(struct task_struct *p, int new_cpu __maybe_unused)
 {
 	struct rq *rq;
 
-	if (p->state != TASK_WAKING)
+	rq = task_rq(p);
+
+	if (p->state != TASK_WAKING) {
+		free_old_cpuset_bw_dl(rq, p);

What happens if a DEADLINE task is moved between cpusets while it was sleeping? Don't we miss removing from the old cpuset if the task gets migrated on wakeup?

Thanks,

Juri

author
#15 lore raw

Re: [PATCH RT 5/8] sched/deadline: Reclaim cpuset bandwidth in .migrate_task_rq()

Quoted 54 lines · Juri Lelli

Hi Scott,

Quoted 51 lines · Scott Wood

With the changes to migrate disabling, ->set_cpus_allowed() no longer gets deferred until migrate_enable(). To avoid releasing the bandwidth while the task may still be executing on the old CPU, move the subtraction
to ->migrate_task_rq().

---
 kernel/sched/deadline.c | 67 +++++++++++++++++++++++-------------------
-------
 1 file changed, 31 insertions(+), 36 deletions(-)
kernel/sched/deadline.c +17 -0
index c18be51f7608..2f18d0cf1b56 100644
@@ -1606,14 +1606,42 @@ static void yield_task_dl(struct rq *rq)
 	return cpu;
 }
 
+static void free_old_cpuset_bw_dl(struct rq *rq, struct task_struct *p)
+{
+	struct root_domain *src_rd = rq->rd;
+
+	/*
+	 * Migrating a SCHED_DEADLINE task between exclusive
+	 * cpusets (different root_domains) entails a bandwidth
+	 * update. We already made space for us in the destination
+	 * domain (see cpuset_can_attach()).
+	 */
+	if (!cpumask_intersects(src_rd->span, p->cpus_ptr)) {
+		struct dl_bw *src_dl_b;
+
+		src_dl_b = dl_bw_of(cpu_of(rq));
+		/*
+		 * We now free resources of the root_domain we are migrating
+		 * off. In the worst case, sched_setattr() may temporary

fail

kernel/sched/deadline.c +8 -0
+		 * until we complete the update.
+		 */
+		raw_spin_lock(&src_dl_b->lock);
+		__dl_sub(src_dl_b, p->dl.dl_bw, dl_bw_cpus(task_cpu(p)));
+		raw_spin_unlock(&src_dl_b->lock);
+	}
+}
+
 static void migrate_task_rq_dl(struct task_struct *p, int new_cpu

__maybe_unused)
{
struct rq *rq;

Quoted 9 lines

kernel/sched/deadline.c static void yield_task_dl(struct rq *rq) · line 1641

Quoted 5 lines
kernel/sched/deadline.c +4 -1
-	if (p->state != TASK_WAKING)
+	rq = task_rq(p);
+
+	if (p->state != TASK_WAKING) {
+		free_old_cpuset_bw_dl(rq, p);

What happens if a DEADLINE task is moved between cpusets while it was sleeping? Don't we miss removing from the old cpuset if the task gets migrated on wakeup?

In that case set_task_cpu() is called by ttwu after setting state to TASK_WAKING. I guess it could be annoying if the task doesn't wake up for a long time and therefore doesn't release the bandwidth until then.

-Scott

maintainer
#16 lore raw

Re: [PATCH RT 5/8] sched/deadline: Reclaim cpuset bandwidth in .migrate_task_rq()

Quoted 65 lines · Scott Wood
Quoted 54 lines · Juri Lelli

Hi Scott,

Quoted 51 lines · Scott Wood

With the changes to migrate disabling, ->set_cpus_allowed() no longer gets deferred until migrate_enable(). To avoid releasing the bandwidth while the task may still be executing on the old CPU, move the subtraction
to ->migrate_task_rq().

---
 kernel/sched/deadline.c | 67 +++++++++++++++++++++++-------------------
-------
 1 file changed, 31 insertions(+), 36 deletions(-)
kernel/sched/deadline.c +17 -0
index c18be51f7608..2f18d0cf1b56 100644
@@ -1606,14 +1606,42 @@ static void yield_task_dl(struct rq *rq)
 	return cpu;
 }
 
+static void free_old_cpuset_bw_dl(struct rq *rq, struct task_struct *p)
+{
+	struct root_domain *src_rd = rq->rd;
+
+	/*
+	 * Migrating a SCHED_DEADLINE task between exclusive
+	 * cpusets (different root_domains) entails a bandwidth
+	 * update. We already made space for us in the destination
+	 * domain (see cpuset_can_attach()).
+	 */
+	if (!cpumask_intersects(src_rd->span, p->cpus_ptr)) {
+		struct dl_bw *src_dl_b;
+
+		src_dl_b = dl_bw_of(cpu_of(rq));
+		/*
+		 * We now free resources of the root_domain we are migrating
+		 * off. In the worst case, sched_setattr() may temporary

fail

kernel/sched/deadline.c +8 -0
+		 * until we complete the update.
+		 */
+		raw_spin_lock(&src_dl_b->lock);
+		__dl_sub(src_dl_b, p->dl.dl_bw, dl_bw_cpus(task_cpu(p)));
+		raw_spin_unlock(&src_dl_b->lock);
+	}
+}
+
 static void migrate_task_rq_dl(struct task_struct *p, int new_cpu

__maybe_unused)
{
struct rq *rq;

Quoted 9 lines

kernel/sched/deadline.c static void yield_task_dl(struct rq *rq) · line 1641

Quoted 5 lines
kernel/sched/deadline.c +4 -1
-	if (p->state != TASK_WAKING)
+	rq = task_rq(p);
+
+	if (p->state != TASK_WAKING) {
+		free_old_cpuset_bw_dl(rq, p);

What happens if a DEADLINE task is moved between cpusets while it was sleeping? Don't we miss removing from the old cpuset if the task gets migrated on wakeup?

Quoted 2 lines

In that case set_task_cpu() is called by ttwu after setting state to TASK_WAKING.

Right.

Quoted 2 lines

I guess it could be annoying if the task doesn't wake up for a long time and therefore doesn't release the bandwidth until then.

Hummm, I was actually more worried about the fact that we call free_old_ cpuset_bw_dl() only if p->state != TASK_WAKING.

author
#17 lore raw

Re: [PATCH RT 5/8] sched/deadline: Reclaim cpuset bandwidth in .migrate_task_rq()

Quoted 76 lines · Juri Lelli
Quoted 72 lines · Scott Wood
Quoted 61 lines · Juri Lelli

Hi Scott,

Quoted 58 lines · Scott Wood

With the changes to migrate disabling, ->set_cpus_allowed() no longer
gets deferred until migrate_enable(). To avoid releasing the bandwidth
while the task may still be executing on the old CPU, move the subtraction
to ->migrate_task_rq().

---
 kernel/sched/deadline.c | 67 +++++++++++++++++++++++---------------
----
-------
 1 file changed, 31 insertions(+), 36 deletions(-)
kernel/sched/deadline.c +16 -0
index c18be51f7608..2f18d0cf1b56 100644
@@ -1606,14 +1606,42 @@ static void yield_task_dl(struct rq *rq)
 	return cpu;
 }
 
+static void free_old_cpuset_bw_dl(struct rq *rq, struct task_struct
 *p)
+{
+	struct root_domain *src_rd = rq->rd;
+
+	/*
+	 * Migrating a SCHED_DEADLINE task between exclusive
+	 * cpusets (different root_domains) entails a bandwidth
+	 * update. We already made space for us in the destination
+	 * domain (see cpuset_can_attach()).
+	 */
+	if (!cpumask_intersects(src_rd->span, p->cpus_ptr)) {
+		struct dl_bw *src_dl_b;
+
+		src_dl_b = dl_bw_of(cpu_of(rq));
+		/*
+		 * We now free resources of the root_domain we are

migrating

kernel/sched/deadline.c +1 -0
+		 * off. In the worst case, sched_setattr() may

temporary
fail

kernel/sched/deadline.c +4 -0
+		 * until we complete the update.
+		 */
+		raw_spin_lock(&src_dl_b->lock);
+		__dl_sub(src_dl_b, p->dl.dl_bw,

dl_bw_cpus(task_cpu(p)));

kernel/sched/deadline.c +4 -0
+		raw_spin_unlock(&src_dl_b->lock);
+	}
+}
+
 static void migrate_task_rq_dl(struct task_struct *p, int new_cpu

__maybe_unused)
{
struct rq *rq;

Quoted 9 lines

kernel/sched/deadline.c static void yield_task_dl(struct rq *rq) · line 1641

Quoted 5 lines
kernel/sched/deadline.c +4 -1
-	if (p->state != TASK_WAKING)
+	rq = task_rq(p);
+
+	if (p->state != TASK_WAKING) {
+		free_old_cpuset_bw_dl(rq, p);

What happens if a DEADLINE task is moved between cpusets while it was sleeping? Don't we miss removing from the old cpuset if the task gets migrated on wakeup?

Quoted 2 lines

In that case set_task_cpu() is called by ttwu after setting state to TASK_WAKING.

Quoted 7 lines

Right.

Quoted 2 lines

I guess it could be annoying if the task doesn't wake up for a long time and therefore doesn't release the bandwidth until then.

Hummm, I was actually more worried about the fact that we call free_old_ cpuset_bw_dl() only if p->state != TASK_WAKING.

Oh, right. :-P Not sure what I had in mind there; we want to call it regardless.

I assume we need rq->lock in free_old_cpuset_bw_dl()? So something like this:

	if (p->state == TASK_WAITING)
		raw_spin_lock(&rq->lock);
	free_old_cpuset_bw_dl(rq, p);
	if (p->state != TASK_WAITING)
		return;

	if (p->dl.dl_non_contending) {
	....	

BTW, is the full cpumask_intersects() necessary or would it suffice to see that the new cpu is not in the old span?

-Scott

maintainer
#18 lore raw

Re: [PATCH RT 5/8] sched/deadline: Reclaim cpuset bandwidth in .migrate_task_rq()

Quoted 1 line · Scott Wood

On Mon, 2019-09-30 at 09:12 +0200, Juri Lelli wrote:

[...]

Quoted 7 lines
Quoted 2 lines

Hummm, I was actually more worried about the fact that we call free_old_ cpuset_bw_dl() only if p->state != TASK_WAKING.

Oh, right. :-P Not sure what I had in mind there; we want to call it regardless.

I assume we need rq->lock in free_old_cpuset_bw_dl()? So something like

I think we can do with rcu_read_lock_sched() (see dl_task_can_attach()).

Quoted 11 lines

this:

	if (p->state == TASK_WAITING)
		raw_spin_lock(&rq->lock);
	free_old_cpuset_bw_dl(rq, p);
	if (p->state != TASK_WAITING)
		return;

	if (p->dl.dl_non_contending) {
	....	
Quoted 2 lines

BTW, is the full cpumask_intersects() necessary or would it suffice to see that the new cpu is not in the old span?

Checking new cpu only should be OK.

Thanks,

Juri

author
#19 lore raw

Re: [PATCH RT 5/8] sched/deadline: Reclaim cpuset bandwidth in .migrate_task_rq()

Quoted 9 lines · Juri Lelli
Quoted 1 line · Scott Wood

On Mon, 2019-09-30 at 09:12 +0200, Juri Lelli wrote:

[...]

Quoted 4 lines
Quoted 3 lines

Hummm, I was actually more worried about the fact that we call free_old_
cpuset_bw_dl() only if p->state != TASK_WAKING.

Quoted 6 lines
Quoted 4 lines

Oh, right. :-P Not sure what I had in mind there; we want to call it regardless.

I assume we need rq->lock in free_old_cpuset_bw_dl()? So something like

I think we can do with rcu_read_lock_sched() (see dl_task_can_attach()).

RCU will keep dl_bw from being freed under us (we're implicitly in an RCU sched read section due to atomic context). It won't stop rq->rd from changing, but that could have happened before we took rq->lock. If the cpu the task was running on was removed from the cpuset, and that raced with the task being moved to a different cpuset, couldn't we end up erroneously subtracting from the cpu's new root domain (or failing to subtract at all if the old cpu's new cpuset happens to be the task's new cpuset)? I don't see anything that forces tasks off of the cpu when a cpu is removed from a cpuset (though maybe I'm not looking in the right place), so the race window could be quite large. In any case, that's an existing problem that's not going to get solved in this patchset.

-Scott

maintainer
#20 lore raw

Re: [PATCH RT 5/8] sched/deadline: Reclaim cpuset bandwidth in .migrate_task_rq()

Quoted 17 lines · Scott Wood
Quoted 9 lines · Juri Lelli
Quoted 1 line · Scott Wood

On Mon, 2019-09-30 at 09:12 +0200, Juri Lelli wrote:

[...]

Quoted 4 lines
Quoted 3 lines

Hummm, I was actually more worried about the fact that we call free_old_
cpuset_bw_dl() only if p->state != TASK_WAKING.

Quoted 6 lines
Quoted 4 lines

Oh, right. :-P Not sure what I had in mind there; we want to call it regardless.

I assume we need rq->lock in free_old_cpuset_bw_dl()? So something like

I think we can do with rcu_read_lock_sched() (see dl_task_can_attach()).

Quoted 11 lines

RCU will keep dl_bw from being freed under us (we're implicitly in an RCU sched read section due to atomic context). It won't stop rq->rd from changing, but that could have happened before we took rq->lock. If the cpu the task was running on was removed from the cpuset, and that raced with the task being moved to a different cpuset, couldn't we end up erroneously subtracting from the cpu's new root domain (or failing to subtract at all if the old cpu's new cpuset happens to be the task's new cpuset)? I don't see anything that forces tasks off of the cpu when a cpu is removed from a cpuset (though maybe I'm not looking in the right place), so the race window could be quite large. In any case, that's an existing problem that's not going to get solved in this patchset.

OK. So, mainline has got cpuset_read_lock() which should be enough to guard against changes to rd(s).

I agree that rq->lock is needed here.

Thanks,

Juri

author
#21 lore raw

Re: [PATCH RT 5/8] sched/deadline: Reclaim cpuset bandwidth in .migrate_task_rq()

Quoted 40 lines · Juri Lelli
Quoted 30 lines · Scott Wood
Quoted 13 lines · Juri Lelli
Quoted 1 line · Scott Wood

On Mon, 2019-09-30 at 09:12 +0200, Juri Lelli wrote:

[...]

Quoted 8 lines
Quoted 3 lines

Hummm, I was actually more worried about the fact that we call free_old_
cpuset_bw_dl() only if p->state != TASK_WAKING.

Oh, right. :-P Not sure what I had in mind there; we want to call it
regardless.

Quoted 5 lines
Quoted 2 lines

I assume we need rq->lock in free_old_cpuset_bw_dl()? So something like

I think we can do with rcu_read_lock_sched() (see
dl_task_can_attach()).

RCU will keep dl_bw from being freed under us (we're implicitly in an RCU
sched read section due to atomic context). It won't stop rq->rd from changing, but that could have happened before we took rq->lock. If the cpu
the task was running on was removed from the cpuset, and that raced with the
task being moved to a different cpuset, couldn't we end up erroneously subtracting from the cpu's new root domain (or failing to subtract at all if

Quoted 8 lines

the old cpu's new cpuset happens to be the task's new cpuset)? I don't see
anything that forces tasks off of the cpu when a cpu is removed from a cpuset (though maybe I'm not looking in the right place), so the race window
could be quite large. In any case, that's an existing problem that's not
going to get solved in this patchset.

Quoted 4 lines

OK. So, mainline has got cpuset_read_lock() which should be enough to guard against changes to rd(s).

I agree that rq->lock is needed here.

My point was that rq->lock isn't actually helping, because rq->rd could have changed before rq->lock is acquired (and it's still the old rd that needs the bandwidth subtraction). cpuset_mutex/cpuset_rwsem helps somewhat, though there's still a problem due to the subtraction not happening until the task is woken up (by which time cpuset_mutex may have been released and further reconfiguration could have happened). This would be an issue even without lazy migrate, since in that case ->set_cpus_allowed() can get deferred, but this patch makes the window much bigger.

The right solution is probably to explicitly track the rd for which a task has a pending bandwidth subtraction (if any), and to continue doing it from set_cpus_allowed() if the task is not migrate-disabled. In the meantime, I think we should drop this patch from the patchset -- without it, lazy migrate disable doesn't really make the race situation any worse than it already was.

BTW, what happens to the bw addition in dl_task_can_attach() if a subsequent can_attach fails and the whole operation is cancelled?

-Scott

maintainer
#22 lore raw

Re: [PATCH RT 5/8] sched/deadline: Reclaim cpuset bandwidth in .migrate_task_rq()

Quoted 55 lines · Scott Wood
Quoted 40 lines · Juri Lelli
Quoted 30 lines · Scott Wood
Quoted 13 lines · Juri Lelli
Quoted 1 line · Scott Wood

On Mon, 2019-09-30 at 09:12 +0200, Juri Lelli wrote:

[...]

Quoted 8 lines
Quoted 3 lines

Hummm, I was actually more worried about the fact that we call free_old_
cpuset_bw_dl() only if p->state != TASK_WAKING.

Oh, right. :-P Not sure what I had in mind there; we want to call it
regardless.

Quoted 5 lines
Quoted 2 lines

I assume we need rq->lock in free_old_cpuset_bw_dl()? So something like

I think we can do with rcu_read_lock_sched() (see
dl_task_can_attach()).

RCU will keep dl_bw from being freed under us (we're implicitly in an RCU
sched read section due to atomic context). It won't stop rq->rd from changing, but that could have happened before we took rq->lock. If the cpu
the task was running on was removed from the cpuset, and that raced with the
task being moved to a different cpuset, couldn't we end up erroneously subtracting from the cpu's new root domain (or failing to subtract at all if

Quoted 8 lines

the old cpu's new cpuset happens to be the task's new cpuset)? I don't see
anything that forces tasks off of the cpu when a cpu is removed from a cpuset (though maybe I'm not looking in the right place), so the race window
could be quite large. In any case, that's an existing problem that's not
going to get solved in this patchset.

Quoted 4 lines

OK. So, mainline has got cpuset_read_lock() which should be enough to guard against changes to rd(s).

I agree that rq->lock is needed here.

My point was that rq->lock isn't actually helping, because rq->rd could have changed before rq->lock is acquired (and it's still the old rd that needs the bandwidth subtraction). cpuset_mutex/cpuset_rwsem helps somewhat, though there's still a problem due to the subtraction not happening until the task is woken up (by which time cpuset_mutex may have been released and further reconfiguration could have happened). This would be an issue even without lazy migrate, since in that case ->set_cpus_allowed() can get deferred, but this patch makes the window much bigger.

Quoted 6 lines

The right solution is probably to explicitly track the rd for which a task has a pending bandwidth subtraction (if any), and to continue doing it from set_cpus_allowed() if the task is not migrate-disabled. In the meantime, I think we should drop this patch from the patchset -- without it, lazy migrate disable doesn't really make the race situation any worse than it already was.

I'm OK with dropping it for now (as we also have other possible issues as you point out below), but I really wonder what would be a solution here. Problem is that if a domain(s) reconfiguration happened while the task was migrate disabled, and we let the reconf destroy/rebuild domains, the old rd could be gone by the time the task gets migrate enabled again and the task could continue running, w/o its bandwidth been accounted for, in a new rd since the migrate enable instant, no?

:-/

Quoted 2 lines

BTW, what happens to the bw addition in dl_task_can_attach() if a subsequent can_attach fails and the whole operation is cancelled?

Oh, yeah, that doesn't look good. :-(

Maybe we can use cancel_attach() to fix things up?

Thanks,

Juri

author
#23 lore raw

PATCH 6/8 sched: migrate_enable: Set state to TASK_RUNNING

In reply to Scott Wood

If migrate_enable() is called while a task is preparing to sleep (state != TASK_RUNNING), that triggers a debug check in stop_one_cpu(). Explicitly reset state to acknowledge that we're accepting the spurious wakeup.

---
 kernel/sched/core.c | 8 ++++++++
 1 file changed, 8 insertions(+)
kernel/sched/core.c +8 -0
index 38a9a9df5638..eb27a9bf70d7 100644
@@ -7396,6 +7396,14 @@ void migrate_enable(void)
 			unpin_current_cpu();
 			preempt_lazy_enable();
 			preempt_enable();
+
+			/*
+			 * Avoid sleeping with an existing non-running
+			 * state.  This will result in a spurious wakeup
+			 * for the calling context.
+			 */
+			__set_current_state(TASK_RUNNING);
+
 			sleeping_lock_inc();
 			stop_one_cpu(task_cpu(p), migration_cpu_stop, &arg);
 			sleeping_lock_dec();
Signature · 1 line
1.8.3.1
#24 lore raw

Re: [PATCH RT 6/8] sched: migrate_enable: Set state to TASK_RUNNING

Quoted 15 lines · Scott Wood

If migrate_enable() is called while a task is preparing to sleep (state != TASK_RUNNING), that triggers a debug check in stop_one_cpu(). Explicitly reset state to acknowledge that we're accepting the spurious wakeup.

---
 kernel/sched/core.c | 8 ++++++++
 1 file changed, 8 insertions(+)
kernel/sched/core.c +0 -0
index 38a9a9df5638..eb27a9bf70d7 100644

kernel/sched/core.c void migrate_enable(void) · line 7405

Quoted 10 lines
kernel/sched/core.c +7 -0
@@ -7396,6 +7396,14 @@ void migrate_enable(void)
 			unpin_current_cpu();
 			preempt_lazy_enable();
 			preempt_enable();
+
+			/*
+			 * Avoid sleeping with an existing non-running
+			 * state.  This will result in a spurious wakeup
+			 * for the calling context.
+			 */
+			__set_current_state(TASK_RUNNING);

Do you have an example for this? I'm not too sure if we are not using a state by doing this. Actually we were losing it and get yelled at. We do:

|rt_spin_unlock()
|{
|         rt_spin_lock_fastunlock();
|         migrate_enable();
|}

So save the state as part of the locking process and lose it as part of migrate_enable() if the CPU mask was changed. I *think* we need to preserve that state until after the migration to the other CPU.

kernel/sched/core.c void migrate_enable(void) · line 7409

Quoted 3 lines
kernel/sched/core.c +0 -0
 			sleeping_lock_inc();
 			stop_one_cpu(task_cpu(p), migration_cpu_stop, &arg);
 			sleeping_lock_dec();

Sebastian

author
#25 lore raw

Re: [PATCH RT 6/8] sched: migrate_enable: Set state to TASK_RUNNING

Quoted 20 lines · Sebastian Andrzej Siewior
Quoted 19 lines · Scott Wood

If migrate_enable() is called while a task is preparing to sleep (state != TASK_RUNNING), that triggers a debug check in stop_one_cpu(). Explicitly reset state to acknowledge that we're accepting the spurious wakeup.

---
 kernel/sched/core.c | 8 ++++++++
 1 file changed, 8 insertions(+)
kernel/sched/core.c +1 -0
index 38a9a9df5638..eb27a9bf70d7 100644
@@ -7396,6 +7396,14 @@ void migrate_enable(void)
 			unpin_current_cpu();
 			preempt_lazy_enable();
 			preempt_enable();
+
Quoted 8 lines

kernel/sched/core.c void migrate_enable(void) · line 7405

Quoted 6 lines
kernel/sched/core.c +6 -0
+			/*
+			 * Avoid sleeping with an existing non-running
+			 * state.  This will result in a spurious wakeup
+			 * for the calling context.
+			 */
+			__set_current_state(TASK_RUNNING);

Do you have an example for this?

Unfortunately I didn't save the traceback of where I originally saw it, but I ran it again and hit the warning in prepare_to_wait_event().

Quoted 11 lines

I'm not too sure if we are not using a
state by doing this. Actually we were losing it and get yelled at. We do:

Quoted 5 lines
kernel/sched/core.c +0 -0
 rt_spin_unlock()
 {
        rt_spin_lock_fastunlock();
        migrate_enable();

}

So save the state as part of the locking process and lose it as part of migrate_enable() if the CPU mask was changed. I *think* we need to preserve that state until after the migration to the other CPU.

Preserving the state would be ideal, though in most cases occasionally losing the state should be tolerable as long as it doesn't happen persistently. TASK_DEAD is an exception but that doesn't do anything before schedule() that could end up here. I suppose there could be some places that use TASK_UNINTERRUPTIBLE without checking the actual condition after schedule(), and which do something that can end up here before schedule()...

We can't use saved_state here though, because we can get here inside the rtmutex code (e.g. from debug_rt_mutex_print_deadlock)... and besides, the waker won't have WF_LOCK_SLEEPER and so saved_state will get cleared.

-Scott

author
#26 lore raw

PATCH 7/8 sched: migrate_enable: Use select_fallback_rq()

In reply to Scott Wood

migrate_enable() currently open-codes a variant of select_fallback_rq(). However, it does not have the "No more Mr. Nice Guy" fallback and thus it will pass an invalid CPU to the migration thread if cpus_mask only contains a CPU that is !active.

---
This scenario will be more likely after the next patch, since the migrate_disable_update check goes away. However, it could happen anyway if cpus_mask was updated to a CPU other than the one we were pinned to, and that CPU subsequently became inactive.

---
 kernel/sched/core.c | 25 ++++++++++---------------
 1 file changed, 10 insertions(+), 15 deletions(-)
kernel/sched/core.c +10 -15
index eb27a9bf70d7..3a2d8251a30c 100644
@@ -7368,6 +7368,7 @@ void migrate_enable(void)
 	if (p->migrate_disable_update) {
 		struct rq *rq;
 		struct rq_flags rf;
+		int cpu = task_cpu(p);
 
 		rq = task_rq_lock(p, &rf);
 		update_rq_clock(rq);
@@ -7377,21 +7378,15 @@ void migrate_enable(void)
 
 		p->migrate_disable_update = 0;
 
-		WARN_ON(smp_processor_id() != task_cpu(p));
-		if (!cpumask_test_cpu(task_cpu(p), &p->cpus_mask)) {
-			const struct cpumask *cpu_valid_mask = cpu_active_mask;
-			struct migration_arg arg;
-			unsigned int dest_cpu;
-
-			if (p->flags & PF_KTHREAD) {
-				/*
-				 * Kernel threads are allowed on online && !active CPUs
-				 */
-				cpu_valid_mask = cpu_online_mask;
-			}
-			dest_cpu = cpumask_any_and(cpu_valid_mask, &p->cpus_mask);
-			arg.task = p;
-			arg.dest_cpu = dest_cpu;
+		WARN_ON(smp_processor_id() != cpu);
+		if (!cpumask_test_cpu(cpu, &p->cpus_mask)) {
+			struct migration_arg arg = { p };
+			struct rq_flags rf;
+
+			rq = task_rq_lock(p, &rf);
+			update_rq_clock(rq);
+			arg.dest_cpu = select_fallback_rq(cpu, p);
+			task_rq_unlock(rq, p, &rf);
 
 			unpin_current_cpu();
 			preempt_lazy_enable();
Signature · 1 line
1.8.3.1
#27 lore raw

Re: [PATCH RT 7/8] sched: migrate_enable: Use select_fallback_rq()

Quoted 11 lines · Scott Wood

migrate_enable() currently open-codes a variant of select_fallback_rq(). However, it does not have the "No more Mr. Nice Guy" fallback and thus it will pass an invalid CPU to the migration thread if cpus_mask only contains a CPU that is !active.

---
This scenario will be more likely after the next patch, since the migrate_disable_update check goes away. However, it could happen anyway if cpus_mask was updated to a CPU other than the one we were pinned to, and that CPU subsequently became inactive.

I'm unclear about the problem / side effect this has (before and after the change). It is possible (before and after that change) that a CPU is selected which is invalid / goes offline after the "preempt_enable()" statement and before stop_one_cpu() does its job, correct?

Quoted 37 lines
---
 kernel/sched/core.c | 25 ++++++++++---------------
 1 file changed, 10 insertions(+), 15 deletions(-)
kernel/sched/core.c +3 -15
index eb27a9bf70d7..3a2d8251a30c 100644
@@ -7368,6 +7368,7 @@ void migrate_enable(void)
 	if (p->migrate_disable_update) {
 		struct rq *rq;
 		struct rq_flags rf;
+		int cpu = task_cpu(p);
 
 		rq = task_rq_lock(p, &rf);
 		update_rq_clock(rq);
@@ -7377,21 +7378,15 @@ void migrate_enable(void)
 
 		p->migrate_disable_update = 0;
 
-		WARN_ON(smp_processor_id() != task_cpu(p));
-		if (!cpumask_test_cpu(task_cpu(p), &p->cpus_mask)) {
-			const struct cpumask *cpu_valid_mask = cpu_active_mask;
-			struct migration_arg arg;
-			unsigned int dest_cpu;
-
-			if (p->flags & PF_KTHREAD) {
-				/*
-				 * Kernel threads are allowed on online && !active CPUs
-				 */
-				cpu_valid_mask = cpu_online_mask;
-			}
-			dest_cpu = cpumask_any_and(cpu_valid_mask, &p->cpus_mask);
-			arg.task = p;
-			arg.dest_cpu = dest_cpu;
+		WARN_ON(smp_processor_id() != cpu);
+		if (!cpumask_test_cpu(cpu, &p->cpus_mask)) {

kernel/sched/core.c void migrate_enable(void) · line 7392

Quoted 10 lines
kernel/sched/core.c +7 -0
+			struct migration_arg arg = { p };
+			struct rq_flags rf;
+
+			rq = task_rq_lock(p, &rf);
+			update_rq_clock(rq);
+			arg.dest_cpu = select_fallback_rq(cpu, p);
+			task_rq_unlock(rq, p, &rf);
 
 			unpin_current_cpu();
 			preempt_lazy_enable();

Sebastian

author
#28 lore raw

Re: [PATCH RT 7/8] sched: migrate_enable: Use select_fallback_rq()

Quoted 13 lines · Sebastian Andrzej Siewior
Quoted 11 lines · Scott Wood

migrate_enable() currently open-codes a variant of select_fallback_rq(). However, it does not have the "No more Mr. Nice Guy" fallback and thus it will pass an invalid CPU to the migration thread if cpus_mask only contains a CPU that is !active.

---
This scenario will be more likely after the next patch, since the migrate_disable_update check goes away. However, it could happen anyway if cpus_mask was updated to a CPU other than the one we were pinned to, and that CPU subsequently became inactive.

Quoted 4 lines

I'm unclear about the problem / side effect this has (before and after the change). It is possible (before and after that change) that a CPU is selected which is invalid / goes offline after the "preempt_enable()" statement and before stop_one_cpu() does its job, correct?

By "pass an invalid CPU" I don't mean offline; I mean >= nr_cpu_ids which is what cpumask_any_and() returns if the sets don't intersect (a CPU going offline is merely a way to end up in that situation). At one point I observed that causing a crash. I guess is_cpu_allowed() returned true by chance based on the contents of data beyond the end of the cpumask? That doesn't seem likely based on what comes after p->cpus_mask (at that point migrate_disable should be zero), but maybe I had something else at that point in the struct while developing. In any case, not something to be relied on. :-)

Going offline after selection shouldn't be a problem. migration_cpu_stop() won't do anything if is_cpu_allowed() returns false, and we'll get migrated off the CPU by migrate_tasks(). Even if we get migrated after reading task_cpu(p) but before queuing the stop machine work, it'll either get flushed when the stopper thread parks, or rejected due to stopper->enabled being false.

-Scott

author
#29 lore raw

PATCH 8/8 sched: Lazy migrate_disable processing

In reply to Scott Wood

Avoid overhead on the majority of migrate disable/enable sequences by only manipulating scheduler data (and grabbing the relevant locks) when the task actually schedules while migrate-disabled. Very large speedups were seen during a kernel build.

Instead of cpuhp_pin_lock, CPU hotplug is handled by keeping a per-CPU count of the number of pinned tasks (including tasks which have not scheduled in the migrate-disabled section); takedown_cpu() will wait until that reaches zero (confirmed by take_cpu_down() in stop machine context to deal with races) before migrating tasks off of the cpu.

To simplify synchronization, updating cpus_mask is no longer deferred until migrate_enable(). This lets us not have to worry about migrate_enable() missing the update if it's on the fast path (didn't schedule during the migrate disabled section). It also makes the code a bit simpler and reduces deviation from mainline.

While the main motivation for this is the performance benefit, lazy migrate disable also eliminates the restriction on calling migrate_disable() while atomic but leaving the atomic region prior to calling migrate_enable() -- though this won't help with local_bh_disable() (and thus rcutorture) unless something similar is done with the recently added local_lock.

---
 include/linux/cpu.h    |   4 --
 include/linux/sched.h  |  11 +--
 init/init_task.c       |   4 ++
 kernel/cpu.c           |  97 +++++++++----------------
 kernel/sched/core.c    | 192 ++++++++++++++++++++-----------------------------
 kernel/sched/sched.h   |   4 ++
 lib/smp_processor_id.c |   3 +
 7 files changed, 130 insertions(+), 185 deletions(-)
include/linux/cpu.h +0 -4
index f4a772c12d14..2df500fdcbc4 100644
@@ -113,8 +113,6 @@ static inline void cpu_maps_update_done(void)
 extern void cpu_hotplug_enable(void);
 void clear_tasks_mm_cpumask(int cpu);
 int cpu_down(unsigned int cpu);
-extern void pin_current_cpu(void);
-extern void unpin_current_cpu(void);
 
 #else /* CONFIG_HOTPLUG_CPU */
 
@@ -126,8 +124,6 @@ static inline void cpus_read_unlock(void) { }
 static inline void lockdep_assert_cpus_held(void) { }
 static inline void cpu_hotplug_disable(void) { }
 static inline void cpu_hotplug_enable(void) { }
-static inline void pin_current_cpu(void) { }
-static inline void unpin_current_cpu(void) { }
 
 #endif	/* !CONFIG_HOTPLUG_CPU */
 
include/linux/sched.h +6 -5
index ad23ab939b35..069c46dde15b 100644
@@ -229,6 +229,8 @@
 extern long io_schedule_timeout(long timeout);
 extern void io_schedule(void);
 
+int cpu_nr_pinned(int cpu);
+
 /**
  * struct prev_cputime - snapshot of system and user cputime
  * @utime: time spent in user mode
@@ -661,16 +663,13 @@ struct task_struct {
 	cpumask_t			cpus_mask;
 #if defined(CONFIG_SMP) && defined(CONFIG_PREEMPT_RT_BASE)
 	int				migrate_disable;
-	int				migrate_disable_update;
-	int				pinned_on_cpu;
+	bool				migrate_disable_scheduled;
 # ifdef CONFIG_SCHED_DEBUG
-	int				migrate_disable_atomic;
+	int				pinned_on_cpu;
 # endif
-
 #elif !defined(CONFIG_SMP) && defined(CONFIG_PREEMPT_RT_BASE)
 # ifdef CONFIG_SCHED_DEBUG
 	int				migrate_disable;
-	int				migrate_disable_atomic;
 # endif
 #endif
 #ifdef CONFIG_PREEMPT_RT_BASE
@@ -2066,4 +2065,6 @@ static inline void rseq_syscall(struct pt_regs *regs)
 
 #endif
 
+extern struct task_struct *takedown_cpu_task;
+
 #endif
init/init_task.c +4 -0
index e402413dc47d..c0c7618fd2fb 100644
@@ -81,6 +81,10 @@ struct task_struct init_task
 	.cpus_ptr	= &init_task.cpus_mask,
 	.cpus_mask	= CPU_MASK_ALL,
 	.nr_cpus_allowed= NR_CPUS,
+#if defined(CONFIG_SMP) && defined(CONFIG_PREEMPT_RT_BASE) && \
+    defined(CONFIG_SCHED_DEBUG)
+	.pinned_on_cpu	= -1,
+#endif
 	.mm		= NULL,
 	.active_mm	= &init_mm,
 	.restart_block	= {
kernel/cpu.c +34 -63

142 lines not on this page · raw message

kernel/sched/core.c +79 -113

299 lines not on this page · raw message

kernel/sched/sched.h +4 -0
index 9c1bbd67e971..2e4db417049d 100644
@@ -952,6 +952,10 @@ struct rq {
 	/* Must be inspected within a rcu lock section */
 	struct cpuidle_state	*idle_state;
 #endif
+
+#if defined(CONFIG_PREEMPT_RT_BASE) && defined(CONFIG_SMP)
+	int			nr_pinned;
+#endif
 };
 
 #ifdef CONFIG_FAIR_GROUP_SCHED
lib/smp_processor_id.c +3 -0
index 60ba93fc42ce..ded40a11c867 100644
@@ -23,6 +23,9 @@ unsigned int check_preemption_disabled(const char *what1, const char *what2)
 	 * Kernel threads bound to a single CPU can safely use
 	 * smp_processor_id():
 	 */
+	if (current->migrate_disable)
+		goto out;
+
 	if (cpumask_equal(current->cpus_ptr, cpumask_of(this_cpu)))
 		goto out;
 
Signature · 1 line
1.8.3.1
#30 lore raw

Re: [PATCH RT 8/8] sched: Lazy migrate_disable processing

Quoted 14 lines · Scott Wood
kernel/cpu.c +4 -2
index 885a195dfbe0..0096acf1a692 100644
@@ -939,17 +893,34 @@ static int takedown_cpu(unsigned int cpu)
 	 */
 	irq_lock_sparse();
 
-#ifdef CONFIG_PREEMPT_RT_FULL
-	__write_rt_lock(cpuhp_pin);
+#ifdef CONFIG_PREEMPT_RT_BASE
+	WARN_ON_ONCE(takedown_cpu_task);
+	takedown_cpu_task = current;
+

kernel/cpu.c static int takedown_cpu(unsigned int cpu) · line 909

Quoted 10 lines
kernel/cpu.c +10 -0
+again:
+	for (;;) {
+		int nr_pinned;
+
+		set_current_state(TASK_UNINTERRUPTIBLE);
+		nr_pinned = cpu_nr_pinned(cpu);
+		if (nr_pinned == 0)
+			break;
+		schedule();
+	}

we used to have cpuhp_pin which ensured that once we own the write lock there will be no more tasks that can enter a migrate_disable() section on this CPU. It has been placed fairly late to ensure that nothing new comes in as part of the shutdown process and that it flushes everything out that is still in a migrate_disable() section.
Now you claim that once the counter reached zero it never increments again. I would be happier if there was an explicit check for that :) There is no back off and flush mechanism which means on a busy CPU (as in heavily lock contended by multiple tasks) this will wait until the CPU gets idle again.

kernel/cpu.c static int takedown_cpu(unsigned int cpu) · line 911

Quoted 2 lines
kernel/cpu.c +1 -0
+	set_current_state(TASK_RUNNING);
 #endif

Sebastian

author
#31 lore raw

Re: [PATCH RT 8/8] sched: Lazy migrate_disable processing

Quoted 26 lines · Sebastian Andrzej Siewior
Quoted 14 lines · Scott Wood
kernel/cpu.c +4 -2
index 885a195dfbe0..0096acf1a692 100644
@@ -939,17 +893,34 @@ static int takedown_cpu(unsigned int cpu)
 	 */
 	irq_lock_sparse();
 
-#ifdef CONFIG_PREEMPT_RT_FULL
-	__write_rt_lock(cpuhp_pin);
+#ifdef CONFIG_PREEMPT_RT_BASE
+	WARN_ON_ONCE(takedown_cpu_task);
+	takedown_cpu_task = current;
+

kernel/cpu.c static int takedown_cpu(unsigned int cpu) · line 909

Quoted 10 lines
kernel/cpu.c +10 -0
+again:
+	for (;;) {
+		int nr_pinned;
+
+		set_current_state(TASK_UNINTERRUPTIBLE);
+		nr_pinned = cpu_nr_pinned(cpu);
+		if (nr_pinned == 0)
+			break;
+		schedule();
+	}
Quoted 7 lines

we used to have cpuhp_pin which ensured that once we own the write lock there will be no more tasks that can enter a migrate_disable() section on this CPU. It has been placed fairly late to ensure that nothing new comes in as part of the shutdown process and that it flushes everything out that is still in a migrate_disable() section.
Now you claim that once the counter reached zero it never increments again. I would be happier if there was an explicit check for that :)

I don't claim that. A check is added in take_cpu_down() to see whether it went back up, and if so, exit with EAGAIN. If *that* check succeeds, it can't go back up because it's in stop machine, and any tasks will get migrated to another CPU before they can run again. There's also a WARN in migrate_tasks() if somehow a migrate-disabled task does get encountered.

Quoted 3 lines

There is no back off and flush mechanism which means on a busy CPU (as in heavily lock contended by multiple tasks) this will wait until the CPU gets idle again.

Not really any different from the reader-biased rwlock that this replaces...

-Scott