A user-level RCU implementation method for a microkernel operating system
By deploying RCU services and bitmap variables in the user mode of the microkernel operating system to monitor the CPU static state, the problem of low operating efficiency and blocking of user mode RCU is solved, and efficient shared data update and memory resource recycling are achieved.
Patent Information
- Application Number
- CN202311871108.3
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2023-12-29
- Publication Date
- 2024-09-24
- Estimated Expiration
- 2043-12-29
AI Technical Summary
The existing technology is difficult to implement efficient Read-Copy Update (RCU) operations in user mode, is prone to blocking, and is difficult to effectively manage shared data in a multi-threaded environment.
Deploy the RCU service in the user mode of the microkernel operating system, use the rcu_read_lock and rcu_read_unlock functions to lock and unlock the read operation, the write thread copies to the new memory space when updating the shared data, and opens the grace period through the synchronize_rcu or call_rcu function, the kernel Set bitmap variables to monitor CPU quiescence status and generate quiescence events to reclaim memory resources.
It realizes the efficient operation of RCU in user mode, allowing the writing thread to continue running immediately after changing the data, reducing blocking, improving the efficiency of RCU, and effectively recycling memory resources through the callback function.
Smart Images

Figure CN117785496B
Abstract
Description
Technical Field
[0001] The present invention belongs to the field of microkernel technology, and more specifically, relates to a user-level RCU implementation method of a microkernel operating system. Background Art
[0002] Read-Copy Update (RCU) is a concurrent programming technique for managing shared data in a multi-threaded environment. It aims to provide high-performance and low-overhead concurrent read access while ensuring data consistency without using traditional mutex locks. The main data object of RCU is the linked list, and the purpose is to improve the efficiency of traversing and reading data. In order to achieve this goal, the linked list is not time-consuming to lock when reading data using the RCU mechanism. In this way, multiple threads can read the linked list at the same time, and one thread is allowed to modify the linked list (locking is required when modifying). When shared data needs to be updated, the original data is not modified immediately, but a new copy of the data is created. This new copy will become the new primary data only when no thread references the old copy. The old copy of the data is not destroyed immediately, but is recycled with delay.
[0003] There are many ways to implement RCU, but there are still problems such as the inability to implement user-mode applications or easy blocking, which need further improvement. Summary of the invention
[0004] The purpose of the present invention is to overcome the deficiencies of the prior art and provide a method for implementing user-level RCU in a microkernel operating system, so as to implement RCU in the user state of the microkernel operating system and improve RCU efficiency.
[0005] In order to achieve the above-mentioned object of the invention, the user-level RCU implementation method of the microkernel operating system of the present invention comprises the following steps:
[0006] S1: Deploy the RCU service in the user state of the microkernel operating system and configure the operation interface. The operation interface functions include the read lock function rcu_read_lock, the read unlock function rcu_read_unlock, the release function rcu_assign_pointer, the synchronization function synchronize_rcu and the callback function call_rcu;
[0007] S2: When the read thread accesses shared data, the rcu_read_lock function and rcu_read_unlock function are used to lock and unlock the read operation;
[0008] S3: When the content of a shared data needs to be changed, the write thread applies for a new memory space from the RCU service, copies the original shared data to the new memory space, and then changes the data in the new memory space. After the change is completed, the write thread transfers the link relationship of the original memory space in the data linked list to the new memory space to complete the shared data replacement;
[0009] S4: After the shared data is updated, the write thread calls the synchronize_rcu function or the call_rcu function to register the callback function to start the grace period; the write thread assigns a unique generation number to the grace period and saves the callback function and pointer to the callback function queue; then the RCU service is blocked and the write thread continues to run;
[0010] S5: A bitmap variable is set in the kernel, each bit corresponds to a CPU in the microkernel operating system, and the initial value of the bit value corresponding to each CPU is 1; the kernel monitors each CPU to determine whether the CPU has experienced a quiescent state. If not, no operation is performed. If so, the corresponding bit in the bitmap variable is set to 0; when the bitmap variable is all 0, the grace period ends, a quiescent event is generated, and then the bitmap variable is reset to 1; then the quiescent event is reported to the RCU service;
[0011] S6: The RCU service receives a static event, takes out the callback function and data pointer corresponding to the grace period from the callback function queue, and calls the callback function to reclaim memory resources.
[0012] The user-level RCU implementation method of the microkernel operating system of the present invention deploys an RCU service in the user state of the microkernel operating system, performs locking and unlocking operations for the read operation when the read thread accesses shared data, and after the write thread changes the shared data, starts a global grace period and allocates a generation number for it. The kernel sets a bit of a bitmap variable for each CPU, and the CPU sets the bit to 0 after experiencing a static state. When the bitmap variable is converted to a 0 vector, it indicates that the corresponding grace period ends, and the kernel sends a static event to the RCU service, and the RCU service calls a callback function corresponding to the grace period to recycle memory resources.
[0013] The present invention has the following beneficial effects:
[0014] 1) The present invention provides an implementation of RCU in user mode, which broadens the usage scenarios of RCU;
[0015] 2) In the present invention, the write thread can continue to run directly after the grace period is opened for changing the data, without waiting for the grace period to end. Afterwards, the memory space of the old data is released through the callback function, which effectively reduces the blocking and greatly improves the efficiency of RCU. BRIEF DESCRIPTION OF THE DRAWINGS
[0016] Figure 1 It is a flowchart of a specific implementation method of the user-level RCU implementation method of the microkernel operating system of the present invention;
[0017] Figure 2 is a schematic diagram of the shared data replacement operation;
[0018] Figure 3 This is an example diagram of the RCU implementation process in this embodiment. DETAILED DESCRIPTION
[0019] The specific implementation of the present invention is described below in conjunction with the accompanying drawings so that those skilled in the art can better understand the present invention. It should be noted that in the following description, when the detailed description of known functions and designs may dilute the main content of the present invention, these descriptions will be omitted here.
[0020] Example
[0021] Figure 1 1 is a flowchart of a specific implementation method of the user-level RCU implementation method of the microkernel operating system of the present invention. Figure 1 As shown, the specific steps of the user-level RCU implementation method of the microkernel operating system of the present invention include:
[0022] S101: Deploy RCU service:
[0023] Deploy the RCU service in the user state of the microkernel operating system and configure the operation interface. The operation interface functions include the read lock function rcu_read_lock, the read unlock function rcu_read_unlock, the release function rcu_assign_pointer, the synchronization function synchronize_rcu and the callback function call_rcu.
[0024] S102: Read thread accesses shared data:
[0025] When the reading thread accesses shared data, the rcu_read_lock function and rcu_read_unlock function are used to lock and unlock the read operation. When the reading thread reads data, the rcu_read_lock function is called to lock. There will be a memory barrier here to avoid the pipeline memory reading order. In addition, this function will also prohibit interrupts, otherwise the program will enter the kernel state and cause confusion in recorded information. The locking here only indicates that the thread occupies resources, and there is no lock primitive. When the reading thread ends the reading operation, the rcu_read_unlock function is called to unlock, which will turn on interrupts and generate a memory barrier. The unlocking here only indicates that the thread releases resources, and there is no lock primitive.
[0026] S103: The write thread performs a replacement operation on the shared data:
[0027] When the content of a shared data needs to be changed, the write thread applies for a new memory space, copies the original shared data to the new memory space, and then changes the data in the new memory space. After the change is completed, the write thread transfers the link relationship of the data linked list to the original memory space to the new memory space to complete the shared data replacement. This is an atomic operation to prevent the read thread from accessing data that has not been changed. The read thread before the change operation can only access the old data, and the read thread after the change operation can only access the new data. In any case, the read thread can only access the old data or the new data, and it is impossible to access data in an intermediate state.
[0028] Figure 2 is a schematic diagram of the shared data replacement operation. Figure 2 As shown, thread 1 is reading shared data B. When thread 2 needs to modify shared data B, it first applies for new memory space to copy the data and obtain shared data B' after modification. Then, the link relationship between data A and data C and the original shared data B is transferred to shared data B', thereby completing the replacement of shared data without affecting the read operation of thread 1. The replacement operation needs to ensure atomicity, because the replacement operation does not take too long, so in this embodiment, the write thread uses a spin lock for synchronization instead of a mutex lock.
[0029] S104: Open grace period:
[0030] In the RCU synchronization mechanism, the grace period is an important part of achieving data synchronization. The grace period refers to the time interval during which each CPU experiences at least one quiescent state. (Note that any time interval that includes a grace period is itself a grace period.) After the shared data update is completed, the write thread calls the synchronize_rcu function or the call_rcu function to register the callback function to start the grace period. Since there may be multiple grace periods at the same time in the RCU service, the start time and end time of each grace period are not necessarily the same. In order to distinguish the grace periods, a unique generation number is assigned to each grace period, and the grace period status and callback function can be quickly found through the generation number. Therefore, in the present invention, the write thread assigns a unique generation number to the grace period, and saves the callback function and pointer to the callback function queue. Then the RCU service is blocked, and the write thread continues to run.
[0031] S105: The kernel reports a static event:
[0032] A bitmap variable is set in the kernel, and each bit corresponds to a CPU in the microkernel operating system. The initial value of the bit value corresponding to each CPU is 1. The kernel monitors each CPU to determine whether the CPU has experienced a quiescent state. If not, no operation is performed. If it has, the corresponding bit in the bitmap variable is set to 0. Under a multi-core CPU, the grace period can only end when all CPUs have experienced a quiescent state. Otherwise, it is considered that there are still read threads currently referencing old data. Therefore, when the bitmap variable is all 0, the grace period ends, a quiescent event is generated, and then the bitmap variable is reset to 1. The quiescent event is then reported to the RCU service.
[0033] In this embodiment, the CPU static state is determined as follows: if the CPU is in an idle thread or the thread is executed in the kernel state, the CPU is in a static state. The thread execution in the kernel state generally includes system calls and interrupt exceptions. This is because the reading thread will not enter the kernel state during the reading process. When the reading thread enters the kernel state, it means that the data has been read.
[0034] In addition, to prevent the kernel from sending events to the RCU service frequently, a static event counter can be set in the kernel. Each time the bitmap variable returns to 0, the counter is incremented by 1. When the counter value reaches the preset threshold, the kernel reports the static event to the RCU service, otherwise no action is taken. Because the RCU service originally delays the recovery of memory resources, the above operations will not affect the performance of the RCU.
[0035] S106: Reclaiming memory resources:
[0036] The RCU service receives a static event, takes out the callback function and data pointer corresponding to the grace period from the callback function queue, and calls the callback function to reclaim memory resources.
[0037] In the Linux operating system, a callback function is called by using a soft interrupt or the like, while the present invention directly performs the operation by using an RCU service, calling the callback function of the ended grace period, and reclaiming the memory space where the original data is located, which is more convenient.
[0038] Figure 3 is an example diagram of the RCU implementation process in this embodiment. Figure 3As shown, suppose that when the writing process modifies the shared data, readers 1, 2, 3, and 4 are reading the shared data. When the grace period is turned on, reader 2 completes the reading, and readers 1, 3, and 4 are still reading. At this time, the data in the original memory space is still read. During the grace period, readers 6, 7, and 8 initiate a read thread, and at this time, the data in the new memory space is read. Readers 1, 3, and 4 complete the reading one after another, and the corresponding CPU enters a static state. CPU3 also experiences a static state during the grace period, and the kernel sets the corresponding bitmap vector bit position to 0. After CPU4 corresponding to reader 4 reports the event, the bitmap vector is converted to a 0 vector, and the kernel reports the static event to the RCU service, and the RCU service reclaims the memory space where the original data is located.
[0039] Although the above describes the illustrative specific embodiments of the present invention to facilitate those skilled in the art to understand the present invention, it should be clear that the present invention is not limited to the scope of the specific embodiments. For those of ordinary skill in the art, as long as various changes are within the spirit and scope of the present invention as defined and determined by the attached claims, these changes are obvious, and all inventions and creations using the concept of the present invention are protected.
Claims
1. A user-level RCU implementation method for a microkernel operating system, characterized in that: The following steps are involved: S1: Deploy the RCU service in the user state of the microkernel operating system and configure the operation interface. The operation interface functions include the read lock function rcu_read_lock, the read unlock function rcu_read_unlock, the release function rcu_assign_pointer, the synchronization function synchronize_rcu and the callback function call_rcu; S2: When the read thread accesses shared data, the rcu_read_lock function and rcu_read_unlock function are used to lock and unlock the read operation; S3: When the content of a shared data needs to be changed, the write thread applies for a new memory space from the RCU service, copies the original shared data to the new memory space, and then changes the data in the new memory space. After the change is completed, the write thread transfers the link relationship of the original memory space in the data linked list to the new memory space to complete the shared data replacement; S4: After the shared data is updated, the write thread calls the synchronize_rcu function or the call_rcu function to register the callback function to start the grace period; the write thread assigns a unique generation number to the grace period and saves the callback function and pointer to the callback function queue; then the RCU service is blocked and the write thread continues to run; S5: a bitmap variable is set in the kernel, each bit corresponds to a CPU in the microkernel operating system, and the initial value of the bit value corresponding to each CPU is 1; the kernel monitors each CPU to determine whether the CPU has experienced a quiescent state, and the judgment method is as follows: if the CPU is in an idle thread or the thread is executed in the kernel state, the CPU is in a quiescent state, if it has not experienced it, no operation is performed, and if it has experienced it, the corresponding bit in the bitmap variable is set to 0; when the bitmap variable is all 0, the grace period ends, a quiescent event is generated, and then the bitmap variable is reset to 1; then the quiescent event is reported to the RCU service, and the specific method is as follows: a quiescent event counter is set in the kernel, and each time the bitmap variable returns to 0, the counter is incremented by 1, and when the counter value reaches the preset threshold, the kernel reports the quiescent event to the RCU service, otherwise no operation is performed; S6: The RCU service receives a static event, takes out the callback function and data pointer corresponding to the grace period from the callback function queue, and calls the callback function to reclaim memory resources.
