Re: [PATCH] fork: fix default_timer_slack_ns inheritance from RT parent

"MOESSBAUER, Felix" <[email protected]>
Newsgroups gmane.linux.kernel,gmane.linux.kernel.mm
Message-ID <[email protected]>
On Thu, 2026-08-06 at 16:20 +0800, Guanyou.Chen wrote:
> Per prctl(2), "Timer slack is not applied to threads that are scheduled
> under a real-time scheduling policy." RT tasks' timer_slack_ns is
> forcibly 0 - not a user-chosen value but a "not applicable" state.
> 
> When copy_process() sets the child's default_timer_slack_ns from the
> parent's timer_slack_ns, it inherits this forced 0 for RT parents. This
> corrupts the child's reset target, making prctl(PR_SET_TIMERSLACK, 0)
> and sched_setscheduler() back to NORMAL unable to restore a meaningful
> default.
> 
> Fix this by using default_timer_slack_ns (which preserves the pre-RT
> value) when the parent is RT/DL. For non-RT parents, timer_slack_ns is
> a meaningful user value and the existing behavior is preserved.
> 
> Fixes: ed4fb6d7ef68 ("hrtimer: Use and report correct timerslack values for realtime tasks")
> Depends-on: 63c1a12bc0e0 ("sched: restore timer_slack_ns when resetting RT policy on fork")
> Reported-by: Qiaoting.Lin <[email protected]>
> Signed-off-by: Guanyou.Chen <[email protected]>
> Signed-off-by: Chunhui.Li <[email protected]>
> ---
>  kernel/fork.c | 6 +++++-
>  1 file changed, 5 insertions(+), 1 deletion(-)
> 
> diff --git a/kernel/fork.c b/kernel/fork.c
> index 65113a304518..bc4df18bfd90 100644
> --- a/kernel/fork.c
> +++ b/kernel/fork.c
> @@ -23,6 +23,7 @@
>  #include <linux/sched/task_stack.h>
>  #include <linux/sched/cputime.h>
>  #include <linux/sched/ext.h>
> +#include <linux/sched/rt.h>
>  #include <linux/seq_file.h>
>  #include <linux/rtmutex.h>
>  #include <linux/init.h>
> @@ -2133,7 +2134,10 @@ __latent_entropy struct task_struct *copy_process(
>  	retval = -EAGAIN;
>  #endif
>  
> -	p->default_timer_slack_ns = current->timer_slack_ns;

Hi, I'm wondering why it was implemented like that before, instead of

p->default_timer_slack_ns = current->default_timer_slack_ns

along with setting the default _timer_slack_ns of the init task as
well. Currently this is needed because the init_task only sets the
timer_slack_ns, which is then copied by all forks.

That logic goes back to 6976675d94042 ("hrtimer: create a "timer_slack"
field in the task struct") from 17 years ago.

Felix

> +	if (rt_or_dl_task_policy(current))
> +		p->default_timer_slack_ns = current->default_timer_slack_ns;
> +	else
> +		p->default_timer_slack_ns = current->timer_slack_ns;
>  
>  #ifdef CONFIG_PSI
>  	p->psi_flags = 0;
> -- 
> 2.34.1
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.