LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software > Linux - Kernel
User Name
Password
Linux - Kernel This forum is for all discussion relating to the Linux kernel.

Notices

Reply
 
Search this Thread
Old 06-08-2011, 07:03 AM   #1
osdev
LQ Newbie
 
Registered: Jun 2011
Posts: 6

Rep: Reputation: Disabled
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!!
 
Old 06-09-2011, 12:47 AM   #2
paulsm4
Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled

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.

Last edited by paulsm4; 06-09-2011 at 12:58 AM.
 
Old 06-09-2011, 08:13 AM   #3
osdev
LQ Newbie
 
Registered: Jun 2011
Posts: 6

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by paulsm4 View Post


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?
 
Old 06-09-2011, 02:25 PM   #4
nini09
Member
 
Registered: Apr 2009
Posts: 968

Rep: Reputation: 67
User space don't know that these virtual address is from kernel space. They will be swapped out when they think it is necessary.
 
Old 06-09-2011, 10:26 PM   #5
osdev
LQ Newbie
 
Registered: Jun 2011
Posts: 6

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by nini09 View Post
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??
 
Old 06-10-2011, 02:30 PM   #6
nini09
Member
 
Registered: Apr 2009
Posts: 968

Rep: Reputation: 67
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.
 
Old 06-11-2011, 04:59 AM   #7
osdev
LQ Newbie
 
Registered: Jun 2011
Posts: 6

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by nini09 View Post
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?
 
Old 09-12-2012, 03:45 PM   #8
Constantine
LQ Newbie
 
Registered: Dec 2007
Posts: 13

Rep: Reputation: 1
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
 
Old 09-13-2012, 06:50 AM   #9
sundialsvcs
Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 5,230

Rep: Reputation: 1071Reputation: 1071Reputation: 1071Reputation: 1071Reputation: 1071Reputation: 1071Reputation: 1071Reputation: 1071
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.
 
1 members found this post helpful.
Old 03-10-2013, 06:16 PM   #10
codelvr
LQ Newbie
 
Registered: Mar 2013
Location: Santa Clara, CA
Distribution: Ubuntu
Posts: 1

Rep: Reputation: Disabled
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.

Last edited by codelvr; 03-10-2013 at 06:20 PM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Free user space pages of different user processes from inside kernel space trueskyte Linux - Kernel 1 10-22-2010 04:37 PM
Division of Logical Memory Space in to User Space and Kernel Space shreshtha Linux - Newbie 2 01-14-2010 09:59 AM
Map kernel physical space to user space zvivered Linux - Embedded & Single-board computer 2 12-11-2009 06:18 AM
Do we have any chance of calling user space callback function from kernel space? ravishankar.g Linux - Newbie 1 09-22-2009 07:14 PM
how to call socket prog code written in user space from kernel space???HELP kurt2 Programming 2 07-15-2009 09:56 PM


All times are GMT -5. The time now is 12:32 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration