From: Alistair Popple <apopple@nvidia.com>
To: Shivank Garg <shivankg@amd.com>
Cc: 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, linux-kernel@vger.kernel.org,
linux-trace-kernel@vger.kernel.org,
linux-perf-users@vger.kernel.org, linux-mm@kvack.org
Subject: Re: [RFC] mm: use folio_expected_ref_count() helper for reference counting
Date: Tue, 10 Jun 2025 12:26:44 +1000 [thread overview]
Message-ID: <yhmscjk4j22mi6yjqpswighh2h5phnmdc7cr2otvn43ng4w577@vheecvjlwmmr> (raw)
In-Reply-To: <20250609170806.447302-2-shivankg@amd.com>
On Mon, Jun 09, 2025 at 05:08:07PM +0000, Shivank Garg wrote:
> 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.
> 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 prev parent reply other threads:[~2025-06-10 2:26 UTC|newest]
Thread overview: 11+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-06-09 17:08 Shivank Garg
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 [this message]
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=yhmscjk4j22mi6yjqpswighh2h5phnmdc7cr2otvn43ng4w577@vheecvjlwmmr \
--to=apopple@nvidia.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 \
--cc=shivankg@amd.com \
/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