Re: [PATCH v4 05/16] mm/hugetlb: use direct assignment instead of folio_change_private()
From: David Hildenbrand (Arm)
Date: Wed Sep 16 2026 - 02:16:59 EST
On 9/16/26 03:56, Zi Yan wrote:
> On 13 Sep 2026, at 22:24, Zi Yan wrote:
>
>> folio_change_private() should be used along with folio_attach_private() and
>> folio_detach_private(), where adding and remove ->private content requires
>> folio refcount change. add_hugetlb_folio() simply sets folio->private to
>> NULL without refcount manipulation. Change it to direct assignment to avoid
>> semantic confusion.
>>
>> It prepares for a future commit that remove PG_private.
>>
>> No functional change intended.
>>
>> Assisted-by: LLM
>> To: Muchun Song <muchun.song@xxxxxxxxx>
>> To: Oscar Salvador <osalvador@xxxxxxx>
>> To: Andrew Morton <akpm@xxxxxxxxxxxxxxxxxxxx>
>> Cc: David Hildenbrand <david@xxxxxxxxxx>
>> Cc: linux-mm@xxxxxxxxx
>> Cc: linux-kernel@xxxxxxxxxxxxxxx
>> Acked-by: Usama Arif <usama.arif@xxxxxxxxx>
>> Reviewed-by: Gregory Price (Meta) <gourry@xxxxxxxxxx>
>> Signed-off-by: Zi Yan <ziy@xxxxxxxxxx>
>> ---
>> mm/hugetlb.c | 7 ++-----
>> 1 file changed, 2 insertions(+), 5 deletions(-)
>>
>> diff --git a/mm/hugetlb.c b/mm/hugetlb.c
>> index d3a0650ff6905..8d551cdaef9db 100644
>> --- a/mm/hugetlb.c
>> +++ b/mm/hugetlb.c
>> @@ -1446,11 +1446,8 @@ void add_hugetlb_folio(struct hstate *h, struct folio *folio,
>> }
>>
>> __folio_set_hugetlb(folio);
>> - folio_change_private(folio, NULL);
>> - /*
>> - * We have to set hugetlb_vmemmap_optimized again as above
>> - * folio_change_private(folio, NULL) cleared it.
>> - */
>> + /* Clear all folio->private flags except hugetlb_vmemmap_optimized. */
>> + folio->private = NULL;
>> folio_set_hugetlb_vmemmap_optimized(folio);
>>
>
> Sashiko[1] said when a CMA hugetlb folio is freed but fails vmemmap restore,
> add_hugetlb_folio() clears HPG_cma. It either bypasses
> hugetlb_cma_free_frozen_folio() due to the missing HPG_cma flag, or
> skips cma_release() and causes a CMA memory leak if it is free directly
> to buddy allocator. Sashiko also asked if HPG_temporary should be preserved
> as well HPG_cma.
>
> Answer:
>
> Missing HPG_cma is a real issue. It can be fixed by checking
> folio_test_hugetlb_cma(folio) at the beginning of add_hugetlb_folio() and
> setting HPG_cma after folio->private is assigned to NULL.
Maybe we should just simply only clear flags we actually want to clear ...
--
Cheers,
David