[PATCH] Restructure of klp_init_* functions

From: Miroslav Benes
Date: Tue Dec 02 2014 - 08:34:21 EST


Structure of klp_init_{patch|objects|funcs} functions could be a bit hard to
read. We can move the loops in klp_init_objects and klp_init_funcs one level up
to klp_init_patch and klp_init_objects respectively. Thus klp_init_objects
become klp_init_object and similarly klp_init_funcs is klp_init_func now.

This change also makes it symmetric with the naming scheme used for
klp_{enable|disable}_* functions.

Signed-off-by: Miroslav Benes <mbenes@xxxxxxx>
---
kernel/livepatch/core.c | 88 +++++++++++++++++++++----------------------------
1 file changed, 37 insertions(+), 51 deletions(-)

diff --git a/kernel/livepatch/core.c b/kernel/livepatch/core.c
index 8e2e8cd..4ffc281 100644
--- a/kernel/livepatch/core.c
+++ b/kernel/livepatch/core.c
@@ -627,103 +627,89 @@ static void klp_free_patch(struct klp_patch *patch)
kobject_put(&patch->kobj);
}

-static int klp_init_funcs(struct klp_object *obj)
+static int klp_init_func(struct klp_func *func, struct klp_object *obj)
{
- struct klp_func *func;
struct ftrace_ops *ops;
int ret;

- if (!obj->funcs)
- return -EINVAL;
-
- for (func = obj->funcs; func->old_name; func++) {
- func->state = KLP_DISABLED;
+ func->state = KLP_DISABLED;

- ops = kzalloc(sizeof(*ops), GFP_KERNEL);
- if (!ops) {
- ret = -ENOMEM;
- goto free;
- }
- ops->private = func;
- ops->func = klp_ftrace_handler;
- ops->flags = FTRACE_OPS_FL_SAVE_REGS | FTRACE_OPS_FL_DYNAMIC;
+ ops = kzalloc(sizeof(*ops), GFP_KERNEL);
+ if (!ops)
+ return -ENOMEM;

- /* sysfs */
- ret = kobject_init_and_add(&func->kobj, &klp_ktype_func,
- obj->kobj, func->old_name);
- if (ret) {
- kfree(ops);
- goto free;
- }
+ ops->private = func;
+ ops->func = klp_ftrace_handler;
+ ops->flags = FTRACE_OPS_FL_SAVE_REGS | FTRACE_OPS_FL_DYNAMIC;
+ func->fops = ops;

- func->fops = ops;
+ ret = kobject_init_and_add(&func->kobj, &klp_ktype_func,
+ obj->kobj, func->old_name);
+ if (ret) {
+ kfree(func->fops);
+ return ret;
}

return 0;
-free:
- klp_free_funcs_limited(obj, func);
- return ret;
}

-static int klp_init_objects(struct klp_patch *patch)
+static int klp_init_object(struct klp_object *obj, struct klp_patch *patch)
{
- struct klp_object *obj;
+ struct klp_func *func;
int ret;

- if (!patch->objs)
+ if (!obj->funcs)
return -EINVAL;

- for (obj = patch->objs; obj->funcs; obj++) {
- /* obj->mod set by klp_object_module_get() */
- obj->state = KLP_DISABLED;
+ /* obj->mod set by klp_object_module_get() */
+ obj->state = KLP_DISABLED;

- /* sysfs */
- obj->kobj = kobject_create_and_add(obj->name, &patch->kobj);
- if (!obj->kobj)
- goto free;
+ obj->kobj = kobject_create_and_add(obj->name, &patch->kobj);
+ if (!obj->kobj)
+ return -ENOMEM;

- /* init functions */
- ret = klp_init_funcs(obj);
- if (ret) {
- kobject_put(obj->kobj);
+ for (func = obj->funcs; func->old_name; func++) {
+ ret = klp_init_func(func, obj);
+ if (ret)
goto free;
- }
}

return 0;
free:
- klp_free_objects_limited(patch, obj);
- return -ENOMEM;
+ klp_free_funcs_limited(obj, func);
+ return ret;
}

static int klp_init_patch(struct klp_patch *patch)
{
+ struct klp_object *obj;
int ret;

- if (!patch)
+ if (!patch || !patch->objs)
return -EINVAL;

- /* init */
patch->state = KLP_DISABLED;

- /* sysfs */
ret = kobject_init_and_add(&patch->kobj, &klp_ktype_patch,
klp_root_kobj, patch->mod->name);
if (ret)
return ret;

- ret = klp_init_objects(patch);
- if (ret) {
- kobject_put(&patch->kobj);
- return ret;
+ for (obj = patch->objs; obj->funcs; obj++) {
+ ret = klp_init_object(obj, patch);
+ if (ret)
+ goto free;
}

- /* add to global list of patches */
mutex_lock(&klp_mutex);
list_add(&patch->list, &klp_patches);
mutex_unlock(&klp_mutex);

return 0;
+free:
+ klp_free_objects_limited(patch, obj);
+ kobject_put(&patch->kobj);
+ return ret;
}

/**
--
2.1.2
--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/