1 /* SPDX-License-Identifier: GPL-2.0 */
5 * Memory merging support.
7 * This code enables dynamic sharing of identical pages found in different
8 * memory areas, even if they are not shared by fork().
11 #include <linux/bitops.h>
13 #include <linux/pagemap.h>
14 #include <linux/rmap.h>
15 #include <linux/sched.h>
16 #include <linux/sched/coredump.h>
19 int ksm_madvise(struct vm_area_struct *vma, unsigned long start,
20 unsigned long end, int advice, unsigned long *vm_flags);
22 void ksm_add_vma(struct vm_area_struct *vma);
23 int ksm_enable_merge_any(struct mm_struct *mm);
25 int __ksm_enter(struct mm_struct *mm);
26 void __ksm_exit(struct mm_struct *mm);
28 static inline int ksm_fork(struct mm_struct *mm, struct mm_struct *oldmm)
32 if (test_bit(MMF_VM_MERGEABLE, &oldmm->flags)) {
33 ret = __ksm_enter(mm);
38 if (test_bit(MMF_VM_MERGE_ANY, &oldmm->flags))
39 set_bit(MMF_VM_MERGE_ANY, &mm->flags);
44 static inline void ksm_exit(struct mm_struct *mm)
46 if (test_bit(MMF_VM_MERGEABLE, &mm->flags))
51 * When do_swap_page() first faults in from swap what used to be a KSM page,
52 * no problem, it will be assigned to this vma's anon_vma; but thereafter,
53 * it might be faulted into a different anon_vma (or perhaps to a different
54 * offset in the same anon_vma). do_swap_page() cannot do all the locking
55 * needed to reconstitute a cross-anon_vma KSM page: for now it has to make
56 * a copy, and leave remerging the pages to a later pass of ksmd.
58 * We'd like to make this conditional on vma->vm_flags & VM_MERGEABLE,
59 * but what if the vma was unmerged while the page was swapped out?
61 struct page *ksm_might_need_to_copy(struct page *page,
62 struct vm_area_struct *vma, unsigned long address);
64 void rmap_walk_ksm(struct folio *folio, struct rmap_walk_control *rwc);
65 void folio_migrate_ksm(struct folio *newfolio, struct folio *folio);
67 #ifdef CONFIG_MEMORY_FAILURE
68 void collect_procs_ksm(struct page *page, struct list_head *to_kill,
73 long ksm_process_profit(struct mm_struct *);
74 #endif /* CONFIG_PROC_FS */
76 #else /* !CONFIG_KSM */
78 static inline void ksm_add_vma(struct vm_area_struct *vma)
82 static inline int ksm_fork(struct mm_struct *mm, struct mm_struct *oldmm)
87 static inline void ksm_exit(struct mm_struct *mm)
91 #ifdef CONFIG_MEMORY_FAILURE
92 static inline void collect_procs_ksm(struct page *page,
93 struct list_head *to_kill, int force_early)
99 static inline int ksm_madvise(struct vm_area_struct *vma, unsigned long start,
100 unsigned long end, int advice, unsigned long *vm_flags)
105 static inline struct page *ksm_might_need_to_copy(struct page *page,
106 struct vm_area_struct *vma, unsigned long address)
111 static inline void rmap_walk_ksm(struct folio *folio,
112 struct rmap_walk_control *rwc)
116 static inline void folio_migrate_ksm(struct folio *newfolio, struct folio *old)
119 #endif /* CONFIG_MMU */
120 #endif /* !CONFIG_KSM */
122 #endif /* __LINUX_KSM_H */