c733811cdfcd4bb99ae0362a58f798886ee5d08b
[kernel/swap-modules.git] / us_manager / helper.c
1 /*
2  *  SWAP uprobe manager
3  *  modules/us_manager/helper.c
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18  *
19  * Copyright (C) Samsung Electronics, 2013
20  *
21  * 2013  Vyacheslav Cherkashin: SWAP us_manager implement
22  *
23  */
24
25
26 #include <kprobe/dbi_kprobes.h>
27 #include <kprobe/dbi_kprobes_deps.h>
28 #include <ksyms/ksyms.h>
29 #include <writer/kernel_operations.h>
30 #include <writer/swap_writer_module.h>
31 #include "us_slot_manager.h"
32 #include "sspt/sspt.h"
33 #include "helper.h"
34
35 struct task_struct;
36
37 struct task_struct *check_task(struct task_struct *task);
38
39 static atomic_t stop_flag = ATOMIC_INIT(0);
40
41
42 /*
43  ******************************************************************************
44  *                               do_page_fault()                              *
45  ******************************************************************************
46  */
47
48 struct pf_data {
49         unsigned long addr;
50 };
51
52 static int entry_handler_mf(struct kretprobe_instance *ri, struct pt_regs *regs)
53 {
54         struct pf_data *data = (struct pf_data *)ri->data;
55
56         data->addr = swap_get_karg(regs, 2);
57
58         return 0;
59 }
60
61 /* Detects when IPs are really loaded into phy mem and installs probes. */
62 static int ret_handler_mf(struct kretprobe_instance *ri, struct pt_regs *regs)
63 {
64         struct task_struct *task = current;
65         unsigned long page_addr;
66
67         if (is_kthread(task))
68                 return 0;
69
70         /* TODO: check return value */
71         page_addr = ((struct pf_data *)ri->data)->addr & PAGE_MASK;
72         call_page_fault(task, page_addr);
73
74         return 0;
75 }
76
77 static struct kretprobe mf_kretprobe = {
78         .entry_handler = entry_handler_mf,
79         .handler = ret_handler_mf,
80         .data_size = sizeof(struct pf_data)
81 };
82
83 static int register_mf(void)
84 {
85         int ret;
86
87         ret = dbi_register_kretprobe(&mf_kretprobe);
88         if (ret)
89                 printk("dbi_register_kretprobe(handle_mm_fault) ret=%d!\n",
90                        ret);
91
92         return ret;
93 }
94
95 static void unregister_mf(void)
96 {
97         dbi_unregister_kretprobe(&mf_kretprobe);
98 }
99
100
101
102
103
104 /*
105  ******************************************************************************
106  *                              copy_process()                                *
107  ******************************************************************************
108  */
109 static atomic_t copy_process_cnt = ATOMIC_INIT(0);
110
111 static void recover_child(struct task_struct *child_task, struct sspt_proc *proc)
112 {
113         sspt_proc_uninstall(proc, child_task, US_DISARM);
114         dbi_disarm_urp_inst_for_task(current, child_task);
115 }
116
117 static void rm_uprobes_child(struct task_struct *task)
118 {
119         struct sspt_proc *proc;
120
121         sspt_proc_write_lock();
122
123         proc = sspt_proc_get_by_task(current);
124         if (proc)
125                 recover_child(task, proc);
126
127         sspt_proc_write_unlock();
128 }
129
130 static int entry_handler_cp(struct kretprobe_instance *ri, struct pt_regs *regs)
131 {
132         atomic_inc(&copy_process_cnt);
133
134         if (atomic_read(&stop_flag))
135                 call_mm_release(current);
136
137         return 0;
138 }
139
140 /* Delete uprobs in children at fork */
141 static int ret_handler_cp(struct kretprobe_instance *ri, struct pt_regs *regs)
142 {
143         struct task_struct *task = (struct task_struct *)regs_return_value(regs);
144
145         if(!task || IS_ERR(task))
146                 goto out;
147
148         if(task->mm != current->mm) {   /* check flags CLONE_VM */
149                 rm_uprobes_child(task);
150         }
151 out:
152         atomic_dec(&copy_process_cnt);
153
154         return 0;
155 }
156
157 static struct kretprobe cp_kretprobe = {
158         .entry_handler = entry_handler_cp,
159         .handler = ret_handler_cp,
160 };
161
162 static int register_cp(void)
163 {
164         int ret;
165
166         ret = dbi_register_kretprobe(&cp_kretprobe);
167         if (ret)
168                 printk("dbi_register_kretprobe(copy_process) ret=%d!\n", ret);
169
170         return ret;
171 }
172
173 static void unregister_cp(void)
174 {
175         dbi_unregister_kretprobe_top(&cp_kretprobe, 0);
176         do {
177                 synchronize_sched();
178         } while (atomic_read(&copy_process_cnt));
179         dbi_unregister_kretprobe_bottom(&cp_kretprobe);
180 }
181
182
183
184
185
186 /*
187  ******************************************************************************
188  *                                mm_release()                                *
189  ******************************************************************************
190  */
191
192 /* Detects when target process removes IPs. */
193 static int mr_pre_handler(struct kprobe *p, struct pt_regs *regs)
194 {
195         struct task_struct *task = (struct task_struct *)swap_get_karg(regs, 0);
196
197         if (is_kthread(task))
198                 goto out;
199
200         if (task->tgid != task->pid) {
201                 goto out;
202         }
203
204         call_mm_release(task);
205 out:
206         return 0;
207 }
208
209 static struct kprobe mr_kprobe = {
210         .pre_handler = mr_pre_handler
211 };
212
213 static int register_mr(void)
214 {
215         int ret;
216
217         ret = dbi_register_kprobe(&mr_kprobe);
218         if (ret)
219                 printk("dbi_register_kprobe(mm_release) ret=%d!\n", ret);
220
221         return ret;
222 }
223
224 static void unregister_mr(void)
225 {
226         dbi_unregister_kprobe(&mr_kprobe);
227 }
228
229
230
231
232
233 /*
234  ******************************************************************************
235  *                                 do_munmap()                                *
236  ******************************************************************************
237  */
238 struct unmap_data {
239         unsigned long start;
240         size_t len;
241 };
242
243 static atomic_t unmap_cnt = ATOMIC_INIT(0);
244
245 static void __remove_unmap_probes(struct sspt_proc *proc,
246                                   struct unmap_data *umd)
247 {
248         struct task_struct *task = proc->task;
249         unsigned long start = umd->start;
250         size_t len = PAGE_ALIGN(umd->len);
251         LIST_HEAD(head);
252
253         if (sspt_proc_get_files_by_region(proc, &head, start, len)) {
254                 struct sspt_file *file, *n;
255                 unsigned long end = start + len;
256
257                 list_for_each_entry_safe(file, n, &head, list) {
258                         if (file->vm_start >= end)
259                                 continue;
260
261                         if (file->vm_start >= start) {
262                                 sspt_file_uninstall(file, task, US_UNINSTALL);
263                         } else {
264                                 /* TODO: uninstall pages: start..file->vm_end */
265                         }
266                 }
267
268                 sspt_proc_insert_files(proc, &head);
269
270                 proc_unmap_msg(start, end);
271         }
272 }
273
274 static void remove_unmap_probes(struct task_struct *task,
275                                 struct unmap_data *umd)
276 {
277         struct sspt_proc *proc;
278
279         sspt_proc_write_lock();
280
281         proc = sspt_proc_get_by_task(task);
282         if (proc)
283                 __remove_unmap_probes(proc, umd);
284
285         sspt_proc_write_unlock();
286 }
287
288 static int entry_handler_unmap(struct kretprobe_instance *ri,
289                                struct pt_regs *regs)
290 {
291         struct unmap_data *data = (struct unmap_data *)ri->data;
292         struct task_struct *task = current->group_leader;
293
294         atomic_inc(&unmap_cnt);
295
296         data->start = swap_get_karg(regs, 1);
297         data->len = (size_t)swap_get_karg(regs, 2);
298
299         if (!is_kthread(task) && atomic_read(&stop_flag))
300                 remove_unmap_probes(task, data);
301
302         return 0;
303 }
304
305 static int ret_handler_unmap(struct kretprobe_instance *ri,
306                              struct pt_regs *regs)
307 {
308         struct task_struct *task;
309
310         task = current->group_leader;
311         if (is_kthread(task) ||
312             get_regs_ret_val(regs))
313                 return 0;
314
315         remove_unmap_probes(task, (struct unmap_data *)ri->data);
316
317         atomic_dec(&unmap_cnt);
318
319         return 0;
320 }
321
322 static struct kretprobe unmap_kretprobe = {
323         .entry_handler = entry_handler_unmap,
324         .handler = ret_handler_unmap,
325         .data_size = sizeof(struct unmap_data)
326 };
327
328 static int register_unmap(void)
329 {
330         int ret;
331
332         ret = dbi_register_kretprobe(&unmap_kretprobe);
333         if (ret)
334                 printk("dbi_register_kprobe(do_munmap) ret=%d!\n", ret);
335
336         return ret;
337 }
338
339 static void unregister_unmap(void)
340 {
341         dbi_unregister_kretprobe_top(&unmap_kretprobe, 0);
342         do {
343                 synchronize_sched();
344         } while (atomic_read(&unmap_cnt));
345         dbi_unregister_kretprobe_bottom(&unmap_kretprobe);
346 }
347
348
349
350
351
352 /*
353  ******************************************************************************
354  *                               do_mmap_pgoff()                              *
355  ******************************************************************************
356  */
357 static int ret_handler_mmap(struct kretprobe_instance *ri,
358                             struct pt_regs *regs)
359 {
360         struct sspt_proc *proc;
361         struct task_struct *task;
362         unsigned long start_addr;
363         struct vm_area_struct *vma;
364
365         task = current->group_leader;
366         if (is_kthread(task))
367                 return 0;
368
369         start_addr = (unsigned long)get_regs_ret_val(regs);
370         if (IS_ERR_VALUE(start_addr))
371                 return 0;
372
373         proc = sspt_proc_get_by_task(task);
374         if (proc == NULL)
375                 return 0;
376
377         vma = find_vma_intersection(task->mm, start_addr, start_addr + 1);
378         if (vma && check_vma(vma))
379                 pcoc_map_msg(vma);
380
381         return 0;
382 }
383
384 static struct kretprobe mmap_kretprobe = {
385         .handler = ret_handler_mmap
386 };
387
388 static int register_mmap(void)
389 {
390         int ret;
391
392         ret = dbi_register_kretprobe(&mmap_kretprobe);
393         if (ret)
394                 printk("dbi_register_kretprobe(do_mmap_pgoff) ret=%d!\n", ret);
395
396         return ret;
397 }
398
399 static void unregister_mmap(void)
400 {
401         dbi_unregister_kretprobe(&mmap_kretprobe);
402 }
403
404
405
406
407
408 int register_helper(void)
409 {
410         int ret = 0;
411
412         atomic_set(&stop_flag, 0);
413
414         /* install probe on 'do_munmap' to detect when for remove US probes */
415         ret = register_unmap();
416         if (ret)
417                 return ret;
418
419         /* install probe on 'mm_release' to detect when for remove US probes */
420         ret = register_mr();
421         if (ret)
422                 goto unreg_unmap;
423
424         /* install probe on 'copy_process' to disarm children process */
425         ret = register_cp();
426         if (ret)
427                 goto unreg_mr;
428
429         /* install probe on 'do_mmap_pgoff' to detect when mapping file */
430         ret = register_mmap();
431         if (ret)
432                 goto unreg_cp;
433
434         /*
435          * install probe on 'handle_mm_fault' to detect when US pages will be
436          * loaded
437          */
438         ret = register_mf();
439         if (ret)
440                 goto unreg_mmap;
441
442         return ret;
443
444 unreg_mmap:
445         unregister_mmap();
446
447 unreg_cp:
448         unregister_cp();
449
450 unreg_mr:
451         unregister_mr();
452
453 unreg_unmap:
454         unregister_unmap();
455
456         return ret;
457 }
458
459 void unregister_helper_top(void)
460 {
461         unregister_mf();
462         atomic_set(&stop_flag, 1);
463 }
464
465 void unregister_helper_bottom(void)
466 {
467         unregister_mmap();
468         unregister_cp();
469         unregister_mr();
470         unregister_unmap();
471 }
472
473 int init_helper(void)
474 {
475         unsigned long addr;
476         addr = swap_ksyms("handle_mm_fault");
477         if (addr == 0) {
478                 printk("Cannot find address for handle_mm_fault function!\n");
479                 return -EINVAL;
480         }
481         mf_kretprobe.kp.addr = (kprobe_opcode_t *)addr;
482
483         addr = swap_ksyms_substr("copy_process");
484         if (addr == 0) {
485                 printk("Cannot find address for copy_process function!\n");
486                 return -EINVAL;
487         }
488         cp_kretprobe.kp.addr = (kprobe_opcode_t *)addr;
489
490         addr = swap_ksyms("mm_release");
491         if (addr == 0) {
492                 printk("Cannot find address for mm_release function!\n");
493                 return -EINVAL;
494         }
495         mr_kprobe.addr = (kprobe_opcode_t *)addr;
496
497         addr = swap_ksyms("do_munmap");
498         if (addr == 0) {
499                 printk("Cannot find address for do_munmap function!\n");
500                 return -EINVAL;
501         }
502         unmap_kretprobe.kp.addr = (kprobe_opcode_t *)addr;
503
504         addr = swap_ksyms("do_mmap_pgoff");
505         if (addr == 0) {
506                 printk("Cannot find address for do_mmap_pgoff function!\n");
507                 return -EINVAL;
508         }
509         mmap_kretprobe.kp.addr = (kprobe_opcode_t *)addr;
510
511         return 0;
512 }
513
514 void uninit_helper(void)
515 {
516 }