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 E1663C25B75 for ; Tue, 7 May 2024 07:59:15 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 757D56B008A; Tue, 7 May 2024 03:59:15 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 707416B008C; Tue, 7 May 2024 03:59:15 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 5A8306B0092; Tue, 7 May 2024 03:59:15 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0012.hostedemail.com [216.40.44.12]) by kanga.kvack.org (Postfix) with ESMTP id 3DF946B008A for ; Tue, 7 May 2024 03:59:15 -0400 (EDT) Received: from smtpin22.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id AEC0A12086F for ; Tue, 7 May 2024 07:59:14 +0000 (UTC) X-FDA: 82090849428.22.3825D77 Received: from mail-ej1-f46.google.com (mail-ej1-f46.google.com [209.85.218.46]) by imf02.hostedemail.com (Postfix) with ESMTP id E718D80002 for ; Tue, 7 May 2024 07:59:12 +0000 (UTC) Authentication-Results: imf02.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=RCF2M2dz; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (imf02.hostedemail.com: domain of richard.weiyang@gmail.com designates 209.85.218.46 as permitted sender) smtp.mailfrom=richard.weiyang@gmail.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1715068753; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:content-type: content-transfer-encoding:in-reply-to:in-reply-to: references:references:dkim-signature; bh=qjbF9huOhyjeh3V47TboYkyqTO1+dYj+fAbZDTlbTBU=; b=vd7kzXBokGLLDShr9oMjDxqS3vbL94R/IiqSXknjlnbnsYVChD0ufGpA+rYkamkRv7DS4Z J0kvysrtEXb0t0XwDW1l2ve6A+woUs2vWvHns3EWsMmmpNgE/D3fbiLwOIoZ9SVhKfanX1 S15BNqH+KLYOG5yxLQPjjYOubiqOXt8= ARC-Authentication-Results: i=1; imf02.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=RCF2M2dz; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (imf02.hostedemail.com: domain of richard.weiyang@gmail.com designates 209.85.218.46 as permitted sender) smtp.mailfrom=richard.weiyang@gmail.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1715068753; a=rsa-sha256; cv=none; b=O6Cn8l9ZoCsecJGlFBAuE/uo37epFdBPMbJxWVQCGtLiaClD4dDtuIK+YU5z3S6L/iV/KE vM+ClIQZ5hQwxh9RwCSb0TQ9fgcDOcWtwFPKQfJnotxrMzPCXXz+mpRtFjYLy44utXyFqB Q3L81Ey1V/I8w8C8AQ2NoDKlr4RYhFE= Received: by mail-ej1-f46.google.com with SMTP id a640c23a62f3a-a59b58fe083so411033366b.0 for ; Tue, 07 May 2024 00:59:12 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1715068751; x=1715673551; darn=kvack.org; h=references:in-reply-to:message-id:date:subject:cc:to:from:from:to :cc:subject:date:message-id:reply-to; bh=qjbF9huOhyjeh3V47TboYkyqTO1+dYj+fAbZDTlbTBU=; b=RCF2M2dz2cnFS7GAoM7/YJPKl3PsV1psEwPGyPZC7EoFZB+Tvtj6skWe4zYhTylV41 lcCjGzIIGXisyGebbTMeO5RShu0HiRFHfq65g8HT2HDz/2b7DC+115m3QF22dJadQXnc kq8tbgHHCOCwQPbg4eiE68PfFZxbU2fAVg8pHNd+zUhWmaXVjZhXQKGIU2BsDYl75ytl WdoR3lsJbS/kEoeRBHarU/34ENRSIGw2RkiH4m6kObIjdZXos5PcF7BIpmONzdk3HN04 8nJZYU03E6BL1sHg79Jf5L6jAad2v7V8uiJy+qi5p56lC31vNjyviodhsZ2UjVs4zkRI narA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1715068751; x=1715673551; h=references:in-reply-to:message-id:date:subject:cc:to:from :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=qjbF9huOhyjeh3V47TboYkyqTO1+dYj+fAbZDTlbTBU=; b=oO85/znr1DDd7fqdmtn+fs3Jifpv5m0kIEDs9m7zTNqzmIyWpp6qC/Q29jg9mEo1XO tZ2Sis/M6jN5uYNw6Y9Qu4gRqGHUWFnoSkC9Zl3BJV07cOmpEQhxcJFcBlUfGkJibe8O 4dYbqv931YfC5pmYmjE4yb6qhPTuk4Bx66K8IoG8KitIZFXwcyok74GOxIwBFU/TD9Qv 2fGp5Dvm0wky1OYZkJurIx+70tU+BiV7KxKNrRTB+DmEbBawnLXBB6fpbHL9wz5ye1WV xnINpehSg5MNvGb3HSMwta6HhklsFQD3cgJ0fiV9qvZXnGt/7EPKcjdBBJ4UaTlO01mI EEJA== X-Gm-Message-State: AOJu0YxGSCvQBv7oz76Nn+1YxJqDpYkoV7V/vxDcALAonwggohIkf7mS TTJWRNPaY1j2p5glKgLRtvPdHq0BME5CdAIlweybqWYFbD1zDsV4 X-Google-Smtp-Source: AGHT+IEja25gAtV1OFSjwuaA2tKVGljSfw0SCoQ7U2M9686hYiOTlvIsWnsenX0v6OIZ392FioSM8Q== X-Received: by 2002:a50:c043:0:b0:572:943a:cdf2 with SMTP id u3-20020a50c043000000b00572943acdf2mr7633829edd.1.1715068751479; Tue, 07 May 2024 00:59:11 -0700 (PDT) Received: from localhost ([185.92.221.13]) by smtp.gmail.com with ESMTPSA id ec43-20020a0564020d6b00b00572dea90dabsm4409917edb.63.2024.05.07.00.59.10 (version=TLS1_2 cipher=ECDHE-ECDSA-CHACHA20-POLY1305 bits=256/256); Tue, 07 May 2024 00:59:10 -0700 (PDT) From: Wei Yang To: rppt@kernel.org, akpm@linux-foundation.org Cc: linux-mm@kvack.org, Wei Yang Subject: [Patch v3 2/7] memblock tests: add memblock_reserve_many_may_conflict_check() Date: Tue, 7 May 2024 07:58:28 +0000 Message-Id: <20240507075833.6346-3-richard.weiyang@gmail.com> X-Mailer: git-send-email 2.11.0 In-Reply-To: <20240507075833.6346-1-richard.weiyang@gmail.com> References: <20240507075833.6346-1-richard.weiyang@gmail.com> X-Rspamd-Server: rspam07 X-Rspamd-Queue-Id: E718D80002 X-Stat-Signature: 66it3rfzekybij6fwnsobswmd3mmsjiy X-Rspam-User: X-HE-Tag: 1715068752-450475 X-HE-Meta: U2FsdGVkX1+Q8ccTf5gj5upVxaga25lVvDfnoeDpTWyyYpmOGelvuNzVKKwi35Qir7OZggiBPfhToSnzOsbqtQaJYJiu1Cv2Poc/0n4EsKwpkbsuedRE7oZ3QbMoSk2jpyJoUwKaZ1saNxOGTjxHupd3pOCt+rxlqx2EfxX0suLKCZJzDRCNuKHyTh/QqgkaxlQymT0vwArWlm2fdq9fvKVHoAZWy0os6T8234x+1Pqrw0txsB/lUuZxcsWK/zAB2yWuOjuiMdpxGQr0ZoIgS2shtiAduhbLQ/Amau2y31SUeMZyyJY9qWRhlK0gg2a04i1UsqAt5wv/J1pDv+W4p5MsLjLFZj5UYV5RYkY1TGkSKTX5od5yUg48U3VeH1tUIGRHsdkajSFhI2PjHKNeq8OjR/pf1vnXcF39m6cJMqn28fNhdTvjNqUIhIiDTCXcPIhHK1yAR92Z5A12D/HqXII9l/Fc6cQMH+xKcxmryzIHYWIewNYIucRJMWliA2LYZCY3qSCjCF4x7YANseFUSVAiT0AbC4e7aAmEo6l/rk/Zhb7i7JLXoz8J3REL6iCrAWiin+0cidSPxmHM1IELfVxeIkblcpMudcv+TmNJBk2XVdSDM613rb5nXF8IZef2Phu8w405LMUaV2x1UgDqzUox4V5y3bLSGsl/TpIP6Al14Am7aFJs6pIZdFfIu+VFk36aJ/IdLY69APJajvMecoCUoaVawLHZIfhL3ZWMGDRblgZnJWFon+ChmXpSTvC+ItwMn990oCxTAyFXzVsgDjYuMp1Eb+FbI79myD9vcSYHRJ71vyIW45AwXMNQY2+cicLqM1u12lfVs5rUd/BDFt73lQ1IUXihTAkpxERatD/d48k10ggB4cS4oW6N2sFVGqnVuW3WMeTqDuWVBYOpkRZEYUTqFskPCLZBle/sHkoKxhFBoz0kpcWySyJCKWnPZ0VuLxuz1JH9TCOzP2G wStJsNpl WFZNTN1TnYg74RCMzbse1aEUsZuMvPGmUBzEt6jcSldT+q5KptwxKwAIhCbvU0xVo47nQMHcxwM75s48Zx6mSBCAVnWsgKw1zIUUuYNrVMs/mLgbhU6fWtg9qjJmjUSEVnpMSsAW0cAvwJg6HHlC02vgn3+G9a99NZKzmSFO3by2i90NQWTvak4dqMWBujUJ9psmcD9g7l8MuvZZJ5CH7800oAT0pLNBTeFgRLFv4XpBYWX/tTe6IWh2FeuLCdrzSxLGGXuHboDE5ZjQYLEsVR6RBW3uo5VZ5+8G/RyEsAz+bgClbevk8V35kc2e3f1qVZqWvk3Ve/T1yZS/B5ewrYFfTQ2BLcwuerkEpMfM4X/tomVr472pgkYno60giK4dAU/aCEVWqF9c4fkTQoD3FS0sZNfkNMKV5ETeubLcaWuOYjmcmw/PpLH5k9F76KLeKYFlsP0PgAfZBn1LZEqz2xVcjFWJa3WTacfAmAjpfZ9YiDVTpIOI0dU3uRXIUdNFeNJRcnF9quLXGpu/td43i8w8Keh+YOrFmeRLETMzBAVQDayU= X-Bogosity: Ham, tests=bogofilter, spamicity=0.000082, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: This may trigger the case fixed by commit 48c3b583bbdd ("mm/memblock: fix overlapping allocation when doubling reserved array"). This is done by adding the 129th reserve region into memblock.memory. If memblock_double_array() use this reserve region as new array, it fails. Signed-off-by: Wei Yang --- v3: * rename MEM_ALLOC_SIZE to PHYS_MEM_SIZE * add test description --- tools/testing/memblock/tests/basic_api.c | 151 +++++++++++++++++++++++ tools/testing/memblock/tests/common.c | 4 +- tools/testing/memblock/tests/common.h | 1 + 3 files changed, 154 insertions(+), 2 deletions(-) diff --git a/tools/testing/memblock/tests/basic_api.c b/tools/testing/memblock/tests/basic_api.c index bd3ebbf6b697..fdac82656d15 100644 --- a/tools/testing/memblock/tests/basic_api.c +++ b/tools/testing/memblock/tests/basic_api.c @@ -1088,6 +1088,156 @@ static int memblock_reserve_all_locations_check(void) return 0; } +/* + * A test that trying to reserve the 129th memory block at all locations. + * Expect to trigger memblock_double_array() to double the + * memblock.memory.max, find a new valid memory as reserved.regions. And make + * sure it doesn't conflict with the range we want to reserve. + * + * For example, we have 128 regions in reserved and now want to reserve + * the skipped one. Since reserved is full, memblock_double_array() would find + * an available range in memory for the new array. We intended to put two + * ranges in memory with one is the exact range of the skipped one. Before + * commit 48c3b583bbdd ("mm/memblock: fix overlapping allocation when doubling + * reserved array"), the new array would sits in the skipped range which is a + * conflict. The expected new array should be allocated from memory.regions[0]. + * + * 0 1 + * memory +-------+ +-------+ + * | 32K | | 32K | + * +-------+ ------+-------+-------+-------+ + * |<-32K->|<-32K->|<-32K->| + * + * 0 skipped 127 + * reserved +-------+ ......... +-------+ + * | 32K | . 32K . ... | 32K | + * +-------+-------+-------+ +-------+ + * |<-32K->| + * ^ + * | + * | + * skipped one + */ +/* Keep the gap so these memory region will not be merged. */ +#define MEMORY_BASE_OFFSET(idx, offset) ((offset) + (MEM_SIZE * 2) * (idx)) +static int memblock_reserve_many_may_conflict_check(void) +{ + int i, skip; + void *orig_region; + struct region r = { + .base = SZ_16K, + .size = SZ_16K, + }; + phys_addr_t new_reserved_regions_size; + + /* + * 0 1 129 + * +---+ +---+ +---+ + * |32K| |32K| .. |32K| + * +---+ +---+ +---+ + * + * Pre-allocate the range for 129 memory block + one range for double + * memblock.reserved.regions at idx 0. + */ + dummy_physical_memory_init(); + phys_addr_t memory_base = dummy_physical_memory_base(); + phys_addr_t offset = PAGE_ALIGN(memory_base); + + PREFIX_PUSH(); + + /* Reserve the 129th memory block for all possible positions*/ + for (skip = 1; skip <= INIT_MEMBLOCK_REGIONS + 1; skip++) { + reset_memblock_regions(); + memblock_allow_resize(); + + reset_memblock_attributes(); + /* Add a valid memory region used by double_array(). */ + memblock_add(MEMORY_BASE_OFFSET(0, offset), MEM_SIZE); + /* + * Add a memory region which will be reserved as 129th memory + * region. This is not expected to be used by double_array(). + */ + memblock_add(MEMORY_BASE_OFFSET(skip, offset), MEM_SIZE); + + for (i = 1; i <= INIT_MEMBLOCK_REGIONS + 1; i++) { + if (i == skip) + continue; + + /* Reserve some fakes memory region to fulfill the memblock. */ + memblock_reserve(MEMORY_BASE_OFFSET(i, offset), MEM_SIZE); + + if (i < skip) { + ASSERT_EQ(memblock.reserved.cnt, i); + ASSERT_EQ(memblock.reserved.total_size, i * MEM_SIZE); + } else { + ASSERT_EQ(memblock.reserved.cnt, i - 1); + ASSERT_EQ(memblock.reserved.total_size, (i - 1) * MEM_SIZE); + } + } + + orig_region = memblock.reserved.regions; + + /* This reserve the 129 memory_region, and makes it double array. */ + memblock_reserve(MEMORY_BASE_OFFSET(skip, offset), MEM_SIZE); + + /* + * This is the memory region size used by the doubled reserved.regions, + * and it has been reserved due to it has been used. The size is used to + * calculate the total_size that the memblock.reserved have now. + */ + new_reserved_regions_size = PAGE_ALIGN((INIT_MEMBLOCK_REGIONS * 2) * + sizeof(struct memblock_region)); + /* + * The double_array() will find a free memory region as the new + * reserved.regions, and the used memory region will be reserved, so + * there will be one more region exist in the reserved memblock. And the + * one more reserved region's size is new_reserved_regions_size. + */ + ASSERT_EQ(memblock.reserved.cnt, INIT_MEMBLOCK_REGIONS + 2); + ASSERT_EQ(memblock.reserved.total_size, (INIT_MEMBLOCK_REGIONS + 1) * MEM_SIZE + + new_reserved_regions_size); + ASSERT_EQ(memblock.reserved.max, INIT_MEMBLOCK_REGIONS * 2); + + /* + * The first reserved region is allocated for double array + * with the size of new_reserved_regions_size and the base to be + * MEMORY_BASE_OFFSET(0, offset) + SZ_32K - new_reserved_regions_size + */ + ASSERT_EQ(memblock.reserved.regions[0].base + memblock.reserved.regions[0].size, + MEMORY_BASE_OFFSET(0, offset) + SZ_32K); + ASSERT_EQ(memblock.reserved.regions[0].size, new_reserved_regions_size); + + /* + * Now memblock_double_array() works fine. Let's check after the + * double_array(), the memblock_reserve() still works as normal. + */ + memblock_reserve(r.base, r.size); + ASSERT_EQ(memblock.reserved.regions[0].base, r.base); + ASSERT_EQ(memblock.reserved.regions[0].size, r.size); + + ASSERT_EQ(memblock.reserved.cnt, INIT_MEMBLOCK_REGIONS + 3); + ASSERT_EQ(memblock.reserved.total_size, (INIT_MEMBLOCK_REGIONS + 1) * MEM_SIZE + + new_reserved_regions_size + + r.size); + ASSERT_EQ(memblock.reserved.max, INIT_MEMBLOCK_REGIONS * 2); + + /* + * The current reserved.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.reserved.regions = orig_region; + memblock.reserved.cnt = INIT_MEMBLOCK_RESERVED_REGIONS; + } + + dummy_physical_memory_cleanup(); + + test_pass_pop(); + + return 0; +} + static int memblock_reserve_checks(void) { prefix_reset(); @@ -1104,6 +1254,7 @@ static int memblock_reserve_checks(void) memblock_reserve_near_max_check(); memblock_reserve_many_check(); memblock_reserve_all_locations_check(); + memblock_reserve_many_may_conflict_check(); prefix_pop(); diff --git a/tools/testing/memblock/tests/common.c b/tools/testing/memblock/tests/common.c index c2c569f12178..3250c8e5124b 100644 --- a/tools/testing/memblock/tests/common.c +++ b/tools/testing/memblock/tests/common.c @@ -61,7 +61,7 @@ void reset_memblock_attributes(void) static inline void fill_memblock(void) { - memset(memory_block.base, 1, MEM_SIZE); + memset(memory_block.base, 1, PHYS_MEM_SIZE); } void setup_memblock(void) @@ -103,7 +103,7 @@ void setup_numa_memblock(const unsigned int node_fracs[]) void dummy_physical_memory_init(void) { - memory_block.base = malloc(MEM_SIZE); + memory_block.base = malloc(PHYS_MEM_SIZE); assert(memory_block.base); fill_memblock(); } diff --git a/tools/testing/memblock/tests/common.h b/tools/testing/memblock/tests/common.h index b5ec59aa62d7..2f26405562b0 100644 --- a/tools/testing/memblock/tests/common.h +++ b/tools/testing/memblock/tests/common.h @@ -12,6 +12,7 @@ #include <../selftests/kselftest.h> #define MEM_SIZE SZ_32K +#define PHYS_MEM_SIZE SZ_16M #define NUMA_NODES 8 #define INIT_MEMBLOCK_REGIONS 128 -- 2.34.1