From nobody Fri Apr 3 16:14:08 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 3B2E03E0249 for ; Tue, 24 Mar 2026 10:00:33 +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=1774346433; cv=none; b=X0CjVr3U+LZoj/3YmMAwF2FDkYjTYrOrU5C11joy+wd+UF5K4fKW6I4lURlcy3hToHvQQDliXif5wWwTOzD6/aVHZNe8LwlmK2WWIXV4dV08ORbTWS70NR/18gQ32VCHVBrCWbtzj/UFNIF71V+3NYDpxz282lkxKeBS393bWg8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1774346433; c=relaxed/simple; bh=lvRKpv9TIZt0GjqdnA46kctN4zrgkYUFc8NhDjAPR0g=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=WP7/yVNTnmV83F9E5efcmEi1MOaStp51HEcWgdtoXU9UxEf2gVBWyguef0MX7t7TKS0ZQk//JsWbd2CPLRfRMgCZy15jqVm/iyACXv1DKG9N9dCDppN8NnKy9fybrKWnLETdMKiQDfxh5hJHzeVzpzMm0q7EIVdsVlqG7klzjto= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=jSMiLBo1; 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="jSMiLBo1" Received: by smtp.kernel.org (Postfix) with ESMTPS id D13F2C2BCB6; Tue, 24 Mar 2026 10:00:32 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1774346432; bh=lvRKpv9TIZt0GjqdnA46kctN4zrgkYUFc8NhDjAPR0g=; h=From:Date:Subject:References:In-Reply-To:To:Cc:Reply-To:From; b=jSMiLBo131wqj1FXMCmPuiWlsDWs4E4y7DNDf1rzL/7IA/iUzmmDFk9BF3vEPMgfe Hwz7Z990wQJu3N80LBtu449qV56Y/IYOSadjUrg/eNqU7l42Ot+Y3kcppQgMN2sIPU f7+g8QXnb05OucTwny1xyPRiFfyD9o2vtpOOb+rtnPPuLh5XwXeuN40XLqq/2sjeiC 7d3tyFqnjAAF2G/5XkFt9Cdg/4G5Irig8JLzS77yRRaYkw7XcYwmwMo/AVQJKCro1v PKh8rwN4Z19RXUqBvaONrUdAvW2nAR40jsHNkXBtALj1loN5UJd5mz4VaoqHUlelEv 2QOp08E2KRm2A== 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 CA809E9A75A; Tue, 24 Mar 2026 10:00:32 +0000 (UTC) From: Shivam Kalra via B4 Relay Date: Tue, 24 Mar 2026 15:30:29 +0530 Subject: [PATCH v7 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: <20260324-vmalloc-shrink-v7-4-c0e62b8e5d83@zohomail.in> References: <20260324-vmalloc-shrink-v7-0-c0e62b8e5d83@zohomail.in> In-Reply-To: <20260324-vmalloc-shrink-v7-0-c0e62b8e5d83@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=1774346430; l=2959; i=shivamkalra98@zohomail.in; s=20260212; h=from:subject:message-id; bh=q4Ito1vUmJmb3A3de47pGFK3WSX+j54Hs6oFM89TDPE=; b=3qyErl+BdMfksbupmMLQP9N6WcJ4/N+QyYsiJ6o6Do43NdkisYyO+pqnixaf4/KMJIZr53QVS n2yE/CxvHXWBew0wRQcmzqpR4d5njVMgs0tkA8bYJzMKeukfqW/6VBs 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