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:
- What are all the kinds of memory zones in the kernel?
- How do I tell them apart?
- How do I obtain page mapping information for each of them?
If it matters, the code is running on ARM (with an MMU), and the kernel version is at least 2.6.26.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…