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:
do_munmap(my_tsk->mm, saved_address, saved_length);
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.