blob: 1225b3c87aba058a099a0c1ba53f2641e60dea5a [file] [log] [blame]
Thomas Gleixnerc942fdd2019-05-27 08:55:06 +02001/* SPDX-License-Identifier: GPL-2.0-or-later */
Jérôme Glisse133ff0e2017-09-08 16:11:23 -07002/*
3 * Copyright 2013 Red Hat Inc.
4 *
Jérôme Glissef813f212018-10-30 15:04:06 -07005 * Authors: Jérôme Glisse <jglisse@redhat.com>
Jérôme Glisse133ff0e2017-09-08 16:11:23 -07006 */
7/*
8 * Heterogeneous Memory Management (HMM)
9 *
Mike Rapoportad56b732018-03-21 21:22:47 +020010 * See Documentation/vm/hmm.rst for reasons and overview of what HMM is and it
Jérôme Glisse133ff0e2017-09-08 16:11:23 -070011 * is for. Here we focus on the HMM API description, with some explanation of
12 * the underlying implementation.
13 *
14 * Short description: HMM provides a set of helpers to share a virtual address
15 * space between CPU and a device, so that the device can access any valid
16 * address of the process (while still obeying memory protection). HMM also
17 * provides helpers to migrate process memory to device memory, and back. Each
18 * set of functionality (address space mirroring, and migration to and from
19 * device memory) can be used independently of the other.
20 *
21 *
22 * HMM address space mirroring API:
23 *
Ralph Campbell085ea252019-05-06 16:29:39 -070024 * Use HMM address space mirroring if you want to mirror a range of the CPU
25 * page tables of a process into a device page table. Here, "mirror" means "keep
Jérôme Glisse133ff0e2017-09-08 16:11:23 -070026 * synchronized". Prerequisites: the device must provide the ability to write-
27 * protect its page tables (at PAGE_SIZE granularity), and must be able to
28 * recover from the resulting potential page faults.
29 *
30 * HMM guarantees that at any point in time, a given virtual address points to
31 * either the same memory in both CPU and device page tables (that is: CPU and
32 * device page tables each point to the same pages), or that one page table (CPU
33 * or device) points to no entry, while the other still points to the old page
34 * for the address. The latter case happens when the CPU page table update
35 * happens first, and then the update is mirrored over to the device page table.
36 * This does not cause any issue, because the CPU page table cannot start
37 * pointing to a new page until the device page table is invalidated.
38 *
39 * HMM uses mmu_notifiers to monitor the CPU page tables, and forwards any
40 * updates to each device driver that has registered a mirror. It also provides
41 * some API calls to help with taking a snapshot of the CPU page table, and to
42 * synchronize with any updates that might happen concurrently.
43 *
44 *
45 * HMM migration to and from device memory:
46 *
47 * HMM provides a set of helpers to hotplug device memory as ZONE_DEVICE, with
48 * a new MEMORY_DEVICE_PRIVATE type. This provides a struct page for each page
49 * of the device memory, and allows the device driver to manage its memory
50 * using those struct pages. Having struct pages for device memory makes
51 * migration easier. Because that memory is not addressable by the CPU it must
52 * never be pinned to the device; in other words, any CPU page fault can always
53 * cause the device memory to be migrated (copied/moved) back to regular memory.
54 *
55 * A new migrate helper (migrate_vma()) has been added (see mm/migrate.c) that
56 * allows use of a device DMA engine to perform the copy operation between
57 * regular system memory and device memory.
58 */
59#ifndef LINUX_HMM_H
60#define LINUX_HMM_H
61
62#include <linux/kconfig.h>
Dan Williams063a7d12018-12-28 00:39:46 -080063#include <asm/pgtable.h>
Jérôme Glisse133ff0e2017-09-08 16:11:23 -070064
Jérôme Glisse858b54d2017-09-08 16:12:02 -070065#include <linux/device.h>
Jérôme Glisse4ef589d2017-09-08 16:11:58 -070066#include <linux/migrate.h>
67#include <linux/memremap.h>
68#include <linux/completion.h>
Jérôme Glissea3e0d412019-05-13 17:20:01 -070069#include <linux/mmu_notifier.h>
Jérôme Glisse4ef589d2017-09-08 16:11:58 -070070
Jérôme Glisse133ff0e2017-09-08 16:11:23 -070071/*
Jérôme Glissef88a1e92018-04-10 16:29:06 -070072 * hmm_pfn_flag_e - HMM flag enums
73 *
Jérôme Glisse133ff0e2017-09-08 16:11:23 -070074 * Flags:
Jérôme Glisse86586a42018-04-10 16:28:34 -070075 * HMM_PFN_VALID: pfn is valid. It has, at least, read permission.
Jérôme Glisse133ff0e2017-09-08 16:11:23 -070076 * HMM_PFN_WRITE: CPU page table has write permission set
Jérôme Glissef88a1e92018-04-10 16:29:06 -070077 * HMM_PFN_DEVICE_PRIVATE: private device memory (ZONE_DEVICE)
78 *
Ralph Campbell085ea252019-05-06 16:29:39 -070079 * The driver provides a flags array for mapping page protections to device
80 * PTE bits. If the driver valid bit for an entry is bit 3,
81 * i.e., (entry & (1 << 3)), then the driver must provide
Jérôme Glissef88a1e92018-04-10 16:29:06 -070082 * an array in hmm_range.flags with hmm_range.flags[HMM_PFN_VALID] == 1 << 3.
Ralph Campbell085ea252019-05-06 16:29:39 -070083 * Same logic apply to all flags. This is the same idea as vm_page_prot in vma
Jérôme Glissef88a1e92018-04-10 16:29:06 -070084 * except that this is per device driver rather than per architecture.
85 */
86enum hmm_pfn_flag_e {
87 HMM_PFN_VALID = 0,
88 HMM_PFN_WRITE,
89 HMM_PFN_DEVICE_PRIVATE,
90 HMM_PFN_FLAG_MAX
91};
92
93/*
94 * hmm_pfn_value_e - HMM pfn special value
95 *
96 * Flags:
Jérôme Glisseda4c3c72017-09-08 16:11:31 -070097 * HMM_PFN_ERROR: corresponding CPU page table entry points to poisoned memory
Jérôme Glissef88a1e92018-04-10 16:29:06 -070098 * HMM_PFN_NONE: corresponding CPU page table entry is pte_none()
Jérôme Glisseda4c3c72017-09-08 16:11:31 -070099 * HMM_PFN_SPECIAL: corresponding CPU page table entry is special; i.e., the
Matthew Wilcox67fa1662018-10-26 15:04:26 -0700100 * result of vmf_insert_pfn() or vm_insert_page(). Therefore, it should not
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700101 * be mirrored by a device, because the entry will never have HMM_PFN_VALID
102 * set and the pfn value is undefined.
Jérôme Glissef88a1e92018-04-10 16:29:06 -0700103 *
Ralph Campbell085ea252019-05-06 16:29:39 -0700104 * Driver provides values for none entry, error entry, and special entry.
105 * Driver can alias (i.e., use same value) error and special, but
106 * it should not alias none with error or special.
Jérôme Glissef88a1e92018-04-10 16:29:06 -0700107 *
108 * HMM pfn value returned by hmm_vma_get_pfns() or hmm_vma_fault() will be:
109 * hmm_range.values[HMM_PFN_ERROR] if CPU page table entry is poisonous,
Ralph Campbell085ea252019-05-06 16:29:39 -0700110 * hmm_range.values[HMM_PFN_NONE] if there is no CPU page table entry,
Jérôme Glissef88a1e92018-04-10 16:29:06 -0700111 * hmm_range.values[HMM_PFN_SPECIAL] if CPU page table entry is a special one
Jérôme Glisse133ff0e2017-09-08 16:11:23 -0700112 */
Jérôme Glissef88a1e92018-04-10 16:29:06 -0700113enum hmm_pfn_value_e {
114 HMM_PFN_ERROR,
115 HMM_PFN_NONE,
116 HMM_PFN_SPECIAL,
117 HMM_PFN_VALUE_MAX
118};
119
120/*
121 * struct hmm_range - track invalidation lock on virtual address range
122 *
Jason Gunthorpea22dd502019-11-12 16:22:30 -0400123 * @notifier: a mmu_interval_notifier that includes the start/end
124 * @notifier_seq: result of mmu_interval_read_begin()
Jérôme Glisse704f3f22019-05-13 17:19:48 -0700125 * @hmm: the core HMM structure this range is active against
Jérôme Glissef88a1e92018-04-10 16:29:06 -0700126 * @vma: the vm area struct for the range
127 * @list: all range lock are on a list
128 * @start: range virtual start address (inclusive)
129 * @end: range virtual end address (exclusive)
130 * @pfns: array of pfns (big enough for the range)
131 * @flags: pfn flags to match device driver page table
132 * @values: pfn value for some special case (none, special, error, ...)
Jérôme Glisse023a0192019-05-13 17:20:05 -0700133 * @default_flags: default flags for the range (write, read, ... see hmm doc)
134 * @pfn_flags_mask: allows to mask pfn flags so that only default_flags matter
Jérôme Glissef88a1e92018-04-10 16:29:06 -0700135 * @pfn_shifts: pfn shift value (should be <= PAGE_SHIFT)
136 * @valid: pfns array did not change since it has been fill by an HMM function
137 */
138struct hmm_range {
Jason Gunthorpe04ec32f2019-11-12 16:22:20 -0400139 struct mmu_interval_notifier *notifier;
140 unsigned long notifier_seq;
Jérôme Glissef88a1e92018-04-10 16:29:06 -0700141 unsigned long start;
142 unsigned long end;
143 uint64_t *pfns;
144 const uint64_t *flags;
145 const uint64_t *values;
Jérôme Glisse023a0192019-05-13 17:20:05 -0700146 uint64_t default_flags;
147 uint64_t pfn_flags_mask;
Jérôme Glissef88a1e92018-04-10 16:29:06 -0700148 uint8_t pfn_shift;
Jérôme Glissef88a1e92018-04-10 16:29:06 -0700149};
Jérôme Glisse133ff0e2017-09-08 16:11:23 -0700150
151/*
Jérôme Glisse391aab12019-05-13 17:20:31 -0700152 * hmm_device_entry_to_page() - return struct page pointed to by a device entry
153 * @range: range use to decode device entry value
154 * @entry: device entry value to get corresponding struct page from
Ralph Campbell085ea252019-05-06 16:29:39 -0700155 * Return: struct page pointer if entry is a valid, NULL otherwise
Jérôme Glisse133ff0e2017-09-08 16:11:23 -0700156 *
Jérôme Glisse391aab12019-05-13 17:20:31 -0700157 * If the device entry is valid (ie valid flag set) then return the struct page
158 * matching the entry value. Otherwise return NULL.
Jérôme Glisse133ff0e2017-09-08 16:11:23 -0700159 */
Jérôme Glisse391aab12019-05-13 17:20:31 -0700160static inline struct page *hmm_device_entry_to_page(const struct hmm_range *range,
161 uint64_t entry)
Jérôme Glisse133ff0e2017-09-08 16:11:23 -0700162{
Jérôme Glisse391aab12019-05-13 17:20:31 -0700163 if (entry == range->values[HMM_PFN_NONE])
Jérôme Glisse133ff0e2017-09-08 16:11:23 -0700164 return NULL;
Jérôme Glisse391aab12019-05-13 17:20:31 -0700165 if (entry == range->values[HMM_PFN_ERROR])
Jérôme Glissef88a1e92018-04-10 16:29:06 -0700166 return NULL;
Jérôme Glisse391aab12019-05-13 17:20:31 -0700167 if (entry == range->values[HMM_PFN_SPECIAL])
Jérôme Glissef88a1e92018-04-10 16:29:06 -0700168 return NULL;
Jérôme Glisse391aab12019-05-13 17:20:31 -0700169 if (!(entry & range->flags[HMM_PFN_VALID]))
Jérôme Glissef88a1e92018-04-10 16:29:06 -0700170 return NULL;
Jérôme Glisse391aab12019-05-13 17:20:31 -0700171 return pfn_to_page(entry >> range->pfn_shift);
Jérôme Glisse133ff0e2017-09-08 16:11:23 -0700172}
173
174/*
Jérôme Glisse391aab12019-05-13 17:20:31 -0700175 * hmm_device_entry_to_pfn() - return pfn value store in a device entry
176 * @range: range use to decode device entry value
177 * @entry: device entry to extract pfn from
Ralph Campbell085ea252019-05-06 16:29:39 -0700178 * Return: pfn value if device entry is valid, -1UL otherwise
Jérôme Glisse133ff0e2017-09-08 16:11:23 -0700179 */
Jérôme Glisse391aab12019-05-13 17:20:31 -0700180static inline unsigned long
181hmm_device_entry_to_pfn(const struct hmm_range *range, uint64_t pfn)
Jérôme Glisse133ff0e2017-09-08 16:11:23 -0700182{
Jérôme Glissef88a1e92018-04-10 16:29:06 -0700183 if (pfn == range->values[HMM_PFN_NONE])
Jérôme Glisse133ff0e2017-09-08 16:11:23 -0700184 return -1UL;
Jérôme Glissef88a1e92018-04-10 16:29:06 -0700185 if (pfn == range->values[HMM_PFN_ERROR])
186 return -1UL;
187 if (pfn == range->values[HMM_PFN_SPECIAL])
188 return -1UL;
189 if (!(pfn & range->flags[HMM_PFN_VALID]))
190 return -1UL;
191 return (pfn >> range->pfn_shift);
Jérôme Glisse133ff0e2017-09-08 16:11:23 -0700192}
193
194/*
Jérôme Glisse391aab12019-05-13 17:20:31 -0700195 * hmm_device_entry_from_page() - create a valid device entry for a page
Jérôme Glissef88a1e92018-04-10 16:29:06 -0700196 * @range: range use to encode HMM pfn value
Jérôme Glisse391aab12019-05-13 17:20:31 -0700197 * @page: page for which to create the device entry
Ralph Campbell085ea252019-05-06 16:29:39 -0700198 * Return: valid device entry for the page
Jérôme Glisse133ff0e2017-09-08 16:11:23 -0700199 */
Jérôme Glisse391aab12019-05-13 17:20:31 -0700200static inline uint64_t hmm_device_entry_from_page(const struct hmm_range *range,
201 struct page *page)
Jérôme Glisse133ff0e2017-09-08 16:11:23 -0700202{
Jérôme Glissef88a1e92018-04-10 16:29:06 -0700203 return (page_to_pfn(page) << range->pfn_shift) |
204 range->flags[HMM_PFN_VALID];
Jérôme Glisse133ff0e2017-09-08 16:11:23 -0700205}
206
207/*
Jérôme Glisse391aab12019-05-13 17:20:31 -0700208 * hmm_device_entry_from_pfn() - create a valid device entry value from pfn
Jérôme Glissef88a1e92018-04-10 16:29:06 -0700209 * @range: range use to encode HMM pfn value
Jérôme Glisse391aab12019-05-13 17:20:31 -0700210 * @pfn: pfn value for which to create the device entry
Ralph Campbell085ea252019-05-06 16:29:39 -0700211 * Return: valid device entry for the pfn
Jérôme Glisse133ff0e2017-09-08 16:11:23 -0700212 */
Jérôme Glisse391aab12019-05-13 17:20:31 -0700213static inline uint64_t hmm_device_entry_from_pfn(const struct hmm_range *range,
214 unsigned long pfn)
Jérôme Glisse133ff0e2017-09-08 16:11:23 -0700215{
Jérôme Glissef88a1e92018-04-10 16:29:06 -0700216 return (pfn << range->pfn_shift) |
217 range->flags[HMM_PFN_VALID];
Jérôme Glisse133ff0e2017-09-08 16:11:23 -0700218}
219
Jérôme Glisse391aab12019-05-13 17:20:31 -0700220/*
Jason Gunthorpe107e8992019-11-12 16:22:21 -0400221 * Retry fault if non-blocking, drop mmap_sem and return -EAGAIN in that case.
222 */
223#define HMM_FAULT_ALLOW_RETRY (1 << 0)
224
225/* Don't fault in missing PTEs, just snapshot the current state. */
226#define HMM_FAULT_SNAPSHOT (1 << 1)
227
228#ifdef CONFIG_HMM_MIRROR
Jérôme Glisse20239412019-05-13 17:20:24 -0700229/*
Jérôme Glissea3e0d412019-05-13 17:20:01 -0700230 * Please see Documentation/vm/hmm.rst for how to use the range API.
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700231 */
Christoph Hellwig9a4903e2019-07-25 17:56:46 -0700232long hmm_range_fault(struct hmm_range *range, unsigned int flags);
233
Jérôme Glisse55c0ece2019-05-13 17:20:28 -0700234long hmm_range_dma_map(struct hmm_range *range,
235 struct device *device,
236 dma_addr_t *daddrs,
Christoph Hellwig9a4903e2019-07-25 17:56:46 -0700237 unsigned int flags);
Jérôme Glisse55c0ece2019-05-13 17:20:28 -0700238long hmm_range_dma_unmap(struct hmm_range *range,
Jérôme Glisse55c0ece2019-05-13 17:20:28 -0700239 struct device *device,
240 dma_addr_t *daddrs,
241 bool dirty);
Jason Gunthorpe107e8992019-11-12 16:22:21 -0400242#else
Jason Gunthorpe107e8992019-11-12 16:22:21 -0400243static inline long hmm_range_fault(struct hmm_range *range, unsigned int flags)
244{
245 return -EOPNOTSUPP;
246}
247
248static inline long hmm_range_dma_map(struct hmm_range *range,
249 struct device *device, dma_addr_t *daddrs,
250 unsigned int flags)
251{
252 return -EOPNOTSUPP;
253}
254
255static inline long hmm_range_dma_unmap(struct hmm_range *range,
256 struct device *device,
257 dma_addr_t *daddrs, bool dirty)
258{
259 return -EOPNOTSUPP;
260}
261#endif
Jérôme Glisse74eee182017-09-08 16:11:35 -0700262
263/*
Jérôme Glissea3e0d412019-05-13 17:20:01 -0700264 * HMM_RANGE_DEFAULT_TIMEOUT - default timeout (ms) when waiting for a range
Jérôme Glisse74eee182017-09-08 16:11:35 -0700265 *
Jérôme Glissea3e0d412019-05-13 17:20:01 -0700266 * When waiting for mmu notifiers we need some kind of time out otherwise we
267 * could potentialy wait for ever, 1000ms ie 1s sounds like a long time to
268 * wait already.
Jérôme Glisse74eee182017-09-08 16:11:35 -0700269 */
Jérôme Glissea3e0d412019-05-13 17:20:01 -0700270#define HMM_RANGE_DEFAULT_TIMEOUT 1000
271
Jérôme Glisse133ff0e2017-09-08 16:11:23 -0700272#endif /* LINUX_HMM_H */