Skip to contentSkip to conversation

sched/ext: Add cpumask to skip unsuitable dispatch queues

PATCH In review Qiliang Yuan 2 participants 2 messages Feb 4

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 sched/ext: Add cpumask to skip unsuitable dispatch queues

Add a cpus_allowed cpumask to struct scx_dispatch_q to track the union of affinity masks for all tasks enqueued in a user-defined DSQ. This allows a CPU to quickly skip DSQs that contain no tasks runnable on the current CPU, avoiding wasteful O(N) scans.

- Allocate/free cpus_allowed only for user-defined DSQs.
- Use free_dsq_rcu_callback to safely free the DSQ and its nested mask.
- Update the mask in dispatch_enqueue() using cpumask_copy() for the first task and cpumask_or() for subsequent ones. Skip updates if the mask is already full.
- Update the DSQ mask in set_cpus_allowed_scx() when a task's affinity changes while enqueued.
- Handle allocation failures in scx_create_dsq() to prevent memory leaks.

This optimization improves performance with many DSQs and tight affinity constraints. The bitwise overhead is significantly lower than potential cache misses during task iteration.

---
v2:
- Fix memory leak by adding RCU callback to free dsq->cpus_allowed.
- Handle affinity changes while task is in DSQ via set_cpus_allowed_scx().
- Ensure dsq->cpus_allowed is only allocated for user DSQs.
- Handle allocation failures in scx_create_dsq().
- Optimize enqueue path by using cpumask_copy() for the first task and skipping OR if mask is already full.

 include/linux/sched/ext.h |  1 +
 kernel/sched/ext.c        | 68 ++++++++++++++++++++++++++++++++++++---
 2 files changed, 64 insertions(+), 5 deletions(-)
include/linux/sched/ext.h +1 -0
index bcb962d5ee7d..f20e57cf53a3 100644
@@ -79,6 +79,7 @@ struct scx_dispatch_q {
 	struct rhash_head	hash_node;
 	struct llist_node	free_node;
 	struct rcu_head		rcu;
+	struct cpumask		*cpus_allowed; /* union of all tasks' allowed cpus */
 };
 
 /* scx_entity.flags */
kernel/sched/ext.c +63 -5

127 lines not on this page · raw message

Signature · 1 line
2.51.0
reviewer
#1 lore raw
Quoted 14 lines · Qiliang Yuan

Add a cpus_allowed cpumask to struct scx_dispatch_q to track the union of affinity masks for all tasks enqueued in a user-defined DSQ. This allows a CPU to quickly skip DSQs that contain no tasks runnable on the current CPU, avoiding wasteful O(N) scans.

- Allocate/free cpus_allowed only for user-defined DSQs.
- Use free_dsq_rcu_callback to safely free the DSQ and its nested mask.
- Update the mask in dispatch_enqueue() using cpumask_copy() for the first task and cpumask_or() for subsequent ones. Skip updates if the mask is already full.
- Update the DSQ mask in set_cpus_allowed_scx() when a task's affinity changes while enqueued.
- Handle allocation failures in scx_create_dsq() to prevent memory leaks.

Quoted 6 lines

This optimization improves performance with many DSQs and tight affinity constraints. The bitwise overhead is significantly lower than potential cache misses during task iteration.

As Emil pointed out earlier, this adds overhead in general path which scales with the number of CPUs and the benefit isn't that generic. Similar optimizations can be done from BPF side and throwing a lot of tasks with varying affinity restrictions into a single queue frequently scanned by multiple CPUs is not scalable to begin with.

Thanks.

Signature · 1 line
tejun