Commit 573de2a6 authored by Linus Torvalds's avatar Linus Torvalds

Merge branch 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/livepatching/livepatching

Pull livepatching updates from Jiri Kosina:

 - livepatching kselftests improvements from Joe Lawrence and Miroslav
   Benes

 - making use of gcc's -flive-patching option when available, from
   Miroslav Benes

 - kobject handling cleanups, from Petr Mladek

* 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/livepatching/livepatching:
  livepatch: Remove duplicated code for early initialization
  livepatch: Remove custom kobject state handling
  livepatch: Convert error about unsupported reliable stacktrace into a warning
  selftests/livepatch: Add functions.sh to TEST_PROGS_EXTENDED
  kbuild: use -flive-patching when CONFIG_LIVEPATCH is enabled
  selftests/livepatch: use TEST_PROGS for test scripts
parents b4dd05de 1efbd99e
...@@ -811,6 +811,10 @@ KBUILD_CFLAGS_KERNEL += -ffunction-sections -fdata-sections ...@@ -811,6 +811,10 @@ KBUILD_CFLAGS_KERNEL += -ffunction-sections -fdata-sections
LDFLAGS_vmlinux += --gc-sections LDFLAGS_vmlinux += --gc-sections
endif endif
ifdef CONFIG_LIVEPATCH
KBUILD_CFLAGS += $(call cc-option, -flive-patching=inline-clone)
endif
# arch Makefile may override CC so keep this after arch Makefile is included # arch Makefile may override CC so keep this after arch Makefile is included
NOSTDINC_FLAGS += -nostdinc -isystem $(shell $(CC) -print-file-name=include) NOSTDINC_FLAGS += -nostdinc -isystem $(shell $(CC) -print-file-name=include)
......
...@@ -86,7 +86,6 @@ struct klp_func { ...@@ -86,7 +86,6 @@ struct klp_func {
struct list_head node; struct list_head node;
struct list_head stack_node; struct list_head stack_node;
unsigned long old_size, new_size; unsigned long old_size, new_size;
bool kobj_added;
bool nop; bool nop;
bool patched; bool patched;
bool transition; bool transition;
...@@ -141,7 +140,6 @@ struct klp_object { ...@@ -141,7 +140,6 @@ struct klp_object {
struct list_head func_list; struct list_head func_list;
struct list_head node; struct list_head node;
struct module *mod; struct module *mod;
bool kobj_added;
bool dynamic; bool dynamic;
bool patched; bool patched;
}; };
...@@ -170,7 +168,6 @@ struct klp_patch { ...@@ -170,7 +168,6 @@ struct klp_patch {
struct list_head list; struct list_head list;
struct kobject kobj; struct kobject kobj;
struct list_head obj_list; struct list_head obj_list;
bool kobj_added;
bool enabled; bool enabled;
bool forced; bool forced;
struct work_struct free_work; struct work_struct free_work;
......
...@@ -426,7 +426,13 @@ static void klp_free_object_dynamic(struct klp_object *obj) ...@@ -426,7 +426,13 @@ static void klp_free_object_dynamic(struct klp_object *obj)
kfree(obj); kfree(obj);
} }
static struct klp_object *klp_alloc_object_dynamic(const char *name) 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,
struct klp_patch *patch)
{ {
struct klp_object *obj; struct klp_object *obj;
...@@ -442,7 +448,7 @@ static struct klp_object *klp_alloc_object_dynamic(const char *name) ...@@ -442,7 +448,7 @@ static struct klp_object *klp_alloc_object_dynamic(const char *name)
} }
} }
INIT_LIST_HEAD(&obj->func_list); klp_init_object_early(patch, obj);
obj->dynamic = true; obj->dynamic = true;
return obj; return obj;
...@@ -471,6 +477,7 @@ static struct klp_func *klp_alloc_func_nop(struct klp_func *old_func, ...@@ -471,6 +477,7 @@ static struct klp_func *klp_alloc_func_nop(struct klp_func *old_func,
} }
} }
klp_init_func_early(obj, func);
/* /*
* func->new_func is same as func->old_func. These addresses are * func->new_func is same as func->old_func. These addresses are
* set when the object is loaded, see klp_init_object_loaded(). * set when the object is loaded, see klp_init_object_loaded().
...@@ -490,11 +497,9 @@ static int klp_add_object_nops(struct klp_patch *patch, ...@@ -490,11 +497,9 @@ static int klp_add_object_nops(struct klp_patch *patch,
obj = klp_find_object(patch, old_obj); obj = klp_find_object(patch, old_obj);
if (!obj) { if (!obj) {
obj = klp_alloc_object_dynamic(old_obj->name); obj = klp_alloc_object_dynamic(old_obj->name, patch);
if (!obj) if (!obj)
return -ENOMEM; return -ENOMEM;
list_add_tail(&obj->node, &patch->obj_list);
} }
klp_for_each_func(old_obj, old_func) { klp_for_each_func(old_obj, old_func) {
...@@ -505,8 +510,6 @@ static int klp_add_object_nops(struct klp_patch *patch, ...@@ -505,8 +510,6 @@ static int klp_add_object_nops(struct klp_patch *patch,
func = klp_alloc_func_nop(old_func, obj); func = klp_alloc_func_nop(old_func, obj);
if (!func) if (!func)
return -ENOMEM; return -ENOMEM;
list_add_tail(&func->node, &obj->func_list);
} }
return 0; return 0;
...@@ -588,13 +591,7 @@ static void __klp_free_funcs(struct klp_object *obj, bool nops_only) ...@@ -588,13 +591,7 @@ static void __klp_free_funcs(struct klp_object *obj, bool nops_only)
continue; continue;
list_del(&func->node); list_del(&func->node);
kobject_put(&func->kobj);
/* Might be called from klp_init_patch() error path. */
if (func->kobj_added) {
kobject_put(&func->kobj);
} else if (func->nop) {
klp_free_func_nop(func);
}
} }
} }
...@@ -624,13 +621,7 @@ static void __klp_free_objects(struct klp_patch *patch, bool nops_only) ...@@ -624,13 +621,7 @@ static void __klp_free_objects(struct klp_patch *patch, bool nops_only)
continue; continue;
list_del(&obj->node); list_del(&obj->node);
kobject_put(&obj->kobj);
/* Might be called from klp_init_patch() error path. */
if (obj->kobj_added) {
kobject_put(&obj->kobj);
} else if (obj->dynamic) {
klp_free_object_dynamic(obj);
}
} }
} }
...@@ -675,10 +666,8 @@ static void klp_free_patch_finish(struct klp_patch *patch) ...@@ -675,10 +666,8 @@ static void klp_free_patch_finish(struct klp_patch *patch)
* this is called when the patch gets disabled and it * this is called when the patch gets disabled and it
* cannot get enabled again. * cannot get enabled again.
*/ */
if (patch->kobj_added) { kobject_put(&patch->kobj);
kobject_put(&patch->kobj); wait_for_completion(&patch->finish);
wait_for_completion(&patch->finish);
}
/* Put the module after the last access to struct klp_patch. */ /* Put the module after the last access to struct klp_patch. */
if (!patch->forced) if (!patch->forced)
...@@ -700,8 +689,6 @@ static void klp_free_patch_work_fn(struct work_struct *work) ...@@ -700,8 +689,6 @@ static void klp_free_patch_work_fn(struct work_struct *work)
static int klp_init_func(struct klp_object *obj, struct klp_func *func) static int klp_init_func(struct klp_object *obj, struct klp_func *func)
{ {
int ret;
if (!func->old_name) if (!func->old_name)
return -EINVAL; return -EINVAL;
...@@ -724,13 +711,9 @@ static int klp_init_func(struct klp_object *obj, struct klp_func *func) ...@@ -724,13 +711,9 @@ static int klp_init_func(struct klp_object *obj, struct klp_func *func)
* object. If the user selects 0 for old_sympos, then 1 will be used * object. If the user selects 0 for old_sympos, then 1 will be used
* since a unique symbol will be the first occurrence. * since a unique symbol will be the first occurrence.
*/ */
ret = kobject_init_and_add(&func->kobj, &klp_ktype_func, return kobject_add(&func->kobj, &obj->kobj, "%s,%lu",
&obj->kobj, "%s,%lu", func->old_name, func->old_name,
func->old_sympos ? func->old_sympos : 1); func->old_sympos ? func->old_sympos : 1);
if (!ret)
func->kobj_added = true;
return ret;
} }
/* Arches may override this to finish any remaining arch-specific tasks */ /* Arches may override this to finish any remaining arch-specific tasks */
...@@ -801,11 +784,9 @@ static int klp_init_object(struct klp_patch *patch, struct klp_object *obj) ...@@ -801,11 +784,9 @@ static int klp_init_object(struct klp_patch *patch, struct klp_object *obj)
klp_find_object_module(obj); klp_find_object_module(obj);
name = klp_is_module(obj) ? obj->name : "vmlinux"; name = klp_is_module(obj) ? obj->name : "vmlinux";
ret = kobject_init_and_add(&obj->kobj, &klp_ktype_object, ret = kobject_add(&obj->kobj, &patch->kobj, "%s", name);
&patch->kobj, "%s", name);
if (ret) if (ret)
return ret; return ret;
obj->kobj_added = true;
klp_for_each_func(obj, func) { klp_for_each_func(obj, func) {
ret = klp_init_func(obj, func); ret = klp_init_func(obj, func);
...@@ -819,6 +800,21 @@ static int klp_init_object(struct klp_patch *patch, struct klp_object *obj) ...@@ -819,6 +800,21 @@ static int klp_init_object(struct klp_patch *patch, struct klp_object *obj)
return ret; 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) static int klp_init_patch_early(struct klp_patch *patch)
{ {
struct klp_object *obj; struct klp_object *obj;
...@@ -829,7 +825,7 @@ static int klp_init_patch_early(struct klp_patch *patch) ...@@ -829,7 +825,7 @@ static int klp_init_patch_early(struct klp_patch *patch)
INIT_LIST_HEAD(&patch->list); INIT_LIST_HEAD(&patch->list);
INIT_LIST_HEAD(&patch->obj_list); INIT_LIST_HEAD(&patch->obj_list);
patch->kobj_added = false; kobject_init(&patch->kobj, &klp_ktype_patch);
patch->enabled = false; patch->enabled = false;
patch->forced = false; patch->forced = false;
INIT_WORK(&patch->free_work, klp_free_patch_work_fn); INIT_WORK(&patch->free_work, klp_free_patch_work_fn);
...@@ -839,13 +835,10 @@ static int klp_init_patch_early(struct klp_patch *patch) ...@@ -839,13 +835,10 @@ static int klp_init_patch_early(struct klp_patch *patch)
if (!obj->funcs) if (!obj->funcs)
return -EINVAL; return -EINVAL;
INIT_LIST_HEAD(&obj->func_list); klp_init_object_early(patch, obj);
obj->kobj_added = false;
list_add_tail(&obj->node, &patch->obj_list);
klp_for_each_func_static(obj, func) { klp_for_each_func_static(obj, func) {
func->kobj_added = false; klp_init_func_early(obj, func);
list_add_tail(&func->node, &obj->func_list);
} }
} }
...@@ -860,11 +853,9 @@ static int klp_init_patch(struct klp_patch *patch) ...@@ -860,11 +853,9 @@ static int klp_init_patch(struct klp_patch *patch)
struct klp_object *obj; struct klp_object *obj;
int ret; int ret;
ret = kobject_init_and_add(&patch->kobj, &klp_ktype_patch, ret = kobject_add(&patch->kobj, klp_root_kobj, "%s", patch->mod->name);
klp_root_kobj, "%s", patch->mod->name);
if (ret) if (ret)
return ret; return ret;
patch->kobj_added = true;
if (patch->replace) { if (patch->replace) {
ret = klp_add_nops(patch); ret = klp_add_nops(patch);
...@@ -926,9 +917,6 @@ static int __klp_enable_patch(struct klp_patch *patch) ...@@ -926,9 +917,6 @@ static int __klp_enable_patch(struct klp_patch *patch)
if (WARN_ON(patch->enabled)) if (WARN_ON(patch->enabled))
return -EINVAL; return -EINVAL;
if (!patch->kobj_added)
return -EINVAL;
pr_notice("enabling patch '%s'\n", patch->mod->name); pr_notice("enabling patch '%s'\n", patch->mod->name);
klp_init_transition(patch, KLP_PATCHED); klp_init_transition(patch, KLP_PATCHED);
...@@ -1003,11 +991,10 @@ int klp_enable_patch(struct klp_patch *patch) ...@@ -1003,11 +991,10 @@ int klp_enable_patch(struct klp_patch *patch)
return -ENODEV; return -ENODEV;
if (!klp_have_reliable_stack()) { if (!klp_have_reliable_stack()) {
pr_err("This architecture doesn't have support for the livepatch consistency model.\n"); pr_warn("This architecture doesn't have support for the livepatch consistency model.\n");
return -EOPNOTSUPP; pr_warn("The livepatch transition may never complete.\n");
} }
mutex_lock(&klp_mutex); mutex_lock(&klp_mutex);
ret = klp_init_patch_early(patch); ret = klp_init_patch_early(patch);
......
# SPDX-License-Identifier: GPL-2.0 # SPDX-License-Identifier: GPL-2.0
TEST_GEN_PROGS := \ TEST_PROGS_EXTENDED := functions.sh
TEST_PROGS := \
test-livepatch.sh \ test-livepatch.sh \
test-callbacks.sh \ test-callbacks.sh \
test-shadow-vars.sh test-shadow-vars.sh
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment