From nobody Sat Apr 4 01:49:14 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