History

Merge branch 'for-4.15/callbacks' into for-linus

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()

Jiri Kosina committed 9 years ago

Commit fc41efc2 parents cb65dc7 89a9a1c10 files · +1114 −17Browse files

Showing the changes to kernel/livepatch (4 of 10 files). Show all

Unified · Split

4 files
  1. kernel/livepatch/core.c +42 −10
  2. kernel/livepatch/core.h +40 −0
  3. kernel/livepatch/patch.c +1 −0
  4. kernel/livepatch/transition.c +38 −7
kernel/livepatch/core.c+42−10
@@ -54,11 +54,6 @@ static bool klp_is_module(struct klp_object *obj) · 53 unmodified lines
5454 return obj->name;
5555 }
5656
57−static bool klp_is_object_loaded(struct klp_object *obj)
58−{
59− return !obj->name || obj->mod;
60−}
61−
6257 /* sets obj->mod if object is not vmlinux and module is found */
6358 static void klp_find_object_module(struct klp_object *obj)
6459 {
@@ -285,6 +280,11 @@ static int klp_write_object_relocations(struct module *pmod, · 220 unmodified lines
285280
286281 static int __klp_disable_patch(struct klp_patch *patch)
287282 {
283+ struct klp_object *obj;
284+
285+ if (WARN_ON(!patch->enabled))
286+ return -EINVAL;
287+
288288 if (klp_transition_patch)
289289 return -EBUSY;
290290
@@ -295,6 +295,10 @@ static int __klp_disable_patch(struct klp_patch *patch) · 4 unmodified lines
295295
296296 klp_init_transition(patch, KLP_UNPATCHED);
297297
298+ klp_for_each_object(patch, obj)
299+ if (obj->patched)
300+ klp_pre_unpatch_callback(obj);
301+
298302 /*
299303 * Enforce the order of the func->transition writes in
300304 * 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
388392 if (!klp_is_object_loaded(obj))
389393 continue;
390394
391− ret = klp_patch_object(obj);
395+ ret = klp_pre_patch_callback(obj);
392396 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+ }
395401
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;
398407 }
399408 }
400409
@@ -403,6 +412,11 @@ static int __klp_enable_patch(struct klp_patch *patch) · 2 unmodified lines
403412 patch->enabled = true;
404413
405414 return 0;
415+err:
416+ pr_warn("failed to enable patch '%s'\n", patch->mod->name);
417+
418+ klp_cancel_transition();
419+ return ret;
406420 }
407421
408422 /**
@@ -854,9 +868,15 @@ static void klp_cleanup_module_patches_limited(struct module *mod, · 445 unmodified lines
854868 * is in transition.
855869 */
856870 if (patch->enabled || patch == klp_transition_patch) {
871+
872+ if (patch != klp_transition_patch)
873+ klp_pre_unpatch_callback(obj);
874+
857875 pr_notice("reverting patch '%s' on unloading module '%s'\n",
858876 patch->mod->name, obj->mod->name);
859877 klp_unpatch_object(obj);
878+
879+ klp_post_unpatch_callback(obj);
860880 }
861881
862882 klp_free_object_loaded(obj);
@@ -906,13 +926,25 @@ int klp_module_coming(struct module *mod) · 43 unmodified lines
906926 pr_notice("applying patch '%s' to loading module '%s'\n",
907927 patch->mod->name, obj->mod->name);
908928
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+
909936 ret = klp_patch_object(obj);
910937 if (ret) {
911938 pr_warn("failed to apply patch '%s' to module '%s' (%d)\n",
912939 patch->mod->name, obj->mod->name, ret);
940+
941+ klp_post_unpatch_callback(obj);
913942 goto err;
914943 }
915944
945+ if (patch != klp_transition_patch)
946+ klp_post_patch_callback(obj);
947+
916948 break;
917949 }
918950 }
kernel/livepatch/core.h+40−0
@@ -1,6 +1,46 @@
11 #ifndef _LIVEPATCH_CORE_H
22 #define _LIVEPATCH_CORE_H
33
4+#include <linux/livepatch.h>
5+
46 extern struct mutex klp_mutex;
57
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+
646 #endif /* _LIVEPATCH_CORE_H */
kernel/livepatch/patch.c+1−0
@@ -28,6 +28,7 @@ · 27 unmodified lines
2828 #include <linux/slab.h>
2929 #include <linux/bug.h>
3030 #include <linux/printk.h>
31+#include "core.h"
3132 #include "patch.h"
3233 #include "transition.h"
3334
kernel/livepatch/transition.c+38−7
@@ -82,6 +82,10 @@ static void klp_complete_transition(void) · 81 unmodified lines
8282 unsigned int cpu;
8383 bool immediate_func = false;
8484
85+ pr_debug("'%s': completing %s transition\n",
86+ klp_transition_patch->mod->name,
87+ klp_target_state == KLP_PATCHED ? "patching" : "unpatching");
88+
8589 if (klp_target_state == KLP_UNPATCHED) {
8690 /*
8791 * All tasks have transitioned to KLP_UNPATCHED so we can now
@@ -109,9 +113,6 @@ static void klp_complete_transition(void) · 21 unmodified lines
109113 }
110114 }
111115
112− if (klp_target_state == KLP_UNPATCHED && !immediate_func)
113− module_put(klp_transition_patch->mod);
114−
115116 /* Prevent klp_ftrace_handler() from seeing KLP_UNDEFINED state */
116117 if (klp_target_state == KLP_PATCHED)
117118 klp_synchronize_transition();
@@ -130,6 +131,27 @@ static void klp_complete_transition(void) · 12 unmodified lines
130131 }
131132
132133 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+
133155 klp_target_state = KLP_UNDEFINED;
134156 klp_transition_patch = NULL;
135157 }
@@ -145,6 +167,9 @@ void klp_cancel_transition(void) · 9 unmodified lines
145167 if (WARN_ON_ONCE(klp_target_state != KLP_PATCHED))
146168 return;
147169
170+ pr_debug("'%s': canceling patching transition, going to unpatch\n",
171+ klp_transition_patch->mod->name);
172+
148173 klp_target_state = KLP_UNPATCHED;
149174 klp_complete_transition();
150175 }
@@ -408,9 +433,6 @@ void klp_try_complete_transition(void) · 257 unmodified lines
408433 }
409434
410435 success:
411− pr_notice("'%s': %s complete\n", klp_transition_patch->mod->name,
412− klp_target_state == KLP_PATCHED ? "patching" : "unpatching");
413−
414436 /* we're done, now cleanup the data structures */
415437 klp_complete_transition();
416438 }
@@ -426,7 +448,8 @@ void klp_start_transition(void) · 9 unmodified lines
426448
427449 WARN_ON_ONCE(klp_target_state == KLP_UNDEFINED);
428450
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,
430453 klp_target_state == KLP_PATCHED ? "patching" : "unpatching");
431454
432455 /*
@@ -482,6 +505,9 @@ void klp_init_transition(struct klp_patch *patch, int state) · 49 unmodified lines
482505 */
483506 klp_target_state = state;
484507
508+ pr_debug("'%s': initializing %s transition\n", patch->mod->name,
509+ klp_target_state == KLP_PATCHED ? "patching" : "unpatching");
510+
485511 /*
486512 * If the patch can be applied or reverted immediately, skip the
487513 * per-task transitions.
@@ -547,6 +573,11 @@ void klp_reverse_transition(void) · 59 unmodified lines
547573 unsigned int cpu;
548574 struct task_struct *g, *task;
549575
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+
550581 klp_transition_patch->enabled = !klp_transition_patch->enabled;
551582
552583 klp_target_state = !klp_target_state;

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t