From nobody Tue Apr 7 11:34:06 2026 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id CF41E282F03 for ; Sat, 14 Mar 2026 09:06:06 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1773479166; cv=none; b=CU1Tf1iV20aGPeFwpHX5m/k7ys7e5yoZTalRWUecrjH3WYwLTISBxtrMJ2OM25KWZJiGBX7kM9dSuff6HahrKexus+JnagSWx//60ICDNztXdwvHyrQtlmdk8ZUBzT0OusZ4MLdqJz1THbI4dQge4mFmkJm1mJ+qbs3Ra/z/Q94= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1773479166; c=relaxed/simple; bh=k7MxutspFB0C8aBYZ+Cn6E/z5wK6f9LqrZ6dx5IdwE4=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=QsuCEZnzBlhoeKIfuxCzTmb2N0fYpFb9K9tSu2OHnuw2rQ3/ZSbn9f0xpqK5td6Sa1auscEKj12dU05UZ42xyfOlU4oqPjtyTVPY1rPnG/Pmz13SrF6sMidBetapXcbDEYdkKkstO/ujHO+PuTjaSwLng051HNCim/avtvgDtWk= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=k0vYIYNN; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="k0vYIYNN" Received: by smtp.kernel.org (Postfix) with ESMTPS id A7966C19423; Sat, 14 Mar 2026 09:06:06 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1773479166; bh=k7MxutspFB0C8aBYZ+Cn6E/z5wK6f9LqrZ6dx5IdwE4=; h=From:Date:Subject:References:In-Reply-To:To:Cc:Reply-To:From; b=k0vYIYNNUZszXtEF3c7cYvaTfOPFdhfJnGhWlah7FR6LcTBSE3Copuw1Ghl5mNgiT BHLje0y3QWpPBe8L/hxpHHQnx2v7QokXelxssmVQs7+INzIdYkDe01L/+XpWHV6nZ8 GQXjQqDN1W+rek69CgY3TdkUyFCND5eDGdrqHQSYuyGgypuG3LxnkVbI5tLTYC6JU9 CLFJ+q0wi6gjlMJh/XO+21hNv0Mk/Lk4vmb5DzFqcIyR78CFIm3XDe+yO3ztXCPAqR jIManz8ibGa2fCMBZyVgExYgmsJKaXviz2ZReZJHHJD0glahLI44bsfGAAlI8v20kL nH2vdPBpgx7Qw== Received: from aws-us-west-2-korg-lkml-1.web.codeaurora.org (localhost.localdomain [127.0.0.1]) by smtp.lore.kernel.org (Postfix) with ESMTP id 96A8B106ACF9; Sat, 14 Mar 2026 09:06:06 +0000 (UTC) From: Shivam Kalra via B4 Relay Date: Sat, 14 Mar 2026 14:34:13 +0530 Subject: [PATCH v4 1/3] mm/vmalloc: extract vm_area_free_pages() helper from vfree() Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20260314-vmalloc-shrink-v4-1-c1e2e0bb5455@zohomail.in> References: <20260314-vmalloc-shrink-v4-0-c1e2e0bb5455@zohomail.in> In-Reply-To: <20260314-vmalloc-shrink-v4-0-c1e2e0bb5455@zohomail.in> To: Andrew Morton , Uladzislau Rezki Cc: linux-mm@kvack.org, linux-kernel@vger.kernel.org, Alice Ryhl , Danilo Krummrich , Shivam Kalra X-Mailer: b4 0.14.3 X-Developer-Signature: v=1; a=ed25519-sha256; t=1773479164; l=2682; i=shivamkalra98@zohomail.in; s=20260212; h=from:subject:message-id; bh=Y7LaORkgehK4lKdbYmYreDeAD1S2R1PpyepoUGhj4EQ=; b=xQ9l8MuWzhTsdTNLyNVGABBwcIWNRVDpsUQH14mvklMOUh9yd46JjQO6KBF9FtlZ6okPsu0kW czUwPJswlxfB04TFAQkB2OrJh05VSSHcWuhq0GVTrJEWsO/lg77OPZ8 X-Developer-Key: i=shivamkalra98@zohomail.in; a=ed25519; pk=9Q+S1LD/xjbjL7bEaLIlwRADBwU/6LJq7lYm8LFrkQE= X-Endpoint-Received: by B4 Relay for shivamkalra98@zohomail.in/20260212 with auth_id=633 X-Original-From: Shivam Kalra Reply-To: shivamkalra98@zohomail.in From: Shivam Kalra Extract the page-freeing loop and NR_VMALLOC stat accounting from vfree() into a reusable vm_area_free_pages() helper. The helper operates on a range [start, end) of pages from a vm_struct, making it suitable for both full free (vfree) and partial free (upcoming vrealloc shrink). Freed page pointers in vm->pages[] are set to NULL to prevent stale references when the vm_struct outlives the free (as in vrealloc shrink). Signed-off-by: Shivam Kalra --- mm/vmalloc.c | 47 +++++++++++++++++++++++++++++++++-------------- 1 file changed, 33 insertions(+), 14 deletions(-) diff --git a/mm/vmalloc.c b/mm/vmalloc.c index c607307c657a..b29bf58c0e3f 100644 --- a/mm/vmalloc.c +++ b/mm/vmalloc.c @@ -3416,6 +3416,38 @@ void vfree_atomic(const void *addr) schedule_work(&p->wq); } =20 +/* + * vm_area_free_pages - free a range of pages from a vmalloc allocation + * @vm: the vm_struct containing the pages + * @start: first page index to free (inclusive) + * @end: last page index to free (exclusive) + * + * Free pages [start, end) updating NR_VMALLOC stat accounting. + * Freed vm->pages[] entries are set to NULL. + * Caller is responsible for unmapping (vunmap_range) and KASAN + * poisoning before calling this. + */ +static void vm_area_free_pages(struct vm_struct *vm, unsigned int start, + unsigned int end) +{ + unsigned int i; + + for (i =3D start; i < end; i++) { + struct page *page =3D vm->pages[i]; + + BUG_ON(!page); + /* + * High-order allocs for huge vmallocs are split, so + * can be freed as an array of order-0 allocations + */ + if (!(vm->flags & VM_MAP_PUT_PAGES)) + mod_lruvec_page_state(page, NR_VMALLOC, -1); + __free_page(page); + vm->pages[i] =3D NULL; + cond_resched(); + } +} + /** * vfree - Release memory allocated by vmalloc() * @addr: Memory base address @@ -3436,7 +3468,6 @@ void vfree_atomic(const void *addr) void vfree(const void *addr) { struct vm_struct *vm; - int i; =20 if (unlikely(in_interrupt())) { vfree_atomic(addr); @@ -3459,19 +3490,7 @@ void vfree(const void *addr) =20 if (unlikely(vm->flags & VM_FLUSH_RESET_PERMS)) vm_reset_perms(vm); - for (i =3D 0; i < vm->nr_pages; i++) { - struct page *page =3D vm->pages[i]; - - BUG_ON(!page); - /* - * High-order allocs for huge vmallocs are split, so - * can be freed as an array of order-0 allocations - */ - if (!(vm->flags & VM_MAP_PUT_PAGES)) - mod_lruvec_page_state(page, NR_VMALLOC, -1); - __free_page(page); - cond_resched(); - } + vm_area_free_pages(vm, 0, vm->nr_pages); kvfree(vm->pages); kfree(vm); } --=20 2.43.0