Archive

Archive for the ‘Linux Kernel Misc’ Category

Find out if you are invoked by a kernel thread

April 26, 2010 Leave a comment

There may be times when you have patch in the kernel and either

a) Only want to execute if you are invoked by a kernel thread

b) Only want to execute if you are not invoked by a kernel theread

To find out if you are or are not, do this:

//get current task struct to see process info
struct task_struct* task = get_current();
//check if its a kernel thread
if (task->mm == NULL) {
//it’s a kernel thread
}
else {
//it’s not a kernel thread
}
Advertisements
Categories: Linux Kernel Misc Tags: , , ,

Get current time in kernel

April 26, 2010 Leave a comment

The way to get the current time in kernel is by calling current_kernel_time() from the linux/time.h file. There is a macro called CURRENT_TIME() which just calls current_kernel_time(). It saves a few cycles to call it directly. The return type will be timespec struct type.

timespec has 2 fields – seconds and nanoseconds.

Categories: Linux Kernel Misc Tags: ,