Merge tag 'iommu-updates-v6.4' of git://git.kernel.org/pub/scm/linux/kernel/git/joro...
[linux-2.6-block.git] / include / linux / ksm.h
CommitLineData
b2441318 1/* SPDX-License-Identifier: GPL-2.0 */
f8af4da3
HD
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>
5ad64688
HD
13#include <linux/pagemap.h>
14#include <linux/rmap.h>
f8af4da3 15#include <linux/sched.h>
f7ccbae4 16#include <linux/sched/coredump.h>
f8af4da3
HD
17
18#ifdef CONFIG_KSM
19int ksm_madvise(struct vm_area_struct *vma, unsigned long start,
20 unsigned long end, int advice, unsigned long *vm_flags);
d7597f59
SR
21
22void ksm_add_vma(struct vm_area_struct *vma);
23int ksm_enable_merge_any(struct mm_struct *mm);
24
f8af4da3 25int __ksm_enter(struct mm_struct *mm);
1c2fb7a4 26void __ksm_exit(struct mm_struct *mm);
f8af4da3
HD
27
28static inline int ksm_fork(struct mm_struct *mm, struct mm_struct *oldmm)
29{
d7597f59
SR
30 int ret;
31
32 if (test_bit(MMF_VM_MERGEABLE, &oldmm->flags)) {
33 ret = __ksm_enter(mm);
34 if (ret)
35 return ret;
36 }
37
38 if (test_bit(MMF_VM_MERGE_ANY, &oldmm->flags))
39 set_bit(MMF_VM_MERGE_ANY, &mm->flags);
40
f8af4da3
HD
41 return 0;
42}
43
1c2fb7a4 44static inline void ksm_exit(struct mm_struct *mm)
f8af4da3
HD
45{
46 if (test_bit(MMF_VM_MERGEABLE, &mm->flags))
1c2fb7a4 47 __ksm_exit(mm);
f8af4da3 48}
9a840895 49
5ad64688
HD
50/*
51 * When do_swap_page() first faults in from swap what used to be a KSM page,
52 * no problem, it will be assigned to this vma's anon_vma; but thereafter,
53 * it might be faulted into a different anon_vma (or perhaps to a different
54 * offset in the same anon_vma). do_swap_page() cannot do all the locking
55 * needed to reconstitute a cross-anon_vma KSM page: for now it has to make
56 * a copy, and leave remerging the pages to a later pass of ksmd.
57 *
58 * We'd like to make this conditional on vma->vm_flags & VM_MERGEABLE,
59 * but what if the vma was unmerged while the page was swapped out?
60 */
cbf86cfe
HD
61struct page *ksm_might_need_to_copy(struct page *page,
62 struct vm_area_struct *vma, unsigned long address);
5ad64688 63
6d4675e6 64void rmap_walk_ksm(struct folio *folio, struct rmap_walk_control *rwc);
19138349 65void folio_migrate_ksm(struct folio *newfolio, struct folio *folio);
5ad64688 66
4248d008
LX
67#ifdef CONFIG_MEMORY_FAILURE
68void collect_procs_ksm(struct page *page, struct list_head *to_kill,
69 int force_early);
70#endif
d21077fb
SR
71
72#ifdef CONFIG_PROC_FS
73long ksm_process_profit(struct mm_struct *);
74#endif /* CONFIG_PROC_FS */
75
f8af4da3
HD
76#else /* !CONFIG_KSM */
77
d7597f59
SR
78static inline void ksm_add_vma(struct vm_area_struct *vma)
79{
80}
81
f8af4da3
HD
82static inline int ksm_fork(struct mm_struct *mm, struct mm_struct *oldmm)
83{
84 return 0;
85}
86
1c2fb7a4 87static inline void ksm_exit(struct mm_struct *mm)
f8af4da3
HD
88{
89}
9a840895 90
4248d008
LX
91#ifdef CONFIG_MEMORY_FAILURE
92static inline void collect_procs_ksm(struct page *page,
93 struct list_head *to_kill, int force_early)
94{
95}
96#endif
97
f42647ac
HD
98#ifdef CONFIG_MMU
99static inline int ksm_madvise(struct vm_area_struct *vma, unsigned long start,
100 unsigned long end, int advice, unsigned long *vm_flags)
101{
102 return 0;
103}
104
cbf86cfe 105static inline struct page *ksm_might_need_to_copy(struct page *page,
5ad64688
HD
106 struct vm_area_struct *vma, unsigned long address)
107{
cbf86cfe 108 return page;
5ad64688
HD
109}
110
2f031c6f 111static inline void rmap_walk_ksm(struct folio *folio,
6d4675e6 112 struct rmap_walk_control *rwc)
e9995ef9 113{
e9995ef9
HD
114}
115
19138349 116static inline void folio_migrate_ksm(struct folio *newfolio, struct folio *old)
e9995ef9
HD
117{
118}
f42647ac 119#endif /* CONFIG_MMU */
f8af4da3
HD
120#endif /* !CONFIG_KSM */
121
5ad64688 122#endif /* __LINUX_KSM_H */