From nobody Sat Apr 4 00:24: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 A3F9A2DD60E for ; Sat, 21 Mar 2026 18:05:53 +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=1774116353; cv=none; b=Hul8Ke2CKfV5SEZMJSGTXsdTWUQxegSViypuvG4v9RwHfp5ls40CSdU/MSnQE7cE8WNR60QpdP0nAfxgO80SzBPik4bu0Wmq4xl8F2wykEAGa35rda5UT4PmTFtEPh0FjB3dSUxvb5qgBcdyCqnANQHAgawmIPX8m1fshfqxwGw= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1774116353; c=relaxed/simple; bh=1q9ZbSRou3kZy0bVZall4fchWN5XHmjqwgrNK0kmHD4=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=fbqlLttUjKtftz89QnA1PmEGY9xVUX6BVBmqeHF+X9ODJ8uX2XjXRGNlVIwQfpaExqEp3z8vvuz/PW8/+mZ4mQgxEeN2FdtGQCUVq2j7pQNeZfS7rH5WtrX94ToGgmLNlNzr0CpRVDZ0d9f60wHicHSDMUIwVcve3m+m2UGmWJ8= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=ifXqZ9gr; 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="ifXqZ9gr" Received: by smtp.kernel.org (Postfix) with ESMTPS id 5D6CDC2BCB0; Sat, 21 Mar 2026 18:05:53 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1774116353; bh=1q9ZbSRou3kZy0bVZall4fchWN5XHmjqwgrNK0kmHD4=; h=From:Date:Subject:References:In-Reply-To:To:Cc:Reply-To:From; b=ifXqZ9gr79xAqOtFHJ4E7O9+UUMY0J/I3p+9nrsAG3x/hBN4w8qnSbHgch4uT1uBK ozj7t0A2GRZctnTRusqFMZ/kcw91HS/7faSwlvfx0EUWoftCoY05GhpHjyWbmI8SPr lQ4aeIXam8/af8fc3S8v67nUO3VJyk51Rz914cgGoV2M9QD9eHS7UUL2vpkJpbcAgH wGJd0HgIO9EHMh4x0c3jR5SRYUxA8rxxY3yRtSGgBwh7gDnuoqTIoEGPM1E3+fNLbw uasEGEa4LXl/V+cW3isZIfCK5vxUVZD8QLT5W0LVPyNmN1RYF+8L30bs1uyzzk9tNE 72T5MnnSnF3Sg== 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 4DF2E109448B; Sat, 21 Mar 2026 18:05:53 +0000 (UTC) From: Shivam Kalra via B4 Relay Date: Sat, 21 Mar 2026 23:35:46 +0530 Subject: [PATCH v6 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: <20260321-vmalloc-shrink-v6-1-062ca7b7ceb2@zohomail.in> References: <20260321-vmalloc-shrink-v6-0-062ca7b7ceb2@zohomail.in> In-Reply-To: <20260321-vmalloc-shrink-v6-0-062ca7b7ceb2@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=1774116351; l=2771; i=shivamkalra98@zohomail.in; s=20260212; h=from:subject:message-id; bh=H130FITdP+gd6htRVApNT365j03KxXWr+CTNaNFiPA4=; b=uGx5cc0zWuNS8y3W1NbEacOLTPyrdZLL7S5/yLfUTJOFkmM/1kItTiCIB2t76BpqeE1eGdUNi 7JwPlwnRLO9AA63cg4NH9GKVOC/IZDQd9zmjyQFsgPTc5ygKm5h5cf3 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 c607307c657a..3977398e56e4 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 Sat Apr 4 00:24: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 A402E2DF6EA for ; Sat, 21 Mar 2026 18:05:53 +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=1774116353; cv=none; b=js/3zZKaNrWzemCFLGUcr2FUOigWYjvexMyzMk7ZiPzOjotBTha6dxk2FLdfYOEE1wGXt1Voo0Ah9wTBT5Ma5JpnMHKNlUd0FHsin8c+1LGgHiCcYXMAnxEmLhWOusjWY5+klMGzosJBqeLZjy47Bg/F1CPn6/NSxZSj+ZQHDNw= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1774116353; c=relaxed/simple; bh=VHLu6lZjuBOtEUS8VdUimMihtKDkPhY/OYWvCSJtAds=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=o8yTArJsTye4UZNmnzu2ku1aDbamkimf71jL3lm0xreVAI4E363x5fUgcqGxJ7hAvUvyUZ+DSbj5hKOssR4mrO1xXGfgRya5cCh/q0qivef6v/3jOEWFx/JEHEkZQuSOVu4TSGupJTXgZgaqlvPg5gMNeECyJeDebAthEmFWnF0= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=M/vgM6tD; 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="M/vgM6tD" Received: by smtp.kernel.org (Postfix) with ESMTPS id 6999AC2BCAF; Sat, 21 Mar 2026 18:05:53 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1774116353; bh=VHLu6lZjuBOtEUS8VdUimMihtKDkPhY/OYWvCSJtAds=; h=From:Date:Subject:References:In-Reply-To:To:Cc:Reply-To:From; b=M/vgM6tDO/uLn0NZ+xX/qS3lo54sgsfsZ3UMyIfwozPXaOpUDusCBITwW8wJMZz6x A0SLA1FsoXSKlI4W+T7/q7GugqkKWbPE4/oRlMDf2KzCBn4YFfEzXe876XBpjumYGN Dc7HsQs3YAlMMbbNb9QMC+3ID3wx2neTOlYmCYvTvubYsnKhJFgAsduDTlfE9ousL9 tVefWu9ah6zX3v3HgiUbTdBA0w9ae0Ndgp8FODiH+g21ZGe2Lm83Qjk4CbHmdC+IFJ FeMnZ1D9ZcAsgOUoXnkpgrT7oxzFaSqflI8x9zxd1aGSrdr26EYAtqwtxvMHj2urGq ZHhSEfdzNho7w== 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 5B38F109448E; Sat, 21 Mar 2026 18:05:53 +0000 (UTC) From: Shivam Kalra via B4 Relay Date: Sat, 21 Mar 2026 23:35:47 +0530 Subject: [PATCH v6 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: <20260321-vmalloc-shrink-v6-2-062ca7b7ceb2@zohomail.in> References: <20260321-vmalloc-shrink-v6-0-062ca7b7ceb2@zohomail.in> In-Reply-To: <20260321-vmalloc-shrink-v6-0-062ca7b7ceb2@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=1774116351; l=1250; i=shivamkalra98@zohomail.in; s=20260212; h=from:subject:message-id; bh=w90h6aByrvZe7M9pbnNSecs/NY6vWML8p6XHr/QXZq4=; b=KexBhgjzhjxhyLbuUOfWatjeAXgA9SkFNWaZp4aArRYDJYWYq1g/pF7XSztMPVb/Ga6zsw3X0 LKH+xk6OfJiAKNWXkgjApLUsIQtI71bg+osn31qBTzXt5AdUOUQtbVH 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 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mm/vmalloc.c b/mm/vmalloc.c index 3977398e56e4..7bc7a6892c1a 100644 --- a/mm/vmalloc.c +++ b/mm/vmalloc.c @@ -4361,7 +4361,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 Sat Apr 4 00:24: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 A3F30258EC2 for ; Sat, 21 Mar 2026 18:05:53 +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=1774116353; cv=none; b=Jqu2lrh6we46iJkvRE5mSVfFDXABDQXgB9UeYsslxSNjFIdZjr5HuOZmtnnw1+l4K75UPIoGA1sOf3WvxEsOxBAW+trtr0HHwagPT4vJn+4H8yobhiqa6zMnefz1hH7RN7aUiiLbiLWElY0bVuxFDON3dxFboFLURJeaPT/kN/w= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1774116353; c=relaxed/simple; bh=mH1bbzg1toElM+aYhUOHKFjoyynXL2nUy0Bs26YrHAk=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=iE8+GSRuJu6vbJ4WDtVzO2tFSq2Ge2axACKAKRRMbg1tj7SWroehPorTShcuAmzc1U2IDjBxg5UErM42iFLsUA64xaREK4NjIHrQswCwapnGs51ooF2UTpxDBDho1nuo3EMOx3PUhgsT5Wg6tUasMSnvYozgMicWPZxuhsguCsk= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=VHvRtlHi; 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="VHvRtlHi" Received: by smtp.kernel.org (Postfix) with ESMTPS id 77D59C2BCB3; Sat, 21 Mar 2026 18:05:53 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1774116353; bh=mH1bbzg1toElM+aYhUOHKFjoyynXL2nUy0Bs26YrHAk=; h=From:Date:Subject:References:In-Reply-To:To:Cc:Reply-To:From; b=VHvRtlHi/oByD79fv48x6iNOcazJsOSFTHHkzO4noHSGPOpUtzvjwx2WkqpKLfrM/ NxGxGM5BMJ1psArZpsfahO1N3UkoAQ4f+Sd1Jq0D76Gl2x9cxiBElYIb1psAvW1uH0 Tr+WrnoUO+pZCxWaK5j2eEJINYlNtKSKJmHaaaQO2uHhN0iHfsBi41vAAtvpCr0I6E XPiN498BXqJBP3Ju8/h/ZHJjQ6QGmIT2fmnv53CW8AfR/P6C+6+G/+xphlL/pZyc5X NoALVJ8owxqvX/pplQYFkyPbaQ0acZsRBroV4HJ3NuEBYYz6lg32aM0Emba3UQkBg7 n/7Z9Y7q10cdQ== 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 6B3D3109446F; Sat, 21 Mar 2026 18:05:53 +0000 (UTC) From: Shivam Kalra via B4 Relay Date: Sat, 21 Mar 2026 23:35:48 +0530 Subject: [PATCH v6 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: <20260321-vmalloc-shrink-v6-3-062ca7b7ceb2@zohomail.in> References: <20260321-vmalloc-shrink-v6-0-062ca7b7ceb2@zohomail.in> In-Reply-To: <20260321-vmalloc-shrink-v6-0-062ca7b7ceb2@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=1774116351; l=1805; i=shivamkalra98@zohomail.in; s=20260212; h=from:subject:message-id; bh=uKz24R3I3ek2aPMpo8mT2jA7mZwUk/0kKs44mZjEWwY=; b=SbEulE6vIyO33qNSsTDaNxkXiPh02FBirztOinNJUvoERwLlk3BZOWFs/TSe/RbqSHokKF19L hkAhCqBBgK7DPZC6cRSoEnAdkjU3g8wmoC8brnpz5Q6Dahv1SOjUhu3 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 7bc7a6892c1a..64f5d1088281 100644 --- a/mm/vmalloc.c +++ b/mm/vmalloc.c @@ -4362,13 +4362,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 Sat Apr 4 00:24: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 A6BD02FE560 for ; Sat, 21 Mar 2026 18:05:53 +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=1774116353; cv=none; b=DGQFSN7W5lHdSSma6qFzfVshv+WXHnnlIPyajhkORDsBZ8T9vW9CpwNGGuxo4TR/Tx+EvT4Vup8AdfLL0Yvni5sPrn8nKqwsq8YmGOnMASTmXL6D3gTDyYk6uCTaRyO29Lwj7nKlCTGm6bGx4HN8yB+QkzOsHPn3JdJEJ+1/4vk= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1774116353; c=relaxed/simple; bh=F2hoV91NbwBi3C2o5LYPDy0mIxp4r6AJcp++KA8Q6AI=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=dgoXQQ+PXnjiE6Jo8w9xq+4AcP5WpZqpz6XgUCka/X/Rv6HKpMiGFQTBOidSnetzNPWXcaglvzVZFGPueLPleHn81GzVDOC8eOED5hYktR8KZXvZiDffLUse2de/THNuoSW1yp1mIFEDZXsE0p3CuQwO67Fd1oxarZw2Mldd8Ps= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=NGzkfDeX; 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="NGzkfDeX" Received: by smtp.kernel.org (Postfix) with ESMTPS id 8265BC2BCB4; Sat, 21 Mar 2026 18:05:53 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1774116353; bh=F2hoV91NbwBi3C2o5LYPDy0mIxp4r6AJcp++KA8Q6AI=; h=From:Date:Subject:References:In-Reply-To:To:Cc:Reply-To:From; b=NGzkfDeXe1tl5ifK+LffsZUsRVIRJZ95WGHuPK8CHAtkNnEYdGK4/1OcmXsZTZ5ev jiCEzrxrbiJL4OuDiJ6Xhb67NmSXXhvOPfuj0etFIV3MZJ2aqSZLu1zUD6VCQqh4Qk KrdOPcmizeiVQHBtNacJloWPIK3Kvcu+jAoJPRel6CZQ7o4Gxb86uf7Owx7QE6yx6Z GdnCC8SpfiWUfs8ObRygUs7orvqUh0izR0tL/6pb6zhvT+SxbOfWR/g5EicV/i2GH5 ghv0TFhTr93M3n6hqocxorG5AVlXNfLETCAWTlF8ar4M0hM3koHng5T/myJ7yd9TCi GXH//BlF7M6Gw== 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 7961A109448F; Sat, 21 Mar 2026 18:05:53 +0000 (UTC) From: Shivam Kalra via B4 Relay Date: Sat, 21 Mar 2026 23:35:49 +0530 Subject: [PATCH v6 4/6] mm/vmalloc: protect /proc/vmallocinfo readers with READ_ONCE() 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: <20260321-vmalloc-shrink-v6-4-062ca7b7ceb2@zohomail.in> References: <20260321-vmalloc-shrink-v6-0-062ca7b7ceb2@zohomail.in> In-Reply-To: <20260321-vmalloc-shrink-v6-0-062ca7b7ceb2@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=1774116351; l=2501; i=shivamkalra98@zohomail.in; s=20260212; h=from:subject:message-id; bh=zRiycWT30EskDdJb187oavYNW2pvSidAAoWxjYptHoI=; b=Iy7HQsfLM2cG5f52zW/32wxoytcChbH5qmqXDljNno6hiSOpCsM0jsfPSkf4jREnsMI2UYKAj oIaHkILF7FEDFDs3hMJoP9lBl/FLGd09YT28gU2s7//g4yA0mPqxrng 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 /proc/vmallocinfo readers, specifically show_numa_info() and vmalloc_info_show(), 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 show_numa_info() to use READ_ONCE(v->nr_pages) and READ_ONCE(v->pages[nr]), explicitly checking for NULL before dereferencing the page. Similarly, update vmalloc_info_show() to read nr_pages safely to avoid parsing a torn or inconsistent value. Signed-off-by: Shivam Kalra --- mm/vmalloc.c | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/mm/vmalloc.c b/mm/vmalloc.c index 64f5d1088281..7658fdc087d2 100644 --- a/mm/vmalloc.c +++ b/mm/vmalloc.c @@ -5204,7 +5204,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) @@ -5212,8 +5212,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]); @@ -5241,6 +5246,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= ); @@ -5270,8 +5276,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 Sat Apr 4 00:24: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 CB2173264E6 for ; Sat, 21 Mar 2026 18:05:53 +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=1774116353; cv=none; b=GCikLiFh2I/2o3vJAJVUlVu4t8xy8HV/TwHQQn1ghI9PjLa32f2sXP+h/h6zAcdoj7LIx1YokRCocUAYjdkUh92djxpPfwi1CsFMqsPxgyKEa2CuwbKbrd9hb8JY+6vN3gALZGy58Lx47otk7WdUg5W2c28QTEXAQTFinEx2Psk= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1774116353; c=relaxed/simple; bh=FL4KNiyxKIU/BXU9J94AmsqRhY7dSTdiUBUb9DKhYjo=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=aAhvpTxe7+aFhAHtJdhYRvlNNgrS6nK3/uI1JBgrHvZONtytSl1zLRNEptAWVaeVQc9cTrJt7cF8MeJYgJlhTLxYMQCgLnMyMV0EqmlucpO7L7e2cdwCarHJ4Ai+m28jlVV44PA1z1mCOESlRAbRiQCyJw2toKt2t9TgnnH6Lrc= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=tRQM8Fmq; 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="tRQM8Fmq" Received: by smtp.kernel.org (Postfix) with ESMTPS id 91129C2BCB6; Sat, 21 Mar 2026 18:05:53 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1774116353; bh=FL4KNiyxKIU/BXU9J94AmsqRhY7dSTdiUBUb9DKhYjo=; h=From:Date:Subject:References:In-Reply-To:To:Cc:Reply-To:From; b=tRQM8FmqS6OJuC2hsY/oOCOxwwwLxYgb4e+GbBrLAgdT1yng821eOz+j0jVPuE0DV 0rwHn89egQ8eeQWecTDoiIe0Mb2UnsOE2bKKADDfgz6TeSrgPB3DMn/A/PaKg8OlYf tVRNcz1KfX1u6L5s1D8L+Kk7FljPFIU9JE3sj18VRz2/oHUSl7lRY5Lb6ecTuXygLb o7imEPrBvhrB+BrfES9EYXFHYg6VGzZPn8XhDMbXB/K1T2UkNbbgllWaJmvRpcpYzL StSHUizC7mV7ZRuoDPIgYxmctRy8mUMYN+Uhil9ZR4UGoeZw22DuSV59sPBRpN9hGY ODJmuDLit1lWA== 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 8957D1094478; Sat, 21 Mar 2026 18:05:53 +0000 (UTC) From: Shivam Kalra via B4 Relay Date: Sat, 21 Mar 2026 23:35:50 +0530 Subject: [PATCH v6 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: <20260321-vmalloc-shrink-v6-5-062ca7b7ceb2@zohomail.in> References: <20260321-vmalloc-shrink-v6-0-062ca7b7ceb2@zohomail.in> In-Reply-To: <20260321-vmalloc-shrink-v6-0-062ca7b7ceb2@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=1774116351; l=3733; i=shivamkalra98@zohomail.in; s=20260212; h=from:subject:message-id; bh=XcuDrqFqC2b82xJlkUNy90ghNCbi8jDDIdt9qyKsYoA=; b=n6RzlbrUuUJWWVesQJaNtsmVnVtThnwM2RLFCf6wjivYwpMyZX9PivawyFUS9wWW8zOyBDLmf OSHUB2TTzYPD6fRljbtUA0Hu0WnpVStA5qCI4m4kRMUKPt8fIhPk8xZ 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 /proc/vmallocinfo readers, the shrink path updates vm->nr_pages with WRITE_ONCE() 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 | 42 ++++++++++++++++++++++++++++++++++++++---- 1 file changed, 38 insertions(+), 4 deletions(-) diff --git a/mm/vmalloc.c b/mm/vmalloc.c index 7658fdc087d2..8b8752fc10e9 100644 --- a/mm/vmalloc.c +++ b/mm/vmalloc.c @@ -4345,14 +4345,48 @@ 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. + */ + if (new_nr_pages < vm->nr_pages && !vm_area_page_order(vm) && + !(vm->flags & (VM_FLUSH_RESET_PERMS | VM_USERMAP))) { + 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 + (new_nr_pages << PAGE_SHIFT), + (old_nr_pages - new_nr_pages) << PAGE_SHIFT); + + vunmap_range(addr + (new_nr_pages << PAGE_SHIFT), + addr + (old_nr_pages << PAGE_SHIFT)); + + /* + * Update nr_pages before freeing pages to prevent + * a concurrent reader (e.g. show_numa_info via + * /proc/vmallocinfo) from accessing NULL entries. + */ + WRITE_ONCE(vm->nr_pages, new_nr_pages); + 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 Apr 4 00:24: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 CB2A2326D73 for ; Sat, 21 Mar 2026 18:05:53 +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=1774116353; cv=none; b=r+3twDNL5CIZKbzeD4H/HHGxs0yU5quVT+SI58PUWCfUYfVh1BJ3eCFj25E2ANDudwsZWFjgSswkqFCZotKFr+/k90XBZUIKmyjrMQ/Ab5CE9/1/jiWy79eBL/5i+Jg4O3IwGNsfX6TBPNHmYa4drlN65gTzAkQBYxoROPaPKhs= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1774116353; c=relaxed/simple; bh=9bqC5ujVWHb7sWXrNInVzlf5UOUlvn2ar1xzfw2jX1I=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=B9jzYxHK3um/ei6j9Ho1V/IPID/RRk+h+7Givm7XP1+/2OmnhsE/VA1p0P0Tm7Fp5rcvShUmGJIjGROwlqiQBntbGfhFTZKFFkMox60fn39J54hH8uBXDltWOxDforso5DVc1c2hp0TEH13jnGNLUAISvM/4ICV//ioeS2dfgvE= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=VzoOgJeF; 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="VzoOgJeF" Received: by smtp.kernel.org (Postfix) with ESMTPS id A21D8C2BCB1; Sat, 21 Mar 2026 18:05:53 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1774116353; bh=9bqC5ujVWHb7sWXrNInVzlf5UOUlvn2ar1xzfw2jX1I=; h=From:Date:Subject:References:In-Reply-To:To:Cc:Reply-To:From; b=VzoOgJeFrP3udqQQTFkc225kZwa2LrjN4eBtMprzGpM5CSTqTfvOagYMOTBpK0byL /E7NO7rzYd63+oMCUGcFeQV7kgBqq6Ob47gPb5EQGUjdIopLVU/8bFPwRSNS0lXfPo 73RWLFH2mFoMkVQHJaGRr/tmdBWLTU7g9G+ZBCH2qv5+YlTOdF1OC023QDErAAGMJB YcxqX/JVSppS1R+FDP02dcMl9FJSX1LGwBSHEIPNIxnkkZYkgJekHJrpA3Htzg9pbs V3DKG6+wDf58gdFbyg5IUN5MLlM0b7cH4FJQ5ML5kI6ltumW8psNRWYNOmnBSv8af/ v3Isg5cGrxkbQ== 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 98B1F109448B; Sat, 21 Mar 2026 18:05:53 +0000 (UTC) From: Shivam Kalra via B4 Relay Date: Sat, 21 Mar 2026 23:35:51 +0530 Subject: [PATCH v6 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: <20260321-vmalloc-shrink-v6-6-062ca7b7ceb2@zohomail.in> References: <20260321-vmalloc-shrink-v6-0-062ca7b7ceb2@zohomail.in> In-Reply-To: <20260321-vmalloc-shrink-v6-0-062ca7b7ceb2@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=1774116351; l=2829; i=shivamkalra98@zohomail.in; s=20260212; h=from:subject:message-id; bh=CtQwnpMQocxytVG/1g8li/Ip2q6UVo/IWXgqeQX4pgY=; b=2kxGypkdmPFqpovzEYndnxajXkv892H1Ygv2DTuJh3mpZUYOsFS0e2md3H5567P88Hlk1ecoQ DKohaljaoF7BQHlLow50Jup+4M+3hcKIzHBhcn+6/9Nh2ApcvG3RTF6 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..e822830f0887 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 vmalloc(PAGE_SIZE); + 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