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