8, Linux device driver concurrency control

Source: Internet
Author: User
Tags mutex semaphore

One, concurrency and competition

Concurrency refers to the simultaneous execution of multiple execution units, which can easily lead to race when accessing shared resources, such as hardware resources, global variables of software, and static variables.

1.1. Interrupt Shield

In a single-core CPU, a simple and effective way to avoid race is to interrupt the system before entering the critical section. That is, before entering the critical section, interrupts are closed, so that the concurrency between interrupts and processes does not occur, and because the scheduler of the process is dependent on interrupts to implement, without interruption, the process cannot be toggled, ensuring that concurrency between processes does not occur.

Method:

Local_irq_disable ()

Code for XXXXX critical section

Local_irq_enable ()

Although Local_irq_disable () and local_irq_enable () can shut down this CPU interrupt, but in the multi-core CPU, does not solve the problem, because they can only prohibit and enable this CPU interrupt, and does not solve the multi-CPU-induced competition, the In more than one, the method of closing interrupts is generally used in conjunction with a spin lock.

If you point to the bottom of the forbidden interrupt, in should use Locak_bh_disable (), enable Yes, local_bh_enable ();

1.2. Atomic operation

Atomic operation, can guarantee the exclusivity of an integer data modification, that is, the atomic operation is the operation of a data, the operation of the process, is not to be interrupted by the scheduling mechanism of the thread, that is, once started, is absolutely not sleep and blocking until the end of the operation. For the operation of the number of shapes, the operation of the integer number and the bit is atomic respectively.

Operation of 1.2.1, integral type atoms

(1) Set the value of the atomic variable

void Atomic_set (atomic_t *v,int i); Set the value of the atomic variable to I

atomic_t v = atomic_init (0); Defines the atomic variable V and initializes it to 0

(2) Gets the value of the atomic variable

Atomic_read (atomic_t *v)//return value is the value of the atomic variable

(3) Operation of atoms

void Atomic_add (int i, atomic_t *v)//i + V

void atomic_sub (int i,atomic_t *v)//V–i

(4) Operation and testing

int atomic_inc_an_test (atomic_t *v)//self-add 1, test whether 0, so that the words return true

int atomic_dec_and_test (atomic_t *v)//auto minus 1, test if 0, make words, return true

(5) Operation and return

int atomic_add_reture (int i, atomic_t *v); V + I, returns the new value

int atomic _sub_return (int i, atomic_t *v)//V–i, return new value

int Atomic_inc_return (atomic_t *v)//V + 1

int Atomic_dec_return (atomic_t *v)//v–1

Operation of 1.2.2, bit atoms

(1) Set to

void Set_bit (nr,void * addr)//Set address addr nr bit is 1

(2) Clear bits

void Clear_bit (nr, void *addr)//Set address addr nr bit is 0

(3) Change the bit

void Change_bit (nr, void *addr)//Set address addr the NR bit reversal

(4) Detection bit

Test_bit (nr, void *addr); Returns the value of the addr nr bit

(5) Testing and operating as

int Test_and_set_bit (nr, void *addr);

The test is performed before the bit is set.

Characteristics of 1.2.3 and atomic operations

Atomic operation of the characteristics of the operation of the atomic number, it will never be interrupted by the thread scheduler, it is said that this time period, is not the process of the context of the switch, further said that there is absolutely no sleep and blocking.

1.3. Spin Lock

Spin lock spin Lock is another means of mutually exclusive access to critical resources, and its implementation is realized by means of atomic operation. The mechanism of the spin lock is that the process accesses the resource before it needs to read the memory of the feature, and when it reads the value, that is, when it gets to the lock, it has permission to go to the next step, while the other process does not get to the lock, just like waiting in place, and then reading again, this waiting, reading process, We call it spin.

1.3.1, Spin API

(1) Definition of lock

spinlock_t lock;

(2) Initialization of the lock

Spin_lock_init (Lock)//Finish initialization

(3) Get the lock

Spin_lock (Lock)

Try to get the lock, if you get it, return it immediately, without getting it, go into the spin state, Linux also provides a non-blocking way:

Spin_trylock (Lock)

Returns True when the lock is obtained, false when not obtained

(4) Release lock

Spin_unlock (Lock)

1.3.2, general mode of execution

spinlock_t lock;

Spin_init_lock (lock);

Spin_lock (lock);

XXXX Critical Area code

Spin_unlock (lock);

1.3.3, spin lock and interrupt

In a single CPU and the kernel can be preempted in the system, the spin lock during the acquisition of locks, the kernel preemption is forbidden, so at this time, is fully guaranteed to obtain the lock device critical section operation code, do not receive other process disturbance; But in a multicore CPU, one of the CPUs gets a spin lock, but Only on the CPU preemption scheduling is banned, other cores of the CPU is not forbidden to preempt scheduling, so in order to ensure the multi-core situation, the critical section is not affected by this CPU and other CPU preemption process.

because the bottom of the spin lock is realized by atomic operation, the operation of the device that is guaranteed to obtain the lock is not affected by the process scheduling of this CPU and other CPUs, and the spin lock shuts down the preemption system, but these features do not guarantee that when the lock is taken, the critical section code is executed. is not affected by the interruption and the bottom (BH), that is, this time, this CPU interrupt occurs, or it will affect the critical section of the code, so generally if the operation is closed interrupt, this time with the help of the lock derivation mechanism

SPIN_LOCK_IRQ = Spin_lock () + local_irq_disable ()//Acquire lock while closing interrupt

SPIN_INLOCK_IRQ = Spin_unlock () + local_irq_enable//release lock at the same time, open interrupt

Spin_lock_irqsave = Spin_lock () + locak_irq_save ()//Acquire lock while closing interrupt and save interrupt State

Spin_lock_irqrestore () = Spin_unlock () + locak_irq_restore ()//release the lock while the interrupt status is replied

SPIN_LOCK_BH () = Spin_lock () + local_bh_disable (); While acquiring the lock, turn off the bottom interrupt

SPIN_UNLOCK_BH () = Spin_unlock () + local_bh_enable//release lock, and restore bottom interrupt

When the spin lock inside the interrupt, CPU0 obtained a spin lock, into the atom of the operation and CPU 1 in the CPU0 has not released the lock, CPU1 just wait for the place (waste

CPU) and, if interrupted, avoids the effects of this CPU and guarantees the possibility of kernel concurrency. 1.3.4, Spin lock precautions

(1) because after the spin lock, the CPU starts the atomic operation, the other CPU will be in situ always spin, wait for the process, not sleep, so waste more than N resources, so, generally, the process has a lock time is very short, so that is the cost of comparison If the code for the critical section is very large, the other CPUs are waiting in place, wasting resources and causing the system to degrade in performance.

(2) The process can no longer attempt to acquire the lock while it has a lock, otherwise it causes a deadlock.

(3) In the acquisition of the device of the spin lock, absolutely can not appear the situation of process scheduling, that is, the process of sleep and process can never occur, can not appear copy_tousr or Copy_from_usr,kmalloc, msleep and other functions, Because the spin lock implementation is implemented by atomic operations, the atomic operation of the characteristics, once you start execution, there will never be a process scheduling, otherwise it will cause the kernel to crash.

1.4. Signal Volume

Signal volume, is a very common means of mutual exclusion, similar to the lamp, when the number of such a time is not zero, the process to obtain resources, the lights take away one, when the release of resources, the lights back, when the light is zero, this time, the process will be suspended, into the state of sleep until it is awakened.

1.4.1, API

(1) Define signal volume

struct semaphore sem;

(2) Initialization of the semaphore

void Sema_init (struct semaphore * sem, int val)

The value of the initialized semaphore is Val

(3) Get the signal volume

int down (struct semaphore * sem)

Obtain the signal volume, and obtain the lock is similar, when does not obtain the signal quantity, enters the sleep state, namely, cannot be used in the interrupt inside, because the interrupt all is closed, the process scheduler is relies on the interrupt realization, has not interrupted, when the sleep process, has no process scheduler to go to wake the process, Cause the program to stop there with death. The kernel also provides an interface that can be interrupted

int down_interruptible (struct semaphore *sem)

Go back to try to get the semaphore, not get the time, go to sleep, but can be interrupted,

int Dowun_trylock (struct semaphore *sem)

Does not cause sleep even when there is no active semaphore, but returns immediately, so it can be used in an interrupted context

(4) Release signal volume

void up (struct semaphore *sem)

When the semaphore is initialized to 1, the function of the mutex is realized.

1.5, mutual exclusion lock

The use of mutexes is almost identical to the semaphore, except that the interface name is not the same.

(1) Defining a mutual exclusion lock

struct Mutex Mymutex;

(2) Initialize mutex lock

Mutex_init (struct mutex *mymutex)

(3) Obtaining a mutual exclusion lock

void Mutex_lock (struct mutex mymutex;)

Of course there are, void mutex_lock_interruptible (struct mutex mymutex;) and void Mutex_trylock (struct mutex mymutex;)

(4) Releasing the mutex lock

void Mutex_unlock (struct mutex Mymutex)

Selection of 1.5.1, spin-lock and mutex

(1) Spin lock in the absence of a lock, it can only be in situ and so on, so the cost is the other process to obtain lock execution time, and the mutex, in the absence of a lock, will sleep the current process, the cost of the lock, the cost of the process switch.

(2) The process of the mutex, which brings the process to sleep, therefore, the mutex is absolutely not in the context of the interruption, the spin lock is not, it is very suitable for use with the interrupt, the critical section of the spin lock protection, there is absolutely no process blocking and sleep.

8, Linux device driver concurrency control

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.