blob: 2401e7f955d32dbc62b85b1978cc711c5cb136b1 [file] [log] [blame]
Seth Jenningsb700e7f2014-12-16 11:58:19 -06001/*
2 * core.c - Kernel Live Patching Core
3 *
4 * Copyright (C) 2014 Seth Jennings <sjenning@redhat.com>
5 * Copyright (C) 2014 SUSE
6 *
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.
11 *
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.
16 *
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/>.
19 */
20
21#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
22
23#include <linux/module.h>
24#include <linux/kernel.h>
25#include <linux/mutex.h>
26#include <linux/slab.h>
27#include <linux/ftrace.h>
28#include <linux/list.h>
29#include <linux/kallsyms.h>
30#include <linux/livepatch.h>
31
32/*
33 * The klp_mutex protects the klp_patches list and state transitions of any
34 * structure reachable from the patches list. References to any structure must
35 * be obtained under mutex protection.
36 */
37
38static DEFINE_MUTEX(klp_mutex);
39static LIST_HEAD(klp_patches);
40
41static struct kobject *klp_root_kobj;
42
43static bool klp_is_module(struct klp_object *obj)
44{
45 return obj->name;
46}
47
48static bool klp_is_object_loaded(struct klp_object *obj)
49{
50 return !obj->name || obj->mod;
51}
52
53/* sets obj->mod if object is not vmlinux and module is found */
54static void klp_find_object_module(struct klp_object *obj)
55{
56 if (!klp_is_module(obj))
57 return;
58
59 mutex_lock(&module_mutex);
60 /*
61 * We don't need to take a reference on the module here because we have
62 * the klp_mutex, which is also taken by the module notifier. This
63 * prevents any module from unloading until we release the klp_mutex.
64 */
65 obj->mod = find_module(obj->name);
66 mutex_unlock(&module_mutex);
67}
68
69/* klp_mutex must be held by caller */
70static bool klp_is_patch_registered(struct klp_patch *patch)
71{
72 struct klp_patch *mypatch;
73
74 list_for_each_entry(mypatch, &klp_patches, list)
75 if (mypatch == patch)
76 return true;
77
78 return false;
79}
80
81static bool klp_initialized(void)
82{
83 return klp_root_kobj;
84}
85
86struct klp_find_arg {
87 const char *objname;
88 const char *name;
89 unsigned long addr;
90 /*
91 * If count == 0, the symbol was not found. If count == 1, a unique
92 * match was found and addr is set. If count > 1, there is
93 * unresolvable ambiguity among "count" number of symbols with the same
94 * name in the same object.
95 */
96 unsigned long count;
97};
98
99static int klp_find_callback(void *data, const char *name,
100 struct module *mod, unsigned long addr)
101{
102 struct klp_find_arg *args = data;
103
104 if ((mod && !args->objname) || (!mod && args->objname))
105 return 0;
106
107 if (strcmp(args->name, name))
108 return 0;
109
110 if (args->objname && strcmp(args->objname, mod->name))
111 return 0;
112
113 /*
114 * args->addr might be overwritten if another match is found
115 * but klp_find_object_symbol() handles this and only returns the
116 * addr if count == 1.
117 */
118 args->addr = addr;
119 args->count++;
120
121 return 0;
122}
123
124static int klp_find_object_symbol(const char *objname, const char *name,
125 unsigned long *addr)
126{
127 struct klp_find_arg args = {
128 .objname = objname,
129 .name = name,
130 .addr = 0,
131 .count = 0
132 };
133
134 kallsyms_on_each_symbol(klp_find_callback, &args);
135
136 if (args.count == 0)
137 pr_err("symbol '%s' not found in symbol table\n", name);
138 else if (args.count > 1)
139 pr_err("unresolvable ambiguity (%lu matches) on symbol '%s' in object '%s'\n",
140 args.count, name, objname);
141 else {
142 *addr = args.addr;
143 return 0;
144 }
145
146 *addr = 0;
147 return -EINVAL;
148}
149
150struct klp_verify_args {
151 const char *name;
152 const unsigned long addr;
153};
154
155static int klp_verify_callback(void *data, const char *name,
156 struct module *mod, unsigned long addr)
157{
158 struct klp_verify_args *args = data;
159
160 if (!mod &&
161 !strcmp(args->name, name) &&
162 args->addr == addr)
163 return 1;
164
165 return 0;
166}
167
168static int klp_verify_vmlinux_symbol(const char *name, unsigned long addr)
169{
170 struct klp_verify_args args = {
171 .name = name,
172 .addr = addr,
173 };
174
175 if (kallsyms_on_each_symbol(klp_verify_callback, &args))
176 return 0;
177
178 pr_err("symbol '%s' not found at specified address 0x%016lx, kernel mismatch?",
179 name, addr);
180 return -EINVAL;
181}
182
183static int klp_find_verify_func_addr(struct klp_object *obj,
184 struct klp_func *func)
185{
186 int ret;
187
188#if defined(CONFIG_RANDOMIZE_BASE)
189 /* KASLR is enabled, disregard old_addr from user */
190 func->old_addr = 0;
191#endif
192
193 if (!func->old_addr || klp_is_module(obj))
194 ret = klp_find_object_symbol(obj->name, func->old_name,
195 &func->old_addr);
196 else
197 ret = klp_verify_vmlinux_symbol(func->old_name,
198 func->old_addr);
199
200 return ret;
201}
202
203/*
204 * external symbols are located outside the parent object (where the parent
205 * object is either vmlinux or the kmod being patched).
206 */
207static int klp_find_external_symbol(struct module *pmod, const char *name,
208 unsigned long *addr)
209{
210 const struct kernel_symbol *sym;
211
212 /* first, check if it's an exported symbol */
213 preempt_disable();
214 sym = find_symbol(name, NULL, NULL, true, true);
215 preempt_enable();
216 if (sym) {
217 *addr = sym->value;
218 return 0;
219 }
220
221 /* otherwise check if it's in another .o within the patch module */
222 return klp_find_object_symbol(pmod->name, name, addr);
223}
224
225static int klp_write_object_relocations(struct module *pmod,
226 struct klp_object *obj)
227{
228 int ret;
229 struct klp_reloc *reloc;
230
231 if (WARN_ON(!klp_is_object_loaded(obj)))
232 return -EINVAL;
233
234 if (WARN_ON(!obj->relocs))
235 return -EINVAL;
236
237 for (reloc = obj->relocs; reloc->name; reloc++) {
238 if (!klp_is_module(obj)) {
239 ret = klp_verify_vmlinux_symbol(reloc->name,
240 reloc->val);
241 if (ret)
242 return ret;
243 } else {
244 /* module, reloc->val needs to be discovered */
245 if (reloc->external)
246 ret = klp_find_external_symbol(pmod,
247 reloc->name,
248 &reloc->val);
249 else
250 ret = klp_find_object_symbol(obj->mod->name,
251 reloc->name,
252 &reloc->val);
253 if (ret)
254 return ret;
255 }
256 ret = klp_write_module_reloc(pmod, reloc->type, reloc->loc,
257 reloc->val + reloc->addend);
258 if (ret) {
259 pr_err("relocation failed for symbol '%s' at 0x%016lx (%d)\n",
260 reloc->name, reloc->val, ret);
261 return ret;
262 }
263 }
264
265 return 0;
266}
267
268static void notrace klp_ftrace_handler(unsigned long ip,
269 unsigned long parent_ip,
270 struct ftrace_ops *ops,
271 struct pt_regs *regs)
272{
273 struct klp_func *func = ops->private;
274
Li Binb5bfc512014-12-19 14:11:17 +0800275 klp_arch_set_pc(regs, (unsigned long)func->new_func);
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600276}
277
278static int klp_disable_func(struct klp_func *func)
279{
280 int ret;
281
282 if (WARN_ON(func->state != KLP_ENABLED))
283 return -EINVAL;
284
285 if (WARN_ON(!func->old_addr))
286 return -EINVAL;
287
288 ret = unregister_ftrace_function(func->fops);
289 if (ret) {
290 pr_err("failed to unregister ftrace handler for function '%s' (%d)\n",
291 func->old_name, ret);
292 return ret;
293 }
294
295 ret = ftrace_set_filter_ip(func->fops, func->old_addr, 1, 0);
296 if (ret)
297 pr_warn("function unregister succeeded but failed to clear the filter\n");
298
299 func->state = KLP_DISABLED;
300
301 return 0;
302}
303
304static int klp_enable_func(struct klp_func *func)
305{
306 int ret;
307
308 if (WARN_ON(!func->old_addr))
309 return -EINVAL;
310
311 if (WARN_ON(func->state != KLP_DISABLED))
312 return -EINVAL;
313
314 ret = ftrace_set_filter_ip(func->fops, func->old_addr, 0, 0);
315 if (ret) {
316 pr_err("failed to set ftrace filter for function '%s' (%d)\n",
317 func->old_name, ret);
318 return ret;
319 }
320
321 ret = register_ftrace_function(func->fops);
322 if (ret) {
323 pr_err("failed to register ftrace handler for function '%s' (%d)\n",
324 func->old_name, ret);
325 ftrace_set_filter_ip(func->fops, func->old_addr, 1, 0);
326 } else {
327 func->state = KLP_ENABLED;
328 }
329
330 return ret;
331}
332
333static int klp_disable_object(struct klp_object *obj)
334{
335 struct klp_func *func;
336 int ret;
337
338 for (func = obj->funcs; func->old_name; func++) {
339 if (func->state != KLP_ENABLED)
340 continue;
341
342 ret = klp_disable_func(func);
343 if (ret)
344 return ret;
345 }
346
347 obj->state = KLP_DISABLED;
348
349 return 0;
350}
351
352static int klp_enable_object(struct klp_object *obj)
353{
354 struct klp_func *func;
355 int ret;
356
357 if (WARN_ON(obj->state != KLP_DISABLED))
358 return -EINVAL;
359
360 if (WARN_ON(!klp_is_object_loaded(obj)))
361 return -EINVAL;
362
363 for (func = obj->funcs; func->old_name; func++) {
364 ret = klp_enable_func(func);
365 if (ret)
366 goto unregister;
367 }
368 obj->state = KLP_ENABLED;
369
370 return 0;
371
372unregister:
373 WARN_ON(klp_disable_object(obj));
374 return ret;
375}
376
377static int __klp_disable_patch(struct klp_patch *patch)
378{
379 struct klp_object *obj;
380 int ret;
381
Josh Poimboeuf83a90bb2015-01-20 09:26:18 -0600382 /* enforce stacking: only the last enabled patch can be disabled */
383 if (!list_is_last(&patch->list, &klp_patches) &&
384 list_next_entry(patch, list)->state == KLP_ENABLED)
385 return -EBUSY;
386
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600387 pr_notice("disabling patch '%s'\n", patch->mod->name);
388
389 for (obj = patch->objs; obj->funcs; obj++) {
390 if (obj->state != KLP_ENABLED)
391 continue;
392
393 ret = klp_disable_object(obj);
394 if (ret)
395 return ret;
396 }
397
398 patch->state = KLP_DISABLED;
399
400 return 0;
401}
402
403/**
404 * klp_disable_patch() - disables a registered patch
405 * @patch: The registered, enabled patch to be disabled
406 *
407 * Unregisters the patched functions from ftrace.
408 *
409 * Return: 0 on success, otherwise error
410 */
411int klp_disable_patch(struct klp_patch *patch)
412{
413 int ret;
414
415 mutex_lock(&klp_mutex);
416
417 if (!klp_is_patch_registered(patch)) {
418 ret = -EINVAL;
419 goto err;
420 }
421
422 if (patch->state == KLP_DISABLED) {
423 ret = -EINVAL;
424 goto err;
425 }
426
427 ret = __klp_disable_patch(patch);
428
429err:
430 mutex_unlock(&klp_mutex);
431 return ret;
432}
433EXPORT_SYMBOL_GPL(klp_disable_patch);
434
435static int __klp_enable_patch(struct klp_patch *patch)
436{
437 struct klp_object *obj;
438 int ret;
439
440 if (WARN_ON(patch->state != KLP_DISABLED))
441 return -EINVAL;
442
Josh Poimboeuf83a90bb2015-01-20 09:26:18 -0600443 /* enforce stacking: only the first disabled patch can be enabled */
444 if (patch->list.prev != &klp_patches &&
445 list_prev_entry(patch, list)->state == KLP_DISABLED)
446 return -EBUSY;
447
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600448 pr_notice_once("tainting kernel with TAINT_LIVEPATCH\n");
449 add_taint(TAINT_LIVEPATCH, LOCKDEP_STILL_OK);
450
451 pr_notice("enabling patch '%s'\n", patch->mod->name);
452
453 for (obj = patch->objs; obj->funcs; obj++) {
454 klp_find_object_module(obj);
455
456 if (!klp_is_object_loaded(obj))
457 continue;
458
459 ret = klp_enable_object(obj);
460 if (ret)
461 goto unregister;
462 }
463
464 patch->state = KLP_ENABLED;
465
466 return 0;
467
468unregister:
469 WARN_ON(__klp_disable_patch(patch));
470 return ret;
471}
472
473/**
474 * klp_enable_patch() - enables a registered patch
475 * @patch: The registered, disabled patch to be enabled
476 *
477 * Performs the needed symbol lookups and code relocations,
478 * then registers the patched functions with ftrace.
479 *
480 * Return: 0 on success, otherwise error
481 */
482int klp_enable_patch(struct klp_patch *patch)
483{
484 int ret;
485
486 mutex_lock(&klp_mutex);
487
488 if (!klp_is_patch_registered(patch)) {
489 ret = -EINVAL;
490 goto err;
491 }
492
493 ret = __klp_enable_patch(patch);
494
495err:
496 mutex_unlock(&klp_mutex);
497 return ret;
498}
499EXPORT_SYMBOL_GPL(klp_enable_patch);
500
501/*
502 * Sysfs Interface
503 *
504 * /sys/kernel/livepatch
505 * /sys/kernel/livepatch/<patch>
506 * /sys/kernel/livepatch/<patch>/enabled
507 * /sys/kernel/livepatch/<patch>/<object>
508 * /sys/kernel/livepatch/<patch>/<object>/<func>
509 */
510
511static ssize_t enabled_store(struct kobject *kobj, struct kobj_attribute *attr,
512 const char *buf, size_t count)
513{
514 struct klp_patch *patch;
515 int ret;
516 unsigned long val;
517
518 ret = kstrtoul(buf, 10, &val);
519 if (ret)
520 return -EINVAL;
521
522 if (val != KLP_DISABLED && val != KLP_ENABLED)
523 return -EINVAL;
524
525 patch = container_of(kobj, struct klp_patch, kobj);
526
527 mutex_lock(&klp_mutex);
528
529 if (val == patch->state) {
530 /* already in requested state */
531 ret = -EINVAL;
532 goto err;
533 }
534
535 if (val == KLP_ENABLED) {
536 ret = __klp_enable_patch(patch);
537 if (ret)
538 goto err;
539 } else {
540 ret = __klp_disable_patch(patch);
541 if (ret)
542 goto err;
543 }
544
545 mutex_unlock(&klp_mutex);
546
547 return count;
548
549err:
550 mutex_unlock(&klp_mutex);
551 return ret;
552}
553
554static ssize_t enabled_show(struct kobject *kobj,
555 struct kobj_attribute *attr, char *buf)
556{
557 struct klp_patch *patch;
558
559 patch = container_of(kobj, struct klp_patch, kobj);
560 return snprintf(buf, PAGE_SIZE-1, "%d\n", patch->state);
561}
562
563static struct kobj_attribute enabled_kobj_attr = __ATTR_RW(enabled);
564static struct attribute *klp_patch_attrs[] = {
565 &enabled_kobj_attr.attr,
566 NULL
567};
568
569static void klp_kobj_release_patch(struct kobject *kobj)
570{
571 /*
572 * Once we have a consistency model we'll need to module_put() the
573 * patch module here. See klp_register_patch() for more details.
574 */
575}
576
577static struct kobj_type klp_ktype_patch = {
578 .release = klp_kobj_release_patch,
579 .sysfs_ops = &kobj_sysfs_ops,
580 .default_attrs = klp_patch_attrs,
581};
582
583static void klp_kobj_release_func(struct kobject *kobj)
584{
585 struct klp_func *func;
586
587 func = container_of(kobj, struct klp_func, kobj);
588 kfree(func->fops);
589}
590
591static struct kobj_type klp_ktype_func = {
592 .release = klp_kobj_release_func,
593 .sysfs_ops = &kobj_sysfs_ops,
594};
595
596/*
597 * Free all functions' kobjects in the array up to some limit. When limit is
598 * NULL, all kobjects are freed.
599 */
600static void klp_free_funcs_limited(struct klp_object *obj,
601 struct klp_func *limit)
602{
603 struct klp_func *func;
604
605 for (func = obj->funcs; func->old_name && func != limit; func++)
606 kobject_put(&func->kobj);
607}
608
609/* Clean up when a patched object is unloaded */
610static void klp_free_object_loaded(struct klp_object *obj)
611{
612 struct klp_func *func;
613
614 obj->mod = NULL;
615
616 for (func = obj->funcs; func->old_name; func++)
617 func->old_addr = 0;
618}
619
620/*
621 * Free all objects' kobjects in the array up to some limit. When limit is
622 * NULL, all kobjects are freed.
623 */
624static void klp_free_objects_limited(struct klp_patch *patch,
625 struct klp_object *limit)
626{
627 struct klp_object *obj;
628
629 for (obj = patch->objs; obj->funcs && obj != limit; obj++) {
630 klp_free_funcs_limited(obj, NULL);
631 kobject_put(obj->kobj);
632 }
633}
634
635static void klp_free_patch(struct klp_patch *patch)
636{
637 klp_free_objects_limited(patch, NULL);
638 if (!list_empty(&patch->list))
639 list_del(&patch->list);
640 kobject_put(&patch->kobj);
641}
642
643static int klp_init_func(struct klp_object *obj, struct klp_func *func)
644{
645 struct ftrace_ops *ops;
646 int ret;
647
648 ops = kzalloc(sizeof(*ops), GFP_KERNEL);
649 if (!ops)
650 return -ENOMEM;
651
652 ops->private = func;
653 ops->func = klp_ftrace_handler;
Josh Poimboeuf33e86122014-12-22 13:39:54 +0100654 ops->flags = FTRACE_OPS_FL_SAVE_REGS | FTRACE_OPS_FL_DYNAMIC |
655 FTRACE_OPS_FL_IPMODIFY;
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600656 func->fops = ops;
657 func->state = KLP_DISABLED;
658
659 ret = kobject_init_and_add(&func->kobj, &klp_ktype_func,
660 obj->kobj, func->old_name);
661 if (ret) {
662 kfree(func->fops);
663 return ret;
664 }
665
666 return 0;
667}
668
669/* parts of the initialization that is done only when the object is loaded */
670static int klp_init_object_loaded(struct klp_patch *patch,
671 struct klp_object *obj)
672{
673 struct klp_func *func;
674 int ret;
675
676 if (obj->relocs) {
677 ret = klp_write_object_relocations(patch->mod, obj);
678 if (ret)
679 return ret;
680 }
681
682 for (func = obj->funcs; func->old_name; func++) {
683 ret = klp_find_verify_func_addr(obj, func);
684 if (ret)
685 return ret;
686 }
687
688 return 0;
689}
690
691static int klp_init_object(struct klp_patch *patch, struct klp_object *obj)
692{
693 struct klp_func *func;
694 int ret;
695 const char *name;
696
697 if (!obj->funcs)
698 return -EINVAL;
699
700 obj->state = KLP_DISABLED;
701
702 klp_find_object_module(obj);
703
704 name = klp_is_module(obj) ? obj->name : "vmlinux";
705 obj->kobj = kobject_create_and_add(name, &patch->kobj);
706 if (!obj->kobj)
707 return -ENOMEM;
708
709 for (func = obj->funcs; func->old_name; func++) {
710 ret = klp_init_func(obj, func);
711 if (ret)
712 goto free;
713 }
714
715 if (klp_is_object_loaded(obj)) {
716 ret = klp_init_object_loaded(patch, obj);
717 if (ret)
718 goto free;
719 }
720
721 return 0;
722
723free:
724 klp_free_funcs_limited(obj, func);
725 kobject_put(obj->kobj);
726 return ret;
727}
728
729static int klp_init_patch(struct klp_patch *patch)
730{
731 struct klp_object *obj;
732 int ret;
733
734 if (!patch->objs)
735 return -EINVAL;
736
737 mutex_lock(&klp_mutex);
738
739 patch->state = KLP_DISABLED;
740
741 ret = kobject_init_and_add(&patch->kobj, &klp_ktype_patch,
742 klp_root_kobj, patch->mod->name);
743 if (ret)
744 goto unlock;
745
746 for (obj = patch->objs; obj->funcs; obj++) {
747 ret = klp_init_object(patch, obj);
748 if (ret)
749 goto free;
750 }
751
Josh Poimboeuf99590ba2015-01-09 14:03:04 -0600752 list_add_tail(&patch->list, &klp_patches);
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600753
754 mutex_unlock(&klp_mutex);
755
756 return 0;
757
758free:
759 klp_free_objects_limited(patch, obj);
760 kobject_put(&patch->kobj);
761unlock:
762 mutex_unlock(&klp_mutex);
763 return ret;
764}
765
766/**
767 * klp_unregister_patch() - unregisters a patch
768 * @patch: Disabled patch to be unregistered
769 *
770 * Frees the data structures and removes the sysfs interface.
771 *
772 * Return: 0 on success, otherwise error
773 */
774int klp_unregister_patch(struct klp_patch *patch)
775{
776 int ret = 0;
777
778 mutex_lock(&klp_mutex);
779
780 if (!klp_is_patch_registered(patch)) {
781 ret = -EINVAL;
782 goto out;
783 }
784
785 if (patch->state == KLP_ENABLED) {
786 ret = -EBUSY;
787 goto out;
788 }
789
790 klp_free_patch(patch);
791
792out:
793 mutex_unlock(&klp_mutex);
794 return ret;
795}
796EXPORT_SYMBOL_GPL(klp_unregister_patch);
797
798/**
799 * klp_register_patch() - registers a patch
800 * @patch: Patch to be registered
801 *
802 * Initializes the data structure associated with the patch and
803 * creates the sysfs interface.
804 *
805 * Return: 0 on success, otherwise error
806 */
807int klp_register_patch(struct klp_patch *patch)
808{
809 int ret;
810
811 if (!klp_initialized())
812 return -ENODEV;
813
814 if (!patch || !patch->mod)
815 return -EINVAL;
816
817 /*
818 * A reference is taken on the patch module to prevent it from being
819 * unloaded. Right now, we don't allow patch modules to unload since
820 * there is currently no method to determine if a thread is still
821 * running in the patched code contained in the patch module once
822 * the ftrace registration is successful.
823 */
824 if (!try_module_get(patch->mod))
825 return -ENODEV;
826
827 ret = klp_init_patch(patch);
828 if (ret)
829 module_put(patch->mod);
830
831 return ret;
832}
833EXPORT_SYMBOL_GPL(klp_register_patch);
834
835static void klp_module_notify_coming(struct klp_patch *patch,
836 struct klp_object *obj)
837{
838 struct module *pmod = patch->mod;
839 struct module *mod = obj->mod;
840 int ret;
841
842 ret = klp_init_object_loaded(patch, obj);
843 if (ret)
844 goto err;
845
846 if (patch->state == KLP_DISABLED)
847 return;
848
849 pr_notice("applying patch '%s' to loading module '%s'\n",
850 pmod->name, mod->name);
851
852 ret = klp_enable_object(obj);
853 if (!ret)
854 return;
855
856err:
857 pr_warn("failed to apply patch '%s' to module '%s' (%d)\n",
858 pmod->name, mod->name, ret);
859}
860
861static void klp_module_notify_going(struct klp_patch *patch,
862 struct klp_object *obj)
863{
864 struct module *pmod = patch->mod;
865 struct module *mod = obj->mod;
866 int ret;
867
868 if (patch->state == KLP_DISABLED)
869 goto disabled;
870
871 pr_notice("reverting patch '%s' on unloading module '%s'\n",
872 pmod->name, mod->name);
873
874 ret = klp_disable_object(obj);
875 if (ret)
876 pr_warn("failed to revert patch '%s' on module '%s' (%d)\n",
877 pmod->name, mod->name, ret);
878
879disabled:
880 klp_free_object_loaded(obj);
881}
882
883static int klp_module_notify(struct notifier_block *nb, unsigned long action,
884 void *data)
885{
886 struct module *mod = data;
887 struct klp_patch *patch;
888 struct klp_object *obj;
889
890 if (action != MODULE_STATE_COMING && action != MODULE_STATE_GOING)
891 return 0;
892
893 mutex_lock(&klp_mutex);
894
895 list_for_each_entry(patch, &klp_patches, list) {
896 for (obj = patch->objs; obj->funcs; obj++) {
897 if (!klp_is_module(obj) || strcmp(obj->name, mod->name))
898 continue;
899
900 if (action == MODULE_STATE_COMING) {
901 obj->mod = mod;
902 klp_module_notify_coming(patch, obj);
903 } else /* MODULE_STATE_GOING */
904 klp_module_notify_going(patch, obj);
905
906 break;
907 }
908 }
909
910 mutex_unlock(&klp_mutex);
911
912 return 0;
913}
914
915static struct notifier_block klp_module_nb = {
916 .notifier_call = klp_module_notify,
917 .priority = INT_MIN+1, /* called late but before ftrace notifier */
918};
919
920static int klp_init(void)
921{
922 int ret;
923
Jiri Kosinab9dfe0b2015-01-09 10:53:21 +0100924 ret = klp_check_compiler_support();
925 if (ret) {
926 pr_info("Your compiler is too old; turning off.\n");
927 return -EINVAL;
928 }
929
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600930 ret = register_module_notifier(&klp_module_nb);
931 if (ret)
932 return ret;
933
934 klp_root_kobj = kobject_create_and_add("livepatch", kernel_kobj);
935 if (!klp_root_kobj) {
936 ret = -ENOMEM;
937 goto unregister;
938 }
939
940 return 0;
941
942unregister:
943 unregister_module_notifier(&klp_module_nb);
944 return ret;
945}
946
947module_init(klp_init);