xref: /linux-6.15/include/linux/migrate.h (revision 8a1e6bb3)
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 /* Defined in mm/debug.c: */
23 extern const char *migrate_reason_names[MR_TYPES];
24 
25 #ifdef CONFIG_MIGRATION
26 
27 extern void putback_movable_pages(struct list_head *l);
28 extern int migrate_page(struct address_space *mapping,
29 			struct page *newpage, struct page *page,
30 			enum migrate_mode mode);
31 extern int migrate_pages(struct list_head *l, new_page_t new, free_page_t free,
32 		unsigned long private, enum migrate_mode mode, int reason,
33 		unsigned int *ret_succeeded);
34 extern struct page *alloc_migration_target(struct page *page, unsigned long private);
35 extern int isolate_movable_page(struct page *page, isolate_mode_t mode);
36 
37 extern void migrate_page_states(struct page *newpage, struct page *page);
38 extern void migrate_page_copy(struct page *newpage, struct page *page);
39 extern int migrate_huge_page_move_mapping(struct address_space *mapping,
40 				  struct page *newpage, struct page *page);
41 extern int migrate_page_move_mapping(struct address_space *mapping,
42 		struct page *newpage, struct page *page, int extra_count);
43 void migration_entry_wait_on_locked(swp_entry_t entry, pte_t *ptep,
44 				spinlock_t *ptl);
45 void folio_migrate_flags(struct folio *newfolio, struct folio *folio);
46 void folio_migrate_copy(struct folio *newfolio, struct folio *folio);
47 int folio_migrate_mapping(struct address_space *mapping,
48 		struct folio *newfolio, struct folio *folio, int extra_count);
49 
50 extern bool numa_demotion_enabled;
51 extern void migrate_on_reclaim_init(void);
52 #ifdef CONFIG_HOTPLUG_CPU
53 extern void set_migration_target_nodes(void);
54 #else
55 static inline void set_migration_target_nodes(void) {}
56 #endif
57 #else
58 
59 static inline void set_migration_target_nodes(void) {}
60 
61 static inline void putback_movable_pages(struct list_head *l) {}
62 static inline int migrate_pages(struct list_head *l, new_page_t new,
63 		free_page_t free, unsigned long private, enum migrate_mode mode,
64 		int reason, unsigned int *ret_succeeded)
65 	{ return -ENOSYS; }
66 static inline struct page *alloc_migration_target(struct page *page,
67 		unsigned long private)
68 	{ return NULL; }
69 static inline int isolate_movable_page(struct page *page, isolate_mode_t mode)
70 	{ return -EBUSY; }
71 
72 static inline void migrate_page_states(struct page *newpage, struct page *page)
73 {
74 }
75 
76 static inline void migrate_page_copy(struct page *newpage,
77 				     struct page *page) {}
78 
79 static inline int migrate_huge_page_move_mapping(struct address_space *mapping,
80 				  struct page *newpage, struct page *page)
81 {
82 	return -ENOSYS;
83 }
84 
85 #define numa_demotion_enabled	false
86 #endif /* CONFIG_MIGRATION */
87 
88 #ifdef CONFIG_COMPACTION
89 extern int PageMovable(struct page *page);
90 extern void __SetPageMovable(struct page *page, struct address_space *mapping);
91 extern void __ClearPageMovable(struct page *page);
92 #else
93 static inline int PageMovable(struct page *page) { return 0; }
94 static inline void __SetPageMovable(struct page *page,
95 				struct address_space *mapping)
96 {
97 }
98 static inline void __ClearPageMovable(struct page *page)
99 {
100 }
101 #endif
102 
103 #ifdef CONFIG_NUMA_BALANCING
104 extern int migrate_misplaced_page(struct page *page,
105 				  struct vm_area_struct *vma, int node);
106 #else
107 static inline int migrate_misplaced_page(struct page *page,
108 					 struct vm_area_struct *vma, int node)
109 {
110 	return -EAGAIN; /* can't migrate now */
111 }
112 #endif /* CONFIG_NUMA_BALANCING */
113 
114 #ifdef CONFIG_MIGRATION
115 
116 /*
117  * Watch out for PAE architecture, which has an unsigned long, and might not
118  * have enough bits to store all physical address and flags. So far we have
119  * enough room for all our flags.
120  */
121 #define MIGRATE_PFN_VALID	(1UL << 0)
122 #define MIGRATE_PFN_MIGRATE	(1UL << 1)
123 #define MIGRATE_PFN_WRITE	(1UL << 3)
124 #define MIGRATE_PFN_SHIFT	6
125 
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 
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 
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