xref: /linux/include/linux/ksm.h (revision 8804d970fab45726b3c7cd7f240b31122aa94219)
1b2441318SGreg Kroah-Hartman /* SPDX-License-Identifier: GPL-2.0 */
2f8af4da3SHugh Dickins #ifndef __LINUX_KSM_H
3f8af4da3SHugh Dickins #define __LINUX_KSM_H
4f8af4da3SHugh Dickins /*
5f8af4da3SHugh Dickins  * Memory merging support.
6f8af4da3SHugh Dickins  *
7f8af4da3SHugh Dickins  * This code enables dynamic sharing of identical pages found in different
8f8af4da3SHugh Dickins  * memory areas, even if they are not shared by fork().
9f8af4da3SHugh Dickins  */
10f8af4da3SHugh Dickins 
11f8af4da3SHugh Dickins #include <linux/bitops.h>
12f8af4da3SHugh Dickins #include <linux/mm.h>
135ad64688SHugh Dickins #include <linux/pagemap.h>
145ad64688SHugh Dickins #include <linux/rmap.h>
15f8af4da3SHugh Dickins #include <linux/sched.h>
16f8af4da3SHugh Dickins 
17f8af4da3SHugh Dickins #ifdef CONFIG_KSM
18f8af4da3SHugh Dickins int ksm_madvise(struct vm_area_struct *vma, unsigned long start,
19bfbe7110SLorenzo Stoakes 		unsigned long end, int advice, vm_flags_t *vm_flags);
20cf7e7a35SLorenzo Stoakes vm_flags_t ksm_vma_flags(const struct mm_struct *mm, const struct file *file,
21cf7e7a35SLorenzo Stoakes 			 vm_flags_t vm_flags);
22d7597f59SStefan Roesch int ksm_enable_merge_any(struct mm_struct *mm);
2324139c07SDavid Hildenbrand int ksm_disable_merge_any(struct mm_struct *mm);
242c281f54SDavid Hildenbrand int ksm_disable(struct mm_struct *mm);
25d7597f59SStefan Roesch 
26f8af4da3SHugh Dickins int __ksm_enter(struct mm_struct *mm);
271c2fb7a4SAndrea Arcangeli void __ksm_exit(struct mm_struct *mm);
2879271476Sxu xin /*
2979271476Sxu xin  * To identify zeropages that were mapped by KSM, we reuse the dirty bit
3079271476Sxu xin  * in the PTE. If the PTE is dirty, the zeropage was mapped by KSM when
3179271476Sxu xin  * deduplicating memory.
3279271476Sxu xin  */
3379271476Sxu xin #define is_ksm_zero_pte(pte)	(is_zero_pfn(pte_pfn(pte)) && pte_dirty(pte))
34f8af4da3SHugh Dickins 
35c2dc78b8SChengming Zhou extern atomic_long_t ksm_zero_pages;
36c2dc78b8SChengming Zhou 
ksm_map_zero_page(struct mm_struct * mm)37c2dc78b8SChengming Zhou static inline void ksm_map_zero_page(struct mm_struct *mm)
38c2dc78b8SChengming Zhou {
39c2dc78b8SChengming Zhou 	atomic_long_inc(&ksm_zero_pages);
40c2dc78b8SChengming Zhou 	atomic_long_inc(&mm->ksm_zero_pages);
41c2dc78b8SChengming Zhou }
42e2942062Sxu xin 
ksm_might_unmap_zero_page(struct mm_struct * mm,pte_t pte)436080d19fSxu xin static inline void ksm_might_unmap_zero_page(struct mm_struct *mm, pte_t pte)
44e2942062Sxu xin {
456080d19fSxu xin 	if (is_ksm_zero_pte(pte)) {
46c2dc78b8SChengming Zhou 		atomic_long_dec(&ksm_zero_pages);
47c2dc78b8SChengming Zhou 		atomic_long_dec(&mm->ksm_zero_pages);
486080d19fSxu xin 	}
49e2942062Sxu xin }
50e2942062Sxu xin 
mm_ksm_zero_pages(struct mm_struct * mm)51c2dc78b8SChengming Zhou static inline long mm_ksm_zero_pages(struct mm_struct *mm)
52c2dc78b8SChengming Zhou {
53c2dc78b8SChengming Zhou 	return atomic_long_read(&mm->ksm_zero_pages);
54c2dc78b8SChengming Zhou }
55c2dc78b8SChengming Zhou 
ksm_fork(struct mm_struct * mm,struct mm_struct * oldmm)56985da552SLorenzo Stoakes static inline void ksm_fork(struct mm_struct *mm, struct mm_struct *oldmm)
57f8af4da3SHugh Dickins {
58985da552SLorenzo Stoakes 	/* Adding mm to ksm is best effort on fork. */
59*4d6fc29fSDonet Tom 	if (mm_flags_test(MMF_VM_MERGEABLE, oldmm)) {
60*4d6fc29fSDonet Tom 		long nr_ksm_zero_pages = atomic_long_read(&mm->ksm_zero_pages);
61*4d6fc29fSDonet Tom 
62*4d6fc29fSDonet Tom 		mm->ksm_merging_pages = 0;
63*4d6fc29fSDonet Tom 		mm->ksm_rmap_items = 0;
64*4d6fc29fSDonet Tom 		atomic_long_add(nr_ksm_zero_pages, &ksm_zero_pages);
65985da552SLorenzo Stoakes 		__ksm_enter(mm);
66f8af4da3SHugh Dickins 	}
67*4d6fc29fSDonet Tom }
68f8af4da3SHugh Dickins 
ksm_execve(struct mm_struct * mm)693a9e567cSJinjiang Tu static inline int ksm_execve(struct mm_struct *mm)
703a9e567cSJinjiang Tu {
7112e423baSLorenzo Stoakes 	if (mm_flags_test(MMF_VM_MERGE_ANY, mm))
723a9e567cSJinjiang Tu 		return __ksm_enter(mm);
733a9e567cSJinjiang Tu 
743a9e567cSJinjiang Tu 	return 0;
753a9e567cSJinjiang Tu }
763a9e567cSJinjiang Tu 
ksm_exit(struct mm_struct * mm)771c2fb7a4SAndrea Arcangeli static inline void ksm_exit(struct mm_struct *mm)
78f8af4da3SHugh Dickins {
7912e423baSLorenzo Stoakes 	if (mm_flags_test(MMF_VM_MERGEABLE, mm))
801c2fb7a4SAndrea Arcangeli 		__ksm_exit(mm);
81f8af4da3SHugh Dickins }
829a840895SHugh Dickins 
835ad64688SHugh Dickins /*
845ad64688SHugh Dickins  * When do_swap_page() first faults in from swap what used to be a KSM page,
855ad64688SHugh Dickins  * no problem, it will be assigned to this vma's anon_vma; but thereafter,
865ad64688SHugh Dickins  * it might be faulted into a different anon_vma (or perhaps to a different
875ad64688SHugh Dickins  * offset in the same anon_vma).  do_swap_page() cannot do all the locking
885ad64688SHugh Dickins  * needed to reconstitute a cross-anon_vma KSM page: for now it has to make
895ad64688SHugh Dickins  * a copy, and leave remerging the pages to a later pass of ksmd.
905ad64688SHugh Dickins  *
915ad64688SHugh Dickins  * We'd like to make this conditional on vma->vm_flags & VM_MERGEABLE,
925ad64688SHugh Dickins  * but what if the vma was unmerged while the page was swapped out?
935ad64688SHugh Dickins  */
9496db66d9SMatthew Wilcox (Oracle) struct folio *ksm_might_need_to_copy(struct folio *folio,
951486fb50SKefeng Wang 			struct vm_area_struct *vma, unsigned long addr);
965ad64688SHugh Dickins 
976d4675e6SMinchan Kim void rmap_walk_ksm(struct folio *folio, struct rmap_walk_control *rwc);
9819138349SMatthew Wilcox (Oracle) void folio_migrate_ksm(struct folio *newfolio, struct folio *folio);
9968158bfaSMatthew Wilcox (Oracle) void collect_procs_ksm(const struct folio *folio, const struct page *page,
100b650e1d2SMatthew Wilcox (Oracle) 		struct list_head *to_kill, int force_early);
101d21077fbSStefan Roesch long ksm_process_profit(struct mm_struct *);
1023ab76c76Sxu xin bool ksm_process_mergeable(struct mm_struct *mm);
103d21077fbSStefan Roesch 
104f8af4da3SHugh Dickins #else  /* !CONFIG_KSM */
105f8af4da3SHugh Dickins 
ksm_vma_flags(const struct mm_struct * mm,const struct file * file,vm_flags_t vm_flags)106cf7e7a35SLorenzo Stoakes static inline vm_flags_t ksm_vma_flags(const struct mm_struct *mm,
107cf7e7a35SLorenzo Stoakes 		const struct file *file, vm_flags_t vm_flags)
108d7597f59SStefan Roesch {
109cf7e7a35SLorenzo Stoakes 	return vm_flags;
110d7597f59SStefan Roesch }
111d7597f59SStefan Roesch 
ksm_disable(struct mm_struct * mm)1122c281f54SDavid Hildenbrand static inline int ksm_disable(struct mm_struct *mm)
1132c281f54SDavid Hildenbrand {
1142c281f54SDavid Hildenbrand 	return 0;
1152c281f54SDavid Hildenbrand }
1162c281f54SDavid Hildenbrand 
ksm_fork(struct mm_struct * mm,struct mm_struct * oldmm)117985da552SLorenzo Stoakes static inline void ksm_fork(struct mm_struct *mm, struct mm_struct *oldmm)
118f8af4da3SHugh Dickins {
119f8af4da3SHugh Dickins }
120f8af4da3SHugh Dickins 
ksm_execve(struct mm_struct * mm)1213a9e567cSJinjiang Tu static inline int ksm_execve(struct mm_struct *mm)
1223a9e567cSJinjiang Tu {
1233a9e567cSJinjiang Tu 	return 0;
1243a9e567cSJinjiang Tu }
1253a9e567cSJinjiang Tu 
ksm_exit(struct mm_struct * mm)1261c2fb7a4SAndrea Arcangeli static inline void ksm_exit(struct mm_struct *mm)
127f8af4da3SHugh Dickins {
128f8af4da3SHugh Dickins }
1299a840895SHugh Dickins 
ksm_might_unmap_zero_page(struct mm_struct * mm,pte_t pte)1306080d19fSxu xin static inline void ksm_might_unmap_zero_page(struct mm_struct *mm, pte_t pte)
131e2942062Sxu xin {
132e2942062Sxu xin }
133e2942062Sxu xin 
collect_procs_ksm(const struct folio * folio,const struct page * page,struct list_head * to_kill,int force_early)13468158bfaSMatthew Wilcox (Oracle) static inline void collect_procs_ksm(const struct folio *folio,
13568158bfaSMatthew Wilcox (Oracle) 		const struct page *page, struct list_head *to_kill,
13668158bfaSMatthew Wilcox (Oracle) 		int force_early)
1374248d008SLonglong Xia {
1384248d008SLonglong Xia }
1394248d008SLonglong Xia 
140f42647acSHugh Dickins #ifdef CONFIG_MMU
ksm_madvise(struct vm_area_struct * vma,unsigned long start,unsigned long end,int advice,vm_flags_t * vm_flags)141f42647acSHugh Dickins static inline int ksm_madvise(struct vm_area_struct *vma, unsigned long start,
142bfbe7110SLorenzo Stoakes 		unsigned long end, int advice, vm_flags_t *vm_flags)
143f42647acSHugh Dickins {
144f42647acSHugh Dickins 	return 0;
145f42647acSHugh Dickins }
146f42647acSHugh Dickins 
ksm_might_need_to_copy(struct folio * folio,struct vm_area_struct * vma,unsigned long addr)14796db66d9SMatthew Wilcox (Oracle) static inline struct folio *ksm_might_need_to_copy(struct folio *folio,
1481486fb50SKefeng Wang 			struct vm_area_struct *vma, unsigned long addr)
1495ad64688SHugh Dickins {
15096db66d9SMatthew Wilcox (Oracle) 	return folio;
1515ad64688SHugh Dickins }
1525ad64688SHugh Dickins 
rmap_walk_ksm(struct folio * folio,struct rmap_walk_control * rwc)1532f031c6fSMatthew Wilcox (Oracle) static inline void rmap_walk_ksm(struct folio *folio,
1546d4675e6SMinchan Kim 			struct rmap_walk_control *rwc)
155e9995ef9SHugh Dickins {
156e9995ef9SHugh Dickins }
157e9995ef9SHugh Dickins 
folio_migrate_ksm(struct folio * newfolio,struct folio * old)15819138349SMatthew Wilcox (Oracle) static inline void folio_migrate_ksm(struct folio *newfolio, struct folio *old)
159e9995ef9SHugh Dickins {
160e9995ef9SHugh Dickins }
161f42647acSHugh Dickins #endif /* CONFIG_MMU */
162f8af4da3SHugh Dickins #endif /* !CONFIG_KSM */
163f8af4da3SHugh Dickins 
1645ad64688SHugh Dickins #endif /* __LINUX_KSM_H */
165