#include <scx/common.bpf.h>
#include "scx_userland.h"
#define MAX_ENQUEUED_TASKS 4096
char _license[] SEC("license") = "GPL";
const volatile s32 usersched_pid;
const volatile u32 num_possible_cpus = 64;
u64 nr_failed_enqueues, nr_kernel_enqueues, nr_user_enqueues;
volatile u64 nr_queued;
volatile u64 nr_scheduled;
UEI_DEFINE(uei);
struct {
__uint(type, BPF_MAP_TYPE_QUEUE);
__uint(max_entries, MAX_ENQUEUED_TASKS);
__type(value, struct scx_userland_enqueued_task);
} enqueued SEC(".maps");
struct {
__uint(type, BPF_MAP_TYPE_QUEUE);
__uint(max_entries, MAX_ENQUEUED_TASKS);
__type(value, s32);
} dispatched SEC(".maps");
struct task_ctx {
bool force_local;
};
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");
static volatile u32 usersched_needed;
static void set_usersched_needed(void)
{
__sync_fetch_and_or(&usersched_needed, 1);
}
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);
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)
{
struct task_struct *p;
p = usersched_task();
if (p) {
scx_bpf_dsq_insert(p, SCX_DSQ_GLOBAL, SCX_SLICE_DFL, 0);
bpf_task_release(p);
}
}
static void enqueue_task_in_user_space(struct task_struct *p, u64 enq_flags)
{
struct scx_userland_enqueued_task task = {};
task.pid = p->pid;
task.sum_exec_runtime = p->se.sum_exec_runtime;
task.weight = p->scx.weight;
if (bpf_map_push_elem(&enqueued, &task, 0)) {
__sync_fetch_and_add(&nr_failed_enqueues, 1);
scx_bpf_dsq_insert(p, SCX_DSQ_GLOBAL, SCX_SLICE_DFL, enq_flags);
} else {
__sync_fetch_and_add(&nr_user_enqueues, 1);
set_usersched_needed();
}
}
void BPF_STRUCT_OPS(userland_enqueue, struct task_struct *p, u64 enq_flags)
{
if (keep_in_kernel(p)) {
u64 dsq_id = SCX_DSQ_GLOBAL;
struct task_ctx *tctx;
tctx = bpf_task_storage_get(&task_ctx_stor, p, 0, 0);
if (!tctx) {
scx_bpf_error("Failed to lookup task ctx for %s", p->comm);
return;
}
if (tctx->force_local)
dsq_id = SCX_DSQ_LOCAL;
tctx->force_local = false;
scx_bpf_dsq_insert(p, dsq_id, SCX_SLICE_DFL, enq_flags);
__sync_fetch_and_add(&nr_kernel_enqueues, 1);
return;
} else if (!is_usersched_task(p)) {
enqueue_task_in_user_space(p, enq_flags);
}
}
void BPF_STRUCT_OPS(userland_dispatch, s32 cpu, struct task_struct *prev)
{
if (test_and_clear_usersched_needed())
dispatch_user_scheduler();
bpf_repeat(MAX_ENQUEUED_TASKS) {
s32 pid;
struct task_struct *p;
if (bpf_map_pop_elem(&dispatched, &pid))
break;
p = bpf_task_from_pid(pid);
if (!p)
continue;
scx_bpf_dsq_insert(p, SCX_DSQ_GLOBAL, SCX_SLICE_DFL, 0);
bpf_task_release(p);
}
}
void BPF_STRUCT_OPS(userland_update_idle, s32 cpu, bool idle)
{
if (!idle)
return;
if (nr_queued || nr_scheduled) {
set_usersched_needed();
scx_bpf_kick_cpu(cpu, 0);
}
}
s32 BPF_STRUCT_OPS(userland_init_task, struct task_struct *p,
struct scx_init_task_args *args)
{
if (bpf_task_storage_get(&task_ctx_stor, p, 0,
BPF_LOCAL_STORAGE_GET_F_CREATE))
return 0;
else
return -ENOMEM;
}
s32 BPF_STRUCT_OPS(userland_init)
{
if (num_possible_cpus == 0) {
scx_bpf_error("User scheduler # CPUs uninitialized (%d)",
num_possible_cpus);
return -EINVAL;
}
if (usersched_pid <= 0) {
scx_bpf_error("User scheduler pid uninitialized (%d)",
usersched_pid);
return -EINVAL;
}
return 0;
}
void BPF_STRUCT_OPS(userland_exit, struct scx_exit_info *ei)
{
UEI_RECORD(uei, ei);
}
SCX_OPS_DEFINE(userland_ops,
.select_cpu = (void *)userland_select_cpu,
.enqueue = (void *)userland_enqueue,
.dispatch = (void *)userland_dispatch,
.update_idle = (void *)userland_update_idle,
.init_task = (void *)userland_init_task,
.init = (void *)userland_init,
.exit = (void *)userland_exit,
.flags = SCX_OPS_ENQ_LAST |
SCX_OPS_KEEP_BUILTIN_IDLE,
.name = "userland");