In one of our demo kernel modules regarding kernel threads (in ch15/kthread_simple/kthread_simple.c), we created a kernel thread and then employed the get_task_struct() inline function to mark the kernel thread's task structure as being in use. As you can now guess, the get_task_struct() routine increments the task structure's reference counter – a refcount_t variable named usage – via the refcount_inc() API:
// include/linux/sched/task.h
static inline struct task_struct *get_task_struct(struct task_struct *t)
{
refcount_inc(&t->usage);
return t;
}
The converse routine, put_task_struct(), performs the subsequent decrement on the reference counter. The actual routine employed by it internally, refcount_dec_and_test(), tests whether the new refcount value has dropped to 0; if so, it returns true, and if this is the case, it implies that the task structure isn't being...