2013-08-12 11:02:04

by Wladislav Wiebe

[permalink] [raw]
Subject: [PATCH 1/1] of: fdt: fix memory initialization for expanded DT

Already existing property flags are filled wrong for properties created from
initial FDT. This could cause problems if this DYNAMIC device-tree functions
are used later, i.e. properties are attached/detached/replaced. Simply dumping
flags from the running system show, that some initial static (not allocated via
kzmalloc()) nodes are marked as dynamic.

I putted some debug extensions to property_proc_show(..) :
..
+ if (OF_IS_DYNAMIC(pp))
+ pr_err("DEBUG: xxx : OF_IS_DYNAMIC\n");
+ if (OF_IS_DETACHED(pp))
+ pr_err("DEBUG: xxx : OF_IS_DETACHED\n");

when you operate on the nodes (e.g.: ~$ cat /proc/device-tree/*some_node*) you
will see that those flags are filled wrong, basically in most cases it will dump
a DYNAMIC or DETACHED status, which is in not true.
(BTW. this OF_IS_DETACHED is a own define for debug purposes which which just
make a test_bit(OF_DETACHED, &x->_flags)

If nodes are dynamic kernel is allowed to kfree() them. But it will crash
attempting to do so on the nodes from FDT -- they are not allocated via
kzmalloc().

Signed-off-by: Wladislav Wiebe <[email protected]>
---
drivers/of/fdt.c | 2 ++
1 files changed, 2 insertions(+), 0 deletions(-)

diff --git a/drivers/of/fdt.c b/drivers/of/fdt.c
index 6bb7cf2..b10ba00 100644
--- a/drivers/of/fdt.c
+++ b/drivers/of/fdt.c
@@ -392,6 +392,8 @@ static void __unflatten_device_tree(struct boot_param_header *blob,
mem = (unsigned long)
dt_alloc(size + 4, __alignof__(struct device_node));

+ memset((void *)mem, 0, size);
+
((__be32 *)mem)[size / 4] = cpu_to_be32(0xdeadbeef);

pr_debug(" unflattening %lx...\n", mem);
--
1.7.1



2013-08-16 12:30:36

by Alexander Sverdlin

[permalink] [raw]
Subject: Re: [PATCH 1/1] of: fdt: fix memory initialization for expanded DT

Hi!

On 08/12/2013 01:00 PM, ext Wladislav Wiebe wrote:
> Already existing property flags are filled wrong for properties created from
> initial FDT. This could cause problems if this DYNAMIC device-tree functions
> are used later, i.e. properties are attached/detached/replaced. Simply dumping
> flags from the running system show, that some initial static (not allocated via
> kzmalloc()) nodes are marked as dynamic.
>
> I putted some debug extensions to property_proc_show(..) :
> ..
> + if (OF_IS_DYNAMIC(pp))
> + pr_err("DEBUG: xxx : OF_IS_DYNAMIC\n");
> + if (OF_IS_DETACHED(pp))
> + pr_err("DEBUG: xxx : OF_IS_DETACHED\n");
>
> when you operate on the nodes (e.g.: ~$ cat /proc/device-tree/*some_node*) you
> will see that those flags are filled wrong, basically in most cases it will dump
> a DYNAMIC or DETACHED status, which is in not true.
> (BTW. this OF_IS_DETACHED is a own define for debug purposes which which just
> make a test_bit(OF_DETACHED, &x->_flags)
>
> If nodes are dynamic kernel is allowed to kfree() them. But it will crash
> attempting to do so on the nodes from FDT -- they are not allocated via
> kzmalloc().
>
> Signed-off-by: Wladislav Wiebe <[email protected]>

Acked-by: Alexander Sverdlin <[email protected]>

> ---
> drivers/of/fdt.c | 2 ++
> 1 files changed, 2 insertions(+), 0 deletions(-)
>
> diff --git a/drivers/of/fdt.c b/drivers/of/fdt.c
> index 6bb7cf2..b10ba00 100644
> --- a/drivers/of/fdt.c
> +++ b/drivers/of/fdt.c
> @@ -392,6 +392,8 @@ static void __unflatten_device_tree(struct boot_param_header *blob,
> mem = (unsigned long)
> dt_alloc(size + 4, __alignof__(struct device_node));
>
> + memset((void *)mem, 0, size);
> +
> ((__be32 *)mem)[size / 4] = cpu_to_be32(0xdeadbeef);
>
> pr_debug(" unflattening %lx...\n", mem);
>

--
Best regards,
Alexander Sverdlin.

2013-08-28 20:21:00

by Grant Likely

[permalink] [raw]
Subject: Re: [PATCH 1/1] of: fdt: fix memory initialization for expanded DT

On Mon, 12 Aug 2013 13:00:26 +0200, Wladislav Wiebe <[email protected]> wrote:
> Already existing property flags are filled wrong for properties created from
> initial FDT. This could cause problems if this DYNAMIC device-tree functions
> are used later, i.e. properties are attached/detached/replaced. Simply dumping
> flags from the running system show, that some initial static (not allocated via
> kzmalloc()) nodes are marked as dynamic.
>
> I putted some debug extensions to property_proc_show(..) :
> ..
> + if (OF_IS_DYNAMIC(pp))
> + pr_err("DEBUG: xxx : OF_IS_DYNAMIC\n");
> + if (OF_IS_DETACHED(pp))
> + pr_err("DEBUG: xxx : OF_IS_DETACHED\n");
>
> when you operate on the nodes (e.g.: ~$ cat /proc/device-tree/*some_node*) you
> will see that those flags are filled wrong, basically in most cases it will dump
> a DYNAMIC or DETACHED status, which is in not true.
> (BTW. this OF_IS_DETACHED is a own define for debug purposes which which just
> make a test_bit(OF_DETACHED, &x->_flags)
>
> If nodes are dynamic kernel is allowed to kfree() them. But it will crash
> attempting to do so on the nodes from FDT -- they are not allocated via
> kzmalloc().
>
> Signed-off-by: Wladislav Wiebe <[email protected]>
> ---
> drivers/of/fdt.c | 2 ++
> 1 files changed, 2 insertions(+), 0 deletions(-)
>
> diff --git a/drivers/of/fdt.c b/drivers/of/fdt.c
> index 6bb7cf2..b10ba00 100644
> --- a/drivers/of/fdt.c
> +++ b/drivers/of/fdt.c
> @@ -392,6 +392,8 @@ static void __unflatten_device_tree(struct boot_param_header *blob,
> mem = (unsigned long)
> dt_alloc(size + 4, __alignof__(struct device_node));
>
> + memset((void *)mem, 0, size);
> +

It seems to me that this would be a problem for any call to the early
allocation function; early_init_dt_alloc_memory_arch(). There is at
least one other call to the allocator via of_alias_scan(). I think this
patch is okay for now (I'll add the missing hunk), but it should be
revisited.

Most of the platforms use the memblock_alloc implementation; that can
probably be consolidated and removed from arch code. I've just crafted a
patch and I'll be posting it shortly.

g.

2013-08-28 20:24:23

by Grant Likely

[permalink] [raw]
Subject: Re: [PATCH 1/1] of: fdt: fix memory initialization for expanded DT

On Wed, Aug 28, 2013 at 9:21 PM, Grant Likely <[email protected]> wrote:
> On Mon, 12 Aug 2013 13:00:26 +0200, Wladislav Wiebe <[email protected]> wrote:
>> Already existing property flags are filled wrong for properties created from
>> initial FDT. This could cause problems if this DYNAMIC device-tree functions
>> are used later, i.e. properties are attached/detached/replaced. Simply dumping
>> flags from the running system show, that some initial static (not allocated via
>> kzmalloc()) nodes are marked as dynamic.
>>
>> I putted some debug extensions to property_proc_show(..) :
>> ..
>> + if (OF_IS_DYNAMIC(pp))
>> + pr_err("DEBUG: xxx : OF_IS_DYNAMIC\n");
>> + if (OF_IS_DETACHED(pp))
>> + pr_err("DEBUG: xxx : OF_IS_DETACHED\n");
>>
>> when you operate on the nodes (e.g.: ~$ cat /proc/device-tree/*some_node*) you
>> will see that those flags are filled wrong, basically in most cases it will dump
>> a DYNAMIC or DETACHED status, which is in not true.
>> (BTW. this OF_IS_DETACHED is a own define for debug purposes which which just
>> make a test_bit(OF_DETACHED, &x->_flags)
>>
>> If nodes are dynamic kernel is allowed to kfree() them. But it will crash
>> attempting to do so on the nodes from FDT -- they are not allocated via
>> kzmalloc().
>>
>> Signed-off-by: Wladislav Wiebe <[email protected]>
>> ---
>> drivers/of/fdt.c | 2 ++
>> 1 files changed, 2 insertions(+), 0 deletions(-)
>>
>> diff --git a/drivers/of/fdt.c b/drivers/of/fdt.c
>> index 6bb7cf2..b10ba00 100644
>> --- a/drivers/of/fdt.c
>> +++ b/drivers/of/fdt.c
>> @@ -392,6 +392,8 @@ static void __unflatten_device_tree(struct boot_param_header *blob,
>> mem = (unsigned long)
>> dt_alloc(size + 4, __alignof__(struct device_node));
>>
>> + memset((void *)mem, 0, size);
>> +
>
> It seems to me that this would be a problem for any call to the early
> allocation function; early_init_dt_alloc_memory_arch(). There is at
> least one other call to the allocator via of_alias_scan(). I think this
> patch is okay for now (I'll add the missing hunk), but it should be
> revisited.

Oops, I see that Rob has already applied it. I'll post the other
change needed and post as a follow-on patch. Sorry for the noise.

g.