I've written a code something like this:
It is in non-interrupt context:
spin_lock_irqsave(&lock, flags);
printk("some message blah...\n");
spin_unlock_irqrestore(&lock, flags);
I am running this code and "looks" safe because I don't see any crashes. But, I'm not sure this is really a safe code or not. Because that may trigger a system crash with 1/100000 probability.
Additionally, I would like to know if calling "sleep" function inside spin_lock_irqsave is safe (in not-interrupt context).
Kernel code should not sleep while holding a spin lock. In Linux Device Drivers, Third Edition, the "Spinlocks and Atomic Context" section in Chapter 5 states:
As for
printk
, I believe it is safe to call within a critical section. The comments in the source code even mention: