feeling_lonely
feeling_lonely

Reputation: 6853

How to serve a page fault in the linux kernel?

I am working on a project that requires heavy modifications in the Linux kernel. In one of the modifications I have to change the way the page fault handler works. I would like to be to intercept page faults from specific processes and satisfy them possible by getting copying data from another machine.

As a first step, I would like to write some experimentation code that can help me understand how Linux satisfies a page fault and how it also tells the process that the page fault can not be served right now and it needs to do a retry at a later time.

So, I would like to modify handle_mm_fault in a way that helps me understand all the above. Something like this:

int handle_mm_fault(struct mm_struct *mm, struct vm_area_struct *vma,
                     unsigned long address, unsigned int flags)
{
     /* some code */
     if(current->pid == my_target_pid)
     {
         /*
          1. Chose randomly between 1 and 2 -> rand_num
          2. if rand_num from (1) is 1 then allocate a block of memory, write 'X's to it and then give it to the process and then return.
          3. if rand_num from (1) is 2 then tell process to come back later and then return.
         */
     }
     /* rest of handle_mm_fault for all other process here */
}

Upvotes: 3

Views: 3551

Answers (2)

ya07228
ya07228

Reputation: 61

You can have a look at the struct vm_operations_struct. Its function member 'fault' is used to deal with the page fault situation

Upvotes: 1

user3373708
user3373708

Reputation: 57

The question you described sound like page demanding for data abort. First of all, data abort could happen because of invalid page mapping from kernel space or user space. handle_mm_fault is the sub-routine to fix the page table for user space in linux. Your design has to cover followings as far as I can understand.

  1. You need a design in place to keep track of the right PID.
  2. Have you ever considered, how do you decide which part of vma should rely on demanding
    Page? The whole process VMA or just some parts? Linux could use other techniques to create memory mapping for user programs, such as mmap.
  3. In order to avoid keeping retry, you have to fix the mapping anyway as CPU will resume execution from aborted position. If you can't server the mapping from your designated area immediately, a temporary mapping should be created in stead and page out later.

Upvotes: 0

Related Questions