Re: [PATCH V3] mm: fix use-after free of page_ext after race with memory-offline

From: Matthew Wilcox
Date: Mon Aug 15 2022 - 11:27:04 EST


On Mon, Aug 15, 2022 at 05:06:18PM +0200, Michal Hocko wrote:
> > + * This function gives proper page_ext of a memory section
> > + * during race with the offline operation on a memory block
> > + * this section falls into. Not using this function to get
> > + * page_ext of a page, in code paths where extra refcount
> > + * is not taken on that page eg: pfn walking, can lead to
> > + * use-after-free access of page_ext.
>
> I do not think this is really useful comment, it goes into way too much
> detail about memory hotplug yet not enough to actually understand the
> interaction because there are no references to the actual
> synchronization scheme. I would go with something like:
>
> /*
> * Get a page_ext associated with the given page. Returns NULL if
> * no such page_ext exists otherwise ensures that the page_ext will
> * stay alive until page_ext_put is called.
> * This implies a non-sleeping context.
> */

I'd go further and turn this into kernel-doc:

/**
* page_ext_get() - Get the extended information for a page.
* @page: The page we're interested in.
*
* Ensures that the page_ext will remain valid until page_ext_put()
* is called.
*
* Return: NULL if no page_ext exists for this page.
* Context: Any context. Caller may not sleep until they have called
* page_ext_put().
*/