blob: 78b44a024eaae8e9a9a0e2c448b4b3e2b7f48719 [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>
Hugh Dickins5ad64682009-12-14 17:59:24 -080012#include <linux/pagemap.h>
13#include <linux/rmap.h>
Hugh Dickinsf8af4da2009-09-21 17:01:57 -070014#include <linux/sched.h>
Ingo Molnarf7ccbae2017-02-08 18:51:30 +010015#include <linux/sched/coredump.h>
Hugh Dickinsf8af4da2009-09-21 17:01:57 -070016
Hugh Dickins08beca42009-12-14 17:59:21 -080017struct stable_node;
Hugh Dickins5ad64682009-12-14 17:59:24 -080018struct mem_cgroup;
Hugh Dickins08beca42009-12-14 17:59:21 -080019
Hugh Dickinsf8af4da2009-09-21 17:01:57 -070020#ifdef CONFIG_KSM
21int ksm_madvise(struct vm_area_struct *vma, unsigned long start,
22 unsigned long end, int advice, unsigned long *vm_flags);
23int __ksm_enter(struct mm_struct *mm);
Andrea Arcangeli1c2fb7a2009-09-21 17:02:22 -070024void __ksm_exit(struct mm_struct *mm);
Hugh Dickinsf8af4da2009-09-21 17:01:57 -070025
26static inline int ksm_fork(struct mm_struct *mm, struct mm_struct *oldmm)
27{
28 if (test_bit(MMF_VM_MERGEABLE, &oldmm->flags))
29 return __ksm_enter(mm);
30 return 0;
31}
32
Andrea Arcangeli1c2fb7a2009-09-21 17:02:22 -070033static inline void ksm_exit(struct mm_struct *mm)
Hugh Dickinsf8af4da2009-09-21 17:01:57 -070034{
35 if (test_bit(MMF_VM_MERGEABLE, &mm->flags))
Andrea Arcangeli1c2fb7a2009-09-21 17:02:22 -070036 __ksm_exit(mm);
Hugh Dickinsf8af4da2009-09-21 17:01:57 -070037}
Hugh Dickins9a840892009-09-21 17:02:01 -070038
Hugh Dickins08beca42009-12-14 17:59:21 -080039static inline struct stable_node *page_stable_node(struct page *page)
40{
41 return PageKsm(page) ? page_rmapping(page) : NULL;
42}
43
44static inline void set_page_stable_node(struct page *page,
45 struct stable_node *stable_node)
46{
Minchan Kimbda807d2016-07-26 15:23:05 -070047 page->mapping = (void *)((unsigned long)stable_node | PAGE_MAPPING_KSM);
Hugh Dickins08beca42009-12-14 17:59:21 -080048}
49
Hugh Dickins5ad64682009-12-14 17:59:24 -080050/*
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 */
Hugh Dickinscbf86cf2013-02-22 16:35:08 -080061struct page *ksm_might_need_to_copy(struct page *page,
62 struct vm_area_struct *vma, unsigned long address);
Hugh Dickins5ad64682009-12-14 17:59:24 -080063
Minchan Kim1df631a2017-05-03 14:54:23 -070064void rmap_walk_ksm(struct page *page, struct rmap_walk_control *rwc);
Hugh Dickinse9995ef2009-12-14 17:59:31 -080065void ksm_migrate_page(struct page *newpage, struct page *oldpage);
Hugh Dickins5ad64682009-12-14 17:59:24 -080066
Hugh Dickinsf8af4da2009-09-21 17:01:57 -070067#else /* !CONFIG_KSM */
68
Hugh Dickinsf8af4da2009-09-21 17:01:57 -070069static inline int ksm_fork(struct mm_struct *mm, struct mm_struct *oldmm)
70{
71 return 0;
72}
73
Andrea Arcangeli1c2fb7a2009-09-21 17:02:22 -070074static inline void ksm_exit(struct mm_struct *mm)
Hugh Dickinsf8af4da2009-09-21 17:01:57 -070075{
76}
Hugh Dickins9a840892009-09-21 17:02:01 -070077
Hugh Dickinsf42647a2009-12-16 08:56:57 +000078#ifdef CONFIG_MMU
79static inline int ksm_madvise(struct vm_area_struct *vma, unsigned long start,
80 unsigned long end, int advice, unsigned long *vm_flags)
81{
82 return 0;
83}
84
Hugh Dickinscbf86cf2013-02-22 16:35:08 -080085static inline struct page *ksm_might_need_to_copy(struct page *page,
Hugh Dickins5ad64682009-12-14 17:59:24 -080086 struct vm_area_struct *vma, unsigned long address)
87{
Hugh Dickinscbf86cf2013-02-22 16:35:08 -080088 return page;
Hugh Dickins5ad64682009-12-14 17:59:24 -080089}
90
91static inline int page_referenced_ksm(struct page *page,
92 struct mem_cgroup *memcg, unsigned long *vm_flags)
93{
94 return 0;
95}
96
Minchan Kim1df631a2017-05-03 14:54:23 -070097static inline void rmap_walk_ksm(struct page *page,
Joonsoo Kim051ac832014-01-21 15:49:48 -080098 struct rmap_walk_control *rwc)
Hugh Dickinse9995ef2009-12-14 17:59:31 -080099{
Hugh Dickinse9995ef2009-12-14 17:59:31 -0800100}
101
102static inline void ksm_migrate_page(struct page *newpage, struct page *oldpage)
103{
104}
Hugh Dickinsf42647a2009-12-16 08:56:57 +0000105#endif /* CONFIG_MMU */
Hugh Dickinsf8af4da2009-09-21 17:01:57 -0700106#endif /* !CONFIG_KSM */
107
Hugh Dickins5ad64682009-12-14 17:59:24 -0800108#endif /* __LINUX_KSM_H */