From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id CA006C433EF for ; Mon, 7 Mar 2022 14:13:53 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 401278D0002; Mon, 7 Mar 2022 09:13:53 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 3AFFF8D0001; Mon, 7 Mar 2022 09:13:53 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 278008D0002; Mon, 7 Mar 2022 09:13:53 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (relay.a.hostedemail.com [64.99.140.24]) by kanga.kvack.org (Postfix) with ESMTP id 1959A8D0001 for ; Mon, 7 Mar 2022 09:13:53 -0500 (EST) Received: from smtpin07.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay11.hostedemail.com (Postfix) with ESMTP id E24F581518 for ; Mon, 7 Mar 2022 14:13:52 +0000 (UTC) X-FDA: 79217783904.07.9266DEF Received: from mail-qv1-f49.google.com (mail-qv1-f49.google.com [209.85.219.49]) by imf19.hostedemail.com (Postfix) with ESMTP id 4C2C71A0014 for ; Mon, 7 Mar 2022 14:13:52 +0000 (UTC) Received: by mail-qv1-f49.google.com with SMTP id kd28so3631258qvb.2 for ; Mon, 07 Mar 2022 06:13:52 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=gGmnx14YtQ+A7UsJQ2e9RGYf4zsmCH66jJ5hYBKHlHM=; b=goleXkesODFS56wDrTk45RF0KEjmfqH3+UjfQAXrs5EEJubznl1IYbTQN/IB3eJVs/ eswGUe6ixHD7qaSaee8HJfeHLQHyclThNFRcgoF/fheos+lAo0D6Xt09wGI5FxFi0QSA u9Sqs9AUH9oGrILPe2+puPWdAP3dtZchGp36+XqbGNx7cCsg4/VUrtx1dSpMR0GLZTee A9qxpxzEJtd4ZxEMF8upzSHEIKWJDWbG6gJ5c5afgkg+Has7bBXHrZbr0Q+p7VLfdCeE p5ZMtUGpSLyvwSWNyP5OuqLmCsQVF3fl7OQT6g+6FneSpsvC88RztxhNXlvmG8fVJtce d3gA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=gGmnx14YtQ+A7UsJQ2e9RGYf4zsmCH66jJ5hYBKHlHM=; b=GmXcBi1GRB/FQ23g4SbAmwZc69dCO8BvYnMP6ocbIN5+HUq7SzPQC1CRva9BrZSUXy 3F8ef28X5pKXwlCd6hT9+fcl+BSAfgq0Ijmcq1ZchiBOX3EOTIhGfWLGUFPfDhSQF5PS jPzoY9tON4MlCkDAikGqMTa90MrOTSVljU3a/MTEYm0onesGn4md03t0AyOSE9ibiRQA pFhcbYAHtpfpRZ//nhZxkoUMvtCxJ5LtopPWdTbFfRkBaEPXUIomFYDGPe6vTfL7lDkR 338gczcJ9YpQTNScf7Z8YOHqL3q7M7AZ0iaTmAn8tyimhcH0r7Muddu1b+ut1btl7gXl uzag== X-Gm-Message-State: AOAM533VJvjIU8iXNjtNJNq5otXftrfYo2LbEC1QnjhLOdSWnEYj5rrN csKnQikAmZ23XvEQtj6G29bKYdR1UaRdXtuSfVicUg== X-Google-Smtp-Source: ABdhPJwBMYBymMHTkz3hOMskhI6sd7omxQD0+ephTFtPVBDYh/GWuH9qN3yPCczQNe4lkzOEBG/HkqbrCJzRJJd9ycg= X-Received: by 2002:a05:6214:212f:b0:42d:f8f0:fca7 with SMTP id r15-20020a056214212f00b0042df8f0fca7mr8770443qvc.14.1646662431338; Mon, 07 Mar 2022 06:13:51 -0800 (PST) MIME-Version: 1.0 References: In-Reply-To: From: Alexander Potapenko Date: Mon, 7 Mar 2022 15:13:14 +0100 Message-ID: Subject: Re: [PATCH] lib/test_meminit: add checks for the allocation functions To: xkernel.wang@foxmail.com Cc: Andrew Morton , Andrey Konovalov , Marco Elver , Dmitriy Vyukov , Andrey Ryabinin , kasan-dev , Linux Memory Management List , LKML Content-Type: multipart/alternative; boundary="000000000000497d9705d9a179fc" X-Rspamd-Server: rspam06 X-Rspamd-Queue-Id: 4C2C71A0014 X-Stat-Signature: rcswn4f9bfa77y84ha8otrsfibfac43p Authentication-Results: imf19.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b=goleXkes; spf=pass (imf19.hostedemail.com: domain of glider@google.com designates 209.85.219.49 as permitted sender) smtp.mailfrom=glider@google.com; dmarc=pass (policy=reject) header.from=google.com X-Rspam-User: X-HE-Tag: 1646662432-895804 X-Bogosity: Ham, tests=bogofilter, spamicity=0.014091, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: --000000000000497d9705d9a179fc Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable On Fri, Mar 4, 2022 at 10:13 AM wrote: > From: Xiaoke Wang > > 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 > Reviewed-by: Alexander Potapenko > --- > 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 =3D PAGE_SIZE << order; > > page =3D alloc_pages(GFP_KERNEL, order); > + if (!page) > + goto err; > buf =3D page_address(page); > fill_with_garbage(buf, size); > __free_pages(page, order); > > page =3D alloc_pages(GFP_KERNEL, order); > + if (!page) > + goto err; > buf =3D 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 =3D kmalloc(size, GFP_KERNEL); > + if (!buf) > + goto err; > fill_with_garbage(buf, size); > kfree(buf); > > buf =3D 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 =3D vmalloc(size); > + if (!buf) > + goto err; > fill_with_garbage(buf, size); > vfree(buf); > > buf =3D 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. */ > -- > --=20 Alexander Potapenko Software Engineer Google Germany GmbH Erika-Mann-Stra=C3=9Fe, 33 80636 M=C3=BCnchen Gesch=C3=A4ftsf=C3=BChrer: Paul Manicle, Liana Sebastian Registergericht und -nummer: Hamburg, HRB 86891 Sitz der Gesellschaft: Hamburg Diese E-Mail ist vertraulich. Falls Sie diese f=C3=A4lschlicherweise erhalt= en haben sollten, leiten Sie diese bitte nicht an jemand anderes weiter, l=C3=B6schen Sie alle Kopien und Anh=C3=A4nge davon und lassen Sie mich bit= te 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. --000000000000497d9705d9a179fc Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable


=
On Fri, Mar 4, 2022 at 10:13 AM <<= a href=3D"mailto:xkernel.wang@foxmail.com">xkernel.wang@foxmail.com>= wrote:
From: Xi= aoke 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>
Rev= iewed-by: Alexander Potapenko <glid= er@google.com>=C2=A0
---
=C2=A0lib/test_meminit.c | 21 +++++++++++++++++++++
=C2=A01 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)
=C2=A0 =C2=A0 =C2=A0 =C2=A0 size_t size =3D PAGE_SIZE << order;

=C2=A0 =C2=A0 =C2=A0 =C2=A0 page =3D alloc_pages(GFP_KERNEL, order);
+=C2=A0 =C2=A0 =C2=A0 =C2=A0if (!page)
+=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0goto err;
=C2=A0 =C2=A0 =C2=A0 =C2=A0 buf =3D page_address(page);
=C2=A0 =C2=A0 =C2=A0 =C2=A0 fill_with_garbage(buf, size);
=C2=A0 =C2=A0 =C2=A0 =C2=A0 __free_pages(page, order);

=C2=A0 =C2=A0 =C2=A0 =C2=A0 page =3D alloc_pages(GFP_KERNEL, order);
+=C2=A0 =C2=A0 =C2=A0 =C2=A0if (!page)
+=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0goto err;
=C2=A0 =C2=A0 =C2=A0 =C2=A0 buf =3D page_address(page);
=C2=A0 =C2=A0 =C2=A0 =C2=A0 if (count_nonzero_bytes(buf, size))
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 (*total_failures)++= ;
=C2=A0 =C2=A0 =C2=A0 =C2=A0 fill_with_garbage(buf, size);
=C2=A0 =C2=A0 =C2=A0 =C2=A0 __free_pages(page, order);
=C2=A0 =C2=A0 =C2=A0 =C2=A0 return 1;
+err:
+=C2=A0 =C2=A0 =C2=A0 =C2=A0(*total_failures)++;
+=C2=A0 =C2=A0 =C2=A0 =C2=A0return 1;
=C2=A0}

=C2=A0/* Test the page allocator by calling alloc_pages with different orde= rs. */
@@ -100,15 +107,22 @@ static int __init do_kmalloc_size(size_t size, int *t= otal_failures)
=C2=A0 =C2=A0 =C2=A0 =C2=A0 void *buf;

=C2=A0 =C2=A0 =C2=A0 =C2=A0 buf =3D kmalloc(size, GFP_KERNEL);
+=C2=A0 =C2=A0 =C2=A0 =C2=A0if (!buf)
+=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0goto err;
=C2=A0 =C2=A0 =C2=A0 =C2=A0 fill_with_garbage(buf, size);
=C2=A0 =C2=A0 =C2=A0 =C2=A0 kfree(buf);

=C2=A0 =C2=A0 =C2=A0 =C2=A0 buf =3D kmalloc(size, GFP_KERNEL);
+=C2=A0 =C2=A0 =C2=A0 =C2=A0if (!buf)
+=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0goto err;
=C2=A0 =C2=A0 =C2=A0 =C2=A0 if (count_nonzero_bytes(buf, size))
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 (*total_failures)++= ;
=C2=A0 =C2=A0 =C2=A0 =C2=A0 fill_with_garbage(buf, size);
=C2=A0 =C2=A0 =C2=A0 =C2=A0 kfree(buf);
=C2=A0 =C2=A0 =C2=A0 =C2=A0 return 1;
+err:
+=C2=A0 =C2=A0 =C2=A0 =C2=A0(*total_failures)++;
+=C2=A0 =C2=A0 =C2=A0 =C2=A0return 1;
=C2=A0}

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

=C2=A0 =C2=A0 =C2=A0 =C2=A0 buf =3D vmalloc(size);
+=C2=A0 =C2=A0 =C2=A0 =C2=A0if (!buf)
+=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0goto err;
=C2=A0 =C2=A0 =C2=A0 =C2=A0 fill_with_garbage(buf, size);
=C2=A0 =C2=A0 =C2=A0 =C2=A0 vfree(buf);

=C2=A0 =C2=A0 =C2=A0 =C2=A0 buf =3D vmalloc(size);
+=C2=A0 =C2=A0 =C2=A0 =C2=A0if (!buf)
+=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0goto err;
=C2=A0 =C2=A0 =C2=A0 =C2=A0 if (count_nonzero_bytes(buf, size))
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 (*total_failures)++= ;
=C2=A0 =C2=A0 =C2=A0 =C2=A0 fill_with_garbage(buf, size);
=C2=A0 =C2=A0 =C2=A0 =C2=A0 vfree(buf);
=C2=A0 =C2=A0 =C2=A0 =C2=A0 return 1;
+err:
+=C2=A0 =C2=A0 =C2=A0 =C2=A0(*total_failures)++;
+=C2=A0 =C2=A0 =C2=A0 =C2=A0return 1;
=C2=A0}

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


--
Alexander Potapenko
Software= Engineer

Google Germany GmbH
Erika-Mann-Stra=C3=9Fe, 33
80636= M=C3=BCnchen

Gesch=C3=A4ftsf=C3=BChrer: Paul Manicle, Liana Sebasti= an
Registergericht und -nummer: Hamburg, HRB 86891
Sitz der Gesellsch= aft: Hamburg

Diese E-Mail ist vertraulich. Falls Sie diese f=C3=A4ls= chlicherweise erhalten haben sollten, leiten Sie diese bitte nicht an jeman= d anderes weiter, l=C3=B6schen Sie alle Kopien und Anh=C3=A4nge davon und l= assen Sie mich bitte wissen, dass die E-Mail an die falsche Person gesendet= wurde.

=C2=A0 =C2=A0 =C2=A0

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 kn= ow that it has gone to the wrong person.
--000000000000497d9705d9a179fc--