Re: [PATCH v4 1/7] mm/zone_device: Add order argument to folio_free callback

Jason Gunthorpe
2026-01-12T23:33:03Z

On Mon, Jan 12, 2026 at 06:15:26PM -0500, Zi Yan wrote:
> > We could move the call to free_zone_device_folio_prepare() [1] into the
> > driver-side implementation of ->folio_free() and drop the order argument
> > here. Zi didn’t particularly like that; he preferred calling
> > free_zone_device_folio_prepare() [2] before invoking ->folio_free(),
> > which is why this patch exists.
>
> On a second thought, if calling free_zone_device_folio_prepare() in
> ->folio_free() works, feel free to do so.

I don't think there is anything "prepare" about
free_zone_device_folio_prepare() it effectively zeros the struct page
memory - ie undoes some amount of zone_device_page_init() and AFAIK
there are only two reasons to do this:

1) It helps catch bugs where things are UAF'ing the folio, now they
read back zeros (it also creates bugs where zero might be OK, so
you might be better to poison it under a debug flag)

2) It avoids the allocate side having to zero the page memory - and
perhaps the allocate side is not doing a good job of this right now
but I think you should state a position why it makes more sense for
the free side to do this instead of the allocate side.

IOW why should it be mandatory to call
free_zone_device_folio_prepare() prior to zone_device_page_init()
?

Certainly if the only reason you are passing the order is because the
core code zero'd the order too early, that doesn't make alot of sense.

I think calling the deinit function paired with
zone_device_page_init() within the driver does make alot of sense and
I see no issue with that. But please name it more sensibly and
describe concretely why it should be split up like this.

Because what I see is you write to all the folios on free and then
write to them all again on allocation - which is 2x the cost that is
probably really needed...

Jason