#include <linux/elf.h>
 #include <linux/moduleloader.h>
 #include <linux/completion.h>
+#include <linux/memory.h>
 #include <asm/cacheflush.h>
 #include "core.h"
 #include "patch.h"
        struct klp_func *func;
        int ret;
 
+       mutex_lock(&text_mutex);
+
        module_disable_ro(patch->mod);
        ret = klp_write_object_relocations(patch->mod, obj);
        if (ret) {
                module_enable_ro(patch->mod, true);
+               mutex_unlock(&text_mutex);
                return ret;
        }
 
        arch_klp_init_object_loaded(patch, obj);
        module_enable_ro(patch->mod, true);
 
+       mutex_unlock(&text_mutex);
+
        klp_for_each_func(obj, func) {
                ret = klp_find_object_symbol(obj->name, func->old_name,
                                             func->old_sympos,
 
 #include <linux/hash.h>
 #include <linux/rcupdate.h>
 #include <linux/kprobes.h>
+#include <linux/memory.h>
 
 #include <trace/events/sched.h>
 
 {
        int ret;
 
+       mutex_lock(&text_mutex);
+
        ret = ftrace_arch_code_modify_prepare();
        FTRACE_WARN_ON(ret);
        if (ret)
-               return;
+               goto out_unlock;
 
        /*
         * By default we use stop_machine() to modify the code.
 
        ret = ftrace_arch_code_modify_post_process();
        FTRACE_WARN_ON(ret);
+
+out_unlock:
+       mutex_unlock(&text_mutex);
 }
 
 static void ftrace_run_modify_code(struct ftrace_ops *ops, int command,
        struct ftrace_page *pg;
 
        mutex_lock(&ftrace_lock);
+       mutex_lock(&text_mutex);
 
        if (ftrace_disabled)
                goto out_unlock;
                ftrace_arch_code_modify_post_process();
 
  out_unlock:
+       mutex_unlock(&text_mutex);
        mutex_unlock(&ftrace_lock);
 
        process_cached_mods(mod->name);