ksm.h 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. #ifndef __LINUX_KSM_H
  2. #define __LINUX_KSM_H
  3. /*
  4. * Memory merging support.
  5. *
  6. * This code enables dynamic sharing of identical pages found in different
  7. * memory areas, even if they are not shared by fork().
  8. */
  9. #include <linux/bitops.h>
  10. #include <linux/mm.h>
  11. #include <linux/sched.h>
  12. #include <linux/vmstat.h>
  13. struct mmu_gather;
  14. #ifdef CONFIG_KSM
  15. int ksm_madvise(struct vm_area_struct *vma, unsigned long start,
  16. unsigned long end, int advice, unsigned long *vm_flags);
  17. int __ksm_enter(struct mm_struct *mm);
  18. void __ksm_exit(struct mm_struct *mm);
  19. static inline int ksm_fork(struct mm_struct *mm, struct mm_struct *oldmm)
  20. {
  21. if (test_bit(MMF_VM_MERGEABLE, &oldmm->flags))
  22. return __ksm_enter(mm);
  23. return 0;
  24. }
  25. /*
  26. * For KSM to handle OOM without deadlock when it's breaking COW in a
  27. * likely victim of the OOM killer, exit_mmap() has to serialize with
  28. * ksm_exit() after freeing mm's pages but before freeing its page tables.
  29. * That leaves a window in which KSM might refault pages which have just
  30. * been finally unmapped: guard against that with ksm_test_exit(), and
  31. * use it after getting mmap_sem in ksm.c, to check if mm is exiting.
  32. */
  33. static inline bool ksm_test_exit(struct mm_struct *mm)
  34. {
  35. return atomic_read(&mm->mm_users) == 0;
  36. }
  37. static inline void ksm_exit(struct mm_struct *mm)
  38. {
  39. if (test_bit(MMF_VM_MERGEABLE, &mm->flags))
  40. __ksm_exit(mm);
  41. }
  42. /*
  43. * A KSM page is one of those write-protected "shared pages" or "merged pages"
  44. * which KSM maps into multiple mms, wherever identical anonymous page content
  45. * is found in VM_MERGEABLE vmas. It's a PageAnon page, with NULL anon_vma.
  46. */
  47. static inline int PageKsm(struct page *page)
  48. {
  49. return ((unsigned long)page->mapping == PAGE_MAPPING_ANON);
  50. }
  51. /*
  52. * But we have to avoid the checking which page_add_anon_rmap() performs.
  53. */
  54. static inline void page_add_ksm_rmap(struct page *page)
  55. {
  56. if (atomic_inc_and_test(&page->_mapcount)) {
  57. page->mapping = (void *) PAGE_MAPPING_ANON;
  58. __inc_zone_page_state(page, NR_ANON_PAGES);
  59. }
  60. }
  61. #else /* !CONFIG_KSM */
  62. static inline int ksm_madvise(struct vm_area_struct *vma, unsigned long start,
  63. unsigned long end, int advice, unsigned long *vm_flags)
  64. {
  65. return 0;
  66. }
  67. static inline int ksm_fork(struct mm_struct *mm, struct mm_struct *oldmm)
  68. {
  69. return 0;
  70. }
  71. static inline bool ksm_test_exit(struct mm_struct *mm)
  72. {
  73. return 0;
  74. }
  75. static inline void ksm_exit(struct mm_struct *mm)
  76. {
  77. }
  78. static inline int PageKsm(struct page *page)
  79. {
  80. return 0;
  81. }
  82. /* No stub required for page_add_ksm_rmap(page) */
  83. #endif /* !CONFIG_KSM */
  84. #endif