Received: from list by lists.gnu.org with archive (Exim 4.90_1) id 1pMZb5-0002iG-4I for mharc-grub-devel@gnu.org; Mon, 30 Jan 2023 14:13:43 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1pMZaq-0002eR-SN for grub-devel@gnu.org; Mon, 30 Jan 2023 14:13:29 -0500 Received: from mx0a-001b2d01.pphosted.com ([148.163.156.1]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1pMZan-00045A-Mo for grub-devel@gnu.org; Mon, 30 Jan 2023 14:13:28 -0500 Received: from pps.filterd (m0098399.ppops.net [127.0.0.1]) by mx0a-001b2d01.pphosted.com (8.17.1.19/8.17.1.19) with ESMTP id 30UISWxM032253; Mon, 30 Jan 2023 19:13:22 GMT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ibm.com; h=from : to : cc : subject : date : message-id : in-reply-to : references : content-transfer-encoding : mime-version; s=pp1; bh=dt8uPqaYbtF32vXSOMbNldi1fVMSLmg4j14bjINPD84=; b=SGwfCu6fY3vwan8RqHwFBt3eW1n5MLV94BEjuSKUTV0ezjnS/l48bkjFmpWgPLECn/aO qStuA+FzT5RL6Fr/pzHrh913RJBlvH/HROdEisPn5C3NI3eI5RNaFeLyaQmfXsgf3pKI UuIY+Yo8w0kwIJu+R627lr8kVRBMZeX7AUwo80jLb7MRfnnoaQxr7NIUXhzljWazqF65 EriqSazxuRsFB29IDY4cDjFxj2A2w5xuewLTqUWknPLlLOl1wXqmNniRcpHo71dIC7Bo /aqZgXMbWyXc+pDUnukRmhyc7opQJL+5J6rgjZgrdtuDZ3VgtqXTIWQYghND61mDiEVc aw== Received: from ppma02dal.us.ibm.com (a.bd.3ea9.ip4.static.sl-reverse.com [169.62.189.10]) by mx0a-001b2d01.pphosted.com (PPS) with ESMTPS id 3nek738wga-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Mon, 30 Jan 2023 19:13:22 +0000 Received: from pps.filterd (ppma02dal.us.ibm.com [127.0.0.1]) by ppma02dal.us.ibm.com (8.17.1.19/8.17.1.19) with ESMTP id 30UHvxB3019225; Mon, 30 Jan 2023 19:13:21 GMT Received: from smtprelay04.wdc07v.mail.ibm.com ([9.208.129.114]) by ppma02dal.us.ibm.com (PPS) with ESMTPS id 3ncvuqf7qr-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Mon, 30 Jan 2023 19:13:21 +0000 Received: from smtpav03.wdc07v.mail.ibm.com (smtpav03.wdc07v.mail.ibm.com [10.39.53.230]) by smtprelay04.wdc07v.mail.ibm.com (8.14.9/8.14.9/NCO v10.0) with ESMTP id 30UJDJpN49217830 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Mon, 30 Jan 2023 19:13:19 GMT Received: from smtpav03.wdc07v.mail.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id 8790458054; Mon, 30 Jan 2023 19:13:19 +0000 (GMT) Received: from smtpav03.wdc07v.mail.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id B70425805A; Mon, 30 Jan 2023 19:13:18 +0000 (GMT) Received: from sbct-3.pok.ibm.com (unknown [9.47.158.153]) by smtpav03.wdc07v.mail.ibm.com (Postfix) with ESMTP; Mon, 30 Jan 2023 19:13:18 +0000 (GMT) From: Stefan Berger To: grub-devel@gnu.org, dkiper@net-space.pl Cc: development@efficientek.com, diegodo@linux.vnet.ibm.com, dja@axtens.net, sudhakar@linux.ibm.com, nasastry@in.ibm.com, Stefan Berger , Daniel Kiper Subject: [PATCH v3 5/6] Add memtool module with memory allocation stress-test Date: Mon, 30 Jan 2023 14:13:03 -0500 Message-Id: <20230130191304.3518171-6-stefanb@linux.ibm.com> X-Mailer: git-send-email 2.39.0 In-Reply-To: <20230130191304.3518171-1-stefanb@linux.ibm.com> References: <20230130191304.3518171-1-stefanb@linux.ibm.com> X-TM-AS-GCONF: 00 X-Proofpoint-ORIG-GUID: 4sJw9cMeenUTP6mkXgmmc91bS8h3i7wT X-Proofpoint-GUID: 4sJw9cMeenUTP6mkXgmmc91bS8h3i7wT Content-Transfer-Encoding: 8bit X-Proofpoint-UnRewURL: 0 URL was un-rewritten MIME-Version: 1.0 X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.219,Aquarius:18.0.930,Hydra:6.0.562,FMLib:17.11.122.1 definitions=2023-01-30_17,2023-01-30_01,2022-06-22_01 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 spamscore=0 priorityscore=1501 mlxscore=0 mlxlogscore=999 phishscore=0 suspectscore=0 malwarescore=0 clxscore=1015 impostorscore=0 adultscore=0 bulkscore=0 lowpriorityscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2212070000 definitions=main-2301300180 Received-SPF: pass client-ip=148.163.156.1; envelope-from=stefanb@linux.ibm.com; helo=mx0a-001b2d01.pphosted.com X-Spam_score_int: -19 X-Spam_score: -2.0 X-Spam_bar: -- X-Spam_report: (-2.0 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_EF=-0.1, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: grub-devel@gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: The development of GNU GRUB List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Mon, 30 Jan 2023 19:13:29 -0000 From: Daniel Axtens When working on memory, it's nice to be able to test your work. Add a memtest module. When compiled with --enable-mm-debug, it exposes 3 commands: * lsmem - print all allocations and free space in all regions * lsfreemem - print free space in all regions * stress_big_allocs - stress test large allocations: - how much memory can we allocate in one chunk? - how many 1MB chunks can we allocate? - check that gap-filling works with a 1MB aligned 900kB alloc + a 100kB alloc. Signed-off-by: Daniel Axtens Signed-off-by: Stefan Berger Reviewed-by: Daniel Kiper --- configure.ac | 1 + grub-core/Makefile.core.def | 6 ++ grub-core/commands/memtools.c | 160 ++++++++++++++++++++++++++++++++++ grub-core/kern/mm.c | 4 + 4 files changed, 171 insertions(+) create mode 100644 grub-core/commands/memtools.c diff --git a/configure.ac b/configure.ac index 93626b798..ca42ff8f7 100644 --- a/configure.ac +++ b/configure.ac @@ -1520,6 +1520,7 @@ else MM_DEBUG=0 fi AC_SUBST([MM_DEBUG]) +AM_CONDITIONAL([COND_MM_DEBUG], [test x$MM_DEBUG = x1]) AC_ARG_ENABLE([cache-stats], AS_HELP_STRING([--enable-cache-stats], diff --git a/grub-core/Makefile.core.def b/grub-core/Makefile.core.def index 71093a100..6d30fd11f 100644 --- a/grub-core/Makefile.core.def +++ b/grub-core/Makefile.core.def @@ -2548,3 +2548,9 @@ module = { common = commands/i386/wrmsr.c; enable = x86; }; + +module = { + name = memtools; + common = commands/memtools.c; + condition = COND_MM_DEBUG; +}; diff --git a/grub-core/commands/memtools.c b/grub-core/commands/memtools.c new file mode 100644 index 000000000..3471e5941 --- /dev/null +++ b/grub-core/commands/memtools.c @@ -0,0 +1,160 @@ +/* + * GRUB -- GRand Unified Bootloader + * Copyright (C) 2022 Free Software Foundation, Inc. + * Copyright (C) 2022 IBM Corporation + * + * GRUB is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * GRUB is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with GRUB. If not, see . + */ + +#include +#include +#include +#include +#include +#include +#include + +GRUB_MOD_LICENSE ("GPLv3+"); + +#ifdef MM_DEBUG + +static grub_err_t +grub_cmd_lsmem (grub_command_t cmd __attribute__ ((unused)), + int argc __attribute__ ((unused)), + char **args __attribute__ ((unused))) + +{ +#ifndef GRUB_MACHINE_EMU + grub_mm_dump (0); +#endif + + return 0; +} + +static grub_err_t +grub_cmd_lsfreemem (grub_command_t cmd __attribute__ ((unused)), + int argc __attribute__ ((unused)), + char **args __attribute__ ((unused))) + +{ +#ifndef GRUB_MACHINE_EMU + grub_mm_dump_free (); +#endif + + return 0; +} + + +static grub_err_t +grub_cmd_stress_big_allocs (grub_command_t cmd __attribute__ ((unused)), + int argc __attribute__ ((unused)), + char **args __attribute__ ((unused))) +{ + int i, max_mb, blocks_alloced; + void *mem; + void **blocklist; + + grub_printf ("Test 1: increasingly sized allocs to 1GB block\n"); + for (i = 1; i < 1024; i++) + { + grub_printf ("%4d MB . ", i); + mem = grub_malloc (i * 1024 * 1024); + if (mem == NULL) + { + grub_printf ("failed\n"); + break; + } + else + grub_free (mem); + + if (i % 7 == 0) + grub_printf ("\n"); + } + + max_mb = i - 1; + grub_printf ("\nMax sized allocation we did was %d MB\n", max_mb); + + grub_printf ("\nTest 2: 1MB at a time, max 4GB\n"); + blocklist = grub_calloc (4096, sizeof (void *)); + for (i = 0; i < 4096; i++) + { + blocklist[i] = grub_malloc (1024 * 1024); + if (blocklist[i] == NULL) + { + grub_printf ("Ran out of memory at iteration %d\n", i); + break; + } + } + blocks_alloced = i; + for (i = 0; i < blocks_alloced; i++) + grub_free (blocklist[i]); + + grub_printf ("\nTest 3: 1MB aligned 900kB + 100kB\n"); + /* grub_mm_debug=1;*/ + for (i = 0; i < 4096; i += 2) + { + blocklist[i] = grub_memalign (1024 * 1024, 900 * 1024); + if (blocklist[i] == NULL) + { + grub_printf ("Failed big allocation, iteration %d\n", i); + blocks_alloced = i; + break; + } + + blocklist[i + 1] = grub_malloc (100 * 1024); + if (blocklist[i + 1] == NULL) + { + grub_printf ("Failed small allocation, iteration %d\n", i); + blocks_alloced = i + 1; + break; + } + grub_printf ("."); + } + for (i = 0; i < blocks_alloced; i++) + grub_free (blocklist[i]); + + grub_free (blocklist); + +#if defined(__powerpc__) + grub_printf ("\nA reboot may now be required.\n"); +#endif + + grub_errno = GRUB_ERR_NONE; + return GRUB_ERR_NONE; +} + +static grub_command_t cmd_lsmem, cmd_lsfreemem, cmd_sba; + +#endif /* MM_DEBUG */ + +GRUB_MOD_INIT (memtools) +{ +#ifdef MM_DEBUG + cmd_lsmem = grub_register_command ("lsmem", grub_cmd_lsmem, + 0, N_("List free and allocated memory blocks.")); + cmd_lsfreemem = grub_register_command ("lsfreemem", grub_cmd_lsfreemem, + 0, N_("List free memory blocks.")); + cmd_sba = grub_register_command ("stress_big_allocs", grub_cmd_stress_big_allocs, + 0, N_("Stress test large allocations.")); +#endif +} + +GRUB_MOD_FINI (memtools) +{ +#ifdef MM_DEBUG + grub_unregister_command (cmd_lsmem); + grub_unregister_command (cmd_lsfreemem); + grub_unregister_command (cmd_sba); +#endif +} diff --git a/grub-core/kern/mm.c b/grub-core/kern/mm.c index 94eb29108..792ab0a83 100644 --- a/grub-core/kern/mm.c +++ b/grub-core/kern/mm.c @@ -734,6 +734,8 @@ grub_mm_dump_free (void) { grub_mm_header_t p; + grub_printf ("Region %p (size %" PRIuGRUB_SIZE ")\n\n", r, r->size); + /* Follow the free list. */ p = r->first; do @@ -761,6 +763,8 @@ grub_mm_dump (unsigned lineno) { grub_mm_header_t p; + grub_printf ("Region %p (size %" PRIuGRUB_SIZE ")\n\n", r, r->size); + for (p = (grub_mm_header_t) ALIGN_UP ((grub_addr_t) (r + 1), GRUB_MM_ALIGN); (grub_addr_t) p < (grub_addr_t) (r+1) + r->size; -- 2.25.1