Skip to contentSkip to conversation

rust: add a few helpers

PATCH Superseded Andreas Hindborg 5 participants 28 messages Sep 29 – Nov 10

Subsystems (4)

v3 of 4 Superseded Latest is v4 →

lore
Share & tools
lore

Andreas Hindborg

author reviewer
#0 lore raw

v3 cover rust: add a few helpers

This series adds two helpers for obtaining information about the number of numa domains and cpu count of the system. It also moves the NumaNode struct from alloc to numa.

---
Changes in v3:
- Drop Alice tag on patch 2 due to restructuring.
- Use a relaxed load to read nr_online_nodes (Gary).
- Move NumaNode from the alloc module to the numa module in a new patch (Alice).
- Rebase on v7.3-rc5.
- Link to v2: https://msgid.link/[email protected]

Changes in v2:
- Move helpers out of the crate root into the cpu and numa modules (Miguel).
- Mark Rust wrappers #[inline] (Alice).
- Mark the num_possible_cpus C helper __rust_helper (Dirk).
- Mention the Rust null block driver as the consumer in commit messages (Miguel).
- Link to v1: https://msgid.link/[email protected]

---
Andreas Hindborg (3):
rust: add a wrapper for the num_possible_cpus C function
rust: add a wrapper for the nr_online_nodes C function
rust: alloc: move NumaNode to the numa module

 rust/helpers/helpers.c         |  1 +
 rust/helpers/num_cpus.c        |  8 ++++++
 rust/kernel/alloc.rs           | 30 +++-----------------
 rust/kernel/alloc/allocator.rs |  6 ++--
 rust/kernel/alloc/kbox.rs      |  4 +--
 rust/kernel/alloc/kvec.rs      |  4 +--
 rust/kernel/cpu.rs             |  7 +++++
 rust/kernel/lib.rs             |  1 +
 rust/kernel/numa.rs            | 62 ++++++++++++++++++++++++++++++++++++++++++
 9 files changed, 90 insertions(+), 33 deletions(-)

---
base-commit: 72d3fcf802c45d00b300f25b848a93c3a2bd7c7e
change-id: 20260215-cpu-helpers-08efb2572487

Best regards,

Signature · 1 line
Andreas Hindborg <[email protected]>
author reviewer
#1 lore raw

PATCH v3 1/3 rust: add a wrapper for the `num_possible_cpus` C function

This function returns the maximum number of CPUs that can be online.

The wrapper is needed by the Rust null block driver.

---
 rust/helpers/helpers.c  | 1 +
 rust/helpers/num_cpus.c | 8 ++++++++
 rust/kernel/cpu.rs      | 7 +++++++
 3 files changed, 16 insertions(+)
rust/helpers/helpers.c +1 -0
index 440fb7638e3c..89e38b08fd17 100644
@@ -76,6 +76,7 @@
 #include "mm.c"
 #include "mutex.c"
 #include "net/genetlink.c"
+#include "num_cpus.c"
 #include "of.c"
 #include "page.c"
 #include "pci.c"
rust/helpers/num_cpus.c new file +8 -0
new file mode 100644
index 000000000000..54e8bacb46c3
@@ -0,0 +1,8 @@
+// SPDX-License-Identifier: GPL-2.0
+
+#include <linux/cpumask.h>
+
+__rust_helper unsigned int rust_helper_num_possible_cpus(void)
+{
+	return num_possible_cpus();
+}
rust/kernel/cpu.rs +7 -0
index cb6c0338ef5a..b38b6914b12e 100644
@@ -6,6 +6,13 @@
 
 use crate::{bindings, device::Device, error::Result, prelude::ENODEV};
 
+/// Returns the maximum number of CPUs that may be online on the system.
+#[inline]
+pub fn num_possible_cpus() -> u32 {
+    // SAFETY: FFI call with no additional requirements.
+    unsafe { bindings::num_possible_cpus() }
+}
+
 /// Returns the maximum number of possible CPUs in the current system configuration.
 #[inline]
 pub fn nr_cpu_ids() -> u32 {
Signature · 1 line
2.54.0
#2 lore raw

Re: [PATCH v3 1/3] rust: add a wrapper for the `num_possible_cpus` C function

Quoted 8 lines · Andreas Hindborg

This function returns the maximum number of CPUs that can be online.

The wrapper is needed by the Rust null block driver.

I can pick this one -- it could have been an independent patch anyway, no?

Thanks!

Cheers,
Miguel

author reviewer
#3 lore raw

Re: [PATCH v3 1/3] rust: add a wrapper for the `num_possible_cpus` C function

Quoted 11 lines · Miguel Ojeda
Quoted 8 lines · Andreas Hindborg

This function returns the maximum number of CPUs that can be online.

The wrapper is needed by the Rust null block driver.

I can pick this one -- it could have been an independent patch anyway, no?

Yes, this one is independent. At v1 I had 2 patches adding helpers, that was the only thing relating them. I thought it was better to lump them, but maybe not?

Best regards,
Andreas Hindborg

#4 lore raw

Re: [PATCH v3 1/3] rust: add a wrapper for the `num_possible_cpus` C function

Quoted 4 lines · Andreas Hindborg

Yes, this one is independent. At v1 I had 2 patches adding helpers, that was the only thing relating them. I thought it was better to lump them, but maybe not?

Thanks for clarifying!

By the way, do you think the new header is worth it instead of using the existing cpu.c one? If we do that, then we should probably add it under their MAINTAINERS file.

tip may want to take a look or give an Acked-by...

Cheers,
Miguel

author reviewer
#5 lore raw

Re: [PATCH v3 1/3] rust: add a wrapper for the `num_possible_cpus` C function

Quoted 11 lines · Miguel Ojeda
Quoted 4 lines · Andreas Hindborg

Yes, this one is independent. At v1 I had 2 patches adding helpers, that was the only thing relating them. I thought it was better to lump them, but maybe not?

Thanks for clarifying!

By the way, do you think the new header is worth it instead of using the existing cpu.c one? If we do that, then we should probably add it under their MAINTAINERS file.

The helpers should go in cpu.c, that makes sense. Not sure why I put in a separate file, maybe cpu.c was not present when I sent v1.

Do you mind moving the code when applying or do you want a new version?

Best regards,
Andreas Hindborg

#6 lore raw

Re: [PATCH v3 1/3] rust: add a wrapper for the `num_possible_cpus` C function

Quoted 5 lines · Andreas Hindborg

The helpers should go in cpu.c, that makes sense. Not sure why I put in a separate file, maybe cpu.c was not present when I sent v1.

Do you mind moving the code when applying or do you want a new version?

I can do it.

@tip / Peter / Thomas: please shout if you wanted to take this patch instead.

Cheers,
Miguel

#7 lore raw

Re: [PATCH v3 1/3] rust: add a wrapper for the `num_possible_cpus` C function

Quoted 2 lines · Miguel Ojeda

I can do it.

Actually, cpumask.c may be better -- that is where C defines it and that helper has the right #include.

Which in turn makes me wonder if we should also put the Rust one in cpumask.rs instead, rather than cpu.rs.

However, we do already have nr_cpu_ids() in cpu.rs, though, which is however in cpumask.h in C.

What do you think?

Cc'ing cpumask.

Cheers,
Miguel

author reviewer
#8 lore raw

Re: [PATCH v3 1/3] rust: add a wrapper for the `num_possible_cpus` C function

Quoted 7 lines · Miguel Ojeda
Quoted 2 lines · Miguel Ojeda

I can do it.

Actually, cpumask.c may be better -- that is where C defines it and that helper has the right #include.

I don't think we need to split the helper C files by the headers they are defined in. We can include multiple headers in a C file that logically lumps things together. If you are worried about finding things becoming difficult because they are not in the file one expect, I do not share the concern. I usually just grep in the directory through my editor.

But I also don't care. If you want to put it in another file, I will not object at all.

Quoted 2 lines

Which in turn makes me wonder if we should also put the Rust one in cpumask.rs instead, rather than cpu.rs.

I don't think we need that kind of granularity at the moment. If things become messy, we can just refactor.

Quoted 2 lines

However, we do already have nr_cpu_ids() in cpu.rs, though, which is however in cpumask.h in C.

It's fine.

Best regards,
Andreas Hindborg

#9 lore raw

Re: [PATCH v3 1/3] rust: add a wrapper for the `num_possible_cpus` C function

Quoted 7 lines · Andreas Hindborg

I don't think we need to split the helper C files by the headers they are defined in. We can include multiple headers in a C file that logically lumps things together. If you are worried about finding things becoming difficult because they are not in the file one expect, I do not share the concern. I usually just grep in the directory through my editor.

Of course, it doesn't need to be a strict split. But, where possible, I think it is better to match the C side for the helpers, because they are thin wrappers that need to be updated and maintained at the same time as the C side.

In any case, I mentioned it especially because cpumask.h already exists and because it avoids an extra #include as I mentioned.

Quoted 2 lines

I don't think we need that kind of granularity at the moment. If things become messy, we can just refactor.

Just in case: cpumask.rs already exists, i.e. we are not adding new granularity (or what do you mean by "at the moment" otherwise?)

Cheers,
Miguel

#10 lore raw

Re: [PATCH v3 1/3] rust: add a wrapper for the `num_possible_cpus` C function

Quoted 9 lines · Miguel Ojeda
Quoted 7 lines · Andreas Hindborg

I don't think we need to split the helper C files by the headers they are defined in. We can include multiple headers in a C file that logically lumps things together. If you are worried about finding things becoming difficult because they are not in the file one expect, I do not share the concern. I usually just grep in the directory through my editor.

Quoted 4 lines

Of course, it doesn't need to be a strict split. But, where possible, I think it is better to match the C side for the helpers, because they are thin wrappers that need to be updated and maintained at the same time as the C side.

Yes, that's correct. This should mirror the kernel file structure unless there's a strong reason to change that.

Andreas, can you put this into rust/helpers/cpumask.c, and please have the __rust helper thing on a separate line for better greppability.

This patch creates a new file without a maintenance entry, which is a no-go anyways, so we need v4.

Quoted 11 lines

In any case, I mentioned it especially because cpumask.h already exists and because it avoids an extra #include as I mentioned.

Quoted 2 lines

I don't think we need that kind of granularity at the moment. If things become messy, we can just refactor.

Just in case: cpumask.rs already exists, i.e. we are not adding new granularity (or what do you mean by "at the moment" otherwise?)

Cheers,
Miguel

author reviewer
#11 lore raw

Re: [PATCH v3 1/3] rust: add a wrapper for the `num_possible_cpus` C function

Quoted 15 lines · Yury Norov
Quoted 9 lines · Miguel Ojeda
Quoted 7 lines · Andreas Hindborg

I don't think we need to split the helper C files by the headers they are defined in. We can include multiple headers in a C file that logically lumps things together. If you are worried about finding things becoming difficult because they are not in the file one expect, I do not share the concern. I usually just grep in the directory through my editor.

Quoted 4 lines

Of course, it doesn't need to be a strict split. But, where possible, I think it is better to match the C side for the helpers, because they are thin wrappers that need to be updated and maintained at the same time as the C side.

Quoted 8 lines

Yes, that's correct. This should mirror the kernel file structure unless there's a strong reason to change that.

Andreas, can you put this into rust/helpers/cpumask.c, and please have the __rust helper thing on a separate line for better greppability.

This patch creates a new file without a maintenance entry, which is a no-go anyways, so we need v4.

Ok, thanks.

@Miguel, feel free to make these changes as you apply. Otherwise I'll send v4 next cycle.

Best regards,
Andreas Hindborg

#12 lore raw

Re: [PATCH v3 1/3] rust: add a wrapper for the `num_possible_cpus` C function

Quoted 5 lines · Andreas Hindborg

Ok, thanks.

@Miguel, feel free to make these changes as you apply. Otherwise I'll send v4 next cycle.

I think at least for this one (#1) it is best to have a v4 so that it is clear to everyone what we agree to finally, and there is no rush.

For #2, I guess Yury or I could take it already. Yury said he would perhaps take them (not sure if the Reviewed-by was so that I take it or as a self-review, Yury? Thanks for taking a look at Andreas' patches, by the way!)

Cheers,
Miguel

#13 lore raw

Re: [PATCH v3 1/3] rust: add a wrapper for the `num_possible_cpus` C function

Quoted 7 lines · Miguel Ojeda
Quoted 5 lines · Andreas Hindborg

Ok, thanks.

@Miguel, feel free to make these changes as you apply. Otherwise I'll send v4 next cycle.

Quoted 7 lines

I think at least for this one (#1) it is best to have a v4 so that it is clear to everyone what we agree to finally, and there is no rush.

For #2, I guess Yury or I could take it already. Yury said he would perhaps take them (not sure if the Reviewed-by was so that I take it or as a self-review, Yury? Thanks for taking a look at Andreas' patches, by the way!)

Sure, np!

If there's no rush, let's not cut the corners and send a proper v4.

Regarding the branch to merge in - I just wanted to unload your burden a little :), but if you prefer to move it yourself - my reviewed-by tags are already there. Ether way works.

Thanks,
Yury

maintainer
#14 lore raw

Re: [PATCH v3 1/3] rust: add a wrapper for the `num_possible_cpus` C function

In reply to Andreas Hindborg
Quoted 3 lines · Andreas Hindborg

This function returns the maximum number of CPUs that can be online.

The wrapper is needed by the Rust null block driver.

What does it need it for?

author reviewer
#15 lore raw

Re: [PATCH v3 1/3] rust: add a wrapper for the `num_possible_cpus` C function

Quoted 6 lines · Peter Zijlstra
Quoted 3 lines · Andreas Hindborg

This function returns the maximum number of CPUs that can be online.

The wrapper is needed by the Rust null block driver.

What does it need it for?

Setting an upper bound on the number of submit and poll queues.

Best regards,
Andreas Hindborg

maintainer
#16 lore raw

Re: [PATCH v3 1/3] rust: add a wrapper for the `num_possible_cpus` C function

Quoted 10 lines · Andreas Hindborg
Quoted 6 lines · Peter Zijlstra
Quoted 3 lines · Andreas Hindborg

This function returns the maximum number of CPUs that can be online.

The wrapper is needed by the Rust null block driver.

What does it need it for?

Setting an upper bound on the number of submit and poll queues.

OK I suppose. As long as nobody is going to try and use the return value as a max for cpuid. That is, num_possible_cpus() <= nr_cpu_ids and all that.

author reviewer
#17 lore raw

PATCH v3 2/3 rust: add a wrapper for the `nr_online_nodes` C function

In reply to Andreas Hindborg

This function returns the number of online NUMA nodes.

The wrapper is needed by the Rust null block driver.

---
 rust/kernel/lib.rs  |  1 +
 rust/kernel/numa.rs | 29 +++++++++++++++++++++++++++++
 2 files changed, 30 insertions(+)
rust/kernel/lib.rs +1 -0
index 4d5c96ddc49c..a7b1c9cf454f 100644
@@ -105,6 +105,7 @@
 #[cfg(CONFIG_NET)]
 pub mod net;
 pub mod num;
+pub mod numa;
 pub mod of;
 #[cfg(CONFIG_PM_OPP)]
 pub mod opp;
rust/kernel/numa.rs new file +29 -0
new file mode 100644
index 000000000000..164e622d325f
@@ -0,0 +1,29 @@
+// SPDX-License-Identifier: GPL-2.0
+
+//! NUMA topology utilities.
+//!
+//! C header: [`include/linux/nodemask.h`](srctree/include/linux/nodemask.h)
+
+use crate::bindings;
+#[cfg(CONFIG_NUMA)]
+use crate::sync::atomic::{
+    Atomic,
+    Relaxed, //
+};
+
+/// Returns the number of online NUMA nodes.
+#[inline]
+pub fn num_online_nodes() -> u32 {
+    #[cfg(CONFIG_NUMA)]
+    {
+        // SAFETY: `nr_online_nodes` is a global variable that is valid for the lifetime of the
+        // kernel. The C side updates it with plain writes during node hotplug and reads it with
+        // plain reads. A relaxed load is the Rust equivalent of `READ_ONCE()`, which is the best
+        // we can do on the reader side.
+        unsafe { Atomic::from_ptr(&raw mut bindings::nr_online_nodes) }.load(Relaxed)
+    }
+    #[cfg(not(CONFIG_NUMA))]
+    {
+        bindings::nr_online_nodes
+    }
+}
Signature · 1 line
2.54.0
reviewer Reviewed-by on v3 2/3
#18 lore raw

Re: [PATCH v3 2/3] rust: add a wrapper for the `nr_online_nodes` C function

Quoted 5 lines · Andreas Hindborg

This function returns the number of online NUMA nodes.

The wrapper is needed by the Rust null block driver.

It's fine to ignore Sashiko. Although, ideally we change C code to do WRITE_ONCE().

Best,
Gary

Quoted 4 lines
---
 rust/kernel/lib.rs  |  1 +
 rust/kernel/numa.rs | 29 +++++++++++++++++++++++++++++
 2 files changed, 30 insertions(+)
#19 lore raw

Re: [PATCH v3 2/3] rust: add a wrapper for the `nr_online_nodes` C function

In reply to Andreas Hindborg

rust/kernel/numa.rs line 5

Quoted 4 lines · Andreas Hindborg
rust/kernel/numa.rs new file +3 -0
+//! NUMA topology utilities.
+//!
+//! C header: [`include/linux/nodemask.h`](srctree/include/linux/nodemask.h)

Cc'ing Yury and Rasmus since it refers to this file.

page_alloc.c also has the definition of the variable in the MAX_NUMNODES > 1 case, so I guess the MM page alloc folks should also be Cc'd -- doing it too here.

In any case, I am happy to pick this one up if they are OK with it.

Thanks!

Cheers,
Miguel

#20 lore raw

Re: [PATCH v3 2/3] rust: add a wrapper for the `nr_online_nodes` C function

Quoted 7 lines · Miguel Ojeda

rust/kernel/numa.rs line 5

Quoted 4 lines · Andreas Hindborg
rust/kernel/numa.rs new file +3 -0
+//! NUMA topology utilities.
+//!
+//! C header: [`include/linux/nodemask.h`](srctree/include/linux/nodemask.h)

Cc'ing Yury and Rasmus since it refers to this file.

Thanks, Miguel.

Quoted 5 lines

page_alloc.c also has the definition of the variable in the MAX_NUMNODES > 1 case, so I guess the MM page alloc folks should also be Cc'd -- doing it too here.

In any case, I am happy to pick this one up if they are OK with it.

Let me walk through the series. If everything is OK, I'll take it myself.

Reviewed-by on v3 2/3
#21 lore raw

Re: [PATCH v3 2/3] rust: add a wrapper for the `nr_online_nodes` C function

In reply to Andreas Hindborg
Quoted 5 lines · Andreas Hindborg

This function returns the number of online NUMA nodes.

The wrapper is needed by the Rust null block driver.

Quoted 57 lines
---
 rust/kernel/lib.rs  |  1 +
 rust/kernel/numa.rs | 29 +++++++++++++++++++++++++++++
 2 files changed, 30 insertions(+)
rust/kernel/lib.rs +1 -0
index 4d5c96ddc49c..a7b1c9cf454f 100644
@@ -105,6 +105,7 @@
 #[cfg(CONFIG_NET)]
 pub mod net;
 pub mod num;
+pub mod numa;
 pub mod of;
 #[cfg(CONFIG_PM_OPP)]
 pub mod opp;
rust/kernel/numa.rs new file +29 -0
new file mode 100644
index 000000000000..164e622d325f
@@ -0,0 +1,29 @@
+// SPDX-License-Identifier: GPL-2.0
+
+//! NUMA topology utilities.
+//!
+//! C header: [`include/linux/nodemask.h`](srctree/include/linux/nodemask.h)
+
+use crate::bindings;
+#[cfg(CONFIG_NUMA)]
+use crate::sync::atomic::{
+    Atomic,
+    Relaxed, //
+};
+
+/// Returns the number of online NUMA nodes.
+#[inline]
+pub fn num_online_nodes() -> u32 {
+    #[cfg(CONFIG_NUMA)]
+    {
+        // SAFETY: `nr_online_nodes` is a global variable that is valid for the lifetime of the
+        // kernel. The C side updates it with plain writes during node hotplug and reads it with
+        // plain reads. A relaxed load is the Rust equivalent of `READ_ONCE()`, which is the best
+        // we can do on the reader side.
+        unsafe { Atomic::from_ptr(&raw mut bindings::nr_online_nodes) }.load(Relaxed)
+    }
+    #[cfg(not(CONFIG_NUMA))]
+    {
+        bindings::nr_online_nodes
+    }
+}
Signature · 2 lines
-- 
2.54.0
author reviewer
#22 lore raw

PATCH v3 3/3 rust: alloc: move `NumaNode` to the `numa` module

In reply to Andreas Hindborg

NumaNode identifies a NUMA node and is not specific to the allocator API. Move it from the alloc module to the numa module so that NUMA related items live together.

No functional change.

---
 rust/kernel/alloc.rs           | 30 ++++--------------------------
 rust/kernel/alloc/allocator.rs |  6 +++---
 rust/kernel/alloc/kbox.rs      |  4 ++--
 rust/kernel/alloc/kvec.rs      |  4 ++--
 rust/kernel/numa.rs            | 35 ++++++++++++++++++++++++++++++++++-
 5 files changed, 45 insertions(+), 34 deletions(-)
rust/kernel/alloc.rs +4 -26
index 21067bde6860..9daddf540e7d 100644
@@ -22,7 +22,10 @@
 #[derive(Copy, Clone, PartialEq, Eq, Debug)]
 pub struct AllocError;
 
-use crate::prelude::*;
+use crate::{
+    numa::NumaNode,
+    prelude::*, //
+};
 
 use core::{
     alloc::Layout,
@@ -114,31 +117,6 @@ pub mod flags {
     pub const __GFP_NOWARN: Flags = Flags(bindings::__GFP_NOWARN);
 }
 
-/// Non Uniform Memory Access (NUMA) node identifier.
-#[derive(Clone, Copy, PartialEq)]
-pub struct NumaNode(i32);
-
-impl NumaNode {
-    /// Create a new NUMA node identifier (non-negative integer).
-    ///
-    /// Returns [`EINVAL`] if a negative id or an id exceeding [`bindings::MAX_NUMNODES`] is
-    /// specified.
-    pub fn new(node: i32) -> Result<Self> {
-        // MAX_NUMNODES never exceeds 2**10 because NODES_SHIFT is 0..10.
-        if node < 0 || node >= bindings::MAX_NUMNODES as i32 {
-            return Err(EINVAL);
-        }
-        Ok(Self(node))
-    }
-}
-
-/// Specify necessary constant to pass the information to Allocator that the caller doesn't care
-/// about the NUMA node to allocate memory from.
-impl NumaNode {
-    /// No node preference.
-    pub const NO_NODE: NumaNode = NumaNode(bindings::NUMA_NO_NODE);
-}
-
 /// The kernel's [`Allocator`] trait.
 ///
 /// An implementation of [`Allocator`] can allocate, re-allocate and free memory buffers described
rust/kernel/alloc/allocator.rs +3 -3
index cd4203f27aed..4af50e20933b 100644
@@ -11,12 +11,12 @@
 use super::{
     AllocError,
     Allocator,
-    Flags,
-    NumaNode, //
+    Flags, //
 };
 
 use crate::{
     bindings,
+    numa::NumaNode,
     page, //
 };
 
@@ -122,7 +122,7 @@ unsafe fn call(
         // - Those functions provide the guarantees of this function.
         let raw_ptr = unsafe {
             // If `size == 0` and `ptr != NULL` the memory behind the pointer is freed.
-            self.0(ptr.cast(), size, layout.align(), flags.0, nid.0).cast()
+            self.0(ptr.cast(), size, layout.align(), flags.0, nid.as_raw()).cast()
         };
 
         let ptr = if size == 0 {
rust/kernel/alloc/kbox.rs +2 -2
index c63d6acdbb6f..0fa736c81287 100644
@@ -13,12 +13,12 @@
 use super::{
     AllocError,
     Allocator,
-    Flags,
-    NumaNode, //
+    Flags, //
 };
 
 use crate::{
     fmt,
+    numa::NumaNode,
     page::AsPageIter,
     prelude::*,
     types::ForeignOwnable, //
rust/kernel/alloc/kvec.rs +2 -2
index c7546b9da4fa..b669ca10b3f7 100644
@@ -14,12 +14,12 @@
     AllocError,
     Allocator,
     Box,
-    Flags,
-    NumaNode, //
+    Flags, //
 };
 
 use crate::{
     fmt,
+    numa::NumaNode,
     page::{
         AsPageIter,
         PAGE_SIZE, //
rust/kernel/numa.rs +34 -1
index 164e622d325f..4a3a880308f3 100644
@@ -4,12 +4,15 @@
 //!
 //! C header: [`include/linux/nodemask.h`](srctree/include/linux/nodemask.h)
 
-use crate::bindings;
 #[cfg(CONFIG_NUMA)]
 use crate::sync::atomic::{
     Atomic,
     Relaxed, //
 };
+use crate::{
+    bindings,
+    prelude::*, //
+};
 
 /// Returns the number of online NUMA nodes.
 #[inline]
@@ -27,3 +30,33 @@ pub fn num_online_nodes() -> u32 {
         bindings::nr_online_nodes
     }
 }
+
+/// Non Uniform Memory Access (NUMA) node identifier.
+#[derive(Clone, Copy, PartialEq)]
+pub struct NumaNode(i32);
+
+impl NumaNode {
+    /// Create a new NUMA node identifier (non-negative integer).
+    ///
+    /// Returns [`EINVAL`] if a negative id or an id exceeding [`bindings::MAX_NUMNODES`] is
+    /// specified.
+    pub fn new(node: i32) -> Result<Self> {
+        // MAX_NUMNODES never exceeds 2**10 because NODES_SHIFT is 0..10.
+        if node < 0 || node >= bindings::MAX_NUMNODES as i32 {
+            return Err(EINVAL);
+        }
+        Ok(Self(node))
+    }
+
+    /// Get the raw representation of this node identifier.
+    pub(crate) fn as_raw(self) -> i32 {
+        self.0
+    }
+}
+
+/// Specify necessary constant to pass the information to Allocator that the caller doesn't care
+/// about the NUMA node to allocate memory from.
+impl NumaNode {
+    /// No node preference.
+    pub const NO_NODE: NumaNode = NumaNode(bindings::NUMA_NO_NODE);
+}
Signature · 1 line
2.54.0
reviewer
#23 lore raw

Re: [PATCH v3 3/3] rust: alloc: move `NumaNode` to the `numa` module

Quoted 153 lines · Andreas Hindborg

NumaNode identifies a NUMA node and is not specific to the allocator API. Move it from the alloc module to the numa module so that NUMA related items live together.

No functional change.

---
 rust/kernel/alloc.rs           | 30 ++++--------------------------
 rust/kernel/alloc/allocator.rs |  6 +++---
 rust/kernel/alloc/kbox.rs      |  4 ++--
 rust/kernel/alloc/kvec.rs      |  4 ++--
 rust/kernel/numa.rs            | 35 ++++++++++++++++++++++++++++++++++-
 5 files changed, 45 insertions(+), 34 deletions(-)
rust/kernel/alloc.rs +4 -26
index 21067bde6860..9daddf540e7d 100644
@@ -22,7 +22,10 @@
 #[derive(Copy, Clone, PartialEq, Eq, Debug)]
 pub struct AllocError;
 
-use crate::prelude::*;
+use crate::{
+    numa::NumaNode,
+    prelude::*, //
+};
 
 use core::{
     alloc::Layout,
@@ -114,31 +117,6 @@ pub mod flags {
     pub const __GFP_NOWARN: Flags = Flags(bindings::__GFP_NOWARN);
 }
 
-/// Non Uniform Memory Access (NUMA) node identifier.
-#[derive(Clone, Copy, PartialEq)]
-pub struct NumaNode(i32);
-
-impl NumaNode {
-    /// Create a new NUMA node identifier (non-negative integer).
-    ///
-    /// Returns [`EINVAL`] if a negative id or an id exceeding [`bindings::MAX_NUMNODES`] is
-    /// specified.
-    pub fn new(node: i32) -> Result<Self> {
-        // MAX_NUMNODES never exceeds 2**10 because NODES_SHIFT is 0..10.
-        if node < 0 || node >= bindings::MAX_NUMNODES as i32 {
-            return Err(EINVAL);
-        }
-        Ok(Self(node))
-    }
-}
-
-/// Specify necessary constant to pass the information to Allocator that the caller doesn't care
-/// about the NUMA node to allocate memory from.
-impl NumaNode {
-    /// No node preference.
-    pub const NO_NODE: NumaNode = NumaNode(bindings::NUMA_NO_NODE);
-}
-
 /// The kernel's [`Allocator`] trait.
 ///
 /// An implementation of [`Allocator`] can allocate, re-allocate and free memory buffers described
rust/kernel/alloc/allocator.rs +3 -3
index cd4203f27aed..4af50e20933b 100644
@@ -11,12 +11,12 @@
 use super::{
     AllocError,
     Allocator,
-    Flags,
-    NumaNode, //
+    Flags, //
 };
 
 use crate::{
     bindings,
+    numa::NumaNode,
     page, //
 };
 
@@ -122,7 +122,7 @@ unsafe fn call(
         // - Those functions provide the guarantees of this function.
         let raw_ptr = unsafe {
             // If `size == 0` and `ptr != NULL` the memory behind the pointer is freed.
-            self.0(ptr.cast(), size, layout.align(), flags.0, nid.0).cast()
+            self.0(ptr.cast(), size, layout.align(), flags.0, nid.as_raw()).cast()
         };
 
         let ptr = if size == 0 {
rust/kernel/alloc/kbox.rs +2 -2
index c63d6acdbb6f..0fa736c81287 100644
@@ -13,12 +13,12 @@
 use super::{
     AllocError,
     Allocator,
-    Flags,
-    NumaNode, //
+    Flags, //
 };
 
 use crate::{
     fmt,
+    numa::NumaNode,
     page::AsPageIter,
     prelude::*,
     types::ForeignOwnable, //
rust/kernel/alloc/kvec.rs +2 -2
index c7546b9da4fa..b669ca10b3f7 100644
@@ -14,12 +14,12 @@
     AllocError,
     Allocator,
     Box,
-    Flags,
-    NumaNode, //
+    Flags, //
 };
 
 use crate::{
     fmt,
+    numa::NumaNode,
     page::{
         AsPageIter,
         PAGE_SIZE, //
rust/kernel/numa.rs +4 -1
index 164e622d325f..4a3a880308f3 100644
@@ -4,12 +4,15 @@
 //!
 //! C header: [`include/linux/nodemask.h`](srctree/include/linux/nodemask.h)
 
-use crate::bindings;
 #[cfg(CONFIG_NUMA)]
 use crate::sync::atomic::{
     Atomic,
     Relaxed, //
 };
+use crate::{
+    bindings,
+    prelude::*, //
+};
 
 /// Returns the number of online NUMA nodes.
 #[inline]

rust/kernel/numa.rs pub fn num_online_nodes() -> u32 { · line 36

Quoted 7 lines
rust/kernel/numa.rs +4 -0
@@ -27,3 +30,33 @@ pub fn num_online_nodes() -> u32 {
         bindings::nr_online_nodes
     }
 }
+
+/// Non Uniform Memory Access (NUMA) node identifier.
+#[derive(Clone, Copy, PartialEq)]
+pub struct NumaNode(i32);

FWIW the C type is nodemask_t and this should have mask in the name too. It is not an identifier. That said, this is an existing issue, so shouldn't block the move.

Best,
Gary

Quoted 26 lines
rust/kernel/numa.rs +26 -0
+
+impl NumaNode {
+    /// Create a new NUMA node identifier (non-negative integer).
+    ///
+    /// Returns [`EINVAL`] if a negative id or an id exceeding [`bindings::MAX_NUMNODES`] is
+    /// specified.
+    pub fn new(node: i32) -> Result<Self> {
+        // MAX_NUMNODES never exceeds 2**10 because NODES_SHIFT is 0..10.
+        if node < 0 || node >= bindings::MAX_NUMNODES as i32 {
+            return Err(EINVAL);
+        }
+        Ok(Self(node))
+    }
+
+    /// Get the raw representation of this node identifier.
+    pub(crate) fn as_raw(self) -> i32 {
+        self.0
+    }
+}
+
+/// Specify necessary constant to pass the information to Allocator that the caller doesn't care
+/// about the NUMA node to allocate memory from.
+impl NumaNode {
+    /// No node preference.
+    pub const NO_NODE: NumaNode = NumaNode(bindings::NUMA_NO_NODE);
+}
author reviewer
#24 lore raw

Re: [PATCH v3 3/3] rust: alloc: move `NumaNode` to the `numa` module

Quoted 151 lines · Gary Guo
Quoted 150 lines · Andreas Hindborg

NumaNode identifies a NUMA node and is not specific to the allocator API. Move it from the alloc module to the numa module so that NUMA related items live together.

No functional change.

---
 rust/kernel/alloc.rs           | 30 ++++--------------------------
 rust/kernel/alloc/allocator.rs |  6 +++---
 rust/kernel/alloc/kbox.rs      |  4 ++--
 rust/kernel/alloc/kvec.rs      |  4 ++--
 rust/kernel/numa.rs            | 35 ++++++++++++++++++++++++++++++++++-
 5 files changed, 45 insertions(+), 34 deletions(-)
rust/kernel/alloc.rs +4 -26
index 21067bde6860..9daddf540e7d 100644
@@ -22,7 +22,10 @@
 #[derive(Copy, Clone, PartialEq, Eq, Debug)]
 pub struct AllocError;
 
-use crate::prelude::*;
+use crate::{
+    numa::NumaNode,
+    prelude::*, //
+};
 
 use core::{
     alloc::Layout,
@@ -114,31 +117,6 @@ pub mod flags {
     pub const __GFP_NOWARN: Flags = Flags(bindings::__GFP_NOWARN);
 }
 
-/// Non Uniform Memory Access (NUMA) node identifier.
-#[derive(Clone, Copy, PartialEq)]
-pub struct NumaNode(i32);
-
-impl NumaNode {
-    /// Create a new NUMA node identifier (non-negative integer).
-    ///
-    /// Returns [`EINVAL`] if a negative id or an id exceeding [`bindings::MAX_NUMNODES`] is
-    /// specified.
-    pub fn new(node: i32) -> Result<Self> {
-        // MAX_NUMNODES never exceeds 2**10 because NODES_SHIFT is 0..10.
-        if node < 0 || node >= bindings::MAX_NUMNODES as i32 {
-            return Err(EINVAL);
-        }
-        Ok(Self(node))
-    }
-}
-
-/// Specify necessary constant to pass the information to Allocator that the caller doesn't care
-/// about the NUMA node to allocate memory from.
-impl NumaNode {
-    /// No node preference.
-    pub const NO_NODE: NumaNode = NumaNode(bindings::NUMA_NO_NODE);
-}
-
 /// The kernel's [`Allocator`] trait.
 ///
 /// An implementation of [`Allocator`] can allocate, re-allocate and free memory buffers described
rust/kernel/alloc/allocator.rs +3 -3
index cd4203f27aed..4af50e20933b 100644
@@ -11,12 +11,12 @@
 use super::{
     AllocError,
     Allocator,
-    Flags,
-    NumaNode, //
+    Flags, //
 };
 
 use crate::{
     bindings,
+    numa::NumaNode,
     page, //
 };
 
@@ -122,7 +122,7 @@ unsafe fn call(
         // - Those functions provide the guarantees of this function.
         let raw_ptr = unsafe {
             // If `size == 0` and `ptr != NULL` the memory behind the pointer is freed.
-            self.0(ptr.cast(), size, layout.align(), flags.0, nid.0).cast()
+            self.0(ptr.cast(), size, layout.align(), flags.0, nid.as_raw()).cast()
         };
 
         let ptr = if size == 0 {
rust/kernel/alloc/kbox.rs +2 -2
index c63d6acdbb6f..0fa736c81287 100644
@@ -13,12 +13,12 @@
 use super::{
     AllocError,
     Allocator,
-    Flags,
-    NumaNode, //
+    Flags, //
 };
 
 use crate::{
     fmt,
+    numa::NumaNode,
     page::AsPageIter,
     prelude::*,
     types::ForeignOwnable, //
rust/kernel/alloc/kvec.rs +2 -2
index c7546b9da4fa..b669ca10b3f7 100644
@@ -14,12 +14,12 @@
     AllocError,
     Allocator,
     Box,
-    Flags,
-    NumaNode, //
+    Flags, //
 };
 
 use crate::{
     fmt,
+    numa::NumaNode,
     page::{
         AsPageIter,
         PAGE_SIZE, //
rust/kernel/numa.rs +4 -1
index 164e622d325f..4a3a880308f3 100644
@@ -4,12 +4,15 @@
 //!
 //! C header: [`include/linux/nodemask.h`](srctree/include/linux/nodemask.h)
 
-use crate::bindings;
 #[cfg(CONFIG_NUMA)]
 use crate::sync::atomic::{
     Atomic,
     Relaxed, //
 };
+use crate::{
+    bindings,
+    prelude::*, //
+};
 
Quoted 14 lines

rust/kernel/numa.rs pub fn num_online_nodes() -> u32 { · line 36

Quoted 10 lines
rust/kernel/numa.rs +4 -0
 /// Returns the number of online NUMA nodes.
 #[inline]
@@ -27,3 +30,33 @@ pub fn num_online_nodes() -> u32 {
         bindings::nr_online_nodes
     }
 }
+
+/// Non Uniform Memory Access (NUMA) node identifier.
+#[derive(Clone, Copy, PartialEq)]
+pub struct NumaNode(i32);

FWIW the C type is nodemask_t and this should have mask in the name too. It is not an identifier. That said, this is an existing issue, so shouldn't block the move.

What makes you say this? This is not a mask, it is a numa node id, which is represented as i32. NUMA_NO_NODE is a valid id.

I am using this to specify domain for block mq queues. If you take a look at the allocation path:

blk_mq_alloc_queue
blk_alloc_queue
slab_alloc_node

It's an id, named numa_node, node_id, node. Not a mask. Unless I missed something?

Best regards,
Andreas Hindborg

reviewer
#25 lore raw

Re: [PATCH v3 3/3] rust: alloc: move `NumaNode` to the `numa` module

Quoted 42 lines · Andreas Hindborg
Quoted 19 lines · Gary Guo
Quoted 19 lines
rust/kernel/numa.rs +4 -1
index 164e622d325f..4a3a880308f3 100644
@@ -4,12 +4,15 @@
 //!
 //! C header: [`include/linux/nodemask.h`](srctree/include/linux/nodemask.h)
 
-use crate::bindings;
 #[cfg(CONFIG_NUMA)]
 use crate::sync::atomic::{
     Atomic,
     Relaxed, //
 };
+use crate::{
+    bindings,
+    prelude::*, //
+};
 
Quoted 14 lines

rust/kernel/numa.rs pub fn num_online_nodes() -> u32 { · line 36

Quoted 10 lines
rust/kernel/numa.rs +4 -0
 /// Returns the number of online NUMA nodes.
 #[inline]
@@ -27,3 +30,33 @@ pub fn num_online_nodes() -> u32 {
         bindings::nr_online_nodes
     }
 }
+
+/// Non Uniform Memory Access (NUMA) node identifier.
+#[derive(Clone, Copy, PartialEq)]
+pub struct NumaNode(i32);

FWIW the C type is nodemask_t and this should have mask in the name too. It is not an identifier. That said, this is an existing issue, so shouldn't block the move.

What makes you say this? This is not a mask, it is a numa node id, which is represented as i32. NUMA_NO_NODE is a valid id.

I am using this to specify domain for block mq queues. If you take a look at the allocation path:

Quoted 6 lines

blk_mq_alloc_queue
blk_alloc_queue
slab_alloc_node

It's an id, named numa_node, node_id, node. Not a mask. Unless I missed something?

Right, I got myself confused because NUMA_NO_NODE is defined in nodemask_types.h.

If it's just an ID, would it make more sense to have Option<NumaNode> and None to represent no node preference?

Best,
Gary

author reviewer
#26 lore raw

Re: [PATCH v3 3/3] rust: alloc: move `NumaNode` to the `numa` module

Quoted 47 lines · Gary Guo
Quoted 46 lines · Andreas Hindborg
Quoted 19 lines · Gary Guo
Quoted 19 lines
rust/kernel/numa.rs +4 -1
index 164e622d325f..4a3a880308f3 100644
@@ -4,12 +4,15 @@
 //!
 //! C header: [`include/linux/nodemask.h`](srctree/include/linux/nodemask.h)
 
-use crate::bindings;
 #[cfg(CONFIG_NUMA)]
 use crate::sync::atomic::{
     Atomic,
     Relaxed, //
 };
+use crate::{
+    bindings,
+    prelude::*, //
+};
 
Quoted 14 lines

rust/kernel/numa.rs pub fn num_online_nodes() -> u32 { · line 36

Quoted 10 lines
rust/kernel/numa.rs +4 -0
 /// Returns the number of online NUMA nodes.
 #[inline]
@@ -27,3 +30,33 @@ pub fn num_online_nodes() -> u32 {
         bindings::nr_online_nodes
     }
 }
+
+/// Non Uniform Memory Access (NUMA) node identifier.
+#[derive(Clone, Copy, PartialEq)]
+pub struct NumaNode(i32);

FWIW the C type is nodemask_t and this should have mask in the name too. It is not an identifier. That said, this is an existing issue, so shouldn't block the move.

What makes you say this? This is not a mask, it is a numa node id, which is represented as i32. NUMA_NO_NODE is a valid id.

I am using this to specify domain for block mq queues. If you take a look at the allocation path:

blk_mq_alloc_queue
blk_alloc_queue
slab_alloc_node

Quoted 8 lines
Quoted 2 lines

It's an id, named numa_node, node_id, node. Not a mask. Unless I missed something?

Right, I got myself confused because NUMA_NO_NODE is defined in nodemask_types.h.

If it's just an ID, would it make more sense to have Option<NumaNode> and None to represent no node preference?

I guess we could define it as Option<i32>. That would be nice.

I can send a follow up once this lands. FYI there is a v4 of this [1].

Best regards,
Andreas Hindborg

[1] https://lore.kernel.org/r/[email protected]

Reviewed-by on v3 3/3
#27 lore raw

Re: [PATCH v3 3/3] rust: alloc: move `NumaNode` to the `numa` module

In reply to Andreas Hindborg
Quoted 9 lines · Andreas Hindborg

NumaNode identifies a NUMA node and is not specific to the allocator API. Move it from the alloc module to the numa module so that NUMA related items live together.

No functional change.

Quoted 182 lines
---
 rust/kernel/alloc.rs           | 30 ++++--------------------------
 rust/kernel/alloc/allocator.rs |  6 +++---
 rust/kernel/alloc/kbox.rs      |  4 ++--
 rust/kernel/alloc/kvec.rs      |  4 ++--
 rust/kernel/numa.rs            | 35 ++++++++++++++++++++++++++++++++++-
 5 files changed, 45 insertions(+), 34 deletions(-)
rust/kernel/alloc.rs +4 -26
index 21067bde6860..9daddf540e7d 100644
@@ -22,7 +22,10 @@
 #[derive(Copy, Clone, PartialEq, Eq, Debug)]
 pub struct AllocError;
 
-use crate::prelude::*;
+use crate::{
+    numa::NumaNode,
+    prelude::*, //
+};
 
 use core::{
     alloc::Layout,
@@ -114,31 +117,6 @@ pub mod flags {
     pub const __GFP_NOWARN: Flags = Flags(bindings::__GFP_NOWARN);
 }
 
-/// Non Uniform Memory Access (NUMA) node identifier.
-#[derive(Clone, Copy, PartialEq)]
-pub struct NumaNode(i32);
-
-impl NumaNode {
-    /// Create a new NUMA node identifier (non-negative integer).
-    ///
-    /// Returns [`EINVAL`] if a negative id or an id exceeding [`bindings::MAX_NUMNODES`] is
-    /// specified.
-    pub fn new(node: i32) -> Result<Self> {
-        // MAX_NUMNODES never exceeds 2**10 because NODES_SHIFT is 0..10.
-        if node < 0 || node >= bindings::MAX_NUMNODES as i32 {
-            return Err(EINVAL);
-        }
-        Ok(Self(node))
-    }
-}
-
-/// Specify necessary constant to pass the information to Allocator that the caller doesn't care
-/// about the NUMA node to allocate memory from.
-impl NumaNode {
-    /// No node preference.
-    pub const NO_NODE: NumaNode = NumaNode(bindings::NUMA_NO_NODE);
-}
-
 /// The kernel's [`Allocator`] trait.
 ///
 /// An implementation of [`Allocator`] can allocate, re-allocate and free memory buffers described
rust/kernel/alloc/allocator.rs +3 -3
index cd4203f27aed..4af50e20933b 100644
@@ -11,12 +11,12 @@
 use super::{
     AllocError,
     Allocator,
-    Flags,
-    NumaNode, //
+    Flags, //
 };
 
 use crate::{
     bindings,
+    numa::NumaNode,
     page, //
 };
 
@@ -122,7 +122,7 @@ unsafe fn call(
         // - Those functions provide the guarantees of this function.
         let raw_ptr = unsafe {
             // If `size == 0` and `ptr != NULL` the memory behind the pointer is freed.
-            self.0(ptr.cast(), size, layout.align(), flags.0, nid.0).cast()
+            self.0(ptr.cast(), size, layout.align(), flags.0, nid.as_raw()).cast()
         };
 
         let ptr = if size == 0 {
rust/kernel/alloc/kbox.rs +2 -2
index c63d6acdbb6f..0fa736c81287 100644
@@ -13,12 +13,12 @@
 use super::{
     AllocError,
     Allocator,
-    Flags,
-    NumaNode, //
+    Flags, //
 };
 
 use crate::{
     fmt,
+    numa::NumaNode,
     page::AsPageIter,
     prelude::*,
     types::ForeignOwnable, //
rust/kernel/alloc/kvec.rs +2 -2
index c7546b9da4fa..b669ca10b3f7 100644
@@ -14,12 +14,12 @@
     AllocError,
     Allocator,
     Box,
-    Flags,
-    NumaNode, //
+    Flags, //
 };
 
 use crate::{
     fmt,
+    numa::NumaNode,
     page::{
         AsPageIter,
         PAGE_SIZE, //
rust/kernel/numa.rs +34 -1
index 164e622d325f..4a3a880308f3 100644
@@ -4,12 +4,15 @@
 //!
 //! C header: [`include/linux/nodemask.h`](srctree/include/linux/nodemask.h)
 
-use crate::bindings;
 #[cfg(CONFIG_NUMA)]
 use crate::sync::atomic::{
     Atomic,
     Relaxed, //
 };
+use crate::{
+    bindings,
+    prelude::*, //
+};
 
 /// Returns the number of online NUMA nodes.
 #[inline]
@@ -27,3 +30,33 @@ pub fn num_online_nodes() -> u32 {
         bindings::nr_online_nodes
     }
 }
+
+/// Non Uniform Memory Access (NUMA) node identifier.
+#[derive(Clone, Copy, PartialEq)]
+pub struct NumaNode(i32);
+
+impl NumaNode {
+    /// Create a new NUMA node identifier (non-negative integer).
+    ///
+    /// Returns [`EINVAL`] if a negative id or an id exceeding [`bindings::MAX_NUMNODES`] is
+    /// specified.
+    pub fn new(node: i32) -> Result<Self> {
+        // MAX_NUMNODES never exceeds 2**10 because NODES_SHIFT is 0..10.
+        if node < 0 || node >= bindings::MAX_NUMNODES as i32 {
+            return Err(EINVAL);
+        }
+        Ok(Self(node))
+    }
+
+    /// Get the raw representation of this node identifier.
+    pub(crate) fn as_raw(self) -> i32 {
+        self.0
+    }
+}
+
+/// Specify necessary constant to pass the information to Allocator that the caller doesn't care
+/// about the NUMA node to allocate memory from.
+impl NumaNode {
+    /// No node preference.
+    pub const NO_NODE: NumaNode = NumaNode(bindings::NUMA_NO_NODE);
+}
Signature · 2 lines
-- 
2.54.0