Commit | Line | Data |
---|---|---|
f8af4da3 HD |
1 | #ifndef __LINUX_KSM_H |
2 | #define __LINUX_KSM_H | |
3 | /* | |
4 | * Memory merging support. | |
5 | * | |
6 | * This code enables dynamic sharing of identical pages found in different | |
7 | * memory areas, even if they are not shared by fork(). | |
8 | */ | |
9 | ||
10 | #include <linux/bitops.h> | |
11 | #include <linux/mm.h> | |
5ad64688 HD |
12 | #include <linux/pagemap.h> |
13 | #include <linux/rmap.h> | |
f8af4da3 HD |
14 | #include <linux/sched.h> |
15 | ||
08beca44 | 16 | struct stable_node; |
5ad64688 | 17 | struct mem_cgroup; |
08beca44 | 18 | |
4969c119 AA |
19 | struct page *ksm_does_need_to_copy(struct page *page, |
20 | struct vm_area_struct *vma, unsigned long address); | |
21 | ||
f8af4da3 HD |
22 | #ifdef CONFIG_KSM |
23 | int ksm_madvise(struct vm_area_struct *vma, unsigned long start, | |
24 | unsigned long end, int advice, unsigned long *vm_flags); | |
25 | int __ksm_enter(struct mm_struct *mm); | |
1c2fb7a4 | 26 | void __ksm_exit(struct mm_struct *mm); |
f8af4da3 HD |
27 | |
28 | static inline int ksm_fork(struct mm_struct *mm, struct mm_struct *oldmm) | |
29 | { | |
30 | if (test_bit(MMF_VM_MERGEABLE, &oldmm->flags)) | |
31 | return __ksm_enter(mm); | |
32 | return 0; | |
33 | } | |
34 | ||
1c2fb7a4 | 35 | static inline void ksm_exit(struct mm_struct *mm) |
f8af4da3 HD |
36 | { |
37 | if (test_bit(MMF_VM_MERGEABLE, &mm->flags)) | |
1c2fb7a4 | 38 | __ksm_exit(mm); |
f8af4da3 | 39 | } |
9a840895 HD |
40 | |
41 | /* | |
42 | * A KSM page is one of those write-protected "shared pages" or "merged pages" | |
43 | * which KSM maps into multiple mms, wherever identical anonymous page content | |
08beca44 HD |
44 | * is found in VM_MERGEABLE vmas. It's a PageAnon page, pointing not to any |
45 | * anon_vma, but to that page's node of the stable tree. | |
9a840895 HD |
46 | */ |
47 | static inline int PageKsm(struct page *page) | |
48 | { | |
3ca7b3c5 HD |
49 | return ((unsigned long)page->mapping & PAGE_MAPPING_FLAGS) == |
50 | (PAGE_MAPPING_ANON | PAGE_MAPPING_KSM); | |
9a840895 HD |
51 | } |
52 | ||
08beca44 HD |
53 | static inline struct stable_node *page_stable_node(struct page *page) |
54 | { | |
55 | return PageKsm(page) ? page_rmapping(page) : NULL; | |
56 | } | |
57 | ||
58 | static inline void set_page_stable_node(struct page *page, | |
59 | struct stable_node *stable_node) | |
60 | { | |
61 | page->mapping = (void *)stable_node + | |
62 | (PAGE_MAPPING_ANON | PAGE_MAPPING_KSM); | |
63 | } | |
64 | ||
5ad64688 HD |
65 | /* |
66 | * When do_swap_page() first faults in from swap what used to be a KSM page, | |
67 | * no problem, it will be assigned to this vma's anon_vma; but thereafter, | |
68 | * it might be faulted into a different anon_vma (or perhaps to a different | |
69 | * offset in the same anon_vma). do_swap_page() cannot do all the locking | |
70 | * needed to reconstitute a cross-anon_vma KSM page: for now it has to make | |
71 | * a copy, and leave remerging the pages to a later pass of ksmd. | |
72 | * | |
73 | * We'd like to make this conditional on vma->vm_flags & VM_MERGEABLE, | |
74 | * but what if the vma was unmerged while the page was swapped out? | |
75 | */ | |
4969c119 | 76 | static inline int ksm_might_need_to_copy(struct page *page, |
5ad64688 | 77 | struct vm_area_struct *vma, unsigned long address) |
9a840895 | 78 | { |
5ad64688 HD |
79 | struct anon_vma *anon_vma = page_anon_vma(page); |
80 | ||
4969c119 AA |
81 | return anon_vma && |
82 | (anon_vma->root != vma->anon_vma->root || | |
83 | page->index != linear_page_index(vma, address)); | |
9a840895 | 84 | } |
5ad64688 HD |
85 | |
86 | int page_referenced_ksm(struct page *page, | |
87 | struct mem_cgroup *memcg, unsigned long *vm_flags); | |
88 | int try_to_unmap_ksm(struct page *page, enum ttu_flags flags); | |
e9995ef9 HD |
89 | int rmap_walk_ksm(struct page *page, int (*rmap_one)(struct page *, |
90 | struct vm_area_struct *, unsigned long, void *), void *arg); | |
91 | void ksm_migrate_page(struct page *newpage, struct page *oldpage); | |
5ad64688 | 92 | |
f8af4da3 HD |
93 | #else /* !CONFIG_KSM */ |
94 | ||
f8af4da3 HD |
95 | static inline int ksm_fork(struct mm_struct *mm, struct mm_struct *oldmm) |
96 | { | |
97 | return 0; | |
98 | } | |
99 | ||
1c2fb7a4 | 100 | static inline void ksm_exit(struct mm_struct *mm) |
f8af4da3 HD |
101 | { |
102 | } | |
9a840895 HD |
103 | |
104 | static inline int PageKsm(struct page *page) | |
105 | { | |
106 | return 0; | |
107 | } | |
108 | ||
f42647ac HD |
109 | #ifdef CONFIG_MMU |
110 | static inline int ksm_madvise(struct vm_area_struct *vma, unsigned long start, | |
111 | unsigned long end, int advice, unsigned long *vm_flags) | |
112 | { | |
113 | return 0; | |
114 | } | |
115 | ||
4969c119 | 116 | static inline int ksm_might_need_to_copy(struct page *page, |
5ad64688 HD |
117 | struct vm_area_struct *vma, unsigned long address) |
118 | { | |
4969c119 | 119 | return 0; |
5ad64688 HD |
120 | } |
121 | ||
122 | static inline int page_referenced_ksm(struct page *page, | |
123 | struct mem_cgroup *memcg, unsigned long *vm_flags) | |
124 | { | |
125 | return 0; | |
126 | } | |
127 | ||
128 | static inline int try_to_unmap_ksm(struct page *page, enum ttu_flags flags) | |
129 | { | |
130 | return 0; | |
131 | } | |
e9995ef9 HD |
132 | |
133 | static inline int rmap_walk_ksm(struct page *page, int (*rmap_one)(struct page*, | |
134 | struct vm_area_struct *, unsigned long, void *), void *arg) | |
135 | { | |
136 | return 0; | |
137 | } | |
138 | ||
139 | static inline void ksm_migrate_page(struct page *newpage, struct page *oldpage) | |
140 | { | |
141 | } | |
f42647ac | 142 | #endif /* CONFIG_MMU */ |
f8af4da3 HD |
143 | #endif /* !CONFIG_KSM */ |
144 | ||
5ad64688 | 145 | #endif /* __LINUX_KSM_H */ |