Commit 0dade9f3 authored by Josh Poimboeuf's avatar Josh Poimboeuf Committed by Jiri Kosina

livepatch: separate enabled and patched states

Once we have a consistency model, patches and their objects will be
enabled and disabled at different times.  For example, when a patch is
disabled, its loaded objects' funcs can remain registered with ftrace
indefinitely until the unpatching operation is complete and they're no
longer in use.

It's less confusing if we give them different names: patches can be
enabled or disabled; objects (and their funcs) can be patched or
unpatched:

- Enabled means that a patch is logically enabled (but not necessarily
  fully applied).

- Patched means that an object's funcs are registered with ftrace and
  added to the klp_ops func stack.

Also, since these states are binary, represent them with booleans
instead of ints.
Signed-off-by: default avatarJosh Poimboeuf <jpoimboe@redhat.com>
Acked-by: default avatarMiroslav Benes <mbenes@suse.cz>
Reviewed-by: default avatarPetr Mladek <pmladek@suse.com>
Reviewed-by: default avatarKamalesh Babulal <kamalesh@linux.vnet.ibm.com>
Signed-off-by: default avatarJiri Kosina <jkosina@suse.cz>
parent 2f09ca60
...@@ -28,11 +28,6 @@ ...@@ -28,11 +28,6 @@
#include <asm/livepatch.h> #include <asm/livepatch.h>
enum klp_state {
KLP_DISABLED,
KLP_ENABLED
};
/** /**
* struct klp_func - function structure for live patching * struct klp_func - function structure for live patching
* @old_name: name of the function to be patched * @old_name: name of the function to be patched
...@@ -41,8 +36,8 @@ enum klp_state { ...@@ -41,8 +36,8 @@ enum klp_state {
* can be found (optional) * can be found (optional)
* @old_addr: the address of the function being patched * @old_addr: the address of the function being patched
* @kobj: kobject for sysfs resources * @kobj: kobject for sysfs resources
* @state: tracks function-level patch application state
* @stack_node: list node for klp_ops func_stack list * @stack_node: list node for klp_ops func_stack list
* @patched: the func has been added to the klp_ops list
*/ */
struct klp_func { struct klp_func {
/* external */ /* external */
...@@ -60,8 +55,8 @@ struct klp_func { ...@@ -60,8 +55,8 @@ struct klp_func {
/* internal */ /* internal */
unsigned long old_addr; unsigned long old_addr;
struct kobject kobj; struct kobject kobj;
enum klp_state state;
struct list_head stack_node; struct list_head stack_node;
bool patched;
}; };
/** /**
...@@ -71,7 +66,7 @@ struct klp_func { ...@@ -71,7 +66,7 @@ struct klp_func {
* @kobj: kobject for sysfs resources * @kobj: kobject for sysfs resources
* @mod: kernel module associated with the patched object * @mod: kernel module associated with the patched object
* (NULL for vmlinux) * (NULL for vmlinux)
* @state: tracks object-level patch application state * @patched: the object's funcs have been added to the klp_ops list
*/ */
struct klp_object { struct klp_object {
/* external */ /* external */
...@@ -81,7 +76,7 @@ struct klp_object { ...@@ -81,7 +76,7 @@ struct klp_object {
/* internal */ /* internal */
struct kobject kobj; struct kobject kobj;
struct module *mod; struct module *mod;
enum klp_state state; bool patched;
}; };
/** /**
...@@ -90,7 +85,7 @@ struct klp_object { ...@@ -90,7 +85,7 @@ struct klp_object {
* @objs: object entries for kernel objects to be patched * @objs: object entries for kernel objects to be patched
* @list: list node for global list of registered patches * @list: list node for global list of registered patches
* @kobj: kobject for sysfs resources * @kobj: kobject for sysfs resources
* @state: tracks patch-level application state * @enabled: the patch is enabled (but operation may be incomplete)
*/ */
struct klp_patch { struct klp_patch {
/* external */ /* external */
...@@ -100,7 +95,7 @@ struct klp_patch { ...@@ -100,7 +95,7 @@ struct klp_patch {
/* internal */ /* internal */
struct list_head list; struct list_head list;
struct kobject kobj; struct kobject kobj;
enum klp_state state; bool enabled;
}; };
#define klp_for_each_object(patch, obj) \ #define klp_for_each_object(patch, obj) \
......
...@@ -348,11 +348,11 @@ static unsigned long klp_get_ftrace_location(unsigned long faddr) ...@@ -348,11 +348,11 @@ static unsigned long klp_get_ftrace_location(unsigned long faddr)
} }
#endif #endif
static void klp_disable_func(struct klp_func *func) static void klp_unpatch_func(struct klp_func *func)
{ {
struct klp_ops *ops; struct klp_ops *ops;
if (WARN_ON(func->state != KLP_ENABLED)) if (WARN_ON(!func->patched))
return; return;
if (WARN_ON(!func->old_addr)) if (WARN_ON(!func->old_addr))
return; return;
...@@ -378,10 +378,10 @@ static void klp_disable_func(struct klp_func *func) ...@@ -378,10 +378,10 @@ static void klp_disable_func(struct klp_func *func)
list_del_rcu(&func->stack_node); list_del_rcu(&func->stack_node);
} }
func->state = KLP_DISABLED; func->patched = false;
} }
static int klp_enable_func(struct klp_func *func) static int klp_patch_func(struct klp_func *func)
{ {
struct klp_ops *ops; struct klp_ops *ops;
int ret; int ret;
...@@ -389,7 +389,7 @@ static int klp_enable_func(struct klp_func *func) ...@@ -389,7 +389,7 @@ static int klp_enable_func(struct klp_func *func)
if (WARN_ON(!func->old_addr)) if (WARN_ON(!func->old_addr))
return -EINVAL; return -EINVAL;
if (WARN_ON(func->state != KLP_DISABLED)) if (WARN_ON(func->patched))
return -EINVAL; return -EINVAL;
ops = klp_find_ops(func->old_addr); ops = klp_find_ops(func->old_addr);
...@@ -437,7 +437,7 @@ static int klp_enable_func(struct klp_func *func) ...@@ -437,7 +437,7 @@ static int klp_enable_func(struct klp_func *func)
list_add_rcu(&func->stack_node, &ops->func_stack); list_add_rcu(&func->stack_node, &ops->func_stack);
} }
func->state = KLP_ENABLED; func->patched = true;
return 0; return 0;
...@@ -448,36 +448,36 @@ static int klp_enable_func(struct klp_func *func) ...@@ -448,36 +448,36 @@ static int klp_enable_func(struct klp_func *func)
return ret; return ret;
} }
static void klp_disable_object(struct klp_object *obj) static void klp_unpatch_object(struct klp_object *obj)
{ {
struct klp_func *func; struct klp_func *func;
klp_for_each_func(obj, func) klp_for_each_func(obj, func)
if (func->state == KLP_ENABLED) if (func->patched)
klp_disable_func(func); klp_unpatch_func(func);
obj->state = KLP_DISABLED; obj->patched = false;
} }
static int klp_enable_object(struct klp_object *obj) static int klp_patch_object(struct klp_object *obj)
{ {
struct klp_func *func; struct klp_func *func;
int ret; int ret;
if (WARN_ON(obj->state != KLP_DISABLED)) if (WARN_ON(obj->patched))
return -EINVAL; return -EINVAL;
if (WARN_ON(!klp_is_object_loaded(obj))) if (WARN_ON(!klp_is_object_loaded(obj)))
return -EINVAL; return -EINVAL;
klp_for_each_func(obj, func) { klp_for_each_func(obj, func) {
ret = klp_enable_func(func); ret = klp_patch_func(func);
if (ret) { if (ret) {
klp_disable_object(obj); klp_unpatch_object(obj);
return ret; return ret;
} }
} }
obj->state = KLP_ENABLED; obj->patched = true;
return 0; return 0;
} }
...@@ -488,17 +488,17 @@ static int __klp_disable_patch(struct klp_patch *patch) ...@@ -488,17 +488,17 @@ static int __klp_disable_patch(struct klp_patch *patch)
/* enforce stacking: only the last enabled patch can be disabled */ /* enforce stacking: only the last enabled patch can be disabled */
if (!list_is_last(&patch->list, &klp_patches) && if (!list_is_last(&patch->list, &klp_patches) &&
list_next_entry(patch, list)->state == KLP_ENABLED) list_next_entry(patch, list)->enabled)
return -EBUSY; return -EBUSY;
pr_notice("disabling patch '%s'\n", patch->mod->name); pr_notice("disabling patch '%s'\n", patch->mod->name);
klp_for_each_object(patch, obj) { klp_for_each_object(patch, obj) {
if (obj->state == KLP_ENABLED) if (obj->patched)
klp_disable_object(obj); klp_unpatch_object(obj);
} }
patch->state = KLP_DISABLED; patch->enabled = false;
return 0; return 0;
} }
...@@ -522,7 +522,7 @@ int klp_disable_patch(struct klp_patch *patch) ...@@ -522,7 +522,7 @@ int klp_disable_patch(struct klp_patch *patch)
goto err; goto err;
} }
if (patch->state == KLP_DISABLED) { if (!patch->enabled) {
ret = -EINVAL; ret = -EINVAL;
goto err; goto err;
} }
...@@ -540,12 +540,12 @@ static int __klp_enable_patch(struct klp_patch *patch) ...@@ -540,12 +540,12 @@ static int __klp_enable_patch(struct klp_patch *patch)
struct klp_object *obj; struct klp_object *obj;
int ret; int ret;
if (WARN_ON(patch->state != KLP_DISABLED)) if (WARN_ON(patch->enabled))
return -EINVAL; return -EINVAL;
/* enforce stacking: only the first disabled patch can be enabled */ /* enforce stacking: only the first disabled patch can be enabled */
if (patch->list.prev != &klp_patches && if (patch->list.prev != &klp_patches &&
list_prev_entry(patch, list)->state == KLP_DISABLED) !list_prev_entry(patch, list)->enabled)
return -EBUSY; return -EBUSY;
pr_notice("enabling patch '%s'\n", patch->mod->name); pr_notice("enabling patch '%s'\n", patch->mod->name);
...@@ -554,12 +554,12 @@ static int __klp_enable_patch(struct klp_patch *patch) ...@@ -554,12 +554,12 @@ static int __klp_enable_patch(struct klp_patch *patch)
if (!klp_is_object_loaded(obj)) if (!klp_is_object_loaded(obj))
continue; continue;
ret = klp_enable_object(obj); ret = klp_patch_object(obj);
if (ret) if (ret)
goto unregister; goto unregister;
} }
patch->state = KLP_ENABLED; patch->enabled = true;
return 0; return 0;
...@@ -617,20 +617,20 @@ static ssize_t enabled_store(struct kobject *kobj, struct kobj_attribute *attr, ...@@ -617,20 +617,20 @@ static ssize_t enabled_store(struct kobject *kobj, struct kobj_attribute *attr,
if (ret) if (ret)
return -EINVAL; return -EINVAL;
if (val != KLP_DISABLED && val != KLP_ENABLED) if (val > 1)
return -EINVAL; return -EINVAL;
patch = container_of(kobj, struct klp_patch, kobj); patch = container_of(kobj, struct klp_patch, kobj);
mutex_lock(&klp_mutex); mutex_lock(&klp_mutex);
if (val == patch->state) { if (patch->enabled == val) {
/* already in requested state */ /* already in requested state */
ret = -EINVAL; ret = -EINVAL;
goto err; goto err;
} }
if (val == KLP_ENABLED) { if (val) {
ret = __klp_enable_patch(patch); ret = __klp_enable_patch(patch);
if (ret) if (ret)
goto err; goto err;
...@@ -655,7 +655,7 @@ static ssize_t enabled_show(struct kobject *kobj, ...@@ -655,7 +655,7 @@ static ssize_t enabled_show(struct kobject *kobj,
struct klp_patch *patch; struct klp_patch *patch;
patch = container_of(kobj, struct klp_patch, kobj); patch = container_of(kobj, struct klp_patch, kobj);
return snprintf(buf, PAGE_SIZE-1, "%d\n", patch->state); return snprintf(buf, PAGE_SIZE-1, "%d\n", patch->enabled);
} }
static struct kobj_attribute enabled_kobj_attr = __ATTR_RW(enabled); static struct kobj_attribute enabled_kobj_attr = __ATTR_RW(enabled);
...@@ -749,7 +749,7 @@ static int klp_init_func(struct klp_object *obj, struct klp_func *func) ...@@ -749,7 +749,7 @@ static int klp_init_func(struct klp_object *obj, struct klp_func *func)
return -EINVAL; return -EINVAL;
INIT_LIST_HEAD(&func->stack_node); INIT_LIST_HEAD(&func->stack_node);
func->state = KLP_DISABLED; func->patched = false;
/* The format for the sysfs directory is <function,sympos> where sympos /* The format for the sysfs directory is <function,sympos> where sympos
* is the nth occurrence of this symbol in kallsyms for the patched * is the nth occurrence of this symbol in kallsyms for the patched
...@@ -804,7 +804,7 @@ static int klp_init_object(struct klp_patch *patch, struct klp_object *obj) ...@@ -804,7 +804,7 @@ static int klp_init_object(struct klp_patch *patch, struct klp_object *obj)
if (!obj->funcs) if (!obj->funcs)
return -EINVAL; return -EINVAL;
obj->state = KLP_DISABLED; obj->patched = false;
obj->mod = NULL; obj->mod = NULL;
klp_find_object_module(obj); klp_find_object_module(obj);
...@@ -845,7 +845,7 @@ static int klp_init_patch(struct klp_patch *patch) ...@@ -845,7 +845,7 @@ static int klp_init_patch(struct klp_patch *patch)
mutex_lock(&klp_mutex); mutex_lock(&klp_mutex);
patch->state = KLP_DISABLED; patch->enabled = false;
ret = kobject_init_and_add(&patch->kobj, &klp_ktype_patch, ret = kobject_init_and_add(&patch->kobj, &klp_ktype_patch,
klp_root_kobj, "%s", patch->mod->name); klp_root_kobj, "%s", patch->mod->name);
...@@ -891,7 +891,7 @@ int klp_unregister_patch(struct klp_patch *patch) ...@@ -891,7 +891,7 @@ int klp_unregister_patch(struct klp_patch *patch)
goto out; goto out;
} }
if (patch->state == KLP_ENABLED) { if (patch->enabled) {
ret = -EBUSY; ret = -EBUSY;
goto out; goto out;
} }
...@@ -978,13 +978,13 @@ int klp_module_coming(struct module *mod) ...@@ -978,13 +978,13 @@ int klp_module_coming(struct module *mod)
goto err; goto err;
} }
if (patch->state == KLP_DISABLED) if (!patch->enabled)
break; break;
pr_notice("applying patch '%s' to loading module '%s'\n", pr_notice("applying patch '%s' to loading module '%s'\n",
patch->mod->name, obj->mod->name); patch->mod->name, obj->mod->name);
ret = klp_enable_object(obj); ret = klp_patch_object(obj);
if (ret) { if (ret) {
pr_warn("failed to apply patch '%s' to module '%s' (%d)\n", pr_warn("failed to apply patch '%s' to module '%s' (%d)\n",
patch->mod->name, obj->mod->name, ret); patch->mod->name, obj->mod->name, ret);
...@@ -1035,10 +1035,10 @@ void klp_module_going(struct module *mod) ...@@ -1035,10 +1035,10 @@ void klp_module_going(struct module *mod)
if (!klp_is_module(obj) || strcmp(obj->name, mod->name)) if (!klp_is_module(obj) || strcmp(obj->name, mod->name))
continue; continue;
if (patch->state != KLP_DISABLED) { if (patch->enabled) {
pr_notice("reverting patch '%s' on unloading module '%s'\n", pr_notice("reverting patch '%s' on unloading module '%s'\n",
patch->mod->name, obj->mod->name); patch->mod->name, obj->mod->name);
klp_disable_object(obj); klp_unpatch_object(obj);
} }
klp_free_object_loaded(obj); klp_free_object_loaded(obj);
......
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