Skip to content

scx_eevdf: Back with scx_arena - #3929

Open
etsal wants to merge 5 commits into
sched-ext:mainfrom
etsal:scx-arena-parity-eevdf
Open

etsal wants to merge 5 commits into
sched-ext:mainfrom
etsal:scx-arena-parity-eevdf

Conversation

@etsal

@etsal etsal commented Oct 23, 2026

Copy link
Copy Markdown
Contributor

Back the scx_eevdf scheduler with the scx_arena package. This delegates lib/ dependency management to Cargo, incidentally making the scheduler self-contained and publishable by removing the symbolic link to lib/. The patch first syncs scx_arena with recent changes to lib/, then adjusts scx_eevdf to import C dependencies through the scx_arena package.

@etsal
etsal requested a review from arighi October 23, 2026 21:13
@etsal

etsal commented Oct 23, 2026

Copy link
Copy Markdown
Contributor Author

Checked that the scheduler loads for a stock 7.2, I didn't try any possible extra flags though.

@sirlucjan

Copy link
Copy Markdown
Collaborator
❯ sudo ./scx_eevdf 
[sudo: authenticate] Password: 
23:33:15 [INFO] kernel features: arena_fetch_bitops=no prog_stream_fds=no
23:33:20 [INFO] scx_eevdf 1.1.4 | 16 CPUs | 1 NUMA node | SMT on
23:33:20 [INFO] build               1.1.4 x86_64-unknown-linux-gnu
23:33:20 [INFO] kernel              7.2.10-1-cachyos
23:33:20 [INFO] tick                1000 us (HZ=1000)
23:33:20 [INFO] slice               2800 us (700 us scaled as update_sysctl() does)
23:33:20 [INFO] options             
23:33:20 [INFO] cgroup scheduling   on (cgroup_* callbacks)
23:33:20 [INFO] service clock       rq_clock_task
23:33:20 [INFO] CPU capacity        kernel (1 tier, tolerance 0%)
23:33:20 [INFO] proxy execution     off (not supported by the kernel)
23:33:20 [INFO] scheduler flags     0x136
23:33:20 [INFO] domain spans        (fork, wake-affine, asym-capacity, source=schedstat): [(16, 16, 0)]
23:33:20 [INFO] asymmetric packing  kernel (7 priority tiers, CPUs [1, 5, 9, 13, 7, 15, 4, 12, 3, 11, 6, 14, 2, 10, 0, 8])
23:33:20 [INFO] scx_eevdf scheduler attached and running

Works pretty good!

@arighi arighi left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@etsal thanks for working on this! I still need to do a detailed review (will do tomorrow).

Few comments before I forget:

  • The arena page counters with --stats are wrong with libarena. The fexit/fentry probes on bpf_arena_alloc_pages/bpf_arena_free_pages count the buddy allocator's reservation drops as frees. arena_buddy_reset unreserves the whole 1 GiB range, and each new chunk drops its reservation before it is mapped. Without memlock accounting (e.g., on a 7.2 kernel) the reported usage is 0 B. Kernels that account arena pages in memlock report the right size and never read these counters. Rather than teaching the probes about the libarena layout, I think it's easier to just drop them.
  • Can we drop the lib/ copies that are now orphaned? After this PR, nothing in the tree builds lib/edq.bpf.c, scheds/include/lib/edq.h, scheds/include/lib/cid_idle.h or scheds/include/lib/cid_topology.h.
  • Task context size: struct task_ctx is 368 bytes. The buddy allocator rounds it up to 512, where the slab allocator used 368. Not a blocker, just noting it and I'm not sure whether it might introduce regressions or not.

@etsal

etsal commented Oct 23, 2026

Copy link
Copy Markdown
Contributor Author

Hi Andrea, thanks for the feedback!

  1. Let's drop them. The other way would be to also measure reserved address space, but if we don't have a use for the counters I'll remove them in the next version.
  2. Sounds great, that way we also retain the history of the files. I'll update to directly move the files instead of copying them. This may break bisection for a single commit (I could first copy the files, update scx_eevdf to point to scx_arena, then delete the originals, but again that would lose history).
  3. I'll work on this, this is bound to be a problem as we get more complex BPF programs up and running.

@etsal

etsal commented Oct 27, 2026

Copy link
Copy Markdown
Contributor Author

Rebased and dealt with points 1 and 2 above.

@etsal
etsal force-pushed the scx-arena-parity-eevdf branch from 928ddc2 to aa63a11 Compare October 29, 2026 16:08
return -ENOMEM;
tctx = scx_alloc(&task_ctx_allocator);
if (!tctx)
tctx = arena_calloc(1, sizeof(struct task_ctx));

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This loses the bounded page-allocation retries provided by the old allocator. libarena's buddy_chunk_get() calls bpf_arena_alloc_pages() once when growing the pool and IIRC that kfunc can fail transiently under contention even from a sleepable callback (unless it's now fixed in newer kernels), so this can now reject a fork despite available memory.

Can we carry the retry handling into libarena so everything is still working also with old kernels?

Moreover, there is also a potential allocation-latency regression: the buddy allocator scans its chunk list under a global IRQ-disabled lock. Have you compared fork/exit throughput and tail latency with a large resident task population and concurrent task creation?

@etsal etsal Nov 3, 2026 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The issue is fixed for newer kernels. For libarena we can add limited in the GH compat branch, though when I tested it it unfortunately did not alleviate the problem. I will test with eevdf specifically.

Regarding latency: I'm running some experiments to see overall performance, but no concrete numbers yet. If it becomes a problem we can adjust the chunk size (with some adjustments), and once we have percpu caching we should be going to the allocator itself relatively infrequently.

return;
}
scx_free(&task_ctx_allocator, tctx);
arena_free(tctx);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the buddy lock acquisition fails, the context remains allocated, but we decrement arena_active_allocs and keep running.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point, I will adjust to check the return value.

@etsal
etsal force-pushed the scx-arena-parity-eevdf branch 2 times, most recently from fe93ef3 to 6703c56 Compare November 21, 2026 00:12
The legacy B-tree is used only by its own selftest, and the libarena
suite already exercises the packaged implementation. Remove the legacy
source, header, and selftest wiring.
External schedulers need shared CID and deadline queue code without
maintaining a lib/ checkout. Include these inputs in the packaged BPF
assets together with the spinlock and cleanup helpers.

The imported CID idle header uses libarena's common header to share
the existing arena map. EDQ embeds nodes in task contexts, so callers
must disable preemption and hold task references across queue handoffs.
Import the red-black EDQ implementation to match main and bound the
verifier cost of balancing.

Source updates carried by the imported helpers:
- c9b90b3 ("scheds/include: Sync with kernel sched_ext/for-7.4
  (c6fe97c34a1a)")
- f41991b ("scx_eevdf, lib/cid: Drop the unused CID idle word count")
- 12242c9 ("lib/cid: Split idle masks by topology segment")
- bf12c29 ("lib/cid: Rearm idle CIDs only from idle dispatch")
- 7a2f1a7 ("lib/cid: Share summary-driven idle scans and init")
- d84b8fc ("lib/cid: Add cluster idle scopes and splits")
- 81a6508 ("scx: Add scx_spin_lock(), which aborts the scheduler on a
  failed acquire")
- 7e6ef66 ("scx: Add scx_spin_lock_irqsave() and its guard")
- a9344c9 ("lib/edq: Switch to an augmented red-black tree")
Deeper buddy allocator paths can push the packaged arena selftests
past the verifier's call-frame and stack limits. Inline the buddy
header lookup and selected topology, B-tree, and selftest helpers to
keep those paths within both limits.
Packaged arena queues and bandwidth code missed fixes applied to the
shared lib/ copies. Sync them so Cargo consumers get the same lock
failure handling and cgroup-root semantics.

Retain libarena allocation and ATQ's embedded rbtree nodes when
translating the changes. Apply the fatal lock policy to the
libarena-specific removal error path as well. Use shared cacheline
definitions and put packaged headers first in the library selftests.

Source patches:
- c9b90b3 ("scheds/include: Sync with kernel sched_ext/for-7.4
  (c6fe97c34a1a)")
- 81a6508 ("scx: Add scx_spin_lock(), which aborts the scheduler on a
  failed acquire")
- c59aa57 ("lib/cgroup_bw: Let the library be rooted at a given
  cgroup")

Already mirrored upstream:
- ba842bd ("scx: centralize div_round_up in common BPF header")
- 172fb11 ("lib: Free stray pages in the arena NULL guard range before
  reserving it")
@etsal
etsal force-pushed the scx-arena-parity-eevdf branch from 6703c56 to b806ec3 Compare November 21, 2026 00:41
scx_eevdf links shared BPF sources through paths into this repository,
which makes an independent checkout depend on a separate copy of lib/.
Use the packaged scx_arena inputs and its libarena allocator so Cargo
can supply the shared code.

Initialize the buddy allocator before allocating the CID tables. Its
syscall program takes no context, so pass none for a zero-sized argument.
Remove the loose CID and EDQ copies after switching their last consumer
to the package.

Use kernel arena map accounting for memory usage. libarena drops virtual
address reservations through bpf_arena_free_pages(), so page probes count
unmapped reservations as frees and cannot measure allocated memory. Show
usage as unavailable where kernel accounting is absent. Keep the task
context allocation counters in the scheduler because libarena does not
expose the legacy allocator's statistics.
@etsal
etsal force-pushed the scx-arena-parity-eevdf branch from b806ec3 to e3c8e6e Compare November 23, 2026 19:51

@arighi arighi left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Left some comments and there are some conflicts with the latest main (but the conflicts should be easy to fix). Thanks!

context_in: Some(&mut args),
..Default::default()
},
)?;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we clear the prefix before reserving it here? The old guard setup freed these pages first, otherwise reading /proc/PID/mem can potentially fault them in before initialization and reservation then fails with -EBUSY.

if (!tctx) {
__sync_fetch_and_add(&arena_alloc_failures, 1);
return -ENOMEM;
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This still loses the bounded page-allocation retries provided by the old allocator. In libarena 0.3.4, buddy_chunk_get() calls bpf_arena_alloc_pages() only once when growing the pool.

On older kernels affected by the transient allocation failures under contention, this can reject a fork despite available memory. The new per-CPU caches reduce how often pool growth happens, but don't handle that failure, if I'm not missing anything.

In that case, do you think it makes sense to add bounded retries around the backing-page allocation in libarena's buddy_chunk_get(), as the old arena_alloc_pages_retry() did?

.context("missing arena globals")?;
let arena_globals = std::ptr::from_ref(arena_globals).addr();
ScxLibArena::init_buddy(skel.object(), arena_globals)
.context("initializing libarena buddy allocator")?;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Libarena 0.3.4 initializes per-CPU caches for all 8192 CPU slots, regardless of the machine's CPU count. Each cache is 1600 bytes, rounded to a 2048-byte buddy allocation. That consumes 16 MiB for cache objects alone, before chunk metadata and scheduler state, even on a small machine.

Can we pass *NR_CPU_IDS into buddy initialization and initialize only that range of per-CPU caches, instead of all 8192 slots?

build->node_pool = bpf_arena_alloc_pages(&arena, NULL, pages, node, 0);
build->node_pool = arena_calloc(1, bytes);
if (!build->node_pool)
return -ENOMEM;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This loses the NUMA placement of the idle-tracking pool. Previously, scx_cid_idle_init_node() passed __COMPAT_scx_bpf_cid_node(cid) through to bpf_arena_alloc_pages(). Libarena allocates its backing pages with NUMA_NO_NODE, so the pool is no longer explicitly allocated on the node whose idle state it tracks.

These structures are accessed during CPU selection and idle updates (hot paths). Can we preserve node-aware allocation here?

} else {
None
};
.filter(|&bytes| bytes > 0);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The old probes counted released virtual-address reservations as freed physical memory, which could incorrectly report 0 B. Dropping them avoids that, but removes usage reporting on older kernels, even with --stats. Can we use libarena's buddy.mapped_bytes as a fallback? It would need to be labeled as allocator backing memory, since it excludes arena globals and any physically populated prefix pages.

Not a blocker, this is just an optional observability improvement.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants