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