ksm.h 4.0 KB

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