linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] sched/uclamp: Avoid setting cpu.uclamp.min bigger than cpu.uclamp.max
@ 2021-06-02 12:38 Xuewen Yan
  2021-06-02 13:22 ` Quentin Perret
  0 siblings, 1 reply; 15+ messages in thread
From: Xuewen Yan @ 2021-06-02 12:38 UTC (permalink / raw)
  To: mingo, peterz, juri.lelli, vincent.guittot, dietmar.eggemann
  Cc: rostedt, bsegall, mgorman, bristot, linux-kernel, zhang.lyra, xuewyan

From: Xuewen Yan <xuewen.yan@unisoc.com>

When setting cpu.uclamp.min/max in cgroup, there is no validating
like uclamp_validate() in __sched_setscheduler(). It may cause the
cpu.uclamp.min is bigger than cpu.uclamp.max.

Although there is protection in cpu_util_update_eff():
“eff[UCLAMP_MIN] = min(eff[UCLAMP_MIN], eff[UCLAMP_MAX])”, it's better
not to let it happen.

Judging the uclamp value before setting uclamp_min/max, avoid
the cpu.uclamp.min is bigger than cpu.uclamp.max.

Signed-off-by: Xuewen Yan <xuewen.yan@unisoc.com>
---
 kernel/sched/core.c | 26 +++++++++++++++++++++++++-
 1 file changed, 25 insertions(+), 1 deletion(-)

diff --git a/kernel/sched/core.c b/kernel/sched/core.c
index 5226cc26a095..520a2da40dc9 100644
--- a/kernel/sched/core.c
+++ b/kernel/sched/core.c
@@ -8867,6 +8867,30 @@ static ssize_t cpu_uclamp_write(struct kernfs_open_file *of, char *buf,
 	rcu_read_lock();
 
 	tg = css_tg(of_css(of));
+
+	switch (clamp_id) {
+	case UCLAMP_MIN: {
+		unsigned int uc_req_max = tg->uclamp_req[UCLAMP_MAX].value;
+
+		if (req.util > uc_req_max) {
+			nbytes = -EINVAL;
+			goto unlock;
+		}
+		break;
+	}
+	case UCLAMP_MAX: {
+		unsigned int uc_req_min = tg->uclamp_req[UCLAMP_MIN].value;
+
+		if (req.util < uc_req_min) {
+			nbytes = -EINVAL;
+			goto unlock;
+		}
+		break;
+	}
+	default:
+		nbytes = -EINVAL;
+		goto unlock;
+	}
 	if (tg->uclamp_req[clamp_id].value != req.util)
 		uclamp_se_set(&tg->uclamp_req[clamp_id], req.util, false);
 
@@ -8878,7 +8902,7 @@ static ssize_t cpu_uclamp_write(struct kernfs_open_file *of, char *buf,
 
 	/* Update effective clamps to track the most restrictive value */
 	cpu_util_update_eff(of_css(of));
-
+unlock:
 	rcu_read_unlock();
 	mutex_unlock(&uclamp_mutex);
 
-- 
2.25.1


^ permalink raw reply related	[flat|nested] 15+ messages in thread

end of thread, other threads:[~2021-06-08 18:22 UTC | newest]

Thread overview: 15+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-06-02 12:38 [PATCH] sched/uclamp: Avoid setting cpu.uclamp.min bigger than cpu.uclamp.max Xuewen Yan
2021-06-02 13:22 ` Quentin Perret
2021-06-03  2:24   ` Xuewen Yan
2021-06-04 16:08     ` Qais Yousef
2021-06-04 16:22       ` Dietmar Eggemann
2021-06-05  2:14         ` Xuewen Yan
2021-06-05  2:12       ` Xuewen Yan
2021-06-05 11:49         ` Qais Yousef
2021-06-05 13:24           ` Xuewen Yan
2021-06-05 14:14             ` Qais Yousef
2021-06-07 13:49             ` Qais Yousef
2021-06-08 11:45               ` Xuewen Yan
2021-06-08 14:25                 ` Qais Yousef
2021-06-08 15:01                   ` Xuewen Yan
2021-06-08 18:21                     ` Qais Yousef

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).