【问题标题】:Implementing a system call for CPU hotplug on RPI3/ModelB在 RPI3/Model B 上实现 CPU 热插拔系统调用
【发布时间】:2018-01-28 21:22:48
【问题描述】:

我的目标是在 linux 内核中实现一个系统调用来启用/禁用 CPU 内核。

首先,我实现了一个在 4 核系统中禁用 CPU3 的系统调用。

系统调用代码如下:

#include <linux/kernel.h>
#include <linux/slab.h>
#include <asm/uaccess.h>
#include <asm/unistd.h>
#include <linux/cpumask.h>


asmlinkage long sys_new_syscall(void)
{
    unsigned int cpu3 = 3;

    set_cpu_online (cpu3, false) ;          /* clears the CPU in the cpumask */
    printk ("CPU%u is offline\n", cpu3);


    return 0;
}

系统调用已在内核中正确注册,并且我在内核配置期间启用了“cpu hotplug”功能(见图)

内核配置:

内核已构建。但是当我使用 test.c 检查系统调用时:

#include <stdio.h>
#include <linux/kernel.h>
#include <sys/syscall.h>
#include <unistd.h>



long new_syscall(void)
{
    return syscall(394);
}

int main(int argc, char *argv[])
{
     long int a = new_syscall();
     printf("System call returned %ld\n", a);
     return 0;
}

操作系统死机! 我做错了什么?

【问题讨论】:

    标签: linux linux-kernel real-time embedded-linux raspberry-pi3


    【解决方案1】:

    为什么要实现一个专用的系统调用?使 cpus 脱机的标准方法是通过写入 sysfs。在极不可能的情况下,有充分的理由创建一个专用的系统调用,您必须检查脱机在后台的工作方式并重复此操作。

    set_cpu_online (cpu3, false) ;          /* clears the CPU in the cpumask */
    

    您自己的评论强烈表明这太简单了。例如,如果执行此操作的线程在所述 cpu 上运行怎么办?排队的线程呢?

    等等

    【讨论】:

    • 我知道要使 CPUn 离线,我必须在 /sys/devices/system/cpu/offline 中写入 n。我需要在专门的系统调用中完成。您知道“离线”属性在哪里/如何实现吗?
    【解决方案2】:

    这是一个老话题,但是您可以使用来自include/linux/cpu.h 的函数cpu_up(cpu_id)cpu_down(cpu_id) 在内核领域启动/关闭CPU。

    似乎set_cpu_online 没有被导出,因为从其他内核部分的角度来看它似乎并不安全(例如,它不考虑进程关联性和其他复杂性)。

    所以,你的系统调用可以写成:

    asmlinkage long sys_new_syscall(void)
    {
        unsigned int cpu3 = 3;
    
        cpu_down(cpu3) ;          /* clears the CPU in the cpumask */
        printk ("CPU%u is offline\n", cpu3);
    
        return 0;
    }
    

    我在这里有一个使用这些方法的示例模块:https://github.com/pappacena/cpuautoscaling

    【讨论】:

      猜你喜欢
      • 2018-01-23
      • 2023-03-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-03-14
      • 1970-01-01
      相关资源
      最近更新 更多