mm: use folio_expected_ref_count() helper for reference counting
Subsystems (5)
RFC 1/1 mm: use folio_expected_ref_count() helper for reference counting
Replace open-coded folio reference count calculations with the folio_expected_ref_count() helper to improve code maintainability and reduce duplication.
No functional changes intended.
--- kernel/events/uprobes.c | 5 +++-- mm/memfd.c | 4 ++-- 2 files changed, 5 insertions(+), 4 deletions(-)
kernel/events/uprobes.c +3 -2
@@ -434,10 +434,11 @@ static int __uprobe_write_opcode(struct vm_area_struct *vma,/* * When unregistering, we may only zap a PTE if uffd is disabled and * there are no unexpected folio references ... +* Expected refs: mappings + swapcache. +* We hold one additional reference (+1). */ 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;/*
mm/memfd.c +2 -2
@@ -32,8 +32,8 @@static bool memfd_folio_has_extra_refs (struct folio *folio) { -return folio_ref_count (folio) -folio_mapcount (folio) != -folio_nr_pages (folio); +/* Expected refs: pagecache + mappings */ +return folio_ref_count (folio) !=folio_expected_ref_count (folio); }static void memfd_tag_pins (struct xa_state *xas)
Signature · 1 line
2.43.0
Quoted 3 lines · Shivank Garg
Replace open-coded folio reference count calculations with the
folio_expected_ref_count()helper to improve code maintainability and reduce duplication.
If it needs this much additional commentary, perhaps it's not actually clearer?
Steven Rostedt
maintainerQuoted 7 lines · Matthew Wilcox
Quoted 3 lines · Shivank Garg
Replace open-coded folio reference count calculations with the
folio_expected_ref_count()helper to improve code maintainability and reduce duplication.If it needs this much additional commentary, perhaps it's not actually clearer?
I don't know. I tend to over explain as I rather make it totally obvious what is happening. I wouldn't say excessive commentary is necessarily a sign that it's not clearer.
Not knowing the code, the explanation makes sense to me.
-- Steve
Quoted 4 lines · Steven Rostedt
Quoted 1 line · Matthew Wilcox
On Mon, Jun 09, 2025 at 05:08:07PM +0000, Shivank Garg wrote:
Quoted 8 lines
Quoted 6 lines
Quoted 3 lines
Replace open-coded folio reference count calculations with the
folio_expected_ref_count()helper to improve code maintainability and reduce duplication.If it needs this much additional commentary, perhaps it's not actually clearer?
I don't know. I tend to over explain as I rather make it totally obvious what is happening. I wouldn't say excessive commentary is necessarily a sign that it's not clearer.
That was a Socratic question, not for you to answer.
My opinion is that the extra commentary is obfuscatory and should be removed.
Steven Rostedt
maintainerQuoted 12 lines · On Mon, 9 Jun 2025 20:51:00 +0100 Matthew Wilcox <[email protected]> wrote:
Quoted 11 lines · Steven Rostedt
Quoted 7 lines · Matthew Wilcox
Quoted 3 lines · Shivank Garg
Replace open-coded folio reference count calculations with the
folio_expected_ref_count()helper to improve code maintainability and reduce duplication.If it needs this much additional commentary, perhaps it's not actually clearer?
Quoted 8 lines
Quoted 3 lines
I don't know. I tend to over explain as I rather make it totally obvious what is happening. I wouldn't say excessive commentary is necessarily a sign that it's not clearer.
That was a Socratic question, not for you to answer.
My opinion is that the extra commentary is obfuscatory and should be removed.
Ah, sorry, your response wasn't clear ;-)
-- Steve
Quoted 3 lines · Shivank Garg
Replace open-coded folio reference count calculations with the
folio_expected_ref_count()helper to improve code maintainability and reduce duplication.
I wonder if there is any opportunity for reducing duplication more broadly? The migration code has similar helpers (folio_expected_refs) as does khugepaged (is_refcount_suitable) and vmscan (is_page_cache_freeable). do_huge_pmd_wp_page() also has an open-coded version of these checks and there are probably others around the place to.
These could all be converted to a helper that returns all the "extra" references after taking into account things like mapping, swapcache, etc. depending on folio.
Quoted 45 lines
No functional changes intended.
Signed-off-by Shivank Garg <[email protected]>--- kernel/events/uprobes.c | 5 +++-- mm/memfd.c | 4 ++-- 2 files changed, 5 insertions(+), 4 deletions(-)kernel/events/uprobes.c +3
-2@@ -434,10 +434,11 @@ static int __uprobe_write_opcode(struct vm_area_struct *vma,/* * When unregistering, we may only zap a PTE if uffd is disabled and * there are no unexpected folio references ... +* Expected refs: mappings + swapcache. +* We hold one additional reference (+1). */ 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;/* mm/memfd.c +2
-2@@ -32,8 +32,8 @@static bool memfd_folio_has_extra_refs (struct folio *folio) { -return folio_ref_count (folio) -folio_mapcount (folio) != -folio_nr_pages (folio); +/* Expected refs: pagecache + mappings */ +return folio_ref_count (folio) !=folio_expected_ref_count (folio); }static void memfd_tag_pins (struct xa_state *xas)Signature · 1 line
2.43.0
Quoted 8 lines · Alistair Popple
Quoted 3 lines · Shivank Garg
Replace open-coded folio reference count calculations with the
folio_expected_ref_count()helper to improve code maintainability and reduce duplication.I wonder if there is any opportunity for reducing duplication more broadly? The migration code has similar helpers (
folio_expected_refs) as does khugepaged (is_refcount_suitable) and vmscan (is_page_cache_freeable).
The folio_expected_refs() and is_refcount_suitable() consolidation was recently merged:
- 86ebd50224c0 ("mm: add folio_expected_ref_count() for reference count calculation")
- 0b43b8bc8ef8 ("mm/khugepaged: clean up refcount check using folio_expected_ref_count()")
Quoted 50 lines
do_huge_pmd_wp_page()also has an open-coded version of these checks and there are probably others around the place to.These could all be converted to a helper that returns all the "extra" references after taking into account things like mapping, swapcache, etc. depending on folio.>
Quoted 45 lines
No functional changes intended.
Signed-off-by Shivank Garg <[email protected]>--- kernel/events/uprobes.c | 5 +++-- mm/memfd.c | 4 ++-- 2 files changed, 5 insertions(+), 4 deletions(-)kernel/events/uprobes.c +3
-2@@ -434,10 +434,11 @@ static int __uprobe_write_opcode(struct vm_area_struct *vma,/* * When unregistering, we may only zap a PTE if uffd is disabled and * there are no unexpected folio references ... +* Expected refs: mappings + swapcache. +* We hold one additional reference (+1). */ 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;/* mm/memfd.c +2
-2@@ -32,8 +32,8 @@static bool memfd_folio_has_extra_refs (struct folio *folio) { -return folio_ref_count (folio) -folio_mapcount (folio) != -folio_nr_pages (folio); +/* Expected refs: pagecache + mappings */ +return folio_ref_count (folio) !=folio_expected_ref_count (folio); }static void memfd_tag_pins (struct xa_state *xas)Signature · 1 line
2.43.0
Quoted 12 lines · Matthew Wilcox
Quoted 11 lines · Steven Rostedt
Quoted 7 lines · Matthew Wilcox
Quoted 3 lines · Shivank Garg
Replace open-coded folio reference count calculations with the
folio_expected_ref_count()helper to improve code maintainability and reduce duplication.If it needs this much additional commentary, perhaps it's not actually clearer?
Quoted 6 lines
Quoted 1 line
I don't know. I tend to over explain as I rather make it totally obvious what is happening. I wouldn't say excessive commentary is necessarily a sign that it's not clearer.
That was a Socratic question, not for you to answer.
My opinion is that the extra commentary is obfuscatory and should be removed.
+1
Signature · 3 lines
Cheers, David / dhildenb
Quoted 19 lines · Shivank Garg
Replace open-coded folio reference count calculations with the
folio_expected_ref_count()helper to improve code maintainability and reduce duplication.No functional changes intended.
Signed-off-by Shivank Garg <[email protected]>--- kernel/events/uprobes.c | 5 +++-- mm/memfd.c | 4 ++-- 2 files changed, 5 insertions(+), 4 deletions(-)kernel/events/uprobes.c +0
-0@@ -434,10 +434,11 @@ static int __uprobe_write_opcode(struct vm_area_struct *vma,/* * When unregistering, we may only zap a PTE if uffd is disabled and
kernel/events/uprobes.c static int __uprobe_write_opcode(struct vm_area_struct *vma, · line 443
Quoted 10 lines
kernel/events/uprobes.c +3
-2* there are no unexpected folio references ... +* Expected refs: mappings + swapcache. +* We hold one additional reference (+1). */ 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;
With the comment removed (the caller holds a reference from GUP) or simplified ("Caller holds an additional folio reference.")
Signature · 3 lines
Cheers, David / dhildenb
Quoted 19 lines · Steven Rostedt
Quoted 16 lines · On Mon, 9 Jun 2025 20:51:00 +0100 Matthew Wilcox <[email protected]> wrote:
Quoted 14 lines · Steven Rostedt
Quoted 7 lines · Matthew Wilcox
Quoted 3 lines · Shivank Garg
Replace open-coded folio reference count calculations with the
folio_expected_ref_count()helper to improve code maintainability and reduce duplication.If it needs this much additional commentary, perhaps it's not actually clearer?
I don't know. I tend to over explain as I rather make it totally obvious what is happening. I wouldn't say excessive commentary is necessarily a sign that it's not clearer.
Quoted 7 lines
Quoted 4 lines
That was a Socratic question, not for you to answer.
My opinion is that the extra commentary is obfuscatory and should be removed.
Ah, sorry, your response wasn't clear ;-)
Thank you Willy, Steve for the discussion. Fair point about extra comments, I'll remove them.
Thanks,
Shivank
Quoted 24 lines · David Hildenbrand
Quoted 23 lines · Shivank Garg
Replace open-coded folio reference count calculations with the
folio_expected_ref_count()helper to improve code maintainability and reduce duplication.No functional changes intended.
Signed-off-by Shivank Garg <[email protected]>---
kernel/events/uprobes.c| 5 +++--
mm/memfd.c | 4 ++--2 files changed, 5 insertions(+), 4 deletions(-)kernel/events/uprobes.c +2
-0@@ -434,10 +434,11 @@ static int __uprobe_write_opcode(struct vm_area_struct *vma, /** When unregistering, we may only zap a PTE if uffd is disabled and * there are no unexpected folio references ... +* Expected refs: mappings + swapcache. +* We hold one additional reference (+1). */
Quoted 9 lines
Quoted 6 lines
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;With the comment removed (the caller holds a reference from GUP) or simplified ("Caller holds an additional folio reference.")
Sure, I'll make this change in revision.
Quoted 2 lines
Thank you!
Best Regards,
Shivank