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