Skip to content

Commit 4d141ab

Browse files
pmladekJiri Kosina
authored andcommitted
livepatch: Remove custom kobject state handling
kobject_init() always succeeds and sets the reference count to 1. It allows to always free the structures via kobject_put() and the related release callback. Note that the custom kobject state handling was used only because we did not know that kobject_put() can and actually should get called even when kobject_init_and_add() fails. The patch should not change the existing behavior. Suggested-by: "Tobin C. Harding" <tobin@kernel.org> Signed-off-by: Petr Mladek <pmladek@suse.com> Reviewed-by: Kamalesh Babulal <kamalesh@linux.vnet.ibm.com> Acked-by: Joe Lawrence <joe.lawrence@redhat.com> Signed-off-by: Jiri Kosina <jkosina@suse.cz>
1 parent 31adf23 commit 4d141ab

2 files changed

Lines changed: 17 additions & 42 deletions

File tree

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: 17 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -426,6 +426,9 @@ 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;
431+
429432
static struct klp_object *klp_alloc_object_dynamic(const char *name)
430433
{
431434
struct klp_object *obj;
@@ -443,6 +446,7 @@ static struct klp_object *klp_alloc_object_dynamic(const char *name)
443446
}
444447

445448
INIT_LIST_HEAD(&obj->func_list);
449+
kobject_init(&obj->kobj, &klp_ktype_object);
446450
obj->dynamic = true;
447451

448452
return obj;
@@ -471,6 +475,7 @@ static struct klp_func *klp_alloc_func_nop(struct klp_func *old_func,
471475
}
472476
}
473477

478+
kobject_init(&func->kobj, &klp_ktype_func);
474479
/*
475480
* func->new_func is same as func->old_func. These addresses are
476481
* set when the object is loaded, see klp_init_object_loaded().
@@ -588,13 +593,7 @@ static void __klp_free_funcs(struct klp_object *obj, bool nops_only)
588593
continue;
589594

590595
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-
}
596+
kobject_put(&func->kobj);
598597
}
599598
}
600599

@@ -624,13 +623,7 @@ static void __klp_free_objects(struct klp_patch *patch, bool nops_only)
624623
continue;
625624

626625
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-
}
626+
kobject_put(&obj->kobj);
634627
}
635628
}
636629

@@ -675,10 +668,8 @@ static void klp_free_patch_finish(struct klp_patch *patch)
675668
* this is called when the patch gets disabled and it
676669
* cannot get enabled again.
677670
*/
678-
if (patch->kobj_added) {
679-
kobject_put(&patch->kobj);
680-
wait_for_completion(&patch->finish);
681-
}
671+
kobject_put(&patch->kobj);
672+
wait_for_completion(&patch->finish);
682673

683674
/* Put the module after the last access to struct klp_patch. */
684675
if (!patch->forced)
@@ -700,8 +691,6 @@ static void klp_free_patch_work_fn(struct work_struct *work)
700691

701692
static int klp_init_func(struct klp_object *obj, struct klp_func *func)
702693
{
703-
int ret;
704-
705694
if (!func->old_name)
706695
return -EINVAL;
707696

@@ -724,13 +713,9 @@ static int klp_init_func(struct klp_object *obj, struct klp_func *func)
724713
* object. If the user selects 0 for old_sympos, then 1 will be used
725714
* since a unique symbol will be the first occurrence.
726715
*/
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;
716+
return kobject_add(&func->kobj, &obj->kobj, "%s,%lu",
717+
func->old_name,
718+
func->old_sympos ? func->old_sympos : 1);
734719
}
735720

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

803788
name = klp_is_module(obj) ? obj->name : "vmlinux";
804-
ret = kobject_init_and_add(&obj->kobj, &klp_ktype_object,
805-
&patch->kobj, "%s", name);
789+
ret = kobject_add(&obj->kobj, &patch->kobj, "%s", name);
806790
if (ret)
807791
return ret;
808-
obj->kobj_added = true;
809792

810793
klp_for_each_func(obj, func) {
811794
ret = klp_init_func(obj, func);
@@ -829,7 +812,7 @@ static int klp_init_patch_early(struct klp_patch *patch)
829812

830813
INIT_LIST_HEAD(&patch->list);
831814
INIT_LIST_HEAD(&patch->obj_list);
832-
patch->kobj_added = false;
815+
kobject_init(&patch->kobj, &klp_ktype_patch);
833816
patch->enabled = false;
834817
patch->forced = false;
835818
INIT_WORK(&patch->free_work, klp_free_patch_work_fn);
@@ -840,11 +823,11 @@ static int klp_init_patch_early(struct klp_patch *patch)
840823
return -EINVAL;
841824

842825
INIT_LIST_HEAD(&obj->func_list);
843-
obj->kobj_added = false;
826+
kobject_init(&obj->kobj, &klp_ktype_object);
844827
list_add_tail(&obj->node, &patch->obj_list);
845828

846829
klp_for_each_func_static(obj, func) {
847-
func->kobj_added = false;
830+
kobject_init(&func->kobj, &klp_ktype_func);
848831
list_add_tail(&func->node, &obj->func_list);
849832
}
850833
}
@@ -860,11 +843,9 @@ static int klp_init_patch(struct klp_patch *patch)
860843
struct klp_object *obj;
861844
int ret;
862845

863-
ret = kobject_init_and_add(&patch->kobj, &klp_ktype_patch,
864-
klp_root_kobj, "%s", patch->mod->name);
846+
ret = kobject_add(&patch->kobj, klp_root_kobj, "%s", patch->mod->name);
865847
if (ret)
866848
return ret;
867-
patch->kobj_added = true;
868849

869850
if (patch->replace) {
870851
ret = klp_add_nops(patch);
@@ -926,9 +907,6 @@ static int __klp_enable_patch(struct klp_patch *patch)
926907
if (WARN_ON(patch->enabled))
927908
return -EINVAL;
928909

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

934912
klp_init_transition(patch, KLP_PATCHED);

0 commit comments

Comments
 (0)