Skip to content

Commit 8da7bf2

Browse files
committed
tools/sched_ext: Receive updates from SCX repo
Receive tools/sched_ext updates form https://github.com/sched-ext/scx to sync userspace bits: - scx_bpf_dump_header() added which can be used to print out basic scheduler info on dump. - BPF possible/online CPU iterators added. - CO-RE enums added. The enums are autogenerated from vmlinux.h. Include the generated artifacts in tools/sched_ext to keep the Makefile simpler. - Other misc changes. Signed-off-by: Tejun Heo <[email protected]>
1 parent 4572541 commit 8da7bf2

14 files changed

+286
-8
lines changed

tools/sched_ext/include/scx/common.bpf.h

Lines changed: 81 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
#ifdef LSP
1111
#define __bpf__
12-
#include "../vmlinux/vmlinux.h"
12+
#include "../vmlinux.h"
1313
#else
1414
#include "vmlinux.h"
1515
#endif
@@ -24,6 +24,10 @@
2424
#define PF_EXITING 0x00000004
2525
#define CLOCK_MONOTONIC 1
2626

27+
extern int LINUX_KERNEL_VERSION __kconfig;
28+
extern const char CONFIG_CC_VERSION_TEXT[64] __kconfig __weak;
29+
extern const char CONFIG_LOCALVERSION[64] __kconfig __weak;
30+
2731
/*
2832
* Earlier versions of clang/pahole lost upper 32bits in 64bit enums which can
2933
* lead to really confusing misbehaviors. Let's trigger a build failure.
@@ -98,7 +102,7 @@ void ___scx_bpf_bstr_format_checker(const char *fmt, ...) {}
98102
_Pragma("GCC diagnostic push") \
99103
_Pragma("GCC diagnostic ignored \"-Wint-conversion\"") \
100104
___bpf_fill(___param, args); \
101-
_Pragma("GCC diagnostic pop") \
105+
_Pragma("GCC diagnostic pop")
102106

103107
/*
104108
* scx_bpf_exit() wraps the scx_bpf_exit_bstr() kfunc with variadic arguments
@@ -136,6 +140,20 @@ void ___scx_bpf_bstr_format_checker(const char *fmt, ...) {}
136140
___scx_bpf_bstr_format_checker(fmt, ##args); \
137141
})
138142

143+
/*
144+
* scx_bpf_dump_header() is a wrapper around scx_bpf_dump that adds a header
145+
* of system information for debugging.
146+
*/
147+
#define scx_bpf_dump_header() \
148+
({ \
149+
scx_bpf_dump("kernel: %d.%d.%d %s\ncc: %s\n", \
150+
LINUX_KERNEL_VERSION >> 16, \
151+
LINUX_KERNEL_VERSION >> 8 & 0xFF, \
152+
LINUX_KERNEL_VERSION & 0xFF, \
153+
CONFIG_LOCALVERSION, \
154+
CONFIG_CC_VERSION_TEXT); \
155+
})
156+
139157
#define BPF_STRUCT_OPS(name, args...) \
140158
SEC("struct_ops/"#name) \
141159
BPF_PROG(name, ##args)
@@ -317,6 +335,66 @@ u32 bpf_cpumask_any_and_distribute(const struct cpumask *src1,
317335
const struct cpumask *src2) __ksym;
318336
u32 bpf_cpumask_weight(const struct cpumask *cpumask) __ksym;
319337

338+
int bpf_iter_bits_new(struct bpf_iter_bits *it, const u64 *unsafe_ptr__ign, u32 nr_words) __ksym;
339+
int *bpf_iter_bits_next(struct bpf_iter_bits *it) __ksym;
340+
void bpf_iter_bits_destroy(struct bpf_iter_bits *it) __ksym;
341+
342+
#define def_iter_struct(name) \
343+
struct bpf_iter_##name { \
344+
struct bpf_iter_bits it; \
345+
const struct cpumask *bitmap; \
346+
};
347+
348+
#define def_iter_new(name) \
349+
static inline int bpf_iter_##name##_new( \
350+
struct bpf_iter_##name *it, const u64 *unsafe_ptr__ign, u32 nr_words) \
351+
{ \
352+
it->bitmap = scx_bpf_get_##name##_cpumask(); \
353+
return bpf_iter_bits_new(&it->it, (const u64 *)it->bitmap, \
354+
sizeof(struct cpumask) / 8); \
355+
}
356+
357+
#define def_iter_next(name) \
358+
static inline int *bpf_iter_##name##_next(struct bpf_iter_##name *it) { \
359+
return bpf_iter_bits_next(&it->it); \
360+
}
361+
362+
#define def_iter_destroy(name) \
363+
static inline void bpf_iter_##name##_destroy(struct bpf_iter_##name *it) { \
364+
scx_bpf_put_cpumask(it->bitmap); \
365+
bpf_iter_bits_destroy(&it->it); \
366+
}
367+
#define def_for_each_cpu(cpu, name) for_each_##name##_cpu(cpu)
368+
369+
/// Provides iterator for possible and online cpus.
370+
///
371+
/// # Example
372+
///
373+
/// ```
374+
/// static inline void example_use() {
375+
/// int *cpu;
376+
///
377+
/// for_each_possible_cpu(cpu){
378+
/// bpf_printk("CPU %d is possible", *cpu);
379+
/// }
380+
///
381+
/// for_each_online_cpu(cpu){
382+
/// bpf_printk("CPU %d is online", *cpu);
383+
/// }
384+
/// }
385+
/// ```
386+
def_iter_struct(possible);
387+
def_iter_new(possible);
388+
def_iter_next(possible);
389+
def_iter_destroy(possible);
390+
#define for_each_possible_cpu(cpu) bpf_for_each(possible, cpu, NULL, 0)
391+
392+
def_iter_struct(online);
393+
def_iter_new(online);
394+
def_iter_next(online);
395+
def_iter_destroy(online);
396+
#define for_each_online_cpu(cpu) bpf_for_each(online, cpu, NULL, 0)
397+
320398
/*
321399
* Access a cpumask in read-only mode (typically to check bits).
322400
*/
@@ -423,5 +501,6 @@ static inline u32 log2_u64(u64 v)
423501
}
424502

425503
#include "compat.bpf.h"
504+
#include "enums.bpf.h"
426505

427506
#endif /* __SCX_COMMON_BPF_H */

tools/sched_ext/include/scx/common.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,5 +71,11 @@ typedef int64_t s64;
7171

7272
#include "user_exit_info.h"
7373
#include "compat.h"
74+
#include "enums.h"
75+
76+
/* not available when building kernel tools/sched_ext */
77+
#if __has_include(<lib/sdt_task.h>)
78+
#include <lib/sdt_task.h>
79+
#endif
7480

7581
#endif /* __SCHED_EXT_COMMON_H */

tools/sched_ext/include/scx/compat.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,7 @@ static inline long scx_hotplug_seq(void)
149149
__skel = __scx_name##__open(); \
150150
SCX_BUG_ON(!__skel, "Could not open " #__scx_name); \
151151
__skel->struct_ops.__ops_name->hotplug_seq = scx_hotplug_seq(); \
152+
SCX_ENUM_INIT(__skel); \
152153
__skel; \
153154
})
154155

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
/*
2+
* WARNING: This file is autogenerated from scripts/gen_enums.py. If you would
3+
* like to access an enum that is currently missing, add it to the script
4+
* and run it from the root directory to update this file.
5+
*/
6+
7+
const volatile u64 __SCX_OPS_NAME_LEN __weak;
8+
#define SCX_OPS_NAME_LEN __SCX_OPS_NAME_LEN
9+
10+
const volatile u64 __SCX_SLICE_DFL __weak;
11+
#define SCX_SLICE_DFL __SCX_SLICE_DFL
12+
13+
const volatile u64 __SCX_SLICE_INF __weak;
14+
#define SCX_SLICE_INF __SCX_SLICE_INF
15+
16+
const volatile u64 __SCX_DSQ_FLAG_BUILTIN __weak;
17+
#define SCX_DSQ_FLAG_BUILTIN __SCX_DSQ_FLAG_BUILTIN
18+
19+
const volatile u64 __SCX_DSQ_FLAG_LOCAL_ON __weak;
20+
#define SCX_DSQ_FLAG_LOCAL_ON __SCX_DSQ_FLAG_LOCAL_ON
21+
22+
const volatile u64 __SCX_DSQ_INVALID __weak;
23+
#define SCX_DSQ_INVALID __SCX_DSQ_INVALID
24+
25+
const volatile u64 __SCX_DSQ_GLOBAL __weak;
26+
#define SCX_DSQ_GLOBAL __SCX_DSQ_GLOBAL
27+
28+
const volatile u64 __SCX_DSQ_LOCAL __weak;
29+
#define SCX_DSQ_LOCAL __SCX_DSQ_LOCAL
30+
31+
const volatile u64 __SCX_DSQ_LOCAL_ON __weak;
32+
#define SCX_DSQ_LOCAL_ON __SCX_DSQ_LOCAL_ON
33+
34+
const volatile u64 __SCX_DSQ_LOCAL_CPU_MASK __weak;
35+
#define SCX_DSQ_LOCAL_CPU_MASK __SCX_DSQ_LOCAL_CPU_MASK
36+
37+
const volatile u64 __SCX_TASK_QUEUED __weak;
38+
#define SCX_TASK_QUEUED __SCX_TASK_QUEUED
39+
40+
const volatile u64 __SCX_TASK_RESET_RUNNABLE_AT __weak;
41+
#define SCX_TASK_RESET_RUNNABLE_AT __SCX_TASK_RESET_RUNNABLE_AT
42+
43+
const volatile u64 __SCX_TASK_DEQD_FOR_SLEEP __weak;
44+
#define SCX_TASK_DEQD_FOR_SLEEP __SCX_TASK_DEQD_FOR_SLEEP
45+
46+
const volatile u64 __SCX_TASK_STATE_SHIFT __weak;
47+
#define SCX_TASK_STATE_SHIFT __SCX_TASK_STATE_SHIFT
48+
49+
const volatile u64 __SCX_TASK_STATE_BITS __weak;
50+
#define SCX_TASK_STATE_BITS __SCX_TASK_STATE_BITS
51+
52+
const volatile u64 __SCX_TASK_STATE_MASK __weak;
53+
#define SCX_TASK_STATE_MASK __SCX_TASK_STATE_MASK
54+
55+
const volatile u64 __SCX_TASK_CURSOR __weak;
56+
#define SCX_TASK_CURSOR __SCX_TASK_CURSOR
57+
58+
const volatile u64 __SCX_TASK_NONE __weak;
59+
#define SCX_TASK_NONE __SCX_TASK_NONE
60+
61+
const volatile u64 __SCX_TASK_INIT __weak;
62+
#define SCX_TASK_INIT __SCX_TASK_INIT
63+
64+
const volatile u64 __SCX_TASK_READY __weak;
65+
#define SCX_TASK_READY __SCX_TASK_READY
66+
67+
const volatile u64 __SCX_TASK_ENABLED __weak;
68+
#define SCX_TASK_ENABLED __SCX_TASK_ENABLED
69+
70+
const volatile u64 __SCX_TASK_NR_STATES __weak;
71+
#define SCX_TASK_NR_STATES __SCX_TASK_NR_STATES
72+
73+
const volatile u64 __SCX_TASK_DSQ_ON_PRIQ __weak;
74+
#define SCX_TASK_DSQ_ON_PRIQ __SCX_TASK_DSQ_ON_PRIQ
75+
76+
const volatile u64 __SCX_KICK_IDLE __weak;
77+
#define SCX_KICK_IDLE __SCX_KICK_IDLE
78+
79+
const volatile u64 __SCX_KICK_PREEMPT __weak;
80+
#define SCX_KICK_PREEMPT __SCX_KICK_PREEMPT
81+
82+
const volatile u64 __SCX_KICK_WAIT __weak;
83+
#define SCX_KICK_WAIT __SCX_KICK_WAIT
84+
85+
const volatile u64 __SCX_ENQ_WAKEUP __weak;
86+
#define SCX_ENQ_WAKEUP __SCX_ENQ_WAKEUP
87+
88+
const volatile u64 __SCX_ENQ_HEAD __weak;
89+
#define SCX_ENQ_HEAD __SCX_ENQ_HEAD
90+
91+
const volatile u64 __SCX_ENQ_PREEMPT __weak;
92+
#define SCX_ENQ_PREEMPT __SCX_ENQ_PREEMPT
93+
94+
const volatile u64 __SCX_ENQ_REENQ __weak;
95+
#define SCX_ENQ_REENQ __SCX_ENQ_REENQ
96+
97+
const volatile u64 __SCX_ENQ_LAST __weak;
98+
#define SCX_ENQ_LAST __SCX_ENQ_LAST
99+
100+
const volatile u64 __SCX_ENQ_CLEAR_OPSS __weak;
101+
#define SCX_ENQ_CLEAR_OPSS __SCX_ENQ_CLEAR_OPSS
102+
103+
const volatile u64 __SCX_ENQ_DSQ_PRIQ __weak;
104+
#define SCX_ENQ_DSQ_PRIQ __SCX_ENQ_DSQ_PRIQ
105+
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/*
2+
* WARNING: This file is autogenerated from scripts/gen_enums.py. If you would
3+
* like to access an enum that is currently missing, add it to the script
4+
* and run it from the root directory to update this file.
5+
*/
6+
7+
#define SCX_ENUM_INIT(skel) do { \
8+
SCX_ENUM_SET(skel, scx_public_consts, SCX_OPS_NAME_LEN); \
9+
SCX_ENUM_SET(skel, scx_public_consts, SCX_SLICE_DFL); \
10+
SCX_ENUM_SET(skel, scx_public_consts, SCX_SLICE_INF); \
11+
SCX_ENUM_SET(skel, scx_dsq_id_flags, SCX_DSQ_FLAG_BUILTIN); \
12+
SCX_ENUM_SET(skel, scx_dsq_id_flags, SCX_DSQ_FLAG_LOCAL_ON); \
13+
SCX_ENUM_SET(skel, scx_dsq_id_flags, SCX_DSQ_INVALID); \
14+
SCX_ENUM_SET(skel, scx_dsq_id_flags, SCX_DSQ_GLOBAL); \
15+
SCX_ENUM_SET(skel, scx_dsq_id_flags, SCX_DSQ_LOCAL); \
16+
SCX_ENUM_SET(skel, scx_dsq_id_flags, SCX_DSQ_LOCAL_ON); \
17+
SCX_ENUM_SET(skel, scx_dsq_id_flags, SCX_DSQ_LOCAL_CPU_MASK); \
18+
SCX_ENUM_SET(skel, scx_ent_flags, SCX_TASK_QUEUED); \
19+
SCX_ENUM_SET(skel, scx_ent_flags, SCX_TASK_RESET_RUNNABLE_AT); \
20+
SCX_ENUM_SET(skel, scx_ent_flags, SCX_TASK_DEQD_FOR_SLEEP); \
21+
SCX_ENUM_SET(skel, scx_ent_flags, SCX_TASK_STATE_SHIFT); \
22+
SCX_ENUM_SET(skel, scx_ent_flags, SCX_TASK_STATE_BITS); \
23+
SCX_ENUM_SET(skel, scx_ent_flags, SCX_TASK_STATE_MASK); \
24+
SCX_ENUM_SET(skel, scx_ent_flags, SCX_TASK_CURSOR); \
25+
SCX_ENUM_SET(skel, scx_task_state, SCX_TASK_NONE); \
26+
SCX_ENUM_SET(skel, scx_task_state, SCX_TASK_INIT); \
27+
SCX_ENUM_SET(skel, scx_task_state, SCX_TASK_READY); \
28+
SCX_ENUM_SET(skel, scx_task_state, SCX_TASK_ENABLED); \
29+
SCX_ENUM_SET(skel, scx_task_state, SCX_TASK_NR_STATES); \
30+
SCX_ENUM_SET(skel, scx_ent_dsq_flags, SCX_TASK_DSQ_ON_PRIQ); \
31+
SCX_ENUM_SET(skel, scx_kick_flags, SCX_KICK_IDLE); \
32+
SCX_ENUM_SET(skel, scx_kick_flags, SCX_KICK_PREEMPT); \
33+
SCX_ENUM_SET(skel, scx_kick_flags, SCX_KICK_WAIT); \
34+
SCX_ENUM_SET(skel, scx_enq_flags, SCX_ENQ_WAKEUP); \
35+
SCX_ENUM_SET(skel, scx_enq_flags, SCX_ENQ_HEAD); \
36+
SCX_ENUM_SET(skel, scx_enq_flags, SCX_ENQ_PREEMPT); \
37+
SCX_ENUM_SET(skel, scx_enq_flags, SCX_ENQ_REENQ); \
38+
SCX_ENUM_SET(skel, scx_enq_flags, SCX_ENQ_LAST); \
39+
SCX_ENUM_SET(skel, scx_enq_flags, SCX_ENQ_CLEAR_OPSS); \
40+
SCX_ENUM_SET(skel, scx_enq_flags, SCX_ENQ_DSQ_PRIQ); \
41+
} while (0)
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
/* SPDX-License-Identifier: GPL-2.0 */
2+
/*
3+
* Convenience macros for getting/setting struct scx_enums instances.
4+
*
5+
* Copyright (c) 2024 Meta Platforms, Inc. and affiliates.
6+
*/
7+
#ifndef __SCX_ENUMS_BPF_H
8+
#define __SCX_ENUMS_BPF_H
9+
10+
#include "enums.autogen.bpf.h"
11+
12+
#endif /* __SCX_ENUMS_BPF_H */

tools/sched_ext/include/scx/enums.h

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/* SPDX-License-Identifier: GPL-2.0 */
2+
/*
3+
* Define struct scx_enums that stores the load-time values of enums
4+
* used by the BPF program.
5+
*
6+
* Copyright (c) 2024 Meta Platforms, Inc. and affiliates.
7+
*/
8+
9+
#ifndef __SCX_ENUMS_H
10+
#define __SCX_ENUMS_H
11+
12+
static inline void __ENUM_set(u64 *val, char *type, char *name)
13+
{
14+
bool res;
15+
16+
res = __COMPAT_read_enum(type, name, val);
17+
SCX_BUG_ON(!res, "enum not found(%s)", name);
18+
}
19+
20+
#define SCX_ENUM_SET(skel, type, name) do { \
21+
__ENUM_set(&skel->rodata->__##name, #type, #name); \
22+
} while (0)
23+
24+
25+
#include "enums.autogen.h"
26+
27+
#endif /* __SCX_ENUMS_H */

tools/sched_ext/include/scx/user_exit_info.h

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,11 @@
1010
#ifndef __USER_EXIT_INFO_H
1111
#define __USER_EXIT_INFO_H
1212

13+
#ifdef LSP
14+
#define __bpf__
15+
#include "../vmlinux.h"
16+
#endif
17+
1318
enum uei_sizes {
1419
UEI_REASON_LEN = 128,
1520
UEI_MSG_LEN = 1024,
@@ -25,9 +30,7 @@ struct user_exit_info {
2530

2631
#ifdef __bpf__
2732

28-
#ifdef LSP
29-
#include "../vmlinux/vmlinux.h"
30-
#else
33+
#ifndef LSP
3134
#include "vmlinux.h"
3235
#endif
3336
#include <bpf/bpf_core_read.h>

tools/sched_ext/scx_central.bpf.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ enum {
5757

5858
const volatile s32 central_cpu;
5959
const volatile u32 nr_cpu_ids = 1; /* !0 for veristat, set during init */
60-
const volatile u64 slice_ns = SCX_SLICE_DFL;
60+
const volatile u64 slice_ns;
6161

6262
bool timer_pinned = true;
6363
u64 nr_total, nr_locals, nr_queued, nr_lost_pids;

tools/sched_ext/scx_central.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ int main(int argc, char **argv)
5858

5959
skel->rodata->central_cpu = 0;
6060
skel->rodata->nr_cpu_ids = libbpf_num_possible_cpus();
61+
skel->rodata->slice_ns = __COMPAT_ENUM_OR_ZERO("scx_public_consts", "SCX_SLICE_DFL");
6162

6263
while ((opt = getopt(argc, argv, "s:c:pvh")) != -1) {
6364
switch (opt) {

0 commit comments

Comments
 (0)