of: reserved_mem: fix reserve memory leak
PATCH 1/1 of: reserved_mem: fix reserve memory leak
The __reserved_mem_init_node will call region specific reserved memory init codes, but once all compatibled init codes failed, the memory region will left in memory.reserved and cause leakage.
Take cma reserve memory DTS for example, if user declare 1MB size, which is not align to (PAGE_SIZE << max(MAX_ORDER - 1, pageblock_order)), rmem_cma_setup will return -EINVAL. Meanwhile, rmem_dma_setup will also return -EINVAL since "reusable" property is not set. If finally there is no reserved memory init pick up this memory, kernel will left the 1MB leak in memory.reserved.
This patch will remove this kind of memory from memory.reserved, only when __reserved_mem_init_node return neither 0 nor -ENOENT.
--- drivers/of/of_reserved_mem.c | 22 +++++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-)
drivers/of/of_reserved_mem.c +17 -5
@@ -181,6 +181,7 @@ static int __init __reserved_mem_init_node(struct reserved_mem *rmem) {extern const struct of_device_id __reservedmem_of_table[];const struct of_device_id *i; +int ret = -ENOENT ;for (i = __reservedmem_of_table; i < &__rmem_of_table_sentinel; i++) {reservedmem_of_init_fn initfn = i->data;
@@ -189,13 +190,14 @@ static int __init __reserved_mem_init_node(struct reserved_mem *rmem)if (!of_flat_dt_is_compatible (rmem->fdt_node, compat))continue ; -if (initfn (rmem) ==0 ) { + ret =initfn (rmem); +if (ret ==0 ) {pr_info ("initialized node %s, compatible id %s\n" , rmem->name, compat); -return 0 ; +break ; } } -return -ENOENT ; +return ret; }static int __init __rmem_cmp (const void *a,const void *b)
@@ -255,7 +257,9 @@ void __init fdt_init_reserved_mem(void)int len;const __be32 *prop;int err =0 ; +int nomap; + nomap =of_get_flat_dt_prop (node,"no-map" ,NULL ) !=NULL ; prop =of_get_flat_dt_prop (node,"phandle" , &len);if (!prop) prop =of_get_flat_dt_prop (node,"linux,phandle" , &len);
@@ -265,8 +269,16 @@ void __init fdt_init_reserved_mem(void)if (rmem->size ==0 ) err =__reserved_mem_alloc_size (node, rmem->name, &rmem->base, &rmem->size); -if (err ==0 ) -__reserved_mem_init_node (rmem); +if (err ==0 ) { + err =__reserved_mem_init_node (rmem); +if (err !=0 && err != -ENOENT ) { +pr_info ("node %s compatible matching fail\n" , + rmem->name); +memblock_free (rmem->base, rmem->size); +if (nomap) +memblock_add (rmem->base, rmem->size); + } + } } }
Signature · 1 line
2.17.1
You should Cc the author(s) of this code. I've added Marek.
Quoted 12 lines · pierre Kuo
The
__reserved_mem_init_nodewill call region specific reserved memory init codes, but once all compatibled init codes failed, the memory region will left in memory.reserved and cause leakage.Take cma reserve memory DTS for example, if user declare 1MB size, which is not align to (
PAGE_SIZE<<max(MAX_ORDER - 1, pageblock_order)),rmem_cma_setupwill return-EINVAL. Meanwhile,rmem_dma_setupwill also return-EINVALsince "reusable" property is not set. If finally there is no reserved memory init pick up this memory, kernel will left the 1MB leak in memory.reserved.
Quoted 2 lines
This patch will remove this kind of memory from memory.reserved, only when
__reserved_mem_init_nodereturn neither 0 nor-ENOENT.
I'm not sure that un-reserving memory on error is the correct behavior. It may be fine for something like CMA, but if it is some shared memory used by another processor in the system not reserving it would probably never be correct.
Quoted 68 lines
Signed-off-by pierre Kuo <[email protected]>--- drivers/of/of_reserved_mem.c | 22 +++++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-)drivers/of/of_reserved_mem.c +17
-6@@ -181,6 +181,7 @@ static int __init __reserved_mem_init_node(struct reserved_mem *rmem) {extern const struct of_device_id __reservedmem_of_table[];const struct of_device_id *i; +int ret = -ENOENT ;for (i = __reservedmem_of_table; i < &__rmem_of_table_sentinel; i++) {reservedmem_of_init_fn initfn = i->data;@@ -189,13 +190,14 @@ static int __init __reserved_mem_init_node(struct reserved_mem *rmem)if (!of_flat_dt_is_compatible (rmem->fdt_node, compat))continue ; -if (initfn (rmem) ==0 ) { + ret =initfn (rmem); +if (ret ==0 ) {pr_info ("initialized node %s, compatible id %s\n" , rmem->name, compat); -return 0 ; +break ; } } -return -ENOENT ; +return ret; }static int __init __rmem_cmp (const void *a,const void *b)@@ -255,7 +257,9 @@ void __init fdt_init_reserved_mem(void)int len;const __be32 *prop;int err =0 ; +int nomap; + nomap =of_get_flat_dt_prop (node,"no-map" ,NULL ) !=NULL ; prop =of_get_flat_dt_prop (node,"phandle" , &len);if (!prop) prop =of_get_flat_dt_prop (node,"linux,phandle" , &len);@@ -265,8 +269,16 @@ void __init fdt_init_reserved_mem(void)if (rmem->size ==0 ) err =__reserved_mem_alloc_size (node, rmem->name, &rmem->base, &rmem->size); -if (err ==0 ) -__reserved_mem_init_node (rmem); +if (err ==0 ) { + err =__reserved_mem_init_node (rmem); +if (err !=0 && err != -ENOENT ) { +pr_info ("node %s compatible matching fail\n" , + rmem->name); +memblock_free (rmem->base, rmem->size); +if (nomap) +memblock_add (rmem->base, rmem->size); + } + } } } --2.17.1
Quoted 1 line
You should Cc the author(s) of this code. I've added Marek.
Thanks ^^
The cc lists I got were from get_maintainer.pl, no matter running with of_reserved_mem.c or patch file.
(with file)
$ perl scripts/get_maintainer.pl --separator , --norolestats drivers/of/of_reserved_mem.c Rob Herring <[email protected]>,Frank Rowand <[email protected]>,[email protected],[email protected]
(with patch)
$ perl scripts/get_maintainer.pl --separator , --norolestats 0001-of-reserved_mem-fix-reserve-memory-leak.patch Rob Herring <[email protected]>,Frank Rowand <[email protected]>,[email protected],[email protected]
Quoted 11 lines
Quoted 11 lines
The
__reserved_mem_init_nodewill call region specific reserved memory init codes, but once all compatibled init codes failed, the memory region will left in memory.reserved and cause leakage.Take cma reserve memory DTS for example, if user declare 1MB size, which is not align to (
PAGE_SIZE<<max(MAX_ORDER - 1, pageblock_order)),rmem_cma_setupwill return-EINVAL. Meanwhile,rmem_dma_setupwill also return-EINVALsince "reusable" property is not set. If finally there is no reserved memory init pick up this memory, kernel will left the 1MB leak in memory.reserved.
Quoted 7 lines
Quoted 2 lines
This patch will remove this kind of memory from memory.reserved, only when
__reserved_mem_init_nodereturn neither 0 nor-ENOENT.I'm not sure that un-reserving memory on error is the correct behavior. It may be fine for something like CMA, but if it is some shared memory used by another processor in the system not reserving it would probably never be correct.
In this patch, we un-reserving memory ONLY if explicit compatible matching fail. That mean driver found something wrong while matching and let OS know. (But reserved-memory without compatible property will not be affected.)
So per ur explaination, there would be cases that driver reported matching memory fail,
but the memory is still needed by another processor?
Appreciate ur kind help,
hi Rob, Marek and Frank:
Quoted 19 lines
Quoted 11 lines
Quoted 11 lines
The
__reserved_mem_init_nodewill call region specific reserved memory init codes, but once all compatibled init codes failed, the memory region will left in memory.reserved and cause leakage.Take cma reserve memory DTS for example, if user declare 1MB size, which is not align to (
PAGE_SIZE<<max(MAX_ORDER - 1, pageblock_order)),rmem_cma_setupwill return-EINVAL. Meanwhile,rmem_dma_setupwill also return-EINVALsince "reusable" property is not set. If finally there is no reserved memory init pick up this memory, kernel will left the 1MB leak in memory.reserved.Quoted 7 lines
Quoted 2 lines
This patch will remove this kind of memory from memory.reserved, only when
__reserved_mem_init_nodereturn neither 0 nor-ENOENT.I'm not sure that un-reserving memory on error is the correct behavior. It may be fine for something like CMA, but if it is some shared memory used by another processor in the system not reserving it would probably never be correct.
Quoted 7 lines
In this patch, we un-reserving memory ONLY if explicit compatible matching fail. That mean driver found something wrong while matching and let OS know. (But reserved-memory without compatible property will not be affected.)
So per ur explaination, there would be cases that driver reported matching memory fail,
but the memory is still needed by another processor?
Would you mind to give some comment and suggestion for this patch? Sincerely appreciate ur kind help.
hi Rob, Marek and Frank:
Quoted 7 lines
Quoted 7 lines
In this patch, we un-reserving memory ONLY if explicit compatible matching fail. That mean driver found something wrong while matching and let OS know. (But reserved-memory without compatible property will not be affected.)
So per ur explaination, there would be cases that driver reported matching memory fail,
but the memory is still needed by another processor?
Would you mind to give some comment and suggestion for this patch?
Sincerely appreciate ur kind help,
hi Rob, Marek and Frank:
Quoted 9 lines
Quoted 8 lines
In this patch, we un-reserving memory ONLY if explicit compatible matching fail. That mean driver found something wrong while matching and let OS know. (But reserved-memory without compatible property will not be affected.)
So per ur explaination, there would be cases that driver reported matching memory fail,
but the memory is still needed by another processor?
Would you mind to give some comment and suggestion for this patch?
Sincerely appreciate ur kind help.
Rob Herring
maintainerQuoted 11 lines · pierre Kuo
The
__reserved_mem_init_nodewill call region specific reserved memory init codes, but once all compatibled init codes failed, the memory region will left in memory.reserved and cause leakage.Take cma reserve memory DTS for example, if user declare 1MB size, which is not align to (
PAGE_SIZE<<max(MAX_ORDER - 1, pageblock_order)),rmem_cma_setupwill return-EINVAL. Meanwhile,rmem_dma_setupwill also return-EINVALsince "reusable" property is not set. If finally there is no reserved memory init pick up this memory, kernel will left the 1MB leak in memory.reserved.
Quoted 7 lines
This patch will remove this kind of memory from memory.reserved, only when
__reserved_mem_init_nodereturn neither 0 nor-ENOENT.Signed-off-by pierre Kuo <[email protected]>--- drivers/of/of_reserved_mem.c | 22 +++++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-)
As no one else seems to have any comments, I've applied it.
Rob
Hi Rob:
Quoted 14 lines · pierre Kuo
Quoted 14 lines
The
__reserved_mem_init_nodewill call region specific reserved memory init codes, but once all compatibled init codes failed, the memory region will left in memory.reserved and cause leakage.Take cma reserve memory DTS for example, if user declare 1MB size, which is not align to (
PAGE_SIZE<<max(MAX_ORDER - 1, pageblock_order)),rmem_cma_setupwill return-EINVAL. Meanwhile,rmem_dma_setupwill also return-EINVALsince "reusable" property is not set. If finally there is no reserved memory init pick up this memory, kernel will left the 1MB leak in memory.reserved.This patch will remove this kind of memory from memory.reserved, only when
__reserved_mem_init_nodereturn neither 0 nor-ENOENT.
Quoted 7 lines
Quoted 4 lines
Signed-off-by pierre Kuo <[email protected]>--- drivers/of/of_reserved_mem.c | 22 +++++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-)As no one else seems to have any comments, I've applied it.
Thanks for your help.
Quoted 1 line
hi Rob:
Quoted 1 line
As no one else seems to have any comments, I've applied it.
Sorry for bothering you.
Since I haven't see this patch on below up stream repository, "git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git" if there is anything wrong about the patch, please let me know.
Appreciate ur kind help,