在可加载 linux 内核模块上设置 cpu 亲和力

Set cpu affinity on a loadable linux kernel module

我需要创建一个内核模块,在计算机的每个内核上启用 ARM PMU 计数器。我在设置 cpu 亲和力时遇到问题。我试过 sched_get_affinity,但显然,它只适用于用户 space 进程。我的代码如下。有什么想法吗?

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

cpu affinity 在内核模块方面毫无意义,据我所知,您需要一个接一个地遍历 cpus 来初始化 PM。

像这样:

for_each_cpu(cpu, mask) 
  include/linux/cpumask.h +152