linux-mm.kvack.org archive mirror
 help / color / mirror / Atom feed
From: Wei Yang <richard.weiyang@gmail.com>
To: Zi Yan <ziy@nvidia.com>
Cc: Wei Yang <richard.weiyang@gmail.com>,
	"David Hildenbrand (Arm)" <david@kernel.org>,
	Linux MM <linux-mm@kvack.org>
Subject: Re: A potential refcount issue during __folio_split
Date: Fri, 27 Feb 2026 00:51:29 +0000	[thread overview]
Message-ID: <20260227005129.ujpgdvjjyqpemzxy@master> (raw)
In-Reply-To: <097A507A-C60A-47AF-9590-1D6CF712B1FE@nvidia.com>

On Mon, Feb 23, 2026 at 11:00:01PM -0500, Zi Yan wrote:
>On 23 Feb 2026, at 6:59, Wei Yang wrote:
>
>> On Mon, Feb 23, 2026 at 10:23:11AM +0100, David Hildenbrand (Arm) wrote:
>>>> BTW, in the folio world, I do not think it is possible to perform the aforementioned
>>>> split_huge_page_to_list_to_order() pattern any more, since you always work on folio,
>>>> the head. Unless there is a need of getting hold of a tail after-split folio after
>>>> a folio split, the pattern would be:
>>>>
>>>> tail_page = folio_page(folio, N);
>>>>
>>>> folio_get(folio);
>>>> folio_lock(folio);
>>>> folio_split(folio, ..., /* new parameter: lock_at = */ tail_page, ...);
>>>> tail_folio = page_folio(tail_page);
>>>> folio_unlock(tail_folio);
>>>> folio_put(tail_folio);
>>>
>>
>> Missed this. Agree.
>>
>>> Agreed. Maybe it would be even nicer if the split function could return the
>>> new folio directly.
>>>
>>> folio_get(folio);
>>> folio_lock(folio);
>>> split_folio = folio_split_XXX(folio, ..., tail_page, ...);
>>> if (IS_ERR_VALUE(split_folio)) {
>>> 	...
>>> }
>>> folio_unlock(split_folio);
>>> folio_put(split__folio);
>>>
>>
>> I am afraid it would be complicated?
>>
>> Well, we don't have this usecase now, could decide it when we do need it.
>
>The patch below should work, but for now, since we do not have any user,
>it is better to update the comment and add a check to make sure @lock_at
>always points to the head page if @list is not NULL.

Hi, Zi Yan

Below is my draft change for the comment and check. If it looks good, I would
like to send a formal patch.

Look forward your opinion.

diff --git a/mm/huge_memory.c b/mm/huge_memory.c
index 2dbb35accf4b..8047f00bfc2a 100644
--- a/mm/huge_memory.c
+++ b/mm/huge_memory.c
@@ -4158,8 +4158,9 @@ int folio_split_unmapped(struct folio *folio, unsigned int new_order)
 
 /*
  * This function splits a large folio into smaller folios of order @new_order.
- * @page can point to any page of the large folio to split. The split operation
- * does not change the position of @page.
+ * If @list is null, @page can point to any page of the large folio to split.
+ * If @list is !null, @page must be head page of the large folio to split.
+ * The split operation does not change the position of @page.
  *
  * Prerequisites:
  *
@@ -4208,6 +4209,16 @@ int __split_huge_page_to_list_to_order(struct page *page, struct list_head *list
 {
 	struct folio *folio = page_folio(page);
 
+	/*
+	 * When @list is !null, each after-split tail folio would get one
+	 * refcount in lru_add_split_folio(). But all after-split folio
+	 * would release one refcount except the one at @page.
+	 *
+	 * So @page should be the head page for this case, otherwise the
+	 * refcount would be wrong.
+	 */
+	VM_WARN_ON_ONCE_FOLIO(list && (page != folio_page(folio, 0)), folio);
+
 	return __folio_split(folio, new_order, &folio->page, page, list,
 			     SPLIT_TYPE_UNIFORM);
 }
-- 
2.34.1

-- 
Wei Yang
Help you, Help me


      parent reply	other threads:[~2026-02-27  0:51 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <20260222010425.gbsjzhrew3pg4qrw@master>
     [not found] ` <20260222010708.uohpmddmzaa4i4ic@master>
2026-02-22  3:00   ` Zi Yan
2026-02-22 10:28     ` Wei Yang
2026-02-23  9:23     ` David Hildenbrand (Arm)
2026-02-23 11:59       ` Wei Yang
2026-02-24  4:00         ` Zi Yan
2026-02-24  4:25           ` Wei Yang
2026-02-27  0:51           ` Wei Yang [this message]

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=20260227005129.ujpgdvjjyqpemzxy@master \
    --to=richard.weiyang@gmail.com \
    --cc=david@kernel.org \
    --cc=linux-mm@kvack.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