SEQ locks and barrier for Kernel Synchronization

Source: Internet
Author: User

5. Complete the variable

If one task in the kernel needs to send a signal to notify another task of a specific event, using the completion variable is a simple way to synchronize the two tasks.

The completion variable is represented by the structure completion, in <Linux/completion. h>. Static initialization:

Declare_copletion (my_comp );

Dynamic initialization during runtime:

Int_completion (my_comp );

On a specified completion variable, the task to wait for calls wait_for_completion () to wait for a specific event. When a specific event occurs, the task that generates the event calls completion () to send a signal to wake up the waiting task. For examples of variable completion, refer to the kernel/sched. C and/kernel/fork. c files.

 

6. seq lock

SEQ locks are new locks introduced in the 2.6 kernel. To implement this lock, a Sequence Counter is primarily used. When suspicious data is written, a lock is obtained and the sequence value increases. The serial number is read before and after the data is read. If the read serial number value is the same, it indicates that the read operation has not been interrupted by the write operation. If the read value is an even number, it indicates that the write operation has not occurred (because the initial value of the lock is 0, the write lock will make the value odd and the value even when it is released ). The interface is defined in <Linux/seqlock. h>.

Example:

Seqlock_t my_seq = seqlock_unlocked;

Write_seqlock (& my_seq );

/* Write data */

Write_sequnlock (& my_seq );

This is similar to the general spin lock, but the difference is:

Unsigned long seq;

Do {

SEQ = read_seqbegin (& my_seq );

/* Read data */

} While (read_seqretry (& my_seq, SEQ ));

The reader enters the critical section by obtaining an integer ordered value. When you exit, the sequential value is compared with the current value. If the value is not the same, you must retry read access.

 

SeqThe lock is more advantageous to the writer. As long as there are no other writers, the write lock can always be obtained successfully. The suspended writer continuously loops the reader until no writer holds the lock. SeqThe lock is usually not used to protect the data structure containing pointers, because when the writer modifies the data structure pointer, the reader may follow an invalid pointer.

 

7. RCU (read-copy-Update)

Read-copy-Update (RCU) is also an advanced mutex mechanism. Under the correct conditions, it also gets high performance.It is optimized for situations where frequent reads and few writes are made. Protected Resources should be accessed through pointers, and references to these resources must be owned only by atomic code. When you need to modify the data structure, the writing thread first copies the data, then modifies the copy, and then replaces the related pointer with a new version.If you are sure that the old version has no other reference, release the old version.

Use the instance's network route table and starmode rf ip driver in the kernel. The code for using RCU should contain <Linux/rcupdate. h>.

On the Reading end, when the Code uses the data structure protected by RCU, the code that must reference the data structure is included between the rcu_read_lock and rcu_read_unlock calls:

Struct data_buff * my_buff;

Rcu_read_lock ();

Do_something_with (my_buff );

Rcu_read_unlock ();

The rcu_read_lock function is called very quickly. It will disable kernel preemption, but will not wait for anything. The code used to read the "Lock" must be atomic.Before callingRcu_read_unlockIn the future, no reference should be made to the data structure protected by the opponent.

Code for modifying the RCU-protected data structure must assign a struct rcu_head data structure to obtain the clearing callback function. After modifying the resource, make the following calls:

Void call_rcu (struct rcu_head * head, void (* func) (void * Arg), void * Arg );

When the resource can be safely released, the given func will be called. func usually calls kfree.

 

8. preemption prohibited

We know that the kernel cannot be preemptible if a spin lock is held. However, in some cases, spin locks are not required, but kernel preemption must be disabled. For example, data on each processor. If the data is unique to each processor, such data may not need to be protected by locks because the data can only be accessed by one processor. If the spin lock is not held and the kernel is preemptible, a new scheduled task may access a variable.

To solve this problem, you can use preempt_disable () to disable kernel preemption. This is a nested function that can be called any time. Each call must have a corresponding preempt_enable () call. After the last preempt_enable () is called, kernel preemption is enabled again. For example:

Preempt_disable ();

/* Access the unique data of the current processor */

Preempt_enable ();

The preemption count stores the number of locks held and the number of preempt_disable () function calls. If the count is 0, the kernel will be preemptible. If it is 1 or greater, then the kernel will not be preemptible.

 

9. Order and barrier

When the processor interacts with the hardware, it is often necessary to ensure that a given read operation occurs before other read or write operations. On a multi-processor, you may need to read data in the order of writing data. However, the compiler and processor may re-sort the read and write operations to improve efficiency. However, the processor provides machine commands to ensure the order and instructs the compiler not to re-sort the command sequences around the specified point. These commands that ensure the order are called barrier ).

If there is no direct data dependency between the preceding and following commands, they will be re-ordered by the processor. However, in the following example, the processor and compiler will not re-order the commands:

A = 1;

B =;

The RMB () function provides a "read" memory barrier, which ensures that the loading actions that span RMB () are not reordered. That is to say, the loading operation before RMB () will not be placed after the call again.

 

The function WMB () provides a "write" memory barrier. The difference with RMB () is that it only targets storage rather than loading.

Function MB () provides both read and write barrier.

Let's take a look at the example below. The initial value of A is 1, and the initial value of B is 2:

Thread 1Thread 2

A = 3 ;-

MB ();_

B = 4; C = B;

-RMB ();

-D =;

If the memory barrier is not used, C may accept B's new value (4) on some processors, and D accepts a's original value (1 ).

Macros smp_ RMB (), smp_wmb (), smp_mb (), and smp_read_barrier_depends () provide a useful optimization. In the SMP kernel they are defined as common memory barriers, while in the single processor kernel they are defined as compiler barriers. Method barrier () can prevent the compiler from cracking the barrier to optimize loading or storage operations. The compiler does not reorganize the storage or loading operations to prevent the dependency between the effect of the C code and the existing data from being changed. The memory barrier discussed earlier can implement the compiler barrier function, but the compiler barrier is much lighter than the memory barrier (which is actually brisk ).

 

 

 

 

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.