-->

How to run code on every CPU

2020-02-14 08:32发布

问题:

I am trying to set the Performance Monitor User Mode Enable register on all cpus on a Nexus 4 running a mako kernel.

Right now I am setting the registers in a loadable module:

    void enable_registers(void* info)
    {
        unsigned int set = 1;
        /* enable user-mode access to the performance counter*/
        asm volatile ("mcr p15,  0, %0, c9,  c14, 0\n\t" : : "r" (set));
    }

    int init_module(void)
    {
       online = num_online_cpus();
       possible = num_possible_cpus();
       present = num_present_cpus();
       printk (KERN_INFO "Online Cpus=%d\nPossible Cpus=%d\nPresent Cpus=%d\n", online, possible, present);
       on_each_cpu(enable_registers , NULL, 1);
       return 0;
    }

The problem is that on_each_cpu only runs the function on Online cpus and as shown by the printk statment:

Online Cpus=1
Possible Cpus=4
Present Cpus=4

Only one of the four is online when I call on_each_cpu. So my question is, how do I force a cpu to be online, or how can force a certain cpu to execute code? Thanks

回答1:

You don't need to run the code on every cpu right now. What you need to do is arrange so that when the offline cpus come back online, your code is able to execute and enable the access to the PMU.

One way to achieve that would be with a cpu hotplug notifier.