hmm.h 18 KB

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