livepatch: cleanup kpl_patch kobject release
v2 cover livepatch: cleanup kpl_patch kobject release
Hello,
The 1st patch moves module_put() to release handler of klp_patch kobject.
The 2nd patch changes to free klp_patch and other kobjects without klp_mutex.
The 3rd patch switches to synchronous kobject release for klp_patch.
V2:
- remove enabled attribute before deleting this klp_patch kobject, for avoiding deadlock in deleting me
Ming Lei (3):
livepatch: remove 'struct completion finish' from klp_patch
livepatch: free klp_patch object without holding klp_mutex
livepatch: free klp_patch object synchronously
include/linux/livepatch.h | 2 -- kernel/livepatch/core.c | 67 ++++++++++++++++------------------- kernel/livepatch/core.h | 3 +- kernel/livepatch/transition.c | 23 ++++++++---- kernel/livepatch/transition.h | 2 +- 5 files changed, 50 insertions(+), 47 deletions(-)
Signature · 1 line
2.31.1
PATCH v2 1/3 livepatch: remove 'struct completion finish' from klp_patch
The completion finish is just for waiting release of the klp_patch object, then releases module refcnt. We can simply drop the module refcnt in the kobject release handler of klp_patch.
This way also helps to support allocating klp_patch from heap.
--- include/linux/livepatch.h | 1 - kernel/livepatch/core.c | 12 +++--------- 2 files changed, 3 insertions(+), 10 deletions(-)
include/linux/livepatch.h +0 -1
@@ -170,7 +170,6 @@ struct klp_patch {bool enabled;bool forced;struct work_struct free_work; -struct completion finish; };#define klp_for_each_object_static (patch, obj) \
kernel/livepatch/core.c +3 -9
@@ -551,10 +551,10 @@ static int klp_add_nops(struct klp_patch *patch)static void klp_kobj_release_patch (struct kobject *kobj) { -struct klp_patch *patch; + struct klp_patch *patch = container_of(kobj, struct klp_patch, kobj); - patch = container_of(kobj, struct klp_patch, kobj); -complete (&patch->finish); +if (!patch->forced) +module_put (patch->mod); }static struct kobj_type klp_ktype_patch = {
@@ -678,11 +678,6 @@ static void klp_free_patch_finish(struct klp_patch *patch)* cannot get enabled again. */ kobject_put (&patch->kobj); -wait_for_completion (&patch->finish); - -/* Put the module after the last access to struct klp_patch. */ -if (!patch->forced) -module_put (patch->mod); }/*
@@ -876,7 +871,6 @@ static int klp_init_patch_early(struct klp_patch *patch) patch->enabled = false; patch->forced = false;INIT_WORK (&patch->free_work, klp_free_patch_work_fn); -init_completion (&patch->finish);klp_for_each_object_static (patch, obj) {if (!obj->funcs)
Signature · 1 line
2.31.1
PATCH v2 2/3 livepatch: free klp_patch object without holding klp_mutex
In reply to Ming Leikobject_del() is called from kobject_put(), and after the klp_patch kobject is deleted, any show()/store() are done.
Once the klp_patch object is removed from list and prepared for releasing, no need to hold the global mutex of klp_mutex, so move the freeing outside of klp_mutex.
--- kernel/livepatch/core.c | 30 ++++++++++++++++++------------ kernel/livepatch/core.h | 3 +-- kernel/livepatch/transition.c | 23 +++++++++++++++++------ kernel/livepatch/transition.h | 2 +- 4 files changed, 37 insertions(+), 21 deletions(-)
kernel/livepatch/core.c +18 -12
@@ -327,7 +327,8 @@ int klp_apply_section_relocs(struct module *pmod, Elf_Shdr *sechdrs,* /sys/kernel/livepatch/<patch>/<object> * /sys/kernel/livepatch/<patch>/<object>/<function,sympos> */ -static int __klp_disable_patch (struct klp_patch *patch); +static int __klp_disable_patch (struct klp_patch *patch, +struct list_head *to_free);static ssize_t enabled_store (struct kobject *kobj,struct kobj_attribute *attr,const char *buf,size_t count)
@@ -335,6 +336,7 @@ static ssize_t enabled_store(struct kobject *kobj, struct kobj_attribute *attr,struct klp_patch *patch;int ret;bool enabled; +LIST_HEAD (to_free); ret =kstrtobool (buf, &enabled);if (ret)
@@ -360,13 +362,15 @@ static ssize_t enabled_store(struct kobject *kobj, struct kobj_attribute *attr,if (patch == klp_transition_patch)klp_reverse_transition ();else if (!enabled) - ret =__klp_disable_patch (patch); + ret =__klp_disable_patch (patch, &to_free);else ret = -EINVAL ; out:mutex_unlock (&klp_mutex); +klp_free_patches_async (&to_free); +if (ret)return ret;return count;
@@ -693,20 +697,19 @@ static void klp_free_patch_work_fn(struct work_struct *work)klp_free_patch_finish (patch); } -void klp_free_patch_async (struct klp_patch *patch) +static void klp_free_patch_async (struct klp_patch *patch) {klp_free_patch_start (patch);schedule_work (&patch->free_work); } -void klp_free_replaced_patches_async (struct klp_patch *new_patch) +void klp_free_patches_async (struct list_head *to_free) { -struct klp_patch *old_patch, *tmp_patch; +struct klp_patch *patch, *tmp_patch; -klp_for_each_patch_safe (old_patch, tmp_patch) { -if (old_patch == new_patch) -return ; -klp_free_patch_async (old_patch); +list_for_each_entry_safe (patch, tmp_patch, to_free, list) { +list_del_init (&patch->list); +klp_free_patch_async (patch); } }
@@ -915,7 +918,8 @@ static int klp_init_patch(struct klp_patch *patch)return 0 ; } -static int __klp_disable_patch (struct klp_patch *patch) +static int __klp_disable_patch (struct klp_patch *patch, +struct list_head *to_free) {struct klp_object *obj;
@@ -942,7 +946,7 @@ static int __klp_disable_patch(struct klp_patch *patch)klp_start_transition (); patch->enabled = false; -klp_try_complete_transition (); +klp_try_complete_transition (to_free);return 0 ; }
@@ -951,6 +955,7 @@ static int __klp_enable_patch(struct klp_patch *patch) {struct klp_object *obj;int ret; +LIST_HEAD (unused);if (klp_transition_patch)return -EBUSY ;
@@ -992,7 +997,8 @@ static int __klp_enable_patch(struct klp_patch *patch)klp_start_transition (); patch->enabled = true; -klp_try_complete_transition (); +klp_try_complete_transition (&unused); +WARN_ON_ONCE (!list_empty (&unused));return 0 ; err:
kernel/livepatch/core.h +1 -2
@@ -13,8 +13,7 @@ extern struct list_head klp_patches;#define klp_for_each_patch (patch) \ list_for_each_entry(patch, &klp_patches, list) -void klp_free_patch_async (struct klp_patch *patch); -void klp_free_replaced_patches_async (struct klp_patch *new_patch); +void klp_free_patches_async (struct list_head *to_free);void klp_unpatch_replaced_patches (struct klp_patch *new_patch);void klp_discard_nops (struct klp_patch *new_patch);
kernel/livepatch/transition.c +17 -6
@@ -32,12 +32,16 @@ static unsigned int klp_signals_cnt;*/ static void klp_transition_work_fn (struct work_struct *work) { +LIST_HEAD (to_free); +mutex_lock (&klp_mutex);if (klp_transition_patch) -klp_try_complete_transition (); +klp_try_complete_transition (&to_free);mutex_unlock (&klp_mutex); + +klp_free_patches_async (&to_free); } static DECLARE_DELAYED_WORK(klp_transition_work, klp_transition_work_fn);
@@ -384,7 +388,7 @@ static void klp_send_signals(void)* * If any tasks are still stuck in the initial patch state, schedule a retry. */ -void klp_try_complete_transition (void ) +void klp_try_complete_transition (struct list_head *to_free) {unsigned int cpu;struct task_struct *g, *task;
@@ -449,10 +453,17 @@ void klp_try_complete_transition(void)* klp_complete_transition() but it is called also * from klp_cancel_transition(). */ -if (!patch->enabled) -klp_free_patch_async (patch); -else if (patch->replace) -klp_free_replaced_patches_async (patch); +if (!patch->enabled) { +list_move (&patch->list, to_free); + }else if (patch->replace) { +struct klp_patch *old_patch, *tmp_patch; + +klp_for_each_patch_safe (old_patch, tmp_patch) { +if (old_patch == patch) +break ; +list_move (&old_patch->list, to_free); + } + } }/*
kernel/livepatch/transition.h +1 -1
@@ -9,7 +9,7 @@ extern struct klp_patch *klp_transition_patch;void klp_init_transition (struct klp_patch *patch,int state);void klp_cancel_transition (void );void klp_start_transition (void ); -void klp_try_complete_transition (void ); +void klp_try_complete_transition (struct list_head *to_free);void klp_reverse_transition (void );void klp_force_transition (void );
Signature · 1 line
2.31.1
PATCH v2 3/3 livepatch: free klp_patch object synchronously
In reply to Ming Leiklp_mutex isn't acquired before calling kobject_put(klp_patch), so it is fine to free klp_patch object synchronously.
One issue is that enabled store() method, in which the klp_patch kobject itself is deleted & released. However, sysfs has provided APIs for dealing with this corner case, so use sysfs_break_active_protection() and sysfs_unbreak_active_protection() for releasing klp_patch kobject from enabled_store(), meantime the enabled attribute has to be removed before deleting the klp_patch kobject.
--- include/linux/livepatch.h | 1 - kernel/livepatch/core.c | 35 +++++++++++++++-------------------- kernel/livepatch/core.h | 2 +- kernel/livepatch/transition.c | 2 +- 4 files changed, 17 insertions(+), 23 deletions(-)
include/linux/livepatch.h +0 -1
@@ -169,7 +169,6 @@ struct klp_patch {struct list_head obj_list;bool enabled;bool forced; -struct work_struct free_work; };#define klp_for_each_object_static (patch, obj) \
kernel/livepatch/core.c +15 -20
@@ -337,6 +337,7 @@ static ssize_t enabled_store(struct kobject *kobj, struct kobj_attribute *attr,int ret;bool enabled;LIST_HEAD (to_free); +struct kernfs_node *kn =NULL ; ret =kstrtobool (buf, &enabled);if (ret)
@@ -369,10 +370,18 @@ static ssize_t enabled_store(struct kobject *kobj, struct kobj_attribute *attr, out:mutex_unlock (&klp_mutex); -klp_free_patches_async (&to_free); -if (ret)return ret; + +if (list_empty (&to_free)) { + kn =sysfs_break_active_protection (kobj, &attr->attr); +WARN_ON_ONCE (!kn); +sysfs_remove_file (kobj, &attr->attr); +klp_free_patches (&to_free); +if (kn) +sysfs_unbreak_active_protection (kn); + } +return count; }
@@ -684,32 +693,19 @@ static void klp_free_patch_finish(struct klp_patch *patch)kobject_put (&patch->kobj); } -/* -* The livepatch might be freed from sysfs interface created by the patch. -* This work allows to wait until the interface is destroyed in a separate -* context. -*/ -static void klp_free_patch_work_fn (struct work_struct *work) -{ -struct klp_patch *patch = - container_of(work, struct klp_patch, free_work); - -klp_free_patch_finish (patch); -} - -static void klp_free_patch_async (struct klp_patch *patch) +static void klp_free_patch (struct klp_patch *patch) {klp_free_patch_start (patch); -schedule_work (&patch->free_work); +klp_free_patch_finish (patch); } -void klp_free_patches_async (struct list_head *to_free) +void klp_free_patches (struct list_head *to_free) {struct klp_patch *patch, *tmp_patch;list_for_each_entry_safe (patch, tmp_patch, to_free, list) {list_del_init (&patch->list); -klp_free_patch_async (patch); +klp_free_patch (patch); } }
@@ -873,7 +869,6 @@ static int klp_init_patch_early(struct klp_patch *patch)kobject_init (&patch->kobj, &klp_ktype_patch); patch->enabled = false; patch->forced = false; -INIT_WORK (&patch->free_work, klp_free_patch_work_fn);klp_for_each_object_static (patch, obj) {if (!obj->funcs)
kernel/livepatch/core.h +1 -1
@@ -13,7 +13,7 @@ extern struct list_head klp_patches;#define klp_for_each_patch (patch) \ list_for_each_entry(patch, &klp_patches, list) -void klp_free_patches_async (struct list_head *to_free); +void klp_free_patches (struct list_head *to_free);void klp_unpatch_replaced_patches (struct klp_patch *new_patch);void klp_discard_nops (struct klp_patch *new_patch);
kernel/livepatch/transition.c +1 -1
@@ -41,7 +41,7 @@ static void klp_transition_work_fn(struct work_struct *work)mutex_unlock (&klp_mutex); -klp_free_patches_async (&to_free); +klp_free_patches (&to_free); } static DECLARE_DELAYED_WORK(klp_transition_work, klp_transition_work_fn);
Signature · 1 line
2.31.1