Memory management method and device, equipment and medium
By recording the identification information of kernel modules in the Linux operating system memory management system, the problem of difficulty in locating the responsible team when memory anomalies occur is solved, and the kernel module to which the memory belongs can be quickly and accurately located, making maintenance easier.
Patent Information
- Application Number
- CN202510795673.9
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2025-06-13
- Publication Date
- 2025-09-16
AI Technical Summary
The existing Linux operating system memory management system has difficulty locating the kernel module to which abnormal memory belongs, making it difficult to determine the responsible team for maintenance.
Receive the memory allocation request of the kernel module in the memory management system, and record the identification information at the allocated memory location, including the identification field and magic field of the kernel module, so as to clearly identify the kernel module to which the memory belongs when a memory exception occurs.
It enables the kernel module to which the abnormal memory belongs to be quickly located when a memory anomaly occurs, making it easier for the responsible team to perform maintenance and improving the efficiency and accuracy of memory management.
Smart Images

Figure CN120653578A_ABST
Abstract
Description
Technical Field
[0001] The present application relates to the field of computer technology, and in particular to a memory management method, apparatus, device, and medium. Background Art
[0002] In an operating system such as Linux, the memory management system is responsible for managing the allocation, deallocation, mapping, and protection of physical and virtual memory. The goal of the memory management system is to efficiently utilize memory resources while providing secure and transparent memory access mechanisms for software developed using the Linux operating system.
[0003] Currently, software developed using the Linux operating system typically consists of multiple kernel modules. These modules request memory through the memory management system during runtime, and different kernel modules are developed and maintained by teams in different departments. When memory anomalies occur, current memory management systems have difficulty locating the kernel module to which the anomaly belongs, making it difficult to identify the responsible team for kernel module maintenance. Summary of the Invention
[0004] The purpose of the embodiments of the present application is to provide a memory management method, apparatus, device, and medium to clearly identify the kernel module to which memory belongs. When a memory anomaly occurs, it is easy to locate the kernel module to which the abnormal memory belongs, so that the responsible team can be determined to maintain the kernel module. The specific technical solution is as follows:
[0005] In a first aspect, an embodiment of the present application provides a memory management method, which is applied to a memory management system. The method includes:
[0006] receiving a memory allocation request sent by a kernel module, wherein the memory allocation request includes memory size information and identification information of the kernel module;
[0007] Allocating memory for the kernel module in physical memory according to the memory size information;
[0008] The identification information is recorded at a location associated with the memory.
[0009] In some embodiments, the memory size information includes a first length; and the step of allocating memory for the kernel module in the physical memory according to the memory size information includes:
[0010] Obtaining a second length of the identification information;
[0011] In the physical memory, allocating a memory of a third length for the kernel module, where the third length is greater than or equal to the sum of the first length and the second length;
[0012] The step of recording the identification information at the associated location in the memory includes:
[0013] The identification information is recorded at a preset position in the memory, and the length of the preset position is a second length.
[0014] In some embodiments, the preset position is located at the end of the memory.
[0015] In some embodiments, the method further comprises:
[0016] When the memory is abnormal, prompt information is output according to the identification information recorded at the preset location.
[0017] In some embodiments, the memory size information includes a first page number; and the step of allocating memory for the kernel module in the physical memory according to the memory size information includes:
[0018] In the physical memory, allocating the first number of pages of memory to the kernel module;
[0019] Recording the identification information at the location associated with the memory includes:
[0020] generating a first node of the memory association;
[0021] The identification information is recorded in the first node.
[0022] In some embodiments, the memory management system manages a first linked list corresponding to the first number of pages; and the step of generating the first node associated with the memory includes:
[0023] Mounting the first node at the end of the first linked list;
[0024] The address of the memory is recorded at the first node.
[0025] In some embodiments, the method further comprises:
[0026] receiving a memory release request, wherein the memory release request includes an address of the memory;
[0027] Release the memory according to the address of the memory;
[0028] Determining the first node in the first linked list according to the first number of pages included in the memory and the address of the memory;
[0029] Delete the first node.
[0030] In some embodiments, the method further comprises:
[0031] When the memory is abnormal, determining the first node associated with the memory;
[0032] Output prompt information according to the identification information recorded in the first node.
[0033] In some embodiments, the identification information includes at least one of the following identification fields:
[0034] A first identification field, used to carry an identification of the department to which the kernel module belongs;
[0035] A second identification field is used to carry an identification of the group to which the kernel module belongs;
[0036] The third identification field is used to carry the identification of the kernel module.
[0037] In some embodiments, the identification information further includes at least one of the following magic fields:
[0038] A first magic field, located at the head of the identification information, is used to carry a first magic word;
[0039] The second magic field is located at the end of the identification information and is used to carry a second magic word.
[0040] In some embodiments, the identification information includes the first magic field and the second magic field, and the identification information is located at a preset location in the memory; the method further includes:
[0041] When the value carried by the first magic field is different from the first magic word, determining that the memory preceding the first magic field in the memory is abnormal;
[0042] When the value carried by the second magic field is different from the second magic word, it is determined that the memory behind the second magic field in the memory is abnormal.
[0043] In a second aspect, an embodiment of the present application provides a memory management device, which is applied to a memory management system. The device includes:
[0044] a receiving unit, configured to receive a memory allocation request sent by a kernel module, wherein the memory allocation request includes memory size information and identification information of the kernel module;
[0045] an allocation unit, configured to allocate memory for the kernel module in the physical memory according to the memory size information;
[0046] A recording unit is used to record the identification information at a location associated with the memory.
[0047] In some embodiments, the memory size information includes a first length; the allocation unit is specifically configured to obtain a second length of the identification information; and allocate memory of a third length for the kernel module in the physical memory, the third length being greater than or equal to the sum of the first length and the second length;
[0048] The recording unit is specifically configured to record the identification information at a preset location in the memory, where the length of the preset location is a second length.
[0049] In some embodiments, the preset position is located at the end of the memory.
[0050] In some embodiments, the device further includes: an output unit, configured to output prompt information according to the identification information recorded at the preset location when the memory is abnormal.
[0051] In some embodiments, the memory size information includes a first number of pages; the allocation unit is specifically configured to allocate memory of the first number of pages to the kernel module in the physical memory;
[0052] The recording unit is specifically configured to generate a first node associated with the memory; and record the identification information in the first node.
[0053] In some embodiments, the memory management system manages a first linked list corresponding to the first number of pages; the recording unit is specifically used to mount a first node at the end of the first linked list; and record the address of the memory at the first node.
[0054] In some embodiments, the device also includes: a release unit, used to receive a memory release request, the memory release request includes the address of the memory; release the memory according to the address of the memory; determine the first node in the first linked list according to the first number of pages included in the memory and the address of the memory; delete the first node.
[0055] In some embodiments, the device further includes: an output unit, configured to determine the first node associated with the memory when the memory is abnormal; and output prompt information based on the identification information recorded in the first node.
[0056] In some embodiments, the identification information includes at least one of the following identification fields:
[0057] A first identification field, used to carry an identification of the department to which the kernel module belongs;
[0058] A second identification field is used to carry an identification of the group to which the kernel module belongs;
[0059] The third identification field is used to carry the identification of the kernel module.
[0060] In some embodiments, the identification information further includes at least one of the following magic fields:
[0061] A first magic field, located at the head of the identification information, is used to carry a first magic word;
[0062] The second magic field is located at the end of the identification information and is used to carry a second magic word.
[0063] In some embodiments, the identification information includes the first magic field and the second magic field, and the identification information is located at a preset location in the memory; the apparatus further comprises: a determining unit configured to determine that a memory preceding the first magic field in the memory is abnormal when a value carried by the first magic field is different from the first magic word; and to determine that a memory following the second magic field in the memory is abnormal when a value carried by the second magic field is different from the second magic word.
[0064] In a third aspect, an embodiment of the present application provides an electronic device, including a processor, a communication interface, a memory, and a communication bus, wherein the processor, the communication interface, and the memory communicate with each other via the communication bus;
[0065] Memory for storing computer programs;
[0066] The processor is configured to implement any of the methods described in the first aspect when executing a program stored in the memory.
[0067] In a fourth aspect, an embodiment of the present application provides a computer-readable storage medium, wherein the computer-readable storage medium stores a computer program, and when the computer program is executed by a processor, any of the methods provided in the first aspect is implemented.
[0068] In a fifth 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 any of the methods provided in the first aspect above.
[0069] Beneficial effects of the embodiments of the present application:
[0070] In the technical solution provided in the embodiment of the present application, the memory management system receives a memory allocation request from a kernel module, and the memory allocation request includes identification information of the kernel module. After allocating memory to the kernel module according to the memory size information, the memory management system can record the identification information at a location associated with the allocated memory. By applying the technical solution provided in the embodiment of the present application, the identification information is recorded at a location associated with the allocated memory, and the identification information is associated with the allocated memory. In this way, the memory management system can clearly identify the kernel module to which the memory belongs. When an abnormality occurs in the memory, it is easy to locate the kernel module to which the abnormal memory belongs, so as to determine the responsible team for maintenance of the kernel module.
[0071] Of course, it is not necessary to achieve all the advantages described above at the same time when implementing any product or method of the present application. BRIEF DESCRIPTION OF THE DRAWINGS
[0072] In order to more clearly illustrate the embodiments of the present application or the technical solutions in the prior art, the following briefly introduces the drawings required for use in the embodiments or the description of the prior art. Obviously, the drawings described below are only some embodiments of the present application. For ordinary technicians in this field, other embodiments can also be obtained based on these drawings.
[0073] Figure 1 This is the first diagram of memory management;
[0074] Figure 2 This is the second schematic diagram of memory management;
[0075] Figure 3 A schematic diagram of a first flow chart of the memory management method provided in an embodiment of the present application;
[0076] Figure 4a A first schematic diagram of identification information provided in an embodiment of the present application;
[0077] Figure 4b A second schematic diagram of identification information provided in an embodiment of the present application;
[0078] Figure 5 A second flow chart of the memory management method provided in an embodiment of the present application;
[0079] Figure 6 A third flow chart of the memory management method provided in an embodiment of the present application;
[0080] Figure 7 A schematic diagram of memory management provided in an embodiment of the present application;
[0081] Figure 8 A schematic diagram of a linked list array provided in an embodiment of the present application;
[0082] Figure 9 A schematic diagram of the structure of a memory management device provided in an embodiment of the present application;
[0083] Figure 10 A schematic diagram of the structure of an electronic device provided in an embodiment of the present application. DETAILED DESCRIPTION
[0084] The following will be combined with the drawings in the embodiments of this application to clearly and completely describe the technical solutions in the embodiments of this application. Obviously, the embodiments described are only part of the embodiments of this application, not all of the embodiments. Based on the embodiments in this application, all other embodiments obtained by ordinary technicians in this field based on this application are within the scope of protection of this application.
[0085] To facilitate understanding, the terms appearing in the embodiments of this application are explained below.
[0086] Linux operating system: An open source, Unix-like operating system known for its stability, high security, and flexibility. It is widely used on various platforms such as servers, desktop computers, embedded systems, and mobile devices.
[0087] Buddy system algorithm: An algorithm for managing physical memory pages. Its primary goal is to efficiently allocate and release memory blocks while reducing memory fragmentation. The buddy system divides memory into equal-sized blocks and satisfies memory allocation requests by recursively merging and splitting these blocks.
[0088] Memory allocator (Slab): An efficient memory management mechanism for managing memory allocation and deallocation of kernel objects. It is designed to reduce memory fragmentation and improve the performance of frequently allocating and releasing small memory blocks.
[0089] Hash List: A data structure that combines hash tables and linked lists for efficient data storage and search. It uses a hash function to distribute data across multiple linked lists, reducing search time. The core idea of a hash list is that the hash table is used to quickly locate items in the linked list, while the linked list is used to resolve hash conflicts.
[0090] In an operating system such as Linux, the memory management system is responsible for managing the allocation, deallocation, mapping, and protection of physical and virtual memory. The goal of the memory management system is to efficiently utilize memory resources while providing secure and transparent memory access mechanisms for software developed using the Linux operating system.
[0091] A physical page is the basic unit of memory management and is typically 4 kilobytes (KB) or 64 KB in size. For example, a total of 8 gigabytes (GB) of physical memory can be divided into 2,097,152 pages using 4 KB physical pages.
[0092] The memory management system uses a buddy system algorithm to manage physical pages. This algorithm groups all free pages into 11 block lists. Each block list can contain multiple blocks, and the sizes of the blocks in each block list are 1, 2, 4, 8, ..., 1024 consecutive pages. For example, block list 3 can contain blocks of 8 consecutive pages, meaning that block list 3 is an 8-page block list. The physical address of the first page of each block is an integer multiple of the block size.
[0093] like Figure 1 The memory management diagram shown in Figure 1 is as follows. If a memory user (such as a kernel module) needs to request 32KB of memory (i.e., 8 pages) from physical memory, it must use the memory management system, employing a buddy system algorithm, to first search the 8-page block list for a free block. If not, it searches the 16-page block list. If a free block is found, it divides the 16-page block into two halves, inserting half into the 8-page block list and using the remaining half to satisfy the request. If there is still no free block in the 16-page block list, it searches a larger block list until it finds one.
[0094] The process of freeing memory is the reverse of allocating it. The memory management system uses a buddy system algorithm, merging a pair of free buddy blocks of size X into a single block of size 2X. For example, when releasing an 8-page block, the memory management system checks to see if its buddy block is free. If so, the blocks are merged into a single 16-page block. The memory management system then checks to see if the buddy block is free. If so, the blocks are merged again until no more blocks can be merged.
[0095] The memory management system uses the buddy system algorithm to allocate and release large memory. However, the minimum unit of memory allocated by the buddy system algorithm is a page, making it unsuitable for allocating small memory (memory smaller than a page). In the memory management system, small memory is allocated and released using the slab allocator (also known as the slab memory management algorithm).
[0096] The memory management system uses the Slab memory management algorithm. It first requests one or several consecutive pages from the buddy system algorithm and divides them into cache objects of the same size for unified management. It also carries necessary information to meet the small memory request and release requirements of memory users. Cache object sizes can range from 96, 192, 8, 16, 32, ..., 2048, and 4096 bytes.
[0097] Take the 8-byte cache object (i.e. kmalloc-8) as an example, Figure 2 As shown, the memory management system uses the Slab memory management algorithm. It applies for multiple 4KB pages through the buddy system algorithm and divides each page into 52-byte segments, with each 52-byte segment forming a cache object. Each cache object includes an 8-byte data area, which stores user data when in use. The following fields are control fields, including a 4-byte red area, a 4-byte pointer to the next free object, a 16-byte requester information record, a 16-byte releaser information record, and a 4-byte magic word. The information record includes the function address, processor core number, process number, and time.
[0098] Currently, software developed using the Linux operating system typically consists of multiple kernel modules. These modules request memory through the memory management system during runtime, and different kernel modules are developed and maintained by teams in different departments. When memory anomalies occur, current memory management systems have difficulty locating the kernel module to which the anomaly belongs, making it difficult to identify the responsible team for kernel module maintenance.
[0099] Although memory allocated using the Slab memory management algorithm records information such as the function address of the requester and releaser, the processor core number, process number, and time, this information cannot be used to distinguish kernel modules. Because a process can contain multiple kernel modules, and these modules may use the same function to allocate memory, the requester and releaser information recorded in memory allocated by different kernel modules may be identical. This makes many memory issues (such as memory leaks, memory anomalies, and memory corruption) difficult to identify and locate.
[0100] Currently, the slab memory management algorithm uses a cache creation and string naming method to distinguish kernel modules. Specifically, 1) Cache creation: Use a function (e.g., kmem_cache_create) to create a cache with a string name. 2) Object allocation: Use a function (e.g., kmem_cache_alloc) to allocate an object from a specified cache as the requested memory. 3) Object release: Use a function (e.g., kmem_cache_free) to release an object back to the specified cache. 4) Cache destruction: Use a function (e.g., kmem_cache_destroy) to release all cache memory and its descriptor (i.e., name).
[0101] For example, if kernel module A wants to request 8 bytes of memory, it first creates a cache using the slab memory management algorithm and names the cache (e.g., "A_module_8"). Then, the request is made within the created cache. The cache memory and name can be recorded in a configuration file. This way, the memory with the corresponding name (e.g., A_module_8) can be viewed in a configuration file (e.g., / proc / slabinfo) to distinguish it from other modules.
[0102] However, with this approach, each kernel module must create a unique cache with a unique name and size before requesting memory, making it significantly more complex than directly requesting memory. Furthermore, when there are many kernel modules and a large amount of memory usage, configuration files will record a large number of memory with different names and sizes. If the names don't correlate well with the kernel modules, it can be difficult to determine which kernel module a memory belongs to. Even for large memory requests directly using the buddy system algorithm, it's still impossible to clearly identify which kernel module the memory belongs to.
[0103] To solve the above problems, the present invention provides a memory management method, which is applied to the memory management system. Figure 3 , Figure 3 This is a first flow chart of a memory management method provided in an embodiment of the present application, and the memory management method includes the following steps.
[0104] Step S31: Receive a memory allocation request sent by a kernel module. The memory allocation request includes memory size information and kernel module identification information.
[0105] Step S32: Allocate memory for the kernel module in the physical memory according to the memory size information.
[0106] Step S33: record the identification information at the associated location in the memory.
[0107] In the technical solution provided in the embodiment of the present application, the memory management system receives a memory allocation request from a kernel module, and the memory allocation request includes identification information of the kernel module. After allocating memory to the kernel module according to the memory size information, the memory management system can record the identification information at a location associated with the allocated memory. By applying the technical solution provided in the embodiment of the present application, the identification information is recorded at a location associated with the allocated memory, and the identification information is associated with the allocated memory. In this way, the memory management system can clearly identify the kernel module to which the memory belongs. When an abnormality occurs in the memory, it is easy to locate the kernel module to which the abnormal memory belongs, so as to determine the responsible team for maintenance of the kernel module.
[0108] In the embodiment of the present application, the memory management system runs in the operating system of the electronic device (such as the Linux operating system) and is used to perform memory management according to the request of the kernel module. The electronic device in the embodiment of the present application can be a personal computer (PC), a server or other device, and the kernel module can be a self-developed software module developed and maintained by a team from a different department, without limitation.
[0109] In the above step S31 , the memory allocation request is a request for a kernel module to apply for memory, including memory size information of the memory applied for and identification (Identifier, ID) information (Module ID, MID) of the kernel module to which it belongs.
[0110] The memory size information indicates the size of the requested memory. For example, the memory size information may be the length of the requested memory or the number of pages of the requested memory, which is not limited.
[0111] The identification information is used to identify the kernel module to which the memory being applied belongs. In an embodiment of the present application, the identification information may include at least one of the following identification fields: a first identification field, used to carry the identification of the department to which the kernel module belongs; a second identification field, used to carry the identification of the group to which the kernel module belongs, that is, the identification of the group under the department to which it belongs; and a third identification field, used to carry the identification to which the kernel module belongs. The length of each identification field can be set according to actual conditions. For example, the total length of the identification field can be a fixed value (such as 24 bits (bits)). If the identification information includes two identification fields (such as the first identification field and the second identification field, the length of each identification field can be 12 bits. If the identification information includes three identification fields, the length of each identification field can be 8 bits, and there is no limitation on this. Through one or more identification fields included in the identification information, the kernel module to which the memory belongs can be clearly identified, thereby realizing the identification of the kernel module.
[0112] The identification information may also include at least one of the following magic fields: a first magic field, located at the head of the identification information, used to carry a first magic word; and a second magic field, located at the tail of the identification information, used to carry a second magic word. The length of each magic field can be set based on actual circumstances, such as 4 bits, and is not limited to this. The first magic word and the second magic word can be fixed values, for example, the first magic word can be 1010, and the second magic word can be 0101, and this is not limited to this. The inclusion of one or more magic fields in the identification information, and the fixed values carried by the magic fields, facilitates detection of memory anomalies.
[0113] For example, in the case where the identification information includes a first identification field, a second identification field, a first magic field, and a second magic field, if the length of each identification field is 12 bits and the length of each magic field is 4 bits, the identification information includes: a 4-bit header magic field (i.e., the first magic field), a 12-bit first-level module ID field (i.e., the first identification field), a 12-bit second-level module ID field (i.e., the second identification field), and a 4-bit tail magic field (i.e., the second magic field), as shown in FIG. Figure 4a shown.
[0114] For another example, when the identification information includes a first identification field, a second identification field, a third identification field, a first magic field, and a second magic field, and the length of each identification field is 8 bits, the middle identification field includes: an 8-bit first-level module ID field (i.e., the first identification field), an 8-bit second-level module ID field (i.e., the second identification field), and an 8-bit third-level module ID field (i.e., the third identification field), as shown in FIG. Figure 4b shown. Figure 4a and Figure 4b The length of the identification information shown is 32 bits. The identification information can also be of other lengths. For the convenience of description, the following explanation is based on the example of the identification information having a length of 32 bits (ie, 4 bytes), without limitation.
[0115] In an embodiment of the present application, the kernel module may send a memory allocation request to the memory management system, and the memory management system may receive the memory allocation request sent by the kernel module and allocate memory to the kernel module.
[0116] In the above step S32, the memory management system can determine the memory size indicated by the memory information, and allocate memory greater than or equal to the determined memory size to the kernel module in the physical memory. For the sake of ease of description, the allocated memory will be referred to as the first memory in the following, which does not serve as a limitation.
[0117] In the above step S33, the memory management system can determine a piece of free memory in the physical memory whose length is greater than or equal to the length of the identification information, and associate the determined memory with the first memory. The determined memory is the location associated with the first memory. The memory management system can record the identification information in the determined memory to associate the identification information with the first memory. For ease of description, the memory that records the identification information will be referred to as the second memory. For example, if the length of the identification information is 4 bytes, the memory management system can determine a piece of free memory with a length of 4 bytes or greater than 4 bytes in the physical memory as the second memory to record the identification information.
[0118] When the first memory is larger than the sum of the memory size indicated by the memory information and the length of the identification information, the memory management system may also determine a block of memory in the first memory whose length is greater than or equal to the length of the identification information as the second memory to record the identification information.
[0119] In an embodiment of the present application, the kernel module may transmit a memory allocation request to the memory management system via an interface function. The interface function may include function variables indicating information such as memory size information and identification information. The memory management system may execute the interface function based on the information indicated by each function variable of the interface function, allocate memory to the kernel module, and record the identification information in a location associated with the allocated memory.
[0120] In an embodiment of the present application, the memory management system can use the above steps S31 to S33 to allocate memory to each kernel module, record the identification information of each kernel module at the location associated with the allocated memory, and realize the association between the allocated memory and the identification information. When an abnormality occurs in any memory, the memory management system can obtain the recorded identification information from the location associated with the memory where the abnormality occurs (i.e., the second memory), and output a prompt message including the obtained identification information. In this way, development and maintenance personnel can determine the kernel module to which the abnormal memory belongs based on the identification information, and then the responsible team of the kernel module can maintain the kernel module.
[0121] In some embodiments, the memory size information may be the length of the memory requested, that is, the memory size information may include a first length. In this case, the memory management system may use a Slab memory management algorithm to request a small memory in the physical memory. Figure 5 , Figure 5 This is a second flow chart of the memory management method provided in an embodiment of the present application. The memory management method may include the following steps.
[0122] Step S51: Receive a memory allocation request from the kernel module. The memory allocation request includes memory size information and kernel module identification information. This is the same as step S31 above.
[0123] Step S52: Obtain the second length of the identification information; and allocate a memory of a third length to the kernel module in the physical memory, wherein the third length is greater than or equal to the sum of the first length and the second length.
[0124] Step S53: Record the identification information at a preset location in the memory, where the length of the preset location is a second length.
[0125] In the technical solution provided in the embodiment of the present application, the memory management system can adopt the Slab memory management algorithm to apply for a small memory in the physical memory, and record the identification information at a preset position of the allocated memory to realize the association of the identification information with the first memory, so as to more quickly determine the identification information of the kernel module to which the allocated memory belongs, thereby improving efficiency.
[0126] In step S52 above, the memory management system may, after receiving the memory allocation request, obtain the length of the identification information included in the memory allocation request to obtain the second length. In an embodiment of the present application, since the second length is a fixed value, the memory management system may also directly obtain the second length without having to obtain the second length based on the identification information after receiving the memory allocation request. The method for obtaining the second length is not limited herein.
[0127] The memory management system may calculate the sum of the first length and the second length and determine a third length that is greater than or equal to the sum. For example, if the first length is 8 bytes, the second length is 4 bytes, and the sum is 12 bytes, then the third length may be 12 bytes, or may be greater than 12 bytes (e.g., 16 bytes), without limitation.
[0128] In an embodiment of the present application, the memory management system may allocate memory using a ladder alignment method based on the size of the cache object. In this case, the third length is the size of the smallest cache object that is greater than or equal to the sum value. For example, if the sum value is 12 bytes, which is greater than 8 bytes but less than 16 bytes, the third length is 16 bytes; if the sum value is 20 bytes, which is greater than 16 bytes but less than 32 bytes, the third length is 32 bytes.
[0129] The memory management system may allocate a memory having a third length in the physical memory to the kernel module as the first memory.
[0130] In the above step S53, the preset position can be located at the head or tail of the first memory. The memory management system can determine the memory with a length of the second length at the tail of the third memory as the second memory, and record the identification information in the second memory. The memory management system can also determine the memory with a length of the second length at the head of the third memory as the second memory, and record the identification information in the second memory. In this embodiment of the present application, the remaining memory in the first memory is used to carry user data.
[0131] In an embodiment of the present application, the kernel module can pass the memory allocation request into the memory management system through the first interface function (such as the kmalloc_mid function). The function prototype of the first interface function may include a function name (such as kmalloc_mid), a function variable indicating a first length (such as a size variable) and the data type of the variable (such as size_t), and a function variable indicating identification information (such as a mid variable) and the data type of the variable (such as unsigned int). The function prototype of the first interface function may also include a function variable indicating flag information (such as a flags variable) and the data type of the variable (such as gfp_t). The function prototype of the first interface function can be expressed as: void*kmalloc_mid(size_t size, gfp_tflags, unsigned int mid).
[0132] The memory management system can execute the first interface function based on the information indicated by each function variable of the first interface function (such as the first length and identification information, etc.), allocate memory to the kernel module, and record the identification information at a preset location of the allocated memory.
[0133] In some embodiments, when a first memory exception occurs, the memory management system can output a prompt message based on the identification information recorded at a preset location. Specifically, the memory management system can obtain the identification information recorded in a second memory at a preset location within the first memory and output a prompt message including the obtained identification information. In this way, developers and maintenance personnel can determine the kernel module to which the abnormal memory belongs based on the identification information, and the responsible team for that kernel module can then perform maintenance on that kernel module.
[0134] In some embodiments, when the identification information is located at a preset location in the first memory, the memory management system can determine whether the memory is abnormal based on the first magic field and the second magic field included in the identification information. That is, when the value carried by the first magic field is different from the first magic word, the memory management system can determine that the memory where the first magic field is located is abnormal, and determine that the memory in front of the first magic field in the memory is abnormal; when the value carried by the second magic field is different from the second magic word, the memory management system can determine that the memory where the second magic field is located is abnormal, and determine that the memory behind the second magic field in the memory is abnormal. Using the magic field included in the identification information and the fixed value carried by the magic field, the memory management system can determine whether the memory where the magic field is located is abnormal, and determine whether the memory in front of or behind the magic field is abnormal.
[0135] In some embodiments, the memory size information may be the number of pages of memory requested, that is, the memory size information may include the first page number. In this case, the memory management system may use a buddy system algorithm to request a large memory in the physical memory. Figure 6 , Figure 6 This is a third flow chart of the memory management method provided in an embodiment of the present application. The memory management method may include the following steps.
[0136] Step S61: Receive a memory allocation request from the kernel module. The memory allocation request includes memory size information and kernel module identification information. This is the same as step S31 above.
[0137] Step S62: Allocate a first page of memory to the kernel module in the physical memory.
[0138] Step S63: Generate a first node of memory association; record identification information in the first node.
[0139] In the technical solution provided in the embodiment of the present application, the memory management system can adopt a buddy system algorithm to apply for a large memory in the physical memory, and realize the association of the identification information with the first memory by generating a node and recording the identification information in the node. Compared with allocating one more page of memory in the physical memory to record the identification information, memory space is saved.
[0140] In the above step S62 , the memory management system may allocate a memory having a first number of pages in the physical memory to the kernel module as the first memory.
[0141] In step S63, the memory management system may determine a free memory block in the physical memory, generate a node as a first node using the determined free memory block, and associate the first node with the first memory. For example, the memory management system may record the address of the first memory block in the first node to associate the first node with the first memory block.
[0142] After generating the first node, the memory management system may record the identification information in the first node and associate the identification information with the first memory through the first node.
[0143] In an embodiment of the present application, the kernel module can pass the memory allocation request into the memory management system through the second interface function (such as the alloc_pages_mid function). The function prototype of the first interface function may include a function name (such as alloc_pages_mid), a function variable indicating the first number of pages (such as the order variable) and the data type of the variable (such as unsigned int), and a function variable indicating identification information (such as the mid variable) and the data type of the variable (such as unsigned int). The function prototype of the second interface function may also include a function variable indicating mask information (such as the gfp_mask variable) and the data type of the variable (such as gfp_t). The function prototype of the second interface function can be expressed as: struct page*alloc_page_mid(gfp_t gfp_mask, unsigned int order, unsigned int mid).
[0144] The memory management system can execute the second interface function based on the information indicated by each function variable of the second interface function (such as the first page number and identification information, etc.), allocate memory to the kernel module, generate a first memory-associated node, and record the identification information in the first node.
[0145] In an embodiment of the present application, a memory release request is a request from a kernel module to release memory, including the address of the memory to be released. The memory management system can receive a memory release request from any kernel module and release the memory based on the address of the memory included in the memory release request. The kernel module that sends the memory release request can be the same as or different from the kernel module that sends the memory allocation request, and this is not limited.
[0146] Taking the example of a memory release request including the address of the first memory, the memory management system can receive the memory release request sent by any kernel module, determine the address of the first memory included in the memory release request, and release the first memory at the determined address. In an embodiment of the present application, the memory management system can also find the first node that records the address of the first memory in the generated nodes and delete the first node.
[0147] In some embodiments, when a first memory is abnormal, the memory management system can determine the first node associated with the first memory and output a prompt message based on the identification information recorded in the first node. That is, the memory management system can search for the first node that records the address of the first memory among the generated nodes, obtain the recorded identification information from the first node, and output a prompt message including the obtained identification information. In this way, developers and maintenance personnel can determine the kernel module to which the abnormal memory belongs based on the identification information, and then the responsible team of the kernel module can maintain the kernel module.
[0148] In some embodiments, the memory management system can mount the generated nodes through a linked list array to facilitate node management. The linked list array includes multiple linked lists, and each page number corresponds to a linked list, which is used to mount the memory-associated nodes of the page number. For example, 1 page corresponds to linked list 0, which is used to mount the memory-associated nodes of 1 page; 2 pages correspond to linked list 1, which is used to mount the memory-associated nodes of 2 pages, and so on. The memory management system can manage 11 linked lists from linked list 0 to linked list 10, respectively mounting the memory-associated nodes of 1, 2, 4, 8...1024 pages, and store these 11 linked lists in free memory.
[0149] In an embodiment of the present application, the memory management system can manage the first linked list corresponding to the first page number, and the memory management system generates the first node of the first memory association through the following steps: mounting the first node at the end of the first linked list; and recording the address of the memory at the first node.
[0150] In an embodiment of the present application, the node mounted under each linked list records the address, identification information, and node address of the associated memory (i.e., the address of the next node to be mounted). The memory management system can generate a first node and mount the first node at the end of the linked list corresponding to the first page number (i.e., the first linked list), and update the node address recorded in the last node of the first linked list to the address of the first node. After mounting the first node, the memory management system can record the address of the first memory at the first node.
[0151] In some embodiments, after releasing the first memory, the memory management system may also delete the first node associated with the first memory through the following steps: determining the first node in the first linked list based on the first page number of pages included in the memory and the address of the memory; and deleting the first node.
[0152] After releasing the first memory, the memory management system can determine the number of pages of the released first memory (i.e., the first page number) and determine a linked list corresponding to the first page number (i.e., the first linked list). The memory management system can traverse the nodes mounted in the first linked list, find the first node that records the address of the first memory, delete the first node, and update the node address recorded in the previous node of the first node to the address of the next node of the first node.
[0153] By implementing node management through a linked list, the memory management system can, when a first memory exception occurs, more quickly find the first node recording the address of the first memory through the first linked list corresponding to the first page number of the first memory, determine the identification information belonging to the first memory, and output prompt information, thereby improving the speed of determining the identification information and the efficiency of abnormal diagnosis.
[0154] In an embodiment of the present application, the memory management system can also transmit a memory release request to the memory management system through an interface function. The memory management system can execute the interface function based on the address indicated by the function variable of the interface function, release the memory, and delete the first node.
[0155] In an embodiment of the present application, the memory management system can perform locking operations on each linked list, that is, for each linked list, the memory management system can only mount and delete one node at a time, ensuring the consistency and accuracy of node management.
[0156] The following combination Figure 7 and Figure 8 The memory management method provided in the embodiments of the present application is described in detail.
[0157] In the embodiment of the present application, an interface function for applying for a small memory (ie, a first interface function), an interface function for applying for a large memory (ie, a second interface function), and a MID field for carrying identification information are added.
[0158] The function prototype of the interface function for requesting a small memory in the embodiment of the present application includes three function variables: a function variable indicating the memory length (i.e., the first length), a function variable indicating the MID field, and a function variable indicating flag information. The function prototype is: void*kmalloc_mid(size_t size, gfp_t flags, unsignedint mid). In contrast, in the original interface function for requesting a small memory (such as kmalloc), the function prototype does not include a function variable indicating the MID field, and is: void*kmalloc(size_t size, gfp_t flags).
[0159] The function prototype of the interface function for applying for a large memory in the embodiment of the present application includes three function variables, namely, a function variable indicating the number of memory pages (i.e., the first page number), a function variable indicating the MID field, and a function variable indicating mask information. The function prototype is: struct page*alloc_page_mid(gfp_t gfp_mask, unsigned int order, unsigned int mid). In the original interface function for applying for a large memory (such as alloc_pages), the function prototype does not include a function variable indicating the MID field, and is: struct page*alloc_pages(gfp_t gfp_mask, unsigned int order).
[0160] The MID field is 4 bytes long and can be divided into three parts: a header magic field, an identification field (such as the first-level module ID field, the second-level module ID field, and the third-level module ID field), and a tail magic field. The header magic field and tail magic field can carry the header magic word (i.e., the first magic word) (e.g., 1010) and the tail magic word (i.e., the second magic word) (e.g., 0101), respectively. The first-level module ID field in the MID field corresponds to the department to which the memory belongs, the second-level module ID field indicates the group within the department to which the memory belongs, and the second-level module ID field indicates the kernel module within the group to which the memory belongs. When a memory problem (such as a memory leak or memory anomaly) occurs, the memory management system can use the MID field to help locate the memory problem, find the kernel module to which it belongs, and the department and group to which the kernel module belongs. This allows for clear analysis and confirmation, facilitating the identification of the responsible team for the memory problem and helping users pinpoint the problem.
[0161] The kernel module requests memory allocation from the memory management system through an interface function. Depending on the interface function used, the memory management system can use the Slab memory management algorithm or the Buddy System algorithm to allocate memory to the kernel module.
[0162] 1) The kernel module applies for memory through the interface function kmalloc_mid. The memory management system obtains the length of the memory applied for through the interface function kmalloc_mid as follows: the length indicated by the size variable (i.e., the first length) + 4 bytes. Among them, the length indicated by the size variable is the memory length actually used by the user, and the corresponding memory is located at the head of the data area; 4 bytes is the length of the MID field, and the corresponding memory is located at the end of the data area. Taking the application of 8 bytes of memory as an example, Figure 7 As shown, the memory management system adopts the Slab memory management algorithm, applies for multiple 4KB pages through the buddy system algorithm, and obtains the length of the applied memory as 8+4=12 bytes. According to the Slab memory ladder alignment, the actual applied memory length is 16 bytes. Adding the remaining control area (i.e., 44 bytes), it is determined that a page needs to be cut into 60 (i.e., 16+44) bytes, among which the data area is 16 bytes, the first 12 bytes of the data area (i.e., the first 8 bytes of the 12 bytes) store user data, and the last 4 bytes of the data area store the MID field.
[0163] In this case, the memory management system can detect discrepancies between the magic field value and the magic word during memory allocation and release, allowing timely detection of memory issues. If the leading magic word is incorrect, the problem is likely with the preceding memory; if the trailing magic field is incorrect, the problem is likely with the following memory. Using the magic word, the memory management system can promptly identify memory anomalies and help users locate the problem.
[0164] 2) The kernel module applies for memory through the interface function alloc_page_mid. The memory management system can record the MID field in the node and record the node through the global hash list. According to the number of pages allocated by the buddy system algorithm, the memory management system can define a global hash list array pages_mid
[11] with an array length of 11, that is, including 11 lists from pages_mid[0] to pages_mid
[10] , and the number of pages is the power of 2 as the subscript of the list. Figure 8 As shown, linked lists 0 to 10 are linked list heads. The node mounted on linked list 0 (i.e. pages_mid[0]) is used to record the address and MID field of the memory requested when applying for 1 page of memory. The node mounted on linked list 1 (i.e. pages_mid[1]) is used to record the address and MID field of the memory requested when applying for 2 pages of memory. And so on. The node mounted on linked list 10 (i.e. pages_mid
[10] ) is used to record the address and MID field of the memory requested when applying for 1024 pages of memory.
[0165] The memory management system also defines a structure for each node (such as strcut pages_mid_s), which includes: a pointer indicating the memory address (such as *page), with a data type of struct page; a variable indicating the MID field (such as mid), with a data type of unsigned int; and a pointer indicating the node address (such as *next), with a data type of void. The structure is as follows: struct pages_mid_s{struct page*page; unsigned int mid; void*next;}; structpages_mid_s pages_mid
[11] .
[0166] The memory management system uses the interface function alloc_page_mid to obtain the number of pages of memory requested: 2 raised to the power of the order variable (i.e., the first page number). Using the buddy system algorithm, the memory management system requests this number of pages of memory and, based on this number of pages, determines the corresponding linked list header. It then generates a node and mounts it at the end of the linked list, thereby recording the MID field and the address of the requested memory. After releasing memory, the memory management system calculates the corresponding linked list header based on the number of released pages and then traverses the linked list to delete nodes.
[0167] In the embodiment of the present application, the self-developed kernel module can use the kmalloc_mid function or the alloc_page_mid function to apply for memory. In order to ensure that the memory applied for by the Linux native module remains unchanged, the Linux native module still uses the kmalloc function or the alloc_page function to apply for memory.
[0168] In the technical solution provided by the embodiment of the present application, the memory management system adopts the corresponding interface function when applying for a small memory or a large memory, and can directly confirm the kernel module to which the memory belongs. Due to the simple data structure and linked list structure, when applying for a large memory, the speed of applying for and releasing corresponding additional operations (i.e., adding and deleting linked list nodes) can be improved. In addition, through the reuse of the MID field, users can be helped to perform error and exception diagnosis, such as memory leaks, memory anomalies, and memory damage.
[0169] Corresponding to the above memory management method, the present application embodiment provides a memory management device, see Figure 9 , applied to a memory management system, the device includes:
[0170] A receiving unit 91 is configured to receive a memory allocation request sent by a kernel module, wherein the memory allocation request includes memory size information and identification information of the kernel module;
[0171] an allocation unit 92 for allocating memory for the kernel module in the physical memory according to the memory size information;
[0172] The recording unit 93 is used to record the identification information at the associated location in the memory.
[0173] In the technical solution provided in the embodiment of the present application, the memory management system receives a memory allocation request from a kernel module, and the memory allocation request includes identification information of the kernel module. After allocating memory to the kernel module according to the memory size information, the memory management system can record the identification information at a location associated with the allocated memory. By applying the technical solution provided in the embodiment of the present application, the identification information is recorded at a location associated with the allocated memory, and the identification information is associated with the allocated memory. In this way, the memory management system can clearly identify the kernel module to which the memory belongs. When an abnormality occurs in the memory, it is easy to locate the kernel module to which the abnormal memory belongs, so as to determine the responsible team for maintenance of the kernel module.
[0174] In some embodiments, the memory size information may include a first length; the allocation unit 92 may be specifically configured to obtain a second length of the identification information; and allocate memory of a third length for the kernel module in the physical memory, the third length being greater than or equal to the sum of the first length and the second length;
[0175] The recording unit 93 may be specifically configured to record identification information at a preset location in the memory, where the length of the preset location is the second length.
[0176] In some embodiments, the preset location may be located at the end of the memory.
[0177] In some embodiments, the device may further include: an output unit, configured to output prompt information according to identification information recorded at a preset location when the memory is abnormal.
[0178] In some embodiments, the memory size information may include a first page number; the allocation unit 92 may specifically allocate the first page number of memory to the kernel module in the physical memory;
[0179] The recording unit 93 may be specifically configured to generate a first node associated with the memory; and record identification information in the first node.
[0180] In some embodiments, the memory management system can manage a first linked list corresponding to the first page number; the recording unit can be specifically used to mount a first node at the end of the first linked list; and record the address of the memory at the first node.
[0181] In some embodiments, the device may also include: a release unit for receiving a memory release request, the memory release request including the address of the memory; releasing the memory according to the address of the memory; determining the first node in the first linked list according to the first page number of pages included in the memory and the address of the memory; and deleting the first node.
[0182] In some embodiments, the device may further include: an output unit, configured to determine a first node associated with the memory when the memory is abnormal; and output prompt information based on identification information recorded in the first node.
[0183] In some embodiments, the identification information may include at least one of the following identification fields: a first identification field, used to carry the identification of the department to which the kernel module belongs; a second identification field, used to carry the identification of the group to which the kernel module belongs; and a third identification field, used to carry the identification to which the kernel module belongs.
[0184] In some embodiments, the identification information may further include at least one of the following magic fields: a first magic field located at the head of the identification information and used to carry a first magic word; and a second magic field located at the tail of the identification information and used to carry a second magic word.
[0185] In some embodiments, the identification information may include a first magic field and a second magic field, and the identification information may be located at a preset position in the memory; the device may further include: a determination unit, configured to determine that a memory in front of the first magic field in the memory is abnormal when the value carried by the first magic field is different from the first magic word; and to determine that a memory in back of the second magic field in the memory is abnormal when the value carried by the second magic field is different from the second magic word.
[0186] The present application also provides an electronic device, such as Figure 10 As shown, a memory management system is running on the electronic device, and the electronic device includes a processor 101, a communication interface 102, a memory 103 and a communication bus 104, wherein the processor 101, the communication interface 102 and the memory 103 communicate with each other through the communication bus 104;
[0187] Memory 103, for storing computer programs;
[0188] The processor 101 is configured to implement any of the above-mentioned steps of the memory management method when executing the program stored in the memory 103 .
[0189] The communication bus mentioned in the electronic device mentioned above may be a Peripheral Component Interconnect (PCI) bus or an Extended Industry Standard Architecture (EISA) bus. This communication bus can be divided into an address bus, a data bus, a control bus, etc. For ease of illustration, only one thick line is used in the figure, but this does not mean that there is only one bus or only one type of bus.
[0190] The communication interface is used for communication between the above electronic device and other devices.
[0191] The memory may include random access memory (RAM) or non-volatile memory (NVM), such as at least one disk storage. Alternatively, the memory may be at least one storage device located away from the processor.
[0192] The above-mentioned processor can be a general-purpose processor, including a central processing unit (CPU), a network processor (NP), etc.; it can also be a digital signal processor (DSP), an application-specific integrated circuit (ASIC), a field-programmable gate array (FPGA) or other programmable logic devices, discrete gate or transistor logic devices, and discrete hardware components.
[0193] In another embodiment provided by the present application, a computer-readable storage medium is further provided, wherein a computer program is stored in the computer-readable storage medium, and when the computer program is executed by a processor, any of the above-mentioned memory management methods is implemented.
[0194] In another embodiment provided by the present application, a computer program product including instructions is also provided, which, when executed on a computer, enables the computer to execute the steps of any memory management method in the above embodiments.
[0195] In the above embodiments, it can be implemented in whole or in part by software, hardware, firmware or any combination thereof. When software is used for implementation, it can be implemented in whole or in part in the form of a computer program product. The computer program product includes one or more computer instructions. When the computer program instructions are loaded and executed on a computer, the process or function described in the embodiment of the present application is generated in whole or in part. The computer can be a general-purpose computer, a special-purpose computer, a computer network, or other programmable device. The computer instructions can be stored in a computer-readable storage medium or transmitted from one computer-readable storage medium to another computer-readable storage medium. For example, the computer instructions can be transmitted from a website, computer, server or data center to another website, computer, server or data center via a wired (e.g., coaxial cable, optical fiber, digital subscriber line (DSL)) or wireless (e.g., infrared, wireless, microwave, etc.) method. The computer-readable storage medium can be any available medium that a computer can access or a data storage device such as a server or data center that includes one or more available media integrations. The available medium can be a magnetic medium (e.g., a floppy disk, a hard disk, a tape), an optical medium (e.g., a DVD), or a semiconductor medium (e.g., a solid-state drive (SSD)).
[0196] It should be noted that, in this document, relational terms such as first and second, etc., are used only to distinguish one entity or operation from another entity or operation, and do not necessarily require or imply the existence of any such actual relationship or order between these entities or operations. Moreover, the terms "comprises," "comprising," or any other variants thereof are intended to cover non-exclusive inclusion, so that a process, method, article, or device comprising a series of elements includes not only those elements, but also other elements not explicitly listed, or elements inherent to such process, method, article, or device. In the absence of further limitations, an element defined by the phrase "comprising a ..." does not exclude the presence of other identical elements in the process, method, article, or device comprising the element.
[0197] Each embodiment in this specification is described in a related manner. Similar portions between the various embodiments can be referenced to each other. Each embodiment focuses on the differences between the other embodiments. In particular, since the device, electronic device, computer storage medium, and computer program product embodiments are generally similar to the method embodiments, their descriptions are relatively simple. For related portions, reference can be made to the descriptions of the method embodiments.
[0198] The above description is only a preferred embodiment of the present application and is not intended to limit the scope of protection of the present application. Any modifications, equivalent replacements, improvements, etc. made within the spirit and principles of the present application are included in the scope of protection of the present application.
Claims
1. A memory management method, characterized in that: Applied to a memory management system, the method includes: receiving a memory allocation request sent by a kernel module, wherein the memory allocation request includes memory size information and identification information of the kernel module; Allocating memory for the kernel module in physical memory according to the memory size information; The identification information is recorded at a location associated with the memory.
2. The method according to claim 1, characterized in that The memory size information includes a first length; and the step of allocating memory for the kernel module in the physical memory according to the memory size information includes: Obtaining a second length of the identification information; In the physical memory, allocating a memory of a third length for the kernel module, where the third length is greater than or equal to the sum of the first length and the second length; The step of recording the identification information at the associated location in the memory includes: The identification information is recorded at a preset position in the memory, and the length of the preset position is a second length.
3. The method according to claim 2, characterized in that The preset position is located at the end of the memory.
4. The method according to claim 2 or 3, characterized in that The method further comprises: When the memory is abnormal, prompt information is output according to the identification information recorded at the preset location.
5. The method according to claim 1, wherein The memory size information includes a first page number; and the step of allocating memory for the kernel module in the physical memory according to the memory size information includes: In the physical memory, allocating the first number of pages of memory to the kernel module; Recording the identification information at the location associated with the memory includes: generating a first node of the memory association; The identification information is recorded in the first node.
6. The method according to claim 5, characterized in that The memory management system manages a first linked list corresponding to the first number of pages; The step of generating the first node of the memory association includes: Mounting the first node at the end of the first linked list; The address of the memory is recorded at the first node.
7. The method according to claim 6, characterized in that The method further comprises: receiving a memory release request, wherein the memory release request includes an address of the memory; Release the memory according to the address of the memory; Determining the first node in the first linked list according to the first number of pages included in the memory and the address of the memory; Delete the first node.
8. The method according to any one of claims 5 to 7, characterized in that: The method further comprises: When the memory is abnormal, determining the first node associated with the memory; Output prompt information according to the identification information recorded in the first node.
9. The method according to claim 1, characterized in that The identification information includes at least one of the following identification fields: A first identification field, used to carry an identification of the department to which the kernel module belongs; A second identification field is used to carry an identification of the group to which the kernel module belongs; The third identification field is used to carry the identification of the kernel module.
10. The method according to claim 9, characterized in that The identification information also includes at least one of the following magic fields: A first magic field, located at the head of the identification information, is used to carry a first magic word; The second magic field is located at the end of the identification information and is used to carry a second magic word.
11. The method according to claim 10, characterized in that The identification information includes the first magic field and the second magic field, and the identification information is located at a preset position in the memory; the method further includes: When the value carried by the first magic field is different from the first magic word, determining that the memory preceding the first magic field in the memory is abnormal; When the value carried by the second magic field is different from the second magic word, it is determined that the memory behind the second magic field in the memory is abnormal.
12. A memory management device, characterized in that: Applied to a memory management system, the device comprises: a receiving unit, configured to receive a memory allocation request sent by a kernel module, wherein the memory allocation request includes memory size information and identification information of the kernel module; an allocation unit, configured to allocate memory for the kernel module in the physical memory according to the memory size information; A recording unit is used to record the identification information at a location associated with the memory.
13. The device according to claim 12, characterized in that The memory size information includes a first length; the allocating unit is specifically configured to obtain a second length of the identification information; in the physical memory, allocate memory of a third length for the kernel module, the third length being greater than or equal to the sum of the first length and the second length; the recording unit is specifically configured to record the identification information at a preset location in the memory, the length of the preset location being the second length; and / or, The preset position is located at the end of the memory; and / or, The device further includes: an output unit, configured to output prompt information according to the identification information recorded at the preset location when the memory is abnormal; and / or, The memory size information includes a first page number; the allocation unit is specifically used to allocate the first page number of memory to the kernel module in the physical memory; the recording unit is specifically used to generate a first node associated with the memory; and record the identification information in the first node; and / or, The memory management system manages a first linked list corresponding to the first page number; the recording unit is specifically configured to mount a first node at the end of the first linked list; and record the address of the memory at the first node; and / or, The apparatus further includes: a release unit configured to receive a memory release request, the memory release request including the address of the memory; release the memory according to the address of the memory; determine the first node in the first linked list according to the first number of pages included in the memory and the address of the memory; delete the first node; and / or, The device further includes: an output unit, configured to determine the first node associated with the memory when the memory is abnormal; output prompt information according to the identification information recorded in the first node; and / or, The identification information includes at least one of the following identification fields: a first identification field for carrying the identification of the department to which the kernel module belongs; a second identification field for carrying the identification of the group to which the kernel module belongs; a third identification field for carrying the identification to which the kernel module belongs; and / or, The identification information further includes at least one of the following magic fields: a first magic field located at the head of the identification information and used to carry a first magic word; a second magic field located at the tail of the identification information and used to carry a second magic word; and / or, The identification information includes the first magic field and the second magic field, and the identification information is located at a preset position in the memory. The apparatus further includes: a determining unit configured to determine that a memory preceding the first magic field in the memory is abnormal when a value carried by the first magic field is different from the first magic word; and to determine that a memory following the second magic field in the memory is abnormal when a value carried by the second magic field is different from the second magic word.
14. An electronic device, characterized in that: The processor, the communication interface, the memory and the communication bus are connected to each other via the communication bus. Memory for storing computer programs; A processor, configured to implement the method according to any one of claims 1 to 11 when executing a program stored in a memory.
15. A computer-readable storage medium, characterized in that The computer-readable storage medium stores a computer program, and when the computer program is executed by a processor, the method according to any one of claims 1 to 11 is implemented.
Citation Information
Patent Citations
Kernel module loading method and device
CN106815031A
Program dynamic memory leak-proof detection and tracking method based on head and tail flag bits
CN118034908A
System and method for finding kernel memory leaks
US20090013145A1