sched_ext: Implement DSQ iterator
Subsystems (3)
Tejun Heo
reviewerPATCH v5 1/1 sched_ext: Implement DSQ iterator
DSQs are very opaque in the consumption path. The BPF scheduler has no way of knowing which tasks are being considered and which is picked. This patchset adds BPF DSQ iterator to allow BPF schedulers more flexibility in how DSQs are used. See the 0002 patch for more details.
Changes from v4 (https://lore.kernel.org/all/[email protected]/):
- Comment added to clarify use of naked list_empty(&dsq->list) test.
- scx_qmap changes separated into its own patch (0003).
There are no functional changes since v4. I'll apply these patches to sched_ext/for-6.11 with the outstanding acks.
This patchset contains the following three patches:
0001-sched_ext-Take-out-priq-and-flags-from-scx_dsq_node.patch 0002-sched_ext-Implement-DSQ-iterator.patch 0003-sched_ext-scx_qmap-Add-an-example-usage-of-DSQ-itera.patch
and is also availalbe in the following git branch:
git://git.kernel.org/pub/scm/linux/kernel/git/tj/sched_ext.git scx-dsq-iter-v5
diffstat follows. Thanks.
include/linux/sched/ext.h | 13 +- init/init_task.c | 2 kernel/sched/ext.c | 244 ++++++++++++++++++++++++++++++++++++++++++++++------- tools/sched_ext/include/scx/common.bpf.h | 3 tools/sched_ext/scx_qmap.bpf.c | 25 +++++ tools/sched_ext/scx_qmap.c | 8 + 6 files changed, 259 insertions(+), 36 deletions(-)
--
tejun
Tejun Heo
reviewerPATCH 1/3 sched_ext: Take out ->priq and ->flags from scx_dsq_node
struct scx_dsq_node contains two data structure nodes to link the containing task to a DSQ and a flags field that is protected by the lock of the associated DSQ. One reason why they are grouped into a struct is to use the type independently as a cursor node when iterating tasks on a DSQ. However, when iterating, the cursor only needs to be linked on the FIFO list and the rb_node part ends up inflating the size of the iterator data structure unnecessarily making it potentially too expensive to place it on stack.
Take ->priq and ->flags out of scx_dsq_node and put them in sched_ext_entity as ->dsq_priq and ->dsq_flags, respectively. scx_dsq_node is renamed to scx_dsq_list_node and the field names are renamed accordingly. This will help implementing DSQ task iterator that can be allocated on stack.
No functional change intended.
--- include/linux/sched/ext.h | 10 ++++---- init/init_task.c | 2 +- kernel/sched/ext.c | 54 +++++++++++++++++++-------------------- 3 files changed, 33 insertions(+), 33 deletions(-)
include/linux/sched/ext.h +5 -5
@@ -121,10 +121,8 @@ enum scx_kf_mask { __SCX_KF_TERMINAL = SCX_KF_ENQUEUE | SCX_KF_SELECT_CPU | SCX_KF_REST, }; -struct scx_dsq_node { -struct list_head list;/* dispatch order */ -struct rb_node priq;/* p->scx.dsq_vtime order */ -u32 flags;/* SCX_TASK_DSQ_* flags */ +struct scx_dsq_list_node { +struct list_head node; };/*
@@ -133,7 +131,9 @@ struct scx_dsq_node {*/ struct sched_ext_entity {struct scx_dispatch_q *dsq; -struct scx_dsq_node dsq_node;/* protected by dsq lock */ +struct scx_dsq_list_node dsq_list;/* dispatch order */ +struct rb_node dsq_priq;/* p->scx.dsq_vtime order */ +u32 dsq_flags;/* protected by DSQ lock */ u32 flags;/* protected by rq lock */ u32 weight;s32 sticky_cpu;
init/init_task.c +1 -1
@@ -102,7 +102,7 @@ struct task_struct init_task __aligned(L1_CACHE_BYTES) = {#endif #ifdef CONFIG_SCHED_CLASS_EXT .scx = { - .dsq_node.list = LIST_HEAD_INIT(init_task.scx.dsq_node.list), + .dsq_list.node = LIST_HEAD_INIT(init_task.scx.dsq_list.node), .sticky_cpu = -1, .holding_cpu = -1, .runnable_node = LIST_HEAD_INIT(init_task.scx.runnable_node),
kernel/sched/ext.c +27 -27
137 lines not on this page · raw message
Signature · 1 line
2.45.2
Re: [PATCH 1/3] sched_ext: Take out ->priq and ->flags from scx_dsq_node
Quoted 13 lines · Tejun Heo
struct
scx_dsq_nodecontains two data structure nodes to link the containing task to a DSQ and a flags field that is protected by the lock of the associated DSQ. One reason why they are grouped into a struct is to use the type independently as a cursor node when iterating tasks on a DSQ. However, when iterating, the cursor only needs to be linked on the FIFO list and therb_nodepart ends up inflating the size of the iterator data structure unnecessarily making it potentially too expensive to place it on stack.Take ->priq and ->flags out of
scx_dsq_nodeand put them insched_ext_entityas ->dsq_priq and ->dsq_flags, respectively.scx_dsq_nodeis renamed toscx_dsq_list_nodeand the field names are renamed accordingly. This will help implementing DSQ task iterator that can be allocated on stack.
Quoted 6 lines
No functional change intended.
Signed-off-by Tejun Heo <[email protected]>Suggested-by Alexei Starovoitov <[email protected]>Cc David Vernet <[email protected]>
Tejun Heo
reviewerPATCH 2/3 sched_ext: Implement DSQ iterator
In reply to Tejun HeoDSQs are very opaque in the consumption path. The BPF scheduler has no way of knowing which tasks are being considered and which is picked. This patch adds BPF DSQ iterator.
- Allows iterating tasks queued on a DSQ in the dispatch order or reverse from anywhere using bpf_for_each(scx_dsq) or calling the iterator kfuncs directly.
- Has ordering guarantee where only tasks which were already queued when the iteration started are visible and consumable during the iteration.
v5: - Add a comment to the naked list_empty(&dsq->list) test in
consume_dispatch_q() to explain the reasoning behind the lockless test and by extension why nldsq_next_task() isn't used there.
- scx_qmap changes separated into its own patch.
v4: - bpf_iter_scx_dsq_new() declaration in common.bpf.h was using the wrong
type for the last argument (bool rev instead of u64 flags). Fix it.
v3: - Alexei pointed out that the iterator is too big to allocate on stack.
Added a prep patch to reduce the size of the cursor. Now bpf_iter_scx_dsq is 48 bytes and bpf_iter_scx_dsq_kern is 40 bytes on 64bit.
- u32_before() comparison factored out.
v2: - scx_bpf_consume_task() is separated out into a separate patch.
- DSQ seq and iter flags don't need to be u64. Use u32.
--- include/linux/sched/ext.h | 3 + kernel/sched/ext.c | 192 ++++++++++++++++++++++- tools/sched_ext/include/scx/common.bpf.h | 3 + 3 files changed, 196 insertions(+), 2 deletions(-)
include/linux/sched/ext.h +3 -0
@@ -61,6 +61,7 @@ struct scx_dispatch_q {struct list_head list;/* tasks in dispatch order */ struct rb_root priq;/* used to order by p->scx.dsq_vtime */ u32 nr; +u32 seq;/* used by BPF iter */ u64 id;struct rhash_head hash_node;struct llist_node free_node;
@@ -123,6 +124,7 @@ enum scx_kf_mask {struct scx_dsq_list_node {struct list_head node; +bool is_bpf_iter_cursor; };/*
@@ -133,6 +135,7 @@ struct sched_ext_entity {struct scx_dispatch_q *dsq;struct scx_dsq_list_node dsq_list;/* dispatch order */ struct rb_node dsq_priq;/* p->scx.dsq_vtime order */ +u32 dsq_seq;u32 dsq_flags;/* protected by DSQ lock */ u32 flags;/* protected by rq lock */ u32 weight;
kernel/sched/ext.c +190 -2
239 lines not on this page · raw message
tools/sched_ext/include/scx/common.bpf.h +3 -0
@@ -39,6 +39,9 @@ u32 scx_bpf_reenqueue_local(void) __ksym; void scx_bpf_kick_cpu(s32 cpu, u64 flags) __ksym; s32 scx_bpf_dsq_nr_queued(u64 dsq_id) __ksym; void scx_bpf_destroy_dsq(u64 dsq_id) __ksym; +int bpf_iter_scx_dsq_new(struct bpf_iter_scx_dsq *it, u64 dsq_id, u64 flags) __ksym __weak; +struct task_struct *bpf_iter_scx_dsq_next (struct bpf_iter_scx_dsq *it) __ksym__weak ; +void bpf_iter_scx_dsq_destroy(struct bpf_iter_scx_dsq *it) __ksym __weak; void scx_bpf_exit_bstr(s64 exit_code, char *fmt, unsigned long long *data, u32 data__sz) __ksym __weak; void scx_bpf_error_bstr(char *fmt, unsigned long long *data, u32 data_len) __ksym; void scx_bpf_dump_bstr(char *fmt, unsigned long long *data, u32 data_len) __ksym __weak;
Signature · 1 line
2.45.2
Tejun Heo
reviewerPATCH 3/3 sched_ext/scx_qmap: Add an example usage of DSQ iterator
In reply to Tejun HeoImplement periodic dumping of the shared DSQ to demonstrate the use of the newly added DSQ iterator.
--- tools/sched_ext/scx_qmap.bpf.c | 25 +++++++++++++++++++++++++ tools/sched_ext/scx_qmap.c | 8 ++++++-- 2 files changed, 31 insertions(+), 2 deletions(-)
tools/sched_ext/scx_qmap.bpf.c +25 -0
@@ -36,6 +36,7 @@ const volatile u32 stall_user_nth;const volatile u32 stall_kernel_nth;const volatile u32 dsp_inf_loop_after;const volatile u32 dsp_batch; +const volatile bool print_shared_dsq;const volatile s32 disallow_tgid;const volatile bool suppress_dump;
@@ -604,10 +605,34 @@ static void monitor_cpuperf(void)scx_bpf_put_cpumask (online); } +/* +* Dump the currently queued tasks in the shared DSQ to demonstrate the usage of +* scx_bpf_dsq_nr_queued() and DSQ iterator. Raise the dispatch batch count to +* see meaningful dumps in the trace pipe. +*/ +static void dump_shared_dsq (void ) +{ +struct task_struct *p; +s32 nr; + +if (!(nr =scx_bpf_dsq_nr_queued (SHARED_DSQ ))) +return ; + +bpf_printk ("Dumping %d tasks in SHARED_DSQ in reverse order" , nr); + +bpf_rcu_read_lock (); +bpf_for_each (scx_dsq, p,SHARED_DSQ ,SCX_DSQ_ITER_REV ) +bpf_printk ("%s[%d]" , p->comm, p->pid); +bpf_rcu_read_unlock (); +} +static int monitor_timerfn (void *map,int *key,struct bpf_timer *timer) {monitor_cpuperf (); +if (print_shared_dsq) +dump_shared_dsq (); +bpf_timer_start (timer,ONE_SEC_IN_NS ,0 );return 0 ; }
tools/sched_ext/scx_qmap.c +6 -2
@@ -20,7 +20,7 @@ const char help_fmt[] ="See the top-level comment in .bpf.c for more details.\n" "\n" "Usage: %s [-s SLICE_US] [-e COUNT] [-t COUNT] [-T COUNT] [-l COUNT] [-b COUNT]\n" -" [-d PID] [-D LEN] [-p] [-v]\n" +" [-P] [-d PID] [-D LEN] [-p] [-v]\n" "\n" " -s SLICE_US Override slice duration\n" " -e COUNT Trigger scx_bpf_error() after COUNT enqueues\n"
@@ -28,6 +28,7 @@ const char help_fmt[] =" -T COUNT Stall every COUNT'th kernel thread\n" " -l COUNT Trigger dispatch infinite looping after COUNT dispatches\n" " -b COUNT Dispatch upto COUNT tasks together\n" +" -P Print out DSQ content to trace_pipe every second, use with -b\n" " -d PID Disallow a process from switching into SCHED_EXT (-1 for self)\n" " -D LEN Set scx_exit_info.dump buffer length\n" " -S Suppress qmap-specific debug dump\n"
@@ -62,7 +63,7 @@ int main(int argc, char **argv) skel =SCX_OPS_OPEN (qmap_ops, scx_qmap); -while ((opt =getopt (argc, argv,"s:e:t:T:l:b:d:D:Spvh" )) !=-1 ) { +while ((opt =getopt (argc, argv,"s:e:t:T:l:b:Pd:D:Spvh" )) !=-1 ) {switch (opt) {case 's' : skel->rodata->slice_ns =strtoull (optarg,NULL ,0 ) *1000 ;
@@ -82,6 +83,9 @@ int main(int argc, char **argv)case 'b' : skel->rodata->dsp_batch =strtoul (optarg,NULL ,0 );break ; +case 'P' : + skel->rodata->print_shared_dsq = true; +break ;case 'd' : skel->rodata->disallow_tgid =strtol (optarg,NULL ,0 );if (skel->rodata->disallow_tgid <0 )
Signature · 1 line
2.45.2