From: Viresh Kumar <viresh.kumar@linaro.org>
To: "Rafael J. Wysocki" <rafael@kernel.org>,
"Miguel Ojeda" <miguel.ojeda.sandonis@gmail.com>,
"Danilo Krummrich" <dakr@redhat.com>,
"Yury Norov" <yury.norov@gmail.com>,
"Rasmus Villemoes" <linux@rasmusvillemoes.dk>,
"Miguel Ojeda" <ojeda@kernel.org>,
"Alex Gaynor" <alex.gaynor@gmail.com>,
"Boqun Feng" <boqun.feng@gmail.com>,
"Gary Guo" <gary@garyguo.net>,
"Björn Roy Baron" <bjorn3_gh@protonmail.com>,
"Benno Lossin" <benno.lossin@proton.me>,
"Andreas Hindborg" <a.hindborg@kernel.org>,
"Alice Ryhl" <aliceryhl@google.com>,
"Trevor Gross" <tmgross@umich.edu>
Cc: "Viresh Kumar" <viresh.kumar@linaro.org>,
linux-pm@vger.kernel.org,
"Vincent Guittot" <vincent.guittot@linaro.org>,
"Stephen Boyd" <sboyd@kernel.org>, "Nishanth Menon" <nm@ti.com>,
rust-for-linux@vger.kernel.org,
"Manos Pitsidianakis" <manos.pitsidianakis@linaro.org>,
"Erik Schilling" <erik.schilling@linaro.org>,
"Alex Bennée" <alex.bennee@linaro.org>,
"Joakim Bech" <joakim.bech@linaro.org>,
"Rob Herring" <robh@kernel.org>,
linux-kernel@vger.kernel.org
Subject: [PATCH V8 05/14] rust: Add bindings for cpumask
Date: Thu, 6 Feb 2025 14:58:26 +0530 [thread overview]
Message-ID: <f1f96961c1d32f73d9316fd297c1b184fa73d9b5.1738832118.git.viresh.kumar@linaro.org> (raw)
In-Reply-To: <cover.1738832118.git.viresh.kumar@linaro.org>
This patch adds basic Rust bindings for struct cpumask.
These will be used by Rust based cpufreq / OPP core.
Signed-off-by: Viresh Kumar <viresh.kumar@linaro.org>
---
MAINTAINERS | 1 +
rust/kernel/cpumask.rs | 138 +++++++++++++++++++++++++++++++++++++++++
rust/kernel/lib.rs | 1 +
3 files changed, 140 insertions(+)
create mode 100644 rust/kernel/cpumask.rs
diff --git a/MAINTAINERS b/MAINTAINERS
index bfc1bf2ebd77..ff4511914e0a 100644
--- a/MAINTAINERS
+++ b/MAINTAINERS
@@ -4022,6 +4022,7 @@ F: lib/find_bit.c
F: lib/find_bit_benchmark.c
F: lib/test_bitmap.c
F: rust/helpers/cpumask.c
+F: rust/kernel/cpumask.rs
F: tools/include/linux/bitfield.h
F: tools/include/linux/bitmap.h
F: tools/include/linux/bits.h
diff --git a/rust/kernel/cpumask.rs b/rust/kernel/cpumask.rs
new file mode 100644
index 000000000000..b0be8c75a2c2
--- /dev/null
+++ b/rust/kernel/cpumask.rs
@@ -0,0 +1,138 @@
+// SPDX-License-Identifier: GPL-2.0
+
+//! CPU mask abstractions.
+//!
+//! C header: [`include/linux/cpumask.h`](srctree/include/linux/cpumask.h)
+
+use crate::{bindings, error::Result, prelude::ENOMEM};
+
+#[cfg(not(CONFIG_CPUMASK_OFFSTACK))]
+use crate::prelude::{KBox, GFP_KERNEL};
+
+#[cfg(CONFIG_CPUMASK_OFFSTACK)]
+use core::ptr;
+
+/// A simple implementation of `struct cpumask` from the C code.
+pub struct Cpumask {
+ ptr: *mut bindings::cpumask,
+ owned: bool,
+}
+
+impl Cpumask {
+ /// Creates empty cpumask.
+ #[cfg(CONFIG_CPUMASK_OFFSTACK)]
+ pub fn new() -> Result<Self> {
+ let mut ptr: *mut bindings::cpumask = ptr::null_mut();
+
+ // SAFETY: Depending on the value of `gfp_flags`, this call may sleep. Other than that, it
+ // is always safe to call this method.
+ if !unsafe { bindings::zalloc_cpumask_var(&mut ptr, bindings::GFP_KERNEL) } {
+ return Err(ENOMEM);
+ }
+
+ Ok(Self { ptr, owned: true })
+ }
+
+ /// Creates empty cpumask.
+ #[cfg(not(CONFIG_CPUMASK_OFFSTACK))]
+ pub fn new() -> Result<Self> {
+ let ptr = KBox::into_raw(KBox::new([bindings::cpumask::default(); 1], GFP_KERNEL)?);
+
+ // SAFETY: Depending on the value of `gfp_flags`, this call may sleep. Other than that, it
+ // is always safe to call this method.
+ if !unsafe { bindings::zalloc_cpumask_var(ptr, bindings::GFP_KERNEL) } {
+ return Err(ENOMEM);
+ }
+
+ Ok(Self {
+ ptr: ptr as *mut _,
+ owned: true,
+ })
+ }
+
+ /// Creates a new abstraction instance of an existing `struct cpumask` pointer.
+ ///
+ /// # Safety
+ ///
+ /// Callers must ensure that `ptr` is valid, and non-null.
+ #[cfg(CONFIG_CPUMASK_OFFSTACK)]
+ pub unsafe fn get_cpumask(ptr: &mut *mut bindings::cpumask) -> Self {
+ Self {
+ ptr: *ptr,
+ owned: false,
+ }
+ }
+
+ /// Creates a new abstraction instance of an existing `struct cpumask` pointer.
+ ///
+ /// # Safety
+ ///
+ /// Callers must ensure that `ptr` is valid, and non-null.
+ #[cfg(not(CONFIG_CPUMASK_OFFSTACK))]
+ pub unsafe fn get_cpumask(ptr: &mut bindings::cpumask_var_t) -> Self {
+ Self {
+ ptr: ptr as *mut _,
+ owned: false,
+ }
+ }
+
+ /// Obtain the raw `struct cpumask *`.
+ pub fn as_raw(&mut self) -> *mut bindings::cpumask {
+ self.ptr
+ }
+
+ /// Sets CPU in the cpumask.
+ ///
+ /// Update the cpumask with a single CPU.
+ pub fn set(&mut self, cpu: u32) {
+ // SAFETY: `ptr` is guaranteed to be valid for the lifetime of `self`. And it is safe to
+ // call `cpumask_set_cpus()` for any CPU.
+ unsafe { bindings::cpumask_set_cpu(cpu, self.ptr) };
+ }
+
+ /// Clears CPU in the cpumask.
+ ///
+ /// Update the cpumask with a single CPU.
+ pub fn clear(&mut self, cpu: i32) {
+ // SAFETY: `ptr` is guaranteed to be valid for the lifetime of `self`. And it is safe to
+ // call `cpumask_clear_cpu()` for any CPU.
+ unsafe { bindings::cpumask_clear_cpu(cpu, self.ptr) };
+ }
+
+ /// Sets all CPUs in the cpumask.
+ pub fn set_all(&mut self) {
+ // SAFETY: `ptr` is guaranteed to be valid for the lifetime of `self`. And it is safe to
+ // call `cpumask_setall()`.
+ unsafe { bindings::cpumask_setall(self.ptr) };
+ }
+
+ /// Gets weight of a cpumask.
+ pub fn weight(&self) -> u32 {
+ // SAFETY: `ptr` is guaranteed to be valid for the lifetime of `self`. And it is safe to
+ // call `cpumask_weight()`.
+ unsafe { bindings::cpumask_weight(self.ptr) }
+ }
+
+ /// Copies cpumask.
+ pub fn copy(&self, dstp: &mut Self) {
+ // SAFETY: `ptr` is guaranteed to be valid for the lifetime of `self`. And it is safe to
+ // call `cpumask_copy()`.
+ unsafe { bindings::cpumask_copy(dstp.as_raw(), self.ptr) };
+ }
+}
+
+impl Drop for Cpumask {
+ fn drop(&mut self) {
+ if self.owned {
+ // SAFETY: `ptr` is guaranteed to be valid for the lifetime of `self`. And it is safe
+ // to call `free_cpumask_var()`.
+ unsafe { bindings::free_cpumask_var(self.ptr) }
+
+ #[cfg(not(CONFIG_CPUMASK_OFFSTACK))]
+ // SAFETY: The pointer was earlier initialized from the result of `KBox::into_raw()`.
+ unsafe {
+ drop(KBox::from_raw(self.ptr))
+ };
+ }
+ }
+}
diff --git a/rust/kernel/lib.rs b/rust/kernel/lib.rs
index 415c500212dd..ccbf7fa087a0 100644
--- a/rust/kernel/lib.rs
+++ b/rust/kernel/lib.rs
@@ -41,6 +41,7 @@
#[doc(hidden)]
pub mod build_assert;
pub mod cpu;
+pub mod cpumask;
pub mod cred;
pub mod device;
pub mod device_id;
--
2.31.1.272.g89b43f80a514
next prev parent reply other threads:[~2025-02-06 9:29 UTC|newest]
Thread overview: 53+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-02-06 9:28 [PATCH V8 00/14] Rust bindings for cpufreq and OPP core + sample driver Viresh Kumar
2025-02-06 9:28 ` [PATCH V8 01/14] rust: macros: enable use of hyphens in module names Viresh Kumar
2025-02-06 9:28 ` [PATCH V8 02/14] cpufreq: Use enum for cpufreq flags that use BIT() Viresh Kumar
2025-02-06 9:28 ` [PATCH V8 03/14] rust: cpu: Add from_cpu() Viresh Kumar
2025-02-06 9:28 ` [PATCH V8 04/14] rust: Add cpumask helpers Viresh Kumar
2025-02-11 0:02 ` Yury Norov
2025-02-11 4:29 ` Viresh Kumar
2025-02-11 16:24 ` Yury Norov
2025-02-11 16:49 ` Jason Gunthorpe
2025-02-11 17:27 ` Danilo Krummrich
2025-02-11 21:37 ` Miguel Ojeda
2025-02-14 2:20 ` Yury Norov
2025-02-14 3:36 ` Viresh Kumar
2025-02-14 17:56 ` Miguel Ojeda
2025-02-14 19:11 ` Jason Gunthorpe
2025-02-14 20:24 ` Miguel Ojeda
2025-02-14 21:06 ` Jason Gunthorpe
2025-02-14 22:36 ` Miguel Ojeda
2025-02-15 9:55 ` Andreas Hindborg
2025-02-17 9:45 ` Philipp Stanner
2025-02-14 20:58 ` Miguel Ojeda
2025-02-12 7:34 ` Viresh Kumar
2025-02-15 10:16 ` Andreas Hindborg
2025-02-06 9:28 ` Viresh Kumar [this message]
2025-02-06 9:28 ` [PATCH V8 06/14] rust: Add bare minimal bindings for clk framework Viresh Kumar
2025-02-06 11:49 ` Danilo Krummrich
2025-02-06 11:52 ` Danilo Krummrich
2025-02-06 20:05 ` Stephen Boyd
2025-02-06 23:11 ` Danilo Krummrich
2025-02-07 9:24 ` Viresh Kumar
2025-02-07 10:43 ` Viresh Kumar
2025-02-07 17:19 ` Danilo Krummrich
2025-02-10 8:06 ` Viresh Kumar
2025-02-10 22:07 ` Stephen Boyd
2025-02-17 12:19 ` Daniel Almeida
2025-02-21 6:35 ` Viresh Kumar
2025-02-06 9:28 ` [PATCH V8 07/14] rust: Add initial bindings for OPP framework Viresh Kumar
2025-02-06 9:28 ` [PATCH V8 08/14] rust: Extend OPP bindings for the OPP table Viresh Kumar
2025-02-06 9:28 ` [PATCH V8 09/14] rust: Extend OPP bindings for the configuration options Viresh Kumar
2025-02-06 9:28 ` [PATCH V8 10/14] rust: Add initial bindings for cpufreq framework Viresh Kumar
2025-02-06 9:28 ` [PATCH V8 11/14] rust: Extend cpufreq bindings for policy and driver ops Viresh Kumar
2025-02-06 9:28 ` [PATCH V8 12/14] rust: Extend cpufreq bindings for driver registration Viresh Kumar
2025-02-06 12:04 ` Danilo Krummrich
2025-02-06 12:06 ` Alice Ryhl
2025-02-07 9:15 ` Viresh Kumar
2025-02-06 9:28 ` [PATCH V8 13/14] rust: Extend OPP bindings with CPU frequency table Viresh Kumar
2025-02-06 9:28 ` [PATCH V8 14/14] cpufreq: Add Rust based cpufreq-dt driver Viresh Kumar
2025-02-06 11:45 ` [PATCH V8 00/14] Rust bindings for cpufreq and OPP core + sample driver Danilo Krummrich
2025-02-07 7:15 ` Viresh Kumar
2025-02-07 11:07 ` Miguel Ojeda
2025-02-10 8:06 ` Viresh Kumar
2025-02-17 8:39 ` Miguel Ojeda
2025-02-17 10:18 ` Viresh Kumar
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=f1f96961c1d32f73d9316fd297c1b184fa73d9b5.1738832118.git.viresh.kumar@linaro.org \
--to=viresh.kumar@linaro.org \
--cc=a.hindborg@kernel.org \
--cc=alex.bennee@linaro.org \
--cc=alex.gaynor@gmail.com \
--cc=aliceryhl@google.com \
--cc=benno.lossin@proton.me \
--cc=bjorn3_gh@protonmail.com \
--cc=boqun.feng@gmail.com \
--cc=dakr@redhat.com \
--cc=erik.schilling@linaro.org \
--cc=gary@garyguo.net \
--cc=joakim.bech@linaro.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-pm@vger.kernel.org \
--cc=linux@rasmusvillemoes.dk \
--cc=manos.pitsidianakis@linaro.org \
--cc=miguel.ojeda.sandonis@gmail.com \
--cc=nm@ti.com \
--cc=ojeda@kernel.org \
--cc=rafael@kernel.org \
--cc=robh@kernel.org \
--cc=rust-for-linux@vger.kernel.org \
--cc=sboyd@kernel.org \
--cc=tmgross@umich.edu \
--cc=vincent.guittot@linaro.org \
--cc=yury.norov@gmail.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).