Skip to contentSkip to conversation

livepatch: Remove custom kobject state handling and duplicated code

PATCH Applied Petr Mladek 6 participants 9 messages May 3, 2019 – May 7, 2019 Live patching

lore
Share & tools
lore

Petr Mladek

author maintainer
#0 lore raw

cover livepatch: Remove custom kobject state handling and duplicated code

Tobin's patch[1] uncovered that the livepatching code handles kobjects a too complicated way.

The first patch removes the unnecessary custom kobject state handling.

The second patch is an optional code deduplication. I did something similar already when introducing the atomic replace. But it was not considered worth it. There are more duplicated things now...

[1] https://lkml.kernel.org/r/[email protected]

Petr Mladek (2):
livepatch: Remove custom kobject state handling
livepatch: Remove duplicated code for early initialization

 include/linux/livepatch.h |  3 --
 kernel/livepatch/core.c   | 86 ++++++++++++++++++++---------------------------
 2 files changed, 37 insertions(+), 52 deletions(-)
Signature · 1 line
2.16.4
author maintainer
#1 lore raw

PATCH 1/2 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.

---
 include/linux/livepatch.h |  3 ---
 kernel/livepatch/core.c   | 56 ++++++++++++++---------------------------------
 2 files changed, 17 insertions(+), 42 deletions(-)
include/linux/livepatch.h +0 -3
index 53551f470722..a14bab1a0a3e 100644
@@ -86,7 +86,6 @@ struct klp_func {
 	struct list_head node;
 	struct list_head stack_node;
 	unsigned long old_size, new_size;
-	bool kobj_added;
 	bool nop;
 	bool patched;
 	bool transition;
@@ -141,7 +140,6 @@ struct klp_object {
 	struct list_head func_list;
 	struct list_head node;
 	struct module *mod;
-	bool kobj_added;
 	bool dynamic;
 	bool patched;
 };
@@ -170,7 +168,6 @@ struct klp_patch {
 	struct list_head list;
 	struct kobject kobj;
 	struct list_head obj_list;
-	bool kobj_added;
 	bool enabled;
 	bool forced;
 	struct work_struct free_work;
kernel/livepatch/core.c +17 -39

141 lines not on this page · raw message

Signature · 1 line
2.16.4
Reviewed-by on v1 1/2
#2 lore raw

Re: [PATCH 1/2] livepatch: Remove custom kobject state handling

Quoted 22 lines · Petr Mladek

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.

---
 include/linux/livepatch.h |  3 ---
 kernel/livepatch/core.c   | 56 ++++++++++++++---------------------------------
 2 files changed, 17 insertions(+), 42 deletions(-)
include/linux/livepatch.h +0 -0
index 53551f470722..a14bab1a0a3e 100644

include/linux/livepatch.h struct klp_func { · line 91

Quoted 7 lines
include/linux/livepatch.h +0 -1
@@ -86,7 +86,6 @@ struct klp_func {
 	struct list_head node;
 	struct list_head stack_node;
 	unsigned long old_size, new_size;
-	bool kobj_added;
 	bool nop;
 	bool patched;
 	bool transition;

Minor nitpick, the description of kobj_added needs to be removed from structure descriptions.

Signature · 1 line
Kamalesh
maintainer
#3 lore raw

Re: [PATCH 1/2] livepatch: Remove custom kobject state handling

In reply to Petr Mladek
Quoted 9 lines · Petr Mladek

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.

Pity that the changelog does not describe the change from kobject_init_and_add() to two-stage kobject init (separate kobject_init() and kobject_add()).

Petr changed it, because now each member of new dynamic lists (created in klp_init_patch_early()) is initialized with kobject_init(), so we do not have to worry about calling kobject_put() (this is slightly different from kobj_added).

It would also be possible to retain kobject_init_and_add() and move it to klp_init_patch_early(), but it would be uglier in my opinion.

Miroslav

#4 lore raw

Re: [PATCH 1/2] livepatch: Remove custom kobject state handling

Quoted 16 lines · Miroslav Benes
Quoted 9 lines · Petr Mladek

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.

Pity that the changelog does not describe the change from kobject_init_and_add() to two-stage kobject init (separate kobject_init() and kobject_add()).

Quoted 7 lines

Petr changed it, because now each member of new dynamic lists (created in klp_init_patch_early()) is initialized with kobject_init(), so we do not have to worry about calling kobject_put() (this is slightly different from kobj_added).

It would also be possible to retain kobject_init_and_add() and move it to klp_init_patch_early(), but it would be uglier in my opinion.

kobject_init_and_add() is only there for the "simple" use cases. There's no problem with doing the two-stage process on your own like this, that's exactly what it is there for :)

thanks,

greg k-h

author maintainer
#5 lore raw

PATCH 2/2 livepatch: Remove duplicated code for early initialization

In reply to Petr Mladek

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.

---
 kernel/livepatch/core.c | 42 ++++++++++++++++++++++++++----------------
 1 file changed, 26 insertions(+), 16 deletions(-)
kernel/livepatch/core.c +26 -16
index 1ff91f7cbafb..0ec6ce8691b8 100644
@@ -426,10 +426,13 @@ static void klp_free_object_dynamic(struct klp_object *obj)
 	kfree(obj);
 }
 
-static struct kobj_type klp_ktype_object;
-static struct kobj_type klp_ktype_func;
+static void klp_init_func_early(struct klp_object *obj,
+				struct klp_func *func);
+static void klp_init_object_early(struct klp_patch *patch,
+				  struct klp_object *obj);
 
-static struct klp_object *klp_alloc_object_dynamic(const char *name)
+static struct klp_object *klp_alloc_object_dynamic(const char *name,
+						   struct klp_patch *patch)
 {
 	struct klp_object *obj;
 
@@ -445,8 +448,7 @@ static struct klp_object *klp_alloc_object_dynamic(const char *name)
 		}
 	}
 
-	INIT_LIST_HEAD(&obj->func_list);
-	kobject_init(&obj->kobj, &klp_ktype_object);
+	klp_init_object_early(patch, obj);
 	obj->dynamic = true;
 
 	return obj;
@@ -475,7 +477,7 @@ static struct klp_func *klp_alloc_func_nop(struct klp_func *old_func,
 		}
 	}
 
-	kobject_init(&func->kobj, &klp_ktype_func);
+	klp_init_func_early(obj, func);
 	/*
 	 * func->new_func is same as func->old_func. These addresses are
 	 * 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,
 	obj = klp_find_object(patch, old_obj);
 
 	if (!obj) {
-		obj = klp_alloc_object_dynamic(old_obj->name);
+		obj = klp_alloc_object_dynamic(old_obj->name, patch);
 		if (!obj)
 			return -ENOMEM;
-
-		list_add_tail(&obj->node, &patch->obj_list);
 	}
 
 	klp_for_each_func(old_obj, old_func) {
@@ -510,8 +510,6 @@ static int klp_add_object_nops(struct klp_patch *patch,
 		func = klp_alloc_func_nop(old_func, obj);
 		if (!func)
 			return -ENOMEM;
-
-		list_add_tail(&func->node, &obj->func_list);
 	}
 
 	return 0;
@@ -802,6 +800,21 @@ static int klp_init_object(struct klp_patch *patch, struct klp_object *obj)
 	return ret;
 }
 
+static void klp_init_func_early(struct klp_object *obj,
+				struct klp_func *func)
+{
+	kobject_init(&func->kobj, &klp_ktype_func);
+	list_add_tail(&func->node, &obj->func_list);
+}
+
+static void klp_init_object_early(struct klp_patch *patch,
+				  struct klp_object *obj)
+{
+	INIT_LIST_HEAD(&obj->func_list);
+	kobject_init(&obj->kobj, &klp_ktype_object);
+	list_add_tail(&obj->node, &patch->obj_list);
+}
+
 static int klp_init_patch_early(struct klp_patch *patch)
 {
 	struct klp_object *obj;
@@ -822,13 +835,10 @@ static int klp_init_patch_early(struct klp_patch *patch)
 		if (!obj->funcs)
 			return -EINVAL;
 
-		INIT_LIST_HEAD(&obj->func_list);
-		kobject_init(&obj->kobj, &klp_ktype_object);
-		list_add_tail(&obj->node, &patch->obj_list);
+		klp_init_object_early(patch, obj);
 
 		klp_for_each_func_static(obj, func) {
-			kobject_init(&func->kobj, &klp_ktype_func);
-			list_add_tail(&func->node, &obj->func_list);
+			klp_init_func_early(obj, func);
 		}
 	}
 
Signature · 1 line
2.16.4
Reviewed-by on v1 2/2
#6 lore raw

Re: [PATCH 2/2] livepatch: Remove duplicated code for early initialization

Quoted 11 lines · Petr Mladek

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.

Signature · 1 line
Kamalesh
maintainer Acked-by on the v1 series
#7 lore raw
In reply to Petr Mladek
Quoted 12 lines · Petr Mladek

Tobin's patch[1] uncovered that the livepatching code handles kobjects a too complicated way.

The first patch removes the unnecessary custom kobject state handling.

The second patch is an optional code deduplication. I did something similar already when introducing the atomic replace. But it was not considered worth it. There are more duplicated things now...

[1] https://lkml.kernel.org/r/[email protected]

Quoted 11 lines

Petr Mladek (2):
livepatch: Remove custom kobject state handling
livepatch: Remove duplicated code for early initialization

 include/linux/livepatch.h |  3 --
 kernel/livepatch/core.c   | 86 ++++++++++++++++++++---------------------------
 2 files changed, 37 insertions(+), 52 deletions(-)
Signature · 1 line
2.16.4

For the series,

-- Joe

Applied
#8 lore raw
In reply to Petr Mladek
Quoted 9 lines · Petr Mladek

Tobin's patch[1] uncovered that the livepatching code handles kobjects a too complicated way.

The first patch removes the unnecessary custom kobject state handling.

The second patch is an optional code deduplication. I did something similar already when introducing the atomic replace. But it was not considered worth it. There are more duplicated things now...

Quoted 6 lines

[1] https://lkml.kernel.org/r/[email protected]

Petr Mladek (2):
livepatch: Remove custom kobject state handling
livepatch: Remove duplicated code for early initialization

I've applied this to for-5.2/core. Thanks,

Signature · 2 lines
Jiri Kosina
SUSE Labs