Re: [PATCH v12 23/43] sched/deadline: Hold a reference on the deadline donor of a boosted task

From: Juri Lelli

Date: Mon Nov 30 2026 - 09:25:46 EST


Hello,

On 27/11/26 09:35, Mathieu Desnoyers wrote:

...

> Fix this by holding a reference on the donor's task, for each task whose
> donor is another task. dl_set_donor() gives a task the donor of its top
> waiter. dl_clear_donor() ends the boost. Both move the reference. A
> late walk can still leave P on the parameters of an exited donor for a
> while. P now reads them from valid memory.

Thanks for implementing this. As far as I can see it covers all the
writers of pi_se, and it closes the use after free without needing the
two rtmutex patches before it.

...

> Cc: stable@xxxxxxxxxxxxxxx # 6.16.x and older: 8671bad873eb: sched: Do not call __put_task_struct() on rt if pi_blocked_on is set
> Cc: stable@xxxxxxxxxxxxxxx

Nit: the second line looks redundant. Also, tryget_task_struct() is only
there since v6.12, so older trees need something else on top of
8671bad873eb.

...

> +static inline void dl_set_donor(struct sched_dl_entity *dl_se,
> + struct task_struct *pi_task)
> +{
> + struct sched_dl_entity *pi_se;
> +
> + for (;;) {
> + pi_se = READ_ONCE(pi_task->dl.pi_se);
> + if (pi_se == dl_se->pi_se)
> + return;
> + if (pi_se == dl_se ||
> + tryget_task_struct(container_of(pi_se, struct task_struct, dl)))
> + break;
> + /* Pairs with the ordering of refcount_dec_and_test(). */
> + smp_rmb();
> + }
> + dl_replace_donor(dl_se, pi_se);
> +}

I convinced myself that this terminates: tryget can only fail once the
last reference on the donor is gone, which means the waiter dropped its
own. The waiter does that only in dl_replace_donor(), after storing the
new donor in its pi_se, so the re-read after the smp_rmb() finds a
newer pointer. And the rq lock keeps the old task_struct from being
freed meanwhile.

Still, this is an open-ended loop with the rq lock and the pi_lock held
and interrupts disabled, and it ends only as long as every writer of
pi_se that points it at another task holds a reference on that task. We
have a few of them already and later patches rework this code. If one of
them ever gets it wrong we spin here forever, with nothing telling us
why.

Would it make sense to give up after a few tries, warn, and fall back to
the top waiter itself? It is blocked, so it cannot go away, and a waiter
with no parameters of its own is what "sched/deadline: Do not replenish
a boost from cleared parameters" already handles. The boost is then less
accurate in a case that should never happen, which looks better to me
than a silent lockup. Giving up also puts a hard limit on the time spent
here with interrupts off.

One more thing. Since this fixes a use after free and seems to stand on
its own, can we move it ahead of the rest of the series?

Thanks,
Juri