From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=209.85.215.176 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796098868; b=irXJEl6MyAMa0cRXDza09/j3IQvGY6Wcqt8amNx+1FCxWr/sPNrGX8DY4KAIGQstDZig Pn9J4MBbBZRNKTnXX8QKy84FJ0c9B5iT+3xcwh88WNRpM9INqpf+NAZpGuCdKbrVqofi+ t5nDU8mJTZF9PKrcn3Tg5JCFXNeVwqm5ipSt1htH+DYcyzNtB0HGt/4zK+EE/JUbd5r/M a8k/kXagF37Skjz5uUzjy730WOU9PH8dEYwDSfDd1200BuFxpkGIQGWJbP17PEPR/vytt sAjFsmFwXJXxHg1Z7R0GiZPUemM2N2MwM+wJV/7AK7+H7uqO8rOHPbuKmbgn28HgOCg== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796098868; h=DMARC-Filter:Received:DKIM-Signature:X-Google-DKIM-Signature: X-Gm-Message-State:X-Gm-Gg:X-Received:Received:From:To:Cc:Subject: Date:Message-ID:X-Mailer:In-Reply-To:References:MIME-Version: Content-Transfer-Encoding; bh=nORWfMZSQQzuMDK5qharpXaPcx/h7IRWqKu23ER7IfU=; b=ZU1zVo2jMyG6cZG4UYEyqx7oIBeRvqQn85kQxsZp8Jq+T+3l3RMUCHhd1olpJyYorqS6 QC2sRnCWoJyh2gOhHtlcrfn6sM+PE/jE0q0WJd5FbaaYN/4sFv5eAqfomFbBhKjUqVQ/1 3dJ7EXd4QhcH0AKsb/MASrZVDuiZk9vdMjmlFXxEB8G229Zzr2agRWfqJ2oFh4doZYyfL qSYuWSTpK9cPlnAva/gbvQQKFVYs8sZ7QDdhf7J1+Po4hL/KIdrbWz6G7oXmLXlA7AkQU BbyZ2zCHIb2x16Lq+7MxpBSP0U47lwV+oCyHZRf28tfZGh511k1VjOwD0vgLf22/PHQ== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20260707 header.b=VvrI97SB; arc=none smtp.remote-ip=209.85.215.176 Received: from mail-pg1-f176.google.com (mail-pg1-f176.google.com [209.85.215.176]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 4F5EB606A31A for ; Tue, 1 Dec 2026 04:21:08 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 4F5EB606A31A Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20260707 header.b=VvrI97SB Received: by mail-pg1-f176.google.com with SMTP id 41be03b00d2f7-cdadf7e248aso5066292a12.3 for ; Mon, 30 Nov 2026 20:21:08 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20260707; t=1796098868; x=1796703668; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to:content-type; bh=nORWfMZSQQzuMDK5qharpXaPcx/h7IRWqKu23ER7IfU=; b=VvrI97SBlnUUv1kbXzsNKfzeeJAgQV66UFRk1ZrMNxPdg1UcnkdCOHrlFaVFBEolQZ 1HgZtMiGtcgFKtaUXJOCuUMQ+wDFgXRAu3F21ynpTpB6Qk5rkFEQKvmmzW6hcE8Uz+7N TsWbGSXEgcfqxK+NQXRQYS3aKqCLMt1/V8gQkmkJxkNkKj1gWrdgZE73xQ/iXOuL9J8Q VPIOBiYAzrOQqFX6ucT/IH0CTWefLzMgA+eUlUOmce2lzN1vdHcoN30b1VinKvdF2asW +IQGjUQhN5r20z3spzfaymKNLkiHzOPfN29FyFlGqv2ZEY1c+6uUPMcg5FDGvR+aTf08 EXag== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1796098868; x=1796703668; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to:content-type; bh=nORWfMZSQQzuMDK5qharpXaPcx/h7IRWqKu23ER7IfU=; b=cYSAkOnO4iHMVkyEchMc8rnVL+WmhhjppcNQjqSZxBUSACPXnPwH+RlMSSDjpn7bdP HtAfTyNSGK+VMkcZaRpOYEbvABEhf+NAiucGZT3c99pUjwG2JnIzAxaSi1WA5yUW8OPR NQDxiFRpkZ8B404v+LUNraegYVmYqJD5vbj0sm2ArtSBpgJWxL0NqGGh6sP8CnMejdbu zbKiaCLZD1P2f3UhqBGU8FYcthGD8tWrjepv3JPknkpJMTtgz0eINtWItH1F/rMPuwCR 8d6oS8sfssWNyAkFVcB2qPoG7YLtfl0Nj+YUOJC0Lk/synbeJ7396wHNApkibtkw35hV 7cpg== X-Gm-Message-State: APuU4Z9WrkdBcvXcSEYaxlmJGo6Zc09yHbaKLtCYa+RY8eRkD0GV3D/t yOwrQLwligY5jooix2aO2NKMHFO1JdUiEc7oUDvdGXwis3GpLtVu6n/L X-Gm-Gg: AZFdTZfaUPhugsTBDMkxEmOY5GQyQT9bqcB1Q4uXWldxrXW6LSlXlxHbvX8kHTUOIvB YCbnLUZUvhtGURqI6TE9xKs9Wqp4g4OyK+wJvf45kJDNTLqw9ayA4Gb3ITQLBzhAi2nZii9bsb0 YTFkcy0QaCH6SlhkyK7gCnVZdZsDSRpU04bdwp4IwWU/Wjf5C/e1R9RIPpD66nwcBQUNEVW+CXK EPdMeD3HQxD+dwVedF4zczbcBxMmiqhuKjP+V/lXIWRFHBaW9YPPdcqaVOMGhJ8a58LFDoxtjgp 2MMC2+La9Z3K7cmaHoJgTs336a18/aZ1jnBqWNgiLccbnmJYZyDfszBBVyPtq8O/QTfaV3Z34XQ r4sMhrf6p4XszXd44QyBjSpCmrrI3YxIf1mN8+lBBE+t/W4SnEwVM8qxu3tGHdChdx8T0+q9gQm PQxNOdubY0ezo0hYdYm+oZf0tQnM6Pa+MUcjaD7ZrOkTSXALKaDO1AMf00J/bM60op8bh3vm+5A NXU0WZQnNZCSTOabpPZWsnzWwz3YqzDimfYmJ1kpf9ehl4FCZcUAO74wH9JYIfNyS20fRkwkt9t MiXgUB4paUagtAi+hBBbgSS+DI2+VWL9 X-Received: by 2002:a17:90b:3f0f:b0:3a0:b312:1fbe with SMTP id 98e67ed59e1d1-3b41c9006d4mr50458549a91.23.1796098867432; Mon, 30 Nov 2026 20:21:07 -0800 (PST) Received: from ysq-QiTianM428-A376 ([111.201.215.191]) by smtp.gmail.com with ESMTPSA id 41be03b00d2f7-cdec1168866sm9297127a12.25.2026.11.30.20.20.47 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 30 Nov 2026 20:21:07 -0800 (PST) From: Franklin-Qi To: Miguel Ojeda , Danilo Krummrich , Alice Ryhl , Vlastimil Babka Cc: rust-for-linux@vger.kernel.org, linux-mm@kvack.org, Boqun Feng , Gary Guo , =?UTF-8?q?Bj=C3=B6rn=20Roy=20Baron?= , Benno Lossin , Andreas Hindborg , Trevor Gross , Daniel Almeida , Tamir Duberstein , Alexandre Courbot , =?UTF-8?q?Onur=20=C3=96zkan?= , Lorenzo Stoakes , "Liam R . Howlett" , Uladzislau Rezki , Harry Yoo , Georgios Androutsopoulos , Vitaly Wool , Andrew Morton , stable@vger.kernel.org, Franklin-Qi Subject: [PATCH v2] rust: alloc: reject non-online NUMA nodes in allocator Date: Tue, 1 Dec 2026 12:20:38 +0800 Message-ID: <20261201042038.107187-1-yusq77@gmail.com> X-Mailer: git-send-email 2.55.0 In-Reply-To: <20261120021633.12274-1-yusq77@gmail.com> References: <20261120021633.12274-1-yusq77@gmail.com> X-Mailing-List: rust-for-linux@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: 8bit NumaNode::new() only checks that the id is in [0, MAX_NUMNODES). NODE_DATA is allocated for possible nodes (free_area_init / for_each_node). A never-possible id in that range therefore has NODE_DATA(nid) == NULL, and forwarding it into k[v]realloc_node_align() can GPF. Node offlining does not free NODE_DATA; the NULL path is the never-initialized case, not hot-offline. Checking node_online() at the shared ReallocFunc boundary rejects never-possible ids (they are never online) and also offline nodes, matching the C-side warn_if_node_offline() policy for nonzero allocations. Leave NUMA_NO_NODE and zero-sized free/realloc paths unchanged. Fixes: 7760b6421b6c ("rust: add support for NUMA ids in allocations") Cc: stable@vger.kernel.org Reported-by: Georgios Androutsopoulos Closes: https://github.com/Rust-for-Linux/linux/issues/1263 Signed-off-by: Franklin-Qi --- Changes in v2: - Correct the rationale: offlining does not free NODE_DATA; the NULL dereference is from never-possible ids in [0, MAX_NUMNODES) that never got NODE_DATA initialized. - Update the ReallocFunc comment accordingly; keep node_online() (rejects never-possible ids and matches warn_if_node_offline()). rust/helpers/mm.c | 6 ++++++ rust/kernel/alloc/allocator.rs | 18 ++++++++++++++++++ 2 files changed, 24 insertions(+) diff --git a/rust/helpers/mm.c b/rust/helpers/mm.c index b5540997bd20..b61e54e16d2e 100644 --- a/rust/helpers/mm.c +++ b/rust/helpers/mm.c @@ -1,6 +1,7 @@ // SPDX-License-Identifier: GPL-2.0 #include +#include #include __rust_helper void rust_helper_mmgrab(struct mm_struct *mm) @@ -38,6 +39,11 @@ __rust_helper void rust_helper_mmap_read_unlock(struct mm_struct *mm) mmap_read_unlock(mm); } +__rust_helper bool rust_helper_node_online(int nid) +{ + return node_online(nid); +} + __rust_helper struct vm_area_struct * rust_helper_vma_lookup(struct mm_struct *mm, unsigned long addr) { diff --git a/rust/kernel/alloc/allocator.rs b/rust/kernel/alloc/allocator.rs index cd4203f27aed..9bd85f2d3163 100644 --- a/rust/kernel/alloc/allocator.rs +++ b/rust/kernel/alloc/allocator.rs @@ -101,6 +101,23 @@ unsafe fn call( nid: NumaNode, ) -> Result, AllocError> { let size = layout.size(); + + // `NumaNode::new` only validates `[0, MAX_NUMNODES)`. `NODE_DATA` is + // initialized for possible nodes, so a never-possible id in that range + // has `NODE_DATA(nid) == NULL` and can GPF in `k[v]realloc_node_align()`. + // Offlining a node does not free `NODE_DATA`; `node_online` still rejects + // never-possible ids (they are never online) and matches the C-side + // `warn_if_node_offline()` policy for nonzero allocations. Keep + // `NUMA_NO_NODE` and zero-sized free/realloc paths unchanged. + if size != 0 && nid != NumaNode::NO_NODE { + // SAFETY: The helper only reads the kernel's online-node mask. `nid` + // is either `NUMA_NO_NODE` (excluded above) or a value in + // `[0, MAX_NUMNODES)` from `NumaNode::new`. + if !unsafe { bindings::node_online(nid.0) } { + return Err(AllocError); + } + } + let ptr = match ptr { Some(ptr) => { if old_layout.size() == 0 { @@ -116,6 +133,7 @@ unsafe fn call( // - `self.0` is one of `krealloc`, `vrealloc`, `kvrealloc` and thus only requires that // `ptr` is NULL or valid. // - `ptr` is either NULL or valid by the safety requirements of this function. + // - For nonzero allocations with a specific node, `nid` was checked online above. // // GUARANTEE: // - `self.0` is one of `krealloc`, `vrealloc`, `kvrealloc`. -- 2.55.0