linux - 我正在创建一个内核模块来查找所有进程的驻留页面

标签 linux process linux-kernel

我正在创建一个内核模块来查找所有进程的驻留页面。我正在使用 get_mm_rss() 和 for_each_process 但它有效 仅适用于 init 进程/第一次迭代后的第一次它不起作用。

int __init schedp(void){
    struct task_struct *p;

    for_each_process(p) {
        int pid = task_pid_nr(p);
        printk("Process: %s (pid = %d) , (rpages : %lu)\n", 
                p->comm, pid, get_mm_rss(p->mm)); 
    } 
    return 0;
} 

结果:

BUG: unable to handle kernel NULL pointer dereference at 00000160,

最佳答案

您可能在 p->mm 中得到 NULL,因为某些任务可能有无效的 mm 指针,因为它们正在退出或停止没有 mm(因为它们是内核线程,不确定)。

当您对如何使用内核 API 感到困惑时,请始终在内核本身中寻找示例。使用交叉引用工具快速搜索给了我 kernel/cpu.c :

for_each_process(p) {
    struct task_struct *t;

    /*
     * Main thread might exit, but other threads may still have
     * a valid mm. Find one.
     */
    t = find_lock_task_mm(p);
    if (!t)
        continue;
    cpumask_clear_cpu(cpu, mm_cpumask(t->mm));
    task_unlock(t);
}

请注意,您需要调用 find_lock_task_mm()task_unlock() 并显式检查 NULL

关于linux - 我正在创建一个内核模块来查找所有进程的驻留页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30435563/

相关文章:

linux - `' ` 在 `printf "%x""' 你"` 中的功能是什么?

linux - 如何检查进程是否有正在运行的磁盘 I/O 请求?

java - 杀死在 Java 中调用的子进程的问题

linux - Linux中用户空间内存和内核空间内存如何映射到物理内存?

linux - Linux 中的设备驱动程序开发

linux - 尽管模块已卸载,为什么设备仍存在于/proc/devices中

python - bash on Ubuntu on windows Linux、文件夹识别和运行 Python 脚本

linux - R 2.15 在 Redhat 中安装

linux - ubuntu 14.04如何定位thrift安装目录?

c# - 判断进程是否为系统进程