Jérôme Glisse | 133ff0e | 2017-09-08 16:11:23 -0700 | [diff] [blame] | 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 | * |
Jérôme Glisse | f813f21 | 2018-10-30 15:04:06 -0700 | [diff] [blame] | 14 | * Authors: Jérôme Glisse <jglisse@redhat.com> |
Jérôme Glisse | 133ff0e | 2017-09-08 16:11:23 -0700 | [diff] [blame] | 15 | */ |
| 16 | /* |
| 17 | * Heterogeneous Memory Management (HMM) |
| 18 | * |
Mike Rapoport | ad56b73 | 2018-03-21 21:22:47 +0200 | [diff] [blame] | 19 | * See Documentation/vm/hmm.rst for reasons and overview of what HMM is and it |
Jérôme Glisse | 133ff0e | 2017-09-08 16:11:23 -0700 | [diff] [blame] | 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> |
Dan Williams | 063a7d1 | 2018-12-28 00:39:46 -0800 | [diff] [blame] | 72 | #include <asm/pgtable.h> |
Jérôme Glisse | 133ff0e | 2017-09-08 16:11:23 -0700 | [diff] [blame] | 73 | |
| 74 | #if IS_ENABLED(CONFIG_HMM) |
| 75 | |
Jérôme Glisse | 858b54d | 2017-09-08 16:12:02 -0700 | [diff] [blame] | 76 | #include <linux/device.h> |
Jérôme Glisse | 4ef589d | 2017-09-08 16:11:58 -0700 | [diff] [blame] | 77 | #include <linux/migrate.h> |
| 78 | #include <linux/memremap.h> |
| 79 | #include <linux/completion.h> |
Jérôme Glisse | a3e0d41 | 2019-05-13 17:20:01 -0700 | [diff] [blame] | 80 | #include <linux/mmu_notifier.h> |
Jérôme Glisse | 4ef589d | 2017-09-08 16:11:58 -0700 | [diff] [blame] | 81 | |
Jérôme Glisse | a3e0d41 | 2019-05-13 17:20:01 -0700 | [diff] [blame] | 82 | |
| 83 | /* |
| 84 | * struct hmm - HMM per mm struct |
| 85 | * |
| 86 | * @mm: mm struct this HMM struct is bound to |
| 87 | * @lock: lock protecting ranges list |
| 88 | * @ranges: list of range being snapshotted |
| 89 | * @mirrors: list of mirrors for this mm |
| 90 | * @mmu_notifier: mmu notifier to track updates to CPU page table |
| 91 | * @mirrors_sem: read/write semaphore protecting the mirrors list |
| 92 | * @wq: wait queue for user waiting on a range invalidation |
| 93 | * @notifiers: count of active mmu notifiers |
| 94 | * @dead: is the mm dead ? |
| 95 | */ |
| 96 | struct hmm { |
| 97 | struct mm_struct *mm; |
| 98 | struct kref kref; |
| 99 | struct mutex lock; |
| 100 | struct list_head ranges; |
| 101 | struct list_head mirrors; |
| 102 | struct mmu_notifier mmu_notifier; |
| 103 | struct rw_semaphore mirrors_sem; |
| 104 | wait_queue_head_t wq; |
| 105 | long notifiers; |
| 106 | bool dead; |
| 107 | }; |
Jérôme Glisse | 133ff0e | 2017-09-08 16:11:23 -0700 | [diff] [blame] | 108 | |
| 109 | /* |
Jérôme Glisse | f88a1e9 | 2018-04-10 16:29:06 -0700 | [diff] [blame] | 110 | * hmm_pfn_flag_e - HMM flag enums |
| 111 | * |
Jérôme Glisse | 133ff0e | 2017-09-08 16:11:23 -0700 | [diff] [blame] | 112 | * Flags: |
Jérôme Glisse | 86586a4 | 2018-04-10 16:28:34 -0700 | [diff] [blame] | 113 | * HMM_PFN_VALID: pfn is valid. It has, at least, read permission. |
Jérôme Glisse | 133ff0e | 2017-09-08 16:11:23 -0700 | [diff] [blame] | 114 | * HMM_PFN_WRITE: CPU page table has write permission set |
Jérôme Glisse | f88a1e9 | 2018-04-10 16:29:06 -0700 | [diff] [blame] | 115 | * HMM_PFN_DEVICE_PRIVATE: private device memory (ZONE_DEVICE) |
| 116 | * |
| 117 | * The driver provide a flags array, if driver valid bit for an entry is bit |
| 118 | * 3 ie (entry & (1 << 3)) is true if entry is valid then driver must provide |
| 119 | * an array in hmm_range.flags with hmm_range.flags[HMM_PFN_VALID] == 1 << 3. |
| 120 | * Same logic apply to all flags. This is same idea as vm_page_prot in vma |
| 121 | * except that this is per device driver rather than per architecture. |
| 122 | */ |
| 123 | enum hmm_pfn_flag_e { |
| 124 | HMM_PFN_VALID = 0, |
| 125 | HMM_PFN_WRITE, |
| 126 | HMM_PFN_DEVICE_PRIVATE, |
| 127 | HMM_PFN_FLAG_MAX |
| 128 | }; |
| 129 | |
| 130 | /* |
| 131 | * hmm_pfn_value_e - HMM pfn special value |
| 132 | * |
| 133 | * Flags: |
Jérôme Glisse | da4c3c7 | 2017-09-08 16:11:31 -0700 | [diff] [blame] | 134 | * HMM_PFN_ERROR: corresponding CPU page table entry points to poisoned memory |
Jérôme Glisse | f88a1e9 | 2018-04-10 16:29:06 -0700 | [diff] [blame] | 135 | * HMM_PFN_NONE: corresponding CPU page table entry is pte_none() |
Jérôme Glisse | da4c3c7 | 2017-09-08 16:11:31 -0700 | [diff] [blame] | 136 | * HMM_PFN_SPECIAL: corresponding CPU page table entry is special; i.e., the |
Matthew Wilcox | 67fa166 | 2018-10-26 15:04:26 -0700 | [diff] [blame] | 137 | * result of vmf_insert_pfn() or vm_insert_page(). Therefore, it should not |
Jérôme Glisse | da4c3c7 | 2017-09-08 16:11:31 -0700 | [diff] [blame] | 138 | * be mirrored by a device, because the entry will never have HMM_PFN_VALID |
| 139 | * set and the pfn value is undefined. |
Jérôme Glisse | f88a1e9 | 2018-04-10 16:29:06 -0700 | [diff] [blame] | 140 | * |
| 141 | * Driver provide entry value for none entry, error entry and special entry, |
| 142 | * driver can alias (ie use same value for error and special for instance). It |
| 143 | * should not alias none and error or special. |
| 144 | * |
| 145 | * HMM pfn value returned by hmm_vma_get_pfns() or hmm_vma_fault() will be: |
| 146 | * hmm_range.values[HMM_PFN_ERROR] if CPU page table entry is poisonous, |
| 147 | * hmm_range.values[HMM_PFN_NONE] if there is no CPU page table |
| 148 | * hmm_range.values[HMM_PFN_SPECIAL] if CPU page table entry is a special one |
Jérôme Glisse | 133ff0e | 2017-09-08 16:11:23 -0700 | [diff] [blame] | 149 | */ |
Jérôme Glisse | f88a1e9 | 2018-04-10 16:29:06 -0700 | [diff] [blame] | 150 | enum hmm_pfn_value_e { |
| 151 | HMM_PFN_ERROR, |
| 152 | HMM_PFN_NONE, |
| 153 | HMM_PFN_SPECIAL, |
| 154 | HMM_PFN_VALUE_MAX |
| 155 | }; |
| 156 | |
| 157 | /* |
| 158 | * struct hmm_range - track invalidation lock on virtual address range |
| 159 | * |
Jérôme Glisse | 704f3f2 | 2019-05-13 17:19:48 -0700 | [diff] [blame] | 160 | * @hmm: the core HMM structure this range is active against |
Jérôme Glisse | f88a1e9 | 2018-04-10 16:29:06 -0700 | [diff] [blame] | 161 | * @vma: the vm area struct for the range |
| 162 | * @list: all range lock are on a list |
| 163 | * @start: range virtual start address (inclusive) |
| 164 | * @end: range virtual end address (exclusive) |
| 165 | * @pfns: array of pfns (big enough for the range) |
| 166 | * @flags: pfn flags to match device driver page table |
| 167 | * @values: pfn value for some special case (none, special, error, ...) |
Jérôme Glisse | 023a019 | 2019-05-13 17:20:05 -0700 | [diff] [blame] | 168 | * @default_flags: default flags for the range (write, read, ... see hmm doc) |
| 169 | * @pfn_flags_mask: allows to mask pfn flags so that only default_flags matter |
Jérôme Glisse | f88a1e9 | 2018-04-10 16:29:06 -0700 | [diff] [blame] | 170 | * @pfn_shifts: pfn shift value (should be <= PAGE_SHIFT) |
| 171 | * @valid: pfns array did not change since it has been fill by an HMM function |
| 172 | */ |
| 173 | struct hmm_range { |
Jérôme Glisse | 704f3f2 | 2019-05-13 17:19:48 -0700 | [diff] [blame] | 174 | struct hmm *hmm; |
Jérôme Glisse | f88a1e9 | 2018-04-10 16:29:06 -0700 | [diff] [blame] | 175 | struct vm_area_struct *vma; |
| 176 | struct list_head list; |
| 177 | unsigned long start; |
| 178 | unsigned long end; |
| 179 | uint64_t *pfns; |
| 180 | const uint64_t *flags; |
| 181 | const uint64_t *values; |
Jérôme Glisse | 023a019 | 2019-05-13 17:20:05 -0700 | [diff] [blame] | 182 | uint64_t default_flags; |
| 183 | uint64_t pfn_flags_mask; |
Jérôme Glisse | 63d5066 | 2019-05-13 17:20:18 -0700 | [diff] [blame] | 184 | uint8_t page_shift; |
Jérôme Glisse | f88a1e9 | 2018-04-10 16:29:06 -0700 | [diff] [blame] | 185 | uint8_t pfn_shift; |
| 186 | bool valid; |
| 187 | }; |
Jérôme Glisse | 133ff0e | 2017-09-08 16:11:23 -0700 | [diff] [blame] | 188 | |
| 189 | /* |
Jérôme Glisse | 63d5066 | 2019-05-13 17:20:18 -0700 | [diff] [blame] | 190 | * hmm_range_page_shift() - return the page shift for the range |
| 191 | * @range: range being queried |
| 192 | * Returns: page shift (page size = 1 << page shift) for the range |
| 193 | */ |
| 194 | static inline unsigned hmm_range_page_shift(const struct hmm_range *range) |
| 195 | { |
| 196 | return range->page_shift; |
| 197 | } |
| 198 | |
| 199 | /* |
| 200 | * hmm_range_page_size() - return the page size for the range |
| 201 | * @range: range being queried |
| 202 | * Returns: page size for the range in bytes |
| 203 | */ |
| 204 | static inline unsigned long hmm_range_page_size(const struct hmm_range *range) |
| 205 | { |
| 206 | return 1UL << hmm_range_page_shift(range); |
| 207 | } |
| 208 | |
| 209 | /* |
Jérôme Glisse | a3e0d41 | 2019-05-13 17:20:01 -0700 | [diff] [blame] | 210 | * hmm_range_wait_until_valid() - wait for range to be valid |
| 211 | * @range: range affected by invalidation to wait on |
| 212 | * @timeout: time out for wait in ms (ie abort wait after that period of time) |
| 213 | * Returns: true if the range is valid, false otherwise. |
| 214 | */ |
| 215 | static inline bool hmm_range_wait_until_valid(struct hmm_range *range, |
| 216 | unsigned long timeout) |
| 217 | { |
| 218 | /* Check if mm is dead ? */ |
| 219 | if (range->hmm == NULL || range->hmm->dead || range->hmm->mm == NULL) { |
| 220 | range->valid = false; |
| 221 | return false; |
| 222 | } |
| 223 | if (range->valid) |
| 224 | return true; |
| 225 | wait_event_timeout(range->hmm->wq, range->valid || range->hmm->dead, |
| 226 | msecs_to_jiffies(timeout)); |
| 227 | /* Return current valid status just in case we get lucky */ |
| 228 | return range->valid; |
| 229 | } |
| 230 | |
| 231 | /* |
| 232 | * hmm_range_valid() - test if a range is valid or not |
| 233 | * @range: range |
| 234 | * Returns: true if the range is valid, false otherwise. |
| 235 | */ |
| 236 | static inline bool hmm_range_valid(struct hmm_range *range) |
| 237 | { |
| 238 | return range->valid; |
| 239 | } |
| 240 | |
| 241 | /* |
Jérôme Glisse | ff05c0c | 2018-04-10 16:28:38 -0700 | [diff] [blame] | 242 | * hmm_pfn_to_page() - return struct page pointed to by a valid HMM pfn |
Jérôme Glisse | f88a1e9 | 2018-04-10 16:29:06 -0700 | [diff] [blame] | 243 | * @range: range use to decode HMM pfn value |
Jérôme Glisse | ff05c0c | 2018-04-10 16:28:38 -0700 | [diff] [blame] | 244 | * @pfn: HMM pfn value to get corresponding struct page from |
| 245 | * Returns: struct page pointer if pfn is a valid HMM pfn, NULL otherwise |
Jérôme Glisse | 133ff0e | 2017-09-08 16:11:23 -0700 | [diff] [blame] | 246 | * |
Jérôme Glisse | ff05c0c | 2018-04-10 16:28:38 -0700 | [diff] [blame] | 247 | * If the HMM pfn is valid (ie valid flag set) then return the struct page |
| 248 | * matching the pfn value stored in the HMM pfn. Otherwise return NULL. |
Jérôme Glisse | 133ff0e | 2017-09-08 16:11:23 -0700 | [diff] [blame] | 249 | */ |
Jérôme Glisse | f88a1e9 | 2018-04-10 16:29:06 -0700 | [diff] [blame] | 250 | static inline struct page *hmm_pfn_to_page(const struct hmm_range *range, |
| 251 | uint64_t pfn) |
Jérôme Glisse | 133ff0e | 2017-09-08 16:11:23 -0700 | [diff] [blame] | 252 | { |
Jérôme Glisse | f88a1e9 | 2018-04-10 16:29:06 -0700 | [diff] [blame] | 253 | if (pfn == range->values[HMM_PFN_NONE]) |
Jérôme Glisse | 133ff0e | 2017-09-08 16:11:23 -0700 | [diff] [blame] | 254 | return NULL; |
Jérôme Glisse | f88a1e9 | 2018-04-10 16:29:06 -0700 | [diff] [blame] | 255 | if (pfn == range->values[HMM_PFN_ERROR]) |
| 256 | return NULL; |
| 257 | if (pfn == range->values[HMM_PFN_SPECIAL]) |
| 258 | return NULL; |
| 259 | if (!(pfn & range->flags[HMM_PFN_VALID])) |
| 260 | return NULL; |
| 261 | return pfn_to_page(pfn >> range->pfn_shift); |
Jérôme Glisse | 133ff0e | 2017-09-08 16:11:23 -0700 | [diff] [blame] | 262 | } |
| 263 | |
| 264 | /* |
Jérôme Glisse | ff05c0c | 2018-04-10 16:28:38 -0700 | [diff] [blame] | 265 | * hmm_pfn_to_pfn() - return pfn value store in a HMM pfn |
Jérôme Glisse | f88a1e9 | 2018-04-10 16:29:06 -0700 | [diff] [blame] | 266 | * @range: range use to decode HMM pfn value |
Jérôme Glisse | ff05c0c | 2018-04-10 16:28:38 -0700 | [diff] [blame] | 267 | * @pfn: HMM pfn value to extract pfn from |
| 268 | * Returns: pfn value if HMM pfn is valid, -1UL otherwise |
Jérôme Glisse | 133ff0e | 2017-09-08 16:11:23 -0700 | [diff] [blame] | 269 | */ |
Jérôme Glisse | f88a1e9 | 2018-04-10 16:29:06 -0700 | [diff] [blame] | 270 | static inline unsigned long hmm_pfn_to_pfn(const struct hmm_range *range, |
| 271 | uint64_t pfn) |
Jérôme Glisse | 133ff0e | 2017-09-08 16:11:23 -0700 | [diff] [blame] | 272 | { |
Jérôme Glisse | f88a1e9 | 2018-04-10 16:29:06 -0700 | [diff] [blame] | 273 | if (pfn == range->values[HMM_PFN_NONE]) |
Jérôme Glisse | 133ff0e | 2017-09-08 16:11:23 -0700 | [diff] [blame] | 274 | return -1UL; |
Jérôme Glisse | f88a1e9 | 2018-04-10 16:29:06 -0700 | [diff] [blame] | 275 | if (pfn == range->values[HMM_PFN_ERROR]) |
| 276 | return -1UL; |
| 277 | if (pfn == range->values[HMM_PFN_SPECIAL]) |
| 278 | return -1UL; |
| 279 | if (!(pfn & range->flags[HMM_PFN_VALID])) |
| 280 | return -1UL; |
| 281 | return (pfn >> range->pfn_shift); |
Jérôme Glisse | 133ff0e | 2017-09-08 16:11:23 -0700 | [diff] [blame] | 282 | } |
| 283 | |
| 284 | /* |
Jérôme Glisse | ff05c0c | 2018-04-10 16:28:38 -0700 | [diff] [blame] | 285 | * hmm_pfn_from_page() - create a valid HMM pfn value from struct page |
Jérôme Glisse | f88a1e9 | 2018-04-10 16:29:06 -0700 | [diff] [blame] | 286 | * @range: range use to encode HMM pfn value |
Jérôme Glisse | ff05c0c | 2018-04-10 16:28:38 -0700 | [diff] [blame] | 287 | * @page: struct page pointer for which to create the HMM pfn |
| 288 | * Returns: valid HMM pfn for the page |
Jérôme Glisse | 133ff0e | 2017-09-08 16:11:23 -0700 | [diff] [blame] | 289 | */ |
Jérôme Glisse | f88a1e9 | 2018-04-10 16:29:06 -0700 | [diff] [blame] | 290 | static inline uint64_t hmm_pfn_from_page(const struct hmm_range *range, |
| 291 | struct page *page) |
Jérôme Glisse | 133ff0e | 2017-09-08 16:11:23 -0700 | [diff] [blame] | 292 | { |
Jérôme Glisse | f88a1e9 | 2018-04-10 16:29:06 -0700 | [diff] [blame] | 293 | return (page_to_pfn(page) << range->pfn_shift) | |
| 294 | range->flags[HMM_PFN_VALID]; |
Jérôme Glisse | 133ff0e | 2017-09-08 16:11:23 -0700 | [diff] [blame] | 295 | } |
| 296 | |
| 297 | /* |
Jérôme Glisse | ff05c0c | 2018-04-10 16:28:38 -0700 | [diff] [blame] | 298 | * hmm_pfn_from_pfn() - create a valid HMM pfn value from pfn |
Jérôme Glisse | f88a1e9 | 2018-04-10 16:29:06 -0700 | [diff] [blame] | 299 | * @range: range use to encode HMM pfn value |
Jérôme Glisse | ff05c0c | 2018-04-10 16:28:38 -0700 | [diff] [blame] | 300 | * @pfn: pfn value for which to create the HMM pfn |
| 301 | * Returns: valid HMM pfn for the pfn |
Jérôme Glisse | 133ff0e | 2017-09-08 16:11:23 -0700 | [diff] [blame] | 302 | */ |
Jérôme Glisse | f88a1e9 | 2018-04-10 16:29:06 -0700 | [diff] [blame] | 303 | static inline uint64_t hmm_pfn_from_pfn(const struct hmm_range *range, |
| 304 | unsigned long pfn) |
Jérôme Glisse | 133ff0e | 2017-09-08 16:11:23 -0700 | [diff] [blame] | 305 | { |
Jérôme Glisse | f88a1e9 | 2018-04-10 16:29:06 -0700 | [diff] [blame] | 306 | return (pfn << range->pfn_shift) | |
| 307 | range->flags[HMM_PFN_VALID]; |
Jérôme Glisse | 133ff0e | 2017-09-08 16:11:23 -0700 | [diff] [blame] | 308 | } |
| 309 | |
| 310 | |
Jérôme Glisse | c0b1240 | 2017-09-08 16:11:27 -0700 | [diff] [blame] | 311 | #if IS_ENABLED(CONFIG_HMM_MIRROR) |
| 312 | /* |
| 313 | * Mirroring: how to synchronize device page table with CPU page table. |
| 314 | * |
| 315 | * A device driver that is participating in HMM mirroring must always |
| 316 | * synchronize with CPU page table updates. For this, device drivers can either |
| 317 | * directly use mmu_notifier APIs or they can use the hmm_mirror API. Device |
| 318 | * drivers can decide to register one mirror per device per process, or just |
| 319 | * one mirror per process for a group of devices. The pattern is: |
| 320 | * |
| 321 | * int device_bind_address_space(..., struct mm_struct *mm, ...) |
| 322 | * { |
| 323 | * struct device_address_space *das; |
| 324 | * |
| 325 | * // Device driver specific initialization, and allocation of das |
| 326 | * // which contains an hmm_mirror struct as one of its fields. |
| 327 | * ... |
| 328 | * |
| 329 | * ret = hmm_mirror_register(&das->mirror, mm, &device_mirror_ops); |
| 330 | * if (ret) { |
| 331 | * // Cleanup on error |
| 332 | * return ret; |
| 333 | * } |
| 334 | * |
| 335 | * // Other device driver specific initialization |
| 336 | * ... |
| 337 | * } |
| 338 | * |
| 339 | * Once an hmm_mirror is registered for an address space, the device driver |
| 340 | * will get callbacks through sync_cpu_device_pagetables() operation (see |
| 341 | * hmm_mirror_ops struct). |
| 342 | * |
| 343 | * Device driver must not free the struct containing the hmm_mirror struct |
| 344 | * before calling hmm_mirror_unregister(). The expected usage is to do that when |
| 345 | * the device driver is unbinding from an address space. |
| 346 | * |
| 347 | * |
| 348 | * void device_unbind_address_space(struct device_address_space *das) |
| 349 | * { |
| 350 | * // Device driver specific cleanup |
| 351 | * ... |
| 352 | * |
| 353 | * hmm_mirror_unregister(&das->mirror); |
| 354 | * |
| 355 | * // Other device driver specific cleanup, and now das can be freed |
| 356 | * ... |
| 357 | * } |
| 358 | */ |
| 359 | |
| 360 | struct hmm_mirror; |
| 361 | |
| 362 | /* |
Jérôme Glisse | 44532d4 | 2018-10-30 15:04:24 -0700 | [diff] [blame] | 363 | * enum hmm_update_event - type of update |
Jérôme Glisse | c0b1240 | 2017-09-08 16:11:27 -0700 | [diff] [blame] | 364 | * @HMM_UPDATE_INVALIDATE: invalidate range (no indication as to why) |
| 365 | */ |
Jérôme Glisse | 44532d4 | 2018-10-30 15:04:24 -0700 | [diff] [blame] | 366 | enum hmm_update_event { |
Jérôme Glisse | c0b1240 | 2017-09-08 16:11:27 -0700 | [diff] [blame] | 367 | HMM_UPDATE_INVALIDATE, |
| 368 | }; |
| 369 | |
| 370 | /* |
Jérôme Glisse | 44532d4 | 2018-10-30 15:04:24 -0700 | [diff] [blame] | 371 | * struct hmm_update - HMM update informations for callback |
| 372 | * |
| 373 | * @start: virtual start address of the range to update |
| 374 | * @end: virtual end address of the range to update |
| 375 | * @event: event triggering the update (what is happening) |
| 376 | * @blockable: can the callback block/sleep ? |
| 377 | */ |
| 378 | struct hmm_update { |
| 379 | unsigned long start; |
| 380 | unsigned long end; |
| 381 | enum hmm_update_event event; |
| 382 | bool blockable; |
| 383 | }; |
| 384 | |
| 385 | /* |
Jérôme Glisse | c0b1240 | 2017-09-08 16:11:27 -0700 | [diff] [blame] | 386 | * struct hmm_mirror_ops - HMM mirror device operations callback |
| 387 | * |
| 388 | * @update: callback to update range on a device |
| 389 | */ |
| 390 | struct hmm_mirror_ops { |
Ralph Campbell | e140151 | 2018-04-10 16:28:19 -0700 | [diff] [blame] | 391 | /* release() - release hmm_mirror |
| 392 | * |
| 393 | * @mirror: pointer to struct hmm_mirror |
| 394 | * |
| 395 | * This is called when the mm_struct is being released. |
| 396 | * The callback should make sure no references to the mirror occur |
| 397 | * after the callback returns. |
| 398 | */ |
| 399 | void (*release)(struct hmm_mirror *mirror); |
| 400 | |
Jérôme Glisse | c0b1240 | 2017-09-08 16:11:27 -0700 | [diff] [blame] | 401 | /* sync_cpu_device_pagetables() - synchronize page tables |
| 402 | * |
| 403 | * @mirror: pointer to struct hmm_mirror |
Jérôme Glisse | 44532d4 | 2018-10-30 15:04:24 -0700 | [diff] [blame] | 404 | * @update: update informations (see struct hmm_update) |
| 405 | * Returns: -EAGAIN if update.blockable false and callback need to |
| 406 | * block, 0 otherwise. |
Jérôme Glisse | c0b1240 | 2017-09-08 16:11:27 -0700 | [diff] [blame] | 407 | * |
| 408 | * This callback ultimately originates from mmu_notifiers when the CPU |
| 409 | * page table is updated. The device driver must update its page table |
| 410 | * in response to this callback. The update argument tells what action |
| 411 | * to perform. |
| 412 | * |
| 413 | * The device driver must not return from this callback until the device |
| 414 | * page tables are completely updated (TLBs flushed, etc); this is a |
| 415 | * synchronous call. |
| 416 | */ |
Jérôme Glisse | 44532d4 | 2018-10-30 15:04:24 -0700 | [diff] [blame] | 417 | int (*sync_cpu_device_pagetables)(struct hmm_mirror *mirror, |
| 418 | const struct hmm_update *update); |
Jérôme Glisse | c0b1240 | 2017-09-08 16:11:27 -0700 | [diff] [blame] | 419 | }; |
| 420 | |
| 421 | /* |
| 422 | * struct hmm_mirror - mirror struct for a device driver |
| 423 | * |
| 424 | * @hmm: pointer to struct hmm (which is unique per mm_struct) |
| 425 | * @ops: device driver callback for HMM mirror operations |
| 426 | * @list: for list of mirrors of a given mm |
| 427 | * |
| 428 | * Each address space (mm_struct) being mirrored by a device must register one |
| 429 | * instance of an hmm_mirror struct with HMM. HMM will track the list of all |
| 430 | * mirrors for each mm_struct. |
| 431 | */ |
| 432 | struct hmm_mirror { |
| 433 | struct hmm *hmm; |
| 434 | const struct hmm_mirror_ops *ops; |
| 435 | struct list_head list; |
| 436 | }; |
| 437 | |
| 438 | int hmm_mirror_register(struct hmm_mirror *mirror, struct mm_struct *mm); |
| 439 | void hmm_mirror_unregister(struct hmm_mirror *mirror); |
Jérôme Glisse | da4c3c7 | 2017-09-08 16:11:31 -0700 | [diff] [blame] | 440 | |
Jérôme Glisse | 2023941 | 2019-05-13 17:20:24 -0700 | [diff] [blame] | 441 | /* |
| 442 | * hmm_mirror_mm_is_alive() - test if mm is still alive |
| 443 | * @mirror: the HMM mm mirror for which we want to lock the mmap_sem |
| 444 | * Returns: false if the mm is dead, true otherwise |
| 445 | * |
| 446 | * This is an optimization it will not accurately always return -EINVAL if the |
| 447 | * mm is dead ie there can be false negative (process is being kill but HMM is |
| 448 | * not yet inform of that). It is only intented to be use to optimize out case |
| 449 | * where driver is about to do something time consuming and it would be better |
| 450 | * to skip it if the mm is dead. |
| 451 | */ |
| 452 | static inline bool hmm_mirror_mm_is_alive(struct hmm_mirror *mirror) |
| 453 | { |
| 454 | struct mm_struct *mm; |
| 455 | |
| 456 | if (!mirror || !mirror->hmm) |
| 457 | return false; |
| 458 | mm = READ_ONCE(mirror->hmm->mm); |
| 459 | if (mirror->hmm->dead || !mm) |
| 460 | return false; |
| 461 | |
| 462 | return true; |
| 463 | } |
| 464 | |
Jérôme Glisse | da4c3c7 | 2017-09-08 16:11:31 -0700 | [diff] [blame] | 465 | |
| 466 | /* |
Jérôme Glisse | a3e0d41 | 2019-05-13 17:20:01 -0700 | [diff] [blame] | 467 | * Please see Documentation/vm/hmm.rst for how to use the range API. |
Jérôme Glisse | da4c3c7 | 2017-09-08 16:11:31 -0700 | [diff] [blame] | 468 | */ |
Jérôme Glisse | a3e0d41 | 2019-05-13 17:20:01 -0700 | [diff] [blame] | 469 | int hmm_range_register(struct hmm_range *range, |
| 470 | struct mm_struct *mm, |
| 471 | unsigned long start, |
Jérôme Glisse | 63d5066 | 2019-05-13 17:20:18 -0700 | [diff] [blame] | 472 | unsigned long end, |
| 473 | unsigned page_shift); |
Jérôme Glisse | a3e0d41 | 2019-05-13 17:20:01 -0700 | [diff] [blame] | 474 | void hmm_range_unregister(struct hmm_range *range); |
Jérôme Glisse | 25f23a0 | 2019-05-13 17:19:55 -0700 | [diff] [blame] | 475 | long hmm_range_snapshot(struct hmm_range *range); |
Jérôme Glisse | a3e0d41 | 2019-05-13 17:20:01 -0700 | [diff] [blame] | 476 | long hmm_range_fault(struct hmm_range *range, bool block); |
Jérôme Glisse | 55c0ece | 2019-05-13 17:20:28 -0700 | [diff] [blame^] | 477 | long hmm_range_dma_map(struct hmm_range *range, |
| 478 | struct device *device, |
| 479 | dma_addr_t *daddrs, |
| 480 | bool block); |
| 481 | long hmm_range_dma_unmap(struct hmm_range *range, |
| 482 | struct vm_area_struct *vma, |
| 483 | struct device *device, |
| 484 | dma_addr_t *daddrs, |
| 485 | bool dirty); |
Jérôme Glisse | 74eee18 | 2017-09-08 16:11:35 -0700 | [diff] [blame] | 486 | |
| 487 | /* |
Jérôme Glisse | a3e0d41 | 2019-05-13 17:20:01 -0700 | [diff] [blame] | 488 | * HMM_RANGE_DEFAULT_TIMEOUT - default timeout (ms) when waiting for a range |
Jérôme Glisse | 74eee18 | 2017-09-08 16:11:35 -0700 | [diff] [blame] | 489 | * |
Jérôme Glisse | a3e0d41 | 2019-05-13 17:20:01 -0700 | [diff] [blame] | 490 | * When waiting for mmu notifiers we need some kind of time out otherwise we |
| 491 | * could potentialy wait for ever, 1000ms ie 1s sounds like a long time to |
| 492 | * wait already. |
Jérôme Glisse | 74eee18 | 2017-09-08 16:11:35 -0700 | [diff] [blame] | 493 | */ |
Jérôme Glisse | a3e0d41 | 2019-05-13 17:20:01 -0700 | [diff] [blame] | 494 | #define HMM_RANGE_DEFAULT_TIMEOUT 1000 |
| 495 | |
| 496 | /* This is a temporary helper to avoid merge conflict between trees. */ |
| 497 | static inline bool hmm_vma_range_done(struct hmm_range *range) |
| 498 | { |
| 499 | bool ret = hmm_range_valid(range); |
| 500 | |
| 501 | hmm_range_unregister(range); |
| 502 | return ret; |
| 503 | } |
Jérôme Glisse | 7323161 | 2019-05-13 17:19:58 -0700 | [diff] [blame] | 504 | |
| 505 | /* This is a temporary helper to avoid merge conflict between trees. */ |
| 506 | static inline int hmm_vma_fault(struct hmm_range *range, bool block) |
| 507 | { |
Jérôme Glisse | a3e0d41 | 2019-05-13 17:20:01 -0700 | [diff] [blame] | 508 | long ret; |
| 509 | |
Jérôme Glisse | 023a019 | 2019-05-13 17:20:05 -0700 | [diff] [blame] | 510 | /* |
| 511 | * With the old API the driver must set each individual entries with |
| 512 | * the requested flags (valid, write, ...). So here we set the mask to |
| 513 | * keep intact the entries provided by the driver and zero out the |
| 514 | * default_flags. |
| 515 | */ |
| 516 | range->default_flags = 0; |
| 517 | range->pfn_flags_mask = -1UL; |
| 518 | |
Jérôme Glisse | a3e0d41 | 2019-05-13 17:20:01 -0700 | [diff] [blame] | 519 | ret = hmm_range_register(range, range->vma->vm_mm, |
Jérôme Glisse | 63d5066 | 2019-05-13 17:20:18 -0700 | [diff] [blame] | 520 | range->start, range->end, |
| 521 | PAGE_SHIFT); |
Jérôme Glisse | a3e0d41 | 2019-05-13 17:20:01 -0700 | [diff] [blame] | 522 | if (ret) |
| 523 | return (int)ret; |
| 524 | |
| 525 | if (!hmm_range_wait_until_valid(range, HMM_RANGE_DEFAULT_TIMEOUT)) { |
| 526 | /* |
| 527 | * The mmap_sem was taken by driver we release it here and |
| 528 | * returns -EAGAIN which correspond to mmap_sem have been |
| 529 | * drop in the old API. |
| 530 | */ |
| 531 | up_read(&range->vma->vm_mm->mmap_sem); |
| 532 | return -EAGAIN; |
| 533 | } |
| 534 | |
| 535 | ret = hmm_range_fault(range, block); |
| 536 | if (ret <= 0) { |
| 537 | if (ret == -EBUSY || !ret) { |
| 538 | /* Same as above drop mmap_sem to match old API. */ |
| 539 | up_read(&range->vma->vm_mm->mmap_sem); |
| 540 | ret = -EBUSY; |
| 541 | } else if (ret == -EAGAIN) |
| 542 | ret = -EBUSY; |
| 543 | hmm_range_unregister(range); |
| 544 | return ret; |
| 545 | } |
| 546 | return 0; |
Jérôme Glisse | 7323161 | 2019-05-13 17:19:58 -0700 | [diff] [blame] | 547 | } |
Jérôme Glisse | c0b1240 | 2017-09-08 16:11:27 -0700 | [diff] [blame] | 548 | |
Arnd Bergmann | 9d8a463 | 2018-04-10 16:29:13 -0700 | [diff] [blame] | 549 | /* Below are for HMM internal use only! Not to be used by device driver! */ |
| 550 | void hmm_mm_destroy(struct mm_struct *mm); |
| 551 | |
| 552 | static inline void hmm_mm_init(struct mm_struct *mm) |
| 553 | { |
| 554 | mm->hmm = NULL; |
| 555 | } |
| 556 | #else /* IS_ENABLED(CONFIG_HMM_MIRROR) */ |
| 557 | static inline void hmm_mm_destroy(struct mm_struct *mm) {} |
| 558 | static inline void hmm_mm_init(struct mm_struct *mm) {} |
| 559 | #endif /* IS_ENABLED(CONFIG_HMM_MIRROR) */ |
Jérôme Glisse | c0b1240 | 2017-09-08 16:11:27 -0700 | [diff] [blame] | 560 | |
Jérôme Glisse | df6ad69 | 2017-09-08 16:12:24 -0700 | [diff] [blame] | 561 | #if IS_ENABLED(CONFIG_DEVICE_PRIVATE) || IS_ENABLED(CONFIG_DEVICE_PUBLIC) |
Jérôme Glisse | 4ef589d | 2017-09-08 16:11:58 -0700 | [diff] [blame] | 562 | struct hmm_devmem; |
| 563 | |
| 564 | struct page *hmm_vma_alloc_locked_page(struct vm_area_struct *vma, |
| 565 | unsigned long addr); |
| 566 | |
| 567 | /* |
| 568 | * struct hmm_devmem_ops - callback for ZONE_DEVICE memory events |
| 569 | * |
| 570 | * @free: call when refcount on page reach 1 and thus is no longer use |
| 571 | * @fault: call when there is a page fault to unaddressable memory |
| 572 | * |
| 573 | * Both callback happens from page_free() and page_fault() callback of struct |
| 574 | * dev_pagemap respectively. See include/linux/memremap.h for more details on |
| 575 | * those. |
| 576 | * |
| 577 | * The hmm_devmem_ops callback are just here to provide a coherent and |
| 578 | * uniq API to device driver and device driver should not register their |
| 579 | * own page_free() or page_fault() but rely on the hmm_devmem_ops call- |
| 580 | * back. |
| 581 | */ |
| 582 | struct hmm_devmem_ops { |
| 583 | /* |
| 584 | * free() - free a device page |
| 585 | * @devmem: device memory structure (see struct hmm_devmem) |
| 586 | * @page: pointer to struct page being freed |
| 587 | * |
| 588 | * Call back occurs whenever a device page refcount reach 1 which |
| 589 | * means that no one is holding any reference on the page anymore |
| 590 | * (ZONE_DEVICE page have an elevated refcount of 1 as default so |
| 591 | * that they are not release to the general page allocator). |
| 592 | * |
| 593 | * Note that callback has exclusive ownership of the page (as no |
| 594 | * one is holding any reference). |
| 595 | */ |
| 596 | void (*free)(struct hmm_devmem *devmem, struct page *page); |
| 597 | /* |
| 598 | * fault() - CPU page fault or get user page (GUP) |
| 599 | * @devmem: device memory structure (see struct hmm_devmem) |
| 600 | * @vma: virtual memory area containing the virtual address |
| 601 | * @addr: virtual address that faulted or for which there is a GUP |
| 602 | * @page: pointer to struct page backing virtual address (unreliable) |
| 603 | * @flags: FAULT_FLAG_* (see include/linux/mm.h) |
| 604 | * @pmdp: page middle directory |
| 605 | * Returns: VM_FAULT_MINOR/MAJOR on success or one of VM_FAULT_ERROR |
| 606 | * on error |
| 607 | * |
| 608 | * The callback occurs whenever there is a CPU page fault or GUP on a |
| 609 | * virtual address. This means that the device driver must migrate the |
| 610 | * page back to regular memory (CPU accessible). |
| 611 | * |
| 612 | * The device driver is free to migrate more than one page from the |
| 613 | * fault() callback as an optimization. However if device decide to |
| 614 | * migrate more than one page it must always priotirize the faulting |
| 615 | * address over the others. |
| 616 | * |
| 617 | * The struct page pointer is only given as an hint to allow quick |
| 618 | * lookup of internal device driver data. A concurrent migration |
| 619 | * might have already free that page and the virtual address might |
| 620 | * not longer be back by it. So it should not be modified by the |
| 621 | * callback. |
| 622 | * |
| 623 | * Note that mmap semaphore is held in read mode at least when this |
| 624 | * callback occurs, hence the vma is valid upon callback entry. |
| 625 | */ |
Souptick Joarder | b57e622e6 | 2019-03-11 23:28:10 -0700 | [diff] [blame] | 626 | vm_fault_t (*fault)(struct hmm_devmem *devmem, |
Jérôme Glisse | 4ef589d | 2017-09-08 16:11:58 -0700 | [diff] [blame] | 627 | struct vm_area_struct *vma, |
| 628 | unsigned long addr, |
| 629 | const struct page *page, |
| 630 | unsigned int flags, |
| 631 | pmd_t *pmdp); |
| 632 | }; |
| 633 | |
| 634 | /* |
| 635 | * struct hmm_devmem - track device memory |
| 636 | * |
| 637 | * @completion: completion object for device memory |
| 638 | * @pfn_first: first pfn for this resource (set by hmm_devmem_add()) |
| 639 | * @pfn_last: last pfn for this resource (set by hmm_devmem_add()) |
| 640 | * @resource: IO resource reserved for this chunk of memory |
| 641 | * @pagemap: device page map for that chunk |
| 642 | * @device: device to bind resource to |
| 643 | * @ops: memory operations callback |
| 644 | * @ref: per CPU refcount |
Dan Williams | 063a7d1 | 2018-12-28 00:39:46 -0800 | [diff] [blame] | 645 | * @page_fault: callback when CPU fault on an unaddressable device page |
Jérôme Glisse | 4ef589d | 2017-09-08 16:11:58 -0700 | [diff] [blame] | 646 | * |
| 647 | * This an helper structure for device drivers that do not wish to implement |
| 648 | * the gory details related to hotplugging new memoy and allocating struct |
| 649 | * pages. |
| 650 | * |
| 651 | * Device drivers can directly use ZONE_DEVICE memory on their own if they |
| 652 | * wish to do so. |
Dan Williams | 063a7d1 | 2018-12-28 00:39:46 -0800 | [diff] [blame] | 653 | * |
| 654 | * The page_fault() callback must migrate page back, from device memory to |
| 655 | * system memory, so that the CPU can access it. This might fail for various |
| 656 | * reasons (device issues, device have been unplugged, ...). When such error |
| 657 | * conditions happen, the page_fault() callback must return VM_FAULT_SIGBUS and |
| 658 | * set the CPU page table entry to "poisoned". |
| 659 | * |
| 660 | * Note that because memory cgroup charges are transferred to the device memory, |
| 661 | * this should never fail due to memory restrictions. However, allocation |
| 662 | * of a regular system page might still fail because we are out of memory. If |
| 663 | * that happens, the page_fault() callback must return VM_FAULT_OOM. |
| 664 | * |
| 665 | * The page_fault() callback can also try to migrate back multiple pages in one |
| 666 | * chunk, as an optimization. It must, however, prioritize the faulting address |
| 667 | * over all the others. |
Jérôme Glisse | 4ef589d | 2017-09-08 16:11:58 -0700 | [diff] [blame] | 668 | */ |
Souptick Joarder | b57e622e6 | 2019-03-11 23:28:10 -0700 | [diff] [blame] | 669 | typedef vm_fault_t (*dev_page_fault_t)(struct vm_area_struct *vma, |
Dan Williams | 063a7d1 | 2018-12-28 00:39:46 -0800 | [diff] [blame] | 670 | unsigned long addr, |
| 671 | const struct page *page, |
| 672 | unsigned int flags, |
| 673 | pmd_t *pmdp); |
| 674 | |
Jérôme Glisse | 4ef589d | 2017-09-08 16:11:58 -0700 | [diff] [blame] | 675 | struct hmm_devmem { |
| 676 | struct completion completion; |
| 677 | unsigned long pfn_first; |
| 678 | unsigned long pfn_last; |
| 679 | struct resource *resource; |
| 680 | struct device *device; |
| 681 | struct dev_pagemap pagemap; |
| 682 | const struct hmm_devmem_ops *ops; |
| 683 | struct percpu_ref ref; |
Dan Williams | 063a7d1 | 2018-12-28 00:39:46 -0800 | [diff] [blame] | 684 | dev_page_fault_t page_fault; |
Jérôme Glisse | 4ef589d | 2017-09-08 16:11:58 -0700 | [diff] [blame] | 685 | }; |
| 686 | |
| 687 | /* |
| 688 | * To add (hotplug) device memory, HMM assumes that there is no real resource |
| 689 | * that reserves a range in the physical address space (this is intended to be |
| 690 | * use by unaddressable device memory). It will reserve a physical range big |
| 691 | * enough and allocate struct page for it. |
| 692 | * |
| 693 | * The device driver can wrap the hmm_devmem struct inside a private device |
Dan Williams | 58ef15b | 2018-12-28 00:35:07 -0800 | [diff] [blame] | 694 | * driver struct. |
Jérôme Glisse | 4ef589d | 2017-09-08 16:11:58 -0700 | [diff] [blame] | 695 | */ |
| 696 | struct hmm_devmem *hmm_devmem_add(const struct hmm_devmem_ops *ops, |
| 697 | struct device *device, |
| 698 | unsigned long size); |
Jérôme Glisse | d3df0a4 | 2017-09-08 16:12:28 -0700 | [diff] [blame] | 699 | struct hmm_devmem *hmm_devmem_add_resource(const struct hmm_devmem_ops *ops, |
| 700 | struct device *device, |
| 701 | struct resource *res); |
Jérôme Glisse | 4ef589d | 2017-09-08 16:11:58 -0700 | [diff] [blame] | 702 | |
| 703 | /* |
| 704 | * hmm_devmem_page_set_drvdata - set per-page driver data field |
| 705 | * |
| 706 | * @page: pointer to struct page |
| 707 | * @data: driver data value to set |
| 708 | * |
| 709 | * Because page can not be on lru we have an unsigned long that driver can use |
| 710 | * to store a per page field. This just a simple helper to do that. |
| 711 | */ |
| 712 | static inline void hmm_devmem_page_set_drvdata(struct page *page, |
| 713 | unsigned long data) |
| 714 | { |
Matthew Wilcox | 50e7fbc | 2018-06-07 17:09:01 -0700 | [diff] [blame] | 715 | page->hmm_data = data; |
Jérôme Glisse | 4ef589d | 2017-09-08 16:11:58 -0700 | [diff] [blame] | 716 | } |
| 717 | |
| 718 | /* |
| 719 | * hmm_devmem_page_get_drvdata - get per page driver data field |
| 720 | * |
| 721 | * @page: pointer to struct page |
| 722 | * Return: driver data value |
| 723 | */ |
Ralph Campbell | 0bea803 | 2017-11-15 17:34:00 -0800 | [diff] [blame] | 724 | static inline unsigned long hmm_devmem_page_get_drvdata(const struct page *page) |
Jérôme Glisse | 4ef589d | 2017-09-08 16:11:58 -0700 | [diff] [blame] | 725 | { |
Matthew Wilcox | 50e7fbc | 2018-06-07 17:09:01 -0700 | [diff] [blame] | 726 | return page->hmm_data; |
Jérôme Glisse | 4ef589d | 2017-09-08 16:11:58 -0700 | [diff] [blame] | 727 | } |
Jérôme Glisse | 858b54d | 2017-09-08 16:12:02 -0700 | [diff] [blame] | 728 | |
| 729 | |
| 730 | /* |
| 731 | * struct hmm_device - fake device to hang device memory onto |
| 732 | * |
| 733 | * @device: device struct |
| 734 | * @minor: device minor number |
| 735 | */ |
| 736 | struct hmm_device { |
| 737 | struct device device; |
| 738 | unsigned int minor; |
| 739 | }; |
| 740 | |
| 741 | /* |
| 742 | * A device driver that wants to handle multiple devices memory through a |
| 743 | * single fake device can use hmm_device to do so. This is purely a helper and |
| 744 | * it is not strictly needed, in order to make use of any HMM functionality. |
| 745 | */ |
| 746 | struct hmm_device *hmm_device_new(void *drvdata); |
| 747 | void hmm_device_put(struct hmm_device *hmm_device); |
Jérôme Glisse | df6ad69 | 2017-09-08 16:12:24 -0700 | [diff] [blame] | 748 | #endif /* CONFIG_DEVICE_PRIVATE || CONFIG_DEVICE_PUBLIC */ |
Jérôme Glisse | 6b368cd | 2017-09-08 16:12:32 -0700 | [diff] [blame] | 749 | #else /* IS_ENABLED(CONFIG_HMM) */ |
| 750 | static inline void hmm_mm_destroy(struct mm_struct *mm) {} |
| 751 | static inline void hmm_mm_init(struct mm_struct *mm) {} |
Jérôme Glisse | b28b08d | 2018-04-10 16:28:15 -0700 | [diff] [blame] | 752 | #endif /* IS_ENABLED(CONFIG_HMM) */ |
Arnd Bergmann | 9d8a463 | 2018-04-10 16:29:13 -0700 | [diff] [blame] | 753 | |
Jérôme Glisse | 133ff0e | 2017-09-08 16:11:23 -0700 | [diff] [blame] | 754 | #endif /* LINUX_HMM_H */ |