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); From patchwork Thu Feb 20 09:56:50 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sughosh Ganu X-Patchwork-Id: 866736 Delivered-To: patch@linaro.org Received: by 2002:a05:6000:1f85:b0:38f:210b:807b with SMTP id hj5csp115384wrb; Thu, 20 Feb 2025 01:57:46 -0800 (PST) X-Forwarded-Encrypted: i=2; AJvYcCVHfsIyFBJM8tLgMJNWvgA793z3Oj7LIzZDIXSFBnYLOpPTWRTpwOGIcRN5HwAyRt+dLO/Hog==@linaro.org X-Google-Smtp-Source: AGHT+IEVfbVQcUglcld2tp2pIVh5wsJJHuGdDniWLeLWp2ntxTpfQenNLpDpteVMV4nfQudQZYqn X-Received: by 2002:a05:600c:190c:b0:439:65f0:c9ce with SMTP id 5b1f17b1804b1-43999ddac3bmr56294315e9.25.1740045465860; Thu, 20 Feb 2025 01:57:45 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1740045465; cv=none; d=google.com; s=arc-20240605; b=ALmHJgq3bDqCKDbCW1zK90uEha603frlipzFCUUWdeKwP+ajbX65xWrTjb8QGASWxV j4TBO8d5kyMa0GVyNW9ewzxiZYbafsX4PRXCJe4b2GExly2A/P+u1eQecXMNDNLHHIea tX6PTod1n6Fp/5MehkF1g6fpqJKnbTTW09/A8bx/YsSwlry4Kqfu7yfjC0vpJ+6pke6F kmdjczGBe3Fe1a7UfT1IBDf/a+gryiv2BpNrrsvsSIO9mvvZj8pqkgVAKgmPAlkkwVF1 UXSJ+mUUQUL2eke6FdR9hUdkObZMbwh+uPZqPSBP8F/f8HwORK8cIzU9weNnvPKzcXnO zWXw== 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=BgI+EHX2Bs/W1uLFtLPcFiQ5PrBF/09lep+WySI1m8c=; fh=2HGKihHUAicyV3F+aHiaoPpwvqWA0zlC04Ew6QaQoEg=; b=T6ol4DW/XGEjMhuY52+x2ovks/eukBTbzdPAoisTpDM0XHyt+4JpPJOzi2pKSZWg5s MhGOJH0w9e+r3FM+c9cr+7UqF0RlEQ4OOuntsZTUHp8O7QX3jIk5KY8NkTbeBt8qF2mW 9fCYby7ePM/Hd/3P8GT/hMgnMCgT2Tyg6+lDdqw5uXQnZJCVRoRlosKlULjSKsK9WaXo dEH2nM1lOLrmmVxTS/qa0iHT1QSQ/JeIuUKLnMYkGl7FEM6bjdXN0FfG63keZbKlDmxJ 71KrdTrMRRmUquvJpQ6U2bdPgHw2tFT8yuo1DBtMFeEfNucMX2vBX7SJSoeWxkgJC3/R vnwA==; 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-4398adc8c58si52589255e9.6.2025.02.20.01.57.45 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 20 Feb 2025 01:57:45 -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 4EC2480F84; Thu, 20 Feb 2025 10:57:32 +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 5EC5D80F84; Thu, 20 Feb 2025 10:57:31 +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 486B880077 for ; 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=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 D27251BB0; Thu, 20 Feb 2025 01:57:46 -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 1EC863F59E; Thu, 20 Feb 2025 01:57:25 -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 2/6] lmb: handle scenario of encompassing overlap Date: Thu, 20 Feb 2025 15:26:50 +0530 Message-Id: <20250220095654.121634-3-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 lmb_fix_over_lap_regions() function is called if the added region overlaps with an existing region. The function then fixes the overlap and removes the redundant region. However, it makes certain assumptions. One assumption is that the overlap would not encompass the existing region. Another assumption is that the overlap only occurs between two regions -- the scenario of the added region overlapping multiple existing regions is not being handled. Handle these cases by instead calling lmb_resize_regions(). Also remove the now superfluous lmb_fix_over_lap_regions(). Signed-off-by: Sughosh Ganu Reviewed-by: Heinrich Schuchardt --- Changes since V1: * Use lmb_resize_regions() to fix the overlap instead of lmb_fix_over_lap_regions(). * Remove the now superfluous lmb_fix_over_lap_regions(). Note: To be applied after an A-b/R-b/T-b from the original author of lmb_fix_over_lap_regions(). lib/lmb.c | 30 +++++++++--------------------- 1 file changed, 9 insertions(+), 21 deletions(-) diff --git a/lib/lmb.c b/lib/lmb.c index d7d2c8c6dfd..207f059ceb9 100644 --- a/lib/lmb.c +++ b/lib/lmb.c @@ -100,25 +100,6 @@ static void lmb_coalesce_regions(struct alist *lmb_rgn_lst, unsigned long r1, lmb_remove_region(lmb_rgn_lst, r2); } -/*Assumption : base addr of region 1 < base addr of region 2*/ -static void lmb_fix_over_lap_regions(struct alist *lmb_rgn_lst, - unsigned long r1, unsigned long r2) -{ - struct lmb_region *rgn = lmb_rgn_lst->data; - - phys_addr_t base1 = rgn[r1].base; - phys_size_t size1 = rgn[r1].size; - phys_addr_t base2 = rgn[r2].base; - phys_size_t size2 = rgn[r2].size; - - if (base1 + size1 > base2 + size2) { - printf("This will not be a case any time\n"); - return; - } - rgn[r1].size = base2 + size2 - base1; - lmb_remove_region(lmb_rgn_lst, r2); -} - static long lmb_resize_regions(struct alist *lmb_rgn_lst, unsigned long idx_start, phys_addr_t base, phys_size_t size) @@ -239,8 +220,15 @@ static long lmb_add_region_flags(struct alist *lmb_rgn_lst, phys_addr_t base, lmb_coalesce_regions(lmb_rgn_lst, i, i + 1); coalesced++; } else if (lmb_regions_overlap(lmb_rgn_lst, i, i + 1)) { - /* fix overlapping area */ - lmb_fix_over_lap_regions(lmb_rgn_lst, i, i + 1); + /* fix overlapping areas */ + phys_addr_t rgnbase = rgn[i].base; + phys_size_t rgnsize = rgn[i].size; + + ret = lmb_resize_regions(lmb_rgn_lst, i, + rgnbase, rgnsize); + if (ret < 0) + return -1; + coalesced++; } } From patchwork Thu Feb 20 09:56:51 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sughosh Ganu X-Patchwork-Id: 866737 Delivered-To: patch@linaro.org Received: by 2002:a05:6000:1f85:b0:38f:210b:807b with SMTP id hj5csp115437wrb; Thu, 20 Feb 2025 01:57:55 -0800 (PST) X-Forwarded-Encrypted: i=2; AJvYcCUk0reogasy4meRGygNQPAptOylZqTI6hi88d6yjKoP2ydQnSroez6Aza/31diIDbXvYW0yGQ==@linaro.org X-Google-Smtp-Source: AGHT+IE6QAv2fwLjnFtbVP97y8DPfS+lefcDxvyhweX2U5dpmPTZaR2qTrlyXjFUkUJqj32n6SUv X-Received: by 2002:a05:600c:5101:b0:439:9496:181c with SMTP id 5b1f17b1804b1-43994961a51mr96605045e9.29.1740045475101; Thu, 20 Feb 2025 01:57:55 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1740045475; cv=none; d=google.com; s=arc-20240605; b=OSZJ4PNoI8Ybn0dUpdjgWb0b4SdWp3ZLqR2q5waXrOhka75bpjyAF6xLzunbBIaUNS IZF4mGB99IFs8NDrC6TyTWAXPBZoLCHEQhHC3KRfBBdT7Vp5mSlRJiJiqI8q4K2xE11w AaoOvZOhM4sIXSLzjrDZgy6rWzjqVXL4COMfFi/GscO8fEohf0iXHV2eDhA1aROTQaQ6 36TGoHt/j7tPSrGGl/Q5Tt8VsCx6YMpCqejVhovptNt3Lt84PqlJdwACQN815hga86QK 20OeUYns6o0eyZ5fY7uP+WLEKL1lKxai+LjQWDZN46xMwQn6DFHIfhlwGiWIJV55+rvl QqxQ== 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=iyL5pNhWk6tw2RwW3HpN/Y7Q2HB3QR4S+KFr8y8XHLU=; fh=2HGKihHUAicyV3F+aHiaoPpwvqWA0zlC04Ew6QaQoEg=; b=DMN42n+HgLfVUFmrcDbMrRmJnc1Zoqlc4zrc3smVxfsXOks4Oj2z1iqnWleQMRoVoQ V7nZvw2TYR5Oa9yHtw6Wd/AkWpwl8lbyEK8IwsPqJIsD0CWUwERiATM+sgLQ8BTn+r34 37YUBUHpMnoTrh4MXmv+TvRO2X7EK/4HCH9YTi4TvMJYK+nIN1OU7PoJaeHl4niAICX+ 6+1k4CKOHYVT0hmFSr9PFBOUmgC8JXclek7y8wGQGirMqnUu4lHB53kQua1QebhfUPJx 4IZlfF14f26L+l9b2SbFcCAm0J1lfGV/JKKRsv5g6LLcdgQDY7g55qygoVN3xVr8tdJ0 tWDQ==; dara=google.com ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of u-boot-bounces@lists.denx.de designates 2a01:238:438b:c500:173d:9f52:ddab:ee01 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. [2a01:238:438b:c500:173d:9f52:ddab:ee01]) by mx.google.com with ESMTPS id 5b1f17b1804b1-439958e8215si37151425e9.138.2025.02.20.01.57.54 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 20 Feb 2025 01:57:55 -0800 (PST) Received-SPF: pass (google.com: domain of u-boot-bounces@lists.denx.de designates 2a01:238:438b:c500:173d:9f52:ddab:ee01 as permitted sender) client-ip=2a01:238:438b:c500:173d:9f52:ddab:ee01; Authentication-Results: mx.google.com; spf=pass (google.com: domain of u-boot-bounces@lists.denx.de designates 2a01:238:438b:c500:173d:9f52:ddab:ee01 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 C4FBB80F67; Thu, 20 Feb 2025 10:57:35 +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 886C480A46; Thu, 20 Feb 2025 10:57:34 +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 5EDB880F96 for ; Thu, 20 Feb 2025 10:57:32 +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 F02E01BB0; Thu, 20 Feb 2025 01:57:49 -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 3C1923F59E; Thu, 20 Feb 2025 01:57:28 -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 3/6] lmb: check for a region's coalescing with all existing regions Date: Thu, 20 Feb 2025 15:26:51 +0530 Message-Id: <20250220095654.121634-4-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 lmb_add_region_flags() first checks if the new region to be added can be coalesced with existing regions. The check stops if the two regions are adjecent but their flags do not match. However, it is possible that the newly added region might be adjacent with the next existing region and with matching flags. Check for this possibility by not breaking out of the loop. Signed-off-by: Sughosh Ganu Reviewed-by: Heinrich Schuchardt --- Changes since V1: * Continue the loop only in the case where the newly added region is after the existing region. lib/lmb.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/lmb.c b/lib/lmb.c index 207f059ceb9..45888989457 100644 --- a/lib/lmb.c +++ b/lib/lmb.c @@ -194,7 +194,7 @@ static long lmb_add_region_flags(struct alist *lmb_rgn_lst, phys_addr_t base, break; } else if (ret < 0) { if (flags != rgnflags) - break; + continue; rgn[i].size += size; coalesced++; break; From patchwork Thu Feb 20 09:56:52 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sughosh Ganu X-Patchwork-Id: 866738 Delivered-To: patch@linaro.org Received: by 2002:a05:6000:1f85:b0:38f:210b:807b with SMTP id hj5csp115494wrb; Thu, 20 Feb 2025 01:58:03 -0800 (PST) X-Forwarded-Encrypted: i=2; AJvYcCVQpfmme15f1zJ2etLcTO0L5fvf4UbA4mB6B+nbmOJo9FuNXHbJQ3Jg6Ue4RKzbHM83pTtccw==@linaro.org X-Google-Smtp-Source: AGHT+IF2SCW8OOkffwLyhFVr+3sJ+O5tumdwXX1QZSoRjKt2ZOJtfr3VvLUJLdeEaN2dXC8pA78a X-Received: by 2002:a5d:47ce:0:b0:38f:3a89:fdae with SMTP id ffacd0b85a97d-38f3a8a019emr16178090f8f.46.1740045483141; Thu, 20 Feb 2025 01:58:03 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1740045483; cv=none; d=google.com; s=arc-20240605; b=Ci7F5mO0fia4//pt76vcRc3tDyZGTYGZmTe6YFjA52jGqyzM/wRM2N6PkiRyXOkLXu Ol5lTUdwrcEvBb9uhuXMtZZx8bd5/l/rfPUMNER7ucXypL6ePsDEvrudurvUGeHnzk9L pIw4uHGlgqji0xKsMgRfZvN/zOvC1UOc80kTDdfQ+6rcImpa4v4F3xDPtJlVANCsCmRz 3wYT+d5sciOCSGboXgOIhef0SQwMPDJYhfDtVi8SvdtvDS9u7ssVvy78AmFe8jhN5DeO E1xlwWRa88jYDgAPVGxfGXZJ06QsjJGb0fMwDCz/NFJcE+ul7UkPctAnsPbaMpmb7lR3 nCUg== 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=ulmnsxgXDKax8iU8lGVDQoi4TxBcCSl3JyOXz04FGqw=; fh=2HGKihHUAicyV3F+aHiaoPpwvqWA0zlC04Ew6QaQoEg=; b=jRWoG6I026979XTg7zl5L/hPP3h3cDvvH32naA8pwEStQZOwgAAzadgt6xwPirleOP 40B5geIJrpez4msXKcDMX1QOaROYYlfUZlepwZ7rUiqSxkhY/DcT/6R7RAYeQXR06RRa NKcEoRFVjYVCIwry6iFD8nNXt7ByrC2P1mi2hRb5iun6gkJ5UjBxUH8P0wUvo+DLFewj sWuaN8Ii1Vma9yjWufarL1A9U1bxGwJuYWAqzmiMAsPkaxjDjOZdBqkecdjTxNyMkiuR 8/NRWxkpsXZ/BMtAstPfTDH65qAjIiAg8ttEYbEHh2vdjw2ut1Q0Vm90LlK3jHnyUTZq AZDQ==; dara=google.com ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of u-boot-bounces@lists.denx.de designates 2a01:238:438b:c500:173d:9f52:ddab:ee01 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. [2a01:238:438b:c500:173d:9f52:ddab:ee01]) by mx.google.com with ESMTPS id ffacd0b85a97d-38f25a32bb4si14364534f8f.769.2025.02.20.01.58.02 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 20 Feb 2025 01:58:03 -0800 (PST) Received-SPF: pass (google.com: domain of u-boot-bounces@lists.denx.de designates 2a01:238:438b:c500:173d:9f52:ddab:ee01 as permitted sender) client-ip=2a01:238:438b:c500:173d:9f52:ddab:ee01; Authentication-Results: mx.google.com; spf=pass (google.com: domain of u-boot-bounces@lists.denx.de designates 2a01:238:438b:c500:173d:9f52:ddab:ee01 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 384AC80C98; Thu, 20 Feb 2025 10:57:39 +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 B2BDD80F7B; Thu, 20 Feb 2025 10:57:37 +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 9F37780C98 for ; Thu, 20 Feb 2025 10:57:35 +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 1D16F1BB0; Thu, 20 Feb 2025 01:57:53 -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 58CBE3F59E; Thu, 20 Feb 2025 01:57:32 -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 4/6] lmb: remove superfluous address overlap check from lmb_add_region_flags() Date: Thu, 20 Feb 2025 15:26:52 +0530 Message-Id: <20250220095654.121634-5-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 lmb_reserve() function now does a check for a reservation request with existing reserved regions, and returns -EEXIST in case of an overlap. Remove this now redundant check from lmb_add_region_flags(). Signed-off-by: Sughosh Ganu --- Changes since V1: New patch lib/lmb.c | 3 --- 1 file changed, 3 deletions(-) diff --git a/lib/lmb.c b/lib/lmb.c index 45888989457..061f9a07541 100644 --- a/lib/lmb.c +++ b/lib/lmb.c @@ -199,9 +199,6 @@ static long lmb_add_region_flags(struct alist *lmb_rgn_lst, phys_addr_t base, coalesced++; break; } else if (lmb_addrs_overlap(base, size, rgnbase, rgnsize)) { - if (flags != LMB_NONE) - return -EEXIST; - ret = lmb_resize_regions(lmb_rgn_lst, i, base, size); if (ret < 0) return -1; From patchwork Thu Feb 20 09:56:53 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sughosh Ganu X-Patchwork-Id: 866739 Delivered-To: patch@linaro.org Received: by 2002:a05:6000:1f85:b0:38f:210b:807b with SMTP id hj5csp115523wrb; Thu, 20 Feb 2025 01:58:11 -0800 (PST) X-Forwarded-Encrypted: i=2; AJvYcCX7z4atup4oaXyhMBQw5/NUkiKw/1AYY90kEaR+lYnd+63Y8I7aD79jLtQnV8+e9b1uqGCO7w==@linaro.org X-Google-Smtp-Source: AGHT+IFiUi0o9hLvIQm9g3svoR9JXgRdmMfnWbJCqzM730vPiecVKHcLjFdtEj7aDufgvoxV9zfp X-Received: by 2002:a5d:404d:0:b0:38d:cf33:31a1 with SMTP id ffacd0b85a97d-38f33f40a63mr15489741f8f.23.1740045491617; Thu, 20 Feb 2025 01:58:11 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1740045491; cv=none; d=google.com; s=arc-20240605; b=IC1HdF8/GM5U6kQK4dlWjAu49Ly5rmbTQCOQ5eUWW2JraRV6NHw9rCkS7bWpP/wSzF VFhU+2JDXpNtPny0demQY+rz3GwzqlpWq3eNTYYGJsPb3+KhgWs/3848pzXQleTaDjpg NqlDAjZU7b9nIObgdWSa/hh3iSnSrh8KFuZPJS54C4QGO1bEMGeXYHJhpysdXCG3OUiw VTvR34P1VV+rWrQJcGTLR+wskpjtHMWm60Qj++EcUBlzwAjgJX3f4xoA202AvsYRWDJN ZogH8Kezl2tSMKUa1elOw6+7J8KuRbdsgN0UW08keWqtOP+K7snorGilGJimRilrk+Gh Fz2A== 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=MsU/bQMWpG6r5nGn7BJ5KaMERbbLjRP89KJ6Vl55yhM=; fh=2HGKihHUAicyV3F+aHiaoPpwvqWA0zlC04Ew6QaQoEg=; b=GY0xOql7OV+g9e6/rkymWyayVxaJ+5TATLnSIoLB3Lu2av4BTheK7sQh6qZvgyegT8 36MUY9RzpJkK7Inu/Hsk/Ui/lwp4GgTuujKF/3jR51gVeeoAT6CQSwMeVVd+JotsgCl6 DEz4X1W0nqBsj6CaqtE/SgtRnX/ICa2o0ZyFalUTR4+29KQQxwXqEL9jDeDDnpeOuwiB coNXr+lbEqcgZ8acgjwzJxiXF9xwONzTtSpMkWZwnwdivn/YHQaC7NrovK89qXdvwHsC 1xDl5HBkj1g1jNAz49DAfxxARVWTSPpmvmldengT4weZgRUGUev5XnSMV8B4FTs5xQYT 5Xpg==; 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 ffacd0b85a97d-38f259d60c6si14353577f8f.281.2025.02.20.01.58.11 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 20 Feb 2025 01:58:11 -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 6C21080F9A; Thu, 20 Feb 2025 10:57:43 +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 542D880A46; Thu, 20 Feb 2025 10:57:42 +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 522AB80F56 for ; Thu, 20 Feb 2025 10:57:39 +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 335771BB0; Thu, 20 Feb 2025 01:57:56 -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 747FF3F59E; Thu, 20 Feb 2025 01:57:35 -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 5/6] lmb: use a common function to check if regions overlap or are adjacent Date: Thu, 20 Feb 2025 15:26:53 +0530 Message-Id: <20250220095654.121634-6-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 functions to check if the two said regions are adjacent or overlap are pretty similar in nature. Club the functionality into a single function lmb_regions_check() and return the appropriate return value to signify this aspect. Signed-off-by: Sughosh Ganu --- Changes since V1: New patch lib/lmb.c | 71 ++++++++++++++++++++++++++++++++----------------------- 1 file changed, 41 insertions(+), 30 deletions(-) diff --git a/lib/lmb.c b/lib/lmb.c index 061f9a07541..874063fc1f5 100644 --- a/lib/lmb.c +++ b/lib/lmb.c @@ -27,6 +27,9 @@ DECLARE_GLOBAL_DATA_PTR; #define MAP_OP_FREE (u8)0x2 #define MAP_OP_ADD (u8)0x3 +#define LMB_RGN_OVERLAP 1 +#define LMB_RGN_ADJACENT 2 + /* * The following low level LMB functions must not access the global LMB memory * map since they are also used to manage IOVA memory maps in iommu drivers like @@ -53,8 +56,22 @@ static long lmb_addrs_adjacent(phys_addr_t base1, phys_size_t size1, return 0; } -static long lmb_regions_overlap(struct alist *lmb_rgn_lst, unsigned long r1, - unsigned long r2) +/** + * lmb_regions_check() - Check if the regions overlap, or are adjacent + * @lmb_rgn_lst: List of LMB regions + * @r1: First region to check + * @r2: Second region to check + * + * Check if the two regions with matching flags, r1 and r2 are + * adjacent to each other, or if they overlap. + * + * Return: + * * %LMB_RGN_OVERLAP - Regions overlap + * * %LMB_RGN_ADJACENT - Regions adjacent to each other + * * 0 - Neither of the above, or flags mismatch + */ +static long lmb_regions_check(struct alist *lmb_rgn_lst, unsigned long r1, + unsigned long r2) { struct lmb_region *rgn = lmb_rgn_lst->data; phys_addr_t base1 = rgn[r1].base; @@ -62,19 +79,15 @@ static long lmb_regions_overlap(struct alist *lmb_rgn_lst, unsigned long r1, phys_addr_t base2 = rgn[r2].base; phys_size_t size2 = rgn[r2].size; - return lmb_addrs_overlap(base1, size1, base2, size2); -} + if (rgn[r1].flags != rgn[r2].flags) + return 0; -static long lmb_regions_adjacent(struct alist *lmb_rgn_lst, unsigned long r1, - unsigned long r2) -{ - struct lmb_region *rgn = lmb_rgn_lst->data; - phys_addr_t base1 = rgn[r1].base; - phys_size_t size1 = rgn[r1].size; - phys_addr_t base2 = rgn[r2].base; - phys_size_t size2 = rgn[r2].size; + if (lmb_addrs_overlap(base1, size1, base2, size2)) + return LMB_RGN_OVERLAP; + else if (lmb_addrs_adjacent(base1, size1, base2, size2)) + return LMB_RGN_ADJACENT; - return lmb_addrs_adjacent(base1, size1, base2, size2); + return 0; } static void lmb_remove_region(struct alist *lmb_rgn_lst, unsigned long r) @@ -211,23 +224,21 @@ static long lmb_add_region_flags(struct alist *lmb_rgn_lst, phys_addr_t base, } if (lmb_rgn_lst->count && i < lmb_rgn_lst->count - 1) { - rgn = lmb_rgn_lst->data; - if (rgn[i].flags == rgn[i + 1].flags) { - if (lmb_regions_adjacent(lmb_rgn_lst, i, i + 1)) { - lmb_coalesce_regions(lmb_rgn_lst, i, i + 1); - coalesced++; - } else if (lmb_regions_overlap(lmb_rgn_lst, i, i + 1)) { - /* fix overlapping areas */ - phys_addr_t rgnbase = rgn[i].base; - phys_size_t rgnsize = rgn[i].size; - - ret = lmb_resize_regions(lmb_rgn_lst, i, - rgnbase, rgnsize); - if (ret < 0) - return -1; - - coalesced++; - } + ret = lmb_regions_check(lmb_rgn_lst, i, i + 1); + if (ret == LMB_RGN_ADJACENT) { + lmb_coalesce_regions(lmb_rgn_lst, i, i + 1); + coalesced++; + } else if (ret == LMB_RGN_OVERLAP) { + /* fix overlapping areas */ + phys_addr_t rgnbase = rgn[i].base; + phys_size_t rgnsize = rgn[i].size; + + ret = lmb_resize_regions(lmb_rgn_lst, i, + rgnbase, rgnsize); + if (ret < 0) + return -1; + + coalesced++; } } From patchwork Thu Feb 20 09:56:54 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sughosh Ganu X-Patchwork-Id: 866740 Delivered-To: patch@linaro.org Received: by 2002:a05:6000:1f85:b0:38f:210b:807b with SMTP id hj5csp115564wrb; Thu, 20 Feb 2025 01:58:20 -0800 (PST) X-Forwarded-Encrypted: i=2; AJvYcCUTmxExf56syw4Z21bPcFL4flC363cwJ2T08LvxeCh54POeOJ4JH/cvnLp/SbO6QbqlmQuang==@linaro.org X-Google-Smtp-Source: AGHT+IEUFkJIhChh+QxFzuxwRgJaWHX/ZFMTUnkHQ59AuBdRGuL7f9cU3BlMHRKsRoMo9sc5g5xa X-Received: by 2002:a05:600c:458a:b0:439:3d5c:8bfb with SMTP id 5b1f17b1804b1-43999dd11d6mr63382925e9.22.1740045500515; Thu, 20 Feb 2025 01:58:20 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1740045500; cv=none; d=google.com; s=arc-20240605; b=fbbD6RfKgrXGWe9I/qbZA+cSv6e/s+EuJv3UwGoxISn/D+vNAsH+3oZtqrztrtnERI bpy2zfpv6KxWygrMyAPikjj7WWr89rk8oG7d3nqwORxL44Kl+c1sjssLFNp5wKJM0u17 TjOFbdsGcdtU/jKW8h44Yj9t3/nQ9YwJYXyoZRW/e8ihax8n5lQXXp8qMZANugBeQFrH OLx2Sx5ZxoKKhTv63cMoJNKB+JlhRSa8N8xuW9aY4pB9E2HWk4+11IXdCd+9PfNJAb3w vmb5+Z5NtNzcxJePiCORiaMlBMjNCNW56PUKBOKTaUXRztTq6e7Ezi2hBmfpXW63bOXu 2PAw== 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=ADVVLtgbFTNyNko3gblTHKRV9WFn6p7G8j1FKFVK7P0=; fh=2HGKihHUAicyV3F+aHiaoPpwvqWA0zlC04Ew6QaQoEg=; b=Ly749s5a9quiAzpIZ9GMLm3vP/feli7p8V0yAm3n4EeyhOE4+zgBmKBN9O4ClGBWsO VrE3mkHgwFIu7ghsFprd/uh8vmr+W2rcU+OVfu56/T+pftLDoNz32G9PqgzWjdedcVaf +C5Ey1SZ+GH/p0Ir2675/7oFgklWL+DFSByiF5L+JXsS3lhtCcsHF87AKK3Mj5TMdwgf g5uISXRoGfGd9TCJfs0j0lXrdvACEXpWTLTaMPInABHte86iD6J+WKNwumx60OpGDRJC d3Pai89qmxnGu6P8jYgh70/Zh1g/ESmOTqNq4Vd/RiOTIuC89wLhEQi6dXN1bC4z7xoK ud+Q==; 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-439618b0226si143728495e9.127.2025.02.20.01.58.20 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 20 Feb 2025 01:58:20 -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 BE67E80F77; Thu, 20 Feb 2025 10:57:45 +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 AD14680F7B; Thu, 20 Feb 2025 10:57:44 +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 B7BA480FA4 for ; Thu, 20 Feb 2025 10:57:41 +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 4F4CF1BB0; Thu, 20 Feb 2025 01:57:59 -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 913493F59E; Thu, 20 Feb 2025 01:57:38 -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 6/6] lmb: optimise the lmb allocation functions Date: Thu, 20 Feb 2025 15:26:54 +0530 Message-Id: <20250220095654.121634-7-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 actual logic to allocate a region of memory is in the _lmb_alloc_base() function. The lmb_alloc() API function calls lmb_alloc_base(), which then calls _lmb_alloc_base() to do the allocation. Instead, call the _lmb_alloc_base() directly from both the allocation API's, and move the error message to the _lmb_alloc_base(). Signed-off-by: Sughosh Ganu --- Changes since V1: New patch lib/lmb.c | 16 ++++++---------- 1 file changed, 6 insertions(+), 10 deletions(-) diff --git a/lib/lmb.c b/lib/lmb.c index 874063fc1f5..6bfc0dbc9ce 100644 --- a/lib/lmb.c +++ b/lib/lmb.c @@ -758,26 +758,22 @@ static phys_addr_t _lmb_alloc_base(phys_size_t size, ulong align, base = ALIGN_DOWN(res_base - size, align); } } + + log_err("Failed to allocate 0x%lx bytes below 0x%lx\n", (ulong)size, + (ulong)max_addr); + return 0; } phys_addr_t lmb_alloc(phys_size_t size, ulong align) { - return lmb_alloc_base(size, align, LMB_ALLOC_ANYWHERE, LMB_NONE); + return _lmb_alloc_base(size, align, LMB_ALLOC_ANYWHERE, LMB_NONE); } phys_addr_t lmb_alloc_base(phys_size_t size, ulong align, phys_addr_t max_addr, uint flags) { - phys_addr_t alloc; - - alloc = _lmb_alloc_base(size, align, max_addr, flags); - - if (alloc == 0) - printf("ERROR: Failed to allocate 0x%lx bytes below 0x%lx.\n", - (ulong)size, (ulong)max_addr); - - return alloc; + return _lmb_alloc_base(size, align, max_addr, flags); } phys_addr_t lmb_alloc_addr(phys_addr_t base, phys_size_t size, u32 flags)