提交 ea091fa5 编写于 作者: X Xiaoke Wang 提交者: Andrew Morton

lib/test_meminit: add checks for the allocation functions

alloc_pages(), kmalloc() and vmalloc() are all memory allocation functions
which can return NULL when some internal memory failures happen.  So it is
better to check the return of them to catch the failure in time for better
test them.

Link: https://lkml.kernel.org/r/tencent_D44A49FFB420EDCCBFB9221C8D14DFE12908@qq.comSigned-off-by: NXiaoke Wang <xkernel.wang@foxmail.com>
Reviewed-by: NAlexander Potapenko <glider@google.com>
Cc: Andrey Konovalov <andreyknvl@gmail.com>
Cc: Andrey Ryabinin <ryabinin.a.a@gmail.com>
Cc: Dmitry Vyukov <dvyukov@google.com>
Cc: Marco Elver <elver@google.com>
Signed-off-by: NAndrew Morton <akpm@linux-foundation.org>
上级 ac801e7e
...@@ -67,17 +67,24 @@ static int __init do_alloc_pages_order(int order, int *total_failures) ...@@ -67,17 +67,24 @@ static int __init do_alloc_pages_order(int order, int *total_failures)
size_t size = PAGE_SIZE << order; size_t size = PAGE_SIZE << order;
page = alloc_pages(GFP_KERNEL, order); page = alloc_pages(GFP_KERNEL, order);
if (!page)
goto err;
buf = page_address(page); buf = page_address(page);
fill_with_garbage(buf, size); fill_with_garbage(buf, size);
__free_pages(page, order); __free_pages(page, order);
page = alloc_pages(GFP_KERNEL, order); page = alloc_pages(GFP_KERNEL, order);
if (!page)
goto err;
buf = page_address(page); buf = page_address(page);
if (count_nonzero_bytes(buf, size)) if (count_nonzero_bytes(buf, size))
(*total_failures)++; (*total_failures)++;
fill_with_garbage(buf, size); fill_with_garbage(buf, size);
__free_pages(page, order); __free_pages(page, order);
return 1; return 1;
err:
(*total_failures)++;
return 1;
} }
/* Test the page allocator by calling alloc_pages with different orders. */ /* Test the page allocator by calling alloc_pages with different orders. */
...@@ -100,15 +107,22 @@ static int __init do_kmalloc_size(size_t size, int *total_failures) ...@@ -100,15 +107,22 @@ static int __init do_kmalloc_size(size_t size, int *total_failures)
void *buf; void *buf;
buf = kmalloc(size, GFP_KERNEL); buf = kmalloc(size, GFP_KERNEL);
if (!buf)
goto err;
fill_with_garbage(buf, size); fill_with_garbage(buf, size);
kfree(buf); kfree(buf);
buf = kmalloc(size, GFP_KERNEL); buf = kmalloc(size, GFP_KERNEL);
if (!buf)
goto err;
if (count_nonzero_bytes(buf, size)) if (count_nonzero_bytes(buf, size))
(*total_failures)++; (*total_failures)++;
fill_with_garbage(buf, size); fill_with_garbage(buf, size);
kfree(buf); kfree(buf);
return 1; return 1;
err:
(*total_failures)++;
return 1;
} }
/* Test vmalloc() with given parameters. */ /* Test vmalloc() with given parameters. */
...@@ -117,15 +131,22 @@ static int __init do_vmalloc_size(size_t size, int *total_failures) ...@@ -117,15 +131,22 @@ static int __init do_vmalloc_size(size_t size, int *total_failures)
void *buf; void *buf;
buf = vmalloc(size); buf = vmalloc(size);
if (!buf)
goto err;
fill_with_garbage(buf, size); fill_with_garbage(buf, size);
vfree(buf); vfree(buf);
buf = vmalloc(size); buf = vmalloc(size);
if (!buf)
goto err;
if (count_nonzero_bytes(buf, size)) if (count_nonzero_bytes(buf, size))
(*total_failures)++; (*total_failures)++;
fill_with_garbage(buf, size); fill_with_garbage(buf, size);
vfree(buf); vfree(buf);
return 1; return 1;
err:
(*total_failures)++;
return 1;
} }
/* Test kmalloc()/vmalloc() by allocating objects of different sizes. */ /* Test kmalloc()/vmalloc() by allocating objects of different sizes. */
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册