[FIX] race condition when use sspt_proc
[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 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                 proc->tgid = leader->tgid;
182                 proc->leader = leader;
183                 /* FIXME: change the task leader */
184                 proc->sm = create_sm_us(leader);
185                 INIT_LIST_HEAD(&proc->file_head);
186                 mutex_init(&proc->filters.mtx);
187                 INIT_LIST_HEAD(&proc->filters.head);
188                 atomic_set(&proc->usage, 1);
189
190                 get_task_struct(proc->leader);
191
192                 proc->suspect.after_exec = 1;
193                 proc->suspect.after_fork = 0;
194         }
195
196         return proc;
197 }
198
199 /**
200  * @brief Remove sspt_proc struct
201  *
202  * @param proc remove object
203  * @return Void
204  */
205
206 /* called with sspt_proc_write_lock() */
207 void sspt_proc_cleanup(struct sspt_proc *proc)
208 {
209         struct sspt_file *file, *n;
210
211         sspt_proc_del_all_filters(proc);
212
213         list_for_each_entry_safe(file, n, &proc->file_head, list) {
214                 list_del(&file->list);
215                 sspt_file_free(file);
216         }
217
218         sspt_destroy_feature(proc->feature);
219
220         free_sm_us(proc->sm);
221         sspt_proc_put(proc);
222 }
223
224 struct sspt_proc *sspt_proc_get(struct sspt_proc *proc)
225 {
226         atomic_inc(&proc->usage);
227
228         return proc;
229 }
230
231 void sspt_proc_put(struct sspt_proc *proc)
232 {
233         if (atomic_dec_and_test(&proc->usage)) {
234                 if (proc->__mm) {
235                         mmput(proc->__mm);
236                         proc->__mm = NULL;
237                 }
238                 if (proc->__task) {
239                         put_task_struct(proc->__task);
240                         proc->__task = NULL;
241                 }
242
243                 put_task_struct(proc->leader);
244                 kfree(proc);
245         }
246 }
247 EXPORT_SYMBOL_GPL(sspt_proc_put);
248
249 struct sspt_proc *sspt_proc_by_task(struct task_struct *task)
250 {
251         return kproc_by_task(task->group_leader)->proc;
252 }
253 EXPORT_SYMBOL_GPL(sspt_proc_by_task);
254
255 struct sspt_proc *sspt_proc_get_by_task(struct task_struct *task)
256 {
257         struct ktd_proc *kproc = kproc_by_task(task->group_leader);
258         struct sspt_proc *proc;
259
260         spin_lock(&kproc->lock);
261         proc = kproc->proc;
262         if (proc)
263                 sspt_proc_get(proc);
264         spin_unlock(&kproc->lock);
265
266         return proc;
267 }
268 EXPORT_SYMBOL_GPL(sspt_proc_get_by_task);
269
270 /**
271  * @brief Call func() on each proc (no lock)
272  *
273  * @param func Callback
274  * @param data Data for callback
275  * @return Void
276  */
277 void on_each_proc_no_lock(void (*func)(struct sspt_proc *, void *), void *data)
278 {
279         struct sspt_proc *proc, *tmp;
280
281         list_for_each_entry_safe(proc, tmp, &proc_probes_list, list) {
282                 func(proc, data);
283         }
284 }
285
286 /**
287  * @brief Call func() on each proc
288  *
289  * @param func Callback
290  * @param data Data for callback
291  * @return Void
292  */
293 void on_each_proc(void (*func)(struct sspt_proc *, void *), void *data)
294 {
295         sspt_proc_read_lock();
296         on_each_proc_no_lock(func, data);
297         sspt_proc_read_unlock();
298 }
299 EXPORT_SYMBOL_GPL(on_each_proc);
300
301 /**
302  * @brief Get sspt_proc by task or create sspt_proc
303  *
304  * @param task Pointer on the task_struct struct
305  * @param priv Private data
306  * @return Pointer on the sspt_proc struct
307  */
308 struct sspt_proc *sspt_proc_get_by_task_or_new(struct task_struct *task)
309 {
310         static DEFINE_MUTEX(local_mutex);
311         struct ktd_proc *kproc;
312         struct sspt_proc *proc;
313         struct task_struct *leader = task->group_leader;
314
315         kproc = kproc_by_task(leader);
316         if (kproc->proc)
317                 goto out;
318
319         proc = sspt_proc_create(leader);
320
321         spin_lock(&kproc->lock);
322         if (kproc->proc == NULL) {
323                 sspt_proc_get(proc);
324                 kproc->proc = proc;
325                 proc = NULL;
326
327                 sspt_proc_write_lock();
328                 list_add(&kproc->proc->list, &proc_probes_list);
329                 sspt_proc_write_unlock();
330         }
331         spin_unlock(&kproc->lock);
332
333         if (proc)
334                 sspt_proc_cleanup(proc);
335
336 out:
337         return kproc->proc;
338 }
339
340 /**
341  * @brief Check sspt_proc on empty
342  *
343  * @return Pointer on the sspt_proc struct
344  */
345 void sspt_proc_check_empty(void)
346 {
347         WARN_ON(!list_empty(&proc_probes_list));
348 }
349
350 static void sspt_proc_add_file(struct sspt_proc *proc, struct sspt_file *file)
351 {
352         list_add(&file->list, &proc->file_head);
353         file->proc = proc;
354 }
355
356 /**
357  * @brief Get sspt_file from sspt_proc by dentry or new
358  *
359  * @param proc Pointer on the sspt_proc struct
360  * @param dentry Dentry of file
361  * @return Pointer on the sspt_file struct
362  */
363 struct sspt_file *sspt_proc_find_file_or_new(struct sspt_proc *proc,
364                                              struct dentry *dentry)
365 {
366         struct sspt_file *file;
367
368         file = sspt_proc_find_file(proc, dentry);
369         if (file == NULL) {
370                 file = sspt_file_create(dentry, 10);
371                 if (file)
372                         sspt_proc_add_file(proc, file);
373         }
374
375         return file;
376 }
377
378 /**
379  * @brief Get sspt_file from sspt_proc by dentry
380  *
381  * @param proc Pointer on the sspt_proc struct
382  * @param dentry Dentry of file
383  * @return Pointer on the sspt_file struct
384  */
385 struct sspt_file *sspt_proc_find_file(struct sspt_proc *proc,
386                                       struct dentry *dentry)
387 {
388         struct sspt_file *file;
389
390         list_for_each_entry(file, &proc->file_head, list) {
391                 if (dentry == file->dentry)
392                         return file;
393         }
394
395         return NULL;
396 }
397
398 /**
399  * @brief Install probes on the page to monitored process
400  *
401  * @param proc Pointer on the sspt_proc struct
402  * @param page_addr Page address
403  * @return Void
404  */
405 void sspt_proc_install_page(struct sspt_proc *proc, unsigned long page_addr)
406 {
407         struct mm_struct *mm = proc->leader->mm;
408         struct vm_area_struct *vma;
409
410         vma = find_vma_intersection(mm, page_addr, page_addr + 1);
411         if (vma && check_vma(vma)) {
412                 struct dentry *dentry = vma->vm_file->f_dentry;
413                 struct sspt_file *file = sspt_proc_find_file(proc, dentry);
414                 if (file) {
415                         struct sspt_page *page;
416
417                         sspt_file_set_mapping(file, vma);
418
419                         page = sspt_find_page_mapped(file, page_addr);
420                         if (page)
421                                 sspt_register_page(page, file);
422                 }
423         }
424 }
425
426 /**
427  * @brief Install probes to monitored process
428  *
429  * @param proc Pointer on the sspt_proc struct
430  * @return Void
431  */
432 void sspt_proc_install(struct sspt_proc *proc)
433 {
434         struct vm_area_struct *vma;
435         struct mm_struct *mm = proc->leader->mm;
436
437         proc->first_install = 1;
438
439         for (vma = mm->mmap; vma; vma = vma->vm_next) {
440                 if (check_vma(vma)) {
441                         struct dentry *dentry = vma->vm_file->f_dentry;
442                         struct sspt_file *file =
443                                 sspt_proc_find_file(proc, dentry);
444                         if (file) {
445                                 sspt_file_set_mapping(file, vma);
446                                 sspt_file_install(file);
447                         }
448                 }
449         }
450 }
451
452 /**
453  * @brief Uninstall probes to monitored process
454  *
455  * @param proc Pointer on the sspt_proc struct
456  * @param task Pointer on the task_struct struct
457  * @param flag Action for probes
458  * @return Error code
459  */
460 int sspt_proc_uninstall(struct sspt_proc *proc,
461                         struct task_struct *task,
462                         enum US_FLAGS flag)
463 {
464         int err = 0;
465         struct sspt_file *file;
466
467         list_for_each_entry_rcu(file, &proc->file_head, list) {
468                 err = sspt_file_uninstall(file, task, flag);
469                 if (err != 0) {
470                         printk(KERN_INFO "ERROR sspt_proc_uninstall: err=%d\n",
471                                err);
472                         return err;
473                 }
474         }
475
476         return err;
477 }
478
479 static int intersection(unsigned long start_a, unsigned long end_a,
480                         unsigned long start_b, unsigned long end_b)
481 {
482         return start_a < start_b ?
483                         end_a > start_b :
484                         start_a < end_b;
485 }
486
487 /**
488  * @brief Get sspt_file list by region (remove sspt_file from sspt_proc list)
489  *
490  * @param proc Pointer on the sspt_proc struct
491  * @param head[out] Pointer on the head list
492  * @param start Region start
493  * @param len Region length
494  * @return Error code
495  */
496 int sspt_proc_get_files_by_region(struct sspt_proc *proc,
497                                   struct list_head *head,
498                                   unsigned long start, size_t len)
499 {
500         int ret = 0;
501         struct sspt_file *file, *n;
502         unsigned long end = start + len;
503
504         list_for_each_entry_safe(file, n, &proc->file_head, list) {
505                 if (intersection(file->vm_start, file->vm_end, start, end)) {
506                         ret = 1;
507                         list_move(&file->list, head);
508                 }
509         }
510
511         return ret;
512 }
513
514 /**
515  * @brief Insert sspt_file to sspt_proc list
516  *
517  * @param proc Pointer on the sspt_proc struct
518  * @param head Pointer on the head list
519  * @return Void
520  */
521 void sspt_proc_insert_files(struct sspt_proc *proc, struct list_head *head)
522 {
523         list_splice(head, &proc->file_head);
524 }
525
526 /**
527  * @brief Add sspt_filter to sspt_proc list
528  *
529  * @param proc Pointer to sspt_proc struct
530  * @param pfg Pointer to pf_group struct
531  * @return Void
532  */
533 void sspt_proc_add_filter(struct sspt_proc *proc, struct pf_group *pfg)
534 {
535         struct sspt_filter *f;
536
537         f = sspt_filter_create(proc, pfg);
538         if (f)
539                 list_add(&f->list, &proc->filters.head);
540 }
541
542 /**
543  * @brief Remove sspt_filter from sspt_proc list
544  *
545  * @param proc Pointer to sspt_proc struct
546  * @param pfg Pointer to pf_group struct
547  * @return Void
548  */
549 void sspt_proc_del_filter(struct sspt_proc *proc, struct pf_group *pfg)
550 {
551         struct sspt_filter *fl, *tmp;
552
553         mutex_lock(&proc->filters.mtx);
554         list_for_each_entry_safe(fl, tmp, &proc->filters.head, list) {
555                 if (fl->pfg == pfg) {
556                         list_del(&fl->list);
557                         sspt_filter_free(fl);
558                 }
559         }
560         mutex_unlock(&proc->filters.mtx);
561 }
562
563 /**
564  * @brief Remove all sspt_filters from sspt_proc list
565  *
566  * @param proc Pointer to sspt_proc struct
567  * @return Void
568  */
569 void sspt_proc_del_all_filters(struct sspt_proc *proc)
570 {
571         struct sspt_filter *fl, *tmp;
572
573         mutex_lock(&proc->filters.mtx);
574         list_for_each_entry_safe(fl, tmp, &proc->filters.head, list) {
575                 list_del(&fl->list);
576                 sspt_filter_free(fl);
577         }
578         mutex_unlock(&proc->filters.mtx);
579 }
580
581 /**
582  * @brief Check if sspt_filter is already in sspt_proc list
583  *
584  * @param proc Pointer to sspt_proc struct
585  * @param pfg Pointer to pf_group struct
586  * @return Boolean
587  */
588 bool sspt_proc_is_filter_new(struct sspt_proc *proc, struct pf_group *pfg)
589 {
590         struct sspt_filter *fl;
591
592         list_for_each_entry(fl, &proc->filters.head, list)
593                 if (fl->pfg == pfg)
594                         return false;
595
596         return true;
597 }
598
599 void sspt_proc_on_each_filter(struct sspt_proc *proc,
600                               void (*func)(struct sspt_filter *, void *),
601                               void *data)
602 {
603         struct sspt_filter *fl;
604
605         list_for_each_entry(fl, &proc->filters.head, list)
606                 func(fl, data);
607 }
608
609 void sspt_proc_on_each_ip(struct sspt_proc *proc,
610                           void (*func)(struct sspt_ip *, void *), void *data)
611 {
612         struct sspt_file *file;
613
614         list_for_each_entry(file, &proc->file_head, list)
615                 sspt_file_on_each_ip(file, func, data);
616 }
617
618 static void is_send_event(struct sspt_filter *f, void *data)
619 {
620         bool *is_send = (bool *)data;
621
622         if (!*is_send && f->pfg_is_inst)
623                 *is_send = !!pfg_msg_cb_get(f->pfg);
624 }
625
626 bool sspt_proc_is_send_event(struct sspt_proc *proc)
627 {
628         bool is_send = false;
629
630         /* FIXME: add read lock (deadlock in sampler) */
631         sspt_proc_on_each_filter(proc, is_send_event, (void *)&is_send);
632
633         return is_send;
634 }
635
636
637 static struct sspt_proc_cb *proc_cb;
638
639 int sspt_proc_cb_set(struct sspt_proc_cb *cb)
640 {
641         if (cb && proc_cb)
642                 return -EBUSY;
643
644         proc_cb = cb;
645
646         return 0;
647 }
648 EXPORT_SYMBOL_GPL(sspt_proc_cb_set);
649
650 void sspt_proc_priv_create(struct sspt_proc *proc)
651 {
652         if (proc_cb && proc_cb->priv_create)
653                 proc->private_data = proc_cb->priv_create(proc);
654 }
655
656 void sspt_proc_priv_destroy(struct sspt_proc *proc)
657 {
658         if (proc->first_install && proc_cb && proc_cb->priv_destroy)
659                 proc_cb->priv_destroy(proc, proc->private_data);
660 }