I have existing code that takes a list of struct page *
and builds a descriptor table to share memory with a device. The upper layer of that code currently expects a buffer allocated with vmalloc
or from user space, and uses vmalloc_to_page
to obtain the corresponding struct page *
.
Now the upper layer needs to cope with all kinds of memory, not just memory obtained through vmalloc
. This could be a buffer obtained with kmalloc
, a pointer inside the stack of a kernel thread, or other cases that I'm not aware of. The only guarantee I have is that the caller of this upper layer must ensure that the memory buffer in question is mapped in kernel space at that point (i.e. it is valid to access buffer[i]
for all 0<=i<size
at this point). How do I obtain a struct page*
corresponding to an arbitrary pointer?
Putting it in pseudo-code, I have this:
lower_layer(struct page*);
upper_layer(void *buffer, size_t size) {
for (addr = buffer & PAGE_MASK; addr <= buffer + size; addr += PAGE_SIZE) {
struct page *pg = vmalloc_to_page(addr);
lower_layer(pg);
}
}
and I now need to change upper_layer
to cope with any valid buffer (without changing lower_layer
).
I've found virt_to_page
, which Linux Device Drivers indicates operates on “a logical address, [not] memory from vmalloc
or high memory”. Furthermore, is_vmalloc_addr
tests whether an address comes from vmalloc
, and virt_addr_valid
tests if an address is a valid virtual address (fodder for virt_to_page
; this includes kmalloc(GFP_KERNEL)
and kernel stacks). What about other cases: global buffers, high memory (it'll come one day, though I can ignore it for now), possibly other kinds that I'm not aware of? So I could reformulate my question as:
If it matters, the code is running on ARM (with an MMU), and the kernel version is at least 2.6.26.
I guess what you want is a page table walk, something like (warning, not actual code, locking missing etc):
struct mm_struct *mm = current->mm;
pgd = pgd_offset(mm, address);
pmd = pmd_offset(pgd, address);
pte = *pte_offset_map(pmd, address);
page = pte_page(pte);
But you you should be very very careful with this. the kmalloc address you got might very well be not page aligned for example. This sounds like a very dangerous API to me.