History

Merge branch 'for-6.1/sysfs-patched-object' into for-linus

Petr Mladek committed 4 years ago

Commit 59b2a382 parents 857300b ff1b80e5 files · +148 −1Browse files

Unified · Split

5 files
  1. Documentation/ABI/testing/sysfs-kernel-livepatch +8 −0
  2. kernel/livepatch/core.c +18 −0
  3. tools/testing/selftests/livepatch/Makefile +2 −1
  4. tools/testing/selftests/livepatch/functions.sh +34 −0
  5. tools/testing/selftests/livepatch/test-sysfs.sh +86 −0
Documentation/ABI/testing/sysfs-kernel-livepatch+8−0
@@ -55,6 +55,14 @@ Description: · 54 unmodified lines
55 The object directory contains subdirectories for each function55 The object directory contains subdirectories for each function
56 that is patched within the object.56 that is patched within the object.
57 57
58What: /sys/kernel/livepatch/<patch>/<object>/patched
59Date: August 2022
60KernelVersion: 6.1.0
61Contact: live-patching@vger.kernel.org
62Description:
63 An attribute which indicates whether the object is currently
64 patched.
65
58What: /sys/kernel/livepatch/<patch>/<object>/<function,sympos>66What: /sys/kernel/livepatch/<patch>/<object>/<function,sympos>
59Date: Nov 201467Date: Nov 2014
60KernelVersion: 3.19.068KernelVersion: 3.19.0
kernel/livepatch/core.c+18−0
@@ -325,6 +325,7 @@ int klp_apply_section_relocs(struct module *pmod, Elf_Shdr *sechdrs, · 324 unmodified lines
325 * /sys/kernel/livepatch/<patch>/transition325 * /sys/kernel/livepatch/<patch>/transition
326 * /sys/kernel/livepatch/<patch>/force326 * /sys/kernel/livepatch/<patch>/force
327 * /sys/kernel/livepatch/<patch>/<object>327 * /sys/kernel/livepatch/<patch>/<object>
328 * /sys/kernel/livepatch/<patch>/<object>/patched
328 * /sys/kernel/livepatch/<patch>/<object>/<function,sympos>329 * /sys/kernel/livepatch/<patch>/<object>/<function,sympos>
329 */330 */
330static int __klp_disable_patch(struct klp_patch *patch);331static int __klp_disable_patch(struct klp_patch *patch);
@@ -431,6 +432,22 @@ static struct attribute *klp_patch_attrs[] = { · 100 unmodified lines
431};432};
432ATTRIBUTE_GROUPS(klp_patch);433ATTRIBUTE_GROUPS(klp_patch);
433 434
435static ssize_t patched_show(struct kobject *kobj,
436 struct kobj_attribute *attr, char *buf)
437{
438 struct klp_object *obj;
439
440 obj = container_of(kobj, struct klp_object, kobj);
441 return sysfs_emit(buf, "%d\n", obj->patched);
442}
443
444static struct kobj_attribute patched_kobj_attr = __ATTR_RO(patched);
445static struct attribute *klp_object_attrs[] = {
446 &patched_kobj_attr.attr,
447 NULL,
448};
449ATTRIBUTE_GROUPS(klp_object);
450
434static void klp_free_object_dynamic(struct klp_object *obj)451static void klp_free_object_dynamic(struct klp_object *obj)
435{452{
436 kfree(obj->name);453 kfree(obj->name);
@@ -576,6 +593,7 @@ static void klp_kobj_release_object(struct kobject *kobj) · 139 unmodified lines
576static struct kobj_type klp_ktype_object = {593static struct kobj_type klp_ktype_object = {
577 .release = klp_kobj_release_object,594 .release = klp_kobj_release_object,
578 .sysfs_ops = &kobj_sysfs_ops,595 .sysfs_ops = &kobj_sysfs_ops,
596 .default_groups = klp_object_groups,
579};597};
580 598
581static void klp_kobj_release_func(struct kobject *kobj)599static void klp_kobj_release_func(struct kobject *kobj)
tools/testing/selftests/livepatch/Makefile+2−1
@@ -6,7 +6,8 @@ TEST_PROGS := \ · 5 unmodified lines
6 test-callbacks.sh \6 test-callbacks.sh \
7 test-shadow-vars.sh \7 test-shadow-vars.sh \
8 test-state.sh \8 test-state.sh \
9 test-ftrace.sh9 test-ftrace.sh \
10 test-sysfs.sh
10 11
11TEST_FILES := settings12TEST_FILES := settings
12 13
tools/testing/selftests/livepatch/functions.sh+34−0
@@ -6,6 +6,7 @@ · 5 unmodified lines
6 6
7MAX_RETRIES=6007MAX_RETRIES=600
8RETRY_INTERVAL=".1" # seconds8RETRY_INTERVAL=".1" # seconds
9KLP_SYSFS_DIR="/sys/kernel/livepatch"
9 10
10# Kselftest framework requirement - SKIP code is 411# Kselftest framework requirement - SKIP code is 4
11ksft_skip=412ksft_skip=4
@@ -308,3 +309,36 @@ function check_result { · 296 unmodified lines
308 309
309 cleanup_dmesg_file310 cleanup_dmesg_file
310}311}
312
313# check_sysfs_rights(modname, rel_path, expected_rights) - check sysfs
314# path permissions
315# modname - livepatch module creating the sysfs interface
316# rel_path - relative path of the sysfs interface
317# expected_rights - expected access rights
318function check_sysfs_rights() {
319 local mod="$1"; shift
320 local rel_path="$1"; shift
321 local expected_rights="$1"; shift
322
323 local path="$KLP_SYSFS_DIR/$mod/$rel_path"
324 local rights=$(/bin/stat --format '%A' "$path")
325 if test "$rights" != "$expected_rights" ; then
326 die "Unexpected access rights of $path: $expected_rights vs. $rights"
327 fi
328}
329
330# check_sysfs_value(modname, rel_path, expected_value) - check sysfs value
331# modname - livepatch module creating the sysfs interface
332# rel_path - relative path of the sysfs interface
333# expected_value - expected value read from the file
334function check_sysfs_value() {
335 local mod="$1"; shift
336 local rel_path="$1"; shift
337 local expected_value="$1"; shift
338
339 local path="$KLP_SYSFS_DIR/$mod/$rel_path"
340 local value=`cat $path`
341 if test "$value" != "$expected_value" ; then
342 die "Unexpected value in $path: $expected_value vs. $value"
343 fi
344}
tools/testing/selftests/livepatch/test-sysfs.sh+86−0
@@ -0,0 +1,86 @@
1#!/bin/bash
2# SPDX-License-Identifier: GPL-2.0
3# Copyright (C) 2022 Song Liu <song@kernel.org>
4
5. $(dirname $0)/functions.sh
6
7MOD_LIVEPATCH=test_klp_livepatch
8
9setup_config
10
11# - load a livepatch and verifies the sysfs entries work as expected
12
13start_test "sysfs test"
14
15load_lp $MOD_LIVEPATCH
16
17check_sysfs_rights "$MOD_LIVEPATCH" "" "drwxr-xr-x"
18check_sysfs_rights "$MOD_LIVEPATCH" "enabled" "-rw-r--r--"
19check_sysfs_value "$MOD_LIVEPATCH" "enabled" "1"
20check_sysfs_rights "$MOD_LIVEPATCH" "force" "--w-------"
21check_sysfs_rights "$MOD_LIVEPATCH" "transition" "-r--r--r--"
22check_sysfs_value "$MOD_LIVEPATCH" "transition" "0"
23check_sysfs_rights "$MOD_LIVEPATCH" "vmlinux/patched" "-r--r--r--"
24check_sysfs_value "$MOD_LIVEPATCH" "vmlinux/patched" "1"
25
26disable_lp $MOD_LIVEPATCH
27
28unload_lp $MOD_LIVEPATCH
29
30check_result "% modprobe $MOD_LIVEPATCH
31livepatch: enabling patch '$MOD_LIVEPATCH'
32livepatch: '$MOD_LIVEPATCH': initializing patching transition
33livepatch: '$MOD_LIVEPATCH': starting patching transition
34livepatch: '$MOD_LIVEPATCH': completing patching transition
35livepatch: '$MOD_LIVEPATCH': patching complete
36% echo 0 > /sys/kernel/livepatch/$MOD_LIVEPATCH/enabled
37livepatch: '$MOD_LIVEPATCH': initializing unpatching transition
38livepatch: '$MOD_LIVEPATCH': starting unpatching transition
39livepatch: '$MOD_LIVEPATCH': completing unpatching transition
40livepatch: '$MOD_LIVEPATCH': unpatching complete
41% rmmod $MOD_LIVEPATCH"
42
43start_test "sysfs test object/patched"
44
45MOD_LIVEPATCH=test_klp_callbacks_demo
46MOD_TARGET=test_klp_callbacks_mod
47load_lp $MOD_LIVEPATCH
48
49# check the "patch" file changes as target module loads/unloads
50check_sysfs_value "$MOD_LIVEPATCH" "$MOD_TARGET/patched" "0"
51load_mod $MOD_TARGET
52check_sysfs_value "$MOD_LIVEPATCH" "$MOD_TARGET/patched" "1"
53unload_mod $MOD_TARGET
54check_sysfs_value "$MOD_LIVEPATCH" "$MOD_TARGET/patched" "0"
55
56disable_lp $MOD_LIVEPATCH
57unload_lp $MOD_LIVEPATCH
58
59check_result "% modprobe test_klp_callbacks_demo
60livepatch: enabling patch 'test_klp_callbacks_demo'
61livepatch: 'test_klp_callbacks_demo': initializing patching transition
62test_klp_callbacks_demo: pre_patch_callback: vmlinux
63livepatch: 'test_klp_callbacks_demo': starting patching transition
64livepatch: 'test_klp_callbacks_demo': completing patching transition
65test_klp_callbacks_demo: post_patch_callback: vmlinux
66livepatch: 'test_klp_callbacks_demo': patching complete
67% modprobe test_klp_callbacks_mod
68livepatch: applying patch 'test_klp_callbacks_demo' to loading module 'test_klp_callbacks_mod'
69test_klp_callbacks_demo: pre_patch_callback: test_klp_callbacks_mod -> [MODULE_STATE_COMING] Full formed, running module_init
70test_klp_callbacks_demo: post_patch_callback: test_klp_callbacks_mod -> [MODULE_STATE_COMING] Full formed, running module_init
71test_klp_callbacks_mod: test_klp_callbacks_mod_init
72% rmmod test_klp_callbacks_mod
73test_klp_callbacks_mod: test_klp_callbacks_mod_exit
74test_klp_callbacks_demo: pre_unpatch_callback: test_klp_callbacks_mod -> [MODULE_STATE_GOING] Going away
75livepatch: reverting patch 'test_klp_callbacks_demo' on unloading module 'test_klp_callbacks_mod'
76test_klp_callbacks_demo: post_unpatch_callback: test_klp_callbacks_mod -> [MODULE_STATE_GOING] Going away
77% echo 0 > /sys/kernel/livepatch/test_klp_callbacks_demo/enabled
78livepatch: 'test_klp_callbacks_demo': initializing unpatching transition
79test_klp_callbacks_demo: pre_unpatch_callback: vmlinux
80livepatch: 'test_klp_callbacks_demo': starting unpatching transition
81livepatch: 'test_klp_callbacks_demo': completing unpatching transition
82test_klp_callbacks_demo: post_unpatch_callback: vmlinux
83livepatch: 'test_klp_callbacks_demo': unpatching complete
84% rmmod test_klp_callbacks_demo"
85
86exit 0

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t