Skip to content

cpumask: add a function like for_each_online_cpu #728

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: rust
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
201 changes: 201 additions & 0 deletions rust/kernel/cpumask.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,201 @@
// SPDX-License-Identifier: GPL-2.0

//! Cpumask variables and related functions.
//!
//! C header: [`include/linux/cpumask.h`](../../../../include/linux/cpumask.h).

use crate::bindings;
use core::iter::Iterator;

/// A valid CPU index.
///
/// # Safety
///
/// - The 'ValidCpuIndex' should be used during the iteration of a CPU index iterator.
pub struct ValidCpuIndex(u32);

impl ValidCpuIndex {
/// Get the valid CPU index in u32.
pub fn get(&self) -> u32 {
self.0
}
}

/// An possible CPU index iterator.
///
/// This iterator has a similar abilitiy to the kernel's macro `for_each_possible_cpu`.
pub struct PossibleCpusIndexIter {
index: i32,
}

/// An online CPU index iterator.
///
/// This iterator has a similar abilitiy to the kernel's macro `for_each_online_cpu`.
pub struct OnlineCpusIndexIter {
index: i32,
}

/// An present CPU index iterator.
///
/// This iterator has a similar abilitiy to the kernel's macro `for_each_present_cpu`.
pub struct PresentCpusIndexIter {
index: i32,
}

impl Iterator for PossibleCpusIndexIter {
type Item = ValidCpuIndex;

fn next(&mut self) -> Option<ValidCpuIndex> {
let next_cpu_id =
// SAFETY: Since [`bindings::__cpu_possible_mask`] will not change, there will not
// be data race in this part. When the last valid CPU index is found, this iterator
// will return `None`. Therefore, the index parameter is always valid.
unsafe { bindings::cpumask_next(self.index, &bindings::__cpu_possible_mask) };
// When [`bindings::cpumask_next`] can not find further CPUs set in the
// [`bindings::__cpu_possible_mask`], it returns a value >= [`bindings::nr_cpu_ids`].
//
// SAFETY: The [`bindings::nr_cpu_ids`] is fixed at the boot time.
if next_cpu_id >= unsafe { bindings::nr_cpu_ids } {
return None;
}
self.index = next_cpu_id as i32;
Some(ValidCpuIndex(next_cpu_id))
}
}

impl Iterator for OnlineCpusIndexIter {
type Item = ValidCpuIndex;

fn next(&mut self) -> Option<ValidCpuIndex> {
#[cfg(CONFIG_HOTPLUG_CPU)]
if self.index == -1 {
// The [`bindings::__cpu_online_mask`] and [`bindings::nr_cpu_ids`] may chanage if
// `CONFIG_HOTPLUG_CPU` is enabled. In case of race condition, a lock is needed
// here. If `CONFIG_HOTPLUG_CPU` is disabled, this function will not have any cost.
//
// SAFETY: FFI call, this is called once during iteration in case of dead lock.
unsafe { bindings::cpus_read_lock() };
}
let next_cpu_id =
// SAFETY: The [`bindings::cpus_read_lock`] prevents the data race. When the last
// valid CPU index is found, this iterator will return `None`. Therefore, the
// index parameter is always valid.
unsafe { bindings::cpumask_next(self.index, &bindings::__cpu_online_mask) };
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing a couple // SAFETY comments.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems that these two unsafe blocks may have some problems. I noticebindings::__cpu_online_mask and bindings::nr_cpu_ids are static mut variables. They can change during the iteration due to the hotplug. They are only safe when CONFIG_HOTPLUG_CPU is n. I'm trying to fix this bug.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The missing // SAFETY comments are added in this version. Using lock to address the data race problem.

// When [`bindings::cpumask_next`] can not find further CPUs set in the
// [`bindings::__cpu_online_mask`], it returns a value >= [`bindings::nr_cpu_ids`].
//
// SAFETY: The [`bindings::nr_cpu_ids`] is fixed at the boot time.
if next_cpu_id >= unsafe { bindings::nr_cpu_ids } {
// Unlock after finishing iteration.
//
// SAFETY: FFI call.
#[cfg(CONFIG_HOTPLUG_CPU)]
unsafe {
bindings::cpus_read_unlock()
};
return None;
}
self.index = next_cpu_id as i32;
Some(ValidCpuIndex(next_cpu_id))
}
}

impl Iterator for PresentCpusIndexIter {
type Item = ValidCpuIndex;

fn next(&mut self) -> Option<ValidCpuIndex> {
#[cfg(CONFIG_HOTPLUG_CPU)]
if self.index == -1 {
// The [`bindings::__cpu_present_mask`] and [`bindings::nr_cpu_ids`] may chanage
// if `CONFIG_HOTPLUG_CPU` is enabled. In case of race condition, a lock is needed
// here. If `CONFIG_HOTPLUG_CPU` is disabled, this function will not have any cost.
//
// SAFETY: FFI call, this is called once during iteration in case of dead lock.
unsafe { bindings::cpus_read_lock() };
}
let next_cpu_id =
// SAFETY: The [`bindings::cpus_read_lock`] prevents the data race. When the last
// valid CPU index is found, this iterator will return `None`. Therefore, the
// index parameter is always valid.
unsafe { bindings::cpumask_next(self.index, &bindings::__cpu_present_mask) };
// When [`bindings::cpumask_next`] can not find further CPUs set in the
// [`bindings::__cpu_present_mask`], it returns a value >= [`bindings::nr_cpu_ids`].
//
// SAFETY: The [`bindings::nr_cpu_ids`] is fixed at the boot time.
if next_cpu_id >= unsafe { bindings::nr_cpu_ids } {
// Unlock after finishing iteration.
//
// SAFETY: FFI call.
#[cfg(CONFIG_HOTPLUG_CPU)]
unsafe {
bindings::cpus_read_unlock()
};
return None;
}
self.index = next_cpu_id as i32;
Some(ValidCpuIndex(next_cpu_id))
}
}

/// Returns a [`PossibleCpusIndexIter`] that gives the possible CPU indexes.
///
/// # Examples
///
/// ```
/// # use kernel::prelude::*;
/// # use kernel::cpumask::possible_cpus;
///
/// fn example() {
/// // This prints all the possible cpu indexes.
/// for cpu in possible_cpus(){
/// pr_info!("{}\n", cpu.get());
/// }
/// }
/// ```
pub fn possible_cpus() -> PossibleCpusIndexIter {
// Initial index is set to -1. Since [`bindings::cpumask_next`] return the next set bit in a
// [`bindings::__cpu_possible_mask`], the CPU index should begins from 0.
PossibleCpusIndexIter { index: -1 }
}

/// Returns a [`OnlineCpusIndexIter`] that gives the online CPU indexes.
///
/// # Examples
///
/// ```
/// # use kernel::prelude::*;
/// # use kernel::cpumask::online_cpus;
///
/// fn example() {
/// // This prints all the online cpu indexes.
/// for cpu in online_cpus(){
/// pr_info!("{}\n", cpu.get());
/// }
/// }
/// ```
pub fn online_cpus() -> OnlineCpusIndexIter {
// Initial index is set to -1. Since [`bindings::cpumask_next`] return the next set bit in a
// [`bindings::__cpu_online_mask`], the CPU index should begins from 0.
OnlineCpusIndexIter { index: -1 }
}

/// Returns a [`PresentCpusIndexIter`] that gives the present CPU indexes.
///
/// # Examples
///
/// ```
/// # use kernel::prelude::*;
/// # use kernel::cpumask::present_cpus;
///
/// fn example() {
/// // This prints all the present cpu indexes.
/// for cpu in present_cpus(){
/// pr_info!("{}\n", cpu.get());
/// }
/// }
/// ```
pub fn present_cpus() -> PresentCpusIndexIter {
// Initial index is set to -1. Since [`bindings::cpumask_next`] return the next set bit in a
// [`bindings::__cpu_present_mask`], the CPU index should begins from 0.
PresentCpusIndexIter { index: -1 }
}
2 changes: 2 additions & 0 deletions rust/kernel/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,8 @@ pub mod user_ptr;
#[cfg(CONFIG_KUNIT)]
pub mod kunit;

pub mod cpumask;

#[doc(hidden)]
pub use build_error::build_error;

Expand Down