2 * core.c - Kernel Live Patching Core
4 * Copyright (C) 2014 Seth Jennings <sjenning@redhat.com>
5 * Copyright (C) 2014 SUSE
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version 2
10 * of the License, or (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, see <http://www.gnu.org/licenses/>.
21 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
23 #include <linux/module.h>
24 #include <linux/kernel.h>
25 #include <linux/mutex.h>
26 #include <linux/slab.h>
27 #include <linux/list.h>
28 #include <linux/kallsyms.h>
29 #include <linux/livepatch.h>
30 #include <linux/elf.h>
31 #include <linux/moduleloader.h>
32 #include <linux/completion.h>
33 #include <asm/cacheflush.h>
36 #include "transition.h"
39 * klp_mutex is a coarse lock which serializes access to klp data. All
40 * accesses to klp-related variables and structures must have mutex protection,
41 * except within the following functions which carefully avoid the need for it:
43 * - klp_ftrace_handler()
44 * - klp_update_patch_state()
46 DEFINE_MUTEX(klp_mutex);
49 * Actively used patches: enabled or in transition. Note that replaced
50 * or disabled patches are not listed even though the related kernel
51 * module still can be loaded.
53 LIST_HEAD(klp_patches);
55 static struct kobject *klp_root_kobj;
57 static bool klp_is_module(struct klp_object *obj)
62 /* sets obj->mod if object is not vmlinux and module is found */
63 static void klp_find_object_module(struct klp_object *obj)
67 if (!klp_is_module(obj))
70 mutex_lock(&module_mutex);
72 * We do not want to block removal of patched modules and therefore
73 * we do not take a reference here. The patches are removed by
74 * klp_module_going() instead.
76 mod = find_module(obj->name);
78 * Do not mess work of klp_module_coming() and klp_module_going().
79 * Note that the patch might still be needed before klp_module_going()
80 * is called. Module functions can be called even in the GOING state
81 * until mod->exit() finishes. This is especially important for
82 * patches that modify semantic of the functions.
84 if (mod && mod->klp_alive)
87 mutex_unlock(&module_mutex);
90 static bool klp_initialized(void)
92 return !!klp_root_kobj;
95 static struct klp_func *klp_find_func(struct klp_object *obj,
96 struct klp_func *old_func)
98 struct klp_func *func;
100 klp_for_each_func(obj, func) {
101 if ((strcmp(old_func->old_name, func->old_name) == 0) &&
102 (old_func->old_sympos == func->old_sympos)) {
110 static struct klp_object *klp_find_object(struct klp_patch *patch,
111 struct klp_object *old_obj)
113 struct klp_object *obj;
115 klp_for_each_object(patch, obj) {
116 if (klp_is_module(old_obj)) {
117 if (klp_is_module(obj) &&
118 strcmp(old_obj->name, obj->name) == 0) {
121 } else if (!klp_is_module(obj)) {
129 struct klp_find_arg {
137 static int klp_find_callback(void *data, const char *name,
138 struct module *mod, unsigned long addr)
140 struct klp_find_arg *args = data;
142 if ((mod && !args->objname) || (!mod && args->objname))
145 if (strcmp(args->name, name))
148 if (args->objname && strcmp(args->objname, mod->name))
155 * Finish the search when the symbol is found for the desired position
156 * or the position is not defined for a non-unique symbol.
158 if ((args->pos && (args->count == args->pos)) ||
159 (!args->pos && (args->count > 1)))
165 static int klp_find_object_symbol(const char *objname, const char *name,
166 unsigned long sympos, unsigned long *addr)
168 struct klp_find_arg args = {
176 mutex_lock(&module_mutex);
178 module_kallsyms_on_each_symbol(klp_find_callback, &args);
180 kallsyms_on_each_symbol(klp_find_callback, &args);
181 mutex_unlock(&module_mutex);
184 * Ensure an address was found. If sympos is 0, ensure symbol is unique;
185 * otherwise ensure the symbol position count matches sympos.
188 pr_err("symbol '%s' not found in symbol table\n", name);
189 else if (args.count > 1 && sympos == 0) {
190 pr_err("unresolvable ambiguity for symbol '%s' in object '%s'\n",
192 } else if (sympos != args.count && sympos > 0) {
193 pr_err("symbol position %lu for symbol '%s' in object '%s' not found\n",
194 sympos, name, objname ? objname : "vmlinux");
204 static int klp_resolve_symbols(Elf_Shdr *relasec, struct module *pmod)
206 int i, cnt, vmlinux, ret;
207 char objname[MODULE_NAME_LEN];
208 char symname[KSYM_NAME_LEN];
209 char *strtab = pmod->core_kallsyms.strtab;
212 unsigned long sympos, addr;
215 * Since the field widths for objname and symname in the sscanf()
216 * call are hard-coded and correspond to MODULE_NAME_LEN and
217 * KSYM_NAME_LEN respectively, we must make sure that MODULE_NAME_LEN
218 * and KSYM_NAME_LEN have the values we expect them to have.
220 * Because the value of MODULE_NAME_LEN can differ among architectures,
221 * we use the smallest/strictest upper bound possible (56, based on
222 * the current definition of MODULE_NAME_LEN) to prevent overflows.
224 BUILD_BUG_ON(MODULE_NAME_LEN < 56 || KSYM_NAME_LEN != 128);
226 relas = (Elf_Rela *) relasec->sh_addr;
227 /* For each rela in this klp relocation section */
228 for (i = 0; i < relasec->sh_size / sizeof(Elf_Rela); i++) {
229 sym = pmod->core_kallsyms.symtab + ELF_R_SYM(relas[i].r_info);
230 if (sym->st_shndx != SHN_LIVEPATCH) {
231 pr_err("symbol %s is not marked as a livepatch symbol\n",
232 strtab + sym->st_name);
236 /* Format: .klp.sym.objname.symname,sympos */
237 cnt = sscanf(strtab + sym->st_name,
238 ".klp.sym.%55[^.].%127[^,],%lu",
239 objname, symname, &sympos);
241 pr_err("symbol %s has an incorrectly formatted name\n",
242 strtab + sym->st_name);
246 /* klp_find_object_symbol() treats a NULL objname as vmlinux */
247 vmlinux = !strcmp(objname, "vmlinux");
248 ret = klp_find_object_symbol(vmlinux ? NULL : objname,
249 symname, sympos, &addr);
253 sym->st_value = addr;
259 static int klp_write_object_relocations(struct module *pmod,
260 struct klp_object *obj)
263 const char *objname, *secname;
264 char sec_objname[MODULE_NAME_LEN];
267 if (WARN_ON(!klp_is_object_loaded(obj)))
270 objname = klp_is_module(obj) ? obj->name : "vmlinux";
272 /* For each klp relocation section */
273 for (i = 1; i < pmod->klp_info->hdr.e_shnum; i++) {
274 sec = pmod->klp_info->sechdrs + i;
275 secname = pmod->klp_info->secstrings + sec->sh_name;
276 if (!(sec->sh_flags & SHF_RELA_LIVEPATCH))
280 * Format: .klp.rela.sec_objname.section_name
281 * See comment in klp_resolve_symbols() for an explanation
282 * of the selected field width value.
284 cnt = sscanf(secname, ".klp.rela.%55[^.]", sec_objname);
286 pr_err("section %s has an incorrectly formatted name\n",
292 if (strcmp(objname, sec_objname))
295 ret = klp_resolve_symbols(sec, pmod);
299 ret = apply_relocate_add(pmod->klp_info->sechdrs,
300 pmod->core_kallsyms.strtab,
301 pmod->klp_info->symndx, i, pmod);
312 * /sys/kernel/livepatch
313 * /sys/kernel/livepatch/<patch>
314 * /sys/kernel/livepatch/<patch>/enabled
315 * /sys/kernel/livepatch/<patch>/transition
316 * /sys/kernel/livepatch/<patch>/force
317 * /sys/kernel/livepatch/<patch>/<object>
318 * /sys/kernel/livepatch/<patch>/<object>/<function,sympos>
320 static int __klp_disable_patch(struct klp_patch *patch);
322 static ssize_t enabled_store(struct kobject *kobj, struct kobj_attribute *attr,
323 const char *buf, size_t count)
325 struct klp_patch *patch;
329 ret = kstrtobool(buf, &enabled);
333 patch = container_of(kobj, struct klp_patch, kobj);
335 mutex_lock(&klp_mutex);
337 if (patch->enabled == enabled) {
338 /* already in requested state */
344 * Allow to reverse a pending transition in both ways. It might be
345 * necessary to complete the transition without forcing and breaking
346 * the system integrity.
348 * Do not allow to re-enable a disabled patch.
350 if (patch == klp_transition_patch)
351 klp_reverse_transition();
353 ret = __klp_disable_patch(patch);
358 mutex_unlock(&klp_mutex);
365 static ssize_t enabled_show(struct kobject *kobj,
366 struct kobj_attribute *attr, char *buf)
368 struct klp_patch *patch;
370 patch = container_of(kobj, struct klp_patch, kobj);
371 return snprintf(buf, PAGE_SIZE-1, "%d\n", patch->enabled);
374 static ssize_t transition_show(struct kobject *kobj,
375 struct kobj_attribute *attr, char *buf)
377 struct klp_patch *patch;
379 patch = container_of(kobj, struct klp_patch, kobj);
380 return snprintf(buf, PAGE_SIZE-1, "%d\n",
381 patch == klp_transition_patch);
384 static ssize_t force_store(struct kobject *kobj, struct kobj_attribute *attr,
385 const char *buf, size_t count)
387 struct klp_patch *patch;
391 ret = kstrtobool(buf, &val);
398 mutex_lock(&klp_mutex);
400 patch = container_of(kobj, struct klp_patch, kobj);
401 if (patch != klp_transition_patch) {
402 mutex_unlock(&klp_mutex);
406 klp_force_transition();
408 mutex_unlock(&klp_mutex);
413 static struct kobj_attribute enabled_kobj_attr = __ATTR_RW(enabled);
414 static struct kobj_attribute transition_kobj_attr = __ATTR_RO(transition);
415 static struct kobj_attribute force_kobj_attr = __ATTR_WO(force);
416 static struct attribute *klp_patch_attrs[] = {
417 &enabled_kobj_attr.attr,
418 &transition_kobj_attr.attr,
419 &force_kobj_attr.attr,
422 ATTRIBUTE_GROUPS(klp_patch);
424 static void klp_free_object_dynamic(struct klp_object *obj)
430 static void klp_init_func_early(struct klp_object *obj,
431 struct klp_func *func);
432 static void klp_init_object_early(struct klp_patch *patch,
433 struct klp_object *obj);
435 static struct klp_object *klp_alloc_object_dynamic(const char *name,
436 struct klp_patch *patch)
438 struct klp_object *obj;
440 obj = kzalloc(sizeof(*obj), GFP_KERNEL);
445 obj->name = kstrdup(name, GFP_KERNEL);
452 klp_init_object_early(patch, obj);
458 static void klp_free_func_nop(struct klp_func *func)
460 kfree(func->old_name);
464 static struct klp_func *klp_alloc_func_nop(struct klp_func *old_func,
465 struct klp_object *obj)
467 struct klp_func *func;
469 func = kzalloc(sizeof(*func), GFP_KERNEL);
473 if (old_func->old_name) {
474 func->old_name = kstrdup(old_func->old_name, GFP_KERNEL);
475 if (!func->old_name) {
481 klp_init_func_early(obj, func);
483 * func->new_func is same as func->old_func. These addresses are
484 * set when the object is loaded, see klp_init_object_loaded().
486 func->old_sympos = old_func->old_sympos;
492 static int klp_add_object_nops(struct klp_patch *patch,
493 struct klp_object *old_obj)
495 struct klp_object *obj;
496 struct klp_func *func, *old_func;
498 obj = klp_find_object(patch, old_obj);
501 obj = klp_alloc_object_dynamic(old_obj->name, patch);
506 klp_for_each_func(old_obj, old_func) {
507 func = klp_find_func(obj, old_func);
511 func = klp_alloc_func_nop(old_func, obj);
520 * Add 'nop' functions which simply return to the caller to run
521 * the original function. The 'nop' functions are added to a
522 * patch to facilitate a 'replace' mode.
524 static int klp_add_nops(struct klp_patch *patch)
526 struct klp_patch *old_patch;
527 struct klp_object *old_obj;
529 klp_for_each_patch(old_patch) {
530 klp_for_each_object(old_patch, old_obj) {
533 err = klp_add_object_nops(patch, old_obj);
542 static void klp_kobj_release_patch(struct kobject *kobj)
544 struct klp_patch *patch;
546 patch = container_of(kobj, struct klp_patch, kobj);
547 complete(&patch->finish);
550 static struct kobj_type klp_ktype_patch = {
551 .release = klp_kobj_release_patch,
552 .sysfs_ops = &kobj_sysfs_ops,
553 .default_groups = klp_patch_groups,
556 static void klp_kobj_release_object(struct kobject *kobj)
558 struct klp_object *obj;
560 obj = container_of(kobj, struct klp_object, kobj);
563 klp_free_object_dynamic(obj);
566 static struct kobj_type klp_ktype_object = {
567 .release = klp_kobj_release_object,
568 .sysfs_ops = &kobj_sysfs_ops,
571 static void klp_kobj_release_func(struct kobject *kobj)
573 struct klp_func *func;
575 func = container_of(kobj, struct klp_func, kobj);
578 klp_free_func_nop(func);
581 static struct kobj_type klp_ktype_func = {
582 .release = klp_kobj_release_func,
583 .sysfs_ops = &kobj_sysfs_ops,
586 static void __klp_free_funcs(struct klp_object *obj, bool nops_only)
588 struct klp_func *func, *tmp_func;
590 klp_for_each_func_safe(obj, func, tmp_func) {
591 if (nops_only && !func->nop)
594 list_del(&func->node);
595 kobject_put(&func->kobj);
599 /* Clean up when a patched object is unloaded */
600 static void klp_free_object_loaded(struct klp_object *obj)
602 struct klp_func *func;
606 klp_for_each_func(obj, func) {
607 func->old_func = NULL;
610 func->new_func = NULL;
614 static void __klp_free_objects(struct klp_patch *patch, bool nops_only)
616 struct klp_object *obj, *tmp_obj;
618 klp_for_each_object_safe(patch, obj, tmp_obj) {
619 __klp_free_funcs(obj, nops_only);
621 if (nops_only && !obj->dynamic)
624 list_del(&obj->node);
625 kobject_put(&obj->kobj);
629 static void klp_free_objects(struct klp_patch *patch)
631 __klp_free_objects(patch, false);
634 static void klp_free_objects_dynamic(struct klp_patch *patch)
636 __klp_free_objects(patch, true);
640 * This function implements the free operations that can be called safely
643 * The operation must be completed by calling klp_free_patch_finish()
646 void klp_free_patch_start(struct klp_patch *patch)
648 if (!list_empty(&patch->list))
649 list_del(&patch->list);
651 klp_free_objects(patch);
655 * This function implements the free part that must be called outside
658 * It must be called after klp_free_patch_start(). And it has to be
659 * the last function accessing the livepatch structures when the patch
662 static void klp_free_patch_finish(struct klp_patch *patch)
665 * Avoid deadlock with enabled_store() sysfs callback by
666 * calling this outside klp_mutex. It is safe because
667 * this is called when the patch gets disabled and it
668 * cannot get enabled again.
670 kobject_put(&patch->kobj);
671 wait_for_completion(&patch->finish);
673 /* Put the module after the last access to struct klp_patch. */
675 module_put(patch->mod);
679 * The livepatch might be freed from sysfs interface created by the patch.
680 * This work allows to wait until the interface is destroyed in a separate
683 static void klp_free_patch_work_fn(struct work_struct *work)
685 struct klp_patch *patch =
686 container_of(work, struct klp_patch, free_work);
688 klp_free_patch_finish(patch);
691 static int klp_init_func(struct klp_object *obj, struct klp_func *func)
697 * NOPs get the address later. The patched module must be loaded,
698 * see klp_init_object_loaded().
700 if (!func->new_func && !func->nop)
703 if (strlen(func->old_name) >= KSYM_NAME_LEN)
706 INIT_LIST_HEAD(&func->stack_node);
707 func->patched = false;
708 func->transition = false;
710 /* The format for the sysfs directory is <function,sympos> where sympos
711 * is the nth occurrence of this symbol in kallsyms for the patched
712 * object. If the user selects 0 for old_sympos, then 1 will be used
713 * since a unique symbol will be the first occurrence.
715 return kobject_add(&func->kobj, &obj->kobj, "%s,%lu",
717 func->old_sympos ? func->old_sympos : 1);
720 /* Arches may override this to finish any remaining arch-specific tasks */
721 void __weak arch_klp_init_object_loaded(struct klp_patch *patch,
722 struct klp_object *obj)
726 /* parts of the initialization that is done only when the object is loaded */
727 static int klp_init_object_loaded(struct klp_patch *patch,
728 struct klp_object *obj)
730 struct klp_func *func;
733 module_disable_ro(patch->mod);
734 ret = klp_write_object_relocations(patch->mod, obj);
736 module_enable_ro(patch->mod, true);
740 arch_klp_init_object_loaded(patch, obj);
741 module_enable_ro(patch->mod, true);
743 klp_for_each_func(obj, func) {
744 ret = klp_find_object_symbol(obj->name, func->old_name,
746 (unsigned long *)&func->old_func);
750 ret = kallsyms_lookup_size_offset((unsigned long)func->old_func,
751 &func->old_size, NULL);
753 pr_err("kallsyms size lookup failed for '%s'\n",
759 func->new_func = func->old_func;
761 ret = kallsyms_lookup_size_offset((unsigned long)func->new_func,
762 &func->new_size, NULL);
764 pr_err("kallsyms size lookup failed for '%s' replacement\n",
773 static int klp_init_object(struct klp_patch *patch, struct klp_object *obj)
775 struct klp_func *func;
779 if (klp_is_module(obj) && strlen(obj->name) >= MODULE_NAME_LEN)
782 obj->patched = false;
785 klp_find_object_module(obj);
787 name = klp_is_module(obj) ? obj->name : "vmlinux";
788 ret = kobject_add(&obj->kobj, &patch->kobj, "%s", name);
792 klp_for_each_func(obj, func) {
793 ret = klp_init_func(obj, func);
798 if (klp_is_object_loaded(obj))
799 ret = klp_init_object_loaded(patch, obj);
804 static void klp_init_func_early(struct klp_object *obj,
805 struct klp_func *func)
807 kobject_init(&func->kobj, &klp_ktype_func);
808 list_add_tail(&func->node, &obj->func_list);
811 static void klp_init_object_early(struct klp_patch *patch,
812 struct klp_object *obj)
814 INIT_LIST_HEAD(&obj->func_list);
815 kobject_init(&obj->kobj, &klp_ktype_object);
816 list_add_tail(&obj->node, &patch->obj_list);
819 static int klp_init_patch_early(struct klp_patch *patch)
821 struct klp_object *obj;
822 struct klp_func *func;
827 INIT_LIST_HEAD(&patch->list);
828 INIT_LIST_HEAD(&patch->obj_list);
829 kobject_init(&patch->kobj, &klp_ktype_patch);
830 patch->enabled = false;
831 patch->forced = false;
832 INIT_WORK(&patch->free_work, klp_free_patch_work_fn);
833 init_completion(&patch->finish);
835 klp_for_each_object_static(patch, obj) {
839 klp_init_object_early(patch, obj);
841 klp_for_each_func_static(obj, func) {
842 klp_init_func_early(obj, func);
846 if (!try_module_get(patch->mod))
852 static int klp_init_patch(struct klp_patch *patch)
854 struct klp_object *obj;
857 ret = kobject_add(&patch->kobj, klp_root_kobj, "%s", patch->mod->name);
861 if (patch->replace) {
862 ret = klp_add_nops(patch);
867 klp_for_each_object(patch, obj) {
868 ret = klp_init_object(patch, obj);
873 list_add_tail(&patch->list, &klp_patches);
878 static int __klp_disable_patch(struct klp_patch *patch)
880 struct klp_object *obj;
882 if (WARN_ON(!patch->enabled))
885 if (klp_transition_patch)
888 klp_init_transition(patch, KLP_UNPATCHED);
890 klp_for_each_object(patch, obj)
892 klp_pre_unpatch_callback(obj);
895 * Enforce the order of the func->transition writes in
896 * klp_init_transition() and the TIF_PATCH_PENDING writes in
897 * klp_start_transition(). In the rare case where klp_ftrace_handler()
898 * is called shortly after klp_update_patch_state() switches the task,
899 * this ensures the handler sees that func->transition is set.
903 klp_start_transition();
904 patch->enabled = false;
905 klp_try_complete_transition();
910 static int __klp_enable_patch(struct klp_patch *patch)
912 struct klp_object *obj;
915 if (klp_transition_patch)
918 if (WARN_ON(patch->enabled))
921 pr_notice("enabling patch '%s'\n", patch->mod->name);
923 klp_init_transition(patch, KLP_PATCHED);
926 * Enforce the order of the func->transition writes in
927 * klp_init_transition() and the ops->func_stack writes in
928 * klp_patch_object(), so that klp_ftrace_handler() will see the
929 * func->transition updates before the handler is registered and the
930 * new funcs become visible to the handler.
934 klp_for_each_object(patch, obj) {
935 if (!klp_is_object_loaded(obj))
938 ret = klp_pre_patch_callback(obj);
940 pr_warn("pre-patch callback failed for object '%s'\n",
941 klp_is_module(obj) ? obj->name : "vmlinux");
945 ret = klp_patch_object(obj);
947 pr_warn("failed to patch object '%s'\n",
948 klp_is_module(obj) ? obj->name : "vmlinux");
953 klp_start_transition();
954 patch->enabled = true;
955 klp_try_complete_transition();
959 pr_warn("failed to enable patch '%s'\n", patch->mod->name);
961 klp_cancel_transition();
966 * klp_enable_patch() - enable the livepatch
967 * @patch: patch to be enabled
969 * Initializes the data structure associated with the patch, creates the sysfs
970 * interface, performs the needed symbol lookups and code relocations,
971 * registers the patched functions with ftrace.
973 * This function is supposed to be called from the livepatch module_init()
976 * Return: 0 on success, otherwise error
978 int klp_enable_patch(struct klp_patch *patch)
982 if (!patch || !patch->mod)
985 if (!is_livepatch_module(patch->mod)) {
986 pr_err("module %s is not marked as a livepatch module\n",
991 if (!klp_initialized())
994 if (!klp_have_reliable_stack()) {
995 pr_warn("This architecture doesn't have support for the livepatch consistency model.\n");
996 pr_warn("The livepatch transition may never complete.\n");
999 mutex_lock(&klp_mutex);
1001 ret = klp_init_patch_early(patch);
1003 mutex_unlock(&klp_mutex);
1007 ret = klp_init_patch(patch);
1011 ret = __klp_enable_patch(patch);
1015 mutex_unlock(&klp_mutex);
1020 klp_free_patch_start(patch);
1022 mutex_unlock(&klp_mutex);
1024 klp_free_patch_finish(patch);
1028 EXPORT_SYMBOL_GPL(klp_enable_patch);
1031 * This function removes replaced patches.
1033 * We could be pretty aggressive here. It is called in the situation where
1034 * these structures are no longer accessible. All functions are redirected
1035 * by the klp_transition_patch. They use either a new code or they are in
1036 * the original code because of the special nop function patches.
1038 * The only exception is when the transition was forced. In this case,
1039 * klp_ftrace_handler() might still see the replaced patch on the stack.
1040 * Fortunately, it is carefully designed to work with removed functions
1041 * thanks to RCU. We only have to keep the patches on the system. Also
1042 * this is handled transparently by patch->module_put.
1044 void klp_discard_replaced_patches(struct klp_patch *new_patch)
1046 struct klp_patch *old_patch, *tmp_patch;
1048 klp_for_each_patch_safe(old_patch, tmp_patch) {
1049 if (old_patch == new_patch)
1052 old_patch->enabled = false;
1053 klp_unpatch_objects(old_patch);
1054 klp_free_patch_start(old_patch);
1055 schedule_work(&old_patch->free_work);
1060 * This function removes the dynamically allocated 'nop' functions.
1062 * We could be pretty aggressive. NOPs do not change the existing
1063 * behavior except for adding unnecessary delay by the ftrace handler.
1065 * It is safe even when the transition was forced. The ftrace handler
1066 * will see a valid ops->func_stack entry thanks to RCU.
1068 * We could even free the NOPs structures. They must be the last entry
1069 * in ops->func_stack. Therefore unregister_ftrace_function() is called.
1070 * It does the same as klp_synchronize_transition() to make sure that
1071 * nobody is inside the ftrace handler once the operation finishes.
1073 * IMPORTANT: It must be called right after removing the replaced patches!
1075 void klp_discard_nops(struct klp_patch *new_patch)
1077 klp_unpatch_objects_dynamic(klp_transition_patch);
1078 klp_free_objects_dynamic(klp_transition_patch);
1082 * Remove parts of patches that touch a given kernel module. The list of
1083 * patches processed might be limited. When limit is NULL, all patches
1086 static void klp_cleanup_module_patches_limited(struct module *mod,
1087 struct klp_patch *limit)
1089 struct klp_patch *patch;
1090 struct klp_object *obj;
1092 klp_for_each_patch(patch) {
1096 klp_for_each_object(patch, obj) {
1097 if (!klp_is_module(obj) || strcmp(obj->name, mod->name))
1100 if (patch != klp_transition_patch)
1101 klp_pre_unpatch_callback(obj);
1103 pr_notice("reverting patch '%s' on unloading module '%s'\n",
1104 patch->mod->name, obj->mod->name);
1105 klp_unpatch_object(obj);
1107 klp_post_unpatch_callback(obj);
1109 klp_free_object_loaded(obj);
1115 int klp_module_coming(struct module *mod)
1118 struct klp_patch *patch;
1119 struct klp_object *obj;
1121 if (WARN_ON(mod->state != MODULE_STATE_COMING))
1124 mutex_lock(&klp_mutex);
1126 * Each module has to know that klp_module_coming()
1127 * has been called. We never know what module will
1128 * get patched by a new patch.
1130 mod->klp_alive = true;
1132 klp_for_each_patch(patch) {
1133 klp_for_each_object(patch, obj) {
1134 if (!klp_is_module(obj) || strcmp(obj->name, mod->name))
1139 ret = klp_init_object_loaded(patch, obj);
1141 pr_warn("failed to initialize patch '%s' for module '%s' (%d)\n",
1142 patch->mod->name, obj->mod->name, ret);
1146 pr_notice("applying patch '%s' to loading module '%s'\n",
1147 patch->mod->name, obj->mod->name);
1149 ret = klp_pre_patch_callback(obj);
1151 pr_warn("pre-patch callback failed for object '%s'\n",
1156 ret = klp_patch_object(obj);
1158 pr_warn("failed to apply patch '%s' to module '%s' (%d)\n",
1159 patch->mod->name, obj->mod->name, ret);
1161 klp_post_unpatch_callback(obj);
1165 if (patch != klp_transition_patch)
1166 klp_post_patch_callback(obj);
1172 mutex_unlock(&klp_mutex);
1178 * If a patch is unsuccessfully applied, return
1179 * error to the module loader.
1181 pr_warn("patch '%s' failed for module '%s', refusing to load module '%s'\n",
1182 patch->mod->name, obj->mod->name, obj->mod->name);
1183 mod->klp_alive = false;
1184 klp_cleanup_module_patches_limited(mod, patch);
1185 mutex_unlock(&klp_mutex);
1190 void klp_module_going(struct module *mod)
1192 if (WARN_ON(mod->state != MODULE_STATE_GOING &&
1193 mod->state != MODULE_STATE_COMING))
1196 mutex_lock(&klp_mutex);
1198 * Each module has to know that klp_module_going()
1199 * has been called. We never know what module will
1200 * get patched by a new patch.
1202 mod->klp_alive = false;
1204 klp_cleanup_module_patches_limited(mod, NULL);
1206 mutex_unlock(&klp_mutex);
1209 static int __init klp_init(void)
1213 ret = klp_check_compiler_support();
1215 pr_info("Your compiler is too old; turning off.\n");
1219 klp_root_kobj = kobject_create_and_add("livepatch", kernel_kobj);
1226 module_init(klp_init);