3

I would like to create a kernel module that is when given an id of a process can lookup its task struct and then its memory map. When it finds that it should attach a function that will serve page faults to a particular set of pages (namely the heap pages).

Cœur
  • 37,241
  • 25
  • 195
  • 267
feeling_lonely
  • 6,665
  • 4
  • 27
  • 53

1 Answers1

4

Set vma->vm_ops->fault of the needed VMA. It can be easier, if you will hack into heap allocator and replace mmaps MMAP_ANONYMOUS with mmap of your special device.

Code related: http://lxr.free-electrons.com/source/mm/memory.c?v=3.12#L3676

3689 static int handle_pte_fault(struct mm_struct *mm,
3690                      struct vm_area_struct *vma, unsigned long address,
3691                      pte_t *pte, pmd_t *pmd, unsigned int flags)
3692 {
3693         pte_t entry;
3694         spinlock_t *ptl;
3695 
3696         entry = *pte;
3697         if (!pte_present(entry)) {
3698                 if (pte_none(entry)) {
3699                         if (vma->vm_ops) {
3700                                 if (likely(vma->vm_ops->fault))  /* HERE */
3701                                         return do_linear_fault(mm, vma, address,
3702                                                 pte, pmd, flags, entry);
3703                         }
3704                         return do_anonymous_page(mm, vma, address,
3705                                                  pte, pmd, flags);
3706                 }
3707                 if (pte_file(entry))
3708                         return do_nonlinear_fault(mm, vma, address,
3709                                         pte, pmd, flags, entry);
3710                 return do_swap_page(mm, vma, address,
3711                                         pte, pmd, flags, entry);
3712         }

Some docs: https://lwn.net/Articles/242625/ "fault()" - [Posted July 23, 2007 by corbet], LWN

You may also check LDD book, chapter 15: http://lwn.net/images/pdf/LDD3/ch15.pdf (bit outdated, it uses nopage hanlder which was replaced with fault() handler in 2007)

osgx
  • 90,338
  • 53
  • 357
  • 513
  • 1
    Thanks for the answer! Is there a complete documentation for this method and what steps it should be implemented to get a correct working function? For example, In some implementations I see a call to get_page(page) to increment the ref count. Well, is there a complete list of requirements to avoid bugs? Thanks. – feeling_lonely Mar 20 '14 at 07:09