loader: remove swap_kprobe dependence 85/144185/2
authorVyacheslav Cherkashin <v.cherkashin@samsung.com>
Fri, 11 Aug 2017 15:38:43 +0000 (18:38 +0300)
committerVyacheslav Cherkashin <v.cherkashin@samsung.com>
Thu, 17 Aug 2017 12:16:03 +0000 (15:16 +0300)
use usm_hook instead of swap_kprobe

Change-Id: Id3b9b9b9dbd7c7f4ae1d82c087b578ba17ed2e3f
Signed-off-by: Vyacheslav Cherkashin <v.cherkashin@samsung.com>
loader/loader_module.c

index 87e9978..71d94af 100644 (file)
@@ -4,11 +4,10 @@
 #include <linux/mman.h>
 #include <linux/err.h>
 #include <linux/types.h>
-#include <kprobe/swap_kprobes.h>
-#include <kprobe/swap_kprobes_deps.h>
 #include <us_manager/sspt/sspt_proc.h>
 #include <us_manager/sspt/sspt_ip.h>
 #include <us_manager/callbacks.h>
+#include <us_manager/usm_hook.h>
 #include <writer/kernel_operations.h>
 #include <master/swap_initializer.h>
 #include "loader_defs.h"
@@ -255,115 +254,34 @@ static bool __should_we_load_handlers(struct task_struct *task,
 
 
 
-struct mmap_priv {
-       struct dentry *dentry;
-};
-
-static inline bool check_prot(unsigned long prot)
-{
-       return !!((prot & PROT_READ) && (prot & PROT_EXEC));
-}
-
-static int mmap_entry_handler(struct kretprobe_instance *ri,
-                             struct pt_regs *regs)
-{
-       struct file *file = (struct file *)swap_get_karg(regs, 0);
-       unsigned long prot = swap_get_karg(regs, 3);
-       struct mmap_priv *priv = (struct mmap_priv *)ri->data;
-       struct task_struct *task = current->group_leader;
-       struct dentry *dentry, *loader_dentry;
-       struct pd_t *pd;
-       struct hd_t *hd;
-       struct sspt_proc *proc;
-
-       priv->dentry = NULL;
-       if (!check_prot(prot))
-               return 0;
-
-       if (!file)
-               return 0;
-
-       dentry = file->f_path.dentry;
-       if (dentry == NULL)
-               return 0;
-
-       loader_dentry = ld_get_loader_dentry();
-       if (dentry == loader_dentry) {
-               priv->dentry = loader_dentry;
-               return 0;
-       }
-
-       proc = sspt_proc_get_by_task(task);
-       if (!proc)
-               return 0;
-
-       pd = lpd_get(proc);
-       if (pd == NULL) {
-               printk(LOADER_PREFIX "%d: No process data! Current %d %s\n",
-                      __LINE__, current->tgid, current->comm);
-               return 0;
-       }
-
-       hd = lpd_get_hd(pd, dentry);
-       if (hd != NULL)
-               priv->dentry = lpd_get_dentry(hd);
-
-       return 0;
-}
-
-static int mmap_ret_handler(struct kretprobe_instance *ri,
-                           struct pt_regs *regs)
+static void mmap_handler(struct sspt_proc *proc, struct vm_area_struct *vma)
 {
-       struct mmap_priv *priv = (struct mmap_priv *)ri->data;
-       struct task_struct *task = current->group_leader;
        struct pd_t *pd;
-       struct hd_t *hd;
-       struct sspt_proc *proc;
-       struct dentry *loader_dentry;
-       unsigned long vaddr;
-
-       if (!task->mm)
-               return 0;
-
-       if (priv->dentry == NULL)
-               return 0;
-
-       vaddr = (unsigned long)regs_return_value(regs);
-       if (IS_ERR_VALUE(vaddr))
-               return 0;
-
-       proc = sspt_proc_get_by_task(task);
-       if (!proc)
-               return 0;
+       unsigned long vaddr = vma->vm_start;
+       struct dentry *dentry = vma->vm_file->f_path.dentry;
 
        pd = lpd_get(proc);
        if (pd == NULL) {
                printk(LOADER_PREFIX "%d: No process data! Current %d %s\n",
                       __LINE__, current->tgid, current->comm);
-               return 0;
+               return;
        }
 
-       loader_dentry = ld_get_loader_dentry();
-       if (priv->dentry == loader_dentry)
+       if (dentry == ld_get_loader_dentry()) {
                lpd_set_loader_base(pd, vaddr);
+       } else {
+               struct hd_t *hd;
 
-
-       hd = lpd_get_hd(pd, priv->dentry);
-       if (hd != NULL)
-               lpd_set_handlers_base(hd, vaddr);
-
-       return 0;
+               hd = lpd_get_hd(pd, dentry);
+               if (hd)
+                       lpd_set_handlers_base(hd, vaddr);
+       }
 }
 
-static struct kretprobe mmap_rp = {
-#if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 3, 0)
-       .kp.symbol_name = "do_mmap",
-#else /* (LINUX_VERSION_CODE >= KERNEL_VERSION(4, 3, 0)) */
-       .kp.symbol_name = "do_mmap_pgoff",
-#endif /* (LINUX_VERSION_CODE >= KERNEL_VERSION(4, 3, 0)) */
-       .data_size = sizeof(struct mmap_priv),
-       .entry_handler = mmap_entry_handler,
-       .handler = mmap_ret_handler
+
+static struct usm_hook usm_hook = {
+       .owner = THIS_MODULE,
+       .mmap = mmap_handler,
 };
 
 static bool mmap_rp_inst = false;
@@ -374,9 +292,9 @@ static void loader_start_cb(void)
        int res;
 
        mutex_lock(&mmap_rp_mtx);
-       res = swap_register_kretprobe(&mmap_rp);
+       res = usm_hook_reg(&usm_hook);
        if (res != 0)
-               printk(KERN_ERR LOADER_PREFIX "Registering do_mmap_pgoff probe failed\n");
+               pr_err(LOADER_PREFIX "Cannot register usm_hook\n");
        else
                mmap_rp_inst = true;
        mutex_unlock(&mmap_rp_mtx);
@@ -386,7 +304,7 @@ static void loader_stop_cb(void)
 {
        mutex_lock(&mmap_rp_mtx);
        if (mmap_rp_inst) {
-               swap_unregister_kretprobe(&mmap_rp);
+               usm_hook_unreg(&usm_hook);
                mmap_rp_inst = false;
        }
        mutex_unlock(&mmap_rp_mtx);
@@ -530,7 +448,7 @@ void loader_unset(void)
 {
        mutex_lock(&mmap_rp_mtx);
        if (mmap_rp_inst) {
-               swap_unregister_kretprobe(&mmap_rp);
+               usm_hook_unreg(&usm_hook);
                mmap_rp_inst = false;
        }
        mutex_unlock(&mmap_rp_mtx);