[FIX] race condition on probes disarming (at stop)
[kernel/swap-modules.git] / us_manager / us_slot_manager.c
1 /*
2  *  SWAP uprobe manager
3  *  modules/us_manager/us_slot_manager.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 <linux/slab.h>
27 #include <linux/hardirq.h>
28 #include <linux/sched.h>
29 #include <linux/mm.h>
30 #include <linux/mman.h>
31 #include <linux/list.h>
32 #include <kprobe/dbi_insn_slots.h>
33 #include <kprobe/arch/asm/dbi_kprobes.h>
34 #include "us_manager_common.h"
35
36
37 static void *sm_alloc_us(struct slot_manager *sm)
38 {
39         unsigned long addr;
40
41         addr = swap_do_mmap(NULL, 0, PAGE_SIZE, PROT_EXEC|PROT_READ|PROT_WRITE,
42                             MAP_ANONYMOUS|MAP_PRIVATE, 0);
43         return (void *)addr;
44 }
45
46 static void sm_free_us(struct slot_manager *sm, void *ptr)
47 {
48         /*
49          * E. G.: This code provides kernel dump because of rescheduling while atomic.
50          * As workaround, this code was commented. In this case we will have memory leaks
51          * for instrumented process, but instrumentation process should functionate correctly.
52          * Planned that good solution for this problem will be done during redesigning KProbe
53          * for improving supportability and performance.
54          */
55 #if 0
56         struct task_struct *task = sm->data;
57
58         mm = get_task_mm(task);
59         if (mm) {
60                 down_write(&mm->mmap_sem);
61                 do_munmap(mm, (unsigned long)(ptr), PAGE_SIZE);
62                 up_write(&mm->mmap_sem);
63                 mmput(mm);
64         }
65 #endif
66         /* FIXME: implement the removal of memory for task */
67 }
68
69 struct slot_manager *create_sm_us(struct task_struct *task)
70 {
71         struct slot_manager *sm = kmalloc(sizeof(*sm), GFP_ATOMIC);
72         sm->slot_size = UPROBES_TRAMP_LEN;
73         sm->alloc = sm_alloc_us;
74         sm->free = sm_free_us;
75         INIT_HLIST_HEAD(&sm->page_list);
76         sm->data = task;
77
78         return sm;
79 }
80
81 void free_sm_us(struct slot_manager *sm)
82 {
83         /* FIXME: free */
84 }