LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Kernel (https://www.linuxquestions.org/questions/linux-kernel-70/)
-   -   Why? Why SetPageReserved is needed when map a kernel space to user space? (https://www.linuxquestions.org/questions/linux-kernel-70/why-why-setpagereserved-is-needed-when-map-a-kernel-space-to-user-space-885176/)

osdev 06-08-2011 07:03 AM

Why? Why SetPageReserved is needed when map a kernel space to user space?
 

I am confused by this question for long time, no clear answer about this question when google, the only anser is SetPageReserved is used to prevent the page mapped into user space swapping out!!

But the page is also mapped by a kernel virtual address, how can it be swapped out? In other word, if I don't use SetPageReserved when I remap a kernel virtual address into user space, what's the bad result????

Thanks very much!!

paulsm4 06-09-2011 12:47 AM


Quote:

Q: Why? Why SetPageReserved is needed when map a kernel space to user space?
A: Why? Why do you think that's necessarily the case?


PS:
If you're not already familiar with them, Google for the kernel APIs copy_from_user()/copy_to_user(), and the user-space API "mmap()". In general, the kernel isn't allowed to access user-space memory directly.

osdev 06-09-2011 08:13 AM

Quote:

Originally Posted by paulsm4 (Post 4380593)


A: Why? Why do you think that's necessarily the case?


PS:
If you're not already familiar with them, Google for the kernel APIs copy_from_user()/copy_to_user(), and the user-space API "mmap()". In general, the kernel isn't allowed to access user-space memory directly.

I know the user space cant access the kernel memory directly, but my question is when allocate a kernel virtual memory, for instance, __get_free_pages() and map this virtual space to user space by using remap_pfn_range(), but before that, some drivers code in Linux kernel will call SetPageReserved at the VA from __get_from_pages, why SetPageReserved is needed in this case?

nini09 06-09-2011 02:25 PM

User space don't know that these virtual address is from kernel space. They will be swapped out when they think it is necessary.

osdev 06-09-2011 10:26 PM

Quote:

Originally Posted by nini09 (Post 4381255)
User space don't know that these virtual address is from kernel space. They will be swapped out when they think it is necessary.

The question is although the user space doesn't know these address comes from kernel space, but I've mentioned that these virtual address is comes from __get_free_pages, so even the user space mapped to those pages, the kswapd still haven't chance to swap it out. Hence, SetPageReserved is necessary or not for this case??

nini09 06-10-2011 02:30 PM

When user space do mmap, it will map kernel memory to user space normally and call SetPageReserved. For your case, I don't see any different from normal case.

osdev 06-11-2011 04:59 AM

Quote:

Originally Posted by nini09 (Post 4382161)
When user space do mmap, it will map kernel memory to user space normally and call SetPageReserved. For your case, I don't see any different from normal case.

The KEY POINT in my question is why SetpageReserved is needed?

Constantine 09-12-2012 03:45 PM

Strange, but it is
 
Quote:

As the page is accessible form user space also, it may be swapped out. So as to stop it

SetPageReserved(pfn);

This should be done for all pfn's which are mmap'ed.

PTE (page table entries) do not have information whether the pfn belongs to user or kernel.

It just has an entry saying if it is accessible from user space.

(So swapper doesn't care if it is the result of kmalloc in kernel. It just sees accessible from user space bit. swapper's are note mmap() aware)

As this pfn is accessible from user space ,the swapper may think that it can be swapped out.

Hence it is mandatory to lock these pages to stop swapper from swapping them out.
https://sites.google.com/site/skartikeyan/mmap.html

sundialsvcs 09-13-2012 06:50 AM

As noted, this bit stops a "race condition" that could otherwise exist between this code and the swapper. If two pieces of code (the user, and the kernel) are relying upon a particular physical-page, that page is now a shared resource between the two for the duration, and it is necessary to guarantee that the page will not be "stolen" in the meantime. The bit indicates among other things that this page is not a candidate for stealing.

codelvr 03-10-2013 06:16 PM

Though the pages are reserved via a kernel driver, it is meant to be accessed via user space. As a result, the PTE (page table entries) do not know if the pfn belongs to user space or kernel space (even though they are allocated via kernel driver).

This is why they are marked with SetPageReserved.


All times are GMT -5. The time now is 04:37 AM.