From nobody Thu Apr 2 17:14: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 781883D9045 for ; Fri, 27 Mar 2026 09:48: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=1774604920; cv=none; b=DkmclisU2zo0J5593cJ4K+uBrHqfYfQrrWv7fnyvfs73ryoW12kQNUeWcpgAH1fbbEwf2a6GcmCS2MPK4ucReTY47eE/fdYlJrlZahqoghoH2e2LZVCSb+I66HhDX1kWN/Q3MwgCU5ZLSLL/7f2qwYcVZFYBL/eTuEjL4lZkNmE= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1774604920; c=relaxed/simple; bh=6VrX9bhFglZF/oJnl60voFlMr8m+M0s8BuRej35uxeQ=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=ghvgRDIrXkD4jes4VSI9nXTs4cdXXir1w+XELbw3FTQuOLRe35FDcBDe2MSltJFkIuBpFS7r920kfYKX6MzPkDBdIuHt2je6VUBS++bQ0xaqvUdJnH8QwGgiaiq18KvDsRCGlIAf8G0TYazflrPk0nygNxoXuImyi8Uc+OhjFJQ= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=UAW6YGfI; 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="UAW6YGfI" Received: by smtp.kernel.org (Postfix) with ESMTPS id 2C222C19424; Fri, 27 Mar 2026 09:48:40 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1774604920; bh=6VrX9bhFglZF/oJnl60voFlMr8m+M0s8BuRej35uxeQ=; h=From:Date:Subject:References:In-Reply-To:To:Cc:Reply-To:From; b=UAW6YGfIxnl3BAWfqOXI9gfOhdjgRj268DY3QAqYPuEx0MTpj01eMR+rkhWfYjPpO PDmOEtf8Hb4j6tI7Cwm+oq1GUEwUdcq+mVcCxn9Ve3FmXEZAA4p+j1ZHBIh6pViz63 JeMU+CHQy9+3BxXxQ7TZ4BwDYhf3wL4ffUeEMzPEBodjPk0iGlouiVqx4Rc70ONAFW 8RUL2Y50MReCM3Lh+TAqKgrVku50yCo6FGqdqae89bFqLf9ah+j9hacbePLPNop4qF IKu+QAqVZ13UazhHMxFmFGK3Q9Zd7PYc01wsLQgHAsYDNSEX0FlVrf3wMHuPR1mZOJ TDAha71cHZAKw== 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 1963910ED643; Fri, 27 Mar 2026 09:48:40 +0000 (UTC) From: Shivam Kalra via B4 Relay Date: Fri, 27 Mar 2026 15:18:37 +0530 Subject: [PATCH v8 1/6] 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: <20260327-vmalloc-shrink-v8-1-cc6b57059ed7@zohomail.in> References: <20260327-vmalloc-shrink-v8-0-cc6b57059ed7@zohomail.in> In-Reply-To: <20260327-vmalloc-shrink-v8-0-cc6b57059ed7@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=1774604917; l=2771; i=shivamkalra98@zohomail.in; s=20260212; h=from:subject:message-id; bh=4HOXJZyeFmBfI0AlNIxmjeDSGExIrgz5ceojgaPu1Zc=; b=3FTEEhaMk3fS0LVGGP93t/XcThQSZTfaK7fq45GzsrmP8QCFomBgVOwrXiCTLoVrHKeIDYRzD RMSM72jY5KjABvTbTF8K/WI3YztHJGTiwexkulsVWfOJ5+sqJY8QR0l 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 Signed-off-by: Shivam Kalra Reviewed-by: Uladzislau Rezki (Sony) 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 d75151649c97..79a57955345d 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 Thu Apr 2 17:14: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 7824A3D9059 for ; Fri, 27 Mar 2026 09:48: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=1774604920; cv=none; b=q1eqPNC6gs35QcJORC1nnFWRPc7k1JxEK6DxNbKFQSA45SIsHPwL4TcU8Ox6aOsD1kv/2QpyQWa9raSeCCO5t4YjksRQavBfPy3ZOdN9UHTpuRhvBgvh8qH2ou3kicfMw1NPrhrzjqi5OslVuS7gQIlQdgIUK8VwQy0n7jzQ6c8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1774604920; c=relaxed/simple; bh=uC3ozkw98B2T0ybqh69BkU+YKKQ53sm/70mGSobtbU4=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=QanruJ2MwgVMLsFFL8QqyOJyBO5BEjJRrS7f4ZFhTfJY/cMlJpMiwWSBsWI3TPo3mVDsUzXdV8ZJgu7bTRPRmzo7gyQ3AFeC+kgldI6CD7/k/Ncw1p/TUGzhm1sM0ODqzv0AX3gnNJgjGbNfzsm9TNq5o/MHx1WCHT1nZxMnrNY= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=XIvuzlww; 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="XIvuzlww" Received: by smtp.kernel.org (Postfix) with ESMTPS id 3AE39C2BCB0; Fri, 27 Mar 2026 09:48:40 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1774604920; bh=uC3ozkw98B2T0ybqh69BkU+YKKQ53sm/70mGSobtbU4=; h=From:Date:Subject:References:In-Reply-To:To:Cc:Reply-To:From; b=XIvuzlww9+w0d3N+thAzXfc9znDZ7WUg7i5xCLoyWEtLfOfydFHUiKoCbuqIGScuU cjxd7jNV+G6KVXi/T9h7XCGn9Cy4WaUT+hdsXQbHIGOIPbZCZZlnu2pzbickCKUQAJ 0+2zCHStXV8hRljkfStdYvK30SLHZXwhiU78qpjocEo5h2PuEawgoPah6iO5NKCEHX /A1YCb0U3SaVBb7EXT5asMWczhzBE7fA4s+muzosnAxa9fXJsCD9pE4TIL6hSVnUFo e0vgySZfaS/bhqN/otzR3YIpRlsnQxGQz2ZMd5x4nrz+qu9OfRBZib0GRWGXRztSvz ElshThRBCn+9w== 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 3068710ED646; Fri, 27 Mar 2026 09:48:40 +0000 (UTC) From: Shivam Kalra via B4 Relay Date: Fri, 27 Mar 2026 15:18:38 +0530 Subject: [PATCH v8 2/6] mm/vmalloc: fix 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: <20260327-vmalloc-shrink-v8-2-cc6b57059ed7@zohomail.in> References: <20260327-vmalloc-shrink-v8-0-cc6b57059ed7@zohomail.in> In-Reply-To: <20260327-vmalloc-shrink-v8-0-cc6b57059ed7@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=1774604917; l=1675; i=shivamkalra98@zohomail.in; s=20260212; h=from:subject:message-id; bh=JBxDN0g82pC84smB7v9qq2A4Kq3XUFnm46m5uC3JQmo=; b=HSIEpP+gWS/CI0lJnxlmSaw0gfljH77Mtt5owCSfUm1SMlxuTzAAVF4GvU7JLLlRDvUW0TnLR 4/ZSGhlJUfKDNiY1tyUWDUeY34dA/y/qK0fs599OWmzNHGaO5xsmhJS 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 Fix 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()). The virtual reservation size (get_vm_area_size()) does not decrease when pages are freed during a shrink operation. Consequently, without this fix, a subsequent grow-in-place operation after a shrink would incorrectly succeed and attempt to access freed pages. Correcting this check is a prerequisite for the upcoming vrealloc() shrink functionality. 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 79a57955345d..133c3b0418fe 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 Thu Apr 2 17:14: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 8E40F3DA5AE for ; Fri, 27 Mar 2026 09:48: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=1774604920; cv=none; b=TuN+0cf4RjKP7zz+lAL6xP0VPspPE+/ktGSTMShIKfVdcC8fTqebIrSunmC1y7wuWVXjKuvprkpe8VgB7b8NWREUE712A9AhIxmUkByYpRW4oaql0UIFhHrWpCWmyGUpitEwmF90yaER/GnZ78kkj6+LhuQmFiAq0lSL4MMJJtg= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1774604920; c=relaxed/simple; bh=HUOngOFoyGHQgaZ85Xk/jo93b+mIJRl7GJuKRuvWg48=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=jbYJrJtM86b2ILr1wUZcAGKtQGik3aACkBSYNxyjihuK4NWHAmoJlLN0tJXXxJtMk+n4W2hdH4ToZddpC0TOSNlP9RZkVrdkhvetdWlEmEwhPqmYGSLk6eb4MbTAyM3d3+ZhOZzhUE92tBkMwNj4SBHHMeD2YjZ5Fd9mn37cVWk= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=KuZ/XKC9; 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="KuZ/XKC9" Received: by smtp.kernel.org (Postfix) with ESMTPS id 538A5C2BCB3; Fri, 27 Mar 2026 09:48:40 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1774604920; bh=HUOngOFoyGHQgaZ85Xk/jo93b+mIJRl7GJuKRuvWg48=; h=From:Date:Subject:References:In-Reply-To:To:Cc:Reply-To:From; b=KuZ/XKC9Nae+tDa7pw4bitE/mfl5R+qU585u315/GoxdAS1eBAhe/pyLwISgIbp/x 712A6r1i0XL5BLc4b/zm9HUGSGVn4cc0WOhZmzsa02y/HTkgXVBKuRYaGppcoguzSr VTDYnIJbm0nSC6rcQDqyldT+NgjCapR80C/E2YNdDkBkEOUReadsQ2pf0pw/I3GVFR 3E8FbqRiDJ5KSssR3ZLU+s/+lqgDFJFghEbu3O/FVpwq2xfgOZ4UhtUWDOKto5M0ii 8cG5XhjVXSh0yzdHG1hPZFCZt//nHhlie0P+T53Uqp+UqcOk+/Dp0X3H+24a748I+E rbBAfJGLKppBw== 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 474F010ED643; Fri, 27 Mar 2026 09:48:40 +0000 (UTC) From: Shivam Kalra via B4 Relay Date: Fri, 27 Mar 2026 15:18:39 +0530 Subject: [PATCH v8 3/6] mm/vmalloc: zero newly exposed memory on vrealloc() grow 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: <20260327-vmalloc-shrink-v8-3-cc6b57059ed7@zohomail.in> References: <20260327-vmalloc-shrink-v8-0-cc6b57059ed7@zohomail.in> In-Reply-To: <20260327-vmalloc-shrink-v8-0-cc6b57059ed7@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=1774604917; l=1805; i=shivamkalra98@zohomail.in; s=20260212; h=from:subject:message-id; bh=LSUYWjYOjJYKYliRO3bYLhKxcMuMIBGaVN2tymO6+ew=; b=rhThTDLnB57E/DBczd8HBXwMc5oEaaeJggo8YDeT6Y5MrRyp+O/EQh7sxM1hZvm1pvxDVce7R Zy3ihFALKZ+BT2UKkAd+06tqJ3RxfEKcdMJTVjTZPZTwIrL3lutscSU 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 growing an existing vmalloc allocation in-place, zero the newly exposed memory region [old_size, size) if the caller requested it via __GFP_ZERO (checked via want_init_on_alloc(flags)). Previously, the code assumed that the unused capacity in the vm_struct was already zeroed either at initial allocation time or during a prior shrink. However, if an intermediate shrink operation occurred without __GFP_ZERO and without init_on_free enabled, the "freed" portion of the allocation would retain its old data. If a subsequent grow-in-place operation then explicitly requests __GFP_ZERO, failing to zero the memory here would violate the allocation flags and leak the previously discarded, potentially sensitive data. Signed-off-by: Shivam Kalra Suggested-by: Danilo Krummrich --- mm/vmalloc.c | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/mm/vmalloc.c b/mm/vmalloc.c index 133c3b0418fe..ddb689bf9ba5 100644 --- a/mm/vmalloc.c +++ b/mm/vmalloc.c @@ -4368,13 +4368,16 @@ void *vrealloc_node_align_noprof(const void *p, siz= e_t size, unsigned long align * We already have the bytes available in the allocation; use them. */ 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 - * realloc shrink time. - */ vm->requested_size =3D size; kasan_vrealloc(p, old_size, size); + + /* + * Zero the newly exposed bytes if requested. + * The region [old_size, size) may contain stale data from + * a previous shrink that did not use __GFP_ZERO. + */ + if (want_init_on_alloc(flags)) + memset((void *)p + old_size, 0, size - old_size); return (void *)p; } =20 --=20 2.43.0 From nobody Thu Apr 2 17:14: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 9085B3D75A0 for ; Fri, 27 Mar 2026 09:48: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=1774604920; cv=none; b=J7lZNJZBEYbTPFF5D8JppoWkp/nA5GDC4u3/PeZObhTQM7DSf3VOlUvOnWGaJnUUi/OQgXxGqIVkoAB9Ggh6NbHDQWnD+ksLY1HeBlriPbPY1Z9BylydgiF00rW0JgkNaHJjneM8Rnc2LL7L/QcTfYuJRC+Y1g9XW+WeMzQ9Ruk= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1774604920; c=relaxed/simple; bh=lvRKpv9TIZt0GjqdnA46kctN4zrgkYUFc8NhDjAPR0g=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=eT9z2O7b/8BKFj5htiY1A1VW44N732Y7XLtXDP9kSdqFa/LQGLLKnkHWpNOs3z1uYBZmoLKaYm+wEUPZ8G27wZTiMLbSNCLbvJtq58doWmut+9e/6dFo1xb+TYXGRnQmJlkX99fR+Lq6rTWnQm12u7WfjEjEjjl3SDIxQ/87RM0= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=M3KcyTb4; 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="M3KcyTb4" Received: by smtp.kernel.org (Postfix) with ESMTPS id 66A9BC2BCB2; Fri, 27 Mar 2026 09:48:40 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1774604920; bh=lvRKpv9TIZt0GjqdnA46kctN4zrgkYUFc8NhDjAPR0g=; h=From:Date:Subject:References:In-Reply-To:To:Cc:Reply-To:From; b=M3KcyTb47w0vFP6hbGYmnuAm0Wtz87yqb7HojB2qNLft7UErvvx7wgkC6P4B8LuSj b//75QRXdqXDAt3zlwmUpRZM2OXSKlEAu69j1Dz9JqHHyAXcujV0Pwz/m7ZnSc40Li 6/Q7QGaRjnZ+ujy8hxaYr1gQLx3UZE4z9LOsulj7q2y95sjrhdZF9QFBEpoGrcDkxR Sek03pTvc+vdsEvplCO44tG8Id6KdMI1aLmyaK+FStzGck6ymAw5duZ4C07tOY5CrR u2U8PaO9pi728wBnFw2JtOTz5JH8KOFvn4Ad6VxCUnOfFflGvNQ5I3oyzoFoQQMeL5 2glAc05fPmeQg== 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 5CE8E10ED645; Fri, 27 Mar 2026 09:48:40 +0000 (UTC) From: Shivam Kalra via B4 Relay Date: Fri, 27 Mar 2026 15:18:40 +0530 Subject: [PATCH v8 4/6] mm/vmalloc: use READ_ONCE() for vmalloc nr_pages status readers 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: <20260327-vmalloc-shrink-v8-4-cc6b57059ed7@zohomail.in> References: <20260327-vmalloc-shrink-v8-0-cc6b57059ed7@zohomail.in> In-Reply-To: <20260327-vmalloc-shrink-v8-0-cc6b57059ed7@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=1774604917; l=2959; i=shivamkalra98@zohomail.in; s=20260212; h=from:subject:message-id; bh=q4Ito1vUmJmb3A3de47pGFK3WSX+j54Hs6oFM89TDPE=; b=WAfAOXMaxhCxjvt2ewSFJ24eqk8Y14S2Fzvr0Bi6nQKMPvQrC4YQDdEU9hGatJ8o3viLlAzov V4kYd9euOsHDeADLt//193DDbkOHhKjTrrbTTQyhPFP8B9THCu6sr/a 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 The vmalloc status readers (vmalloc_info_show(), show_numa_info(), and vmalloc_dump_obj()) currently read v->nr_pages and the v->pages array without any concurrent protection. In preparation for vrealloc() shrink support, where v->nr_pages can be decreased and entries in the v->pages array can be nulled out concurrently, these readers must be protected to prevent use-after-free or NULL pointer dereferences. Update these functions to use READ_ONCE() when accessing v->nr_pages and v->pages[nr]. This ensures the compiler does not re-fetch these values and provides a consistent view of the vmap area's state. Additionally, in show_numa_info(), explicitly check for a NULL page pointer before dereferencing it to avoid potential crashes if a page was concurrently removed during a shrink operation. Signed-off-by: Shivam Kalra Suggested-by: Danilo Krummrich --- mm/vmalloc.c | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/mm/vmalloc.c b/mm/vmalloc.c index ddb689bf9ba5..c6bdddee6266 100644 --- a/mm/vmalloc.c +++ b/mm/vmalloc.c @@ -5189,7 +5189,7 @@ bool vmalloc_dump_obj(void *object) vm =3D va->vm; addr =3D (unsigned long) vm->addr; caller =3D vm->caller; - nr_pages =3D vm->nr_pages; + nr_pages =3D READ_ONCE(vm->nr_pages); spin_unlock(&vn->busy.lock); =20 pr_cont(" %u-page vmalloc region starting at %#lx allocated at %pS\n", @@ -5210,7 +5210,7 @@ bool vmalloc_dump_obj(void *object) static void show_numa_info(struct seq_file *m, struct vm_struct *v, unsigned int *counters) { - unsigned int nr; + unsigned int nr, nr_pages; unsigned int step =3D 1U << vm_area_page_order(v); =20 if (!counters) @@ -5218,8 +5218,13 @@ static void show_numa_info(struct seq_file *m, struc= t vm_struct *v, =20 memset(counters, 0, nr_node_ids * sizeof(unsigned int)); =20 - for (nr =3D 0; nr < v->nr_pages; nr +=3D step) - counters[page_to_nid(v->pages[nr])] +=3D step; + nr_pages =3D READ_ONCE(v->nr_pages); + for (nr =3D 0; nr < nr_pages; nr +=3D step) { + struct page *page =3D READ_ONCE(v->pages[nr]); + + if (page) + counters[page_to_nid(page)] +=3D step; + } for_each_node_state(nr, N_HIGH_MEMORY) if (counters[nr]) seq_printf(m, " N%u=3D%u", nr, counters[nr]); @@ -5247,6 +5252,7 @@ static int vmalloc_info_show(struct seq_file *m, void= *p) struct vmap_area *va; struct vm_struct *v; unsigned int *counters; + unsigned int nr_pages; =20 if (IS_ENABLED(CONFIG_NUMA)) counters =3D kmalloc_array(nr_node_ids, sizeof(unsigned int), GFP_KERNEL= ); @@ -5276,8 +5282,9 @@ static int vmalloc_info_show(struct seq_file *m, void= *p) if (v->caller) seq_printf(m, " %pS", v->caller); =20 - if (v->nr_pages) - seq_printf(m, " pages=3D%d", v->nr_pages); + nr_pages =3D READ_ONCE(v->nr_pages); + if (nr_pages) + seq_printf(m, " pages=3D%d", nr_pages); =20 if (v->phys_addr) seq_printf(m, " phys=3D%pa", &v->phys_addr); --=20 2.43.0 From nobody Thu Apr 2 17:14: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 A7AC83DA7E9 for ; Fri, 27 Mar 2026 09:48: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=1774604920; cv=none; b=flttGp4yFdsXolyv9EQGfIvxRPc+JkTBTYz4JbAEaLEHDa/IKzB7pojiM0VVbRuHE6AnYE/QKP+/2ASWhkDKzrFELdBJs9GzNyKfvi06VbZvWPAcx30z2/3Un+0nAuEFKTlaznv3pzpjMMPF/cftp4W38Us6/kQUILq2sfIwbjk= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1774604920; c=relaxed/simple; bh=VAn5qbLsvUENIW/va6GuMZmrw3yUMFjIWRDfQx0oC18=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=AlxRXhFVKc6zFY312y3Vq8hj1X42at+12u12At/KLyQ6OThL35EFmovyVyvw37iBQqu2gYHfgxx4gjC5Ey+pwgBQdaByfRRr0MsAugRlPupil2Xg97sfIGhY+/A0tRWGNSea6Yst4oV0VaxH0HsAKEbkmw8QjNJvCHuQ+mXmNA8= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=rtEZrHO2; 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="rtEZrHO2" Received: by smtp.kernel.org (Postfix) with ESMTPS id 7CB43C2BCB6; Fri, 27 Mar 2026 09:48:40 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1774604920; bh=VAn5qbLsvUENIW/va6GuMZmrw3yUMFjIWRDfQx0oC18=; h=From:Date:Subject:References:In-Reply-To:To:Cc:Reply-To:From; b=rtEZrHO2SlFLOfw3fqO0+ywOHHW6vpglwQibetS+TFxd8N/FqQ2IOEmduenSBVq/I 7Q/TPqLOIFcpzLJI9QZ9VOakktLS6HT6CKgwAONPzB8fDyMz4+Y9ds/Jpy3E4lngk0 UyzClMeGYlAlfx44amUv91slA+w+rHumhv6WWynLn1+LM47ZycWs54wC215AwLqNQ0 vIYzT2gnVOkG497UQ3RkV1lLoA22cSmB7+QByGkh0qBD/884BCl8e+/9Ipqg6IWatI awofZ7mPS3GA1xOfSOr1LPc7BmdvCG3y6qsP0z8KXtHLRnWxcRTg1Dh9epNd8SXmKB uKR6qK1x2y5ag== 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 7155E10ED647; Fri, 27 Mar 2026 09:48:40 +0000 (UTC) From: Shivam Kalra via B4 Relay Date: Fri, 27 Mar 2026 15:18:41 +0530 Subject: [PATCH v8 5/6] 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: <20260327-vmalloc-shrink-v8-5-cc6b57059ed7@zohomail.in> References: <20260327-vmalloc-shrink-v8-0-cc6b57059ed7@zohomail.in> In-Reply-To: <20260327-vmalloc-shrink-v8-0-cc6b57059ed7@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=1774604917; l=4140; i=shivamkalra98@zohomail.in; s=20260212; h=from:subject:message-id; bh=yGSj5LIL6EaRfv6ZvzI9tnknpwNqR3EakaaeUvTgq0c=; b=yVF907VkAdaIM4lQvlpR88OTp+REg4u+jdY88y3PxpHAqm6Bz3MM0KyTpgnQwxXaWM1Ung3gd wuG0msrMscMDV1Owh6S2AakGeW6x5cnTOOdhyjOEnNbxd1L2FcwiDJD 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 c6bdddee6266..57aec552e038 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; + + /* 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)); + + /* + * 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); + WRITE_ONCE(vm->nr_pages, new_nr_pages); + spin_unlock(&vn->busy.lock); + + 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 Thu Apr 2 17:14: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 ADB5D3DB62F for ; Fri, 27 Mar 2026 09:48: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=1774604920; cv=none; b=mgf3JLd5sI2oywlEmHfUHUqGqNTv38UcC+pLtFlc/RmEovtQl2VFnHYUyyPoMtk4/d5BDc0NqOjTYg3t9bQG6bIZJ6A12G/cDAYauwtdnMTfU40MCs4c71Sqh6zdpG+X6fN10SSFKG71zqGosV9dIOY0ObiDhCX3KAwS5VPYLso= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1774604920; c=relaxed/simple; bh=6f7TOEvk+v4ej1arN46RE3RzMBqAOnaX4qdIPzrc6Wc=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=FbSQ2MQ8WPvhN0pZ5sCTHu9duFCSviYA+SRVyNwEueRiCvESaLJSiQeV9QY8dokWvM3uU9Gy7yZPfOra7KHicSHVfCcxDf0js/O+nQvFCGnRTpbFwuKIvRXn1IzD9oFCXn8N44N4hWk7CoN6geEAyKuTtqgLeVvdKw5IqUzsYWc= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=N4p0vxGv; 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="N4p0vxGv" Received: by smtp.kernel.org (Postfix) with ESMTPS id 8DF0DC2BCB5; Fri, 27 Mar 2026 09:48:40 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1774604920; bh=6f7TOEvk+v4ej1arN46RE3RzMBqAOnaX4qdIPzrc6Wc=; h=From:Date:Subject:References:In-Reply-To:To:Cc:Reply-To:From; b=N4p0vxGvACzu6SJQ693+B0nTysqHWVCI3HVkGtz9AVMl7McDIWinDaCbqK9zHr3nX uutu3RHr5+jf7Q5GsfKDg3O2AvwW/gJS1QSUVJQWQYn1cq+MQcFWWzQCj5byCqO9vf SqXu0Od8pfI9w6Ew7NMQtpGd1B694ovtD1myrHX1ED7YYJQbRg9zpKrXTPYRQW1kjF WsYTGo/mzqZKWmtsT6scaRzLIbs9d7PM6lXBRjkbb5HuuEuDtp0U86frpAUmMAUT5K xHn87GNeeKh4WQpR/3zYY3yBJ8ePUlDXM4ADJ8UG+V3orFRmsNnskXXylV6K5UOrZY dg0l2PxjpO5+A== 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 8431110ED643; Fri, 27 Mar 2026 09:48:40 +0000 (UTC) From: Shivam Kalra via B4 Relay Date: Fri, 27 Mar 2026 15:18:42 +0530 Subject: [PATCH v8 6/6] 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: <20260327-vmalloc-shrink-v8-6-cc6b57059ed7@zohomail.in> References: <20260327-vmalloc-shrink-v8-0-cc6b57059ed7@zohomail.in> In-Reply-To: <20260327-vmalloc-shrink-v8-0-cc6b57059ed7@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=1774604917; l=2848; i=shivamkalra98@zohomail.in; s=20260212; h=from:subject:message-id; bh=q1HLhsQQ92caXQ9Tt0laK39H3cv2PUzBofvprsTi44k=; b=R0zROdvr8i0XX6srRKvEs7y5ZcvavZ3hzjKSDrUemQuMhSzNz/REZ8S6xHmVM5osq+FaQvBjq MEDVIjWXfGOBialWV0d7t7bHDRlk+HhpqGGPILoVSn1BG4qWkK198OY 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). Signed-off-by: Shivam Kalra Reviewed-by: Uladzislau Rezki (Sony) 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