From patchwork Mon Aug 1 06:48:36 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Huang, Shaoqin" X-Patchwork-Id: 12933581 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 EA2E7C00144 for ; Mon, 1 Aug 2022 06:50:35 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 829F18E0002; Mon, 1 Aug 2022 02:50:35 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 7DA068E0001; Mon, 1 Aug 2022 02:50:35 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 6A1928E0002; Mon, 1 Aug 2022 02:50:35 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0011.hostedemail.com [216.40.44.11]) by kanga.kvack.org (Postfix) with ESMTP id 597BE8E0001 for ; Mon, 1 Aug 2022 02:50:35 -0400 (EDT) Received: from smtpin09.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay09.hostedemail.com (Postfix) with ESMTP id 22BF3807C7 for ; Mon, 1 Aug 2022 06:50:35 +0000 (UTC) X-FDA: 79750100430.09.D145929 Received: from mga02.intel.com (mga02.intel.com [134.134.136.20]) by imf26.hostedemail.com (Postfix) with ESMTP id 6D99A1400EE for ; Mon, 1 Aug 2022 06:50:34 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1659336634; x=1690872634; h=from:to:cc:subject:date:message-id:mime-version: content-transfer-encoding; bh=O4xw36pWwNvl8nNGSsxqwNyLAQHYyK0GB+/F0QU27Ks=; b=Y5UwdjCU466xz/B2sQ1wdrCWLx/mpJ8do0pwkv549TqP74sJaZIPHTme l1EaiVzMgkzJbACyO0xExaETD5M6337YaHPmbAOnL3pOEVXQAXGgLd2Zp WG1/NEAVcv1JqXg5uCqp904owp+cTF447PUl00rH1HFz1OUS903ZHSbfU IxgXr/0127rKCnoP1iAhoVxOp0g+lItoeXfpwSjqvWSOMQ2Xhlzz7I2tM 7ZpXNP4fG6crGDd+bKAmLqbceRC0TOsyNl9aPmCEAIa0a77GNq0+cTHOW QWmqsOhgOEWQrVGWTNP4p6vv/U9BclXnYPikAoADf8e/2x5fMPAoQnI/W A==; X-IronPort-AV: E=McAfee;i="6400,9594,10425"; a="275977222" X-IronPort-AV: E=Sophos;i="5.93,206,1654585200"; d="scan'208";a="275977222" Received: from fmsmga001.fm.intel.com ([10.253.24.23]) by orsmga101.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 31 Jul 2022 23:50:32 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.93,206,1654585200"; d="scan'208";a="744161525" Received: from q.bj.intel.com ([10.238.154.102]) by fmsmga001.fm.intel.com with ESMTP; 31 Jul 2022 23:50:30 -0700 From: shaoqin.huang@intel.com To: rppt@kernel.org Cc: Shaoqin Huang , Karolina Drobnik , Rebecca Mckeever , David Hildenbrand , linux-mm@kvack.org, linux-kernel@vger.kernel.org Subject: [PATCH v2] memblock test: Add test to memblock_add() 129th region Date: Mon, 1 Aug 2022 14:48:36 +0800 Message-Id: <20220801064901.980558-1-shaoqin.huang@intel.com> X-Mailer: git-send-email 2.30.2 MIME-Version: 1.0 ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1659336634; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-transfer-encoding:content-transfer-encoding: in-reply-to:references:dkim-signature; bh=2D1tidsxT4t9I0xCtnLNMuHc5X/FYB5yTAk6AiC9c+E=; b=U0k1g5+hn/ZKmd6m06+YNf6IGfOH2HTGyjuAAh2NlRp3LKzgdKORLU8/B21ZCAEAbe5/Ao 3XZgZNb0Ag0RvrZtgK8h2RUXdkFlfNkudhg6thYMDB0/MSBQleZAi/1J2BDGUZfg5kxjcX TnW8TaCq7G1zpb2XT6OX7ehafLQF/YQ= ARC-Authentication-Results: i=1; imf26.hostedemail.com; dkim=none ("invalid DKIM record") header.d=intel.com header.s=Intel header.b=Y5UwdjCU; spf=pass (imf26.hostedemail.com: domain of shaoqin.huang@intel.com designates 134.134.136.20 as permitted sender) smtp.mailfrom=shaoqin.huang@intel.com; dmarc=pass (policy=none) header.from=intel.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1659336634; a=rsa-sha256; cv=none; b=mPy2Ic3So0u+oC8BxnckG5MvY6vCfeKJG01gqX4ruKG/94YoHf5SPOj7ljVdzfWn/jwwNH euzYn7o4ghYMCv1skiHMNGHMjgFBLLHIGYFIbu7G33qfKHGaccivr40BH7yc0ipaPlYwf8 tXQicsx60kIp17/6jx9/D8jCjk8y1jM= X-Rspam-User: X-Stat-Signature: q1iywp6u7h5xbrkw1egngrqzj53f3nmj X-Rspamd-Queue-Id: 6D99A1400EE Authentication-Results: imf26.hostedemail.com; dkim=none ("invalid DKIM record") header.d=intel.com header.s=Intel header.b=Y5UwdjCU; spf=pass (imf26.hostedemail.com: domain of shaoqin.huang@intel.com designates 134.134.136.20 as permitted sender) smtp.mailfrom=shaoqin.huang@intel.com; dmarc=pass (policy=none) header.from=intel.com X-Rspamd-Server: rspam08 X-HE-Tag: 1659336634-225081 X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: From: Shaoqin Huang Add 129th region into the memblock, and this will trigger the memblock_double_array() function, this needs valid memory regions. So using dummy_physical_memory_init() to allocate some valid memory, when memblock_double_array() choose a new memory region from memory.regions, it will always choose a valid memory region if we add all valid memory region, so the memblock_double_array() must success. Another thing should be done is to restore the memory.regions after memblock_double_array(), due to now the memory.regions is pointing to a memory region allocated by dummy_physical_memory_init(). And it will affect the subsequent tests if we don't restore the memory region. So Simply record the origin region, and restore it after the test. Signed-off-by: Shaoqin Huang --- Changelog: ---------- v2: - Use ASSERT_EQ() to replace assert(). - Not to expose memory_block, and add a function get_memory_block_base() to get the memory_block.base. - Add two functions for common usage, and now it has been used by this patch to allocate many valid memory regions and free them at the end. tools/testing/memblock/tests/basic_api.c | 54 ++++++++++++++++++++++++ tools/testing/memblock/tests/common.c | 38 +++++++++++++++-- tools/testing/memblock/tests/common.h | 6 +++ 3 files changed, 95 insertions(+), 3 deletions(-) diff --git a/tools/testing/memblock/tests/basic_api.c b/tools/testing/memblock/tests/basic_api.c index 66f46f261e66..46948d5a975e 100644 --- a/tools/testing/memblock/tests/basic_api.c +++ b/tools/testing/memblock/tests/basic_api.c @@ -326,6 +326,59 @@ static int memblock_add_twice_check(void) return 0; } +static int memblock_add_many_check(void) +{ + void *base[INIT_MEMBLOCK_REGIONS + 1]; + void *orig_region; + struct region r = { + .base = SZ_16K, + .size = MEM_SIZE, + }; + + PREFIX_PUSH(); + + reset_memblock_regions(); + memblock_allow_resize(); + + dummy_physical_memory_many_init(base, INIT_MEMBLOCK_REGIONS); + + orig_region = memblock.memory.regions; + + /* This adds the 129 memory_region, and makes it double array. */ + dummy_physical_memory_init(); + append_memblock(); + base[INIT_MEMBLOCK_REGIONS] = get_memory_block_base(); + + ASSERT_EQ(memblock.memory.cnt, INIT_MEMBLOCK_REGIONS + 1); + ASSERT_EQ(memblock.memory.total_size, (INIT_MEMBLOCK_REGIONS + 1) * MEM_SIZE); + ASSERT_EQ(memblock.memory.max, INIT_MEMBLOCK_REGIONS * 2); + + /* The base is very small, so it should be insert to the first region. */ + memblock_add(r.base, r.size); + ASSERT_EQ(memblock.memory.regions[0].base, r.base); + ASSERT_EQ(memblock.memory.regions[0].size, r.size); + + ASSERT_EQ(memblock.memory.cnt, INIT_MEMBLOCK_REGIONS + 2); + ASSERT_EQ(memblock.memory.total_size, (INIT_MEMBLOCK_REGIONS + 2) * MEM_SIZE); + ASSERT_EQ(memblock.memory.max, INIT_MEMBLOCK_REGIONS * 2); + + /* Free these allocated memory. */ + dummy_physical_memory_many_cleanup(base, INIT_MEMBLOCK_REGIONS + 1); + + /* + * The current memory.regions is occupying a range of memory that + * allocated from dummy_physical_memory_init(). After free the memory, + * we must not use it. So restore the origin memory region to make sure + * the tests can run as normal and not affected by the double array. + */ + memblock.memory.regions = orig_region; + memblock.memory.cnt = INIT_MEMBLOCK_REGIONS; + + test_pass_pop(); + + return 0; +} + static int memblock_add_checks(void) { prefix_reset(); @@ -339,6 +392,7 @@ static int memblock_add_checks(void) memblock_add_overlap_bottom_check(); memblock_add_within_check(); memblock_add_twice_check(); + memblock_add_many_check(); prefix_pop(); diff --git a/tools/testing/memblock/tests/common.c b/tools/testing/memblock/tests/common.c index e43b2676af81..960b3ce07696 100644 --- a/tools/testing/memblock/tests/common.c +++ b/tools/testing/memblock/tests/common.c @@ -5,8 +5,6 @@ #include #include -#define INIT_MEMBLOCK_REGIONS 128 -#define INIT_MEMBLOCK_RESERVED_REGIONS INIT_MEMBLOCK_REGIONS #define PREFIXES_MAX 15 #define DELIM ": " @@ -58,10 +56,20 @@ void reset_memblock_attributes(void) memblock.current_limit = MEMBLOCK_ALLOC_ANYWHERE; } +void *get_memory_block_base(void) +{ + return memory_block.base; +} + +void append_memblock(void) +{ + memblock_add((phys_addr_t)memory_block.base, MEM_SIZE); +} + void setup_memblock(void) { reset_memblock_regions(); - memblock_add((phys_addr_t)memory_block.base, MEM_SIZE); + append_memblock(); } void dummy_physical_memory_init(void) @@ -75,6 +83,30 @@ void dummy_physical_memory_cleanup(void) free(memory_block.base); } +void dummy_physical_memory_many_init(void *base[], int cnt) +{ + int i; + + for (i = 0; i < cnt; i++) { + dummy_physical_memory_init(); + append_memblock(); + base[i] = memory_block.base; + + ASSERT_EQ(memblock.memory.cnt, i + 1); + ASSERT_EQ(memblock.memory.total_size, (i + 1) * MEM_SIZE); + } +} + +void dummy_physical_memory_many_cleanup(void *base[], int cnt) +{ + int i; + + for (i = 0; i < cnt; i++) { + memory_block.base = base[i]; + dummy_physical_memory_cleanup(); + } +} + static void usage(const char *prog) { BUILD_BUG_ON(ARRAY_SIZE(help_opts) != ARRAY_SIZE(long_opts) - 1); diff --git a/tools/testing/memblock/tests/common.h b/tools/testing/memblock/tests/common.h index 3e7f23d341d7..848900aa8db6 100644 --- a/tools/testing/memblock/tests/common.h +++ b/tools/testing/memblock/tests/common.h @@ -11,6 +11,8 @@ #include <../selftests/kselftest.h> #define MEM_SIZE SZ_16K +#define INIT_MEMBLOCK_REGIONS 128 +#define INIT_MEMBLOCK_RESERVED_REGIONS INIT_MEMBLOCK_REGIONS /** * ASSERT_EQ(): @@ -68,8 +70,12 @@ struct region { void reset_memblock_regions(void); void reset_memblock_attributes(void); void setup_memblock(void); +void append_memblock(void); +void *get_memory_block_base(void); void dummy_physical_memory_init(void); void dummy_physical_memory_cleanup(void); +void dummy_physical_memory_many_init(void *base[], int cnt); +void dummy_physical_memory_many_cleanup(void *base[], int cnt); void parse_args(int argc, char **argv); void test_fail(void);