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
11 #include <linux/bitops.h>
12 #include <linux/mm.h>
13 #include <linux/pagemap.h>
14 #include <linux/rmap.h>
15 #include <linux/sched.h>
16 #include <linux/sched/coredump.h>
17
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
ksm_fork(struct mm_struct * mm,struct mm_struct * oldmm)24 static inline int ksm_fork(struct mm_struct *mm, struct mm_struct *oldmm)
25 {
26 if (test_bit(MMF_VM_MERGEABLE, &oldmm->flags))
27 return __ksm_enter(mm);
28 return 0;
29 }
30
ksm_exit(struct mm_struct * mm)31 static inline void ksm_exit(struct mm_struct *mm)
32 {
33 if (test_bit(MMF_VM_MERGEABLE, &mm->flags))
34 __ksm_exit(mm);
35 }
36
37 /*
38 * When do_swap_page() first faults in from swap what used to be a KSM page,
39 * no problem, it will be assigned to this vma's anon_vma; but thereafter,
40 * it might be faulted into a different anon_vma (or perhaps to a different
41 * offset in the same anon_vma). do_swap_page() cannot do all the locking
42 * needed to reconstitute a cross-anon_vma KSM page: for now it has to make
43 * a copy, and leave remerging the pages to a later pass of ksmd.
44 *
45 * We'd like to make this conditional on vma->vm_flags & VM_MERGEABLE,
46 * but what if the vma was unmerged while the page was swapped out?
47 */
48 struct page *ksm_might_need_to_copy(struct page *page,
49 struct vm_area_struct *vma, unsigned long address);
50
51 void rmap_walk_ksm(struct folio *folio, struct rmap_walk_control *rwc);
52 void folio_migrate_ksm(struct folio *newfolio, struct folio *folio);
53
54 #else /* !CONFIG_KSM */
55
ksm_fork(struct mm_struct * mm,struct mm_struct * oldmm)56 static inline int ksm_fork(struct mm_struct *mm, struct mm_struct *oldmm)
57 {
58 return 0;
59 }
60
ksm_exit(struct mm_struct * mm)61 static inline void ksm_exit(struct mm_struct *mm)
62 {
63 }
64
65 #ifdef CONFIG_MMU
ksm_madvise(struct vm_area_struct * vma,unsigned long start,unsigned long end,int advice,unsigned long * vm_flags)66 static inline int ksm_madvise(struct vm_area_struct *vma, unsigned long start,
67 unsigned long end, int advice, unsigned long *vm_flags)
68 {
69 return 0;
70 }
71
ksm_might_need_to_copy(struct page * page,struct vm_area_struct * vma,unsigned long address)72 static inline struct page *ksm_might_need_to_copy(struct page *page,
73 struct vm_area_struct *vma, unsigned long address)
74 {
75 return page;
76 }
77
rmap_walk_ksm(struct folio * folio,struct rmap_walk_control * rwc)78 static inline void rmap_walk_ksm(struct folio *folio,
79 struct rmap_walk_control *rwc)
80 {
81 }
82
folio_migrate_ksm(struct folio * newfolio,struct folio * old)83 static inline void folio_migrate_ksm(struct folio *newfolio, struct folio *old)
84 {
85 }
86 #endif /* CONFIG_MMU */
87 #endif /* !CONFIG_KSM */
88
89 #endif /* __LINUX_KSM_H */
90