Thomas Gleixner | c942fdd | 2019-05-27 08:55:06 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
Jérôme Glisse | 133ff0e | 2017-09-08 16:11:23 -0700 | [diff] [blame] | 2 | /* |
| 3 | * Copyright 2013 Red Hat Inc. |
| 4 | * |
Jérôme Glisse | f813f21 | 2018-10-30 15:04:06 -0700 | [diff] [blame] | 5 | * Authors: Jérôme Glisse <jglisse@redhat.com> |
Jérôme Glisse | 133ff0e | 2017-09-08 16:11:23 -0700 | [diff] [blame] | 6 | */ |
| 7 | /* |
| 8 | * Refer to include/linux/hmm.h for information about heterogeneous memory |
| 9 | * management or HMM for short. |
| 10 | */ |
| 11 | #include <linux/mm.h> |
| 12 | #include <linux/hmm.h> |
Jérôme Glisse | 858b54d | 2017-09-08 16:12:02 -0700 | [diff] [blame] | 13 | #include <linux/init.h> |
Jérôme Glisse | da4c3c7 | 2017-09-08 16:11:31 -0700 | [diff] [blame] | 14 | #include <linux/rmap.h> |
| 15 | #include <linux/swap.h> |
Jérôme Glisse | 133ff0e | 2017-09-08 16:11:23 -0700 | [diff] [blame] | 16 | #include <linux/slab.h> |
| 17 | #include <linux/sched.h> |
Jérôme Glisse | 4ef589d | 2017-09-08 16:11:58 -0700 | [diff] [blame] | 18 | #include <linux/mmzone.h> |
| 19 | #include <linux/pagemap.h> |
Jérôme Glisse | da4c3c7 | 2017-09-08 16:11:31 -0700 | [diff] [blame] | 20 | #include <linux/swapops.h> |
| 21 | #include <linux/hugetlb.h> |
Jérôme Glisse | 4ef589d | 2017-09-08 16:11:58 -0700 | [diff] [blame] | 22 | #include <linux/memremap.h> |
Jason Gunthorpe | c8a53b2 | 2019-05-23 10:36:46 -0300 | [diff] [blame] | 23 | #include <linux/sched/mm.h> |
Jérôme Glisse | 7b2d55d2 | 2017-09-08 16:11:46 -0700 | [diff] [blame] | 24 | #include <linux/jump_label.h> |
Jérôme Glisse | 55c0ece | 2019-05-13 17:20:28 -0700 | [diff] [blame] | 25 | #include <linux/dma-mapping.h> |
Jérôme Glisse | c0b1240 | 2017-09-08 16:11:27 -0700 | [diff] [blame] | 26 | #include <linux/mmu_notifier.h> |
Jérôme Glisse | 4ef589d | 2017-09-08 16:11:58 -0700 | [diff] [blame] | 27 | #include <linux/memory_hotplug.h> |
| 28 | |
Jérôme Glisse | c0b1240 | 2017-09-08 16:11:27 -0700 | [diff] [blame] | 29 | static const struct mmu_notifier_ops hmm_mmu_notifier_ops; |
| 30 | |
Jérôme Glisse | 704f3f2 | 2019-05-13 17:19:48 -0700 | [diff] [blame] | 31 | /** |
| 32 | * hmm_get_or_create - register HMM against an mm (HMM internal) |
| 33 | * |
| 34 | * @mm: mm struct to attach to |
Ralph Campbell | d2e8d55 | 2019-07-25 17:56:45 -0700 | [diff] [blame] | 35 | * Return: an HMM object, either by referencing the existing |
Jérôme Glisse | 704f3f2 | 2019-05-13 17:19:48 -0700 | [diff] [blame] | 36 | * (per-process) object, or by creating a new one. |
| 37 | * |
| 38 | * This is not intended to be used directly by device drivers. If mm already |
| 39 | * has an HMM struct then it get a reference on it and returns it. Otherwise |
| 40 | * it allocates an HMM struct, initializes it, associate it with the mm and |
| 41 | * returns it. |
| 42 | */ |
| 43 | static struct hmm *hmm_get_or_create(struct mm_struct *mm) |
| 44 | { |
Jason Gunthorpe | 8a9320b | 2019-05-23 10:24:13 -0300 | [diff] [blame] | 45 | struct hmm *hmm; |
Jérôme Glisse | 133ff0e | 2017-09-08 16:11:23 -0700 | [diff] [blame] | 46 | |
Linus Torvalds | fec88ab | 2019-07-14 19:42:11 -0700 | [diff] [blame] | 47 | lockdep_assert_held_write(&mm->mmap_sem); |
Jason Gunthorpe | 8a9320b | 2019-05-23 10:24:13 -0300 | [diff] [blame] | 48 | |
| 49 | /* Abuse the page_table_lock to also protect mm->hmm. */ |
| 50 | spin_lock(&mm->page_table_lock); |
| 51 | hmm = mm->hmm; |
| 52 | if (mm->hmm && kref_get_unless_zero(&mm->hmm->kref)) |
| 53 | goto out_unlock; |
| 54 | spin_unlock(&mm->page_table_lock); |
Jérôme Glisse | c0b1240 | 2017-09-08 16:11:27 -0700 | [diff] [blame] | 55 | |
| 56 | hmm = kmalloc(sizeof(*hmm), GFP_KERNEL); |
| 57 | if (!hmm) |
| 58 | return NULL; |
Jérôme Glisse | a3e0d41 | 2019-05-13 17:20:01 -0700 | [diff] [blame] | 59 | init_waitqueue_head(&hmm->wq); |
Jérôme Glisse | c0b1240 | 2017-09-08 16:11:27 -0700 | [diff] [blame] | 60 | INIT_LIST_HEAD(&hmm->mirrors); |
| 61 | init_rwsem(&hmm->mirrors_sem); |
Jérôme Glisse | c0b1240 | 2017-09-08 16:11:27 -0700 | [diff] [blame] | 62 | hmm->mmu_notifier.ops = NULL; |
Jérôme Glisse | da4c3c7 | 2017-09-08 16:11:31 -0700 | [diff] [blame] | 63 | INIT_LIST_HEAD(&hmm->ranges); |
Jason Gunthorpe | 5a136b4 | 2019-06-07 12:10:33 -0300 | [diff] [blame] | 64 | spin_lock_init(&hmm->ranges_lock); |
Jérôme Glisse | 704f3f2 | 2019-05-13 17:19:48 -0700 | [diff] [blame] | 65 | kref_init(&hmm->kref); |
Jérôme Glisse | a3e0d41 | 2019-05-13 17:20:01 -0700 | [diff] [blame] | 66 | hmm->notifiers = 0; |
Jérôme Glisse | c0b1240 | 2017-09-08 16:11:27 -0700 | [diff] [blame] | 67 | hmm->mm = mm; |
| 68 | |
Jason Gunthorpe | 8a9320b | 2019-05-23 10:24:13 -0300 | [diff] [blame] | 69 | hmm->mmu_notifier.ops = &hmm_mmu_notifier_ops; |
| 70 | if (__mmu_notifier_register(&hmm->mmu_notifier, mm)) { |
| 71 | kfree(hmm); |
| 72 | return NULL; |
| 73 | } |
Jérôme Glisse | c0b1240 | 2017-09-08 16:11:27 -0700 | [diff] [blame] | 74 | |
Jason Gunthorpe | c8a53b2 | 2019-05-23 10:36:46 -0300 | [diff] [blame] | 75 | mmgrab(hmm->mm); |
Ralph Campbell | 86a2d59 | 2018-10-30 15:04:14 -0700 | [diff] [blame] | 76 | |
| 77 | /* |
Jason Gunthorpe | 8a9320b | 2019-05-23 10:24:13 -0300 | [diff] [blame] | 78 | * We hold the exclusive mmap_sem here so we know that mm->hmm is |
| 79 | * still NULL or 0 kref, and is safe to update. |
Ralph Campbell | 86a2d59 | 2018-10-30 15:04:14 -0700 | [diff] [blame] | 80 | */ |
Ralph Campbell | 86a2d59 | 2018-10-30 15:04:14 -0700 | [diff] [blame] | 81 | spin_lock(&mm->page_table_lock); |
Jason Gunthorpe | 8a9320b | 2019-05-23 10:24:13 -0300 | [diff] [blame] | 82 | mm->hmm = hmm; |
| 83 | |
| 84 | out_unlock: |
Ralph Campbell | 86a2d59 | 2018-10-30 15:04:14 -0700 | [diff] [blame] | 85 | spin_unlock(&mm->page_table_lock); |
Jason Gunthorpe | 8a9320b | 2019-05-23 10:24:13 -0300 | [diff] [blame] | 86 | return hmm; |
Jérôme Glisse | 133ff0e | 2017-09-08 16:11:23 -0700 | [diff] [blame] | 87 | } |
| 88 | |
Jason Gunthorpe | 6d7c3cd | 2019-05-22 16:52:52 -0300 | [diff] [blame] | 89 | static void hmm_free_rcu(struct rcu_head *rcu) |
| 90 | { |
Jason Gunthorpe | 8a9320b | 2019-05-23 10:24:13 -0300 | [diff] [blame] | 91 | struct hmm *hmm = container_of(rcu, struct hmm, rcu); |
| 92 | |
| 93 | mmdrop(hmm->mm); |
Jérôme Glisse | 133ff0e | 2017-09-08 16:11:23 -0700 | [diff] [blame] | 94 | kfree(hmm); |
Jérôme Glisse | 133ff0e | 2017-09-08 16:11:23 -0700 | [diff] [blame] | 95 | } |
| 96 | |
Jérôme Glisse | 704f3f2 | 2019-05-13 17:19:48 -0700 | [diff] [blame] | 97 | static void hmm_free(struct kref *kref) |
| 98 | { |
| 99 | struct hmm *hmm = container_of(kref, struct hmm, kref); |
Jérôme Glisse | 704f3f2 | 2019-05-13 17:19:48 -0700 | [diff] [blame] | 100 | |
Jason Gunthorpe | 8a9320b | 2019-05-23 10:24:13 -0300 | [diff] [blame] | 101 | spin_lock(&hmm->mm->page_table_lock); |
| 102 | if (hmm->mm->hmm == hmm) |
| 103 | hmm->mm->hmm = NULL; |
| 104 | spin_unlock(&hmm->mm->page_table_lock); |
Jérôme Glisse | 704f3f2 | 2019-05-13 17:19:48 -0700 | [diff] [blame] | 105 | |
Jason Gunthorpe | 8a9320b | 2019-05-23 10:24:13 -0300 | [diff] [blame] | 106 | mmu_notifier_unregister_no_release(&hmm->mmu_notifier, hmm->mm); |
Jason Gunthorpe | 6d7c3cd | 2019-05-22 16:52:52 -0300 | [diff] [blame] | 107 | mmu_notifier_call_srcu(&hmm->rcu, hmm_free_rcu); |
Jérôme Glisse | 704f3f2 | 2019-05-13 17:19:48 -0700 | [diff] [blame] | 108 | } |
| 109 | |
| 110 | static inline void hmm_put(struct hmm *hmm) |
| 111 | { |
| 112 | kref_put(&hmm->kref, hmm_free); |
| 113 | } |
| 114 | |
Jérôme Glisse | a3e0d41 | 2019-05-13 17:20:01 -0700 | [diff] [blame] | 115 | static void hmm_release(struct mmu_notifier *mn, struct mm_struct *mm) |
Jérôme Glisse | c0b1240 | 2017-09-08 16:11:27 -0700 | [diff] [blame] | 116 | { |
Jason Gunthorpe | 6d7c3cd | 2019-05-22 16:52:52 -0300 | [diff] [blame] | 117 | struct hmm *hmm = container_of(mn, struct hmm, mmu_notifier); |
Jérôme Glisse | c0b1240 | 2017-09-08 16:11:27 -0700 | [diff] [blame] | 118 | struct hmm_mirror *mirror; |
Jérôme Glisse | da4c3c7 | 2017-09-08 16:11:31 -0700 | [diff] [blame] | 119 | |
Jason Gunthorpe | 6d7c3cd | 2019-05-22 16:52:52 -0300 | [diff] [blame] | 120 | /* Bail out if hmm is in the process of being freed */ |
| 121 | if (!kref_get_unless_zero(&hmm->kref)) |
| 122 | return; |
Jérôme Glisse | da4c3c7 | 2017-09-08 16:11:31 -0700 | [diff] [blame] | 123 | |
Jason Gunthorpe | 47f2459 | 2019-05-23 11:08:28 -0300 | [diff] [blame] | 124 | /* |
| 125 | * Since hmm_range_register() holds the mmget() lock hmm_release() is |
| 126 | * prevented as long as a range exists. |
| 127 | */ |
| 128 | WARN_ON(!list_empty_careful(&hmm->ranges)); |
Ralph Campbell | e140151 | 2018-04-10 16:28:19 -0700 | [diff] [blame] | 129 | |
Jason Gunthorpe | 1433172 | 2019-05-24 12:14:08 -0300 | [diff] [blame] | 130 | down_read(&hmm->mirrors_sem); |
Jérôme Glisse | a3e0d41 | 2019-05-13 17:20:01 -0700 | [diff] [blame] | 131 | list_for_each_entry(mirror, &hmm->mirrors, list) { |
Jason Gunthorpe | 1433172 | 2019-05-24 12:14:08 -0300 | [diff] [blame] | 132 | /* |
| 133 | * Note: The driver is not allowed to trigger |
| 134 | * hmm_mirror_unregister() from this thread. |
| 135 | */ |
| 136 | if (mirror->ops->release) |
Ralph Campbell | e140151 | 2018-04-10 16:28:19 -0700 | [diff] [blame] | 137 | mirror->ops->release(mirror); |
Jérôme Glisse | a3e0d41 | 2019-05-13 17:20:01 -0700 | [diff] [blame] | 138 | } |
| 139 | up_read(&hmm->mirrors_sem); |
| 140 | |
Jérôme Glisse | 704f3f2 | 2019-05-13 17:19:48 -0700 | [diff] [blame] | 141 | hmm_put(hmm); |
Jérôme Glisse | c0b1240 | 2017-09-08 16:11:27 -0700 | [diff] [blame] | 142 | } |
| 143 | |
Jason Gunthorpe | 5a136b4 | 2019-06-07 12:10:33 -0300 | [diff] [blame] | 144 | static void notifiers_decrement(struct hmm *hmm) |
Jérôme Glisse | c0b1240 | 2017-09-08 16:11:27 -0700 | [diff] [blame] | 145 | { |
Jason Gunthorpe | 5a136b4 | 2019-06-07 12:10:33 -0300 | [diff] [blame] | 146 | unsigned long flags; |
Jérôme Glisse | c0b1240 | 2017-09-08 16:11:27 -0700 | [diff] [blame] | 147 | |
Jason Gunthorpe | 5a136b4 | 2019-06-07 12:10:33 -0300 | [diff] [blame] | 148 | spin_lock_irqsave(&hmm->ranges_lock, flags); |
Jérôme Glisse | a3e0d41 | 2019-05-13 17:20:01 -0700 | [diff] [blame] | 149 | hmm->notifiers--; |
| 150 | if (!hmm->notifiers) { |
| 151 | struct hmm_range *range; |
| 152 | |
| 153 | list_for_each_entry(range, &hmm->ranges, list) { |
| 154 | if (range->valid) |
| 155 | continue; |
| 156 | range->valid = true; |
| 157 | } |
| 158 | wake_up_all(&hmm->wq); |
| 159 | } |
Jason Gunthorpe | 5a136b4 | 2019-06-07 12:10:33 -0300 | [diff] [blame] | 160 | spin_unlock_irqrestore(&hmm->ranges_lock, flags); |
| 161 | } |
Jérôme Glisse | a3e0d41 | 2019-05-13 17:20:01 -0700 | [diff] [blame] | 162 | |
Jérôme Glisse | 133ff0e | 2017-09-08 16:11:23 -0700 | [diff] [blame] | 163 | static int hmm_invalidate_range_start(struct mmu_notifier *mn, |
| 164 | const struct mmu_notifier_range *nrange) |
| 165 | { |
Jason Gunthorpe | 6d7c3cd | 2019-05-22 16:52:52 -0300 | [diff] [blame] | 166 | struct hmm *hmm = container_of(mn, struct hmm, mmu_notifier); |
Jérôme Glisse | 133ff0e | 2017-09-08 16:11:23 -0700 | [diff] [blame] | 167 | struct hmm_mirror *mirror; |
Jérôme Glisse | 133ff0e | 2017-09-08 16:11:23 -0700 | [diff] [blame] | 168 | struct hmm_range *range; |
Jason Gunthorpe | 5a136b4 | 2019-06-07 12:10:33 -0300 | [diff] [blame] | 169 | unsigned long flags; |
Jérôme Glisse | 133ff0e | 2017-09-08 16:11:23 -0700 | [diff] [blame] | 170 | int ret = 0; |
| 171 | |
Jason Gunthorpe | 6d7c3cd | 2019-05-22 16:52:52 -0300 | [diff] [blame] | 172 | if (!kref_get_unless_zero(&hmm->kref)) |
| 173 | return 0; |
Jérôme Glisse | 133ff0e | 2017-09-08 16:11:23 -0700 | [diff] [blame] | 174 | |
Jason Gunthorpe | 5a136b4 | 2019-06-07 12:10:33 -0300 | [diff] [blame] | 175 | spin_lock_irqsave(&hmm->ranges_lock, flags); |
Jérôme Glisse | 133ff0e | 2017-09-08 16:11:23 -0700 | [diff] [blame] | 176 | hmm->notifiers++; |
| 177 | list_for_each_entry(range, &hmm->ranges, list) { |
Ralph Campbell | 1f96180 | 2019-07-25 17:56:44 -0700 | [diff] [blame] | 178 | if (nrange->end < range->start || nrange->start >= range->end) |
Jérôme Glisse | 133ff0e | 2017-09-08 16:11:23 -0700 | [diff] [blame] | 179 | continue; |
| 180 | |
| 181 | range->valid = false; |
| 182 | } |
Jason Gunthorpe | 5a136b4 | 2019-06-07 12:10:33 -0300 | [diff] [blame] | 183 | spin_unlock_irqrestore(&hmm->ranges_lock, flags); |
Jérôme Glisse | c0b1240 | 2017-09-08 16:11:27 -0700 | [diff] [blame] | 184 | |
| 185 | if (mmu_notifier_range_blockable(nrange)) |
| 186 | down_read(&hmm->mirrors_sem); |
| 187 | else if (!down_read_trylock(&hmm->mirrors_sem)) { |
| 188 | ret = -EAGAIN; |
| 189 | goto out; |
| 190 | } |
Jérôme Glisse | c0b1240 | 2017-09-08 16:11:27 -0700 | [diff] [blame] | 191 | |
Jason Gunthorpe | 5a136b4 | 2019-06-07 12:10:33 -0300 | [diff] [blame] | 192 | list_for_each_entry(mirror, &hmm->mirrors, list) { |
| 193 | int rc; |
| 194 | |
Ralph Campbell | 1f96180 | 2019-07-25 17:56:44 -0700 | [diff] [blame] | 195 | rc = mirror->ops->sync_cpu_device_pagetables(mirror, nrange); |
Jason Gunthorpe | 5a136b4 | 2019-06-07 12:10:33 -0300 | [diff] [blame] | 196 | if (rc) { |
Ralph Campbell | 1f96180 | 2019-07-25 17:56:44 -0700 | [diff] [blame] | 197 | if (WARN_ON(mmu_notifier_range_blockable(nrange) || |
| 198 | rc != -EAGAIN)) |
Jason Gunthorpe | 5a136b4 | 2019-06-07 12:10:33 -0300 | [diff] [blame] | 199 | continue; |
| 200 | ret = -EAGAIN; |
Ralph Campbell | 085ea25 | 2019-05-06 16:29:39 -0700 | [diff] [blame] | 201 | break; |
Jason Gunthorpe | 5a136b4 | 2019-06-07 12:10:33 -0300 | [diff] [blame] | 202 | } |
Jérôme Glisse | c0b1240 | 2017-09-08 16:11:27 -0700 | [diff] [blame] | 203 | } |
| 204 | up_read(&hmm->mirrors_sem); |
| 205 | |
| 206 | out: |
Jason Gunthorpe | 5a136b4 | 2019-06-07 12:10:33 -0300 | [diff] [blame] | 207 | if (ret) |
| 208 | notifiers_decrement(hmm); |
Jérôme Glisse | c0b1240 | 2017-09-08 16:11:27 -0700 | [diff] [blame] | 209 | hmm_put(hmm); |
| 210 | return ret; |
| 211 | } |
| 212 | |
| 213 | static void hmm_invalidate_range_end(struct mmu_notifier *mn, |
| 214 | const struct mmu_notifier_range *nrange) |
| 215 | { |
Jason Gunthorpe | 6d7c3cd | 2019-05-22 16:52:52 -0300 | [diff] [blame] | 216 | struct hmm *hmm = container_of(mn, struct hmm, mmu_notifier); |
Jérôme Glisse | c0b1240 | 2017-09-08 16:11:27 -0700 | [diff] [blame] | 217 | |
Jason Gunthorpe | 6d7c3cd | 2019-05-22 16:52:52 -0300 | [diff] [blame] | 218 | if (!kref_get_unless_zero(&hmm->kref)) |
| 219 | return; |
Jérôme Glisse | c0b1240 | 2017-09-08 16:11:27 -0700 | [diff] [blame] | 220 | |
Jason Gunthorpe | 5a136b4 | 2019-06-07 12:10:33 -0300 | [diff] [blame] | 221 | notifiers_decrement(hmm); |
Jérôme Glisse | 704f3f2 | 2019-05-13 17:19:48 -0700 | [diff] [blame] | 222 | hmm_put(hmm); |
Jérôme Glisse | c0b1240 | 2017-09-08 16:11:27 -0700 | [diff] [blame] | 223 | } |
| 224 | |
| 225 | static const struct mmu_notifier_ops hmm_mmu_notifier_ops = { |
Ralph Campbell | e140151 | 2018-04-10 16:28:19 -0700 | [diff] [blame] | 226 | .release = hmm_release, |
Jérôme Glisse | c0b1240 | 2017-09-08 16:11:27 -0700 | [diff] [blame] | 227 | .invalidate_range_start = hmm_invalidate_range_start, |
| 228 | .invalidate_range_end = hmm_invalidate_range_end, |
| 229 | }; |
| 230 | |
| 231 | /* |
| 232 | * hmm_mirror_register() - register a mirror against an mm |
| 233 | * |
| 234 | * @mirror: new mirror struct to register |
| 235 | * @mm: mm to register against |
Ralph Campbell | 085ea25 | 2019-05-06 16:29:39 -0700 | [diff] [blame] | 236 | * Return: 0 on success, -ENOMEM if no memory, -EINVAL if invalid arguments |
Jérôme Glisse | c0b1240 | 2017-09-08 16:11:27 -0700 | [diff] [blame] | 237 | * |
| 238 | * To start mirroring a process address space, the device driver must register |
| 239 | * an HMM mirror struct. |
Jérôme Glisse | c0b1240 | 2017-09-08 16:11:27 -0700 | [diff] [blame] | 240 | */ |
| 241 | int hmm_mirror_register(struct hmm_mirror *mirror, struct mm_struct *mm) |
| 242 | { |
Linus Torvalds | fec88ab | 2019-07-14 19:42:11 -0700 | [diff] [blame] | 243 | lockdep_assert_held_write(&mm->mmap_sem); |
Jason Gunthorpe | 8a1a0cd | 2019-05-23 11:23:30 -0300 | [diff] [blame] | 244 | |
Jérôme Glisse | c0b1240 | 2017-09-08 16:11:27 -0700 | [diff] [blame] | 245 | /* Sanity check */ |
| 246 | if (!mm || !mirror || !mirror->ops) |
| 247 | return -EINVAL; |
| 248 | |
Jérôme Glisse | 704f3f2 | 2019-05-13 17:19:48 -0700 | [diff] [blame] | 249 | mirror->hmm = hmm_get_or_create(mm); |
Jérôme Glisse | c0b1240 | 2017-09-08 16:11:27 -0700 | [diff] [blame] | 250 | if (!mirror->hmm) |
| 251 | return -ENOMEM; |
| 252 | |
| 253 | down_write(&mirror->hmm->mirrors_sem); |
Jérôme Glisse | 704f3f2 | 2019-05-13 17:19:48 -0700 | [diff] [blame] | 254 | list_add(&mirror->list, &mirror->hmm->mirrors); |
| 255 | up_write(&mirror->hmm->mirrors_sem); |
Jérôme Glisse | c0b1240 | 2017-09-08 16:11:27 -0700 | [diff] [blame] | 256 | |
| 257 | return 0; |
| 258 | } |
| 259 | EXPORT_SYMBOL(hmm_mirror_register); |
| 260 | |
| 261 | /* |
| 262 | * hmm_mirror_unregister() - unregister a mirror |
| 263 | * |
Ralph Campbell | 085ea25 | 2019-05-06 16:29:39 -0700 | [diff] [blame] | 264 | * @mirror: mirror struct to unregister |
Jérôme Glisse | c0b1240 | 2017-09-08 16:11:27 -0700 | [diff] [blame] | 265 | * |
| 266 | * Stop mirroring a process address space, and cleanup. |
| 267 | */ |
| 268 | void hmm_mirror_unregister(struct hmm_mirror *mirror) |
| 269 | { |
Jason Gunthorpe | 187229c | 2019-05-23 11:31:45 -0300 | [diff] [blame] | 270 | struct hmm *hmm = mirror->hmm; |
Jérôme Glisse | c01cbba | 2018-04-10 16:28:23 -0700 | [diff] [blame] | 271 | |
Jérôme Glisse | c0b1240 | 2017-09-08 16:11:27 -0700 | [diff] [blame] | 272 | down_write(&hmm->mirrors_sem); |
Jason Gunthorpe | 1433172 | 2019-05-24 12:14:08 -0300 | [diff] [blame] | 273 | list_del(&mirror->list); |
Jérôme Glisse | c0b1240 | 2017-09-08 16:11:27 -0700 | [diff] [blame] | 274 | up_write(&hmm->mirrors_sem); |
Jérôme Glisse | 704f3f2 | 2019-05-13 17:19:48 -0700 | [diff] [blame] | 275 | hmm_put(hmm); |
Jérôme Glisse | c0b1240 | 2017-09-08 16:11:27 -0700 | [diff] [blame] | 276 | } |
| 277 | EXPORT_SYMBOL(hmm_mirror_unregister); |
Jérôme Glisse | da4c3c7 | 2017-09-08 16:11:31 -0700 | [diff] [blame] | 278 | |
Jérôme Glisse | 74eee18 | 2017-09-08 16:11:35 -0700 | [diff] [blame] | 279 | struct hmm_vma_walk { |
| 280 | struct hmm_range *range; |
Jérôme Glisse | 992de9a | 2019-05-13 17:20:21 -0700 | [diff] [blame] | 281 | struct dev_pagemap *pgmap; |
Jérôme Glisse | 74eee18 | 2017-09-08 16:11:35 -0700 | [diff] [blame] | 282 | unsigned long last; |
| 283 | bool fault; |
Christoph Hellwig | 9a4903e | 2019-07-25 17:56:46 -0700 | [diff] [blame^] | 284 | unsigned int flags; |
Jérôme Glisse | 74eee18 | 2017-09-08 16:11:35 -0700 | [diff] [blame] | 285 | }; |
| 286 | |
Jérôme Glisse | 2aee09d | 2018-04-10 16:29:02 -0700 | [diff] [blame] | 287 | static int hmm_vma_do_fault(struct mm_walk *walk, unsigned long addr, |
| 288 | bool write_fault, uint64_t *pfn) |
Jérôme Glisse | 74eee18 | 2017-09-08 16:11:35 -0700 | [diff] [blame] | 289 | { |
Kuehling, Felix | 9b1ae60 | 2019-05-10 19:53:24 +0000 | [diff] [blame] | 290 | unsigned int flags = FAULT_FLAG_REMOTE; |
Jérôme Glisse | 74eee18 | 2017-09-08 16:11:35 -0700 | [diff] [blame] | 291 | struct hmm_vma_walk *hmm_vma_walk = walk->private; |
Jérôme Glisse | f88a1e9 | 2018-04-10 16:29:06 -0700 | [diff] [blame] | 292 | struct hmm_range *range = hmm_vma_walk->range; |
Jérôme Glisse | 74eee18 | 2017-09-08 16:11:35 -0700 | [diff] [blame] | 293 | struct vm_area_struct *vma = walk->vma; |
Souptick Joarder | 50a7ca3 | 2018-08-17 15:44:47 -0700 | [diff] [blame] | 294 | vm_fault_t ret; |
Jérôme Glisse | 74eee18 | 2017-09-08 16:11:35 -0700 | [diff] [blame] | 295 | |
Christoph Hellwig | 9a4903e | 2019-07-25 17:56:46 -0700 | [diff] [blame^] | 296 | if (hmm_vma_walk->flags & HMM_FAULT_ALLOW_RETRY) |
| 297 | flags |= FAULT_FLAG_ALLOW_RETRY; |
| 298 | if (write_fault) |
| 299 | flags |= FAULT_FLAG_WRITE; |
| 300 | |
Souptick Joarder | 50a7ca3 | 2018-08-17 15:44:47 -0700 | [diff] [blame] | 301 | ret = handle_mm_fault(vma, addr, flags); |
Jason Gunthorpe | e709acc | 2019-07-24 08:52:58 +0200 | [diff] [blame] | 302 | if (ret & VM_FAULT_RETRY) { |
| 303 | /* Note, handle_mm_fault did up_read(&mm->mmap_sem)) */ |
Jérôme Glisse | 7323161 | 2019-05-13 17:19:58 -0700 | [diff] [blame] | 304 | return -EAGAIN; |
Jason Gunthorpe | e709acc | 2019-07-24 08:52:58 +0200 | [diff] [blame] | 305 | } |
Souptick Joarder | 50a7ca3 | 2018-08-17 15:44:47 -0700 | [diff] [blame] | 306 | if (ret & VM_FAULT_ERROR) { |
Jérôme Glisse | f88a1e9 | 2018-04-10 16:29:06 -0700 | [diff] [blame] | 307 | *pfn = range->values[HMM_PFN_ERROR]; |
Jérôme Glisse | 74eee18 | 2017-09-08 16:11:35 -0700 | [diff] [blame] | 308 | return -EFAULT; |
| 309 | } |
| 310 | |
Jérôme Glisse | 7323161 | 2019-05-13 17:19:58 -0700 | [diff] [blame] | 311 | return -EBUSY; |
Jérôme Glisse | 74eee18 | 2017-09-08 16:11:35 -0700 | [diff] [blame] | 312 | } |
| 313 | |
Jérôme Glisse | da4c3c7 | 2017-09-08 16:11:31 -0700 | [diff] [blame] | 314 | static int hmm_pfns_bad(unsigned long addr, |
| 315 | unsigned long end, |
| 316 | struct mm_walk *walk) |
| 317 | { |
Jérôme Glisse | c719547 | 2018-04-10 16:28:27 -0700 | [diff] [blame] | 318 | struct hmm_vma_walk *hmm_vma_walk = walk->private; |
| 319 | struct hmm_range *range = hmm_vma_walk->range; |
Jérôme Glisse | ff05c0c | 2018-04-10 16:28:38 -0700 | [diff] [blame] | 320 | uint64_t *pfns = range->pfns; |
Jérôme Glisse | da4c3c7 | 2017-09-08 16:11:31 -0700 | [diff] [blame] | 321 | unsigned long i; |
| 322 | |
| 323 | i = (addr - range->start) >> PAGE_SHIFT; |
| 324 | for (; addr < end; addr += PAGE_SIZE, i++) |
Jérôme Glisse | f88a1e9 | 2018-04-10 16:29:06 -0700 | [diff] [blame] | 325 | pfns[i] = range->values[HMM_PFN_ERROR]; |
Jérôme Glisse | da4c3c7 | 2017-09-08 16:11:31 -0700 | [diff] [blame] | 326 | |
| 327 | return 0; |
| 328 | } |
| 329 | |
Jérôme Glisse | 5504ed2 | 2018-04-10 16:28:46 -0700 | [diff] [blame] | 330 | /* |
Ralph Campbell | d2e8d55 | 2019-07-25 17:56:45 -0700 | [diff] [blame] | 331 | * hmm_vma_walk_hole_() - handle a range lacking valid pmd or pte(s) |
| 332 | * @addr: range virtual start address (inclusive) |
Jérôme Glisse | 5504ed2 | 2018-04-10 16:28:46 -0700 | [diff] [blame] | 333 | * @end: range virtual end address (exclusive) |
Jérôme Glisse | 2aee09d | 2018-04-10 16:29:02 -0700 | [diff] [blame] | 334 | * @fault: should we fault or not ? |
| 335 | * @write_fault: write fault ? |
Jérôme Glisse | 5504ed2 | 2018-04-10 16:28:46 -0700 | [diff] [blame] | 336 | * @walk: mm_walk structure |
Ralph Campbell | 085ea25 | 2019-05-06 16:29:39 -0700 | [diff] [blame] | 337 | * Return: 0 on success, -EBUSY after page fault, or page fault error |
Jérôme Glisse | 5504ed2 | 2018-04-10 16:28:46 -0700 | [diff] [blame] | 338 | * |
| 339 | * This function will be called whenever pmd_none() or pte_none() returns true, |
| 340 | * or whenever there is no page directory covering the virtual address range. |
| 341 | */ |
Jérôme Glisse | 2aee09d | 2018-04-10 16:29:02 -0700 | [diff] [blame] | 342 | static int hmm_vma_walk_hole_(unsigned long addr, unsigned long end, |
| 343 | bool fault, bool write_fault, |
| 344 | struct mm_walk *walk) |
Jérôme Glisse | da4c3c7 | 2017-09-08 16:11:31 -0700 | [diff] [blame] | 345 | { |
Jérôme Glisse | 74eee18 | 2017-09-08 16:11:35 -0700 | [diff] [blame] | 346 | struct hmm_vma_walk *hmm_vma_walk = walk->private; |
| 347 | struct hmm_range *range = hmm_vma_walk->range; |
Jérôme Glisse | ff05c0c | 2018-04-10 16:28:38 -0700 | [diff] [blame] | 348 | uint64_t *pfns = range->pfns; |
Jérôme Glisse | 63d5066 | 2019-05-13 17:20:18 -0700 | [diff] [blame] | 349 | unsigned long i, page_size; |
Jérôme Glisse | da4c3c7 | 2017-09-08 16:11:31 -0700 | [diff] [blame] | 350 | |
Jérôme Glisse | 74eee18 | 2017-09-08 16:11:35 -0700 | [diff] [blame] | 351 | hmm_vma_walk->last = addr; |
Jérôme Glisse | 63d5066 | 2019-05-13 17:20:18 -0700 | [diff] [blame] | 352 | page_size = hmm_range_page_size(range); |
| 353 | i = (addr - range->start) >> range->page_shift; |
| 354 | |
| 355 | for (; addr < end; addr += page_size, i++) { |
Jérôme Glisse | f88a1e9 | 2018-04-10 16:29:06 -0700 | [diff] [blame] | 356 | pfns[i] = range->values[HMM_PFN_NONE]; |
Jérôme Glisse | 2aee09d | 2018-04-10 16:29:02 -0700 | [diff] [blame] | 357 | if (fault || write_fault) { |
Jérôme Glisse | 74eee18 | 2017-09-08 16:11:35 -0700 | [diff] [blame] | 358 | int ret; |
Jérôme Glisse | da4c3c7 | 2017-09-08 16:11:31 -0700 | [diff] [blame] | 359 | |
Jérôme Glisse | 2aee09d | 2018-04-10 16:29:02 -0700 | [diff] [blame] | 360 | ret = hmm_vma_do_fault(walk, addr, write_fault, |
| 361 | &pfns[i]); |
Jérôme Glisse | 7323161 | 2019-05-13 17:19:58 -0700 | [diff] [blame] | 362 | if (ret != -EBUSY) |
Jérôme Glisse | 74eee18 | 2017-09-08 16:11:35 -0700 | [diff] [blame] | 363 | return ret; |
| 364 | } |
| 365 | } |
| 366 | |
Jérôme Glisse | 7323161 | 2019-05-13 17:19:58 -0700 | [diff] [blame] | 367 | return (fault || write_fault) ? -EBUSY : 0; |
Jérôme Glisse | 2aee09d | 2018-04-10 16:29:02 -0700 | [diff] [blame] | 368 | } |
| 369 | |
| 370 | static inline void hmm_pte_need_fault(const struct hmm_vma_walk *hmm_vma_walk, |
| 371 | uint64_t pfns, uint64_t cpu_flags, |
| 372 | bool *fault, bool *write_fault) |
| 373 | { |
Jérôme Glisse | f88a1e9 | 2018-04-10 16:29:06 -0700 | [diff] [blame] | 374 | struct hmm_range *range = hmm_vma_walk->range; |
| 375 | |
Jérôme Glisse | 2aee09d | 2018-04-10 16:29:02 -0700 | [diff] [blame] | 376 | if (!hmm_vma_walk->fault) |
| 377 | return; |
| 378 | |
Jérôme Glisse | 023a019 | 2019-05-13 17:20:05 -0700 | [diff] [blame] | 379 | /* |
| 380 | * So we not only consider the individual per page request we also |
| 381 | * consider the default flags requested for the range. The API can |
Ralph Campbell | d2e8d55 | 2019-07-25 17:56:45 -0700 | [diff] [blame] | 382 | * be used 2 ways. The first one where the HMM user coalesces |
| 383 | * multiple page faults into one request and sets flags per pfn for |
| 384 | * those faults. The second one where the HMM user wants to pre- |
Jérôme Glisse | 023a019 | 2019-05-13 17:20:05 -0700 | [diff] [blame] | 385 | * fault a range with specific flags. For the latter one it is a |
| 386 | * waste to have the user pre-fill the pfn arrays with a default |
| 387 | * flags value. |
| 388 | */ |
| 389 | pfns = (pfns & range->pfn_flags_mask) | range->default_flags; |
| 390 | |
Jérôme Glisse | 2aee09d | 2018-04-10 16:29:02 -0700 | [diff] [blame] | 391 | /* We aren't ask to do anything ... */ |
Jérôme Glisse | f88a1e9 | 2018-04-10 16:29:06 -0700 | [diff] [blame] | 392 | if (!(pfns & range->flags[HMM_PFN_VALID])) |
Jérôme Glisse | 2aee09d | 2018-04-10 16:29:02 -0700 | [diff] [blame] | 393 | return; |
Ralph Campbell | d2e8d55 | 2019-07-25 17:56:45 -0700 | [diff] [blame] | 394 | /* If this is device memory then only fault if explicitly requested */ |
Jérôme Glisse | f88a1e9 | 2018-04-10 16:29:06 -0700 | [diff] [blame] | 395 | if ((cpu_flags & range->flags[HMM_PFN_DEVICE_PRIVATE])) { |
| 396 | /* Do we fault on device memory ? */ |
| 397 | if (pfns & range->flags[HMM_PFN_DEVICE_PRIVATE]) { |
| 398 | *write_fault = pfns & range->flags[HMM_PFN_WRITE]; |
| 399 | *fault = true; |
| 400 | } |
Jérôme Glisse | 2aee09d | 2018-04-10 16:29:02 -0700 | [diff] [blame] | 401 | return; |
| 402 | } |
Jérôme Glisse | f88a1e9 | 2018-04-10 16:29:06 -0700 | [diff] [blame] | 403 | |
| 404 | /* If CPU page table is not valid then we need to fault */ |
| 405 | *fault = !(cpu_flags & range->flags[HMM_PFN_VALID]); |
| 406 | /* Need to write fault ? */ |
| 407 | if ((pfns & range->flags[HMM_PFN_WRITE]) && |
| 408 | !(cpu_flags & range->flags[HMM_PFN_WRITE])) { |
| 409 | *write_fault = true; |
Jérôme Glisse | 2aee09d | 2018-04-10 16:29:02 -0700 | [diff] [blame] | 410 | *fault = true; |
| 411 | } |
| 412 | } |
| 413 | |
| 414 | static void hmm_range_need_fault(const struct hmm_vma_walk *hmm_vma_walk, |
| 415 | const uint64_t *pfns, unsigned long npages, |
| 416 | uint64_t cpu_flags, bool *fault, |
| 417 | bool *write_fault) |
| 418 | { |
| 419 | unsigned long i; |
| 420 | |
| 421 | if (!hmm_vma_walk->fault) { |
| 422 | *fault = *write_fault = false; |
| 423 | return; |
| 424 | } |
| 425 | |
Jérôme Glisse | a3e0d41 | 2019-05-13 17:20:01 -0700 | [diff] [blame] | 426 | *fault = *write_fault = false; |
Jérôme Glisse | 2aee09d | 2018-04-10 16:29:02 -0700 | [diff] [blame] | 427 | for (i = 0; i < npages; ++i) { |
| 428 | hmm_pte_need_fault(hmm_vma_walk, pfns[i], cpu_flags, |
| 429 | fault, write_fault); |
Jérôme Glisse | a3e0d41 | 2019-05-13 17:20:01 -0700 | [diff] [blame] | 430 | if ((*write_fault)) |
Jérôme Glisse | 2aee09d | 2018-04-10 16:29:02 -0700 | [diff] [blame] | 431 | return; |
| 432 | } |
| 433 | } |
| 434 | |
| 435 | static int hmm_vma_walk_hole(unsigned long addr, unsigned long end, |
| 436 | struct mm_walk *walk) |
| 437 | { |
| 438 | struct hmm_vma_walk *hmm_vma_walk = walk->private; |
| 439 | struct hmm_range *range = hmm_vma_walk->range; |
| 440 | bool fault, write_fault; |
| 441 | unsigned long i, npages; |
| 442 | uint64_t *pfns; |
| 443 | |
| 444 | i = (addr - range->start) >> PAGE_SHIFT; |
| 445 | npages = (end - addr) >> PAGE_SHIFT; |
| 446 | pfns = &range->pfns[i]; |
| 447 | hmm_range_need_fault(hmm_vma_walk, pfns, npages, |
| 448 | 0, &fault, &write_fault); |
| 449 | return hmm_vma_walk_hole_(addr, end, fault, write_fault, walk); |
| 450 | } |
| 451 | |
Jérôme Glisse | f88a1e9 | 2018-04-10 16:29:06 -0700 | [diff] [blame] | 452 | 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] | 453 | { |
| 454 | if (pmd_protnone(pmd)) |
| 455 | return 0; |
Jérôme Glisse | f88a1e9 | 2018-04-10 16:29:06 -0700 | [diff] [blame] | 456 | return pmd_write(pmd) ? range->flags[HMM_PFN_VALID] | |
| 457 | range->flags[HMM_PFN_WRITE] : |
| 458 | range->flags[HMM_PFN_VALID]; |
Jérôme Glisse | da4c3c7 | 2017-09-08 16:11:31 -0700 | [diff] [blame] | 459 | } |
| 460 | |
Jérôme Glisse | 992de9a | 2019-05-13 17:20:21 -0700 | [diff] [blame] | 461 | static inline uint64_t pud_to_hmm_pfn_flags(struct hmm_range *range, pud_t pud) |
| 462 | { |
| 463 | if (!pud_present(pud)) |
| 464 | return 0; |
| 465 | return pud_write(pud) ? range->flags[HMM_PFN_VALID] | |
| 466 | range->flags[HMM_PFN_WRITE] : |
| 467 | range->flags[HMM_PFN_VALID]; |
| 468 | } |
| 469 | |
Jérôme Glisse | 53f5c3f | 2018-04-10 16:28:59 -0700 | [diff] [blame] | 470 | static int hmm_vma_handle_pmd(struct mm_walk *walk, |
| 471 | unsigned long addr, |
| 472 | unsigned long end, |
| 473 | uint64_t *pfns, |
| 474 | pmd_t pmd) |
| 475 | { |
Jérôme Glisse | 992de9a | 2019-05-13 17:20:21 -0700 | [diff] [blame] | 476 | #ifdef CONFIG_TRANSPARENT_HUGEPAGE |
Jérôme Glisse | 53f5c3f | 2018-04-10 16:28:59 -0700 | [diff] [blame] | 477 | struct hmm_vma_walk *hmm_vma_walk = walk->private; |
Jérôme Glisse | f88a1e9 | 2018-04-10 16:29:06 -0700 | [diff] [blame] | 478 | struct hmm_range *range = hmm_vma_walk->range; |
Jérôme Glisse | 2aee09d | 2018-04-10 16:29:02 -0700 | [diff] [blame] | 479 | unsigned long pfn, npages, i; |
Jérôme Glisse | 2aee09d | 2018-04-10 16:29:02 -0700 | [diff] [blame] | 480 | bool fault, write_fault; |
Jérôme Glisse | f88a1e9 | 2018-04-10 16:29:06 -0700 | [diff] [blame] | 481 | uint64_t cpu_flags; |
Jérôme Glisse | 53f5c3f | 2018-04-10 16:28:59 -0700 | [diff] [blame] | 482 | |
Jérôme Glisse | 2aee09d | 2018-04-10 16:29:02 -0700 | [diff] [blame] | 483 | npages = (end - addr) >> PAGE_SHIFT; |
Jérôme Glisse | f88a1e9 | 2018-04-10 16:29:06 -0700 | [diff] [blame] | 484 | cpu_flags = pmd_to_hmm_pfn_flags(range, pmd); |
Jérôme Glisse | 2aee09d | 2018-04-10 16:29:02 -0700 | [diff] [blame] | 485 | hmm_range_need_fault(hmm_vma_walk, pfns, npages, cpu_flags, |
| 486 | &fault, &write_fault); |
Jérôme Glisse | 53f5c3f | 2018-04-10 16:28:59 -0700 | [diff] [blame] | 487 | |
Jérôme Glisse | 2aee09d | 2018-04-10 16:29:02 -0700 | [diff] [blame] | 488 | if (pmd_protnone(pmd) || fault || write_fault) |
| 489 | 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] | 490 | |
| 491 | pfn = pmd_pfn(pmd) + pte_index(addr); |
Jérôme Glisse | 992de9a | 2019-05-13 17:20:21 -0700 | [diff] [blame] | 492 | for (i = 0; addr < end; addr += PAGE_SIZE, i++, pfn++) { |
| 493 | if (pmd_devmap(pmd)) { |
| 494 | hmm_vma_walk->pgmap = get_dev_pagemap(pfn, |
| 495 | hmm_vma_walk->pgmap); |
| 496 | if (unlikely(!hmm_vma_walk->pgmap)) |
| 497 | return -EBUSY; |
| 498 | } |
Jérôme Glisse | 391aab1 | 2019-05-13 17:20:31 -0700 | [diff] [blame] | 499 | pfns[i] = hmm_device_entry_from_pfn(range, pfn) | cpu_flags; |
Jérôme Glisse | 992de9a | 2019-05-13 17:20:21 -0700 | [diff] [blame] | 500 | } |
| 501 | if (hmm_vma_walk->pgmap) { |
| 502 | put_dev_pagemap(hmm_vma_walk->pgmap); |
| 503 | hmm_vma_walk->pgmap = NULL; |
| 504 | } |
Jérôme Glisse | 53f5c3f | 2018-04-10 16:28:59 -0700 | [diff] [blame] | 505 | hmm_vma_walk->last = end; |
| 506 | return 0; |
Jérôme Glisse | 992de9a | 2019-05-13 17:20:21 -0700 | [diff] [blame] | 507 | #else |
Ralph Campbell | d2e8d55 | 2019-07-25 17:56:45 -0700 | [diff] [blame] | 508 | /* If THP is not enabled then we should never reach this code ! */ |
Jérôme Glisse | 992de9a | 2019-05-13 17:20:21 -0700 | [diff] [blame] | 509 | return -EINVAL; |
| 510 | #endif |
Jérôme Glisse | 53f5c3f | 2018-04-10 16:28:59 -0700 | [diff] [blame] | 511 | } |
| 512 | |
Jérôme Glisse | f88a1e9 | 2018-04-10 16:29:06 -0700 | [diff] [blame] | 513 | 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] | 514 | { |
Philip Yang | 789c2af | 2019-05-23 16:32:31 -0400 | [diff] [blame] | 515 | if (pte_none(pte) || !pte_present(pte) || pte_protnone(pte)) |
Jérôme Glisse | 2aee09d | 2018-04-10 16:29:02 -0700 | [diff] [blame] | 516 | return 0; |
Jérôme Glisse | f88a1e9 | 2018-04-10 16:29:06 -0700 | [diff] [blame] | 517 | return pte_write(pte) ? range->flags[HMM_PFN_VALID] | |
| 518 | range->flags[HMM_PFN_WRITE] : |
| 519 | range->flags[HMM_PFN_VALID]; |
Jérôme Glisse | 2aee09d | 2018-04-10 16:29:02 -0700 | [diff] [blame] | 520 | } |
| 521 | |
Jérôme Glisse | 53f5c3f | 2018-04-10 16:28:59 -0700 | [diff] [blame] | 522 | static int hmm_vma_handle_pte(struct mm_walk *walk, unsigned long addr, |
| 523 | unsigned long end, pmd_t *pmdp, pte_t *ptep, |
| 524 | uint64_t *pfn) |
| 525 | { |
| 526 | struct hmm_vma_walk *hmm_vma_walk = walk->private; |
Jérôme Glisse | f88a1e9 | 2018-04-10 16:29:06 -0700 | [diff] [blame] | 527 | struct hmm_range *range = hmm_vma_walk->range; |
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]; |
Jérôme Glisse | 7323161 | 2019-05-13 17:19:58 -0700 | [diff] [blame] | 534 | fault = write_fault = false; |
Jérôme Glisse | 53f5c3f | 2018-04-10 16:28:59 -0700 | [diff] [blame] | 535 | |
| 536 | if (pte_none(pte)) { |
Jérôme Glisse | 7323161 | 2019-05-13 17:19:58 -0700 | [diff] [blame] | 537 | hmm_pte_need_fault(hmm_vma_walk, orig_pfn, 0, |
| 538 | &fault, &write_fault); |
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; |
Jérôme Glisse | 391aab1 | 2019-05-13 17:20:31 -0700 | [diff] [blame] | 566 | *pfn = hmm_device_entry_from_pfn(range, |
| 567 | swp_offset(entry)); |
Jérôme Glisse | f88a1e9 | 2018-04-10 16:29:06 -0700 | [diff] [blame] | 568 | *pfn |= cpu_flags; |
Jérôme Glisse | 53f5c3f | 2018-04-10 16:28:59 -0700 | [diff] [blame] | 569 | return 0; |
| 570 | } |
| 571 | |
| 572 | if (is_migration_entry(entry)) { |
Jérôme Glisse | 2aee09d | 2018-04-10 16:29:02 -0700 | [diff] [blame] | 573 | if (fault || write_fault) { |
Jérôme Glisse | 53f5c3f | 2018-04-10 16:28:59 -0700 | [diff] [blame] | 574 | pte_unmap(ptep); |
| 575 | hmm_vma_walk->last = addr; |
Ralph Campbell | d2e8d55 | 2019-07-25 17:56:45 -0700 | [diff] [blame] | 576 | migration_entry_wait(walk->mm, pmdp, addr); |
Jérôme Glisse | 7323161 | 2019-05-13 17:19:58 -0700 | [diff] [blame] | 577 | return -EBUSY; |
Jérôme Glisse | 53f5c3f | 2018-04-10 16:28:59 -0700 | [diff] [blame] | 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; |
Jérôme Glisse | 7323161 | 2019-05-13 17:19:58 -0700 | [diff] [blame] | 585 | } else { |
| 586 | cpu_flags = pte_to_hmm_pfn_flags(range, pte); |
| 587 | hmm_pte_need_fault(hmm_vma_walk, orig_pfn, cpu_flags, |
| 588 | &fault, &write_fault); |
Jérôme Glisse | 53f5c3f | 2018-04-10 16:28:59 -0700 | [diff] [blame] | 589 | } |
| 590 | |
Jérôme Glisse | 2aee09d | 2018-04-10 16:29:02 -0700 | [diff] [blame] | 591 | if (fault || write_fault) |
Jérôme Glisse | 53f5c3f | 2018-04-10 16:28:59 -0700 | [diff] [blame] | 592 | goto fault; |
| 593 | |
Jérôme Glisse | 992de9a | 2019-05-13 17:20:21 -0700 | [diff] [blame] | 594 | if (pte_devmap(pte)) { |
| 595 | hmm_vma_walk->pgmap = get_dev_pagemap(pte_pfn(pte), |
| 596 | hmm_vma_walk->pgmap); |
| 597 | if (unlikely(!hmm_vma_walk->pgmap)) |
| 598 | return -EBUSY; |
| 599 | } else if (IS_ENABLED(CONFIG_ARCH_HAS_PTE_SPECIAL) && pte_special(pte)) { |
| 600 | *pfn = range->values[HMM_PFN_SPECIAL]; |
| 601 | return -EFAULT; |
| 602 | } |
| 603 | |
Jérôme Glisse | 391aab1 | 2019-05-13 17:20:31 -0700 | [diff] [blame] | 604 | *pfn = hmm_device_entry_from_pfn(range, pte_pfn(pte)) | cpu_flags; |
Jérôme Glisse | 53f5c3f | 2018-04-10 16:28:59 -0700 | [diff] [blame] | 605 | return 0; |
| 606 | |
| 607 | fault: |
Jérôme Glisse | 992de9a | 2019-05-13 17:20:21 -0700 | [diff] [blame] | 608 | if (hmm_vma_walk->pgmap) { |
| 609 | put_dev_pagemap(hmm_vma_walk->pgmap); |
| 610 | hmm_vma_walk->pgmap = NULL; |
| 611 | } |
Jérôme Glisse | 53f5c3f | 2018-04-10 16:28:59 -0700 | [diff] [blame] | 612 | pte_unmap(ptep); |
| 613 | /* Fault any virtual address we were asked to fault */ |
Jérôme Glisse | 2aee09d | 2018-04-10 16:29:02 -0700 | [diff] [blame] | 614 | 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] | 615 | } |
| 616 | |
Jérôme Glisse | da4c3c7 | 2017-09-08 16:11:31 -0700 | [diff] [blame] | 617 | static int hmm_vma_walk_pmd(pmd_t *pmdp, |
| 618 | unsigned long start, |
| 619 | unsigned long end, |
| 620 | struct mm_walk *walk) |
| 621 | { |
Jérôme Glisse | 74eee18 | 2017-09-08 16:11:35 -0700 | [diff] [blame] | 622 | struct hmm_vma_walk *hmm_vma_walk = walk->private; |
| 623 | struct hmm_range *range = hmm_vma_walk->range; |
Jérôme Glisse | ff05c0c | 2018-04-10 16:28:38 -0700 | [diff] [blame] | 624 | uint64_t *pfns = range->pfns; |
Jérôme Glisse | da4c3c7 | 2017-09-08 16:11:31 -0700 | [diff] [blame] | 625 | unsigned long addr = start, i; |
Jérôme Glisse | da4c3c7 | 2017-09-08 16:11:31 -0700 | [diff] [blame] | 626 | pte_t *ptep; |
Jérôme Glisse | d08faca | 2018-10-30 15:04:20 -0700 | [diff] [blame] | 627 | pmd_t pmd; |
Jérôme Glisse | da4c3c7 | 2017-09-08 16:11:31 -0700 | [diff] [blame] | 628 | |
Jérôme Glisse | da4c3c7 | 2017-09-08 16:11:31 -0700 | [diff] [blame] | 629 | again: |
Jérôme Glisse | d08faca | 2018-10-30 15:04:20 -0700 | [diff] [blame] | 630 | pmd = READ_ONCE(*pmdp); |
| 631 | if (pmd_none(pmd)) |
Jérôme Glisse | da4c3c7 | 2017-09-08 16:11:31 -0700 | [diff] [blame] | 632 | return hmm_vma_walk_hole(start, end, walk); |
| 633 | |
Jérôme Glisse | d08faca | 2018-10-30 15:04:20 -0700 | [diff] [blame] | 634 | if (pmd_huge(pmd) && (range->vma->vm_flags & VM_HUGETLB)) |
Jérôme Glisse | da4c3c7 | 2017-09-08 16:11:31 -0700 | [diff] [blame] | 635 | return hmm_pfns_bad(start, end, walk); |
| 636 | |
Jérôme Glisse | d08faca | 2018-10-30 15:04:20 -0700 | [diff] [blame] | 637 | if (thp_migration_supported() && is_pmd_migration_entry(pmd)) { |
| 638 | bool fault, write_fault; |
| 639 | unsigned long npages; |
| 640 | uint64_t *pfns; |
Jérôme Glisse | da4c3c7 | 2017-09-08 16:11:31 -0700 | [diff] [blame] | 641 | |
Jérôme Glisse | d08faca | 2018-10-30 15:04:20 -0700 | [diff] [blame] | 642 | i = (addr - range->start) >> PAGE_SHIFT; |
| 643 | npages = (end - addr) >> PAGE_SHIFT; |
| 644 | pfns = &range->pfns[i]; |
| 645 | |
| 646 | hmm_range_need_fault(hmm_vma_walk, pfns, npages, |
| 647 | 0, &fault, &write_fault); |
| 648 | if (fault || write_fault) { |
| 649 | hmm_vma_walk->last = addr; |
Ralph Campbell | d2e8d55 | 2019-07-25 17:56:45 -0700 | [diff] [blame] | 650 | pmd_migration_entry_wait(walk->mm, pmdp); |
Jérôme Glisse | 7323161 | 2019-05-13 17:19:58 -0700 | [diff] [blame] | 651 | return -EBUSY; |
Jérôme Glisse | d08faca | 2018-10-30 15:04:20 -0700 | [diff] [blame] | 652 | } |
| 653 | return 0; |
| 654 | } else if (!pmd_present(pmd)) |
| 655 | return hmm_pfns_bad(start, end, walk); |
| 656 | |
| 657 | if (pmd_devmap(pmd) || pmd_trans_huge(pmd)) { |
Jérôme Glisse | da4c3c7 | 2017-09-08 16:11:31 -0700 | [diff] [blame] | 658 | /* |
Ralph Campbell | d2e8d55 | 2019-07-25 17:56:45 -0700 | [diff] [blame] | 659 | * No need to take pmd_lock here, even if some other thread |
Jérôme Glisse | da4c3c7 | 2017-09-08 16:11:31 -0700 | [diff] [blame] | 660 | * is splitting the huge pmd we will get that event through |
| 661 | * mmu_notifier callback. |
| 662 | * |
Ralph Campbell | d2e8d55 | 2019-07-25 17:56:45 -0700 | [diff] [blame] | 663 | * So just read pmd value and check again it's a transparent |
Jérôme Glisse | da4c3c7 | 2017-09-08 16:11:31 -0700 | [diff] [blame] | 664 | * huge or device mapping one and compute corresponding pfn |
| 665 | * values. |
| 666 | */ |
| 667 | pmd = pmd_read_atomic(pmdp); |
| 668 | barrier(); |
| 669 | if (!pmd_devmap(pmd) && !pmd_trans_huge(pmd)) |
| 670 | goto again; |
Jérôme Glisse | da4c3c7 | 2017-09-08 16:11:31 -0700 | [diff] [blame] | 671 | |
Jérôme Glisse | d08faca | 2018-10-30 15:04:20 -0700 | [diff] [blame] | 672 | i = (addr - range->start) >> PAGE_SHIFT; |
Jérôme Glisse | 53f5c3f | 2018-04-10 16:28:59 -0700 | [diff] [blame] | 673 | 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] | 674 | } |
| 675 | |
Jérôme Glisse | d08faca | 2018-10-30 15:04:20 -0700 | [diff] [blame] | 676 | /* |
Ralph Campbell | d2e8d55 | 2019-07-25 17:56:45 -0700 | [diff] [blame] | 677 | * We have handled all the valid cases above ie either none, migration, |
Jérôme Glisse | d08faca | 2018-10-30 15:04:20 -0700 | [diff] [blame] | 678 | * huge or transparent huge. At this point either it is a valid pmd |
| 679 | * entry pointing to pte directory or it is a bad pmd that will not |
| 680 | * recover. |
| 681 | */ |
| 682 | if (pmd_bad(pmd)) |
Jérôme Glisse | da4c3c7 | 2017-09-08 16:11:31 -0700 | [diff] [blame] | 683 | return hmm_pfns_bad(start, end, walk); |
| 684 | |
| 685 | ptep = pte_offset_map(pmdp, addr); |
Jérôme Glisse | d08faca | 2018-10-30 15:04:20 -0700 | [diff] [blame] | 686 | i = (addr - range->start) >> PAGE_SHIFT; |
Jérôme Glisse | da4c3c7 | 2017-09-08 16:11:31 -0700 | [diff] [blame] | 687 | for (; addr < end; addr += PAGE_SIZE, ptep++, i++) { |
Jérôme Glisse | 53f5c3f | 2018-04-10 16:28:59 -0700 | [diff] [blame] | 688 | int r; |
Jérôme Glisse | da4c3c7 | 2017-09-08 16:11:31 -0700 | [diff] [blame] | 689 | |
Jérôme Glisse | 53f5c3f | 2018-04-10 16:28:59 -0700 | [diff] [blame] | 690 | r = hmm_vma_handle_pte(walk, addr, end, pmdp, ptep, &pfns[i]); |
| 691 | if (r) { |
| 692 | /* hmm_vma_handle_pte() did unmap pte directory */ |
| 693 | hmm_vma_walk->last = addr; |
| 694 | return r; |
Jérôme Glisse | da4c3c7 | 2017-09-08 16:11:31 -0700 | [diff] [blame] | 695 | } |
Jérôme Glisse | da4c3c7 | 2017-09-08 16:11:31 -0700 | [diff] [blame] | 696 | } |
Jérôme Glisse | 992de9a | 2019-05-13 17:20:21 -0700 | [diff] [blame] | 697 | if (hmm_vma_walk->pgmap) { |
| 698 | /* |
| 699 | * We do put_dev_pagemap() here and not in hmm_vma_handle_pte() |
| 700 | * so that we can leverage get_dev_pagemap() optimization which |
| 701 | * will not re-take a reference on a pgmap if we already have |
| 702 | * one. |
| 703 | */ |
| 704 | put_dev_pagemap(hmm_vma_walk->pgmap); |
| 705 | hmm_vma_walk->pgmap = NULL; |
| 706 | } |
Jérôme Glisse | da4c3c7 | 2017-09-08 16:11:31 -0700 | [diff] [blame] | 707 | pte_unmap(ptep - 1); |
| 708 | |
Jérôme Glisse | 53f5c3f | 2018-04-10 16:28:59 -0700 | [diff] [blame] | 709 | hmm_vma_walk->last = addr; |
Jérôme Glisse | da4c3c7 | 2017-09-08 16:11:31 -0700 | [diff] [blame] | 710 | return 0; |
| 711 | } |
| 712 | |
Jérôme Glisse | 992de9a | 2019-05-13 17:20:21 -0700 | [diff] [blame] | 713 | static int hmm_vma_walk_pud(pud_t *pudp, |
| 714 | unsigned long start, |
| 715 | unsigned long end, |
| 716 | struct mm_walk *walk) |
| 717 | { |
| 718 | struct hmm_vma_walk *hmm_vma_walk = walk->private; |
| 719 | struct hmm_range *range = hmm_vma_walk->range; |
| 720 | unsigned long addr = start, next; |
| 721 | pmd_t *pmdp; |
| 722 | pud_t pud; |
| 723 | int ret; |
| 724 | |
| 725 | again: |
| 726 | pud = READ_ONCE(*pudp); |
| 727 | if (pud_none(pud)) |
| 728 | return hmm_vma_walk_hole(start, end, walk); |
| 729 | |
| 730 | if (pud_huge(pud) && pud_devmap(pud)) { |
| 731 | unsigned long i, npages, pfn; |
| 732 | uint64_t *pfns, cpu_flags; |
| 733 | bool fault, write_fault; |
| 734 | |
| 735 | if (!pud_present(pud)) |
| 736 | return hmm_vma_walk_hole(start, end, walk); |
| 737 | |
| 738 | i = (addr - range->start) >> PAGE_SHIFT; |
| 739 | npages = (end - addr) >> PAGE_SHIFT; |
| 740 | pfns = &range->pfns[i]; |
| 741 | |
| 742 | cpu_flags = pud_to_hmm_pfn_flags(range, pud); |
| 743 | hmm_range_need_fault(hmm_vma_walk, pfns, npages, |
| 744 | cpu_flags, &fault, &write_fault); |
| 745 | if (fault || write_fault) |
| 746 | return hmm_vma_walk_hole_(addr, end, fault, |
| 747 | write_fault, walk); |
| 748 | |
Jérôme Glisse | 992de9a | 2019-05-13 17:20:21 -0700 | [diff] [blame] | 749 | pfn = pud_pfn(pud) + ((addr & ~PUD_MASK) >> PAGE_SHIFT); |
| 750 | for (i = 0; i < npages; ++i, ++pfn) { |
| 751 | hmm_vma_walk->pgmap = get_dev_pagemap(pfn, |
| 752 | hmm_vma_walk->pgmap); |
| 753 | if (unlikely(!hmm_vma_walk->pgmap)) |
| 754 | return -EBUSY; |
Jérôme Glisse | 391aab1 | 2019-05-13 17:20:31 -0700 | [diff] [blame] | 755 | pfns[i] = hmm_device_entry_from_pfn(range, pfn) | |
| 756 | cpu_flags; |
Jérôme Glisse | 992de9a | 2019-05-13 17:20:21 -0700 | [diff] [blame] | 757 | } |
| 758 | if (hmm_vma_walk->pgmap) { |
| 759 | put_dev_pagemap(hmm_vma_walk->pgmap); |
| 760 | hmm_vma_walk->pgmap = NULL; |
| 761 | } |
| 762 | hmm_vma_walk->last = end; |
| 763 | return 0; |
Jérôme Glisse | 992de9a | 2019-05-13 17:20:21 -0700 | [diff] [blame] | 764 | } |
| 765 | |
| 766 | split_huge_pud(walk->vma, pudp, addr); |
| 767 | if (pud_none(*pudp)) |
| 768 | goto again; |
| 769 | |
| 770 | pmdp = pmd_offset(pudp, addr); |
| 771 | do { |
| 772 | next = pmd_addr_end(addr, end); |
| 773 | ret = hmm_vma_walk_pmd(pmdp, addr, next, walk); |
| 774 | if (ret) |
| 775 | return ret; |
| 776 | } while (pmdp++, addr = next, addr != end); |
| 777 | |
| 778 | return 0; |
| 779 | } |
| 780 | |
Jérôme Glisse | 63d5066 | 2019-05-13 17:20:18 -0700 | [diff] [blame] | 781 | static int hmm_vma_walk_hugetlb_entry(pte_t *pte, unsigned long hmask, |
| 782 | unsigned long start, unsigned long end, |
| 783 | struct mm_walk *walk) |
| 784 | { |
| 785 | #ifdef CONFIG_HUGETLB_PAGE |
| 786 | unsigned long addr = start, i, pfn, mask, size, pfn_inc; |
| 787 | struct hmm_vma_walk *hmm_vma_walk = walk->private; |
| 788 | struct hmm_range *range = hmm_vma_walk->range; |
| 789 | struct vm_area_struct *vma = walk->vma; |
| 790 | struct hstate *h = hstate_vma(vma); |
| 791 | uint64_t orig_pfn, cpu_flags; |
| 792 | bool fault, write_fault; |
| 793 | spinlock_t *ptl; |
| 794 | pte_t entry; |
| 795 | int ret = 0; |
| 796 | |
Ralph Campbell | d2e8d55 | 2019-07-25 17:56:45 -0700 | [diff] [blame] | 797 | size = huge_page_size(h); |
Jérôme Glisse | 63d5066 | 2019-05-13 17:20:18 -0700 | [diff] [blame] | 798 | mask = size - 1; |
| 799 | if (range->page_shift != PAGE_SHIFT) { |
Ralph Campbell | d2e8d55 | 2019-07-25 17:56:45 -0700 | [diff] [blame] | 800 | /* Make sure we are looking at a full page. */ |
Jérôme Glisse | 63d5066 | 2019-05-13 17:20:18 -0700 | [diff] [blame] | 801 | if (start & mask) |
| 802 | return -EINVAL; |
| 803 | if (end < (start + size)) |
| 804 | return -EINVAL; |
| 805 | pfn_inc = size >> PAGE_SHIFT; |
| 806 | } else { |
| 807 | pfn_inc = 1; |
| 808 | size = PAGE_SIZE; |
| 809 | } |
| 810 | |
Ralph Campbell | d2e8d55 | 2019-07-25 17:56:45 -0700 | [diff] [blame] | 811 | ptl = huge_pte_lock(hstate_vma(vma), walk->mm, pte); |
Jérôme Glisse | 63d5066 | 2019-05-13 17:20:18 -0700 | [diff] [blame] | 812 | entry = huge_ptep_get(pte); |
| 813 | |
| 814 | i = (start - range->start) >> range->page_shift; |
| 815 | orig_pfn = range->pfns[i]; |
| 816 | range->pfns[i] = range->values[HMM_PFN_NONE]; |
| 817 | cpu_flags = pte_to_hmm_pfn_flags(range, entry); |
| 818 | fault = write_fault = false; |
| 819 | hmm_pte_need_fault(hmm_vma_walk, orig_pfn, cpu_flags, |
| 820 | &fault, &write_fault); |
| 821 | if (fault || write_fault) { |
| 822 | ret = -ENOENT; |
| 823 | goto unlock; |
| 824 | } |
| 825 | |
| 826 | pfn = pte_pfn(entry) + ((start & mask) >> range->page_shift); |
| 827 | for (; addr < end; addr += size, i++, pfn += pfn_inc) |
Jérôme Glisse | 391aab1 | 2019-05-13 17:20:31 -0700 | [diff] [blame] | 828 | range->pfns[i] = hmm_device_entry_from_pfn(range, pfn) | |
| 829 | cpu_flags; |
Jérôme Glisse | 63d5066 | 2019-05-13 17:20:18 -0700 | [diff] [blame] | 830 | hmm_vma_walk->last = end; |
| 831 | |
| 832 | unlock: |
| 833 | spin_unlock(ptl); |
| 834 | |
| 835 | if (ret == -ENOENT) |
| 836 | return hmm_vma_walk_hole_(addr, end, fault, write_fault, walk); |
| 837 | |
| 838 | return ret; |
| 839 | #else /* CONFIG_HUGETLB_PAGE */ |
| 840 | return -EINVAL; |
| 841 | #endif |
| 842 | } |
| 843 | |
Jérôme Glisse | f88a1e9 | 2018-04-10 16:29:06 -0700 | [diff] [blame] | 844 | static void hmm_pfns_clear(struct hmm_range *range, |
| 845 | uint64_t *pfns, |
Jérôme Glisse | 33cd47d | 2018-04-10 16:28:54 -0700 | [diff] [blame] | 846 | unsigned long addr, |
| 847 | unsigned long end) |
| 848 | { |
| 849 | for (; addr < end; addr += PAGE_SIZE, pfns++) |
Jérôme Glisse | f88a1e9 | 2018-04-10 16:29:06 -0700 | [diff] [blame] | 850 | *pfns = range->values[HMM_PFN_NONE]; |
Jérôme Glisse | 33cd47d | 2018-04-10 16:28:54 -0700 | [diff] [blame] | 851 | } |
| 852 | |
Jérôme Glisse | da4c3c7 | 2017-09-08 16:11:31 -0700 | [diff] [blame] | 853 | /* |
Jérôme Glisse | a3e0d41 | 2019-05-13 17:20:01 -0700 | [diff] [blame] | 854 | * hmm_range_register() - start tracking change to CPU page table over a range |
Jérôme Glisse | 25f23a0 | 2019-05-13 17:19:55 -0700 | [diff] [blame] | 855 | * @range: range |
Jérôme Glisse | a3e0d41 | 2019-05-13 17:20:01 -0700 | [diff] [blame] | 856 | * @mm: the mm struct for the range of virtual address |
| 857 | * @start: start virtual address (inclusive) |
| 858 | * @end: end virtual address (exclusive) |
Jérôme Glisse | 63d5066 | 2019-05-13 17:20:18 -0700 | [diff] [blame] | 859 | * @page_shift: expect page shift for the range |
Ralph Campbell | d2e8d55 | 2019-07-25 17:56:45 -0700 | [diff] [blame] | 860 | * Return: 0 on success, -EFAULT if the address space is no longer valid |
Jérôme Glisse | 25f23a0 | 2019-05-13 17:19:55 -0700 | [diff] [blame] | 861 | * |
Jérôme Glisse | a3e0d41 | 2019-05-13 17:20:01 -0700 | [diff] [blame] | 862 | * Track updates to the CPU page table see include/linux/hmm.h |
Jérôme Glisse | da4c3c7 | 2017-09-08 16:11:31 -0700 | [diff] [blame] | 863 | */ |
Jérôme Glisse | a3e0d41 | 2019-05-13 17:20:01 -0700 | [diff] [blame] | 864 | int hmm_range_register(struct hmm_range *range, |
Jason Gunthorpe | e36acfe | 2019-05-23 09:41:19 -0300 | [diff] [blame] | 865 | struct hmm_mirror *mirror, |
Jérôme Glisse | a3e0d41 | 2019-05-13 17:20:01 -0700 | [diff] [blame] | 866 | unsigned long start, |
Jérôme Glisse | 63d5066 | 2019-05-13 17:20:18 -0700 | [diff] [blame] | 867 | unsigned long end, |
| 868 | unsigned page_shift) |
Jérôme Glisse | da4c3c7 | 2017-09-08 16:11:31 -0700 | [diff] [blame] | 869 | { |
Jérôme Glisse | 63d5066 | 2019-05-13 17:20:18 -0700 | [diff] [blame] | 870 | unsigned long mask = ((1UL << page_shift) - 1UL); |
Jason Gunthorpe | e36acfe | 2019-05-23 09:41:19 -0300 | [diff] [blame] | 871 | struct hmm *hmm = mirror->hmm; |
Jason Gunthorpe | 5a136b4 | 2019-06-07 12:10:33 -0300 | [diff] [blame] | 872 | unsigned long flags; |
Jérôme Glisse | 63d5066 | 2019-05-13 17:20:18 -0700 | [diff] [blame] | 873 | |
Jérôme Glisse | a3e0d41 | 2019-05-13 17:20:01 -0700 | [diff] [blame] | 874 | range->valid = false; |
Jérôme Glisse | 704f3f2 | 2019-05-13 17:19:48 -0700 | [diff] [blame] | 875 | range->hmm = NULL; |
| 876 | |
Jérôme Glisse | 63d5066 | 2019-05-13 17:20:18 -0700 | [diff] [blame] | 877 | if ((start & mask) || (end & mask)) |
| 878 | return -EINVAL; |
| 879 | if (start >= end) |
Jérôme Glisse | da4c3c7 | 2017-09-08 16:11:31 -0700 | [diff] [blame] | 880 | return -EINVAL; |
| 881 | |
Jérôme Glisse | 63d5066 | 2019-05-13 17:20:18 -0700 | [diff] [blame] | 882 | range->page_shift = page_shift; |
Jérôme Glisse | a3e0d41 | 2019-05-13 17:20:01 -0700 | [diff] [blame] | 883 | range->start = start; |
| 884 | range->end = end; |
| 885 | |
Jason Gunthorpe | 47f2459 | 2019-05-23 11:08:28 -0300 | [diff] [blame] | 886 | /* Prevent hmm_release() from running while the range is valid */ |
| 887 | if (!mmget_not_zero(hmm->mm)) |
Jérôme Glisse | a3e0d41 | 2019-05-13 17:20:01 -0700 | [diff] [blame] | 888 | return -EFAULT; |
Jérôme Glisse | 704f3f2 | 2019-05-13 17:19:48 -0700 | [diff] [blame] | 889 | |
Ralph Campbell | 085ea25 | 2019-05-06 16:29:39 -0700 | [diff] [blame] | 890 | /* Initialize range to track CPU page table updates. */ |
Jason Gunthorpe | 5a136b4 | 2019-06-07 12:10:33 -0300 | [diff] [blame] | 891 | spin_lock_irqsave(&hmm->ranges_lock, flags); |
Jérôme Glisse | 86586a4 | 2018-04-10 16:28:34 -0700 | [diff] [blame] | 892 | |
Ralph Campbell | 085ea25 | 2019-05-06 16:29:39 -0700 | [diff] [blame] | 893 | range->hmm = hmm; |
Jason Gunthorpe | e36acfe | 2019-05-23 09:41:19 -0300 | [diff] [blame] | 894 | kref_get(&hmm->kref); |
Jason Gunthorpe | 157816f | 2019-05-23 11:43:43 -0300 | [diff] [blame] | 895 | list_add(&range->list, &hmm->ranges); |
Jérôme Glisse | 74eee18 | 2017-09-08 16:11:35 -0700 | [diff] [blame] | 896 | |
Jérôme Glisse | 704f3f2 | 2019-05-13 17:19:48 -0700 | [diff] [blame] | 897 | /* |
Jérôme Glisse | a3e0d41 | 2019-05-13 17:20:01 -0700 | [diff] [blame] | 898 | * If there are any concurrent notifiers we have to wait for them for |
| 899 | * the range to be valid (see hmm_range_wait_until_valid()). |
Jérôme Glisse | 704f3f2 | 2019-05-13 17:19:48 -0700 | [diff] [blame] | 900 | */ |
Ralph Campbell | 085ea25 | 2019-05-06 16:29:39 -0700 | [diff] [blame] | 901 | if (!hmm->notifiers) |
Jérôme Glisse | a3e0d41 | 2019-05-13 17:20:01 -0700 | [diff] [blame] | 902 | range->valid = true; |
Jason Gunthorpe | 5a136b4 | 2019-06-07 12:10:33 -0300 | [diff] [blame] | 903 | spin_unlock_irqrestore(&hmm->ranges_lock, flags); |
Jérôme Glisse | a3e0d41 | 2019-05-13 17:20:01 -0700 | [diff] [blame] | 904 | |
| 905 | return 0; |
| 906 | } |
| 907 | EXPORT_SYMBOL(hmm_range_register); |
| 908 | |
| 909 | /* |
| 910 | * hmm_range_unregister() - stop tracking change to CPU page table over a range |
| 911 | * @range: range |
| 912 | * |
| 913 | * Range struct is used to track updates to the CPU page table after a call to |
| 914 | * hmm_range_register(). See include/linux/hmm.h for how to use it. |
| 915 | */ |
| 916 | void hmm_range_unregister(struct hmm_range *range) |
| 917 | { |
Ralph Campbell | 085ea25 | 2019-05-06 16:29:39 -0700 | [diff] [blame] | 918 | struct hmm *hmm = range->hmm; |
Jason Gunthorpe | 5a136b4 | 2019-06-07 12:10:33 -0300 | [diff] [blame] | 919 | unsigned long flags; |
Jérôme Glisse | a3e0d41 | 2019-05-13 17:20:01 -0700 | [diff] [blame] | 920 | |
Jason Gunthorpe | 5a136b4 | 2019-06-07 12:10:33 -0300 | [diff] [blame] | 921 | spin_lock_irqsave(&hmm->ranges_lock, flags); |
Jason Gunthorpe | 47f2459 | 2019-05-23 11:08:28 -0300 | [diff] [blame] | 922 | list_del_init(&range->list); |
Jason Gunthorpe | 5a136b4 | 2019-06-07 12:10:33 -0300 | [diff] [blame] | 923 | spin_unlock_irqrestore(&hmm->ranges_lock, flags); |
Jérôme Glisse | a3e0d41 | 2019-05-13 17:20:01 -0700 | [diff] [blame] | 924 | |
| 925 | /* Drop reference taken by hmm_range_register() */ |
Jason Gunthorpe | 47f2459 | 2019-05-23 11:08:28 -0300 | [diff] [blame] | 926 | mmput(hmm->mm); |
Ralph Campbell | 085ea25 | 2019-05-06 16:29:39 -0700 | [diff] [blame] | 927 | hmm_put(hmm); |
Jason Gunthorpe | 2dcc3eb | 2019-05-23 11:40:24 -0300 | [diff] [blame] | 928 | |
| 929 | /* |
| 930 | * The range is now invalid and the ref on the hmm is dropped, so |
| 931 | * poison the pointer. Leave other fields in place, for the caller's |
| 932 | * use. |
| 933 | */ |
Jérôme Glisse | a3e0d41 | 2019-05-13 17:20:01 -0700 | [diff] [blame] | 934 | range->valid = false; |
Jason Gunthorpe | 2dcc3eb | 2019-05-23 11:40:24 -0300 | [diff] [blame] | 935 | memset(&range->hmm, POISON_INUSE, sizeof(range->hmm)); |
Jérôme Glisse | a3e0d41 | 2019-05-13 17:20:01 -0700 | [diff] [blame] | 936 | } |
| 937 | EXPORT_SYMBOL(hmm_range_unregister); |
| 938 | |
| 939 | /* |
| 940 | * hmm_range_snapshot() - snapshot CPU page table for a range |
| 941 | * @range: range |
Ralph Campbell | 085ea25 | 2019-05-06 16:29:39 -0700 | [diff] [blame] | 942 | * Return: -EINVAL if invalid argument, -ENOMEM out of memory, -EPERM invalid |
Jérôme Glisse | a3e0d41 | 2019-05-13 17:20:01 -0700 | [diff] [blame] | 943 | * permission (for instance asking for write and range is read only), |
Christoph Hellwig | 2bcbeae | 2019-07-24 08:52:52 +0200 | [diff] [blame] | 944 | * -EBUSY if you need to retry, -EFAULT invalid (ie either no valid |
Jérôme Glisse | a3e0d41 | 2019-05-13 17:20:01 -0700 | [diff] [blame] | 945 | * vma or it is illegal to access that range), number of valid pages |
| 946 | * in range->pfns[] (from range start address). |
| 947 | * |
| 948 | * This snapshots the CPU page table for a range of virtual addresses. Snapshot |
| 949 | * validity is tracked by range struct. See in include/linux/hmm.h for example |
| 950 | * on how to use. |
| 951 | */ |
| 952 | long hmm_range_snapshot(struct hmm_range *range) |
| 953 | { |
Jérôme Glisse | 63d5066 | 2019-05-13 17:20:18 -0700 | [diff] [blame] | 954 | const unsigned long device_vma = VM_IO | VM_PFNMAP | VM_MIXEDMAP; |
Jérôme Glisse | a3e0d41 | 2019-05-13 17:20:01 -0700 | [diff] [blame] | 955 | unsigned long start = range->start, end; |
| 956 | struct hmm_vma_walk hmm_vma_walk; |
| 957 | struct hmm *hmm = range->hmm; |
| 958 | struct vm_area_struct *vma; |
| 959 | struct mm_walk mm_walk; |
| 960 | |
Jason Gunthorpe | 47f2459 | 2019-05-23 11:08:28 -0300 | [diff] [blame] | 961 | lockdep_assert_held(&hmm->mm->mmap_sem); |
Jérôme Glisse | a3e0d41 | 2019-05-13 17:20:01 -0700 | [diff] [blame] | 962 | do { |
| 963 | /* If range is no longer valid force retry. */ |
| 964 | if (!range->valid) |
Christoph Hellwig | 2bcbeae | 2019-07-24 08:52:52 +0200 | [diff] [blame] | 965 | return -EBUSY; |
Jérôme Glisse | a3e0d41 | 2019-05-13 17:20:01 -0700 | [diff] [blame] | 966 | |
| 967 | vma = find_vma(hmm->mm, start); |
Jérôme Glisse | 63d5066 | 2019-05-13 17:20:18 -0700 | [diff] [blame] | 968 | if (vma == NULL || (vma->vm_flags & device_vma)) |
Jérôme Glisse | a3e0d41 | 2019-05-13 17:20:01 -0700 | [diff] [blame] | 969 | return -EFAULT; |
| 970 | |
Jérôme Glisse | 63d5066 | 2019-05-13 17:20:18 -0700 | [diff] [blame] | 971 | if (is_vm_hugetlb_page(vma)) { |
Jason Gunthorpe | 1c2308f0 | 2019-05-27 17:02:21 -0300 | [diff] [blame] | 972 | if (huge_page_shift(hstate_vma(vma)) != |
| 973 | range->page_shift && |
Jérôme Glisse | 63d5066 | 2019-05-13 17:20:18 -0700 | [diff] [blame] | 974 | range->page_shift != PAGE_SHIFT) |
| 975 | return -EINVAL; |
| 976 | } else { |
| 977 | if (range->page_shift != PAGE_SHIFT) |
| 978 | return -EINVAL; |
| 979 | } |
| 980 | |
Jérôme Glisse | a3e0d41 | 2019-05-13 17:20:01 -0700 | [diff] [blame] | 981 | if (!(vma->vm_flags & VM_READ)) { |
| 982 | /* |
| 983 | * If vma do not allow read access, then assume that it |
| 984 | * does not allow write access, either. HMM does not |
| 985 | * support architecture that allow write without read. |
| 986 | */ |
| 987 | hmm_pfns_clear(range, range->pfns, |
| 988 | range->start, range->end); |
| 989 | return -EPERM; |
| 990 | } |
| 991 | |
| 992 | range->vma = vma; |
Jérôme Glisse | 992de9a | 2019-05-13 17:20:21 -0700 | [diff] [blame] | 993 | hmm_vma_walk.pgmap = NULL; |
Jérôme Glisse | a3e0d41 | 2019-05-13 17:20:01 -0700 | [diff] [blame] | 994 | hmm_vma_walk.last = start; |
| 995 | hmm_vma_walk.fault = false; |
| 996 | hmm_vma_walk.range = range; |
| 997 | mm_walk.private = &hmm_vma_walk; |
| 998 | end = min(range->end, vma->vm_end); |
| 999 | |
| 1000 | mm_walk.vma = vma; |
| 1001 | mm_walk.mm = vma->vm_mm; |
| 1002 | mm_walk.pte_entry = NULL; |
| 1003 | mm_walk.test_walk = NULL; |
| 1004 | mm_walk.hugetlb_entry = NULL; |
Jérôme Glisse | 992de9a | 2019-05-13 17:20:21 -0700 | [diff] [blame] | 1005 | mm_walk.pud_entry = hmm_vma_walk_pud; |
Jérôme Glisse | a3e0d41 | 2019-05-13 17:20:01 -0700 | [diff] [blame] | 1006 | mm_walk.pmd_entry = hmm_vma_walk_pmd; |
| 1007 | mm_walk.pte_hole = hmm_vma_walk_hole; |
Jérôme Glisse | 63d5066 | 2019-05-13 17:20:18 -0700 | [diff] [blame] | 1008 | mm_walk.hugetlb_entry = hmm_vma_walk_hugetlb_entry; |
Jérôme Glisse | a3e0d41 | 2019-05-13 17:20:01 -0700 | [diff] [blame] | 1009 | |
| 1010 | walk_page_range(start, end, &mm_walk); |
| 1011 | start = end; |
| 1012 | } while (start < range->end); |
| 1013 | |
Jérôme Glisse | 25f23a0 | 2019-05-13 17:19:55 -0700 | [diff] [blame] | 1014 | return (hmm_vma_walk.last - range->start) >> PAGE_SHIFT; |
Jérôme Glisse | da4c3c7 | 2017-09-08 16:11:31 -0700 | [diff] [blame] | 1015 | } |
Jérôme Glisse | 25f23a0 | 2019-05-13 17:19:55 -0700 | [diff] [blame] | 1016 | EXPORT_SYMBOL(hmm_range_snapshot); |
Jérôme Glisse | da4c3c7 | 2017-09-08 16:11:31 -0700 | [diff] [blame] | 1017 | |
Christoph Hellwig | 9a4903e | 2019-07-25 17:56:46 -0700 | [diff] [blame^] | 1018 | /** |
| 1019 | * hmm_range_fault - try to fault some address in a virtual address range |
| 1020 | * @range: range being faulted |
| 1021 | * @flags: HMM_FAULT_* flags |
Jérôme Glisse | 7323161 | 2019-05-13 17:19:58 -0700 | [diff] [blame] | 1022 | * |
Christoph Hellwig | 9a4903e | 2019-07-25 17:56:46 -0700 | [diff] [blame^] | 1023 | * Return: the number of valid pages in range->pfns[] (from range start |
| 1024 | * address), which may be zero. On error one of the following status codes |
| 1025 | * can be returned: |
| 1026 | * |
| 1027 | * -EINVAL: Invalid arguments or mm or virtual address is in an invalid vma |
| 1028 | * (e.g., device file vma). |
| 1029 | * -ENOMEM: Out of memory. |
| 1030 | * -EPERM: Invalid permission (e.g., asking for write and range is read |
| 1031 | * only). |
| 1032 | * -EAGAIN: A page fault needs to be retried and mmap_sem was dropped. |
| 1033 | * -EBUSY: The range has been invalidated and the caller needs to wait for |
| 1034 | * the invalidation to finish. |
| 1035 | * -EFAULT: Invalid (i.e., either no valid vma or it is illegal to access |
| 1036 | * that range) number of valid pages in range->pfns[] (from |
| 1037 | * range start address). |
Jérôme Glisse | 74eee18 | 2017-09-08 16:11:35 -0700 | [diff] [blame] | 1038 | * |
| 1039 | * This is similar to a regular CPU page fault except that it will not trigger |
Jérôme Glisse | 7323161 | 2019-05-13 17:19:58 -0700 | [diff] [blame] | 1040 | * any memory migration if the memory being faulted is not accessible by CPUs |
| 1041 | * and caller does not ask for migration. |
Jérôme Glisse | 74eee18 | 2017-09-08 16:11:35 -0700 | [diff] [blame] | 1042 | * |
Jérôme Glisse | ff05c0c | 2018-04-10 16:28:38 -0700 | [diff] [blame] | 1043 | * On error, for one virtual address in the range, the function will mark the |
| 1044 | * corresponding HMM pfn entry with an error flag. |
Jérôme Glisse | 74eee18 | 2017-09-08 16:11:35 -0700 | [diff] [blame] | 1045 | */ |
Christoph Hellwig | 9a4903e | 2019-07-25 17:56:46 -0700 | [diff] [blame^] | 1046 | long hmm_range_fault(struct hmm_range *range, unsigned int flags) |
Jérôme Glisse | 74eee18 | 2017-09-08 16:11:35 -0700 | [diff] [blame] | 1047 | { |
Jérôme Glisse | 63d5066 | 2019-05-13 17:20:18 -0700 | [diff] [blame] | 1048 | const unsigned long device_vma = VM_IO | VM_PFNMAP | VM_MIXEDMAP; |
Jérôme Glisse | a3e0d41 | 2019-05-13 17:20:01 -0700 | [diff] [blame] | 1049 | unsigned long start = range->start, end; |
Jérôme Glisse | 74eee18 | 2017-09-08 16:11:35 -0700 | [diff] [blame] | 1050 | struct hmm_vma_walk hmm_vma_walk; |
Jérôme Glisse | a3e0d41 | 2019-05-13 17:20:01 -0700 | [diff] [blame] | 1051 | struct hmm *hmm = range->hmm; |
| 1052 | struct vm_area_struct *vma; |
Jérôme Glisse | 74eee18 | 2017-09-08 16:11:35 -0700 | [diff] [blame] | 1053 | struct mm_walk mm_walk; |
Jérôme Glisse | 74eee18 | 2017-09-08 16:11:35 -0700 | [diff] [blame] | 1054 | int ret; |
| 1055 | |
Jason Gunthorpe | 47f2459 | 2019-05-23 11:08:28 -0300 | [diff] [blame] | 1056 | lockdep_assert_held(&hmm->mm->mmap_sem); |
Jérôme Glisse | 74eee18 | 2017-09-08 16:11:35 -0700 | [diff] [blame] | 1057 | |
| 1058 | do { |
Jérôme Glisse | a3e0d41 | 2019-05-13 17:20:01 -0700 | [diff] [blame] | 1059 | /* If range is no longer valid force retry. */ |
Christoph Hellwig | 2bcbeae | 2019-07-24 08:52:52 +0200 | [diff] [blame] | 1060 | if (!range->valid) |
| 1061 | return -EBUSY; |
Jérôme Glisse | 74eee18 | 2017-09-08 16:11:35 -0700 | [diff] [blame] | 1062 | |
Jérôme Glisse | a3e0d41 | 2019-05-13 17:20:01 -0700 | [diff] [blame] | 1063 | vma = find_vma(hmm->mm, start); |
Jérôme Glisse | 63d5066 | 2019-05-13 17:20:18 -0700 | [diff] [blame] | 1064 | if (vma == NULL || (vma->vm_flags & device_vma)) |
Jérôme Glisse | a3e0d41 | 2019-05-13 17:20:01 -0700 | [diff] [blame] | 1065 | return -EFAULT; |
Jérôme Glisse | 74eee18 | 2017-09-08 16:11:35 -0700 | [diff] [blame] | 1066 | |
Jérôme Glisse | 63d5066 | 2019-05-13 17:20:18 -0700 | [diff] [blame] | 1067 | if (is_vm_hugetlb_page(vma)) { |
| 1068 | if (huge_page_shift(hstate_vma(vma)) != |
| 1069 | range->page_shift && |
| 1070 | range->page_shift != PAGE_SHIFT) |
| 1071 | return -EINVAL; |
| 1072 | } else { |
| 1073 | if (range->page_shift != PAGE_SHIFT) |
| 1074 | return -EINVAL; |
| 1075 | } |
| 1076 | |
Jérôme Glisse | a3e0d41 | 2019-05-13 17:20:01 -0700 | [diff] [blame] | 1077 | if (!(vma->vm_flags & VM_READ)) { |
| 1078 | /* |
| 1079 | * If vma do not allow read access, then assume that it |
| 1080 | * does not allow write access, either. HMM does not |
| 1081 | * support architecture that allow write without read. |
| 1082 | */ |
| 1083 | hmm_pfns_clear(range, range->pfns, |
| 1084 | range->start, range->end); |
| 1085 | return -EPERM; |
| 1086 | } |
| 1087 | |
| 1088 | range->vma = vma; |
Jérôme Glisse | 992de9a | 2019-05-13 17:20:21 -0700 | [diff] [blame] | 1089 | hmm_vma_walk.pgmap = NULL; |
Jérôme Glisse | a3e0d41 | 2019-05-13 17:20:01 -0700 | [diff] [blame] | 1090 | hmm_vma_walk.last = start; |
| 1091 | hmm_vma_walk.fault = true; |
Christoph Hellwig | 9a4903e | 2019-07-25 17:56:46 -0700 | [diff] [blame^] | 1092 | hmm_vma_walk.flags = flags; |
Jérôme Glisse | a3e0d41 | 2019-05-13 17:20:01 -0700 | [diff] [blame] | 1093 | hmm_vma_walk.range = range; |
| 1094 | mm_walk.private = &hmm_vma_walk; |
| 1095 | end = min(range->end, vma->vm_end); |
| 1096 | |
| 1097 | mm_walk.vma = vma; |
| 1098 | mm_walk.mm = vma->vm_mm; |
| 1099 | mm_walk.pte_entry = NULL; |
| 1100 | mm_walk.test_walk = NULL; |
| 1101 | mm_walk.hugetlb_entry = NULL; |
Jérôme Glisse | 992de9a | 2019-05-13 17:20:21 -0700 | [diff] [blame] | 1102 | mm_walk.pud_entry = hmm_vma_walk_pud; |
Jérôme Glisse | a3e0d41 | 2019-05-13 17:20:01 -0700 | [diff] [blame] | 1103 | mm_walk.pmd_entry = hmm_vma_walk_pmd; |
| 1104 | mm_walk.pte_hole = hmm_vma_walk_hole; |
Jérôme Glisse | 63d5066 | 2019-05-13 17:20:18 -0700 | [diff] [blame] | 1105 | mm_walk.hugetlb_entry = hmm_vma_walk_hugetlb_entry; |
Jérôme Glisse | a3e0d41 | 2019-05-13 17:20:01 -0700 | [diff] [blame] | 1106 | |
| 1107 | do { |
| 1108 | ret = walk_page_range(start, end, &mm_walk); |
| 1109 | start = hmm_vma_walk.last; |
| 1110 | |
| 1111 | /* Keep trying while the range is valid. */ |
| 1112 | } while (ret == -EBUSY && range->valid); |
| 1113 | |
| 1114 | if (ret) { |
| 1115 | unsigned long i; |
| 1116 | |
| 1117 | i = (hmm_vma_walk.last - range->start) >> PAGE_SHIFT; |
| 1118 | hmm_pfns_clear(range, &range->pfns[i], |
| 1119 | hmm_vma_walk.last, range->end); |
| 1120 | return ret; |
| 1121 | } |
| 1122 | start = end; |
| 1123 | |
| 1124 | } while (start < range->end); |
Jérôme Glisse | 704f3f2 | 2019-05-13 17:19:48 -0700 | [diff] [blame] | 1125 | |
Jérôme Glisse | 7323161 | 2019-05-13 17:19:58 -0700 | [diff] [blame] | 1126 | return (hmm_vma_walk.last - range->start) >> PAGE_SHIFT; |
Jérôme Glisse | 74eee18 | 2017-09-08 16:11:35 -0700 | [diff] [blame] | 1127 | } |
Jérôme Glisse | 7323161 | 2019-05-13 17:19:58 -0700 | [diff] [blame] | 1128 | EXPORT_SYMBOL(hmm_range_fault); |
Jérôme Glisse | 55c0ece | 2019-05-13 17:20:28 -0700 | [diff] [blame] | 1129 | |
| 1130 | /** |
Christoph Hellwig | 9a4903e | 2019-07-25 17:56:46 -0700 | [diff] [blame^] | 1131 | * hmm_range_dma_map - hmm_range_fault() and dma map page all in one. |
| 1132 | * @range: range being faulted |
| 1133 | * @device: device to map page to |
| 1134 | * @daddrs: array of dma addresses for the mapped pages |
| 1135 | * @flags: HMM_FAULT_* |
Jérôme Glisse | 55c0ece | 2019-05-13 17:20:28 -0700 | [diff] [blame] | 1136 | * |
Christoph Hellwig | 9a4903e | 2019-07-25 17:56:46 -0700 | [diff] [blame^] | 1137 | * Return: the number of pages mapped on success (including zero), or any |
| 1138 | * status return from hmm_range_fault() otherwise. |
Jérôme Glisse | 55c0ece | 2019-05-13 17:20:28 -0700 | [diff] [blame] | 1139 | */ |
Christoph Hellwig | 9a4903e | 2019-07-25 17:56:46 -0700 | [diff] [blame^] | 1140 | long hmm_range_dma_map(struct hmm_range *range, struct device *device, |
| 1141 | dma_addr_t *daddrs, unsigned int flags) |
Jérôme Glisse | 55c0ece | 2019-05-13 17:20:28 -0700 | [diff] [blame] | 1142 | { |
| 1143 | unsigned long i, npages, mapped; |
| 1144 | long ret; |
| 1145 | |
Christoph Hellwig | 9a4903e | 2019-07-25 17:56:46 -0700 | [diff] [blame^] | 1146 | ret = hmm_range_fault(range, flags); |
Jérôme Glisse | 55c0ece | 2019-05-13 17:20:28 -0700 | [diff] [blame] | 1147 | if (ret <= 0) |
| 1148 | return ret ? ret : -EBUSY; |
| 1149 | |
| 1150 | npages = (range->end - range->start) >> PAGE_SHIFT; |
| 1151 | for (i = 0, mapped = 0; i < npages; ++i) { |
| 1152 | enum dma_data_direction dir = DMA_TO_DEVICE; |
| 1153 | struct page *page; |
| 1154 | |
| 1155 | /* |
| 1156 | * FIXME need to update DMA API to provide invalid DMA address |
| 1157 | * value instead of a function to test dma address value. This |
| 1158 | * would remove lot of dumb code duplicated accross many arch. |
| 1159 | * |
| 1160 | * For now setting it to 0 here is good enough as the pfns[] |
| 1161 | * value is what is use to check what is valid and what isn't. |
| 1162 | */ |
| 1163 | daddrs[i] = 0; |
| 1164 | |
Jérôme Glisse | 391aab1 | 2019-05-13 17:20:31 -0700 | [diff] [blame] | 1165 | page = hmm_device_entry_to_page(range, range->pfns[i]); |
Jérôme Glisse | 55c0ece | 2019-05-13 17:20:28 -0700 | [diff] [blame] | 1166 | if (page == NULL) |
| 1167 | continue; |
| 1168 | |
| 1169 | /* Check if range is being invalidated */ |
| 1170 | if (!range->valid) { |
| 1171 | ret = -EBUSY; |
| 1172 | goto unmap; |
| 1173 | } |
| 1174 | |
| 1175 | /* If it is read and write than map bi-directional. */ |
| 1176 | if (range->pfns[i] & range->flags[HMM_PFN_WRITE]) |
| 1177 | dir = DMA_BIDIRECTIONAL; |
| 1178 | |
| 1179 | daddrs[i] = dma_map_page(device, page, 0, PAGE_SIZE, dir); |
| 1180 | if (dma_mapping_error(device, daddrs[i])) { |
| 1181 | ret = -EFAULT; |
| 1182 | goto unmap; |
| 1183 | } |
| 1184 | |
| 1185 | mapped++; |
| 1186 | } |
| 1187 | |
| 1188 | return mapped; |
| 1189 | |
| 1190 | unmap: |
| 1191 | for (npages = i, i = 0; (i < npages) && mapped; ++i) { |
| 1192 | enum dma_data_direction dir = DMA_TO_DEVICE; |
| 1193 | struct page *page; |
| 1194 | |
Jérôme Glisse | 391aab1 | 2019-05-13 17:20:31 -0700 | [diff] [blame] | 1195 | page = hmm_device_entry_to_page(range, range->pfns[i]); |
Jérôme Glisse | 55c0ece | 2019-05-13 17:20:28 -0700 | [diff] [blame] | 1196 | if (page == NULL) |
| 1197 | continue; |
| 1198 | |
| 1199 | if (dma_mapping_error(device, daddrs[i])) |
| 1200 | continue; |
| 1201 | |
| 1202 | /* If it is read and write than map bi-directional. */ |
| 1203 | if (range->pfns[i] & range->flags[HMM_PFN_WRITE]) |
| 1204 | dir = DMA_BIDIRECTIONAL; |
| 1205 | |
| 1206 | dma_unmap_page(device, daddrs[i], PAGE_SIZE, dir); |
| 1207 | mapped--; |
| 1208 | } |
| 1209 | |
| 1210 | return ret; |
| 1211 | } |
| 1212 | EXPORT_SYMBOL(hmm_range_dma_map); |
| 1213 | |
| 1214 | /** |
| 1215 | * hmm_range_dma_unmap() - unmap range of that was map with hmm_range_dma_map() |
| 1216 | * @range: range being unmapped |
| 1217 | * @vma: the vma against which the range (optional) |
| 1218 | * @device: device against which dma map was done |
| 1219 | * @daddrs: dma address of mapped pages |
| 1220 | * @dirty: dirty page if it had the write flag set |
Ralph Campbell | 085ea25 | 2019-05-06 16:29:39 -0700 | [diff] [blame] | 1221 | * Return: number of page unmapped on success, -EINVAL otherwise |
Jérôme Glisse | 55c0ece | 2019-05-13 17:20:28 -0700 | [diff] [blame] | 1222 | * |
| 1223 | * Note that caller MUST abide by mmu notifier or use HMM mirror and abide |
| 1224 | * to the sync_cpu_device_pagetables() callback so that it is safe here to |
| 1225 | * call set_page_dirty(). Caller must also take appropriate locks to avoid |
| 1226 | * concurrent mmu notifier or sync_cpu_device_pagetables() to make progress. |
| 1227 | */ |
| 1228 | long hmm_range_dma_unmap(struct hmm_range *range, |
| 1229 | struct vm_area_struct *vma, |
| 1230 | struct device *device, |
| 1231 | dma_addr_t *daddrs, |
| 1232 | bool dirty) |
| 1233 | { |
| 1234 | unsigned long i, npages; |
| 1235 | long cpages = 0; |
| 1236 | |
| 1237 | /* Sanity check. */ |
| 1238 | if (range->end <= range->start) |
| 1239 | return -EINVAL; |
| 1240 | if (!daddrs) |
| 1241 | return -EINVAL; |
| 1242 | if (!range->pfns) |
| 1243 | return -EINVAL; |
| 1244 | |
| 1245 | npages = (range->end - range->start) >> PAGE_SHIFT; |
| 1246 | for (i = 0; i < npages; ++i) { |
| 1247 | enum dma_data_direction dir = DMA_TO_DEVICE; |
| 1248 | struct page *page; |
| 1249 | |
Jérôme Glisse | 391aab1 | 2019-05-13 17:20:31 -0700 | [diff] [blame] | 1250 | page = hmm_device_entry_to_page(range, range->pfns[i]); |
Jérôme Glisse | 55c0ece | 2019-05-13 17:20:28 -0700 | [diff] [blame] | 1251 | if (page == NULL) |
| 1252 | continue; |
| 1253 | |
| 1254 | /* If it is read and write than map bi-directional. */ |
| 1255 | if (range->pfns[i] & range->flags[HMM_PFN_WRITE]) { |
| 1256 | dir = DMA_BIDIRECTIONAL; |
| 1257 | |
| 1258 | /* |
| 1259 | * See comments in function description on why it is |
| 1260 | * safe here to call set_page_dirty() |
| 1261 | */ |
| 1262 | if (dirty) |
| 1263 | set_page_dirty(page); |
| 1264 | } |
| 1265 | |
| 1266 | /* Unmap and clear pfns/dma address */ |
| 1267 | dma_unmap_page(device, daddrs[i], PAGE_SIZE, dir); |
| 1268 | range->pfns[i] = range->values[HMM_PFN_NONE]; |
| 1269 | /* FIXME see comments in hmm_vma_dma_map() */ |
| 1270 | daddrs[i] = 0; |
| 1271 | cpages++; |
| 1272 | } |
| 1273 | |
| 1274 | return cpages; |
| 1275 | } |
| 1276 | EXPORT_SYMBOL(hmm_range_dma_unmap); |