2022-03-04 13:03:52

by Xiaoke Wang

[permalink] [raw]
Subject: [PATCH] lib/test_meminit: add checks for the allocation functions

From: Xiaoke Wang <[email protected]>

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.

Signed-off-by: Xiaoke Wang <[email protected]>
---
lib/test_meminit.c | 21 +++++++++++++++++++++
1 file changed, 21 insertions(+)

diff --git a/lib/test_meminit.c b/lib/test_meminit.c
index e4f706a..2f4c4bc 100644
--- a/lib/test_meminit.c
+++ b/lib/test_meminit.c
@@ -67,17 +67,24 @@ static int __init do_alloc_pages_order(int order, int *total_failures)
size_t size = PAGE_SIZE << order;

page = alloc_pages(GFP_KERNEL, order);
+ if (!page)
+ goto err;
buf = page_address(page);
fill_with_garbage(buf, size);
__free_pages(page, order);

page = alloc_pages(GFP_KERNEL, order);
+ if (!page)
+ goto err;
buf = page_address(page);
if (count_nonzero_bytes(buf, size))
(*total_failures)++;
fill_with_garbage(buf, size);
__free_pages(page, order);
return 1;
+err:
+ (*total_failures)++;
+ return 1;
}

/* 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)
void *buf;

buf = kmalloc(size, GFP_KERNEL);
+ if (!buf)
+ goto err;
fill_with_garbage(buf, size);
kfree(buf);

buf = kmalloc(size, GFP_KERNEL);
+ if (!buf)
+ goto err;
if (count_nonzero_bytes(buf, size))
(*total_failures)++;
fill_with_garbage(buf, size);
kfree(buf);
return 1;
+err:
+ (*total_failures)++;
+ return 1;
}

/* Test vmalloc() with given parameters. */
@@ -117,15 +131,22 @@ static int __init do_vmalloc_size(size_t size, int *total_failures)
void *buf;

buf = vmalloc(size);
+ if (!buf)
+ goto err;
fill_with_garbage(buf, size);
vfree(buf);

buf = vmalloc(size);
+ if (!buf)
+ goto err;
if (count_nonzero_bytes(buf, size))
(*total_failures)++;
fill_with_garbage(buf, size);
vfree(buf);
return 1;
+err:
+ (*total_failures)++;
+ return 1;
}

/* Test kmalloc()/vmalloc() by allocating objects of different sizes. */
--


2022-09-26 04:11:58

by Xiaoke Wang

[permalink] [raw]
Subject: Re: [PATCH] lib/test_meminit: add checks for the allocation functions

Hi Andrew,

This patch seems to have been forgotten.

Regards,
Xiaoke Wang

On Friday, March 4, 2022 5:12 PM, <[email protected]> wrote:
> From: Xiaoke Wang <[email protected]>
>
> 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.
>
> Signed-off-by: Xiaoke Wang <[email protected]>
> ---
> lib/test_meminit.c | 21 +++++++++++++++++++++
> 1 file changed, 21 insertions(+)
>
> diff --git a/lib/test_meminit.c b/lib/test_meminit.c
> index e4f706a..2f4c4bc 100644
> --- a/lib/test_meminit.c
> +++ b/lib/test_meminit.c
> @@ -67,17 +67,24 @@ static int __init do_alloc_pages_order(int order, int
> *total_failures)
> size_t size = PAGE_SIZE << order;
>
> page = alloc_pages(GFP_KERNEL, order);
> + if (!page)
> + goto err;
> buf = page_address(page);
> fill_with_garbage(buf, size);
> __free_pages(page, order);
>
> page = alloc_pages(GFP_KERNEL, order);
> + if (!page)
> + goto err;
> buf = page_address(page);
> if (count_nonzero_bytes(buf, size))
> (*total_failures)++;
> fill_with_garbage(buf, size);
> __free_pages(page, order);
> return 1;
> +err:
> + (*total_failures)++;
> + return 1;
> }
>
> /* 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)
> void *buf;
>
> buf = kmalloc(size, GFP_KERNEL);
> + if (!buf)
> + goto err;
> fill_with_garbage(buf, size);
> kfree(buf);
>
> buf = kmalloc(size, GFP_KERNEL);
> + if (!buf)
> + goto err;
> if (count_nonzero_bytes(buf, size))
> (*total_failures)++;
> fill_with_garbage(buf, size);
> kfree(buf);
> return 1;
> +err:
> + (*total_failures)++;
> + return 1;
> }
>
> /* Test vmalloc() with given parameters. */
> @@ -117,15 +131,22 @@ static int __init do_vmalloc_size(size_t size, int
> *total_failures)
> void *buf;
>
> buf = vmalloc(size);
> + if (!buf)
> + goto err;
> fill_with_garbage(buf, size);
> vfree(buf);
>
> buf = vmalloc(size);
> + if (!buf)
> + goto err;
> if (count_nonzero_bytes(buf, size))
> (*total_failures)++;
> fill_with_garbage(buf, size);
> vfree(buf);
> return 1;
> +err:
> + (*total_failures)++;
> + return 1;
> }
>
> /* Test kmalloc()/vmalloc() by allocating objects of different sizes. */
> --