mm/hmm: cleanup special vma handling (VM_SPECIAL)
[linux-block.git] / include / linux / hmm.h
CommitLineData
133ff0ea
JG
1/*
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 *
14 * Authors: Jérôme Glisse <jglisse@redhat.com>
15 */
16/*
17 * Heterogeneous Memory Management (HMM)
18 *
19 * See Documentation/vm/hmm.txt for reasons and overview of what HMM is and it
20 * is for. Here we focus on the HMM API description, with some explanation of
21 * the underlying implementation.
22 *
23 * Short description: HMM provides a set of helpers to share a virtual address
24 * space between CPU and a device, so that the device can access any valid
25 * address of the process (while still obeying memory protection). HMM also
26 * provides helpers to migrate process memory to device memory, and back. Each
27 * set of functionality (address space mirroring, and migration to and from
28 * device memory) can be used independently of the other.
29 *
30 *
31 * HMM address space mirroring API:
32 *
33 * Use HMM address space mirroring if you want to mirror range of the CPU page
34 * table of a process into a device page table. Here, "mirror" means "keep
35 * synchronized". Prerequisites: the device must provide the ability to write-
36 * protect its page tables (at PAGE_SIZE granularity), and must be able to
37 * recover from the resulting potential page faults.
38 *
39 * HMM guarantees that at any point in time, a given virtual address points to
40 * either the same memory in both CPU and device page tables (that is: CPU and
41 * device page tables each point to the same pages), or that one page table (CPU
42 * or device) points to no entry, while the other still points to the old page
43 * for the address. The latter case happens when the CPU page table update
44 * happens first, and then the update is mirrored over to the device page table.
45 * This does not cause any issue, because the CPU page table cannot start
46 * pointing to a new page until the device page table is invalidated.
47 *
48 * HMM uses mmu_notifiers to monitor the CPU page tables, and forwards any
49 * updates to each device driver that has registered a mirror. It also provides
50 * some API calls to help with taking a snapshot of the CPU page table, and to
51 * synchronize with any updates that might happen concurrently.
52 *
53 *
54 * HMM migration to and from device memory:
55 *
56 * HMM provides a set of helpers to hotplug device memory as ZONE_DEVICE, with
57 * a new MEMORY_DEVICE_PRIVATE type. This provides a struct page for each page
58 * of the device memory, and allows the device driver to manage its memory
59 * using those struct pages. Having struct pages for device memory makes
60 * migration easier. Because that memory is not addressable by the CPU it must
61 * never be pinned to the device; in other words, any CPU page fault can always
62 * cause the device memory to be migrated (copied/moved) back to regular memory.
63 *
64 * A new migrate helper (migrate_vma()) has been added (see mm/migrate.c) that
65 * allows use of a device DMA engine to perform the copy operation between
66 * regular system memory and device memory.
67 */
68#ifndef LINUX_HMM_H
69#define LINUX_HMM_H
70
71#include <linux/kconfig.h>
72
73#if IS_ENABLED(CONFIG_HMM)
74
858b54da 75#include <linux/device.h>
4ef589dc
JG
76#include <linux/migrate.h>
77#include <linux/memremap.h>
78#include <linux/completion.h>
79
c0b12405 80struct hmm;
133ff0ea
JG
81
82/*
133ff0ea 83 * Flags:
86586a41 84 * HMM_PFN_VALID: pfn is valid. It has, at least, read permission.
133ff0ea 85 * HMM_PFN_WRITE: CPU page table has write permission set
da4c3c73
JG
86 * HMM_PFN_ERROR: corresponding CPU page table entry points to poisoned memory
87 * HMM_PFN_EMPTY: corresponding CPU page table entry is pte_none()
88 * HMM_PFN_SPECIAL: corresponding CPU page table entry is special; i.e., the
89 * result of vm_insert_pfn() or vm_insert_page(). Therefore, it should not
90 * be mirrored by a device, because the entry will never have HMM_PFN_VALID
91 * set and the pfn value is undefined.
92 * HMM_PFN_DEVICE_UNADDRESSABLE: unaddressable device memory (ZONE_DEVICE)
133ff0ea 93 */
133ff0ea 94#define HMM_PFN_VALID (1 << 0)
86586a41
JG
95#define HMM_PFN_WRITE (1 << 1)
96#define HMM_PFN_ERROR (1 << 2)
97#define HMM_PFN_EMPTY (1 << 3)
98#define HMM_PFN_SPECIAL (1 << 4)
99#define HMM_PFN_DEVICE_UNADDRESSABLE (1 << 5)
100#define HMM_PFN_SHIFT 6
133ff0ea
JG
101
102/*
ff05c0c6
JG
103 * hmm_pfn_to_page() - return struct page pointed to by a valid HMM pfn
104 * @pfn: HMM pfn value to get corresponding struct page from
105 * Returns: struct page pointer if pfn is a valid HMM pfn, NULL otherwise
133ff0ea 106 *
ff05c0c6
JG
107 * If the HMM pfn is valid (ie valid flag set) then return the struct page
108 * matching the pfn value stored in the HMM pfn. Otherwise return NULL.
133ff0ea 109 */
ff05c0c6 110static inline struct page *hmm_pfn_to_page(uint64_t pfn)
133ff0ea
JG
111{
112 if (!(pfn & HMM_PFN_VALID))
113 return NULL;
114 return pfn_to_page(pfn >> HMM_PFN_SHIFT);
115}
116
117/*
ff05c0c6
JG
118 * hmm_pfn_to_pfn() - return pfn value store in a HMM pfn
119 * @pfn: HMM pfn value to extract pfn from
120 * Returns: pfn value if HMM pfn is valid, -1UL otherwise
133ff0ea 121 */
ff05c0c6 122static inline unsigned long hmm_pfn_to_pfn(uint64_t pfn)
133ff0ea
JG
123{
124 if (!(pfn & HMM_PFN_VALID))
125 return -1UL;
126 return (pfn >> HMM_PFN_SHIFT);
127}
128
129/*
ff05c0c6
JG
130 * hmm_pfn_from_page() - create a valid HMM pfn value from struct page
131 * @page: struct page pointer for which to create the HMM pfn
132 * Returns: valid HMM pfn for the page
133ff0ea 133 */
ff05c0c6 134static inline uint64_t hmm_pfn_from_page(struct page *page)
133ff0ea
JG
135{
136 return (page_to_pfn(page) << HMM_PFN_SHIFT) | HMM_PFN_VALID;
137}
138
139/*
ff05c0c6
JG
140 * hmm_pfn_from_pfn() - create a valid HMM pfn value from pfn
141 * @pfn: pfn value for which to create the HMM pfn
142 * Returns: valid HMM pfn for the pfn
133ff0ea 143 */
ff05c0c6 144static inline uint64_t hmm_pfn_from_pfn(unsigned long pfn)
133ff0ea
JG
145{
146 return (pfn << HMM_PFN_SHIFT) | HMM_PFN_VALID;
147}
148
149
c0b12405
JG
150#if IS_ENABLED(CONFIG_HMM_MIRROR)
151/*
152 * Mirroring: how to synchronize device page table with CPU page table.
153 *
154 * A device driver that is participating in HMM mirroring must always
155 * synchronize with CPU page table updates. For this, device drivers can either
156 * directly use mmu_notifier APIs or they can use the hmm_mirror API. Device
157 * drivers can decide to register one mirror per device per process, or just
158 * one mirror per process for a group of devices. The pattern is:
159 *
160 * int device_bind_address_space(..., struct mm_struct *mm, ...)
161 * {
162 * struct device_address_space *das;
163 *
164 * // Device driver specific initialization, and allocation of das
165 * // which contains an hmm_mirror struct as one of its fields.
166 * ...
167 *
168 * ret = hmm_mirror_register(&das->mirror, mm, &device_mirror_ops);
169 * if (ret) {
170 * // Cleanup on error
171 * return ret;
172 * }
173 *
174 * // Other device driver specific initialization
175 * ...
176 * }
177 *
178 * Once an hmm_mirror is registered for an address space, the device driver
179 * will get callbacks through sync_cpu_device_pagetables() operation (see
180 * hmm_mirror_ops struct).
181 *
182 * Device driver must not free the struct containing the hmm_mirror struct
183 * before calling hmm_mirror_unregister(). The expected usage is to do that when
184 * the device driver is unbinding from an address space.
185 *
186 *
187 * void device_unbind_address_space(struct device_address_space *das)
188 * {
189 * // Device driver specific cleanup
190 * ...
191 *
192 * hmm_mirror_unregister(&das->mirror);
193 *
194 * // Other device driver specific cleanup, and now das can be freed
195 * ...
196 * }
197 */
198
199struct hmm_mirror;
200
201/*
202 * enum hmm_update_type - type of update
203 * @HMM_UPDATE_INVALIDATE: invalidate range (no indication as to why)
204 */
205enum hmm_update_type {
206 HMM_UPDATE_INVALIDATE,
207};
208
209/*
210 * struct hmm_mirror_ops - HMM mirror device operations callback
211 *
212 * @update: callback to update range on a device
213 */
214struct hmm_mirror_ops {
e1401513
RC
215 /* release() - release hmm_mirror
216 *
217 * @mirror: pointer to struct hmm_mirror
218 *
219 * This is called when the mm_struct is being released.
220 * The callback should make sure no references to the mirror occur
221 * after the callback returns.
222 */
223 void (*release)(struct hmm_mirror *mirror);
224
c0b12405
JG
225 /* sync_cpu_device_pagetables() - synchronize page tables
226 *
227 * @mirror: pointer to struct hmm_mirror
228 * @update_type: type of update that occurred to the CPU page table
229 * @start: virtual start address of the range to update
230 * @end: virtual end address of the range to update
231 *
232 * This callback ultimately originates from mmu_notifiers when the CPU
233 * page table is updated. The device driver must update its page table
234 * in response to this callback. The update argument tells what action
235 * to perform.
236 *
237 * The device driver must not return from this callback until the device
238 * page tables are completely updated (TLBs flushed, etc); this is a
239 * synchronous call.
240 */
241 void (*sync_cpu_device_pagetables)(struct hmm_mirror *mirror,
242 enum hmm_update_type update_type,
243 unsigned long start,
244 unsigned long end);
245};
246
247/*
248 * struct hmm_mirror - mirror struct for a device driver
249 *
250 * @hmm: pointer to struct hmm (which is unique per mm_struct)
251 * @ops: device driver callback for HMM mirror operations
252 * @list: for list of mirrors of a given mm
253 *
254 * Each address space (mm_struct) being mirrored by a device must register one
255 * instance of an hmm_mirror struct with HMM. HMM will track the list of all
256 * mirrors for each mm_struct.
257 */
258struct hmm_mirror {
259 struct hmm *hmm;
260 const struct hmm_mirror_ops *ops;
261 struct list_head list;
262};
263
264int hmm_mirror_register(struct hmm_mirror *mirror, struct mm_struct *mm);
265void hmm_mirror_unregister(struct hmm_mirror *mirror);
da4c3c73
JG
266
267
268/*
269 * struct hmm_range - track invalidation lock on virtual address range
270 *
08232a45 271 * @vma: the vm area struct for the range
da4c3c73
JG
272 * @list: all range lock are on a list
273 * @start: range virtual start address (inclusive)
274 * @end: range virtual end address (exclusive)
275 * @pfns: array of pfns (big enough for the range)
276 * @valid: pfns array did not change since it has been fill by an HMM function
277 */
278struct hmm_range {
08232a45 279 struct vm_area_struct *vma;
da4c3c73
JG
280 struct list_head list;
281 unsigned long start;
282 unsigned long end;
ff05c0c6 283 uint64_t *pfns;
da4c3c73
JG
284 bool valid;
285};
286
287/*
288 * To snapshot the CPU page table, call hmm_vma_get_pfns(), then take a device
289 * driver lock that serializes device page table updates, then call
290 * hmm_vma_range_done(), to check if the snapshot is still valid. The same
291 * device driver page table update lock must also be used in the
292 * hmm_mirror_ops.sync_cpu_device_pagetables() callback, so that CPU page
293 * table invalidation serializes on it.
294 *
295 * YOU MUST CALL hmm_vma_range_done() ONCE AND ONLY ONCE EACH TIME YOU CALL
296 * hmm_vma_get_pfns() WITHOUT ERROR !
297 *
298 * IF YOU DO NOT FOLLOW THE ABOVE RULE THE SNAPSHOT CONTENT MIGHT BE INVALID !
299 */
08232a45
JG
300int hmm_vma_get_pfns(struct hmm_range *range);
301bool hmm_vma_range_done(struct hmm_range *range);
74eee180
JG
302
303
304/*
305 * Fault memory on behalf of device driver. Unlike handle_mm_fault(), this will
ff05c0c6 306 * not migrate any device memory back to system memory. The HMM pfn array will
74eee180
JG
307 * be updated with the fault result and current snapshot of the CPU page table
308 * for the range.
309 *
310 * The mmap_sem must be taken in read mode before entering and it might be
311 * dropped by the function if the block argument is false. In that case, the
312 * function returns -EAGAIN.
313 *
314 * Return value does not reflect if the fault was successful for every single
ff05c0c6 315 * address or not. Therefore, the caller must to inspect the HMM pfn array to
74eee180
JG
316 * determine fault status for each address.
317 *
318 * Trying to fault inside an invalid vma will result in -EINVAL.
319 *
320 * See the function description in mm/hmm.c for further documentation.
321 */
08232a45 322int hmm_vma_fault(struct hmm_range *range, bool write, bool block);
c0b12405
JG
323#endif /* IS_ENABLED(CONFIG_HMM_MIRROR) */
324
325
df6ad698 326#if IS_ENABLED(CONFIG_DEVICE_PRIVATE) || IS_ENABLED(CONFIG_DEVICE_PUBLIC)
4ef589dc
JG
327struct hmm_devmem;
328
329struct page *hmm_vma_alloc_locked_page(struct vm_area_struct *vma,
330 unsigned long addr);
331
332/*
333 * struct hmm_devmem_ops - callback for ZONE_DEVICE memory events
334 *
335 * @free: call when refcount on page reach 1 and thus is no longer use
336 * @fault: call when there is a page fault to unaddressable memory
337 *
338 * Both callback happens from page_free() and page_fault() callback of struct
339 * dev_pagemap respectively. See include/linux/memremap.h for more details on
340 * those.
341 *
342 * The hmm_devmem_ops callback are just here to provide a coherent and
343 * uniq API to device driver and device driver should not register their
344 * own page_free() or page_fault() but rely on the hmm_devmem_ops call-
345 * back.
346 */
347struct hmm_devmem_ops {
348 /*
349 * free() - free a device page
350 * @devmem: device memory structure (see struct hmm_devmem)
351 * @page: pointer to struct page being freed
352 *
353 * Call back occurs whenever a device page refcount reach 1 which
354 * means that no one is holding any reference on the page anymore
355 * (ZONE_DEVICE page have an elevated refcount of 1 as default so
356 * that they are not release to the general page allocator).
357 *
358 * Note that callback has exclusive ownership of the page (as no
359 * one is holding any reference).
360 */
361 void (*free)(struct hmm_devmem *devmem, struct page *page);
362 /*
363 * fault() - CPU page fault or get user page (GUP)
364 * @devmem: device memory structure (see struct hmm_devmem)
365 * @vma: virtual memory area containing the virtual address
366 * @addr: virtual address that faulted or for which there is a GUP
367 * @page: pointer to struct page backing virtual address (unreliable)
368 * @flags: FAULT_FLAG_* (see include/linux/mm.h)
369 * @pmdp: page middle directory
370 * Returns: VM_FAULT_MINOR/MAJOR on success or one of VM_FAULT_ERROR
371 * on error
372 *
373 * The callback occurs whenever there is a CPU page fault or GUP on a
374 * virtual address. This means that the device driver must migrate the
375 * page back to regular memory (CPU accessible).
376 *
377 * The device driver is free to migrate more than one page from the
378 * fault() callback as an optimization. However if device decide to
379 * migrate more than one page it must always priotirize the faulting
380 * address over the others.
381 *
382 * The struct page pointer is only given as an hint to allow quick
383 * lookup of internal device driver data. A concurrent migration
384 * might have already free that page and the virtual address might
385 * not longer be back by it. So it should not be modified by the
386 * callback.
387 *
388 * Note that mmap semaphore is held in read mode at least when this
389 * callback occurs, hence the vma is valid upon callback entry.
390 */
391 int (*fault)(struct hmm_devmem *devmem,
392 struct vm_area_struct *vma,
393 unsigned long addr,
394 const struct page *page,
395 unsigned int flags,
396 pmd_t *pmdp);
397};
398
399/*
400 * struct hmm_devmem - track device memory
401 *
402 * @completion: completion object for device memory
403 * @pfn_first: first pfn for this resource (set by hmm_devmem_add())
404 * @pfn_last: last pfn for this resource (set by hmm_devmem_add())
405 * @resource: IO resource reserved for this chunk of memory
406 * @pagemap: device page map for that chunk
407 * @device: device to bind resource to
408 * @ops: memory operations callback
409 * @ref: per CPU refcount
410 *
411 * This an helper structure for device drivers that do not wish to implement
412 * the gory details related to hotplugging new memoy and allocating struct
413 * pages.
414 *
415 * Device drivers can directly use ZONE_DEVICE memory on their own if they
416 * wish to do so.
417 */
418struct hmm_devmem {
419 struct completion completion;
420 unsigned long pfn_first;
421 unsigned long pfn_last;
422 struct resource *resource;
423 struct device *device;
424 struct dev_pagemap pagemap;
425 const struct hmm_devmem_ops *ops;
426 struct percpu_ref ref;
427};
428
429/*
430 * To add (hotplug) device memory, HMM assumes that there is no real resource
431 * that reserves a range in the physical address space (this is intended to be
432 * use by unaddressable device memory). It will reserve a physical range big
433 * enough and allocate struct page for it.
434 *
435 * The device driver can wrap the hmm_devmem struct inside a private device
436 * driver struct. The device driver must call hmm_devmem_remove() before the
437 * device goes away and before freeing the hmm_devmem struct memory.
438 */
439struct hmm_devmem *hmm_devmem_add(const struct hmm_devmem_ops *ops,
440 struct device *device,
441 unsigned long size);
d3df0a42
JG
442struct hmm_devmem *hmm_devmem_add_resource(const struct hmm_devmem_ops *ops,
443 struct device *device,
444 struct resource *res);
4ef589dc
JG
445void hmm_devmem_remove(struct hmm_devmem *devmem);
446
447/*
448 * hmm_devmem_page_set_drvdata - set per-page driver data field
449 *
450 * @page: pointer to struct page
451 * @data: driver data value to set
452 *
453 * Because page can not be on lru we have an unsigned long that driver can use
454 * to store a per page field. This just a simple helper to do that.
455 */
456static inline void hmm_devmem_page_set_drvdata(struct page *page,
457 unsigned long data)
458{
459 unsigned long *drvdata = (unsigned long *)&page->pgmap;
460
461 drvdata[1] = data;
462}
463
464/*
465 * hmm_devmem_page_get_drvdata - get per page driver data field
466 *
467 * @page: pointer to struct page
468 * Return: driver data value
469 */
0bea803e 470static inline unsigned long hmm_devmem_page_get_drvdata(const struct page *page)
4ef589dc 471{
0bea803e 472 const unsigned long *drvdata = (const unsigned long *)&page->pgmap;
4ef589dc
JG
473
474 return drvdata[1];
475}
858b54da
JG
476
477
478/*
479 * struct hmm_device - fake device to hang device memory onto
480 *
481 * @device: device struct
482 * @minor: device minor number
483 */
484struct hmm_device {
485 struct device device;
486 unsigned int minor;
487};
488
489/*
490 * A device driver that wants to handle multiple devices memory through a
491 * single fake device can use hmm_device to do so. This is purely a helper and
492 * it is not strictly needed, in order to make use of any HMM functionality.
493 */
494struct hmm_device *hmm_device_new(void *drvdata);
495void hmm_device_put(struct hmm_device *hmm_device);
df6ad698 496#endif /* CONFIG_DEVICE_PRIVATE || CONFIG_DEVICE_PUBLIC */
4ef589dc 497
133ff0ea
JG
498/* Below are for HMM internal use only! Not to be used by device driver! */
499void hmm_mm_destroy(struct mm_struct *mm);
500
501static inline void hmm_mm_init(struct mm_struct *mm)
502{
503 mm->hmm = NULL;
504}
6b368cd4 505#else /* IS_ENABLED(CONFIG_HMM) */
133ff0ea
JG
506static inline void hmm_mm_destroy(struct mm_struct *mm) {}
507static inline void hmm_mm_init(struct mm_struct *mm) {}
b28b08de 508#endif /* IS_ENABLED(CONFIG_HMM) */
133ff0ea 509#endif /* LINUX_HMM_H */