mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: "Lorenzo Stoakes (ARM)" <ljs@kernel.org>
To: "Jan Harkes" <jaharkes@cs.cmu.edu>,
	coda@cs.cmu.edu, "Andrew Morton" <akpm@linux-foundation.org>,
	"David Rheinsberg" <david@readahead.eu>,
	"Hugh Dickins" <hughd@google.com>,
	"Vlastimil Babka" <vbabka@kernel.org>,
	"Liam R. Howlett" <liam@infradead.org>,
	"David Hildenbrand" <david@kernel.org>,
	"Jann Horn" <jannh@google.com>, "Alex Shi" <alexs@kernel.org>,
	"Johannes Weiner" <hannes@cmpxchg.org>,
	"Pedro Falcato" <pfalcato@suse.de>,
	"David Rientjes" <rientjes@google.com>,
	"Rik van Riel" <riel@redhat.com>,
	"Mike Rapoport" <rppt@kernel.org>,
	"Suren Baghdasaryan" <surenb@google.com>,
	"Michal Hocko" <mhocko@suse.com>,
	"Jonathan Corbet" <corbet@lwn.net>,
	"Shuah Khan" <skhan@linuxfoundation.org>,
	"Randy Dunlap" <rdunlap@infradead.org>,
	"Michał Mirosław" <mirq-linux@rere.qmqm.pl>,
	"Arnd Bergmann" <arnd@arndb.de>,
	"Stephen Rothwell" <sfr@canb.auug.org.au>,
	"Muhammad Usama Anjum" <usama.anjum@arm.com>
Cc: codalist@coda.cs.cmu.edu, linux-mm@kvack.org,
	 Andrei Vagin <avagin@gmail.com>,
	linux-doc@vger.kernel.org,  linux-kernel@vger.kernel.org,
	linux-fsdevel@vger.kernel.org,
	 "Lorenzo Stoakes (ARM)" <ljs@kernel.org>,
	Chris Mason <mason@kernel.org>
Subject: [PATCH 2/4] mm/madvise: don't declare OOM for MADV_WILLNEED swapin
Date: Fri, 27 Nov 2026 19:45:41 +0000	[thread overview]
Message-ID: <20261127-mlock-madvise-mprotect-ai-fixes-v1-2-e46b2b1eb435@kernel.org> (raw)
In-Reply-To: <20261127-mlock-madvise-mprotect-ai-fixes-v1-0-e46b2b1eb435@kernel.org>

From: Chris Mason <mason@kernel.org>

MADV_WILLNEED on memory in swap can OOM kill a task when the memcg that
the swap entries are charged to is at its limit.

The hint is only a prefetch, so it has to give up on a page when memory
is short. But swapin_walk_pmd_entry() and shmem_swapin_range() call
read_swap_cache_async() with GFP_HIGHUSER_MOVABLE and with the gfp mask
of the mapping, so the allocation and the memcg charge can both go as
far as the OOM killer.

The charge goes to the memcg of the swap entry, which need not be the
caller's.

The fix used here is to allocate with the gfp mask of readahead, and to
flush the block plug when a page fails. madvise_do_behavior() plugs the
whole walk, so the swap writes of reclaim can sit in that plug. Their
folios stay under writeback, and with no retries the rest of the range
can fail as well.

The hint can now skip pages when memory is short, whether in a memcg at
its limit or on the system as a whole, since neither the allocation nor
the memcg charge is retried any longer.

This applies in particular where reclaim has to write to swap to make
room. Skipped pages stay in swap until a fault reads them.

Fixes: 4c6355b25e8b ("mm: memcontrol: charge swapin pages on instantiation")
Signed-off-by: Chris Mason <mason@kernel.org>
[ ljs: commit message tweak ]
Signed-off-by: Lorenzo Stoakes (ARM) <ljs@kernel.org>
---
 mm/madvise.c | 19 ++++++++++++++-----
 1 file changed, 14 insertions(+), 5 deletions(-)

diff --git a/mm/madvise.c b/mm/madvise.c
index ebb4ce7daf6c..8b56f7aea935 100644
--- a/mm/madvise.c
+++ b/mm/madvise.c
@@ -196,6 +196,8 @@ static int madvise_update_vma(vm_flags_t new_flags,
 static int swapin_walk_pmd_entry(pmd_t *pmd, unsigned long start,
 		unsigned long end, struct mm_walk *walk)
 {
+	/* Only a prefetch, as in readahead_gfp_mask() */
+	const gfp_t gfp = GFP_HIGHUSER_MOVABLE | __GFP_NORETRY | __GFP_NOWARN;
 	struct vm_area_struct *vma = walk->private;
 	struct swap_io_ctx ctx = {};
 	pte_t *ptep = NULL;
@@ -221,10 +223,13 @@ static int swapin_walk_pmd_entry(pmd_t *pmd, unsigned long start,
 		pte_unmap_unlock(ptep, ptl);
 		ptep = NULL;
 
-		folio = read_swap_cache_async(&ctx, entry, GFP_HIGHUSER_MOVABLE,
-					vma, addr);
-		if (folio)
+		folio = read_swap_cache_async(&ctx, entry, gfp, vma, addr);
+		if (folio) {
 			folio_put(folio);
+		} else {
+			/* Reclaim queues its swap writes in our plug */
+			blk_flush_plug(current->plug, false);
+		}
 	}
 
 	if (ptep)
@@ -267,9 +272,13 @@ static void shmem_swapin_range(struct vm_area_struct *vma,
 		rcu_read_unlock();
 
 		folio = read_swap_cache_async(&ctx, entry,
-				mapping_gfp_mask(mapping), vma, addr);
-		if (folio)
+				readahead_gfp_mask(mapping), vma, addr);
+		if (folio) {
 			folio_put(folio);
+		} else {
+			/* Reclaim queues its swap writes in our plug */
+			blk_flush_plug(current->plug, false);
+		}
 
 		rcu_read_lock();
 	}

-- 
2.56.1


  parent reply	other threads:[~2026-11-27 19:46 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-27 19:45 [PATCH 0/4] mm: AI-generated fixes for coda, madvise, mprotect and PAGEMAP_SCAN Lorenzo Stoakes (ARM)
2026-11-27 19:45 ` [PATCH 1/4] coda: never point a file's f_mapping at a container's mapping Lorenzo Stoakes (ARM)
2026-11-27 20:44   ` Andrew Morton
2026-11-27 22:05     ` Pedro Falcato
2026-11-27 22:01   ` Pedro Falcato
2026-11-27 19:45 ` Lorenzo Stoakes (ARM) [this message]
2026-11-27 22:14   ` [PATCH 2/4] mm/madvise: don't declare OOM for MADV_WILLNEED swapin Pedro Falcato
2026-11-27 19:45 ` [PATCH 3/4] mm/mprotect: fix unpaired notifier start at address 0 Lorenzo Stoakes (ARM)
2026-11-27 22:15   ` Pedro Falcato
2026-11-27 19:45 ` [PATCH 4/4] fs/proc/task_mmu: write-protect only with ptrace attach Lorenzo Stoakes (ARM)
2026-11-27 22:24   ` Pedro Falcato

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=20261127-mlock-madvise-mprotect-ai-fixes-v1-2-e46b2b1eb435@kernel.org \
    --to=ljs@kernel.org \
    --cc=akpm@linux-foundation.org \
    --cc=alexs@kernel.org \
    --cc=arnd@arndb.de \
    --cc=avagin@gmail.com \
    --cc=coda@cs.cmu.edu \
    --cc=codalist@coda.cs.cmu.edu \
    --cc=corbet@lwn.net \
    --cc=david@kernel.org \
    --cc=david@readahead.eu \
    --cc=hannes@cmpxchg.org \
    --cc=hughd@google.com \
    --cc=jaharkes@cs.cmu.edu \
    --cc=jannh@google.com \
    --cc=liam@infradead.org \
    --cc=linux-doc@vger.kernel.org \
    --cc=linux-fsdevel@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-mm@kvack.org \
    --cc=mason@kernel.org \
    --cc=mhocko@suse.com \
    --cc=mirq-linux@rere.qmqm.pl \
    --cc=pfalcato@suse.de \
    --cc=rdunlap@infradead.org \
    --cc=riel@redhat.com \
    --cc=rientjes@google.com \
    --cc=rppt@kernel.org \
    --cc=sfr@canb.auug.org.au \
    --cc=skhan@linuxfoundation.org \
    --cc=surenb@google.com \
    --cc=usama.anjum@arm.com \
    --cc=vbabka@kernel.org \
    /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®