Re: Resend [PATCH] kernel/resource.c: invalidate parent when freed resource has childs

From: Linus Torvalds
Date: Fri Aug 09 2019 - 16:10:20 EST


On Fri, Aug 9, 2019 at 6:50 AM Schmid, Carsten
<Carsten_Schmid@xxxxxxxxxx> wrote:
>
> @@ -1200,6 +1200,15 @@ void __release_region(struct resource *parent, resource_size_t start,
> write_unlock(&resource_lock);
> if (res->flags & IORESOURCE_MUXED)
> wake_up(&muxed_resource_wait);
> +
> + write_lock(&resource_lock);
> + if (res->child) {
> + printk(KERN_WARNING "__release_region: %s has child %s,"
> + "invalidating childs parent\n",
> + res->name, res->child->name);
> + res->child->parent = NULL;
> + }
> + write_unlock(&resource_lock);
> free_resource(res);

So I think that this should be inside the previous resource_lock, and
before the whole "wake up muxed resource".

Also, a few other issues:

- what about other freeing cases? I'm looking at

release_mem_region_adjustable()

which has the same pattern where a resource may be freed.

- what about multiple children? Your patch sets res->child->parent to
NULL, but what about possible other children (iow, the
res->child->sibling list)

- releasing a resource without having released its children is a
nasty bug, but the bug is now here in release_region, it is in the
*caller*. The printk() (or pr_warn()) doesn't really help find that.

So my gut feel is that this patch is a symptom of a real bug, and a
warning is worthwhile to fix that bug, but more thought is needed.

Maybe something more along the line of

diff --git a/kernel/resource.c b/kernel/resource.c
index 7ea4306503c5..ebe06d77b06a 100644
--- a/kernel/resource.c
+++ b/kernel/resource.c
@@ -1211,6 +1211,8 @@ void __release_region(struct resource
*parent, resource_size_t start,
}
if (res->start != start || res->end != end)
break;
+ if (WARN_ON_ONCE(res->child))
+ break;
*p = res->sibling;
write_unlock(&resource_lock);
if (res->flags & IORESOURCE_MUXED)

would be more appropriate? It simply refuses to free a resource that
has children, and gives a warning (with a backtrace) for the situation
(since clearly we now end up with a resource leak).

Linus