4

I need to create a kernel module that enables ARM PMU counters on every core in the computer. I have trouble setting the cpu affinity. Ive tried sched_get_affinity, but apparently, it only works for user space processes. My code is below. Any ideas?

 #define _GNU_SOURCE

 #include <linux/module.h>  /* Needed by all modules */
 #include <linux/kernel.h>  /* Needed for KERN_INFO */


 int init_module(void){


    unsigned reg;



    /* enable user-mode access to the performance counters*/

        asm volatile("MRC p15, 0, %0, C9, C14, 0\n\t" : "=r"(reg));

        reg |= 1;

        asm volatile("MCR p15, 0, %0, C9, C14, 0\n\t" :: "r"(reg));


    printk(KERN_INFO "User mode Performance Counters are enabled.\n",reg);

    return 0;
}

void cleanup_module(void){

    unsigned reg;

    /* disable user-mode access to the performance counters*/
    asm volatile("MRC p15, 0, %0, C9, C14, 0\n\t" : "=r"(reg));

    reg &= (~0 << 1);

    asm volatile("MCR p15, 0, %0, C9, C14, 0\n\t" :: "r"(reg));


    printk(KERN_INFO "User mode Performance Counters are disabled.\n");
}

1 Answers1

0

cpu affinity is pretty meaningless in terms of kernel module, as far as i can see you need to traverse cpus one by one to initialize PM.

like so:

for_each_cpu(cpu, mask) 
  include/linux/cpumask.h +152
reptildarat
  • 1,036
  • 2
  • 18
  • 35
  • 1
    the `for_each_cpu' though, only indexes the cpus, which means that for every loop, the instructions that i use, will be used on the default core that is currently in use. i.e i will enable the counters on core 0 twice. If I dont enable it on both cores, then when I try to use other instructions regarding the performance counters, there is a chance i will get the ''illegal instruction error'' if the instructions jump to another core where the counters aren't enabled. – Emmanouil Skordalakis Feb 06 '15 at 13:09
  • ok, i see, seems like it's on_each_cpu(_mask) at the beginning and something from hotplug staff for every cpu going online. – user4536444 Feb 09 '15 at 14:07
  • for every cpu become online, sorry well, rule 34: [How to run code on every CPU] (http://stackoverflow.com/questions/17456812/how-to-run-code-on-every-cpu) – user4536444 Feb 09 '15 at 14:13
  • Thank you so much. The link saved my life. It actually worked by using the "on_each_cpu(function, NULL, 1)" stuff of the guy who did the question :D – Emmanouil Skordalakis Feb 09 '15 at 15:05