From nobody Wed Apr 8 10:33:11 2026 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id BEDC3C433F5 for ; Sun, 2 Oct 2022 23:42:06 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229725AbiJBXmE (ORCPT ); Sun, 2 Oct 2022 19:42:04 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:59102 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229547AbiJBXmB (ORCPT ); Sun, 2 Oct 2022 19:42:01 -0400 Received: from mail-pg1-x541.google.com (mail-pg1-x541.google.com [IPv6:2607:f8b0:4864:20::541]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 9DDC632A9D for ; Sun, 2 Oct 2022 16:41:59 -0700 (PDT) Received: by mail-pg1-x541.google.com with SMTP id j71so2251824pge.2 for ; Sun, 02 Oct 2022 16:41:59 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date; bh=B03Bi+65NaX8/Pr/N9BYK3Eqgzig4DJIQAoxxtHUJhE=; b=MzeeeoAjel/z3fSRxYPQJB1XN6ym6wzEDPQA36mh4xIYU42UJi/thQrY93CpGwEkIb SckgHMqnhZtW5X+rVD2JRQ28EXZVle+PI8q7oQWoPkP8cbO8kPf15lXHA2nFuY3GV4aE FjxF3R7T4wu8z7HjxEgBl1fKn8Z4dxX7ft31zFc+RGV6dm+e6PNv65tnLqCycq4JtAZK LLqVxX3i1vq4YKrdR0q2XhT19TM5m2O24c1DYQLSRE1W6WeqBL2z0AG0sK/Gm+X9qFxp wwENxfF74r+l/pOGf+5z8ZuI6FwPBCSSL/G/QZvFbNeTxvRU5x2NjcXz/JbwFrWp763n zhiA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date; bh=B03Bi+65NaX8/Pr/N9BYK3Eqgzig4DJIQAoxxtHUJhE=; b=0Hp5oZ/Ph3YwjdX2hAnEoiBXS3f9cDTa8wHG5MgCf8tJnbwuMh5/yrM9Z9T1FcgS7l YwP3xXrr4pfUkGtXsTUKeI0fXQnvaFLR2STLhjpQynEdostliXygiKKZyCG5VSbDW02t 9NoNWB+jg0BScyGOTT7/Yj4aneL60qgSJT27P3EWgTf5eDhfuC6dJMvRsMIpHE3OV+ML +o9wLCb1kzbLjNlxF0MK5YO1KrIVHieyf8rL9AGAHHdlln1ZXjM4l60H0hah6FeXsWlV KfjkoQtOKujEqrdpySKt/oGWZjHCBs9My76I24KWYhA6N2shBi2DaHeqZeEyVLGPrz+z wI1g== X-Gm-Message-State: ACrzQf3FEab2VnHbvQK/ClkPkomMu12vAo7xDd0ZLA9hw1kO5iO190B9 AB/w9gS+byeAYS90kfjzcfY= X-Google-Smtp-Source: AMsMyM5yTi1+nTF5kec8wD3wo9roWUWWlcMjNQwFlJphUlzkrfHq0+BJGK+MbAL1NmoRke7VYwwMIw== X-Received: by 2002:a63:e709:0:b0:438:98e8:d1c with SMTP id b9-20020a63e709000000b0043898e80d1cmr16739764pgi.403.1664754118938; Sun, 02 Oct 2022 16:41:58 -0700 (PDT) Received: from sophie ([89.46.114.181]) by smtp.gmail.com with ESMTPSA id 186-20020a6219c3000000b0056082e13cacsm2332066pfz.138.2022.10.02.16.41.58 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 02 Oct 2022 16:41:58 -0700 (PDT) From: Rebecca Mckeever To: Mike Rapoport , linux-mm@kvack.org, linux-kernel@vger.kernel.org Cc: David Hildenbrand , Rebecca Mckeever Subject: [PATCH v2 1/5] memblock tests: add range tests for memblock_alloc_exact_nid_raw Date: Sun, 2 Oct 2022 18:41:34 -0500 Message-Id: X-Mailer: git-send-email 2.25.1 In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" Add tests for memblock_alloc_exact_nid_raw() that are very similar to the range tests for memblock_alloc_try_nid_raw(). Signed-off-by: Rebecca Mckeever --- tools/testing/memblock/Makefile | 2 +- tools/testing/memblock/main.c | 2 + .../memblock/tests/alloc_exact_nid_api.c | 1208 +++++++++++++++++ .../memblock/tests/alloc_exact_nid_api.h | 9 + 4 files changed, 1220 insertions(+), 1 deletion(-) create mode 100644 tools/testing/memblock/tests/alloc_exact_nid_api.c create mode 100644 tools/testing/memblock/tests/alloc_exact_nid_api.h diff --git a/tools/testing/memblock/Makefile b/tools/testing/memblock/Makef= ile index 246f7ac8489b..2310ac4d080e 100644 --- a/tools/testing/memblock/Makefile +++ b/tools/testing/memblock/Makefile @@ -7,7 +7,7 @@ CFLAGS +=3D -I. -I../../include -Wall -O2 -fsanitize=3Daddr= ess \ LDFLAGS +=3D -fsanitize=3Daddress -fsanitize=3Dundefined TARGETS =3D main TEST_OFILES =3D tests/alloc_nid_api.o tests/alloc_helpers_api.o tests/allo= c_api.o \ - tests/basic_api.o tests/common.o + tests/basic_api.o tests/common.o tests/alloc_exact_nid_api.o DEP_OFILES =3D memblock.o lib/slab.o mmzone.o slab.o OFILES =3D main.o $(DEP_OFILES) $(TEST_OFILES) EXTR_SRC =3D ../../../mm/memblock.c diff --git a/tools/testing/memblock/main.c b/tools/testing/memblock/main.c index 4ca1024342b1..278f9dec5008 100644 --- a/tools/testing/memblock/main.c +++ b/tools/testing/memblock/main.c @@ -3,6 +3,7 @@ #include "tests/alloc_api.h" #include "tests/alloc_helpers_api.h" #include "tests/alloc_nid_api.h" +#include "tests/alloc_exact_nid_api.h" #include "tests/common.h" =20 int main(int argc, char **argv) @@ -12,6 +13,7 @@ int main(int argc, char **argv) memblock_alloc_checks(); memblock_alloc_helpers_checks(); memblock_alloc_nid_checks(); + memblock_alloc_exact_nid_checks(); =20 return 0; } diff --git a/tools/testing/memblock/tests/alloc_exact_nid_api.c b/tools/tes= ting/memblock/tests/alloc_exact_nid_api.c new file mode 100644 index 000000000000..452aba1f19db --- /dev/null +++ b/tools/testing/memblock/tests/alloc_exact_nid_api.c @@ -0,0 +1,1208 @@ +// SPDX-License-Identifier: GPL-2.0-or-later +#include "alloc_exact_nid_api.h" + +#define FUNC_NAME "memblock_alloc_exact_nid_raw" + +/* + * A simple test that tries to allocate a memory region within min_addr and + * max_addr range: + * + * + + + * | + +-----------+ | + * | | | rgn | | + * +----+-------+-----------+------+ + * ^ ^ + * | | + * min_addr max_addr + * + * Expect to allocate a region that ends at max_addr. + */ +static int alloc_exact_nid_top_down_simple_check(void) +{ + struct memblock_region *rgn =3D &memblock.reserved.regions[0]; + void *allocated_ptr =3D NULL; + phys_addr_t size =3D SZ_128; + phys_addr_t min_addr; + phys_addr_t max_addr; + phys_addr_t rgn_end; + + PREFIX_PUSH(); + setup_memblock(); + + min_addr =3D memblock_start_of_DRAM() + SMP_CACHE_BYTES * 2; + max_addr =3D min_addr + SZ_512; + + allocated_ptr =3D memblock_alloc_exact_nid_raw(size, SMP_CACHE_BYTES, + min_addr, max_addr, + NUMA_NO_NODE); + rgn_end =3D rgn->base + rgn->size; + + ASSERT_NE(allocated_ptr, NULL); + ASSERT_MEM_NE(allocated_ptr, 0, size); + + ASSERT_EQ(rgn->size, size); + ASSERT_EQ(rgn->base, max_addr - size); + ASSERT_EQ(rgn_end, max_addr); + + ASSERT_EQ(memblock.reserved.cnt, 1); + ASSERT_EQ(memblock.reserved.total_size, size); + + test_pass_pop(); + + return 0; +} + +/* + * A simple test that tries to allocate a memory region within min_addr and + * max_addr range, where the end address is misaligned: + * + * + + + + * | + +---------+ + | + * | | | rgn | | | + * +------+-------+---------+--+----+ + * ^ ^ ^ + * | | | + * min_add | max_addr + * | + * Aligned address + * boundary + * + * Expect to allocate an aligned region that ends before max_addr. + */ +static int alloc_exact_nid_top_down_end_misaligned_check(void) +{ + struct memblock_region *rgn =3D &memblock.reserved.regions[0]; + void *allocated_ptr =3D NULL; + phys_addr_t size =3D SZ_128; + phys_addr_t misalign =3D SZ_2; + phys_addr_t min_addr; + phys_addr_t max_addr; + phys_addr_t rgn_end; + + PREFIX_PUSH(); + setup_memblock(); + + min_addr =3D memblock_start_of_DRAM() + SMP_CACHE_BYTES * 2; + max_addr =3D min_addr + SZ_512 + misalign; + + allocated_ptr =3D memblock_alloc_exact_nid_raw(size, SMP_CACHE_BYTES, + min_addr, max_addr, + NUMA_NO_NODE); + rgn_end =3D rgn->base + rgn->size; + + ASSERT_NE(allocated_ptr, NULL); + ASSERT_MEM_NE(allocated_ptr, 0, size); + + ASSERT_EQ(rgn->size, size); + ASSERT_EQ(rgn->base, max_addr - size - misalign); + ASSERT_LT(rgn_end, max_addr); + + ASSERT_EQ(memblock.reserved.cnt, 1); + ASSERT_EQ(memblock.reserved.total_size, size); + + test_pass_pop(); + + return 0; +} + +/* + * A simple test that tries to allocate a memory region, which spans over = the + * min_addr and max_addr range: + * + * + + + * | +---------------+ | + * | | rgn | | + * +------+---------------+-------+ + * ^ ^ + * | | + * min_addr max_addr + * + * Expect to allocate a region that starts at min_addr and ends at + * max_addr, given that min_addr is aligned. + */ +static int alloc_exact_nid_exact_address_generic_check(void) +{ + struct memblock_region *rgn =3D &memblock.reserved.regions[0]; + void *allocated_ptr =3D NULL; + phys_addr_t size =3D SZ_1K; + phys_addr_t min_addr; + phys_addr_t max_addr; + phys_addr_t rgn_end; + + PREFIX_PUSH(); + setup_memblock(); + + min_addr =3D memblock_start_of_DRAM() + SMP_CACHE_BYTES; + max_addr =3D min_addr + size; + + allocated_ptr =3D memblock_alloc_exact_nid_raw(size, SMP_CACHE_BYTES, + min_addr, max_addr, + NUMA_NO_NODE); + rgn_end =3D rgn->base + rgn->size; + + ASSERT_NE(allocated_ptr, NULL); + ASSERT_MEM_NE(allocated_ptr, 0, size); + + ASSERT_EQ(rgn->size, size); + ASSERT_EQ(rgn->base, min_addr); + ASSERT_EQ(rgn_end, max_addr); + + ASSERT_EQ(memblock.reserved.cnt, 1); + ASSERT_EQ(memblock.reserved.total_size, size); + + test_pass_pop(); + + return 0; +} + +/* + * A test that tries to allocate a memory region, which can't fit into + * min_addr and max_addr range: + * + * + + + + * | +----------+-----+ | + * | | rgn + | | + * +--------+----------+-----+----+ + * ^ ^ ^ + * | | | + * Aligned | max_addr + * address | + * boundary min_add + * + * Expect to drop the lower limit and allocate a memory region which + * ends at max_addr (if the address is aligned). + */ +static int alloc_exact_nid_top_down_narrow_range_check(void) +{ + struct memblock_region *rgn =3D &memblock.reserved.regions[0]; + void *allocated_ptr =3D NULL; + phys_addr_t size =3D SZ_256; + phys_addr_t min_addr; + phys_addr_t max_addr; + + PREFIX_PUSH(); + setup_memblock(); + + min_addr =3D memblock_start_of_DRAM() + SZ_512; + max_addr =3D min_addr + SMP_CACHE_BYTES; + + allocated_ptr =3D memblock_alloc_exact_nid_raw(size, SMP_CACHE_BYTES, + min_addr, max_addr, + NUMA_NO_NODE); + + ASSERT_NE(allocated_ptr, NULL); + ASSERT_MEM_NE(allocated_ptr, 0, size); + + ASSERT_EQ(rgn->size, size); + ASSERT_EQ(rgn->base, max_addr - size); + + ASSERT_EQ(memblock.reserved.cnt, 1); + ASSERT_EQ(memblock.reserved.total_size, size); + + test_pass_pop(); + + return 0; +} + +/* + * A test that tries to allocate a memory region, which can't fit into + * min_addr and max_addr range, with the latter being too close to the beg= inning + * of the available memory: + * + * +-------------+ + * | new | + * +-------------+ + * + + + * | + | + * | | | + * +-------+--------------+ + * ^ ^ + * | | + * | max_addr + * | + * min_addr + * + * Expect no allocation to happen. + */ +static int alloc_exact_nid_low_max_generic_check(void) +{ + void *allocated_ptr =3D NULL; + phys_addr_t size =3D SZ_1K; + phys_addr_t min_addr; + phys_addr_t max_addr; + + PREFIX_PUSH(); + setup_memblock(); + + min_addr =3D memblock_start_of_DRAM(); + max_addr =3D min_addr + SMP_CACHE_BYTES; + + allocated_ptr =3D memblock_alloc_exact_nid_raw(size, SMP_CACHE_BYTES, + min_addr, max_addr, + NUMA_NO_NODE); + + ASSERT_EQ(allocated_ptr, NULL); + + test_pass_pop(); + + return 0; +} + +/* + * A test that tries to allocate a memory region within min_addr min_addr = range, + * with min_addr being so close that it's next to an allocated region: + * + * + + + * | +--------+---------------| + * | | r1 | rgn | + * +-------+--------+---------------+ + * ^ ^ + * | | + * min_addr max_addr + * + * Expect a merge of both regions. Only the region size gets updated. + */ +static int alloc_exact_nid_min_reserved_generic_check(void) +{ + struct memblock_region *rgn =3D &memblock.reserved.regions[0]; + void *allocated_ptr =3D NULL; + phys_addr_t r1_size =3D SZ_128; + phys_addr_t r2_size =3D SZ_64; + phys_addr_t total_size =3D r1_size + r2_size; + phys_addr_t min_addr; + phys_addr_t max_addr; + phys_addr_t reserved_base; + + PREFIX_PUSH(); + setup_memblock(); + + max_addr =3D memblock_end_of_DRAM(); + min_addr =3D max_addr - r2_size; + reserved_base =3D min_addr - r1_size; + + memblock_reserve(reserved_base, r1_size); + + allocated_ptr =3D memblock_alloc_exact_nid_raw(r2_size, SMP_CACHE_BYTES, + min_addr, max_addr, + NUMA_NO_NODE); + + ASSERT_NE(allocated_ptr, NULL); + ASSERT_MEM_NE(allocated_ptr, 0, r2_size); + + ASSERT_EQ(rgn->size, total_size); + ASSERT_EQ(rgn->base, reserved_base); + + ASSERT_EQ(memblock.reserved.cnt, 1); + ASSERT_EQ(memblock.reserved.total_size, total_size); + + test_pass_pop(); + + return 0; +} + +/* + * A test that tries to allocate a memory region within min_addr and max_a= ddr, + * with max_addr being so close that it's next to an allocated region: + * + * + + + * | +-------------+--------| + * | | rgn | r1 | + * +----------+-------------+--------+ + * ^ ^ + * | | + * min_addr max_addr + * + * Expect a merge of regions. Only the region size gets updated. + */ +static int alloc_exact_nid_max_reserved_generic_check(void) +{ + struct memblock_region *rgn =3D &memblock.reserved.regions[0]; + void *allocated_ptr =3D NULL; + phys_addr_t r1_size =3D SZ_64; + phys_addr_t r2_size =3D SZ_128; + phys_addr_t total_size =3D r1_size + r2_size; + phys_addr_t min_addr; + phys_addr_t max_addr; + + PREFIX_PUSH(); + setup_memblock(); + + max_addr =3D memblock_end_of_DRAM() - r1_size; + min_addr =3D max_addr - r2_size; + + memblock_reserve(max_addr, r1_size); + + allocated_ptr =3D memblock_alloc_exact_nid_raw(r2_size, SMP_CACHE_BYTES, + min_addr, max_addr, + NUMA_NO_NODE); + + ASSERT_NE(allocated_ptr, NULL); + ASSERT_MEM_NE(allocated_ptr, 0, r2_size); + + ASSERT_EQ(rgn->size, total_size); + ASSERT_EQ(rgn->base, min_addr); + + ASSERT_EQ(memblock.reserved.cnt, 1); + ASSERT_EQ(memblock.reserved.total_size, total_size); + + test_pass_pop(); + + return 0; +} + +/* + * A test that tries to allocate memory within min_addr and max_add range,= when + * there are two reserved regions at the borders, with a gap big enough to= fit + * a new region: + * + * + + + * | +--------+ +-------+------+ | + * | | r2 | | rgn | r1 | | + * +----+--------+---+-------+------+--+ + * ^ ^ + * | | + * min_addr max_addr + * + * Expect to merge the new region with r1. The second region does not get + * updated. The total size field gets updated. + */ + +static int alloc_exact_nid_top_down_reserved_with_space_check(void) +{ + struct memblock_region *rgn1 =3D &memblock.reserved.regions[1]; + struct memblock_region *rgn2 =3D &memblock.reserved.regions[0]; + void *allocated_ptr =3D NULL; + struct region r1, r2; + phys_addr_t r3_size =3D SZ_64; + phys_addr_t gap_size =3D SMP_CACHE_BYTES; + phys_addr_t total_size; + phys_addr_t max_addr; + phys_addr_t min_addr; + + PREFIX_PUSH(); + setup_memblock(); + + r1.base =3D memblock_end_of_DRAM() - SMP_CACHE_BYTES * 2; + r1.size =3D SMP_CACHE_BYTES; + + r2.size =3D SZ_128; + r2.base =3D r1.base - (r3_size + gap_size + r2.size); + + total_size =3D r1.size + r2.size + r3_size; + min_addr =3D r2.base + r2.size; + max_addr =3D r1.base; + + memblock_reserve(r1.base, r1.size); + memblock_reserve(r2.base, r2.size); + + allocated_ptr =3D memblock_alloc_exact_nid_raw(r3_size, SMP_CACHE_BYTES, + min_addr, max_addr, + NUMA_NO_NODE); + + ASSERT_NE(allocated_ptr, NULL); + ASSERT_MEM_NE(allocated_ptr, 0, r3_size); + + ASSERT_EQ(rgn1->size, r1.size + r3_size); + ASSERT_EQ(rgn1->base, max_addr - r3_size); + + ASSERT_EQ(rgn2->size, r2.size); + ASSERT_EQ(rgn2->base, r2.base); + + ASSERT_EQ(memblock.reserved.cnt, 2); + ASSERT_EQ(memblock.reserved.total_size, total_size); + + test_pass_pop(); + + return 0; +} + +/* + * A test that tries to allocate memory within min_addr and max_add range,= when + * there are two reserved regions at the borders, with a gap of a size equ= al to + * the size of the new region: + * + * + + + * | +--------+--------+--------+ | + * | | r2 | r3 | r1 | | + * +-----+--------+--------+--------+-----+ + * ^ ^ + * | | + * min_addr max_addr + * + * Expect to merge all of the regions into one. The region counter and tot= al + * size fields get updated. + */ +static int alloc_exact_nid_reserved_full_merge_generic_check(void) +{ + struct memblock_region *rgn =3D &memblock.reserved.regions[0]; + void *allocated_ptr =3D NULL; + struct region r1, r2; + phys_addr_t r3_size =3D SZ_64; + phys_addr_t total_size; + phys_addr_t max_addr; + phys_addr_t min_addr; + + PREFIX_PUSH(); + setup_memblock(); + + r1.base =3D memblock_end_of_DRAM() - SMP_CACHE_BYTES * 2; + r1.size =3D SMP_CACHE_BYTES; + + r2.size =3D SZ_128; + r2.base =3D r1.base - (r3_size + r2.size); + + total_size =3D r1.size + r2.size + r3_size; + min_addr =3D r2.base + r2.size; + max_addr =3D r1.base; + + memblock_reserve(r1.base, r1.size); + memblock_reserve(r2.base, r2.size); + + allocated_ptr =3D memblock_alloc_exact_nid_raw(r3_size, SMP_CACHE_BYTES, + min_addr, max_addr, + NUMA_NO_NODE); + + ASSERT_NE(allocated_ptr, NULL); + ASSERT_MEM_NE(allocated_ptr, 0, r3_size); + + ASSERT_EQ(rgn->size, total_size); + ASSERT_EQ(rgn->base, r2.base); + + ASSERT_EQ(memblock.reserved.cnt, 1); + ASSERT_EQ(memblock.reserved.total_size, total_size); + + test_pass_pop(); + + return 0; +} + +/* + * A test that tries to allocate memory within min_addr and max_add range,= when + * there are two reserved regions at the borders, with a gap that can't fit + * a new region: + * + * + + + * | +----------+------+ +------+ | + * | | r3 | r2 | | r1 | | + * +--+----------+------+----+------+---+ + * ^ ^ + * | | + * | max_addr + * | + * min_addr + * + * Expect to merge the new region with r2. The second region does not get + * updated. The total size counter gets updated. + */ +static int alloc_exact_nid_top_down_reserved_no_space_check(void) +{ + struct memblock_region *rgn1 =3D &memblock.reserved.regions[1]; + struct memblock_region *rgn2 =3D &memblock.reserved.regions[0]; + void *allocated_ptr =3D NULL; + struct region r1, r2; + phys_addr_t r3_size =3D SZ_256; + phys_addr_t gap_size =3D SMP_CACHE_BYTES; + phys_addr_t total_size; + phys_addr_t max_addr; + phys_addr_t min_addr; + + PREFIX_PUSH(); + setup_memblock(); + + r1.base =3D memblock_end_of_DRAM() - SMP_CACHE_BYTES * 2; + r1.size =3D SMP_CACHE_BYTES; + + r2.size =3D SZ_128; + r2.base =3D r1.base - (r2.size + gap_size); + + total_size =3D r1.size + r2.size + r3_size; + min_addr =3D r2.base + r2.size; + max_addr =3D r1.base; + + memblock_reserve(r1.base, r1.size); + memblock_reserve(r2.base, r2.size); + + allocated_ptr =3D memblock_alloc_exact_nid_raw(r3_size, SMP_CACHE_BYTES, + min_addr, max_addr, + NUMA_NO_NODE); + + ASSERT_NE(allocated_ptr, NULL); + ASSERT_MEM_NE(allocated_ptr, 0, r3_size); + + ASSERT_EQ(rgn1->size, r1.size); + ASSERT_EQ(rgn1->base, r1.base); + + ASSERT_EQ(rgn2->size, r2.size + r3_size); + ASSERT_EQ(rgn2->base, r2.base - r3_size); + + ASSERT_EQ(memblock.reserved.cnt, 2); + ASSERT_EQ(memblock.reserved.total_size, total_size); + + test_pass_pop(); + + return 0; +} + +/* + * A test that tries to allocate memory within min_addr and max_add range,= but + * it's too narrow and everything else is reserved: + * + * +-----------+ + * | new | + * +-----------+ + * + + + * |--------------+ +----------| + * | r2 | | r1 | + * +--------------+------+----------+ + * ^ ^ + * | | + * | max_addr + * | + * min_addr + * + * Expect no allocation to happen. + */ + +static int alloc_exact_nid_reserved_all_generic_check(void) +{ + void *allocated_ptr =3D NULL; + struct region r1, r2; + phys_addr_t r3_size =3D SZ_256; + phys_addr_t gap_size =3D SMP_CACHE_BYTES; + phys_addr_t max_addr; + phys_addr_t min_addr; + + PREFIX_PUSH(); + setup_memblock(); + + r1.base =3D memblock_end_of_DRAM() - SMP_CACHE_BYTES; + r1.size =3D SMP_CACHE_BYTES; + + r2.size =3D MEM_SIZE - (r1.size + gap_size); + r2.base =3D memblock_start_of_DRAM(); + + min_addr =3D r2.base + r2.size; + max_addr =3D r1.base; + + memblock_reserve(r1.base, r1.size); + memblock_reserve(r2.base, r2.size); + + allocated_ptr =3D memblock_alloc_exact_nid_raw(r3_size, SMP_CACHE_BYTES, + min_addr, max_addr, + NUMA_NO_NODE); + + ASSERT_EQ(allocated_ptr, NULL); + + test_pass_pop(); + + return 0; +} + +/* + * A test that tries to allocate a memory region, where max_addr is + * bigger than the end address of the available memory. Expect to allocate + * a region that ends before the end of the memory. + */ +static int alloc_exact_nid_top_down_cap_max_check(void) +{ + struct memblock_region *rgn =3D &memblock.reserved.regions[0]; + void *allocated_ptr =3D NULL; + phys_addr_t size =3D SZ_256; + phys_addr_t min_addr; + phys_addr_t max_addr; + + PREFIX_PUSH(); + setup_memblock(); + + min_addr =3D memblock_end_of_DRAM() - SZ_1K; + max_addr =3D memblock_end_of_DRAM() + SZ_256; + + allocated_ptr =3D memblock_alloc_exact_nid_raw(size, SMP_CACHE_BYTES, + min_addr, max_addr, + NUMA_NO_NODE); + + ASSERT_NE(allocated_ptr, NULL); + ASSERT_MEM_NE(allocated_ptr, 0, size); + + ASSERT_EQ(rgn->size, size); + ASSERT_EQ(rgn->base, memblock_end_of_DRAM() - size); + + ASSERT_EQ(memblock.reserved.cnt, 1); + ASSERT_EQ(memblock.reserved.total_size, size); + + test_pass_pop(); + + return 0; +} + +/* + * A test that tries to allocate a memory region, where min_addr is + * smaller than the start address of the available memory. Expect to alloc= ate + * a region that ends before the end of the memory. + */ +static int alloc_exact_nid_top_down_cap_min_check(void) +{ + struct memblock_region *rgn =3D &memblock.reserved.regions[0]; + void *allocated_ptr =3D NULL; + phys_addr_t size =3D SZ_1K; + phys_addr_t min_addr; + phys_addr_t max_addr; + + PREFIX_PUSH(); + setup_memblock(); + + min_addr =3D memblock_start_of_DRAM() - SZ_256; + max_addr =3D memblock_end_of_DRAM(); + + allocated_ptr =3D memblock_alloc_exact_nid_raw(size, SMP_CACHE_BYTES, + min_addr, max_addr, + NUMA_NO_NODE); + + ASSERT_NE(allocated_ptr, NULL); + ASSERT_MEM_NE(allocated_ptr, 0, size); + + ASSERT_EQ(rgn->size, size); + ASSERT_EQ(rgn->base, memblock_end_of_DRAM() - size); + + ASSERT_EQ(memblock.reserved.cnt, 1); + ASSERT_EQ(memblock.reserved.total_size, size); + + test_pass_pop(); + + return 0; +} + +/* + * A simple test that tries to allocate a memory region within min_addr and + * max_addr range: + * + * + + + * | +-----------+ | | + * | | rgn | | | + * +----+-----------+-----------+------+ + * ^ ^ + * | | + * min_addr max_addr + * + * Expect to allocate a region that ends before max_addr. + */ +static int alloc_exact_nid_bottom_up_simple_check(void) +{ + struct memblock_region *rgn =3D &memblock.reserved.regions[0]; + void *allocated_ptr =3D NULL; + phys_addr_t size =3D SZ_128; + phys_addr_t min_addr; + phys_addr_t max_addr; + phys_addr_t rgn_end; + + PREFIX_PUSH(); + setup_memblock(); + + min_addr =3D memblock_start_of_DRAM() + SMP_CACHE_BYTES * 2; + max_addr =3D min_addr + SZ_512; + + allocated_ptr =3D memblock_alloc_exact_nid_raw(size, SMP_CACHE_BYTES, + min_addr, max_addr, + NUMA_NO_NODE); + rgn_end =3D rgn->base + rgn->size; + + ASSERT_NE(allocated_ptr, NULL); + ASSERT_MEM_NE(allocated_ptr, 0, size); + + ASSERT_EQ(rgn->size, size); + ASSERT_EQ(rgn->base, min_addr); + ASSERT_LT(rgn_end, max_addr); + + ASSERT_EQ(memblock.reserved.cnt, 1); + ASSERT_EQ(memblock.reserved.total_size, size); + + test_pass_pop(); + + return 0; +} + +/* + * A simple test that tries to allocate a memory region within min_addr and + * max_addr range, where the start address is misaligned: + * + * + + + * | + +-----------+ + | + * | | | rgn | | | + * +-----+---+-----------+-----+-----+ + * ^ ^----. ^ + * | | | + * min_add | max_addr + * | + * Aligned address + * boundary + * + * Expect to allocate an aligned region that ends before max_addr. + */ +static int alloc_exact_nid_bottom_up_start_misaligned_check(void) +{ + struct memblock_region *rgn =3D &memblock.reserved.regions[0]; + void *allocated_ptr =3D NULL; + phys_addr_t size =3D SZ_128; + phys_addr_t misalign =3D SZ_2; + phys_addr_t min_addr; + phys_addr_t max_addr; + phys_addr_t rgn_end; + + PREFIX_PUSH(); + setup_memblock(); + + min_addr =3D memblock_start_of_DRAM() + misalign; + max_addr =3D min_addr + SZ_512; + + allocated_ptr =3D memblock_alloc_exact_nid_raw(size, SMP_CACHE_BYTES, + min_addr, max_addr, + NUMA_NO_NODE); + rgn_end =3D rgn->base + rgn->size; + + ASSERT_NE(allocated_ptr, NULL); + ASSERT_MEM_NE(allocated_ptr, 0, size); + + ASSERT_EQ(rgn->size, size); + ASSERT_EQ(rgn->base, min_addr + (SMP_CACHE_BYTES - misalign)); + ASSERT_LT(rgn_end, max_addr); + + ASSERT_EQ(memblock.reserved.cnt, 1); + ASSERT_EQ(memblock.reserved.total_size, size); + + test_pass_pop(); + + return 0; +} + +/* + * A test that tries to allocate a memory region, which can't fit into min= _addr + * and max_addr range: + * + * + + + * |---------+ + + | + * | rgn | | | | + * +---------+---------+----+------+ + * ^ ^ + * | | + * | max_addr + * | + * min_add + * + * Expect to drop the lower limit and allocate a memory region which + * starts at the beginning of the available memory. + */ +static int alloc_exact_nid_bottom_up_narrow_range_check(void) +{ + struct memblock_region *rgn =3D &memblock.reserved.regions[0]; + void *allocated_ptr =3D NULL; + phys_addr_t size =3D SZ_256; + phys_addr_t min_addr; + phys_addr_t max_addr; + + PREFIX_PUSH(); + setup_memblock(); + + min_addr =3D memblock_start_of_DRAM() + SZ_512; + max_addr =3D min_addr + SMP_CACHE_BYTES; + + allocated_ptr =3D memblock_alloc_exact_nid_raw(size, SMP_CACHE_BYTES, + min_addr, max_addr, + NUMA_NO_NODE); + + ASSERT_NE(allocated_ptr, NULL); + ASSERT_MEM_NE(allocated_ptr, 0, size); + + ASSERT_EQ(rgn->size, size); + ASSERT_EQ(rgn->base, memblock_start_of_DRAM()); + + ASSERT_EQ(memblock.reserved.cnt, 1); + ASSERT_EQ(memblock.reserved.total_size, size); + + test_pass_pop(); + + return 0; +} + +/* + * A test that tries to allocate memory within min_addr and max_add range,= when + * there are two reserved regions at the borders, with a gap big enough to= fit + * a new region: + * + * + + + * | +--------+-------+ +------+ | + * | | r2 | rgn | | r1 | | + * +----+--------+-------+---+------+--+ + * ^ ^ + * | | + * min_addr max_addr + * + * Expect to merge the new region with r2. The second region does not get + * updated. The total size field gets updated. + */ + +static int alloc_exact_nid_bottom_up_reserved_with_space_check(void) +{ + struct memblock_region *rgn1 =3D &memblock.reserved.regions[1]; + struct memblock_region *rgn2 =3D &memblock.reserved.regions[0]; + void *allocated_ptr =3D NULL; + struct region r1, r2; + phys_addr_t r3_size =3D SZ_64; + phys_addr_t gap_size =3D SMP_CACHE_BYTES; + phys_addr_t total_size; + phys_addr_t max_addr; + phys_addr_t min_addr; + + PREFIX_PUSH(); + setup_memblock(); + + r1.base =3D memblock_end_of_DRAM() - SMP_CACHE_BYTES * 2; + r1.size =3D SMP_CACHE_BYTES; + + r2.size =3D SZ_128; + r2.base =3D r1.base - (r3_size + gap_size + r2.size); + + total_size =3D r1.size + r2.size + r3_size; + min_addr =3D r2.base + r2.size; + max_addr =3D r1.base; + + memblock_reserve(r1.base, r1.size); + memblock_reserve(r2.base, r2.size); + + allocated_ptr =3D memblock_alloc_exact_nid_raw(r3_size, SMP_CACHE_BYTES, + min_addr, max_addr, + NUMA_NO_NODE); + + ASSERT_NE(allocated_ptr, NULL); + ASSERT_MEM_NE(allocated_ptr, 0, r3_size); + + ASSERT_EQ(rgn1->size, r1.size); + ASSERT_EQ(rgn1->base, max_addr); + + ASSERT_EQ(rgn2->size, r2.size + r3_size); + ASSERT_EQ(rgn2->base, r2.base); + + ASSERT_EQ(memblock.reserved.cnt, 2); + ASSERT_EQ(memblock.reserved.total_size, total_size); + + test_pass_pop(); + + return 0; +} + +/* + * A test that tries to allocate memory within min_addr and max_add range,= when + * there are two reserved regions at the borders, with a gap of a size equ= al to + * the size of the new region: + * + * + + + * |----------+ +------+ +----+ | + * | r3 | | r2 | | r1 | | + * +----------+----+------+---+----+--+ + * ^ ^ + * | | + * | max_addr + * | + * min_addr + * + * Expect to drop the lower limit and allocate memory at the beginning of = the + * available memory. The region counter and total size fields get updated. + * Other regions are not modified. + */ + +static int alloc_exact_nid_bottom_up_reserved_no_space_check(void) +{ + struct memblock_region *rgn1 =3D &memblock.reserved.regions[2]; + struct memblock_region *rgn2 =3D &memblock.reserved.regions[1]; + struct memblock_region *rgn3 =3D &memblock.reserved.regions[0]; + void *allocated_ptr =3D NULL; + struct region r1, r2; + phys_addr_t r3_size =3D SZ_256; + phys_addr_t gap_size =3D SMP_CACHE_BYTES; + phys_addr_t total_size; + phys_addr_t max_addr; + phys_addr_t min_addr; + + PREFIX_PUSH(); + setup_memblock(); + + r1.base =3D memblock_end_of_DRAM() - SMP_CACHE_BYTES * 2; + r1.size =3D SMP_CACHE_BYTES; + + r2.size =3D SZ_128; + r2.base =3D r1.base - (r2.size + gap_size); + + total_size =3D r1.size + r2.size + r3_size; + min_addr =3D r2.base + r2.size; + max_addr =3D r1.base; + + memblock_reserve(r1.base, r1.size); + memblock_reserve(r2.base, r2.size); + + allocated_ptr =3D memblock_alloc_exact_nid_raw(r3_size, SMP_CACHE_BYTES, + min_addr, max_addr, + NUMA_NO_NODE); + + ASSERT_NE(allocated_ptr, NULL); + ASSERT_MEM_NE(allocated_ptr, 0, r3_size); + + ASSERT_EQ(rgn3->size, r3_size); + ASSERT_EQ(rgn3->base, memblock_start_of_DRAM()); + + ASSERT_EQ(rgn2->size, r2.size); + ASSERT_EQ(rgn2->base, r2.base); + + ASSERT_EQ(rgn1->size, r1.size); + ASSERT_EQ(rgn1->base, r1.base); + + ASSERT_EQ(memblock.reserved.cnt, 3); + ASSERT_EQ(memblock.reserved.total_size, total_size); + + test_pass_pop(); + + return 0; +} + +/* + * A test that tries to allocate a memory region, where max_addr is + * bigger than the end address of the available memory. Expect to allocate + * a region that starts at the min_addr. + */ +static int alloc_exact_nid_bottom_up_cap_max_check(void) +{ + struct memblock_region *rgn =3D &memblock.reserved.regions[0]; + void *allocated_ptr =3D NULL; + phys_addr_t size =3D SZ_256; + phys_addr_t min_addr; + phys_addr_t max_addr; + + PREFIX_PUSH(); + setup_memblock(); + + min_addr =3D memblock_start_of_DRAM() + SZ_1K; + max_addr =3D memblock_end_of_DRAM() + SZ_256; + + allocated_ptr =3D memblock_alloc_exact_nid_raw(size, SMP_CACHE_BYTES, + min_addr, max_addr, + NUMA_NO_NODE); + + ASSERT_NE(allocated_ptr, NULL); + ASSERT_MEM_NE(allocated_ptr, 0, size); + + ASSERT_EQ(rgn->size, size); + ASSERT_EQ(rgn->base, min_addr); + + ASSERT_EQ(memblock.reserved.cnt, 1); + ASSERT_EQ(memblock.reserved.total_size, size); + + test_pass_pop(); + + return 0; +} + +/* + * A test that tries to allocate a memory region, where min_addr is + * smaller than the start address of the available memory. Expect to alloc= ate + * a region at the beginning of the available memory. + */ +static int alloc_exact_nid_bottom_up_cap_min_check(void) +{ + struct memblock_region *rgn =3D &memblock.reserved.regions[0]; + void *allocated_ptr =3D NULL; + phys_addr_t size =3D SZ_1K; + phys_addr_t min_addr; + phys_addr_t max_addr; + + PREFIX_PUSH(); + setup_memblock(); + + min_addr =3D memblock_start_of_DRAM(); + max_addr =3D memblock_end_of_DRAM() - SZ_256; + + allocated_ptr =3D memblock_alloc_exact_nid_raw(size, SMP_CACHE_BYTES, + min_addr, max_addr, + NUMA_NO_NODE); + + ASSERT_NE(allocated_ptr, NULL); + ASSERT_MEM_NE(allocated_ptr, 0, size); + + ASSERT_EQ(rgn->size, size); + ASSERT_EQ(rgn->base, memblock_start_of_DRAM()); + + ASSERT_EQ(memblock.reserved.cnt, 1); + ASSERT_EQ(memblock.reserved.total_size, size); + + test_pass_pop(); + + return 0; +} + +/* Test case wrappers for range tests */ +static int alloc_exact_nid_simple_check(void) +{ + test_print("\tRunning %s...\n", __func__); + memblock_set_bottom_up(false); + alloc_exact_nid_top_down_simple_check(); + memblock_set_bottom_up(true); + alloc_exact_nid_bottom_up_simple_check(); + + return 0; +} + +static int alloc_exact_nid_misaligned_check(void) +{ + test_print("\tRunning %s...\n", __func__); + memblock_set_bottom_up(false); + alloc_exact_nid_top_down_end_misaligned_check(); + memblock_set_bottom_up(true); + alloc_exact_nid_bottom_up_start_misaligned_check(); + + return 0; +} + +static int alloc_exact_nid_narrow_range_check(void) +{ + test_print("\tRunning %s...\n", __func__); + memblock_set_bottom_up(false); + alloc_exact_nid_top_down_narrow_range_check(); + memblock_set_bottom_up(true); + alloc_exact_nid_bottom_up_narrow_range_check(); + + return 0; +} + +static int alloc_exact_nid_reserved_with_space_check(void) +{ + test_print("\tRunning %s...\n", __func__); + memblock_set_bottom_up(false); + alloc_exact_nid_top_down_reserved_with_space_check(); + memblock_set_bottom_up(true); + alloc_exact_nid_bottom_up_reserved_with_space_check(); + + return 0; +} + +static int alloc_exact_nid_reserved_no_space_check(void) +{ + test_print("\tRunning %s...\n", __func__); + memblock_set_bottom_up(false); + alloc_exact_nid_top_down_reserved_no_space_check(); + memblock_set_bottom_up(true); + alloc_exact_nid_bottom_up_reserved_no_space_check(); + + return 0; +} + +static int alloc_exact_nid_cap_max_check(void) +{ + test_print("\tRunning %s...\n", __func__); + memblock_set_bottom_up(false); + alloc_exact_nid_top_down_cap_max_check(); + memblock_set_bottom_up(true); + alloc_exact_nid_bottom_up_cap_max_check(); + + return 0; +} + +static int alloc_exact_nid_cap_min_check(void) +{ + test_print("\tRunning %s...\n", __func__); + memblock_set_bottom_up(false); + alloc_exact_nid_top_down_cap_min_check(); + memblock_set_bottom_up(true); + alloc_exact_nid_bottom_up_cap_min_check(); + + return 0; +} + +static int alloc_exact_nid_min_reserved_check(void) +{ + test_print("\tRunning %s...\n", __func__); + run_top_down(alloc_exact_nid_min_reserved_generic_check); + run_bottom_up(alloc_exact_nid_min_reserved_generic_check); + + return 0; +} + +static int alloc_exact_nid_max_reserved_check(void) +{ + test_print("\tRunning %s...\n", __func__); + run_top_down(alloc_exact_nid_max_reserved_generic_check); + run_bottom_up(alloc_exact_nid_max_reserved_generic_check); + + return 0; +} + +static int alloc_exact_nid_exact_address_check(void) +{ + test_print("\tRunning %s...\n", __func__); + run_top_down(alloc_exact_nid_exact_address_generic_check); + run_bottom_up(alloc_exact_nid_exact_address_generic_check); + + return 0; +} + +static int alloc_exact_nid_reserved_full_merge_check(void) +{ + test_print("\tRunning %s...\n", __func__); + run_top_down(alloc_exact_nid_reserved_full_merge_generic_check); + run_bottom_up(alloc_exact_nid_reserved_full_merge_generic_check); + + return 0; +} + +static int alloc_exact_nid_reserved_all_check(void) +{ + test_print("\tRunning %s...\n", __func__); + run_top_down(alloc_exact_nid_reserved_all_generic_check); + run_bottom_up(alloc_exact_nid_reserved_all_generic_check); + + return 0; +} + +static int alloc_exact_nid_low_max_check(void) +{ + test_print("\tRunning %s...\n", __func__); + run_top_down(alloc_exact_nid_low_max_generic_check); + run_bottom_up(alloc_exact_nid_low_max_generic_check); + + return 0; +} + +static int memblock_alloc_exact_nid_range_checks(void) +{ + test_print("Running %s range tests...\n", FUNC_NAME); + + alloc_exact_nid_simple_check(); + alloc_exact_nid_misaligned_check(); + alloc_exact_nid_narrow_range_check(); + alloc_exact_nid_reserved_with_space_check(); + alloc_exact_nid_reserved_no_space_check(); + alloc_exact_nid_cap_max_check(); + alloc_exact_nid_cap_min_check(); + + alloc_exact_nid_min_reserved_check(); + alloc_exact_nid_max_reserved_check(); + alloc_exact_nid_exact_address_check(); + alloc_exact_nid_reserved_full_merge_check(); + alloc_exact_nid_reserved_all_check(); + alloc_exact_nid_low_max_check(); + + return 0; +} + +int memblock_alloc_exact_nid_checks(void) +{ + prefix_reset(); + prefix_push(FUNC_NAME); + + reset_memblock_attributes(); + dummy_physical_memory_init(); + + memblock_alloc_exact_nid_range_checks(); + + dummy_physical_memory_cleanup(); + + prefix_pop(); + + return 0; +} diff --git a/tools/testing/memblock/tests/alloc_exact_nid_api.h b/tools/tes= ting/memblock/tests/alloc_exact_nid_api.h new file mode 100644 index 000000000000..4408719de3b9 --- /dev/null +++ b/tools/testing/memblock/tests/alloc_exact_nid_api.h @@ -0,0 +1,9 @@ +/* SPDX-License-Identifier: GPL-2.0-or-later */ +#ifndef _MEMBLOCK_ALLOC_EXACT_NID_H +#define _MEMBLOCK_ALLOC_EXACT_NID_H + +#include "common.h" + +int memblock_alloc_exact_nid_checks(void); + +#endif --=20 2.25.1 From nobody Wed Apr 8 10:33:11 2026 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id EC833C433FE for ; Sun, 2 Oct 2022 23:42:12 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229773AbiJBXmK (ORCPT ); Sun, 2 Oct 2022 19:42:10 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:59120 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229616AbiJBXmC (ORCPT ); Sun, 2 Oct 2022 19:42:02 -0400 Received: from mail-pj1-x1041.google.com (mail-pj1-x1041.google.com [IPv6:2607:f8b0:4864:20::1041]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 84DAD32B9B for ; Sun, 2 Oct 2022 16:42:01 -0700 (PDT) Received: by mail-pj1-x1041.google.com with SMTP id e11-20020a17090a77cb00b00205edbfd646so13827494pjs.1 for ; Sun, 02 Oct 2022 16:42:01 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date; bh=O3V0nPhtlK3TXA87oz1HqqVg77ploxiQ5BsPzqU7s6E=; b=pH+nMu9MHlTouMQCEPX8JYQnMpH5S3XId0ox2EcBfKIftzjva3tHlXAXN+Cf74tJ4Z e71RsHSSlmTGcLJLviZKgf8Kpv4yAq+DI+UiF8vPs1MOMYaN5S4hN/llw8tbYuumWvQQ 240LA+0EvIY4zxb8VRKBuPlJfJl6TtHJlqFJC2GilxHPFmA1cnUJt7822RiKwqCm8VLX WpASbefGuP/v1bLApR3UrbVR9MzAXBX8fE/uJ9F0oEg5GNQ5nx/RUtlcRXN0YljY0esF gwDSUAsGj4EMKoZOTlFl5lMsYZkQERffDaltM8PsfecEPsX2+07qMowqT/w1yS7MqfH7 Fx6w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date; bh=O3V0nPhtlK3TXA87oz1HqqVg77ploxiQ5BsPzqU7s6E=; b=Pl0oslvygZdFx2IMOlxaEoGE0u2tlYF2UL9d+LOuEpOM6E3VG0tsTx1yjqhIzRQNrL 1FHIo24k9Mr2+KY27HhAU2j+SD00/R3f0Vuy/6k2S3DFMx5Ao8MjP1mN1u0tD/SoCZyh JtI7+wuDynv4QKBvPiVe/swu/Z9tZukY8FJlWYQF4YqABxn74iP+D9pLD1WdFKYoHezG 5K2BZsS3PfIyoyrFFCZm4mWd57DOY8GKzbvLoFoPU4WJ7aDAEDKCqbyOjaBGt/GYMYpm nwVtI5XFK5GpHqGztyU0kH1EzAQ3cZRKONt2frO5kt5E1sZMJPffzMVV8e3Lj8gGT7Vp lTWg== X-Gm-Message-State: ACrzQf2na8vlSKuY5M3TyshU7GbNWDPY5GfWosngKLsI5FGOpzrUNNNU kxKde0mGgqjmpYxRRJ584hc= X-Google-Smtp-Source: AMsMyM6TpiObcZgj3h8/OES/8fZdbphnzDfBkFQl5E+8YTXgvdORApjKygQjCoW2Vm3puBrV8GEtZQ== X-Received: by 2002:a17:90b:f18:b0:20a:9d2b:b75a with SMTP id br24-20020a17090b0f1800b0020a9d2bb75amr2576019pjb.95.1664754120906; Sun, 02 Oct 2022 16:42:00 -0700 (PDT) Received: from sophie ([89.46.114.181]) by smtp.gmail.com with ESMTPSA id n11-20020a17090a394b00b00208c58d5a0esm6273539pjf.40.2022.10.02.16.42.00 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 02 Oct 2022 16:42:00 -0700 (PDT) From: Rebecca Mckeever To: Mike Rapoport , linux-mm@kvack.org, linux-kernel@vger.kernel.org Cc: David Hildenbrand , Rebecca Mckeever Subject: [PATCH v2 2/5] memblock tests: add top-down NUMA tests for memblock_alloc_exact_nid_raw Date: Sun, 2 Oct 2022 18:41:35 -0500 Message-Id: <2cc4c0972a472579da227007fc160d69e4f33727.1664753534.git.remckee0@gmail.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" Add tests for memblock_alloc_exact_nid_raw() where the simulated physical memory is set up with multiple NUMA nodes. Additionally, all of these tests set nid !=3D NUMA_NO_NODE. These tests are run with a top-down allocation direction. The tested scenarios are: Range unrestricted: - region can be allocated in the specific node requested: + there are no previously reserved regions + the requested node is partially reserved but has enough space Range restricted: - region can be allocated in the specific node requested after dropping min_addr: + range partially overlaps with two different nodes, where the first node is the requested node + range partially overlaps with two different nodes, where the requested node ends before min_addr + range overlaps with multiple nodes along node boundaries, and the requested node ends before min_addr Signed-off-by: Rebecca Mckeever --- .../memblock/tests/alloc_exact_nid_api.c | 344 ++++++++++++++++++ .../memblock/tests/alloc_exact_nid_api.h | 16 + 2 files changed, 360 insertions(+) diff --git a/tools/testing/memblock/tests/alloc_exact_nid_api.c b/tools/tes= ting/memblock/tests/alloc_exact_nid_api.c index 452aba1f19db..48c1b97a3f98 100644 --- a/tools/testing/memblock/tests/alloc_exact_nid_api.c +++ b/tools/testing/memblock/tests/alloc_exact_nid_api.c @@ -3,6 +3,21 @@ =20 #define FUNC_NAME "memblock_alloc_exact_nid_raw" =20 +/* + * contains the fraction of MEM_SIZE contained in each node in basis point + * units (one hundredth of 1% or 1/10000) + */ +static const unsigned int node_fractions[] =3D { + 2500, /* 1/4 */ + 625, /* 1/16 */ + 1250, /* 1/8 */ + 1250, /* 1/8 */ + 625, /* 1/16 */ + 625, /* 1/16 */ + 2500, /* 1/4 */ + 625, /* 1/16 */ +}; + /* * A simple test that tries to allocate a memory region within min_addr and * max_addr range: @@ -1190,6 +1205,334 @@ static int memblock_alloc_exact_nid_range_checks(vo= id) return 0; } =20 +/* + * A test that tries to allocate a memory region in a specific NUMA node t= hat + * has enough memory to allocate a region of the requested size. + * Expect to allocate an aligned region at the end of the requested node. + */ +static int alloc_exact_nid_top_down_numa_simple_check(void) +{ + int nid_req =3D 3; + struct memblock_region *new_rgn =3D &memblock.reserved.regions[0]; + struct memblock_region *req_node =3D &memblock.memory.regions[nid_req]; + void *allocated_ptr =3D NULL; + phys_addr_t size; + phys_addr_t min_addr; + phys_addr_t max_addr; + + PREFIX_PUSH(); + setup_numa_memblock(node_fractions); + + ASSERT_LE(SZ_4, req_node->size); + size =3D req_node->size / SZ_4; + min_addr =3D memblock_start_of_DRAM(); + max_addr =3D memblock_end_of_DRAM(); + + allocated_ptr =3D memblock_alloc_exact_nid_raw(size, SMP_CACHE_BYTES, + min_addr, max_addr, + nid_req); + + ASSERT_NE(allocated_ptr, NULL); + ASSERT_MEM_NE(allocated_ptr, 0, size); + + ASSERT_EQ(new_rgn->size, size); + ASSERT_EQ(new_rgn->base, region_end(req_node) - size); + ASSERT_LE(req_node->base, new_rgn->base); + + ASSERT_EQ(memblock.reserved.cnt, 1); + ASSERT_EQ(memblock.reserved.total_size, size); + + test_pass_pop(); + + return 0; +} + +/* + * A test that tries to allocate a memory region in a specific NUMA node t= hat + * is partially reserved but has enough memory for the allocated region: + * + * | +---------------------------------------+ | + * | | requested | | + * +-----------+---------------------------------------+----------+ + * + * | +------------------+ +-----+ | + * | | reserved | | new | | + * +-----------+------------------+--------------+-----+----------+ + * + * Expect to allocate an aligned region at the end of the requested node. = The + * region count and total size get updated. + */ +static int alloc_exact_nid_top_down_numa_part_reserved_check(void) +{ + int nid_req =3D 4; + struct memblock_region *new_rgn =3D &memblock.reserved.regions[1]; + struct memblock_region *req_node =3D &memblock.memory.regions[nid_req]; + void *allocated_ptr =3D NULL; + struct region r1; + phys_addr_t size; + phys_addr_t min_addr; + phys_addr_t max_addr; + + PREFIX_PUSH(); + setup_numa_memblock(node_fractions); + + ASSERT_LE(SZ_8, req_node->size); + r1.base =3D req_node->base; + r1.size =3D req_node->size / SZ_2; + size =3D r1.size / SZ_4; + min_addr =3D memblock_start_of_DRAM(); + max_addr =3D memblock_end_of_DRAM(); + + memblock_reserve(r1.base, r1.size); + allocated_ptr =3D memblock_alloc_exact_nid_raw(size, SMP_CACHE_BYTES, + min_addr, max_addr, + nid_req); + + ASSERT_NE(allocated_ptr, NULL); + ASSERT_MEM_NE(allocated_ptr, 0, size); + + ASSERT_EQ(new_rgn->size, size); + ASSERT_EQ(new_rgn->base, region_end(req_node) - size); + ASSERT_LE(req_node->base, new_rgn->base); + + ASSERT_EQ(memblock.reserved.cnt, 2); + ASSERT_EQ(memblock.reserved.total_size, size + r1.size); + + test_pass_pop(); + + return 0; +} + +/* + * A test that tries to allocate a memory region that spans over the min_a= ddr + * and max_addr range and overlaps with two different nodes, where the fir= st + * node is the requested node: + * + * min_addr + * | max_addr + * | | + * v v + * | +-----------------------+-----------+ | + * | | requested | node3 | | + * +-----------+-----------------------+-----------+--------------+ + * + + + * | +-----------+ | + * | | rgn | | + * +-----------------------+-----------+--------------------------+ + * + * Expect to drop the lower limit and allocate a memory region that ends at + * the end of the requested node. + */ +static int alloc_exact_nid_top_down_numa_split_range_low_check(void) +{ + int nid_req =3D 2; + struct memblock_region *new_rgn =3D &memblock.reserved.regions[0]; + struct memblock_region *req_node =3D &memblock.memory.regions[nid_req]; + void *allocated_ptr =3D NULL; + phys_addr_t size =3D SZ_512; + phys_addr_t min_addr; + phys_addr_t max_addr; + phys_addr_t req_node_end; + + PREFIX_PUSH(); + setup_numa_memblock(node_fractions); + + req_node_end =3D region_end(req_node); + min_addr =3D req_node_end - SZ_256; + max_addr =3D min_addr + size; + + allocated_ptr =3D memblock_alloc_exact_nid_raw(size, SMP_CACHE_BYTES, + min_addr, max_addr, + nid_req); + + ASSERT_NE(allocated_ptr, NULL); + ASSERT_MEM_NE(allocated_ptr, 0, size); + + ASSERT_EQ(new_rgn->size, size); + ASSERT_EQ(new_rgn->base, req_node_end - size); + ASSERT_LE(req_node->base, new_rgn->base); + + ASSERT_EQ(memblock.reserved.cnt, 1); + ASSERT_EQ(memblock.reserved.total_size, size); + + test_pass_pop(); + + return 0; +} + +/* + * A test that tries to allocate a memory region that spans over the min_a= ddr + * and max_addr range and overlaps with two different nodes, where the req= uested + * node ends before min_addr: + * + * min_addr + * | max_addr + * | | + * v v + * | +---------------+ +-------------+---------+ | + * | | requested | | node1 | node2 | | + * +----+---------------+--------+-------------+---------+----------+ + * + + + * | +---------+ | + * | | rgn | | + * +----------+---------+-------------------------------------------+ + * + * Expect to drop the lower limit and allocate a memory region that ends at + * the end of the requested node. + */ +static int alloc_exact_nid_top_down_numa_no_overlap_split_check(void) +{ + int nid_req =3D 2; + struct memblock_region *new_rgn =3D &memblock.reserved.regions[0]; + struct memblock_region *req_node =3D &memblock.memory.regions[nid_req]; + struct memblock_region *node2 =3D &memblock.memory.regions[6]; + void *allocated_ptr =3D NULL; + phys_addr_t size; + phys_addr_t min_addr; + phys_addr_t max_addr; + + PREFIX_PUSH(); + setup_numa_memblock(node_fractions); + + size =3D SZ_512; + min_addr =3D node2->base - SZ_256; + max_addr =3D min_addr + size; + + allocated_ptr =3D memblock_alloc_exact_nid_raw(size, SMP_CACHE_BYTES, + min_addr, max_addr, + nid_req); + + ASSERT_NE(allocated_ptr, NULL); + ASSERT_MEM_NE(allocated_ptr, 0, size); + + ASSERT_EQ(new_rgn->size, size); + ASSERT_EQ(new_rgn->base, region_end(req_node) - size); + ASSERT_LE(req_node->base, new_rgn->base); + + ASSERT_EQ(memblock.reserved.cnt, 1); + ASSERT_EQ(memblock.reserved.total_size, size); + + test_pass_pop(); + + return 0; +} + +/* + * A test that tries to allocate memory within min_addr and max_add range = when + * the requested node and the range do not overlap, and requested node ends + * before min_addr. The range overlaps with multiple nodes along node + * boundaries: + * + * min_addr + * | max_addr + * | | + * v v + * |-----------+ +----------+----...----+----------+ | + * | requested | | min node | ... | max node | | + * +-----------+-----------+----------+----...----+----------+------+ + * + + + * | +-----+ | + * | | rgn | | + * +-----+-----+----------------------------------------------------+ + * + * Expect to drop the lower limit and allocate a memory region that ends at + * the end of the requested node. + */ +static int alloc_exact_nid_top_down_numa_no_overlap_low_check(void) +{ + int nid_req =3D 0; + struct memblock_region *new_rgn =3D &memblock.reserved.regions[0]; + struct memblock_region *req_node =3D &memblock.memory.regions[nid_req]; + struct memblock_region *min_node =3D &memblock.memory.regions[2]; + struct memblock_region *max_node =3D &memblock.memory.regions[5]; + void *allocated_ptr =3D NULL; + phys_addr_t size =3D SZ_64; + phys_addr_t max_addr; + phys_addr_t min_addr; + + PREFIX_PUSH(); + setup_numa_memblock(node_fractions); + + min_addr =3D min_node->base; + max_addr =3D region_end(max_node); + + allocated_ptr =3D memblock_alloc_exact_nid_raw(size, SMP_CACHE_BYTES, + min_addr, max_addr, + nid_req); + + ASSERT_NE(allocated_ptr, NULL); + ASSERT_MEM_NE(allocated_ptr, 0, size); + + ASSERT_EQ(new_rgn->size, size); + ASSERT_EQ(new_rgn->base, region_end(req_node) - size); + + ASSERT_EQ(memblock.reserved.cnt, 1); + ASSERT_EQ(memblock.reserved.total_size, size); + + test_pass_pop(); + + return 0; +} + +/* Test case wrappers for NUMA tests */ +static int alloc_exact_nid_numa_simple_check(void) +{ + test_print("\tRunning %s...\n", __func__); + memblock_set_bottom_up(false); + alloc_exact_nid_top_down_numa_simple_check(); + + return 0; +} + +static int alloc_exact_nid_numa_part_reserved_check(void) +{ + test_print("\tRunning %s...\n", __func__); + memblock_set_bottom_up(false); + alloc_exact_nid_top_down_numa_part_reserved_check(); + + return 0; +} + +static int alloc_exact_nid_numa_split_range_low_check(void) +{ + test_print("\tRunning %s...\n", __func__); + memblock_set_bottom_up(false); + alloc_exact_nid_top_down_numa_split_range_low_check(); + + return 0; +} + +static int alloc_exact_nid_numa_no_overlap_split_check(void) +{ + test_print("\tRunning %s...\n", __func__); + memblock_set_bottom_up(false); + alloc_exact_nid_top_down_numa_no_overlap_split_check(); + + return 0; +} + +static int alloc_exact_nid_numa_no_overlap_low_check(void) +{ + test_print("\tRunning %s...\n", __func__); + memblock_set_bottom_up(false); + alloc_exact_nid_top_down_numa_no_overlap_low_check(); + + return 0; +} + +int __memblock_alloc_exact_nid_numa_checks(void) +{ + test_print("Running %s NUMA tests...\n", FUNC_NAME); + + alloc_exact_nid_numa_simple_check(); + alloc_exact_nid_numa_part_reserved_check(); + alloc_exact_nid_numa_split_range_low_check(); + alloc_exact_nid_numa_no_overlap_split_check(); + alloc_exact_nid_numa_no_overlap_low_check(); + + return 0; +} + int memblock_alloc_exact_nid_checks(void) { prefix_reset(); @@ -1199,6 +1542,7 @@ int memblock_alloc_exact_nid_checks(void) dummy_physical_memory_init(); =20 memblock_alloc_exact_nid_range_checks(); + memblock_alloc_exact_nid_numa_checks(); =20 dummy_physical_memory_cleanup(); =20 diff --git a/tools/testing/memblock/tests/alloc_exact_nid_api.h b/tools/tes= ting/memblock/tests/alloc_exact_nid_api.h index 4408719de3b9..cef419d55d2a 100644 --- a/tools/testing/memblock/tests/alloc_exact_nid_api.h +++ b/tools/testing/memblock/tests/alloc_exact_nid_api.h @@ -5,5 +5,21 @@ #include "common.h" =20 int memblock_alloc_exact_nid_checks(void); +int __memblock_alloc_exact_nid_numa_checks(void); + +#ifdef CONFIG_NUMA +static inline int memblock_alloc_exact_nid_numa_checks(void) +{ + __memblock_alloc_exact_nid_numa_checks(); + return 0; +} + +#else +static inline int memblock_alloc_exact_nid_numa_checks(void) +{ + return 0; +} + +#endif /* CONFIG_NUMA */ =20 #endif --=20 2.25.1 From nobody Wed Apr 8 10:33:11 2026 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 7B1C4C433FE for ; Sun, 2 Oct 2022 23:42:16 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230123AbiJBXmO (ORCPT ); Sun, 2 Oct 2022 19:42:14 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:59152 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229749AbiJBXmF (ORCPT ); Sun, 2 Oct 2022 19:42:05 -0400 Received: from mail-pf1-x441.google.com (mail-pf1-x441.google.com [IPv6:2607:f8b0:4864:20::441]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 6D4A432BAF for ; Sun, 2 Oct 2022 16:42:03 -0700 (PDT) Received: by mail-pf1-x441.google.com with SMTP id w2so8845441pfb.0 for ; Sun, 02 Oct 2022 16:42:03 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date; bh=eGAcKbYoA45EhpEphg+aCEcYqZsvjPC//SZfHS2nD+Q=; b=hdQ5JLomtEWxXVMprvrgvxvBCH3IPPw9JI+Pp74Sx8wVfO886XcblZYxJLcBZT0Qph G9ZXPEih8DQjtu3eMIFQTVkdwXLjkCh2LPgxp3IuLPc75nK9dJsn4BqmBgIb1MCuTlB6 fvlDMNy57tbKQ8jWRKEwYRsd5bsmBADUe3zLZyjTxVuBEBGZeLo3TkYpALT+JnOXorjU jRJotuZr52uzQghnLghFMH412m1SKtfAkIcLOnOY7aEEqXKQvw8OdN0HZLeGHDoEyMqZ 09Ioe+36V7SxQ6q9KC8smAmOQ0KKl1qBFJ60FOtKnkgM/TIbPCe/4eHxjRRucS/TRoxA dd+w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date; bh=eGAcKbYoA45EhpEphg+aCEcYqZsvjPC//SZfHS2nD+Q=; b=6kORUF5M99UfcWyZhHKQzZNpaFd0A3cl3frrtTR7WcTmSc9yqKKb1lBl3Bjlgzd2Vy /bWD2Ocu1CqtqGftGu3k2iXQJO4Zool3fLc8zjCrb7YXuBUquDeosjzdiYLUSrZ0pm+5 yK2mUSQTTcg335JV6dnL/Ara4hXclclF350AY3iKsOceVIkHksLxGB2+6LCD95DZq2YW rNz/ns02LvRi4kJtdVejj8HdGlzX7Cr3u6hMUz9mjGvaU5ijsQOqAerHcgQQJTRLckm3 FjXKPvnGxQ6cWnPVMyXVWatVQuNnO8hGgY/NhDLm36Xr3uBei/TpFw9T57N+XevUCya4 j8UA== X-Gm-Message-State: ACrzQf2td1PyqN7zvA765HvZe7oTJ0BGos/WIlQm0Y3sCzgayshPGu0R zDGQ7kYfYg4SDLCww6yrtWSj/1bM+mE= X-Google-Smtp-Source: AMsMyM4CjgjYVGaCZLBFbjoh6DP7VFmeRsvAOJFF0qxLMpItlwbe7mkxohalDZj1VPqgyijwWxO5cQ== X-Received: by 2002:a63:6c06:0:b0:43c:783f:46b3 with SMTP id h6-20020a636c06000000b0043c783f46b3mr16311624pgc.101.1664754122920; Sun, 02 Oct 2022 16:42:02 -0700 (PDT) Received: from sophie ([89.46.114.181]) by smtp.gmail.com with ESMTPSA id x20-20020aa78f14000000b0053e0d6f353esm5980350pfr.27.2022.10.02.16.42.02 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 02 Oct 2022 16:42:02 -0700 (PDT) From: Rebecca Mckeever To: Mike Rapoport , linux-mm@kvack.org, linux-kernel@vger.kernel.org Cc: David Hildenbrand , Rebecca Mckeever Subject: [PATCH v2 3/5] memblock tests: add bottom-up NUMA tests for memblock_alloc_exact_nid_raw Date: Sun, 2 Oct 2022 18:41:36 -0500 Message-Id: X-Mailer: git-send-email 2.25.1 In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" Add tests for memblock_alloc_exact_nid_raw() where the simulated physical memory is set up with multiple NUMA nodes. Additionally, all of these tests set nid !=3D NUMA_NO_NODE. These tests are run with a bottom-up allocation direction. The tested scenarios are: Range unrestricted: - region can be allocated in the specific node requested: + there are no previously reserved regions + the requested node is partially reserved but has enough space Range restricted: - region can be allocated in the specific node requested after dropping min_addr: + range partially overlaps with two different nodes, where the first node is the requested node + range partially overlaps with two different nodes, where the requested node ends before min_addr + range overlaps with multiple nodes along node boundaries, and the requested node ends before min_addr Signed-off-by: Rebecca Mckeever --- .../memblock/tests/alloc_exact_nid_api.c | 282 ++++++++++++++++++ 1 file changed, 282 insertions(+) diff --git a/tools/testing/memblock/tests/alloc_exact_nid_api.c b/tools/tes= ting/memblock/tests/alloc_exact_nid_api.c index 48c1b97a3f98..63279d1e35b1 100644 --- a/tools/testing/memblock/tests/alloc_exact_nid_api.c +++ b/tools/testing/memblock/tests/alloc_exact_nid_api.c @@ -1474,12 +1474,286 @@ static int alloc_exact_nid_top_down_numa_no_overla= p_low_check(void) return 0; } =20 +/* + * A test that tries to allocate a memory region in a specific NUMA node t= hat + * has enough memory to allocate a region of the requested size. + * Expect to allocate an aligned region at the beginning of the requested = node. + */ +static int alloc_exact_nid_bottom_up_numa_simple_check(void) +{ + int nid_req =3D 3; + struct memblock_region *new_rgn =3D &memblock.reserved.regions[0]; + struct memblock_region *req_node =3D &memblock.memory.regions[nid_req]; + void *allocated_ptr =3D NULL; + phys_addr_t size; + phys_addr_t min_addr; + phys_addr_t max_addr; + + PREFIX_PUSH(); + setup_numa_memblock(node_fractions); + + ASSERT_LE(SZ_4, req_node->size); + size =3D req_node->size / SZ_4; + min_addr =3D memblock_start_of_DRAM(); + max_addr =3D memblock_end_of_DRAM(); + + allocated_ptr =3D memblock_alloc_exact_nid_raw(size, SMP_CACHE_BYTES, + min_addr, max_addr, + nid_req); + + ASSERT_NE(allocated_ptr, NULL); + ASSERT_MEM_NE(allocated_ptr, 0, size); + + ASSERT_EQ(new_rgn->size, size); + ASSERT_EQ(new_rgn->base, req_node->base); + ASSERT_LE(region_end(new_rgn), region_end(req_node)); + + ASSERT_EQ(memblock.reserved.cnt, 1); + ASSERT_EQ(memblock.reserved.total_size, size); + + test_pass_pop(); + + return 0; +} + +/* + * A test that tries to allocate a memory region in a specific NUMA node t= hat + * is partially reserved but has enough memory for the allocated region: + * + * | +---------------------------------------+ | + * | | requested | | + * +-----------+---------------------------------------+---------+ + * + * | +------------------+-----+ | + * | | reserved | new | | + * +-----------+------------------+-----+------------------------+ + * + * Expect to allocate an aligned region in the requested node that merges = with + * the existing reserved region. The total size gets updated. + */ +static int alloc_exact_nid_bottom_up_numa_part_reserved_check(void) +{ + int nid_req =3D 4; + struct memblock_region *new_rgn =3D &memblock.reserved.regions[0]; + struct memblock_region *req_node =3D &memblock.memory.regions[nid_req]; + void *allocated_ptr =3D NULL; + struct region r1; + phys_addr_t size; + phys_addr_t min_addr; + phys_addr_t max_addr; + phys_addr_t total_size; + + PREFIX_PUSH(); + setup_numa_memblock(node_fractions); + + ASSERT_LE(SZ_8, req_node->size); + r1.base =3D req_node->base; + r1.size =3D req_node->size / SZ_2; + size =3D r1.size / SZ_4; + min_addr =3D memblock_start_of_DRAM(); + max_addr =3D memblock_end_of_DRAM(); + total_size =3D size + r1.size; + + memblock_reserve(r1.base, r1.size); + allocated_ptr =3D memblock_alloc_exact_nid_raw(size, SMP_CACHE_BYTES, + min_addr, max_addr, + nid_req); + + ASSERT_NE(allocated_ptr, NULL); + ASSERT_MEM_NE(allocated_ptr, 0, size); + + ASSERT_EQ(new_rgn->size, total_size); + ASSERT_EQ(new_rgn->base, req_node->base); + ASSERT_LE(region_end(new_rgn), region_end(req_node)); + + ASSERT_EQ(memblock.reserved.cnt, 1); + ASSERT_EQ(memblock.reserved.total_size, total_size); + + test_pass_pop(); + + return 0; +} + +/* + * A test that tries to allocate a memory region that spans over the min_a= ddr + * and max_addr range and overlaps with two different nodes, where the fir= st + * node is the requested node: + * + * min_addr + * | max_addr + * | | + * v v + * | +-----------------------+-----------+ | + * | | requested | node3 | | + * +-----------+-----------------------+-----------+--------------+ + * + + + * | +-----------+ | + * | | rgn | | + * +-----------+-----------+--------------------------------------+ + * + * Expect to drop the lower limit and allocate a memory region at the begi= nning + * of the requested node. + */ +static int alloc_exact_nid_bottom_up_numa_split_range_low_check(void) +{ + int nid_req =3D 2; + struct memblock_region *new_rgn =3D &memblock.reserved.regions[0]; + struct memblock_region *req_node =3D &memblock.memory.regions[nid_req]; + void *allocated_ptr =3D NULL; + phys_addr_t size =3D SZ_512; + phys_addr_t min_addr; + phys_addr_t max_addr; + phys_addr_t req_node_end; + + PREFIX_PUSH(); + setup_numa_memblock(node_fractions); + + req_node_end =3D region_end(req_node); + min_addr =3D req_node_end - SZ_256; + max_addr =3D min_addr + size; + + allocated_ptr =3D memblock_alloc_exact_nid_raw(size, SMP_CACHE_BYTES, + min_addr, max_addr, + nid_req); + + ASSERT_NE(allocated_ptr, NULL); + ASSERT_MEM_NE(allocated_ptr, 0, size); + + ASSERT_EQ(new_rgn->size, size); + ASSERT_EQ(new_rgn->base, req_node->base); + ASSERT_LE(region_end(new_rgn), req_node_end); + + ASSERT_EQ(memblock.reserved.cnt, 1); + ASSERT_EQ(memblock.reserved.total_size, size); + + test_pass_pop(); + + return 0; +} + +/* + * A test that tries to allocate a memory region that spans over the min_a= ddr + * and max_addr range and overlaps with two different nodes, where the req= uested + * node ends before min_addr: + * + * min_addr + * | max_addr + * | | + * v v + * | +---------------+ +-------------+---------+ | + * | | requested | | node1 | node2 | | + * +----+---------------+--------+-------------+---------+---------+ + * + + + * | +---------+ | + * | | rgn | | + * +----+---------+------------------------------------------------+ + * + * Expect to drop the lower limit and allocate a memory region that starts= at + * the beginning of the requested node. + */ +static int alloc_exact_nid_bottom_up_numa_no_overlap_split_check(void) +{ + int nid_req =3D 2; + struct memblock_region *new_rgn =3D &memblock.reserved.regions[0]; + struct memblock_region *req_node =3D &memblock.memory.regions[nid_req]; + struct memblock_region *node2 =3D &memblock.memory.regions[6]; + void *allocated_ptr =3D NULL; + phys_addr_t size; + phys_addr_t min_addr; + phys_addr_t max_addr; + + PREFIX_PUSH(); + setup_numa_memblock(node_fractions); + + size =3D SZ_512; + min_addr =3D node2->base - SZ_256; + max_addr =3D min_addr + size; + + allocated_ptr =3D memblock_alloc_exact_nid_raw(size, SMP_CACHE_BYTES, + min_addr, max_addr, + nid_req); + + ASSERT_NE(allocated_ptr, NULL); + ASSERT_MEM_NE(allocated_ptr, 0, size); + + ASSERT_EQ(new_rgn->size, size); + ASSERT_EQ(new_rgn->base, req_node->base); + ASSERT_LE(region_end(new_rgn), region_end(req_node)); + + ASSERT_EQ(memblock.reserved.cnt, 1); + ASSERT_EQ(memblock.reserved.total_size, size); + + test_pass_pop(); + + return 0; +} + +/* + * A test that tries to allocate memory within min_addr and max_add range = when + * the requested node and the range do not overlap, and requested node ends + * before min_addr. The range overlaps with multiple nodes along node + * boundaries: + * + * min_addr + * | max_addr + * | | + * v v + * |-----------+ +----------+----...----+----------+ | + * | requested | | min node | ... | max node | | + * +-----------+-----------+----------+----...----+----------+------+ + * + + + * |-----+ | + * | rgn | | + * +-----+----------------------------------------------------------+ + * + * Expect to drop the lower limit and allocate a memory region that starts= at + * the beginning of the requested node. + */ +static int alloc_exact_nid_bottom_up_numa_no_overlap_low_check(void) +{ + int nid_req =3D 0; + struct memblock_region *new_rgn =3D &memblock.reserved.regions[0]; + struct memblock_region *req_node =3D &memblock.memory.regions[nid_req]; + struct memblock_region *min_node =3D &memblock.memory.regions[2]; + struct memblock_region *max_node =3D &memblock.memory.regions[5]; + void *allocated_ptr =3D NULL; + phys_addr_t size =3D SZ_64; + phys_addr_t max_addr; + phys_addr_t min_addr; + + PREFIX_PUSH(); + setup_numa_memblock(node_fractions); + + min_addr =3D min_node->base; + max_addr =3D region_end(max_node); + + allocated_ptr =3D memblock_alloc_exact_nid_raw(size, SMP_CACHE_BYTES, + min_addr, max_addr, + nid_req); + + ASSERT_NE(allocated_ptr, NULL); + ASSERT_MEM_NE(allocated_ptr, 0, size); + + ASSERT_EQ(new_rgn->size, size); + ASSERT_EQ(new_rgn->base, req_node->base); + ASSERT_LE(region_end(new_rgn), region_end(req_node)); + + ASSERT_EQ(memblock.reserved.cnt, 1); + ASSERT_EQ(memblock.reserved.total_size, size); + + test_pass_pop(); + + return 0; +} + /* Test case wrappers for NUMA tests */ static int alloc_exact_nid_numa_simple_check(void) { test_print("\tRunning %s...\n", __func__); memblock_set_bottom_up(false); alloc_exact_nid_top_down_numa_simple_check(); + memblock_set_bottom_up(true); + alloc_exact_nid_bottom_up_numa_simple_check(); =20 return 0; } @@ -1489,6 +1763,8 @@ static int alloc_exact_nid_numa_part_reserved_check(v= oid) test_print("\tRunning %s...\n", __func__); memblock_set_bottom_up(false); alloc_exact_nid_top_down_numa_part_reserved_check(); + memblock_set_bottom_up(true); + alloc_exact_nid_bottom_up_numa_part_reserved_check(); =20 return 0; } @@ -1498,6 +1774,8 @@ static int alloc_exact_nid_numa_split_range_low_check= (void) test_print("\tRunning %s...\n", __func__); memblock_set_bottom_up(false); alloc_exact_nid_top_down_numa_split_range_low_check(); + memblock_set_bottom_up(true); + alloc_exact_nid_bottom_up_numa_split_range_low_check(); =20 return 0; } @@ -1507,6 +1785,8 @@ static int alloc_exact_nid_numa_no_overlap_split_chec= k(void) test_print("\tRunning %s...\n", __func__); memblock_set_bottom_up(false); alloc_exact_nid_top_down_numa_no_overlap_split_check(); + memblock_set_bottom_up(true); + alloc_exact_nid_bottom_up_numa_no_overlap_split_check(); =20 return 0; } @@ -1516,6 +1796,8 @@ static int alloc_exact_nid_numa_no_overlap_low_check(= void) test_print("\tRunning %s...\n", __func__); memblock_set_bottom_up(false); alloc_exact_nid_top_down_numa_no_overlap_low_check(); + memblock_set_bottom_up(true); + alloc_exact_nid_bottom_up_numa_no_overlap_low_check(); =20 return 0; } --=20 2.25.1 From nobody Wed Apr 8 10:33:11 2026 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 9380EC433F5 for ; Sun, 2 Oct 2022 23:42:21 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230062AbiJBXmS (ORCPT ); Sun, 2 Oct 2022 19:42:18 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:59176 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229913AbiJBXmI (ORCPT ); Sun, 2 Oct 2022 19:42:08 -0400 Received: from mail-pg1-x543.google.com (mail-pg1-x543.google.com [IPv6:2607:f8b0:4864:20::543]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id D035520BC6 for ; Sun, 2 Oct 2022 16:42:05 -0700 (PDT) Received: by mail-pg1-x543.google.com with SMTP id 129so8403758pgc.5 for ; Sun, 02 Oct 2022 16:42:05 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date; bh=AnSl/+Yx5Yy04UR1QvyY6oxDRVK8JRL9AgwGNDJ8kEQ=; b=jwwzWqIGAyHwUapB5rxOX0kfa/pdgkOHHNuEPirz/YxGlAmJQguzRFdQmI6A8B1qME gL4ODbBbRhUTrgDsAyfCpekvUuvlS3VJgzX1vXwHjjg4qFjlKVlwWDVabhGWIJ5rVUav V501zOjzYzQC47H/qbZpELX+o6TCUoi7CkjKQ3OwrmggDCPtkyZERwJ+7uMhAnYCcCE6 0fOavEzsc+0zVnwL4air6HEhyLiKMImVizM//S35ijJq8sLeTFKyfQjPTZ4wi71+xB5d X5h3VD6D4GS5eeKA1PQFAcRPbJ9HRSErXBibTv/KrDXNxkx+028jWWawAZ0lz11Ua8nr z0ug== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date; bh=AnSl/+Yx5Yy04UR1QvyY6oxDRVK8JRL9AgwGNDJ8kEQ=; b=7NGa2itk2cG6EDxYJ9zUxUEu8LAmzC4QBxQA90dPV38pwYSJBs1OqUJVsoDCciDLZX 9ZNRekZDL3Am5on9Ei/Dz8q0f6MoNqZmwwmYgOQdQJF7K2LQDRXEL2JRuO8UDHa+m6ro 9cK6DCzTPkd785Zg0C80Jy21g6jv1Xu/lgmZqTxjIcKXynIRL6i7GHO8OI6/ZLuvsIX0 9wah3kjC9msLQ08Wblti8y0/JUlK3/c2ZcLBGNKJmsWckC73A3r9doJnZLRt85RXBhsT RH7goWYGAeUPJylLpfL6wW+HzV+Dft+EgyC8bdmPagl56jUpz7YlRtSmSiDGdbZuylM1 Vsrw== X-Gm-Message-State: ACrzQf0djGnLDTYzATsS+Vce0IL+gIF7Y2QdR/nuay+mW/GDSJxUGF/Y Y0BT1vz8LRRdn756nZXnIIo= X-Google-Smtp-Source: AMsMyM5Fh8Qh6xigI/cuascMk8n0FvtIbQlGb8YNOdVghJZnfReIZ34ZSt5bZvnFIS/jiAT9lmgTFg== X-Received: by 2002:a63:f349:0:b0:43a:b82b:1173 with SMTP id t9-20020a63f349000000b0043ab82b1173mr16840101pgj.534.1664754124950; Sun, 02 Oct 2022 16:42:04 -0700 (PDT) Received: from sophie ([89.46.114.181]) by smtp.gmail.com with ESMTPSA id p17-20020a170902e35100b00179988ca61bsm5808151plc.161.2022.10.02.16.42.04 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 02 Oct 2022 16:42:04 -0700 (PDT) From: Rebecca Mckeever To: Mike Rapoport , linux-mm@kvack.org, linux-kernel@vger.kernel.org Cc: David Hildenbrand , Rebecca Mckeever Subject: [PATCH v2 4/5] memblock tests: add generic NUMA tests for memblock_alloc_exact_nid_raw Date: Sun, 2 Oct 2022 18:41:37 -0500 Message-Id: X-Mailer: git-send-email 2.25.1 In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" Add tests for memblock_alloc_exact_nid_raw() where the simulated physical memory is set up with multiple NUMA nodes. Additionally, all but one of these tests set nid !=3D NUMA_NO_NODE. All tests are run for both top-down and bottom-up allocation directions. The tested scenarios are: Range unrestricted: - region cannot be allocated: + there are no previously reserved regions, but requested node is too small + the requested node is fully reserved + the requested node is partially reserved and does not have enough space + none of the nodes have enough memory to allocate the region Range restricted: - region can be allocated in the specific node requested without dropping min_addr: + the range fully overlaps with the node, and there are adjacent reserved regions - region cannot be allocated: + range partially overlaps with two different nodes, where the second node is the requested node + range overlaps with multiple nodes along node boundaries, and the requested node starts after max_addr + nid is set to NUMA_NO_NODE and the total range can fit the region, but the range is split between two nodes and everything else is reserved Signed-off-by: Rebecca Mckeever --- .../memblock/tests/alloc_exact_nid_api.c | 465 ++++++++++++++++++ 1 file changed, 465 insertions(+) diff --git a/tools/testing/memblock/tests/alloc_exact_nid_api.c b/tools/tes= ting/memblock/tests/alloc_exact_nid_api.c index 63279d1e35b1..0f9a84867207 100644 --- a/tools/testing/memblock/tests/alloc_exact_nid_api.c +++ b/tools/testing/memblock/tests/alloc_exact_nid_api.c @@ -1746,6 +1746,390 @@ static int alloc_exact_nid_bottom_up_numa_no_overla= p_low_check(void) return 0; } =20 +/* + * A test that tries to allocate a memory region in a specific NUMA node t= hat + * does not have enough memory to allocate a region of the requested size: + * + * | +-----+ | + * | | req | | + * +---+-----+----------------------------+ + * + * +---------+ + * | rgn | + * +---------+ + * + * Expect no allocation to happen. + */ +static int alloc_exact_nid_numa_small_node_generic_check(void) +{ + int nid_req =3D 1; + struct memblock_region *req_node =3D &memblock.memory.regions[nid_req]; + void *allocated_ptr =3D NULL; + phys_addr_t size; + phys_addr_t min_addr; + phys_addr_t max_addr; + + PREFIX_PUSH(); + setup_numa_memblock(node_fractions); + + size =3D SZ_2 * req_node->size; + min_addr =3D memblock_start_of_DRAM(); + max_addr =3D memblock_end_of_DRAM(); + + allocated_ptr =3D memblock_alloc_exact_nid_raw(size, SMP_CACHE_BYTES, + min_addr, max_addr, + nid_req); + + ASSERT_EQ(allocated_ptr, NULL); + + test_pass_pop(); + + return 0; +} + +/* + * A test that tries to allocate a memory region in a specific NUMA node t= hat + * is fully reserved: + * + * | +---------+ | + * | |requested| | + * +--------------+---------+-------------+ + * + * | +---------+ | + * | | reserved| | + * +--------------+---------+-------------+ + * + * Expect no allocation to happen. + */ +static int alloc_exact_nid_numa_node_reserved_generic_check(void) +{ + int nid_req =3D 2; + struct memblock_region *req_node =3D &memblock.memory.regions[nid_req]; + void *allocated_ptr =3D NULL; + phys_addr_t size; + phys_addr_t min_addr; + phys_addr_t max_addr; + + PREFIX_PUSH(); + setup_numa_memblock(node_fractions); + + size =3D req_node->size; + min_addr =3D memblock_start_of_DRAM(); + max_addr =3D memblock_end_of_DRAM(); + + memblock_reserve(req_node->base, req_node->size); + allocated_ptr =3D memblock_alloc_exact_nid_raw(size, SMP_CACHE_BYTES, + min_addr, max_addr, + nid_req); + + ASSERT_EQ(allocated_ptr, NULL); + + test_pass_pop(); + + return 0; +} + +/* + * A test that tries to allocate a memory region in a specific NUMA node t= hat + * is partially reserved and does not have enough contiguous memory for the + * allocated region: + * + * | +-----------------------+ | + * | | requested | | + * +-----------+-----------------------+----+ + * + * | +----------+ | + * | | reserved | | + * +-----------------+----------+-----------+ + * + * Expect no allocation to happen. + */ +static int alloc_exact_nid_numa_part_reserved_fail_generic_check(void) +{ + int nid_req =3D 4; + struct memblock_region *req_node =3D &memblock.memory.regions[nid_req]; + void *allocated_ptr =3D NULL; + struct region r1; + phys_addr_t size; + phys_addr_t min_addr; + phys_addr_t max_addr; + + PREFIX_PUSH(); + setup_numa_memblock(node_fractions); + + ASSERT_LE(SZ_4, req_node->size); + size =3D req_node->size / SZ_2; + r1.base =3D req_node->base + (size / SZ_2); + r1.size =3D size; + + min_addr =3D memblock_start_of_DRAM(); + max_addr =3D memblock_end_of_DRAM(); + + memblock_reserve(r1.base, r1.size); + allocated_ptr =3D memblock_alloc_exact_nid_raw(size, SMP_CACHE_BYTES, + min_addr, max_addr, + nid_req); + + ASSERT_EQ(allocated_ptr, NULL); + + test_pass_pop(); + + return 0; +} + +/* + * A test that tries to allocate a memory region that spans over the min_a= ddr + * and max_addr range and overlaps with two different nodes, where the sec= ond + * node is the requested node: + * + * min_addr + * | max_addr + * | | + * v v + * | +--------------------------+---------+ | + * | | first node |requested| | + * +------+--------------------------+---------+----------------+ + * + * Expect no allocation to happen. + */ +static int alloc_exact_nid_numa_split_range_high_generic_check(void) +{ + int nid_req =3D 3; + struct memblock_region *req_node =3D &memblock.memory.regions[nid_req]; + void *allocated_ptr =3D NULL; + phys_addr_t size =3D SZ_512; + phys_addr_t min_addr; + phys_addr_t max_addr; + + PREFIX_PUSH(); + setup_numa_memblock(node_fractions); + + min_addr =3D req_node->base - SZ_256; + max_addr =3D min_addr + size; + + allocated_ptr =3D memblock_alloc_exact_nid_raw(size, SMP_CACHE_BYTES, + min_addr, max_addr, + nid_req); + + ASSERT_EQ(allocated_ptr, NULL); + + test_pass_pop(); + + return 0; +} + +/* + * A test that tries to allocate memory within min_addr and max_add range = when + * the requested node and the range do not overlap, and requested node sta= rts + * after max_addr. The range overlaps with multiple nodes along node + * boundaries: + * + * min_addr + * | max_addr + * | | + * v v + * | +----------+----...----+----------+ +-----------+ | + * | | min node | ... | max node | | requested | | + * +-----+----------+----...----+----------+--------+-----------+---+ + * + * Expect no allocation to happen. + */ +static int alloc_exact_nid_numa_no_overlap_high_generic_check(void) +{ + int nid_req =3D 7; + struct memblock_region *min_node =3D &memblock.memory.regions[2]; + struct memblock_region *max_node =3D &memblock.memory.regions[5]; + void *allocated_ptr =3D NULL; + phys_addr_t size =3D SZ_64; + phys_addr_t max_addr; + phys_addr_t min_addr; + + PREFIX_PUSH(); + setup_numa_memblock(node_fractions); + + min_addr =3D min_node->base; + max_addr =3D region_end(max_node); + + allocated_ptr =3D memblock_alloc_exact_nid_raw(size, SMP_CACHE_BYTES, + min_addr, max_addr, + nid_req); + + ASSERT_EQ(allocated_ptr, NULL); + + test_pass_pop(); + + return 0; +} + +/* + * A test that tries to allocate a memory region in a specific NUMA node t= hat + * does not have enough memory to allocate a region of the requested size. + * Additionally, none of the nodes have enough memory to allocate the regi= on: + * + * +-----------------------------------+ + * | new | + * +-----------------------------------+ + * |-------+-------+-------+-------+-------+-------+-------+-------| + * | node0 | node1 | node2 | node3 | node4 | node5 | node6 | node7 | + * +-------+-------+-------+-------+-------+-------+-------+-------+ + * + * Expect no allocation to happen. + */ +static int alloc_exact_nid_numa_large_region_generic_check(void) +{ + int nid_req =3D 3; + void *allocated_ptr =3D NULL; + phys_addr_t size =3D MEM_SIZE / SZ_2; + phys_addr_t min_addr; + phys_addr_t max_addr; + + PREFIX_PUSH(); + setup_numa_memblock(node_fractions); + + min_addr =3D memblock_start_of_DRAM(); + max_addr =3D memblock_end_of_DRAM(); + + allocated_ptr =3D memblock_alloc_exact_nid_raw(size, SMP_CACHE_BYTES, + min_addr, max_addr, + nid_req); + ASSERT_EQ(allocated_ptr, NULL); + + test_pass_pop(); + + return 0; +} + +/* + * A test that tries to allocate memory within min_addr and max_addr range= when + * there are two reserved regions at the borders. The requested node start= s at + * min_addr and ends at max_addr and is the same size as the region to be + * allocated: + * + * min_addr + * | max_addr + * | | + * v v + * | +-----------+-----------------------+-----------------------| + * | | node5 | requested | node7 | + * +------+-----------+-----------------------+-----------------------+ + * + + + * | +----+-----------------------+----+ | + * | | r2 | new | r1 | | + * +-------------+----+-----------------------+----+------------------+ + * + * Expect to merge all of the regions into one. The region counter and tot= al + * size fields get updated. + */ +static int alloc_exact_nid_numa_reserved_full_merge_generic_check(void) +{ + int nid_req =3D 6; + int nid_next =3D nid_req + 1; + struct memblock_region *new_rgn =3D &memblock.reserved.regions[0]; + struct memblock_region *req_node =3D &memblock.memory.regions[nid_req]; + struct memblock_region *next_node =3D &memblock.memory.regions[nid_next]; + void *allocated_ptr =3D NULL; + struct region r1, r2; + phys_addr_t size =3D req_node->size; + phys_addr_t total_size; + phys_addr_t max_addr; + phys_addr_t min_addr; + + PREFIX_PUSH(); + setup_numa_memblock(node_fractions); + + r1.base =3D next_node->base; + r1.size =3D SZ_128; + + r2.size =3D SZ_128; + r2.base =3D r1.base - (size + r2.size); + + total_size =3D r1.size + r2.size + size; + min_addr =3D r2.base + r2.size; + max_addr =3D r1.base; + + memblock_reserve(r1.base, r1.size); + memblock_reserve(r2.base, r2.size); + + allocated_ptr =3D memblock_alloc_exact_nid_raw(size, SMP_CACHE_BYTES, + min_addr, max_addr, + nid_req); + + ASSERT_NE(allocated_ptr, NULL); + ASSERT_MEM_NE(allocated_ptr, 0, size); + + ASSERT_EQ(new_rgn->size, total_size); + ASSERT_EQ(new_rgn->base, r2.base); + + ASSERT_LE(new_rgn->base, req_node->base); + ASSERT_LE(region_end(req_node), region_end(new_rgn)); + + ASSERT_EQ(memblock.reserved.cnt, 1); + ASSERT_EQ(memblock.reserved.total_size, total_size); + + test_pass_pop(); + + return 0; +} + +/* + * A test that tries to allocate memory within min_addr and max_add range, + * where the total range can fit the region, but it is split between two n= odes + * and everything else is reserved. Additionally, nid is set to NUMA_NO_NO= DE + * instead of requesting a specific node: + * + * +-----------+ + * | new | + * +-----------+ + * | +---------------------+-----------| + * | | prev node | next node | + * +------+---------------------+-----------+ + * + + + * |----------------------+ +-----| + * | r1 | | r2 | + * +----------------------+-----------+-----+ + * ^ ^ + * | | + * | max_addr + * | + * min_addr + * + * Expect no allocation to happen. + */ +static int alloc_exact_nid_numa_split_all_reserved_generic_check(void) +{ + void *allocated_ptr =3D NULL; + struct memblock_region *next_node =3D &memblock.memory.regions[7]; + struct region r1, r2; + phys_addr_t size =3D SZ_256; + phys_addr_t max_addr; + phys_addr_t min_addr; + + PREFIX_PUSH(); + setup_numa_memblock(node_fractions); + + r2.base =3D next_node->base + SZ_128; + r2.size =3D memblock_end_of_DRAM() - r2.base; + + r1.size =3D MEM_SIZE - (r2.size + size); + r1.base =3D memblock_start_of_DRAM(); + + min_addr =3D r1.base + r1.size; + max_addr =3D r2.base; + + memblock_reserve(r1.base, r1.size); + memblock_reserve(r2.base, r2.size); + + allocated_ptr =3D memblock_alloc_exact_nid_raw(size, SMP_CACHE_BYTES, + min_addr, max_addr, + NUMA_NO_NODE); + + ASSERT_EQ(allocated_ptr, NULL); + + test_pass_pop(); + + return 0; +} + /* Test case wrappers for NUMA tests */ static int alloc_exact_nid_numa_simple_check(void) { @@ -1802,6 +2186,78 @@ static int alloc_exact_nid_numa_no_overlap_low_check= (void) return 0; } =20 +static int alloc_exact_nid_numa_small_node_check(void) +{ + test_print("\tRunning %s...\n", __func__); + run_top_down(alloc_exact_nid_numa_small_node_generic_check); + run_bottom_up(alloc_exact_nid_numa_small_node_generic_check); + + return 0; +} + +static int alloc_exact_nid_numa_node_reserved_check(void) +{ + test_print("\tRunning %s...\n", __func__); + run_top_down(alloc_exact_nid_numa_node_reserved_generic_check); + run_bottom_up(alloc_exact_nid_numa_node_reserved_generic_check); + + return 0; +} + +static int alloc_exact_nid_numa_part_reserved_fail_check(void) +{ + test_print("\tRunning %s...\n", __func__); + run_top_down(alloc_exact_nid_numa_part_reserved_fail_generic_check); + run_bottom_up(alloc_exact_nid_numa_part_reserved_fail_generic_check); + + return 0; +} + +static int alloc_exact_nid_numa_split_range_high_check(void) +{ + test_print("\tRunning %s...\n", __func__); + run_top_down(alloc_exact_nid_numa_split_range_high_generic_check); + run_bottom_up(alloc_exact_nid_numa_split_range_high_generic_check); + + return 0; +} + +static int alloc_exact_nid_numa_no_overlap_high_check(void) +{ + test_print("\tRunning %s...\n", __func__); + run_top_down(alloc_exact_nid_numa_no_overlap_high_generic_check); + run_bottom_up(alloc_exact_nid_numa_no_overlap_high_generic_check); + + return 0; +} + +static int alloc_exact_nid_numa_large_region_check(void) +{ + test_print("\tRunning %s...\n", __func__); + run_top_down(alloc_exact_nid_numa_large_region_generic_check); + run_bottom_up(alloc_exact_nid_numa_large_region_generic_check); + + return 0; +} + +static int alloc_exact_nid_numa_reserved_full_merge_check(void) +{ + test_print("\tRunning %s...\n", __func__); + run_top_down(alloc_exact_nid_numa_reserved_full_merge_generic_check); + run_bottom_up(alloc_exact_nid_numa_reserved_full_merge_generic_check); + + return 0; +} + +static int alloc_exact_nid_numa_split_all_reserved_check(void) +{ + test_print("\tRunning %s...\n", __func__); + run_top_down(alloc_exact_nid_numa_split_all_reserved_generic_check); + run_bottom_up(alloc_exact_nid_numa_split_all_reserved_generic_check); + + return 0; +} + int __memblock_alloc_exact_nid_numa_checks(void) { test_print("Running %s NUMA tests...\n", FUNC_NAME); @@ -1812,6 +2268,15 @@ int __memblock_alloc_exact_nid_numa_checks(void) alloc_exact_nid_numa_no_overlap_split_check(); alloc_exact_nid_numa_no_overlap_low_check(); =20 + alloc_exact_nid_numa_small_node_check(); + alloc_exact_nid_numa_node_reserved_check(); + alloc_exact_nid_numa_part_reserved_fail_check(); + alloc_exact_nid_numa_split_range_high_check(); + alloc_exact_nid_numa_no_overlap_high_check(); + alloc_exact_nid_numa_large_region_check(); + alloc_exact_nid_numa_reserved_full_merge_check(); + alloc_exact_nid_numa_split_all_reserved_check(); + return 0; } =20 --=20 2.25.1 From nobody Wed Apr 8 10:33:11 2026 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 21C17C433F5 for ; Sun, 2 Oct 2022 23:42:25 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230175AbiJBXmW (ORCPT ); Sun, 2 Oct 2022 19:42:22 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:59266 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230019AbiJBXmI (ORCPT ); Sun, 2 Oct 2022 19:42:08 -0400 Received: from mail-pg1-x543.google.com (mail-pg1-x543.google.com [IPv6:2607:f8b0:4864:20::543]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 9957B32D94 for ; Sun, 2 Oct 2022 16:42:07 -0700 (PDT) Received: by mail-pg1-x543.google.com with SMTP id b5so8397431pgb.6 for ; Sun, 02 Oct 2022 16:42:07 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date; bh=nBwF90pcIfh6GRwU6cpt0vmJWG9cDZH3z3WIrcLz5YE=; b=FaTigLz8IORgfJoMn5fUUHTGlPYGPvZu3GjLKbHoXG6p1xC0618KmmUqAet84dw7yj 1Jihh9K9koqMc2JL/sb3TkCkx0fjJ7IRFqBbQ5RJjK7hpPHU548inICnZvb3moNqKSYu gh8NqwOBe63qJIY2dV/F2CmpSgBYcaP/+9PEHpp7JLSbZPYLPMhKN1ZDOn1iGgqYVXNv U+rKaPL98HBuOSza6EBOKEHokW3UOb79EGzbbDDfTGdd+IQSAmguHefseFnOjO/tM5F6 /+R/d9ZpOdTCD+UiydTTo9r6COYTaEuP41SoRZ3RkPG9pgFvRkthZ13gYQ28Z53TI+BG COzQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date; bh=nBwF90pcIfh6GRwU6cpt0vmJWG9cDZH3z3WIrcLz5YE=; b=lH2oeYfXM/miKSy56/Fi/eVZlFIXfLGSldJjy9NB0doHA5hSwA6WxNhc7Kciqwtbgt TwJHBB1tDO46Mxf2sHJ47fPpFuJ/f7AO+zKd6tJOk6L0lRxvas1vrJqKm3GwudY1gbgE mimMWlMJEd7H/C/JbdWTwTIDuOoLZob40IU1tqh1iA7bghRcKGn5kgTYm2Mo8ZaZYtoN pGZCFaSlwlwg3CbXGlsv3AFfRrwh0YMNsvPZPZHGetu2K/Jan8k4PBFZbxJ1Cmhr19Ad XSSQ+QzymL1zwW8C3zUZ+rUKGOxAiSkxPxpIw+ZiG4DBh06K8lebQec+/oOn7knTvsPK 64Hw== X-Gm-Message-State: ACrzQf2A9y1mZBH2jfH3iZYCxoCwrmpLZhjPsIGKcZ3kRbZ1lk8is+Pu mOKcfa+eYUxqBCPUmTkSi4Q= X-Google-Smtp-Source: AMsMyM4Z59BYphjBrlwx/7tFU9vLgn/N4C9Sme77ZwrfoMDCLGo1MXPXgLXVjaSETJsidR8dTmigEQ== X-Received: by 2002:a63:1e21:0:b0:43a:b17f:cd13 with SMTP id e33-20020a631e21000000b0043ab17fcd13mr16481063pge.553.1664754126964; Sun, 02 Oct 2022 16:42:06 -0700 (PDT) Received: from sophie ([89.46.114.181]) by smtp.gmail.com with ESMTPSA id 21-20020a630a15000000b0042b291a89bfsm5308759pgk.11.2022.10.02.16.42.06 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 02 Oct 2022 16:42:06 -0700 (PDT) From: Rebecca Mckeever To: Mike Rapoport , linux-mm@kvack.org, linux-kernel@vger.kernel.org Cc: David Hildenbrand , Rebecca Mckeever Subject: [PATCH v2 5/5] memblock tests: remove completed TODO item Date: Sun, 2 Oct 2022 18:41:38 -0500 Message-Id: <5df0bb56e4f32ae2734b7a74c65530d84012b4de.1664753535.git.remckee0@gmail.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" Remove completed item from TODO list. Signed-off-by: Rebecca Mckeever Reviewed-by: David Hildenbrand --- tools/testing/memblock/TODO | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/tools/testing/memblock/TODO b/tools/testing/memblock/TODO index 33044c634ea7..cb7fac47a643 100644 --- a/tools/testing/memblock/TODO +++ b/tools/testing/memblock/TODO @@ -8,10 +8,5 @@ TODO likely that the current MEM_SIZE won't be enough for these test cases. Use realloc to adjust the size accordingly. =20 -2. Add test cases using this functions (implement them for both directions= ): - + memblock_alloc_raw() - + memblock_alloc_exact_nid_raw() - + memblock_alloc_try_nid_raw() - -3. Add tests for memblock_alloc_node() to check if the correct NUMA node i= s set +2. Add tests for memblock_alloc_node() to check if the correct NUMA node i= s set for the new region --=20 2.25.1