From: deepakraog <gaikwad.dcg@gmail.com>
To: Andrew Morton <akpm@linux-foundation.org>,
Uladzislau Rezki <urezki@gmail.com>,
linux-mm@kvack.org, linux-kernel@vger.kernel.org
Subject: [PATCH 3/4] mm: vrealloc: grow vm_area mappings in place
Date: Wed, 15 Jul 2026 20:06:43 +0530 [thread overview]
Message-ID: <20260715143646.15828-3-gaikwad.dcg@gmail.com> (raw)
In-Reply-To: <20260715143646.15828-1-gaikwad.dcg@gmail.com>
When vrealloc() needs more pages than are currently mapped, allocate and
map additional pages at the end of the existing vm_area instead of
always allocating a new area and memcpy()'ing. Fall back to the
existing realloc path when in-place grow is not possible.
Signed-off-by: deepakraog <gaikwad.dcg@gmail.com>
---
mm/vmalloc.c | 50 +++++++++++++++++++++++++++++++++++++++++++++++++-
1 file changed, 49 insertions(+), 1 deletion(-)
diff --git a/mm/vmalloc.c b/mm/vmalloc.c
index 1afca3568..46cf10c62 100644
--- a/mm/vmalloc.c
+++ b/mm/vmalloc.c
@@ -4426,8 +4426,56 @@ void *vrealloc_node_align_noprof(const void *p, size_t size, unsigned long align
return (void *)p;
}
+ {
+ unsigned int new_nr_pages = PAGE_ALIGN(size) >> PAGE_SHIFT;
+ unsigned int old_nr_pages = vm->nr_pages;
+
+ if (new_nr_pages > old_nr_pages && !vm_area_page_order(vm) &&
+ !(vm->flags & (VM_FLUSH_RESET_PERMS | VM_USERMAP)) &&
+ gfp_has_io_fs(flags)) {
+ unsigned int extra = new_nr_pages - old_nr_pages;
+ unsigned long addr = (unsigned long)kasan_reset_tag(p);
+ struct vmap_node *vn = addr_to_node(addr);
+ struct page **pages;
+ int ret;
+
+ pages = kvmalloc_array(extra, sizeof(struct page *), gfp);
+ if (!pages)
+ goto need_realloc;
+
+ if (vm_area_alloc_pages(vmalloc_gfp_adjust(gfp, 0), nid,
+ 0, extra, pages) != extra) {
+ kvfree(pages);
+ goto need_realloc;
+ }
+
+ spin_lock(&vn->busy.lock);
+ ret = vmap_pages_range(addr + ((unsigned long)old_nr_pages
+ << PAGE_SHIFT),
+ addr + ((unsigned long)new_nr_pages
+ << PAGE_SHIFT),
+ PAGE_KERNEL, pages, PAGE_SHIFT);
+ spin_unlock(&vn->busy.lock);
+ if (ret < 0) {
+ free_pages_bulk(pages, extra);
+ kvfree(pages);
+ goto need_realloc;
+ }
+
+ memcpy(&vm->pages[old_nr_pages], pages,
+ extra * sizeof(struct page *));
+ kvfree(pages);
+ vm->nr_pages = new_nr_pages;
+ vm->requested_size = size;
+ kasan_vrealloc(p, old_size, size);
+ if (want_init_on_alloc(flags))
+ memset((void *)p + old_size, 0, size - old_size);
+ return (void *)p;
+ }
+ }
+
need_realloc:
- /* TODO: Grow the vm_area, i.e. allocate and map additional pages. */
+ /* Fall back to a fresh vm_area when in-place grow is not possible. */
n = __vmalloc_node_noprof(size, align, flags, nid, __builtin_return_address(0));
if (!n)
--
Deepak Rao Gaikwad
next prev parent reply other threads:[~2026-07-15 14:37 UTC|newest]
Thread overview: 5+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-07-15 14:36 [PATCH 1/4] mm: vmpressure: scale sampling window with machine size deepakraog
2026-07-15 14:36 ` [PATCH 2/4] selftests/proc: expand smaps field coverage and require TMPFS deepakraog
2026-07-15 14:36 ` deepakraog [this message]
2026-07-15 14:36 ` [PATCH 4/4] ceph: release unused cap reservation on readdir error deepakraog
2026-07-15 17:51 ` Viacheslav Dubeyko
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=20260715143646.15828-3-gaikwad.dcg@gmail.com \
--to=gaikwad.dcg@gmail.com \
--cc=akpm@linux-foundation.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-mm@kvack.org \
--cc=urezki@gmail.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
Powered by JetHome