As you now know, kernel code runs in one of two contexts:
- Process (or task) context
- Interrupt (or atomic) context
They are mutually exclusive – kernel code runs in either the process or atomic/interrupt context at any given point in time.
Often, when writing kernel or driver code, it is imperative for you to first figure out what context the code that you're working on is running in. One way to learn this is by employing the following macro:
#include <linux/preempt.h>
in_task()
It returns a Boolean: True if your code is running in process (or task) context, where it's – usually – safe to sleep; returning False implies you are in some kind of atomic or interrupt context where it is never safe to sleep.
You might have come across the usage of the in_interrupt() macro; if it returns True, your code is within an interrupt context, if False, it isn't. However, the recommendation for modern code is to ...