Re: [PATCH v2 1/5] mm: introduce num_pages_contiguous()
From: Jason Gunthorpe
Date: Fri Jul 04 2025 - 13:10:46 EST
On Fri, Jul 04, 2025 at 02:25:58PM +0800, lizhe.67@xxxxxxxxxxxxx wrote:
> From: Li Zhe <lizhe.67@xxxxxxxxxxxxx>
>
> Function num_pages_contiguous() determine the number of contiguous
> pages starting from the first page in the given array of page pointers.
> VFIO will utilize this interface to accelerate the VFIO DMA map process.
>
> Suggested-by: David Hildenbrand <david@xxxxxxxxxx>
> Signed-off-by: Li Zhe <lizhe.67@xxxxxxxxxxxxx>
> ---
> include/linux/mm.h | 20 ++++++++++++++++++++
> 1 file changed, 20 insertions(+)
>
> diff --git a/include/linux/mm.h b/include/linux/mm.h
> index 0ef2ba0c667a..1d26203d1ced 100644
> --- a/include/linux/mm.h
> +++ b/include/linux/mm.h
> @@ -205,6 +205,26 @@ extern unsigned long sysctl_admin_reserve_kbytes;
> #define folio_page_idx(folio, p) ((p) - &(folio)->page)
> #endif
>
> +/*
> + * num_pages_contiguous() - determine the number of contiguous pages
> + * starting from the first page.
> + *
> + * @pages: an array of page pointers
> + * @nr_pages: length of the array
> + */
> +static inline unsigned long num_pages_contiguous(struct page **pages,
> + unsigned long nr_pages)
Both longs should be size_t I think
> +{
> + struct page *first_page = pages[0];
> + unsigned long i;
Size_t
> +
> + for (i = 1; i < nr_pages; i++)
> + if (pages[i] != nth_page(first_page, i))
> + break;
It seems OK. So the reasoning here is this is faster on
CONFIG_SPARSEMEM_VMEMMAP/nonsparse and about the same on sparse mem?
(or we don't care?)
Jason