From mboxrd@z Thu Jan 1 00:00:00 1970 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 F14DA47B42F for ; Wed, 1 Apr 2026 17:16:49 +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=1775063810; cv=none; b=IGlke6uTvEYwsJdGbTMvED9UjPpNY+1GfzTqlvBd4ZeQh49mjT7scQxVstoyY/V9UmFfpEV7iNpfeD6Jb1LLkp5TX0aD6qft6fUMHwgF9o2J/LD4QfLg17qG7g21cz5NSketeOsWZvoAXH1b2kveL0X9duFDQQNHZvzZ45+Oels= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1775063810; c=relaxed/simple; bh=Mi9R4u3lZc00+hP0YhV+QFhEHE6cFT/fYerPqhKLNAg=; h=From:Subject:Date:Message-Id:MIME-Version:Content-Type:To:Cc; b=Q49nKb2Fwe7nCVw5GSrF+W7zvMDDVqBjjhc9eIEqNTtVdwpM0vD4mYvYtjB7mhyDJdfVpF8hRiOtJrVjGu1a8onBdawFsnizlx1RZzQv1fsIPEDxmpjmACP6nxeAEZ1h234MNEbsIky0rDbD+/oB75rRKAT3ljGNaFbI+ZX9w2I= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=BHF2LsJB; 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="BHF2LsJB" Received: by smtp.kernel.org (Postfix) with ESMTPS id AA7B1C4CEF7; Wed, 1 Apr 2026 17:16:49 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1775063809; bh=Mi9R4u3lZc00+hP0YhV+QFhEHE6cFT/fYerPqhKLNAg=; h=From:Subject:Date:To:Cc:Reply-To:From; b=BHF2LsJBDQrF5Rl7TOjfHUiRH3dpVR1GWWzlMcXVRdNjRg88TxA/nuH0tpZQCLlED +YXCgAKKAvFqU4ssl107d+lETjlMV3DuKND81o38KMb03E1aK4RRgB+wq1I+riQ7ot 5FDyoyKw+eliOYvD206W72l9goGhdIPHfBT/Jo9MGLyNGtbRtugeE/bwyu71D53KQu PlyZLfPaPQlPmeSVCn7HqTisjPIqnxpVwEm8FGI+eP8cd/uWpeYjXAKIgw6PZYSYJ1 RzlC5fuxGKjlGt2ALmZAZY4cOcUmUMml8h1cyYCxij4k+6ennD5Kgn5tG6AlIy8Rml +24/JrXkE6DlQ== 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 9E65A1076381; Wed, 1 Apr 2026 17:16:49 +0000 (UTC) From: Shivam Kalra via B4 Relay Subject: [PATCH v9 0/4] mm/vmalloc: free unused pages on vrealloc() shrink Date: Wed, 01 Apr 2026 22:46:32 +0530 Message-Id: <20260401-vmalloc-shrink-v9-0-bf58dfb997d8@zohomail.in> 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: 7bit X-B4-Tracking: v=1; b=H4sIAPFSzWkC/3XPwU7EIBAG4FfZcBYzTBmge/I9jIcCgyXutqY1j brpu0v3hEGP/yT/l/lvYuUl8yrOp5tYeMtrnqcS+oeTCOMwvbLMsWSBgAY6QLldh8tlDnIdlzy 9SdAe02CcG5QWpfS+cMqfd/D5peQxrx/z8nX3N3Vc/6U2JUFqEzklbchbfvqex/k65MtjnsSBb VgDugGwAOgC9ioaUAAt0NVA3wBdAYh6SNFFRvYtoCtAtR/oAgRVquA9aaIWoBqwDUAF8D75RDo Qmj8AUwGoGsAUAAyGwXob2GML2BpoJ9hjArBB75ii61rA1UA7wR1AMJ4sUM/R/gb2ff8Bi1sLh XUCAAA= X-Change-ID: 20260302-vmalloc-shrink-04b2fa688a14 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=1775063806; l=5371; i=shivamkalra98@zohomail.in; s=20260212; h=from:subject:message-id; bh=Mi9R4u3lZc00+hP0YhV+QFhEHE6cFT/fYerPqhKLNAg=; b=FM8tzE01dzz+t7eQbf/IJh3MbSsxbVwMnbOU3+5oxRl54okCRP27G8YwkjlxtvPelqoPIfX56 ETb0nSG8TDfDa+9zDhsePpM/JxSGTn6R2tp6ECsi+1OX1q5v41dA2+r 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 This series implements the TODO in vrealloc() to unmap and free unused pages when shrinking across a page boundary. Problem: When vrealloc() shrinks an allocation, it updates bookkeeping (requested_size, KASAN shadow) but does not free the underlying physical pages. This wastes memory for the lifetime of the allocation. Solution: - Patch 1: Extracts a vm_area_free_pages(vm, start_idx, end_idx) helper from vfree() that frees a range of pages with memcg and nr_vmalloc_pages accounting. Freed page pointers are set to NULL to prevent stale references. - Patch 2: Update the grow-in-place check in vrealloc() to compare the requested size against the actual physical page count (vm->nr_pages) rather than the virtual area sizes. This is a prerequisite for shrinking. - Patch 3: Uses the helper to free tail pages when vrealloc() shrinks across a page boundary. - Patch 4: Adds a vrealloc test case to lib/test_vmalloc that exercises grow-realloc, shrink-across-boundary, shrink-within-page, and grow-in-place paths. The virtual address reservation is kept intact to preserve the range for potential future grow-in-place support. A concrete user is the Rust binder driver's KVVec::shrink_to [1], which performs explicit vrealloc() shrinks for memory reclamation. Tested: - KASAN KUnit (vmalloc_oob passes) - lib/test_vmalloc stress tests (3/3, 1M iterations each) - checkpatch, sparse, W=1, allmodconfig, coccicheck clean [1] https://lore.kernel.org/all/20260216-binder-shrink-vec-v3-v6-0-ece8e8593e53@zohomail.in/ Suggested-by: Danilo Krummrich Signed-off-by: Shivam Kalra --- Changes in v9: - Remove READ_ONCE, WRITE_ONCE and drop commit about show_numa_info. (Uladzislau Rezki) - Update the commit message in Patch 2. (Alice Ryhl) - Remove zero newly exposed memory commit. - Link to v8: https://lore.kernel.org/r/20260327-vmalloc-shrink-v8-0-cc6b57059ed7@zohomail.in Changes in v8: - Strip the KASAN tag from the pointer before addr_to_node() to avoid acquiring the wrong node lock (Sashiko). - Rebase to latest mm-new. - Link to v7: https://lore.kernel.org/r/20260324-vmalloc-shrink-v7-0-c0e62b8e5d83@zohomail.in Changes in v7: - Fix NULL pointer dereference in shrink path (Sashiko) - Acquire vn->busy.lock when updating vm->nr_pages to synchronize with concurrent readers (Uladzislau Rezki) - Use READ_ONCE in vmalloc_dump_obj (Sashiko) - Skip shrink path on GFP_NIO or GFP_NOFS. (Sashiko) - Fix Overflow issue for large allocations. (Sashiko) - Use vrealloc instead of vmalloc in vrealloc test. - Link to v6: https://lore.kernel.org/r/20260321-vmalloc-shrink-v6-0-062ca7b7ceb2@zohomail.in Changes in v6: - Fix VM_USERMAP crash by explicitly bypassing early in the shrink path if the flag is set.(Sashiko) - Fix Kmemleak scanner panic by calling kmemleak_free_part() to update tracking on shrink.(Sashiko) - Fix /proc/vmallocinfo race condition by protecting vm->nr_pages access with READ_ONCE()/WRITE_ONCE() for concurrent readers.(Sashiko) - Fix stale data leak on grow-after-shrink by enforcing mandatory zeroing of the newly exposed memory.(Sashiko) - Fix memory leaks in vrealloc_test() by using a temporary pointer to preserve and free the original allocation upon failure.(Sashiko) - Rename vmalloc_free_pages parameters from start/end to start_idx/end_idx for better clarity.(Uladzislau Rezki) - Link to v5: https://lore.kernel.org/r/20260317-vmalloc-shrink-v5-0-bbfbf54c5265@zohomail.in - Link to Sashiko: https://sashiko.dev/#/patchset/20260317-vmalloc-shrink-v5-0-bbfbf54c5265%40zohomail.in Changes in v5: - Skip vrealloc shrink for VM_FLUSH_RESET_PERMS (Uladzislau Rezki) - Link to v4: https://lore.kernel.org/r/20260314-vmalloc-shrink-v4-0-c1e2e0bb5455@zohomail.in Changes in v4: - Rename vmalloc_free_pages() to vm_area_free_pages() to align with vm_area_alloc_pages() (Uladzislau Rezki) - NULL out freed vm->pages[] entries to prevent stale pointers (Alice Ryhl) - Remove redundant if (vm->nr_pages) guard in vfree() (Uladzislau Rezki) - Add vrealloc test case to lib/test_vmalloc (new patch 3/3) - Link to v3: https://lore.kernel.org/r/20260309-vmalloc-shrink-v3-0-5590fd8de2eb@zohomail.in Changes in v3: - Restore the comment. - Rebase to the latest mm-new - Link to v2: https://lore.kernel.org/r/20260304-vmalloc-shrink-v2-0-28c291d60100@zohomail.in Changes in v2: - Updated the base-commit to mm-new - Fix conflicts after rebase - Ran `clang-format` on the changes made - Use a single `kasan_vrealloc` (Alice Ryhl) - Link to v1: https://lore.kernel.org/r/20260302-vmalloc-shrink-v1-0-46deff465b7e@zohomail.in --- Shivam Kalra (4): mm/vmalloc: extract vm_area_free_pages() helper from vfree() mm/vmalloc: use physical page count for vrealloc() grow-in-place check mm/vmalloc: free unused pages on vrealloc() shrink lib/test_vmalloc: add vrealloc test case lib/test_vmalloc.c | 62 ++++++++++++++++++++++++++++++ mm/vmalloc.c | 111 ++++++++++++++++++++++++++++++++++++++++++++--------- 2 files changed, 154 insertions(+), 19 deletions(-) --- base-commit: 54c9d0359b180b34070aa7ff8d9428fa3db8acbb change-id: 20260302-vmalloc-shrink-04b2fa688a14 Best regards, -- Shivam Kalra