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 | * Refer to include/linux/hmm.h for information about heterogeneous memory |
| 18 | * management or HMM for short. |
| 19 | */ |
| 20 | #include <linux/mm.h> |
| 21 | #include <linux/hmm.h> |
Jérôme Glisse | 858b54d | 2017-09-08 16:12:02 -0700 | [diff] [blame] | 22 | #include <linux/init.h> |
Jérôme Glisse | da4c3c7 | 2017-09-08 16:11:31 -0700 | [diff] [blame] | 23 | #include <linux/rmap.h> |
| 24 | #include <linux/swap.h> |
Jérôme Glisse | 133ff0e | 2017-09-08 16:11:23 -0700 | [diff] [blame] | 25 | #include <linux/slab.h> |
| 26 | #include <linux/sched.h> |
Jérôme Glisse | 4ef589d | 2017-09-08 16:11:58 -0700 | [diff] [blame] | 27 | #include <linux/mmzone.h> |
| 28 | #include <linux/pagemap.h> |
Jérôme Glisse | da4c3c7 | 2017-09-08 16:11:31 -0700 | [diff] [blame] | 29 | #include <linux/swapops.h> |
| 30 | #include <linux/hugetlb.h> |
Jérôme Glisse | 4ef589d | 2017-09-08 16:11:58 -0700 | [diff] [blame] | 31 | #include <linux/memremap.h> |
Jérôme Glisse | 7b2d55d2 | 2017-09-08 16:11:46 -0700 | [diff] [blame] | 32 | #include <linux/jump_label.h> |
Jérôme Glisse | c0b1240 | 2017-09-08 16:11:27 -0700 | [diff] [blame] | 33 | #include <linux/mmu_notifier.h> |
Jérôme Glisse | 4ef589d | 2017-09-08 16:11:58 -0700 | [diff] [blame] | 34 | #include <linux/memory_hotplug.h> |
| 35 | |
| 36 | #define PA_SECTION_SIZE (1UL << PA_SECTION_SHIFT) |
Jérôme Glisse | 133ff0e | 2017-09-08 16:11:23 -0700 | [diff] [blame] | 37 | |
Jérôme Glisse | 6b368cd | 2017-09-08 16:12:32 -0700 | [diff] [blame] | 38 | #if defined(CONFIG_DEVICE_PRIVATE) || defined(CONFIG_DEVICE_PUBLIC) |
Jérôme Glisse | 7b2d55d2 | 2017-09-08 16:11:46 -0700 | [diff] [blame] | 39 | /* |
| 40 | * Device private memory see HMM (Documentation/vm/hmm.txt) or hmm.h |
| 41 | */ |
| 42 | DEFINE_STATIC_KEY_FALSE(device_private_key); |
| 43 | EXPORT_SYMBOL(device_private_key); |
Jérôme Glisse | 6b368cd | 2017-09-08 16:12:32 -0700 | [diff] [blame] | 44 | #endif /* CONFIG_DEVICE_PRIVATE || CONFIG_DEVICE_PUBLIC */ |
Jérôme Glisse | 7b2d55d2 | 2017-09-08 16:11:46 -0700 | [diff] [blame] | 45 | |
| 46 | |
Jérôme Glisse | 6b368cd | 2017-09-08 16:12:32 -0700 | [diff] [blame] | 47 | #if IS_ENABLED(CONFIG_HMM_MIRROR) |
Jérôme Glisse | c0b1240 | 2017-09-08 16:11:27 -0700 | [diff] [blame] | 48 | static const struct mmu_notifier_ops hmm_mmu_notifier_ops; |
| 49 | |
Jérôme Glisse | 133ff0e | 2017-09-08 16:11:23 -0700 | [diff] [blame] | 50 | /* |
| 51 | * struct hmm - HMM per mm struct |
| 52 | * |
| 53 | * @mm: mm struct this HMM struct is bound to |
Jérôme Glisse | da4c3c7 | 2017-09-08 16:11:31 -0700 | [diff] [blame] | 54 | * @lock: lock protecting ranges list |
Jérôme Glisse | c0b1240 | 2017-09-08 16:11:27 -0700 | [diff] [blame] | 55 | * @sequence: we track updates to the CPU page table with a sequence number |
Jérôme Glisse | da4c3c7 | 2017-09-08 16:11:31 -0700 | [diff] [blame] | 56 | * @ranges: list of range being snapshotted |
Jérôme Glisse | c0b1240 | 2017-09-08 16:11:27 -0700 | [diff] [blame] | 57 | * @mirrors: list of mirrors for this mm |
| 58 | * @mmu_notifier: mmu notifier to track updates to CPU page table |
| 59 | * @mirrors_sem: read/write semaphore protecting the mirrors list |
Jérôme Glisse | 133ff0e | 2017-09-08 16:11:23 -0700 | [diff] [blame] | 60 | */ |
| 61 | struct hmm { |
| 62 | struct mm_struct *mm; |
Jérôme Glisse | da4c3c7 | 2017-09-08 16:11:31 -0700 | [diff] [blame] | 63 | spinlock_t lock; |
Jérôme Glisse | c0b1240 | 2017-09-08 16:11:27 -0700 | [diff] [blame] | 64 | atomic_t sequence; |
Jérôme Glisse | da4c3c7 | 2017-09-08 16:11:31 -0700 | [diff] [blame] | 65 | struct list_head ranges; |
Jérôme Glisse | c0b1240 | 2017-09-08 16:11:27 -0700 | [diff] [blame] | 66 | struct list_head mirrors; |
| 67 | struct mmu_notifier mmu_notifier; |
| 68 | struct rw_semaphore mirrors_sem; |
Jérôme Glisse | 133ff0e | 2017-09-08 16:11:23 -0700 | [diff] [blame] | 69 | }; |
| 70 | |
| 71 | /* |
| 72 | * hmm_register - register HMM against an mm (HMM internal) |
| 73 | * |
| 74 | * @mm: mm struct to attach to |
| 75 | * |
| 76 | * This is not intended to be used directly by device drivers. It allocates an |
| 77 | * HMM struct if mm does not have one, and initializes it. |
| 78 | */ |
| 79 | static struct hmm *hmm_register(struct mm_struct *mm) |
| 80 | { |
Jérôme Glisse | c0b1240 | 2017-09-08 16:11:27 -0700 | [diff] [blame] | 81 | struct hmm *hmm = READ_ONCE(mm->hmm); |
| 82 | bool cleanup = false; |
Jérôme Glisse | 133ff0e | 2017-09-08 16:11:23 -0700 | [diff] [blame] | 83 | |
| 84 | /* |
| 85 | * The hmm struct can only be freed once the mm_struct goes away, |
| 86 | * hence we should always have pre-allocated an new hmm struct |
| 87 | * above. |
| 88 | */ |
Jérôme Glisse | c0b1240 | 2017-09-08 16:11:27 -0700 | [diff] [blame] | 89 | if (hmm) |
| 90 | return hmm; |
| 91 | |
| 92 | hmm = kmalloc(sizeof(*hmm), GFP_KERNEL); |
| 93 | if (!hmm) |
| 94 | return NULL; |
| 95 | INIT_LIST_HEAD(&hmm->mirrors); |
| 96 | init_rwsem(&hmm->mirrors_sem); |
| 97 | atomic_set(&hmm->sequence, 0); |
| 98 | hmm->mmu_notifier.ops = NULL; |
Jérôme Glisse | da4c3c7 | 2017-09-08 16:11:31 -0700 | [diff] [blame] | 99 | INIT_LIST_HEAD(&hmm->ranges); |
| 100 | spin_lock_init(&hmm->lock); |
Jérôme Glisse | c0b1240 | 2017-09-08 16:11:27 -0700 | [diff] [blame] | 101 | hmm->mm = mm; |
| 102 | |
| 103 | /* |
| 104 | * We should only get here if hold the mmap_sem in write mode ie on |
| 105 | * registration of first mirror through hmm_mirror_register() |
| 106 | */ |
| 107 | hmm->mmu_notifier.ops = &hmm_mmu_notifier_ops; |
| 108 | if (__mmu_notifier_register(&hmm->mmu_notifier, mm)) { |
| 109 | kfree(hmm); |
| 110 | return NULL; |
| 111 | } |
| 112 | |
| 113 | spin_lock(&mm->page_table_lock); |
| 114 | if (!mm->hmm) |
| 115 | mm->hmm = hmm; |
| 116 | else |
| 117 | cleanup = true; |
| 118 | spin_unlock(&mm->page_table_lock); |
| 119 | |
| 120 | if (cleanup) { |
| 121 | mmu_notifier_unregister(&hmm->mmu_notifier, mm); |
| 122 | kfree(hmm); |
| 123 | } |
| 124 | |
Jérôme Glisse | 133ff0e | 2017-09-08 16:11:23 -0700 | [diff] [blame] | 125 | return mm->hmm; |
| 126 | } |
| 127 | |
| 128 | void hmm_mm_destroy(struct mm_struct *mm) |
| 129 | { |
| 130 | kfree(mm->hmm); |
| 131 | } |
Jérôme Glisse | c0b1240 | 2017-09-08 16:11:27 -0700 | [diff] [blame] | 132 | |
Jérôme Glisse | c0b1240 | 2017-09-08 16:11:27 -0700 | [diff] [blame] | 133 | static void hmm_invalidate_range(struct hmm *hmm, |
| 134 | enum hmm_update_type action, |
| 135 | unsigned long start, |
| 136 | unsigned long end) |
| 137 | { |
| 138 | struct hmm_mirror *mirror; |
Jérôme Glisse | da4c3c7 | 2017-09-08 16:11:31 -0700 | [diff] [blame] | 139 | struct hmm_range *range; |
| 140 | |
| 141 | spin_lock(&hmm->lock); |
| 142 | list_for_each_entry(range, &hmm->ranges, list) { |
| 143 | unsigned long addr, idx, npages; |
| 144 | |
| 145 | if (end < range->start || start >= range->end) |
| 146 | continue; |
| 147 | |
| 148 | range->valid = false; |
| 149 | addr = max(start, range->start); |
| 150 | idx = (addr - range->start) >> PAGE_SHIFT; |
| 151 | npages = (min(range->end, end) - addr) >> PAGE_SHIFT; |
| 152 | memset(&range->pfns[idx], 0, sizeof(*range->pfns) * npages); |
| 153 | } |
| 154 | spin_unlock(&hmm->lock); |
Jérôme Glisse | c0b1240 | 2017-09-08 16:11:27 -0700 | [diff] [blame] | 155 | |
| 156 | down_read(&hmm->mirrors_sem); |
| 157 | list_for_each_entry(mirror, &hmm->mirrors, list) |
| 158 | mirror->ops->sync_cpu_device_pagetables(mirror, action, |
| 159 | start, end); |
| 160 | up_read(&hmm->mirrors_sem); |
| 161 | } |
| 162 | |
Ralph Campbell | e140151 | 2018-04-10 16:28:19 -0700 | [diff] [blame] | 163 | static void hmm_release(struct mmu_notifier *mn, struct mm_struct *mm) |
| 164 | { |
| 165 | struct hmm_mirror *mirror; |
| 166 | struct hmm *hmm = mm->hmm; |
| 167 | |
| 168 | down_write(&hmm->mirrors_sem); |
| 169 | mirror = list_first_entry_or_null(&hmm->mirrors, struct hmm_mirror, |
| 170 | list); |
| 171 | while (mirror) { |
| 172 | list_del_init(&mirror->list); |
| 173 | if (mirror->ops->release) { |
| 174 | /* |
| 175 | * Drop mirrors_sem so callback can wait on any pending |
| 176 | * work that might itself trigger mmu_notifier callback |
| 177 | * and thus would deadlock with us. |
| 178 | */ |
| 179 | up_write(&hmm->mirrors_sem); |
| 180 | mirror->ops->release(mirror); |
| 181 | down_write(&hmm->mirrors_sem); |
| 182 | } |
| 183 | mirror = list_first_entry_or_null(&hmm->mirrors, |
| 184 | struct hmm_mirror, list); |
| 185 | } |
| 186 | up_write(&hmm->mirrors_sem); |
| 187 | } |
| 188 | |
Jérôme Glisse | c0b1240 | 2017-09-08 16:11:27 -0700 | [diff] [blame] | 189 | static void hmm_invalidate_range_start(struct mmu_notifier *mn, |
| 190 | struct mm_struct *mm, |
| 191 | unsigned long start, |
| 192 | unsigned long end) |
| 193 | { |
| 194 | struct hmm *hmm = mm->hmm; |
| 195 | |
| 196 | VM_BUG_ON(!hmm); |
| 197 | |
| 198 | atomic_inc(&hmm->sequence); |
| 199 | } |
| 200 | |
| 201 | static void hmm_invalidate_range_end(struct mmu_notifier *mn, |
| 202 | struct mm_struct *mm, |
| 203 | unsigned long start, |
| 204 | unsigned long end) |
| 205 | { |
| 206 | struct hmm *hmm = mm->hmm; |
| 207 | |
| 208 | VM_BUG_ON(!hmm); |
| 209 | |
| 210 | hmm_invalidate_range(mm->hmm, HMM_UPDATE_INVALIDATE, start, end); |
| 211 | } |
| 212 | |
| 213 | static const struct mmu_notifier_ops hmm_mmu_notifier_ops = { |
Ralph Campbell | e140151 | 2018-04-10 16:28:19 -0700 | [diff] [blame] | 214 | .release = hmm_release, |
Jérôme Glisse | c0b1240 | 2017-09-08 16:11:27 -0700 | [diff] [blame] | 215 | .invalidate_range_start = hmm_invalidate_range_start, |
| 216 | .invalidate_range_end = hmm_invalidate_range_end, |
| 217 | }; |
| 218 | |
| 219 | /* |
| 220 | * hmm_mirror_register() - register a mirror against an mm |
| 221 | * |
| 222 | * @mirror: new mirror struct to register |
| 223 | * @mm: mm to register against |
| 224 | * |
| 225 | * To start mirroring a process address space, the device driver must register |
| 226 | * an HMM mirror struct. |
| 227 | * |
| 228 | * THE mm->mmap_sem MUST BE HELD IN WRITE MODE ! |
| 229 | */ |
| 230 | int hmm_mirror_register(struct hmm_mirror *mirror, struct mm_struct *mm) |
| 231 | { |
| 232 | /* Sanity check */ |
| 233 | if (!mm || !mirror || !mirror->ops) |
| 234 | return -EINVAL; |
| 235 | |
Jérôme Glisse | c01cbba | 2018-04-10 16:28:23 -0700 | [diff] [blame] | 236 | again: |
Jérôme Glisse | c0b1240 | 2017-09-08 16:11:27 -0700 | [diff] [blame] | 237 | mirror->hmm = hmm_register(mm); |
| 238 | if (!mirror->hmm) |
| 239 | return -ENOMEM; |
| 240 | |
| 241 | down_write(&mirror->hmm->mirrors_sem); |
Jérôme Glisse | c01cbba | 2018-04-10 16:28:23 -0700 | [diff] [blame] | 242 | if (mirror->hmm->mm == NULL) { |
| 243 | /* |
| 244 | * A racing hmm_mirror_unregister() is about to destroy the hmm |
| 245 | * struct. Try again to allocate a new one. |
| 246 | */ |
| 247 | up_write(&mirror->hmm->mirrors_sem); |
| 248 | mirror->hmm = NULL; |
| 249 | goto again; |
| 250 | } else { |
| 251 | list_add(&mirror->list, &mirror->hmm->mirrors); |
| 252 | up_write(&mirror->hmm->mirrors_sem); |
| 253 | } |
Jérôme Glisse | c0b1240 | 2017-09-08 16:11:27 -0700 | [diff] [blame] | 254 | |
| 255 | return 0; |
| 256 | } |
| 257 | EXPORT_SYMBOL(hmm_mirror_register); |
| 258 | |
| 259 | /* |
| 260 | * hmm_mirror_unregister() - unregister a mirror |
| 261 | * |
| 262 | * @mirror: new mirror struct to register |
| 263 | * |
| 264 | * Stop mirroring a process address space, and cleanup. |
| 265 | */ |
| 266 | void hmm_mirror_unregister(struct hmm_mirror *mirror) |
| 267 | { |
Jérôme Glisse | c01cbba | 2018-04-10 16:28:23 -0700 | [diff] [blame] | 268 | bool should_unregister = false; |
| 269 | struct mm_struct *mm; |
| 270 | struct hmm *hmm; |
Jérôme Glisse | c0b1240 | 2017-09-08 16:11:27 -0700 | [diff] [blame] | 271 | |
Jérôme Glisse | c01cbba | 2018-04-10 16:28:23 -0700 | [diff] [blame] | 272 | if (mirror->hmm == NULL) |
| 273 | return; |
| 274 | |
| 275 | hmm = mirror->hmm; |
Jérôme Glisse | c0b1240 | 2017-09-08 16:11:27 -0700 | [diff] [blame] | 276 | down_write(&hmm->mirrors_sem); |
Ralph Campbell | e140151 | 2018-04-10 16:28:19 -0700 | [diff] [blame] | 277 | list_del_init(&mirror->list); |
Jérôme Glisse | c01cbba | 2018-04-10 16:28:23 -0700 | [diff] [blame] | 278 | should_unregister = list_empty(&hmm->mirrors); |
| 279 | mirror->hmm = NULL; |
| 280 | mm = hmm->mm; |
| 281 | hmm->mm = NULL; |
Jérôme Glisse | c0b1240 | 2017-09-08 16:11:27 -0700 | [diff] [blame] | 282 | up_write(&hmm->mirrors_sem); |
Jérôme Glisse | c01cbba | 2018-04-10 16:28:23 -0700 | [diff] [blame] | 283 | |
| 284 | if (!should_unregister || mm == NULL) |
| 285 | return; |
| 286 | |
| 287 | spin_lock(&mm->page_table_lock); |
| 288 | if (mm->hmm == hmm) |
| 289 | mm->hmm = NULL; |
| 290 | spin_unlock(&mm->page_table_lock); |
| 291 | |
| 292 | mmu_notifier_unregister_no_release(&hmm->mmu_notifier, mm); |
| 293 | kfree(hmm); |
Jérôme Glisse | c0b1240 | 2017-09-08 16:11:27 -0700 | [diff] [blame] | 294 | } |
| 295 | EXPORT_SYMBOL(hmm_mirror_unregister); |
Jérôme Glisse | da4c3c7 | 2017-09-08 16:11:31 -0700 | [diff] [blame] | 296 | |
Jérôme Glisse | 74eee18 | 2017-09-08 16:11:35 -0700 | [diff] [blame] | 297 | struct hmm_vma_walk { |
| 298 | struct hmm_range *range; |
| 299 | unsigned long last; |
| 300 | bool fault; |
| 301 | bool block; |
Jérôme Glisse | 74eee18 | 2017-09-08 16:11:35 -0700 | [diff] [blame] | 302 | }; |
| 303 | |
Jérôme Glisse | 2aee09d | 2018-04-10 16:29:02 -0700 | [diff] [blame] | 304 | static int hmm_vma_do_fault(struct mm_walk *walk, unsigned long addr, |
| 305 | bool write_fault, uint64_t *pfn) |
Jérôme Glisse | 74eee18 | 2017-09-08 16:11:35 -0700 | [diff] [blame] | 306 | { |
| 307 | unsigned int flags = FAULT_FLAG_ALLOW_RETRY | FAULT_FLAG_REMOTE; |
| 308 | struct hmm_vma_walk *hmm_vma_walk = walk->private; |
Jérôme Glisse | f88a1e9 | 2018-04-10 16:29:06 -0700 | [diff] [blame^] | 309 | struct hmm_range *range = hmm_vma_walk->range; |
Jérôme Glisse | 74eee18 | 2017-09-08 16:11:35 -0700 | [diff] [blame] | 310 | struct vm_area_struct *vma = walk->vma; |
| 311 | int r; |
| 312 | |
| 313 | flags |= hmm_vma_walk->block ? 0 : FAULT_FLAG_ALLOW_RETRY; |
Jérôme Glisse | 2aee09d | 2018-04-10 16:29:02 -0700 | [diff] [blame] | 314 | flags |= write_fault ? FAULT_FLAG_WRITE : 0; |
Jérôme Glisse | 74eee18 | 2017-09-08 16:11:35 -0700 | [diff] [blame] | 315 | r = handle_mm_fault(vma, addr, flags); |
| 316 | if (r & VM_FAULT_RETRY) |
| 317 | return -EBUSY; |
| 318 | if (r & VM_FAULT_ERROR) { |
Jérôme Glisse | f88a1e9 | 2018-04-10 16:29:06 -0700 | [diff] [blame^] | 319 | *pfn = range->values[HMM_PFN_ERROR]; |
Jérôme Glisse | 74eee18 | 2017-09-08 16:11:35 -0700 | [diff] [blame] | 320 | return -EFAULT; |
| 321 | } |
| 322 | |
| 323 | return -EAGAIN; |
| 324 | } |
| 325 | |
Jérôme Glisse | da4c3c7 | 2017-09-08 16:11:31 -0700 | [diff] [blame] | 326 | static int hmm_pfns_bad(unsigned long addr, |
| 327 | unsigned long end, |
| 328 | struct mm_walk *walk) |
| 329 | { |
Jérôme Glisse | c719547 | 2018-04-10 16:28:27 -0700 | [diff] [blame] | 330 | struct hmm_vma_walk *hmm_vma_walk = walk->private; |
| 331 | struct hmm_range *range = hmm_vma_walk->range; |
Jérôme Glisse | ff05c0c | 2018-04-10 16:28:38 -0700 | [diff] [blame] | 332 | uint64_t *pfns = range->pfns; |
Jérôme Glisse | da4c3c7 | 2017-09-08 16:11:31 -0700 | [diff] [blame] | 333 | unsigned long i; |
| 334 | |
| 335 | i = (addr - range->start) >> PAGE_SHIFT; |
| 336 | for (; addr < end; addr += PAGE_SIZE, i++) |
Jérôme Glisse | f88a1e9 | 2018-04-10 16:29:06 -0700 | [diff] [blame^] | 337 | pfns[i] = range->values[HMM_PFN_ERROR]; |
Jérôme Glisse | da4c3c7 | 2017-09-08 16:11:31 -0700 | [diff] [blame] | 338 | |
| 339 | return 0; |
| 340 | } |
| 341 | |
Jérôme Glisse | 5504ed2 | 2018-04-10 16:28:46 -0700 | [diff] [blame] | 342 | /* |
| 343 | * hmm_vma_walk_hole() - handle a range lacking valid pmd or pte(s) |
| 344 | * @start: range virtual start address (inclusive) |
| 345 | * @end: range virtual end address (exclusive) |
Jérôme Glisse | 2aee09d | 2018-04-10 16:29:02 -0700 | [diff] [blame] | 346 | * @fault: should we fault or not ? |
| 347 | * @write_fault: write fault ? |
Jérôme Glisse | 5504ed2 | 2018-04-10 16:28:46 -0700 | [diff] [blame] | 348 | * @walk: mm_walk structure |
| 349 | * Returns: 0 on success, -EAGAIN after page fault, or page fault error |
| 350 | * |
| 351 | * This function will be called whenever pmd_none() or pte_none() returns true, |
| 352 | * or whenever there is no page directory covering the virtual address range. |
| 353 | */ |
Jérôme Glisse | 2aee09d | 2018-04-10 16:29:02 -0700 | [diff] [blame] | 354 | static int hmm_vma_walk_hole_(unsigned long addr, unsigned long end, |
| 355 | bool fault, bool write_fault, |
| 356 | struct mm_walk *walk) |
Jérôme Glisse | da4c3c7 | 2017-09-08 16:11:31 -0700 | [diff] [blame] | 357 | { |
Jérôme Glisse | 74eee18 | 2017-09-08 16:11:35 -0700 | [diff] [blame] | 358 | struct hmm_vma_walk *hmm_vma_walk = walk->private; |
| 359 | struct hmm_range *range = hmm_vma_walk->range; |
Jérôme Glisse | ff05c0c | 2018-04-10 16:28:38 -0700 | [diff] [blame] | 360 | uint64_t *pfns = range->pfns; |
Jérôme Glisse | da4c3c7 | 2017-09-08 16:11:31 -0700 | [diff] [blame] | 361 | unsigned long i; |
| 362 | |
Jérôme Glisse | 74eee18 | 2017-09-08 16:11:35 -0700 | [diff] [blame] | 363 | hmm_vma_walk->last = addr; |
Jérôme Glisse | da4c3c7 | 2017-09-08 16:11:31 -0700 | [diff] [blame] | 364 | i = (addr - range->start) >> PAGE_SHIFT; |
Jérôme Glisse | 74eee18 | 2017-09-08 16:11:35 -0700 | [diff] [blame] | 365 | for (; addr < end; addr += PAGE_SIZE, i++) { |
Jérôme Glisse | f88a1e9 | 2018-04-10 16:29:06 -0700 | [diff] [blame^] | 366 | pfns[i] = range->values[HMM_PFN_NONE]; |
Jérôme Glisse | 2aee09d | 2018-04-10 16:29:02 -0700 | [diff] [blame] | 367 | if (fault || write_fault) { |
Jérôme Glisse | 74eee18 | 2017-09-08 16:11:35 -0700 | [diff] [blame] | 368 | int ret; |
Jérôme Glisse | da4c3c7 | 2017-09-08 16:11:31 -0700 | [diff] [blame] | 369 | |
Jérôme Glisse | 2aee09d | 2018-04-10 16:29:02 -0700 | [diff] [blame] | 370 | ret = hmm_vma_do_fault(walk, addr, write_fault, |
| 371 | &pfns[i]); |
Jérôme Glisse | 74eee18 | 2017-09-08 16:11:35 -0700 | [diff] [blame] | 372 | if (ret != -EAGAIN) |
| 373 | return ret; |
| 374 | } |
| 375 | } |
| 376 | |
Jérôme Glisse | 2aee09d | 2018-04-10 16:29:02 -0700 | [diff] [blame] | 377 | return (fault || write_fault) ? -EAGAIN : 0; |
| 378 | } |
| 379 | |
| 380 | static inline void hmm_pte_need_fault(const struct hmm_vma_walk *hmm_vma_walk, |
| 381 | uint64_t pfns, uint64_t cpu_flags, |
| 382 | bool *fault, bool *write_fault) |
| 383 | { |
Jérôme Glisse | f88a1e9 | 2018-04-10 16:29:06 -0700 | [diff] [blame^] | 384 | struct hmm_range *range = hmm_vma_walk->range; |
| 385 | |
Jérôme Glisse | 2aee09d | 2018-04-10 16:29:02 -0700 | [diff] [blame] | 386 | *fault = *write_fault = false; |
| 387 | if (!hmm_vma_walk->fault) |
| 388 | return; |
| 389 | |
| 390 | /* We aren't ask to do anything ... */ |
Jérôme Glisse | f88a1e9 | 2018-04-10 16:29:06 -0700 | [diff] [blame^] | 391 | if (!(pfns & range->flags[HMM_PFN_VALID])) |
Jérôme Glisse | 2aee09d | 2018-04-10 16:29:02 -0700 | [diff] [blame] | 392 | return; |
Jérôme Glisse | f88a1e9 | 2018-04-10 16:29:06 -0700 | [diff] [blame^] | 393 | /* If this is device memory than only fault if explicitly requested */ |
| 394 | if ((cpu_flags & range->flags[HMM_PFN_DEVICE_PRIVATE])) { |
| 395 | /* Do we fault on device memory ? */ |
| 396 | if (pfns & range->flags[HMM_PFN_DEVICE_PRIVATE]) { |
| 397 | *write_fault = pfns & range->flags[HMM_PFN_WRITE]; |
| 398 | *fault = true; |
| 399 | } |
Jérôme Glisse | 2aee09d | 2018-04-10 16:29:02 -0700 | [diff] [blame] | 400 | return; |
| 401 | } |
Jérôme Glisse | f88a1e9 | 2018-04-10 16:29:06 -0700 | [diff] [blame^] | 402 | |
| 403 | /* If CPU page table is not valid then we need to fault */ |
| 404 | *fault = !(cpu_flags & range->flags[HMM_PFN_VALID]); |
| 405 | /* Need to write fault ? */ |
| 406 | if ((pfns & range->flags[HMM_PFN_WRITE]) && |
| 407 | !(cpu_flags & range->flags[HMM_PFN_WRITE])) { |
| 408 | *write_fault = true; |
Jérôme Glisse | 2aee09d | 2018-04-10 16:29:02 -0700 | [diff] [blame] | 409 | *fault = true; |
| 410 | } |
| 411 | } |
| 412 | |
| 413 | static void hmm_range_need_fault(const struct hmm_vma_walk *hmm_vma_walk, |
| 414 | const uint64_t *pfns, unsigned long npages, |
| 415 | uint64_t cpu_flags, bool *fault, |
| 416 | bool *write_fault) |
| 417 | { |
| 418 | unsigned long i; |
| 419 | |
| 420 | if (!hmm_vma_walk->fault) { |
| 421 | *fault = *write_fault = false; |
| 422 | return; |
| 423 | } |
| 424 | |
| 425 | for (i = 0; i < npages; ++i) { |
| 426 | hmm_pte_need_fault(hmm_vma_walk, pfns[i], cpu_flags, |
| 427 | fault, write_fault); |
| 428 | if ((*fault) || (*write_fault)) |
| 429 | return; |
| 430 | } |
| 431 | } |
| 432 | |
| 433 | static int hmm_vma_walk_hole(unsigned long addr, unsigned long end, |
| 434 | struct mm_walk *walk) |
| 435 | { |
| 436 | struct hmm_vma_walk *hmm_vma_walk = walk->private; |
| 437 | struct hmm_range *range = hmm_vma_walk->range; |
| 438 | bool fault, write_fault; |
| 439 | unsigned long i, npages; |
| 440 | uint64_t *pfns; |
| 441 | |
| 442 | i = (addr - range->start) >> PAGE_SHIFT; |
| 443 | npages = (end - addr) >> PAGE_SHIFT; |
| 444 | pfns = &range->pfns[i]; |
| 445 | hmm_range_need_fault(hmm_vma_walk, pfns, npages, |
| 446 | 0, &fault, &write_fault); |
| 447 | return hmm_vma_walk_hole_(addr, end, fault, write_fault, walk); |
| 448 | } |
| 449 | |
Jérôme Glisse | f88a1e9 | 2018-04-10 16:29:06 -0700 | [diff] [blame^] | 450 | static inline uint64_t pmd_to_hmm_pfn_flags(struct hmm_range *range, pmd_t pmd) |
Jérôme Glisse | 2aee09d | 2018-04-10 16:29:02 -0700 | [diff] [blame] | 451 | { |
| 452 | if (pmd_protnone(pmd)) |
| 453 | return 0; |
Jérôme Glisse | f88a1e9 | 2018-04-10 16:29:06 -0700 | [diff] [blame^] | 454 | return pmd_write(pmd) ? range->flags[HMM_PFN_VALID] | |
| 455 | range->flags[HMM_PFN_WRITE] : |
| 456 | range->flags[HMM_PFN_VALID]; |
Jérôme Glisse | da4c3c7 | 2017-09-08 16:11:31 -0700 | [diff] [blame] | 457 | } |
| 458 | |
Jérôme Glisse | 53f5c3f | 2018-04-10 16:28:59 -0700 | [diff] [blame] | 459 | static int hmm_vma_handle_pmd(struct mm_walk *walk, |
| 460 | unsigned long addr, |
| 461 | unsigned long end, |
| 462 | uint64_t *pfns, |
| 463 | pmd_t pmd) |
| 464 | { |
| 465 | struct hmm_vma_walk *hmm_vma_walk = walk->private; |
Jérôme Glisse | f88a1e9 | 2018-04-10 16:29:06 -0700 | [diff] [blame^] | 466 | struct hmm_range *range = hmm_vma_walk->range; |
Jérôme Glisse | 2aee09d | 2018-04-10 16:29:02 -0700 | [diff] [blame] | 467 | unsigned long pfn, npages, i; |
Jérôme Glisse | 2aee09d | 2018-04-10 16:29:02 -0700 | [diff] [blame] | 468 | bool fault, write_fault; |
Jérôme Glisse | f88a1e9 | 2018-04-10 16:29:06 -0700 | [diff] [blame^] | 469 | uint64_t cpu_flags; |
Jérôme Glisse | 53f5c3f | 2018-04-10 16:28:59 -0700 | [diff] [blame] | 470 | |
Jérôme Glisse | 2aee09d | 2018-04-10 16:29:02 -0700 | [diff] [blame] | 471 | npages = (end - addr) >> PAGE_SHIFT; |
Jérôme Glisse | f88a1e9 | 2018-04-10 16:29:06 -0700 | [diff] [blame^] | 472 | cpu_flags = pmd_to_hmm_pfn_flags(range, pmd); |
Jérôme Glisse | 2aee09d | 2018-04-10 16:29:02 -0700 | [diff] [blame] | 473 | hmm_range_need_fault(hmm_vma_walk, pfns, npages, cpu_flags, |
| 474 | &fault, &write_fault); |
Jérôme Glisse | 53f5c3f | 2018-04-10 16:28:59 -0700 | [diff] [blame] | 475 | |
Jérôme Glisse | 2aee09d | 2018-04-10 16:29:02 -0700 | [diff] [blame] | 476 | if (pmd_protnone(pmd) || fault || write_fault) |
| 477 | return hmm_vma_walk_hole_(addr, end, fault, write_fault, walk); |
Jérôme Glisse | 53f5c3f | 2018-04-10 16:28:59 -0700 | [diff] [blame] | 478 | |
| 479 | pfn = pmd_pfn(pmd) + pte_index(addr); |
Jérôme Glisse | 53f5c3f | 2018-04-10 16:28:59 -0700 | [diff] [blame] | 480 | for (i = 0; addr < end; addr += PAGE_SIZE, i++, pfn++) |
Jérôme Glisse | f88a1e9 | 2018-04-10 16:29:06 -0700 | [diff] [blame^] | 481 | pfns[i] = hmm_pfn_from_pfn(range, pfn) | cpu_flags; |
Jérôme Glisse | 53f5c3f | 2018-04-10 16:28:59 -0700 | [diff] [blame] | 482 | hmm_vma_walk->last = end; |
| 483 | return 0; |
| 484 | } |
| 485 | |
Jérôme Glisse | f88a1e9 | 2018-04-10 16:29:06 -0700 | [diff] [blame^] | 486 | static inline uint64_t pte_to_hmm_pfn_flags(struct hmm_range *range, pte_t pte) |
Jérôme Glisse | 2aee09d | 2018-04-10 16:29:02 -0700 | [diff] [blame] | 487 | { |
| 488 | if (pte_none(pte) || !pte_present(pte)) |
| 489 | return 0; |
Jérôme Glisse | f88a1e9 | 2018-04-10 16:29:06 -0700 | [diff] [blame^] | 490 | return pte_write(pte) ? range->flags[HMM_PFN_VALID] | |
| 491 | range->flags[HMM_PFN_WRITE] : |
| 492 | range->flags[HMM_PFN_VALID]; |
Jérôme Glisse | 2aee09d | 2018-04-10 16:29:02 -0700 | [diff] [blame] | 493 | } |
| 494 | |
Jérôme Glisse | 53f5c3f | 2018-04-10 16:28:59 -0700 | [diff] [blame] | 495 | static int hmm_vma_handle_pte(struct mm_walk *walk, unsigned long addr, |
| 496 | unsigned long end, pmd_t *pmdp, pte_t *ptep, |
| 497 | uint64_t *pfn) |
| 498 | { |
| 499 | struct hmm_vma_walk *hmm_vma_walk = walk->private; |
Jérôme Glisse | f88a1e9 | 2018-04-10 16:29:06 -0700 | [diff] [blame^] | 500 | struct hmm_range *range = hmm_vma_walk->range; |
Jérôme Glisse | 53f5c3f | 2018-04-10 16:28:59 -0700 | [diff] [blame] | 501 | struct vm_area_struct *vma = walk->vma; |
Jérôme Glisse | 2aee09d | 2018-04-10 16:29:02 -0700 | [diff] [blame] | 502 | bool fault, write_fault; |
| 503 | uint64_t cpu_flags; |
Jérôme Glisse | 53f5c3f | 2018-04-10 16:28:59 -0700 | [diff] [blame] | 504 | pte_t pte = *ptep; |
Jérôme Glisse | f88a1e9 | 2018-04-10 16:29:06 -0700 | [diff] [blame^] | 505 | uint64_t orig_pfn = *pfn; |
Jérôme Glisse | 53f5c3f | 2018-04-10 16:28:59 -0700 | [diff] [blame] | 506 | |
Jérôme Glisse | f88a1e9 | 2018-04-10 16:29:06 -0700 | [diff] [blame^] | 507 | *pfn = range->values[HMM_PFN_NONE]; |
| 508 | cpu_flags = pte_to_hmm_pfn_flags(range, pte); |
| 509 | hmm_pte_need_fault(hmm_vma_walk, orig_pfn, cpu_flags, |
Jérôme Glisse | 2aee09d | 2018-04-10 16:29:02 -0700 | [diff] [blame] | 510 | &fault, &write_fault); |
Jérôme Glisse | 53f5c3f | 2018-04-10 16:28:59 -0700 | [diff] [blame] | 511 | |
| 512 | if (pte_none(pte)) { |
Jérôme Glisse | 2aee09d | 2018-04-10 16:29:02 -0700 | [diff] [blame] | 513 | if (fault || write_fault) |
Jérôme Glisse | 53f5c3f | 2018-04-10 16:28:59 -0700 | [diff] [blame] | 514 | goto fault; |
| 515 | return 0; |
| 516 | } |
| 517 | |
| 518 | if (!pte_present(pte)) { |
| 519 | swp_entry_t entry = pte_to_swp_entry(pte); |
| 520 | |
| 521 | if (!non_swap_entry(entry)) { |
Jérôme Glisse | 2aee09d | 2018-04-10 16:29:02 -0700 | [diff] [blame] | 522 | if (fault || write_fault) |
Jérôme Glisse | 53f5c3f | 2018-04-10 16:28:59 -0700 | [diff] [blame] | 523 | goto fault; |
| 524 | return 0; |
| 525 | } |
| 526 | |
| 527 | /* |
| 528 | * This is a special swap entry, ignore migration, use |
| 529 | * device and report anything else as error. |
| 530 | */ |
| 531 | if (is_device_private_entry(entry)) { |
Jérôme Glisse | f88a1e9 | 2018-04-10 16:29:06 -0700 | [diff] [blame^] | 532 | cpu_flags = range->flags[HMM_PFN_VALID] | |
| 533 | range->flags[HMM_PFN_DEVICE_PRIVATE]; |
Jérôme Glisse | 2aee09d | 2018-04-10 16:29:02 -0700 | [diff] [blame] | 534 | cpu_flags |= is_write_device_private_entry(entry) ? |
Jérôme Glisse | f88a1e9 | 2018-04-10 16:29:06 -0700 | [diff] [blame^] | 535 | range->flags[HMM_PFN_WRITE] : 0; |
| 536 | hmm_pte_need_fault(hmm_vma_walk, orig_pfn, cpu_flags, |
| 537 | &fault, &write_fault); |
| 538 | if (fault || write_fault) |
| 539 | goto fault; |
| 540 | *pfn = hmm_pfn_from_pfn(range, swp_offset(entry)); |
| 541 | *pfn |= cpu_flags; |
Jérôme Glisse | 53f5c3f | 2018-04-10 16:28:59 -0700 | [diff] [blame] | 542 | return 0; |
| 543 | } |
| 544 | |
| 545 | if (is_migration_entry(entry)) { |
Jérôme Glisse | 2aee09d | 2018-04-10 16:29:02 -0700 | [diff] [blame] | 546 | if (fault || write_fault) { |
Jérôme Glisse | 53f5c3f | 2018-04-10 16:28:59 -0700 | [diff] [blame] | 547 | pte_unmap(ptep); |
| 548 | hmm_vma_walk->last = addr; |
| 549 | migration_entry_wait(vma->vm_mm, |
Jérôme Glisse | 2aee09d | 2018-04-10 16:29:02 -0700 | [diff] [blame] | 550 | pmdp, addr); |
Jérôme Glisse | 53f5c3f | 2018-04-10 16:28:59 -0700 | [diff] [blame] | 551 | return -EAGAIN; |
| 552 | } |
| 553 | return 0; |
| 554 | } |
| 555 | |
| 556 | /* Report error for everything else */ |
Jérôme Glisse | f88a1e9 | 2018-04-10 16:29:06 -0700 | [diff] [blame^] | 557 | *pfn = range->values[HMM_PFN_ERROR]; |
Jérôme Glisse | 53f5c3f | 2018-04-10 16:28:59 -0700 | [diff] [blame] | 558 | return -EFAULT; |
| 559 | } |
| 560 | |
Jérôme Glisse | 2aee09d | 2018-04-10 16:29:02 -0700 | [diff] [blame] | 561 | if (fault || write_fault) |
Jérôme Glisse | 53f5c3f | 2018-04-10 16:28:59 -0700 | [diff] [blame] | 562 | goto fault; |
| 563 | |
Jérôme Glisse | f88a1e9 | 2018-04-10 16:29:06 -0700 | [diff] [blame^] | 564 | *pfn = hmm_pfn_from_pfn(range, pte_pfn(pte)) | cpu_flags; |
Jérôme Glisse | 53f5c3f | 2018-04-10 16:28:59 -0700 | [diff] [blame] | 565 | return 0; |
| 566 | |
| 567 | fault: |
| 568 | pte_unmap(ptep); |
| 569 | /* Fault any virtual address we were asked to fault */ |
Jérôme Glisse | 2aee09d | 2018-04-10 16:29:02 -0700 | [diff] [blame] | 570 | return hmm_vma_walk_hole_(addr, end, fault, write_fault, walk); |
Jérôme Glisse | 53f5c3f | 2018-04-10 16:28:59 -0700 | [diff] [blame] | 571 | } |
| 572 | |
Jérôme Glisse | da4c3c7 | 2017-09-08 16:11:31 -0700 | [diff] [blame] | 573 | static int hmm_vma_walk_pmd(pmd_t *pmdp, |
| 574 | unsigned long start, |
| 575 | unsigned long end, |
| 576 | struct mm_walk *walk) |
| 577 | { |
Jérôme Glisse | 74eee18 | 2017-09-08 16:11:35 -0700 | [diff] [blame] | 578 | struct hmm_vma_walk *hmm_vma_walk = walk->private; |
| 579 | struct hmm_range *range = hmm_vma_walk->range; |
Jérôme Glisse | ff05c0c | 2018-04-10 16:28:38 -0700 | [diff] [blame] | 580 | uint64_t *pfns = range->pfns; |
Jérôme Glisse | da4c3c7 | 2017-09-08 16:11:31 -0700 | [diff] [blame] | 581 | unsigned long addr = start, i; |
Jérôme Glisse | da4c3c7 | 2017-09-08 16:11:31 -0700 | [diff] [blame] | 582 | pte_t *ptep; |
| 583 | |
| 584 | i = (addr - range->start) >> PAGE_SHIFT; |
Jérôme Glisse | da4c3c7 | 2017-09-08 16:11:31 -0700 | [diff] [blame] | 585 | |
| 586 | again: |
| 587 | if (pmd_none(*pmdp)) |
| 588 | return hmm_vma_walk_hole(start, end, walk); |
| 589 | |
Jérôme Glisse | 53f5c3f | 2018-04-10 16:28:59 -0700 | [diff] [blame] | 590 | if (pmd_huge(*pmdp) && (range->vma->vm_flags & VM_HUGETLB)) |
Jérôme Glisse | da4c3c7 | 2017-09-08 16:11:31 -0700 | [diff] [blame] | 591 | return hmm_pfns_bad(start, end, walk); |
| 592 | |
| 593 | if (pmd_devmap(*pmdp) || pmd_trans_huge(*pmdp)) { |
Jérôme Glisse | da4c3c7 | 2017-09-08 16:11:31 -0700 | [diff] [blame] | 594 | pmd_t pmd; |
| 595 | |
| 596 | /* |
| 597 | * No need to take pmd_lock here, even if some other threads |
| 598 | * is splitting the huge pmd we will get that event through |
| 599 | * mmu_notifier callback. |
| 600 | * |
| 601 | * So just read pmd value and check again its a transparent |
| 602 | * huge or device mapping one and compute corresponding pfn |
| 603 | * values. |
| 604 | */ |
| 605 | pmd = pmd_read_atomic(pmdp); |
| 606 | barrier(); |
| 607 | if (!pmd_devmap(pmd) && !pmd_trans_huge(pmd)) |
| 608 | goto again; |
Jérôme Glisse | da4c3c7 | 2017-09-08 16:11:31 -0700 | [diff] [blame] | 609 | |
Jérôme Glisse | 53f5c3f | 2018-04-10 16:28:59 -0700 | [diff] [blame] | 610 | return hmm_vma_handle_pmd(walk, addr, end, &pfns[i], pmd); |
Jérôme Glisse | da4c3c7 | 2017-09-08 16:11:31 -0700 | [diff] [blame] | 611 | } |
| 612 | |
| 613 | if (pmd_bad(*pmdp)) |
| 614 | return hmm_pfns_bad(start, end, walk); |
| 615 | |
| 616 | ptep = pte_offset_map(pmdp, addr); |
| 617 | for (; addr < end; addr += PAGE_SIZE, ptep++, i++) { |
Jérôme Glisse | 53f5c3f | 2018-04-10 16:28:59 -0700 | [diff] [blame] | 618 | int r; |
Jérôme Glisse | da4c3c7 | 2017-09-08 16:11:31 -0700 | [diff] [blame] | 619 | |
Jérôme Glisse | 53f5c3f | 2018-04-10 16:28:59 -0700 | [diff] [blame] | 620 | r = hmm_vma_handle_pte(walk, addr, end, pmdp, ptep, &pfns[i]); |
| 621 | if (r) { |
| 622 | /* hmm_vma_handle_pte() did unmap pte directory */ |
| 623 | hmm_vma_walk->last = addr; |
| 624 | return r; |
Jérôme Glisse | da4c3c7 | 2017-09-08 16:11:31 -0700 | [diff] [blame] | 625 | } |
Jérôme Glisse | da4c3c7 | 2017-09-08 16:11:31 -0700 | [diff] [blame] | 626 | } |
| 627 | pte_unmap(ptep - 1); |
| 628 | |
Jérôme Glisse | 53f5c3f | 2018-04-10 16:28:59 -0700 | [diff] [blame] | 629 | hmm_vma_walk->last = addr; |
Jérôme Glisse | da4c3c7 | 2017-09-08 16:11:31 -0700 | [diff] [blame] | 630 | return 0; |
| 631 | } |
| 632 | |
Jérôme Glisse | f88a1e9 | 2018-04-10 16:29:06 -0700 | [diff] [blame^] | 633 | static void hmm_pfns_clear(struct hmm_range *range, |
| 634 | uint64_t *pfns, |
Jérôme Glisse | 33cd47d | 2018-04-10 16:28:54 -0700 | [diff] [blame] | 635 | unsigned long addr, |
| 636 | unsigned long end) |
| 637 | { |
| 638 | for (; addr < end; addr += PAGE_SIZE, pfns++) |
Jérôme Glisse | f88a1e9 | 2018-04-10 16:29:06 -0700 | [diff] [blame^] | 639 | *pfns = range->values[HMM_PFN_NONE]; |
Jérôme Glisse | 33cd47d | 2018-04-10 16:28:54 -0700 | [diff] [blame] | 640 | } |
| 641 | |
Jérôme Glisse | 855ce7d | 2018-04-10 16:28:42 -0700 | [diff] [blame] | 642 | static void hmm_pfns_special(struct hmm_range *range) |
| 643 | { |
| 644 | unsigned long addr = range->start, i = 0; |
| 645 | |
| 646 | for (; addr < range->end; addr += PAGE_SIZE, i++) |
Jérôme Glisse | f88a1e9 | 2018-04-10 16:29:06 -0700 | [diff] [blame^] | 647 | range->pfns[i] = range->values[HMM_PFN_SPECIAL]; |
Jérôme Glisse | 855ce7d | 2018-04-10 16:28:42 -0700 | [diff] [blame] | 648 | } |
| 649 | |
Jérôme Glisse | da4c3c7 | 2017-09-08 16:11:31 -0700 | [diff] [blame] | 650 | /* |
| 651 | * hmm_vma_get_pfns() - snapshot CPU page table for a range of virtual addresses |
Jérôme Glisse | 08232a4 | 2018-04-10 16:28:30 -0700 | [diff] [blame] | 652 | * @range: range being snapshotted |
Jérôme Glisse | 86586a4 | 2018-04-10 16:28:34 -0700 | [diff] [blame] | 653 | * Returns: -EINVAL if invalid argument, -ENOMEM out of memory, -EPERM invalid |
| 654 | * vma permission, 0 success |
Jérôme Glisse | da4c3c7 | 2017-09-08 16:11:31 -0700 | [diff] [blame] | 655 | * |
| 656 | * This snapshots the CPU page table for a range of virtual addresses. Snapshot |
| 657 | * validity is tracked by range struct. See hmm_vma_range_done() for further |
| 658 | * information. |
| 659 | * |
| 660 | * The range struct is initialized here. It tracks the CPU page table, but only |
| 661 | * if the function returns success (0), in which case the caller must then call |
| 662 | * hmm_vma_range_done() to stop CPU page table update tracking on this range. |
| 663 | * |
| 664 | * NOT CALLING hmm_vma_range_done() IF FUNCTION RETURNS 0 WILL LEAD TO SERIOUS |
| 665 | * MEMORY CORRUPTION ! YOU HAVE BEEN WARNED ! |
| 666 | */ |
Jérôme Glisse | 08232a4 | 2018-04-10 16:28:30 -0700 | [diff] [blame] | 667 | int hmm_vma_get_pfns(struct hmm_range *range) |
Jérôme Glisse | da4c3c7 | 2017-09-08 16:11:31 -0700 | [diff] [blame] | 668 | { |
Jérôme Glisse | 08232a4 | 2018-04-10 16:28:30 -0700 | [diff] [blame] | 669 | struct vm_area_struct *vma = range->vma; |
Jérôme Glisse | 74eee18 | 2017-09-08 16:11:35 -0700 | [diff] [blame] | 670 | struct hmm_vma_walk hmm_vma_walk; |
Jérôme Glisse | da4c3c7 | 2017-09-08 16:11:31 -0700 | [diff] [blame] | 671 | struct mm_walk mm_walk; |
| 672 | struct hmm *hmm; |
| 673 | |
Jérôme Glisse | da4c3c7 | 2017-09-08 16:11:31 -0700 | [diff] [blame] | 674 | /* Sanity check, this really should not happen ! */ |
Jérôme Glisse | 08232a4 | 2018-04-10 16:28:30 -0700 | [diff] [blame] | 675 | if (range->start < vma->vm_start || range->start >= vma->vm_end) |
Jérôme Glisse | da4c3c7 | 2017-09-08 16:11:31 -0700 | [diff] [blame] | 676 | return -EINVAL; |
Jérôme Glisse | 08232a4 | 2018-04-10 16:28:30 -0700 | [diff] [blame] | 677 | if (range->end < vma->vm_start || range->end > vma->vm_end) |
Jérôme Glisse | da4c3c7 | 2017-09-08 16:11:31 -0700 | [diff] [blame] | 678 | return -EINVAL; |
| 679 | |
| 680 | hmm = hmm_register(vma->vm_mm); |
| 681 | if (!hmm) |
| 682 | return -ENOMEM; |
| 683 | /* Caller must have registered a mirror, via hmm_mirror_register() ! */ |
| 684 | if (!hmm->mmu_notifier.ops) |
| 685 | return -EINVAL; |
| 686 | |
Jérôme Glisse | 855ce7d | 2018-04-10 16:28:42 -0700 | [diff] [blame] | 687 | /* FIXME support hugetlb fs */ |
| 688 | if (is_vm_hugetlb_page(vma) || (vma->vm_flags & VM_SPECIAL)) { |
| 689 | hmm_pfns_special(range); |
| 690 | return -EINVAL; |
| 691 | } |
| 692 | |
Jérôme Glisse | 86586a4 | 2018-04-10 16:28:34 -0700 | [diff] [blame] | 693 | if (!(vma->vm_flags & VM_READ)) { |
| 694 | /* |
| 695 | * If vma do not allow read access, then assume that it does |
| 696 | * not allow write access, either. Architecture that allow |
| 697 | * write without read access are not supported by HMM, because |
| 698 | * operations such has atomic access would not work. |
| 699 | */ |
Jérôme Glisse | f88a1e9 | 2018-04-10 16:29:06 -0700 | [diff] [blame^] | 700 | hmm_pfns_clear(range, range->pfns, range->start, range->end); |
Jérôme Glisse | 86586a4 | 2018-04-10 16:28:34 -0700 | [diff] [blame] | 701 | return -EPERM; |
| 702 | } |
| 703 | |
Jérôme Glisse | da4c3c7 | 2017-09-08 16:11:31 -0700 | [diff] [blame] | 704 | /* Initialize range to track CPU page table update */ |
Jérôme Glisse | da4c3c7 | 2017-09-08 16:11:31 -0700 | [diff] [blame] | 705 | spin_lock(&hmm->lock); |
| 706 | range->valid = true; |
| 707 | list_add_rcu(&range->list, &hmm->ranges); |
| 708 | spin_unlock(&hmm->lock); |
| 709 | |
Jérôme Glisse | 74eee18 | 2017-09-08 16:11:35 -0700 | [diff] [blame] | 710 | hmm_vma_walk.fault = false; |
| 711 | hmm_vma_walk.range = range; |
| 712 | mm_walk.private = &hmm_vma_walk; |
| 713 | |
Jérôme Glisse | da4c3c7 | 2017-09-08 16:11:31 -0700 | [diff] [blame] | 714 | mm_walk.vma = vma; |
| 715 | mm_walk.mm = vma->vm_mm; |
Jérôme Glisse | da4c3c7 | 2017-09-08 16:11:31 -0700 | [diff] [blame] | 716 | mm_walk.pte_entry = NULL; |
| 717 | mm_walk.test_walk = NULL; |
| 718 | mm_walk.hugetlb_entry = NULL; |
| 719 | mm_walk.pmd_entry = hmm_vma_walk_pmd; |
| 720 | mm_walk.pte_hole = hmm_vma_walk_hole; |
| 721 | |
Jérôme Glisse | 08232a4 | 2018-04-10 16:28:30 -0700 | [diff] [blame] | 722 | walk_page_range(range->start, range->end, &mm_walk); |
Jérôme Glisse | da4c3c7 | 2017-09-08 16:11:31 -0700 | [diff] [blame] | 723 | return 0; |
| 724 | } |
| 725 | EXPORT_SYMBOL(hmm_vma_get_pfns); |
| 726 | |
| 727 | /* |
| 728 | * hmm_vma_range_done() - stop tracking change to CPU page table over a range |
Jérôme Glisse | da4c3c7 | 2017-09-08 16:11:31 -0700 | [diff] [blame] | 729 | * @range: range being tracked |
| 730 | * Returns: false if range data has been invalidated, true otherwise |
| 731 | * |
| 732 | * Range struct is used to track updates to the CPU page table after a call to |
| 733 | * either hmm_vma_get_pfns() or hmm_vma_fault(). Once the device driver is done |
| 734 | * using the data, or wants to lock updates to the data it got from those |
| 735 | * functions, it must call the hmm_vma_range_done() function, which will then |
| 736 | * stop tracking CPU page table updates. |
| 737 | * |
| 738 | * Note that device driver must still implement general CPU page table update |
| 739 | * tracking either by using hmm_mirror (see hmm_mirror_register()) or by using |
| 740 | * the mmu_notifier API directly. |
| 741 | * |
| 742 | * CPU page table update tracking done through hmm_range is only temporary and |
| 743 | * to be used while trying to duplicate CPU page table contents for a range of |
| 744 | * virtual addresses. |
| 745 | * |
| 746 | * There are two ways to use this : |
| 747 | * again: |
Jérôme Glisse | 08232a4 | 2018-04-10 16:28:30 -0700 | [diff] [blame] | 748 | * hmm_vma_get_pfns(range); or hmm_vma_fault(...); |
Jérôme Glisse | da4c3c7 | 2017-09-08 16:11:31 -0700 | [diff] [blame] | 749 | * trans = device_build_page_table_update_transaction(pfns); |
| 750 | * device_page_table_lock(); |
Jérôme Glisse | 08232a4 | 2018-04-10 16:28:30 -0700 | [diff] [blame] | 751 | * if (!hmm_vma_range_done(range)) { |
Jérôme Glisse | da4c3c7 | 2017-09-08 16:11:31 -0700 | [diff] [blame] | 752 | * device_page_table_unlock(); |
| 753 | * goto again; |
| 754 | * } |
| 755 | * device_commit_transaction(trans); |
| 756 | * device_page_table_unlock(); |
| 757 | * |
| 758 | * Or: |
Jérôme Glisse | 08232a4 | 2018-04-10 16:28:30 -0700 | [diff] [blame] | 759 | * hmm_vma_get_pfns(range); or hmm_vma_fault(...); |
Jérôme Glisse | da4c3c7 | 2017-09-08 16:11:31 -0700 | [diff] [blame] | 760 | * device_page_table_lock(); |
Jérôme Glisse | 08232a4 | 2018-04-10 16:28:30 -0700 | [diff] [blame] | 761 | * hmm_vma_range_done(range); |
| 762 | * device_update_page_table(range->pfns); |
Jérôme Glisse | da4c3c7 | 2017-09-08 16:11:31 -0700 | [diff] [blame] | 763 | * device_page_table_unlock(); |
| 764 | */ |
Jérôme Glisse | 08232a4 | 2018-04-10 16:28:30 -0700 | [diff] [blame] | 765 | bool hmm_vma_range_done(struct hmm_range *range) |
Jérôme Glisse | da4c3c7 | 2017-09-08 16:11:31 -0700 | [diff] [blame] | 766 | { |
| 767 | unsigned long npages = (range->end - range->start) >> PAGE_SHIFT; |
| 768 | struct hmm *hmm; |
| 769 | |
| 770 | if (range->end <= range->start) { |
| 771 | BUG(); |
| 772 | return false; |
| 773 | } |
| 774 | |
Jérôme Glisse | 08232a4 | 2018-04-10 16:28:30 -0700 | [diff] [blame] | 775 | hmm = hmm_register(range->vma->vm_mm); |
Jérôme Glisse | da4c3c7 | 2017-09-08 16:11:31 -0700 | [diff] [blame] | 776 | if (!hmm) { |
| 777 | memset(range->pfns, 0, sizeof(*range->pfns) * npages); |
| 778 | return false; |
| 779 | } |
| 780 | |
| 781 | spin_lock(&hmm->lock); |
| 782 | list_del_rcu(&range->list); |
| 783 | spin_unlock(&hmm->lock); |
| 784 | |
| 785 | return range->valid; |
| 786 | } |
| 787 | EXPORT_SYMBOL(hmm_vma_range_done); |
Jérôme Glisse | 74eee18 | 2017-09-08 16:11:35 -0700 | [diff] [blame] | 788 | |
| 789 | /* |
| 790 | * hmm_vma_fault() - try to fault some address in a virtual address range |
Jérôme Glisse | 08232a4 | 2018-04-10 16:28:30 -0700 | [diff] [blame] | 791 | * @range: range being faulted |
Jérôme Glisse | 74eee18 | 2017-09-08 16:11:35 -0700 | [diff] [blame] | 792 | * @block: allow blocking on fault (if true it sleeps and do not drop mmap_sem) |
| 793 | * Returns: 0 success, error otherwise (-EAGAIN means mmap_sem have been drop) |
| 794 | * |
| 795 | * This is similar to a regular CPU page fault except that it will not trigger |
| 796 | * any memory migration if the memory being faulted is not accessible by CPUs. |
| 797 | * |
Jérôme Glisse | ff05c0c | 2018-04-10 16:28:38 -0700 | [diff] [blame] | 798 | * On error, for one virtual address in the range, the function will mark the |
| 799 | * corresponding HMM pfn entry with an error flag. |
Jérôme Glisse | 74eee18 | 2017-09-08 16:11:35 -0700 | [diff] [blame] | 800 | * |
| 801 | * Expected use pattern: |
| 802 | * retry: |
| 803 | * down_read(&mm->mmap_sem); |
| 804 | * // Find vma and address device wants to fault, initialize hmm_pfn_t |
| 805 | * // array accordingly |
Jérôme Glisse | 08232a4 | 2018-04-10 16:28:30 -0700 | [diff] [blame] | 806 | * ret = hmm_vma_fault(range, write, block); |
Jérôme Glisse | 74eee18 | 2017-09-08 16:11:35 -0700 | [diff] [blame] | 807 | * switch (ret) { |
| 808 | * case -EAGAIN: |
Jérôme Glisse | 08232a4 | 2018-04-10 16:28:30 -0700 | [diff] [blame] | 809 | * hmm_vma_range_done(range); |
Jérôme Glisse | 74eee18 | 2017-09-08 16:11:35 -0700 | [diff] [blame] | 810 | * // You might want to rate limit or yield to play nicely, you may |
| 811 | * // also commit any valid pfn in the array assuming that you are |
| 812 | * // getting true from hmm_vma_range_monitor_end() |
| 813 | * goto retry; |
| 814 | * case 0: |
| 815 | * break; |
Jérôme Glisse | 86586a4 | 2018-04-10 16:28:34 -0700 | [diff] [blame] | 816 | * case -ENOMEM: |
| 817 | * case -EINVAL: |
| 818 | * case -EPERM: |
Jérôme Glisse | 74eee18 | 2017-09-08 16:11:35 -0700 | [diff] [blame] | 819 | * default: |
| 820 | * // Handle error ! |
| 821 | * up_read(&mm->mmap_sem) |
| 822 | * return; |
| 823 | * } |
| 824 | * // Take device driver lock that serialize device page table update |
| 825 | * driver_lock_device_page_table_update(); |
Jérôme Glisse | 08232a4 | 2018-04-10 16:28:30 -0700 | [diff] [blame] | 826 | * hmm_vma_range_done(range); |
Jérôme Glisse | 74eee18 | 2017-09-08 16:11:35 -0700 | [diff] [blame] | 827 | * // Commit pfns we got from hmm_vma_fault() |
| 828 | * driver_unlock_device_page_table_update(); |
| 829 | * up_read(&mm->mmap_sem) |
| 830 | * |
| 831 | * YOU MUST CALL hmm_vma_range_done() AFTER THIS FUNCTION RETURN SUCCESS (0) |
| 832 | * BEFORE FREEING THE range struct OR YOU WILL HAVE SERIOUS MEMORY CORRUPTION ! |
| 833 | * |
| 834 | * YOU HAVE BEEN WARNED ! |
| 835 | */ |
Jérôme Glisse | 2aee09d | 2018-04-10 16:29:02 -0700 | [diff] [blame] | 836 | int hmm_vma_fault(struct hmm_range *range, bool block) |
Jérôme Glisse | 74eee18 | 2017-09-08 16:11:35 -0700 | [diff] [blame] | 837 | { |
Jérôme Glisse | 08232a4 | 2018-04-10 16:28:30 -0700 | [diff] [blame] | 838 | struct vm_area_struct *vma = range->vma; |
| 839 | unsigned long start = range->start; |
Jérôme Glisse | 74eee18 | 2017-09-08 16:11:35 -0700 | [diff] [blame] | 840 | struct hmm_vma_walk hmm_vma_walk; |
| 841 | struct mm_walk mm_walk; |
| 842 | struct hmm *hmm; |
| 843 | int ret; |
| 844 | |
| 845 | /* Sanity check, this really should not happen ! */ |
Jérôme Glisse | 08232a4 | 2018-04-10 16:28:30 -0700 | [diff] [blame] | 846 | if (range->start < vma->vm_start || range->start >= vma->vm_end) |
Jérôme Glisse | 74eee18 | 2017-09-08 16:11:35 -0700 | [diff] [blame] | 847 | return -EINVAL; |
Jérôme Glisse | 08232a4 | 2018-04-10 16:28:30 -0700 | [diff] [blame] | 848 | if (range->end < vma->vm_start || range->end > vma->vm_end) |
Jérôme Glisse | 74eee18 | 2017-09-08 16:11:35 -0700 | [diff] [blame] | 849 | return -EINVAL; |
| 850 | |
| 851 | hmm = hmm_register(vma->vm_mm); |
| 852 | if (!hmm) { |
Jérôme Glisse | f88a1e9 | 2018-04-10 16:29:06 -0700 | [diff] [blame^] | 853 | hmm_pfns_clear(range, range->pfns, range->start, range->end); |
Jérôme Glisse | 74eee18 | 2017-09-08 16:11:35 -0700 | [diff] [blame] | 854 | return -ENOMEM; |
| 855 | } |
| 856 | /* Caller must have registered a mirror using hmm_mirror_register() */ |
| 857 | if (!hmm->mmu_notifier.ops) |
| 858 | return -EINVAL; |
| 859 | |
Jérôme Glisse | 855ce7d | 2018-04-10 16:28:42 -0700 | [diff] [blame] | 860 | /* FIXME support hugetlb fs */ |
| 861 | if (is_vm_hugetlb_page(vma) || (vma->vm_flags & VM_SPECIAL)) { |
| 862 | hmm_pfns_special(range); |
| 863 | return -EINVAL; |
| 864 | } |
| 865 | |
Jérôme Glisse | 86586a4 | 2018-04-10 16:28:34 -0700 | [diff] [blame] | 866 | if (!(vma->vm_flags & VM_READ)) { |
| 867 | /* |
| 868 | * If vma do not allow read access, then assume that it does |
| 869 | * not allow write access, either. Architecture that allow |
| 870 | * write without read access are not supported by HMM, because |
| 871 | * operations such has atomic access would not work. |
| 872 | */ |
Jérôme Glisse | f88a1e9 | 2018-04-10 16:29:06 -0700 | [diff] [blame^] | 873 | hmm_pfns_clear(range, range->pfns, range->start, range->end); |
Jérôme Glisse | 86586a4 | 2018-04-10 16:28:34 -0700 | [diff] [blame] | 874 | return -EPERM; |
| 875 | } |
Jérôme Glisse | 74eee18 | 2017-09-08 16:11:35 -0700 | [diff] [blame] | 876 | |
Jérôme Glisse | 86586a4 | 2018-04-10 16:28:34 -0700 | [diff] [blame] | 877 | /* Initialize range to track CPU page table update */ |
| 878 | spin_lock(&hmm->lock); |
| 879 | range->valid = true; |
| 880 | list_add_rcu(&range->list, &hmm->ranges); |
| 881 | spin_unlock(&hmm->lock); |
| 882 | |
Jérôme Glisse | 74eee18 | 2017-09-08 16:11:35 -0700 | [diff] [blame] | 883 | hmm_vma_walk.fault = true; |
Jérôme Glisse | 74eee18 | 2017-09-08 16:11:35 -0700 | [diff] [blame] | 884 | hmm_vma_walk.block = block; |
| 885 | hmm_vma_walk.range = range; |
| 886 | mm_walk.private = &hmm_vma_walk; |
| 887 | hmm_vma_walk.last = range->start; |
| 888 | |
| 889 | mm_walk.vma = vma; |
| 890 | mm_walk.mm = vma->vm_mm; |
| 891 | mm_walk.pte_entry = NULL; |
| 892 | mm_walk.test_walk = NULL; |
| 893 | mm_walk.hugetlb_entry = NULL; |
| 894 | mm_walk.pmd_entry = hmm_vma_walk_pmd; |
| 895 | mm_walk.pte_hole = hmm_vma_walk_hole; |
| 896 | |
| 897 | do { |
Jérôme Glisse | 08232a4 | 2018-04-10 16:28:30 -0700 | [diff] [blame] | 898 | ret = walk_page_range(start, range->end, &mm_walk); |
Jérôme Glisse | 74eee18 | 2017-09-08 16:11:35 -0700 | [diff] [blame] | 899 | start = hmm_vma_walk.last; |
| 900 | } while (ret == -EAGAIN); |
| 901 | |
| 902 | if (ret) { |
| 903 | unsigned long i; |
| 904 | |
| 905 | i = (hmm_vma_walk.last - range->start) >> PAGE_SHIFT; |
Jérôme Glisse | f88a1e9 | 2018-04-10 16:29:06 -0700 | [diff] [blame^] | 906 | hmm_pfns_clear(range, &range->pfns[i], hmm_vma_walk.last, |
| 907 | range->end); |
Jérôme Glisse | 08232a4 | 2018-04-10 16:28:30 -0700 | [diff] [blame] | 908 | hmm_vma_range_done(range); |
Jérôme Glisse | 74eee18 | 2017-09-08 16:11:35 -0700 | [diff] [blame] | 909 | } |
| 910 | return ret; |
| 911 | } |
| 912 | EXPORT_SYMBOL(hmm_vma_fault); |
Jérôme Glisse | c0b1240 | 2017-09-08 16:11:27 -0700 | [diff] [blame] | 913 | #endif /* IS_ENABLED(CONFIG_HMM_MIRROR) */ |
Jérôme Glisse | 4ef589d | 2017-09-08 16:11:58 -0700 | [diff] [blame] | 914 | |
| 915 | |
Jérôme Glisse | df6ad69 | 2017-09-08 16:12:24 -0700 | [diff] [blame] | 916 | #if IS_ENABLED(CONFIG_DEVICE_PRIVATE) || IS_ENABLED(CONFIG_DEVICE_PUBLIC) |
Jérôme Glisse | 4ef589d | 2017-09-08 16:11:58 -0700 | [diff] [blame] | 917 | struct page *hmm_vma_alloc_locked_page(struct vm_area_struct *vma, |
| 918 | unsigned long addr) |
| 919 | { |
| 920 | struct page *page; |
| 921 | |
| 922 | page = alloc_page_vma(GFP_HIGHUSER, vma, addr); |
| 923 | if (!page) |
| 924 | return NULL; |
| 925 | lock_page(page); |
| 926 | return page; |
| 927 | } |
| 928 | EXPORT_SYMBOL(hmm_vma_alloc_locked_page); |
| 929 | |
| 930 | |
| 931 | static void hmm_devmem_ref_release(struct percpu_ref *ref) |
| 932 | { |
| 933 | struct hmm_devmem *devmem; |
| 934 | |
| 935 | devmem = container_of(ref, struct hmm_devmem, ref); |
| 936 | complete(&devmem->completion); |
| 937 | } |
| 938 | |
| 939 | static void hmm_devmem_ref_exit(void *data) |
| 940 | { |
| 941 | struct percpu_ref *ref = data; |
| 942 | struct hmm_devmem *devmem; |
| 943 | |
| 944 | devmem = container_of(ref, struct hmm_devmem, ref); |
| 945 | percpu_ref_exit(ref); |
| 946 | devm_remove_action(devmem->device, &hmm_devmem_ref_exit, data); |
| 947 | } |
| 948 | |
| 949 | static void hmm_devmem_ref_kill(void *data) |
| 950 | { |
| 951 | struct percpu_ref *ref = data; |
| 952 | struct hmm_devmem *devmem; |
| 953 | |
| 954 | devmem = container_of(ref, struct hmm_devmem, ref); |
| 955 | percpu_ref_kill(ref); |
| 956 | wait_for_completion(&devmem->completion); |
| 957 | devm_remove_action(devmem->device, &hmm_devmem_ref_kill, data); |
| 958 | } |
| 959 | |
| 960 | static int hmm_devmem_fault(struct vm_area_struct *vma, |
| 961 | unsigned long addr, |
| 962 | const struct page *page, |
| 963 | unsigned int flags, |
| 964 | pmd_t *pmdp) |
| 965 | { |
| 966 | struct hmm_devmem *devmem = page->pgmap->data; |
| 967 | |
| 968 | return devmem->ops->fault(devmem, vma, addr, page, flags, pmdp); |
| 969 | } |
| 970 | |
| 971 | static void hmm_devmem_free(struct page *page, void *data) |
| 972 | { |
| 973 | struct hmm_devmem *devmem = data; |
| 974 | |
| 975 | devmem->ops->free(devmem, page); |
| 976 | } |
| 977 | |
| 978 | static DEFINE_MUTEX(hmm_devmem_lock); |
| 979 | static RADIX_TREE(hmm_devmem_radix, GFP_KERNEL); |
| 980 | |
| 981 | static void hmm_devmem_radix_release(struct resource *resource) |
| 982 | { |
Colin Ian King | fec11bc | 2017-11-15 17:38:52 -0800 | [diff] [blame] | 983 | resource_size_t key, align_start, align_size; |
Jérôme Glisse | 4ef589d | 2017-09-08 16:11:58 -0700 | [diff] [blame] | 984 | |
| 985 | align_start = resource->start & ~(PA_SECTION_SIZE - 1); |
| 986 | align_size = ALIGN(resource_size(resource), PA_SECTION_SIZE); |
Jérôme Glisse | 4ef589d | 2017-09-08 16:11:58 -0700 | [diff] [blame] | 987 | |
| 988 | mutex_lock(&hmm_devmem_lock); |
| 989 | for (key = resource->start; |
| 990 | key <= resource->end; |
| 991 | key += PA_SECTION_SIZE) |
| 992 | radix_tree_delete(&hmm_devmem_radix, key >> PA_SECTION_SHIFT); |
| 993 | mutex_unlock(&hmm_devmem_lock); |
| 994 | } |
| 995 | |
| 996 | static void hmm_devmem_release(struct device *dev, void *data) |
| 997 | { |
| 998 | struct hmm_devmem *devmem = data; |
| 999 | struct resource *resource = devmem->resource; |
| 1000 | unsigned long start_pfn, npages; |
| 1001 | struct zone *zone; |
| 1002 | struct page *page; |
| 1003 | |
| 1004 | if (percpu_ref_tryget_live(&devmem->ref)) { |
| 1005 | dev_WARN(dev, "%s: page mapping is still live!\n", __func__); |
| 1006 | percpu_ref_put(&devmem->ref); |
| 1007 | } |
| 1008 | |
| 1009 | /* pages are dead and unused, undo the arch mapping */ |
| 1010 | start_pfn = (resource->start & ~(PA_SECTION_SIZE - 1)) >> PAGE_SHIFT; |
| 1011 | npages = ALIGN(resource_size(resource), PA_SECTION_SIZE) >> PAGE_SHIFT; |
| 1012 | |
| 1013 | page = pfn_to_page(start_pfn); |
| 1014 | zone = page_zone(page); |
| 1015 | |
| 1016 | mem_hotplug_begin(); |
Jérôme Glisse | d3df0a4 | 2017-09-08 16:12:28 -0700 | [diff] [blame] | 1017 | if (resource->desc == IORES_DESC_DEVICE_PRIVATE_MEMORY) |
Christoph Hellwig | da02451 | 2017-12-29 08:53:55 +0100 | [diff] [blame] | 1018 | __remove_pages(zone, start_pfn, npages, NULL); |
Jérôme Glisse | d3df0a4 | 2017-09-08 16:12:28 -0700 | [diff] [blame] | 1019 | else |
| 1020 | arch_remove_memory(start_pfn << PAGE_SHIFT, |
Christoph Hellwig | da02451 | 2017-12-29 08:53:55 +0100 | [diff] [blame] | 1021 | npages << PAGE_SHIFT, NULL); |
Jérôme Glisse | 4ef589d | 2017-09-08 16:11:58 -0700 | [diff] [blame] | 1022 | mem_hotplug_done(); |
| 1023 | |
| 1024 | hmm_devmem_radix_release(resource); |
| 1025 | } |
| 1026 | |
| 1027 | static struct hmm_devmem *hmm_devmem_find(resource_size_t phys) |
| 1028 | { |
| 1029 | WARN_ON_ONCE(!rcu_read_lock_held()); |
| 1030 | |
| 1031 | return radix_tree_lookup(&hmm_devmem_radix, phys >> PA_SECTION_SHIFT); |
| 1032 | } |
| 1033 | |
| 1034 | static int hmm_devmem_pages_create(struct hmm_devmem *devmem) |
| 1035 | { |
| 1036 | resource_size_t key, align_start, align_size, align_end; |
| 1037 | struct device *device = devmem->device; |
| 1038 | int ret, nid, is_ram; |
| 1039 | unsigned long pfn; |
| 1040 | |
| 1041 | align_start = devmem->resource->start & ~(PA_SECTION_SIZE - 1); |
| 1042 | align_size = ALIGN(devmem->resource->start + |
| 1043 | resource_size(devmem->resource), |
| 1044 | PA_SECTION_SIZE) - align_start; |
| 1045 | |
| 1046 | is_ram = region_intersects(align_start, align_size, |
| 1047 | IORESOURCE_SYSTEM_RAM, |
| 1048 | IORES_DESC_NONE); |
| 1049 | if (is_ram == REGION_MIXED) { |
| 1050 | WARN_ONCE(1, "%s attempted on mixed region %pr\n", |
| 1051 | __func__, devmem->resource); |
| 1052 | return -ENXIO; |
| 1053 | } |
| 1054 | if (is_ram == REGION_INTERSECTS) |
| 1055 | return -ENXIO; |
| 1056 | |
Jérôme Glisse | d3df0a4 | 2017-09-08 16:12:28 -0700 | [diff] [blame] | 1057 | if (devmem->resource->desc == IORES_DESC_DEVICE_PUBLIC_MEMORY) |
| 1058 | devmem->pagemap.type = MEMORY_DEVICE_PUBLIC; |
| 1059 | else |
| 1060 | devmem->pagemap.type = MEMORY_DEVICE_PRIVATE; |
| 1061 | |
Logan Gunthorpe | e7744aa | 2017-12-29 08:54:04 +0100 | [diff] [blame] | 1062 | devmem->pagemap.res = *devmem->resource; |
Jérôme Glisse | 4ef589d | 2017-09-08 16:11:58 -0700 | [diff] [blame] | 1063 | devmem->pagemap.page_fault = hmm_devmem_fault; |
| 1064 | devmem->pagemap.page_free = hmm_devmem_free; |
| 1065 | devmem->pagemap.dev = devmem->device; |
| 1066 | devmem->pagemap.ref = &devmem->ref; |
| 1067 | devmem->pagemap.data = devmem; |
| 1068 | |
| 1069 | mutex_lock(&hmm_devmem_lock); |
| 1070 | align_end = align_start + align_size - 1; |
| 1071 | for (key = align_start; key <= align_end; key += PA_SECTION_SIZE) { |
| 1072 | struct hmm_devmem *dup; |
| 1073 | |
| 1074 | rcu_read_lock(); |
| 1075 | dup = hmm_devmem_find(key); |
| 1076 | rcu_read_unlock(); |
| 1077 | if (dup) { |
| 1078 | dev_err(device, "%s: collides with mapping for %s\n", |
| 1079 | __func__, dev_name(dup->device)); |
| 1080 | mutex_unlock(&hmm_devmem_lock); |
| 1081 | ret = -EBUSY; |
| 1082 | goto error; |
| 1083 | } |
| 1084 | ret = radix_tree_insert(&hmm_devmem_radix, |
| 1085 | key >> PA_SECTION_SHIFT, |
| 1086 | devmem); |
| 1087 | if (ret) { |
| 1088 | dev_err(device, "%s: failed: %d\n", __func__, ret); |
| 1089 | mutex_unlock(&hmm_devmem_lock); |
| 1090 | goto error_radix; |
| 1091 | } |
| 1092 | } |
| 1093 | mutex_unlock(&hmm_devmem_lock); |
| 1094 | |
| 1095 | nid = dev_to_node(device); |
| 1096 | if (nid < 0) |
| 1097 | nid = numa_mem_id(); |
| 1098 | |
| 1099 | mem_hotplug_begin(); |
| 1100 | /* |
| 1101 | * For device private memory we call add_pages() as we only need to |
| 1102 | * allocate and initialize struct page for the device memory. More- |
| 1103 | * over the device memory is un-accessible thus we do not want to |
| 1104 | * create a linear mapping for the memory like arch_add_memory() |
| 1105 | * would do. |
Jérôme Glisse | d3df0a4 | 2017-09-08 16:12:28 -0700 | [diff] [blame] | 1106 | * |
| 1107 | * For device public memory, which is accesible by the CPU, we do |
| 1108 | * want the linear mapping and thus use arch_add_memory(). |
Jérôme Glisse | 4ef589d | 2017-09-08 16:11:58 -0700 | [diff] [blame] | 1109 | */ |
Jérôme Glisse | d3df0a4 | 2017-09-08 16:12:28 -0700 | [diff] [blame] | 1110 | if (devmem->pagemap.type == MEMORY_DEVICE_PUBLIC) |
Christoph Hellwig | 24e6d5a | 2017-12-29 08:53:53 +0100 | [diff] [blame] | 1111 | ret = arch_add_memory(nid, align_start, align_size, NULL, |
| 1112 | false); |
Jérôme Glisse | d3df0a4 | 2017-09-08 16:12:28 -0700 | [diff] [blame] | 1113 | else |
| 1114 | ret = add_pages(nid, align_start >> PAGE_SHIFT, |
Christoph Hellwig | 24e6d5a | 2017-12-29 08:53:53 +0100 | [diff] [blame] | 1115 | align_size >> PAGE_SHIFT, NULL, false); |
Jérôme Glisse | 4ef589d | 2017-09-08 16:11:58 -0700 | [diff] [blame] | 1116 | if (ret) { |
| 1117 | mem_hotplug_done(); |
| 1118 | goto error_add_memory; |
| 1119 | } |
| 1120 | move_pfn_range_to_zone(&NODE_DATA(nid)->node_zones[ZONE_DEVICE], |
| 1121 | align_start >> PAGE_SHIFT, |
Christoph Hellwig | a99583e | 2017-12-29 08:53:57 +0100 | [diff] [blame] | 1122 | align_size >> PAGE_SHIFT, NULL); |
Jérôme Glisse | 4ef589d | 2017-09-08 16:11:58 -0700 | [diff] [blame] | 1123 | mem_hotplug_done(); |
| 1124 | |
| 1125 | for (pfn = devmem->pfn_first; pfn < devmem->pfn_last; pfn++) { |
| 1126 | struct page *page = pfn_to_page(pfn); |
| 1127 | |
| 1128 | page->pgmap = &devmem->pagemap; |
| 1129 | } |
| 1130 | return 0; |
| 1131 | |
| 1132 | error_add_memory: |
| 1133 | untrack_pfn(NULL, PHYS_PFN(align_start), align_size); |
| 1134 | error_radix: |
| 1135 | hmm_devmem_radix_release(devmem->resource); |
| 1136 | error: |
| 1137 | return ret; |
| 1138 | } |
| 1139 | |
| 1140 | static int hmm_devmem_match(struct device *dev, void *data, void *match_data) |
| 1141 | { |
| 1142 | struct hmm_devmem *devmem = data; |
| 1143 | |
| 1144 | return devmem->resource == match_data; |
| 1145 | } |
| 1146 | |
| 1147 | static void hmm_devmem_pages_remove(struct hmm_devmem *devmem) |
| 1148 | { |
| 1149 | devres_release(devmem->device, &hmm_devmem_release, |
| 1150 | &hmm_devmem_match, devmem->resource); |
| 1151 | } |
| 1152 | |
| 1153 | /* |
| 1154 | * hmm_devmem_add() - hotplug ZONE_DEVICE memory for device memory |
| 1155 | * |
| 1156 | * @ops: memory event device driver callback (see struct hmm_devmem_ops) |
| 1157 | * @device: device struct to bind the resource too |
| 1158 | * @size: size in bytes of the device memory to add |
| 1159 | * Returns: pointer to new hmm_devmem struct ERR_PTR otherwise |
| 1160 | * |
| 1161 | * This function first finds an empty range of physical address big enough to |
| 1162 | * contain the new resource, and then hotplugs it as ZONE_DEVICE memory, which |
| 1163 | * in turn allocates struct pages. It does not do anything beyond that; all |
| 1164 | * events affecting the memory will go through the various callbacks provided |
| 1165 | * by hmm_devmem_ops struct. |
| 1166 | * |
| 1167 | * Device driver should call this function during device initialization and |
| 1168 | * is then responsible of memory management. HMM only provides helpers. |
| 1169 | */ |
| 1170 | struct hmm_devmem *hmm_devmem_add(const struct hmm_devmem_ops *ops, |
| 1171 | struct device *device, |
| 1172 | unsigned long size) |
| 1173 | { |
| 1174 | struct hmm_devmem *devmem; |
| 1175 | resource_size_t addr; |
| 1176 | int ret; |
| 1177 | |
| 1178 | static_branch_enable(&device_private_key); |
| 1179 | |
| 1180 | devmem = devres_alloc_node(&hmm_devmem_release, sizeof(*devmem), |
| 1181 | GFP_KERNEL, dev_to_node(device)); |
| 1182 | if (!devmem) |
| 1183 | return ERR_PTR(-ENOMEM); |
| 1184 | |
| 1185 | init_completion(&devmem->completion); |
| 1186 | devmem->pfn_first = -1UL; |
| 1187 | devmem->pfn_last = -1UL; |
| 1188 | devmem->resource = NULL; |
| 1189 | devmem->device = device; |
| 1190 | devmem->ops = ops; |
| 1191 | |
| 1192 | ret = percpu_ref_init(&devmem->ref, &hmm_devmem_ref_release, |
| 1193 | 0, GFP_KERNEL); |
| 1194 | if (ret) |
| 1195 | goto error_percpu_ref; |
| 1196 | |
| 1197 | ret = devm_add_action(device, hmm_devmem_ref_exit, &devmem->ref); |
| 1198 | if (ret) |
| 1199 | goto error_devm_add_action; |
| 1200 | |
| 1201 | size = ALIGN(size, PA_SECTION_SIZE); |
| 1202 | addr = min((unsigned long)iomem_resource.end, |
| 1203 | (1UL << MAX_PHYSMEM_BITS) - 1); |
| 1204 | addr = addr - size + 1UL; |
| 1205 | |
| 1206 | /* |
| 1207 | * FIXME add a new helper to quickly walk resource tree and find free |
| 1208 | * range |
| 1209 | * |
| 1210 | * FIXME what about ioport_resource resource ? |
| 1211 | */ |
| 1212 | for (; addr > size && addr >= iomem_resource.start; addr -= size) { |
| 1213 | ret = region_intersects(addr, size, 0, IORES_DESC_NONE); |
| 1214 | if (ret != REGION_DISJOINT) |
| 1215 | continue; |
| 1216 | |
| 1217 | devmem->resource = devm_request_mem_region(device, addr, size, |
| 1218 | dev_name(device)); |
| 1219 | if (!devmem->resource) { |
| 1220 | ret = -ENOMEM; |
| 1221 | goto error_no_resource; |
| 1222 | } |
| 1223 | break; |
| 1224 | } |
| 1225 | if (!devmem->resource) { |
| 1226 | ret = -ERANGE; |
| 1227 | goto error_no_resource; |
| 1228 | } |
| 1229 | |
| 1230 | devmem->resource->desc = IORES_DESC_DEVICE_PRIVATE_MEMORY; |
| 1231 | devmem->pfn_first = devmem->resource->start >> PAGE_SHIFT; |
| 1232 | devmem->pfn_last = devmem->pfn_first + |
| 1233 | (resource_size(devmem->resource) >> PAGE_SHIFT); |
| 1234 | |
| 1235 | ret = hmm_devmem_pages_create(devmem); |
| 1236 | if (ret) |
| 1237 | goto error_pages; |
| 1238 | |
| 1239 | devres_add(device, devmem); |
| 1240 | |
| 1241 | ret = devm_add_action(device, hmm_devmem_ref_kill, &devmem->ref); |
| 1242 | if (ret) { |
| 1243 | hmm_devmem_remove(devmem); |
| 1244 | return ERR_PTR(ret); |
| 1245 | } |
| 1246 | |
| 1247 | return devmem; |
| 1248 | |
| 1249 | error_pages: |
| 1250 | devm_release_mem_region(device, devmem->resource->start, |
| 1251 | resource_size(devmem->resource)); |
| 1252 | error_no_resource: |
| 1253 | error_devm_add_action: |
| 1254 | hmm_devmem_ref_kill(&devmem->ref); |
| 1255 | hmm_devmem_ref_exit(&devmem->ref); |
| 1256 | error_percpu_ref: |
| 1257 | devres_free(devmem); |
| 1258 | return ERR_PTR(ret); |
| 1259 | } |
| 1260 | EXPORT_SYMBOL(hmm_devmem_add); |
| 1261 | |
Jérôme Glisse | d3df0a4 | 2017-09-08 16:12:28 -0700 | [diff] [blame] | 1262 | struct hmm_devmem *hmm_devmem_add_resource(const struct hmm_devmem_ops *ops, |
| 1263 | struct device *device, |
| 1264 | struct resource *res) |
| 1265 | { |
| 1266 | struct hmm_devmem *devmem; |
| 1267 | int ret; |
| 1268 | |
| 1269 | if (res->desc != IORES_DESC_DEVICE_PUBLIC_MEMORY) |
| 1270 | return ERR_PTR(-EINVAL); |
| 1271 | |
| 1272 | static_branch_enable(&device_private_key); |
| 1273 | |
| 1274 | devmem = devres_alloc_node(&hmm_devmem_release, sizeof(*devmem), |
| 1275 | GFP_KERNEL, dev_to_node(device)); |
| 1276 | if (!devmem) |
| 1277 | return ERR_PTR(-ENOMEM); |
| 1278 | |
| 1279 | init_completion(&devmem->completion); |
| 1280 | devmem->pfn_first = -1UL; |
| 1281 | devmem->pfn_last = -1UL; |
| 1282 | devmem->resource = res; |
| 1283 | devmem->device = device; |
| 1284 | devmem->ops = ops; |
| 1285 | |
| 1286 | ret = percpu_ref_init(&devmem->ref, &hmm_devmem_ref_release, |
| 1287 | 0, GFP_KERNEL); |
| 1288 | if (ret) |
| 1289 | goto error_percpu_ref; |
| 1290 | |
| 1291 | ret = devm_add_action(device, hmm_devmem_ref_exit, &devmem->ref); |
| 1292 | if (ret) |
| 1293 | goto error_devm_add_action; |
| 1294 | |
| 1295 | |
| 1296 | devmem->pfn_first = devmem->resource->start >> PAGE_SHIFT; |
| 1297 | devmem->pfn_last = devmem->pfn_first + |
| 1298 | (resource_size(devmem->resource) >> PAGE_SHIFT); |
| 1299 | |
| 1300 | ret = hmm_devmem_pages_create(devmem); |
| 1301 | if (ret) |
| 1302 | goto error_devm_add_action; |
| 1303 | |
| 1304 | devres_add(device, devmem); |
| 1305 | |
| 1306 | ret = devm_add_action(device, hmm_devmem_ref_kill, &devmem->ref); |
| 1307 | if (ret) { |
| 1308 | hmm_devmem_remove(devmem); |
| 1309 | return ERR_PTR(ret); |
| 1310 | } |
| 1311 | |
| 1312 | return devmem; |
| 1313 | |
| 1314 | error_devm_add_action: |
| 1315 | hmm_devmem_ref_kill(&devmem->ref); |
| 1316 | hmm_devmem_ref_exit(&devmem->ref); |
| 1317 | error_percpu_ref: |
| 1318 | devres_free(devmem); |
| 1319 | return ERR_PTR(ret); |
| 1320 | } |
| 1321 | EXPORT_SYMBOL(hmm_devmem_add_resource); |
| 1322 | |
Jérôme Glisse | 4ef589d | 2017-09-08 16:11:58 -0700 | [diff] [blame] | 1323 | /* |
| 1324 | * hmm_devmem_remove() - remove device memory (kill and free ZONE_DEVICE) |
| 1325 | * |
| 1326 | * @devmem: hmm_devmem struct use to track and manage the ZONE_DEVICE memory |
| 1327 | * |
| 1328 | * This will hot-unplug memory that was hotplugged by hmm_devmem_add on behalf |
| 1329 | * of the device driver. It will free struct page and remove the resource that |
| 1330 | * reserved the physical address range for this device memory. |
| 1331 | */ |
| 1332 | void hmm_devmem_remove(struct hmm_devmem *devmem) |
| 1333 | { |
| 1334 | resource_size_t start, size; |
| 1335 | struct device *device; |
Jérôme Glisse | d3df0a4 | 2017-09-08 16:12:28 -0700 | [diff] [blame] | 1336 | bool cdm = false; |
Jérôme Glisse | 4ef589d | 2017-09-08 16:11:58 -0700 | [diff] [blame] | 1337 | |
| 1338 | if (!devmem) |
| 1339 | return; |
| 1340 | |
| 1341 | device = devmem->device; |
| 1342 | start = devmem->resource->start; |
| 1343 | size = resource_size(devmem->resource); |
| 1344 | |
Jérôme Glisse | d3df0a4 | 2017-09-08 16:12:28 -0700 | [diff] [blame] | 1345 | cdm = devmem->resource->desc == IORES_DESC_DEVICE_PUBLIC_MEMORY; |
Jérôme Glisse | 4ef589d | 2017-09-08 16:11:58 -0700 | [diff] [blame] | 1346 | hmm_devmem_ref_kill(&devmem->ref); |
| 1347 | hmm_devmem_ref_exit(&devmem->ref); |
| 1348 | hmm_devmem_pages_remove(devmem); |
| 1349 | |
Jérôme Glisse | d3df0a4 | 2017-09-08 16:12:28 -0700 | [diff] [blame] | 1350 | if (!cdm) |
| 1351 | devm_release_mem_region(device, start, size); |
Jérôme Glisse | 4ef589d | 2017-09-08 16:11:58 -0700 | [diff] [blame] | 1352 | } |
| 1353 | EXPORT_SYMBOL(hmm_devmem_remove); |
Jérôme Glisse | 858b54d | 2017-09-08 16:12:02 -0700 | [diff] [blame] | 1354 | |
| 1355 | /* |
| 1356 | * A device driver that wants to handle multiple devices memory through a |
| 1357 | * single fake device can use hmm_device to do so. This is purely a helper |
| 1358 | * and it is not needed to make use of any HMM functionality. |
| 1359 | */ |
| 1360 | #define HMM_DEVICE_MAX 256 |
| 1361 | |
| 1362 | static DECLARE_BITMAP(hmm_device_mask, HMM_DEVICE_MAX); |
| 1363 | static DEFINE_SPINLOCK(hmm_device_lock); |
| 1364 | static struct class *hmm_device_class; |
| 1365 | static dev_t hmm_device_devt; |
| 1366 | |
| 1367 | static void hmm_device_release(struct device *device) |
| 1368 | { |
| 1369 | struct hmm_device *hmm_device; |
| 1370 | |
| 1371 | hmm_device = container_of(device, struct hmm_device, device); |
| 1372 | spin_lock(&hmm_device_lock); |
| 1373 | clear_bit(hmm_device->minor, hmm_device_mask); |
| 1374 | spin_unlock(&hmm_device_lock); |
| 1375 | |
| 1376 | kfree(hmm_device); |
| 1377 | } |
| 1378 | |
| 1379 | struct hmm_device *hmm_device_new(void *drvdata) |
| 1380 | { |
| 1381 | struct hmm_device *hmm_device; |
| 1382 | |
| 1383 | hmm_device = kzalloc(sizeof(*hmm_device), GFP_KERNEL); |
| 1384 | if (!hmm_device) |
| 1385 | return ERR_PTR(-ENOMEM); |
| 1386 | |
| 1387 | spin_lock(&hmm_device_lock); |
| 1388 | hmm_device->minor = find_first_zero_bit(hmm_device_mask, HMM_DEVICE_MAX); |
| 1389 | if (hmm_device->minor >= HMM_DEVICE_MAX) { |
| 1390 | spin_unlock(&hmm_device_lock); |
| 1391 | kfree(hmm_device); |
| 1392 | return ERR_PTR(-EBUSY); |
| 1393 | } |
| 1394 | set_bit(hmm_device->minor, hmm_device_mask); |
| 1395 | spin_unlock(&hmm_device_lock); |
| 1396 | |
| 1397 | dev_set_name(&hmm_device->device, "hmm_device%d", hmm_device->minor); |
| 1398 | hmm_device->device.devt = MKDEV(MAJOR(hmm_device_devt), |
| 1399 | hmm_device->minor); |
| 1400 | hmm_device->device.release = hmm_device_release; |
| 1401 | dev_set_drvdata(&hmm_device->device, drvdata); |
| 1402 | hmm_device->device.class = hmm_device_class; |
| 1403 | device_initialize(&hmm_device->device); |
| 1404 | |
| 1405 | return hmm_device; |
| 1406 | } |
| 1407 | EXPORT_SYMBOL(hmm_device_new); |
| 1408 | |
| 1409 | void hmm_device_put(struct hmm_device *hmm_device) |
| 1410 | { |
| 1411 | put_device(&hmm_device->device); |
| 1412 | } |
| 1413 | EXPORT_SYMBOL(hmm_device_put); |
| 1414 | |
| 1415 | static int __init hmm_init(void) |
| 1416 | { |
| 1417 | int ret; |
| 1418 | |
| 1419 | ret = alloc_chrdev_region(&hmm_device_devt, 0, |
| 1420 | HMM_DEVICE_MAX, |
| 1421 | "hmm_device"); |
| 1422 | if (ret) |
| 1423 | return ret; |
| 1424 | |
| 1425 | hmm_device_class = class_create(THIS_MODULE, "hmm_device"); |
| 1426 | if (IS_ERR(hmm_device_class)) { |
| 1427 | unregister_chrdev_region(hmm_device_devt, HMM_DEVICE_MAX); |
| 1428 | return PTR_ERR(hmm_device_class); |
| 1429 | } |
| 1430 | return 0; |
| 1431 | } |
| 1432 | |
| 1433 | device_initcall(hmm_init); |
Jérôme Glisse | df6ad69 | 2017-09-08 16:12:24 -0700 | [diff] [blame] | 1434 | #endif /* CONFIG_DEVICE_PRIVATE || CONFIG_DEVICE_PUBLIC */ |