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