From mboxrd@z Thu Jan 1 00:00:00 1970 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 6C2463E5564 for ; Mon, 2 Mar 2026 13:57:37 +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=1772459857; cv=none; b=ed4jpY08SAFpa0jhg+cRqSSR6qGW3dkI6lx564i3RfAnUZ41l20PN+zRcfQAdXGInwvz/L8DjBbFHBK2aLDiJ62u0tgogHIVcnfHKxoVJkEYQqA3gS+vWZa7EQxRbuj0tdivRJ/cxdPcHm7ylw+MpTI3ylnnAy9Bc2f5vIKx+68= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1772459857; c=relaxed/simple; bh=SGZI8pDW0epe37A1Z7dyf0Tg/40E/cxPxi86tx3hh/A=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=sc2OtQVkidsD+h/qlFoeBqWHhORAp7s8WlF9XUClUQ1ivu/VVufoq0Q5Yn8W074RQt0FyWJ3ma5dG2scEyRtwwVUkjW0AD/qq8r5dRGpqFQxt8y393G7T8RsfUlQo888uFoS9cswvwSicOqkI4ci7208/rmhD9Fz9USioSbtlX8= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=OVT6eiUl; 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="OVT6eiUl" Received: by smtp.kernel.org (Postfix) with ESMTPS id 3A41DC2BC87; Mon, 2 Mar 2026 13:57:37 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1772459857; bh=SGZI8pDW0epe37A1Z7dyf0Tg/40E/cxPxi86tx3hh/A=; h=From:Date:Subject:References:In-Reply-To:To:Cc:Reply-To:From; b=OVT6eiUl3Oy/gfAiLymmF7YVs/tm6NZ6ASwXX2fWqmL7s+zWKjd549IuLenZGbrSo YLo/esTjH/fAigYX47zwplJr7Kdkt+wFmX5f/2vUGcC7N8XIaIYjNWjkDupUwKB0O9 tZvWSgdM5X7mLGNPrrdbaWqAnpKi4N4g+pDjVCMgb5qWmmY6nmgPaVKQ5x0jjrwNtW 34xn9ZGmuggpZyC9OVd9nMh0jcR7xXGk2i3pxaVv9S1WlelmUIw610+DzqOlJ+ijbv 3j9QNiJ0FfOFRL4wMooYRumNLqYe+5diTiTmRg3AShzkInFJLHG2H8uX0FeokyDLc1 wgQVz0UgjqMVQ== 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 263F3EA4E04; Mon, 2 Mar 2026 13:57:37 +0000 (UTC) From: Shivam Kalra via B4 Relay Date: Mon, 02 Mar 2026 19:27:11 +0530 Subject: [PATCH 1/2] mm/vmalloc: extract vmalloc_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: 7bit Message-Id: <20260302-vmalloc-shrink-v1-1-46deff465b7e@zohomail.in> References: <20260302-vmalloc-shrink-v1-0-46deff465b7e@zohomail.in> In-Reply-To: <20260302-vmalloc-shrink-v1-0-46deff465b7e@zohomail.in> To: Andrew Morton , Uladzislau Rezki Cc: linux-mm@kvack.org, linux-kernel@vger.kernel.org, Shivam Kalra X-Mailer: b4 0.14.3 X-Developer-Signature: v=1; a=ed25519-sha256; t=1772459853; l=2751; i=shivamkalra98@zohomail.in; s=20260212; h=from:subject:message-id; bh=0JaRY63WuGpryYiSzUoBhSyJJKl7JuNe9P31f6tdjtc=; b=hWTwNZvKyvuYQpyWQFxgnjJGMxbWDPnuQ7m7FkLTfXN+BeWu1g4CHLZFsNnvu45DMMwYVa3qT pWbsHO4Vr70APHUiO3ECVzEapWycFN5r8gTidioHz35BeGbPDDGK3E2 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, memcg accounting, and nr_vmalloc_pages update from vfree() into a reusable vmalloc_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). No functional change. Signed-off-by: Shivam Kalra --- mm/vmalloc.c | 48 +++++++++++++++++++++++++++++++----------------- 1 file changed, 31 insertions(+), 17 deletions(-) diff --git a/mm/vmalloc.c b/mm/vmalloc.c index 628f96e83b118..54e76a47e9957 100644 --- a/mm/vmalloc.c +++ b/mm/vmalloc.c @@ -3409,6 +3409,35 @@ void vfree_atomic(const void *addr) schedule_work(&p->wq); } +/* + * vmalloc_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 memcg accounting and the global + * nr_vmalloc_pages counter. Caller is responsible for unmapping + * (vunmap_range) and KASAN poisoning before calling this. + */ +static void vmalloc_free_pages(struct vm_struct *vm, + unsigned int start, unsigned int end) +{ + unsigned int i; + unsigned int nr = end - start; + + if (!(vm->flags & VM_MAP_PUT_PAGES)) + mod_memcg_page_state(vm->pages[start], MEMCG_VMALLOC, -nr); + + for (i = start; i < end; i++) { + BUG_ON(!vm->pages[i]); + __free_page(vm->pages[i]); + cond_resched(); + } + + if (!(vm->flags & VM_MAP_PUT_PAGES)) + atomic_long_sub(nr, &nr_vmalloc_pages); +} + /** * vfree - Release memory allocated by vmalloc() * @addr: Memory base address @@ -3429,7 +3458,6 @@ void vfree_atomic(const void *addr) void vfree(const void *addr) { struct vm_struct *vm; - int i; if (unlikely(in_interrupt())) { vfree_atomic(addr); @@ -3452,22 +3480,8 @@ void vfree(const void *addr) if (unlikely(vm->flags & VM_FLUSH_RESET_PERMS)) vm_reset_perms(vm); - /* All pages of vm should be charged to same memcg, so use first one. */ - if (vm->nr_pages && !(vm->flags & VM_MAP_PUT_PAGES)) - mod_memcg_page_state(vm->pages[0], MEMCG_VMALLOC, -vm->nr_pages); - for (i = 0; i < vm->nr_pages; i++) { - struct page *page = 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 - */ - __free_page(page); - cond_resched(); - } - if (!(vm->flags & VM_MAP_PUT_PAGES)) - atomic_long_sub(vm->nr_pages, &nr_vmalloc_pages); + if (vm->nr_pages) + vmalloc_free_pages(vm, 0, vm->nr_pages); kvfree(vm->pages); kfree(vm); } -- 2.43.0