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

From: Vineet Gupta
Date: Thu Feb 21 2013 - 15:49:03 EST


On Friday 22 February 2013 02:01 AM, Yinghai Lu wrote:
> On Thu, Feb 21, 2013 at 12:10 PM, Vineet Gupta
> <Vineet.Gupta1@xxxxxxxxxxxx> wrote:
>> 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.
>>
>> Signed-off-by: Vineet Gupta <vgupta@xxxxxxxxxxxx>
>> Cc: Andrew Morton <akpm@xxxxxxxxxxxxxxxxxxxx>
>> Cc: Tejun Heo <tj@xxxxxxxxxx>
>> Cc: Yinghai Lu <yinghai@xxxxxxxxxx>
>> Cc: Wanpeng Li <liwanp@xxxxxxxxxxxxxxxxxx>
>> Cc: Ingo Molnar <mingo@xxxxxxxxxx>
>> Cc: linux-mm@xxxxxxxxx
>> Cc: linux-kernel@xxxxxxxxxxxxxxx
>> ---
>> mm/memblock.c | 3 +++
>> 1 files changed, 3 insertions(+), 0 deletions(-)
>>
>> diff --git a/mm/memblock.c b/mm/memblock.c
>> index 1bcd9b9..f3804bd 100644
>> --- a/mm/memblock.c
>> +++ b/mm/memblock.c
>> @@ -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?

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 from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/