Skip to contentSkip to conversation

memblock: add assertion for zero allocation size

PATCH Superseded Vineet Gupta 4 participants 15 messages Feb 21, 2013 – May 31, 2013 1 message not in the archive

Subsystems (3)
lore
Share & tools
lore

A message that is not in the archive (sent off-list or not yet received): [email protected]

#1 lore raw

PATCH 1/2 memblock: add assertion for zero allocation size

This came to light when calling memblock allocator from arc port (for copying flattended DT). If a "0" alignment is passed, the allocator round_up() call incorrectly rounds up the size to 0.

round_up(num, alignto) => ((num - 1) | (alignto -1)) + 1

While the obvious allocation failure causes kernel to panic, it is better to BUG_ON() if effective size for allocation (as passed by caller and/or computed after alignemtn rounding) is zero.

---
 mm/memblock.c |    2 ++
 1 files changed, 2 insertions(+), 0 deletions(-)
mm/memblock.c +2 -0
index 1bcd9b9..32b36d0 100644
@@ -824,6 +824,8 @@ static phys_addr_t __init memblock_alloc_base_nid(phys_addr_t size,
 	/* align @size to avoid excessive fragmentation on reserved array */
 	size = round_up(size, align);
 
+	BUG_ON(!size);
+
 	found = memblock_find_in_range_node(0, max_addr, size, align, nid);
 	if (found && !memblock_reserve(found, size))
 		return found;
Signature · 7 lines
1.7.4.1

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to [email protected].  For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
#2 lore raw

[+Cc: hpa]

Quoted 9 lines · Vineet Gupta

This came to light when calling memblock allocator from arc port (for copying flattended DT). If a "0" alignment is passed, the allocator round_up() call incorrectly rounds up the size to 0.

round_up(num, alignto) => ((num - 1) | (alignto -1)) + 1

While the obvious allocation failure causes kernel to panic, it is better to BUG_ON() if effective size for allocation (as passed by caller and/or computed after alignemtn rounding) is zero.

should we just make align to 1 instead of 0 ?

or BUG_ON(!align) instead?

Quoted 29 lines
---
 mm/memblock.c |    2 ++
 1 files changed, 2 insertions(+), 0 deletions(-)
mm/memblock.c +2 -1
index 1bcd9b9..32b36d0 100644
@@ -824,6 +824,8 @@ static phys_addr_t __init memblock_alloc_base_nid(phys_addr_t size,
        /* align @size to avoid excessive fragmentation on reserved array */
        size = round_up(size, align);
 
+       BUG_ON(!size);
+
        found = memblock_find_in_range_node(0, max_addr, size, align, nid);
        if (found && !memblock_reserve(found, size))
                return found;
--

1.7.4.1

Quoted 5 lines

--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>

#3 lore raw
Quoted 8 lines · Yinghai Lu

[+Cc: hpa]

Quoted 4 lines · Vineet Gupta

This came to light when calling memblock allocator from arc port (for copying flattended DT). If a "0" alignment is passed, the allocator round_up() call incorrectly rounds up the size to 0.

Quoted 6 lines
Quoted 5 lines

round_up(num, alignto) => ((num - 1) | (alignto -1)) + 1

While the obvious allocation failure causes kernel to panic, it is better to BUG_ON() if effective size for allocation (as passed by caller and/or computed after alignemtn rounding) is zero.

should we just make align to 1 instead of 0 ?

Where - you mean if user passes 0, just make it 1. Nah - it's better to complain and get the call site fixed !

Quoted 1 line

or BUG_ON(!align) instead?

That could be done too but you would also need BUG_ON(!size) - to catch another API abuse.
BUG_ON(!size) however catches both the cases.

Quoted 29 lines
Quoted 29 lines
---
 mm/memblock.c |    2 ++
 1 files changed, 2 insertions(+), 0 deletions(-)
mm/memblock.c +2 -1
index 1bcd9b9..32b36d0 100644
@@ -824,6 +824,8 @@ static phys_addr_t __init memblock_alloc_base_nid(phys_addr_t size,
        /* align @size to avoid excessive fragmentation on reserved array */
        size = round_up(size, align);
 
+       BUG_ON(!size);
+
        found = memblock_find_in_range_node(0, max_addr, size, align, nid);
        if (found && !memblock_reserve(found, size))
                return found;
--

1.7.4.1

Quoted 5 lines
Quoted 5 lines

--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>

#4 lore raw
Quoted 8 lines · Vineet Gupta

Where - you mean if user passes 0, just make it 1. Nah - it's better to complain and get the call site fixed !

Quoted 1 line

or BUG_ON(!align) instead?

That could be done too but you would also need BUG_ON(!size) - to catch another API abuse.
BUG_ON(!size) however catches both the cases.

How about "if (WARN_ON(!align)) align = __alignof__(long long);"? Early BUG_ON()s can be painful to debug depending on setup.

Thanks.

Signature · 7 lines
tejun

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to [email protected].  For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
#5 lore raw
Quoted 10 lines · Tejun Heo
Quoted 7 lines · Vineet Gupta

Where - you mean if user passes 0, just make it 1. Nah - it's better to complain and get the call site fixed !

Quoted 1 line

or BUG_ON(!align) instead?

That could be done too but you would also need BUG_ON(!size) - to catch another API abuse.
BUG_ON(!size) however catches both the cases.

How about "if (WARN_ON(!align)) align = __alignof__(long long);"? Early BUG_ON()s can be painful to debug depending on setup.

Totally agree - been there - seen that :-)
Also for caller passing zero, the panic will force the caller to fix it. I'll respin the patch.

Thx,
-Vineet

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>

#6 lore raw

PATCH v2 1/2 memblock: add assertion for zero allocation alignment

In reply to Tejun Heo

This came to light when calling memblock allocator from arc port (for copying flattended DT). If a "0" alignment is passed, the allocator round_up() call incorrectly rounds up the size to 0.

round_up(num, alignto) => ((num - 1) | (alignto -1)) + 1

While the obvious allocation failure causes kernel to panic, it is better to warn the caller to fix the code.

Tejun suggested that instead of BUG_ON(!align) - which might be ineffective due to pending console init and such, it is better to WARN_ON, and continue the boot with a reasonable default align.

Caller passing @size need not be handled similarly as the subsequent panic will indicate that anyhow.

---
 mm/memblock.c |    3 +++
 1 files changed, 3 insertions(+), 0 deletions(-)
mm/memblock.c +3 -0
index 1bcd9b9..f3804bd 100644
@@ -824,6 +824,9 @@ static phys_addr_t __init memblock_alloc_base_nid(phys_addr_t size,
 	/* align @size to avoid excessive fragmentation on reserved array */
 	size = round_up(size, align);
 
+	if (WARN_ON(!align))
+		align = __alignof__(long long);
+
 	found = memblock_find_in_range_node(0, max_addr, size, align, nid);
 	if (found && !memblock_reserve(found, size))
 		return found;
Signature · 7 lines
1.7.4.1

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to [email protected].  For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
#7 lore raw

Re: [PATCH v2 1/2] memblock: add assertion for zero allocation alignment

Quoted 33 lines · Vineet Gupta

This came to light when calling memblock allocator from arc port (for copying flattended DT). If a "0" alignment is passed, the allocator round_up() call incorrectly rounds up the size to 0.

round_up(num, alignto) => ((num - 1) | (alignto -1)) + 1

While the obvious allocation failure causes kernel to panic, it is better to warn the caller to fix the code.

Tejun suggested that instead of BUG_ON(!align) - which might be ineffective due to pending console init and such, it is better to WARN_ON, and continue the boot with a reasonable default align.

Caller passing @size need not be handled similarly as the subsequent panic will indicate that anyhow.

---
 mm/memblock.c |    3 +++
 1 files changed, 3 insertions(+), 0 deletions(-)
mm/memblock.c +0 -0
index 1bcd9b9..f3804bd 100644

mm/memblock.c static phys_addr_t __init memblock_alloc_base_nid(phys_addr_t size, · line 829

Quoted 6 lines
mm/memblock.c +3 -0
@@ -824,6 +824,9 @@ static phys_addr_t __init memblock_alloc_base_nid(phys_addr_t size,
        /* align @size to avoid excessive fragmentation on reserved array */
        size = round_up(size, align);
 
+       if (WARN_ON(!align))
+               align = __alignof__(long long);
+

the checking should be put before round_up?

Quoted 6 lines
mm/memblock.c +0 -1
        found = memblock_find_in_range_node(0, max_addr, size, align, nid);
        if (found && !memblock_reserve(found, size))
                return found;
--

1.7.4.1

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>

#8 lore raw

Re: [PATCH v2 1/2] memblock: add assertion for zero allocation alignment

Quoted 38 lines · Yinghai Lu
Quoted 36 lines · Vineet Gupta

This came to light when calling memblock allocator from arc port (for copying flattended DT). If a "0" alignment is passed, the allocator round_up() call incorrectly rounds up the size to 0.

round_up(num, alignto) => ((num - 1) | (alignto -1)) + 1

While the obvious allocation failure causes kernel to panic, it is better to warn the caller to fix the code.

Tejun suggested that instead of BUG_ON(!align) - which might be ineffective due to pending console init and such, it is better to WARN_ON, and continue the boot with a reasonable default align.

Caller passing @size need not be handled similarly as the subsequent panic will indicate that anyhow.

---
 mm/memblock.c |    3 +++
 1 files changed, 3 insertions(+), 0 deletions(-)
mm/memblock.c +0 -0
index 1bcd9b9..f3804bd 100644
@@ -824,6 +824,9 @@ static phys_addr_t __init memblock_alloc_base_nid(phys_addr_t size,
        /* align @size to avoid excessive fragmentation on reserved array */
        size = round_up(size, align);
 
Quoted 4 lines

mm/memblock.c static phys_addr_t __init memblock_alloc_base_nid(phys_addr_t size, · line 829

Quoted 3 lines
mm/memblock.c +3 -0
+       if (WARN_ON(!align))
+               align = __alignof__(long long);
+

the checking should be put before round_up?

Oops my bad.

Interestingly however, I did test this exact patch on ARC before sending out - passing @align = 0 to make it hit the WARN. It prints the warning, and uses @size=0, @align=8 for memblock_find_in_range_node() and successfully allocates memory as opposed to failure for @size=0, @align=0 scenario. This is kind of weird.

Anyhow I'll send the updated patch to fix the gotcha !

Thx,
-Vineet

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>

#9 lore raw

PATCH v3 1/2 memblock: add assertion for zero allocation alignment

In reply to Yinghai Lu

This came to light when calling memblock allocator from arc port (for copying flattended DT). If a "0" alignment is passed, the allocator round_up() call incorrectly rounds up the size to 0.

round_up(num, alignto) => ((num - 1) | (alignto -1)) + 1

While the obvious allocation failure causes kernel to panic, it is better to warn the caller to fix the code.

Tejun suggested that instead of BUG_ON(!align) - which might be ineffective due to pending console init and such, it is better to WARN_ON, and continue the boot with a reasonable default align.

Caller passing @size need not be handled similarly as the subsequent panic will indicate that anyhow.

---
 mm/memblock.c |    3 +++
 1 files changed, 3 insertions(+), 0 deletions(-)
mm/memblock.c +3 -0
index 1bcd9b9..8080cf8 100644
@@ -821,6 +821,9 @@ static phys_addr_t __init memblock_alloc_base_nid(phys_addr_t size,
 {
 	phys_addr_t found;
 
+	if (WARN_ON(!align))
+		align = __alignof__(long long);
+
 	/* align @size to avoid excessive fragmentation on reserved array */
 	size = round_up(size, align);
 
Signature · 7 lines
1.7.4.1

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to [email protected].  For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
Acked-by on v3 1/2
#10 lore raw

Re: [PATCH v3 1/2] memblock: add assertion for zero allocation alignment

Quoted 16 lines · Vineet Gupta

This came to light when calling memblock allocator from arc port (for copying flattended DT). If a "0" alignment is passed, the allocator round_up() call incorrectly rounds up the size to 0.

round_up(num, alignto) => ((num - 1) | (alignto -1)) + 1

While the obvious allocation failure causes kernel to panic, it is better to warn the caller to fix the code.

Tejun suggested that instead of BUG_ON(!align) - which might be ineffective due to pending console init and such, it is better to WARN_ON, and continue the boot with a reasonable default align.

Caller passing @size need not be handled similarly as the subsequent panic will indicate that anyhow.

Quoted 8 lines

Thanks.

Signature · 7 lines
tejun

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to [email protected].  For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
#11 lore raw

Re: [PATCH v3 1/2] memblock: add assertion for zero allocation alignment

Hi Andrew,

Quoted 27 lines · Tejun Heo
Quoted 16 lines · Vineet Gupta

This came to light when calling memblock allocator from arc port (for copying flattended DT). If a "0" alignment is passed, the allocator round_up() call incorrectly rounds up the size to 0.

round_up(num, alignto) => ((num - 1) | (alignto -1)) + 1

While the obvious allocation failure causes kernel to panic, it is better to warn the caller to fix the code.

Tejun suggested that instead of BUG_ON(!align) - which might be ineffective due to pending console init and such, it is better to WARN_ON, and continue the boot with a reasonable default align.

Caller passing @size need not be handled similarly as the subsequent panic will indicate that anyhow.

Quoted 8 lines
Quoted 4 lines

Thanks.

I'm hoping this will be routed via the mm tree.

Thx,
-Vineet

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>

#12 lore raw

PATCH 2/2 xtensa: Flat DeviceTree copy not future-safe

In reply to a message not in the archive

flat DT copy code calls bootmem allocator with @align = 0. This is probably OK with legacy allocator which xtensa uses right now, but this will panic right away with memblock allocator

---
 arch/xtensa/kernel/setup.c |    2 +-
 1 files changed, 1 insertions(+), 1 deletions(-)
arch/xtensa/kernel/setup.c +1 -1
index 6dd25ec..c2a526e 100644
@@ -256,7 +256,7 @@ void __init early_init_devtree(void *params)
 static void __init copy_devtree(void)
 {
 	void *alloc = early_init_dt_alloc_memory_arch(
-			be32_to_cpu(initial_boot_params->totalsize), 0);
+			be32_to_cpu(initial_boot_params->totalsize), 8);
 	if (alloc) {
 		memcpy(alloc, initial_boot_params,
 				be32_to_cpu(initial_boot_params->totalsize));
Signature · 1 line
1.7.4.1
maintainer Acked-by on v1 2/2
#13 lore raw

Re: [PATCH 2/2] xtensa: Flat DeviceTree copy not future-safe

Quoted 10 lines · Vineet Gupta

flat DT copy code calls bootmem allocator with @align = 0. This is probably OK with legacy allocator which xtensa uses right now, but this will panic right away with memblock allocator

Signature · 2 lines
Thanks.
-- Max
#14 lore raw

PATCH 2/2 xtensa: Flat DeviceTree copy not future-safe

In reply to Vineet Gupta

Hi Max,

This patch - acked by you then, got lost in the mist of time. Care to take it in via xtensa tree for 3.11

Thx,
-Vineet

-------- Original Message --------
Subject: [PATCH 2/2] xtensa: Flat DeviceTree copy not future-safe
Date: Fri, 22 Feb 2013 00:09:22 +0530
From: Vineet Gupta <[email protected]>
To: Andrew Morton <[email protected]>, Chris Zankel <[email protected]>, Max Filippov <[email protected]>, Marc Gauthier <[email protected]>
CC: Vineet Gupta <[email protected]>, <[email protected]>, <[email protected]>
Newsgroups: gmane.linux.kernel
References: <[email protected]>

flat DT copy code calls bootmem allocator with @align = 0. This is probably OK with legacy allocator which xtensa uses right now, but this will panic right away with memblock allocator

---
 arch/xtensa/kernel/setup.c |    2 +-
 1 files changed, 1 insertions(+), 1 deletions(-)
arch/xtensa/kernel/setup.c +1 -1
index 6dd25ec..c2a526e 100644
@@ -256,7 +256,7 @@ void __init early_init_devtree(void *params)
 static void __init copy_devtree(void)
 {
 	void *alloc = early_init_dt_alloc_memory_arch(
-			be32_to_cpu(initial_boot_params->totalsize), 0);
+			be32_to_cpu(initial_boot_params->totalsize), 8);
 	if (alloc) {
 		memcpy(alloc, initial_boot_params,
 				be32_to_cpu(initial_boot_params->totalsize));
Signature · 1 line
1.7.4.1
maintainer
#15 lore raw

Re: [PATCH 2/2] xtensa: Flat DeviceTree copy not future-safe

Hi Vineet,

Quoted 4 lines · Vineet Gupta

Hi Max,

This patch - acked by you then, got lost in the mist of time. Care to take it in via xtensa tree for 3.11

Oops. Thanks for the reminder. Took it to the xtensa-fixes branch.

Quoted 10 lines

-------- Original Message --------
Subject: [PATCH 2/2] xtensa: Flat DeviceTree copy not future-safe
Date: Fri, 22 Feb 2013 00:09:22 +0530
From: Vineet Gupta <[email protected]>
To: Andrew Morton <[email protected]>, Chris Zankel <[email protected]>, Max Filippov <[email protected]>, Marc Gauthier <[email protected]>
CC: Vineet Gupta <[email protected]>, <[email protected]>, <[email protected]>
Newsgroups: gmane.linux.kernel
References: <[email protected]>

Signature · 2 lines
Thanks.
-- Max