Skip to content

mitosis: skip foreign cgroup hierarchies in tp_cgroup_mkdir - #3606

Open
likewhatevs wants to merge 2 commits into
sched-ext:mainfrom
likewhatevs:mitosis-cgrp-ctx-lookup-extra-mkdir-repro
Open

mitosis: skip foreign cgroup hierarchies in tp_cgroup_mkdir#3606
likewhatevs wants to merge 2 commits into
sched-ext:mainfrom
likewhatevs:mitosis-cgrp-ctx-lookup-extra-mkdir-repro

Conversation

@likewhatevs

Copy link
Copy Markdown
Contributor

tp_btf/cgroup_mkdir fires for every cgroup created system-wide, not just cgroups in --cell-parent-cgroup and not just cgroupv2. When a cgroup is created in any foreign cgroup hierarchy (cgroupv1 named hierarchy, cgroupv1 subsystem mount, or any non-default cgroup hierarchy), init_cgrp_ctx_with_ancestors walks the foreign-hierarchy ancestor chain. The first level-1-ancestor init_cgrp_ctx call hits lookup_cgrp_ctx(parent_cg) where parent_cg is the foreign hierarchy's root cgroup. That root has no cgrp_ctx storage — mitosis_init only initialized the cgroupv2 default root via bpf_cgroup_from_id(root_cgid=1), which searches cgrp_dfl_root.kf_root — so the non-fallible lookup_cgrp_ctx wrapper bails:

scx_bpf_error (./mitosis.bpf.c:149: cgrp_ctx lookup failed for cgid 1)

Two commits:

  1. Test — ktstr test that mounts per-worker cgroupv1 named hierarchies and churns level-2 mkdirs. Fires the bail reliably within ~2 s with the for-realsies backtrace.
  2. Fix — skip path in tp_cgroup_mkdir that compares cgrp's top-of-hierarchy cgroup pointer (via bpf_cgroup_ancestor(cgrp, 0)) against root_cgrp's; skips init_cgrp_ctx_with_ancestors entirely when they differ. Gated behind a new --fix-ignore-cg-v1 CLI flag (off by default, plumbed through rodata.fix_ignore_cg_v1) so the bail remains observable until the operator opts in.

Test plan

  • cargo build --tests --features ktstr-tests builds clean
  • Reproducer with default sched_args: fails at ~2 s with scx_bpf_error (mitosis.bpf.c:149: cgrp_ctx lookup failed for cgid 1)
  • Reproducer with --fix-ignore-cg-v1 added to sched_args: passes at ~34 s

For-realsies crash within ms of scheduler attach on a host with
cgroup activity outside the configured --cell-parent-cgroup
subtree:

  scx_bpf_error (./mitosis.bpf.c:148: cgrp_ctx lookup failed for
  cgid 1)

  Backtrace:
    bpf_prog_init_cgrp_ctx
    bpf_prog_init_cgrp_ctx_with_ancestors
    bpf_prog_tp_cgroup_mkdir
    bpf_trace_run2
    cgroup_mkdir

mitosis_init only initializes cgrp_ctx storage for the cgroupv2
default root via bpf_cgroup_from_id(root_cgid=1), which resolves
through cgrp_dfl_root.kf_root. When any cgroup is created in a
cgroupv1 named hierarchy (or any other non-default cgroup
hierarchy), the v1 root cgroup also reports kn->id = 1 (first
allocated in its own kernfs_root's idr) but its cgrp_ctx was
never initialized — it is a different cgroup struct than the v2
root.

tp_cgroup_mkdir fires for the v1 child. init_cgrp_ctx_with_ancestors
walks the chain. The level-1 ancestor's init_cgrp_ctx calls
lookup_cgrp_ctx(parent_cg) at mitosis.bpf.c:1510 where parent_cg
is the v1 root. Lookup returns NULL and the non-fallible wrapper
bails the scheduler at mitosis.bpf.c:148.

Reproducer mounts a per-worker named v1 hierarchy and mkdir+rmdir
level-2 cgroups in it. The bail fires reliably within ~2 s of
scheduler attach with the for-realsies backtrace.

Signed-off-by: Pat Somaru <patso@likewhatevs.io>
tp_btf/cgroup_mkdir fires for every cgroup created system-wide,
not just cgroups in the configured --cell-parent-cgroup subtree
and not just cgroupv2. When any cgroup is created in a foreign
cgroup hierarchy (cgroupv1 named hierarchy, cgroupv1 subsystem
mount, or any other non-default cgroup hierarchy), the handler's
init_cgrp_ctx_with_ancestors walks the foreign-hierarchy ancestor
chain. The walk's first level-1-ancestor init_cgrp_ctx call hits
lookup_cgrp_ctx(parent_cg) where parent_cg is the foreign
hierarchy's root cgroup. That root has no cgrp_ctx storage —
mitosis_init only initialized the cgroupv2 default root via
bpf_cgroup_from_id(root_cgid=1), which searches
cgrp_dfl_root.kf_root — so the non-fallible lookup_cgrp_ctx
wrapper bails the scheduler:

  scx_bpf_error (./mitosis.bpf.c:148: cgrp_ctx lookup failed for
  cgid 1)

mitosis already ignores foreign hierarchies everywhere else: the
mitosis_init descendants walk iterates only descendants of
root_cgrp; mitosis_init_task reads task_cgroup() which returns
the v2 unified cgroup; the SCX cgroup struct_ops callbacks only
fire for v2. tp_cgroup_mkdir is the lone path that sees foreign
cgroups, so the skip is local to this handler: when cgrp's
top-of-hierarchy struct pointer differs from root_cgrp's, skip
init_cgrp_ctx_with_ancestors entirely. The check uses
bpf_cgroup_ancestor(cgrp, 0) to get the actual struct cgroup of
cgrp's hierarchy root and compares pointer equality against an
explicit bpf_cgroup_acquire(root_cgrp) inside an rcu-guarded
block.

Gated behind a new --fix-ignore-cg-v1 CLI flag (off by default,
plumbed through rodata.fix_ignore_cg_v1) so the for-realsies bail
is still observable until the operator opts in. With the flag set
ktstr_mitosis_cgrp_ctx_lookup_v1_root passes; without it the test
still fails with the bail backtrace, demonstrating the bug.

Signed-off-by: Pat Somaru <patso@likewhatevs.io>
@likewhatevs
likewhatevs force-pushed the mitosis-cgrp-ctx-lookup-extra-mkdir-repro branch from b89de5d to f6550f2 Compare May 28, 2026 05:32
@likewhatevs likewhatevs changed the title scx_mitosis: skip foreign cgroup hierarchies in tp_cgroup_mkdir mitosis: skip foreign cgroup hierarchies in tp_cgroup_mkdir May 28, 2026
@likewhatevs
likewhatevs requested a review from hodgesds June 2, 2026 15:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant