Hugh Dickins | f8af4da | 2009-09-21 17:01:57 -0700 | [diff] [blame] | 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> |
Hugh Dickins | 5ad6468 | 2009-12-14 17:59:24 -0800 | [diff] [blame] | 12 | #include <linux/pagemap.h> |
| 13 | #include <linux/rmap.h> |
Hugh Dickins | f8af4da | 2009-09-21 17:01:57 -0700 | [diff] [blame] | 14 | #include <linux/sched.h> |
| 15 | |
Hugh Dickins | 08beca4 | 2009-12-14 17:59:21 -0800 | [diff] [blame] | 16 | struct stable_node; |
Hugh Dickins | 5ad6468 | 2009-12-14 17:59:24 -0800 | [diff] [blame] | 17 | struct mem_cgroup; |
Hugh Dickins | 08beca4 | 2009-12-14 17:59:21 -0800 | [diff] [blame] | 18 | |
Hugh Dickins | f8af4da | 2009-09-21 17:01:57 -0700 | [diff] [blame] | 19 | #ifdef CONFIG_KSM |
| 20 | int ksm_madvise(struct vm_area_struct *vma, unsigned long start, |
| 21 | unsigned long end, int advice, unsigned long *vm_flags); |
| 22 | int __ksm_enter(struct mm_struct *mm); |
Andrea Arcangeli | 1c2fb7a | 2009-09-21 17:02:22 -0700 | [diff] [blame] | 23 | void __ksm_exit(struct mm_struct *mm); |
Hugh Dickins | f8af4da | 2009-09-21 17:01:57 -0700 | [diff] [blame] | 24 | |
| 25 | static 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 Arcangeli | 1c2fb7a | 2009-09-21 17:02:22 -0700 | [diff] [blame] | 32 | static inline void ksm_exit(struct mm_struct *mm) |
Hugh Dickins | f8af4da | 2009-09-21 17:01:57 -0700 | [diff] [blame] | 33 | { |
| 34 | if (test_bit(MMF_VM_MERGEABLE, &mm->flags)) |
Andrea Arcangeli | 1c2fb7a | 2009-09-21 17:02:22 -0700 | [diff] [blame] | 35 | __ksm_exit(mm); |
Hugh Dickins | f8af4da | 2009-09-21 17:01:57 -0700 | [diff] [blame] | 36 | } |
Hugh Dickins | 9a84089 | 2009-09-21 17:02:01 -0700 | [diff] [blame] | 37 | |
Hugh Dickins | 08beca4 | 2009-12-14 17:59:21 -0800 | [diff] [blame] | 38 | static inline struct stable_node *page_stable_node(struct page *page) |
| 39 | { |
| 40 | return PageKsm(page) ? page_rmapping(page) : NULL; |
| 41 | } |
| 42 | |
| 43 | static inline void set_page_stable_node(struct page *page, |
| 44 | struct stable_node *stable_node) |
| 45 | { |
Minchan Kim | bda807d | 2016-07-26 15:23:05 -0700 | [diff] [blame] | 46 | page->mapping = (void *)((unsigned long)stable_node | PAGE_MAPPING_KSM); |
Hugh Dickins | 08beca4 | 2009-12-14 17:59:21 -0800 | [diff] [blame] | 47 | } |
| 48 | |
Hugh Dickins | 5ad6468 | 2009-12-14 17:59:24 -0800 | [diff] [blame] | 49 | /* |
| 50 | * When do_swap_page() first faults in from swap what used to be a KSM page, |
| 51 | * no problem, it will be assigned to this vma's anon_vma; but thereafter, |
| 52 | * it might be faulted into a different anon_vma (or perhaps to a different |
| 53 | * offset in the same anon_vma). do_swap_page() cannot do all the locking |
| 54 | * needed to reconstitute a cross-anon_vma KSM page: for now it has to make |
| 55 | * a copy, and leave remerging the pages to a later pass of ksmd. |
| 56 | * |
| 57 | * We'd like to make this conditional on vma->vm_flags & VM_MERGEABLE, |
| 58 | * but what if the vma was unmerged while the page was swapped out? |
| 59 | */ |
Hugh Dickins | cbf86cf | 2013-02-22 16:35:08 -0800 | [diff] [blame] | 60 | struct page *ksm_might_need_to_copy(struct page *page, |
| 61 | struct vm_area_struct *vma, unsigned long address); |
Hugh Dickins | 5ad6468 | 2009-12-14 17:59:24 -0800 | [diff] [blame] | 62 | |
Joonsoo Kim | 051ac83 | 2014-01-21 15:49:48 -0800 | [diff] [blame] | 63 | int rmap_walk_ksm(struct page *page, struct rmap_walk_control *rwc); |
Hugh Dickins | e9995ef | 2009-12-14 17:59:31 -0800 | [diff] [blame] | 64 | void ksm_migrate_page(struct page *newpage, struct page *oldpage); |
Hugh Dickins | 5ad6468 | 2009-12-14 17:59:24 -0800 | [diff] [blame] | 65 | |
Hugh Dickins | f8af4da | 2009-09-21 17:01:57 -0700 | [diff] [blame] | 66 | #else /* !CONFIG_KSM */ |
| 67 | |
Hugh Dickins | f8af4da | 2009-09-21 17:01:57 -0700 | [diff] [blame] | 68 | static inline int ksm_fork(struct mm_struct *mm, struct mm_struct *oldmm) |
| 69 | { |
| 70 | return 0; |
| 71 | } |
| 72 | |
Andrea Arcangeli | 1c2fb7a | 2009-09-21 17:02:22 -0700 | [diff] [blame] | 73 | static inline void ksm_exit(struct mm_struct *mm) |
Hugh Dickins | f8af4da | 2009-09-21 17:01:57 -0700 | [diff] [blame] | 74 | { |
| 75 | } |
Hugh Dickins | 9a84089 | 2009-09-21 17:02:01 -0700 | [diff] [blame] | 76 | |
Hugh Dickins | f42647a | 2009-12-16 08:56:57 +0000 | [diff] [blame] | 77 | #ifdef CONFIG_MMU |
| 78 | static inline int ksm_madvise(struct vm_area_struct *vma, unsigned long start, |
| 79 | unsigned long end, int advice, unsigned long *vm_flags) |
| 80 | { |
| 81 | return 0; |
| 82 | } |
| 83 | |
Hugh Dickins | cbf86cf | 2013-02-22 16:35:08 -0800 | [diff] [blame] | 84 | static inline struct page *ksm_might_need_to_copy(struct page *page, |
Hugh Dickins | 5ad6468 | 2009-12-14 17:59:24 -0800 | [diff] [blame] | 85 | struct vm_area_struct *vma, unsigned long address) |
| 86 | { |
Hugh Dickins | cbf86cf | 2013-02-22 16:35:08 -0800 | [diff] [blame] | 87 | return page; |
Hugh Dickins | 5ad6468 | 2009-12-14 17:59:24 -0800 | [diff] [blame] | 88 | } |
| 89 | |
| 90 | static inline int page_referenced_ksm(struct page *page, |
| 91 | struct mem_cgroup *memcg, unsigned long *vm_flags) |
| 92 | { |
| 93 | return 0; |
| 94 | } |
| 95 | |
Joonsoo Kim | 051ac83 | 2014-01-21 15:49:48 -0800 | [diff] [blame] | 96 | static inline int rmap_walk_ksm(struct page *page, |
| 97 | struct rmap_walk_control *rwc) |
Hugh Dickins | e9995ef | 2009-12-14 17:59:31 -0800 | [diff] [blame] | 98 | { |
| 99 | return 0; |
| 100 | } |
| 101 | |
| 102 | static inline void ksm_migrate_page(struct page *newpage, struct page *oldpage) |
| 103 | { |
| 104 | } |
Hugh Dickins | f42647a | 2009-12-16 08:56:57 +0000 | [diff] [blame] | 105 | #endif /* CONFIG_MMU */ |
Hugh Dickins | f8af4da | 2009-09-21 17:01:57 -0700 | [diff] [blame] | 106 | #endif /* !CONFIG_KSM */ |
| 107 | |
Hugh Dickins | 5ad6468 | 2009-12-14 17:59:24 -0800 | [diff] [blame] | 108 | #endif /* __LINUX_KSM_H */ |