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 A09DA379EFC for ; Mon, 20 Apr 2026 16:57:19 +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=1776704239; cv=none; b=NUbGZX+WLh08hF6WJg2WWL5L+8AY1ziONGQuD381INB3p4aUUOTGlRwxDC+V8p5kDsNp4P8ilUHJVrG9vwsNHLsWEozEgmYXPTiAYXb6yi1UpzB1IuuiTN3qgAkv3X9kRPIHssHN4dz2Bmh5idO7oCX6el2kIeVOSLJJWPFgenU= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1776704239; c=relaxed/simple; bh=w7WwPKWlE7tC0vV5+G5MIRv2zSA/nkARAEI7OpAmJ/U=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=InFQeVGsp0ZZpj6TRAlxrV0qOeC8eteUtXbdGzoOGZVowiJBgmbT013sxCrmMLVg5GYEOdQlr2d8bk0wldVqspc8KQTVN9kTp/dn0v6j83Hewg7XbEcoLmhpIyEFk/5AOA5PX+8abCbcGN1xFj/aBSY7tsnSumc1BW2eNzCaf1c= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=hrutyRJK; 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="hrutyRJK" Received: by smtp.kernel.org (Postfix) with ESMTPS id 6076AC2BCB8; Mon, 20 Apr 2026 16:57:19 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1776704239; bh=w7WwPKWlE7tC0vV5+G5MIRv2zSA/nkARAEI7OpAmJ/U=; h=From:Date:Subject:References:In-Reply-To:To:Cc:Reply-To:From; b=hrutyRJKsEhec8uSASXDJsTZpMceAqlVsO89RQemu51hpSAwIkbU8cSz8oZocwph7 mtj+IGFxl06nnVRMXjAgFEC/9JzHGZHXJl6j5BNbkuUoyu51Zn5eTnGyNQxu6jGmxm DqvNo/ubcq3TEZHEuDuFQ856e+KVOqlRQsilkZnLcaEdCp4tD+EU71JfnvYLJpQg1G A3cVV9KlN5A6xDBPGLyhH7TfBlH4wonOA2AVqKOT9yTqvsijaMMhjC/TBqnmAFu0K8 17kXEVwRv5K0z41Aug2G4YbhnYh5ANLmPLH6NCubbiPJ++bcqFvmBMxpQ241BK3oS/ N43MI8aciAZQA== 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 52655F5A8A2; Mon, 20 Apr 2026 16:57:19 +0000 (UTC) From: Shivam Kalra via B4 Relay Date: Mon, 20 Apr 2026 22:27:10 +0530 Subject: [PATCH v11 2/5] mm/vmalloc: use physical page count for 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: 7bit Message-Id: <20260420-vmalloc-shrink-v11-2-cad80b00853a@zohomail.in> References: <20260420-vmalloc-shrink-v11-0-cad80b00853a@zohomail.in> In-Reply-To: <20260420-vmalloc-shrink-v11-0-cad80b00853a@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=1776704237; l=1711; i=shivamkalra98@zohomail.in; s=20260212; h=from:subject:message-id; bh=aeGg5lwRHTMk0K+2dJ+u29myWJHCj9HiY2k+TFLXFfY=; b=O8wYeWoimURVBA+aNcK6Q7i1GzfTIJSlkRFvb7sohqALLUPWR6yNP3UmsRe5bB2fPKrNeok9p J/l3hmoIKgSBiTyTUmC50uSWCEbqYGjzGM+jwR9NleBuN1TQv8Du1Uy 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 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 size (alloced_size, derived from get_vm_area_size()). Currently both values are equivalent, but the upcoming vrealloc() shrink functionality will free pages without reducing the virtual reservation size. After such a shrink, the old alloced_size-based comparison would incorrectly allow a grow-in-place operation to succeed and attempt to access freed pages. Switch to vm->nr_pages now so the check remains correct once shrink support is added. 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 f1be263fdc58..9e2a08c56322 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 != NUMA_NO_NODE && nid != 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; } /* @@ -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 <= alloced_size) { + if (size <= 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 -- 2.43.0