| 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 folio *new_folio_t(struct folio *folio, unsigned long private); |
| 11 | typedef void free_folio_t(struct folio *folio, unsigned long private); |
| 12 | |
| 13 | struct 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 | */ |
| 44 | struct 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: */ |
| 52 | extern const char *migrate_reason_names[MR_TYPES]; |
| 53 | |
| 54 | #ifdef CONFIG_MIGRATION |
| 55 | |
| 56 | void putback_movable_pages(struct list_head *l); |
| 57 | int migrate_folio(struct address_space *mapping, struct folio *dst, |
| 58 | struct folio *src, enum migrate_mode mode); |
| 59 | int 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); |
| 62 | struct folio *alloc_migration_target(struct folio *src, unsigned long private); |
| 63 | bool isolate_movable_ops_page(struct page *page, isolate_mode_t mode); |
| 64 | bool isolate_folio_to_list(struct folio *folio, struct list_head *list); |
| 65 | |
| 66 | int migrate_huge_page_move_mapping(struct address_space *mapping, |
| 67 | struct folio *dst, struct folio *src); |
| 68 | void migration_entry_wait_on_locked(softleaf_t entry, spinlock_t *ptl) |
| 69 | __releases(ptl); |
| 70 | void folio_migrate_flags(struct folio *newfolio, struct folio *folio); |
| 71 | int folio_migrate_mapping(struct address_space *mapping, |
| 72 | struct folio *newfolio, struct folio *folio, int ); |
| 73 | int set_movable_ops(const struct movable_operations *ops, enum pagetype type); |
| 74 | |
| 75 | #else |
| 76 | |
| 77 | static inline void putback_movable_pages(struct list_head *l) {} |
| 78 | static 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; } |
| 82 | static inline struct folio *alloc_migration_target(struct folio *src, |
| 83 | unsigned long private) |
| 84 | { return NULL; } |
| 85 | static inline bool isolate_movable_ops_page(struct page *page, isolate_mode_t mode) |
| 86 | { return false; } |
| 87 | static inline bool isolate_folio_to_list(struct folio *folio, struct list_head *list) |
| 88 | { return false; } |
| 89 | |
| 90 | static inline int migrate_huge_page_move_mapping(struct address_space *mapping, |
| 91 | struct folio *dst, struct folio *src) |
| 92 | { |
| 93 | return -ENOSYS; |
| 94 | } |
| 95 | static 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 |
| 103 | int migrate_misplaced_folio_prepare(struct folio *folio, |
| 104 | struct vm_area_struct *vma, int node); |
| 105 | int migrate_misplaced_folio(struct folio *folio, int node); |
| 106 | #else |
| 107 | static 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 | } |
| 112 | static 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_COMPOUND (1UL << 4) |
| 129 | #define MIGRATE_PFN_SHIFT 6 |
| 130 | |
| 131 | static inline struct page *migrate_pfn_to_page(unsigned long mpfn) |
| 132 | { |
| 133 | if (!(mpfn & MIGRATE_PFN_VALID)) |
| 134 | return NULL; |
| 135 | return pfn_to_page(mpfn >> MIGRATE_PFN_SHIFT); |
| 136 | } |
| 137 | |
| 138 | static inline unsigned long migrate_pfn(unsigned long pfn) |
| 139 | { |
| 140 | return (pfn << MIGRATE_PFN_SHIFT) | MIGRATE_PFN_VALID; |
| 141 | } |
| 142 | |
| 143 | enum migrate_vma_direction { |
| 144 | MIGRATE_VMA_SELECT_SYSTEM = 1 << 0, |
| 145 | MIGRATE_VMA_SELECT_DEVICE_PRIVATE = 1 << 1, |
| 146 | MIGRATE_VMA_SELECT_DEVICE_COHERENT = 1 << 2, |
| 147 | MIGRATE_VMA_SELECT_COMPOUND = 1 << 3, |
| 148 | }; |
| 149 | |
| 150 | struct migrate_vma { |
| 151 | struct vm_area_struct *vma; |
| 152 | /* |
| 153 | * Both src and dst array must be big enough for |
| 154 | * (end - start) >> PAGE_SHIFT entries. |
| 155 | * |
| 156 | * The src array must not be modified by the caller after |
| 157 | * migrate_vma_setup(), and must not change the dst array after |
| 158 | * migrate_vma_pages() returns. |
| 159 | */ |
| 160 | unsigned long *dst; |
| 161 | unsigned long *src; |
| 162 | unsigned long cpages; |
| 163 | unsigned long npages; |
| 164 | unsigned long start; |
| 165 | unsigned long end; |
| 166 | |
| 167 | /* |
| 168 | * Set to the owner value also stored in page_pgmap(page)->owner |
| 169 | * for migrating out of device private memory. The flags also need to |
| 170 | * be set to MIGRATE_VMA_SELECT_DEVICE_PRIVATE. |
| 171 | * The caller should always set this field when using mmu notifier |
| 172 | * callbacks to avoid device MMU invalidations for device private |
| 173 | * pages that are not being migrated. |
| 174 | */ |
| 175 | void *pgmap_owner; |
| 176 | unsigned long flags; |
| 177 | |
| 178 | /* |
| 179 | * Set to vmf->page if this is being called to migrate a page as part of |
| 180 | * a migrate_to_ram() callback. |
| 181 | */ |
| 182 | struct page *fault_page; |
| 183 | }; |
| 184 | |
| 185 | int migrate_vma_setup(struct migrate_vma *args); |
| 186 | void migrate_vma_pages(struct migrate_vma *migrate); |
| 187 | void migrate_vma_finalize(struct migrate_vma *migrate); |
| 188 | int migrate_device_range(unsigned long *src_pfns, unsigned long start, |
| 189 | unsigned long npages); |
| 190 | int migrate_device_pfns(unsigned long *src_pfns, unsigned long npages); |
| 191 | void migrate_device_pages(unsigned long *src_pfns, unsigned long *dst_pfns, |
| 192 | unsigned long npages); |
| 193 | void migrate_device_finalize(unsigned long *src_pfns, |
| 194 | unsigned long *dst_pfns, unsigned long npages); |
| 195 | |
| 196 | #endif /* CONFIG_MIGRATION */ |
| 197 | |
| 198 | #endif /* _LINUX_MIGRATE_H */ |
| 199 | |