From nobody Thu Apr 2 18:47:48 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 9085B3D75A0 for ; Fri, 27 Mar 2026 09:48:40 +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=1774604920; cv=none; b=J7lZNJZBEYbTPFF5D8JppoWkp/nA5GDC4u3/PeZObhTQM7DSf3VOlUvOnWGaJnUUi/OQgXxGqIVkoAB9Ggh6NbHDQWnD+ksLY1HeBlriPbPY1Z9BylydgiF00rW0JgkNaHJjneM8Rnc2LL7L/QcTfYuJRC+Y1g9XW+WeMzQ9Ruk= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1774604920; c=relaxed/simple; bh=lvRKpv9TIZt0GjqdnA46kctN4zrgkYUFc8NhDjAPR0g=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=eT9z2O7b/8BKFj5htiY1A1VW44N732Y7XLtXDP9kSdqFa/LQGLLKnkHWpNOs3z1uYBZmoLKaYm+wEUPZ8G27wZTiMLbSNCLbvJtq58doWmut+9e/6dFo1xb+TYXGRnQmJlkX99fR+Lq6rTWnQm12u7WfjEjEjjl3SDIxQ/87RM0= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=M3KcyTb4; 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="M3KcyTb4" Received: by smtp.kernel.org (Postfix) with ESMTPS id 66A9BC2BCB2; Fri, 27 Mar 2026 09:48:40 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1774604920; bh=lvRKpv9TIZt0GjqdnA46kctN4zrgkYUFc8NhDjAPR0g=; h=From:Date:Subject:References:In-Reply-To:To:Cc:Reply-To:From; b=M3KcyTb47w0vFP6hbGYmnuAm0Wtz87yqb7HojB2qNLft7UErvvx7wgkC6P4B8LuSj b//75QRXdqXDAt3zlwmUpRZM2OXSKlEAu69j1Dz9JqHHyAXcujV0Pwz/m7ZnSc40Li 6/Q7QGaRjnZ+ujy8hxaYr1gQLx3UZE4z9LOsulj7q2y95sjrhdZF9QFBEpoGrcDkxR Sek03pTvc+vdsEvplCO44tG8Id6KdMI1aLmyaK+FStzGck6ymAw5duZ4C07tOY5CrR u2U8PaO9pi728wBnFw2JtOTz5JH8KOFvn4Ad6VxCUnOfFflGvNQ5I3oyzoFoQQMeL5 2glAc05fPmeQg== 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 5CE8E10ED645; Fri, 27 Mar 2026 09:48:40 +0000 (UTC) From: Shivam Kalra via B4 Relay Date: Fri, 27 Mar 2026 15:18:40 +0530 Subject: [PATCH v8 4/6] mm/vmalloc: use READ_ONCE() for vmalloc nr_pages status readers 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: <20260327-vmalloc-shrink-v8-4-cc6b57059ed7@zohomail.in> References: <20260327-vmalloc-shrink-v8-0-cc6b57059ed7@zohomail.in> In-Reply-To: <20260327-vmalloc-shrink-v8-0-cc6b57059ed7@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=1774604917; l=2959; i=shivamkalra98@zohomail.in; s=20260212; h=from:subject:message-id; bh=q4Ito1vUmJmb3A3de47pGFK3WSX+j54Hs6oFM89TDPE=; b=WAfAOXMaxhCxjvt2ewSFJ24eqk8Y14S2Fzvr0Bi6nQKMPvQrC4YQDdEU9hGatJ8o3viLlAzov V4kYd9euOsHDeADLt//193DDbkOHhKjTrrbTTQyhPFP8B9THCu6sr/a 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 vmalloc status readers (vmalloc_info_show(), show_numa_info(), and vmalloc_dump_obj()) 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 these functions to use READ_ONCE() when accessing v->nr_pages and v->pages[nr]. This ensures the compiler does not re-fetch these values and provides a consistent view of the vmap area's state. Additionally, in show_numa_info(), explicitly check for a NULL page pointer before dereferencing it to avoid potential crashes if a page was concurrently removed during a shrink operation. Signed-off-by: Shivam Kalra --- mm/vmalloc.c | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/mm/vmalloc.c b/mm/vmalloc.c index ddb689bf9ba5..c6bdddee6266 100644 --- a/mm/vmalloc.c +++ b/mm/vmalloc.c @@ -5189,7 +5189,7 @@ bool vmalloc_dump_obj(void *object) vm =3D va->vm; addr =3D (unsigned long) vm->addr; caller =3D vm->caller; - nr_pages =3D vm->nr_pages; + nr_pages =3D READ_ONCE(vm->nr_pages); spin_unlock(&vn->busy.lock); =20 pr_cont(" %u-page vmalloc region starting at %#lx allocated at %pS\n", @@ -5210,7 +5210,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) @@ -5218,8 +5218,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]); @@ -5247,6 +5252,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= ); @@ -5276,8 +5282,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