Skip to content

Commit f68d67c

Browse files
pmladekJiri Kosina
authored andcommitted
livepatch: Remove duplicated code for early initialization
kobject_init() call added one more operation that has to be done when doing the early initialization of both static and dynamic livepatch structures. It would have been easier when the early initialization code was not duplicated. Let's deduplicate it for future generations of livepatching hackers. The patch does not change the existing behavior. Signed-off-by: Petr Mladek <[email protected]> Reviewed-by: Kamalesh Babulal <[email protected]> Acked-by: Joe Lawrence <[email protected]> Signed-off-by: Jiri Kosina <[email protected]>
1 parent 4d141ab commit f68d67c

File tree

1 file changed

+26
-16
lines changed

1 file changed

+26
-16
lines changed

kernel/livepatch/core.c

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

429-
static struct kobj_type klp_ktype_object;
430-
static struct kobj_type klp_ktype_func;
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);
431433

432-
static struct klp_object *klp_alloc_object_dynamic(const char *name)
434+
static struct klp_object *klp_alloc_object_dynamic(const char *name,
435+
struct klp_patch *patch)
433436
{
434437
struct klp_object *obj;
435438

@@ -445,8 +448,7 @@ static struct klp_object *klp_alloc_object_dynamic(const char *name)
445448
}
446449
}
447450

448-
INIT_LIST_HEAD(&obj->func_list);
449-
kobject_init(&obj->kobj, &klp_ktype_object);
451+
klp_init_object_early(patch, obj);
450452
obj->dynamic = true;
451453

452454
return obj;
@@ -475,7 +477,7 @@ static struct klp_func *klp_alloc_func_nop(struct klp_func *old_func,
475477
}
476478
}
477479

478-
kobject_init(&func->kobj, &klp_ktype_func);
480+
klp_init_func_early(obj, func);
479481
/*
480482
* func->new_func is same as func->old_func. These addresses are
481483
* set when the object is loaded, see klp_init_object_loaded().
@@ -495,11 +497,9 @@ static int klp_add_object_nops(struct klp_patch *patch,
495497
obj = klp_find_object(patch, old_obj);
496498

497499
if (!obj) {
498-
obj = klp_alloc_object_dynamic(old_obj->name);
500+
obj = klp_alloc_object_dynamic(old_obj->name, patch);
499501
if (!obj)
500502
return -ENOMEM;
501-
502-
list_add_tail(&obj->node, &patch->obj_list);
503503
}
504504

505505
klp_for_each_func(old_obj, old_func) {
@@ -510,8 +510,6 @@ static int klp_add_object_nops(struct klp_patch *patch,
510510
func = klp_alloc_func_nop(old_func, obj);
511511
if (!func)
512512
return -ENOMEM;
513-
514-
list_add_tail(&func->node, &obj->func_list);
515513
}
516514

517515
return 0;
@@ -802,6 +800,21 @@ static int klp_init_object(struct klp_patch *patch, struct klp_object *obj)
802800
return ret;
803801
}
804802

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+
805818
static int klp_init_patch_early(struct klp_patch *patch)
806819
{
807820
struct klp_object *obj;
@@ -822,13 +835,10 @@ static int klp_init_patch_early(struct klp_patch *patch)
822835
if (!obj->funcs)
823836
return -EINVAL;
824837

825-
INIT_LIST_HEAD(&obj->func_list);
826-
kobject_init(&obj->kobj, &klp_ktype_object);
827-
list_add_tail(&obj->node, &patch->obj_list);
838+
klp_init_object_early(patch, obj);
828839

829840
klp_for_each_func_static(obj, func) {
830-
kobject_init(&func->kobj, &klp_ktype_func);
831-
list_add_tail(&func->node, &obj->func_list);
841+
klp_init_func_early(obj, func);
832842
}
833843
}
834844

0 commit comments

Comments
 (0)