Skip to content

Commit e664932

Browse files
t-8chrobherring
authored andcommitted
of: address: Add kunit test for __of_address_resource_bounds()
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> Link: https://lore.kernel.org/r/20250129-of-address-overflow-v3-1-95d1760ed791@linutronix.de Signed-off-by: Rob Herring (Arm) <robh@kernel.org>
1 parent 3e86e57 commit e664932

File tree

3 files changed

+126
-2
lines changed

3 files changed

+126
-2
lines changed

drivers/of/address.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
#include <linux/string.h>
1717
#include <linux/dma-direct.h> /* for bus_dma_region */
1818

19+
#include <kunit/visibility.h>
20+
1921
/* Uncomment me to enable of_dump_addr() debugging output */
2022
// #define DEBUG
2123

@@ -183,7 +185,7 @@ static u64 of_bus_pci_map(__be32 *addr, const __be32 *range, int na, int ns,
183185

184186
#endif /* CONFIG_PCI */
185187

186-
static int __of_address_resource_bounds(struct resource *r, u64 start, u64 size)
188+
VISIBLE_IF_KUNIT int __of_address_resource_bounds(struct resource *r, u64 start, u64 size)
187189
{
188190
if (overflows_type(start, r->start))
189191
return -EOVERFLOW;
@@ -197,6 +199,7 @@ static int __of_address_resource_bounds(struct resource *r, u64 start, u64 size)
197199

198200
return 0;
199201
}
202+
EXPORT_SYMBOL_IF_KUNIT(__of_address_resource_bounds);
200203

201204
/*
202205
* of_pci_range_to_resource - Create a resource from an of_pci_range

drivers/of/of_private.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,4 +208,8 @@ static void __maybe_unused of_dump_addr(const char *s, const __be32 *addr, int n
208208
static void __maybe_unused of_dump_addr(const char *s, const __be32 *addr, int na) { }
209209
#endif
210210

211+
#if IS_ENABLED(CONFIG_KUNIT)
212+
int __of_address_resource_bounds(struct resource *r, u64 start, u64 size);
213+
#endif
214+
211215
#endif /* _LINUX_OF_PRIVATE_H */

drivers/of/of_test.c

Lines changed: 118 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
/*
33
* KUnit tests for OF APIs
44
*/
5+
#include <linux/ioport.h>
56
#include <linux/module.h>
67
#include <linux/of.h>
78

@@ -54,8 +55,124 @@ static struct kunit_suite of_dtb_suite = {
5455
.init = of_dtb_test_init,
5556
};
5657

58+
struct of_address_resource_bounds_case {
59+
u64 start;
60+
u64 size;
61+
int ret;
62+
63+
u64 res_start;
64+
u64 res_end;
65+
};
66+
67+
static void of_address_resource_bounds_case_desc(const struct of_address_resource_bounds_case *p,
68+
char *name)
69+
{
70+
snprintf(name, KUNIT_PARAM_DESC_SIZE, "start=0x%016llx,size=0x%016llx", p->start, p->size);
71+
}
72+
73+
static const struct of_address_resource_bounds_case of_address_resource_bounds_cases[] = {
74+
{
75+
.start = 0,
76+
.size = 0,
77+
.ret = 0,
78+
.res_start = 0,
79+
.res_end = -1,
80+
},
81+
{
82+
.start = 0,
83+
.size = 0x1000,
84+
.ret = 0,
85+
.res_start = 0,
86+
.res_end = 0xfff,
87+
},
88+
{
89+
.start = 0x1000,
90+
.size = 0,
91+
.ret = 0,
92+
.res_start = 0x1000,
93+
.res_end = 0xfff,
94+
},
95+
{
96+
.start = 0x1000,
97+
.size = 0x1000,
98+
.ret = 0,
99+
.res_start = 0x1000,
100+
.res_end = 0x1fff,
101+
},
102+
{
103+
.start = 1,
104+
.size = RESOURCE_SIZE_MAX,
105+
.ret = 0,
106+
.res_start = 1,
107+
.res_end = RESOURCE_SIZE_MAX,
108+
},
109+
{
110+
.start = RESOURCE_SIZE_MAX,
111+
.size = 1,
112+
.ret = 0,
113+
.res_start = RESOURCE_SIZE_MAX,
114+
.res_end = RESOURCE_SIZE_MAX,
115+
},
116+
{
117+
.start = 2,
118+
.size = RESOURCE_SIZE_MAX,
119+
.ret = -EOVERFLOW,
120+
},
121+
{
122+
.start = RESOURCE_SIZE_MAX,
123+
.size = 2,
124+
.ret = -EOVERFLOW,
125+
},
126+
{
127+
.start = ULL(0x100000000),
128+
.size = 1,
129+
.ret = sizeof(resource_size_t) > sizeof(u32) ? 0 : -EOVERFLOW,
130+
.res_start = ULL(0x100000000),
131+
.res_end = ULL(0x100000000),
132+
},
133+
{
134+
.start = 0x1000,
135+
.size = 0xffffffff,
136+
.ret = sizeof(resource_size_t) > sizeof(u32) ? 0 : -EOVERFLOW,
137+
.res_start = 0x1000,
138+
.res_end = ULL(0x100000ffe),
139+
},
140+
};
141+
142+
KUNIT_ARRAY_PARAM(of_address_resource_bounds,
143+
of_address_resource_bounds_cases, of_address_resource_bounds_case_desc);
144+
145+
static void of_address_resource_bounds(struct kunit *test)
146+
{
147+
const struct of_address_resource_bounds_case *param = test->param_value;
148+
struct resource r; /* Intentionally uninitialized */
149+
int ret;
150+
151+
if (!IS_ENABLED(CONFIG_OF_ADDRESS))
152+
kunit_skip(test, "CONFIG_OF_ADDRESS not enabled\n");
153+
154+
ret = __of_address_resource_bounds(&r, param->start, param->size);
155+
KUNIT_EXPECT_EQ(test, param->ret, ret);
156+
if (ret == 0) {
157+
KUNIT_EXPECT_EQ(test, (resource_size_t)param->res_start, r.start);
158+
KUNIT_EXPECT_EQ(test, (resource_size_t)param->res_end, r.end);
159+
KUNIT_EXPECT_EQ(test, param->size, resource_size(&r));
160+
}
161+
}
162+
163+
static struct kunit_case of_address_test_cases[] = {
164+
KUNIT_CASE_PARAM(of_address_resource_bounds, of_address_resource_bounds_gen_params),
165+
{}
166+
};
167+
168+
static struct kunit_suite of_address_suite = {
169+
.name = "of_address",
170+
.test_cases = of_address_test_cases,
171+
};
172+
57173
kunit_test_suites(
58-
&of_dtb_suite,
174+
&of_dtb_suite, &of_address_suite,
59175
);
60176
MODULE_DESCRIPTION("KUnit tests for OF APIs");
177+
MODULE_IMPORT_NS("EXPORTED_FOR_KUNIT_TESTING");
61178
MODULE_LICENSE("GPL");

0 commit comments

Comments
 (0)