ksm.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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/pagemap.h>
  12. #include <linux/rmap.h>
  13. #include <linux/sched.h>
  14. struct stable_node;
  15. struct mem_cgroup;
  16. struct page *ksm_does_need_to_copy(struct page *page,
  17. struct vm_area_struct *vma, unsigned long address);
  18. #ifdef CONFIG_KSM
  19. int ksm_madvise(struct vm_area_struct *vma, unsigned long start,
  20. unsigned long end, int advice, unsigned long *vm_flags);
  21. int __ksm_enter(struct mm_struct *mm);
  22. void __ksm_exit(struct mm_struct *mm);
  23. static inline int ksm_fork(struct mm_struct *mm, struct mm_struct *oldmm)
  24. {
  25. if (test_bit(MMF_VM_MERGEABLE, &oldmm->flags))
  26. return __ksm_enter(mm);
  27. return 0;
  28. }
  29. static inline void ksm_exit(struct mm_struct *mm)
  30. {
  31. if (test_bit(MMF_VM_MERGEABLE, &mm->flags))
  32. __ksm_exit(mm);
  33. }
  34. /*
  35. * A KSM page is one of those write-protected "shared pages" or "merged pages"
  36. * which KSM maps into multiple mms, wherever identical anonymous page content
  37. * is found in VM_MERGEABLE vmas. It's a PageAnon page, pointing not to any
  38. * anon_vma, but to that page's node of the stable tree.
  39. */
  40. static inline int PageKsm(struct page *page)
  41. {
  42. return ((unsigned long)page->mapping & PAGE_MAPPING_FLAGS) ==
  43. (PAGE_MAPPING_ANON | PAGE_MAPPING_KSM);
  44. }
  45. static inline struct stable_node *page_stable_node(struct page *page)
  46. {
  47. return PageKsm(page) ? page_rmapping(page) : NULL;
  48. }
  49. static inline void set_page_stable_node(struct page *page,
  50. struct stable_node *stable_node)
  51. {
  52. page->mapping = (void *)stable_node +
  53. (PAGE_MAPPING_ANON | PAGE_MAPPING_KSM);
  54. }
  55. /*
  56. * When do_swap_page() first faults in from swap what used to be a KSM page,
  57. * no problem, it will be assigned to this vma's anon_vma; but thereafter,
  58. * it might be faulted into a different anon_vma (or perhaps to a different
  59. * offset in the same anon_vma). do_swap_page() cannot do all the locking
  60. * needed to reconstitute a cross-anon_vma KSM page: for now it has to make
  61. * a copy, and leave remerging the pages to a later pass of ksmd.
  62. *
  63. * We'd like to make this conditional on vma->vm_flags & VM_MERGEABLE,
  64. * but what if the vma was unmerged while the page was swapped out?
  65. */
  66. static inline int ksm_might_need_to_copy(struct page *page,
  67. struct vm_area_struct *vma, unsigned long address)
  68. {
  69. struct anon_vma *anon_vma = page_anon_vma(page);
  70. return anon_vma &&
  71. (anon_vma->root != vma->anon_vma->root ||
  72. page->index != linear_page_index(vma, address));
  73. }
  74. int page_referenced_ksm(struct page *page,
  75. struct mem_cgroup *memcg, unsigned long *vm_flags);
  76. int try_to_unmap_ksm(struct page *page, enum ttu_flags flags);
  77. int rmap_walk_ksm(struct page *page, int (*rmap_one)(struct page *,
  78. struct vm_area_struct *, unsigned long, void *), void *arg);
  79. void ksm_migrate_page(struct page *newpage, struct page *oldpage);
  80. #else /* !CONFIG_KSM */
  81. static inline int ksm_fork(struct mm_struct *mm, struct mm_struct *oldmm)
  82. {
  83. return 0;
  84. }
  85. static inline void ksm_exit(struct mm_struct *mm)
  86. {
  87. }
  88. static inline int PageKsm(struct page *page)
  89. {
  90. return 0;
  91. }
  92. #ifdef CONFIG_MMU
  93. static inline int ksm_madvise(struct vm_area_struct *vma, unsigned long start,
  94. unsigned long end, int advice, unsigned long *vm_flags)
  95. {
  96. return 0;
  97. }
  98. static inline int ksm_might_need_to_copy(struct page *page,
  99. struct vm_area_struct *vma, unsigned long address)
  100. {
  101. return 0;
  102. }
  103. static inline int page_referenced_ksm(struct page *page,
  104. struct mem_cgroup *memcg, unsigned long *vm_flags)
  105. {
  106. return 0;
  107. }
  108. static inline int try_to_unmap_ksm(struct page *page, enum ttu_flags flags)
  109. {
  110. return 0;
  111. }
  112. static inline int rmap_walk_ksm(struct page *page, int (*rmap_one)(struct page*,
  113. struct vm_area_struct *, unsigned long, void *), void *arg)
  114. {
  115. return 0;
  116. }
  117. static inline void ksm_migrate_page(struct page *newpage, struct page *oldpage)
  118. {
  119. }
  120. #endif /* CONFIG_MMU */
  121. #endif /* !CONFIG_KSM */
  122. #endif /* __LINUX_KSM_H */