From: Alexander Potapenko <glider@google.com>
To: xkernel.wang@foxmail.com
Cc: Andrew Morton <akpm@linux-foundation.org>,
Andrey Konovalov <andreyknvl@gmail.com>,
Marco Elver <elver@google.com>,
Dmitriy Vyukov <dvyukov@google.com>,
Andrey Ryabinin <ryabinin.a.a@gmail.com>,
kasan-dev <kasan-dev@googlegroups.com>,
Linux Memory Management List <linux-mm@kvack.org>,
LKML <linux-kernel@vger.kernel.org>
Subject: Re: [PATCH] lib/test_meminit: add checks for the allocation functions
Date: Mon, 7 Mar 2022 15:13:14 +0100 [thread overview]
Message-ID: <CAG_fn=W02BVqA_JhCF=Xzs5VkTZm4Caf_rnusT-RPD_r6=0U9Q@mail.gmail.com> (raw)
In-Reply-To: <tencent_D44A49FFB420EDCCBFB9221C8D14DFE12908@qq.com>
[-- Attachment #1: Type: text/plain, Size: 3674 bytes --]
On Fri, Mar 4, 2022 at 10:13 AM <xkernel.wang@foxmail.com> wrote:
> From: Xiaoke Wang <xkernel.wang@foxmail.com>
>
> 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 <xkernel.wang@foxmail.com>
>
Reviewed-by: Alexander Potapenko <glider@google.com>
> ---
> 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. */
> --
>
--
Alexander Potapenko
Software Engineer
Google Germany GmbH
Erika-Mann-Straße, 33
80636 München
Geschäftsführer: Paul Manicle, Liana Sebastian
Registergericht und -nummer: Hamburg, HRB 86891
Sitz der Gesellschaft: Hamburg
Diese E-Mail ist vertraulich. Falls Sie diese fälschlicherweise erhalten
haben sollten, leiten Sie diese bitte nicht an jemand anderes weiter,
löschen Sie alle Kopien und Anhänge davon und lassen Sie mich bitte wissen,
dass die E-Mail an die falsche Person gesendet wurde.
This e-mail is confidential. If you received this communication by mistake,
please don't forward it to anyone else, please erase all copies and
attachments, and please let me know that it has gone to the wrong person.
[-- Attachment #2: Type: text/html, Size: 4859 bytes --]
next prev parent reply other threads:[~2022-03-07 14:13 UTC|newest]
Thread overview: 3+ messages / expand[flat|nested] mbox.gz Atom feed top
2022-03-04 9:12 xkernel.wang
2022-03-07 14:13 ` Alexander Potapenko [this message]
2022-09-26 3:16 ` xkernel.wang
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to='CAG_fn=W02BVqA_JhCF=Xzs5VkTZm4Caf_rnusT-RPD_r6=0U9Q@mail.gmail.com' \
--to=glider@google.com \
--cc=akpm@linux-foundation.org \
--cc=andreyknvl@gmail.com \
--cc=dvyukov@google.com \
--cc=elver@google.com \
--cc=kasan-dev@googlegroups.com \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-mm@kvack.org \
--cc=ryabinin.a.a@gmail.com \
--cc=xkernel.wang@foxmail.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox