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 6C16E3E5563 for ; Mon, 2 Mar 2026 13:57:37 +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=1772459857; cv=none; b=i5e2GDkw1NhJp4gWbK1iiXnrh1EbhtOcoNfk8NUImiTVaSPlKBplRkMQ1vG5VWuz7SlIpoVFjjeRG/roNIFVhzkjaBMz7MdOsLlU46eZwmsxEkFR+ADe/UEByYYt3eA8wCwJ0dPiqyKrzCeD9nPqYNpFSDuQoPGtSRBt7uvPPs4= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1772459857; c=relaxed/simple; bh=5iYnK32dzzvyJuYT0YsGe8MIZxjRwNUxLtlTWSN7ucQ=; h=From:Subject:Date:Message-Id:MIME-Version:Content-Type:To:Cc; b=fD1q/sOI/gJxuQrtTuV4/W1VFkkz5x029NQhDHCoSvDlgF6b/2BC+J7Pvw912WHbO1FjM1OCC7K39rJiKOtrvwT7SRq+kcm3CoPIXbl5gBpl0P+THna/bOqQedyLJFQdIGbLulneZRndl+qRI3OVIHttUHrhfvaP6H9WAwVrkK8= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=RBkuiSzJ; 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="RBkuiSzJ" Received: by smtp.kernel.org (Postfix) with ESMTPS id 26772C19423; Mon, 2 Mar 2026 13:57:37 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1772459857; bh=5iYnK32dzzvyJuYT0YsGe8MIZxjRwNUxLtlTWSN7ucQ=; h=From:Subject:Date:To:Cc:Reply-To:From; b=RBkuiSzJ19xMqa1li5Y49aF40bQSdnanfH9HcVlxyEENyfve4bLC4yca5Pljz7XgJ 2116FbacPCVyp6CzCZAP24pUBiC1d9Oyf7XPf+RtCGzC2P7WBPMsbOmJm+mb+f+i1n pw7qQv5fUrPBHOkL0Phumx00oAC126DjVtUbWkNJLod+PyX+zV8ceBseq26SILwEgI pqTOI576ZVAcBr9CqpAnMpHUpUV2pFusQ4iD1iRVuMUBE7LVxjntAS4k0pxR3gDnmr AQNHVG4BI3AYdw34Wo7onzWuZB3lf+b5qwgWmYI4Nnqhb73PtVzUO1rW/iNjFg2llW odwGJTyp5tSGg== 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 1200FEA4E07; Mon, 2 Mar 2026 13:57:37 +0000 (UTC) From: Shivam Kalra via B4 Relay Subject: [PATCH 0/2] mm/vmalloc: free unused pages on vrealloc() shrink Date: Mon, 02 Mar 2026 19:27:10 +0530 Message-Id: <20260302-vmalloc-shrink-v1-0-46deff465b7e@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=H4sIADaXpWkC/6tWKk4tykwtVrJSqFYqSi3LLM7MzwNyDHUUlJIzE vPSU3UzU4B8JSMDIzMDYwMj3bLcxJyc/GTd4oyizLxsXQOTJKO0RDMLi0RDEyWgpoKi1LTMCrC B0bG1tQAsCUzIYAAAAA== X-Change-ID: 20260302-vmalloc-shrink-04b2fa688a14 To: Andrew Morton , Uladzislau Rezki Cc: linux-mm@kvack.org, linux-kernel@vger.kernel.org, Shivam Kalra X-Mailer: b4 0.14.3 X-Developer-Signature: v=1; a=ed25519-sha256; t=1772459853; l=1924; i=shivamkalra98@zohomail.in; s=20260212; h=from:subject:message-id; bh=5iYnK32dzzvyJuYT0YsGe8MIZxjRwNUxLtlTWSN7ucQ=; b=BKgLUNX7T5+1UaP68S9aSGWliNwqbIqEeeSBcb/pPTyr3ztcYx4XwgHAxmWrHIoo4l7Tb5wrk MDdDsWbd3r+C2DBna4RbPECc2AE/2IPrRi4vnPjbkySXBypYrM7wwdp 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 vmalloc_free_pages(vm, start, end) helper from vfree() that frees a range of pages with memcg and nr_vmalloc_pages accounting. Pure refactor, no functional change. - Patch 2: Uses the helper to free tail pages when vrealloc() shrinks across a page boundary. Skips huge page allocations (page_order > 0) since compound pages cannot be partially freed. Also fixes the grow-in-place path to check vm->nr_pages instead of get_vm_area_size(), which reflects the virtual reservation and does not change on shrink. 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/ Signed-off-by: Shivam Kalra --- Shivam Kalra (2): mm/vmalloc: extract vmalloc_free_pages() helper from vfree() mm/vmalloc: free unused pages on vrealloc() shrink mm/vmalloc.c | 73 ++++++++++++++++++++++++++++++++++++++++++------------------ 1 file changed, 51 insertions(+), 22 deletions(-) --- base-commit: 3c4ae63073d84abee5d81ce46d86a94e9dae9c89 change-id: 20260302-vmalloc-shrink-04b2fa688a14 Best regards, -- Shivam Kalra