Skip to content

Commit 0f49fc9

Browse files
committed
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
2 parents 49dc2b7 + 7e545d6 commit 0f49fc9

File tree

6 files changed

+113
-96
lines changed

6 files changed

+113
-96
lines changed

MAINTAINERS

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6596,9 +6596,10 @@ F: drivers/platform/x86/hp_accel.c
65966596

65976597
LIVE PATCHING
65986598
M: Josh Poimboeuf <[email protected]>
6599-
M: Seth Jennings <sjenning@redhat.com>
6599+
M: Jessica Yu <jeyu@redhat.com>
66006600
M: Jiri Kosina <[email protected]>
6601-
M: Vojtech Pavlik <[email protected]>
6601+
M: Miroslav Benes <[email protected]>
6602+
R: Petr Mladek <[email protected]>
66026603
S: Maintained
66036604
F: kernel/livepatch/
66046605
F: include/linux/livepatch.h

arch/s390/include/asm/livepatch.h

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919

2020
#include <linux/module.h>
2121

22-
#ifdef CONFIG_LIVEPATCH
2322
static inline int klp_check_compiler_support(void)
2423
{
2524
return 0;
@@ -36,8 +35,5 @@ static inline void klp_arch_set_pc(struct pt_regs *regs, unsigned long ip)
3635
{
3736
regs->psw.addr = ip;
3837
}
39-
#else
40-
#error Include linux/livepatch.h, not asm/livepatch.h
41-
#endif
4238

4339
#endif

arch/x86/include/asm/livepatch.h

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525
#include <linux/module.h>
2626
#include <linux/ftrace.h>
2727

28-
#ifdef CONFIG_LIVEPATCH
2928
static inline int klp_check_compiler_support(void)
3029
{
3130
#ifndef CC_USING_FENTRY
@@ -40,8 +39,5 @@ static inline void klp_arch_set_pc(struct pt_regs *regs, unsigned long ip)
4039
{
4140
regs->ip = ip;
4241
}
43-
#else
44-
#error Include linux/livepatch.h, not asm/livepatch.h
45-
#endif
4642

4743
#endif /* _ASM_X86_LIVEPATCH_H */

include/linux/livepatch.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,15 @@ int klp_unregister_patch(struct klp_patch *);
134134
int klp_enable_patch(struct klp_patch *);
135135
int klp_disable_patch(struct klp_patch *);
136136

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+
137146
#endif /* CONFIG_LIVEPATCH */
138147

139148
#endif /* _LINUX_LIVEPATCH_H_ */

kernel/livepatch/core.c

Lines changed: 73 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -99,12 +99,12 @@ static void klp_find_object_module(struct klp_object *obj)
9999
/*
100100
* We do not want to block removal of patched modules and therefore
101101
* we do not take a reference here. The patches are removed by
102-
* a going module handler instead.
102+
* klp_module_going() instead.
103103
*/
104104
mod = find_module(obj->name);
105105
/*
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()
108108
* is called. Module functions can be called even in the GOING state
109109
* until mod->exit() finishes. This is especially important for
110110
* patches that modify semantic of the functions.
@@ -190,8 +190,8 @@ static int klp_find_object_symbol(const char *objname, const char *name,
190190
if (args.addr == 0)
191191
pr_err("symbol '%s' not found in symbol table\n", name);
192192
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);
195195
} else if (sympos != args.count && sympos > 0) {
196196
pr_err("symbol position %lu for symbol '%s' in object '%s' not found\n",
197197
sympos, name, objname ? objname : "vmlinux");
@@ -866,103 +866,108 @@ int klp_register_patch(struct klp_patch *patch)
866866
}
867867
EXPORT_SYMBOL_GPL(klp_register_patch);
868868

869-
static int klp_module_notify_coming(struct klp_patch *patch,
870-
struct klp_object *obj)
869+
int klp_module_coming(struct module *mod)
871870
{
872-
struct module *pmod = patch->mod;
873-
struct module *mod = obj->mod;
874871
int ret;
872+
struct klp_patch *patch;
873+
struct klp_object *obj;
875874

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-
}
875+
if (WARN_ON(mod->state != MODULE_STATE_COMING))
876+
return -EINVAL;
882877

883-
if (patch->state == KLP_DISABLED)
884-
return 0;
878+
mutex_lock(&klp_mutex);
879+
/*
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.
883+
*/
884+
mod->klp_alive = true;
885885

886-
pr_notice("applying patch '%s' to loading module '%s'\n",
887-
pmod->name, mod->name);
886+
list_for_each_entry(patch, &klp_patches, list) {
887+
klp_for_each_object(patch, obj) {
888+
if (!klp_is_module(obj) || strcmp(obj->name, mod->name))
889+
continue;
888890

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-
}
891+
obj->mod = mod;
895892

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;
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+
}
901899

902-
if (patch->state == KLP_DISABLED)
903-
goto disabled;
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+
}
912+
913+
break;
914+
}
915+
}
904916

905-
pr_notice("reverting patch '%s' on unloading module '%s'\n",
906-
pmod->name, mod->name);
917+
mutex_unlock(&klp_mutex);
907918

908-
klp_disable_object(obj);
919+
return 0;
909920

910-
disabled:
921+
err:
922+
/*
923+
* If a patch is unsuccessfully applied, return
924+
* error to the module loader.
925+
*/
926+
pr_warn("patch '%s' failed for module '%s', refusing to load module '%s'\n",
927+
patch->mod->name, obj->mod->name, obj->mod->name);
928+
mod->klp_alive = false;
911929
klp_free_object_loaded(obj);
930+
mutex_unlock(&klp_mutex);
931+
932+
return ret;
912933
}
913934

914-
static int klp_module_notify(struct notifier_block *nb, unsigned long action,
915-
void *data)
935+
void klp_module_going(struct module *mod)
916936
{
917-
int ret;
918-
struct module *mod = data;
919937
struct klp_patch *patch;
920938
struct klp_object *obj;
921939

922-
if (action != MODULE_STATE_COMING && action != MODULE_STATE_GOING)
923-
return 0;
940+
if (WARN_ON(mod->state != MODULE_STATE_GOING &&
941+
mod->state != MODULE_STATE_COMING))
942+
return;
924943

925944
mutex_lock(&klp_mutex);
926-
927945
/*
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.
946+
* Each module has to know that klp_module_going()
947+
* has been called. We never know what module will
948+
* get patched by a new patch.
930949
*/
931-
if (action == MODULE_STATE_COMING)
932-
mod->klp_alive = true;
933-
else /* MODULE_STATE_GOING */
934-
mod->klp_alive = false;
950+
mod->klp_alive = false;
935951

936952
list_for_each_entry(patch, &klp_patches, list) {
937953
klp_for_each_object(patch, obj) {
938954
if (!klp_is_module(obj) || strcmp(obj->name, mod->name))
939955
continue;
940956

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);
957+
if (patch->state != KLP_DISABLED) {
958+
pr_notice("reverting patch '%s' on unloading module '%s'\n",
959+
patch->mod->name, obj->mod->name);
960+
klp_disable_object(obj);
961+
}
951962

963+
klp_free_object_loaded(obj);
952964
break;
953965
}
954966
}
955967

956968
mutex_unlock(&klp_mutex);
957-
958-
return 0;
959969
}
960970

961-
static struct notifier_block klp_module_nb = {
962-
.notifier_call = klp_module_notify,
963-
.priority = INT_MIN+1, /* called late but before ftrace notifier */
964-
};
965-
966971
static int __init klp_init(void)
967972
{
968973
int ret;
@@ -973,21 +978,11 @@ static int __init klp_init(void)
973978
return -EINVAL;
974979
}
975980

976-
ret = register_module_notifier(&klp_module_nb);
977-
if (ret)
978-
return ret;
979-
980981
klp_root_kobj = kobject_create_and_add("livepatch", kernel_kobj);
981-
if (!klp_root_kobj) {
982-
ret = -ENOMEM;
983-
goto unregister;
984-
}
982+
if (!klp_root_kobj)
983+
return -ENOMEM;
985984

986985
return 0;
987-
988-
unregister:
989-
unregister_module_notifier(&klp_module_nb);
990-
return ret;
991986
}
992987

993988
module_init(klp_init);

kernel/module.c

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@
5353
#include <asm/sections.h>
5454
#include <linux/tracepoint.h>
5555
#include <linux/ftrace.h>
56+
#include <linux/livepatch.h>
5657
#include <linux/async.h>
5758
#include <linux/percpu.h>
5859
#include <linux/kmemleak.h>
@@ -984,6 +985,7 @@ SYSCALL_DEFINE2(delete_module, const char __user *, name_user,
984985
mod->exit();
985986
blocking_notifier_call_chain(&module_notify_list,
986987
MODULE_STATE_GOING, mod);
988+
klp_module_going(mod);
987989
ftrace_release_mod(mod);
988990

989991
async_synchronize_full();
@@ -3258,6 +3260,7 @@ static noinline int do_init_module(struct module *mod)
32583260
module_put(mod);
32593261
blocking_notifier_call_chain(&module_notify_list,
32603262
MODULE_STATE_GOING, mod);
3263+
klp_module_going(mod);
32613264
ftrace_release_mod(mod);
32623265
free_module(mod);
32633266
wake_up_all(&module_wq);
@@ -3335,16 +3338,27 @@ static int complete_formation(struct module *mod, struct load_info *info)
33353338
mod->state = MODULE_STATE_COMING;
33363339
mutex_unlock(&module_mutex);
33373340

3338-
ftrace_module_enable(mod);
3339-
blocking_notifier_call_chain(&module_notify_list,
3340-
MODULE_STATE_COMING, mod);
33413341
return 0;
33423342

33433343
out:
33443344
mutex_unlock(&module_mutex);
33453345
return err;
33463346
}
33473347

3348+
static int prepare_coming_module(struct module *mod)
3349+
{
3350+
int err;
3351+
3352+
ftrace_module_enable(mod);
3353+
err = klp_module_coming(mod);
3354+
if (err)
3355+
return err;
3356+
3357+
blocking_notifier_call_chain(&module_notify_list,
3358+
MODULE_STATE_COMING, mod);
3359+
return 0;
3360+
}
3361+
33483362
static int unknown_module_param_cb(char *param, char *val, const char *modname,
33493363
void *arg)
33503364
{
@@ -3459,13 +3473,17 @@ static int load_module(struct load_info *info, const char __user *uargs,
34593473
if (err)
34603474
goto ddebug_cleanup;
34613475

3476+
err = prepare_coming_module(mod);
3477+
if (err)
3478+
goto bug_cleanup;
3479+
34623480
/* Module is ready to execute: parsing args may do that. */
34633481
after_dashes = parse_args(mod->name, mod->args, mod->kp, mod->num_kp,
34643482
-32768, 32767, mod,
34653483
unknown_module_param_cb);
34663484
if (IS_ERR(after_dashes)) {
34673485
err = PTR_ERR(after_dashes);
3468-
goto bug_cleanup;
3486+
goto coming_cleanup;
34693487
} else if (after_dashes) {
34703488
pr_warn("%s: parameters '%s' after `--' ignored\n",
34713489
mod->name, after_dashes);
@@ -3474,7 +3492,7 @@ static int load_module(struct load_info *info, const char __user *uargs,
34743492
/* Link in to syfs. */
34753493
err = mod_sysfs_setup(mod, info, mod->kp, mod->num_kp);
34763494
if (err < 0)
3477-
goto bug_cleanup;
3495+
goto coming_cleanup;
34783496

34793497
/* Get rid of temporary copy. */
34803498
free_copy(info);
@@ -3484,15 +3502,17 @@ static int load_module(struct load_info *info, const char __user *uargs,
34843502

34853503
return do_init_module(mod);
34863504

3505+
coming_cleanup:
3506+
blocking_notifier_call_chain(&module_notify_list,
3507+
MODULE_STATE_GOING, mod);
3508+
klp_module_going(mod);
3509+
34873510
bug_cleanup:
34883511
/* module_bug_cleanup needs module_mutex protection */
34893512
mutex_lock(&module_mutex);
34903513
module_bug_cleanup(mod);
34913514
mutex_unlock(&module_mutex);
34923515

3493-
blocking_notifier_call_chain(&module_notify_list,
3494-
MODULE_STATE_GOING, mod);
3495-
34963516
/* we can't deallocate the module until we clear memory protection */
34973517
module_disable_ro(mod);
34983518
module_disable_nx(mod);

0 commit comments

Comments
 (0)