blob: 73c8af4827fe87ca521e4b9486e53df6ca0981a2 [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 */
11#include <linux/mm.h>
12#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
29#define PA_SECTION_SIZE (1UL << PA_SECTION_SHIFT)
Jérôme Glisse133ff0e2017-09-08 16:11:23 -070030
Jérôme Glisse6b368cd2017-09-08 16:12:32 -070031#if IS_ENABLED(CONFIG_HMM_MIRROR)
Jérôme Glissec0b12402017-09-08 16:11:27 -070032static const struct mmu_notifier_ops hmm_mmu_notifier_ops;
33
Jérôme Glisse704f3f22019-05-13 17:19:48 -070034/**
35 * hmm_get_or_create - register HMM against an mm (HMM internal)
36 *
37 * @mm: mm struct to attach to
38 * Returns: returns an HMM object, either by referencing the existing
39 * (per-process) object, or by creating a new one.
40 *
41 * This is not intended to be used directly by device drivers. If mm already
42 * has an HMM struct then it get a reference on it and returns it. Otherwise
43 * it allocates an HMM struct, initializes it, associate it with the mm and
44 * returns it.
45 */
46static struct hmm *hmm_get_or_create(struct mm_struct *mm)
47{
Jason Gunthorpe8a9320b2019-05-23 10:24:13 -030048 struct hmm *hmm;
Jérôme Glisse133ff0e2017-09-08 16:11:23 -070049
Jason Gunthorpe8a9320b2019-05-23 10:24:13 -030050 lockdep_assert_held_exclusive(&mm->mmap_sem);
51
52 /* Abuse the page_table_lock to also protect mm->hmm. */
53 spin_lock(&mm->page_table_lock);
54 hmm = mm->hmm;
55 if (mm->hmm && kref_get_unless_zero(&mm->hmm->kref))
56 goto out_unlock;
57 spin_unlock(&mm->page_table_lock);
Jérôme Glissec0b12402017-09-08 16:11:27 -070058
59 hmm = kmalloc(sizeof(*hmm), GFP_KERNEL);
60 if (!hmm)
61 return NULL;
Jérôme Glissea3e0d412019-05-13 17:20:01 -070062 init_waitqueue_head(&hmm->wq);
Jérôme Glissec0b12402017-09-08 16:11:27 -070063 INIT_LIST_HEAD(&hmm->mirrors);
64 init_rwsem(&hmm->mirrors_sem);
Jérôme Glissec0b12402017-09-08 16:11:27 -070065 hmm->mmu_notifier.ops = NULL;
Jérôme Glisseda4c3c72017-09-08 16:11:31 -070066 INIT_LIST_HEAD(&hmm->ranges);
Jérôme Glissea3e0d412019-05-13 17:20:01 -070067 mutex_init(&hmm->lock);
Jérôme Glisse704f3f22019-05-13 17:19:48 -070068 kref_init(&hmm->kref);
Jérôme Glissea3e0d412019-05-13 17:20:01 -070069 hmm->notifiers = 0;
70 hmm->dead = false;
Jérôme Glissec0b12402017-09-08 16:11:27 -070071 hmm->mm = mm;
Jason Gunthorpe8a9320b2019-05-23 10:24:13 -030072
73 hmm->mmu_notifier.ops = &hmm_mmu_notifier_ops;
74 if (__mmu_notifier_register(&hmm->mmu_notifier, mm)) {
75 kfree(hmm);
76 return NULL;
77 }
78
Jason Gunthorpec8a53b22019-05-23 10:36:46 -030079 mmgrab(hmm->mm);
Jérôme Glissec0b12402017-09-08 16:11:27 -070080
Ralph Campbell86a2d592018-10-30 15:04:14 -070081 /*
Jason Gunthorpe8a9320b2019-05-23 10:24:13 -030082 * We hold the exclusive mmap_sem here so we know that mm->hmm is
83 * still NULL or 0 kref, and is safe to update.
Ralph Campbell86a2d592018-10-30 15:04:14 -070084 */
Ralph Campbell86a2d592018-10-30 15:04:14 -070085 spin_lock(&mm->page_table_lock);
Jason Gunthorpe8a9320b2019-05-23 10:24:13 -030086 mm->hmm = hmm;
87
88out_unlock:
Ralph Campbell86a2d592018-10-30 15:04:14 -070089 spin_unlock(&mm->page_table_lock);
Jason Gunthorpe8a9320b2019-05-23 10:24:13 -030090 return hmm;
Jérôme Glisse133ff0e2017-09-08 16:11:23 -070091}
92
Jason Gunthorpe6d7c3cd2019-05-22 16:52:52 -030093static void hmm_free_rcu(struct rcu_head *rcu)
94{
Jason Gunthorpe8a9320b2019-05-23 10:24:13 -030095 struct hmm *hmm = container_of(rcu, struct hmm, rcu);
96
97 mmdrop(hmm->mm);
98 kfree(hmm);
Jason Gunthorpe6d7c3cd2019-05-22 16:52:52 -030099}
100
Jérôme Glisse704f3f22019-05-13 17:19:48 -0700101static void hmm_free(struct kref *kref)
102{
103 struct hmm *hmm = container_of(kref, struct hmm, kref);
Jérôme Glisse704f3f22019-05-13 17:19:48 -0700104
Jason Gunthorpe8a9320b2019-05-23 10:24:13 -0300105 spin_lock(&hmm->mm->page_table_lock);
106 if (hmm->mm->hmm == hmm)
107 hmm->mm->hmm = NULL;
108 spin_unlock(&hmm->mm->page_table_lock);
Jérôme Glisse704f3f22019-05-13 17:19:48 -0700109
Jason Gunthorpe8a9320b2019-05-23 10:24:13 -0300110 mmu_notifier_unregister_no_release(&hmm->mmu_notifier, hmm->mm);
Jason Gunthorpe6d7c3cd2019-05-22 16:52:52 -0300111 mmu_notifier_call_srcu(&hmm->rcu, hmm_free_rcu);
Jérôme Glisse704f3f22019-05-13 17:19:48 -0700112}
113
114static inline void hmm_put(struct hmm *hmm)
115{
116 kref_put(&hmm->kref, hmm_free);
117}
118
Jérôme Glissea3e0d412019-05-13 17:20:01 -0700119static void hmm_release(struct mmu_notifier *mn, struct mm_struct *mm)
Jérôme Glissec0b12402017-09-08 16:11:27 -0700120{
Jason Gunthorpe6d7c3cd2019-05-22 16:52:52 -0300121 struct hmm *hmm = container_of(mn, struct hmm, mmu_notifier);
Jérôme Glissec0b12402017-09-08 16:11:27 -0700122 struct hmm_mirror *mirror;
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700123 struct hmm_range *range;
124
Jason Gunthorpe6d7c3cd2019-05-22 16:52:52 -0300125 /* Bail out if hmm is in the process of being freed */
126 if (!kref_get_unless_zero(&hmm->kref))
127 return;
128
Jérôme Glissea3e0d412019-05-13 17:20:01 -0700129 /* Report this HMM as dying. */
130 hmm->dead = true;
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700131
Jérôme Glissea3e0d412019-05-13 17:20:01 -0700132 /* Wake-up everyone waiting on any range. */
133 mutex_lock(&hmm->lock);
Ralph Campbell085ea252019-05-06 16:29:39 -0700134 list_for_each_entry(range, &hmm->ranges, list)
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700135 range->valid = false;
Jérôme Glissea3e0d412019-05-13 17:20:01 -0700136 wake_up_all(&hmm->wq);
137 mutex_unlock(&hmm->lock);
Ralph Campbelle1401512018-04-10 16:28:19 -0700138
139 down_write(&hmm->mirrors_sem);
140 mirror = list_first_entry_or_null(&hmm->mirrors, struct hmm_mirror,
141 list);
142 while (mirror) {
143 list_del_init(&mirror->list);
144 if (mirror->ops->release) {
145 /*
Ralph Campbell085ea252019-05-06 16:29:39 -0700146 * Drop mirrors_sem so the release callback can wait
147 * on any pending work that might itself trigger a
148 * mmu_notifier callback and thus would deadlock with
149 * us.
Ralph Campbelle1401512018-04-10 16:28:19 -0700150 */
151 up_write(&hmm->mirrors_sem);
152 mirror->ops->release(mirror);
153 down_write(&hmm->mirrors_sem);
154 }
155 mirror = list_first_entry_or_null(&hmm->mirrors,
156 struct hmm_mirror, list);
157 }
158 up_write(&hmm->mirrors_sem);
Jérôme Glisse704f3f22019-05-13 17:19:48 -0700159
160 hmm_put(hmm);
Ralph Campbelle1401512018-04-10 16:28:19 -0700161}
162
Michal Hocko93065ac2018-08-21 21:52:33 -0700163static int hmm_invalidate_range_start(struct mmu_notifier *mn,
Jérôme Glissea3e0d412019-05-13 17:20:01 -0700164 const struct mmu_notifier_range *nrange)
Jérôme Glissec0b12402017-09-08 16:11:27 -0700165{
Jason Gunthorpe6d7c3cd2019-05-22 16:52:52 -0300166 struct hmm *hmm = container_of(mn, struct hmm, mmu_notifier);
Jérôme Glissea3e0d412019-05-13 17:20:01 -0700167 struct hmm_mirror *mirror;
Jérôme Glisseec131b22018-10-30 15:04:28 -0700168 struct hmm_update update;
Jérôme Glissea3e0d412019-05-13 17:20:01 -0700169 struct hmm_range *range;
170 int ret = 0;
Jérôme Glissec0b12402017-09-08 16:11:27 -0700171
Jason Gunthorpe6d7c3cd2019-05-22 16:52:52 -0300172 if (!kref_get_unless_zero(&hmm->kref))
173 return 0;
Jérôme Glissec0b12402017-09-08 16:11:27 -0700174
Jérôme Glissea3e0d412019-05-13 17:20:01 -0700175 update.start = nrange->start;
176 update.end = nrange->end;
Jérôme Glisseec131b22018-10-30 15:04:28 -0700177 update.event = HMM_UPDATE_INVALIDATE;
Jérôme Glissedfcd6662019-05-13 17:20:38 -0700178 update.blockable = mmu_notifier_range_blockable(nrange);
Jérôme Glissea3e0d412019-05-13 17:20:01 -0700179
Jérôme Glissedfcd6662019-05-13 17:20:38 -0700180 if (mmu_notifier_range_blockable(nrange))
Jérôme Glissea3e0d412019-05-13 17:20:01 -0700181 mutex_lock(&hmm->lock);
182 else if (!mutex_trylock(&hmm->lock)) {
183 ret = -EAGAIN;
184 goto out;
185 }
186 hmm->notifiers++;
187 list_for_each_entry(range, &hmm->ranges, list) {
188 if (update.end < range->start || update.start >= range->end)
189 continue;
190
191 range->valid = false;
192 }
193 mutex_unlock(&hmm->lock);
194
Jérôme Glissedfcd6662019-05-13 17:20:38 -0700195 if (mmu_notifier_range_blockable(nrange))
Jérôme Glissea3e0d412019-05-13 17:20:01 -0700196 down_read(&hmm->mirrors_sem);
197 else if (!down_read_trylock(&hmm->mirrors_sem)) {
198 ret = -EAGAIN;
199 goto out;
200 }
201 list_for_each_entry(mirror, &hmm->mirrors, list) {
202 int ret;
203
204 ret = mirror->ops->sync_cpu_device_pagetables(mirror, &update);
Ralph Campbell085ea252019-05-06 16:29:39 -0700205 if (!update.blockable && ret == -EAGAIN)
206 break;
Jérôme Glissea3e0d412019-05-13 17:20:01 -0700207 }
208 up_read(&hmm->mirrors_sem);
209
210out:
Jérôme Glisse704f3f22019-05-13 17:19:48 -0700211 hmm_put(hmm);
212 return ret;
Jérôme Glissec0b12402017-09-08 16:11:27 -0700213}
214
215static void hmm_invalidate_range_end(struct mmu_notifier *mn,
Jérôme Glissea3e0d412019-05-13 17:20:01 -0700216 const struct mmu_notifier_range *nrange)
Jérôme Glissec0b12402017-09-08 16:11:27 -0700217{
Jason Gunthorpe6d7c3cd2019-05-22 16:52:52 -0300218 struct hmm *hmm = container_of(mn, struct hmm, mmu_notifier);
Jérôme Glissec0b12402017-09-08 16:11:27 -0700219
Jason Gunthorpe6d7c3cd2019-05-22 16:52:52 -0300220 if (!kref_get_unless_zero(&hmm->kref))
221 return;
Jérôme Glissec0b12402017-09-08 16:11:27 -0700222
Jérôme Glissea3e0d412019-05-13 17:20:01 -0700223 mutex_lock(&hmm->lock);
224 hmm->notifiers--;
225 if (!hmm->notifiers) {
226 struct hmm_range *range;
227
228 list_for_each_entry(range, &hmm->ranges, list) {
229 if (range->valid)
230 continue;
231 range->valid = true;
232 }
233 wake_up_all(&hmm->wq);
234 }
235 mutex_unlock(&hmm->lock);
236
Jérôme Glisse704f3f22019-05-13 17:19:48 -0700237 hmm_put(hmm);
Jérôme Glissec0b12402017-09-08 16:11:27 -0700238}
239
240static const struct mmu_notifier_ops hmm_mmu_notifier_ops = {
Ralph Campbelle1401512018-04-10 16:28:19 -0700241 .release = hmm_release,
Jérôme Glissec0b12402017-09-08 16:11:27 -0700242 .invalidate_range_start = hmm_invalidate_range_start,
243 .invalidate_range_end = hmm_invalidate_range_end,
244};
245
246/*
247 * hmm_mirror_register() - register a mirror against an mm
248 *
249 * @mirror: new mirror struct to register
250 * @mm: mm to register against
Ralph Campbell085ea252019-05-06 16:29:39 -0700251 * Return: 0 on success, -ENOMEM if no memory, -EINVAL if invalid arguments
Jérôme Glissec0b12402017-09-08 16:11:27 -0700252 *
253 * To start mirroring a process address space, the device driver must register
254 * an HMM mirror struct.
255 *
256 * THE mm->mmap_sem MUST BE HELD IN WRITE MODE !
257 */
258int hmm_mirror_register(struct hmm_mirror *mirror, struct mm_struct *mm)
259{
260 /* Sanity check */
261 if (!mm || !mirror || !mirror->ops)
262 return -EINVAL;
263
Jérôme Glisse704f3f22019-05-13 17:19:48 -0700264 mirror->hmm = hmm_get_or_create(mm);
Jérôme Glissec0b12402017-09-08 16:11:27 -0700265 if (!mirror->hmm)
266 return -ENOMEM;
267
268 down_write(&mirror->hmm->mirrors_sem);
Jérôme Glisse704f3f22019-05-13 17:19:48 -0700269 list_add(&mirror->list, &mirror->hmm->mirrors);
270 up_write(&mirror->hmm->mirrors_sem);
Jérôme Glissec0b12402017-09-08 16:11:27 -0700271
272 return 0;
273}
274EXPORT_SYMBOL(hmm_mirror_register);
275
276/*
277 * hmm_mirror_unregister() - unregister a mirror
278 *
Ralph Campbell085ea252019-05-06 16:29:39 -0700279 * @mirror: mirror struct to unregister
Jérôme Glissec0b12402017-09-08 16:11:27 -0700280 *
281 * Stop mirroring a process address space, and cleanup.
282 */
283void hmm_mirror_unregister(struct hmm_mirror *mirror)
284{
Jérôme Glisse704f3f22019-05-13 17:19:48 -0700285 struct hmm *hmm = READ_ONCE(mirror->hmm);
Jérôme Glissec0b12402017-09-08 16:11:27 -0700286
Jérôme Glisse704f3f22019-05-13 17:19:48 -0700287 if (hmm == NULL)
Jérôme Glissec01cbba2018-04-10 16:28:23 -0700288 return;
289
Jérôme Glissec0b12402017-09-08 16:11:27 -0700290 down_write(&hmm->mirrors_sem);
Ralph Campbelle1401512018-04-10 16:28:19 -0700291 list_del_init(&mirror->list);
Jérôme Glisse704f3f22019-05-13 17:19:48 -0700292 /* To protect us against double unregister ... */
Jérôme Glissec01cbba2018-04-10 16:28:23 -0700293 mirror->hmm = NULL;
Jérôme Glissec0b12402017-09-08 16:11:27 -0700294 up_write(&hmm->mirrors_sem);
Jérôme Glissec01cbba2018-04-10 16:28:23 -0700295
Jérôme Glisse704f3f22019-05-13 17:19:48 -0700296 hmm_put(hmm);
Jérôme Glissec0b12402017-09-08 16:11:27 -0700297}
298EXPORT_SYMBOL(hmm_mirror_unregister);
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700299
Jérôme Glisse74eee182017-09-08 16:11:35 -0700300struct hmm_vma_walk {
301 struct hmm_range *range;
Jérôme Glisse992de9a2019-05-13 17:20:21 -0700302 struct dev_pagemap *pgmap;
Jérôme Glisse74eee182017-09-08 16:11:35 -0700303 unsigned long last;
304 bool fault;
305 bool block;
Jérôme Glisse74eee182017-09-08 16:11:35 -0700306};
307
Jérôme Glisse2aee09d2018-04-10 16:29:02 -0700308static int hmm_vma_do_fault(struct mm_walk *walk, unsigned long addr,
309 bool write_fault, uint64_t *pfn)
Jérôme Glisse74eee182017-09-08 16:11:35 -0700310{
Kuehling, Felix9b1ae602019-05-10 19:53:24 +0000311 unsigned int flags = FAULT_FLAG_REMOTE;
Jérôme Glisse74eee182017-09-08 16:11:35 -0700312 struct hmm_vma_walk *hmm_vma_walk = walk->private;
Jérôme Glissef88a1e92018-04-10 16:29:06 -0700313 struct hmm_range *range = hmm_vma_walk->range;
Jérôme Glisse74eee182017-09-08 16:11:35 -0700314 struct vm_area_struct *vma = walk->vma;
Souptick Joarder50a7ca32018-08-17 15:44:47 -0700315 vm_fault_t ret;
Jérôme Glisse74eee182017-09-08 16:11:35 -0700316
317 flags |= hmm_vma_walk->block ? 0 : FAULT_FLAG_ALLOW_RETRY;
Jérôme Glisse2aee09d2018-04-10 16:29:02 -0700318 flags |= write_fault ? FAULT_FLAG_WRITE : 0;
Souptick Joarder50a7ca32018-08-17 15:44:47 -0700319 ret = handle_mm_fault(vma, addr, flags);
320 if (ret & VM_FAULT_RETRY)
Jérôme Glisse73231612019-05-13 17:19:58 -0700321 return -EAGAIN;
Souptick Joarder50a7ca32018-08-17 15:44:47 -0700322 if (ret & VM_FAULT_ERROR) {
Jérôme Glissef88a1e92018-04-10 16:29:06 -0700323 *pfn = range->values[HMM_PFN_ERROR];
Jérôme Glisse74eee182017-09-08 16:11:35 -0700324 return -EFAULT;
325 }
326
Jérôme Glisse73231612019-05-13 17:19:58 -0700327 return -EBUSY;
Jérôme Glisse74eee182017-09-08 16:11:35 -0700328}
329
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700330static int hmm_pfns_bad(unsigned long addr,
331 unsigned long end,
332 struct mm_walk *walk)
333{
Jérôme Glissec7195472018-04-10 16:28:27 -0700334 struct hmm_vma_walk *hmm_vma_walk = walk->private;
335 struct hmm_range *range = hmm_vma_walk->range;
Jérôme Glisseff05c0c2018-04-10 16:28:38 -0700336 uint64_t *pfns = range->pfns;
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700337 unsigned long i;
338
339 i = (addr - range->start) >> PAGE_SHIFT;
340 for (; addr < end; addr += PAGE_SIZE, i++)
Jérôme Glissef88a1e92018-04-10 16:29:06 -0700341 pfns[i] = range->values[HMM_PFN_ERROR];
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700342
343 return 0;
344}
345
Jérôme Glisse5504ed22018-04-10 16:28:46 -0700346/*
347 * hmm_vma_walk_hole() - handle a range lacking valid pmd or pte(s)
348 * @start: range virtual start address (inclusive)
349 * @end: range virtual end address (exclusive)
Jérôme Glisse2aee09d2018-04-10 16:29:02 -0700350 * @fault: should we fault or not ?
351 * @write_fault: write fault ?
Jérôme Glisse5504ed22018-04-10 16:28:46 -0700352 * @walk: mm_walk structure
Ralph Campbell085ea252019-05-06 16:29:39 -0700353 * Return: 0 on success, -EBUSY after page fault, or page fault error
Jérôme Glisse5504ed22018-04-10 16:28:46 -0700354 *
355 * This function will be called whenever pmd_none() or pte_none() returns true,
356 * or whenever there is no page directory covering the virtual address range.
357 */
Jérôme Glisse2aee09d2018-04-10 16:29:02 -0700358static int hmm_vma_walk_hole_(unsigned long addr, unsigned long end,
359 bool fault, bool write_fault,
360 struct mm_walk *walk)
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700361{
Jérôme Glisse74eee182017-09-08 16:11:35 -0700362 struct hmm_vma_walk *hmm_vma_walk = walk->private;
363 struct hmm_range *range = hmm_vma_walk->range;
Jérôme Glisseff05c0c2018-04-10 16:28:38 -0700364 uint64_t *pfns = range->pfns;
Jérôme Glisse63d50662019-05-13 17:20:18 -0700365 unsigned long i, page_size;
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700366
Jérôme Glisse74eee182017-09-08 16:11:35 -0700367 hmm_vma_walk->last = addr;
Jérôme Glisse63d50662019-05-13 17:20:18 -0700368 page_size = hmm_range_page_size(range);
369 i = (addr - range->start) >> range->page_shift;
370
371 for (; addr < end; addr += page_size, i++) {
Jérôme Glissef88a1e92018-04-10 16:29:06 -0700372 pfns[i] = range->values[HMM_PFN_NONE];
Jérôme Glisse2aee09d2018-04-10 16:29:02 -0700373 if (fault || write_fault) {
Jérôme Glisse74eee182017-09-08 16:11:35 -0700374 int ret;
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700375
Jérôme Glisse2aee09d2018-04-10 16:29:02 -0700376 ret = hmm_vma_do_fault(walk, addr, write_fault,
377 &pfns[i]);
Jérôme Glisse73231612019-05-13 17:19:58 -0700378 if (ret != -EBUSY)
Jérôme Glisse74eee182017-09-08 16:11:35 -0700379 return ret;
380 }
381 }
382
Jérôme Glisse73231612019-05-13 17:19:58 -0700383 return (fault || write_fault) ? -EBUSY : 0;
Jérôme Glisse2aee09d2018-04-10 16:29:02 -0700384}
385
386static inline void hmm_pte_need_fault(const struct hmm_vma_walk *hmm_vma_walk,
387 uint64_t pfns, uint64_t cpu_flags,
388 bool *fault, bool *write_fault)
389{
Jérôme Glissef88a1e92018-04-10 16:29:06 -0700390 struct hmm_range *range = hmm_vma_walk->range;
391
Jérôme Glisse2aee09d2018-04-10 16:29:02 -0700392 if (!hmm_vma_walk->fault)
393 return;
394
Jérôme Glisse023a0192019-05-13 17:20:05 -0700395 /*
396 * So we not only consider the individual per page request we also
397 * consider the default flags requested for the range. The API can
398 * be use in 2 fashions. The first one where the HMM user coalesce
399 * multiple page fault into one request and set flags per pfns for
400 * of those faults. The second one where the HMM user want to pre-
401 * fault a range with specific flags. For the latter one it is a
402 * waste to have the user pre-fill the pfn arrays with a default
403 * flags value.
404 */
405 pfns = (pfns & range->pfn_flags_mask) | range->default_flags;
406
Jérôme Glisse2aee09d2018-04-10 16:29:02 -0700407 /* We aren't ask to do anything ... */
Jérôme Glissef88a1e92018-04-10 16:29:06 -0700408 if (!(pfns & range->flags[HMM_PFN_VALID]))
Jérôme Glisse2aee09d2018-04-10 16:29:02 -0700409 return;
Jérôme Glissef88a1e92018-04-10 16:29:06 -0700410 /* If this is device memory than only fault if explicitly requested */
411 if ((cpu_flags & range->flags[HMM_PFN_DEVICE_PRIVATE])) {
412 /* Do we fault on device memory ? */
413 if (pfns & range->flags[HMM_PFN_DEVICE_PRIVATE]) {
414 *write_fault = pfns & range->flags[HMM_PFN_WRITE];
415 *fault = true;
416 }
Jérôme Glisse2aee09d2018-04-10 16:29:02 -0700417 return;
418 }
Jérôme Glissef88a1e92018-04-10 16:29:06 -0700419
420 /* If CPU page table is not valid then we need to fault */
421 *fault = !(cpu_flags & range->flags[HMM_PFN_VALID]);
422 /* Need to write fault ? */
423 if ((pfns & range->flags[HMM_PFN_WRITE]) &&
424 !(cpu_flags & range->flags[HMM_PFN_WRITE])) {
425 *write_fault = true;
Jérôme Glisse2aee09d2018-04-10 16:29:02 -0700426 *fault = true;
427 }
428}
429
430static void hmm_range_need_fault(const struct hmm_vma_walk *hmm_vma_walk,
431 const uint64_t *pfns, unsigned long npages,
432 uint64_t cpu_flags, bool *fault,
433 bool *write_fault)
434{
435 unsigned long i;
436
437 if (!hmm_vma_walk->fault) {
438 *fault = *write_fault = false;
439 return;
440 }
441
Jérôme Glissea3e0d412019-05-13 17:20:01 -0700442 *fault = *write_fault = false;
Jérôme Glisse2aee09d2018-04-10 16:29:02 -0700443 for (i = 0; i < npages; ++i) {
444 hmm_pte_need_fault(hmm_vma_walk, pfns[i], cpu_flags,
445 fault, write_fault);
Jérôme Glissea3e0d412019-05-13 17:20:01 -0700446 if ((*write_fault))
Jérôme Glisse2aee09d2018-04-10 16:29:02 -0700447 return;
448 }
449}
450
451static int hmm_vma_walk_hole(unsigned long addr, unsigned long end,
452 struct mm_walk *walk)
453{
454 struct hmm_vma_walk *hmm_vma_walk = walk->private;
455 struct hmm_range *range = hmm_vma_walk->range;
456 bool fault, write_fault;
457 unsigned long i, npages;
458 uint64_t *pfns;
459
460 i = (addr - range->start) >> PAGE_SHIFT;
461 npages = (end - addr) >> PAGE_SHIFT;
462 pfns = &range->pfns[i];
463 hmm_range_need_fault(hmm_vma_walk, pfns, npages,
464 0, &fault, &write_fault);
465 return hmm_vma_walk_hole_(addr, end, fault, write_fault, walk);
466}
467
Jérôme Glissef88a1e92018-04-10 16:29:06 -0700468static inline uint64_t pmd_to_hmm_pfn_flags(struct hmm_range *range, pmd_t pmd)
Jérôme Glisse2aee09d2018-04-10 16:29:02 -0700469{
470 if (pmd_protnone(pmd))
471 return 0;
Jérôme Glissef88a1e92018-04-10 16:29:06 -0700472 return pmd_write(pmd) ? range->flags[HMM_PFN_VALID] |
473 range->flags[HMM_PFN_WRITE] :
474 range->flags[HMM_PFN_VALID];
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700475}
476
Jérôme Glisse992de9a2019-05-13 17:20:21 -0700477static inline uint64_t pud_to_hmm_pfn_flags(struct hmm_range *range, pud_t pud)
478{
479 if (!pud_present(pud))
480 return 0;
481 return pud_write(pud) ? range->flags[HMM_PFN_VALID] |
482 range->flags[HMM_PFN_WRITE] :
483 range->flags[HMM_PFN_VALID];
484}
485
Jérôme Glisse53f5c3f2018-04-10 16:28:59 -0700486static int hmm_vma_handle_pmd(struct mm_walk *walk,
487 unsigned long addr,
488 unsigned long end,
489 uint64_t *pfns,
490 pmd_t pmd)
491{
Jérôme Glisse992de9a2019-05-13 17:20:21 -0700492#ifdef CONFIG_TRANSPARENT_HUGEPAGE
Jérôme Glisse53f5c3f2018-04-10 16:28:59 -0700493 struct hmm_vma_walk *hmm_vma_walk = walk->private;
Jérôme Glissef88a1e92018-04-10 16:29:06 -0700494 struct hmm_range *range = hmm_vma_walk->range;
Jérôme Glisse2aee09d2018-04-10 16:29:02 -0700495 unsigned long pfn, npages, i;
Jérôme Glisse2aee09d2018-04-10 16:29:02 -0700496 bool fault, write_fault;
Jérôme Glissef88a1e92018-04-10 16:29:06 -0700497 uint64_t cpu_flags;
Jérôme Glisse53f5c3f2018-04-10 16:28:59 -0700498
Jérôme Glisse2aee09d2018-04-10 16:29:02 -0700499 npages = (end - addr) >> PAGE_SHIFT;
Jérôme Glissef88a1e92018-04-10 16:29:06 -0700500 cpu_flags = pmd_to_hmm_pfn_flags(range, pmd);
Jérôme Glisse2aee09d2018-04-10 16:29:02 -0700501 hmm_range_need_fault(hmm_vma_walk, pfns, npages, cpu_flags,
502 &fault, &write_fault);
Jérôme Glisse53f5c3f2018-04-10 16:28:59 -0700503
Jérôme Glisse2aee09d2018-04-10 16:29:02 -0700504 if (pmd_protnone(pmd) || fault || write_fault)
505 return hmm_vma_walk_hole_(addr, end, fault, write_fault, walk);
Jérôme Glisse53f5c3f2018-04-10 16:28:59 -0700506
507 pfn = pmd_pfn(pmd) + pte_index(addr);
Jérôme Glisse992de9a2019-05-13 17:20:21 -0700508 for (i = 0; addr < end; addr += PAGE_SIZE, i++, pfn++) {
509 if (pmd_devmap(pmd)) {
510 hmm_vma_walk->pgmap = get_dev_pagemap(pfn,
511 hmm_vma_walk->pgmap);
512 if (unlikely(!hmm_vma_walk->pgmap))
513 return -EBUSY;
514 }
Jérôme Glisse391aab12019-05-13 17:20:31 -0700515 pfns[i] = hmm_device_entry_from_pfn(range, pfn) | cpu_flags;
Jérôme Glisse992de9a2019-05-13 17:20:21 -0700516 }
517 if (hmm_vma_walk->pgmap) {
518 put_dev_pagemap(hmm_vma_walk->pgmap);
519 hmm_vma_walk->pgmap = NULL;
520 }
Jérôme Glisse53f5c3f2018-04-10 16:28:59 -0700521 hmm_vma_walk->last = end;
522 return 0;
Jérôme Glisse992de9a2019-05-13 17:20:21 -0700523#else
524 /* If THP is not enabled then we should never reach that code ! */
525 return -EINVAL;
526#endif
Jérôme Glisse53f5c3f2018-04-10 16:28:59 -0700527}
528
Jérôme Glissef88a1e92018-04-10 16:29:06 -0700529static inline uint64_t pte_to_hmm_pfn_flags(struct hmm_range *range, pte_t pte)
Jérôme Glisse2aee09d2018-04-10 16:29:02 -0700530{
Philip Yang789c2af2019-05-23 16:32:31 -0400531 if (pte_none(pte) || !pte_present(pte) || pte_protnone(pte))
Jérôme Glisse2aee09d2018-04-10 16:29:02 -0700532 return 0;
Jérôme Glissef88a1e92018-04-10 16:29:06 -0700533 return pte_write(pte) ? range->flags[HMM_PFN_VALID] |
534 range->flags[HMM_PFN_WRITE] :
535 range->flags[HMM_PFN_VALID];
Jérôme Glisse2aee09d2018-04-10 16:29:02 -0700536}
537
Jérôme Glisse53f5c3f2018-04-10 16:28:59 -0700538static int hmm_vma_handle_pte(struct mm_walk *walk, unsigned long addr,
539 unsigned long end, pmd_t *pmdp, pte_t *ptep,
540 uint64_t *pfn)
541{
542 struct hmm_vma_walk *hmm_vma_walk = walk->private;
Jérôme Glissef88a1e92018-04-10 16:29:06 -0700543 struct hmm_range *range = hmm_vma_walk->range;
Jérôme Glisse53f5c3f2018-04-10 16:28:59 -0700544 struct vm_area_struct *vma = walk->vma;
Jérôme Glisse2aee09d2018-04-10 16:29:02 -0700545 bool fault, write_fault;
546 uint64_t cpu_flags;
Jérôme Glisse53f5c3f2018-04-10 16:28:59 -0700547 pte_t pte = *ptep;
Jérôme Glissef88a1e92018-04-10 16:29:06 -0700548 uint64_t orig_pfn = *pfn;
Jérôme Glisse53f5c3f2018-04-10 16:28:59 -0700549
Jérôme Glissef88a1e92018-04-10 16:29:06 -0700550 *pfn = range->values[HMM_PFN_NONE];
Jérôme Glisse73231612019-05-13 17:19:58 -0700551 fault = write_fault = false;
Jérôme Glisse53f5c3f2018-04-10 16:28:59 -0700552
553 if (pte_none(pte)) {
Jérôme Glisse73231612019-05-13 17:19:58 -0700554 hmm_pte_need_fault(hmm_vma_walk, orig_pfn, 0,
555 &fault, &write_fault);
Jérôme Glisse2aee09d2018-04-10 16:29:02 -0700556 if (fault || write_fault)
Jérôme Glisse53f5c3f2018-04-10 16:28:59 -0700557 goto fault;
558 return 0;
559 }
560
561 if (!pte_present(pte)) {
562 swp_entry_t entry = pte_to_swp_entry(pte);
563
564 if (!non_swap_entry(entry)) {
Jérôme Glisse2aee09d2018-04-10 16:29:02 -0700565 if (fault || write_fault)
Jérôme Glisse53f5c3f2018-04-10 16:28:59 -0700566 goto fault;
567 return 0;
568 }
569
570 /*
571 * This is a special swap entry, ignore migration, use
572 * device and report anything else as error.
573 */
574 if (is_device_private_entry(entry)) {
Jérôme Glissef88a1e92018-04-10 16:29:06 -0700575 cpu_flags = range->flags[HMM_PFN_VALID] |
576 range->flags[HMM_PFN_DEVICE_PRIVATE];
Jérôme Glisse2aee09d2018-04-10 16:29:02 -0700577 cpu_flags |= is_write_device_private_entry(entry) ?
Jérôme Glissef88a1e92018-04-10 16:29:06 -0700578 range->flags[HMM_PFN_WRITE] : 0;
579 hmm_pte_need_fault(hmm_vma_walk, orig_pfn, cpu_flags,
580 &fault, &write_fault);
581 if (fault || write_fault)
582 goto fault;
Jérôme Glisse391aab12019-05-13 17:20:31 -0700583 *pfn = hmm_device_entry_from_pfn(range,
584 swp_offset(entry));
Jérôme Glissef88a1e92018-04-10 16:29:06 -0700585 *pfn |= cpu_flags;
Jérôme Glisse53f5c3f2018-04-10 16:28:59 -0700586 return 0;
587 }
588
589 if (is_migration_entry(entry)) {
Jérôme Glisse2aee09d2018-04-10 16:29:02 -0700590 if (fault || write_fault) {
Jérôme Glisse53f5c3f2018-04-10 16:28:59 -0700591 pte_unmap(ptep);
592 hmm_vma_walk->last = addr;
593 migration_entry_wait(vma->vm_mm,
Jérôme Glisse2aee09d2018-04-10 16:29:02 -0700594 pmdp, addr);
Jérôme Glisse73231612019-05-13 17:19:58 -0700595 return -EBUSY;
Jérôme Glisse53f5c3f2018-04-10 16:28:59 -0700596 }
597 return 0;
598 }
599
600 /* Report error for everything else */
Jérôme Glissef88a1e92018-04-10 16:29:06 -0700601 *pfn = range->values[HMM_PFN_ERROR];
Jérôme Glisse53f5c3f2018-04-10 16:28:59 -0700602 return -EFAULT;
Jérôme Glisse73231612019-05-13 17:19:58 -0700603 } else {
604 cpu_flags = pte_to_hmm_pfn_flags(range, pte);
605 hmm_pte_need_fault(hmm_vma_walk, orig_pfn, cpu_flags,
606 &fault, &write_fault);
Jérôme Glisse53f5c3f2018-04-10 16:28:59 -0700607 }
608
Jérôme Glisse2aee09d2018-04-10 16:29:02 -0700609 if (fault || write_fault)
Jérôme Glisse53f5c3f2018-04-10 16:28:59 -0700610 goto fault;
611
Jérôme Glisse992de9a2019-05-13 17:20:21 -0700612 if (pte_devmap(pte)) {
613 hmm_vma_walk->pgmap = get_dev_pagemap(pte_pfn(pte),
614 hmm_vma_walk->pgmap);
615 if (unlikely(!hmm_vma_walk->pgmap))
616 return -EBUSY;
617 } else if (IS_ENABLED(CONFIG_ARCH_HAS_PTE_SPECIAL) && pte_special(pte)) {
618 *pfn = range->values[HMM_PFN_SPECIAL];
619 return -EFAULT;
620 }
621
Jérôme Glisse391aab12019-05-13 17:20:31 -0700622 *pfn = hmm_device_entry_from_pfn(range, pte_pfn(pte)) | cpu_flags;
Jérôme Glisse53f5c3f2018-04-10 16:28:59 -0700623 return 0;
624
625fault:
Jérôme Glisse992de9a2019-05-13 17:20:21 -0700626 if (hmm_vma_walk->pgmap) {
627 put_dev_pagemap(hmm_vma_walk->pgmap);
628 hmm_vma_walk->pgmap = NULL;
629 }
Jérôme Glisse53f5c3f2018-04-10 16:28:59 -0700630 pte_unmap(ptep);
631 /* Fault any virtual address we were asked to fault */
Jérôme Glisse2aee09d2018-04-10 16:29:02 -0700632 return hmm_vma_walk_hole_(addr, end, fault, write_fault, walk);
Jérôme Glisse53f5c3f2018-04-10 16:28:59 -0700633}
634
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700635static int hmm_vma_walk_pmd(pmd_t *pmdp,
636 unsigned long start,
637 unsigned long end,
638 struct mm_walk *walk)
639{
Jérôme Glisse74eee182017-09-08 16:11:35 -0700640 struct hmm_vma_walk *hmm_vma_walk = walk->private;
641 struct hmm_range *range = hmm_vma_walk->range;
Jérôme Glissed08faca2018-10-30 15:04:20 -0700642 struct vm_area_struct *vma = walk->vma;
Jérôme Glisseff05c0c2018-04-10 16:28:38 -0700643 uint64_t *pfns = range->pfns;
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700644 unsigned long addr = start, i;
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700645 pte_t *ptep;
Jérôme Glissed08faca2018-10-30 15:04:20 -0700646 pmd_t pmd;
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700647
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700648
649again:
Jérôme Glissed08faca2018-10-30 15:04:20 -0700650 pmd = READ_ONCE(*pmdp);
651 if (pmd_none(pmd))
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700652 return hmm_vma_walk_hole(start, end, walk);
653
Jérôme Glissed08faca2018-10-30 15:04:20 -0700654 if (pmd_huge(pmd) && (range->vma->vm_flags & VM_HUGETLB))
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700655 return hmm_pfns_bad(start, end, walk);
656
Jérôme Glissed08faca2018-10-30 15:04:20 -0700657 if (thp_migration_supported() && is_pmd_migration_entry(pmd)) {
658 bool fault, write_fault;
659 unsigned long npages;
660 uint64_t *pfns;
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700661
Jérôme Glissed08faca2018-10-30 15:04:20 -0700662 i = (addr - range->start) >> PAGE_SHIFT;
663 npages = (end - addr) >> PAGE_SHIFT;
664 pfns = &range->pfns[i];
665
666 hmm_range_need_fault(hmm_vma_walk, pfns, npages,
667 0, &fault, &write_fault);
668 if (fault || write_fault) {
669 hmm_vma_walk->last = addr;
670 pmd_migration_entry_wait(vma->vm_mm, pmdp);
Jérôme Glisse73231612019-05-13 17:19:58 -0700671 return -EBUSY;
Jérôme Glissed08faca2018-10-30 15:04:20 -0700672 }
673 return 0;
674 } else if (!pmd_present(pmd))
675 return hmm_pfns_bad(start, end, walk);
676
677 if (pmd_devmap(pmd) || pmd_trans_huge(pmd)) {
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700678 /*
679 * No need to take pmd_lock here, even if some other threads
680 * is splitting the huge pmd we will get that event through
681 * mmu_notifier callback.
682 *
683 * So just read pmd value and check again its a transparent
684 * huge or device mapping one and compute corresponding pfn
685 * values.
686 */
687 pmd = pmd_read_atomic(pmdp);
688 barrier();
689 if (!pmd_devmap(pmd) && !pmd_trans_huge(pmd))
690 goto again;
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700691
Jérôme Glissed08faca2018-10-30 15:04:20 -0700692 i = (addr - range->start) >> PAGE_SHIFT;
Jérôme Glisse53f5c3f2018-04-10 16:28:59 -0700693 return hmm_vma_handle_pmd(walk, addr, end, &pfns[i], pmd);
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700694 }
695
Jérôme Glissed08faca2018-10-30 15:04:20 -0700696 /*
697 * We have handled all the valid case above ie either none, migration,
698 * huge or transparent huge. At this point either it is a valid pmd
699 * entry pointing to pte directory or it is a bad pmd that will not
700 * recover.
701 */
702 if (pmd_bad(pmd))
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700703 return hmm_pfns_bad(start, end, walk);
704
705 ptep = pte_offset_map(pmdp, addr);
Jérôme Glissed08faca2018-10-30 15:04:20 -0700706 i = (addr - range->start) >> PAGE_SHIFT;
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700707 for (; addr < end; addr += PAGE_SIZE, ptep++, i++) {
Jérôme Glisse53f5c3f2018-04-10 16:28:59 -0700708 int r;
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700709
Jérôme Glisse53f5c3f2018-04-10 16:28:59 -0700710 r = hmm_vma_handle_pte(walk, addr, end, pmdp, ptep, &pfns[i]);
711 if (r) {
712 /* hmm_vma_handle_pte() did unmap pte directory */
713 hmm_vma_walk->last = addr;
714 return r;
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700715 }
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700716 }
Jérôme Glisse992de9a2019-05-13 17:20:21 -0700717 if (hmm_vma_walk->pgmap) {
718 /*
719 * We do put_dev_pagemap() here and not in hmm_vma_handle_pte()
720 * so that we can leverage get_dev_pagemap() optimization which
721 * will not re-take a reference on a pgmap if we already have
722 * one.
723 */
724 put_dev_pagemap(hmm_vma_walk->pgmap);
725 hmm_vma_walk->pgmap = NULL;
726 }
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700727 pte_unmap(ptep - 1);
728
Jérôme Glisse53f5c3f2018-04-10 16:28:59 -0700729 hmm_vma_walk->last = addr;
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700730 return 0;
731}
732
Jérôme Glisse992de9a2019-05-13 17:20:21 -0700733static int hmm_vma_walk_pud(pud_t *pudp,
734 unsigned long start,
735 unsigned long end,
736 struct mm_walk *walk)
737{
738 struct hmm_vma_walk *hmm_vma_walk = walk->private;
739 struct hmm_range *range = hmm_vma_walk->range;
740 unsigned long addr = start, next;
741 pmd_t *pmdp;
742 pud_t pud;
743 int ret;
744
745again:
746 pud = READ_ONCE(*pudp);
747 if (pud_none(pud))
748 return hmm_vma_walk_hole(start, end, walk);
749
750 if (pud_huge(pud) && pud_devmap(pud)) {
751 unsigned long i, npages, pfn;
752 uint64_t *pfns, cpu_flags;
753 bool fault, write_fault;
754
755 if (!pud_present(pud))
756 return hmm_vma_walk_hole(start, end, walk);
757
758 i = (addr - range->start) >> PAGE_SHIFT;
759 npages = (end - addr) >> PAGE_SHIFT;
760 pfns = &range->pfns[i];
761
762 cpu_flags = pud_to_hmm_pfn_flags(range, pud);
763 hmm_range_need_fault(hmm_vma_walk, pfns, npages,
764 cpu_flags, &fault, &write_fault);
765 if (fault || write_fault)
766 return hmm_vma_walk_hole_(addr, end, fault,
767 write_fault, walk);
768
Jérôme Glisse992de9a2019-05-13 17:20:21 -0700769 pfn = pud_pfn(pud) + ((addr & ~PUD_MASK) >> PAGE_SHIFT);
770 for (i = 0; i < npages; ++i, ++pfn) {
771 hmm_vma_walk->pgmap = get_dev_pagemap(pfn,
772 hmm_vma_walk->pgmap);
773 if (unlikely(!hmm_vma_walk->pgmap))
774 return -EBUSY;
Jérôme Glisse391aab12019-05-13 17:20:31 -0700775 pfns[i] = hmm_device_entry_from_pfn(range, pfn) |
776 cpu_flags;
Jérôme Glisse992de9a2019-05-13 17:20:21 -0700777 }
778 if (hmm_vma_walk->pgmap) {
779 put_dev_pagemap(hmm_vma_walk->pgmap);
780 hmm_vma_walk->pgmap = NULL;
781 }
782 hmm_vma_walk->last = end;
783 return 0;
Jérôme Glisse992de9a2019-05-13 17:20:21 -0700784 }
785
786 split_huge_pud(walk->vma, pudp, addr);
787 if (pud_none(*pudp))
788 goto again;
789
790 pmdp = pmd_offset(pudp, addr);
791 do {
792 next = pmd_addr_end(addr, end);
793 ret = hmm_vma_walk_pmd(pmdp, addr, next, walk);
794 if (ret)
795 return ret;
796 } while (pmdp++, addr = next, addr != end);
797
798 return 0;
799}
800
Jérôme Glisse63d50662019-05-13 17:20:18 -0700801static int hmm_vma_walk_hugetlb_entry(pte_t *pte, unsigned long hmask,
802 unsigned long start, unsigned long end,
803 struct mm_walk *walk)
804{
805#ifdef CONFIG_HUGETLB_PAGE
806 unsigned long addr = start, i, pfn, mask, size, pfn_inc;
807 struct hmm_vma_walk *hmm_vma_walk = walk->private;
808 struct hmm_range *range = hmm_vma_walk->range;
809 struct vm_area_struct *vma = walk->vma;
810 struct hstate *h = hstate_vma(vma);
811 uint64_t orig_pfn, cpu_flags;
812 bool fault, write_fault;
813 spinlock_t *ptl;
814 pte_t entry;
815 int ret = 0;
816
817 size = 1UL << huge_page_shift(h);
818 mask = size - 1;
819 if (range->page_shift != PAGE_SHIFT) {
820 /* Make sure we are looking at full page. */
821 if (start & mask)
822 return -EINVAL;
823 if (end < (start + size))
824 return -EINVAL;
825 pfn_inc = size >> PAGE_SHIFT;
826 } else {
827 pfn_inc = 1;
828 size = PAGE_SIZE;
829 }
830
831
832 ptl = huge_pte_lock(hstate_vma(walk->vma), walk->mm, pte);
833 entry = huge_ptep_get(pte);
834
835 i = (start - range->start) >> range->page_shift;
836 orig_pfn = range->pfns[i];
837 range->pfns[i] = range->values[HMM_PFN_NONE];
838 cpu_flags = pte_to_hmm_pfn_flags(range, entry);
839 fault = write_fault = false;
840 hmm_pte_need_fault(hmm_vma_walk, orig_pfn, cpu_flags,
841 &fault, &write_fault);
842 if (fault || write_fault) {
843 ret = -ENOENT;
844 goto unlock;
845 }
846
847 pfn = pte_pfn(entry) + ((start & mask) >> range->page_shift);
848 for (; addr < end; addr += size, i++, pfn += pfn_inc)
Jérôme Glisse391aab12019-05-13 17:20:31 -0700849 range->pfns[i] = hmm_device_entry_from_pfn(range, pfn) |
850 cpu_flags;
Jérôme Glisse63d50662019-05-13 17:20:18 -0700851 hmm_vma_walk->last = end;
852
853unlock:
854 spin_unlock(ptl);
855
856 if (ret == -ENOENT)
857 return hmm_vma_walk_hole_(addr, end, fault, write_fault, walk);
858
859 return ret;
860#else /* CONFIG_HUGETLB_PAGE */
861 return -EINVAL;
862#endif
863}
864
Jérôme Glissef88a1e92018-04-10 16:29:06 -0700865static void hmm_pfns_clear(struct hmm_range *range,
866 uint64_t *pfns,
Jérôme Glisse33cd47d2018-04-10 16:28:54 -0700867 unsigned long addr,
868 unsigned long end)
869{
870 for (; addr < end; addr += PAGE_SIZE, pfns++)
Jérôme Glissef88a1e92018-04-10 16:29:06 -0700871 *pfns = range->values[HMM_PFN_NONE];
Jérôme Glisse33cd47d2018-04-10 16:28:54 -0700872}
873
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700874/*
Jérôme Glissea3e0d412019-05-13 17:20:01 -0700875 * hmm_range_register() - start tracking change to CPU page table over a range
Jérôme Glisse25f23a02019-05-13 17:19:55 -0700876 * @range: range
Jérôme Glissea3e0d412019-05-13 17:20:01 -0700877 * @mm: the mm struct for the range of virtual address
878 * @start: start virtual address (inclusive)
879 * @end: end virtual address (exclusive)
Jérôme Glisse63d50662019-05-13 17:20:18 -0700880 * @page_shift: expect page shift for the range
Jérôme Glissea3e0d412019-05-13 17:20:01 -0700881 * Returns 0 on success, -EFAULT if the address space is no longer valid
Jérôme Glisse25f23a02019-05-13 17:19:55 -0700882 *
Jérôme Glissea3e0d412019-05-13 17:20:01 -0700883 * Track updates to the CPU page table see include/linux/hmm.h
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700884 */
Jérôme Glissea3e0d412019-05-13 17:20:01 -0700885int hmm_range_register(struct hmm_range *range,
Jason Gunthorpee36acfe2019-05-23 09:41:19 -0300886 struct hmm_mirror *mirror,
Jérôme Glissea3e0d412019-05-13 17:20:01 -0700887 unsigned long start,
Jérôme Glisse63d50662019-05-13 17:20:18 -0700888 unsigned long end,
889 unsigned page_shift)
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700890{
Jérôme Glisse63d50662019-05-13 17:20:18 -0700891 unsigned long mask = ((1UL << page_shift) - 1UL);
Jason Gunthorpee36acfe2019-05-23 09:41:19 -0300892 struct hmm *hmm = mirror->hmm;
Jérôme Glisse63d50662019-05-13 17:20:18 -0700893
Jérôme Glissea3e0d412019-05-13 17:20:01 -0700894 range->valid = false;
Jérôme Glisse704f3f22019-05-13 17:19:48 -0700895 range->hmm = NULL;
896
Jérôme Glisse63d50662019-05-13 17:20:18 -0700897 if ((start & mask) || (end & mask))
898 return -EINVAL;
899 if (start >= end)
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700900 return -EINVAL;
901
Jérôme Glisse63d50662019-05-13 17:20:18 -0700902 range->page_shift = page_shift;
Jérôme Glissea3e0d412019-05-13 17:20:01 -0700903 range->start = start;
904 range->end = end;
905
Jérôme Glisse704f3f22019-05-13 17:19:48 -0700906 /* Check if hmm_mm_destroy() was call. */
Jason Gunthorpee36acfe2019-05-23 09:41:19 -0300907 if (hmm->mm == NULL || hmm->dead)
Jérôme Glissea3e0d412019-05-13 17:20:01 -0700908 return -EFAULT;
Jérôme Glisse86586a42018-04-10 16:28:34 -0700909
Ralph Campbell085ea252019-05-06 16:29:39 -0700910 /* Initialize range to track CPU page table updates. */
911 mutex_lock(&hmm->lock);
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700912
Ralph Campbell085ea252019-05-06 16:29:39 -0700913 range->hmm = hmm;
Jason Gunthorpee36acfe2019-05-23 09:41:19 -0300914 kref_get(&hmm->kref);
Jason Gunthorpe157816f2019-05-23 11:43:43 -0300915 list_add(&range->list, &hmm->ranges);
Jérôme Glisse74eee182017-09-08 16:11:35 -0700916
Jérôme Glisse704f3f22019-05-13 17:19:48 -0700917 /*
Jérôme Glissea3e0d412019-05-13 17:20:01 -0700918 * If there are any concurrent notifiers we have to wait for them for
919 * the range to be valid (see hmm_range_wait_until_valid()).
Jérôme Glisse704f3f22019-05-13 17:19:48 -0700920 */
Ralph Campbell085ea252019-05-06 16:29:39 -0700921 if (!hmm->notifiers)
Jérôme Glissea3e0d412019-05-13 17:20:01 -0700922 range->valid = true;
Ralph Campbell085ea252019-05-06 16:29:39 -0700923 mutex_unlock(&hmm->lock);
Jérôme Glissea3e0d412019-05-13 17:20:01 -0700924
925 return 0;
926}
927EXPORT_SYMBOL(hmm_range_register);
928
929/*
930 * hmm_range_unregister() - stop tracking change to CPU page table over a range
931 * @range: range
932 *
933 * Range struct is used to track updates to the CPU page table after a call to
934 * hmm_range_register(). See include/linux/hmm.h for how to use it.
935 */
936void hmm_range_unregister(struct hmm_range *range)
937{
Ralph Campbell085ea252019-05-06 16:29:39 -0700938 struct hmm *hmm = range->hmm;
939
Jérôme Glissea3e0d412019-05-13 17:20:01 -0700940 /* Sanity check this really should not happen. */
Ralph Campbell085ea252019-05-06 16:29:39 -0700941 if (hmm == NULL || range->end <= range->start)
Jérôme Glissea3e0d412019-05-13 17:20:01 -0700942 return;
943
Ralph Campbell085ea252019-05-06 16:29:39 -0700944 mutex_lock(&hmm->lock);
Jason Gunthorpe157816f2019-05-23 11:43:43 -0300945 list_del(&range->list);
Ralph Campbell085ea252019-05-06 16:29:39 -0700946 mutex_unlock(&hmm->lock);
Jérôme Glissea3e0d412019-05-13 17:20:01 -0700947
948 /* Drop reference taken by hmm_range_register() */
949 range->valid = false;
Ralph Campbell085ea252019-05-06 16:29:39 -0700950 hmm_put(hmm);
Jérôme Glissea3e0d412019-05-13 17:20:01 -0700951 range->hmm = NULL;
952}
953EXPORT_SYMBOL(hmm_range_unregister);
954
955/*
956 * hmm_range_snapshot() - snapshot CPU page table for a range
957 * @range: range
Ralph Campbell085ea252019-05-06 16:29:39 -0700958 * Return: -EINVAL if invalid argument, -ENOMEM out of memory, -EPERM invalid
Jérôme Glissea3e0d412019-05-13 17:20:01 -0700959 * permission (for instance asking for write and range is read only),
960 * -EAGAIN if you need to retry, -EFAULT invalid (ie either no valid
961 * vma or it is illegal to access that range), number of valid pages
962 * in range->pfns[] (from range start address).
963 *
964 * This snapshots the CPU page table for a range of virtual addresses. Snapshot
965 * validity is tracked by range struct. See in include/linux/hmm.h for example
966 * on how to use.
967 */
968long hmm_range_snapshot(struct hmm_range *range)
969{
Jérôme Glisse63d50662019-05-13 17:20:18 -0700970 const unsigned long device_vma = VM_IO | VM_PFNMAP | VM_MIXEDMAP;
Jérôme Glissea3e0d412019-05-13 17:20:01 -0700971 unsigned long start = range->start, end;
972 struct hmm_vma_walk hmm_vma_walk;
973 struct hmm *hmm = range->hmm;
974 struct vm_area_struct *vma;
975 struct mm_walk mm_walk;
976
977 /* Check if hmm_mm_destroy() was call. */
978 if (hmm->mm == NULL || hmm->dead)
979 return -EFAULT;
980
981 do {
982 /* If range is no longer valid force retry. */
983 if (!range->valid)
984 return -EAGAIN;
985
986 vma = find_vma(hmm->mm, start);
Jérôme Glisse63d50662019-05-13 17:20:18 -0700987 if (vma == NULL || (vma->vm_flags & device_vma))
Jérôme Glissea3e0d412019-05-13 17:20:01 -0700988 return -EFAULT;
989
Jérôme Glisse63d50662019-05-13 17:20:18 -0700990 if (is_vm_hugetlb_page(vma)) {
Jason Gunthorpe1c2308f02019-05-27 17:02:21 -0300991 if (huge_page_shift(hstate_vma(vma)) !=
992 range->page_shift &&
Jérôme Glisse63d50662019-05-13 17:20:18 -0700993 range->page_shift != PAGE_SHIFT)
994 return -EINVAL;
995 } else {
996 if (range->page_shift != PAGE_SHIFT)
997 return -EINVAL;
998 }
999
Jérôme Glissea3e0d412019-05-13 17:20:01 -07001000 if (!(vma->vm_flags & VM_READ)) {
1001 /*
1002 * If vma do not allow read access, then assume that it
1003 * does not allow write access, either. HMM does not
1004 * support architecture that allow write without read.
1005 */
1006 hmm_pfns_clear(range, range->pfns,
1007 range->start, range->end);
1008 return -EPERM;
1009 }
1010
1011 range->vma = vma;
Jérôme Glisse992de9a2019-05-13 17:20:21 -07001012 hmm_vma_walk.pgmap = NULL;
Jérôme Glissea3e0d412019-05-13 17:20:01 -07001013 hmm_vma_walk.last = start;
1014 hmm_vma_walk.fault = false;
1015 hmm_vma_walk.range = range;
1016 mm_walk.private = &hmm_vma_walk;
1017 end = min(range->end, vma->vm_end);
1018
1019 mm_walk.vma = vma;
1020 mm_walk.mm = vma->vm_mm;
1021 mm_walk.pte_entry = NULL;
1022 mm_walk.test_walk = NULL;
1023 mm_walk.hugetlb_entry = NULL;
Jérôme Glisse992de9a2019-05-13 17:20:21 -07001024 mm_walk.pud_entry = hmm_vma_walk_pud;
Jérôme Glissea3e0d412019-05-13 17:20:01 -07001025 mm_walk.pmd_entry = hmm_vma_walk_pmd;
1026 mm_walk.pte_hole = hmm_vma_walk_hole;
Jérôme Glisse63d50662019-05-13 17:20:18 -07001027 mm_walk.hugetlb_entry = hmm_vma_walk_hugetlb_entry;
Jérôme Glissea3e0d412019-05-13 17:20:01 -07001028
1029 walk_page_range(start, end, &mm_walk);
1030 start = end;
1031 } while (start < range->end);
1032
Jérôme Glisse25f23a02019-05-13 17:19:55 -07001033 return (hmm_vma_walk.last - range->start) >> PAGE_SHIFT;
Jérôme Glisseda4c3c72017-09-08 16:11:31 -07001034}
Jérôme Glisse25f23a02019-05-13 17:19:55 -07001035EXPORT_SYMBOL(hmm_range_snapshot);
Jérôme Glisseda4c3c72017-09-08 16:11:31 -07001036
1037/*
Jérôme Glisse73231612019-05-13 17:19:58 -07001038 * hmm_range_fault() - try to fault some address in a virtual address range
Jérôme Glisse08232a42018-04-10 16:28:30 -07001039 * @range: range being faulted
Jérôme Glisse74eee182017-09-08 16:11:35 -07001040 * @block: allow blocking on fault (if true it sleeps and do not drop mmap_sem)
Ralph Campbell085ea252019-05-06 16:29:39 -07001041 * Return: number of valid pages in range->pfns[] (from range start
Jérôme Glisse73231612019-05-13 17:19:58 -07001042 * address). This may be zero. If the return value is negative,
1043 * then one of the following values may be returned:
1044 *
1045 * -EINVAL invalid arguments or mm or virtual address are in an
Jérôme Glisse63d50662019-05-13 17:20:18 -07001046 * invalid vma (for instance device file vma).
Jérôme Glisse73231612019-05-13 17:19:58 -07001047 * -ENOMEM: Out of memory.
1048 * -EPERM: Invalid permission (for instance asking for write and
1049 * range is read only).
1050 * -EAGAIN: If you need to retry and mmap_sem was drop. This can only
1051 * happens if block argument is false.
1052 * -EBUSY: If the the range is being invalidated and you should wait
1053 * for invalidation to finish.
1054 * -EFAULT: Invalid (ie either no valid vma or it is illegal to access
1055 * that range), number of valid pages in range->pfns[] (from
1056 * range start address).
Jérôme Glisse74eee182017-09-08 16:11:35 -07001057 *
1058 * This is similar to a regular CPU page fault except that it will not trigger
Jérôme Glisse73231612019-05-13 17:19:58 -07001059 * any memory migration if the memory being faulted is not accessible by CPUs
1060 * and caller does not ask for migration.
Jérôme Glisse74eee182017-09-08 16:11:35 -07001061 *
Jérôme Glisseff05c0c2018-04-10 16:28:38 -07001062 * On error, for one virtual address in the range, the function will mark the
1063 * corresponding HMM pfn entry with an error flag.
Jérôme Glisse74eee182017-09-08 16:11:35 -07001064 */
Jérôme Glisse73231612019-05-13 17:19:58 -07001065long hmm_range_fault(struct hmm_range *range, bool block)
Jérôme Glisse74eee182017-09-08 16:11:35 -07001066{
Jérôme Glisse63d50662019-05-13 17:20:18 -07001067 const unsigned long device_vma = VM_IO | VM_PFNMAP | VM_MIXEDMAP;
Jérôme Glissea3e0d412019-05-13 17:20:01 -07001068 unsigned long start = range->start, end;
Jérôme Glisse74eee182017-09-08 16:11:35 -07001069 struct hmm_vma_walk hmm_vma_walk;
Jérôme Glissea3e0d412019-05-13 17:20:01 -07001070 struct hmm *hmm = range->hmm;
1071 struct vm_area_struct *vma;
Jérôme Glisse74eee182017-09-08 16:11:35 -07001072 struct mm_walk mm_walk;
Jérôme Glisse74eee182017-09-08 16:11:35 -07001073 int ret;
1074
Jérôme Glisse704f3f22019-05-13 17:19:48 -07001075 /* Check if hmm_mm_destroy() was call. */
Jérôme Glissea3e0d412019-05-13 17:20:01 -07001076 if (hmm->mm == NULL || hmm->dead)
1077 return -EFAULT;
Jérôme Glisse74eee182017-09-08 16:11:35 -07001078
1079 do {
Jérôme Glissea3e0d412019-05-13 17:20:01 -07001080 /* If range is no longer valid force retry. */
1081 if (!range->valid) {
1082 up_read(&hmm->mm->mmap_sem);
1083 return -EAGAIN;
1084 }
Jérôme Glisse74eee182017-09-08 16:11:35 -07001085
Jérôme Glissea3e0d412019-05-13 17:20:01 -07001086 vma = find_vma(hmm->mm, start);
Jérôme Glisse63d50662019-05-13 17:20:18 -07001087 if (vma == NULL || (vma->vm_flags & device_vma))
Jérôme Glissea3e0d412019-05-13 17:20:01 -07001088 return -EFAULT;
Jérôme Glisse74eee182017-09-08 16:11:35 -07001089
Jérôme Glisse63d50662019-05-13 17:20:18 -07001090 if (is_vm_hugetlb_page(vma)) {
1091 if (huge_page_shift(hstate_vma(vma)) !=
1092 range->page_shift &&
1093 range->page_shift != PAGE_SHIFT)
1094 return -EINVAL;
1095 } else {
1096 if (range->page_shift != PAGE_SHIFT)
1097 return -EINVAL;
1098 }
1099
Jérôme Glissea3e0d412019-05-13 17:20:01 -07001100 if (!(vma->vm_flags & VM_READ)) {
1101 /*
1102 * If vma do not allow read access, then assume that it
1103 * does not allow write access, either. HMM does not
1104 * support architecture that allow write without read.
1105 */
1106 hmm_pfns_clear(range, range->pfns,
1107 range->start, range->end);
1108 return -EPERM;
1109 }
1110
1111 range->vma = vma;
Jérôme Glisse992de9a2019-05-13 17:20:21 -07001112 hmm_vma_walk.pgmap = NULL;
Jérôme Glissea3e0d412019-05-13 17:20:01 -07001113 hmm_vma_walk.last = start;
1114 hmm_vma_walk.fault = true;
1115 hmm_vma_walk.block = block;
1116 hmm_vma_walk.range = range;
1117 mm_walk.private = &hmm_vma_walk;
1118 end = min(range->end, vma->vm_end);
1119
1120 mm_walk.vma = vma;
1121 mm_walk.mm = vma->vm_mm;
1122 mm_walk.pte_entry = NULL;
1123 mm_walk.test_walk = NULL;
1124 mm_walk.hugetlb_entry = NULL;
Jérôme Glisse992de9a2019-05-13 17:20:21 -07001125 mm_walk.pud_entry = hmm_vma_walk_pud;
Jérôme Glissea3e0d412019-05-13 17:20:01 -07001126 mm_walk.pmd_entry = hmm_vma_walk_pmd;
1127 mm_walk.pte_hole = hmm_vma_walk_hole;
Jérôme Glisse63d50662019-05-13 17:20:18 -07001128 mm_walk.hugetlb_entry = hmm_vma_walk_hugetlb_entry;
Jérôme Glissea3e0d412019-05-13 17:20:01 -07001129
1130 do {
1131 ret = walk_page_range(start, end, &mm_walk);
1132 start = hmm_vma_walk.last;
1133
1134 /* Keep trying while the range is valid. */
1135 } while (ret == -EBUSY && range->valid);
1136
1137 if (ret) {
1138 unsigned long i;
1139
1140 i = (hmm_vma_walk.last - range->start) >> PAGE_SHIFT;
1141 hmm_pfns_clear(range, &range->pfns[i],
1142 hmm_vma_walk.last, range->end);
1143 return ret;
1144 }
1145 start = end;
1146
1147 } while (start < range->end);
Jérôme Glisse704f3f22019-05-13 17:19:48 -07001148
Jérôme Glisse73231612019-05-13 17:19:58 -07001149 return (hmm_vma_walk.last - range->start) >> PAGE_SHIFT;
Jérôme Glisse74eee182017-09-08 16:11:35 -07001150}
Jérôme Glisse73231612019-05-13 17:19:58 -07001151EXPORT_SYMBOL(hmm_range_fault);
Jérôme Glisse55c0ece2019-05-13 17:20:28 -07001152
1153/**
1154 * hmm_range_dma_map() - hmm_range_fault() and dma map page all in one.
1155 * @range: range being faulted
1156 * @device: device against to dma map page to
1157 * @daddrs: dma address of mapped pages
1158 * @block: allow blocking on fault (if true it sleeps and do not drop mmap_sem)
Ralph Campbell085ea252019-05-06 16:29:39 -07001159 * Return: number of pages mapped on success, -EAGAIN if mmap_sem have been
Jérôme Glisse55c0ece2019-05-13 17:20:28 -07001160 * drop and you need to try again, some other error value otherwise
1161 *
1162 * Note same usage pattern as hmm_range_fault().
1163 */
1164long hmm_range_dma_map(struct hmm_range *range,
1165 struct device *device,
1166 dma_addr_t *daddrs,
1167 bool block)
1168{
1169 unsigned long i, npages, mapped;
1170 long ret;
1171
1172 ret = hmm_range_fault(range, block);
1173 if (ret <= 0)
1174 return ret ? ret : -EBUSY;
1175
1176 npages = (range->end - range->start) >> PAGE_SHIFT;
1177 for (i = 0, mapped = 0; i < npages; ++i) {
1178 enum dma_data_direction dir = DMA_TO_DEVICE;
1179 struct page *page;
1180
1181 /*
1182 * FIXME need to update DMA API to provide invalid DMA address
1183 * value instead of a function to test dma address value. This
1184 * would remove lot of dumb code duplicated accross many arch.
1185 *
1186 * For now setting it to 0 here is good enough as the pfns[]
1187 * value is what is use to check what is valid and what isn't.
1188 */
1189 daddrs[i] = 0;
1190
Jérôme Glisse391aab12019-05-13 17:20:31 -07001191 page = hmm_device_entry_to_page(range, range->pfns[i]);
Jérôme Glisse55c0ece2019-05-13 17:20:28 -07001192 if (page == NULL)
1193 continue;
1194
1195 /* Check if range is being invalidated */
1196 if (!range->valid) {
1197 ret = -EBUSY;
1198 goto unmap;
1199 }
1200
1201 /* If it is read and write than map bi-directional. */
1202 if (range->pfns[i] & range->flags[HMM_PFN_WRITE])
1203 dir = DMA_BIDIRECTIONAL;
1204
1205 daddrs[i] = dma_map_page(device, page, 0, PAGE_SIZE, dir);
1206 if (dma_mapping_error(device, daddrs[i])) {
1207 ret = -EFAULT;
1208 goto unmap;
1209 }
1210
1211 mapped++;
1212 }
1213
1214 return mapped;
1215
1216unmap:
1217 for (npages = i, i = 0; (i < npages) && mapped; ++i) {
1218 enum dma_data_direction dir = DMA_TO_DEVICE;
1219 struct page *page;
1220
Jérôme Glisse391aab12019-05-13 17:20:31 -07001221 page = hmm_device_entry_to_page(range, range->pfns[i]);
Jérôme Glisse55c0ece2019-05-13 17:20:28 -07001222 if (page == NULL)
1223 continue;
1224
1225 if (dma_mapping_error(device, daddrs[i]))
1226 continue;
1227
1228 /* If it is read and write than map bi-directional. */
1229 if (range->pfns[i] & range->flags[HMM_PFN_WRITE])
1230 dir = DMA_BIDIRECTIONAL;
1231
1232 dma_unmap_page(device, daddrs[i], PAGE_SIZE, dir);
1233 mapped--;
1234 }
1235
1236 return ret;
1237}
1238EXPORT_SYMBOL(hmm_range_dma_map);
1239
1240/**
1241 * hmm_range_dma_unmap() - unmap range of that was map with hmm_range_dma_map()
1242 * @range: range being unmapped
1243 * @vma: the vma against which the range (optional)
1244 * @device: device against which dma map was done
1245 * @daddrs: dma address of mapped pages
1246 * @dirty: dirty page if it had the write flag set
Ralph Campbell085ea252019-05-06 16:29:39 -07001247 * Return: number of page unmapped on success, -EINVAL otherwise
Jérôme Glisse55c0ece2019-05-13 17:20:28 -07001248 *
1249 * Note that caller MUST abide by mmu notifier or use HMM mirror and abide
1250 * to the sync_cpu_device_pagetables() callback so that it is safe here to
1251 * call set_page_dirty(). Caller must also take appropriate locks to avoid
1252 * concurrent mmu notifier or sync_cpu_device_pagetables() to make progress.
1253 */
1254long hmm_range_dma_unmap(struct hmm_range *range,
1255 struct vm_area_struct *vma,
1256 struct device *device,
1257 dma_addr_t *daddrs,
1258 bool dirty)
1259{
1260 unsigned long i, npages;
1261 long cpages = 0;
1262
1263 /* Sanity check. */
1264 if (range->end <= range->start)
1265 return -EINVAL;
1266 if (!daddrs)
1267 return -EINVAL;
1268 if (!range->pfns)
1269 return -EINVAL;
1270
1271 npages = (range->end - range->start) >> PAGE_SHIFT;
1272 for (i = 0; i < npages; ++i) {
1273 enum dma_data_direction dir = DMA_TO_DEVICE;
1274 struct page *page;
1275
Jérôme Glisse391aab12019-05-13 17:20:31 -07001276 page = hmm_device_entry_to_page(range, range->pfns[i]);
Jérôme Glisse55c0ece2019-05-13 17:20:28 -07001277 if (page == NULL)
1278 continue;
1279
1280 /* If it is read and write than map bi-directional. */
1281 if (range->pfns[i] & range->flags[HMM_PFN_WRITE]) {
1282 dir = DMA_BIDIRECTIONAL;
1283
1284 /*
1285 * See comments in function description on why it is
1286 * safe here to call set_page_dirty()
1287 */
1288 if (dirty)
1289 set_page_dirty(page);
1290 }
1291
1292 /* Unmap and clear pfns/dma address */
1293 dma_unmap_page(device, daddrs[i], PAGE_SIZE, dir);
1294 range->pfns[i] = range->values[HMM_PFN_NONE];
1295 /* FIXME see comments in hmm_vma_dma_map() */
1296 daddrs[i] = 0;
1297 cpages++;
1298 }
1299
1300 return cpages;
1301}
1302EXPORT_SYMBOL(hmm_range_dma_unmap);
Jérôme Glissec0b12402017-09-08 16:11:27 -07001303#endif /* IS_ENABLED(CONFIG_HMM_MIRROR) */
Jérôme Glisse4ef589d2017-09-08 16:11:58 -07001304
1305
Jérôme Glissedf6ad692017-09-08 16:12:24 -07001306#if IS_ENABLED(CONFIG_DEVICE_PRIVATE) || IS_ENABLED(CONFIG_DEVICE_PUBLIC)
Jérôme Glisse4ef589d2017-09-08 16:11:58 -07001307struct page *hmm_vma_alloc_locked_page(struct vm_area_struct *vma,
1308 unsigned long addr)
1309{
1310 struct page *page;
1311
1312 page = alloc_page_vma(GFP_HIGHUSER, vma, addr);
1313 if (!page)
1314 return NULL;
1315 lock_page(page);
1316 return page;
1317}
1318EXPORT_SYMBOL(hmm_vma_alloc_locked_page);
1319
1320
1321static void hmm_devmem_ref_release(struct percpu_ref *ref)
1322{
1323 struct hmm_devmem *devmem;
1324
1325 devmem = container_of(ref, struct hmm_devmem, ref);
1326 complete(&devmem->completion);
1327}
1328
1329static void hmm_devmem_ref_exit(void *data)
1330{
1331 struct percpu_ref *ref = data;
1332 struct hmm_devmem *devmem;
1333
1334 devmem = container_of(ref, struct hmm_devmem, ref);
Dan Williamsbbecd94e2018-12-28 00:35:11 -08001335 wait_for_completion(&devmem->completion);
Jérôme Glisse4ef589d2017-09-08 16:11:58 -07001336 percpu_ref_exit(ref);
Jérôme Glisse4ef589d2017-09-08 16:11:58 -07001337}
1338
Dan Williamsbbecd94e2018-12-28 00:35:11 -08001339static void hmm_devmem_ref_kill(struct percpu_ref *ref)
Jérôme Glisse4ef589d2017-09-08 16:11:58 -07001340{
Jérôme Glisse4ef589d2017-09-08 16:11:58 -07001341 percpu_ref_kill(ref);
Jérôme Glisse4ef589d2017-09-08 16:11:58 -07001342}
1343
Souptick Joarderb57e622e62019-03-11 23:28:10 -07001344static vm_fault_t hmm_devmem_fault(struct vm_area_struct *vma,
Jérôme Glisse4ef589d2017-09-08 16:11:58 -07001345 unsigned long addr,
1346 const struct page *page,
1347 unsigned int flags,
1348 pmd_t *pmdp)
1349{
1350 struct hmm_devmem *devmem = page->pgmap->data;
1351
1352 return devmem->ops->fault(devmem, vma, addr, page, flags, pmdp);
1353}
1354
1355static void hmm_devmem_free(struct page *page, void *data)
1356{
1357 struct hmm_devmem *devmem = data;
1358
Dan Williams2fa147b2018-07-13 21:50:01 -07001359 page->mapping = NULL;
1360
Jérôme Glisse4ef589d2017-09-08 16:11:58 -07001361 devmem->ops->free(devmem, page);
1362}
1363
Jérôme Glisse4ef589d2017-09-08 16:11:58 -07001364/*
1365 * hmm_devmem_add() - hotplug ZONE_DEVICE memory for device memory
1366 *
1367 * @ops: memory event device driver callback (see struct hmm_devmem_ops)
1368 * @device: device struct to bind the resource too
1369 * @size: size in bytes of the device memory to add
Ralph Campbell085ea252019-05-06 16:29:39 -07001370 * Return: pointer to new hmm_devmem struct ERR_PTR otherwise
Jérôme Glisse4ef589d2017-09-08 16:11:58 -07001371 *
1372 * This function first finds an empty range of physical address big enough to
1373 * contain the new resource, and then hotplugs it as ZONE_DEVICE memory, which
1374 * in turn allocates struct pages. It does not do anything beyond that; all
1375 * events affecting the memory will go through the various callbacks provided
1376 * by hmm_devmem_ops struct.
1377 *
1378 * Device driver should call this function during device initialization and
1379 * is then responsible of memory management. HMM only provides helpers.
1380 */
1381struct hmm_devmem *hmm_devmem_add(const struct hmm_devmem_ops *ops,
1382 struct device *device,
1383 unsigned long size)
1384{
1385 struct hmm_devmem *devmem;
1386 resource_size_t addr;
Dan Williamsbbecd94e2018-12-28 00:35:11 -08001387 void *result;
Jérôme Glisse4ef589d2017-09-08 16:11:58 -07001388 int ret;
1389
Dan Williamse76384882018-05-16 11:46:08 -07001390 dev_pagemap_get_ops();
Jérôme Glisse4ef589d2017-09-08 16:11:58 -07001391
Dan Williams58ef15b2018-12-28 00:35:07 -08001392 devmem = devm_kzalloc(device, sizeof(*devmem), GFP_KERNEL);
Jérôme Glisse4ef589d2017-09-08 16:11:58 -07001393 if (!devmem)
1394 return ERR_PTR(-ENOMEM);
1395
1396 init_completion(&devmem->completion);
1397 devmem->pfn_first = -1UL;
1398 devmem->pfn_last = -1UL;
1399 devmem->resource = NULL;
1400 devmem->device = device;
1401 devmem->ops = ops;
1402
1403 ret = percpu_ref_init(&devmem->ref, &hmm_devmem_ref_release,
1404 0, GFP_KERNEL);
1405 if (ret)
Dan Williams58ef15b2018-12-28 00:35:07 -08001406 return ERR_PTR(ret);
Jérôme Glisse4ef589d2017-09-08 16:11:58 -07001407
Dan Williams58ef15b2018-12-28 00:35:07 -08001408 ret = devm_add_action_or_reset(device, hmm_devmem_ref_exit, &devmem->ref);
Jérôme Glisse4ef589d2017-09-08 16:11:58 -07001409 if (ret)
Dan Williams58ef15b2018-12-28 00:35:07 -08001410 return ERR_PTR(ret);
Jérôme Glisse4ef589d2017-09-08 16:11:58 -07001411
1412 size = ALIGN(size, PA_SECTION_SIZE);
1413 addr = min((unsigned long)iomem_resource.end,
1414 (1UL << MAX_PHYSMEM_BITS) - 1);
1415 addr = addr - size + 1UL;
1416
1417 /*
1418 * FIXME add a new helper to quickly walk resource tree and find free
1419 * range
1420 *
1421 * FIXME what about ioport_resource resource ?
1422 */
1423 for (; addr > size && addr >= iomem_resource.start; addr -= size) {
1424 ret = region_intersects(addr, size, 0, IORES_DESC_NONE);
1425 if (ret != REGION_DISJOINT)
1426 continue;
1427
1428 devmem->resource = devm_request_mem_region(device, addr, size,
1429 dev_name(device));
Dan Williams58ef15b2018-12-28 00:35:07 -08001430 if (!devmem->resource)
1431 return ERR_PTR(-ENOMEM);
Jérôme Glisse4ef589d2017-09-08 16:11:58 -07001432 break;
1433 }
Dan Williams58ef15b2018-12-28 00:35:07 -08001434 if (!devmem->resource)
1435 return ERR_PTR(-ERANGE);
Jérôme Glisse4ef589d2017-09-08 16:11:58 -07001436
1437 devmem->resource->desc = IORES_DESC_DEVICE_PRIVATE_MEMORY;
1438 devmem->pfn_first = devmem->resource->start >> PAGE_SHIFT;
1439 devmem->pfn_last = devmem->pfn_first +
1440 (resource_size(devmem->resource) >> PAGE_SHIFT);
Dan Williams063a7d12018-12-28 00:39:46 -08001441 devmem->page_fault = hmm_devmem_fault;
Jérôme Glisse4ef589d2017-09-08 16:11:58 -07001442
Dan Williamsbbecd94e2018-12-28 00:35:11 -08001443 devmem->pagemap.type = MEMORY_DEVICE_PRIVATE;
1444 devmem->pagemap.res = *devmem->resource;
Dan Williamsbbecd94e2018-12-28 00:35:11 -08001445 devmem->pagemap.page_free = hmm_devmem_free;
1446 devmem->pagemap.altmap_valid = false;
1447 devmem->pagemap.ref = &devmem->ref;
1448 devmem->pagemap.data = devmem;
1449 devmem->pagemap.kill = hmm_devmem_ref_kill;
Dan Williams58ef15b2018-12-28 00:35:07 -08001450
Dan Williamsbbecd94e2018-12-28 00:35:11 -08001451 result = devm_memremap_pages(devmem->device, &devmem->pagemap);
1452 if (IS_ERR(result))
1453 return result;
Jérôme Glisse4ef589d2017-09-08 16:11:58 -07001454 return devmem;
Jérôme Glisse4ef589d2017-09-08 16:11:58 -07001455}
Dan Williams02917e92018-12-28 00:35:15 -08001456EXPORT_SYMBOL_GPL(hmm_devmem_add);
Jérôme Glisse4ef589d2017-09-08 16:11:58 -07001457
Jérôme Glissed3df0a42017-09-08 16:12:28 -07001458struct hmm_devmem *hmm_devmem_add_resource(const struct hmm_devmem_ops *ops,
1459 struct device *device,
1460 struct resource *res)
1461{
1462 struct hmm_devmem *devmem;
Dan Williamsbbecd94e2018-12-28 00:35:11 -08001463 void *result;
Jérôme Glissed3df0a42017-09-08 16:12:28 -07001464 int ret;
1465
1466 if (res->desc != IORES_DESC_DEVICE_PUBLIC_MEMORY)
1467 return ERR_PTR(-EINVAL);
1468
Dan Williamse76384882018-05-16 11:46:08 -07001469 dev_pagemap_get_ops();
Jérôme Glissed3df0a42017-09-08 16:12:28 -07001470
Dan Williams58ef15b2018-12-28 00:35:07 -08001471 devmem = devm_kzalloc(device, sizeof(*devmem), GFP_KERNEL);
Jérôme Glissed3df0a42017-09-08 16:12:28 -07001472 if (!devmem)
1473 return ERR_PTR(-ENOMEM);
1474
1475 init_completion(&devmem->completion);
1476 devmem->pfn_first = -1UL;
1477 devmem->pfn_last = -1UL;
1478 devmem->resource = res;
1479 devmem->device = device;
1480 devmem->ops = ops;
1481
1482 ret = percpu_ref_init(&devmem->ref, &hmm_devmem_ref_release,
1483 0, GFP_KERNEL);
1484 if (ret)
Dan Williams58ef15b2018-12-28 00:35:07 -08001485 return ERR_PTR(ret);
Jérôme Glissed3df0a42017-09-08 16:12:28 -07001486
Dan Williams58ef15b2018-12-28 00:35:07 -08001487 ret = devm_add_action_or_reset(device, hmm_devmem_ref_exit,
1488 &devmem->ref);
Jérôme Glissed3df0a42017-09-08 16:12:28 -07001489 if (ret)
Dan Williams58ef15b2018-12-28 00:35:07 -08001490 return ERR_PTR(ret);
Jérôme Glissed3df0a42017-09-08 16:12:28 -07001491
1492 devmem->pfn_first = devmem->resource->start >> PAGE_SHIFT;
1493 devmem->pfn_last = devmem->pfn_first +
1494 (resource_size(devmem->resource) >> PAGE_SHIFT);
Dan Williams063a7d12018-12-28 00:39:46 -08001495 devmem->page_fault = hmm_devmem_fault;
Jérôme Glissed3df0a42017-09-08 16:12:28 -07001496
Dan Williamsbbecd94e2018-12-28 00:35:11 -08001497 devmem->pagemap.type = MEMORY_DEVICE_PUBLIC;
1498 devmem->pagemap.res = *devmem->resource;
Dan Williamsbbecd94e2018-12-28 00:35:11 -08001499 devmem->pagemap.page_free = hmm_devmem_free;
1500 devmem->pagemap.altmap_valid = false;
1501 devmem->pagemap.ref = &devmem->ref;
1502 devmem->pagemap.data = devmem;
1503 devmem->pagemap.kill = hmm_devmem_ref_kill;
Dan Williams58ef15b2018-12-28 00:35:07 -08001504
Dan Williamsbbecd94e2018-12-28 00:35:11 -08001505 result = devm_memremap_pages(devmem->device, &devmem->pagemap);
1506 if (IS_ERR(result))
1507 return result;
Jérôme Glissed3df0a42017-09-08 16:12:28 -07001508 return devmem;
Jérôme Glissed3df0a42017-09-08 16:12:28 -07001509}
Dan Williams02917e92018-12-28 00:35:15 -08001510EXPORT_SYMBOL_GPL(hmm_devmem_add_resource);
Jérôme Glissed3df0a42017-09-08 16:12:28 -07001511
Jérôme Glisse4ef589d2017-09-08 16:11:58 -07001512/*
Jérôme Glisse858b54d2017-09-08 16:12:02 -07001513 * A device driver that wants to handle multiple devices memory through a
1514 * single fake device can use hmm_device to do so. This is purely a helper
1515 * and it is not needed to make use of any HMM functionality.
1516 */
1517#define HMM_DEVICE_MAX 256
1518
1519static DECLARE_BITMAP(hmm_device_mask, HMM_DEVICE_MAX);
1520static DEFINE_SPINLOCK(hmm_device_lock);
1521static struct class *hmm_device_class;
1522static dev_t hmm_device_devt;
1523
1524static void hmm_device_release(struct device *device)
1525{
1526 struct hmm_device *hmm_device;
1527
1528 hmm_device = container_of(device, struct hmm_device, device);
1529 spin_lock(&hmm_device_lock);
1530 clear_bit(hmm_device->minor, hmm_device_mask);
1531 spin_unlock(&hmm_device_lock);
1532
1533 kfree(hmm_device);
1534}
1535
1536struct hmm_device *hmm_device_new(void *drvdata)
1537{
1538 struct hmm_device *hmm_device;
1539
1540 hmm_device = kzalloc(sizeof(*hmm_device), GFP_KERNEL);
1541 if (!hmm_device)
1542 return ERR_PTR(-ENOMEM);
1543
1544 spin_lock(&hmm_device_lock);
1545 hmm_device->minor = find_first_zero_bit(hmm_device_mask, HMM_DEVICE_MAX);
1546 if (hmm_device->minor >= HMM_DEVICE_MAX) {
1547 spin_unlock(&hmm_device_lock);
1548 kfree(hmm_device);
1549 return ERR_PTR(-EBUSY);
1550 }
1551 set_bit(hmm_device->minor, hmm_device_mask);
1552 spin_unlock(&hmm_device_lock);
1553
1554 dev_set_name(&hmm_device->device, "hmm_device%d", hmm_device->minor);
1555 hmm_device->device.devt = MKDEV(MAJOR(hmm_device_devt),
1556 hmm_device->minor);
1557 hmm_device->device.release = hmm_device_release;
1558 dev_set_drvdata(&hmm_device->device, drvdata);
1559 hmm_device->device.class = hmm_device_class;
1560 device_initialize(&hmm_device->device);
1561
1562 return hmm_device;
1563}
1564EXPORT_SYMBOL(hmm_device_new);
1565
1566void hmm_device_put(struct hmm_device *hmm_device)
1567{
1568 put_device(&hmm_device->device);
1569}
1570EXPORT_SYMBOL(hmm_device_put);
1571
1572static int __init hmm_init(void)
1573{
1574 int ret;
1575
1576 ret = alloc_chrdev_region(&hmm_device_devt, 0,
1577 HMM_DEVICE_MAX,
1578 "hmm_device");
1579 if (ret)
1580 return ret;
1581
1582 hmm_device_class = class_create(THIS_MODULE, "hmm_device");
1583 if (IS_ERR(hmm_device_class)) {
1584 unregister_chrdev_region(hmm_device_devt, HMM_DEVICE_MAX);
1585 return PTR_ERR(hmm_device_class);
1586 }
1587 return 0;
1588}
1589
1590device_initcall(hmm_init);
Jérôme Glissedf6ad692017-09-08 16:12:24 -07001591#endif /* CONFIG_DEVICE_PRIVATE || CONFIG_DEVICE_PUBLIC */