ksm.h 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. #ifndef __LINUX_KSM_H
  3. #define __LINUX_KSM_H
  4. /*
  5. * Memory merging support.
  6. *
  7. * This code enables dynamic sharing of identical pages found in different
  8. * memory areas, even if they are not shared by fork().
  9. */
  10. #include <linux/bitops.h>
  11. #include <linux/mm.h>
  12. #include <linux/pagemap.h>
  13. #include <linux/rmap.h>
  14. #include <linux/sched.h>
  15. #include <linux/sched/coredump.h>
  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. * When do_swap_page() first faults in from swap what used to be a KSM page,
  34. * no problem, it will be assigned to this vma's anon_vma; but thereafter,
  35. * it might be faulted into a different anon_vma (or perhaps to a different
  36. * offset in the same anon_vma). do_swap_page() cannot do all the locking
  37. * needed to reconstitute a cross-anon_vma KSM page: for now it has to make
  38. * a copy, and leave remerging the pages to a later pass of ksmd.
  39. *
  40. * We'd like to make this conditional on vma->vm_flags & VM_MERGEABLE,
  41. * but what if the vma was unmerged while the page was swapped out?
  42. */
  43. struct page *ksm_might_need_to_copy(struct page *page,
  44. struct vm_area_struct *vma, unsigned long address);
  45. void rmap_walk_ksm(struct folio *folio, struct rmap_walk_control *rwc);
  46. void folio_migrate_ksm(struct folio *newfolio, struct folio *folio);
  47. #else /* !CONFIG_KSM */
  48. static inline int ksm_fork(struct mm_struct *mm, struct mm_struct *oldmm)
  49. {
  50. return 0;
  51. }
  52. static inline void ksm_exit(struct mm_struct *mm)
  53. {
  54. }
  55. #ifdef CONFIG_MMU
  56. static inline int ksm_madvise(struct vm_area_struct *vma, unsigned long start,
  57. unsigned long end, int advice, unsigned long *vm_flags)
  58. {
  59. return 0;
  60. }
  61. static inline struct page *ksm_might_need_to_copy(struct page *page,
  62. struct vm_area_struct *vma, unsigned long address)
  63. {
  64. return page;
  65. }
  66. static inline void rmap_walk_ksm(struct folio *folio,
  67. struct rmap_walk_control *rwc)
  68. {
  69. }
  70. static inline void folio_migrate_ksm(struct folio *newfolio, struct folio *old)
  71. {
  72. }
  73. #endif /* CONFIG_MMU */
  74. #endif /* !CONFIG_KSM */
  75. #endif /* __LINUX_KSM_H */