From: Ahmad Fatoum <a.fatoum@pengutronix.de>
To: barebox@lists.infradead.org
Cc: Ahmad Fatoum <a.fatoum@pengutronix.de>
Subject: [PATCH 2/6] test: self: implement resource walker selftest
Date: Thu, 11 Dec 2025 21:50:04 +0100 [thread overview]
Message-ID: <20251211205240.2836186-3-a.fatoum@pengutronix.de> (raw)
In-Reply-To: <20251211205240.2836186-1-a.fatoum@pengutronix.de>
To get some confidence into the new resource iteration code before
making use for it in the EFI loader case, add a selftest that exercises
its normal use.
Signed-off-by: Ahmad Fatoum <a.fatoum@pengutronix.de>
---
test/self/Kconfig | 4 +
test/self/Makefile | 1 +
test/self/resource.c | 189 +++++++++++++++++++++++++++++++++++++++++++
3 files changed, 194 insertions(+)
create mode 100644 test/self/resource.c
diff --git a/test/self/Kconfig b/test/self/Kconfig
index adef8609ef00..2ccdfe621821 100644
--- a/test/self/Kconfig
+++ b/test/self/Kconfig
@@ -45,6 +45,7 @@ config SELFTEST_ENABLE_ALL
select SELFTEST_STRING
select SELFTEST_SETJMP if ARCH_HAS_SJLJ
select SELFTEST_REGULATOR if REGULATOR_FIXED
+ select SELFTEST_RESOURCE
select SELFTEST_TEST_COMMAND if CMD_TEST
select SELFTEST_IDR
select SELFTEST_TLV
@@ -131,6 +132,9 @@ config SELFTEST_REGULATOR
depends on REGULATOR_FIXED
select OF_OVERLAY
+config SELFTEST_RESOURCE
+ bool "Resource selftest"
+
config SELFTEST_TEST_COMMAND
bool "test command selftest"
depends on CMD_TEST
diff --git a/test/self/Makefile b/test/self/Makefile
index d244c190522a..9f839fc0d07a 100644
--- a/test/self/Makefile
+++ b/test/self/Makefile
@@ -20,6 +20,7 @@ obj-$(CONFIG_SELFTEST_MMU) += mmu.o
obj-$(CONFIG_SELFTEST_STRING) += string.o
obj-$(CONFIG_SELFTEST_SETJMP) += setjmp.o
obj-$(CONFIG_SELFTEST_REGULATOR) += regulator.o test_regulator.dtbo.o
+obj-$(CONFIG_SELFTEST_RESOURCE) += resource.o
obj-$(CONFIG_SELFTEST_TEST_COMMAND) += test_command.o
obj-$(CONFIG_SELFTEST_IDR) += idr.o
obj-$(CONFIG_SELFTEST_TLV) += tlv.o tlv.dtb.o
diff --git a/test/self/resource.c b/test/self/resource.c
new file mode 100644
index 000000000000..f9cbed51740e
--- /dev/null
+++ b/test/self/resource.c
@@ -0,0 +1,189 @@
+// SPDX-License-Identifier: GPL-2.0-only
+
+#define pr_fmt(fmt) "test-resource: " fmt
+
+#include <common.h>
+#include <command.h>
+#include <malloc.h>
+#include <linux/ioport.h>
+#include <bselftest.h>
+#include <stdio.h>
+
+struct test_ctx {
+ int calls;
+ struct {
+ resource_size_t start;
+ resource_size_t end;
+ bool is_gap;
+ } rec[64];
+ int fail_on;
+};
+
+BSELFTEST_GLOBALS();
+
+#define __expect_equal(x, y, desc, line) do { \
+ typeof(x) __x = (x); \
+ typeof(y) __y = (y); \
+ total_tests++; \
+ if ((__x) != (__y)) { \
+ failed_tests++; \
+ pr_err("%s:%d failed: %s == %llu, but %llu expected\n", \
+ desc, line, #x, (u64)__x, (u64)__y); \
+ } \
+} while (0)
+
+#define expect_equal(x, y) __expect_equal((x), (y), testname, __LINE__)
+
+static int test_cb(const struct resource *res, void *data)
+{
+ bool is_gap = region_is_gap(res);
+ struct test_ctx *ctx = data;
+
+ if (ctx->fail_on && ctx->calls + 1 == ctx->fail_on) {
+ ctx->calls++;
+ return -EIO;
+ }
+
+ ctx->rec[ctx->calls].start = res->start;
+ ctx->rec[ctx->calls].end = res->end;
+ ctx->rec[ctx->calls].is_gap = is_gap;
+ ctx->calls++;
+
+ return 0;
+}
+
+static void add_res(struct resource *resources,
+ resource_size_t start, resource_size_t end)
+{
+ struct resource *res = xzalloc(sizeof(*res));
+
+ res->start = start;
+ res->end = end;
+ res->parent = resources;
+ list_add_tail(&res->sibling, &resources->children);
+}
+
+static void free_reslist(struct resource *resource)
+{
+ struct resource *r, *tmp;
+
+ list_for_each_entry_safe(r, tmp, &resource->children, sibling)
+ free(r);
+}
+
+static int resource_list_walk(struct resource *parent,
+ int (*cb)(const struct resource *res, void *data),
+ void *data,
+ bool reverse, bool include_gaps)
+{
+ struct resource *res, _gap, *gap = include_gaps ? &_gap : NULL;
+ int ret = 0;
+ struct resource *(*start)(struct resource *, struct resource *);
+ struct resource *(*advance)(struct resource *, struct resource *);
+
+ if (reverse) {
+ start = resource_iter_last;
+ advance = resource_iter_prev;
+ } else {
+ start = resource_iter_first;
+ advance = resource_iter_next;
+ }
+
+ for (res = start(parent, gap); res; res = advance(res, gap)) {
+ ret = cb(res, data);
+ if (ret < 0)
+ return ret;
+ }
+
+ return ret;
+}
+
+static int test_resource_list_walk(const char *testname,
+ resource_size_t start,
+ resource_size_t end)
+{
+ struct resource resources = { .start = start, .end = end };
+ struct test_ctx ctx;
+ int ret, i, subranges = 0;
+
+ INIT_LIST_HEAD(&resources.children);
+
+ add_res(&resources, 100, 199);
+ add_res(&resources, 300, 399);
+ subranges = 3; /* 2 regions and range in-between */
+
+ if (start < 100)
+ subranges++;
+ if (end > 399)
+ subranges++;
+
+ memset(&ctx, 0, sizeof(ctx));
+ ret = resource_list_walk(&resources, test_cb, &ctx,
+ false, true);
+ expect_equal(ret, 0);
+ expect_equal(ctx.calls, subranges);
+
+ i = 0;
+ if (start < 100)
+ expect_equal(ctx.rec[i++].is_gap, true); /* [0–99] */
+ expect_equal(ctx.rec[i++].is_gap, false); /* [100–199] */
+ expect_equal(ctx.rec[i++].is_gap, true); /* [200–299] */
+ expect_equal(ctx.rec[i++].is_gap, false); /* [300–399] */
+ if (end > 399)
+ expect_equal(ctx.rec[i++].is_gap, true); /* [400–500] */
+
+ memset(&ctx, 0, sizeof(ctx));
+ ret = resource_list_walk(&resources, test_cb, &ctx,
+ false, false);
+ expect_equal(ctx.calls, 2);
+ expect_equal(ctx.rec[0].start, 100);
+ expect_equal(ctx.rec[1].start, 300);
+
+ memset(&ctx, 0, sizeof(ctx));
+ ctx.fail_on = 2;
+ ret = resource_list_walk(&resources, test_cb, &ctx,
+ false, true);
+ expect_equal(ret, -EIO);
+ expect_equal(ctx.calls, 2);
+
+ memset(&ctx, 0, sizeof(ctx));
+ ret = resource_list_walk(&resources, test_cb, &ctx,
+ true, true);
+ expect_equal(ret, 0);
+ expect_equal(ctx.calls, subranges);
+
+ i = 0;
+ if (end > 399)
+ expect_equal(ctx.rec[i++].is_gap, true);
+ expect_equal(ctx.rec[i++].is_gap, false);
+ expect_equal(ctx.rec[i++].is_gap, true);
+ expect_equal(ctx.rec[i++].is_gap, false);
+ if (start < 100)
+ expect_equal(ctx.rec[i++].is_gap, true);
+
+ i = 0;
+ if (end > 399)
+ expect_equal(ctx.rec[i++].start, 400);
+ expect_equal(ctx.rec[i++].start, 300);
+ expect_equal(ctx.rec[++i].start, 100);
+
+ memset(&ctx, 0, sizeof(ctx));
+ ret = resource_list_walk(&resources, test_cb, &ctx,
+ true, false);
+ expect_equal(ctx.calls, 2);
+ expect_equal(ctx.rec[0].start, 300);
+ expect_equal(ctx.rec[1].start, 100);
+
+ free_reslist(&resources);
+
+ return 0;
+}
+
+static void test_resources(void)
+{
+ test_resource_list_walk("gap-around", 0, 499);
+ test_resource_list_walk("gap-end", 100, 499);
+ test_resource_list_walk("gap-start", 0, 399);
+ test_resource_list_walk("gap-around-none", 100, 399);
+}
+bselftest(core, test_resources);
--
2.47.3
next prev parent reply other threads:[~2025-12-11 20:53 UTC|newest]
Thread overview: 8+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-12-11 20:50 [PATCH 0/6] resource: add support for walking resource gaps Ahmad Fatoum
2025-12-11 20:50 ` [PATCH 1/6] resource: implement resource walker Ahmad Fatoum
2025-12-11 20:50 ` Ahmad Fatoum [this message]
2025-12-11 20:50 ` [PATCH 3/6] commands: iomem: add support for printing gaps Ahmad Fatoum
2025-12-11 20:50 ` [PATCH 4/6] test: py: add test for valid JSON output from iomem/clk_dump Ahmad Fatoum
2025-12-11 20:50 ` [PATCH 5/6] memory: add helpers for iterating over memory regions Ahmad Fatoum
2025-12-11 20:50 ` [PATCH 6/6] resource: implement release_region_range Ahmad Fatoum
2025-12-15 9:29 ` [PATCH 0/6] resource: add support for walking resource gaps Sascha Hauer
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20251211205240.2836186-3-a.fatoum@pengutronix.de \
--to=a.fatoum@pengutronix.de \
--cc=barebox@lists.infradead.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox