Skip to content

Commit 0430f78

Browse files
pmladekJiri Kosina
authored andcommitted
livepatch: Consolidate klp_free functions
The code for freeing livepatch structures is a bit scattered and tricky: + direct calls to klp_free_*_limited() and kobject_put() are used to release partially initialized objects + klp_free_patch() removes the patch from the public list and releases all objects except for patch->kobj + object_put(&patch->kobj) and the related wait_for_completion() are called directly outside klp_mutex; this code is duplicated; Now, we are going to remove the registration stage to simplify the API and the code. This would require handling more situations in klp_enable_patch() error paths. More importantly, we are going to add a feature called atomic replace. It will need to dynamically create func and object structures. We will want to reuse the existing init() and free() functions. This would create even more error path scenarios. This patch implements more straightforward free functions: + checks kobj_added flag instead of @limit[*] + initializes patch->list early so that the check for empty list always works + The action(s) that has to be done outside klp_mutex are done in separate klp_free_patch_finish() function. It waits only when patch->kobj was really released via the _start() part. The patch does not change the existing behavior. [*] We need our own flag to track that the kobject was successfully added to the hierarchy. Note that kobj.state_initialized only indicates that kobject has been initialized, not whether is has been added (and needs to be removed on cleanup). Signed-off-by: Petr Mladek <pmladek@suse.com> Cc: Josh Poimboeuf <jpoimboe@redhat.com> Cc: Miroslav Benes <mbenes@suse.cz> Cc: Jessica Yu <jeyu@kernel.org> Cc: Jiri Kosina <jikos@kernel.org> Cc: Jason Baron <jbaron@akamai.com> Acked-by: Miroslav Benes <mbenes@suse.cz> Acked-by: Josh Poimboeuf <jpoimboe@redhat.com> Signed-off-by: Jiri Kosina <jkosina@suse.cz>
1 parent 26c3e98 commit 0430f78

2 files changed

Lines changed: 98 additions & 45 deletions

File tree

‎include/linux/livepatch.h‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
* @stack_node: list node for klp_ops func_stack list
4646
* @old_size: size of the old function
4747
* @new_size: size of the new function
48+
* @kobj_added: @kobj has been added and needs freeing
4849
* @patched: the func has been added to the klp_ops list
4950
* @transition: the func is currently being applied or reverted
5051
*
@@ -81,6 +82,7 @@ struct klp_func {
8182
struct kobject kobj;
8283
struct list_head stack_node;
8384
unsigned long old_size, new_size;
85+
bool kobj_added;
8486
bool patched;
8587
bool transition;
8688
};
@@ -117,6 +119,7 @@ struct klp_callbacks {
117119
* @kobj: kobject for sysfs resources
118120
* @mod: kernel module associated with the patched object
119121
* (NULL for vmlinux)
122+
* @kobj_added: @kobj has been added and needs freeing
120123
* @patched: the object's funcs have been added to the klp_ops list
121124
*/
122125
struct klp_object {
@@ -128,6 +131,7 @@ struct klp_object {
128131
/* internal */
129132
struct kobject kobj;
130133
struct module *mod;
134+
bool kobj_added;
131135
bool patched;
132136
};
133137

@@ -137,6 +141,7 @@ struct klp_object {
137141
* @objs: object entries for kernel objects to be patched
138142
* @list: list node for global list of registered patches
139143
* @kobj: kobject for sysfs resources
144+
* @kobj_added: @kobj has been added and needs freeing
140145
* @enabled: the patch is enabled (but operation may be incomplete)
141146
* @finish: for waiting till it is safe to remove the patch module
142147
*/
@@ -148,6 +153,7 @@ struct klp_patch {
148153
/* internal */
149154
struct list_head list;
150155
struct kobject kobj;
156+
bool kobj_added;
151157
bool enabled;
152158
struct completion finish;
153159
};

‎kernel/livepatch/core.c‎

Lines changed: 92 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -465,17 +465,15 @@ static struct kobj_type klp_ktype_func = {
465465
.sysfs_ops = &kobj_sysfs_ops,
466466
};
467467

468-
/*
469-
* Free all functions' kobjects in the array up to some limit. When limit is
470-
* NULL, all kobjects are freed.
471-
*/
472-
static void klp_free_funcs_limited(struct klp_object *obj,
473-
struct klp_func *limit)
468+
static void klp_free_funcs(struct klp_object *obj)
474469
{
475470
struct klp_func *func;
476471

477-
for (func = obj->funcs; func->old_name && func != limit; func++)
478-
kobject_put(&func->kobj);
472+
klp_for_each_func(obj, func) {
473+
/* Might be called from klp_init_patch() error path. */
474+
if (func->kobj_added)
475+
kobject_put(&func->kobj);
476+
}
479477
}
480478

481479
/* Clean up when a patched object is unloaded */
@@ -489,30 +487,60 @@ static void klp_free_object_loaded(struct klp_object *obj)
489487
func->old_func = NULL;
490488
}
491489

492-
/*
493-
* Free all objects' kobjects in the array up to some limit. When limit is
494-
* NULL, all kobjects are freed.
495-
*/
496-
static void klp_free_objects_limited(struct klp_patch *patch,
497-
struct klp_object *limit)
490+
static void klp_free_objects(struct klp_patch *patch)
498491
{
499492
struct klp_object *obj;
500493

501-
for (obj = patch->objs; obj->funcs && obj != limit; obj++) {
502-
klp_free_funcs_limited(obj, NULL);
503-
kobject_put(&obj->kobj);
494+
klp_for_each_object(patch, obj) {
495+
klp_free_funcs(obj);
496+
497+
/* Might be called from klp_init_patch() error path. */
498+
if (obj->kobj_added)
499+
kobject_put(&obj->kobj);
504500
}
505501
}
506502

507-
static void klp_free_patch(struct klp_patch *patch)
503+
/*
504+
* This function implements the free operations that can be called safely
505+
* under klp_mutex.
506+
*
507+
* The operation must be completed by calling klp_free_patch_finish()
508+
* outside klp_mutex.
509+
*/
510+
static void klp_free_patch_start(struct klp_patch *patch)
508511
{
509-
klp_free_objects_limited(patch, NULL);
510512
if (!list_empty(&patch->list))
511513
list_del(&patch->list);
514+
515+
klp_free_objects(patch);
516+
}
517+
518+
/*
519+
* This function implements the free part that must be called outside
520+
* klp_mutex.
521+
*
522+
* It must be called after klp_free_patch_start(). And it has to be
523+
* the last function accessing the livepatch structures when the patch
524+
* gets disabled.
525+
*/
526+
static void klp_free_patch_finish(struct klp_patch *patch)
527+
{
528+
/*
529+
* Avoid deadlock with enabled_store() sysfs callback by
530+
* calling this outside klp_mutex. It is safe because
531+
* this is called when the patch gets disabled and it
532+
* cannot get enabled again.
533+
*/
534+
if (patch->kobj_added) {
535+
kobject_put(&patch->kobj);
536+
wait_for_completion(&patch->finish);
537+
}
512538
}
513539

514540
static int klp_init_func(struct klp_object *obj, struct klp_func *func)
515541
{
542+
int ret;
543+
516544
if (!func->old_name || !func->new_func)
517545
return -EINVAL;
518546

@@ -528,9 +556,13 @@ static int klp_init_func(struct klp_object *obj, struct klp_func *func)
528556
* object. If the user selects 0 for old_sympos, then 1 will be used
529557
* since a unique symbol will be the first occurrence.
530558
*/
531-
return kobject_init_and_add(&func->kobj, &klp_ktype_func,
532-
&obj->kobj, "%s,%lu", func->old_name,
533-
func->old_sympos ? func->old_sympos : 1);
559+
ret = kobject_init_and_add(&func->kobj, &klp_ktype_func,
560+
&obj->kobj, "%s,%lu", func->old_name,
561+
func->old_sympos ? func->old_sympos : 1);
562+
if (!ret)
563+
func->kobj_added = true;
564+
565+
return ret;
534566
}
535567

536568
/* Arches may override this to finish any remaining arch-specific tasks */
@@ -589,9 +621,6 @@ static int klp_init_object(struct klp_patch *patch, struct klp_object *obj)
589621
int ret;
590622
const char *name;
591623

592-
if (!obj->funcs)
593-
return -EINVAL;
594-
595624
if (klp_is_module(obj) && strlen(obj->name) >= MODULE_NAME_LEN)
596625
return -EINVAL;
597626

@@ -605,46 +634,66 @@ static int klp_init_object(struct klp_patch *patch, struct klp_object *obj)
605634
&patch->kobj, "%s", name);
606635
if (ret)
607636
return ret;
637+
obj->kobj_added = true;
608638

609639
klp_for_each_func(obj, func) {
610640
ret = klp_init_func(obj, func);
611641
if (ret)
612-
goto free;
642+
return ret;
613643
}
614644

615-
if (klp_is_object_loaded(obj)) {
645+
if (klp_is_object_loaded(obj))
616646
ret = klp_init_object_loaded(patch, obj);
617-
if (ret)
618-
goto free;
619-
}
620-
621-
return 0;
622647

623-
free:
624-
klp_free_funcs_limited(obj, func);
625-
kobject_put(&obj->kobj);
626648
return ret;
627649
}
628650

629-
static int klp_init_patch(struct klp_patch *patch)
651+
static int klp_init_patch_early(struct klp_patch *patch)
630652
{
631653
struct klp_object *obj;
632-
int ret;
654+
struct klp_func *func;
633655

634656
if (!patch->objs)
635657
return -EINVAL;
636658

637-
mutex_lock(&klp_mutex);
638-
659+
INIT_LIST_HEAD(&patch->list);
660+
patch->kobj_added = false;
639661
patch->enabled = false;
640662
init_completion(&patch->finish);
641663

664+
klp_for_each_object(patch, obj) {
665+
if (!obj->funcs)
666+
return -EINVAL;
667+
668+
obj->kobj_added = false;
669+
670+
klp_for_each_func(obj, func)
671+
func->kobj_added = false;
672+
}
673+
674+
return 0;
675+
}
676+
677+
static int klp_init_patch(struct klp_patch *patch)
678+
{
679+
struct klp_object *obj;
680+
int ret;
681+
682+
mutex_lock(&klp_mutex);
683+
684+
ret = klp_init_patch_early(patch);
685+
if (ret) {
686+
mutex_unlock(&klp_mutex);
687+
return ret;
688+
}
689+
642690
ret = kobject_init_and_add(&patch->kobj, &klp_ktype_patch,
643691
klp_root_kobj, "%s", patch->mod->name);
644692
if (ret) {
645693
mutex_unlock(&klp_mutex);
646694
return ret;
647695
}
696+
patch->kobj_added = true;
648697

649698
klp_for_each_object(patch, obj) {
650699
ret = klp_init_object(patch, obj);
@@ -659,12 +708,11 @@ static int klp_init_patch(struct klp_patch *patch)
659708
return 0;
660709

661710
free:
662-
klp_free_objects_limited(patch, obj);
711+
klp_free_patch_start(patch);
663712

664713
mutex_unlock(&klp_mutex);
665714

666-
kobject_put(&patch->kobj);
667-
wait_for_completion(&patch->finish);
715+
klp_free_patch_finish(patch);
668716

669717
return ret;
670718
}
@@ -693,12 +741,11 @@ int klp_unregister_patch(struct klp_patch *patch)
693741
goto err;
694742
}
695743

696-
klp_free_patch(patch);
744+
klp_free_patch_start(patch);
697745

698746
mutex_unlock(&klp_mutex);
699747

700-
kobject_put(&patch->kobj);
701-
wait_for_completion(&patch->finish);
748+
klp_free_patch_finish(patch);
702749

703750
return 0;
704751
err:

0 commit comments

Comments
 (0)