linux kernel, how to loop through all cpus?
c, kernel, linux-kernel
Solution
Back in the 2.6.26 kernel, `for_each_cpu()` was called `for_each_cpu_mask()`. It's defined in `cpumask.h`, and takes two arguments - an iterator, and a mask. The mask is a `cpumask_t` lvalue that defines the set of CPUs to iterate over.
There are three helper macros that just take an iterator - you probably want to use one of those:
for_each_possible_cpu(cpu)
for_each_present_cpu(cpu)
for_each_online_cpu(cpu)
`for_each_possible_cpu()` iterates over all CPUs that could possibly be present on this boot of the kernel; `for_each_present_cpu()` iterates over all CPUs that are currently present (on a system that does not support CPU hotplug, these two are the same); and `for_each_online_cpu()` iterates over all CPUs that are currently enabled and available to the scheduler.
Note that `for_each_online_cpu()` should be used within a `get_online_cpus()` / `put_online_cpus()` section, to prevent the online CPU map changing while you iterate.
It is most likely that `for_each_possible_cpu()` is what you want.
Problem
I need to loop through each CPU so I can get a `per_cpu` value from them, but there does not seem to be a `for_each_cpu(i)` macro in version 2.6.26.5 of the Linux Kernel. How can I achieve something like the following? ``` for_each_cpu(i) per_cpu(vm_event_states, i) ```