]> git.proxmox.com Git - mirror_ubuntu-jammy-kernel.git/blame - include/linux/hmm.h
mm/hmm: return the fault type from hmm_pte_need_fault()
[mirror_ubuntu-jammy-kernel.git] / include / linux / hmm.h
CommitLineData
c942fddf 1/* SPDX-License-Identifier: GPL-2.0-or-later */
133ff0ea
JG
2/*
3 * Copyright 2013 Red Hat Inc.
4 *
f813f219 5 * Authors: Jérôme Glisse <jglisse@redhat.com>
133ff0ea
JG
6 */
7/*
8 * Heterogeneous Memory Management (HMM)
9 *
ad56b738 10 * See Documentation/vm/hmm.rst for reasons and overview of what HMM is and it
133ff0ea
JG
11 * 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 *
085ea250
RC
24 * 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
133ff0ea
JG
26 * 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>
063a7d1d 63#include <asm/pgtable.h>
133ff0ea 64
858b54da 65#include <linux/device.h>
4ef589dc
JG
66#include <linux/migrate.h>
67#include <linux/memremap.h>
68#include <linux/completion.h>
a3e0d41c 69#include <linux/mmu_notifier.h>
4ef589dc 70
133ff0ea 71/*
f88a1e90
JG
72 * hmm_pfn_flag_e - HMM flag enums
73 *
133ff0ea 74 * Flags:
86586a41 75 * HMM_PFN_VALID: pfn is valid. It has, at least, read permission.
133ff0ea 76 * HMM_PFN_WRITE: CPU page table has write permission set
f88a1e90 77 *
085ea250
RC
78 * The driver provides a flags array for mapping page protections to device
79 * PTE bits. If the driver valid bit for an entry is bit 3,
80 * i.e., (entry & (1 << 3)), then the driver must provide
f88a1e90 81 * an array in hmm_range.flags with hmm_range.flags[HMM_PFN_VALID] == 1 << 3.
085ea250 82 * Same logic apply to all flags. This is the same idea as vm_page_prot in vma
f88a1e90
JG
83 * except that this is per device driver rather than per architecture.
84 */
85enum hmm_pfn_flag_e {
86 HMM_PFN_VALID = 0,
87 HMM_PFN_WRITE,
f88a1e90
JG
88 HMM_PFN_FLAG_MAX
89};
90
91/*
92 * hmm_pfn_value_e - HMM pfn special value
93 *
94 * Flags:
da4c3c73 95 * HMM_PFN_ERROR: corresponding CPU page table entry points to poisoned memory
f88a1e90 96 * HMM_PFN_NONE: corresponding CPU page table entry is pte_none()
da4c3c73 97 * HMM_PFN_SPECIAL: corresponding CPU page table entry is special; i.e., the
67fa1666 98 * result of vmf_insert_pfn() or vm_insert_page(). Therefore, it should not
da4c3c73
JG
99 * be mirrored by a device, because the entry will never have HMM_PFN_VALID
100 * set and the pfn value is undefined.
f88a1e90 101 *
085ea250
RC
102 * Driver provides values for none entry, error entry, and special entry.
103 * Driver can alias (i.e., use same value) error and special, but
104 * it should not alias none with error or special.
f88a1e90
JG
105 *
106 * HMM pfn value returned by hmm_vma_get_pfns() or hmm_vma_fault() will be:
107 * hmm_range.values[HMM_PFN_ERROR] if CPU page table entry is poisonous,
085ea250 108 * hmm_range.values[HMM_PFN_NONE] if there is no CPU page table entry,
f88a1e90 109 * hmm_range.values[HMM_PFN_SPECIAL] if CPU page table entry is a special one
133ff0ea 110 */
f88a1e90
JG
111enum hmm_pfn_value_e {
112 HMM_PFN_ERROR,
113 HMM_PFN_NONE,
114 HMM_PFN_SPECIAL,
115 HMM_PFN_VALUE_MAX
116};
117
118/*
119 * struct hmm_range - track invalidation lock on virtual address range
120 *
a22dd506
JG
121 * @notifier: a mmu_interval_notifier that includes the start/end
122 * @notifier_seq: result of mmu_interval_read_begin()
704f3f2c 123 * @hmm: the core HMM structure this range is active against
f88a1e90
JG
124 * @vma: the vm area struct for the range
125 * @list: all range lock are on a list
126 * @start: range virtual start address (inclusive)
127 * @end: range virtual end address (exclusive)
128 * @pfns: array of pfns (big enough for the range)
129 * @flags: pfn flags to match device driver page table
130 * @values: pfn value for some special case (none, special, error, ...)
023a019a
JG
131 * @default_flags: default flags for the range (write, read, ... see hmm doc)
132 * @pfn_flags_mask: allows to mask pfn flags so that only default_flags matter
f88a1e90
JG
133 * @pfn_shifts: pfn shift value (should be <= PAGE_SHIFT)
134 * @valid: pfns array did not change since it has been fill by an HMM function
08ddddda 135 * @dev_private_owner: owner of device private pages
f88a1e90
JG
136 */
137struct hmm_range {
04ec32fb
JG
138 struct mmu_interval_notifier *notifier;
139 unsigned long notifier_seq;
f88a1e90
JG
140 unsigned long start;
141 unsigned long end;
142 uint64_t *pfns;
143 const uint64_t *flags;
144 const uint64_t *values;
023a019a
JG
145 uint64_t default_flags;
146 uint64_t pfn_flags_mask;
f88a1e90 147 uint8_t pfn_shift;
08ddddda 148 void *dev_private_owner;
f88a1e90 149};
133ff0ea
JG
150
151/*
391aab11
JG
152 * 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
085ea250 155 * Return: struct page pointer if entry is a valid, NULL otherwise
133ff0ea 156 *
391aab11
JG
157 * If the device entry is valid (ie valid flag set) then return the struct page
158 * matching the entry value. Otherwise return NULL.
133ff0ea 159 */
391aab11
JG
160static inline struct page *hmm_device_entry_to_page(const struct hmm_range *range,
161 uint64_t entry)
133ff0ea 162{
391aab11 163 if (entry == range->values[HMM_PFN_NONE])
f88a1e90 164 return NULL;
391aab11 165 if (entry == range->values[HMM_PFN_ERROR])
f88a1e90 166 return NULL;
391aab11 167 if (entry == range->values[HMM_PFN_SPECIAL])
133ff0ea 168 return NULL;
391aab11 169 if (!(entry & range->flags[HMM_PFN_VALID]))
f88a1e90 170 return NULL;
391aab11 171 return pfn_to_page(entry >> range->pfn_shift);
133ff0ea
JG
172}
173
174/*
391aab11
JG
175 * 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
085ea250 178 * Return: pfn value if device entry is valid, -1UL otherwise
133ff0ea 179 */
391aab11
JG
180static inline unsigned long
181hmm_device_entry_to_pfn(const struct hmm_range *range, uint64_t pfn)
133ff0ea 182{
f88a1e90
JG
183 if (pfn == range->values[HMM_PFN_NONE])
184 return -1UL;
185 if (pfn == range->values[HMM_PFN_ERROR])
186 return -1UL;
187 if (pfn == range->values[HMM_PFN_SPECIAL])
133ff0ea 188 return -1UL;
f88a1e90
JG
189 if (!(pfn & range->flags[HMM_PFN_VALID]))
190 return -1UL;
191 return (pfn >> range->pfn_shift);
133ff0ea
JG
192}
193
194/*
391aab11 195 * hmm_device_entry_from_page() - create a valid device entry for a page
f88a1e90 196 * @range: range use to encode HMM pfn value
391aab11 197 * @page: page for which to create the device entry
085ea250 198 * Return: valid device entry for the page
133ff0ea 199 */
391aab11
JG
200static inline uint64_t hmm_device_entry_from_page(const struct hmm_range *range,
201 struct page *page)
133ff0ea 202{
f88a1e90
JG
203 return (page_to_pfn(page) << range->pfn_shift) |
204 range->flags[HMM_PFN_VALID];
133ff0ea
JG
205}
206
207/*
391aab11 208 * hmm_device_entry_from_pfn() - create a valid device entry value from pfn
f88a1e90 209 * @range: range use to encode HMM pfn value
391aab11 210 * @pfn: pfn value for which to create the device entry
085ea250 211 * Return: valid device entry for the pfn
133ff0ea 212 */
391aab11
JG
213static inline uint64_t hmm_device_entry_from_pfn(const struct hmm_range *range,
214 unsigned long pfn)
133ff0ea 215{
f88a1e90
JG
216 return (pfn << range->pfn_shift) |
217 range->flags[HMM_PFN_VALID];
133ff0ea
JG
218}
219
107e8998
JG
220/* Don't fault in missing PTEs, just snapshot the current state. */
221#define HMM_FAULT_SNAPSHOT (1 << 1)
222
da4c3c73 223/*
a3e0d41c 224 * Please see Documentation/vm/hmm.rst for how to use the range API.
da4c3c73 225 */
9a4903e4 226long hmm_range_fault(struct hmm_range *range, unsigned int flags);
74eee180
JG
227
228/*
a3e0d41c 229 * HMM_RANGE_DEFAULT_TIMEOUT - default timeout (ms) when waiting for a range
74eee180 230 *
a3e0d41c
JG
231 * When waiting for mmu notifiers we need some kind of time out otherwise we
232 * could potentialy wait for ever, 1000ms ie 1s sounds like a long time to
233 * wait already.
74eee180 234 */
a3e0d41c
JG
235#define HMM_RANGE_DEFAULT_TIMEOUT 1000
236
133ff0ea 237#endif /* LINUX_HMM_H */