Re: [RFC PATCH 5/5] livepatch: Do not manually track kobject initialization

From: Petr Mladek
Date: Thu May 02 2019 - 03:31:06 EST


On Thu 2019-05-02 09:12:32, Greg Kroah-Hartman wrote:
> On Thu, May 02, 2019 at 12:31:42PM +1000, Tobin C. Harding wrote:
> > Currently we use custom logic to track kobject initialization. Recently
> > a predicate function was added to the kobject API so we now no longer
> > need to do this.
> >
> > Use kobject API to check for initialized state of kobjects instead of
> > using custom logic to track state.
> >
> > Signed-off-by: Tobin C. Harding <tobin@xxxxxxxxxx>
> > ---
> > include/linux/livepatch.h | 6 ------
> > kernel/livepatch/core.c | 18 +++++-------------
> > 2 files changed, 5 insertions(+), 19 deletions(-)
> >
> > @@ -626,7 +626,7 @@ static void __klp_free_objects(struct klp_patch *patch, bool nops_only)
> > list_del(&obj->node);
> >
> > /* Might be called from klp_init_patch() error path. */
> > - if (obj->kobj_added) {
> > + if (kobject_is_initialized(&obj->kobj)) {
> > kobject_put(&obj->kobj);
> > } else if (obj->dynamic) {
> > klp_free_object_dynamic(obj);
>
> Same here, let's not be lazy.
>
> The code should "know" if the kobject has been initialized or not
> because it is the entity that asked for it to be initialized. Don't add
> extra logic to the kobject core (like the patch before this did) just
> because this one subsystem wanted to only write 1 "cleanup" function.

We use kobject for a mix of statically and dynamically defined
structures[*]. And we misunderstood the behavior of kobject_init().

Anyway, the right solution is to call kobject_init()
already in klp_init_patch_early() for the statically
defined structures and in klp_alloc*() for the dynamically
allocated ones. Then we could simply call kobject_put()
every time.

Tobin, this goes deeper into the livepatching code that
you probably expected. Do you want to do the above
suggested change or should I prepare the patch?

Anyway, thanks for working on this.


[*] Yes, we know that kobject was not designed for
static structures. We even tried to use them but
there was a lot of extra code with not a big benefit.

Best Regards,
Petr