Skip to contentSkip to conversation

Add utilization clamping support

PATCH Superseded Patrick Bellasi 4 participants 32 messages Apr 9, 2018 – Apr 26, 2018

Subsystems (2)

v1 of 14 Superseded Latest is v14 →

lore
Share & tools
lore
#0 lore raw

cover Add utilization clamping support

This is a respin of:

https://lkml.org/lkml/2017/8/24/721

which finally removes the RFC tag since we properly addressed all the major concerns from previous discussions, mainly at LPCs and OSPM conferences, and we want to aim now at finalizing this series for mainline merge.

Comments and feedbacks are more than welcome!

The content of this series will be discussed also at the upcoming OSPM Summit:

http://retis.sssup.it/ospm-summit/

A live stream and record of the event will be available for the benefit of those interested which will not be able to join the summit.

.:: Main changes

The main change in this version is the introduction of a new userspace API, as requested by Tejun. This makes now the CGroups based a "secondary" interface for utilization clamping, which allows to use this feature also on systems where the CGroups's CPU controller is not available or not in use.

The primary interface for utilization clamping is now also a per-task API, which has been added by extending the existing sched_{set,get}attr syscalls. Here we propose a simple yet effective extension of these syscalls based on a couple of additional attributes.
A possible alternative implementation is also described, as a note in the corresponding commit message, which will not require to change the syscall but just to properly re-use existing attributes currently available only for DEADLINE tasks. Since this would require a more complex implementation, we decided to go for the simple option and open up for discussions on this (hopefully last) point while we finalize the patchset.

Due to the new API, the series has also been re-organized into the following described main sections.

Data Structures and Mechanisms
==============================

[PATCH 1/7] sched/core: uclamp: add CPU clamp groups accounting [PATCH 2/7] sched/core: uclamp: map TASK clamp values into CPU clamp groups

Add the necessary data structures and mechanisms to translate task's utilization clamping values into an effective and low-overhead fast path (i.e. enqueue/dequeue time) tracking of the CPU's utilization clamping values.

Here we also introduce a new CONFIG_UCLAMP_TASK KConfig option, which allows to completely remove utilization clamping code for systems not needing it. Being mainly a mechanism to use in conjunction with schedutil, utilization clamping depends also on CPU_FREQ_GOV_SCHEDUTIL being enabled.

We also add the possibility to define at compile time how many different clamp values can be used. This is done because it makes sense from a practical usage standpoint and has it also interesting size/overhead benefits.

Per task (primary) API
======================

[PATCH 3/7] sched/core: uclamp: extend sched_setattr to support utilization clamping

Provides a simple yet effective user-space API to define per-task minimum and maximum utilization clamp values. A simple implementation is proposed here, while a possible alternative is described in the notes.

Per task group (secondary) API
==============================

[PATCH 4/7] sched/core: uclamp: add utilization clamping to the CPU controller [PATCH 5/7] sched/core: uclamp: use TG clamps to restrict TASK clamps

Add the same task group based API presented in the previous posting, but this time on top of the per-task one. The second patch is dedicated to the aggregation between per-task and per-task_group clamp values.

Schedutil integration
=====================

[PATCH 6/7] sched/cpufreq: uclamp: add utilization clamping for FAIR tasks [PATCH 7/7] sched/cpufreq: uclamp: add utilization clamping for RT tasks

Extend sugov_aggregate_util() and sugov_set_iowait_boost() to clamp the utilization reported by cfs_rq and rt_rq in the selection of the OPP.

This patch set is based on today's tip/sched/core:

commit b720342 ("sched/core: Update preempt_notifier_key to modern API")

but it depends on a couple of schedutil related refactoring patches which I've posted separately on the list. For your convenience, a complete tree for testing and evaluation is available here:

git://linux-arm.org/linux-pb.git lkml/utilclamp_v1
http://www.linux-arm.org/git?p=linux-pb.git;a=shortlog;h=refs/heads/lkml/utilclamp_v1

.:: Newcomer's Short Abstract

The Linux scheduler is able to drive frequency selection, when the schedutil cpufreq's governor is in use, based on task utilization aggregated at CPU level. The CPU utilization is then used to select the frequency which better fits the task's generated workload. The current translation of utilization values into a frequency selection is pretty simple: we just go to max for RT tasks or to the minimum frequency which can accommodate the utilization of DL+FAIR tasks.

While this simple mechanism is good enough for DL tasks, for RT and FAIR tasks we can aim at some better frequency driving which can take into consideration hints coming from user-space.

Utilization clamping is a mechanism which allows to "clamp" (i.e. filter) the utilization generated by RT and FAIR tasks within a range defined from user-space. The clamped utilization value can then be used to enforce a minimum and/or maximum frequency depending on which tasks are currently active on a CPU.

The main use-cases for utilization clamping are:

- boosting: better interactive response for small tasks which are affecting the user experience. Consider for example the case of a small control thread for an external accelerator (e.g. GPU, DSP, other devices). In this case the scheduler does not have a complete view of what are the task bandwidth requirements and, if it's a small task, schedutil will keep selecting a lower frequency thus affecting the overall time required to complete the task activations.

- clamping: increase energy efficiency for background tasks not directly affecting the user experience. Since running at a lower frequency is in general more energy efficient, when the completion time is not a main goal then clamping the maximum frequency to use for certain (maybe big) tasks can have positive effects, both on energy consumption and thermal stress.
Moreover, this last support allows also to make RT tasks more energy friendly on mobile systems, where running them at the maximum frequency is not strictly required.

Cheers Patrick

Patrick Bellasi (7):
sched/core: uclamp: add CPU clamp groups accounting sched/core: uclamp: map TASK clamp values into CPU clamp groups sched/core: uclamp: extend sched_setattr to support utilization
clamping
sched/core: uclamp: add utilization clamping to the CPU controller sched/core: uclamp: use TG clamps to restrict TASK clamps sched/cpufreq: uclamp: add utilization clamping for FAIR tasks sched/cpufreq: uclamp: add utilization clamping for RT tasks

 include/linux/sched.h            |  34 ++
 include/uapi/linux/sched.h       |   4 +-
 include/uapi/linux/sched/types.h |  65 ++-
 init/Kconfig                     |  64 +++
 kernel/sched/core.c              | 824 +++++++++++++++++++++++++++++++++++++++
 kernel/sched/cpufreq_schedutil.c |  46 ++-
 kernel/sched/sched.h             | 180 +++++++++
 7 files changed, 1192 insertions(+), 25 deletions(-)
Signature · 1 line
2.15.1
#1 lore raw

PATCH 1/7 sched/core: uclamp: add CPU clamp groups accounting

Utilization clamping allows to clamp the utilization of a CPU within a [util_min, util_max] range. This range depends on the set of currently active tasks on a CPU, where each task references two "clamp groups" defining the util_min and the util_max clamp values to be considered for that task. The clamp value mapped by a clamp group applies to a CPU only when there is at least one task active referencing that clamp group.

When tasks are enqueued/dequeued on/from a CPU, the set of clamp groups active on that CPU can change. Since each clamp group enforces a different utilization clamp value, once the set of these groups changes it can be required to re-compute what is the new "aggregated" clamp value to apply on that CPU.

Clamp values are always MAX aggregated for both util_min and util_max. This is to ensure that no tasks can affect the performances of other co-scheduled tasks which are either more boosted (i.e. with higher util_min clamp) or less capped (i.e. with higher util_max clamp).

Here we introduce the required support to properly reference count clamp groups at each task enqueue/dequeue time.

Tasks have a task_struct::uclamp::group_id indexing the clamp group in which they should be accounted into at enqueue time. This index is cached into task_struct::uclamp_group_id once a task is enqueued in a CPU to ensure a consistent and efficient update of the reference count at dequeue time.

CPUs rq have a rq::uclamp::group[].tasks which is used to reference count how many tasks are currently active on that CPU for each clamp group. The clamp value of each clamp group is tracked by rq::uclamp::group[].value, thus making rq::uclamp::group[] an unordered array of clamp values. However, the MAX aggregation of the currently active clamp groups is implemented to minimizes the number of times we need to scan the complete (unordered) clamp group array to figure out the new max value. This operation indeed happens only when we dequeue last task of the clamp group corresponding to the current max clamp, and thus the CPU is either entering IDLE or going to schedule a less boosted or more clamped task.
Moreover, the expected number of different clamp values, which can be configured at build time, is usually so small to make not worth a more advanced ordering algorithm. In real use-cases we expect less then 10 different values.

Cc Ingo Molnar <[email protected]>
Cc Peter Zijlstra <[email protected]>
Cc Paul Turner <[email protected]>
Cc Joel Fernandes <[email protected]>
Cc Juri Lelli <[email protected]>
Cc Dietmar Eggemann <[email protected]>
Cc Morten Rasmussen <[email protected]>
---
 include/linux/sched.h |  34 +++++++++
 init/Kconfig          |  42 +++++++++++
 kernel/sched/core.c   | 198 ++++++++++++++++++++++++++++++++++++++++++++++++++
 kernel/sched/sched.h  |  79 ++++++++++++++++++++
 4 files changed, 353 insertions(+)
include/linux/sched.h +34 -0
index f228c6033832..d25460754d03 100644
@@ -238,6 +238,17 @@ struct vtime {
 	u64			gtime;
 };
 
+enum uclamp_id {
+	/* No utilization clamp group assigned */
+	UCLAMP_NONE = -1,
+
+	UCLAMP_MIN = 0, /* Minimum utilization */
+	UCLAMP_MAX,     /* Maximum utilization */
+
+	/* Utilization clamping constraints count */
+	UCLAMP_CNT
+};
+
 struct sched_info {
 #ifdef CONFIG_SCHED_INFO
 	/* Cumulative counters: */
@@ -526,6 +537,22 @@ struct sched_dl_entity {
 	struct hrtimer inactive_timer;
 };
 
+/**
+ * Utilization's clamp group
+ *
+ * A utilization clamp group maps a "clamp value" (value), i.e.
+ * util_{min,max}, to a "clamp group index" (group_id).
+ *
+ * Thus, the same "group_id" is used by all the TG's which enforce the same
+ * clamp "value" for a given clamp index.
+ */
+struct uclamp_se {
+	/* Utilization constraint for tasks in this group */
+	unsigned int value;
+	/* Utilization clamp group for this constraint */
+	unsigned int group_id;
+};
+
 union rcu_special {
 	struct {
 		u8			blocked;
@@ -608,6 +635,13 @@ struct task_struct {
 #endif
 	struct sched_dl_entity		dl;
 
+#ifdef CONFIG_UCLAMP_TASK
+	/* Clamp group the task is currently accounted into */
+	int				uclamp_group_id[UCLAMP_CNT];
+	/* Utlization clamp values for this task */
+	struct uclamp_se		uclamp[UCLAMP_CNT];
+#endif
+
 #ifdef CONFIG_PREEMPT_NOTIFIERS
 	/* List of struct preempt_notifier: */
 	struct hlist_head		preempt_notifiers;
init/Kconfig +42 -0

48 lines not on this page · raw message

kernel/sched/core.c +198 -0

228 lines not on this page · raw message

kernel/sched/sched.h +79 -0

91 lines not on this page · raw message

Signature · 1 line
2.15.1
#21 lore raw

PATCH 2/7 sched/core: uclamp: map TASK clamp values into CPU clamp groups

In reply to Patrick Bellasi

Utilization clamping requires each CPU to know which clamp values are assigned to tasks, which are currently active on that CPU, and multiple tasks can be assigned the same clamp value, for a given clamp index (clamp_id := util_{min,max}). Thus, it is required to map clamp values to a data structure which is suitable for fast and efficient aggregation of the clamp values required for by active tasks.

To this purpose we use a per-CPU array of reference counters, where each slot is used to account how many tasks requiring a certain clamp value are currently active on each CPU.
Each clamp value corresponds to a "clamp index" which identifies the position within the reference counters array.

                                 :
                                 :
             SLOW PATH           :             FAST PATH
    task_struct::uclamp::value   :     sched/core::enqueue/dequeue
                                 :         cpufreq_schedutil
                                 :
  +----------------+    +--------------------+     +-------------------+
  |      TASK      |    |     CLAMP GROUP    |     |    CPU CLAMPS     |
  +----------------+    +--------------------+     +-------------------+
  |                |    |   clamp_{min,max}  |     |  clamp_{min,max}  |
  | util_{min,max} |    |      tg_count      |     |    tasks count    |
  +----------------+    +--------------------+     +-------------------+
                                 :
           +------------------>  :  +------------------->
     map(clamp_value, group_id)  :  ref_count(group_id)
                                 :
                                 :

This patch introduces the support to map tasks to "clamp groups". Specifically it introduces the required functions to translate a clamp value into a clamp group index (group_id).

Only a limited number of (different) clamp values are supported since:
1. there are usually only few classes of workloads for which it makes sense to boost/cap to different frequencies
e.g. background vs foreground, interactive vs low-priority
2. it allows a simpler and more memory/time efficient tracking of the per-CPU clamp values in the fast path

The number of possible different clamp values is currently defined at compile time. Thus, setting a new clamp value for a task can result into a -ENOSPC error in case this will exceed the number of maximum different clamp values supported.

Cc Ingo Molnar <[email protected]>
Cc Peter Zijlstra <[email protected]>
Cc Paul Turner <[email protected]>
Cc Joel Fernandes <[email protected]>
Cc Juri Lelli <[email protected]>
Cc Dietmar Eggemann <[email protected]>
Cc Morten Rasmussen <[email protected]>
---
 kernel/sched/core.c  | 294 +++++++++++++++++++++++++++++++++++++++++++++++++++
 kernel/sched/sched.h |  21 ++++
 2 files changed, 315 insertions(+)
kernel/sched/core.c +294 -0
index 009e65cbd4f4..a602b7b9d5f9 100644
@@ -747,6 +747,168 @@ static void set_load_weight(struct task_struct *p, bool update_load)
  */
 static DEFINE_MUTEX(uclamp_mutex);
 
+/**
+ * uclamp_map: reference counts a utilization "clamp value"
+ * @value:    the utilization "clamp value" required
+ * @se_count: the number of scheduling entities requiring the "clamp value"
+ * @se_lock:  serialize reference count updates by protecting se_count
+ */
+struct uclamp_map {
+	int value;
+	int se_count;
+	raw_spinlock_t se_lock;
+};
+
+/**
+ * uclamp_maps: maps each SEs "clamp value" into a CPUs "clamp group"
+ *
+ * Since only a limited number of different "clamp values" are supported, we
+ * need to map each possible "clamp value" into a "clamp group" to be used by
+ * the per-CPU accounting in the fast-path (i.e.  tasks enqueuing/dequeuing).
+ * We also support different kind of utilization clamping, min and max
+ * utilization, each representing a "clamp index" (clamp_id).
+ *
+ * A matrix is thus required to map clamp values to clamp groups (group_id)
+ * for each clamp index (clamp_id), where:
+ * - rows are indexed by clamp_id and collect the clamp groups for a given
+ *   clamp index, i.e. min or max utilization;
+ * - columns are indexed by group_id and collect the clamp values which maps
+ *   to that clamp group, i.e. actual uclamp_map instances;
+ *
+ * Thus, the column index of a given (clamp_id, value) pair represents the
+ * clamp group (group_id) used by the fast-path's per-CPU accounting.
+ *
+ * NOTE: first clamp group (group_id=0) is reserved for tracking of non
+ * clamped tasks.  Thus we allocate one more slot than the value of
+ * CONFIG_UCLAMP_GROUPS_COUNT.
+ *
+ * Here is the map layout and, right below, how entries are accessed by the
+ * following code.
+ *
+ *                          uclamp_maps is a matrix of
+ *          +------- UCLAMP_CNT by CONFIG_UCLAMP_GROUPS_COUNT+1 entries
+ *          |                                |
+ *          |                /---------------+---------------\
+ *          |               +------------+       +------------+
+ *          |  / UCLAMP_MIN | value      |       | value      |
+ *          |  |            | se_count   |...... | se_count   |
+ *          |  |            +------------+       +------------+
+ *          +--+            +------------+       +------------+
+ *             |            | value      |       | value      |
+ *             \ UCLAMP_MAX | se_count   |...... | se_count   |
+ *                          +-----^------+       +----^-------+
+ *                                |                   |
+ *                      uc_map =  +                   |
+ *                     &uclamp_maps[clamp_id][0]      +
+ *                                                clamp_value =
+ *                                       uc_map[group_id].value
+ */
+static struct uclamp_map uclamp_maps[UCLAMP_CNT][CONFIG_UCLAMP_GROUPS_COUNT + 1];
+
+/**
+ * uclamp_group_available: checks if a clamp group is available
+ * @clamp_id: the utilization clamp index (i.e. min or max clamp)
+ * @group_id: the group index in the given clamp_id
+ *
+ * A clamp group is not free if there is at least one TG's using a clamp value
+ * mapped on the specified clamp_id. These TG's are reference counted by the
+ * se_count of a uclamp_map entry.
+ *
+ * Return: true if there are no TG's mapped on the specified clamp
+ *         index and group
+ */
+static inline bool uclamp_group_available(int clamp_id, int group_id)
+{
+	struct uclamp_map *uc_map = &uclamp_maps[clamp_id][0];
+
+	return (uc_map[group_id].value == UCLAMP_NONE);
+}
+
+/**
+ * uclamp_group_init: maps a clamp value on a specified clamp group
+ * @clamp_id: the utilization clamp index (i.e. min or max clamp)
+ * @group_id: the group index to map a given clamp_value
+ * @clamp_value: the utilization clamp value to map
+ *
+ * Each different clamp value, for a given clamp index (i.e. min/max
+ * utilization clamp), is mapped by a clamp group which index is use by the
+ * fast-path code to keep track of active tasks requiring a certain clamp
+ * value.
+ *
+ * This function initializes a clamp group to track tasks from the fast-path.
+ */
+static inline void uclamp_group_init(int clamp_id, int group_id,
+				     unsigned int clamp_value)
+{
+	struct uclamp_map *uc_map = &uclamp_maps[clamp_id][0];
+	struct uclamp_cpu *uc_cpu;
+	int cpu;
+
+	/* Set clamp group map */
+	uc_map[group_id].value = clamp_value;
+	uc_map[group_id].se_count = 0;
+
+	/* Set clamp groups on all CPUs */
+	for_each_possible_cpu(cpu) {
+		uc_cpu = &cpu_rq(cpu)->uclamp[clamp_id];
+		uc_cpu->group[group_id].value = clamp_value;
+		uc_cpu->group[group_id].tasks = 0;
+	}
+}
+
+/**
+ * uclamp_group_reset: resets a specified clamp group
+ * @clamp_id: the utilization clamp index (i.e. min or max clamping)
+ * @group_id: the group index to release
+ *
+ * A clamp group can be reset every time there are no more TGs using the
+ * clamp value it maps for a given clamp index.
+ */
+static inline void uclamp_group_reset(int clamp_id, int group_id)
+{
+	uclamp_group_init(clamp_id, group_id, UCLAMP_NONE);
+}
+
+/**
+ * uclamp_group_find: finds the group index of a utilization clamp group
+ * @clamp_id: the utilization clamp index (i.e. min or max clamping)
+ * @clamp_value: the utilization clamping value lookup for
+ *
+ * Verify if a group has been assigned to a certain clamp value and return
+ * its index to be used for accounting.
+ *
+ * Since only a limited number of utilization clamp groups are allowed, if no
+ * groups have been assigned for the specified value, a new group is assigned
+ * if possible. Otherwise an error is returned, meaning that a different clamp
+ * value is not (currently) supported.
+ */
+static int
+uclamp_group_find(int clamp_id, unsigned int clamp_value)
+{
+	struct uclamp_map *uc_map = &uclamp_maps[clamp_id][0];
+	int free_group_id = UCLAMP_NONE;
+	unsigned int group_id = 0;
+
+	for ( ; group_id <= CONFIG_UCLAMP_GROUPS_COUNT; ++group_id) {
+		/* Keep track of first free clamp group */
+		if (uclamp_group_available(clamp_id, group_id)) {
+			if (free_group_id == UCLAMP_NONE)
+				free_group_id = group_id;
+			continue;
+		}
+		/* Return index of first group with same clamp value */
+		if (uc_map[group_id].value == clamp_value)
+			return group_id;
+	}
+	/* Default to first free clamp group */
+	if (group_id > CONFIG_UCLAMP_GROUPS_COUNT)
+		group_id = free_group_id;
+	/* All clamp group already tracking different clamp values */
+	if (group_id == UCLAMP_NONE)
+		return -ENOSPC;
+	return group_id;
+}
+
 /**
  * uclamp_cpu_update: updates the utilization clamp of a CPU
  * @cpu: the CPU which utilization clamp has to be updated
@@ -896,18 +1058,150 @@ static inline void uclamp_task_update(struct rq *rq, struct task_struct *p)
 	}
 }
 
+/**
+ * uclamp_task_update_active: update the clamp group of a RUNNABLE task
+ * @p: the task which clamp groups must be updated
+ * @clamp_id: the clamp index to consider
+ * @group_id: the clamp group to update
+ *
+ * Each time the clamp value of a task group is changed, the old and new clamp
+ * groups have to be updated for each CPU containing a RUNNABLE task belonging
+ * to this tasks group. Sleeping tasks are not updated since they will be
+ * enqueued with the proper clamp group index at their next activation.
+ */
+static inline void
+uclamp_task_update_active(struct task_struct *p, int clamp_id, int group_id)
+{
+	struct rq_flags rf;
+	struct rq *rq;
+
+	/*
+	 * Lock the task and the CPU where the task is (or was) queued.
+	 *
+	 * We might lock the (previous) RQ of a !RUNNABLE task, but that's the
+	 * price to pay to safely serialize util_{min,max} updates with
+	 * enqueues, dequeues and migration operations.
+	 * This is the same locking schema used by __set_cpus_allowed_ptr().
+	 */
+	rq = task_rq_lock(p, &rf);
+
+	/*
+	 * The setting of the clamp group is serialized by task_rq_lock().
+	 * Thus, if the task's task_struct is not referencing a valid group
+	 * index, then that task is not yet RUNNABLE or it's going to be
+	 * enqueued with the proper clamp group value.
+	 */
+	if (!uclamp_task_active(p))
+		goto done;
+
+	/* Release p's currently referenced clamp group */
+	uclamp_cpu_put(p, task_cpu(p), clamp_id);
+
+	/* Get p's new clamp group */
+	uclamp_cpu_get(p, task_cpu(p), clamp_id);
+
+done:
+	task_rq_unlock(rq, p, &rf);
+}
+
+/**
+ * uclamp_group_put: decrease the reference count for a clamp group
+ * @clamp_id: the clamp index which was affected by a task group
+ * @uc_se: the utilization clamp data for that task group
+ *
+ * When the clamp value for a task group is changed we decrease the reference
+ * count for the clamp group mapping its current clamp value. A clamp group is
+ * released when there are no more task groups referencing its clamp value.
+ */
+static inline void uclamp_group_put(int clamp_id, int group_id)
+{
+	struct uclamp_map *uc_map = &uclamp_maps[clamp_id][0];
+	unsigned long flags;
+
+	/* Ignore SE's not yet attached */
+	if (group_id == UCLAMP_NONE)
+		return;
+
+	/* Remove SE from this clamp group */
+	raw_spin_lock_irqsave(&uc_map[group_id].se_lock, flags);
+	uc_map[group_id].se_count -= 1;
+	if (uc_map[group_id].se_count == 0)
+		uclamp_group_reset(clamp_id, group_id);
+	raw_spin_unlock_irqrestore(&uc_map[group_id].se_lock, flags);
+}
+
+/**
+ * uclamp_group_get: increase the reference count for a clamp group
+ * @clamp_id: the clamp index affected by the task group
+ * @uc_se: the utilization clamp data for the task group
+ * @clamp_value: the new clamp value for the task group
+ *
+ * Each time a task group changes the utilization clamp value, for a specified
+ * clamp index, we need to find an available clamp group which can be used
+ * to track its new clamp value. The corresponding clamp group index will be
+ * used by tasks in this task group to reference count the clamp value on CPUs
+ * where they are enqueued.
+ *
+ * Return: -ENOSPC if there are not available clamp groups, 0 on success.
+ */
+static inline int uclamp_group_get(struct task_struct *p,
+				   int clamp_id, struct uclamp_se *uc_se,
+				   unsigned int clamp_value)
+{
+	struct uclamp_map *uc_map = &uclamp_maps[clamp_id][0];
+	int prev_group_id = uc_se->group_id;
+	int next_group_id = UCLAMP_NONE;
+	unsigned long flags;
+
+	/* Lookup for a usable utilization clamp group */
+	next_group_id = uclamp_group_find(clamp_id, clamp_value);
+	if (next_group_id < 0) {
+		pr_err("Cannot allocate more than %d utilization clamp groups\n",
+		       CONFIG_UCLAMP_GROUPS_COUNT);
+		return -ENOSPC;
+	}
+
+	/* Allocate new clamp group for this clamp value */
+	if (uclamp_group_available(clamp_id, next_group_id))
+		uclamp_group_init(clamp_id, next_group_id, clamp_value);
+
+	/* Update SE's clamp values and attach it to new clamp group */
+	raw_spin_lock_irqsave(&uc_map[next_group_id].se_lock, flags);
+	uc_se->value = clamp_value;
+	uc_se->group_id = next_group_id;
+	uc_map[next_group_id].se_count += 1;
+	raw_spin_unlock_irqrestore(&uc_map[next_group_id].se_lock, flags);
+
+	/* Update current task if task specific clamp has been changed */
+	uclamp_task_update_active(p, clamp_id, next_group_id);
+
+	/* Release the previous clamp group */
+	uclamp_group_put(clamp_id, prev_group_id);
+
+	return 0;
+}
+
 /**
  * init_uclamp: initialize data structures required for utilization clamping
  */
 static inline void init_uclamp(void)
 {
 	struct uclamp_cpu *uc_cpu;
+	struct uclamp_map *uc_map;
+	int group_id;
 	int clamp_id;
 	int cpu;
 
 	mutex_init(&uclamp_mutex);
 
 	for (clamp_id = 0; clamp_id < UCLAMP_CNT; ++clamp_id) {
+		uc_map = &uclamp_maps[clamp_id][0];
+		/* Init SE's clamp map */
+		group_id = 0;
+		for ( ; group_id <= CONFIG_UCLAMP_GROUPS_COUNT; ++group_id) {
+			uc_map[group_id].value = UCLAMP_NONE;
+			raw_spin_lock_init(&uc_map[group_id].se_lock);
+		}
 		/* Init CPU's clamp groups */
 		for_each_possible_cpu(cpu) {
 			uc_cpu = &cpu_rq(cpu)->uclamp[clamp_id];
kernel/sched/sched.h +21 -0
index be93d833ad6b..25c2011ecc41 100644
@@ -481,6 +481,27 @@ static inline bool uclamp_task_affects(struct task_struct *p, int clamp_id)
 	return (task_group_id != UCLAMP_NONE);
 }
 
+/**
+ * uclamp_task_active: check if a task is currently clamping a CPU
+ * @p: the task to check
+ *
+ * A task affects the utilization clamp of a CPU if it references a valid
+ * clamp group index for at least one clamp index.
+ *
+ * Return: true if p is currently clamping the utilization of its CPU.
+ */
+static inline bool uclamp_task_active(struct task_struct *p)
+{
+	int clamp_id;
+
+	for (clamp_id = 0; clamp_id < UCLAMP_CNT; ++clamp_id) {
+		if (uclamp_task_affects(p, clamp_id))
+			return true;
+	}
+
+	return false;
+}
+
 /**
  * uclamp_group_active: check if a clamp group is active on a CPU
  * @uc_cpu: the array of clamp groups for a CPU
Signature · 1 line
2.15.1
#22 lore raw

PATCH 3/7 sched/core: uclamp: extend sched_setattr to support utilization clamping

In reply to Patrick Bellasi

The SCHED_DEADLINE scheduling class provides an advanced and formal model to define task requirements which can be translated into proper decisions for both task placements and frequencies selections. Other classes have a simpler model which is essentially based on the relatively simple concept of POSIX priorities.

Such a simple priority based model however does not allow to exploit some of the more advanced features of the Linux scheduler like, for example, driving frequencies selection via the schedutil cpufreq governor. However, also for non SCHED_DEADLINE tasks, it's still interesting to define tasks properties which can be used to better support certain scheduler decisions.

Utilization clamping aims at exposing to user-space a new set of per-task attribute which can be used to provide the scheduler with some hints about the expected/required utilization should consider for a task. This will allow to implement a more advanced per-task frequency control mechanism which is not based just on a "passive" measured task utilization but on a more "proactive" approach. For example, it could be possible to boost interactive tasks, thus getting better performance, or cap background tasks, thus being more energy efficient. Ultimately, such a mechanism, can be considered similar to the cpufreq's powersave, performance and userspace governor but with a much more fine grained and per-task control.

Let's introduce a new API to set utilization clamping values for a specified task by extending sched_setattr, a syscall which already allows to define task specific properties for different scheduling classes.
Specifically, a new pair of attributes allows to specify a minimum and maximum utilization which the scheduler should consider for a task.

Cc Ingo Molnar <[email protected]>
Cc Peter Zijlstra <[email protected]>
Cc Tejun Heo <[email protected]>
Cc Rafael J. Wysocki <[email protected]>
Cc Vincent Guittot <[email protected]>
Cc Viresh Kumar <[email protected]>
Cc Paul Turner <[email protected]>
Cc Joel Fernandes <[email protected]>
Cc Steve Muckle <[email protected]>
Cc Juri Lelli <[email protected]>
Cc Dietmar Eggemann <[email protected]>
Cc Morten Rasmussen <[email protected]>

---
The solution proposed here exposes the concept of "utilization" to userspace. This should be a quite generic concept, maybe if we abstract it a bit to be considered just as a percentage of the CPU capacity, and thus in the range [0..100] instead of [0..SCHED_CAPACITY_SCALE] as it is now.

If such a defined utilization should still be considered too much of an implementation detail, a possible alternative proposal can be that do "recycle" the usage of:
sched_runtime
sched_period
to be translated internally into a proper utilization.

Such a model, although being slightly more complicated from a coding standpoint, it would allow to have a more abstract description of the expected task utilization and, combined with in-kernel knowledge of the math governing PELT, can probably be translated into a better min/max utilization clamp value.

For this first proposal, I've opted to present the most simple solution based on a and simple "abstract" utilization metric.

---
 include/uapi/linux/sched.h       |  4 ++-
 include/uapi/linux/sched/types.h | 65 ++++++++++++++++++++++++++++++++++------
 kernel/sched/core.c              | 52 ++++++++++++++++++++++++++++++++
 3 files changed, 111 insertions(+), 10 deletions(-)
include/uapi/linux/sched.h +3 -1
index 22627f80063e..c27d6e81517b 100644
@@ -50,9 +50,11 @@
 #define SCHED_FLAG_RESET_ON_FORK	0x01
 #define SCHED_FLAG_RECLAIM		0x02
 #define SCHED_FLAG_DL_OVERRUN		0x04
+#define SCHED_FLAG_UTIL_CLAMP		0x08
 
 #define SCHED_FLAG_ALL	(SCHED_FLAG_RESET_ON_FORK	| \
 			 SCHED_FLAG_RECLAIM		| \
-			 SCHED_FLAG_DL_OVERRUN)
+			 SCHED_FLAG_DL_OVERRUN		| \
+			 SCHED_FLAG_UTIL_CLAMP)
 
 #endif /* _UAPI_LINUX_SCHED_H */
include/uapi/linux/sched/types.h +56 -9

90 lines not on this page · raw message

kernel/sched/core.c +52 -0

76 lines not on this page · raw message

Signature · 1 line
2.15.1
#23 lore raw

PATCH 4/7 sched/core: uclamp: add utilization clamping to the CPU controller

In reply to Patrick Bellasi

The cgroup's CPU controller allows to assign a specified (maximum) bandwidth to the tasks of a group. However this bandwidth is defined and enforced only on a temporal base, without considering the actual frequency a CPU is running on. Thus, the amount of computation completed by a task within an allocated bandwidth can be very different depending on the actual frequency the CPU is running that task.

With the availability of schedutil, the scheduler is now able to drive frequency selections based on the actual tasks utilization. Moreover, the utilization clamping support provides a mechanism to constraint the frequency selection operated by schedutil depending on constraints assigned to the tasks currently active on a CPU.

Give the above mechanisms, it is now possible to extend the cpu controller to specify what is the minimum (or maximum) utilization which a task is allowed to generate. By adding new constraints on minimum and maximum utilization allowed for tasks in a cpu control group it will also be possible to better control the actual amount of CPU bandwidth consumed by these tasks.

The ultimate goal of this new pair of constraints is to enable:

- boosting: by selecting a higher execution frequency for small tasks which are affecting the user interactive experience

- capping: by selecting lower execution frequency, which usually improves energy efficiency, for big tasks which are mainly related to background activities, and thus without a direct impact on the user experience.

This patch extends the CPU controller by adding a couple of new attributes, util_min and util_max, which can be used to enforce frequency boosting and capping. Specifically:

- util_min: defines the minimum CPU utilization which should be considered, e.g. when schedutil selects the frequency for a CPU while a task in this group is RUNNABLE.
i.e. the task will run at least at a minimum frequency which
corresponds to the min_util utilization

- util_max: defines the maximum CPU utilization which should be considered, e.g. when schedutil selects the frequency for a CPU while a task in this group is RUNNABLE.
i.e. the task will run up to a maximum frequency which
corresponds to the max_util utilization

These attributes:
a) are tunable at all hierarchy levels, i.e. at root group level too, thus allowing to define the minimum and maximum frequency constraints for all otherwise non-classified tasks (e.g. autogroups) and to be a sort-of replacement for cpufreq's powersave, ondemand and performance governors.
b) allow to create subgroups of tasks which are not violating the utilization constraints defined by the parent group.

Tasks on a subgroup can only be more boosted and/or capped, which is matching with the "limits" schema proposed by the "Resource Distribution Model (RDM)" suggested by the CGroups v2 documentation:
Documentation/cgroup-v2.txt

This patch provides the basic support to expose the two new attributes and to validate their run-time update based on the "limits" of the aforementioned RDM schema.

We first ensure that, whenever a task group is assigned a specific clamp_value, this is properly translated into a unique clamp group to be used in the fast-path (i.e. at enqueue/dequeue time). This is done by slightly refactoring uclamp_group_get to accept a *cgroup_subsys_state alongside *task_struct.

When uclamp_group_get is called with a valid *cgroup_subsys_state, a clamp group is assigned to the task, which is possibly different than the task specific clamp group. We then ensure to update the current clamp group accounting for all the tasks which are currently runnable on the cgroup via a new uclamp_group_get_tg() call.

Cc Ingo Molnar <[email protected]>
Cc Peter Zijlstra <[email protected]>
Cc Tejun Heo <[email protected]>
Cc Rafael J. Wysocki <[email protected]>
Cc Viresh Kumar <[email protected]>
Cc Joel Fernandes <[email protected]>
Cc Juri Lelli <[email protected]>

---
The actual aggregation of per-task and per-task_group utilization constraints is provided in a separate patch to make it more clear and documented how this aggregation is performed.

---
 init/Kconfig         |  22 +++++
 kernel/sched/core.c  | 271 ++++++++++++++++++++++++++++++++++++++++++++++++++-
 kernel/sched/sched.h |  21 ++++
 3 files changed, 311 insertions(+), 3 deletions(-)
init/Kconfig +22 -0
index 977aa4d1e42a..d999879f8625 100644
@@ -795,6 +795,28 @@ config RT_GROUP_SCHED
 
 endif #CGROUP_SCHED
 
+config UCLAMP_TASK_GROUP
+	bool "Utilization clamping per group of tasks"
+	depends on CGROUP_SCHED
+	depends on UCLAMP_TASK
+	default n
+	help
+	  This feature enables the scheduler to track the clamped utilization
+	  of each CPU based on RUNNABLE tasks currently scheduled on that CPU.
+
+	  When this option is enabled, the user can specify a min and max
+	  CPU bandwidth which is allowed for each single task in a group.
+	  The max bandwidth allows to clamp the maximum frequency a task
+	  can use, while the min bandwidth allows to define a minimum
+	  frequency a task will alwasy use.
+
+	  When task group based utilization clamping is enabled,  an eventually
+          specified task-specific clamp value is constrained by the cgroup
+	  specified clamp value. Both minimum and maximum task clamping cannot
+          be bigger then the corresponing clamping defined at task group level.
+
+	  If in doubt, say N.
+
 config CGROUP_PIDS
 	bool "PIDs controller"
 	help
kernel/sched/core.c +268 -3

342 lines not on this page · raw message

kernel/sched/sched.h +21 -0
index 25c2011ecc41..a91b9cd162a3 100644
@@ -388,6 +388,11 @@ struct task_group {
 #endif
 
 	struct cfs_bandwidth	cfs_bandwidth;
+
+#ifdef CONFIG_UCLAMP_TASK_GROUP
+	struct			uclamp_se uclamp[UCLAMP_CNT];
+#endif
+
 };
 
 #ifdef CONFIG_FAIR_GROUP_SCHED
@@ -460,6 +465,22 @@ struct uclamp_cpu {
 	struct uclamp_group group[CONFIG_UCLAMP_GROUPS_COUNT + 1];
 };
 
+/**
+ * uclamp_none: default value for a clamp
+ *
+ * This returns the default value for each clamp
+ * - 0 for a min utilization clamp
+ * - SCHED_CAPACITY_SCALE for a max utilization clamp
+ *
+ * Return: the default value for a given utilization clamp
+ */
+static inline unsigned int uclamp_none(int clamp_id)
+{
+	if (clamp_id == UCLAMP_MIN)
+		return 0;
+	return SCHED_CAPACITY_SCALE;
+}
+
 /**
  * uclamp_task_affects: check if a task affects a utilization clamp
  * @p: the task to consider
Signature · 1 line
2.15.1
#29 lore raw

PATCH 5/7 sched/core: uclamp: use TG clamps to restrict TASK clamps

In reply to Patrick Bellasi

When a task's util_clamp value is configured via sched_setattr, this value has to be properly accounted in the corresponding clamp group every time the task is enqueue and dequeued. When cgroups are also in use, per-task clamp values have to be aggregated to those of the CPU's controller's CGroup in which the task is currently living.

Let's update uclamp_cpu_get() to provide an aggregation between the task and the TG clamp values. Every time a task is enqueued, it will be accounted in the clamp_group which defines the smaller clamp value between the task and the TG's ones. This mimics what already happen for a task's CPU affinity mask when the task is also living in a cpuset. The overall idea is that: CGroups attributes are always used to restrict the per-task attributes.

For consistency purposes, as well as to properly inform userspace, the sched_getattr call is updated to always return the properly aggregated constrains as described above. This will also make sched_getattr a convenient userpace API to know the utilization constraints enforced on a task by the CGroups's CPU controller.

Cc Ingo Molnar <[email protected]>
Cc Peter Zijlstra <[email protected]>
Cc Tejun Heo <[email protected]>
Cc Paul Turner <[email protected]>
Cc Joel Fernandes <[email protected]>
Cc Steve Muckle <[email protected]>
Cc Juri Lelli <[email protected]>
Cc Dietmar Eggemann <[email protected]>
Cc Morten Rasmussen <[email protected]>
---
 kernel/sched/core.c | 15 +++++++++++++++
 1 file changed, 15 insertions(+)
kernel/sched/core.c +15 -0
index b8299a4f03e7..592de8d32427 100644
@@ -966,9 +966,18 @@ static inline void uclamp_cpu_get(struct task_struct *p, int cpu, int clamp_id)
 	clamp_value = p->uclamp[clamp_id].value;
 	group_id = p->uclamp[clamp_id].group_id;
 
+#ifdef CONFIG_UCLAMP_TASK_GROUP
+	/* Use TG's clamp value to limit task specific values */
+	if (group_id == UCLAMP_NONE ||
+	    clamp_value >= task_group(p)->uclamp[clamp_id].value) {
+		clamp_value = task_group(p)->uclamp[clamp_id].value;
+		group_id = task_group(p)->uclamp[clamp_id].group_id;
+	}
+#else
 	/* No task specific clamp values: nothing to do */
 	if (group_id == UCLAMP_NONE)
 		return;
+#endif
 
 	/* Increment the current group_id */
 	uc_cpu->group[group_id].tasks += 1;
@@ -5401,6 +5410,12 @@ SYSCALL_DEFINE4(sched_getattr, pid_t, pid, struct sched_attr __user *, uattr,
 #ifdef CONFIG_UCLAMP_TASK
 	attr.sched_util_min = p->uclamp[UCLAMP_MIN].value;
 	attr.sched_util_max = p->uclamp[UCLAMP_MAX].value;
+#ifdef CONFIG_UCLAMP_TASK_GROUP
+	if (task_group(p)->uclamp[UCLAMP_MIN].value < attr.sched_util_min)
+		attr.sched_util_min = task_group(p)->uclamp[UCLAMP_MIN].value;
+	if (task_group(p)->uclamp[UCLAMP_MAX].value < attr.sched_util_max)
+		attr.sched_util_max = task_group(p)->uclamp[UCLAMP_MAX].value;
+#endif
 #endif
 
 	rcu_read_unlock();
Signature · 1 line
2.15.1
#30 lore raw

PATCH 6/7 sched/cpufreq: uclamp: add utilization clamping for FAIR tasks

In reply to Patrick Bellasi

Each time a frequency update is required via schedutil, a frequency is selected to (possibly) satisfy the utilization reported by the CFS class. However, when utilization clamping is in use, the frequency selection should consider the requirements suggested by userspace, for example, to:

- boost tasks which are directly affecting the user experience by running them at least at a minimum "required" frequency

- cap low priority tasks not directly affecting the user experience by running them only up to a maximum "allowed" frequency

These constraints are meant to support a per-task based tuning of the operating frequency selection thus allowing to have a fine grained definition of performance boosting vs energy saving strategies in kernel space.

Let's add the required support to clamp the utilization generated by FAIR tasks within the boundaries defined by their aggregated utilization clamp constraints.
On each CPU the aggregated clamp values are obtained by considering the maximum of the {min,max}_util values for each task. This max aggregation responds to the goal of not penalizing, for example, high boosted (i.e. more important for the user-experience) CFS tasks which happens to be co-scheduled with high capped (i.e. less important for the user-experience) CFS tasks.

For FAIR tasks both the utilization as well as the IOWait boot values are clamped according to the CPU aggregated utilization clamp constraints.

The default values for boosting and capping are defined to be:
- util_min: 0
- util_max: SCHED_CAPACITY_SCALE
which means that by default no boosting/capping is enforced on FAIR tasks, and thus the frequency will be selected considering the actual utilization value of each CPU.

Cc Ingo Molnar <[email protected]>
Cc Peter Zijlstra <[email protected]>
Cc Rafael J. Wysocki <[email protected]>
Cc Viresh Kumar <[email protected]>
Cc Joel Fernandes <[email protected]>
Cc Juri Lelli <[email protected]>
Cc Dietmar Eggemann <[email protected]>
Cc Morten Rasmussen <[email protected]>
---
 kernel/sched/cpufreq_schedutil.c | 29 ++++++++++++++------
 kernel/sched/sched.h             | 59 ++++++++++++++++++++++++++++++++++++++++
 2 files changed, 80 insertions(+), 8 deletions(-)
kernel/sched/cpufreq_schedutil.c +21 -8
index e021db562308..fe53984d06a0 100644
@@ -190,7 +190,7 @@ static unsigned long sugov_aggregate_util(struct sugov_cpu *sg_cpu)
 	} else {
 		util = sg_cpu->util_dl;
 		if (rq->cfs.h_nr_running)
-			util += sg_cpu->util_cfs;
+			util += uclamp_util(sg_cpu->cpu, sg_cpu->util_cfs);
 	}
 
 	/*
@@ -213,6 +213,7 @@ static unsigned long sugov_aggregate_util(struct sugov_cpu *sg_cpu)
  */
 static void sugov_set_iowait_boost(struct sugov_cpu *sg_cpu, unsigned int flags)
 {
+	unsigned int max_boost;
 
 	/* Boost only tasks waking up after IO */
 	if (!(flags & SCHED_CPUFREQ_IOWAIT))
@@ -223,16 +224,28 @@ static void sugov_set_iowait_boost(struct sugov_cpu *sg_cpu, unsigned int flags)
 		return;
 	sg_cpu->iowait_boost_pending = true;
 
-	/* Double the IO boost at each frequency increase */
-	if (sg_cpu->iowait_boost) {
-		sg_cpu->iowait_boost <<= 1;
-		if (sg_cpu->iowait_boost > sg_cpu->iowait_boost_max)
-			sg_cpu->iowait_boost = sg_cpu->iowait_boost_max;
+	/* At first wakeup after IO, start with minimum boost */
+	if (!sg_cpu->iowait_boost) {
+		sg_cpu->iowait_boost = sg_cpu->sg_policy->policy->min;
 		return;
 	}
 
-	/* At first wakeup after IO, start with minimum boost */
-	sg_cpu->iowait_boost = sg_cpu->sg_policy->policy->min;
+	/*
+	 * Boost only up to the current max CPU clamped utilization.
+	 *
+	 * Since DL tasks have a much more advanced bandwidth control, it's
+	 * safe to assume that IO boost does not apply to those tasks.
+	 * Instead, since for RT tasks we are going to max, we don't want to
+	 * clamp the IO boost max value.
+	 */
+	max_boost = sg_cpu->iowait_boost_max;
+	if (!cpu_rq(sg_cpu->cpu)->rt.rt_nr_running)
+		max_boost = uclamp_util(sg_cpu->cpu, max_boost);
+
+	/* Double the IO boost at each frequency increase */
+	sg_cpu->iowait_boost <<= 1;
+	if (sg_cpu->iowait_boost > max_boost)
+		sg_cpu->iowait_boost = max_boost;
 }
 
 /**
kernel/sched/sched.h +59 -0

65 lines not on this page · raw message

Signature · 1 line
2.15.1
#31 lore raw

PATCH 7/7 sched/cpufreq: uclamp: add utilization clamping for RT tasks

In reply to Patrick Bellasi

Currently schedutil enforces a maximum frequency when RT tasks are RUNNABLE. Such a mandatory policy can be made more tunable from userspace thus allowing for example to define a max frequency which is still reasonable for the execution of a specific RT workload. This will contribute to make the RT class more friendly for power/energy sensitive use-cases.

This patch extends the usage of util_{min,max} to the RT scheduling class. Whenever a task in this class is RUNNABLE, the util required is defined by the constraints of the CPU control group the task belongs to.

The IO wait boost value is thus subject to clamping for RT tasks too. This is to ensure that RT tasks as well as CFS ones are always subject to the set of current utilization clampinig constraints. It's worth to notice that, by default, clamp values are
min_util, max_util = (0, SCHED_CAPACITY_SCALE)
and thus, RT tasks always run at the maximum OPP if not otherwise constrained by userspace.

Cc Ingo Molnar <[email protected]>
Cc Peter Zijlstra <[email protected]>
Cc Rafael J. Wysocki <[email protected]>
Cc Viresh Kumar <[email protected]>
Cc Joel Fernandes <[email protected]>
Cc Juri Lelli <[email protected]>
Cc Dietmar Eggemann <[email protected]>
Cc Morten Rasmussen <[email protected]>
---
 kernel/sched/cpufreq_schedutil.c | 29 ++++++++++++++++-------------
 1 file changed, 16 insertions(+), 13 deletions(-)
kernel/sched/cpufreq_schedutil.c +16 -13
index fe53984d06a0..1c80ad65065a 100644
@@ -182,16 +182,21 @@ static void sugov_get_util(struct sugov_cpu *sg_cpu)
 
 static unsigned long sugov_aggregate_util(struct sugov_cpu *sg_cpu)
 {
+	unsigned long util = sg_cpu->util_dl;
 	struct rq *rq = cpu_rq(sg_cpu->cpu);
-	unsigned long util;
 
-	if (rq->rt.rt_nr_running) {
-		util = sg_cpu->max;
-	} else {
-		util = sg_cpu->util_dl;
-		if (rq->cfs.h_nr_running)
-			util += uclamp_util(sg_cpu->cpu, sg_cpu->util_cfs);
-	}
+	/*
+	 * RT and CFS utilization are clamped, according to the current CPU
+	 * constrains, only when they have RUNNABLE tasks.
+	 * Their utilization are individually clamped to ensure fairness across
+	 * classes, meaning that CFS always get (if possible) the (minimum)
+	 * required bandwidth on top of that required by higher priority
+	 * classes.
+	 */
+	if (rq->rt.rt_nr_running)
+		util += uclamp_util(sg_cpu->cpu, sg_cpu->max);
+	if (rq->cfs.h_nr_running)
+		util += uclamp_util(sg_cpu->cpu, sg_cpu->util_cfs);
 
 	/*
 	 * Ideally we would like to set util_dl as min/guaranteed freq and
@@ -235,12 +240,10 @@ static void sugov_set_iowait_boost(struct sugov_cpu *sg_cpu, unsigned int flags)
 	 *
 	 * Since DL tasks have a much more advanced bandwidth control, it's
 	 * safe to assume that IO boost does not apply to those tasks.
-	 * Instead, since for RT tasks we are going to max, we don't want to
-	 * clamp the IO boost max value.
+	 * Instead, for CFS and RT tasks we clamp the IO boost max value
+	 * considering the current constraints for the CPU.
 	 */
-	max_boost = sg_cpu->iowait_boost_max;
-	if (!cpu_rq(sg_cpu->cpu)->rt.rt_nr_running)
-		max_boost = uclamp_util(sg_cpu->cpu, max_boost);
+	max_boost = uclamp_util(sg_cpu->cpu, sg_cpu->iowait_boost_max);
 
 	/* Double the IO boost at each frequency increase */
 	sg_cpu->iowait_boost <<= 1;
Signature · 1 line
2.15.1
#24 lore raw

Re: [PATCH 4/7] sched/core: uclamp: add utilization clamping to the CPU controller

In reply to Patrick Bellasi

Hello, Patrick.

Comments purely on cgroup interface side.

Quoted 10 lines · Patrick Bellasi

This patch extends the CPU controller by adding a couple of new attributes, util_min and util_max, which can be used to enforce frequency boosting and capping. Specifically:

- util_min: defines the minimum CPU utilization which should be considered, e.g. when schedutil selects the frequency for a CPU while a task in this group is RUNNABLE.
i.e. the task will run at least at a minimum frequency which
corresponds to the min_util utilization

Quoted 5 lines

- util_max: defines the maximum CPU utilization which should be considered, e.g. when schedutil selects the frequency for a CPU while a task in this group is RUNNABLE.
i.e. the task will run up to a maximum frequency which
corresponds to the max_util utilization

I'm not too enthusiastic about util_min/max given that it can easily be read as actual utilization based bandwidth control when what's actually implemented, IIUC, is affecting CPU frequency selection. Maybe something like cpu.freq.min/max are better names?

Quoted 6 lines

These attributes:
a) are tunable at all hierarchy levels, i.e. at root group level too, thus allowing to define the minimum and maximum frequency constraints for all otherwise non-classified tasks (e.g. autogroups) and to be a sort-of replacement for cpufreq's powersave, ondemand and performance governors.

This is a problem which exists for all other interfaces. For historical and other reasons, at least till now, we've opted to put everything at system level outside of cgroup interface. We might change this in the future and duplicate system-level information and interfaces in the root cgroup but we wanna do that in a more systemtic fashion than adding an one-off knob in the cgroup root.

Besides, if a feature makes sense at the system level which is the cgroup root, it makes sense without cgroup mounted or enabled, so it needs a place outside cgroup one way or the other.

Quoted 2 lines

b) allow to create subgroups of tasks which are not violating the utilization constraints defined by the parent group.

Tying creation / config operations to the config propagation doesn't work well with delegation and is inconsistent with what other controllers are doing. For cases where the propagated config being visible in a sub cgroup is necessary, please add .effective files.

Quoted 1 line

Tasks on a subgroup can only be more boosted and/or capped, which is

Less boosted. .low at a parent level must set the upper bound of .low that all its descendants can have.

Thanks.

Signature · 1 line
tejun
#25 lore raw

Re: [PATCH 4/7] sched/core: uclamp: add utilization clamping to the CPU controller

Hi Tejun,

Quoted 17 lines · Tejun Heo
Quoted 10 lines · Patrick Bellasi

This patch extends the CPU controller by adding a couple of new attributes, util_min and util_max, which can be used to enforce frequency boosting and capping. Specifically:

- util_min: defines the minimum CPU utilization which should be considered, e.g. when schedutil selects the frequency for a CPU while a task in this group is RUNNABLE.
i.e. the task will run at least at a minimum frequency which
corresponds to the min_util utilization

Quoted 5 lines

- util_max: defines the maximum CPU utilization which should be considered, e.g. when schedutil selects the frequency for a CPU while a task in this group is RUNNABLE.
i.e. the task will run up to a maximum frequency which
corresponds to the max_util utilization

Quoted 3 lines

I'm not too enthusiastic about util_min/max given that it can easily be read as actual utilization based bandwidth control when what's actually implemented, IIUC, is affecting CPU frequency selection.

Right now we are basically affecting the frequency selection. However, the next step is to use this same interface to possibly bias task placement.

The idea is that:

- the util_min value can be used to possibly avoid CPUs which have a (maybe temporarily) limited capacity, for example, due to thermal pressure.

- a util_max value can use used to possibly identify tasks which can be co-scheduled together in a (maybe) limited capacity CPU since they are more likely "less important" tasks.

Thus, since this is a new user-space API, we would like to find a concept which is generic enough to express the current requirement but also easily accommodate future extensions.

Quoted 1 line

Maybe something like cpu.freq.min/max are better names?

IMO this is something too much platform specific.

I agree that utilization is maybe too much an implementation detail, but perhaps this can be solved by using a more generic range.

What about using values in the [0..100] range which define:

a percentage of the maximum available capacity
for the CPUs in the target system

Do you think this can work?

Quoted 7 lines
Quoted 6 lines

These attributes:
a) are tunable at all hierarchy levels, i.e. at root group level too, thus allowing to define the minimum and maximum frequency constraints for all otherwise non-classified tasks (e.g. autogroups) and to be a sort-of replacement for cpufreq's powersave, ondemand and performance governors.

Quoted 6 lines

This is a problem which exists for all other interfaces. For historical and other reasons, at least till now, we've opted to put everything at system level outside of cgroup interface. We might change this in the future and duplicate system-level information and interfaces in the root cgroup but we wanna do that in a more systemtic fashion than adding an one-off knob in the cgroup root.

I see, I think we can easily come up with a procfs/sysfs interface usable to define system-wide values.

Any suggestion for something already existing which I can use as a reference?

Quoted 3 lines

Besides, if a feature makes sense at the system level which is the cgroup root, it makes sense without cgroup mounted or enabled, so it needs a place outside cgroup one way or the other.

Indeed, and it makes perfectly sense now that we have also a non cgroup-based primary APU.

Quoted 7 lines
Quoted 2 lines

b) allow to create subgroups of tasks which are not violating the utilization constraints defined by the parent group.

Tying creation / config operations to the config propagation doesn't work well with delegation and is inconsistent with what other controllers are doing. For cases where the propagated config being visible in a sub cgroup is necessary, please add .effective files.

I'm not sure to understand this point: you mean that we should not enforce "consistency rules" among parent-child groups?

I have to look better into this "effective" concept. Meanwhile, can you make a simple example?

Quoted 4 lines
Quoted 1 line

Tasks on a subgroup can only be more boosted and/or capped, which is

Less boosted. .low at a parent level must set the upper bound of .low that all its descendants can have.

Is that a mandatory requirement? Or based on a proper justification you can also accept what I'm proposing?

I've always been more of the idea that what I'm proposing could make more sense for a general case but perhaps I just need to go back and better check the use-cases we have on hand to see if it's really required or not.

Thanks for the prompt feedbacks!

Signature · 3 lines
#include <best/regards.h>

Patrick Bellasi
#26 lore raw

Re: [PATCH 4/7] sched/core: uclamp: add utilization clamping to the CPU controller

Hello,

Quoted 14 lines · Patrick Bellasi
Quoted 3 lines

I'm not too enthusiastic about util_min/max given that it can easily be read as actual utilization based bandwidth control when what's actually implemented, IIUC, is affecting CPU frequency selection.

Right now we are basically affecting the frequency selection. However, the next step is to use this same interface to possibly bias task placement.

The idea is that:

- the util_min value can be used to possibly avoid CPUs which have a (maybe temporarily) limited capacity, for example, due to thermal pressure.

Quoted 7 lines

- a util_max value can use used to possibly identify tasks which can be co-scheduled together in a (maybe) limited capacity CPU since they are more likely "less important" tasks.

Thus, since this is a new user-space API, we would like to find a concept which is generic enough to express the current requirement but also easily accommodate future extensions.

I'm not sure we can overload the meanings like that on the same interface. Right now, it doesn't say anything about bandwidth (or utilization) allocation. It just limits the frequency range the particular cpu that the task ended up on can be in and what you're describing above is the third different thing. It doesn't seem clear that they're something which can be overloaded onto the same interface.

Quoted 7 lines
Quoted 1 line

Maybe something like cpu.freq.min/max are better names?

IMO this is something too much platform specific.

I agree that utilization is maybe too much an implementation detail, but perhaps this can be solved by using a more generic range.

Quoted 6 lines

What about using values in the [0..100] range which define:

a percentage of the maximum available capacity
for the CPUs in the target system

Do you think this can work?

Yeah, sure, it's more that right now the intention isn't clear. A cgroup control knob which limits cpu frequency range while the cgroup is on a cpu is a very different thing from a cgroup knob which restricts what tasks can be scheduled on the same cpu. They're actually incompatible. Doing the latter actively breaks the former.

Quoted 7 lines
Quoted 6 lines

This is a problem which exists for all other interfaces. For historical and other reasons, at least till now, we've opted to put everything at system level outside of cgroup interface. We might change this in the future and duplicate system-level information and interfaces in the root cgroup but we wanna do that in a more systemtic fashion than adding an one-off knob in the cgroup root.

Quoted 5 lines

I see, I think we can easily come up with a procfs/sysfs interface usable to define system-wide values.

Any suggestion for something already existing which I can use as a reference?

Most system level interfaces are there with a long history and things aren't that consistent. One route could be finding an interface implementing a nearby feature and staying consistent with that.

Quoted 7 lines
Quoted 4 lines

Tying creation / config operations to the config propagation doesn't work well with delegation and is inconsistent with what other controllers are doing. For cases where the propagated config being visible in a sub cgroup is necessary, please add .effective files.

I'm not sure to understand this point: you mean that we should not enforce "consistency rules" among parent-child groups?

You should. It just shouldn't make configurations fail cuz that ends up breaking delegations.

Quoted 2 lines

I have to look better into this "effective" concept. Meanwhile, can you make a simple example?

There's a recent cpuset patchset posted by Waiman Long. Googling for lkml cpuset and Waiman Long should find it easily.

Quoted 5 lines
Quoted 4 lines
Quoted 1 line

Tasks on a subgroup can only be more boosted and/or capped, which is

Less boosted. .low at a parent level must set the upper bound of .low that all its descendants can have.

Quoted 7 lines

Is that a mandatory requirement? Or based on a proper justification you can also accept what I'm proposing?

I've always been more of the idea that what I'm proposing could make more sense for a general case but perhaps I just need to go back and better check the use-cases we have on hand to see if it's really required or not.

Yeah, I think we want to stick to that semantics. That's what memory controller does and it'd be really confusing to flip the directions on different controllers.

Thanks.

Signature · 1 line
tejun
maintainer
#2 lore raw

Re: [PATCH 1/7] sched/core: uclamp: add CPU clamp groups accounting

In reply to Patrick Bellasi

kernel/sched/core.c static void set_load_weight(struct task_struct *p, bool update_load) · line 840

Quoted 7 lines · Patrick Bellasi
kernel/sched/core.c +6 -0
+static inline void uclamp_cpu_get(struct task_struct *p, int cpu, int clamp_id)
+{
+	struct uclamp_cpu *uc_cpu = &cpu_rq(cpu)->uclamp[clamp_id];
 
+static inline void uclamp_cpu_put(struct task_struct *p, int cpu, int clamp_id)
+{
+	struct uclamp_cpu *uc_cpu = &cpu_rq(cpu)->uclamp[clamp_id];

That all seems daft, since you already have rq at the call site.

kernel/sched/core.c static void set_load_weight(struct task_struct *p, bool update_load) · line 889

Quoted 11 lines
kernel/sched/core.c +11 -0
+static inline void uclamp_task_update(struct rq *rq, struct task_struct *p)
+{
+	int cpu = cpu_of(rq);
+	int clamp_id;
+
+	/* The idle task does not affect CPU's clamps */
+	if (unlikely(p->sched_class == &idle_sched_class))
+		return;
+	/* DEADLINE tasks do not affect CPU's clamps */
+	if (unlikely(p->sched_class == &dl_sched_class))
+		return;

This is wrong; it misses the stop_sched_class.

And since you're looking at sched_class anyway, maybe put a marker in there:

	if (!p->sched_class->has_clamping)
		return;
Quoted 7 lines
kernel/sched/core.c +7 -0
+	for (clamp_id = 0; clamp_id < UCLAMP_CNT; ++clamp_id) {
+		if (uclamp_task_affects(p, clamp_id))
+			uclamp_cpu_put(p, cpu, clamp_id);
+		else
+			uclamp_cpu_get(p, cpu, clamp_id);
+	}
+}
maintainer
#6 lore raw

Re: [PATCH 1/7] sched/core: uclamp: add CPU clamp groups accounting

In reply to Patrick Bellasi
Quoted 1 line · Patrick Bellasi

+static inline void init_uclamp(void)

WTH is that inline?

Quoted 4 lines
kernel/sched/core.c +4 -0
+{
+	struct uclamp_cpu *uc_cpu;
+	int clamp_id;
+	int cpu;

kernel/sched/core.c static void set_load_weight(struct task_struct *p, bool update_load) · line 916

Quoted 10 lines
kernel/sched/core.c +10 -0
+
+	mutex_init(&uclamp_mutex);
+
+	for (clamp_id = 0; clamp_id < UCLAMP_CNT; ++clamp_id) {
+		/* Init CPU's clamp groups */
+		for_each_possible_cpu(cpu) {
+			uc_cpu = &cpu_rq(cpu)->uclamp[clamp_id];
+			memset(uc_cpu, UCLAMP_NONE, sizeof(struct uclamp_cpu));
+		}
+	}

Those loops are the wrong way around, this shreds your cache. This is a slow path so it doesn't much matter, but it is sloppy.

Quoted 1 line

+}

maintainer
#10 lore raw

Re: [PATCH 1/7] sched/core: uclamp: add CPU clamp groups accounting

In reply to Patrick Bellasi
Quoted 9 lines · Patrick Bellasi
kernel/sched/core.c +9 -0
+static inline void uclamp_task_update(struct rq *rq, struct task_struct *p)
+{
+	int cpu = cpu_of(rq);
+	int clamp_id;
+
+	/* The idle task does not affect CPU's clamps */
+	if (unlikely(p->sched_class == &idle_sched_class))
+		return;
+	/* DEADLINE tasks do not affect CPU's clamps */

kernel/sched/core.c static void set_load_weight(struct task_struct *p, bool update_load) · line 897

Quoted 10 lines
kernel/sched/core.c +10 -0
+	if (unlikely(p->sched_class == &dl_sched_class))
+		return;
+
+	for (clamp_id = 0; clamp_id < UCLAMP_CNT; ++clamp_id) {
+		if (uclamp_task_affects(p, clamp_id))
+			uclamp_cpu_put(p, cpu, clamp_id);
+		else
+			uclamp_cpu_get(p, cpu, clamp_id);
+	}
+}

Is that uclamp_task_affects() thing there to fix up the fact you failed to propagate the calling context (enqueue/dequeue) ?

I find this code _really_ hard to read...

Quoted 16 lines
kernel/sched/core.c +2 -0
@@ -743,6 +929,7 @@ static inline void enqueue_task(struct rq *rq, struct task_struct *p, int flags)
 	if (!(flags & ENQUEUE_RESTORE))
 		sched_info_queued(rq, p);
 
+	uclamp_task_update(rq, p);
 	p->sched_class->enqueue_task(rq, p, flags);
 }
 
@@ -754,6 +941,7 @@ static inline void dequeue_task(struct rq *rq, struct task_struct *p, int flags)
 	if (!(flags & DEQUEUE_SAVE))
 		sched_info_dequeued(rq, p);
 
+	uclamp_task_update(rq, p);
 	p->sched_class->dequeue_task(rq, p, flags);
 }
 
maintainer
#16 lore raw

Re: [PATCH 1/7] sched/core: uclamp: add CPU clamp groups accounting

In reply to Patrick Bellasi
Quoted 16 lines · Patrick Bellasi
kernel/sched/sched.h +12 -0
+struct uclamp_group {
+	/* Utilization clamp value for tasks on this clamp group */
+	int value;
+	/* Number of RUNNABLE tasks on this clamp group */
+	int tasks;
+};
 
+struct uclamp_cpu {
+	/* Utilization clamp value for a CPU */
+	int value;
+	/* Utilization clamp groups affecting this CPU */
+	struct uclamp_group group[CONFIG_UCLAMP_GROUPS_COUNT + 1];
+};
@@ -811,6 +885,11 @@ struct rq {
 	unsigned long		cpu_capacity;

kernel/sched/sched.h struct rq { · line 895

Quoted 10 lines
kernel/sched/sched.h +5 -0
 	unsigned long		cpu_capacity_orig;
 
+#ifdef CONFIG_UCLAMP_TASK
+	/* util_{min,max} clamp values based on CPU's active tasks */
+	struct uclamp_cpu uclamp[UCLAMP_CNT];
+#endif
+
 	struct callback_head	*balance_callback;
 
 	unsigned char		idle_balance;

So that is:

struct rq {

	struct uclamp_cpu {
		int value;

		/* 4 byte hole */

		struct uclamp_group {
			int value;
			int tasks;
		} [COUNT + 1];	// size: COUNT+2 * 8 [bytes]

	} [2]; // size: 2 * COUNT+2 * 8 [bytes]

};

Which for the default (4) ends up being 96 bytes, or at least 2 lines. Do you want some alignment such that we don't span more lines?

Which touch extra on enqueue/dequeue, at least it's only for tasks that have a clamp set.

maintainer
#17 lore raw

Re: [PATCH 1/7] sched/core: uclamp: add CPU clamp groups accounting

Quoted 44 lines · Peter Zijlstra
Quoted 16 lines · Patrick Bellasi
kernel/sched/sched.h +12 -0
+struct uclamp_group {
+	/* Utilization clamp value for tasks on this clamp group */
+	int value;
+	/* Number of RUNNABLE tasks on this clamp group */
+	int tasks;
+};
 
+struct uclamp_cpu {
+	/* Utilization clamp value for a CPU */
+	int value;
+	/* Utilization clamp groups affecting this CPU */
+	struct uclamp_group group[CONFIG_UCLAMP_GROUPS_COUNT + 1];
+};
@@ -811,6 +885,11 @@ struct rq {
 	unsigned long		cpu_capacity;

kernel/sched/sched.h struct rq { · line 895

Quoted 10 lines
kernel/sched/sched.h +5 -0
 	unsigned long		cpu_capacity_orig;
 
+#ifdef CONFIG_UCLAMP_TASK
+	/* util_{min,max} clamp values based on CPU's active tasks */
+	struct uclamp_cpu uclamp[UCLAMP_CNT];
+#endif
+
 	struct callback_head	*balance_callback;
 
 	unsigned char		idle_balance;

So that is:

struct rq {

	struct uclamp_cpu {
		int value;

		/* 4 byte hole */

		struct uclamp_group {
			int value;
			int tasks;
		} [COUNT + 1];	// size: COUNT+2 * 8 [bytes]

	} [2]; // size: 2 * COUNT+2 * 8 [bytes]
Quoted 1 line

};

Note that you can slightly compress the data structure if you do something like:

	struct rq {

		int active_clamp[2];

		struct uclamp_group {
			int value;
			int task;
		} [COUNT + 1][2]; // XXX check array order of C
	};

It also avoids the active values being split over 2 lines; and we need those values every time, even if there are no active clamp tasks on the system.

maintainer
#18 lore raw

Re: [PATCH 1/7] sched/core: uclamp: add CPU clamp groups accounting

In reply to Patrick Bellasi
Quoted 5 lines · Patrick Bellasi
kernel/sched/core.c +5 -0
+static inline void uclamp_cpu_get(struct task_struct *p, int cpu, int clamp_id)
+{
+	struct uclamp_cpu *uc_cpu = &cpu_rq(cpu)->uclamp[clamp_id];
+	int clamp_value;
+	int group_id;

kernel/sched/core.c static void set_load_weight(struct task_struct *p, bool update_load) · line 811

Quoted 10 lines
kernel/sched/core.c +10 -0
+
+	/* Get task's specific clamp value */
+	clamp_value = p->uclamp[clamp_id].value;
+	group_id = p->uclamp[clamp_id].group_id;
+
+	/* No task specific clamp values: nothing to do */
+	if (group_id == UCLAMP_NONE)
+		return;
+
+	/* Increment the current group_id */

That I think qualifies being called a bad comment.

kernel/sched/core.c static void set_load_weight(struct task_struct *p, bool update_load) · line 815

Quoted 4 lines
kernel/sched/core.c +4 -0
+	uc_cpu->group[group_id].tasks += 1;
+
+	/* Mark task as enqueued for this clamp index */
+	p->uclamp_group_id[clamp_id] = group_id;

Why exactly do we need this? we got group_id from @p in the first place.

I suspect this is because when we update p->uclamp[], we don't update this active value (when needed), is that worth it?

Quoted 8 lines
kernel/sched/core.c +8 -0
+	/*
+	 * If this is the new max utilization clamp value, then we can update
+	 * straight away the CPU clamp value. Otherwise, the current CPU clamp
+	 * value is still valid and we are done.
+	 */
+	if (uc_cpu->value < clamp_value)
+		uc_cpu->value = clamp_value;
+}
maintainer
#3 lore raw

Re: [PATCH 1/7] sched/core: uclamp: add CPU clamp groups accounting

In reply to Peter Zijlstra
Quoted 25 lines · Peter Zijlstra

kernel/sched/core.c static void set_load_weight(struct task_struct *p, bool update_load) · line 840

Quoted 7 lines · Patrick Bellasi
kernel/sched/core.c +6 -0
+static inline void uclamp_cpu_get(struct task_struct *p, int cpu, int clamp_id)
+{
+	struct uclamp_cpu *uc_cpu = &cpu_rq(cpu)->uclamp[clamp_id];
 
+static inline void uclamp_cpu_put(struct task_struct *p, int cpu, int clamp_id)
+{
+	struct uclamp_cpu *uc_cpu = &cpu_rq(cpu)->uclamp[clamp_id];

That all seems daft, since you already have rq at the call site.

kernel/sched/core.c static void set_load_weight(struct task_struct *p, bool update_load) · line 889

Quoted 11 lines
kernel/sched/core.c +11 -0
+static inline void uclamp_task_update(struct rq *rq, struct task_struct *p)
+{
+	int cpu = cpu_of(rq);
+	int clamp_id;
+
+	/* The idle task does not affect CPU's clamps */
+	if (unlikely(p->sched_class == &idle_sched_class))
+		return;
+	/* DEADLINE tasks do not affect CPU's clamps */
+	if (unlikely(p->sched_class == &dl_sched_class))
+		return;

This is wrong; it misses the stop_sched_class.

Quoted 5 lines

And since you're looking at sched_class anyway, maybe put a marker in there:

	if (!p->sched_class->has_clamping)
		return;

Alternatively, we could simply add uclamp_task_{en,de}queue() into {en,de}queue_task_{fair,rt}().

Quoted 7 lines
Quoted 7 lines
kernel/sched/core.c +7 -0
+	for (clamp_id = 0; clamp_id < UCLAMP_CNT; ++clamp_id) {
+		if (uclamp_task_affects(p, clamp_id))
+			uclamp_cpu_put(p, cpu, clamp_id);
+		else
+			uclamp_cpu_get(p, cpu, clamp_id);
+	}
+}
#4 lore raw

Re: [PATCH 1/7] sched/core: uclamp: add CPU clamp groups accounting

Quoted 26 lines · Peter Zijlstra
Quoted 25 lines · Peter Zijlstra

kernel/sched/core.c static void set_load_weight(struct task_struct *p, bool update_load) · line 840

Quoted 7 lines · Patrick Bellasi
kernel/sched/core.c +6 -0
+static inline void uclamp_cpu_get(struct task_struct *p, int cpu, int clamp_id)
+{
+	struct uclamp_cpu *uc_cpu = &cpu_rq(cpu)->uclamp[clamp_id];
 
+static inline void uclamp_cpu_put(struct task_struct *p, int cpu, int clamp_id)
+{
+	struct uclamp_cpu *uc_cpu = &cpu_rq(cpu)->uclamp[clamp_id];

That all seems daft, since you already have rq at the call site.

kernel/sched/core.c static void set_load_weight(struct task_struct *p, bool update_load) · line 889

Quoted 11 lines
kernel/sched/core.c +11 -0
+static inline void uclamp_task_update(struct rq *rq, struct task_struct *p)
+{
+	int cpu = cpu_of(rq);
+	int clamp_id;
+
+	/* The idle task does not affect CPU's clamps */
+	if (unlikely(p->sched_class == &idle_sched_class))
+		return;
+	/* DEADLINE tasks do not affect CPU's clamps */
+	if (unlikely(p->sched_class == &dl_sched_class))
+		return;

This is wrong; it misses the stop_sched_class.

Quoted 8 lines
Quoted 5 lines

And since you're looking at sched_class anyway, maybe put a marker in there:

	if (!p->sched_class->has_clamping)
		return;

Alternatively, we could simply add uclamp_task_{en,de}queue() into {en,de}queue_task_{fair,rt}().

I like better your first proposal, I think it makes sense to factor out in core code used by both RT and FAIR the same way.

Do you have a strong preference?

Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
kernel/sched/core.c +7 -0
+	for (clamp_id = 0; clamp_id < UCLAMP_CNT; ++clamp_id) {
+		if (uclamp_task_affects(p, clamp_id))
+			uclamp_cpu_put(p, cpu, clamp_id);
+		else
+			uclamp_cpu_get(p, cpu, clamp_id);
+	}
+}
Signature · 3 lines
#include <best/regards.h>

Patrick Bellasi
#19 lore raw

Re: [PATCH 1/7] sched/core: uclamp: add CPU clamp groups accounting

In reply to Peter Zijlstra
Quoted 10 lines · Peter Zijlstra
Quoted 9 lines · Patrick Bellasi
kernel/sched/core.c +9 -0
+static inline void uclamp_cpu_get(struct task_struct *p, int cpu, int clamp_id)
+{
+	struct uclamp_cpu *uc_cpu = &cpu_rq(cpu)->uclamp[clamp_id];
+	int clamp_value;
+	int group_id;
+
+	/* Get task's specific clamp value */
+	clamp_value = p->uclamp[clamp_id].value;
+	group_id = p->uclamp[clamp_id].group_id;
Quoted 8 lines

kernel/sched/core.c static void set_load_weight(struct task_struct *p, bool update_load) · line 811

Quoted 6 lines
kernel/sched/core.c +6 -0
+
+	/* No task specific clamp values: nothing to do */
+	if (group_id == UCLAMP_NONE)
+		return;
+
+	/* Increment the current group_id */

That I think qualifies being called a bad comment.

my bad :/

Quoted 6 lines

kernel/sched/core.c static void set_load_weight(struct task_struct *p, bool update_load) · line 815

Quoted 4 lines
kernel/sched/core.c +4 -0
+	uc_cpu->group[group_id].tasks += 1;
+
+	/* Mark task as enqueued for this clamp index */
+	p->uclamp_group_id[clamp_id] = group_id;

Why exactly do we need this? we got group_id from @p in the first place.

The idea is to back-annotate on the task the group in which it has been refcounted. That allows a much simpler and less racy refcount decrements at dequeue/migration time.

That's also why we have a single call-back, uclamp_task_update(), for both enqueue/dequeue. Depending on the check performed by uclamp_task_affects() we know if we have to get or put the refcounter.

Quoted 2 lines

I suspect this is because when we update p->uclamp[], we don't update this active value (when needed), is that worth it?

What you mean by "we don't update this active value"?

Quoted 8 lines
Quoted 8 lines
kernel/sched/core.c +8 -0
+	/*
+	 * If this is the new max utilization clamp value, then we can update
+	 * straight away the CPU clamp value. Otherwise, the current CPU clamp
+	 * value is still valid and we are done.
+	 */
+	if (uc_cpu->value < clamp_value)
+		uc_cpu->value = clamp_value;
+}
Signature · 3 lines
#include <best/regards.h>

Patrick Bellasi
#11 lore raw

Re: [PATCH 1/7] sched/core: uclamp: add CPU clamp groups accounting

In reply to Peter Zijlstra
Quoted 15 lines · Peter Zijlstra
Quoted 14 lines · Patrick Bellasi
kernel/sched/core.c +14 -0
+static inline void uclamp_task_update(struct rq *rq, struct task_struct *p)
+{
+	int cpu = cpu_of(rq);
+	int clamp_id;
+
+	/* The idle task does not affect CPU's clamps */
+	if (unlikely(p->sched_class == &idle_sched_class))
+		return;
+	/* DEADLINE tasks do not affect CPU's clamps */
+	if (unlikely(p->sched_class == &dl_sched_class))
+		return;
+
+	for (clamp_id = 0; clamp_id < UCLAMP_CNT; ++clamp_id) {
+		if (uclamp_task_affects(p, clamp_id))
Quoted 8 lines

kernel/sched/core.c static void set_load_weight(struct task_struct *p, bool update_load) · line 897

Quoted 5 lines
kernel/sched/core.c +5 -0
+			uclamp_cpu_put(p, cpu, clamp_id);
+		else
+			uclamp_cpu_get(p, cpu, clamp_id);
+	}
+}

Is that uclamp_task_affects() thing there to fix up the fact you failed to propagate the calling context (enqueue/dequeue) ?

Not really, it's intended by design: we back annotate the clamp_group a task has been refcounted in.

The uclamp_task_affects() tells if we are refcounted now and then we know from the back-annotation from which refcounter we need to remove the task.

I found this solution much less racy and effective in avoiding to screw up the refcounter whenever we look at a task at either dequeue/migration time and these operations can overlaps with the slow-path. Meaning, when we change the task specific clamp_group either via syscall or cgroups attributes.

IOW, the back annotation allows to decouple refcounting from clamp_group configuration in a lockless way.

Quoted 1 line

I find this code _really_ hard to read...

Hope the explanation above clarifies the logic... do you have alternative proposals?

Quoted 16 lines
Quoted 16 lines
kernel/sched/core.c +2 -0
@@ -743,6 +929,7 @@ static inline void enqueue_task(struct rq *rq, struct task_struct *p, int flags)
 	if (!(flags & ENQUEUE_RESTORE))
 		sched_info_queued(rq, p);
 
+	uclamp_task_update(rq, p);
 	p->sched_class->enqueue_task(rq, p, flags);
 }
 
@@ -754,6 +941,7 @@ static inline void dequeue_task(struct rq *rq, struct task_struct *p, int flags)
 	if (!(flags & DEQUEUE_SAVE))
 		sched_info_dequeued(rq, p);
 
+	uclamp_task_update(rq, p);
 	p->sched_class->dequeue_task(rq, p, flags);
 }
 
Signature · 3 lines
#include <best/regards.h>

Patrick Bellasi
maintainer
#5 lore raw

Re: [PATCH 1/7] sched/core: uclamp: add CPU clamp groups accounting

In reply to Patrick Bellasi
Quoted 33 lines · Patrick Bellasi
Quoted 32 lines · Peter Zijlstra
Quoted 30 lines · Peter Zijlstra

kernel/sched/core.c static void set_load_weight(struct task_struct *p, bool update_load) · line 840

Quoted 7 lines · Patrick Bellasi
kernel/sched/core.c +6 -0
+static inline void uclamp_cpu_get(struct task_struct *p, int cpu, int clamp_id)
+{
+	struct uclamp_cpu *uc_cpu = &cpu_rq(cpu)->uclamp[clamp_id];
 
+static inline void uclamp_cpu_put(struct task_struct *p, int cpu, int clamp_id)
+{
+	struct uclamp_cpu *uc_cpu = &cpu_rq(cpu)->uclamp[clamp_id];

That all seems daft, since you already have rq at the call site.

kernel/sched/core.c static void set_load_weight(struct task_struct *p, bool update_load) · line 889

Quoted 11 lines
kernel/sched/core.c +11 -0
+static inline void uclamp_task_update(struct rq *rq, struct task_struct *p)
+{
+	int cpu = cpu_of(rq);
+	int clamp_id;
+
+	/* The idle task does not affect CPU's clamps */
+	if (unlikely(p->sched_class == &idle_sched_class))
+		return;
+	/* DEADLINE tasks do not affect CPU's clamps */
+	if (unlikely(p->sched_class == &dl_sched_class))
+		return;

This is wrong; it misses the stop_sched_class.

And since you're looking at sched_class anyway, maybe put a marker in there:

	if (!p->sched_class->has_clamping)
		return;
Quoted 7 lines
Quoted 2 lines

Alternatively, we could simply add uclamp_task_{en,de}queue() into {en,de}queue_task_{fair,rt}().

I like better your first proposal, I think it makes sense to factor out in core code used by both RT and FAIR the same way.

Do you have a strong preference?

The second is probably faster as it avoids the load+branch; then again, without LTO you'll get an actual call in return. Dunno...

#7 lore raw

Re: [PATCH 1/7] sched/core: uclamp: add CPU clamp groups accounting

In reply to Peter Zijlstra
Quoted 4 lines · Peter Zijlstra
Quoted 1 line · Patrick Bellasi

+static inline void init_uclamp(void)

WTH is that inline?

You mean I can avoid the attribute?
... or that I should do it in another way?

Quoted 9 lines
Quoted 9 lines
kernel/sched/core.c +9 -0
+{
+	struct uclamp_cpu *uc_cpu;
+	int clamp_id;
+	int cpu;
+
+	mutex_init(&uclamp_mutex);
+
+	for (clamp_id = 0; clamp_id < UCLAMP_CNT; ++clamp_id) {
+		/* Init CPU's clamp groups */
Quoted 8 lines

kernel/sched/core.c static void set_load_weight(struct task_struct *p, bool update_load) · line 916

Quoted 5 lines
kernel/sched/core.c +5 -0
+		for_each_possible_cpu(cpu) {
+			uc_cpu = &cpu_rq(cpu)->uclamp[clamp_id];
+			memset(uc_cpu, UCLAMP_NONE, sizeof(struct uclamp_cpu));
+		}
+	}

Those loops are the wrong way around, this shreds your cache. This is a slow path so it doesn't much matter, but it is sloppy.

Yess... my bad!... good catch! ;)

Quoted 2 lines
Quoted 2 lines

+}

Signature · 3 lines
#include <best/regards.h>

Patrick Bellasi
maintainer
#20 lore raw

Re: [PATCH 1/7] sched/core: uclamp: add CPU clamp groups accounting

In reply to Patrick Bellasi
Quoted 29 lines · Patrick Bellasi
Quoted 10 lines · Peter Zijlstra
Quoted 9 lines · Patrick Bellasi
kernel/sched/core.c +9 -0
+static inline void uclamp_cpu_get(struct task_struct *p, int cpu, int clamp_id)
+{
+	struct uclamp_cpu *uc_cpu = &cpu_rq(cpu)->uclamp[clamp_id];
+	int clamp_value;
+	int group_id;
+
+	/* Get task's specific clamp value */
+	clamp_value = p->uclamp[clamp_id].value;
+	group_id = p->uclamp[clamp_id].group_id;
Quoted 8 lines

kernel/sched/core.c static void set_load_weight(struct task_struct *p, bool update_load) · line 811

Quoted 6 lines
kernel/sched/core.c +6 -0
+
+	/* No task specific clamp values: nothing to do */
+	if (group_id == UCLAMP_NONE)
+		return;
+
+	/* Increment the current group_id */

That I think qualifies being called a bad comment.

my bad :/

Quoted 6 lines

kernel/sched/core.c static void set_load_weight(struct task_struct *p, bool update_load) · line 815

Quoted 4 lines
kernel/sched/core.c +4 -0
+	uc_cpu->group[group_id].tasks += 1;
+
+	/* Mark task as enqueued for this clamp index */
+	p->uclamp_group_id[clamp_id] = group_id;

Why exactly do we need this? we got group_id from @p in the first place.

Quoted 3 lines

The idea is to back-annotate on the task the group in which it has been refcounted. That allows a much simpler and less racy refcount decrements at dequeue/migration time.

I'm not following; the only possible reason for having this second copy of group_id is when your original value (p->uclamp[clamp_id].group_id) can change between enqueue and dequeue.

Why can this happen?

Quoted 3 lines

That's also why we have a single call-back, uclamp_task_update(), for both enqueue/dequeue. Depending on the check performed by uclamp_task_affects() we know if we have to get or put the refcounter.

But that check is _completely_ redundant, because you already _know_ from being in the en/de-queue path. So having that single callback is actively harmfull (and confusing).

maintainer
#8 lore raw

Re: [PATCH 1/7] sched/core: uclamp: add CPU clamp groups accounting

In reply to Patrick Bellasi
Quoted 8 lines · Patrick Bellasi
Quoted 4 lines · Peter Zijlstra
Quoted 1 line · Patrick Bellasi

+static inline void init_uclamp(void)

WTH is that inline?

You mean I can avoid the attribute?
... or that I should do it in another way?

yea, its init code, no need to go all inline with that (gcc will likely inline it anyway because static-with-single-callsite).

#9 lore raw

Re: [PATCH 1/7] sched/core: uclamp: add CPU clamp groups accounting

Quoted 5 lines · Peter Zijlstra
Quoted 4 lines · Patrick Bellasi
Quoted 3 lines · Peter Zijlstra
Quoted 1 line · Patrick Bellasi

+static inline void init_uclamp(void)

Quoted 7 lines
Quoted 4 lines
Quoted 1 line

WTH is that inline?

You mean I can avoid the attribute?
... or that I should do it in another way?

yea, its init code, no need to go all inline with that (gcc will likely inline it anyway because static-with-single-callsite).

Yes, indeed... I think I've just got the right above init_schedstats() as a reference and lazily want for code consistency :(

However, if we remove inline, we should probably still add an __init, isn't it?

Signature · 3 lines
#include <best/regards.h>

Patrick Bellasi
maintainer
#12 lore raw

Re: [PATCH 1/7] sched/core: uclamp: add CPU clamp groups accounting

In reply to Patrick Bellasi
Quoted 32 lines · Patrick Bellasi
Quoted 15 lines · Peter Zijlstra
Quoted 14 lines · Patrick Bellasi
kernel/sched/core.c +14 -0
+static inline void uclamp_task_update(struct rq *rq, struct task_struct *p)
+{
+	int cpu = cpu_of(rq);
+	int clamp_id;
+
+	/* The idle task does not affect CPU's clamps */
+	if (unlikely(p->sched_class == &idle_sched_class))
+		return;
+	/* DEADLINE tasks do not affect CPU's clamps */
+	if (unlikely(p->sched_class == &dl_sched_class))
+		return;
+
+	for (clamp_id = 0; clamp_id < UCLAMP_CNT; ++clamp_id) {
+		if (uclamp_task_affects(p, clamp_id))
Quoted 8 lines

kernel/sched/core.c static void set_load_weight(struct task_struct *p, bool update_load) · line 897

Quoted 5 lines
kernel/sched/core.c +5 -0
+			uclamp_cpu_put(p, cpu, clamp_id);
+		else
+			uclamp_cpu_get(p, cpu, clamp_id);
+	}
+}

Is that uclamp_task_affects() thing there to fix up the fact you failed to propagate the calling context (enqueue/dequeue) ?

Not really, it's intended by design: we back annotate the clamp_group a task has been refcounted in.

The uclamp_task_affects() tells if we are refcounted now and then we know from the back-annotation from which refcounter we need to remove the task.

Quoted 8 lines

I found this solution much less racy and effective in avoiding to screw up the refcounter whenever we look at a task at either dequeue/migration time and these operations can overlaps with the slow-path. Meaning, when we change the task specific clamp_group either via syscall or cgroups attributes.

IOW, the back annotation allows to decouple refcounting from clamp_group configuration in a lockless way.

But it adds extra state and logic, to a fastpath, for no reason.

I suspect you messed up the cgroup side; because the syscall should already have done task_rq_lock() and hold both p->pi_lock and rq->lock and have dequeued the task when changing the attribute.

It is actually really hard to make the syscall do it wrong.

#13 lore raw

Re: [PATCH 1/7] sched/core: uclamp: add CPU clamp groups accounting

Quoted 39 lines · Peter Zijlstra
Quoted 38 lines · Patrick Bellasi
Quoted 15 lines · Peter Zijlstra
Quoted 14 lines · Patrick Bellasi
kernel/sched/core.c +14 -0
+static inline void uclamp_task_update(struct rq *rq, struct task_struct *p)
+{
+	int cpu = cpu_of(rq);
+	int clamp_id;
+
+	/* The idle task does not affect CPU's clamps */
+	if (unlikely(p->sched_class == &idle_sched_class))
+		return;
+	/* DEADLINE tasks do not affect CPU's clamps */
+	if (unlikely(p->sched_class == &dl_sched_class))
+		return;
+
+	for (clamp_id = 0; clamp_id < UCLAMP_CNT; ++clamp_id) {
+		if (uclamp_task_affects(p, clamp_id))
Quoted 8 lines

kernel/sched/core.c static void set_load_weight(struct task_struct *p, bool update_load) · line 897

Quoted 5 lines
kernel/sched/core.c +5 -0
+			uclamp_cpu_put(p, cpu, clamp_id);
+		else
+			uclamp_cpu_get(p, cpu, clamp_id);
+	}
+}

Is that uclamp_task_affects() thing there to fix up the fact you failed to propagate the calling context (enqueue/dequeue) ?

Not really, it's intended by design: we back annotate the clamp_group a task has been refcounted in.

The uclamp_task_affects() tells if we are refcounted now and then we know from the back-annotation from which refcounter we need to remove the task.

I found this solution much less racy and effective in avoiding to screw up the refcounter whenever we look at a task at either dequeue/migration time and these operations can overlaps with the slow-path. Meaning, when we change the task specific clamp_group either via syscall or cgroups attributes.

Quoted 8 lines
Quoted 2 lines

IOW, the back annotation allows to decouple refcounting from clamp_group configuration in a lockless way.

But it adds extra state and logic, to a fastpath, for no reason.

I suspect you messed up the cgroup side; because the syscall should already have done task_rq_lock() and hold both p->pi_lock and rq->lock and have dequeued the task when changing the attribute.

Yes, actually I'm using task_rq_lock() from the cgroup callback to update each task already queued. And I do the same from the sched_setattr syscall...

Quoted 1 line

It is actually really hard to make the syscall do it wrong.

... thus, I'll look better into this.

Not sure now if there was some other corner-case.

In the past I remember some funny dance in cgroup callbacks when a task was terminating (like being moved in the root-rq just before exiting). But, as you say, if we always have the task_rq_lock we should be safe.

Signature · 3 lines
#include <best/regards.h>

Patrick Bellasi
#14 lore raw

Re: [PATCH 1/7] sched/core: uclamp: add CPU clamp groups accounting

Quoted 53 lines · Patrick Bellasi
Quoted 39 lines · Peter Zijlstra
Quoted 38 lines · Patrick Bellasi
Quoted 15 lines · Peter Zijlstra
Quoted 14 lines · Patrick Bellasi
kernel/sched/core.c +14 -0
+static inline void uclamp_task_update(struct rq *rq, struct task_struct *p)
+{
+	int cpu = cpu_of(rq);
+	int clamp_id;
+
+	/* The idle task does not affect CPU's clamps */
+	if (unlikely(p->sched_class == &idle_sched_class))
+		return;
+	/* DEADLINE tasks do not affect CPU's clamps */
+	if (unlikely(p->sched_class == &dl_sched_class))
+		return;
+
+	for (clamp_id = 0; clamp_id < UCLAMP_CNT; ++clamp_id) {
+		if (uclamp_task_affects(p, clamp_id))
Quoted 8 lines

kernel/sched/core.c static void set_load_weight(struct task_struct *p, bool update_load) · line 897

Quoted 5 lines
kernel/sched/core.c +5 -0
+			uclamp_cpu_put(p, cpu, clamp_id);
+		else
+			uclamp_cpu_get(p, cpu, clamp_id);
+	}
+}

Is that uclamp_task_affects() thing there to fix up the fact you failed to propagate the calling context (enqueue/dequeue) ?

Not really, it's intended by design: we back annotate the clamp_group a task has been refcounted in.

The uclamp_task_affects() tells if we are refcounted now and then we know from the back-annotation from which refcounter we need to remove the task.

I found this solution much less racy and effective in avoiding to screw up the refcounter whenever we look at a task at either dequeue/migration time and these operations can overlaps with the slow-path. Meaning, when we change the task specific clamp_group either via syscall or cgroups attributes.

Quoted 8 lines
Quoted 2 lines

IOW, the back annotation allows to decouple refcounting from clamp_group configuration in a lockless way.

But it adds extra state and logic, to a fastpath, for no reason.

I suspect you messed up the cgroup side; because the syscall should already have done task_rq_lock() and hold both p->pi_lock and rq->lock and have dequeued the task when changing the attribute.

Yes, actually I'm using task_rq_lock() from the cgroup callback to update each task already queued. And I do the same from the sched_setattr syscall...

Quoted 5 lines
Quoted 1 line

It is actually really hard to make the syscall do it wrong.

... thus, I'll look better into this.

Not sure now if there was some other corner-case.

Actually, I've just remembered another use-case for that back-annotation. That's used when we have cgroups and per-task API asserting two different clamp values.

For example, a task in a TG with max_clamp=50 is further clamped with a task specific max_clamp=10. The back annotation tracks the group_id in which we have been refcount right now, which is the task specific group in the previous example.

Signature · 3 lines
#include <best/regards.h>

Patrick Bellasi
maintainer
#15 lore raw

Re: [PATCH 1/7] sched/core: uclamp: add CPU clamp groups accounting

In reply to Patrick Bellasi
Quoted 4 lines · Patrick Bellasi

In the past I remember some funny dance in cgroup callbacks when a task was terminating (like being moved in the root-rq just before exiting). But, as you say, if we always have the task_rq_lock we should be safe.

The syscall does the whole:

	task_rq_lock();
	queued = task_on_rq_queued();
	running = task_current();
	if (queued)
		dequeue_task();
	if (running)
		put_prev_task();

	/*
	 * task is in invariant state here,
	 * muck with it.
	 */

	if (queued)
		enqueue_task();
	if (running)
		set_curr_task()
	task_rq_unlock();

pattern; which because C sucks, we've not found a good template for yet.

Without the dequeue/put,enqueue/set you have to jump a few extra hoops.

#27 lore raw

Re: [PATCH 4/7] sched/core: uclamp: add utilization clamping to the CPU controller

In reply to Tejun Heo

Hi Tejun,

Quoted 25 lines · Tejun Heo

Hello,

Quoted 14 lines · Patrick Bellasi
Quoted 3 lines

I'm not too enthusiastic about util_min/max given that it can easily be read as actual utilization based bandwidth control when what's actually implemented, IIUC, is affecting CPU frequency selection.

Right now we are basically affecting the frequency selection. However, the next step is to use this same interface to possibly bias task placement.

The idea is that:

- the util_min value can be used to possibly avoid CPUs which have a (maybe temporarily) limited capacity, for example, due to thermal pressure.

Quoted 7 lines

- a util_max value can use used to possibly identify tasks which can be co-scheduled together in a (maybe) limited capacity CPU since they are more likely "less important" tasks.

Thus, since this is a new user-space API, we would like to find a concept which is generic enough to express the current requirement but also easily accommodate future extensions.

Quoted 7 lines

I'm not sure we can overload the meanings like that on the same interface. Right now, it doesn't say anything about bandwidth (or utilization) allocation. It just limits the frequency range the particular cpu that the task ended up on can be in and what you're describing above is the third different thing. It doesn't seem clear that they're something which can be overloaded onto the same interface.

Actually no, its not about overloading them. What's Patrick is defining here is a property/attribute. What that attribute is used for (the algorithms that use it) are a different topic. Like, it can be used by the frequency selection algorithms or the task placement algorithm. There are multiple algorithms that can use the property. To me, this part of the patch makes sense. Maybe it should really be called "task_size" or something, since that's what it really is.

[...]

Quoted 8 lines
Quoted 8 lines
Quoted 4 lines
Quoted 1 line

Tasks on a subgroup can only be more boosted and/or capped, which is

Less boosted. .low at a parent level must set the upper bound of .low that all its descendants can have.

Is that a mandatory requirement? Or based on a proper justification you can also accept what I'm proposing?

Quoted 9 lines
Quoted 4 lines

I've always been more of the idea that what I'm proposing could make more sense for a general case but perhaps I just need to go back and better check the use-cases we have on hand to see if it's really required or not.

Yeah, I think we want to stick to that semantics. That's what memory controller does and it'd be really confusing to flip the directions on different controllers.

What about the .high ? I think there was some confusion about how to define that for subgroups. It could perhaps be such that the .high of parent is the lower bound of the .high on child but then I'm not sure if that fits well with the delegation policies...

thanks,

- Joel

#28 lore raw

Re: [PATCH 4/7] sched/core: uclamp: add utilization clamping to the CPU controller

Hello, Joel.

Quoted 7 lines · Joel Fernandes

Actually no, its not about overloading them. What's Patrick is defining here is a property/attribute. What that attribute is used for (the algorithms that use it) are a different topic. Like, it can be used by the frequency selection algorithms or the task placement algorithm. There are multiple algorithms that can use the property. To me, this part of the patch makes sense. Maybe it should really be called "task_size" or something, since that's what it really is.

I understand that the interface can encode certain intentions and then there can be different strategies to implement that, but the two things mentioned here seem fundamentally different to declare them to be two different implementations of the same intention.

Quoted 8 lines
Quoted 3 lines

Yeah, I think we want to stick to that semantics. That's what memory controller does and it'd be really confusing to flip the directions on different controllers.

What about the .high ? I think there was some confusion about how to define that for subgroups. It could perhaps be such that the .high of parent is the lower bound of the .high on child but then I'm not sure if that fits well with the delegation policies...

The basic rule is simple. A child can never obtain more than its ancestors.

Thanks.

Signature · 1 line
tejun