From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 9A075C433EF for ; Thu, 27 Jan 2022 13:23:00 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 279FA6B0098; Thu, 27 Jan 2022 08:23:00 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 1B51E6B0096; Thu, 27 Jan 2022 08:23:00 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 0062A6B0098; Thu, 27 Jan 2022 08:22:59 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0028.hostedemail.com [216.40.44.28]) by kanga.kvack.org (Postfix) with ESMTP id DDB0A6B0095 for ; Thu, 27 Jan 2022 08:22:59 -0500 (EST) Received: from smtpin16.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay03.hostedemail.com (Postfix) with ESMTP id 99CEC8249980 for ; Thu, 27 Jan 2022 13:22:59 +0000 (UTC) X-FDA: 79076132478.16.B4D83C9 Received: from mail-lj1-f175.google.com (mail-lj1-f175.google.com [209.85.208.175]) by imf21.hostedemail.com (Postfix) with ESMTP id 424111C0002 for ; Thu, 27 Jan 2022 13:22:59 +0000 (UTC) Received: by mail-lj1-f175.google.com with SMTP id q22so4283974ljh.7 for ; Thu, 27 Jan 2022 05:22:58 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=S1q+vnglpO2PDv2QmjtSXkjrsf77H3g2pU/xh3PtXsQ=; b=dBoiU1gV4FcN5h0v1+Ht5FHOoXQeAL25AFLxoz/HhI1aTyz+dzrdmjyM7KgAfIU5Kx +Oh+BJYNSfMgfqiBCxlizwUauGa9t1H5FoBokNBdRNGJTcPuBEYEb+1cM2wYoy++6f1b m64NXmz+zWb9MT8KE86VJUuJZLnCuetMwLTfXD/z6Ubfa+uEQFGUx1YFJN19tSuVzK6s IXIAT5iYtQbpXAseRn9OR7Hw/ktnnNS5UdBBZeZd1r9myqhRsu+DxZ0FP89/q855f8+c 66yZY6kGbao2rU8l6Kt4Ek2OMeIWkEggjjZhcri0DbF9a68H26pKvlIFqHfx6wb/ZFL9 xgMA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=S1q+vnglpO2PDv2QmjtSXkjrsf77H3g2pU/xh3PtXsQ=; b=vuLEFNUxFKAqdKZsAXvh6FfH7vuAHyEIduczAfdQw32Qb/dYIZEUDGg7am5kcFEJJl S6U50oM+RIFJnwLKGvW9q4sBcfxoq7NA2vvGUQ2aj0raoqOPzNYEfqYz6fFnxyWcqhio /IQ4CzfeghzN/JVDG8ctu/tA630IKSAP5AR9FtM9go/49KprDRvIlz1kCcVsUj0GX+fV jNn5SAZ38oE0qHygulz4v2e40DKwv/L8g9aa6rRbRK/mFZT2q64OIQ05lp9/x686lzs0 LmiPY7uGbRLICh9MGim05Aosb+4wPWYoFArD1ONWh4tnKiQzQyGTPkwQBuws6goBjBiR Jxpw== X-Gm-Message-State: AOAM5332LXQ3ZCHJYbVvd5MDB0AH8ledZ8ISH1BUGf6b/iETCiPqs3wD HeyRU7O2vn8cJ/F0a2c4+iy/idP9NJE= X-Google-Smtp-Source: ABdhPJyW21GLrmtKSmGHnLJmdJ5DP3VKdlko5bJTSPTxdviTYdSWzSMpaEY/E2uPRqOTTYtWphLnWg== X-Received: by 2002:a2e:8e98:: with SMTP id z24mr2842965ljk.19.1643289777871; Thu, 27 Jan 2022 05:22:57 -0800 (PST) Received: from elysium.toya.net.pl (staticline-31-183-166-172.toya.net.pl. [31.183.166.172]) by smtp.gmail.com with ESMTPSA id s16sm1306918lfp.197.2022.01.27.05.22.56 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 27 Jan 2022 05:22:57 -0800 (PST) From: Karolina Drobnik To: linux-mm@kvack.org Cc: akpm@linux-foundation.org, mike.rapoport@gmail.com, linux-kernel@vger.kernel.org, Karolina Drobnik Subject: [PATCH 12/16] memblock tests: Add memblock_add tests Date: Thu, 27 Jan 2022 14:21:30 +0100 Message-Id: <13745fec282f5c96d39faea7cac8e034abbbfd5c.1643206612.git.karolinadrobnik@gmail.com> X-Mailer: git-send-email 2.30.2 In-Reply-To: References: MIME-Version: 1.0 X-Rspamd-Server: rspam11 X-Rspamd-Queue-Id: 424111C0002 X-Stat-Signature: ra7hr7mjr4pwmrm4tjcpq116xpjsxa6s Authentication-Results: imf21.hostedemail.com; dkim=pass header.d=gmail.com header.s=20210112 header.b=dBoiU1gV; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (imf21.hostedemail.com: domain of karolinadrobnik@gmail.com designates 209.85.208.175 as permitted sender) smtp.mailfrom=karolinadrobnik@gmail.com X-Rspam-User: nil X-HE-Tag: 1643289779-460159 Content-Transfer-Encoding: quoted-printable X-Bogosity: Ham, tests=bogofilter, spamicity=0.000720, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: Add checks for adding a new region in different scenarios: - The region does not overlap with existing entries - The region overlaps with one of the previous entries: from the top (its end address is bigger than the base of the existing region) or from the bottom (its base address is smaller than the end address of one of the regions) - The region is within an already defined region - The same region is added twice to the collection of available memory regions Add checks for memblock initialization to verify it sets memblock data structures to expected values. Signed-off-by: Karolina Drobnik --- tools/testing/memblock/tests/basic_api.c | 215 +++++++++++++++++++++++ 1 file changed, 215 insertions(+) diff --git a/tools/testing/memblock/tests/basic_api.c b/tools/testing/mem= block/tests/basic_api.c index 7f2597b3dd4d..6c047b7b31ab 100644 --- a/tools/testing/memblock/tests/basic_api.c +++ b/tools/testing/memblock/tests/basic_api.c @@ -1,6 +1,7 @@ // SPDX-License-Identifier: GPL-2.0-or-later #include #include +#include #include "basic_api.h" =20 #define EXPECTED_MEMBLOCK_REGIONS 128 @@ -25,8 +26,222 @@ static int memblock_initialization_check(void) return 0; } =20 +/* + * A simple test that adds a memory block of a specified base address + * and size to the collection of available memory regions (memblock.memo= ry). + * It checks if a new entry was created and if region counter and total = memory + * were correctly updated. + */ +static int memblock_add_simple_check(void) +{ + struct memblock_region *rgn; + + rgn =3D &memblock.memory.regions[0]; + + struct region r =3D { + .base =3D SZ_1G, + .size =3D SZ_4M + }; + + reset_memblock(); + memblock_add(r.base, r.size); + + assert(rgn->base =3D=3D r.base); + assert(rgn->size =3D=3D r.size); + + assert(memblock.memory.cnt =3D=3D 1); + assert(memblock.memory.total_size =3D=3D r.size); + + return 0; +} + +/* + * A test that tries to add two memory blocks that don't overlap with on= e another. + * It checks if two correctly initialized entries were added to the coll= ection + * of available memory regions (memblock.memory) and if this change was = reflected + * in memblock.memory's total size and region counter. + */ +static int memblock_add_disjoint_check(void) +{ + struct memblock_region *rgn1, *rgn2; + + rgn1 =3D &memblock.memory.regions[0]; + rgn2 =3D &memblock.memory.regions[1]; + + struct region r1 =3D { + .base =3D SZ_1G, + .size =3D SZ_8K + }; + struct region r2 =3D { + .base =3D SZ_1G + SZ_16K, + .size =3D SZ_8K + }; + + reset_memblock(); + memblock_add(r1.base, r1.size); + memblock_add(r2.base, r2.size); + + assert(rgn1->base =3D=3D r1.base); + assert(rgn1->size =3D=3D r1.size); + + assert(rgn2->base =3D=3D r2.base); + assert(rgn2->size =3D=3D r2.size); + + assert(memblock.memory.cnt =3D=3D 2); + assert(memblock.memory.total_size =3D=3D r1.size + r2.size); + + return 0; +} + +/* + * A test that tries to add two memory blocks, where the second one over= laps + * with the beginning of the first entry (that is r1.base < r2.base + r2= .size). + * After this, it checks if two entries are merged into one region that = starts + * at r2.base and has size of two regions minus their intersection. It a= lso + * verifies the reported total size of the available memory and region c= ounter. + */ +static int memblock_add_overlap_top_check(void) +{ + struct memblock_region *rgn; + phys_addr_t total_size; + + rgn =3D &memblock.memory.regions[0]; + + struct region r1 =3D { + .base =3D SZ_512M, + .size =3D SZ_1G + }; + struct region r2 =3D { + .base =3D SZ_256M, + .size =3D SZ_512M + }; + + total_size =3D (r1.base - r2.base) + r1.size; + + reset_memblock(); + memblock_add(r1.base, r1.size); + memblock_add(r2.base, r2.size); + + assert(rgn->base =3D=3D r2.base); + assert(rgn->size =3D=3D total_size); + + assert(memblock.memory.cnt =3D=3D 1); + assert(memblock.memory.total_size =3D=3D total_size); + + return 0; +} + +/* + * A test that tries to add two memory blocks, where the second one over= laps + * with the end of the first entry (that is r2.base < r1.base + r1.size)= . + * After this, it checks if two entries are merged into one region that = starts + * at r1.base and has size of two regions minus their intersection. It v= erifies + * that memblock can still see only one entry and has a correct total si= ze of + * the available memory. + */ +static int memblock_add_overlap_bottom_check(void) +{ + struct memblock_region *rgn; + phys_addr_t total_size; + + rgn =3D &memblock.memory.regions[0]; + + struct region r1 =3D { + .base =3D SZ_128M, + .size =3D SZ_512M + }; + struct region r2 =3D { + .base =3D SZ_256M, + .size =3D SZ_1G + }; + + total_size =3D (r2.base - r1.base) + r2.size; + + reset_memblock(); + memblock_add(r1.base, r1.size); + memblock_add(r2.base, r2.size); + + assert(rgn->base =3D=3D r1.base); + assert(rgn->size =3D=3D total_size); + + assert(memblock.memory.cnt =3D=3D 1); + assert(memblock.memory.total_size =3D=3D total_size); + + return 0; +} + +/* + * A test that tries to add two memory blocks, where the second one is + * within the range of the first entry (that is r1.base < r2.base && + * r2.base + r2.size < r1.base + r1.size). It checks if two entries are = merged + * into one region that stays the same. The counter and total size of av= ailable + * memory are expected to not be updated. + */ +static int memblock_add_within_check(void) +{ + struct memblock_region *rgn; + + rgn =3D &memblock.memory.regions[0]; + + struct region r1 =3D { + .base =3D SZ_8M, + .size =3D SZ_32M + }; + struct region r2 =3D { + .base =3D SZ_16M, + .size =3D SZ_1M + }; + + reset_memblock(); + memblock_add(r1.base, r1.size); + memblock_add(r2.base, r2.size); + + assert(rgn->base =3D=3D r1.base); + assert(rgn->size =3D=3D r1.size); + + assert(memblock.memory.cnt =3D=3D 1); + assert(memblock.memory.total_size =3D=3D r1.size); + + return 0; +} + +/* + * A simple test that tries to add the same memory block twice. The coun= ter + * and total size of available memory are expected to not be updated. + */ +static int memblock_add_twice_check(void) +{ + struct region r =3D { + .base =3D SZ_16K, + .size =3D SZ_2M + }; + + reset_memblock(); + + memblock_add(r.base, r.size); + memblock_add(r.base, r.size); + + assert(memblock.memory.cnt =3D=3D 1); + assert(memblock.memory.total_size =3D=3D r.size); + + return 0; +} + +static int memblock_add_checks(void) +{ + memblock_add_simple_check(); + memblock_add_disjoint_check(); + memblock_add_overlap_top_check(); + memblock_add_overlap_bottom_check(); + memblock_add_within_check(); + memblock_add_twice_check(); + + return 0; +} + int memblock_basic_checks(void) { memblock_initialization_check(); + memblock_add_checks(); return 0; } --=20 2.30.2