[REFACTOR] Buffer: move getting next queue element into separate function
[kernel/swap-modules.git] / ks_features / ks_features.c
1 /*
2  *  SWAP kernel features
3  *  modules/ks_features/ks_features.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 ks_features implement
22  *
23  */
24
25
26 #include <linux/module.h>
27 #include <asm/errno.h>
28 #include <ksyms/ksyms.h>
29 #include <kprobe/dbi_kprobes.h>
30 #include <writer/swap_writer_module.h>
31 #include "ks_features.h"
32 #include "syscall_list.h"
33 #include "features_data.c"
34
35 struct ks_probe {
36         struct kretprobe rp;
37         int counter;
38         char *args;
39         int sub_type;
40 };
41
42 #define CREATE_RP(name)                                         \
43 {                                                               \
44         .entry_handler = NULL,                                  \
45         .handler = NULL                                         \
46 }
47
48 #define X(name, args) #name
49 static const char *const syscall_name[] = {
50         SYSCALL_LIST
51 };
52 #undef X
53
54 enum {
55         syscall_name_cnt = sizeof(syscall_name) / sizeof(char *)
56 };
57
58
59 #define X(name, args__)                                         \
60 {                                                               \
61         .rp = CREATE_RP(name),                                  \
62         .counter = 0,                                           \
63         .args = #args__,                                        \
64         .sub_type = PST_NONE                                    \
65 }
66
67 static struct ks_probe ksp[] = {
68         SYSCALL_LIST
69 };
70 #undef X
71
72 static const char *get_sys_name(size_t id)
73 {
74         return syscall_name[id];
75 }
76
77 static int get_counter(size_t id)
78 {
79         return ksp[id].counter;
80 }
81
82 static void inc_counter(size_t id)
83 {
84         ++ksp[id].counter;
85 }
86
87 static void dec_counter(size_t id)
88 {
89         --ksp[id].counter;
90 }
91
92 /* ========================= HANDLERS ========================= */
93 static int entry_handler(struct kretprobe_instance *ri, struct pt_regs *regs)
94 {
95         struct kretprobe *rp = ri->rp;
96
97         if (rp) {
98                 struct ks_probe *ksp = container_of(rp, struct ks_probe, rp);
99                 const char *fmt = ksp->args;
100                 unsigned long addr = (unsigned long)ksp->rp.kp.addr;
101                 int sub_type = ksp->sub_type;
102
103                 entry_event(fmt, addr, regs, PT_KS, sub_type);
104         }
105
106         return 0;
107 }
108
109 static int ret_handler(struct kretprobe_instance *ri, struct pt_regs *regs)
110 {
111         struct kretprobe *rp = ri->rp;
112
113         if (rp) {
114                 unsigned long func_addr = (unsigned long)rp->kp.addr;
115                 unsigned long ret_addr = (unsigned long)ri->ret_addr;
116
117                 exit_event('x', regs, func_addr, ret_addr);
118         }
119
120         return 0;
121 }
122 /* ========================= HANDLERS ========================= */
123
124
125
126
127 /* ====================== SWITCH_CONTEXT ======================= */
128 static int switch_entry_handler(struct kretprobe_instance *ri, struct pt_regs *regs)
129 {
130         switch_entry(regs);
131
132         return 0;
133 }
134
135 static int switch_ret_handler(struct kretprobe_instance *ri, struct pt_regs *regs)
136 {
137         switch_exit(regs);
138
139         return 0;
140 }
141
142 struct kretprobe switch_rp = {
143         .entry_handler = switch_entry_handler,
144         .handler = switch_ret_handler
145 };
146
147 static DEFINE_MUTEX(mutex_sc_enable);
148 static int sc_enable = 0;
149
150 int init_switch_context(void)
151 {
152         unsigned long addr;
153
154         addr = swap_ksyms("__switch_to");
155         if (addr == 0) {
156                 printk("ERROR: not found '__switch_to'\n");
157                 return -EINVAL;
158         }
159
160         switch_rp.kp.addr = (kprobe_opcode_t *)addr;
161
162         return 0;
163 }
164
165 void exit_switch_context(void)
166 {
167         if (sc_enable)
168                 dbi_unregister_kretprobe(&switch_rp);
169 }
170
171 static int register_switch_context(void)
172 {
173         int ret = -EINVAL;
174
175         mutex_lock(&mutex_sc_enable);
176         if (sc_enable) {
177                 printk("switch context profiling is already run!\n");
178                 goto unlock;
179         }
180
181         ret = dbi_register_kretprobe(&switch_rp);
182         if (ret == 0)
183                 sc_enable = 1;
184
185 unlock:
186         mutex_unlock(&mutex_sc_enable);
187
188         return ret;
189 }
190
191 static int unregister_switch_context(void)
192 {
193         int ret = 0;
194
195         mutex_lock(&mutex_sc_enable);
196         if (sc_enable == 0) {
197                 printk("switch context profiling is not running!\n");
198                 ret = -EINVAL;
199                 goto unlock;
200         }
201
202         dbi_unregister_kretprobe(&switch_rp);
203
204         sc_enable = 0;
205 unlock:
206         mutex_unlock(&mutex_sc_enable);
207
208         return ret;
209 }
210 /* ====================== SWITCH_CONTEXT ======================= */
211
212
213
214
215
216 static int register_syscall(size_t id)
217 {
218         int ret;
219         printk("register_syscall: %s\n", get_sys_name(id));
220
221         if (ksp[id].rp.kp.addr == NULL)
222                 return 0;
223
224         ksp[id].rp.entry_handler = entry_handler;
225         ksp[id].rp.handler = ret_handler;
226
227         ret = dbi_register_kretprobe(&ksp[id].rp);
228
229         return ret;
230 }
231
232 static int unregister_syscall(size_t id)
233 {
234         printk("unregister_syscall: %s\n", get_sys_name(id));
235
236         if (ksp[id].rp.kp.addr == NULL)
237                 return 0;
238
239         dbi_unregister_kretprobe(&ksp[id].rp);
240
241         return 0;
242 }
243
244 static void set_pst(struct feature *f, size_t id)
245 {
246         ksp[id].sub_type |= f->sub_type;
247 }
248
249 static void unset_pst(struct feature *f, size_t id)
250 {
251         ksp[id].sub_type &= !f->sub_type;
252 }
253
254 static void do_uninstall_features(struct feature *f, size_t i)
255 {
256         int ret;
257         size_t id;
258         const size_t end = ((size_t) 0) - 1;
259
260         for (; i != end; --i) {
261                 id = f->feature_list[i];
262
263                 if (get_counter(id) == 0) {
264                         printk("syscall %s not installed\n",
265                                get_sys_name(id));
266                         BUG();
267                 }
268
269                 dec_counter(id);
270
271                 if (get_counter(id) == 0) {
272                         ret = unregister_syscall(id);
273                         if (ret)
274                                 printk("syscall %s uninstall error, ret=%d\n",
275                                        get_sys_name(id), ret);
276
277                 }
278
279                 unset_pst(f, id);
280         }
281 }
282
283 static int do_install_features(struct feature *f)
284 {
285         int ret;
286         size_t i, id;
287
288         for (i = 0; i < f->cnt; ++i) {
289                 id = f->feature_list[i];
290                 set_pst(f, id);
291
292                 if (get_counter(id) == 0) {
293                         ret = register_syscall(id);
294                         if (ret) {
295                                 printk("syscall %s install error, ret=%d\n",
296                                        get_sys_name(id), ret);
297
298                                 do_uninstall_features(f, --i);
299                                 return ret;
300                         }
301                 }
302
303                 inc_counter(id);
304         }
305
306         return 0;
307 }
308
309 static DEFINE_MUTEX(mutex_features);
310
311 static int install_features(struct feature *f)
312 {
313         int ret = 0;
314
315         mutex_lock(&mutex_features);
316         if (f->enable) {
317                 printk("energy profiling is already run!\n");
318                 ret = -EINVAL;
319                 goto unlock;
320         }
321
322         ret = do_install_features(f);
323
324         f->enable = 1;
325 unlock:
326         mutex_unlock(&mutex_features);
327         return ret;
328 }
329
330 static int uninstall_features(struct feature *f)
331 {
332         int ret = 0;
333
334         mutex_lock(&mutex_features);
335         if (f->enable == 0) {
336                 printk("feature[%d] is not running!\n", feature_index(f));
337                 ret = -EINVAL;
338                 goto unlock;
339         }
340
341         do_uninstall_features(f, f->cnt - 1);
342
343         f->enable = 0;
344 unlock:
345         mutex_unlock(&mutex_features);
346         return ret;
347 }
348
349 static struct feature *get_feature(enum feature_id id)
350 {
351         if (id < 0 || id >= (int)feature_cnt)
352                 return NULL;
353
354         return &features[id];
355 }
356
357 int set_feature(enum feature_id id)
358 {
359         struct feature *f;
360
361         if (id == FID_SWITCH) {
362                 return register_switch_context();
363         }
364
365         f = get_feature(id);
366         if (f == NULL)
367                 return -EINVAL;
368
369         return install_features(f);
370 }
371 EXPORT_SYMBOL_GPL(set_feature);
372
373 int unset_feature(enum feature_id id)
374 {
375         struct feature *f;
376
377         if (id == FID_SWITCH) {
378                 return unregister_switch_context();
379         }
380
381         f = get_feature(id);
382         if (f == NULL)
383                 return -EINVAL;
384
385         return uninstall_features(f);
386 }
387 EXPORT_SYMBOL_GPL(unset_feature);
388
389 static int init_syscall_features(void)
390 {
391         size_t i;
392         unsigned long addr, ni_syscall;
393         const char *name;
394
395         ni_syscall = swap_ksyms("sys_ni_syscall");
396
397         for (i = 0; i < syscall_name_cnt; ++i) {
398                 name = get_sys_name(i);
399                 addr = swap_ksyms(name);
400                 if (addr == 0) {
401                         printk("%s() not found\n", name);
402                         return -EFAULT;
403                 }
404
405                 if (ni_syscall == addr) {
406                         printk("INFO: %s is not install\n", name);
407                         addr = 0;
408                 }
409
410                 ksp[i].rp.kp.addr = (kprobe_opcode_t *)addr;
411         }
412
413         return 0;
414 }
415
416 static void uninit_syscall_features(void)
417 {
418         size_t id;
419
420         for (id = 0; id < syscall_name_cnt; ++id) {
421                 if (get_counter(id) > 0)
422                         unregister_syscall(id);
423         }
424 }
425
426 static int __init init_ks_feature(void)
427 {
428         int ret;
429
430         ret = init_switch_context();
431         if (ret)
432                 return ret;
433
434         ret = init_syscall_features();
435         if (ret)
436                 exit_switch_context();
437
438         return ret;
439 }
440
441 static void __exit exit_ks_feature(void)
442 {
443         uninit_syscall_features();
444         exit_switch_context();
445 }
446
447 module_init(init_ks_feature);
448 module_exit(exit_ks_feature);
449
450 MODULE_LICENSE("GPL");
451
452 /* debug */
453 static void print_feature(struct feature *f)
454 {
455         size_t i;
456
457         for (i = 0; i < f->cnt; ++i) {
458                 printk("    feature[%3u]: %s\n", i, get_sys_name(f->feature_list[i]));
459         }
460 }
461
462 void print_features(void)
463 {
464         int i;
465
466         printk("print_features:\n");
467         for (i = 0; i < feature_cnt; ++i) {
468                 printk("feature: %d\n", i);
469                 print_feature(&features[i]);
470         }
471 }
472
473 void print_all_syscall(void)
474 {
475         int i;
476
477         printk("SYSCALL:\n");
478         for (i = 0; i < syscall_name_cnt; ++i) {
479                 printk("    [%2d] %s\n", get_counter(i), get_sys_name(i));
480         }
481 }
482 /* debug */