-
Notifications
You must be signed in to change notification settings - Fork 10
/
amap.cpp
272 lines (241 loc) · 6.8 KB
/
amap.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
/*
* Copyright (c) 2023 Pedro Falcato
* This file is part of Onyx, and is released under the terms of the MIT License
* check LICENSE at the root directory for more information
*
* SPDX-License-Identifier: MIT
*/
#include <onyx/mm/amap.h>
#include <onyx/mm/slab.h>
#include <onyx/page.h>
#include <onyx/memory.hpp>
__always_inline void amap_init(struct amap *amap)
{
new (amap) struct amap;
spinlock_init(&amap->am_lock);
amap->am_refc = 1;
amap->am_size = 0;
}
/**
* @brief Allocate a new anonymous memory map
*
* @param size Size of the amap
* @return struct amap*
*/
struct amap *amap_alloc(size_t size)
{
struct amap *amap = (struct amap *) kmalloc(sizeof(struct amap), GFP_KERNEL);
if (!amap)
return nullptr;
amap_init(amap);
amap->am_size = size;
return amap;
}
void amap_free(struct amap *amap)
{
DCHECK(amap->am_refc == 0);
auto cursor = radix_tree::cursor::from_index(&amap->am_map);
while (!cursor.is_end())
{
auto page = (struct page *) cursor.get();
dec_page_stat(page, NR_ANON);
free_page(page);
cursor.advance();
}
amap->~amap();
kfree(amap);
}
/**
* @brief Create a copy of an amap
*
* @param amap amap to copy
* @return New amap, or NULL in case of OOM
*/
static struct amap *amap_copy(struct amap *amap)
{
struct amap *namap = amap_alloc(amap->am_size);
if (!namap)
return nullptr;
auto ex = amap->am_map.copy(
[](unsigned long entry, void *ctx) -> unsigned long {
struct page *page = (struct page *) entry;
page_ref(page);
return entry;
},
nullptr);
if (ex.has_error())
{
amap_free(namap);
return nullptr;
}
namap->am_map = cul::move(ex.value());
return namap;
}
/**
* @brief Add a page to an amap
*
* @param amap Amap to add to
* @param page Page to add
* @param region Region to which the amap belongs
* @param pgoff Page offset (in pfn, shifted right by PAGE_SHIFT)
* @param nocopy Don't copy if we find an old page
* @return 0 on success, negative error codes
*/
int amap_add(struct amap *amap, struct page *page, struct vm_area_struct *region, size_t pgoff,
bool nocopy)
{
if (amap->am_refc > 1) [[unlikely]]
{
/* Note: We do not need a lock here, no one can touch this amap while am_refc > 1 */
struct amap *namap = amap_copy(amap);
if (!namap)
return -ENOMEM;
amap_unref(amap);
amap = namap;
region->vm_amap = amap;
}
scoped_lock g{amap->am_lock};
auto old = amap->am_map.xchg(pgoff, (unsigned long) page);
if (radix_err(old))
return old;
if (!nocopy && old != 0)
{
struct page *oldp = (struct page *) old;
copy_page_to_page(page_to_phys(page), page_to_phys(oldp));
}
inc_page_stat(page, NR_ANON);
return 0;
}
/**
* @brief Get a page from the amap
*
* @param amap Amap to lookup from
* @param pgoff Page offset (in pfn, shifted right by PAGE_SHIFT)
* @return struct page in the amap, or NULL
*/
struct page *amap_get(struct amap *amap, size_t pgoff)
{
scoped_lock g{amap->am_lock};
auto ex = amap->am_map.get(pgoff);
if (ex.has_error())
return nullptr;
struct page *page = (struct page *) ex.value();
page_ref(page);
return page;
}
/**
* @brief Split an amap into two
*
* @param amap Original amap
* @param region Region to which the amap belongs
* @param pgoff Page offset for the new amap
* @return New amap, or NULL
*/
struct amap *amap_split(struct amap *amap, struct vm_area_struct *region, size_t pgoff)
{
if (amap->am_refc > 1) [[unlikely]]
{
/* Note: We do not need a lock here, no one can touch this amap while am_refc > 1 */
struct amap *namap = amap_copy(amap);
if (!namap)
return nullptr;
amap_unref(amap);
amap = namap;
region->vm_amap = amap;
}
struct amap *namap = amap_alloc(0);
if (!namap)
return nullptr;
/* Since we are the exclusive owners of this amap, and callers hold the mm address space lock,
* we do not need to lock. This saves us from GFP_ATOMIC.
*/
auto cursor = radix_tree::cursor::from_range(&amap->am_map, pgoff);
while (!cursor.is_end())
{
/* Move pages from one amap to the other by storing to new and store(0). store(0) is done
* later in case of OOM.
*/
unsigned long curr_idx = cursor.current_idx() - pgoff;
if (namap->am_map.store(curr_idx, cursor.get()) < 0)
goto err;
cursor.advance();
}
cursor = radix_tree::cursor::from_range(&amap->am_map, pgoff);
while (!cursor.is_end())
{
/* store(0) is done now..
*/
cursor.store(0);
cursor.advance();
}
return namap;
err:
/* Open-coded amap_free, to avoid page_ref/page_unref shenanigans. */
amap->~amap();
kfree(amap);
return nullptr;
}
/**
* @brief Truncate an amap
*
* @param amap Amap
* @param region Region to which the amap belongs
* @param new_pgsize New size, in pages
* @return 0 on success, negative error codes
*/
int amap_truncate(struct amap *amap, struct vm_area_struct *region, size_t new_pgsize)
{
if (amap->am_refc > 1) [[unlikely]]
{
/* Note: We do not need a lock here, no one can touch this amap while am_refc > 1 */
struct amap *namap = amap_copy(amap);
if (!namap)
return -ENOMEM;
amap_unref(amap);
amap = namap;
region->vm_amap = amap;
}
auto cursor = radix_tree::cursor::from_range(&amap->am_map, new_pgsize);
while (!cursor.is_end())
{
struct page *page = (struct page *) cursor.get();
dec_page_stat(page, NR_ANON);
page_unref(page);
cursor.store(0);
cursor.advance();
}
return 0;
}
/**
* @brief Punch a hole through an amap
*
* @param amap Amap
* @param region Region to which the amap belongs
* @param first_pg First pfn of the hole
* @param end_pg End of the hole
* @return 0 on success, negative error codes
*/
int amap_punch_hole(struct amap *amap, struct vm_area_struct *region, size_t first_pg,
size_t end_pg)
{
if (amap->am_refc > 1) [[unlikely]]
{
/* Note: We do not need a lock here, no one can touch this amap while am_refc > 1 */
struct amap *namap = amap_copy(amap);
if (!namap)
return -ENOMEM;
amap_unref(amap);
amap = namap;
region->vm_amap = amap;
}
auto cursor = radix_tree::cursor::from_range(&amap->am_map, first_pg, end_pg);
while (!cursor.is_end())
{
struct page *page = (struct page *) cursor.get();
dec_page_stat(page, NR_ANON);
page_unref(page);
cursor.store(0);
cursor.advance();
}
return 0;
}