current->mm gives NULL in linux kernel

寵の児 提交于 2020-01-01 03:16:28

问题


I would like to walk the page table, so I have accessed the current->mm, but it gives NULL value.

I'm working on linux kernel 3.9 and I don't understand how could current->mm is zero.

Is there something I miss here?


回答1:


It means you are in a kernel thread.

In Linux, kernel threads have no mm struct. A kernel thread borrows the mm from the previous user thread and records it in active_mm. So you should use active_mm instead.


More details:

in /kernel/sched/core.c you can find the following code:

static inline void
context_switch(struct rq *rq, struct task_struct *prev,
           struct task_struct *next)
{
    ...
    if (!mm) {
        next->active_mm = oldmm;
        atomic_inc(&oldmm->mm_count);
        enter_lazy_tlb(oldmm, next);
    } else
        switch_mm(oldmm, mm, next);
    ...
}

If the next thread has no mm (a kernel thread), the scheduler would not switch mm and just reuse the mm of the previous thread.




回答2:


Need for active_mm assignment : The call to switch_mm(), which results in a TLB flush, is avoided by “borrowing” the mm_struct used by the previous task and placing it in task_struct→active_mm. This technique has made large improvements to context switches times.



来源:https://stackoverflow.com/questions/16975393/current-mm-gives-null-in-linux-kernel

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!