[PATCH v2] of: address: Add kunit test for __of_address_resource_bounds()

Thomas Weißschuh posted 1 patch 2 days, 23 hours ago
There is a newer version of this series
drivers/of/address.c    |   5 +-
drivers/of/of_private.h |   4 ++
drivers/of/of_test.c    | 121 +++++++++++++++++++++++++++++++++++++++++++++++-
3 files changed, 128 insertions(+), 2 deletions(-)
[PATCH v2] of: address: Add kunit test for __of_address_resource_bounds()
Posted by Thomas Weißschuh 2 days, 23 hours ago
The overflow checking has to deal with different datatypes and
edgecases. Add a new kunit testcase to make sure it works correctly.

Signed-off-by: Thomas Weißschuh <thomas.weissschuh@linutronix.de>
---
Changes in v2:
- Rebase on robh/for-master
- Drop already applied patch
- Add missing MODULE_IMPORT_NS()
- Fix sparse warnings: "cast truncates bits from constant value"
- Link to v1: https://lore.kernel.org/r/20250120-of-address-overflow-v1-0-dd68dbf47bce@linutronix.de
---
Technically it's possible to run this unittest with !CONFIG_OF_ADDRESS,
so there is an explicit check inside the test.
It would also be possible to add a dedicated source file, but that
seemed like a lot of churn to me.
---
 drivers/of/address.c    |   5 +-
 drivers/of/of_private.h |   4 ++
 drivers/of/of_test.c    | 121 +++++++++++++++++++++++++++++++++++++++++++++++-
 3 files changed, 128 insertions(+), 2 deletions(-)

diff --git a/drivers/of/address.c b/drivers/of/address.c
index 26f7fc3d759976539dbd05fb07fe25b27ae4faa7..4976cdf33dd4d81b0de6b979bb79bce734634af5 100644
--- a/drivers/of/address.c
+++ b/drivers/of/address.c
@@ -16,6 +16,8 @@
 #include <linux/string.h>
 #include <linux/dma-direct.h> /* for bus_dma_region */
 
+#include <kunit/visibility.h>
+
 /* Uncomment me to enable of_dump_addr() debugging output */
 // #define DEBUG
 
@@ -183,7 +185,7 @@ static u64 of_bus_pci_map(__be32 *addr, const __be32 *range, int na, int ns,
 
 #endif /* CONFIG_PCI */
 
-static int __of_address_resource_bounds(struct resource *r, u64 start, u64 size)
+VISIBLE_IF_KUNIT int __of_address_resource_bounds(struct resource *r, u64 start, u64 size)
 {
 	if (overflows_type(start, r->start))
 		return -EOVERFLOW;
@@ -197,6 +199,7 @@ static int __of_address_resource_bounds(struct resource *r, u64 start, u64 size)
 
 	return 0;
 }
+EXPORT_SYMBOL_IF_KUNIT(__of_address_resource_bounds);
 
 /*
  * of_pci_range_to_resource - Create a resource from an of_pci_range
diff --git a/drivers/of/of_private.h b/drivers/of/of_private.h
index f3e1193c8ded4899f39677a76da073e2266a1b9a..1bdc7ceef3c5fc854bd7708a50281bbfa439838d 100644
--- a/drivers/of/of_private.h
+++ b/drivers/of/of_private.h
@@ -208,4 +208,8 @@ static void __maybe_unused of_dump_addr(const char *s, const __be32 *addr, int n
 static void __maybe_unused of_dump_addr(const char *s, const __be32 *addr, int na) { }
 #endif
 
+#if IS_ENABLED(CONFIG_KUNIT)
+int __of_address_resource_bounds(struct resource *r, u64 start, u64 size);
+#endif
+
 #endif /* _LINUX_OF_PRIVATE_H */
diff --git a/drivers/of/of_test.c b/drivers/of/of_test.c
index b0557ded838fdf70f0b679c31ead38f501371304..d86270455be8562d3100c6dca8986ab4bb661dbe 100644
--- a/drivers/of/of_test.c
+++ b/drivers/of/of_test.c
@@ -2,6 +2,7 @@
 /*
  * KUnit tests for OF APIs
  */
+#include <linux/ioport.h>
 #include <linux/module.h>
 #include <linux/of.h>
 
@@ -54,8 +55,126 @@ static struct kunit_suite of_dtb_suite = {
 	.init = of_dtb_test_init,
 };
 
+struct of_address_resource_bounds_case {
+	u64 start;
+	u64 size;
+	int ret;
+
+	resource_size_t res_start;
+	resource_size_t res_end;
+};
+
+static void of_address_resource_bounds_case_desc(const struct of_address_resource_bounds_case *p,
+						 char *name)
+{
+	snprintf(name, KUNIT_PARAM_DESC_SIZE, "start=0x%016llx,size=0x%016llx", p->start, p->size);
+}
+
+#define resource_size_32bit() (sizeof(resource_size_t) == sizeof(u32))
+
+static const struct of_address_resource_bounds_case of_address_resource_bounds_cases[] = {
+	{
+		.start = 0,
+		.size = 0,
+		.ret = 0,
+		.res_start = 0,
+		.res_end = -1,
+	},
+	{
+		.start = 0,
+		.size = 0x1000,
+		.ret = 0,
+		.res_start = 0,
+		.res_end = 0xfff,
+	},
+	{
+		.start = 0x1000,
+		.size = 0,
+		.ret = 0,
+		.res_start = 0x1000,
+		.res_end = 0xfff,
+	},
+	{
+		.start = 0x1000,
+		.size = 0x1000,
+		.ret = 0,
+		.res_start = 0x1000,
+		.res_end = 0x1fff,
+	},
+	{
+		.start = 1,
+		.size = RESOURCE_SIZE_MAX,
+		.ret = 0,
+		.res_start = 1,
+		.res_end = RESOURCE_SIZE_MAX,
+	},
+	{
+		.start = RESOURCE_SIZE_MAX,
+		.size = 1,
+		.ret = 0,
+		.res_start = RESOURCE_SIZE_MAX,
+		.res_end = RESOURCE_SIZE_MAX,
+	},
+	{
+		.start = 2,
+		.size = RESOURCE_SIZE_MAX,
+		.ret = -EOVERFLOW,
+	},
+	{
+		.start = RESOURCE_SIZE_MAX,
+		.size = 2,
+		.ret = -EOVERFLOW,
+	},
+	{
+		.start = 0x100000000ULL,
+		.size = 1,
+		.ret = resource_size_32bit() ? -EOVERFLOW : 0,
+		.res_start = resource_size_32bit() ? 0 : 0x100000000,
+		.res_end = resource_size_32bit() ? 0 : 0x100000000,
+	},
+	{
+		.start = 0x1000,
+		.size = 0xffffffff,
+		.ret = resource_size_32bit() ? -EOVERFLOW : 0,
+		.res_start = 0x1000,
+		.res_end = resource_size_32bit() ? 0 : 0x100000ffe,
+	},
+};
+
+KUNIT_ARRAY_PARAM(of_address_resource_bounds,
+		  of_address_resource_bounds_cases, of_address_resource_bounds_case_desc);
+
+static void of_address_resource_bounds(struct kunit *test)
+{
+	const struct of_address_resource_bounds_case *param = test->param_value;
+	struct resource r; /* Intentionally uninitialized */
+	int ret;
+
+	if (!IS_ENABLED(CONFIG_OF_ADDRESS))
+		kunit_skip(test, "CONFIG_OF_ADDRESS not enabled\n");
+
+	ret = __of_address_resource_bounds(&r, param->start, param->size);
+	KUNIT_EXPECT_EQ(test, param->ret, ret);
+	if (ret == 0) {
+		KUNIT_EXPECT_EQ(test, param->res_start, r.start);
+		KUNIT_EXPECT_EQ(test, param->res_end, r.end);
+		KUNIT_EXPECT_EQ(test, param->size, resource_size(&r));
+	}
+}
+
+static struct kunit_case of_address_test_cases[] = {
+	KUNIT_CASE_PARAM(of_address_resource_bounds, of_address_resource_bounds_gen_params),
+	{}
+};
+
+static struct kunit_suite of_address_suite = {
+	.name = "of_address",
+	.test_cases = of_address_test_cases,
+};
+
 kunit_test_suites(
-	&of_dtb_suite,
+	&of_dtb_suite, &of_address_suite,
 );
 MODULE_DESCRIPTION("KUnit tests for OF APIs");
+MODULE_IMPORT_NS("EXPORTED_FOR_KUNIT_TESTING");
 MODULE_LICENSE("GPL");

---
base-commit: 15e2f65f2ecfeb8e39315522e2b5cfdc5651fc10
change-id: 20250120-of-address-overflow-a59476362885

Best regards,
-- 
Thomas Weißschuh <thomas.weissschuh@linutronix.de>

Re: [PATCH v2] of: address: Add kunit test for __of_address_resource_bounds()
Posted by kernel test robot 2 days, 21 hours ago
Hi Thomas,

kernel test robot noticed the following build warnings:

[auto build test WARNING on 15e2f65f2ecfeb8e39315522e2b5cfdc5651fc10]

url:    https://github.com/intel-lab-lkp/linux/commits/Thomas-Wei-schuh/of-address-Add-kunit-test-for-__of_address_resource_bounds/20250127-165902
base:   15e2f65f2ecfeb8e39315522e2b5cfdc5651fc10
patch link:    https://lore.kernel.org/r/20250127-of-address-overflow-v2-1-61b5046044e9%40linutronix.de
patch subject: [PATCH v2] of: address: Add kunit test for __of_address_resource_bounds()
config: hexagon-randconfig-001-20250127 (https://download.01.org/0day-ci/archive/20250127/202501271803.wd0vg8zR-lkp@intel.com/config)
compiler: clang version 17.0.6 (https://github.com/llvm/llvm-project 6009708b4367171ccdbf4b5905cb6a803753fe18)
reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20250127/202501271803.wd0vg8zR-lkp@intel.com/reproduce)

If you fix the issue in a separate patch/commit (i.e. not just a new version of
the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <lkp@intel.com>
| Closes: https://lore.kernel.org/oe-kbuild-all/202501271803.wd0vg8zR-lkp@intel.com/

All warnings (new ones prefixed by >>):

>> drivers/of/of_test.c:140:42: warning: implicit conversion from 'long long' to 'resource_size_t' (aka 'unsigned int') changes value from 4294971390 to 4094 [-Wconstant-conversion]
     135 |         {
         |         ~
     136 |                 .start = 0x1000,
     137 |                 .size = 0xffffffff,
     138 |                 .ret = resource_size_32bit() ? -EOVERFLOW : 0,
     139 |                 .res_start = 0x1000,
     140 |                 .res_end = resource_size_32bit() ? 0 : 0x100000ffe,
         |                                                        ^~~~~~~~~~~
   drivers/of/of_test.c:133:42: warning: implicit conversion from 'long long' to 'resource_size_t' (aka 'unsigned int') changes value from 4294967296 to 0 [-Wconstant-conversion]
     128 |         {
         |         ~
     129 |                 .start = 0x100000000ULL,
     130 |                 .size = 1,
     131 |                 .ret = resource_size_32bit() ? -EOVERFLOW : 0,
     132 |                 .res_start = resource_size_32bit() ? 0 : 0x100000000,
     133 |                 .res_end = resource_size_32bit() ? 0 : 0x100000000,
         |                                                        ^~~~~~~~~~~
   drivers/of/of_test.c:132:44: warning: implicit conversion from 'long long' to 'resource_size_t' (aka 'unsigned int') changes value from 4294967296 to 0 [-Wconstant-conversion]
     128 |         {
         |         ~
     129 |                 .start = 0x100000000ULL,
     130 |                 .size = 1,
     131 |                 .ret = resource_size_32bit() ? -EOVERFLOW : 0,
     132 |                 .res_start = resource_size_32bit() ? 0 : 0x100000000,
         |                                                          ^~~~~~~~~~~
   3 warnings generated.


vim +140 drivers/of/of_test.c

    74	
    75	static const struct of_address_resource_bounds_case of_address_resource_bounds_cases[] = {
    76		{
    77			.start = 0,
    78			.size = 0,
    79			.ret = 0,
    80			.res_start = 0,
    81			.res_end = -1,
    82		},
    83		{
    84			.start = 0,
    85			.size = 0x1000,
    86			.ret = 0,
    87			.res_start = 0,
    88			.res_end = 0xfff,
    89		},
    90		{
    91			.start = 0x1000,
    92			.size = 0,
    93			.ret = 0,
    94			.res_start = 0x1000,
    95			.res_end = 0xfff,
    96		},
    97		{
    98			.start = 0x1000,
    99			.size = 0x1000,
   100			.ret = 0,
   101			.res_start = 0x1000,
   102			.res_end = 0x1fff,
   103		},
   104		{
   105			.start = 1,
   106			.size = RESOURCE_SIZE_MAX,
   107			.ret = 0,
   108			.res_start = 1,
   109			.res_end = RESOURCE_SIZE_MAX,
   110		},
   111		{
   112			.start = RESOURCE_SIZE_MAX,
   113			.size = 1,
   114			.ret = 0,
   115			.res_start = RESOURCE_SIZE_MAX,
   116			.res_end = RESOURCE_SIZE_MAX,
   117		},
   118		{
   119			.start = 2,
   120			.size = RESOURCE_SIZE_MAX,
   121			.ret = -EOVERFLOW,
   122		},
   123		{
   124			.start = RESOURCE_SIZE_MAX,
   125			.size = 2,
   126			.ret = -EOVERFLOW,
   127		},
   128		{
   129			.start = 0x100000000ULL,
   130			.size = 1,
   131			.ret = resource_size_32bit() ? -EOVERFLOW : 0,
   132			.res_start = resource_size_32bit() ? 0 : 0x100000000,
   133			.res_end = resource_size_32bit() ? 0 : 0x100000000,
   134		},
   135		{
   136			.start = 0x1000,
   137			.size = 0xffffffff,
   138			.ret = resource_size_32bit() ? -EOVERFLOW : 0,
   139			.res_start = 0x1000,
 > 140			.res_end = resource_size_32bit() ? 0 : 0x100000ffe,
   141		},
   142	};
   143	

-- 
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki