Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between vm_insert_page() and remap_pfn_range()?

I want to map device memory (NIC) to the kernel space memory region by using ioremap_wc(). And then I want to remap memory region from kernel space to user space, and I can use 2 functions for this: vm_insert_page() and remap_pfn_range()

POSIX mmap(3) usually use the second: remap_pfn_range()

What is the difference between vm_insert_page() and remap_pfn_range(), and when do I need to use vm_insert_page() instead of remap_pfn_range()?

like image 940
Alex Avatar asked Dec 14 '14 09:12

Alex


2 Answers

As their name suggest vm_insert_page() map a single page, while remap_pfn_range() maps a consecutive block of kernel memory. Check the prototypes and comments vm_insert_page, remap_pfn_range

For example, you can use vm_insert_page to map vmalloc aree

do {
    page = vmalloc_to_page(vaddr);
    vm_insert_page(vma, uaddr, page);
    vaddr += PAGE_SIZE;
} while(/* there is something to map */);

it is not possible using remap_pfn_range because it maps only a consecutive block of kernel memory.

Another difference is that with remap_pfn_range you can map not only RAM buffers, but other ranges. With vm_inser_page you can map only RAM buffers

An explanation from Linus

like image 189
Federico Avatar answered Sep 18 '22 16:09

Federico


vm_insert_page() allows drivers to insert individual pages they've allocated into a user vma. The page has to be allocate within the kernel independently. It requires the page be an order-zero allocation obtained for this purpose. It does not put out warnings, and does not require that PG_reserved be set.

Traditionally, this was done with remap_pfn_range() which took an arbitrary page protection parameter. vm_insert_page() doesn't allow that. Your vma protection will have to be set up correctly, which means that if you want a shared writeable mapping, you'd better ask for a shared writeable mapping!

remap_pfn_range() is used for mapping or remapping a group of pages into the memory.

Refer

like image 25
askb Avatar answered Sep 20 '22 16:09

askb