From: "Jose A. Perez de Azpillaga" <azpijr@gmail.com>
To: Hugh Dickins <hughd@google.com>,
David Hildenbrand <david@kernel.org>,
Andrew Morton <akpm@linux-foundation.org>
Cc: Baolin Wang <baolin.wang@linux.alibaba.com>,
linux-mm@kvack.org,
"Jose A. Perez de Azpillaga" <azpijr@gmail.com>
Subject: [PATCH v3] mm/shmem: don't allocate the replacement folio with the folio locked
Date: Sat, 5 Dec 2026 17:53:15 +0100 [thread overview]
Message-ID: <20261205165318.114891-1-azpijr@gmail.com> (raw)
shmem_swapin_folio() allocates the replacement for a swapcache folio in
the wrong zone while holding the folio lock, usually with __GFP_FS.
The zone check doesn't need the lock, so allocate the replacement before
locking the folio. The folio can't be split in the meantime, since we
hold a reference to it.
If the allocation fails, shmem_swapin_folio() still returns -ENOMEM
after the swap entry checks.
Suggested-by: David Hildenbrand (Arm) <david@kernel.org>
Link: https://lore.kernel.org/c45c8b8c-0ccc-4e0e-898f-039f84fe1e56@kernel.org
Signed-off-by: Jose A. Perez de Azpillaga <azpijr@gmail.com>
---
Changes in v3:
- call shmem_should_replace_folio() only once (David)
- return -ENOMEM from the caller, shmem_replace_folio() is now void
(David, Baolin)
Changes in v2:
- return -ENOMEM from shmem_replace_folio(), after the swap entry checks
(Baolin)
v2: https://lore.kernel.org/20261202221938.32846-1-azpijr@gmail.com
v1: https://lore.kernel.org/20261201221305.93395-1-azpijr@gmail.com
Tested on x86_64 in QEMU.
mm/shmem.c | 46 ++++++++++++++++++++++++++++------------------
1 file changed, 28 insertions(+), 18 deletions(-)
diff --git a/mm/shmem.c b/mm/shmem.c
index 2e0ca70da2bf..6285da732705 100644
--- a/mm/shmem.c
+++ b/mm/shmem.c
@@ -2370,32 +2370,32 @@ static bool shmem_should_replace_folio(struct folio *folio, gfp_t gfp)
return folio_zonenum(folio) > gfp_zone(gfp);
}
-static int shmem_replace_folio(struct folio **foliop, gfp_t gfp,
- struct shmem_inode_info *info, pgoff_t index,
- struct vm_area_struct *vma)
+static struct folio *shmem_alloc_replacement(struct folio *old, gfp_t gfp,
+ struct shmem_inode_info *info, pgoff_t index,
+ struct vm_area_struct *vma)
{
- struct swap_cluster_info *ci;
- struct folio *new, *old = *foliop;
- swp_entry_t entry = old->swap;
- int nr_pages = folio_nr_pages(old);
- int error = 0;
-
/*
* We have arrived here because our zones are constrained, so don't
* limit chance of success by further cpuset and node constraints.
*/
gfp &= ~GFP_CONSTRAINT_MASK;
#ifdef CONFIG_TRANSPARENT_HUGEPAGE
- if (nr_pages > 1) {
+ if (folio_test_large(old)) {
gfp_t huge_gfp = vma_thp_gfp_mask(vma);
gfp = thp_shmem_limit_gfp_mask(huge_gfp, gfp);
}
#endif
- new = shmem_alloc_folio(gfp, folio_order(old), info, index);
- if (!new)
- return -ENOMEM;
+ return shmem_alloc_folio(gfp, folio_order(old), info, index);
+}
+
+static void shmem_replace_folio(struct folio **foliop, struct folio *new)
+{
+ struct swap_cluster_info *ci;
+ struct folio *old = *foliop;
+ swp_entry_t entry = old->swap;
+ int nr_pages = folio_nr_pages(old);
folio_ref_add(new, nr_pages);
folio_copy(new, old);
@@ -2427,7 +2427,6 @@ static int shmem_replace_folio(struct folio **foliop, gfp_t gfp,
* cache.
*/
folio_put_refs(old, nr_pages + 1);
- return error;
}
static void shmem_set_folio_swapin_error(struct inode *inode, pgoff_t index,
@@ -2546,9 +2545,10 @@ static int shmem_swapin_folio(struct inode *inode, pgoff_t index,
swp_entry_t swap;
softleaf_t index_entry;
struct swap_info_struct *si;
- struct folio *folio = NULL;
+ struct folio *folio = NULL, *new = NULL;
int error, nr_pages, order;
pgoff_t offset;
+ bool replace;
VM_BUG_ON(!*foliop || !xa_is_value(*foliop));
index_entry = radix_to_swp_entry(*foliop);
@@ -2637,6 +2637,10 @@ static int shmem_swapin_folio(struct inode *inode, pgoff_t index,
index = round_down(index, nr_pages);
}
+ replace = shmem_should_replace_folio(folio, gfp);
+ if (replace)
+ new = shmem_alloc_replacement(folio, gfp, info, index, vma);
+
/*
* We have to do this with the folio locked to prevent races.
* The shmem_confirm_swap below only checks if the first swap
@@ -2665,10 +2669,14 @@ static int shmem_swapin_folio(struct inode *inode, pgoff_t index,
*/
arch_swap_restore(folio_swap(swap, folio), folio);
- if (shmem_should_replace_folio(folio, gfp)) {
- error = shmem_replace_folio(&folio, gfp, info, index, vma);
- if (error)
+ if (replace) {
+ if (!new) {
+ error = -ENOMEM;
goto failed;
+ }
+
+ shmem_replace_folio(&folio, new);
+ new = NULL;
}
error = shmem_add_to_page_cache(folio, mapping, index,
@@ -2708,6 +2716,8 @@ static int shmem_swapin_folio(struct inode *inode, pgoff_t index,
if (folio)
folio_unlock(folio);
failed_nolock:
+ if (new)
+ folio_put(new);
if (folio)
folio_put(folio);
put_swap_device(si);
--
2.56.1
next reply other threads:[~2026-12-05 16:53 UTC|newest]
Thread overview: 2+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-12-05 16:53 Jose A. Perez de Azpillaga [this message]
2026-12-05 17:42 ` [PATCH v3] mm/shmem: don't allocate the replacement folio with the folio locked David Hildenbrand (Arm)
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=20261205165318.114891-1-azpijr@gmail.com \
--to=azpijr@gmail.com \
--cc=akpm@linux-foundation.org \
--cc=baolin.wang@linux.alibaba.com \
--cc=david@kernel.org \
--cc=hughd@google.com \
--cc=linux-mm@kvack.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