freenode
Kernel & Low-Level

Linux defers memory teardown to stop a BPF iterator deadlock

Walking another task's memory maps from softirq could re-enter a page-table lock already held on x86.

A Linux memory-management fix queued for a hotfix release closes a deadlock that can hang a CPU when BPF walks another task's virtual memory areas from a context that must not sleep.

The task VMA iterator was switched to per-VMA locks so it could run without the mmap lock, including from softirq. That path was never guaranteed to be safe outside process context. Dropping the last reference to a process memory descriptor can, on x86, free its page tables and take a global page-table lock. If a softirq runs the iterator while that lock is already held, the same CPU tries to take it again and deadlocks.

Alexei Starovoitov identified a second route to the same hang: a BPF program attached to the kernel's lock-contention tracepoint can re-enter the iterator while the page-table lock is still held in the slow path of that lock.

Lorenzo Stoakes fixed it by deferring the memory-descriptor drop to a work queue, so the non-sleepable path no longer takes the lock. The asynchronous drop helper is being kept internal to the memory-management code so other callers do not pick it up casually. Andrew Morton has added the change to the -mm hotfix tree.