Memory application method for avoiding OOM
By adding a new system call mem in the Linux operating system, allocating memory in the kernel state, and mapping device nodes /dev/memX in the application, the problem of OOM when memory is insufficient is solved, and a more flexible and stable memory application mechanism is achieved.
Patent Information
- Application Number
- CN202510599992.2
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2025-05-12
- Publication Date
- 2025-06-13
- Estimated Expiration
- 2045-05-12
AI Technical Summary
Existing Linux operating systems can easily lead to OOM when there is insufficient memory, causing application crashes, and existing solutions such as reserved memory or frequent viewing of memory usage affect system performance.
By adding a new system call mem, allocating memory in the kernel state, and opening and mapping device node /dev/memX in the application, if the memory application fails, the application can adjust the memory size of the application according to the error code to avoid the occurrence of OOM.
It effectively avoids OOM caused by applying for large memory when memory is insufficient, and allows applications to adjust the memory application size according to error codes, which improves the stability of the system when memory is severely insufficient.
Smart Images

Figure CN120144317A_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the field of Linux memory application, and particularly relates 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, only virtual memory is obtained. Physical memory is allocated only when physical memory is used, and a page fault occurs. When allocating physical memory, it is possible to encounter a situation of insufficient 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. This method requires a large amount of calculation and saving of the memory usage situation, which 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 in the kernel state to allocate memory in the kernel state. If successful, a device node / dev / memX is created, and 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 determine 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, avoiding the occurrence of OOM.
[0004] To achieve the above object, the present invention provides a memory application method for avoiding OOM, including: 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; S2. According to the application status of the physical memory calls, perform user-state memory allocation processing to complete the memory application.
[0005] 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: S1-1. Based on the Linux kernel, establish variables mem_flags and a new mem system call respectively; S1-2. Use the new mem system call to obtain the application status of physical memory calls according to the variable mem_flags.
[0006] Further, using the newly added mem system call to obtain the physical memory call application status according to the variable mem_flags includes: S1-2-1, 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; 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; Among them, the variable mem_flags is 256 bits.
[0007] 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: S1-2-1-1, determine whether the physical memory and virtual memory are both 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 newly added mem system call obtains the free label of the memory device based on the variable mem_flags in the Linux kernel state, 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 ENOMEM is returned; 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 the structure mmm_struct is used to perform private member record processing to establish a device node / dev / memX.
[0008] Further, performing user-mode memory allocation processing to complete the memory application according to the physical memory call application state includes: 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; S2-2, using the mem system call to obtain the remaining callable memory of the memory device; S2-3, using the open system call and mmap system call to perform memory allocation initialization processing according to the remaining callable memory; S2-4. Use the munmap system call and the close system call to perform memory allocation termination processing; S2-5. Use the memfree system call to perform memory release processing to complete the memory application.
[0009] Furthermore, using the mem system call to obtain the remaining callable memory of the memory device includes: S2-2-1. Use the mem system call to perform memory call application processing 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 not greater than the capacity upper limit of the memory device, and the capacity of the re-memory call application is 2 n-1 .
[0010] Furthermore, using the open system call and the mmap system call to perform memory allocation start processing according to the remaining callable memory 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; Use the mmap member function mem_mmap to obtain the corresponding mmm_struct according to the device node / dev / memX; Based on the corresponding structure mmm_struct, the physical memory of the corresponding structure mmm_struct is continuously mapped to a virtual address using the remap_pfn_range function to complete the initial memory allocation process.
[0011] Furthermore, using the munmap system call and the close system call to perform memory allocation termination processing includes: Based on the kernel mode, use the munmap system call to call the device node / dev / memX to 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 to 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 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 one using the corresponding close member function mem_close, complete the memory allocation termination process.
[0012] Furthermore, using the memfree system call to perform memory release processing to complete the memory application includes: S2-5-1. Determine whether the f_count member count corresponding to the device node / dev / memX in the kernel mode is 0. If so, obtain all the structure mmm_struct according to the device node / dev / memX; S2-5-2. After performing memory release processing by calling the mmfree function 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, after releasing the memory based on the kernel memory function kfree, exit the kernel memory function kfree. Otherwise, obtain the area corresponding to the mmm_struct according to the said mmm_struct, and execute S2-5-4; S2-5-4. After traversing and processing using the area corresponding to the mmm_struct, perform a release process based on the free_pages function to complete the memory application.
[0013] The beneficial effects of the present invention are as follows: It avoids the possibility of triggering OOM due to insufficient memory after applying for memory; in case of insufficient memory, when applying for a 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. Description of the Drawings
[0014] 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: Figure 1 is the overall flowchart of a memory application method for avoiding OOM provided by the present invention; Figure 2 is the flowchart of the preliminary preparation of a memory application method for avoiding OOM provided by the present invention; Figure 3 is the flowchart of the call of the mem system call of a memory application method for avoiding OOM provided by the present invention; Figure 4 is the starting flowchart of the user state process of a memory application method for avoiding OOM provided by the present invention; Figure 5 is the ending flowchart of the user state process of a memory application method for avoiding OOM provided by the present invention; Figure 6 is the flowchart of the call of the mmap system call of a memory application method for avoiding OOM provided by the present invention. Detailed Embodiments
[0015] 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.
[0016] 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. Apparently, 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.
[0017] The present invention will be described in detail below with reference to the accompanying drawings and in conjunction with the embodiments.
[0018] Embodiment The present invention provides a memory application method for avoiding OOM, as Figure 1 shown, including: S1. Based on the Linux kernel, perform pre-processing in the kernel state to establish a mem system call to obtain the physical memory call application status; S2. Perform user-state memory allocation processing according to the physical memory call application status to complete the memory application.
[0019] S1 specifically includes: S1-1. Based on the Linux kernel, establish variables mem_flags and a new mem system call respectively; S1-2. Use the new mem system call to obtain the physical memory call application status according to the variable mem_flags.
[0020] S1-2 specifically includes: 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; 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; Among them, the variable mem_flags is 256 bits.
[0021] S1-2-1 specifically includes: 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; S1-2-1-2. Determine whether there is an available label for the memory device obtained according to the variable mem_flags in the Linux kernel state for the newly added mem system call. If so, set the available 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. Use the structure mmm_struct to perform private member record processing to create the device node / dev / memX.
[0022] In this embodiment, a memory application method for avoiding OOM. The preliminary preparations are as Figure 2 shown and include: 1. Create a 256-bit variable mem_flags in the linux kernel to record the usage of / dev / memX. 2. 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. If successful, return the label X; if failed, return an error code. 3. Add a new system call memfree in the linux kernel to judge 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. 4. Add a function mmmalloc in the linux kernel to allocate memory with discontinuous physical memory but continuous virtual memory. 5. Add a function mmfree in the linux kernel to release the memory allocated by mmmalloc. Prepare a method set for / dev / memX in the linux kernel, including open, mmap, close, and munmap.
[0023] In this embodiment, a memory application method for avoiding OOM. The specific call process of the mem system call is as Figure 3 shown and includes: 1. When the system call mem enters the kernel state, find the available label X according to the mem_flags variable. If the available label X cannot be found, return -ENFILE. 2. If the available label X is found, set the Xth bit of mem_flags to 1. 3. Use the mmmalloc function to allocate physical memory. 4. If the allocation fails (the return value of mmmalloc is -ENOMEM), return -ENOMEM; 5. If the allocation is successful (the return value of mmmalloc is the mmm_struct structure, a positive number), create the device node / dev / memX; 6. Record the memory mmm_struct allocated by mmmalloc in the private member of / dev / memX; When the system call of mem ends, return to label X.
[0024] In this embodiment, a memory application method for avoiding OOM, the specific memory process allocated by mmmalloc includes: 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 mmm_struct structure, and return the pointer to this structure; 2. If the application fails, perform page alignment on the applied memory; 3. Call the function __get_vm_area_node to allocate a virtual address; 4. If the allocation of physical memory fails, return -ENOMEM. If the allocation is successful, continue to run; 5. Call the function __vmalloc_area_node to allocate a physical address; 6. If the allocation of physical memory fails, return -ENOMEM. If the allocation is successful, continue to run; 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 mmm_struct structure, and return the pointer to this structure.
[0025] S2 specifically includes: 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; S2-2. Use the mem system call to obtain the remaining available callable memory of the memory device; 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; S2-4. Use the munmap system call and close system call to perform the termination process of memory allocation; S2-5. Use the memfree system call to complete the memory application after memory release processing.
[0026] S2-2 specifically includes: S2-2-1. Use the mem system call to process the memory call application 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. Make 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 capacity upper limit of the memory device. The capacity of the re-memory call application is 2 n-1 .
[0027] In this embodiment, a memory application method for avoiding OOM. The user-mode process starts as Figure 4 shown, and includes: 1. The application uses the mem system call to apply for 1M of memory and enters the mem system call for the call process; 2. If mem returns -ENFILE, it means that the mem_flags are used up, and the memory application ends; 3. If mem returns -ENOMEM, it means that the memory is insufficient. Reuse 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; 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; 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; The application uses the memory.
[0028] The user-mode process ends as Figure 5 shown, and includes: 1. After the application has finished using the memory, it makes a call using the mummap system call, enters the mummap system call process, and cancels the mapping relationship. 2. The application closes / dev / memX through the system call close and enters the close system call process. 3. The application releases the memory allocated by mem through the system call memfree and enters the memfree system call process.
[0029] S2-3 specifically includes: S2-3-1. Based on the kernel state, 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. S2-3-2. Based on the kernel state, 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. S2-3-3. After increasing the f_count member count of the file operation method set file_operations by 1 using the open member function mem_open, obtain the file descriptor fd. S2-3-4. Use the mmap member function mem_mmap to obtain the corresponding mmm_struct according to the device node / dev / memX. S2-3-5. Use the corresponding mmm_struct to perform continuous mapping of 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 process.
[0030] S2-4 specifically includes: S2-4-1. Based on the kernel state, 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. S2-4-2. Based on the kernel state, 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. S2-4-3. Use the munmap member function mem_munmap to obtain the corresponding mmm_struct according to the device node / dev / memX. S2-4-4. Traverse the virtual memory area of the structure struct vm_struct *area according to the corresponding structure mmm_struct to obtain the file descriptor fd of the virtual memory area of the structure struct vm_struct *area; S2-4-5. Cancel the file descriptor fd of the virtual memory area of the structure struct vm_struct *area to complete the memory allocation termination process; S2-4-6. After using the corresponding close member function mem_close to decrement the f_count member count of the file operation method set file_operations, complete the memory allocation termination process.
[0031] In this embodiment, a memory application method for avoiding OOM, the starting process of the system call, where the specific call process of the open system call includes: 1. When entering the kernel state in the open system call, 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; 2. The mem_open function increments the f_count member of the file and returns the file descriptor fd; The call process of the mmap system call is as Figure 6 shown, including: 1. When entering the kernel state in the mmap system call, 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; 2. The mem_mmap function first finds the mmm_struct structure that records the physical memory from the private variables; 3. According to the mmm_struct structure, use the remap_pfn_range function to map each physical memory to a continuous virtual address in the user space; 4. Finally, return the virtual address in the mmap system call.
[0032] In this embodiment, a memory application method for avoiding OOM, the ending process of the system call, where the specific call process of the munmap system call includes: 1. When entering the kernel mode in the munmap system call, it will call the mem_munmap member function of the file_operations set of the character device of the device node / dev / memX, which is mem_munmap; 2. The mem_munmap function first finds the mmm_struct structure from the private variables; 3. According to the mmm_struct structure, traverse each member page of struct vm_struct *area and cancel the mapping of each page; The specific call process of the close system call includes: 1. When entering the kernel mode in the close system call, it will call the mem_close member function of the file_operations set of the character device of the device node / dev / memX; The mem_close function decrements the f_count member of the file by one.
[0033] S2-5 specifically includes: S2-5-1. Determine whether the number of f_count members corresponding to the device node / dev / memX based on the kernel mode is 0. If so, obtain all the mmm_struct structures according to the device node / dev / memX; 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; 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 mmm_struct structure according to the mmm_struct structure and execute S2-5-4; S2-5-4. After traversing and processing using the area corresponding to the mmm_struct structure, perform release processing based on the free_pages function to complete the memory application.
[0034] In this embodiment, for a memory application method to avoid OOM, the specific call process of the memfree system call includes: 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; 2. If the f_count member of file is 0, find the mmm_struct member from the private variables and call the mmfree function to free the memory; 3. Unregister the / dev / memX device node; 4. Set the Xth bit of mem_flags to 0; In this embodiment, a memory application method for avoiding OOM, the specific memory freeing process of mmfree includes: 1. Judge the situation of physical pages according to mmm_struct. If they are continuous physical pages, use kfree to free the physical memory and then exit the function; 2. If they are not continuous physical pages, find the member area from mmm_struc; Traverse each page member in area and call the __free_pages function to free each page.
[0035] Those skilled in the art should understand that the embodiments of the present invention can be provided as methods, systems, or computer program products. Therefore, the present invention can take the form of an entirely hardware embodiment, an entirely software embodiment, or an embodiment combining software and hardware aspects. Moreover, the present invention can take 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.
[0036] 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 flowcharts and / or block diagrams, and the combination of processes and / or blocks in the flowcharts and / or block diagrams, can be realized by computer program instructions. These computer program instructions can be provided to the processors of general-purpose computers, special-purpose computers, embedded processors, or other programmable data processing devices to generate a machine, so that the instructions executed by the processors of the computer or other programmable data processing devices generate means for realizing the functions specified in Figure 1 one process or multiple processes and / or blocks Figure 1 one block or multiple blocks.
[0037] 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 realizes the functions specified in Figure 1 one process or multiple processes and / or blocks Figure 1 one block or multiple blocks.
[0038] These computer program instructions can also be loaded onto a computer or other programmable data processing apparatus, so that a series of operation steps are performed on the computer or other programmable apparatus to produce a computer-implemented process, thereby the instructions executed on the computer or other programmable apparatus provide steps for realizing the functions specified in one process or a plurality of processes and / or blocks Figure 1 one process or a plurality of processes and / or blocks Figure 1 steps of the functions specified in one block or a plurality of blocks.
[0039] Finally, it should be noted that the above embodiments are only used to illustrate the technical solutions of the present invention and not 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: the specific implementation manners of the present invention can still be modified or equivalently replaced, and any modification or equivalent replacement without departing 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: include: S1. Perform kernel state preprocessing based on the Linux kernel to establish a mem system call to obtain the physical memory call application status; S1-1. Create variables mem_flags and add new mem system calls based on the Linux kernel; S1-2, using the newly added mem system call to obtain the physical memory call application status according to the variable mem_flags; S1-2-1, 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; 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; Wherein, the variable mem_flags is 256 bits; S2. Perform user-mode memory allocation processing according to the physical memory call application status to complete the memory application.
2. A memory application method for avoiding OOM as claimed in claim 1, characterized in that: 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 the physical memory and virtual memory are both 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 newly added mem system call obtains the free label of the memory device based on the variable mem_flags in the Linux kernel state, 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 ENOMEM is returned; 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 the structure mmm_struct is used to perform private member record processing to establish a device node / dev / memX.
3. A memory application method for avoiding OOM as claimed in claim 2, characterized in that: Performing user-mode memory allocation processing according to the physical memory call application state to complete the memory application includes: 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; S2-2, using the mem system call to obtain the remaining callable memory of the memory device; S2-3, using the open system call and mmap system call to perform memory allocation initialization processing according to the remaining callable memory; S2-4, using the munmap system call and the close system call to perform memory allocation termination processing; S2-5, using the memfree system call to perform memory release processing to complete the memory application.
4. A memory application method for avoiding OOM as claimed in claim 3, characterized in that: Using the mem system call to obtain the remaining callable memory of the memory device includes: S2-2-1, using the mem system call to perform memory call application processing to obtain an 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, performing another memory call application according to the memory call application result at the current moment to obtain an updated memory call application result, and returning to S2-2-3; The memory capacity requested is 2 n , 2 n Not greater than the upper limit of the memory device capacity, the capacity requested by the memory call again is 2 n-1 .
5. A memory application method for avoiding OOM as claimed in claim 4, characterized in that: Using the open system call and the mmap system call to perform memory allocation initial processing according to the remaining callable memory includes: Based on the kernel state, the device node / dev / memX is opened by using the open system call and the corresponding open member function mem_open is obtained according to the file operation method set file_operations; Based on the kernel state, the mmap system call is used to open the device node / dev / memX and obtain the corresponding mmap member function me_mmap according to the file operation method set file_operations; Using the open member function mem_open, the number of f_count members of the file operation method set file_operations is increased by one, and the file descriptor fd is obtained; Utilize the mmap member function mem_mmap to obtain the corresponding structure mmm_struct according to the device node / dev / memX; The corresponding structure mmm_struct is used to continuously map the physical memory of the corresponding structure mmm_struct to the virtual address based on the remap_pfn_range function to complete the memory allocation start processing.
6. A memory application method for avoiding OOM as claimed in claim 5, characterized in that: Utilizing the munmap system call and the close system call to perform memory allocation termination processing includes: Based on the kernel state, the munmap system call is used to call the device node / dev / memX to obtain the corresponding munmap member function mem_munmap according to the file operation method set file_operations; Based on the kernel state, the close system call is used to call the device node / dev / memX to obtain the corresponding close member function mem_close according to the file operation method set file_operations; Utilize the munmap member function mem_munmap to obtain the corresponding structure mmm_struct according to the device node / dev / memX; According to the corresponding structure mmm_struct, the structure virtual memory area struct vm_struct *area is traversed 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 the number of f_count members of the file operation method set file_operations is reduced by one by using the corresponding close member function mem_close, the memory allocation termination process is completed.
7. A memory application method for avoiding OOM as claimed in claim 6, characterized in that: Using the memfree system call to perform memory release processing to complete the memory application includes: S2-5-1, determine whether the number of f_count members corresponding to the kernel-state device node / dev / memX is 0, if so, obtain all structures mmm_struct according to the device node / dev / memX; S2-5-2, after calling the mmfree function to release the memory according to all the structures mmm_struct, the corresponding device node / dev / memX is deregistered, and the free number X of the memory device corresponding to the device node / dev / memX is set 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 exit the kernel memory function kfree, otherwise, obtain the corresponding area of the structure mmm_struct according to the structure mmm_struct, and execute S2-5-4; S2-5-4. After traversing the corresponding area of the structure mmm_struct, the memory application is completed by performing release processing based on the free_pages function.
Citation Information
Patent Citations
Memory management method, system, computer equipment and storage medium
CN111679914A
Memory management method for microkernel architecture operating system
CN118069545A
Core dump method and device based on embedded equipment and computer equipment
CN118519783A
Memory management method and apparatus, and computing device
WO2025044111A1