Skip to contentSkip to conversation

mm: limit pagecache folio refcount

PATCH Superseded David Hildenbrand (Arm) 7 participants 33 messages Nov 5 – Nov 6

Subsystems (14)

v1 of 4 Superseded Latest is v4 →

lore
Share & tools
lore

David Hildenbrand (Arm)

author maintainer
#0 lore raw

cover mm: limit pagecache folio refcount

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 really only relevant for large folios / thps in the page cache. With anon folios (including THPs), we should not be
able to trigger folio refcount overflows (until we get PUD-sized ones ;) ).

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 reover a problematic system). We still leave some headroom to allow for some slight limit overshooting, to make the implementation easier.

Then, we also check when splicing pagecache folios to a pipe, and improve our existing checks in gup. 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).

This is only a temporary solution for upstream+stable, until we have something better. These fixes will be a bit challenging to backport to stable, but it will be much easier compared to the RFC series I have here were we do things properly by (what we discussed a couple of times earlier):

- 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

I'll polish that up and send it once we merged something that can be backported to stable.

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.

---
David Hildenbrand (Arm) (5):
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

 drivers/iommu/iommufd/pages.c | 15 +++++++++++----
 include/linux/mm.h            | 43 ++++++++++++++++++++++++++++++++++++++++---
 mm/filemap.c                  | 10 ++++++++++
 mm/gup.c                      | 12 ++++++++----
 mm/huge_memory.c              | 24 ++++++++++++++++++++++--
 mm/memory.c                   | 27 ++++++++++++++++++++++-----
 mm/migrate.c                  | 11 +++++++++++
 mm/mmu_gather.c               |  5 +++++
 mm/shmem.c                    |  4 ++++
 mm/userfaultfd.c              |  4 ++++
 10 files changed, 137 insertions(+), 18 deletions(-)

---

base-commit: 02f72aa35c5e7dff443c62cd9964686396c8b201

change-id: 20261105-refcount_overflow_stable-542e024130fe

--

Cheers,

David

author maintainer
#1 lore raw

PATCH 1/5 mm: limit pagecache folio refcount when mapping pages

With large file folios, overflowing the folio mapcount and refcount became possible. For anon folios it is currently not really an issue:
each large anon folio can only be mapped once into a process.

That's not really big news, but it's about time that we do something about it because some modern technology might find ways to abuse it :)

One of the enablers for that was commit ddc58f27f9ee ("mm: drop tail page refcounting"), however, file thp support was added later.

File folios can be mapped multiple times into the same process, allowing for processes to hold many references to the same folio.

As a short-term solution, let's introduce two folio refcount limits:
one for ordinary consumers and one for admins. Leave some headroom before the refcount goes negative, which we really don't want (having the mapcount go negative is also not expected).

Start reporting SIGBUS if we run into limits. When restoring mappings from migration entries, instead zap the pte/pmd entry. We allow for slightly exceeding the limit to keep things simpler.

This is far from ideal, but better than all easy alternatives we can backport comfortably. For now it fixes the big problem: letting the refcount (+mapcount) go negative and cause trouble.

This is only a temporary fix, we'll follow up with something better.

While the refcount checks are not required for order 0 folios, let's just add them for consistency: each folio_add_file_rmap*() and folio_dup_file_rmap*() needs a prior check.

Also, just document -EHWPOISON for copy_present_ptes() while at it.

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")
---
 include/linux/mm.h | 33 +++++++++++++++++++++++++++++++++
 mm/filemap.c       |  6 ++++++
 mm/huge_memory.c   | 24 ++++++++++++++++++++++--
 mm/memory.c        | 23 ++++++++++++++++++++---
 mm/migrate.c       | 11 +++++++++++
 mm/userfaultfd.c   |  4 ++++
 6 files changed, 96 insertions(+), 5 deletions(-)
include/linux/mm.h +33 -0
index a2dd76cc5bbfe..79ae8fc0d6bb4 100644
@@ -8,6 +8,7 @@
 #include <linux/gfp.h>
 #include <linux/pgalloc_tag.h>
 #include <linux/bug.h>
+#include <linux/capability.h>
 #include <linux/list.h>
 #include <linux/mmzone.h>
 #include <linux/rbtree.h>
@@ -2228,6 +2229,38 @@ vm_fault_t finish_fault(struct vm_fault *vmf);
 #define folio_ref_zero_or_close_to_overflow(folio) \
 	((unsigned int) folio_ref_count(folio) + 127u <= 127u)
 
+/* Leave separate reserves for administrators and concurrent reference gains. */
+#define FOLIO_REF_COUNT_LIMIT		(INT_MAX - (1U << 26))
+#define FOLIO_REF_COUNT_ADMIN_LIMIT	(INT_MAX - (1U << 25))
+
+/**
+ * folio_ref_count_over_limit - Check whether to refuse more folio references.
+ * @folio: The folio.
+ *
+ * Relevant for large pagecache folios (!hugetlb). For these, we have to check
+ * before adding page table mappings or other significant refcount users whether
+ * we are close to our folio ref count limit, to avoid overflows.
+ *
+ * Checking this for other folios does no harm but is mostly unnecessary.
+ *
+ * This check is an approximation; we leave some headroom such that concurrent
+ * increments, batches and temporary references are not a problem.
+ *
+ * Return: True if the caller should refuse additional references, false
+ * otherwise.
+ */
+static inline bool folio_ref_count_over_limit(const struct folio *folio)
+{
+	const unsigned int ref_count = folio_ref_count(folio);
+
+	if (likely(ref_count < FOLIO_REF_COUNT_LIMIT))
+		return false;
+	if (ref_count >= FOLIO_REF_COUNT_ADMIN_LIMIT)
+		return true;
+	/* Background threads must not consume admin reserves. */
+	return (current->flags & PF_KTHREAD) || !capable(CAP_SYS_RESOURCE);
+}
+
 /**
  * folio_get - Increment the reference count on a folio.
  * @folio: The folio.
mm/filemap.c +6 -0
index 86297841f9955..920bbf86dfdff 100644
@@ -3968,6 +3968,11 @@ vm_fault_t filemap_map_pages(struct vm_fault *vmf,
 		unsigned long end;
 		vm_fault_t map_ret;
 
+		if (unlikely(folio_ref_count_over_limit(folio))) {
+			folio_ref_dec(folio);
+			goto next;
+		}
+
 		addr += (xas.xa_index - last_pgoff) << PAGE_SHIFT;
 		vmf->pte += xas.xa_index - last_pgoff;
 		last_pgoff = xas.xa_index;
@@ -4009,6 +4014,7 @@ vm_fault_t filemap_map_pages(struct vm_fault *vmf,
 					   mmap_miss - 1);
 		}
 
+next:
 		folio_unlock(folio);
 	} while ((folio = next_uptodate_folio(&xas, mapping, end_pgoff)) != NULL);
 	add_mm_counter(vma->vm_mm, folio_type, rss);
mm/huge_memory.c +22 -2
index d0e19d9e6b651..2596ff0f0e185 100644
@@ -1626,6 +1626,7 @@ static vm_fault_t insert_pmd(struct vm_area_struct *vma, unsigned long addr,
 {
 	struct mm_struct *mm = vma->vm_mm;
 	pgtable_t pgtable = NULL;
+	vm_fault_t ret = VM_FAULT_NOPAGE;
 	spinlock_t *ptl;
 	pmd_t entry;
 
@@ -1662,6 +1663,10 @@ static vm_fault_t insert_pmd(struct vm_area_struct *vma, unsigned long addr,
 		if (is_huge_zero_folio(fop.folio)) {
 			entry = pmd_mkspecial(entry);
 		} else {
+			if (unlikely(folio_ref_count_over_limit(fop.folio))) {
+				ret = VM_FAULT_SIGBUS;
+				goto out_unlock;
+			}
 			folio_get(fop.folio);
 			folio_add_file_rmap_pmd(fop.folio, &fop.folio->page, vma);
 			add_mm_counter(mm, mm_counter_file(fop.folio), HPAGE_PMD_NR);
@@ -1688,7 +1693,7 @@ static vm_fault_t insert_pmd(struct vm_area_struct *vma, unsigned long addr,
 	spin_unlock(ptl);
 	if (pgtable)
 		pte_free(mm, pgtable);
-	return VM_FAULT_NOPAGE;
+	return ret;
 }
 
 /**
@@ -1759,6 +1764,7 @@ static vm_fault_t insert_pud(struct vm_area_struct *vma, unsigned long addr,
 		pud_t *pud, struct folio_or_pfn fop, pgprot_t prot, bool write)
 {
 	struct mm_struct *mm = vma->vm_mm;
+	vm_fault_t ret = VM_FAULT_NOPAGE;
 	spinlock_t *ptl;
 	pud_t entry;
 
@@ -1784,6 +1790,10 @@ static vm_fault_t insert_pud(struct vm_area_struct *vma, unsigned long addr,
 	if (fop.is_folio) {
 		entry = folio_mk_pud(fop.folio, vma->vm_page_prot);
 
+		if (unlikely(folio_ref_count_over_limit(fop.folio))) {
+			ret = VM_FAULT_SIGBUS;
+			goto out_unlock;
+		}
 		folio_get(fop.folio);
 		folio_add_file_rmap_pud(fop.folio, &fop.folio->page, vma);
 		add_mm_counter(mm, mm_counter_file(fop.folio), HPAGE_PUD_NR);
@@ -1799,7 +1809,7 @@ static vm_fault_t insert_pud(struct vm_area_struct *vma, unsigned long addr,
 	update_mmu_cache_pud(vma, addr, pud);
 out_unlock:
 	spin_unlock(ptl);
-	return VM_FAULT_NOPAGE;
+	return ret;
 }
 
 /**
@@ -5215,6 +5225,16 @@ void remove_migration_pmd(struct page_vma_mapped_walk *pvmw, struct folio *folio
 	if (!(pvmw->pmd && !pvmw->pte))
 		return;
 
+	if (!folio_test_anon(folio) && unlikely(folio_ref_count_over_limit(folio))) {
+		/* Leave the file mapping absent so it can be faulted in. */
+		pmd_clear(pvmw->pmd);
+		if (arch_needs_pgtable_deposit())
+			zap_deposited_table(mm, pvmw->pmd);
+		update_hiwater_rss(mm);
+		add_mm_counter(mm, mm_counter_file(folio), -HPAGE_PMD_NR);
+		return;
+	}
+
 	entry = softleaf_from_pmd(*pvmw->pmd);
 	folio_get(folio);
 	pmde = folio_mk_pmd(folio, READ_ONCE(vma->vm_page_prot));
mm/memory.c +20 -3
index becf53a2a0944..4800a0da14a32 100644
@@ -1168,6 +1168,8 @@ static __always_inline void __copy_present_ptes(struct vm_area_struct *dst_vma,
  * consecutive pages of the same folio by copying them as well.
  *
  * Returns -EAGAIN if one preallocated page is required to copy the next PTE.
+ * Returns -ENOMEM if the file folio refcount is too high.
+ * Returns -EHWPOISON if copying a page fails.
  * Otherwise, returns the number of copied PTEs (at least 1).
  */
 static inline int
@@ -1186,6 +1188,9 @@ copy_present_ptes(struct vm_area_struct *dst_vma, struct vm_area_struct *src_vma
 
 	folio = page_folio(page);
 
+	if (!folio_test_anon(folio) && unlikely(folio_ref_count_over_limit(folio)))
+		return -ENOMEM;
+
 	/*
 	 * If we likely have to copy, just don't bother with batching. Make
 	 * sure that the common "small folio" case is as fast as possible
@@ -1367,9 +1372,9 @@ copy_pte_range(struct vm_area_struct *dst_vma, struct vm_area_struct *src_vma,
 		/*
 		 * If we need a pre-allocated page for this pte, drop the
 		 * locks, allocate, and try again.
-		 * If copy failed due to hwpoison in source page, break out.
+		 * Other errors abort the copy after releasing the page table locks.
 		 */
-		if (unlikely(ret == -EAGAIN || ret == -EHWPOISON))
+		if (unlikely(ret < 0))
 			break;
 		if (unlikely(prealloc)) {
 			/*
@@ -1399,7 +1404,7 @@ copy_pte_range(struct vm_area_struct *dst_vma, struct vm_area_struct *src_vma,
 			goto out;
 		}
 		entry.val = 0;
-	} else if (ret == -EBUSY || unlikely(ret == -EHWPOISON)) {
+	} else if (ret == -EBUSY || ret == -ENOMEM || unlikely(ret == -EHWPOISON)) {
 		goto out;
 	} else if (ret ==  -EAGAIN) {
 		prealloc = folio_prealloc(src_mm, src_vma, addr, false);
@@ -2469,6 +2474,8 @@ static int insert_page_into_pte_locked(struct vm_area_struct *vma, pte_t *pte,
 	if (unlikely(is_zero_folio(folio))) {
 		pteval = pte_mkspecial(pteval);
 	} else {
+		if (unlikely(folio_ref_count_over_limit(folio)))
+			return -EOVERFLOW;
 		folio_get(folio);
 		pteval = mk_pte(page, prot);
 		if (mkwrite) {
@@ -5717,6 +5724,9 @@ vm_fault_t do_set_pmd(struct vm_fault *vmf, struct folio *folio, struct page *pa
 	if (unlikely(folio_test_has_hwpoisoned(folio)))
 		return ret;
 
+	if (unlikely(folio_ref_count_over_limit(folio)))
+		return VM_FAULT_SIGBUS;
+
 	/*
 	 * Archs like ppc64 need additional space to store information
 	 * related to pte entry. Use the preallocated table for that.
@@ -5771,6 +5781,9 @@ vm_fault_t do_set_pmd(struct vm_fault *vmf, struct folio *folio, struct page *pa
  * @page: The first page to create a PTE for.
  * @nr: The number of PTEs to create.
  * @addr: The first address to create a PTE for.
+ *
+ * For file mappings, the caller must check folio_ref_count_over_limit()
+ * before adding the mapping references and installing PTEs.
  */
 void set_pte_range(struct vm_fault *vmf, struct folio *folio,
 		struct page *page, unsigned int nr, unsigned long addr)
@@ -5937,6 +5950,10 @@ vm_fault_t finish_fault(struct vm_fault *vmf)
 		goto fallback;
 	}
 
+	if (!is_cow && unlikely(folio_ref_count_over_limit(folio))) {
+		ret = VM_FAULT_SIGBUS;
+		goto unlock;
+	}
 	folio_ref_add(folio, nr_pages - 1);
 	set_pte_range(vmf, folio, page, nr_pages, addr);
 	type = is_cow ? MM_ANONPAGES : mm_counter_file(folio);
mm/migrate.c +11 -0
index 6a96e3136fb2a..ca7471aa7a76a 100644
@@ -385,6 +385,17 @@ static bool remove_migration_pte(struct folio *folio,
 		    try_to_map_unused_to_zeropage(&pvmw, folio, old_pte, idx))
 			continue;
 
+		if (!folio_test_anon(folio) && !folio_test_hugetlb(folio) &&
+		    unlikely(folio_ref_count_over_limit(folio))) {
+			/* File contents remain available for a later fault. */
+			pte_clear(vma->vm_mm, pvmw.address, pvmw.pte);
+			cond_install_uffd_wp_ptes(vma, pvmw.address,
+						  pvmw.pte, old_pte, 1);
+			update_hiwater_rss(vma->vm_mm);
+			dec_mm_counter(vma->vm_mm, mm_counter_file(folio));
+			continue;
+		}
+
 		folio_get(folio);
 		new = folio_page(folio, idx);
 		pte = mk_pte(new, READ_ONCE(vma->vm_page_prot));
mm/userfaultfd.c +4 -0
index 169f681f9849b..89853c157fa91 100644
@@ -373,6 +373,10 @@ static int mfill_atomic_install_pte(pmd_t *dst_pmd,
 		goto out_unlock;
 
 	if (page_in_cache) {
+		if (unlikely(folio_ref_count_over_limit(folio))) {
+			ret = -ENOMEM;
+			goto out_unlock;
+		}
 		folio_add_file_rmap_pte(folio, page, dst_vma);
 	} else {
 		folio_add_new_anon_rmap(folio, dst_vma, dst_addr, RMAP_EXCLUSIVE);
Signature · 1 line
2.43.0
author maintainer
#11 lore raw

PATCH 2/5 mm: limit pagecache folio refcount when splicing into pipes

In reply to David Hildenbrand (Arm)

Similar to mapping pagecache folios into page tables, let's reject splicing folios into a pipe if the refcount is at it's limit, similar to how we handle it during GUP.

This will prevent more cases of possible folio refcount overflows with large pagecache folios.

Let's keep it simple and check in the two callers of splice_folio_into_pipe().

Also check in try_get_page() to cover pipe-buffer duplication by tee() and partial pipe-to-pipe splicing.

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")
---
 include/linux/mm.h | 9 ++++++---
 mm/filemap.c       | 4 ++++
 mm/shmem.c         | 4 ++++
 3 files changed, 14 insertions(+), 3 deletions(-)
include/linux/mm.h +6 -3
index 79ae8fc0d6bb4..a82cfbce18215 100644
@@ -2287,10 +2287,13 @@ static inline void get_page(struct page *page)
 
 static inline __must_check bool try_get_page(struct page *page)
 {
-	page = compound_head(page);
-	if (WARN_ON_ONCE(page_ref_count(page) <= 0))
+	struct folio *folio = page_folio(page);
+
+	if (WARN_ON_ONCE(folio_ref_count(folio) <= 0))
 		return false;
-	page_ref_inc(page);
+	if (unlikely(folio_ref_count_over_limit(folio)))
+		return false;
+	folio_ref_inc(folio);
 	return true;
 }
 
mm/filemap.c +4 -0
index 920bbf86dfdff..0f2ec790a5b65 100644
@@ -3168,6 +3168,10 @@ ssize_t filemap_splice_read(struct file *in, loff_t *ppos,
 			if (writably_mapped)
 				flush_dcache_folio(folio);
 
+			if (unlikely(folio_ref_count_over_limit(folio))) {
+				error = -ENOMEM;
+				goto out;
+			}
 			n = min_t(loff_t, len, isize - *ppos);
 			n = splice_folio_into_pipe(pipe, folio, *ppos, n);
 			if (!n)
mm/shmem.c +4 -0
index cda1a0f7db99d..1c0f6bceac486 100644
@@ -3819,6 +3819,10 @@ static ssize_t shmem_file_splice_read(struct file *in, loff_t *ppos,
 			 * Ok, we have the page, and it's up-to-date, so we can
 			 * now splice it into the pipe.
 			 */
+			if (unlikely(folio_ref_count_over_limit(folio))) {
+				error = -ENOMEM;
+				break;
+			}
 			n = splice_folio_into_pipe(pipe, folio, *ppos, part);
 			folio_put(folio);
 			folio = NULL;
Signature · 1 line
2.43.0
author maintainer
#19 lore raw

PATCH 3/5 mm/mmu_gather: flush pending references early before the folio limit

In reply to David Hildenbrand (Arm)

When a task zaps mappings (e.g., MADV_DONTNEED), it can drop the PT lock and defer dropping the folio references. In the meantime, page faults can refault pagecache folios, creating more references for the same folios.

Let's just request a flush after enqueueing a pagecache folio whose refcount reached a certain threshold. Start flushing well before new references are refused, regardless of the caller's privileges.

This is a temporary measure until we have something better.

Fixes 10ebac4f95e7 ("mm/memory: optimize unmap/zap with PTE-mapped THP")
---
 include/linux/mm.h | 1 +
 mm/memory.c        | 4 ++--
 mm/mmu_gather.c    | 5 +++++
 3 files changed, 8 insertions(+), 2 deletions(-)
include/linux/mm.h +1 -0
index a82cfbce18215..87f7646a7f3ba 100644
@@ -2232,6 +2232,7 @@ vm_fault_t finish_fault(struct vm_fault *vmf);
 /* Leave separate reserves for administrators and concurrent reference gains. */
 #define FOLIO_REF_COUNT_LIMIT		(INT_MAX - (1U << 26))
 #define FOLIO_REF_COUNT_ADMIN_LIMIT	(INT_MAX - (1U << 25))
+#define FOLIO_REF_COUNT_FLUSH_LIMIT	(INT_MAX - (1U << 27))
 
 /**
  * folio_ref_count_over_limit - Check whether to refuse more folio references.
mm/memory.c +2 -2
index 4800a0da14a32..d2eb313832559 100644
@@ -2061,8 +2061,8 @@ static unsigned long zap_pte_range(struct mmu_gather *tlb,
 	pte_unmap_unlock(start_pte, ptl);
 
 	/*
-	 * If we forced a TLB flush (either due to running out of
-	 * batch buffers or because we needed to flush dirty TLB
+	 * If we forced a TLB flush (either to release batched
+	 * references or because we needed to flush dirty TLB
 	 * entries before releasing the ptl), free the batched
 	 * memory too. Come back again if we didn't do everything.
 	 */
mm/mmu_gather.c +5 -0
index 9f353f0e2ef4d..5988c42cc5144 100644
@@ -190,6 +190,11 @@ static bool __tlb_remove_folio_pages_size(struct mmu_gather *tlb,
 		batch->encoded_pages[batch->nr++] = encode_page(page, flags);
 		batch->encoded_pages[batch->nr++] = encode_nr_pages(nr_pages);
 	}
+	/* Flush pending references well before new references are refused. */
+	if (unlikely((unsigned int)folio_ref_count(page_folio(page)) >=
+		     FOLIO_REF_COUNT_FLUSH_LIMIT))
+		return true;
+
 	/*
 	 * Make sure that we can always add another "page" + "nr_pages",
 	 * requiring two entries instead of only a single one.
Signature · 1 line
2.43.0
author maintainer
#22 lore raw

PATCH 4/5 iommufd: fix cleanup when adding memfd folio pins fails

In reply to David Hildenbrand (Arm)

folio_add_pins() can fail after memfd_pin_folios() succeeded pinning a folio. Upcoming folio refcount limit checks will make this easier to trigger.

Let's fix three problems in the error handling:

1. pfn_reader_release_pins() uses the allocated array size instead of the number of folios returned by memfd_pin_folios(). With large folios, cleanup can unpin uninitialized entries. Remember the number of returned folios and use that during cleanup.

2. pin_memfd_pages() returns on failure without releasing the pins acquired by the current call. Release the per-page pins for folios already expanded into the page array, and the single pins for the remaining folios.

3. pfn_reader_next() returns on failure before updating batch_end_index, even if the batch already contains successfully expanded folios. Update it before propagating the error so cleanup releases those pins as well.

Fixes 92687c793644 ("iommufd: pfn_reader for file mappings")
---
 drivers/iommu/iommufd/pages.c | 15 +++++++++++----
 1 file changed, 11 insertions(+), 4 deletions(-)
drivers/iommu/iommufd/pages.c +11 -4
index 2f6153108add7..fcdbde007a9ab 100644
@@ -781,6 +781,7 @@ struct pfn_reader_user {
 	struct file *file;
 	struct folio **ufolios;
 	size_t ufolios_len;
+	unsigned long ufolios_count;
 	unsigned long ufolios_offset;
 	struct folio **ufolios_next;
 };
@@ -800,6 +801,7 @@ static void pfn_reader_user_init(struct pfn_reader_user *user,
 	user->file = (pages->type == IOPT_ADDRESS_FILE) ? pages->file : NULL;
 	user->ufolios = NULL;
 	user->ufolios_len = 0;
+	user->ufolios_count = 0;
 	user->ufolios_next = NULL;
 	user->ufolios_offset = 0;
 }
@@ -843,6 +845,7 @@ static long pin_memfd_pages(struct pfn_reader_user *user, unsigned long start,
 
 	offset >>= PAGE_SHIFT;
 	user->ufolios_next = user->ufolios;
+	user->ufolios_count = nfolios;
 	user->ufolios_offset = offset;
 
 	for (i = 0; i < nfolios; i++) {
@@ -859,8 +862,12 @@ static long pin_memfd_pages(struct pfn_reader_user *user, unsigned long start,
 			} else {
 				int rc = folio_add_pins(folio, npin - 1);
 
-				if (rc)
+				if (rc) {
+					unpin_user_pages(user->upages,
+							 upages - user->upages);
+					unpin_folios(user->ufolios + i, nfolios - i);
 					return rc;
+				}
 
 				while (npin--)
 					*upages++ = folio_page(folio, offset++);
@@ -1251,14 +1258,14 @@ static int pfn_reader_next(struct pfn_reader *pfns)
 			return -EINVAL;
 
 		rc = pfn_reader_fill_span(pfns);
+		pfns->batch_end_index = pfns->batch_start_index +
+					pfns->batch.total_pfns;
 		if (rc)
 			return rc;
 
 		if (WARN_ON(!pfns->batch.total_pfns))
 			return -EINVAL;
 
-		pfns->batch_end_index =
-			pfns->batch_start_index + pfns->batch.total_pfns;
 		if (pfns->batch_end_index == pfns->span.last_used + 1)
 			interval_tree_double_span_iter_next(&pfns->span);
 
@@ -1320,7 +1327,7 @@ static void pfn_reader_release_pins(struct pfn_reader *pfns)
 		if (!user->file) {
 			unpin_user_pages(user->upages + start_index, npages);
 		} else {
-			long n = user->ufolios_len / sizeof(*user->ufolios);
+			const unsigned long n = user->ufolios_count;
 
 			unpin_folios(user->ufolios_next,
 				     user->ufolios + n - user->ufolios_next);
Signature · 1 line
2.43.0
author maintainer
#25 lore raw

PATCH 5/5 mm/gup: limit pagecache folio refcount when grabbing references

In reply to David Hildenbrand (Arm)

Let's replace our existing safety check by the new folio_ref_count_over_limit() check in fast and slow GUP before acquiring additional references or pins.

Keep it simple and use the check for all folio types.

Drop the WARN_ON_ONCE(), because apparently these things can currently happen with large pagecache folios.

Keep the zero-refcount rejection if the refcount is 0.

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")
---
 mm/gup.c | 12 ++++++++----
 1 file changed, 8 insertions(+), 4 deletions(-)
mm/gup.c +8 -4
index 2ab42fc6834e5..db9db6b2a4662 100644
@@ -77,7 +77,7 @@ static inline struct folio *try_get_folio(struct page *page, int refs)
 
 retry:
 	folio = page_folio(page);
-	if (WARN_ON_ONCE(folio_ref_count(folio) < 0))
+	if (unlikely(folio_ref_count_over_limit(folio)))
 		return NULL;
 	if (unlikely(!folio_ref_try_add(folio, refs)))
 		return NULL;
@@ -140,7 +140,11 @@ 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))
+	if ((flags & (FOLL_GET | FOLL_PIN)) &&
+	    unlikely(folio_ref_count_over_limit(folio)))
+		return -ENOMEM;
+
+	if (WARN_ON_ONCE(!folio_ref_count(folio)))
 		return -ENOMEM;
 
 	if (unlikely(!(flags & FOLL_PCI_P2PDMA) && folio_is_pci_p2pdma(folio)))
@@ -1482,8 +1486,8 @@ 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)) {
Signature · 1 line
2.43.0
author maintainer
#30 lore raw
In reply to David Hildenbrand (Arm)
Quoted 7 lines · David Hildenbrand (Arm)

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.

Quoted 6 lines

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 really only relevant for large folios / thps in the page cache. With anon folios (including THPs), we should not be
able to trigger folio refcount overflows (until we get PUD-sized ones ;) ).

Just a note that I don't think anon THP are as really relevant today, at least for 4k kernels, as one would usually need that so many processes (consuming so much memory for everything else) that this is mostly irrelevant in practice as an attack vector.

At least with 512 pages per THP.

The exception might be arm64 with 16k or 64k page size with 2k. (putting aside that 512M THPs are barely usable in practice).

#processes to overflow the folio refcount

4k kernel: 512 pages per THP: 4m processes (irrelevant in practice)

16k kernel: 2048 pages per THP: 1m processes (mostly irrelevant in practice)

64k kernel: 8192 pages per THP: 256k processes

The 64k case might be the only relevant one, but I think even there all processes combined would already consume way more than 100 GiB of kernel memory.

If we really care about the latter case we could throw in some cheap checks during fork() checks.

Handling migration entry restore / swapin would be a bit more nasty, but not sure if that's really something to worry about in the big scheme of things.

Signature · 3 lines
Cheers,

David
reviewer Reviewed-by on v1 1/5
#2 lore raw

Re: [PATCH 1/5] mm: limit pagecache folio refcount when mapping pages

In reply to David Hildenbrand (Arm)
Quoted 40 lines · David Hildenbrand (Arm)

With large file folios, overflowing the folio mapcount and refcount became possible. For anon folios it is currently not really an issue:
each large anon folio can only be mapped once into a process.

That's not really big news, but it's about time that we do something about it because some modern technology might find ways to abuse it :)

One of the enablers for that was commit ddc58f27f9ee ("mm: drop tail page refcounting"), however, file thp support was added later.

File folios can be mapped multiple times into the same process, allowing for processes to hold many references to the same folio.

As a short-term solution, let's introduce two folio refcount limits:
one for ordinary consumers and one for admins. Leave some headroom before the refcount goes negative, which we really don't want (having the mapcount go negative is also not expected).

Start reporting SIGBUS if we run into limits. When restoring mappings from migration entries, instead zap the pte/pmd entry. We allow for slightly exceeding the limit to keep things simpler.

This is far from ideal, but better than all easy alternatives we can backport comfortably. For now it fixes the big problem: letting the refcount (+mapcount) go negative and cause trouble.

This is only a temporary fix, we'll follow up with something better.

While the refcount checks are not required for order 0 folios, let's just add them for consistency: each folio_add_file_rmap*() and folio_dup_file_rmap*() needs a prior check.

Also, just document -EHWPOISON for copy_present_ptes() while at it.

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")
Quoted 8 lines

---

include/linux/mm.h | 33 +++++++++++++++++++++++++++++++++
mm/filemap.c       |  6 ++++++
mm/huge_memory.c   | 24 ++++++++++++++++++++++--
mm/memory.c        | 23 ++++++++++++++++++++---
mm/migrate.c       | 11 +++++++++++
mm/userfaultfd.c   |  4 ++++
6 files changed, 96 insertions(+), 5 deletions(-)

Nice work! I just went through all folio_add_file_rmap*() and folio_dup_file_rmap*() callers, including callers of set_pte_range().

All covered, looks good to me1

Reviewed-by on v1 1/5
#3 lore raw

Re: [PATCH 1/5] mm: limit pagecache folio refcount when mapping pages

In reply to David Hildenbrand (Arm)
Quoted 26 lines · David Hildenbrand (Arm)

With large file folios, overflowing the folio mapcount and refcount became possible. For anon folios it is currently not really an issue:
each large anon folio can only be mapped once into a process.

That's not really big news, but it's about time that we do something about it because some modern technology might find ways to abuse it :)

One of the enablers for that was commit ddc58f27f9ee ("mm: drop tail page refcounting"), however, file thp support was added later.

File folios can be mapped multiple times into the same process, allowing for processes to hold many references to the same folio.

As a short-term solution, let's introduce two folio refcount limits:
one for ordinary consumers and one for admins. Leave some headroom before the refcount goes negative, which we really don't want (having the mapcount go negative is also not expected).

Start reporting SIGBUS if we run into limits. When restoring mappings from migration entries, instead zap the pte/pmd entry. We allow for slightly exceeding the limit to keep things simpler.

This is far from ideal, but better than all easy alternatives we can backport comfortably. For now it fixes the big problem: letting the refcount (+mapcount) go negative and cause trouble.

Quoted 5 lines

This is only a temporary fix, we'll follow up with something better.

While the refcount checks are not required for order 0 folios, let's just add them for consistency: each folio_add_file_rmap*() and folio_dup_file_rmap*() needs a prior check.

in mm-new copy_huge_pmd() now does a folio_dup_file_rmap_pmd() at fork, since Konstantin's "mm/huge_memory: copy file THPs at fork like copy_present_ptes() does". doesn't that one need the check too?

the rest LGTM:

Signature · 2 lines
cheers,
jose a. p-a
Reviewed-by on v1 2/5
#12 lore raw

Re: [PATCH 2/5] mm: limit pagecache folio refcount when splicing into pipes

In reply to David Hildenbrand (Arm)
Quoted 10 lines · David Hildenbrand (Arm)

Similar to mapping pagecache folios into page tables, let's reject splicing folios into a pipe if the refcount is at it's limit, similar to how we handle it during GUP.

This will prevent more cases of possible folio refcount overflows with large pagecache folios.

Let's keep it simple and check in the two callers of splice_folio_into_pipe().

Quoted 8 lines

Also check in try_get_page() to cover pipe-buffer duplication by tee() and partial pipe-to-pipe splicing.

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")

LGTM:

Signature · 2 lines
cheers,
jose a. p-a
Reviewed-by on v1 3/5
#20 lore raw

Re: [PATCH 3/5] mm/mmu_gather: flush pending references early before the folio limit

In reply to David Hildenbrand (Arm)
Quoted 9 lines · David Hildenbrand (Arm)

When a task zaps mappings (e.g., MADV_DONTNEED), it can drop the PT lock and defer dropping the folio references. In the meantime, page faults can refault pagecache folios, creating more references for the same folios.

Let's just request a flush after enqueueing a pagecache folio whose refcount reached a certain threshold. Start flushing well before new references are refused, regardless of the caller's privileges.

Quoted 6 lines

This is a temporary measure until we have something better.

Fixes 10ebac4f95e7 ("mm/memory: optimize unmap/zap with PTE-mapped THP")

LGTM:

Signature · 2 lines
cheers,
jose a. p-a
reviewer
#13 lore raw

Re: [PATCH 2/5] mm: limit pagecache folio refcount when splicing into pipes

In reply to David Hildenbrand (Arm)
Quoted 4 lines · David Hildenbrand (Arm)

Similar to mapping pagecache folios into page tables, let's reject splicing folios into a pipe if the refcount is at it's limit, similar to how we handle it during GUP.

Quoted 8 lines

This will prevent more cases of possible folio refcount overflows with large pagecache folios.

Let's keep it simple and check in the two callers of splice_folio_into_pipe().

Also check in try_get_page() to cover pipe-buffer duplication by tee() and partial pipe-to-pipe splicing.

What about fuse_ref_folio()? Buffered FUSE_WRITE requests can carry pagecache folios into a pipe through /dev/fuse without going through splice_folio_into_pipe() or try_get_page() ...

Cheers, Lance

Quoted 67 lines
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")

---

include/linux/mm.h | 9 ++++++---
mm/filemap.c       | 4 ++++
mm/shmem.c         | 4 ++++
3 files changed, 14 insertions(+), 3 deletions(-)
include/linux/mm.h +6 -3
index 79ae8fc0d6bb4..a82cfbce18215 100644
@@ -2287,10 +2287,13 @@ static inline void get_page(struct page *page)
 
 static inline __must_check bool try_get_page(struct page *page)
 {
-	page = compound_head(page);
-	if (WARN_ON_ONCE(page_ref_count(page) <= 0))
+	struct folio *folio = page_folio(page);
+
+	if (WARN_ON_ONCE(folio_ref_count(folio) <= 0))
 		return false;
-	page_ref_inc(page);
+	if (unlikely(folio_ref_count_over_limit(folio)))
+		return false;
+	folio_ref_inc(folio);
 	return true;
 }
 
mm/filemap.c +4 -0
index 920bbf86dfdff..0f2ec790a5b65 100644
@@ -3168,6 +3168,10 @@ ssize_t filemap_splice_read(struct file *in, loff_t *ppos,
 			if (writably_mapped)
 				flush_dcache_folio(folio);
 
+			if (unlikely(folio_ref_count_over_limit(folio))) {
+				error = -ENOMEM;
+				goto out;
+			}
 			n = min_t(loff_t, len, isize - *ppos);
 			n = splice_folio_into_pipe(pipe, folio, *ppos, n);
 			if (!n)
mm/shmem.c +4 -0
index cda1a0f7db99d..1c0f6bceac486 100644
@@ -3819,6 +3819,10 @@ static ssize_t shmem_file_splice_read(struct file *in, loff_t *ppos,
 			 * Ok, we have the page, and it's up-to-date, so we can
 			 * now splice it into the pipe.
 			 */
+			if (unlikely(folio_ref_count_over_limit(folio))) {
+				error = -ENOMEM;
+				break;
+			}
 			n = splice_folio_into_pipe(pipe, folio, *ppos, part);
 			folio_put(folio);
 			folio = NULL;
Signature · 2 lines
-- 
2.43.0
author maintainer
#4 lore raw

Re: [PATCH 1/5] mm: limit pagecache folio refcount when mapping pages

In reply to Jose A. Perez de Azpillaga
Quoted 27 lines · Jose A. Perez de Azpillaga
Quoted 26 lines · David Hildenbrand (Arm)

With large file folios, overflowing the folio mapcount and refcount became possible. For anon folios it is currently not really an issue:
each large anon folio can only be mapped once into a process.

That's not really big news, but it's about time that we do something about it because some modern technology might find ways to abuse it :)

One of the enablers for that was commit ddc58f27f9ee ("mm: drop tail page refcounting"), however, file thp support was added later.

File folios can be mapped multiple times into the same process, allowing for processes to hold many references to the same folio.

As a short-term solution, let's introduce two folio refcount limits:
one for ordinary consumers and one for admins. Leave some headroom before the refcount goes negative, which we really don't want (having the mapcount go negative is also not expected).

Start reporting SIGBUS if we run into limits. When restoring mappings from migration entries, instead zap the pte/pmd entry. We allow for slightly exceeding the limit to keep things simpler.

This is far from ideal, but better than all easy alternatives we can backport comfortably. For now it fixes the big problem: letting the refcount (+mapcount) go negative and cause trouble.

Quoted 8 lines
Quoted 5 lines

This is only a temporary fix, we'll follow up with something better.

While the refcount checks are not required for order 0 folios, let's just add them for consistency: each folio_add_file_rmap*() and folio_dup_file_rmap*() needs a prior check.

in mm-new copy_huge_pmd() now does a folio_dup_file_rmap_pmd() at fork, since Konstantin's "mm/huge_memory: copy file THPs at fork like copy_present_ptes() does". doesn't that one need the check too?

Yes, that one will need care as well, thanks. I think I missed it when rebasing on top of the other fixes.

Thanks!

Signature · 3 lines
Cheers,

David
reviewer
#5 lore raw

Re: [PATCH 1/5] mm: limit pagecache folio refcount when mapping pages

In reply to Jose A. Perez de Azpillaga
Quoted 27 lines · Jose A. Perez de Azpillaga
Quoted 26 lines · David Hildenbrand (Arm)

With large file folios, overflowing the folio mapcount and refcount became possible. For anon folios it is currently not really an issue:
each large anon folio can only be mapped once into a process.

That's not really big news, but it's about time that we do something about it because some modern technology might find ways to abuse it :)

One of the enablers for that was commit ddc58f27f9ee ("mm: drop tail page refcounting"), however, file thp support was added later.

File folios can be mapped multiple times into the same process, allowing for processes to hold many references to the same folio.

As a short-term solution, let's introduce two folio refcount limits:
one for ordinary consumers and one for admins. Leave some headroom before the refcount goes negative, which we really don't want (having the mapcount go negative is also not expected).

Start reporting SIGBUS if we run into limits. When restoring mappings from migration entries, instead zap the pte/pmd entry. We allow for slightly exceeding the limit to keep things simpler.

This is far from ideal, but better than all easy alternatives we can backport comfortably. For now it fixes the big problem: letting the refcount (+mapcount) go negative and cause trouble.

Quoted 8 lines
Quoted 5 lines

This is only a temporary fix, we'll follow up with something better.

While the refcount checks are not required for order 0 folios, let's just add them for consistency: each folio_add_file_rmap*() and folio_dup_file_rmap*() needs a prior check.

in mm-new copy_huge_pmd() now does a folio_dup_file_rmap_pmd() at fork, since Konstantin's "mm/huge_memory: copy file THPs at fork like copy_present_ptes() does". doesn't that one need the check too?

Ah, good catch! My local tree didn't have Konstantin's series [1] yet. And right, that path needs a check too I guess :)

[1] https://lore.kernel.org/all/20261104-b4-prep-vstor-149847-copy_huge_pmd-recheck-special-pmd-v5-2-c56d0d26f970@virtuozzo.com/

Quoted 9 lines

the rest LGTM:

Signature · 2 lines
cheers,
jose a. p-a
#26 lore raw

Re: [PATCH 5/5] mm/gup: limit pagecache folio refcount when grabbing references

In reply to David Hildenbrand (Arm)
Quoted 47 lines · David Hildenbrand (Arm)

Let's replace our existing safety check by the new folio_ref_count_over_limit() check in fast and slow GUP before acquiring additional references or pins.

Keep it simple and use the check for all folio types.

Drop the WARN_ON_ONCE(), because apparently these things can currently happen with large pagecache folios.

Keep the zero-refcount rejection if the refcount is 0.

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")
---
 mm/gup.c | 12 ++++++++----
 1 file changed, 8 insertions(+), 4 deletions(-)
mm/gup.c +6 -2
index 2ab42fc6834e5..db9db6b2a4662 100644
@@ -77,7 +77,7 @@ static inline struct folio *try_get_folio(struct page *page, int refs)
 
 retry:
 	folio = page_folio(page);
-	if (WARN_ON_ONCE(folio_ref_count(folio) < 0))
+	if (unlikely(folio_ref_count_over_limit(folio)))
 		return NULL;
 	if (unlikely(!folio_ref_try_add(folio, refs)))
 		return NULL;
@@ -140,7 +140,11 @@ 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))
+	if ((flags & (FOLL_GET | FOLL_PIN)) &&
+	    unlikely(folio_ref_count_over_limit(folio)))
+		return -ENOMEM;
+
+	if (WARN_ON_ONCE(!folio_ref_count(folio)))
 		return -ENOMEM;
 
 	if (unlikely(!(flags & FOLL_PCI_P2PDMA) && folio_is_pci_p2pdma(folio)))

mm/gup.c static long __get_user_pages(struct mm_struct *mm, · line 1493

Quoted 10 lines
mm/gup.c +2 -2
@@ -1482,8 +1486,8 @@ 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)) {

this can fail now, so shouldn't the error path below return -ENOMEM like the first page does, instead of -EFAULT?

Signature · 2 lines
cheers,
jose a. p-a
#7 lore raw

Re: [PATCH 1/5] mm: limit pagecache folio refcount when mapping pages

In reply to David Hildenbrand (Arm)
Quoted 90 lines · David Hildenbrand (Arm)

With large file folios, overflowing the folio mapcount and refcount became possible. For anon folios it is currently not really an issue:
each large anon folio can only be mapped once into a process.

That's not really big news, but it's about time that we do something about it because some modern technology might find ways to abuse it :)

One of the enablers for that was commit ddc58f27f9ee ("mm: drop tail page refcounting"), however, file thp support was added later.

File folios can be mapped multiple times into the same process, allowing for processes to hold many references to the same folio.

As a short-term solution, let's introduce two folio refcount limits:
one for ordinary consumers and one for admins. Leave some headroom before the refcount goes negative, which we really don't want (having the mapcount go negative is also not expected).

Start reporting SIGBUS if we run into limits. When restoring mappings from migration entries, instead zap the pte/pmd entry. We allow for slightly exceeding the limit to keep things simpler.

This is far from ideal, but better than all easy alternatives we can backport comfortably. For now it fixes the big problem: letting the refcount (+mapcount) go negative and cause trouble.

This is only a temporary fix, we'll follow up with something better.

While the refcount checks are not required for order 0 folios, let's just add them for consistency: each folio_add_file_rmap*() and folio_dup_file_rmap*() needs a prior check.

Also, just document -EHWPOISON for copy_present_ptes() while at it.

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")
---
 include/linux/mm.h | 33 +++++++++++++++++++++++++++++++++
 mm/filemap.c       |  6 ++++++
 mm/huge_memory.c   | 24 ++++++++++++++++++++++--
 mm/memory.c        | 23 ++++++++++++++++++++---
 mm/migrate.c       | 11 +++++++++++
 mm/userfaultfd.c   |  4 ++++
 6 files changed, 96 insertions(+), 5 deletions(-)
include/linux/mm.h +26 -0
index a2dd76cc5bbfe..79ae8fc0d6bb4 100644
@@ -8,6 +8,7 @@
 #include <linux/gfp.h>
 #include <linux/pgalloc_tag.h>
 #include <linux/bug.h>
+#include <linux/capability.h>
 #include <linux/list.h>
 #include <linux/mmzone.h>
 #include <linux/rbtree.h>
@@ -2228,6 +2229,38 @@ vm_fault_t finish_fault(struct vm_fault *vmf);
 #define folio_ref_zero_or_close_to_overflow(folio) \
 	((unsigned int) folio_ref_count(folio) + 127u <= 127u)
 
+/* Leave separate reserves for administrators and concurrent reference gains. */
+#define FOLIO_REF_COUNT_LIMIT		(INT_MAX - (1U << 26))
+#define FOLIO_REF_COUNT_ADMIN_LIMIT	(INT_MAX - (1U << 25))
+
+/**
+ * folio_ref_count_over_limit - Check whether to refuse more folio references.
+ * @folio: The folio.
+ *
+ * Relevant for large pagecache folios (!hugetlb). For these, we have to check
+ * before adding page table mappings or other significant refcount users whether
+ * we are close to our folio ref count limit, to avoid overflows.
+ *
+ * Checking this for other folios does no harm but is mostly unnecessary.
+ *
+ * This check is an approximation; we leave some headroom such that concurrent
+ * increments, batches and temporary references are not a problem.
+ *
+ * Return: True if the caller should refuse additional references, false
+ * otherwise.
+ */
+static inline bool folio_ref_count_over_limit(const struct folio *folio)
+{
+	const unsigned int ref_count = folio_ref_count(folio);
+
+	if (likely(ref_count < FOLIO_REF_COUNT_LIMIT))

include/linux/mm.h vm_fault_t finish_fault(struct vm_fault *vmf); · line 2266

Quoted 10 lines
include/linux/mm.h +7 -0
+		return false;
+	if (ref_count >= FOLIO_REF_COUNT_ADMIN_LIMIT)
+		return true;
+	/* Background threads must not consume admin reserves. */
+	return (current->flags & PF_KTHREAD) || !capable(CAP_SYS_RESOURCE);
+}
+
 /**
  * folio_get - Increment the reference count on a folio.
  * @folio: The folio.

Does the slow path need to be inline at all 15 call sites, GUP-fast among them?

Maybe only leave the likely() check in inline and move the rest into a function call?

Something like:

static inline bool folio_ref_count_over_limit(const struct folio *folio)

{
        const unsigned int ref_count = folio_ref_count(folio);

        if (likely(ref_count < FOLIO_REF_COUNT_LIMIT))
                return false;
        return __folio_ref_count_over_limit(ref_count);
}

bool __folio_ref_count_over_limit(unsigned int ref_count)

{
        if (ref_count >= FOLIO_REF_COUNT_ADMIN_LIMIT)
                return true;
        /* Background threads must not consume admin reserves */
        if (current->flags & PF_KTHREAD)
                return true;
        return !capable(CAP_SYS_RESOURCE);
}

Also, should this be capable_noaudit()? I am not sure a refused reference should leave an audit record. cap_vm_enough_memory() checks the admin reserve with CAP_OPT_NOAUDIT.

Signature · 1 line
  Kiryl Shutsemau / Kirill A. Shutemov
#10 lore raw

Re: [PATCH 1/5] mm: limit pagecache folio refcount when mapping pages

In reply to David Hildenbrand (Arm)
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [Medium] mm: missing folio_ref_count_over_limit() check in copy_huge_pud() --

Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [Medium] mm: missing folio_ref_count_over_limit() check in copy_huge_pud()
--

commit f7e3ed38c742cda863dfe55787d1e17c689cd9cb
Author: David Hildenbrand (Arm) <[email protected]>

mm: limit pagecache folio refcount when mapping pages

This commit introduces FOLIO_REF_COUNT_LIMIT, FOLIO_REF_COUNT_ADMIN_LIMIT, and folio_ref_count_over_limit() to prevent large pagecache folio reference counts and mapcounts from overflowing when adding or duplicating file mappings.

Quoted 4 lines
mm/huge_memory.c +0 -0
index d0e19d9e6b651..2596ff0f0e185 100644

[ ... ]

mm/huge_memory.c static vm_fault_t insert_pud(struct vm_area_struct *vma, unsigned long addr, · line 1799

Quoted 11 lines
mm/huge_memory.c +4 -0
@@ -1784,6 +1790,10 @@ static vm_fault_t insert_pud(struct vm_area_struct *vma, unsigned long addr,
 	if (fop.is_folio) {
 		entry = folio_mk_pud(fop.folio, vma->vm_page_prot);
 
+		if (unlikely(folio_ref_count_over_limit(fop.folio))) {
+			ret = VM_FAULT_SIGBUS;
+			goto out_unlock;
+		}
 		folio_get(fop.folio);
 		folio_add_file_rmap_pud(fop.folio, &fop.folio->page, vma);
 		add_mm_counter(mm, mm_counter_file(fop.folio), HPAGE_PUD_NR);

[Severity: Medium]
The commit message notes that each folio_add_file_rmap*() and folio_dup_file_rmap*() call needs a prior folio_ref_count_over_limit() check, and insert_pud() is updated here accordingly.

However, when forking a VMA that passes vma_needs_copy() and contains a PUD-mapped folio, copy_page_range() -> copy_p4d_range() -> copy_pud_range() -> copy_huge_pud() in mm/huge_memory.c also increments the folio reference count and calls folio_add_file_rmap_pud():

mm/huge_memory.c:copy_huge_pud() {
	...
	page = vm_normal_page_pud(src_vma, addr, pud);
	if (page) {
		struct folio *folio = page_folio(page);

		folio_get(folio);
		/*
		 * TODO: once we support anonymous pages, use
		 * folio_try_dup_anon_rmap_*() and split if duplicating fails.
		 */
		folio_add_file_rmap_pud(folio, page, dst_vma);
		add_mm_counter(dst_mm, mm_counter_file(folio), HPAGE_PUD_NR);
	}
	...
}

Should copy_huge_pud() also check folio_ref_count_over_limit(folio) and return -ENOMEM (which copy_pud_range() already handles) before calling folio_get() and folio_add_file_rmap_pud()?

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
#28 lore raw

Re: [PATCH 5/5] mm/gup: limit pagecache folio refcount when grabbing references

In reply to David Hildenbrand (Arm)
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [High] mm/gup: try_get_folio() calls capable() with IRQs disabled in GUP-fast --

Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [High] mm/gup: try_get_folio() calls capable() with IRQs disabled in GUP-fast
--

commit 9290efad216cdf3b5ee1cdfafab0a5d9928a1976
Author: David Hildenbrand (Arm) <[email protected]>

mm/gup: limit pagecache folio refcount when grabbing references

This commit replaces the negative refcount safety checks in try_get_folio() and try_grab_folio() with folio_ref_count_over_limit() to enforce folio refcount limits in fast and slow GUP before acquiring references or pins.

mm/gup.c static inline struct folio *try_get_folio(struct page *page, int refs) · line 81

Quoted 11 lines
mm/gup.c +1 -1
index 2ab42fc6834e5..db9db6b2a4662 100644
@@ -77,7 +77,7 @@ static inline struct folio *try_get_folio(struct page *page, int refs)
 
 retry:
 	folio = page_folio(page);
-	if (WARN_ON_ONCE(folio_ref_count(folio) < 0))
+	if (unlikely(folio_ref_count_over_limit(folio)))
 		return NULL;

[Severity: High]
Is it safe for try_get_folio() to call folio_ref_count_over_limit() in the fast GUP path?

In mm/gup.c, gup_fast() disables hardware interrupts across the page table walk:

mm/gup.c:gup_fast() {
	...
	local_irq_save(flags);
	nr_pinned = gup_fast_pgd_range(start, end, gup_flags, pages);
	local_irq_restore(flags);
	...
}

which calls into try_grab_folio_fast() -> try_get_folio() with IRQs disabled:

mm/gup.c:try_grab_folio_fast() {
	...
	/* Raise warn if it is not called in fast GUP */
	VM_WARN_ON_ONCE(!irqs_disabled());
	...
}

Furthermore, get_user_page_fast_only() -> gup_fast() -> try_get_folio() is invoked from hardirq and NMI contexts, such as perf_prepare_sample() -> perf_virt_to_phys() in kernel/events/core.c.

When folio_ref_count(folio) is between FOLIO_REF_COUNT_LIMIT and FOLIO_REF_COUNT_ADMIN_LIMIT and current is not a kernel thread, folio_ref_count_over_limit() in include/linux/mm.h calls capable(CAP_SYS_RESOURCE):

include/linux/mm.h:folio_ref_count_over_limit() {
	const unsigned int ref_count = folio_ref_count(folio);

	if (likely(ref_count < FOLIO_REF_COUNT_LIMIT))
		return false;
	if (ref_count >= FOLIO_REF_COUNT_ADMIN_LIMIT)
		return true;
	/* Background threads must not consume admin reserves. */
	return (current->flags & PF_KTHREAD) || !capable(CAP_SYS_RESOURCE);
}

Calling capable(CAP_SYS_RESOURCE) -> ns_capable_common() -> security_capable() in these contexts appears to cause several issues:

1. LSM capability hooks (such as selinux_capable() and apparmor_capable()) perform audit logging via common_lsm_audit(), which allocates memory and acquires spinlock_t, rwlock_t, and SLUB local_lock_t locks. On CONFIG_PREEMPT_RT, these are sleeping rt_mutex locks, so acquiring them with hardware interrupts disabled in gup_fast() will sleep in an invalid atomic context.

2. When get_user_page_fast_only() is called from hardirq or NMI context, acquiring non-IRQ/NMI-safe locks in the LSM, audit, or SLUB paths can self-deadlock if the interrupted context on the same CPU already holds one of those locks.

3. In security/apparmor/capability.c, audit_caps() accesses the per-CPU audit_cache using get_cpu_var(), which only disables preemption:

   security/apparmor/capability.c:audit_caps() {
	...
	ent = &get_cpu_var(audit_cache);
	if (ad->subj_cred == ent->ad_subj_cred &&
	    ktime_get_ns() <= ent->ktime_ns_expiration[cap]) {
		...
	} else {
		put_cred(ent->ad_subj_cred);
		ent->ad_subj_cred = get_cred(ad->subj_cred);
		ent->ktime_ns_expiration[cap] = ...;
	}
	put_cpu_var(audit_cache);
	...

}

If an IRQ or NMI invokes get_user_page_fast_only() while the interrupted context is inside audit_caps() on the same CPU, both contexts can race on ent->ad_subj_cred and cause a double put_cred().

4. In kernel/capability.c, ns_capable_common() updates current->flags non-atomically:

   kernel/capability.c:ns_capable_common() {
	...
	capable = security_capable(current_cred(), ns, cap, opts);
	if (capable == 0) {
		current->flags |= PF_SUPERPRIV;
		return true;
	}
	...

}

When called from hardirq or NMI context, this mutates the interrupted task's flags and can race with process-context updates to current->flags.

Should try_get_folio() avoid calling capable() in fast GUP (for example, by falling back to slow GUP once the folio refcount reaches FOLIO_REF_COUNT_LIMIT)?

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=5
reviewer Reviewed-by on v1 3/5
#21 lore raw

Re: [PATCH 3/5] mm/mmu_gather: flush pending references early before the folio limit

In reply to David Hildenbrand (Arm)
Quoted 9 lines · David Hildenbrand (Arm)

When a task zaps mappings (e.g., MADV_DONTNEED), it can drop the PT lock and defer dropping the folio references. In the meantime, page faults can refault pagecache folios, creating more references for the same folios.

Let's just request a flush after enqueueing a pagecache folio whose refcount reached a certain threshold. Start flushing well before new references are refused, regardless of the caller's privileges.

Quoted 7 lines

This is a temporary measure until we have something better.

Fixes 10ebac4f95e7 ("mm/memory: optimize unmap/zap with PTE-mapped THP")

---

reviewer Reviewed-by on v1 4/5
#23 lore raw

Re: [PATCH 4/5] iommufd: fix cleanup when adding memfd folio pins fails

In reply to David Hildenbrand (Arm)
Quoted 16 lines · David Hildenbrand (Arm)

folio_add_pins() can fail after memfd_pin_folios() succeeded pinning a folio. Upcoming folio refcount limit checks will make this easier to trigger.

Let's fix three problems in the error handling:

1. pfn_reader_release_pins() uses the allocated array size instead of the number of folios returned by memfd_pin_folios(). With large folios, cleanup can unpin uninitialized entries. Remember the number of returned folios and use that during cleanup.

2. pin_memfd_pages() returns on failure without releasing the pins acquired by the current call. Release the per-page pins for folios already expanded into the page array, and the single pins for the remaining folios.

Quoted 8 lines

3. pfn_reader_next() returns on failure before updating batch_end_index, even if the batch already contains successfully expanded folios. Update it before propagating the error so cleanup releases those pins as well.

Fixes 92687c793644 ("iommufd: pfn_reader for file mappings")

---

Nothing jumped out at me, so:

#15 lore raw

Re: [PATCH 2/5] mm: limit pagecache folio refcount when splicing into pipes

In reply to David Hildenbrand (Arm)
Quoted 29 lines · David Hildenbrand (Arm)

Similar to mapping pagecache folios into page tables, let's reject splicing folios into a pipe if the refcount is at it's limit, similar to how we handle it during GUP.

This will prevent more cases of possible folio refcount overflows with large pagecache folios.

Let's keep it simple and check in the two callers of splice_folio_into_pipe().

Also check in try_get_page() to cover pipe-buffer duplication by tee() and partial pipe-to-pipe splicing.

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")
---
 include/linux/mm.h | 9 ++++++---
 mm/filemap.c       | 4 ++++
 mm/shmem.c         | 4 ++++
 3 files changed, 14 insertions(+), 3 deletions(-)
include/linux/mm.h +0 -0
index 79ae8fc0d6bb4..a82cfbce18215 100644

include/linux/mm.h static inline void get_page(struct page *page) · line 2292

Quoted 8 lines
include/linux/mm.h +3 -2
@@ -2287,10 +2287,13 @@ static inline void get_page(struct page *page)
 
 static inline __must_check bool try_get_page(struct page *page)
 {
-	page = compound_head(page);
-	if (WARN_ON_ONCE(page_ref_count(page) <= 0))
+	struct folio *folio = page_folio(page);
+
+	if (WARN_ON_ONCE(folio_ref_count(folio) <= 0))

Since you're here anyway can you change this to something other than WARN_ON_ONCE, since that results in an OOPS with panic_on_warn set? Thanks,

Josef

#31 lore raw
In reply to David Hildenbrand (Arm)
Quoted 2 lines · David Hildenbrand (Arm)

Users can create a lot of folio references to pagecache folios, making it possible to overflow the refcount of large folios.

This is a summary of my Claude review of everything, minus what has already been said on the thread. The numbers are from a TCG guest with the limits lowered to user 6000 and admin 9000.

1) Small folios, 1/5 and 5/5

On an order-0 folio one FOLL_PIN is worth GUP_PIN_COUNTING_BIAS references, so about 2M pins reach the limit. That used to make GUP refuse more pins, now it is also SIGBUS on new faults and failing fork and splice for everybody else. 6 io_uring registered buffers on one order-0 shmem page (ref 6146) and another task gets SIGBUS mapping it. Mappings alone can't get a small folio near the limit, so could the mapping and splice sites skip small folios?

2) Migration, 1/5

remove_migration_pte() and remove_migration_pmd() check the limit for whoever migrates, not for the owner of the mapping, and kcompactd always gets the user limit. With mappings in the admin reserve the next migration drops whatever is over the user limit, in rmap walk order.

At ref 7681, move_pages(MPOL_MF_MOVE_ALL) without CAP_SYS_RESOURCE left 1682 of 7168 PTEs unrestored, including all 512 of an mlock()ed VMA, and touching them again got SIGBUS for all 1682. Could the restore check against the admin limit only?

3) fork, 1/5

If the VMA is only copied because it has an anon rmap, the pagecache PTEs could be left out to refault in the child, like copy_huge_pmd() does, instead of failing the fork. Only the VM_COPY_ON_FORK cases need the error.

4) splice, 2/5

filemap_splice_read() fails where read() still works. sendfile(), the copy_file_range() fallback and overlayfs copy-up get -ENOMEM, and nfsd turns it into nfserr_jukebox so the client retries for as long as the folio stays at the limit. Could do_splice_read() fall back to copy_splice_read(), like it does for O_DIRECT and DAX?

backing_file_splice_read() runs capable() under ctx->cred. overlayfs lowers CAP_SYS_RESOURCE there, so root never gets the reserve, and fuse passthrough uses the server's cred, so an unprivileged reader may.

5) Errors

The one condition comes out as SIGBUS, -ENOMEM, -EFAULT (tee, pipe to pipe, MADV_POPULATE_READ, process_vm_readv), -EIO (/proc/pid/mem), -EOVERFLOW (vm_insert_page) and -EINVAL (memfd_pin_folios, fuse_dev_splice_write). Nothing is logged or counted, so there is nothing that points at the folio.

6) khugepaged, 1/5

try_collapse_pte_mapped_thp() clears the PTEs and drops their references, and then do_set_pmd() can refuse, so the mapping is gone although the collapse would have lowered the refcount.

7) Changelogs, comments, stable

- 3/5 doesn't say what goes wrong without it. It is tagged for 10ebac4f95e7 (v6.9) while 1, 2 and 5 go back to v4.8, so 6.6 and older get the limits without the flush, and it doesn't say why that is fine.
- 3/5 leaves the "queue is (now) full" comments in asm-generic/tlb.h and above __tlb_remove_folio_pages_size() stale.
- The ufolios_count fix in 4/5 doesn't depend on the rest of the series, so it could be its own patch for stable.
- 5/5 needs 1/5 and 4/5, which isn't stated for stable.
- After 5/5 the check is the only guard for pins on small folios, the kernel-doc calls it "mostly unnecessary" for them. The comment in folio_maybe_dma_pinned() still describes the negative refcount scheme.

Thanks,

Josef

author maintainer
#16 lore raw

Re: [PATCH 2/5] mm: limit pagecache folio refcount when splicing into pipes

In reply to Josef Bacik
Quoted 31 lines · Josef Bacik
Quoted 30 lines · David Hildenbrand (Arm)

Similar to mapping pagecache folios into page tables, let's reject splicing folios into a pipe if the refcount is at it's limit, similar to how we handle it during GUP.

This will prevent more cases of possible folio refcount overflows with large pagecache folios.

Let's keep it simple and check in the two callers of splice_folio_into_pipe().

Also check in try_get_page() to cover pipe-buffer duplication by tee() and partial pipe-to-pipe splicing.

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")
---
 include/linux/mm.h | 9 ++++++---
 mm/filemap.c       | 4 ++++
 mm/shmem.c         | 4 ++++
 3 files changed, 14 insertions(+), 3 deletions(-)
include/linux/mm.h +0 -0
index 79ae8fc0d6bb4..a82cfbce18215 100644
@@ -2287,10 +2287,13 @@ static inline void get_page(struct page *page)
 
Quoted 11 lines

include/linux/mm.h static inline void get_page(struct page *page) · line 2292

Quoted 7 lines
include/linux/mm.h +3 -2
 static inline __must_check bool try_get_page(struct page *page)
 {
-	page = compound_head(page);
-	if (WARN_ON_ONCE(page_ref_count(page) <= 0))
+	struct folio *folio = page_folio(page);
+
+	if (WARN_ON_ONCE(folio_ref_count(folio) <= 0))

Since you're here anyway can you change this to something other than WARN_ON_ONCE, since that results in an OOPS with panic_on_warn set? Thanks,

If it actually turns negative (or 0) with out changes, we had an overflow and for sure want to know about it :)

Signature · 3 lines
Cheers,

David
author maintainer
#32 lore raw
In reply to Josef Bacik
Quoted 7 lines · Josef Bacik
Quoted 2 lines · David Hildenbrand (Arm)

Users can create a lot of folio references to pagecache folios, making it possible to overflow the refcount of large folios.

This is a summary of my Claude review of everything, minus what has already been said on the thread. The numbers are from a TCG guest with the limits lowered to user 6000 and admin 9000.

Thanks.

Quoted 10 lines

1) Small folios, 1/5 and 5/5

On an order-0 folio one FOLL_PIN is worth GUP_PIN_COUNTING_BIAS references, so about 2M pins reach the limit. That used to make GUP refuse more pins, now it is also SIGBUS on new faults and failing fork and splice for everybody else. 6 io_uring registered buffers on one order-0 shmem page (ref 6146) and another task gets SIGBUS mapping it. Mappings alone can't get a small folio near the limit, so could the mapping and splice sites skip small folios?

Sure, our limit slightly lower, but not as much as we would care for small folios? I assume your LLM is confused about the actual limits vs. your fake limits?

Quoted 7 lines

2) Migration, 1/5

remove_migration_pte() and remove_migration_pmd() check the limit for whoever migrates, not for the owner of the mapping, and kcompactd always gets the user limit. With mappings in the admin reserve the next migration drops whatever is over the user limit, in rmap walk order.

Right.

Quoted 5 lines

At ref 7681, move_pages(MPOL_MF_MOVE_ALL) without CAP_SYS_RESOURCE left 1682 of 7168 PTEs unrestored, including all 512 of an mlock()ed VMA, and touching them again got SIGBUS for all 1682. Could the restore check against the admin limit only?

An actual admin access would, however, not SIGBUS. Sure, we harm mlock() as well but that's just expected.

I don't see why the extra complexity for this temporary solution would be warranted.

Quoted 7 lines

3) fork, 1/5

If the VMA is only copied because it has an anon rmap, the pagecache PTEs could be left out to refault in the child, like copy_huge_pmd() does, instead of failing the fork. Only the VM_COPY_ON_FORK cases need the error.

That could be done indeed, although I am not sure if really relevant in practice.

I'll see if it can easily be done.

Quoted 8 lines

4) splice, 2/5

filemap_splice_read() fails where read() still works. sendfile(), the copy_file_range() fallback and overlayfs copy-up get -ENOMEM, and nfsd turns it into nfserr_jukebox so the client retries for as long as the folio stays at the limit. Could do_splice_read() fall back to copy_splice_read(), like it does for O_DIRECT and DAX?

Same as the above. This solution here is not going to be the solution we'll have upstream soon, so I try to keep it as small as possible.

I'll see if it can easily be done.

Quoted 5 lines

backing_file_splice_read() runs capable() under ctx->cred. overlayfs lowers CAP_SYS_RESOURCE there, so root never gets the reserve, and fuse passthrough uses the server's cred, so an unprivileged reader may.

Quoted 7 lines

5) Errors

The one condition comes out as SIGBUS, -ENOMEM, -EFAULT (tee, pipe to pipe, MADV_POPULATE_READ, process_vm_readv), -EIO (/proc/pid/mem), -EOVERFLOW (vm_insert_page) and -EINVAL (memfd_pin_folios, fuse_dev_splice_write). Nothing is logged or counted, so there is nothing that points at the folio.

Right, again, a temporary solution which we can reasonably backport to stable.

Quoted 6 lines

6) khugepaged, 1/5

try_collapse_pte_mapped_thp() clears the PTEs and drops their references, and then do_set_pmd() can refuse, so the mapping is gone although the collapse would have lowered the refcount.

Am I wrong or #nobodycares?

Quoted 6 lines

7) Changelogs, comments, stable

- 3/5 doesn't say what goes wrong without it. It is tagged for 10ebac4f95e7 (v6.9) while 1, 2 and 5 go back to v4.8, so 6.6 and older get the limits without the flush, and it doesn't say why that is fine.

Yeah, probably it should go further back. The PTE batching case was just one scenario. It was possible when batching over VMAs before (which my commit handles properly).

Quoted 2 lines

- 3/5 leaves the "queue is (now) full" comments in asm-generic/tlb.h and above __tlb_remove_folio_pages_size() stale.

That seems easy to fix.

Quoted 2 lines

- The ufolios_count fix in 4/5 doesn't depend on the rest of the series, so it could be its own patch for stable.

5/5 makes it easier to trigger. Which is how it was found in the first place.

Quoted 1 line

- 5/5 needs 1/5 and 4/5, which isn't stated for stable.

lol, they have the same Fixes tags, that sure is good enough in one series.

Quoted 3 lines

- After 5/5 the check is the only guard for pins on small folios, the kernel-doc calls it "mostly unnecessary" for them. The comment in folio_maybe_dma_pinned() still describes the negative refcount scheme.

Yeah, I'm trying not to change to much becuase the while thing will get ripped out soon again.

Signature · 3 lines
Cheers,

David
#17 lore raw

Re: [PATCH 2/5] mm: limit pagecache folio refcount when splicing into pipes

In reply to David Hildenbrand (Arm)
Quoted 44 lines · David Hildenbrand (Arm)
Quoted 31 lines · Josef Bacik
Quoted 30 lines · David Hildenbrand (Arm)

Similar to mapping pagecache folios into page tables, let's reject splicing folios into a pipe if the refcount is at it's limit, similar to how we handle it during GUP.

This will prevent more cases of possible folio refcount overflows with large pagecache folios.

Let's keep it simple and check in the two callers of splice_folio_into_pipe().

Also check in try_get_page() to cover pipe-buffer duplication by tee() and partial pipe-to-pipe splicing.

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")
---
 include/linux/mm.h | 9 ++++++---
 mm/filemap.c       | 4 ++++
 mm/shmem.c         | 4 ++++
 3 files changed, 14 insertions(+), 3 deletions(-)
include/linux/mm.h +0 -0
index 79ae8fc0d6bb4..a82cfbce18215 100644
@@ -2287,10 +2287,13 @@ static inline void get_page(struct page *page)
 
Quoted 11 lines

include/linux/mm.h static inline void get_page(struct page *page) · line 2292

Quoted 7 lines
include/linux/mm.h +3 -2
 static inline __must_check bool try_get_page(struct page *page)
 {
-	page = compound_head(page);
-	if (WARN_ON_ONCE(page_ref_count(page) <= 0))
+	struct folio *folio = page_folio(page);
+
+	if (WARN_ON_ONCE(folio_ref_count(folio) <= 0))

Since you're here anyway can you change this to something other than WARN_ON_ONCE, since that results in an OOPS with panic_on_warn set? Thanks,

Quoted 2 lines

If it actually turns negative (or 0) with out changes, we had an overflow and for sure want to know about it :)

Yeah, didn't you get Docs merged to clarify WARN_ON is fine if userspace can't trigger it?

If you use panic_on_warn you *WANT* your system to stop if it detects corruption, that's the whole point. Removing corruption detection so panic doesn't trigger is pure 100% nonsense.

This keeps coming up and it's frustrating to go over again and again.

Jason

author maintainer
#18 lore raw

Re: [PATCH 2/5] mm: limit pagecache folio refcount when splicing into pipes

Quoted 7 lines · Jason Gunthorpe
Quoted 6 lines · David Hildenbrand (Arm)
Quoted 4 lines · Josef Bacik

Since you're here anyway can you change this to something other than WARN_ON_ONCE, since that results in an OOPS with panic_on_warn set? Thanks,

Quoted 5 lines
Quoted 2 lines

If it actually turns negative (or 0) with out changes, we had an overflow and for sure want to know about it :)

Yeah, didn't you get Docs merged to clarify WARN_ON is fine if userspace can't trigger it?

Yes, I pointed Josef at it off-list.

Signature · 3 lines
Cheers,

David
Reviewed-by on v1 4/5
#24 lore raw

Re: [PATCH 4/5] iommufd: fix cleanup when adding memfd folio pins fails

In reply to David Hildenbrand (Arm)
Quoted 20 lines · David Hildenbrand (Arm)

folio_add_pins() can fail after memfd_pin_folios() succeeded pinning a folio. Upcoming folio refcount limit checks will make this easier to trigger.

Let's fix three problems in the error handling:

1. pfn_reader_release_pins() uses the allocated array size instead of the number of folios returned by memfd_pin_folios(). With large folios, cleanup can unpin uninitialized entries. Remember the number of returned folios and use that during cleanup.

2. pin_memfd_pages() returns on failure without releasing the pins acquired by the current call. Release the per-page pins for folios already expanded into the page array, and the single pins for the remaining folios.

3. pfn_reader_next() returns on failure before updating batch_end_index, even if the batch already contains successfully expanded folios. Update it before propagating the error so cleanup releases those pins as well.

Quoted 6 lines
Fixes 92687c793644 ("iommufd: pfn_reader for file mappings")
---
 drivers/iommu/iommufd/pages.c | 15 +++++++++++----
 1 file changed, 11 insertions(+), 4 deletions(-)

Jason

author maintainer
#8 lore raw

Re: [PATCH 1/5] mm: limit pagecache folio refcount when mapping pages

In reply to Kiryl Shutsemau
Quoted 96 lines · Kiryl Shutsemau
Quoted 95 lines · David Hildenbrand (Arm)

With large file folios, overflowing the folio mapcount and refcount became possible. For anon folios it is currently not really an issue:
each large anon folio can only be mapped once into a process.

That's not really big news, but it's about time that we do something about it because some modern technology might find ways to abuse it :)

One of the enablers for that was commit ddc58f27f9ee ("mm: drop tail page refcounting"), however, file thp support was added later.

File folios can be mapped multiple times into the same process, allowing for processes to hold many references to the same folio.

As a short-term solution, let's introduce two folio refcount limits:
one for ordinary consumers and one for admins. Leave some headroom before the refcount goes negative, which we really don't want (having the mapcount go negative is also not expected).

Start reporting SIGBUS if we run into limits. When restoring mappings from migration entries, instead zap the pte/pmd entry. We allow for slightly exceeding the limit to keep things simpler.

This is far from ideal, but better than all easy alternatives we can backport comfortably. For now it fixes the big problem: letting the refcount (+mapcount) go negative and cause trouble.

This is only a temporary fix, we'll follow up with something better.

While the refcount checks are not required for order 0 folios, let's just add them for consistency: each folio_add_file_rmap*() and folio_dup_file_rmap*() needs a prior check.

Also, just document -EHWPOISON for copy_present_ptes() while at it.

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")
---
 include/linux/mm.h | 33 +++++++++++++++++++++++++++++++++
 mm/filemap.c       |  6 ++++++
 mm/huge_memory.c   | 24 ++++++++++++++++++++++--
 mm/memory.c        | 23 ++++++++++++++++++++---
 mm/migrate.c       | 11 +++++++++++
 mm/userfaultfd.c   |  4 ++++
 6 files changed, 96 insertions(+), 5 deletions(-)
include/linux/mm.h +31 -0
index a2dd76cc5bbfe..79ae8fc0d6bb4 100644
@@ -8,6 +8,7 @@
 #include <linux/gfp.h>
 #include <linux/pgalloc_tag.h>
 #include <linux/bug.h>
+#include <linux/capability.h>
 #include <linux/list.h>
 #include <linux/mmzone.h>
 #include <linux/rbtree.h>
@@ -2228,6 +2229,38 @@ vm_fault_t finish_fault(struct vm_fault *vmf);
 #define folio_ref_zero_or_close_to_overflow(folio) \
 	((unsigned int) folio_ref_count(folio) + 127u <= 127u)
 
+/* Leave separate reserves for administrators and concurrent reference gains. */
+#define FOLIO_REF_COUNT_LIMIT		(INT_MAX - (1U << 26))
+#define FOLIO_REF_COUNT_ADMIN_LIMIT	(INT_MAX - (1U << 25))
+
+/**
+ * folio_ref_count_over_limit - Check whether to refuse more folio references.
+ * @folio: The folio.
+ *
+ * Relevant for large pagecache folios (!hugetlb). For these, we have to check
+ * before adding page table mappings or other significant refcount users whether
+ * we are close to our folio ref count limit, to avoid overflows.
+ *
+ * Checking this for other folios does no harm but is mostly unnecessary.
+ *
+ * This check is an approximation; we leave some headroom such that concurrent
+ * increments, batches and temporary references are not a problem.
+ *
+ * Return: True if the caller should refuse additional references, false
+ * otherwise.
+ */
+static inline bool folio_ref_count_over_limit(const struct folio *folio)
+{
+	const unsigned int ref_count = folio_ref_count(folio);
+
+	if (likely(ref_count < FOLIO_REF_COUNT_LIMIT))
+		return false;
+	if (ref_count >= FOLIO_REF_COUNT_ADMIN_LIMIT)
+		return true;
+	/* Background threads must not consume admin reserves. */
+	return (current->flags & PF_KTHREAD) || !capable(CAP_SYS_RESOURCE);
Quoted 8 lines

include/linux/mm.h vm_fault_t finish_fault(struct vm_fault *vmf); · line 2266

Quoted 5 lines
include/linux/mm.h +2 -0
+}
+
 /**
  * folio_get - Increment the reference count on a folio.
  * @folio: The folio.

Does the slow path need to be inline at all 15 call sites, GUP-fast among them?

It's not a lot of code. Do we really care?

E.g., in gup.c, the compiler is free to not inline into each of the two callsites.

Quoted 25 lines

Maybe only leave the likely() check in inline and move the rest into a function call?

Something like:

static inline bool folio_ref_count_over_limit(const struct folio *folio)

{
        const unsigned int ref_count = folio_ref_count(folio);

        if (likely(ref_count < FOLIO_REF_COUNT_LIMIT))
                return false;
        return __folio_ref_count_over_limit(ref_count);
}

bool __folio_ref_count_over_limit(unsigned int ref_count)

{
        if (ref_count >= FOLIO_REF_COUNT_ADMIN_LIMIT)
                return true;
        /* Background threads must not consume admin reserves */
        if (current->flags & PF_KTHREAD)
                return true;
        return !capable(CAP_SYS_RESOURCE);
}
Quoted 4 lines

Also, should this be capable_noaudit()? I am not sure a refused reference should leave an audit record. cap_vm_enough_memory() checks the admin reserve with CAP_OPT_NOAUDIT.

Not an expert, but capable_noaudit() sounds good.

Not sure if that fully resolves sashiko's complaint in the last commit.

Thanks!

Signature · 3 lines
Cheers,

David
author maintainer
#6 lore raw

Re: [PATCH 1/5] mm: limit pagecache folio refcount when mapping pages

In reply to Lance Yang
Quoted 34 lines · Lance Yang
Quoted 32 lines · Jose A. Perez de Azpillaga
Quoted 31 lines · David Hildenbrand (Arm)

With large file folios, overflowing the folio mapcount and refcount became possible. For anon folios it is currently not really an issue:
each large anon folio can only be mapped once into a process.

That's not really big news, but it's about time that we do something about it because some modern technology might find ways to abuse it :)

One of the enablers for that was commit ddc58f27f9ee ("mm: drop tail page refcounting"), however, file thp support was added later.

File folios can be mapped multiple times into the same process, allowing for processes to hold many references to the same folio.

As a short-term solution, let's introduce two folio refcount limits:
one for ordinary consumers and one for admins. Leave some headroom before the refcount goes negative, which we really don't want (having the mapcount go negative is also not expected).

Start reporting SIGBUS if we run into limits. When restoring mappings from migration entries, instead zap the pte/pmd entry. We allow for slightly exceeding the limit to keep things simpler.

This is far from ideal, but better than all easy alternatives we can backport comfortably. For now it fixes the big problem: letting the refcount (+mapcount) go negative and cause trouble.

This is only a temporary fix, we'll follow up with something better.

While the refcount checks are not required for order 0 folios, let's just add them for consistency: each folio_add_file_rmap*() and folio_dup_file_rmap*() needs a prior check.

Quoted 6 lines
Quoted 3 lines

in mm-new copy_huge_pmd() now does a folio_dup_file_rmap_pmd() at fork, since Konstantin's "mm/huge_memory: copy file THPs at fork like copy_present_ptes() does". doesn't that one need the check too?

Ah, good catch! My local tree didn't have Konstantin's series [1] yet. And right, that path needs a check too I guess :)

So, likely my series will go in first, it fixes a way older problem. I'll tell Andrew to hold that one off and let me handle it.

Signature · 3 lines
Cheers,

David
author maintainer
#14 lore raw

Re: [PATCH 2/5] mm: limit pagecache folio refcount when splicing into pipes

In reply to Lance Yang
Quoted 12 lines · Lance Yang
Quoted 10 lines · David Hildenbrand (Arm)

Similar to mapping pagecache folios into page tables, let's reject splicing folios into a pipe if the refcount is at it's limit, similar to how we handle it during GUP.

This will prevent more cases of possible folio refcount overflows with large pagecache folios.

Let's keep it simple and check in the two callers of splice_folio_into_pipe().

Quoted 6 lines
Quoted 2 lines

Also check in try_get_page() to cover pipe-buffer duplication by tee() and partial pipe-to-pipe splicing.

What about fuse_ref_folio()? Buffered FUSE_WRITE requests can carry pagecache folios into a pipe through /dev/fuse without going through splice_folio_into_pipe() or try_get_page() ...

Of course it has its own handling ... :)

Thanks! I was hoping the number of folio references would be bounded ... I guess one reference per pipe buffer is not the end of the world, but, let's just add it, and fallback to copying.

Signature · 3 lines
Cheers,

David
author maintainer
#27 lore raw

Re: [PATCH 5/5] mm/gup: limit pagecache folio refcount when grabbing references

In reply to Jose A. Perez de Azpillaga
Quoted 50 lines · Jose A. Perez de Azpillaga
Quoted 49 lines · David Hildenbrand (Arm)

Let's replace our existing safety check by the new folio_ref_count_over_limit() check in fast and slow GUP before acquiring additional references or pins.

Keep it simple and use the check for all folio types.

Drop the WARN_ON_ONCE(), because apparently these things can currently happen with large pagecache folios.

Keep the zero-refcount rejection if the refcount is 0.

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")
---
 mm/gup.c | 12 ++++++++----
 1 file changed, 8 insertions(+), 4 deletions(-)
mm/gup.c +6 -2
index 2ab42fc6834e5..db9db6b2a4662 100644
@@ -77,7 +77,7 @@ static inline struct folio *try_get_folio(struct page *page, int refs)
 
 retry:
 	folio = page_folio(page);
-	if (WARN_ON_ONCE(folio_ref_count(folio) < 0))
+	if (unlikely(folio_ref_count_over_limit(folio)))
 		return NULL;
 	if (unlikely(!folio_ref_try_add(folio, refs)))
 		return NULL;
@@ -140,7 +140,11 @@ 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))
+	if ((flags & (FOLL_GET | FOLL_PIN)) &&
+	    unlikely(folio_ref_count_over_limit(folio)))
+		return -ENOMEM;
+
+	if (WARN_ON_ONCE(!folio_ref_count(folio)))
 		return -ENOMEM;
 
 	if (unlikely(!(flags & FOLL_PCI_P2PDMA) && folio_is_pci_p2pdma(folio)))
@@ -1482,8 +1486,8 @@ static long __get_user_pages(struct mm_struct *mm,
 				struct folio *folio = page_folio(page);
 
Quoted 11 lines

mm/gup.c static long __get_user_pages(struct mm_struct *mm, · line 1493

Quoted 8 lines
mm/gup.c +2 -2
 				/*
-				 * 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)) {

this can fail now, so shouldn't the error path below return -ENOMEM like the first page does, instead of -EFAULT?

Hm, indeed, that's inconsistent. The issue is that we have this -EREMOTEIO check mangled in there. But the ordinary path would already have checked that and failed with he same error.

So this hunk would become:

                                /*
-                                * 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;
                                }
                        }
Signature · 3 lines
Cheers,

David
author maintainer
#29 lore raw

Re: [PATCH 5/5] mm/gup: limit pagecache folio refcount when grabbing references

In reply to [email protected]
Quoted 125 lines · On 11/6/26 14:11, [email protected] wrote:

Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [High] mm/gup: try_get_folio() calls capable() with IRQs disabled in GUP-fast
--

commit 9290efad216cdf3b5ee1cdfafab0a5d9928a1976
Author: David Hildenbrand (Arm) <[email protected]>

mm/gup: limit pagecache folio refcount when grabbing references

This commit replaces the negative refcount safety checks in try_get_folio() and try_grab_folio() with folio_ref_count_over_limit() to enforce folio refcount limits in fast and slow GUP before acquiring references or pins.

mm/gup.c static inline struct folio *try_get_folio(struct page *page, int refs) · line 81

Quoted 11 lines
mm/gup.c +1 -1
index 2ab42fc6834e5..db9db6b2a4662 100644
@@ -77,7 +77,7 @@ static inline struct folio *try_get_folio(struct page *page, int refs)
 
 retry:
 	folio = page_folio(page);
-	if (WARN_ON_ONCE(folio_ref_count(folio) < 0))
+	if (unlikely(folio_ref_count_over_limit(folio)))
 		return NULL;

[Severity: High]
Is it safe for try_get_folio() to call folio_ref_count_over_limit() in the fast GUP path?

In mm/gup.c, gup_fast() disables hardware interrupts across the page table walk:

mm/gup.c:gup_fast() {
	...
	local_irq_save(flags);
	nr_pinned = gup_fast_pgd_range(start, end, gup_flags, pages);
	local_irq_restore(flags);
	...
}

which calls into try_grab_folio_fast() -> try_get_folio() with IRQs disabled:

mm/gup.c:try_grab_folio_fast() {
	...
	/* Raise warn if it is not called in fast GUP */
	VM_WARN_ON_ONCE(!irqs_disabled());
	...
}

Furthermore, get_user_page_fast_only() -> gup_fast() -> try_get_folio() is invoked from hardirq and NMI contexts, such as perf_prepare_sample() -> perf_virt_to_phys() in kernel/events/core.c.

When folio_ref_count(folio) is between FOLIO_REF_COUNT_LIMIT and FOLIO_REF_COUNT_ADMIN_LIMIT and current is not a kernel thread, folio_ref_count_over_limit() in include/linux/mm.h calls capable(CAP_SYS_RESOURCE):

include/linux/mm.h:folio_ref_count_over_limit() {
	const unsigned int ref_count = folio_ref_count(folio);

	if (likely(ref_count < FOLIO_REF_COUNT_LIMIT))
		return false;
	if (ref_count >= FOLIO_REF_COUNT_ADMIN_LIMIT)
		return true;
	/* Background threads must not consume admin reserves. */
	return (current->flags & PF_KTHREAD) || !capable(CAP_SYS_RESOURCE);
}

Calling capable(CAP_SYS_RESOURCE) -> ns_capable_common() -> security_capable() in these contexts appears to cause several issues:

1. LSM capability hooks (such as selinux_capable() and apparmor_capable()) perform audit logging via common_lsm_audit(), which allocates memory and acquires spinlock_t, rwlock_t, and SLUB local_lock_t locks. On CONFIG_PREEMPT_RT, these are sleeping rt_mutex locks, so acquiring them with hardware interrupts disabled in gup_fast() will sleep in an invalid atomic context.

2. When get_user_page_fast_only() is called from hardirq or NMI context, acquiring non-IRQ/NMI-safe locks in the LSM, audit, or SLUB paths can self-deadlock if the interrupted context on the same CPU already holds one of those locks.

3. In security/apparmor/capability.c, audit_caps() accesses the per-CPU audit_cache using get_cpu_var(), which only disables preemption:

   security/apparmor/capability.c:audit_caps() {
	...
	ent = &get_cpu_var(audit_cache);
	if (ad->subj_cred == ent->ad_subj_cred &&
	    ktime_get_ns() <= ent->ktime_ns_expiration[cap]) {
		...
	} else {
		put_cred(ent->ad_subj_cred);
		ent->ad_subj_cred = get_cred(ad->subj_cred);
		ent->ktime_ns_expiration[cap] = ...;
	}
	put_cpu_var(audit_cache);
	...
   }

   If an IRQ or NMI invokes get_user_page_fast_only() while the interrupted
   context is inside audit_caps() on the same CPU, both contexts can race on
   ent->ad_subj_cred and cause a double put_cred().

4. In kernel/capability.c, ns_capable_common() updates current->flags non-atomically:

   kernel/capability.c:ns_capable_common() {
	...
	capable = security_capable(current_cred(), ns, cap, opts);
	if (capable == 0) {
		current->flags |= PF_SUPERPRIV;
		return true;
	}
	...
   }

   When called from hardirq or NMI context, this mutates the interrupted
   task's flags and can race with process-context updates to current->flags.
Quoted 3 lines

Should try_get_folio() avoid calling capable() in fast GUP (for example, by falling back to slow GUP once the folio refcount reaches FOLIO_REF_COUNT_LIMIT)?

What a minefiled :)

Best to throw in something like

	if (!in_task() || irqs_disabled())
		true;

In the (new) slow helper to just reject admin reserves in such context.

Signature · 3 lines
Cheers,

David
author maintainer
#9 lore raw

Re: [PATCH 1/5] mm: limit pagecache folio refcount when mapping pages

In reply to David Hildenbrand (Arm)
Quoted 106 lines · David Hildenbrand (Arm)
Quoted 96 lines · Kiryl Shutsemau
Quoted 95 lines · David Hildenbrand (Arm)

With large file folios, overflowing the folio mapcount and refcount became possible. For anon folios it is currently not really an issue:
each large anon folio can only be mapped once into a process.

That's not really big news, but it's about time that we do something about it because some modern technology might find ways to abuse it :)

One of the enablers for that was commit ddc58f27f9ee ("mm: drop tail page refcounting"), however, file thp support was added later.

File folios can be mapped multiple times into the same process, allowing for processes to hold many references to the same folio.

As a short-term solution, let's introduce two folio refcount limits:
one for ordinary consumers and one for admins. Leave some headroom before the refcount goes negative, which we really don't want (having the mapcount go negative is also not expected).

Start reporting SIGBUS if we run into limits. When restoring mappings from migration entries, instead zap the pte/pmd entry. We allow for slightly exceeding the limit to keep things simpler.

This is far from ideal, but better than all easy alternatives we can backport comfortably. For now it fixes the big problem: letting the refcount (+mapcount) go negative and cause trouble.

This is only a temporary fix, we'll follow up with something better.

While the refcount checks are not required for order 0 folios, let's just add them for consistency: each folio_add_file_rmap*() and folio_dup_file_rmap*() needs a prior check.

Also, just document -EHWPOISON for copy_present_ptes() while at it.

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")
---
 include/linux/mm.h | 33 +++++++++++++++++++++++++++++++++
 mm/filemap.c       |  6 ++++++
 mm/huge_memory.c   | 24 ++++++++++++++++++++++--
 mm/memory.c        | 23 ++++++++++++++++++++---
 mm/migrate.c       | 11 +++++++++++
 mm/userfaultfd.c   |  4 ++++
 6 files changed, 96 insertions(+), 5 deletions(-)
include/linux/mm.h +31 -0
index a2dd76cc5bbfe..79ae8fc0d6bb4 100644
@@ -8,6 +8,7 @@
 #include <linux/gfp.h>
 #include <linux/pgalloc_tag.h>
 #include <linux/bug.h>
+#include <linux/capability.h>
 #include <linux/list.h>
 #include <linux/mmzone.h>
 #include <linux/rbtree.h>
@@ -2228,6 +2229,38 @@ vm_fault_t finish_fault(struct vm_fault *vmf);
 #define folio_ref_zero_or_close_to_overflow(folio) \
 	((unsigned int) folio_ref_count(folio) + 127u <= 127u)
 
+/* Leave separate reserves for administrators and concurrent reference gains. */
+#define FOLIO_REF_COUNT_LIMIT		(INT_MAX - (1U << 26))
+#define FOLIO_REF_COUNT_ADMIN_LIMIT	(INT_MAX - (1U << 25))
+
+/**
+ * folio_ref_count_over_limit - Check whether to refuse more folio references.
+ * @folio: The folio.
+ *
+ * Relevant for large pagecache folios (!hugetlb). For these, we have to check
+ * before adding page table mappings or other significant refcount users whether
+ * we are close to our folio ref count limit, to avoid overflows.
+ *
+ * Checking this for other folios does no harm but is mostly unnecessary.
+ *
+ * This check is an approximation; we leave some headroom such that concurrent
+ * increments, batches and temporary references are not a problem.
+ *
+ * Return: True if the caller should refuse additional references, false
+ * otherwise.
+ */
+static inline bool folio_ref_count_over_limit(const struct folio *folio)
+{
+	const unsigned int ref_count = folio_ref_count(folio);
+
+	if (likely(ref_count < FOLIO_REF_COUNT_LIMIT))
+		return false;
+	if (ref_count >= FOLIO_REF_COUNT_ADMIN_LIMIT)
+		return true;
+	/* Background threads must not consume admin reserves. */
+	return (current->flags & PF_KTHREAD) || !capable(CAP_SYS_RESOURCE);
Quoted 8 lines

include/linux/mm.h vm_fault_t finish_fault(struct vm_fault *vmf); · line 2266

Quoted 5 lines
include/linux/mm.h +2 -0
+}
+
 /**
  * folio_get - Increment the reference count on a folio.
  * @folio: The folio.

Does the slow path need to be inline at all 15 call sites, GUP-fast among them?

Quoted 4 lines

It's not a lot of code. Do we really care?

E.g., in gup.c, the compiler is free to not inline into each of the two callsites.

Okay, I ended up splitting it up. A bit nsaty now that fuse will require an exported symbol.

Quoted 25 lines
Quoted 25 lines

Maybe only leave the likely() check in inline and move the rest into a function call?

Something like:

static inline bool folio_ref_count_over_limit(const struct folio *folio)

{
        const unsigned int ref_count = folio_ref_count(folio);

        if (likely(ref_count < FOLIO_REF_COUNT_LIMIT))
                return false;
        return __folio_ref_count_over_limit(ref_count);
}

bool __folio_ref_count_over_limit(unsigned int ref_count)

{
        if (ref_count >= FOLIO_REF_COUNT_ADMIN_LIMIT)
                return true;
        /* Background threads must not consume admin reserves */
        if (current->flags & PF_KTHREAD)
                return true;
        return !capable(CAP_SYS_RESOURCE);
}
Quoted 8 lines
Quoted 4 lines

Also, should this be capable_noaudit()? I am not sure a refused reference should leave an audit record. cap_vm_enough_memory() checks the admin reserve with CAP_OPT_NOAUDIT.

Not an expert, but capable_noaudit() sounds good.

Not sure if that fully resolves sashiko's complaint in the last commit.

Ok, the noaudit definitely seems to be the right thing. But getting overlayfs + fuse oddities done "better", I am using has_capability_noaudit(current, CAP_SYS_RESOURCE) now.

Hoping that is the right thing to use now ... review appreciated.

Signature · 3 lines
Cheers,

David