ksm.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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_might_need_to_copy(struct page *page,
  65. struct vm_area_struct *vma, unsigned long address);
  66. int page_referenced_ksm(struct page *page,
  67. struct mem_cgroup *memcg, unsigned long *vm_flags);
  68. int try_to_unmap_ksm(struct page *page, enum ttu_flags flags);
  69. int rmap_walk_ksm(struct page *page, int (*rmap_one)(struct page *,
  70. struct vm_area_struct *, unsigned long, void *), void *arg);
  71. void ksm_migrate_page(struct page *newpage, struct page *oldpage);
  72. #else /* !CONFIG_KSM */
  73. static inline int ksm_fork(struct mm_struct *mm, struct mm_struct *oldmm)
  74. {
  75. return 0;
  76. }
  77. static inline void ksm_exit(struct mm_struct *mm)
  78. {
  79. }
  80. static inline int PageKsm(struct page *page)
  81. {
  82. return 0;
  83. }
  84. #ifdef CONFIG_MMU
  85. static inline int ksm_madvise(struct vm_area_struct *vma, unsigned long start,
  86. unsigned long end, int advice, unsigned long *vm_flags)
  87. {
  88. return 0;
  89. }
  90. static inline struct page *ksm_might_need_to_copy(struct page *page,
  91. struct vm_area_struct *vma, unsigned long address)
  92. {
  93. return page;
  94. }
  95. static inline int page_referenced_ksm(struct page *page,
  96. struct mem_cgroup *memcg, unsigned long *vm_flags)
  97. {
  98. return 0;
  99. }
  100. static inline int try_to_unmap_ksm(struct page *page, enum ttu_flags flags)
  101. {
  102. return 0;
  103. }
  104. static inline int rmap_walk_ksm(struct page *page, int (*rmap_one)(struct page*,
  105. struct vm_area_struct *, unsigned long, void *), void *arg)
  106. {
  107. return 0;
  108. }
  109. static inline void ksm_migrate_page(struct page *newpage, struct page *oldpage)
  110. {
  111. }
  112. #endif /* CONFIG_MMU */
  113. #endif /* !CONFIG_KSM */
  114. #endif /* __LINUX_KSM_H */