current->mm gives NULL in linux kernel

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.