A method and system for managing virtual machine memory
By dividing the physical machine's memory into dedicated areas and allocating memory on demand, the problem of wasted physical memory during virtual machine creation is solved, achieving more efficient memory management.
Patent Information
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2021-03-24
- Publication Date
- 2026-03-06
AI Technical Summary
In the device pass-through mode, existing technologies require all physical memory to be pre-allocated when the virtual machine is created in order to ensure DMA operations of the virtual machine, resulting in a waste of physical memory resources.
A dedicated memory area is allocated in the physical machine's memory for DMA operations on pass-through devices. Non-pass-through devices are allocated memory as needed by the virtual machine monitor. The device type is identified through API functions and modified DMA operation functions to control memory allocation.
This reduces the physical memory usage of virtual machines, avoids resource waste, and improves memory utilization efficiency.
Smart Images

Figure CN113076174B_ABST
Abstract
Description
Technical Field
[0001] This invention relates to the field of virtual machine technology, and in particular to a method and system for managing virtual machine memory. Background Technology
[0002] Currently, virtualization technology is widely used in data centers, and hypervisors typically provide virtual devices to virtual machines in software. However, for certain high-speed I / O (Input / Output) scenarios, device pass-through can be used to improve virtual machine I / O performance. This method directly provides hardware devices to the virtual machine, thereby improving I / O performance. Generally, virtual machine memory is allocated on demand, meaning memory is allocated only when real physical memory is used, thus saving significant memory resources. In existing technologies, under device pass-through, to ensure that memory is allocated before DMA (Direct Memory Access) operations, all the physical memory corresponding to the virtual machine is usually pre-allocated when the virtual machine is created. However, DMA operations using pass-through only require a portion of the memory buffer. The above approach pre-allocates all the memory of the entire virtual machine to pre-allocate this portion of memory, resulting in excessive physical memory overhead and wasted resources. Summary of the Invention
[0003] This invention provides a method and system for managing virtual machine memory, avoiding the waste of physical memory caused by allocating all physical memory of the virtual machine when it is created.
[0004] In a first aspect, the present invention provides a method for managing virtual machine memory, wherein the virtual machine runs on a virtual machine monitor, and the virtual machine monitor is installed on a physical machine. The management method includes: allocating a dedicated memory area in the physical machine's memory; creating pass-through devices and non-pass-through devices on the virtual machine; when a pass-through device performs a DMA operation, the dedicated memory area is directly provided to the pass-through device; when a non-pass-through device performs a DMA operation, the virtual machine monitor allocates other memory areas in memory for the non-pass-through device to use.
[0005] In the above scheme, a dedicated memory area is allocated in the physical machine's memory for direct use during DMA operations on pass-through devices. Non-pass-through devices on the virtual machine are still allocated by the virtual machine monitor. Using this scheme, only the dedicated memory area for DMA operations needs to be pre-allocated at virtual machine startup; there is no need to allocate additional physical memory for non-pass-through devices within the virtual machine. This significantly reduces the virtual machine's physical memory usage, saving physical memory. It avoids the waste of physical memory that would occur if all the physical memory of the virtual machine were allocated during virtual machine creation.
[0006] In one specific implementation, both passthrough device drivers and non-passthrough device drivers are installed on the virtual machine. After allocating a dedicated memory area in the physical machine's memory, the management method further includes providing a set of API (Application Programming Interface) functions for allocating and releasing DMA buffers for the dedicated memory area. When the passthrough device performs DMA operations, the dedicated memory area is directly provided to the passthrough device for its use. This includes the passthrough device driver calling these API functions to allocate and release DMA buffers during DMA operations. This facilitates the passthrough device's allocation and release of DMA buffers from the dedicated memory area through the passthrough device driver.
[0007] In one specific implementation, the management method further includes: modifying the DMA operation functions in the virtual machine kernel to determine whether a device is a pass-through device when the DMA operation function is called; if the determination result is a pass-through device, the pass-through device driver calls the set of API functions to allocate and release the DMA buffer. By modifying the DMA operation functions, when the DMA operation function is called, it first determines whether a device is a pass-through device. If the determination result is a pass-through device, the pass-through device driver allocates and releases the DMA buffer by calling the set of API functions, which facilitates quick identification of DMA operations performed by a pass-through device.
[0008] In one specific implementation, if the determination result is a non-passthrough device, the non-passthrough device driver requests memory allocation from the virtual machine monitor, and the virtual machine monitor allocates other memory areas in memory for the non-passthrough device to use. This facilitates quick identification of DMA operations performed on non-passthrough devices, and the virtual machine monitor allocates memory areas for DMA operations on non-passthrough devices using an on-demand allocation method, saving physical memory.
[0009] In one specific implementation, the management method further includes: when a virtual machine creates a passthrough device, the IOMMU uses a dedicated memory region as input to establish a DMA Remapping page table for the dedicated memory region. This supports a partitioning method where the dedicated memory region consists of multiple non-contiguous physical addresses.
[0010] In one specific implementation, after allocating a dedicated memory area in the physical machine's memory, the management method further includes: locking (pinning) the dedicated memory area to ensure that the dedicated memory area is actually allocated and cannot be swapped out.
[0011] In one specific implementation, the capacity of the dedicated memory area is specified according to actual needs in order to save physical memory while ensuring that the pass-through device can perform DMA operations normally.
[0012] Secondly, the present invention also provides a virtual machine memory management system, which includes a physical machine, a virtual machine monitor installed on the physical machine, and virtual machines running on the virtual machine monitor. Passthrough devices and non-passthrough devices are created on the virtual machines. A dedicated memory area is allocated in the physical machine's memory. This dedicated memory area is directly provided to the passthrough device during DMA operations. When a non-passthrough device performs DMA operations, the virtual machine monitor allocates other memory areas for the non-passthrough device.
[0013] In the above scheme, a dedicated memory area is allocated in the physical machine's memory for direct use during DMA operations on pass-through devices. Non-pass-through devices on the virtual machine are still allocated by the virtual machine monitor. Using this scheme, only the dedicated memory area for DMA operations needs to be pre-allocated at virtual machine startup; there is no need to allocate additional physical memory for non-pass-through devices within the virtual machine. This significantly reduces the virtual machine's physical memory usage, saving physical memory. It avoids the waste of physical memory that would occur if all the physical memory of the virtual machine were allocated during virtual machine creation.
[0014] In one specific implementation, the virtual machine has both passthrough and non-passthrough device drivers installed. The virtual machine has a set of API functions that provide allocation and release of DMA buffers for dedicated memory areas. When a passthrough device performs DMA operations, the passthrough device driver calls these API functions to allocate and release the DMA buffer. This facilitates the passthrough device in allocating and releasing DMA buffers from the dedicated memory area through the passthrough device driver.
[0015] In one specific implementation, the virtual machine kernel includes modified DMA operation functions to determine whether a device is a pass-through device when these functions are called. When the pass-through device driver determines that a device is a pass-through device, it calls these API functions to allocate and release the DMA buffer. This facilitates rapid identification of DMA operations on non-pass-through devices, allowing the virtual machine monitor to allocate memory for DMA operations on non-pass-through devices on demand, thus saving physical memory.
[0016] In one specific implementation, the management system also includes an IOMMU (input / output memory management unit) installed on the physical machine. This IOMMU is used to establish a DMARemapping page table for the dedicated memory area when a virtual machine creates a passthrough device, using a dedicated memory area as input. This supports a partitioning method where the dedicated memory area consists of multiple non-contiguous physical addresses. Attached Figure Description
[0017] Figure 1 A flowchart illustrating a virtual machine memory management method provided in an embodiment of the present invention;
[0018] Figure 2 A flowchart illustrating another virtual machine memory management method provided in an embodiment of the present invention;
[0019] Figure 3 A structural block diagram of a virtual machine memory management system provided in an embodiment of the present invention;
[0020] Figure 4 This is a structural block diagram of another virtual machine memory management system provided in an embodiment of the present invention.
[0021] Figure label:
[0022] 10 - Physical Machines 20 - Virtual Machine Monitors 30 - Virtual Machines
[0023] 40 - Dedicated memory area; 51 - Pass-through device; 52 - Non-pass-through device
[0024] 61-Pass-through device driver 62-Non-pass-through device driver 70-IOMMU Detailed Implementation
[0025] To make the objectives, technical solutions, and advantages of the embodiments of the present invention clearer, the technical solutions of the embodiments of the present invention will be clearly and completely described below with reference to the accompanying drawings. Obviously, the described embodiments are only some embodiments of the present invention, and not all embodiments. Based on the embodiments of the present invention, all other embodiments obtained by those skilled in the art without creative effort are within the scope of protection of the present invention.
[0026] To facilitate understanding of the virtual machine memory management method provided in this embodiment of the invention, the application scenario of the management method provided in this embodiment of the invention is first described below. This management method is applied to the process of virtual machine memory management. The virtual machine runs on a virtual machine monitor, which is installed on a physical machine. The virtual machine memory management method will now be described in detail with reference to the accompanying drawings.
[0027] refer to Figure 1 The virtual machine memory management method provided in this embodiment of the invention includes:
[0028] Step 10: Allocate a dedicated memory area 40 in the memory of physical machine 10;
[0029] Step 20: Create pass-through device 51 and non-pass-through device 52 on virtual machine 30;
[0030] Step 31: When performing DMA operations in the pass-through device 51, the dedicated memory area 40 is directly provided to the pass-through device 51 for use;
[0031] Step 32: When performing DMA operations on the non-pass-through device 52, the virtual machine monitor 20 allocates other memory areas in memory for the non-pass-through device 52 to use.
[0032] In the above scheme, a dedicated memory area 40 is allocated in the memory of physical machine 10 for direct use by passthrough device 51 during DMA operations. Non-passthrough device 52 on virtual machine 30 is still allocated by virtual machine monitor 20. Using this scheme, only the dedicated memory area 40 for DMA operations by passthrough device 51 needs to be pre-allocated when virtual machine 30 starts; there is no need to allocate additional physical memory for non-passthrough device 52 in virtual machine 30, thus significantly reducing the physical memory usage of virtual machine 30 and saving physical memory. This avoids the waste of physical memory caused by allocating all physical memory for virtual machine 30 during its creation. The following detailed explanation of each method step is provided with reference to the accompanying drawings.
[0033] First, it is necessary to explain the system in which the virtual machine 30 memory management method provided in this embodiment of the invention is applied, such as... Figure 3 The diagram shown is a structural block diagram of a management system provided in an embodiment of the present invention. A virtual machine monitor 20 is installed on a physical machine 10, and a virtual machine 30 runs on the virtual machine monitor 20. Pass-through devices 51 and non-pass-through devices 52 are created on the virtual machine 30. The physical machine 10 provides physical memory for the pass-through devices 51 and non-pass-through devices 52.
[0034] During memory management, such as Figure 1 and Figure 3As shown, a dedicated memory area 40 is first allocated in the memory of physical machine 10. This dedicated memory area 40 is specifically provided to the passthrough device 51 for use during DMA operations. Furthermore, after allocating the dedicated memory area 40 in the memory of physical machine 10, it can be locked (pinned) to ensure that the dedicated memory area 40 is actually allocated and cannot be swapped out. The size of this dedicated memory area 40 can be specified according to actual needs to save physical memory while ensuring that the passthrough device 51 can perform DMA operations normally.
[0035] Next, continue to refer to Figure 1 Create passthrough device 51 and non-passthrough device 52 on virtual machine 30. (See reference) Figure 2 , Figure 3 and Figure 4 When creating a passthrough device 51 in virtual machine 30, the IOMMU 70 can be selected to take the dedicated memory region 40 as input, and a DMA Remapping page table for the dedicated memory region 40 can be created in the IOMMU 70 for the passthrough device 51. This supports the partitioning of the dedicated memory region 40, which consists of multiple non-contiguous physical addresses.
[0036] like Figure 3 As shown, passthrough device driver 61 and non-passthrough device driver 62 are also installed on virtual machine 30. Passthrough device driver 61 corresponds to passthrough device 51, and non-passthrough device driver 62 corresponds to non-passthrough device 52.
[0037] Next, as Figure 1 As shown, when the passthrough device 51 performs a DMA operation, the dedicated memory area 40 is directly provided for use by the passthrough device 51. After allocating a dedicated memory area 40 in the memory of the physical machine 10, a set of API functions for allocating and releasing the DMA cache can be provided for this dedicated memory area 40. The direct provision of the dedicated memory area 40 to the passthrough device 51 during DMA operations can be achieved by calling the aforementioned set of API functions. For details, refer to... Figure 2 When the passthrough device 51 performs DMA operations, the passthrough device driver 61 can call this set of API functions to request and release the DMA buffer. This allows the passthrough device 51 to request and release the DMA buffer from the dedicated memory area 40 through the passthrough device driver 61.
[0038] When a DMA operation is performed on the non-passthrough device 52, the virtual machine monitor 20 allocates other memory areas in memory for the non-passthrough device 52 to use. That is, for the non-passthrough device 52, the passthrough device driver 61 still operates according to the existing DMA operation method and is unaffected. Specifically, the memory in the virtual machine 30 other than the DMA-dedicated area of the passthrough device 51 can be allocated on demand. For example, on the Xen platform (an open-source virtual machine monitor), memory can be allocated to the non-passthrough device 52 on demand using the traditional Pod (Populated-On-Demand, an on-demand memory allocation method). Of course, other on-demand memory allocation methods can also be used to allocate memory to the non-passthrough device 52.
[0039] Additionally, refer to Figure 2 The management method may further include: modifying the DMA operation function in the kernel of virtual machine 30 to determine whether it is a pass-through device 51 when the DMA operation function is called. If the determination result is a pass-through device 51, the pass-through device driver 61 calls the set of API functions to allocate and release the DMA buffer. By modifying the DMA operation function, when the DMA operation function is called, it first determines whether it is a pass-through device 51. If the determination result is a pass-through device 51, the pass-through device driver 61 allocates and releases the DMA buffer by calling the set of API functions, which facilitates quick identification that the DMA operation is performed by the pass-through device 51.
[0040] Continue to refer to Figure 2 If the determination result is that it is a non-passthrough device 52, then the non-passthrough device driver 62 requests memory allocation from the virtual machine monitor 20, and the virtual machine monitor 20 allocates other memory areas in memory for the non-passthrough device 52 to use. This facilitates quick identification of DMA operations performed on non-passthrough device 52, and the virtual machine monitor 30 allocates memory areas for DMA operations to non-passthrough device 52 in an on-demand allocation manner, saving physical memory.
[0041] By allocating a dedicated memory area 40 within the physical machine 10's memory for direct use by the passthrough device 51 during DMA operations, the non-passthrough device 52 on the virtual machine 30 is still allocated by the virtual machine monitor 20. With this approach, only the dedicated memory area 40 for the passthrough device 51 needs to be pre-allocated when the virtual machine 30 starts; there's no need to allocate additional physical memory for the non-passthrough device 52 within the virtual machine 30. This significantly reduces the physical memory usage of the virtual machine 30, saving physical memory. It avoids the waste of physical memory that would occur if all the physical memory of the virtual machine 30 were allocated during its creation.
[0042] In addition, embodiments of the present invention also provide a memory management system for a virtual machine 30, see reference. Figure 3 The management system includes a physical machine 10, a virtual machine monitor 20 installed on the physical machine 10, and virtual machines 30 running on the virtual machine monitor 20. Passthrough devices 51 and non-passthrough devices 52 are created on virtual machines 30. A dedicated memory area 40 is allocated in the memory of the physical machine 10. This dedicated memory area 40 is directly provided to the passthrough device 51 when it performs DMA operations. When the non-passthrough device 52 performs DMA operations, the virtual machine monitor 20 allocates other memory areas for the non-passthrough device 52 to use.
[0043] In the above scheme, a dedicated memory area 40 is allocated in the memory of physical machine 10 for direct use by passthrough device 51 during DMA operations. Non-passthrough device 52 on virtual machine 30 is still allocated by virtual machine monitor 20. Using this scheme, only the dedicated memory area 40 for DMA operations by passthrough device 51 needs to be pre-allocated when virtual machine 30 starts; there is no need to allocate additional physical memory for non-passthrough device 52 in virtual machine 30, thus significantly reducing the physical memory usage of virtual machine 30 and saving physical memory. This avoids the waste of physical memory caused by allocating all physical memory for virtual machine 30 during its creation. The following is a detailed description of each structure with reference to the accompanying drawings.
[0044] refer to Figure 3 A passthrough device 51 and a non-passthrough device driver 62 are installed on virtual machine 30. A set of API functions can be configured on virtual machine 30 to allocate and release DMA cache for dedicated memory area 40. When passthrough device 51 performs DMA operations, passthrough device driver 61 calls this set of API functions to allocate and release DMA cache. This facilitates passthrough device 51 in allocating and releasing DMA cache from dedicated memory area 40 through passthrough device driver 61.
[0045] refer to Figure 4 The management system may also include an IOMMU 70 installed on the physical machine 10. This IOMMU 70 is used to create a DMARemapping page table for the dedicated memory region 40 as input when the virtual machine 30 creates the passthrough device 51. This supports partitioning the dedicated memory region 40 into multiple non-contiguous physical addresses.
[0046] Additionally, a modified DMA operation function can be configured in the kernel of virtual machine 30 to determine whether the device is a pass-through device 51 when the DMA operation function is called. When the pass-through device driver 61 determines that the device is a pass-through device 51, it calls this set of API functions to request and release the DMA buffer. This facilitates quick identification of DMA operations performed on non-pass-through devices 52, allowing the virtual machine 30 monitor to allocate memory for DMA operations on non-pass-through devices 52 using an on-demand allocation method, thus saving physical memory. If the determination is that the device is a non-pass-through device 52, the non-pass-through device driver 62 requests memory allocation from the virtual machine monitor 20, which then allocates other memory areas for the non-pass-through device 52. This facilitates quick identification of DMA operations performed on non-pass-through devices 52, allowing the virtual machine 30 monitor to allocate memory for DMA operations on non-pass-through devices 52 using an on-demand allocation method, thus saving physical memory.
[0047] The above description is merely a specific embodiment of the present invention, but the scope of protection of the present invention is not limited thereto. Any variations or substitutions that can be easily conceived by those skilled in the art within the technical scope disclosed in the present invention should be included within the scope of protection of the present invention. Therefore, the scope of protection of the present invention should be determined by the scope of the claims.
Claims
1. A method of managing memory of a virtual machine, wherein, The virtual machine runs on a virtual machine monitor, and the virtual machine monitor is installed on a physical machine, characterized in that the management method comprises: a dedicated memory area is divided in the memory of the physical machine; a pass-through device and a non-pass-through device are created on the virtual machine; wherein the pass-through device is a hardware device, and the virtual machine is installed with a pass-through device driver and a non-pass-through device driver; a set of API functions for applying and releasing DMA cache areas are provided for the dedicated memory area; when the pass-through device performs DMA operation, the set of API functions are called by the pass-through device driver to apply and release the DMA cache areas, so that the dedicated memory area is directly provided for the pass-through device to use; when the non-pass-through device performs DMA operation, other memory areas in the memory are allocated by the virtual machine monitor and provided for the non-pass-through device to use; further comprising: a DMA operation function in the kernel of the virtual machine is modified, and when the DMA operation function is called, it is determined whether it is a pass-through device; if the result of the determination is a pass-through device, the set of API functions are called by the pass-through device driver to apply and release the DMA cache areas; and when the virtual machine creates the pass-through device, the dedicated memory area is taken as input by the IOMMU to establish a DMA Remapping page table of the dedicated memory area for the pass-through device.
2. The management method of claim 1, wherein, if the result of the determination is a non-pass-through device, memory areas are applied and allocated by the non-pass-through device driver to the virtual machine monitor, and other memory areas in the memory are allocated by the virtual machine monitor and provided for the non-pass-through device to use.
3. The management method of claim 1, wherein, After the dedicated memory area is divided in the memory of the physical machine, the management method further comprises locking the dedicated memory area.
4. A system for managing memory of a virtual machine, the system comprising: comprise: a physical machine; a virtual machine monitor installed on the physical machine; a virtual machine running on the virtual machine monitor, and a pass-through device and a non-pass-through device are created on the virtual machine; wherein the pass-through device is a hardware device, and the virtual machine is installed with a pass-through device driver and a non-pass-through device driver; wherein a dedicated memory area is divided in the memory of the physical machine, and a set of API functions for applying and releasing DMA cache areas are provided for the dedicated memory area on the virtual machine; when the pass-through device performs DMA operation, the set of API functions are called by the pass-through device driver to apply and release the DMA cache areas, so that the dedicated memory area is directly provided for the pass-through device to use; when the non-pass-through device performs DMA operation, other memory areas in the memory are allocated by the virtual machine monitor and provided for the non-pass-through device to use; a modified DMA operation function is provided in the kernel of the virtual machine, which is used to determine whether it is a pass-through device when the DMA operation function is called; and the pass-through device driver calls the set of API functions to apply and release the DMA cache areas when the result of the determination is a pass-through device; further comprising: An IOMMU installed on the physical machine, the IOMMU is configured to establish a DMA Remapping page table of the dedicated memory region for the pass-through device as input when the virtual machine creates the pass-through device.
Citation Information
Patent Citations
Method and device for assisting communication between virtual machines
CN101819564A
Internal memory resource distribution method based on internal memory hole mechanism
CN102779074A
Unvarnished transmission method and system for PCI device of virtual machine
CN105159742A
Virtual machine memory sharing method based on combination of KSM and Pass-through
CN106155933A
Method and device for managing memories of virtual machine
CN107783812A