From nobody Thu Apr 16 05:08:38 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 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: quoted-printable 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); } =20 +/* + * 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 =3D end - start; + + if (!(vm->flags & VM_MAP_PUT_PAGES)) + mod_memcg_page_state(vm->pages[start], MEMCG_VMALLOC, -nr); + + for (i =3D 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; =20 if (unlikely(in_interrupt())) { vfree_atomic(addr); @@ -3452,22 +3480,8 @@ void vfree(const void *addr) =20 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 =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 - */ - __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); } --=20 2.43.0 From nobody Thu Apr 16 05:08:38 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 6C2DC3E5582 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=sJkv2RHikRUWyN89Bj3YHOKLDcAQkKIWdbZqG8p85th4zfNT69NvW4g1B62ilkfKjmq++l+4RwreLnKI8IEtacKiXRYsf5wcycJZ/6WFGthSxQNPe27hbO9lWI+1bkiwtrP/ejob1RddkgkumEXkd3whCDF6J5tvBUUuMPGPrqI= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1772459857; c=relaxed/simple; bh=uLsMOF44rMJZ/TvDeYiLPdJwJ5UTQPAzG+OJCt0sUVY=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=gh4YtIOMxAOpshzgewBmpiyehM5B7qVDpR40FKtb+Fp9cNLm1Tg7zOfgRn1km3VwssLcBnbeYf3Ae4SxwBuzqbR0Idn7sCZySFhNReUGoGJ2toPxCyqNUYr2L1WAncqxRPDF06lfKy1ZHGYsNOpDl1HA7e4HvKY3dmmf95pjz8I= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=AhpV3jtf; 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="AhpV3jtf" Received: by smtp.kernel.org (Postfix) with ESMTPS id 4380EC2BCAF; 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=uLsMOF44rMJZ/TvDeYiLPdJwJ5UTQPAzG+OJCt0sUVY=; h=From:Date:Subject:References:In-Reply-To:To:Cc:Reply-To:From; b=AhpV3jtf90t9D0MsqAvUADUen8OxXt6WiK3KhJ18SP77E8jxYR52DQkHAg7xqGcAc 2R0BFW9YpjA2vC9etB8EwizVIBQe6qNdm1ixriVQWTCSaqVuOez2HrlrcTuTULAixy g9yVUG0UfuITDZVQ3J02VYnJD0VrsHyvNF1bGoCfPG90sefCYyXZRAN/vwyxPMh42M 2x8X7EVv0+7mmxOz/wbDi7gz5Srr6p0PSx/LvZsPWHZdnrrQtVfsCqoLhDUdph/OyM C1YXNrEDarMJzPap0SsvckVawqXl2qo8U/EBWesZ5myvN3DYUhyd3OTEyT6ZVllvbg VoEdx+eDmNLLQ== 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 399C6EA4E09; Mon, 2 Mar 2026 13:57:37 +0000 (UTC) From: Shivam Kalra via B4 Relay Date: Mon, 02 Mar 2026 19:27:12 +0530 Subject: [PATCH 2/2] mm/vmalloc: free unused pages on vrealloc() shrink 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: <20260302-vmalloc-shrink-v1-2-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=2712; i=shivamkalra98@zohomail.in; s=20260212; h=from:subject:message-id; bh=1erzrKM7o2q1ZdLZf3ysmYhvkVLpMZRusvoIpirIWHk=; b=s8dCbDyuolYo1EwgvZlaLveGaiqIPpn8jL2D/pue+DAQCoZWL5qLQQu2bmpearqaj1v9tgV41 Z9VRutTIjuiBZHd/CR4Mhuj2rKwBIB21N2vcFieESoQCCZb6iesAsmH 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 When vrealloc() shrinks an allocation and the new size crosses a page boundary, unmap and free the tail pages that are no longer needed. This reclaims physical memory that was previously wasted for the lifetime of the allocation. The heuristic is simple: always free when at least one full page becomes unused. Huge page allocations (page_order > 0) are skipped, as partial freeing would require splitting. The virtual address reservation (vm->size / vmap_area) is intentionally kept unchanged, preserving the address for potential future grow-in-place support. Fix the grow-in-place check to compare against vm->nr_pages rather than get_vm_area_size(), since the latter reflects the virtual reservation which does not shrink. Without this fix, a grow after shrink would access freed pages. Signed-off-by: Shivam Kalra --- mm/vmalloc.c | 25 ++++++++++++++++++++----- 1 file changed, 20 insertions(+), 5 deletions(-) diff --git a/mm/vmalloc.c b/mm/vmalloc.c index 54e76a47e995..7a4c59422638 100644 --- a/mm/vmalloc.c +++ b/mm/vmalloc.c @@ -4327,14 +4327,29 @@ void *vrealloc_node_align_noprof(const void *p, siz= e_t size, unsigned long align goto need_realloc; } =20 - /* - * TODO: Shrink the vm_area, i.e. unmap and free unused pages. What - * would be a good heuristic for when to shrink the vm_area? - */ if (size <=3D old_size) { + unsigned int new_nr_pages =3D PAGE_ALIGN(size) >> PAGE_SHIFT; + /* Zero out "freed" memory, potentially for future realloc. */ if (want_init_on_free() || want_init_on_alloc(flags)) memset((void *)p + size, 0, old_size - size); + + /* Free tail pages when shrink crosses a page boundary. */ + if (new_nr_pages < vm->nr_pages && + !vm_area_page_order(vm)) { + unsigned long addr =3D (unsigned long)p; + + vunmap_range(addr + (new_nr_pages << PAGE_SHIFT), + addr + (vm->nr_pages << PAGE_SHIFT)); + + kasan_poison_vmalloc( + (void *)(addr + (new_nr_pages << PAGE_SHIFT)), + (vm->nr_pages - new_nr_pages) << PAGE_SHIFT); + + vmalloc_free_pages(vm, new_nr_pages, vm->nr_pages); + vm->nr_pages =3D new_nr_pages; + } + vm->requested_size =3D size; kasan_poison_vmalloc(p + size, old_size - size); return (void *)p; @@ -4343,7 +4358,7 @@ void *vrealloc_node_align_noprof(const void *p, size_= t size, unsigned long align /* * We already have the bytes available in the allocation; use them. */ - if (size <=3D alloced_size) { + if (size <=3D (size_t)vm->nr_pages << PAGE_SHIFT) { kasan_unpoison_vmalloc(p + old_size, size - old_size, KASAN_VMALLOC_PROT_NORMAL | KASAN_VMALLOC_VM_ALLOC | --=20 2.43.0