Skip to content

Commit 3c2c018

Browse files
Marco PaganiXu Yilun
authored andcommitted
fpga: Simplify and improve fpga bridge 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. 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 dbcbd36 commit 3c2c018

File tree

1 file changed

+11
-14
lines changed

1 file changed

+11
-14
lines changed

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

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,13 @@ struct bridge_ctx {
2323
struct bridge_stats stats;
2424
};
2525

26+
/*
27+
* Wrapper to avoid a cast warning when passing the action function directly
28+
* to kunit_add_action().
29+
*/
30+
KUNIT_DEFINE_ACTION_WRAPPER(fpga_bridge_unregister_wrapper, fpga_bridge_unregister,
31+
struct fpga_bridge *);
32+
2633
static int op_enable_set(struct fpga_bridge *bridge, bool enable)
2734
{
2835
struct bridge_stats *stats = bridge->priv;
@@ -50,6 +57,7 @@ static const struct fpga_bridge_ops fake_bridge_ops = {
5057
static struct bridge_ctx *register_test_bridge(struct kunit *test, const char *dev_name)
5158
{
5259
struct bridge_ctx *ctx;
60+
int ret;
5361

5462
ctx = kunit_kzalloc(test, sizeof(*ctx), GFP_KERNEL);
5563
KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ctx);
@@ -61,13 +69,10 @@ static struct bridge_ctx *register_test_bridge(struct kunit *test, const char *d
6169
&ctx->stats);
6270
KUNIT_ASSERT_FALSE(test, IS_ERR_OR_NULL(ctx->bridge));
6371

64-
return ctx;
65-
}
72+
ret = kunit_add_action_or_reset(test, fpga_bridge_unregister_wrapper, ctx->bridge);
73+
KUNIT_ASSERT_EQ(test, ret, 0);
6674

67-
static void unregister_test_bridge(struct kunit *test, struct bridge_ctx *ctx)
68-
{
69-
fpga_bridge_unregister(ctx->bridge);
70-
kunit_device_unregister(test, ctx->dev);
75+
return ctx;
7176
}
7277

7378
static void fpga_bridge_test_get(struct kunit *test)
@@ -141,8 +146,6 @@ static void fpga_bridge_test_get_put_list(struct kunit *test)
141146
fpga_bridges_put(&bridge_list);
142147

143148
KUNIT_EXPECT_TRUE(test, list_empty(&bridge_list));
144-
145-
unregister_test_bridge(test, ctx_1);
146149
}
147150

148151
static int fpga_bridge_test_init(struct kunit *test)
@@ -152,11 +155,6 @@ static int fpga_bridge_test_init(struct kunit *test)
152155
return 0;
153156
}
154157

155-
static void fpga_bridge_test_exit(struct kunit *test)
156-
{
157-
unregister_test_bridge(test, test->priv);
158-
}
159-
160158
static struct kunit_case fpga_bridge_test_cases[] = {
161159
KUNIT_CASE(fpga_bridge_test_get),
162160
KUNIT_CASE(fpga_bridge_test_toggle),
@@ -167,7 +165,6 @@ static struct kunit_case fpga_bridge_test_cases[] = {
167165
static struct kunit_suite fpga_bridge_suite = {
168166
.name = "fpga_bridge",
169167
.init = fpga_bridge_test_init,
170-
.exit = fpga_bridge_test_exit,
171168
.test_cases = fpga_bridge_test_cases,
172169
};
173170

0 commit comments

Comments
 (0)