-
Notifications
You must be signed in to change notification settings - Fork 435
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
cpumask: add a function like for_each_online_cpu #728
base: rust
Are you sure you want to change the base?
Conversation
The detail of this PR can be seen in #727. |
(missing Signed-Off-By) |
Thanks for reminding me:) |
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.
Some general notes:
- There should be an example of usage (since this depends on a running kernel, wrap the code inside a
fn
-- please grep for# Example
to see how it is done elsewhere). - Please use Markdown in documentation and links wherever possible.
- Should we add other iterators, not just online ones?
- More importantly, how will the index be used by users of the iterator? Should we have higher-level iterators/abstractions over the "final data" (e.g. per CPU data) or at least wrap the index in a type that is known to be a valid index?
|
||
fn next(&mut self) -> Option<u32> { | ||
let next_cpu_id = | ||
unsafe { bindings::cpumask_next(self.index, &bindings::__cpu_online_mask) }; |
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.
Missing a couple // SAFETY
comments.
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.
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.
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 missing // SAFETY comments are added in this version. Using lock to address the data race problem.
882810d
to
3ce79cd
Compare
I just add an example for this function.
In the updated version, rust items in the documentation are properly linked.
Yes, I will add other similar iterators soon that are
In the kernel, the CPU index is an int number and is commonly used in macros like |
I have added these two functions in this version. |
What I meant is that the iterator will return a valid index, but it will be a plain integer. Thus we lose the fact that it is valid in the type system. For instance, it may be the case that the user has to pass that index to something else in order to do something useful with it -- and then the receiver will not know it is a valid index, so it may have to check again (or be This is why I mentioned it would be better to have a complete use case or example, which will show how the index will be used and whether it is worth wrapping the index into its own type or not. Printing the index is fine as a simple example, but it does not show how it will be used. Thanks for working on the PR, by the way! |
Thanks for the detailed explanation! I get your point now. It's exciting to work in this community. I receive much fun and knowledge during this coding phase. :D |
0ebac55
to
f232e20
Compare
342a7c9
to
6bc6732
Compare
Sorry for the delay. I'm interrupted by something else. After some struggling, I'm back again:) The previously left unsolved problems are:
These two problems are addressed as follows respectively:
|
This commit add a function return a struct implementing Iterator trait. Thus, this struct can be used as an iterator that returns an online cpu index each time. Signed-off-by: Li Hongyu <lihongyu1999@bupt.edu.cn>
Hi, @ojeda. I just rebase this PR on the lasted commit. BTW, Is there anything I can do to advance this PR. |
This commit add a function return a struct implementing Iterator trait. Thus, this struct can be used as an iterator that returns an online cpu index each time.
Signed-off-by: Hongyu Li lihongyu1999@bupt.edu.cn