linux-mm.kvack.org archive mirror
 help / color / mirror / Atom feed
From: David Hildenbrand <david@redhat.com>
To: yangge1116@126.com, akpm@linux-foundation.org
Cc: linux-mm@kvack.org, linux-kernel@vger.kernel.org,
	21cnbao@gmail.com, baolin.wang@linux.alibaba.com,
	muchun.song@linux.dev, osalvador@suse.de, liuzixing@hygon.cn
Subject: Re: [PATCH] mm/hugetlb: remove redundant folio_test_hugetlb
Date: Mon, 26 May 2025 14:23:58 +0200	[thread overview]
Message-ID: <347b3035-26fe-43af-8df4-b1610d305908@redhat.com> (raw)
In-Reply-To: <427043ca-ae91-4386-8ffd-aaf164773226@redhat.com>

On 26.05.25 14:20, David Hildenbrand wrote:
> On 23.05.25 10:05, yangge1116@126.com wrote:
>> From: Ge Yang <yangge1116@126.com>
>>
>> In the isolate_or_dissolve_huge_folio() function, the folio_test_hugetlb()
>> function is called to determine whether a folio is a hugetlb folio.
>> However, in the subsequent alloc_and_dissolve_hugetlb_folio() function,
>> the folio_test_hugetlb() function is called again to make the same
>> determination about whether the folio is a hugetlb folio. It appears that
>> the folio_test_hugetlb() check in the isolate_or_dissolve_huge_folio()
>> function can be removed. Additionally, a similar issue exists in the
>> replace_free_hugepage_folios() function, and it should be addressed as
>> well.
>>
>> Suggested-by: Oscar Salvador <osalvador@suse.de>
>> Signed-off-by: Ge Yang <yangge1116@126.com>
>> ---
>>    mm/hugetlb.c | 51 +++++++++++++--------------------------------------
>>    1 file changed, 13 insertions(+), 38 deletions(-)
>>
>> diff --git a/mm/hugetlb.c b/mm/hugetlb.c
>> index 6c2e007..6e46f2f 100644
>> --- a/mm/hugetlb.c
>> +++ b/mm/hugetlb.c
>> @@ -2787,20 +2787,24 @@ void restore_reserve_on_error(struct hstate *h, struct vm_area_struct *vma,
>>    /*
>>     * alloc_and_dissolve_hugetlb_folio - Allocate a new folio and dissolve
>>     * the old one
>> - * @h: struct hstate old page belongs to
>>     * @old_folio: Old folio to dissolve
>>     * @list: List to isolate the page in case we need to
>>     * Returns 0 on success, otherwise negated error.
>>     */
>> -static int alloc_and_dissolve_hugetlb_folio(struct hstate *h,
>> -			struct folio *old_folio, struct list_head *list)
>> +static int alloc_and_dissolve_hugetlb_folio(struct folio *old_folio,
>> +			struct list_head *list)
>>    {
>> -	gfp_t gfp_mask = htlb_alloc_mask(h) | __GFP_THISNODE;
>> +	gfp_t gfp_mask;
>> +	struct hstate *h;
>>    	int nid = folio_nid(old_folio);
>>    	struct folio *new_folio = NULL;
>>    	int ret = 0;
>>    
>>    retry:
>> +	/*
>> +	 * The old_folio might have been dissolved from under our feet, so make sure
>> +	 * to carefully check the state under the lock.
>> +	 */
>>    	spin_lock_irq(&hugetlb_lock);
>>    	if (!folio_test_hugetlb(old_folio)) {
>>    		/*
>> @@ -2829,8 +2833,10 @@ static int alloc_and_dissolve_hugetlb_folio(struct hstate *h,
>>    		cond_resched();
>>    		goto retry;
>>    	} else {
>> +		h = folio_hstate(old_folio);
>>    		if (!new_folio) {
>>    			spin_unlock_irq(&hugetlb_lock);
>> +			gfp_mask = htlb_alloc_mask(h) | __GFP_THISNODE;
>>    			new_folio = alloc_buddy_hugetlb_folio(h, gfp_mask, nid,
>>    							      NULL, NULL);
>>    			if (!new_folio)
>> @@ -2874,35 +2880,20 @@ static int alloc_and_dissolve_hugetlb_folio(struct hstate *h,
>>    
>>    int isolate_or_dissolve_huge_folio(struct folio *folio, struct list_head *list)
>>    {
>> -	struct hstate *h;
>>    	int ret = -EBUSY;
>>    
>>    	/*
>> -	 * The page might have been dissolved from under our feet, so make sure
>> -	 * to carefully check the state under the lock.
>> -	 * Return success when racing as if we dissolved the page ourselves.
>> -	 */
>> -	spin_lock_irq(&hugetlb_lock);
>> -	if (folio_test_hugetlb(folio)) {
>> -		h = folio_hstate(folio);
>> -	} else {
>> -		spin_unlock_irq(&hugetlb_lock);
>> -		return 0;
>> -	}
>> -	spin_unlock_irq(&hugetlb_lock);
>> -
>> -	/*
>>    	 * Fence off gigantic pages as there is a cyclic dependency between
>>    	 * alloc_contig_range and them. Return -ENOMEM as this has the effect
>>    	 * of bailing out right away without further retrying.
>>    	 */
>> -	if (hstate_is_gigantic(h))
>> +	if (folio_order(folio) > MAX_PAGE_ORDER)
>>    		return -ENOMEM;
>>    
>>    	if (folio_ref_count(folio) && folio_isolate_hugetlb(folio, list))
>>    		ret = 0;
>>    	else if (!folio_ref_count(folio))
>> -		ret = alloc_and_dissolve_hugetlb_folio(h, folio, list);
>> +		ret = alloc_and_dissolve_hugetlb_folio(folio, list);
>>    
>>    	return ret;
>>    }
>> @@ -2916,7 +2907,6 @@ int isolate_or_dissolve_huge_folio(struct folio *folio, struct list_head *list)
>>     */
>>    int replace_free_hugepage_folios(unsigned long start_pfn, unsigned long end_pfn)
>>    {
>> -	struct hstate *h;
>>    	struct folio *folio;
>>    	int ret = 0;
>>    
>> @@ -2925,23 +2915,8 @@ int replace_free_hugepage_folios(unsigned long start_pfn, unsigned long end_pfn)
>>    	while (start_pfn < end_pfn) {
>>    		folio = pfn_folio(start_pfn);
>>    
>> -		/*
>> -		 * The folio might have been dissolved from under our feet, so make sure
>> -		 * to carefully check the state under the lock.
>> -		 */
>> -		spin_lock_irq(&hugetlb_lock);
>> -		if (folio_test_hugetlb(folio)) {
>> -			h = folio_hstate(folio);
>> -		} else {
>> -			spin_unlock_irq(&hugetlb_lock);
>> -			start_pfn++;
>> -			continue;
>> -		}
>> -		spin_unlock_irq(&hugetlb_lock);
> 
> ^ oh my, that is bad code.
> 
> Taking the hugetlb_lock for each and ever page in the range.
> 
> Let me find that code and nack it.

Already in 6.15, gah.

Please convert that code to never ever take any hugeglb locks unless we 
are clearly dealing with a hugetlb folios.

-- 
Cheers,

David / dhildenb



  reply	other threads:[~2025-05-26 12:24 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-05-23  8:05 yangge1116
2025-05-23  8:23 ` Oscar Salvador
2025-05-23  8:30 ` Muchun Song
2025-05-26 12:20 ` David Hildenbrand
2025-05-26 12:23   ` David Hildenbrand [this message]
2025-05-26 13:05     ` Oscar Salvador
2025-05-26 13:06       ` David Hildenbrand
2025-05-26 13:09         ` Oscar Salvador
2025-05-26 13:10         ` Ge Yang

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=347b3035-26fe-43af-8df4-b1610d305908@redhat.com \
    --to=david@redhat.com \
    --cc=21cnbao@gmail.com \
    --cc=akpm@linux-foundation.org \
    --cc=baolin.wang@linux.alibaba.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-mm@kvack.org \
    --cc=liuzixing@hygon.cn \
    --cc=muchun.song@linux.dev \
    --cc=osalvador@suse.de \
    --cc=yangge1116@126.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