# \[PATCH V2\] mm: use folio\_expected\_ref\_count() helper for reference counting

- From: Shivank Garg \<hidden\>
- Date: 2025-06-11 05:28:17 
- List: linux-trace-kernel
- Page: https://ratatoskr.run/linux-trace-kernel/2025/06/3086445
- Thread: https://ratatoskr.run/linux-trace-kernel/2025/06/3086445/t.md

```text
Replace open-coded folio reference count calculations with the
folio_expected_ref_count().

No functional changes intended.

Acked-by: David Hildenbrand <redacted>
Signed-off-by: Shivank Garg <redacted>
---
Changes from V1:
- https://lore.kernel.org/linux-mm/20250609170806.447302-2-shivankg@amd.com
- remove extra comments
- drop RFC tag
---
 kernel/events/uprobes.c | 3 +--
 mm/memfd.c              | 3 +--
 2 files changed, 2 insertions(+), 4 deletions(-)

diff --git a/kernel/events/uprobes.c b/kernel/events/uprobes.c
index 4c965ba77f9f..8a601df87072 100644
--- a/kernel/events/uprobes.c
+++ b/kernel/events/uprobes.c
@@ -436,8 +436,7 @@ static int __uprobe_write_opcode(struct vm_area_struct *vma,
 	 * there are no unexpected folio references ...
 	 */
 	if (is_register || userfaultfd_missing(vma) ||
-	    (folio_ref_count(folio) != folio_mapcount(folio) + 1 +
-	     folio_test_swapcache(folio) * folio_nr_pages(folio)))
+	    (folio_ref_count(folio) != folio_expected_ref_count(folio) + 1))
 		goto remap;
 
 	/*
diff --git a/mm/memfd.c b/mm/memfd.c
index ab367e61553d..4db1fd2959aa 100644
--- a/mm/memfd.c
+++ b/mm/memfd.c
@@ -32,8 +32,7 @@
 
 static bool memfd_folio_has_extra_refs(struct folio *folio)
 {
-	return folio_ref_count(folio) - folio_mapcount(folio) !=
-	       folio_nr_pages(folio);
+	return folio_ref_count(folio) != folio_expected_ref_count(folio);
 }
 
 static void memfd_tag_pins(struct xa_state *xas)
-- 
2.43.0

```
