2013-06-27 09:39:52

by Ralf Baechle

[permalink] [raw]
Subject: Re: [PATCH v2] mm: module_alloc: check if size is 0

Warming up an ancient thread because the discussion seems to have just
stalled at some point and I still have this patch bitrotting in patchwork.

The original thread can be found at:

http://www.linux-mips.org/archives/linux-mips/2012-03/msg00006.html
http://www.linux-mips.org/archives/linux-mips/2012-03/msg00028.html

On Wed, Mar 07, 2012 at 03:09:28PM +0200, Veli-Pekka Peltola wrote:

> After commit de7d2b567d040e3b67fe7121945982f14343213d (mm/vmalloc.c: report
> more vmalloc failures) users will get a warning if vmalloc_node_range() is
> called with size 0. This happens if module's init size equals to 0. This
> patch changes ARM, MIPS and x86 module_alloc() to return NULL before calling
> vmalloc_node_range() that would also return NULL and print a warning.
>
> Signed-off-by: Veli-Pekka Peltola <[email protected]>
> Cc: Russell King <[email protected]>
> Cc: Thomas Gleixner <[email protected]>
> Cc: Ingo Molnar <[email protected]>
> Cc: "H. Peter Anvin" <[email protected]>
> Cc: [email protected]
> ---
> I found this with ARM but after checking out various implementations of
> module_alloc() I thought it would be better to fix all at once.
>
> One way to replicate the warning:
> compile kernel with CONFIG_KALLSYMS=n
> insmod a module without init, I used usb-common.ko

I didn't try to reproduce the issue but the code in question doesn't seem
to have changed so the issue should still persist.

Imho de7d2b567d040e3b67fe7121945982f14343213d [mm/vmalloc.c: report more
vmalloc failures] is overly strict in that it also reports zero-sized
allocations. I consider such allocations stupid but legitimiate and often
better preferrable over having to scatter checks for zero size all over
place. So maybe something like below patch?

Thanks,

Ralf
---

Signed-off-by: Ralf Baechle <[email protected]>

mm/vmalloc.c | 6 +++++-
1 file changed, 5 insertions(+), 1 deletion(-)

diff --git a/mm/vmalloc.c b/mm/vmalloc.c
index d365724..e58ca10 100644
--- a/mm/vmalloc.c
+++ b/mm/vmalloc.c
@@ -1679,7 +1679,10 @@ void *__vmalloc_node_range(unsigned long size, unsigned long align,
unsigned long real_size = size;

size = PAGE_ALIGN(size);
- if (!size || (size >> PAGE_SHIFT) > totalram_pages)
+ if (unlikely(!size))
+ return NULL;
+
+ if ((size >> PAGE_SHIFT) > totalram_pages)
goto fail;

area = __get_vm_area_node(size, align, VM_ALLOC | VM_UNLIST,
@@ -1711,6 +1714,7 @@ fail:
warn_alloc_failed(gfp_mask, 0,
"vmalloc: allocation failure: %lu bytes\n",
real_size);
+
return NULL;
}


2013-06-27 22:23:38

by Andrew Morton

[permalink] [raw]
Subject: Re: [PATCH v2] mm: module_alloc: check if size is 0

On Thu, 27 Jun 2013 11:39:17 +0200 Ralf Baechle <[email protected]> wrote:

> Imho de7d2b567d040e3b67fe7121945982f14343213d [mm/vmalloc.c: report more
> vmalloc failures] is overly strict in that it also reports zero-sized
> allocations. I consider such allocations stupid but legitimiate and often
> better preferrable over having to scatter checks for zero size all over
> place. So maybe something like below patch?
>
> ...
>
> --- a/mm/vmalloc.c
> +++ b/mm/vmalloc.c
> @@ -1679,7 +1679,10 @@ void *__vmalloc_node_range(unsigned long size, unsigned long align,
> unsigned long real_size = size;
>
> size = PAGE_ALIGN(size);
> - if (!size || (size >> PAGE_SHIFT) > totalram_pages)
> + if (unlikely(!size))
> + return NULL;
> +
> + if ((size >> PAGE_SHIFT) > totalram_pages)
> goto fail;
>
> area = __get_vm_area_node(size, align, VM_ALLOC | VM_UNLIST,
> @@ -1711,6 +1714,7 @@ fail:
> warn_alloc_failed(gfp_mask, 0,
> "vmalloc: allocation failure: %lu bytes\n",
> real_size);
> +
> return NULL;
> }

If the caller actually dereferences the returned pointer the kernel
will go oops, which should provide adequate notification of a
programming error ;) But all callers should be checking the return
value. So I worry about the by-far-most-common case where code does

size = some_screwed_up_calculation();
p = vmalloc(size);
if (!p)
return -ENOMEM;

So the mistake gets propagated back to who-knows-where as memory
exhaustion and thereby becomes a lot harder to diagnose.


How many callsites really truly need to be edited to avoid the warning?


Veli-Pekka's original patch would be neater if we were to add a new

void *__vmalloc_node_range_zero_size_ok(<args>)
{
if (size == 0)
return NULL;
return __vmalloc_node_range(<args>);
}

(with a better name than __vmalloc_node_range_zero_size_ok!)

2013-06-27 22:46:31

by Joe Perches

[permalink] [raw]
Subject: Re: [PATCH v2] mm: module_alloc: check if size is 0

On Thu, 2013-06-27 at 15:23 -0700, Andrew Morton wrote:
> On Thu, 27 Jun 2013 11:39:17 +0200 Ralf Baechle <[email protected]> wrote:
[]
> Veli-Pekka's original patch would be neater if we were to add a new
>
> void *__vmalloc_node_range_zero_size_ok(<args>)
> {
> if (size == 0)
> return NULL;

I believe you mean
return ZERO_SIZE_PTR;

2013-07-01 08:53:08

by Rusty Russell

[permalink] [raw]
Subject: Re: [PATCH v2] mm: module_alloc: check if size is 0

Joe Perches <[email protected]> writes:
> On Thu, 2013-06-27 at 15:23 -0700, Andrew Morton wrote:
>> On Thu, 27 Jun 2013 11:39:17 +0200 Ralf Baechle <[email protected]> wrote:
> []
>> Veli-Pekka's original patch would be neater if we were to add a new
>>
>> void *__vmalloc_node_range_zero_size_ok(<args>)
>> {
>> if (size == 0)
>> return NULL;
>
> I believe you mean
> return ZERO_SIZE_PTR;

Yes, this is the Right Fix.

Thanks,
Rusty.