From: Zi Yan <ziy@nvidia.com>
To: Nilay Vaish <nilayvaish@google.com>
Cc: Andrew Morton <akpm@linux-foundation.org>,
Balbir Singh <balbirs@nvidia.com>,
David Hildenbrand <david@kernel.org>,
Zenghui Yu <zenghui.yu@linux.dev>, Jason Gunthorpe <jgg@ziepe.ca>,
Leon Romanovsky <leon@kernel.org>,
Alistair Popple <apopple@nvidia.com>,
Suren Baghdasaryan <surenb@google.com>, <linux-mm@kvack.org>,
<linux-kernel@vger.kernel.org>
Subject: Re: [PATCH] lib/test_hmm: fix alloc tag leak when splitting device-private folios
Date: Thu, 19 Nov 2026 10:38:08 -0500 [thread overview]
Message-ID: <72E0E689-0FA4-4DB9-B978-711EF6DEC490@nvidia.com> (raw)
In-Reply-To: <1CB074CC-E9E3-436D-8D04-8C203C825E82@nvidia.com>
On 18 Nov 2026, at 18:24, Zi Yan wrote:
> On 18 Nov 2026, at 18:00, Nilay Vaish wrote:
>
>> For device-private memory, test_hmm backs each fake device folio with
>> real system RAM of the same order, allocated with folio_alloc() in
>> dmirror_devmem_alloc_page().
>>
>> When the core MM splits a large device-private folio, the driver's
>> ->folio_split() callback, dmirror_devmem_folio_split(), has to split
>> the backing memory as well. It does that by hand: clear_compound_head()
>> on the tail pages, folio_reset_order() on the head page, and
>> folio_set_count() on each of them. dmirror_devmem_free() then frees
>> every backing page as an order-0 page.
>>
>> This open-coded split does not split the allocator's metadata. The
>> allocation tag and the page_owner record still describe one high-order
>> allocation, and PG_head stays set on the head page. When the pages are
>> freed one by one, the tag is debited for only one page when the head is
>> freed, and the tail pages carry no tag at all. With
>> CONFIG_MEM_ALLOC_PROFILING, running the HMM selftests and unloading the
>> module reports the leak:
>>
>> lib/test_hmm.c:808 module test_hmm func:dmirror_devmem_alloc_page has 16744448 allocated at module unload
>>
>> and with CONFIG_MEM_ALLOC_PROFILING_DEBUG freeing a tail page triggers:
>>
>> alloc_tag was not set
>> WARNING: ./include/linux/alloc_tag.h:164 at ___free_pages+0x2a0/0x2d0
>>
>> The backing memory is a private buffer of the test driver and nothing
>> requires it to be a folio. Allocate it with alloc_pages() instead and
>> split it right away with split_page(), which splits the allocation tag,
>> page_owner and memcg state and is already exported to modules. It is
>> still allocated as one high-order block rather than as individual
>> order-0 pages because the driver finds the backing page of the Nth page
>> of a device folio by offsetting from the first backing page, so the
>> backing pages have to be physically contiguous. Every backing page is
>> then an independent order-0 page for its whole lifetime, so
>> dmirror_devmem_folio_split() only has to point the new device folio at
>> its backing page, and dmirror_devmem_free() frees the backing pages one
>> at a time.
>>
>> As the backing pages no longer form a folio, take the order from the
>> device folio rather than from the backing page, and index and copy the
>> backing pages individually instead of using folio_page() and
>> folio_copy().
>>
>> With this patch the device-private HMM selftests pass and the module
>> unloads without the messages above.
>>
>> Fixes: 56ef39899643 ("mm/memremap: add driver callback support for folio splitting")
>> Reported-by: Zenghui Yu <zenghui.yu@linux.dev>
>> Closes: https://lore.kernel.org/linux-mm/be9a8bae-9223-4966-bba9-2cbe39c8f4de@linux.dev/
>> Suggested-by: David Hildenbrand (Arm) <david@kernel.org>
>> Link: https://lore.kernel.org/linux-mm/8ef6e92d-4cb3-444e-857e-fdbf88c035ea@kernel.org/
>> Signed-off-by: Nilay Vaish <nilayvaish@google.com>
>> ---
>> lib/test_hmm.c | 41 +++++++++++++++++------------------------
>> 1 file changed, 17 insertions(+), 24 deletions(-)
>
>
>>
>> diff --git a/lib/test_hmm.c b/lib/test_hmm.c
>> index b409c42bfe6f..371ead009f8a 100644
>> --- a/lib/test_hmm.c
>> +++ b/lib/test_hmm.c
>> @@ -201,6 +201,7 @@ static void dmirror_device_evict_chunk(struct dmirror_chunk *chunk)
>> migrate_device_range(src_pfns, start_pfn, npages);
>> for (i = 0; i < npages; i++) {
>> struct page *dpage, *spage;
>> + unsigned int j;
>>
>> spage = migrate_pfn_to_page(src_pfns[i]);
>> if (!spage || !(src_pfns[i] & MIGRATE_PFN_MIGRATE))
>> @@ -227,7 +228,9 @@ static void dmirror_device_evict_chunk(struct dmirror_chunk *chunk)
>> dst_pfns[i] |= MIGRATE_PFN_WRITE;
>> if (order)
>> dst_pfns[i] |= MIGRATE_PFN_COMPOUND;
>> - folio_copy(page_folio(dpage), page_folio(spage));
>> + for (j = 0; j < (1U << order); j++)
>> + copy_highpage(pfn_to_page(page_to_pfn(dpage) + j),
>> + pfn_to_page(page_to_pfn(spage) + j));
>
> struct page should be contiguous if their PFNs are contiguous.
> There is no need to do this conversion, right?
>
> In addition, dpage, based on the comment before BACKING_PAGE,
> is device memory, so it should also be allocated using alloc_pages()
> like rpage. Am I getting it wrong?
I misread the comment, dpage is destination page. It can be ZONE_DEVICE
page or system memory depending on functions.
Best Regards,
Yan, Zi
next prev parent reply other threads:[~2026-11-19 15:38 UTC|newest]
Thread overview: 4+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-18 23:00 Nilay Vaish
2026-11-18 23:24 ` Zi Yan
2026-11-19 15:38 ` Zi Yan [this message]
2026-11-19 10:53 ` Zenghui Yu
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=72E0E689-0FA4-4DB9-B978-711EF6DEC490@nvidia.com \
--to=ziy@nvidia.com \
--cc=akpm@linux-foundation.org \
--cc=apopple@nvidia.com \
--cc=balbirs@nvidia.com \
--cc=david@kernel.org \
--cc=jgg@ziepe.ca \
--cc=leon@kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-mm@kvack.org \
--cc=nilayvaish@google.com \
--cc=surenb@google.com \
--cc=zenghui.yu@linux.dev \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®