Skip to content

Commit

Permalink
scx_rustland_core: keep CPUs alive with pending tasks
Browse files Browse the repository at this point in the history
Prevent CPUs from going idle when the user-space scheduler has some
pending activities to complete.

Keeping the CPU alive allows to consume tasks from the user-space
scheduler more efficiently, preventing bubbles in the scheduling
pipeline.

To achieve this, trigger a CPU kick from ops.update_idle() and set a
flag in the CPU context to prevent it from going idle. Then keep kicking
the CPU from ops.dispatch() until the flag is cleared, which occurs when
no more tasks are pending or when the CPU exits idle as a task starts
running on it.

This allows to fix the performance regression introduced by the
put_prev_task_scx() behavior change in Linux 6.12 (see #788).

Link: https://lore.kernel.org/lkml/[email protected]/
Signed-off-by: Andrea Righi <[email protected]>
  • Loading branch information
arighi committed Oct 15, 2024
1 parent abfb4c5 commit 40ccca8
Showing 1 changed file with 43 additions and 3 deletions.
46 changes: 43 additions & 3 deletions rust/scx_rustland_core/assets/bpf/main.bpf.c
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,15 @@ struct {
struct cpu_ctx {
struct bpf_cpumask __kptr *l2_cpumask;
struct bpf_cpumask __kptr *l3_cpumask;
/*
* Prevent the CPU from going idle.
*
* This is set when the CPU has no task to run, but the user-space
* scheduler has still some pending activities to process. Keeping the
* CPU alive allows to consume tasks from the user-space scheduler more
* efficiently, preventing bubbles in the scheduling pipeline.
*/
bool prevent_idle;
};

struct {
Expand Down Expand Up @@ -926,6 +935,8 @@ static long handle_dispatched_task(struct bpf_dynptr *dynptr, void *context)
*/
void BPF_STRUCT_OPS(rustland_dispatch, s32 cpu, struct task_struct *prev)
{
struct cpu_ctx *cctx;

/*
* Consume all tasks from the @dispatched list and immediately dispatch
* them on the target CPU decided by the user-space scheduler.
Expand All @@ -946,7 +957,18 @@ void BPF_STRUCT_OPS(rustland_dispatch, s32 cpu, struct task_struct *prev)
/*
* Consume a task from the shared DSQ.
*/
scx_bpf_consume(SHARED_DSQ);
if (scx_bpf_consume(SHARED_DSQ))
return;

/*
* No more tasks to process, check if we need to keep the CPU alive to
* process pending tasks from the user-space scheduler.
*/
cctx = try_lookup_cpu_ctx(cpu);
if (!cctx)
return;
if (cctx->prevent_idle)
scx_bpf_kick_cpu(cpu, SCX_KICK_IDLE);
}

/*
Expand Down Expand Up @@ -992,12 +1014,21 @@ void BPF_STRUCT_OPS(rustland_stopping, struct task_struct *p, bool runnable)
*/
void BPF_STRUCT_OPS(rustland_update_idle, s32 cpu, bool idle)
{
struct cpu_ctx *cctx;

cctx = try_lookup_cpu_ctx(cpu);
if (!cctx)
return;

/*
* Don't do anything if we exit from and idle state, a CPU owner will
* be assigned in .running().
*/
if (!idle)
if (!idle) {
cctx->prevent_idle = false;
return;
}

/*
* A CPU is now available, notify the user-space scheduler that tasks
* can be dispatched.
Expand All @@ -1008,6 +1039,7 @@ void BPF_STRUCT_OPS(rustland_update_idle, s32 cpu, bool idle)
* Wake up the idle CPU and trigger a resched, so that it can
* immediately accept dispatched tasks.
*/
cctx->prevent_idle = true;
scx_bpf_kick_cpu(cpu, 0);
return;
}
Expand All @@ -1016,8 +1048,16 @@ void BPF_STRUCT_OPS(rustland_update_idle, s32 cpu, bool idle)
* Kick the CPU if there are still tasks dispatched to the
* corresponding per-CPU DSQ.
*/
if (scx_bpf_dsq_nr_queued(cpu_to_dsq(cpu)) > 0)
if (scx_bpf_dsq_nr_queued(cpu_to_dsq(cpu)) > 0) {
cctx->prevent_idle = true;
scx_bpf_kick_cpu(cpu, 0);
return;
}

/*
* Nothing to do: allow the CPU to go idle.
*/
cctx->prevent_idle = false;
}

/*
Expand Down

0 comments on commit 40ccca8

Please sign in to comment.