[PATCH] sched/deadline: Impose global limits on sched_attr::sched_period

From: Peter Zijlstra
Date: Thu Aug 22 2019 - 12:51:25 EST


There are two DoS scenarios with SCHED_DEADLINE related to
sched_attr::sched_period:

- since access-control only looks at utilization and density, a very
large period can allow a very large runtime, which in turn can
incur a very large latency to lower priority tasks.

- for very short periods we can end up spending more time programming
the hardware timer than actually running the task.

Mitigate these by imposing limits on the period.

Signed-off-by: Peter Zijlstra (Intel) <peterz@xxxxxxxxxxxxx>
Signed-off-by: Alessio Balsini <balsini@xxxxxxxxxxx>
Cc: rostedt@xxxxxxxxxxx
Cc: mingo@xxxxxxxxxx
Cc: luca.abeni@xxxxxxxxxxxxxxx
Cc: bristot@xxxxxxxxxx
Cc: vpillai@xxxxxxxxxxxxxxxx
Cc: kernel-team@xxxxxxxxxxx
Cc: juri.lelli@xxxxxxxxxx
Cc: dietmar.eggemann@xxxxxxx
Cc: dvyukov@xxxxxxxxxx
Cc: tglx@xxxxxxxxxxxxx
---
include/linux/sched/sysctl.h | 7 ++++
kernel/sched/deadline.c | 68 ++++++++++++++++++++++++++++++++++--
kernel/sysctl.c | 14 ++++++++
3 files changed, 87 insertions(+), 2 deletions(-)

diff --git a/include/linux/sched/sysctl.h b/include/linux/sched/sysctl.h
index d4f6215ee03f7..7c8ef07e52133 100644
--- a/include/linux/sched/sysctl.h
+++ b/include/linux/sched/sysctl.h
@@ -56,6 +56,13 @@ int sched_proc_update_handler(struct ctl_table *table, int write,
extern unsigned int sysctl_sched_rt_period;
extern int sysctl_sched_rt_runtime;

+extern unsigned int sysctl_sched_dl_period_max;
+extern unsigned int sysctl_sched_dl_period_min;
+
+extern int sched_dl_period_handler(struct ctl_table *table, int write,
+ void __user *buffer, size_t *lenp,
+ loff_t *ppos);
+
#ifdef CONFIG_UCLAMP_TASK
extern unsigned int sysctl_sched_uclamp_util_min;
extern unsigned int sysctl_sched_uclamp_util_max;
diff --git a/kernel/sched/deadline.c b/kernel/sched/deadline.c
index 0b9cbfb2b1d4f..c4a6107e055c7 100644
--- a/kernel/sched/deadline.c
+++ b/kernel/sched/deadline.c
@@ -2640,6 +2640,59 @@ void __getparam_dl(struct task_struct *p, struct sched_attr *attr)
attr->sched_flags = dl_se->flags;
}

+/*
+ * Default limits for DL period: on the top end we guard against small util
+ * tasks still getting ridiculous long effective runtimes, on the bottom end we
+ * guard against timer DoS.
+ */
+unsigned int sysctl_sched_dl_period_max = 1 << 22; /* ~4.2 seconds */
+unsigned int sysctl_sched_dl_period_min = 100; /* 100 us */
+
+int sched_dl_period_handler(struct ctl_table *table, int write,
+ void __user *buffer, size_t *lenp,
+ loff_t *ppos)
+{
+ static DEFINE_MUTEX(mutex);
+ int ret;
+
+ mutex_lock(&mutex);
+ if (write) {
+ /*
+ * Use a temporary data structure to store the value read from
+ * userspace. The sysctl_sched_dl_period_{max,min} value will
+ * be updated only if the data is consistent.
+ */
+ struct ctl_table new_table = *table;
+ unsigned int max, min;
+
+ if (new_table.data == &sysctl_sched_dl_period_max) {
+ new_table.data = &max;
+ min = sysctl_sched_dl_period_min;
+ } else {
+ new_table.data = &min;
+ max = sysctl_sched_dl_period_max;
+ }
+
+ ret = proc_douintvec(&new_table, write, buffer, lenp, ppos);
+ if (!ret) {
+ if (min > max ||
+ (u64)min * NSEC_PER_USEC < 1ULL << DL_SCALE) {
+ ret = -EINVAL;
+ } else {
+ unsigned int *src = new_table.data;
+ unsigned int *dst = table->data;
+
+ WRITE_ONCE(*dst, *src);
+ }
+ }
+ } else {
+ ret = proc_douintvec(table, write, buffer, lenp, ppos);
+ }
+ mutex_unlock(&mutex);
+
+ return ret;
+}
+
/*
* This function validates the new parameters of a -deadline task.
* We ask for the deadline not being zero, and greater or equal
@@ -2652,6 +2705,8 @@ void __getparam_dl(struct task_struct *p, struct sched_attr *attr)
*/
bool __checkparam_dl(const struct sched_attr *attr)
{
+ u64 period, max, min;
+
/* special dl tasks don't actually use any parameter */
if (attr->sched_flags & SCHED_FLAG_SUGOV)
return true;
@@ -2675,12 +2730,21 @@ bool __checkparam_dl(const struct sched_attr *attr)
attr->sched_period & (1ULL << 63))
return false;

+ period = attr->sched_period;
+ if (!period)
+ period = attr->sched_deadline;
+
/* runtime <= deadline <= period (if period != 0) */
- if ((attr->sched_period != 0 &&
- attr->sched_period < attr->sched_deadline) ||
+ if (period < attr->sched_deadline ||
attr->sched_deadline < attr->sched_runtime)
return false;

+ max = (u64)READ_ONCE(sysctl_sched_dl_period_max) * NSEC_PER_USEC;
+ min = (u64)READ_ONCE(sysctl_sched_dl_period_min) * NSEC_PER_USEC;
+
+ if (period < min || period > max)
+ return false;
+
return true;
}

diff --git a/kernel/sysctl.c b/kernel/sysctl.c
index 078950d9605ba..0d07e4707e9d2 100644
--- a/kernel/sysctl.c
+++ b/kernel/sysctl.c
@@ -442,6 +442,20 @@ static struct ctl_table kern_table[] = {
.mode = 0644,
.proc_handler = sched_rt_handler,
},
+ {
+ .procname = "sched_deadline_period_max_us",
+ .data = &sysctl_sched_dl_period_max,
+ .maxlen = sizeof(unsigned int),
+ .mode = 0644,
+ .proc_handler = sched_dl_period_handler,
+ },
+ {
+ .procname = "sched_deadline_period_min_us",
+ .data = &sysctl_sched_dl_period_min,
+ .maxlen = sizeof(unsigned int),
+ .mode = 0644,
+ .proc_handler = sched_dl_period_handler,
+ },
{
.procname = "sched_rr_timeslice_ms",
.data = &sysctl_sched_rr_timeslice,
--
2.23.0.187.g17f5b7556c-goog