Allow resuming per-CPU arena selection via thread.arena (#2957)

* Allow resuming per-CPU arena selection via thread.arena

With percpu_arena enabled, thread.arena control is one-directional. A
thread can be bound to a manually created arena (an index at or above the
per-CPU auto range) to route a bounded region of work to a dedicated,
long-lived arena, but there is no way back: thread_arena_ctl returns EPERM
for any index within the auto range, and arena_choose_impl only re-selects
a per-CPU arena for threads whose current arena is already in that range.
So once a thread is bound to a manual arena it stays pinned there forever,
and its later allocations land there instead of following the CPU.

Treat setting thread.arena to an index within the per-CPU range as a
request to resume automatic per-CPU selection: hand the thread back to
percpu management (rebinding it to the current CPU's arena via
percpu_arena_update) and return 0 instead of EPERM. Binding to a manual
arena is unchanged. The requested index is advisory; under percpu the
thread is governed by its current CPU, so it resumes on the current CPU's
arena regardless of the value passed.

Add test/unit/percpu_arena_resume covering the manual-arena to resume
round trip with allocs & dallocs happening meanwhile, and update 
test_thread_arena, which asserted the old EPERM.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
Raúl Marín 2026-07-16 23:53:25 +02:00 committed by GitHub
parent 8361239b03
commit 68c35f6557
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 104 additions and 6 deletions

View file

@ -2374,10 +2374,18 @@ thread_arena_ctl(tsd_t *tsd, const size_t *mib, size_t miblen, void *oldp,
if (have_percpu_arena && PERCPU_ARENA_ENABLED(opt_percpu_arena)) {
if (newind < percpu_arena_ind_limit(opt_percpu_arena)) {
/*
* If perCPU arena is enabled, thread_arena control is
* not allowed for the auto arena range.
* Setting thread.arena to an arena in the auto range
* means "resume automatic per-CPU selection" rather than
* pinning to a specific per-CPU arena. This lets a caller
* that temporarily switched to a manually managed arena
* (e.g. a scoped guard) hand the thread back to per-CPU
* management. It is otherwise impossible: a thread bound
* to a manual arena is never reclaimed by percpu (see
* arena_choose_impl), so without this it would stay
* pinned forever.
*/
return EPERM;
percpu_arena_update(tsd, percpu_arena_choose());
return 0;
}
}