-
Notifications
You must be signed in to change notification settings - Fork 18
feat: axtask set active cpu num #77
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change | ||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -1,5 +1,7 @@ | ||||||||||||||||||
| //! Task APIs for multi-task configuration. | ||||||||||||||||||
|
|
||||||||||||||||||
| use core::sync::atomic::AtomicUsize; | ||||||||||||||||||
|
|
||||||||||||||||||
| use alloc::{string::String, sync::Arc}; | ||||||||||||||||||
|
|
||||||||||||||||||
| use kernel_guard::NoPreemptIrqSave; | ||||||||||||||||||
|
|
@@ -19,6 +21,8 @@ pub type AxTaskRef = Arc<AxTask>; | |||||||||||||||||
| /// The wrapper type for [`cpumask::CpuMask`] with SMP configuration. | ||||||||||||||||||
| pub type AxCpuMask = cpumask::CpuMask<{ axconfig::plat::CPU_NUM }>; | ||||||||||||||||||
|
|
||||||||||||||||||
| static CPU_NUM: AtomicUsize = AtomicUsize::new(1); | ||||||||||||||||||
|
|
||||||||||||||||||
| cfg_if::cfg_if! { | ||||||||||||||||||
| if #[cfg(feature = "sched-rr")] { | ||||||||||||||||||
| const MAX_TIME_SLICE: usize = 5; | ||||||||||||||||||
|
|
@@ -70,15 +74,24 @@ pub fn current() -> CurrentTask { | |||||||||||||||||
|
|
||||||||||||||||||
| /// Initializes the task scheduler (for the primary CPU). | ||||||||||||||||||
| pub fn init_scheduler() { | ||||||||||||||||||
| info!("Initialize scheduling..."); | ||||||||||||||||||
| init_scheduler_with_cpu_num(axconfig::plat::CPU_NUM); | ||||||||||||||||||
| } | ||||||||||||||||||
|
|
||||||||||||||||||
| /// Initializes the task scheduler with cpu_num (for the primary CPU). | ||||||||||||||||||
| pub fn init_scheduler_with_cpu_num(cpu_num: usize) { | ||||||||||||||||||
|
||||||||||||||||||
| pub fn init_scheduler_with_cpu_num(cpu_num: usize) { | |
| pub fn init_scheduler_with_cpu_num(cpu_num: usize) { | |
| assert!( | |
| cpu_num > 0 && cpu_num <= axconfig::plat::CPU_NUM, | |
| "cpu_num must be in range [1, {}], got {}", | |
| axconfig::plat::CPU_NUM, | |
| cpu_num | |
| ); |
Copilot
AI
Nov 27, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The memory ordering used for storing and loading CPU_NUM should be Ordering::Release and Ordering::Acquire respectively (or stronger), not Ordering::Relaxed.
Currently, Relaxed ordering provides no synchronization guarantees. When init_scheduler_with_cpu_num stores the CPU count, tasks created immediately after in crate::run_queue::init() (line 84) call active_cpu_num() to read this value. Without proper ordering, there's no guarantee the write is visible to these reads, potentially causing tasks to be initialized with an incorrect CPU mask.
Recommended fix:
CPU_NUM.store(cpu_num, core::sync::atomic::Ordering::Release);and
CPU_NUM.load(core::sync::atomic::Ordering::Acquire)
Copilot
AI
Nov 27, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The new public API function init_scheduler_with_cpu_num lacks test coverage. Given that the module has comprehensive tests in tests.rs for other scheduler functionality, this new function should also be tested.
Consider adding test cases that verify:
- Tasks created after calling
init_scheduler_with_cpu_numhave the correct CPU mask (matching the specified cpu_num) - The function properly handles edge cases (cpu_num = 1, cpu_num = max)
- Invalid inputs are properly rejected (if validation is added)
Copilot
AI
Nov 27, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The memory ordering should be Ordering::Acquire to properly synchronize with the Release store in init_scheduler_with_cpu_num.
Using Relaxed ordering provides no guarantees that the value written by the primary CPU during initialization will be visible to secondary CPUs or to code reading this value later. This could lead to tasks being created with incorrect CPU masks.
Recommended fix:
CPU_NUM.load(core::sync::atomic::Ordering::Acquire)| CPU_NUM.load(core::sync::atomic::Ordering::Relaxed) | |
| CPU_NUM.load(core::sync::atomic::Ordering::Acquire) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The new public API function
init_scheduler_with_cpu_numis missing documentation. Public APIs should have doc comments explaining their purpose, parameters, and any safety considerations.Recommended addition: