From nobody Fri Apr 3 10:19:01 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 38E293E0242 for ; Tue, 24 Mar 2026 10:00:32 +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=1774346433; cv=none; b=slOYJCnmDl/ChBi5mTSyYEx0E+k0F56ArxiT1vMl395T4Tr+1hFYa5rg0Jj2UDqaf9IYh7XC/4HmIBg3sI7UCssT0dkAGL+mljzI/6DYCLM/J/UAQbI1OLqiEXXvNiQICSIq0CmPRgYyHAaLfKUjwCd3bp1asCpUZPPi2ohyyVI= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1774346433; c=relaxed/simple; bh=6VrX9bhFglZF/oJnl60voFlMr8m+M0s8BuRej35uxeQ=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=UMl/mjCJiXRog380vM5YwuoDRiu4sutt4BP3H8F759Y2XOoHijvfxf4TpVCEEBW7x/bMUWifnIxZmKAYC2ZVg7ksQl16uVDo6dcSeNMp52q/MhyGss09Yubr6hCVCPZizGEDWrDgktE6/SCQcWCZ2dRGGrVW36qvUqvGNeSnrZw= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=VNOhBgb6; 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="VNOhBgb6" Received: by smtp.kernel.org (Postfix) with ESMTPS id A87FEC2BCB3; Tue, 24 Mar 2026 10:00:32 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1774346432; bh=6VrX9bhFglZF/oJnl60voFlMr8m+M0s8BuRej35uxeQ=; h=From:Date:Subject:References:In-Reply-To:To:Cc:Reply-To:From; b=VNOhBgb6I+1yGSQ5AOUmtTmFmJw3OJLaxv9bw5EmkX8IP8WfuN+b0dD6YIXAl8zdq pX+0fPbXOqlapQGVHzrDpFC9UFaR4pudTDfT/4kELZIrxdXWN3LnYpezZpyR/Ru1n0 f/Oty7JskPl55+xkTlZO3XNMpfGW9PNaoyHJeoFWiLRAU+pOOMsgtifE8mrduwZy5Q 5nVlLpvSYzUsfUizaaqzGuO6bQWyr5UUoa4INSY21SsBeOWowskX2f5PR2gre31S9K h+bccmkV4WEMBQbpFLFZGkkyHFd2j/hjAw0gupYdgKpHYd/T9aM0UOiAXom2waZkCE t639SGYYzS83Q== 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 94F4BE9A755; Tue, 24 Mar 2026 10:00:32 +0000 (UTC) From: Shivam Kalra via B4 Relay Date: Tue, 24 Mar 2026 15:30:26 +0530 Subject: [PATCH v7 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: <20260324-vmalloc-shrink-v7-1-c0e62b8e5d83@zohomail.in> References: <20260324-vmalloc-shrink-v7-0-c0e62b8e5d83@zohomail.in> In-Reply-To: <20260324-vmalloc-shrink-v7-0-c0e62b8e5d83@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=1774346430; l=2771; i=shivamkalra98@zohomail.in; s=20260212; h=from:subject:message-id; bh=4HOXJZyeFmBfI0AlNIxmjeDSGExIrgz5ceojgaPu1Zc=; b=F+YbrJjMIrhbbW7TDW736w8J1y1lGyqEohEcZY03q4o/K71b02N0sWnHE7fL+CRP+SEr1BNex oLMttuFA6mWBEunvkqyL8uEa9AEODGLFOXycFokBtr0R5HLzuvfad7n 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 --- 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 Fri Apr 3 10:19:01 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 3B3DF3E024E for ; Tue, 24 Mar 2026 10:00:33 +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=1774346433; cv=none; b=bH4Qv2CGYKwmbgAgzO80Eajte8roQWpyCbwfz54P8418XNZxTxlaPE3Z++hjB+GPVnqlSyP9jWE1ok6Uo2im3+fK6A6lpE7b1UeDjBwNLqrDLp6t4n16IA4HThg5EyuxwRLbL9vham+kIHtMIMWMMt3E3atUBeIq64GAoHUV+NA= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1774346433; c=relaxed/simple; bh=uC3ozkw98B2T0ybqh69BkU+YKKQ53sm/70mGSobtbU4=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=MdCC6WQunAef80SUGY8n79nOOCqNqtVsJPpqa7jA+0OGDqAh1UJcjr4Nyn5fujCD3p828VicKxhSKuvcL7jX3t9XkRPKHvpljrCfJgnd1hq2N76vE+Bn9XIx8G24dneqYzOLlcfvd1s69REH19Hr3lLuLlhrqgymIARmwLF+zKA= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=Tc4DToxI; 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="Tc4DToxI" Received: by smtp.kernel.org (Postfix) with ESMTPS id B5A42C2BCB4; Tue, 24 Mar 2026 10:00:32 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1774346432; bh=uC3ozkw98B2T0ybqh69BkU+YKKQ53sm/70mGSobtbU4=; h=From:Date:Subject:References:In-Reply-To:To:Cc:Reply-To:From; b=Tc4DToxIV3GStb+nL3sBLMs9W86XVJT2Kcvl21vGHCt6oAWSFwHHqPTKBOMyv0dGA tbZ7Eaux4csRP4w/j6UOQrLLFK8APCL/Ykx4wDwcF2uem9f6KBcnfME0H1IH6hezLh FfnALjwuW9iXPsVlH5pkaZB62UTKlnqUp/3yEcIAe6hPt+7lzCWjBerkZK7U/vPquy 901D19Hc0gX9w54SvGtuqgwidzmmg4v3nUq3YedV4gLCmhmIB0hdpEvdy6vVpCdulf WIDWnIga/zrm3jsI8XwTENVvmJuAgjtdK13BsKF21t8LyTM4+4JM6plAvC1y9JYuJ8 9CvtBnoycEgtQ== 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 A7B81E9A756; Tue, 24 Mar 2026 10:00:32 +0000 (UTC) From: Shivam Kalra via B4 Relay Date: Tue, 24 Mar 2026 15:30:27 +0530 Subject: [PATCH v7 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: <20260324-vmalloc-shrink-v7-2-c0e62b8e5d83@zohomail.in> References: <20260324-vmalloc-shrink-v7-0-c0e62b8e5d83@zohomail.in> In-Reply-To: <20260324-vmalloc-shrink-v7-0-c0e62b8e5d83@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=1774346430; l=1675; i=shivamkalra98@zohomail.in; s=20260212; h=from:subject:message-id; bh=JBxDN0g82pC84smB7v9qq2A4Kq3XUFnm46m5uC3JQmo=; b=ToQEc7W3F4dNaO9NS6e2EhJdVXvhUeyMcRZr8rpKG784fzxPhhFR4MxXuZliL3GRgONMCpgmM B7Z3kphVsrNDcB5h2un8LpXbgHAS6kTepiP/iKs2WIkhXvjwazEsxDl 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 --- 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 Fri Apr 3 10:19:01 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 3813F3E0241 for ; Tue, 24 Mar 2026 10:00:33 +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=1774346433; cv=none; b=ccng7Eexet2ap3wTfX31hmoNzqz4LZY2+0MFdEWfpaJBMHGiuZ8Bq3SgfOKNPo8T2OUT+8cvorpdxGgZtgsyDCmdTTriV8RTpK9wtLasz4dOefyRaqQP8+3km+j9kXcK3V+INX6XtwqxX4eos7oDd7/nNOuvqU/DFvW/4ipXFjo= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1774346433; c=relaxed/simple; bh=HUOngOFoyGHQgaZ85Xk/jo93b+mIJRl7GJuKRuvWg48=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=QLOwrJE/WOh0GR+UPypqyXp2CMSGhz0nW04a48ZEwburG7CB6sT1QE1XCImYalCuLPjWvEEsGIOAHp3uFyI5Zlls7xkPuQyLPcp8iyXBmYhrTjAwI8SJw4s4mRW6f/0hnDuTZYoDXEQg0VNOPgdYSt/lS1FWT+KaBU7Ku0TBjG4= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=CmtA77oh; 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="CmtA77oh" Received: by smtp.kernel.org (Postfix) with ESMTPS id C45D7C2BCB5; Tue, 24 Mar 2026 10:00:32 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1774346432; bh=HUOngOFoyGHQgaZ85Xk/jo93b+mIJRl7GJuKRuvWg48=; h=From:Date:Subject:References:In-Reply-To:To:Cc:Reply-To:From; b=CmtA77ohefRx7XW6oLVK6YckpgUjIQ9mBCd9cRe05GAn/fLi8dYPU7tFrxUl9Zg4Z xsYfVQYUr1L1gS/7t7usl8Oa/QxEGRVWuLFNcHZ0vaLiMvf+f3f933eXifK2gcu/F4 PnFsmApMZXOkojdPjAMpzf6kkUG5x7w5zNJBOIW04Mybay7iVRsb8kjB8bjPF8rv0D 33DceFVCzfRjWGXkwJ4ZkDTsL9iLqbBd7w0Cv+XNDX4zEJoynxu3sXDG9W/mWWRgbQ 45sn6LSOC5VdsnXvF/1i3oFEJVykqMxQoyDWMuWsRoelnPBebINulo1GbxzIDdf2lf d6P+JGLMtbYUw== 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 BAF8EE9A75C; Tue, 24 Mar 2026 10:00:32 +0000 (UTC) From: Shivam Kalra via B4 Relay Date: Tue, 24 Mar 2026 15:30:28 +0530 Subject: [PATCH v7 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: <20260324-vmalloc-shrink-v7-3-c0e62b8e5d83@zohomail.in> References: <20260324-vmalloc-shrink-v7-0-c0e62b8e5d83@zohomail.in> In-Reply-To: <20260324-vmalloc-shrink-v7-0-c0e62b8e5d83@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=1774346430; l=1805; i=shivamkalra98@zohomail.in; s=20260212; h=from:subject:message-id; bh=LSUYWjYOjJYKYliRO3bYLhKxcMuMIBGaVN2tymO6+ew=; b=rnj4cYZkzkJ86bsOX6qnSlWl5dROAsN7ly5d6iDcEQPVC7kXfPVNp2+aT/Y+JklM8mQ3ukx5Y aizhMbSd8BvDTD8JdpVEUyF3RLaOHctT575Mvr0Wj0aLjsC/fSjn72S 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 --- 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 Fri Apr 3 10:19:01 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 3B2E03E0249 for ; Tue, 24 Mar 2026 10:00:33 +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=1774346433; cv=none; b=X0CjVr3U+LZoj/3YmMAwF2FDkYjTYrOrU5C11joy+wd+UF5K4fKW6I4lURlcy3hToHvQQDliXif5wWwTOzD6/aVHZNe8LwlmK2WWIXV4dV08ORbTWS70NR/18gQ32VCHVBrCWbtzj/UFNIF71V+3NYDpxz282lkxKeBS393bWg8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1774346433; c=relaxed/simple; bh=lvRKpv9TIZt0GjqdnA46kctN4zrgkYUFc8NhDjAPR0g=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=WP7/yVNTnmV83F9E5efcmEi1MOaStp51HEcWgdtoXU9UxEf2gVBWyguef0MX7t7TKS0ZQk//JsWbd2CPLRfRMgCZy15jqVm/iyACXv1DKG9N9dCDppN8NnKy9fybrKWnLETdMKiQDfxh5hJHzeVzpzMm0q7EIVdsVlqG7klzjto= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=jSMiLBo1; 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="jSMiLBo1" Received: by smtp.kernel.org (Postfix) with ESMTPS id D13F2C2BCB6; Tue, 24 Mar 2026 10:00:32 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1774346432; bh=lvRKpv9TIZt0GjqdnA46kctN4zrgkYUFc8NhDjAPR0g=; h=From:Date:Subject:References:In-Reply-To:To:Cc:Reply-To:From; b=jSMiLBo131wqj1FXMCmPuiWlsDWs4E4y7DNDf1rzL/7IA/iUzmmDFk9BF3vEPMgfe Hwz7Z990wQJu3N80LBtu449qV56Y/IYOSadjUrg/eNqU7l42Ot+Y3kcppQgMN2sIPU f7+g8QXnb05OucTwny1xyPRiFfyD9o2vtpOOb+rtnPPuLh5XwXeuN40XLqq/2sjeiC 7d3tyFqnjAAF2G/5XkFt9Cdg/4G5Irig8JLzS77yRRaYkw7XcYwmwMo/AVQJKCro1v PKh8rwN4Z19RXUqBvaONrUdAvW2nAR40jsHNkXBtALj1loN5UJd5mz4VaoqHUlelEv 2QOp08E2KRm2A== 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 CA809E9A75A; Tue, 24 Mar 2026 10:00:32 +0000 (UTC) From: Shivam Kalra via B4 Relay Date: Tue, 24 Mar 2026 15:30:29 +0530 Subject: [PATCH v7 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: <20260324-vmalloc-shrink-v7-4-c0e62b8e5d83@zohomail.in> References: <20260324-vmalloc-shrink-v7-0-c0e62b8e5d83@zohomail.in> In-Reply-To: <20260324-vmalloc-shrink-v7-0-c0e62b8e5d83@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=1774346430; l=2959; i=shivamkalra98@zohomail.in; s=20260212; h=from:subject:message-id; bh=q4Ito1vUmJmb3A3de47pGFK3WSX+j54Hs6oFM89TDPE=; b=3qyErl+BdMfksbupmMLQP9N6WcJ4/N+QyYsiJ6o6Do43NdkisYyO+pqnixaf4/KMJIZr53QVS n2yE/CxvHXWBew0wRQcmzqpR4d5njVMgs0tkA8bYJzMKeukfqW/6VBs 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 --- 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 Fri Apr 3 10:19:01 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 4CCE73E0251 for ; Tue, 24 Mar 2026 10:00:33 +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=1774346433; cv=none; b=D2bF3paDdWOaF246SAy3eBqJdAn8N/I2U4naaB2G8npALpt2z7ka/YOzHMmgD71IJTssax4rGeBrX2svdh6kQPgsT/aWvoSf/gU6ktm4FVoISKo8OQaiR2yacbDp2Q9xWPTzw7dvIy3oFu6bNpnke9MfM+iwgol7TDIbVqrk11Q= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1774346433; c=relaxed/simple; bh=XIeELIdTrhstFpbqKHyFqorwUxZ6LKcOcj+spwpUTMI=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=J6MD2q7v/t1YpForLD+AABgm/M7hk2vMJzhJgrSfe7euJPzZmrZ4+kNmVuFRIch2C3g3l8x6S/1LKYNREco1WdFaYAdpwUz+ENOSokCC2ucVoISMB8DzUAOlyUNH1f+J8uQbNZIz+mymalhjYi6DSXx9DHMN8XGg0maBcxPqI2k= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=ioVEyuNs; 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="ioVEyuNs" Received: by smtp.kernel.org (Postfix) with ESMTPS id E1111C2BCB7; Tue, 24 Mar 2026 10:00:32 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1774346432; bh=XIeELIdTrhstFpbqKHyFqorwUxZ6LKcOcj+spwpUTMI=; h=From:Date:Subject:References:In-Reply-To:To:Cc:Reply-To:From; b=ioVEyuNsZawlKy8sLSV/94NrDwx53sYHeSInn6ovt8kw5tkgNTjqDopTYczVCsdNa /XXGyWEQgQIIAd6SZOrily/E57NFVjXXoBZPze0M3+MSdl6mSKBMQoCGctnw/i+mvj edMDWw8qAgeJ4zVKE7G7oNXt+m0KzRltvEYk5AqL+i2KpUBBmIRZSFzXSwZBC35pgY LaBZMvVCm+G8Dxf/rXv6Jf9E8P6KVgvTV+JQSx2kpz7LDFUJMq2iMhB+bN/cVJu+rr 14vEpNGB1wyFw0I9LkB2sxh+DYFVQTBx+5ake0YFXt9E6L5xbX28V5luCzUDvai3LD y5Ue2kvGLe1Hg== 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 DA72CE9A759; Tue, 24 Mar 2026 10:00:32 +0000 (UTC) From: Shivam Kalra via B4 Relay Date: Tue, 24 Mar 2026 15:30:30 +0530 Subject: [PATCH v7 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: <20260324-vmalloc-shrink-v7-5-c0e62b8e5d83@zohomail.in> References: <20260324-vmalloc-shrink-v7-0-c0e62b8e5d83@zohomail.in> In-Reply-To: <20260324-vmalloc-shrink-v7-0-c0e62b8e5d83@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=1774346430; l=4123; i=shivamkalra98@zohomail.in; s=20260212; h=from:subject:message-id; bh=KqZ1PvSQ5YdeKVCbQTHi48p4pEM47gXj7CPAPxehb3Y=; b=YQXCV+r7FoeEN9pxg9G8f0Y0NC/wtYTE9YQUW4iQaDD1hSwI5QnnHb+kEjCCFcLG5whwIHvxL mywr/WNhWP7DZ1IGZ/WiWkbC+uJS0nz800ngyjyCz5Uz6w/wpSGw3Fy 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..95d6676a8500 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)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 Fri Apr 3 10:19:01 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 457093E024F for ; Tue, 24 Mar 2026 10:00:33 +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=1774346433; cv=none; b=dIyQEjWeu1r7umm/PLVL52tHfx4Ii+Aakld95+xpA8J81BqZztrXCUl9bIl9QIqjG+9wHVatWzwV2yUuGFB+r5x5/LL/T3byxYAXsWLzpxavaSZKGc2EpC7EwNTa2UI4eh/QOhFrMhnNChJAYXG4EBOJJQk/UnZRLfV/dDyttik= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1774346433; c=relaxed/simple; bh=6f7TOEvk+v4ej1arN46RE3RzMBqAOnaX4qdIPzrc6Wc=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=cTM9sSbN1S9BNycYFbwsArtdCYfB/VDrC2q9UKOBAunody8lMeLx+YC9uNfvB/XSo9jo5ZkTOqmERPQaQfejLcnXgt6nG06DYajd3jtwZcYGQo5B4kU7gKAckp3fKQzg1ZlbgIG0NZjG+HnDFi0qMPwMZ4pL+QzZDu409ELO8Pk= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=t7d4aquf; 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="t7d4aquf" Received: by smtp.kernel.org (Postfix) with ESMTPS id F1656C2BCC7; Tue, 24 Mar 2026 10:00:32 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1774346433; bh=6f7TOEvk+v4ej1arN46RE3RzMBqAOnaX4qdIPzrc6Wc=; h=From:Date:Subject:References:In-Reply-To:To:Cc:Reply-To:From; b=t7d4aquf9ItiXQ/V3qTovPcariGkr6uT2eOuxxoQDQxWhDVsgznPqBGgEt/DY/lca blIQdcCA85fis+WXojOw3O9O+Xetf9RXFOx08UuSPpf9NyeBDvz9dEYQWfAkBPTL7+ rhvjcgtfKcXqYDCwXKfdlVYTOcgfi6LN+eYD34CKIqq9zYYDlh6QZGUtZfksEDMCRI hZnTI7UPXzj+ApKLCMkOF/HA7HV+kgHkEwqqhxRzDw9b2mNdZ8xiRdADAEBOh4Q2EU +uhiXC6lONssBaa+1thggtCrBekXHVSW4oEpwtftqw4lYYAmfmg9SQMiBCQjguHG+n 26azKymrbiisw== 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 EA4CDE9A75C; Tue, 24 Mar 2026 10:00:32 +0000 (UTC) From: Shivam Kalra via B4 Relay Date: Tue, 24 Mar 2026 15:30:31 +0530 Subject: [PATCH v7 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: <20260324-vmalloc-shrink-v7-6-c0e62b8e5d83@zohomail.in> References: <20260324-vmalloc-shrink-v7-0-c0e62b8e5d83@zohomail.in> In-Reply-To: <20260324-vmalloc-shrink-v7-0-c0e62b8e5d83@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=1774346430; l=2848; i=shivamkalra98@zohomail.in; s=20260212; h=from:subject:message-id; bh=q1HLhsQQ92caXQ9Tt0laK39H3cv2PUzBofvprsTi44k=; b=BEPTytl4CB3BkcU9Nf+8kk+Rt9BywSe/EuCrTiSmPF99Nwk8oIMH3YmkayqBpuiXrPub0paHm Ibp4V+UQChFCtBgC+E78zmGRwm6aMpqHl26Uu+vIgXrRTK92fNYaxi+ 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 --- 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