Design and Implementation of Linux kernel Reading Notes (7)-kernel synchronization method (1)

Source: Internet
Author: User

Kernel Synchronization Method

1. atomic operation

Atomic operations can ensure that commands are executed in an atomic manner-the execution process is not interrupted. The kernel provides two sets of atomic operation interfaces, one for integer operations, and the other for separate bit operations.

Atomic operations on integers can only process atomic_t data.

In addition to the atomic Integer Operation, the kernel also provides a set of functions that operate on Bit-level data. The bitwise operation function operates on common memory addresses. Its parameters are a pointer and a single-digit number. The 0th-bit operation is the lowest valid bit for a given address.

The kernel also provides a set of non-atomic functions corresponding to the preceding operations. The operation of a non-atomic bit function is the same as that of an atomic bit function, but the former does not guarantee atomicity, and its name prefix has two underscores. For example, the non-atomic form corresponding to set_bit is _ set_bit. If you do not need atomic operations (for example, if you have used locks to protect your data ), these non-atomic bitwise functions may execute faster than the atomic bitwise functions.


2. spin lock

The most common lock in Linux kernel is spin lock ). A spin lock can only be held by one executable thread. If an execution thread tries to obtain a contention (held) spin lock, the thread will keep repeating-Rotating-waiting for the lock to be available again.

Spinlock_t mr_lock = spin_lock_unlocked; spin_lock (& mr_lock);/* critical section... */spin_unlock (& mr_lock );

The spin locks implemented by the Linux kernel cannot be recursive.


Spin locks can be used in Interrupt ProcessingProgramMedium (semaphores cannot be used here because they cause sleep ). When using a spin lock in the interrupt handler, you must first disable local interruption (Interrupt requests on the current processor) before obtaining the lock. Otherwise, the interrupt handler interrupts the kernel that is holding the lock.Code, May try to compete with the already held spin lock, resulting in a deadlock. Note that all you need to disable is the interruption on the current processor. If the interrupt occurs on different processors, even if the interrupt handler spin on the same lock, it does not prevent the lock owner (on different processors) from eventually releasing the lock.

The kernel provides an interface to prohibit interruption and request locks at the same time:

Spinlock_t mr_lock = spin_lock_unlocked; unsigned long flags; spin_lock_irqsave (& mr_lock, flags); // Save the current state of the interrupt, disable the interrupt, and obtain the lock. /* Critical section... */spin_unlock_irqrestore (& mr_lock, flags); // unlock and restore the interruption to the status before the lock.

When used with the lower half, the lock mechanism must be carefully used. The spin_lock_bh function is used to obtain the specified lock. At the same time, it will disable the execution of all lower half. The corresponding spin_unlock_bh function executes the opposite operation.

Because the lower half can preemptible the code of the process context, when the lower half shares data with the process context, the shared data in the process context must be protected, and the lower half must be locked. Similarly, since the interrupt handler can take the lower half, if the interrupt handler and the lower half share data, the interrupt handler must be prevented while obtaining the appropriate lock.

Tasklets of the same type cannot run at the same time, so shared data in tasklets of the same type does not need to be protected. However, when data is shared by two different types of tasklets, You need to obtain a general spin lock before accessing the data in the lower half. The lower half is not allowed here, this is because there will never be a situation where tasklet occupies each other on the same processor.

For soft interruptions, whether or not data of the same type is shared by soft interruptions, the data must be protected by a lock, this is because even two soft interrupts of the same type can run on multiple processors of a system at the same time. However, one soft cut on the same processor won't seize another Soft Interrupt. Therefore, it is not necessary to disable the lower half.


3. read-write spin lock

This spin lock provides different locks for read and write respectively. One or more read tasks can hold reader locks concurrently. On the contrary, the locks used for writing can only be held by one write task, and concurrent read operations cannot be performed at this time.

Rwlock_t mr_rwlock = read_lock (& mr_rwlock);/* critical section (read-only) */read_unlock (& mr_rwlock); write_lock (& mr_rwlock);/* critical section (read/write) */write_unlock (& mr_rwlock );

Note: A read lock cannot be upgraded to a write lock.

Read_lock (& mr_rwlock); write_lock (& mr_rwlock );

It will lead to deadlocks, because the write lock will continue to spin and wait for all readers to release the lock, including itself.

This locking mechanism takes care of reading more than writing. When a read lock is held, the reader can only wait for mutex access, but the reader can continue to successfully occupy the lock, the spin wait writer cannot obtain the lock before all readers release the lock. Therefore, a large number of readers will surely make the suspended writers hungry.

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: 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.