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