From: Kefeng Wang <wangkefeng.wang@huawei.com>
To: Andrew Morton <akpm@linux-foundation.org>
Cc: David Hildenbrand <david@kernel.org>,
Oscar Salvador <osalvador@suse.de>,
Muchun Song <muchun.song@linux.dev>, <linux-mm@kvack.org>,
<sidhartha.kumar@oracle.com>, <jane.chu@oracle.com>,
Zi Yan <ziy@nvidia.com>, Vlastimil Babka <vbabka@suse.cz>,
Brendan Jackman <jackmanb@google.com>,
Johannes Weiner <hannes@cmpxchg.org>,
Matthew Wilcox <willy@infradead.org>
Subject: Re: [PATCH v2 3/5] mm: hugetlb: optimize replace_free_hugepage_folios()
Date: Fri, 16 Jan 2026 08:58:50 +0800 [thread overview]
Message-ID: <da4d80b5-339a-400b-8e1c-c758ef56c460@huawei.com> (raw)
In-Reply-To: <20260115144228.4b103d0b37b0f3fd017a4d9c@linux-foundation.org>
On 2026/1/16 6:42, Andrew Morton wrote:
> On Wed, 14 Jan 2026 21:55:12 +0800 Kefeng Wang <wangkefeng.wang@huawei.com> wrote:
>
>> If no free hugepage folios are available, there is no need to perform
>> any replacement operations. Additionally, gigantic folios should not
>> be replaced under any circumstances. Therefore, we only check for the
>> presence of non-gigantic folios, also adding the gigantic folio check
>> to avoid accidental replacement.
>>
>> To optimize performance, we skip unnecessary iterations over pfn
>> for compound pages and high-order buddy pages to save processing time.
>>
>> A simple test on machine with 114G free memory, allocate 120 * 1G
>> HugeTLB folios(104 successfully returned),
>>
>> time echo 120 > /sys/kernel/mm/hugepages/hugepages-1048576kB/nr_hugepages
>>
>> Before: 0m0.602s
>> After: 0m0.431s
>>
>
> Thanks, I'll queue this as a -fix, below.
>
> nit: the function has become rather a jumble of bailout styles:
>
> : if (order_is_gigantic(folio_order(folio)))
> : return -ENOMEM;
>
> This could have done ret=-ENOMEM;break;
>
> :
> : ret = alloc_and_dissolve_hugetlb_folio(folio, &list);
> : if (ret)
> : break;
>
> While this could have done `return ret;'!
>
> Single-return-point is preferred style, I think. Mainly to help avoid
> leakage of resources and locks as the code evolves.
>
> How does this look on top?
Hi Andrew, I'm fine with it, thanks your for the update.
>
> From: Andrew Morton <akpm@linux-foundation.org>
> Subject: mm-hugetlb-optimize-replace_free_hugepage_folios-v2-fix
> Date: Thu Jan 15 02:34:07 PM PST 2026
>
> use single-return-point style, tweak comment
>
> Cc: Kefeng Wang <wangkefeng.wang@huawei.com>
> Cc: Brendan Jackman <jackmanb@google.com>
> Cc: David Hildenbrand <david@kernel.org>
> Cc: Jane Chu <jane.chu@oracle.com>
> Cc: Johannes Weiner <hannes@cmpxchg.org>
> Cc: Matthew Wilcox (Oracle) <willy@infradead.org>
> Cc: Muchun Song <muchun.song@linux.dev>
> Cc: Oscar Salvador <osalvador@suse.de>
> Cc: Sidhartha Kumar <sidhartha.kumar@oracle.com>
> Cc: Vlastimil Babka <vbabka@suse.cz>
> Cc: Zi Yan <ziy@nvidia.com>
> Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
> ---
>
> mm/hugetlb.c | 11 ++++++++---
> 1 file changed, 8 insertions(+), 3 deletions(-)
>
> --- a/mm/hugetlb.c~mm-hugetlb-optimize-replace_free_hugepage_folios-v2-fix
> +++ a/mm/hugetlb.c
> @@ -2834,10 +2834,15 @@ int replace_free_hugepage_folios(unsigne
>
> nr = folio_nr_pages(folio) - folio_page_idx(folio, page);
>
> - /* Not to disrupt normal path by vainly holding hugetlb_lock */
> + /*
> + * Don't disrupt normal path by vainly holding
> + * hugetlb_lock
> + */
> if (folio_test_hugetlb(folio) && !folio_ref_count(folio)) {
> - if (order_is_gigantic(folio_order(folio)))
> - return -ENOMEM;
> + if (order_is_gigantic(folio_order(folio))) {
> + ret = -ENOMEM;
> + break;
> + }
>
> ret = alloc_and_dissolve_hugetlb_folio(folio, &list);
> if (ret)
> _
>
>
>
>
>
>
> From: Kefeng Wang <wangkefeng.wang@huawei.com>
> Subject: mm: hugetlb: optimize replace_free_hugepage_folios()
> Date: Wed, 14 Jan 2026 21:55:12 +0800
>
> Turn back to use alloc_and_dissolve_hugetlb_folio() since Oscar pointed
> out that the return value is different. Then adding gigantic folio check
> independently, also update changelog.
>
> Link: https://lkml.kernel.org/r/20260114135512.2159799-1-wangkefeng.wang@huawei.com
> Signed-off-by: Kefeng Wang <wangkefeng.wang@huawei.com>
> Cc: Brendan Jackman <jackmanb@google.com>
> Cc: David Hildenbrand <david@kernel.org>
> Cc: Jane Chu <jane.chu@oracle.com>
> Cc: Johannes Weiner <hannes@cmpxchg.org>
> Cc: Matthew Wilcox (Oracle) <willy@infradead.org>
> Cc: Muchun Song <muchun.song@linux.dev>
> Cc: Oscar Salvador <osalvador@suse.de>
> Cc: Sidhartha Kumar <sidhartha.kumar@oracle.com>
> Cc: Vlastimil Babka <vbabka@suse.cz>
> Cc: Zi Yan <ziy@nvidia.com>
> Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
> ---
>
> mm/hugetlb.c | 10 +++++++---
> 1 file changed, 7 insertions(+), 3 deletions(-)
>
> --- a/mm/hugetlb.c~mm-hugetlb-optimize-replace_free_hugepage_folios-v2
> +++ a/mm/hugetlb.c
> @@ -2810,6 +2810,7 @@ int replace_free_hugepage_folios(unsigne
> struct page *page;
> struct hstate *h;
> LIST_HEAD(list);
> + int ret = 0;
>
> /* Avoid pfn iterations if no free non-gigantic huge pages */
> for_each_hstate(h) {
> @@ -2835,9 +2836,13 @@ int replace_free_hugepage_folios(unsigne
>
> /* Not to disrupt normal path by vainly holding hugetlb_lock */
> if (folio_test_hugetlb(folio) && !folio_ref_count(folio)) {
> - if (isolate_or_dissolve_huge_folio(folio, &list))
> + if (order_is_gigantic(folio_order(folio)))
> return -ENOMEM;
>
> + ret = alloc_and_dissolve_hugetlb_folio(folio, &list);
> + if (ret)
> + break;
> +
> putback_movable_pages(&list);
> }
> } else if (PageBuddy(page)) {
> @@ -2851,11 +2856,10 @@ int replace_free_hugepage_folios(unsigne
> if (order <= MAX_PAGE_ORDER)
> nr = 1UL << order;
> }
> -
> start_pfn += nr;
> }
>
> - return 0;
> + return ret;
> }
>
> void wait_for_freed_hugetlb_folios(void)
> _
>
>
next prev parent reply other threads:[~2026-01-16 0:59 UTC|newest]
Thread overview: 23+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-01-12 15:09 [PATCH mm-new resend 0/5] mm: accelerate gigantic folio allocation Kefeng Wang
2026-01-12 15:09 ` [PATCH 1/5] mm: page_isolation: introduce page_is_unmovable() Kefeng Wang
2026-01-12 16:36 ` Zi Yan
2026-01-13 4:37 ` Oscar Salvador
2026-01-12 15:09 ` [PATCH 2/5] mm: page_alloc: optimize pfn_range_valid_contig() Kefeng Wang
2026-01-12 17:02 ` Zi Yan
2026-01-13 1:24 ` Kefeng Wang
2026-01-13 1:27 ` Zi Yan
2026-01-13 9:55 ` Kefeng Wang
2026-01-14 1:47 ` Andrew Morton
2026-01-13 5:14 ` Oscar Salvador
2026-01-13 9:55 ` Kefeng Wang
2026-01-12 15:09 ` [PATCH 3/5] mm: hugetlb: optimize replace_free_hugepage_folios() Kefeng Wang
2026-01-13 5:27 ` Oscar Salvador
2026-01-13 11:38 ` Kefeng Wang
2026-01-14 13:55 ` [PATCH v2 " Kefeng Wang
2026-01-15 22:42 ` Andrew Morton
2026-01-16 0:58 ` Kefeng Wang [this message]
2026-01-12 15:09 ` [PATCH 4/5] mm: hugetlb_cma: optimize hugetlb_cma_alloc_frozen_folio() Kefeng Wang
2026-01-13 16:29 ` Zi Yan
2026-01-12 15:09 ` [PATCH 5/5] mm: hugetlb_cma: mark hugetlb_cma{_only} as __ro_after_init Kefeng Wang
2026-01-13 16:29 ` Zi Yan
2026-01-14 7:36 ` Muchun Song
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=da4d80b5-339a-400b-8e1c-c758ef56c460@huawei.com \
--to=wangkefeng.wang@huawei.com \
--cc=akpm@linux-foundation.org \
--cc=david@kernel.org \
--cc=hannes@cmpxchg.org \
--cc=jackmanb@google.com \
--cc=jane.chu@oracle.com \
--cc=linux-mm@kvack.org \
--cc=muchun.song@linux.dev \
--cc=osalvador@suse.de \
--cc=sidhartha.kumar@oracle.com \
--cc=vbabka@suse.cz \
--cc=willy@infradead.org \
--cc=ziy@nvidia.com \
/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