sched/deadline: Lock the task's runqueue for dynamic sched_getattr()
PATCH 1/1 sched/deadline: Lock the task's runqueue for dynamic sched_getattr()
From: Guopeng Zhang <[email protected]>
SCHED_GETATTR_FLAG_DL_DYNAMIC reads task_rq(p) before taking the rq lock. If p migrates between the lookup and lock acquisition, the old rq lock does not protect p->dl and the absolute deadline is converted with the wrong rq clock. Directly locking rq->__lock also bypasses the rq lock indirection used by core scheduling.
The policy can also change after the initial check. Use task_rq_lock() to stabilize p's runqueue, recheck its policy, and read the dynamic deadline fields while holding the lock.
Update deadline accounting first when p is the current donor. Under proxy execution, update_curr_dl() accounts rq->donor rather than rq->curr.
--- kernel/sched/deadline.c | 36 +++++++++++++++++++++++++++--------- kernel/sched/sched.h | 2 +- kernel/sched/syscalls.c | 20 +++++++++++++++----- 3 files changed, 43 insertions(+), 15 deletions(-)
kernel/sched/deadline.c +27 -9
@@ -3853,29 +3853,47 @@ void __setparam_dl(struct task_struct *p, const struct sched_attr *attr) dl_se->dl_density =to_ratio (dl_se->dl_deadline, dl_se->dl_runtime); } -void __getparam_dl (struct task_struct *p,struct sched_attr *attr,unsigned int flags) +int __getparam_dl (struct task_struct *p,struct sched_attr *attr,unsigned int flags) {struct sched_dl_entity *dl_se = &p->dl; -struct rq *rq =task_rq (p); -u64 adj_deadline; +struct rq_flags rf; +struct rq *rq; - attr->sched_priority = p->rt_priority;if (flags &SCHED_GETATTR_FLAG_DL_DYNAMIC ) { -guard (raw_spinlock_irq)(&rq->__lock); +u64 adj_deadline; + + rq =task_rq_lock (p, &rf); +if (!task_has_dl_policy (p)) { +task_rq_unlock (rq, p, &rf); +return -EINVAL ; + } +update_rq_clock (rq); -if (task_current (rq, p)) +if (task_current_donor (rq, p))update_curr_dl (rq); + attr->sched_policy = p->policy; + attr->sched_priority = p->rt_priority; attr->sched_runtime = dl_se->runtime; adj_deadline = dl_se->deadline -rq_clock (rq) +ktime_get_ns (); attr->sched_deadline = adj_deadline; - }else { - attr->sched_runtime = dl_se->dl_runtime; - attr->sched_deadline = dl_se->dl_deadline; + attr->sched_period = dl_se->dl_period; + attr->sched_flags &= ~(SCHED_FLAG_RESET_ON_FORK |SCHED_DL_FLAGS ); +if (p->sched_reset_on_fork) + attr->sched_flags |=SCHED_FLAG_RESET_ON_FORK ; + attr->sched_flags |= dl_se->flags; +task_rq_unlock (rq, p, &rf); +return 0 ; } + + attr->sched_priority = p->rt_priority; + attr->sched_runtime = dl_se->dl_runtime; + attr->sched_deadline = dl_se->dl_deadline; attr->sched_period = dl_se->dl_period; attr->sched_flags &= ~SCHED_DL_FLAGS ; attr->sched_flags |= dl_se->flags; + +return 0 ; }/*
kernel/sched/sched.h +1 -1
@@ -356,7 +356,7 @@ extern int sched_dl_global_validate(void);extern void sched_dl_do_global (void );extern int sched_dl_overflow (struct task_struct *p,int policy,const struct sched_attr *attr);extern void __setparam_dl (struct task_struct *p,const struct sched_attr *attr); -extern void __getparam_dl (struct task_struct *p,struct sched_attr *attr,unsigned int flags); +extern int __getparam_dl (struct task_struct *p,struct sched_attr *attr,unsigned int flags);extern bool __checkparam_dl (const struct sched_attr *attr);extern bool dl_param_changed (struct task_struct *p,const struct sched_attr *attr);extern int dl_cpuset_cpumask_can_shrink (const struct cpumask *cur,const struct cpumask *trial);
kernel/sched/syscalls.c +15 -5
@@ -911,16 +911,21 @@ static int sched_copy_attr(struct sched_attr __user *uattr, struct sched_attr *areturn -E2BIG ; } -static void get_params (struct task_struct *p,struct sched_attr *attr,unsigned int flags) +static int get_params (struct task_struct *p,struct sched_attr *attr,unsigned int flags) { +if (flags) +return __getparam_dl (p, attr, flags); +if (task_has_dl_policy (p)) { -__getparam_dl (p, attr, flags); +return __getparam_dl (p, attr, flags); }else if (task_has_rt_policy (p)) { attr->sched_priority = p->rt_priority; }else { attr->sched_nice =task_nice (p); attr->sched_runtime = p->se.slice; } + +return 0 ; }/**
@@ -979,8 +984,11 @@ SYSCALL_DEFINE3(sched_setattr, pid_t, pid, struct sched_attr __user *, uattr,if (!p)return -ESRCH ; -if (attr.sched_flags &SCHED_FLAG_KEEP_PARAMS ) -get_params (p, &attr,0 ); +if (attr.sched_flags &SCHED_FLAG_KEEP_PARAMS ) { + retval =get_params (p, &attr,0 ); +if (retval) +return retval; + }return sched_setattr (p, &attr); }
@@ -1086,7 +1094,9 @@ SYSCALL_DEFINE4(sched_getattr, pid_t, pid, struct sched_attr __user *, uattr, kattr.sched_policy = p->policy;if (p->sched_reset_on_fork) kattr.sched_flags |=SCHED_FLAG_RESET_ON_FORK ; -get_params (p, &kattr, flags); + retval =get_params (p, &kattr, flags); +if (retval) +return retval; kattr.sched_flags &=SCHED_FLAG_ALL ;#ifdef CONFIG_UCLAMP_TASK
Signature · 1 line
2.43.0