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