From nobody Sat Jun 13 03:31:07 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 3D4EE1607A4 for ; Mon, 11 May 2026 08:17:40 +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=1778487460; cv=none; b=r5FIvNf+wmZ4dPLgQddNdBKOE9pv1pHd6PYvLdf5tMm7rVsNY4GkBzcjy1y8wVpwP/mGfLGGKpU+1FigNdIj4b4/oGqutQTSWBmYw3783Hf+L0WdV7HAVWg7JOqXUfpRlWQEAQzq0EsUouAEPNG+12mFSP1ovusHM28gkU0jEzI= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1778487460; c=relaxed/simple; bh=Yuu5418MZenraO1cEJhij9RE3E8satvpwxl3d6qiHac=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=HBU9Fvx/nDY/SsWDB9oOcV5WxVQkUr+WrK7l+L7AgZA78dhf6UpPVk7NcTIyu9CiIHnULof/ddsOKXvlAS4KD9FYHdx/PMK4fmw9pe+QX0GLz0/WszNUmu9tZaIHJqWuIAnJBkiTMe2HewLNWVI9LpVuttnXy9mt5LHIVgZM+ZM= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=Ph7scNI8; 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="Ph7scNI8" Received: by smtp.kernel.org (Postfix) with ESMTPS id A564EC2BCF5; Mon, 11 May 2026 08:17:39 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1778487459; bh=Yuu5418MZenraO1cEJhij9RE3E8satvpwxl3d6qiHac=; h=From:Date:Subject:References:In-Reply-To:To:Cc:Reply-To:From; b=Ph7scNI86bC4VGTy2uoY2f7ulGwX7+Z0je7yEI9robBIJAoHbKOsfsWI4nXWSE/eB //9W7la3o39/K+0ZqqTnixnTuFzLjR7Yrc57u4vmFGlcqlbT4WY+Mwddu+TlRMqw5R sTXw/Bx+Vfe+kxBrQdlCEkXw/FknwvGA5Qt3mjBCtz/iU37zdarT/GkrxMJY0cYlz/ qTRU3f3Cyep8A8JPe4ZQTgabAXGMznT1xleTJsMo6NTK8tTMkyCGlGRJRoJnyGGZB9 3i06VTqHREv0Bb6dPZtz+4SVXG5RU70BqcQ5eQF94Jx6ZXPkPuyyYqRrfle6fcGSZF jJs+PXug0BkJA== 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 8FBA4CD37AC; Mon, 11 May 2026 08:17:39 +0000 (UTC) From: Shivam Kalra via B4 Relay Date: Mon, 11 May 2026 13:47:29 +0530 Subject: [PATCH v13 1/5] 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: <20260511-vmalloc-shrink-v13-1-643b7ec277a9@zohomail.in> References: <20260511-vmalloc-shrink-v13-0-643b7ec277a9@zohomail.in> In-Reply-To: <20260511-vmalloc-shrink-v13-0-643b7ec277a9@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.15.2 X-Developer-Signature: v=1; a=ed25519-sha256; t=1778487457; l=2470; i=shivamkalra98@zohomail.in; s=20260212; h=from:subject:message-id; bh=g8lqMk/sZRbF3gcdhaoXiAZumYYOTY4cCcb8pTUlYtU=; b=yTI5v4v022BkUsxgF/ATbU7cWVQWSCdz6ULdUZZYrurBnocjrT3VhfJT8S1YJIAB1TaEvo+7W fiO4ZjiJPrSAs65M19Bw+nn3MbIyt5cV3QPt9SRaL3oj2/fIv/QgxZ2 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 page freeing 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: Uladzislau Rezki (Sony) Signed-off-by: Shivam Kalra Suggested-by: Danilo Krummrich --- mm/vmalloc.c | 34 +++++++++++++++++++++++++++------- 1 file changed, 27 insertions(+), 7 deletions(-) diff --git a/mm/vmalloc.c b/mm/vmalloc.c index 3e9e5156f43e..19a16e6f4014 100644 --- a/mm/vmalloc.c +++ b/mm/vmalloc.c @@ -3416,6 +3416,32 @@ 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; + + if (!(vm->flags & VM_MAP_PUT_PAGES)) { + for (i =3D start_idx; i < end_idx; i++) + mod_lruvec_page_state(vm->pages[i], NR_VMALLOC, -1); + } + free_pages_bulk(vm->pages + start_idx, end_idx - start_idx); + + for (i =3D start_idx; i < end_idx; i++) + vm->pages[i] =3D NULL; +} + /** * vfree - Release memory allocated by vmalloc() * @addr: Memory base address @@ -3436,7 +3462,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); @@ -3460,12 +3485,7 @@ void vfree(const void *addr) if (unlikely(vm->flags & VM_FLUSH_RESET_PERMS)) vm_reset_perms(vm); =20 - if (!(vm->flags & VM_MAP_PUT_PAGES)) { - for (i =3D 0; i < vm->nr_pages; i++) - mod_lruvec_page_state(vm->pages[i], NR_VMALLOC, -1); - } - free_pages_bulk(vm->pages, vm->nr_pages); - + vm_area_free_pages(vm, 0, vm->nr_pages); kvfree(vm->pages); kfree(vm); } --=20 2.43.0 From nobody Sat Jun 13 03:31:07 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 3E2A63033E9 for ; Mon, 11 May 2026 08:17:40 +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=1778487460; cv=none; b=aaKYeXC0109mF7vK7Kxyso/88JJuPz6ylhROPYfUOnWMUlE4i5GP2Kl7jOmSxq3x+wy5I2Mx6pbxKrX2cSbCspI0HJ5Drvg6fCIrmdJKSfLVn/Y4Fm94Bedcvh0YkNEzn1XvFhGnGnMZQeZX787r7h6eXCYFjiObF6lxtBFGIPM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1778487460; c=relaxed/simple; bh=xUL2bCuAEjBLi47uhpNn9uPzuF0H6sC9FRHMffmSW3w=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=Zigqp53I+kGCiuZ+Tn8st5vQZsA02cPutcVjoSEryyEE0NUsTp5+gRs6bis2kge3cbOYTA9kHiH06oSdkWFjinpp/Km5I8am0iC48LB8wUtji5skJs32syy4tWbqnzTKlXQnD4s1TatnWsX82WNMARtrmhTmgYMBQIv5sCyFPcM= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=Svqvc9K/; 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="Svqvc9K/" Received: by smtp.kernel.org (Postfix) with ESMTPS id AEF81C2BCFB; Mon, 11 May 2026 08:17:39 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1778487459; bh=xUL2bCuAEjBLi47uhpNn9uPzuF0H6sC9FRHMffmSW3w=; h=From:Date:Subject:References:In-Reply-To:To:Cc:Reply-To:From; b=Svqvc9K/dl9Z59pPFhtjJAeWv1BR2dP4BLuy18g+HZo8t4ptRnShYy51NXx6RYgEj WMLCb14SXAjsuzM53yBQp1UHU5kCE4scpHcTZ8SbsQx9BxYD54wzjN4lrU9/qcvwxb TeldGvmBuJz+ypLly2Z9EaLb3n0Bkuwf0nS7so4aeJgdPlONOR+P9q/ol4Wwm6sQKN NDtATn2MLUMtVcz8fZa6juHv+Llp9358sYeLE38S9wIKJr/8jHOXTRW9+OzERyOv1X Y4kKaF7NbxupanvhxxK0TQrTDWT5Dy/VyYRKj3eU6Bb8lKM7QdvgC6gmdnY6yexYvw QJjO8m4/t00yg== 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 A4E6CCD37B5; Mon, 11 May 2026 08:17:39 +0000 (UTC) From: Shivam Kalra via B4 Relay Date: Mon, 11 May 2026 13:47:30 +0530 Subject: [PATCH v13 2/5] 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: <20260511-vmalloc-shrink-v13-2-643b7ec277a9@zohomail.in> References: <20260511-vmalloc-shrink-v13-0-643b7ec277a9@zohomail.in> In-Reply-To: <20260511-vmalloc-shrink-v13-0-643b7ec277a9@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.15.2 X-Developer-Signature: v=1; a=ed25519-sha256; t=1778487457; l=1768; i=shivamkalra98@zohomail.in; s=20260212; h=from:subject:message-id; bh=pF2aHwJ46DNqTh9Jbrwu8jcsAr22gjU+XXXuqkw2DrY=; b=/1HUHN2N0cHnc5KsBXIpLCF/Nv2737vresqIEFbzYyMmpLDgFkQuwP5OGqt0ILOzvkenwBOam CVxhPglvCogDrj+ukCvHe4S7tdA12+gSmaIWXue6LuZoPsBRu4wJLVM 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. Reviewed-by: Uladzislau Rezki (Sony) 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 19a16e6f4014..87278cf2046e 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 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 Sat Jun 13 03:31:07 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 3DB163AF667 for ; Mon, 11 May 2026 08:17:40 +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=1778487460; cv=none; b=g4QhQPvJ0f6o9YdJDE+jgbU9GXaPLnxVuWGIBpa8fKSwtNdbZoLMxfEQ1j01R4NiCQ4btGPftuRHzLCKQ1mmZEM7yVwxUGy9RjWGafUDoP8lngpWdvuQLu/71qk4kUDxcZNLNACKZKlLDCLwTD8/CucOnnSJNskqolOos+x4i+U= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1778487460; c=relaxed/simple; bh=+Jo7ZBrr4/gMgS1hfJFlO2RVkK5ROniR+PDpelGSBTY=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=r3YOH5zyDGWuBlkU1r3irzTVoxmQfMbHiI+jnJE6L1fGGiulF+xgWlFR1a05/Edx5DIScazi0XddBTV2GRSvm/OB/4igxsAJczMgBaFpMw8KUwkxrueoGhRlTOfoX78fbtNnqsbsqlcgsH/zi/m5OGAa+mg7CQUmGNdAkNdefmY= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=TNVFxvoV; 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="TNVFxvoV" Received: by smtp.kernel.org (Postfix) with ESMTPS id C372FC2BCFD; Mon, 11 May 2026 08:17:39 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1778487459; bh=+Jo7ZBrr4/gMgS1hfJFlO2RVkK5ROniR+PDpelGSBTY=; h=From:Date:Subject:References:In-Reply-To:To:Cc:Reply-To:From; b=TNVFxvoVsDGagYqVPlBdtgcf+b4nTZk/C3KQnJWMQR6XfwdJxVm7pR6qCSccZr3s2 WEwrhmdpwX6HjbFmtNdLN9hCizPNkmgdf6TY5cVTtBechxPgZUXMKmWawade20f2HB 1q2BM5QHBpgHVchrSC8bi+yDLmedwsAxnXmaLtRLL+QcD0ZAR/SgQYBv+eX2RaYSCe YwHm5HDxNp7IoAW8pT5SetNeWdGu28wt9ogo3UIWfH9Wz3XQa8ppiVMo78r4HtHcTw BLl86r6ILDGxzFZuGPNxDVHnQ9OOneBzEy26Vpndx+3nWbp1PMKdv++z1AmV/ZiX2D 8qlrCEG+83whg== 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 BD5D9CD37AC; Mon, 11 May 2026 08:17:39 +0000 (UTC) From: Shivam Kalra via B4 Relay Date: Mon, 11 May 2026 13:47:31 +0530 Subject: [PATCH v13 3/5] mm/vmalloc: use physical page count in vread_iter() 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: <20260511-vmalloc-shrink-v13-3-643b7ec277a9@zohomail.in> References: <20260511-vmalloc-shrink-v13-0-643b7ec277a9@zohomail.in> In-Reply-To: <20260511-vmalloc-shrink-v13-0-643b7ec277a9@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.15.2 X-Developer-Signature: v=1; a=ed25519-sha256; t=1778487457; l=1325; i=shivamkalra98@zohomail.in; s=20260212; h=from:subject:message-id; bh=0/E2gQvGxEo3eGw/8wt9W0DQApE+QcKjBLa75ti9yYs=; b=KDZ8dim5ESybiJyOGbwcOhc1DmY5tSwZqA0TstiryUq+cc1v4bqjKAmKi/l3UjbFw+8qZij1e rmWpniLO2UoBwyy/+uajlyh+a2vwajeXbgiu6q3QKpj22mSFi8QVYFi 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 vread_iter() to derive the vm area size from vm->nr_pages rather than 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 get_vm_area_size() based calculation would overestimate the mapped range, causing vread_iter() to attempt reading from unmapped addresses. Switch to vm->nr_pages now so the reader remains correct once shrink support is added. Reviewed-by: Uladzislau Rezki (Sony) Signed-off-by: Shivam Kalra Suggested-by: Danilo Krummrich --- mm/vmalloc.c | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/mm/vmalloc.c b/mm/vmalloc.c index 87278cf2046e..d69174afb171 100644 --- a/mm/vmalloc.c +++ b/mm/vmalloc.c @@ -4666,7 +4666,14 @@ long vread_iter(struct iov_iter *iter, const char *a= ddr, size_t count) smp_rmb(); =20 vaddr =3D (char *) va->va_start; - size =3D vm ? get_vm_area_size(vm) : va_size(va); + if (vm) + /* + * Cannot use get_vm_area_size() because realloc() + * may shrink the mapping and area->size may be outdated. + */ + size =3D vm->nr_pages << PAGE_SHIFT; + else + size =3D va_size(va); =20 if (addr >=3D vaddr + size) goto next_va; --=20 2.43.0 From nobody Sat Jun 13 03:31:07 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 3D46A1CD2C for ; Mon, 11 May 2026 08:17:40 +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=1778487460; cv=none; b=UK7yS5eQuOH9wknkAoXyeX3lcKGuwf1p4EUa35HsuyDdJFN9vcDpSf8Fk+oWavgzERHtBI4VMopz1dCHX3o/EMmpecE2H4ib0tFb/WEFAEjLtd0TmZ8KDhJ3a0uT1JfP6cgR5NrgOz/+YDWGenVLENBheVs+3GQmDNIzu/LbPSQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1778487460; c=relaxed/simple; bh=Jqo4TED9HuG8bs7dY11fJ4mAkfZHK71BmYBSb5ttQFU=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=q4PFeo9TrC3qsiIGmP+BsD6p/LO/jf5f7oqKlZYtemR5RzCi/vZF4ZeeCfoLkwQQ7gS7Ia96LAPLUdHcwTSAmtJHc1A6slyQvNNLamcLxDfN2DHxRYNB+BabkBiBd/sjeJs7JzWcgUFKIjmHTI+B/6sTJqQTj7JxEa3+449rLnA= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=Ln4eiZ25; 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="Ln4eiZ25" Received: by smtp.kernel.org (Postfix) with ESMTPS id E81EFC2BCFA; Mon, 11 May 2026 08:17:39 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1778487459; bh=Jqo4TED9HuG8bs7dY11fJ4mAkfZHK71BmYBSb5ttQFU=; h=From:Date:Subject:References:In-Reply-To:To:Cc:Reply-To:From; b=Ln4eiZ255dCAigpndUiO6CBmguaCBYc0uQdSF131tnTVsWp7NzhtCJvNBpGZZ5Ueb cUhu5tgKuAHtZ4+p3rNYlsYZAuEit5JlGl2tbR/OxUHEOUF49zbN4PZp2baft1G4+J DfqKZDmNhb0+mLdTnlj81sVio+cxD8T/PAtb0acxwnsqC9+yPsJXdJ7vbkvXPU51yE QA/FPwLzGA4EDMtIM7XoGxJVaVdleaDdWKufxONZK1KJcDLBvw18aWy5Q2cPnh/aHK ObFALLtoqj/b2pIQKHkHIZ+aaVi1r6wMNg1aBlZAJM5blWQ806YNHcrkfJVzvpT3y+ 4nfLdKZldyoAw== 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 E1F5FCD3427; Mon, 11 May 2026 08:17:39 +0000 (UTC) From: Shivam Kalra via B4 Relay Date: Mon, 11 May 2026 13:47:32 +0530 Subject: [PATCH v13 4/5] 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: <20260511-vmalloc-shrink-v13-4-643b7ec277a9@zohomail.in> References: <20260511-vmalloc-shrink-v13-0-643b7ec277a9@zohomail.in> In-Reply-To: <20260511-vmalloc-shrink-v13-0-643b7ec277a9@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.15.2 X-Developer-Signature: v=1; a=ed25519-sha256; t=1778487457; l=4186; i=shivamkalra98@zohomail.in; s=20260212; h=from:subject:message-id; bh=kfU4/ZakIo7xbcyPX++3+x8brIr6bumOe9FreCdlhcw=; b=TTJiWOsE2EtDAJyIAtXtdpX8KAZsIwiXDx+fVJTX1dZyfgXV3Cx3PWQo3KytzqtmqGVvL1UIK cnVIekZEibgBnJuuefw0JzJfQwZYSJt8hVkWK71B72PPVd1ZtVo9tE3 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 Reviewed-by: Uladzislau Rezki (Sony) 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 d69174afb171..4baa951c901f 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 Sat Jun 13 03:31:07 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 4441C3BA237 for ; Mon, 11 May 2026 08:17:42 +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=1778487462; cv=none; b=RXzC4oQdEfMRjEBYLBpNlChwPHQPJtfQzCZeBIJevg4MhkKXzCObRsOdgSxlQq8A3bwU+TUFjBhSg+jubY8sR3FmifFF2X5Xr56Rm3r/1JiH7Dw0wKAT1XE0lF3XFRnCNLfS4BmpKF8RcK5YOuPzFXQTmP5SY1Kyo1LTRJhWwTg= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1778487462; c=relaxed/simple; bh=NgcWajqEHrBjrpmlHzE3kqeLHOp/AqP67j8VknlwGaM=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=gjgi8qdkEzsmLZkGeJiAJM9+EfV8CNAR9JyOOl9A/V/d2p4YlT+KQqyaAn61yOTU/FRi1kW2KXxdGIfGBuTYeNO4cxTccD/LRPJi/8ph2aDN0rhGel+q0l9AJLHj58CDlbgNltybafgTM9Uczvo9Pe44MR3law4KyK0sNpslwIs= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=lklb5dBo; 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="lklb5dBo" Received: by smtp.kernel.org (Postfix) with ESMTPS id 05DE8C2BCFF; Mon, 11 May 2026 08:17:40 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1778487460; bh=NgcWajqEHrBjrpmlHzE3kqeLHOp/AqP67j8VknlwGaM=; h=From:Date:Subject:References:In-Reply-To:To:Cc:Reply-To:From; b=lklb5dBoxVRCImOirb44qTKbSoeHc6BP4hxrDKlb3sYWqCGmeZcqa34gKZaj0Ewd1 DuIkCs82md5UslRClm3NFlVvCmMM4lpMLhKtOScmLvlfW/R6DTo7SRKEVoT2Nlxfvg J+HJk4YzaF8OmgMTkBJe+RLhcrP/yXg1S/Hvy03lKfQLJ9lL0BGA5VupgkiksmgPWz pOpnzRCQOwud28UXJqYg/cBKyIm81C2filUOE+1VbMSJC/1PTUNODNE0/4r4kdW+9K IGQItFuswyeF9kvoRO1Qo+dtKW9L9QzMk2cQLiGhH7ZMqymDDB5MLBYsl9FK1djZYg bAPpyaMEfb5og== 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 F216FCD37B7; Mon, 11 May 2026 08:17:39 +0000 (UTC) From: Shivam Kalra via B4 Relay Date: Mon, 11 May 2026 13:47:33 +0530 Subject: [PATCH v13 5/5] 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: <20260511-vmalloc-shrink-v13-5-643b7ec277a9@zohomail.in> References: <20260511-vmalloc-shrink-v13-0-643b7ec277a9@zohomail.in> In-Reply-To: <20260511-vmalloc-shrink-v13-0-643b7ec277a9@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.15.2 X-Developer-Signature: v=1; a=ed25519-sha256; t=1778487457; l=2905; i=shivamkalra98@zohomail.in; s=20260212; h=from:subject:message-id; bh=yP1QXayDidKnNU2S8I7BqJprRzQI2mQyl0fytHsaqPM=; b=4sftg0lX1QRYVC4uS1Mfv3d3yWqs1LKXS4cjJshvIUVh1O9KSllGK1tniouv34PgmJwRDEb8s OYiPT367YL+BL639bOvAx6C1LgV+B0ayCRMFwLVnSSZXuPwNbPbhllO 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