From patchwork Thu Feb 20 09:56:49 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sughosh Ganu X-Patchwork-Id: 866735 Delivered-To: patch@linaro.org Received: by 2002:a05:6000:1f85:b0:38f:210b:807b with SMTP id hj5csp115340wrb; Thu, 20 Feb 2025 01:57:36 -0800 (PST) X-Forwarded-Encrypted: i=2; AJvYcCVLIKUSFxhpgiAecIlCod59vqtm6qqdq1T8/sIun6MnI2s3K7DIvXJhI0f8zlSGR4Iy+XD7uA==@linaro.org X-Google-Smtp-Source: AGHT+IECh/g0XDMTMVQ9c7OlTLKOp3zlF+C/ETMJUfI86PkVjklaxIJFizI/9ezAXoH9PylTvfW6 X-Received: by 2002:a05:6000:1fa5:b0:38d:d69e:1314 with SMTP id ffacd0b85a97d-38f58781302mr6609078f8f.9.1740045456469; Thu, 20 Feb 2025 01:57:36 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1740045456; cv=none; d=google.com; s=arc-20240605; b=hurUTJkI5WIeb0kMapqdzYFV7Xvt0JU+NJRqtHteEhsAVSEN0e06q81uodBCeq/Jtw V0N5vUYc2W+P2lufWJIOOxJRMGJj7r3kE980iX9H5uhoCwVn/s3HuKmx+eMkqANq5ys5 efv5lNo/UDXeyyIsSY9+oBEuiQ1d8UwErcTrsn/P0pX0heFFR5K8+R7dO2MGy4ien98V Fk5RbYMdJK2y2gxiHsk7qMO3eJfwbxnJBZEH7H5DsUi9yYDoDRswdu6Qnbrqtp3Siso2 K3DcZizmdCguJMtFV3vGTt9kv9vov2hqp9Xran45IJHXOGgr0CK7xTaACrZrm/TJ2bQa C3TA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20240605; h=sender:errors-to:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:content-transfer-encoding :mime-version:references:in-reply-to:message-id:date:subject:cc:to :from; bh=Xz0VHkkxJ4tnjis5pGf0VVck5W4bDad1UAw6+r8D79A=; fh=2HGKihHUAicyV3F+aHiaoPpwvqWA0zlC04Ew6QaQoEg=; b=L5ePuMJJ0nLsKge/cxKPbZRudJnLdIfUZOwGt8bmGxqUNamsH6A5UPRCkZQAk9Qh9/ ocJuZQ1v0Y66274PzWAJ+/slLoxEIi8FDeSKiGu7WtlBr+JD2LRiyKgJ7TlUeBs48942 czmg0Z0X8FKeHnSh+gH2qcMuJqwiJ+qVBxGz17F5JHyDrJJLCTQO4pU5LNlMYNaeyJDe 6heO2DCxnH42diLf7dk0Vf3dqxiY/KU9nPfF7W3rY4mNxj1aq12PmxzreLVZHbu99PS9 VMPbCFAb2XJOmx/YnNZ2dZzSehrLWYpH4HbvUuxmxhDYM8AX7tJ6rhK5gI2MnBgAr/lW leaA==; dara=google.com ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of u-boot-bounces@lists.denx.de designates 85.214.62.61 as permitted sender) smtp.mailfrom=u-boot-bounces@lists.denx.de; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from phobos.denx.de (phobos.denx.de. [85.214.62.61]) by mx.google.com with ESMTPS id 5b1f17b1804b1-439818c0fe5si63168065e9.33.2025.02.20.01.57.36 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 20 Feb 2025 01:57:36 -0800 (PST) Received-SPF: pass (google.com: domain of u-boot-bounces@lists.denx.de designates 85.214.62.61 as permitted sender) client-ip=85.214.62.61; Authentication-Results: mx.google.com; spf=pass (google.com: domain of u-boot-bounces@lists.denx.de designates 85.214.62.61 as permitted sender) smtp.mailfrom=u-boot-bounces@lists.denx.de; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: from h2850616.stratoserver.net (localhost [IPv6:::1]) by phobos.denx.de (Postfix) with ESMTP id F08F280F5C; Thu, 20 Feb 2025 10:57:29 +0100 (CET) Authentication-Results: phobos.denx.de; dmarc=fail (p=none dis=none) header.from=linaro.org Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=u-boot-bounces@lists.denx.de Received: by phobos.denx.de (Postfix, from userid 109) id 8900680F56; Thu, 20 Feb 2025 10:57:28 +0100 (CET) X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on phobos.denx.de X-Spam-Level: X-Spam-Status: No, score=-1.2 required=5.0 tests=BAYES_00, RCVD_IN_DNSWL_BLOCKED,RCVD_IN_VALIDITY_CERTIFIED_BLOCKED, RCVD_IN_VALIDITY_RPBL_BLOCKED,SPF_HELO_NONE,SPF_SOFTFAIL autolearn=no autolearn_force=no version=3.4.2 Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by phobos.denx.de (Postfix) with ESMTP id 2427180077 for ; Thu, 20 Feb 2025 10:57:26 +0100 (CET) Authentication-Results: phobos.denx.de; dmarc=fail (p=none dis=none) header.from=linaro.org Authentication-Results: phobos.denx.de; spf=fail smtp.mailfrom=sughosh.ganu@linaro.org Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.121.207.14]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id B4A181BB0; Thu, 20 Feb 2025 01:57:43 -0800 (PST) Received: from a079122.blr.arm.com (a079122.arm.com [10.162.17.48]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id 011C43F59E; Thu, 20 Feb 2025 01:57:22 -0800 (PST) From: Sughosh Ganu To: u-boot@lists.denx.de Cc: Ilias Apalodimas , Simon Glass , Tom Rini , Quentin Schulz , Udit Kumar , Heinrich Schuchardt , Sughosh Ganu Subject: [PATCH v2 1/6] lmb: check if a region can be reserved by lmb_reserve() Date: Thu, 20 Feb 2025 15:26:49 +0530 Message-Id: <20250220095654.121634-2-sughosh.ganu@linaro.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20250220095654.121634-1-sughosh.ganu@linaro.org> References: <20250220095654.121634-1-sughosh.ganu@linaro.org> MIME-Version: 1.0 X-BeenThere: u-boot@lists.denx.de X-Mailman-Version: 2.1.39 Precedence: list List-Id: U-Boot discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: u-boot-bounces@lists.denx.de Sender: "U-Boot" X-Virus-Scanned: clamav-milter 0.103.8 at phobos.denx.de X-Virus-Status: Clean The logic used in lmb_alloc() takes into consideration the existing reserved regions, and ensures that the allocated region does not overlap with any existing allocated regions. The lmb_reserve() function is not doing any such checks -- the requested region might overlap with an existing region. This also shows up with lmb_alloc_addr() as this function ends up calling lmb_reserve(). Add a function which checks if the region requested is overlapping with an existing reserved region, and allow for the reservation to happen only if both the regions have LMB_NONE flag, which allows re-requesting of the region. In any other scenario of an overlap, have lmb_reserve() return -EEXIST, implying that the requested region is already reserved. Add corresponding test cases which check for overlapping reservation requests made through lmb_reserve() and lmb_alloc_addr(). And while here, fix some of the comments in the test function being touched. Signed-off-by: Sughosh Ganu Reviewed-by: Heinrich Schuchardt --- Changes since V1: * Add documentation for the lmb_can_reserve_region() function. * Add an additional test case where a new region is being added, and the new region overlaps two different existing regions, with mismatching flags. * Some cleanup of comments in the lib_test_lmb_overlapping_reserve() function. lib/lmb.c | 36 ++++++++++++++++ test/lib/lmb.c | 114 +++++++++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 146 insertions(+), 4 deletions(-) diff --git a/lib/lmb.c b/lib/lmb.c index 7ca44591e1d..d7d2c8c6dfd 100644 --- a/lib/lmb.c +++ b/lib/lmb.c @@ -595,6 +595,39 @@ static __maybe_unused void lmb_reserve_common_spl(void) } } +/** + * lmb_can_reserve_region() - check if the region can be reserved + * @base: base address of region to be reserved + * @size: size of region to be reserved + * @flags: flag of the region to be reserved + * + * Go through all the reserved regions and ensure that the requested + * region does not overlap with any existing regions. An overlap is + * allowed only when the flag of the request region and the existing + * region is LMB_NONE. + * + * Return: true if region can be reserved, false otherwise + */ +static bool lmb_can_reserve_region(phys_addr_t base, phys_size_t size, + u32 flags) +{ + uint i; + struct lmb_region *lmb_reserved = lmb.used_mem.data; + + for (i = 0; i < lmb.used_mem.count; i++) { + u32 rgnflags = lmb_reserved[i].flags; + phys_addr_t rgnbase = lmb_reserved[i].base; + phys_size_t rgnsize = lmb_reserved[i].size; + + if (lmb_addrs_overlap(base, size, rgnbase, rgnsize)) { + if (flags != LMB_NONE || flags != rgnflags) + return false; + } + } + + return true; +} + void lmb_add_memory(void) { int i; @@ -667,6 +700,9 @@ long lmb_reserve(phys_addr_t base, phys_size_t size, u32 flags) long ret = 0; struct alist *lmb_rgn_lst = &lmb.used_mem; + if (!lmb_can_reserve_region(base, size, flags)) + return -EEXIST; + ret = lmb_add_region_flags(lmb_rgn_lst, base, size, flags); if (ret) return ret; diff --git a/test/lib/lmb.c b/test/lib/lmb.c index fcb5f1af532..24416e83491 100644 --- a/test/lib/lmb.c +++ b/test/lib/lmb.c @@ -471,17 +471,17 @@ static int lib_test_lmb_overlapping_reserve(struct unit_test_state *uts) ASSERT_LMB(mem_lst, used_lst, ram, ram_size, 1, 0x40010000, 0x10000, 0, 0, 0, 0); - /* allocate overlapping region should return the coalesced count */ + /* allocate overlapping region */ ret = lmb_reserve(0x40011000, 0x10000, LMB_NONE); ut_asserteq(ret, 0); ASSERT_LMB(mem_lst, used_lst, ram, ram_size, 1, 0x40010000, 0x11000, 0, 0, 0, 0); - /* allocate 3nd region */ + /* allocate 2nd region */ ret = lmb_reserve(0x40030000, 0x10000, LMB_NONE); ut_asserteq(ret, 0); ASSERT_LMB(mem_lst, used_lst, ram, ram_size, 2, 0x40010000, 0x11000, 0x40030000, 0x10000, 0, 0); - /* allocate 2nd region , This should coalesced all region into one */ + /* allocate 3rd region , This should coalesce all regions into one */ ret = lmb_reserve(0x40020000, 0x10000, LMB_NONE); ut_assert(ret >= 0); ASSERT_LMB(mem_lst, used_lst, ram, ram_size, 1, 0x40010000, 0x30000, @@ -499,6 +499,41 @@ static int lib_test_lmb_overlapping_reserve(struct unit_test_state *uts) ASSERT_LMB(mem_lst, used_lst, ram, ram_size, 1, 0x40000000, 0x40000, 0, 0, 0, 0); + /* try to allocate overlapping region with a different flag, should fail */ + ret = lmb_reserve(0x40008000, 0x1000, LMB_NOOVERWRITE); + ut_asserteq(ret, -EEXIST); + + /* allocate another region at 0x40050000 with a different flag */ + ret = lmb_reserve(0x40050000, 0x10000, LMB_NOOVERWRITE); + ut_asserteq(ret, 0); + ASSERT_LMB(mem_lst, used_lst, ram, ram_size, 2, 0x40000000, 0x40000, + 0x40050000, 0x10000, 0, 0); + + /* + * try to reserve a region adjacent to region 1 overlapping the 2nd region, + * should fail + */ + ret = lmb_reserve(0x40040000, 0x20000, LMB_NONE); + ut_asserteq(ret, -EEXIST); + + /* + * try to reserve a region between the two regions, but without an overlap, + * should succeed. this added region coalesces with the region 1 + */ + ret = lmb_reserve(0x40040000, 0x10000, LMB_NONE); + ut_asserteq(ret, 0); + ASSERT_LMB(mem_lst, used_lst, ram, ram_size, 2, 0x40000000, 0x50000, + 0x40050000, 0x10000, 0, 0); + + /* + * try to reserve a region which overlaps with both the regions, + * should fail as the flags do not match + */ + ret = lmb_reserve(0x40020000, 0x80000, LMB_NONE); + ut_asserteq(ret, -EEXIST); + ASSERT_LMB(mem_lst, used_lst, ram, ram_size, 2, 0x40000000, 0x50000, + 0x40050000, 0x10000, 0, 0); + lmb_pop(&store); return 0; @@ -549,6 +584,77 @@ static int test_alloc_addr(struct unit_test_state *uts, const phys_addr_t ram) ret = lmb_free(alloc_addr_a, 0x1000); ut_asserteq(ret, 0); + /* + * Add two regions with different flags, region1 and region2 with + * a gap between them. + * Try adding another region, adjacent to region 1 and overlapping + * region 2. Should fail. + */ + a = lmb_alloc_addr(alloc_addr_a, 0x1000, LMB_NONE); + ut_asserteq(a, alloc_addr_a); + + b = lmb_alloc_addr(alloc_addr_a + 0x4000, 0x1000, LMB_NOOVERWRITE); + ut_asserteq(b, alloc_addr_a + 0x4000); + ASSERT_LMB(mem_lst, used_lst, ram, ram_size, 2, a, 0x1000, + b, 0x1000, 0, 0); + + c = lmb_alloc_addr(alloc_addr_a + 0x1000, 0x5000, LMB_NONE); + ut_asserteq(c, 0); + ASSERT_LMB(mem_lst, used_lst, ram, ram_size, 2, a, 0x1000, + b, 0x1000, 0, 0); + + ret = lmb_free(a, 0x1000); + ut_asserteq(ret, 0); + ret = lmb_free(b, 0x1000); + ut_asserteq(ret, 0); + + /* + * Add two regions with same flags(LMB_NONE), region1 and region2 + * with a gap between them. + * Try adding another region, adjacent to region 1 and overlapping + * region 2. Should succeed. All regions should coalesce into a + * single region. + */ + a = lmb_alloc_addr(alloc_addr_a, 0x1000, LMB_NONE); + ut_asserteq(a, alloc_addr_a); + + b = lmb_alloc_addr(alloc_addr_a + 0x4000, 0x1000, LMB_NONE); + ut_asserteq(b, alloc_addr_a + 0x4000); + ASSERT_LMB(mem_lst, used_lst, ram, ram_size, 2, a, 0x1000, + b, 0x1000, 0, 0); + + c = lmb_alloc_addr(alloc_addr_a + 0x1000, 0x5000, LMB_NONE); + ut_asserteq(c, alloc_addr_a + 0x1000); + ASSERT_LMB(mem_lst, used_lst, ram, ram_size, 1, a, 0x6000, + 0, 0, 0, 0); + + ret = lmb_free(a, 0x6000); + ut_asserteq(ret, 0); + + /* + * Add two regions with same flags(LMB_NOOVERWRITE), region1 and + * region2 with a gap between them. + * Try adding another region, adjacent to region 1 and overlapping + * region 2. Should fail. + */ + a = lmb_alloc_addr(alloc_addr_a, 0x1000, LMB_NOOVERWRITE); + ut_asserteq(a, alloc_addr_a); + + b = lmb_alloc_addr(alloc_addr_a + 0x4000, 0x1000, LMB_NOOVERWRITE); + ut_asserteq(b, alloc_addr_a + 0x4000); + ASSERT_LMB(mem_lst, used_lst, ram, ram_size, 2, a, 0x1000, + b, 0x1000, 0, 0); + + c = lmb_alloc_addr(alloc_addr_a + 0x1000, 0x5000, LMB_NOOVERWRITE); + ut_asserteq(c, 0); + ASSERT_LMB(mem_lst, used_lst, ram, ram_size, 2, a, 0x1000, + b, 0x1000, 0, 0); + + ret = lmb_free(a, 0x1000); + ut_asserteq(ret, 0); + ret = lmb_free(b, 0x1000); + ut_asserteq(ret, 0); + /* reserve 3 blocks */ ret = lmb_reserve(alloc_addr_a, 0x10000, LMB_NONE); ut_asserteq(ret, 0); @@ -760,7 +866,7 @@ static int lib_test_lmb_flags(struct unit_test_state *uts) /* reserve again, new flag */ ret = lmb_reserve(0x40010000, 0x10000, LMB_NONE); - ut_asserteq(ret, -1); + ut_asserteq(ret, -EEXIST); ASSERT_LMB(mem_lst, used_lst, ram, ram_size, 1, 0x40010000, 0x10000, 0, 0, 0, 0);