I need to allocate physically contiguous pages from user space and get back the physical address. How can I do so? I need it physically contiguous because I'm working in-conjunction with hardware that needs that.
get_free_pages as I understood is Kernel function and returns virtual address
Related
Virtual address to physical page mapping can be changed during application runtime by swapping or physical page reallocation for memory defragmentation or etc.
What if I want to cache physical page numbers (PPNs) of some virtual address range from /proc/PID/pagemap, since accessing proc/PID/pagemap is extremely expansive overhead to be checked every time, is there a way to be notified if a page has been moved to other physical address or swapped, on the effective address or just any part of memory?
Any kind of method will be ok(not just userspace method, but also those that can be only implemented in kernel space).
I'd like to reserve a large contiguous region (4GiB) of virtual address space. I can do this with mmap. Then, as I write to the memory, the kernel will gradually cause it to become physically backed. At some stage I'd like to return physical pages within this range back to the kernel, while keeping the entire region of virtual address space still mapped. My issue is that if I return physical pages with munmap, then I not only return the physical pages, but I also return the virtual address space.
Is it possible to return just the physical pages while still keeping the virtual address space?
Yes, call madvise(2) with advice set to MADV_DONTNEED.
I can understand the rationale to divide memory into kernel space and user space when dealing with physical addresses, so user process won't be able to contaminate kernel space.
With virtual address and page table, isolation between process is guaranteed by page table mapping(multiple process won't be able to corrupt each other's memory, and they have the view of owning the whole memory), but why do we still need to reserve kernel space in user process's virtual memory address space, as far as I know, user process can't read or write into kernel space, so what is the point? is it for backward compatibility?
I think kmalloc() allocates continuous physical pages in the kernel because the virtual memory space is directly mapping to the physical memory space, by simply adding an offset.
However, I still don't understand why it is more efficient than vmalloc().
It still needs to go through the page table (the kernel page table), right? Because the MMU is not disabled when the process is switching to the kernel. So why Linux directly maps the kernel virtual space to the physical memory? What is the benefit?
In include/asm-x86/page_32.h, there is:
#define __pa(x) ((unsigned long)(x)-PAGE_OFFSET)
#define __va(x) ((void *)((unsigned long)(x)+PAGE_OFFSET))
Why does the kernel need to calculate the physical address? It has to use the virtual address to access the memory anyway, right? I cannot figure out why the physical address is needed.
Your Queries :-
why is Kmalloc more efficient than vmalloc()?
kmalloc allocates a region of physically contiguous (also virtually contiguous) memory. The physical to virtual map is one-to-one.
For vmalloc(), an MMU/PTE value is allocated for each page; the physical to virtual mapping is not continuous.
vmalloc is often slower than kmalloc, because it may have to remap the buffer space into a virtually contiguous range. kmalloc never remaps.
why Linux directly maps the kernel virtual space to the physical memory?
There is one concept in linux kernel known as DMA(Direct Memory Access) which require contiguous physical memory. so when kernel trigger DMA operation we need to specify physically contiguous memory. that's why we need direct memory mapping.
Why the kernel needs to calculate the physical address? It has to use the virtual address to access the memory anyway, right?
for this question answer you need to read difference between virtual memory and physical memory. but in short, every load and store operation is performed on physical memory(You RAM on PC)
physical memory point to RAM.
virtual memory point to swap area of your HARD DISK.
What's the benifit of allocating a chunk of contiguous physical memory?
Is it faster when access the contiguous physical address than virtual address? And why?
All memory accesses from the CPU go through the MMU; the speed does not depend on the actual location of the pages in physical memory.
Physically contiguous memory is needed for other devices that access memory but are not able to remap pages.
In that case, the contiguous allocation is needed to make the device work to begin with, and is not a question of speed.