[IMPROVE] add US filter for events
[kernel/swap-modules.git] / us_manager / us_manager.c
1 /*
2  *  SWAP uprobe manager
3  *  modules/us_manager/us_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 #include <linux/module.h>
26 #include <linux/mutex.h>
27 #include "pf/pf_group.h"
28 #include "sspt/sspt_proc.h"
29 #include "helper.h"
30 #include <writer/event_filter.h>
31
32 /* FIXME: move /un/init_msg() elsewhere and remove this include  */
33 #include <writer/swap_writer_module.h>          /* for /un/init_msg() */
34
35
36
37 static DEFINE_MUTEX(mutex_inst);
38 static int flag_inst = 0;
39
40
41 static void do_usm_stop(void)
42 {
43         unregister_helper();
44         uninstall_all();
45         sspt_proc_free_all();
46 }
47
48 static int do_usm_start(void)
49 {
50         int ret;
51
52         ret = register_helper();
53         if (ret)
54                 return ret;
55
56         install_all();
57
58         return 0;
59 }
60
61 int usm_stop(void)
62 {
63         int ret = 0;
64
65         mutex_lock(&mutex_inst);
66         if (flag_inst == 0) {
67                 printk("US instrumentation is not running!\n");
68                 ret = -EINVAL;
69                 goto unlock;
70         }
71
72         do_usm_stop();
73
74         flag_inst = 0;
75 unlock:
76         mutex_unlock(&mutex_inst);
77
78         return ret;
79 }
80 EXPORT_SYMBOL_GPL(usm_stop);
81
82 int usm_start(void)
83 {
84         int ret = -EINVAL;
85
86         mutex_lock(&mutex_inst);
87         if (flag_inst) {
88                 printk("US instrumentation is already run!\n");
89                 goto unlock;
90         }
91
92         ret = do_usm_start();
93         if (ret == 0)
94                 flag_inst = 1;
95
96 unlock:
97         mutex_unlock(&mutex_inst);
98
99         return ret;
100 }
101 EXPORT_SYMBOL_GPL(usm_start);
102
103
104
105
106
107 /* ============================================================================
108  * ===                              US_FILTER                               ===
109  * ============================================================================
110  */
111 static int us_filter(struct task_struct *task)
112 {
113         return !!sspt_proc_get_by_task(task);
114 }
115
116 static struct ev_filter ev_us_filter = {
117         .name = "traced_process_only",
118         .filter = us_filter
119 };
120
121 static int init_us_filter(void)
122 {
123         int ret;
124
125         ret = event_filter_register(&ev_us_filter);
126         if (ret)
127                 return ret;
128
129         return event_filter_set(ev_us_filter.name);
130 }
131
132 static void exit_us_filter(void)
133 {
134         event_filter_unregister(&ev_us_filter);
135 }
136
137
138
139
140
141 static int __init init_us_manager(void)
142 {
143         int ret;
144
145         ret = init_us_filter();
146         if (ret)
147                 return ret;
148
149         init_msg(32*1024);
150
151         ret = init_helper();
152         if (ret)
153                 return ret;
154
155         return 0;
156 }
157
158 static void __exit exit_us_manager(void)
159 {
160         mutex_lock(&mutex_inst);
161         if (flag_inst)
162                 do_usm_stop();
163         mutex_unlock(&mutex_inst);
164
165         uninit_msg();
166         uninit_helper();
167         exit_us_filter();
168 }
169
170 module_init(init_us_manager);
171 module_exit(exit_us_manager);
172
173 MODULE_LICENSE ("GPL");
174