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; |
| 302 | bool write; |
| 303 | }; |
| 304 | |
| 305 | static int hmm_vma_do_fault(struct mm_walk *walk, |
| 306 | unsigned long addr, |
| 307 | hmm_pfn_t *pfn) |
| 308 | { |
| 309 | unsigned int flags = FAULT_FLAG_ALLOW_RETRY | FAULT_FLAG_REMOTE; |
| 310 | struct hmm_vma_walk *hmm_vma_walk = walk->private; |
| 311 | struct vm_area_struct *vma = walk->vma; |
| 312 | int r; |
| 313 | |
| 314 | flags |= hmm_vma_walk->block ? 0 : FAULT_FLAG_ALLOW_RETRY; |
| 315 | flags |= hmm_vma_walk->write ? FAULT_FLAG_WRITE : 0; |
| 316 | r = handle_mm_fault(vma, addr, flags); |
| 317 | if (r & VM_FAULT_RETRY) |
| 318 | return -EBUSY; |
| 319 | if (r & VM_FAULT_ERROR) { |
| 320 | *pfn = HMM_PFN_ERROR; |
| 321 | return -EFAULT; |
| 322 | } |
| 323 | |
| 324 | return -EAGAIN; |
| 325 | } |
| 326 | |
Jérôme Glisse | da4c3c7 | 2017-09-08 16:11:31 -0700 | [diff] [blame] | 327 | static void hmm_pfns_special(hmm_pfn_t *pfns, |
| 328 | unsigned long addr, |
| 329 | unsigned long end) |
| 330 | { |
| 331 | for (; addr < end; addr += PAGE_SIZE, pfns++) |
| 332 | *pfns = HMM_PFN_SPECIAL; |
| 333 | } |
| 334 | |
| 335 | static int hmm_pfns_bad(unsigned long addr, |
| 336 | unsigned long end, |
| 337 | struct mm_walk *walk) |
| 338 | { |
Jérôme Glisse | c719547 | 2018-04-10 16:28:27 -0700 | [diff] [blame] | 339 | struct hmm_vma_walk *hmm_vma_walk = walk->private; |
| 340 | struct hmm_range *range = hmm_vma_walk->range; |
Jérôme Glisse | da4c3c7 | 2017-09-08 16:11:31 -0700 | [diff] [blame] | 341 | hmm_pfn_t *pfns = range->pfns; |
| 342 | unsigned long i; |
| 343 | |
| 344 | i = (addr - range->start) >> PAGE_SHIFT; |
| 345 | for (; addr < end; addr += PAGE_SIZE, i++) |
| 346 | pfns[i] = HMM_PFN_ERROR; |
| 347 | |
| 348 | return 0; |
| 349 | } |
| 350 | |
Jérôme Glisse | 74eee18 | 2017-09-08 16:11:35 -0700 | [diff] [blame] | 351 | static void hmm_pfns_clear(hmm_pfn_t *pfns, |
| 352 | unsigned long addr, |
| 353 | unsigned long end) |
| 354 | { |
| 355 | for (; addr < end; addr += PAGE_SIZE, pfns++) |
| 356 | *pfns = 0; |
| 357 | } |
| 358 | |
Jérôme Glisse | da4c3c7 | 2017-09-08 16:11:31 -0700 | [diff] [blame] | 359 | static int hmm_vma_walk_hole(unsigned long addr, |
| 360 | unsigned long end, |
| 361 | struct mm_walk *walk) |
| 362 | { |
Jérôme Glisse | 74eee18 | 2017-09-08 16:11:35 -0700 | [diff] [blame] | 363 | struct hmm_vma_walk *hmm_vma_walk = walk->private; |
| 364 | struct hmm_range *range = hmm_vma_walk->range; |
Jérôme Glisse | da4c3c7 | 2017-09-08 16:11:31 -0700 | [diff] [blame] | 365 | hmm_pfn_t *pfns = range->pfns; |
| 366 | unsigned long i; |
| 367 | |
Jérôme Glisse | 74eee18 | 2017-09-08 16:11:35 -0700 | [diff] [blame] | 368 | hmm_vma_walk->last = addr; |
Jérôme Glisse | da4c3c7 | 2017-09-08 16:11:31 -0700 | [diff] [blame] | 369 | i = (addr - range->start) >> PAGE_SHIFT; |
Jérôme Glisse | 74eee18 | 2017-09-08 16:11:35 -0700 | [diff] [blame] | 370 | for (; addr < end; addr += PAGE_SIZE, i++) { |
Jérôme Glisse | da4c3c7 | 2017-09-08 16:11:31 -0700 | [diff] [blame] | 371 | pfns[i] = HMM_PFN_EMPTY; |
Jérôme Glisse | 74eee18 | 2017-09-08 16:11:35 -0700 | [diff] [blame] | 372 | if (hmm_vma_walk->fault) { |
| 373 | int ret; |
Jérôme Glisse | da4c3c7 | 2017-09-08 16:11:31 -0700 | [diff] [blame] | 374 | |
Jérôme Glisse | 74eee18 | 2017-09-08 16:11:35 -0700 | [diff] [blame] | 375 | ret = hmm_vma_do_fault(walk, addr, &pfns[i]); |
| 376 | if (ret != -EAGAIN) |
| 377 | return ret; |
| 378 | } |
| 379 | } |
| 380 | |
| 381 | return hmm_vma_walk->fault ? -EAGAIN : 0; |
Jérôme Glisse | da4c3c7 | 2017-09-08 16:11:31 -0700 | [diff] [blame] | 382 | } |
| 383 | |
| 384 | static int hmm_vma_walk_clear(unsigned long addr, |
| 385 | unsigned long end, |
| 386 | struct mm_walk *walk) |
| 387 | { |
Jérôme Glisse | 74eee18 | 2017-09-08 16:11:35 -0700 | [diff] [blame] | 388 | struct hmm_vma_walk *hmm_vma_walk = walk->private; |
| 389 | struct hmm_range *range = hmm_vma_walk->range; |
Jérôme Glisse | da4c3c7 | 2017-09-08 16:11:31 -0700 | [diff] [blame] | 390 | hmm_pfn_t *pfns = range->pfns; |
| 391 | unsigned long i; |
| 392 | |
Jérôme Glisse | 74eee18 | 2017-09-08 16:11:35 -0700 | [diff] [blame] | 393 | hmm_vma_walk->last = addr; |
Jérôme Glisse | da4c3c7 | 2017-09-08 16:11:31 -0700 | [diff] [blame] | 394 | i = (addr - range->start) >> PAGE_SHIFT; |
Jérôme Glisse | 74eee18 | 2017-09-08 16:11:35 -0700 | [diff] [blame] | 395 | for (; addr < end; addr += PAGE_SIZE, i++) { |
Jérôme Glisse | da4c3c7 | 2017-09-08 16:11:31 -0700 | [diff] [blame] | 396 | pfns[i] = 0; |
Jérôme Glisse | 74eee18 | 2017-09-08 16:11:35 -0700 | [diff] [blame] | 397 | if (hmm_vma_walk->fault) { |
| 398 | int ret; |
Jérôme Glisse | da4c3c7 | 2017-09-08 16:11:31 -0700 | [diff] [blame] | 399 | |
Jérôme Glisse | 74eee18 | 2017-09-08 16:11:35 -0700 | [diff] [blame] | 400 | ret = hmm_vma_do_fault(walk, addr, &pfns[i]); |
| 401 | if (ret != -EAGAIN) |
| 402 | return ret; |
| 403 | } |
| 404 | } |
| 405 | |
| 406 | return hmm_vma_walk->fault ? -EAGAIN : 0; |
Jérôme Glisse | da4c3c7 | 2017-09-08 16:11:31 -0700 | [diff] [blame] | 407 | } |
| 408 | |
| 409 | static int hmm_vma_walk_pmd(pmd_t *pmdp, |
| 410 | unsigned long start, |
| 411 | unsigned long end, |
| 412 | struct mm_walk *walk) |
| 413 | { |
Jérôme Glisse | 74eee18 | 2017-09-08 16:11:35 -0700 | [diff] [blame] | 414 | struct hmm_vma_walk *hmm_vma_walk = walk->private; |
| 415 | struct hmm_range *range = hmm_vma_walk->range; |
Jérôme Glisse | da4c3c7 | 2017-09-08 16:11:31 -0700 | [diff] [blame] | 416 | struct vm_area_struct *vma = walk->vma; |
| 417 | hmm_pfn_t *pfns = range->pfns; |
| 418 | unsigned long addr = start, i; |
Jérôme Glisse | 74eee18 | 2017-09-08 16:11:35 -0700 | [diff] [blame] | 419 | bool write_fault; |
Jérôme Glisse | da4c3c7 | 2017-09-08 16:11:31 -0700 | [diff] [blame] | 420 | hmm_pfn_t flag; |
| 421 | pte_t *ptep; |
| 422 | |
| 423 | i = (addr - range->start) >> PAGE_SHIFT; |
| 424 | flag = vma->vm_flags & VM_READ ? HMM_PFN_READ : 0; |
Jérôme Glisse | 74eee18 | 2017-09-08 16:11:35 -0700 | [diff] [blame] | 425 | write_fault = hmm_vma_walk->fault & hmm_vma_walk->write; |
Jérôme Glisse | da4c3c7 | 2017-09-08 16:11:31 -0700 | [diff] [blame] | 426 | |
| 427 | again: |
| 428 | if (pmd_none(*pmdp)) |
| 429 | return hmm_vma_walk_hole(start, end, walk); |
| 430 | |
| 431 | if (pmd_huge(*pmdp) && vma->vm_flags & VM_HUGETLB) |
| 432 | return hmm_pfns_bad(start, end, walk); |
| 433 | |
| 434 | if (pmd_devmap(*pmdp) || pmd_trans_huge(*pmdp)) { |
| 435 | unsigned long pfn; |
| 436 | pmd_t pmd; |
| 437 | |
| 438 | /* |
| 439 | * No need to take pmd_lock here, even if some other threads |
| 440 | * is splitting the huge pmd we will get that event through |
| 441 | * mmu_notifier callback. |
| 442 | * |
| 443 | * So just read pmd value and check again its a transparent |
| 444 | * huge or device mapping one and compute corresponding pfn |
| 445 | * values. |
| 446 | */ |
| 447 | pmd = pmd_read_atomic(pmdp); |
| 448 | barrier(); |
| 449 | if (!pmd_devmap(pmd) && !pmd_trans_huge(pmd)) |
| 450 | goto again; |
| 451 | if (pmd_protnone(pmd)) |
| 452 | return hmm_vma_walk_clear(start, end, walk); |
| 453 | |
Linus Torvalds | f6f3732 | 2017-12-15 18:53:22 -0800 | [diff] [blame] | 454 | if (write_fault && !pmd_write(pmd)) |
Jérôme Glisse | 74eee18 | 2017-09-08 16:11:35 -0700 | [diff] [blame] | 455 | return hmm_vma_walk_clear(start, end, walk); |
| 456 | |
Jérôme Glisse | da4c3c7 | 2017-09-08 16:11:31 -0700 | [diff] [blame] | 457 | pfn = pmd_pfn(pmd) + pte_index(addr); |
Linus Torvalds | f6f3732 | 2017-12-15 18:53:22 -0800 | [diff] [blame] | 458 | flag |= pmd_write(pmd) ? HMM_PFN_WRITE : 0; |
Jérôme Glisse | da4c3c7 | 2017-09-08 16:11:31 -0700 | [diff] [blame] | 459 | for (; addr < end; addr += PAGE_SIZE, i++, pfn++) |
| 460 | pfns[i] = hmm_pfn_t_from_pfn(pfn) | flag; |
| 461 | return 0; |
| 462 | } |
| 463 | |
| 464 | if (pmd_bad(*pmdp)) |
| 465 | return hmm_pfns_bad(start, end, walk); |
| 466 | |
| 467 | ptep = pte_offset_map(pmdp, addr); |
| 468 | for (; addr < end; addr += PAGE_SIZE, ptep++, i++) { |
| 469 | pte_t pte = *ptep; |
| 470 | |
| 471 | pfns[i] = 0; |
| 472 | |
Jérôme Glisse | 74eee18 | 2017-09-08 16:11:35 -0700 | [diff] [blame] | 473 | if (pte_none(pte)) { |
Jérôme Glisse | da4c3c7 | 2017-09-08 16:11:31 -0700 | [diff] [blame] | 474 | pfns[i] = HMM_PFN_EMPTY; |
Jérôme Glisse | 74eee18 | 2017-09-08 16:11:35 -0700 | [diff] [blame] | 475 | if (hmm_vma_walk->fault) |
| 476 | goto fault; |
Jérôme Glisse | da4c3c7 | 2017-09-08 16:11:31 -0700 | [diff] [blame] | 477 | continue; |
| 478 | } |
| 479 | |
Jérôme Glisse | 74eee18 | 2017-09-08 16:11:35 -0700 | [diff] [blame] | 480 | if (!pte_present(pte)) { |
Ralph Campbell | 8d63e4c | 2018-01-31 16:20:30 -0800 | [diff] [blame] | 481 | swp_entry_t entry = pte_to_swp_entry(pte); |
Jérôme Glisse | 74eee18 | 2017-09-08 16:11:35 -0700 | [diff] [blame] | 482 | |
| 483 | if (!non_swap_entry(entry)) { |
| 484 | if (hmm_vma_walk->fault) |
| 485 | goto fault; |
| 486 | continue; |
| 487 | } |
| 488 | |
Jérôme Glisse | 74eee18 | 2017-09-08 16:11:35 -0700 | [diff] [blame] | 489 | /* |
| 490 | * This is a special swap entry, ignore migration, use |
| 491 | * device and report anything else as error. |
| 492 | */ |
Jérôme Glisse | 4ef589d | 2017-09-08 16:11:58 -0700 | [diff] [blame] | 493 | if (is_device_private_entry(entry)) { |
| 494 | pfns[i] = hmm_pfn_t_from_pfn(swp_offset(entry)); |
| 495 | if (is_write_device_private_entry(entry)) { |
| 496 | pfns[i] |= HMM_PFN_WRITE; |
| 497 | } else if (write_fault) |
| 498 | goto fault; |
| 499 | pfns[i] |= HMM_PFN_DEVICE_UNADDRESSABLE; |
| 500 | pfns[i] |= flag; |
| 501 | } else if (is_migration_entry(entry)) { |
Jérôme Glisse | 74eee18 | 2017-09-08 16:11:35 -0700 | [diff] [blame] | 502 | if (hmm_vma_walk->fault) { |
| 503 | pte_unmap(ptep); |
| 504 | hmm_vma_walk->last = addr; |
| 505 | migration_entry_wait(vma->vm_mm, |
| 506 | pmdp, addr); |
| 507 | return -EAGAIN; |
| 508 | } |
| 509 | continue; |
| 510 | } else { |
| 511 | /* Report error for everything else */ |
| 512 | pfns[i] = HMM_PFN_ERROR; |
| 513 | } |
| 514 | continue; |
| 515 | } |
| 516 | |
Linus Torvalds | f6f3732 | 2017-12-15 18:53:22 -0800 | [diff] [blame] | 517 | if (write_fault && !pte_write(pte)) |
Jérôme Glisse | 74eee18 | 2017-09-08 16:11:35 -0700 | [diff] [blame] | 518 | goto fault; |
| 519 | |
Jérôme Glisse | da4c3c7 | 2017-09-08 16:11:31 -0700 | [diff] [blame] | 520 | pfns[i] = hmm_pfn_t_from_pfn(pte_pfn(pte)) | flag; |
Linus Torvalds | f6f3732 | 2017-12-15 18:53:22 -0800 | [diff] [blame] | 521 | pfns[i] |= pte_write(pte) ? HMM_PFN_WRITE : 0; |
Jérôme Glisse | 74eee18 | 2017-09-08 16:11:35 -0700 | [diff] [blame] | 522 | continue; |
| 523 | |
| 524 | fault: |
| 525 | pte_unmap(ptep); |
| 526 | /* Fault all pages in range */ |
| 527 | return hmm_vma_walk_clear(start, end, walk); |
Jérôme Glisse | da4c3c7 | 2017-09-08 16:11:31 -0700 | [diff] [blame] | 528 | } |
| 529 | pte_unmap(ptep - 1); |
| 530 | |
| 531 | return 0; |
| 532 | } |
| 533 | |
| 534 | /* |
| 535 | * 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^] | 536 | * @range: range being snapshotted |
Jérôme Glisse | da4c3c7 | 2017-09-08 16:11:31 -0700 | [diff] [blame] | 537 | * Returns: -EINVAL if invalid argument, -ENOMEM out of memory, 0 success |
| 538 | * |
| 539 | * This snapshots the CPU page table for a range of virtual addresses. Snapshot |
| 540 | * validity is tracked by range struct. See hmm_vma_range_done() for further |
| 541 | * information. |
| 542 | * |
| 543 | * The range struct is initialized here. It tracks the CPU page table, but only |
| 544 | * if the function returns success (0), in which case the caller must then call |
| 545 | * hmm_vma_range_done() to stop CPU page table update tracking on this range. |
| 546 | * |
| 547 | * NOT CALLING hmm_vma_range_done() IF FUNCTION RETURNS 0 WILL LEAD TO SERIOUS |
| 548 | * MEMORY CORRUPTION ! YOU HAVE BEEN WARNED ! |
| 549 | */ |
Jérôme Glisse | 08232a4 | 2018-04-10 16:28:30 -0700 | [diff] [blame^] | 550 | int hmm_vma_get_pfns(struct hmm_range *range) |
Jérôme Glisse | da4c3c7 | 2017-09-08 16:11:31 -0700 | [diff] [blame] | 551 | { |
Jérôme Glisse | 08232a4 | 2018-04-10 16:28:30 -0700 | [diff] [blame^] | 552 | struct vm_area_struct *vma = range->vma; |
Jérôme Glisse | 74eee18 | 2017-09-08 16:11:35 -0700 | [diff] [blame] | 553 | struct hmm_vma_walk hmm_vma_walk; |
Jérôme Glisse | da4c3c7 | 2017-09-08 16:11:31 -0700 | [diff] [blame] | 554 | struct mm_walk mm_walk; |
| 555 | struct hmm *hmm; |
| 556 | |
| 557 | /* FIXME support hugetlb fs */ |
| 558 | if (is_vm_hugetlb_page(vma) || (vma->vm_flags & VM_SPECIAL)) { |
Jérôme Glisse | 08232a4 | 2018-04-10 16:28:30 -0700 | [diff] [blame^] | 559 | hmm_pfns_special(range->pfns, range->start, range->end); |
Jérôme Glisse | da4c3c7 | 2017-09-08 16:11:31 -0700 | [diff] [blame] | 560 | return -EINVAL; |
| 561 | } |
| 562 | |
| 563 | /* Sanity check, this really should not happen ! */ |
Jérôme Glisse | 08232a4 | 2018-04-10 16:28:30 -0700 | [diff] [blame^] | 564 | 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] | 565 | return -EINVAL; |
Jérôme Glisse | 08232a4 | 2018-04-10 16:28:30 -0700 | [diff] [blame^] | 566 | 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] | 567 | return -EINVAL; |
| 568 | |
| 569 | hmm = hmm_register(vma->vm_mm); |
| 570 | if (!hmm) |
| 571 | return -ENOMEM; |
| 572 | /* Caller must have registered a mirror, via hmm_mirror_register() ! */ |
| 573 | if (!hmm->mmu_notifier.ops) |
| 574 | return -EINVAL; |
| 575 | |
| 576 | /* Initialize range to track CPU page table update */ |
Jérôme Glisse | da4c3c7 | 2017-09-08 16:11:31 -0700 | [diff] [blame] | 577 | spin_lock(&hmm->lock); |
| 578 | range->valid = true; |
| 579 | list_add_rcu(&range->list, &hmm->ranges); |
| 580 | spin_unlock(&hmm->lock); |
| 581 | |
Jérôme Glisse | 74eee18 | 2017-09-08 16:11:35 -0700 | [diff] [blame] | 582 | hmm_vma_walk.fault = false; |
| 583 | hmm_vma_walk.range = range; |
| 584 | mm_walk.private = &hmm_vma_walk; |
| 585 | |
Jérôme Glisse | da4c3c7 | 2017-09-08 16:11:31 -0700 | [diff] [blame] | 586 | mm_walk.vma = vma; |
| 587 | mm_walk.mm = vma->vm_mm; |
Jérôme Glisse | da4c3c7 | 2017-09-08 16:11:31 -0700 | [diff] [blame] | 588 | mm_walk.pte_entry = NULL; |
| 589 | mm_walk.test_walk = NULL; |
| 590 | mm_walk.hugetlb_entry = NULL; |
| 591 | mm_walk.pmd_entry = hmm_vma_walk_pmd; |
| 592 | mm_walk.pte_hole = hmm_vma_walk_hole; |
| 593 | |
Jérôme Glisse | 08232a4 | 2018-04-10 16:28:30 -0700 | [diff] [blame^] | 594 | walk_page_range(range->start, range->end, &mm_walk); |
Jérôme Glisse | da4c3c7 | 2017-09-08 16:11:31 -0700 | [diff] [blame] | 595 | return 0; |
| 596 | } |
| 597 | EXPORT_SYMBOL(hmm_vma_get_pfns); |
| 598 | |
| 599 | /* |
| 600 | * 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] | 601 | * @range: range being tracked |
| 602 | * Returns: false if range data has been invalidated, true otherwise |
| 603 | * |
| 604 | * Range struct is used to track updates to the CPU page table after a call to |
| 605 | * either hmm_vma_get_pfns() or hmm_vma_fault(). Once the device driver is done |
| 606 | * using the data, or wants to lock updates to the data it got from those |
| 607 | * functions, it must call the hmm_vma_range_done() function, which will then |
| 608 | * stop tracking CPU page table updates. |
| 609 | * |
| 610 | * Note that device driver must still implement general CPU page table update |
| 611 | * tracking either by using hmm_mirror (see hmm_mirror_register()) or by using |
| 612 | * the mmu_notifier API directly. |
| 613 | * |
| 614 | * CPU page table update tracking done through hmm_range is only temporary and |
| 615 | * to be used while trying to duplicate CPU page table contents for a range of |
| 616 | * virtual addresses. |
| 617 | * |
| 618 | * There are two ways to use this : |
| 619 | * again: |
Jérôme Glisse | 08232a4 | 2018-04-10 16:28:30 -0700 | [diff] [blame^] | 620 | * hmm_vma_get_pfns(range); or hmm_vma_fault(...); |
Jérôme Glisse | da4c3c7 | 2017-09-08 16:11:31 -0700 | [diff] [blame] | 621 | * trans = device_build_page_table_update_transaction(pfns); |
| 622 | * device_page_table_lock(); |
Jérôme Glisse | 08232a4 | 2018-04-10 16:28:30 -0700 | [diff] [blame^] | 623 | * if (!hmm_vma_range_done(range)) { |
Jérôme Glisse | da4c3c7 | 2017-09-08 16:11:31 -0700 | [diff] [blame] | 624 | * device_page_table_unlock(); |
| 625 | * goto again; |
| 626 | * } |
| 627 | * device_commit_transaction(trans); |
| 628 | * device_page_table_unlock(); |
| 629 | * |
| 630 | * Or: |
Jérôme Glisse | 08232a4 | 2018-04-10 16:28:30 -0700 | [diff] [blame^] | 631 | * hmm_vma_get_pfns(range); or hmm_vma_fault(...); |
Jérôme Glisse | da4c3c7 | 2017-09-08 16:11:31 -0700 | [diff] [blame] | 632 | * device_page_table_lock(); |
Jérôme Glisse | 08232a4 | 2018-04-10 16:28:30 -0700 | [diff] [blame^] | 633 | * hmm_vma_range_done(range); |
| 634 | * device_update_page_table(range->pfns); |
Jérôme Glisse | da4c3c7 | 2017-09-08 16:11:31 -0700 | [diff] [blame] | 635 | * device_page_table_unlock(); |
| 636 | */ |
Jérôme Glisse | 08232a4 | 2018-04-10 16:28:30 -0700 | [diff] [blame^] | 637 | bool hmm_vma_range_done(struct hmm_range *range) |
Jérôme Glisse | da4c3c7 | 2017-09-08 16:11:31 -0700 | [diff] [blame] | 638 | { |
| 639 | unsigned long npages = (range->end - range->start) >> PAGE_SHIFT; |
| 640 | struct hmm *hmm; |
| 641 | |
| 642 | if (range->end <= range->start) { |
| 643 | BUG(); |
| 644 | return false; |
| 645 | } |
| 646 | |
Jérôme Glisse | 08232a4 | 2018-04-10 16:28:30 -0700 | [diff] [blame^] | 647 | hmm = hmm_register(range->vma->vm_mm); |
Jérôme Glisse | da4c3c7 | 2017-09-08 16:11:31 -0700 | [diff] [blame] | 648 | if (!hmm) { |
| 649 | memset(range->pfns, 0, sizeof(*range->pfns) * npages); |
| 650 | return false; |
| 651 | } |
| 652 | |
| 653 | spin_lock(&hmm->lock); |
| 654 | list_del_rcu(&range->list); |
| 655 | spin_unlock(&hmm->lock); |
| 656 | |
| 657 | return range->valid; |
| 658 | } |
| 659 | EXPORT_SYMBOL(hmm_vma_range_done); |
Jérôme Glisse | 74eee18 | 2017-09-08 16:11:35 -0700 | [diff] [blame] | 660 | |
| 661 | /* |
| 662 | * 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^] | 663 | * @range: range being faulted |
Jérôme Glisse | 74eee18 | 2017-09-08 16:11:35 -0700 | [diff] [blame] | 664 | * @write: is it a write fault |
| 665 | * @block: allow blocking on fault (if true it sleeps and do not drop mmap_sem) |
| 666 | * Returns: 0 success, error otherwise (-EAGAIN means mmap_sem have been drop) |
| 667 | * |
| 668 | * This is similar to a regular CPU page fault except that it will not trigger |
| 669 | * any memory migration if the memory being faulted is not accessible by CPUs. |
| 670 | * |
| 671 | * On error, for one virtual address in the range, the function will set the |
| 672 | * hmm_pfn_t error flag for the corresponding pfn entry. |
| 673 | * |
| 674 | * Expected use pattern: |
| 675 | * retry: |
| 676 | * down_read(&mm->mmap_sem); |
| 677 | * // Find vma and address device wants to fault, initialize hmm_pfn_t |
| 678 | * // array accordingly |
Jérôme Glisse | 08232a4 | 2018-04-10 16:28:30 -0700 | [diff] [blame^] | 679 | * ret = hmm_vma_fault(range, write, block); |
Jérôme Glisse | 74eee18 | 2017-09-08 16:11:35 -0700 | [diff] [blame] | 680 | * switch (ret) { |
| 681 | * case -EAGAIN: |
Jérôme Glisse | 08232a4 | 2018-04-10 16:28:30 -0700 | [diff] [blame^] | 682 | * hmm_vma_range_done(range); |
Jérôme Glisse | 74eee18 | 2017-09-08 16:11:35 -0700 | [diff] [blame] | 683 | * // You might want to rate limit or yield to play nicely, you may |
| 684 | * // also commit any valid pfn in the array assuming that you are |
| 685 | * // getting true from hmm_vma_range_monitor_end() |
| 686 | * goto retry; |
| 687 | * case 0: |
| 688 | * break; |
| 689 | * default: |
| 690 | * // Handle error ! |
| 691 | * up_read(&mm->mmap_sem) |
| 692 | * return; |
| 693 | * } |
| 694 | * // Take device driver lock that serialize device page table update |
| 695 | * driver_lock_device_page_table_update(); |
Jérôme Glisse | 08232a4 | 2018-04-10 16:28:30 -0700 | [diff] [blame^] | 696 | * hmm_vma_range_done(range); |
Jérôme Glisse | 74eee18 | 2017-09-08 16:11:35 -0700 | [diff] [blame] | 697 | * // Commit pfns we got from hmm_vma_fault() |
| 698 | * driver_unlock_device_page_table_update(); |
| 699 | * up_read(&mm->mmap_sem) |
| 700 | * |
| 701 | * YOU MUST CALL hmm_vma_range_done() AFTER THIS FUNCTION RETURN SUCCESS (0) |
| 702 | * BEFORE FREEING THE range struct OR YOU WILL HAVE SERIOUS MEMORY CORRUPTION ! |
| 703 | * |
| 704 | * YOU HAVE BEEN WARNED ! |
| 705 | */ |
Jérôme Glisse | 08232a4 | 2018-04-10 16:28:30 -0700 | [diff] [blame^] | 706 | int hmm_vma_fault(struct hmm_range *range, bool write, bool block) |
Jérôme Glisse | 74eee18 | 2017-09-08 16:11:35 -0700 | [diff] [blame] | 707 | { |
Jérôme Glisse | 08232a4 | 2018-04-10 16:28:30 -0700 | [diff] [blame^] | 708 | struct vm_area_struct *vma = range->vma; |
| 709 | unsigned long start = range->start; |
Jérôme Glisse | 74eee18 | 2017-09-08 16:11:35 -0700 | [diff] [blame] | 710 | struct hmm_vma_walk hmm_vma_walk; |
| 711 | struct mm_walk mm_walk; |
| 712 | struct hmm *hmm; |
| 713 | int ret; |
| 714 | |
| 715 | /* Sanity check, this really should not happen ! */ |
Jérôme Glisse | 08232a4 | 2018-04-10 16:28:30 -0700 | [diff] [blame^] | 716 | 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] | 717 | return -EINVAL; |
Jérôme Glisse | 08232a4 | 2018-04-10 16:28:30 -0700 | [diff] [blame^] | 718 | 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] | 719 | return -EINVAL; |
| 720 | |
| 721 | hmm = hmm_register(vma->vm_mm); |
| 722 | if (!hmm) { |
Jérôme Glisse | 08232a4 | 2018-04-10 16:28:30 -0700 | [diff] [blame^] | 723 | hmm_pfns_clear(range->pfns, range->start, range->end); |
Jérôme Glisse | 74eee18 | 2017-09-08 16:11:35 -0700 | [diff] [blame] | 724 | return -ENOMEM; |
| 725 | } |
| 726 | /* Caller must have registered a mirror using hmm_mirror_register() */ |
| 727 | if (!hmm->mmu_notifier.ops) |
| 728 | return -EINVAL; |
| 729 | |
| 730 | /* Initialize range to track CPU page table update */ |
Jérôme Glisse | 74eee18 | 2017-09-08 16:11:35 -0700 | [diff] [blame] | 731 | spin_lock(&hmm->lock); |
| 732 | range->valid = true; |
| 733 | list_add_rcu(&range->list, &hmm->ranges); |
| 734 | spin_unlock(&hmm->lock); |
| 735 | |
| 736 | /* FIXME support hugetlb fs */ |
| 737 | if (is_vm_hugetlb_page(vma) || (vma->vm_flags & VM_SPECIAL)) { |
Jérôme Glisse | 08232a4 | 2018-04-10 16:28:30 -0700 | [diff] [blame^] | 738 | hmm_pfns_special(range->pfns, range->start, range->end); |
Jérôme Glisse | 74eee18 | 2017-09-08 16:11:35 -0700 | [diff] [blame] | 739 | return 0; |
| 740 | } |
| 741 | |
| 742 | hmm_vma_walk.fault = true; |
| 743 | hmm_vma_walk.write = write; |
| 744 | hmm_vma_walk.block = block; |
| 745 | hmm_vma_walk.range = range; |
| 746 | mm_walk.private = &hmm_vma_walk; |
| 747 | hmm_vma_walk.last = range->start; |
| 748 | |
| 749 | mm_walk.vma = vma; |
| 750 | mm_walk.mm = vma->vm_mm; |
| 751 | mm_walk.pte_entry = NULL; |
| 752 | mm_walk.test_walk = NULL; |
| 753 | mm_walk.hugetlb_entry = NULL; |
| 754 | mm_walk.pmd_entry = hmm_vma_walk_pmd; |
| 755 | mm_walk.pte_hole = hmm_vma_walk_hole; |
| 756 | |
| 757 | do { |
Jérôme Glisse | 08232a4 | 2018-04-10 16:28:30 -0700 | [diff] [blame^] | 758 | ret = walk_page_range(start, range->end, &mm_walk); |
Jérôme Glisse | 74eee18 | 2017-09-08 16:11:35 -0700 | [diff] [blame] | 759 | start = hmm_vma_walk.last; |
| 760 | } while (ret == -EAGAIN); |
| 761 | |
| 762 | if (ret) { |
| 763 | unsigned long i; |
| 764 | |
| 765 | i = (hmm_vma_walk.last - range->start) >> PAGE_SHIFT; |
Jérôme Glisse | 08232a4 | 2018-04-10 16:28:30 -0700 | [diff] [blame^] | 766 | hmm_pfns_clear(&range->pfns[i], hmm_vma_walk.last, range->end); |
| 767 | hmm_vma_range_done(range); |
Jérôme Glisse | 74eee18 | 2017-09-08 16:11:35 -0700 | [diff] [blame] | 768 | } |
| 769 | return ret; |
| 770 | } |
| 771 | EXPORT_SYMBOL(hmm_vma_fault); |
Jérôme Glisse | c0b1240 | 2017-09-08 16:11:27 -0700 | [diff] [blame] | 772 | #endif /* IS_ENABLED(CONFIG_HMM_MIRROR) */ |
Jérôme Glisse | 4ef589d | 2017-09-08 16:11:58 -0700 | [diff] [blame] | 773 | |
| 774 | |
Jérôme Glisse | df6ad69 | 2017-09-08 16:12:24 -0700 | [diff] [blame] | 775 | #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] | 776 | struct page *hmm_vma_alloc_locked_page(struct vm_area_struct *vma, |
| 777 | unsigned long addr) |
| 778 | { |
| 779 | struct page *page; |
| 780 | |
| 781 | page = alloc_page_vma(GFP_HIGHUSER, vma, addr); |
| 782 | if (!page) |
| 783 | return NULL; |
| 784 | lock_page(page); |
| 785 | return page; |
| 786 | } |
| 787 | EXPORT_SYMBOL(hmm_vma_alloc_locked_page); |
| 788 | |
| 789 | |
| 790 | static void hmm_devmem_ref_release(struct percpu_ref *ref) |
| 791 | { |
| 792 | struct hmm_devmem *devmem; |
| 793 | |
| 794 | devmem = container_of(ref, struct hmm_devmem, ref); |
| 795 | complete(&devmem->completion); |
| 796 | } |
| 797 | |
| 798 | static void hmm_devmem_ref_exit(void *data) |
| 799 | { |
| 800 | struct percpu_ref *ref = data; |
| 801 | struct hmm_devmem *devmem; |
| 802 | |
| 803 | devmem = container_of(ref, struct hmm_devmem, ref); |
| 804 | percpu_ref_exit(ref); |
| 805 | devm_remove_action(devmem->device, &hmm_devmem_ref_exit, data); |
| 806 | } |
| 807 | |
| 808 | static void hmm_devmem_ref_kill(void *data) |
| 809 | { |
| 810 | struct percpu_ref *ref = data; |
| 811 | struct hmm_devmem *devmem; |
| 812 | |
| 813 | devmem = container_of(ref, struct hmm_devmem, ref); |
| 814 | percpu_ref_kill(ref); |
| 815 | wait_for_completion(&devmem->completion); |
| 816 | devm_remove_action(devmem->device, &hmm_devmem_ref_kill, data); |
| 817 | } |
| 818 | |
| 819 | static int hmm_devmem_fault(struct vm_area_struct *vma, |
| 820 | unsigned long addr, |
| 821 | const struct page *page, |
| 822 | unsigned int flags, |
| 823 | pmd_t *pmdp) |
| 824 | { |
| 825 | struct hmm_devmem *devmem = page->pgmap->data; |
| 826 | |
| 827 | return devmem->ops->fault(devmem, vma, addr, page, flags, pmdp); |
| 828 | } |
| 829 | |
| 830 | static void hmm_devmem_free(struct page *page, void *data) |
| 831 | { |
| 832 | struct hmm_devmem *devmem = data; |
| 833 | |
| 834 | devmem->ops->free(devmem, page); |
| 835 | } |
| 836 | |
| 837 | static DEFINE_MUTEX(hmm_devmem_lock); |
| 838 | static RADIX_TREE(hmm_devmem_radix, GFP_KERNEL); |
| 839 | |
| 840 | static void hmm_devmem_radix_release(struct resource *resource) |
| 841 | { |
Colin Ian King | fec11bc | 2017-11-15 17:38:52 -0800 | [diff] [blame] | 842 | resource_size_t key, align_start, align_size; |
Jérôme Glisse | 4ef589d | 2017-09-08 16:11:58 -0700 | [diff] [blame] | 843 | |
| 844 | align_start = resource->start & ~(PA_SECTION_SIZE - 1); |
| 845 | align_size = ALIGN(resource_size(resource), PA_SECTION_SIZE); |
Jérôme Glisse | 4ef589d | 2017-09-08 16:11:58 -0700 | [diff] [blame] | 846 | |
| 847 | mutex_lock(&hmm_devmem_lock); |
| 848 | for (key = resource->start; |
| 849 | key <= resource->end; |
| 850 | key += PA_SECTION_SIZE) |
| 851 | radix_tree_delete(&hmm_devmem_radix, key >> PA_SECTION_SHIFT); |
| 852 | mutex_unlock(&hmm_devmem_lock); |
| 853 | } |
| 854 | |
| 855 | static void hmm_devmem_release(struct device *dev, void *data) |
| 856 | { |
| 857 | struct hmm_devmem *devmem = data; |
| 858 | struct resource *resource = devmem->resource; |
| 859 | unsigned long start_pfn, npages; |
| 860 | struct zone *zone; |
| 861 | struct page *page; |
| 862 | |
| 863 | if (percpu_ref_tryget_live(&devmem->ref)) { |
| 864 | dev_WARN(dev, "%s: page mapping is still live!\n", __func__); |
| 865 | percpu_ref_put(&devmem->ref); |
| 866 | } |
| 867 | |
| 868 | /* pages are dead and unused, undo the arch mapping */ |
| 869 | start_pfn = (resource->start & ~(PA_SECTION_SIZE - 1)) >> PAGE_SHIFT; |
| 870 | npages = ALIGN(resource_size(resource), PA_SECTION_SIZE) >> PAGE_SHIFT; |
| 871 | |
| 872 | page = pfn_to_page(start_pfn); |
| 873 | zone = page_zone(page); |
| 874 | |
| 875 | mem_hotplug_begin(); |
Jérôme Glisse | d3df0a4 | 2017-09-08 16:12:28 -0700 | [diff] [blame] | 876 | if (resource->desc == IORES_DESC_DEVICE_PRIVATE_MEMORY) |
Christoph Hellwig | da02451 | 2017-12-29 08:53:55 +0100 | [diff] [blame] | 877 | __remove_pages(zone, start_pfn, npages, NULL); |
Jérôme Glisse | d3df0a4 | 2017-09-08 16:12:28 -0700 | [diff] [blame] | 878 | else |
| 879 | arch_remove_memory(start_pfn << PAGE_SHIFT, |
Christoph Hellwig | da02451 | 2017-12-29 08:53:55 +0100 | [diff] [blame] | 880 | npages << PAGE_SHIFT, NULL); |
Jérôme Glisse | 4ef589d | 2017-09-08 16:11:58 -0700 | [diff] [blame] | 881 | mem_hotplug_done(); |
| 882 | |
| 883 | hmm_devmem_radix_release(resource); |
| 884 | } |
| 885 | |
| 886 | static struct hmm_devmem *hmm_devmem_find(resource_size_t phys) |
| 887 | { |
| 888 | WARN_ON_ONCE(!rcu_read_lock_held()); |
| 889 | |
| 890 | return radix_tree_lookup(&hmm_devmem_radix, phys >> PA_SECTION_SHIFT); |
| 891 | } |
| 892 | |
| 893 | static int hmm_devmem_pages_create(struct hmm_devmem *devmem) |
| 894 | { |
| 895 | resource_size_t key, align_start, align_size, align_end; |
| 896 | struct device *device = devmem->device; |
| 897 | int ret, nid, is_ram; |
| 898 | unsigned long pfn; |
| 899 | |
| 900 | align_start = devmem->resource->start & ~(PA_SECTION_SIZE - 1); |
| 901 | align_size = ALIGN(devmem->resource->start + |
| 902 | resource_size(devmem->resource), |
| 903 | PA_SECTION_SIZE) - align_start; |
| 904 | |
| 905 | is_ram = region_intersects(align_start, align_size, |
| 906 | IORESOURCE_SYSTEM_RAM, |
| 907 | IORES_DESC_NONE); |
| 908 | if (is_ram == REGION_MIXED) { |
| 909 | WARN_ONCE(1, "%s attempted on mixed region %pr\n", |
| 910 | __func__, devmem->resource); |
| 911 | return -ENXIO; |
| 912 | } |
| 913 | if (is_ram == REGION_INTERSECTS) |
| 914 | return -ENXIO; |
| 915 | |
Jérôme Glisse | d3df0a4 | 2017-09-08 16:12:28 -0700 | [diff] [blame] | 916 | if (devmem->resource->desc == IORES_DESC_DEVICE_PUBLIC_MEMORY) |
| 917 | devmem->pagemap.type = MEMORY_DEVICE_PUBLIC; |
| 918 | else |
| 919 | devmem->pagemap.type = MEMORY_DEVICE_PRIVATE; |
| 920 | |
Logan Gunthorpe | e7744aa | 2017-12-29 08:54:04 +0100 | [diff] [blame] | 921 | devmem->pagemap.res = *devmem->resource; |
Jérôme Glisse | 4ef589d | 2017-09-08 16:11:58 -0700 | [diff] [blame] | 922 | devmem->pagemap.page_fault = hmm_devmem_fault; |
| 923 | devmem->pagemap.page_free = hmm_devmem_free; |
| 924 | devmem->pagemap.dev = devmem->device; |
| 925 | devmem->pagemap.ref = &devmem->ref; |
| 926 | devmem->pagemap.data = devmem; |
| 927 | |
| 928 | mutex_lock(&hmm_devmem_lock); |
| 929 | align_end = align_start + align_size - 1; |
| 930 | for (key = align_start; key <= align_end; key += PA_SECTION_SIZE) { |
| 931 | struct hmm_devmem *dup; |
| 932 | |
| 933 | rcu_read_lock(); |
| 934 | dup = hmm_devmem_find(key); |
| 935 | rcu_read_unlock(); |
| 936 | if (dup) { |
| 937 | dev_err(device, "%s: collides with mapping for %s\n", |
| 938 | __func__, dev_name(dup->device)); |
| 939 | mutex_unlock(&hmm_devmem_lock); |
| 940 | ret = -EBUSY; |
| 941 | goto error; |
| 942 | } |
| 943 | ret = radix_tree_insert(&hmm_devmem_radix, |
| 944 | key >> PA_SECTION_SHIFT, |
| 945 | devmem); |
| 946 | if (ret) { |
| 947 | dev_err(device, "%s: failed: %d\n", __func__, ret); |
| 948 | mutex_unlock(&hmm_devmem_lock); |
| 949 | goto error_radix; |
| 950 | } |
| 951 | } |
| 952 | mutex_unlock(&hmm_devmem_lock); |
| 953 | |
| 954 | nid = dev_to_node(device); |
| 955 | if (nid < 0) |
| 956 | nid = numa_mem_id(); |
| 957 | |
| 958 | mem_hotplug_begin(); |
| 959 | /* |
| 960 | * For device private memory we call add_pages() as we only need to |
| 961 | * allocate and initialize struct page for the device memory. More- |
| 962 | * over the device memory is un-accessible thus we do not want to |
| 963 | * create a linear mapping for the memory like arch_add_memory() |
| 964 | * would do. |
Jérôme Glisse | d3df0a4 | 2017-09-08 16:12:28 -0700 | [diff] [blame] | 965 | * |
| 966 | * For device public memory, which is accesible by the CPU, we do |
| 967 | * want the linear mapping and thus use arch_add_memory(). |
Jérôme Glisse | 4ef589d | 2017-09-08 16:11:58 -0700 | [diff] [blame] | 968 | */ |
Jérôme Glisse | d3df0a4 | 2017-09-08 16:12:28 -0700 | [diff] [blame] | 969 | if (devmem->pagemap.type == MEMORY_DEVICE_PUBLIC) |
Christoph Hellwig | 24e6d5a | 2017-12-29 08:53:53 +0100 | [diff] [blame] | 970 | ret = arch_add_memory(nid, align_start, align_size, NULL, |
| 971 | false); |
Jérôme Glisse | d3df0a4 | 2017-09-08 16:12:28 -0700 | [diff] [blame] | 972 | else |
| 973 | ret = add_pages(nid, align_start >> PAGE_SHIFT, |
Christoph Hellwig | 24e6d5a | 2017-12-29 08:53:53 +0100 | [diff] [blame] | 974 | align_size >> PAGE_SHIFT, NULL, false); |
Jérôme Glisse | 4ef589d | 2017-09-08 16:11:58 -0700 | [diff] [blame] | 975 | if (ret) { |
| 976 | mem_hotplug_done(); |
| 977 | goto error_add_memory; |
| 978 | } |
| 979 | move_pfn_range_to_zone(&NODE_DATA(nid)->node_zones[ZONE_DEVICE], |
| 980 | align_start >> PAGE_SHIFT, |
Christoph Hellwig | a99583e | 2017-12-29 08:53:57 +0100 | [diff] [blame] | 981 | align_size >> PAGE_SHIFT, NULL); |
Jérôme Glisse | 4ef589d | 2017-09-08 16:11:58 -0700 | [diff] [blame] | 982 | mem_hotplug_done(); |
| 983 | |
| 984 | for (pfn = devmem->pfn_first; pfn < devmem->pfn_last; pfn++) { |
| 985 | struct page *page = pfn_to_page(pfn); |
| 986 | |
| 987 | page->pgmap = &devmem->pagemap; |
| 988 | } |
| 989 | return 0; |
| 990 | |
| 991 | error_add_memory: |
| 992 | untrack_pfn(NULL, PHYS_PFN(align_start), align_size); |
| 993 | error_radix: |
| 994 | hmm_devmem_radix_release(devmem->resource); |
| 995 | error: |
| 996 | return ret; |
| 997 | } |
| 998 | |
| 999 | static int hmm_devmem_match(struct device *dev, void *data, void *match_data) |
| 1000 | { |
| 1001 | struct hmm_devmem *devmem = data; |
| 1002 | |
| 1003 | return devmem->resource == match_data; |
| 1004 | } |
| 1005 | |
| 1006 | static void hmm_devmem_pages_remove(struct hmm_devmem *devmem) |
| 1007 | { |
| 1008 | devres_release(devmem->device, &hmm_devmem_release, |
| 1009 | &hmm_devmem_match, devmem->resource); |
| 1010 | } |
| 1011 | |
| 1012 | /* |
| 1013 | * hmm_devmem_add() - hotplug ZONE_DEVICE memory for device memory |
| 1014 | * |
| 1015 | * @ops: memory event device driver callback (see struct hmm_devmem_ops) |
| 1016 | * @device: device struct to bind the resource too |
| 1017 | * @size: size in bytes of the device memory to add |
| 1018 | * Returns: pointer to new hmm_devmem struct ERR_PTR otherwise |
| 1019 | * |
| 1020 | * This function first finds an empty range of physical address big enough to |
| 1021 | * contain the new resource, and then hotplugs it as ZONE_DEVICE memory, which |
| 1022 | * in turn allocates struct pages. It does not do anything beyond that; all |
| 1023 | * events affecting the memory will go through the various callbacks provided |
| 1024 | * by hmm_devmem_ops struct. |
| 1025 | * |
| 1026 | * Device driver should call this function during device initialization and |
| 1027 | * is then responsible of memory management. HMM only provides helpers. |
| 1028 | */ |
| 1029 | struct hmm_devmem *hmm_devmem_add(const struct hmm_devmem_ops *ops, |
| 1030 | struct device *device, |
| 1031 | unsigned long size) |
| 1032 | { |
| 1033 | struct hmm_devmem *devmem; |
| 1034 | resource_size_t addr; |
| 1035 | int ret; |
| 1036 | |
| 1037 | static_branch_enable(&device_private_key); |
| 1038 | |
| 1039 | devmem = devres_alloc_node(&hmm_devmem_release, sizeof(*devmem), |
| 1040 | GFP_KERNEL, dev_to_node(device)); |
| 1041 | if (!devmem) |
| 1042 | return ERR_PTR(-ENOMEM); |
| 1043 | |
| 1044 | init_completion(&devmem->completion); |
| 1045 | devmem->pfn_first = -1UL; |
| 1046 | devmem->pfn_last = -1UL; |
| 1047 | devmem->resource = NULL; |
| 1048 | devmem->device = device; |
| 1049 | devmem->ops = ops; |
| 1050 | |
| 1051 | ret = percpu_ref_init(&devmem->ref, &hmm_devmem_ref_release, |
| 1052 | 0, GFP_KERNEL); |
| 1053 | if (ret) |
| 1054 | goto error_percpu_ref; |
| 1055 | |
| 1056 | ret = devm_add_action(device, hmm_devmem_ref_exit, &devmem->ref); |
| 1057 | if (ret) |
| 1058 | goto error_devm_add_action; |
| 1059 | |
| 1060 | size = ALIGN(size, PA_SECTION_SIZE); |
| 1061 | addr = min((unsigned long)iomem_resource.end, |
| 1062 | (1UL << MAX_PHYSMEM_BITS) - 1); |
| 1063 | addr = addr - size + 1UL; |
| 1064 | |
| 1065 | /* |
| 1066 | * FIXME add a new helper to quickly walk resource tree and find free |
| 1067 | * range |
| 1068 | * |
| 1069 | * FIXME what about ioport_resource resource ? |
| 1070 | */ |
| 1071 | for (; addr > size && addr >= iomem_resource.start; addr -= size) { |
| 1072 | ret = region_intersects(addr, size, 0, IORES_DESC_NONE); |
| 1073 | if (ret != REGION_DISJOINT) |
| 1074 | continue; |
| 1075 | |
| 1076 | devmem->resource = devm_request_mem_region(device, addr, size, |
| 1077 | dev_name(device)); |
| 1078 | if (!devmem->resource) { |
| 1079 | ret = -ENOMEM; |
| 1080 | goto error_no_resource; |
| 1081 | } |
| 1082 | break; |
| 1083 | } |
| 1084 | if (!devmem->resource) { |
| 1085 | ret = -ERANGE; |
| 1086 | goto error_no_resource; |
| 1087 | } |
| 1088 | |
| 1089 | devmem->resource->desc = IORES_DESC_DEVICE_PRIVATE_MEMORY; |
| 1090 | devmem->pfn_first = devmem->resource->start >> PAGE_SHIFT; |
| 1091 | devmem->pfn_last = devmem->pfn_first + |
| 1092 | (resource_size(devmem->resource) >> PAGE_SHIFT); |
| 1093 | |
| 1094 | ret = hmm_devmem_pages_create(devmem); |
| 1095 | if (ret) |
| 1096 | goto error_pages; |
| 1097 | |
| 1098 | devres_add(device, devmem); |
| 1099 | |
| 1100 | ret = devm_add_action(device, hmm_devmem_ref_kill, &devmem->ref); |
| 1101 | if (ret) { |
| 1102 | hmm_devmem_remove(devmem); |
| 1103 | return ERR_PTR(ret); |
| 1104 | } |
| 1105 | |
| 1106 | return devmem; |
| 1107 | |
| 1108 | error_pages: |
| 1109 | devm_release_mem_region(device, devmem->resource->start, |
| 1110 | resource_size(devmem->resource)); |
| 1111 | error_no_resource: |
| 1112 | error_devm_add_action: |
| 1113 | hmm_devmem_ref_kill(&devmem->ref); |
| 1114 | hmm_devmem_ref_exit(&devmem->ref); |
| 1115 | error_percpu_ref: |
| 1116 | devres_free(devmem); |
| 1117 | return ERR_PTR(ret); |
| 1118 | } |
| 1119 | EXPORT_SYMBOL(hmm_devmem_add); |
| 1120 | |
Jérôme Glisse | d3df0a4 | 2017-09-08 16:12:28 -0700 | [diff] [blame] | 1121 | struct hmm_devmem *hmm_devmem_add_resource(const struct hmm_devmem_ops *ops, |
| 1122 | struct device *device, |
| 1123 | struct resource *res) |
| 1124 | { |
| 1125 | struct hmm_devmem *devmem; |
| 1126 | int ret; |
| 1127 | |
| 1128 | if (res->desc != IORES_DESC_DEVICE_PUBLIC_MEMORY) |
| 1129 | return ERR_PTR(-EINVAL); |
| 1130 | |
| 1131 | static_branch_enable(&device_private_key); |
| 1132 | |
| 1133 | devmem = devres_alloc_node(&hmm_devmem_release, sizeof(*devmem), |
| 1134 | GFP_KERNEL, dev_to_node(device)); |
| 1135 | if (!devmem) |
| 1136 | return ERR_PTR(-ENOMEM); |
| 1137 | |
| 1138 | init_completion(&devmem->completion); |
| 1139 | devmem->pfn_first = -1UL; |
| 1140 | devmem->pfn_last = -1UL; |
| 1141 | devmem->resource = res; |
| 1142 | devmem->device = device; |
| 1143 | devmem->ops = ops; |
| 1144 | |
| 1145 | ret = percpu_ref_init(&devmem->ref, &hmm_devmem_ref_release, |
| 1146 | 0, GFP_KERNEL); |
| 1147 | if (ret) |
| 1148 | goto error_percpu_ref; |
| 1149 | |
| 1150 | ret = devm_add_action(device, hmm_devmem_ref_exit, &devmem->ref); |
| 1151 | if (ret) |
| 1152 | goto error_devm_add_action; |
| 1153 | |
| 1154 | |
| 1155 | devmem->pfn_first = devmem->resource->start >> PAGE_SHIFT; |
| 1156 | devmem->pfn_last = devmem->pfn_first + |
| 1157 | (resource_size(devmem->resource) >> PAGE_SHIFT); |
| 1158 | |
| 1159 | ret = hmm_devmem_pages_create(devmem); |
| 1160 | if (ret) |
| 1161 | goto error_devm_add_action; |
| 1162 | |
| 1163 | devres_add(device, devmem); |
| 1164 | |
| 1165 | ret = devm_add_action(device, hmm_devmem_ref_kill, &devmem->ref); |
| 1166 | if (ret) { |
| 1167 | hmm_devmem_remove(devmem); |
| 1168 | return ERR_PTR(ret); |
| 1169 | } |
| 1170 | |
| 1171 | return devmem; |
| 1172 | |
| 1173 | error_devm_add_action: |
| 1174 | hmm_devmem_ref_kill(&devmem->ref); |
| 1175 | hmm_devmem_ref_exit(&devmem->ref); |
| 1176 | error_percpu_ref: |
| 1177 | devres_free(devmem); |
| 1178 | return ERR_PTR(ret); |
| 1179 | } |
| 1180 | EXPORT_SYMBOL(hmm_devmem_add_resource); |
| 1181 | |
Jérôme Glisse | 4ef589d | 2017-09-08 16:11:58 -0700 | [diff] [blame] | 1182 | /* |
| 1183 | * hmm_devmem_remove() - remove device memory (kill and free ZONE_DEVICE) |
| 1184 | * |
| 1185 | * @devmem: hmm_devmem struct use to track and manage the ZONE_DEVICE memory |
| 1186 | * |
| 1187 | * This will hot-unplug memory that was hotplugged by hmm_devmem_add on behalf |
| 1188 | * of the device driver. It will free struct page and remove the resource that |
| 1189 | * reserved the physical address range for this device memory. |
| 1190 | */ |
| 1191 | void hmm_devmem_remove(struct hmm_devmem *devmem) |
| 1192 | { |
| 1193 | resource_size_t start, size; |
| 1194 | struct device *device; |
Jérôme Glisse | d3df0a4 | 2017-09-08 16:12:28 -0700 | [diff] [blame] | 1195 | bool cdm = false; |
Jérôme Glisse | 4ef589d | 2017-09-08 16:11:58 -0700 | [diff] [blame] | 1196 | |
| 1197 | if (!devmem) |
| 1198 | return; |
| 1199 | |
| 1200 | device = devmem->device; |
| 1201 | start = devmem->resource->start; |
| 1202 | size = resource_size(devmem->resource); |
| 1203 | |
Jérôme Glisse | d3df0a4 | 2017-09-08 16:12:28 -0700 | [diff] [blame] | 1204 | cdm = devmem->resource->desc == IORES_DESC_DEVICE_PUBLIC_MEMORY; |
Jérôme Glisse | 4ef589d | 2017-09-08 16:11:58 -0700 | [diff] [blame] | 1205 | hmm_devmem_ref_kill(&devmem->ref); |
| 1206 | hmm_devmem_ref_exit(&devmem->ref); |
| 1207 | hmm_devmem_pages_remove(devmem); |
| 1208 | |
Jérôme Glisse | d3df0a4 | 2017-09-08 16:12:28 -0700 | [diff] [blame] | 1209 | if (!cdm) |
| 1210 | devm_release_mem_region(device, start, size); |
Jérôme Glisse | 4ef589d | 2017-09-08 16:11:58 -0700 | [diff] [blame] | 1211 | } |
| 1212 | EXPORT_SYMBOL(hmm_devmem_remove); |
Jérôme Glisse | 858b54d | 2017-09-08 16:12:02 -0700 | [diff] [blame] | 1213 | |
| 1214 | /* |
| 1215 | * A device driver that wants to handle multiple devices memory through a |
| 1216 | * single fake device can use hmm_device to do so. This is purely a helper |
| 1217 | * and it is not needed to make use of any HMM functionality. |
| 1218 | */ |
| 1219 | #define HMM_DEVICE_MAX 256 |
| 1220 | |
| 1221 | static DECLARE_BITMAP(hmm_device_mask, HMM_DEVICE_MAX); |
| 1222 | static DEFINE_SPINLOCK(hmm_device_lock); |
| 1223 | static struct class *hmm_device_class; |
| 1224 | static dev_t hmm_device_devt; |
| 1225 | |
| 1226 | static void hmm_device_release(struct device *device) |
| 1227 | { |
| 1228 | struct hmm_device *hmm_device; |
| 1229 | |
| 1230 | hmm_device = container_of(device, struct hmm_device, device); |
| 1231 | spin_lock(&hmm_device_lock); |
| 1232 | clear_bit(hmm_device->minor, hmm_device_mask); |
| 1233 | spin_unlock(&hmm_device_lock); |
| 1234 | |
| 1235 | kfree(hmm_device); |
| 1236 | } |
| 1237 | |
| 1238 | struct hmm_device *hmm_device_new(void *drvdata) |
| 1239 | { |
| 1240 | struct hmm_device *hmm_device; |
| 1241 | |
| 1242 | hmm_device = kzalloc(sizeof(*hmm_device), GFP_KERNEL); |
| 1243 | if (!hmm_device) |
| 1244 | return ERR_PTR(-ENOMEM); |
| 1245 | |
| 1246 | spin_lock(&hmm_device_lock); |
| 1247 | hmm_device->minor = find_first_zero_bit(hmm_device_mask, HMM_DEVICE_MAX); |
| 1248 | if (hmm_device->minor >= HMM_DEVICE_MAX) { |
| 1249 | spin_unlock(&hmm_device_lock); |
| 1250 | kfree(hmm_device); |
| 1251 | return ERR_PTR(-EBUSY); |
| 1252 | } |
| 1253 | set_bit(hmm_device->minor, hmm_device_mask); |
| 1254 | spin_unlock(&hmm_device_lock); |
| 1255 | |
| 1256 | dev_set_name(&hmm_device->device, "hmm_device%d", hmm_device->minor); |
| 1257 | hmm_device->device.devt = MKDEV(MAJOR(hmm_device_devt), |
| 1258 | hmm_device->minor); |
| 1259 | hmm_device->device.release = hmm_device_release; |
| 1260 | dev_set_drvdata(&hmm_device->device, drvdata); |
| 1261 | hmm_device->device.class = hmm_device_class; |
| 1262 | device_initialize(&hmm_device->device); |
| 1263 | |
| 1264 | return hmm_device; |
| 1265 | } |
| 1266 | EXPORT_SYMBOL(hmm_device_new); |
| 1267 | |
| 1268 | void hmm_device_put(struct hmm_device *hmm_device) |
| 1269 | { |
| 1270 | put_device(&hmm_device->device); |
| 1271 | } |
| 1272 | EXPORT_SYMBOL(hmm_device_put); |
| 1273 | |
| 1274 | static int __init hmm_init(void) |
| 1275 | { |
| 1276 | int ret; |
| 1277 | |
| 1278 | ret = alloc_chrdev_region(&hmm_device_devt, 0, |
| 1279 | HMM_DEVICE_MAX, |
| 1280 | "hmm_device"); |
| 1281 | if (ret) |
| 1282 | return ret; |
| 1283 | |
| 1284 | hmm_device_class = class_create(THIS_MODULE, "hmm_device"); |
| 1285 | if (IS_ERR(hmm_device_class)) { |
| 1286 | unregister_chrdev_region(hmm_device_devt, HMM_DEVICE_MAX); |
| 1287 | return PTR_ERR(hmm_device_class); |
| 1288 | } |
| 1289 | return 0; |
| 1290 | } |
| 1291 | |
| 1292 | device_initcall(hmm_init); |
Jérôme Glisse | df6ad69 | 2017-09-08 16:12:24 -0700 | [diff] [blame] | 1293 | #endif /* CONFIG_DEVICE_PRIVATE || CONFIG_DEVICE_PUBLIC */ |