tools/sched_ext/scx_userland.bpf.c
Source file repositories/reference/linux-study-clean/tools/sched_ext/scx_userland.bpf.c
File Facts
- System
- Linux kernel
- Corpus path
tools/sched_ext/scx_userland.bpf.c- Extension
.c- Size
- 9521 bytes
- Lines
- 345
- Domain
- Support Tooling And Documentation
- Bucket
- tools
- Inferred role
- Support Tooling And Documentation: implementation source
- Status
- source implementation candidate
Why This File Exists
Repository support layer: documentation, build tooling, samples, user-space helper tools, generated initramfs support, licenses, and validation utilities.
- Repository support layer: documentation, build tooling, samples, user-space helper tools, generated initramfs support, licenses, and validation utilities.
- Defines or uses C structs; map object ownership, embedded links, reference counts, and lock ownership.
Dependency Surface
scx/common.bpf.hscx_userland.h
Detected Declarations
struct task_ctxfunction set_usersched_neededfunction test_and_clear_usersched_neededfunction is_usersched_taskfunction keep_in_kernelfunction BPF_STRUCT_OPSfunction dispatch_user_schedulerfunction enqueue_task_in_user_spacefunction BPF_STRUCT_OPSfunction BPF_STRUCT_OPSfunction bpf_repeatfunction BPF_STRUCT_OPSfunction update_idlefunction BPF_STRUCT_OPSfunction BPF_STRUCT_OPSfunction BPF_STRUCT_OPS
Annotated Snippet
struct task_ctx {
bool force_local; /* Dispatch directly to local DSQ */
};
/* Map that contains task-local storage. */
struct {
__uint(type, BPF_MAP_TYPE_TASK_STORAGE);
__uint(map_flags, BPF_F_NO_PREALLOC);
__type(key, int);
__type(value, struct task_ctx);
} task_ctx_stor SEC(".maps");
/*
* Flag used to wake-up the user-space scheduler.
*/
static volatile u32 usersched_needed;
/*
* Set user-space scheduler wake-up flag (equivalent to an atomic release
* operation).
*/
static void set_usersched_needed(void)
{
__sync_fetch_and_or(&usersched_needed, 1);
}
/*
* Check and clear user-space scheduler wake-up flag (equivalent to an atomic
* acquire operation).
*/
static bool test_and_clear_usersched_needed(void)
{
return __sync_fetch_and_and(&usersched_needed, 0) == 1;
}
static bool is_usersched_task(const struct task_struct *p)
{
return p->pid == usersched_pid;
}
static bool keep_in_kernel(const struct task_struct *p)
{
return p->nr_cpus_allowed < num_possible_cpus;
}
static struct task_struct *usersched_task(void)
{
struct task_struct *p;
p = bpf_task_from_pid(usersched_pid);
/*
* Should never happen -- the usersched task should always be managed
* by sched_ext.
*/
if (!p)
scx_bpf_error("Failed to find usersched task %d", usersched_pid);
return p;
}
s32 BPF_STRUCT_OPS(userland_select_cpu, struct task_struct *p,
s32 prev_cpu, u64 wake_flags)
{
if (keep_in_kernel(p)) {
s32 cpu;
struct task_ctx *tctx;
tctx = bpf_task_storage_get(&task_ctx_stor, p, 0, 0);
if (!tctx) {
scx_bpf_error("Failed to look up task-local storage for %s", p->comm);
return -ESRCH;
}
if (p->nr_cpus_allowed == 1 ||
scx_bpf_test_and_clear_cpu_idle(prev_cpu)) {
tctx->force_local = true;
return prev_cpu;
}
cpu = scx_bpf_pick_idle_cpu(p->cpus_ptr, 0);
if (cpu >= 0) {
tctx->force_local = true;
return cpu;
}
}
return prev_cpu;
}
static void dispatch_user_scheduler(void)
Annotation
- Immediate include surface: `scx/common.bpf.h`, `scx_userland.h`.
- Detected declarations: `struct task_ctx`, `function set_usersched_needed`, `function test_and_clear_usersched_needed`, `function is_usersched_task`, `function keep_in_kernel`, `function BPF_STRUCT_OPS`, `function dispatch_user_scheduler`, `function enqueue_task_in_user_space`, `function BPF_STRUCT_OPS`, `function BPF_STRUCT_OPS`.
- Atlas domain: Support Tooling And Documentation / tools.
- Implementation status: source implementation candidate.
Implementation Notes
- This generated page is the file-by-file coverage layer; curated subsystem chapters should link here when they synthesize a multi-file control flow.
- Core OS pages should be promoted from atlas-only to deep-reviewed when they explain data structures, invariants, locking, lifecycle, and C implementation snippets.
- Driver-family pages are intentionally pattern-oriented unless they are part of the selected PCIe/NVMe representative device path.