Skip to contentSkip to conversation

vmapping user pages - feasible?

DISCUSSION Zenek 2 participants 4 messages Feb 25, 2010 Memory management

lore
Share & tools
lore

Zenek

author
#0 lore raw

Hello,

my kernel driver gets a userspace pointer to a memory area (virtually contiguous), allocated by userspace. I would like to use that memory in a vmalloc-like manner, i.e. I want it to be:

- accessible from kernel
- contiguous in virtual address space
- I may need access from interrupt context as well.

I will be writing to it using the CPU only, in kernel mode.

I understand that:
- no page pinning is required (as only the CPU will be writing to that area)
- I would be able to use that memory even directly without vmapping, but only if I didn't want to access it from interrupt context and as long as it'd be mapped below highmem?

There will be no multithreaded access to that memory.

If the userspace free()s the memory, I still have the pages unless I vunmap() them, right?

How should I go about it? Get the user's vm_area_struct, go through all the pages, construct an array of struct *page and vmap it?

Thank you!
Zenek Blus

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>

#1 lore raw
Quoted 3 lines · Zenek

my kernel driver gets a userspace pointer to a memory area (virtually contiguous), allocated by userspace. I would like to use that memory in a vmalloc-like manner, i.e. I want it to be:

Its already virtually mapped for the process. The kernel can access the data.

Quoted 1 line

I will be writing to it using the CPU only, in kernel mode.

Thats possible already.

Quoted 3 lines

I understand that:
- no page pinning is required (as only the CPU will be writing to that area)

Page pinning is required if the access from the kernel is asynchrononous to user space.

Quoted 1 line

There will be no multithreaded access to that memory.

The kernel and userspace are not concurrently accessing the memory?

Quoted 2 lines

If the userspace free()s the memory, I still have the pages unless I vunmap() them, right?

If you increase the page count then you still have the pages. The virtual mapping goes away with the process.

Quoted 2 lines

How should I go about it? Get the user's vm_area_struct, go through all the pages, construct an array of struct *page and vmap it?

Do a get_user_pages() on the range?

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>

author
#2 lore raw

Thank you for your response,

although I'm not sure I've understood...

Quoted 5 lines · Christoph Lameter
Quoted 2 lines · Zenek

I will be writing to it using the CPU only, in kernel mode.

Thats possible already.

What about in the interrupt context?

Quoted 6 lines
Quoted 3 lines

I understand that:
- no page pinning is required (as only the CPU will be writing to that area)

Page pinning is required if the access from the kernel is asynchrononous to user space.

Because simultaneous page faults can happen?

Quoted 3 lines
Quoted 1 line

There will be no multithreaded access to that memory.

The kernel and userspace are not concurrently accessing the memory?

Could we take into account both cases?
1. not concurrently accessing the memory (e.g. ioctl() call, write by kernel, return - no more kernel access after return). I can just use the user mapping, right? What about page faults?
2. concurrently (e.g. ioctl() call, save the pointer in the kernel, return, asynchronous write by kernel). What then? What happens if user process calls free() on that area?

Quoted 4 lines
Quoted 2 lines

How should I go about it? Get the user's vm_area_struct, go through all the pages, construct an array of struct *page and vmap it?

Do a get_user_pages() on the range?

Right... so do a get_user_pages(), pass them to vmap(), and write away... if the process messes up the mapping or dies, it's its fault anyway and it doesn't get the data, but everything is safe until I call vunmap... right?

The pages won't get swapped out until I vunmap, and after that even if they do, they data will get swapped and later restored correctly, right?

Thank you again!
Zenek

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>

author
#3 lore raw
In reply to Christoph Lameter

On Thu, 25 Feb 2010 14:58:52 -0600, Christoph Lameter wrote:

Ah, and one more question if I may:
Can vmalloc()ed memory be swapped out? From Uderstanding the Linux
Kernel I understand that no kernel memory can be swapped out... Can anything besides what have been allocated by user be swapped out?

Thank you!

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>