Re: [PATCH v5 3/7] sched: Allow sched_{get,set}attr to change latency_nice of the task
From: Vincent Guittot
Date: Wed Oct 12 2022 - 11:44:58 EST
On Wed, 12 Oct 2022 at 17:07, K Prateek Nayak <kprateek.nayak@xxxxxxx> wrote:
>
> Hello Vincent,
>
> On 9/25/2022 8:09 PM, Vincent Guittot wrote:
> > From: Parth Shah <parth@xxxxxxxxxxxxx>
> >
> > Introduce the latency_nice attribute to sched_attr and provide a
> > mechanism to change the value with the use of sched_setattr/sched_getattr
> > syscall.
> >
> > Also add new flag "SCHED_FLAG_LATENCY_NICE" to hint the change in
> > latency_nice of the task on every sched_setattr syscall.
> >
> > Signed-off-by: Parth Shah <parth@xxxxxxxxxxxxx>
> > [rebase and add a dedicated __setscheduler_latency ]
> > Signed-off-by: Vincent Guittot <vincent.guittot@xxxxxxxxxx>
> > ---
> > include/uapi/linux/sched.h | 4 +++-
> > include/uapi/linux/sched/types.h | 19 +++++++++++++++++++
> > kernel/sched/core.c | 24 ++++++++++++++++++++++++
> > tools/include/uapi/linux/sched.h | 4 +++-
> > 4 files changed, 49 insertions(+), 2 deletions(-)
> >
> > diff --git a/include/uapi/linux/sched.h b/include/uapi/linux/sched.h
> > index 3bac0a8ceab2..b2e932c25be6 100644
> > --- a/include/uapi/linux/sched.h
> > +++ b/include/uapi/linux/sched.h
> > @@ -132,6 +132,7 @@ struct clone_args {
> > #define SCHED_FLAG_KEEP_PARAMS 0x10
> > #define SCHED_FLAG_UTIL_CLAMP_MIN 0x20
> > #define SCHED_FLAG_UTIL_CLAMP_MAX 0x40
> > +#define SCHED_FLAG_LATENCY_NICE 0x80
>
> [1]
>
> >
> > #define SCHED_FLAG_KEEP_ALL (SCHED_FLAG_KEEP_POLICY | \
> > SCHED_FLAG_KEEP_PARAMS)
> > @@ -143,6 +144,7 @@ struct clone_args {
> > SCHED_FLAG_RECLAIM | \
> > SCHED_FLAG_DL_OVERRUN | \
> > SCHED_FLAG_KEEP_ALL | \
> > - SCHED_FLAG_UTIL_CLAMP)
> > + SCHED_FLAG_UTIL_CLAMP | \
> > + SCHED_FLAG_LATENCY_NICE)
> >
> > #endif /* _UAPI_LINUX_SCHED_H */
> > diff --git a/include/uapi/linux/sched/types.h b/include/uapi/linux/sched/types.h
> > index f2c4589d4dbf..db1e8199e8c8 100644
> > --- a/include/uapi/linux/sched/types.h
> > +++ b/include/uapi/linux/sched/types.h
> > @@ -10,6 +10,7 @@ struct sched_param {
> >
> > #define SCHED_ATTR_SIZE_VER0 48 /* sizeof first published struct */
> > #define SCHED_ATTR_SIZE_VER1 56 /* add: util_{min,max} */
> > +#define SCHED_ATTR_SIZE_VER2 60 /* add: latency_nice */
> >
> > /*
> > * Extended scheduling parameters data structure.
> > @@ -98,6 +99,22 @@ struct sched_param {
> > * scheduled on a CPU with no more capacity than the specified value.
> > *
> > * A task utilization boundary can be reset by setting the attribute to -1.
> > + *
> > + * Latency Tolerance Attributes
> > + * ===========================
> > + *
> > + * A subset of sched_attr attributes allows to specify the relative latency
> > + * requirements of a task with respect to the other tasks running/queued in the
> > + * system.
> > + *
> > + * @ sched_latency_nice task's latency_nice value
> > + *
> > + * The latency_nice of a task can have any value in a range of
> > + * [MIN_LATENCY_NICE..MAX_LATENCY_NICE].
> > + *
> > + * A task with latency_nice with the value of LATENCY_NICE_MIN can be
> > + * taken for a task requiring a lower latency as opposed to the task with
> > + * higher latency_nice.
> > */
> > struct sched_attr {
> > __u32 size;
> > @@ -120,6 +137,8 @@ struct sched_attr {
> > __u32 sched_util_min;
> > __u32 sched_util_max;
> >
> > + /* latency requirement hints */
> > + __s32 sched_latency_nice;
> > };
> >
> > #endif /* _UAPI_LINUX_SCHED_TYPES_H */
> > diff --git a/kernel/sched/core.c b/kernel/sched/core.c
> > index ada2d05bd894..6a6116ea4c2c 100644
> > --- a/kernel/sched/core.c
> > +++ b/kernel/sched/core.c
> > @@ -7318,6 +7318,14 @@ static void __setscheduler_params(struct task_struct *p,
> > p->rt_priority = attr->sched_priority;
> > p->normal_prio = normal_prio(p);
> > set_load_weight(p, true);
> > +
> > +}
> > +
> > +static void __setscheduler_latency(struct task_struct *p,
> > + const struct sched_attr *attr)
> > +{
> > + if (attr->sched_flags & SCHED_FLAG_LATENCY_NICE)
> > + p->latency_nice = attr->sched_latency_nice;
> > }
> >
> > /*
> > @@ -7460,6 +7468,13 @@ static int __sched_setscheduler(struct task_struct *p,
> > return retval;
> > }
> >
> > + if (attr->sched_flags & SCHED_FLAG_LATENCY_NICE) {
> > + if (attr->sched_latency_nice > MAX_LATENCY_NICE)
> > + return -EINVAL;
> > + if (attr->sched_latency_nice < MIN_LATENCY_NICE)
> > + return -EINVAL;
> > + }
> > +
> > if (pi)
> > cpuset_read_lock();
> >
> > @@ -7494,6 +7509,9 @@ static int __sched_setscheduler(struct task_struct *p,
> > goto change;
> > if (attr->sched_flags & SCHED_FLAG_UTIL_CLAMP)
> > goto change;
> > + if (attr->sched_flags & SCHED_FLAG_LATENCY_NICE &&
> > + attr->sched_latency_nice != p->latency_nice)
> > + goto change;
> >
> > p->sched_reset_on_fork = reset_on_fork;
> > retval = 0;
> > @@ -7582,6 +7600,7 @@ static int __sched_setscheduler(struct task_struct *p,
> > __setscheduler_params(p, attr);
> > __setscheduler_prio(p, newprio);
> > }
> > + __setscheduler_latency(p, attr);
> > __setscheduler_uclamp(p, attr);
> >
> > if (queued) {
> > @@ -7792,6 +7811,9 @@ static int sched_copy_attr(struct sched_attr __user *uattr, struct sched_attr *a
> > size < SCHED_ATTR_SIZE_VER1)
> > return -EINVAL;
> >
> > + if ((attr->sched_flags & SCHED_FLAG_LATENCY_NICE) &&
> > + size < SCHED_ATTR_SIZE_VER2)
> > + return -EINVAL;
> > /*
> > * XXX: Do we want to be lenient like existing syscalls; or do we want
> > * to be strict and return an error on out-of-bounds values?
> > @@ -8029,6 +8051,8 @@ SYSCALL_DEFINE4(sched_getattr, pid_t, pid, struct sched_attr __user *, uattr,
> > get_params(p, &kattr);
> > kattr.sched_flags &= SCHED_FLAG_ALL;
> >
> > + kattr.sched_latency_nice = p->latency_nice;
> > +
> > #ifdef CONFIG_UCLAMP_TASK
> > /*
> > * This could race with another potential updater, but this is fine
> > diff --git a/tools/include/uapi/linux/sched.h b/tools/include/uapi/linux/sched.h
> > index 3bac0a8ceab2..ecc4884bfe4b 100644
> > --- a/tools/include/uapi/linux/sched.h
> > +++ b/tools/include/uapi/linux/sched.h
> > @@ -132,6 +132,7 @@ struct clone_args {
> > #define SCHED_FLAG_KEEP_PARAMS 0x10
> > #define SCHED_FLAG_UTIL_CLAMP_MIN 0x20
> > #define SCHED_FLAG_UTIL_CLAMP_MAX 0x40
> > +#define SCHED_FLAG_LATENCY_NICE 0X80
>
> Small nit.
> Can you change the "x" in "0X80" to lower case here to make it
> consistent with [1] and other #define in the same file. When building
> tools like perf, I see the following warning:
Yes, that's a typo. It should be 0x80
>
> Warning: Kernel ABI header at 'tools/include/uapi/linux/sched.h' differs from latest version at 'include/uapi/linux/sched.h'
> diff -u tools/include/uapi/linux/sched.h include/uapi/linux/sched.h
>
> Following is the output of
> diff -u tools/include/uapi/linux/sched.h include/uapi/linux/sched.h
>
> --- tools/include/uapi/linux/sched.h 2022-10-12 14:56:00.925360275 +0000
> +++ include/uapi/linux/sched.h 2022-10-12 14:56:00.917360219 +0000
> @@ -132,7 +132,7 @@
> #define SCHED_FLAG_KEEP_PARAMS 0x10
> #define SCHED_FLAG_UTIL_CLAMP_MIN 0x20
> #define SCHED_FLAG_UTIL_CLAMP_MAX 0x40
> -#define SCHED_FLAG_LATENCY_NICE 0X80
> +#define SCHED_FLAG_LATENCY_NICE 0x80
>
> #define SCHED_FLAG_KEEP_ALL (SCHED_FLAG_KEEP_POLICY | \
> SCHED_FLAG_KEEP_PARAMS)
> --
>
> >
> > #define SCHED_FLAG_KEEP_ALL (SCHED_FLAG_KEEP_POLICY | \
> > SCHED_FLAG_KEEP_PARAMS)
> > @@ -143,6 +144,7 @@ struct clone_args {
> > SCHED_FLAG_RECLAIM | \
> > SCHED_FLAG_DL_OVERRUN | \
> > SCHED_FLAG_KEEP_ALL | \
> > - SCHED_FLAG_UTIL_CLAMP)
> > + SCHED_FLAG_UTIL_CLAMP | \
> > + SCHED_FLAG_LATENCY_NICE)
> >
> > #endif /* _UAPI_LINUX_SCHED_H */
>
> --
> Thanks and Regards,
> Prateek