The difference between the 1,kmalloc () function and the Vmalloc () function:
The memory allocated by the Kmalloc () function is physically contiguous, whereas the memory allocated by the Vmalloc () function is only a continuous virtual address, and normal kernel programming usually uses the Kmalloc (), which is primarily in performance consideration because Vmalloc () Converting a physically discontinuous page into a contiguous page on a virtual address space must be specifically set up for page table entries, and Vmalloc () is used only when large chunks of memory are needed, typically when the module is dynamically inserted into the kernel. Many other hardware devices require a contiguous page of physical addresses, because many hardware devices exist outside of the memory Management Unit (MMU). In addition Vmalloc () function may sleep, can not be used in the middle end of the context, while Kmalloc plus gfp_atomic can be guaranteed to be used in places where sleep is not possible.
If you want your code to be very portable on different platforms, you should not allocate more than 128K of memory.
Kmalloc corresponds to Kfree, the allocated memory is between 3gb~high_memory, this kernel space corresponds to the mapping of physical memory one by one, can allocate contiguous physical memory, Vmalloc corresponds to Vfree, allocated memory in VMALLOC_ START~4GB, allocating contiguous virtual memory, but not necessarily physically contiguous, vmalloc () allocated for high-end memory ~
2. How to prevent memory fragmentation caused by frequent malloc () free () operations
Answer: Use memory pool technology.
Memory pool technology first allocates a large chunk of memory to the program, when the program needs to allocate memory from the memory pool to obtain, and do not need to release memory, when the memory pool is not in use when the entire memory can be allocated only, not released, greatly reducing the time.
How to build a memory pool
3, whether the kernel space can access the memory of the user space
A: Yes, such as Copy_to_user () and Copy_from_user () two functions,
[CPP]View Plaincopy
- Unsigned long copy_to_user (void __user *to,const void *from,unsigned Long Count)
The parameter to is to represent the user space pointer, the kernel space to access user space content, must use the above interface, because in Linux, the user memory and kernel memory is independent, in the respective address space implementation, the above function will do in the implementation of memory conversion and user space address check. The realization of Copy_from_user
[Linux Memory]linux Memory learning--Frequently Asked questions