LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
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 10-11-2010, 07:16 PM   #1
trueskyte
LQ Newbie
 
Registered: Oct 2010
Posts: 2

Rep: Reputation: 0
Free user space pages of different user processes from inside kernel space


Hi,

I am working on some kind of weak references, more precisely weak pages.
Therefore I implemented a syscall. It can map (using do_mmap_pgoff) and unmap (using do_munmap) pages and keeps track of them by memorizing the pid, the address that mmap returns and the length of the required memory piece in bytes.

The pages are called weak, because when the swap daemon wants to swap out something, it first calls a function called wfree(int pages_to_free). wfree() is then supposed to unmap the given amount of weak pages (of which was kept track).

I thought this could also be accomplished by using do_munmap (like in the syscall): int do_munmap(struct mm_struct *mm, unsigned long start, size_t len);
But I'm having trouble.
So what I'm trying to do in wfree() is first getting the appropriate mm_struct for each record, which was memorized.
Since I have the pid of the user processes which allocated those weak pages, I do: struct task_struct *my_tsk = find_task_by_vpid(saved_pid);
I think for do_munmap() it is required to hold a semaphore, therefore I do:

Code:
down_write(&my_tsk->mm->mmap_sem);
do_munmap(my_tsk->mm, saved_address, saved_length);
up_write(&my_tsk->mm->mmap_sem);
It's basically the same code as in my syscall. Because the user process that initiated the syscall is the same user process that wants to unmap the pages, the appropriate task_struct there is simply current and the proper mm_struct is current->mm.
With this configuration everything is working fine.

But inside the wfree() function, which wants to unmap pages of arbitrary user processes, I obviously can't just use the current task_struct.
Like I said before I tried to get the proper task_struct with find_task_by_vpid(), but that's not all there is to it, because when it comes to the execution of down_write(&my_tsk->mm->mmap_sem), the system freezes.

Obviously I am doing something wrong, but unfortunately I don't know how to do it the right way. I figured the memory context isn't set correctly and found functions like:
- void use_mm(struct mm_struct *mm)
- void switch_mm(struct mm_struct *prev, struct mm_struct *next, struct task_struct *tsk)
- void activate_mm(struct mm_struct *prev, struct mm_struct *next)

I tried those out and with use_mm() I get a crash and the other two don't seem to have any effect at all.

So long story short: I want to unmap user space pages from different user processes, which were mapped with do_mmap_pgoff().
Can you help me with that?
What am I doing wrong?

Thanks in advance.
 
Old 10-22-2010, 04:37 PM   #2
trueskyte
LQ Newbie
 
Registered: Oct 2010
Posts: 2

Original Poster
Rep: Reputation: 0
Nobody got an idea?
Maybe I can put it in other and fewer words:

Lets say I got some user processes running which mapped some memory with mmap.
Now I want to free some of those mappings from inside kernel space.

I tried:
Code:
struct mm_struct mm = find_task_by_vpid(some_user_pid)->mm;
down_write(&mm->mmap_sem);
do_munmap(mm, address, size);
up_write(&mm->mmap_sem);
But that isn't working. Well, it works when this code is a part of a syscall and some_user_pid is the pid of the process which has initiated the syscall.
But I want it to work regardless of how I got into a kernel space context and it should work with any running user process which mapped something with mmap.

How can I achieve that?
 
  


Reply



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
Division of Logical Memory Space in to User Space and Kernel Space shreshtha Linux - Newbie 2 01-14-2010 09:59 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
Function in kernel space called form user space.. Linux_Kid_ Programming 2 09-22-2009 04:34 AM
how to call socket prog code written in user space from kernel space???HELP kurt2 Programming 2 07-15-2009 09:56 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software > Linux - Kernel

All times are GMT -5. The time now is 08:38 PM.

Main Menu
Advertisement
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
Open Source Consulting | Domain Registration