A page fault exception handling method, apparatus, device and storage medium
By judging the cleared mark of the free memory page during the virtual machine startup process, the time-consuming clearing operation of the memory page is skipped, which solves the problem of extended virtual machine startup time and improves the startup speed of the virtual machine.
Patent Information
- Application Number
- CN202211025832.X
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2022-08-25
- Publication Date
- 2025-07-08
- Estimated Expiration
- 2042-08-25
AI Technical Summary
During startup, the virtual machine has a page-free exception caused by physical device transmission, which leads to a prolonged startup time, especially when large-capacity memory is configured to affect the user experience.
During the virtual machine startup process, determine whether the free memory page has been set to clear the mark. If it has been set, skip the memory page clearing operation and directly use the free memory page to map physical memory for virtual memory.
It effectively reduces the page-missing execution time during virtual machine startup, improves the startup speed of virtual machine, and is particularly effective in large-capacity memory configuration.
Smart Images

Figure CN115408105B_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the field of computer technologies, and particularly relates to a page fault exception handling method, apparatus, device, and storage medium. Background Art
[0002] Currently, in order to obtain better hardware performance in virtual machines, physical devices are directly passed through to the inside of the virtual machines. For example, after passing through the GPU (Graphics Processing Unit) and network card devices to the inside of the virtual machine through the VFIO (Virtual Function I / O, that is, I / O virtualization) technology, the same performance of the GPU and network card on the HOST (i.e., the host) can be obtained on the virtual machine. And generally, a relatively large amount of memory is also specified for the virtual machine now, which will cause the startup time of the virtual machine with the IO passthrough device added to be significantly longer than that of the virtual machine without the IO passthrough device. For example, for virtual machines with the same 1TB of memory allocated, the startup time of the virtual machine with GPU passthrough will be extended by several minutes compared to the virtual machine without GPU passthrough. This seriously affects the user experience in some scenarios that are sensitive to the startup time of the virtual machine. Since the physical address inside the virtual machine is a virtual address block applied by the QEMU (i.e., emulated processor) process, in order to enable the driver process inside the virtual machine to map to the same physical address every time it accesses the same memory, QEMU performs an iommu map operation on the entire memory allocated for the virtual machine. This process pins the entire virtual memory allocated for the virtual machine, and then performs an iommu map on the virtual memory and the physical memory. Among them, the function of the iommu is similar to the mmu function of the cpu. The iommu can convert the address accessed by the device into a memory address when the device performs DMA (Direct Memory Access). The pin operation is to bind the relationship between the virtual memory and the physical memory, so that the physical memory corresponding to the virtual memory will not change due to memory recycling and migration. If the virtual address is not mapped to the physical address during this process, a page fault will be triggered. For example, if the virtual machine memory size is 1TB, the QEMU process will perform a pin operation on this 1TB of memory. During the pinning process, a page fault will be triggered to allocate physical memory for the virtual memory. However, there is a very time-consuming operation during the page fault process, which is the clearing of the memory. Therefore, performing the clearing of the memory pages during the pinning of the memory is an important reason for the extension of the virtual machine startup time. That is to say, when passing through the physical device to the inside of the virtual machine, it is necessary to enable the driver process inside the virtual machine to map to the same physical address every time it accesses the same memory. Therefore, when allocating memory for the virtual machine, an iommu map operation can be performed. When performing this operation, it is necessary to first pin the memory allocated for the virtual machine. During the pinning process, a page fault will be triggered to allocate physical memory for the virtual machine memory. There is an operation of clearing the memory during the page fault process, and this operation is very time-consuming, so the startup time of the virtual machine is extended. Summary of the Invention
[0003] In view of this, an object of the present invention is to provide a page fault handling method, apparatus, device, and storage medium, which can reduce the page fault execution time triggered during the virtual machine startup process, thereby improving the virtual machine startup speed. The specific solution is as follows:
[0004] In a first aspect, the present application discloses a page fault handling method, including:
[0005] During the startup process of the target process in the target virtual machine, determine whether a page fault is currently triggered;
[0006] If the page fault is currently triggered, obtain free memory pages from different storage spaces according to whether the target virtual machine uses standard large pages, and determine whether the free memory pages are set with a cleared flag;
[0007] If the free memory pages are set with the cleared flag, skip the memory page clearing operation, and use the free memory pages to map physical memory for the virtual memory of the target process.
[0008] Optionally, before determining whether a page fault is currently triggered, it further includes:
[0009] For the case where the target virtual machine does not use standard large pages, use the free page tracking mechanism of the Linux kernel to perform a pre-clearing operation on the free memory pages in the obtained Buddy memory at a first preset period, and set a corresponding cleared flag on the target flag bit corresponding to the free memory pages;
[0010] Correspondingly, obtaining free memory pages from different storage spaces according to whether the target virtual machine uses standard large pages, and determining whether the free memory pages are set with a cleared flag includes:
[0011] For the case where the target virtual machine does not use standard large pages, if the page fault is currently triggered, obtain the free memory pages from the Buddy memory, and determine whether the cleared flag is set on the target flag bit corresponding to the free memory pages.
[0012] Optionally, using the free page tracking mechanism of the Linux kernel to perform a pre-clearing operation on the free memory pages in the obtained Buddy memory at a first preset period includes:
[0013] Register a free page clearing processing function based on the free page tracking mechanism of the Linux kernel;
[0014] Obtain each free memory page corresponding to the free memory block in the Buddy memory at a first preset period through the free page tracking mechanism;
[0015] Execute a pre-clearing operation on each of the free memory pages using the free page zeroing processing function.
[0016] Optionally, before determining whether a page fault exception is currently triggered, it further includes:
[0017] For the case where the target virtual machine uses standard large pages, perform a pre-clearing operation on the free large page memory in the obtained large page memory pool at a second preset period through the created kernel thread, and set a corresponding cleared flag on the target flag bit corresponding to the free large page memory;
[0018] Correspondingly, obtaining free memory pages from different storage spaces according to whether the target virtual machine uses standard large pages, and determining whether a cleared flag is set on the free memory pages includes:
[0019] For the case where the target virtual machine uses standard large pages, if the page fault exception is currently triggered, obtain the free large page memory from the large page memory pool, and determine whether the cleared flag is set on the target flag bit corresponding to the free large page memory.
[0020] Optionally, the performing a pre-clearing operation on the free large page memory in the obtained large page memory pool at a second preset period through the created kernel thread includes:
[0021] Determine whether there is free large page memory in the current large page memory pool at a second preset period through the created kernel thread;
[0022] If there is free large page memory in the current large page memory pool, obtain the free large page memory, and perform a pre-clearing operation on each free memory page corresponding to the free large page memory.
[0023] Optionally, before determining whether there is free large page memory in the current large page memory pool at a second preset period through the created kernel thread, it includes:
[0024] Determine whether there is currently a process applying for large page memory in the current large page memory pool during a page fault exception;
[0025] If there is currently no such process applying for the large page memory in the current large page memory pool during a page fault exception, trigger the step of determining whether there is free large page memory in the current large page memory pool at a second preset period through the created kernel thread;
[0026] If there is currently a process applying for the large page memory in the current large page memory pool during a page fault exception, wait for a preset time and then re - execute the step of determining whether there is currently a process applying for the large page memory in the current large page memory pool during a page fault exception until there is no such process currently applying for the large page memory in the current large page memory pool during a page fault exception.
[0027] Optionally, after obtaining the free large page memory, it further includes:
[0028] Set the target state of the free large page memory to 1;
[0029] Correspondingly, after performing the pre - clearing operation on each free memory page corresponding to the free large page memory, it further includes:
[0030] Set the target state of the free large page memory to 0.
[0031] In a second aspect, the present application discloses a page fault exception handling device, including:
[0032] A first judgment module, configured to judge whether a page fault exception is currently triggered during the startup of a target process in a target virtual machine;
[0033] A free page acquisition module, configured to, when the page fault exception is currently triggered, acquire free memory pages from different storage spaces according to whether the target virtual machine uses standard large pages;
[0034] A second judgment module, configured to judge whether the free memory page is set with a cleared flag;
[0035] A memory clearing control module, configured to, when the free memory page is set with the cleared flag, skip the memory page clearing operation and use the free memory page to map physical memory for the virtual memory of the target process.
[0036] In a third aspect, the present application discloses an electronic device, including:
[0037] A memory, configured to save computer processes;
[0038] A processor, configured to execute the computer processes to implement the steps of the foregoing disclosed page fault exception handling method.
[0039] In a fourth aspect, the present application discloses a computer - readable storage medium, configured to store computer processes; wherein, when the computer processes are executed by a processor, the steps of the foregoing disclosed page fault exception handling method are implemented.
[0040] It can be seen that the present application provides a page fault handling method, including: during the startup process of a target process in a target virtual machine, determining whether a page fault is currently triggered; if the page fault is currently triggered, obtaining a free memory page from different storage spaces according to whether the target virtual machine uses standard large pages, and determining whether the free memory page is set with a cleared flag; if the free memory page is set with the cleared flag, skipping the memory page clearing operation, and using the free memory page to map physical memory for the virtual memory of the target process. It can be seen from this that by pre-clearing the free memory page and setting the relevant cleared flag in the present application, when a page fault occurs to allocate physical memory for the virtual memory of the target process, it is possible to first determine whether the obtained free memory page is set with the cleared flag. If the free memory page is set with the cleared flag, it is possible to avoid the time-consuming memory page clearing operation during the startup process of the virtual machine, thereby reducing the page fault execution time triggered during the startup process of the virtual machine and improving the startup speed of the virtual machine. BRIEF DESCRIPTION OF THE DRAWINGS
[0041] In order to more clearly illustrate the technical solutions in the embodiments of the present invention or the prior art, the following will briefly introduce the drawings required for use in the description of the embodiments or the prior art. Obviously, the following drawings are only the embodiments of the present invention. For those of ordinary skill in the art, other drawings can be obtained according to the provided drawings without creative efforts.
[0042] Figure 1 It is a flowchart of a page fault handling method disclosed in the present application;
[0043] Figure 2 It is a schematic diagram of a struct page data structure disclosed in the present application;
[0044] Figure 3 It is a flowchart of a specific page fault handling method disclosed in the present application;
[0045] Figure 4 It is a flowchart of a specific free memory page pre-clearing method disclosed in the present application;
[0046] Figure 5 It is a schematic diagram of a free memory page traversal method disclosed in the present application;
[0047] Figure 6 It is a flowchart of a specific page fault handling method disclosed in the present application;
[0048] Figure 7 It is a flowchart of another specific memory page pre-clearing method disclosed in the present application;
[0049] Figure 8 A flowchart of a specific page fault exception handling method disclosed in this application;
[0050] Figure 9 A schematic structural diagram of a page fault exception handling device disclosed in this application;
[0051] Figure 10 A structural diagram of an electronic device disclosed in this application. Specific implementation manners
[0052] Next, the technical solutions in the embodiments of the present invention will be clearly and completely described in conjunction with the accompanying drawings in the embodiments of the present invention. Obviously, the described embodiments are only a part of the embodiments of the present invention, rather than all the embodiments. All other embodiments obtained by those of ordinary skill in the art based on the embodiments of the present invention without creative efforts shall fall within the protection scope of the present invention.
[0053] Currently, in order to obtain better hardware performance in a virtual machine, physical devices are passed through to the inside of the virtual machine. However, after passing the physical devices through to the inside of the virtual machine, in order to enable the driver process inside the virtual machine to map to the same physical address every time it accesses the same memory when the virtual machine starts up, it is necessary to perform a pin operation on the entire allocated memory, and then perform an iommu map on the virtual memory and the physical memory. A page fault will be triggered during the pin operation. However, there is a very time-consuming memory zeroing operation during the page fault process, and this operation is very time-consuming, which will increase the startup time of the virtual machine. For this reason, this application provides a page fault exception handling solution, which can reduce the execution time of the page fault triggered during the startup process of the virtual machine, thereby improving the startup speed of the virtual machine.
[0054] An embodiment of the present invention discloses a page fault exception handling method. Refer to Figure 1 As shown, the method includes:
[0055] Step S11: During the startup process of a target process in a target virtual machine, determine whether a page fault exception is currently triggered.
[0056] In this embodiment, a page fault exception is an exception that occurs during the pinning process. During the iommu map operation on the entire memory allocated by QEMU (simulated processor) for the virtual machine, a pinning operation is performed on the entire virtual memory allocated for the virtual machine. Therefore, during the startup process of the target process in the target virtual machine, in order to ensure that the driver process inside the virtual machine can map to the same physical address each time it accesses the same memory, QEMU will perform the iommu map operation on the entire memory allocated for the virtual machine. Therefore, when the pinning operation of the memory is triggered during the startup of the QEMU process of the virtual machine that supports IO passthrough devices, it is necessary to determine whether a page fault exception is currently triggered. It should be noted that when an application allocates memory, such as when calling mmap to apply for memory, only virtual memory is allocated for the application. Only when the program accesses the applied memory will the kernel trigger a page fault exception, and only then will the virtual memory of the application be mapped to physical memory.
[0057] Step S12: If the current page fault exception is triggered, obtain free memory pages from different storage spaces according to whether the target virtual machine uses standard large pages, and determine whether the free memory pages are set with a zeroed flag.
[0058] It should be noted that free memory is divided into two cases. One is the free memory in the buddy. Ordinary memory and transparent huge page memory used by the program are both allocated in the buddy memory. The other is the free memory in the huge page memory pool, which will be allocated in the huge page memory pool when standard large pages are used. Among them, a large page, that is, a page, is the basic unit for Linux to manage memory. In the X86 architecture, it is generally 4KB, but 2M / 1G large pages can also be used. The advantage of using large pages is to increase the hit rate of the TLB and improve the memory access performance. When using the standard large pages, a certain number of physically contiguous ordinary pages need to be applied from the system and stored in the huge page memory pool first. Subsequently, when the program uses the standard large pages, it can apply in the huge page memory pool. Since standard large pages require pre-allocation and the memory allocation interface of the application program needs to be modified, their use is restricted. The transparent huge page is to transparently map large page memory for the virtual address when a page fault occurs in the kernel when the program uses memory. In the buddy memory management system, all physical memory is allocated in the buddy, and the allocated memory is physically contiguous. In the x86 architecture, the maximum memory that can be allocated from the buddy is 4M.
[0059] In this embodiment, the free memory pages of the system are pre-cleared in advance and corresponding cleared flags are set on the free memory pages. Moreover, in order to reduce the impact of the memory pre-clearing operation on the system performance, it is only executed when the CPU is idle. Therefore, when the QEMU process of the virtual machine supporting the IO passthrough device is started, it will trigger the pin operation of the memory, and then enter the page fault exception. When the kernel has a page fault exception and applies for physical memory, that is, when the current page fault exception is triggered to allocate physical memory for the virtual memory of the QEMU process, after obtaining the free memory page from the linked list of the system free memory according to whether the target virtual machine uses the standard large page, it is judged whether the free memory page is set with the cleared flag. For example, the struct page data structure is used in the kernel to describe a memory page, where page->flags is used to mark the status of the page. A new flag is added to identify whether the current memory page has been cleared, that is, when the PG_clean bit of page->flags is set to 1, it means that the current memory page has been cleared, and if it is not set to 1, it means that the current memory page has not been cleared. The relevant code is shown as follows:
[0060]
[0061] Among them, if the memory page that needs to be pre-cleared in advance is a standard large page, then the flags in the first page data structure of the standard large page are used to identify whether the entire standard large page has been cleared. As Figure 2 shown, the 2M standard large page uses the flags in the first page to identify whether the standard large page has been cleared.
[0062] Step S13: If the free memory page is set with the cleared flag, skip the memory page clearing operation and use the free memory page to map physical memory for the virtual memory of the target process.
[0063] In this embodiment, when judging whether the free memory page is set with the cleared flag, if the free memory page is set with the cleared flag, it means that the free memory page has been pre-cleared in advance. Directly skip the memory page clearing operation for the free memory page and directly apply for physical memory, that is, use the free memory page to map physical memory for the virtual memory of the target process, thereby reducing the processing time of the page fault exception.
[0064] It can be seen that in the embodiments of the present application, by clearing the idle memory pages in advance and setting relevant cleared flags, when a page fault exception occurs to allocate physical memory for the virtual memory mapping of the target process, it is possible to first determine whether the obtained idle memory page is set with a cleared flag. If the idle memory page is set with the cleared flag, it is possible to avoid the time-consuming memory page clearing operation during the startup of the virtual machine, thereby reducing the page fault execution time triggered during the startup of the virtual machine, effectively reducing the startup time of the virtual machine supporting device passthrough, improving the startup speed of the virtual machine, especially when the virtual machine is configured with a large amount of memory, the effect is more obvious. The technical solution of the present application can also be applied to scenarios where users are sensitive to the startup time of virtual machines in virtualization scenarios.
[0065] See Figure 3 As shown, the embodiments of the present invention disclose a specific page fault exception handling method. Compared with the previous embodiment, this embodiment further explains and optimizes the technical solution.
[0066] Step S21: During the startup of the target process of the target virtual machine, determine whether a page fault exception is currently triggered.
[0067] Step S22: For the case where the target virtual machine does not use standard large pages, if the page fault exception is currently triggered, obtain the idle memory page from the Buddy memory, and determine whether the cleared flag is set on the target flag bit corresponding to the idle memory page.
[0068] It should be noted that the pre-clearing of the idle memory in the buddy can specifically include: for the case where the target virtual machine does not use standard large pages, use the idle page tracking mechanism of the Linux kernel to perform a pre-clearing operation on the obtained idle memory pages in the Buddy memory at a first preset period, and set the corresponding cleared flag on the target flag bit corresponding to the idle memory page. It can be understood that an idle page clearing processing function is registered based on the idle page tracking mechanism of the Linux kernel; each idle memory page corresponding to the idle memory block in the Buddy memory is obtained through the idle page tracking mechanism at a first preset period; the pre-clearing operation is performed on each of the idle memory pages by using the idle page clearing processing function. For example, Figure 4As shown, a free page zeroing handler function clean_free_page is registered through the kernel free page reporting mechanism (i.e., the free page tracking mechanism). The kernel free page reporting mechanism traverses the free memory of a specified order and above every 2 seconds. Herein, the Free page reporting is the free page tracking mechanism in the Linux kernel. According to this free tracking mechanism, the unused pages in the kernel can be reported to the caller periodically, and the caller can process the free pages according to its own needs. For example, as Figure 5 shown, under the X86 architecture, only the memory pages of 2 to the 9th power and 2 to the 10th power in the buddy are traversed, that is, only the memory areas of 2M and 4M are traversed. Then, each piece of memory is removed from the free list and the clean_free_page function is called. The kernel clean_free_page function performs a pre-zeroing operation on each memory page in each memory block. For example, for a 10th-order memory, 1024 memory pages corresponding to each memory block need to be zeroed. After the zeroing, the flag of the page is set to PG_clean. Moreover, since the memory page zeroing operation has a lower priority than other programs in the system, in order not to affect the operation of other business programs in the system, the CPU is relinquished after each memory block is processed to allow other business programs to execute.
[0069] In this embodiment, for the case where the target virtual machine uses standard large pages, since the pre-zeroing process has been performed on the free pages in the Buddy memory in advance, when the QEMU process of the virtual machine supporting the IO passthrough device starts, it will trigger a memory pin operation, and then enter a page fault exception. When the kernel applies for physical memory during a page fault exception, that is, when the page fault exception is currently triggered, the free memory page is obtained from the Buddy memory, and it is determined whether the cleared flag is set on the target flag bit corresponding to the free memory page.
[0070] Step S23: If the cleared flag is set on the free memory page, skip the memory page zeroing operation and use the free memory page to map the physical memory for the virtual memory of the target process.
[0071] For the specific content of the above steps S21 and S23, reference can be made to the corresponding content disclosed in the foregoing embodiments, and details will not be elaborated herein.
[0072] It can be seen that in the embodiments of the present application, by clearing the idle memory pages in advance and setting relevant cleared flags, when a page fault exception occurs to allocate physical memory for the virtual memory mapping of the target process, it is possible to first determine whether the obtained idle memory page is set with a cleared flag. If the idle memory page is set with the cleared flag, it is possible to avoid the time-consuming memory page clearing operation during the startup of the virtual machine, thereby reducing the page fault execution time triggered during the startup of the virtual machine, effectively reducing the startup time of the virtual machine supporting device passthrough, improving the startup speed of the virtual machine, especially when the virtual machine is configured with a large amount of memory, the effect is more obvious. The technical solution of the present application can also be applied to scenarios where users are sensitive to the startup time of virtual machines in virtualization scenarios.
[0073] See Figure 6 As shown, an embodiment of the present invention discloses a specific page fault exception handling method. Compared with the previous embodiment, this embodiment further explains and optimizes the technical solution.
[0074] Step S31: During the startup of the target process of the target virtual machine, determine whether a page fault exception is currently triggered.
[0075] Step S32: For the case where the target virtual machine uses standard large pages, if the page fault exception is currently triggered, obtain the idle large page memory from the large page memory pool, and determine whether the cleared flag is set on the target flag bit corresponding to the idle large page memory.
[0076] It should be noted that the pre-clearing of the free memory in the large page memory pool may specifically include: for the case where the target virtual machine uses standard large pages, the created kernel thread performs pre-clearing operations on the obtained free large page memory in the large page memory pool at a second preset period, and sets corresponding cleared marks on the target flag bits corresponding to the free large page memory. It can be understood that the created kernel thread judges whether there is free large page memory in the current large page memory pool at a second preset period; if there is free large page memory in the current large page memory pool, the free large page memory is obtained, and pre-clearing operations are performed on each free memory page corresponding to the free large page memory. Moreover, before the created kernel thread judges whether there is free large page memory in the current large page memory pool at a second preset period, it may further include: judging whether there is a process currently applying for large page memory in the current large page memory pool during a page fault; if there is no such process currently applying for the large page memory in the current large page memory pool during a page fault, the step of triggering the created kernel thread to judge whether there is free large page memory in the current large page memory pool at a second preset period is executed; if there is such a process currently applying for the large page memory in the current large page memory pool during a page fault, after waiting for a preset time, the step of judging whether there is a process currently applying for large page memory in the current large page memory pool during a page fault is re-executed until there is no such process currently applying for the large page memory in the current large page memory pool during a page fault. After obtaining the free large page memory, it further includes: setting the target state of the free large page memory to 1; correspondingly, after performing pre-clearing operations on each free memory page corresponding to the free large page memory, it further includes: setting the target state of the free large page memory to 0. It can be understood that when removing a free large page memory from the free list, first set the HUGEPAGE_CLEANING state to 1, and set the HUGEPAGE_CLEANING state to 0 when adding it back to the list after the clearing operation is completed. For example, as Figure 7As shown in the figure, a kernel thread is created for each large page memory pool during the initialization phase. The kernel thread is used to poll and clear the idle large pages of memory. For example, on the X86 architecture, two kernel threads need to be created for 2M large pages and 1G large pages. And this kernel thread judges whether there are idle memory pages in the current large page memory pool every N1 seconds. If there are idle memory pages in the current large page memory pool, the idle memory pages are taken off one by one from the free list, and then each memory page corresponding to the large page memory is cleared and the flags of the first page of the idle memory page are set to PG_clean. Similar to the pre-clearing of idle memory in buddy, the pre-clearing operation of idle memory in the large page memory pool has a lower priority than other business programs. Therefore, after processing each memory block, the CPU also needs to be relinquished to try to let other business programs execute. Moreover, in order not to affect the program's application for large page memory, it is necessary to judge at the beginning of each poll whether there is a program currently applying for large page memory during a page fault. If there is a program currently applying for large page memory during a page fault, it waits for N2 seconds and then judges again whether there is a program currently applying for large page memory during a page fault until it is found that there is no program currently applying for large page memory during a page fault. The purpose of this is that during the process of clearing memory pages, if a program starts to apply for large page memory and the available large page memory is insufficient, it is necessary to wait for the large page memory being cleared to be returned to the large page memory pool.
[0077] Step S33: If the cleared flag has been set for the idle memory page, skip the memory page clearing operation and use the idle memory page to map physical memory for the virtual memory of the target process.
[0078] For the specific content of the above steps S31 and S33, reference can be made to the corresponding content disclosed in the foregoing embodiments, and details will not be elaborated here.
[0079] It can be seen that in the embodiment of the present application, by pre-clearing the idle memory pages and setting relevant cleared flags, when a page fault occurs and physical memory is allocated for the virtual memory mapping of the target process, it is possible to first judge whether the obtained idle memory page has the cleared flag set. If the idle memory page has the cleared flag set, it is possible to avoid the time-consuming memory page clearing operation during the startup of the virtual machine, thereby being able to reduce the page fault execution time triggered during the startup of the virtual machine, effectively reducing the startup time of the virtual machine supporting device passthrough, improving the startup speed of the virtual machine, especially when the virtual machine is configured with a large amount of memory, the effect is more obvious. The technical solution of the present application can also be applied to scenarios where users are sensitive to the startup time of virtual machines in a virtualization scenario.
[0080] For example, for the page fault exception handling when the virtual machine does not use standard large pages: An unused free page is obtained from the buddy. It is judged whether the PG_clean flag is set for the flags of the free page. If the PG_clean flag is set for the flags of the free page, the page clearing operation is skipped, thereby reducing the processing time of the page fault exception. Otherwise, the corresponding page clearing operation is executed. As Figure 8 shown, for the page fault exception handling when the virtual machine uses standard large pages: Enter the page fault exception handling function for large page memory, that is, an unused free large page is obtained from the large page memory pool. It is judged whether the PG_clean flag is set for the flags of the first page of the free large page. If the PG_clean flag is set for the flags of the first page of the free large page, the page clearing operation is skipped. Otherwise, the corresponding page clearing operation is executed. And when the process enters the page fault exception to apply for large page memory, the hugepage_allocating count can be incremented by 1, and the hugepage_allocating count is decremented by 1 when exiting. In this way, the memory clearing kernel thread can judge whether there is a process applying for large page memory through hugepage_allocating. If it is found that the large page memory is insufficient when applying for large page memory, it is judged whether the HUGEPAGE_CLEANING status is 1. If the HUGEPAGE_CLEANING status is 1, wait until the HUGEPAGE_CLEANING status is 0 and then try to apply for large page memory again. That is to say, in order not to affect the application for large page memory, the clearing operation of the large page memory is only executed when there is no program applying for large page memory, and when the program fails to apply for large page memory, it will wait until the memory pre-clearing operation is completed and then try to apply for large page memory again.
[0081] Correspondingly, the embodiment of the present application also discloses a page fault exception handling device. See Figure 9 shown. The device includes:
[0082] A first judgment module 11, configured to judge whether a page fault exception is currently triggered during the startup process of a target process of a target virtual machine;
[0083] A free page obtaining module 12, configured to obtain a free memory page from different storage spaces according to whether the target virtual machine uses standard large pages when the page fault exception is currently triggered;
[0084] A second judgment module 13, configured to judge whether the free memory page is set with a cleared flag;
[0085] The memory zeroing control module 14 is used to skip the memory page zeroing operation when the cleared flag has been set for the free memory page, and map physical memory for the virtual memory of the target process using the free memory page.
[0086] As can be seen from the above, in the embodiments of the present application, by pre-clearing the free memory page and setting the relevant cleared flag in advance, when a page fault occurs to allocate physical memory for the virtual memory mapping of the target process, it is possible to first determine whether the obtained free memory page has the cleared flag set. If the free memory page has the cleared flag set, it is possible to avoid the time-consuming memory page zeroing operation during the startup of the virtual machine, thereby reducing the page fault execution time triggered during the startup of the virtual machine and improving the startup speed of the virtual machine.
[0087] In some specific embodiments, the page fault handling device may specifically include:
[0088] The first pre-clearing module is used to, in the case where the target virtual machine does not use standard large pages, perform a pre-clearing operation on the free memory pages obtained from the Buddy memory according to a first preset period by using the free page tracking mechanism of the Linux kernel, and set the corresponding cleared flag on the target flag bit corresponding to the free memory page;
[0089] In some specific embodiments, the free page obtaining module 12 may specifically include:
[0090] The first free page obtaining unit is used to, in the case where the target virtual machine does not use standard large pages, if the page fault is currently triggered, obtain the free memory page from the Buddy memory;
[0091] In some specific embodiments, the second judgment module 13 may specifically include:
[0092] The first flag judgment unit is used to judge whether the cleared flag is set on the target flag bit corresponding to the free memory page.
[0093] In some specific embodiments, the first pre-clearing module may specifically include:
[0094] The function registration unit is used to register a free page zeroing processing function based on the free page tracking mechanism of the Linux kernel;
[0095] The second free page obtaining unit is used to obtain each free memory page corresponding to the free memory block in the Buddy memory according to a first preset period through the free page tracking mechanism;
[0096] A first pre-clearing unit, configured to perform a pre-clearing operation on each of the free memory pages by using the free page clearing processing function.
[0097] In some specific embodiments, the page fault handling device may specifically include:
[0098] A second pre-clearing module, configured to, for the case where the target virtual machine uses standard large pages, perform a pre-clearing operation on the free large-page memory in the obtained large-page memory pool at a second preset period through a created kernel thread, and set a corresponding cleared flag on the target flag bit corresponding to the free large-page memory;
[0099] In some specific embodiments, the free page obtaining module 12 may specifically include:
[0100] A third free page obtaining unit, configured to, for the case where the target virtual machine uses standard large pages, if a page fault is currently triggered, obtain the free large-page memory from the large-page memory pool;
[0101] A second flag judging unit, configured to judge whether the cleared flag is set on the target flag bit corresponding to the free large-page memory.
[0102] In some specific embodiments, the second pre-clearing module may specifically include:
[0103] A memory judging unit, configured to judge whether there is free large-page memory in the current large-page memory pool at a second preset period through a created kernel thread;
[0104] A fourth free page obtaining unit, configured to, when there is free large-page memory in the current large-page memory pool, obtain the free large-page memory and perform a pre-clearing operation on each free memory page corresponding to the free large-page memory.
[0105] Before judging whether there is free large-page memory in the current large-page memory pool at a second preset period through a created kernel thread in some specific embodiments, it may specifically include:
[0106] A memory application judging unit, configured to judge whether there is a process currently applying for large-page memory in the current large-page memory pool during a page fault;
[0107] A step triggering unit, configured to, when there is no such process currently applying for the large-page memory in the current large-page memory pool during a page fault, trigger the step of judging whether there is free large-page memory in the current large-page memory pool at a second preset period through a created kernel thread;
[0108] A step re - execution unit, configured to, when there is currently a process applying for the large - page memory in the current large - page memory pool during a page - fault exception, re - execute the step of determining whether there is currently a process applying for the large - page memory in the current large - page memory pool after waiting for a preset time, until there is no longer a process applying for the large - page memory in the current large - page memory pool during a page - fault exception.
[0109] In some specific embodiments, after obtaining the free large - page memory, it may specifically include:
[0110] A first status setting unit, configured to set the target status of the free large - page memory to 1;
[0111] In some specific embodiments, after performing a pre - clearing operation on each free memory page corresponding to the free large - page memory, it may specifically include:
[0112] A second status setting unit, configured to set the target status of the free large - page memory to 0.
[0113] Furthermore, an embodiment of the present application also provides an electronic device. Figure 10 It is a structural diagram of an electronic device 20 shown according to an exemplary embodiment. The content in the figure should not be considered as any limitation to the scope of use of the present application.
[0114] Figure 10 It is a schematic structural diagram of an electronic device 20 provided by an embodiment of the present application. The electronic device 20 may specifically include: at least one processor 21, at least one memory 22, a power supply 23, a communication interface 24, an input / output interface 25, and a communication bus 26. Among them, the memory 22 is used to store computer processes, and the computer processes are loaded and executed by the processor 21 to implement the relevant steps in the page - fault exception handling method disclosed in any of the foregoing embodiments. In addition, the electronic device 20 in this embodiment may specifically be an electronic computer.
[0115] In this embodiment, the power supply 23 is used to provide working voltages for each hardware device on the electronic device 20; the communication interface 24 can create a data transmission channel between the electronic device 20 and external devices, and the communication protocol it follows is any communication protocol applicable to the technical solution of the present application, and no specific limitation is imposed on it here; the input / output interface 25 is used to obtain external input data or output data to the outside, and its specific interface type can be selected according to specific application requirements, and no specific limitation is made here.
[0116] In addition, the memory 22, as the carrier for resource storage, can be a read-only memory, a random access memory, a magnetic disk, an optical disc, etc. The resources stored thereon can include an operating system 221, computer processes 222, etc. The storage method can be transient storage or permanent storage.
[0117] Among them, the operating system 221 is used to manage and control each hardware device and computer processes 222 on the electronic device 20, and it can be Windows Server, Netware, Unix, Linux, etc. In addition to the computer processes that can be used to complete the page fault exception handling method executed by the electronic device 20 disclosed in any of the foregoing embodiments, the computer processes 222 can further include computer processes that can be used to complete other specific tasks.
[0118] Furthermore, an embodiment of the present application also discloses a computer-readable storage medium. When a computer process stored in the computer-readable storage medium is loaded and executed by a processor, the steps of the page fault exception handling method disclosed in any of the foregoing embodiments are implemented.
[0119] In this specification, the various embodiments are described in a progressive manner. Each embodiment focuses on the differences from other embodiments. The same or similar parts among the various embodiments can be referred to each other. For the devices disclosed in the embodiments, since they correspond to the methods disclosed in the embodiments, the description is relatively simple, and the relevant parts can be referred to the description of the method part.
[0120] Finally, it should also be noted that in this text, relational terms such as first and second are only used to distinguish one entity or operation from another entity or operation, and do not necessarily require or imply any actual relationship or order between these entities or operations. Moreover, the term "comprising", "including" or any other variant thereof is intended to cover non-exclusive inclusion, so that a process, method, article or device including a series of elements not only includes those elements, but also includes other elements not expressly listed, or further includes elements inherent to such process, method, article or device. Without further limitation, an element defined by the statement "including a..." does not exclude the existence of additional identical elements in the process, method, article or device including the element.
[0121] The above has introduced in detail a page fault exception handling method, apparatus, device, and storage medium provided by the present invention. Specific examples are used in this article to elaborate on the principle and implementation manner of the present invention. The description of the above embodiments is only used to help understand the method and its core idea of the present invention; at the same time, for those of ordinary skill in the art, according to the idea of the present invention, there will be changes in the specific implementation manner and application scope. In summary, the content of this specification should not be construed as a limitation to the present invention.
Claims
1. A page fault exception handling method, characterized in that, Including: During the startup process of the target process in the target virtual machine, determine whether a page fault exception is currently triggered; If the page fault exception is currently triggered, obtain free memory pages from different storage spaces according to whether the target virtual machine uses standard large pages, and determine whether the free memory pages are set with a cleared flag; If the free memory pages are set with the cleared flag, skip the memory page clearing operation, and use the free memory pages to map physical memory for the virtual memory of the target process; Before determining whether the page fault exception is currently triggered, it further includes: for the case where the target virtual machine does not use standard large pages, use the free page tracking mechanism of the Linux kernel to perform a pre-clearing operation on the free memory pages in the obtained Buddy memory at a first preset period, and set a corresponding cleared flag on the target flag bit corresponding to the free memory pages; Correspondingly, obtaining free memory pages from different storage spaces according to whether the target virtual machine uses standard large pages, and determining whether the free memory pages are set with a cleared flag includes: for the case where the target virtual machine does not use standard large pages, if the page fault exception is currently triggered, obtain the free memory pages from the Buddy memory, and determine whether the cleared flag is set on the target flag bit corresponding to the free memory pages; Performing a pre-clearing operation on the free memory pages in the obtained Buddy memory at a first preset period by using the free page tracking mechanism of the Linux kernel includes: registering a free page clearing processing function based on the free page tracking mechanism of the Linux kernel; obtaining each free memory page corresponding to the free memory block in the Buddy memory at a first preset period through the free page tracking mechanism; performing a pre-clearing operation on each free memory page by using the free page clearing processing function.
2. The page fault exception handling method according to claim 1, wherein Before determining whether the page fault exception is currently triggered, it further includes: For the case where the target virtual machine uses standard large pages, perform a pre-clearing operation on the free large page memory in the obtained large page memory pool at a second preset period through the created kernel thread, and set a corresponding cleared flag on the target flag bit corresponding to the free large page memory; Correspondingly, obtaining free memory pages from different storage spaces according to whether the target virtual machine uses standard large pages, and determining whether the free memory pages are set with a cleared flag includes: For the case where the target virtual machine uses standard large pages, if the page fault exception is currently triggered, obtain the free large page memory from the large page memory pool, and determine whether the cleared flag is set on the target flag bit corresponding to the free large page memory.
3. The page fault exception handling method according to claim 2, wherein Performing a pre-clearing operation on the free large page memory in the obtained large page memory pool at a second preset period through the created kernel thread includes: Judging whether there is free large page memory in the large page memory pool currently at a second preset period through the created kernel thread; If there is such free huge page memory in the current huge page memory pool, obtain the free huge page memory, and perform a pre-clearing operation on each free memory page corresponding to the free huge page memory.
4. The page fault exception handling method according to claim 3, characterized in that, Before the created kernel thread determines whether there is free huge page memory in the current huge page memory pool according to the second preset period, it includes: Determine whether there is a process currently applying for huge page memory in the current huge page memory pool during a page fault exception; If there is no such process currently applying for the huge page memory in the current huge page memory pool during a page fault exception, trigger the step of the created kernel thread to determine whether there is free huge page memory in the current huge page memory pool according to the second preset period; If there is such a process currently applying for the huge page memory in the current huge page memory pool during a page fault exception, wait for a preset time and then re-execute the step of determining whether there is a process currently applying for huge page memory in the current huge page memory pool during a page fault exception until there is no such process currently applying for the huge page memory in the current huge page memory pool during a page fault exception.
5. The page fault exception handling method according to claim 3, characterized in that After obtaining the free huge page memory, it further includes: Set the target state of the free huge page memory to 1; Correspondingly, after performing the pre-clearing operation on each free memory page corresponding to the free huge page memory, it further includes: Set the target state of the free huge page memory to 0.
6. A page fault exception handling device, characterized in that, It includes: A first judgment module, used to judge whether a page fault exception is currently triggered during the startup of a target process in a target virtual machine; A free page acquisition module, used to, when the page fault exception is currently triggered, acquire free memory pages from different storage spaces according to whether the target virtual machine uses standard huge pages; A second judgment module, used to judge whether the free memory page is set with a cleared flag; A memory clearing control module, used to, when the free memory page is set with the cleared flag, skip the memory page clearing operation and use the free memory page to map physical memory for the virtual memory of the target process; The device is further used to, for the case where the target virtual machine does not use standard huge pages, perform a pre-clearing operation on the free memory pages in the obtained Buddy memory according to the first preset period by using the free page tracking mechanism of the Linux kernel, and set a corresponding cleared flag on the target flag bit corresponding to the free memory page; Correspondingly, the second judgment module is specifically used to, for the case where the target virtual machine does not use standard huge pages, if the page fault exception is currently triggered, acquire the free memory page from the Buddy memory, and judge whether the cleared flag is set on the target flag bit corresponding to the free memory page; The device is further used to register a free page clearing processing function based on the free page tracking mechanism of the Linux kernel; obtain each free memory page corresponding to the free memory block in the Buddy memory according to the first preset period through the free page tracking mechanism; and perform a pre-clearing operation on each free memory page by using the free page clearing processing function.
7. An electronic device, characterized in that, It includes: A memory, used to save computer processes; A processor for executing the computer process to implement the steps of the page fault exception handling method according to any one of claims 1 to 5.
8. A computer-readable storage medium, characterized in that, For storing a computer process; wherein, when the computer process is executed by a processor, the steps of the page fault exception handling method according to any one of claims 1 to 5 are implemented.
Citation Information
Patent Citations
Data storage method and device based on memory, processor and electronic equipment
CN103793332A
Page fault anomaly solution based on hugetlb
CN106970826A