【问题标题】:Set cpu affinity on a loadable linux kernel module在可加载的 linux 内核模块上设置 cpu 亲和性
【发布时间】:2015-02-05 15:31:57
【问题描述】:

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

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

【问题讨论】:

    标签: c module kernel cpu affinity


    【解决方案1】:

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

    像这样:

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

    【讨论】:

    • 虽然,`for_each_cpu' 仅索引 cpu,这意味着对于每个循环,我使用的指令将在当前使用的默认内核上使用。即我将启用核心 0 上的计数器两次。如果我没有在两个内核上启用它,那么当我尝试使用有关性能计数器的其他指令时,如果指令跳转到另一个没有计数器的内核,我有可能会收到“非法指令错误”已启用。
    • 好的,我明白了,一开始好像是 on_each_cpu(_mask) 和 hotplug 工作人员为每个在线的 cpu 提供的东西。
    • 对于每个 cpu 上线,抱歉,规则 34:[如何在每个 CPU 上运行代码] (stackoverflow.com/questions/17456812/…)
    • 非常感谢。该链接挽救了我的生命。它实际上是通过使用提出问题的人的“on_each_cpu(function, NULL, 1)”来工作的:D
    猜你喜欢
    • 2013-08-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-10-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多