Michele
Michele

Reputation: 131

how to know if a spinlock is held in kernel space

I'm writing a Linux Devide Driver using NVIDIA API, and I notice that there's a function that fails if I call it holding a spinlock. I was asking myself, how a kernel function knows if it's called holding a spinklock?

maybe better if I go with an pseudocode example:

spin_lock_irqsave(&my_lock,flags)
nvidia_p2p_get_pages(...)
spin_unlock_irqrestore(&my_lock, flags)

the function p2p_get_pages in this situation returns an error (but it works if I use it without the spinlock... anyway this is not the problem).

What could happen inside that function that fails? maybe it tries to take another spinlock? or sleep? or it checks for spinlocks... and how? how can it knows about the spinlock?

thank you!

Upvotes: 0

Views: 1936

Answers (1)

CL.
CL.

Reputation: 180020

It would be possible to check whether a specific lock is locked by calling spin_is_locked(). However, it is unlikely that the nvidia driver knows about your my_lock.

Many functions care only about the fact that they (might) need to sleep. This would blow up when in an atomic context (i.e., when interrupts are disabled). To detect this, many such functions call might_sleep() to log a warning before bad things actually happen. (might_sleep() does not know about your locks either, it just checks whether interrupts are disabled.)

Upvotes: 1

Related Questions