* [PATCH v3 1/3] livepatch: Add "replace" sysfs attribute
2024-06-25 15:11 [PATCH v3 0/3] livepatch: Add "replace" sysfs attribute Yafang Shao
@ 2024-06-25 15:11 ` Yafang Shao
2024-06-25 15:11 ` [PATCH v3 2/3] selftests/livepatch: Add selftests for " Yafang Shao
` (3 subsequent siblings)
4 siblings, 0 replies; 6+ messages in thread
From: Yafang Shao @ 2024-06-25 15:11 UTC (permalink / raw)
To: jpoimboe, jikos, mbenes, pmladek, joe.lawrence, song, mpdesouza
Cc: live-patching, Yafang Shao
There are situations when it might make sense to combine livepatches
with and without the atomic replace on the same system. For example,
the livepatch without the atomic replace might provide a hotfix
or extra tuning.
Managing livepatches on such systems might be challenging. And the
information which of the installed livepatches do not use the atomic
replace would be useful.
Add new sysfs interface 'replace'. It works as follows:
$ cat /sys/kernel/livepatch/livepatch-non_replace/replace
0
$ cat /sys/kernel/livepatch/livepatch-replace/replace
1
[ commit log improved by Petr ]
Signed-off-by: Yafang Shao <laoar.shao@gmail.com>
Reviewed-by: Petr Mladek <pmladek@suse.com>
---
Documentation/ABI/testing/sysfs-kernel-livepatch | 8 ++++++++
kernel/livepatch/core.c | 12 ++++++++++++
2 files changed, 20 insertions(+)
diff --git a/Documentation/ABI/testing/sysfs-kernel-livepatch b/Documentation/ABI/testing/sysfs-kernel-livepatch
index a5df9b4910dc..3735d868013d 100644
--- a/Documentation/ABI/testing/sysfs-kernel-livepatch
+++ b/Documentation/ABI/testing/sysfs-kernel-livepatch
@@ -47,6 +47,14 @@ Description:
disabled when the feature is used. See
Documentation/livepatch/livepatch.rst for more information.
+What: /sys/kernel/livepatch/<patch>/replace
+Date: Jun 2024
+KernelVersion: 6.11.0
+Contact: live-patching@vger.kernel.org
+Description:
+ An attribute which indicates whether the patch supports
+ atomic-replace.
+
What: /sys/kernel/livepatch/<patch>/<object>
Date: Nov 2014
KernelVersion: 3.19.0
diff --git a/kernel/livepatch/core.c b/kernel/livepatch/core.c
index 52426665eecc..ad28617bfd75 100644
--- a/kernel/livepatch/core.c
+++ b/kernel/livepatch/core.c
@@ -346,6 +346,7 @@ int klp_apply_section_relocs(struct module *pmod, Elf_Shdr *sechdrs,
* /sys/kernel/livepatch/<patch>/enabled
* /sys/kernel/livepatch/<patch>/transition
* /sys/kernel/livepatch/<patch>/force
+ * /sys/kernel/livepatch/<patch>/replace
* /sys/kernel/livepatch/<patch>/<object>
* /sys/kernel/livepatch/<patch>/<object>/patched
* /sys/kernel/livepatch/<patch>/<object>/<function,sympos>
@@ -443,13 +444,24 @@ static ssize_t force_store(struct kobject *kobj, struct kobj_attribute *attr,
return count;
}
+static ssize_t replace_show(struct kobject *kobj,
+ struct kobj_attribute *attr, char *buf)
+{
+ struct klp_patch *patch;
+
+ patch = container_of(kobj, struct klp_patch, kobj);
+ return sysfs_emit(buf, "%d\n", patch->replace);
+}
+
static struct kobj_attribute enabled_kobj_attr = __ATTR_RW(enabled);
static struct kobj_attribute transition_kobj_attr = __ATTR_RO(transition);
static struct kobj_attribute force_kobj_attr = __ATTR_WO(force);
+static struct kobj_attribute replace_kobj_attr = __ATTR_RO(replace);
static struct attribute *klp_patch_attrs[] = {
&enabled_kobj_attr.attr,
&transition_kobj_attr.attr,
&force_kobj_attr.attr,
+ &replace_kobj_attr.attr,
NULL
};
ATTRIBUTE_GROUPS(klp_patch);
--
2.39.1
^ permalink raw reply related [flat|nested] 6+ messages in thread* [PATCH v3 2/3] selftests/livepatch: Add selftests for "replace" sysfs attribute
2024-06-25 15:11 [PATCH v3 0/3] livepatch: Add "replace" sysfs attribute Yafang Shao
2024-06-25 15:11 ` [PATCH v3 1/3] " Yafang Shao
@ 2024-06-25 15:11 ` Yafang Shao
2024-06-25 15:11 ` [PATCH v3 3/3] livepatch: Replace snprintf() with sysfs_emit() Yafang Shao
` (2 subsequent siblings)
4 siblings, 0 replies; 6+ messages in thread
From: Yafang Shao @ 2024-06-25 15:11 UTC (permalink / raw)
To: jpoimboe, jikos, mbenes, pmladek, joe.lawrence, song, mpdesouza
Cc: live-patching, Yafang Shao
Add selftests for both atomic replace and non atomic replace
livepatches. The result is as follows,
TEST: sysfs test ... ok
TEST: sysfs test object/patched ... ok
TEST: sysfs test replace enabled ... ok
TEST: sysfs test replace disabled ... ok
Suggested-by: Marcos Paulo de Souza <mpdesouza@suse.com>
Signed-off-by: Yafang Shao <laoar.shao@gmail.com>
Tested-by: Marcos Paulo de Souza <mpdesouza@suse.com>
Reviewed-by: Marcos Paulo de Souza <mpdesouza@suse.com>
Reviewed-by: Petr Mladek <pmladek@suse.com>
---
.../testing/selftests/livepatch/test-sysfs.sh | 48 +++++++++++++++++++
1 file changed, 48 insertions(+)
diff --git a/tools/testing/selftests/livepatch/test-sysfs.sh b/tools/testing/selftests/livepatch/test-sysfs.sh
index 6c646afa7395..05a14f5a7bfb 100755
--- a/tools/testing/selftests/livepatch/test-sysfs.sh
+++ b/tools/testing/selftests/livepatch/test-sysfs.sh
@@ -18,6 +18,7 @@ check_sysfs_rights "$MOD_LIVEPATCH" "" "drwxr-xr-x"
check_sysfs_rights "$MOD_LIVEPATCH" "enabled" "-rw-r--r--"
check_sysfs_value "$MOD_LIVEPATCH" "enabled" "1"
check_sysfs_rights "$MOD_LIVEPATCH" "force" "--w-------"
+check_sysfs_rights "$MOD_LIVEPATCH" "replace" "-r--r--r--"
check_sysfs_rights "$MOD_LIVEPATCH" "transition" "-r--r--r--"
check_sysfs_value "$MOD_LIVEPATCH" "transition" "0"
check_sysfs_rights "$MOD_LIVEPATCH" "vmlinux/patched" "-r--r--r--"
@@ -83,4 +84,51 @@ test_klp_callbacks_demo: post_unpatch_callback: vmlinux
livepatch: 'test_klp_callbacks_demo': unpatching complete
% rmmod test_klp_callbacks_demo"
+start_test "sysfs test replace enabled"
+
+MOD_LIVEPATCH=test_klp_atomic_replace
+load_lp $MOD_LIVEPATCH replace=1
+
+check_sysfs_rights "$MOD_LIVEPATCH" "replace" "-r--r--r--"
+check_sysfs_value "$MOD_LIVEPATCH" "replace" "1"
+
+disable_lp $MOD_LIVEPATCH
+unload_lp $MOD_LIVEPATCH
+
+check_result "% insmod test_modules/$MOD_LIVEPATCH.ko replace=1
+livepatch: enabling patch '$MOD_LIVEPATCH'
+livepatch: '$MOD_LIVEPATCH': initializing patching transition
+livepatch: '$MOD_LIVEPATCH': starting patching transition
+livepatch: '$MOD_LIVEPATCH': completing patching transition
+livepatch: '$MOD_LIVEPATCH': patching complete
+% echo 0 > /sys/kernel/livepatch/$MOD_LIVEPATCH/enabled
+livepatch: '$MOD_LIVEPATCH': initializing unpatching transition
+livepatch: '$MOD_LIVEPATCH': starting unpatching transition
+livepatch: '$MOD_LIVEPATCH': completing unpatching transition
+livepatch: '$MOD_LIVEPATCH': unpatching complete
+% rmmod $MOD_LIVEPATCH"
+
+start_test "sysfs test replace disabled"
+
+load_lp $MOD_LIVEPATCH replace=0
+
+check_sysfs_rights "$MOD_LIVEPATCH" "replace" "-r--r--r--"
+check_sysfs_value "$MOD_LIVEPATCH" "replace" "0"
+
+disable_lp $MOD_LIVEPATCH
+unload_lp $MOD_LIVEPATCH
+
+check_result "% insmod test_modules/$MOD_LIVEPATCH.ko replace=0
+livepatch: enabling patch '$MOD_LIVEPATCH'
+livepatch: '$MOD_LIVEPATCH': initializing patching transition
+livepatch: '$MOD_LIVEPATCH': starting patching transition
+livepatch: '$MOD_LIVEPATCH': completing patching transition
+livepatch: '$MOD_LIVEPATCH': patching complete
+% echo 0 > /sys/kernel/livepatch/$MOD_LIVEPATCH/enabled
+livepatch: '$MOD_LIVEPATCH': initializing unpatching transition
+livepatch: '$MOD_LIVEPATCH': starting unpatching transition
+livepatch: '$MOD_LIVEPATCH': completing unpatching transition
+livepatch: '$MOD_LIVEPATCH': unpatching complete
+% rmmod $MOD_LIVEPATCH"
+
exit 0
--
2.39.1
^ permalink raw reply related [flat|nested] 6+ messages in thread* [PATCH v3 3/3] livepatch: Replace snprintf() with sysfs_emit()
2024-06-25 15:11 [PATCH v3 0/3] livepatch: Add "replace" sysfs attribute Yafang Shao
2024-06-25 15:11 ` [PATCH v3 1/3] " Yafang Shao
2024-06-25 15:11 ` [PATCH v3 2/3] selftests/livepatch: Add selftests for " Yafang Shao
@ 2024-06-25 15:11 ` Yafang Shao
2024-06-27 13:23 ` [PATCH v3 0/3] livepatch: Add "replace" sysfs attribute Miroslav Benes
2024-07-03 8:30 ` Petr Mladek
4 siblings, 0 replies; 6+ messages in thread
From: Yafang Shao @ 2024-06-25 15:11 UTC (permalink / raw)
To: jpoimboe, jikos, mbenes, pmladek, joe.lawrence, song, mpdesouza
Cc: live-patching, Yafang Shao
Let's use sysfs_emit() instead of snprintf().
Suggested-by: Miroslav Benes <mbenes@suse.cz>
Signed-off-by: Yafang Shao <laoar.shao@gmail.com>
Reviewed-by: Petr Mladek <pmladek@suse.com>
---
kernel/livepatch/core.c | 5 ++---
1 file changed, 2 insertions(+), 3 deletions(-)
diff --git a/kernel/livepatch/core.c b/kernel/livepatch/core.c
index ad28617bfd75..3c21c31796db 100644
--- a/kernel/livepatch/core.c
+++ b/kernel/livepatch/core.c
@@ -402,7 +402,7 @@ static ssize_t enabled_show(struct kobject *kobj,
struct klp_patch *patch;
patch = container_of(kobj, struct klp_patch, kobj);
- return snprintf(buf, PAGE_SIZE-1, "%d\n", patch->enabled);
+ return sysfs_emit(buf, "%d\n", patch->enabled);
}
static ssize_t transition_show(struct kobject *kobj,
@@ -411,8 +411,7 @@ static ssize_t transition_show(struct kobject *kobj,
struct klp_patch *patch;
patch = container_of(kobj, struct klp_patch, kobj);
- return snprintf(buf, PAGE_SIZE-1, "%d\n",
- patch == klp_transition_patch);
+ return sysfs_emit(buf, "%d\n", patch == klp_transition_patch);
}
static ssize_t force_store(struct kobject *kobj, struct kobj_attribute *attr,
--
2.39.1
^ permalink raw reply related [flat|nested] 6+ messages in thread
* Re: [PATCH v3 0/3] livepatch: Add "replace" sysfs attribute
2024-06-25 15:11 [PATCH v3 0/3] livepatch: Add "replace" sysfs attribute Yafang Shao
` (2 preceding siblings ...)
2024-06-25 15:11 ` [PATCH v3 3/3] livepatch: Replace snprintf() with sysfs_emit() Yafang Shao
@ 2024-06-27 13:23 ` Miroslav Benes
2024-07-03 8:30 ` Petr Mladek
4 siblings, 0 replies; 6+ messages in thread
From: Miroslav Benes @ 2024-06-27 13:23 UTC (permalink / raw)
To: Yafang Shao
Cc: jpoimboe, jikos, pmladek, joe.lawrence, song, mpdesouza,
live-patching
On Tue, 25 Jun 2024, Yafang Shao wrote:
> There are situations when it might make sense to combine livepatches
> with and without the atomic replace on the same system. For example,
> the livepatch without the atomic replace might provide a hotfix
> or extra tuning.
>
> Managing livepatches on such systems might be challenging. And the
> information which of the installed livepatches do not use the atomic
> replace would be useful. Therefore, "replace" sysfs attribute is added
> to show whether a livepatch supports atomic replace or not.
>
> A minor cleanup is also included in this patchset.
>
> v2->v3:
> - Improve the commit log (Petr)
>
> v1->v2: https://lore.kernel.org/live-patching/20240610013237.92646-1-laoar.shao@gmail.com/
> - Refine the subject (Miroslav)
> - Use sysfs_emit() instead and replace other snprintf() as well (Miroslav)
> - Add selftests (Marcos)
>
> v1: https://lore.kernel.org/live-patching/20240607070157.33828-1-laoar.shao@gmail.com/
>
> Yafang Shao (3):
> livepatch: Add "replace" sysfs attribute
> selftests/livepatch: Add selftests for "replace" sysfs attribute
> livepatch: Replace snprintf() with sysfs_emit()
>
> .../ABI/testing/sysfs-kernel-livepatch | 8 ++++
> kernel/livepatch/core.c | 17 +++++--
> .../testing/selftests/livepatch/test-sysfs.sh | 48 +++++++++++++++++++
> 3 files changed, 70 insertions(+), 3 deletions(-)
Acked-by: Miroslav Benes <mbenes@suse.cz>
M
^ permalink raw reply [flat|nested] 6+ messages in thread* Re: [PATCH v3 0/3] livepatch: Add "replace" sysfs attribute
2024-06-25 15:11 [PATCH v3 0/3] livepatch: Add "replace" sysfs attribute Yafang Shao
` (3 preceding siblings ...)
2024-06-27 13:23 ` [PATCH v3 0/3] livepatch: Add "replace" sysfs attribute Miroslav Benes
@ 2024-07-03 8:30 ` Petr Mladek
4 siblings, 0 replies; 6+ messages in thread
From: Petr Mladek @ 2024-07-03 8:30 UTC (permalink / raw)
To: Yafang Shao
Cc: jpoimboe, jikos, mbenes, joe.lawrence, song, mpdesouza,
live-patching
On Tue 2024-06-25 23:11:20, Yafang Shao wrote:
> There are situations when it might make sense to combine livepatches
> with and without the atomic replace on the same system. For example,
> the livepatch without the atomic replace might provide a hotfix
> or extra tuning.
>
> Managing livepatches on such systems might be challenging. And the
> information which of the installed livepatches do not use the atomic
> replace would be useful. Therefore, "replace" sysfs attribute is added
> to show whether a livepatch supports atomic replace or not.
>
> A minor cleanup is also included in this patchset.
>
> v2->v3:
> - Improve the commit log (Petr)
>
> v1->v2: https://lore.kernel.org/live-patching/20240610013237.92646-1-laoar.shao@gmail.com/
> - Refine the subject (Miroslav)
> - Use sysfs_emit() instead and replace other snprintf() as well (Miroslav)
> - Add selftests (Marcos)
>
> v1: https://lore.kernel.org/live-patching/20240607070157.33828-1-laoar.shao@gmail.com/
>
> Yafang Shao (3):
> livepatch: Add "replace" sysfs attribute
> selftests/livepatch: Add selftests for "replace" sysfs attribute
> livepatch: Replace snprintf() with sysfs_emit()
>
> .../ABI/testing/sysfs-kernel-livepatch | 8 ++++
> kernel/livepatch/core.c | 17 +++++--
> .../testing/selftests/livepatch/test-sysfs.sh | 48 +++++++++++++++++++
> 3 files changed, 70 insertions(+), 3 deletions(-)
JFYI, the patchset has been committed into livepatchining.git,
branch for-6.11/sysfs-patch-replace.
Best Regards,
Petr
^ permalink raw reply [flat|nested] 6+ messages in thread