mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Kiryl Shutsemau <kirill@shutemov.name>
To: Zi Yan <ziy@nvidia.com>, Balbir Singh <balbirs@nvidia.com>
Cc: Usama Arif <usama.arif@linux.dev>,
	akpm@linux-foundation.org,  david@kernel.org, ljs@kernel.org,
	hannes@cmpxchg.org, lance.yang@linux.dev,  hughd@google.com,
	baolin.wang@linux.alibaba.com, baohua@kernel.org,
	 liam@infradead.org, nico.pache@linux.dev, dev.jain@arm.com,
	ryan.roberts@arm.com,  balbirs@nvidia.com, linux-mm@kvack.org,
	linux-kernel@vger.kernel.org
Subject: Re: [PATCH 1/5] mm/huge_memory: do not touch frozen folios in deferred_split_isolate()
Date: Mon, 31 Aug 2026 01:33:02 +0100	[thread overview]
Message-ID: <apTKkAkdUZYv-Zab@thinkstation> (raw)
In-Reply-To: <DL07CYI5866P.2HCBRH4BWPMKN@nvidia.com>

On Thu, Aug 27, 2026 at 09:57:20PM -0400, Zi Yan wrote:
> On Thu Aug 27, 2026 at 12:38 PM EDT, Usama Arif wrote:
> > On Wed, 26 Aug 2026 17:20:57 +0100 Kiryl Shutsemau <kirill@shutemov.name> wrote:
> >
> >> From: "Kiryl Shutsemau (Meta)" <kas@kernel.org>
> >> 
> >> deferred_split_isolate() probes each queued folio with folio_try_get().
> >> folio_try_get() failure is treated as a lost race with folio_put(): clear
> >> PG_partially_mapped, correct MTHP_STAT_NR_ANON_PARTIALLY_MAPPED, take
> >> the folio off the queue.
> >> 
> >> The folio_put() race is the most common case for !folio_try_get(), but
> >> it is not the only option. Another scenario is folio_ref_freeze().
> >> 
> >> A zero refcount in such cases does not mean the folio is going away.  It
> >> means "don't touch me" and current deferred_split_isolate() doesn't
> >> respect it. It can lead to unqueueing folios from the deferred list for
> >> no reason:
> >> 
> >>     CPU 0                            CPU 1
> >>     ---------------------------      ------------------------------
> >>     freeze a mapped folio            deferred_split_scan()
> >>       folio_ref_freeze()               folio_try_get() fails
> >>                                        folio_clear_partially_mapped()
> >>                                        NR_ANON_PARTIALLY_MAPPED--
> >>                                        folio off the queue
> >>     give up, put it back
> >>       folio_ref_unfreeze()
> >> 
> >> The folio is still partially mapped, but it is no longer a split candidate.
> >> Nothing queues it again until part of it is unmapped once more.
> >> 
> >> Skip the folio instead: whoever freezes the folio, owns it and owner is
> >> responsible for its fate. It also covers the folio_put() case:
> >> __folio_put() unqueues the folio via folio_unqueue_deferred_split().
> >> 
> >> Reported-by: Lance Yang <lance.yang@linux.dev>
> >> Link: https://lore.kernel.org/all/20260824131224.73344-1-lance.yang@linux.dev/
> >> Assisted-by: Claude-Code:claude-opus-5
> >> Signed-off-by: Kiryl Shutsemau (Meta) <kas@kernel.org>
> >> ---
> >>  mm/huge_memory.c | 19 ++++---------------
> >>  1 file changed, 4 insertions(+), 15 deletions(-)
> >> 
> >> diff --git a/mm/huge_memory.c b/mm/huge_memory.c
> >> index ced400f72d43..6281ed993243 100644
> >> --- a/mm/huge_memory.c
> >> +++ b/mm/huge_memory.c
> >> @@ -4590,22 +4590,11 @@ static enum lru_status deferred_split_isolate(struct list_head *item,
> >>  	struct folio *folio = container_of(item, struct folio, _deferred_list);
> >>  	struct list_head *freeable = cb_arg;
> >>  
> >> -	if (folio_try_get(folio)) {
> >> -		list_lru_isolate_move(lru, item, freeable);
> >> -		return LRU_REMOVED;
> >> -	}
> >> +	/* Lost race to folio_put() or the folio is under folio_ref_freeze() */
> >> +	if (!folio_try_get(folio))
> >> +		return LRU_SKIP;
> >
> > I think we might have a problem here for ZONE_DEVICE folios?
> 
> For coherent ZONE_DEVICE folios, yes. IIRC, private ZONE_DEVICE folios
> are not added to deferred split queue.
> >
> > This assumes the final put always dequeues the folio, but ZONE_DEVICE folios
> > bypass the generic folio_unqueue_deferred_split() path.
> > With memcg disabled, this can leave a recycled folio linked on the
> > deferred-split list?
> >
> > Should we dequeue folios in free_zone_device_folio()?
> 
> I think so, before mem_cgroup_uncharge().
> 
> But it is a pre-existing issue. We need a separate patch unqueuing
> folios in free_zone_device_folio() to fix commit a30b48bf1b24
> ("mm/migrate_device: implement THP migration of zone device pages").

Agreed, and it is inert today: nothing allocates a large coherent folio.

amdkfd is the only driver with a coherent pgmap and it hands out order-0
pages, and test_hmm builds its coherent chunk the same way. With test_hmm
taught to hand out PMD sized folios, memcg on gives the WARN_ON_ONCE() in
uncharge_folio() and cgroup_disable=memory gives list_del corruption once
the driver reuses the page.

I am not sure what the right solution is: keep such pages off the queue
or dequeue them on free? Or both?

Balbir, I don't know much about zone device. Do you want to take it on?

-- 
  Kiryl Shutsemau / Kirill A. Shutemov

  reply	other threads:[~2026-08-31  0:33 UTC|newest]

Thread overview: 20+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-08-26 16:20 [PATCH 0/5] Fix deferred_split_isolate() and clean up __folio_freeze_and_split_unmapped() Kiryl Shutsemau
2026-08-26 16:20 ` [PATCH 1/5] mm/huge_memory: do not touch frozen folios in deferred_split_isolate() Kiryl Shutsemau
2026-08-26 16:45   ` Zi Yan
2026-08-27 15:02   ` Johannes Weiner
2026-08-27 15:23   ` David Hildenbrand (Arm)
2026-08-27 15:38     ` Zi Yan
2026-08-27 15:56       ` David Hildenbrand (Arm)
2026-08-27 16:38   ` Usama Arif
2026-08-28  1:57     ` Zi Yan
2026-08-31  0:33       ` Kiryl Shutsemau [this message]
2026-08-26 16:20 ` [PATCH 2/5] mm/huge_memory: dequeue the deferred split after the split freeze Kiryl Shutsemau
2026-08-26 17:10   ` Zi Yan
2026-08-27 15:25   ` David Hildenbrand (Arm)
2026-08-27 16:59   ` Johannes Weiner
2026-08-26 16:20 ` [PATCH 3/5] mm/huge_memory: reduce indent level in __folio_freeze_and_split_unmapped() Kiryl Shutsemau
2026-08-26 16:34   ` Zi Yan
2026-08-26 16:43     ` Kiryl Shutsemau
2026-08-26 16:21 ` [PATCH 4/5] mm/huge_memory: fold nested ifs " Kiryl Shutsemau
2026-08-26 16:21 ` [PATCH 5/5] mm/huge_memory: turn the swapcache-with-mapping error case into an assert Kiryl Shutsemau
2026-08-28  3:14 ` [PATCH 0/5] Fix deferred_split_isolate() and clean up __folio_freeze_and_split_unmapped() Balbir Singh

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=apTKkAkdUZYv-Zab@thinkstation \
    --to=kirill@shutemov.name \
    --cc=akpm@linux-foundation.org \
    --cc=balbirs@nvidia.com \
    --cc=baohua@kernel.org \
    --cc=baolin.wang@linux.alibaba.com \
    --cc=david@kernel.org \
    --cc=dev.jain@arm.com \
    --cc=hannes@cmpxchg.org \
    --cc=hughd@google.com \
    --cc=lance.yang@linux.dev \
    --cc=liam@infradead.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-mm@kvack.org \
    --cc=ljs@kernel.org \
    --cc=nico.pache@linux.dev \
    --cc=ryan.roberts@arm.com \
    --cc=usama.arif@linux.dev \
    --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

all inboxes | Powered by JetHome®