Kernel synchronization Glossary and atomic operations

Source: Internet
Author: User

1. Glossary

Applications with shared memory must protect shared resources to prevent concurrent access to shared resources. Kernel is no exception. Shared resources prevent concurrent access because if multiple execution threads access data and operation data at the same time, data sharing may occur between threads, the accessed data is inconsistent.

The so-calledCritical Section(Critical region)It is the code segment for accessing and operating shared data. If two execution threads may be in the same critical section, this is because the program contains a bug. If this happens, we call itCompetitive conditions(Race Condition). Avoid concurrency and prevent competition conditionsSynchronization(Synchronization).

 

Now let's look at the following example:

I ++;

This operation can be converted into a sequence of machine commands similar to the following:

1) obtain the value of the current variable I and copy it to the Register.

2) Add the value in the Register to 1.

3) write the new I value back to the memory.

Assume that two execution threads enter this critical section at the same time. If the initial value of I is 7, the actual execution sequence may be as follows:

Thread 1Thread 2

Obtain I (7 )-

-Obtain I (7)

Add I (7-> 8 )-

-Add I (7-> 8)

Write back I (8 )-

-Write back to I (8)

As a result, the value stored in the variable is 8 instead of the expected value 9.

 

The reason why the user space needs to be synchronized is that the user program will be preemptible and rescheduled by the scheduling program. Even if multiple processes in a single thread share data or process signals in a program (signal processing is sent asynchronously), there may be long competition conditions. This type of concurrent operations does not actually occur at the same time, but they are cross-performed.Pseudo-concurrent execution.

 

If it is on a machine with a multi-processor, the two processes can actually be executed simultaneously in the critical section. This type is calledReal concurrency.

There are similar causes for concurrent execution in the kernel, as follows:

1) interrupt-the interrupt can occur asynchronously at almost any time, and the code being executed may be interrupted at any time.

2) Soft Interrupt and tasklet-internal nuclear power can wake up or schedule Soft Interrupt and tasklet at any time to interrupt the code currently being executed

3) kernel preemption-the kernel may be preemptible by another task.

4) sleep and synchronization with the user space-processes executed in the kernel may be sleep, which will wake up the scheduler and lead to scheduling of a new user process

5) symmetric processor-two or more processors can execute code simultaneously

The locking mechanism can be used to solve concurrency. Locks have a variety of forms, and their granularity ranges are different. The main difference between various lock mechanisms lies in the behavior when the lock is in conflict: some locks will simply execute the busy wait; some locks will make the current task sleep until the lock is available. The Linux Kernel provides a complete set of Synchronization Methods.

 

2. Atomic operations

Atomic operations can ensure that commands are executed in an atomic manner and the execution process is not interrupted. The kernel provides two sets of atomic operation interfaces: one for integers and the other for individual bits.

 

2.1. Atomic Integer Operation

Atomic operations for integers can only process atomic_t data. The Int type of C language is not used here, mainly because:

1) Let the atomic function only accept the atomic_t type operand. It can ensure that the atomic operation is only used with this special type of data.

2) use the atomic_t type to ensure that the compiler does not optimize access to the corresponding values.

3) The atomic_t type can be used to shield Data Types in different architectures.AlthoughLinuxThe integer data on all supported machines is 32Bit, but use atomic_tThe code can only treat this type of data as 24Bits.This restriction is entirely because the implementation of atomic operations is different from that of other systems in the structure of the iSCSI system: A 32-bit int-type low 8-bit locks are embedded, because the iSCSI architecture lacks command-level support for atomic operations, this lock can only be used to avoid concurrent access to atomic data.

 

The most common use of atomic integer operations is to implement counters. The atomic Integer Operation list is defined in <ASM/Atomic. h>. Atomic operations are usually restrained functions, which are often implemented through Embedded Assembly commands. If a function is atomic, it is often defined as a macro.

When writing the kernel, the operation is also simple:

Atomic_t use_cnt;

Atomic_set (& use_cnt, 2 );

Atomic_add (4, & use_cnt );

Atomic_inc (use_cnt );

 

2.2. atomicity and Sequence

Atomicity ensures that commands are not interrupted during execution, either fully or not executed at all. The sequence ensures that even if two or more commands appear in an independent execution thread or even on an independent processor, they should be executed in a proper order.

 

2.3. Atomic bit operation

The atomic bit operation is defined in the file <ASM/bitop. h>.It is strange that bitwise operation functions operate on normal memory addresses.In most cases, the atomic bit operation accesses a long memory, so the bit number is between 0 and 31 (between 0 and 63 on 64-bit machines), but there is no limit on the range of the bit number.

To write the kernel code, you only need to give the operation function a pointer to the data you want, and then you can perform bitwise operations:

Unsigned long word = 0;

Set_bit (0, & Word);/* 0th bits are set */

Set_bit (1, & Word);/* 1st bits are set */

Clear_bit (1, & Word);/* The 1st bits are cleared */

Change_bit (0, & Word);/* flip 0th bits */

For convenience, the kernel also provides a set of non-atomic functions that correspond to the preceding operations.The operation of a non-atomic bitwise function is identical to that of an atomic bitwise function. However, the former does not guarantee atomicity and its name prefix contains two underscores.

The kernel also provides two routines to search for the first set (not set) bit from the address.

Int find_first_bit (unsigned long * ADDR, unsigned int size );

Int find_first_zero_bit (unsigned long * ADDR, unsigned int size );

If the search range is limited to one word, it is better to use the _ FFs () and _ ffz () functions.

 

 

 

 

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.