Re: [PATCH v14 07/11] livepatch: Add atomic replace

From: Josh Poimboeuf
Date: Thu Dec 13 2018 - 17:55:36 EST


On Thu, Nov 29, 2018 at 10:44:27AM +0100, Petr Mladek wrote:
> @@ -146,6 +150,7 @@ struct klp_object {
> * struct klp_patch - patch structure for live patching
> * @mod: reference to the live patch module
> * @objs: object entries for kernel objects to be patched
> + * @replace: replace all already registered patches

"all already registered" -> "all existing" ?

since we don't have a concept of "registered" patches anymore.

> @@ -415,6 +449,124 @@ static struct attribute *klp_patch_attrs[] = {
> NULL
> };
>
> +/*
> + * Dynamically allocated objects and functions.
> + */

I don't think this comment is needed.

> +static void klp_free_object_dynamic(struct klp_object *obj)
> +{
> + kfree(obj->name);
> + kfree(obj);
> +}
> @@ -456,6 +620,8 @@ static void klp_free_funcs(struct klp_object *obj)
> if (func->kobj_alive) {
> func->kobj_alive = false;
> kobject_put(&func->kobj);
> + } else if (func->nop) {
> + klp_free_func_nop(func);

This removes 'func' from the list, so it needs to do a 'safe' list
iteration.

> }
> }
> }
> @@ -467,8 +633,12 @@ static void klp_free_object_loaded(struct klp_object *obj)
>
> obj->mod = NULL;
>
> - klp_for_each_func(obj, func)
> + klp_for_each_func(obj, func) {
> func->old_func = NULL;
> +
> + if (func->nop)
> + func->new_func = NULL;
> + }
> }
>
> static void klp_free_objects(struct klp_patch *patch)
> @@ -482,6 +652,8 @@ static void klp_free_objects(struct klp_patch *patch)
> if (obj->kobj_alive) {
> obj->kobj_alive = false;
> kobject_put(&obj->kobj);
> + } else if (obj->dynamic) {
> + klp_free_object_dynamic(obj);

Ditto.

> +void klp_discard_replaced_patches(struct klp_patch *new_patch)
> +{
> + struct klp_patch *old_patch, *tmp_patch;
> +
> + list_for_each_entry_safe(old_patch, tmp_patch, &klp_patches, list) {
> + if (old_patch == new_patch)
> + return;
> +
> + old_patch->enabled = false;
> + klp_unpatch_objects(old_patch);
> + klp_free_patch_start(old_patch);
> + schedule_work(&old_patch->free_work);
> + }

This doesn't need the "safe" list iteration because it doesn't remove
the patch from the list.

Side note, it would probably be useful to have a klp_for_each_patch()
helper.

--
Josh