Method and device for applying DMA continuous physical memory under linux user mode and storage medium

By creating character device file nodes in Linux user space and combining them with memory mapping technology, the problem of not being able to directly request contiguous physical memory for DMA in user space is solved. This implements the DMA operation interface and memory space mapping in user space, supporting user programs to access DMA resources.

CN115357385BActive Publication Date: 2025-11-28TIANJIN JINHANG COMP TECH RES INST
View PDF 1 Cites 0 Cited by

Patent Information

Application Number
CN202210991658.8
Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
Filing Date
2022-08-18
Publication Date
2025-11-28
Estimated Expiration
2042-08-18

AI Technical Summary

Technical Problem

In Linux user space, programmers cannot directly request contiguous physical memory via DMA, which limits the development of user-space programs, especially when accessing PCIe peripherals where DMA-related operations cannot be performed.

Method used

By creating character device file nodes, dynamically allocating device numbers, and combining kernel-level operation libraries and memory mapping technology, an interface for allocating and releasing contiguous physical memory for DMA is implemented. The ioctl interface is used to perform memory space conversion and mapping, enabling user programs to access contiguous physical memory.

Benefits of technology

It enables fast and reliable allocation and release of contiguous physical memory in Linux user space, solves the resource access limitations in user space programming, and supports DMA operations in user space.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN115357385B_ABST
    Figure CN115357385B_ABST
Patent Text Reader

Abstract

The application provides a Linux user state DMA continuous physical memory application method, and the method comprises the following steps: S1, creating a character device file node; the character device file node is used as a medium for system resource access; S2, based on the character device file node, applying for a continuous memory space to the system, and converting the continuous memory space into a continuous physical memory address; S3, defining an interface for accessing the continuous physical memory address in step S2. In the application scenario of the Linux system, the user programs in the user state, and according to the memory application requirements, the kernel bottom operation library and the memory mapping technology are combined to design and realize a Linux user state DMA continuous physical memory application method according to the system platform characteristics; the application solves the problem that DMA continuous physical memory cannot be directly applied when programming in the Linux user state.
Need to check novelty before this filing date? Find Prior Art

Description

TECHNICAL FIELD

[0001] The application relates to the technical field of DMA continuous physical memory application, and particularly relates to a Linux user mode DMA continuous physical memory application method, device and storage medium. BACKGROUND

[0002] The Linux operating system is a UNIX-like operating system free of use and free of dissemination, and is a multi-user, multi-task, multi-thread and multi-CPU operating system based on POSIX, and the user ecology is rich. A plurality of domestic operating systems appearing in China, such as the NeoKylin operating system, the Kylin operating system and the UbuntuKylin operating system, all belong to the Linux-like operating system. The system platform applied in the present application adopts the Linux operating system, and can be implemented on the NeoKylin, Kylin and UbuntuKylin domestic operating systems.

[0003] Programmers develop programs on the Linux operating system, and usually use programming tools such as Qt and PyCharm to develop C / C++ / Python voice programs, which belong to user state programming. This programming mode can very conveniently perform program code writing, compiling, debugging and running by means of the code checking function and the debugging function of the programming tool, and is very friendly to programmers. However, due to the limitation of the Linux operating system on user state programming, the user state programmer cannot directly operate system resources, and a typical problem scenario is that when accessing a PCIE peripheral, DMA related operations such as DMA continuous physical memory application are usually required, and these system resource operations cannot be performed, thereby limiting the program development of the user state programming.

[0004] In order to solve the problem that DMA continuous physical memory cannot be directly applied when programming in the Linux user state, based on a kernel bottom layer operation library and a memory mapping technology, the application provides a Linux user state DMA continuous physical memory application method, device and storage medium. SUMMARY

[0005] The application aims at the above problems, and provides a Linux user state DMA continuous physical memory application method, device and storage medium. The specific technical scheme is as follows:

[0006] In a first aspect, the application provides a Linux user state DMA continuous physical memory application method, and the method comprises the following steps:

[0007] S1, a character device file node is created; the character device file node is used as a medium for system resource access;

[0008] S2, based on the character device file node, applying a continuous memory space to the system, and converting the continuous memory space into a continuous physical memory address;

[0009] S3, defining an interface for accessing the continuous physical memory address in step S2.

[0010] According to the technical scheme provided by some embodiments of the present application, in step S1, the creating of the character device file node specifically comprises:

[0011] S11, dynamically allocating a major device number;

[0012] S12, initializing the major device to obtain the major device number;

[0013] S13, registering the major device number as a character device;

[0014] S14, creating a character device association class;

[0015] S15, dynamically creating a character device file node.

[0016] According to the technical scheme provided by some embodiments of the present application, in step S2, based on the character device file node, applying a continuous memory space to the system, and converting the continuous memory space into a continuous physical memory address, specifically comprising:

[0017] S21, applying a continuous physical memory interface;

[0018] S22, applying a continuous memory space to the system;

[0019] S23, converting the continuous memory space in step S22 into a continuous physical memory address;

[0020] S24, releasing the continuous physical memory interface.

[0021] According to the technical scheme provided by some embodiments of the present application, in step S3, based on the memory mapping mechanism, defining an interface for accessing the continuous physical memory address in step S2.

[0022] According to the technical scheme provided by some embodiments of the present application, the application and release of the continuous physical memory interface are realized in an ioctl interface.

[0023] In a second aspect, the present application further provides a device for applying DMA continuous physical memory in a Linux user state, the application device comprising:

[0024] a memory, a processor, and a computer program stored on the memory and executable on the processor;

[0025] The computer program is executed by the processor to implement the steps of the Linux user state DMA continuous physical memory application method.

[0026] In a third aspect, the present application also provides a computer readable storage medium, which stores a Linux user state DMA continuous physical memory application program, and the Linux user state DMA continuous physical memory application program is executed by a processor to implement the steps of the Linux user state DMA continuous physical memory application method.

[0027] Compared with the prior art, the present application has the following beneficial effects: the DMA continuous physical memory application method provided by the present application first creates a character device file node as a medium for system resource access, dynamically allocates a device number and dynamically creates a character device file node, and completes the creation of a character device in a Linux file system, and then on this basis, implements a continuous physical memory application and release method based on memory page allocation, and completes the application and release interface of the DMA continuous physical memory of the character device called by the user program, and finally implements a memory access method of the character device file node based on memory mapping, and completes the mapping of the continuous physical memory space of the device file node to the user program memory space; the present application is applied to a system of Linux, and a user programs in the user state, and according to the memory application requirements, in combination with the kernel bottom operation library and the memory mapping technology, a DMA continuous physical memory application method in the Linux user state is designed and implemented according to the characteristics of the system platform; the present application solves the problem that the DMA continuous physical memory cannot be directly applied when programming in the Linux user state. BRIEF DESCRIPTION OF DRAWINGS

[0028] Figure 1 A flowchart of the Linux user state DMA continuous physical memory application method provided by the first embodiment of the present application is shown in the figure.

[0029] Figure 2 A specific flowchart of step S1 in the first embodiment of the present application is shown in the figure. Figure 1 A specific flowchart of step S2 in the first embodiment of the present application is shown in the figure.

[0030] Figure 3 A specific flowchart of step S2 in the first embodiment of the present application is shown in the figure. Figure 1 A specific flowchart of step S2 in the first embodiment of the present application is shown in the figure.

[0031] Figure 4 A structure diagram of the server provided by the fourth embodiment of the present application is shown in the figure.

[0032] The text annotations in the figure represent:

[0033] 400, server; 401, central processing unit (CPU); 402, read-only memory (ROM); 403, random access memory (RAM); 404, bus; 405, input / output (I / O) interface; 406, input part; 407, output part; 408, storage part; 409, communication part; 410, drive; 411, detachable medium. DETAILED DESCRIPTION

[0034] In order for those skilled in the art to better understand the technical solutions of the present application, the present application will be described in detail below with reference to the drawings, and the description in this part is only exemplary and explanatory, and should not have any limiting effect on the protection scope of the present application.

[0035] It should be noted that: similar reference numerals and letters represent similar items in the following drawings, therefore, once an item is defined in one drawing, it does not need to be further defined and explained in the subsequent drawings.

[0036] Embodiment one

[0037] The present embodiment provides a method for applying DMA continuous physical memory under Linux user state, a flow chart of the method is shown as Figure 1 The method comprises the following steps:

[0038] S1, creating a character device file node; the character device file node is used as a medium for system resource access.

[0039] The device management of Linux is closely combined with the file system, various devices are stored in the / dev directory in the form of files, which are called device files. Application programs can open, close and read and write these device files to complete the operation of the device, just like operating ordinary data files. The character device file node is a kind of system resource related file node in Linux, and the character device file node will be used as a medium for system resource application and access to realize the application and access of DMA continuous physical memory under user state.

[0040] Please refer to Figure 2 to create a character device file node, which specifically comprises the following steps:

[0041] S11, dynamically allocating a major device number, specifically by calling alloc_chrdev_region to dynamically allocate a major device number;

[0042] S12, initializing the main device to obtain the major device number, specifically, initializing the device using the MKDEV macro to obtain the major device number, and associating the corresponding file operation;

[0043] S13, register the master device number as a character device, specifically, register the master device number as a character device using cdev_dev;

[0044] S14, create a character device association class, specifically, invoke class_create to create a device association class;

[0045] S15, dynamically create a character device file node, specifically, invoke device_create to dynamically create a character device file node, and specify the file name of the character device under the Linux file system / dev.

[0046] S2, based on the character device file node, apply for a continuous memory space from the system, and convert the continuous memory space into a continuous physical memory address.

[0047] After the creation of the character device file node, the continuous physical memory application and release interface needs to be implemented for application program calling. The operation on the character device file node is implemented through a unified ioctl interface, and the continuous physical memory application and release interface based on memory page allocation is implemented in the ioctl interface.

[0048] Please refer to Figure 3 The specific steps of implementing the continuous physical memory application and release interface are as follows:

[0049] S21, apply for a continuous physical memory interface;

[0050] In the ioctl interface, the command value of the continuous physical memory application interface is defined as an integer value IOCALLOC, and the input parameter is defined as a structure containing the size of the physical memory to be applied for and the starting address pointer of the physical memory.

[0051] S22, apply for a continuous memory space from the system;

[0052] In the continuous physical memory application interface, the system function kmalloc is called to apply for a continuous memory space with a specified size from the system. In order to meet the requirement of DMA hardware read and write, the kmalloc specifies GFP_DMA as the zone modification parameter for memory application. The kmalloc returns the starting address of the continuous memory in virtual memory when the memory application is successful.

[0053] S23, convert the continuous memory space in step S22 into a continuous physical memory address;

[0054] The virt_to_phys is called to convert the physical memory address, which is written into the physical memory starting address pointer in the input parameter, and the continuous physical memory application is completed.

[0055] S24, release the continuous physical memory interface.

[0056] In the ioctl interface, the command value of the interface for releasing the continuous physical memory is an integer value IOCFREE, and the input parameter is defined as containing the start address of the physical memory to be released; in the interface for releasing the continuous physical memory, the start address of the physical memory to be released is searched in the applied memory space, the corresponding virtual memory address is found, the system function kfree is called to release the applied memory space, and the releasing of the continuous physical memory is completed.

[0057] S3, defining an interface for accessing the continuous physical memory address in step S2. In this step, the interface for accessing the continuous physical memory address in step S2 is defined based on the memory mapping mechanism.

[0058] Linux provides a memory mapping mechanism to enable user programs to have the ability of direct access to files. The memory mapping is used to map a file or other object to the address space of a user program, and to realize the mutual mapping between the file node and the process virtual address space. The memory mapping function (mmap) is implemented in the character device file node, the applied continuous physical memory space is mapped to the continuous virtual space of the user program, and the function of reading and modifying the continuous physical memory space by the user program is achieved. The specific steps of implementing the memory mapping function of the character device file node are as follows:

[0059] S31, defining the memory mapping function (mmap), and specifically taking the mapping offset vm_pgoff of the virtual memory block structure vm_area_struct and the difference between the start address vm_start and the end address vm_end of the memory space as parameters, and inputting the created page table function remap_pfn_range to complete the mapping of the continuous physical memory space of the device file node to the memory space of the user program;

[0060] S32, registering the self-defined memory mapping function (mmap) in the operation file interface;

[0061] S33, after the user program opens the character device file node and applies the DMA continuous physical space, calling mmap and inputting the start address of the applied physical space as the page offset off_t parameter to obtain the access start address of the DMA continuous physical memory in the user space.

[0062] The Linux user state DMA continuous physical memory application method provided in the application first creates a character device file node as a medium for system resource access, dynamically allocates a device number and dynamically creates a character device file node, completes creation of a character device in a Linux file system, then on this basis, implements a continuous physical memory application and release method based on memory page allocation, completes user program calling of the character device DMA continuous physical memory application and release interface, and finally implements a memory access method of the character device file node based on memory mapping, and completes mapping of the continuous physical memory space of the device file node to the user program memory space.

[0063] The application creates a character device file node as a medium for system resource application and access, implements DMA continuous physical memory application and release interfaces in the character device file node, and provides a user program with conversion from a virtual address to a physical address, finally maps the applied continuous physical memory space to the continuous virtual space of the user program through a memory mapping function of the character device file node, and achieves the function of reading and modifying the continuous physical memory space by the user program.

[0064] The Linux user state DMA continuous physical memory application method provided in the application can realize DMA continuous physical memory application in the Linux user state, and the method has been verified by a system of Baidu Kirin and a system of Galaxy Kirin, and has been tested by a simulation experiment. Results show that the scheme can quickly and reliably realize DMA continuous physical space application in the user state.

[0065] Embodiment Two

[0066] The embodiment provides a Linux user state DMA continuous physical memory application device, which comprises:

[0067] a memory, a processor, and a computer program stored in the memory and capable of running on the processor;

[0068] The computer program is executed by the processor to implement the steps of the Linux user state DMA continuous physical memory application method.

[0069] Embodiment Three

[0070] This embodiment provides a computer-readable storage medium storing a Linux user-mode DMA contiguous physical memory allocation program. When executed by a processor, the Linux user-mode DMA contiguous physical memory allocation program implements the steps of any of the above-described Linux user-mode DMA contiguous physical memory allocation methods.

[0071] Example 4

[0072] This embodiment provides a server 400, such as Figure 4 As shown, the server 400 includes a central processing unit (CPU) 401, which can perform various appropriate actions and processes based on programs stored in read-only memory (ROM) 402 or programs loaded from storage into random access memory (RAM) 403. RAM 403 also stores various programs and data required for system operation. CPU 401, ROM 402, and RAM 403 are interconnected via bus 404. Input / output (I / O) interface 405 is also connected to bus 404.

[0073] The following components are connected to I / O interface 405: an input section 406 including a keyboard, mouse, etc.; an output section 407 including a cathode ray tube (CRT), liquid crystal display (LCD), etc., and speakers, etc.; a storage section 408 including a hard disk, etc.; and a communication section 409 including a network interface card such as a LAN card, modem, etc. The communication section 409 performs communication processing via a network such as the Internet. A drive is also connected to I / O interface 405 as needed. A removable medium 411, such as a disk, optical disk, magneto-optical disk, semiconductor memory, etc., is installed on drive 410 as needed so that computer programs read from it can be installed into storage section 408 as needed.

[0074] In particular, according to embodiments of the present invention, the above references Figures 1-3 The described process can be implemented as a computer software program. For example, embodiments of the present invention include a computer program product comprising a computer program carried on a computer-readable medium, the computer program including methods for executing... Figures 1-3 The program code for the method shown. In such an embodiment, the computer program can be downloaded and installed from a network via communication section 409, and / or installed from removable media 411.

[0075] It should be noted that the computer-readable medium shown in the present application can be a computer-readable signal medium or a computer-readable storage medium or any combination of the above two. The computer-readable storage medium may, for example, but is not limited to, an electrical, magnetic, optical, electromagnetic, infrared, or semiconductor system, device or component, or any combination of the above. More specific examples of computer-readable storage media can include, but are not limited to, an electrical connection having one or more wires, a portable computer diskette, a hard disk, a random access memory (RAM), a read-only memory (ROM), an erasable programmable read-only memory (EPROM or flash memory), an optical fiber, a portable compact disk read-only memory (CD-ROM), an optical storage device, a magnetic storage device, or any suitable combination of the above. In the present application, the computer-readable storage medium can be any tangible medium containing or storing a program that can be used by or in conjunction with an instruction execution system, device or component. In the present application, the computer-readable signal medium can include a data signal carried in a baseband or as a part of a carrier wave, which carries computer-readable program code. Such a propagated data signal can take many forms, including but not limited to an electromagnetic signal, an optical signal, or any suitable combination of the above. The computer-readable signal medium can also be any computer-readable medium other than the computer-readable storage medium, which can send, propagate or transmit a program for use by or in conjunction with an instruction execution system, device or component. The program code contained on the computer-readable medium can be transmitted using any suitable medium, including but not limited to wireless, wire, optical cable, RF, etc., or any suitable combination of the above.

[0076] The flowcharts and block diagrams in the drawings illustrate the possible implementation architectures, functions and operations of the methods and computer program products according to various embodiments of the present application. In this regard, each block in the flowcharts or block diagrams can represent a module, a program segment, or a part of code containing one or more executable instructions for implementing the specified logical functions. It should also be noted that in some alternative implementations, the functions noted in the blocks can occur in different order than that shown in the drawings. For example, two blocks that are shown in succession can actually be executed substantially in parallel, and they can also be executed in reverse order, depending on the functions involved. It should also be noted that each block in the block diagrams and / or flowcharts, and the combination of blocks in the block diagrams and / or flowcharts, can be implemented by a dedicated hardware-based system that performs the specified functions or operations, or can be implemented by a combination of dedicated hardware and computer instructions.

[0077] The units described in the embodiments of the present application can be implemented by software, or can be implemented by hardware, and the described units can also be arranged in a processor. In some cases, the names of the units do not constitute a limitation on the units themselves. The described units or modules can also be arranged in a processor, for example, a processor can be described as including a first generation module, an acquisition module, a lookup module, a second generation module, and a merging module. In some cases, the names of the units or modules do not constitute a limitation on the units or modules themselves.

[0078] As another aspect, the present application also provides a computer readable medium, which can be included in the server described in the above embodiments, or can exist independently without being assembled into the electronic device. The computer readable medium carries one or more programs, which, when executed by the electronic device, cause the electronic device to implement the method for applying DMA continuous physical memory in Linux user mode as described in the above embodiments. For example, the electronic device can implement each step as shown in Figures 1-3

[0079] It should be noted that, although several modules or units of the device for action execution are mentioned in the above detailed description, such division is not mandatory. In fact, according to the embodiments of the present disclosure, the features and functions of two or more modules or units described above can be embodied in one module or unit. Conversely, the features and functions of one module or unit described above can be further divided into multiple modules or units.

[0080] In addition, although the steps of the method of the present disclosure are described in a specific order in the drawings, this does not require or imply that the steps must be performed in this specific order, or that all the steps shown must be performed to achieve the desired result. Additionally or alternatively, some steps can be omitted, multiple steps can be combined into one step, one step can be divided into multiple steps, etc.

[0081] From the above description of the embodiments, those skilled in the art can easily understand that the example embodiments described herein can be implemented by software, or by software combined with necessary hardware.

[0082] ​It has to be noted that, in the present document, relational terms are intended only to convey a possible relationship between elements or

[0083] The above description is merely that of a specific implementation of the application and as such is not to be taken in a limiting sense. Various modifications and changes can be made by those skilled in the art to which this application pertains without departing from the spirit and scope of the application as defined by the appended claims. The description is thus to be interpreted in the broadest sense and is intended to include all modifications and equivalents thereof.

Claims

1. A method for applying DMA contiguous physical memory in Linux user mode, characterized in that, The method comprises the following steps: S1, creating a character device file node; the character device file node is used as a medium for system resource access; wherein, creating the character device file node comprises: S11, dynamically allocating a major device number, specifically by calling alloc_chrdev_region to dynamically allocate the major device number; S12, initializing the major device, obtaining the major device number, specifically, initializing the device using the MKDEV macro, obtaining the major device number, and associating corresponding file operations; S13, registering the major device number as a character device, specifically, using cdev_dev to register the major device number as a character device; S14, creating a character device association class, specifically, calling class_create to create a device association class; S15, dynamically creating a character device file node, specifically, calling device_create to dynamically create a character device file node, and specifying the character device file name under the Linux file system / dev; S2, based on the character device file node, applying for a continuous memory space from the system, and converting the continuous memory space into a continuous physical memory address; wherein, comprising: S21, applying for a continuous physical memory interface, in the ioctl interface, setting the command value of the continuous physical memory interface as an integer value IOCALLOC, and defining the input parameter as a structure containing the size of the physical memory to be applied for and a physical memory start address pointer; S22, applying for a continuous memory space from the system, in the continuous physical memory interface, calling the system function kmalloc to apply for a continuous memory space of a specified size from the system, in order to meet the requirement of DMA hardware readable and writable, kmalloc specifies GFP_DMA as the zone modification parameter for applying for memory; S23, converting the continuous memory space in step S22 into a continuous physical memory address, calling virt_to_phys to convert it into a physical memory address, and writing it into the physical memory start address pointer in the input parameter; S24, releasing the continuous physical memory interface, in the ioctl interface, setting the command value of the continuous physical memory interface as an integer value IOCFREE; S3, defining an interface for accessing the continuous physical memory address in step S2 based on the memory mapping mechanism; wherein, comprising: S31, customizing a memory mapping function (mmap), specifically, setting the mapping offset of the virtual memory block structure vm_area_struct as vm_pgoff, and the difference between the start address vm_start and the end address vm_end of the memory space as parameters, and inputting them into the create page table function remap_pfn_range to complete the mapping of the continuous physical memory space of the device file node to the user program memory space; S32, registering the customized memory mapping function (mmap) in the operation file interface; S33, after the user program opens the character device file node and applies for the DMA continuous physical space, the mmap is called, the first address of the applied physical space is transmitted as the page offset off_t parameter, and the access starting address of the DMA continuous physical memory in the user space is obtained.

2. The method for applying for DMA contiguous physical memory in Linux user mode according to claim 1, wherein, In step S1, creating the character device file node specifically includes: S11, dynamically allocating a major device number; S12, initializing the major device to obtain the major device number; S13, registering the major device number as a character device; S14, creating a character device association class; S15, dynamically creating a character device file node.

3. The method for applying for DMA contiguous physical memory in Linux user mode according to claim 1, wherein, In step S2, based on the character device file node, a continuous memory space is applied to the system, and the continuous memory space is converted into a continuous physical memory address, specifically including: S21, applying a continuous physical memory interface; S22, applying a continuous memory space to the system; S23, converting the continuous memory space in step S22 into a continuous physical memory address; S24, releasing the continuous physical memory interface.

4. The method for applying for DMA contiguous physical memory in Linux user mode according to claim 1, wherein, In step S3, an interface for accessing the continuous physical memory address in step S2 is defined based on a memory mapping mechanism.

5. The method for applying for DMA contiguous physical memory in Linux user mode according to claim 3, wherein, The continuous physical memory interface is applied and released in the ioctl interface.

6. A device for applying for DMA contiguous physical memory under Linux user mode, characterized in that, The application device includes: a memory, a processor, and a computer program stored on the memory and executable on the processor; when the computer program is executed by the processor, the steps of the Linux user state DMA continuous physical memory application method according to any one of claims 1 to 5 are implemented.

7. A computer-readable storage medium, characterized in that, The computer readable storage medium stores a Linux user state DMA continuous physical memory application program, and when the Linux user state DMA continuous physical memory application program is executed by the processor, the steps of the Linux user state DMA continuous physical memory application method according to any one of claims 1 to 5 are implemented.

Citation Information

Patent Citations

  • Data transmission method and device, electronic equipment and readable storage medium

    CN109992352A