-
Notifications
You must be signed in to change notification settings - Fork 0
/
nice_oom.c
348 lines (305 loc) · 9.38 KB
/
nice_oom.c
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
/*
Copyright (c) 2013, Alexander Sterligov
All rights reserved.
Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice, this
list of conditions and the following disclaimer in the documentation and/or
other materials provided with the distribution.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <sys/param.h>
#include <sys/module.h>
#include <sys/kernel.h>
#include <sys/systm.h>
#include <sys/lock.h>
#include <sys/mutex.h>
#include <sys/proc.h>
#include <sys/sched.h>
#include <sys/syslog.h>
#include <sys/mman.h>
#include <vm/vm.h>
#include <vm/vm_param.h>
#include <vm/vm_object.h>
#include <vm/vm_page.h>
#include <vm/vm_map.h>
#include <vm/vm_pageout.h>
#include <vm/vm_pager.h>
#include <vm/swap_pager.h>
#include <vm/vm_extern.h>
#include <vm/uma.h>
static int event_handler(struct module* module, int event, void *arg);
static moduledata_t
nice_oom_conf =
{
"nice_oom",
event_handler,
NULL
};
DECLARE_MODULE(nice_oom, nice_oom_conf, SI_SUB_KTHREAD_PAGE, SI_ORDER_FIRST);
/*
* Original vm_pageout_oom, will be called if LRU pageout_oom will fail
*/
static void
original_vm_pageout_oom(int shortage)
{
struct proc *p, *bigproc;
vm_offset_t size, bigsize;
struct thread *td;
struct vmspace *vm;
/*
* We keep the process bigproc locked once we find it to keep anyone
* from messing with it; however, there is a possibility of
* deadlock if process B is bigproc and one of it's child processes
* attempts to propagate a signal to B while we are waiting for A's
* lock while walking this list. To avoid this, we don't block on
* the process lock but just skip a process if it is already locked.
*/
bigproc = NULL;
bigsize = 0;
sx_slock(&allproc_lock);
FOREACH_PROC_IN_SYSTEM(p) {
int breakout;
if (PROC_TRYLOCK(p) == 0)
continue;
/*
* If this is a system, protected or killed process, skip it.
*/
if (p->p_state != PRS_NORMAL ||
(p->p_flag & (P_INEXEC | P_PROTECTED | P_SYSTEM)) ||
(p->p_pid == 1) || P_KILLED(p) ||
((p->p_pid < 48) && (swap_pager_avail != 0))) {
PROC_UNLOCK(p);
continue;
}
/*
* If the process is in a non-running type state,
* don't touch it. Check all the threads individually.
*/
breakout = 0;
FOREACH_THREAD_IN_PROC(p, td) {
thread_lock(td);
if (!TD_ON_RUNQ(td) &&
!TD_IS_RUNNING(td) &&
!TD_IS_SLEEPING(td)) {
thread_unlock(td);
breakout = 1;
break;
}
thread_unlock(td);
}
if (breakout) {
PROC_UNLOCK(p);
continue;
}
/*
* get the process size
*/
vm = vmspace_acquire_ref(p);
if (vm == NULL) {
PROC_UNLOCK(p);
continue;
}
if (!vm_map_trylock_read(&vm->vm_map)) {
vmspace_free(vm);
PROC_UNLOCK(p);
continue;
}
size = vmspace_swap_count(vm);
vm_map_unlock_read(&vm->vm_map);
if (shortage == VM_OOM_MEM)
size += vmspace_resident_count(vm);
vmspace_free(vm);
/*
* if the this process is bigger than the biggest one
* remember it.
*/
if (size > bigsize) {
if (bigproc != NULL)
PROC_UNLOCK(bigproc);
bigproc = p;
bigsize = size;
} else
PROC_UNLOCK(p);
}
sx_sunlock(&allproc_lock);
if (bigproc != NULL) {
killproc(bigproc, "out of swap space");
sched_nice(bigproc, PRIO_MIN);
PROC_UNLOCK(bigproc);
wakeup(&cnt.v_free_count);
}
}
/*
* Pathed version of vm_pageout_oom
* It tries to unload LRU pages, if then it's still needed to find more memory, calls original vm_pageout_oom
*/
static void
vm_pageout_oom_unload_lru(int shortage)
{
int addl_page_shortage_init, page_shortage, deactivated_count;
int maxscan;
vm_page_t m, next;
struct vm_page marker;
boolean_t unchanged;
vm_object_t object;
bzero(&marker, sizeof(marker));
marker.flags = PG_MARKER;
marker.oflags = VPO_BUSY;
marker.queue = PQ_ACTIVE;
marker.hold_count = 1;
vm_page_lock_queues();
addl_page_shortage_init = atomic_readandclear_int(&vm_pageout_deficit);
page_shortage = vm_paging_target() + addl_page_shortage_init;
deactivated_count = 0;
maxscan = cnt.v_active_count;
m = TAILQ_FIRST(&vm_page_queues[PQ_ACTIVE].pl);
while ((m != NULL) && (maxscan-- > 0) && (page_shortage > 0)) {
KASSERT(m->queue == PQ_ACTIVE,
("vm_pageout_oom_unload_lru: page %p isn't active", m));
next = TAILQ_NEXT(m, pageq);
/*
* skip marker pages
*/
if ((m->flags & PG_MARKER) != 0) {
m = next;
continue;
}
/*
* Lock the page while holding the page queue lock. Use marker page
* to detect page queue changes and maintain notion of next page on
* page queue
*/
if ( !vm_page_trylock(m) ) {
TAILQ_INSERT_AFTER(&vm_page_queues[PQ_ACTIVE].pl, m, &marker, pageq);
vm_page_unlock_queues();
vm_page_lock(m);
vm_page_lock_queues();
unchanged = (m->queue == PQ_ACTIVE &&
&marker == TAILQ_NEXT(m, pageq));
TAILQ_REMOVE(&vm_page_queues[PQ_ACTIVE].pl, &marker, pageq);
if (!unchanged){
vm_page_unlock(m);
++page_shortage;
m = next;
continue;
}
}
object = m->object;
if (!VM_OBJECT_TRYLOCK(object)){
/*
* To avoid lock order violation, unlock the page queues
* while locking the vm object. Use marker page to detect page queue
* changes and maintain notion of next page on page queue.
*/
TAILQ_INSERT_AFTER(&vm_page_queues[PQ_ACTIVE].pl, m, &marker, pageq);
vm_page_unlock_queues();
vm_page_unlock(m);
VM_OBJECT_LOCK(object);
vm_page_lock(m);
vm_page_lock_queues();
next = TAILQ_NEXT(&marker, pageq);
unchanged = (m->queue == PQ_ACTIVE &&
m->object == object &&
&marker == TAILQ_NEXT(m, pageq));
TAILQ_REMOVE(&vm_page_queues[PQ_ACTIVE].pl, &marker, pageq);
if (!unchanged) {
VM_OBJECT_UNLOCK(object);
vm_page_unlock(m);
++page_shortage;
m = next;
continue;
}
}
if (m->hold_count ||
m->wire_count ||
m->dirty ||
m->busy ||
(m->oflags & VPO_BUSY) ) {
vm_page_unlock(m);
VM_OBJECT_UNLOCK(object);
m = next;
continue;
}
++deactivated_count;
--page_shortage;
m->act_count = 0;
//vm_page_dontneed(m);
vm_page_deactivate(m);
vm_page_unlock(m);
VM_OBJECT_UNLOCK(object);
m = next;
}
vm_page_unlock_queues();
if (page_shortage > 0 &&
deactivated_count < 100) {
log(LOG_ERR, "Deactivated %d pages to avoid OOM, still need %d\n", deactivated_count, page_shortage);
if ((swap_pager_avail < 64 && vm_page_count_min()) ||
(swap_pager_full && vm_paging_target() > 0)) {
original_vm_pageout_oom(shortage);
}
} else {
log(LOG_ERR, "Deactivated %d pages to avoid OOM\n", deactivated_count);
}
}
static int
kernel_is_known(void) {
static const unsigned char expected_vm_pageout_oom_begin[] = { 0x55, 0x89, 0xE5, 0x57 };
static const unsigned char expected_vm_pageout_oom_begin_x64[] = { 0x55, 0x48, 0x89, 0xE5 };
if (memcmp(vm_pageout_oom, expected_vm_pageout_oom_begin, sizeof(expected_vm_pageout_oom_begin)) != 0 &&
memcmp(vm_pageout_oom, expected_vm_pageout_oom_begin_x64, sizeof(expected_vm_pageout_oom_begin_x64)) != 0 ){
uprintf("Beginning of vm_pageout_oom is unexpected! Cannot patch kernel!\n");
return FALSE;
}
return TRUE;
}
static void
patch_kernel( void (*patched_vm_pageout_oom)(int) ) {
static unsigned char jmp_shellcode[] = { 0xe9, 0x00, 0x00, 0x00, 0x00 }; /* jmp <displacement> */
size_t destination_address = (size_t)patched_vm_pageout_oom;
size_t source_address = (size_t)vm_pageout_oom;
size_t instruction_address_after_inserted_jmp = source_address + sizeof(jmp_shellcode);
size_t displacement = destination_address - instruction_address_after_inserted_jmp;
*((int*)&jmp_shellcode[1]) = displacement;
memcpy(vm_pageout_oom, jmp_shellcode, sizeof(jmp_shellcode));
}
static int
event_handler(struct module* module, int event, void *arg) {
static char kernel_backup[100];
static int kernel_is_patched = FALSE;
int result = 0;
switch (event) {
case MOD_LOAD:
if (kernel_is_known()) {
memcpy(kernel_backup, vm_pageout_oom, sizeof(kernel_backup));
patch_kernel(vm_pageout_oom_unload_lru);
kernel_is_patched = TRUE;
}
else {
uprintf("It's not safe to patch this kernel! Nice OOM has not been loaded!\n");
result = EOPNOTSUPP;
}
break;
case MOD_UNLOAD:
if (kernel_is_patched){
memcpy(vm_pageout_oom, kernel_backup, sizeof(kernel_backup));
kernel_is_patched = FALSE;
}
break;
default:
result = EOPNOTSUPP;
break;
}
return result;
}