linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH v4 1/1] sched/uclamp: add SCHED_FLAG_UTIL_CLAMP_RESET flag to reset uclamp
@ 2020-11-01  8:44 Yun Hsiang
  2020-11-02 20:51 ` kernel test robot
  0 siblings, 1 reply; 2+ messages in thread
From: Yun Hsiang @ 2020-11-01  8:44 UTC (permalink / raw)
  To: dietmar.eggemann, peterz
  Cc: linux-kernel, qais.yousef, patrick.bellasi, Yun Hsiang

If the user wants to stop controlling uclamp and let the task inherit
the value from the group, we need a method to reset.

Add SCHED_FLAG_UTIL_CLAMP_RESET flag to allow the user to reset uclamp via
sched_setattr syscall.

The policy is
_CLAMP_RESET                           => reset both min and max
_CLAMP_RESET | _CLAMP_MIN              => reset min value
_CLAMP_RESET | _CLAMP_MAX              => reset max value
_CLAMP_RESET | _CLAMP_MIN | _CLAMP_MAX => reset both min and max

Signed-off-by: Yun Hsiang <hsiang023167@gmail.com>
---
 include/uapi/linux/sched.h |  7 +++--
 kernel/sched/core.c        | 59 ++++++++++++++++++++++++++++----------
 2 files changed, 49 insertions(+), 17 deletions(-)

diff --git a/include/uapi/linux/sched.h b/include/uapi/linux/sched.h
index 3bac0a8ceab2..6c823ddb1a1e 100644
--- a/include/uapi/linux/sched.h
+++ b/include/uapi/linux/sched.h
@@ -132,17 +132,20 @@ 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_UTIL_CLAMP_RESET	0x80
 
 #define SCHED_FLAG_KEEP_ALL	(SCHED_FLAG_KEEP_POLICY | \
 				 SCHED_FLAG_KEEP_PARAMS)
 
 #define SCHED_FLAG_UTIL_CLAMP	(SCHED_FLAG_UTIL_CLAMP_MIN | \
-				 SCHED_FLAG_UTIL_CLAMP_MAX)
+				 SCHED_FLAG_UTIL_CLAMP_MAX | \
+				 SCHED_FLAG_UTIL_CLAMP_RESET)
 
 #define SCHED_FLAG_ALL	(SCHED_FLAG_RESET_ON_FORK	| \
 			 SCHED_FLAG_RECLAIM		| \
 			 SCHED_FLAG_DL_OVERRUN		| \
 			 SCHED_FLAG_KEEP_ALL		| \
-			 SCHED_FLAG_UTIL_CLAMP)
+			 SCHED_FLAG_UTIL_CLAMP		| \
+			 SCHED_FLAG_UTIL_CLAMP_RESET)
 
 #endif /* _UAPI_LINUX_SCHED_H */
diff --git a/kernel/sched/core.c b/kernel/sched/core.c
index 8160ab5263f8..2b644f4bf2eb 100644
--- a/kernel/sched/core.c
+++ b/kernel/sched/core.c
@@ -1004,7 +1004,7 @@ unsigned int uclamp_rq_max_value(struct rq *rq, enum uclamp_id clamp_id,
 	return uclamp_idle_value(rq, clamp_id, clamp_value);
 }
 
-static void __uclamp_update_util_min_rt_default(struct task_struct *p)
+static inline void __uclamp_update_util_min_rt_default(struct task_struct *p)
 {
 	unsigned int default_util_min;
 	struct uclamp_se *uc_se;
@@ -1413,8 +1413,14 @@ int sysctl_sched_uclamp_handler(struct ctl_table *table, int write,
 static int uclamp_validate(struct task_struct *p,
 			   const struct sched_attr *attr)
 {
-	unsigned int lower_bound = p->uclamp_req[UCLAMP_MIN].value;
-	unsigned int upper_bound = p->uclamp_req[UCLAMP_MAX].value;
+	unsigned int lower_bound, upper_bound;
+
+	/* Do not check uclamp attributes values in reset case. */
+	if (attr->sched_flags & SCHED_FLAG_UTIL_CLAMP_RESET)
+		return 0;
+
+	lower_bound = p->uclamp_req[UCLAMP_MIN].value;
+	upper_bound = p->uclamp_req[UCLAMP_MAX].value;
 
 	if (attr->sched_flags & SCHED_FLAG_UTIL_CLAMP_MIN)
 		lower_bound = attr->sched_util_min;
@@ -1438,20 +1444,43 @@ static int uclamp_validate(struct task_struct *p,
 	return 0;
 }
 
+static int uclamp_reset(enum uclamp_id clamp_id, unsigned long flags)
+{
+	/* No _UCLAMP_RESET flag set: do not reset */
+	if (!(flags & SCHED_FLAG_UTIL_CLAMP_RESET))
+		return false;
+
+	/* Only _UCLAMP_RESET flag set: reset both clamps */
+	if (!(flags & (SCHED_FLAG_UTIL_CLAMP_MIN | SCHED_FLAG_UTIL_CLAMP_MAX)))
+		return true;
+
+	/* Both _UCLAMP_RESET and _UCLAMP_MIN flags are set: reset only min */
+	if ((flags & SCHED_FLAG_UTIL_CLAMP_MIN) && clamp_id == UCLAMP_MIN)
+		return true;
+
+	/* Both _UCLAMP_RESET and _UCLAMP_MAX flags are set: reset only max */
+	if ((flags & SCHED_FLAG_UTIL_CLAMP_MAX) && clamp_id == UCLAMP_MAX)
+		return true;
+
+	return false;
+}
+
 static void __setscheduler_uclamp(struct task_struct *p,
 				  const struct sched_attr *attr)
 {
+	unsigned int clamp_value;
 	enum uclamp_id clamp_id;
 
 	/*
-	 * On scheduling class change, reset to default clamps for tasks
-	 * without a task-specific value.
+	 * Reset to default clamps on forced _UCLAMP_RESET (always) and
+	 * for tasks without a task-specific value (on scheduling class change).
 	 */
 	for_each_clamp_id(clamp_id) {
 		struct uclamp_se *uc_se = &p->uclamp_req[clamp_id];
 
 		/* Keep using defined clamps across class changes */
-		if (uc_se->user_defined)
+		if (!uclamp_reset(clamp_id, attr->sched_flags) &&
+				uc_se->user_defined)
 			continue;
 
 		/*
@@ -1459,24 +1488,24 @@ static void __setscheduler_uclamp(struct task_struct *p,
 		 * at runtime.
 		 */
 		if (unlikely(rt_task(p) && clamp_id == UCLAMP_MIN))
-			__uclamp_update_util_min_rt_default(p);
+			clamp_value = sysctl_sched_uclamp_util_min_rt_default;
 		else
-			uclamp_se_set(uc_se, uclamp_none(clamp_id), false);
+			clamp_value = uclamp_none(clamp_id);
 
+		uclamp_se_set(uc_se, uclamp_none(clamp_id), false);
 	}
 
-	if (likely(!(attr->sched_flags & SCHED_FLAG_UTIL_CLAMP)))
+	if (likely(!(attr->sched_flags && SCHED_FLAG_UTIL_CLAMP)) ||
+		attr->sched_flags & SCHED_FLAG_UTIL_CLAMP_RESET)
 		return;
 
-	if (attr->sched_flags & SCHED_FLAG_UTIL_CLAMP_MIN) {
+	if (attr->sched_flags & SCHED_FLAG_UTIL_CLAMP_MIN)
 		uclamp_se_set(&p->uclamp_req[UCLAMP_MIN],
-			      attr->sched_util_min, true);
-	}
+				attr->sched_util_min, true);
 
-	if (attr->sched_flags & SCHED_FLAG_UTIL_CLAMP_MAX) {
+	if (attr->sched_flags & SCHED_FLAG_UTIL_CLAMP_MAX)
 		uclamp_se_set(&p->uclamp_req[UCLAMP_MAX],
-			      attr->sched_util_max, true);
-	}
+				attr->sched_util_max, true);
 }
 
 static void uclamp_fork(struct task_struct *p)
-- 
2.25.1


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

* Re: [PATCH v4 1/1] sched/uclamp: add SCHED_FLAG_UTIL_CLAMP_RESET flag to reset uclamp
  2020-11-01  8:44 [PATCH v4 1/1] sched/uclamp: add SCHED_FLAG_UTIL_CLAMP_RESET flag to reset uclamp Yun Hsiang
@ 2020-11-02 20:51 ` kernel test robot
  0 siblings, 0 replies; 2+ messages in thread
From: kernel test robot @ 2020-11-02 20:51 UTC (permalink / raw)
  To: Yun Hsiang, dietmar.eggemann, peterz
  Cc: kbuild-all, linux-kernel, qais.yousef, patrick.bellasi, Yun Hsiang

[-- Attachment #1: Type: text/plain, Size: 3515 bytes --]

Hi Yun,

Thank you for the patch! Perhaps something to improve:

[auto build test WARNING on tip/sched/core]
[also build test WARNING on v5.10-rc2 next-20201102]
[If your patch is applied to the wrong git tree, kindly drop us a note.
And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch]

url:    https://github.com/0day-ci/linux/commits/Yun-Hsiang/sched-uclamp-add-SCHED_FLAG_UTIL_CLAMP_RESET-flag-to-reset-uclamp/20201101-164635
base:   https://git.kernel.org/pub/scm/linux/kernel/git/tip/tip.git d8fcb81f1acf651a0e50eacecca43d0524984f87
config: x86_64-allyesconfig (attached as .config)
compiler: gcc-9 (Debian 9.3.0-15) 9.3.0
reproduce (this is a W=1 build):
        # https://github.com/0day-ci/linux/commit/b705c245791a37b85013c6604ee1301022312dc5
        git remote add linux-review https://github.com/0day-ci/linux
        git fetch --no-tags linux-review Yun-Hsiang/sched-uclamp-add-SCHED_FLAG_UTIL_CLAMP_RESET-flag-to-reset-uclamp/20201101-164635
        git checkout b705c245791a37b85013c6604ee1301022312dc5
        # save the attached .config to linux build tree
        make W=1 ARCH=x86_64 

If you fix the issue, kindly add following tag as appropriate
Reported-by: kernel test robot <lkp@intel.com>

All warnings (new ones prefixed by >>):

   kernel/sched/core.c: In function '__setscheduler_uclamp':
>> kernel/sched/core.c:1471:15: warning: variable 'clamp_value' set but not used [-Wunused-but-set-variable]
    1471 |  unsigned int clamp_value;
         |               ^~~~~~~~~~~
   kernel/sched/core.c: At top level:
   kernel/sched/core.c:2405:6: warning: no previous prototype for 'sched_set_stop_task' [-Wmissing-prototypes]
    2405 | void sched_set_stop_task(int cpu, struct task_struct *stop)
         |      ^~~~~~~~~~~~~~~~~~~

vim +/clamp_value +1471 kernel/sched/core.c

  1467	
  1468	static void __setscheduler_uclamp(struct task_struct *p,
  1469					  const struct sched_attr *attr)
  1470	{
> 1471		unsigned int clamp_value;
  1472		enum uclamp_id clamp_id;
  1473	
  1474		/*
  1475		 * Reset to default clamps on forced _UCLAMP_RESET (always) and
  1476		 * for tasks without a task-specific value (on scheduling class change).
  1477		 */
  1478		for_each_clamp_id(clamp_id) {
  1479			struct uclamp_se *uc_se = &p->uclamp_req[clamp_id];
  1480	
  1481			/* Keep using defined clamps across class changes */
  1482			if (!uclamp_reset(clamp_id, attr->sched_flags) &&
  1483					uc_se->user_defined)
  1484				continue;
  1485	
  1486			/*
  1487			 * RT by default have a 100% boost value that could be modified
  1488			 * at runtime.
  1489			 */
  1490			if (unlikely(rt_task(p) && clamp_id == UCLAMP_MIN))
  1491				clamp_value = sysctl_sched_uclamp_util_min_rt_default;
  1492			else
  1493				clamp_value = uclamp_none(clamp_id);
  1494	
  1495			uclamp_se_set(uc_se, uclamp_none(clamp_id), false);
  1496		}
  1497	
  1498		if (likely(!(attr->sched_flags && SCHED_FLAG_UTIL_CLAMP)) ||
  1499			attr->sched_flags & SCHED_FLAG_UTIL_CLAMP_RESET)
  1500			return;
  1501	
  1502		if (attr->sched_flags & SCHED_FLAG_UTIL_CLAMP_MIN)
  1503			uclamp_se_set(&p->uclamp_req[UCLAMP_MIN],
  1504					attr->sched_util_min, true);
  1505	
  1506		if (attr->sched_flags & SCHED_FLAG_UTIL_CLAMP_MAX)
  1507			uclamp_se_set(&p->uclamp_req[UCLAMP_MAX],
  1508					attr->sched_util_max, true);
  1509	}
  1510	

---
0-DAY CI Kernel Test Service, Intel Corporation
https://lists.01.org/hyperkitty/list/kbuild-all@lists.01.org

[-- Attachment #2: .config.gz --]
[-- Type: application/gzip, Size: 77103 bytes --]

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

end of thread, other threads:[~2020-11-02 20:51 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-11-01  8:44 [PATCH v4 1/1] sched/uclamp: add SCHED_FLAG_UTIL_CLAMP_RESET flag to reset uclamp Yun Hsiang
2020-11-02 20:51 ` kernel test robot

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).