Skip to contentSkip to conversation

resource: add a simple test for walk_iomem_res_desc()

PATCH In review Chia-I Wu 2 participants 4 messages Jun 6, 2024

lore
Share & tools
lore

Chia-I Wu

author
#0 lore raw

PATCH v5 1/1 resource: add a simple test for walk_iomem_res_desc()

This mainly tests that find_next_iomem_res() does not miss resources.

---
v2: update subject, use DEFINE_RES_NAMED and hardcoded offsets
v3: really hardcode offsets, with 4KB intervals since 0x1000 is easier

    to read than 0x400

v4: use RESOURCE_SIZE_MAX, split allocate_resource and KUNIT_ASSERT_EQ,
and other cosmetic changes
v5: include linux/limits.h, add a comment on the resource layout, and
add more negative tests for holes

---
 kernel/resource_kunit.c | 99 +++++++++++++++++++++++++++++++++++++++++
 1 file changed, 99 insertions(+)
kernel/resource_kunit.c +99 -0
index 58ab9f914602b..b13f01f290606 100644
@@ -6,6 +6,7 @@
 #include <kunit/test.h>
 #include <linux/ioport.h>
 #include <linux/kernel.h>
+#include <linux/limits.h>
 #include <linux/string.h>
 
 #define R0_START	0x0000
@@ -137,9 +138,107 @@ static void resource_test_intersection(struct kunit *test)
 	} while (++i < ARRAY_SIZE(results_for_intersection));
 }
 
+static int resource_walk_count(struct resource *res, void *data)
+{
+	int *count = data;
+
+	(*count)++;
+	return 0;
+}
+
+static void resource_test_walk_iomem_res_desc(struct kunit *test)
+{
+	struct resource root = {
+		.name = "Resource Walk Test",
+	};
+	struct resource res[8];
+	int count;
+	int ret;
+
+	ret = allocate_resource(&iomem_resource, &root, 0x100000,
+			0, RESOURCE_SIZE_MAX, 0x100000, NULL, NULL);
+	KUNIT_ASSERT_EQ(test, 0, ret);
+
+	/* build the resource tree under the test root:
+	 *
+	 *   0x0000-0x0fff: res[0], a match
+	 *   0x1000-0x1fff: res[1], a non-match
+	 *   0x2000-0x2fff: a hole
+	 *   0x3000-0x3fff: res[2], a non-match
+	 *     0x3800-0x3bff: res[3], a match
+	 *   0x4000-0x4fff: res[4], a match
+	 */
+	res[0] = DEFINE_RES_NAMED(root.start + 0x0000, 0x1000, "SYSRAM 1",
+			IORESOURCE_SYSTEM_RAM);
+	res[1] = DEFINE_RES_NAMED(root.start + 0x1000, 0x1000, "OTHER", 0);
+
+	res[2] = DEFINE_RES_NAMED(root.start + 0x3000, 0x1000, "NESTED", 0);
+	res[3] = DEFINE_RES_NAMED(root.start + 0x3800, 0x0400, "SYSRAM 2",
+			IORESOURCE_SYSTEM_RAM);
+
+	res[4] = DEFINE_RES_NAMED(root.start + 0x4000, 0x1000, "SYSRAM 3",
+			IORESOURCE_SYSTEM_RAM);
+
+	KUNIT_EXPECT_EQ(test, 0, request_resource(&root, &res[0]));
+	KUNIT_EXPECT_EQ(test, 0, request_resource(&root, &res[1]));
+	KUNIT_EXPECT_EQ(test, 0, request_resource(&root, &res[2]));
+	KUNIT_EXPECT_EQ(test, 0, request_resource(&res[2], &res[3]));
+	KUNIT_EXPECT_EQ(test, 0, request_resource(&root, &res[4]));
+
+	/* walk the entire region */
+	count = 0;
+	walk_iomem_res_desc(IORES_DESC_NONE, IORESOURCE_SYSTEM_RAM,
+			root.start, root.end, &count, resource_walk_count);
+	KUNIT_EXPECT_EQ(test, count, 3);
+
+	/* walk the region requested by res[0] */
+	count = 0;
+	walk_iomem_res_desc(IORES_DESC_NONE, IORESOURCE_SYSTEM_RAM,
+			res[0].start, res[0].end, &count, resource_walk_count);
+	KUNIT_EXPECT_EQ(test, count, 1);
+
+	/* walk the region requested by res[1] */
+	count = 0;
+	walk_iomem_res_desc(IORES_DESC_NONE, IORESOURCE_SYSTEM_RAM,
+			res[1].start, res[1].end, &count, resource_walk_count);
+	KUNIT_EXPECT_EQ(test, count, 0);
+
+	/* walk the hole between res[1] and res[2] */
+	count = 0;
+	walk_iomem_res_desc(IORES_DESC_NONE, IORESOURCE_SYSTEM_RAM,
+			res[1].end + 1, res[2].start - 1, &count,
+			resource_walk_count);
+	KUNIT_EXPECT_EQ(test, count, 0);
+
+	/* walk the region requested by res[2] */
+	count = 0;
+	walk_iomem_res_desc(IORES_DESC_NONE, IORESOURCE_SYSTEM_RAM,
+			res[2].start, res[2].end, &count, resource_walk_count);
+	KUNIT_EXPECT_EQ(test, count, 1);
+
+	/* walk the holes before and after res[3] nested under res[2] */
+	count = 0;
+	walk_iomem_res_desc(IORES_DESC_NONE, IORESOURCE_SYSTEM_RAM,
+			res[2].start, res[3].start - 1, &count,
+			resource_walk_count);
+	walk_iomem_res_desc(IORES_DESC_NONE, IORESOURCE_SYSTEM_RAM,
+			res[2].end + 1, res[3].end, &count,
+			resource_walk_count);
+	KUNIT_EXPECT_EQ(test, count, 0);
+
+	/* walk the region requested by res[4] */
+	count = 0;
+	walk_iomem_res_desc(IORES_DESC_NONE, IORESOURCE_SYSTEM_RAM,
+			res[4].start, res[4].end, &count, resource_walk_count);
+	KUNIT_EXPECT_EQ(test, count, 1);
+
+	release_resource(&root);
+}
+
 static struct kunit_case resource_test_cases[] = {
 	KUNIT_CASE(resource_test_union),
 	KUNIT_CASE(resource_test_intersection),
+	KUNIT_CASE(resource_test_walk_iomem_res_desc),
 	{}
 };
 
Signature · 1 line
2.45.1.467.gbab1589fc0-goog
author
#1 lore raw
Quoted 108 lines · Chia-I Wu

This mainly tests that find_next_iomem_res() does not miss resources.

---
v2: update subject, use DEFINE_RES_NAMED and hardcoded offsets
v3: really hardcode offsets, with 4KB intervals since 0x1000 is easier

    to read than 0x400

v4: use RESOURCE_SIZE_MAX, split allocate_resource and KUNIT_ASSERT_EQ,
and other cosmetic changes
v5: include linux/limits.h, add a comment on the resource layout, and
add more negative tests for holes

---
 kernel/resource_kunit.c | 99 +++++++++++++++++++++++++++++++++++++++++
 1 file changed, 99 insertions(+)
kernel/resource_kunit.c +76 -0
index 58ab9f914602b..b13f01f290606 100644
@@ -6,6 +6,7 @@
 #include <kunit/test.h>
 #include <linux/ioport.h>
 #include <linux/kernel.h>
+#include <linux/limits.h>
 #include <linux/string.h>
 
 #define R0_START       0x0000
@@ -137,9 +138,107 @@ static void resource_test_intersection(struct kunit *test)
        } while (++i < ARRAY_SIZE(results_for_intersection));
 }
 
+static int resource_walk_count(struct resource *res, void *data)
+{
+       int *count = data;
+
+       (*count)++;
+       return 0;
+}
+
+static void resource_test_walk_iomem_res_desc(struct kunit *test)
+{
+       struct resource root = {
+               .name = "Resource Walk Test",
+       };
+       struct resource res[8];
+       int count;
+       int ret;
+
+       ret = allocate_resource(&iomem_resource, &root, 0x100000,
+                       0, RESOURCE_SIZE_MAX, 0x100000, NULL, NULL);
+       KUNIT_ASSERT_EQ(test, 0, ret);
+
+       /* build the resource tree under the test root:
+        *
+        *   0x0000-0x0fff: res[0], a match
+        *   0x1000-0x1fff: res[1], a non-match
+        *   0x2000-0x2fff: a hole
+        *   0x3000-0x3fff: res[2], a non-match
+        *     0x3800-0x3bff: res[3], a match
+        *   0x4000-0x4fff: res[4], a match
+        */
+       res[0] = DEFINE_RES_NAMED(root.start + 0x0000, 0x1000, "SYSRAM 1",
+                       IORESOURCE_SYSTEM_RAM);
+       res[1] = DEFINE_RES_NAMED(root.start + 0x1000, 0x1000, "OTHER", 0);
+
+       res[2] = DEFINE_RES_NAMED(root.start + 0x3000, 0x1000, "NESTED", 0);
+       res[3] = DEFINE_RES_NAMED(root.start + 0x3800, 0x0400, "SYSRAM 2",
+                       IORESOURCE_SYSTEM_RAM);
+
+       res[4] = DEFINE_RES_NAMED(root.start + 0x4000, 0x1000, "SYSRAM 3",
+                       IORESOURCE_SYSTEM_RAM);
+
+       KUNIT_EXPECT_EQ(test, 0, request_resource(&root, &res[0]));
+       KUNIT_EXPECT_EQ(test, 0, request_resource(&root, &res[1]));
+       KUNIT_EXPECT_EQ(test, 0, request_resource(&root, &res[2]));
+       KUNIT_EXPECT_EQ(test, 0, request_resource(&res[2], &res[3]));
+       KUNIT_EXPECT_EQ(test, 0, request_resource(&root, &res[4]));
+
+       /* walk the entire region */
+       count = 0;
+       walk_iomem_res_desc(IORES_DESC_NONE, IORESOURCE_SYSTEM_RAM,
+                       root.start, root.end, &count, resource_walk_count);
+       KUNIT_EXPECT_EQ(test, count, 3);
+
+       /* walk the region requested by res[0] */
+       count = 0;
+       walk_iomem_res_desc(IORES_DESC_NONE, IORESOURCE_SYSTEM_RAM,
+                       res[0].start, res[0].end, &count, resource_walk_count);
+       KUNIT_EXPECT_EQ(test, count, 1);
+
+       /* walk the region requested by res[1] */
+       count = 0;
+       walk_iomem_res_desc(IORES_DESC_NONE, IORESOURCE_SYSTEM_RAM,
+                       res[1].start, res[1].end, &count, resource_walk_count);
+       KUNIT_EXPECT_EQ(test, count, 0);
+
+       /* walk the hole between res[1] and res[2] */
+       count = 0;
+       walk_iomem_res_desc(IORES_DESC_NONE, IORESOURCE_SYSTEM_RAM,
+                       res[1].end + 1, res[2].start - 1, &count,
+                       resource_walk_count);
+       KUNIT_EXPECT_EQ(test, count, 0);
+
+       /* walk the region requested by res[2] */
+       count = 0;
+       walk_iomem_res_desc(IORES_DESC_NONE, IORESOURCE_SYSTEM_RAM,

kernel/resource_kunit.c static void resource_test_intersection(struct kunit *test) · line 225

Quoted 10 lines
kernel/resource_kunit.c +10 -0
+                       res[2].start, res[2].end, &count, resource_walk_count);
+       KUNIT_EXPECT_EQ(test, count, 1);
+
+       /* walk the holes before and after res[3] nested under res[2] */
+       count = 0;
+       walk_iomem_res_desc(IORES_DESC_NONE, IORESOURCE_SYSTEM_RAM,
+                       res[2].start, res[3].start - 1, &count,
+                       resource_walk_count);
+       walk_iomem_res_desc(IORES_DESC_NONE, IORESOURCE_SYSTEM_RAM,
+                       res[2].end + 1, res[3].end, &count,

This should be from "res[3].end + 1" to "res[2].end". Not sure if I should resend or if you can make the fix when applying.

Quoted 22 lines
kernel/resource_kunit.c +13 -1
+                       resource_walk_count);
+       KUNIT_EXPECT_EQ(test, count, 0);
+
+       /* walk the region requested by res[4] */
+       count = 0;
+       walk_iomem_res_desc(IORES_DESC_NONE, IORESOURCE_SYSTEM_RAM,
+                       res[4].start, res[4].end, &count, resource_walk_count);
+       KUNIT_EXPECT_EQ(test, count, 1);
+
+       release_resource(&root);
+}
+
 static struct kunit_case resource_test_cases[] = {
        KUNIT_CASE(resource_test_union),
        KUNIT_CASE(resource_test_intersection),
+       KUNIT_CASE(resource_test_walk_iomem_res_desc),
        {}
 };
 
--

2.45.1.467.gbab1589fc0-goog

#2 lore raw
Quoted 3 lines · Chia-I Wu
Quoted 2 lines · Chia-I Wu

This mainly tests that find_next_iomem_res() does not miss resources.

...

Quoted 4 lines

Review

Quoted 2 lines
(quoted diff) +2 -0
+       walk_iomem_res_desc(IORES_DESC_NONE, IORESOURCE_SYSTEM_RAM,
+                       res[2].end + 1, res[3].end, &count,

This should be from "res[3].end + 1" to "res[2].end". Not sure if I should resend or if you can make the fix when applying.

Please, slow down. You sent three (!) versions over a day, this is against the recommendations. And it seems you want to learn a hard way the clear thing:
Hurrying just increases a chance of a mistake.

Signature · 2 lines
With Best Regards,
Andy Shevchenko
#3 lore raw
In reply to Chia-I Wu
Quoted 1 line · Chia-I Wu

This mainly tests that find_next_iomem_res() does not miss resources.

Read Submitting Patches documentation on imperative mood and modify the commit message to follow.

...

kernel/resource_kunit.c static void resource_test_intersection(struct kunit *test) · line 170

Quoted 9 lines
kernel/resource_kunit.c +9 -0
+	/* build the resource tree under the test root:
+	 *
+	 *   0x0000-0x0fff: res[0], a match
+	 *   0x1000-0x1fff: res[1], a non-match
+	 *   0x2000-0x2fff: a hole
+	 *   0x3000-0x3fff: res[2], a non-match
+	 *     0x3800-0x3bff: res[3], a match
+	 *   0x4000-0x4fff: res[4], a match
+	 */

/*
* This is good, but multi-line comment
* style is broken along with English grammar, i.e.
* do not forget that sentences are started with
* a capital letter.
*/

...

Please, do not send a new version this week. Take your time.

Signature · 2 lines
With Best Regards,
Andy Shevchenko