blob: dee2f8953b2e93b07ab4e82bf63f49867c77f1c7 [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 *
Jérôme Glissef813f212018-10-30 15:04:06 -070014 * Authors: Jérôme Glisse <jglisse@redhat.com>
Jérôme Glisse133ff0e2017-09-08 16:11:23 -070015 */
16/*
17 * Heterogeneous Memory Management (HMM)
18 *
Mike Rapoportad56b732018-03-21 21:22:47 +020019 * See Documentation/vm/hmm.rst for reasons and overview of what HMM is and it
Jérôme Glisse133ff0e2017-09-08 16:11:23 -070020 * 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 Williams063a7d12018-12-28 00:39:46 -080072#include <asm/pgtable.h>
Jérôme Glisse133ff0e2017-09-08 16:11:23 -070073
74#if IS_ENABLED(CONFIG_HMM)
75
Jérôme Glisse858b54d2017-09-08 16:12:02 -070076#include <linux/device.h>
Jérôme Glisse4ef589d2017-09-08 16:11:58 -070077#include <linux/migrate.h>
78#include <linux/memremap.h>
79#include <linux/completion.h>
Jérôme Glissea3e0d412019-05-13 17:20:01 -070080#include <linux/mmu_notifier.h>
Jérôme Glisse4ef589d2017-09-08 16:11:58 -070081
Jérôme Glissea3e0d412019-05-13 17:20:01 -070082
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 */
96struct 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 Glisse133ff0e2017-09-08 16:11:23 -0700108
109/*
Jérôme Glissef88a1e92018-04-10 16:29:06 -0700110 * hmm_pfn_flag_e - HMM flag enums
111 *
Jérôme Glisse133ff0e2017-09-08 16:11:23 -0700112 * Flags:
Jérôme Glisse86586a42018-04-10 16:28:34 -0700113 * HMM_PFN_VALID: pfn is valid. It has, at least, read permission.
Jérôme Glisse133ff0e2017-09-08 16:11:23 -0700114 * HMM_PFN_WRITE: CPU page table has write permission set
Jérôme Glissef88a1e92018-04-10 16:29:06 -0700115 * 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 */
123enum 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 Glisseda4c3c72017-09-08 16:11:31 -0700134 * HMM_PFN_ERROR: corresponding CPU page table entry points to poisoned memory
Jérôme Glissef88a1e92018-04-10 16:29:06 -0700135 * HMM_PFN_NONE: corresponding CPU page table entry is pte_none()
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700136 * HMM_PFN_SPECIAL: corresponding CPU page table entry is special; i.e., the
Matthew Wilcox67fa1662018-10-26 15:04:26 -0700137 * result of vmf_insert_pfn() or vm_insert_page(). Therefore, it should not
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700138 * 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 Glissef88a1e92018-04-10 16:29:06 -0700140 *
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 Glisse133ff0e2017-09-08 16:11:23 -0700149 */
Jérôme Glissef88a1e92018-04-10 16:29:06 -0700150enum 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 Glisse704f3f22019-05-13 17:19:48 -0700160 * @hmm: the core HMM structure this range is active against
Jérôme Glissef88a1e92018-04-10 16:29:06 -0700161 * @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 Glisse023a0192019-05-13 17:20:05 -0700168 * @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 Glissef88a1e92018-04-10 16:29:06 -0700170 * @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 */
173struct hmm_range {
Jérôme Glisse704f3f22019-05-13 17:19:48 -0700174 struct hmm *hmm;
Jérôme Glissef88a1e92018-04-10 16:29:06 -0700175 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 Glisse023a0192019-05-13 17:20:05 -0700182 uint64_t default_flags;
183 uint64_t pfn_flags_mask;
Jérôme Glissef88a1e92018-04-10 16:29:06 -0700184 uint8_t pfn_shift;
185 bool valid;
186};
Jérôme Glisse133ff0e2017-09-08 16:11:23 -0700187
188/*
Jérôme Glissea3e0d412019-05-13 17:20:01 -0700189 * hmm_range_wait_until_valid() - wait for range to be valid
190 * @range: range affected by invalidation to wait on
191 * @timeout: time out for wait in ms (ie abort wait after that period of time)
192 * Returns: true if the range is valid, false otherwise.
193 */
194static inline bool hmm_range_wait_until_valid(struct hmm_range *range,
195 unsigned long timeout)
196{
197 /* Check if mm is dead ? */
198 if (range->hmm == NULL || range->hmm->dead || range->hmm->mm == NULL) {
199 range->valid = false;
200 return false;
201 }
202 if (range->valid)
203 return true;
204 wait_event_timeout(range->hmm->wq, range->valid || range->hmm->dead,
205 msecs_to_jiffies(timeout));
206 /* Return current valid status just in case we get lucky */
207 return range->valid;
208}
209
210/*
211 * hmm_range_valid() - test if a range is valid or not
212 * @range: range
213 * Returns: true if the range is valid, false otherwise.
214 */
215static inline bool hmm_range_valid(struct hmm_range *range)
216{
217 return range->valid;
218}
219
220/*
Jérôme Glisseff05c0c2018-04-10 16:28:38 -0700221 * hmm_pfn_to_page() - return struct page pointed to by a valid HMM pfn
Jérôme Glissef88a1e92018-04-10 16:29:06 -0700222 * @range: range use to decode HMM pfn value
Jérôme Glisseff05c0c2018-04-10 16:28:38 -0700223 * @pfn: HMM pfn value to get corresponding struct page from
224 * Returns: struct page pointer if pfn is a valid HMM pfn, NULL otherwise
Jérôme Glisse133ff0e2017-09-08 16:11:23 -0700225 *
Jérôme Glisseff05c0c2018-04-10 16:28:38 -0700226 * If the HMM pfn is valid (ie valid flag set) then return the struct page
227 * matching the pfn value stored in the HMM pfn. Otherwise return NULL.
Jérôme Glisse133ff0e2017-09-08 16:11:23 -0700228 */
Jérôme Glissef88a1e92018-04-10 16:29:06 -0700229static inline struct page *hmm_pfn_to_page(const struct hmm_range *range,
230 uint64_t pfn)
Jérôme Glisse133ff0e2017-09-08 16:11:23 -0700231{
Jérôme Glissef88a1e92018-04-10 16:29:06 -0700232 if (pfn == range->values[HMM_PFN_NONE])
Jérôme Glisse133ff0e2017-09-08 16:11:23 -0700233 return NULL;
Jérôme Glissef88a1e92018-04-10 16:29:06 -0700234 if (pfn == range->values[HMM_PFN_ERROR])
235 return NULL;
236 if (pfn == range->values[HMM_PFN_SPECIAL])
237 return NULL;
238 if (!(pfn & range->flags[HMM_PFN_VALID]))
239 return NULL;
240 return pfn_to_page(pfn >> range->pfn_shift);
Jérôme Glisse133ff0e2017-09-08 16:11:23 -0700241}
242
243/*
Jérôme Glisseff05c0c2018-04-10 16:28:38 -0700244 * hmm_pfn_to_pfn() - return pfn value store in a HMM pfn
Jérôme Glissef88a1e92018-04-10 16:29:06 -0700245 * @range: range use to decode HMM pfn value
Jérôme Glisseff05c0c2018-04-10 16:28:38 -0700246 * @pfn: HMM pfn value to extract pfn from
247 * Returns: pfn value if HMM pfn is valid, -1UL otherwise
Jérôme Glisse133ff0e2017-09-08 16:11:23 -0700248 */
Jérôme Glissef88a1e92018-04-10 16:29:06 -0700249static inline unsigned long hmm_pfn_to_pfn(const struct hmm_range *range,
250 uint64_t pfn)
Jérôme Glisse133ff0e2017-09-08 16:11:23 -0700251{
Jérôme Glissef88a1e92018-04-10 16:29:06 -0700252 if (pfn == range->values[HMM_PFN_NONE])
Jérôme Glisse133ff0e2017-09-08 16:11:23 -0700253 return -1UL;
Jérôme Glissef88a1e92018-04-10 16:29:06 -0700254 if (pfn == range->values[HMM_PFN_ERROR])
255 return -1UL;
256 if (pfn == range->values[HMM_PFN_SPECIAL])
257 return -1UL;
258 if (!(pfn & range->flags[HMM_PFN_VALID]))
259 return -1UL;
260 return (pfn >> range->pfn_shift);
Jérôme Glisse133ff0e2017-09-08 16:11:23 -0700261}
262
263/*
Jérôme Glisseff05c0c2018-04-10 16:28:38 -0700264 * hmm_pfn_from_page() - create a valid HMM pfn value from struct page
Jérôme Glissef88a1e92018-04-10 16:29:06 -0700265 * @range: range use to encode HMM pfn value
Jérôme Glisseff05c0c2018-04-10 16:28:38 -0700266 * @page: struct page pointer for which to create the HMM pfn
267 * Returns: valid HMM pfn for the page
Jérôme Glisse133ff0e2017-09-08 16:11:23 -0700268 */
Jérôme Glissef88a1e92018-04-10 16:29:06 -0700269static inline uint64_t hmm_pfn_from_page(const struct hmm_range *range,
270 struct page *page)
Jérôme Glisse133ff0e2017-09-08 16:11:23 -0700271{
Jérôme Glissef88a1e92018-04-10 16:29:06 -0700272 return (page_to_pfn(page) << range->pfn_shift) |
273 range->flags[HMM_PFN_VALID];
Jérôme Glisse133ff0e2017-09-08 16:11:23 -0700274}
275
276/*
Jérôme Glisseff05c0c2018-04-10 16:28:38 -0700277 * hmm_pfn_from_pfn() - create a valid HMM pfn value from pfn
Jérôme Glissef88a1e92018-04-10 16:29:06 -0700278 * @range: range use to encode HMM pfn value
Jérôme Glisseff05c0c2018-04-10 16:28:38 -0700279 * @pfn: pfn value for which to create the HMM pfn
280 * Returns: valid HMM pfn for the pfn
Jérôme Glisse133ff0e2017-09-08 16:11:23 -0700281 */
Jérôme Glissef88a1e92018-04-10 16:29:06 -0700282static inline uint64_t hmm_pfn_from_pfn(const struct hmm_range *range,
283 unsigned long pfn)
Jérôme Glisse133ff0e2017-09-08 16:11:23 -0700284{
Jérôme Glissef88a1e92018-04-10 16:29:06 -0700285 return (pfn << range->pfn_shift) |
286 range->flags[HMM_PFN_VALID];
Jérôme Glisse133ff0e2017-09-08 16:11:23 -0700287}
288
289
Jérôme Glissec0b12402017-09-08 16:11:27 -0700290#if IS_ENABLED(CONFIG_HMM_MIRROR)
291/*
292 * Mirroring: how to synchronize device page table with CPU page table.
293 *
294 * A device driver that is participating in HMM mirroring must always
295 * synchronize with CPU page table updates. For this, device drivers can either
296 * directly use mmu_notifier APIs or they can use the hmm_mirror API. Device
297 * drivers can decide to register one mirror per device per process, or just
298 * one mirror per process for a group of devices. The pattern is:
299 *
300 * int device_bind_address_space(..., struct mm_struct *mm, ...)
301 * {
302 * struct device_address_space *das;
303 *
304 * // Device driver specific initialization, and allocation of das
305 * // which contains an hmm_mirror struct as one of its fields.
306 * ...
307 *
308 * ret = hmm_mirror_register(&das->mirror, mm, &device_mirror_ops);
309 * if (ret) {
310 * // Cleanup on error
311 * return ret;
312 * }
313 *
314 * // Other device driver specific initialization
315 * ...
316 * }
317 *
318 * Once an hmm_mirror is registered for an address space, the device driver
319 * will get callbacks through sync_cpu_device_pagetables() operation (see
320 * hmm_mirror_ops struct).
321 *
322 * Device driver must not free the struct containing the hmm_mirror struct
323 * before calling hmm_mirror_unregister(). The expected usage is to do that when
324 * the device driver is unbinding from an address space.
325 *
326 *
327 * void device_unbind_address_space(struct device_address_space *das)
328 * {
329 * // Device driver specific cleanup
330 * ...
331 *
332 * hmm_mirror_unregister(&das->mirror);
333 *
334 * // Other device driver specific cleanup, and now das can be freed
335 * ...
336 * }
337 */
338
339struct hmm_mirror;
340
341/*
Jérôme Glisse44532d42018-10-30 15:04:24 -0700342 * enum hmm_update_event - type of update
Jérôme Glissec0b12402017-09-08 16:11:27 -0700343 * @HMM_UPDATE_INVALIDATE: invalidate range (no indication as to why)
344 */
Jérôme Glisse44532d42018-10-30 15:04:24 -0700345enum hmm_update_event {
Jérôme Glissec0b12402017-09-08 16:11:27 -0700346 HMM_UPDATE_INVALIDATE,
347};
348
349/*
Jérôme Glisse44532d42018-10-30 15:04:24 -0700350 * struct hmm_update - HMM update informations for callback
351 *
352 * @start: virtual start address of the range to update
353 * @end: virtual end address of the range to update
354 * @event: event triggering the update (what is happening)
355 * @blockable: can the callback block/sleep ?
356 */
357struct hmm_update {
358 unsigned long start;
359 unsigned long end;
360 enum hmm_update_event event;
361 bool blockable;
362};
363
364/*
Jérôme Glissec0b12402017-09-08 16:11:27 -0700365 * struct hmm_mirror_ops - HMM mirror device operations callback
366 *
367 * @update: callback to update range on a device
368 */
369struct hmm_mirror_ops {
Ralph Campbelle1401512018-04-10 16:28:19 -0700370 /* release() - release hmm_mirror
371 *
372 * @mirror: pointer to struct hmm_mirror
373 *
374 * This is called when the mm_struct is being released.
375 * The callback should make sure no references to the mirror occur
376 * after the callback returns.
377 */
378 void (*release)(struct hmm_mirror *mirror);
379
Jérôme Glissec0b12402017-09-08 16:11:27 -0700380 /* sync_cpu_device_pagetables() - synchronize page tables
381 *
382 * @mirror: pointer to struct hmm_mirror
Jérôme Glisse44532d42018-10-30 15:04:24 -0700383 * @update: update informations (see struct hmm_update)
384 * Returns: -EAGAIN if update.blockable false and callback need to
385 * block, 0 otherwise.
Jérôme Glissec0b12402017-09-08 16:11:27 -0700386 *
387 * This callback ultimately originates from mmu_notifiers when the CPU
388 * page table is updated. The device driver must update its page table
389 * in response to this callback. The update argument tells what action
390 * to perform.
391 *
392 * The device driver must not return from this callback until the device
393 * page tables are completely updated (TLBs flushed, etc); this is a
394 * synchronous call.
395 */
Jérôme Glisse44532d42018-10-30 15:04:24 -0700396 int (*sync_cpu_device_pagetables)(struct hmm_mirror *mirror,
397 const struct hmm_update *update);
Jérôme Glissec0b12402017-09-08 16:11:27 -0700398};
399
400/*
401 * struct hmm_mirror - mirror struct for a device driver
402 *
403 * @hmm: pointer to struct hmm (which is unique per mm_struct)
404 * @ops: device driver callback for HMM mirror operations
405 * @list: for list of mirrors of a given mm
406 *
407 * Each address space (mm_struct) being mirrored by a device must register one
408 * instance of an hmm_mirror struct with HMM. HMM will track the list of all
409 * mirrors for each mm_struct.
410 */
411struct hmm_mirror {
412 struct hmm *hmm;
413 const struct hmm_mirror_ops *ops;
414 struct list_head list;
415};
416
417int hmm_mirror_register(struct hmm_mirror *mirror, struct mm_struct *mm);
418void hmm_mirror_unregister(struct hmm_mirror *mirror);
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700419
420
421/*
Jérôme Glissea3e0d412019-05-13 17:20:01 -0700422 * Please see Documentation/vm/hmm.rst for how to use the range API.
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700423 */
Jérôme Glissea3e0d412019-05-13 17:20:01 -0700424int hmm_range_register(struct hmm_range *range,
425 struct mm_struct *mm,
426 unsigned long start,
427 unsigned long end);
428void hmm_range_unregister(struct hmm_range *range);
Jérôme Glisse25f23a02019-05-13 17:19:55 -0700429long hmm_range_snapshot(struct hmm_range *range);
Jérôme Glissea3e0d412019-05-13 17:20:01 -0700430long hmm_range_fault(struct hmm_range *range, bool block);
Jérôme Glisse74eee182017-09-08 16:11:35 -0700431
432/*
Jérôme Glissea3e0d412019-05-13 17:20:01 -0700433 * HMM_RANGE_DEFAULT_TIMEOUT - default timeout (ms) when waiting for a range
Jérôme Glisse74eee182017-09-08 16:11:35 -0700434 *
Jérôme Glissea3e0d412019-05-13 17:20:01 -0700435 * When waiting for mmu notifiers we need some kind of time out otherwise we
436 * could potentialy wait for ever, 1000ms ie 1s sounds like a long time to
437 * wait already.
Jérôme Glisse74eee182017-09-08 16:11:35 -0700438 */
Jérôme Glissea3e0d412019-05-13 17:20:01 -0700439#define HMM_RANGE_DEFAULT_TIMEOUT 1000
440
441/* This is a temporary helper to avoid merge conflict between trees. */
442static inline bool hmm_vma_range_done(struct hmm_range *range)
443{
444 bool ret = hmm_range_valid(range);
445
446 hmm_range_unregister(range);
447 return ret;
448}
Jérôme Glisse73231612019-05-13 17:19:58 -0700449
450/* This is a temporary helper to avoid merge conflict between trees. */
451static inline int hmm_vma_fault(struct hmm_range *range, bool block)
452{
Jérôme Glissea3e0d412019-05-13 17:20:01 -0700453 long ret;
454
Jérôme Glisse023a0192019-05-13 17:20:05 -0700455 /*
456 * With the old API the driver must set each individual entries with
457 * the requested flags (valid, write, ...). So here we set the mask to
458 * keep intact the entries provided by the driver and zero out the
459 * default_flags.
460 */
461 range->default_flags = 0;
462 range->pfn_flags_mask = -1UL;
463
Jérôme Glissea3e0d412019-05-13 17:20:01 -0700464 ret = hmm_range_register(range, range->vma->vm_mm,
465 range->start, range->end);
466 if (ret)
467 return (int)ret;
468
469 if (!hmm_range_wait_until_valid(range, HMM_RANGE_DEFAULT_TIMEOUT)) {
470 /*
471 * The mmap_sem was taken by driver we release it here and
472 * returns -EAGAIN which correspond to mmap_sem have been
473 * drop in the old API.
474 */
475 up_read(&range->vma->vm_mm->mmap_sem);
476 return -EAGAIN;
477 }
478
479 ret = hmm_range_fault(range, block);
480 if (ret <= 0) {
481 if (ret == -EBUSY || !ret) {
482 /* Same as above drop mmap_sem to match old API. */
483 up_read(&range->vma->vm_mm->mmap_sem);
484 ret = -EBUSY;
485 } else if (ret == -EAGAIN)
486 ret = -EBUSY;
487 hmm_range_unregister(range);
488 return ret;
489 }
490 return 0;
Jérôme Glisse73231612019-05-13 17:19:58 -0700491}
Jérôme Glissec0b12402017-09-08 16:11:27 -0700492
Arnd Bergmann9d8a4632018-04-10 16:29:13 -0700493/* Below are for HMM internal use only! Not to be used by device driver! */
494void hmm_mm_destroy(struct mm_struct *mm);
495
496static inline void hmm_mm_init(struct mm_struct *mm)
497{
498 mm->hmm = NULL;
499}
500#else /* IS_ENABLED(CONFIG_HMM_MIRROR) */
501static inline void hmm_mm_destroy(struct mm_struct *mm) {}
502static inline void hmm_mm_init(struct mm_struct *mm) {}
503#endif /* IS_ENABLED(CONFIG_HMM_MIRROR) */
Jérôme Glissec0b12402017-09-08 16:11:27 -0700504
Jérôme Glissedf6ad692017-09-08 16:12:24 -0700505#if IS_ENABLED(CONFIG_DEVICE_PRIVATE) || IS_ENABLED(CONFIG_DEVICE_PUBLIC)
Jérôme Glisse4ef589d2017-09-08 16:11:58 -0700506struct hmm_devmem;
507
508struct page *hmm_vma_alloc_locked_page(struct vm_area_struct *vma,
509 unsigned long addr);
510
511/*
512 * struct hmm_devmem_ops - callback for ZONE_DEVICE memory events
513 *
514 * @free: call when refcount on page reach 1 and thus is no longer use
515 * @fault: call when there is a page fault to unaddressable memory
516 *
517 * Both callback happens from page_free() and page_fault() callback of struct
518 * dev_pagemap respectively. See include/linux/memremap.h for more details on
519 * those.
520 *
521 * The hmm_devmem_ops callback are just here to provide a coherent and
522 * uniq API to device driver and device driver should not register their
523 * own page_free() or page_fault() but rely on the hmm_devmem_ops call-
524 * back.
525 */
526struct hmm_devmem_ops {
527 /*
528 * free() - free a device page
529 * @devmem: device memory structure (see struct hmm_devmem)
530 * @page: pointer to struct page being freed
531 *
532 * Call back occurs whenever a device page refcount reach 1 which
533 * means that no one is holding any reference on the page anymore
534 * (ZONE_DEVICE page have an elevated refcount of 1 as default so
535 * that they are not release to the general page allocator).
536 *
537 * Note that callback has exclusive ownership of the page (as no
538 * one is holding any reference).
539 */
540 void (*free)(struct hmm_devmem *devmem, struct page *page);
541 /*
542 * fault() - CPU page fault or get user page (GUP)
543 * @devmem: device memory structure (see struct hmm_devmem)
544 * @vma: virtual memory area containing the virtual address
545 * @addr: virtual address that faulted or for which there is a GUP
546 * @page: pointer to struct page backing virtual address (unreliable)
547 * @flags: FAULT_FLAG_* (see include/linux/mm.h)
548 * @pmdp: page middle directory
549 * Returns: VM_FAULT_MINOR/MAJOR on success or one of VM_FAULT_ERROR
550 * on error
551 *
552 * The callback occurs whenever there is a CPU page fault or GUP on a
553 * virtual address. This means that the device driver must migrate the
554 * page back to regular memory (CPU accessible).
555 *
556 * The device driver is free to migrate more than one page from the
557 * fault() callback as an optimization. However if device decide to
558 * migrate more than one page it must always priotirize the faulting
559 * address over the others.
560 *
561 * The struct page pointer is only given as an hint to allow quick
562 * lookup of internal device driver data. A concurrent migration
563 * might have already free that page and the virtual address might
564 * not longer be back by it. So it should not be modified by the
565 * callback.
566 *
567 * Note that mmap semaphore is held in read mode at least when this
568 * callback occurs, hence the vma is valid upon callback entry.
569 */
Souptick Joarderb57e622e62019-03-11 23:28:10 -0700570 vm_fault_t (*fault)(struct hmm_devmem *devmem,
Jérôme Glisse4ef589d2017-09-08 16:11:58 -0700571 struct vm_area_struct *vma,
572 unsigned long addr,
573 const struct page *page,
574 unsigned int flags,
575 pmd_t *pmdp);
576};
577
578/*
579 * struct hmm_devmem - track device memory
580 *
581 * @completion: completion object for device memory
582 * @pfn_first: first pfn for this resource (set by hmm_devmem_add())
583 * @pfn_last: last pfn for this resource (set by hmm_devmem_add())
584 * @resource: IO resource reserved for this chunk of memory
585 * @pagemap: device page map for that chunk
586 * @device: device to bind resource to
587 * @ops: memory operations callback
588 * @ref: per CPU refcount
Dan Williams063a7d12018-12-28 00:39:46 -0800589 * @page_fault: callback when CPU fault on an unaddressable device page
Jérôme Glisse4ef589d2017-09-08 16:11:58 -0700590 *
591 * This an helper structure for device drivers that do not wish to implement
592 * the gory details related to hotplugging new memoy and allocating struct
593 * pages.
594 *
595 * Device drivers can directly use ZONE_DEVICE memory on their own if they
596 * wish to do so.
Dan Williams063a7d12018-12-28 00:39:46 -0800597 *
598 * The page_fault() callback must migrate page back, from device memory to
599 * system memory, so that the CPU can access it. This might fail for various
600 * reasons (device issues, device have been unplugged, ...). When such error
601 * conditions happen, the page_fault() callback must return VM_FAULT_SIGBUS and
602 * set the CPU page table entry to "poisoned".
603 *
604 * Note that because memory cgroup charges are transferred to the device memory,
605 * this should never fail due to memory restrictions. However, allocation
606 * of a regular system page might still fail because we are out of memory. If
607 * that happens, the page_fault() callback must return VM_FAULT_OOM.
608 *
609 * The page_fault() callback can also try to migrate back multiple pages in one
610 * chunk, as an optimization. It must, however, prioritize the faulting address
611 * over all the others.
Jérôme Glisse4ef589d2017-09-08 16:11:58 -0700612 */
Souptick Joarderb57e622e62019-03-11 23:28:10 -0700613typedef vm_fault_t (*dev_page_fault_t)(struct vm_area_struct *vma,
Dan Williams063a7d12018-12-28 00:39:46 -0800614 unsigned long addr,
615 const struct page *page,
616 unsigned int flags,
617 pmd_t *pmdp);
618
Jérôme Glisse4ef589d2017-09-08 16:11:58 -0700619struct hmm_devmem {
620 struct completion completion;
621 unsigned long pfn_first;
622 unsigned long pfn_last;
623 struct resource *resource;
624 struct device *device;
625 struct dev_pagemap pagemap;
626 const struct hmm_devmem_ops *ops;
627 struct percpu_ref ref;
Dan Williams063a7d12018-12-28 00:39:46 -0800628 dev_page_fault_t page_fault;
Jérôme Glisse4ef589d2017-09-08 16:11:58 -0700629};
630
631/*
632 * To add (hotplug) device memory, HMM assumes that there is no real resource
633 * that reserves a range in the physical address space (this is intended to be
634 * use by unaddressable device memory). It will reserve a physical range big
635 * enough and allocate struct page for it.
636 *
637 * The device driver can wrap the hmm_devmem struct inside a private device
Dan Williams58ef15b2018-12-28 00:35:07 -0800638 * driver struct.
Jérôme Glisse4ef589d2017-09-08 16:11:58 -0700639 */
640struct hmm_devmem *hmm_devmem_add(const struct hmm_devmem_ops *ops,
641 struct device *device,
642 unsigned long size);
Jérôme Glissed3df0a42017-09-08 16:12:28 -0700643struct hmm_devmem *hmm_devmem_add_resource(const struct hmm_devmem_ops *ops,
644 struct device *device,
645 struct resource *res);
Jérôme Glisse4ef589d2017-09-08 16:11:58 -0700646
647/*
648 * hmm_devmem_page_set_drvdata - set per-page driver data field
649 *
650 * @page: pointer to struct page
651 * @data: driver data value to set
652 *
653 * Because page can not be on lru we have an unsigned long that driver can use
654 * to store a per page field. This just a simple helper to do that.
655 */
656static inline void hmm_devmem_page_set_drvdata(struct page *page,
657 unsigned long data)
658{
Matthew Wilcox50e7fbc2018-06-07 17:09:01 -0700659 page->hmm_data = data;
Jérôme Glisse4ef589d2017-09-08 16:11:58 -0700660}
661
662/*
663 * hmm_devmem_page_get_drvdata - get per page driver data field
664 *
665 * @page: pointer to struct page
666 * Return: driver data value
667 */
Ralph Campbell0bea8032017-11-15 17:34:00 -0800668static inline unsigned long hmm_devmem_page_get_drvdata(const struct page *page)
Jérôme Glisse4ef589d2017-09-08 16:11:58 -0700669{
Matthew Wilcox50e7fbc2018-06-07 17:09:01 -0700670 return page->hmm_data;
Jérôme Glisse4ef589d2017-09-08 16:11:58 -0700671}
Jérôme Glisse858b54d2017-09-08 16:12:02 -0700672
673
674/*
675 * struct hmm_device - fake device to hang device memory onto
676 *
677 * @device: device struct
678 * @minor: device minor number
679 */
680struct hmm_device {
681 struct device device;
682 unsigned int minor;
683};
684
685/*
686 * A device driver that wants to handle multiple devices memory through a
687 * single fake device can use hmm_device to do so. This is purely a helper and
688 * it is not strictly needed, in order to make use of any HMM functionality.
689 */
690struct hmm_device *hmm_device_new(void *drvdata);
691void hmm_device_put(struct hmm_device *hmm_device);
Jérôme Glissedf6ad692017-09-08 16:12:24 -0700692#endif /* CONFIG_DEVICE_PRIVATE || CONFIG_DEVICE_PUBLIC */
Jérôme Glisse6b368cd2017-09-08 16:12:32 -0700693#else /* IS_ENABLED(CONFIG_HMM) */
694static inline void hmm_mm_destroy(struct mm_struct *mm) {}
695static inline void hmm_mm_init(struct mm_struct *mm) {}
Jérôme Glisseb28b08d2018-04-10 16:28:15 -0700696#endif /* IS_ENABLED(CONFIG_HMM) */
Arnd Bergmann9d8a4632018-04-10 16:29:13 -0700697
Jérôme Glisse133ff0e2017-09-08 16:11:23 -0700698#endif /* LINUX_HMM_H */