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; |
Christoph Hellwig | 9a4903e | 2019-07-25 17:56:46 -0700 | [diff] [blame] | 283 | unsigned int flags; |
Jérôme Glisse | 74eee18 | 2017-09-08 16:11:35 -0700 | [diff] [blame] | 284 | }; |
| 285 | |
Jérôme Glisse | 2aee09d | 2018-04-10 16:29:02 -0700 | [diff] [blame] | 286 | static int hmm_vma_do_fault(struct mm_walk *walk, unsigned long addr, |
| 287 | bool write_fault, uint64_t *pfn) |
Jérôme Glisse | 74eee18 | 2017-09-08 16:11:35 -0700 | [diff] [blame] | 288 | { |
Kuehling, Felix | 9b1ae60 | 2019-05-10 19:53:24 +0000 | [diff] [blame] | 289 | unsigned int flags = FAULT_FLAG_REMOTE; |
Jérôme Glisse | 74eee18 | 2017-09-08 16:11:35 -0700 | [diff] [blame] | 290 | struct hmm_vma_walk *hmm_vma_walk = walk->private; |
Jérôme Glisse | f88a1e9 | 2018-04-10 16:29:06 -0700 | [diff] [blame] | 291 | struct hmm_range *range = hmm_vma_walk->range; |
Jérôme Glisse | 74eee18 | 2017-09-08 16:11:35 -0700 | [diff] [blame] | 292 | struct vm_area_struct *vma = walk->vma; |
Souptick Joarder | 50a7ca3 | 2018-08-17 15:44:47 -0700 | [diff] [blame] | 293 | vm_fault_t ret; |
Jérôme Glisse | 74eee18 | 2017-09-08 16:11:35 -0700 | [diff] [blame] | 294 | |
Christoph Hellwig | 9a4903e | 2019-07-25 17:56:46 -0700 | [diff] [blame] | 295 | if (hmm_vma_walk->flags & HMM_FAULT_ALLOW_RETRY) |
| 296 | flags |= FAULT_FLAG_ALLOW_RETRY; |
| 297 | if (write_fault) |
| 298 | flags |= FAULT_FLAG_WRITE; |
| 299 | |
Souptick Joarder | 50a7ca3 | 2018-08-17 15:44:47 -0700 | [diff] [blame] | 300 | ret = handle_mm_fault(vma, addr, flags); |
Jason Gunthorpe | e709acc | 2019-07-24 08:52:58 +0200 | [diff] [blame] | 301 | if (ret & VM_FAULT_RETRY) { |
| 302 | /* Note, handle_mm_fault did up_read(&mm->mmap_sem)) */ |
Jérôme Glisse | 7323161 | 2019-05-13 17:19:58 -0700 | [diff] [blame] | 303 | return -EAGAIN; |
Jason Gunthorpe | e709acc | 2019-07-24 08:52:58 +0200 | [diff] [blame] | 304 | } |
Souptick Joarder | 50a7ca3 | 2018-08-17 15:44:47 -0700 | [diff] [blame] | 305 | if (ret & VM_FAULT_ERROR) { |
Jérôme Glisse | f88a1e9 | 2018-04-10 16:29:06 -0700 | [diff] [blame] | 306 | *pfn = range->values[HMM_PFN_ERROR]; |
Jérôme Glisse | 74eee18 | 2017-09-08 16:11:35 -0700 | [diff] [blame] | 307 | return -EFAULT; |
| 308 | } |
| 309 | |
Jérôme Glisse | 7323161 | 2019-05-13 17:19:58 -0700 | [diff] [blame] | 310 | return -EBUSY; |
Jérôme Glisse | 74eee18 | 2017-09-08 16:11:35 -0700 | [diff] [blame] | 311 | } |
| 312 | |
Jérôme Glisse | da4c3c7 | 2017-09-08 16:11:31 -0700 | [diff] [blame] | 313 | static int hmm_pfns_bad(unsigned long addr, |
| 314 | unsigned long end, |
| 315 | struct mm_walk *walk) |
| 316 | { |
Jérôme Glisse | c719547 | 2018-04-10 16:28:27 -0700 | [diff] [blame] | 317 | struct hmm_vma_walk *hmm_vma_walk = walk->private; |
| 318 | struct hmm_range *range = hmm_vma_walk->range; |
Jérôme Glisse | ff05c0c | 2018-04-10 16:28:38 -0700 | [diff] [blame] | 319 | uint64_t *pfns = range->pfns; |
Jérôme Glisse | da4c3c7 | 2017-09-08 16:11:31 -0700 | [diff] [blame] | 320 | unsigned long i; |
| 321 | |
| 322 | i = (addr - range->start) >> PAGE_SHIFT; |
| 323 | for (; addr < end; addr += PAGE_SIZE, i++) |
Jérôme Glisse | f88a1e9 | 2018-04-10 16:29:06 -0700 | [diff] [blame] | 324 | pfns[i] = range->values[HMM_PFN_ERROR]; |
Jérôme Glisse | da4c3c7 | 2017-09-08 16:11:31 -0700 | [diff] [blame] | 325 | |
| 326 | return 0; |
| 327 | } |
| 328 | |
Jérôme Glisse | 5504ed2 | 2018-04-10 16:28:46 -0700 | [diff] [blame] | 329 | /* |
Ralph Campbell | d2e8d55 | 2019-07-25 17:56:45 -0700 | [diff] [blame] | 330 | * hmm_vma_walk_hole_() - handle a range lacking valid pmd or pte(s) |
| 331 | * @addr: range virtual start address (inclusive) |
Jérôme Glisse | 5504ed2 | 2018-04-10 16:28:46 -0700 | [diff] [blame] | 332 | * @end: range virtual end address (exclusive) |
Jérôme Glisse | 2aee09d | 2018-04-10 16:29:02 -0700 | [diff] [blame] | 333 | * @fault: should we fault or not ? |
| 334 | * @write_fault: write fault ? |
Jérôme Glisse | 5504ed2 | 2018-04-10 16:28:46 -0700 | [diff] [blame] | 335 | * @walk: mm_walk structure |
Ralph Campbell | 085ea25 | 2019-05-06 16:29:39 -0700 | [diff] [blame] | 336 | * 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] | 337 | * |
| 338 | * This function will be called whenever pmd_none() or pte_none() returns true, |
| 339 | * or whenever there is no page directory covering the virtual address range. |
| 340 | */ |
Jérôme Glisse | 2aee09d | 2018-04-10 16:29:02 -0700 | [diff] [blame] | 341 | static int hmm_vma_walk_hole_(unsigned long addr, unsigned long end, |
| 342 | bool fault, bool write_fault, |
| 343 | struct mm_walk *walk) |
Jérôme Glisse | da4c3c7 | 2017-09-08 16:11:31 -0700 | [diff] [blame] | 344 | { |
Jérôme Glisse | 74eee18 | 2017-09-08 16:11:35 -0700 | [diff] [blame] | 345 | struct hmm_vma_walk *hmm_vma_walk = walk->private; |
| 346 | struct hmm_range *range = hmm_vma_walk->range; |
Jérôme Glisse | ff05c0c | 2018-04-10 16:28:38 -0700 | [diff] [blame] | 347 | uint64_t *pfns = range->pfns; |
Christoph Hellwig | 7f08263 | 2019-08-06 19:05:45 +0300 | [diff] [blame] | 348 | unsigned long i; |
Jérôme Glisse | da4c3c7 | 2017-09-08 16:11:31 -0700 | [diff] [blame] | 349 | |
Jérôme Glisse | 74eee18 | 2017-09-08 16:11:35 -0700 | [diff] [blame] | 350 | hmm_vma_walk->last = addr; |
Christoph Hellwig | 7f08263 | 2019-08-06 19:05:45 +0300 | [diff] [blame] | 351 | i = (addr - range->start) >> PAGE_SHIFT; |
Jérôme Glisse | 63d5066 | 2019-05-13 17:20:18 -0700 | [diff] [blame] | 352 | |
Christoph Hellwig | 7f08263 | 2019-08-06 19:05:45 +0300 | [diff] [blame] | 353 | for (; addr < end; addr += PAGE_SIZE, i++) { |
Jérôme Glisse | f88a1e9 | 2018-04-10 16:29:06 -0700 | [diff] [blame] | 354 | pfns[i] = range->values[HMM_PFN_NONE]; |
Jérôme Glisse | 2aee09d | 2018-04-10 16:29:02 -0700 | [diff] [blame] | 355 | if (fault || write_fault) { |
Jérôme Glisse | 74eee18 | 2017-09-08 16:11:35 -0700 | [diff] [blame] | 356 | int ret; |
Jérôme Glisse | da4c3c7 | 2017-09-08 16:11:31 -0700 | [diff] [blame] | 357 | |
Jérôme Glisse | 2aee09d | 2018-04-10 16:29:02 -0700 | [diff] [blame] | 358 | ret = hmm_vma_do_fault(walk, addr, write_fault, |
| 359 | &pfns[i]); |
Jérôme Glisse | 7323161 | 2019-05-13 17:19:58 -0700 | [diff] [blame] | 360 | if (ret != -EBUSY) |
Jérôme Glisse | 74eee18 | 2017-09-08 16:11:35 -0700 | [diff] [blame] | 361 | return ret; |
| 362 | } |
| 363 | } |
| 364 | |
Jérôme Glisse | 7323161 | 2019-05-13 17:19:58 -0700 | [diff] [blame] | 365 | return (fault || write_fault) ? -EBUSY : 0; |
Jérôme Glisse | 2aee09d | 2018-04-10 16:29:02 -0700 | [diff] [blame] | 366 | } |
| 367 | |
| 368 | static inline void hmm_pte_need_fault(const struct hmm_vma_walk *hmm_vma_walk, |
| 369 | uint64_t pfns, uint64_t cpu_flags, |
| 370 | bool *fault, bool *write_fault) |
| 371 | { |
Jérôme Glisse | f88a1e9 | 2018-04-10 16:29:06 -0700 | [diff] [blame] | 372 | struct hmm_range *range = hmm_vma_walk->range; |
| 373 | |
Christoph Hellwig | d45d464 | 2019-07-25 17:56:47 -0700 | [diff] [blame] | 374 | if (hmm_vma_walk->flags & HMM_FAULT_SNAPSHOT) |
Jérôme Glisse | 2aee09d | 2018-04-10 16:29:02 -0700 | [diff] [blame] | 375 | return; |
| 376 | |
Jérôme Glisse | 023a019 | 2019-05-13 17:20:05 -0700 | [diff] [blame] | 377 | /* |
| 378 | * So we not only consider the individual per page request we also |
| 379 | * consider the default flags requested for the range. The API can |
Ralph Campbell | d2e8d55 | 2019-07-25 17:56:45 -0700 | [diff] [blame] | 380 | * be used 2 ways. The first one where the HMM user coalesces |
| 381 | * multiple page faults into one request and sets flags per pfn for |
| 382 | * 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] | 383 | * fault a range with specific flags. For the latter one it is a |
| 384 | * waste to have the user pre-fill the pfn arrays with a default |
| 385 | * flags value. |
| 386 | */ |
| 387 | pfns = (pfns & range->pfn_flags_mask) | range->default_flags; |
| 388 | |
Jérôme Glisse | 2aee09d | 2018-04-10 16:29:02 -0700 | [diff] [blame] | 389 | /* We aren't ask to do anything ... */ |
Jérôme Glisse | f88a1e9 | 2018-04-10 16:29:06 -0700 | [diff] [blame] | 390 | if (!(pfns & range->flags[HMM_PFN_VALID])) |
Jérôme Glisse | 2aee09d | 2018-04-10 16:29:02 -0700 | [diff] [blame] | 391 | return; |
Ralph Campbell | d2e8d55 | 2019-07-25 17:56:45 -0700 | [diff] [blame] | 392 | /* 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] | 393 | if ((cpu_flags & range->flags[HMM_PFN_DEVICE_PRIVATE])) { |
| 394 | /* Do we fault on device memory ? */ |
| 395 | if (pfns & range->flags[HMM_PFN_DEVICE_PRIVATE]) { |
| 396 | *write_fault = pfns & range->flags[HMM_PFN_WRITE]; |
| 397 | *fault = true; |
| 398 | } |
Jérôme Glisse | 2aee09d | 2018-04-10 16:29:02 -0700 | [diff] [blame] | 399 | return; |
| 400 | } |
Jérôme Glisse | f88a1e9 | 2018-04-10 16:29:06 -0700 | [diff] [blame] | 401 | |
| 402 | /* If CPU page table is not valid then we need to fault */ |
| 403 | *fault = !(cpu_flags & range->flags[HMM_PFN_VALID]); |
| 404 | /* Need to write fault ? */ |
| 405 | if ((pfns & range->flags[HMM_PFN_WRITE]) && |
| 406 | !(cpu_flags & range->flags[HMM_PFN_WRITE])) { |
| 407 | *write_fault = true; |
Jérôme Glisse | 2aee09d | 2018-04-10 16:29:02 -0700 | [diff] [blame] | 408 | *fault = true; |
| 409 | } |
| 410 | } |
| 411 | |
| 412 | static void hmm_range_need_fault(const struct hmm_vma_walk *hmm_vma_walk, |
| 413 | const uint64_t *pfns, unsigned long npages, |
| 414 | uint64_t cpu_flags, bool *fault, |
| 415 | bool *write_fault) |
| 416 | { |
| 417 | unsigned long i; |
| 418 | |
Christoph Hellwig | d45d464 | 2019-07-25 17:56:47 -0700 | [diff] [blame] | 419 | if (hmm_vma_walk->flags & HMM_FAULT_SNAPSHOT) { |
Jérôme Glisse | 2aee09d | 2018-04-10 16:29:02 -0700 | [diff] [blame] | 420 | *fault = *write_fault = false; |
| 421 | return; |
| 422 | } |
| 423 | |
Jérôme Glisse | a3e0d41 | 2019-05-13 17:20:01 -0700 | [diff] [blame] | 424 | *fault = *write_fault = false; |
Jérôme Glisse | 2aee09d | 2018-04-10 16:29:02 -0700 | [diff] [blame] | 425 | for (i = 0; i < npages; ++i) { |
| 426 | hmm_pte_need_fault(hmm_vma_walk, pfns[i], cpu_flags, |
| 427 | fault, write_fault); |
Jérôme Glisse | a3e0d41 | 2019-05-13 17:20:01 -0700 | [diff] [blame] | 428 | if ((*write_fault)) |
Jérôme Glisse | 2aee09d | 2018-04-10 16:29:02 -0700 | [diff] [blame] | 429 | return; |
| 430 | } |
| 431 | } |
| 432 | |
| 433 | static int hmm_vma_walk_hole(unsigned long addr, unsigned long end, |
| 434 | struct mm_walk *walk) |
| 435 | { |
| 436 | struct hmm_vma_walk *hmm_vma_walk = walk->private; |
| 437 | struct hmm_range *range = hmm_vma_walk->range; |
| 438 | bool fault, write_fault; |
| 439 | unsigned long i, npages; |
| 440 | uint64_t *pfns; |
| 441 | |
| 442 | i = (addr - range->start) >> PAGE_SHIFT; |
| 443 | npages = (end - addr) >> PAGE_SHIFT; |
| 444 | pfns = &range->pfns[i]; |
| 445 | hmm_range_need_fault(hmm_vma_walk, pfns, npages, |
| 446 | 0, &fault, &write_fault); |
| 447 | return hmm_vma_walk_hole_(addr, end, fault, write_fault, walk); |
| 448 | } |
| 449 | |
Jérôme Glisse | f88a1e9 | 2018-04-10 16:29:06 -0700 | [diff] [blame] | 450 | static inline uint64_t pmd_to_hmm_pfn_flags(struct hmm_range *range, pmd_t pmd) |
Jérôme Glisse | 2aee09d | 2018-04-10 16:29:02 -0700 | [diff] [blame] | 451 | { |
| 452 | if (pmd_protnone(pmd)) |
| 453 | return 0; |
Jérôme Glisse | f88a1e9 | 2018-04-10 16:29:06 -0700 | [diff] [blame] | 454 | return pmd_write(pmd) ? range->flags[HMM_PFN_VALID] | |
| 455 | range->flags[HMM_PFN_WRITE] : |
| 456 | range->flags[HMM_PFN_VALID]; |
Jérôme Glisse | da4c3c7 | 2017-09-08 16:11:31 -0700 | [diff] [blame] | 457 | } |
| 458 | |
Jérôme Glisse | 992de9a | 2019-05-13 17:20:21 -0700 | [diff] [blame] | 459 | static inline uint64_t pud_to_hmm_pfn_flags(struct hmm_range *range, pud_t pud) |
| 460 | { |
| 461 | if (!pud_present(pud)) |
| 462 | return 0; |
| 463 | return pud_write(pud) ? range->flags[HMM_PFN_VALID] | |
| 464 | range->flags[HMM_PFN_WRITE] : |
| 465 | range->flags[HMM_PFN_VALID]; |
| 466 | } |
| 467 | |
Jérôme Glisse | 53f5c3f | 2018-04-10 16:28:59 -0700 | [diff] [blame] | 468 | static int hmm_vma_handle_pmd(struct mm_walk *walk, |
| 469 | unsigned long addr, |
| 470 | unsigned long end, |
| 471 | uint64_t *pfns, |
| 472 | pmd_t pmd) |
| 473 | { |
Jérôme Glisse | 992de9a | 2019-05-13 17:20:21 -0700 | [diff] [blame] | 474 | #ifdef CONFIG_TRANSPARENT_HUGEPAGE |
Jérôme Glisse | 53f5c3f | 2018-04-10 16:28:59 -0700 | [diff] [blame] | 475 | struct hmm_vma_walk *hmm_vma_walk = walk->private; |
Jérôme Glisse | f88a1e9 | 2018-04-10 16:29:06 -0700 | [diff] [blame] | 476 | struct hmm_range *range = hmm_vma_walk->range; |
Jérôme Glisse | 2aee09d | 2018-04-10 16:29:02 -0700 | [diff] [blame] | 477 | unsigned long pfn, npages, i; |
Jérôme Glisse | 2aee09d | 2018-04-10 16:29:02 -0700 | [diff] [blame] | 478 | bool fault, write_fault; |
Jérôme Glisse | f88a1e9 | 2018-04-10 16:29:06 -0700 | [diff] [blame] | 479 | uint64_t cpu_flags; |
Jérôme Glisse | 53f5c3f | 2018-04-10 16:28:59 -0700 | [diff] [blame] | 480 | |
Jérôme Glisse | 2aee09d | 2018-04-10 16:29:02 -0700 | [diff] [blame] | 481 | npages = (end - addr) >> PAGE_SHIFT; |
Jérôme Glisse | f88a1e9 | 2018-04-10 16:29:06 -0700 | [diff] [blame] | 482 | cpu_flags = pmd_to_hmm_pfn_flags(range, pmd); |
Jérôme Glisse | 2aee09d | 2018-04-10 16:29:02 -0700 | [diff] [blame] | 483 | hmm_range_need_fault(hmm_vma_walk, pfns, npages, cpu_flags, |
| 484 | &fault, &write_fault); |
Jérôme Glisse | 53f5c3f | 2018-04-10 16:28:59 -0700 | [diff] [blame] | 485 | |
Jérôme Glisse | 2aee09d | 2018-04-10 16:29:02 -0700 | [diff] [blame] | 486 | if (pmd_protnone(pmd) || fault || write_fault) |
| 487 | 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] | 488 | |
Christoph Hellwig | 309f9a4 | 2019-08-06 19:05:47 +0300 | [diff] [blame^] | 489 | pfn = pmd_pfn(pmd) + ((addr & ~PMD_MASK) >> PAGE_SHIFT); |
Jérôme Glisse | 992de9a | 2019-05-13 17:20:21 -0700 | [diff] [blame] | 490 | for (i = 0; addr < end; addr += PAGE_SIZE, i++, pfn++) { |
| 491 | if (pmd_devmap(pmd)) { |
| 492 | hmm_vma_walk->pgmap = get_dev_pagemap(pfn, |
| 493 | hmm_vma_walk->pgmap); |
| 494 | if (unlikely(!hmm_vma_walk->pgmap)) |
| 495 | return -EBUSY; |
| 496 | } |
Jérôme Glisse | 391aab1 | 2019-05-13 17:20:31 -0700 | [diff] [blame] | 497 | 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] | 498 | } |
| 499 | if (hmm_vma_walk->pgmap) { |
| 500 | put_dev_pagemap(hmm_vma_walk->pgmap); |
| 501 | hmm_vma_walk->pgmap = NULL; |
| 502 | } |
Jérôme Glisse | 53f5c3f | 2018-04-10 16:28:59 -0700 | [diff] [blame] | 503 | hmm_vma_walk->last = end; |
| 504 | return 0; |
Jérôme Glisse | 992de9a | 2019-05-13 17:20:21 -0700 | [diff] [blame] | 505 | #else |
Ralph Campbell | d2e8d55 | 2019-07-25 17:56:45 -0700 | [diff] [blame] | 506 | /* 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] | 507 | return -EINVAL; |
| 508 | #endif |
Jérôme Glisse | 53f5c3f | 2018-04-10 16:28:59 -0700 | [diff] [blame] | 509 | } |
| 510 | |
Jérôme Glisse | f88a1e9 | 2018-04-10 16:29:06 -0700 | [diff] [blame] | 511 | 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] | 512 | { |
Philip Yang | 789c2af | 2019-05-23 16:32:31 -0400 | [diff] [blame] | 513 | if (pte_none(pte) || !pte_present(pte) || pte_protnone(pte)) |
Jérôme Glisse | 2aee09d | 2018-04-10 16:29:02 -0700 | [diff] [blame] | 514 | return 0; |
Jérôme Glisse | f88a1e9 | 2018-04-10 16:29:06 -0700 | [diff] [blame] | 515 | return pte_write(pte) ? range->flags[HMM_PFN_VALID] | |
| 516 | range->flags[HMM_PFN_WRITE] : |
| 517 | range->flags[HMM_PFN_VALID]; |
Jérôme Glisse | 2aee09d | 2018-04-10 16:29:02 -0700 | [diff] [blame] | 518 | } |
| 519 | |
Jérôme Glisse | 53f5c3f | 2018-04-10 16:28:59 -0700 | [diff] [blame] | 520 | static int hmm_vma_handle_pte(struct mm_walk *walk, unsigned long addr, |
| 521 | unsigned long end, pmd_t *pmdp, pte_t *ptep, |
| 522 | uint64_t *pfn) |
| 523 | { |
| 524 | struct hmm_vma_walk *hmm_vma_walk = walk->private; |
Jérôme Glisse | f88a1e9 | 2018-04-10 16:29:06 -0700 | [diff] [blame] | 525 | struct hmm_range *range = hmm_vma_walk->range; |
Jérôme Glisse | 2aee09d | 2018-04-10 16:29:02 -0700 | [diff] [blame] | 526 | bool fault, write_fault; |
| 527 | uint64_t cpu_flags; |
Jérôme Glisse | 53f5c3f | 2018-04-10 16:28:59 -0700 | [diff] [blame] | 528 | pte_t pte = *ptep; |
Jérôme Glisse | f88a1e9 | 2018-04-10 16:29:06 -0700 | [diff] [blame] | 529 | uint64_t orig_pfn = *pfn; |
Jérôme Glisse | 53f5c3f | 2018-04-10 16:28:59 -0700 | [diff] [blame] | 530 | |
Jérôme Glisse | f88a1e9 | 2018-04-10 16:29:06 -0700 | [diff] [blame] | 531 | *pfn = range->values[HMM_PFN_NONE]; |
Jérôme Glisse | 7323161 | 2019-05-13 17:19:58 -0700 | [diff] [blame] | 532 | fault = write_fault = false; |
Jérôme Glisse | 53f5c3f | 2018-04-10 16:28:59 -0700 | [diff] [blame] | 533 | |
| 534 | if (pte_none(pte)) { |
Jérôme Glisse | 7323161 | 2019-05-13 17:19:58 -0700 | [diff] [blame] | 535 | hmm_pte_need_fault(hmm_vma_walk, orig_pfn, 0, |
| 536 | &fault, &write_fault); |
Jérôme Glisse | 2aee09d | 2018-04-10 16:29:02 -0700 | [diff] [blame] | 537 | if (fault || write_fault) |
Jérôme Glisse | 53f5c3f | 2018-04-10 16:28:59 -0700 | [diff] [blame] | 538 | goto fault; |
| 539 | return 0; |
| 540 | } |
| 541 | |
| 542 | if (!pte_present(pte)) { |
| 543 | swp_entry_t entry = pte_to_swp_entry(pte); |
| 544 | |
| 545 | if (!non_swap_entry(entry)) { |
Jérôme Glisse | 2aee09d | 2018-04-10 16:29:02 -0700 | [diff] [blame] | 546 | if (fault || write_fault) |
Jérôme Glisse | 53f5c3f | 2018-04-10 16:28:59 -0700 | [diff] [blame] | 547 | goto fault; |
| 548 | return 0; |
| 549 | } |
| 550 | |
| 551 | /* |
| 552 | * This is a special swap entry, ignore migration, use |
| 553 | * device and report anything else as error. |
| 554 | */ |
| 555 | if (is_device_private_entry(entry)) { |
Jérôme Glisse | f88a1e9 | 2018-04-10 16:29:06 -0700 | [diff] [blame] | 556 | cpu_flags = range->flags[HMM_PFN_VALID] | |
| 557 | range->flags[HMM_PFN_DEVICE_PRIVATE]; |
Jérôme Glisse | 2aee09d | 2018-04-10 16:29:02 -0700 | [diff] [blame] | 558 | cpu_flags |= is_write_device_private_entry(entry) ? |
Jérôme Glisse | f88a1e9 | 2018-04-10 16:29:06 -0700 | [diff] [blame] | 559 | range->flags[HMM_PFN_WRITE] : 0; |
| 560 | hmm_pte_need_fault(hmm_vma_walk, orig_pfn, cpu_flags, |
| 561 | &fault, &write_fault); |
| 562 | if (fault || write_fault) |
| 563 | goto fault; |
Jérôme Glisse | 391aab1 | 2019-05-13 17:20:31 -0700 | [diff] [blame] | 564 | *pfn = hmm_device_entry_from_pfn(range, |
| 565 | swp_offset(entry)); |
Jérôme Glisse | f88a1e9 | 2018-04-10 16:29:06 -0700 | [diff] [blame] | 566 | *pfn |= cpu_flags; |
Jérôme Glisse | 53f5c3f | 2018-04-10 16:28:59 -0700 | [diff] [blame] | 567 | return 0; |
| 568 | } |
| 569 | |
| 570 | if (is_migration_entry(entry)) { |
Jérôme Glisse | 2aee09d | 2018-04-10 16:29:02 -0700 | [diff] [blame] | 571 | if (fault || write_fault) { |
Jérôme Glisse | 53f5c3f | 2018-04-10 16:28:59 -0700 | [diff] [blame] | 572 | pte_unmap(ptep); |
| 573 | hmm_vma_walk->last = addr; |
Ralph Campbell | d2e8d55 | 2019-07-25 17:56:45 -0700 | [diff] [blame] | 574 | migration_entry_wait(walk->mm, pmdp, addr); |
Jérôme Glisse | 7323161 | 2019-05-13 17:19:58 -0700 | [diff] [blame] | 575 | return -EBUSY; |
Jérôme Glisse | 53f5c3f | 2018-04-10 16:28:59 -0700 | [diff] [blame] | 576 | } |
| 577 | return 0; |
| 578 | } |
| 579 | |
| 580 | /* Report error for everything else */ |
Jérôme Glisse | f88a1e9 | 2018-04-10 16:29:06 -0700 | [diff] [blame] | 581 | *pfn = range->values[HMM_PFN_ERROR]; |
Jérôme Glisse | 53f5c3f | 2018-04-10 16:28:59 -0700 | [diff] [blame] | 582 | return -EFAULT; |
Jérôme Glisse | 7323161 | 2019-05-13 17:19:58 -0700 | [diff] [blame] | 583 | } else { |
| 584 | cpu_flags = pte_to_hmm_pfn_flags(range, pte); |
| 585 | hmm_pte_need_fault(hmm_vma_walk, orig_pfn, cpu_flags, |
| 586 | &fault, &write_fault); |
Jérôme Glisse | 53f5c3f | 2018-04-10 16:28:59 -0700 | [diff] [blame] | 587 | } |
| 588 | |
Jérôme Glisse | 2aee09d | 2018-04-10 16:29:02 -0700 | [diff] [blame] | 589 | if (fault || write_fault) |
Jérôme Glisse | 53f5c3f | 2018-04-10 16:28:59 -0700 | [diff] [blame] | 590 | goto fault; |
| 591 | |
Jérôme Glisse | 992de9a | 2019-05-13 17:20:21 -0700 | [diff] [blame] | 592 | if (pte_devmap(pte)) { |
| 593 | hmm_vma_walk->pgmap = get_dev_pagemap(pte_pfn(pte), |
| 594 | hmm_vma_walk->pgmap); |
| 595 | if (unlikely(!hmm_vma_walk->pgmap)) |
| 596 | return -EBUSY; |
| 597 | } else if (IS_ENABLED(CONFIG_ARCH_HAS_PTE_SPECIAL) && pte_special(pte)) { |
| 598 | *pfn = range->values[HMM_PFN_SPECIAL]; |
| 599 | return -EFAULT; |
| 600 | } |
| 601 | |
Jérôme Glisse | 391aab1 | 2019-05-13 17:20:31 -0700 | [diff] [blame] | 602 | *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] | 603 | return 0; |
| 604 | |
| 605 | fault: |
Jérôme Glisse | 992de9a | 2019-05-13 17:20:21 -0700 | [diff] [blame] | 606 | if (hmm_vma_walk->pgmap) { |
| 607 | put_dev_pagemap(hmm_vma_walk->pgmap); |
| 608 | hmm_vma_walk->pgmap = NULL; |
| 609 | } |
Jérôme Glisse | 53f5c3f | 2018-04-10 16:28:59 -0700 | [diff] [blame] | 610 | pte_unmap(ptep); |
| 611 | /* Fault any virtual address we were asked to fault */ |
Jérôme Glisse | 2aee09d | 2018-04-10 16:29:02 -0700 | [diff] [blame] | 612 | 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] | 613 | } |
| 614 | |
Jérôme Glisse | da4c3c7 | 2017-09-08 16:11:31 -0700 | [diff] [blame] | 615 | static int hmm_vma_walk_pmd(pmd_t *pmdp, |
| 616 | unsigned long start, |
| 617 | unsigned long end, |
| 618 | struct mm_walk *walk) |
| 619 | { |
Jérôme Glisse | 74eee18 | 2017-09-08 16:11:35 -0700 | [diff] [blame] | 620 | struct hmm_vma_walk *hmm_vma_walk = walk->private; |
| 621 | struct hmm_range *range = hmm_vma_walk->range; |
Jérôme Glisse | ff05c0c | 2018-04-10 16:28:38 -0700 | [diff] [blame] | 622 | uint64_t *pfns = range->pfns; |
Jérôme Glisse | da4c3c7 | 2017-09-08 16:11:31 -0700 | [diff] [blame] | 623 | unsigned long addr = start, i; |
Jérôme Glisse | da4c3c7 | 2017-09-08 16:11:31 -0700 | [diff] [blame] | 624 | pte_t *ptep; |
Jérôme Glisse | d08faca | 2018-10-30 15:04:20 -0700 | [diff] [blame] | 625 | pmd_t pmd; |
Jérôme Glisse | da4c3c7 | 2017-09-08 16:11:31 -0700 | [diff] [blame] | 626 | |
Jérôme Glisse | da4c3c7 | 2017-09-08 16:11:31 -0700 | [diff] [blame] | 627 | again: |
Jérôme Glisse | d08faca | 2018-10-30 15:04:20 -0700 | [diff] [blame] | 628 | pmd = READ_ONCE(*pmdp); |
| 629 | if (pmd_none(pmd)) |
Jérôme Glisse | da4c3c7 | 2017-09-08 16:11:31 -0700 | [diff] [blame] | 630 | return hmm_vma_walk_hole(start, end, walk); |
| 631 | |
Jérôme Glisse | d08faca | 2018-10-30 15:04:20 -0700 | [diff] [blame] | 632 | if (thp_migration_supported() && is_pmd_migration_entry(pmd)) { |
| 633 | bool fault, write_fault; |
| 634 | unsigned long npages; |
| 635 | uint64_t *pfns; |
Jérôme Glisse | da4c3c7 | 2017-09-08 16:11:31 -0700 | [diff] [blame] | 636 | |
Jérôme Glisse | d08faca | 2018-10-30 15:04:20 -0700 | [diff] [blame] | 637 | i = (addr - range->start) >> PAGE_SHIFT; |
| 638 | npages = (end - addr) >> PAGE_SHIFT; |
| 639 | pfns = &range->pfns[i]; |
| 640 | |
| 641 | hmm_range_need_fault(hmm_vma_walk, pfns, npages, |
| 642 | 0, &fault, &write_fault); |
| 643 | if (fault || write_fault) { |
| 644 | hmm_vma_walk->last = addr; |
Ralph Campbell | d2e8d55 | 2019-07-25 17:56:45 -0700 | [diff] [blame] | 645 | pmd_migration_entry_wait(walk->mm, pmdp); |
Jérôme Glisse | 7323161 | 2019-05-13 17:19:58 -0700 | [diff] [blame] | 646 | return -EBUSY; |
Jérôme Glisse | d08faca | 2018-10-30 15:04:20 -0700 | [diff] [blame] | 647 | } |
| 648 | return 0; |
| 649 | } else if (!pmd_present(pmd)) |
| 650 | return hmm_pfns_bad(start, end, walk); |
| 651 | |
| 652 | if (pmd_devmap(pmd) || pmd_trans_huge(pmd)) { |
Jérôme Glisse | da4c3c7 | 2017-09-08 16:11:31 -0700 | [diff] [blame] | 653 | /* |
Ralph Campbell | d2e8d55 | 2019-07-25 17:56:45 -0700 | [diff] [blame] | 654 | * 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] | 655 | * is splitting the huge pmd we will get that event through |
| 656 | * mmu_notifier callback. |
| 657 | * |
Ralph Campbell | d2e8d55 | 2019-07-25 17:56:45 -0700 | [diff] [blame] | 658 | * 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] | 659 | * huge or device mapping one and compute corresponding pfn |
| 660 | * values. |
| 661 | */ |
| 662 | pmd = pmd_read_atomic(pmdp); |
| 663 | barrier(); |
| 664 | if (!pmd_devmap(pmd) && !pmd_trans_huge(pmd)) |
| 665 | goto again; |
Jérôme Glisse | da4c3c7 | 2017-09-08 16:11:31 -0700 | [diff] [blame] | 666 | |
Jérôme Glisse | d08faca | 2018-10-30 15:04:20 -0700 | [diff] [blame] | 667 | i = (addr - range->start) >> PAGE_SHIFT; |
Jérôme Glisse | 53f5c3f | 2018-04-10 16:28:59 -0700 | [diff] [blame] | 668 | 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] | 669 | } |
| 670 | |
Jérôme Glisse | d08faca | 2018-10-30 15:04:20 -0700 | [diff] [blame] | 671 | /* |
Ralph Campbell | d2e8d55 | 2019-07-25 17:56:45 -0700 | [diff] [blame] | 672 | * 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] | 673 | * huge or transparent huge. At this point either it is a valid pmd |
| 674 | * entry pointing to pte directory or it is a bad pmd that will not |
| 675 | * recover. |
| 676 | */ |
| 677 | if (pmd_bad(pmd)) |
Jérôme Glisse | da4c3c7 | 2017-09-08 16:11:31 -0700 | [diff] [blame] | 678 | return hmm_pfns_bad(start, end, walk); |
| 679 | |
| 680 | ptep = pte_offset_map(pmdp, addr); |
Jérôme Glisse | d08faca | 2018-10-30 15:04:20 -0700 | [diff] [blame] | 681 | i = (addr - range->start) >> PAGE_SHIFT; |
Jérôme Glisse | da4c3c7 | 2017-09-08 16:11:31 -0700 | [diff] [blame] | 682 | for (; addr < end; addr += PAGE_SIZE, ptep++, i++) { |
Jérôme Glisse | 53f5c3f | 2018-04-10 16:28:59 -0700 | [diff] [blame] | 683 | int r; |
Jérôme Glisse | da4c3c7 | 2017-09-08 16:11:31 -0700 | [diff] [blame] | 684 | |
Jérôme Glisse | 53f5c3f | 2018-04-10 16:28:59 -0700 | [diff] [blame] | 685 | r = hmm_vma_handle_pte(walk, addr, end, pmdp, ptep, &pfns[i]); |
| 686 | if (r) { |
| 687 | /* hmm_vma_handle_pte() did unmap pte directory */ |
| 688 | hmm_vma_walk->last = addr; |
| 689 | return r; |
Jérôme Glisse | da4c3c7 | 2017-09-08 16:11:31 -0700 | [diff] [blame] | 690 | } |
Jérôme Glisse | da4c3c7 | 2017-09-08 16:11:31 -0700 | [diff] [blame] | 691 | } |
Jérôme Glisse | 992de9a | 2019-05-13 17:20:21 -0700 | [diff] [blame] | 692 | if (hmm_vma_walk->pgmap) { |
| 693 | /* |
| 694 | * We do put_dev_pagemap() here and not in hmm_vma_handle_pte() |
| 695 | * so that we can leverage get_dev_pagemap() optimization which |
| 696 | * will not re-take a reference on a pgmap if we already have |
| 697 | * one. |
| 698 | */ |
| 699 | put_dev_pagemap(hmm_vma_walk->pgmap); |
| 700 | hmm_vma_walk->pgmap = NULL; |
| 701 | } |
Jérôme Glisse | da4c3c7 | 2017-09-08 16:11:31 -0700 | [diff] [blame] | 702 | pte_unmap(ptep - 1); |
| 703 | |
Jérôme Glisse | 53f5c3f | 2018-04-10 16:28:59 -0700 | [diff] [blame] | 704 | hmm_vma_walk->last = addr; |
Jérôme Glisse | da4c3c7 | 2017-09-08 16:11:31 -0700 | [diff] [blame] | 705 | return 0; |
| 706 | } |
| 707 | |
Jérôme Glisse | 992de9a | 2019-05-13 17:20:21 -0700 | [diff] [blame] | 708 | static int hmm_vma_walk_pud(pud_t *pudp, |
| 709 | unsigned long start, |
| 710 | unsigned long end, |
| 711 | struct mm_walk *walk) |
| 712 | { |
| 713 | struct hmm_vma_walk *hmm_vma_walk = walk->private; |
| 714 | struct hmm_range *range = hmm_vma_walk->range; |
| 715 | unsigned long addr = start, next; |
| 716 | pmd_t *pmdp; |
| 717 | pud_t pud; |
| 718 | int ret; |
| 719 | |
| 720 | again: |
| 721 | pud = READ_ONCE(*pudp); |
| 722 | if (pud_none(pud)) |
| 723 | return hmm_vma_walk_hole(start, end, walk); |
| 724 | |
| 725 | if (pud_huge(pud) && pud_devmap(pud)) { |
| 726 | unsigned long i, npages, pfn; |
| 727 | uint64_t *pfns, cpu_flags; |
| 728 | bool fault, write_fault; |
| 729 | |
| 730 | if (!pud_present(pud)) |
| 731 | return hmm_vma_walk_hole(start, end, walk); |
| 732 | |
| 733 | i = (addr - range->start) >> PAGE_SHIFT; |
| 734 | npages = (end - addr) >> PAGE_SHIFT; |
| 735 | pfns = &range->pfns[i]; |
| 736 | |
| 737 | cpu_flags = pud_to_hmm_pfn_flags(range, pud); |
| 738 | hmm_range_need_fault(hmm_vma_walk, pfns, npages, |
| 739 | cpu_flags, &fault, &write_fault); |
| 740 | if (fault || write_fault) |
| 741 | return hmm_vma_walk_hole_(addr, end, fault, |
| 742 | write_fault, walk); |
| 743 | |
Jérôme Glisse | 992de9a | 2019-05-13 17:20:21 -0700 | [diff] [blame] | 744 | pfn = pud_pfn(pud) + ((addr & ~PUD_MASK) >> PAGE_SHIFT); |
| 745 | for (i = 0; i < npages; ++i, ++pfn) { |
| 746 | hmm_vma_walk->pgmap = get_dev_pagemap(pfn, |
| 747 | hmm_vma_walk->pgmap); |
| 748 | if (unlikely(!hmm_vma_walk->pgmap)) |
| 749 | return -EBUSY; |
Jérôme Glisse | 391aab1 | 2019-05-13 17:20:31 -0700 | [diff] [blame] | 750 | pfns[i] = hmm_device_entry_from_pfn(range, pfn) | |
| 751 | cpu_flags; |
Jérôme Glisse | 992de9a | 2019-05-13 17:20:21 -0700 | [diff] [blame] | 752 | } |
| 753 | if (hmm_vma_walk->pgmap) { |
| 754 | put_dev_pagemap(hmm_vma_walk->pgmap); |
| 755 | hmm_vma_walk->pgmap = NULL; |
| 756 | } |
| 757 | hmm_vma_walk->last = end; |
| 758 | return 0; |
Jérôme Glisse | 992de9a | 2019-05-13 17:20:21 -0700 | [diff] [blame] | 759 | } |
| 760 | |
| 761 | split_huge_pud(walk->vma, pudp, addr); |
| 762 | if (pud_none(*pudp)) |
| 763 | goto again; |
| 764 | |
| 765 | pmdp = pmd_offset(pudp, addr); |
| 766 | do { |
| 767 | next = pmd_addr_end(addr, end); |
| 768 | ret = hmm_vma_walk_pmd(pmdp, addr, next, walk); |
| 769 | if (ret) |
| 770 | return ret; |
| 771 | } while (pmdp++, addr = next, addr != end); |
| 772 | |
| 773 | return 0; |
| 774 | } |
| 775 | |
Jérôme Glisse | 63d5066 | 2019-05-13 17:20:18 -0700 | [diff] [blame] | 776 | static int hmm_vma_walk_hugetlb_entry(pte_t *pte, unsigned long hmask, |
| 777 | unsigned long start, unsigned long end, |
| 778 | struct mm_walk *walk) |
| 779 | { |
| 780 | #ifdef CONFIG_HUGETLB_PAGE |
Christoph Hellwig | 05c23af | 2019-08-06 19:05:46 +0300 | [diff] [blame] | 781 | unsigned long addr = start, i, pfn; |
Jérôme Glisse | 63d5066 | 2019-05-13 17:20:18 -0700 | [diff] [blame] | 782 | struct hmm_vma_walk *hmm_vma_walk = walk->private; |
| 783 | struct hmm_range *range = hmm_vma_walk->range; |
| 784 | struct vm_area_struct *vma = walk->vma; |
Jérôme Glisse | 63d5066 | 2019-05-13 17:20:18 -0700 | [diff] [blame] | 785 | uint64_t orig_pfn, cpu_flags; |
| 786 | bool fault, write_fault; |
| 787 | spinlock_t *ptl; |
| 788 | pte_t entry; |
| 789 | int ret = 0; |
| 790 | |
Ralph Campbell | d2e8d55 | 2019-07-25 17:56:45 -0700 | [diff] [blame] | 791 | ptl = huge_pte_lock(hstate_vma(vma), walk->mm, pte); |
Jérôme Glisse | 63d5066 | 2019-05-13 17:20:18 -0700 | [diff] [blame] | 792 | entry = huge_ptep_get(pte); |
| 793 | |
Christoph Hellwig | 7f08263 | 2019-08-06 19:05:45 +0300 | [diff] [blame] | 794 | i = (start - range->start) >> PAGE_SHIFT; |
Jérôme Glisse | 63d5066 | 2019-05-13 17:20:18 -0700 | [diff] [blame] | 795 | orig_pfn = range->pfns[i]; |
| 796 | range->pfns[i] = range->values[HMM_PFN_NONE]; |
| 797 | cpu_flags = pte_to_hmm_pfn_flags(range, entry); |
| 798 | fault = write_fault = false; |
| 799 | hmm_pte_need_fault(hmm_vma_walk, orig_pfn, cpu_flags, |
| 800 | &fault, &write_fault); |
| 801 | if (fault || write_fault) { |
| 802 | ret = -ENOENT; |
| 803 | goto unlock; |
| 804 | } |
| 805 | |
Christoph Hellwig | 05c23af | 2019-08-06 19:05:46 +0300 | [diff] [blame] | 806 | pfn = pte_pfn(entry) + ((start & ~hmask) >> PAGE_SHIFT); |
Christoph Hellwig | 7f08263 | 2019-08-06 19:05:45 +0300 | [diff] [blame] | 807 | for (; addr < end; addr += PAGE_SIZE, i++, pfn++) |
Jérôme Glisse | 391aab1 | 2019-05-13 17:20:31 -0700 | [diff] [blame] | 808 | range->pfns[i] = hmm_device_entry_from_pfn(range, pfn) | |
| 809 | cpu_flags; |
Jérôme Glisse | 63d5066 | 2019-05-13 17:20:18 -0700 | [diff] [blame] | 810 | hmm_vma_walk->last = end; |
| 811 | |
| 812 | unlock: |
| 813 | spin_unlock(ptl); |
| 814 | |
| 815 | if (ret == -ENOENT) |
| 816 | return hmm_vma_walk_hole_(addr, end, fault, write_fault, walk); |
| 817 | |
| 818 | return ret; |
| 819 | #else /* CONFIG_HUGETLB_PAGE */ |
| 820 | return -EINVAL; |
| 821 | #endif |
| 822 | } |
| 823 | |
Jérôme Glisse | f88a1e9 | 2018-04-10 16:29:06 -0700 | [diff] [blame] | 824 | static void hmm_pfns_clear(struct hmm_range *range, |
| 825 | uint64_t *pfns, |
Jérôme Glisse | 33cd47d | 2018-04-10 16:28:54 -0700 | [diff] [blame] | 826 | unsigned long addr, |
| 827 | unsigned long end) |
| 828 | { |
| 829 | for (; addr < end; addr += PAGE_SIZE, pfns++) |
Jérôme Glisse | f88a1e9 | 2018-04-10 16:29:06 -0700 | [diff] [blame] | 830 | *pfns = range->values[HMM_PFN_NONE]; |
Jérôme Glisse | 33cd47d | 2018-04-10 16:28:54 -0700 | [diff] [blame] | 831 | } |
| 832 | |
Jérôme Glisse | da4c3c7 | 2017-09-08 16:11:31 -0700 | [diff] [blame] | 833 | /* |
Jérôme Glisse | a3e0d41 | 2019-05-13 17:20:01 -0700 | [diff] [blame] | 834 | * 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] | 835 | * @range: range |
Jérôme Glisse | a3e0d41 | 2019-05-13 17:20:01 -0700 | [diff] [blame] | 836 | * @mm: the mm struct for the range of virtual address |
Christoph Hellwig | fac555a | 2019-08-06 19:05:44 +0300 | [diff] [blame] | 837 | * |
Ralph Campbell | d2e8d55 | 2019-07-25 17:56:45 -0700 | [diff] [blame] | 838 | * 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] | 839 | * |
Jérôme Glisse | a3e0d41 | 2019-05-13 17:20:01 -0700 | [diff] [blame] | 840 | * 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] | 841 | */ |
Christoph Hellwig | fac555a | 2019-08-06 19:05:44 +0300 | [diff] [blame] | 842 | int hmm_range_register(struct hmm_range *range, struct hmm_mirror *mirror) |
Jérôme Glisse | da4c3c7 | 2017-09-08 16:11:31 -0700 | [diff] [blame] | 843 | { |
Jason Gunthorpe | e36acfe | 2019-05-23 09:41:19 -0300 | [diff] [blame] | 844 | struct hmm *hmm = mirror->hmm; |
Jason Gunthorpe | 5a136b4 | 2019-06-07 12:10:33 -0300 | [diff] [blame] | 845 | unsigned long flags; |
Jérôme Glisse | 63d5066 | 2019-05-13 17:20:18 -0700 | [diff] [blame] | 846 | |
Jérôme Glisse | a3e0d41 | 2019-05-13 17:20:01 -0700 | [diff] [blame] | 847 | range->valid = false; |
Jérôme Glisse | 704f3f2 | 2019-05-13 17:19:48 -0700 | [diff] [blame] | 848 | range->hmm = NULL; |
| 849 | |
Christoph Hellwig | 7f08263 | 2019-08-06 19:05:45 +0300 | [diff] [blame] | 850 | if ((range->start & (PAGE_SIZE - 1)) || (range->end & (PAGE_SIZE - 1))) |
Jérôme Glisse | 63d5066 | 2019-05-13 17:20:18 -0700 | [diff] [blame] | 851 | return -EINVAL; |
Christoph Hellwig | fac555a | 2019-08-06 19:05:44 +0300 | [diff] [blame] | 852 | if (range->start >= range->end) |
Jérôme Glisse | da4c3c7 | 2017-09-08 16:11:31 -0700 | [diff] [blame] | 853 | return -EINVAL; |
| 854 | |
Jason Gunthorpe | 47f2459 | 2019-05-23 11:08:28 -0300 | [diff] [blame] | 855 | /* Prevent hmm_release() from running while the range is valid */ |
| 856 | if (!mmget_not_zero(hmm->mm)) |
Jérôme Glisse | a3e0d41 | 2019-05-13 17:20:01 -0700 | [diff] [blame] | 857 | return -EFAULT; |
Jérôme Glisse | 704f3f2 | 2019-05-13 17:19:48 -0700 | [diff] [blame] | 858 | |
Ralph Campbell | 085ea25 | 2019-05-06 16:29:39 -0700 | [diff] [blame] | 859 | /* Initialize range to track CPU page table updates. */ |
Jason Gunthorpe | 5a136b4 | 2019-06-07 12:10:33 -0300 | [diff] [blame] | 860 | spin_lock_irqsave(&hmm->ranges_lock, flags); |
Jérôme Glisse | 86586a4 | 2018-04-10 16:28:34 -0700 | [diff] [blame] | 861 | |
Ralph Campbell | 085ea25 | 2019-05-06 16:29:39 -0700 | [diff] [blame] | 862 | range->hmm = hmm; |
Jason Gunthorpe | e36acfe | 2019-05-23 09:41:19 -0300 | [diff] [blame] | 863 | kref_get(&hmm->kref); |
Jason Gunthorpe | 157816f | 2019-05-23 11:43:43 -0300 | [diff] [blame] | 864 | list_add(&range->list, &hmm->ranges); |
Jérôme Glisse | 74eee18 | 2017-09-08 16:11:35 -0700 | [diff] [blame] | 865 | |
Jérôme Glisse | 704f3f2 | 2019-05-13 17:19:48 -0700 | [diff] [blame] | 866 | /* |
Jérôme Glisse | a3e0d41 | 2019-05-13 17:20:01 -0700 | [diff] [blame] | 867 | * If there are any concurrent notifiers we have to wait for them for |
| 868 | * 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] | 869 | */ |
Ralph Campbell | 085ea25 | 2019-05-06 16:29:39 -0700 | [diff] [blame] | 870 | if (!hmm->notifiers) |
Jérôme Glisse | a3e0d41 | 2019-05-13 17:20:01 -0700 | [diff] [blame] | 871 | range->valid = true; |
Jason Gunthorpe | 5a136b4 | 2019-06-07 12:10:33 -0300 | [diff] [blame] | 872 | spin_unlock_irqrestore(&hmm->ranges_lock, flags); |
Jérôme Glisse | a3e0d41 | 2019-05-13 17:20:01 -0700 | [diff] [blame] | 873 | |
| 874 | return 0; |
| 875 | } |
| 876 | EXPORT_SYMBOL(hmm_range_register); |
| 877 | |
| 878 | /* |
| 879 | * hmm_range_unregister() - stop tracking change to CPU page table over a range |
| 880 | * @range: range |
| 881 | * |
| 882 | * Range struct is used to track updates to the CPU page table after a call to |
| 883 | * hmm_range_register(). See include/linux/hmm.h for how to use it. |
| 884 | */ |
| 885 | void hmm_range_unregister(struct hmm_range *range) |
| 886 | { |
Ralph Campbell | 085ea25 | 2019-05-06 16:29:39 -0700 | [diff] [blame] | 887 | struct hmm *hmm = range->hmm; |
Jason Gunthorpe | 5a136b4 | 2019-06-07 12:10:33 -0300 | [diff] [blame] | 888 | unsigned long flags; |
Jérôme Glisse | a3e0d41 | 2019-05-13 17:20:01 -0700 | [diff] [blame] | 889 | |
Jason Gunthorpe | 5a136b4 | 2019-06-07 12:10:33 -0300 | [diff] [blame] | 890 | spin_lock_irqsave(&hmm->ranges_lock, flags); |
Jason Gunthorpe | 47f2459 | 2019-05-23 11:08:28 -0300 | [diff] [blame] | 891 | list_del_init(&range->list); |
Jason Gunthorpe | 5a136b4 | 2019-06-07 12:10:33 -0300 | [diff] [blame] | 892 | spin_unlock_irqrestore(&hmm->ranges_lock, flags); |
Jérôme Glisse | a3e0d41 | 2019-05-13 17:20:01 -0700 | [diff] [blame] | 893 | |
| 894 | /* Drop reference taken by hmm_range_register() */ |
Jason Gunthorpe | 47f2459 | 2019-05-23 11:08:28 -0300 | [diff] [blame] | 895 | mmput(hmm->mm); |
Ralph Campbell | 085ea25 | 2019-05-06 16:29:39 -0700 | [diff] [blame] | 896 | hmm_put(hmm); |
Jason Gunthorpe | 2dcc3eb | 2019-05-23 11:40:24 -0300 | [diff] [blame] | 897 | |
| 898 | /* |
| 899 | * The range is now invalid and the ref on the hmm is dropped, so |
| 900 | * poison the pointer. Leave other fields in place, for the caller's |
| 901 | * use. |
| 902 | */ |
Jérôme Glisse | a3e0d41 | 2019-05-13 17:20:01 -0700 | [diff] [blame] | 903 | range->valid = false; |
Jason Gunthorpe | 2dcc3eb | 2019-05-23 11:40:24 -0300 | [diff] [blame] | 904 | memset(&range->hmm, POISON_INUSE, sizeof(range->hmm)); |
Jérôme Glisse | a3e0d41 | 2019-05-13 17:20:01 -0700 | [diff] [blame] | 905 | } |
| 906 | EXPORT_SYMBOL(hmm_range_unregister); |
| 907 | |
Christoph Hellwig | 9a4903e | 2019-07-25 17:56:46 -0700 | [diff] [blame] | 908 | /** |
| 909 | * hmm_range_fault - try to fault some address in a virtual address range |
| 910 | * @range: range being faulted |
| 911 | * @flags: HMM_FAULT_* flags |
Jérôme Glisse | 7323161 | 2019-05-13 17:19:58 -0700 | [diff] [blame] | 912 | * |
Christoph Hellwig | 9a4903e | 2019-07-25 17:56:46 -0700 | [diff] [blame] | 913 | * Return: the number of valid pages in range->pfns[] (from range start |
| 914 | * address), which may be zero. On error one of the following status codes |
| 915 | * can be returned: |
| 916 | * |
| 917 | * -EINVAL: Invalid arguments or mm or virtual address is in an invalid vma |
| 918 | * (e.g., device file vma). |
| 919 | * -ENOMEM: Out of memory. |
| 920 | * -EPERM: Invalid permission (e.g., asking for write and range is read |
| 921 | * only). |
| 922 | * -EAGAIN: A page fault needs to be retried and mmap_sem was dropped. |
| 923 | * -EBUSY: The range has been invalidated and the caller needs to wait for |
| 924 | * the invalidation to finish. |
| 925 | * -EFAULT: Invalid (i.e., either no valid vma or it is illegal to access |
| 926 | * that range) number of valid pages in range->pfns[] (from |
| 927 | * range start address). |
Jérôme Glisse | 74eee18 | 2017-09-08 16:11:35 -0700 | [diff] [blame] | 928 | * |
| 929 | * 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] | 930 | * any memory migration if the memory being faulted is not accessible by CPUs |
| 931 | * and caller does not ask for migration. |
Jérôme Glisse | 74eee18 | 2017-09-08 16:11:35 -0700 | [diff] [blame] | 932 | * |
Jérôme Glisse | ff05c0c | 2018-04-10 16:28:38 -0700 | [diff] [blame] | 933 | * On error, for one virtual address in the range, the function will mark the |
| 934 | * corresponding HMM pfn entry with an error flag. |
Jérôme Glisse | 74eee18 | 2017-09-08 16:11:35 -0700 | [diff] [blame] | 935 | */ |
Christoph Hellwig | 9a4903e | 2019-07-25 17:56:46 -0700 | [diff] [blame] | 936 | 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] | 937 | { |
Jérôme Glisse | 63d5066 | 2019-05-13 17:20:18 -0700 | [diff] [blame] | 938 | 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] | 939 | unsigned long start = range->start, end; |
Jérôme Glisse | 74eee18 | 2017-09-08 16:11:35 -0700 | [diff] [blame] | 940 | struct hmm_vma_walk hmm_vma_walk; |
Jérôme Glisse | a3e0d41 | 2019-05-13 17:20:01 -0700 | [diff] [blame] | 941 | struct hmm *hmm = range->hmm; |
| 942 | struct vm_area_struct *vma; |
Jérôme Glisse | 74eee18 | 2017-09-08 16:11:35 -0700 | [diff] [blame] | 943 | struct mm_walk mm_walk; |
Jérôme Glisse | 74eee18 | 2017-09-08 16:11:35 -0700 | [diff] [blame] | 944 | int ret; |
| 945 | |
Jason Gunthorpe | 47f2459 | 2019-05-23 11:08:28 -0300 | [diff] [blame] | 946 | lockdep_assert_held(&hmm->mm->mmap_sem); |
Jérôme Glisse | 74eee18 | 2017-09-08 16:11:35 -0700 | [diff] [blame] | 947 | |
| 948 | do { |
Jérôme Glisse | a3e0d41 | 2019-05-13 17:20:01 -0700 | [diff] [blame] | 949 | /* If range is no longer valid force retry. */ |
Christoph Hellwig | 2bcbeae | 2019-07-24 08:52:52 +0200 | [diff] [blame] | 950 | if (!range->valid) |
| 951 | return -EBUSY; |
Jérôme Glisse | 74eee18 | 2017-09-08 16:11:35 -0700 | [diff] [blame] | 952 | |
Jérôme Glisse | a3e0d41 | 2019-05-13 17:20:01 -0700 | [diff] [blame] | 953 | vma = find_vma(hmm->mm, start); |
Jérôme Glisse | 63d5066 | 2019-05-13 17:20:18 -0700 | [diff] [blame] | 954 | if (vma == NULL || (vma->vm_flags & device_vma)) |
Jérôme Glisse | a3e0d41 | 2019-05-13 17:20:01 -0700 | [diff] [blame] | 955 | return -EFAULT; |
Jérôme Glisse | 74eee18 | 2017-09-08 16:11:35 -0700 | [diff] [blame] | 956 | |
Jérôme Glisse | a3e0d41 | 2019-05-13 17:20:01 -0700 | [diff] [blame] | 957 | if (!(vma->vm_flags & VM_READ)) { |
| 958 | /* |
| 959 | * If vma do not allow read access, then assume that it |
| 960 | * does not allow write access, either. HMM does not |
| 961 | * support architecture that allow write without read. |
| 962 | */ |
| 963 | hmm_pfns_clear(range, range->pfns, |
| 964 | range->start, range->end); |
| 965 | return -EPERM; |
| 966 | } |
| 967 | |
Jérôme Glisse | 992de9a | 2019-05-13 17:20:21 -0700 | [diff] [blame] | 968 | hmm_vma_walk.pgmap = NULL; |
Jérôme Glisse | a3e0d41 | 2019-05-13 17:20:01 -0700 | [diff] [blame] | 969 | hmm_vma_walk.last = start; |
Christoph Hellwig | 9a4903e | 2019-07-25 17:56:46 -0700 | [diff] [blame] | 970 | hmm_vma_walk.flags = flags; |
Jérôme Glisse | a3e0d41 | 2019-05-13 17:20:01 -0700 | [diff] [blame] | 971 | hmm_vma_walk.range = range; |
| 972 | mm_walk.private = &hmm_vma_walk; |
| 973 | end = min(range->end, vma->vm_end); |
| 974 | |
| 975 | mm_walk.vma = vma; |
| 976 | mm_walk.mm = vma->vm_mm; |
| 977 | mm_walk.pte_entry = NULL; |
| 978 | mm_walk.test_walk = NULL; |
| 979 | mm_walk.hugetlb_entry = NULL; |
Jérôme Glisse | 992de9a | 2019-05-13 17:20:21 -0700 | [diff] [blame] | 980 | mm_walk.pud_entry = hmm_vma_walk_pud; |
Jérôme Glisse | a3e0d41 | 2019-05-13 17:20:01 -0700 | [diff] [blame] | 981 | mm_walk.pmd_entry = hmm_vma_walk_pmd; |
| 982 | mm_walk.pte_hole = hmm_vma_walk_hole; |
Jérôme Glisse | 63d5066 | 2019-05-13 17:20:18 -0700 | [diff] [blame] | 983 | mm_walk.hugetlb_entry = hmm_vma_walk_hugetlb_entry; |
Jérôme Glisse | a3e0d41 | 2019-05-13 17:20:01 -0700 | [diff] [blame] | 984 | |
| 985 | do { |
| 986 | ret = walk_page_range(start, end, &mm_walk); |
| 987 | start = hmm_vma_walk.last; |
| 988 | |
| 989 | /* Keep trying while the range is valid. */ |
| 990 | } while (ret == -EBUSY && range->valid); |
| 991 | |
| 992 | if (ret) { |
| 993 | unsigned long i; |
| 994 | |
| 995 | i = (hmm_vma_walk.last - range->start) >> PAGE_SHIFT; |
| 996 | hmm_pfns_clear(range, &range->pfns[i], |
| 997 | hmm_vma_walk.last, range->end); |
| 998 | return ret; |
| 999 | } |
| 1000 | start = end; |
| 1001 | |
| 1002 | } while (start < range->end); |
Jérôme Glisse | 704f3f2 | 2019-05-13 17:19:48 -0700 | [diff] [blame] | 1003 | |
Jérôme Glisse | 7323161 | 2019-05-13 17:19:58 -0700 | [diff] [blame] | 1004 | return (hmm_vma_walk.last - range->start) >> PAGE_SHIFT; |
Jérôme Glisse | 74eee18 | 2017-09-08 16:11:35 -0700 | [diff] [blame] | 1005 | } |
Jérôme Glisse | 7323161 | 2019-05-13 17:19:58 -0700 | [diff] [blame] | 1006 | EXPORT_SYMBOL(hmm_range_fault); |
Jérôme Glisse | 55c0ece | 2019-05-13 17:20:28 -0700 | [diff] [blame] | 1007 | |
| 1008 | /** |
Christoph Hellwig | 9a4903e | 2019-07-25 17:56:46 -0700 | [diff] [blame] | 1009 | * hmm_range_dma_map - hmm_range_fault() and dma map page all in one. |
| 1010 | * @range: range being faulted |
| 1011 | * @device: device to map page to |
| 1012 | * @daddrs: array of dma addresses for the mapped pages |
| 1013 | * @flags: HMM_FAULT_* |
Jérôme Glisse | 55c0ece | 2019-05-13 17:20:28 -0700 | [diff] [blame] | 1014 | * |
Christoph Hellwig | 9a4903e | 2019-07-25 17:56:46 -0700 | [diff] [blame] | 1015 | * Return: the number of pages mapped on success (including zero), or any |
| 1016 | * status return from hmm_range_fault() otherwise. |
Jérôme Glisse | 55c0ece | 2019-05-13 17:20:28 -0700 | [diff] [blame] | 1017 | */ |
Christoph Hellwig | 9a4903e | 2019-07-25 17:56:46 -0700 | [diff] [blame] | 1018 | long hmm_range_dma_map(struct hmm_range *range, struct device *device, |
| 1019 | dma_addr_t *daddrs, unsigned int flags) |
Jérôme Glisse | 55c0ece | 2019-05-13 17:20:28 -0700 | [diff] [blame] | 1020 | { |
| 1021 | unsigned long i, npages, mapped; |
| 1022 | long ret; |
| 1023 | |
Christoph Hellwig | 9a4903e | 2019-07-25 17:56:46 -0700 | [diff] [blame] | 1024 | ret = hmm_range_fault(range, flags); |
Jérôme Glisse | 55c0ece | 2019-05-13 17:20:28 -0700 | [diff] [blame] | 1025 | if (ret <= 0) |
| 1026 | return ret ? ret : -EBUSY; |
| 1027 | |
| 1028 | npages = (range->end - range->start) >> PAGE_SHIFT; |
| 1029 | for (i = 0, mapped = 0; i < npages; ++i) { |
| 1030 | enum dma_data_direction dir = DMA_TO_DEVICE; |
| 1031 | struct page *page; |
| 1032 | |
| 1033 | /* |
| 1034 | * FIXME need to update DMA API to provide invalid DMA address |
| 1035 | * value instead of a function to test dma address value. This |
| 1036 | * would remove lot of dumb code duplicated accross many arch. |
| 1037 | * |
| 1038 | * For now setting it to 0 here is good enough as the pfns[] |
| 1039 | * value is what is use to check what is valid and what isn't. |
| 1040 | */ |
| 1041 | daddrs[i] = 0; |
| 1042 | |
Jérôme Glisse | 391aab1 | 2019-05-13 17:20:31 -0700 | [diff] [blame] | 1043 | page = hmm_device_entry_to_page(range, range->pfns[i]); |
Jérôme Glisse | 55c0ece | 2019-05-13 17:20:28 -0700 | [diff] [blame] | 1044 | if (page == NULL) |
| 1045 | continue; |
| 1046 | |
| 1047 | /* Check if range is being invalidated */ |
| 1048 | if (!range->valid) { |
| 1049 | ret = -EBUSY; |
| 1050 | goto unmap; |
| 1051 | } |
| 1052 | |
| 1053 | /* If it is read and write than map bi-directional. */ |
| 1054 | if (range->pfns[i] & range->flags[HMM_PFN_WRITE]) |
| 1055 | dir = DMA_BIDIRECTIONAL; |
| 1056 | |
| 1057 | daddrs[i] = dma_map_page(device, page, 0, PAGE_SIZE, dir); |
| 1058 | if (dma_mapping_error(device, daddrs[i])) { |
| 1059 | ret = -EFAULT; |
| 1060 | goto unmap; |
| 1061 | } |
| 1062 | |
| 1063 | mapped++; |
| 1064 | } |
| 1065 | |
| 1066 | return mapped; |
| 1067 | |
| 1068 | unmap: |
| 1069 | for (npages = i, i = 0; (i < npages) && mapped; ++i) { |
| 1070 | enum dma_data_direction dir = DMA_TO_DEVICE; |
| 1071 | struct page *page; |
| 1072 | |
Jérôme Glisse | 391aab1 | 2019-05-13 17:20:31 -0700 | [diff] [blame] | 1073 | page = hmm_device_entry_to_page(range, range->pfns[i]); |
Jérôme Glisse | 55c0ece | 2019-05-13 17:20:28 -0700 | [diff] [blame] | 1074 | if (page == NULL) |
| 1075 | continue; |
| 1076 | |
| 1077 | if (dma_mapping_error(device, daddrs[i])) |
| 1078 | continue; |
| 1079 | |
| 1080 | /* If it is read and write than map bi-directional. */ |
| 1081 | if (range->pfns[i] & range->flags[HMM_PFN_WRITE]) |
| 1082 | dir = DMA_BIDIRECTIONAL; |
| 1083 | |
| 1084 | dma_unmap_page(device, daddrs[i], PAGE_SIZE, dir); |
| 1085 | mapped--; |
| 1086 | } |
| 1087 | |
| 1088 | return ret; |
| 1089 | } |
| 1090 | EXPORT_SYMBOL(hmm_range_dma_map); |
| 1091 | |
| 1092 | /** |
| 1093 | * hmm_range_dma_unmap() - unmap range of that was map with hmm_range_dma_map() |
| 1094 | * @range: range being unmapped |
Jérôme Glisse | 55c0ece | 2019-05-13 17:20:28 -0700 | [diff] [blame] | 1095 | * @device: device against which dma map was done |
| 1096 | * @daddrs: dma address of mapped pages |
| 1097 | * @dirty: dirty page if it had the write flag set |
Ralph Campbell | 085ea25 | 2019-05-06 16:29:39 -0700 | [diff] [blame] | 1098 | * Return: number of page unmapped on success, -EINVAL otherwise |
Jérôme Glisse | 55c0ece | 2019-05-13 17:20:28 -0700 | [diff] [blame] | 1099 | * |
| 1100 | * Note that caller MUST abide by mmu notifier or use HMM mirror and abide |
| 1101 | * to the sync_cpu_device_pagetables() callback so that it is safe here to |
| 1102 | * call set_page_dirty(). Caller must also take appropriate locks to avoid |
| 1103 | * concurrent mmu notifier or sync_cpu_device_pagetables() to make progress. |
| 1104 | */ |
| 1105 | long hmm_range_dma_unmap(struct hmm_range *range, |
Jérôme Glisse | 55c0ece | 2019-05-13 17:20:28 -0700 | [diff] [blame] | 1106 | struct device *device, |
| 1107 | dma_addr_t *daddrs, |
| 1108 | bool dirty) |
| 1109 | { |
| 1110 | unsigned long i, npages; |
| 1111 | long cpages = 0; |
| 1112 | |
| 1113 | /* Sanity check. */ |
| 1114 | if (range->end <= range->start) |
| 1115 | return -EINVAL; |
| 1116 | if (!daddrs) |
| 1117 | return -EINVAL; |
| 1118 | if (!range->pfns) |
| 1119 | return -EINVAL; |
| 1120 | |
| 1121 | npages = (range->end - range->start) >> PAGE_SHIFT; |
| 1122 | for (i = 0; i < npages; ++i) { |
| 1123 | enum dma_data_direction dir = DMA_TO_DEVICE; |
| 1124 | struct page *page; |
| 1125 | |
Jérôme Glisse | 391aab1 | 2019-05-13 17:20:31 -0700 | [diff] [blame] | 1126 | page = hmm_device_entry_to_page(range, range->pfns[i]); |
Jérôme Glisse | 55c0ece | 2019-05-13 17:20:28 -0700 | [diff] [blame] | 1127 | if (page == NULL) |
| 1128 | continue; |
| 1129 | |
| 1130 | /* If it is read and write than map bi-directional. */ |
| 1131 | if (range->pfns[i] & range->flags[HMM_PFN_WRITE]) { |
| 1132 | dir = DMA_BIDIRECTIONAL; |
| 1133 | |
| 1134 | /* |
| 1135 | * See comments in function description on why it is |
| 1136 | * safe here to call set_page_dirty() |
| 1137 | */ |
| 1138 | if (dirty) |
| 1139 | set_page_dirty(page); |
| 1140 | } |
| 1141 | |
| 1142 | /* Unmap and clear pfns/dma address */ |
| 1143 | dma_unmap_page(device, daddrs[i], PAGE_SIZE, dir); |
| 1144 | range->pfns[i] = range->values[HMM_PFN_NONE]; |
| 1145 | /* FIXME see comments in hmm_vma_dma_map() */ |
| 1146 | daddrs[i] = 0; |
| 1147 | cpages++; |
| 1148 | } |
| 1149 | |
| 1150 | return cpages; |
| 1151 | } |
| 1152 | EXPORT_SYMBOL(hmm_range_dma_unmap); |