Memory processing method, electronic equipment and readable storage medium
By allowing applications to actively request the recovery of physical memory and redistribute it in the scenario where applications and hardware devices share physical memory, the problem of physical memory being permanently occupied is solved, and the available memory and memory usage of electronic devices is improved.
Patent Information
- Application Number
- CN202311160306.9
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Priority Date
- 2023-08-29
- Filing Date
- 2023-09-07
- Publication Date
- 2025-02-28
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.
It provides a memory processing method, allowing an application to actively request the recycling of shared physical memory when switching to the background, send commands to free up physical memory through the memory processing device, and redistribute the shared physical memory when needed.
By actively reclaiming physical memory, the available memory of electronic devices is increased, the memory usage is improved, the problem of physical memory management module still accessible after being released, and data security is improved.
Smart Images

Figure 00000000_0000_ABST
Abstract
Description
[0001] This application claims priority to the Chinese patent application filed with the State Intellectual Property Office of China on August 29, 2023, with application number 202311103138.X and application name “Memory Processing Method, Electronic Device and Readable Storage Medium”, all contents of which are incorporated by reference in 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 technology is a memory management technology that allows application processes and hardware devices in electronic devices to believe that they have continuous available memory, but in fact, these memories may be scattered on the physical memory. Virtual memory technology can achieve efficient use of memory and isolation of application processes.
[0004] Currently, in the scenario where the application process and the hardware device 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 out of memory, the physical memory cannot be reclaimed. As this situation increases, the available memory of the electronic device decreases. Summary of the invention
[0005] 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 program can actively request to reclaim the physical memory segment to increase the available memory of the electronic device.
[0006] In a first aspect, an embodiment of the present application provides a memory processing method, which is applied to a memory processing device. In this method, an application and a hardware device can share a first physical memory. When the application does not need 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. Accordingly, the memory processing device can receive the first command from the application, and the first command is used to instruct to release the first physical memory of the application. In response to the first command, the memory processing device can release the first physical memory.
[0007] In an embodiment of the present application, when the application is switched to the background and the first physical memory is temporarily not used, the application can actively request to release the first physical memory to increase the available memory of the electronic device.
[0008] 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 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.
[0009] 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.
[0010] 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.
[0011] 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 that switch to the background can still access the first physical memory, thereby improving data security.
[0012] 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.
[0013] 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.
[0014] In a possible implementation, when the 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 to execute the business. When the application starts to switch from the background to the foreground, it sends 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.
[0015] 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.
[0016] In a possible implementation manner, 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.
[0017] 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. Among them, 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 application business.
[0018] 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.
[0019] In a possible implementation, the identifier of the first physical memory is: an identifier of a buffer for recording information of the first physical memory. After allocating a shared first physical memory for an application and a hardware device, the memory processing device may also store in the buffer a first virtual address of an application mapped to the first physical memory, a second virtual address of a hardware device, and a third virtual address of a kernel space.
[0020] In a possible implementation, the memory processing device includes: a memory recycling driver module and a physical memory management module. The following is an introduction to the memory processing method provided by the embodiment of the present application based on the interaction of more detailed modules:
[0021] The memory processing device receives a first command from an application program, including: the memory recovery driving module receives the first command from the application program.
[0022] The memory processing device releases the first physical memory, comprising: the memory recovery driving module sends a second command to the physical memory management module, the second command is used to instruct to release the first physical memory. The physical memory management module can release the first physical memory in response to the second command.
[0023] In a possible implementation, 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.
[0024] The memory recovery driver module responds to the first command and can query the buffer corresponding to the identifier according to the buffer, obtain the first virtual address and the second virtual address mapped to the first physical memory, and modify the first physical address mapped by the first virtual address in the first page table.
[0025] The memory recovery driver module may send a third command to the IOMMU driver, wherein the third command is used to instruct to modify the first physical address mapped by the second virtual address in the second page table. In response to the third command, the IOMMU driver may modify the first physical address mapped by the second virtual address in the second page table.
[0026] 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.
[0027] In an embodiment of the present application, based on the interaction of 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.
[0028] In a possible implementation, correspondingly, the memory processing device receives a fourth command from the application, including: the memory recovery driver module receives the fourth command from the application. In response to the fourth command, the memory recovery driver module can allocate the same second physical memory for the application and the hardware device, including: the memory recovery driver module sends a fifth command to the physical memory management module, and the fifth command is used to instruct to allocate the same physical memory for the application and the hardware device.
[0029] 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.
[0030] After the memory recovery driver module receives the second physical address of the second physical memory from the physical memory management module, the memory recovery driver module modifies the physical address mapped by the first virtual address in the first page table to the second physical address. In addition, the memory recovery driver module modifies the physical address mapped by the third virtual address in the third page table to the second physical address.
[0031] In addition, the memory recovery driver module sends a sixth command to the IOMMU driver, and 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. In response to the sixth command, the IOMMU driver modifies the physical address mapped by the second virtual address in the second page table to the second physical address.
[0032] In a possible implementation, the memory processing device is located in the kernel space of the electronic device.
[0033] In an embodiment of the present application, based on the interaction of 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 can execute its business smoothly.
[0034] 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 is switched 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 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.
[0035] 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 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.
[0036] 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 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.
[0037] In one possible implementation, the method also 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.
[0038] 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 virtual address driven by a service 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.
[0039] After the memory processing device allocates a shared second physical memory for the application and the hardware device, the further step 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.
[0040] In a possible implementation, the first command includes: an identifier of the first physical memory.
[0041] In a possible implementation manner, the identifier of the first physical memory is: an identifier of a buffer zone used to record information of the first physical memory.
[0042] 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.
[0043] 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.
[0044] 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; the memory processing device modifies the first physical address mapped by the third virtual address in the third page table.
[0045] In a possible implementation, the memory processing device may include a memory recycling driver module, a physical memory management module, and an IOMMU driver, which are used to implement the steps of the memory processing device. The steps performed by the memory recycling driver module, the physical memory management module, and the IOMMU driver can refer to the description in the first aspect.
[0046] 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.
[0047] 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.
[0048] In a fourth aspect, an embodiment of the present application provides an electronic device, including an application program and a memory processing device as in the second aspect.
[0049] 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, and the program code includes instructions; when the processor executes the instructions, the instructions cause the electronic device to execute the method in the first aspect.
[0050] 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.
[0051] In a seventh aspect, an embodiment of the present application provides a computer-readable storage medium, wherein instructions are stored in the computer-readable storage medium, and when the computer-readable storage medium is run on a computer, the computer executes the method in the above-mentioned first aspect.
[0052] 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.
[0053] 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
[0054] Figure 1 A schematic diagram of the structure of an electronic device provided in an embodiment of the present application;
[0055] Figure 2 A schematic diagram of a mapping relationship between a virtual address and a physical address;
[0056] Figure 3A Another structural schematic diagram of an electronic device provided in an embodiment of the present application;
[0057] Figure 3B A schematic diagram of the ION driver provided in an embodiment of the present application allocating shared physical memory for an application and video decoding hardware;
[0058] Figure 3C A schematic diagram of memory mapping of the shared physical memory by the application, codec service, codec driver, and IOMMU driver provided in an embodiment of the present application;
[0059] Figure 3D A schematic diagram of an application and video decoding hardware accessing a shared physical memory provided in an embodiment of the present application;
[0060] Figure 4A A flowchart of an embodiment of a memory processing method provided in an embodiment of the present application;
[0061] Figure 4B A flowchart of another embodiment of the memory processing method provided in the embodiment of the present application;
[0062] Figure 5 A flowchart of another embodiment of the memory processing method provided in the embodiment of the present application;
[0063] Figure 6 A flowchart of another embodiment of the memory processing method provided in the embodiment of the present application;
[0064] Figure 7 A flowchart of another embodiment of the memory processing method provided in the embodiment of the present application;
[0065] Figure 8Another schematic diagram of the structure of an electronic device provided in an embodiment of the present application. DETAILED DESCRIPTION
[0066] For ease of understanding, the following first introduces the relevant terms and concepts involved in the embodiments of the present application:
[0067] The electronic device in the embodiments of the present application may be referred to as user equipment (UE), terminal, etc. For example, the electronic device may be a mobile phone, a portable android device (PAD), a personal digital assistant (PDA), a handheld device with a wireless communication function, a computing device, a vehicle-mounted device or a wearable device, a virtual reality (VR) terminal device, an augmented reality (AR) terminal device, a wireless terminal in industrial control, a wireless terminal in a smart home, etc. The embodiments of the present application do not specifically limit the form of the electronic device.
[0068] Application process: When an electronic device runs an application, it can create an application process, and the application can run in the process, which can be called an application process. An application can be understood as a program in an electronic device to complete one or more specific tasks. In some embodiments, the application can include application software (application, APP) programs, system software programs, and system service programs, etc., which are not limited in the embodiments of the present application. In some embodiments, an application process can be simply referred to as a process.
[0069] Physical memory: refers to the memory that actually exists in an electronic device. For example, physical memory may include random access memory (RAM).
[0070] Physical address: A physical memory may include at least one storage unit, and a physical address may include the address of each storage unit.
[0071] In the early memory allocation mechanism, when an electronic device runs an application, the operating system (OS) can write the executable file of the application and related data into the physical memory. The application runs directly on the physical memory, that is, the memory address accessed by the application is the actual physical address. When a computer runs multiple applications at the same time, 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. Assume that the physical memory size of the electronic device is 128M, and now two applications A and B are running at the same time, A needs to occupy 10M of memory, and B needs to occupy 110M of memory. When allocating physical memory to applications, the electronic device will adopt the following method: first allocate the first 10M of physical memory to A, and then divide 110M from the remaining 118M to B. This allocation method can ensure that both A and B can run, but this simple memory allocation strategy will have the following problems:
[0072] 1. Application processes are not isolated. Since applications directly access physical memory, they can arbitrarily access data in physical memory of other applications, which poses a data security risk. For example, malicious programs can arbitrarily modify the data in physical memory of other applications to achieve the purpose of destruction.
[0073] 2. Low efficiency in the use of physical memory. For example, when both A and B are running, if the electronic device also runs application C, and C requires 20M of physical memory to run, but only 8M of space is available, the electronic device must select one of the running applications, temporarily copy the data of the application to the hard disk, release some space for C, and then write C's data into the memory for operation. In this process, a large amount of data is copied and written, resulting in low efficiency.
[0074] In order 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, and applications can use an indirect address access method to access physical memory. 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 spaces of other processes, so the isolation of processes of different applications and the data security of different applications can be guaranteed. Among them, the virtual address space of the process can be regarded as the virtual space allocated by the operating system to the process.
[0075] 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 a 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 may be understood as the address of the virtual memory, which may be referred to as a virtual address for short.
[0076] However, for electronic devices to execute applications, the applications must run on real physical memory. Therefore, after the operating system allocates an independent virtual address space to each application process, it can establish a mapping between the virtual address and the physical address in the virtual address space. In this way, through the mapping mechanism, when an application accesses a virtual address in the virtual address space, the virtual address can be mapped to the physical address to achieve access to the physical address.
[0077] In order to improve the utilization of physical memory, a paging mechanism can be used to manage physical memory. The basic method of the paging mechanism is: virtual memory is divided into fixed-size units, called pages. The size of the page depends on the operating system. For example, the size of 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.
[0078] The following describes the paging mechanism by introducing the process of application data storage:
[0079] After the application is started, the operating system will first create a virtual address space for the application. During the running process, the application can request physical memory from the operating system, and the physical memory is used to store the data generated by the application during the running process. In response to the request of the application, the operating system can allocate physical memory to the application. Among them, the operating system can allocate free page frames to the application, and map the free page frames to pages to establish a mapping relationship between physical addresses and virtual addresses. The operating system can add the mapping relationship between virtual addresses and physical addresses to the page table.
[0080] The page table can store the mapping relationship between virtual addresses and physical addresses. Each application can correspond to a page table, which stores the mapping relationship between virtual addresses and physical addresses assigned by the operating system to the process of the application. The operating system is responsible for managing and maintaining the page table.
[0081] When an application needs to access data stored in the 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.
[0082] Figure 1 A schematic diagram of the structure of an electronic device provided in an embodiment of the present application. Figure 1 , an electronic device may include: an application, a hardware device, a central processing unit (CPU), a direct memory access (DMA) module, a memory management unit (MMU), an input output memory management unit (IOMMU), and a physical memory.
[0083] Referring to the above description, when the application is running, the operating system ( Figure 1 (not shown) can allocate physical memory for the process of the application, 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 for storing the mapping relationship between the virtual address and the physical address of the process can be called a process page table. In some embodiments, the process page table can also be called a first page table.
[0084] The process page table is stored in the physical memory. When an application accesses a virtual address, the CPU can query the process page table, query the physical address mapped by the virtual address, and then access the physical address. In some embodiments, the CPU queries the physical address mapped by the virtual address, which can be understood as "converting the virtual address to the physical address".
[0085] In some embodiments, the MMU can be used to query the process page table and perform the conversion step of the process's virtual address to the physical address. When an application accesses a certain virtual address, the MMU can query the process page table and convert the virtual address to a physical address, so that the application can access the physical address. In some embodiments, the MMU can be integrated into the CPU or set independently of the CPU. Figure 1 The example in which the MMU is set to be independent of the CPU is taken.
[0086] In some embodiments, the MMU may include a translation lookaside buffer (TLB), which is used to store the mapping relationship between the most recently used virtual addresses and physical addresses. When the MMU performs the step of converting the virtual address to the physical address, the MMU first checks whether there is a corresponding mapping relationship in the TLB. If not, the MMU can query the process page table in the physical memory. This can avoid the MMU from frequently accessing the process page table in the physical memory and improve the efficiency of address conversion.
[0087] In some embodiments, a virtual page number (VPN) is used to represent a page in a virtual address space, and a physical frame number (PFN) is used to represent a page frame in a physical memory. Figure 2 The virtual address allocated by the operating system to the application may include VPN and virtual address offset (VA offset), and the physical address allocated by the operating system to the application includes PFN and physical address offset (PA offset).
[0088] In some embodiments, the mapping relationship between the virtual address and the physical address of the process may include: a mapping relationship between VPN and PFN. Accordingly, the mapping relationship between VPN and PFN may be stored in the process page table. In this example, when the MMU performs the step of converting the virtual address to the physical address, the MMU may search the PFN mapped by the VPN in the virtual address in the process page table, and then add the PFN and the VA offset to obtain the physical address, thereby realizing the conversion of the virtual address to the physical address.
[0089] Hardware devices can be understood as other hardware devices in electronic devices except CPU and physical memory. Hardware devices may include but are not limited to: video decoding hardware, audio player, camera, communication chip, network card, etc.
[0090] Similar to an application, 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 for storing 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.
[0091] 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.
[0092] 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.
[0093] In some embodiments, the IOMMU in the electronic device can be used to query the IOMMU page table and perform the conversion step of the virtual address of the hardware device to the physical address. When the hardware device accesses a certain virtual address, the IOMMU can query the IOMMU page table, convert the virtual address into a physical address, and enable the hardware device to access the physical address.
[0094] 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 segment of physical memory.
[0095] Exemplarily, taking the video decoding scenario as an example, the operating system can allocate the same physical memory 1 for the application and the video decoding hardware, that is, the application and the video decoding hardware can share the 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 the 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. Among them, 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.
[0096] 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, 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.
[0097] 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 the hardware device 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 the application and the hardware device share physical memory.
[0098] It should be understood that the scenario in which the application and the hardware device share physical memory is not limited to the video decoding scenario, and can also be applicable to the following scenario: in the application and the hardware device, one writes data in the shared physical memory, and the other reads data in the shared physical memory. Alternatively, the application and the hardware device both read data or write data in the shared physical memory. It should be understood that the number of times the application and the hardware device write data in the shared physical memory, and the number of times the data is read, are not limited in the embodiments of the present application.
[0099] Currently, in scenarios where applications and hardware devices share physical memory, if the application does not actively release the physical memory, the physical memory will be permanently occupied by the application, and even when the electronic device is running out of memory, the physical memory cannot be reclaimed. As this situation increases, the available memory of electronic devices decreases.
[0100] 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 system layer of the electronic device to play a video. 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 recycle the physical memory segment.
[0101] In order to solve the above problems, the embodiment of the present application provides a memory processing method. In the scenario where the application and the 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 to increase 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.
[0102] Before introducing the memory processing method provided in the embodiment of the present application, the structure of the electronic device is first introduced:
[0103] Figure 3A Another structural diagram of an electronic device provided in an embodiment of the present application. Figure 3A , 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 the scenario where the application and the hardware device share physical memory, because the memory processing method provided in the embodiment of the present application does not involve the actions performed by the hardware device, therefore Figure 3A The hardware devices are not shown.
[0104] In some embodiments, Figure 3A The structure shown does not limit the structure of the electronic device, and the electronic device may also include more or fewer modules. Figure 3A The memory recycling driver module, IOMMU driver, and physical memory management module shown in can be integrated into one, for example, the memory processing device can include the memory recycling driver module, IOMMU driver, and physical memory management module, and the memory processing device can perform the actions of the memory recycling driver module, IOMMU driver, and physical memory management module in the following embodiments. In some embodiments, Figure 3A The memory recovery driver module and IOMMU driver shown in the figure can be integrated into one. The integrated module can be used to execute the actions of the memory recovery driver module and IOMMU driver in the following embodiments. The embodiments of the present application are not limited to this. The following embodiments are explained by taking the memory recovery driver module, IOMMU driver, and physical memory management module as independent settings.
[0105] The electronic device may include a user space and a kernel space. The application is in the user space, and the memory recovery driver module, the IOMMU driver, and the physical memory management module are in the kernel space.
[0106] In some embodiments, the kernel space may be configured with an interface, and the application may call the interface to interact with the module in the kernel space. Exemplarily, the kernel space may provide an input / output control (ioctl) interface for the application process, and the application may call the ioctl interface to interact with the memory recovery driver module.
[0107] 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 the physical memory is released, the application can also actively request the memory recovery driver module to reallocate physical memory to the application.
[0108] The memory recovery 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 the hardware device.
[0109] In some embodiments, the memory recycling driver module is further used to manage the first page table. The IOMMU driver is used to manage the second page table.
[0110] In some embodiments, in a scenario where an application and a hardware device share physical memory, the operating system can allocate shared physical memory to the application and the hardware device, and establish a mapping relationship between the application's virtual address and physical address, as well as a mapping relationship between the hardware device's virtual address and physical address.
[0111] In the embodiment of the present application, in order to facilitate the distinction between the virtual address of the process of the application program mapped by the physical memory and the virtual address of the hardware device mapped by the physical memory. In some embodiments, the virtual address of the process of the application program mapped by the physical memory can be used as the first virtual address, and the virtual address of the hardware device mapped by the physical memory can be used as the second virtual address. In other words, the mapping relationship between the physical address of the physical memory and the first virtual address can be stored in the first page table, and the mapping relationship between the physical address of the physical memory and the second virtual address can be stored in the second page table.
[0112] Among them, the functions of the application program, the memory recovery driver module, the IOMMU driver, and the physical memory management module may be specifically described in the following embodiments.
[0113] 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.
[0114] The DMA-BUF memory management module is used to allocate shared physical memory for applications and hardware devices.
[0115] In order to better understand the memory processing method provided in the embodiment of the present application, here, taking the video decoding scenario as an example, and taking "kernel space using DMA-BUF framework" as an example, the process of the DMA-BUF memory management module allocating the same physical memory for the application and the hardware device (such as video decoding hardware) is briefly described:
[0116] Reference Figure 3B , electronic devices may include user space, kernel space, and hardware layer. Figure 3B , the user space includes applications and codec services, the kernel space includes memory management modules, IOMMU drivers, and codec drivers. The hardware layer includes IOMMU, DMA modules, and video decoding hardware. For example, Figure 3B In the example, the memory management module is ION driver.
[0117] Combine the following Figure 3B The structure of the electronic device shown in the figure first introduces the process of ION driver allocating the same physical memory for the application and video decoding hardware:
[0118] Step 1: The application calls the codec service to enable codec service initialization.
[0119] Among them, the application calls the codec service, which can trigger the initialization of the codec service.
[0120] Step 2: The codec service requests physical memory from the ION driver.
[0121] In some embodiments, the codec service requests physical memory from the ION driver, which can be viewed as an application requesting physical memory from the ION driver.
[0122] 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 indicate allocation of the same physical memory for the application and the video decoding hardware.
[0123] Step 3: The ION driver allocates shared physical memory for the application and the video decoding hardware.
[0124] 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 Figure 3BThe physical memory is represented by a 4K physical page frame.
[0125] In some embodiments, the ION driver may use an ION buffer to describe the information of the physical memory. The ION buffer may be regarded as a data structure, and the ION buffer is used to record the information of the physical memory. After the ION driver stores the ION buffer, it may feed back the identifier of the ION buffer to the application. The identifier of the ION buffer is used to distinguish the ION buffers corresponding to different physical memories. The identifier of the ION buffer may include but is not limited to information such as a number and an ID. In some embodiments, the identifier of the ION buffer may be, for example, a file descriptor (FD).
[0126] In some embodiments, because the ION buffer is used to record the 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.
[0127] Step 4: The application performs memory mapping on the shared physical memory.
[0128] 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 perform memory mapping on the shared physical memory. Exemplarily, 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.
[0129] Step 5: The codec service performs memory mapping on the shared physical memory.
[0130] 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 by the operating system to the codec service. For example, the operating system may allocate a virtual address space to an application program.
[0131] It should be understood that both the application and the codec service are in the 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.
[0132] 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.
[0133] Step 6: The codec service calls the codec driver to enable the codec driver initialization.
[0134] Step 7: The codec driver performs memory mapping on the shared physical memory.
[0135] Similarly, the codec driver can perform memory mapping on 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 by the operating system to the codec driver.
[0136] In some embodiments, the codec driver is in the kernel space, and the virtual address and physical address of the driver or service running in the 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 the physical address of the driver or service running in the 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.
[0137] 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.
[0138] Step 8: The codec driver calls the video decoding hardware to enable the initialization of the video decoding hardware.
[0139] Step 9: The IOMMU driver performs DMA memory mapping on the shared physical memory.
[0140] Referring to the description in step 4, because the shared physical memory is allocated to the application and the video decoding hardware, similarly, the IOMMU driver can perform DMA memory mapping on the shared physical memory. Among them, 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.
[0141] In an embodiment of the present application, the first page table may store a mapping relationship between the first virtual address and the physical address of the physical memory, and a mapping relationship between the fourth virtual address and the physical address of the physical memory, the second page table may store a mapping relationship between the second virtual address and the physical address of the physical memory, and the third page table may store a mapping relationship between the third virtual address of the kernel space and the physical address of the physical memory. When the application accesses the shared physical memory, the MMU may 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 may 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 may be converted into a physical address, and the codec driver in the kernel space may access the physical memory.
[0142] Figure 3C The diagram shows the application, codec service, codec driver, and IOMMU driver mapping the shared physical memory. Figure 3C , the application can perform memory mapping (memorymap) on the physical memory, and map the first virtual address of the application to the physical memory. Among them, mmap is the abbreviation of memory mapping. Similarly, the codec service can perform mmap mapping on the physical memory, and can also realize mapping the fourth virtual address of the codec service to the physical memory.
[0143] The vmap technology implements virtual memory management by allocating unique "virtual pages" to all page table entries of kernel space mapped in virtual memory space. 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.
[0144] 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.
[0145] Figure 3D It shows the process of the application and the video decoding hardware accessing the physical memory after the ION driver allocates the same physical memory to the application and the video decoding hardware, and the application, the codec service, the codec driver, and the IOMMU driver memory-map the shared physical memory:
[0146] Step 1A: The application writes the video data to be decoded into the physical memory.
[0147] In the embodiment of the present application, when the application accesses the physical memory, the MMU may convert the 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.
[0148] Step 2A: The application requests decoding from the codec service.
[0149] Step 3A: The codec service requests decoding from the codec driver.
[0150] Step 4A: The codec driver requests decoding from the video decoding hardware.
[0151] Step 5A: The DMA module reads the video data to be decoded from the physical memory.
[0152] 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.
[0153] Step 6A: The video decoding hardware decodes the video data to be decoded to obtain decoded data.
[0154] Step 7A, the DMA module writes the decoded data into the physical memory.
[0155] Step 8A: the video decoding hardware feeds back to the codec driver that decoding is complete.
[0156] Step 9A: The codec driver feeds back decoding completion to the codec service.
[0157] Step 10A: The codec service feeds back decoding completion to the application.
[0158] Step 11A, the application reads the decoded data from the physical memory.
[0159] In the embodiment of the present application, when the application accesses the physical memory, the MMU can convert the first virtual address into the physical address of the physical memory, so that the application can access the physical memory to read the decoded data. After the application obtains the decoded data, the video can be played according to the decoded data.
[0160] The memory processing method provided by the embodiment 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 described in detail in some embodiments.
[0161] Figure 4A A flowchart of an embodiment of a memory processing method provided in an embodiment of the present application. Figure 4A, the memory processing method provided in the embodiment of the present application may include:
[0162] S401, when the application is switched to the background, the application sends a first command to a memory recovery driving module, where the first command is used to instruct to release a first physical memory.
[0163] When the application is switched to the background, the application can suspend the execution of the target business, and the application temporarily does not need to use the first physical memory corresponding to the target business. The first physical memory can be regarded as the physical memory allocated by the operating system (or the physical memory management module in the operating system) for the target business of the application, and the first physical memory can be used to store data associated with the target business.
[0164] 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 service of the application.
[0165] Exemplarily, taking the application as a video playback application, the video playback application is playing a video, and 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 a video playback business. Accordingly, in a scenario where the video playback application and the video decoding hardware share a 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 may 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 suspend 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.
[0166] In some embodiments, when the application is switched to the background, the application may suspend execution of the target service, and the application may send a first command to the memory recovery driver module after reading data associated with the target service in the first physical memory.
[0167] In some embodiments, the first command may include an identifier of the first physical memory, and 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.
[0168] In some embodiments, when different applications are switched to the background, the target services that the applications suspend can be different. In the embodiment of the present application, when different applications are switched to the background, the specific target services that are suspended can be pre-configured in the application. In this way, when the application is switched to the background, the target service that is suspended can be determined, so that the application can determine the identifier of the first physical memory to send the first command to the memory recovery driver module.
[0169] S402: The memory recovery driver module triggers the physical memory management module to release the first physical memory.
[0170] The physical memory management module is used to manage physical memory.
[0171] In response to the first command, the memory recovery driver module may determine the identifier of the first physical memory, for example, the identifier of the ION buffer used to record the information of the first physical memory, FD1.
[0172] In some embodiments, the memory recovery driver module may 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 may include an identifier of the first physical memory, such as FD1. In response to the second command, the physical memory management module may query the ION buffer corresponding to FD1. Because the ION buffer records the information of the first physical memory, the physical memory management module may 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 The step in which the memory recovery driver module sends the second command to the physical memory management module and the physical memory management module releases the first physical memory is shown.
[0173] 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 services, thereby improving memory utilization.
[0174] In some embodiments, because the target business can be suspended when the application is switched 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 the 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.
[0175] Reference Figure 4B , the memory processing method provided in the embodiment of the present application may include:
[0176] S401A, when the application is switched to the background, the application sends a first command to the memory recovery driving module, where the first command is used to instruct to release the first physical memory.
[0177] S401A may refer to the description in S401.
[0178] 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.
[0179] In the embodiment of the present application, in response to the first command, the memory recovery driver module can determine the identifier of the first physical memory, such as FD1. The memory recovery driver module can access the ION buffer identified as FD1 according to FD1, because the ION buffer records the first virtual address mapping the first physical memory, so the memory recovery driver module can determine the mapping relationship between the first physical memory and the first virtual address.
[0180] The first page table may store a mapping relationship between the first physical address and the first virtual address. The memory recovery driver module may modify the first page table to release the mapping relationship between the first physical address and the first virtual address.
[0181] In some embodiments, after the memory recovery driver module determines the first virtual address, the memory recovery driver module may modify the first physical address mapped by the first virtual address in the first page table. For example, the memory recovery driver module may modify the first physical address mapped by the first virtual address to be empty, or modify the first physical address to be a preset physical address. The value in the preset physical address may be a preset value. Exemplarily, for example, if the preset value is 0, the memory recovery driver module may fill the first physical address mapped by the first virtual address in the first page table with 0.
[0182] 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) mapping 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.
[0183] 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.
[0184] The third page table can store a mapping relationship between the first physical address and the third virtual address. The memory recovery driver module can modify the third page table to release the mapping relationship between the first physical address and the third virtual address.
[0185] In some embodiments, after the memory recovery driver module determines the third virtual address, the memory recovery driver module can modify the first physical address mapped by the third virtual address in the third page table. For example, the memory recovery driver module can modify the first physical address mapped by the third virtual address to be empty, or modify the third physical address to a preset physical address.
[0186] 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.
[0187] It is understandable that, in the embodiment of the present application, taking the video decoding scenario as an example, in the video encoding and decoding process, it is necessary to use the encoding and decoding service in the user space and the encoding and decoding driver in the kernel space. In some embodiments, the encoding and decoding service can be used as a business service, and the encoding and decoding driver can be used as a business driver. It is understandable 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.
[0188] 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, and 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, and 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.
[0189] S403A, the memory recovery driver module triggers the IOMMU driver to release the mapping relationship between the first physical address and the second virtual address.
[0190] In some embodiments, there is no order distinction between S402A and S403A.
[0191] In the embodiment of the present application, in response to the first command, the memory recovery driver module can determine the identifier of the first physical memory, such as FD1. The memory recovery driver module can access the ION buffer identified as FD1 according to 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, so the memory recovery driver module can determine the mapping relationship between the first physical memory and the second virtual address.
[0192] The second page table may store a mapping relationship between a first physical address and a 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, and the third command is 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.
[0193] 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 modifies the first physical address mapped by the second virtual address to empty, or modifies the first physical address to a preset physical address. The value in the preset physical address may be a preset value. Exemplarily, 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.
[0194] 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.
[0195] S404A, the memory recovery driver module triggers the physical memory management module to release the first physical memory.
[0196] S404A may refer to the description in S402.
[0197] Figure 5 A flowchart of another embodiment of the memory processing method provided in the embodiment of the present application. Figure 5 , memory handling methods can include:
[0198] Step 1B: When the application is switched to the background, the memory recovery driver module is instructed to recover the first physical memory through the ioctl interface.
[0199] Step 2B: The memory recovery driver module modifies the first page table to release 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.
[0200] 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.
[0201] Step 4B: The physical memory management module releases the first physical memory.
[0202] Step 1B-Step 4B can refer to Figure 4A and Figure 4B Description in .
[0203] 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, thereby avoiding 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.
[0204] 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. Exemplarily, 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.
[0205] In some embodiments, when the application is switched to the background, the application sends a first command to the memory recovery driver module, and the application may not have finished reading the data associated with the target business in the first physical memory. Therefore, in order to ensure that the application can still access the data associated with the target business after the physical memory management module releases the first physical memory, the physical memory management module can copy the data stored in the first physical memory to the hard disk before releasing the first physical memory.
[0206] When the application is switched to the foreground and needs to continue to access the data associated with the target business, the physical memory management module can reallocate the second physical memory for the target business of the application and copy the data associated with the target business stored in 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.
[0207] 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 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.
[0208] 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 to 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.
[0209] 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.
[0210] The following describes the process of the physical memory management module reallocating the second physical memory for the target business of the application. Figure 6 , the memory processing method provided in the embodiment of the present application may include:
[0211] S601, when the application starts to switch from the background to the foreground, the application sends a fourth command to the memory recovery driver module, where the fourth command is used to instruct to allocate physical memory for the application.
[0212] After the application is switched to the foreground, the application can continue to execute the target business, and the application needs to continue to access the data associated with the target business, so the operating system needs to reallocate the second physical memory for the target business of the application, and 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 recovery driver module, and the fourth command is used to instruct to allocate physical memory for the application.
[0213] 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.
[0214] In some embodiments, the fourth command may include an identifier of the first physical memory, such as FD1.
[0215] S602: The memory recovery driver module triggers the physical memory management module to allocate a shared second physical memory for the application program and the hardware device.
[0216] In response to the fourth command, the memory recovery driver module can access the ION buffer corresponding to FD1 to determine the size of the first physical memory and other information. The memory recovery driver module can trigger the physical memory management module to allocate a second physical memory of the same size to the application.
[0217] In some embodiments, the memory recovery 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 shared physical memory for the application and the hardware device. In some embodiments, the fifth command may include information such as the size of the second physical memory.
[0218] S603: The physical memory management module feeds back the second physical address of the second physical memory to the memory recovery driving module.
[0219] In response to the fifth command, the physical memory management module may allocate a shared second physical memory for the application and the hardware device, and the address of the second physical memory may be referred to as a second physical address. The physical memory management module may feed back the second physical address to the memory recycling driver module.
[0220] 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.
[0221] The memory recycling driver module can 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 can modify the physical address mapped by the first virtual address in the first page table, for example, the memory recycling driver module modifies the preset physical address mapped by the first virtual address to the second physical address.
[0222] 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.
[0223] Similarly, the memory recovery 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 recovery driver module can modify the physical address mapped by the third virtual address in the third page table, for example, the memory recovery driver module modifies the preset physical address mapped by the third virtual address to the second physical address.
[0224] S605: The memory recovery driver module triggers the IOMMU driver to establish a mapping relationship between the second virtual address and the second physical address.
[0225] In some embodiments, there is no order distinction between S604 and S605.
[0226] Similarly, the memory recovery 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 recovery driver module can send a sixth command to the IOMMU driver, and the sixth command is used to instruct the IOMMU driver to establish a mapping relationship between the second virtual address and the second physical address. Wherein, the sixth command may include the second physical address.
[0227] 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.
[0228] 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, convert the first virtual address into the second physical address, and enable 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, convert the second virtual address into the second physical address, and enable the hardware device to access the second physical memory.
[0229] Figure 7 Another flowchart of the memory processing method provided in the embodiment of the present application is shown in FIG. Figure 7 , memory handling methods can include:
[0230] Step 1C: When the application starts to switch from the background to the foreground, the memory recovery driver module is instructed to reallocate physical memory to the application through the ioctl interface.
[0231] Step 2C: The ION driver reallocates the shared second physical memory for the application and the hardware device, and feeds back the second physical address of the second physical memory to the memory recovery driver module.
[0232] Step 3C: the memory recovery 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 recovery driver module modifies the third page table to establish a mapping relationship between the third virtual address and the second physical address.
[0233] 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.
[0234] Steps 1C to 4C can refer to Figure 6 Description in .
[0235] 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 the 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, and the IOMMU driver can modify the second page table to establish a mapping relationship between the second virtual address and the second physical address, so that 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.
[0236] 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 relevant laws, regulations and standards of relevant countries and regions, and provide corresponding operation entrances for users to choose to authorize or refuse.
[0237] In one embodiment, the present application also provides an electronic device, which can perform the steps in the above embodiments. Figure 8, the electronic device may include: a processor 801 (such as 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 memory. The memory 802 may store various instructions for completing various processing functions and implementing the method steps of the present application.
[0238] Optionally, the electronic device involved in the present application may further include: a power supply 803, a communication bus 804 and a communication port 805. The above-mentioned communication port 805 is used to realize the connection and communication between the electronic device and other peripherals. In the embodiment of the present application, the memory 802 is used to store computer executable program code, and the program code includes instructions; when the processor 801 executes the instruction, the instruction causes the processor 801 of the electronic device to perform the action in the above-mentioned method embodiment, and its implementation principle and technical effect are similar, which will not be repeated here.
[0239] 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), or one or more digital signal processors (DSPs), or one or more field programmable gate arrays (FPGAs). For another example, when a module above is implemented in the form of a processing element scheduling program code, the processing element may be a general-purpose processor, such as a central processing unit (CPU) or other processors that can call program codes, 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).
[0240] In the above embodiments, it can be implemented in whole or in part by software, hardware, firmware or any combination thereof. When implemented using software, 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 loading and executing computer program instructions 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 devices. 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 site, a computer, a server or a data center by wired (e.g., coaxial cable, optical fiber, digital subscriber line (DSL)) or wireless (e.g., infrared, wireless, microwave, etc.) mode to another website site, computer, server or data center. The computer-readable storage medium can be any available medium that a computer can access or a data storage device such as a server, a data center, etc. that contains one or more available media integrated. 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 hard disk Solid State Disk (SSD)), etc.
[0241] 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 objects associated before and after are in an "or" relationship. "At least one of the following" or its similar expressions refers to any combination of these items, including any combination of single items 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.
[0242] It should be noted that the "at..." in the embodiments of the present application can be the instant when a certain situation occurs, or can be a period of time after a certain situation occurs, and the embodiments of the present application do not specifically limit this. It is understood that the various numerical numbers involved in the embodiments of the present application are only distinguished for the convenience of description and are not used to limit the scope of the embodiments of the present application.
[0243] 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
Cited By
Method for releasing DMA buffer area, electronic equipment, readable storage medium and program product
CN120670134A
Memory processing method, electronic device and readable storage medium
WO2025044567A1
DMA buffer release method, electronic device, readable storage medium and program product
WO2026184113A1