concurrency control in Linux device drivers

Source: Internet
Author: User
Tags mutex semaphore

Concurrency refers to multiple execution units being executed concurrently and concurrently, while concurrent execution units access to shared resources can easily lead to race

Main race in Linux kernel
1. Multi-symmetric processor with multiple CPUs 2. A single CPU process and the process that preempted it 3. Interrupts (hard interrupt, soft interrupt, Tasklet, lower half) and process
The code area that accesses the shared memory resource is called the "critical section", and the critical section needs to be protected by an exclusive mechanism such as interrupt masking, atomic manipulation, spin locks, and semaphores.
is a mutually exclusive approach that can be used in Linux device drivers.

Here are a few mutually exclusive introductions:

1. Interrupt shield, which is mainly used for single CPU, interrupt masking will cause the interruption and the concurrency between processes to no longer occur. How to use:

Local_irq_disable ();//Shielded Interrupt
...
...
Critical section
...
Local_irq_enable ();//interrupt on/off
Because many important operations, such as asynchronous IO, process scheduling, and so on, are dependent on interrupts, interrupts are critical to the operation of the kernel, and all interrupts are not processed during a masking outage.
Therefore, prolonged shielding interruptions are dangerous and can result in data loss or even a system crash. So this is not a major discussion.

*************************************************************************************************************** *******************************************

2. Atomic operations, atomic operations are a series of operations that cannot be interrupted. The Linux kernel provides a series of functions to implement atomic operations in the kernel, which are divided into 2 classes, respectively, for bits and integer variables
Perform atomic operations.
Steps to implement atomic operations:

1. Define an atomic variable and set the value of the variable
void Atomic_set (atomic_t *v, int i);//Set atomic variable value to I
atomic_t v = atomic_init (0);  //define atomic variable V, Initialize to 0
2. Gets the value of the atomic variable
Atomic_read (atomic_t *v);
3. Atomic variable addition and subtraction operation
void Atomic_add (int i,atomic_t *v);//atomic variable plus i
void atomic_sub (int i, atomic_t *v);//atomic variable minus I
4. Atomic variable self-increment /decrement
void Atomic_inc (atomic_t *v);//self-increment 1
void Atomic_dec (atomic_t *v);//Auto minus 1
5. Operation and test: Perform self-increment on atomic variable, The self-decrement (no addition) tests whether it is 0, or returns False if 0 returns true.
int atomic_inc_and_test (atomic_t *v);
int atomic_dec_and_test (atomic_t *v);
int atomic_sub_and_test (int i, atomic_t *v);
6. Operation and return
int atomic_add_return (int i, atomic_t *v);
int Atomic_sub_return (int i, atomic_t *v);
int Atomic_inc_return (atomic_t * v);
int Atomic_dec_return (atomic_t * v);

*************************************************************************************************************** *******************************************
3. Spin Lock
A spin lock is a busy lock that repeatedly tests and sets the operation within a small loop.
The characteristics of the critical zone of the spin lock protection: The critical area is small, and the critical area can not cause sleep operation, otherwise it may cause the system to crash. Spin locks can cause system deadlocks,
The most common scenario that causes this problem is recursion using a spin lock.

Operation Steps of Spin Lock:

1. Define SPIN Lock
spinlock_t lock;
2. Initialize the spin lock
Spin_lock_init (lock); This is a macro that is used for dynamic initialization of spin lock lock;
3. Get spin Lock
Spin_lock (lock); The macro is used to lock, and if the lock can be obtained immediately, it can return immediately, otherwise, he will spin there until the spin lock is released by the holding person.
Spin_trylock (lock); be able to obtain, then return True, otherwise return false, actually is not in situ circles just.
4. Release the spin lock
Spin_unlock (lock);
Used with the above two pairs.
Example:

spinlock_t lock;
Spin_lock_init (&lock);
Spin_lock (&lock); Get spin lock, protect critical section

。。。。 Critical section

Spin_unlock (&lock);//Release spin lock

The spin lock does not care how the locked critical section is executed. Whether read or write, in fact, reading a shared resource should allow multiple execution units to be
Access, then the spin lock has its drawbacks. A read-write lock is then derived.

It retains the properties of the spin, but can allow multiple unit processes to operate simultaneously on the operation. Of course, reading and writing cannot be done at the same time.

Now there is a problem, if my first process to write a shared resource, the second process read, once written, then can not read, may write more things, but the second process read very small, then
Can the first process be written at the same time that my second process reads?
Of course, that leads to the concept of sequential locking. is the same operation.


*************************************************************************************************************** *******************************************
4. Signal Volume
is a common method used to protect critical areas, which is used in the same way as a spin lock, but it is not spinning in place, and when the semaphore is not acquired, the process enters a dormant wait state.

Main operation:

1. Define the SEM signal volume
struct semaphore sem;
2. Initialize the semaphore
void Sema_init (struct semaphore *sem, int val);
Initialize the semaphore and set the value of the SEM to Val
Initialization can also be used in this way, Init_mutex (SEM), which is a macro #define INIT_MUTEX (SEM) sema_init (SEM, 1)
init_mutex_locked (SEM), which is a macro #define INIT_MUTEX_LOCKED (SEM) sema_init (SEM, 0)

3. Get the semaphore
void down (struct semaphore * sem);
The function is used to obtain the semaphore of the SEM, which causes sleep, so it cannot be used in interrupts.
int down_interruptible (struct semaphore* sem);
Similar to the above function, because the process down to sleep can not be interrupted by the signal, and it can be interrupted by the signal, the signal will also cause the function to return.
int Down_trylock (struct semaphore * sem);

4. Releasing the semaphore
void up (struct semaphore * sem);
This function is used to release the semaphore while waking the waiting person.
Semaphores are generally used in this way:

Declare_mutex (SEM);

Down (&SEM);

..... Critical section

Up (&sem);

Linux spin locks and semaphores take the form of "get lock-access critical section-release lock".


*************************************************************************************************************** **********

5. Mutex


The mutex and the semaphore are basically the same. Not introduced.


Summary: Concurrency and race are widespread, these mechanisms are a good way to solve the problem, interrupt masking is rarely used alone, atomic operations can only be done for integers, so the spin lock and signal volume is the most widely used.
A spin lock causes a dead loop that does not allow blocking during locking, so the critical section of the lock is required to be small. The signal volume allows the critical section to be blocked, which can be applied to large critical areas. Read/write spin locks and read/write semaphores are
Relaxed the condition of spin locks and semaphores, they allow multiple processes to concurrently read the shared space.

Concurrency control in Linux device drivers

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.