Re: [PATCH] RCU safety for vma maple tree walks
From: Pedro Falcato
Date: Mon Aug 31 2026 - 13:04:35 EST
On Mon, Aug 31, 2026 at 07:35:11AM -0700, Andi Kleen wrote:
> I ran into the following scenario in a slightly modified kernel:
>
> 1. vms_gather_munmap_vmas walks the unmap range and caches a maple node N
> in the maple iterator.
> 2. It triggers a __split_vma to fix up a range and during that node N
> is queued for freeing with kfree_rcu
> 3. There is another __split_vma that allocates memory and sleeps due to
> memory pressure.
> 4. During the sleep the grace period expires and node N gets freed for
> real.
> 5. The iterator still has node N cached
> 6. When the iteration continues it accesses the freed node and KASAN
> trips:
>
> BUG: KASAN: slab-use-after-free in mas_next_slot+0x1e95/0x2860
> Read of size 8 at addr ffff8881160bdc00 by task pool-e/5770
> CPU: 1 UID: 0 PID: 5770 Comm: pool-e Not tainted 7.2.0-rc7-1-debug+ #106
> Allocated by task 5770:
> mas_alloc_nodes <- mas_preallocate <- __split_vma <- vms_gather_munmap_vmas
> <- do_vmi_align_munmap <- do_vmi_munmap <- __vm_munmap <- elf_load
> <- load_elf_binary <- bprm_execve (pool-e's exec)
> Freed by task 25 (ksoftirqd):
> __rcu_free_sheaf_prepare <- rcu_free_sheaf_nobarn <- rcu_do_batch <- rcu_core
> The buggy address ... cache maple_node of size 256
> freed 256-byte region [ffff8881160bdc00, ffff8881160bdd00)
>
> There was also a more complex scenario when the node was immediately
> recycled for the next split VMA insert, modified, but the access by
> the iterator for the previous walk saw corrupted state and triggered
> KASAN too.
>
> Basically the problem is that any sleeping during VMA walks breaks the
> RCU reader guarantees for the RCU Maple tree iterators. But sleeping
> is unavoidable for various reasons.
>
> I hit it with a kernel modification that makes this more likely
> (It can do VMA splits on exec mm teardown)
> and also in a very memory constrained environment (4GB guest running a
> stress test), but based on code review I believe it's a generic problem that
> could happen in a unmodified kernel.
>
> That said I wasn't actually able to trigger it in a unmodified kernel
> so far with stress testing.
>
> The following old unsolved syzkaller report has a similar signature,
> so maybe it was already seen:
> https://syzkaller.appspot.com/bug?id=4c5268fbb1d6d508a4c34dc425e2693d1ff9911a
>
> I guess in many cases where it happens for real you don't notice it
> if you don't have KASAN active.
>
> The patch fixes up all callers to maintain the RCU reader lock
> regions correctly during the VMA walk. If they cannot be maintained the
> iterator is refreshed by a new VMA address lookup in a new region, unless
> it is proven safe not to.
>
But none of this code uses RCU? I'm confused. The maple tree state should not
be keeping bad state. That is a bug.
All of these functions take the mmap write lock. That should exclude
against other concurrent changes. Using RCU here makes no logical sense.
Does the kernel say anything interesting when CONFIG_DEBUG_VM_MAPLE_TREE=y?
--
Pedro