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
10typedef struct folio *new_folio_t(struct folio *folio, unsigned long private);
11typedef void free_folio_t(struct folio *folio, unsigned long private);
12
13struct migration_target_control;
14
15/**
16 * struct movable_operations - Driver page migration
17 * @isolate_page:
18 * The VM calls this function to prepare the page to be moved. The page
19 * is locked and the driver should not unlock it. The driver should
20 * return ``true`` if the page is movable and ``false`` if it is not
21 * currently movable. After this function returns, the VM uses the
22 * page->lru field, so the driver must preserve any information which
23 * is usually stored here.
24 *
25 * @migrate_page:
26 * After isolation, the VM calls this function with the isolated
27 * @src page. The driver should copy the contents of the
28 * @src page to the @dst page and set up the fields of @dst page.
29 * Both pages are locked.
30 * If page migration is successful, the driver should return 0.
31 * If the driver cannot migrate the page at the moment, it can return
32 * -EAGAIN. The VM interprets this as a temporary migration failure and
33 * will retry it later. Any other error value is a permanent migration
34 * failure and migration will not be retried.
35 * The driver shouldn't touch the @src->lru field while in the
36 * migrate_page() function. It may write to @dst->lru.
37 *
38 * @putback_page:
39 * If migration fails on the isolated page, the VM informs the driver
40 * that the page is no longer a candidate for migration by calling
41 * this function. The driver should put the isolated page back into
42 * its own data structure.
43 */
44struct movable_operations {
45 bool (*isolate_page)(struct page *, isolate_mode_t);
46 int (*migrate_page)(struct page *dst, struct page *src,
47 enum migrate_mode);
48 void (*putback_page)(struct page *);
49};
50
51/* Defined in mm/debug.c: */
52extern const char *migrate_reason_names[MR_TYPES];
53
54#ifdef CONFIG_MIGRATION
55
56void putback_movable_pages(struct list_head *l);
57int migrate_folio(struct address_space *mapping, struct folio *dst,
58 struct folio *src, enum migrate_mode mode);
59int migrate_pages(struct list_head *l, new_folio_t new, free_folio_t free,
60 unsigned long private, enum migrate_mode mode, int reason,
61 unsigned int *ret_succeeded);
62struct folio *alloc_migration_target(struct folio *src, unsigned long private);
63bool isolate_movable_ops_page(struct page *page, isolate_mode_t mode);
64bool isolate_folio_to_list(struct folio *folio, struct list_head *list);
65
66int migrate_huge_page_move_mapping(struct address_space *mapping,
67 struct folio *dst, struct folio *src);
68void migration_entry_wait_on_locked(swp_entry_t entry, spinlock_t *ptl)
69 __releases(ptl);
70void folio_migrate_flags(struct folio *newfolio, struct folio *folio);
71int folio_migrate_mapping(struct address_space *mapping,
72 struct folio *newfolio, struct folio *folio, int extra_count);
73int set_movable_ops(const struct movable_operations *ops, enum pagetype type);
74
75#else
76
77static inline void putback_movable_pages(struct list_head *l) {}
78static inline int migrate_pages(struct list_head *l, new_folio_t new,
79 free_folio_t free, unsigned long private,
80 enum migrate_mode mode, int reason, unsigned int *ret_succeeded)
81 { return -ENOSYS; }
82static inline struct folio *alloc_migration_target(struct folio *src,
83 unsigned long private)
84 { return NULL; }
85static inline bool isolate_movable_ops_page(struct page *page, isolate_mode_t mode)
86 { return false; }
87static inline bool isolate_folio_to_list(struct folio *folio, struct list_head *list)
88 { return false; }
89
90static inline int migrate_huge_page_move_mapping(struct address_space *mapping,
91 struct folio *dst, struct folio *src)
92{
93 return -ENOSYS;
94}
95static inline int set_movable_ops(const struct movable_operations *ops, enum pagetype type)
96{
97 return -ENOSYS;
98}
99
100#endif /* CONFIG_MIGRATION */
101
102#ifdef CONFIG_NUMA_BALANCING
103int migrate_misplaced_folio_prepare(struct folio *folio,
104 struct vm_area_struct *vma, int node);
105int migrate_misplaced_folio(struct folio *folio, int node);
106#else
107static inline int migrate_misplaced_folio_prepare(struct folio *folio,
108 struct vm_area_struct *vma, int node)
109{
110 return -EAGAIN; /* can't migrate now */
111}
112static inline int migrate_misplaced_folio(struct folio *folio, int node)
113{
114 return -EAGAIN; /* can't migrate now */
115}
116#endif /* CONFIG_NUMA_BALANCING */
117
118#ifdef CONFIG_MIGRATION
119
120/*
121 * Watch out for PAE architecture, which has an unsigned long, and might not
122 * have enough bits to store all physical address and flags. So far we have
123 * enough room for all our flags.
124 */
125#define MIGRATE_PFN_VALID (1UL << 0)
126#define MIGRATE_PFN_MIGRATE (1UL << 1)
127#define MIGRATE_PFN_WRITE (1UL << 3)
128#define MIGRATE_PFN_SHIFT 6
129
130static inline struct page *migrate_pfn_to_page(unsigned long mpfn)
131{
132 if (!(mpfn & MIGRATE_PFN_VALID))
133 return NULL;
134 return pfn_to_page(mpfn >> MIGRATE_PFN_SHIFT);
135}
136
137static inline unsigned long migrate_pfn(unsigned long pfn)
138{
139 return (pfn << MIGRATE_PFN_SHIFT) | MIGRATE_PFN_VALID;
140}
141
142enum migrate_vma_direction {
143 MIGRATE_VMA_SELECT_SYSTEM = 1 << 0,
144 MIGRATE_VMA_SELECT_DEVICE_PRIVATE = 1 << 1,
145 MIGRATE_VMA_SELECT_DEVICE_COHERENT = 1 << 2,
146};
147
148struct migrate_vma {
149 struct vm_area_struct *vma;
150 /*
151 * Both src and dst array must be big enough for
152 * (end - start) >> PAGE_SHIFT entries.
153 *
154 * The src array must not be modified by the caller after
155 * migrate_vma_setup(), and must not change the dst array after
156 * migrate_vma_pages() returns.
157 */
158 unsigned long *dst;
159 unsigned long *src;
160 unsigned long cpages;
161 unsigned long npages;
162 unsigned long start;
163 unsigned long end;
164
165 /*
166 * Set to the owner value also stored in page_pgmap(page)->owner
167 * for migrating out of device private memory. The flags also need to
168 * be set to MIGRATE_VMA_SELECT_DEVICE_PRIVATE.
169 * The caller should always set this field when using mmu notifier
170 * callbacks to avoid device MMU invalidations for device private
171 * pages that are not being migrated.
172 */
173 void *pgmap_owner;
174 unsigned long flags;
175
176 /*
177 * Set to vmf->page if this is being called to migrate a page as part of
178 * a migrate_to_ram() callback.
179 */
180 struct page *fault_page;
181};
182
183int migrate_vma_setup(struct migrate_vma *args);
184void migrate_vma_pages(struct migrate_vma *migrate);
185void migrate_vma_finalize(struct migrate_vma *migrate);
186int migrate_device_range(unsigned long *src_pfns, unsigned long start,
187 unsigned long npages);
188int migrate_device_pfns(unsigned long *src_pfns, unsigned long npages);
189void migrate_device_pages(unsigned long *src_pfns, unsigned long *dst_pfns,
190 unsigned long npages);
191void migrate_device_finalize(unsigned long *src_pfns,
192 unsigned long *dst_pfns, unsigned long npages);
193
194#endif /* CONFIG_MIGRATION */
195
196#endif /* _LINUX_MIGRATE_H */
197