Scheduler fixes six rseq concurrency ID bugs, one a use-after-free
Failed execve and affinity changes could hand threads a bogus ID, lock up small machines, or panic the kernel.
Mathieu Desnoyers has posted fixes for six bugs in the Linux scheduler's per-process concurrency IDs, the numbers restartable sequences use so threads in one address space can index shared data without a lock. All six are marked for stable kernels. New selftests cover cases the existing suite never reached: thread counts that rise and fall, a failed execve, and affinity changes made by another process.
The IDs are supposed to stay compact and below the number of CPUs a process may run on. Several of the bugs show up when a process is the last user of its memory map, leaves it, and joins again, which is what a failed execve does. In one case the returning thread came back to user space with the sentinel value 0x80000000 instead of a real ID. On a machine with two or three CPUs, stale per-CPU state left behind by the previous users could lock the machine up after that failed execve. A related window let a running task spin while it still held the lock that protects ID assignment.
Affinity changes during execve were dropped when they landed before the first user had joined the new memory map. A process that started on one CPU and was widened to many CPUs in that window could then switch into per-CPU mode and hand its two threads IDs such as 0 and 63: still in range, but no longer compact. Desnoyers also stops io_uring and vhost worker tasks from taking IDs that belong to the process's own threads.
The most serious fix closes a use-after-free that Weiming Shi reported. Changing another task's CPU affinity reads that task's memory map while holding its scheduler lock, but the lock did not keep the pointer stable across an exec that replaces and frees the old map. On architectures that tear down lazy translation state instead of holding a reference, the same gap exists when a task exits. Shi's report shows the kernel panicking under KASAN when one task loops on execve while others call sched_setaffinity on it. The fix publishes the new pointer, then takes and drops the same lock before the old map is freed, so an updater still using the old pointer has finished.
The new tests fail on an unfixed kernel for the execve, lockup, affinity, and teardown cases. A separate case only prints when IDs miss the compactness target after affinity is widened; it fails only if an ID is out of range.