blob: 2ef14b2b5505f69309c43dfd0c60105288b5c358 [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;
Jérôme Glissec0b12402017-09-08 16:11:27 -070070 hmm->mm = mm;
Jason Gunthorpe8a9320b2019-05-23 10:24:13 -030071
72 hmm->mmu_notifier.ops = &hmm_mmu_notifier_ops;
73 if (__mmu_notifier_register(&hmm->mmu_notifier, mm)) {
74 kfree(hmm);
75 return NULL;
76 }
77
Jason Gunthorpec8a53b22019-05-23 10:36:46 -030078 mmgrab(hmm->mm);
Jérôme Glissec0b12402017-09-08 16:11:27 -070079
Ralph Campbell86a2d592018-10-30 15:04:14 -070080 /*
Jason Gunthorpe8a9320b2019-05-23 10:24:13 -030081 * We hold the exclusive mmap_sem here so we know that mm->hmm is
82 * still NULL or 0 kref, and is safe to update.
Ralph Campbell86a2d592018-10-30 15:04:14 -070083 */
Ralph Campbell86a2d592018-10-30 15:04:14 -070084 spin_lock(&mm->page_table_lock);
Jason Gunthorpe8a9320b2019-05-23 10:24:13 -030085 mm->hmm = hmm;
86
87out_unlock:
Ralph Campbell86a2d592018-10-30 15:04:14 -070088 spin_unlock(&mm->page_table_lock);
Jason Gunthorpe8a9320b2019-05-23 10:24:13 -030089 return hmm;
Jérôme Glisse133ff0e2017-09-08 16:11:23 -070090}
91
Jason Gunthorpe6d7c3cd2019-05-22 16:52:52 -030092static void hmm_free_rcu(struct rcu_head *rcu)
93{
Jason Gunthorpe8a9320b2019-05-23 10:24:13 -030094 struct hmm *hmm = container_of(rcu, struct hmm, rcu);
95
96 mmdrop(hmm->mm);
97 kfree(hmm);
Jason Gunthorpe6d7c3cd2019-05-22 16:52:52 -030098}
99
Jérôme Glisse704f3f22019-05-13 17:19:48 -0700100static void hmm_free(struct kref *kref)
101{
102 struct hmm *hmm = container_of(kref, struct hmm, kref);
Jérôme Glisse704f3f22019-05-13 17:19:48 -0700103
Jason Gunthorpe8a9320b2019-05-23 10:24:13 -0300104 spin_lock(&hmm->mm->page_table_lock);
105 if (hmm->mm->hmm == hmm)
106 hmm->mm->hmm = NULL;
107 spin_unlock(&hmm->mm->page_table_lock);
Jérôme Glisse704f3f22019-05-13 17:19:48 -0700108
Jason Gunthorpe8a9320b2019-05-23 10:24:13 -0300109 mmu_notifier_unregister_no_release(&hmm->mmu_notifier, hmm->mm);
Jason Gunthorpe6d7c3cd2019-05-22 16:52:52 -0300110 mmu_notifier_call_srcu(&hmm->rcu, hmm_free_rcu);
Jérôme Glisse704f3f22019-05-13 17:19:48 -0700111}
112
113static inline void hmm_put(struct hmm *hmm)
114{
115 kref_put(&hmm->kref, hmm_free);
116}
117
Jérôme Glissea3e0d412019-05-13 17:20:01 -0700118static void hmm_release(struct mmu_notifier *mn, struct mm_struct *mm)
Jérôme Glissec0b12402017-09-08 16:11:27 -0700119{
Jason Gunthorpe6d7c3cd2019-05-22 16:52:52 -0300120 struct hmm *hmm = container_of(mn, struct hmm, mmu_notifier);
Jérôme Glissec0b12402017-09-08 16:11:27 -0700121 struct hmm_mirror *mirror;
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700122
Jason Gunthorpe6d7c3cd2019-05-22 16:52:52 -0300123 /* Bail out if hmm is in the process of being freed */
124 if (!kref_get_unless_zero(&hmm->kref))
125 return;
126
Jason Gunthorpe47f24592019-05-23 11:08:28 -0300127 /*
128 * Since hmm_range_register() holds the mmget() lock hmm_release() is
129 * prevented as long as a range exists.
130 */
131 WARN_ON(!list_empty_careful(&hmm->ranges));
Ralph Campbelle1401512018-04-10 16:28:19 -0700132
133 down_write(&hmm->mirrors_sem);
134 mirror = list_first_entry_or_null(&hmm->mirrors, struct hmm_mirror,
135 list);
136 while (mirror) {
137 list_del_init(&mirror->list);
138 if (mirror->ops->release) {
139 /*
Ralph Campbell085ea252019-05-06 16:29:39 -0700140 * Drop mirrors_sem so the release callback can wait
141 * on any pending work that might itself trigger a
142 * mmu_notifier callback and thus would deadlock with
143 * us.
Ralph Campbelle1401512018-04-10 16:28:19 -0700144 */
145 up_write(&hmm->mirrors_sem);
146 mirror->ops->release(mirror);
147 down_write(&hmm->mirrors_sem);
148 }
149 mirror = list_first_entry_or_null(&hmm->mirrors,
150 struct hmm_mirror, list);
151 }
152 up_write(&hmm->mirrors_sem);
Jérôme Glisse704f3f22019-05-13 17:19:48 -0700153
154 hmm_put(hmm);
Ralph Campbelle1401512018-04-10 16:28:19 -0700155}
156
Michal Hocko93065ac2018-08-21 21:52:33 -0700157static int hmm_invalidate_range_start(struct mmu_notifier *mn,
Jérôme Glissea3e0d412019-05-13 17:20:01 -0700158 const struct mmu_notifier_range *nrange)
Jérôme Glissec0b12402017-09-08 16:11:27 -0700159{
Jason Gunthorpe6d7c3cd2019-05-22 16:52:52 -0300160 struct hmm *hmm = container_of(mn, struct hmm, mmu_notifier);
Jérôme Glissea3e0d412019-05-13 17:20:01 -0700161 struct hmm_mirror *mirror;
Jérôme Glisseec131b22018-10-30 15:04:28 -0700162 struct hmm_update update;
Jérôme Glissea3e0d412019-05-13 17:20:01 -0700163 struct hmm_range *range;
164 int ret = 0;
Jérôme Glissec0b12402017-09-08 16:11:27 -0700165
Jason Gunthorpe6d7c3cd2019-05-22 16:52:52 -0300166 if (!kref_get_unless_zero(&hmm->kref))
167 return 0;
Jérôme Glissec0b12402017-09-08 16:11:27 -0700168
Jérôme Glissea3e0d412019-05-13 17:20:01 -0700169 update.start = nrange->start;
170 update.end = nrange->end;
Jérôme Glisseec131b22018-10-30 15:04:28 -0700171 update.event = HMM_UPDATE_INVALIDATE;
Jérôme Glissedfcd6662019-05-13 17:20:38 -0700172 update.blockable = mmu_notifier_range_blockable(nrange);
Jérôme Glissea3e0d412019-05-13 17:20:01 -0700173
Jérôme Glissedfcd6662019-05-13 17:20:38 -0700174 if (mmu_notifier_range_blockable(nrange))
Jérôme Glissea3e0d412019-05-13 17:20:01 -0700175 mutex_lock(&hmm->lock);
176 else if (!mutex_trylock(&hmm->lock)) {
177 ret = -EAGAIN;
178 goto out;
179 }
180 hmm->notifiers++;
181 list_for_each_entry(range, &hmm->ranges, list) {
182 if (update.end < range->start || update.start >= range->end)
183 continue;
184
185 range->valid = false;
186 }
187 mutex_unlock(&hmm->lock);
188
Jérôme Glissedfcd6662019-05-13 17:20:38 -0700189 if (mmu_notifier_range_blockable(nrange))
Jérôme Glissea3e0d412019-05-13 17:20:01 -0700190 down_read(&hmm->mirrors_sem);
191 else if (!down_read_trylock(&hmm->mirrors_sem)) {
192 ret = -EAGAIN;
193 goto out;
194 }
195 list_for_each_entry(mirror, &hmm->mirrors, list) {
196 int ret;
197
198 ret = mirror->ops->sync_cpu_device_pagetables(mirror, &update);
Ralph Campbell085ea252019-05-06 16:29:39 -0700199 if (!update.blockable && ret == -EAGAIN)
200 break;
Jérôme Glissea3e0d412019-05-13 17:20:01 -0700201 }
202 up_read(&hmm->mirrors_sem);
203
204out:
Jérôme Glisse704f3f22019-05-13 17:19:48 -0700205 hmm_put(hmm);
206 return ret;
Jérôme Glissec0b12402017-09-08 16:11:27 -0700207}
208
209static void hmm_invalidate_range_end(struct mmu_notifier *mn,
Jérôme Glissea3e0d412019-05-13 17:20:01 -0700210 const struct mmu_notifier_range *nrange)
Jérôme Glissec0b12402017-09-08 16:11:27 -0700211{
Jason Gunthorpe6d7c3cd2019-05-22 16:52:52 -0300212 struct hmm *hmm = container_of(mn, struct hmm, mmu_notifier);
Jérôme Glissec0b12402017-09-08 16:11:27 -0700213
Jason Gunthorpe6d7c3cd2019-05-22 16:52:52 -0300214 if (!kref_get_unless_zero(&hmm->kref))
215 return;
Jérôme Glissec0b12402017-09-08 16:11:27 -0700216
Jérôme Glissea3e0d412019-05-13 17:20:01 -0700217 mutex_lock(&hmm->lock);
218 hmm->notifiers--;
219 if (!hmm->notifiers) {
220 struct hmm_range *range;
221
222 list_for_each_entry(range, &hmm->ranges, list) {
223 if (range->valid)
224 continue;
225 range->valid = true;
226 }
227 wake_up_all(&hmm->wq);
228 }
229 mutex_unlock(&hmm->lock);
230
Jérôme Glisse704f3f22019-05-13 17:19:48 -0700231 hmm_put(hmm);
Jérôme Glissec0b12402017-09-08 16:11:27 -0700232}
233
234static const struct mmu_notifier_ops hmm_mmu_notifier_ops = {
Ralph Campbelle1401512018-04-10 16:28:19 -0700235 .release = hmm_release,
Jérôme Glissec0b12402017-09-08 16:11:27 -0700236 .invalidate_range_start = hmm_invalidate_range_start,
237 .invalidate_range_end = hmm_invalidate_range_end,
238};
239
240/*
241 * hmm_mirror_register() - register a mirror against an mm
242 *
243 * @mirror: new mirror struct to register
244 * @mm: mm to register against
Ralph Campbell085ea252019-05-06 16:29:39 -0700245 * Return: 0 on success, -ENOMEM if no memory, -EINVAL if invalid arguments
Jérôme Glissec0b12402017-09-08 16:11:27 -0700246 *
247 * To start mirroring a process address space, the device driver must register
248 * an HMM mirror struct.
Jérôme Glissec0b12402017-09-08 16:11:27 -0700249 */
250int hmm_mirror_register(struct hmm_mirror *mirror, struct mm_struct *mm)
251{
Jason Gunthorpe8a1a0cd2019-05-23 11:23:30 -0300252 lockdep_assert_held_exclusive(&mm->mmap_sem);
253
Jérôme Glissec0b12402017-09-08 16:11:27 -0700254 /* Sanity check */
255 if (!mm || !mirror || !mirror->ops)
256 return -EINVAL;
257
Jérôme Glisse704f3f22019-05-13 17:19:48 -0700258 mirror->hmm = hmm_get_or_create(mm);
Jérôme Glissec0b12402017-09-08 16:11:27 -0700259 if (!mirror->hmm)
260 return -ENOMEM;
261
262 down_write(&mirror->hmm->mirrors_sem);
Jérôme Glisse704f3f22019-05-13 17:19:48 -0700263 list_add(&mirror->list, &mirror->hmm->mirrors);
264 up_write(&mirror->hmm->mirrors_sem);
Jérôme Glissec0b12402017-09-08 16:11:27 -0700265
266 return 0;
267}
268EXPORT_SYMBOL(hmm_mirror_register);
269
270/*
271 * hmm_mirror_unregister() - unregister a mirror
272 *
Ralph Campbell085ea252019-05-06 16:29:39 -0700273 * @mirror: mirror struct to unregister
Jérôme Glissec0b12402017-09-08 16:11:27 -0700274 *
275 * Stop mirroring a process address space, and cleanup.
276 */
277void hmm_mirror_unregister(struct hmm_mirror *mirror)
278{
Jason Gunthorpe187229c2019-05-23 11:31:45 -0300279 struct hmm *hmm = mirror->hmm;
Jérôme Glissec01cbba2018-04-10 16:28:23 -0700280
Jérôme Glissec0b12402017-09-08 16:11:27 -0700281 down_write(&hmm->mirrors_sem);
Ralph Campbelle1401512018-04-10 16:28:19 -0700282 list_del_init(&mirror->list);
Jérôme Glissec0b12402017-09-08 16:11:27 -0700283 up_write(&hmm->mirrors_sem);
Jérôme Glisse704f3f22019-05-13 17:19:48 -0700284 hmm_put(hmm);
Jérôme Glissec0b12402017-09-08 16:11:27 -0700285}
286EXPORT_SYMBOL(hmm_mirror_unregister);
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700287
Jérôme Glisse74eee182017-09-08 16:11:35 -0700288struct hmm_vma_walk {
289 struct hmm_range *range;
Jérôme Glisse992de9a2019-05-13 17:20:21 -0700290 struct dev_pagemap *pgmap;
Jérôme Glisse74eee182017-09-08 16:11:35 -0700291 unsigned long last;
292 bool fault;
293 bool block;
Jérôme Glisse74eee182017-09-08 16:11:35 -0700294};
295
Jérôme Glisse2aee09d2018-04-10 16:29:02 -0700296static int hmm_vma_do_fault(struct mm_walk *walk, unsigned long addr,
297 bool write_fault, uint64_t *pfn)
Jérôme Glisse74eee182017-09-08 16:11:35 -0700298{
Kuehling, Felix9b1ae602019-05-10 19:53:24 +0000299 unsigned int flags = FAULT_FLAG_REMOTE;
Jérôme Glisse74eee182017-09-08 16:11:35 -0700300 struct hmm_vma_walk *hmm_vma_walk = walk->private;
Jérôme Glissef88a1e92018-04-10 16:29:06 -0700301 struct hmm_range *range = hmm_vma_walk->range;
Jérôme Glisse74eee182017-09-08 16:11:35 -0700302 struct vm_area_struct *vma = walk->vma;
Souptick Joarder50a7ca32018-08-17 15:44:47 -0700303 vm_fault_t ret;
Jérôme Glisse74eee182017-09-08 16:11:35 -0700304
305 flags |= hmm_vma_walk->block ? 0 : FAULT_FLAG_ALLOW_RETRY;
Jérôme Glisse2aee09d2018-04-10 16:29:02 -0700306 flags |= write_fault ? FAULT_FLAG_WRITE : 0;
Souptick Joarder50a7ca32018-08-17 15:44:47 -0700307 ret = handle_mm_fault(vma, addr, flags);
308 if (ret & VM_FAULT_RETRY)
Jérôme Glisse73231612019-05-13 17:19:58 -0700309 return -EAGAIN;
Souptick Joarder50a7ca32018-08-17 15:44:47 -0700310 if (ret & VM_FAULT_ERROR) {
Jérôme Glissef88a1e92018-04-10 16:29:06 -0700311 *pfn = range->values[HMM_PFN_ERROR];
Jérôme Glisse74eee182017-09-08 16:11:35 -0700312 return -EFAULT;
313 }
314
Jérôme Glisse73231612019-05-13 17:19:58 -0700315 return -EBUSY;
Jérôme Glisse74eee182017-09-08 16:11:35 -0700316}
317
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700318static int hmm_pfns_bad(unsigned long addr,
319 unsigned long end,
320 struct mm_walk *walk)
321{
Jérôme Glissec7195472018-04-10 16:28:27 -0700322 struct hmm_vma_walk *hmm_vma_walk = walk->private;
323 struct hmm_range *range = hmm_vma_walk->range;
Jérôme Glisseff05c0c2018-04-10 16:28:38 -0700324 uint64_t *pfns = range->pfns;
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700325 unsigned long i;
326
327 i = (addr - range->start) >> PAGE_SHIFT;
328 for (; addr < end; addr += PAGE_SIZE, i++)
Jérôme Glissef88a1e92018-04-10 16:29:06 -0700329 pfns[i] = range->values[HMM_PFN_ERROR];
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700330
331 return 0;
332}
333
Jérôme Glisse5504ed22018-04-10 16:28:46 -0700334/*
335 * hmm_vma_walk_hole() - handle a range lacking valid pmd or pte(s)
336 * @start: range virtual start address (inclusive)
337 * @end: range virtual end address (exclusive)
Jérôme Glisse2aee09d2018-04-10 16:29:02 -0700338 * @fault: should we fault or not ?
339 * @write_fault: write fault ?
Jérôme Glisse5504ed22018-04-10 16:28:46 -0700340 * @walk: mm_walk structure
Ralph Campbell085ea252019-05-06 16:29:39 -0700341 * Return: 0 on success, -EBUSY after page fault, or page fault error
Jérôme Glisse5504ed22018-04-10 16:28:46 -0700342 *
343 * This function will be called whenever pmd_none() or pte_none() returns true,
344 * or whenever there is no page directory covering the virtual address range.
345 */
Jérôme Glisse2aee09d2018-04-10 16:29:02 -0700346static int hmm_vma_walk_hole_(unsigned long addr, unsigned long end,
347 bool fault, bool write_fault,
348 struct mm_walk *walk)
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700349{
Jérôme Glisse74eee182017-09-08 16:11:35 -0700350 struct hmm_vma_walk *hmm_vma_walk = walk->private;
351 struct hmm_range *range = hmm_vma_walk->range;
Jérôme Glisseff05c0c2018-04-10 16:28:38 -0700352 uint64_t *pfns = range->pfns;
Jérôme Glisse63d50662019-05-13 17:20:18 -0700353 unsigned long i, page_size;
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700354
Jérôme Glisse74eee182017-09-08 16:11:35 -0700355 hmm_vma_walk->last = addr;
Jérôme Glisse63d50662019-05-13 17:20:18 -0700356 page_size = hmm_range_page_size(range);
357 i = (addr - range->start) >> range->page_shift;
358
359 for (; addr < end; addr += page_size, i++) {
Jérôme Glissef88a1e92018-04-10 16:29:06 -0700360 pfns[i] = range->values[HMM_PFN_NONE];
Jérôme Glisse2aee09d2018-04-10 16:29:02 -0700361 if (fault || write_fault) {
Jérôme Glisse74eee182017-09-08 16:11:35 -0700362 int ret;
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700363
Jérôme Glisse2aee09d2018-04-10 16:29:02 -0700364 ret = hmm_vma_do_fault(walk, addr, write_fault,
365 &pfns[i]);
Jérôme Glisse73231612019-05-13 17:19:58 -0700366 if (ret != -EBUSY)
Jérôme Glisse74eee182017-09-08 16:11:35 -0700367 return ret;
368 }
369 }
370
Jérôme Glisse73231612019-05-13 17:19:58 -0700371 return (fault || write_fault) ? -EBUSY : 0;
Jérôme Glisse2aee09d2018-04-10 16:29:02 -0700372}
373
374static inline void hmm_pte_need_fault(const struct hmm_vma_walk *hmm_vma_walk,
375 uint64_t pfns, uint64_t cpu_flags,
376 bool *fault, bool *write_fault)
377{
Jérôme Glissef88a1e92018-04-10 16:29:06 -0700378 struct hmm_range *range = hmm_vma_walk->range;
379
Jérôme Glisse2aee09d2018-04-10 16:29:02 -0700380 if (!hmm_vma_walk->fault)
381 return;
382
Jérôme Glisse023a0192019-05-13 17:20:05 -0700383 /*
384 * So we not only consider the individual per page request we also
385 * consider the default flags requested for the range. The API can
386 * be use in 2 fashions. The first one where the HMM user coalesce
387 * multiple page fault into one request and set flags per pfns for
388 * of those faults. The second one where the HMM user want to pre-
389 * fault a range with specific flags. For the latter one it is a
390 * waste to have the user pre-fill the pfn arrays with a default
391 * flags value.
392 */
393 pfns = (pfns & range->pfn_flags_mask) | range->default_flags;
394
Jérôme Glisse2aee09d2018-04-10 16:29:02 -0700395 /* We aren't ask to do anything ... */
Jérôme Glissef88a1e92018-04-10 16:29:06 -0700396 if (!(pfns & range->flags[HMM_PFN_VALID]))
Jérôme Glisse2aee09d2018-04-10 16:29:02 -0700397 return;
Jérôme Glissef88a1e92018-04-10 16:29:06 -0700398 /* If this is device memory than only fault if explicitly requested */
399 if ((cpu_flags & range->flags[HMM_PFN_DEVICE_PRIVATE])) {
400 /* Do we fault on device memory ? */
401 if (pfns & range->flags[HMM_PFN_DEVICE_PRIVATE]) {
402 *write_fault = pfns & range->flags[HMM_PFN_WRITE];
403 *fault = true;
404 }
Jérôme Glisse2aee09d2018-04-10 16:29:02 -0700405 return;
406 }
Jérôme Glissef88a1e92018-04-10 16:29:06 -0700407
408 /* If CPU page table is not valid then we need to fault */
409 *fault = !(cpu_flags & range->flags[HMM_PFN_VALID]);
410 /* Need to write fault ? */
411 if ((pfns & range->flags[HMM_PFN_WRITE]) &&
412 !(cpu_flags & range->flags[HMM_PFN_WRITE])) {
413 *write_fault = true;
Jérôme Glisse2aee09d2018-04-10 16:29:02 -0700414 *fault = true;
415 }
416}
417
418static void hmm_range_need_fault(const struct hmm_vma_walk *hmm_vma_walk,
419 const uint64_t *pfns, unsigned long npages,
420 uint64_t cpu_flags, bool *fault,
421 bool *write_fault)
422{
423 unsigned long i;
424
425 if (!hmm_vma_walk->fault) {
426 *fault = *write_fault = false;
427 return;
428 }
429
Jérôme Glissea3e0d412019-05-13 17:20:01 -0700430 *fault = *write_fault = false;
Jérôme Glisse2aee09d2018-04-10 16:29:02 -0700431 for (i = 0; i < npages; ++i) {
432 hmm_pte_need_fault(hmm_vma_walk, pfns[i], cpu_flags,
433 fault, write_fault);
Jérôme Glissea3e0d412019-05-13 17:20:01 -0700434 if ((*write_fault))
Jérôme Glisse2aee09d2018-04-10 16:29:02 -0700435 return;
436 }
437}
438
439static int hmm_vma_walk_hole(unsigned long addr, unsigned long end,
440 struct mm_walk *walk)
441{
442 struct hmm_vma_walk *hmm_vma_walk = walk->private;
443 struct hmm_range *range = hmm_vma_walk->range;
444 bool fault, write_fault;
445 unsigned long i, npages;
446 uint64_t *pfns;
447
448 i = (addr - range->start) >> PAGE_SHIFT;
449 npages = (end - addr) >> PAGE_SHIFT;
450 pfns = &range->pfns[i];
451 hmm_range_need_fault(hmm_vma_walk, pfns, npages,
452 0, &fault, &write_fault);
453 return hmm_vma_walk_hole_(addr, end, fault, write_fault, walk);
454}
455
Jérôme Glissef88a1e92018-04-10 16:29:06 -0700456static inline uint64_t pmd_to_hmm_pfn_flags(struct hmm_range *range, pmd_t pmd)
Jérôme Glisse2aee09d2018-04-10 16:29:02 -0700457{
458 if (pmd_protnone(pmd))
459 return 0;
Jérôme Glissef88a1e92018-04-10 16:29:06 -0700460 return pmd_write(pmd) ? range->flags[HMM_PFN_VALID] |
461 range->flags[HMM_PFN_WRITE] :
462 range->flags[HMM_PFN_VALID];
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700463}
464
Jérôme Glisse992de9a2019-05-13 17:20:21 -0700465static inline uint64_t pud_to_hmm_pfn_flags(struct hmm_range *range, pud_t pud)
466{
467 if (!pud_present(pud))
468 return 0;
469 return pud_write(pud) ? range->flags[HMM_PFN_VALID] |
470 range->flags[HMM_PFN_WRITE] :
471 range->flags[HMM_PFN_VALID];
472}
473
Jérôme Glisse53f5c3f2018-04-10 16:28:59 -0700474static int hmm_vma_handle_pmd(struct mm_walk *walk,
475 unsigned long addr,
476 unsigned long end,
477 uint64_t *pfns,
478 pmd_t pmd)
479{
Jérôme Glisse992de9a2019-05-13 17:20:21 -0700480#ifdef CONFIG_TRANSPARENT_HUGEPAGE
Jérôme Glisse53f5c3f2018-04-10 16:28:59 -0700481 struct hmm_vma_walk *hmm_vma_walk = walk->private;
Jérôme Glissef88a1e92018-04-10 16:29:06 -0700482 struct hmm_range *range = hmm_vma_walk->range;
Jérôme Glisse2aee09d2018-04-10 16:29:02 -0700483 unsigned long pfn, npages, i;
Jérôme Glisse2aee09d2018-04-10 16:29:02 -0700484 bool fault, write_fault;
Jérôme Glissef88a1e92018-04-10 16:29:06 -0700485 uint64_t cpu_flags;
Jérôme Glisse53f5c3f2018-04-10 16:28:59 -0700486
Jérôme Glisse2aee09d2018-04-10 16:29:02 -0700487 npages = (end - addr) >> PAGE_SHIFT;
Jérôme Glissef88a1e92018-04-10 16:29:06 -0700488 cpu_flags = pmd_to_hmm_pfn_flags(range, pmd);
Jérôme Glisse2aee09d2018-04-10 16:29:02 -0700489 hmm_range_need_fault(hmm_vma_walk, pfns, npages, cpu_flags,
490 &fault, &write_fault);
Jérôme Glisse53f5c3f2018-04-10 16:28:59 -0700491
Jérôme Glisse2aee09d2018-04-10 16:29:02 -0700492 if (pmd_protnone(pmd) || fault || write_fault)
493 return hmm_vma_walk_hole_(addr, end, fault, write_fault, walk);
Jérôme Glisse53f5c3f2018-04-10 16:28:59 -0700494
495 pfn = pmd_pfn(pmd) + pte_index(addr);
Jérôme Glisse992de9a2019-05-13 17:20:21 -0700496 for (i = 0; addr < end; addr += PAGE_SIZE, i++, pfn++) {
497 if (pmd_devmap(pmd)) {
498 hmm_vma_walk->pgmap = get_dev_pagemap(pfn,
499 hmm_vma_walk->pgmap);
500 if (unlikely(!hmm_vma_walk->pgmap))
501 return -EBUSY;
502 }
Jérôme Glisse391aab12019-05-13 17:20:31 -0700503 pfns[i] = hmm_device_entry_from_pfn(range, pfn) | cpu_flags;
Jérôme Glisse992de9a2019-05-13 17:20:21 -0700504 }
505 if (hmm_vma_walk->pgmap) {
506 put_dev_pagemap(hmm_vma_walk->pgmap);
507 hmm_vma_walk->pgmap = NULL;
508 }
Jérôme Glisse53f5c3f2018-04-10 16:28:59 -0700509 hmm_vma_walk->last = end;
510 return 0;
Jérôme Glisse992de9a2019-05-13 17:20:21 -0700511#else
512 /* If THP is not enabled then we should never reach that code ! */
513 return -EINVAL;
514#endif
Jérôme Glisse53f5c3f2018-04-10 16:28:59 -0700515}
516
Jérôme Glissef88a1e92018-04-10 16:29:06 -0700517static inline uint64_t pte_to_hmm_pfn_flags(struct hmm_range *range, pte_t pte)
Jérôme Glisse2aee09d2018-04-10 16:29:02 -0700518{
Philip Yang789c2af2019-05-23 16:32:31 -0400519 if (pte_none(pte) || !pte_present(pte) || pte_protnone(pte))
Jérôme Glisse2aee09d2018-04-10 16:29:02 -0700520 return 0;
Jérôme Glissef88a1e92018-04-10 16:29:06 -0700521 return pte_write(pte) ? range->flags[HMM_PFN_VALID] |
522 range->flags[HMM_PFN_WRITE] :
523 range->flags[HMM_PFN_VALID];
Jérôme Glisse2aee09d2018-04-10 16:29:02 -0700524}
525
Jérôme Glisse53f5c3f2018-04-10 16:28:59 -0700526static int hmm_vma_handle_pte(struct mm_walk *walk, unsigned long addr,
527 unsigned long end, pmd_t *pmdp, pte_t *ptep,
528 uint64_t *pfn)
529{
530 struct hmm_vma_walk *hmm_vma_walk = walk->private;
Jérôme Glissef88a1e92018-04-10 16:29:06 -0700531 struct hmm_range *range = hmm_vma_walk->range;
Jérôme Glisse53f5c3f2018-04-10 16:28:59 -0700532 struct vm_area_struct *vma = walk->vma;
Jérôme Glisse2aee09d2018-04-10 16:29:02 -0700533 bool fault, write_fault;
534 uint64_t cpu_flags;
Jérôme Glisse53f5c3f2018-04-10 16:28:59 -0700535 pte_t pte = *ptep;
Jérôme Glissef88a1e92018-04-10 16:29:06 -0700536 uint64_t orig_pfn = *pfn;
Jérôme Glisse53f5c3f2018-04-10 16:28:59 -0700537
Jérôme Glissef88a1e92018-04-10 16:29:06 -0700538 *pfn = range->values[HMM_PFN_NONE];
Jérôme Glisse73231612019-05-13 17:19:58 -0700539 fault = write_fault = false;
Jérôme Glisse53f5c3f2018-04-10 16:28:59 -0700540
541 if (pte_none(pte)) {
Jérôme Glisse73231612019-05-13 17:19:58 -0700542 hmm_pte_need_fault(hmm_vma_walk, orig_pfn, 0,
543 &fault, &write_fault);
Jérôme Glisse2aee09d2018-04-10 16:29:02 -0700544 if (fault || write_fault)
Jérôme Glisse53f5c3f2018-04-10 16:28:59 -0700545 goto fault;
546 return 0;
547 }
548
549 if (!pte_present(pte)) {
550 swp_entry_t entry = pte_to_swp_entry(pte);
551
552 if (!non_swap_entry(entry)) {
Jérôme Glisse2aee09d2018-04-10 16:29:02 -0700553 if (fault || write_fault)
Jérôme Glisse53f5c3f2018-04-10 16:28:59 -0700554 goto fault;
555 return 0;
556 }
557
558 /*
559 * This is a special swap entry, ignore migration, use
560 * device and report anything else as error.
561 */
562 if (is_device_private_entry(entry)) {
Jérôme Glissef88a1e92018-04-10 16:29:06 -0700563 cpu_flags = range->flags[HMM_PFN_VALID] |
564 range->flags[HMM_PFN_DEVICE_PRIVATE];
Jérôme Glisse2aee09d2018-04-10 16:29:02 -0700565 cpu_flags |= is_write_device_private_entry(entry) ?
Jérôme Glissef88a1e92018-04-10 16:29:06 -0700566 range->flags[HMM_PFN_WRITE] : 0;
567 hmm_pte_need_fault(hmm_vma_walk, orig_pfn, cpu_flags,
568 &fault, &write_fault);
569 if (fault || write_fault)
570 goto fault;
Jérôme Glisse391aab12019-05-13 17:20:31 -0700571 *pfn = hmm_device_entry_from_pfn(range,
572 swp_offset(entry));
Jérôme Glissef88a1e92018-04-10 16:29:06 -0700573 *pfn |= cpu_flags;
Jérôme Glisse53f5c3f2018-04-10 16:28:59 -0700574 return 0;
575 }
576
577 if (is_migration_entry(entry)) {
Jérôme Glisse2aee09d2018-04-10 16:29:02 -0700578 if (fault || write_fault) {
Jérôme Glisse53f5c3f2018-04-10 16:28:59 -0700579 pte_unmap(ptep);
580 hmm_vma_walk->last = addr;
581 migration_entry_wait(vma->vm_mm,
Jérôme Glisse2aee09d2018-04-10 16:29:02 -0700582 pmdp, addr);
Jérôme Glisse73231612019-05-13 17:19:58 -0700583 return -EBUSY;
Jérôme Glisse53f5c3f2018-04-10 16:28:59 -0700584 }
585 return 0;
586 }
587
588 /* Report error for everything else */
Jérôme Glissef88a1e92018-04-10 16:29:06 -0700589 *pfn = range->values[HMM_PFN_ERROR];
Jérôme Glisse53f5c3f2018-04-10 16:28:59 -0700590 return -EFAULT;
Jérôme Glisse73231612019-05-13 17:19:58 -0700591 } else {
592 cpu_flags = pte_to_hmm_pfn_flags(range, pte);
593 hmm_pte_need_fault(hmm_vma_walk, orig_pfn, cpu_flags,
594 &fault, &write_fault);
Jérôme Glisse53f5c3f2018-04-10 16:28:59 -0700595 }
596
Jérôme Glisse2aee09d2018-04-10 16:29:02 -0700597 if (fault || write_fault)
Jérôme Glisse53f5c3f2018-04-10 16:28:59 -0700598 goto fault;
599
Jérôme Glisse992de9a2019-05-13 17:20:21 -0700600 if (pte_devmap(pte)) {
601 hmm_vma_walk->pgmap = get_dev_pagemap(pte_pfn(pte),
602 hmm_vma_walk->pgmap);
603 if (unlikely(!hmm_vma_walk->pgmap))
604 return -EBUSY;
605 } else if (IS_ENABLED(CONFIG_ARCH_HAS_PTE_SPECIAL) && pte_special(pte)) {
606 *pfn = range->values[HMM_PFN_SPECIAL];
607 return -EFAULT;
608 }
609
Jérôme Glisse391aab12019-05-13 17:20:31 -0700610 *pfn = hmm_device_entry_from_pfn(range, pte_pfn(pte)) | cpu_flags;
Jérôme Glisse53f5c3f2018-04-10 16:28:59 -0700611 return 0;
612
613fault:
Jérôme Glisse992de9a2019-05-13 17:20:21 -0700614 if (hmm_vma_walk->pgmap) {
615 put_dev_pagemap(hmm_vma_walk->pgmap);
616 hmm_vma_walk->pgmap = NULL;
617 }
Jérôme Glisse53f5c3f2018-04-10 16:28:59 -0700618 pte_unmap(ptep);
619 /* Fault any virtual address we were asked to fault */
Jérôme Glisse2aee09d2018-04-10 16:29:02 -0700620 return hmm_vma_walk_hole_(addr, end, fault, write_fault, walk);
Jérôme Glisse53f5c3f2018-04-10 16:28:59 -0700621}
622
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700623static int hmm_vma_walk_pmd(pmd_t *pmdp,
624 unsigned long start,
625 unsigned long end,
626 struct mm_walk *walk)
627{
Jérôme Glisse74eee182017-09-08 16:11:35 -0700628 struct hmm_vma_walk *hmm_vma_walk = walk->private;
629 struct hmm_range *range = hmm_vma_walk->range;
Jérôme Glissed08faca2018-10-30 15:04:20 -0700630 struct vm_area_struct *vma = walk->vma;
Jérôme Glisseff05c0c2018-04-10 16:28:38 -0700631 uint64_t *pfns = range->pfns;
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700632 unsigned long addr = start, i;
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700633 pte_t *ptep;
Jérôme Glissed08faca2018-10-30 15:04:20 -0700634 pmd_t pmd;
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700635
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700636
637again:
Jérôme Glissed08faca2018-10-30 15:04:20 -0700638 pmd = READ_ONCE(*pmdp);
639 if (pmd_none(pmd))
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700640 return hmm_vma_walk_hole(start, end, walk);
641
Jérôme Glissed08faca2018-10-30 15:04:20 -0700642 if (pmd_huge(pmd) && (range->vma->vm_flags & VM_HUGETLB))
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700643 return hmm_pfns_bad(start, end, walk);
644
Jérôme Glissed08faca2018-10-30 15:04:20 -0700645 if (thp_migration_supported() && is_pmd_migration_entry(pmd)) {
646 bool fault, write_fault;
647 unsigned long npages;
648 uint64_t *pfns;
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700649
Jérôme Glissed08faca2018-10-30 15:04:20 -0700650 i = (addr - range->start) >> PAGE_SHIFT;
651 npages = (end - addr) >> PAGE_SHIFT;
652 pfns = &range->pfns[i];
653
654 hmm_range_need_fault(hmm_vma_walk, pfns, npages,
655 0, &fault, &write_fault);
656 if (fault || write_fault) {
657 hmm_vma_walk->last = addr;
658 pmd_migration_entry_wait(vma->vm_mm, pmdp);
Jérôme Glisse73231612019-05-13 17:19:58 -0700659 return -EBUSY;
Jérôme Glissed08faca2018-10-30 15:04:20 -0700660 }
661 return 0;
662 } else if (!pmd_present(pmd))
663 return hmm_pfns_bad(start, end, walk);
664
665 if (pmd_devmap(pmd) || pmd_trans_huge(pmd)) {
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700666 /*
667 * No need to take pmd_lock here, even if some other threads
668 * is splitting the huge pmd we will get that event through
669 * mmu_notifier callback.
670 *
671 * So just read pmd value and check again its a transparent
672 * huge or device mapping one and compute corresponding pfn
673 * values.
674 */
675 pmd = pmd_read_atomic(pmdp);
676 barrier();
677 if (!pmd_devmap(pmd) && !pmd_trans_huge(pmd))
678 goto again;
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700679
Jérôme Glissed08faca2018-10-30 15:04:20 -0700680 i = (addr - range->start) >> PAGE_SHIFT;
Jérôme Glisse53f5c3f2018-04-10 16:28:59 -0700681 return hmm_vma_handle_pmd(walk, addr, end, &pfns[i], pmd);
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700682 }
683
Jérôme Glissed08faca2018-10-30 15:04:20 -0700684 /*
685 * We have handled all the valid case above ie either none, migration,
686 * huge or transparent huge. At this point either it is a valid pmd
687 * entry pointing to pte directory or it is a bad pmd that will not
688 * recover.
689 */
690 if (pmd_bad(pmd))
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700691 return hmm_pfns_bad(start, end, walk);
692
693 ptep = pte_offset_map(pmdp, addr);
Jérôme Glissed08faca2018-10-30 15:04:20 -0700694 i = (addr - range->start) >> PAGE_SHIFT;
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700695 for (; addr < end; addr += PAGE_SIZE, ptep++, i++) {
Jérôme Glisse53f5c3f2018-04-10 16:28:59 -0700696 int r;
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700697
Jérôme Glisse53f5c3f2018-04-10 16:28:59 -0700698 r = hmm_vma_handle_pte(walk, addr, end, pmdp, ptep, &pfns[i]);
699 if (r) {
700 /* hmm_vma_handle_pte() did unmap pte directory */
701 hmm_vma_walk->last = addr;
702 return r;
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700703 }
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700704 }
Jérôme Glisse992de9a2019-05-13 17:20:21 -0700705 if (hmm_vma_walk->pgmap) {
706 /*
707 * We do put_dev_pagemap() here and not in hmm_vma_handle_pte()
708 * so that we can leverage get_dev_pagemap() optimization which
709 * will not re-take a reference on a pgmap if we already have
710 * one.
711 */
712 put_dev_pagemap(hmm_vma_walk->pgmap);
713 hmm_vma_walk->pgmap = NULL;
714 }
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700715 pte_unmap(ptep - 1);
716
Jérôme Glisse53f5c3f2018-04-10 16:28:59 -0700717 hmm_vma_walk->last = addr;
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700718 return 0;
719}
720
Jérôme Glisse992de9a2019-05-13 17:20:21 -0700721static int hmm_vma_walk_pud(pud_t *pudp,
722 unsigned long start,
723 unsigned long end,
724 struct mm_walk *walk)
725{
726 struct hmm_vma_walk *hmm_vma_walk = walk->private;
727 struct hmm_range *range = hmm_vma_walk->range;
728 unsigned long addr = start, next;
729 pmd_t *pmdp;
730 pud_t pud;
731 int ret;
732
733again:
734 pud = READ_ONCE(*pudp);
735 if (pud_none(pud))
736 return hmm_vma_walk_hole(start, end, walk);
737
738 if (pud_huge(pud) && pud_devmap(pud)) {
739 unsigned long i, npages, pfn;
740 uint64_t *pfns, cpu_flags;
741 bool fault, write_fault;
742
743 if (!pud_present(pud))
744 return hmm_vma_walk_hole(start, end, walk);
745
746 i = (addr - range->start) >> PAGE_SHIFT;
747 npages = (end - addr) >> PAGE_SHIFT;
748 pfns = &range->pfns[i];
749
750 cpu_flags = pud_to_hmm_pfn_flags(range, pud);
751 hmm_range_need_fault(hmm_vma_walk, pfns, npages,
752 cpu_flags, &fault, &write_fault);
753 if (fault || write_fault)
754 return hmm_vma_walk_hole_(addr, end, fault,
755 write_fault, walk);
756
Jérôme Glisse992de9a2019-05-13 17:20:21 -0700757 pfn = pud_pfn(pud) + ((addr & ~PUD_MASK) >> PAGE_SHIFT);
758 for (i = 0; i < npages; ++i, ++pfn) {
759 hmm_vma_walk->pgmap = get_dev_pagemap(pfn,
760 hmm_vma_walk->pgmap);
761 if (unlikely(!hmm_vma_walk->pgmap))
762 return -EBUSY;
Jérôme Glisse391aab12019-05-13 17:20:31 -0700763 pfns[i] = hmm_device_entry_from_pfn(range, pfn) |
764 cpu_flags;
Jérôme Glisse992de9a2019-05-13 17:20:21 -0700765 }
766 if (hmm_vma_walk->pgmap) {
767 put_dev_pagemap(hmm_vma_walk->pgmap);
768 hmm_vma_walk->pgmap = NULL;
769 }
770 hmm_vma_walk->last = end;
771 return 0;
Jérôme Glisse992de9a2019-05-13 17:20:21 -0700772 }
773
774 split_huge_pud(walk->vma, pudp, addr);
775 if (pud_none(*pudp))
776 goto again;
777
778 pmdp = pmd_offset(pudp, addr);
779 do {
780 next = pmd_addr_end(addr, end);
781 ret = hmm_vma_walk_pmd(pmdp, addr, next, walk);
782 if (ret)
783 return ret;
784 } while (pmdp++, addr = next, addr != end);
785
786 return 0;
787}
788
Jérôme Glisse63d50662019-05-13 17:20:18 -0700789static int hmm_vma_walk_hugetlb_entry(pte_t *pte, unsigned long hmask,
790 unsigned long start, unsigned long end,
791 struct mm_walk *walk)
792{
793#ifdef CONFIG_HUGETLB_PAGE
794 unsigned long addr = start, i, pfn, mask, size, pfn_inc;
795 struct hmm_vma_walk *hmm_vma_walk = walk->private;
796 struct hmm_range *range = hmm_vma_walk->range;
797 struct vm_area_struct *vma = walk->vma;
798 struct hstate *h = hstate_vma(vma);
799 uint64_t orig_pfn, cpu_flags;
800 bool fault, write_fault;
801 spinlock_t *ptl;
802 pte_t entry;
803 int ret = 0;
804
805 size = 1UL << huge_page_shift(h);
806 mask = size - 1;
807 if (range->page_shift != PAGE_SHIFT) {
808 /* Make sure we are looking at full page. */
809 if (start & mask)
810 return -EINVAL;
811 if (end < (start + size))
812 return -EINVAL;
813 pfn_inc = size >> PAGE_SHIFT;
814 } else {
815 pfn_inc = 1;
816 size = PAGE_SIZE;
817 }
818
819
820 ptl = huge_pte_lock(hstate_vma(walk->vma), walk->mm, pte);
821 entry = huge_ptep_get(pte);
822
823 i = (start - range->start) >> range->page_shift;
824 orig_pfn = range->pfns[i];
825 range->pfns[i] = range->values[HMM_PFN_NONE];
826 cpu_flags = pte_to_hmm_pfn_flags(range, entry);
827 fault = write_fault = false;
828 hmm_pte_need_fault(hmm_vma_walk, orig_pfn, cpu_flags,
829 &fault, &write_fault);
830 if (fault || write_fault) {
831 ret = -ENOENT;
832 goto unlock;
833 }
834
835 pfn = pte_pfn(entry) + ((start & mask) >> range->page_shift);
836 for (; addr < end; addr += size, i++, pfn += pfn_inc)
Jérôme Glisse391aab12019-05-13 17:20:31 -0700837 range->pfns[i] = hmm_device_entry_from_pfn(range, pfn) |
838 cpu_flags;
Jérôme Glisse63d50662019-05-13 17:20:18 -0700839 hmm_vma_walk->last = end;
840
841unlock:
842 spin_unlock(ptl);
843
844 if (ret == -ENOENT)
845 return hmm_vma_walk_hole_(addr, end, fault, write_fault, walk);
846
847 return ret;
848#else /* CONFIG_HUGETLB_PAGE */
849 return -EINVAL;
850#endif
851}
852
Jérôme Glissef88a1e92018-04-10 16:29:06 -0700853static void hmm_pfns_clear(struct hmm_range *range,
854 uint64_t *pfns,
Jérôme Glisse33cd47d2018-04-10 16:28:54 -0700855 unsigned long addr,
856 unsigned long end)
857{
858 for (; addr < end; addr += PAGE_SIZE, pfns++)
Jérôme Glissef88a1e92018-04-10 16:29:06 -0700859 *pfns = range->values[HMM_PFN_NONE];
Jérôme Glisse33cd47d2018-04-10 16:28:54 -0700860}
861
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700862/*
Jérôme Glissea3e0d412019-05-13 17:20:01 -0700863 * hmm_range_register() - start tracking change to CPU page table over a range
Jérôme Glisse25f23a02019-05-13 17:19:55 -0700864 * @range: range
Jérôme Glissea3e0d412019-05-13 17:20:01 -0700865 * @mm: the mm struct for the range of virtual address
866 * @start: start virtual address (inclusive)
867 * @end: end virtual address (exclusive)
Jérôme Glisse63d50662019-05-13 17:20:18 -0700868 * @page_shift: expect page shift for the range
Jérôme Glissea3e0d412019-05-13 17:20:01 -0700869 * Returns 0 on success, -EFAULT if the address space is no longer valid
Jérôme Glisse25f23a02019-05-13 17:19:55 -0700870 *
Jérôme Glissea3e0d412019-05-13 17:20:01 -0700871 * Track updates to the CPU page table see include/linux/hmm.h
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700872 */
Jérôme Glissea3e0d412019-05-13 17:20:01 -0700873int hmm_range_register(struct hmm_range *range,
Jason Gunthorpee36acfe2019-05-23 09:41:19 -0300874 struct hmm_mirror *mirror,
Jérôme Glissea3e0d412019-05-13 17:20:01 -0700875 unsigned long start,
Jérôme Glisse63d50662019-05-13 17:20:18 -0700876 unsigned long end,
877 unsigned page_shift)
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700878{
Jérôme Glisse63d50662019-05-13 17:20:18 -0700879 unsigned long mask = ((1UL << page_shift) - 1UL);
Jason Gunthorpee36acfe2019-05-23 09:41:19 -0300880 struct hmm *hmm = mirror->hmm;
Jérôme Glisse63d50662019-05-13 17:20:18 -0700881
Jérôme Glissea3e0d412019-05-13 17:20:01 -0700882 range->valid = false;
Jérôme Glisse704f3f22019-05-13 17:19:48 -0700883 range->hmm = NULL;
884
Jérôme Glisse63d50662019-05-13 17:20:18 -0700885 if ((start & mask) || (end & mask))
886 return -EINVAL;
887 if (start >= end)
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700888 return -EINVAL;
889
Jérôme Glisse63d50662019-05-13 17:20:18 -0700890 range->page_shift = page_shift;
Jérôme Glissea3e0d412019-05-13 17:20:01 -0700891 range->start = start;
892 range->end = end;
893
Jason Gunthorpe47f24592019-05-23 11:08:28 -0300894 /* Prevent hmm_release() from running while the range is valid */
895 if (!mmget_not_zero(hmm->mm))
Jérôme Glissea3e0d412019-05-13 17:20:01 -0700896 return -EFAULT;
Jérôme Glisse86586a42018-04-10 16:28:34 -0700897
Ralph Campbell085ea252019-05-06 16:29:39 -0700898 /* Initialize range to track CPU page table updates. */
899 mutex_lock(&hmm->lock);
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700900
Ralph Campbell085ea252019-05-06 16:29:39 -0700901 range->hmm = hmm;
Jason Gunthorpee36acfe2019-05-23 09:41:19 -0300902 kref_get(&hmm->kref);
Jason Gunthorpe157816f2019-05-23 11:43:43 -0300903 list_add(&range->list, &hmm->ranges);
Jérôme Glisse74eee182017-09-08 16:11:35 -0700904
Jérôme Glisse704f3f22019-05-13 17:19:48 -0700905 /*
Jérôme Glissea3e0d412019-05-13 17:20:01 -0700906 * If there are any concurrent notifiers we have to wait for them for
907 * the range to be valid (see hmm_range_wait_until_valid()).
Jérôme Glisse704f3f22019-05-13 17:19:48 -0700908 */
Ralph Campbell085ea252019-05-06 16:29:39 -0700909 if (!hmm->notifiers)
Jérôme Glissea3e0d412019-05-13 17:20:01 -0700910 range->valid = true;
Ralph Campbell085ea252019-05-06 16:29:39 -0700911 mutex_unlock(&hmm->lock);
Jérôme Glissea3e0d412019-05-13 17:20:01 -0700912
913 return 0;
914}
915EXPORT_SYMBOL(hmm_range_register);
916
917/*
918 * hmm_range_unregister() - stop tracking change to CPU page table over a range
919 * @range: range
920 *
921 * Range struct is used to track updates to the CPU page table after a call to
922 * hmm_range_register(). See include/linux/hmm.h for how to use it.
923 */
924void hmm_range_unregister(struct hmm_range *range)
925{
Ralph Campbell085ea252019-05-06 16:29:39 -0700926 struct hmm *hmm = range->hmm;
927
Jérôme Glissea3e0d412019-05-13 17:20:01 -0700928 /* Sanity check this really should not happen. */
Ralph Campbell085ea252019-05-06 16:29:39 -0700929 if (hmm == NULL || range->end <= range->start)
Jérôme Glissea3e0d412019-05-13 17:20:01 -0700930 return;
931
Ralph Campbell085ea252019-05-06 16:29:39 -0700932 mutex_lock(&hmm->lock);
Jason Gunthorpe47f24592019-05-23 11:08:28 -0300933 list_del_init(&range->list);
Ralph Campbell085ea252019-05-06 16:29:39 -0700934 mutex_unlock(&hmm->lock);
Jérôme Glissea3e0d412019-05-13 17:20:01 -0700935
936 /* Drop reference taken by hmm_range_register() */
937 range->valid = false;
Jason Gunthorpe47f24592019-05-23 11:08:28 -0300938 mmput(hmm->mm);
Ralph Campbell085ea252019-05-06 16:29:39 -0700939 hmm_put(hmm);
Jérôme Glissea3e0d412019-05-13 17:20:01 -0700940 range->hmm = NULL;
941}
942EXPORT_SYMBOL(hmm_range_unregister);
943
944/*
945 * hmm_range_snapshot() - snapshot CPU page table for a range
946 * @range: range
Ralph Campbell085ea252019-05-06 16:29:39 -0700947 * Return: -EINVAL if invalid argument, -ENOMEM out of memory, -EPERM invalid
Jérôme Glissea3e0d412019-05-13 17:20:01 -0700948 * permission (for instance asking for write and range is read only),
949 * -EAGAIN if you need to retry, -EFAULT invalid (ie either no valid
950 * vma or it is illegal to access that range), number of valid pages
951 * in range->pfns[] (from range start address).
952 *
953 * This snapshots the CPU page table for a range of virtual addresses. Snapshot
954 * validity is tracked by range struct. See in include/linux/hmm.h for example
955 * on how to use.
956 */
957long hmm_range_snapshot(struct hmm_range *range)
958{
Jérôme Glisse63d50662019-05-13 17:20:18 -0700959 const unsigned long device_vma = VM_IO | VM_PFNMAP | VM_MIXEDMAP;
Jérôme Glissea3e0d412019-05-13 17:20:01 -0700960 unsigned long start = range->start, end;
961 struct hmm_vma_walk hmm_vma_walk;
962 struct hmm *hmm = range->hmm;
963 struct vm_area_struct *vma;
964 struct mm_walk mm_walk;
965
Jason Gunthorpe47f24592019-05-23 11:08:28 -0300966 lockdep_assert_held(&hmm->mm->mmap_sem);
Jérôme Glissea3e0d412019-05-13 17:20:01 -0700967 do {
968 /* If range is no longer valid force retry. */
969 if (!range->valid)
970 return -EAGAIN;
971
972 vma = find_vma(hmm->mm, start);
Jérôme Glisse63d50662019-05-13 17:20:18 -0700973 if (vma == NULL || (vma->vm_flags & device_vma))
Jérôme Glissea3e0d412019-05-13 17:20:01 -0700974 return -EFAULT;
975
Jérôme Glisse63d50662019-05-13 17:20:18 -0700976 if (is_vm_hugetlb_page(vma)) {
Jason Gunthorpe1c2308f02019-05-27 17:02:21 -0300977 if (huge_page_shift(hstate_vma(vma)) !=
978 range->page_shift &&
Jérôme Glisse63d50662019-05-13 17:20:18 -0700979 range->page_shift != PAGE_SHIFT)
980 return -EINVAL;
981 } else {
982 if (range->page_shift != PAGE_SHIFT)
983 return -EINVAL;
984 }
985
Jérôme Glissea3e0d412019-05-13 17:20:01 -0700986 if (!(vma->vm_flags & VM_READ)) {
987 /*
988 * If vma do not allow read access, then assume that it
989 * does not allow write access, either. HMM does not
990 * support architecture that allow write without read.
991 */
992 hmm_pfns_clear(range, range->pfns,
993 range->start, range->end);
994 return -EPERM;
995 }
996
997 range->vma = vma;
Jérôme Glisse992de9a2019-05-13 17:20:21 -0700998 hmm_vma_walk.pgmap = NULL;
Jérôme Glissea3e0d412019-05-13 17:20:01 -0700999 hmm_vma_walk.last = start;
1000 hmm_vma_walk.fault = false;
1001 hmm_vma_walk.range = range;
1002 mm_walk.private = &hmm_vma_walk;
1003 end = min(range->end, vma->vm_end);
1004
1005 mm_walk.vma = vma;
1006 mm_walk.mm = vma->vm_mm;
1007 mm_walk.pte_entry = NULL;
1008 mm_walk.test_walk = NULL;
1009 mm_walk.hugetlb_entry = NULL;
Jérôme Glisse992de9a2019-05-13 17:20:21 -07001010 mm_walk.pud_entry = hmm_vma_walk_pud;
Jérôme Glissea3e0d412019-05-13 17:20:01 -07001011 mm_walk.pmd_entry = hmm_vma_walk_pmd;
1012 mm_walk.pte_hole = hmm_vma_walk_hole;
Jérôme Glisse63d50662019-05-13 17:20:18 -07001013 mm_walk.hugetlb_entry = hmm_vma_walk_hugetlb_entry;
Jérôme Glissea3e0d412019-05-13 17:20:01 -07001014
1015 walk_page_range(start, end, &mm_walk);
1016 start = end;
1017 } while (start < range->end);
1018
Jérôme Glisse25f23a02019-05-13 17:19:55 -07001019 return (hmm_vma_walk.last - range->start) >> PAGE_SHIFT;
Jérôme Glisseda4c3c72017-09-08 16:11:31 -07001020}
Jérôme Glisse25f23a02019-05-13 17:19:55 -07001021EXPORT_SYMBOL(hmm_range_snapshot);
Jérôme Glisseda4c3c72017-09-08 16:11:31 -07001022
1023/*
Jérôme Glisse73231612019-05-13 17:19:58 -07001024 * hmm_range_fault() - try to fault some address in a virtual address range
Jérôme Glisse08232a42018-04-10 16:28:30 -07001025 * @range: range being faulted
Jérôme Glisse74eee182017-09-08 16:11:35 -07001026 * @block: allow blocking on fault (if true it sleeps and do not drop mmap_sem)
Ralph Campbell085ea252019-05-06 16:29:39 -07001027 * Return: number of valid pages in range->pfns[] (from range start
Jérôme Glisse73231612019-05-13 17:19:58 -07001028 * address). This may be zero. If the return value is negative,
1029 * then one of the following values may be returned:
1030 *
1031 * -EINVAL invalid arguments or mm or virtual address are in an
Jérôme Glisse63d50662019-05-13 17:20:18 -07001032 * invalid vma (for instance device file vma).
Jérôme Glisse73231612019-05-13 17:19:58 -07001033 * -ENOMEM: Out of memory.
1034 * -EPERM: Invalid permission (for instance asking for write and
1035 * range is read only).
1036 * -EAGAIN: If you need to retry and mmap_sem was drop. This can only
1037 * happens if block argument is false.
1038 * -EBUSY: If the the range is being invalidated and you should wait
1039 * for invalidation to finish.
1040 * -EFAULT: Invalid (ie either no valid vma or it is illegal to access
1041 * that range), number of valid pages in range->pfns[] (from
1042 * range start address).
Jérôme Glisse74eee182017-09-08 16:11:35 -07001043 *
1044 * This is similar to a regular CPU page fault except that it will not trigger
Jérôme Glisse73231612019-05-13 17:19:58 -07001045 * any memory migration if the memory being faulted is not accessible by CPUs
1046 * and caller does not ask for migration.
Jérôme Glisse74eee182017-09-08 16:11:35 -07001047 *
Jérôme Glisseff05c0c2018-04-10 16:28:38 -07001048 * On error, for one virtual address in the range, the function will mark the
1049 * corresponding HMM pfn entry with an error flag.
Jérôme Glisse74eee182017-09-08 16:11:35 -07001050 */
Jérôme Glisse73231612019-05-13 17:19:58 -07001051long hmm_range_fault(struct hmm_range *range, bool block)
Jérôme Glisse74eee182017-09-08 16:11:35 -07001052{
Jérôme Glisse63d50662019-05-13 17:20:18 -07001053 const unsigned long device_vma = VM_IO | VM_PFNMAP | VM_MIXEDMAP;
Jérôme Glissea3e0d412019-05-13 17:20:01 -07001054 unsigned long start = range->start, end;
Jérôme Glisse74eee182017-09-08 16:11:35 -07001055 struct hmm_vma_walk hmm_vma_walk;
Jérôme Glissea3e0d412019-05-13 17:20:01 -07001056 struct hmm *hmm = range->hmm;
1057 struct vm_area_struct *vma;
Jérôme Glisse74eee182017-09-08 16:11:35 -07001058 struct mm_walk mm_walk;
Jérôme Glisse74eee182017-09-08 16:11:35 -07001059 int ret;
1060
Jason Gunthorpe47f24592019-05-23 11:08:28 -03001061 lockdep_assert_held(&hmm->mm->mmap_sem);
Jérôme Glisse74eee182017-09-08 16:11:35 -07001062
1063 do {
Jérôme Glissea3e0d412019-05-13 17:20:01 -07001064 /* If range is no longer valid force retry. */
1065 if (!range->valid) {
1066 up_read(&hmm->mm->mmap_sem);
1067 return -EAGAIN;
1068 }
Jérôme Glisse74eee182017-09-08 16:11:35 -07001069
Jérôme Glissea3e0d412019-05-13 17:20:01 -07001070 vma = find_vma(hmm->mm, start);
Jérôme Glisse63d50662019-05-13 17:20:18 -07001071 if (vma == NULL || (vma->vm_flags & device_vma))
Jérôme Glissea3e0d412019-05-13 17:20:01 -07001072 return -EFAULT;
Jérôme Glisse74eee182017-09-08 16:11:35 -07001073
Jérôme Glisse63d50662019-05-13 17:20:18 -07001074 if (is_vm_hugetlb_page(vma)) {
1075 if (huge_page_shift(hstate_vma(vma)) !=
1076 range->page_shift &&
1077 range->page_shift != PAGE_SHIFT)
1078 return -EINVAL;
1079 } else {
1080 if (range->page_shift != PAGE_SHIFT)
1081 return -EINVAL;
1082 }
1083
Jérôme Glissea3e0d412019-05-13 17:20:01 -07001084 if (!(vma->vm_flags & VM_READ)) {
1085 /*
1086 * If vma do not allow read access, then assume that it
1087 * does not allow write access, either. HMM does not
1088 * support architecture that allow write without read.
1089 */
1090 hmm_pfns_clear(range, range->pfns,
1091 range->start, range->end);
1092 return -EPERM;
1093 }
1094
1095 range->vma = vma;
Jérôme Glisse992de9a2019-05-13 17:20:21 -07001096 hmm_vma_walk.pgmap = NULL;
Jérôme Glissea3e0d412019-05-13 17:20:01 -07001097 hmm_vma_walk.last = start;
1098 hmm_vma_walk.fault = true;
1099 hmm_vma_walk.block = block;
1100 hmm_vma_walk.range = range;
1101 mm_walk.private = &hmm_vma_walk;
1102 end = min(range->end, vma->vm_end);
1103
1104 mm_walk.vma = vma;
1105 mm_walk.mm = vma->vm_mm;
1106 mm_walk.pte_entry = NULL;
1107 mm_walk.test_walk = NULL;
1108 mm_walk.hugetlb_entry = NULL;
Jérôme Glisse992de9a2019-05-13 17:20:21 -07001109 mm_walk.pud_entry = hmm_vma_walk_pud;
Jérôme Glissea3e0d412019-05-13 17:20:01 -07001110 mm_walk.pmd_entry = hmm_vma_walk_pmd;
1111 mm_walk.pte_hole = hmm_vma_walk_hole;
Jérôme Glisse63d50662019-05-13 17:20:18 -07001112 mm_walk.hugetlb_entry = hmm_vma_walk_hugetlb_entry;
Jérôme Glissea3e0d412019-05-13 17:20:01 -07001113
1114 do {
1115 ret = walk_page_range(start, end, &mm_walk);
1116 start = hmm_vma_walk.last;
1117
1118 /* Keep trying while the range is valid. */
1119 } while (ret == -EBUSY && range->valid);
1120
1121 if (ret) {
1122 unsigned long i;
1123
1124 i = (hmm_vma_walk.last - range->start) >> PAGE_SHIFT;
1125 hmm_pfns_clear(range, &range->pfns[i],
1126 hmm_vma_walk.last, range->end);
1127 return ret;
1128 }
1129 start = end;
1130
1131 } while (start < range->end);
Jérôme Glisse704f3f22019-05-13 17:19:48 -07001132
Jérôme Glisse73231612019-05-13 17:19:58 -07001133 return (hmm_vma_walk.last - range->start) >> PAGE_SHIFT;
Jérôme Glisse74eee182017-09-08 16:11:35 -07001134}
Jérôme Glisse73231612019-05-13 17:19:58 -07001135EXPORT_SYMBOL(hmm_range_fault);
Jérôme Glisse55c0ece2019-05-13 17:20:28 -07001136
1137/**
1138 * hmm_range_dma_map() - hmm_range_fault() and dma map page all in one.
1139 * @range: range being faulted
1140 * @device: device against to dma map page to
1141 * @daddrs: dma address of mapped pages
1142 * @block: allow blocking on fault (if true it sleeps and do not drop mmap_sem)
Ralph Campbell085ea252019-05-06 16:29:39 -07001143 * Return: number of pages mapped on success, -EAGAIN if mmap_sem have been
Jérôme Glisse55c0ece2019-05-13 17:20:28 -07001144 * drop and you need to try again, some other error value otherwise
1145 *
1146 * Note same usage pattern as hmm_range_fault().
1147 */
1148long hmm_range_dma_map(struct hmm_range *range,
1149 struct device *device,
1150 dma_addr_t *daddrs,
1151 bool block)
1152{
1153 unsigned long i, npages, mapped;
1154 long ret;
1155
1156 ret = hmm_range_fault(range, block);
1157 if (ret <= 0)
1158 return ret ? ret : -EBUSY;
1159
1160 npages = (range->end - range->start) >> PAGE_SHIFT;
1161 for (i = 0, mapped = 0; i < npages; ++i) {
1162 enum dma_data_direction dir = DMA_TO_DEVICE;
1163 struct page *page;
1164
1165 /*
1166 * FIXME need to update DMA API to provide invalid DMA address
1167 * value instead of a function to test dma address value. This
1168 * would remove lot of dumb code duplicated accross many arch.
1169 *
1170 * For now setting it to 0 here is good enough as the pfns[]
1171 * value is what is use to check what is valid and what isn't.
1172 */
1173 daddrs[i] = 0;
1174
Jérôme Glisse391aab12019-05-13 17:20:31 -07001175 page = hmm_device_entry_to_page(range, range->pfns[i]);
Jérôme Glisse55c0ece2019-05-13 17:20:28 -07001176 if (page == NULL)
1177 continue;
1178
1179 /* Check if range is being invalidated */
1180 if (!range->valid) {
1181 ret = -EBUSY;
1182 goto unmap;
1183 }
1184
1185 /* If it is read and write than map bi-directional. */
1186 if (range->pfns[i] & range->flags[HMM_PFN_WRITE])
1187 dir = DMA_BIDIRECTIONAL;
1188
1189 daddrs[i] = dma_map_page(device, page, 0, PAGE_SIZE, dir);
1190 if (dma_mapping_error(device, daddrs[i])) {
1191 ret = -EFAULT;
1192 goto unmap;
1193 }
1194
1195 mapped++;
1196 }
1197
1198 return mapped;
1199
1200unmap:
1201 for (npages = i, i = 0; (i < npages) && mapped; ++i) {
1202 enum dma_data_direction dir = DMA_TO_DEVICE;
1203 struct page *page;
1204
Jérôme Glisse391aab12019-05-13 17:20:31 -07001205 page = hmm_device_entry_to_page(range, range->pfns[i]);
Jérôme Glisse55c0ece2019-05-13 17:20:28 -07001206 if (page == NULL)
1207 continue;
1208
1209 if (dma_mapping_error(device, daddrs[i]))
1210 continue;
1211
1212 /* If it is read and write than map bi-directional. */
1213 if (range->pfns[i] & range->flags[HMM_PFN_WRITE])
1214 dir = DMA_BIDIRECTIONAL;
1215
1216 dma_unmap_page(device, daddrs[i], PAGE_SIZE, dir);
1217 mapped--;
1218 }
1219
1220 return ret;
1221}
1222EXPORT_SYMBOL(hmm_range_dma_map);
1223
1224/**
1225 * hmm_range_dma_unmap() - unmap range of that was map with hmm_range_dma_map()
1226 * @range: range being unmapped
1227 * @vma: the vma against which the range (optional)
1228 * @device: device against which dma map was done
1229 * @daddrs: dma address of mapped pages
1230 * @dirty: dirty page if it had the write flag set
Ralph Campbell085ea252019-05-06 16:29:39 -07001231 * Return: number of page unmapped on success, -EINVAL otherwise
Jérôme Glisse55c0ece2019-05-13 17:20:28 -07001232 *
1233 * Note that caller MUST abide by mmu notifier or use HMM mirror and abide
1234 * to the sync_cpu_device_pagetables() callback so that it is safe here to
1235 * call set_page_dirty(). Caller must also take appropriate locks to avoid
1236 * concurrent mmu notifier or sync_cpu_device_pagetables() to make progress.
1237 */
1238long hmm_range_dma_unmap(struct hmm_range *range,
1239 struct vm_area_struct *vma,
1240 struct device *device,
1241 dma_addr_t *daddrs,
1242 bool dirty)
1243{
1244 unsigned long i, npages;
1245 long cpages = 0;
1246
1247 /* Sanity check. */
1248 if (range->end <= range->start)
1249 return -EINVAL;
1250 if (!daddrs)
1251 return -EINVAL;
1252 if (!range->pfns)
1253 return -EINVAL;
1254
1255 npages = (range->end - range->start) >> PAGE_SHIFT;
1256 for (i = 0; i < npages; ++i) {
1257 enum dma_data_direction dir = DMA_TO_DEVICE;
1258 struct page *page;
1259
Jérôme Glisse391aab12019-05-13 17:20:31 -07001260 page = hmm_device_entry_to_page(range, range->pfns[i]);
Jérôme Glisse55c0ece2019-05-13 17:20:28 -07001261 if (page == NULL)
1262 continue;
1263
1264 /* If it is read and write than map bi-directional. */
1265 if (range->pfns[i] & range->flags[HMM_PFN_WRITE]) {
1266 dir = DMA_BIDIRECTIONAL;
1267
1268 /*
1269 * See comments in function description on why it is
1270 * safe here to call set_page_dirty()
1271 */
1272 if (dirty)
1273 set_page_dirty(page);
1274 }
1275
1276 /* Unmap and clear pfns/dma address */
1277 dma_unmap_page(device, daddrs[i], PAGE_SIZE, dir);
1278 range->pfns[i] = range->values[HMM_PFN_NONE];
1279 /* FIXME see comments in hmm_vma_dma_map() */
1280 daddrs[i] = 0;
1281 cpages++;
1282 }
1283
1284 return cpages;
1285}
1286EXPORT_SYMBOL(hmm_range_dma_unmap);
Jérôme Glissec0b12402017-09-08 16:11:27 -07001287#endif /* IS_ENABLED(CONFIG_HMM_MIRROR) */
Jérôme Glisse4ef589d2017-09-08 16:11:58 -07001288
1289
Jérôme Glissedf6ad692017-09-08 16:12:24 -07001290#if IS_ENABLED(CONFIG_DEVICE_PRIVATE) || IS_ENABLED(CONFIG_DEVICE_PUBLIC)
Jérôme Glisse4ef589d2017-09-08 16:11:58 -07001291struct page *hmm_vma_alloc_locked_page(struct vm_area_struct *vma,
1292 unsigned long addr)
1293{
1294 struct page *page;
1295
1296 page = alloc_page_vma(GFP_HIGHUSER, vma, addr);
1297 if (!page)
1298 return NULL;
1299 lock_page(page);
1300 return page;
1301}
1302EXPORT_SYMBOL(hmm_vma_alloc_locked_page);
1303
1304
1305static void hmm_devmem_ref_release(struct percpu_ref *ref)
1306{
1307 struct hmm_devmem *devmem;
1308
1309 devmem = container_of(ref, struct hmm_devmem, ref);
1310 complete(&devmem->completion);
1311}
1312
1313static void hmm_devmem_ref_exit(void *data)
1314{
1315 struct percpu_ref *ref = data;
1316 struct hmm_devmem *devmem;
1317
1318 devmem = container_of(ref, struct hmm_devmem, ref);
Dan Williamsbbecd94e2018-12-28 00:35:11 -08001319 wait_for_completion(&devmem->completion);
Jérôme Glisse4ef589d2017-09-08 16:11:58 -07001320 percpu_ref_exit(ref);
Jérôme Glisse4ef589d2017-09-08 16:11:58 -07001321}
1322
Dan Williamsbbecd94e2018-12-28 00:35:11 -08001323static void hmm_devmem_ref_kill(struct percpu_ref *ref)
Jérôme Glisse4ef589d2017-09-08 16:11:58 -07001324{
Jérôme Glisse4ef589d2017-09-08 16:11:58 -07001325 percpu_ref_kill(ref);
Jérôme Glisse4ef589d2017-09-08 16:11:58 -07001326}
1327
Souptick Joarderb57e622e62019-03-11 23:28:10 -07001328static vm_fault_t hmm_devmem_fault(struct vm_area_struct *vma,
Jérôme Glisse4ef589d2017-09-08 16:11:58 -07001329 unsigned long addr,
1330 const struct page *page,
1331 unsigned int flags,
1332 pmd_t *pmdp)
1333{
1334 struct hmm_devmem *devmem = page->pgmap->data;
1335
1336 return devmem->ops->fault(devmem, vma, addr, page, flags, pmdp);
1337}
1338
1339static void hmm_devmem_free(struct page *page, void *data)
1340{
1341 struct hmm_devmem *devmem = data;
1342
Dan Williams2fa147b2018-07-13 21:50:01 -07001343 page->mapping = NULL;
1344
Jérôme Glisse4ef589d2017-09-08 16:11:58 -07001345 devmem->ops->free(devmem, page);
1346}
1347
Jérôme Glisse4ef589d2017-09-08 16:11:58 -07001348/*
1349 * hmm_devmem_add() - hotplug ZONE_DEVICE memory for device memory
1350 *
1351 * @ops: memory event device driver callback (see struct hmm_devmem_ops)
1352 * @device: device struct to bind the resource too
1353 * @size: size in bytes of the device memory to add
Ralph Campbell085ea252019-05-06 16:29:39 -07001354 * Return: pointer to new hmm_devmem struct ERR_PTR otherwise
Jérôme Glisse4ef589d2017-09-08 16:11:58 -07001355 *
1356 * This function first finds an empty range of physical address big enough to
1357 * contain the new resource, and then hotplugs it as ZONE_DEVICE memory, which
1358 * in turn allocates struct pages. It does not do anything beyond that; all
1359 * events affecting the memory will go through the various callbacks provided
1360 * by hmm_devmem_ops struct.
1361 *
1362 * Device driver should call this function during device initialization and
1363 * is then responsible of memory management. HMM only provides helpers.
1364 */
1365struct hmm_devmem *hmm_devmem_add(const struct hmm_devmem_ops *ops,
1366 struct device *device,
1367 unsigned long size)
1368{
1369 struct hmm_devmem *devmem;
1370 resource_size_t addr;
Dan Williamsbbecd94e2018-12-28 00:35:11 -08001371 void *result;
Jérôme Glisse4ef589d2017-09-08 16:11:58 -07001372 int ret;
1373
Dan Williamse76384882018-05-16 11:46:08 -07001374 dev_pagemap_get_ops();
Jérôme Glisse4ef589d2017-09-08 16:11:58 -07001375
Dan Williams58ef15b2018-12-28 00:35:07 -08001376 devmem = devm_kzalloc(device, sizeof(*devmem), GFP_KERNEL);
Jérôme Glisse4ef589d2017-09-08 16:11:58 -07001377 if (!devmem)
1378 return ERR_PTR(-ENOMEM);
1379
1380 init_completion(&devmem->completion);
1381 devmem->pfn_first = -1UL;
1382 devmem->pfn_last = -1UL;
1383 devmem->resource = NULL;
1384 devmem->device = device;
1385 devmem->ops = ops;
1386
1387 ret = percpu_ref_init(&devmem->ref, &hmm_devmem_ref_release,
1388 0, GFP_KERNEL);
1389 if (ret)
Dan Williams58ef15b2018-12-28 00:35:07 -08001390 return ERR_PTR(ret);
Jérôme Glisse4ef589d2017-09-08 16:11:58 -07001391
Dan Williams58ef15b2018-12-28 00:35:07 -08001392 ret = devm_add_action_or_reset(device, hmm_devmem_ref_exit, &devmem->ref);
Jérôme Glisse4ef589d2017-09-08 16:11:58 -07001393 if (ret)
Dan Williams58ef15b2018-12-28 00:35:07 -08001394 return ERR_PTR(ret);
Jérôme Glisse4ef589d2017-09-08 16:11:58 -07001395
1396 size = ALIGN(size, PA_SECTION_SIZE);
1397 addr = min((unsigned long)iomem_resource.end,
1398 (1UL << MAX_PHYSMEM_BITS) - 1);
1399 addr = addr - size + 1UL;
1400
1401 /*
1402 * FIXME add a new helper to quickly walk resource tree and find free
1403 * range
1404 *
1405 * FIXME what about ioport_resource resource ?
1406 */
1407 for (; addr > size && addr >= iomem_resource.start; addr -= size) {
1408 ret = region_intersects(addr, size, 0, IORES_DESC_NONE);
1409 if (ret != REGION_DISJOINT)
1410 continue;
1411
1412 devmem->resource = devm_request_mem_region(device, addr, size,
1413 dev_name(device));
Dan Williams58ef15b2018-12-28 00:35:07 -08001414 if (!devmem->resource)
1415 return ERR_PTR(-ENOMEM);
Jérôme Glisse4ef589d2017-09-08 16:11:58 -07001416 break;
1417 }
Dan Williams58ef15b2018-12-28 00:35:07 -08001418 if (!devmem->resource)
1419 return ERR_PTR(-ERANGE);
Jérôme Glisse4ef589d2017-09-08 16:11:58 -07001420
1421 devmem->resource->desc = IORES_DESC_DEVICE_PRIVATE_MEMORY;
1422 devmem->pfn_first = devmem->resource->start >> PAGE_SHIFT;
1423 devmem->pfn_last = devmem->pfn_first +
1424 (resource_size(devmem->resource) >> PAGE_SHIFT);
Dan Williams063a7d12018-12-28 00:39:46 -08001425 devmem->page_fault = hmm_devmem_fault;
Jérôme Glisse4ef589d2017-09-08 16:11:58 -07001426
Dan Williamsbbecd94e2018-12-28 00:35:11 -08001427 devmem->pagemap.type = MEMORY_DEVICE_PRIVATE;
1428 devmem->pagemap.res = *devmem->resource;
Dan Williamsbbecd94e2018-12-28 00:35:11 -08001429 devmem->pagemap.page_free = hmm_devmem_free;
1430 devmem->pagemap.altmap_valid = false;
1431 devmem->pagemap.ref = &devmem->ref;
1432 devmem->pagemap.data = devmem;
1433 devmem->pagemap.kill = hmm_devmem_ref_kill;
Dan Williams58ef15b2018-12-28 00:35:07 -08001434
Dan Williamsbbecd94e2018-12-28 00:35:11 -08001435 result = devm_memremap_pages(devmem->device, &devmem->pagemap);
1436 if (IS_ERR(result))
1437 return result;
Jérôme Glisse4ef589d2017-09-08 16:11:58 -07001438 return devmem;
Jérôme Glisse4ef589d2017-09-08 16:11:58 -07001439}
Dan Williams02917e92018-12-28 00:35:15 -08001440EXPORT_SYMBOL_GPL(hmm_devmem_add);
Jérôme Glisse4ef589d2017-09-08 16:11:58 -07001441
Jérôme Glissed3df0a42017-09-08 16:12:28 -07001442struct hmm_devmem *hmm_devmem_add_resource(const struct hmm_devmem_ops *ops,
1443 struct device *device,
1444 struct resource *res)
1445{
1446 struct hmm_devmem *devmem;
Dan Williamsbbecd94e2018-12-28 00:35:11 -08001447 void *result;
Jérôme Glissed3df0a42017-09-08 16:12:28 -07001448 int ret;
1449
1450 if (res->desc != IORES_DESC_DEVICE_PUBLIC_MEMORY)
1451 return ERR_PTR(-EINVAL);
1452
Dan Williamse76384882018-05-16 11:46:08 -07001453 dev_pagemap_get_ops();
Jérôme Glissed3df0a42017-09-08 16:12:28 -07001454
Dan Williams58ef15b2018-12-28 00:35:07 -08001455 devmem = devm_kzalloc(device, sizeof(*devmem), GFP_KERNEL);
Jérôme Glissed3df0a42017-09-08 16:12:28 -07001456 if (!devmem)
1457 return ERR_PTR(-ENOMEM);
1458
1459 init_completion(&devmem->completion);
1460 devmem->pfn_first = -1UL;
1461 devmem->pfn_last = -1UL;
1462 devmem->resource = res;
1463 devmem->device = device;
1464 devmem->ops = ops;
1465
1466 ret = percpu_ref_init(&devmem->ref, &hmm_devmem_ref_release,
1467 0, GFP_KERNEL);
1468 if (ret)
Dan Williams58ef15b2018-12-28 00:35:07 -08001469 return ERR_PTR(ret);
Jérôme Glissed3df0a42017-09-08 16:12:28 -07001470
Dan Williams58ef15b2018-12-28 00:35:07 -08001471 ret = devm_add_action_or_reset(device, hmm_devmem_ref_exit,
1472 &devmem->ref);
Jérôme Glissed3df0a42017-09-08 16:12:28 -07001473 if (ret)
Dan Williams58ef15b2018-12-28 00:35:07 -08001474 return ERR_PTR(ret);
Jérôme Glissed3df0a42017-09-08 16:12:28 -07001475
1476 devmem->pfn_first = devmem->resource->start >> PAGE_SHIFT;
1477 devmem->pfn_last = devmem->pfn_first +
1478 (resource_size(devmem->resource) >> PAGE_SHIFT);
Dan Williams063a7d12018-12-28 00:39:46 -08001479 devmem->page_fault = hmm_devmem_fault;
Jérôme Glissed3df0a42017-09-08 16:12:28 -07001480
Dan Williamsbbecd94e2018-12-28 00:35:11 -08001481 devmem->pagemap.type = MEMORY_DEVICE_PUBLIC;
1482 devmem->pagemap.res = *devmem->resource;
Dan Williamsbbecd94e2018-12-28 00:35:11 -08001483 devmem->pagemap.page_free = hmm_devmem_free;
1484 devmem->pagemap.altmap_valid = false;
1485 devmem->pagemap.ref = &devmem->ref;
1486 devmem->pagemap.data = devmem;
1487 devmem->pagemap.kill = hmm_devmem_ref_kill;
Dan Williams58ef15b2018-12-28 00:35:07 -08001488
Dan Williamsbbecd94e2018-12-28 00:35:11 -08001489 result = devm_memremap_pages(devmem->device, &devmem->pagemap);
1490 if (IS_ERR(result))
1491 return result;
Jérôme Glissed3df0a42017-09-08 16:12:28 -07001492 return devmem;
Jérôme Glissed3df0a42017-09-08 16:12:28 -07001493}
Dan Williams02917e92018-12-28 00:35:15 -08001494EXPORT_SYMBOL_GPL(hmm_devmem_add_resource);
Jérôme Glissed3df0a42017-09-08 16:12:28 -07001495
Jérôme Glisse4ef589d2017-09-08 16:11:58 -07001496/*
Jérôme Glisse858b54d2017-09-08 16:12:02 -07001497 * A device driver that wants to handle multiple devices memory through a
1498 * single fake device can use hmm_device to do so. This is purely a helper
1499 * and it is not needed to make use of any HMM functionality.
1500 */
1501#define HMM_DEVICE_MAX 256
1502
1503static DECLARE_BITMAP(hmm_device_mask, HMM_DEVICE_MAX);
1504static DEFINE_SPINLOCK(hmm_device_lock);
1505static struct class *hmm_device_class;
1506static dev_t hmm_device_devt;
1507
1508static void hmm_device_release(struct device *device)
1509{
1510 struct hmm_device *hmm_device;
1511
1512 hmm_device = container_of(device, struct hmm_device, device);
1513 spin_lock(&hmm_device_lock);
1514 clear_bit(hmm_device->minor, hmm_device_mask);
1515 spin_unlock(&hmm_device_lock);
1516
1517 kfree(hmm_device);
1518}
1519
1520struct hmm_device *hmm_device_new(void *drvdata)
1521{
1522 struct hmm_device *hmm_device;
1523
1524 hmm_device = kzalloc(sizeof(*hmm_device), GFP_KERNEL);
1525 if (!hmm_device)
1526 return ERR_PTR(-ENOMEM);
1527
1528 spin_lock(&hmm_device_lock);
1529 hmm_device->minor = find_first_zero_bit(hmm_device_mask, HMM_DEVICE_MAX);
1530 if (hmm_device->minor >= HMM_DEVICE_MAX) {
1531 spin_unlock(&hmm_device_lock);
1532 kfree(hmm_device);
1533 return ERR_PTR(-EBUSY);
1534 }
1535 set_bit(hmm_device->minor, hmm_device_mask);
1536 spin_unlock(&hmm_device_lock);
1537
1538 dev_set_name(&hmm_device->device, "hmm_device%d", hmm_device->minor);
1539 hmm_device->device.devt = MKDEV(MAJOR(hmm_device_devt),
1540 hmm_device->minor);
1541 hmm_device->device.release = hmm_device_release;
1542 dev_set_drvdata(&hmm_device->device, drvdata);
1543 hmm_device->device.class = hmm_device_class;
1544 device_initialize(&hmm_device->device);
1545
1546 return hmm_device;
1547}
1548EXPORT_SYMBOL(hmm_device_new);
1549
1550void hmm_device_put(struct hmm_device *hmm_device)
1551{
1552 put_device(&hmm_device->device);
1553}
1554EXPORT_SYMBOL(hmm_device_put);
1555
1556static int __init hmm_init(void)
1557{
1558 int ret;
1559
1560 ret = alloc_chrdev_region(&hmm_device_devt, 0,
1561 HMM_DEVICE_MAX,
1562 "hmm_device");
1563 if (ret)
1564 return ret;
1565
1566 hmm_device_class = class_create(THIS_MODULE, "hmm_device");
1567 if (IS_ERR(hmm_device_class)) {
1568 unregister_chrdev_region(hmm_device_devt, HMM_DEVICE_MAX);
1569 return PTR_ERR(hmm_device_class);
1570 }
1571 return 0;
1572}
1573
1574device_initcall(hmm_init);
Jérôme Glissedf6ad692017-09-08 16:12:24 -07001575#endif /* CONFIG_DEVICE_PRIVATE || CONFIG_DEVICE_PUBLIC */