2023-02-06 14:12:20

by Valentin Schneider

[permalink] [raw]
Subject: [RFC PATCH v2] sched/deadline: Add more reschedule cases to prio_changed_dl()

I've been tracking down an issue on a ~5.17ish kernel where:

CPUx CPUy

<DL task p0 owns an rtmutex M>
<p0 depletes its runtime, gets throttled>
<rq switches to the idle task>
<DL task p1 blocks on M, boost/replenish p0>
<No call to resched_curr() happens here>

[idle task keeps running here until *something*
accidentally sets TIF_NEED_RESCHED]

On that kernel, it is quite easy to trigger using rt-tests's deadline_test
[1] with the test running on isolated CPUs (this reduces the chance of
something unrelated setting TIF_NEED_RESCHED on the idle tasks, making the
issue even more obvious as the hung task detector chimes in).

I haven't been able to reproduce this using a mainline kernel, even if I
revert

2972e3050e35 ("tracing: Make trace_marker{,_raw} stream-like")

which gets rid of the lock involved in the above test, *but* I cannot
convince myself the issue isn't there from looking at the code.

Make prio_changed_dl() issue a reschedule if the current task isn't a
deadline one. While at it, ensure a reschedule is emitted when a
queued-but-not-current task gets boosted with an earlier deadline that
current's.

[1]: https://git.kernel.org/pub/scm/utils/rt-tests/rt-tests.git
Signed-off-by: Valentin Schneider <[email protected]>
---
RFCv1 -> RFCv2
++++++++++++++
o Fixed UP build issue (Juri)
---
kernel/sched/deadline.c | 42 ++++++++++++++++++++++++++---------------
1 file changed, 27 insertions(+), 15 deletions(-)

diff --git a/kernel/sched/deadline.c b/kernel/sched/deadline.c
index 0d97d54276cc8..71b24371a6f77 100644
--- a/kernel/sched/deadline.c
+++ b/kernel/sched/deadline.c
@@ -2663,17 +2663,20 @@ static void switched_to_dl(struct rq *rq, struct task_struct *p)
static void prio_changed_dl(struct rq *rq, struct task_struct *p,
int oldprio)
{
- if (task_on_rq_queued(p) || task_current(rq, p)) {
+ if (!task_on_rq_queued(p))
+ return;
+
#ifdef CONFIG_SMP
- /*
- * This might be too much, but unfortunately
- * we don't have the old deadline value, and
- * we can't argue if the task is increasing
- * or lowering its prio, so...
- */
- if (!rq->dl.overloaded)
- deadline_queue_pull_task(rq);
+ /*
+ * This might be too much, but unfortunately
+ * we don't have the old deadline value, and
+ * we can't argue if the task is increasing
+ * or lowering its prio, so...
+ */
+ if (!rq->dl.overloaded)
+ deadline_queue_pull_task(rq);

+ if (task_current(rq, p)) {
/*
* If we now have a earlier deadline task than p,
* then reschedule, provided p is still on this
@@ -2681,15 +2684,24 @@ static void prio_changed_dl(struct rq *rq, struct task_struct *p,
*/
if (dl_time_before(rq->dl.earliest_dl.curr, p->dl.deadline))
resched_curr(rq);
-#else
+ } else {
/*
- * Again, we don't know if p has a earlier
- * or later deadline, so let's blindly set a
- * (maybe not needed) rescheduling point.
+ * Current may not be deadline in case p was throttled but we
+ * have just replenished it (e.g. rt_mutex_setprio()).
+ *
+ * Otherwise, if p was given an earlier deadline, reschedule.
*/
- resched_curr(rq);
-#endif /* CONFIG_SMP */
+ if (!dl_task(rq->curr) ||
+ dl_time_before(p->dl.deadline, rq->curr->dl.deadline))
+ resched_curr(rq);
}
+#else
+ /*
+ * We don't know if p has a earlier or later deadline, so let's blindly
+ * set a (maybe not needed) rescheduling point.
+ */
+ resched_curr(rq);
+#endif
}

DEFINE_SCHED_CLASS(dl) = {
--
2.31.1



2023-02-07 11:59:31

by Juri Lelli

[permalink] [raw]
Subject: Re: [RFC PATCH v2] sched/deadline: Add more reschedule cases to prio_changed_dl()

Hi,

On 06/02/23 14:06, Valentin Schneider wrote:
> I've been tracking down an issue on a ~5.17ish kernel where:
>
> CPUx CPUy
>
> <DL task p0 owns an rtmutex M>
> <p0 depletes its runtime, gets throttled>
> <rq switches to the idle task>
> <DL task p1 blocks on M, boost/replenish p0>
> <No call to resched_curr() happens here>
>
> [idle task keeps running here until *something*
> accidentally sets TIF_NEED_RESCHED]
>
> On that kernel, it is quite easy to trigger using rt-tests's deadline_test
> [1] with the test running on isolated CPUs (this reduces the chance of
> something unrelated setting TIF_NEED_RESCHED on the idle tasks, making the
> issue even more obvious as the hung task detector chimes in).
>
> I haven't been able to reproduce this using a mainline kernel, even if I
> revert
>
> 2972e3050e35 ("tracing: Make trace_marker{,_raw} stream-like")
>
> which gets rid of the lock involved in the above test, *but* I cannot
> convince myself the issue isn't there from looking at the code.
>
> Make prio_changed_dl() issue a reschedule if the current task isn't a
> deadline one. While at it, ensure a reschedule is emitted when a
> queued-but-not-current task gets boosted with an earlier deadline that
> current's.
>
> [1]: https://git.kernel.org/pub/scm/utils/rt-tests/rt-tests.git
> Signed-off-by: Valentin Schneider <[email protected]>
> ---

This looks now good to me, thanks!

Acked-by: Juri Lelli <[email protected]>

Best,
Juri


Subject: Re: [RFC PATCH v2] sched/deadline: Add more reschedule cases to prio_changed_dl()

On 2/7/23 12:58, Juri Lelli wrote:
> Hi,
>
> On 06/02/23 14:06, Valentin Schneider wrote:
>> I've been tracking down an issue on a ~5.17ish kernel where:
>>
>> CPUx CPUy
>>
>> <DL task p0 owns an rtmutex M>
>> <p0 depletes its runtime, gets throttled>
>> <rq switches to the idle task>
>> <DL task p1 blocks on M, boost/replenish p0>
>> <No call to resched_curr() happens here>
>>
>> [idle task keeps running here until *something*
>> accidentally sets TIF_NEED_RESCHED]
>>
>> On that kernel, it is quite easy to trigger using rt-tests's deadline_test
>> [1] with the test running on isolated CPUs (this reduces the chance of
>> something unrelated setting TIF_NEED_RESCHED on the idle tasks, making the
>> issue even more obvious as the hung task detector chimes in).
>>
>> I haven't been able to reproduce this using a mainline kernel, even if I
>> revert
>>
>> 2972e3050e35 ("tracing: Make trace_marker{,_raw} stream-like")
>>
>> which gets rid of the lock involved in the above test, *but* I cannot
>> convince myself the issue isn't there from looking at the code.
>>
>> Make prio_changed_dl() issue a reschedule if the current task isn't a
>> deadline one. While at it, ensure a reschedule is emitted when a
>> queued-but-not-current task gets boosted with an earlier deadline that
>> current's.
>>
>> [1]: https://git.kernel.org/pub/scm/utils/rt-tests/rt-tests.git
>> Signed-off-by: Valentin Schneider <[email protected]>
>> ---
> This looks now good to me, thanks!
>
> Acked-by: Juri Lelli <[email protected]>

Reviewed-by: Daniel Bristot de Oliveira <[email protected]>

-- Daniel


Subject: [tip: sched/core] sched/deadline: Add more reschedule cases to prio_changed_dl()

The following commit has been merged into the sched/core branch of tip:

Commit-ID: 7ea98dfa44917a201e76d4fe96bf61d76e60f524
Gitweb: https://git.kernel.org/tip/7ea98dfa44917a201e76d4fe96bf61d76e60f524
Author: Valentin Schneider <[email protected]>
AuthorDate: Mon, 06 Feb 2023 14:06:12
Committer: Peter Zijlstra <[email protected]>
CommitterDate: Sat, 11 Feb 2023 11:18:10 +01:00

sched/deadline: Add more reschedule cases to prio_changed_dl()

I've been tracking down an issue on a ~5.17ish kernel where:

CPUx CPUy

<DL task p0 owns an rtmutex M>
<p0 depletes its runtime, gets throttled>
<rq switches to the idle task>
<DL task p1 blocks on M, boost/replenish p0>
<No call to resched_curr() happens here>

[idle task keeps running here until *something*
accidentally sets TIF_NEED_RESCHED]

On that kernel, it is quite easy to trigger using rt-tests's deadline_test
[1] with the test running on isolated CPUs (this reduces the chance of
something unrelated setting TIF_NEED_RESCHED on the idle tasks, making the
issue even more obvious as the hung task detector chimes in).

I haven't been able to reproduce this using a mainline kernel, even if I
revert

2972e3050e35 ("tracing: Make trace_marker{,_raw} stream-like")

which gets rid of the lock involved in the above test, *but* I cannot
convince myself the issue isn't there from looking at the code.

Make prio_changed_dl() issue a reschedule if the current task isn't a
deadline one. While at it, ensure a reschedule is emitted when a
queued-but-not-current task gets boosted with an earlier deadline that
current's.

[1]: https://git.kernel.org/pub/scm/utils/rt-tests/rt-tests.git

Signed-off-by: Valentin Schneider <[email protected]>
Signed-off-by: Peter Zijlstra (Intel) <[email protected]>
Acked-by: Juri Lelli <[email protected]>
Link: https://lore.kernel.org/r/[email protected]
---
kernel/sched/deadline.c | 42 +++++++++++++++++++++++++---------------
1 file changed, 27 insertions(+), 15 deletions(-)

diff --git a/kernel/sched/deadline.c b/kernel/sched/deadline.c
index 0d97d54..71b2437 100644
--- a/kernel/sched/deadline.c
+++ b/kernel/sched/deadline.c
@@ -2663,17 +2663,20 @@ static void switched_to_dl(struct rq *rq, struct task_struct *p)
static void prio_changed_dl(struct rq *rq, struct task_struct *p,
int oldprio)
{
- if (task_on_rq_queued(p) || task_current(rq, p)) {
+ if (!task_on_rq_queued(p))
+ return;
+
#ifdef CONFIG_SMP
- /*
- * This might be too much, but unfortunately
- * we don't have the old deadline value, and
- * we can't argue if the task is increasing
- * or lowering its prio, so...
- */
- if (!rq->dl.overloaded)
- deadline_queue_pull_task(rq);
+ /*
+ * This might be too much, but unfortunately
+ * we don't have the old deadline value, and
+ * we can't argue if the task is increasing
+ * or lowering its prio, so...
+ */
+ if (!rq->dl.overloaded)
+ deadline_queue_pull_task(rq);

+ if (task_current(rq, p)) {
/*
* If we now have a earlier deadline task than p,
* then reschedule, provided p is still on this
@@ -2681,15 +2684,24 @@ static void prio_changed_dl(struct rq *rq, struct task_struct *p,
*/
if (dl_time_before(rq->dl.earliest_dl.curr, p->dl.deadline))
resched_curr(rq);
-#else
+ } else {
/*
- * Again, we don't know if p has a earlier
- * or later deadline, so let's blindly set a
- * (maybe not needed) rescheduling point.
+ * Current may not be deadline in case p was throttled but we
+ * have just replenished it (e.g. rt_mutex_setprio()).
+ *
+ * Otherwise, if p was given an earlier deadline, reschedule.
*/
- resched_curr(rq);
-#endif /* CONFIG_SMP */
+ if (!dl_task(rq->curr) ||
+ dl_time_before(p->dl.deadline, rq->curr->dl.deadline))
+ resched_curr(rq);
}
+#else
+ /*
+ * We don't know if p has a earlier or later deadline, so let's blindly
+ * set a (maybe not needed) rescheduling point.
+ */
+ resched_curr(rq);
+#endif
}

DEFINE_SCHED_CLASS(dl) = {