Skip to content

mm: limit pagecache folio refcount

David Hildenbrand (Arm) fuse-devel linux-arch linux-fsdevel linux-iommu linux-mm lkml netdev stable 4 versions – Applied

Status from this message: Applied, thanks!

Get series with b4
b4 am [email protected] change-id 20261105-refcount_overflow_stable-542e024130fe

Versions newest first

  1. v4 Latest9 patches 4 repliesApplied
  2. v39 patches 22 repliesApplied
  3. v25 patches 13 repliesSuperseded
Earlier versions · 1
VersionPostedPatchesRepliesStatus
v15 patches25 repliesSuperseded

Changes in v4 from the cover letter

- 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/[email protected]

Patches in v4 Applied

#Subject and filesSizeRepliesApplied
1/9mm: limit pagecache folio refcount when mapping pagesinclude/linux/mm.h mm/filemap.c mm/huge_memory.c +4 more+138 -103 replies
2/9mm: limit pagecache folio refcount when splicing into pipesfs/fuse/dev.c include/linux/mm.h mm/filemap.c +2 more+67 -230 replies
3/9mm/mmu_gather: flush pending references early before the folio limitinclude/asm-generic/tlb.h include/linux/mm.h mm/memory.c +1 more+11 -80 replies
4/9iommufd: fix cleanup when adding memfd folio pins failsdrivers/iommu/iommufd/pages.c+11 -40 replies
5/9mm/gup: limit pagecache folio refcount when grabbing referencesinclude/linux/mm.h mm/gup.c+26 -170 replies
6/9net: limit folio refcount when copying skb frag referencesinclude/linux/skbuff_ref.h net/core/skbuff.c+76 -120 replies
7/9net: limit folio refcount when rearranging skb fragsnet/core/skbuff.c net/ipv4/tcp_output.c+26 -140 replies
8/9net: limit folio refcount when splicing pages into skbsnet/core/skbuff.c+9 -30 replies
9/9net: limit folio refcount when splicing from socketsnet/core/skbuff.c+51 -390 replies

Reviews trailers per patch

All 9 patches have a Reviewed-by or Acked-by.

PatchJose A. Perez de AzpillagaZi YanPedro FalcatoLance YangJason Gunthorpe
1/9R-b A-b R-b
2/9R-b R-b
3/9R-b R-b R-b
4/9R-b R-b
5/9R-b R-b
6/9A-b
7/9A-b
8/9A-b
9/9A-b

Trailers given in replies are filled; those carried in the patches from earlier versions are outlined with dashes. Scroll the table sideways for all 5 reviewers.

Read the v4 thread

Cover letter [PATCH v4 0/9] mm: limit pagecache folio refcount

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.

---
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/[email protected]

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/[email protected]

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/[email protected]

To: David Hildenbrand <[email protected]>
To: Lorenzo Stoakes <[email protected]>
To: "Liam R. Howlett" <[email protected]>
To: Vlastimil Babka <[email protected]>
To: Mike Rapoport <[email protected]>
To: Suren Baghdasaryan <[email protected]>
To: Michal Hocko <[email protected]>
To: "Matthew Wilcox (Oracle)" <[email protected]>
To: Jan Kara <[email protected]>
To: Andrew Morton <[email protected]>
To: Zi Yan <[email protected]>
To: Baolin Wang <[email protected]>
To: Nico Pache <[email protected]>
To: Ryan Roberts <[email protected]>
To: Dev Jain <[email protected]>
To: Barry Song <[email protected]>
To: Lance Yang <[email protected]>
To: Usama Arif <[email protected]>
To: Kiryl Shutsemau <[email protected]>
To: Matthew Brost <[email protected]>
To: Joshua Hahn <[email protected]>
To: Byungchul Park <[email protected]>
To: Gregory Price <[email protected]>
To: Ying Huang <[email protected]>
To: Alistair Popple <[email protected]>
To: Peter Xu <[email protected]>
To: Johannes Weiner <[email protected]>
To: Song Liu <[email protected]>
To: Rik van Riel <[email protected]>
To: Miklos Szeredi <[email protected]>
To: Alexander Viro <[email protected]>
To: Christian Brauner <[email protected]>
To: Hugh Dickins <[email protected]>
To: Will Deacon <[email protected]>
To: "Aneesh Kumar K.V" <[email protected]>
To: Nick Piggin <[email protected]>
To: Peter Zijlstra <[email protected]>
To: Arnd Bergmann <[email protected]>
To: Jason Gunthorpe <[email protected]>
To: Kevin Tian <[email protected]>
To: "Joerg Roedel (AMD)" <[email protected]>
To: Robin Murphy <[email protected]>
To: Steve Sistare <[email protected]>
To: John Hubbard <[email protected]>
To: Jose A. Perez de Azpillaga <[email protected]>
To: Josef Bacik <[email protected]>
To: "David S. Miller" <[email protected]>
To: Eric Dumazet <[email protected]>
To: Jakub Kicinski <[email protected]>
To: Paolo Abeni <[email protected]>
To: Simon Horman <[email protected]>
To: Neal Cardwell <[email protected]>
To: Kuniyuki Iwashima <[email protected]>
Cc: [email protected]
Cc: [email protected]
Cc: [email protected]
Cc: [email protected]
Cc: [email protected]
Cc: [email protected]
Cc: [email protected]

---
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,

Signature · 1 line
David Hildenbrand (Arm) <[email protected]>