A memory management method, electronic equipment and storage medium
Patent Information
- Application Number
- CN202510543902.8
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2025-04-27
- Publication Date
- 2026-10-27
AI Technical Summary
[0003]然而,基于dma-buf管理者释放内存的方式,完全依赖dma-buf管理者对于dma-buf对象的主动管理,管理不当会使得已经不再使用的dma-buf对象没有被及时释放;同时,目前dma-buf框架中,没有针对dma-buf对象的回收功能,这些都导致了系统中存在很多未被及时释放的dma-buf对象,造成极大的内存浪费
[0014]结合第一方面和上述实现方式,在第一方面的某些实现方式中,上述方法还包括:在回收目标dma-buf对象占用的内存之后,将目标dma-buf对象的状态设置为已回收状态。这样,回收后的dma-buf对象可以被重新使用,从而避免出现内存浪费的问题。
Smart Images

Figure CN122985369A_ABST
Abstract
Description
Technical Field
[0001] This application relates to the field of electronic technology, and in particular to a memory management method, electronic device, and storage medium. Background Technology
[0002] Direct Memory Access Buffer (dma-buf) is a framework for sharing hardware access buffers across multiple device drivers and subsystems. Currently, in the dma-buf framework, the data structures of a dma-buf object and the memory it occupies are only released when the dma-buf manager actively calls the dma-buf release function.
[0003] However, the way the dma-buf manager releases memory relies entirely on the active management of dma-buf objects by the dma-buf manager. Improper management can lead to dma-buf objects that are no longer in use not being released in a timely manner. At the same time, the current dma-buf framework does not have a function for recycling dma-buf objects. All of these factors result in a large number of dma-buf objects that are not released in a timely manner in the system, causing a great deal of memory waste. Summary of the Invention
[0004] This application provides a memory management method, electronic device, and storage medium, which can reduce memory waste, alleviate memory pressure, and improve system smoothness. The technical solution is as follows:
[0005] In a first aspect, embodiments of this application provide a memory management method applied to an electronic device. The electronic device creates at least one dma-buf object, which has different states. In this method, when the electronic device meets the triggering conditions for the memory reclamation process, it determines whether the target dma-buf object meets the target conditions. The target conditions are that the current state of the target dma-buf object is a reclaimable state. If the target dma-buf object meets the target conditions, the memory occupied by the target dma-buf object is reclaimed.
[0006] Based on the above technical solution, dma-buf objects have different states. When the current state of a dma-buf object is a reclaimable state, the dma-buf object can be reclaimed. This application embodiment provides a reclamation mechanism so that dma-buf objects that are not released in a timely manner can be reclaimed. After reclamation, the memory occupied by the dma-buf object can be reused, thereby reducing memory pressure, improving memory utilization, and enhancing system smoothness.
[0007] In conjunction with the first aspect, in some implementations of the first aspect, the target condition is that the duration for which the target dma-buf object is currently in a reclaimable state exceeds a preset threshold. This prevents dma-buf objects still in use from being reclaimed, ensuring that users of dma-buf can use them normally.
[0008] Combining the first aspect and the aforementioned implementation methods, in some implementations of the first aspect, the data structure of the target DMA-BUF object includes a first structure, which is used to record the current state of the target DMA-BUF object. Thus, when the memory reclamation process triggers, the current state of the target DMA-BUF object can be obtained from the first structure.
[0009] Specifically, if the target dma-buf object is not used by a consumer, the target dma-buf object is in a state that can be reclaimed; if the target dma-buf object is used by a consumer, the target dma-buf object is in a state that cannot be reclaimed; and after the target dma-buf object is reclaimed, the target dma-buf object is in a state that has been reclaimed.
[0010] Combining the first aspect and the above implementation methods, in some implementations of the first aspect, the first structure also includes a hijacking function, which is used to maintain the state of the target DMA-BUF object. In this way, the state of the target DMA-BUF object can be updated.
[0011] Combining the first aspect and the above implementation methods, in some implementations of the first aspect, the method further includes: after detecting an access operation to the target DMA-buf object, executing a hijacking function; accessing the target DMA-buf object if its state is either recyclable or non-recyclable; and after the access is completed, setting the target DMA-buf object's state to recyclable if it is recyclable. In this way, the external device's access operation to the target DMA-buf object is hijacked by the hijacking function. The hijacking function first checks the state of the target DMA-buf object before performing the access. After the access is completed, the hijacking function updates the state of the target DMA-buf object. This achieves the maintenance of the DMA-buf object's state.
[0012] In combination with the first aspect and the above implementation methods, in some implementation methods of the first aspect, the above method further includes: rereading the target dma-buf object when the target dma-buf object is in a reclaimed state; and after rereading the target dma-buf object, setting the target dma-buf object to a non-reclaimable state.
[0013] Combining the first aspect and the aforementioned implementation methods, in some implementations of the first aspect, reclaiming the memory occupied by the target DMA-BUF object includes: allocating a data storage address for the target DMA-BUF object in the backend device; and storing the data in the memory occupied by the target DMA-BUF object to the data storage address. This way, upon re-reading, the reclaimed data can be retrieved from the data storage address.
[0014] Combining the first aspect and the above implementation methods, in some implementations of the first aspect, the method further includes: after reclaiming the memory occupied by the target dma-buf object, setting the state of the target dma-buf object to a reclaimed state. In this way, the reclaimed dma-buf object can be reused, thereby avoiding the problem of memory waste.
[0015] In a second aspect, embodiments of this application provide an electronic device, including: one or more processors; one or more memories; the memories storing one or more programs, which, when executed by the processors, cause the electronic device to perform any of the possible methods described in the first aspect.
[0016] Thirdly, embodiments of this application provide an apparatus included in an electronic device, which has the function of implementing the behaviors of the electronic device in the above aspects and possible implementations thereof. The functions can be implemented by hardware or by hardware executing corresponding software. The hardware or software includes one or more modules or units corresponding to the above functions. For example, a display module or unit, a detection module or unit, a processing module or unit, etc.
[0017] Fourthly, embodiments of this application provide a computer-readable storage medium storing instructions that, when executed on a computer, cause the computer to perform the method described in the first aspect above.
[0018] Fifthly, embodiments of this application provide a computer program product containing instructions that, when run on a computer, cause the computer to perform the method described in the first aspect above.
[0019] The technical effects achieved by the second, third, fourth, and fifth aspects mentioned above are similar to those achieved by the corresponding technical means in the first aspect mentioned above, and will not be repeated here. Attached Figure Description
[0020] Figure 1 A schematic diagram of the hardware structure of an electronic device provided in an embodiment of this application is shown;
[0021] Figure 2A schematic diagram of the software structure of an electronic device provided in an embodiment of this application is shown;
[0022] Figure 3 A schematic diagram of a state change provided in an embodiment of this application is shown;
[0023] Figure 4 This illustration shows a flowchart of accessing a dma-buf object according to an embodiment of this application;
[0024] Figure 5 This illustration shows another flowchart for accessing a dma-buf object provided in an embodiment of this application;
[0025] Figure 6 This illustration shows a flowchart of creating a dma-buf object according to an embodiment of this application;
[0026] Figure 7 This illustration shows another flowchart for creating a dma-buf object according to an embodiment of this application;
[0027] Figure 8 This illustration shows another flowchart for accessing a dma-buf object provided in an embodiment of this application;
[0028] Figure 9 This illustration shows a flowchart of a process for recycling dma-buf objects according to an embodiment of this application;
[0029] Figure 10 This illustration shows a flowchart of a process for re-reading a dma-buf object according to an embodiment of this application;
[0030] Figure 11 A schematic diagram of the structure of an apparatus provided in an embodiment of this application;
[0031] Figure 12 This is a schematic diagram of the structure of a chip provided in an embodiment of this application. Detailed Implementation
[0032] To make the objectives, technical solutions, and advantages of this application clearer, the embodiments of this application will be further described in detail below with reference to the accompanying drawings. Hereinafter, the terms "first" and "second" are used for descriptive purposes only and should not be construed as indicating or implying relative importance or implicitly specifying the number of indicated technical features. Thus, a feature defined as "first" or "second" may explicitly or implicitly include one or more of that feature. In the description of the embodiments of this application, unless otherwise stated, "a plurality of" means two or more.
[0033] To facilitate understanding of the technical solutions in the embodiments of this application, the terms involved in the embodiments of this application will be briefly explained below.
[0034] 1. Direct Memory Access (DMA): A technology used for high-speed data transfer between memory and external devices, and between memory modules in a computer system. External devices include graphics processors, cameras, monitors, image signal processors, etc.
[0035] In traditional device-to-device communication, it's sometimes necessary to read or transfer data from external devices. Each data transfer requires processing by the Central Processing Unit (CPU), increasing the CPU's workload and potentially leading to inefficient data transfer. The DMA (Direct Memory Access) mechanism allows external devices to directly read and write to shared memory regions, enabling one device to share its contents with another without copying the data. This eliminates CPU intervention in data transfer, avoiding unnecessary CPU intervention, reducing the CPU load, and significantly improving system efficiency.
[0036] 2. DMA Buffer (Direct Memory Access Buffer, dma-buf): A framework for sharing hardware access buffers among multiple device drivers and subsystems. This framework is primarily used for direct memory access, allowing different devices and drivers to share the same memory buffer, thereby improving resource utilization.
[0037] Besides direct memory access, DMA caches can also be used for asynchronous operation synchronization, providing a mechanism to indicate when asynchronous hardware operations complete, ensuring data consistency and integrity. They can also be used for user-space access, exposing DMA-buf objects to user space via file descriptors, allowing user programs to directly access shared buffers. Furthermore, in the Direct Rendering Manager (DRM) subsystem, DMA-buf is widely used to exchange buffers between processes, contexts, and library APIs.
[0038] 3. DMA-BUF object: A standardized data structure in the kernel representing DMA-BUF memory, encapsulating metadata about physical memory (such as size) and a set of operation functions. The memory encapsulated by a DMA-BUF object is simply referred to as DMA-BUF memory.
[0039] 4. DMA-BUF Memory Reclamation: The physical memory used by DMA-BUF objects is reclaimed and released to the kernel's memory management subsystem. Data in DMA-BUF memory is properly preserved, including but not limited to compression, writing to block devices, and writing to files. However, the data structures of the DMA-BUF object are not released, recorded, or marked as released. For the DMA-BUF subsystem, the DMA-BUF object still exists.
[0040] 5. dma-buf release: The entire data structure of the dma-buf object (including the memory it uses) is completely released.
[0041] 6. DMA-buf Producer (referred to as Producer): This includes the DMA memory management module (DMAHeap) and the process responsible for requesting new DMA-buf objects from the DMAHeap. The former is responsible for creating and managing the shared DMA cache devices or drivers, and for requesting memory and registering it as a DMA-buf object for use by other modules. The latter is responsible for sharing the requested DMA-buf objects with other processes or driver devices that use the DMA-buf object.
[0042] 7. DMA-BUF Consumer (referred to as Consumer): A device or driver (such as CPU, display controller, encoder) that imports and uses DMA-BUF objects provided by the producer. Consumers directly access memory through mapping and DMA operations, achieving zero-copy data transfer. DMA-BUF consumers can also be called DMA-BUF users.
[0043] Currently, the release of DMA-buf memory relies entirely on the active management of DMA-buf objects by the DMA-buf manager (producer). However, when upper-layer applications use the system improperly, some DMA-buf objects that should have been released promptly are not. For example, after one hour of phone use, the amount of idle DMA-buf is approximately 500 to 600 MB (this may vary depending on the system version and the amount of DMA-buf used by upper-layer applications). This portion of DMA-buf occupies memory but is not actually used by any consumers. Since DMA-buf memory belongs to neither file pages nor anonymous pages, existing management mechanisms for file pages and anonymous pages cannot reclaim this portion of DMA-buf memory. Furthermore, because the kernel does not provide registration for DMA-buf shrinker callback functions, the kernel cannot reclaim this portion of DMA-buf memory during memory reclamation.
[0044] In summary, the current way dma-buf managers release memory can lead to some dma-buf objects not being released or reclaimed in a timely manner, resulting in memory waste and affecting memory utilization.
[0045] In view of this, embodiments of this application provide a memory management method that sets different states for dma-buf objects based on how consumers use them. Based on the states of the dma-buf objects, dynamic reclamation, access, reading, and release of dma-buf memory are achieved. The dma-buf reclamation mechanism provided by embodiments of this application can improve memory utilization, reduce memory pressure, and improve system smoothness, with particularly significant effects in memory-constrained scenarios.
[0046] The memory management method provided in this application can be applied to mobile phones, smart screens, tablets, wearable electronic devices, in-vehicle electronic devices, augmented reality (AR) devices, virtual reality (VR) devices, laptops, ultra-mobile personal computers (UMPCs), netbooks, personal digital assistants (PDAs), projectors, etc. This application does not limit the type of electronic device.
[0047] Figure 1 A schematic diagram of the hardware structure of an electronic device 100 is shown.
[0048] Electronic device 100 may include processor 110, external memory interface 120, internal memory 121, universal serial bus (USB) interface 130, charging management module 140, power management module 141, battery 142, antenna 1, antenna 2, mobile communication module 150, wireless communication module 160, audio module 170, speaker 170A, receiver 170B, microphone 170C, headphone jack 170D, sensor module 180, button 190, motor 191, indicator 192, camera 193, display screen 194, subscriber identity module (SIM) card interface 195, etc. The sensor module 180 may include a pressure sensor 180A, a gyroscope sensor 180B, a barometric pressure sensor 180C, a magnetic sensor 180D, an accelerometer sensor 180E, a distance sensor 180F, a proximity sensor 180G, a fingerprint sensor 180H, a temperature sensor 180J, a touch sensor 180K, an ambient light sensor 180L, a bone conduction sensor 180M, etc.
[0049] Processor 110 may include one or more processing units, such as application processors (APs), modem processors, graphics processing units (GPUs), image signal processors (ISPs), controllers, video codecs, digital signal processors (DSPs), baseband processors, neural network processing units (NPUs), etc. These different processing units may be independent devices or integrated into one or more processors.
[0050] The controller can generate operation control signals based on the instruction opcode and timing signals to complete the control of instruction fetching and execution.
[0051] The processor 110 may also include a memory for storing instructions and data. In some embodiments, the memory in the processor 110 is a cache memory. This memory can store instructions or data that the processor 110 has just used or that are used repeatedly. If the processor 110 needs to use the instruction or data again, it can retrieve it directly from the memory. This avoids repeated accesses, reduces the waiting time of the processor 110, and thus improves the efficiency of the system.
[0052] In some embodiments, the processor 110 may include one or more interfaces. Interfaces may include an inter-integrated circuit (I2C) interface, an inter-integrated circuit sound (I2S) interface, a pulse code modulation (PCM) interface, a universal asynchronous receiver / transmitter (UART) interface, a mobile industry processor interface (MIPI), a general-purpose input / output (GPIO) interface, a subscriber identity module (SIM) interface, a universal serial bus (USB) interface, etc.
[0053] The charging management module 140 receives charging input from a charger, which can be a wireless charger or a wired charger. While charging the battery 142, the charging management module 140 can also supply power to electronic devices via the power management module 141.
[0054] The power management module 141 is used to connect the battery 142, the charging management module 140, and the processor 110. The power management module 141 receives input from the battery 142 and / or the charging management module 140 to power the processor 110, internal memory 121, display screen 194, camera 193, wireless communication module 160, etc.
[0055] The wireless communication function of electronic device 100 can be realized through antenna 1, antenna 2, mobile communication module 150, wireless communication module 160, modem processor, baseband processor, etc.
[0056] Antenna 1 and antenna 2 are used to transmit and receive electromagnetic wave signals. Each antenna in electronic device 100 can be used to cover one or more communication frequency bands. Different antennas can also be reused to improve antenna utilization.
[0057] The mobile communication module 150 can provide solutions for wireless communication, including 2G, 3G, 4G, and 5G, for use on electronic devices 100.
[0058] The wireless communication module 160 can provide solutions for wireless communication applications on the electronic device 100, including wireless local area network (WLAN), Bluetooth (BT), global navigation satellite system (GNSS), frequency modulation (FM), near field communication (NFC), and infrared (IR).
[0059] In some embodiments, antenna 1 of electronic device 100 is coupled to mobile communication module 150, and antenna 2 is coupled to wireless communication module 160, enabling electronic device 100 to communicate with networks and other devices via wireless communication technology. Wireless communication technology may include Global System for Mobile Communications (GSM), General Packet Radio Service (GPRS), Code Division Multiple Access (CDMA), Wideband Code Division Multiple Access (WCDMA), Time Division Code Division Multiple Access (TD-SCDMA), Long Term Evolution (LTE), BT, GNSS, WLAN, NFC, FM, IR, and other technologies.
[0060] Electronic device 100 implements display functions through a GPU, display screen 194, application processor, etc. The GPU is a microprocessor for image processing, connected to the display screen 194 and the application processor. The GPU is used to perform mathematical and geometric calculations and for graphics rendering. Processor 110 may include one or more GPUs, which execute program instructions to generate or modify display information.
[0061] The display screen 194 is used to display images, videos, etc. In some embodiments, the electronic device 100 may include one or N display screens 194, where N is a positive integer greater than 1.
[0062] Electronic device 100 can perform shooting functions through ISP, camera 193, video codec, GPU, display 194, application processor, etc.
[0063] The ISP is used to process data fed back from the camera. For example, when taking a picture, the shutter is opened, and light is transmitted through the lens to the camera's image sensor. The light signal is converted into an electrical signal, and the camera's image sensor transmits the electrical signal to the ISP for processing, transforming it into an image visible to the naked eye.
[0064] Camera 193 is used to capture still images or videos. An object is projected onto a photosensitive element by generating an optical image through the lens. The photosensitive element can be a charge-coupled device (CCD) or a complementary metal-oxide-semiconductor (CMOS) phototransistor. The photosensitive element converts the light signal into an electrical signal, which is then passed to an ISP for conversion into a digital image signal. The ISP outputs the digital image signal to a DSP for processing. The DSP converts the digital image signal into image signals in standard RGB, YUV, or other formats. In some embodiments, the electronic device 100 may include one or N cameras 193, where N is a positive integer greater than 1.
[0065] A digital signal processor is used to process digital signals. In addition to processing digital image signals, it can also process other digital signals.
[0066] Video codecs are used to compress or decompress digital video. Electronic device 100 may support one or more video codecs. Thus, electronic device 100 can play or record videos in various encoding formats.
[0067] An NPU (Neural Processing Unit) is a computational processor that, by borrowing the structure of biological neural networks—such as the transmission patterns between neurons in the human brain—processes input information rapidly and can continuously learn on its own. NPUs enable intelligent cognitive applications in electronic devices, such as image recognition, facial recognition, speech recognition, and text understanding.
[0068] The external storage interface 120 can be used to connect an external memory card, such as a Micro SD card, to expand the storage capacity of the electronic device 100. The external memory card communicates with the processor 110 through the external storage interface 120 to perform data storage functions. For example, music, video, and other files can be saved on the external memory card.
[0069] Internal memory 121 can be used to store executable program code, which includes instructions. Internal memory 121 may include a program storage area and a data storage area. The program storage area may store the operating system, application programs, etc. The data storage area may store data created during the use of electronic device 100 (e.g., audio data, phone book, etc.). In addition, internal memory 121 may include high-speed random access memory, and may also include non-volatile memory, such as at least one disk storage device, flash memory device, universal flash storage (UFS), etc. Processor 110 executes various functional applications and data processing of electronic device 100 by running instructions stored in internal memory 121 and instructions stored in memory located in the processor.
[0070] Electronic device 100 can implement audio functions such as music playback and recording through audio module 170, speaker 170A, receiver 170B, microphone 170C, headphone jack 170D, and application processor.
[0071] The audio module 170 is used to convert digital audio information into analog audio signals for output, and also to convert analog audio input into digital audio signals. The audio module 170 can also be used for encoding and decoding audio signals. In some embodiments, the audio module 170 may be located in the processor 110, or some functional modules of the audio module 170 may be located in the processor 110.
[0072] Buttons 190 include a power button, volume buttons, etc. Buttons 190 can be mechanical buttons or touch-sensitive buttons. The SIM card interface 195 is used to connect a SIM card. The SIM card can be inserted into or removed from the SIM card interface 195 to achieve contact and separation with the electronic device 100. The electronic device 100 can support one or N SIM card interfaces, where N is a positive integer greater than 1.
[0073] It is understood that the structures illustrated in the embodiments of this application do not constitute a specific limitation on the electronic device 100. In other embodiments of this application, the electronic device 100 may include more or fewer components than illustrated, or combine some components, or split some components, or have different component arrangements. The illustrated components may be implemented in hardware, software, or a combination of software and hardware.
[0074] The software system of electronic device 100 can adopt a layered architecture, event-driven architecture, microkernel architecture, microservice architecture, cloud architecture, etc. This application embodiment uses the layered architecture Android system as an example to illustrate the software structure of electronic device 100.
[0075] Figure 2 This is a software structure block diagram of an electronic device 100 according to an embodiment of this application.
[0076] A layered architecture divides software into several layers, each with a clear role and function. Layers communicate with each other through software interfaces. In some embodiments, the Android system is divided into four layers, from top to bottom: the application layer, the application framework layer, the Android runtime and system libraries, and the kernel layer.
[0077] The application layer can include a series of application packages. For example... Figure 2 As shown, the application package may include applications such as camera, gallery, calendar, call, map, navigation, WLAN, Bluetooth, music, video, and SMS.
[0078] The application framework layer provides application programming interfaces (APIs) and a programming framework for applications in the application layer. The application framework layer includes some predefined functions.
[0079] like Figure 2 As shown, the application framework layer may include a window manager, content provider, view system, phone manager, resource manager, notification manager, etc.
[0080] The window manager is used to manage windowed applications. It can retrieve screen size, determine the presence of a status bar, lock the screen, and capture screenshots, among other things.
[0081] Content providers are used to store and retrieve data, making that data accessible to applications. This data can include videos, images, audio, phone calls made and received, browsing history and bookmarks, phone books, and more.
[0082] A view system includes visual controls, such as controls for displaying text and controls for displaying images. View systems can be used to build applications. A display interface can consist of one or more views.
[0083] The phone manager is used to provide communication functions for electronic device 100. For example, it manages call status (including connection and disconnection).
[0084] The file explorer provides applications with various resources, such as localized strings, icons, images, layout files, video files, etc.
[0085] The notification manager allows applications to display notification information in the status bar.
[0086] The Android Runtime consists of core libraries and a virtual machine. The Android runtime is responsible for the scheduling and management of the Android system.
[0087] The core library consists of two parts: one part is the functionalities that need to be called by the Java language, and the other part is the Android core library.
[0088] The application layer and application framework layer run in a virtual machine. The virtual machine executes the Java files of the application layer and application framework layer as binary files. The virtual machine is used to perform functions such as object lifecycle management, stack management, thread management, security and exception management, and garbage collection.
[0089] The system library can include multiple functional modules, such as: surface manager, media libraries, 3D graphics processing libraries (e.g., OpenGL ES), 2D graphics engines (e.g., SGL), etc.
[0090] The Surface Manager is used to manage the display subsystem and to provide the blending of 2D and 3D layers for multiple applications.
[0091] The media library supports playback and recording of various common audio and video formats, while also being compatible with reading and storing still image files. The media library supports multiple audio and video encoding formats, such as MPEG4, H.264, MP3, AAC, AMR, JPG, and PNG.
[0092] The 3D graphics processing library is used to implement 3D graphics drawing, image rendering, compositing, layer processing, etc.
[0093] A 2D graphics engine is a graphics engine for 2D drawing.
[0094] The kernel layer is the layer between hardware and software. The kernel layer can contain the DMA-BUF subsystem, ISP driver, GPU driver, camera driver, display driver, etc.
[0095] The DMA-BUF subsystem can include a DMA memory management module (dmaheap) and dma-buf objects.
[0096] A DMA-buf object can be created by the DMA memory management module executing the DMA-buf allocation function. A DMA-buf object can be described by a struct dma_buf structure. The structure of a DMA-buf object typically includes two parts: one part is the actual physical memory required by the DMA-buf object, allocated and managed by dmaheap, and the other part is the defined callback function dma_buf_ops for accessing the DMA-buf object.
[0097] The `dma_buf_ops` structure is a collection of function pointers, with implementations provided through callback functions. Users of dma-buf can assign specific implementation functions to the function pointers in `dma_buf_ops` to perform various operations on the dma-buf object.
[0098] The specific implementation of dma-buf objects varies across different platforms. The actual physical memory management is defined by dmaheap. For example, in some implementations, the actual physical memory required by dma-buf objects can be managed using a linked list (sg_table), which records the physical address, length, and offset of memory blocks.
[0099] This application embodiment adds a first structure for maintaining the state of dma-buf objects. This first structure can include two parts: one part is a state machine data structure used to indicate the current state of the dma-buf object and manage various information related to the reclamation of the corresponding dma-buf object; the other part is a hijacking function used to modify the data structure representing the state machine. This first structure can be named dma-buf_oc.
[0100] The `dma-buf_oc` data structure can include, but is not limited to, the following: whether the current `dma-buf` object can be garbage collected, whether the current `dma-buf` object has already been garbage collected, and the data storage address of the garbage collected `dma-buf` object. In some implementations, the `dma-buf_oc` structure can include multiple parameters, each recording different information. For example, a status parameter records the state of the `dma-buf` object; a time parameter records the duration the `dma-buf` object is in a certain state to determine its popularity or obscurity.
[0101] In this embodiment, a dma-buf object can have different states, including: a non-reclaimable state (busy), a reclaimable state (idle), a reclaimed state (offline), an intermediate reclaiming state (going_offline), and an intermediate reading state (going_online). Through these different states, operations such as reclaiming and reading from the dma-buf object can be implemented.
[0102] The "busy" state indicates that another external device (i.e., a consumer) is currently using the dma-buf object, and the reference count (attach_cnt) of the dma-buf object is not 0. The reference count represents the currently executing function operating on the dma-buf object, such as a device using the dma-buf object, a vmap operation on the dma-buf object, or an mmap mapping of the dma-buf object. When any of these operations are present, the reference count of the dma-buf object will not be 0. Note that the reference count here only indicates whether the dma-buf object can be garbage collected, and does not solely represent the execution status of the dma-buf's operations.
[0103] The "idle" state indicates that no other external devices are currently using the dma-buf object, and its reference count is 0. In the idle state, the dma-buf object is eligible for garbage collection but has not yet been collected, and can be collected at any time. Note that a reference count of 0 does not necessarily mean that no dma-buf operations are currently executing. For example, even with only mmap mappings present, developers can register relevant page fault mechanisms to reclaim the dma-buf object, thus enabling garbage collection even when mmap mappings exist. Therefore, a reference count of 0 merely indicates that the dma-buf object is currently in a reclaimable state. After the dma-buf object is reclaimed, it can still be accessed correctly by the driver or CPU through other means, and the kernel will not experience any exceptions.
[0104] The busy and idle states can be combined to represent the online state, indicating that the current dma-buf object has not been garbage collected.
[0105] The "Reclaimed Complete (offline)" status indicates that the current dma-buf object has been reclaimed, and the contents of the corresponding dma-buf object in memory have been stored on the backend device. The "Reclaimed Complete (offline)" status can also be described as an offline state.
[0106] The intermediate state of recycling (going_offline) indicates that dma-buf objects are currently being recycled.
[0107] The intermediate state of the read-in process (going_online) indicates that the dma-buf object has been garbage collected and needs to be read back into memory to create a usable dma-buf object, but the read-in operation is not yet complete.
[0108] It should be noted that dma-buf objects can have more or fewer states. For example, they can also include the intermediate state `going_idle` to ensure state synchronization. Furthermore, intermediate states that are recycled or read in can be omitted, directly completing the transition from a state that has not been recycled to one that has been recycled.
[0109] Furthermore, it should be understood that any state with the same meaning as the above-mentioned state can be summarized as a state in the embodiments of this application. The embodiments of this application only use the above names for explanation and description, and do not limit the names of the states in any way.
[0110] When an external device calls a function in `dma_buf_ops` to perform an operation on a `dma-buf` object, the operation will be intercepted by a hijacking function. This hijacking function will update the state of the `dma-buf` object, and the updated state will be recorded in the state machine's data structure. The following section explains the state machine changes before and after the `dma-buf` object is recycled, using the aforementioned states as examples.
[0111] Reference Figure 3 A block of memory begins in its initial state (allocate), such as... Figure 3 As shown in ①, when a dma-buf object is created, if no consumer is currently using the dma-buf object, the reference count in dma-buf_oc is set to 0, and the state machine is set to idle state. Add this dma-buf object to the online state management structure.
[0112] It should be noted that the reference count mentioned here is a count added to the state machine in this embodiment of the application, used to indicate whether the dma-buf object can be garbage collected. Specifically, when no consumer is using the dma-buf object, the state machine's reference count is 0, indicating that the dma-buf object can be garbage collected. When a consumer is using the dma-buf object, the reference count is not 0, indicating that it cannot be garbage collected. This reference count is different from the reference count of the dma-buf manager (producer) for the dma-buf object. In the idle state, the reference count of the dma-buf manager (producer) for the dma-buf object may be 0 or not. In fact, the dma-buf object can be considered to be in the idle state if the following conditions are met: no driver is currently using the dma-buf object (e.g., the corresponding `attachements` linked list is empty), no vmap maps the dma-buf object (e.g., `vmap_cnt` is 0), and no mmap maps the dma-buf object. Alternatively, even if the above conditions are not met, the current dma-buf object can be reclaimed through a mechanism, and subsequent accesses can recognize that the current dma-buf object has been reclaimed and can be reread into memory.
[0113] like Figure 3 As shown in ②, when an external device uses a dma-buf object, dma-buf_oc will increment its reference count. If the reference count changes from 0 to non-zero, the state machine will change from the idle state to the busy state.
[0114] like Figure 3 As shown in ③, when an external device stops using a dma-buf object, dma-buf_oc will decrement its reference count by one. If the reference count changes from non-zero to zero, the state machine will change from busy to idle.
[0115] If intermediate states are not omitted, such as Figure 3 As shown in ④, when a dma-buf object is to be reclaimed, after the reclamation operation begins, dma-buf_oc will change the state machine from the idle state to the going_offline state.
[0116] like Figure 3 As shown in ⑤, after the dma-buf object is reclaimed, dma-buf_oc changes the state machine from the going_offline state to the offline state. Simultaneously, the dma-buf object is removed from the online state management structure and added to the offline state management structure.
[0117] like Figure 3As shown in ⑥, when the recycling operation fails, dma-buf_oc changes the state machine from the going_offline state to the idle state.
[0118] like Figure 3 As shown in ⑦, when the original content in the memory corresponding to the dma-buf object needs to be read back into memory, the physical memory management structure (e.g., sg_table) corresponding to the dma-buf object is re-established. At the same time, dma-buf_oc changes the state machine from the offline state to the going_online state.
[0119] like Figure 3 As shown in step ⑧, after the original content in the memory corresponding to the dma-buf object is re-read, dma-buf_oc changes the state machine from the going_online state to the idle state. Simultaneously, the dma-buf object is removed from the offline state management structure and added to the online state management structure.
[0120] If intermediate states are omitted, during memory reclamation, idle dma-buf objects can be reclaimed. After the dma-buf objects are reclaimed, dma-buf_oc changes the state machine from the idle state to the offline state. For reloading, after the reload is complete, dma-buf_oc changes the state machine from the offline state to the idle state.
[0121] like Figure 3 As shown in ⑨, after the dma-buf object has been recycled, the dma-buf manager (producer) can actively call the release function and use the release callback interface to release the dma-buf object and delete it from the offline state management structure.
[0122] like Figure 3 As shown in ⑩, in the idle state, the dma-buf object can also be released directly, and the dma-buf object can be deleted from the online state management structure.
[0123] When there are no consumers using it, the online status can be directly changed to the offline status (not shown in the figure).
[0124] The online state management structure described above includes dma-buf objects currently in the busy and idle states, while the offline state management structure includes dma-buf objects currently in the offline state. Using both online and offline state management structures makes it easier to index dma-buf objects.
[0125] The online / offline management structure can be in the form of a linked list, an array, or other forms. This application does not limit the specific form of the management structure.
[0126] The online / offline management structure can be managed by the DMA memory management module, or by global variables or other methods. This application embodiment does not limit the manager of the management structure.
[0127] The DMA memory management module (dmaheap) is a heap data structure that manages one or more blocks of physical memory and is responsible for the creation, management, and destruction of DMA-buf objects. This application provides a data structure (which can be named dma-heap_oc) that is embedded into the existing data structure of a DMA-buf object during object creation, thereby enabling functions such as function hijacking and state updates.
[0128] Specifically, dma-heap_oc can be responsible for creating new dma-buf objects and initializing the dma-buf_oc structure associated with the dma-buf object.
[0129] dma-heap_oc can initialize the dma_buf_export function and hijack it, redirecting related function calls to a new dma_buf_export function. The new dma_buf_export function differs from the traditional dma_buf_export function in its process of creating dma-buf objects, thus making the process of accessing dma-buf objects different as well.
[0130] Figure 4 This illustrates the overall operation flow of the dma-buf object before the dma_buf_export function provided in this application embodiment is hijacked.
[0131] like Figure 4 As shown in step ①, the DMA-BUF producer requests DMA-BUF memory from the DMAHeap. For example... Figure 4 As shown in step ②, dmaheap requests a free memory page from physical memory. Figure 4 As shown in step ③, dmaheap assembles the allocated memory into a basic memory structure (e.g., sg_table) and allocates it to dma-buf objects. For example... Figure 4As shown in step ④, dmaheap defines a series of operation functions for dma-buf objects and registers these functions into the dma_buf_ops data structure or an internally implemented data structure for the kernel to call during buffer mapping, synchronization, or release. The internally implemented data structure varies depending on the specific implementation by different vendors, but the operation functions are essentially callback functions defined by the dma-buf framework. For example... Figure 4 As shown in step ⑤, after a dma-buf object is produced, the dma-buf producer calls the export function to expose the dma-buf object to the dma-buf user. Thus, the dma-buf user can use the dma-buf object by opening a file. Figure 4 As shown in step 6, dma-buf users can access dma-buf objects by calling functions in dma_buf_ops.
[0132] Figure 5 This illustrates the overall operation flow of the dma-buf object after the dma_buf_export function provided in this application embodiment is hijacked.
[0133] like Figure 5 As shown in step ①, the DMA-BUF producer requests DMA-BUF memory from the DMAHeap. For example... Figure 5 As shown in step ②, dmaheap requests a free memory page from physical memory. Figure 5 As shown in step ③, dmaheap assembles the allocated memory into a basic memory structure and allocates it to dma-buf objects. For example... Figure 5 As shown in step ④, the process of hijacking the registration of dma_buf_ops by the hijacking function includes: dmaheap defining a series of operation functions on dma-buf objects, registering this series of functions to the hijacking function, and pointing to the hijacked dma_buf_ops. For example... Figure 5 As shown in step ⑤, after the dma-buf object is produced, the dma-buf producer calls the hijacked export function to expose the dma-buf object to the dma-buf user. Figure 5 As shown in step ⑥, when a dma-buf user accesses a dma-buf object, its access operation first points to the hijacking function. After the hijacking function performs the corresponding processing, it then executes the access to the dma-buf object. The definition of the dma-buf operation function and its exposure to dma-buf users can usually be performed synchronously.
[0134] In this embodiment, the dma-buf consumer can only access the dma-buf object through a hijacking function. The hijacking function intercepts the user's call to the callback function in dma_buf_ops, thus blocking the user's access to the dma-buf object. This allows the hijacking function to check and update the state machine before accessing the dma-buf object. In this way, the hijacking function can maintain the state machine, and when the dma-buf object is garbage collected, the process of reading it again or releasing it can be determined based on the object's state, ensuring that the user can use the dma-buf object normally.
[0135] The following sections provide detailed explanations of the creation, access, recycling, and reading processes of dma-buf objects.
[0136] Figure 6 This illustration shows a schematic diagram of the creation process of a dma-buf object before the dma_buf_export function is hijacked, according to an embodiment of this application. (Refer to...) Figure 6 The creation process prior to the hijacking may include the following steps S601-S607.
[0137] S601, the DMA-buf producer requests DMA-buf memory from the DMA memory management module (DMAheap).
[0138] S602, the DMA memory management module (dmaheap) creates the struct dma_buf structure.
[0139] The S603 DMA memory management module requests a blank memory page from physical memory.
[0140] S604, the DMA memory management module will use the allocated memory to form a memory management structure.
[0141] S605, the struct dma_buf structure was created successfully.
[0142] S606, register callback function dma_buf_ops.
[0143] S607, the dma-buf object has been created.
[0144] Figure 7 This illustration shows a schematic diagram of the creation process of a dma-buf object after the dma_buf_export function has been hijacked, as provided in an embodiment of this application. (Refer to...) Figure 7The creation process prior to the hijacking may include the following steps S701-S709.
[0145] S701, the DMA-buf producer requests DMA-buf memory from the DMA memory management module (DMAheap).
[0146] S702, the DMA memory management module (dmaheap) creates the struct dma_buf structure.
[0147] The DMA memory management module (dmaheap) defines a struct dma_buf structure variable and initializes its member variables. Member variables can include the buffer size, file pointer, list of devices attached to the buffer, pointers to operation functions, and pointers to private data, etc.
[0148] S703, the DMA memory management module creates the struct dma-buf_oc data structure.
[0149] After the struct dma-buf_oc is created, it is bound to the struct dma_buf structure, and the dma-buf_oc data structure is initialized so that information such as the state of the dma-buf object and the data storage address after reclamation can be recorded through dma-buf_oc.
[0150] This application does not limit the specific binding method. For example, dma-buf_oc can be embedded in the data structure of the dma-buf object. At the same time, it can be nested in the first-level structure, second-level structure or other hierarchical structure of the dma-buf object according to actual needs. This application does not limit this.
[0151] After the `struct dma-buf_oc` is created, it can be added to the online state management structure for managing `dma-buf` objects. For example, if `dmaheap` manages `dma-buf` objects using a linked list, after creating the `struct dma-buf_oc`, it can be added to the online state management structure (which can be called an online linked list) maintained by `dmaheap`.
[0152] The S704 DMA memory management module requests a blank memory page from physical memory.
[0153] The S705 DMA memory management module uses the allocated memory to form a memory management structure.
[0154] For example, the memory management structure could be a linked list, sg_table. During use, memory data is sequentially filled into the corresponding memory pages of the sg_table.
[0155] S706, the struct dma_buf structure was created successfully.
[0156] S707, DMA memory management module registration hijacking function.
[0157] Hijacking functions can be used to maintain the state machine and execute access to the dma-buf object. After registering a hijacking function, the registered callback function dma_buf_ops can be hijacked, causing access from dma-buf consumers to first point to the hijacking function.
[0158] S708, DMA memory management module registers callback function dma_buf_ops.
[0159] S709, the dma-buf object has been created.
[0160] Based on steps S701-S709 above, this embodiment of the application modifies the process of dmaheap producing dma-buf objects by hijacking the dma_buf_export function. This allows for the synchronous creation of struct dma-buf_oc when creating the struct dma_buf structure, and the binding of struct dma-buf_oc with the corresponding dma-buf object. Furthermore, a hijacking function can be registered in dma-buf_oc to hijack the registered callback function dma_buf_ops. Thus, when a dma-buf user accesses a dma-buf object, the access operation first points to the hijacking function, which checks and updates the state machine before executing the access to the dma-buf object. In this way, the hijacking function can maintain the state machine, updating the state of the dma-buf object recorded in dma-buf_oc after each function call (including use and deuse). When reclaiming memory, the current state of the dma-buf object can be used to determine whether to reclaim it. In this way, dma-buf objects that have not been released in time can be reclaimed, and the reclaimed dma-buf objects can be reused, thereby avoiding the problem of memory waste.
[0161] After a DMA-buf producer requests a DMA-buf object from DMAHeap, it exports the corresponding DMA-buf object and provides it to DMA-buf consumers. Consumers can directly access this DMA-buf object. The process of a DMA-buf consumer accessing the target DMA-buf object is explained below.
[0162] Reference Figure 8 The access process after the dma_buf_export function is hijacked may include the following steps S801-S811.
[0163] S801, the dma-buf consumer obtains the target dma-buf object.
[0164] For example, during camera usage, the dma-buf producer (e.g., the camera user-space program) requests a target dma-buf object from the dmaheap. This target dma-buf object is then exported as a file descriptor (fd) and exposed to user space.
[0165] DMA-buf consumers (such as GPUs, display controllers, ISPs, etc.) obtain the target DMA-buf object through a file descriptor and map the memory management structure (such as sg_table) into their own DMA address space, and then perform DMA operations.
[0166] S802, the dma-buf consumer calls the callback function dma_buf_ops.
[0167] The callback functions `dma_buf_ops` can include `attach`, `detach`, `mmap`, `map_dma_buf`, `unmap_dma_buf`, `vmap`, and `vunmap`. The declarations of these functions can be found in the relevant file (`linux / dma-buf.h`). The function declarations may vary depending on the version. A brief introduction to these functions follows.
[0168] (1) int(*attach)(struct dma_buf*,struct dma_buf_attachment*): API provided by the dma-buf subsystem for DMA hardware access. It is called when a device is associated with dma-buf so that the device can share and access the buffer.
[0169] (2) void(*detach)(struct dma_buf*,struct dma_buf_attachment*): Called when the device is unattached.
[0170] (3) int(*pin)(struct dma_buf_attachment*attach): Used to lock a DMA buffer so that it is not reclaimed or reallocated by the system.
[0171] (4) void(*unpin)(struct dma_buf_attachment*attach): Used to unlock the DMA buffer that was previously locked by the pin() function.
[0172] (5) struct sg_table*(*map_dma_buf)(struct dma_buf_attachment*,enumdma_data_direction) maps sg_table to the device address space.
[0173] (6) void(*unmap_dma_buf)(struct dma_buf_attachment*,struct sg_table*,enum dma_data_direction): Unmaps sg_table after the dma-buf consumer finishes the DMA operation.
[0174] (7) void(*release)(struct dma_buf*): Releases resources when the reference count of dma-buf reaches zero.
[0175] (8) int(*mmap)(struct dma_buf*,struct vm_area_struct*vma): Used for user-space mmap mapping. Through the mmap callback interface, dma-buf can be directly mapped to user space, so that applications can access dma-buf like ordinary files, making it convenient for applications to read and write dma-buf directly in user space.
[0176] (9) int(*vmap)(struct dma_buf*dma-buf,struct iosys_map*map): is used to map dma-buf memory to kernel space so that the CPU can directly access the data.
[0177] (10) void(*vunmap)(struct dma_buf*dma-buf,struct iosys_map*map): Called when the CPU no longer needs to access the buffer, releases the previous virtual address mapping and releases the related resources.
[0178] (11) int(*get_flags)(struct dma_buf*dma-buf,unsigned long*flags): Used to get the attribute flags of the DMA buffer, such as whether it is writable, whether the cache is consistent, etc.
[0179] DMA-buf consumers can call functions (such as attach, map_dma_buf, begin_cpu_access, mmap, vmap, etc.) to use the target DMA-buf object. For example, a DMA-buf consumer can bind the target DMA-buf object to its own device using int(*attach). After use, functions (such as detach, unmap_dma_buf, end_cpu_access, vunmap, end_cpu_access, etc.) can be called to detach from the target DMA-buf object.
[0180] S803, DMA memory management module (dmaheap) executes hijacking function.
[0181] When a dma-buf consumer performs function operations on the target dma-buf object, a hijacking function is executed to intercept the dma-buf consumer's calls to dma_buf_ops in order to maintain the state machine.
[0182] S804, the DMA memory management module modifies the reference count of the state machine.
[0183] Each time a dma-buf consumer calls a function to use the target dma-buf object, the state machine's reference count is incremented by one.
[0184] S805, the DMA memory management module determines whether the target dma-buf object has been reclaimed.
[0185] After intercepting the dma-buf consumer's call to dma_buf_ops, check the state of the target dma-buf object to determine if it has been reclaimed. If it is currently in a reclaimed (offline) state, execute steps S806-S810. If it is currently in a non-reclaimed (online) state, execute steps S808-S810.
[0186] S806: If the target DMA-buf object has been reclaimed, the DMA memory management module will re-read the original data into memory.
[0187] After the target DMA-BUF object is garbage collected, its data is stored on the backend device. When a re-read is triggered, the original data can be read from the backend device and written to the memory corresponding to the target DMA-BUF object.
[0188] S807, the DMA memory management module re-establishes the memory management structure.
[0189] During memory reclamation, the memory management structure of the target dma-buf object is released. Therefore, when the object is re-read, its memory management structure needs to be rebuilt.
[0190] S808, the DMA memory management module executes the function corresponding to dma_buf_ops.
[0191] In some implementations, after maintaining the state machine, the hijacking function redirects the hijacked calls to `dma_buf_ops` to an internal platform data structure, thereby executing functions registered within that data structure. In other implementations, after maintaining the state machine, the hijacking function executes the function corresponding to `dma_buf_ops` registered within the hijacking function and returns the execution result to the dma-buf consumer.
[0192] S809, dma-buf consumers access the target dma-buf object.
[0193] dma-buf consumers obtain the physical memory address and length through a memory management structure (such as sg_table) and read data directly from the memory corresponding to the target dma-buf object.
[0194] S810, the DMA memory management module maintains the state of the target dma-buf object.
[0195] After a dma-buf consumer finishes accessing the object, it can release the target dma-buf object. After release, if the state machine's reference count is 0, indicating no external device is using the dma-buf object, the target dma-buf object can be garbage collected, and the state machine changes from the busy state to the idle state. If the state machine's reference count is not 0, indicating some external device is still using the dma-buf object, the target dma-buf object cannot be garbage collected, and the state machine remains in the busy state.
[0196] Based on steps S801-S810, DMA-buf producers and consumers directly share data through DMA-buf objects, avoiding data duplication between devices. In this embodiment, at the start of access, the state of the target DMA-buf object is checked, and a re-read or direct access process is initiated based on the target DMA-buf object's state. After access ends, the state of the target DMA-buf object is updated to determine if it can be reclaimed. When the reclamation timing and conditions are met, the reclamation of the target DMA-buf object is triggered.
[0197] Generally, there are two times when memory reclamation is triggered: background memory reclamation and direct memory reclamation. Background memory reclamation involves a background memory reclamation thread (kswapd) mounted in the daemon process (kernel_slabd) scanning the memory usage status. When the system's free memory falls below a threshold, it automatically reclaims idle memory. When dma-buf objects meet the target conditions, they can be reclaimed.
[0198] Direct memory reclamation: When faced with sudden memory demands and insufficient system memory, the kernel attempts to reclaim memory. The sysfs file system forces memory reclamation through a fast reclamation interface. DMA-BUF objects can be reclaimed when they meet certain criteria. Direct memory reclamation is further divided into two types: bulk reclamation and single reclamation. Bulk reclamation calls the `force_reclaim` parameter to attempt to reclaim a specified amount of memory. The `force_reclaim` parameter specifies the desired reclaim size in MB. Single reclamation calls the `reclaim_one` parameter to reclaim a single memory node. The `reclaim_one` parameter points to the node of the DMA-BUF object.
[0199] Figure 9 This illustration shows a memory reclamation method provided in an embodiment of this application. (Refer to...) Figure 9 In scenarios involving large-scale memory reclamation, this reclamation method includes the following steps S901-S912. This method is executed by the DMA memory management module dmaheap. dmaheap is mounted on a background memory reclamation thread (kswapd).
[0200] S901, the DMA memory management module traverses the dma-buf objects.
[0201] For example, taking the DMA memory management module's management of DMA-buf objects via linked lists as an example, the DMA memory management module can maintain an online linked list. After memory reclamation is triggered, the DMA memory management module can call the `dma-buf_oc_shrinker` function under the `shrink_slab` function to traverse the system-type DMAheap linked list (`heap_list`) and find the online linked list. The DMA memory management module's online linked list manages multiple DMA-buf objects. The DMA memory management module can traverse the online linked list in a certain order to determine whether the memory corresponding to each DMA-buf object can be reclaimed, and reclaim the reclaimable memory until the system memory is sufficient.
[0202] S902, for a given dma-buf object, determines whether the dma-buf object can be garbage collected.
[0203] In this embodiment, the dma-buf object can be garbage collected when its state meets a target condition. In some implementations, the target condition may be that the dma-buf object is in an idle state. In other implementations, the target condition may be that the time it takes for the dma-buf object to remain in an idle state exceeds a preset threshold. This embodiment does not limit the size of the preset threshold; for example, it could be 5 seconds.
[0204] If the dma-buf object can be reclaimed, proceed to step S903; if the dma-buf object cannot be reclaimed, repeat step S902 to determine if the next dma-buf object can be reclaimed.
[0205] S903, the DMA memory management module locks reclaimable dma-buf objects.
[0206] When a DMA-buf object is eligible for garbage collection, the `mutex_lock` function is called to lock the object, preventing it from being used by DMA-buf consumers during the garbage collection process. Furthermore, the DMA memory management module can also remove reclaimable DMA-buf objects from the online state management structure.
[0207] The S904 DMA memory management module allocates data storage addresses for reclaimable DMA-buf objects.
[0208] Since the data stored in memory corresponding to a dma-buf object is meaningful and has no associated file, it cannot be directly released. Its memory reclamation is not done through a backend file; the data in memory needs to be stored on a backend device. Therefore, an address for storing data on a backend device is allocated for reclaimable dma-buf objects. This address can be stored in the dma-buf_oc structure.
[0209] S905, the DMA memory management module reclaims the memory used by dma-buf objects.
[0210] S906, the DMA memory management module determines whether the memory used by the dma-buf object has been successfully reclaimed.
[0211] In this embodiment, memory is reclaimed according to the actual physical memory management structure. For example, the system manages memory through a linked list `sg_table`, where one `sg_table` manages multiple memory pages. The DMA memory management module can traverse the `sg_table` of reclaimable DMA-buf objects and reclaim the memory pages managed by the `sg_table` one by one. After each memory page is reclaimed, it can be determined whether the current page has been successfully reclaimed. If the current page is successfully reclaimed, the next memory page is reclaimed until all memory pages have been reclaimed, and then steps S907-S908 are executed. If the current page reclamation fails, step S909 is executed.
[0212] For example, the DMA memory management module can reclaim the memory management structure as a whole. If the reclamation is successful, steps S907-S908 are executed; if the reclamation is unsuccessful, step S909 is executed.
[0213] In this embodiment, the recycling method includes, but is not limited to, writing the DMA-buf back to a file, or compressing and writing it to a block device, as long as it ensures that the recycled DMA-buf can still be reloaded into memory and that the data remains unchanged. The file write-back method includes, but is not limited to, synchronous write-back and asynchronous write-back.
[0214] For example, compression and a memory pool (zs_pool) can be used to implement a backend device to store reclaimed data. Specifically, the data in dma-buf memory is compressed by memory page, and the compressed data is stored in the corresponding zs_pool through the standard zsmalloc interface.
[0215] S907, the DMA memory management module releases the memory management structure of reclaimable dma-buf objects.
[0216] S908, the DMA memory management module sets the status of the dma-buf object to the offline state.
[0217] The DMA memory management module can add reclaimed dma-buf objects to the offline state management structure.
[0218] S909, the DMA memory management module releases a portion of the memory that has been reclaimed.
[0219] After a failed reclamation, the DMA memory management module can re-add the dma-buf object to the online state management structure. In step S910, the DMA memory management module calls the mutex_unlock function to unlock the object.
[0220] S911, the DMA memory management module determines whether reclamation is required.
[0221] The DMA memory management module determines the expected size of memory to be reclaimed, such as 500MB, using the `force_reclaim` parameter. If the currently reclaimed memory size reaches 500MB, the reclamation is satisfied, and the reclamation process ends. If the currently reclaimed memory size is less than 500MB, the reclamation is not satisfied, and S912 is executed.
[0222] In step S912, the DMA memory management module determines whether the current DMA-BUF object is the last node. If yes, the recycling process ends. If not, it returns to step S902.
[0223] In a single reclamation scenario, the DMA memory management module traverses the dma-buf objects, finds the specified dma-buf object, determines whether the dma-buf object can be reclaimed, and if it can be reclaimed, executes S903-S910 to reclaim the dma-buf object.
[0224] Based on S901-S912 described above, after triggering memory reclamation, the DMA memory management module can determine whether the DMA-buf object can be reclaimed, and then reclaim the DMA-buf memory in the idle state. This allows for the reclamation of memory used by DMA-buf objects that were not released in a timely manner. The reclaimed memory can then be reused, thereby reducing memory pressure, improving memory utilization, and enhancing system smoothness.
[0225] After the memory encapsulated by the dma-buf object has been reclaimed, that is, after the dma-buf object is set to offline state, the reclaimed data can still be read back into memory.
[0226] There are two timings for re-reading. One is triggered by accessing `dma_buf_ops`. When a DMA-buf consumer re-consumes an offline DMA-buf object, the consumer calls `dma_buf_ops` to access the DMA-buf object, triggering a re-read of the DMA-buf. The other is triggered by sysfs nodes. When system memory is sufficient, the file system can actively read reclaimed data. Active reading is further divided into full reading and single reading. Full reading calls the `reonline_all` parameter, re-reading all DMA-buf memory data currently in an offline state back into memory. Single reading calls the `read_one` parameter, inputting the inode number of the specified DMA-buf object, re-reading the corresponding offline DMA-buf memory data back into memory.
[0227] For example, when the system actively reads in a dma-buf object, or when a dma-buf consumer reuses a dma-buf object, the original contents of the dma-buf object in memory are read back into memory. Figure 10 A flowchart illustrating a re-reading method provided in an embodiment of this application is shown. (Refer to...) Figure 10 The method may include the following steps S1001-S1009.
[0228] S1001, the DMA memory management module calls the mutex_lock function to lock the target dma-buf object.
[0229] In addition, the DMA memory management module can also remove the target dma-buf object from the offline state management structure.
[0230] S1002, the DMA memory management module allocates a memory management structure for the target dma-buf object.
[0231] During memory reclamation, the memory management structure of the target dma-buf object is released. Therefore, upon re-reading, the memory management structure of the target dma-buf object needs to be rebuilt.
[0232] S1003, the DMA memory management module reads data from the data storage address.
[0233] S1004, the DMA memory management module writes the read data into memory.
[0234] S1005, the DMA memory management module determines whether the read was successful.
[0235] For example, taking the DMA memory management module's management of DMA-buf objects via a linked list as an example, each page is traversed and reclaimed during garbage collection. When writing to memory, the DMA memory management module can request a memory page, write the read data into the requested memory page; determine whether the current page has been successfully read; if the current page has been successfully read, continue reading the next page until the last page has been successfully read, and then execute step S1006; if the current page has not been successfully read, then execute step S1007.
[0236] For example, during overall recycling, the DMA memory management module can request a memory management structure, write the read data into the requested memory management structure, and then determine whether the reading was successful. If successful, step S1006 is executed; if it fails, step S1007 is executed.
[0237] After successful data import, the record's data storage address can be cleared. Data stored on the backend device is also deleted.
[0238] S1006, the DMA memory management module executes the hijacking function to set the state of the target dma-buf object to online.
[0239] If the system actively reads the target DMA-buf object and no DMA-buf consumer is currently using it, the target DMA-buf object's state is set to idle. If the read is triggered by a DMA_buf_ops access and a DMA-buf consumer is currently using the target DMA-buf object, the target DMA-buf object's state is set to busy. After setting the state, the DMA memory management module can also add the target DMA-buf object to the online state management structure.
[0240] S1007, the DMA memory management module releases the memory where data has been read.
[0241] In addition, the DMA memory management module can also re-add the target dma-buf object to the offline state management structure.
[0242] S1008, the DMA memory management module calls the mutex_unlock function to unlock.
[0243] Based on steps S1001-S1008, during the re-reading process, the memory management structure corresponding to the target dma-buf object is rebuilt, and the reclaimed original memory data is filled into the corresponding memory in sequence, restoring its data state before reclamation. Afterwards, the dma-buf consumer can reuse the target dma-buf object and write new data into its memory.
[0244] Figure 11 This is a schematic diagram of a device provided in an embodiment of this application. The device may be an electronic device as described in this application, or a chip or chip system within an electronic device. Figure 11 As shown, the device 1100 may include a processing unit 1101. The processing unit 1101 is used to support the device 1100 in performing the above-described processing steps.
[0245] In one implementation, the device 1100 further includes a storage unit 1102. The storage unit 1102 and the processing unit 1101 are connected via a line. The storage unit 1102 may include one or more memories, which may be devices in one or more devices or circuits used to store programs or data. The storage unit 1102 may exist independently and be connected to the processing unit 1101 via a communication bus. Alternatively, the storage unit 1102 may be integrated with the processing unit 1101.
[0246] Storage unit 1102 may store computer-executable instructions for methods in an electronic device, so that processing unit 1101 executes the methods in the above embodiments. Storage unit 1102 may be a register, cache, or random access memory (RAM), or it may be a read-only memory (ROM) or other types of static storage devices that can store static information and instructions.
[0247] Figure 12 This is a schematic diagram of a chip structure provided in an embodiment of this application. Figure 12 As shown, chip 1200 includes one or more (including two) processors 1201, communication lines 1202 and communication interfaces 1203. Optionally, chip 1200 also includes a memory 1204.
[0248] In some implementations, memory 1204 stores elements such as executable modules or data structures, or subsets thereof, or extended sets thereof.
[0249] The methods described in the embodiments of this application can be applied to or implemented by the processor 1201. The processor 1201 may be an integrated circuit chip with signal processing capabilities. In implementation, each step of the above method can be completed by the integrated logic circuit in the hardware of the processor 1201 or by instructions in the form of software. The processor 1201 may be a general-purpose processor (e.g., a microprocessor or conventional processor), a digital signal processor, an application-specific integrated circuit (ASIC), a field-programmable gate array (FPGA), or other programmable logic devices, discrete gates, transistor logic devices, or discrete hardware components. The processor 1201 can implement or execute the methods, steps, and logic block diagrams disclosed in the embodiments of this application.
[0250] The steps of the method disclosed in the embodiments of this application can be directly implemented by a hardware decoding processor, or implemented by a combination of hardware and software modules in the decoding processor. The software modules can be located in mature storage media in the art, such as random access memory, read-only memory, programmable read-only memory, or electrically erasable programmable read-only memory (EEPROM). This storage medium is located in memory 1204, and processor 1201 reads information from memory 1204 and, in conjunction with its hardware, completes the steps of the above method.
[0251] The processor 1201, memory 1204 and communication interface 1203 can communicate with each other via communication line 1202.
[0252] In the above embodiments, the instructions stored in the memory for execution by the processor can be implemented in the form of a computer program product. This computer program product can be pre-written into the memory, or it can be downloaded and installed into the memory as software.
[0253] This application also provides a computer program product comprising one or more computer instructions. When the computer program instructions are loaded and executed on a computer, all or part of the flow or function according to the embodiments of this application is generated. The computer may be a general-purpose computer, a special-purpose computer, a computer network, or other programmable device. The computer instructions may be stored in a computer-readable storage medium or transmitted from one computer-readable storage medium to another. For example, computer instructions may be transmitted from one website, computer, server, or data center to another website, computer, server, or data center via wired (e.g., coaxial cable, fiber optic, digital subscriber line (DSL)) or wireless (e.g., infrared, wireless, microwave, etc.) means. The computer-readable storage medium may be any available medium that a computer can store or a data storage device such as a server or data center that integrates one or more available media. For example, available media may include magnetic media (e.g., floppy disks, hard disks, or magnetic tapes), optical media (e.g., digital versatile discs (DVDs)), or semiconductor media (e.g., solid-state drives (SSDs)).
[0254] This application provides an electronic device, which includes a processor and a memory. The memory stores a computer program, and the processor executes the computer program to perform the method described above.
[0255] This application provides a chip. The chip includes a processor, which is used to call a computer program in memory to execute the technical solutions in the above embodiments. Its implementation principle and technical effects are similar to those in the related embodiments described above, and will not be repeated here.
[0256] In the embodiments provided in this application, it should be understood that the disclosed devices / electronic devices and methods can be implemented in other ways. For example, the device / electronic device embodiments described above are merely illustrative. For instance, the division of modules or units is only a logical functional division, and in actual implementation, there may be other division methods. For example, multiple units or components may be combined or integrated into another system, or some features may be ignored or not executed. Furthermore, the displayed or discussed mutual couplings or direct couplings or communication connections may be through some interfaces; indirect couplings or communication connections between devices or units may be electrical, mechanical, or other forms. Additionally, the functional units in the various embodiments of this application may be integrated into one processing unit, or each unit may exist physically separately, or two or more units may be integrated into one unit. The integrated units described above can be implemented in hardware or as software functional units.
[0257] This application also provides a computer-readable storage medium storing a computer program that, when executed by a processor, implements the steps described in the various method embodiments above. The computer-readable storage medium stores a computer program or instructions. When executed by a processor, the computer program or instructions implement the methods described above. The methods described in the above embodiments can be implemented wholly or partially by software, hardware, firmware, or any combination thereof. If implemented in software, the functionality can be stored as one or more instructions or code on or transmitted over the computer-readable medium. The computer-readable medium can include computer storage media and communication media, and can also include any medium that can transfer a computer program from one place to another. The storage medium can be any target medium accessible by a computer.
[0258] As one possible design, computer-readable media may include compact disc read-only memory (CD-ROM), RAM, ROM, EEPROM, or other optical disc storage; computer-readable media may include disk storage or other disk storage devices. Furthermore, any connecting cable may also be appropriately referred to as computer-readable media. For example, if software is transmitted from a website, server, or other remote source using coaxial cable, fiber optic cable, twisted pair, DSL, or wireless technologies such as infrared, radio, and microwave, then coaxial cable, fiber optic cable, twisted pair, DSL, or wireless technologies such as infrared, radio, and microwave are included in the definition of media. As used herein, disks and optical discs include optical discs (CD), laser discs, optical discs, DVDs, floppy disks, and Blu-ray discs, where disks typically reproduce data magnetically, while optical discs optically reproduce data using lasers. Combinations of the above should also be included within the scope of computer-readable media.
[0259] This application describes embodiments with reference to flowchart illustrations and / or block diagrams of methods, apparatus (systems), and computer program products according to embodiments of this application. It should be understood that each block of the flowchart illustrations and / or block diagrams, and combinations of blocks in the flowchart illustrations and / or block diagrams, can be implemented by computer program instructions. These computer program instructions can be provided to a processing unit of a general-purpose computer, special-purpose computer, embedded processor, or other programmable data processing apparatus to produce a machine, such that the instructions, which execute via the processing unit of the computer or other programmable data processing apparatus, generate instructions for implementing the flowchart illustrations. Figure 1 One or more processes and / or boxes Figure 1 A device that provides the functions specified in one or more boxes.
[0260] In the foregoing description, specific details such as particular system architectures and techniques have been set forth for illustrative purposes and not for limitation, in order to provide a thorough understanding of the embodiments of this application. However, those skilled in the art will understand that this application can also be implemented in other embodiments without these specific details. In other instances, detailed descriptions of well-known systems, apparatuses, circuits, and methods have been omitted to avoid unnecessary detail that could obscure the description of this application.
[0261] It should be understood that, when used in this application specification and the appended claims, the term "comprising" indicates the presence of the described features, integrals, steps, operations, elements and / or components, but does not exclude the presence or addition of one or more other features, integrals, steps, operations, elements, components and / or a collection thereof.
[0262] It should also be understood that, in the description of this application, unless otherwise stated, " / " means "or", for example, A / B can mean A or B; "and / or" in this document is merely a description of the relationship between related objects, referring to any combination and all possible combinations of one or more of the related listed items, and including these combinations, for example, A and / or B can mean: A alone, A and B together, and B alone.
[0263] As used in this application specification and the appended claims, the term "if" may be interpreted, depending on the context, as "when," "once," "in response to determination," or "in response to detection." Similarly, the phrase "if determined" or "if detected [the described condition or event]" may be interpreted, depending on the context, as meaning "once determined," "in response to determination," "once detected [the described condition or event]," or "in response to detection [the described condition or event]."
[0264] References to "one embodiment" or "some embodiments" as described in this specification mean that one or more embodiments of this application include a specific feature, structure, or characteristic described in connection with that embodiment. Therefore, the phrases "in one embodiment," "in some embodiments," "in other embodiments," "in still other embodiments," etc., appearing in different parts of this specification do not necessarily refer to the same embodiment, but rather mean "one or more, but not all, embodiments," unless otherwise specifically emphasized. The terms "comprising," "including," "having," and variations thereof mean "including but not limited to," unless otherwise specifically emphasized.
[0265] The above-described embodiments are only used to illustrate the technical solutions of this application, and are not intended to limit them. Although this application has been described in detail with reference to the foregoing embodiments, those skilled in the art should understand that modifications can still be made to the technical solutions described in the foregoing embodiments, or equivalent substitutions can be made to some of the technical features. Such modifications or substitutions do not cause the essence of the corresponding technical solutions to deviate from the spirit and scope of the technical solutions of the embodiments of this application, and should all be included within the protection scope of this application.
Claims
1. A memory management method, characterized in that, Applied to an electronic device in which at least one dma-buf object is created, the dma-buf object having different states, the method includes: When the memory reclamation process is triggered, it is determined whether the target dma-buf object meets the target condition, wherein the target condition is that the current state of the target dma-buf object is a reclaimable state; If the target dma-buf object meets the target conditions, the memory occupied by the target dma-buf object is reclaimed.
2. The method according to claim 1, characterized in that, The target condition is: the duration for which the target dma-buf object is in a recyclable state exceeds a preset threshold.
3. The method according to claim 1 or 2, characterized in that, The data structure of the target dma-buf object includes a first structure, which is used to record the current state of the target dma-buf object; Wherein, if the target dma-buf object is not used by a consumer, the target dma-buf object is in a recyclable state; When the target dma-buf object is used by a consumer, the target dma-buf object is in a state that cannot be reclaimed; After the target dma-buf object is reclaimed, the target dma-buf object is in a reclaimed state.
4. The method according to claim 3, characterized in that, The first structure also includes a hijacking function, which is used to maintain the state of the target dma-buf object.
5. The method according to claim 4, characterized in that, The method further includes: After detecting an access operation to the target dma-buf object, the hijacking function is executed; Access the target dma-buf object if the target dma-buf object is in a state that can be reclaimed or a state that cannot be reclaimed; After the access is completed, if the target dma-buf object is reclaimable, the state of the target dma-buf object is set to the reclaimable state.
6. The method according to claim 5, characterized in that, The method further includes: If the target dma-buf object is in the recycled state, the target dma-buf object is reread. After rereading the target dma-buf object, the state of the target dma-buf object is set to the non-reclaimable state.
7. The method according to claim 1, characterized in that, The process of reclaiming the memory occupied by the target dma-buf object includes: Allocate a data storage address for the target dma-buf object in the backend device; The data in the memory occupied by the target dma-buf object is stored to the data storage address.
8. The method according to claim 1, characterized in that, The method further includes: After reclaiming the memory occupied by the target dma-buf object, the state of the target dma-buf object is set to the reclaimed state.
9. An electronic device, characterized in that, include: One or more processors; one or more memories; The memory stores one or more programs that, when executed by the processor, cause the electronic device to perform the method of any one of claims 1 to 8.
10. A computer-readable storage medium, characterized in that, The computer-readable storage medium stores instructions that, when executed on a computer, cause the computer to perform the method as described in any one of claims 1 to 8.