Re: [PATCH v14 01/39] sched/deadline: Hold a reference on the deadline donor of a boosted task
From: Peter Zijlstra
Date: Fri Dec 04 2026 - 12:27:02 EST
On Thu, Dec 03, 2026 at 09:58:15AM -0500, Mathieu Desnoyers wrote:
> +#ifdef CONFIG_RT_MUTEXES
> +/*
> + * Make @pi_se the deadline donor of @dl_se. The caller holds a reference
> + * on the task of @pi_se, unless @pi_se is @dl_se. Put the reference on
> + * the old donor.
> + */
> +static inline void dl_replace_donor(struct sched_dl_entity *dl_se,
> + struct sched_dl_entity *pi_se)
> +{
> + struct sched_dl_entity *old = dl_se->pi_se;
> +
> + WRITE_ONCE(dl_se->pi_se, pi_se);
> + if (old != dl_se)
> + put_task_struct(container_of(old, struct task_struct, dl));
> +}
> +
> +/* Reads of the donor of a waiter before the lookup falls back. */
> +#define DL_DONOR_TRIES 8
> +
> +/*
> + * Make the deadline donor of the waiter @pi_task the donor of @dl_se. A
> + * boosted entity holds a reference on the task of its donor. The donor
> + * is the deadline task at the head of the chain. It can stop waiting
> + * and exit before the chain walk reaches @dl_se: that walk can be
> + * preempted.
> + *
> + * The donor of @pi_task is read without the locks of @pi_task. Another
> + * CPU can change it meanwhile, and put the last reference on the old
> + * donor. That CPU stores the new donor first: read it again then. The
> + * old task_struct stays readable while the rq lock is held:
> + * put_task_struct() frees it through call_rcu(), and the rq lock holds
> + * off the grace period.
> + *
> + * Each failed tryget_task_struct() means that another CPU stored a newer
> + * donor. This holds as long as every writer of pi_se holds a reference
> + * on the new donor. A writer that breaks this rule could leave a dead
> + * donor in pi_se, and every read would fail on it. So stop after
> + * DL_DONOR_TRIES reads, rather than spin forever with interrupts off.
> + * Warn, and fall back to @pi_task itself. It waits on a lock, so it
> + * cannot exit. If it has no parameters of its own,
> + * replenish_dl_entity() handles it as a donor that left the class.
> + *
> + * The caller holds the rq lock and the pi_lock of the task of @dl_se.
> + */
> +static inline void dl_set_donor(struct sched_dl_entity *dl_se,
> + struct task_struct *pi_task)
> +{
> + struct sched_dl_entity *pi_se;
> + int tries;
> +
> + for (tries = 0; tries < DL_DONOR_TRIES; tries++) {
> + 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)))
> + goto set;
> + /* Pairs with the ordering of refcount_dec_and_test(). */
> + smp_rmb();
> + }
> + WARN_ON_ONCE(1);
*groan*, so it feels like this WARN is something that could be
constructed to trigger, which would be bad.
All this takes is for a high frequency pthread_cancel() or somesuch, no?
Create a ton of high prio tasks, get them involved in a boost chain and
cancel them.
Or am I missing soemthing?
> + pi_se = &pi_task->dl;
> + if (pi_se == dl_se->pi_se)
> + return;
> + if (pi_se != dl_se)
> + get_task_struct(pi_task);
> +set:
> + dl_replace_donor(dl_se, pi_se);
> +}
> +
> +/* Make @dl_se its own donor. The caller holds the rq lock of its task. */
> +static inline void dl_clear_donor(struct sched_dl_entity *dl_se)
> +{
> + dl_replace_donor(dl_se, dl_se);
> +}
> +#endif