blob: 157d83dbaef890eddef04a17d1cc12aab4f07445 [file] [log] [blame]
Hugh Dickinsf8af4da2009-09-21 17:01:57 -07001#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>
Hugh Dickins5ad64682009-12-14 17:59:24 -080012#include <linux/pagemap.h>
13#include <linux/rmap.h>
Hugh Dickinsf8af4da2009-09-21 17:01:57 -070014#include <linux/sched.h>
15
Hugh Dickins08beca42009-12-14 17:59:21 -080016struct stable_node;
Hugh Dickins5ad64682009-12-14 17:59:24 -080017struct mem_cgroup;
Hugh Dickins08beca42009-12-14 17:59:21 -080018
Hugh Dickinsf8af4da2009-09-21 17:01:57 -070019#ifdef CONFIG_KSM
20int ksm_madvise(struct vm_area_struct *vma, unsigned long start,
21 unsigned long end, int advice, unsigned long *vm_flags);
22int __ksm_enter(struct mm_struct *mm);
Andrea Arcangeli1c2fb7a2009-09-21 17:02:22 -070023void __ksm_exit(struct mm_struct *mm);
Hugh Dickinsf8af4da2009-09-21 17:01:57 -070024
25static inline int ksm_fork(struct mm_struct *mm, struct mm_struct *oldmm)
26{
27 if (test_bit(MMF_VM_MERGEABLE, &oldmm->flags))
28 return __ksm_enter(mm);
29 return 0;
30}
31
Andrea Arcangeli1c2fb7a2009-09-21 17:02:22 -070032static inline void ksm_exit(struct mm_struct *mm)
Hugh Dickinsf8af4da2009-09-21 17:01:57 -070033{
34 if (test_bit(MMF_VM_MERGEABLE, &mm->flags))
Andrea Arcangeli1c2fb7a2009-09-21 17:02:22 -070035 __ksm_exit(mm);
Hugh Dickinsf8af4da2009-09-21 17:01:57 -070036}
Hugh Dickins9a840892009-09-21 17:02:01 -070037
38/*
39 * A KSM page is one of those write-protected "shared pages" or "merged pages"
40 * which KSM maps into multiple mms, wherever identical anonymous page content
Hugh Dickins08beca42009-12-14 17:59:21 -080041 * is found in VM_MERGEABLE vmas. It's a PageAnon page, pointing not to any
42 * anon_vma, but to that page's node of the stable tree.
Hugh Dickins9a840892009-09-21 17:02:01 -070043 */
44static inline int PageKsm(struct page *page)
45{
Hugh Dickins3ca7b3c2009-12-14 17:58:57 -080046 return ((unsigned long)page->mapping & PAGE_MAPPING_FLAGS) ==
47 (PAGE_MAPPING_ANON | PAGE_MAPPING_KSM);
Hugh Dickins9a840892009-09-21 17:02:01 -070048}
49
Hugh Dickins08beca42009-12-14 17:59:21 -080050static inline struct stable_node *page_stable_node(struct page *page)
51{
52 return PageKsm(page) ? page_rmapping(page) : NULL;
53}
54
55static inline void set_page_stable_node(struct page *page,
56 struct stable_node *stable_node)
57{
58 page->mapping = (void *)stable_node +
59 (PAGE_MAPPING_ANON | PAGE_MAPPING_KSM);
60}
61
Hugh Dickins5ad64682009-12-14 17:59:24 -080062/*
63 * When do_swap_page() first faults in from swap what used to be a KSM page,
64 * no problem, it will be assigned to this vma's anon_vma; but thereafter,
65 * it might be faulted into a different anon_vma (or perhaps to a different
66 * offset in the same anon_vma). do_swap_page() cannot do all the locking
67 * needed to reconstitute a cross-anon_vma KSM page: for now it has to make
68 * a copy, and leave remerging the pages to a later pass of ksmd.
69 *
70 * We'd like to make this conditional on vma->vm_flags & VM_MERGEABLE,
71 * but what if the vma was unmerged while the page was swapped out?
72 */
73struct page *ksm_does_need_to_copy(struct page *page,
74 struct vm_area_struct *vma, unsigned long address);
75static inline struct page *ksm_might_need_to_copy(struct page *page,
76 struct vm_area_struct *vma, unsigned long address)
Hugh Dickins9a840892009-09-21 17:02:01 -070077{
Hugh Dickins5ad64682009-12-14 17:59:24 -080078 struct anon_vma *anon_vma = page_anon_vma(page);
79
80 if (!anon_vma ||
81 (anon_vma == vma->anon_vma &&
82 page->index == linear_page_index(vma, address)))
83 return page;
84
85 return ksm_does_need_to_copy(page, vma, address);
Hugh Dickins9a840892009-09-21 17:02:01 -070086}
Hugh Dickins5ad64682009-12-14 17:59:24 -080087
88int page_referenced_ksm(struct page *page,
89 struct mem_cgroup *memcg, unsigned long *vm_flags);
90int try_to_unmap_ksm(struct page *page, enum ttu_flags flags);
91
Hugh Dickinsf8af4da2009-09-21 17:01:57 -070092#else /* !CONFIG_KSM */
93
94static inline int ksm_madvise(struct vm_area_struct *vma, unsigned long start,
95 unsigned long end, int advice, unsigned long *vm_flags)
96{
97 return 0;
98}
99
100static inline int ksm_fork(struct mm_struct *mm, struct mm_struct *oldmm)
101{
102 return 0;
103}
104
Andrea Arcangeli1c2fb7a2009-09-21 17:02:22 -0700105static inline void ksm_exit(struct mm_struct *mm)
Hugh Dickinsf8af4da2009-09-21 17:01:57 -0700106{
107}
Hugh Dickins9a840892009-09-21 17:02:01 -0700108
109static inline int PageKsm(struct page *page)
110{
111 return 0;
112}
113
Hugh Dickins5ad64682009-12-14 17:59:24 -0800114static inline struct page *ksm_might_need_to_copy(struct page *page,
115 struct vm_area_struct *vma, unsigned long address)
116{
117 return page;
118}
119
120static inline int page_referenced_ksm(struct page *page,
121 struct mem_cgroup *memcg, unsigned long *vm_flags)
122{
123 return 0;
124}
125
126static inline int try_to_unmap_ksm(struct page *page, enum ttu_flags flags)
127{
128 return 0;
129}
Hugh Dickinsf8af4da2009-09-21 17:01:57 -0700130#endif /* !CONFIG_KSM */
131
Hugh Dickins5ad64682009-12-14 17:59:24 -0800132#endif /* __LINUX_KSM_H */