From patchwork Mon Mar 3 13:32:26 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sughosh Ganu X-Patchwork-Id: 869832 Delivered-To: patch@linaro.org Received: by 2002:a05:6000:1561:b0:38f:210b:807b with SMTP id 1csp2122232wrz; Mon, 3 Mar 2025 05:33:12 -0800 (PST) X-Forwarded-Encrypted: i=2; AJvYcCUjzh/yefMyYDlsQwLJzJrbtDMXESHbMEVv2l6gbBIhfygFpSQRFE5gCtpFYha9+lTYNWIC+g==@linaro.org X-Google-Smtp-Source: AGHT+IFO79KX1Zsa/wKlyWwc/JZkYR9RP2WAOr3Kdgbv/r1qo3ekdSbjLk8C40AgRt3SsfI3qks8 X-Received: by 2002:a05:6a00:2e23:b0:736:4cde:5c0e with SMTP id d2e1a72fcca58-7364cde5ec6mr6825226b3a.10.1741008791818; Mon, 03 Mar 2025 05:33:11 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1741008791; cv=none; d=google.com; s=arc-20240605; b=DXgWJbfxsYnPZDtXODrhAgL0cMONuTASEl3g8XM2//5aoF3jqfgFWvJ5o6fD0Xt1kW SoMvyNAuzbobxm6kqvcxtqBxelXWf5CW4U7kJa8U4IbXXYjO2YFXC4w2H52PWhngU+sU e7zi/A+IamaxMBwi3Exwr5Io/LL/Ur+ULYSdVuethPy9hd4nijEVCRtTK6v+naBT3+b7 sweEkjOz1iNiGWfv6d1kbK/IrXtHspVWtGnAzYi7p4qjW8P4oCWwyjrlnpkKVo8/szGC TGcr2VF1m+0UzGsg1wnNfT7I1nKw1KASH8Tn+ijRkXbsqTvBLPSbTAJ2oAde/AO2bhoD gVlQ== 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=bJA1XR7QXArFnzN8LuU5sVFkhwGm6JlPltyNk5z3fu8=; fh=2HGKihHUAicyV3F+aHiaoPpwvqWA0zlC04Ew6QaQoEg=; b=JR//seTp0GnUzyVEsp+pIxuM/WmLbhLm4XCdjk96jxwyV6YaDA4AQ7zHRVh1EQVRoP Bk1lBv5E3bl38GIikYTKtjJkq4R0aaJOMigiySn4yDXg4Q2VZ/cHm0Od7MEtltjAayzk 1+WZv4uz9IZJkYDk2UMHdUOr1hUUTTUurC0qhn1zXTGLsHlEtkdPoxAYWCgffif3hlec wKHt0IouNw5yMUzJV3R7jMsGT7SOzS8Mz54ZXgCu9nGkvJvJWqP0g/2Qqi3SXWU7Nn9U vcOrL4y3ic9+GMGFbUlaVlscvYcyl+BiHGgMrXZ2aeiQxdIVDGZI8M6sqEQXCurw8XfP G5mw==; 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 d2e1a72fcca58-73634a781acsi7423113b3a.272.2025.03.03.05.33.10 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 03 Mar 2025 05:33:11 -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 6F18381253; Mon, 3 Mar 2025 14:33:00 +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 4372581249; Mon, 3 Mar 2025 14:32:59 +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_RPBL_BLOCKED, RCVD_IN_VALIDITY_SAFE_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 E0108811B2 for ; Mon, 3 Mar 2025 14:32:56 +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 6DBB6106F; Mon, 3 Mar 2025 05:33:10 -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 C9B5A3F673; Mon, 3 Mar 2025 05:32:53 -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 v3 1/6] lmb: check if a region can be reserved by lmb_reserve() Date: Mon, 3 Mar 2025 19:02:26 +0530 Message-Id: <20250303133231.405279-2-sughosh.ganu@linaro.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20250303133231.405279-1-sughosh.ganu@linaro.org> References: <20250303133231.405279-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 V2: None 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 93fc1bea07c..32c787f8adf 100644 --- a/lib/lmb.c +++ b/lib/lmb.c @@ -561,6 +561,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; @@ -633,6 +666,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 Mon Mar 3 13:32:27 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sughosh Ganu X-Patchwork-Id: 869833 Delivered-To: patch@linaro.org Received: by 2002:a05:6000:1561:b0:38f:210b:807b with SMTP id 1csp2122322wrz; Mon, 3 Mar 2025 05:33:23 -0800 (PST) X-Forwarded-Encrypted: i=2; AJvYcCUhC8caxJfX+mtI/9aVR0h32iFLbS46ZktIbZojaiSWUUgTNmJYXgmmS2SRu5f/zzsEvXtU6A==@linaro.org X-Google-Smtp-Source: AGHT+IGHsjNpcpbzJCU97nKy2+cY9C0DNKjUJM2IiBy2nhR/V2yn9Tg2g5uoD5nJFAInAcXx8Ar8 X-Received: by 2002:a17:902:e750:b0:21f:ba96:5de9 with SMTP id d9443c01a7336-2236926f502mr261131805ad.49.1741008802942; Mon, 03 Mar 2025 05:33:22 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1741008802; cv=none; d=google.com; s=arc-20240605; b=OFz/KsCbTMJvbhv06ydiM/+vsPCe6ZRkruGzK06e6fmvtxWojaJiY2XKpJ0/W8gBhW qfbHzzMTELr1X4U9VxrbnTWAoxG2PsrwEPHlxe6OfeMWR+w48Q1GM1qDQHjI9umFiMpY 9rSOplPejaURNBRF14VKkTvkSFkFoHADIUZcLZ7/rgWOZPlXKfa+AYU95e9AcitE52NL 6htYKISV4sKb2IGUy+M7pihRQhJ+UrqV1ABoJCyWsxnU7zPHWXMfxLuA6mOrD3acTcbD 0yYovlgKZGYOeVqGU+jmYlAi478pQRI5uZbK7BUmVbd7qgo7rkDyYkhqb0mKBMGh/qq6 OKSQ== 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=UwstgArQWQ5IcpvQPVZqPcpB2WtGxbvyxBmyRVRWyls=; fh=2HGKihHUAicyV3F+aHiaoPpwvqWA0zlC04Ew6QaQoEg=; b=hkFS3OXeGZ81on9wuOAduZzsw1D4Xep4STJKo7HKzq64Jql+DAwMeOybmSriE5G1yZ T4Z4zRaSF84sD00+fPj3HF0VcYlttu/1qI8/Fdp3jHj2maDflMnoJ9ftMbHZ+SwbFqL+ UpV1ki4eQpfD9L/TM0QI3r964PRZUWonCEnFAXuKFhME/F5Tr49NjqCwOXBntp9lXka3 sO3fesTlGknAQkfXk+X5EOHLfZu07PvwTbiwpATOQe4jzt9rQ93qvKphqWlRpjEW6wfE mbZqOrRl9TsTHyTfj33L/kgHecO7/CPns6CqVDTXnZbnVdUglCKiXnYK2hQThdebUX1H Vc0w==; 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 d9443c01a7336-223501d6cf1si148844095ad.22.2025.03.03.05.33.21 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 03 Mar 2025 05:33:22 -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 B3BAB811B3; Mon, 3 Mar 2025 14:33:02 +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 1C80281276; Mon, 3 Mar 2025 14:33:02 +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_RPBL_BLOCKED, RCVD_IN_VALIDITY_SAFE_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 0A520811B2 for ; Mon, 3 Mar 2025 14:33:00 +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 85258106F; Mon, 3 Mar 2025 05:33:13 -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 E152D3F673; Mon, 3 Mar 2025 05:32:56 -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 v3 2/6] lmb: handle scenario of encompassing overlap Date: Mon, 3 Mar 2025 19:02:27 +0530 Message-Id: <20250303133231.405279-3-sughosh.ganu@linaro.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20250303133231.405279-1-sughosh.ganu@linaro.org> References: <20250303133231.405279-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 V2: None lib/lmb.c | 30 +++++++++--------------------- 1 file changed, 9 insertions(+), 21 deletions(-) diff --git a/lib/lmb.c b/lib/lmb.c index 32c787f8adf..26d9cafef41 100644 --- a/lib/lmb.c +++ b/lib/lmb.c @@ -96,25 +96,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) @@ -235,8 +216,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 Mon Mar 3 13:32:28 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sughosh Ganu X-Patchwork-Id: 869834 Delivered-To: patch@linaro.org Received: by 2002:a05:6000:1561:b0:38f:210b:807b with SMTP id 1csp2122415wrz; Mon, 3 Mar 2025 05:33:33 -0800 (PST) X-Forwarded-Encrypted: i=2; AJvYcCXlt5th3sRUUbEmC0aTWEjAhDh6IBbgQuLRQM6B2IOjG5osstCiMUlXrxTshfsmMbwXAWfvHQ==@linaro.org X-Google-Smtp-Source: AGHT+IEzfo+7lbmHu62Cm3VPWDWRhockbSIlMvQjN+6L0hnumCVhnhCsf2d7nNaj2pY2JaNYFCA7 X-Received: by 2002:a05:6a21:1518:b0:1ee:68e3:ff45 with SMTP id adf61e73a8af0-1f2f4e4c740mr25213570637.35.1741008812787; Mon, 03 Mar 2025 05:33:32 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1741008812; cv=none; d=google.com; s=arc-20240605; b=aCodzB1P89KLKNZoLUqek0nctCrNx36KJbBABv84TI+saKOzckqpFK/dC9YpiuAycP WEi4efH4E0RGpr/3uz/nGzxwFilSUPM3xT1U0SLoHHOE0BGS0wkVzzxDRgCXGNIbtPTt FMvvVeVCIxY4Q4ocs2El1feXayryeQVmAF5QU/oOgziPynO7Q4QZsgKld3h2aUPqYrtM smj9KR/7wHdz8iBuEr5bfQ3WBf48ZiXlBJdXkqa8Ft47CEVyeX4s+Oq8JWnaiUzt2ASX P2Jz5Z3NXhbpctLDi53XWrExQTGSlNP6iFGryo2ibY56WS0trBz2FhoaI6OhKp4D7N32 s7MQ== 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=2nfI1dXARh/XFigsWfRy5RMxb90eyWenlaYK5g7DPlA=; fh=2HGKihHUAicyV3F+aHiaoPpwvqWA0zlC04Ew6QaQoEg=; b=Ue3XWjCPZEHSa6z69Ks6ewC7YGgqFe0Wf0QiUEqPpvlu0Ti+PTgrNGyYfHx6RZSTlm 5mfiNhHIzrlBpuPjMSvHcl5/w0owRwdrwtHa7acGoEVM381fHDq2YeB627j9ayGVy4Mo lMudgx2OW4ISxwoVUOfAuULTj5LxiqXEk+51di/7lFGDLpMFBgk/dQJb2ZCExMNqLav/ XESaCuSEocB/2uKMn8yb9fsSpNRq7lBiYFfsTL7733ihL6riSrlxfJ9GlDggCXCooogK oBKVMI2u/1qo1KM7S68lE9PMhrotxnJ0Wkj0fqDcZaRHLEb/taojcW91aUyrmAvGZ4DX lmug==; 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 41be03b00d2f7-aee7ddf2a85si14112120a12.134.2025.03.03.05.33.32 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 03 Mar 2025 05:33:32 -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 170C88128D; Mon, 3 Mar 2025 14:33:06 +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 2124E81298; Mon, 3 Mar 2025 14:33:05 +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_RPBL_BLOCKED, RCVD_IN_VALIDITY_SAFE_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 1B49081280 for ; Mon, 3 Mar 2025 14:33:03 +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 9CEC4106F; Mon, 3 Mar 2025 05:33:16 -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 04ABC3F673; Mon, 3 Mar 2025 05:32:59 -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 v3 3/6] lmb: check for a region's coalescing with all existing regions Date: Mon, 3 Mar 2025 19:02:28 +0530 Message-Id: <20250303133231.405279-4-sughosh.ganu@linaro.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20250303133231.405279-1-sughosh.ganu@linaro.org> References: <20250303133231.405279-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 V2: None lib/lmb.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/lmb.c b/lib/lmb.c index 26d9cafef41..53af96fa2a9 100644 --- a/lib/lmb.c +++ b/lib/lmb.c @@ -190,7 +190,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 Mon Mar 3 13:32:29 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sughosh Ganu X-Patchwork-Id: 869835 Delivered-To: patch@linaro.org Received: by 2002:a05:6000:1561:b0:38f:210b:807b with SMTP id 1csp2122501wrz; Mon, 3 Mar 2025 05:33:43 -0800 (PST) X-Forwarded-Encrypted: i=2; AJvYcCVVagYjgwkowGG4gwJDbM3IQtr+eJmu4zHtURUhlkfUF2dTQOw5PjDr8H+bzIWrVt6phCWknA==@linaro.org X-Google-Smtp-Source: AGHT+IEWoI08FjOlMn5q3sRk2fW9xgkmI7A3G6PmX1SaD8T/rNOFx017day5vzJzlXHU0nFb0+/7 X-Received: by 2002:a05:6a20:7348:b0:1ee:be88:f5cf with SMTP id adf61e73a8af0-1f2f4e00bfbmr25529452637.32.1741008823281; Mon, 03 Mar 2025 05:33:43 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1741008823; cv=none; d=google.com; s=arc-20240605; b=LjmsLI9291Ktth1qFiMLpojwwDUbOCtKYiyhT8tHrFrXQolPv1suPj6aYnn+rUKETJ CHuG7o0ndVCtA2XNRVVQI/J0Xm0JvgqFX6pLp6tPP+O9s4XZssnrxSphpQCiK2/P7qc3 TVg64sNfxqk9UVbw395sX3bMADzYz13sBD4uDEqqIQqpcsC0u2keX3HT7ehn3txXn/bU W1P0zXdFjJVRFEJ/s7uIePdUUnUDGRvEzlF61HY9FhoXENmwV4BAYyL2H94EixkVVngI 42SsFGlX34r+WqhQyHDYsWbC2tzUAdbN10wUQexmtVhIsC/+wTym+oIlc1wc5z9PT3jB 4UnQ== 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=JnizsOqRrfPu/ZSgQ5K24sT6epsbRQfjAhXUblbywo8=; fh=2HGKihHUAicyV3F+aHiaoPpwvqWA0zlC04Ew6QaQoEg=; b=Qv02dBRzas0tOXjiC/A3ffb+T5z0ngp4uHUjDH41aMoTVS5DiYdNq0PWXL2056pH3R HW5MkynxjNQqDSYaSh6J450Ifh0lmhfDmsqlLfL+Umuw7/9s3AtwBRPPi5pERl5jwuiq 23BnYUeEy7o0SWUzkmoj/siONgWe18qfE7y/JJcp2z1HzRkOLElCd8dUBiioahnSt+/c P5cu5Az+0Bh1aHC1w2FfbWUPzwDNgIH5Iz8E3Gc1WGuOU/DuASKuaFRMLtwZ8td8FXxW rJxZKSRV6mmvWcgYpi9gP9kOkgGeOa/elStfoHnoWlrsv9ey1O7prGZcT//DI/uYDJc5 RTBg==; 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 41be03b00d2f7-af1e7ea62d9si7853388a12.289.2025.03.03.05.33.42 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 03 Mar 2025 05:33:43 -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 5DC5A812C8; Mon, 3 Mar 2025 14:33:09 +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 3DD55811B2; Mon, 3 Mar 2025 14:33:08 +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_RPBL_BLOCKED, RCVD_IN_VALIDITY_SAFE_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 33349812BE for ; Mon, 3 Mar 2025 14:33:06 +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 B495D106F; Mon, 3 Mar 2025 05:33:19 -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 1C49A3F673; Mon, 3 Mar 2025 05:33:02 -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 v3 4/6] lmb: remove superfluous address overlap check from lmb_add_region_flags() Date: Mon, 3 Mar 2025 19:02:29 +0530 Message-Id: <20250303133231.405279-5-sughosh.ganu@linaro.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20250303133231.405279-1-sughosh.ganu@linaro.org> References: <20250303133231.405279-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 U-Boot allows re-use of already reserved memory through the lmb_reserve() and lmb_alloc_addr() API's. This memory re-use is allowed only when the flag of the existing reserved region and that of the requested region is LMB_NONE. A check was put in the lmb_add_region_flags() in commit 8b8b35a4f5e to handle the scenario where an already reserved region was re-requested with region flag other than LMB_NONE -- the function then returns -EEXIST in such a scenario. The lmb_reserve() function now does a check for a reservation request with existing reserved regions, and returns -EEXIST in case of an overlap but when the flag check fails. Remove this now redundant check from lmb_add_region_flags(). Signed-off-by: Sughosh Ganu --- Changes since V2: * Add more description to the commit message on the logic behind this change lib/lmb.c | 3 --- 1 file changed, 3 deletions(-) diff --git a/lib/lmb.c b/lib/lmb.c index 53af96fa2a9..9af942c6b42 100644 --- a/lib/lmb.c +++ b/lib/lmb.c @@ -195,9 +195,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 Mon Mar 3 13:32:30 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sughosh Ganu X-Patchwork-Id: 869836 Delivered-To: patch@linaro.org Received: by 2002:a05:6000:1561:b0:38f:210b:807b with SMTP id 1csp2122606wrz; Mon, 3 Mar 2025 05:33:54 -0800 (PST) X-Forwarded-Encrypted: i=2; AJvYcCX1Uz00g/hYSjIgngITmz1qt5YtyOtbMQpgCNLzEIFwtBrO4E9Teeg0TzQDJBp/gzAGun621w==@linaro.org X-Google-Smtp-Source: AGHT+IF6PZrrFiGl27TLp9f6cXtJl3hjOkG9s3CO/HEqi4h6620Zbc8nUg0O0EoVTfE6Pp4712iB X-Received: by 2002:a05:6a21:3a93:b0:1e1:aef4:9ce8 with SMTP id adf61e73a8af0-1f2f4dfc2bamr21591891637.28.1741008834262; Mon, 03 Mar 2025 05:33:54 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1741008834; cv=none; d=google.com; s=arc-20240605; b=fnhA8+BHAux46CsC+V1ZPvINRwt3VLPNnhLHRgF59TKdFBabpjWFE9zIX3Cx+vxa5f +zaXvXdcFTcqNpJ658e6ojZZD2mYliidvtsnQQrF+wdSK9Kib2cUP4pLTWQKXXl6Es3G 2YMgjqwb11hKL9aTv+pIadNGARDfVwNOIBAnFL9UsKNV6hwjSmoFGeNRpeKPfjOM5xaE 3gmXwowCacVLAFSBC1rX3djBjhbFIGgleeA6yH7qIVLR7eu1hu4DGSUgBjq8WzXartlF IYnUVhJSgnUrBRgEbum1J5cJ1yt60dBF9ZkI6I3wKmo+yeXevTVFwY9lmPd78yMa7GkC 7GOQ== 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=zZVwjfMpLGMqbNLEna5bPaa00fuo/VrVlTDdvPZTVq0=; fh=2HGKihHUAicyV3F+aHiaoPpwvqWA0zlC04Ew6QaQoEg=; b=eLmo8STk76bztC3VHUfrq59ckDDw7BIJQOEXHLI3L2pqVZTVmNAKTA1wq4mieeef6+ /n0BGrI62WFwnw01jN7SzCk3TQxAMjhNN+gq8Y2ouvIouQ8ltR7R4iXvswdlaKnpjcG4 VNojCF6wvmwvuRICt8+733djU2XQfgTeSRDaWoD4prA5t+73tYo1y69/l7Ij9UblGCEu bYYs4WtaotIXL3ZJcID549n25IKOhPEsgT5Bp038g3ZMEYulSqP3nOB2LoTG9Qtrx1zC c5lVTuiMqiQC/qfIamehPQv6/BfYEqyH/k4vDXmCWbNSn6FNv0yBeQ7UuwjaMAQSa0tc qXNA==; 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 41be03b00d2f7-af249bbbd88si2383498a12.485.2025.03.03.05.33.53 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 03 Mar 2025 05:33:54 -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 A4409812BB; Mon, 3 Mar 2025 14:33:12 +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 9642481280; Mon, 3 Mar 2025 14:33:11 +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_RPBL_BLOCKED, RCVD_IN_VALIDITY_SAFE_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 51B67811B2 for ; Mon, 3 Mar 2025 14:33:09 +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 CC730106F; Mon, 3 Mar 2025 05:33:22 -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 3412B3F673; Mon, 3 Mar 2025 05:33:05 -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 v3 5/6] lmb: use a common function to check if regions overlap or are adjacent Date: Mon, 3 Mar 2025 19:02:30 +0530 Message-Id: <20250303133231.405279-6-sughosh.ganu@linaro.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20250303133231.405279-1-sughosh.ganu@linaro.org> References: <20250303133231.405279-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 V2: None lib/lmb.c | 71 ++++++++++++++++++++++++++++++++----------------------- 1 file changed, 41 insertions(+), 30 deletions(-) diff --git a/lib/lmb.c b/lib/lmb.c index 9af942c6b42..b42a512f6c0 100644 --- a/lib/lmb.c +++ b/lib/lmb.c @@ -23,6 +23,9 @@ DECLARE_GLOBAL_DATA_PTR; +#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 @@ -49,8 +52,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; @@ -58,19 +75,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) @@ -207,23 +220,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 Mon Mar 3 13:32:31 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sughosh Ganu X-Patchwork-Id: 869837 Delivered-To: patch@linaro.org Received: by 2002:a05:6000:1561:b0:38f:210b:807b with SMTP id 1csp2122700wrz; Mon, 3 Mar 2025 05:34:05 -0800 (PST) X-Forwarded-Encrypted: i=2; AJvYcCU2JRrFOIMpbK7MG7CJ6WSHeNXYZMh8mQYobjDHIny91i2gbAbScFdH3OUBFtm3kR51jMxVNg==@linaro.org X-Google-Smtp-Source: AGHT+IGa1ouDCwomh+xo36itmOWYBSjpvPx1dnQbIz9S8gfzkmD7jfBegIYo4/WBfGFG4MCDcuNS X-Received: by 2002:a05:6a21:4d14:b0:1f1:42f:5a3a with SMTP id adf61e73a8af0-1f2e389c8ccmr29046519637.9.1741008845556; Mon, 03 Mar 2025 05:34:05 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1741008845; cv=none; d=google.com; s=arc-20240605; b=OApPxG6r699qJsBvG5w2awfk9ePvIpFxP1gWrj9t+vQAOlQLKyxb9Svvvzd19XRv2e T+VKnKm6IkZ2rpDVmPRVzmI6b6sS/5G2C9dNpa1vQrBNN3KANEErTg3PmkinVB+N2m0n jrEc4RwO5aoLpgCy6H3x6YuPntIpKXlU4byDB3gS0kGfpV14xOuY0iTCDuRv4k5j2prv yJ6pr17X46UkgAx9m3vkis42JAp33nKIzSK9UzaUTKg0C0f1JRRuIcnfLG8y71K/mI+k LW7QU7mSfLLSkMsmdz6Oz+ompAu16IP0VnXOclxFjqLMqjR2e+EcRxaCiburJkFGSwGB QppA== 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=qooCdfkgAt2MOZ8GVriPD2oaFNM3aowBH6Yv+GjMpTg=; fh=2HGKihHUAicyV3F+aHiaoPpwvqWA0zlC04Ew6QaQoEg=; b=EeDTWZJ/STumJit/bMrYusj0GiZErxrzoitdpxumJMnLjfTpQvZEGLn82HR1Bk0vP0 wrFxPrnV9iktF2ZzGpY7OEeHTQhbnF/5ws79/HKopRpG/xVKzc1+QtiEN/q5nGNE7GhO EjsFnJL/ddEkXrHflVzi2no+5KZiUgr5H54s60/hTld3emqVglJe6VuTcOAfQxgALtGH iKRG/KQUvL/hpbQM2VbF3n9e5hiClFvtmyOGTZN95ZBRpK87iYwGCYwHhiFqnAfr7Lbt v16+dgWdVcb5BiKljv4ZwoppyoOJE4/ar6cTS+7y9vuKHqexOQ3xVzgv93ipLtXhuszs TyvQ==; 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 41be03b00d2f7-af1801575desi9013217a12.62.2025.03.03.05.34.04 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 03 Mar 2025 05:34:05 -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 ED9CB81280; Mon, 3 Mar 2025 14:33:14 +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 AC37981335; Mon, 3 Mar 2025 14:33:14 +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_RPBL_BLOCKED, RCVD_IN_VALIDITY_SAFE_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 5F9B681280 for ; Mon, 3 Mar 2025 14:33:12 +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 E3FBD113E; Mon, 3 Mar 2025 05:33:25 -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 4BC763F673; Mon, 3 Mar 2025 05:33:09 -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 v3 6/6] lmb: optimise the lmb allocation functions Date: Mon, 3 Mar 2025 19:02:31 +0530 Message-Id: <20250303133231.405279-7-sughosh.ganu@linaro.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20250303133231.405279-1-sughosh.ganu@linaro.org> References: <20250303133231.405279-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 V2: * Change the print for allocation failure into a debug message as per suggestion from Heinrich lib/lmb.c | 16 ++++++---------- 1 file changed, 6 insertions(+), 10 deletions(-) diff --git a/lib/lmb.c b/lib/lmb.c index b42a512f6c0..981ea1b2ca0 100644 --- a/lib/lmb.c +++ b/lib/lmb.c @@ -724,26 +724,22 @@ static phys_addr_t _lmb_alloc_base(phys_size_t size, ulong align, base = ALIGN_DOWN(res_base - size, align); } } + + log_debug("%s: Failed to allocate 0x%lx bytes below 0x%lx\n", + __func__, (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)