Re: [PATCH v3 7/9] mm/swapfile: Add batched version of folio_put_swap

From: David Hildenbrand (Arm)

Date: Mon May 11 2026 - 04:30:39 EST


On 5/6/26 11:45, Dev Jain wrote:
> Add folio_put_swap_pages to handle a batch of consecutive pages. Note
> that folio_put_swap already can handle a subset of this: nr_pages == 1 and
> nr_pages == folio_nr_pages(folio). Generalize this to any nr_pages.
>
> Currently we have a not-so-nice logic of passing in subpage == NULL if
> we mean to exercise the logic on the entire folio, and subpage != NULL if
> we want to exercise the logic on only that subpage. Remove this
> indirection: the caller invokes folio_put_swap_pages() if it wants to
> operate on a range of pages in the folio (i.e nr_pages may be anything
> between 1 till folio_nr_pages()), and invokes folio_put_swap() if it
> wants to operate on the entire folio.
>
> Signed-off-by: Dev Jain <dev.jain@xxxxxxx>
> ---


[...]

>
> -static inline void folio_put_swap(struct folio *folio, struct page *page)
> +static inline void folio_put_swap(struct folio *folio)
> +{
> +}

Same comment regarding common function.

> +
> +static inline void folio_put_swap_pages(struct folio *folio, struct page *page,
> + unsigned long nr_pages)
> {
> }
>
> diff --git a/mm/swapfile.c b/mm/swapfile.c
> index 28daf92839e77..ac576cc63b194 100644
> --- a/mm/swapfile.c
> +++ b/mm/swapfile.c
> @@ -1778,31 +1778,34 @@ int folio_dup_swap(struct folio *folio)
> }
>
> /**
> - * folio_put_swap() - Decrease swap count of swap entries of a folio.
> + * folio_put_swap_pages() - Decrease swap count of swap entries of a folio.
> * @folio: folio with swap entries bounded, must be in swap cache and locked.
> - * @subpage: if not NULL, only decrease the swap count of this subpage.
> + * @page: the first page in the folio to decrease the swap count for.
> + * @nr_pages: the number of pages in the folio to decrease the swap count for.
> *
> * This won't free the swap slots even if swap count drops to zero, they are
> * still pinned by the swap cache. User may call folio_free_swap to free them.
> * Context: Caller must ensure the folio is locked and in the swap cache.
> */
> -void folio_put_swap(struct folio *folio, struct page *subpage)
> +void folio_put_swap_pages(struct folio *folio, struct page *page,
> + unsigned long nr_pages)
> {
> swp_entry_t entry = folio->swap;
> - unsigned long nr_pages = folio_nr_pages(folio);
> struct swap_info_struct *si = __swap_entry_to_info(entry);
>
> VM_WARN_ON_FOLIO(!folio_test_locked(folio), folio);
> VM_WARN_ON_FOLIO(!folio_test_swapcache(folio), folio);
>
> - if (subpage) {
> - entry.val += folio_page_idx(folio, subpage);
> - nr_pages = 1;
> - }
> + entry.val += folio_page_idx(folio, page);
>
> swap_put_entries_cluster(si, swp_offset(entry), nr_pages, false);
> }
>
> +void folio_put_swap(struct folio *folio)
> +{
> + folio_put_swap_pages(folio, folio_page(folio, 0), folio_nr_pages(folio));
> +}

Same comment regarding kerneldoc :)


... and same comment that this definitely looks nicer.

--
Cheers,

David