mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: "David Hildenbrand (Arm)" <david@kernel.org>
To: Zi Yan <ziy@nvidia.com>
Cc: "Lorenzo Stoakes" <ljs@kernel.org>,
	"Liam R. Howlett" <liam@infradead.org>,
	"Vlastimil Babka" <vbabka@kernel.org>,
	"Borislav Petkov (AMD)" <bp@alien8.de>,
	"Cenk Yılmaz" <cenk@deepfinresearch.com>,
	"Zhang Peng" <zippermonkey@icloud.com>,
	"Mike Rapoport" <rppt@kernel.org>,
	"Suren Baghdasaryan" <surenb@google.com>,
	"Michal Hocko" <mhocko@suse.com>,
	"Andrew Morton" <akpm@linux-foundation.org>,
	"Matthew Brost" <matthew.brost@intel.com>,
	"Joshua Hahn" <joshua.hahnjy@gmail.com>,
	"Byungchul Park" <byungchul@sk.com>,
	"Gregory Price" <gourry@gourry.net>,
	"Ying Huang" <ying.huang@linux.alibaba.com>,
	"Alistair Popple" <apopple@nvidia.com>,
	"Chris Li" <chrisl@kernel.org>,
	"Kairui Song" <kasong@tencent.com>,
	"Kemeng Shi" <shikemeng@huaweicloud.com>,
	"Nhat Pham" <nphamcs@gmail.com>,
	"Baoquan He" <baoquan.he@linux.dev>,
	"Barry Song" <baohua@kernel.org>,
	"Youngjun Park" <youngjun.park@lge.com>,
	"Mel Gorman" <mgorman@suse.de>,
	"Sasha Levin" <sasha.levin@oracle.com>,
	"Rik van Riel" <riel@redhat.com>,
	"Ingo Molnar" <mingo@kernel.org>, "Peter Xu" <peterx@redhat.com>,
	"Hugh Dickins" <hughd@google.com>,
	"Axel Rasmussen" <axelrasmussen@google.com>,
	"Johannes Weiner" <hannes@cmpxchg.org>,
	"Michal Hocko" <mhocko@kernel.org>,
	"Qi Zheng" <qi.zheng@linux.dev>,
	"Shakeel Butt" <shakeel.butt@linux.dev>,
	"Yuanchu Xie" <yuanchu@google.com>, "Wei Xu" <weixugc@google.com>,
	"Baolin Wang" <baolin.wang@linux.alibaba.com>,
	"Andrea Arcangeli" <aarcange@redhat.com>,
	"Thomas Gleixner" <tglx@kernel.org>,
	linux-mm@kvack.org, linux-kernel@vger.kernel.org,
	stable@vger.kernel.org
Subject: Re: [PATCH 4/4] RFC: mm/vmscan: unlock unmapped folios only after the deferred TLB flush
Date: Tue, 1 Dec 2026 16:33:00 +0100	[thread overview]
Message-ID: <87e84f86-87b8-47e9-ba19-f1ce41d14113@kernel.org> (raw)
In-Reply-To: <A4AA8719-376D-4AF9-BD12-0F0CCA49F68E@nvidia.com>

... coming back to this.


>> This can be reproduced easily by swapping out an anon memory range with
>> MADV_PAGEOUT, reading it back in, and swapping it out again
>> (MADV_PAGEOUT) while one thread keeps reading the range and another
>> thread writes to pages as soon as reclaim removed the folios from the
>> swap-cache. The first thread will occasionally read the old content.
> 
> Who would have thought about it? I also wonder why we never get a report
> for this issue.
> 

I am asking myself the same question. It's rather obvious how this can happen
once you know about the race.

>>
>> Fix it by reclaiming folios in batches of up to 64 folios:
>>
>>  (a) Lock each folio, check whether we want to reclaim it, and unmap it.
>>      Keep the folio locked if any deferred TLB flush is pending after
>>      unmapping it, otherwise unlock it right away.
>>
>>  (b) Flush the TLB once, and unlock all folios that are still locked.
>>
>>  (c) Lock the folios again, one at a time, to try reclaiming them.
>>      Activate folios that are still or again mapped, and skip folios
>>      that are under writeback or poisoned by now, or that somebody else
>>      holds locked.
>>
>> As others might map or unmap folios while they are unlocked, we might
>> now activate or reclaim some folios that we would previously have kept
>> or activated, which is fine.
>>
>> Keeping folios locked until the TLB flush makes sure that nobody can
>> remove them from the page-cache / swap-cache, copy them or start
>> writeback. Page faults will either wait on the folio lock, or find the
>> old folio and map it again, which is fine.
>>
>> Unlocking folios before trying to reclaim them makes sure that we never
>> hold more than one folio lock while performing I/O or waiting on the
>> filesystem.
>>
>> The idea of unlocking folios and locking them again using a trylock
>> before trying to reclaim them was inspired by [1] from Zhang Peng.
> 
> This solution makes sense to me.
> 

Thanks!

[...]

>> +static struct folio *staged_folios_next(struct staged_folios *staged,
>> +		struct list_head *folio_list)
>> +{
>> +	struct folio *folio;
>> +
>> +	if (staged_folios_empty(staged))
>> +		return NULL;
>> +	/* Collect more folios until we have enough or there are no more. */
>> +	if (staged->nr_folios < MAX_STAGED_FOLIOS && !list_empty(folio_list))
> 
> One thing to note here is that !list_empty(folio_list) can happen
> in the middle of phase (c), when pageout() splits a large shmem folio,
> and the code switches back to phase (a) and folios on ->flushed
> will stay unlocked longer. This is discovered by AI and I verified it.

Ack.

> 
> To make the code strictly follows the comment, you will want to
> add ->draining and set it to true at phase (c) and back to false
> at phase (a) and check it instead of !list_empty(folio_list).

Sounds good. I'll use "reclaiming".

>>  retry:
>> -	while (!list_empty(folio_list)) {
>> +	/*
>> +	 * We take folios from folio_list to pre-process them (lock, check,
>> +	 * unmap). Then, they move to the staged(unflushed) list, from where
>> +	 * they move to the staged(flushed) list after a batched TLB flush
>> +	 * and after unlocking them. If no TLB flush is pending for a folio,
>> +	 * it moves to the staged(flushed) list directly.
>> +	 *
>> +	 * We then take folios from the staged(flushed) list to actually
>> +	 * reclaim them, or to activate them if they are still mapped.
>> +	 *
>> +	 * So we pre-process folios from folio_list until we collected a batch
>> +	 * of staged folios, or until there are no more folios to
>> +	 * pre-process. Then, we perform a single TLB flush and reclaim all
>> +	 * folios of that batch, before we pre-process more folios.
>> +	 *
>> +	 * Once all lists are empty, we are done.
> 
> I wonder if you want to label the above three phases/steps and
> add comments in staged_folios_next() to clarify how phase transition
> happens.

Ack, will do. But this will need proper cleanups on top to cleanly split all of
that.

Thanks!

-- 
Cheers,

David

  reply	other threads:[~2026-12-01 15:33 UTC|newest]

Thread overview: 27+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-18  9:20 [PATCH 0/4] mm: fixes for stale TLB entries due to delayed/batched TLB flushes David Hildenbrand (Arm)
2026-11-18  9:20 ` [PATCH 1/4] mm: flush stale TLB entries before installing a CoW'ed folio David Hildenbrand (Arm)
2026-11-18 13:55   ` Cenk Yılmaz
2026-11-18 22:35   ` Jose A. Perez de Azpillaga
2026-11-19 16:47   ` Zi Yan
2026-11-19 16:49     ` David Hildenbrand (Arm)
2026-11-19 17:04       ` Zi Yan
2026-11-19 17:07         ` David Hildenbrand (Arm)
2026-11-20 17:39   ` Vlastimil Babka (SUSE)
2026-11-18  9:20 ` [PATCH 2/4] mm/vmscan: flush deferred writable TLB entries before unlocking the folio David Hildenbrand (Arm)
2026-11-18 22:35   ` Jose A. Perez de Azpillaga
2026-11-19  0:13   ` Johannes Weiner
2026-11-19 21:47   ` Zi Yan
2026-11-20 17:42   ` Vlastimil Babka (SUSE)
2026-11-18  9:20 ` [PATCH 3/4] RFC: mm/rmap: don't defer TLB flushes when unmapping lazyfree folios David Hildenbrand (Arm)
2026-11-19 22:10   ` Zi Yan
2026-11-18  9:20 ` [PATCH 4/4] RFC: mm/vmscan: unlock unmapped folios only after the deferred TLB flush David Hildenbrand (Arm)
2026-11-23  4:08   ` Zi Yan
2026-12-01 15:33     ` David Hildenbrand (Arm) [this message]
2026-11-29 11:20   ` Kairui Song
2026-11-29 11:30     ` David Hildenbrand (Arm)
2026-11-29 11:41       ` Kairui Song
2026-12-01 15:34         ` David Hildenbrand (Arm)
2026-11-29 12:44       ` Zhang Peng
2026-12-01 15:34         ` David Hildenbrand (Arm)
2026-11-20  9:00 ` [PATCH 0/4] mm: fixes for stale TLB entries due to delayed/batched TLB flushes David Hildenbrand (Arm)
2026-11-20  9:01 ` (subset) " David Hildenbrand

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=87e84f86-87b8-47e9-ba19-f1ce41d14113@kernel.org \
    --to=david@kernel.org \
    --cc=aarcange@redhat.com \
    --cc=akpm@linux-foundation.org \
    --cc=apopple@nvidia.com \
    --cc=axelrasmussen@google.com \
    --cc=baohua@kernel.org \
    --cc=baolin.wang@linux.alibaba.com \
    --cc=baoquan.he@linux.dev \
    --cc=bp@alien8.de \
    --cc=byungchul@sk.com \
    --cc=cenk@deepfinresearch.com \
    --cc=chrisl@kernel.org \
    --cc=gourry@gourry.net \
    --cc=hannes@cmpxchg.org \
    --cc=hughd@google.com \
    --cc=joshua.hahnjy@gmail.com \
    --cc=kasong@tencent.com \
    --cc=liam@infradead.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-mm@kvack.org \
    --cc=ljs@kernel.org \
    --cc=matthew.brost@intel.com \
    --cc=mgorman@suse.de \
    --cc=mhocko@kernel.org \
    --cc=mhocko@suse.com \
    --cc=mingo@kernel.org \
    --cc=nphamcs@gmail.com \
    --cc=peterx@redhat.com \
    --cc=qi.zheng@linux.dev \
    --cc=riel@redhat.com \
    --cc=rppt@kernel.org \
    --cc=sasha.levin@oracle.com \
    --cc=shakeel.butt@linux.dev \
    --cc=shikemeng@huaweicloud.com \
    --cc=stable@vger.kernel.org \
    --cc=surenb@google.com \
    --cc=tglx@kernel.org \
    --cc=vbabka@kernel.org \
    --cc=weixugc@google.com \
    --cc=ying.huang@linux.alibaba.com \
    --cc=youngjun.park@lge.com \
    --cc=yuanchu@google.com \
    --cc=zippermonkey@icloud.com \
    --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®