Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

sched/deadline: Add reporting of runtime left & abs deadline to sched_getattr() for DEADLINE tasks

The SCHED_DEADLINE scheduler allows reading the statically configured
run-time, deadline, and period parameters through the sched_getattr()
system call. However, there is no immediate way to access, from user space,
the current parameters used within the scheduler: the instantaneous runtime
left in the current cycle, as well as the current absolute deadline.

The `flags' sched_getattr() parameter, so far mandated to contain zero,
now supports the SCHED_GETATTR_FLAG_DL_DYNAMIC=1 flag, to request
retrieval of the leftover runtime and absolute deadline, converted to a
CLOCK_MONOTONIC reference, instead of the statically configured parameters.

This feature is useful for adaptive SCHED_DEADLINE tasks that need to
modify their behavior depending on whether or not there is enough runtime
left in the current period, and/or what is the current absolute deadline.

Notes:
- before returning the instantaneous parameters, the runtime is updated;
- the abs deadline is returned shifted from rq_clock() to ktime_get_ns(),
in CLOCK_MONOTONIC reference; this causes multiple invocations from the
same period to return values that may differ for a few ns (showing some
small drift), albeit the deadline doesn't move, in rq_clock() reference;
- the abs deadline value returned to user-space, as unsigned 64-bit value,
can represent nearly 585 years since boot time;
- setting flags=0 provides the old behavior (retrieve static parameters).

See also the notes from discussion held at OSPM 2025 on the topic
"Making user space aware of current deadline-scheduler parameters".

Signed-off-by: Tommaso Cucinotta <tommaso.cucinotta@santannapisa.it>
Signed-off-by: Peter Zijlstra (Intel) <peterz@infradead.org>
Tested-by: Matteo Martelli <matteo.martelli@codethink.co.uk>
Link: https://patch.msgid.link/20250912053937.31636-2-tommaso.cucinotta@santannapisa.it

authored by

Tommaso Cucinotta and committed by
Peter Zijlstra
2e7af192 fd54d81c

+31 -9
+3
include/uapi/linux/sched.h
··· 146 146 SCHED_FLAG_KEEP_ALL | \ 147 147 SCHED_FLAG_UTIL_CLAMP) 148 148 149 + /* Only for sched_getattr() own flag param, if task is SCHED_DEADLINE */ 150 + #define SCHED_GETATTR_FLAG_DL_DYNAMIC 0x01 151 + 149 152 #endif /* _UAPI_LINUX_SCHED_H */
+16 -3
kernel/sched/deadline.c
··· 3617 3617 dl_se->dl_density = to_ratio(dl_se->dl_deadline, dl_se->dl_runtime); 3618 3618 } 3619 3619 3620 - void __getparam_dl(struct task_struct *p, struct sched_attr *attr) 3620 + void __getparam_dl(struct task_struct *p, struct sched_attr *attr, unsigned int flags) 3621 3621 { 3622 3622 struct sched_dl_entity *dl_se = &p->dl; 3623 + struct rq *rq = task_rq(p); 3624 + u64 adj_deadline; 3623 3625 3624 3626 attr->sched_priority = p->rt_priority; 3625 - attr->sched_runtime = dl_se->dl_runtime; 3626 - attr->sched_deadline = dl_se->dl_deadline; 3627 + if (flags & SCHED_GETATTR_FLAG_DL_DYNAMIC) { 3628 + guard(raw_spinlock_irq)(&rq->__lock); 3629 + update_rq_clock(rq); 3630 + if (task_current(rq, p)) 3631 + update_curr_dl(rq); 3632 + 3633 + attr->sched_runtime = dl_se->runtime; 3634 + adj_deadline = dl_se->deadline - rq_clock(rq) + ktime_get_ns(); 3635 + attr->sched_deadline = adj_deadline; 3636 + } else { 3637 + attr->sched_runtime = dl_se->dl_runtime; 3638 + attr->sched_deadline = dl_se->dl_deadline; 3639 + } 3627 3640 attr->sched_period = dl_se->dl_period; 3628 3641 attr->sched_flags &= ~SCHED_DL_FLAGS; 3629 3642 attr->sched_flags |= dl_se->flags;
+1 -1
kernel/sched/sched.h
··· 356 356 extern void sched_dl_do_global(void); 357 357 extern int sched_dl_overflow(struct task_struct *p, int policy, const struct sched_attr *attr); 358 358 extern void __setparam_dl(struct task_struct *p, const struct sched_attr *attr); 359 - extern void __getparam_dl(struct task_struct *p, struct sched_attr *attr); 359 + extern void __getparam_dl(struct task_struct *p, struct sched_attr *attr, unsigned int flags); 360 360 extern bool __checkparam_dl(const struct sched_attr *attr); 361 361 extern bool dl_param_changed(struct task_struct *p, const struct sched_attr *attr); 362 362 extern int dl_cpuset_cpumask_can_shrink(const struct cpumask *cur, const struct cpumask *trial);
+11 -5
kernel/sched/syscalls.c
··· 881 881 return -E2BIG; 882 882 } 883 883 884 - static void get_params(struct task_struct *p, struct sched_attr *attr) 884 + static void get_params(struct task_struct *p, struct sched_attr *attr, unsigned int flags) 885 885 { 886 886 if (task_has_dl_policy(p)) { 887 - __getparam_dl(p, attr); 887 + __getparam_dl(p, attr, flags); 888 888 } else if (task_has_rt_policy(p)) { 889 889 attr->sched_priority = p->rt_priority; 890 890 } else { ··· 950 950 return -ESRCH; 951 951 952 952 if (attr.sched_flags & SCHED_FLAG_KEEP_PARAMS) 953 - get_params(p, &attr); 953 + get_params(p, &attr, 0); 954 954 955 955 return sched_setattr(p, &attr); 956 956 } ··· 1035 1035 int retval; 1036 1036 1037 1037 if (unlikely(!uattr || pid < 0 || usize > PAGE_SIZE || 1038 - usize < SCHED_ATTR_SIZE_VER0 || flags)) 1038 + usize < SCHED_ATTR_SIZE_VER0)) 1039 1039 return -EINVAL; 1040 1040 1041 1041 scoped_guard (rcu) { 1042 1042 p = find_process_by_pid(pid); 1043 1043 if (!p) 1044 1044 return -ESRCH; 1045 + 1046 + if (flags) { 1047 + if (!task_has_dl_policy(p) || 1048 + flags != SCHED_GETATTR_FLAG_DL_DYNAMIC) 1049 + return -EINVAL; 1050 + } 1045 1051 1046 1052 retval = security_task_getscheduler(p); 1047 1053 if (retval) ··· 1056 1050 kattr.sched_policy = p->policy; 1057 1051 if (p->sched_reset_on_fork) 1058 1052 kattr.sched_flags |= SCHED_FLAG_RESET_ON_FORK; 1059 - get_params(p, &kattr); 1053 + get_params(p, &kattr, flags); 1060 1054 kattr.sched_flags &= SCHED_FLAG_ALL; 1061 1055 1062 1056 #ifdef CONFIG_UCLAMP_TASK