Skip to content

Commit 20e5502

Browse files
almostivanJiri Kosina
authored andcommitted
livepatch: Use lists to manage patches, objects and functions
Currently klp_patch contains a pointer to a statically allocated array of struct klp_object and struct klp_objects contains a pointer to a statically allocated array of klp_func. In order to allow for the dynamic allocation of objects and functions, link klp_patch, klp_object, and klp_func together via linked lists. This allows us to more easily allocate new objects and functions, while having the iterator be a simple linked list walk. The static structures are added to the lists early. It allows to add the dynamically allocated objects before klp_init_object() and klp_init_func() calls. Therefore it reduces the further changes to the code. This patch does not change the existing behavior. Signed-off-by: Jason Baron <[email protected]> [[email protected]: Initialize lists before init calls] Signed-off-by: Petr Mladek <[email protected]> Acked-by: Miroslav Benes <[email protected]> Acked-by: Joe Lawrence <[email protected]> Cc: Josh Poimboeuf <[email protected]> Cc: Jiri Kosina <[email protected]> Acked-by: Josh Poimboeuf <[email protected]> Signed-off-by: Jiri Kosina <[email protected]>
1 parent 958ef1e commit 20e5502

File tree

2 files changed

+24
-4
lines changed

2 files changed

+24
-4
lines changed

include/linux/livepatch.h

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
#include <linux/module.h>
2525
#include <linux/ftrace.h>
2626
#include <linux/completion.h>
27+
#include <linux/list.h>
2728

2829
#if IS_ENABLED(CONFIG_LIVEPATCH)
2930

@@ -42,6 +43,7 @@
4243
* can be found (optional)
4344
* @old_func: pointer to the function being patched
4445
* @kobj: kobject for sysfs resources
46+
* @node: list node for klp_object func_list
4547
* @stack_node: list node for klp_ops func_stack list
4648
* @old_size: size of the old function
4749
* @new_size: size of the new function
@@ -80,6 +82,7 @@ struct klp_func {
8082
/* internal */
8183
void *old_func;
8284
struct kobject kobj;
85+
struct list_head node;
8386
struct list_head stack_node;
8487
unsigned long old_size, new_size;
8588
bool kobj_added;
@@ -117,6 +120,8 @@ struct klp_callbacks {
117120
* @funcs: function entries for functions to be patched in the object
118121
* @callbacks: functions to be executed pre/post (un)patching
119122
* @kobj: kobject for sysfs resources
123+
* @func_list: dynamic list of the function entries
124+
* @node: list node for klp_patch obj_list
120125
* @mod: kernel module associated with the patched object
121126
* (NULL for vmlinux)
122127
* @kobj_added: @kobj has been added and needs freeing
@@ -130,6 +135,8 @@ struct klp_object {
130135

131136
/* internal */
132137
struct kobject kobj;
138+
struct list_head func_list;
139+
struct list_head node;
133140
struct module *mod;
134141
bool kobj_added;
135142
bool patched;
@@ -141,6 +148,7 @@ struct klp_object {
141148
* @objs: object entries for kernel objects to be patched
142149
* @list: list node for global list of actively used patches
143150
* @kobj: kobject for sysfs resources
151+
* @obj_list: dynamic list of the object entries
144152
* @kobj_added: @kobj has been added and needs freeing
145153
* @enabled: the patch is enabled (but operation may be incomplete)
146154
* @forced: was involved in a forced transition
@@ -155,21 +163,28 @@ struct klp_patch {
155163
/* internal */
156164
struct list_head list;
157165
struct kobject kobj;
166+
struct list_head obj_list;
158167
bool kobj_added;
159168
bool enabled;
160169
bool forced;
161170
struct work_struct free_work;
162171
struct completion finish;
163172
};
164173

165-
#define klp_for_each_object(patch, obj) \
174+
#define klp_for_each_object_static(patch, obj) \
166175
for (obj = patch->objs; obj->funcs || obj->name; obj++)
167176

168-
#define klp_for_each_func(obj, func) \
177+
#define klp_for_each_object(patch, obj) \
178+
list_for_each_entry(obj, &patch->obj_list, node)
179+
180+
#define klp_for_each_func_static(obj, func) \
169181
for (func = obj->funcs; \
170182
func->old_name || func->new_func || func->old_sympos; \
171183
func++)
172184

185+
#define klp_for_each_func(obj, func) \
186+
list_for_each_entry(func, &obj->func_list, node)
187+
173188
int klp_enable_patch(struct klp_patch *);
174189

175190
void arch_klp_init_object_loaded(struct klp_patch *patch,

kernel/livepatch/core.c

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -659,20 +659,25 @@ static int klp_init_patch_early(struct klp_patch *patch)
659659
return -EINVAL;
660660

661661
INIT_LIST_HEAD(&patch->list);
662+
INIT_LIST_HEAD(&patch->obj_list);
662663
patch->kobj_added = false;
663664
patch->enabled = false;
664665
patch->forced = false;
665666
INIT_WORK(&patch->free_work, klp_free_patch_work_fn);
666667
init_completion(&patch->finish);
667668

668-
klp_for_each_object(patch, obj) {
669+
klp_for_each_object_static(patch, obj) {
669670
if (!obj->funcs)
670671
return -EINVAL;
671672

673+
INIT_LIST_HEAD(&obj->func_list);
672674
obj->kobj_added = false;
675+
list_add_tail(&obj->node, &patch->obj_list);
673676

674-
klp_for_each_func(obj, func)
677+
klp_for_each_func_static(obj, func) {
675678
func->kobj_added = false;
679+
list_add_tail(&func->node, &obj->func_list);
680+
}
676681
}
677682

678683
if (!try_module_get(patch->mod))

0 commit comments

Comments
 (0)