From nobody Mon Apr 6 16:47:29 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 3A3AE1A6805 for ; Sat, 4 Apr 2026 08:37:16 +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=1775291836; cv=none; b=LDr/k9qWMCpOE6sVbJZAipwEZP/BAwwGbKLgyi1Ri/VY1v6O+H+bjIkANYZpGw36uw9kKJsP3R16LnVSOhd1/rUjScglYiZfKEE2bAi/So27T6VeGj2+UuAfTd9sOpByMwbrZ+r+DeDX4KGMTL1Vg2o4dsvv9SvjF6QaWqO0XS0= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1775291836; c=relaxed/simple; bh=16rkqI0szlH/LFm5GEEQXbW6/vo6EV3zeZslKn4GxMw=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=sIb812ZpbIGRdzf+agQWfNtPAvY6GJyc84vSeWlltPUsrmy+nlRgFXUfhO6GDlV3k54N0CQpGISgmrrY8z6A2exBuMwvL9md8Zdt+NmJJSD8LbNeKNYjM0M6nE9+cYqdU5fFg2APaHWAF3eqKK2d3M2AXYFGH1KoVSJpOE0UECI= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=HOIEi3IQ; 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="HOIEi3IQ" Received: by smtp.kernel.org (Postfix) with ESMTPS id D2A40C19425; Sat, 4 Apr 2026 08:37:15 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1775291835; bh=16rkqI0szlH/LFm5GEEQXbW6/vo6EV3zeZslKn4GxMw=; h=From:Date:Subject:References:In-Reply-To:To:Cc:Reply-To:From; b=HOIEi3IQDZJ3QaHo0xfDBufFrmxxYyl4st7eaxg7Fi7tVi4tuJ0BIOQEy2P25tcXp RaEjO88jKIhSMCywmyZ9ujyiL7BlR2bcXZMdJtqI0Zt1HBpPdG6uXG++FHwVm4O+KF pZ3joprrM9cIR8iQ396N/cGWCH1NC93aWNbNfx3oZKjH7zmk3iqeWahqbkOGdwPKio QJvG5D+9qu350tMSHYiyiLQWwN9JfyWbnICj2p0pNBHvTKlbjgAChw2AqUTFvNDyqI Vbvkr15+4/Qa1Mc5QNQU8wBTO9AW4EATPRjc5gO4NvBUBsBve+7ohXs5NIZlPbZQpt bkUkkbKpwMFRg== 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 C14C1E88D8F; Sat, 4 Apr 2026 08:37:15 +0000 (UTC) From: Shivam Kalra via B4 Relay Date: Sat, 04 Apr 2026 14:06:57 +0530 Subject: [PATCH v10 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: <20260404-vmalloc-shrink-v10-1-335759165dfa@zohomail.in> References: <20260404-vmalloc-shrink-v10-0-335759165dfa@zohomail.in> In-Reply-To: <20260404-vmalloc-shrink-v10-0-335759165dfa@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=1775291829; l=2828; i=shivamkalra98@zohomail.in; s=20260212; h=from:subject:message-id; bh=pPxio16sTZmRdQnFJiC4z+Co3PLVcVoIySf6x2/MXmo=; b=h1QBFXl+j95owXc6zAb1hldqce0tKV3luXhiWMIzcDI39Tj65UEqoVIriqEqgi/XZx8nVxDbW qsiV7AnmE+jBQaHWluQjOueqWKM8ZPxO37k9opPAdhZ+l6MXyhGNf19 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 Suggested-by: Danilo Krummrich --- mm/vmalloc.c | 47 +++++++++++++++++++++++++++++++++-------------- 1 file changed, 33 insertions(+), 14 deletions(-) diff --git a/mm/vmalloc.c b/mm/vmalloc.c index b31b208f6ecb..8dd7df79c5c5 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_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 @@ -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 From nobody Mon Apr 6 16:47:29 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 3A4412848A8 for ; Sat, 4 Apr 2026 08:37:16 +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=1775291836; cv=none; b=njNYKouWZYy2QxLkjmUr48c3SpzlEFkR8wpTnJNmloaLBfWO6LNSZerr9jKV7H9DnEYZJdI5UO7A7AQ0zasBUeBGf+erszR0zVdoNSd3YpajygpRSz3B4zVhZLNFHI/YmNHwO21YuLB8wJw3AByaNyiWszVqP8IhszTJ+2JqUH4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1775291836; c=relaxed/simple; bh=WVbjKtJOze0is7NfE71xoEUfsefgsKodZwF/gpCaG4I=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=V2TS5h3gyuGtG1zIZA35D/cheXERMo1n3mGd3Glr7FaxOi+6JQhSUKhkmK0JV5d8D3dSsOZhpIlE5h4YsnGIV4B1m8F8RJOZMF/0tP5FhmBGBplkLKo5ibvCFuuzD2iAsPUBvtpUEukBLeEvfwQiIQ+KMEv3jegyZZ1ksxOqtHk= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=QFH7J/Y9; 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="QFH7J/Y9" Received: by smtp.kernel.org (Postfix) with ESMTPS id E6CF7C19424; Sat, 4 Apr 2026 08:37:15 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1775291836; bh=WVbjKtJOze0is7NfE71xoEUfsefgsKodZwF/gpCaG4I=; h=From:Date:Subject:References:In-Reply-To:To:Cc:Reply-To:From; b=QFH7J/Y9tX4y0YRt8oW1fPQf7QU4bR1hYlGC7zz1EygJ0EIx51/Usrdo3lpT9yJ6a AD5u7ZjiHXYInYoTAvf6tGN09p3y5DS7S75M/lhq8KXt4Puk5voWCNUmy1E3Hx2ktf KWU1gIiP8jxmQiBIiQJzM96QcdulWZG3rFjkp3xOMB3Qj0U2pvFZxts7OtxGw1fBiT JEegmVp9C58l7P6unIAs7GG1ipQYV0DfVw3D3ZuArhb6B6rVPdrMPoJQmClbRbPlEg 7Pwk36HiJdN13FDprIwJJUFtqSx442TYK9b8uj2m9roI4I84BfoIFBssUbEe7X2O7z uKG/sraYij9fQ== 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 DE214E88D8B; Sat, 4 Apr 2026 08:37:15 +0000 (UTC) From: Shivam Kalra via B4 Relay Date: Sat, 04 Apr 2026 14:06:58 +0530 Subject: [PATCH v10 2/4] mm/vmalloc: use physical page count for vrealloc() grow-in-place check 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: <20260404-vmalloc-shrink-v10-2-335759165dfa@zohomail.in> References: <20260404-vmalloc-shrink-v10-0-335759165dfa@zohomail.in> In-Reply-To: <20260404-vmalloc-shrink-v10-0-335759165dfa@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=1775291829; l=1719; i=shivamkalra98@zohomail.in; s=20260212; h=from:subject:message-id; bh=xFhoJw4VuJmdvCHLk875WygxZhGJrQEEk01VRX1BGPc=; b=M/8QbhgSmBFZzu1NbR1WSvEBdyJKUP+HMOfMdENN8sNw+NcvDEXBOA29TwzBIdI/JzBpUJkLP W8ObSAyZTgDCf25iW+/f6ivvhvfSUcsgBLmLF4px+HO7wEBUtYiNQ4l 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 Update the grow-in-place check in vrealloc() to compare the requested size against the actual physical page count (vm->nr_pages) rather than the virtual area size (alloced_size, derived from get_vm_area_size()). Currently both values are equivalent, but the upcoming vrealloc() shrink functionality will free pages without reducing the virtual reservation size. After such a shrink, the old alloced_size-based comparison would incorrectly allow a grow-in-place operation to succeed and attempt to access freed pages. Switch to vm->nr_pages now so the check remains correct once shrink support is added. Signed-off-by: Shivam Kalra Suggested-by: Danilo Krummrich --- mm/vmalloc.c | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/mm/vmalloc.c b/mm/vmalloc.c index 8dd7df79c5c5..d0ebe16b584a 100644 --- a/mm/vmalloc.c +++ b/mm/vmalloc.c @@ -4343,6 +4343,12 @@ void *vrealloc_node_align_noprof(const void *p, size= _t size, unsigned long align if (unlikely(flags & __GFP_THISNODE) && nid !=3D NUMA_NO_NODE && nid !=3D page_to_nid(vmalloc_to_page(p))) goto need_realloc; + } else { + /* + * If p is NULL, vrealloc behaves exactly like vmalloc. + * Skip the shrink and in-place grow paths. + */ + goto need_realloc; } =20 /* @@ -4361,7 +4367,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) { /* * No need to zero memory here, as unused memory will have * already been zeroed at initial allocation time or during --=20 2.43.0 From nobody Mon Apr 6 16:47:29 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 3A55D331A41 for ; Sat, 4 Apr 2026 08:37:16 +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=1775291836; cv=none; b=PtP3vxC4UlTKBAie26IecetCiRkkhBEu8LCRRa2KtbIoew5Y+PH7TQsxXSZwwaONMFh9JvRD8lTelzWyOQvurR6O1yPzWlu7siyxWcWEW1pqJSaac4K5zaM/XpVxQY9gM9Fp74BdvZK3A8cXMv3MikZs7n1IP6oQS81Fd0KebOY= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1775291836; c=relaxed/simple; bh=XThFxJ8L8zzp8mH3ivNr+7GA22fgJiagDi51rDdmpPI=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=cdfJkuNYcIq+WhDYQTyiEx489RsfWNEmZ0aykDuJ1yHp83sB05auY/UmwL4cdD3p5SP3ufYZ0jWRMu5ue9cO0J7geB7whRWVnxOw/DiAZ/ggKYwUWLFl6tH4vnnIUbG1i/+fexTNC8FuZGYNj2bNaAaPJBc4dsDWzKrhozmg1PE= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=C/IchYX9; 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="C/IchYX9" Received: by smtp.kernel.org (Postfix) with ESMTPS id 01EBDC2BCB1; Sat, 4 Apr 2026 08:37:16 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1775291836; bh=XThFxJ8L8zzp8mH3ivNr+7GA22fgJiagDi51rDdmpPI=; h=From:Date:Subject:References:In-Reply-To:To:Cc:Reply-To:From; b=C/IchYX9uzAOieSJiu/dtX+taxIEQTU+iY/FWnYsM0OUwuMxt9IjN64NUE12Woign X/NnOtPZZxSKb5tXJE2sddQdrYFfFurZYAxhBGYiM+bpjcvC3Xkrjn1UyfM5JgtZ7j Gwtj6yjr5KcvTv/VhYEikS85EuYlt0jrKL7bJ7gl4PWhgNuYCy5+SxauFzx6emL76i 4c7e6VdN8GTrJfuzc3Q7Z8wcDSjZEWhIZtr1HuahKo25uxG0FWwwT3Yyhb9yG9psSS I4Yx/9UYAgPIrSeVPFSQNI0lctooWyrPijyqBP2G+KN5qd3gU1Qp/V9lAd0e6nwY8M NMuPPtn89aehA== 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 ED3C0E88D93; Sat, 4 Apr 2026 08:37:15 +0000 (UTC) From: Shivam Kalra via B4 Relay Date: Sat, 04 Apr 2026 14:06:59 +0530 Subject: [PATCH v10 3/4] 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: <20260404-vmalloc-shrink-v10-3-335759165dfa@zohomail.in> References: <20260404-vmalloc-shrink-v10-0-335759165dfa@zohomail.in> In-Reply-To: <20260404-vmalloc-shrink-v10-0-335759165dfa@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=1775291829; l=4129; i=shivamkalra98@zohomail.in; s=20260212; h=from:subject:message-id; bh=NTcsJV6Fe2i65TzPLdK6tq0Gp/8YPdcZEqfefS9wmas=; b=3xy+2OFbgtn27Z6rXBw8u9PZX1QmZS7QrK5pPrcibCMj2J0VPpUaEYe9UCfbek57I+UsoCH7F d93ITXGkvMiCCoLQHn44KHjP+BxBycLKQs/ZYPJyFPK8e7aVmJ5Wa4a 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. Allocations with VM_FLUSH_RESET_PERMS are also skipped, as their direct-map permissions must be reset before pages are returned to the page allocator, which is handled by vm_reset_perms() during vfree(). Additionally, allocations with VM_USERMAP are skipped because remap_vmalloc_range_partial() validates mapping requests against the unchanged vm->size; freeing tail pages would cause vmalloc_to_page() to return NULL for the unmapped range. To protect concurrent readers, the shrink path uses Node lock to synchronize before freeing the pages. Finally, we notify kmemleak of the reduced allocation size using kmemleak_free_part() to prevent the kmemleak scanner from faulting on the newly unmapped virtual addresses. The virtual address reservation (vm->size / vmap_area) is intentionally kept unchanged, preserving the address for potential future grow-in-place support. Suggested-by: Danilo Krummrich Signed-off-by: Shivam Kalra --- mm/vmalloc.c | 56 ++++++++++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 52 insertions(+), 4 deletions(-) diff --git a/mm/vmalloc.c b/mm/vmalloc.c index d0ebe16b584a..05b121845df3 100644 --- a/mm/vmalloc.c +++ b/mm/vmalloc.c @@ -4351,14 +4351,62 @@ 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. + * + * Skip huge page allocations (page_order > 0) as partial + * freeing would require splitting. + * + * Skip VM_FLUSH_RESET_PERMS, as direct-map permissions must + * be reset before pages are returned to the allocator. + * + * Skip VM_USERMAP, as remap_vmalloc_range_partial() validates + * mapping requests against the unchanged vm->size; freeing + * tail pages would cause vmalloc_to_page() to return NULL for + * the unmapped range. + * + * Skip if either GFP_NOFS or GFP_NOIO are used. + * kmemleak_free_part() internally allocates with + * GFP_KERNEL, which could trigger a recursive deadlock + * if we are under filesystem or I/O reclaim. + */ + if (new_nr_pages < vm->nr_pages && !vm_area_page_order(vm) && + !(vm->flags & (VM_FLUSH_RESET_PERMS | VM_USERMAP)) && + gfp_has_io_fs(flags)) { + unsigned long addr =3D (unsigned long)kasan_reset_tag(p); + unsigned int old_nr_pages =3D vm->nr_pages; + + /* + * Use the node lock to synchronize with concurrent + * readers (vmalloc_info_show). + */ + struct vmap_node *vn =3D addr_to_node(addr); + + spin_lock(&vn->busy.lock); + vm->nr_pages =3D new_nr_pages; + spin_unlock(&vn->busy.lock); + + /* Notify kmemleak of the reduced allocation size before unmapping. */ + kmemleak_free_part( + (void *)addr + ((unsigned long)new_nr_pages + << PAGE_SHIFT), + (unsigned long)(old_nr_pages - new_nr_pages) + << PAGE_SHIFT); + + vunmap_range(addr + ((unsigned long)new_nr_pages + << PAGE_SHIFT), + addr + ((unsigned long)old_nr_pages + << PAGE_SHIFT)); + + vm_area_free_pages(vm, new_nr_pages, old_nr_pages); + } vm->requested_size =3D size; kasan_vrealloc(p, old_size, size); return (void *)p; --=20 2.43.0 From nobody Mon Apr 6 16:47:29 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 3A4C131CA4A for ; Sat, 4 Apr 2026 08:37:16 +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=1775291836; cv=none; b=ks1JXVkNf7PGIMYnmrU3S7mTt69DxyCB9b/kqo1TEC1oDtyjljPnM4x5oNdZE7K65u9/Lin+UHzwAj6u8r5zKuRcwNc5v+GWMFRmu0nXAYW8/NV7WWuEvR4co3YvJel8N8d4gG9wdEYLVxgBFK94FMJo9pM71sH/5dVdxz67C2g= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1775291836; c=relaxed/simple; bh=NgcWajqEHrBjrpmlHzE3kqeLHOp/AqP67j8VknlwGaM=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=sq+xRWWhCcH62+I8ceILBcVt+EZ7+NO0B1t0jat6twnWXrsOwHBTBqskvtU3Rx+b2vPPJwI1NaQsGdmf6RvCWZOzfGT1VjMYT3nqkBxcKEn6Lli+YM9VPnuqF9u/qZiuaEccxcCjyjdcOJwBO1tU5Hkf0laKj6qvhRcAVWjyCac= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=uDLox2rF; 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="uDLox2rF" Received: by smtp.kernel.org (Postfix) with ESMTPS id 12164C2BCB3; Sat, 4 Apr 2026 08:37:16 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1775291836; bh=NgcWajqEHrBjrpmlHzE3kqeLHOp/AqP67j8VknlwGaM=; h=From:Date:Subject:References:In-Reply-To:To:Cc:Reply-To:From; b=uDLox2rFXJy8aPMosrDB7kLjc93lEQzAn0506hp1UJ4ucub7vv4h6uImg6x+WEX9y wifEofWp5t5K0R5tSPyU7gerW2kZd9tmGys6PlyLQa6AB1w4I2yP3koxDGtITpvrEi HWoHVYe/JomrRk/NI2vlwK6ummXC45Y3QJpe/nM9Bl3pDBXaFH8hTHIUPfzPoI71iS 0PrYwWSkOOHfKpxmXWbN4c+8vOsJX9dlzAu9aNPkSUEO3epIPAZXpU41e5KbvIrG7j K2PcdMrn51bVcA2DIQIIvxxVEtV1QTEQWW2/4zmbJh2ruwezhSTC3xk4UUnlXafUH8 L4A115L2elAAA== 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 0AE9EE88D94; Sat, 4 Apr 2026 08:37:16 +0000 (UTC) From: Shivam Kalra via B4 Relay Date: Sat, 04 Apr 2026 14:07:00 +0530 Subject: [PATCH v10 4/4] lib/test_vmalloc: add vrealloc test case 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: <20260404-vmalloc-shrink-v10-4-335759165dfa@zohomail.in> References: <20260404-vmalloc-shrink-v10-0-335759165dfa@zohomail.in> In-Reply-To: <20260404-vmalloc-shrink-v10-0-335759165dfa@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=1775291829; l=2905; i=shivamkalra98@zohomail.in; s=20260212; h=from:subject:message-id; bh=yP1QXayDidKnNU2S8I7BqJprRzQI2mQyl0fytHsaqPM=; b=dHZhZPRkkUDs6TFIAA3rjD7JuxXY1sOelhR86C12exe20D0SPMHRVlTp7p3OVi9D2nlfRjv5u +1z1AHGx4dyB6YcKii5U+wJH1nzYNJ7+E0/AP6fUqyPTJD+69ACNh/g 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 Introduce a new test case "vrealloc_test" that exercises the vrealloc() shrink and in-place grow paths: - Grow beyond allocated pages (triggers full reallocation). - Shrink crossing a page boundary (frees tail pages). - Shrink within the same page (no page freeing). - Grow within the already allocated page count (in-place). Data integrity is validated after each realloc step by checking that the first byte of the original allocation is preserved. The test is gated behind run_test_mask bit 12 (id 4096). Reviewed-by: Uladzislau Rezki (Sony) Signed-off-by: Shivam Kalra Suggested-by: Danilo Krummrich --- lib/test_vmalloc.c | 62 ++++++++++++++++++++++++++++++++++++++++++++++++++= ++++ 1 file changed, 62 insertions(+) diff --git a/lib/test_vmalloc.c b/lib/test_vmalloc.c index 876c72c18a0c..b23f85e8f8ca 100644 --- a/lib/test_vmalloc.c +++ b/lib/test_vmalloc.c @@ -55,6 +55,7 @@ __param(int, run_test_mask, 7, "\t\tid: 512, name: kvfree_rcu_2_arg_vmalloc_test\n" "\t\tid: 1024, name: vm_map_ram_test\n" "\t\tid: 2048, name: no_block_alloc_test\n" + "\t\tid: 4096, name: vrealloc_test\n" /* Add a new test case description here. */ ); =20 @@ -421,6 +422,66 @@ vm_map_ram_test(void) return nr_allocated !=3D map_nr_pages; } =20 +static int vrealloc_test(void) +{ + void *ptr, *tmp; + int i; + + for (i =3D 0; i < test_loop_count; i++) { + int err =3D -1; + + ptr =3D vrealloc(NULL, PAGE_SIZE, GFP_KERNEL); + if (!ptr) + return -1; + + *((__u8 *)ptr) =3D 'a'; + + /* Grow: beyond allocated pages, triggers full realloc. */ + tmp =3D vrealloc(ptr, 4 * PAGE_SIZE, GFP_KERNEL); + if (!tmp) + goto error; + ptr =3D tmp; + + if (*((__u8 *)ptr) !=3D 'a') + goto error; + + /* Shrink: crosses page boundary, frees tail pages. */ + tmp =3D vrealloc(ptr, PAGE_SIZE, GFP_KERNEL); + if (!tmp) + goto error; + ptr =3D tmp; + + if (*((__u8 *)ptr) !=3D 'a') + goto error; + + /* Shrink: within same page, no page freeing. */ + tmp =3D vrealloc(ptr, PAGE_SIZE / 2, GFP_KERNEL); + if (!tmp) + goto error; + ptr =3D tmp; + + if (*((__u8 *)ptr) !=3D 'a') + goto error; + + /* Grow: within allocated page, in-place, no realloc. */ + tmp =3D vrealloc(ptr, PAGE_SIZE, GFP_KERNEL); + if (!tmp) + goto error; + ptr =3D tmp; + + if (*((__u8 *)ptr) !=3D 'a') + goto error; + + err =3D 0; +error: + vfree(ptr); + if (err) + return err; + } + + return 0; +} + struct test_case_desc { const char *test_name; int (*test_func)(void); @@ -440,6 +501,7 @@ static struct test_case_desc test_case_array[] =3D { { "kvfree_rcu_2_arg_vmalloc_test", kvfree_rcu_2_arg_vmalloc_test, }, { "vm_map_ram_test", vm_map_ram_test, }, { "no_block_alloc_test", no_block_alloc_test, true }, + { "vrealloc_test", vrealloc_test, }, /* Add a new test case here. */ }; =20 --=20 2.43.0