1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef _LINUX_MIGRATE_H
3 #define _LINUX_MIGRATE_H
4 
5 #include <linux/mm.h>
6 #include <linux/mempolicy.h>
7 #include <linux/migrate_mode.h>
8 #include <linux/hugetlb.h>
9 
10 typedef struct page *new_page_t(struct page *page, unsigned long private);
11 typedef void free_page_t(struct page *page, unsigned long private);
12 
13 struct migration_target_control;
14 
15 /*
16  * Return values from addresss_space_operations.migratepage():
17  * - negative errno on page migration failure;
18  * - zero on page migration success;
19  */
20 #define MIGRATEPAGE_SUCCESS		0
21 
22 /*
23  * Keep sync with:
24  * - macro MIGRATE_REASON in include/trace/events/migrate.h
25  * - migrate_reason_names[MR_TYPES] in mm/debug.c
26  */
27 enum migrate_reason {
28 	MR_COMPACTION,
29 	MR_MEMORY_FAILURE,
30 	MR_MEMORY_HOTPLUG,
31 	MR_SYSCALL,		/* also applies to cpusets */
32 	MR_MEMPOLICY_MBIND,
33 	MR_NUMA_MISPLACED,
34 	MR_CONTIG_RANGE,
35 	MR_LONGTERM_PIN,
36 	MR_DEMOTION,
37 	MR_TYPES
38 };
39 
40 extern const char *migrate_reason_names[MR_TYPES];
41 
42 #ifdef CONFIG_MIGRATION
43 
44 extern void putback_movable_pages(struct list_head *l);
45 extern int migrate_page(struct address_space *mapping,
46 			struct page *newpage, struct page *page,
47 			enum migrate_mode mode);
48 extern int migrate_pages(struct list_head *l, new_page_t new, free_page_t free,
49 		unsigned long private, enum migrate_mode mode, int reason,
50 		unsigned int *ret_succeeded);
51 extern struct page *alloc_migration_target(struct page *page, unsigned long private);
52 extern int isolate_movable_page(struct page *page, isolate_mode_t mode);
53 
54 extern void migrate_page_states(struct page *newpage, struct page *page);
55 extern void migrate_page_copy(struct page *newpage, struct page *page);
56 extern int migrate_huge_page_move_mapping(struct address_space *mapping,
57 				  struct page *newpage, struct page *page);
58 extern int migrate_page_move_mapping(struct address_space *mapping,
59 		struct page *newpage, struct page *page, int extra_count);
60 #else
61 
putback_movable_pages(struct list_head * l)62 static inline void putback_movable_pages(struct list_head *l) {}
migrate_pages(struct list_head * l,new_page_t new,free_page_t free,unsigned long private,enum migrate_mode mode,int reason,unsigned int * ret_succeeded)63 static inline int migrate_pages(struct list_head *l, new_page_t new,
64 		free_page_t free, unsigned long private, enum migrate_mode mode,
65 		int reason, unsigned int *ret_succeeded)
66 	{ return -ENOSYS; }
alloc_migration_target(struct page * page,unsigned long private)67 static inline struct page *alloc_migration_target(struct page *page,
68 		unsigned long private)
69 	{ return NULL; }
isolate_movable_page(struct page * page,isolate_mode_t mode)70 static inline int isolate_movable_page(struct page *page, isolate_mode_t mode)
71 	{ return -EBUSY; }
72 
migrate_page_states(struct page * newpage,struct page * page)73 static inline void migrate_page_states(struct page *newpage, struct page *page)
74 {
75 }
76 
migrate_page_copy(struct page * newpage,struct page * page)77 static inline void migrate_page_copy(struct page *newpage,
78 				     struct page *page) {}
79 
migrate_huge_page_move_mapping(struct address_space * mapping,struct page * newpage,struct page * page)80 static inline int migrate_huge_page_move_mapping(struct address_space *mapping,
81 				  struct page *newpage, struct page *page)
82 {
83 	return -ENOSYS;
84 }
85 #endif /* CONFIG_MIGRATION */
86 
87 #ifdef CONFIG_COMPACTION
88 extern int PageMovable(struct page *page);
89 extern void __SetPageMovable(struct page *page, struct address_space *mapping);
90 extern void __ClearPageMovable(struct page *page);
91 #else
PageMovable(struct page * page)92 static inline int PageMovable(struct page *page) { return 0; }
__SetPageMovable(struct page * page,struct address_space * mapping)93 static inline void __SetPageMovable(struct page *page,
94 				struct address_space *mapping)
95 {
96 }
__ClearPageMovable(struct page * page)97 static inline void __ClearPageMovable(struct page *page)
98 {
99 }
100 #endif
101 
102 #ifdef CONFIG_NUMA_BALANCING
103 extern int migrate_misplaced_page(struct page *page,
104 				  struct vm_area_struct *vma, int node);
105 #else
migrate_misplaced_page(struct page * page,struct vm_area_struct * vma,int node)106 static inline int migrate_misplaced_page(struct page *page,
107 					 struct vm_area_struct *vma, int node)
108 {
109 	return -EAGAIN; /* can't migrate now */
110 }
111 #endif /* CONFIG_NUMA_BALANCING */
112 
113 #ifdef CONFIG_MIGRATION
114 
115 /*
116  * Watch out for PAE architecture, which has an unsigned long, and might not
117  * have enough bits to store all physical address and flags. So far we have
118  * enough room for all our flags.
119  */
120 #define MIGRATE_PFN_VALID	(1UL << 0)
121 #define MIGRATE_PFN_MIGRATE	(1UL << 1)
122 #define MIGRATE_PFN_LOCKED	(1UL << 2)
123 #define MIGRATE_PFN_WRITE	(1UL << 3)
124 #define MIGRATE_PFN_SHIFT	6
125 
migrate_pfn_to_page(unsigned long mpfn)126 static inline struct page *migrate_pfn_to_page(unsigned long mpfn)
127 {
128 	if (!(mpfn & MIGRATE_PFN_VALID))
129 		return NULL;
130 	return pfn_to_page(mpfn >> MIGRATE_PFN_SHIFT);
131 }
132 
migrate_pfn(unsigned long pfn)133 static inline unsigned long migrate_pfn(unsigned long pfn)
134 {
135 	return (pfn << MIGRATE_PFN_SHIFT) | MIGRATE_PFN_VALID;
136 }
137 
138 enum migrate_vma_direction {
139 	MIGRATE_VMA_SELECT_SYSTEM = 1 << 0,
140 	MIGRATE_VMA_SELECT_DEVICE_PRIVATE = 1 << 1,
141 };
142 
143 struct migrate_vma {
144 	struct vm_area_struct	*vma;
145 	/*
146 	 * Both src and dst array must be big enough for
147 	 * (end - start) >> PAGE_SHIFT entries.
148 	 *
149 	 * The src array must not be modified by the caller after
150 	 * migrate_vma_setup(), and must not change the dst array after
151 	 * migrate_vma_pages() returns.
152 	 */
153 	unsigned long		*dst;
154 	unsigned long		*src;
155 	unsigned long		cpages;
156 	unsigned long		npages;
157 	unsigned long		start;
158 	unsigned long		end;
159 
160 	/*
161 	 * Set to the owner value also stored in page->pgmap->owner for
162 	 * migrating out of device private memory. The flags also need to
163 	 * be set to MIGRATE_VMA_SELECT_DEVICE_PRIVATE.
164 	 * The caller should always set this field when using mmu notifier
165 	 * callbacks to avoid device MMU invalidations for device private
166 	 * pages that are not being migrated.
167 	 */
168 	void			*pgmap_owner;
169 	unsigned long		flags;
170 };
171 
172 int migrate_vma_setup(struct migrate_vma *args);
173 void migrate_vma_pages(struct migrate_vma *migrate);
174 void migrate_vma_finalize(struct migrate_vma *migrate);
175 int next_demotion_node(int node);
176 
177 #else /* CONFIG_MIGRATION disabled: */
178 
next_demotion_node(int node)179 static inline int next_demotion_node(int node)
180 {
181 	return NUMA_NO_NODE;
182 }
183 
184 #endif /* CONFIG_MIGRATION */
185 
186 #endif /* _LINUX_MIGRATE_H */
187