Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

Merge branch 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/jikos/livepatching

Pull livepatching update from Jiri Kosina:

- cleanup of module notifiers; this depends on a module.c cleanup which
has been acked by Rusty; from Jessica Yu

- small assorted fixes and MAINTAINERS update

* 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/jikos/livepatching:
livepatch/module: remove livepatch module notifier
modules: split part of complete_formation() into prepare_coming_module()
livepatch: Update maintainers
livepatch: Fix the error message about unresolvable ambiguity
klp: remove CONFIG_LIVEPATCH dependency from klp headers
klp: remove superfluous errors in asm/livepatch.h

+123 -106
+3 -2
MAINTAINERS
··· 6596 6596 6597 6597 LIVE PATCHING 6598 6598 M: Josh Poimboeuf <jpoimboe@redhat.com> 6599 - M: Seth Jennings <sjenning@redhat.com> 6599 + M: Jessica Yu <jeyu@redhat.com> 6600 6600 M: Jiri Kosina <jikos@kernel.org> 6601 - M: Vojtech Pavlik <vojtech@suse.com> 6601 + M: Miroslav Benes <mbenes@suse.cz> 6602 + R: Petr Mladek <pmladek@suse.com> 6602 6603 S: Maintained 6603 6604 F: kernel/livepatch/ 6604 6605 F: include/linux/livepatch.h
-4
arch/s390/include/asm/livepatch.h
··· 19 19 20 20 #include <linux/module.h> 21 21 22 - #ifdef CONFIG_LIVEPATCH 23 22 static inline int klp_check_compiler_support(void) 24 23 { 25 24 return 0; ··· 35 36 { 36 37 regs->psw.addr = ip; 37 38 } 38 - #else 39 - #error Include linux/livepatch.h, not asm/livepatch.h 40 - #endif 41 39 42 40 #endif
-4
arch/x86/include/asm/livepatch.h
··· 25 25 #include <linux/module.h> 26 26 #include <linux/ftrace.h> 27 27 28 - #ifdef CONFIG_LIVEPATCH 29 28 static inline int klp_check_compiler_support(void) 30 29 { 31 30 #ifndef CC_USING_FENTRY ··· 39 40 { 40 41 regs->ip = ip; 41 42 } 42 - #else 43 - #error Include linux/livepatch.h, not asm/livepatch.h 44 - #endif 45 43 46 44 #endif /* _ASM_X86_LIVEPATCH_H */
+9
include/linux/livepatch.h
··· 134 134 int klp_enable_patch(struct klp_patch *); 135 135 int klp_disable_patch(struct klp_patch *); 136 136 137 + /* Called from the module loader during module coming/going states */ 138 + int klp_module_coming(struct module *mod); 139 + void klp_module_going(struct module *mod); 140 + 141 + #else /* !CONFIG_LIVEPATCH */ 142 + 143 + static inline int klp_module_coming(struct module *mod) { return 0; } 144 + static inline void klp_module_going(struct module *mod) { } 145 + 137 146 #endif /* CONFIG_LIVEPATCH */ 138 147 139 148 #endif /* _LINUX_LIVEPATCH_H_ */
+83 -88
kernel/livepatch/core.c
··· 99 99 /* 100 100 * We do not want to block removal of patched modules and therefore 101 101 * we do not take a reference here. The patches are removed by 102 - * a going module handler instead. 102 + * klp_module_going() instead. 103 103 */ 104 104 mod = find_module(obj->name); 105 105 /* 106 - * Do not mess work of the module coming and going notifiers. 107 - * Note that the patch might still be needed before the going handler 106 + * Do not mess work of klp_module_coming() and klp_module_going(). 107 + * Note that the patch might still be needed before klp_module_going() 108 108 * is called. Module functions can be called even in the GOING state 109 109 * until mod->exit() finishes. This is especially important for 110 110 * patches that modify semantic of the functions. ··· 190 190 if (args.addr == 0) 191 191 pr_err("symbol '%s' not found in symbol table\n", name); 192 192 else if (args.count > 1 && sympos == 0) { 193 - pr_err("unresolvable ambiguity (%lu matches) on symbol '%s' in object '%s'\n", 194 - args.count, name, objname); 193 + pr_err("unresolvable ambiguity for symbol '%s' in object '%s'\n", 194 + name, objname); 195 195 } else if (sympos != args.count && sympos > 0) { 196 196 pr_err("symbol position %lu for symbol '%s' in object '%s' not found\n", 197 197 sympos, name, objname ? objname : "vmlinux"); ··· 866 866 } 867 867 EXPORT_SYMBOL_GPL(klp_register_patch); 868 868 869 - static int klp_module_notify_coming(struct klp_patch *patch, 870 - struct klp_object *obj) 871 - { 872 - struct module *pmod = patch->mod; 873 - struct module *mod = obj->mod; 874 - int ret; 875 - 876 - ret = klp_init_object_loaded(patch, obj); 877 - if (ret) { 878 - pr_warn("failed to initialize patch '%s' for module '%s' (%d)\n", 879 - pmod->name, mod->name, ret); 880 - return ret; 881 - } 882 - 883 - if (patch->state == KLP_DISABLED) 884 - return 0; 885 - 886 - pr_notice("applying patch '%s' to loading module '%s'\n", 887 - pmod->name, mod->name); 888 - 889 - ret = klp_enable_object(obj); 890 - if (ret) 891 - pr_warn("failed to apply patch '%s' to module '%s' (%d)\n", 892 - pmod->name, mod->name, ret); 893 - return ret; 894 - } 895 - 896 - static void klp_module_notify_going(struct klp_patch *patch, 897 - struct klp_object *obj) 898 - { 899 - struct module *pmod = patch->mod; 900 - struct module *mod = obj->mod; 901 - 902 - if (patch->state == KLP_DISABLED) 903 - goto disabled; 904 - 905 - pr_notice("reverting patch '%s' on unloading module '%s'\n", 906 - pmod->name, mod->name); 907 - 908 - klp_disable_object(obj); 909 - 910 - disabled: 911 - klp_free_object_loaded(obj); 912 - } 913 - 914 - static int klp_module_notify(struct notifier_block *nb, unsigned long action, 915 - void *data) 869 + int klp_module_coming(struct module *mod) 916 870 { 917 871 int ret; 918 - struct module *mod = data; 919 872 struct klp_patch *patch; 920 873 struct klp_object *obj; 921 874 922 - if (action != MODULE_STATE_COMING && action != MODULE_STATE_GOING) 923 - return 0; 875 + if (WARN_ON(mod->state != MODULE_STATE_COMING)) 876 + return -EINVAL; 924 877 925 878 mutex_lock(&klp_mutex); 926 - 927 879 /* 928 - * Each module has to know that the notifier has been called. 929 - * We never know what module will get patched by a new patch. 880 + * Each module has to know that klp_module_coming() 881 + * has been called. We never know what module will 882 + * get patched by a new patch. 930 883 */ 931 - if (action == MODULE_STATE_COMING) 932 - mod->klp_alive = true; 933 - else /* MODULE_STATE_GOING */ 934 - mod->klp_alive = false; 884 + mod->klp_alive = true; 935 885 936 886 list_for_each_entry(patch, &klp_patches, list) { 937 887 klp_for_each_object(patch, obj) { 938 888 if (!klp_is_module(obj) || strcmp(obj->name, mod->name)) 939 889 continue; 940 890 941 - if (action == MODULE_STATE_COMING) { 942 - obj->mod = mod; 943 - ret = klp_module_notify_coming(patch, obj); 944 - if (ret) { 945 - obj->mod = NULL; 946 - pr_warn("patch '%s' is in an inconsistent state!\n", 947 - patch->mod->name); 948 - } 949 - } else /* MODULE_STATE_GOING */ 950 - klp_module_notify_going(patch, obj); 891 + obj->mod = mod; 892 + 893 + ret = klp_init_object_loaded(patch, obj); 894 + if (ret) { 895 + pr_warn("failed to initialize patch '%s' for module '%s' (%d)\n", 896 + patch->mod->name, obj->mod->name, ret); 897 + goto err; 898 + } 899 + 900 + if (patch->state == KLP_DISABLED) 901 + break; 902 + 903 + pr_notice("applying patch '%s' to loading module '%s'\n", 904 + patch->mod->name, obj->mod->name); 905 + 906 + ret = klp_enable_object(obj); 907 + if (ret) { 908 + pr_warn("failed to apply patch '%s' to module '%s' (%d)\n", 909 + patch->mod->name, obj->mod->name, ret); 910 + goto err; 911 + } 951 912 952 913 break; 953 914 } ··· 917 956 mutex_unlock(&klp_mutex); 918 957 919 958 return 0; 959 + 960 + err: 961 + /* 962 + * If a patch is unsuccessfully applied, return 963 + * error to the module loader. 964 + */ 965 + pr_warn("patch '%s' failed for module '%s', refusing to load module '%s'\n", 966 + patch->mod->name, obj->mod->name, obj->mod->name); 967 + mod->klp_alive = false; 968 + klp_free_object_loaded(obj); 969 + mutex_unlock(&klp_mutex); 970 + 971 + return ret; 920 972 } 921 973 922 - static struct notifier_block klp_module_nb = { 923 - .notifier_call = klp_module_notify, 924 - .priority = INT_MIN+1, /* called late but before ftrace notifier */ 925 - }; 974 + void klp_module_going(struct module *mod) 975 + { 976 + struct klp_patch *patch; 977 + struct klp_object *obj; 978 + 979 + if (WARN_ON(mod->state != MODULE_STATE_GOING && 980 + mod->state != MODULE_STATE_COMING)) 981 + return; 982 + 983 + mutex_lock(&klp_mutex); 984 + /* 985 + * Each module has to know that klp_module_going() 986 + * has been called. We never know what module will 987 + * get patched by a new patch. 988 + */ 989 + mod->klp_alive = false; 990 + 991 + list_for_each_entry(patch, &klp_patches, list) { 992 + klp_for_each_object(patch, obj) { 993 + if (!klp_is_module(obj) || strcmp(obj->name, mod->name)) 994 + continue; 995 + 996 + if (patch->state != KLP_DISABLED) { 997 + pr_notice("reverting patch '%s' on unloading module '%s'\n", 998 + patch->mod->name, obj->mod->name); 999 + klp_disable_object(obj); 1000 + } 1001 + 1002 + klp_free_object_loaded(obj); 1003 + break; 1004 + } 1005 + } 1006 + 1007 + mutex_unlock(&klp_mutex); 1008 + } 926 1009 927 1010 static int __init klp_init(void) 928 1011 { ··· 978 973 return -EINVAL; 979 974 } 980 975 981 - ret = register_module_notifier(&klp_module_nb); 982 - if (ret) 983 - return ret; 984 - 985 976 klp_root_kobj = kobject_create_and_add("livepatch", kernel_kobj); 986 - if (!klp_root_kobj) { 987 - ret = -ENOMEM; 988 - goto unregister; 989 - } 977 + if (!klp_root_kobj) 978 + return -ENOMEM; 990 979 991 980 return 0; 992 - 993 - unregister: 994 - unregister_module_notifier(&klp_module_nb); 995 - return ret; 996 981 } 997 982 998 983 module_init(klp_init);
+28 -8
kernel/module.c
··· 53 53 #include <asm/sections.h> 54 54 #include <linux/tracepoint.h> 55 55 #include <linux/ftrace.h> 56 + #include <linux/livepatch.h> 56 57 #include <linux/async.h> 57 58 #include <linux/percpu.h> 58 59 #include <linux/kmemleak.h> ··· 985 984 mod->exit(); 986 985 blocking_notifier_call_chain(&module_notify_list, 987 986 MODULE_STATE_GOING, mod); 987 + klp_module_going(mod); 988 988 ftrace_release_mod(mod); 989 989 990 990 async_synchronize_full(); ··· 3260 3258 module_put(mod); 3261 3259 blocking_notifier_call_chain(&module_notify_list, 3262 3260 MODULE_STATE_GOING, mod); 3261 + klp_module_going(mod); 3263 3262 ftrace_release_mod(mod); 3264 3263 free_module(mod); 3265 3264 wake_up_all(&module_wq); ··· 3338 3335 mod->state = MODULE_STATE_COMING; 3339 3336 mutex_unlock(&module_mutex); 3340 3337 3341 - ftrace_module_enable(mod); 3342 - blocking_notifier_call_chain(&module_notify_list, 3343 - MODULE_STATE_COMING, mod); 3344 3338 return 0; 3345 3339 3346 3340 out: 3347 3341 mutex_unlock(&module_mutex); 3348 3342 return err; 3343 + } 3344 + 3345 + static int prepare_coming_module(struct module *mod) 3346 + { 3347 + int err; 3348 + 3349 + ftrace_module_enable(mod); 3350 + err = klp_module_coming(mod); 3351 + if (err) 3352 + return err; 3353 + 3354 + blocking_notifier_call_chain(&module_notify_list, 3355 + MODULE_STATE_COMING, mod); 3356 + return 0; 3349 3357 } 3350 3358 3351 3359 static int unknown_module_param_cb(char *param, char *val, const char *modname, ··· 3473 3459 if (err) 3474 3460 goto ddebug_cleanup; 3475 3461 3462 + err = prepare_coming_module(mod); 3463 + if (err) 3464 + goto bug_cleanup; 3465 + 3476 3466 /* Module is ready to execute: parsing args may do that. */ 3477 3467 after_dashes = parse_args(mod->name, mod->args, mod->kp, mod->num_kp, 3478 3468 -32768, 32767, mod, 3479 3469 unknown_module_param_cb); 3480 3470 if (IS_ERR(after_dashes)) { 3481 3471 err = PTR_ERR(after_dashes); 3482 - goto bug_cleanup; 3472 + goto coming_cleanup; 3483 3473 } else if (after_dashes) { 3484 3474 pr_warn("%s: parameters '%s' after `--' ignored\n", 3485 3475 mod->name, after_dashes); ··· 3492 3474 /* Link in to syfs. */ 3493 3475 err = mod_sysfs_setup(mod, info, mod->kp, mod->num_kp); 3494 3476 if (err < 0) 3495 - goto bug_cleanup; 3477 + goto coming_cleanup; 3496 3478 3497 3479 /* Get rid of temporary copy. */ 3498 3480 free_copy(info); ··· 3502 3484 3503 3485 return do_init_module(mod); 3504 3486 3487 + coming_cleanup: 3488 + blocking_notifier_call_chain(&module_notify_list, 3489 + MODULE_STATE_GOING, mod); 3490 + klp_module_going(mod); 3491 + 3505 3492 bug_cleanup: 3506 3493 /* module_bug_cleanup needs module_mutex protection */ 3507 3494 mutex_lock(&module_mutex); 3508 3495 module_bug_cleanup(mod); 3509 3496 mutex_unlock(&module_mutex); 3510 - 3511 - blocking_notifier_call_chain(&module_notify_list, 3512 - MODULE_STATE_GOING, mod); 3513 3497 3514 3498 /* we can't deallocate the module until we clear memory protection */ 3515 3499 module_disable_ro(mod);