Greg Kroah-Hartman | b244131 | 2017-11-01 15:07:57 +0100 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0 */ |
Hugh Dickins | f8af4da | 2009-09-21 17:01:57 -0700 | [diff] [blame] | 2 | #ifndef __LINUX_KSM_H |
| 3 | #define __LINUX_KSM_H |
| 4 | /* |
| 5 | * Memory merging support. |
| 6 | * |
| 7 | * This code enables dynamic sharing of identical pages found in different |
| 8 | * memory areas, even if they are not shared by fork(). |
| 9 | */ |
| 10 | |
| 11 | #include <linux/bitops.h> |
| 12 | #include <linux/mm.h> |
Hugh Dickins | 5ad6468 | 2009-12-14 17:59:24 -0800 | [diff] [blame] | 13 | #include <linux/pagemap.h> |
| 14 | #include <linux/rmap.h> |
Hugh Dickins | f8af4da | 2009-09-21 17:01:57 -0700 | [diff] [blame] | 15 | #include <linux/sched.h> |
Ingo Molnar | f7ccbae | 2017-02-08 18:51:30 +0100 | [diff] [blame] | 16 | #include <linux/sched/coredump.h> |
Hugh Dickins | f8af4da | 2009-09-21 17:01:57 -0700 | [diff] [blame] | 17 | |
Hugh Dickins | 08beca4 | 2009-12-14 17:59:21 -0800 | [diff] [blame] | 18 | struct stable_node; |
Hugh Dickins | 5ad6468 | 2009-12-14 17:59:24 -0800 | [diff] [blame] | 19 | struct mem_cgroup; |
Hugh Dickins | 08beca4 | 2009-12-14 17:59:21 -0800 | [diff] [blame] | 20 | |
Hugh Dickins | f8af4da | 2009-09-21 17:01:57 -0700 | [diff] [blame] | 21 | #ifdef CONFIG_KSM |
| 22 | int ksm_madvise(struct vm_area_struct *vma, unsigned long start, |
| 23 | unsigned long end, int advice, unsigned long *vm_flags); |
| 24 | int __ksm_enter(struct mm_struct *mm); |
Andrea Arcangeli | 1c2fb7a | 2009-09-21 17:02:22 -0700 | [diff] [blame] | 25 | void __ksm_exit(struct mm_struct *mm); |
Hugh Dickins | f8af4da | 2009-09-21 17:01:57 -0700 | [diff] [blame] | 26 | |
| 27 | static inline int ksm_fork(struct mm_struct *mm, struct mm_struct *oldmm) |
| 28 | { |
| 29 | if (test_bit(MMF_VM_MERGEABLE, &oldmm->flags)) |
| 30 | return __ksm_enter(mm); |
| 31 | return 0; |
| 32 | } |
| 33 | |
Andrea Arcangeli | 1c2fb7a | 2009-09-21 17:02:22 -0700 | [diff] [blame] | 34 | static inline void ksm_exit(struct mm_struct *mm) |
Hugh Dickins | f8af4da | 2009-09-21 17:01:57 -0700 | [diff] [blame] | 35 | { |
| 36 | if (test_bit(MMF_VM_MERGEABLE, &mm->flags)) |
Andrea Arcangeli | 1c2fb7a | 2009-09-21 17:02:22 -0700 | [diff] [blame] | 37 | __ksm_exit(mm); |
Hugh Dickins | f8af4da | 2009-09-21 17:01:57 -0700 | [diff] [blame] | 38 | } |
Hugh Dickins | 9a84089 | 2009-09-21 17:02:01 -0700 | [diff] [blame] | 39 | |
Hugh Dickins | 5ad6468 | 2009-12-14 17:59:24 -0800 | [diff] [blame] | 40 | /* |
| 41 | * When do_swap_page() first faults in from swap what used to be a KSM page, |
| 42 | * no problem, it will be assigned to this vma's anon_vma; but thereafter, |
| 43 | * it might be faulted into a different anon_vma (or perhaps to a different |
| 44 | * offset in the same anon_vma). do_swap_page() cannot do all the locking |
| 45 | * needed to reconstitute a cross-anon_vma KSM page: for now it has to make |
| 46 | * a copy, and leave remerging the pages to a later pass of ksmd. |
| 47 | * |
| 48 | * We'd like to make this conditional on vma->vm_flags & VM_MERGEABLE, |
| 49 | * but what if the vma was unmerged while the page was swapped out? |
| 50 | */ |
Hugh Dickins | cbf86cf | 2013-02-22 16:35:08 -0800 | [diff] [blame] | 51 | struct page *ksm_might_need_to_copy(struct page *page, |
| 52 | struct vm_area_struct *vma, unsigned long address); |
Hugh Dickins | 5ad6468 | 2009-12-14 17:59:24 -0800 | [diff] [blame] | 53 | |
Minchan Kim | 1df631a | 2017-05-03 14:54:23 -0700 | [diff] [blame] | 54 | void rmap_walk_ksm(struct page *page, struct rmap_walk_control *rwc); |
Hugh Dickins | e9995ef | 2009-12-14 17:59:31 -0800 | [diff] [blame] | 55 | void ksm_migrate_page(struct page *newpage, struct page *oldpage); |
Kirill Tkhai | 52d1e60 | 2019-03-05 15:43:06 -0800 | [diff] [blame^] | 56 | bool reuse_ksm_page(struct page *page, |
| 57 | struct vm_area_struct *vma, unsigned long address); |
Hugh Dickins | 5ad6468 | 2009-12-14 17:59:24 -0800 | [diff] [blame] | 58 | |
Hugh Dickins | f8af4da | 2009-09-21 17:01:57 -0700 | [diff] [blame] | 59 | #else /* !CONFIG_KSM */ |
| 60 | |
Hugh Dickins | f8af4da | 2009-09-21 17:01:57 -0700 | [diff] [blame] | 61 | static inline int ksm_fork(struct mm_struct *mm, struct mm_struct *oldmm) |
| 62 | { |
| 63 | return 0; |
| 64 | } |
| 65 | |
Andrea Arcangeli | 1c2fb7a | 2009-09-21 17:02:22 -0700 | [diff] [blame] | 66 | static inline void ksm_exit(struct mm_struct *mm) |
Hugh Dickins | f8af4da | 2009-09-21 17:01:57 -0700 | [diff] [blame] | 67 | { |
| 68 | } |
Hugh Dickins | 9a84089 | 2009-09-21 17:02:01 -0700 | [diff] [blame] | 69 | |
Hugh Dickins | f42647a | 2009-12-16 08:56:57 +0000 | [diff] [blame] | 70 | #ifdef CONFIG_MMU |
| 71 | static inline int ksm_madvise(struct vm_area_struct *vma, unsigned long start, |
| 72 | unsigned long end, int advice, unsigned long *vm_flags) |
| 73 | { |
| 74 | return 0; |
| 75 | } |
| 76 | |
Hugh Dickins | cbf86cf | 2013-02-22 16:35:08 -0800 | [diff] [blame] | 77 | static inline struct page *ksm_might_need_to_copy(struct page *page, |
Hugh Dickins | 5ad6468 | 2009-12-14 17:59:24 -0800 | [diff] [blame] | 78 | struct vm_area_struct *vma, unsigned long address) |
| 79 | { |
Hugh Dickins | cbf86cf | 2013-02-22 16:35:08 -0800 | [diff] [blame] | 80 | return page; |
Hugh Dickins | 5ad6468 | 2009-12-14 17:59:24 -0800 | [diff] [blame] | 81 | } |
| 82 | |
Minchan Kim | 1df631a | 2017-05-03 14:54:23 -0700 | [diff] [blame] | 83 | static inline void rmap_walk_ksm(struct page *page, |
Joonsoo Kim | 051ac83 | 2014-01-21 15:49:48 -0800 | [diff] [blame] | 84 | struct rmap_walk_control *rwc) |
Hugh Dickins | e9995ef | 2009-12-14 17:59:31 -0800 | [diff] [blame] | 85 | { |
Hugh Dickins | e9995ef | 2009-12-14 17:59:31 -0800 | [diff] [blame] | 86 | } |
| 87 | |
| 88 | static inline void ksm_migrate_page(struct page *newpage, struct page *oldpage) |
| 89 | { |
| 90 | } |
Kirill Tkhai | 52d1e60 | 2019-03-05 15:43:06 -0800 | [diff] [blame^] | 91 | static inline bool reuse_ksm_page(struct page *page, |
| 92 | struct vm_area_struct *vma, unsigned long address) |
| 93 | { |
| 94 | return false; |
| 95 | } |
Hugh Dickins | f42647a | 2009-12-16 08:56:57 +0000 | [diff] [blame] | 96 | #endif /* CONFIG_MMU */ |
Hugh Dickins | f8af4da | 2009-09-21 17:01:57 -0700 | [diff] [blame] | 97 | #endif /* !CONFIG_KSM */ |
| 98 | |
Hugh Dickins | 5ad6468 | 2009-12-14 17:59:24 -0800 | [diff] [blame] | 99 | #endif /* __LINUX_KSM_H */ |