From nobody Wed Apr 1 22:13:35 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 EF3BC37F00D for ; Wed, 1 Apr 2026 17:16:49 +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=1775063810; cv=none; b=lfpV4zAKhoABF+Sp2bzCu5NyF0aiQ+xV+2rlJ42MwdcZpao+ADrFhPKKz99/EMEmwArogH11i68FE89738EzvM2zQsO68TKCzjHjO/5X2MNtycQJkl0KFPnntB0TVKVjUPu/acZJ9vkzbsljb6H6E58ugvie2NHzKGvBWqq/70c= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1775063810; c=relaxed/simple; bh=g2XGgGei5/9K901lcE8j/TYckn3BeFqUsM0mB2rJlX8=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=iW4IK74uQhiPIFrD/1XaDivCneUGE7L2O6OUS9BSIkE9FLhW2YzNtPKe1q/f/l7P98o5qiPFzQuZbPK9MMCohpnjbb3FoRFcA0INQib/vDMRePHx9fHvi+sYSEKDUxQiJ3ihW9rWnc2yO7J0KrwH9/XCiGXZl74gPf4gVX+DpSc= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=r3TyOWoa; 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="r3TyOWoa" Received: by smtp.kernel.org (Postfix) with ESMTPS id C404AC19423; Wed, 1 Apr 2026 17:16:49 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1775063809; bh=g2XGgGei5/9K901lcE8j/TYckn3BeFqUsM0mB2rJlX8=; h=From:Date:Subject:References:In-Reply-To:To:Cc:Reply-To:From; b=r3TyOWoa+zluNcik/LoELjg8BZT6qRfY/Qfkj0+XovMfqSHjlBg3baQmWN9p/wZgC EEPaJFGJiWIM9RXpacgHSWzC8MxBgoght9QG4wyukbjMbXcnRQ7U3I0UhrPQ/R6a5H 3fy/XpMn6fQeO4oHVVrSsy1dM3p+fBHvHw7XGNOe7xvXjHPOAe5ffXfcYFJUECGMOa NicP2aP+0RGF76diAOE9wKMCIeRddU/sHzVmzUT8Zqe0EOUUv1Xpnh/u/BZmuT89MZ Aoj8P81XLDsR9WircFZneW4Dqpn3Jo8gKKT+3BxKgVOsKG0Hzs92Sq6vLWPx/DTfs7 udMA4FWNeY3Jw== 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 AFEF61076386; Wed, 1 Apr 2026 17:16:49 +0000 (UTC) From: Shivam Kalra via B4 Relay Date: Wed, 01 Apr 2026 22:46:33 +0530 Subject: [PATCH v9 1/4] 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: <20260401-vmalloc-shrink-v9-1-bf58dfb997d8@zohomail.in> References: <20260401-vmalloc-shrink-v9-0-bf58dfb997d8@zohomail.in> In-Reply-To: <20260401-vmalloc-shrink-v9-0-bf58dfb997d8@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=1775063806; l=2828; i=shivamkalra98@zohomail.in; s=20260212; h=from:subject:message-id; bh=8O2oBTeVmP5owdpoE0RYwkP6QcFE0K1KL1uyRgBELyY=; b=6jid7SK6D0wuU3u+YB/vxMxjWUUdmWgQlWnWXgP2sZYsn6O9DV3Pny1JVzjbhgPQYpWaz2/h7 SjtzPGetQTGDRuV/FuPOpmtF5AQQvhO3F4fDvJKo4sTpMbKK60oyFMF 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_idx, end_idx) 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). Reviewed-by: Alice Ryhl Reviewed-by: Uladzislau Rezki (Sony) 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 57eae99d9909..fe8700270139 100644 --- a/mm/vmalloc.c +++ b/mm/vmalloc.c @@ -3424,6 +3424,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_idx: first page index to free (inclusive) + * @end_idx: last page index to free (exclusive) + * + * Free pages [start_idx, end_idx) 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_id= x, + unsigned int end_idx) +{ + unsigned int i; + + for (i =3D start_idx; i < end_idx; 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 @@ -3444,7 +3476,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); @@ -3467,19 +3498,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