blob: 3319a6967626e02f91c340080b21950a8310a67f [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
Andrea Arcangeli4969c112010-09-09 16:37:52 -070019struct page *ksm_does_need_to_copy(struct page *page,
20 struct vm_area_struct *vma, unsigned long address);
21
Hugh Dickinsf8af4da2009-09-21 17:01:57 -070022#ifdef CONFIG_KSM
23int ksm_madvise(struct vm_area_struct *vma, unsigned long start,
24 unsigned long end, int advice, unsigned long *vm_flags);
25int __ksm_enter(struct mm_struct *mm);
Andrea Arcangeli1c2fb7a2009-09-21 17:02:22 -070026void __ksm_exit(struct mm_struct *mm);
Hugh Dickinsf8af4da2009-09-21 17:01:57 -070027
28static 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
Andrea Arcangeli1c2fb7a2009-09-21 17:02:22 -070035static inline void ksm_exit(struct mm_struct *mm)
Hugh Dickinsf8af4da2009-09-21 17:01:57 -070036{
37 if (test_bit(MMF_VM_MERGEABLE, &mm->flags))
Andrea Arcangeli1c2fb7a2009-09-21 17:02:22 -070038 __ksm_exit(mm);
Hugh Dickinsf8af4da2009-09-21 17:01:57 -070039}
Hugh Dickins9a840892009-09-21 17:02:01 -070040
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
Hugh Dickins08beca42009-12-14 17:59:21 -080044 * 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.
Hugh Dickins9a840892009-09-21 17:02:01 -070046 */
47static inline int PageKsm(struct page *page)
48{
Hugh Dickins3ca7b3c2009-12-14 17:58:57 -080049 return ((unsigned long)page->mapping & PAGE_MAPPING_FLAGS) ==
50 (PAGE_MAPPING_ANON | PAGE_MAPPING_KSM);
Hugh Dickins9a840892009-09-21 17:02:01 -070051}
52
Hugh Dickins08beca42009-12-14 17:59:21 -080053static inline struct stable_node *page_stable_node(struct page *page)
54{
55 return PageKsm(page) ? page_rmapping(page) : NULL;
56}
57
58static 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
Hugh Dickins5ad64682009-12-14 17:59:24 -080065/*
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 */
Andrea Arcangeli4969c112010-09-09 16:37:52 -070076static inline int ksm_might_need_to_copy(struct page *page,
Hugh Dickins5ad64682009-12-14 17:59:24 -080077 struct vm_area_struct *vma, unsigned long address)
Hugh Dickins9a840892009-09-21 17:02:01 -070078{
Hugh Dickins5ad64682009-12-14 17:59:24 -080079 struct anon_vma *anon_vma = page_anon_vma(page);
80
Andrea Arcangeli4969c112010-09-09 16:37:52 -070081 return anon_vma &&
82 (anon_vma->root != vma->anon_vma->root ||
83 page->index != linear_page_index(vma, address));
Hugh Dickins9a840892009-09-21 17:02:01 -070084}
Hugh Dickins5ad64682009-12-14 17:59:24 -080085
86int page_referenced_ksm(struct page *page,
87 struct mem_cgroup *memcg, unsigned long *vm_flags);
88int try_to_unmap_ksm(struct page *page, enum ttu_flags flags);
Hugh Dickinse9995ef2009-12-14 17:59:31 -080089int rmap_walk_ksm(struct page *page, int (*rmap_one)(struct page *,
90 struct vm_area_struct *, unsigned long, void *), void *arg);
91void ksm_migrate_page(struct page *newpage, struct page *oldpage);
Hugh Dickins5ad64682009-12-14 17:59:24 -080092
Hugh Dickinsf8af4da2009-09-21 17:01:57 -070093#else /* !CONFIG_KSM */
94
Hugh Dickinsf8af4da2009-09-21 17:01:57 -070095static inline int ksm_fork(struct mm_struct *mm, struct mm_struct *oldmm)
96{
97 return 0;
98}
99
Andrea Arcangeli1c2fb7a2009-09-21 17:02:22 -0700100static inline void ksm_exit(struct mm_struct *mm)
Hugh Dickinsf8af4da2009-09-21 17:01:57 -0700101{
102}
Hugh Dickins9a840892009-09-21 17:02:01 -0700103
104static inline int PageKsm(struct page *page)
105{
106 return 0;
107}
108
Hugh Dickinsf42647a2009-12-16 08:56:57 +0000109#ifdef CONFIG_MMU
110static 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
Andrea Arcangeli4969c112010-09-09 16:37:52 -0700116static inline int ksm_might_need_to_copy(struct page *page,
Hugh Dickins5ad64682009-12-14 17:59:24 -0800117 struct vm_area_struct *vma, unsigned long address)
118{
Andrea Arcangeli4969c112010-09-09 16:37:52 -0700119 return 0;
Hugh Dickins5ad64682009-12-14 17:59:24 -0800120}
121
122static inline int page_referenced_ksm(struct page *page,
123 struct mem_cgroup *memcg, unsigned long *vm_flags)
124{
125 return 0;
126}
127
128static inline int try_to_unmap_ksm(struct page *page, enum ttu_flags flags)
129{
130 return 0;
131}
Hugh Dickinse9995ef2009-12-14 17:59:31 -0800132
133static 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
139static inline void ksm_migrate_page(struct page *newpage, struct page *oldpage)
140{
141}
Hugh Dickinsf42647a2009-12-16 08:56:57 +0000142#endif /* CONFIG_MMU */
Hugh Dickinsf8af4da2009-09-21 17:01:57 -0700143#endif /* !CONFIG_KSM */
144
Hugh Dickins5ad64682009-12-14 17:59:24 -0800145#endif /* __LINUX_KSM_H */