[RFC PATCH 3/6] mm/vmalloc: try add vmap folios range

Huan Yang
2025-03-27T09:30:18Z

This patch add a new api vmap_range_folios.

It support both entire folio array and range based
folio array.

By this, some situation can support, like HVO folio range vmap.
Also can reduce array size, compare to 4k based page array, only need to
record each folio and it's range(offset, nr_pages).

Signed-off-by: Huan Yang <link@vivo.com>
---
include/linux/vmalloc.h | 57 +++++++++++++++++++++++++++++++++++++++++
mm/vmalloc.c | 47 +++++++++++++++++++++++++++++++++
2 files changed, 104 insertions(+)

diff --git a/include/linux/vmalloc.h b/include/linux/vmalloc.h
index 31e9ffd936e3..007a398152af 100644
--- a/include/linux/vmalloc.h
+++ b/include/linux/vmalloc.h
@@ -11,6 +11,7 @@
#include <asm/page.h> /* pgprot_t */
#include <linux/rbtree.h>
#include <linux/overflow.h>
+#include <linux/mm.h>

#include <asm/vmalloc.h>

@@ -83,6 +84,24 @@ struct vmap_area {
unsigned long flags; /* mark type of vm_map_ram area */
};

+/**
+ * folio range [pgoff, pgoff + nr_pages) in [0, folio_nr_pages)
+ */
+struct vmap_folios_range {
+ struct folio *folio;
+
+ pgoff_t pgoff;
+ unsigned int nr_pages;
+};
+
+struct vmap_folios_struct {
+ union {
+ struct folio **_folios;
+ struct vmap_folios_range *_range_folios;
+ };
+ bool range;
+};
+
/* archs that select HAVE_ARCH_HUGE_VMAP should override one or more of these */
#ifndef arch_vmap_p4d_supported
static inline bool arch_vmap_p4d_supported(pgprot_t prot)
@@ -195,6 +214,44 @@ extern void *vmap(struct page **pages, unsigned int count,
void *vmap_pfn(unsigned long *pfns, unsigned int count, pgprot_t prot);
extern void vunmap(const void *addr);

+extern void *__vmap_range_folios(struct vmap_folios_struct *folios,
+ unsigned int count, unsigned int size,
+ unsigned long flags, pgprot_t prot);
+
+static inline void *vmap_range_folios(struct vmap_folios_struct *folios,
+ unsigned int count, unsigned long flags,
+ pgprot_t prot)
+{
+ unsigned int size, i;
+
+ for (i = 0, size = 0; i < count; ++i) {
+ struct vmap_folios_range *range = &folios->_range_folios[i];
+
+ if (WARN_ON(!range || !range->folio))
+ return NULL;
+
+ if (WARN_ON(range->pgoff + range->nr_pages >
+ folio_nr_pages(range->folio)))
+ return NULL;
+
+ size += PAGE_SIZE * range->nr_pages;
+ }
+
+ return __vmap_range_folios(folios, count, size, flags, prot);
+}
+
+static inline void *vmap_folios(struct vmap_folios_struct *folios,
+ unsigned int count, unsigned long flags,
+ pgprot_t prot)
+{
+ unsigned int size, i;
+
+ for (i = 0, size = 0; i < count; ++i)
+ size += folio_size(folios->_folios[i]);
+
+ return __vmap_range_folios(folios, count, size, flags, prot);
+}
+
extern int remap_vmalloc_range_partial(struct vm_area_struct *vma,
unsigned long uaddr, void *kaddr,
unsigned long pgoff, unsigned long size);
diff --git a/mm/vmalloc.c b/mm/vmalloc.c
index 044af7088359..247a4b940be1 100644
--- a/mm/vmalloc.c
+++ b/mm/vmalloc.c
@@ -3530,6 +3530,53 @@ void *vmap_pfn(unsigned long *pfns, unsigned int count, pgprot_t prot)
EXPORT_SYMBOL_GPL(vmap_pfn);
#endif /* CONFIG_VMAP_PFN */

+void *__vmap_range_folios(struct vmap_folios_struct *folios, unsigned int count,
+ unsigned int size, unsigned long flags, pgprot_t prot)
+{
+ struct vm_struct *area;
+ unsigned long addr;
+ unsigned int i;
+
+ area = get_vm_area_caller(size, flags, __builtin_return_address(0));
+ if (!area)
+ return NULL;
+
+ addr = (unsigned long)area->addr;
+ for (i = 0; i < count; ++i) {
+ unsigned int nr_pages;
+ unsigned long pfn;
+ int err;
+
+ if (folios->range) {
+ struct vmap_folios_range *folio_range =
+ &folios->_range_folios[i];
+
+ pfn = folio_pfn(folio_range->folio) +
+ folio_range->pgoff;
+ nr_pages = folio_range->nr_pages;
+ } else {
+ struct folio *folio = folios->_folios[i];
+
+ pfn = folio_pfn(folio);
+ nr_pages = folio_nr_pages(folio);
+ }
+
+ err = vmap_range_noflush(addr, addr + (nr_pages << PAGE_SHIFT),
+ PFN_PHYS(pfn), prot, PAGE_SHIFT);
+ if (err) {
+ free_vm_area(area);
+ return NULL;
+ }
+
+ addr += (nr_pages << PAGE_SHIFT);
+ }
+
+ flush_cache_vmap((unsigned long)area->addr,
+ (unsigned long)area->addr + size);
+ return area->addr;
+}
+EXPORT_SYMBOL_GPL(__vmap_range_folios);
+
static inline unsigned int
vm_area_alloc_pages(gfp_t gfp, int nid,
unsigned int order, unsigned int nr_pages, struct page **pages)
--
2.48.1