Repository navigation
Conversation
|
Checked that the scheduler loads for a stock 7.2, I didn't try any possible extra flags though. |
Works pretty good! |
arighi
left a comment
There was a problem hiding this comment.
@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.
|
Hi Andrea, thanks for the feedback!
|
086a4cf to
aa63a11
Compare
|
Rebased and dealt with points 1 and 2 above. |
928ddc2 to
aa63a11
Compare
| return -ENOMEM; | ||
| tctx = scx_alloc(&task_ctx_allocator); | ||
| if (!tctx) | ||
| tctx = arena_calloc(1, sizeof(struct task_ctx)); |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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); |
There was a problem hiding this comment.
If the buddy lock acquisition fails, the context remains allocated, but we decrement arena_active_allocs and keep running.
There was a problem hiding this comment.
Good point, I will adjust to check the return value.
fe93ef3 to
6703c56
Compare
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")
6703c56 to
b806ec3
Compare
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.
b806ec3 to
e3c8e6e
Compare
arighi
left a comment
There was a problem hiding this comment.
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() | ||
| }, | ||
| )?; |
There was a problem hiding this comment.
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; | ||
| } |
There was a problem hiding this comment.
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")?; |
There was a problem hiding this comment.
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; |
There was a problem hiding this comment.
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); |
There was a problem hiding this comment.
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.
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.