blob: ef55ce14a2cecb91363f7b2a049ced0a9013cf89 [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>
12#include <linux/sched.h>
Hugh Dickins9a840892009-09-21 17:02:01 -070013#include <linux/vmstat.h>
Hugh Dickinsf8af4da2009-09-21 17:01:57 -070014
Hugh Dickins08beca42009-12-14 17:59:21 -080015struct stable_node;
16
Hugh Dickinsf8af4da2009-09-21 17:01:57 -070017#ifdef CONFIG_KSM
18int ksm_madvise(struct vm_area_struct *vma, unsigned long start,
19 unsigned long end, int advice, unsigned long *vm_flags);
20int __ksm_enter(struct mm_struct *mm);
Andrea Arcangeli1c2fb7a2009-09-21 17:02:22 -070021void __ksm_exit(struct mm_struct *mm);
Hugh Dickinsf8af4da2009-09-21 17:01:57 -070022
23static inline int ksm_fork(struct mm_struct *mm, struct mm_struct *oldmm)
24{
25 if (test_bit(MMF_VM_MERGEABLE, &oldmm->flags))
26 return __ksm_enter(mm);
27 return 0;
28}
29
Andrea Arcangeli1c2fb7a2009-09-21 17:02:22 -070030static inline void ksm_exit(struct mm_struct *mm)
Hugh Dickinsf8af4da2009-09-21 17:01:57 -070031{
32 if (test_bit(MMF_VM_MERGEABLE, &mm->flags))
Andrea Arcangeli1c2fb7a2009-09-21 17:02:22 -070033 __ksm_exit(mm);
Hugh Dickinsf8af4da2009-09-21 17:01:57 -070034}
Hugh Dickins9a840892009-09-21 17:02:01 -070035
36/*
37 * A KSM page is one of those write-protected "shared pages" or "merged pages"
38 * which KSM maps into multiple mms, wherever identical anonymous page content
Hugh Dickins08beca42009-12-14 17:59:21 -080039 * is found in VM_MERGEABLE vmas. It's a PageAnon page, pointing not to any
40 * anon_vma, but to that page's node of the stable tree.
Hugh Dickins9a840892009-09-21 17:02:01 -070041 */
42static inline int PageKsm(struct page *page)
43{
Hugh Dickins3ca7b3c2009-12-14 17:58:57 -080044 return ((unsigned long)page->mapping & PAGE_MAPPING_FLAGS) ==
45 (PAGE_MAPPING_ANON | PAGE_MAPPING_KSM);
Hugh Dickins9a840892009-09-21 17:02:01 -070046}
47
Hugh Dickins08beca42009-12-14 17:59:21 -080048static inline struct stable_node *page_stable_node(struct page *page)
49{
50 return PageKsm(page) ? page_rmapping(page) : NULL;
51}
52
53static inline void set_page_stable_node(struct page *page,
54 struct stable_node *stable_node)
55{
56 page->mapping = (void *)stable_node +
57 (PAGE_MAPPING_ANON | PAGE_MAPPING_KSM);
58}
59
Hugh Dickins9a840892009-09-21 17:02:01 -070060static inline void page_add_ksm_rmap(struct page *page)
61{
Hugh Dickins08beca42009-12-14 17:59:21 -080062 if (atomic_inc_and_test(&page->_mapcount))
Hugh Dickins9a840892009-09-21 17:02:01 -070063 __inc_zone_page_state(page, NR_ANON_PAGES);
Hugh Dickins9a840892009-09-21 17:02:01 -070064}
Hugh Dickinsf8af4da2009-09-21 17:01:57 -070065#else /* !CONFIG_KSM */
66
67static inline int ksm_madvise(struct vm_area_struct *vma, unsigned long start,
68 unsigned long end, int advice, unsigned long *vm_flags)
69{
70 return 0;
71}
72
73static inline int ksm_fork(struct mm_struct *mm, struct mm_struct *oldmm)
74{
75 return 0;
76}
77
Andrea Arcangeli1c2fb7a2009-09-21 17:02:22 -070078static inline void ksm_exit(struct mm_struct *mm)
Hugh Dickinsf8af4da2009-09-21 17:01:57 -070079{
80}
Hugh Dickins9a840892009-09-21 17:02:01 -070081
82static inline int PageKsm(struct page *page)
83{
84 return 0;
85}
86
87/* No stub required for page_add_ksm_rmap(page) */
Hugh Dickinsf8af4da2009-09-21 17:01:57 -070088#endif /* !CONFIG_KSM */
89
90#endif