From: Shivank Garg <shivankg@amd.com>
To: <mhiramat@kernel.org>, <oleg@redhat.com>, <peterz@infradead.org>,
<mingo@redhat.com>, <acme@kernel.org>, <namhyung@kernel.org>,
<mark.rutland@arm.com>, <alexander.shishkin@linux.intel.com>,
<jolsa@kernel.org>, <irogers@google.com>,
<adrian.hunter@intel.com>, <kan.liang@linux.intel.com>,
<david@redhat.com>, <akpm@linux-foundation.org>
Cc: <linux-kernel@vger.kernel.org>,
<linux-trace-kernel@vger.kernel.org>,
<linux-perf-users@vger.kernel.org>, <linux-mm@kvack.org>,
Shivank Garg <shivankg@amd.com>
Subject: [RFC] mm: use folio_expected_ref_count() helper for reference counting
Date: Mon, 9 Jun 2025 17:08:07 +0000 [thread overview]
Message-ID: <20250609170806.447302-2-shivankg@amd.com> (raw)
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 <shivankg@amd.com>
---
kernel/events/uprobes.c | 5 +++--
mm/memfd.c | 4 ++--
2 files changed, 5 insertions(+), 4 deletions(-)
diff --git a/kernel/events/uprobes.c b/kernel/events/uprobes.c
index 4c965ba77f9f..c978c8c27340 100644
--- a/kernel/events/uprobes.c
+++ b/kernel/events/uprobes.c
@@ -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;
/*
diff --git a/mm/memfd.c b/mm/memfd.c
index ab367e61553d..4ed5506221b7 100644
--- a/mm/memfd.c
+++ b/mm/memfd.c
@@ -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)
--
2.43.0
next reply other threads:[~2025-06-09 17:09 UTC|newest]
Thread overview: 11+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-06-09 17:08 Shivank Garg [this message]
2025-06-09 19:21 ` Matthew Wilcox
2025-06-09 19:31 ` Steven Rostedt
2025-06-09 19:51 ` Matthew Wilcox
2025-06-09 20:14 ` Steven Rostedt
2025-06-10 8:59 ` Shivank Garg
2025-06-10 7:05 ` David Hildenbrand
2025-06-10 2:26 ` Alistair Popple
2025-06-10 5:50 ` Shivank Garg
2025-06-10 7:08 ` David Hildenbrand
2025-06-10 9:00 ` Shivank Garg
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20250609170806.447302-2-shivankg@amd.com \
--to=shivankg@amd.com \
--cc=acme@kernel.org \
--cc=adrian.hunter@intel.com \
--cc=akpm@linux-foundation.org \
--cc=alexander.shishkin@linux.intel.com \
--cc=david@redhat.com \
--cc=irogers@google.com \
--cc=jolsa@kernel.org \
--cc=kan.liang@linux.intel.com \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-mm@kvack.org \
--cc=linux-perf-users@vger.kernel.org \
--cc=linux-trace-kernel@vger.kernel.org \
--cc=mark.rutland@arm.com \
--cc=mhiramat@kernel.org \
--cc=mingo@redhat.com \
--cc=namhyung@kernel.org \
--cc=oleg@redhat.com \
--cc=peterz@infradead.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox