Memory processing method, electronic device and readable storage medium
In the scenario where application processes and hardware devices share physical memory, allowing applications to actively request and recycle unwanted physical memory, solving the problem of physical memory being permanently occupied and improving the available memory and data security of electronic devices.
Patent Information
- Application Number
- PCT/CN2024/105776
- Authority / Receiving Office
- WO · WO
- Patent Type
- Applications
- Current Assignee / Owner
- Priority Date
- 2023-09-07
- Filing Date
- 2024-07-16
- Publication Date
- 2025-03-06
AI Technical Summary
In the scenario where application processes and hardware devices share physical memory, if the application processes do not release physical memory, the physical memory will be permanently occupied and cannot be recycled, resulting in a decrease in available memory for electronic devices.
Provides a memory processing method, allowing an application to actively request the recycling of physical memory when switching to the background, send commands to free unwanted physical memory through the memory processing device, and redistribute the shared physical memory when needed.
It effectively increases the available memory of electronic devices, improves memory usage, avoids the problem that the physical memory management module can still be accessed after freeing memory, and improves data security.
Smart Images

Figure CN2024105776_06032025_PF_FP_ABST
Abstract
Description
Memory processing method, electronic device and readable storage medium
[0001] This application claims priority to the Chinese patent application filed with the China Patent Office on August 29, 2023, with application number 202311103138.X and application name “Memory processing method, electronic device and readable storage medium”, and claims priority to the Chinese patent application filed with the China Patent Office on September 7, 2023, with application number 202311160306.9 and application name “Memory processing method, electronic device and readable storage medium”, the entire contents of which are incorporated by reference into this application. Technical Field
[0002] The embodiments of the present application relate to the field of computer technology, and in particular to a memory processing method, an electronic device, and a readable storage medium. Background Art
[0003] Virtual memory is a memory management technology that allows applications and hardware devices in electronic devices to believe they have continuous available memory, even though in reality, this memory may be scattered across physical memory. Virtual memory technology enables efficient memory utilization and isolation of application processes.
[0004] Currently, in scenarios where application processes and hardware devices share physical memory, if the application process does not release the physical memory, the physical memory will be permanently occupied by the application process. Even when the electronic device is running low on memory, the physical memory cannot be reclaimed. As this situation increases, the available memory of electronic devices decreases.
[0005] Summary of the Invention
[0006] The embodiments of the present application provide a memory processing method, an electronic device, and a readable storage medium. In a scenario where an application process and a hardware device share physical memory, the application can actively request to reclaim the physical memory segment to increase the available memory of the electronic device.
[0007] In a first aspect, an embodiment of the present application provides a memory processing method, applied to a memory processing device. In this method, an application and a hardware device can share a first physical memory. When the application no longer needs to use the first physical memory, for example, when the application is switched to the background, the application can send a first command to the memory processing device. In response, the memory processing device can receive the first command from the application, wherein the first command is used to instruct the release of the first physical memory of the application. In response to the first command, the memory processing device can release the first physical memory.
[0008] In an embodiment of the present application, when the application is switched to the background and the first physical memory is temporarily unavailable, the application can actively request to release the first physical memory to increase the available memory of the electronic device.
[0009] In one possible implementation, the first page table includes a mapping relationship between a first physical address and a first virtual address, and the second page table includes a mapping relationship between the first physical address and the second virtual address, the first physical address is the physical address of the first physical memory, the first virtual address is the virtual address in the virtual address space of the application, and the second virtual address is the virtual address in the virtual address space of the hardware device.
[0010] Before releasing the first physical memory, the method further includes: modifying the first physical address mapped by the first virtual address in the first page table; and modifying the first physical address mapped by the second virtual address in the second page table.
[0011] In one possible implementation, the modification of the first physical address mapped by the first virtual address in the first page table includes: modifying the first physical address to a preset physical address; the modification of the first physical address mapped by the second virtual address in the second page table includes: modifying the first physical address to the preset physical address.
[0012] In this implementation, when the application switches to the background, the memory recovery driver module can release the mapping relationship between the first physical address and the first virtual address, as well as the mapping relationship between the first physical address and the second virtual address. This can avoid the problem that after the physical memory management module releases the first physical memory, the application and hardware devices switched to the background can still access the first physical memory, thereby improving data security.
[0013] In one possible implementation, the business driver in the kernel space can also access the first physical memory, wherein the third page table includes a mapping relationship between the first physical address and the third virtual address, and the third virtual address is the virtual address of the business driver in the kernel space. Before releasing the first physical memory, the memory processing device can also modify the first physical address mapped by the third virtual address in the third page table.
[0014] In this implementation, when the application switches to the background, the memory recovery driver module can release the mapping relationship between the first physical address and the third virtual address, thereby avoiding the problem that the business driver in the kernel space can still access the first physical memory after the physical memory management module releases the first physical memory, thereby improving data security.
[0015] In one possible implementation, when an application starts to switch from the background to the foreground, it needs to continue to use the shared physical memory with the hardware device to continue executing the business. When the application starts to switch from the background to the foreground, it can send a fourth command to the memory processing device, and the fourth command is used to instruct to reallocate the same physical memory to the application and the hardware device. Accordingly, the memory processing device can receive the fourth command from the application, and in response to the fourth command, the memory processing device can allocate the same second physical memory to the application and the hardware device, and the physical address of the second physical memory is the second physical address. The size of the second physical memory is the same as the size of the first physical memory.
[0016] The memory processing device may modify the physical address mapped by the first virtual address in the first page table to the second physical address, and modify the physical address mapped by the second virtual address in the second page table to the second physical address.
[0017] In a possible implementation, the memory processing device may modify the physical address mapped by the third virtual address in the third page table to the second physical address.
[0018] In this implementation, when the application starts to switch from the background to the foreground, it can request the memory processing device to re-allocate the shared second physical memory for the application and the hardware device. The memory processing device can allocate a shared second physical memory for the application and the hardware device, and the memory processing device can modify the first page table to establish a mapping relationship between the first virtual address and the second physical address, modify the third page table to establish a mapping relationship between the third virtual address and the second physical address, and modify the second page table to establish a mapping relationship between the second virtual address and the second physical address. In this way, the application, the hardware device, and the kernel space can continue to access the data associated with the target business in the second physical memory, and the application can continue to execute the target business. The memory processing method provided in the embodiment of the present application does not affect the execution of the business by the application.
[0019] In a possible implementation, the first command includes: an identifier of the first physical memory. The identifier of the first physical memory is used to indicate the first physical memory, so that the memory processing device can determine the first physical memory based on the identifier of the first physical memory.
[0020] In one possible implementation, the identifier of the first physical memory is an identifier of a buffer used to record information about the first physical memory. After allocating the shared first physical memory for the application and the hardware device, the memory processing device may further store, in the buffer, a first virtual address of the application mapped to the first physical memory, a second virtual address of the hardware device, and a third virtual address of the kernel space.
[0021] In one possible implementation, the memory processing device includes: a memory recycling driver module and a physical memory management module. The following describes the memory processing method provided by the embodiment of the present application based on the interaction of more detailed modules:
[0022] The memory processing device receiving the first command from the application includes: the memory recycling driving module receiving the first command from the application.
[0023] The memory processing device releases the first physical memory, comprising: the memory recycling driver module sending a second command to the physical memory management module, the second command being used to instruct the release of the first physical memory. The physical memory management module may release the first physical memory in response to the second command.
[0024] In a possible implementation, the memory processing device further includes: an input-output memory management unit (IOMMU) driver, and the buffer is further configured to record a first virtual address and a second virtual address mapped to the first physical memory.
[0025] In response to the first command, the memory recovery driver module can query the buffer corresponding to the identifier according to the buffer zone, obtain the first virtual address and the second virtual address mapped to the first physical memory, and modify the first physical address mapped to the first virtual address in the first page table.
[0026] The memory reclaim driver module may send a third command to the IOMMU driver, wherein the third command is used to instruct the modification of the first physical address of the second virtual address mapping in the second page table. In response to the third command, the IOMMU driver may modify the first physical address of the second virtual address mapping in the second page table.
[0027] In one possible implementation, the memory recovery driver module, in response to the first command, can query the buffer corresponding to the identifier according to the buffer identifier, obtain the third virtual address mapped to the first physical memory, and modify the first physical address mapped to the third virtual address in the third page table.
[0028] In an embodiment of the present application, based on the interaction between the memory recovery driver module, the physical memory management module, and the IOMMU driver in the memory processing module, the purpose of recovering the first physical memory shared by the application and the hardware device can be achieved, thereby increasing the available physical memory of the electronic device.
[0029] In one possible implementation, the memory processing device receives a fourth command from the application, including: the memory reclamation driver module receives the fourth command from the application. In response to the fourth command, the memory reclamation driver module may allocate the same second physical memory to the application and the hardware device, including: the memory reclamation driver module sends a fifth command to the physical memory management module, the fifth command being used to instruct the application and the hardware device to allocate the same physical memory.
[0030] In response to the fifth command, the physical memory management module allocates the same second physical memory to the application and the hardware device, and the physical memory management module sends a second physical address of the second physical memory to the memory recycling driver module.
[0031] After receiving the second physical address of the second physical memory from the physical memory management module, the memory reclamation driver module modifies the physical address mapped to the first virtual address in the first page table to the second physical address. In addition, the memory reclamation driver module modifies the physical address mapped to the third virtual address in the third page table to the second physical address.
[0032] In addition, the memory reclaim driver module sends a sixth command to the IOMMU driver, the sixth command being used to instruct the physical address mapped to the second virtual address in the second page table to be modified to the second physical address. In response to the sixth command, the IOMMU driver modifies the physical address mapped to the second virtual address in the second page table to the second physical address.
[0033] In a possible implementation, the memory processing device is located in the kernel space of the electronic device.
[0034] In an embodiment of the present application, based on the interaction between the memory recovery driver module, the physical memory management module, and the IOMMU driver in the memory processing module, when the application switches to the foreground, the shared second physical memory can be reallocated for the application and the hardware device, thereby ensuring that the application executes its business smoothly.
[0035] In a second aspect, an embodiment of the present application provides a memory processing method, wherein the electronic device includes an application and a memory processing device, and the method includes: when the application switches to the background, the application sends a first command to the memory processing device, and the first command is used to instruct the release of the first physical memory of the application, and the application and the hardware device share the first physical memory; in response to the first command, the memory processing device releases the first physical memory.
[0036] In one possible implementation, the first page table includes a mapping relationship between a first physical address and a first virtual address, and the second page table includes a mapping relationship between the first physical address and the second virtual address, the first physical address is the physical address of the first physical memory, the first virtual address is the virtual address in the virtual address space of the application, and the second virtual address is the virtual address in the virtual address space of the hardware device.
[0037] Before the memory processing device releases the first physical memory, the memory processing device further includes: modifying the first physical address mapped by the first virtual address in the first page table, and modifying the first physical address mapped by the second virtual address in the second page table.
[0038] In one possible implementation, the method further includes: when the application starts to switch from the background to the foreground, the application sends a fourth command to the memory processing device, and the fourth command is used to instruct the reallocation of shared physical memory for the application and the hardware device; the memory processing device allocates a shared second physical memory for the application and the hardware device, and the physical address of the second physical memory is a second physical address; the memory processing device modifies the physical address mapped by the first virtual address in the first page table to the second physical address, and modifies the physical address mapped by the second virtual address in the second page table to the second physical address.
[0039] In one possible implementation, the third page table includes a mapping relationship between the first physical address and the third virtual address, the third virtual address is a business-driven virtual address in the kernel space, and before the memory processing device releases the first physical memory, it also includes: the memory processing device modifies the first physical address mapped by the third virtual address in the third page table.
[0040] After the memory processing device allocates a shared second physical memory for the application and the hardware device, the method further includes: the memory processing device modifies the physical address mapped by the third virtual address in the third page table to the second physical address.
[0041] In a possible implementation, the first command includes: an identifier of the first physical memory.
[0042] In a possible implementation, the identifier of the first physical memory is an identifier of a buffer zone used to record information of the first physical memory.
[0043] In a possible implementation, the buffer is further used to record a first virtual address and a second virtual address mapped to the first physical memory.
[0044] The memory processing device modifies the first physical address mapped by the first virtual address in the first page table, and modifies the first physical address mapped by the second virtual address in the second page table, including: the memory processing device obtains the first virtual address and the second virtual address mapped to the first physical memory according to the identifier of the buffer; the memory processing device modifies the first physical address mapped by the first virtual address in the first page table, and modifies the first physical address mapped by the second virtual address in the second page table.
[0045] The memory processing device modifies the first physical address mapped by the third virtual address in the third page table, including: the memory processing device obtains the third virtual address mapped to the first physical memory according to the identifier of the buffer zone; the memory processing device modifies the first physical address mapped by the third virtual address in the third page table.
[0046] In one possible implementation, the memory processing device may include a memory reclamation driver module, a physical memory management module, and an IOMMU driver to implement the steps of the memory processing device. The steps performed by the memory reclamation driver module, the physical memory management module, and the IOMMU driver may refer to the description of the first aspect.
[0047] In a possible implementation, the application is located in the user space of the electronic device, and the memory processing device is located in the kernel space of the electronic device.
[0048] In a third aspect, an embodiment of the present application provides a memory processing device, comprising a module for executing the method in the first aspect.
[0049] In a fourth aspect, an embodiment of the present application provides an electronic device, including an application and a memory processing device as in the second aspect.
[0050] In a fifth aspect, an embodiment of the present application provides an electronic device, which may include: a processor and a memory. The memory is used to store computer-executable program code, the program code including instructions; when the processor executes the instructions, the instructions cause the electronic device to perform the method described in the first aspect.
[0051] In a sixth aspect, an embodiment of the present application provides a computer program product comprising instructions, which, when executed on a computer, enables the computer to execute the method in the first aspect above.
[0052] In a seventh aspect, an embodiment of the present application provides a computer-readable storage medium, wherein the computer-readable storage medium stores instructions, which, when executed on a computer, enables the computer to execute the method in the above-mentioned first aspect.
[0053] In an eighth aspect, an embodiment of the present application provides a communication device (chip), comprising a processor and a communication interface, wherein the processor utilizes the communication interface to execute the method in the first aspect.
[0054] The beneficial effects of the possible implementation methods of the second to eighth aspects mentioned above can be referred to the beneficial effects brought about by the first aspect mentioned above, and will not be elaborated here. BRIEF DESCRIPTION OF THE DRAWINGS
[0055] FIG1 is a schematic structural diagram of an electronic device provided in an embodiment of the present application;
[0056] FIG2 is a schematic diagram of a mapping relationship between virtual addresses and physical addresses;
[0057] FIG3A is another schematic diagram of the structure of an electronic device provided in an embodiment of the present application;
[0058] FIG3B is a schematic diagram of an ION driver according to an embodiment of the present application allocating shared physical memory to an application and video decoding hardware;
[0059] FIG3C is a schematic diagram of memory mapping of the shared physical memory by an application, a codec service, a codec driver, and an IOMMU driver provided in an embodiment of the present application;
[0060] FIG3D is a schematic diagram of an application and video decoding hardware accessing a shared physical memory according to an embodiment of the present application;
[0061] FIG4A is a flow chart of an embodiment of a memory processing method provided in an embodiment of the present application;
[0062] FIG4B is a flow chart of another embodiment of a memory processing method provided in an embodiment of the present application;
[0063] FIG5 is a flow chart of another embodiment of a memory processing method provided in an embodiment of the present application;
[0064] FIG6 is a flow chart of another embodiment of the memory processing method provided in an embodiment of the present application;
[0065] FIG7 is a flow chart of another embodiment of a memory processing method provided in an embodiment of the present application;
[0066] FIG8 is another schematic diagram of the structure of an electronic device provided in an embodiment of the present application. DETAILED DESCRIPTION
[0067] For ease of understanding, the following first introduces the relevant terms and concepts involved in the embodiments of the present application:
[0068] The electronic devices in the embodiments of the present application may be referred to as user equipment (UE), terminals, etc. For example, the electronic devices may be mobile phones, portable Android devices (PADs), personal digital assistants (PDAs), handheld devices with wireless communication capabilities, computing devices, vehicle-mounted devices, or wearable devices, virtual reality (VR) terminal devices, augmented reality (AR) terminal devices, wireless terminals in industrial control, wireless terminals in smart homes, etc. The embodiments of the present application do not specifically limit the form of the electronic devices.
[0069] Application process: When an electronic device runs an application, it can create an application process. The application can run in this process, which can be called an application process. An application can be understood as a program in an electronic device that completes one or more specific tasks. In some embodiments, an application can include application software (APP) programs, system software programs, and system service programs, etc., which are not limited in this embodiment of the present application. In some embodiments, an application process can be simply referred to as a process.
[0070] Physical memory: refers to the memory that actually exists in an electronic device. For example, physical memory may include random access memory (RAM).
[0071] Physical address: A physical memory may include at least one storage unit, and a physical address may include the address of each storage unit.
[0072] In early memory allocation mechanisms, when an electronic device ran an application, the operating system (OS) could write the application's executable file and related data to physical memory. The application ran directly in physical memory, meaning that the memory addresses accessed by the application were actual physical addresses. When a computer runs multiple applications simultaneously, it must ensure that the total amount of physical memory used by these applications is less than the total physical memory size of the electronic device. Suppose the physical memory size of an electronic device is 128MB. Now, two applications A and B are running simultaneously. A requires 10MB of memory, and B requires 110MB of memory. When allocating physical memory to the applications, the electronic device will adopt the following method: first, allocate the first 10MB of physical memory to A, and then allocate 110MB of the remaining 118MB to B. This allocation method ensures that both A and B can run, but this simple memory allocation strategy has the following problems:
[0073] 1. Application processes are not isolated. Because applications directly access physical memory, they can freely access the data of other applications in physical memory, posing a data security risk. For example, malicious programs can arbitrarily modify the data of other applications in physical memory, achieving destructive purposes.
[0074] 2. Inefficient use of physical memory. For example, if application C is running while both application A and application B are running, and application C requires 20MB of physical memory, but only 8MB is available, the device must select one of the running applications, temporarily copy the application's data to the hard disk, freeing up some space for application C, and then write C's data back into memory for execution. This process involves copying and writing a large amount of data, resulting in low efficiency.
[0075] To solve the above problems, virtual memory technology came into being. Simply put, it is to create an intermediate layer such as virtual memory in electronic devices, so that applications can access physical memory using an indirect address access method. According to this method, the memory address accessed by the application is no longer the actual physical memory address, but a virtual address. Among them, when the electronic device runs an application, the operating system can allocate an independent virtual address space to the process of each application. Because the virtual address spaces of different processes are independent of each other and do not overlap with each other, processes cannot access the virtual address space of other processes. Therefore, the isolation of processes of different applications and the data security of different applications can be guaranteed. Among them, the virtual address space of a process can be regarded as the virtual space allocated by the operating system to the process.
[0076] The size of the virtual address space depends on the hardware architecture and operating system of the electronic device. For example, in a 32-bit operating system, the addressing range of the pointer is 2 32 For example, the size of the virtual address space allocated by the operating system to each process may be 3GB, and the virtual memory address range of the process may be: 0x0000 0000-0xFFFF FFFF. For example, in a 64-bit operating system, the size of the virtual address space of a process may reach TB or more. In some embodiments, the virtual memory address can be understood as the address of the virtual memory and can be simply referred to as a virtual address.
[0077] However, for electronic devices to execute applications, they must run in real physical memory. Therefore, the operating system allocates a separate virtual address space for each application process and establishes a mapping between virtual addresses in the virtual address space and physical addresses. This mapping mechanism allows an application to access a virtual address in the virtual address space by mapping it to a physical address, enabling access to the physical address.
[0078] To improve physical memory utilization, paging is currently used to manage physical memory. The basic principle of paging is that virtual memory is divided into fixed-size units called pages. The size of a page depends on the operating system; for example, a page can be 4KB. Physical memory is also divided into units of the same size, called page frames. A page in virtual memory corresponds to a page frame in physical memory.
[0079] The following describes the paging mechanism by introducing the process of application data storage:
[0080] After an application is started, the operating system first creates a virtual address space for the application. During operation, the application can request physical memory from the operating system, which is used to store data generated by the application during operation. In response to the application's request, the operating system can allocate physical memory to the application. The operating system can allocate free page frames to the application and map the free page frames to pages, establishing a mapping relationship between physical addresses and virtual addresses. The operating system can then add this mapping relationship between virtual addresses and physical addresses to the page table.
[0081] A page table stores the mapping between virtual addresses and physical addresses. Each application has a corresponding page table, which stores the mapping between virtual addresses and physical addresses assigned by the operating system to the application's process. The operating system is responsible for managing and maintaining the page table.
[0082] When an application needs to access data stored in physical memory, the application can access the page table to convert the virtual address into a physical address. Based on the physical address, the application can directly access the data in the physical memory.
[0083] Figure 1 is a schematic diagram of the structure of an electronic device provided in an embodiment of the present application. Referring to Figure 1, the electronic device may include: an application, hardware devices, a central processing unit (CPU), a direct memory access (DMA) module, a memory management unit (MMU), an input output memory management unit (IOMMU), and physical memory.
[0084] Referring to the above description, when an application is running, the operating system (not shown in FIG1 ) in the electronic device can allocate physical memory for the application's process, establish a mapping relationship between the physical address and the virtual address of the physical memory, and add the mapping relationship between the virtual address and the physical address to the page table. In some embodiments, the page table used to store the mapping relationship between the virtual address and the physical address of the process can be referred to as a process page table. In some embodiments, the process page table can also be referred to as a first page table.
[0085] The process page table is stored in physical memory. When an application accesses a virtual address, the CPU can query the process page table to find the physical address mapped to the virtual address and then access the physical address. In some embodiments, the CPU querying the physical address mapped to the virtual address can be understood as "converting the virtual address to a physical address."
[0086] In some embodiments, the MMU can be used to query the process page table and perform the process's virtual address to physical address conversion. When an application accesses a virtual address, the MMU can query the process page table and convert the virtual address to a physical address, enabling the application to access the physical address. In some embodiments, the MMU can be integrated into the CPU or configured independently of the CPU. Figure 1 illustrates an MMU configured independently of the CPU.
[0087] In some embodiments, the MMU may include a translation lookaside buffer (TLB), which stores the mappings between recently used virtual addresses and physical addresses. When the MMU performs a virtual-to-physical address translation, it first checks the TLB for a corresponding mapping. If not, it then queries the process page table in physical memory. This prevents the MMU from frequently accessing the process page table in physical memory, improving address translation efficiency.
[0088] In some embodiments, a virtual page number (VPN) is used to identify a page in a virtual address space, and a physical frame number (PFN) is used to identify a page frame in physical memory. Referring to FIG2 , the virtual address assigned by the operating system to an application may include the VPN and a virtual address offset (VA offset), while the physical address assigned by the operating system to an application may include the PFN and a physical address offset (PA offset).
[0089] In some embodiments, the mapping relationship between a process's virtual address and physical address may include a mapping relationship between a VPN and a PFN. Accordingly, the mapping relationship between the VPN and the PFN may be stored in the process page table. In this example, when performing the virtual-to-physical address conversion step, the MMU can use the VPN in the virtual address to search the process page table for the PFN mapped to the VPN. The MMU then adds the PFN and the VA offset to obtain the physical address, thereby achieving virtual-to-physical address conversion.
[0090] Hardware devices can be understood as other hardware devices in electronic devices besides the CPU and physical memory. Hardware devices may include but are not limited to: video decoding hardware, audio players, cameras, communication chips, network cards, etc.
[0091] Similar to applications, each hardware device can have its own independent virtual address space. For example, the operating system can allocate a virtual address space for the hardware device, and the IOMMU establishes a mapping relationship between the virtual address and the physical address of the hardware device, and the IOMMU adds the mapping relationship between the virtual address and the physical address of the hardware device to the page table. In some embodiments, the page table used to store the mapping relationship between the virtual address and the physical address of the hardware device can be referred to as an IOMMU page table or a hardware device page table. In some embodiments, the IOMMU page table can also be referred to as a second page table.
[0092] The mapping relationship between the virtual address and the physical address stored in the IOMMU page table may refer to the relevant description of the process page table.
[0093] In some embodiments, when a hardware device accesses a virtual address, the DMA module may query the IOMMU page table and convert the virtual address into a physical address, thereby enabling the hardware device to access the physical address.
[0094] In some embodiments, the IOMMU in the electronic device can be used to query the IOMMU page table and perform the steps of converting the virtual address of the hardware device to the physical address. Specifically, when the hardware device accesses a certain virtual address, the IOMMU can query the IOMMU page table and convert the virtual address to a physical address, enabling the hardware device to access the physical address.
[0095] In some embodiments, in order to reduce content copying and improve service efficiency, applications and hardware devices in the electronic device can share at least one physical memory segment.
[0096] For example, using the video decoding scenario as an example, the operating system can allocate the same physical memory 1 to the application and the video decoding hardware. That is, the application and the video decoding hardware can share physical memory 1. The operating system can also establish a mapping relationship between the virtual address 1 of the application process and the physical address 1 of physical memory 1, and the IOMMU can establish a mapping relationship between the virtual address 2 of the video decoding hardware and the physical address 1. The mapping relationship between the virtual address 1 of the application process and the physical address 1 can be stored in the first page table, and the mapping relationship between the virtual address 2 and the physical address 1 of the video decoding hardware can be stored in the second page table.
[0097] In some embodiments, the physical memory 1 can store video data to be decoded and decoded data. Exemplarily, during the video decoding process, when the application accesses the physical memory 1, the MMU can convert the virtual address 1 into the physical address 1, and the application can write the video data to be decoded into the physical memory 1. When the video decoding hardware accesses the physical memory 1, the IOMMU can convert the virtual address 2 into the physical address 1, and the video decoding hardware can decode the video data to be decoded in the physical memory 1, and write the decoded data into the physical memory 1. Similarly, the application can access the physical memory 1 again, the MMU can convert the virtual address 1 into the physical address 1, the application can read the decoded data in the physical memory 1, and play the video according to the decoded data.
[0098] Applications and hardware devices share physical memory, which can reduce content copying and improve business efficiency. For example, if the application and video decoding hardware do not share physical memory 1, such as the operating system allocates physical memory 1 to the application and allocates physical memory 2 to the video decoding hardware, then during the video decoding process, the video data to be decoded in physical memory 1 needs to be copied to physical memory 2, so that the video decoding hardware can read the video data to be decoded in physical memory 2. After the video decoding hardware decodes the video data to be decoded and obtains the decoded data, it is also necessary to copy the decoded data to physical memory 1, so that the application can read the decoded data in physical memory 1 to play the video. In the scenario where the application and hardware devices do not share physical memory, the number of content copies is large, and business efficiency will be reduced. The memory processing method provided in the embodiment of the present application is suitable for scenarios where applications and hardware devices share physical memory.
[0099] It should be understood that the scenario in which an application and a hardware device share physical memory is not limited to a video decoding scenario, but can also be applicable to the following scenario: in an application and a hardware device, one writes data to a shared physical memory, and the other reads data from the shared physical memory. Alternatively, both the application and the hardware device read or write data to the shared physical memory. It should be understood that the embodiments of the present application do not limit the number of times an application and a hardware device write data to the shared physical memory, or the number of times they read data.
[0100] Currently, in scenarios where applications and hardware devices share physical memory, if the application does not proactively release the physical memory, it will be permanently occupied by the application. Even when the device's memory is tight, this physical memory cannot be reclaimed. As this situation increases, the available memory of electronic devices decreases.
[0101] For example, in a scenario where an application and video decoding hardware share physical memory, for example, the application can use the media player service of the electronic device's system layer to play videos. When the application switches to the background, the current media player service will not be destructed, so the application will not actively release the shared physical memory, which will make it difficult to reclaim this segment of physical memory.
[0102] In order to solve the above problems, the embodiment of the present application provides a memory processing method. In a scenario where an application and a hardware device share physical memory, when the application switches to the background and temporarily does not use the physical memory, the physical memory shared by the application and the hardware device can be released, increasing the available memory of the electronic device. The released physical memory can be allocated to other services, thereby improving memory utilization. When the application switches to the foreground and needs to use physical memory, the shared physical memory can be reallocated to the application and the hardware device, and the first page table and the second page table can be modified to ensure the smooth execution of the business.
[0103] Before introducing the memory processing method provided in the embodiment of the present application, the structure of the electronic device is first introduced:
[0104] FIG3A is another structural diagram of an electronic device provided in an embodiment of the present application. Referring to FIG3A , the electronic device may include: an application, a memory recovery driver module, an IOMMU driver, and a physical memory management module. It should be understood that the memory processing method provided in the embodiment of the present application is applicable to scenarios where applications and hardware devices share physical memory. Because the memory processing method provided in the embodiment of the present application does not involve actions performed by hardware devices, the hardware devices are not shown in FIG3A .
[0105] In some embodiments, the structure shown in FIG3A does not limit the structure of the electronic device, and the electronic device may further include more or fewer modules. In some embodiments, the memory recovery driver module, IOMMU driver, and physical memory management module shown in FIG3A can be integrated into one, for example, the memory processing device may include a memory recovery driver module, an IOMMU driver, and a physical memory management module, and the memory processing device may execute the actions of the memory recovery driver module, the IOMMU driver, and the physical memory management module in the following embodiments. In some embodiments, the memory recovery driver module and the IOMMU driver shown in FIG3A can be integrated into one, and the integrated module can be used to execute the actions of the memory recovery driver module and the IOMMU driver in the following embodiments. The embodiments of the present application do not limit this. The following embodiments are described by taking the memory recovery driver module, the IOMMU driver, and the physical memory management module as independent settings.
[0106] The electronic device may include a user space and a kernel space. The application is in the user space, and the memory recycling driver module, the IOMMU driver, and the physical memory management module are in the kernel space.
[0107] In some embodiments, the kernel space may be configured with an interface that the application can call to interact with the module in the kernel space. For example, the kernel space may provide an input / output control (ioctl) interface for the application process, and the application can call the ioctl interface to interact with the memory recycling driver module.
[0108] In an embodiment of the present application, in a scenario where an application and a hardware device share physical memory, the application can actively request the memory recovery driver module to trigger the release of part of the physical memory occupied by the application. After this part of the physical memory is released, the application can also actively request the memory recovery driver module to reallocate physical memory for the application.
[0109] The memory recycling driver module can trigger the physical memory management module to release part of the physical memory occupied by the application in response to the application's request, and trigger the physical memory management module to reallocate shared physical memory for the application and hardware devices.
[0110] In some embodiments, the memory reclaim driver module is further configured to manage the first page table, and the IOMMU driver is configured to manage the second page table.
[0111] In some embodiments, in a scenario where an application and a hardware device share physical memory, the operating system can allocate shared physical memory for the application and the hardware device, and establish a mapping relationship between the virtual address and the physical address of the application, as well as a mapping relationship between the virtual address and the physical address of the hardware device.
[0112] In an embodiment of the present application, in order to facilitate the distinction between the virtual address of the process of the application program mapped to physical memory and the virtual address of the hardware device mapped to physical memory. In some embodiments, the virtual address of the process of the application program mapped to physical memory can be used as the first virtual address, and the virtual address of the hardware device mapped to the physical memory can be used as the second virtual address. In other words, the first page table can store the mapping relationship between the physical address of the physical memory and the first virtual address, and the second page table can store the mapping relationship between the physical address of the physical memory and the second virtual address.
[0113] Specifically, the functions of the application program, the memory recycling driver module, the IOMMU driver, and the physical memory management module may be described in the following embodiments.
[0114] In some embodiments, the kernel space may use a DMA-BUF framework. Accordingly, the memory recovery driver module may be a DMA-BUF memory recovery driver module, and the physical memory management module may be a DMA-BUF memory management module or an ION driver.
[0115] The DMA-BUF memory management module is used to allocate shared physical memory for applications and hardware devices.
[0116] To better understand the memory processing method provided in the embodiments of the present application, the video decoding scenario is taken as an example, and the "kernel space uses the DMA-BUF framework" is used as an example to briefly describe the process of the DMA-BUF memory management module allocating the same physical memory to the application and hardware devices (such as video decoding hardware):
[0117] Referring to Figure 3B , an electronic device may include a user space, a kernel space, and a hardware layer. Referring to Figure 3B , the user space includes applications and codec services, while the kernel space includes a memory management module, an IOMMU driver, and a codec driver. The hardware layer includes an IOMMU, a DMA module, and video decoding hardware. For example, Figure 3B uses the ION driver as an example of a memory management module.
[0118] The following describes the process of the ION driver allocating the same physical memory for the application and the video decoding hardware, with reference to the structure of the electronic device shown in FIG3B :
[0119] Step 1: The application calls the codec service to enable codec service initialization.
[0120] Among them, the application calls the codec service, which can trigger the initialization of the codec service.
[0121] Step 2: The codec service requests physical memory from the ION driver.
[0122] In some embodiments, the codec service requests physical memory from the ION driver, which can be regarded as the application requesting physical memory from the ION driver.
[0123] In some embodiments, when the codec service requests physical memory from the ION driver, the codec service may send a memory allocation request to the ION driver, where the memory allocation request is used to instruct allocation of the same physical memory for the application and the video decoding hardware.
[0124] Step 3: The ION driver allocates shared physical memory for the application and video decoding hardware.
[0125] In response to the memory allocation request from the codec service, the ION driver can allocate the same physical memory for the application and the hardware device, and record the physical memory information. The physical memory information may include, but is not limited to, the size of the physical memory and the page frame number corresponding to the physical memory. It should be understood that in Figure 3B, the physical memory is represented by a 4K physical page frame.
[0126] In some embodiments, the ION driver may use an ION buffer to describe physical memory information. The ION buffer can be considered a data structure that records physical memory information. After storing the ION buffer, the ION driver may provide the application with an identifier for the ION buffer. The ION buffer identifier is used to distinguish ION buffers corresponding to different physical memories. The ION buffer identifier may include, but is not limited to, a serial number, an ID, and other information. In some embodiments, the ION buffer identifier may be, for example, a file descriptor (FD).
[0127] In some embodiments, because the ION buffer is used to record information of the physical memory, the identifier of the ION buffer can be regarded as the identifier of the physical memory, and the identifier of the ION buffer can indicate the physical memory.
[0128] Step 4: The application performs memory mapping on the shared physical memory.
[0129] In an embodiment of the present application, after the ION driver allocates the same physical memory to the application and the video decoding hardware, the application can memory-map the shared physical memory. For example, the application can map the first virtual address of the application to the physical memory. Accordingly, the ION driver can record the first virtual address mapped to the physical memory in the ION buffer.
[0130] Step 5: The codec service performs memory mapping on the shared physical memory.
[0131] The codec service may memory-map the shared physical memory. In other words, the codec service may map the fourth virtual address of the codec service to the physical memory. The fourth virtual address of the codec service may be an address in the virtual address space of the codec service. The virtual address space of the codec service is allocated to the codec service by the operating system. For details, refer to the description of how the operating system allocates virtual address space to an application.
[0132] It should be understood that both the application and the codec service are in user space, and the virtual address and physical address of the process running in the user space can be recorded in the first page table. Exemplarily, the first page table may include: a mapping relationship between the first virtual address of the application and the physical address of the physical memory, and a mapping relationship between the fourth virtual address of the codec service and the physical address of the physical memory.
[0133] In some embodiments, after the codec service maps the fourth virtual address of the codec service to the physical memory, the ION driver may record the fourth virtual address mapped to the physical memory in the ION buffer.
[0134] Step 6: The codec service calls the codec driver to enable codec driver initialization.
[0135] Step 7: The codec driver performs memory mapping on the shared physical memory.
[0136] Similarly, the codec driver can memory-map the shared physical memory. In other words, the codec driver can map the third virtual address of the kernel space to the physical memory. The third virtual address can be an address in the virtual address space of the kernel space, which is allocated to the codec driver by the operating system.
[0137] In some embodiments, the codec driver is in kernel space, and the virtual address and physical address of the driver or service running in kernel space can be recorded in the third page table. In other words, the third page table is used to store the mapping relationship between the virtual address and physical address of the driver or service running in kernel space. Exemplarily, the third page table may include: a mapping relationship between the third virtual address and the physical address of the physical memory.
[0138] In some embodiments, after the codec driver maps the third virtual address of the kernel space to the physical memory, the ION driver may record the third virtual address mapped to the physical memory in the ION buffer.
[0139] Step 8: The codec driver calls the video decoding hardware to enable the initialization of the video decoding hardware.
[0140] Step 9: The IOMMU driver performs DMA memory mapping on the shared physical memory.
[0141] As described in step 4, because the shared physical memory is allocated to the application and the video decoding hardware, the IOMMU driver can similarly perform DMA memory mapping on the shared physical memory. The IOMMU driver can map the second virtual address of the video decoding hardware to the physical memory. Accordingly, the ION driver can record the second virtual address mapped to the physical memory in the ION buffer.
[0142] In an embodiment of the present application, the first page table can store a mapping relationship between a first virtual address and the physical address of the physical memory, as well as a mapping relationship between a fourth virtual address and the physical address of the physical memory. The second page table can store a mapping relationship between a second virtual address and the physical address of the physical memory. The third page table can store a mapping relationship between a third virtual address of the kernel space and the physical address of the physical memory. When an application accesses the shared physical memory, the MMU can query the first page table, convert the first virtual address into a physical address, and enable the application to access the physical memory. Similarly, when the video decoding hardware accesses the physical memory, the IOMMU can query the second page table, convert the second virtual address into a physical address, and enable the video decoding hardware to access the physical memory. Similarly, when the codec driver in the kernel space accesses the physical memory, the third virtual address can be converted into a physical address, and the codec driver in the kernel space can access the physical memory.
[0143] Figure 3C shows a schematic diagram of the application, codec service, codec driver, and IOMMU driver performing memory mapping on the shared physical memory. Referring to Figure 3C , the application can perform memory mapping on the physical memory, mapping the application's first virtual address to the physical memory. "Mmap" is short for "memory mapping." Similarly, the codec service can perform mmap mapping on the physical memory, and can also map the codec service's fourth virtual address to the physical memory.
[0144] The vmap technology implements virtual memory management by allocating unique "virtual pages" to all page table entries of the kernel space mapped in the virtual memory space. Referring to Figure 3C, the codec driver can perform vmap mapping on the physical memory and map the third virtual address in the kernel space to the physical memory.
[0145] In addition, the IOMMU driver can perform DMA memory mapping on the shared physical memory. For example, the IOMMU driver can map the second virtual address of the video decoding hardware to the physical memory. Correspondingly, the ION driver can record the second virtual address mapped to the physical memory in the ION buffer.
[0146] Figure 3D shows the process of the application and video decoding hardware accessing the physical memory after the ION driver allocates the same physical memory to the application and video decoding hardware. The application, codec service, codec driver, and IOMMU driver memory-map the shared physical memory.
[0147] In step 1A, the application writes the video data to be decoded into the physical memory.
[0148] In an embodiment of the present application, when an application accesses a physical memory, the MMU may convert a first virtual address into a physical address of the physical memory, so that the application accesses the physical memory to write the video data to be decoded.
[0149] Step 2A: The application requests decoding from the codec service.
[0150] Step 3A: The codec service requests decoding from the codec driver.
[0151] Step 4A: The codec driver requests decoding from the video decoding hardware.
[0152] In step 5A, the DMA module reads the video data to be decoded from the physical memory.
[0153] In an embodiment of the present application, the video decoding hardware accesses the physical memory, and the DMA module can convert the second virtual address into a physical address of the physical memory, thereby enabling the DMA module to read the video data to be decoded from the physical memory.
[0154] Step 6A: The video decoding hardware decodes the video data to be decoded to obtain decoded data.
[0155] In step 7A, the DMA module writes the decoded data into the physical memory.
[0156] Step 8A: The video decoding hardware feeds back decoding completion to the codec driver.
[0157] Step 9A: The codec driver feeds back decoding completion to the codec service.
[0158] Step 10A: The codec service feeds back decoding completion to the application.
[0159] In step 11A, the application program reads the decoded data from the physical memory.
[0160] In an embodiment of the present application, when an application accesses physical memory, the MMU may convert a first virtual address into a physical address of the physical memory, enabling the application to access the physical memory to read decoded data. After the application obtains the decoded data, it may play a video based on the decoded data.
[0161] The memory processing method provided by the embodiments of the present application is described below in conjunction with specific embodiments. The following embodiments can be combined with each other, and the same or similar concepts or processes may not be repeated in some embodiments.
[0162] FIG4A is a flow chart of an embodiment of a memory processing method provided by an embodiment of the present application. Referring to FIG4A , the memory processing method provided by an embodiment of the present application may include:
[0163] S401: When the application is switched to the background, the application sends a first command to a memory recovery driver module, where the first command is used to instruct to release a first physical memory.
[0164] When an application switches to the background, it can suspend execution of the target service and temporarily stop using the first physical memory corresponding to the target service. The first physical memory can be considered as physical memory allocated by the operating system (or the physical memory management module in the operating system) for the target service of the application. The first physical memory can be used to store data associated with the target service.
[0165] In some embodiments, when the application is switched to the background, the application may send a first command to the memory recovery driver module, the first command being used to instruct the release of the first physical memory. Specifically, the first command is used to instruct the release of the first physical memory corresponding to the target business of the application.
[0166] Exemplarily, taking the application as a video playback application, the video playback application is playing a video. When the video playback application switches to the background, the video playback application can pause playing the video. The target business can be understood as the video playback business. Accordingly, in the scenario where the video playback application and the video decoding hardware share the first physical memory, the first physical memory corresponding to the video playback business. The first physical memory can be used to store data associated with the video playback business, and the data associated with the video business can include: video data to be decoded, and decoded data. Among them, when the video playback application switches to the background, the video playback application can pause the execution of the video playback business, so the video playback application does not need to access the data in the first physical memory (such as decoded data), so the video playback application can send a first command to the memory recovery driver module to instruct the release of the first physical memory.
[0167] In some embodiments, when the application is switched to the background, the application may suspend execution of the target service. After reading data associated with the target service in the first physical memory, the application may send a first command to the memory recycling driver module.
[0168] In some embodiments, the first command may include an identifier of the first physical memory, where the identifier of the first physical memory is used to indicate the first physical memory. Exemplarily, the identifier of the first physical memory may be an identifier of an ION buffer used to record information of the first physical memory, such as an FD of the ION buffer.
[0169] In some embodiments, when different applications are switched to the background, the target services that are suspended by the applications may be different. In embodiments of the present application, the specific target services that are suspended when different applications are switched to the background can be pre-configured in the applications. In this way, when the application is switched to the background, the target services that are suspended can be determined, so that the application can determine the identifier of the first physical memory and send the first command to the memory recovery driver module.
[0170] S402: The memory recycling driver module triggers the physical memory management module to release the first physical memory.
[0171] Physical memory management module, used to manage physical memory.
[0172] In response to the first command, the memory recycling driver module may determine the identifier of the first physical memory, for example, the identifier of the ION buffer used to record information of the first physical memory, FD1.
[0173] In some embodiments, the memory recovery driver module can send a second command to the physical memory management module. The second command is used to instruct the release of the first physical memory. The second command can include an identifier of the first physical memory, such as FD1. In response to the second command, the physical memory management module can query the ION buffer corresponding to FD1. Because the ION buffer records the information of the first physical memory, the physical memory management module can determine the first physical memory based on the information of the first physical memory, and then release the first physical memory. It should be understood that Figure 4A shows the steps of the memory recovery driver module sending the second command to the physical memory management module and the physical memory management module releasing the first physical memory.
[0174] In an embodiment of the present application, in a scenario where an application and a hardware device share a first physical memory, when the application switches to the background and temporarily does not use the first physical memory, it can actively request to release the first physical memory, thereby increasing the available memory of the electronic device. The released first physical memory can be allocated to other businesses for use, thereby improving memory utilization.
[0175] In some embodiments, because the application can suspend the execution of the target business when it switches to the background, the application will not access the first virtual address corresponding to the target business. However, after the physical memory management module releases the first physical memory corresponding to the target business, the first physical memory may be allocated to the business of other applications, and the first physical memory may store data associated with the business of other applications. In order to avoid the situation where the application switched to the background can still access the first physical memory, the hardware device can still access the first physical memory, and the kernel space (such as the codec service in the kernel space) can still access the first physical memory, causing data security problems, in an embodiment of the present application, the memory recovery driver module can also modify the first page table, the second page table, and the third page table to release the mapping relationship between the first physical address and the first virtual address, the mapping relationship between the first physical address and the second virtual address, and the mapping relationship between the first physical address and the fourth virtual address.
[0176] 4B , the memory processing method provided in the embodiment of the present application may include:
[0177] S401A, when the application is switched to the background, the application sends a first command to the memory recovery driver module, where the first command is used to instruct to release the first physical memory.
[0178] For S401A, reference may be made to the description in S401.
[0179] S402A: The memory recycling driver module releases the mapping relationship between the first physical address and the first virtual address, and the mapping relationship between the first physical address and the third virtual address.
[0180] In an embodiment of the present application, in response to the first command, the memory recycling driver module may determine an identifier of the first physical memory, such as FD1. The memory recycling driver module may access the ION buffer identified as FD1 based on FD1. Because the ION buffer records the first virtual address that maps the first physical memory, the memory recycling driver module may determine the mapping relationship between the first physical memory and the first virtual address.
[0181] The first page table may store a mapping relationship between the first physical address and the first virtual address. The memory recycling driver module may modify the first page table to release the mapping relationship between the first physical address and the first virtual address.
[0182] In some embodiments, after the memory recycling driver module determines the first virtual address, the memory recycling driver module may modify the first physical address mapped to the first virtual address in the first page table. For example, the memory recycling driver module may modify the first physical address mapped to the first virtual address to be empty, or modify the first physical address to a preset physical address. The value in the preset physical address may be a preset value. For example, if the preset value is 0, the memory recycling driver module may fill the first physical address mapped to the first virtual address in the first page table with 0.
[0183] In some embodiments, because the ION buffer records the fourth virtual address of the user space (e.g., the codec service in the user space) that maps the first physical memory, the memory recovery driver module can determine the mapping relationship between the first physical memory and the fourth virtual address. In addition, because the first page table can store the mapping relationship between the first physical address and the fourth virtual address, the memory recovery driver module can modify the first page table to release the mapping relationship between the first physical address and the fourth virtual address. Exemplarily, the memory recovery driver module can modify the first physical address mapped by the fourth virtual address in the first page table.
[0184] Similarly, because the ION buffer records the third virtual address of the kernel space mapping the first physical memory, the memory recovery driver module can determine the mapping relationship between the first physical memory and the third virtual address.
[0185] The third page table can store a mapping relationship between the first physical address and the third virtual address. The memory recycling driver module can modify the third page table to release the mapping relationship between the first physical address and the third virtual address.
[0186] In some embodiments, after the memory reclamation driver module determines the third virtual address, the memory reclamation driver module may modify the first physical address mapped to the third virtual address in the third page table. For example, the memory reclamation driver module may modify the first physical address mapped to the third virtual address to be empty, or modify the third physical address to a preset physical address.
[0187] In summary, the memory recovery driver module can modify the first page table, release the mapping relationship between the first virtual address and the first physical address, and the mapping relationship between the fourth virtual address and the first physical address. In addition, the memory recovery driver module can modify the third page table, release the mapping relationship between the third virtual address and the first physical address.
[0188] It is understood that, in the embodiment of the present application, taking the video decoding scenario as an example, during the video encoding and decoding process, the codec service in the user space and the codec driver in the kernel space are required. In some embodiments, the codec service can be used as a business service, and the codec driver can be used as a business driver. It is understood that in other business scenarios, the business service in the user space or the business driver in the kernel space may not be used, or the business service or business driver may not perform memory mapping on the shared physical memory, and the ION driver will not record the third virtual address and the fourth virtual address mapped to the physical memory in the ION buffer.
[0189] It is understandable that when the business driver in the kernel space does not perform memory mapping on the shared physical memory, the ION driver will not record the third virtual address mapped to the physical memory in the ION buffer. Accordingly, the mapping relationship between the third virtual address and the physical address of the physical memory may not be recorded in the third page table, and the memory recovery driver module will not modify the third page table. Similarly, when the business service in the user space does not perform memory mapping on the shared physical memory, the ION driver will not record the fourth virtual address mapped to the physical memory in the ION buffer. Accordingly, the mapping relationship between the fourth virtual address and the physical address of the physical memory will not be recorded in the first page table, and the memory recovery driver module will not modify the mapping relationship.
[0190] S403A: The memory recycling driver module triggers the IOMMU driver to release the mapping relationship between the first physical address and the second virtual address.
[0191] In some embodiments, there is no order in which S402A and S403A are performed.
[0192] In an embodiment of the present application, in response to the first command, the memory recycling driver module can determine the identifier of the first physical memory, such as FD1. The memory recycling driver module can access the ION buffer identified as FD1 based on FD1. Because the ION buffer not only records the mapping of the first virtual address to the first physical memory, but also records the mapping of the second virtual address to the first physical memory, the memory recycling driver module can determine the mapping relationship between the first physical memory and the second virtual address.
[0193] The second page table may store a mapping relationship between the first physical address and the second virtual address. In an embodiment of the present application, the memory recovery driver module may trigger the IOMMU driver to release the mapping relationship between the first physical address and the second virtual address. In some embodiments, the memory recovery driver module may send a third command to the IOMMU driver, the third command being used to instruct the IOMMU driver to release the mapping relationship between the first physical address and the second virtual address. In some embodiments, the third command may include the second virtual address.
[0194] In some embodiments, the IOMMU driver may modify the first physical address mapped by the second virtual address in the second page table. For example, the IOMMU may modify the first physical address mapped by the second virtual address to be empty, or may modify the first physical address to be a preset physical address. The value in the preset physical address may be a preset value. For example, if the preset value is 0, the IOMMU driver may fill the first physical address mapped by the second virtual address in the second page table with 0.
[0195] In some embodiments, the physical address mapped by the first virtual address in the modified first page table may be different from the physical address mapped by the second virtual address in the modified second page table.
[0196] S404A: The memory recycling driver module triggers the physical memory management module to release the first physical memory.
[0197] S404A may refer to the description in S402.
[0198] FIG5 is a flow chart of another embodiment of the memory processing method provided in the embodiment of the present application. Referring to FIG5 , the memory processing method may include:
[0199] In step 1B, when the application is switched to the background, the application instructs the memory recycling driver module to reclaim the first physical memory through the ioctl interface.
[0200] In step 2B, the memory recycling driver module modifies the first page table, and releases the mapping relationship between the first physical address and the first virtual address, and the mapping relationship between the first physical address and the third virtual address.
[0201] In step 3B, the IOMMU driver modifies the second page table to release the mapping relationship between the first physical address and the second virtual address.
[0202] Step 4B: The physical memory management module releases the first physical memory.
[0203] Steps 1B to 4B may refer to the description in FIG. 4A and FIG. 4B .
[0204] In an embodiment of the present application, when the application switches to the background, the memory recovery driver module can release the mapping relationship between the first physical address and the first virtual address, as well as the mapping relationship between the first physical address and the third virtual address, and the IOMMU driver can release the mapping relationship between the first physical address and the second virtual address. This can avoid the problem that after the physical memory management module releases the first physical memory, the application, hardware device switched to the background, and the business driver in the kernel space can still access the first physical memory, thereby improving data security.
[0205] After the physical memory management module releases the first physical memory, when the application switches to the foreground, the application can continue to execute the target business, and the application needs to continue to access the data in the first physical memory. For example, taking the video decoding scenario as an example, when the video playback application switches to the background, the physical memory management module can release the physical memory corresponding to the video playback business, and the operating system can allocate the physical memory to the business of other applications. When the video playback application switches to the foreground, the video playback application needs to continue to play the video, and the video playback application needs to continue to access the data associated with the target business (such as decoded data) to play the video.
[0206] In some embodiments, when an application switches to the background, the application sends a first command to the memory reclaiming driver module. However, the application may not have completely read the data associated with the target service in the first physical memory. Therefore, to ensure that the application can still access the data associated with the target service after the physical memory management module releases the first physical memory, the physical memory management module may copy the data stored in the first physical memory to the hard disk before releasing the first physical memory.
[0207] When the application switches to the foreground and needs to continue accessing data associated with the target business, the physical memory management module can reallocate the second physical memory to the target business of the application and copy the data associated with the target business stored on the hard disk to the second physical memory. In this way, the application can continue to access the data associated with the target business in the second physical memory and continue to execute the target business.
[0208] In some embodiments, when the application is switched to the background, the application can send a first command to the memory recovery driver module after reading the data associated with the target business in the first physical memory. In this example, because the application has already read the data associated with the target business in the first physical memory, when the application is switched to the foreground, the application can continue to execute the target business based on the data associated with the target business that has been read. In this example, the physical memory management module does not need to copy the data stored in the first physical memory to the hard disk before releasing the physical memory corresponding to the target business, thereby reducing content copying.
[0209] In an embodiment of the present application, when the application switches to the foreground, the application program can continue to execute the target business, and the physical memory management module can reallocate the shared second physical memory for the application program and the hardware device so that the application program can continue to write data in the second physical memory, and / or read data, and the hardware device can continue to write data in the second physical memory, and / or read data.
[0210] In some embodiments, the address of the second physical memory may be referred to as a second physical address, and the size of the second physical memory is equal to the size of the first physical memory.
[0211] The following describes a process in which the physical memory management module reallocates the second physical memory for the target service of the application. Referring to FIG. 6 , the memory processing method provided in the embodiment of the present application may include:
[0212] S601: When an application starts to switch from the background to the foreground, the application sends a fourth command to a memory recovery driver module, where the fourth command is used to instruct allocation of physical memory for the application.
[0213] After the application switches to the foreground, the application can continue to execute the target business. The application needs to continue to access the data associated with the target business. Therefore, the operating system needs to reallocate the second physical memory for the target business of the application. The second physical memory is used to store the data associated with the target business. In an embodiment of the present application, when the application starts to switch from the background to the foreground, the application can send a fourth command to the memory recycling driver module. The fourth command is used to instruct the allocation of physical memory for the application.
[0214] In some embodiments, the user may perform a first operation to trigger the application to switch from the background to the foreground. In response to the first operation, the application sends a fourth command to the memory recovery driver module when starting to switch from the background to the foreground.
[0215] In some embodiments, the fourth command may include an identifier of the first physical memory, such as FD1.
[0216] S602: The memory recycling driver module triggers the physical memory management module to allocate a shared second physical memory for the application program and the hardware device.
[0217] In response to the fourth command, the memory recycling driver module can access the ION buffer corresponding to FD1 to determine the size of the first physical memory, etc. The memory recycling driver module can trigger the physical memory management module to allocate a second physical memory of the same size to the application.
[0218] In some embodiments, the memory recycling driver module may send a fifth command to the physical memory management module, the fifth command being used to instruct the physical memory management module to reallocate the second physical memory shared by the application program and the hardware device. In some embodiments, the fifth command may include information such as the size of the second physical memory.
[0219] S603: The physical memory management module feeds back the second physical address of the second physical memory to the memory recycling driver module.
[0220] In response to the fifth command, the physical memory management module may allocate a second physical memory shared by the application and the hardware device, the address of the second physical memory being referred to as a second physical address, and the physical memory management module may feed back the second physical address to the memory recycling driver module.
[0221] S604: The memory recycling driver module establishes a mapping relationship between the first virtual address and the second physical address, and a mapping relationship between the third virtual address and the second physical address.
[0222] The memory recycling driver module may modify the first page table to establish a mapping relationship between the second physical address and the first virtual address. In some embodiments, the memory recycling driver module may modify the physical address mapped to the first virtual address in the first page table, for example, the memory recycling driver module may modify the preset physical address mapped to the first virtual address to the second physical address.
[0223] In some embodiments, the memory recovery driver module modifies the first physical address mapped by the fourth virtual address in the first page table. After the physical memory management module reallocates the shared second physical memory to the application and the hardware device, the memory recovery driver module can modify the physical address mapped by the fourth virtual address in the first page table. For example, the memory recovery driver module modifies the preset physical address mapped by the fourth virtual address to the second physical address.
[0224] Similarly, the memory recycling driver module can modify the third page table to establish a mapping relationship between the second physical address and the third virtual address. In some embodiments, the memory recycling driver module can modify the physical address mapped to the third virtual address in the third page table. For example, the memory recycling driver module modifies the preset physical address mapped to the third virtual address to the second physical address.
[0225] S605: The memory recycling driver module triggers the IOMMU driver to establish a mapping relationship between the second virtual address and the second physical address.
[0226] In some embodiments, there is no order distinction between S604 and S605.
[0227] Similarly, the memory reclamation driver module can trigger the IOMMU driver to establish a mapping relationship between the second virtual address and the second physical address. In some embodiments, the memory reclamation driver module can send a sixth command to the IOMMU driver, the sixth command being used to instruct the IOMMU driver to establish a mapping relationship between the second virtual address and the second physical address. The sixth command can include the second physical address.
[0228] In some embodiments, in response to the sixth command, the IOMMU driver may modify the physical address mapped by the second virtual address in the second page table. For example, the IOMMU driver may modify the physical address mapped by the second virtual address to the second physical address.
[0229] In this way, the physical memory management module can reallocate the shared second physical memory for the application and the hardware device, and the memory recovery driver module stores the mapping relationship between the first virtual address and the second physical address in the first page table, and the IOMMU driver stores the mapping relationship between the second virtual address and the second physical address in the second page table. Therefore, when the application accesses the second physical memory, the MMU can query the first page table and convert the first virtual address into the second physical address, thereby enabling the application to access the second physical memory. In addition, when the hardware device accesses the second physical memory, the IOMMU can query the second page table and convert the second virtual address into the second physical address, thereby enabling the hardware device to access the second physical memory.
[0230] FIG7 is another flow chart of the memory processing method provided by an embodiment of the present application. Referring to FIG7 , the memory processing method may include:
[0231] In step 1C, when the application starts to switch from the background to the foreground, the memory recycling driver module is instructed to reallocate physical memory to the application through the ioctl interface.
[0232] In step 2C, the ION driver reallocates the shared second physical memory for the application program and the hardware device, and feeds back the second physical address of the second physical memory to the memory recycling driver module.
[0233] In step 3C, the memory recycling driver module modifies the first page table to establish a mapping relationship between the first virtual address and the second physical address, and the memory recycling driver module modifies the third page table to establish a mapping relationship between the third virtual address and the second physical address.
[0234] Step 4C: The IOMMU module modifies the second page table to establish a mapping relationship between the second virtual address and the second physical address.
[0235] Steps 1C to 4C may refer to the description in FIG. 6 .
[0236] In an embodiment of the present application, when the application starts to switch from the background to the foreground, it requests the memory recovery driver module to re-allocate the shared second physical memory for the application and the hardware device. Among them, the memory recovery driver module can trigger the physical memory management module to allocate a shared second physical memory for the application and the hardware device, and the memory recovery driver module can modify the first page table to establish a mapping relationship between the first virtual address and the second physical address. The memory recovery driver module can modify the third page table to establish a mapping relationship between the third virtual address and the second physical address. In addition, the IOMMU driver can modify the second page table to establish a mapping relationship between the second virtual address and the second physical address. In this way, the application, the hardware device, and the kernel space can continue to access the data associated with the target business in the second physical memory, and the application can continue to execute the target business. The memory processing method provided in the embodiment of the present application does not affect the execution of the target business.
[0237] It should be noted that the data involved in this application (including but not limited to data used for analysis, stored data, displayed data, etc.) are all information and data authorized by the user or fully authorized by all parties, and the collection, use and processing of relevant data must comply with the relevant laws, regulations and standards of relevant countries and regions, and provide corresponding operation entrances for users to choose to authorize or refuse.
[0238] In one embodiment, the present application also provides an electronic device that can perform the steps in the above embodiments. Referring to Figure 8, the electronic device may include: a processor 801 (e.g., a CPU) and a memory 802. The memory 802 may include a high-speed random-access memory (RAM) and may also include a non-volatile memory (NVM), such as at least one disk storage. Various instructions can be stored in the memory 802 to perform various processing functions and implement the method steps of the present application.
[0239] Optionally, the electronic device involved in this application may further include: a power supply 803, a communication bus 804, and a communication port 805. The communication port 805 is used to enable communication between the electronic device and other peripheral devices. In the embodiment of the present application, the memory 802 is used to store computer-executable program code, which includes instructions; when the processor 801 executes the instructions, the instructions cause the processor 801 of the electronic device to perform the actions in the above-mentioned method embodiment. The implementation principles and technical effects are similar and will not be repeated here.
[0240] It should be noted that the modules or components described in the above embodiments may be one or more integrated circuits configured to implement the above methods, such as one or more application specific integrated circuits (ASICs), one or more digital signal processors (DSPs), or one or more field programmable gate arrays (FPGAs). For another example, when a module is implemented by scheduling program code through a processing element, the processing element may be a general-purpose processor, such as a central processing unit (CPU) or other processor that can call program code, such as a controller. For another example, these modules may be integrated together and implemented in the form of a system-on-a-chip (SOC).
[0241] In the above embodiments, it can be implemented in whole or in part by software, hardware, firmware or any combination thereof. When software is used for implementation, it can be implemented in whole or in part in the form of a computer program product. The computer program product includes one or more computer instructions. When the computer program instructions are loaded and executed on a computer, the process or function according to the embodiment of the present application is generated in whole or in part. 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 computer-readable storage medium. For example, the computer instructions can be transmitted from a website, computer, server or data center to another website, computer, server or data center via a wired (e.g., coaxial cable, optical fiber, digital subscriber line (DSL)) or wireless (e.g., infrared, wireless, microwave, etc.) method. The computer-readable storage medium can be any available medium that a computer can access or a data storage device such as a server or data center that includes one or more available media integrations. The available medium can be a magnetic medium (e.g., a floppy disk, a hard disk, a tape), an optical medium (e.g., a DVD), or a semiconductor medium (e.g., a solid-state drive (SSD)).
[0242] In the embodiments of the present application, "at least one" refers to one or more, and "plurality" refers to two or more. "And / or" describes the association relationship of associated objects, indicating that three relationships may exist. For example, A and / or B can represent: the existence of A alone, the existence of A and B at the same time, and the existence of B alone, where A and B can be singular or plural. The character " / " generally indicates that the previous and next associated objects are in an "or" relationship. "At least one of the following" or similar expressions refers to any combination of these items, including any combination of single or plural items. In addition, it should be understood that in the description of the present application, words such as "first" and "second" are only used to distinguish the purpose of description, and cannot be understood as indicating or implying relative importance, nor can they be understood as indicating or implying order.
[0243] It should be noted that the term "at the time" in the embodiments of the present application can refer to the instant when a certain situation occurs or a period of time after the situation occurs, and the embodiments of the present application do not specifically limit this. It should be understood that the various numerical numbers involved in the embodiments of the present application are merely for the convenience of description and are not intended to limit the scope of the embodiments of the present application.
[0244] It can be understood that in the embodiments of the present application, the size of the serial numbers of the above-mentioned processes does not mean the order of execution. The execution order of each process should be determined by its function and internal logic, and should not constitute any limitation on the implementation process of the embodiments of the present application.
Claims
1. A memory processing method, characterized in that: Applied to a memory processing device, the method comprises: receiving a first command from an application, the first command being used to instruct to release a first physical memory of the application, the first command being sent when the application is switched to a background, and the application and a hardware device share the first physical memory; The first physical memory is released.
2. The method according to claim 1, characterized in that The first page table includes a mapping relationship between a first physical address and a first virtual address, and the second page table includes a mapping relationship between the first physical address and a second virtual address, the first physical address is a physical address of the first physical memory, the first virtual address is a virtual address in a virtual address space of the application, and the second virtual address is a virtual address in a virtual address space of the hardware device; Before releasing the first physical memory, the method further includes: Modify the first physical address mapped by the first virtual address in the first page table; The first physical address mapped by the second virtual address in the second page table is modified.
3. The method according to claim 2, characterized in that The modifying the first physical address mapped by the first virtual address in the first page table comprises: Modifying the first physical address to a preset physical address; The modifying the first physical address mapped by the second virtual address in the second page table comprises: The first physical address is modified to the preset physical address.
4. The method according to claim 2 or 3, characterized in that: The method further comprises: receiving a fourth command from the application, the fourth command being used to instruct to reallocate a shared physical memory for the application and the hardware device, the fourth command being sent when the application starts to switch from the background to the foreground; Allocating a shared second physical memory for the application and the hardware device, wherein the physical address of the second physical memory is a second physical address; Modify the physical address mapped by the first virtual address in the first page table to the second physical address; The physical address mapped by the second virtual address in the second page table is modified to the second physical address.
5. The method according to any one of claims 2 to 4, characterized in that: The third page table includes a mapping relationship between the first physical address and the third virtual address, where the third virtual address is a virtual address of a service driver in the kernel space. Before releasing the first physical memory, the method further includes: Modify the first physical address mapped by the third virtual address in the third page table; After allocating a shared second physical memory for the application and the hardware device, the method further includes: The physical address mapped by the third virtual address in the third page table is modified to the second physical address.
6. The method according to any one of claims 2 to 5, characterized in that: The first command includes: an identifier of the first physical memory.
7. The method according to claim 6, characterized in that The identifier of the first physical memory is: an identifier of a buffer zone used to record information of the first physical memory.
8. The method according to claim 7, characterized in that The memory processing device comprises: a memory recovery driving module and a physical memory management module; The receiving a first command from an application comprises: The memory recovery driving module receives the first command from the application program; The releasing the first physical memory comprises: The memory recovery driver module sends a second command to the physical memory management module, where the second command is used to instruct to release the first physical memory; The physical memory management module releases the first physical memory.
9. The method according to claim 8, characterized in that The memory processing device further includes: an input-output memory management unit IOMMU driver, and the buffer is further used to record a first virtual address and a second virtual address mapped to the first physical memory; The modifying the first physical address mapped by the first virtual address in the first page table comprises: The memory recovery driving module obtains a first virtual address and a second virtual address mapped to the first physical memory according to the identifier of the buffer zone; Modify the first physical address mapped by the first virtual address in the first page table; The modifying the first physical address mapped by the second virtual address in the second page table comprises: The memory recovery driver module sends a third command to the IOMMU driver, where the third command is used to instruct to modify the first physical address mapped by the second virtual address in the second page table; The IOMMU driver modifies the first physical address mapped by the second virtual address in the second page table.
10. The method according to claim 9, characterized in that Modifying the first physical address of the third virtual address mapping in the third page table includes: The memory recovery driving module obtains a third virtual address mapped to the first physical memory according to the identifier of the buffer zone; The first physical address mapped by the third virtual address in the third page table is modified.
11. The method according to claim 10, characterized in that Receiving a fourth command from the application, comprising: The memory recovery driving module receives a fourth command from the application program; Allocating a shared second physical memory for the application and the hardware device, comprising: The memory recovery driver module sends a fifth command to the physical memory management module, wherein the fifth command is used to instruct to allocate a shared physical memory for the application and the hardware device; The physical memory management module allocates a shared second physical memory for the application and the hardware device; The physical memory management module sends the second physical address of the second physical memory to the memory recovery driver module; Modifying the physical address mapped by the first virtual address in the first page table to the second physical address includes: The memory recovery driver module modifies the physical address mapped by the first virtual address in the first page table into the second physical address; Modifying the physical address mapped by the second virtual address in the second page table to the second physical address includes: The memory recovery driver module sends a sixth command to the IOMMU driver, where the sixth command is used to instruct to modify the physical address mapped by the second virtual address in the second page table to the second physical address; The IOMMU driver modifies the physical address mapped by the second virtual address in the second page table into the second physical address; Modifying the physical address mapped by the third virtual address in the third page table to the second physical address includes: The memory recycling driving module modifies the physical address mapped by the third virtual address in the third page table into the second physical address.
12. The method according to any one of claims 1 to 11, characterized in that The memory processing device is located in the kernel space of the electronic device.
13. A memory processing method, characterized in that: Applied to an electronic device, the electronic device includes an application program and a memory processing device, and the method includes: When the application is switched to the background, the application sends a first command to the memory processing device, where the first command is used to instruct to release a first physical memory of the application, and the application and the hardware device share the first physical memory; In response to the first command, the memory processing device releases the first physical memory.
14. The method according to claim 13, characterized in that The first page table includes a mapping relationship between a first physical address and a first virtual address, and the second page table includes a mapping relationship between the first physical address and a second virtual address, the first physical address is a physical address of the first physical memory, the first virtual address is a virtual address in a virtual address space of the application, and the second virtual address is a virtual address in a virtual address space of the hardware device; Before the memory processing device releases the first physical memory, the method further includes: The memory processing device modifies the first physical address mapped by the first virtual address in the first page table, and modifies the first physical address mapped by the second virtual address in the second page table.
15. The method according to claim 14, characterized in that The method further comprises: When the application starts to switch from the background to the foreground, the application sends a fourth command to the memory processing device, where the fourth command is used to instruct to reallocate a shared physical memory for the application and the hardware device; The memory processing device allocates a shared second physical memory for the application and the hardware device, and the physical address of the second physical memory is a second physical address; The memory processing device modifies the physical address mapped by the first virtual address in the first page table to the second physical address, and modifies the physical address mapped by the second virtual address in the second page table to the second physical address.
16. The method according to claim 15, characterized in that The third page table includes a mapping relationship between the first physical address and the third virtual address, the third virtual address is a virtual address of a service driver in the kernel space, and before the memory processing device releases the first physical memory, the memory processing device further includes: The memory processing device modifies the first physical address mapped by the third virtual address in the third page table; After the memory processing device allocates a shared second physical memory for the application program and the hardware device, the memory processing device further includes: The memory processing device modifies the physical address mapped by the third virtual address in the third page table into the second physical address.
17. The method according to any one of claims 14 to 16, characterized in that: The first command includes: an identifier of the first physical memory.
18. The method according to claim 17, characterized in that The identifier of the first physical memory is: an identifier of a buffer zone used to record information of the first physical memory.
19. The method according to claim 18, characterized in that The buffer is also used to record a first virtual address and a second virtual address mapped to the first physical memory; The memory processing device modifies the first physical address mapped by the first virtual address in the first page table, and modifies the first physical address mapped by the second virtual address in the second page table, including: The memory processing device obtains the first virtual address and the second virtual address mapped to the first physical memory according to the identifier of the buffer; The memory processing device modifies the first physical address mapped by the first virtual address in the first page table, and modifies the first physical address mapped by the second virtual address in the second page table; The memory processing device modifies the first physical address mapped by the third virtual address in the third page table, including: The memory processing device acquires a third virtual address mapped to the first physical memory according to the identifier of the buffer; The memory processing device modifies the first physical address mapped by the third virtual address in the third page table.
20. The method according to any one of claims 13 to 19, characterized in that The application is located in the user space of the electronic device, and the memory processing device is located in the kernel space of the electronic device.
21. A memory processing device, characterized in that: Comprising modules for executing the method of any one of claims 1-12.
22. An electronic device, characterized in that: Includes an application program and the memory processing device as claimed in claim 21.
23. An electronic device, characterized in that: include: Processor and memory; The memory stores computer-executable instructions; The processor executes the computer-executable instructions stored in the memory, so that the processor performs the method according to any one of claims 1 to 20.
24. A computer-readable storage medium, characterized in that: The computer-readable storage medium stores a computer program or instruction, and when the computer program or instruction is executed, the method according to any one of claims 1 to 20 is implemented.
Citation Information
Patent Citations
Memory processing method, electronic equipment and readable storage medium
CN119537260A
Memory freeing method and device
CN104375880A
Android equipment program management method and Android equipment
CN105786519A
Junk memory recycling method and device, electronic equipment and storage medium
CN111258921A
Memory access method, chip, electronic equipment and computer readable storage medium
CN116136826A