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