Rust for Linux List
 help / color / mirror / Atom feed
* [PATCH] rust: alloc: reject offline NUMA nodes in allocator
@ 2026-11-20  2:16 Franklin-Qi
  2026-11-20  9:47 ` Vlastimil Babka (SUSE)
  0 siblings, 1 reply; 2+ messages in thread
From: Franklin-Qi @ 2026-11-20  2:16 UTC (permalink / raw)
  To: Miguel Ojeda, Danilo Krummrich, Alice Ryhl, Vlastimil Babka
  Cc: rust-for-linux, linux-mm, Boqun Feng, Gary Guo,
	Björn Roy Baron, Benno Lossin, Andreas Hindborg,
	Trevor Gross, Daniel Almeida, Tamir Duberstein, Alexandre Courbot,
	Onur Özkan, Lorenzo Stoakes, Liam R . Howlett,
	Uladzislau Rezki, Harry Yoo, Georgios Androutsopoulos,
	Vitaly Wool, stable, Franklin-Qi

NumaNode::new() only checks that the id is in [0, MAX_NUMNODES). Safe
Allocator APIs can still forward an offline node into
k[v]realloc_node_align(), which may dereference NODE_DATA(nid) == NULL
and panic (GPF).

Reject offline nodes at the shared ReallocFunc boundary for nonzero
allocations. Leave NUMA_NO_NODE and zero-sized free/realloc paths
unchanged. A residual hotplug TOCTOU remains if a node goes offline
after the check; this matches practical C usage for rejecting
statically missing nodes from safe Rust.

Fixes: 7760b6421b6c ("rust: add support for NUMA ids in allocations")
Cc: stable@vger.kernel.org
Reported-by: Georgios Androutsopoulos <georgeandrout13@gmail.com>
Closes: https://github.com/Rust-for-Linux/linux/issues/1263

Signed-off-by: Franklin-Qi <yusq77@gmail.com>
---
 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 b5540997b..b61e54e16 100644
--- a/rust/helpers/mm.c
+++ b/rust/helpers/mm.c
@@ -1,6 +1,7 @@
 // SPDX-License-Identifier: GPL-2.0
 
 #include <linux/mm.h>
+#include <linux/nodemask.h>
 #include <linux/sched/mm.h>
 
 __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 cd4203f27..15e17f598 100644
--- a/rust/kernel/alloc/allocator.rs
+++ b/rust/kernel/alloc/allocator.rs
@@ -101,6 +101,23 @@ unsafe fn call(
         nid: NumaNode,
     ) -> Result<NonNull<[u8]>, AllocError> {
         let size = layout.size();
+
+        // `NumaNode::new` validates the ID range, not whether the node is online.
+        // The C allocation paths require an online node for nonzero allocations.
+        // Keep `NUMA_NO_NODE` and zero-sized deallocations unchanged.
+        //
+        // Residual TOCTOU remains if a node goes offline after this check and
+        // before the allocator runs; that matches practical C usage for rejecting
+        // statically missing/offline nodes from safe Rust.
+        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


^ permalink raw reply related	[flat|nested] 2+ messages in thread

* Re: [PATCH] rust: alloc: reject offline NUMA nodes in allocator
  2026-11-20  2:16 [PATCH] rust: alloc: reject offline NUMA nodes in allocator Franklin-Qi
@ 2026-11-20  9:47 ` Vlastimil Babka (SUSE)
  0 siblings, 0 replies; 2+ messages in thread
From: Vlastimil Babka (SUSE) @ 2026-11-20  9:47 UTC (permalink / raw)
  To: Franklin-Qi, Miguel Ojeda, Danilo Krummrich, Alice Ryhl
  Cc: rust-for-linux, linux-mm, Boqun Feng, Gary Guo,
	Björn Roy Baron, Benno Lossin, Andreas Hindborg,
	Trevor Gross, Daniel Almeida, Tamir Duberstein, Alexandre Courbot,
	Onur Özkan, Lorenzo Stoakes, Liam R . Howlett,
	Uladzislau Rezki, Harry Yoo, Georgios Androutsopoulos,
	Vitaly Wool, stable

On 11/20/26 03:16, Franklin-Qi wrote:
> NumaNode::new() only checks that the id is in [0, MAX_NUMNODES). Safe
> Allocator APIs can still forward an offline node into
> k[v]realloc_node_align(), which may dereference NODE_DATA(nid) == NULL
> and panic (GPF).
> 
> Reject offline nodes at the shared ReallocFunc boundary for nonzero
> allocations. Leave NUMA_NO_NODE and zero-sized free/realloc paths
> unchanged. A residual hotplug TOCTOU remains if a node goes offline
> after the check; this matches practical C usage for rejecting
> statically missing nodes from safe Rust.

IIRC we however don't remove node data on offline, so the NULL dereference
won't happen?

> Fixes: 7760b6421b6c ("rust: add support for NUMA ids in allocations")
> Cc: stable@vger.kernel.org
> Reported-by: Georgios Androutsopoulos <georgeandrout13@gmail.com>
> Closes: https://github.com/Rust-for-Linux/linux/issues/1263
> 
> Signed-off-by: Franklin-Qi <yusq77@gmail.com>
> ---
>  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 b5540997b..b61e54e16 100644
> --- a/rust/helpers/mm.c
> +++ b/rust/helpers/mm.c
> @@ -1,6 +1,7 @@
>  // SPDX-License-Identifier: GPL-2.0
>  
>  #include <linux/mm.h>
> +#include <linux/nodemask.h>
>  #include <linux/sched/mm.h>
>  
>  __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 cd4203f27..15e17f598 100644
> --- a/rust/kernel/alloc/allocator.rs
> +++ b/rust/kernel/alloc/allocator.rs
> @@ -101,6 +101,23 @@ unsafe fn call(
>          nid: NumaNode,
>      ) -> Result<NonNull<[u8]>, AllocError> {
>          let size = layout.size();
> +
> +        // `NumaNode::new` validates the ID range, not whether the node is online.
> +        // The C allocation paths require an online node for nonzero allocations.
> +        // Keep `NUMA_NO_NODE` and zero-sized deallocations unchanged.
> +        //
> +        // Residual TOCTOU remains if a node goes offline after this check and
> +        // before the allocator runs; that matches practical C usage for rejecting
> +        // statically missing/offline nodes from safe Rust.
> +        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`.


^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2026-11-20  9:47 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-20  2:16 [PATCH] rust: alloc: reject offline NUMA nodes in allocator Franklin-Qi
2026-11-20  9:47 ` Vlastimil Babka (SUSE)

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox