* [PATCH v3] sched_ext: Introduce NUMA awareness to the default idle selection policy
@ 2024-10-27 17:49 Andrea Righi
2024-10-27 18:03 ` Tejun Heo
0 siblings, 1 reply; 3+ messages in thread
From: Andrea Righi @ 2024-10-27 17:49 UTC (permalink / raw)
To: Tejun Heo, David Vernet; +Cc: linux-kernel
Similarly to commit dfa4ed29b18c ("sched_ext: Introduce LLC awareness to
the default idle selection policy"), extend the built-in idle CPU
selection policy to also prioritize CPUs within the same NUMA node.
With this change applied, the built-in CPU idle selection policy follows
this logic:
- always prioritize CPUs from fully idle SMT cores,
- select the same CPU if possible,
- select a CPU within the same LLC domain,
- select a CPU within the same NUMA node.
Both NUMA and LLC awareness features are enabled only when the system
has multiple NUMA nodes or multiple LLC domains.
In the future, we may want to improve the NUMA node selection to account
the node distance from prev_cpu. Currently, the logic only tries to keep
tasks running on the same NUMA node. If all CPUs within a node are busy,
the next NUMA node is chosen randomly.
Signed-off-by: Andrea Righi <arighi@nvidia.com>
---
kernel/sched/ext.c | 139 ++++++++++++++++++++++++++++++++++++++-------
1 file changed, 120 insertions(+), 19 deletions(-)
ChangeLog v2 -> v3:
- fix RCU locking
- use highest_flag_domain() to determine the NUMA cpumasks
- rely on num_possible_cpus() instead of nr_cpu_ids
- update NUMA/LLC static_keys when an scx scheduler is loaded and on
CPU hotplug events
- rename static_keys to clarify that they are exclusively used by the
built-in select_cpu
ChangeLog v1 -> v2:
- autodetect at boot whether NUMA and LLC capabilities should be used
and use static_keys to control their activation
- rely on cpumask_of_node/cpu_to_node() to determine the NUMA domain
diff --git a/kernel/sched/ext.c b/kernel/sched/ext.c
index 6705c2e67c99..5be80b8ac4db 100644
--- a/kernel/sched/ext.c
+++ b/kernel/sched/ext.c
@@ -869,6 +869,8 @@ static DEFINE_STATIC_KEY_FALSE(scx_ops_enq_last);
static DEFINE_STATIC_KEY_FALSE(scx_ops_enq_exiting);
static DEFINE_STATIC_KEY_FALSE(scx_ops_cpu_preempt);
static DEFINE_STATIC_KEY_FALSE(scx_builtin_idle_enabled);
+static DEFINE_STATIC_KEY_FALSE(scx_selcpu_topo_llc);
+static DEFINE_STATIC_KEY_FALSE(scx_selcpu_topo_numa);
static struct static_key_false scx_has_op[SCX_OPI_END] =
{ [0 ... SCX_OPI_END-1] = STATIC_KEY_FALSE_INIT };
@@ -3124,31 +3126,79 @@ static s32 scx_pick_idle_cpu(const struct cpumask *cpus_allowed, u64 flags)
goto retry;
}
-#ifdef CONFIG_SCHED_MC
/*
- * Return the cpumask of CPUs usable by task @p in the same LLC domain of @cpu,
- * or NULL if the LLC domain cannot be determined.
+ * Initialize topology-aware scheduling.
+ *
+ * Detect if the system has multiple LLC or multiple NUMA domains and enable
+ * cache-aware / NUMA-aware scheduling optimizations in the default CPU idle
+ * selection policy.
*/
-static const struct cpumask *llc_domain(const struct task_struct *p, s32 cpu)
+static void update_selcpu_topology(void)
{
- struct sched_domain *sd = rcu_dereference(per_cpu(sd_llc, cpu));
- const struct cpumask *llc_cpus = sd ? sched_domain_span(sd) : NULL;
+ bool enable_llc = false, enable_numa = false;
+ s32 cpu;
- /*
- * Return the LLC domain only if the task is allowed to run on all
- * CPUs.
- */
- return p->nr_cpus_allowed == nr_cpu_ids ? llc_cpus : NULL;
-}
-#else /* CONFIG_SCHED_MC */
-static inline const struct cpumask *llc_domain(struct task_struct *p, s32 cpu)
-{
- return NULL;
+ rcu_read_lock();
+ for_each_possible_cpu(cpu) {
+ struct sched_domain *sd;
+ const struct cpumask *cpus;
+
+ sd = rcu_dereference(per_cpu(sd_llc, cpu));
+ if (sd) {
+ cpus = sched_domain_span(sd);
+ pr_debug("sched_ext: LLC cpu%d: %*pbl\n",
+ cpu, cpumask_pr_args(cpus));
+ if (cpumask_weight(cpus) < num_possible_cpus())
+ enable_llc = true;
+ }
+
+ sd = highest_flag_domain(cpu, SD_NUMA);
+ if (sd) {
+ cpus = sched_group_span(sd->groups);
+ pr_debug("sched_ext: NUMA cpu%d: %*pbl\n",
+ cpu, cpumask_pr_args(cpus));
+ if (cpumask_weight(cpus) < num_possible_cpus())
+ enable_numa = true;
+ }
+ }
+ rcu_read_unlock();
+
+ pr_debug("sched_ext: LLC idle selection %s\n",
+ enable_llc ? "enabled" : "disabled");
+ pr_debug("sched_ext: NUMA idle selection %s\n",
+ enable_numa ? "enabled" : "disabled");
+
+ if (enable_llc)
+ static_branch_enable_cpuslocked(&scx_selcpu_topo_llc);
+ else
+ static_branch_disable_cpuslocked(&scx_selcpu_topo_llc);
+ if (enable_numa)
+ static_branch_enable_cpuslocked(&scx_selcpu_topo_numa);
+ else
+ static_branch_disable_cpuslocked(&scx_selcpu_topo_numa);
}
-#endif /* CONFIG_SCHED_MC */
/*
- * Built-in cpu idle selection policy.
+ * Built-in CPU idle selection policy:
+ *
+ * 1. Prioritize full-idle cores:
+ * - always prioritize CPUs from fully idle cores (both logical CPUs are
+ * idle) to avoid interference caused by SMT.
+ *
+ * 2. Reuse the same CPU:
+ * - prefer the last used CPU to take advantage of cached data (L1, L2) and
+ * branch prediction optimizations.
+ *
+ * 3. Pick a CPU within the same LLC (Last-Level Cache):
+ * - if the above conditions aren't met, pick a CPU that shares the same LLC
+ * to maintain cache locality.
+ *
+ * 4. Pick a CPU within the same NUMA node, if enabled:
+ * - choose a CPU from the same NUMA node to reduce memory access latency.
+ *
+ * Step 3 and 4 are performed only if the system has, respectively, multiple
+ * LLC domains / multiple NUMA nodes (see scx_selcpu_topo_llc and
+ * scx_selcpu_topo_numa).
*
* NOTE: tasks that can only run on 1 CPU are excluded by this logic, because
* we never call ops.select_cpu() for them, see select_task_rq().
@@ -3156,7 +3206,8 @@ static inline const struct cpumask *llc_domain(struct task_struct *p, s32 cpu)
static s32 scx_select_cpu_dfl(struct task_struct *p, s32 prev_cpu,
u64 wake_flags, bool *found)
{
- const struct cpumask *llc_cpus = llc_domain(p, prev_cpu);
+ const struct cpumask *llc_cpus = NULL;
+ const struct cpumask *numa_cpus = NULL;
s32 cpu;
*found = false;
@@ -3166,6 +3217,30 @@ static s32 scx_select_cpu_dfl(struct task_struct *p, s32 prev_cpu,
return prev_cpu;
}
+ /*
+ * Determine the scheduling domain only if the task is allowed to run
+ * on all CPUs.
+ *
+ * This is done primarily for efficiency, as it avoids the overhead of
+ * updating a cpumask every time we need to select an idle CPU (which
+ * can be costly in large SMP systems), but it also aligns logically:
+ * if a task's scheduling domain is restricted by user-space (through
+ * CPU affinity), the task will simply use the flat scheduling domain
+ * defined by user-space.
+ */
+ if (p->nr_cpus_allowed >= num_possible_cpus()) {
+ if (static_branch_unlikely(&scx_selcpu_topo_numa))
+ numa_cpus = cpumask_of_node(cpu_to_node(prev_cpu));
+
+ if (static_branch_unlikely(&scx_selcpu_topo_llc)) {
+ struct sched_domain *sd;
+
+ sd = rcu_dereference(per_cpu(sd_llc, prev_cpu));
+ if (sd)
+ llc_cpus = sched_domain_span(sd);
+ }
+ }
+
/*
* If WAKE_SYNC, try to migrate the wakee to the waker's CPU.
*/
@@ -3226,6 +3301,15 @@ static s32 scx_select_cpu_dfl(struct task_struct *p, s32 prev_cpu,
goto cpu_found;
}
+ /*
+ * Search for any fully idle core in the same NUMA node.
+ */
+ if (numa_cpus) {
+ cpu = scx_pick_idle_cpu(numa_cpus, SCX_PICK_IDLE_CORE);
+ if (cpu >= 0)
+ goto cpu_found;
+ }
+
/*
* Search for any full idle core usable by the task.
*/
@@ -3251,6 +3335,15 @@ static s32 scx_select_cpu_dfl(struct task_struct *p, s32 prev_cpu,
goto cpu_found;
}
+ /*
+ * Search for any idle CPU in the same NUMA node.
+ */
+ if (numa_cpus) {
+ cpu = scx_pick_idle_cpu(numa_cpus, 0);
+ if (cpu >= 0)
+ goto cpu_found;
+ }
+
/*
* Search for any idle CPU usable by the task.
*/
@@ -3383,6 +3476,10 @@ static void handle_hotplug(struct rq *rq, bool online)
atomic_long_inc(&scx_hotplug_seq);
+ if ((SCX_HAS_OP(cpu_online) || SCX_HAS_OP(cpu_offline)) &&
+ static_branch_likely(&scx_builtin_idle_enabled))
+ update_selcpu_topology();
+
if (online && SCX_HAS_OP(cpu_online))
SCX_CALL_OP(SCX_KF_UNLOCKED, cpu_online, cpu);
else if (!online && SCX_HAS_OP(cpu_offline))
@@ -5202,6 +5299,10 @@ static int scx_ops_enable(struct sched_ext_ops *ops, struct bpf_link *link)
static_branch_enable_cpuslocked(&scx_has_op[i]);
check_hotplug_seq(ops);
+
+ if (!ops->update_idle || (ops->flags & SCX_OPS_KEEP_BUILTIN_IDLE))
+ update_selcpu_topology();
+
cpus_read_unlock();
ret = validate_ops(ops);
--
2.47.0
^ permalink raw reply related [flat|nested] 3+ messages in thread* Re: [PATCH v3] sched_ext: Introduce NUMA awareness to the default idle selection policy
2024-10-27 17:49 [PATCH v3] sched_ext: Introduce NUMA awareness to the default idle selection policy Andrea Righi
@ 2024-10-27 18:03 ` Tejun Heo
2024-10-27 18:51 ` Andrea Righi
0 siblings, 1 reply; 3+ messages in thread
From: Tejun Heo @ 2024-10-27 18:03 UTC (permalink / raw)
To: Andrea Righi; +Cc: David Vernet, linux-kernel
Hello,
On Sun, Oct 27, 2024 at 06:49:53PM +0100, Andrea Righi wrote:
...
> +static void update_selcpu_topology(void)
> {
> + bool enable_llc = false, enable_numa = false;
> + s32 cpu;
>
> + rcu_read_lock();
> + for_each_possible_cpu(cpu) {
> + struct sched_domain *sd;
> + const struct cpumask *cpus;
> +
> + sd = rcu_dereference(per_cpu(sd_llc, cpu));
> + if (sd) {
> + cpus = sched_domain_span(sd);
> + pr_debug("sched_ext: LLC cpu%d: %*pbl\n",
> + cpu, cpumask_pr_args(cpus));
> + if (cpumask_weight(cpus) < num_possible_cpus())
> + enable_llc = true;
> + }
> +
> + sd = highest_flag_domain(cpu, SD_NUMA);
> + if (sd) {
> + cpus = sched_group_span(sd->groups);
> + pr_debug("sched_ext: NUMA cpu%d: %*pbl\n",
> + cpu, cpumask_pr_args(cpus));
> + if (cpumask_weight(cpus) < num_possible_cpus())
> + enable_numa = true;
This isn't a big problem but the loop looks a bit odd in that once both are
set, it's obvious that there's no reason to continue. Doesn't this need to
check only one CPU in fact? e.g. for the first possible CPU, if sd doesn't
exist or cpumask_weight(sd) == num_possible_cpu(), don't we know for sure
that llc or numa doesn't need to be enabled and vice-versa?
> static s32 scx_select_cpu_dfl(struct task_struct *p, s32 prev_cpu,
> u64 wake_flags, bool *found)
> {
> + const struct cpumask *llc_cpus = NULL;
> + const struct cpumask *numa_cpus = NULL;
> s32 cpu;
>
> *found = false;
...
> + if (p->nr_cpus_allowed >= num_possible_cpus()) {
> + if (static_branch_unlikely(&scx_selcpu_topo_numa))
> + numa_cpus = cpumask_of_node(cpu_to_node(prev_cpu));
> +
> + if (static_branch_unlikely(&scx_selcpu_topo_llc)) {
static_branch_maybe() is probably the better one for llc.
Thanks.
--
tejun
^ permalink raw reply [flat|nested] 3+ messages in thread* Re: [PATCH v3] sched_ext: Introduce NUMA awareness to the default idle selection policy
2024-10-27 18:03 ` Tejun Heo
@ 2024-10-27 18:51 ` Andrea Righi
0 siblings, 0 replies; 3+ messages in thread
From: Andrea Righi @ 2024-10-27 18:51 UTC (permalink / raw)
To: Tejun Heo; +Cc: David Vernet, linux-kernel
On Sun, Oct 27, 2024 at 08:03:53AM -1000, Tejun Heo wrote:
> Hello,
>
> On Sun, Oct 27, 2024 at 06:49:53PM +0100, Andrea Righi wrote:
> ...
> > +static void update_selcpu_topology(void)
> > {
> > + bool enable_llc = false, enable_numa = false;
> > + s32 cpu;
> >
> > + rcu_read_lock();
> > + for_each_possible_cpu(cpu) {
> > + struct sched_domain *sd;
> > + const struct cpumask *cpus;
> > +
> > + sd = rcu_dereference(per_cpu(sd_llc, cpu));
> > + if (sd) {
> > + cpus = sched_domain_span(sd);
> > + pr_debug("sched_ext: LLC cpu%d: %*pbl\n",
> > + cpu, cpumask_pr_args(cpus));
> > + if (cpumask_weight(cpus) < num_possible_cpus())
> > + enable_llc = true;
> > + }
> > +
> > + sd = highest_flag_domain(cpu, SD_NUMA);
> > + if (sd) {
> > + cpus = sched_group_span(sd->groups);
> > + pr_debug("sched_ext: NUMA cpu%d: %*pbl\n",
> > + cpu, cpumask_pr_args(cpus));
> > + if (cpumask_weight(cpus) < num_possible_cpus())
> > + enable_numa = true;
>
> This isn't a big problem but the loop looks a bit odd in that once both are
> set, it's obvious that there's no reason to continue. Doesn't this need to
> check only one CPU in fact? e.g. for the first possible CPU, if sd doesn't
> exist or cpumask_weight(sd) == num_possible_cpu(), don't we know for sure
> that llc or numa doesn't need to be enabled and vice-versa?
I was also wondering about this, but I wasn't sure if you can create
non-standard NUMA configurations (like assigning a CPU to multiple
heterogeneous NUMA node), for example with virtualization or NUMA
emulation.
I just checked with qemu and it seems that it doesn't allow to assign a
CPU to multiple NUMA nodes, so maybe it's a valid assumptions that we
won't have unusual overlapping NUMA configurations.
With this assumption, yes, we can just check the first CPU, and it's
probably reasonable to do so anyway.
>
> > static s32 scx_select_cpu_dfl(struct task_struct *p, s32 prev_cpu,
> > u64 wake_flags, bool *found)
> > {
> > + const struct cpumask *llc_cpus = NULL;
> > + const struct cpumask *numa_cpus = NULL;
> > s32 cpu;
> >
> > *found = false;
> ...
> > + if (p->nr_cpus_allowed >= num_possible_cpus()) {
> > + if (static_branch_unlikely(&scx_selcpu_topo_numa))
> > + numa_cpus = cpumask_of_node(cpu_to_node(prev_cpu));
> > +
> > + if (static_branch_unlikely(&scx_selcpu_topo_llc)) {
>
> static_branch_maybe() is probably the better one for llc.
Ah, definitely!
Thanks! Will apply these changes and send a new patch.
-Andrea
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2024-10-27 18:52 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2024-10-27 17:49 [PATCH v3] sched_ext: Introduce NUMA awareness to the default idle selection policy Andrea Righi
2024-10-27 18:03 ` Tejun Heo
2024-10-27 18:51 ` Andrea Righi
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.