ksm.c 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. /*
  2. * Initial dummy version just to illustrate KSM's interface to other files.
  3. */
  4. #include <linux/errno.h>
  5. #include <linux/mman.h>
  6. #include <linux/ksm.h>
  7. int ksm_madvise(struct vm_area_struct *vma, unsigned long start,
  8. unsigned long end, int advice, unsigned long *vm_flags)
  9. {
  10. struct mm_struct *mm = vma->vm_mm;
  11. switch (advice) {
  12. case MADV_MERGEABLE:
  13. /*
  14. * Be somewhat over-protective for now!
  15. */
  16. if (*vm_flags & (VM_MERGEABLE | VM_SHARED | VM_MAYSHARE |
  17. VM_PFNMAP | VM_IO | VM_DONTEXPAND |
  18. VM_RESERVED | VM_HUGETLB | VM_INSERTPAGE |
  19. VM_MIXEDMAP | VM_SAO))
  20. return 0; /* just ignore the advice */
  21. if (!test_bit(MMF_VM_MERGEABLE, &mm->flags))
  22. if (__ksm_enter(mm) < 0)
  23. return -EAGAIN;
  24. *vm_flags |= VM_MERGEABLE;
  25. break;
  26. case MADV_UNMERGEABLE:
  27. if (!(*vm_flags & VM_MERGEABLE))
  28. return 0; /* just ignore the advice */
  29. /* Unmerge any merged pages here */
  30. *vm_flags &= ~VM_MERGEABLE;
  31. break;
  32. }
  33. return 0;
  34. }
  35. int __ksm_enter(struct mm_struct *mm)
  36. {
  37. /* Allocate a structure to track mm and link it into KSM's list */
  38. set_bit(MMF_VM_MERGEABLE, &mm->flags);
  39. return 0;
  40. }
  41. void __ksm_exit(struct mm_struct *mm)
  42. {
  43. /* Unlink and free all KSM's structures which track this mm */
  44. clear_bit(MMF_VM_MERGEABLE, &mm->flags);
  45. }