All of lore.kernel.org
 help / color / mirror / Atom feed
From: Baoquan He <baoquan.he@linux.dev>
To: linux-mm@kvack.org
Cc: akpm@linux-foundation.org, david@kernel.org, chrisl@kernel.org,
	kasong@tencent.com, urezki@gmail.com, hannes@cmpxchg.org,
	corbet@lwn.net, nphamcs@gmail.com, baohua@kernel.org,
	youngjun.park@lge.com, shikemeng@huaweicloud.com,
	yosry@kernel.org, chengming.zhou@linux.dev, kunwu.chan@gmail.com,
	klarasmodin@gmail.com, linux-kernel@vger.kernel.org,
	baoquan.he@linux.dev, Baoquan He <hebaoquan@kylinos.cn>
Subject: [PATCH v7 01/14] mm/vmalloc: support vmalloc-backed VM_SPARSE areas
Date: Sun, 29 Nov 2026 23:07:42 +0800	[thread overview]
Message-ID: <20261129150755.191662-2-baoquan.he@linux.dev> (raw)
In-Reply-To: <20261129150755.191662-1-baoquan.he@linux.dev>

From: Baoquan He <hebaoquan@kylinos.cn>

VM_SPARSE areas are created with get_vm_area() and filled in by the
caller via vm_area_map_pages(), so the caller need to allocate the pages,
keep the struct page array and free everything by itself.

Add vm_area_grow_pages() and vm_area_shrink_pages() for VM_SPARSE |
VM_ALLOC areas. Pages come from the page allocator and are tracked in
area->pages, so the area is released with a single vfree(). The backed
pages always start at the beginning of the area: growing maps
[nr_pages, end) and shrinking unmaps and frees [end, nr_pages). Callers
serialize the two helpers.

nr_pages keeps its existing meaning of "number of backed pages", as
with vrealloc() and vread_iter(), and is updated under the vmap node's
busy.lock so /proc/vmallocinfo never sees a page that is already gone.
Only areas populated through vm_area_map_pages() stay a memory hole.
vm_area_map_pages() now rejects VM_ALLOC areas, because vfree() owns
their pages, to avoid double frees; free_vm_area() warns for the same
combination but not for plain VM_ALLOC areas.

Suggested-by: Chris Li <chrisl@kernel.org>
Signed-off-by: Baoquan He <hebaoquan@kylinos.cn>
---
 include/linux/vmalloc.h |   2 +
 mm/vmalloc.c            | 171 +++++++++++++++++++++++++++++++++++++---
 2 files changed, 160 insertions(+), 13 deletions(-)

diff --git a/include/linux/vmalloc.h b/include/linux/vmalloc.h
index 034a693777ca..9f40edcddfa8 100644
--- a/include/linux/vmalloc.h
+++ b/include/linux/vmalloc.h
@@ -296,6 +296,8 @@ int vm_area_map_pages(struct vm_struct *area, unsigned long start,
 		      unsigned long end, struct page **pages);
 void vm_area_unmap_pages(struct vm_struct *area, unsigned long start,
 			 unsigned long end);
+int vm_area_grow_pages(struct vm_struct *area, unsigned long end, gfp_t gfp);
+void vm_area_shrink_pages(struct vm_struct *area, unsigned long end);
 void vunmap_range(unsigned long addr, unsigned long end);
 
 static inline void set_vm_flush_reset_perms(void *addr)
diff --git a/mm/vmalloc.c b/mm/vmalloc.c
index ee722162da7c..4a8c35b958bb 100644
--- a/mm/vmalloc.c
+++ b/mm/vmalloc.c
@@ -788,6 +788,8 @@ static int check_sparse_vm_area(struct vm_struct *area, unsigned long start,
 		return -EINVAL;
 	if (WARN_ON_ONCE(!(area->flags & VM_SPARSE)))
 		return -EINVAL;
+	if (WARN_ON_ONCE(area->flags & VM_ALLOC))
+		return -EINVAL;
 	if ((end - start) >> PAGE_SHIFT > totalram_pages())
 		return -E2BIG;
 	if (start < (unsigned long)area->addr ||
@@ -4174,6 +4176,136 @@ static void *__vmalloc_area_node(struct vm_struct *area, gfp_t gfp_mask,
 	return NULL;
 }
 
+/**
+ * vm_area_grow_pages - back more of a sparse area with pages
+ * @area: area created with %VM_SPARSE | %VM_ALLOC
+ * @end: end of the range to back, %PAGE_SIZE aligned
+ * @gfp: gfp flags for the page allocation
+ *
+ * Allocate and map pages for [@area->nr_pages << PAGE_SHIFT, @end). The
+ * pages are zeroed when %__GFP_ZERO is set. Pages are always backed from
+ * the start of the area, so a grow only extends that range. An @end that
+ * is already backed is a no-op. On failure nothing is mapped and
+ * @area->nr_pages is unchanged.
+ *
+ * The area must be released with vfree(@area->addr). Callers must
+ * serialize calls to this function.
+ *
+ * Return: 0 on success, -errno on failure.
+ */
+int vm_area_grow_pages(struct vm_struct *area, unsigned long end, gfp_t gfp)
+{
+	unsigned long start = (unsigned long)area->nr_pages << PAGE_SHIFT;
+	unsigned long addr = (unsigned long)area->addr;
+	unsigned long nr_pages, nr_allocated, nr_end, cap;
+	gfp_t nested_gfp = (gfp & GFP_RECLAIM_MASK) | __GFP_ZERO;
+	struct vmap_node *vn;
+	struct page **pages;
+	unsigned int flags;
+	int ret;
+
+	might_sleep();
+
+	if (WARN_ON_ONCE(!(area->flags & VM_ALLOC) ||
+			 !(area->flags & VM_SPARSE)))
+		return -EINVAL;
+	if (WARN_ON_ONCE(!IS_ALIGNED(end, PAGE_SIZE)))
+		return -EINVAL;
+	if (WARN_ON_ONCE(end > get_vm_area_size(area)))
+		return -EINVAL;
+	if (end <= start)
+		return 0;
+	if ((end - start) >> PAGE_SHIFT > totalram_pages())
+		return -E2BIG;
+
+	nr_end = end >> PAGE_SHIFT;
+	cap = roundup_pow_of_two(nr_end);
+	pages = kvrealloc(area->pages, cap * sizeof(*area->pages),
+			  nested_gfp);
+	if (!pages)
+		return -ENOMEM;
+	area->pages = pages;
+
+	if (!(gfp & (GFP_DMA | GFP_DMA32)))
+		gfp |= __GFP_HIGHMEM;
+
+	nr_pages = (end - start) >> PAGE_SHIFT;
+	nr_allocated = vm_area_alloc_pages(vmalloc_gfp_adjust(gfp, 0),
+					   NUMA_NO_NODE, 0, nr_pages,
+					   area->pages + (start >> PAGE_SHIFT));
+	if (nr_allocated != nr_pages) {
+		vm_area_free_pages(area, start >> PAGE_SHIFT,
+				   (start >> PAGE_SHIFT) + nr_allocated);
+		return -ENOMEM;
+	}
+
+	flags = memalloc_apply_gfp_scope(gfp);
+	ret = __vmap_pages_range(addr + start, addr + end, PAGE_KERNEL,
+				 area->pages + (start >> PAGE_SHIFT),
+				 PAGE_SHIFT, nested_gfp);
+	memalloc_restore_scope(flags);
+	if (ret)
+		goto free_pages;
+
+	kasan_unpoison_vmalloc((void *)(addr + start), end - start,
+			       KASAN_VMALLOC_PROT_NORMAL |
+			       KASAN_VMALLOC_KEEP_TAG);
+
+	/*
+	 * Publish under busy.lock, like clear_vm_uninitialized_flag():
+	 * show_busy_info() reads nr_pages and pages[] under that lock.
+	 */
+	vn = addr_to_node(addr);
+	spin_lock(&vn->busy.lock);
+	WRITE_ONCE(area->nr_pages, end >> PAGE_SHIFT);
+	spin_unlock(&vn->busy.lock);
+	return 0;
+
+free_pages:
+	vunmap_range(addr + start, addr + end);
+	vm_area_free_pages(area, start >> PAGE_SHIFT, end >> PAGE_SHIFT);
+	return -ENOMEM;
+}
+
+/**
+ * vm_area_shrink_pages - release the pages backing the end of a sparse area
+ * @area: area created with %VM_SPARSE | %VM_ALLOC
+ * @end: new end of the backed range, %PAGE_SIZE aligned
+ *
+ * Unmap and free pages for [@end, @area->nr_pages << PAGE_SHIFT).  The
+ * released range can be backed again with vm_area_grow_pages().  Callers
+ * must serialize calls to this function.
+ */
+void vm_area_shrink_pages(struct vm_struct *area, unsigned long end)
+{
+	unsigned long start = (unsigned long)area->nr_pages << PAGE_SHIFT;
+	unsigned long addr = (unsigned long)area->addr;
+	struct vmap_node *vn;
+
+	might_sleep();
+
+	if (WARN_ON_ONCE(!(area->flags & VM_ALLOC) ||
+			 !(area->flags & VM_SPARSE)))
+		return;
+	if (WARN_ON_ONCE(!IS_ALIGNED(end, PAGE_SIZE)))
+		return;
+	if (end >= start)
+		return;
+
+	/*
+	 * Lower nr_pages under busy.lock before the pages go away: readers
+	 * walk pages[] up to nr_pages under that lock.
+	 */
+	vn = addr_to_node(addr);
+	spin_lock(&vn->busy.lock);
+	WRITE_ONCE(area->nr_pages, end >> PAGE_SHIFT);
+	spin_unlock(&vn->busy.lock);
+
+	kasan_poison_vmalloc((void *)(addr + end), start - end);
+	vunmap_range(addr + end, addr + start);
+	vm_area_free_pages(area, end >> PAGE_SHIFT, start >> PAGE_SHIFT);
+}
+
 /*
  * See __vmalloc_node_range() for a clear list of supported vmalloc flags.
  * This gfp lists all flags currently passed through vmalloc. Currently,
@@ -4952,18 +5084,21 @@ long vread_iter(struct iov_iter *iter, const char *addr, size_t count)
 		smp_rmb();
 
 		vaddr = (char *) va->va_start;
-		if (vm)
+		if (vm) {
 			/*
-			 * For VM_ALLOC areas, use nr_pages rather than
-			 * get_vm_area_size() because vrealloc() may shrink
-			 * the mapping without updating area->size. Other
-			 * mapping types (vmap, ioremap) don't set nr_pages.
+			 * Use nr_pages for VM_ALLOC areas: vrealloc() may
+			 * shrink them without updating area->size, and a
+			 * VM_SPARSE area is backed lazily, so its zero
+			 * prefix means nothing is mapped.
 			 */
-			size = (vm->flags & VM_ALLOC && vm->nr_pages) ?
-				       (vm->nr_pages << PAGE_SHIFT) :
-				       get_vm_area_size(vm);
-		else
+			if ((vm->flags & VM_ALLOC) &&
+			    (vm->nr_pages || (vm->flags & VM_SPARSE)))
+				size = vm->nr_pages << PAGE_SHIFT;
+			else
+				size = get_vm_area_size(vm);
+		} else {
 			size = va_size(va);
+		}
 
 		if (addr >= vaddr + size)
 			goto next_va;
@@ -4983,12 +5118,18 @@ long vread_iter(struct iov_iter *iter, const char *addr, size_t count)
 		if (n > remains)
 			n = remains;
 
-		if (flags & VMAP_RAM)
+		if (flags & VMAP_RAM) {
 			copied = vmap_ram_vread_iter(iter, addr, n, flags);
-		else if (!(vm && (vm->flags & (VM_IOREMAP | VM_SPARSE))))
-			copied = aligned_vread_iter(iter, addr, n);
-		else /* IOREMAP | SPARSE area is treated as memory hole */
+		} else if (vm && (vm->flags & VM_IOREMAP)) {
+			/* Not backed by struct pages. */
+			copied = zero_iter(iter, n);
+		} else if (vm && (vm->flags & VM_SPARSE) &&
+			   !(vm->flags & VM_ALLOC)) {
+			/* Populated by the caller; treat as a memory hole. */
 			copied = zero_iter(iter, n);
+		} else {
+			copied = aligned_vread_iter(iter, addr, n);
+		}
 
 		addr += copied;
 		remains -= copied;
@@ -5107,6 +5248,10 @@ EXPORT_SYMBOL(remap_vmalloc_range);
 void free_vm_area(struct vm_struct *area)
 {
 	struct vm_struct *ret;
+
+	/* VM_SPARSE | VM_ALLOC areas own their pages and need vfree(). */
+	WARN_ON_ONCE((area->flags & (VM_ALLOC | VM_SPARSE)) ==
+		     (VM_ALLOC | VM_SPARSE));
 	ret = remove_vm_area(area->addr);
 	BUG_ON(ret != area);
 	kfree(area);
-- 
2.54.0



  reply	other threads:[~2026-11-29 15:08 UTC|newest]

Thread overview: 20+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-29 15:07 [PATCH v7 00/14] mm, swap: extendable swap devices (xswap phase I) Baoquan He
2026-11-29 15:07 ` Baoquan He [this message]
2026-11-30 15:09   ` [PATCH v7 01/14] mm/vmalloc: support vmalloc-backed VM_SPARSE areas sashiko-bot
2026-11-29 15:07 ` [PATCH v7 02/14] mm: xswap support for zswap Baoquan He
2026-11-29 15:07 ` [PATCH v7 03/14] mm, swap: refactor free_swap_cluster_info to take swap_info_struct Baoquan He
2026-11-29 15:07 ` [PATCH v7 04/14] mm, swap: use a VM_SPARSE cluster array for xswap devices Baoquan He
2026-11-29 15:07 ` [PATCH v7 05/14] mm, swap: widen swap_info_struct max/pages to unsigned long Baoquan He
2026-11-29 15:07 ` [PATCH v7 06/14] mm, swap: create the xswap device from swapon(2) Baoquan He
2026-11-30 15:09   ` sashiko-bot
2026-11-29 15:07 ` [PATCH v7 07/14] mm, swap: refactor swapoff and add xswap_destroy Baoquan He
2026-11-29 15:07 ` [PATCH v7 08/14] mm, swap: require zswap for xswap devices Baoquan He
2026-11-29 15:07 ` [PATCH v7 09/14] mm, swap: let the command line set the xswap device size Baoquan He
2026-11-30 15:09   ` sashiko-bot
2026-11-29 15:07 ` [PATCH v7 10/14] mm, swap: add CONFIG_XSWAP_DEFAULT_MAX Baoquan He
2026-11-29 15:07 ` [PATCH v7 11/14] mm, swap: add xswap grow trigger on cluster allocation Baoquan He
2026-11-30 15:09   ` sashiko-bot
2026-11-29 15:07 ` [PATCH v7 12/14] mm, swap: add debugfs counters for xswap Baoquan He
2026-11-29 15:07 ` [PATCH v7 13/14] mm, swap: do not give an xswap slot to a cgroup that cannot zswap Baoquan He
2026-11-30 15:09   ` sashiko-bot
2026-11-29 15:07 ` [PATCH v7 14/14] mm, swap: skip the overcommit probe when removing xswap Baoquan He

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=20261129150755.191662-2-baoquan.he@linux.dev \
    --to=baoquan.he@linux.dev \
    --cc=akpm@linux-foundation.org \
    --cc=baohua@kernel.org \
    --cc=chengming.zhou@linux.dev \
    --cc=chrisl@kernel.org \
    --cc=corbet@lwn.net \
    --cc=david@kernel.org \
    --cc=hannes@cmpxchg.org \
    --cc=hebaoquan@kylinos.cn \
    --cc=kasong@tencent.com \
    --cc=klarasmodin@gmail.com \
    --cc=kunwu.chan@gmail.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-mm@kvack.org \
    --cc=nphamcs@gmail.com \
    --cc=shikemeng@huaweicloud.com \
    --cc=urezki@gmail.com \
    --cc=yosry@kernel.org \
    --cc=youngjun.park@lge.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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.