public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
* [PATCH] livepatch: introduce klp_func called interface
@ 2024-05-20  0:58 Wardenjohn
  2024-05-20  6:46 ` Miroslav Benes
  2024-05-20  8:00 ` Markus Elfring
  0 siblings, 2 replies; 31+ messages in thread
From: Wardenjohn @ 2024-05-20  0:58 UTC (permalink / raw)
  To: jpoimboe, mbenes, jikos, pmladek, joe.lawrence
  Cc: live-patching, linux-kernel, Wardenjohn

Livepatch module usually used to modify kernel functions.
If the patched function have bug, it may cause serious result
such as kernel crash.

This is a kobject attribute of klp_func. Sysfs interface named
 "called" is introduced to livepatch which will be set as true
if the patched function is called.

/sys/kernel/livepatch/<patch>/<object>/<function,sympos>/called

This value "called" is quite necessary for kernel stability
assurance for livepatching module of a running system.
Testing process is important before a livepatch module apply to
a production system. With this interface, testing process can
easily find out which function is successfully called.
Any testing process can make sure they have successfully cover
all the patched function that changed with the help of this interface.

Signed-off-by: Wardenjohn <zhangwarden@gmail.com>
---
 include/linux/livepatch.h |  2 ++
 kernel/livepatch/core.c   | 18 ++++++++++++++++++
 kernel/livepatch/patch.c  |  2 ++
 3 files changed, 22 insertions(+)

diff --git a/include/linux/livepatch.h b/include/linux/livepatch.h
index 51a258c24ff5..026431825593 100644
--- a/include/linux/livepatch.h
+++ b/include/linux/livepatch.h
@@ -37,6 +37,7 @@
  * @nop:        temporary patch to use the original code again; dyn. allocated
  * @patched:	the func has been added to the klp_ops list
  * @transition:	the func is currently being applied or reverted
+ * @called:		the func is called
  *
  * The patched and transition variables define the func's patching state.  When
  * patching, a func is always in one of the following states:
@@ -75,6 +76,7 @@ struct klp_func {
 	bool nop;
 	bool patched;
 	bool transition;
+	bool called;
 };
 
 struct klp_object;
diff --git a/kernel/livepatch/core.c b/kernel/livepatch/core.c
index 52426665eecc..a840ddd41d00 100644
--- a/kernel/livepatch/core.c
+++ b/kernel/livepatch/core.c
@@ -470,6 +470,22 @@ static struct attribute *klp_object_attrs[] = {
 };
 ATTRIBUTE_GROUPS(klp_object);
 
+static ssize_t called_show(struct kobject *kobj,
+				struct kobj_attribute *attr, char *buf)
+{
+	struct klp_func *func;
+
+	func = container_of(kobj, struct klp_func, kobj);
+	return sysfs_emit(buf, "%d\n", func->called);
+}
+
+static struct kobj_attribute called_kobj_attr = __ATTR_RO(called);
+static struct attribute *klp_func_attrs[] = {
+	&called_kobj_attr.attr,
+	NULL,
+};
+ATTRIBUTE_GROUPS(klp_func);
+
 static void klp_free_object_dynamic(struct klp_object *obj)
 {
 	kfree(obj->name);
@@ -631,6 +647,7 @@ static void klp_kobj_release_func(struct kobject *kobj)
 static const struct kobj_type klp_ktype_func = {
 	.release = klp_kobj_release_func,
 	.sysfs_ops = &kobj_sysfs_ops,
+	.default_groups = klp_func_groups,
 };
 
 static void __klp_free_funcs(struct klp_object *obj, bool nops_only)
@@ -903,6 +920,7 @@ static int klp_init_object(struct klp_patch *patch, struct klp_object *obj)
 static void klp_init_func_early(struct klp_object *obj,
 				struct klp_func *func)
 {
+	func->called = false;
 	kobject_init(&func->kobj, &klp_ktype_func);
 	list_add_tail(&func->node, &obj->func_list);
 }
diff --git a/kernel/livepatch/patch.c b/kernel/livepatch/patch.c
index 90408500e5a3..75b9603a183f 100644
--- a/kernel/livepatch/patch.c
+++ b/kernel/livepatch/patch.c
@@ -118,6 +118,8 @@ static void notrace klp_ftrace_handler(unsigned long ip,
 	if (func->nop)
 		goto unlock;
 
+	if (!func->called)
+		func->called = true;
 	ftrace_regs_set_instruction_pointer(fregs, (unsigned long)func->new_func);
 
 unlock:
-- 
2.37.3


^ permalink raw reply related	[flat|nested] 31+ messages in thread
* [PATCH] livepatch: introduce klp_func called interface
@ 2024-05-19  7:43 Wardenjohn
  2024-05-19 18:05 ` Markus Elfring
  2024-05-23 14:22 ` Dan Carpenter
  0 siblings, 2 replies; 31+ messages in thread
From: Wardenjohn @ 2024-05-19  7:43 UTC (permalink / raw)
  To: jpoimboe, mbenes, jikos, pmladek, joe.lawrence
  Cc: live-patching, linux-kernel, Wardenjohn

Livepatch module usually used to modify kernel functions.
If the patched function have bug, it may cause serious result
such as kernel crash.

This commit introduce a read only interface of livepatch
sysfs interface. If a livepatch function is called, this
sysfs interface "called" of the patched function will
set to be 1.

/sys/kernel/livepatch/<patch>/<object>/<function,sympos>/called

This value "called" is quite necessary for kernel stability assurance for livepatching
module of a running system. Testing process is important before a livepatch module
apply to a production system. With this interface, testing process can easily
find out which function is successfully called. Any testing process can make sure they
have successfully cover all the patched function that changed with the help of this interface.
---
 include/linux/livepatch.h |  2 ++
 kernel/livepatch/core.c   | 18 ++++++++++++++++++
 kernel/livepatch/patch.c  |  2 ++
 3 files changed, 22 insertions(+)

diff --git a/include/linux/livepatch.h b/include/linux/livepatch.h
index 51a258c24ff5..026431825593 100644
--- a/include/linux/livepatch.h
+++ b/include/linux/livepatch.h
@@ -37,6 +37,7 @@
  * @nop:        temporary patch to use the original code again; dyn. allocated
  * @patched:	the func has been added to the klp_ops list
  * @transition:	the func is currently being applied or reverted
+ * @called:		the func is called
  *
  * The patched and transition variables define the func's patching state.  When
  * patching, a func is always in one of the following states:
@@ -75,6 +76,7 @@ struct klp_func {
 	bool nop;
 	bool patched;
 	bool transition;
+	bool called;
 };
 
 struct klp_object;
diff --git a/kernel/livepatch/core.c b/kernel/livepatch/core.c
index 52426665eecc..bc055b56dbe5 100644
--- a/kernel/livepatch/core.c
+++ b/kernel/livepatch/core.c
@@ -470,6 +470,22 @@ static struct attribute *klp_object_attrs[] = {
 };
 ATTRIBUTE_GROUPS(klp_object);
 
+static ssize_t called_show(struct kobject *kobj,
+				struct kobj_attribute *attr, char *buf)
+{
+	struct klp_func *func;
+	
+	func = container_of(kobj, struct klp_func, kobj);
+	return sysfs_emit(buf, "%d\n", func->called);
+}
+
+static struct kobj_attribute called_kobj_attr = __ATTR_RO(called);
+static struct attribute *klp_func_attrs[] = {
+	&called_kobj_attr.attr,
+	NULL,
+};
+ATTRIBUTE_GROUPS(klp_func);
+
 static void klp_free_object_dynamic(struct klp_object *obj)
 {
 	kfree(obj->name);
@@ -631,6 +647,7 @@ static void klp_kobj_release_func(struct kobject *kobj)
 static const struct kobj_type klp_ktype_func = {
 	.release = klp_kobj_release_func,
 	.sysfs_ops = &kobj_sysfs_ops,
+	.default_groups = klp_func_groups,
 };
 
 static void __klp_free_funcs(struct klp_object *obj, bool nops_only)
@@ -903,6 +920,7 @@ static int klp_init_object(struct klp_patch *patch, struct klp_object *obj)
 static void klp_init_func_early(struct klp_object *obj,
 				struct klp_func *func)
 {
+	func->called = 0;
 	kobject_init(&func->kobj, &klp_ktype_func);
 	list_add_tail(&func->node, &obj->func_list);
 }
diff --git a/kernel/livepatch/patch.c b/kernel/livepatch/patch.c
index 90408500e5a3..75b9603a183f 100644
--- a/kernel/livepatch/patch.c
+++ b/kernel/livepatch/patch.c
@@ -118,6 +118,8 @@ static void notrace klp_ftrace_handler(unsigned long ip,
 	if (func->nop)
 		goto unlock;
 
+	if (!func->called)
+		func->called = true;
 	ftrace_regs_set_instruction_pointer(fregs, (unsigned long)func->new_func);
 
 unlock:
-- 
2.37.3


^ permalink raw reply related	[flat|nested] 31+ messages in thread

end of thread, other threads:[~2024-06-20  9:31 UTC | newest]

Thread overview: 31+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2024-05-20  0:58 [PATCH] livepatch: introduce klp_func called interface Wardenjohn
2024-05-20  6:46 ` Miroslav Benes
2024-05-20  7:10   ` zhang warden
2024-05-21  6:34     ` Miroslav Benes
2024-05-21  8:04       ` Petr Mladek
2024-05-24  8:09         ` zhang warden
2024-05-31  2:16         ` zhang warden
2024-05-31  7:21           ` Miroslav Benes
2024-05-31 13:19             ` zhang warden
2024-05-31 14:06               ` Miroslav Benes
2024-06-04  8:17                 ` zhang warden
2024-06-05  5:04         ` Song Liu
2024-06-07  9:07           ` Miroslav Benes
2024-06-07 16:59             ` Song Liu
2024-06-11  2:23               ` zhang warden
2024-06-20  3:39             ` zhang warden
2024-06-20  9:31               ` Miroslav Benes
2024-05-31 19:16       ` Joe Lawrence
2024-06-04  8:14         ` zhang warden
2024-06-04 11:01           ` zhang warden
2024-06-04 14:37           ` Joe Lawrence
2024-06-05  2:38             ` zhang warden
2024-06-06 15:01               ` Joe Lawrence
2024-06-07  2:26                 ` zhang warden
2024-05-20  8:00 ` Markus Elfring
2024-05-20  9:34   ` zhang warden
  -- strict thread matches above, loose matches on Subject: below --
2024-05-19  7:43 Wardenjohn
2024-05-19 18:05 ` Markus Elfring
2024-05-19 23:42   ` zhang warden
2024-05-23 14:22 ` Dan Carpenter
2024-05-24  8:12   ` zhang warden

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox