ksm: support unsharing KSM-placed zero pages
[linux-2.6-microblaze.git] / include / linux / ksm.h
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
22 void ksm_add_vma(struct vm_area_struct *vma);
23 int ksm_enable_merge_any(struct mm_struct *mm);
24 int ksm_disable_merge_any(struct mm_struct *mm);
25 int ksm_disable(struct mm_struct *mm);
26
27 int __ksm_enter(struct mm_struct *mm);
28 void __ksm_exit(struct mm_struct *mm);
29 /*
30  * To identify zeropages that were mapped by KSM, we reuse the dirty bit
31  * in the PTE. If the PTE is dirty, the zeropage was mapped by KSM when
32  * deduplicating memory.
33  */
34 #define is_ksm_zero_pte(pte)    (is_zero_pfn(pte_pfn(pte)) && pte_dirty(pte))
35
36 static inline int ksm_fork(struct mm_struct *mm, struct mm_struct *oldmm)
37 {
38         int ret;
39
40         if (test_bit(MMF_VM_MERGEABLE, &oldmm->flags)) {
41                 ret = __ksm_enter(mm);
42                 if (ret)
43                         return ret;
44         }
45
46         if (test_bit(MMF_VM_MERGE_ANY, &oldmm->flags))
47                 set_bit(MMF_VM_MERGE_ANY, &mm->flags);
48
49         return 0;
50 }
51
52 static inline void ksm_exit(struct mm_struct *mm)
53 {
54         if (test_bit(MMF_VM_MERGEABLE, &mm->flags))
55                 __ksm_exit(mm);
56 }
57
58 /*
59  * When do_swap_page() first faults in from swap what used to be a KSM page,
60  * no problem, it will be assigned to this vma's anon_vma; but thereafter,
61  * it might be faulted into a different anon_vma (or perhaps to a different
62  * offset in the same anon_vma).  do_swap_page() cannot do all the locking
63  * needed to reconstitute a cross-anon_vma KSM page: for now it has to make
64  * a copy, and leave remerging the pages to a later pass of ksmd.
65  *
66  * We'd like to make this conditional on vma->vm_flags & VM_MERGEABLE,
67  * but what if the vma was unmerged while the page was swapped out?
68  */
69 struct page *ksm_might_need_to_copy(struct page *page,
70                         struct vm_area_struct *vma, unsigned long address);
71
72 void rmap_walk_ksm(struct folio *folio, struct rmap_walk_control *rwc);
73 void folio_migrate_ksm(struct folio *newfolio, struct folio *folio);
74
75 #ifdef CONFIG_MEMORY_FAILURE
76 void collect_procs_ksm(struct page *page, struct list_head *to_kill,
77                        int force_early);
78 #endif
79
80 #ifdef CONFIG_PROC_FS
81 long ksm_process_profit(struct mm_struct *);
82 #endif /* CONFIG_PROC_FS */
83
84 #else  /* !CONFIG_KSM */
85
86 static inline void ksm_add_vma(struct vm_area_struct *vma)
87 {
88 }
89
90 static inline int ksm_disable(struct mm_struct *mm)
91 {
92         return 0;
93 }
94
95 static inline int ksm_fork(struct mm_struct *mm, struct mm_struct *oldmm)
96 {
97         return 0;
98 }
99
100 static inline void ksm_exit(struct mm_struct *mm)
101 {
102 }
103
104 #ifdef CONFIG_MEMORY_FAILURE
105 static inline void collect_procs_ksm(struct page *page,
106                                      struct list_head *to_kill, int force_early)
107 {
108 }
109 #endif
110
111 #ifdef CONFIG_MMU
112 static inline int ksm_madvise(struct vm_area_struct *vma, unsigned long start,
113                 unsigned long end, int advice, unsigned long *vm_flags)
114 {
115         return 0;
116 }
117
118 static inline struct page *ksm_might_need_to_copy(struct page *page,
119                         struct vm_area_struct *vma, unsigned long address)
120 {
121         return page;
122 }
123
124 static inline void rmap_walk_ksm(struct folio *folio,
125                         struct rmap_walk_control *rwc)
126 {
127 }
128
129 static inline void folio_migrate_ksm(struct folio *newfolio, struct folio *old)
130 {
131 }
132 #endif /* CONFIG_MMU */
133 #endif /* !CONFIG_KSM */
134
135 #endif /* __LINUX_KSM_H */