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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
|
/* $OpenBSD: subr_hibernate.c,v 1.4 2011/07/08 18:31:16 ariane Exp $ */
/*
* Copyright (c) 2011 Ariane van der Steldt <ariane@stack.nl>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include <sys/hibernate.h>
#include <sys/param.h>
#include <sys/tree.h>
#include <sys/types.h>
#include <sys/systm.h>
#include <uvm/uvm.h>
/*
* Hib alloc enforced alignment.
*/
#define HIB_ALIGN 8 /* bytes alignment */
/*
* sizeof builtin operation, but with alignment constraint.
*/
#define HIB_SIZEOF(_type) roundup(sizeof(_type), HIB_ALIGN)
struct hiballoc_entry
{
size_t hibe_use;
size_t hibe_space;
RB_ENTRY(hiballoc_entry) hibe_entry;
};
/*
* Compare hiballoc entries based on the address they manage.
*
* Since the address is fixed, relative to struct hiballoc_entry,
* we just compare the hiballoc_entry pointers.
*/
static __inline int
hibe_cmp(struct hiballoc_entry *l, struct hiballoc_entry *r)
{
return l < r ? -1 : (l > r);
}
RB_PROTOTYPE(hiballoc_addr, hiballoc_entry, hibe_entry, hibe_cmp)
/*
* Given a hiballoc entry, return the address it manages.
*/
static __inline void*
hib_entry_to_addr(struct hiballoc_entry *entry)
{
caddr_t addr;
addr = (caddr_t)entry;
addr += HIB_SIZEOF(struct hiballoc_entry);
return addr;
}
/*
* Given an address, find the hiballoc that corresponds.
*/
static __inline struct hiballoc_entry*
hib_addr_to_entry(void* addr_param)
{
caddr_t addr;
addr = (caddr_t)addr_param;
addr -= HIB_SIZEOF(struct hiballoc_entry);
return (struct hiballoc_entry*)addr;
}
RB_GENERATE(hiballoc_addr, hiballoc_entry, hibe_entry, hibe_cmp)
/*
* Allocate memory from the arena.
*
* Returns NULL if no memory is available.
*/
void*
hib_alloc(struct hiballoc_arena *arena, size_t alloc_sz)
{
struct hiballoc_entry *entry, *new_entry;
size_t find_sz;
/*
* Enforce alignment of HIB_ALIGN bytes.
*
* Note that, because the entry is put in front of the allocation,
* 0-byte allocations are guaranteed a unique address.
*/
alloc_sz = roundup(alloc_sz, HIB_ALIGN);
/*
* Find an entry with hibe_space >= find_sz.
*
* If the root node is not large enough, we switch to tree traversal.
* Because all entries are made at the bottom of the free space,
* traversal from the end has a slightly better chance of yielding
* a sufficiently large space.
*/
find_sz = alloc_sz + HIB_SIZEOF(struct hiballoc_entry);
entry = RB_ROOT(&arena->hib_addrs);
if (entry != NULL && entry->hibe_space < find_sz) {
RB_FOREACH_REVERSE(entry, hiballoc_addr, &arena->hib_addrs) {
if (entry->hibe_space >= find_sz)
break;
}
}
/*
* Insufficient or too fragmented memory.
*/
if (entry == NULL)
return NULL;
/*
* Create new entry in allocated space.
*/
new_entry = (struct hiballoc_entry*)(
(caddr_t)hib_entry_to_addr(entry) + entry->hibe_use);
new_entry->hibe_space = entry->hibe_space - find_sz;
new_entry->hibe_use = alloc_sz;
/*
* Insert entry.
*/
if (RB_INSERT(hiballoc_addr, &arena->hib_addrs, new_entry) != NULL)
panic("hib_alloc: insert failure");
entry->hibe_space = 0;
/* Return address managed by entry. */
return hib_entry_to_addr(new_entry);
}
/*
* Free a pointer previously allocated from this arena.
*
* If addr is NULL, this will be silently accepted.
*/
void
hib_free(struct hiballoc_arena *arena, void *addr)
{
struct hiballoc_entry *entry, *prev;
if (addr == NULL)
return;
/*
* Derive entry from addr and check it is really in this arena.
*/
entry = hib_addr_to_entry(addr);
if (RB_FIND(hiballoc_addr, &arena->hib_addrs, entry) != entry)
panic("hib_free: freed item %p not in hib arena", addr);
/*
* Give the space in entry to its predecessor.
*
* If entry has no predecessor, change its used space into free space
* instead.
*/
prev = RB_PREV(hiballoc_addr, &arena->hib_addrs, entry);
if (prev != NULL &&
(void*)((caddr_t)prev + HIB_SIZEOF(struct hiballoc_entry) +
prev->hibe_use + prev->hibe_space) == entry) {
/* Merge entry. */
RB_REMOVE(hiballoc_addr, &arena->hib_addrs, entry);
prev->hibe_space += HIB_SIZEOF(struct hiballoc_entry) +
entry->hibe_use + entry->hibe_space;
} else {
/* Flip used memory to free space. */
entry->hibe_space += entry->hibe_use;
entry->hibe_use = 0;
}
}
/*
* Initialize hiballoc.
*
* The allocator will manage memmory at ptr, which is len bytes.
*/
int
hiballoc_init(struct hiballoc_arena *arena, void *p_ptr, size_t p_len)
{
struct hiballoc_entry *entry;
caddr_t ptr;
size_t len;
RB_INIT(&arena->hib_addrs);
/*
* Hib allocator enforces HIB_ALIGN alignment.
* Fixup ptr and len.
*/
ptr = (caddr_t)roundup((vaddr_t)p_ptr, HIB_ALIGN);
len = p_len - ((size_t)ptr - (size_t)p_ptr);
len &= ~((size_t)HIB_ALIGN - 1);
/*
* Insufficient memory to be able to allocate and also do bookkeeping.
*/
if (len <= HIB_SIZEOF(struct hiballoc_entry))
return ENOMEM;
/*
* Create entry describing space.
*/
entry = (struct hiballoc_entry*)ptr;
entry->hibe_use = 0;
entry->hibe_space = len - HIB_SIZEOF(struct hiballoc_entry);
RB_INSERT(hiballoc_addr, &arena->hib_addrs, entry);
return 0;
}
/*
* Zero all free memory.
*/
void
uvm_pmr_zero_everything(void)
{
struct uvm_pmemrange *pmr;
struct vm_page *pg;
int i;
uvm_lock_fpageq();
TAILQ_FOREACH(pmr, &uvm.pmr_control.use, pmr_use) {
/* Zero single pages. */
while ((pg = TAILQ_FIRST(&pmr->single[UVM_PMR_MEMTYPE_DIRTY]))
!= NULL) {
uvm_pmr_remove(pmr, pg);
uvm_pagezero(pg);
atomic_setbits_int(&pg->pg_flags, PG_ZERO);
uvmexp.zeropages++;
uvm_pmr_insert(pmr, pg, 0);
}
/* Zero multi page ranges. */
while ((pg = RB_ROOT(&pmr->size[UVM_PMR_MEMTYPE_DIRTY]))
!= NULL) {
pg--; /* Size tree always has second page. */
uvm_pmr_remove(pmr, pg);
for (i = 0; i < pg->fpgsz; i++) {
uvm_pagezero(&pg[i]);
atomic_setbits_int(&pg[i].pg_flags, PG_ZERO);
uvmexp.zeropages++;
}
uvm_pmr_insert(pmr, pg, 0);
}
}
uvm_unlock_fpageq();
}
/*
* Allocate the highest address that can hold sz.
*
* sz in bytes.
*/
int
uvm_pmr_alloc_pig(paddr_t *addr, psize_t sz)
{
struct uvm_pmemrange *pmr;
struct vm_page *pig_pg, *pg;
/*
* Convert sz to pages, since that is what pmemrange uses internally.
*/
sz = atop(round_page(sz));
uvm_lock_fpageq();
TAILQ_FOREACH(pmr, &uvm.pmr_control.use, pmr_use) {
RB_FOREACH_REVERSE(pig_pg, uvm_pmr_addr, &pmr->addr) {
if (pig_pg->fpgsz >= sz) {
goto found;
}
}
}
/*
* Allocation failure.
*/
uvm_unlock_pageq();
return ENOMEM;
found:
/* Remove page from freelist. */
uvm_pmr_remove_size(pmr, pig_pg);
pig_pg->fpgsz -= sz;
pg = pig_pg + pig_pg->fpgsz;
if (pig_pg->fpgsz == 0)
uvm_pmr_remove_addr(pmr, pig_pg);
else
uvm_pmr_insert_size(pmr, pig_pg);
uvmexp.free -= sz;
*addr = VM_PAGE_TO_PHYS(pg);
/*
* Update pg flags.
*
* Note that we trash the sz argument now.
*/
while (sz > 0) {
KASSERT(pg->pg_flags & PQ_FREE);
atomic_clearbits_int(&pg->pg_flags,
PG_PMAP0|PG_PMAP1|PG_PMAP2|PG_PMAP3);
if (pg->pg_flags & PG_ZERO)
uvmexp.zeropages -= sz;
atomic_clearbits_int(&pg->pg_flags,
PG_ZERO|PQ_FREE);
pg->uobject = NULL;
pg->uanon = NULL;
pg->pg_version++;
/*
* Next.
*/
pg++;
sz--;
}
/* Return. */
uvm_unlock_fpageq();
return 0;
}
/*
* Allocate a piglet area.
*
* This is as low as possible.
* Piglets are aligned.
*
* sz and align in bytes.
*
* The call will sleep for the pagedaemon to attempt to free memory.
* The pagedaemon may decide its not possible to free enough memory, causing
* the allocation to fail.
*/
int
uvm_pmr_alloc_piglet(paddr_t *addr, psize_t sz, paddr_t align)
{
vaddr_t pg_addr, piglet_addr;
struct uvm_pmemrange *pmr;
struct vm_page *pig_pg, *pg;
struct pglist pageq;
int pdaemon_woken;
KASSERT((align & (align - 1)) == 0);
pdaemon_woken = 0; /* Didn't wake the pagedaemon. */
/*
* Fixup arguments: align must be at least PAGE_SIZE,
* sz will be converted to pagecount, since that is what
* pmemrange uses internally.
*/
if (align < PAGE_SIZE)
align = PAGE_SIZE;
sz = atop(round_page(sz));
uvm_lock_fpageq();
TAILQ_FOREACH_REVERSE(pmr, &uvm.pmr_control.use, uvm_pmemrange_use,
pmr_use) {
retry:
/*
* Search for a range with enough space.
* Use the address tree, to ensure the range is as low as
* possible.
*/
RB_FOREACH(pig_pg, uvm_pmr_addr, &pmr->addr) {
pg_addr = VM_PAGE_TO_PHYS(pig_pg);
piglet_addr = (pg_addr + (align - 1)) & ~(align - 1);
if (pig_pg->fpgsz >= sz) {
goto found;
}
if (atop(pg_addr) + pig_pg->fpgsz >
atop(piglet_addr) + sz) {
goto found;
}
}
/*
* Try to coerse the pagedaemon into freeing memory
* for the piglet.
*
* pdaemon_woken is set to prevent the code from
* falling into an endless loop.
*/
if (!pdaemon_woken) {
pdaemon_woken = 1;
if (uvm_wait_pla(ptoa(pmr->low), ptoa(pmr->high) - 1,
ptoa(sz), UVM_PLA_FAILOK) == 0)
goto retry;
}
}
/* Return failure. */
uvm_unlock_fpageq();
return ENOMEM;
found:
/*
* Extract piglet from pigpen.
*/
TAILQ_INIT(&pageq);
uvm_pmr_extract_range(pmr, pig_pg,
atop(piglet_addr), atop(piglet_addr) + sz, &pageq);
*addr = piglet_addr;
uvmexp.free -= sz;
/*
* Update pg flags.
*
* Note that we trash the sz argument now.
*/
TAILQ_FOREACH(pg, &pageq, pageq) {
KASSERT(pg->pg_flags & PQ_FREE);
atomic_clearbits_int(&pg->pg_flags,
PG_PMAP0|PG_PMAP1|PG_PMAP2|PG_PMAP3);
if (pg->pg_flags & PG_ZERO)
uvmexp.zeropages--;
atomic_clearbits_int(&pg->pg_flags,
PG_ZERO|PQ_FREE);
pg->uobject = NULL;
pg->uanon = NULL;
pg->pg_version++;
}
uvm_unlock_fpageq();
return 0;
}
|