Re: [PATCH v12 09/10] mm/vmalloc: map contiguous pages in batches for vmap() if possible

From: Wen Jiang

Date: Wed Dec 02 2026 - 04:32:52 EST


On Wed, 2 Dec 2026 at 17:13, David Hildenbrand (Arm) <david@xxxxxxxxxx> wrote:
>
> On 11/7/26 14:49, Wen Jiang wrote:
> > From: "Barry Song (Xiaomi)" <baohua@xxxxxxxxxx>
> >
> > In many cases, the pages passed to vmap() may include high-order
> > pages. For example, the systemheap often allocates pages in descending
> > order: order 8, then 4, then 0. Currently, vmap() iterates over every
> > page individually—even pages inside a high-order block are handled
> > one by one.
> >
> > This patch detects physically contiguous pages (regardless of whether
> > they are compound or non-compound) by scanning with
> > num_pages_contiguous(), and maps them as a single contiguous block
> > whenever possible. The mapping order is determined by taking the
> > minimum of the contiguous page count and the pfn alignment, allowing
> > graceful degradation when pfn alignment is less than the contiguous
> > range.
> >
> > Pages with the same page_shift are coalesced and mapped via
> > vmap_pages_range_noflush_walk() to avoid page table rewalk.
> >
> > As users typically allocate memory in descending orders (e.g.
> > 8 → 4 → 0), once an order-0 page is encountered, we stop scanning
> > for contiguous pages since subsequent pages are likely order-0 as well.
> >
> > With large mappings, vmap_try_huge_pmd() may free an empty PTE table
> > left behind by an earlier vunmap() and install a PMD while vread_iter()
> > walks the same range without the mmap lock. Set VM_UNINITIALIZED on the
> > area until the mapping is complete, as __vmalloc_node_range_noprof()
> > already does, so that vread_iter() skips it.
> >
> > Signed-off-by: Barry Song (Xiaomi) <baohua@xxxxxxxxxx>
> > Co-developed-by: Dev Jain <dev.jain@xxxxxxx>
> > Signed-off-by: Dev Jain <dev.jain@xxxxxxx>
> > Signed-off-by: Wen Jiang <jiangwen6@xxxxxxxxxx>
> > Tested-by: Xueyuan Chen <xueyuan.chen21@xxxxxxxxx>
> > Tested-by: Leo Yan <leo.yan@xxxxxxx>
> > Reviewed-by: Uladzislau Rezki (Sony) <urezki@xxxxxxxxx>
> > ---
>
> Apparently this thing here fixes an issue:
>
> When CONFIG_PROC_FS and CONFIG_NUMA are enabled, a race between vmap()
> called with VM_MAP_PUT_PAGES and a concurrent /proc/vmallocinfo read can
> cause a NULL pointer dereference in show_numa_info():
> CPU 0 CPU 1 (/proc/vmallocinfo)
> ----- -------------------------
> vmap(..., VM_MAP_PUT_PAGES, ...)
> area = get_vm_area_caller(...)
> alloc_vmap_area()
> // publishes va->vm into
> // vn->busy.head without
> // VM_UNINITIALIZED
> vmap_pages_range_batched(...)
> area->pages = pages;
> area->nr_pages = count;
> show_busy_info()
> spin_lock(&vn->busy.lock);
> // v->flags lacks VM_UNINITIALIZED
> smp_rmb();
> show_numa_info(m, v, counters)
> for (i = 0; i < v->nr_pages; ...)
> v->pages[i] // NULL dereference
> In vmap(), get_vm_area_caller() allocates a zero-initialized vm_struct
> without setting VM_UNINITIALIZED in area->flags and links it into
> vn->busy.head via alloc_vmap_area(). When VM_MAP_PUT_PAGES is set, vmap()
> subsequently assigns area->pages and area->nr_pages without holding
> vn->busy.lock, without using VM_UNINITIALIZED and
> clear_vm_uninitialized_flag(), and without a write memory barrier ordering
> the two stores. When show_busy_info() concurrently traverses vn->busy.head
> under vn->busy.lock, it sees VM_UNINITIALIZED clear on v->flags and invokes
> show_numa_info(). Because the stores to area->pages and area->nr_pages are
> unserialized and unordered, compiler or CPU reordering allows
> show_numa_info() to observe a non-zero v->nr_pages while v->pages is still
> NULL, crashing the kernel when dereferencing v->pages[i].
>
> --
> Cheers,
>
> David

Hi David,

Thanks, the analysis is right. This patch sets VM_UNINITIALIZED on the
area until the mapping is complete, so show_busy_info() skips it the
same way vread_iter() does, and clear_vm_uninitialized_flag() orders
the stores to area->pages and area->nr_pages before the flag is
cleared. So this race is covered as well.

Thanks,
Wen