Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

scx_rustland_core: avoid critical failures due by missing task context #605

Merged
merged 1 commit into from
Sep 5, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 22 additions & 18 deletions rust/scx_rustland_core/assets/bpf/main.bpf.c
Original file line number Diff line number Diff line change
Expand Up @@ -221,16 +221,17 @@ struct {
__type(value, struct task_ctx);
} task_ctx_stor SEC(".maps");

/* Return a local task context from a generic task */
struct task_ctx *lookup_task_ctx(const struct task_struct *p)
/*
* Return a local task context from a generic task or NULL if the context
* doesn't exist.
*/
struct task_ctx *try_lookup_task_ctx(const struct task_struct *p)
{
struct task_ctx *tctx;

tctx = bpf_task_storage_get(&task_ctx_stor, (struct task_struct *)p, 0, 0);
if (!tctx) {
scx_bpf_error("Failed to lookup task ctx for %s", p->comm);
return NULL;
}
struct task_ctx *tctx = bpf_task_storage_get(&task_ctx_stor,
(struct task_struct *)p, 0, 0);
if (!tctx)
dbg_msg("warning: failed to get task context for pid=%d (%s)",
p->pid, p->comm);
return tctx;
}

Expand Down Expand Up @@ -357,7 +358,7 @@ static inline u64 task_slice(struct task_struct *p)
{
struct task_ctx *tctx;

tctx = lookup_task_ctx(p);
tctx = try_lookup_task_ctx(p);
if (!tctx || !tctx->slice_ns)
return SCX_SLICE_DFL;
return tctx->slice_ns;
Expand All @@ -378,9 +379,15 @@ static void dispatch_task(struct task_struct *p, u64 dsq_id,
/*
* Update task's time slice in its context.
*/
tctx = lookup_task_ctx(p);
if (!tctx)
tctx = try_lookup_task_ctx(p);
if (!tctx) {
/*
* Bounce to the shared DSQ if we can't find a valid task
* context.
*/
scx_bpf_dispatch_vtime(p, dsq_id, SCX_SLICE_DFL, vtime, 0);
return;
}
tctx->slice_ns = slice;

/*
Expand Down Expand Up @@ -611,16 +618,13 @@ int rs_select_cpu(struct task_cpu_arg *input)
static void
get_task_info(struct queued_task_ctx *task, const struct task_struct *p)
{
struct task_ctx *tctx;
struct task_ctx *tctx = try_lookup_task_ctx(p);

tctx = lookup_task_ctx(p);
if (!tctx)
return;
task->pid = p->pid;
task->cpumask_cnt = tctx->cpumask_cnt;
task->sum_exec_runtime = p->se.sum_exec_runtime;
task->weight = p->scx.weight;
task->cpu = scx_bpf_task_cpu(p);
task->cpumask_cnt = tctx ? tctx->cpumask_cnt : 0;
}

/*
Expand Down Expand Up @@ -899,7 +903,7 @@ void BPF_STRUCT_OPS(rustland_set_cpumask, struct task_struct *p,
{
struct task_ctx *tctx;

tctx = lookup_task_ctx(p);
tctx = try_lookup_task_ctx(p);
if (!tctx)
return;
tctx->cpumask_cnt++;
Expand Down
Loading