What is the difference between spin_lock and raw_spin_lock()?
Asked Answered
B

2

9

There is raw variant of each spin lock available in linux kernel, I want to know its usage ? e.g. :

raw_spin_lock(), raw_spin_lock_irqsave(), etc.

Bendwise answered 2/11, 2015 at 17:32 Comment(0)
A
9

spin_lock* functions do the same as raw_spin_lock* ones plus, when lock debugging is enabled(CONFIG_DEBUG_LOCK_ALLOC), perform some additional runtime checks for lock operations, such as checks for deadlock. These checks are performed by lockdep subsystem.

As a rule, spin_lock* functions should be used whenever it is possible.

Only in rare cases of very tricky locking policy, when lockdep can produce false warnings, raw_spin_lock* functions can be used.

Also, raw_* functions can be preferred to common ones for reduce memory usage or perfomance reasons. But it should be actual time/space measurements, reflected significant wins from using these optimizations.

Arkose answered 2/11, 2015 at 19:28 Comment(3)
when lockdep can produce false warnings ? and in -rt normal spin_lock can sleep while raw_spin_lock don't, is it true ?Bendwise
How about the PREEMPT_RT patch that supposedly turns spinlocks into rt_mutexes?Osborn
@PavelŠimerda: PREEMPT_RT case seems to be described in another answer. Is something wrong about it?Arkose
C
9

The main difference is spin_lock variants map to raw_spin_lock variants for non-RT whereas if CONFIG_PREEMPT_RT is set, then they map to rt_spin_lock which can sleep.

By decoupling the spin_lock from sleeping vs non-sleeping variations depending on whether we are RT or not, the spin_lock API can be kept consistent across the kernel code.

Can answered 17/9, 2016 at 21:8 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.