blob: 6b0136665407a3b17e1aa44bb6cb24a4a52851cd [file] [log] [blame]
Thomas Gleixnerc942fdd2019-05-27 08:55:06 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Jérôme Glisse133ff0e2017-09-08 16:11:23 -07002/*
3 * Copyright 2013 Red Hat Inc.
4 *
Jérôme Glissef813f212018-10-30 15:04:06 -07005 * Authors: Jérôme Glisse <jglisse@redhat.com>
Jérôme Glisse133ff0e2017-09-08 16:11:23 -07006 */
7/*
8 * Refer to include/linux/hmm.h for information about heterogeneous memory
9 * management or HMM for short.
10 */
Christoph Hellwiga5201102019-08-28 16:19:53 +020011#include <linux/pagewalk.h>
Jérôme Glisse133ff0e2017-09-08 16:11:23 -070012#include <linux/hmm.h>
Jérôme Glisse858b54d2017-09-08 16:12:02 -070013#include <linux/init.h>
Jérôme Glisseda4c3c72017-09-08 16:11:31 -070014#include <linux/rmap.h>
15#include <linux/swap.h>
Jérôme Glisse133ff0e2017-09-08 16:11:23 -070016#include <linux/slab.h>
17#include <linux/sched.h>
Jérôme Glisse4ef589d2017-09-08 16:11:58 -070018#include <linux/mmzone.h>
19#include <linux/pagemap.h>
Jérôme Glisseda4c3c72017-09-08 16:11:31 -070020#include <linux/swapops.h>
21#include <linux/hugetlb.h>
Jérôme Glisse4ef589d2017-09-08 16:11:58 -070022#include <linux/memremap.h>
Jason Gunthorpec8a53b22019-05-23 10:36:46 -030023#include <linux/sched/mm.h>
Jérôme Glisse7b2d55d22017-09-08 16:11:46 -070024#include <linux/jump_label.h>
Jérôme Glisse55c0ece2019-05-13 17:20:28 -070025#include <linux/dma-mapping.h>
Jérôme Glissec0b12402017-09-08 16:11:27 -070026#include <linux/mmu_notifier.h>
Jérôme Glisse4ef589d2017-09-08 16:11:58 -070027#include <linux/memory_hotplug.h>
28
Jason Gunthorpec7d8b782019-08-06 20:15:42 -030029static struct mmu_notifier *hmm_alloc_notifier(struct mm_struct *mm)
Jérôme Glisse704f3f22019-05-13 17:19:48 -070030{
Jason Gunthorpe8a9320b2019-05-23 10:24:13 -030031 struct hmm *hmm;
Jérôme Glisse133ff0e2017-09-08 16:11:23 -070032
Jason Gunthorpec7d8b782019-08-06 20:15:42 -030033 hmm = kzalloc(sizeof(*hmm), GFP_KERNEL);
Jérôme Glissec0b12402017-09-08 16:11:27 -070034 if (!hmm)
Jason Gunthorpec7d8b782019-08-06 20:15:42 -030035 return ERR_PTR(-ENOMEM);
36
Jérôme Glissea3e0d412019-05-13 17:20:01 -070037 init_waitqueue_head(&hmm->wq);
Jérôme Glissec0b12402017-09-08 16:11:27 -070038 INIT_LIST_HEAD(&hmm->mirrors);
39 init_rwsem(&hmm->mirrors_sem);
Jérôme Glisseda4c3c72017-09-08 16:11:31 -070040 INIT_LIST_HEAD(&hmm->ranges);
Jason Gunthorpe5a136b42019-06-07 12:10:33 -030041 spin_lock_init(&hmm->ranges_lock);
Jérôme Glissea3e0d412019-05-13 17:20:01 -070042 hmm->notifiers = 0;
Jason Gunthorpec7d8b782019-08-06 20:15:42 -030043 return &hmm->mmu_notifier;
Jérôme Glisse133ff0e2017-09-08 16:11:23 -070044}
45
Jason Gunthorpec7d8b782019-08-06 20:15:42 -030046static void hmm_free_notifier(struct mmu_notifier *mn)
Jason Gunthorpe6d7c3cd2019-05-22 16:52:52 -030047{
Jason Gunthorpec7d8b782019-08-06 20:15:42 -030048 struct hmm *hmm = container_of(mn, struct hmm, mmu_notifier);
Jason Gunthorpe8a9320b2019-05-23 10:24:13 -030049
Jason Gunthorpec7d8b782019-08-06 20:15:42 -030050 WARN_ON(!list_empty(&hmm->ranges));
51 WARN_ON(!list_empty(&hmm->mirrors));
Jérôme Glisse133ff0e2017-09-08 16:11:23 -070052 kfree(hmm);
Jérôme Glisse133ff0e2017-09-08 16:11:23 -070053}
54
Jérôme Glissea3e0d412019-05-13 17:20:01 -070055static void hmm_release(struct mmu_notifier *mn, struct mm_struct *mm)
Jérôme Glissec0b12402017-09-08 16:11:27 -070056{
Jason Gunthorpe6d7c3cd2019-05-22 16:52:52 -030057 struct hmm *hmm = container_of(mn, struct hmm, mmu_notifier);
Jérôme Glissec0b12402017-09-08 16:11:27 -070058 struct hmm_mirror *mirror;
Jérôme Glisseda4c3c72017-09-08 16:11:31 -070059
Jason Gunthorpe47f24592019-05-23 11:08:28 -030060 /*
61 * Since hmm_range_register() holds the mmget() lock hmm_release() is
62 * prevented as long as a range exists.
63 */
64 WARN_ON(!list_empty_careful(&hmm->ranges));
Ralph Campbelle1401512018-04-10 16:28:19 -070065
Jason Gunthorpe14331722019-05-24 12:14:08 -030066 down_read(&hmm->mirrors_sem);
Jérôme Glissea3e0d412019-05-13 17:20:01 -070067 list_for_each_entry(mirror, &hmm->mirrors, list) {
Jason Gunthorpe14331722019-05-24 12:14:08 -030068 /*
69 * Note: The driver is not allowed to trigger
70 * hmm_mirror_unregister() from this thread.
71 */
72 if (mirror->ops->release)
Ralph Campbelle1401512018-04-10 16:28:19 -070073 mirror->ops->release(mirror);
Jérôme Glissea3e0d412019-05-13 17:20:01 -070074 }
75 up_read(&hmm->mirrors_sem);
Jérôme Glissec0b12402017-09-08 16:11:27 -070076}
77
Jason Gunthorpe5a136b42019-06-07 12:10:33 -030078static void notifiers_decrement(struct hmm *hmm)
Jérôme Glissec0b12402017-09-08 16:11:27 -070079{
Jason Gunthorpe5a136b42019-06-07 12:10:33 -030080 unsigned long flags;
Jérôme Glissec0b12402017-09-08 16:11:27 -070081
Jason Gunthorpe5a136b42019-06-07 12:10:33 -030082 spin_lock_irqsave(&hmm->ranges_lock, flags);
Jérôme Glissea3e0d412019-05-13 17:20:01 -070083 hmm->notifiers--;
84 if (!hmm->notifiers) {
85 struct hmm_range *range;
86
87 list_for_each_entry(range, &hmm->ranges, list) {
88 if (range->valid)
89 continue;
90 range->valid = true;
91 }
92 wake_up_all(&hmm->wq);
93 }
Jason Gunthorpe5a136b42019-06-07 12:10:33 -030094 spin_unlock_irqrestore(&hmm->ranges_lock, flags);
95}
Jérôme Glissea3e0d412019-05-13 17:20:01 -070096
Jérôme Glisse133ff0e2017-09-08 16:11:23 -070097static int hmm_invalidate_range_start(struct mmu_notifier *mn,
98 const struct mmu_notifier_range *nrange)
99{
Jason Gunthorpe6d7c3cd2019-05-22 16:52:52 -0300100 struct hmm *hmm = container_of(mn, struct hmm, mmu_notifier);
Jérôme Glisse133ff0e2017-09-08 16:11:23 -0700101 struct hmm_mirror *mirror;
Jérôme Glisse133ff0e2017-09-08 16:11:23 -0700102 struct hmm_range *range;
Jason Gunthorpe5a136b42019-06-07 12:10:33 -0300103 unsigned long flags;
Jérôme Glisse133ff0e2017-09-08 16:11:23 -0700104 int ret = 0;
105
Jason Gunthorpe5a136b42019-06-07 12:10:33 -0300106 spin_lock_irqsave(&hmm->ranges_lock, flags);
Jérôme Glisse133ff0e2017-09-08 16:11:23 -0700107 hmm->notifiers++;
108 list_for_each_entry(range, &hmm->ranges, list) {
Ralph Campbell1f961802019-07-25 17:56:44 -0700109 if (nrange->end < range->start || nrange->start >= range->end)
Jérôme Glisse133ff0e2017-09-08 16:11:23 -0700110 continue;
111
112 range->valid = false;
113 }
Jason Gunthorpe5a136b42019-06-07 12:10:33 -0300114 spin_unlock_irqrestore(&hmm->ranges_lock, flags);
Jérôme Glissec0b12402017-09-08 16:11:27 -0700115
116 if (mmu_notifier_range_blockable(nrange))
117 down_read(&hmm->mirrors_sem);
118 else if (!down_read_trylock(&hmm->mirrors_sem)) {
119 ret = -EAGAIN;
120 goto out;
121 }
Jérôme Glissec0b12402017-09-08 16:11:27 -0700122
Jason Gunthorpe5a136b42019-06-07 12:10:33 -0300123 list_for_each_entry(mirror, &hmm->mirrors, list) {
124 int rc;
125
Ralph Campbell1f961802019-07-25 17:56:44 -0700126 rc = mirror->ops->sync_cpu_device_pagetables(mirror, nrange);
Jason Gunthorpe5a136b42019-06-07 12:10:33 -0300127 if (rc) {
Ralph Campbell1f961802019-07-25 17:56:44 -0700128 if (WARN_ON(mmu_notifier_range_blockable(nrange) ||
129 rc != -EAGAIN))
Jason Gunthorpe5a136b42019-06-07 12:10:33 -0300130 continue;
131 ret = -EAGAIN;
Ralph Campbell085ea252019-05-06 16:29:39 -0700132 break;
Jason Gunthorpe5a136b42019-06-07 12:10:33 -0300133 }
Jérôme Glissec0b12402017-09-08 16:11:27 -0700134 }
135 up_read(&hmm->mirrors_sem);
136
137out:
Jason Gunthorpe5a136b42019-06-07 12:10:33 -0300138 if (ret)
139 notifiers_decrement(hmm);
Jérôme Glissec0b12402017-09-08 16:11:27 -0700140 return ret;
141}
142
143static void hmm_invalidate_range_end(struct mmu_notifier *mn,
144 const struct mmu_notifier_range *nrange)
145{
Jason Gunthorpe6d7c3cd2019-05-22 16:52:52 -0300146 struct hmm *hmm = container_of(mn, struct hmm, mmu_notifier);
Jérôme Glissec0b12402017-09-08 16:11:27 -0700147
Jason Gunthorpe5a136b42019-06-07 12:10:33 -0300148 notifiers_decrement(hmm);
Jérôme Glissec0b12402017-09-08 16:11:27 -0700149}
150
151static const struct mmu_notifier_ops hmm_mmu_notifier_ops = {
Ralph Campbelle1401512018-04-10 16:28:19 -0700152 .release = hmm_release,
Jérôme Glissec0b12402017-09-08 16:11:27 -0700153 .invalidate_range_start = hmm_invalidate_range_start,
154 .invalidate_range_end = hmm_invalidate_range_end,
Jason Gunthorpec7d8b782019-08-06 20:15:42 -0300155 .alloc_notifier = hmm_alloc_notifier,
156 .free_notifier = hmm_free_notifier,
Jérôme Glissec0b12402017-09-08 16:11:27 -0700157};
158
159/*
160 * hmm_mirror_register() - register a mirror against an mm
161 *
162 * @mirror: new mirror struct to register
163 * @mm: mm to register against
Ralph Campbell085ea252019-05-06 16:29:39 -0700164 * Return: 0 on success, -ENOMEM if no memory, -EINVAL if invalid arguments
Jérôme Glissec0b12402017-09-08 16:11:27 -0700165 *
166 * To start mirroring a process address space, the device driver must register
167 * an HMM mirror struct.
Jason Gunthorpec7d8b782019-08-06 20:15:42 -0300168 *
169 * The caller cannot unregister the hmm_mirror while any ranges are
170 * registered.
171 *
172 * Callers using this function must put a call to mmu_notifier_synchronize()
173 * in their module exit functions.
Jérôme Glissec0b12402017-09-08 16:11:27 -0700174 */
175int hmm_mirror_register(struct hmm_mirror *mirror, struct mm_struct *mm)
176{
Jason Gunthorpec7d8b782019-08-06 20:15:42 -0300177 struct mmu_notifier *mn;
178
Linus Torvaldsfec88ab2019-07-14 19:42:11 -0700179 lockdep_assert_held_write(&mm->mmap_sem);
Jason Gunthorpe8a1a0cd2019-05-23 11:23:30 -0300180
Jérôme Glissec0b12402017-09-08 16:11:27 -0700181 /* Sanity check */
182 if (!mm || !mirror || !mirror->ops)
183 return -EINVAL;
184
Jason Gunthorpec7d8b782019-08-06 20:15:42 -0300185 mn = mmu_notifier_get_locked(&hmm_mmu_notifier_ops, mm);
186 if (IS_ERR(mn))
187 return PTR_ERR(mn);
188 mirror->hmm = container_of(mn, struct hmm, mmu_notifier);
Jérôme Glissec0b12402017-09-08 16:11:27 -0700189
190 down_write(&mirror->hmm->mirrors_sem);
Jérôme Glisse704f3f22019-05-13 17:19:48 -0700191 list_add(&mirror->list, &mirror->hmm->mirrors);
192 up_write(&mirror->hmm->mirrors_sem);
Jérôme Glissec0b12402017-09-08 16:11:27 -0700193
194 return 0;
195}
196EXPORT_SYMBOL(hmm_mirror_register);
197
198/*
199 * hmm_mirror_unregister() - unregister a mirror
200 *
Ralph Campbell085ea252019-05-06 16:29:39 -0700201 * @mirror: mirror struct to unregister
Jérôme Glissec0b12402017-09-08 16:11:27 -0700202 *
203 * Stop mirroring a process address space, and cleanup.
204 */
205void hmm_mirror_unregister(struct hmm_mirror *mirror)
206{
Jason Gunthorpe187229c2019-05-23 11:31:45 -0300207 struct hmm *hmm = mirror->hmm;
Jérôme Glissec01cbba2018-04-10 16:28:23 -0700208
Jérôme Glissec0b12402017-09-08 16:11:27 -0700209 down_write(&hmm->mirrors_sem);
Jason Gunthorpe14331722019-05-24 12:14:08 -0300210 list_del(&mirror->list);
Jérôme Glissec0b12402017-09-08 16:11:27 -0700211 up_write(&hmm->mirrors_sem);
Jason Gunthorpec7d8b782019-08-06 20:15:42 -0300212 mmu_notifier_put(&hmm->mmu_notifier);
Jérôme Glissec0b12402017-09-08 16:11:27 -0700213}
214EXPORT_SYMBOL(hmm_mirror_unregister);
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700215
Jérôme Glisse74eee182017-09-08 16:11:35 -0700216struct hmm_vma_walk {
217 struct hmm_range *range;
Jérôme Glisse992de9a2019-05-13 17:20:21 -0700218 struct dev_pagemap *pgmap;
Jérôme Glisse74eee182017-09-08 16:11:35 -0700219 unsigned long last;
Christoph Hellwig9a4903e2019-07-25 17:56:46 -0700220 unsigned int flags;
Jérôme Glisse74eee182017-09-08 16:11:35 -0700221};
222
Jérôme Glisse2aee09d2018-04-10 16:29:02 -0700223static int hmm_vma_do_fault(struct mm_walk *walk, unsigned long addr,
224 bool write_fault, uint64_t *pfn)
Jérôme Glisse74eee182017-09-08 16:11:35 -0700225{
Kuehling, Felix9b1ae602019-05-10 19:53:24 +0000226 unsigned int flags = FAULT_FLAG_REMOTE;
Jérôme Glisse74eee182017-09-08 16:11:35 -0700227 struct hmm_vma_walk *hmm_vma_walk = walk->private;
Jérôme Glissef88a1e92018-04-10 16:29:06 -0700228 struct hmm_range *range = hmm_vma_walk->range;
Jérôme Glisse74eee182017-09-08 16:11:35 -0700229 struct vm_area_struct *vma = walk->vma;
Souptick Joarder50a7ca32018-08-17 15:44:47 -0700230 vm_fault_t ret;
Jérôme Glisse74eee182017-09-08 16:11:35 -0700231
Ralph Campbell6c64f2b2019-08-23 15:17:52 -0700232 if (!vma)
233 goto err;
234
Christoph Hellwig9a4903e2019-07-25 17:56:46 -0700235 if (hmm_vma_walk->flags & HMM_FAULT_ALLOW_RETRY)
236 flags |= FAULT_FLAG_ALLOW_RETRY;
237 if (write_fault)
238 flags |= FAULT_FLAG_WRITE;
239
Souptick Joarder50a7ca32018-08-17 15:44:47 -0700240 ret = handle_mm_fault(vma, addr, flags);
Jason Gunthorpee709acc2019-07-24 08:52:58 +0200241 if (ret & VM_FAULT_RETRY) {
242 /* Note, handle_mm_fault did up_read(&mm->mmap_sem)) */
Jérôme Glisse73231612019-05-13 17:19:58 -0700243 return -EAGAIN;
Jason Gunthorpee709acc2019-07-24 08:52:58 +0200244 }
Ralph Campbell6c64f2b2019-08-23 15:17:52 -0700245 if (ret & VM_FAULT_ERROR)
246 goto err;
Jérôme Glisse74eee182017-09-08 16:11:35 -0700247
Jérôme Glisse73231612019-05-13 17:19:58 -0700248 return -EBUSY;
Ralph Campbell6c64f2b2019-08-23 15:17:52 -0700249
250err:
251 *pfn = range->values[HMM_PFN_ERROR];
252 return -EFAULT;
Jérôme Glisse74eee182017-09-08 16:11:35 -0700253}
254
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700255static int hmm_pfns_bad(unsigned long addr,
256 unsigned long end,
257 struct mm_walk *walk)
258{
Jérôme Glissec7195472018-04-10 16:28:27 -0700259 struct hmm_vma_walk *hmm_vma_walk = walk->private;
260 struct hmm_range *range = hmm_vma_walk->range;
Jérôme Glisseff05c0c2018-04-10 16:28:38 -0700261 uint64_t *pfns = range->pfns;
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700262 unsigned long i;
263
264 i = (addr - range->start) >> PAGE_SHIFT;
265 for (; addr < end; addr += PAGE_SIZE, i++)
Jérôme Glissef88a1e92018-04-10 16:29:06 -0700266 pfns[i] = range->values[HMM_PFN_ERROR];
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700267
268 return 0;
269}
270
Jérôme Glisse5504ed22018-04-10 16:28:46 -0700271/*
Ralph Campbelld2e8d552019-07-25 17:56:45 -0700272 * hmm_vma_walk_hole_() - handle a range lacking valid pmd or pte(s)
273 * @addr: range virtual start address (inclusive)
Jérôme Glisse5504ed22018-04-10 16:28:46 -0700274 * @end: range virtual end address (exclusive)
Jérôme Glisse2aee09d2018-04-10 16:29:02 -0700275 * @fault: should we fault or not ?
276 * @write_fault: write fault ?
Jérôme Glisse5504ed22018-04-10 16:28:46 -0700277 * @walk: mm_walk structure
Ralph Campbell085ea252019-05-06 16:29:39 -0700278 * Return: 0 on success, -EBUSY after page fault, or page fault error
Jérôme Glisse5504ed22018-04-10 16:28:46 -0700279 *
280 * This function will be called whenever pmd_none() or pte_none() returns true,
281 * or whenever there is no page directory covering the virtual address range.
282 */
Jérôme Glisse2aee09d2018-04-10 16:29:02 -0700283static int hmm_vma_walk_hole_(unsigned long addr, unsigned long end,
284 bool fault, bool write_fault,
285 struct mm_walk *walk)
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700286{
Jérôme Glisse74eee182017-09-08 16:11:35 -0700287 struct hmm_vma_walk *hmm_vma_walk = walk->private;
288 struct hmm_range *range = hmm_vma_walk->range;
Jérôme Glisseff05c0c2018-04-10 16:28:38 -0700289 uint64_t *pfns = range->pfns;
Christoph Hellwig7f082632019-08-06 19:05:45 +0300290 unsigned long i;
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700291
Jérôme Glisse74eee182017-09-08 16:11:35 -0700292 hmm_vma_walk->last = addr;
Christoph Hellwig7f082632019-08-06 19:05:45 +0300293 i = (addr - range->start) >> PAGE_SHIFT;
Jérôme Glisse63d50662019-05-13 17:20:18 -0700294
Ralph Campbellc18ce674d2019-08-23 15:17:53 -0700295 if (write_fault && walk->vma && !(walk->vma->vm_flags & VM_WRITE))
296 return -EPERM;
297
Christoph Hellwig7f082632019-08-06 19:05:45 +0300298 for (; addr < end; addr += PAGE_SIZE, i++) {
Jérôme Glissef88a1e92018-04-10 16:29:06 -0700299 pfns[i] = range->values[HMM_PFN_NONE];
Jérôme Glisse2aee09d2018-04-10 16:29:02 -0700300 if (fault || write_fault) {
Jérôme Glisse74eee182017-09-08 16:11:35 -0700301 int ret;
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700302
Jérôme Glisse2aee09d2018-04-10 16:29:02 -0700303 ret = hmm_vma_do_fault(walk, addr, write_fault,
304 &pfns[i]);
Jérôme Glisse73231612019-05-13 17:19:58 -0700305 if (ret != -EBUSY)
Jérôme Glisse74eee182017-09-08 16:11:35 -0700306 return ret;
307 }
308 }
309
Jérôme Glisse73231612019-05-13 17:19:58 -0700310 return (fault || write_fault) ? -EBUSY : 0;
Jérôme Glisse2aee09d2018-04-10 16:29:02 -0700311}
312
313static inline void hmm_pte_need_fault(const struct hmm_vma_walk *hmm_vma_walk,
314 uint64_t pfns, uint64_t cpu_flags,
315 bool *fault, bool *write_fault)
316{
Jérôme Glissef88a1e92018-04-10 16:29:06 -0700317 struct hmm_range *range = hmm_vma_walk->range;
318
Christoph Hellwigd45d4642019-07-25 17:56:47 -0700319 if (hmm_vma_walk->flags & HMM_FAULT_SNAPSHOT)
Jérôme Glisse2aee09d2018-04-10 16:29:02 -0700320 return;
321
Jérôme Glisse023a0192019-05-13 17:20:05 -0700322 /*
323 * So we not only consider the individual per page request we also
324 * consider the default flags requested for the range. The API can
Ralph Campbelld2e8d552019-07-25 17:56:45 -0700325 * be used 2 ways. The first one where the HMM user coalesces
326 * multiple page faults into one request and sets flags per pfn for
327 * those faults. The second one where the HMM user wants to pre-
Jérôme Glisse023a0192019-05-13 17:20:05 -0700328 * fault a range with specific flags. For the latter one it is a
329 * waste to have the user pre-fill the pfn arrays with a default
330 * flags value.
331 */
332 pfns = (pfns & range->pfn_flags_mask) | range->default_flags;
333
Jérôme Glisse2aee09d2018-04-10 16:29:02 -0700334 /* We aren't ask to do anything ... */
Jérôme Glissef88a1e92018-04-10 16:29:06 -0700335 if (!(pfns & range->flags[HMM_PFN_VALID]))
Jérôme Glisse2aee09d2018-04-10 16:29:02 -0700336 return;
Ralph Campbelld2e8d552019-07-25 17:56:45 -0700337 /* If this is device memory then only fault if explicitly requested */
Jérôme Glissef88a1e92018-04-10 16:29:06 -0700338 if ((cpu_flags & range->flags[HMM_PFN_DEVICE_PRIVATE])) {
339 /* Do we fault on device memory ? */
340 if (pfns & range->flags[HMM_PFN_DEVICE_PRIVATE]) {
341 *write_fault = pfns & range->flags[HMM_PFN_WRITE];
342 *fault = true;
343 }
Jérôme Glisse2aee09d2018-04-10 16:29:02 -0700344 return;
345 }
Jérôme Glissef88a1e92018-04-10 16:29:06 -0700346
347 /* If CPU page table is not valid then we need to fault */
348 *fault = !(cpu_flags & range->flags[HMM_PFN_VALID]);
349 /* Need to write fault ? */
350 if ((pfns & range->flags[HMM_PFN_WRITE]) &&
351 !(cpu_flags & range->flags[HMM_PFN_WRITE])) {
352 *write_fault = true;
Jérôme Glisse2aee09d2018-04-10 16:29:02 -0700353 *fault = true;
354 }
355}
356
357static void hmm_range_need_fault(const struct hmm_vma_walk *hmm_vma_walk,
358 const uint64_t *pfns, unsigned long npages,
359 uint64_t cpu_flags, bool *fault,
360 bool *write_fault)
361{
362 unsigned long i;
363
Christoph Hellwigd45d4642019-07-25 17:56:47 -0700364 if (hmm_vma_walk->flags & HMM_FAULT_SNAPSHOT) {
Jérôme Glisse2aee09d2018-04-10 16:29:02 -0700365 *fault = *write_fault = false;
366 return;
367 }
368
Jérôme Glissea3e0d412019-05-13 17:20:01 -0700369 *fault = *write_fault = false;
Jérôme Glisse2aee09d2018-04-10 16:29:02 -0700370 for (i = 0; i < npages; ++i) {
371 hmm_pte_need_fault(hmm_vma_walk, pfns[i], cpu_flags,
372 fault, write_fault);
Jérôme Glissea3e0d412019-05-13 17:20:01 -0700373 if ((*write_fault))
Jérôme Glisse2aee09d2018-04-10 16:29:02 -0700374 return;
375 }
376}
377
378static int hmm_vma_walk_hole(unsigned long addr, unsigned long end,
379 struct mm_walk *walk)
380{
381 struct hmm_vma_walk *hmm_vma_walk = walk->private;
382 struct hmm_range *range = hmm_vma_walk->range;
383 bool fault, write_fault;
384 unsigned long i, npages;
385 uint64_t *pfns;
386
387 i = (addr - range->start) >> PAGE_SHIFT;
388 npages = (end - addr) >> PAGE_SHIFT;
389 pfns = &range->pfns[i];
390 hmm_range_need_fault(hmm_vma_walk, pfns, npages,
391 0, &fault, &write_fault);
392 return hmm_vma_walk_hole_(addr, end, fault, write_fault, walk);
393}
394
Jérôme Glissef88a1e92018-04-10 16:29:06 -0700395static inline uint64_t pmd_to_hmm_pfn_flags(struct hmm_range *range, pmd_t pmd)
Jérôme Glisse2aee09d2018-04-10 16:29:02 -0700396{
397 if (pmd_protnone(pmd))
398 return 0;
Jérôme Glissef88a1e92018-04-10 16:29:06 -0700399 return pmd_write(pmd) ? range->flags[HMM_PFN_VALID] |
400 range->flags[HMM_PFN_WRITE] :
401 range->flags[HMM_PFN_VALID];
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700402}
403
Jérôme Glisse992de9a2019-05-13 17:20:21 -0700404#ifdef CONFIG_TRANSPARENT_HUGEPAGE
Christoph Hellwig9d3973d2019-08-06 19:05:49 +0300405static int hmm_vma_handle_pmd(struct mm_walk *walk, unsigned long addr,
406 unsigned long end, uint64_t *pfns, pmd_t pmd)
407{
Jérôme Glisse53f5c3f2018-04-10 16:28:59 -0700408 struct hmm_vma_walk *hmm_vma_walk = walk->private;
Jérôme Glissef88a1e92018-04-10 16:29:06 -0700409 struct hmm_range *range = hmm_vma_walk->range;
Jérôme Glisse2aee09d2018-04-10 16:29:02 -0700410 unsigned long pfn, npages, i;
Jérôme Glisse2aee09d2018-04-10 16:29:02 -0700411 bool fault, write_fault;
Jérôme Glissef88a1e92018-04-10 16:29:06 -0700412 uint64_t cpu_flags;
Jérôme Glisse53f5c3f2018-04-10 16:28:59 -0700413
Jérôme Glisse2aee09d2018-04-10 16:29:02 -0700414 npages = (end - addr) >> PAGE_SHIFT;
Jérôme Glissef88a1e92018-04-10 16:29:06 -0700415 cpu_flags = pmd_to_hmm_pfn_flags(range, pmd);
Jérôme Glisse2aee09d2018-04-10 16:29:02 -0700416 hmm_range_need_fault(hmm_vma_walk, pfns, npages, cpu_flags,
417 &fault, &write_fault);
Jérôme Glisse53f5c3f2018-04-10 16:28:59 -0700418
Jérôme Glisse2aee09d2018-04-10 16:29:02 -0700419 if (pmd_protnone(pmd) || fault || write_fault)
420 return hmm_vma_walk_hole_(addr, end, fault, write_fault, walk);
Jérôme Glisse53f5c3f2018-04-10 16:28:59 -0700421
Christoph Hellwig309f9a42019-08-06 19:05:47 +0300422 pfn = pmd_pfn(pmd) + ((addr & ~PMD_MASK) >> PAGE_SHIFT);
Jérôme Glisse992de9a2019-05-13 17:20:21 -0700423 for (i = 0; addr < end; addr += PAGE_SIZE, i++, pfn++) {
424 if (pmd_devmap(pmd)) {
425 hmm_vma_walk->pgmap = get_dev_pagemap(pfn,
426 hmm_vma_walk->pgmap);
427 if (unlikely(!hmm_vma_walk->pgmap))
428 return -EBUSY;
429 }
Jérôme Glisse391aab12019-05-13 17:20:31 -0700430 pfns[i] = hmm_device_entry_from_pfn(range, pfn) | cpu_flags;
Jérôme Glisse992de9a2019-05-13 17:20:21 -0700431 }
432 if (hmm_vma_walk->pgmap) {
433 put_dev_pagemap(hmm_vma_walk->pgmap);
434 hmm_vma_walk->pgmap = NULL;
435 }
Jérôme Glisse53f5c3f2018-04-10 16:28:59 -0700436 hmm_vma_walk->last = end;
437 return 0;
438}
Christoph Hellwig9d3973d2019-08-06 19:05:49 +0300439#else /* CONFIG_TRANSPARENT_HUGEPAGE */
440/* stub to allow the code below to compile */
441int hmm_vma_handle_pmd(struct mm_walk *walk, unsigned long addr,
442 unsigned long end, uint64_t *pfns, pmd_t pmd);
443#endif /* CONFIG_TRANSPARENT_HUGEPAGE */
Jérôme Glisse53f5c3f2018-04-10 16:28:59 -0700444
Jérôme Glissef88a1e92018-04-10 16:29:06 -0700445static inline uint64_t pte_to_hmm_pfn_flags(struct hmm_range *range, pte_t pte)
Jérôme Glisse2aee09d2018-04-10 16:29:02 -0700446{
Philip Yang789c2af2019-05-23 16:32:31 -0400447 if (pte_none(pte) || !pte_present(pte) || pte_protnone(pte))
Jérôme Glisse2aee09d2018-04-10 16:29:02 -0700448 return 0;
Jérôme Glissef88a1e92018-04-10 16:29:06 -0700449 return pte_write(pte) ? range->flags[HMM_PFN_VALID] |
450 range->flags[HMM_PFN_WRITE] :
451 range->flags[HMM_PFN_VALID];
Jérôme Glisse2aee09d2018-04-10 16:29:02 -0700452}
453
Jérôme Glisse53f5c3f2018-04-10 16:28:59 -0700454static int hmm_vma_handle_pte(struct mm_walk *walk, unsigned long addr,
455 unsigned long end, pmd_t *pmdp, pte_t *ptep,
456 uint64_t *pfn)
457{
458 struct hmm_vma_walk *hmm_vma_walk = walk->private;
Jérôme Glissef88a1e92018-04-10 16:29:06 -0700459 struct hmm_range *range = hmm_vma_walk->range;
Jérôme Glisse2aee09d2018-04-10 16:29:02 -0700460 bool fault, write_fault;
461 uint64_t cpu_flags;
Jérôme Glisse53f5c3f2018-04-10 16:28:59 -0700462 pte_t pte = *ptep;
Jérôme Glissef88a1e92018-04-10 16:29:06 -0700463 uint64_t orig_pfn = *pfn;
Jérôme Glisse53f5c3f2018-04-10 16:28:59 -0700464
Jérôme Glissef88a1e92018-04-10 16:29:06 -0700465 *pfn = range->values[HMM_PFN_NONE];
Jérôme Glisse73231612019-05-13 17:19:58 -0700466 fault = write_fault = false;
Jérôme Glisse53f5c3f2018-04-10 16:28:59 -0700467
468 if (pte_none(pte)) {
Jérôme Glisse73231612019-05-13 17:19:58 -0700469 hmm_pte_need_fault(hmm_vma_walk, orig_pfn, 0,
470 &fault, &write_fault);
Jérôme Glisse2aee09d2018-04-10 16:29:02 -0700471 if (fault || write_fault)
Jérôme Glisse53f5c3f2018-04-10 16:28:59 -0700472 goto fault;
473 return 0;
474 }
475
476 if (!pte_present(pte)) {
477 swp_entry_t entry = pte_to_swp_entry(pte);
478
479 if (!non_swap_entry(entry)) {
Yang, Philipe3fe8e52019-08-15 20:52:56 +0000480 cpu_flags = pte_to_hmm_pfn_flags(range, pte);
481 hmm_pte_need_fault(hmm_vma_walk, orig_pfn, cpu_flags,
482 &fault, &write_fault);
Jérôme Glisse2aee09d2018-04-10 16:29:02 -0700483 if (fault || write_fault)
Jérôme Glisse53f5c3f2018-04-10 16:28:59 -0700484 goto fault;
485 return 0;
486 }
487
488 /*
489 * This is a special swap entry, ignore migration, use
490 * device and report anything else as error.
491 */
492 if (is_device_private_entry(entry)) {
Jérôme Glissef88a1e92018-04-10 16:29:06 -0700493 cpu_flags = range->flags[HMM_PFN_VALID] |
494 range->flags[HMM_PFN_DEVICE_PRIVATE];
Jérôme Glisse2aee09d2018-04-10 16:29:02 -0700495 cpu_flags |= is_write_device_private_entry(entry) ?
Jérôme Glissef88a1e92018-04-10 16:29:06 -0700496 range->flags[HMM_PFN_WRITE] : 0;
497 hmm_pte_need_fault(hmm_vma_walk, orig_pfn, cpu_flags,
498 &fault, &write_fault);
499 if (fault || write_fault)
500 goto fault;
Jérôme Glisse391aab12019-05-13 17:20:31 -0700501 *pfn = hmm_device_entry_from_pfn(range,
502 swp_offset(entry));
Jérôme Glissef88a1e92018-04-10 16:29:06 -0700503 *pfn |= cpu_flags;
Jérôme Glisse53f5c3f2018-04-10 16:28:59 -0700504 return 0;
505 }
506
507 if (is_migration_entry(entry)) {
Jérôme Glisse2aee09d2018-04-10 16:29:02 -0700508 if (fault || write_fault) {
Jérôme Glisse53f5c3f2018-04-10 16:28:59 -0700509 pte_unmap(ptep);
510 hmm_vma_walk->last = addr;
Ralph Campbelld2e8d552019-07-25 17:56:45 -0700511 migration_entry_wait(walk->mm, pmdp, addr);
Jérôme Glisse73231612019-05-13 17:19:58 -0700512 return -EBUSY;
Jérôme Glisse53f5c3f2018-04-10 16:28:59 -0700513 }
514 return 0;
515 }
516
517 /* Report error for everything else */
Jérôme Glissef88a1e92018-04-10 16:29:06 -0700518 *pfn = range->values[HMM_PFN_ERROR];
Jérôme Glisse53f5c3f2018-04-10 16:28:59 -0700519 return -EFAULT;
Jérôme Glisse73231612019-05-13 17:19:58 -0700520 } else {
521 cpu_flags = pte_to_hmm_pfn_flags(range, pte);
522 hmm_pte_need_fault(hmm_vma_walk, orig_pfn, cpu_flags,
523 &fault, &write_fault);
Jérôme Glisse53f5c3f2018-04-10 16:28:59 -0700524 }
525
Jérôme Glisse2aee09d2018-04-10 16:29:02 -0700526 if (fault || write_fault)
Jérôme Glisse53f5c3f2018-04-10 16:28:59 -0700527 goto fault;
528
Jérôme Glisse992de9a2019-05-13 17:20:21 -0700529 if (pte_devmap(pte)) {
530 hmm_vma_walk->pgmap = get_dev_pagemap(pte_pfn(pte),
531 hmm_vma_walk->pgmap);
532 if (unlikely(!hmm_vma_walk->pgmap))
533 return -EBUSY;
534 } else if (IS_ENABLED(CONFIG_ARCH_HAS_PTE_SPECIAL) && pte_special(pte)) {
Ralph Campbellac541f22019-10-23 12:55:14 -0700535 if (!is_zero_pfn(pte_pfn(pte))) {
536 *pfn = range->values[HMM_PFN_SPECIAL];
537 return -EFAULT;
538 }
539 /*
540 * Since each architecture defines a struct page for the zero
541 * page, just fall through and treat it like a normal page.
542 */
Jérôme Glisse992de9a2019-05-13 17:20:21 -0700543 }
544
Jérôme Glisse391aab12019-05-13 17:20:31 -0700545 *pfn = hmm_device_entry_from_pfn(range, pte_pfn(pte)) | cpu_flags;
Jérôme Glisse53f5c3f2018-04-10 16:28:59 -0700546 return 0;
547
548fault:
Jérôme Glisse992de9a2019-05-13 17:20:21 -0700549 if (hmm_vma_walk->pgmap) {
550 put_dev_pagemap(hmm_vma_walk->pgmap);
551 hmm_vma_walk->pgmap = NULL;
552 }
Jérôme Glisse53f5c3f2018-04-10 16:28:59 -0700553 pte_unmap(ptep);
554 /* Fault any virtual address we were asked to fault */
Jérôme Glisse2aee09d2018-04-10 16:29:02 -0700555 return hmm_vma_walk_hole_(addr, end, fault, write_fault, walk);
Jérôme Glisse53f5c3f2018-04-10 16:28:59 -0700556}
557
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700558static int hmm_vma_walk_pmd(pmd_t *pmdp,
559 unsigned long start,
560 unsigned long end,
561 struct mm_walk *walk)
562{
Jérôme Glisse74eee182017-09-08 16:11:35 -0700563 struct hmm_vma_walk *hmm_vma_walk = walk->private;
564 struct hmm_range *range = hmm_vma_walk->range;
Jérôme Glisseff05c0c2018-04-10 16:28:38 -0700565 uint64_t *pfns = range->pfns;
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700566 unsigned long addr = start, i;
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700567 pte_t *ptep;
Jérôme Glissed08faca2018-10-30 15:04:20 -0700568 pmd_t pmd;
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700569
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700570again:
Jérôme Glissed08faca2018-10-30 15:04:20 -0700571 pmd = READ_ONCE(*pmdp);
572 if (pmd_none(pmd))
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700573 return hmm_vma_walk_hole(start, end, walk);
574
Jérôme Glissed08faca2018-10-30 15:04:20 -0700575 if (thp_migration_supported() && is_pmd_migration_entry(pmd)) {
576 bool fault, write_fault;
577 unsigned long npages;
578 uint64_t *pfns;
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700579
Jérôme Glissed08faca2018-10-30 15:04:20 -0700580 i = (addr - range->start) >> PAGE_SHIFT;
581 npages = (end - addr) >> PAGE_SHIFT;
582 pfns = &range->pfns[i];
583
584 hmm_range_need_fault(hmm_vma_walk, pfns, npages,
585 0, &fault, &write_fault);
586 if (fault || write_fault) {
587 hmm_vma_walk->last = addr;
Ralph Campbelld2e8d552019-07-25 17:56:45 -0700588 pmd_migration_entry_wait(walk->mm, pmdp);
Jérôme Glisse73231612019-05-13 17:19:58 -0700589 return -EBUSY;
Jérôme Glissed08faca2018-10-30 15:04:20 -0700590 }
591 return 0;
592 } else if (!pmd_present(pmd))
593 return hmm_pfns_bad(start, end, walk);
594
595 if (pmd_devmap(pmd) || pmd_trans_huge(pmd)) {
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700596 /*
Ralph Campbelld2e8d552019-07-25 17:56:45 -0700597 * No need to take pmd_lock here, even if some other thread
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700598 * is splitting the huge pmd we will get that event through
599 * mmu_notifier callback.
600 *
Ralph Campbelld2e8d552019-07-25 17:56:45 -0700601 * So just read pmd value and check again it's a transparent
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700602 * huge or device mapping one and compute corresponding pfn
603 * values.
604 */
605 pmd = pmd_read_atomic(pmdp);
606 barrier();
607 if (!pmd_devmap(pmd) && !pmd_trans_huge(pmd))
608 goto again;
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700609
Jérôme Glissed08faca2018-10-30 15:04:20 -0700610 i = (addr - range->start) >> PAGE_SHIFT;
Jérôme Glisse53f5c3f2018-04-10 16:28:59 -0700611 return hmm_vma_handle_pmd(walk, addr, end, &pfns[i], pmd);
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700612 }
613
Jérôme Glissed08faca2018-10-30 15:04:20 -0700614 /*
Ralph Campbelld2e8d552019-07-25 17:56:45 -0700615 * We have handled all the valid cases above ie either none, migration,
Jérôme Glissed08faca2018-10-30 15:04:20 -0700616 * huge or transparent huge. At this point either it is a valid pmd
617 * entry pointing to pte directory or it is a bad pmd that will not
618 * recover.
619 */
620 if (pmd_bad(pmd))
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700621 return hmm_pfns_bad(start, end, walk);
622
623 ptep = pte_offset_map(pmdp, addr);
Jérôme Glissed08faca2018-10-30 15:04:20 -0700624 i = (addr - range->start) >> PAGE_SHIFT;
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700625 for (; addr < end; addr += PAGE_SIZE, ptep++, i++) {
Jérôme Glisse53f5c3f2018-04-10 16:28:59 -0700626 int r;
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700627
Jérôme Glisse53f5c3f2018-04-10 16:28:59 -0700628 r = hmm_vma_handle_pte(walk, addr, end, pmdp, ptep, &pfns[i]);
629 if (r) {
630 /* hmm_vma_handle_pte() did unmap pte directory */
631 hmm_vma_walk->last = addr;
632 return r;
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700633 }
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700634 }
Jérôme Glisse992de9a2019-05-13 17:20:21 -0700635 if (hmm_vma_walk->pgmap) {
636 /*
637 * We do put_dev_pagemap() here and not in hmm_vma_handle_pte()
638 * so that we can leverage get_dev_pagemap() optimization which
639 * will not re-take a reference on a pgmap if we already have
640 * one.
641 */
642 put_dev_pagemap(hmm_vma_walk->pgmap);
643 hmm_vma_walk->pgmap = NULL;
644 }
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700645 pte_unmap(ptep - 1);
646
Jérôme Glisse53f5c3f2018-04-10 16:28:59 -0700647 hmm_vma_walk->last = addr;
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700648 return 0;
649}
650
Christoph Hellwigf0b3c452019-08-06 19:05:48 +0300651#if defined(CONFIG_ARCH_HAS_PTE_DEVMAP) && \
652 defined(CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD)
653static inline uint64_t pud_to_hmm_pfn_flags(struct hmm_range *range, pud_t pud)
654{
655 if (!pud_present(pud))
656 return 0;
657 return pud_write(pud) ? range->flags[HMM_PFN_VALID] |
658 range->flags[HMM_PFN_WRITE] :
659 range->flags[HMM_PFN_VALID];
660}
661
662static int hmm_vma_walk_pud(pud_t *pudp, unsigned long start, unsigned long end,
663 struct mm_walk *walk)
Jérôme Glisse992de9a2019-05-13 17:20:21 -0700664{
665 struct hmm_vma_walk *hmm_vma_walk = walk->private;
666 struct hmm_range *range = hmm_vma_walk->range;
667 unsigned long addr = start, next;
668 pmd_t *pmdp;
669 pud_t pud;
670 int ret;
671
672again:
673 pud = READ_ONCE(*pudp);
674 if (pud_none(pud))
675 return hmm_vma_walk_hole(start, end, walk);
676
677 if (pud_huge(pud) && pud_devmap(pud)) {
678 unsigned long i, npages, pfn;
679 uint64_t *pfns, cpu_flags;
680 bool fault, write_fault;
681
682 if (!pud_present(pud))
683 return hmm_vma_walk_hole(start, end, walk);
684
685 i = (addr - range->start) >> PAGE_SHIFT;
686 npages = (end - addr) >> PAGE_SHIFT;
687 pfns = &range->pfns[i];
688
689 cpu_flags = pud_to_hmm_pfn_flags(range, pud);
690 hmm_range_need_fault(hmm_vma_walk, pfns, npages,
691 cpu_flags, &fault, &write_fault);
692 if (fault || write_fault)
693 return hmm_vma_walk_hole_(addr, end, fault,
694 write_fault, walk);
695
Jérôme Glisse992de9a2019-05-13 17:20:21 -0700696 pfn = pud_pfn(pud) + ((addr & ~PUD_MASK) >> PAGE_SHIFT);
697 for (i = 0; i < npages; ++i, ++pfn) {
698 hmm_vma_walk->pgmap = get_dev_pagemap(pfn,
699 hmm_vma_walk->pgmap);
700 if (unlikely(!hmm_vma_walk->pgmap))
701 return -EBUSY;
Jérôme Glisse391aab12019-05-13 17:20:31 -0700702 pfns[i] = hmm_device_entry_from_pfn(range, pfn) |
703 cpu_flags;
Jérôme Glisse992de9a2019-05-13 17:20:21 -0700704 }
705 if (hmm_vma_walk->pgmap) {
706 put_dev_pagemap(hmm_vma_walk->pgmap);
707 hmm_vma_walk->pgmap = NULL;
708 }
709 hmm_vma_walk->last = end;
710 return 0;
Jérôme Glisse992de9a2019-05-13 17:20:21 -0700711 }
712
713 split_huge_pud(walk->vma, pudp, addr);
714 if (pud_none(*pudp))
715 goto again;
716
717 pmdp = pmd_offset(pudp, addr);
718 do {
719 next = pmd_addr_end(addr, end);
720 ret = hmm_vma_walk_pmd(pmdp, addr, next, walk);
721 if (ret)
722 return ret;
723 } while (pmdp++, addr = next, addr != end);
724
725 return 0;
726}
Christoph Hellwigf0b3c452019-08-06 19:05:48 +0300727#else
728#define hmm_vma_walk_pud NULL
729#endif
Jérôme Glisse992de9a2019-05-13 17:20:21 -0700730
Christoph Hellwig251bbe52019-08-06 19:05:50 +0300731#ifdef CONFIG_HUGETLB_PAGE
Jérôme Glisse63d50662019-05-13 17:20:18 -0700732static int hmm_vma_walk_hugetlb_entry(pte_t *pte, unsigned long hmask,
733 unsigned long start, unsigned long end,
734 struct mm_walk *walk)
735{
Christoph Hellwig05c23af2019-08-06 19:05:46 +0300736 unsigned long addr = start, i, pfn;
Jérôme Glisse63d50662019-05-13 17:20:18 -0700737 struct hmm_vma_walk *hmm_vma_walk = walk->private;
738 struct hmm_range *range = hmm_vma_walk->range;
739 struct vm_area_struct *vma = walk->vma;
Jérôme Glisse63d50662019-05-13 17:20:18 -0700740 uint64_t orig_pfn, cpu_flags;
741 bool fault, write_fault;
742 spinlock_t *ptl;
743 pte_t entry;
744 int ret = 0;
745
Ralph Campbelld2e8d552019-07-25 17:56:45 -0700746 ptl = huge_pte_lock(hstate_vma(vma), walk->mm, pte);
Jérôme Glisse63d50662019-05-13 17:20:18 -0700747 entry = huge_ptep_get(pte);
748
Christoph Hellwig7f082632019-08-06 19:05:45 +0300749 i = (start - range->start) >> PAGE_SHIFT;
Jérôme Glisse63d50662019-05-13 17:20:18 -0700750 orig_pfn = range->pfns[i];
751 range->pfns[i] = range->values[HMM_PFN_NONE];
752 cpu_flags = pte_to_hmm_pfn_flags(range, entry);
753 fault = write_fault = false;
754 hmm_pte_need_fault(hmm_vma_walk, orig_pfn, cpu_flags,
755 &fault, &write_fault);
756 if (fault || write_fault) {
757 ret = -ENOENT;
758 goto unlock;
759 }
760
Christoph Hellwig05c23af2019-08-06 19:05:46 +0300761 pfn = pte_pfn(entry) + ((start & ~hmask) >> PAGE_SHIFT);
Christoph Hellwig7f082632019-08-06 19:05:45 +0300762 for (; addr < end; addr += PAGE_SIZE, i++, pfn++)
Jérôme Glisse391aab12019-05-13 17:20:31 -0700763 range->pfns[i] = hmm_device_entry_from_pfn(range, pfn) |
764 cpu_flags;
Jérôme Glisse63d50662019-05-13 17:20:18 -0700765 hmm_vma_walk->last = end;
766
767unlock:
768 spin_unlock(ptl);
769
770 if (ret == -ENOENT)
771 return hmm_vma_walk_hole_(addr, end, fault, write_fault, walk);
772
773 return ret;
Jérôme Glisse63d50662019-05-13 17:20:18 -0700774}
Christoph Hellwig251bbe52019-08-06 19:05:50 +0300775#else
776#define hmm_vma_walk_hugetlb_entry NULL
777#endif /* CONFIG_HUGETLB_PAGE */
Jérôme Glisse63d50662019-05-13 17:20:18 -0700778
Jérôme Glissef88a1e92018-04-10 16:29:06 -0700779static void hmm_pfns_clear(struct hmm_range *range,
780 uint64_t *pfns,
Jérôme Glisse33cd47d2018-04-10 16:28:54 -0700781 unsigned long addr,
782 unsigned long end)
783{
784 for (; addr < end; addr += PAGE_SIZE, pfns++)
Jérôme Glissef88a1e92018-04-10 16:29:06 -0700785 *pfns = range->values[HMM_PFN_NONE];
Jérôme Glisse33cd47d2018-04-10 16:28:54 -0700786}
787
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700788/*
Jérôme Glissea3e0d412019-05-13 17:20:01 -0700789 * hmm_range_register() - start tracking change to CPU page table over a range
Jérôme Glisse25f23a02019-05-13 17:19:55 -0700790 * @range: range
Jérôme Glissea3e0d412019-05-13 17:20:01 -0700791 * @mm: the mm struct for the range of virtual address
Christoph Hellwigfac555a2019-08-06 19:05:44 +0300792 *
Ralph Campbelld2e8d552019-07-25 17:56:45 -0700793 * Return: 0 on success, -EFAULT if the address space is no longer valid
Jérôme Glisse25f23a02019-05-13 17:19:55 -0700794 *
Jérôme Glissea3e0d412019-05-13 17:20:01 -0700795 * Track updates to the CPU page table see include/linux/hmm.h
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700796 */
Christoph Hellwigfac555a2019-08-06 19:05:44 +0300797int hmm_range_register(struct hmm_range *range, struct hmm_mirror *mirror)
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700798{
Jason Gunthorpee36acfe2019-05-23 09:41:19 -0300799 struct hmm *hmm = mirror->hmm;
Jason Gunthorpe5a136b42019-06-07 12:10:33 -0300800 unsigned long flags;
Jérôme Glisse63d50662019-05-13 17:20:18 -0700801
Jérôme Glissea3e0d412019-05-13 17:20:01 -0700802 range->valid = false;
Jérôme Glisse704f3f22019-05-13 17:19:48 -0700803 range->hmm = NULL;
804
Christoph Hellwig7f082632019-08-06 19:05:45 +0300805 if ((range->start & (PAGE_SIZE - 1)) || (range->end & (PAGE_SIZE - 1)))
Jérôme Glisse63d50662019-05-13 17:20:18 -0700806 return -EINVAL;
Christoph Hellwigfac555a2019-08-06 19:05:44 +0300807 if (range->start >= range->end)
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700808 return -EINVAL;
809
Jason Gunthorpe47f24592019-05-23 11:08:28 -0300810 /* Prevent hmm_release() from running while the range is valid */
Jason Gunthorpec7d8b782019-08-06 20:15:42 -0300811 if (!mmget_not_zero(hmm->mmu_notifier.mm))
Jérôme Glissea3e0d412019-05-13 17:20:01 -0700812 return -EFAULT;
Jérôme Glisse704f3f22019-05-13 17:19:48 -0700813
Ralph Campbell085ea252019-05-06 16:29:39 -0700814 /* Initialize range to track CPU page table updates. */
Jason Gunthorpe5a136b42019-06-07 12:10:33 -0300815 spin_lock_irqsave(&hmm->ranges_lock, flags);
Jérôme Glisse86586a42018-04-10 16:28:34 -0700816
Ralph Campbell085ea252019-05-06 16:29:39 -0700817 range->hmm = hmm;
Jason Gunthorpe157816f2019-05-23 11:43:43 -0300818 list_add(&range->list, &hmm->ranges);
Jérôme Glisse74eee182017-09-08 16:11:35 -0700819
Jérôme Glisse704f3f22019-05-13 17:19:48 -0700820 /*
Jérôme Glissea3e0d412019-05-13 17:20:01 -0700821 * If there are any concurrent notifiers we have to wait for them for
822 * the range to be valid (see hmm_range_wait_until_valid()).
Jérôme Glisse704f3f22019-05-13 17:19:48 -0700823 */
Ralph Campbell085ea252019-05-06 16:29:39 -0700824 if (!hmm->notifiers)
Jérôme Glissea3e0d412019-05-13 17:20:01 -0700825 range->valid = true;
Jason Gunthorpe5a136b42019-06-07 12:10:33 -0300826 spin_unlock_irqrestore(&hmm->ranges_lock, flags);
Jérôme Glissea3e0d412019-05-13 17:20:01 -0700827
828 return 0;
829}
830EXPORT_SYMBOL(hmm_range_register);
831
832/*
833 * hmm_range_unregister() - stop tracking change to CPU page table over a range
834 * @range: range
835 *
836 * Range struct is used to track updates to the CPU page table after a call to
837 * hmm_range_register(). See include/linux/hmm.h for how to use it.
838 */
839void hmm_range_unregister(struct hmm_range *range)
840{
Ralph Campbell085ea252019-05-06 16:29:39 -0700841 struct hmm *hmm = range->hmm;
Jason Gunthorpe5a136b42019-06-07 12:10:33 -0300842 unsigned long flags;
Jérôme Glissea3e0d412019-05-13 17:20:01 -0700843
Jason Gunthorpe5a136b42019-06-07 12:10:33 -0300844 spin_lock_irqsave(&hmm->ranges_lock, flags);
Jason Gunthorpe47f24592019-05-23 11:08:28 -0300845 list_del_init(&range->list);
Jason Gunthorpe5a136b42019-06-07 12:10:33 -0300846 spin_unlock_irqrestore(&hmm->ranges_lock, flags);
Jérôme Glissea3e0d412019-05-13 17:20:01 -0700847
848 /* Drop reference taken by hmm_range_register() */
Jason Gunthorpec7d8b782019-08-06 20:15:42 -0300849 mmput(hmm->mmu_notifier.mm);
Jason Gunthorpe2dcc3eb2019-05-23 11:40:24 -0300850
851 /*
852 * The range is now invalid and the ref on the hmm is dropped, so
853 * poison the pointer. Leave other fields in place, for the caller's
854 * use.
855 */
Jérôme Glissea3e0d412019-05-13 17:20:01 -0700856 range->valid = false;
Jason Gunthorpe2dcc3eb2019-05-23 11:40:24 -0300857 memset(&range->hmm, POISON_INUSE, sizeof(range->hmm));
Jérôme Glissea3e0d412019-05-13 17:20:01 -0700858}
859EXPORT_SYMBOL(hmm_range_unregister);
860
Christoph Hellwig7b86ac32019-08-28 16:19:54 +0200861static const struct mm_walk_ops hmm_walk_ops = {
862 .pud_entry = hmm_vma_walk_pud,
863 .pmd_entry = hmm_vma_walk_pmd,
864 .pte_hole = hmm_vma_walk_hole,
865 .hugetlb_entry = hmm_vma_walk_hugetlb_entry,
866};
867
Christoph Hellwig9a4903e2019-07-25 17:56:46 -0700868/**
869 * hmm_range_fault - try to fault some address in a virtual address range
870 * @range: range being faulted
871 * @flags: HMM_FAULT_* flags
Jérôme Glisse73231612019-05-13 17:19:58 -0700872 *
Christoph Hellwig9a4903e2019-07-25 17:56:46 -0700873 * Return: the number of valid pages in range->pfns[] (from range start
874 * address), which may be zero. On error one of the following status codes
875 * can be returned:
876 *
877 * -EINVAL: Invalid arguments or mm or virtual address is in an invalid vma
878 * (e.g., device file vma).
879 * -ENOMEM: Out of memory.
880 * -EPERM: Invalid permission (e.g., asking for write and range is read
881 * only).
882 * -EAGAIN: A page fault needs to be retried and mmap_sem was dropped.
883 * -EBUSY: The range has been invalidated and the caller needs to wait for
884 * the invalidation to finish.
885 * -EFAULT: Invalid (i.e., either no valid vma or it is illegal to access
886 * that range) number of valid pages in range->pfns[] (from
887 * range start address).
Jérôme Glisse74eee182017-09-08 16:11:35 -0700888 *
889 * This is similar to a regular CPU page fault except that it will not trigger
Jérôme Glisse73231612019-05-13 17:19:58 -0700890 * any memory migration if the memory being faulted is not accessible by CPUs
891 * and caller does not ask for migration.
Jérôme Glisse74eee182017-09-08 16:11:35 -0700892 *
Jérôme Glisseff05c0c2018-04-10 16:28:38 -0700893 * On error, for one virtual address in the range, the function will mark the
894 * corresponding HMM pfn entry with an error flag.
Jérôme Glisse74eee182017-09-08 16:11:35 -0700895 */
Christoph Hellwig9a4903e2019-07-25 17:56:46 -0700896long hmm_range_fault(struct hmm_range *range, unsigned int flags)
Jérôme Glisse74eee182017-09-08 16:11:35 -0700897{
Jérôme Glisse63d50662019-05-13 17:20:18 -0700898 const unsigned long device_vma = VM_IO | VM_PFNMAP | VM_MIXEDMAP;
Jérôme Glissea3e0d412019-05-13 17:20:01 -0700899 unsigned long start = range->start, end;
Jérôme Glisse74eee182017-09-08 16:11:35 -0700900 struct hmm_vma_walk hmm_vma_walk;
Jérôme Glissea3e0d412019-05-13 17:20:01 -0700901 struct hmm *hmm = range->hmm;
902 struct vm_area_struct *vma;
Jérôme Glisse74eee182017-09-08 16:11:35 -0700903 int ret;
904
Jason Gunthorpec7d8b782019-08-06 20:15:42 -0300905 lockdep_assert_held(&hmm->mmu_notifier.mm->mmap_sem);
Jérôme Glisse74eee182017-09-08 16:11:35 -0700906
907 do {
Jérôme Glissea3e0d412019-05-13 17:20:01 -0700908 /* If range is no longer valid force retry. */
Christoph Hellwig2bcbeae2019-07-24 08:52:52 +0200909 if (!range->valid)
910 return -EBUSY;
Jérôme Glisse74eee182017-09-08 16:11:35 -0700911
Jason Gunthorpec7d8b782019-08-06 20:15:42 -0300912 vma = find_vma(hmm->mmu_notifier.mm, start);
Jérôme Glisse63d50662019-05-13 17:20:18 -0700913 if (vma == NULL || (vma->vm_flags & device_vma))
Jérôme Glissea3e0d412019-05-13 17:20:01 -0700914 return -EFAULT;
Jérôme Glisse74eee182017-09-08 16:11:35 -0700915
Jérôme Glissea3e0d412019-05-13 17:20:01 -0700916 if (!(vma->vm_flags & VM_READ)) {
917 /*
918 * If vma do not allow read access, then assume that it
919 * does not allow write access, either. HMM does not
920 * support architecture that allow write without read.
921 */
922 hmm_pfns_clear(range, range->pfns,
923 range->start, range->end);
924 return -EPERM;
925 }
926
Jérôme Glisse992de9a2019-05-13 17:20:21 -0700927 hmm_vma_walk.pgmap = NULL;
Jérôme Glissea3e0d412019-05-13 17:20:01 -0700928 hmm_vma_walk.last = start;
Christoph Hellwig9a4903e2019-07-25 17:56:46 -0700929 hmm_vma_walk.flags = flags;
Jérôme Glissea3e0d412019-05-13 17:20:01 -0700930 hmm_vma_walk.range = range;
Jérôme Glissea3e0d412019-05-13 17:20:01 -0700931 end = min(range->end, vma->vm_end);
932
Christoph Hellwig7b86ac32019-08-28 16:19:54 +0200933 walk_page_range(vma->vm_mm, start, end, &hmm_walk_ops,
934 &hmm_vma_walk);
Jérôme Glissea3e0d412019-05-13 17:20:01 -0700935
936 do {
Christoph Hellwig7b86ac32019-08-28 16:19:54 +0200937 ret = walk_page_range(vma->vm_mm, start, end,
938 &hmm_walk_ops, &hmm_vma_walk);
Jérôme Glissea3e0d412019-05-13 17:20:01 -0700939 start = hmm_vma_walk.last;
940
941 /* Keep trying while the range is valid. */
942 } while (ret == -EBUSY && range->valid);
943
944 if (ret) {
945 unsigned long i;
946
947 i = (hmm_vma_walk.last - range->start) >> PAGE_SHIFT;
948 hmm_pfns_clear(range, &range->pfns[i],
949 hmm_vma_walk.last, range->end);
950 return ret;
951 }
952 start = end;
953
954 } while (start < range->end);
Jérôme Glisse704f3f22019-05-13 17:19:48 -0700955
Jérôme Glisse73231612019-05-13 17:19:58 -0700956 return (hmm_vma_walk.last - range->start) >> PAGE_SHIFT;
Jérôme Glisse74eee182017-09-08 16:11:35 -0700957}
Jérôme Glisse73231612019-05-13 17:19:58 -0700958EXPORT_SYMBOL(hmm_range_fault);
Jérôme Glisse55c0ece2019-05-13 17:20:28 -0700959
960/**
Christoph Hellwig9a4903e2019-07-25 17:56:46 -0700961 * hmm_range_dma_map - hmm_range_fault() and dma map page all in one.
962 * @range: range being faulted
963 * @device: device to map page to
964 * @daddrs: array of dma addresses for the mapped pages
965 * @flags: HMM_FAULT_*
Jérôme Glisse55c0ece2019-05-13 17:20:28 -0700966 *
Christoph Hellwig9a4903e2019-07-25 17:56:46 -0700967 * Return: the number of pages mapped on success (including zero), or any
968 * status return from hmm_range_fault() otherwise.
Jérôme Glisse55c0ece2019-05-13 17:20:28 -0700969 */
Christoph Hellwig9a4903e2019-07-25 17:56:46 -0700970long hmm_range_dma_map(struct hmm_range *range, struct device *device,
971 dma_addr_t *daddrs, unsigned int flags)
Jérôme Glisse55c0ece2019-05-13 17:20:28 -0700972{
973 unsigned long i, npages, mapped;
974 long ret;
975
Christoph Hellwig9a4903e2019-07-25 17:56:46 -0700976 ret = hmm_range_fault(range, flags);
Jérôme Glisse55c0ece2019-05-13 17:20:28 -0700977 if (ret <= 0)
978 return ret ? ret : -EBUSY;
979
980 npages = (range->end - range->start) >> PAGE_SHIFT;
981 for (i = 0, mapped = 0; i < npages; ++i) {
982 enum dma_data_direction dir = DMA_TO_DEVICE;
983 struct page *page;
984
985 /*
986 * FIXME need to update DMA API to provide invalid DMA address
987 * value instead of a function to test dma address value. This
988 * would remove lot of dumb code duplicated accross many arch.
989 *
990 * For now setting it to 0 here is good enough as the pfns[]
991 * value is what is use to check what is valid and what isn't.
992 */
993 daddrs[i] = 0;
994
Jérôme Glisse391aab12019-05-13 17:20:31 -0700995 page = hmm_device_entry_to_page(range, range->pfns[i]);
Jérôme Glisse55c0ece2019-05-13 17:20:28 -0700996 if (page == NULL)
997 continue;
998
999 /* Check if range is being invalidated */
1000 if (!range->valid) {
1001 ret = -EBUSY;
1002 goto unmap;
1003 }
1004
1005 /* If it is read and write than map bi-directional. */
1006 if (range->pfns[i] & range->flags[HMM_PFN_WRITE])
1007 dir = DMA_BIDIRECTIONAL;
1008
1009 daddrs[i] = dma_map_page(device, page, 0, PAGE_SIZE, dir);
1010 if (dma_mapping_error(device, daddrs[i])) {
1011 ret = -EFAULT;
1012 goto unmap;
1013 }
1014
1015 mapped++;
1016 }
1017
1018 return mapped;
1019
1020unmap:
1021 for (npages = i, i = 0; (i < npages) && mapped; ++i) {
1022 enum dma_data_direction dir = DMA_TO_DEVICE;
1023 struct page *page;
1024
Jérôme Glisse391aab12019-05-13 17:20:31 -07001025 page = hmm_device_entry_to_page(range, range->pfns[i]);
Jérôme Glisse55c0ece2019-05-13 17:20:28 -07001026 if (page == NULL)
1027 continue;
1028
1029 if (dma_mapping_error(device, daddrs[i]))
1030 continue;
1031
1032 /* If it is read and write than map bi-directional. */
1033 if (range->pfns[i] & range->flags[HMM_PFN_WRITE])
1034 dir = DMA_BIDIRECTIONAL;
1035
1036 dma_unmap_page(device, daddrs[i], PAGE_SIZE, dir);
1037 mapped--;
1038 }
1039
1040 return ret;
1041}
1042EXPORT_SYMBOL(hmm_range_dma_map);
1043
1044/**
1045 * hmm_range_dma_unmap() - unmap range of that was map with hmm_range_dma_map()
1046 * @range: range being unmapped
Jérôme Glisse55c0ece2019-05-13 17:20:28 -07001047 * @device: device against which dma map was done
1048 * @daddrs: dma address of mapped pages
1049 * @dirty: dirty page if it had the write flag set
Ralph Campbell085ea252019-05-06 16:29:39 -07001050 * Return: number of page unmapped on success, -EINVAL otherwise
Jérôme Glisse55c0ece2019-05-13 17:20:28 -07001051 *
1052 * Note that caller MUST abide by mmu notifier or use HMM mirror and abide
1053 * to the sync_cpu_device_pagetables() callback so that it is safe here to
1054 * call set_page_dirty(). Caller must also take appropriate locks to avoid
1055 * concurrent mmu notifier or sync_cpu_device_pagetables() to make progress.
1056 */
1057long hmm_range_dma_unmap(struct hmm_range *range,
Jérôme Glisse55c0ece2019-05-13 17:20:28 -07001058 struct device *device,
1059 dma_addr_t *daddrs,
1060 bool dirty)
1061{
1062 unsigned long i, npages;
1063 long cpages = 0;
1064
1065 /* Sanity check. */
1066 if (range->end <= range->start)
1067 return -EINVAL;
1068 if (!daddrs)
1069 return -EINVAL;
1070 if (!range->pfns)
1071 return -EINVAL;
1072
1073 npages = (range->end - range->start) >> PAGE_SHIFT;
1074 for (i = 0; i < npages; ++i) {
1075 enum dma_data_direction dir = DMA_TO_DEVICE;
1076 struct page *page;
1077
Jérôme Glisse391aab12019-05-13 17:20:31 -07001078 page = hmm_device_entry_to_page(range, range->pfns[i]);
Jérôme Glisse55c0ece2019-05-13 17:20:28 -07001079 if (page == NULL)
1080 continue;
1081
1082 /* If it is read and write than map bi-directional. */
1083 if (range->pfns[i] & range->flags[HMM_PFN_WRITE]) {
1084 dir = DMA_BIDIRECTIONAL;
1085
1086 /*
1087 * See comments in function description on why it is
1088 * safe here to call set_page_dirty()
1089 */
1090 if (dirty)
1091 set_page_dirty(page);
1092 }
1093
1094 /* Unmap and clear pfns/dma address */
1095 dma_unmap_page(device, daddrs[i], PAGE_SIZE, dir);
1096 range->pfns[i] = range->values[HMM_PFN_NONE];
1097 /* FIXME see comments in hmm_vma_dma_map() */
1098 daddrs[i] = 0;
1099 cpages++;
1100 }
1101
1102 return cpages;
1103}
1104EXPORT_SYMBOL(hmm_range_dma_unmap);