3 * modules/us_manager/pf/pf_group.c
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.
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.
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.
19 * Copyright (C) Samsung Electronics, 2013
21 * 2013 Vyacheslav Cherkashin: SWAP us_manager implement
26 #include <linux/module.h>
27 #include <linux/slab.h>
28 #include <linux/list.h>
29 #include <linux/namei.h>
30 #include <linux/mman.h>
31 #include <linux/spinlock.h>
33 #include "proc_filters.h"
34 #include "../sspt/sspt_filter.h"
35 #include "../us_manager_common.h"
36 #include <us_manager/img/img_proc.h>
37 #include <us_manager/img/img_file.h>
38 #include <us_manager/img/img_ip.h>
39 #include <us_manager/sspt/sspt_proc.h>
40 #include <us_manager/helper.h>
41 #include <task_ctx/task_ctx.h>
45 struct list_head list;
46 struct img_proc *i_proc;
47 struct proc_filter filter;
48 struct pfg_msg_cb *msg_cb;
51 spinlock_t pl_lock; /* for proc_list */
52 struct list_head proc_list;
56 struct list_head list;
57 struct sspt_proc *proc;
61 static LIST_HEAD(pfg_list);
62 static DECLARE_RWSEM(pfg_list_sem);
64 static void pfg_list_rlock(void)
66 down_read(&pfg_list_sem);
69 static void pfg_list_runlock(void)
71 up_read(&pfg_list_sem);
74 static void pfg_list_wlock(void)
76 down_write(&pfg_list_sem);
79 static void pfg_list_wunlock(void)
81 up_write(&pfg_list_sem);
85 /* struct pl_struct */
86 static struct pl_struct *create_pl_struct(struct sspt_proc *proc)
88 struct pl_struct *pls = kmalloc(sizeof(*pls), GFP_ATOMIC);
91 INIT_LIST_HEAD(&pls->list);
92 pls->proc = sspt_proc_get(proc);
98 static void free_pl_struct(struct pl_struct *pls)
100 sspt_proc_put(pls->proc);
103 /* struct pl_struct */
105 static struct pf_group *pfg_create(void)
107 struct pf_group *pfg = kmalloc(sizeof(*pfg), GFP_ATOMIC);
112 pfg->i_proc = img_proc_create();
113 if (pfg->i_proc == NULL)
114 goto create_pfg_fail;
116 INIT_LIST_HEAD(&pfg->list);
117 memset(&pfg->filter, 0, sizeof(pfg->filter));
118 spin_lock_init(&pfg->pl_lock);
119 INIT_LIST_HEAD(&pfg->proc_list);
121 atomic_set(&pfg->usage, 1);
132 static void pfg_free(struct pf_group *pfg)
134 struct pl_struct *pl, *n;
136 img_proc_free(pfg->i_proc);
137 free_pf(&pfg->filter);
138 list_for_each_entry_safe(pl, n, &pfg->proc_list, list) {
139 sspt_proc_del_filter(pl->proc, pfg);
146 static int pfg_add_proc(struct pf_group *pfg, struct sspt_proc *proc)
148 struct pl_struct *pls;
150 pls = create_pl_struct(proc);
154 spin_lock(&pfg->pl_lock);
155 list_add(&pls->list, &pfg->proc_list);
156 spin_unlock(&pfg->pl_lock);
161 static int pfg_del_proc(struct pf_group *pfg, struct sspt_proc *proc)
163 struct pl_struct *pls, *pls_free = NULL;
165 spin_lock(&pfg->pl_lock);
166 list_for_each_entry(pls, &pfg->proc_list, list) {
167 if (pls->proc == proc) {
168 list_del(&pls->list);
173 spin_unlock(&pfg->pl_lock);
176 free_pl_struct(pls_free);
182 /* called with pfg_list_lock held */
183 static void pfg_add_to_list(struct pf_group *pfg)
185 list_add(&pfg->list, &pfg_list);
188 /* called with pfg_list_lock held */
189 static void pfg_del_from_list(struct pf_group *pfg)
191 list_del(&pfg->list);
195 static void msg_info(struct sspt_filter *f, void *data)
197 if (f->pfg_is_inst == false) {
198 struct pfg_msg_cb *cb;
200 f->pfg_is_inst = true;
202 cb = pfg_msg_cb_get(f->pfg);
204 struct dentry *dentry;
206 dentry = (struct dentry *)f->pfg->filter.priv;
209 cb->msg_info(f->proc->leader, dentry);
211 if (cb->msg_status_info)
212 cb->msg_status_info(f->proc->leader);
217 static void first_install(struct task_struct *task, struct sspt_proc *proc)
219 sspt_proc_priv_create(proc);
221 down_write(&task->mm->mmap_sem);
222 sspt_proc_on_each_filter(proc, msg_info, NULL);
223 sspt_proc_install(proc);
224 up_write(&task->mm->mmap_sem);
227 static void subsequent_install(struct task_struct *task,
228 struct sspt_proc *proc, unsigned long page_addr)
230 down_write(&task->mm->mmap_sem);
231 sspt_proc_install_page(proc, page_addr);
232 up_write(&task->mm->mmap_sem);
236 * @brief Get dentry struct by path
238 * @param path Path to file
239 * @return Pointer on dentry struct on NULL
241 struct dentry *dentry_by_path(const char *path)
243 struct dentry *dentry;
244 #if LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 38)
246 if (kern_path(path, LOOKUP_FOLLOW, &st_path) != 0) {
247 #else /* LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 38) */
249 if (path_lookup(path, LOOKUP_FOLLOW, &nd) != 0) {
250 #endif /* LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 38) */
251 printk("failed to lookup dentry for path %s!\n", path);
255 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 25)
258 #elif LINUX_VERSION_CODE <= KERNEL_VERSION(2, 6, 38)
259 dentry = nd.path.dentry;
261 #else /* LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 38) */
262 dentry = st_path.dentry;
264 #endif /* LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 25) */
267 EXPORT_SYMBOL_GPL(dentry_by_path);
270 int pfg_msg_cb_set(struct pf_group *pfg, struct pfg_msg_cb *msg_cb)
275 pfg->msg_cb = msg_cb;
279 EXPORT_SYMBOL_GPL(pfg_msg_cb_set);
281 void pfg_msg_cb_reset(struct pf_group *pfg)
285 EXPORT_SYMBOL_GPL(pfg_msg_cb_reset);
287 struct pfg_msg_cb *pfg_msg_cb_get(struct pf_group *pfg)
293 * @brief Get pf_group struct by dentry
295 * @param dentry Dentry of file
296 * @param priv Private data
297 * @return Pointer on pf_group struct
299 struct pf_group *get_pf_group_by_dentry(struct dentry *dentry, void *priv)
301 struct pf_group *pfg;
304 list_for_each_entry(pfg, &pfg_list, list) {
305 if (check_pf_by_dentry(&pfg->filter, dentry)) {
306 atomic_inc(&pfg->usage);
315 set_pf_by_dentry(&pfg->filter, dentry, priv);
317 pfg_add_to_list(pfg);
323 EXPORT_SYMBOL_GPL(get_pf_group_by_dentry);
326 * @brief Get pf_group struct by TGID
328 * @param tgid Thread group ID
329 * @param priv Private data
330 * @return Pointer on pf_group struct
332 struct pf_group *get_pf_group_by_tgid(pid_t tgid, void *priv)
334 struct pf_group *pfg;
337 list_for_each_entry(pfg, &pfg_list, list) {
338 if (check_pf_by_tgid(&pfg->filter, tgid)) {
339 atomic_inc(&pfg->usage);
348 set_pf_by_tgid(&pfg->filter, tgid, priv);
350 pfg_add_to_list(pfg);
356 EXPORT_SYMBOL_GPL(get_pf_group_by_tgid);
359 * @brief Get pf_group struct by comm
361 * @param comm Task comm
362 * @param priv Private data
363 * @return Pointer on pf_group struct
365 struct pf_group *get_pf_group_by_comm(char *comm, void *priv)
368 struct pf_group *pfg;
371 list_for_each_entry(pfg, &pfg_list, list) {
372 if (check_pf_by_comm(&pfg->filter, comm)) {
373 atomic_inc(&pfg->usage);
382 ret = set_pf_by_comm(&pfg->filter, comm, priv);
384 printk(KERN_ERR "ERROR: set_pf_by_comm, ret=%d\n", ret);
390 pfg_add_to_list(pfg);
395 EXPORT_SYMBOL_GPL(get_pf_group_by_comm);
398 * @brief Get pf_group struct for each process
400 * @param priv Private data
401 * @return Pointer on pf_group struct
403 struct pf_group *get_pf_group_dumb(void *priv)
405 struct pf_group *pfg;
408 list_for_each_entry(pfg, &pfg_list, list) {
409 if (check_pf_dumb(&pfg->filter)) {
410 atomic_inc(&pfg->usage);
419 set_pf_dumb(&pfg->filter, priv);
421 pfg_add_to_list(pfg);
427 EXPORT_SYMBOL_GPL(get_pf_group_dumb);
430 * @brief Put pf_group struct
432 * @param pfg Pointer to the pf_group struct
435 void put_pf_group(struct pf_group *pfg)
437 if (atomic_dec_and_test(&pfg->usage)) {
439 pfg_del_from_list(pfg);
445 EXPORT_SYMBOL_GPL(put_pf_group);
448 * @brief Register prober for pf_grpup struct
450 * @param pfg Pointer to the pf_group struct
451 * @param dentry Dentry of file
452 * @param offset Function offset
453 * @param probe_info Pointer to the related probe_info struct
456 int pf_register_probe(struct pf_group *pfg, struct dentry *dentry,
457 unsigned long offset, struct probe_desc *pd)
459 return img_proc_add_ip(pfg->i_proc, dentry, offset, pd);
461 EXPORT_SYMBOL_GPL(pf_register_probe);
464 * @brief Unregister prober from pf_grpup struct
466 * @param pfg Pointer to the pf_group struct
467 * @param dentry Dentry of file
468 * @param offset Function offset
471 int pf_unregister_probe(struct pf_group *pfg, struct dentry *dentry,
472 unsigned long offset, struct probe_desc *pd)
474 return img_proc_del_ip(pfg->i_proc, dentry, offset, pd);
476 EXPORT_SYMBOL_GPL(pf_unregister_probe);
478 static int check_task_on_filters(struct task_struct *task)
481 struct pf_group *pfg;
484 list_for_each_entry(pfg, &pfg_list, list) {
485 if (check_task_f(&pfg->filter, task)) {
503 static enum pf_inst_flag pfg_check_task(struct task_struct *task)
505 struct pf_group *pfg;
506 struct sspt_proc *proc = NULL;
507 enum pf_inst_flag flag = PIF_NONE;
510 list_for_each_entry(pfg, &pfg_list, list) {
511 if (check_task_f(&pfg->filter, task) == NULL)
515 proc = sspt_proc_get_by_task(task);
518 flag = flag == PIF_NONE ? PIF_SECOND : flag;
519 } else if (task->tgid == task->pid) {
520 proc = sspt_proc_get_by_task_or_new(task);
522 printk(KERN_ERR "cannot create sspt_proc\n");
529 mutex_lock(&proc->filters.mtx);
530 if (sspt_proc_is_filter_new(proc, pfg)) {
531 img_proc_copy_to_sspt(pfg->i_proc, proc);
532 sspt_proc_add_filter(proc, pfg);
533 pfg_add_proc(pfg, proc);
534 flag = flag == PIF_FIRST ? flag : PIF_ADD_PFG;
536 mutex_unlock(&proc->filters.mtx);
545 static void pfg_all_del_proc(struct sspt_proc *proc)
547 struct pf_group *pfg;
550 list_for_each_entry(pfg, &pfg_list, list)
551 pfg_del_proc(pfg, proc);
556 * @brief Check task and install probes on demand
558 * @prarm task Pointer on the task_struct struct
561 void check_task_and_install(struct task_struct *task)
563 struct sspt_proc *proc;
564 enum pf_inst_flag flag;
566 flag = pfg_check_task(task);
570 proc = sspt_proc_get_by_task(task);
572 first_install(task, proc);
584 * @brief Check task and install probes on demand
586 * @prarm task Pointer on the task_struct struct
587 * @param page_addr Page fault address
590 void call_page_fault(struct task_struct *task, unsigned long page_addr)
592 struct sspt_proc *proc;
593 enum pf_inst_flag flag;
595 flag = pfg_check_task(task);
599 proc = sspt_proc_get_by_task(task);
601 first_install(task, proc);
607 proc = sspt_proc_get_by_task(task);
609 subsequent_install(task, proc, page_addr);
620 * @brief Uninstall probes from the sspt_proc struct
622 * @prarm proc Pointer on the sspt_proc struct
626 /* called with sspt_proc_write_lock() */
627 void uninstall_proc(struct sspt_proc *proc)
629 struct task_struct *task = proc->leader;
631 sspt_proc_uninstall(proc, task, US_UNREGS_PROBE);
632 sspt_proc_cleanup(proc);
636 static void mmr_from_exit(struct sspt_proc *proc)
638 BUG_ON(proc->leader != current);
640 sspt_proc_write_lock();
641 list_del(&proc->list);
642 sspt_proc_write_unlock();
644 uninstall_proc(proc);
646 pfg_all_del_proc(proc);
647 sspt_reset_proc(proc->leader);
650 static void mmr_from_exec(struct sspt_proc *proc)
652 BUG_ON(proc->leader != current);
654 if (proc->suspect.after_exec) {
655 sspt_proc_uninstall(proc, proc->leader, US_UNREGS_PROBE);
662 * @brief Remove probes from the task on demand
664 * @prarm task Pointer on the task_struct struct
667 void call_mm_release(struct task_struct *task)
669 struct sspt_proc *proc;
671 proc = sspt_proc_get_by_task(task);
673 if (task->flags & PF_EXITING)
682 * @brief Legacy code, it is need remove
684 * @param addr Page address
687 void uninstall_page(unsigned long addr)
693 static void install_cb(void *unused)
695 check_task_and_install(current);
702 struct list_head list;
703 struct task_struct *task;
706 static void tasks_get(struct list_head *head)
708 struct task_item *item;
709 struct task_struct *task;
712 for_each_process(task) {
713 if (task->flags & PF_KTHREAD)
716 if (sspt_proc_by_task(task))
719 /* TODO: get rid of GFP_ATOMIC */
720 item = kmalloc(sizeof(*item), GFP_ATOMIC);
722 WARN(1, "out of memory\n");
726 get_task_struct(task);
728 list_add(&item->list, head);
735 static void tasks_install_and_put(struct list_head *head)
737 struct task_item *item, *n;
739 list_for_each_entry_safe(item, n, head, list) {
741 struct task_struct *task;
744 if (!check_task_on_filters(task))
747 ret = taskctx_run(task, install_cb, NULL);
749 pr_err("cannot tracking task[%u %u %s] ret=%d\n",
750 task->tgid, task->pid, task->comm, ret);
754 put_task_struct(task);
755 list_del(&item->list);
760 static void do_install_all(void)
765 tasks_install_and_put(&head);
769 * @brief Install probes on running processes
773 void install_all(void)
782 pr_err("taskctx_get ret=%d\n", ret);
787 * @brief Uninstall probes from all processes
791 void uninstall_all(void)
793 struct list_head *proc_list = sspt_proc_list();
795 sspt_proc_write_lock();
796 while (!list_empty(proc_list)) {
797 struct sspt_proc *proc;
798 proc = list_first_entry(proc_list, struct sspt_proc, list);
800 list_del(&proc->list);
802 sspt_proc_write_unlock();
803 uninstall_proc(proc);
804 sspt_proc_write_lock();
806 sspt_proc_write_unlock();
809 static void __do_get_proc(struct sspt_proc *proc, void *data)
811 struct task_struct *task = proc->leader;
813 get_task_struct(task);
815 proc->__mm = get_task_mm(task);
818 static void __do_put_proc(struct sspt_proc *proc, void *data)
826 put_task_struct(proc->__task);
831 void get_all_procs(void)
833 sspt_proc_read_lock();
834 on_each_proc_no_lock(__do_get_proc, NULL);
835 sspt_proc_read_unlock();
838 void put_all_procs(void)
840 sspt_proc_read_lock();
841 on_each_proc_no_lock(__do_put_proc, NULL);
842 sspt_proc_read_unlock();
848 * @param pfg Pointer to the pf_group struct
853 void pfg_print(struct pf_group *pfg)
855 img_proc_print(pfg->i_proc);
857 EXPORT_SYMBOL_GPL(pfg_print);