Jérôme Glisse | 133ff0e | 2017-09-08 16:11:23 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2013 Red Hat Inc. |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or modify |
| 5 | * it under the terms of the GNU General Public License as published by |
| 6 | * the Free Software Foundation; either version 2 of the License, or |
| 7 | * (at your option) any later version. |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU General Public License for more details. |
| 13 | * |
| 14 | * Authors: Jérôme Glisse <jglisse@redhat.com> |
| 15 | */ |
| 16 | /* |
| 17 | * Heterogeneous Memory Management (HMM) |
| 18 | * |
| 19 | * See Documentation/vm/hmm.txt for reasons and overview of what HMM is and it |
| 20 | * is for. Here we focus on the HMM API description, with some explanation of |
| 21 | * the underlying implementation. |
| 22 | * |
| 23 | * Short description: HMM provides a set of helpers to share a virtual address |
| 24 | * space between CPU and a device, so that the device can access any valid |
| 25 | * address of the process (while still obeying memory protection). HMM also |
| 26 | * provides helpers to migrate process memory to device memory, and back. Each |
| 27 | * set of functionality (address space mirroring, and migration to and from |
| 28 | * device memory) can be used independently of the other. |
| 29 | * |
| 30 | * |
| 31 | * HMM address space mirroring API: |
| 32 | * |
| 33 | * Use HMM address space mirroring if you want to mirror range of the CPU page |
| 34 | * table of a process into a device page table. Here, "mirror" means "keep |
| 35 | * synchronized". Prerequisites: the device must provide the ability to write- |
| 36 | * protect its page tables (at PAGE_SIZE granularity), and must be able to |
| 37 | * recover from the resulting potential page faults. |
| 38 | * |
| 39 | * HMM guarantees that at any point in time, a given virtual address points to |
| 40 | * either the same memory in both CPU and device page tables (that is: CPU and |
| 41 | * device page tables each point to the same pages), or that one page table (CPU |
| 42 | * or device) points to no entry, while the other still points to the old page |
| 43 | * for the address. The latter case happens when the CPU page table update |
| 44 | * happens first, and then the update is mirrored over to the device page table. |
| 45 | * This does not cause any issue, because the CPU page table cannot start |
| 46 | * pointing to a new page until the device page table is invalidated. |
| 47 | * |
| 48 | * HMM uses mmu_notifiers to monitor the CPU page tables, and forwards any |
| 49 | * updates to each device driver that has registered a mirror. It also provides |
| 50 | * some API calls to help with taking a snapshot of the CPU page table, and to |
| 51 | * synchronize with any updates that might happen concurrently. |
| 52 | * |
| 53 | * |
| 54 | * HMM migration to and from device memory: |
| 55 | * |
| 56 | * HMM provides a set of helpers to hotplug device memory as ZONE_DEVICE, with |
| 57 | * a new MEMORY_DEVICE_PRIVATE type. This provides a struct page for each page |
| 58 | * of the device memory, and allows the device driver to manage its memory |
| 59 | * using those struct pages. Having struct pages for device memory makes |
| 60 | * migration easier. Because that memory is not addressable by the CPU it must |
| 61 | * never be pinned to the device; in other words, any CPU page fault can always |
| 62 | * cause the device memory to be migrated (copied/moved) back to regular memory. |
| 63 | * |
| 64 | * A new migrate helper (migrate_vma()) has been added (see mm/migrate.c) that |
| 65 | * allows use of a device DMA engine to perform the copy operation between |
| 66 | * regular system memory and device memory. |
| 67 | */ |
| 68 | #ifndef LINUX_HMM_H |
| 69 | #define LINUX_HMM_H |
| 70 | |
| 71 | #include <linux/kconfig.h> |
| 72 | |
| 73 | #if IS_ENABLED(CONFIG_HMM) |
| 74 | |
Jérôme Glisse | c0b1240 | 2017-09-08 16:11:27 -0700 | [diff] [blame] | 75 | struct hmm; |
Jérôme Glisse | 133ff0e | 2017-09-08 16:11:23 -0700 | [diff] [blame] | 76 | |
| 77 | /* |
| 78 | * hmm_pfn_t - HMM uses its own pfn type to keep several flags per page |
| 79 | * |
| 80 | * Flags: |
| 81 | * HMM_PFN_VALID: pfn is valid |
Jérôme Glisse | da4c3c7 | 2017-09-08 16:11:31 -0700 | [diff] [blame] | 82 | * HMM_PFN_READ: CPU page table has read permission set |
Jérôme Glisse | 133ff0e | 2017-09-08 16:11:23 -0700 | [diff] [blame] | 83 | * HMM_PFN_WRITE: CPU page table has write permission set |
Jérôme Glisse | da4c3c7 | 2017-09-08 16:11:31 -0700 | [diff] [blame] | 84 | * HMM_PFN_ERROR: corresponding CPU page table entry points to poisoned memory |
| 85 | * HMM_PFN_EMPTY: corresponding CPU page table entry is pte_none() |
| 86 | * HMM_PFN_SPECIAL: corresponding CPU page table entry is special; i.e., the |
| 87 | * result of vm_insert_pfn() or vm_insert_page(). Therefore, it should not |
| 88 | * be mirrored by a device, because the entry will never have HMM_PFN_VALID |
| 89 | * set and the pfn value is undefined. |
| 90 | * HMM_PFN_DEVICE_UNADDRESSABLE: unaddressable device memory (ZONE_DEVICE) |
Jérôme Glisse | 133ff0e | 2017-09-08 16:11:23 -0700 | [diff] [blame] | 91 | */ |
| 92 | typedef unsigned long hmm_pfn_t; |
| 93 | |
| 94 | #define HMM_PFN_VALID (1 << 0) |
Jérôme Glisse | da4c3c7 | 2017-09-08 16:11:31 -0700 | [diff] [blame] | 95 | #define HMM_PFN_READ (1 << 1) |
| 96 | #define HMM_PFN_WRITE (1 << 2) |
| 97 | #define HMM_PFN_ERROR (1 << 3) |
| 98 | #define HMM_PFN_EMPTY (1 << 4) |
| 99 | #define HMM_PFN_SPECIAL (1 << 5) |
| 100 | #define HMM_PFN_DEVICE_UNADDRESSABLE (1 << 6) |
| 101 | #define HMM_PFN_SHIFT 7 |
Jérôme Glisse | 133ff0e | 2017-09-08 16:11:23 -0700 | [diff] [blame] | 102 | |
| 103 | /* |
| 104 | * hmm_pfn_t_to_page() - return struct page pointed to by a valid hmm_pfn_t |
| 105 | * @pfn: hmm_pfn_t to convert to struct page |
| 106 | * Returns: struct page pointer if pfn is a valid hmm_pfn_t, NULL otherwise |
| 107 | * |
| 108 | * If the hmm_pfn_t is valid (ie valid flag set) then return the struct page |
| 109 | * matching the pfn value stored in the hmm_pfn_t. Otherwise return NULL. |
| 110 | */ |
| 111 | static inline struct page *hmm_pfn_t_to_page(hmm_pfn_t pfn) |
| 112 | { |
| 113 | if (!(pfn & HMM_PFN_VALID)) |
| 114 | return NULL; |
| 115 | return pfn_to_page(pfn >> HMM_PFN_SHIFT); |
| 116 | } |
| 117 | |
| 118 | /* |
| 119 | * hmm_pfn_t_to_pfn() - return pfn value store in a hmm_pfn_t |
| 120 | * @pfn: hmm_pfn_t to extract pfn from |
| 121 | * Returns: pfn value if hmm_pfn_t is valid, -1UL otherwise |
| 122 | */ |
| 123 | static inline unsigned long hmm_pfn_t_to_pfn(hmm_pfn_t pfn) |
| 124 | { |
| 125 | if (!(pfn & HMM_PFN_VALID)) |
| 126 | return -1UL; |
| 127 | return (pfn >> HMM_PFN_SHIFT); |
| 128 | } |
| 129 | |
| 130 | /* |
| 131 | * hmm_pfn_t_from_page() - create a valid hmm_pfn_t value from struct page |
| 132 | * @page: struct page pointer for which to create the hmm_pfn_t |
| 133 | * Returns: valid hmm_pfn_t for the page |
| 134 | */ |
| 135 | static inline hmm_pfn_t hmm_pfn_t_from_page(struct page *page) |
| 136 | { |
| 137 | return (page_to_pfn(page) << HMM_PFN_SHIFT) | HMM_PFN_VALID; |
| 138 | } |
| 139 | |
| 140 | /* |
| 141 | * hmm_pfn_t_from_pfn() - create a valid hmm_pfn_t value from pfn |
| 142 | * @pfn: pfn value for which to create the hmm_pfn_t |
| 143 | * Returns: valid hmm_pfn_t for the pfn |
| 144 | */ |
| 145 | static inline hmm_pfn_t hmm_pfn_t_from_pfn(unsigned long pfn) |
| 146 | { |
| 147 | return (pfn << HMM_PFN_SHIFT) | HMM_PFN_VALID; |
| 148 | } |
| 149 | |
| 150 | |
Jérôme Glisse | c0b1240 | 2017-09-08 16:11:27 -0700 | [diff] [blame] | 151 | #if IS_ENABLED(CONFIG_HMM_MIRROR) |
| 152 | /* |
| 153 | * Mirroring: how to synchronize device page table with CPU page table. |
| 154 | * |
| 155 | * A device driver that is participating in HMM mirroring must always |
| 156 | * synchronize with CPU page table updates. For this, device drivers can either |
| 157 | * directly use mmu_notifier APIs or they can use the hmm_mirror API. Device |
| 158 | * drivers can decide to register one mirror per device per process, or just |
| 159 | * one mirror per process for a group of devices. The pattern is: |
| 160 | * |
| 161 | * int device_bind_address_space(..., struct mm_struct *mm, ...) |
| 162 | * { |
| 163 | * struct device_address_space *das; |
| 164 | * |
| 165 | * // Device driver specific initialization, and allocation of das |
| 166 | * // which contains an hmm_mirror struct as one of its fields. |
| 167 | * ... |
| 168 | * |
| 169 | * ret = hmm_mirror_register(&das->mirror, mm, &device_mirror_ops); |
| 170 | * if (ret) { |
| 171 | * // Cleanup on error |
| 172 | * return ret; |
| 173 | * } |
| 174 | * |
| 175 | * // Other device driver specific initialization |
| 176 | * ... |
| 177 | * } |
| 178 | * |
| 179 | * Once an hmm_mirror is registered for an address space, the device driver |
| 180 | * will get callbacks through sync_cpu_device_pagetables() operation (see |
| 181 | * hmm_mirror_ops struct). |
| 182 | * |
| 183 | * Device driver must not free the struct containing the hmm_mirror struct |
| 184 | * before calling hmm_mirror_unregister(). The expected usage is to do that when |
| 185 | * the device driver is unbinding from an address space. |
| 186 | * |
| 187 | * |
| 188 | * void device_unbind_address_space(struct device_address_space *das) |
| 189 | * { |
| 190 | * // Device driver specific cleanup |
| 191 | * ... |
| 192 | * |
| 193 | * hmm_mirror_unregister(&das->mirror); |
| 194 | * |
| 195 | * // Other device driver specific cleanup, and now das can be freed |
| 196 | * ... |
| 197 | * } |
| 198 | */ |
| 199 | |
| 200 | struct hmm_mirror; |
| 201 | |
| 202 | /* |
| 203 | * enum hmm_update_type - type of update |
| 204 | * @HMM_UPDATE_INVALIDATE: invalidate range (no indication as to why) |
| 205 | */ |
| 206 | enum hmm_update_type { |
| 207 | HMM_UPDATE_INVALIDATE, |
| 208 | }; |
| 209 | |
| 210 | /* |
| 211 | * struct hmm_mirror_ops - HMM mirror device operations callback |
| 212 | * |
| 213 | * @update: callback to update range on a device |
| 214 | */ |
| 215 | struct hmm_mirror_ops { |
| 216 | /* sync_cpu_device_pagetables() - synchronize page tables |
| 217 | * |
| 218 | * @mirror: pointer to struct hmm_mirror |
| 219 | * @update_type: type of update that occurred to the CPU page table |
| 220 | * @start: virtual start address of the range to update |
| 221 | * @end: virtual end address of the range to update |
| 222 | * |
| 223 | * This callback ultimately originates from mmu_notifiers when the CPU |
| 224 | * page table is updated. The device driver must update its page table |
| 225 | * in response to this callback. The update argument tells what action |
| 226 | * to perform. |
| 227 | * |
| 228 | * The device driver must not return from this callback until the device |
| 229 | * page tables are completely updated (TLBs flushed, etc); this is a |
| 230 | * synchronous call. |
| 231 | */ |
| 232 | void (*sync_cpu_device_pagetables)(struct hmm_mirror *mirror, |
| 233 | enum hmm_update_type update_type, |
| 234 | unsigned long start, |
| 235 | unsigned long end); |
| 236 | }; |
| 237 | |
| 238 | /* |
| 239 | * struct hmm_mirror - mirror struct for a device driver |
| 240 | * |
| 241 | * @hmm: pointer to struct hmm (which is unique per mm_struct) |
| 242 | * @ops: device driver callback for HMM mirror operations |
| 243 | * @list: for list of mirrors of a given mm |
| 244 | * |
| 245 | * Each address space (mm_struct) being mirrored by a device must register one |
| 246 | * instance of an hmm_mirror struct with HMM. HMM will track the list of all |
| 247 | * mirrors for each mm_struct. |
| 248 | */ |
| 249 | struct hmm_mirror { |
| 250 | struct hmm *hmm; |
| 251 | const struct hmm_mirror_ops *ops; |
| 252 | struct list_head list; |
| 253 | }; |
| 254 | |
| 255 | int hmm_mirror_register(struct hmm_mirror *mirror, struct mm_struct *mm); |
| 256 | void hmm_mirror_unregister(struct hmm_mirror *mirror); |
Jérôme Glisse | da4c3c7 | 2017-09-08 16:11:31 -0700 | [diff] [blame] | 257 | |
| 258 | |
| 259 | /* |
| 260 | * struct hmm_range - track invalidation lock on virtual address range |
| 261 | * |
| 262 | * @list: all range lock are on a list |
| 263 | * @start: range virtual start address (inclusive) |
| 264 | * @end: range virtual end address (exclusive) |
| 265 | * @pfns: array of pfns (big enough for the range) |
| 266 | * @valid: pfns array did not change since it has been fill by an HMM function |
| 267 | */ |
| 268 | struct hmm_range { |
| 269 | struct list_head list; |
| 270 | unsigned long start; |
| 271 | unsigned long end; |
| 272 | hmm_pfn_t *pfns; |
| 273 | bool valid; |
| 274 | }; |
| 275 | |
| 276 | /* |
| 277 | * To snapshot the CPU page table, call hmm_vma_get_pfns(), then take a device |
| 278 | * driver lock that serializes device page table updates, then call |
| 279 | * hmm_vma_range_done(), to check if the snapshot is still valid. The same |
| 280 | * device driver page table update lock must also be used in the |
| 281 | * hmm_mirror_ops.sync_cpu_device_pagetables() callback, so that CPU page |
| 282 | * table invalidation serializes on it. |
| 283 | * |
| 284 | * YOU MUST CALL hmm_vma_range_done() ONCE AND ONLY ONCE EACH TIME YOU CALL |
| 285 | * hmm_vma_get_pfns() WITHOUT ERROR ! |
| 286 | * |
| 287 | * IF YOU DO NOT FOLLOW THE ABOVE RULE THE SNAPSHOT CONTENT MIGHT BE INVALID ! |
| 288 | */ |
| 289 | int hmm_vma_get_pfns(struct vm_area_struct *vma, |
| 290 | struct hmm_range *range, |
| 291 | unsigned long start, |
| 292 | unsigned long end, |
| 293 | hmm_pfn_t *pfns); |
| 294 | bool hmm_vma_range_done(struct vm_area_struct *vma, struct hmm_range *range); |
Jérôme Glisse | 74eee18 | 2017-09-08 16:11:35 -0700 | [diff] [blame] | 295 | |
| 296 | |
| 297 | /* |
| 298 | * Fault memory on behalf of device driver. Unlike handle_mm_fault(), this will |
| 299 | * not migrate any device memory back to system memory. The hmm_pfn_t array will |
| 300 | * be updated with the fault result and current snapshot of the CPU page table |
| 301 | * for the range. |
| 302 | * |
| 303 | * The mmap_sem must be taken in read mode before entering and it might be |
| 304 | * dropped by the function if the block argument is false. In that case, the |
| 305 | * function returns -EAGAIN. |
| 306 | * |
| 307 | * Return value does not reflect if the fault was successful for every single |
| 308 | * address or not. Therefore, the caller must to inspect the hmm_pfn_t array to |
| 309 | * determine fault status for each address. |
| 310 | * |
| 311 | * Trying to fault inside an invalid vma will result in -EINVAL. |
| 312 | * |
| 313 | * See the function description in mm/hmm.c for further documentation. |
| 314 | */ |
| 315 | int hmm_vma_fault(struct vm_area_struct *vma, |
| 316 | struct hmm_range *range, |
| 317 | unsigned long start, |
| 318 | unsigned long end, |
| 319 | hmm_pfn_t *pfns, |
| 320 | bool write, |
| 321 | bool block); |
Jérôme Glisse | c0b1240 | 2017-09-08 16:11:27 -0700 | [diff] [blame] | 322 | #endif /* IS_ENABLED(CONFIG_HMM_MIRROR) */ |
| 323 | |
| 324 | |
Jérôme Glisse | 133ff0e | 2017-09-08 16:11:23 -0700 | [diff] [blame] | 325 | /* Below are for HMM internal use only! Not to be used by device driver! */ |
| 326 | void hmm_mm_destroy(struct mm_struct *mm); |
| 327 | |
| 328 | static inline void hmm_mm_init(struct mm_struct *mm) |
| 329 | { |
| 330 | mm->hmm = NULL; |
| 331 | } |
| 332 | |
| 333 | #else /* IS_ENABLED(CONFIG_HMM) */ |
| 334 | |
| 335 | /* Below are for HMM internal use only! Not to be used by device driver! */ |
| 336 | static inline void hmm_mm_destroy(struct mm_struct *mm) {} |
| 337 | static inline void hmm_mm_init(struct mm_struct *mm) {} |
| 338 | |
| 339 | #endif /* IS_ENABLED(CONFIG_HMM) */ |
| 340 | #endif /* LINUX_HMM_H */ |