This pulls in an infrastructure/API that allows livepatch writers to register pre-patch and post-patch callbacks that allow for running a glue code necessary for finalizing the patching if necessary. Conflicts: kernel/livepatch/core.c - trivial conflict by adding a callback call into module going notifier vs. moving that code block to klp_cleanup_module_patches_limited()
Showing the changes to kernel/livepatch (4 of 10 files). Show all
Unified · Split
| @@ -54,11 +54,6 @@ static bool klp_is_module(struct klp_object *obj) · 53 unmodified lines | ||
| 54 | 54 | return obj->name; |
| 55 | 55 | } |
| 56 | 56 | |
| 57 | −static bool klp_is_object_loaded(struct klp_object *obj) | |
| 58 | −{ | |
| 59 | − return !obj->name || obj->mod; | |
| 60 | −} | |
| 61 | − | |
| 62 | 57 | /* sets obj->mod if object is not vmlinux and module is found */ |
| 63 | 58 | static void klp_find_object_module(struct klp_object *obj) |
| 64 | 59 | { |
| @@ -285,6 +280,11 @@ static int klp_write_object_relocations(struct module *pmod, · 220 unmodified lines | ||
| 285 | 280 | |
| 286 | 281 | static int __klp_disable_patch(struct klp_patch *patch) |
| 287 | 282 | { |
| 283 | + struct klp_object *obj; | |
| 284 | + | |
| 285 | + if (WARN_ON(!patch->enabled)) | |
| 286 | + return -EINVAL; | |
| 287 | + | |
| 288 | 288 | if (klp_transition_patch) |
| 289 | 289 | return -EBUSY; |
| 290 | 290 | |
| @@ -295,6 +295,10 @@ static int __klp_disable_patch(struct klp_patch *patch) · 4 unmodified lines | ||
| 295 | 295 | |
| 296 | 296 | klp_init_transition(patch, KLP_UNPATCHED); |
| 297 | 297 | |
| 298 | + klp_for_each_object(patch, obj) | |
| 299 | + if (obj->patched) | |
| 300 | + klp_pre_unpatch_callback(obj); | |
| 301 | + | |
| 298 | 302 | /* |
| 299 | 303 | * Enforce the order of the func->transition writes in |
| 300 | 304 | * klp_init_transition() and the TIF_PATCH_PENDING writes in |
| @@ -388,13 +392,18 @@ static int __klp_enable_patch(struct klp_patch *patch) · 87 unmodified lines | ||
| 388 | 392 | if (!klp_is_object_loaded(obj)) |
| 389 | 393 | continue; |
| 390 | 394 | |
| 391 | − ret = klp_patch_object(obj); | |
| 395 | + ret = klp_pre_patch_callback(obj); | |
| 392 | 396 | if (ret) { |
| 393 | − pr_warn("failed to enable patch '%s'\n", | |
| 394 | − patch->mod->name); | |
| 397 | + pr_warn("pre-patch callback failed for object '%s'\n", | |
| 398 | + klp_is_module(obj) ? obj->name : "vmlinux"); | |
| 399 | + goto err; | |
| 400 | + } | |
| 395 | 401 | |
| 396 | − klp_cancel_transition(); | |
| 397 | − return ret; | |
| 402 | + ret = klp_patch_object(obj); | |
| 403 | + if (ret) { | |
| 404 | + pr_warn("failed to patch object '%s'\n", | |
| 405 | + klp_is_module(obj) ? obj->name : "vmlinux"); | |
| 406 | + goto err; | |
| 398 | 407 | } |
| 399 | 408 | } |
| 400 | 409 | |
| @@ -403,6 +412,11 @@ static int __klp_enable_patch(struct klp_patch *patch) · 2 unmodified lines | ||
| 403 | 412 | patch->enabled = true; |
| 404 | 413 | |
| 405 | 414 | return 0; |
| 415 | +err: | |
| 416 | + pr_warn("failed to enable patch '%s'\n", patch->mod->name); | |
| 417 | + | |
| 418 | + klp_cancel_transition(); | |
| 419 | + return ret; | |
| 406 | 420 | } |
| 407 | 421 | |
| 408 | 422 | /** |
| @@ -854,9 +868,15 @@ static void klp_cleanup_module_patches_limited(struct module *mod, · 445 unmodified lines | ||
| 854 | 868 | * is in transition. |
| 855 | 869 | */ |
| 856 | 870 | if (patch->enabled || patch == klp_transition_patch) { |
| 871 | + | |
| 872 | + if (patch != klp_transition_patch) | |
| 873 | + klp_pre_unpatch_callback(obj); | |
| 874 | + | |
| 857 | 875 | pr_notice("reverting patch '%s' on unloading module '%s'\n", |
| 858 | 876 | patch->mod->name, obj->mod->name); |
| 859 | 877 | klp_unpatch_object(obj); |
| 878 | + | |
| 879 | + klp_post_unpatch_callback(obj); | |
| 860 | 880 | } |
| 861 | 881 | |
| 862 | 882 | klp_free_object_loaded(obj); |
| @@ -906,13 +926,25 @@ int klp_module_coming(struct module *mod) · 43 unmodified lines | ||
| 906 | 926 | pr_notice("applying patch '%s' to loading module '%s'\n", |
| 907 | 927 | patch->mod->name, obj->mod->name); |
| 908 | 928 | |
| 929 | + ret = klp_pre_patch_callback(obj); | |
| 930 | + if (ret) { | |
| 931 | + pr_warn("pre-patch callback failed for object '%s'\n", | |
| 932 | + obj->name); | |
| 933 | + goto err; | |
| 934 | + } | |
| 935 | + | |
| 909 | 936 | ret = klp_patch_object(obj); |
| 910 | 937 | if (ret) { |
| 911 | 938 | pr_warn("failed to apply patch '%s' to module '%s' (%d)\n", |
| 912 | 939 | patch->mod->name, obj->mod->name, ret); |
| 940 | + | |
| 941 | + klp_post_unpatch_callback(obj); | |
| 913 | 942 | goto err; |
| 914 | 943 | } |
| 915 | 944 | |
| 945 | + if (patch != klp_transition_patch) | |
| 946 | + klp_post_patch_callback(obj); | |
| 947 | + | |
| 916 | 948 | break; |
| 917 | 949 | } |
| 918 | 950 | } |
| @@ -1,6 +1,46 @@ | ||
| 1 | 1 | #ifndef _LIVEPATCH_CORE_H |
| 2 | 2 | #define _LIVEPATCH_CORE_H |
| 3 | 3 | |
| 4 | +#include <linux/livepatch.h> | |
| 5 | + | |
| 4 | 6 | extern struct mutex klp_mutex; |
| 5 | 7 | |
| 8 | +static inline bool klp_is_object_loaded(struct klp_object *obj) | |
| 9 | +{ | |
| 10 | + return !obj->name || obj->mod; | |
| 11 | +} | |
| 12 | + | |
| 13 | +static inline int klp_pre_patch_callback(struct klp_object *obj) | |
| 14 | +{ | |
| 15 | + int ret = 0; | |
| 16 | + | |
| 17 | + if (obj->callbacks.pre_patch) | |
| 18 | + ret = (*obj->callbacks.pre_patch)(obj); | |
| 19 | + | |
| 20 | + obj->callbacks.post_unpatch_enabled = !ret; | |
| 21 | + | |
| 22 | + return ret; | |
| 23 | +} | |
| 24 | + | |
| 25 | +static inline void klp_post_patch_callback(struct klp_object *obj) | |
| 26 | +{ | |
| 27 | + if (obj->callbacks.post_patch) | |
| 28 | + (*obj->callbacks.post_patch)(obj); | |
| 29 | +} | |
| 30 | + | |
| 31 | +static inline void klp_pre_unpatch_callback(struct klp_object *obj) | |
| 32 | +{ | |
| 33 | + if (obj->callbacks.pre_unpatch) | |
| 34 | + (*obj->callbacks.pre_unpatch)(obj); | |
| 35 | +} | |
| 36 | + | |
| 37 | +static inline void klp_post_unpatch_callback(struct klp_object *obj) | |
| 38 | +{ | |
| 39 | + if (obj->callbacks.post_unpatch_enabled && | |
| 40 | + obj->callbacks.post_unpatch) | |
| 41 | + (*obj->callbacks.post_unpatch)(obj); | |
| 42 | + | |
| 43 | + obj->callbacks.post_unpatch_enabled = false; | |
| 44 | +} | |
| 45 | + | |
| 6 | 46 | #endif /* _LIVEPATCH_CORE_H */ |
| @@ -28,6 +28,7 @@ · 27 unmodified lines | ||
| 28 | 28 | #include <linux/slab.h> |
| 29 | 29 | #include <linux/bug.h> |
| 30 | 30 | #include <linux/printk.h> |
| 31 | +#include "core.h" | |
| 31 | 32 | #include "patch.h" |
| 32 | 33 | #include "transition.h" |
| 33 | 34 | |
| @@ -82,6 +82,10 @@ static void klp_complete_transition(void) · 81 unmodified lines | ||
| 82 | 82 | unsigned int cpu; |
| 83 | 83 | bool immediate_func = false; |
| 84 | 84 | |
| 85 | + pr_debug("'%s': completing %s transition\n", | |
| 86 | + klp_transition_patch->mod->name, | |
| 87 | + klp_target_state == KLP_PATCHED ? "patching" : "unpatching"); | |
| 88 | + | |
| 85 | 89 | if (klp_target_state == KLP_UNPATCHED) { |
| 86 | 90 | /* |
| 87 | 91 | * All tasks have transitioned to KLP_UNPATCHED so we can now |
| @@ -109,9 +113,6 @@ static void klp_complete_transition(void) · 21 unmodified lines | ||
| 109 | 113 | } |
| 110 | 114 | } |
| 111 | 115 | |
| 112 | − if (klp_target_state == KLP_UNPATCHED && !immediate_func) | |
| 113 | − module_put(klp_transition_patch->mod); | |
| 114 | − | |
| 115 | 116 | /* Prevent klp_ftrace_handler() from seeing KLP_UNDEFINED state */ |
| 116 | 117 | if (klp_target_state == KLP_PATCHED) |
| 117 | 118 | klp_synchronize_transition(); |
| @@ -130,6 +131,27 @@ static void klp_complete_transition(void) · 12 unmodified lines | ||
| 130 | 131 | } |
| 131 | 132 | |
| 132 | 133 | done: |
| 134 | + klp_for_each_object(klp_transition_patch, obj) { | |
| 135 | + if (!klp_is_object_loaded(obj)) | |
| 136 | + continue; | |
| 137 | + if (klp_target_state == KLP_PATCHED) | |
| 138 | + klp_post_patch_callback(obj); | |
| 139 | + else if (klp_target_state == KLP_UNPATCHED) | |
| 140 | + klp_post_unpatch_callback(obj); | |
| 141 | + } | |
| 142 | + | |
| 143 | + pr_notice("'%s': %s complete\n", klp_transition_patch->mod->name, | |
| 144 | + klp_target_state == KLP_PATCHED ? "patching" : "unpatching"); | |
| 145 | + | |
| 146 | + /* | |
| 147 | + * See complementary comment in __klp_enable_patch() for why we | |
| 148 | + * keep the module reference for immediate patches. | |
| 149 | + */ | |
| 150 | + if (!klp_transition_patch->immediate && !immediate_func && | |
| 151 | + klp_target_state == KLP_UNPATCHED) { | |
| 152 | + module_put(klp_transition_patch->mod); | |
| 153 | + } | |
| 154 | + | |
| 133 | 155 | klp_target_state = KLP_UNDEFINED; |
| 134 | 156 | klp_transition_patch = NULL; |
| 135 | 157 | } |
| @@ -145,6 +167,9 @@ void klp_cancel_transition(void) · 9 unmodified lines | ||
| 145 | 167 | if (WARN_ON_ONCE(klp_target_state != KLP_PATCHED)) |
| 146 | 168 | return; |
| 147 | 169 | |
| 170 | + pr_debug("'%s': canceling patching transition, going to unpatch\n", | |
| 171 | + klp_transition_patch->mod->name); | |
| 172 | + | |
| 148 | 173 | klp_target_state = KLP_UNPATCHED; |
| 149 | 174 | klp_complete_transition(); |
| 150 | 175 | } |
| @@ -408,9 +433,6 @@ void klp_try_complete_transition(void) · 257 unmodified lines | ||
| 408 | 433 | } |
| 409 | 434 | |
| 410 | 435 | success: |
| 411 | − pr_notice("'%s': %s complete\n", klp_transition_patch->mod->name, | |
| 412 | − klp_target_state == KLP_PATCHED ? "patching" : "unpatching"); | |
| 413 | − | |
| 414 | 436 | /* we're done, now cleanup the data structures */ |
| 415 | 437 | klp_complete_transition(); |
| 416 | 438 | } |
| @@ -426,7 +448,8 @@ void klp_start_transition(void) · 9 unmodified lines | ||
| 426 | 448 | |
| 427 | 449 | WARN_ON_ONCE(klp_target_state == KLP_UNDEFINED); |
| 428 | 450 | |
| 429 | − pr_notice("'%s': %s...\n", klp_transition_patch->mod->name, | |
| 451 | + pr_notice("'%s': starting %s transition\n", | |
| 452 | + klp_transition_patch->mod->name, | |
| 430 | 453 | klp_target_state == KLP_PATCHED ? "patching" : "unpatching"); |
| 431 | 454 | |
| 432 | 455 | /* |
| @@ -482,6 +505,9 @@ void klp_init_transition(struct klp_patch *patch, int state) · 49 unmodified lines | ||
| 482 | 505 | */ |
| 483 | 506 | klp_target_state = state; |
| 484 | 507 | |
| 508 | + pr_debug("'%s': initializing %s transition\n", patch->mod->name, | |
| 509 | + klp_target_state == KLP_PATCHED ? "patching" : "unpatching"); | |
| 510 | + | |
| 485 | 511 | /* |
| 486 | 512 | * If the patch can be applied or reverted immediately, skip the |
| 487 | 513 | * per-task transitions. |
| @@ -547,6 +573,11 @@ void klp_reverse_transition(void) · 59 unmodified lines | ||
| 547 | 573 | unsigned int cpu; |
| 548 | 574 | struct task_struct *g, *task; |
| 549 | 575 | |
| 576 | + pr_debug("'%s': reversing transition from %s\n", | |
| 577 | + klp_transition_patch->mod->name, | |
| 578 | + klp_target_state == KLP_PATCHED ? "patching to unpatching" : | |
| 579 | + "unpatching to patching"); | |
| 580 | + | |
| 550 | 581 | klp_transition_patch->enabled = !klp_transition_patch->enabled; |
| 551 | 582 | |
| 552 | 583 | klp_target_state = !klp_target_state; |