Memory management method and apparatus, and computer device and program product
By having each OS in a multi-OS system manage its own shared global free memory, the problems of high performance overhead and poor reliability under centralized OS management are solved, achieving efficient memory allocation and release, and improving the system's reliability and scalability.
Patent Information
- Application Number
- PCT/CN2026/095996
- Authority / Receiving Office
- WO · WO
- Patent Type
- Applications
- Current Assignee / Owner
- Priority Date
- 2025-05-19
- Filing Date
- 2026-05-09
- Publication Date
- 2026-11-26
AI Technical Summary
In a multi-OS system, when memory is managed by a central OS, the performance overhead caused by cross-OS communication mechanisms is significant, the central OS faces heavy processing pressure, and the system reliability is poor.
Each operating system (OS) allocates physical memory independently based on the shared global free memory information in the target physical memory. By updating local free memory information, cross-OS communication is avoided, thus achieving decentralized memory management.
It reduces performance overhead, alleviates the processing pressure on the central OS, and improves system reliability and scalability.
Smart Images

Figure CN2026095996_26112026_PF_FP_ABST
Abstract
Description
Memory management methods, devices, computer equipment and software products
[0001] This application claims priority to Chinese Patent Application No. 202510656250.9, filed on May 19, 2025, entitled “Memory Management Method, Apparatus, Computer Equipment and Program Product”, the entire contents of which are incorporated herein by reference. Technical Field
[0002] This application relates to the field of memory management technology, and in particular to a memory management method, apparatus, computer equipment, and program product. Background Technology
[0003] Currently, as artificial intelligence, high-performance computing, and data communication processing place increasingly higher demands on the performance of central processing units (CPUs) in computer devices, CPUs are gradually evolving towards multi-core or multi-CPU-core architectures. Given computer devices with multiple CPU cores and the lack of cache coherence, running multiple operating systems (OSs) is the mainstream solution in the industry. In this context, how to manage the memory of the computer device becomes a crucial issue.
[0004] In related technologies, when multiple operating systems (OS) are deployed in a computer device, one of these OSes acts as the central OS to manage the memory of the computer device. After booting, the central OS can obtain and manage information about the available memory in the computer device. Based on this, other OSes in the computer device can request memory from the central OS for use after booting through cross-OS communication messages. When there are a large number of OSes in the computer device, the communication messages can cause significant performance overhead, and the central OS faces heavy processing pressure, easily becoming a performance bottleneck. Furthermore, if the central OS fails, the entire system will become inoperable, resulting in poor system reliability. Summary of the Invention
[0005] This application provides a memory management method, apparatus, computer device, and program product to solve the problems of high performance overhead, high processing pressure on the central OS, and poor system reliability caused by cross-OS communication mechanisms when memory is managed by a central OS in a multi-OS system.
[0006] To achieve the above objectives, this application adopts the following technical solution:
[0007] In a first aspect, a memory management method is provided, applied to a first OS in a computer device, the computer device including multiple OSs and a target physical memory, wherein the first OS is any one of the multiple OSs, the method comprising: obtaining information on globally free memory shared by the multiple OSs from the target physical memory, wherein the globally free memory refers to unallocated memory space in the target physical memory; allocating a first physical memory space for the first OS based on the information on the globally free memory; and updating the information on the globally free memory based on the first physical memory space.
[0008] In this application, the target physical memory of the computer device stores information about globally free memory shared by multiple operating systems (OSs). Based on this, any one of the multiple OSs can obtain the information about globally free memory from the target physical memory, and then use this information to allocate physical memory space for itself. Afterwards, it updates the information about globally free memory in the target physical memory so that other OSs can allocate physical memory based on this information. Therefore, in this application, each OS can allocate physical memory independently based on the shared information about globally free memory in the target physical memory, without needing to request memory through cross-OS communication messages, resulting in lower performance overhead. Furthermore, since each OS can allocate physical memory independently, decentralized memory management is achieved. This avoids the problem of heavy processing pressure on a central OS. Moreover, even if one OS crashes, it will not affect the operation of other OS kernels, improving system reliability.
[0009] Optionally, after allocating a first physical memory space for itself, the first OS also updates the information of local free memory based on the first physical memory space. The local free memory refers to the free memory space in the physical memory space allocated to the first OS, and the updated information of local free memory includes the information of the first physical memory space.
[0010] Among them, the information of local free memory is stored in the cache of the first CPU core where the first OS is located or in the private memory of the first OS, which is memory information managed by the first OS alone.
[0011] Optionally, if the first OS maintains information about local free memory, in response to a first memory request from the kernel of the first OS, a second physical memory space is allocated for the kernel of the first OS from the local free memory. That is, the first OS can allocate a second physical memory space for the kernel of the first OS based on information about local free memory.
[0012] In this application, compared to allocating memory for the kernel of the first OS based on information of global free memory shared by multiple OSs, the first OS allocates memory for its own kernel based on information of local free memory. This can reduce the performance overhead caused by using cross-OS atomic operations or cross-OS lock operations when accessing information of global free memory, and has strong scalability.
[0013] Furthermore, since the information of local free memory is stored in the cache of the first CPU core where the first OS resides, this information of local free memory is managed and used exclusively by the first OS. That is, this information of local free memory will not be accessed by other CPU cores. Therefore, it is only necessary to maintain the cache consistency of the information of local free memory within the first CPU core. There is no need to maintain the cache consistency of multiple CPU cores, which reduces the performance overhead of maintaining cache consistency and improves the performance of the CPU.
[0014] Optionally, in response to a first memory release request from the kernel of the first OS, the third physical memory space is released to the local free memory.
[0015] In this application, physical memory space to be released can be first released to local free memory. Since the information of local free memory is managed by the first OS alone, releasing physical memory space to local free memory only requires updating the information of local free memory, without needing to access the information of global free memory. This avoids the performance overhead caused by cross-OS atomic operations or cross-OS lock operations in the process of accessing the information of global free memory, and has strong scalability.
[0016] Optionally, in response to a second memory request from the kernel of the first OS, a first virtual memory space is allocated to the kernel of the first OS from the virtual memory space managed by the first OS in the shared virtual memory. The shared virtual memory is virtual memory shared by the multiple OSes, and each OS manages a portion of the virtual memory space in the shared virtual memory. A fourth physical memory space is allocated to the kernel of the first OS from the local free memory. An address mapping relationship between the first virtual memory space and the fourth physical memory space is added to the kernel page table of the first OS. The second-level page table corresponding to the shared virtual memory in the kernel page table of the first OS is a shared page table of the multiple OSes. The addresses of the portions of the virtual memory space managed by the multiple OSes in the shared virtual memory do not overlap in the page table entries corresponding to the portions of the virtual memory space in the shared virtual memory.
[0017] In this application, the secondary page table corresponding to the shared virtual memory in the kernel page table is a shared page table for multiple operating systems. That is, multiple operating systems share a single secondary page table in the shared virtual address space. Based on this, after the first operating system requests the first virtual memory space and adds the address mapping relationship between the first virtual memory space and the fourth physical memory space in the shared page table, other operating systems can access the fourth physical memory space using the address of the first virtual memory space through the address mapping relationship in the shared secondary page table. Compared to a scheme where each operating system has its own independent page table and does not share page tables, in this application, the first operating system does not need to notify other operating systems to create the corresponding address mapping relationship through communication messages, resulting in lower performance overhead.
[0018] Furthermore, each of the multiple operating systems (OS) manages a portion of the shared virtual memory space. The virtual addresses of the portion of virtual memory managed by each OS do not overlap in the page table entries corresponding to the shared page tables of the multiple OSs. In other words, each OS manages a portion of the page table entries, and the page table entries managed by each OS are different. Based on this, when allocating shared memory, each OS can allocate its own managed virtual memory space to the kernel. Correspondingly, the OS can map the allocated virtual memory space to the allocated physical memory space by updating its own managed entries in the shared page table. In this way, one OS will not modify the entries managed by another OS. Therefore, even without cross-OS atomic operations or cross-OS locking operations, there will be no conflicts when the OSs access the shared page table, improving memory management efficiency and providing strong scalability.
[0019] Optionally, the method further includes: in response to a second memory release request from the kernel of the first OS, if the second virtual memory space is a virtual memory space managed by the first OS in shared virtual memory, then releasing the second virtual memory space and releasing the fifth physical memory space corresponding to the second virtual memory space to the local free memory, wherein the second memory release request includes the address of the second virtual memory space.
[0020] In this application, the secondary page table corresponding to the shared virtual memory in the kernel page table is a shared page table for multiple operating systems. That is, multiple operating systems share a single secondary page table in the shared virtual address space. Based on this, after the first operating system unmaps the address mapping relationship between the second virtual memory space and the fifth physical memory space in the shared page table, other operating systems will no longer be able to access the fifth physical memory space through the address mapping relationship in the shared secondary page table. Compared to a scheme where each operating system has its own independent page table and does not share a page table, in this application, the first operating system does not need to notify other operating systems to unmap the corresponding address relationship through communication messages, resulting in lower performance overhead.
[0021] Furthermore, in this application, where each OS manages a portion of the shared virtual memory space across multiple OSes, and the virtual addresses of the portion of virtual memory managed by each OS do not overlap in the entries of the shared page tables of the multiple OSes, when each OS releases shared memory, if the virtual memory space corresponding to the shared memory to be released is a virtual memory space managed by itself, that OS can release the address mapping relationship between the virtual memory space and the corresponding physical memory space by reading and writing the entries it manages in the shared page table. If the virtual memory space corresponding to the shared memory to be released is not a virtual memory space managed by itself, the OS managing that virtual memory space can release the shared memory by updating the entries it manages in the shared page table. Therefore, one OS will not modify the entries managed by other OSes. Thus, even without cross-OS atomic operations or cross-OS locking operations, there will be no conflicts when the OSes access the shared page table, improving memory management efficiency and providing strong scalability.
[0022] Optionally, if the second virtual memory space is not a virtual memory space managed by the first OS, then a second OS is determined to manage the second virtual memory space; the address of the second virtual memory space is added to the lockless linked list of the second OS, the lockless linked list being used to store the address of the virtual memory space corresponding to the physical memory space requested and to be released by the corresponding OS.
[0023] In this application, if the first OS determines that the virtual memory space corresponding to the shared memory to be released is not a virtual memory space managed by itself, the first OS adds the address of the virtual memory space to be released to the lockless linked list of the second OS that manages the virtual memory space. The second OS then releases the shared memory by reading the lockless linked list. Thus, when releasing shared memory across OSes, the OS does not need to send a message to notify the OS that applied for and manages the shared memory to release the memory. This reduces the performance noise of the OS and alleviates the performance bottleneck that may be caused by message notification when there are a large number of OSes.
[0024] Optionally, in this application, before allocating memory to the kernel, before releasing memory, or when the size of local free memory is detected to be below the memory waterline, if the first OS's unlocked linked list is not empty, then the addresses of one or more virtual memory spaces in the first OS's unlocked linked list are obtained. The unlocked linked list is used to store the addresses of the virtual memory spaces corresponding to the physical memory spaces requested by the corresponding OS and to be released. Based on the addresses of the one or more virtual memory spaces, the one or more virtual memory spaces are released, and the physical memory spaces corresponding to the one or more virtual memory spaces are released to the local free memory for subsequent use, thereby improving local memory utilization.
[0025] Optionally, if the local free memory includes a contiguous free space with a size not less than a first threshold, the contiguous free space is released to the global free memory for use by other OSes, thereby improving memory utilization.
[0026] Optionally, the target physical memory also stores memory metadata shared by the multiple OSes, the memory metadata including the state data of the target physical memory, and the method further includes: updating the memory metadata based on the allocated physical memory space or the released physical memory space.
[0027] In this application, the target physical memory of the computer device stores shared memory metadata for multiple operating systems. Each operating system can directly read and write this memory metadata in the target physical memory to jointly manage it. This way, compared to each operating system managing its own set of memory metadata, the memory metadata does not increase in size with the number of operating systems, reducing memory noise and improving system scalability.
[0028] In a second aspect, a memory management device is provided, applied to a first operating system (OS) in a computer device, the computer device including multiple OSs and target physical memory, the first OS being any one of the multiple OSs, the memory management device including at least one module, the at least one module being used to execute the memory management method described in the first aspect above.
[0029] For example, the memory management device includes: a global physical memory management module, configured to obtain information on global free memory shared by the multiple OSes from the target physical memory, wherein the global free memory refers to unallocated memory space in the target physical memory; allocate a first physical memory space for the first OS based on the information on the global free memory; and update the information on the global free memory based on the first physical memory space.
[0030] Optionally, the memory management device further includes a local physical memory management module; the local physical memory management module is used to update the information of local free memory based on the first physical memory space, wherein the local free memory refers to the free memory space in the physical memory space allocated to the first OS, and the updated information of the local free memory includes the information of the first physical memory space.
[0031] Optionally, the local physical memory management module is further configured to allocate a second physical memory space for the kernel of the first OS from the local free memory in response to a first memory request from the kernel of the first OS.
[0032] Optionally, the local physical memory management module is further configured to release the third physical memory space to the local free memory in response to a first memory release request from the kernel of the first OS.
[0033] Optionally, the memory management device further includes a virtual memory management module; the virtual memory management module is configured to allocate a first virtual memory space for the kernel of the first OS from the virtual memory space managed by the first OS in the shared virtual memory in response to a second memory request from the kernel of the first OS, wherein the shared virtual memory is virtual memory shared by the multiple OSes, and each OS is used to manage a portion of the virtual memory space in the shared virtual memory; the local physical memory management module is configured to allocate a fourth physical memory space for the kernel of the first OS from the local free memory; the virtual memory management module is further configured to add an address mapping relationship between the first virtual memory space and the fourth physical memory space in the kernel page table of the first OS, wherein the second-level page table corresponding to the shared virtual memory in the kernel page table of the first OS is a shared page table of the multiple OSes, and the addresses of the portion of the virtual memory space in the shared virtual memory managed by the multiple OSes do not overlap in the page table entries corresponding to the shared page table.
[0034] Optionally, the virtual memory management module is further configured to respond to a second memory release request from the kernel of the first OS, and if the second virtual memory space is a virtual memory space managed by the first OS in shared virtual memory, release the second virtual memory space and trigger the local physical memory management module to release the third physical memory space corresponding to the second virtual memory space to the local free memory.
[0035] Optionally, the virtual memory management module is further configured to, if the second virtual memory space is not a virtual memory space managed by the first OS, determine a second OS for managing the second virtual memory space, and add the address of the second virtual memory space to the lockless linked list of the second OS, wherein the lockless linked list is used to store the address of the virtual memory space corresponding to the physical memory space requested and to be released by the corresponding OS.
[0036] Optionally, the virtual memory management module is further configured to, if the lock-free linked list of the first OS is not empty, obtain the address of one or more virtual memory spaces in the lock-free linked list of the first OS, wherein the lock-free linked list is used to store the address of the virtual memory space corresponding to the physical memory space requested by the corresponding OS and to be released; based on the address of the one or more virtual memory spaces, release the one or more virtual memory spaces, and trigger the local physical memory management module to release the physical memory space corresponding to the one or more virtual memory spaces to the local free memory.
[0037] Optionally, the local physical memory management module is further configured to: if the local free memory includes a continuous free space with a size not less than a first threshold, then release the continuous free space to the global free memory.
[0038] Optionally, the target physical memory also stores memory metadata shared by the multiple OSes, the memory metadata including the status data of the target physical memory, and the memory management device is further configured to: update the memory metadata based on the allocated physical memory space or the released physical memory space.
[0039] Thirdly, a computer device is provided, the computer device including a CPU and a target physical memory, wherein the CPU runs multiple operating systems, each of the operating systems being configured to execute the memory management method described in the first aspect above, so as to manage the target physical memory.
[0040] Fourthly, a computer-readable storage medium is provided, wherein instructions are stored therein, which, when executed on a computer device, cause the computer device to perform the memory management method described in the first aspect.
[0041] Fifthly, a computer program product containing instructions is provided, which, when run on a computer device, causes the computer device to perform the memory management method described in the first aspect.
[0042] The technical effects achieved by the second to fifth aspects mentioned above are similar to those achieved by the corresponding technical means in the first aspect, and will not be repeated here. Attached Figure Description
[0043] Figure 1 is a schematic diagram of the structure of a computer device to which the memory management method provided in the embodiment of this application is applied;
[0044] Figure 2 is a schematic diagram of another computer device to which the memory management method provided in the embodiments of this application is applied;
[0045] Figure 3 is a schematic diagram of the structure of another computer device to which the memory management method provided in the embodiments of this application is applied;
[0046] Figure 4 is a flowchart of a physical memory management method provided in an embodiment of this application;
[0047] Figure 5 is a flowchart of an OS allocating shared memory to the kernel according to an embodiment of this application;
[0048] Figure 6 is a schematic diagram of a shared virtual memory provided in an embodiment of this application;
[0049] Figure 7 is a schematic diagram of a scenario in which multiple operating systems share a second-level page table and each manages a portion of the entries in the second-level page table, as provided in an embodiment of this application.
[0050] Figure 8 is a flowchart of an OS releasing shared memory according to an embodiment of this application;
[0051] Figure 9 is a schematic diagram of an lockless linked list of an OS provided in an embodiment of this application;
[0052] Figure 10 is an exemplary flowchart of an OS allocating shared memory to the kernel according to an embodiment of this application;
[0053] Figure 11 is an exemplary flowchart of an OS releasing shared memory according to an embodiment of this application;
[0054] Figure 12 is a schematic diagram of the structure of a memory management device provided in an embodiment of this application. Detailed Implementation
[0055] To make the objectives, technical solutions, and advantages of the embodiments of this application clearer, the implementation methods of this application will be further described in detail below with reference to the accompanying drawings.
[0056] Before providing a detailed explanation of the embodiments of this application, some key terms involved in the embodiments of this application will be introduced first.
[0057] 1. Operating System
[0058] The operating system (OS) is the core program in a computer device, used to manage the hardware and software resources of the computer device, and acts as a bridge between the user and the computer hardware. In a broad sense, an OS includes the kernel, drivers, interface libraries, peripheral components, etc. Currently, the CPU in a computer device often contains multiple CPU cores, and multiple OS kernels can run on these multiple CPU cores. Such a computer device running multiple OS kernels can be called a multi-operating system.
[0059] 2. Physical memory
[0060] Physical memory refers to the memory space obtained through the actual hardware storage devices in a computer device. The size of physical memory depends on how many hardware storage devices the computer device has. Physical memory can be encoded using physical memory addresses, also known as physical addresses (PA).
[0061] 3. Virtual memory
[0062] Virtual memory is a virtualized memory space provided by the operating system. In computer devices, the operating system can provide independent, contiguous virtual memory spaces for user processes and the kernel, making the user process or kernel believe that it has exclusive access to the entire memory, but the actual data may be stored in physical memory or on disk. Virtual memory is encoded using virtual memory addresses, which are also called virtual addresses (VA) or logical addresses. In computer devices, the input address for memory access operations is often a virtual address. Computer devices can store a mapping relationship between virtual addresses and physical addresses. Through this mapping relationship, the virtual address to be accessed can be mapped to the corresponding physical address, thereby accessing the data stored at that physical address.
[0063] The mapping relationship between virtual addresses and physical addresses described above can be implemented using page tables. Page tables can be divided into single-level page tables and multi-level page tables. In a single-level page table, each page table entry stores the base address of the physical page corresponding to the virtual address. Based on the base address of the physical page and the page offset, the physical address corresponding to the virtual address can be determined. In a multi-level page table, the page table entries in the first-level page table store the base address of the next-level page table, and the page table entries in the last-level page table store the base address of the physical page corresponding to the virtual address.
[0064] 4. Virtual address space
[0065] The virtual address space can include the user virtual address space and the kernel virtual address space. The user virtual address space is the address range of virtual memory space provided by the operating system for user processes. Each user process has its own independent virtual address space. The kernel virtual address space is the address range of virtual memory space owned by the operating system kernel. The mapping between virtual addresses and physical addresses within the virtual address space is carried out by page tables.
[0066] 5. Shared memory
[0067] In a single-OS system, by mapping a physical address to the virtual address spaces of multiple processes, processes corresponding to different virtual address spaces can share information in the physical memory space corresponding to that physical address. The physical memory space corresponding to that physical address is the shared memory of multiple processes.
[0068] In a multi-OS system, mapping a physical address to the kernel virtual address spaces of multiple OSes allows the kernels of these OSes to share information in the physical memory space corresponding to that physical address. This physical memory space is then considered the shared memory of the multiple OS kernels. The shared memory management discussed in this application primarily refers to the management of shared memory among the kernels of multiple OSes.
[0069] 6. Memory Management
[0070] Memory management typically includes physical memory management, virtual memory management, and page table management. Physical memory management refers to the allocation and release of physical memory in units of physical pages. A physical page can be 4KB in size. Virtual memory management refers to the allocation and release of virtual memory. Page table management refers to the management of page tables that map virtual memory addresses to physical memory addresses. This includes creating the corresponding mapping in the page table when allocating virtual memory and deleting the corresponding mapping when releasing virtual memory.
[0071] 7. Background noise
[0072] In the field of computer science, noise floor refers to the overhead incurred by the underlying software after removing upper-layer business logic. Noise floor can include memory noise floor and performance noise floor. For example, after an OS boots up, without running any upper-layer applications, the memory consumed is the OS's current memory noise floor, and the CPU utilization rate is the OS's current performance noise floor. Furthermore, the noise floor also changes dynamically with the changes in upper-layer business logic. For instance, when the load on upper-layer business logic increases, the services provided by the OS to run these services may increase, and correspondingly, the noise floor will also increase.
[0073] The application scenarios involved in the memory management method provided in the embodiments of this application will be introduced next.
[0074] Due to limitations imposed by physical laws and manufacturing costs, improving the performance of a single-core CPU is quite challenging. However, scenarios such as artificial intelligence, high-performance computing, and data communication processing place significant demands on CPU performance. Therefore, CPUs are gradually evolving towards multi-core and many-core architectures.
[0075] However, multi-core CPUs introduce cache inconsistency issues when implementing parallel computing via shared memory. Cache inconsistency refers to the inconsistent states of multiple copies of the same data stored in the private caches of different CPU cores. For example, if CPU core 1 modifies data stored in its own cache, while CPU core 2 still reads the old value, it can lead to program errors. Although cache consistency can be achieved at the hardware level of a computer device, the cost of implementing cache consistency at the hardware level is high when there are many CPU cores. In addition, current OS kernels often contain many global variables and global locks. As the number of CPU cores increases, lock contention and memory access conflicts become more severe when accessing these global variables and global locks, even becoming performance bottlenecks. To solve these problems, deploying multiple OSs on a single computer device has become the mainstream solution in the industry. For example, one OS runs on each CPU core, and each OS uses the cache of its own CPU core. When sharing data across OSs, data is read and written directly in memory. This reduces cross-core interference, lowers the cost of achieving cache consistency, and mitigates lock contention and memory access conflicts.
[0076] In a computer device with a single operating system (OS), memory management is handled by that OS. However, in a computer device with multiple OSes, how these OSes manage memory becomes a significant issue. Currently, related technologies employ one OS as a central OS to manage the computer device's memory. This central OS, upon startup, can obtain and manage information about the available memory on the computer device. Based on this, other OSes on the computer device can request memory from the central OS through cross-OS communication messages after startup. When there are a large number of OSes on the computer device, cross-OS communication messages can incur significant performance overhead. Furthermore, the central OS faces heavy processing pressure and can easily become a performance bottleneck. Moreover, if the central OS fails, the entire system will become inoperable, resulting in poor system reliability.
[0077] To address the problems existing in related technologies, this application provides a memory management method for a multi-OS computer device. In this method, the target physical memory of the computer device stores information about globally free memory shared by multiple OSes. Based on this, any OS can obtain the information about globally free memory from the target physical memory and then use this information to allocate physical memory space for itself. Afterwards, the information about globally free memory in the target physical memory is updated so that other OSes can allocate physical memory based on this information. Therefore, in this application embodiment, each OS can allocate physical memory independently based on the shared information about globally free memory in the target physical memory, without needing to request memory through cross-OS communication messages, resulting in lower performance overhead. Furthermore, since each OS can allocate physical memory independently, decentralized memory management is achieved. This avoids the problem of high processing pressure on the central OS. Moreover, even if one OS crashes, it will not affect the operation of other OS kernels, improving system reliability.
[0078] The memory management method provided in this application can be applied to computer devices running multiple operating systems. For example, FIG1 is a schematic diagram of the structure of a computer device provided in this application. As shown in FIG1, the computer device 10 includes a CPU 11 with multiple CPU cores and memory 12. Each CPU core runs one operating system. The multiple operating systems running on the multiple CPU cores jointly manage all physical memory, that is, jointly manage memory 12. For example, as shown in FIG1, the CPU 11 includes CPU core 111 and CPU core 112, wherein CPU core 111 runs OS 113, and CPU core 112 runs OS 114. In this case, OS 113 and OS 114 can jointly manage memory 12.
[0079] In some embodiments, CPU 11 may have multi-level caches. For example, as shown in Figure 1, CPU 11 has a three-level cache. Each CPU core has its own private L1 cache and L2 cache, while the L3 cache can be shared by multiple CPU cores. The read / write speed of the L1 cache is greater than that of the L2 cache, and the read / write speed of the L2 cache is greater than that of the L3 cache.
[0080] Memory 12 is an internal memory capable of directly exchanging data with CPU 11. It can read and write data at any time, serving as temporary data storage for the OS or other running programs. Memory 12 may include one or more types of memory, such as random access memory (RAM) or read-only memory (ROM). For example, RAM may be dynamic random access memory (DRAM), such as synchronous dynamic random-access memory (SDRAM) or double data rate synchronous dynamic random-access memory (DDR); or, RAM may be storage class memory (SCM). Memory may also include other types of RAM, such as static random access memory (SRAM). For read-only memory, for example, it may be programmable read-only memory (PROM) or erasable programmable read-only memory (EPROM). Alternatively, memory 12 can be a dual in-line memory module (DIMM), i.e., a module composed of DRAM, or a solid state disk (SSD). Memory 12 can exist independently and be connected to CPU 11 via a communication bus.
[0081] In this embodiment of the application, the memory 12 stores program code and data. The CPU 11 can implement the relevant functions of the computer device by executing the program code in the memory 12 and calling the corresponding data.
[0082] For example, memory 12 stores the program code of the OS and the required data. Each CPU core in CPU 11 can run the OS by executing the program code in memory 12 and calling the data stored in memory 12, thereby executing the memory management method in the following embodiments to manage memory 12.
[0083] Figure 2 is a schematic diagram of another computer device provided in an embodiment of this application. As shown in Figure 2, the computer device 20 may include a CPU 21 with multiple CPU cores and memory 22. Each CPU core runs an operating system (OS), and the memory 22 includes physical memory allocated to each OS, with the physical memory of each OS isolated from each other. In addition, the memory 22 may also include physical memory shared by multiple OSes, allowing them to share data.
[0084] For example, referring to Figure 2, CPU 21 includes CPU core 211, CPU core 212, and CPU core 213. CPU core 211 runs OS 214, and memory 22 includes DDR 221 allocated for OS 214; CPU core 212 runs OS 215, and memory 22 includes DDR 222 allocated for OS 215; CPU core 213 runs OS 216, and memory 22 includes DDR 223 allocated for OS 216; in addition, memory 22 also includes DDR 224 shared by multiple OSes.
[0085] It should be noted that the above description only uses DDR memory 22 as an example. Memory 22 may also include other types of memory. In addition, CPU 21 also has multi-level cache. For related information, please refer to the above description of the computer device shown in Figure 1. It will not be repeated here.
[0086] In this embodiment of the application, memory 22 stores program code and data. CPU 21 can implement the relevant functions of the computer device by executing the program code in memory 22 and calling the corresponding data.
[0087] For example, memory 22 stores the program code of the OS and the required data. Each CPU core in CPU 21 can run the OS by executing the program code in memory 22 and calling the data stored in memory 22, thereby executing the memory management method in the following embodiment to realize the management of shared memory of multiple OSs in memory 22.
[0088] Figure 3 is a schematic diagram of another computer device provided in an embodiment of this application. As shown in Figure 3, the computer device 30 includes a CPU 31 and memory 32. The CPU 31 runs multiple virtual machines, each virtual machine including a virtual machine OS and an application running on the virtual machine OS. The virtual machine OS can also be called a guest OS. The memory 32 includes physical memory allocated to each virtual machine OS, and the physical memory of each virtual machine OS is isolated from each other. In addition, the memory 32 may also include physical memory shared by multiple virtual machine OSes, through which multiple virtual machine OSes can share data.
[0089] For example, referring to Figure 3, virtual machines OS 311, OS 312, and OS 313 run on CPU 31. Memory 32 includes DDR 321 allocated for use by virtual machine OS 311, DDR 322 allocated for use by virtual machine OS 312, and DDR 323 allocated for use by virtual machine OS 313; in addition, memory 32 also includes DDR 324 shared by virtual machine OS 311, virtual machine OS 312, and virtual machine OS 313.
[0090] It should be noted that the above description only uses DDR memory 32 as an example. Memory 32 may also include other types of memory. In addition, CPU 31 also has multi-level cache. For related information, please refer to the previous description of the computer device shown in Figure 1. It will not be repeated here.
[0091] In this embodiment, memory 32 stores program code and data. CPU 31 can execute the program code in memory 32 and call the corresponding data to realize the relevant functions of the computer device.
[0092] For example, memory 32 stores the kernel program code and required data of each virtual machine OS. CPU 31 can run the virtual machine OS by executing the program code in memory 32 and calling the data stored in memory 32, thereby executing the memory management method in the following embodiments and realizing the management of the shared memory of the virtual machine OS in memory 32.
[0093] It is worth noting that the computer devices shown in Figures 1 to 3 above may also include other types of processors besides CPUs. For example, the computer devices may also include graphics processing units (GPUs), application-specific integrated circuits (ASICs), one or more digital signal processors (DSPs), or one or more field programmable gate arrays (FPGAs), etc.
[0094] Furthermore, the device structures shown in Figures 1 to 3 above do not constitute a limitation on the computer device. The computer device may include more or fewer components than shown in the figures, or combine certain components, or have different component arrangements. For example, the computer device may also include a communication bus for communication between components, a communication interface for communication with other external devices or communication networks, input / output devices, etc. The embodiments of this application do not limit this.
[0095] Finally, the aforementioned computer device can be a general-purpose computer device or a special-purpose computer device. For example, the computer device can be a user terminal such as a desktop computer, a portable computer, or a smartphone, or it can be a base station or core network element in a wireless communication network, or it can be a server in a cloud computing scenario, or it can be a network device such as a router or firewall in a data communication network. This application embodiment does not limit this.
[0096] The memory management method provided in the embodiments of this application will be described in detail below.
[0097] Figure 4 is a flowchart of a memory management method provided in an embodiment of this application. This method can be executed by each of multiple operating systems deployed in a computer device. For example, it can be executed by each operating system in the computer device shown in Figure 1 or Figure 2, or by a virtual machine operating system in the computer device shown in Figure 3. In the following embodiments, the first operating system in the computer device is used as an example to describe the detailed process of each operating system executing the memory management method. Referring to Figure 4, the method includes the following steps:
[0098] S401: Obtain information about the global free memory shared by multiple operating systems from the target physical memory. Global free memory refers to the unallocated memory space in the target physical memory.
[0099] Computer devices include memory, and the target physical memory can be the memory space within the computer device that is jointly managed by multiple operating systems. The first operating system can be the operating system running on the first CPU core among the multiple CPU cores included in the computer device's CPU.
[0100] In this embodiment, the target physical memory stores information about globally free memory. Globally free memory refers to memory space in the target physical memory that has not yet been allocated to any operating system. The information about globally free memory may include its size, physical address range, etc. Based on this, the first operating system can read the information about globally free memory from the target physical memory during startup or when it detects insufficient physical memory, thereby determining which memory space is currently available in the target physical memory and allocating usable physical memory for itself.
[0101] It should be noted that the information of the global free memory is shared by multiple operating systems. In this embodiment, multiple operating systems can read and write the information of the global free memory by accessing the target physical memory, rather than by accessing their own cache or the cache of other CPU cores. Therefore, to avoid memory access conflicts, when the first operating system reads the information of the global free memory from the target physical memory, it can use atomic operations or locking mechanisms to ensure the consistency of reading and writing the information of the global free memory.
[0102] S402: Based on information about global free memory, allocate the first physical memory space to the first OS.
[0103] After reading the information of global free memory from the target physical memory, the first OS can allocate first physical memory space for itself from the global free memory based on the information of global free memory.
[0104] In some embodiments, the first OS can allocate a first physical memory space for itself from the global free memory in units of physical pages, based on its required physical memory. The size of a physical page can be 4KB. For example, the first OS can allocate n physical pages from the global free memory, in which case the first physical memory space includes n physical pages, where n is an integer greater than or equal to 1.
[0105] In other embodiments, the first OS may allocate a first physical memory space for itself from the global free memory in units of memory blocks larger than the physical page size. For example, the size of a memory block may be 32MB, and the first OS may allocate n 32MB memory blocks for itself from the global free memory. In this case, the first physical memory space includes n 32MB memory blocks. Here, n is an integer greater than or equal to 1.
[0106] S403: Update the information of global free memory based on the first physical memory space.
[0107] After allocating its first physical memory space, the first operating system can update the global free memory information so that other operating systems can allocate physical memory to themselves based on the updated global free memory information. The updated global free memory information does not include the information about the first physical memory space.
[0108] For example, the information about global free memory includes the size of global free memory and its physical address range. Based on this, the first OS can subtract the size of the first physical memory space from the size of global free memory to obtain the new size of global free memory, and remove the physical addresses of the first physical memory space from the physical address range of global free memory to obtain the new physical address range of global free memory.
[0109] S404: Based on the first physical memory space, update the information of local free memory. Local free memory refers to the free memory space in the physical memory space allocated to the first OS.
[0110] In some possible implementations, the cache of the first CPU core where the first OS resides may also store information about local free memory, or the private memory of the first OS may store information about local free memory. Local free memory refers to the free memory space within the physical memory space allocated to the first OS. The information about local free memory may include its size and physical address. Based on this, after allocating its own first physical memory space, the first OS can also update its local free memory information. The updated local free memory information includes information about the first physical memory space.
[0111] For example, the first OS can add the current size of the local free memory to the size of the first physical memory space to obtain the new size of the local free memory, and add the physical address of the first physical memory space to the physical address of the local free memory to obtain the new physical address of the local free memory.
[0112] When the first OS maintains information about local free memory, it can allocate physical memory for the kernel from local free memory using the method described in S405 below when it receives a physical memory request from the kernel. When it receives a physical memory release request from the kernel, it can release the physical memory space using the methods described in S406 and S408 below. In other words, the first OS can manage the local physical memory space obtained from global free memory using the methods described in S405 to S408 below.
[0113] S405: In response to the first memory request from the kernel of the first OS, allocate a second physical memory space for the kernel of the first OS from local free memory.
[0114] The first memory request can be a physical memory request triggered by the kernel, and this first memory request can carry the size of the physical memory space required by the kernel. Based on this, in response to the first memory request, the first OS can allocate a second physical memory space for the kernel from local free memory based on the size of the physical memory space required by the kernel.
[0115] As described above, the first OS maintains information about local free memory. Based on this, the first OS can select a free memory space of appropriate size from the local free memory as the second physical memory space. This second physical memory space can be either contiguous or non-contiguous.
[0116] For example, if the kernel requires 256K of physical memory space, the first OS can select 256K of free memory space from local free memory as the second physical memory space.
[0117] It should be noted that, in one possible scenario, the first operating system (OS) can detect whether the size of its local free memory is less than the size of the physical memory space required by the kernel. If the size of its local free memory is less than the size of the physical memory space required by the kernel, it indicates that the current local free memory is insufficient. In this case, the first OS can first request available physical memory from the global free memory through S401 to S403 as described above. Then, it can allocate a second physical memory space for the kernel from the requested available physical memory space.
[0118] After allocating memory to the kernel in the manner described above, the first OS can also update the memory metadata based on the second physical memory space allocated to the kernel. This memory metadata is used to indicate the usage status of the target physical memory.
[0119] In some embodiments, the memory metadata is stored in the target physical memory and is shared by multiple operating systems. The first operating system can update the memory metadata stored in the target physical memory based on the second physical memory space allocated to the kernel.
[0120] For example, memory metadata may include a page array, the size of the target physical memory, and the current size of global free memory. The page array comprises multiple array elements, each describing the state and attributes of a physical page in the target physical memory. Based on this, in this embodiment, the first OS can update at least one array element in the page array that describes at least one physical page included in the second physical memory space, according to the second physical memory space.
[0121] For example, array elements can include physical page status flags, reference counts, mapping counts, etc. The status flags indicate whether the physical page is locked, dirty, or active. The reference count indicates the number of entities currently using the physical page; when the reference count is 0, the physical page can be released. These entities can include page tables, caches, device drivers, etc. The mapping count indicates the number of times the physical page has been mapped, i.e., the number of times the physical page has been mapped by the page table. Based on this, after allocating the second physical memory space to the kernel, the first OS can determine the status of each physical page based on the kernel's operations on each physical page in the second physical memory space, and then modify the status flags in the array elements corresponding to each physical page. Furthermore, if each physical page in the second physical memory space is mapped to virtual memory space, the first OS can increment the mapping count of each physical page by 1. Additionally, the first OS can update the reference count of physical pages based on the entities using each physical page in the second physical memory space.
[0122] It should be noted that in this embodiment, multiple operating systems share the memory metadata, and each operating system can directly read and write the memory metadata in the target physical memory. Based on this, the first operating system can update the memory metadata through atomic operations, or the first operating system can acquire a write lock on the metadata to be modified in the memory metadata before updating it, thereby mitigating memory access conflicts caused by multiple operating systems accessing the memory metadata simultaneously.
[0123] S406: In response to the first memory release request from the kernel of the first OS, release the third physical memory space to local free memory.
[0124] The first memory release request can be a kernel-triggered physical memory release request, and this first memory release request can carry the address of the third physical memory space to be released. Based on this, in response to the first memory release request, the first OS can update the information of local free memory based on the address of the third physical memory space, thereby releasing the third physical memory space to local free memory.
[0125] For example, the first OS can determine the size of the third physical memory space based on the address of the third physical memory space, add the size of the third physical memory space to the current size of the local free memory to obtain the new size of the local free memory, and add the physical address of the third physical memory space to the physical address range of the local free memory to obtain the new physical address range of the local free memory.
[0126] S407: Check whether the local free memory includes a contiguous free space with a size not less than the first threshold. If yes, execute S408; otherwise, end the operation.
[0127] If the first OS requests physical memory space from global free memory at a granularity greater than one physical page, then after releasing the third physical memory space to local free memory, the first OS can also detect whether there is a contiguous free space in local free memory with a size not less than the first threshold.
[0128] The first threshold can be equal to the allocation granularity when the first OS allocates memory for itself from global free memory. For example, the allocation granularity is a memory block larger than a physical page size. For instance, if the allocation granularity is a 32M memory block, then the first threshold is 32M.
[0129] S408: Release contiguous free space in local free memory with a size not less than the first threshold to global free memory.
[0130] If there is a contiguous free space in the local free memory with a size not less than the first threshold, the first OS can release the contiguous free space to the global free memory.
[0131] For example, the first OS can update the information of local free memory and the information of global free memory based on the contiguous free space, thereby releasing the contiguous free space into global free memory. The updated local free memory information does not include the information of the contiguous free space, while the updated global free memory information includes the information of the contiguous free space.
[0132] For example, the first operating system can subtract the size of the contiguous free space from the current size of local free memory to obtain the new size of local free memory, and then remove the physical address of the contiguous free space from the current physical address range of local free memory to obtain the new physical address range of local free memory. Similarly, it can add the size of the contiguous free space to the current size of global free memory to obtain the new size of global free memory, and then add the physical address of the contiguous free space to the current physical address range of global free memory to obtain the new physical address range of global free memory.
[0133] Optionally, after releasing the third physical memory space to local free memory, the first OS can also update the shared memory metadata stored in the target physical memory based on the third physical memory space. For example, it can update the reference count, mapping count, etc., in the array elements corresponding to the physical pages included in the third physical memory space in the page array of the memory metadata.
[0134] Optionally, after releasing contiguous free space of at least the first threshold from local free memory to global free memory using the above method, the first OS can also update the memory metadata. For example, it can update the size of global free memory included in the memory metadata. The corresponding update method can be referred to the previous description and will not be repeated here.
[0135] S405 to S408 primarily describe the process by which the first OS handles kernel physical memory allocation and release requests based on local free memory information, when the first OS maintains such information. Optionally, if the first OS does not maintain local free memory information, in response to a kernel physical memory allocation request from the first OS, the first OS can directly allocate the required physical memory space from global free memory in units of physical pages, based on the size of the physical memory space needed by the kernel. The implementation method for allocating physical memory space from global free memory can refer to the methods described in S401 to S403. In response to a kernel physical memory release request from the first OS, the first OS can directly release the physical memory space to be released to global free memory. The corresponding implementation method can refer to the relevant implementation method in S408, and will not be repeated here.
[0136] In this embodiment, the target physical memory in the computer device stores information about globally free memory shared by multiple operating systems (OSs). Based on this, each OS can obtain the information about globally free memory from the target physical memory and then use this information to allocate physical memory space for itself. Afterwards, the information about globally free memory in the target physical memory is updated so that other OSs can allocate physical memory based on this information. Therefore, each OS can allocate physical memory independently based on the shared information about globally free memory in the target physical memory, without needing to send messages to a central OS to request memory, thus achieving decentralized memory management. This alleviates the performance problems associated with memory management by a central OS. Furthermore, even if one OS crashes, it will not affect the operation of other OSs, improving system reliability.
[0137] Furthermore, in this embodiment, the first OS can maintain information about local free memory, which includes the physical memory space available to the first OS that it has requested from global free memory. The first OS can allocate memory for its own kernel based on this local free memory information. Since the local free memory information is stored in the cache of the first CPU core where the first OS resides or in the first OS's private memory, it is memory information managed solely by the first OS. Therefore, compared to allocating memory for the OS kernel based on information from global free memory shared by multiple OSs, allocating memory based on local free memory information reduces the performance overhead caused by cross-OS atomic operations or cross-OS lock operations during access to global free memory information, resulting in strong scalability. Moreover, when the local free memory information is stored in the cache of the first CPU core where the first OS resides, since this information is managed and used solely by the first OS, meaning it is not accessed by other CPU cores, it is only necessary to maintain cache consistency of this local free memory information within the first CPU core. There is no need to maintain cache consistency across multiple CPU cores, reducing the performance overhead of maintaining cache consistency and improving CPU performance.
[0138] In this embodiment, the target physical memory stores shared memory metadata from multiple operating systems. Each operating system can directly read and write this memory metadata in the target physical memory to jointly manage it. This way, compared to each operating system managing its own set of memory metadata, the memory metadata does not increase with the number of operating systems, reducing memory noise and improving system scalability.
[0139] The preceding text primarily described the process by which the first OS requests physical memory from global free memory and manages that physical memory. Building upon the physical memory management methods described above, this application also provides a method for managing shared memory across multiple OSes. The following section will continue to use the first OS as an example to describe the process of each OS managing shared memory.
[0140] For example, Figure 5 illustrates a shared memory allocation process, which may include the following steps:
[0141] S501: In response to a second memory request from the kernel of the first OS, allocate a first virtual memory space for the kernel of the first OS from the virtual memory space managed by the first OS in the shared virtual memory. Each OS is used to manage a portion of the virtual memory space in the shared virtual memory.
[0142] In this embodiment of the application, the second memory request may be triggered by the kernel of the first OS when it starts up or when it needs to share memory during operation. The second memory request is used to request the allocation of shared memory.
[0143] The kernel virtual address spaces of different operating systems (OS) in a computer device are isolated from each other. In the embodiments of this application, the virtual memory space corresponding to a specified virtual address range in the kernel virtual address space of each OS can be used as shared virtual memory for multiple OSs, that is, shared virtual memory for multiple OSs. For example, if the size of the kernel virtual address space of each OS is 128T, the virtual memory space indicated by the virtual address space from 0 to 512G can be determined as shared virtual memory for multiple OSs. Based on this, each OS can be used to manage a portion of the virtual memory space in the shared virtual memory. The first OS can allocate a first virtual memory space for its kernel from the virtual memory space it manages in the shared virtual memory.
[0144] For example, referring to Figure 6, the computer device includes two operating systems (OS). Each OS's kernel virtual address space is 128TB in size. The virtual memory space indicated by the virtual address space from 0 to 512GB can be designated as shared virtual memory for the multiple OSs. The first OS manages the virtual memory space indicated by the virtual address space from 0 to 256GB, and the second OS manages the virtual memory space indicated by the virtual address space from 256GB to 512GB. Based on this, the first OS can determine the virtual address of the first virtual memory space from the virtual address space from 0 to 256GB.
[0145] It should be noted that the second memory request can carry the size of the memory space required by the kernel. The first OS can allocate a first virtual memory space for the kernel, with the size not less than the required memory space, based on the size of the memory space required by the kernel, using pages as the granularity. The size of a page is 4KB.
[0146] S502: Allocate a fourth physical memory space for the kernel of the first OS.
[0147] After allocating the first virtual memory space for the kernel of the first OS, the first OS can allocate a fourth physical memory space for the kernel.
[0148] In some embodiments, the first OS maintains information about local free memory. Based on the size of the first virtual memory space and the information about local free memory, the first OS can allocate a free memory space of the same size as the first virtual memory space, i.e., a fourth physical memory space, from the local free memory for the kernel. This fourth physical memory space can be a contiguous physical memory space or a non-contiguous physical memory space. The implementation method of the first OS allocating the fourth physical memory space from local free memory for the OS kernel can refer to the method described in S405 of the aforementioned embodiments, and will not be repeated here.
[0149] It should be noted that, in one possible scenario, the first OS can detect whether the size of its local free memory is less than the size of its first virtual memory space. If the size of its local free memory is less than the size of its first virtual memory space, it indicates that the current local free memory is insufficient. In this case, the first OS can first request available physical memory from the global free memory through the aforementioned S401 to S403 steps. Then, it allocates a fourth physical memory space for the first OS kernel from the requested available physical memory.
[0150] In other embodiments, the first OS does not maintain information about local free memory. The first OS can directly allocate a free physical memory space of the same size as the first virtual memory space from the global free memory, i.e., a fourth physical memory space, based on the size of the first virtual memory space. The implementation method of allocating the fourth physical memory space from the global free memory can refer to the relevant implementation methods in S401 to S403 above, and will not be repeated here.
[0151] S503: Add an address mapping relationship between the first virtual memory space and the fourth physical memory space in the kernel page table. The second-level page table corresponding to the shared virtual memory in the kernel page table is a shared page table for multiple OSes, and the addresses of some virtual memory spaces in the shared virtual memory managed by multiple OSes do not overlap in the page table entries corresponding to them in the shared page table.
[0152] After allocating a fourth physical memory space for the kernel, the first OS can map the first virtual memory space to the fourth physical memory space so that the kernel of the first OS can access the fourth physical memory space through the virtual address of the first virtual memory space.
[0153] It should be noted that each OS kernel has a kernel page table, which carries the address mapping relationship between the virtual memory space and physical memory space used by the kernel. In this embodiment, the kernel page table of each OS is an m-level page table. The first OS can divide the virtual address of the first virtual memory space into m+1 address segments, use the last address segment as the page offset of the physical page corresponding to the virtual address, and create page table entries corresponding to the address segments in the corresponding page tables at each level based on the first m address segments. The i-th level page table indicated by the (i-1)th address segment of the first virtual memory space includes the page table entry corresponding to the i-th address segment. The page table entry corresponding to the i-th address segment is used to carry the base address of the (i+1)th level page table containing the page table entry corresponding to the (i+1)th address segment, or the base address of the physical page. Here, i is an integer greater than 1.
[0154] For example, if the kernel page table is a four-level page table, and a virtual address in the first virtual memory space is 48 bits, then this virtual address can be divided into five address segments. Address segment 1 consists of the high 9 bits of the virtual address. The first-level page table includes an entry E1 corresponding to address segment 1, which stores the base address of the second-level page table containing the entry E2 corresponding to address segment 2. Address segment 2 consists of the last 9 bits of address segment 1. The entry E2 in the second-level page table contains the base address of the third-level page table containing the entry E3 corresponding to address segment 3. Address segment 3 consists of the last 9 bits of address segment 2. The entry E3 in the third-level page table contains the base address of the fourth-level page table containing the entry E4 corresponding to address segment 4. Address segment 4 consists of the last 9 bits of address segment 3. The entry E4 in the fourth-level page table contains the base address of the physical page corresponding to this virtual address. Address segment 5 consists of the lower 12 bits of the virtual address, which serves as the offset within the physical page. The physical address corresponding to the virtual address can be determined using the base address of the physical page and the offset within the page.
[0155] It should be noted that, in the embodiments of this application, the second-level page table corresponding to the shared virtual memory in the kernel page table is a shared page table of multiple OSes. That is, in the shared virtual address space, multiple OSes share a single second-level page table.
[0156] Furthermore, in this embodiment, the virtual addresses of the virtual memory spaces managed by multiple operating systems do not overlap in the page table entries corresponding to the shared page table. In other words, in this embodiment, the virtual memory space managed by each operating system in the shared virtual memory is obtained by dividing the virtual address space of the shared virtual memory into segments according to the addresses corresponding to the page table entries in the shared page table. Thus, each operating system manages a portion of the page table entries in the shared page table, and the page table entries managed by different operating systems are different. Moreover, the virtual address corresponding to the page table entry managed by each operating system is the address of the virtual memory space managed by that operating system.
[0157] For example, referring to Figure 7, the computer device includes two operating systems, OS1 and OS2. OS1's kernel page table is a four-level page table. The second-level page table corresponding to the shared virtual memory in the kernel page table includes k entries, and this second-level page table is shared by multiple operating systems. In this case, the virtual memory spaces managed by OS1 and OS2 in the shared virtual memory are divided according to the second address segment of each virtual address in the shared virtual address space. For example, the second address segment of the virtual address space managed by OS1 corresponds to the address segment corresponding to the first k / 2 entries in the shared page table; the second address segment of the virtual address space managed by OS2 corresponds to the address segment corresponding to the last k / 2 entries in the shared page table. Thus, in this shared page table, OS1 and OS2 manage different page table entries, and the virtual address corresponding to the page table entry managed by each OS is the address of the virtual memory space it manages.
[0158] When OS1 and OS2 each manage different page table entries in a shared second-level page table, the third-level and fourth-level page tables under the second-level page table are also managed separately by each OS. For example, as shown in Figure 7, the third-level and fourth-level page tables under the first k / 2 entries of the second-level page table managed by OS1 are managed by OS1, while the third-level and fourth-level page tables under the last k / 2 entries of the second-level page table managed by OS2 are managed by OS2. Since each OS manages different page table entries in the second-level page table and different third-level and fourth-level page tables, meaning that one OS will not modify page table entries in the second-level page table managed by other OSs, there is no need to use cross-OS atomic operations or cross-OS locks, resulting in lower performance overhead, no conflicts or contention between OSs, and strong scalability.
[0159] Furthermore, when multiple operating systems share a second-level page table, the third-level and fourth-level page tables under that second-level page table are also shared by multiple operating systems. In other words, multiple operating systems access shared memory by reading the shared second-level page table and the third-level and fourth-level page tables under it. Based on this, in the embodiments of this application, when multiple CPU cores in the computer device use non-coherent caching, the shared second-level page table and the third-level and fourth-level page tables under it can be stored in the memory of the computer device, rather than in the cache of the CPU core where each operating system resides. On this basis, when multiple operating systems access shared memory, the memory management unit of the computer device can directly read the shared second-level page table and the third-level and fourth-level page tables under it from the memory of the computer device.
[0160] It's also important to note that when multiple operating systems share a second-level page table, the first-level page table within each operating system's kernel page table can be private. That is, each operating system's first-level page table is independent and not shared. For example, as shown in Figure 7, OS1 and OS2 each have their own independent first-level page tables. Because each operating system's first-level page table is independent and not shared, even if the caches of the multiple CPU cores hosting the various operating systems are not consistent, each operating system's first-level page table can still be stored in the cache of its own CPU core. In this case, only cache consistency within the CPU core hosting each operating system needs to be maintained.
[0161] After mapping the first virtual memory space to the fourth physical memory space, the first OS updates its local free memory information based on the information from the fourth physical memory space, so as to process subsequent memory allocation requests based on the updated local free memory information. The updated local free memory information does not include the information from the fourth physical memory space.
[0162] For example, the first OS subtracts the size of the fourth physical memory space from the current size of the local free memory to obtain the new size of the local free memory, and removes the physical address of the fourth physical memory space included in the physical address range of the current local free memory to obtain the new physical address range of the local free memory.
[0163] After allocating memory to the kernel of the first OS using the above method, the first OS can also update memory metadata based on the fourth physical memory space allocated to the kernel of the first OS.
[0164] For example, the first operating system can determine the state of each physical page based on the kernel's operations on each physical page in the fourth physical memory space, and then modify the state flags in the array elements corresponding to each physical page. Furthermore, since each physical page in the fourth physical memory space is mapped to the first virtual memory space, the mapping count of each physical page can be incremented by 1. Additionally, the first operating system can update the reference count of physical pages based on the entities using each physical page in the fourth physical memory space.
[0165] It should be noted that in this embodiment, multiple operating systems share the memory metadata, and each operating system can directly read and write the memory metadata in the target physical memory. Based on this, the first operating system can update the memory metadata through atomic operations, or the first operating system can acquire a write lock on the metadata to be modified in the memory metadata before updating it, thereby mitigating memory access conflicts caused by multiple operating systems accessing the memory metadata simultaneously.
[0166] In this embodiment, the secondary page table corresponding to the shared virtual memory in the kernel page table is a shared page table for multiple operating systems. That is, multiple operating systems share a single secondary page table in the shared virtual address space. Based on this, after the first operating system requests the first virtual memory space and adds the address mapping relationship between the first virtual memory space and the fourth physical memory space to the shared page table, other operating systems can access the fourth physical memory space using the address of the first virtual memory space through the address mapping relationship in the shared secondary page table. Compared to a scheme where each operating system has its own independent page table and does not share page tables, in this embodiment, the first operating system does not need to notify other operating systems to create the corresponding address mapping relationship through communication messages, resulting in lower performance overhead.
[0167] Furthermore, in this embodiment, the shared virtual memory can be divided into multiple parts based on the address segments corresponding to each entry in the shared page tables of multiple operating systems. This allows each operating system to manage a portion of the virtual memory space within the shared virtual memory, and the virtual addresses of the portions managed by each operating system do not overlap in the page table entries corresponding to the portions managed by the multiple operating systems. In other words, each operating system manages a portion of the page table entries, and the page table entries managed by each operating system are different. Based on this, each operating system can allocate its own managed virtual memory space to the kernel. Correspondingly, the operating system can map the allocated virtual memory space to the allocated physical memory space by updating the entries it manages in the shared page tables. In this way, one operating system will not modify the entries managed by another operating system. Therefore, even without cross-OS atomic operations or cross-OS locks, there will be no conflicts when the operating systems access the shared page tables, improving memory management efficiency and providing strong scalability.
[0168] Finally, in this embodiment, the target physical memory stores shared memory metadata from multiple operating systems. Each operating system can directly read and write this memory metadata in the target physical memory to jointly manage it. In this way, compared to each operating system managing its own set of memory metadata, the memory metadata does not increase with the number of operating systems, reducing memory noise and improving system scalability.
[0169] The preceding text primarily described an implementation where each OS manages a portion of the shared virtual memory space, and the page table entries in the shared second-level page table do not overlap. In this implementation, the first OS allocates shared memory to the kernel. In some embodiments, multiple OSs can jointly manage multiple page table entries in the shared second-level page table. In this case, in response to a second memory request, the first OS directly allocates a first virtual memory space from the shared virtual memory to the kernel, and also allocates a fourth physical memory space to the kernel. Subsequently, a page table entry is added to the kernel page table to carry the address mapping between the first virtual memory space and the fourth physical memory space. Since the page table entries in the second-level page table corresponding to the shared virtual memory in the kernel page table are jointly managed by multiple OSs, when adding the page table entry to carry the address mapping between the first virtual memory space and the fourth physical memory space, operations can be performed on the page table entries in the shared page table through cross-OS atomic operations or cross-OS locks. In this implementation, since multiple OSes still share a single second-level page table in the shared virtual address space, after any OS creates or modifies the address mapping relationship between the virtual memory space and the physical memory space, other OSes can access the shared memory through the shared second-level page table. Compared to a scheme where each OS has its own independent page table and does not share the page table, in this embodiment, after an OS requests shared memory, it does not need to notify other OSes to create address mapping relationships in their respective page tables through communication messages, resulting in lower performance overhead.
[0170] The preceding text mainly introduced the implementation method of the first OS allocating shared memory for the kernel. The following section describes the process of the first OS releasing shared memory. For example, referring to Figure 8, this process includes the following steps:
[0171] S801: In response to the second memory release request, check whether the second virtual memory space is the virtual memory space managed by the first OS in the shared virtual memory of multiple OSes. If yes, execute S802; otherwise, execute S803.
[0172] The second memory release request can be triggered by the kernel of the first OS after it has finished using shared memory, or it can be triggered by the first OS when it detects that the size of local memory is less than the local memory watermark. The local memory watermark is the minimum threshold for local free memory set in the first OS. Furthermore, the second memory release request may carry the virtual address of the second virtual memory space to be released.
[0173] As described above regarding the memory allocation process, each of the multiple operating systems (OS) can manage a portion of the shared virtual memory space. Correspondingly, the page table entries corresponding to the address segments of the virtual memory spaces managed by each OS in the shared page table of the multiple OSs do not overlap. Therefore, in this embodiment, in response to a second memory release request, the first OS can detect whether the second virtual memory space to be released is a virtual memory space managed by itself, based on the virtual address of the second virtual memory space to be released carried in the second memory release request.
[0174] For example, the shared page table of multiple operating systems is a two-level page table, and the virtual memory space managed by each operating system is divided according to the second address segment of each virtual address in the shared virtual memory virtual address space. Based on this, according to the second address segment of each virtual address in the shared virtual memory virtual address space, the first operating system can determine the virtual address range of the virtual memory space it manages. For ease of explanation, this virtual address range is referred to as the target virtual address range. Based on this, the first operating system can detect whether the virtual address of the second virtual memory space is within the target virtual address range. If the virtual address of the second virtual memory space is within the target virtual address range, then the second virtual memory space is determined to be the virtual memory space it manages. In this case, the first operating system can execute S802. If the virtual address of the second virtual memory space is not within the target virtual address range, then the second virtual memory space is determined not to be the virtual memory space it manages. In this case, the first operating system can execute S803 and S804 sequentially.
[0175] S802: Release the second virtual memory space and the corresponding fifth physical memory space.
[0176] In some embodiments, when the first OS maintains information about local free memory, after determining that the second virtual memory space is the virtual memory space managed by the first OS in the shared virtual memory, the first OS can release the second virtual memory space and release the fifth physical memory space corresponding to the second virtual memory space to local free memory.
[0177] For example, the first OS can delete the address mapping relationship between the second virtual memory space and the corresponding fifth physical memory space in the page table it manages. Then, it updates the local free memory information according to the fifth physical memory space, thereby releasing the fifth physical memory space to local free memory. The updated local free memory information includes the information of the fifth physical memory space.
[0178] Specifically, the first OS can add the current size of the local free memory to the size of the fifth physical memory space to obtain the new size of the local free memory, and add the physical address of the fifth physical memory space to the physical address range of the local free memory to obtain the new physical address range of the local free memory.
[0179] Optionally, after releasing the fifth physical memory space to local free memory, the first OS may also refer to the methods described in S407 and S408 above to detect whether the local free memory includes a continuous free space with a size not less than the first threshold, and if it is detected that the local free memory includes a continuous free space with a size not less than the first threshold, release the continuous free space to global free memory.
[0180] Optionally, after releasing the fifth physical memory space to local free memory, the first OS can also update the shared memory metadata stored in the target physical memory based on the fifth physical memory space. For example, it can update the reference count, mapping count, etc., in the array elements corresponding to the physical pages included in the fifth physical memory space in the page array of the memory metadata.
[0181] Optionally, after releasing contiguous free space of at least the first threshold from local free memory to global free memory using the above method, the first OS can also update the memory metadata. For example, it can update the size of global free memory included in the memory metadata. The corresponding update method can be referred to the previous description and will not be repeated here.
[0182] In other embodiments, if the first OS does not maintain information about local free memory, the first OS can release the second virtual memory space and directly release the fifth physical memory space corresponding to the second virtual memory space to global free memory.
[0183] For example, the first OS can delete the address mapping relationship between the second virtual memory space and the fifth physical memory space in the page table it manages, and then update the global free memory information, thereby releasing the fifth physical memory space into the global free memory. The updated global free memory information includes the information of the fifth physical memory space. Updating the global free memory information can be done by referring to the method for updating local free memory information described earlier, and will not be repeated here.
[0184] After releasing the fifth physical memory space to global free memory, the first OS can also update the shared memory metadata of multiple OSes stored in the target physical memory based on the fifth physical memory space. The relevant implementation methods can be found in the previous introduction.
[0185] S803: Determine the second OS used to manage the second virtual memory space.
[0186] When the second virtual memory space to be released is not a virtual memory space managed by the first OS in the shared virtual memory, the first OS can determine the second OS to manage the second virtual memory space based on the virtual address of the second virtual memory space.
[0187] As mentioned earlier, based on the second address segment of each virtual address in the shared virtual memory virtual address space, the first OS can determine not only the virtual address range of its own managed virtual memory space, but also the virtual address ranges of the virtual memory spaces managed by other OSes. Based on this, the first OS can identify the OS corresponding to the virtual address range of the second virtual memory space as the second OS managing that second virtual memory space.
[0188] S804: Add the address of the second virtual memory space to the lockless linked list of the second OS. The lockless linked list is used to store the address of the virtual memory space corresponding to the physical memory space requested by the OS and to be released.
[0189] In this embodiment, each OS has a lock-free linked list. The lock-free linked list is a concurrent data structure that allows multiple processes or threads to read and write data within the list atomically without locking. Each OS's lock-free linked list stores the addresses of the virtual memory spaces corresponding to the physical memory spaces requested by that OS and awaiting release. Furthermore, each OS's lock-free linked list can be stored in the target physical memory, and any OS can read and write the lock-free linked lists of other OSes stored in the target physical memory.
[0190] Since the second virtual memory space is managed by the second OS, it can be inferred that it is memory space requested by the second OS. Furthermore, the entry in the shared page table corresponding to the virtual address of the second virtual memory space is also managed by the second OS. That is, the entry in the shared page table corresponding to the virtual address of the second virtual memory space can be modified by the second OS, but not by the first OS. Based on this, after determining the second OS that manages the second virtual memory space, the first OS can add the virtual address of the second virtual memory space to the unlocked linked list of the second OS, so that the second OS can release the second virtual memory space and the corresponding fifth physical memory space. The implementation method of the second OS releasing the second virtual memory space and the corresponding fifth physical memory space can be referred to the implementation method in S802, and will not be repeated here.
[0191] For example, referring to Figure 9, OS1 has a lock-free linked list A, and OS2 has a lock-free linked list B. The address of the virtual memory space that OS1 wants to release is VA1, and OS1 determines that this virtual memory space is a shared virtual memory space managed by OS2. Therefore, OS1 can write VA1 into OS2's lock-free linked list B.
[0192] In some embodiments, each OS can check whether its own lock-free list contains the virtual addresses of one or more virtual memory spaces to be released before processing each shared memory allocation request or shared memory release request. If the lock-free list contains the virtual addresses of one or more virtual memory spaces, the OS can first release the physical memory space corresponding to the one or more virtual memory spaces based on their virtual addresses, and then execute the process for processing the shared memory allocation request or the process for processing the shared memory release request. The implementation method for the OS to release the physical memory space corresponding to one or more virtual memory spaces in the lock-free list can be referred to the aforementioned descriptions of S802 and S803, and will not be repeated here.
[0193] In this embodiment, the secondary page table corresponding to the shared virtual memory in the kernel page table is a shared page table for multiple operating systems. That is, multiple operating systems share a single secondary page table in the shared virtual address space. Based on this, after the first operating system removes the address mapping relationship between the second virtual memory space and the fifth physical memory space in the shared page table, other operating systems will no longer be able to access the fifth physical memory space through the address mapping relationship in the shared secondary page table. Compared to a scheme where each operating system has its own independent page table and does not share a page table, in this embodiment, the first operating system does not need to notify other operating systems to remove the corresponding address mapping relationship through communication messages, resulting in lower performance overhead.
[0194] Based on the address segments corresponding to each entry in the shared page tables of multiple operating systems, shared virtual memory can be divided into multiple parts. This allows each operating system to manage a portion of the shared virtual memory space. Furthermore, the virtual addresses of the portions of virtual memory managed by each operating system do not overlap in the entries within the shared page tables of the multiple operating systems. In other words, each operating system manages a different portion of the entries in the shared page tables. Thus, when an operating system releases shared memory, if the shared memory to be released corresponds to a virtual memory space it manages, that operating system can remove the address mapping between the virtual memory space and the corresponding physical memory space by modifying its own entry in the shared page table. If the shared memory to be released corresponds to a virtual memory space it does not manage, the operating system managing that virtual memory space can release the shared memory by modifying its own entry in the shared page table. Therefore, it can be seen that one OS will not modify the table entries managed by other OSes. In this way, even without cross-OS atomic operations and cross-OS locks, there will be no conflict when different OSes access the shared page table, which improves memory management efficiency and has strong scalability.
[0195] Furthermore, in this embodiment, if the first OS determines that the virtual memory space corresponding to the shared memory to be released is not a virtual memory space managed by itself, the first OS can add the address of the virtual memory space to be released to the lockless linked list of the second OS that manages the virtual memory space. The second OS can then release the shared memory by reading the lockless linked list. Thus, in this embodiment, when releasing shared memory across OSs, the OS does not need to send a message notification to notify the OS that applied for and manages the shared memory to release the memory. This can reduce the performance noise of the OS and alleviate the performance bottleneck that may be caused by message notification when there are a large number of OSs.
[0196] The preceding text primarily introduced the implementation method for releasing shared memory when each OS manages a portion of the shared virtual memory space, and the corresponding page table entries in the shared second-level page table do not overlap across the portions of virtual memory managed by different OSs. Optionally, shared virtual memory can also be jointly managed by multiple OSs; that is, multiple OSs can jointly manage multiple page table entries in the shared second-level page table. In this case, the first OS, responding to the second memory release request, can directly release the address mapping relationship between the second virtual memory space and the fifth physical memory space. Specifically, when releasing the address mapping relationship between the second virtual memory space and the fifth physical memory space, the corresponding shared page table entries can be updated through cross-OS atomic operations or cross-OS locking operations. This prevents other OSs from accessing the fifth physical memory space through the shared page table entries that carry the address mapping relationship between the second virtual memory space and the fifth physical memory space. Therefore, in this implementation, since multiple OSes still share a single second-level page table in the shared virtual address space, any OS can release the shared memory after deleting the address mapping relationship between the virtual memory space and the physical memory space in the shared page table. Compared with the scheme where each OS has its own independent page table and does not share the page table, in this embodiment, when an OS releases the shared memory, it does not need to notify other OSs to delete the corresponding address mapping relationship through communication messages, resulting in lower performance overhead.
[0197] Based on the shared memory allocation method described in the above embodiments, and referring to Figure 10, this application embodiment provides an exemplary process for a first OS to allocate shared memory. As shown in Figure 10, this exemplary process includes the following steps:
[0198] S1001: In response to a shared memory request, check if there are addresses of one or more virtual memory spaces to be released in its own unlocked linked list. If yes, execute S1002; otherwise, execute S1003.
[0199] S1002: Release the one or more virtual memory spaces and their corresponding physical memory spaces.
[0200] S1003 is executed after S1002. The implementation methods of S1001 and S1002 can refer to the implementation method of releasing the physical memory space corresponding to the virtual address in the lockless linked list described above, and will not be repeated here.
[0201] S1003: Allocate the first virtual memory space from the virtual memory space managed by itself in the shared virtual memory.
[0202] The implementation method of this step can refer to the implementation method in S501 of the aforementioned embodiment.
[0203] S1004: Check if there is insufficient local free memory. If yes, execute S1005; otherwise, execute S1006.
[0204] If the size of the local free memory is less than the size of the first virtual memory space, then the local free memory is determined to be insufficient; if the size of the local free memory is not less than the size of the first virtual memory space, then the local free memory is determined to be sufficient.
[0205] S1005: Allocate available memory space from global free memory and add it to local free memory.
[0206] The implementation method of this step can refer to the implementation method described in S401 to S404 of the foregoing embodiments, and will not be repeated here.
[0207] In addition, after executing S1005, execute S1006.
[0208] S1006: Allocate a fourth physical memory space from local free memory.
[0209] S1007: Map the first virtual memory space to the fourth physical memory space.
[0210] The implementation method of this step can refer to the implementation method in S503 of the aforementioned embodiment.
[0211] S1008: Update information on local free memory.
[0212] The implementation method for this step can refer to the implementation method of updating the local free memory information based on the second physical memory space described in the previous embodiment.
[0213] Based on the shared memory release methods described in the above embodiments, and referring to Figure 11, this application embodiment also provides an exemplary process for a first OS to release shared memory. As shown in Figure 11, this exemplary process includes the following steps:
[0214] S1101: In response to a shared memory release request, check if there are addresses of one or more virtual memory spaces to be released in its own unlocked linked list. If yes, execute S1102; otherwise, execute S1103.
[0215] S1102: Release one or more virtual memory spaces and their corresponding physical memory spaces.
[0216] Specifically, S1103 is executed after S1102. The implementation methods of S1101 and S1102 can refer to the implementation method of releasing the physical memory space corresponding to the virtual address in the lockless linked list described above, and will not be repeated here.
[0217] S1103: Check whether the second virtual memory space requested to be released by the shared memory release request is a virtual memory space managed by itself. If yes, execute S1104; otherwise, execute S1107.
[0218] The implementation method of this step can refer to the implementation method in S801 of the aforementioned embodiment.
[0219] S1104: Release the second virtual memory space and the corresponding fifth physical memory space to local free memory.
[0220] The implementation method of this step can refer to the implementation method in S802 of the aforementioned embodiment.
[0221] S1105: Detect whether there is a contiguous free memory in the local free memory with a size not less than the first threshold. If so, execute S1106.
[0222] S1106: Release the contiguous free memory to global free memory.
[0223] S1107: Determine the second OS for managing the second virtual memory space.
[0224] The implementation method of this step can refer to the implementation method in S803 of the aforementioned embodiment.
[0225] S1108: Add the address of the second virtual memory space to the unlocked linked list of the second OS.
[0226] The implementation method of this step can refer to the implementation method in S804 of the aforementioned embodiment.
[0227] The memory management device provided in the embodiments of this application will be described next.
[0228] Figure 12 is a schematic diagram of a memory management device provided in an embodiment of this application. This memory management device can be deployed in each OS of the aforementioned computer device, for example, in the first OS. As shown in Figure 12, the memory management device 1200 includes a global physical memory management module 1201, which is used to execute S401 to S403 in the aforementioned embodiments.
[0229] Optionally, the memory management device 1200 may further include a local physical memory management module 1202. The local physical memory management module 1202 is used to update the information of local free memory based on the first physical memory space. Local free memory refers to the free memory space in the physical memory space allocated to the first OS. The updated information of local free memory includes the information of the first physical memory space.
[0230] Optionally, the local physical memory management module 1202 is further configured to: allocate a second physical memory space for the kernel of the first OS from local free memory in response to a first memory request from the kernel of the first OS.
[0231] Optionally, the local physical memory management module 1202 is also configured to: release the third physical memory space to local free memory in response to a first memory release request from the kernel of the first OS.
[0232] Optionally, the memory management device 1200 further includes a virtual memory management module 1203; the virtual memory management module 1203 is used to allocate a first virtual memory space for the kernel of the first OS from the virtual memory space managed by the first OS in the shared virtual memory in response to a second memory request from the kernel of the first OS, the shared virtual memory being virtual memory shared by multiple OSes, each OS being used to manage a portion of the virtual memory space in the shared virtual memory; the local physical memory management module 1202 is used to allocate a fourth physical memory space for the kernel of the first OS from local free memory; the virtual memory management module 1203 is also used to add an address mapping relationship between the first virtual memory space and the fourth physical memory space in the kernel page table of the first OS, the second-level page table corresponding to the shared virtual memory in the kernel page table of the first OS is a shared page table of multiple OSes, and the addresses of the portion of the virtual memory space in the shared virtual memory managed by multiple OSes do not overlap in the page table entries corresponding to the shared page table.
[0233] Optionally, the virtual memory management module 1203 is also used to respond to the second memory release request of the kernel of the first OS. If the second virtual memory space is a virtual memory space managed by the first OS in the shared virtual memory, the second virtual memory space is released, and the local physical memory management module 1202 is triggered to release the fifth physical memory space corresponding to the second virtual memory space to the local free memory.
[0234] Optionally, the virtual memory management module 1203 is further configured to, if the second virtual memory space is not managed by the first OS, determine the second OS that manages the second virtual memory space, and add the address of the second virtual memory space to the lockless linked list of the second OS. The lockless linked list is used to store the address of the virtual memory space corresponding to the physical memory space requested by the corresponding OS and to be released.
[0235] Optionally, the virtual memory management module 1203 is further configured to, if the lock-free linked list of the first OS is not empty, obtain the address of one or more virtual memory spaces in the lock-free linked list of the first OS, the lock-free linked list is used to store the address of the virtual memory space corresponding to the physical memory space requested by the corresponding OS and to be released, release one or more virtual memory spaces based on the address of one or more virtual memory spaces, and trigger the local physical memory management module 1202 to release the physical memory space corresponding to one or more virtual memory spaces to local free memory.
[0236] Optionally, the local physical memory management module 1202 is further configured to: if the local free memory includes a continuous free space with a size not less than a first threshold, release the continuous free space to the global free memory.
[0237] Optionally, the target physical memory also stores memory metadata shared by multiple operating systems. The memory metadata includes the status data of the target physical memory. The memory management device 1200 is also used to update the memory metadata based on the allocated or released physical memory space.
[0238] In this embodiment, the target physical memory of the computer device stores information about globally free memory shared by multiple operating systems (OSs). Based on this, any one of the OSs can obtain the information about globally free memory from the target physical memory and then use this information to allocate physical memory space for itself. Afterwards, it updates the information about globally free memory in the target physical memory so that other OSs can allocate physical memory based on this information. Therefore, in this application, each OS can allocate physical memory independently based on the shared information about globally free memory in the target physical memory, without needing to request memory through cross-OS communication messages, resulting in lower performance overhead. Furthermore, since each OS can allocate physical memory independently, decentralized memory management is achieved. This avoids the problem of high processing pressure on a central OS. Moreover, even if one OS crashes, it will not affect the operation of other OS kernels, improving system reliability.
[0239] It should be noted that the module division in the memory management device provided in the above embodiments is illustrative and only represents one logical functional division. In actual implementation, there may be other division methods. Furthermore, the functional modules in the various embodiments of this application can be integrated into a processor, exist as separate physical entities, or be integrated into a single module. The integrated modules described above can be implemented in hardware or as software functional modules.
[0240] If the integrated module is implemented as a software functional module and sold or used as an independent product, it can be stored in a computer-readable storage medium. Based on this understanding, the technical solutions of the embodiments of this application, essentially, or the parts that contribute to the prior art, or all or part of the technical solutions, can be embodied in the form of a software product. This computer software product is stored in a storage medium and includes several instructions to cause a computer device (which may be a terminal, server, router, wireless communication device, etc.) or processor to execute all or part of the steps of the methods in the various embodiments of this application. The aforementioned storage medium includes various media capable of storing program code, such as USB flash drives, portable hard drives, read-only memory (ROM), random access memory (RAM), magnetic disks, or optical disks.
[0241] Furthermore, the memory management device and memory management method embodiments provided in the above embodiments belong to the same concept, and their specific implementation process can be found in the method embodiments, which will not be repeated here.
[0242] In the above embodiments, implementation can be achieved, in whole or in part, through software, hardware, firmware, or any combination thereof. When implemented in software, it can be implemented, in whole or in part, as a computer program product. The computer program product includes one or more computer instructions. When the computer instructions are loaded and executed on a computer, all or part of the processes or functions described in the embodiments of this application are generated. The computer can be a general-purpose computer, a special-purpose computer, a computer network, or other programmable device. The computer instructions can be stored in a computer-readable storage medium or transmitted from one computer-readable storage medium to another. For example, the computer instructions can be transmitted from one website, computer, server, or data center to another via wired (e.g., coaxial cable, fiber optic, digital subscriber line (DSL)) or wireless (e.g., infrared, wireless, microwave, etc.) means. The computer-readable storage medium can be any available medium accessible to a computer or a data storage device such as a server or data center that integrates one or more available media. The available media can be magnetic media (e.g., floppy disks, hard disks, magnetic tapes), optical media (e.g., digital versatile discs (DVDs)), or semiconductor media (e.g., solid-state disks (SSDs)).
[0243] In the various embodiments of this application, unless otherwise specified or logically conflicting, the terminology and / or descriptions between different embodiments are consistent and can be referenced mutually. Technical features in different embodiments can be combined to form new embodiments based on their inherent logical relationships. In the embodiments of this application, "at least one" refers to one or more, and "more than one" refers to two or more. "And / or" describes the association relationship between related objects, indicating that three relationships can exist. For example, A and / or B can represent: A existing alone, A and B existing simultaneously, or B existing alone, where A and B can be singular or plural. In the textual description of the embodiments of this application, the character " / " generally indicates that the preceding and following related objects have an "or" relationship. In this application, "first," "second," and various numerical designations are only for ease of description and are not used to limit the scope of the embodiments of this application. For example, they are used to distinguish different messages, rather than to describe a specific order or sequence.
[0244] It is understood that the various numerical designations used in the embodiments of this application are merely for descriptive convenience and are not intended to limit the scope of the embodiments of this application. The order of the process numbers does not imply the order of execution; the execution order of each process should be determined by its function and internal logic.
[0245] Finally, it should be noted that the above description is merely a specific embodiment of this application, but the scope of protection of this application is not limited thereto. Any changes or substitutions within the technical scope disclosed in this application should be included within the scope of protection of this application. Therefore, the scope of protection of this application should be determined by the scope of the claims.
Claims
A memory management method, characterized by, A first operating system (OS) is applied to a computer device, the computer device including multiple OSes and target physical memory, wherein the first OS is any one of the multiple OSes, the method comprising: Obtain information about the global free memory shared by the multiple OSes from the target physical memory, wherein the global free memory refers to the unallocated memory space in the target physical memory; Based on the information of the global free memory, allocate a first physical memory space for the first OS; Based on the first physical memory space, update the information of the global free memory. The method of claim 1, wherein The method further includes: Based on the first physical memory space, update the information of local free memory. The local free memory refers to the free memory space in the physical memory space allocated to the first OS. The updated information of local free memory includes the information of the first physical memory space. The method according to claim 2, characterized in that The method further includes: In response to a first memory request from the kernel of the first OS, a second physical memory space is allocated for the kernel of the first OS from the local free memory. The method according to claim 2 or 3, characterized in that The method further includes: In response to a first memory release request from the kernel of the first OS, the third physical memory space is released to the local free memory. The method according to claim 2, characterized in that The method further includes: In response to a second memory request from the kernel of the first OS, a first virtual memory space is allocated to the kernel of the first OS from the virtual memory space managed by the first OS in the shared virtual memory. The shared virtual memory is virtual memory shared by the multiple OSes, and each OS is used to manage a portion of the virtual memory space in the shared virtual memory. Allocate a fourth physical memory space for the kernel of the first OS from the local free memory; An address mapping relationship between the first virtual memory space and the fourth physical memory space is added to the kernel page table of the first OS. The second-level page table corresponding to the shared virtual memory in the kernel page table of the first OS is a shared page table of the multiple OSes. The addresses of some virtual memory spaces in the shared virtual memory managed by the multiple OSs do not overlap with the page table entries corresponding to them in the shared page table. The method according to claim 5, characterized in that The method further includes: In response to a second memory release request from the kernel of the first OS, if the second virtual memory space is a virtual memory space managed by the first OS in shared virtual memory, then the second virtual memory space is released, and the fifth physical memory space corresponding to the second virtual memory space is released to the local free memory. The second memory release request includes the address of the second virtual memory space. The method according to claim 6, characterized in that The method further includes: If the second virtual memory space is not a virtual memory space managed by the first OS, then a second OS for managing the second virtual memory space is determined. The address of the second virtual memory space is added to the lockless linked list of the second OS. The lockless linked list is used to store the address of the virtual memory space corresponding to the physical memory space requested by the OS and to be released. The method according to any one of claims 5 to 7, characterized in that The method further includes: If the first OS's unlocked linked list is not empty, then obtain the address of one or more virtual memory spaces in the first OS's unlocked linked list. The unlocked linked list is used to store the address of the virtual memory space corresponding to the physical memory space requested by the corresponding OS and to be released. Based on the addresses of the one or more virtual memory spaces, the physical memory space corresponding to the one or more virtual memory spaces is released to the local free memory. The method according to any one of claims 2 to 8, characterized in that The method further includes: If the local free memory includes a contiguous free space with a size not less than a first threshold, then the contiguous free space is released to the global free memory. The method according to any one of claims 1 to 9, characterized in that The target physical memory also stores memory metadata shared by the multiple operating systems, the memory metadata including the state data of the target physical memory, and the method further includes: Update the memory metadata based on the allocated or freed physical memory space. A memory management device, characterized by A first operating system (OS) for use in a computer device, the computer device including multiple OSes and target physical memory, wherein the first OS is any one of the multiple OSes, and the memory management device includes: The global physical memory management module is used to obtain information about the global free memory shared by the multiple OSes from the target physical memory, wherein the global free memory refers to the unallocated memory space in the target physical memory; based on the information about the global free memory, allocate a first physical memory space for the first OS; and update the information about the global free memory based on the first physical memory space. The memory management device according to claim 11, wherein The memory management device also includes a local physical memory management module; The local physical memory management module is used to update the information of local free memory based on the first physical memory space. The local free memory refers to the free memory space in the physical memory space allocated to the first OS. The updated information of local free memory includes the information of the first physical memory space. The memory management device according to claim 12, wherein The local physical memory management module is also used for: In response to a first memory request from the kernel of the first OS, a second physical memory space is allocated for the kernel of the first OS from the local free memory. The memory management device according to claim 12 or 13, characterized in that The local physical memory management module is also used for: In response to a first memory release request from the kernel of the first OS, the third physical memory space is released to the local free memory. The memory management device according to claim 12, wherein The memory management device further includes a virtual memory management module; The virtual memory management module is used to respond to a second memory request from the kernel of the first OS, and allocate a first virtual memory space for the kernel of the first OS from the virtual memory space managed by the first OS in the shared virtual memory. The shared virtual memory is virtual memory shared by the multiple OSs, and each OS is used to manage a portion of the virtual memory space in the shared virtual memory. The local physical memory management module is used to allocate a fourth physical memory space for the kernel of the first OS from the local free memory; The virtual memory management module is further configured to add an address mapping relationship between the first virtual memory space and the fourth physical memory space to the kernel page table of the first OS. The second-level page table corresponding to the shared virtual memory in the kernel page table of the first OS is a shared page table of the multiple OSes. The addresses of some virtual memory spaces in the shared virtual memory managed by the multiple OSs do not overlap with the page table entries corresponding to the shared page table. The memory management device according to claim 15, characterized in that, The virtual memory management module is further configured to respond to a second memory release request from the kernel of the first OS. If the second virtual memory space is a virtual memory space managed by the first OS in shared virtual memory, the module releases the second virtual memory space and triggers the local physical memory management module to release the third physical memory space corresponding to the second virtual memory space to the local free memory. The memory management device according to claim 16, characterized in that, The virtual memory management module is further configured to, if the second virtual memory space is not a virtual memory space managed by the first OS, determine a second OS for managing the second virtual memory space, and add the address of the second virtual memory space to the lockless linked list of the second OS. The lockless linked list is used to store the address of the virtual memory space corresponding to the physical memory space requested and to be released by the corresponding OS. The memory management device according to any one of claims 15 to 17 is characterized in that, The virtual memory management module is further configured to, if the first OS's unlocked linked list is not empty, obtain the address of one or more virtual memory spaces in the first OS's unlocked linked list, wherein the unlocked linked list is used to store the address of the virtual memory space corresponding to the physical memory space requested by the corresponding OS and to be released; based on the address of the one or more virtual memory spaces, release the one or more virtual memory spaces, and trigger the local physical memory management module to release the physical memory space corresponding to the one or more virtual memory spaces to the local free memory. The memory management apparatus according to any one of claims 12 to 18, wherein The local physical memory management module is also used for: If the local free memory includes a contiguous free space with a size not less than a first threshold, then the contiguous free space is released to the global free memory. The memory management apparatus according to any one of claims 11 to 19, wherein The target physical memory also stores memory metadata shared by the multiple operating systems, including the status data of the target physical memory. The memory management device is further used for: Update the memory metadata based on the allocated or freed physical memory space. A computer device, characterized in that The computer device includes a central processing unit (CPU) and target physical memory. The CPU runs multiple operating systems (OS), each of which executes the memory management method according to any one of claims 1 to 10 to manage the target physical memory. A computer-readable storage medium, characterized by, The computer-readable storage medium stores instructions that, when executed on a computer device, cause the computer device to perform the memory management method according to any one of claims 1 to 10. A computer program product comprising instructions, characterized in that When the instructions are executed by the computer device, the computer device is caused to perform the memory management method as claimed in any one of claims 1 to 10.
