Re: [PATCH v4 02/18] of: overlay: add missing of_node_put() after add new node to changeset

From: Rob Herring
Date: Thu Oct 18 2018 - 13:05:07 EST


On Mon, Oct 15, 2018 at 07:37:22PM -0700, frowand.list@xxxxxxxxx wrote:
> From: Frank Rowand <frank.rowand@xxxxxxxx>
>
> The refcount of a newly added overlay node decrements to one
> (instead of zero) when the overlay changeset is destroyed. This
> change will cause the final decrement be to zero.
>
> After applying this patch, new validation warnings will be
> reported from the devicetree unittest during boot due to
> a pre-existing devicetree bug. The warnings will be similar to:
>
> OF: ERROR: memory leak of_node_release() overlay node /testcase-data/overlay-node/test-bus/test-unittest4 before free overlay changeset

Same comment on formatting.

>
> This pre-existing devicetree bug will also trigger a WARN_ONCE() from
> refcount_sub_and_test_checked() when an overlay changeset is
> destroyed without having first been applied. This scenario occurs
> when an error in the overlay is detected during the overlay changeset
> creation:
>
> WARNING: CPU: 0 PID: 1 at lib/refcount.c:187 refcount_sub_and_test_checked+0xa8/0xbc
> refcount_t: underflow; use-after-free.
>
> (unwind_backtrace) from (show_stack+0x10/0x14)
> (show_stack) from (dump_stack+0x6c/0x8c)
> (dump_stack) from (__warn+0xdc/0x104)
> (__warn) from (warn_slowpath_fmt+0x44/0x6c)
> (warn_slowpath_fmt) from (refcount_sub_and_test_checked+0xa8/0xbc)
> (refcount_sub_and_test_checked) from (kobject_put+0x24/0x208)
> (kobject_put) from (of_changeset_destroy+0x2c/0xb4)
> (of_changeset_destroy) from (free_overlay_changeset+0x1c/0x9c)
> (free_overlay_changeset) from (of_overlay_remove+0x284/0x2cc)
> (of_overlay_remove) from (of_unittest_apply_revert_overlay_check.constprop.4+0xf8/0x1e8)
> (of_unittest_apply_revert_overlay_check.constprop.4) from (of_unittest_overlay+0x960/0xed8)
> (of_unittest_overlay) from (of_unittest+0x1cc4/0x2138)
> (of_unittest) from (do_one_initcall+0x4c/0x28c)
> (do_one_initcall) from (kernel_init_freeable+0x29c/0x378)
> (kernel_init_freeable) from (kernel_init+0x8/0x110)
> (kernel_init) from (ret_from_fork+0x14/0x2c)
>
> Signed-off-by: Frank Rowand <frank.rowand@xxxxxxxx>
> ---
> drivers/of/overlay.c | 4 +++-
> 1 file changed, 3 insertions(+), 1 deletion(-)
>
> diff --git a/drivers/of/overlay.c b/drivers/of/overlay.c
> index 1176cb4b6e4e..32cfee68f2e3 100644
> --- a/drivers/of/overlay.c
> +++ b/drivers/of/overlay.c
> @@ -379,7 +379,9 @@ static int add_changeset_node(struct overlay_changeset *ovcs,
> if (ret)
> return ret;
>
> - return build_changeset_next_level(ovcs, tchild, node);
> + ret = build_changeset_next_level(ovcs, tchild, node);
> + of_node_put(tchild);
> + return ret;
> }
>
> if (node->phandle && tchild->phandle)
> --
> Frank Rowand <frank.rowand@xxxxxxxx>
>