Skip to content

Commit 573de2a

Browse files
committed
Merge branch 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/livepatching/livepatching
Pull livepatching updates from Jiri Kosina: - livepatching kselftests improvements from Joe Lawrence and Miroslav Benes - making use of gcc's -flive-patching option when available, from Miroslav Benes - kobject handling cleanups, from Petr Mladek * 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/livepatching/livepatching: livepatch: Remove duplicated code for early initialization livepatch: Remove custom kobject state handling livepatch: Convert error about unsupported reliable stacktrace into a warning selftests/livepatch: Add functions.sh to TEST_PROGS_EXTENDED kbuild: use -flive-patching when CONFIG_LIVEPATCH is enabled selftests/livepatch: use TEST_PROGS for test scripts
2 parents b4dd05d + 1efbd99 commit 573de2a

File tree

4 files changed

+45
-56
lines changed

4 files changed

+45
-56
lines changed

Makefile

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -811,6 +811,10 @@ KBUILD_CFLAGS_KERNEL += -ffunction-sections -fdata-sections
811811
LDFLAGS_vmlinux += --gc-sections
812812
endif
813813

814+
ifdef CONFIG_LIVEPATCH
815+
KBUILD_CFLAGS += $(call cc-option, -flive-patching=inline-clone)
816+
endif
817+
814818
# arch Makefile may override CC so keep this after arch Makefile is included
815819
NOSTDINC_FLAGS += -nostdinc -isystem $(shell $(CC) -print-file-name=include)
816820

include/linux/livepatch.h

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,6 @@ struct klp_func {
8686
struct list_head node;
8787
struct list_head stack_node;
8888
unsigned long old_size, new_size;
89-
bool kobj_added;
9089
bool nop;
9190
bool patched;
9291
bool transition;
@@ -141,7 +140,6 @@ struct klp_object {
141140
struct list_head func_list;
142141
struct list_head node;
143142
struct module *mod;
144-
bool kobj_added;
145143
bool dynamic;
146144
bool patched;
147145
};
@@ -170,7 +168,6 @@ struct klp_patch {
170168
struct list_head list;
171169
struct kobject kobj;
172170
struct list_head obj_list;
173-
bool kobj_added;
174171
bool enabled;
175172
bool forced;
176173
struct work_struct free_work;

kernel/livepatch/core.c

Lines changed: 39 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -426,7 +426,13 @@ static void klp_free_object_dynamic(struct klp_object *obj)
426426
kfree(obj);
427427
}
428428

429-
static struct klp_object *klp_alloc_object_dynamic(const char *name)
429+
static void klp_init_func_early(struct klp_object *obj,
430+
struct klp_func *func);
431+
static void klp_init_object_early(struct klp_patch *patch,
432+
struct klp_object *obj);
433+
434+
static struct klp_object *klp_alloc_object_dynamic(const char *name,
435+
struct klp_patch *patch)
430436
{
431437
struct klp_object *obj;
432438

@@ -442,7 +448,7 @@ static struct klp_object *klp_alloc_object_dynamic(const char *name)
442448
}
443449
}
444450

445-
INIT_LIST_HEAD(&obj->func_list);
451+
klp_init_object_early(patch, obj);
446452
obj->dynamic = true;
447453

448454
return obj;
@@ -471,6 +477,7 @@ static struct klp_func *klp_alloc_func_nop(struct klp_func *old_func,
471477
}
472478
}
473479

480+
klp_init_func_early(obj, func);
474481
/*
475482
* func->new_func is same as func->old_func. These addresses are
476483
* set when the object is loaded, see klp_init_object_loaded().
@@ -490,11 +497,9 @@ static int klp_add_object_nops(struct klp_patch *patch,
490497
obj = klp_find_object(patch, old_obj);
491498

492499
if (!obj) {
493-
obj = klp_alloc_object_dynamic(old_obj->name);
500+
obj = klp_alloc_object_dynamic(old_obj->name, patch);
494501
if (!obj)
495502
return -ENOMEM;
496-
497-
list_add_tail(&obj->node, &patch->obj_list);
498503
}
499504

500505
klp_for_each_func(old_obj, old_func) {
@@ -505,8 +510,6 @@ static int klp_add_object_nops(struct klp_patch *patch,
505510
func = klp_alloc_func_nop(old_func, obj);
506511
if (!func)
507512
return -ENOMEM;
508-
509-
list_add_tail(&func->node, &obj->func_list);
510513
}
511514

512515
return 0;
@@ -588,13 +591,7 @@ static void __klp_free_funcs(struct klp_object *obj, bool nops_only)
588591
continue;
589592

590593
list_del(&func->node);
591-
592-
/* Might be called from klp_init_patch() error path. */
593-
if (func->kobj_added) {
594-
kobject_put(&func->kobj);
595-
} else if (func->nop) {
596-
klp_free_func_nop(func);
597-
}
594+
kobject_put(&func->kobj);
598595
}
599596
}
600597

@@ -624,13 +621,7 @@ static void __klp_free_objects(struct klp_patch *patch, bool nops_only)
624621
continue;
625622

626623
list_del(&obj->node);
627-
628-
/* Might be called from klp_init_patch() error path. */
629-
if (obj->kobj_added) {
630-
kobject_put(&obj->kobj);
631-
} else if (obj->dynamic) {
632-
klp_free_object_dynamic(obj);
633-
}
624+
kobject_put(&obj->kobj);
634625
}
635626
}
636627

@@ -675,10 +666,8 @@ static void klp_free_patch_finish(struct klp_patch *patch)
675666
* this is called when the patch gets disabled and it
676667
* cannot get enabled again.
677668
*/
678-
if (patch->kobj_added) {
679-
kobject_put(&patch->kobj);
680-
wait_for_completion(&patch->finish);
681-
}
669+
kobject_put(&patch->kobj);
670+
wait_for_completion(&patch->finish);
682671

683672
/* Put the module after the last access to struct klp_patch. */
684673
if (!patch->forced)
@@ -700,8 +689,6 @@ static void klp_free_patch_work_fn(struct work_struct *work)
700689

701690
static int klp_init_func(struct klp_object *obj, struct klp_func *func)
702691
{
703-
int ret;
704-
705692
if (!func->old_name)
706693
return -EINVAL;
707694

@@ -724,13 +711,9 @@ static int klp_init_func(struct klp_object *obj, struct klp_func *func)
724711
* object. If the user selects 0 for old_sympos, then 1 will be used
725712
* since a unique symbol will be the first occurrence.
726713
*/
727-
ret = kobject_init_and_add(&func->kobj, &klp_ktype_func,
728-
&obj->kobj, "%s,%lu", func->old_name,
729-
func->old_sympos ? func->old_sympos : 1);
730-
if (!ret)
731-
func->kobj_added = true;
732-
733-
return ret;
714+
return kobject_add(&func->kobj, &obj->kobj, "%s,%lu",
715+
func->old_name,
716+
func->old_sympos ? func->old_sympos : 1);
734717
}
735718

736719
/* Arches may override this to finish any remaining arch-specific tasks */
@@ -801,11 +784,9 @@ static int klp_init_object(struct klp_patch *patch, struct klp_object *obj)
801784
klp_find_object_module(obj);
802785

803786
name = klp_is_module(obj) ? obj->name : "vmlinux";
804-
ret = kobject_init_and_add(&obj->kobj, &klp_ktype_object,
805-
&patch->kobj, "%s", name);
787+
ret = kobject_add(&obj->kobj, &patch->kobj, "%s", name);
806788
if (ret)
807789
return ret;
808-
obj->kobj_added = true;
809790

810791
klp_for_each_func(obj, func) {
811792
ret = klp_init_func(obj, func);
@@ -819,6 +800,21 @@ static int klp_init_object(struct klp_patch *patch, struct klp_object *obj)
819800
return ret;
820801
}
821802

803+
static void klp_init_func_early(struct klp_object *obj,
804+
struct klp_func *func)
805+
{
806+
kobject_init(&func->kobj, &klp_ktype_func);
807+
list_add_tail(&func->node, &obj->func_list);
808+
}
809+
810+
static void klp_init_object_early(struct klp_patch *patch,
811+
struct klp_object *obj)
812+
{
813+
INIT_LIST_HEAD(&obj->func_list);
814+
kobject_init(&obj->kobj, &klp_ktype_object);
815+
list_add_tail(&obj->node, &patch->obj_list);
816+
}
817+
822818
static int klp_init_patch_early(struct klp_patch *patch)
823819
{
824820
struct klp_object *obj;
@@ -829,7 +825,7 @@ static int klp_init_patch_early(struct klp_patch *patch)
829825

830826
INIT_LIST_HEAD(&patch->list);
831827
INIT_LIST_HEAD(&patch->obj_list);
832-
patch->kobj_added = false;
828+
kobject_init(&patch->kobj, &klp_ktype_patch);
833829
patch->enabled = false;
834830
patch->forced = false;
835831
INIT_WORK(&patch->free_work, klp_free_patch_work_fn);
@@ -839,13 +835,10 @@ static int klp_init_patch_early(struct klp_patch *patch)
839835
if (!obj->funcs)
840836
return -EINVAL;
841837

842-
INIT_LIST_HEAD(&obj->func_list);
843-
obj->kobj_added = false;
844-
list_add_tail(&obj->node, &patch->obj_list);
838+
klp_init_object_early(patch, obj);
845839

846840
klp_for_each_func_static(obj, func) {
847-
func->kobj_added = false;
848-
list_add_tail(&func->node, &obj->func_list);
841+
klp_init_func_early(obj, func);
849842
}
850843
}
851844

@@ -860,11 +853,9 @@ static int klp_init_patch(struct klp_patch *patch)
860853
struct klp_object *obj;
861854
int ret;
862855

863-
ret = kobject_init_and_add(&patch->kobj, &klp_ktype_patch,
864-
klp_root_kobj, "%s", patch->mod->name);
856+
ret = kobject_add(&patch->kobj, klp_root_kobj, "%s", patch->mod->name);
865857
if (ret)
866858
return ret;
867-
patch->kobj_added = true;
868859

869860
if (patch->replace) {
870861
ret = klp_add_nops(patch);
@@ -926,9 +917,6 @@ static int __klp_enable_patch(struct klp_patch *patch)
926917
if (WARN_ON(patch->enabled))
927918
return -EINVAL;
928919

929-
if (!patch->kobj_added)
930-
return -EINVAL;
931-
932920
pr_notice("enabling patch '%s'\n", patch->mod->name);
933921

934922
klp_init_transition(patch, KLP_PATCHED);
@@ -1003,11 +991,10 @@ int klp_enable_patch(struct klp_patch *patch)
1003991
return -ENODEV;
1004992

1005993
if (!klp_have_reliable_stack()) {
1006-
pr_err("This architecture doesn't have support for the livepatch consistency model.\n");
1007-
return -EOPNOTSUPP;
994+
pr_warn("This architecture doesn't have support for the livepatch consistency model.\n");
995+
pr_warn("The livepatch transition may never complete.\n");
1008996
}
1009997

1010-
1011998
mutex_lock(&klp_mutex);
1012999

10131000
ret = klp_init_patch_early(patch);

tools/testing/selftests/livepatch/Makefile

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# SPDX-License-Identifier: GPL-2.0
22

3-
TEST_GEN_PROGS := \
3+
TEST_PROGS_EXTENDED := functions.sh
4+
TEST_PROGS := \
45
test-livepatch.sh \
56
test-callbacks.sh \
67
test-shadow-vars.sh

0 commit comments

Comments
 (0)