Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between spin_lock and raw_spin_lock()?

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.

like image 460
Saurabh Sengar Avatar asked Nov 02 '15 17:11

Saurabh Sengar


People also ask

What is Spin_lock_irq?

spin_lock_irq will disable the interrupt x and take the the lock. spin_unlock_irq will enable the interrupt x.

How does spinlock work in Linux?

Spinlock works by making sure nothing else will run. If you don't want an interrupt to run you disable it and proceed safely into the critical section. On multicore machine a spinlock will actually spin waiting for another core that holds the lock to release it.

How do you implement a spin lock?

A spinlock implementation requires a special kind of instruction known as a read-modify-write (RMW) instruction. These expensive operations are useful because they act atomically preventing a data race in multithreaded kernels.

Which API is used to lock semaphore in kernel space?

All semaphore API is located in the include/linux/semaphore. h header file. We may see that the semaphore mechanism is represented by the following structure: struct semaphore { raw_spinlock_t lock; unsigned int count; struct list_head wait_list; };


1 Answers

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.

like image 199
Tsyvarev Avatar answered Oct 12 '22 20:10

Tsyvarev