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
);
48 static LIST_HEAD(klp_patches
);
50 static struct kobject
*klp_root_kobj
;
52 static bool klp_is_module(struct klp_object
*obj
)
57 /* sets obj->mod if object is not vmlinux and module is found */
58 static void klp_find_object_module(struct klp_object
*obj
)
62 if (!klp_is_module(obj
))
65 mutex_lock(&module_mutex
);
67 * We do not want to block removal of patched modules and therefore
68 * we do not take a reference here. The patches are removed by
69 * klp_module_going() instead.
71 mod
= find_module(obj
->name
);
73 * Do not mess work of klp_module_coming() and klp_module_going().
74 * Note that the patch might still be needed before klp_module_going()
75 * is called. Module functions can be called even in the GOING state
76 * until mod->exit() finishes. This is especially important for
77 * patches that modify semantic of the functions.
79 if (mod
&& mod
->klp_alive
)
82 mutex_unlock(&module_mutex
);
85 static bool klp_is_patch_registered(struct klp_patch
*patch
)
87 struct klp_patch
*mypatch
;
89 list_for_each_entry(mypatch
, &klp_patches
, list
)
96 static bool klp_initialized(void)
98 return !!klp_root_kobj
;
101 struct klp_find_arg
{
109 static int klp_find_callback(void *data
, const char *name
,
110 struct module
*mod
, unsigned long addr
)
112 struct klp_find_arg
*args
= data
;
114 if ((mod
&& !args
->objname
) || (!mod
&& args
->objname
))
117 if (strcmp(args
->name
, name
))
120 if (args
->objname
&& strcmp(args
->objname
, mod
->name
))
127 * Finish the search when the symbol is found for the desired position
128 * or the position is not defined for a non-unique symbol.
130 if ((args
->pos
&& (args
->count
== args
->pos
)) ||
131 (!args
->pos
&& (args
->count
> 1)))
137 static int klp_find_object_symbol(const char *objname
, const char *name
,
138 unsigned long sympos
, unsigned long *addr
)
140 struct klp_find_arg args
= {
148 mutex_lock(&module_mutex
);
150 module_kallsyms_on_each_symbol(klp_find_callback
, &args
);
152 kallsyms_on_each_symbol(klp_find_callback
, &args
);
153 mutex_unlock(&module_mutex
);
156 * Ensure an address was found. If sympos is 0, ensure symbol is unique;
157 * otherwise ensure the symbol position count matches sympos.
160 pr_err("symbol '%s' not found in symbol table\n", name
);
161 else if (args
.count
> 1 && sympos
== 0) {
162 pr_err("unresolvable ambiguity for symbol '%s' in object '%s'\n",
164 } else if (sympos
!= args
.count
&& sympos
> 0) {
165 pr_err("symbol position %lu for symbol '%s' in object '%s' not found\n",
166 sympos
, name
, objname
? objname
: "vmlinux");
176 static int klp_resolve_symbols(Elf_Shdr
*relasec
, struct module
*pmod
)
178 int i
, cnt
, vmlinux
, ret
;
179 char objname
[MODULE_NAME_LEN
];
180 char symname
[KSYM_NAME_LEN
];
181 char *strtab
= pmod
->core_kallsyms
.strtab
;
184 unsigned long sympos
, addr
;
187 * Since the field widths for objname and symname in the sscanf()
188 * call are hard-coded and correspond to MODULE_NAME_LEN and
189 * KSYM_NAME_LEN respectively, we must make sure that MODULE_NAME_LEN
190 * and KSYM_NAME_LEN have the values we expect them to have.
192 * Because the value of MODULE_NAME_LEN can differ among architectures,
193 * we use the smallest/strictest upper bound possible (56, based on
194 * the current definition of MODULE_NAME_LEN) to prevent overflows.
196 BUILD_BUG_ON(MODULE_NAME_LEN
< 56 || KSYM_NAME_LEN
!= 128);
198 relas
= (Elf_Rela
*) relasec
->sh_addr
;
199 /* For each rela in this klp relocation section */
200 for (i
= 0; i
< relasec
->sh_size
/ sizeof(Elf_Rela
); i
++) {
201 sym
= pmod
->core_kallsyms
.symtab
+ ELF_R_SYM(relas
[i
].r_info
);
202 if (sym
->st_shndx
!= SHN_LIVEPATCH
) {
203 pr_err("symbol %s is not marked as a livepatch symbol\n",
204 strtab
+ sym
->st_name
);
208 /* Format: .klp.sym.objname.symname,sympos */
209 cnt
= sscanf(strtab
+ sym
->st_name
,
210 ".klp.sym.%55[^.].%127[^,],%lu",
211 objname
, symname
, &sympos
);
213 pr_err("symbol %s has an incorrectly formatted name\n",
214 strtab
+ sym
->st_name
);
218 /* klp_find_object_symbol() treats a NULL objname as vmlinux */
219 vmlinux
= !strcmp(objname
, "vmlinux");
220 ret
= klp_find_object_symbol(vmlinux
? NULL
: objname
,
221 symname
, sympos
, &addr
);
225 sym
->st_value
= addr
;
231 static int klp_write_object_relocations(struct module
*pmod
,
232 struct klp_object
*obj
)
235 const char *objname
, *secname
;
236 char sec_objname
[MODULE_NAME_LEN
];
239 if (WARN_ON(!klp_is_object_loaded(obj
)))
242 objname
= klp_is_module(obj
) ? obj
->name
: "vmlinux";
244 /* For each klp relocation section */
245 for (i
= 1; i
< pmod
->klp_info
->hdr
.e_shnum
; i
++) {
246 sec
= pmod
->klp_info
->sechdrs
+ i
;
247 secname
= pmod
->klp_info
->secstrings
+ sec
->sh_name
;
248 if (!(sec
->sh_flags
& SHF_RELA_LIVEPATCH
))
252 * Format: .klp.rela.sec_objname.section_name
253 * See comment in klp_resolve_symbols() for an explanation
254 * of the selected field width value.
256 cnt
= sscanf(secname
, ".klp.rela.%55[^.]", sec_objname
);
258 pr_err("section %s has an incorrectly formatted name\n",
264 if (strcmp(objname
, sec_objname
))
267 ret
= klp_resolve_symbols(sec
, pmod
);
271 ret
= apply_relocate_add(pmod
->klp_info
->sechdrs
,
272 pmod
->core_kallsyms
.strtab
,
273 pmod
->klp_info
->symndx
, i
, pmod
);
284 * /sys/kernel/livepatch
285 * /sys/kernel/livepatch/<patch>
286 * /sys/kernel/livepatch/<patch>/enabled
287 * /sys/kernel/livepatch/<patch>/transition
288 * /sys/kernel/livepatch/<patch>/signal
289 * /sys/kernel/livepatch/<patch>/force
290 * /sys/kernel/livepatch/<patch>/<object>
291 * /sys/kernel/livepatch/<patch>/<object>/<function,sympos>
293 static int __klp_disable_patch(struct klp_patch
*patch
);
294 static int __klp_enable_patch(struct klp_patch
*patch
);
296 static ssize_t
enabled_store(struct kobject
*kobj
, struct kobj_attribute
*attr
,
297 const char *buf
, size_t count
)
299 struct klp_patch
*patch
;
303 ret
= kstrtobool(buf
, &enabled
);
307 patch
= container_of(kobj
, struct klp_patch
, kobj
);
309 mutex_lock(&klp_mutex
);
311 if (!klp_is_patch_registered(patch
)) {
313 * Module with the patch could either disappear meanwhile or is
314 * not properly initialized yet.
320 if (patch
->enabled
== enabled
) {
321 /* already in requested state */
326 if (patch
== klp_transition_patch
) {
327 klp_reverse_transition();
328 } else if (enabled
) {
329 ret
= __klp_enable_patch(patch
);
333 ret
= __klp_disable_patch(patch
);
338 mutex_unlock(&klp_mutex
);
343 mutex_unlock(&klp_mutex
);
347 static ssize_t
enabled_show(struct kobject
*kobj
,
348 struct kobj_attribute
*attr
, char *buf
)
350 struct klp_patch
*patch
;
352 patch
= container_of(kobj
, struct klp_patch
, kobj
);
353 return snprintf(buf
, PAGE_SIZE
-1, "%d\n", patch
->enabled
);
356 static ssize_t
transition_show(struct kobject
*kobj
,
357 struct kobj_attribute
*attr
, char *buf
)
359 struct klp_patch
*patch
;
361 patch
= container_of(kobj
, struct klp_patch
, kobj
);
362 return snprintf(buf
, PAGE_SIZE
-1, "%d\n",
363 patch
== klp_transition_patch
);
366 static ssize_t
signal_store(struct kobject
*kobj
, struct kobj_attribute
*attr
,
367 const char *buf
, size_t count
)
369 struct klp_patch
*patch
;
373 ret
= kstrtobool(buf
, &val
);
380 mutex_lock(&klp_mutex
);
382 patch
= container_of(kobj
, struct klp_patch
, kobj
);
383 if (patch
!= klp_transition_patch
) {
384 mutex_unlock(&klp_mutex
);
390 mutex_unlock(&klp_mutex
);
395 static ssize_t
force_store(struct kobject
*kobj
, struct kobj_attribute
*attr
,
396 const char *buf
, size_t count
)
398 struct klp_patch
*patch
;
402 ret
= kstrtobool(buf
, &val
);
409 mutex_lock(&klp_mutex
);
411 patch
= container_of(kobj
, struct klp_patch
, kobj
);
412 if (patch
!= klp_transition_patch
) {
413 mutex_unlock(&klp_mutex
);
417 klp_force_transition();
419 mutex_unlock(&klp_mutex
);
424 static struct kobj_attribute enabled_kobj_attr
= __ATTR_RW(enabled
);
425 static struct kobj_attribute transition_kobj_attr
= __ATTR_RO(transition
);
426 static struct kobj_attribute signal_kobj_attr
= __ATTR_WO(signal
);
427 static struct kobj_attribute force_kobj_attr
= __ATTR_WO(force
);
428 static struct attribute
*klp_patch_attrs
[] = {
429 &enabled_kobj_attr
.attr
,
430 &transition_kobj_attr
.attr
,
431 &signal_kobj_attr
.attr
,
432 &force_kobj_attr
.attr
,
436 static void klp_kobj_release_patch(struct kobject
*kobj
)
438 struct klp_patch
*patch
;
440 patch
= container_of(kobj
, struct klp_patch
, kobj
);
441 complete(&patch
->finish
);
444 static struct kobj_type klp_ktype_patch
= {
445 .release
= klp_kobj_release_patch
,
446 .sysfs_ops
= &kobj_sysfs_ops
,
447 .default_attrs
= klp_patch_attrs
,
450 static void klp_kobj_release_object(struct kobject
*kobj
)
454 static struct kobj_type klp_ktype_object
= {
455 .release
= klp_kobj_release_object
,
456 .sysfs_ops
= &kobj_sysfs_ops
,
459 static void klp_kobj_release_func(struct kobject
*kobj
)
463 static struct kobj_type klp_ktype_func
= {
464 .release
= klp_kobj_release_func
,
465 .sysfs_ops
= &kobj_sysfs_ops
,
468 static void klp_free_funcs(struct klp_object
*obj
)
470 struct klp_func
*func
;
472 klp_for_each_func(obj
, func
) {
473 /* Might be called from klp_init_patch() error path. */
474 if (func
->kobj_added
)
475 kobject_put(&func
->kobj
);
479 /* Clean up when a patched object is unloaded */
480 static void klp_free_object_loaded(struct klp_object
*obj
)
482 struct klp_func
*func
;
486 klp_for_each_func(obj
, func
)
487 func
->old_func
= NULL
;
490 static void klp_free_objects(struct klp_patch
*patch
)
492 struct klp_object
*obj
;
494 klp_for_each_object(patch
, obj
) {
497 /* Might be called from klp_init_patch() error path. */
499 kobject_put(&obj
->kobj
);
504 * This function implements the free operations that can be called safely
507 * The operation must be completed by calling klp_free_patch_finish()
510 static void klp_free_patch_start(struct klp_patch
*patch
)
512 if (!list_empty(&patch
->list
))
513 list_del(&patch
->list
);
515 klp_free_objects(patch
);
519 * This function implements the free part that must be called outside
522 * It must be called after klp_free_patch_start(). And it has to be
523 * the last function accessing the livepatch structures when the patch
526 static void klp_free_patch_finish(struct klp_patch
*patch
)
529 * Avoid deadlock with enabled_store() sysfs callback by
530 * calling this outside klp_mutex. It is safe because
531 * this is called when the patch gets disabled and it
532 * cannot get enabled again.
534 if (patch
->kobj_added
) {
535 kobject_put(&patch
->kobj
);
536 wait_for_completion(&patch
->finish
);
540 static int klp_init_func(struct klp_object
*obj
, struct klp_func
*func
)
544 if (!func
->old_name
|| !func
->new_func
)
547 if (strlen(func
->old_name
) >= KSYM_NAME_LEN
)
550 INIT_LIST_HEAD(&func
->stack_node
);
551 func
->patched
= false;
552 func
->transition
= false;
554 /* The format for the sysfs directory is <function,sympos> where sympos
555 * is the nth occurrence of this symbol in kallsyms for the patched
556 * object. If the user selects 0 for old_sympos, then 1 will be used
557 * since a unique symbol will be the first occurrence.
559 ret
= kobject_init_and_add(&func
->kobj
, &klp_ktype_func
,
560 &obj
->kobj
, "%s,%lu", func
->old_name
,
561 func
->old_sympos
? func
->old_sympos
: 1);
563 func
->kobj_added
= true;
568 /* Arches may override this to finish any remaining arch-specific tasks */
569 void __weak
arch_klp_init_object_loaded(struct klp_patch
*patch
,
570 struct klp_object
*obj
)
574 /* parts of the initialization that is done only when the object is loaded */
575 static int klp_init_object_loaded(struct klp_patch
*patch
,
576 struct klp_object
*obj
)
578 struct klp_func
*func
;
581 module_disable_ro(patch
->mod
);
582 ret
= klp_write_object_relocations(patch
->mod
, obj
);
584 module_enable_ro(patch
->mod
, true);
588 arch_klp_init_object_loaded(patch
, obj
);
589 module_enable_ro(patch
->mod
, true);
591 klp_for_each_func(obj
, func
) {
592 ret
= klp_find_object_symbol(obj
->name
, func
->old_name
,
594 (unsigned long *)&func
->old_func
);
598 ret
= kallsyms_lookup_size_offset((unsigned long)func
->old_func
,
599 &func
->old_size
, NULL
);
601 pr_err("kallsyms size lookup failed for '%s'\n",
606 ret
= kallsyms_lookup_size_offset((unsigned long)func
->new_func
,
607 &func
->new_size
, NULL
);
609 pr_err("kallsyms size lookup failed for '%s' replacement\n",
618 static int klp_init_object(struct klp_patch
*patch
, struct klp_object
*obj
)
620 struct klp_func
*func
;
624 if (klp_is_module(obj
) && strlen(obj
->name
) >= MODULE_NAME_LEN
)
627 obj
->patched
= false;
630 klp_find_object_module(obj
);
632 name
= klp_is_module(obj
) ? obj
->name
: "vmlinux";
633 ret
= kobject_init_and_add(&obj
->kobj
, &klp_ktype_object
,
634 &patch
->kobj
, "%s", name
);
637 obj
->kobj_added
= true;
639 klp_for_each_func(obj
, func
) {
640 ret
= klp_init_func(obj
, func
);
645 if (klp_is_object_loaded(obj
))
646 ret
= klp_init_object_loaded(patch
, obj
);
651 static int klp_init_patch_early(struct klp_patch
*patch
)
653 struct klp_object
*obj
;
654 struct klp_func
*func
;
659 INIT_LIST_HEAD(&patch
->list
);
660 patch
->kobj_added
= false;
661 patch
->enabled
= false;
662 init_completion(&patch
->finish
);
664 klp_for_each_object(patch
, obj
) {
668 obj
->kobj_added
= false;
670 klp_for_each_func(obj
, func
)
671 func
->kobj_added
= false;
677 static int klp_init_patch(struct klp_patch
*patch
)
679 struct klp_object
*obj
;
682 mutex_lock(&klp_mutex
);
684 ret
= klp_init_patch_early(patch
);
686 mutex_unlock(&klp_mutex
);
690 ret
= kobject_init_and_add(&patch
->kobj
, &klp_ktype_patch
,
691 klp_root_kobj
, "%s", patch
->mod
->name
);
693 mutex_unlock(&klp_mutex
);
696 patch
->kobj_added
= true;
698 klp_for_each_object(patch
, obj
) {
699 ret
= klp_init_object(patch
, obj
);
704 list_add_tail(&patch
->list
, &klp_patches
);
706 mutex_unlock(&klp_mutex
);
711 klp_free_patch_start(patch
);
713 mutex_unlock(&klp_mutex
);
715 klp_free_patch_finish(patch
);
721 * klp_unregister_patch() - unregisters a patch
722 * @patch: Disabled patch to be unregistered
724 * Frees the data structures and removes the sysfs interface.
726 * Return: 0 on success, otherwise error
728 int klp_unregister_patch(struct klp_patch
*patch
)
732 mutex_lock(&klp_mutex
);
734 if (!klp_is_patch_registered(patch
)) {
739 if (patch
->enabled
) {
744 klp_free_patch_start(patch
);
746 mutex_unlock(&klp_mutex
);
748 klp_free_patch_finish(patch
);
752 mutex_unlock(&klp_mutex
);
755 EXPORT_SYMBOL_GPL(klp_unregister_patch
);
758 * klp_register_patch() - registers a patch
759 * @patch: Patch to be registered
761 * Initializes the data structure associated with the patch and
762 * creates the sysfs interface.
764 * There is no need to take the reference on the patch module here. It is done
765 * later when the patch is enabled.
767 * Return: 0 on success, otherwise error
769 int klp_register_patch(struct klp_patch
*patch
)
771 if (!patch
|| !patch
->mod
)
774 if (!is_livepatch_module(patch
->mod
)) {
775 pr_err("module %s is not marked as a livepatch module\n",
780 if (!klp_initialized())
783 if (!klp_have_reliable_stack()) {
784 pr_err("This architecture doesn't have support for the livepatch consistency model.\n");
788 return klp_init_patch(patch
);
790 EXPORT_SYMBOL_GPL(klp_register_patch
);
792 static int __klp_disable_patch(struct klp_patch
*patch
)
794 struct klp_object
*obj
;
796 if (WARN_ON(!patch
->enabled
))
799 if (klp_transition_patch
)
802 /* enforce stacking: only the last enabled patch can be disabled */
803 if (!list_is_last(&patch
->list
, &klp_patches
) &&
804 list_next_entry(patch
, list
)->enabled
)
807 klp_init_transition(patch
, KLP_UNPATCHED
);
809 klp_for_each_object(patch
, obj
)
811 klp_pre_unpatch_callback(obj
);
814 * Enforce the order of the func->transition writes in
815 * klp_init_transition() and the TIF_PATCH_PENDING writes in
816 * klp_start_transition(). In the rare case where klp_ftrace_handler()
817 * is called shortly after klp_update_patch_state() switches the task,
818 * this ensures the handler sees that func->transition is set.
822 klp_start_transition();
823 klp_try_complete_transition();
824 patch
->enabled
= false;
830 * klp_disable_patch() - disables a registered patch
831 * @patch: The registered, enabled patch to be disabled
833 * Unregisters the patched functions from ftrace.
835 * Return: 0 on success, otherwise error
837 int klp_disable_patch(struct klp_patch
*patch
)
841 mutex_lock(&klp_mutex
);
843 if (!klp_is_patch_registered(patch
)) {
848 if (!patch
->enabled
) {
853 ret
= __klp_disable_patch(patch
);
856 mutex_unlock(&klp_mutex
);
859 EXPORT_SYMBOL_GPL(klp_disable_patch
);
861 static int __klp_enable_patch(struct klp_patch
*patch
)
863 struct klp_object
*obj
;
866 if (klp_transition_patch
)
869 if (WARN_ON(patch
->enabled
))
872 /* enforce stacking: only the first disabled patch can be enabled */
873 if (patch
->list
.prev
!= &klp_patches
&&
874 !list_prev_entry(patch
, list
)->enabled
)
878 * A reference is taken on the patch module to prevent it from being
881 if (!try_module_get(patch
->mod
))
884 pr_notice("enabling patch '%s'\n", patch
->mod
->name
);
886 klp_init_transition(patch
, KLP_PATCHED
);
889 * Enforce the order of the func->transition writes in
890 * klp_init_transition() and the ops->func_stack writes in
891 * klp_patch_object(), so that klp_ftrace_handler() will see the
892 * func->transition updates before the handler is registered and the
893 * new funcs become visible to the handler.
897 klp_for_each_object(patch
, obj
) {
898 if (!klp_is_object_loaded(obj
))
901 ret
= klp_pre_patch_callback(obj
);
903 pr_warn("pre-patch callback failed for object '%s'\n",
904 klp_is_module(obj
) ? obj
->name
: "vmlinux");
908 ret
= klp_patch_object(obj
);
910 pr_warn("failed to patch object '%s'\n",
911 klp_is_module(obj
) ? obj
->name
: "vmlinux");
916 klp_start_transition();
917 klp_try_complete_transition();
918 patch
->enabled
= true;
922 pr_warn("failed to enable patch '%s'\n", patch
->mod
->name
);
924 klp_cancel_transition();
929 * klp_enable_patch() - enables a registered patch
930 * @patch: The registered, disabled patch to be enabled
932 * Performs the needed symbol lookups and code relocations,
933 * then registers the patched functions with ftrace.
935 * Return: 0 on success, otherwise error
937 int klp_enable_patch(struct klp_patch
*patch
)
941 mutex_lock(&klp_mutex
);
943 if (!klp_is_patch_registered(patch
)) {
948 ret
= __klp_enable_patch(patch
);
951 mutex_unlock(&klp_mutex
);
954 EXPORT_SYMBOL_GPL(klp_enable_patch
);
957 * Remove parts of patches that touch a given kernel module. The list of
958 * patches processed might be limited. When limit is NULL, all patches
961 static void klp_cleanup_module_patches_limited(struct module
*mod
,
962 struct klp_patch
*limit
)
964 struct klp_patch
*patch
;
965 struct klp_object
*obj
;
967 list_for_each_entry(patch
, &klp_patches
, list
) {
971 klp_for_each_object(patch
, obj
) {
972 if (!klp_is_module(obj
) || strcmp(obj
->name
, mod
->name
))
976 * Only unpatch the module if the patch is enabled or
979 if (patch
->enabled
|| patch
== klp_transition_patch
) {
981 if (patch
!= klp_transition_patch
)
982 klp_pre_unpatch_callback(obj
);
984 pr_notice("reverting patch '%s' on unloading module '%s'\n",
985 patch
->mod
->name
, obj
->mod
->name
);
986 klp_unpatch_object(obj
);
988 klp_post_unpatch_callback(obj
);
991 klp_free_object_loaded(obj
);
997 int klp_module_coming(struct module
*mod
)
1000 struct klp_patch
*patch
;
1001 struct klp_object
*obj
;
1003 if (WARN_ON(mod
->state
!= MODULE_STATE_COMING
))
1006 mutex_lock(&klp_mutex
);
1008 * Each module has to know that klp_module_coming()
1009 * has been called. We never know what module will
1010 * get patched by a new patch.
1012 mod
->klp_alive
= true;
1014 list_for_each_entry(patch
, &klp_patches
, list
) {
1015 klp_for_each_object(patch
, obj
) {
1016 if (!klp_is_module(obj
) || strcmp(obj
->name
, mod
->name
))
1021 ret
= klp_init_object_loaded(patch
, obj
);
1023 pr_warn("failed to initialize patch '%s' for module '%s' (%d)\n",
1024 patch
->mod
->name
, obj
->mod
->name
, ret
);
1029 * Only patch the module if the patch is enabled or is
1032 if (!patch
->enabled
&& patch
!= klp_transition_patch
)
1035 pr_notice("applying patch '%s' to loading module '%s'\n",
1036 patch
->mod
->name
, obj
->mod
->name
);
1038 ret
= klp_pre_patch_callback(obj
);
1040 pr_warn("pre-patch callback failed for object '%s'\n",
1045 ret
= klp_patch_object(obj
);
1047 pr_warn("failed to apply patch '%s' to module '%s' (%d)\n",
1048 patch
->mod
->name
, obj
->mod
->name
, ret
);
1050 klp_post_unpatch_callback(obj
);
1054 if (patch
!= klp_transition_patch
)
1055 klp_post_patch_callback(obj
);
1061 mutex_unlock(&klp_mutex
);
1067 * If a patch is unsuccessfully applied, return
1068 * error to the module loader.
1070 pr_warn("patch '%s' failed for module '%s', refusing to load module '%s'\n",
1071 patch
->mod
->name
, obj
->mod
->name
, obj
->mod
->name
);
1072 mod
->klp_alive
= false;
1073 klp_cleanup_module_patches_limited(mod
, patch
);
1074 mutex_unlock(&klp_mutex
);
1079 void klp_module_going(struct module
*mod
)
1081 if (WARN_ON(mod
->state
!= MODULE_STATE_GOING
&&
1082 mod
->state
!= MODULE_STATE_COMING
))
1085 mutex_lock(&klp_mutex
);
1087 * Each module has to know that klp_module_going()
1088 * has been called. We never know what module will
1089 * get patched by a new patch.
1091 mod
->klp_alive
= false;
1093 klp_cleanup_module_patches_limited(mod
, NULL
);
1095 mutex_unlock(&klp_mutex
);
1098 static int __init
klp_init(void)
1102 ret
= klp_check_compiler_support();
1104 pr_info("Your compiler is too old; turning off.\n");
1108 klp_root_kobj
= kobject_create_and_add("livepatch", kernel_kobj
);
1115 module_init(klp_init
);