Re: [PATCH v4 5/8] x86/mm: Use pagetable_free()

Jason Gunthorpe
2025-09-05T23:04:26Z

On Fri, Sep 05, 2025 at 01:11:15PM -0700, Dave Hansen wrote:
> On 9/5/25 11:41, Jason Gunthorpe wrote:
> >> --- a/arch/x86/mm/init_64.c
> >> +++ b/arch/x86/mm/init_64.c
> >> @@ -1013,7 +1013,7 @@ static void __meminit free_pagetable(struct page *page, int order)
> >> free_reserved_pages(page, nr_pages);
> >> #endif
> >> } else {
> >> - free_pages((unsigned long)page_address(page), order);
> >> + pagetable_free(page_ptdesc(page));
> >> }
> >> }
> > Er.. So if bootmem happens to be under the table and we happen to free
> > it due to memory hotplug we don't go through the SVA fixing path?
> >
> > Seems wrong??
>
> On second thought...
>
> Yes, freeing bootmem with no SVA fixing is wrong. It should be fixed.
> Period. But, it's wrong one time for something super rare: memory unplug
> of memory that was present at boot. It also can't be triggered by
> unprivileged users.
>
> As-is, this series fixes vfree(). That path is not nearly rare, can
> happen an arbitrary number of times on each boot, and might even be
> triggered by folks that are less than root.
>
> So I kinda think we should just make clear that this series leaves
> _some_ holes, but I do think it should go in mostly as-is.

That's reasonable, but also your suggested change is pretty
simple. I'd put an arch hook:

static inline void pagetable_free_kernel(struct ptdesc *pt)
{
struct page *page = ptdesc_page(pt);

ptdesc_clear_kernel(pt);
if (!arch_pagetable_free_kernel(pt))
return;
__pagetable_free((page);
}

With what you showed

Also, probably need to ensure whatever allocates the bootmem in the
first place calls ptdesc_set_kernel()..

Jason