Skip to content

Commit d783ed2

Browse files
Marco PaganiXu Yilun
authored andcommitted
fpga: Simplify and improve fpga region test using deferred actions
Use deferred actions to simplify the test suite and avoid potential memory leaks when test cases fail. Remove unnecessary calls to kunit_device_unregister() since kunit devices are tied to the test context and released by a deferred action when the test is completed. Other changes: fix a typo by changing the test suite name to fpga_region in the kunit_suite struct. Signed-off-by: Marco Pagani <[email protected]> Acked-by: Xu Yilun <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Xu Yilun <[email protected]>
1 parent 3c2c018 commit d783ed2

File tree

1 file changed

+24
-17
lines changed

1 file changed

+24
-17
lines changed

drivers/fpga/tests/fpga-region-test.c

Lines changed: 24 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,19 @@ struct test_ctx {
3535
struct mgr_stats mgr_stats;
3636
};
3737

38+
/*
39+
* Wrappers to avoid cast warnings when passing action functions directly
40+
* to kunit_add_action().
41+
*/
42+
KUNIT_DEFINE_ACTION_WRAPPER(fpga_image_info_free_wrapper, fpga_image_info_free,
43+
struct fpga_image_info *);
44+
45+
KUNIT_DEFINE_ACTION_WRAPPER(fpga_bridge_unregister_wrapper, fpga_bridge_unregister,
46+
struct fpga_bridge *);
47+
48+
KUNIT_DEFINE_ACTION_WRAPPER(fpga_region_unregister_wrapper, fpga_region_unregister,
49+
struct fpga_region *);
50+
3851
static int op_write(struct fpga_manager *mgr, const char *buf, size_t count)
3952
{
4053
struct mgr_stats *stats = mgr->priv;
@@ -111,6 +124,9 @@ static void fpga_region_test_program_fpga(struct kunit *test)
111124
img_info = fpga_image_info_alloc(ctx->mgr_dev);
112125
KUNIT_ASSERT_NOT_ERR_OR_NULL(test, img_info);
113126

127+
ret = kunit_add_action_or_reset(test, fpga_image_info_free_wrapper, img_info);
128+
KUNIT_ASSERT_EQ(test, ret, 0);
129+
114130
img_info->buf = img_buf;
115131
img_info->count = sizeof(img_buf);
116132

@@ -130,8 +146,6 @@ static void fpga_region_test_program_fpga(struct kunit *test)
130146
KUNIT_EXPECT_EQ(test, 2, ctx->bridge_stats.cycles_count);
131147

132148
fpga_bridges_put(&ctx->region->bridge_list);
133-
134-
fpga_image_info_free(img_info);
135149
}
136150

137151
/*
@@ -144,6 +158,7 @@ static int fpga_region_test_init(struct kunit *test)
144158
{
145159
struct test_ctx *ctx;
146160
struct fpga_region_info region_info = { 0 };
161+
int ret;
147162

148163
ctx = kunit_kzalloc(test, sizeof(*ctx), GFP_KERNEL);
149164
KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ctx);
@@ -164,6 +179,9 @@ static int fpga_region_test_init(struct kunit *test)
164179

165180
ctx->bridge_stats.enable = true;
166181

182+
ret = kunit_add_action_or_reset(test, fpga_bridge_unregister_wrapper, ctx->bridge);
183+
KUNIT_ASSERT_EQ(test, ret, 0);
184+
167185
ctx->region_dev = kunit_device_register(test, "fpga-region-test-dev");
168186
KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ctx->region_dev);
169187

@@ -174,34 +192,23 @@ static int fpga_region_test_init(struct kunit *test)
174192
ctx->region = fpga_region_register_full(ctx->region_dev, &region_info);
175193
KUNIT_ASSERT_FALSE(test, IS_ERR_OR_NULL(ctx->region));
176194

195+
ret = kunit_add_action_or_reset(test, fpga_region_unregister_wrapper, ctx->region);
196+
KUNIT_ASSERT_EQ(test, ret, 0);
197+
177198
test->priv = ctx;
178199

179200
return 0;
180201
}
181202

182-
static void fpga_region_test_exit(struct kunit *test)
183-
{
184-
struct test_ctx *ctx = test->priv;
185-
186-
fpga_region_unregister(ctx->region);
187-
kunit_device_unregister(test, ctx->region_dev);
188-
189-
fpga_bridge_unregister(ctx->bridge);
190-
kunit_device_unregister(test, ctx->bridge_dev);
191-
192-
kunit_device_unregister(test, ctx->mgr_dev);
193-
}
194-
195203
static struct kunit_case fpga_region_test_cases[] = {
196204
KUNIT_CASE(fpga_region_test_class_find),
197205
KUNIT_CASE(fpga_region_test_program_fpga),
198206
{}
199207
};
200208

201209
static struct kunit_suite fpga_region_suite = {
202-
.name = "fpga_mgr",
210+
.name = "fpga_region",
203211
.init = fpga_region_test_init,
204-
.exit = fpga_region_test_exit,
205212
.test_cases = fpga_region_test_cases,
206213
};
207214

0 commit comments

Comments
 (0)