A memory application method for avoiding OOM
By introducing mem system calls into the Linux kernel state to manage physical memory applications, the problem of OOM in Linux operating system when memory is insufficient is solved, and more stable memory management is achieved.
Patent Information
- Application Number
- CN202510599992.2
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2025-05-12
- Publication Date
- 2025-08-01
- Estimated Expiration
- 2045-05-12
AI Technical Summary
Linux operating systems are prone to OOM when there is insufficient memory, causing crashes of important applications. The existing technology reserves memory or frequent checks on memory usage affect system performance.
Introduce mem system calls in the kernel state, manage physical memory applications through device node /dev/memX, return label X or error code, and the application adjusts the memory application size according to the error code to avoid OOM.
It effectively avoids the occurrence of OOM when there is insufficient memory, optimizes the memory application process, reduces system resource waste, and improves system stability.
Smart Images

Figure CN120144317B_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the field of Linux memory application, and particularly to a memory application method for avoiding OOM. Background Art
[0002] "Out Of Memory" is also abbreviated as OOM. The reason for OOM in the current Linux operating system is that when an application applies for memory, it only gets virtual memory, and physical memory is allocated during a page fault exception only when physical memory is used. There may be a situation of insufficient memory when allocating physical memory, which will cause the system to have OOM, resulting in the crash of some important applications. Therefore, in many cases, applying for memory when the memory is insufficient will trigger OOM. Currently, for this situation, the Linux operating system generally reserves memory first, which reduces the available memory of the operating system; there are also some that frequently check the memory storage to determine whether to allow memory application, which requires a large amount of calculation and saving of the memory usage situation, and affects the operation of the operating system to a certain extent. Summary of the Invention
[0003] In view of this, the present invention provides a memory application method for avoiding OOM. By adding a new system call mem, the function of this system call is to be embedded into the kernel state to allocate memory in the kernel state. If successful, a device node / dev / memX is created and the label X is returned; if failed, an error is returned. Then, in the application, the device node / dev / memX is opened and mapped. If the memory application fails, the application can decide whether to apply for a smaller amount of memory again through the error code, and apply for memory of a suitable size in the case of insufficient memory to avoid the occurrence of OOM.
[0004] To achieve the above object, the present invention provides a memory application method for avoiding OOM, including:
[0005] S1. Based on the Linux kernel, perform pre-processing in the kernel state to establish a mem system call to obtain the application status of physical memory calls;
[0006] S2. According to the application status of the physical memory calls, perform user-state memory allocation processing to complete the memory application.
[0007] Preferably, the performing pre-processing in the kernel state based on the Linux kernel to establish a mem system call to obtain the application status of physical memory calls includes:
[0008] S1-1. Based on the Linux kernel, establish variables mem_flags and a new mem system call respectively;
[0009] S1-2. Use the newly added mem system call to obtain the physical memory call application status according to the variable mem_flags.
[0010] Further, using the newly added mem system call to obtain the physical memory call application status according to the variable mem_flags includes:
[0011] S1-2-1. Use the newly added mem system call to establish a device node / dev / memX bound to the physical memory according to the variable mem_flags;
[0012] S1-2-2. Determine whether the current device node / dev / memX is successfully established. If so, return the label X of the device node / dev / memX as the physical memory call application status. Otherwise, return an error code as the physical memory call application status;
[0013] Among them, the variable mem_flags is 256 bits.
[0014] Further, using the newly added mem system call to establish a device node / dev / memX bound to the physical memory according to the variable mem_flags includes:
[0015] S1-2-1-1. Determine whether both the physical memory and the virtual memory are continuous. If so, execute S1-2-1-2. Otherwise, perform physical memory allocation processing on the variable mem_flags based on the mmmalloc function according to the free label X to directly obtain the physical memory allocation result;
[0016] S1-2-1-2. Determine whether the free label of the memory device obtained by the newly added mem system call based on the Linux kernel state according to the variable mem_flags exists. If so, set the free label X of the memory device to 1. Otherwise, return ENFILE;
[0017] S1-2-1-3. When the return value corresponding to the physical memory allocation result is ENOMEM, the physical memory allocation fails, and return ENOMEM;
[0018] S1-2-1-4. When the return value corresponding to the physical memory allocation result is the structure mmm_struct, the physical memory allocation is successful, and use the structure mmm_struct to perform private member record processing to establish the device node / dev / memX.
[0019] Further, performing user-state memory allocation processing according to the physical memory call application status to complete the memory application includes:
[0020] S2-1. Establish the open system call, mmap system call, munmap system call, close system call, and memfree system call respectively based on the Linux kernel;
[0021] S2-2. Use the mem system call to obtain the remaining available callable memory of the memory device;
[0022] S2-3. Use the open system call and mmap system call to perform the starting process of memory allocation according to the remaining available callable memory;
[0023] S2-4. Use the munmap system call and close system call to perform the termination process of memory allocation;
[0024] S2-5. Use the memfree system call to perform the memory release process to complete the memory application.
[0025] Furthermore, using the mem system call to obtain the remaining available callable memory of the memory device includes:
[0026] S2-2-1. Use the mem system call to perform the memory call application process to obtain the initial memory call application result;
[0027] S2-2-2. Determine whether the return result of the mem system call corresponding to the initial memory call application result is ENFILE. If so, end the memory call application. Otherwise, execute S2-2-3;
[0028] S2-2-3. Determine whether the memory call application result at the current moment is successful. If so, return the free label X of the device node / dev / memX. Otherwise, execute S2-2-4;
[0029] S2-2-4. Perform a re-memory call application according to the memory call application result at the current moment to obtain an updated memory call application result, and return to S2-2-3;
[0030] Among them, the capacity of the memory call application is 2 n , 2 n which is not greater than the capacity upper limit of the memory device, and the capacity of the re-memory call application is 2 n-1 .
[0031] Furthermore, using the open system call and mmap system call to perform the starting process of memory allocation according to the remaining available callable memory includes:
[0032] Based on the kernel mode, use the open system call to open the device node / dev / memX, and obtain the corresponding open member function mem_open according to the file operation method set file_operations;
[0033] Based on the kernel mode, use the mmap system call to open the device node / dev / memX, and obtain the corresponding mmap member function mem_mmap according to the file operation method set file_operations;
[0034] After increasing the f_count member quantity of the file operation method set file_operations by one using the open member function mem_open, obtain the file descriptor fd;
[0035] Use the mmap member function mem_mmap to obtain the corresponding mmm_struct according to the device node / dev / memX;
[0036] Use the corresponding mmm_struct to perform continuous mapping processing on the physical memory of the corresponding mmm_struct to the virtual address based on the remap_pfn_range function to complete the starting processing of memory allocation.
[0037] Furthermore, using the munmap system call and the close system call for memory allocation termination processing includes:
[0038] Based on the kernel mode, use the munmap system call to call the device node / dev / memX and obtain the corresponding munmap member function mem_munmap according to the file operation method set file_operations;
[0039] Based on the kernel mode, use the close system call to call the device node / dev / memX and obtain the corresponding close member function mem_close according to the file operation method set file_operations;
[0040] Use the munmap member function mem_munmap to obtain the corresponding mmm_struct according to the device node / dev / memX;
[0041] Traverse the structure virtual memory area struct vm_struct *area according to the corresponding mmm_struct to obtain the file descriptor fd of the structure virtual memory area struct vm_struct *area;
[0042] Cancel the file descriptor fd of the struct vm_struct *area of the structure virtual memory area to complete the termination processing of memory allocation;
[0043] After reducing the f_count member quantity of the file operation method set file_operations by one using the corresponding close member function mem_close, complete the termination processing of memory allocation.
[0044] Furthermore, using the memfree system call to perform memory release processing to complete memory application includes:
[0045] S2-5-1. Determine whether the f_count member quantity corresponding to the device node / dev / memX in the kernel state is 0. If so, obtain all the struct mmm_struct according to the device node / dev / memX;
[0046] S2-5-2. After calling the mmfree function to perform memory release processing according to all the struct mmm_struct, cancel the corresponding device node / dev / memX, and set the free label X of the memory device corresponding to the device node / dev / memX to 0;
[0047] S2-5-3. Determine whether the physical memory is continuous. If so, release the memory based on the kernel memory function kfree and then exit the kernel memory function kfree. Otherwise, obtain the area corresponding to the struct mmm_struct according to the struct mmm_struct and execute S2-5-4;
[0048] S2-5-4. After performing traversal processing using the area corresponding to the struct mmm_struct, perform release processing based on the free_pages function to complete memory application.
[0049] The beneficial effects of the present invention are:
[0050] It avoids the possibility of triggering OOM due to insufficient memory after applying for memory; in case of insufficient memory, when applying for large memory, an error can be returned, and the size of the applied memory can be adjusted according to the error code; in case of severe memory shortage, the operation of applying for memory can be stopped to avoid the occurrence of OOM. BRIEF DESCRIPTION OF THE DRAWINGS
[0051] The drawings constituting a part of the present invention are used to provide a further understanding of the present invention. The schematic embodiments of the present invention and their descriptions are used to explain the present invention and do not constitute an improper limitation to the present invention. In the drawings:
[0052] Figure 1It is the overall flowchart of a memory application method for avoiding OOM provided by the present invention;
[0053] Figure 2 It is the preliminary preparation flowchart of a memory application method for avoiding OOM provided by the present invention;
[0054] Figure 3 It is the call flowchart of the mem system call of a memory application method for avoiding OOM provided by the present invention;
[0055] Figure 4 It is the starting flowchart of the user state process of a memory application method for avoiding OOM provided by the present invention;
[0056] Figure 5 It is the ending flowchart of the user state process of a memory application method for avoiding OOM provided by the present invention;
[0057] Figure 6 It is the call flowchart of the mmap system call of a memory application method for avoiding OOM provided by the present invention. Specific embodiments
[0058] It should be noted that, without conflict, the embodiments in the present invention and the features in the embodiments can be combined with each other.
[0059] To make the objectives, technical solutions and advantages of the embodiments of the present invention clearer, the technical solutions in the embodiments of the present invention will be clearly and completely described below with reference to the accompanying drawings in the embodiments of the present invention. Obviously, the described embodiments are some, but not all, of the embodiments of the present invention. All other embodiments obtained by those of ordinary skill in the art based on the embodiments of the present invention without creative efforts shall fall within the protection scope of the present invention.
[0060] The present invention will be described in detail below with reference to the drawings and in conjunction with the embodiments.
[0061] Embodiment
[0062] The present invention provides a memory application method for avoiding OOM, as Figure 1 shown, including:
[0063] S1. Based on the Linux kernel, perform preliminary processing in the kernel state to establish a mem system call to obtain the physical memory call application status;
[0064] S2. Perform user state memory allocation processing according to the physical memory call application status to complete the memory application.
[0065] S1 specifically includes:
[0066] S1-1. Establish variables mem_flags and a new mem system call respectively based on the Linux kernel;
[0067] S1-2. Use the new mem system call to obtain the physical memory call application status according to the variable mem_flags.
[0068] S1-2 specifically includes:
[0069] S1-2-1. Use the new mem system call to establish a device node / dev / memX bound to the physical memory according to the variable mem_flags;
[0070] S1-2-2. Determine whether the current device node / dev / memX is successfully established. If so, return the label X of the device node / dev / memX as the physical memory call application status. Otherwise, return an error code as the physical memory call application status;
[0071] Among them, the variable mem_flags is 256 bits.
[0072] S1-2-1 specifically includes:
[0073] S1-2-1-1. Determine whether both the physical memory and the virtual memory are continuous. If so, execute S1-2-1-2. Otherwise, perform physical memory allocation processing on the variable mem_flags based on the mmmalloc function according to the free label X to directly obtain the physical memory allocation result;
[0074] S1-2-1-2. Determine whether the free label of the memory device obtained by the new mem system call based on the Linux kernel state according to the variable mem_flags exists. If so, set the free label X of the memory device to 1. Otherwise, return ENFILE;
[0075] S1-2-1-3. When the return value corresponding to the physical memory allocation result is ENOMEM, the physical memory allocation fails, and return ENOMEM;
[0076] S1-2-1-4. When the return value corresponding to the physical memory allocation result is the structure mmm_struct, the physical memory allocation is successful, and use the structure mmm_struct to perform private member record processing to establish the device node / dev / memX.
[0077] In this embodiment, a memory application method for avoiding OOM, the preliminary preparation is as Figure 2 shown, including:
[0078] 1. Create a 256-bit variable mem_flags in the Linux kernel to record the usage of / dev / memX;
[0079] 2. Add a new system call mem in the Linux kernel to allocate physical memory in kernel mode, create a device node / dev / memX bound to the physical memory, and return label X if successful; return an error code if failed;
[0080] 3. Add a new system call memfree in the Linux kernel to check the usage of / dev / memX, decide whether to unregister the device node / dev / memX, set the mem_flags variable to zero, and release the physical memory;
[0081] 4. Add a new function mmmalloc in the Linux kernel to allocate memory with discontinuous physical memory but continuous virtual memory;
[0082] 5. Add a new function mmfree in the Linux kernel to release the memory allocated by mmmalloc;
[0083] Prepare a set of methods for / dev / memX in the Linux kernel, including open, mmap, close, and munmap.
[0084] In this embodiment, a memory application method for avoiding OOM, the specific call process of the mem system call is as Figure 3 shown, including:
[0085] 1. When the mem system call enters the kernel mode, find the free label X according to the mem_flags variable, and return -ENFILE if the free label X cannot be found;
[0086] 2. If the free label X is found, set the X-th bit of mem_flags to 1;
[0087] 3. Allocate physical memory using the mmmalloc function;
[0088] 4. If the allocation fails (the return value of mmmalloc is -ENOMEM), return -ENOMEM;
[0089] 5. If the allocation is successful (the return value of mmmalloc is a structure mmm_struct, a positive number), create a device node / dev / memX;
[0090] 6. Record the memory mmm_struct allocated by mmmalloc in the private member of / dev / memX;
[0091] The system call of mem ends and returns to label X.
[0092] In this embodiment, a memory application method for avoiding OOM and the specific memory process allocated by mmmalloc include:
[0093] 1. First, use kmalloc to apply for memory. If the application is successful, calculate the physical address, record the starting physical address and size in the structure mmm_struct, and return the pointer to this structure;
[0094] 2. If the application fails, perform page alignment on the applied memory;
[0095] 3. Call the function __get_vm_area_node to allocate virtual addresses;
[0096] 4. If the allocation of physical memory fails, return -ENOMEM. If the allocation is successful, continue to run;
[0097] 5. Call the function __vmalloc_area_node to allocate physical addresses;
[0098] 6. If the allocation of physical memory fails, return -ENOMEM. If the allocation is successful, continue to run;
[0099] Traverse each member page of struct vm_struct *area, use page_to_phys to calculate the physical address of each physical page, record it in the structure mmm_struct, and return the pointer to this structure.
[0100] S2 specifically includes:
[0101] S2-1. Based on the Linux kernel, establish the open system call, mmap system call, munmap system call, close system call, and memfree system call respectively;
[0102] S2-2. Use the mem system call to obtain the remaining available callable memory of the memory device;
[0103] S2-3. Use the open system call and mmap system call to perform the starting process of memory allocation according to the remaining available callable memory;
[0104] S2-4. Use the munmap system call and close system call to perform the termination process of memory allocation;
[0105] S2-5. Use the memfree system call to perform memory release processing to complete the memory application.
[0106] S2-2 specifically includes:
[0107] S2-2-1. Use the mem system call to process the memory call application to obtain the initial memory call application result;
[0108] S2-2-2. Determine whether the return result of the mem system call corresponding to the initial memory call application result is ENFILE. If so, end the memory call application; otherwise, execute S2-2-3;
[0109] S2-2-3. Determine whether the current memory call application result is successful. If so, return the free label X of the device node / dev / memX; otherwise, execute S2-2-4;
[0110] S2-2-4. Perform a re-memory call application based on the current memory call application result to obtain an updated memory call application result, and return to S2-2-3;
[0111] Among them, the capacity of the memory call application is 2 n , 2 n which is not greater than the capacity upper limit of the memory device. The capacity of the re-memory call application is 2 n-1 .
[0112] In this embodiment, a memory application method for avoiding OOM. The user-mode process starts as Figure 4 shown, and includes:
[0113] 1. The application uses the mem system call to apply for 1M of memory and enters the mem system call for the call process;
[0114] 2. If mem returns -ENFILE, it means that the mem_flags are used up, and the memory application ends;
[0115] 3. If mem returns -ENOMEM, it means that the memory is insufficient. Re-use the mem system call to apply for a smaller amount of memory, such as 512K, and loop this step until the memory application is successful and return the label X;
[0116] 4. The application uses the open system call to open the device node / dev / memX according to the label X and enters the open system call for the call process, returning fd;
[0117] 5. The application uses the mmap system call to map the memory of the file descriptor fd and enters the mmap system call for the call process, returning the user-mode virtual address;
[0118] The application uses the memory.
[0119] The user-mode process ends as Figure 5 shown, including:
[0120] 1. After the application finishes using the memory, it makes a call using the mummap system call, enters the mummap system call process, and cancels the mapping relationship;
[0121] 2. The application closes / dev / memX through the system call close and enters the close system call process;
[0122] 3. The application releases the memory applied by mem through the system call memfree and enters the memfree system call process.
[0123] S2-3 specifically includes:
[0124] S2-3-1. Based on the kernel mode, use the open system call to open the device node / dev / memX and obtain the corresponding open member function mem_open according to the file operation method set file_operations;
[0125] S2-3-2. Based on the kernel mode, use the mmap system call to open the device node / dev / memX and obtain the corresponding mmap member function mem_mmap according to the file operation method set file_operations;
[0126] S2-3-3. After increasing the f_count member quantity of the file operation method set file_operations by one using the open member function mem_open, obtain the file descriptor fd;
[0127] S2-3-4. Use the mmap member function mem_mmap to obtain the corresponding mmm_struct according to the device node / dev / memX;
[0128] S2-3-5. Use the corresponding mmm_struct to perform continuous mapping processing on the physical memory of the corresponding mmm_struct to the virtual address based on the remap_pfn_range function to complete the initial memory allocation processing.
[0129] S2-4 specifically includes:
[0130] S2-4-1. Based on the kernel mode, use the munmap system call to call the device node / dev / memX and obtain the corresponding munmap member function mem_munmap according to the file operation method set file_operations;
[0131] S2-4-2. Invoke the device node / dev / memX based on the kernel state using the close system call, and obtain the corresponding close member function mem_close according to the file operation method set file_operations;
[0132] S2-4-3. Use the munmap member function mem_munmap to obtain the corresponding mmm_struct structure according to the device node / dev / memX;
[0133] S2-4-4. Traverse and process the structure virtual memory area struct vm_struct *area according to the corresponding mmm_struct structure to obtain the file descriptor fd of the structure virtual memory area struct vm_struct *area;
[0134] S2-4-5. Cancel the file descriptor fd of the structure virtual memory area struct vm_struct *area to complete the memory allocation termination process;
[0135] S2-4-6. After reducing the f_count member number of the file operation method set file_operations by one using the corresponding close member function mem_close, complete the memory allocation termination process.
[0136] In this embodiment, a memory application method for avoiding OOM, the starting process of the system call, and the specific call process of the open system call include:
[0137] 1. In the open system call, when entering the kernel state, it will call the open member function mem_open of the file operation method set file_operations of the character device of the device node / dev / memX;
[0138] 2. The mem_open function increments the f_count member of the file and returns the file descriptor fd;
[0139] The call process of the mmap system call is as Figure 6 shown, including:
[0140] 1. In the mmap system call, when entering the kernel state, it will call the mmap member function mem_mmap of the file operation method set file_operations of the character device of the device node / dev / memX;
[0141] 2. The mem_mmap function first finds the mmm_struct structure that records the physical memory from the private variables;
[0142] 3. Use the remap_pfn_range function according to the mmm_struct structure to map each physical memory to a continuous virtual address in the user space;
[0143] 4. Finally, return this virtual address in the mmap system call.
[0144] In this embodiment, a memory application method for avoiding OOM and the system call end process, wherein the specific call process of the munmap system call includes:
[0145] 1. In the munmap system call, when entering the kernel state, the mem_munmap member function of the file_operations of the character device of the device node / dev / memX will be called;
[0146] 2. The mem_munmap function first finds the mmm_struct structure from the private variables;
[0147] 3. Traverse each member page of struct vm_struct *area according to the mmm_struct structure and cancel the mapping of each page;
[0148] The specific call process of the close system call includes:
[0149] 1. In the close system call, when entering the kernel state, the mem_close member function of the file_operations of the character device of the device node / dev / memX will be called;
[0150] The mem_close function decrements the f_count member of the file by one.
[0151] S2-5 specifically includes:
[0152] S2-5-1. Determine whether the number of f_count members corresponding to the device node / dev / memX based on the kernel state is 0. If so, obtain all the mmm_struct structures according to the device node / dev / memX;
[0153] S2-5-2. After calling the mmfree function for memory release processing according to all the mmm_struct structures, unregister the corresponding device node / dev / memX and set the free label X of the memory device corresponding to the device node / dev / memX to 0;
[0154] S2-5-3. Determine whether the physical memory is continuous. If so, after releasing the memory based on the kernel memory function kfree, exit the kernel memory function kfree. Otherwise, obtain the corresponding area of the mmm_struct according to the said mmm_struct, and execute S2-5-4;
[0155] S2-5-4. After performing traversal processing using the corresponding area of the said mmm_struct, perform release processing based on the free_pages function to complete the memory application.
[0156] In this embodiment, a memory application method for avoiding OOM. The specific call process of the memfree system call includes:
[0157] 1. When the memfree system call enters the kernel mode, check whether the f_count member of the file of / dev / memX is 0 according to the passed parameter X. If it is not 0, return -EBUSY;
[0158] 2. If the f_count member of the file is 0, find the mmm_struct member from the private variable and call the mmfree function to release the memory;
[0159] 3. Unregister the / dev / memX device node;
[0160] 4. Set the X-th bit of mem_flags to 0;
[0161] In this embodiment, a memory application method for avoiding OOM. The specific memory release process of mmfree includes:
[0162] 1. Judge the situation of the physical pages according to the mmm_struct. If they are continuous physical pages, use kfree to release the physical memory, and then exit the function;
[0163] 2. If they are not continuous physical pages, find the member area from the mmm_struc;
[0164] Traverse each page member in area and call the __free_pages function to release each page.
[0165] Those skilled in the art should understand that the embodiments of the present invention can be provided as a method, a system, or a computer program product. Therefore, the present invention can adopt the form of a complete hardware embodiment, a complete software embodiment, or an embodiment combining software and hardware aspects. Moreover, the present invention can adopt the form of a computer program product implemented on one or more computer-usable storage media (including but not limited to disk storage, CD-ROM, optical storage, etc.) containing computer-usable program code.
[0166] The present invention is described with reference to the flowcharts and / or block diagrams of methods, apparatuses (systems), and computer program products according to the embodiments of the present application. It should be understood that each process and / or block in the flowchart and / or block diagram, and the combination of processes and / or blocks in the flowchart and / or block diagram, can be implemented by computer program instructions. These computer program instructions can be provided to the processor of a general-purpose computer, a special-purpose computer, an embedded processor, or other programmable data processing devices to generate a machine, so that the instructions executed by the processor of the computer or other programmable data processing devices generate means for implementing the functions specified in one process Figure 1 one process or multiple processes and / or blocks Figure 1 or multiple blocks.
[0167] These computer program instructions can also be stored in a computer-readable memory that can direct a computer or other programmable data processing device to work in a specific manner, so that the instructions stored in the computer-readable memory generate a manufactured article including instruction means, and the instruction means implement the functions specified in one process Figure 1 one process or multiple processes and / or blocks Figure 1 or multiple blocks.
[0168] These computer program instructions can also be loaded onto a computer or other programmable data processing device, so that a series of operation steps are executed on the computer or other programmable device to generate a computer-implemented process. Thus, the instructions executed on the computer or other programmable device provide steps for implementing the functions specified in one process Figure 1 one process or multiple processes and / or blocks Figure 1 or multiple blocks.
[0169] Finally, it should be noted that the above embodiments are only used to illustrate the technical solutions of the present invention and are not intended to limit them. Although the present invention has been described in detail with reference to the above embodiments, those of ordinary skill in the art should understand that modifications or equivalent replacements can still be made to the specific embodiments of the present invention. Any modification or equivalent replacement that does not depart from the spirit and scope of the present invention shall be covered by the protection scope of the claims of the present invention.
Claims
1. A memory application method for avoiding OOM, characterized in that, Including: S1. Perform pre - processing in the kernel state based on the Linux kernel to establish a mem system call to obtain the application status of physical memory calls; S1 - 1. Establish variables mem_flags and a new mem system call based on the Linux kernel respectively: Create a 256 - bit variable mem_flags in the linux kernel to record the usage of / dev / memX; Add a new system call mem in the linux kernel to allocate physical memory in the kernel state, create a device node / dev / memX bound to the physical memory, and return label X if successful; return an error code if failed; S1 - 2. Use the newly added mem system call to obtain the application status of physical memory calls according to the variable mem_flags; S1 - 2 - 1. Use the newly added mem system call to establish a device node / dev / memX bound to the physical memory according to the variable mem_flags; S1 - 2 - 2. Determine whether the current device node / dev / memX is successfully established. If so, return the label X of the device node / dev / memX as the application status of physical memory calls; otherwise, return an error code as the application status of physical memory calls; S2. Perform user - state memory allocation processing according to the application status of physical memory calls to complete the memory application; Using the newly added mem system call to establish a device node / dev / memX bound to the physical memory according to the variable mem_flags includes: S1 - 2 - 1 - 1. Determine whether both physical memory and virtual memory are continuous. If so, execute S1 - 2 - 1 - 2; otherwise, perform physical memory allocation processing on the variable mem_flags based on the mmmalloc function according to the free label X to directly obtain the physical memory allocation result; S1 - 2 - 1 - 2. Determine whether the free number of the memory device obtained by the newly added mem system call based on the Linux kernel state according to the variable mem_flags exists. If so, set the free label X of the memory device to 1; otherwise, return ENFILE; S1 - 2 - 1 - 3. When the return value corresponding to the physical memory allocation result is ENOMEM, the physical memory allocation fails, and return ENOMEM; S1 - 2 - 1 - 4. When the return value corresponding to the physical memory allocation result is the structure mmm_struct, the physical memory allocation is successful, and use the structure mmm_struct to perform private member record processing to establish the device node / dev / memX.
2. The memory application method for avoiding OOM according to claim 1, wherein, Performing user - state memory allocation processing according to the application status of physical memory calls to complete the memory application includes: S2 - 1. Establish open system call, mmap system call, munmap system call, close system call and memfree system call based on the Linux kernel respectively; S2 - 2. Use the mem system call to obtain the remaining available callable memory of the memory device; S2-3. Perform the initial memory allocation processing according to the remaining available callable memory using the open system call and the mmap system call; S2-4. Perform the memory allocation termination processing using the munmap system call and the close system call; S2-5. Perform the memory release processing using the memfree system call to complete the memory application.
3. A memory application method for avoiding OOM according to claim 2, characterized in that, Obtaining the remaining available callable memory of the memory device using the mem system call includes: S2-2-1. Perform the memory call application processing using the mem system call to obtain the initial memory call application result; S2-2-2. Determine whether the return result of the mem system call corresponding to the initial memory call application result is ENFILE. If so, end the memory call application. Otherwise, execute S2-2-3; S2-2-3. Determine whether the memory call application result at the current moment is successful. If so, return the free label X of the device node / dev / memX. Otherwise, execute S2-2-4; S2-2-4. Perform a re-memory call application according to the memory call application result at the current moment to obtain an updated memory call application result, and return to S2-2-3; Among them, the capacity of the memory call application is 2 n , 2 n which is not greater than the upper limit of the capacity of the memory device, and the capacity of the re-memory call application is 2 n-1 .
4. The memory application method for avoiding OOM according to claim 3, wherein Performing the initial memory allocation processing according to the remaining available callable memory using the open system call and the mmap system call includes: Based on the kernel mode, use the open system call to open the device node / dev / memX and obtain the corresponding open member function mem_open according to the file operation method set file_operations; Based on the kernel mode, use the mmap system call to open the device node / dev / memX and obtain the corresponding mmap member function mem_mmap according to the file operation method set file_operations; After increasing the f_count member quantity of the file operation method set file_operations by 1 using the open member function mem_open, obtain the file descriptor fd; Obtain the corresponding mmm_struct according to the device node / dev / memX using the mmap member function mem_mmap; Based on the remap_pfn_range function, perform continuous mapping processing of the physical memory of the corresponding mmm_struct to the virtual address using the corresponding mmm_struct to complete the initial memory allocation processing.
5. The memory application method for avoiding OOM according to claim 4, wherein Performing the memory allocation termination processing using the munmap system call and the close system call includes: Based on the kernel mode, use the munmap system call to call the device node / dev / memX and obtain the corresponding munmap member function mem_munmap according to the file operation method set file_operations; Based on the kernel mode, use the close system call to call the device node / dev / memX and obtain the corresponding close member function mem_close according to the file operation method set file_operations; Use the munmap member function mem_munmap to obtain the corresponding structure mmm_struct according to the device node / dev / memX; Traverse and process the structure virtual memory area struct vm_struct *area according to the corresponding structure mmm_struct to obtain the file descriptor fd of the structure virtual memory area struct vm_struct *area; Cancel the file descriptor fd of the structure virtual memory area struct vm_struct *area to complete the memory allocation termination process; After reducing the f_count member count of the file operation method set file_operations by 1 using the corresponding close member function mem_close, complete the memory allocation termination process.
6. The memory application method for avoiding OOM according to claim 5, characterized in that, Use the memfree system call to perform memory release processing to complete memory application, including: S2-5-1. Determine whether the f_count member count corresponding to the device node / dev / memX in kernel mode is 0. If so, obtain all structure mmm_struct according to the device node / dev / memX; S2-5-2. After calling the mmfree function for memory release processing according to all the structure mmm_struct, unregister the corresponding device node / dev / memX, and set the free label X of the memory device corresponding to the device node / dev / memX to 0; S2-5-3. Determine whether the physical memory is continuous. If so, release the memory based on the kernel memory function kfree and then exit the kernel memory function kfree. Otherwise, obtain the area corresponding to the structure mmm_struct according to the structure mmm_struct and execute S2-5-4; S2-5-4. After traversing and processing the area corresponding to the structure mmm_struct, perform release processing based on the free_pages function to complete memory application.
Citation Information
Patent Citations
Memory management method, system, computer equipment and storage medium
CN111679914A
Core dump method and device based on embedded equipment and computer equipment
CN118519783A