blob: bd957a9f10d1391268b4f3be88df572645cb23cc [file] [log] [blame]
Jérôme Glisse133ff0e2017-09-08 16:11:23 -07001/*
2 * Copyright 2013 Red Hat Inc.
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
Jérôme Glissef813f212018-10-30 15:04:06 -070014 * Authors: Jérôme Glisse <jglisse@redhat.com>
Jérôme Glisse133ff0e2017-09-08 16:11:23 -070015 */
16/*
17 * Refer to include/linux/hmm.h for information about heterogeneous memory
18 * management or HMM for short.
19 */
20#include <linux/mm.h>
21#include <linux/hmm.h>
Jérôme Glisse858b54d2017-09-08 16:12:02 -070022#include <linux/init.h>
Jérôme Glisseda4c3c72017-09-08 16:11:31 -070023#include <linux/rmap.h>
24#include <linux/swap.h>
Jérôme Glisse133ff0e2017-09-08 16:11:23 -070025#include <linux/slab.h>
26#include <linux/sched.h>
Jérôme Glisse4ef589d2017-09-08 16:11:58 -070027#include <linux/mmzone.h>
28#include <linux/pagemap.h>
Jérôme Glisseda4c3c72017-09-08 16:11:31 -070029#include <linux/swapops.h>
30#include <linux/hugetlb.h>
Jérôme Glisse4ef589d2017-09-08 16:11:58 -070031#include <linux/memremap.h>
Jérôme Glisse7b2d55d22017-09-08 16:11:46 -070032#include <linux/jump_label.h>
Jérôme Glissec0b12402017-09-08 16:11:27 -070033#include <linux/mmu_notifier.h>
Jérôme Glisse4ef589d2017-09-08 16:11:58 -070034#include <linux/memory_hotplug.h>
35
36#define PA_SECTION_SIZE (1UL << PA_SECTION_SHIFT)
Jérôme Glisse133ff0e2017-09-08 16:11:23 -070037
Jérôme Glisse6b368cd2017-09-08 16:12:32 -070038#if IS_ENABLED(CONFIG_HMM_MIRROR)
Jérôme Glissec0b12402017-09-08 16:11:27 -070039static const struct mmu_notifier_ops hmm_mmu_notifier_ops;
40
Jérôme Glisse133ff0e2017-09-08 16:11:23 -070041/*
42 * struct hmm - HMM per mm struct
43 *
44 * @mm: mm struct this HMM struct is bound to
Jérôme Glisseda4c3c72017-09-08 16:11:31 -070045 * @lock: lock protecting ranges list
Jérôme Glisseda4c3c72017-09-08 16:11:31 -070046 * @ranges: list of range being snapshotted
Jérôme Glissec0b12402017-09-08 16:11:27 -070047 * @mirrors: list of mirrors for this mm
48 * @mmu_notifier: mmu notifier to track updates to CPU page table
49 * @mirrors_sem: read/write semaphore protecting the mirrors list
Jérôme Glisse133ff0e2017-09-08 16:11:23 -070050 */
51struct hmm {
52 struct mm_struct *mm;
Jérôme Glisse704f3f22019-05-13 17:19:48 -070053 struct kref kref;
Jérôme Glisseda4c3c72017-09-08 16:11:31 -070054 spinlock_t lock;
Jérôme Glisseda4c3c72017-09-08 16:11:31 -070055 struct list_head ranges;
Jérôme Glissec0b12402017-09-08 16:11:27 -070056 struct list_head mirrors;
57 struct mmu_notifier mmu_notifier;
58 struct rw_semaphore mirrors_sem;
Jérôme Glisse133ff0e2017-09-08 16:11:23 -070059};
60
Jérôme Glisse704f3f22019-05-13 17:19:48 -070061static inline struct hmm *mm_get_hmm(struct mm_struct *mm)
Jérôme Glisse133ff0e2017-09-08 16:11:23 -070062{
Jérôme Glissec0b12402017-09-08 16:11:27 -070063 struct hmm *hmm = READ_ONCE(mm->hmm);
Jérôme Glisse704f3f22019-05-13 17:19:48 -070064
65 if (hmm && kref_get_unless_zero(&hmm->kref))
66 return hmm;
67
68 return NULL;
69}
70
71/**
72 * hmm_get_or_create - register HMM against an mm (HMM internal)
73 *
74 * @mm: mm struct to attach to
75 * Returns: returns an HMM object, either by referencing the existing
76 * (per-process) object, or by creating a new one.
77 *
78 * This is not intended to be used directly by device drivers. If mm already
79 * has an HMM struct then it get a reference on it and returns it. Otherwise
80 * it allocates an HMM struct, initializes it, associate it with the mm and
81 * returns it.
82 */
83static struct hmm *hmm_get_or_create(struct mm_struct *mm)
84{
85 struct hmm *hmm = mm_get_hmm(mm);
Jérôme Glissec0b12402017-09-08 16:11:27 -070086 bool cleanup = false;
Jérôme Glisse133ff0e2017-09-08 16:11:23 -070087
Jérôme Glissec0b12402017-09-08 16:11:27 -070088 if (hmm)
89 return hmm;
90
91 hmm = kmalloc(sizeof(*hmm), GFP_KERNEL);
92 if (!hmm)
93 return NULL;
94 INIT_LIST_HEAD(&hmm->mirrors);
95 init_rwsem(&hmm->mirrors_sem);
Jérôme Glissec0b12402017-09-08 16:11:27 -070096 hmm->mmu_notifier.ops = NULL;
Jérôme Glisseda4c3c72017-09-08 16:11:31 -070097 INIT_LIST_HEAD(&hmm->ranges);
98 spin_lock_init(&hmm->lock);
Jérôme Glisse704f3f22019-05-13 17:19:48 -070099 kref_init(&hmm->kref);
Jérôme Glissec0b12402017-09-08 16:11:27 -0700100 hmm->mm = mm;
101
Jérôme Glissec0b12402017-09-08 16:11:27 -0700102 spin_lock(&mm->page_table_lock);
103 if (!mm->hmm)
104 mm->hmm = hmm;
105 else
106 cleanup = true;
107 spin_unlock(&mm->page_table_lock);
108
Ralph Campbell86a2d592018-10-30 15:04:14 -0700109 if (cleanup)
110 goto error;
111
112 /*
113 * We should only get here if hold the mmap_sem in write mode ie on
114 * registration of first mirror through hmm_mirror_register()
115 */
116 hmm->mmu_notifier.ops = &hmm_mmu_notifier_ops;
117 if (__mmu_notifier_register(&hmm->mmu_notifier, mm))
118 goto error_mm;
Jérôme Glissec0b12402017-09-08 16:11:27 -0700119
Jérôme Glisse704f3f22019-05-13 17:19:48 -0700120 return hmm;
Ralph Campbell86a2d592018-10-30 15:04:14 -0700121
122error_mm:
123 spin_lock(&mm->page_table_lock);
124 if (mm->hmm == hmm)
125 mm->hmm = NULL;
126 spin_unlock(&mm->page_table_lock);
127error:
128 kfree(hmm);
129 return NULL;
Jérôme Glisse133ff0e2017-09-08 16:11:23 -0700130}
131
Jérôme Glisse704f3f22019-05-13 17:19:48 -0700132static void hmm_free(struct kref *kref)
133{
134 struct hmm *hmm = container_of(kref, struct hmm, kref);
135 struct mm_struct *mm = hmm->mm;
136
137 mmu_notifier_unregister_no_release(&hmm->mmu_notifier, mm);
138
139 spin_lock(&mm->page_table_lock);
140 if (mm->hmm == hmm)
141 mm->hmm = NULL;
142 spin_unlock(&mm->page_table_lock);
143
144 kfree(hmm);
145}
146
147static inline void hmm_put(struct hmm *hmm)
148{
149 kref_put(&hmm->kref, hmm_free);
150}
151
Jérôme Glisse133ff0e2017-09-08 16:11:23 -0700152void hmm_mm_destroy(struct mm_struct *mm)
153{
Jérôme Glisse704f3f22019-05-13 17:19:48 -0700154 struct hmm *hmm;
155
156 spin_lock(&mm->page_table_lock);
157 hmm = mm_get_hmm(mm);
158 mm->hmm = NULL;
159 if (hmm) {
160 hmm->mm = NULL;
161 spin_unlock(&mm->page_table_lock);
162 hmm_put(hmm);
163 return;
164 }
165
166 spin_unlock(&mm->page_table_lock);
Jérôme Glisse133ff0e2017-09-08 16:11:23 -0700167}
Jérôme Glissec0b12402017-09-08 16:11:27 -0700168
Jérôme Glisseec131b22018-10-30 15:04:28 -0700169static int hmm_invalidate_range(struct hmm *hmm, bool device,
Jérôme Glisse44532d42018-10-30 15:04:24 -0700170 const struct hmm_update *update)
Jérôme Glissec0b12402017-09-08 16:11:27 -0700171{
172 struct hmm_mirror *mirror;
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700173 struct hmm_range *range;
174
175 spin_lock(&hmm->lock);
176 list_for_each_entry(range, &hmm->ranges, list) {
Jérôme Glisse44532d42018-10-30 15:04:24 -0700177 if (update->end < range->start || update->start >= range->end)
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700178 continue;
179
180 range->valid = false;
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700181 }
182 spin_unlock(&hmm->lock);
Jérôme Glissec0b12402017-09-08 16:11:27 -0700183
Jérôme Glisseec131b22018-10-30 15:04:28 -0700184 if (!device)
185 return 0;
186
Jérôme Glissec0b12402017-09-08 16:11:27 -0700187 down_read(&hmm->mirrors_sem);
Jérôme Glisse44532d42018-10-30 15:04:24 -0700188 list_for_each_entry(mirror, &hmm->mirrors, list) {
189 int ret;
190
191 ret = mirror->ops->sync_cpu_device_pagetables(mirror, update);
192 if (!update->blockable && ret == -EAGAIN) {
193 up_read(&hmm->mirrors_sem);
194 return -EAGAIN;
195 }
196 }
Jérôme Glissec0b12402017-09-08 16:11:27 -0700197 up_read(&hmm->mirrors_sem);
Jérôme Glisse44532d42018-10-30 15:04:24 -0700198
199 return 0;
Jérôme Glissec0b12402017-09-08 16:11:27 -0700200}
201
Ralph Campbelle1401512018-04-10 16:28:19 -0700202static void hmm_release(struct mmu_notifier *mn, struct mm_struct *mm)
203{
204 struct hmm_mirror *mirror;
Jérôme Glisse704f3f22019-05-13 17:19:48 -0700205 struct hmm *hmm = mm_get_hmm(mm);
Ralph Campbelle1401512018-04-10 16:28:19 -0700206
207 down_write(&hmm->mirrors_sem);
208 mirror = list_first_entry_or_null(&hmm->mirrors, struct hmm_mirror,
209 list);
210 while (mirror) {
211 list_del_init(&mirror->list);
212 if (mirror->ops->release) {
213 /*
214 * Drop mirrors_sem so callback can wait on any pending
215 * work that might itself trigger mmu_notifier callback
216 * and thus would deadlock with us.
217 */
218 up_write(&hmm->mirrors_sem);
219 mirror->ops->release(mirror);
220 down_write(&hmm->mirrors_sem);
221 }
222 mirror = list_first_entry_or_null(&hmm->mirrors,
223 struct hmm_mirror, list);
224 }
225 up_write(&hmm->mirrors_sem);
Jérôme Glisse704f3f22019-05-13 17:19:48 -0700226
227 hmm_put(hmm);
Ralph Campbelle1401512018-04-10 16:28:19 -0700228}
229
Michal Hocko93065ac2018-08-21 21:52:33 -0700230static int hmm_invalidate_range_start(struct mmu_notifier *mn,
Jérôme Glisse5d6527a2018-12-28 00:38:05 -0800231 const struct mmu_notifier_range *range)
Jérôme Glissec0b12402017-09-08 16:11:27 -0700232{
Jérôme Glisse704f3f22019-05-13 17:19:48 -0700233 struct hmm *hmm = mm_get_hmm(range->mm);
Jérôme Glisseec131b22018-10-30 15:04:28 -0700234 struct hmm_update update;
Jérôme Glisse704f3f22019-05-13 17:19:48 -0700235 int ret;
Jérôme Glissec0b12402017-09-08 16:11:27 -0700236
237 VM_BUG_ON(!hmm);
238
Jérôme Glisse5d6527a2018-12-28 00:38:05 -0800239 update.start = range->start;
240 update.end = range->end;
Jérôme Glisseec131b22018-10-30 15:04:28 -0700241 update.event = HMM_UPDATE_INVALIDATE;
Jérôme Glisse5d6527a2018-12-28 00:38:05 -0800242 update.blockable = range->blockable;
Jérôme Glisse704f3f22019-05-13 17:19:48 -0700243 ret = hmm_invalidate_range(hmm, true, &update);
244 hmm_put(hmm);
245 return ret;
Jérôme Glissec0b12402017-09-08 16:11:27 -0700246}
247
248static void hmm_invalidate_range_end(struct mmu_notifier *mn,
Jérôme Glisse5d6527a2018-12-28 00:38:05 -0800249 const struct mmu_notifier_range *range)
Jérôme Glissec0b12402017-09-08 16:11:27 -0700250{
Jérôme Glisse704f3f22019-05-13 17:19:48 -0700251 struct hmm *hmm = mm_get_hmm(range->mm);
Jérôme Glisse44532d42018-10-30 15:04:24 -0700252 struct hmm_update update;
Jérôme Glissec0b12402017-09-08 16:11:27 -0700253
254 VM_BUG_ON(!hmm);
255
Jérôme Glisse5d6527a2018-12-28 00:38:05 -0800256 update.start = range->start;
257 update.end = range->end;
Jérôme Glisse44532d42018-10-30 15:04:24 -0700258 update.event = HMM_UPDATE_INVALIDATE;
259 update.blockable = true;
Jérôme Glisseec131b22018-10-30 15:04:28 -0700260 hmm_invalidate_range(hmm, false, &update);
Jérôme Glisse704f3f22019-05-13 17:19:48 -0700261 hmm_put(hmm);
Jérôme Glissec0b12402017-09-08 16:11:27 -0700262}
263
264static const struct mmu_notifier_ops hmm_mmu_notifier_ops = {
Ralph Campbelle1401512018-04-10 16:28:19 -0700265 .release = hmm_release,
Jérôme Glissec0b12402017-09-08 16:11:27 -0700266 .invalidate_range_start = hmm_invalidate_range_start,
267 .invalidate_range_end = hmm_invalidate_range_end,
268};
269
270/*
271 * hmm_mirror_register() - register a mirror against an mm
272 *
273 * @mirror: new mirror struct to register
274 * @mm: mm to register against
275 *
276 * To start mirroring a process address space, the device driver must register
277 * an HMM mirror struct.
278 *
279 * THE mm->mmap_sem MUST BE HELD IN WRITE MODE !
280 */
281int hmm_mirror_register(struct hmm_mirror *mirror, struct mm_struct *mm)
282{
283 /* Sanity check */
284 if (!mm || !mirror || !mirror->ops)
285 return -EINVAL;
286
Jérôme Glisse704f3f22019-05-13 17:19:48 -0700287 mirror->hmm = hmm_get_or_create(mm);
Jérôme Glissec0b12402017-09-08 16:11:27 -0700288 if (!mirror->hmm)
289 return -ENOMEM;
290
291 down_write(&mirror->hmm->mirrors_sem);
Jérôme Glisse704f3f22019-05-13 17:19:48 -0700292 list_add(&mirror->list, &mirror->hmm->mirrors);
293 up_write(&mirror->hmm->mirrors_sem);
Jérôme Glissec0b12402017-09-08 16:11:27 -0700294
295 return 0;
296}
297EXPORT_SYMBOL(hmm_mirror_register);
298
299/*
300 * hmm_mirror_unregister() - unregister a mirror
301 *
302 * @mirror: new mirror struct to register
303 *
304 * Stop mirroring a process address space, and cleanup.
305 */
306void hmm_mirror_unregister(struct hmm_mirror *mirror)
307{
Jérôme Glisse704f3f22019-05-13 17:19:48 -0700308 struct hmm *hmm = READ_ONCE(mirror->hmm);
Jérôme Glissec0b12402017-09-08 16:11:27 -0700309
Jérôme Glisse704f3f22019-05-13 17:19:48 -0700310 if (hmm == NULL)
Jérôme Glissec01cbba2018-04-10 16:28:23 -0700311 return;
312
Jérôme Glissec0b12402017-09-08 16:11:27 -0700313 down_write(&hmm->mirrors_sem);
Ralph Campbelle1401512018-04-10 16:28:19 -0700314 list_del_init(&mirror->list);
Jérôme Glisse704f3f22019-05-13 17:19:48 -0700315 /* To protect us against double unregister ... */
Jérôme Glissec01cbba2018-04-10 16:28:23 -0700316 mirror->hmm = NULL;
Jérôme Glissec0b12402017-09-08 16:11:27 -0700317 up_write(&hmm->mirrors_sem);
Jérôme Glissec01cbba2018-04-10 16:28:23 -0700318
Jérôme Glisse704f3f22019-05-13 17:19:48 -0700319 hmm_put(hmm);
Jérôme Glissec0b12402017-09-08 16:11:27 -0700320}
321EXPORT_SYMBOL(hmm_mirror_unregister);
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700322
Jérôme Glisse74eee182017-09-08 16:11:35 -0700323struct hmm_vma_walk {
324 struct hmm_range *range;
325 unsigned long last;
326 bool fault;
327 bool block;
Jérôme Glisse74eee182017-09-08 16:11:35 -0700328};
329
Jérôme Glisse2aee09d2018-04-10 16:29:02 -0700330static int hmm_vma_do_fault(struct mm_walk *walk, unsigned long addr,
331 bool write_fault, uint64_t *pfn)
Jérôme Glisse74eee182017-09-08 16:11:35 -0700332{
333 unsigned int flags = FAULT_FLAG_ALLOW_RETRY | FAULT_FLAG_REMOTE;
334 struct hmm_vma_walk *hmm_vma_walk = walk->private;
Jérôme Glissef88a1e92018-04-10 16:29:06 -0700335 struct hmm_range *range = hmm_vma_walk->range;
Jérôme Glisse74eee182017-09-08 16:11:35 -0700336 struct vm_area_struct *vma = walk->vma;
Souptick Joarder50a7ca32018-08-17 15:44:47 -0700337 vm_fault_t ret;
Jérôme Glisse74eee182017-09-08 16:11:35 -0700338
339 flags |= hmm_vma_walk->block ? 0 : FAULT_FLAG_ALLOW_RETRY;
Jérôme Glisse2aee09d2018-04-10 16:29:02 -0700340 flags |= write_fault ? FAULT_FLAG_WRITE : 0;
Souptick Joarder50a7ca32018-08-17 15:44:47 -0700341 ret = handle_mm_fault(vma, addr, flags);
342 if (ret & VM_FAULT_RETRY)
Jérôme Glisse74eee182017-09-08 16:11:35 -0700343 return -EBUSY;
Souptick Joarder50a7ca32018-08-17 15:44:47 -0700344 if (ret & VM_FAULT_ERROR) {
Jérôme Glissef88a1e92018-04-10 16:29:06 -0700345 *pfn = range->values[HMM_PFN_ERROR];
Jérôme Glisse74eee182017-09-08 16:11:35 -0700346 return -EFAULT;
347 }
348
349 return -EAGAIN;
350}
351
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700352static int hmm_pfns_bad(unsigned long addr,
353 unsigned long end,
354 struct mm_walk *walk)
355{
Jérôme Glissec7195472018-04-10 16:28:27 -0700356 struct hmm_vma_walk *hmm_vma_walk = walk->private;
357 struct hmm_range *range = hmm_vma_walk->range;
Jérôme Glisseff05c0c2018-04-10 16:28:38 -0700358 uint64_t *pfns = range->pfns;
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700359 unsigned long i;
360
361 i = (addr - range->start) >> PAGE_SHIFT;
362 for (; addr < end; addr += PAGE_SIZE, i++)
Jérôme Glissef88a1e92018-04-10 16:29:06 -0700363 pfns[i] = range->values[HMM_PFN_ERROR];
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700364
365 return 0;
366}
367
Jérôme Glisse5504ed22018-04-10 16:28:46 -0700368/*
369 * hmm_vma_walk_hole() - handle a range lacking valid pmd or pte(s)
370 * @start: range virtual start address (inclusive)
371 * @end: range virtual end address (exclusive)
Jérôme Glisse2aee09d2018-04-10 16:29:02 -0700372 * @fault: should we fault or not ?
373 * @write_fault: write fault ?
Jérôme Glisse5504ed22018-04-10 16:28:46 -0700374 * @walk: mm_walk structure
375 * Returns: 0 on success, -EAGAIN after page fault, or page fault error
376 *
377 * This function will be called whenever pmd_none() or pte_none() returns true,
378 * or whenever there is no page directory covering the virtual address range.
379 */
Jérôme Glisse2aee09d2018-04-10 16:29:02 -0700380static int hmm_vma_walk_hole_(unsigned long addr, unsigned long end,
381 bool fault, bool write_fault,
382 struct mm_walk *walk)
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700383{
Jérôme Glisse74eee182017-09-08 16:11:35 -0700384 struct hmm_vma_walk *hmm_vma_walk = walk->private;
385 struct hmm_range *range = hmm_vma_walk->range;
Jérôme Glisseff05c0c2018-04-10 16:28:38 -0700386 uint64_t *pfns = range->pfns;
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700387 unsigned long i;
388
Jérôme Glisse74eee182017-09-08 16:11:35 -0700389 hmm_vma_walk->last = addr;
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700390 i = (addr - range->start) >> PAGE_SHIFT;
Jérôme Glisse74eee182017-09-08 16:11:35 -0700391 for (; addr < end; addr += PAGE_SIZE, i++) {
Jérôme Glissef88a1e92018-04-10 16:29:06 -0700392 pfns[i] = range->values[HMM_PFN_NONE];
Jérôme Glisse2aee09d2018-04-10 16:29:02 -0700393 if (fault || write_fault) {
Jérôme Glisse74eee182017-09-08 16:11:35 -0700394 int ret;
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700395
Jérôme Glisse2aee09d2018-04-10 16:29:02 -0700396 ret = hmm_vma_do_fault(walk, addr, write_fault,
397 &pfns[i]);
Jérôme Glisse74eee182017-09-08 16:11:35 -0700398 if (ret != -EAGAIN)
399 return ret;
400 }
401 }
402
Jérôme Glisse2aee09d2018-04-10 16:29:02 -0700403 return (fault || write_fault) ? -EAGAIN : 0;
404}
405
406static inline void hmm_pte_need_fault(const struct hmm_vma_walk *hmm_vma_walk,
407 uint64_t pfns, uint64_t cpu_flags,
408 bool *fault, bool *write_fault)
409{
Jérôme Glissef88a1e92018-04-10 16:29:06 -0700410 struct hmm_range *range = hmm_vma_walk->range;
411
Jérôme Glisse2aee09d2018-04-10 16:29:02 -0700412 *fault = *write_fault = false;
413 if (!hmm_vma_walk->fault)
414 return;
415
416 /* We aren't ask to do anything ... */
Jérôme Glissef88a1e92018-04-10 16:29:06 -0700417 if (!(pfns & range->flags[HMM_PFN_VALID]))
Jérôme Glisse2aee09d2018-04-10 16:29:02 -0700418 return;
Jérôme Glissef88a1e92018-04-10 16:29:06 -0700419 /* If this is device memory than only fault if explicitly requested */
420 if ((cpu_flags & range->flags[HMM_PFN_DEVICE_PRIVATE])) {
421 /* Do we fault on device memory ? */
422 if (pfns & range->flags[HMM_PFN_DEVICE_PRIVATE]) {
423 *write_fault = pfns & range->flags[HMM_PFN_WRITE];
424 *fault = true;
425 }
Jérôme Glisse2aee09d2018-04-10 16:29:02 -0700426 return;
427 }
Jérôme Glissef88a1e92018-04-10 16:29:06 -0700428
429 /* If CPU page table is not valid then we need to fault */
430 *fault = !(cpu_flags & range->flags[HMM_PFN_VALID]);
431 /* Need to write fault ? */
432 if ((pfns & range->flags[HMM_PFN_WRITE]) &&
433 !(cpu_flags & range->flags[HMM_PFN_WRITE])) {
434 *write_fault = true;
Jérôme Glisse2aee09d2018-04-10 16:29:02 -0700435 *fault = true;
436 }
437}
438
439static void hmm_range_need_fault(const struct hmm_vma_walk *hmm_vma_walk,
440 const uint64_t *pfns, unsigned long npages,
441 uint64_t cpu_flags, bool *fault,
442 bool *write_fault)
443{
444 unsigned long i;
445
446 if (!hmm_vma_walk->fault) {
447 *fault = *write_fault = false;
448 return;
449 }
450
451 for (i = 0; i < npages; ++i) {
452 hmm_pte_need_fault(hmm_vma_walk, pfns[i], cpu_flags,
453 fault, write_fault);
454 if ((*fault) || (*write_fault))
455 return;
456 }
457}
458
459static int hmm_vma_walk_hole(unsigned long addr, unsigned long end,
460 struct mm_walk *walk)
461{
462 struct hmm_vma_walk *hmm_vma_walk = walk->private;
463 struct hmm_range *range = hmm_vma_walk->range;
464 bool fault, write_fault;
465 unsigned long i, npages;
466 uint64_t *pfns;
467
468 i = (addr - range->start) >> PAGE_SHIFT;
469 npages = (end - addr) >> PAGE_SHIFT;
470 pfns = &range->pfns[i];
471 hmm_range_need_fault(hmm_vma_walk, pfns, npages,
472 0, &fault, &write_fault);
473 return hmm_vma_walk_hole_(addr, end, fault, write_fault, walk);
474}
475
Jérôme Glissef88a1e92018-04-10 16:29:06 -0700476static inline uint64_t pmd_to_hmm_pfn_flags(struct hmm_range *range, pmd_t pmd)
Jérôme Glisse2aee09d2018-04-10 16:29:02 -0700477{
478 if (pmd_protnone(pmd))
479 return 0;
Jérôme Glissef88a1e92018-04-10 16:29:06 -0700480 return pmd_write(pmd) ? range->flags[HMM_PFN_VALID] |
481 range->flags[HMM_PFN_WRITE] :
482 range->flags[HMM_PFN_VALID];
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700483}
484
Jérôme Glisse53f5c3f2018-04-10 16:28:59 -0700485static int hmm_vma_handle_pmd(struct mm_walk *walk,
486 unsigned long addr,
487 unsigned long end,
488 uint64_t *pfns,
489 pmd_t pmd)
490{
491 struct hmm_vma_walk *hmm_vma_walk = walk->private;
Jérôme Glissef88a1e92018-04-10 16:29:06 -0700492 struct hmm_range *range = hmm_vma_walk->range;
Jérôme Glisse2aee09d2018-04-10 16:29:02 -0700493 unsigned long pfn, npages, i;
Jérôme Glisse2aee09d2018-04-10 16:29:02 -0700494 bool fault, write_fault;
Jérôme Glissef88a1e92018-04-10 16:29:06 -0700495 uint64_t cpu_flags;
Jérôme Glisse53f5c3f2018-04-10 16:28:59 -0700496
Jérôme Glisse2aee09d2018-04-10 16:29:02 -0700497 npages = (end - addr) >> PAGE_SHIFT;
Jérôme Glissef88a1e92018-04-10 16:29:06 -0700498 cpu_flags = pmd_to_hmm_pfn_flags(range, pmd);
Jérôme Glisse2aee09d2018-04-10 16:29:02 -0700499 hmm_range_need_fault(hmm_vma_walk, pfns, npages, cpu_flags,
500 &fault, &write_fault);
Jérôme Glisse53f5c3f2018-04-10 16:28:59 -0700501
Jérôme Glisse2aee09d2018-04-10 16:29:02 -0700502 if (pmd_protnone(pmd) || fault || write_fault)
503 return hmm_vma_walk_hole_(addr, end, fault, write_fault, walk);
Jérôme Glisse53f5c3f2018-04-10 16:28:59 -0700504
505 pfn = pmd_pfn(pmd) + pte_index(addr);
Jérôme Glisse53f5c3f2018-04-10 16:28:59 -0700506 for (i = 0; addr < end; addr += PAGE_SIZE, i++, pfn++)
Jérôme Glissef88a1e92018-04-10 16:29:06 -0700507 pfns[i] = hmm_pfn_from_pfn(range, pfn) | cpu_flags;
Jérôme Glisse53f5c3f2018-04-10 16:28:59 -0700508 hmm_vma_walk->last = end;
509 return 0;
510}
511
Jérôme Glissef88a1e92018-04-10 16:29:06 -0700512static inline uint64_t pte_to_hmm_pfn_flags(struct hmm_range *range, pte_t pte)
Jérôme Glisse2aee09d2018-04-10 16:29:02 -0700513{
514 if (pte_none(pte) || !pte_present(pte))
515 return 0;
Jérôme Glissef88a1e92018-04-10 16:29:06 -0700516 return pte_write(pte) ? range->flags[HMM_PFN_VALID] |
517 range->flags[HMM_PFN_WRITE] :
518 range->flags[HMM_PFN_VALID];
Jérôme Glisse2aee09d2018-04-10 16:29:02 -0700519}
520
Jérôme Glisse53f5c3f2018-04-10 16:28:59 -0700521static int hmm_vma_handle_pte(struct mm_walk *walk, unsigned long addr,
522 unsigned long end, pmd_t *pmdp, pte_t *ptep,
523 uint64_t *pfn)
524{
525 struct hmm_vma_walk *hmm_vma_walk = walk->private;
Jérôme Glissef88a1e92018-04-10 16:29:06 -0700526 struct hmm_range *range = hmm_vma_walk->range;
Jérôme Glisse53f5c3f2018-04-10 16:28:59 -0700527 struct vm_area_struct *vma = walk->vma;
Jérôme Glisse2aee09d2018-04-10 16:29:02 -0700528 bool fault, write_fault;
529 uint64_t cpu_flags;
Jérôme Glisse53f5c3f2018-04-10 16:28:59 -0700530 pte_t pte = *ptep;
Jérôme Glissef88a1e92018-04-10 16:29:06 -0700531 uint64_t orig_pfn = *pfn;
Jérôme Glisse53f5c3f2018-04-10 16:28:59 -0700532
Jérôme Glissef88a1e92018-04-10 16:29:06 -0700533 *pfn = range->values[HMM_PFN_NONE];
534 cpu_flags = pte_to_hmm_pfn_flags(range, pte);
535 hmm_pte_need_fault(hmm_vma_walk, orig_pfn, cpu_flags,
Jérôme Glisse2aee09d2018-04-10 16:29:02 -0700536 &fault, &write_fault);
Jérôme Glisse53f5c3f2018-04-10 16:28:59 -0700537
538 if (pte_none(pte)) {
Jérôme Glisse2aee09d2018-04-10 16:29:02 -0700539 if (fault || write_fault)
Jérôme Glisse53f5c3f2018-04-10 16:28:59 -0700540 goto fault;
541 return 0;
542 }
543
544 if (!pte_present(pte)) {
545 swp_entry_t entry = pte_to_swp_entry(pte);
546
547 if (!non_swap_entry(entry)) {
Jérôme Glisse2aee09d2018-04-10 16:29:02 -0700548 if (fault || write_fault)
Jérôme Glisse53f5c3f2018-04-10 16:28:59 -0700549 goto fault;
550 return 0;
551 }
552
553 /*
554 * This is a special swap entry, ignore migration, use
555 * device and report anything else as error.
556 */
557 if (is_device_private_entry(entry)) {
Jérôme Glissef88a1e92018-04-10 16:29:06 -0700558 cpu_flags = range->flags[HMM_PFN_VALID] |
559 range->flags[HMM_PFN_DEVICE_PRIVATE];
Jérôme Glisse2aee09d2018-04-10 16:29:02 -0700560 cpu_flags |= is_write_device_private_entry(entry) ?
Jérôme Glissef88a1e92018-04-10 16:29:06 -0700561 range->flags[HMM_PFN_WRITE] : 0;
562 hmm_pte_need_fault(hmm_vma_walk, orig_pfn, cpu_flags,
563 &fault, &write_fault);
564 if (fault || write_fault)
565 goto fault;
566 *pfn = hmm_pfn_from_pfn(range, swp_offset(entry));
567 *pfn |= cpu_flags;
Jérôme Glisse53f5c3f2018-04-10 16:28:59 -0700568 return 0;
569 }
570
571 if (is_migration_entry(entry)) {
Jérôme Glisse2aee09d2018-04-10 16:29:02 -0700572 if (fault || write_fault) {
Jérôme Glisse53f5c3f2018-04-10 16:28:59 -0700573 pte_unmap(ptep);
574 hmm_vma_walk->last = addr;
575 migration_entry_wait(vma->vm_mm,
Jérôme Glisse2aee09d2018-04-10 16:29:02 -0700576 pmdp, addr);
Jérôme Glisse53f5c3f2018-04-10 16:28:59 -0700577 return -EAGAIN;
578 }
579 return 0;
580 }
581
582 /* Report error for everything else */
Jérôme Glissef88a1e92018-04-10 16:29:06 -0700583 *pfn = range->values[HMM_PFN_ERROR];
Jérôme Glisse53f5c3f2018-04-10 16:28:59 -0700584 return -EFAULT;
585 }
586
Jérôme Glisse2aee09d2018-04-10 16:29:02 -0700587 if (fault || write_fault)
Jérôme Glisse53f5c3f2018-04-10 16:28:59 -0700588 goto fault;
589
Jérôme Glissef88a1e92018-04-10 16:29:06 -0700590 *pfn = hmm_pfn_from_pfn(range, pte_pfn(pte)) | cpu_flags;
Jérôme Glisse53f5c3f2018-04-10 16:28:59 -0700591 return 0;
592
593fault:
594 pte_unmap(ptep);
595 /* Fault any virtual address we were asked to fault */
Jérôme Glisse2aee09d2018-04-10 16:29:02 -0700596 return hmm_vma_walk_hole_(addr, end, fault, write_fault, walk);
Jérôme Glisse53f5c3f2018-04-10 16:28:59 -0700597}
598
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700599static int hmm_vma_walk_pmd(pmd_t *pmdp,
600 unsigned long start,
601 unsigned long end,
602 struct mm_walk *walk)
603{
Jérôme Glisse74eee182017-09-08 16:11:35 -0700604 struct hmm_vma_walk *hmm_vma_walk = walk->private;
605 struct hmm_range *range = hmm_vma_walk->range;
Jérôme Glissed08faca2018-10-30 15:04:20 -0700606 struct vm_area_struct *vma = walk->vma;
Jérôme Glisseff05c0c2018-04-10 16:28:38 -0700607 uint64_t *pfns = range->pfns;
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700608 unsigned long addr = start, i;
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700609 pte_t *ptep;
Jérôme Glissed08faca2018-10-30 15:04:20 -0700610 pmd_t pmd;
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700611
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700612
613again:
Jérôme Glissed08faca2018-10-30 15:04:20 -0700614 pmd = READ_ONCE(*pmdp);
615 if (pmd_none(pmd))
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700616 return hmm_vma_walk_hole(start, end, walk);
617
Jérôme Glissed08faca2018-10-30 15:04:20 -0700618 if (pmd_huge(pmd) && (range->vma->vm_flags & VM_HUGETLB))
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700619 return hmm_pfns_bad(start, end, walk);
620
Jérôme Glissed08faca2018-10-30 15:04:20 -0700621 if (thp_migration_supported() && is_pmd_migration_entry(pmd)) {
622 bool fault, write_fault;
623 unsigned long npages;
624 uint64_t *pfns;
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700625
Jérôme Glissed08faca2018-10-30 15:04:20 -0700626 i = (addr - range->start) >> PAGE_SHIFT;
627 npages = (end - addr) >> PAGE_SHIFT;
628 pfns = &range->pfns[i];
629
630 hmm_range_need_fault(hmm_vma_walk, pfns, npages,
631 0, &fault, &write_fault);
632 if (fault || write_fault) {
633 hmm_vma_walk->last = addr;
634 pmd_migration_entry_wait(vma->vm_mm, pmdp);
635 return -EAGAIN;
636 }
637 return 0;
638 } else if (!pmd_present(pmd))
639 return hmm_pfns_bad(start, end, walk);
640
641 if (pmd_devmap(pmd) || pmd_trans_huge(pmd)) {
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700642 /*
643 * No need to take pmd_lock here, even if some other threads
644 * is splitting the huge pmd we will get that event through
645 * mmu_notifier callback.
646 *
647 * So just read pmd value and check again its a transparent
648 * huge or device mapping one and compute corresponding pfn
649 * values.
650 */
651 pmd = pmd_read_atomic(pmdp);
652 barrier();
653 if (!pmd_devmap(pmd) && !pmd_trans_huge(pmd))
654 goto again;
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700655
Jérôme Glissed08faca2018-10-30 15:04:20 -0700656 i = (addr - range->start) >> PAGE_SHIFT;
Jérôme Glisse53f5c3f2018-04-10 16:28:59 -0700657 return hmm_vma_handle_pmd(walk, addr, end, &pfns[i], pmd);
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700658 }
659
Jérôme Glissed08faca2018-10-30 15:04:20 -0700660 /*
661 * We have handled all the valid case above ie either none, migration,
662 * huge or transparent huge. At this point either it is a valid pmd
663 * entry pointing to pte directory or it is a bad pmd that will not
664 * recover.
665 */
666 if (pmd_bad(pmd))
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700667 return hmm_pfns_bad(start, end, walk);
668
669 ptep = pte_offset_map(pmdp, addr);
Jérôme Glissed08faca2018-10-30 15:04:20 -0700670 i = (addr - range->start) >> PAGE_SHIFT;
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700671 for (; addr < end; addr += PAGE_SIZE, ptep++, i++) {
Jérôme Glisse53f5c3f2018-04-10 16:28:59 -0700672 int r;
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700673
Jérôme Glisse53f5c3f2018-04-10 16:28:59 -0700674 r = hmm_vma_handle_pte(walk, addr, end, pmdp, ptep, &pfns[i]);
675 if (r) {
676 /* hmm_vma_handle_pte() did unmap pte directory */
677 hmm_vma_walk->last = addr;
678 return r;
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700679 }
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700680 }
681 pte_unmap(ptep - 1);
682
Jérôme Glisse53f5c3f2018-04-10 16:28:59 -0700683 hmm_vma_walk->last = addr;
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700684 return 0;
685}
686
Jérôme Glissef88a1e92018-04-10 16:29:06 -0700687static void hmm_pfns_clear(struct hmm_range *range,
688 uint64_t *pfns,
Jérôme Glisse33cd47d2018-04-10 16:28:54 -0700689 unsigned long addr,
690 unsigned long end)
691{
692 for (; addr < end; addr += PAGE_SIZE, pfns++)
Jérôme Glissef88a1e92018-04-10 16:29:06 -0700693 *pfns = range->values[HMM_PFN_NONE];
Jérôme Glisse33cd47d2018-04-10 16:28:54 -0700694}
695
Jérôme Glisse855ce7d2018-04-10 16:28:42 -0700696static void hmm_pfns_special(struct hmm_range *range)
697{
698 unsigned long addr = range->start, i = 0;
699
700 for (; addr < range->end; addr += PAGE_SIZE, i++)
Jérôme Glissef88a1e92018-04-10 16:29:06 -0700701 range->pfns[i] = range->values[HMM_PFN_SPECIAL];
Jérôme Glisse855ce7d2018-04-10 16:28:42 -0700702}
703
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700704/*
Jérôme Glisse25f23a02019-05-13 17:19:55 -0700705 * hmm_range_snapshot() - snapshot CPU page table for a range
706 * @range: range
707 * Returns: number of valid pages in range->pfns[] (from range start
708 * address). This may be zero. If the return value is negative,
709 * then one of the following values may be returned:
710 *
711 * -EINVAL invalid arguments or mm or virtual address are in an
712 * invalid vma (ie either hugetlbfs or device file vma).
713 * -EPERM For example, asking for write, when the range is
714 * read-only
715 * -EAGAIN Caller needs to retry
716 * -EFAULT Either no valid vma exists for this range, or it is
717 * illegal to access the range
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700718 *
719 * This snapshots the CPU page table for a range of virtual addresses. Snapshot
720 * validity is tracked by range struct. See hmm_vma_range_done() for further
721 * information.
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700722 */
Jérôme Glisse25f23a02019-05-13 17:19:55 -0700723long hmm_range_snapshot(struct hmm_range *range)
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700724{
Jérôme Glisse08232a42018-04-10 16:28:30 -0700725 struct vm_area_struct *vma = range->vma;
Jérôme Glisse74eee182017-09-08 16:11:35 -0700726 struct hmm_vma_walk hmm_vma_walk;
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700727 struct mm_walk mm_walk;
728 struct hmm *hmm;
729
Jérôme Glisse704f3f22019-05-13 17:19:48 -0700730 range->hmm = NULL;
731
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700732 /* Sanity check, this really should not happen ! */
Jérôme Glisse08232a42018-04-10 16:28:30 -0700733 if (range->start < vma->vm_start || range->start >= vma->vm_end)
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700734 return -EINVAL;
Jérôme Glisse08232a42018-04-10 16:28:30 -0700735 if (range->end < vma->vm_start || range->end > vma->vm_end)
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700736 return -EINVAL;
737
Jérôme Glisse704f3f22019-05-13 17:19:48 -0700738 hmm = hmm_get_or_create(vma->vm_mm);
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700739 if (!hmm)
740 return -ENOMEM;
Jérôme Glisse704f3f22019-05-13 17:19:48 -0700741
742 /* Check if hmm_mm_destroy() was call. */
743 if (hmm->mm == NULL) {
744 hmm_put(hmm);
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700745 return -EINVAL;
Jérôme Glisse704f3f22019-05-13 17:19:48 -0700746 }
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700747
Jérôme Glisse855ce7d2018-04-10 16:28:42 -0700748 /* FIXME support hugetlb fs */
Dave Jiange1fb4a02018-08-17 15:43:40 -0700749 if (is_vm_hugetlb_page(vma) || (vma->vm_flags & VM_SPECIAL) ||
750 vma_is_dax(vma)) {
Jérôme Glisse855ce7d2018-04-10 16:28:42 -0700751 hmm_pfns_special(range);
Jérôme Glisse704f3f22019-05-13 17:19:48 -0700752 hmm_put(hmm);
Jérôme Glisse855ce7d2018-04-10 16:28:42 -0700753 return -EINVAL;
754 }
755
Jérôme Glisse86586a42018-04-10 16:28:34 -0700756 if (!(vma->vm_flags & VM_READ)) {
757 /*
758 * If vma do not allow read access, then assume that it does
759 * not allow write access, either. Architecture that allow
760 * write without read access are not supported by HMM, because
761 * operations such has atomic access would not work.
762 */
Jérôme Glissef88a1e92018-04-10 16:29:06 -0700763 hmm_pfns_clear(range, range->pfns, range->start, range->end);
Jérôme Glisse704f3f22019-05-13 17:19:48 -0700764 hmm_put(hmm);
Jérôme Glisse86586a42018-04-10 16:28:34 -0700765 return -EPERM;
766 }
767
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700768 /* Initialize range to track CPU page table update */
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700769 spin_lock(&hmm->lock);
770 range->valid = true;
771 list_add_rcu(&range->list, &hmm->ranges);
772 spin_unlock(&hmm->lock);
773
Jérôme Glisse74eee182017-09-08 16:11:35 -0700774 hmm_vma_walk.fault = false;
775 hmm_vma_walk.range = range;
776 mm_walk.private = &hmm_vma_walk;
Jérôme Glisse25f23a02019-05-13 17:19:55 -0700777 hmm_vma_walk.last = range->start;
Jérôme Glisse74eee182017-09-08 16:11:35 -0700778
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700779 mm_walk.vma = vma;
780 mm_walk.mm = vma->vm_mm;
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700781 mm_walk.pte_entry = NULL;
782 mm_walk.test_walk = NULL;
783 mm_walk.hugetlb_entry = NULL;
784 mm_walk.pmd_entry = hmm_vma_walk_pmd;
785 mm_walk.pte_hole = hmm_vma_walk_hole;
786
Jérôme Glisse08232a42018-04-10 16:28:30 -0700787 walk_page_range(range->start, range->end, &mm_walk);
Jérôme Glisse704f3f22019-05-13 17:19:48 -0700788 /*
789 * Transfer hmm reference to the range struct it will be drop inside
790 * the hmm_vma_range_done() function (which _must_ be call if this
791 * function return 0).
792 */
793 range->hmm = hmm;
Jérôme Glisse25f23a02019-05-13 17:19:55 -0700794 return (hmm_vma_walk.last - range->start) >> PAGE_SHIFT;
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700795}
Jérôme Glisse25f23a02019-05-13 17:19:55 -0700796EXPORT_SYMBOL(hmm_range_snapshot);
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700797
798/*
799 * hmm_vma_range_done() - stop tracking change to CPU page table over a range
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700800 * @range: range being tracked
801 * Returns: false if range data has been invalidated, true otherwise
802 *
803 * Range struct is used to track updates to the CPU page table after a call to
804 * either hmm_vma_get_pfns() or hmm_vma_fault(). Once the device driver is done
805 * using the data, or wants to lock updates to the data it got from those
806 * functions, it must call the hmm_vma_range_done() function, which will then
807 * stop tracking CPU page table updates.
808 *
809 * Note that device driver must still implement general CPU page table update
810 * tracking either by using hmm_mirror (see hmm_mirror_register()) or by using
811 * the mmu_notifier API directly.
812 *
813 * CPU page table update tracking done through hmm_range is only temporary and
814 * to be used while trying to duplicate CPU page table contents for a range of
815 * virtual addresses.
816 *
817 * There are two ways to use this :
818 * again:
Jérôme Glisse08232a42018-04-10 16:28:30 -0700819 * hmm_vma_get_pfns(range); or hmm_vma_fault(...);
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700820 * trans = device_build_page_table_update_transaction(pfns);
821 * device_page_table_lock();
Jérôme Glisse08232a42018-04-10 16:28:30 -0700822 * if (!hmm_vma_range_done(range)) {
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700823 * device_page_table_unlock();
824 * goto again;
825 * }
826 * device_commit_transaction(trans);
827 * device_page_table_unlock();
828 *
829 * Or:
Jérôme Glisse08232a42018-04-10 16:28:30 -0700830 * hmm_vma_get_pfns(range); or hmm_vma_fault(...);
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700831 * device_page_table_lock();
Jérôme Glisse08232a42018-04-10 16:28:30 -0700832 * hmm_vma_range_done(range);
833 * device_update_page_table(range->pfns);
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700834 * device_page_table_unlock();
835 */
Jérôme Glisse08232a42018-04-10 16:28:30 -0700836bool hmm_vma_range_done(struct hmm_range *range)
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700837{
Jérôme Glisse704f3f22019-05-13 17:19:48 -0700838 bool ret = false;
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700839
Jérôme Glisse704f3f22019-05-13 17:19:48 -0700840 /* Sanity check this really should not happen. */
841 if (range->hmm == NULL || range->end <= range->start) {
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700842 BUG();
843 return false;
844 }
845
Jérôme Glisse704f3f22019-05-13 17:19:48 -0700846 spin_lock(&range->hmm->lock);
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700847 list_del_rcu(&range->list);
Jérôme Glisse704f3f22019-05-13 17:19:48 -0700848 ret = range->valid;
849 spin_unlock(&range->hmm->lock);
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700850
Jérôme Glisse704f3f22019-05-13 17:19:48 -0700851 /* Is the mm still alive ? */
852 if (range->hmm->mm == NULL)
853 ret = false;
854
855 /* Drop reference taken by hmm_vma_fault() or hmm_vma_get_pfns() */
856 hmm_put(range->hmm);
857 range->hmm = NULL;
858 return ret;
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700859}
860EXPORT_SYMBOL(hmm_vma_range_done);
Jérôme Glisse74eee182017-09-08 16:11:35 -0700861
862/*
863 * hmm_vma_fault() - try to fault some address in a virtual address range
Jérôme Glisse08232a42018-04-10 16:28:30 -0700864 * @range: range being faulted
Jérôme Glisse74eee182017-09-08 16:11:35 -0700865 * @block: allow blocking on fault (if true it sleeps and do not drop mmap_sem)
866 * Returns: 0 success, error otherwise (-EAGAIN means mmap_sem have been drop)
867 *
868 * This is similar to a regular CPU page fault except that it will not trigger
869 * any memory migration if the memory being faulted is not accessible by CPUs.
870 *
Jérôme Glisseff05c0c2018-04-10 16:28:38 -0700871 * On error, for one virtual address in the range, the function will mark the
872 * corresponding HMM pfn entry with an error flag.
Jérôme Glisse74eee182017-09-08 16:11:35 -0700873 *
874 * Expected use pattern:
875 * retry:
876 * down_read(&mm->mmap_sem);
877 * // Find vma and address device wants to fault, initialize hmm_pfn_t
878 * // array accordingly
Jérôme Glisse08232a42018-04-10 16:28:30 -0700879 * ret = hmm_vma_fault(range, write, block);
Jérôme Glisse74eee182017-09-08 16:11:35 -0700880 * switch (ret) {
881 * case -EAGAIN:
Jérôme Glisse08232a42018-04-10 16:28:30 -0700882 * hmm_vma_range_done(range);
Jérôme Glisse74eee182017-09-08 16:11:35 -0700883 * // You might want to rate limit or yield to play nicely, you may
884 * // also commit any valid pfn in the array assuming that you are
885 * // getting true from hmm_vma_range_monitor_end()
886 * goto retry;
887 * case 0:
888 * break;
Jérôme Glisse86586a42018-04-10 16:28:34 -0700889 * case -ENOMEM:
890 * case -EINVAL:
891 * case -EPERM:
Jérôme Glisse74eee182017-09-08 16:11:35 -0700892 * default:
893 * // Handle error !
894 * up_read(&mm->mmap_sem)
895 * return;
896 * }
897 * // Take device driver lock that serialize device page table update
898 * driver_lock_device_page_table_update();
Jérôme Glisse08232a42018-04-10 16:28:30 -0700899 * hmm_vma_range_done(range);
Jérôme Glisse74eee182017-09-08 16:11:35 -0700900 * // Commit pfns we got from hmm_vma_fault()
901 * driver_unlock_device_page_table_update();
902 * up_read(&mm->mmap_sem)
903 *
904 * YOU MUST CALL hmm_vma_range_done() AFTER THIS FUNCTION RETURN SUCCESS (0)
905 * BEFORE FREEING THE range struct OR YOU WILL HAVE SERIOUS MEMORY CORRUPTION !
906 *
907 * YOU HAVE BEEN WARNED !
908 */
Jérôme Glisse2aee09d2018-04-10 16:29:02 -0700909int hmm_vma_fault(struct hmm_range *range, bool block)
Jérôme Glisse74eee182017-09-08 16:11:35 -0700910{
Jérôme Glisse08232a42018-04-10 16:28:30 -0700911 struct vm_area_struct *vma = range->vma;
912 unsigned long start = range->start;
Jérôme Glisse74eee182017-09-08 16:11:35 -0700913 struct hmm_vma_walk hmm_vma_walk;
914 struct mm_walk mm_walk;
915 struct hmm *hmm;
916 int ret;
917
Jérôme Glisse704f3f22019-05-13 17:19:48 -0700918 range->hmm = NULL;
919
Jérôme Glisse74eee182017-09-08 16:11:35 -0700920 /* Sanity check, this really should not happen ! */
Jérôme Glisse08232a42018-04-10 16:28:30 -0700921 if (range->start < vma->vm_start || range->start >= vma->vm_end)
Jérôme Glisse74eee182017-09-08 16:11:35 -0700922 return -EINVAL;
Jérôme Glisse08232a42018-04-10 16:28:30 -0700923 if (range->end < vma->vm_start || range->end > vma->vm_end)
Jérôme Glisse74eee182017-09-08 16:11:35 -0700924 return -EINVAL;
925
Jérôme Glisse704f3f22019-05-13 17:19:48 -0700926 hmm = hmm_get_or_create(vma->vm_mm);
Jérôme Glisse74eee182017-09-08 16:11:35 -0700927 if (!hmm) {
Jérôme Glissef88a1e92018-04-10 16:29:06 -0700928 hmm_pfns_clear(range, range->pfns, range->start, range->end);
Jérôme Glisse74eee182017-09-08 16:11:35 -0700929 return -ENOMEM;
930 }
Jérôme Glisse704f3f22019-05-13 17:19:48 -0700931
932 /* Check if hmm_mm_destroy() was call. */
933 if (hmm->mm == NULL) {
934 hmm_put(hmm);
Jérôme Glisse74eee182017-09-08 16:11:35 -0700935 return -EINVAL;
Jérôme Glisse704f3f22019-05-13 17:19:48 -0700936 }
Jérôme Glisse74eee182017-09-08 16:11:35 -0700937
Jérôme Glisse855ce7d2018-04-10 16:28:42 -0700938 /* FIXME support hugetlb fs */
Dave Jiange1fb4a02018-08-17 15:43:40 -0700939 if (is_vm_hugetlb_page(vma) || (vma->vm_flags & VM_SPECIAL) ||
940 vma_is_dax(vma)) {
Jérôme Glisse855ce7d2018-04-10 16:28:42 -0700941 hmm_pfns_special(range);
Jérôme Glisse704f3f22019-05-13 17:19:48 -0700942 hmm_put(hmm);
Jérôme Glisse855ce7d2018-04-10 16:28:42 -0700943 return -EINVAL;
944 }
945
Jérôme Glisse86586a42018-04-10 16:28:34 -0700946 if (!(vma->vm_flags & VM_READ)) {
947 /*
948 * If vma do not allow read access, then assume that it does
949 * not allow write access, either. Architecture that allow
950 * write without read access are not supported by HMM, because
951 * operations such has atomic access would not work.
952 */
Jérôme Glissef88a1e92018-04-10 16:29:06 -0700953 hmm_pfns_clear(range, range->pfns, range->start, range->end);
Jérôme Glisse704f3f22019-05-13 17:19:48 -0700954 hmm_put(hmm);
Jérôme Glisse86586a42018-04-10 16:28:34 -0700955 return -EPERM;
956 }
Jérôme Glisse74eee182017-09-08 16:11:35 -0700957
Jérôme Glisse86586a42018-04-10 16:28:34 -0700958 /* Initialize range to track CPU page table update */
959 spin_lock(&hmm->lock);
960 range->valid = true;
961 list_add_rcu(&range->list, &hmm->ranges);
962 spin_unlock(&hmm->lock);
963
Jérôme Glisse74eee182017-09-08 16:11:35 -0700964 hmm_vma_walk.fault = true;
Jérôme Glisse74eee182017-09-08 16:11:35 -0700965 hmm_vma_walk.block = block;
966 hmm_vma_walk.range = range;
967 mm_walk.private = &hmm_vma_walk;
968 hmm_vma_walk.last = range->start;
969
970 mm_walk.vma = vma;
971 mm_walk.mm = vma->vm_mm;
972 mm_walk.pte_entry = NULL;
973 mm_walk.test_walk = NULL;
974 mm_walk.hugetlb_entry = NULL;
975 mm_walk.pmd_entry = hmm_vma_walk_pmd;
976 mm_walk.pte_hole = hmm_vma_walk_hole;
977
978 do {
Jérôme Glisse08232a42018-04-10 16:28:30 -0700979 ret = walk_page_range(start, range->end, &mm_walk);
Jérôme Glisse74eee182017-09-08 16:11:35 -0700980 start = hmm_vma_walk.last;
981 } while (ret == -EAGAIN);
982
983 if (ret) {
984 unsigned long i;
985
986 i = (hmm_vma_walk.last - range->start) >> PAGE_SHIFT;
Jérôme Glissef88a1e92018-04-10 16:29:06 -0700987 hmm_pfns_clear(range, &range->pfns[i], hmm_vma_walk.last,
988 range->end);
Jérôme Glisse08232a42018-04-10 16:28:30 -0700989 hmm_vma_range_done(range);
Jérôme Glisse704f3f22019-05-13 17:19:48 -0700990 hmm_put(hmm);
991 } else {
992 /*
993 * Transfer hmm reference to the range struct it will be drop
994 * inside the hmm_vma_range_done() function (which _must_ be
995 * call if this function return 0).
996 */
997 range->hmm = hmm;
Jérôme Glisse74eee182017-09-08 16:11:35 -0700998 }
Jérôme Glisse704f3f22019-05-13 17:19:48 -0700999
Jérôme Glisse74eee182017-09-08 16:11:35 -07001000 return ret;
1001}
1002EXPORT_SYMBOL(hmm_vma_fault);
Jérôme Glissec0b12402017-09-08 16:11:27 -07001003#endif /* IS_ENABLED(CONFIG_HMM_MIRROR) */
Jérôme Glisse4ef589d2017-09-08 16:11:58 -07001004
1005
Jérôme Glissedf6ad692017-09-08 16:12:24 -07001006#if IS_ENABLED(CONFIG_DEVICE_PRIVATE) || IS_ENABLED(CONFIG_DEVICE_PUBLIC)
Jérôme Glisse4ef589d2017-09-08 16:11:58 -07001007struct page *hmm_vma_alloc_locked_page(struct vm_area_struct *vma,
1008 unsigned long addr)
1009{
1010 struct page *page;
1011
1012 page = alloc_page_vma(GFP_HIGHUSER, vma, addr);
1013 if (!page)
1014 return NULL;
1015 lock_page(page);
1016 return page;
1017}
1018EXPORT_SYMBOL(hmm_vma_alloc_locked_page);
1019
1020
1021static void hmm_devmem_ref_release(struct percpu_ref *ref)
1022{
1023 struct hmm_devmem *devmem;
1024
1025 devmem = container_of(ref, struct hmm_devmem, ref);
1026 complete(&devmem->completion);
1027}
1028
1029static void hmm_devmem_ref_exit(void *data)
1030{
1031 struct percpu_ref *ref = data;
1032 struct hmm_devmem *devmem;
1033
1034 devmem = container_of(ref, struct hmm_devmem, ref);
Dan Williamsbbecd94e2018-12-28 00:35:11 -08001035 wait_for_completion(&devmem->completion);
Jérôme Glisse4ef589d2017-09-08 16:11:58 -07001036 percpu_ref_exit(ref);
Jérôme Glisse4ef589d2017-09-08 16:11:58 -07001037}
1038
Dan Williamsbbecd94e2018-12-28 00:35:11 -08001039static void hmm_devmem_ref_kill(struct percpu_ref *ref)
Jérôme Glisse4ef589d2017-09-08 16:11:58 -07001040{
Jérôme Glisse4ef589d2017-09-08 16:11:58 -07001041 percpu_ref_kill(ref);
Jérôme Glisse4ef589d2017-09-08 16:11:58 -07001042}
1043
Souptick Joarderb57e622e62019-03-11 23:28:10 -07001044static vm_fault_t hmm_devmem_fault(struct vm_area_struct *vma,
Jérôme Glisse4ef589d2017-09-08 16:11:58 -07001045 unsigned long addr,
1046 const struct page *page,
1047 unsigned int flags,
1048 pmd_t *pmdp)
1049{
1050 struct hmm_devmem *devmem = page->pgmap->data;
1051
1052 return devmem->ops->fault(devmem, vma, addr, page, flags, pmdp);
1053}
1054
1055static void hmm_devmem_free(struct page *page, void *data)
1056{
1057 struct hmm_devmem *devmem = data;
1058
Dan Williams2fa147b2018-07-13 21:50:01 -07001059 page->mapping = NULL;
1060
Jérôme Glisse4ef589d2017-09-08 16:11:58 -07001061 devmem->ops->free(devmem, page);
1062}
1063
Jérôme Glisse4ef589d2017-09-08 16:11:58 -07001064/*
1065 * hmm_devmem_add() - hotplug ZONE_DEVICE memory for device memory
1066 *
1067 * @ops: memory event device driver callback (see struct hmm_devmem_ops)
1068 * @device: device struct to bind the resource too
1069 * @size: size in bytes of the device memory to add
1070 * Returns: pointer to new hmm_devmem struct ERR_PTR otherwise
1071 *
1072 * This function first finds an empty range of physical address big enough to
1073 * contain the new resource, and then hotplugs it as ZONE_DEVICE memory, which
1074 * in turn allocates struct pages. It does not do anything beyond that; all
1075 * events affecting the memory will go through the various callbacks provided
1076 * by hmm_devmem_ops struct.
1077 *
1078 * Device driver should call this function during device initialization and
1079 * is then responsible of memory management. HMM only provides helpers.
1080 */
1081struct hmm_devmem *hmm_devmem_add(const struct hmm_devmem_ops *ops,
1082 struct device *device,
1083 unsigned long size)
1084{
1085 struct hmm_devmem *devmem;
1086 resource_size_t addr;
Dan Williamsbbecd94e2018-12-28 00:35:11 -08001087 void *result;
Jérôme Glisse4ef589d2017-09-08 16:11:58 -07001088 int ret;
1089
Dan Williamse76384882018-05-16 11:46:08 -07001090 dev_pagemap_get_ops();
Jérôme Glisse4ef589d2017-09-08 16:11:58 -07001091
Dan Williams58ef15b2018-12-28 00:35:07 -08001092 devmem = devm_kzalloc(device, sizeof(*devmem), GFP_KERNEL);
Jérôme Glisse4ef589d2017-09-08 16:11:58 -07001093 if (!devmem)
1094 return ERR_PTR(-ENOMEM);
1095
1096 init_completion(&devmem->completion);
1097 devmem->pfn_first = -1UL;
1098 devmem->pfn_last = -1UL;
1099 devmem->resource = NULL;
1100 devmem->device = device;
1101 devmem->ops = ops;
1102
1103 ret = percpu_ref_init(&devmem->ref, &hmm_devmem_ref_release,
1104 0, GFP_KERNEL);
1105 if (ret)
Dan Williams58ef15b2018-12-28 00:35:07 -08001106 return ERR_PTR(ret);
Jérôme Glisse4ef589d2017-09-08 16:11:58 -07001107
Dan Williams58ef15b2018-12-28 00:35:07 -08001108 ret = devm_add_action_or_reset(device, hmm_devmem_ref_exit, &devmem->ref);
Jérôme Glisse4ef589d2017-09-08 16:11:58 -07001109 if (ret)
Dan Williams58ef15b2018-12-28 00:35:07 -08001110 return ERR_PTR(ret);
Jérôme Glisse4ef589d2017-09-08 16:11:58 -07001111
1112 size = ALIGN(size, PA_SECTION_SIZE);
1113 addr = min((unsigned long)iomem_resource.end,
1114 (1UL << MAX_PHYSMEM_BITS) - 1);
1115 addr = addr - size + 1UL;
1116
1117 /*
1118 * FIXME add a new helper to quickly walk resource tree and find free
1119 * range
1120 *
1121 * FIXME what about ioport_resource resource ?
1122 */
1123 for (; addr > size && addr >= iomem_resource.start; addr -= size) {
1124 ret = region_intersects(addr, size, 0, IORES_DESC_NONE);
1125 if (ret != REGION_DISJOINT)
1126 continue;
1127
1128 devmem->resource = devm_request_mem_region(device, addr, size,
1129 dev_name(device));
Dan Williams58ef15b2018-12-28 00:35:07 -08001130 if (!devmem->resource)
1131 return ERR_PTR(-ENOMEM);
Jérôme Glisse4ef589d2017-09-08 16:11:58 -07001132 break;
1133 }
Dan Williams58ef15b2018-12-28 00:35:07 -08001134 if (!devmem->resource)
1135 return ERR_PTR(-ERANGE);
Jérôme Glisse4ef589d2017-09-08 16:11:58 -07001136
1137 devmem->resource->desc = IORES_DESC_DEVICE_PRIVATE_MEMORY;
1138 devmem->pfn_first = devmem->resource->start >> PAGE_SHIFT;
1139 devmem->pfn_last = devmem->pfn_first +
1140 (resource_size(devmem->resource) >> PAGE_SHIFT);
Dan Williams063a7d12018-12-28 00:39:46 -08001141 devmem->page_fault = hmm_devmem_fault;
Jérôme Glisse4ef589d2017-09-08 16:11:58 -07001142
Dan Williamsbbecd94e2018-12-28 00:35:11 -08001143 devmem->pagemap.type = MEMORY_DEVICE_PRIVATE;
1144 devmem->pagemap.res = *devmem->resource;
Dan Williamsbbecd94e2018-12-28 00:35:11 -08001145 devmem->pagemap.page_free = hmm_devmem_free;
1146 devmem->pagemap.altmap_valid = false;
1147 devmem->pagemap.ref = &devmem->ref;
1148 devmem->pagemap.data = devmem;
1149 devmem->pagemap.kill = hmm_devmem_ref_kill;
Dan Williams58ef15b2018-12-28 00:35:07 -08001150
Dan Williamsbbecd94e2018-12-28 00:35:11 -08001151 result = devm_memremap_pages(devmem->device, &devmem->pagemap);
1152 if (IS_ERR(result))
1153 return result;
Jérôme Glisse4ef589d2017-09-08 16:11:58 -07001154 return devmem;
Jérôme Glisse4ef589d2017-09-08 16:11:58 -07001155}
Dan Williams02917e92018-12-28 00:35:15 -08001156EXPORT_SYMBOL_GPL(hmm_devmem_add);
Jérôme Glisse4ef589d2017-09-08 16:11:58 -07001157
Jérôme Glissed3df0a42017-09-08 16:12:28 -07001158struct hmm_devmem *hmm_devmem_add_resource(const struct hmm_devmem_ops *ops,
1159 struct device *device,
1160 struct resource *res)
1161{
1162 struct hmm_devmem *devmem;
Dan Williamsbbecd94e2018-12-28 00:35:11 -08001163 void *result;
Jérôme Glissed3df0a42017-09-08 16:12:28 -07001164 int ret;
1165
1166 if (res->desc != IORES_DESC_DEVICE_PUBLIC_MEMORY)
1167 return ERR_PTR(-EINVAL);
1168
Dan Williamse76384882018-05-16 11:46:08 -07001169 dev_pagemap_get_ops();
Jérôme Glissed3df0a42017-09-08 16:12:28 -07001170
Dan Williams58ef15b2018-12-28 00:35:07 -08001171 devmem = devm_kzalloc(device, sizeof(*devmem), GFP_KERNEL);
Jérôme Glissed3df0a42017-09-08 16:12:28 -07001172 if (!devmem)
1173 return ERR_PTR(-ENOMEM);
1174
1175 init_completion(&devmem->completion);
1176 devmem->pfn_first = -1UL;
1177 devmem->pfn_last = -1UL;
1178 devmem->resource = res;
1179 devmem->device = device;
1180 devmem->ops = ops;
1181
1182 ret = percpu_ref_init(&devmem->ref, &hmm_devmem_ref_release,
1183 0, GFP_KERNEL);
1184 if (ret)
Dan Williams58ef15b2018-12-28 00:35:07 -08001185 return ERR_PTR(ret);
Jérôme Glissed3df0a42017-09-08 16:12:28 -07001186
Dan Williams58ef15b2018-12-28 00:35:07 -08001187 ret = devm_add_action_or_reset(device, hmm_devmem_ref_exit,
1188 &devmem->ref);
Jérôme Glissed3df0a42017-09-08 16:12:28 -07001189 if (ret)
Dan Williams58ef15b2018-12-28 00:35:07 -08001190 return ERR_PTR(ret);
Jérôme Glissed3df0a42017-09-08 16:12:28 -07001191
1192 devmem->pfn_first = devmem->resource->start >> PAGE_SHIFT;
1193 devmem->pfn_last = devmem->pfn_first +
1194 (resource_size(devmem->resource) >> PAGE_SHIFT);
Dan Williams063a7d12018-12-28 00:39:46 -08001195 devmem->page_fault = hmm_devmem_fault;
Jérôme Glissed3df0a42017-09-08 16:12:28 -07001196
Dan Williamsbbecd94e2018-12-28 00:35:11 -08001197 devmem->pagemap.type = MEMORY_DEVICE_PUBLIC;
1198 devmem->pagemap.res = *devmem->resource;
Dan Williamsbbecd94e2018-12-28 00:35:11 -08001199 devmem->pagemap.page_free = hmm_devmem_free;
1200 devmem->pagemap.altmap_valid = false;
1201 devmem->pagemap.ref = &devmem->ref;
1202 devmem->pagemap.data = devmem;
1203 devmem->pagemap.kill = hmm_devmem_ref_kill;
Dan Williams58ef15b2018-12-28 00:35:07 -08001204
Dan Williamsbbecd94e2018-12-28 00:35:11 -08001205 result = devm_memremap_pages(devmem->device, &devmem->pagemap);
1206 if (IS_ERR(result))
1207 return result;
Jérôme Glissed3df0a42017-09-08 16:12:28 -07001208 return devmem;
Jérôme Glissed3df0a42017-09-08 16:12:28 -07001209}
Dan Williams02917e92018-12-28 00:35:15 -08001210EXPORT_SYMBOL_GPL(hmm_devmem_add_resource);
Jérôme Glissed3df0a42017-09-08 16:12:28 -07001211
Jérôme Glisse4ef589d2017-09-08 16:11:58 -07001212/*
Jérôme Glisse858b54d2017-09-08 16:12:02 -07001213 * A device driver that wants to handle multiple devices memory through a
1214 * single fake device can use hmm_device to do so. This is purely a helper
1215 * and it is not needed to make use of any HMM functionality.
1216 */
1217#define HMM_DEVICE_MAX 256
1218
1219static DECLARE_BITMAP(hmm_device_mask, HMM_DEVICE_MAX);
1220static DEFINE_SPINLOCK(hmm_device_lock);
1221static struct class *hmm_device_class;
1222static dev_t hmm_device_devt;
1223
1224static void hmm_device_release(struct device *device)
1225{
1226 struct hmm_device *hmm_device;
1227
1228 hmm_device = container_of(device, struct hmm_device, device);
1229 spin_lock(&hmm_device_lock);
1230 clear_bit(hmm_device->minor, hmm_device_mask);
1231 spin_unlock(&hmm_device_lock);
1232
1233 kfree(hmm_device);
1234}
1235
1236struct hmm_device *hmm_device_new(void *drvdata)
1237{
1238 struct hmm_device *hmm_device;
1239
1240 hmm_device = kzalloc(sizeof(*hmm_device), GFP_KERNEL);
1241 if (!hmm_device)
1242 return ERR_PTR(-ENOMEM);
1243
1244 spin_lock(&hmm_device_lock);
1245 hmm_device->minor = find_first_zero_bit(hmm_device_mask, HMM_DEVICE_MAX);
1246 if (hmm_device->minor >= HMM_DEVICE_MAX) {
1247 spin_unlock(&hmm_device_lock);
1248 kfree(hmm_device);
1249 return ERR_PTR(-EBUSY);
1250 }
1251 set_bit(hmm_device->minor, hmm_device_mask);
1252 spin_unlock(&hmm_device_lock);
1253
1254 dev_set_name(&hmm_device->device, "hmm_device%d", hmm_device->minor);
1255 hmm_device->device.devt = MKDEV(MAJOR(hmm_device_devt),
1256 hmm_device->minor);
1257 hmm_device->device.release = hmm_device_release;
1258 dev_set_drvdata(&hmm_device->device, drvdata);
1259 hmm_device->device.class = hmm_device_class;
1260 device_initialize(&hmm_device->device);
1261
1262 return hmm_device;
1263}
1264EXPORT_SYMBOL(hmm_device_new);
1265
1266void hmm_device_put(struct hmm_device *hmm_device)
1267{
1268 put_device(&hmm_device->device);
1269}
1270EXPORT_SYMBOL(hmm_device_put);
1271
1272static int __init hmm_init(void)
1273{
1274 int ret;
1275
1276 ret = alloc_chrdev_region(&hmm_device_devt, 0,
1277 HMM_DEVICE_MAX,
1278 "hmm_device");
1279 if (ret)
1280 return ret;
1281
1282 hmm_device_class = class_create(THIS_MODULE, "hmm_device");
1283 if (IS_ERR(hmm_device_class)) {
1284 unregister_chrdev_region(hmm_device_devt, HMM_DEVICE_MAX);
1285 return PTR_ERR(hmm_device_class);
1286 }
1287 return 0;
1288}
1289
1290device_initcall(hmm_init);
Jérôme Glissedf6ad692017-09-08 16:12:24 -07001291#endif /* CONFIG_DEVICE_PRIVATE || CONFIG_DEVICE_PUBLIC */