4
votes

I've got a user-mode process and kernel module. Now I want to read certain regions of usermode process from kernel, but there's one catch: no copying of usermode memory and simple access by VA. So what we have: task_struct for target process, other related structs (like mm_struct, vma_struct) and virtual address like 0x0070abcd that I want to read or rather map somehow to my kernel module.

I can get page list using get_user_pages for desired memory regions, but what next? Should I map pages somehow into kernel and then try to read them as continuous memory region or there are better solutions?

1
Not an answer since I can't currently verify the details, but I suggest you take a look at mm/memory.c, at the implementation of access_process_vm. - Hasturkun
Looks like access_process_vm calls copy_from_user_page. I don't want to copy userspace pages unless it's the only solution available. - BSen
copy_from_user_page seems to be a simple memcpy() most of the time, some arch/platform implementations appear to handle D/I cache first. I'd recommend caution unless you know that you're running on x86 or similar. - Hasturkun
Here is an answer to a similar question from Robert Love, the author of Linux Kernel Development: quora.com/Linux-Kernel-How-does-copy_to_user-work - TheAhmad

1 Answers

1
votes

The problem is that "looking" at user-space requires locking a ton of stuff. So it's better that you do a short copy than leave everything locked for arbitrary amounts of time. Your user-space process may not be VM-mapped into the current CPU. In fact, it may be entirely swapped out to disk, running on another CPU, in the middle of it's own kernel call, etc.

Linux Kernel: copy_from_user - struct with pointers