Thread View: linux.kernel.netdev
3 messages
3 total messages
Started by "David Hildenbrand (Arm)"
Thu, 12 Nov 2026 22:53
[PATCH v4 0/9] mm: limit pagecache folio refcount
Author: "David Hildenbrand (Arm)"
Date: Thu, 12 Nov 2026 22:53
Date: Thu, 12 Nov 2026 22:53
260 lines
20541 bytes
20541 bytes
I'm resending this quite quickly, addressing Pedro's review (thanks!!!),
so I can replace the version in -next tomorrow and move forward with
this to get it fixed.
I un-RFC'ed the -net patches, but I will not pick them up. As mentioned
below, they can go in independently.
--
Users can create a lot of folio references to pagecache folios, making
it possible to overflow the refcount of large folios.
While we knew that one could in theory overflow a folio's refcount, we
mostly assumed that bad things would happen and it wouldn't go
unnoticed.
Not so sure if that is really always the case, and leaving that problem
unsolved is irresponsible, so let's limit pagecache folio refcounts.
This is mostly relevant for large folios / thps in the page cache, where
a single VMA can hold multiple folio references, allowing for many folio
references. But with sufficient (memory+process) resources, it is also
possible with small folios, just not that relevant in practice today.
With anon folios (including THPs), we should not be able to trigger folio
refcount overflows, as one would need so many processes that it
becomes impossible in practice. It was likely a problem for anon folios as
well before we had a dedicated pincount.
Let's limit the refcount before it turns negative, giving admin processes
some extra reserves (to recover a problematic system). We still leave some
headroom to allow for some slight limit overshooting, to make the
implementation easier.
But to make that work reliably, we have to make anybody that can take
an unbounded number of folio references obey these limits.
Patch #1 handles mapping references from page tables. We also have to take
care of mmu_gather, to not hold too many references when zapping (whereby
new page faults could remap pages concurrently), which we do in patch #3.
Patch #4 fixes iommufd cleanup when adding folio pins fails, which these
limits make easier to trigger.
Splicing pagecache folios to a pipe (patch #2) also needs care. After
feedback from Linus, these users really should be using try_get_page(),
where we now also implement the new limit check. We also teach GUP in
patch #5 to stay below the limit, to avoid negative refcounts that cause
trouble.
Doing some digging after getting a hint from Andrew, there are various
paths in net that can also take an unbounded number of page refs and are
problematic. Let's use try_get_page() in all affected paths.
Net paths that take page refs that I don't touch because the references
are either bounded or the paths are not relevant in practice:
- skb_split(): can add one reference per split, but each split
allocates a new skb that is charged to the socket, and the references
go away once the data was acked. Reworking that to be able to fail
requires more plumbing.
- IP-TFS reassembly: needs one new inner skb per additional reference.
- io_uring zerocopy send (managed frag references): each zerocopy piece
is charged at least 4 KiB of TCP memory, so not relevant in practice.
- kTLS software splicing: only transfers references from the pipe, so
not a problem.
- AF_XDP zerocopy TX, nfsd splice reads, sockmap / sk_msg, espintcp:
bounded, or require privileges / special setups, so not a problem in
practice.
- Driver-specific paths (e.g., kTLS device offload): require special
hardware, didn't look into these.
- page_pool and net_iov frags: not pagecache pages, so not a problem.
For page table mappings, this is only a temporary solution for
upstream+stable, until we have something better, which will be harder to
backport:
- Letting the mapcount of large folios be a long
- Holding a single refcount if the folio is mapped, across all mappings
- Reverting the limits introduced in this series
After that, most of patch #1 will be reverted, everything else will
stay.
Tested on x86-64 by lowering the folio refcount limits such that limits
can easily be exceeded, to then test fault (SIGBUS), fork(), splice and GUP
behavior briefly when we run into refcount limits on a shmem THP.
The net patches were tested similarly with AF_PACKET, nft dup, NFQUEUE,
software GSO segmentation, TCP receive coalescing with dropped ACKs and
socket splicing.
Signed-off-by: David Hildenbrand (Arm) <david@kernel.org>
---
Changes in v4:
- Refine and update various patch descriptions.
- Pick up RBs + Acks (thanks!)
- "mm: limit pagecache folio refcount when mapping pages"
- Rename __folio_ref_count_over_limit() to
folio_ref_count_over_limit_slowpath(), pass the folio and print a
ratelimited warning when we run into the limit.
- Check in next_uptodate_folio() for fault-around instead.
- Add comments for the VM_MIXEDMAP handling during fork().
- "mm: limit pagecache folio refcount when splicing into pipes"
- Introduce __folio_ref_count_over_limit(), to avoid re-reading the
refcount in folio_get_checked().
- "mm/gup: limit pagecache folio refcount when grabbing references"
- Use __folio_ref_count_over_limit() to avoid re-reading the refcount.
- Link to v3: https://patch.msgid.link/20261110-refcount_overflow_stable-v3-0-318625d63a98@kernel.org
Changes in v3:
- "mm: limit pagecache folio refcount when mapping pages"
- Move IRQ disabled check over from GUP patch.
- Add in_task() check to prepare for networking patches.
- Only check for invalid refcounts in folio_get() and drop
folio_ref_zero_or_close_to_overflow().
- Improve documentation and patch description.
- "mm: limit pagecache folio refcount when splicing into pipes"
- Introduce folio_get_checked() and use it in splice_folio_into_pipe(),
fuse_ref_folio() and try_get_page().
- Return -EFAULT and don't fall back to copying anymore.
- Stop filemap_splice_read() after a partially spliced folio.
- Update patch description.
- "mm/gup: limit pagecache folio refcount when grabbing references"
- Moved the IRQs-disabled check to patch #1.
- Update documentation.
- New RFC patches handling networking paths that copy or add frag
references:
- "net: limit folio refcount when copying skb frag references"
- "net: limit folio refcount when rearranging skb frags"
- "net: limit folio refcount when splicing pages into skbs"
- "net: limit folio refcount when splicing from sockets"
- Link to v2: https://patch.msgid.link/20261107-refcount_overflow_stable-v2-0-c38f6809b106@kernel.org
Changes in v2:
- "mm: limit pagecache folio refcount when mapping pages"
- Try not to fail fork() by skipping the PTE/PUD instead. We still have
to fail in two scenarios.
- Split helper into fast and slow path. Slow path in mm/util.c,
exported for Fuse usage in patch #2.
- Check has_capability_noaudit(current, CAP_SYS_RESOURCE) instead of
capable(CAP_SYS_RESOURCE)
- Extended patch description and improved function documentation.
- Dropped RBs due to changes.
- "mm: limit pagecache folio refcount when splicing into pipes"
- Return -EOVERFLOW; from filemap_splice_read() and
shmem_file_splice_read(), and handle it in do_splice_read() by falling
back to a copy.
- Check in fuse_copy_folio() as well
- Updated patch description and updated+improved comments/doc
- Dropped RB due to changes
- "mm/mmu_gather: flush pending references early before the folio limit"
- Adjust some comments
- Update Fixes:
- Extend patch description
- "mm/gup: limit pagecache folio refcount when grabbing references"
- Keep all WARN_ON_ONCE() checks for negative/null refcounts
- Skip has_capability_noaudit(current, CAP_SYS_RESOURCE) with disabled
interrupts, skipping it for GUP-fast.
- Properly forward try_grab_folio() errors
- Adjust some comments
- Link to v1: https://patch.msgid.link/20261105-refcount_overflow_stable-v1-0-70b9c655da3e@kernel.org
To: David Hildenbrand <david@kernel.org>
To: Lorenzo Stoakes <ljs@kernel.org>
To: "Liam R. Howlett" <liam@infradead.org>
To: Vlastimil Babka <vbabka@kernel.org>
To: Mike Rapoport <rppt@kernel.org>
To: Suren Baghdasaryan <surenb@google.com>
To: Michal Hocko <mhocko@suse.com>
To: "Matthew Wilcox (Oracle)" <willy@infradead.org>
To: Jan Kara <jack@suse.cz>
To: Andrew Morton <akpm@linux-foundation.org>
To: Zi Yan <ziy@nvidia.com>
To: Baolin Wang <baolin.wang@linux.alibaba.com>
To: Nico Pache <nico.pache@linux.dev>
To: Ryan Roberts <ryan.roberts@arm.com>
To: Dev Jain <dev.jain@arm.com>
To: Barry Song <baohua@kernel.org>
To: Lance Yang <lance.yang@linux.dev>
To: Usama Arif <usama.arif@linux.dev>
To: Kiryl Shutsemau <kas@kernel.org>
To: Matthew Brost <matthew.brost@intel.com>
To: Joshua Hahn <joshua.hahnjy@gmail.com>
To: Byungchul Park <byungchul@sk.com>
To: Gregory Price <gourry@gourry.net>
To: Ying Huang <ying.huang@linux.alibaba.com>
To: Alistair Popple <apopple@nvidia.com>
To: Peter Xu <peterx@redhat.com>
To: Johannes Weiner <hannes@cmpxchg.org>
To: Song Liu <songliubraving@fb.com>
To: Rik van Riel <riel@surriel.com>
To: Miklos Szeredi <miklos@szeredi.hu>
To: Alexander Viro <viro@zeniv.linux.org.uk>
To: Christian Brauner <brauner@kernel.org>
To: Hugh Dickins <hughd@google.com>
To: Will Deacon <will@kernel.org>
To: "Aneesh Kumar K.V" <aneesh.kumar@kernel.org>
To: Nick Piggin <npiggin@gmail.com>
To: Peter Zijlstra <peterz@infradead.org>
To: Arnd Bergmann <arnd@arndb.de>
To: Jason Gunthorpe <jgg@ziepe.ca>
To: Kevin Tian <kevin.tian@intel.com>
To: "Joerg Roedel (AMD)" <joro@8bytes.org>
To: Robin Murphy <robin.murphy@arm.com>
To: Steve Sistare <steven.sistare@oracle.com>
To: John Hubbard <jhubbard@nvidia.com>
To: Jose A. Perez de Azpillaga <azpijr@gmail.com>
To: Josef Bacik <josef@toxicpanda.com>
To: "David S. Miller" <davem@davemloft.net>
To: Eric Dumazet <edumazet@kernel.org>
To: Jakub Kicinski <kuba@kernel.org>
To: Paolo Abeni <pabeni@redhat.com>
To: Simon Horman <horms@kernel.org>
To: Neal Cardwell <ncardwell@google.com>
To: Kuniyuki Iwashima <kuniyu@google.com>
Cc: linux-mm@kvack.org
Cc: linux-kernel@vger.kernel.org
Cc: linux-fsdevel@vger.kernel.org
Cc: fuse-devel@lists.linux.dev
Cc: linux-arch@vger.kernel.org
Cc: iommu@lists.linux.dev
Cc: netdev@vger.kernel.org
---
David Hildenbrand (Arm) (9):
mm: limit pagecache folio refcount when mapping pages
mm: limit pagecache folio refcount when splicing into pipes
mm/mmu_gather: flush pending references early before the folio limit
iommufd: fix cleanup when adding memfd folio pins fails
mm/gup: limit pagecache folio refcount when grabbing references
net: limit folio refcount when copying skb frag references
net: limit folio refcount when rearranging skb frags
net: limit folio refcount when splicing pages into skbs
net: limit folio refcount when splicing from sockets
drivers/iommu/iommufd/pages.c | 15 +++-
fs/fuse/dev.c | 4 +-
include/asm-generic/tlb.h | 4 +-
include/linux/mm.h | 89 ++++++++++++++++++----
include/linux/skbuff_ref.h | 53 +++++++++++++
mm/filemap.c | 28 ++++---
mm/gup.c | 32 +++++---
mm/huge_memory.c | 32 +++++++-
mm/internal.h | 4 +-
mm/memory.c | 41 ++++++++--
mm/migrate.c | 11 +++
mm/mmu_gather.c | 10 ++-
mm/shmem.c | 7 +-
mm/userfaultfd.c | 4 +
mm/util.c | 26 +++++++
net/core/skbuff.c | 169 ++++++++++++++++++++++++++----------------
net/ipv4/tcp_output.c | 8 +-
17 files changed, 411 insertions(+), 126 deletions(-)
---
base-commit: 2683df3b7eac1d91380194955ad0c9751454610c
change-id: 20261105-refcount_overflow_stable-542e024130fe
Best regards,
--
David Hildenbrand (Arm) <david@kernel.org>
[PATCH v4 5/9] mm/gup: limit pagecache folio refcount when grabbing references
Author: "David Hildenbrand (Arm)"
Date: Thu, 12 Nov 2026 22:53
Date: Thu, 12 Nov 2026 22:53
129 lines
13010 bytes
13010 bytes
Let's add __folio_ref_count_over_limit() checks in fast and slow GUP
before acquiring additional references or pins.
Keep it simple and use the check for all folio types.
Also propagate try_grab_folio() errors when grabbing additional
references in __get_user_pages() and in memfd_pin_folios(), so hitting
the limit returns -ENOMEM instead of -EFAULT and -EINVAL, respectively.
Keep the existing invalid-refcount warnings.
Fixes: 800d8c63b2e9 ("shmem: add huge pages support")
Fixes: 99cb0dbd47a1 ("mm,thp: add read-only THP support for (non-shmem) FS")
Fixes: 793917d997df ("mm/readahead: Add large folio readahead")
Cc: stable@vger.kernel.org
Reviewed-by: Jose A. Perez de Azpillaga <azpijr@gmail.com>
Reviewed-by: Pedro Falcato <pfalcato@suse.de>
Signed-off-by: David Hildenbrand (Arm) <david@kernel.org>
---
include/linux/mm.h | 11 +++++------
mm/gup.c | 32 +++++++++++++++++++++-----------
2 files changed, 26 insertions(+), 17 deletions(-)
diff --git a/include/linux/mm.h b/include/linux/mm.h
index 0d00fc1838a01..cc6f1e69223aa 100644
--- a/include/linux/mm.h
+++ b/include/linux/mm.h
@@ -2246,6 +2246,9 @@ bool folio_ref_count_over_limit_slowpath(const struct folio *folio,
* to pagecache folios (!hugetlb), either before acquiring references or
* afterwards with rollback if the operation is refused.
*
+ * Also required when pinning folios without a dedicated pincount that use
+ * GUP_PIN_COUNTING_BIAS.
+ *
* Checking other folios does no harm.
*
* This check is an approximation; we leave some headroom such that concurrent
@@ -2967,12 +2970,8 @@ static inline bool folio_maybe_dma_pinned(struct folio *folio)
return atomic_read(&folio->_pincount) > 0;
/*
- * folio_ref_count() is signed. If that refcount overflows, then
- * folio_ref_count() returns a negative value, and callers will avoid
- * further incrementing the refcount.
- *
- * Here, for that overflow case, use the sign bit to count a little
- * bit higher via unsigned math, and thus still get an accurate result.
+ * Use unsigned math so even an unexpected negative refcount is
+ * treated as possibly pinned.
*/
return ((unsigned int)folio_ref_count(folio)) >=
GUP_PIN_COUNTING_BIAS;
diff --git a/mm/gup.c b/mm/gup.c
index ebbfee09b7278..c1b8ec3513913 100644
--- a/mm/gup.c
+++ b/mm/gup.c
@@ -74,10 +74,14 @@ static inline void sanity_check_pinned_pages(struct page **pages,
static inline struct folio *try_get_folio(struct page *page, int refs)
{
struct folio *folio;
+ int ref_count;
retry:
folio = page_folio(page);
- if (WARN_ON_ONCE(folio_ref_count(folio) < 0))
+ ref_count = folio_ref_count(folio);
+ if (WARN_ON_ONCE(ref_count < 0))
+ return NULL;
+ if (unlikely(__folio_ref_count_over_limit(folio, ref_count)))
return NULL;
if (unlikely(!folio_ref_try_add(folio, refs)))
return NULL;
@@ -140,7 +144,13 @@ static void gup_put_folio(struct folio *folio, int refs, unsigned int flags)
int __must_check try_grab_folio(struct folio *folio, int refs,
unsigned int flags)
{
- if (WARN_ON_ONCE(folio_ref_count(folio) <= 0))
+ const int ref_count = folio_ref_count(folio);
+
+ if (WARN_ON_ONCE(ref_count <= 0))
+ return -ENOMEM;
+
+ if ((flags & (FOLL_GET | FOLL_PIN)) &&
+ unlikely(__folio_ref_count_over_limit(folio, ref_count)))
return -ENOMEM;
if (unlikely(!(flags & FOLL_PCI_P2PDMA) && folio_is_pci_p2pdma(folio)))
@@ -1482,17 +1492,17 @@ static long __get_user_pages(struct mm_struct *mm,
struct folio *folio = page_folio(page);
/*
- * Since we already hold refcount on the
- * large folio, this should never fail.
+ * Grabbing more references can fail once we
+ * hit folio refcount limits.
*/
- if (try_grab_folio(folio, page_increm - 1,
- gup_flags)) {
+ ret = try_grab_folio(folio, page_increm - 1,
+ gup_flags);
+ if (ret) {
/*
- * Release the 1st page ref if the
- * folio is problematic, fail hard.
+ * Release the folio reference we
+ * already took.
*/
gup_put_folio(folio, 1, gup_flags);
- ret = -EFAULT;
goto out;
}
}
@@ -3541,10 +3551,10 @@ long memfd_pin_folios(struct file *memfd, loff_t start, loff_t end,
break;
}
- if (try_grab_folio(folio, 1, FOLL_PIN)) {
+ ret = try_grab_folio(folio, 1, FOLL_PIN);
+ if (ret) {
folio_unlock(folio);
folio_batch_release(&fbatch);
- ret = -EINVAL;
goto err;
}
--
2.56.0
Re: (subset) [PATCH v4 0/9] mm: limit pagecache folio refcount
Author: "David Hildenbrand (Arm)"
Date: Fri, 13 Nov 2026 10:52
Date: Fri, 13 Nov 2026 10:52
48 lines
9483 bytes
9483 bytes
On Thu, 12 Nov 2026 23:53:19 +0100, David Hildenbrand (Arm) wrote:
> I'm resending this quite quickly, addressing Pedro's review (thanks!!!),
> so I can replace the version in -next tomorrow and move forward with
> this to get it fixed.
>
> I un-RFC'ed the -net patches, but I will not pick them up. As mentioned
> below, they can go in independently.
>
> [...]
Okay, updating the first 5 patches in -next.
--
Applied, thanks!
tree: mm/core.git
branch: for-next-fixes
Applied patches will appear soon in mm-next, followed by linux-next.
Please report any outstanding bugs that were missed during review, so we
can either drop the patch series or decide how to fix it up.
It is encouraged to provide Acked-bys, Reviewed-bys, and Tested-bys even
though the patches have already been applied. If possible, patch trailers
will be updated.
Note that commit hashes shown below are subject to change due to rebase,
trailer updates or similar. Further, this is not a guarantee that the
patches will go upstream. If in doubt, please check the listed branch.
[1/9] mm: limit pagecache folio refcount when mapping pages
https://git.kernel.org/mm/core/c/86c0d919
[2/9] mm: limit pagecache folio refcount when splicing into pipes
https://git.kernel.org/mm/core/c/6ab934de
[3/9] mm/mmu_gather: flush pending references early before the folio limit
https://git.kernel.org/mm/core/c/89b412c7
[4/9] iommufd: fix cleanup when adding memfd folio pins fails
https://git.kernel.org/mm/core/c/35e8fbf9
[5/9] mm/gup: limit pagecache folio refcount when grabbing references
https://git.kernel.org/mm/core/c/a6f95654
--
Cheers,
David
Thread Navigation
This is a paginated view of messages in the thread with full content displayed inline.
Messages are displayed in chronological order, with the original post highlighted in green.
Use pagination controls to navigate through all messages in large threads.
Back to All Threads