Search code examples
cmodulekernelcpuaffinity

Set cpu affinity on a loadable linux kernel module


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");
}

Solution

  • 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