Linux Kernel(4.19) Hacks

rousalome.egloos.com

포토로그 Kernel Crash


통계 위젯 (화이트)

230224
1178
109352


[LinuxKernel] What is critical section and race condition? 9. Synchronize(spinlock/mutex)

Kernel synchronization is a technique for designing or maintaining code that behaves as follows:

1. Only one process is accessed when executing a function or a specific code segment.
2. Run the code in the specified order

By the way, to properly understand kernel synchronization, you need to know two concepts:
Critical Area
Race condition

This section introduces the main concepts of configuring kernel synchronization. First, find out what the critical areas and race conditions are, then find out why the race conditions occur and analyze the Linux kernel patches associated with the race conditions.

Critical Areas and Race Conditions

A critical section is a block of code that can cause concurrent access problems if two or more processes run simultaneously. That is, the beginning and end of a specific block of code that only one process should execute.

If process A is executing some code segment, assume process B executes the same block of code. This is a situation where process A and process B are simultaneously accessing the critical area. A situation in which two or more processes simultaneously perform some code operation in a critical area is called race or race condition. This is sometimes called a race condition.

Many older operating system books use toilets as an example to help you understand critical areas and race conditions. In this book, we will use the toilet as an example.

Figure 9.1 shows a situation where people A and B enter a public toilet where only one person can enter. Locking the lock on the only bathroom door and seeing business. The numbers in the illustration indicate the actions A and B do before entering the bathroom.

Consider this situation in parallel with the Linux kernel's synchronization process.

First, we look at the bathroom doors and check if they are locked before entering the bathroom to see if someone is already inside. If no one is in the toilet, enter the toilet and lock the toilet door.


Just as there are many types of toilet locks, the Linux kernel has different methods of locking depending on spinlock and mutex techniques.


Second, you can only see one person in the bathroom. Two people can not enter the bathroom at the same time. The only situation where one person sees business is called the critical area of ​​the operating system.

Third, if someone in the bathroom is watching you, wait outside. But the way you wait in the bathroom varies from person to person. A person in a hurry doesn't do anything else and keeps waiting with his feet rolling outside. Some wait leisurely while looking at the magazine outside the bathroom.


In the case of spinlocks in the Linux kernel, if someone has already acquired a spinlock, it will continue to wait without doing anything outside the bathroom. This is called Busy-wait. Instead, the mutex adds itself to the queue and falls asleep outside the bathroom.


Sometimes a process needs to execute a specific section of code, just as only one person needs to go into the toilet to see it. This is called the critical area. The interval indicated by the arrow in Figure 9.1 is the critical area, which can be explained as follows:

Only one person in the bathroom should enter and watch the business. Only one process should run a particular code segment.

You can clean up the critical area as above. If two people (processes) approach a critical zone at the same time, unexpected problems can occur.


This time, let's learn about Race. In Figure 9.1, A did not properly lock the lock on the toilet door before seeing business. After B, the toilet door is not locked, so there is no person inside the toilet. Eventually, two people enter the bathroom together. A race is a situation where two processes are approaching a critical area at the same time as two people enter the bathroom together. This condition is called a race condition.

"There are two people in the bathroom at the same time."
"Two processes run in a specific code segment."

Then in this condition, the race condition can be said as follows.

"A did not lock the toilet door properly."
"The process didn't lock on a particular code segment."

How can you prevent two people from entering the bathroom? Lock the toilet locks well. The same is true in the kernel. Only one process should run in the critical section to lock properly to avoid race conditions.
 

핑백

덧글

댓글 입력 영역