[FIX] arm probes on do_page_fault
[kernel/swap-modules.git] / us_manager / sspt / sspt_proc.c
1 /*
2  *  Dynamic Binary Instrumentation Module based on KProbes
3  *  modules/driver/sspt/sspt_proc.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 <v.cherkashin@samsung.com>
22  *
23  */
24
25 #include "sspt.h"
26 #include "sspt_proc.h"
27 #include "sspt_page.h"
28 #include "sspt_feature.h"
29 #include "sspt_filter.h"
30 #include "../pf/proc_filters.h"
31 #include <linux/module.h>
32 #include <linux/slab.h>
33 #include <linux/list.h>
34 #include <kprobe/swap_ktd.h>
35 #include <us_manager/us_slot_manager.h>
36
37 static LIST_HEAD(proc_probes_list);
38 static DEFINE_RWLOCK(sspt_proc_rwlock);
39
40
41 struct list_head *sspt_proc_list()
42 {
43         return &proc_probes_list;
44 }
45
46 /**
47  * @brief Global read lock for sspt_proc
48  *
49  * @return Void
50  */
51 void sspt_proc_read_lock(void)
52 {
53         read_lock(&sspt_proc_rwlock);
54 }
55
56 /**
57  * @brief Global read unlock for sspt_proc
58  *
59  * @return Void
60  */
61 void sspt_proc_read_unlock(void)
62 {
63         read_unlock(&sspt_proc_rwlock);
64 }
65
66 /**
67  * @brief Global write lock for sspt_proc
68  *
69  * @return Void
70  */
71 void sspt_proc_write_lock(void)
72 {
73         write_lock(&sspt_proc_rwlock);
74 }
75
76 /**
77  * @brief Global write unlock for sspt_proc
78  *
79  * @return Void
80  */
81 void sspt_proc_write_unlock(void)
82 {
83         write_unlock(&sspt_proc_rwlock);
84 }
85
86 struct ktd_proc {
87         struct sspt_proc *proc;
88         spinlock_t lock;
89 };
90
91 static void ktd_init(struct task_struct *task, void *data)
92 {
93         struct ktd_proc *kproc = (struct ktd_proc *)data;
94
95         kproc->proc = NULL;
96         spin_lock_init(&kproc->lock);
97 }
98
99 static void ktd_exit(struct task_struct *task, void *data)
100 {
101         struct ktd_proc *kproc = (struct ktd_proc *)data;
102
103         WARN_ON(kproc->proc);
104 }
105
106 struct ktask_data ktd = {
107         .init = ktd_init,
108         .exit = ktd_exit,
109         .size = sizeof(struct ktd_proc),
110 };
111
112 static struct ktd_proc *kproc_by_task(struct task_struct *task)
113 {
114         return (struct ktd_proc *)swap_ktd(&ktd, task);
115 }
116
117 int sspt_proc_init(void)
118 {
119         return swap_ktd_reg(&ktd);
120 }
121
122 void sspt_proc_uninit(void)
123 {
124         swap_ktd_unreg(&ktd);
125 }
126
127 void sspt_change_leader(struct task_struct *prev, struct task_struct *next)
128 {
129         struct ktd_proc *prev_kproc;
130
131         prev_kproc = kproc_by_task(prev);
132         spin_lock(&prev_kproc->lock);
133         if (prev_kproc->proc) {
134                 struct ktd_proc *next_kproc;
135
136                 next_kproc = kproc_by_task(next);
137                 get_task_struct(next);
138
139                 /* Change the keeper sspt_proc */
140                 BUG_ON(next_kproc->proc);
141
142                 spin_lock(&next_kproc->lock);
143                 next_kproc->proc = prev_kproc->proc;
144                 prev_kproc->proc = NULL;
145                 spin_unlock(&next_kproc->lock);
146
147                 /* Set new the task leader to sspt_proc */
148                 next_kproc->proc->leader = next;
149
150                 put_task_struct(prev);
151         }
152         spin_unlock(&prev_kproc->lock);
153 }
154
155 static void sspt_reset_proc(struct task_struct *task)
156 {
157         struct ktd_proc *kproc;
158
159         kproc = kproc_by_task(task->group_leader);
160         spin_lock(&kproc->lock);
161         kproc->proc = NULL;
162         spin_unlock(&kproc->lock);
163 }
164
165
166
167
168
169 static struct sspt_proc *sspt_proc_create(struct task_struct *leader)
170 {
171         struct sspt_proc *proc = kzalloc(sizeof(*proc), GFP_KERNEL);
172
173         if (proc) {
174                 proc->feature = sspt_create_feature();
175                 if (proc->feature == NULL) {
176                         kfree(proc);
177                         return NULL;
178                 }
179
180                 INIT_LIST_HEAD(&proc->list);
181                 INIT_LIST_HEAD(&proc->files.head);
182                 init_rwsem(&proc->files.sem);
183                 proc->tgid = leader->tgid;
184                 proc->leader = leader;
185                 /* FIXME: change the task leader */
186                 proc->sm = create_sm_us(leader);
187                 mutex_init(&proc->filters.mtx);
188                 INIT_LIST_HEAD(&proc->filters.head);
189                 atomic_set(&proc->usage, 1);
190
191                 get_task_struct(proc->leader);
192
193                 proc->suspect.after_exec = 1;
194                 proc->suspect.after_fork = 0;
195         }
196
197         return proc;
198 }
199
200 static void sspt_proc_free(struct sspt_proc *proc)
201 {
202         put_task_struct(proc->leader);
203         free_sm_us(proc->sm);
204         sspt_destroy_feature(proc->feature);
205         kfree(proc);
206 }
207
208 /**
209  * @brief Remove sspt_proc struct
210  *
211  * @param proc remove object
212  * @return Void
213  */
214
215 /* called with sspt_proc_write_lock() */
216 void sspt_proc_cleanup(struct sspt_proc *proc)
217 {
218         struct sspt_file *file, *n;
219
220         sspt_proc_del_all_filters(proc);
221
222         down_write(&proc->files.sem);
223         list_for_each_entry_safe(file, n, &proc->files.head, list) {
224                 list_del(&file->list);
225                 sspt_file_free(file);
226         }
227         up_write(&proc->files.sem);
228
229         sspt_destroy_feature(proc->feature);
230
231         free_sm_us(proc->sm);
232         sspt_reset_proc(proc->leader);
233         sspt_proc_put(proc);
234 }
235
236 struct sspt_proc *sspt_proc_get(struct sspt_proc *proc)
237 {
238         atomic_inc(&proc->usage);
239
240         return proc;
241 }
242
243 void sspt_proc_put(struct sspt_proc *proc)
244 {
245         if (atomic_dec_and_test(&proc->usage)) {
246                 if (proc->__mm) {
247                         mmput(proc->__mm);
248                         proc->__mm = NULL;
249                 }
250                 if (proc->__task) {
251                         put_task_struct(proc->__task);
252                         proc->__task = NULL;
253                 }
254
255                 WARN_ON(kproc_by_task(proc->leader)->proc);
256
257                 put_task_struct(proc->leader);
258                 kfree(proc);
259         }
260 }
261 EXPORT_SYMBOL_GPL(sspt_proc_put);
262
263 struct sspt_proc *sspt_proc_by_task(struct task_struct *task)
264 {
265         return kproc_by_task(task->group_leader)->proc;
266 }
267 EXPORT_SYMBOL_GPL(sspt_proc_by_task);
268
269 struct sspt_proc *sspt_proc_get_by_task(struct task_struct *task)
270 {
271         struct ktd_proc *kproc = kproc_by_task(task->group_leader);
272         struct sspt_proc *proc;
273
274         spin_lock(&kproc->lock);
275         proc = kproc->proc;
276         if (proc)
277                 sspt_proc_get(proc);
278         spin_unlock(&kproc->lock);
279
280         return proc;
281 }
282 EXPORT_SYMBOL_GPL(sspt_proc_get_by_task);
283
284 /**
285  * @brief Call func() on each proc (no lock)
286  *
287  * @param func Callback
288  * @param data Data for callback
289  * @return Void
290  */
291 void on_each_proc_no_lock(void (*func)(struct sspt_proc *, void *), void *data)
292 {
293         struct sspt_proc *proc, *tmp;
294
295         list_for_each_entry_safe(proc, tmp, &proc_probes_list, list) {
296                 func(proc, data);
297         }
298 }
299
300 /**
301  * @brief Call func() on each proc
302  *
303  * @param func Callback
304  * @param data Data for callback
305  * @return Void
306  */
307 void on_each_proc(void (*func)(struct sspt_proc *, void *), void *data)
308 {
309         sspt_proc_read_lock();
310         on_each_proc_no_lock(func, data);
311         sspt_proc_read_unlock();
312 }
313 EXPORT_SYMBOL_GPL(on_each_proc);
314
315 /**
316  * @brief Get sspt_proc by task or create sspt_proc
317  *
318  * @param task Pointer on the task_struct struct
319  * @param priv Private data
320  * @return Pointer on the sspt_proc struct
321  */
322 struct sspt_proc *sspt_proc_get_by_task_or_new(struct task_struct *task)
323 {
324         static DEFINE_MUTEX(local_mutex);
325         struct ktd_proc *kproc;
326         struct sspt_proc *proc;
327         struct task_struct *leader = task->group_leader;
328
329         kproc = kproc_by_task(leader);
330         if (kproc->proc)
331                 goto out;
332
333         proc = sspt_proc_create(leader);
334
335         spin_lock(&kproc->lock);
336         if (kproc->proc == NULL) {
337                 sspt_proc_get(proc);
338                 kproc->proc = proc;
339                 proc = NULL;
340
341                 sspt_proc_write_lock();
342                 list_add(&kproc->proc->list, &proc_probes_list);
343                 sspt_proc_write_unlock();
344         }
345         spin_unlock(&kproc->lock);
346
347         if (proc)
348                 sspt_proc_free(proc);
349
350 out:
351         return kproc->proc;
352 }
353
354 /**
355  * @brief Check sspt_proc on empty
356  *
357  * @return Pointer on the sspt_proc struct
358  */
359 void sspt_proc_check_empty(void)
360 {
361         WARN_ON(!list_empty(&proc_probes_list));
362 }
363
364 static void sspt_proc_add_file(struct sspt_proc *proc, struct sspt_file *file)
365 {
366         down_write(&proc->files.sem);
367         list_add(&file->list, &proc->files.head);
368         file->proc = proc;
369         up_write(&proc->files.sem);
370 }
371
372 /**
373  * @brief Get sspt_file from sspt_proc by dentry or new
374  *
375  * @param proc Pointer on the sspt_proc struct
376  * @param dentry Dentry of file
377  * @return Pointer on the sspt_file struct
378  */
379 struct sspt_file *sspt_proc_find_file_or_new(struct sspt_proc *proc,
380                                              struct dentry *dentry)
381 {
382         struct sspt_file *file;
383
384         file = sspt_proc_find_file(proc, dentry);
385         if (file == NULL) {
386                 file = sspt_file_create(dentry, 10);
387                 if (file)
388                         sspt_proc_add_file(proc, file);
389         }
390
391         return file;
392 }
393
394 /**
395  * @brief Get sspt_file from sspt_proc by dentry
396  *
397  * @param proc Pointer on the sspt_proc struct
398  * @param dentry Dentry of file
399  * @return Pointer on the sspt_file struct
400  */
401 struct sspt_file *sspt_proc_find_file(struct sspt_proc *proc,
402                                       struct dentry *dentry)
403 {
404         struct sspt_file *file;
405
406         down_read(&proc->files.sem);
407         list_for_each_entry(file, &proc->files.head, list) {
408                 if (dentry == file->dentry)
409                         goto unlock;
410         }
411         file = NULL;
412
413 unlock:
414         up_read(&proc->files.sem);
415
416         return file;
417 }
418
419 /**
420  * @brief Install probes on the page to monitored process
421  *
422  * @param proc Pointer on the sspt_proc struct
423  * @param page_addr Page address
424  * @return Void
425  */
426 void sspt_proc_install_page(struct sspt_proc *proc, unsigned long page_addr)
427 {
428         struct mm_struct *mm = proc->leader->mm;
429         struct vm_area_struct *vma;
430
431         vma = find_vma_intersection(mm, page_addr, page_addr + 1);
432         if (vma && check_vma(vma)) {
433                 struct dentry *dentry = vma->vm_file->f_path.dentry;
434                 struct sspt_file *file = sspt_proc_find_file(proc, dentry);
435                 if (file) {
436                         struct sspt_page *page;
437
438                         sspt_file_set_mapping(file, vma);
439
440                         page = sspt_find_page_mapped(file, page_addr);
441                         sspt_file_install(file);
442                 }
443         }
444 }
445
446 /**
447  * @brief Install probes to monitored process
448  *
449  * @param proc Pointer on the sspt_proc struct
450  * @return Void
451  */
452 void sspt_proc_install(struct sspt_proc *proc)
453 {
454         struct vm_area_struct *vma;
455         struct mm_struct *mm = proc->leader->mm;
456
457         proc->first_install = 1;
458
459         for (vma = mm->mmap; vma; vma = vma->vm_next) {
460                 if (check_vma(vma)) {
461                         struct dentry *dentry = vma->vm_file->f_path.dentry;
462                         struct sspt_file *file =
463                                 sspt_proc_find_file(proc, dentry);
464                         if (file) {
465                                 sspt_file_set_mapping(file, vma);
466                                 sspt_file_install(file);
467                         }
468                 }
469         }
470 }
471
472 /**
473  * @brief Uninstall probes to monitored process
474  *
475  * @param proc Pointer on the sspt_proc struct
476  * @param task Pointer on the task_struct struct
477  * @param flag Action for probes
478  * @return Error code
479  */
480 int sspt_proc_uninstall(struct sspt_proc *proc,
481                         struct task_struct *task,
482                         enum US_FLAGS flag)
483 {
484         int err = 0;
485         struct sspt_file *file;
486
487         down_read(&proc->files.sem);
488         list_for_each_entry(file, &proc->files.head, list) {
489                 err = sspt_file_uninstall(file, task, flag);
490                 if (err != 0) {
491                         printk(KERN_INFO "ERROR sspt_proc_uninstall: err=%d\n",
492                                err);
493                         break;
494                 }
495         }
496         up_read(&proc->files.sem);
497
498         return err;
499 }
500
501 static int intersection(unsigned long start_a, unsigned long end_a,
502                         unsigned long start_b, unsigned long end_b)
503 {
504         return start_a < start_b ?
505                         end_a > start_b :
506                         start_a < end_b;
507 }
508
509 /**
510  * @brief Get sspt_file list by region (remove sspt_file from sspt_proc list)
511  *
512  * @param proc Pointer on the sspt_proc struct
513  * @param head[out] Pointer on the head list
514  * @param start Region start
515  * @param len Region length
516  * @return Error code
517  */
518 int sspt_proc_get_files_by_region(struct sspt_proc *proc,
519                                   struct list_head *head,
520                                   unsigned long start, size_t len)
521 {
522         int ret = 0;
523         struct sspt_file *file, *n;
524         unsigned long end = start + len;
525
526         down_write(&proc->files.sem);
527         list_for_each_entry_safe(file, n, &proc->files.head, list) {
528                 if (intersection(file->vm_start, file->vm_end, start, end)) {
529                         ret = 1;
530                         list_move(&file->list, head);
531                 }
532         }
533         up_write(&proc->files.sem);
534
535         return ret;
536 }
537
538 /**
539  * @brief Insert sspt_file to sspt_proc list
540  *
541  * @param proc Pointer on the sspt_proc struct
542  * @param head Pointer on the head list
543  * @return Void
544  */
545 void sspt_proc_insert_files(struct sspt_proc *proc, struct list_head *head)
546 {
547         down_write(&proc->files.sem);
548         list_splice(head, &proc->files.head);
549         up_write(&proc->files.sem);
550 }
551
552 /**
553  * @brief Add sspt_filter to sspt_proc list
554  *
555  * @param proc Pointer to sspt_proc struct
556  * @param pfg Pointer to pf_group struct
557  * @return Void
558  */
559 void sspt_proc_add_filter(struct sspt_proc *proc, struct pf_group *pfg)
560 {
561         struct sspt_filter *f;
562
563         f = sspt_filter_create(proc, pfg);
564         if (f)
565                 list_add(&f->list, &proc->filters.head);
566 }
567
568 /**
569  * @brief Remove sspt_filter from sspt_proc list
570  *
571  * @param proc Pointer to sspt_proc struct
572  * @param pfg Pointer to pf_group struct
573  * @return Void
574  */
575 void sspt_proc_del_filter(struct sspt_proc *proc, struct pf_group *pfg)
576 {
577         struct sspt_filter *fl, *tmp;
578
579         mutex_lock(&proc->filters.mtx);
580         list_for_each_entry_safe(fl, tmp, &proc->filters.head, list) {
581                 if (fl->pfg == pfg) {
582                         list_del(&fl->list);
583                         sspt_filter_free(fl);
584                 }
585         }
586         mutex_unlock(&proc->filters.mtx);
587 }
588
589 /**
590  * @brief Remove all sspt_filters from sspt_proc list
591  *
592  * @param proc Pointer to sspt_proc struct
593  * @return Void
594  */
595 void sspt_proc_del_all_filters(struct sspt_proc *proc)
596 {
597         struct sspt_filter *fl, *tmp;
598
599         mutex_lock(&proc->filters.mtx);
600         list_for_each_entry_safe(fl, tmp, &proc->filters.head, list) {
601                 list_del(&fl->list);
602                 sspt_filter_free(fl);
603         }
604         mutex_unlock(&proc->filters.mtx);
605 }
606
607 /**
608  * @brief Check if sspt_filter is already in sspt_proc list
609  *
610  * @param proc Pointer to sspt_proc struct
611  * @param pfg Pointer to pf_group struct
612  * @return Boolean
613  */
614 bool sspt_proc_is_filter_new(struct sspt_proc *proc, struct pf_group *pfg)
615 {
616         struct sspt_filter *fl;
617
618         list_for_each_entry(fl, &proc->filters.head, list)
619                 if (fl->pfg == pfg)
620                         return false;
621
622         return true;
623 }
624
625 void sspt_proc_on_each_filter(struct sspt_proc *proc,
626                               void (*func)(struct sspt_filter *, void *),
627                               void *data)
628 {
629         struct sspt_filter *fl;
630
631         list_for_each_entry(fl, &proc->filters.head, list)
632                 func(fl, data);
633 }
634
635 void sspt_proc_on_each_ip(struct sspt_proc *proc,
636                           void (*func)(struct sspt_ip *, void *), void *data)
637 {
638         struct sspt_file *file;
639
640         down_read(&proc->files.sem);
641         list_for_each_entry(file, &proc->files.head, list)
642                 sspt_file_on_each_ip(file, func, data);
643         up_read(&proc->files.sem);
644 }
645
646 static void is_send_event(struct sspt_filter *f, void *data)
647 {
648         bool *is_send = (bool *)data;
649
650         if (!*is_send && f->pfg_is_inst)
651                 *is_send = !!pfg_msg_cb_get(f->pfg);
652 }
653
654 bool sspt_proc_is_send_event(struct sspt_proc *proc)
655 {
656         bool is_send = false;
657
658         /* FIXME: add read lock (deadlock in sampler) */
659         sspt_proc_on_each_filter(proc, is_send_event, (void *)&is_send);
660
661         return is_send;
662 }
663
664
665 static struct sspt_proc_cb *proc_cb;
666
667 int sspt_proc_cb_set(struct sspt_proc_cb *cb)
668 {
669         if (cb && proc_cb)
670                 return -EBUSY;
671
672         proc_cb = cb;
673
674         return 0;
675 }
676 EXPORT_SYMBOL_GPL(sspt_proc_cb_set);
677
678 void sspt_proc_priv_create(struct sspt_proc *proc)
679 {
680         if (proc_cb && proc_cb->priv_create)
681                 proc->private_data = proc_cb->priv_create(proc);
682 }
683
684 void sspt_proc_priv_destroy(struct sspt_proc *proc)
685 {
686         if (proc->first_install && proc_cb && proc_cb->priv_destroy)
687                 proc_cb->priv_destroy(proc, proc->private_data);
688 }