From: Kefeng Wang <wangkefeng.wang@huawei.com>
To: Zi Yan <ziy@nvidia.com>
Cc: Andrew Morton <akpm@linux-foundation.org>,
David Hildenbrand <david@redhat.com>,
Oscar Salvador <osalvador@suse.de>,
Muchun Song <muchun.song@linux.dev>,
Matthew Wilcox <willy@infradead.org>,
<sidhartha.kumar@oracle.com>, <jane.chu@oracle.com>,
Vlastimil Babka <vbabka@suse.cz>,
Brendan Jackman <jackmanb@google.com>,
Johannes Weiner <hannes@cmpxchg.org>, <linux-mm@kvack.org>
Subject: Re: [PATCH v2 1/8] mm: page_alloc: optimize pfn_range_valid_contig()
Date: Fri, 19 Sep 2025 10:03:21 +0800 [thread overview]
Message-ID: <229e5cd0-ed50-488c-88d1-c064b83de5a7@huawei.com> (raw)
In-Reply-To: <E25CD56A-2F37-45A3-8A38-190323ADB4DE@nvidia.com>
On 2025/9/18 23:49, Zi Yan wrote:
> On 18 Sep 2025, at 9:19, Kefeng Wang wrote:
>
>> The alloc_contig_pages() spends a lot of time in pfn_range_valid_contig(),
>> we could check whether the page in this pfn range could be allocated
>> before alloc_contig_range(), if the page can't be migrated, no further
>> action is required, and also skip some unnecessary iterations for
>> compound pages such as THP and non-compound high order buddy, which
>> save times a lot too. The check is racy, but the only danger is skipping
>> too much.
>>
>> A simple test on machine with 116G free memory, allocate 120 * 1G
>> HugeTLB folios(107 successfully returned),
>>
>> time echo 120 > /sys/kernel/mm/hugepages/hugepages-1048576kB/nr_hugepages
>>
>> Before: 0m2.124s
>> After: 0m0.602s
>>
>> Signed-off-by: Kefeng Wang <wangkefeng.wang@huawei.com>
>> ---
>> mm/page_alloc.c | 20 ++++++++++++++++++--
>> 1 file changed, 18 insertions(+), 2 deletions(-)
>>
>> diff --git a/mm/page_alloc.c b/mm/page_alloc.c
>> index 478beaf95f84..5b7d705e9710 100644
>> --- a/mm/page_alloc.c
>> +++ b/mm/page_alloc.c
>> @@ -7012,6 +7012,7 @@ static bool pfn_range_valid_contig(struct zone *z, unsigned long start_pfn,
>> {
>> unsigned long i, end_pfn = start_pfn + nr_pages;
>> struct page *page;
>> + struct folio *folio;
>>
>> for (i = start_pfn; i < end_pfn; i++) {
>> page = pfn_to_online_page(i);
>> @@ -7021,11 +7022,26 @@ static bool pfn_range_valid_contig(struct zone *z, unsigned long start_pfn,
>> if (page_zone(page) != z)
>> return false;
>>
>> - if (PageReserved(page))
>> + folio = page_folio(page);
>> + if (folio_test_reserved(folio))
>> return false;
>>
>> - if (PageHuge(page))
>> + if (folio_test_hugetlb(folio))
>> return false;
>> +
>> + /* The following type of folios aren't migrated */
>
> s/aren’t/cannot be/
ACK.
>
>> + if (folio_test_pgtable(folio) | folio_test_stack(folio))
>> + return false;
should be "||", will fix.
>
> Maybe worth explicitly stating these two types of pages in the commit log.
>
OK, will update.
>> +
>> + /*
>> + * For compound pages such as THP and non-compound high
>> + * order buddy pages, save potentially a lot of iterations
>> + * if we can skip them at once.
>> + */
>> + if (PageCompound(page))
>> + i += (1UL << compound_order(page)) - 1;
>
> Just a note here, if page is tail, this just move i to the next page
> instead of next folio.
As no reference held,it is not too precise but we optimize for most
scenarios.
>
>> + else if (PageBuddy(page))
>> + i += (1UL << buddy_order(page)) - 1;
>> }
>> return true;
>> }
>> --
>> 2.27.0
>
> Otherwise, LGTM. Reviewed-by: Zi Yan <ziy@nvidia.com>
>
>
Thanks.
> Best Regards,
> Yan, Zi
>
next prev parent reply other threads:[~2025-09-19 2:03 UTC|newest]
Thread overview: 23+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-09-18 13:19 [PATCH v2 0/8] mm: hugetlb: allocate frozen gigantic folio Kefeng Wang
2025-09-18 13:19 ` [PATCH v2 1/8] mm: page_alloc: optimize pfn_range_valid_contig() Kefeng Wang
2025-09-18 15:49 ` Zi Yan
2025-09-19 2:03 ` Kefeng Wang [this message]
2025-09-19 1:40 ` kernel test robot
2025-09-19 5:00 ` Dev Jain
2025-09-20 8:19 ` Kefeng Wang
2025-09-30 9:56 ` David Hildenbrand
2025-10-09 12:40 ` Kefeng Wang
2025-09-18 13:19 ` [PATCH v2 2/8] mm: hugetlb: optimize replace_free_hugepage_folios() Kefeng Wang
2025-09-30 9:57 ` David Hildenbrand
2025-10-09 12:40 ` Kefeng Wang
2025-09-18 13:19 ` [PATCH v2 3/8] mm: debug_vm_pgtable: add debug_vm_pgtable_free_huge_page() Kefeng Wang
2025-09-30 10:01 ` David Hildenbrand
2025-09-18 13:19 ` [PATCH v2 4/8] mm: page_alloc: add split_non_compound_page() Kefeng Wang
2025-09-30 10:06 ` David Hildenbrand
2025-10-09 12:40 ` Kefeng Wang
2025-09-18 13:19 ` [PATCH v2 5/8] mm: page_alloc: add alloc_contig_{range_frozen,frozen_pages}() Kefeng Wang
2025-09-18 13:19 ` [PATCH v2 6/8] mm: cma: add __cma_release() Kefeng Wang
2025-09-30 10:15 ` David Hildenbrand
2025-10-09 12:40 ` Kefeng Wang
2025-09-18 13:19 ` [PATCH v2 7/8] mm: cma: add cma_alloc_frozen{_compound}() Kefeng Wang
2025-09-18 13:20 ` [PATCH v2 8/8] mm: hugetlb: allocate frozen pages in alloc_gigantic_folio() Kefeng Wang
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=229e5cd0-ed50-488c-88d1-c064b83de5a7@huawei.com \
--to=wangkefeng.wang@huawei.com \
--cc=akpm@linux-foundation.org \
--cc=david@redhat.com \
--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