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_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);
544 static void pfg_all_del_proc(struct sspt_proc *proc)
546 struct pf_group *pfg;
549 list_for_each_entry(pfg, &pfg_list, list)
550 pfg_del_proc(pfg, proc);
555 * @brief Check task and install probes on demand
557 * @prarm task Pointer on the task_struct struct
560 void check_task_and_install(struct task_struct *task)
562 struct sspt_proc *proc;
563 enum pf_inst_flag flag;
565 flag = pfg_check_task(task);
569 proc = sspt_proc_by_task(task);
571 first_install(task, proc);
581 * @brief Check task and install probes on demand
583 * @prarm task Pointer on the task_struct struct
584 * @param page_addr Page fault address
587 void call_page_fault(struct task_struct *task, unsigned long page_addr)
589 struct sspt_proc *proc;
590 enum pf_inst_flag flag;
592 flag = pfg_check_task(task);
596 proc = sspt_proc_by_task(task);
598 first_install(task, proc);
602 proc = sspt_proc_by_task(task);
604 subsequent_install(task, proc, page_addr);
613 * @brief Uninstall probes from the sspt_proc struct
615 * @prarm proc Pointer on the sspt_proc struct
619 /* called with sspt_proc_write_lock() */
620 void uninstall_proc(struct sspt_proc *proc)
622 struct task_struct *task = proc->leader;
624 sspt_proc_uninstall(proc, task, US_UNREGS_PROBE);
625 sspt_proc_cleanup(proc);
629 static void mmr_from_exit(struct sspt_proc *proc)
631 BUG_ON(proc->leader != current);
633 sspt_proc_write_lock();
634 list_del(&proc->list);
635 sspt_proc_write_unlock();
637 uninstall_proc(proc);
639 pfg_all_del_proc(proc);
640 sspt_reset_proc(proc->leader);
643 static void mmr_from_exec(struct sspt_proc *proc)
645 BUG_ON(proc->leader != current);
647 if (proc->suspect.after_exec) {
648 sspt_proc_uninstall(proc, proc->leader, US_UNREGS_PROBE);
655 * @brief Remove probes from the task on demand
657 * @prarm task Pointer on the task_struct struct
660 void call_mm_release(struct task_struct *task)
662 struct sspt_proc *proc;
664 proc = sspt_proc_by_task(task);
666 if (task->flags & PF_EXITING)
674 * @brief Legacy code, it is need remove
676 * @param addr Page address
679 void uninstall_page(unsigned long addr)
685 static void install_cb(void *unused)
687 check_task_and_install(current);
694 struct list_head list;
695 struct task_struct *task;
698 static void tasks_get(struct list_head *head)
700 struct task_item *item;
701 struct task_struct *task;
704 for_each_process(task) {
705 if (task->flags & PF_KTHREAD)
708 if (sspt_proc_by_task(task))
711 /* TODO: get rid of GFP_ATOMIC */
712 item = kmalloc(sizeof(*item), GFP_ATOMIC);
714 WARN(1, "out of memory\n");
718 get_task_struct(task);
720 list_add(&item->list, head);
727 static void tasks_install_and_put(struct list_head *head)
729 struct task_item *item, *n;
731 list_for_each_entry_safe(item, n, head, list) {
733 struct task_struct *task;
736 if (!check_task_on_filters(task))
739 ret = taskctx_run(task, install_cb, NULL);
741 pr_err("cannot tracking task[%u %u %s] ret=%d\n",
742 task->tgid, task->pid, task->comm, ret);
746 put_task_struct(task);
747 list_del(&item->list);
752 static void do_install_all(void)
757 tasks_install_and_put(&head);
761 * @brief Install probes on running processes
765 void install_all(void)
774 pr_err("taskctx_get ret=%d\n", ret);
779 * @brief Uninstall probes from all processes
783 void uninstall_all(void)
785 struct list_head *proc_list = sspt_proc_list();
787 sspt_proc_write_lock();
788 while (!list_empty(proc_list)) {
789 struct sspt_proc *proc;
790 proc = list_first_entry(proc_list, struct sspt_proc, list);
792 list_del(&proc->list);
794 sspt_proc_write_unlock();
795 uninstall_proc(proc);
796 sspt_proc_write_lock();
798 sspt_proc_write_unlock();
801 static void __do_get_proc(struct sspt_proc *proc, void *data)
803 struct task_struct *task = proc->leader;
805 get_task_struct(task);
807 proc->__mm = get_task_mm(task);
810 static void __do_put_proc(struct sspt_proc *proc, void *data)
818 put_task_struct(proc->__task);
823 void get_all_procs(void)
825 sspt_proc_read_lock();
826 on_each_proc_no_lock(__do_get_proc, NULL);
827 sspt_proc_read_unlock();
830 void put_all_procs(void)
832 sspt_proc_read_lock();
833 on_each_proc_no_lock(__do_put_proc, NULL);
834 sspt_proc_read_unlock();
840 * @param pfg Pointer to the pf_group struct
845 void pfg_print(struct pf_group *pfg)
847 img_proc_print(pfg->i_proc);
849 EXPORT_SYMBOL_GPL(pfg_print);