Skip to content

Commit 6b5cca7

Browse files
Jinjie Ruanbebarino
authored andcommitted
clk: test: Fix some memory leaks
CONFIG_CLK_KUNIT_TEST=y, CONFIG_DEBUG_KMEMLEAK=y and CONFIG_DEBUG_KMEMLEAK_AUTO_SCAN=y, the following memory leak occurs. If the KUNIT_ASSERT_*() fails, the latter (exit() or testcases) clk_put() or clk_hw_unregister() will fail to release the clk resource and cause memory leaks, use new clk_hw_register_kunit() and clk_hw_get_clk_kunit() to automatically release them. unreferenced object 0xffffff80c6af5000 (size 512): comm "kunit_try_catch", pid 371, jiffies 4294896001 hex dump (first 32 bytes): 20 4c c0 86 e1 ff ff ff e0 1a c0 86 e1 ff ff ff L.............. c0 75 e3 c6 80 ff ff ff 00 00 00 00 00 00 00 00 .u.............. backtrace (crc 8ca788fa): [<00000000e21852d0>] kmemleak_alloc+0x34/0x40 [<000000009c583f7b>] __kmalloc_cache_noprof+0x26c/0x2f4 [<00000000d1bc850c>] __clk_register+0x80/0x1ecc [<00000000b08c78c5>] clk_hw_register+0xc4/0x110 [<00000000b16d6df8>] clk_multiple_parents_mux_test_init+0x238/0x288 [<0000000014a7e804>] kunit_try_run_case+0x10c/0x3ac [<0000000026b41f03>] kunit_generic_run_threadfn_adapter+0x80/0xec [<0000000066619fb8>] kthread+0x2e8/0x374 [<00000000a1157f53>] ret_from_fork+0x10/0x20 unreferenced object 0xffffff80c6e37880 (size 96): comm "kunit_try_catch", pid 371, jiffies 4294896002 hex dump (first 32 bytes): 00 50 af c6 80 ff ff ff 00 00 00 00 00 00 00 00 .P.............. 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ backtrace (crc b4b766dd): [<00000000e21852d0>] kmemleak_alloc+0x34/0x40 [<000000009c583f7b>] __kmalloc_cache_noprof+0x26c/0x2f4 [<0000000086e7dd64>] clk_hw_create_clk.part.0.isra.0+0x58/0x2f4 [<00000000dcf1ac31>] clk_hw_get_clk+0x8c/0x114 [<000000006fab5bfa>] clk_test_multiple_parents_mux_set_range_set_parent_get_rate+0x3c/0xa0 [<00000000c97db55a>] kunit_try_run_case+0x13c/0x3ac [<0000000026b41f03>] kunit_generic_run_threadfn_adapter+0x80/0xec [<0000000066619fb8>] kthread+0x2e8/0x374 [<00000000a1157f53>] ret_from_fork+0x10/0x20 unreferenced object 0xffffff80c2b56900 (size 96): comm "kunit_try_catch", pid 395, jiffies 4294896107 hex dump (first 32 bytes): 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ 00 00 00 00 00 00 00 00 e0 49 c0 86 e1 ff ff ff .........I...... backtrace (crc 2e59b327): [<00000000e21852d0>] kmemleak_alloc+0x34/0x40 [<00000000c6c715a8>] __kmalloc_noprof+0x2bc/0x3c0 [<00000000f04a7951>] __clk_register+0x70c/0x1ecc [<00000000b08c78c5>] clk_hw_register+0xc4/0x110 [<00000000cafa9563>] clk_orphan_transparent_multiple_parent_mux_test_init+0x1a8/0x1dc [<0000000014a7e804>] kunit_try_run_case+0x10c/0x3ac [<0000000026b41f03>] kunit_generic_run_threadfn_adapter+0x80/0xec [<0000000066619fb8>] kthread+0x2e8/0x374 [<00000000a1157f53>] ret_from_fork+0x10/0x20 unreferenced object 0xffffff80c87c9400 (size 512): comm "kunit_try_catch", pid 483, jiffies 4294896907 hex dump (first 32 bytes): a0 44 c0 86 e1 ff ff ff e0 1a c0 86 e1 ff ff ff .D.............. 20 05 a8 c8 80 ff ff ff 00 00 00 00 00 00 00 00 ............... backtrace (crc c25b43f): [<00000000e21852d0>] kmemleak_alloc+0x34/0x40 [<000000009c583f7b>] __kmalloc_cache_noprof+0x26c/0x2f4 [<00000000d1bc850c>] __clk_register+0x80/0x1ecc [<00000000b08c78c5>] clk_hw_register+0xc4/0x110 [<000000002688be48>] clk_single_parent_mux_test_init+0x1a0/0x1d4 [<0000000014a7e804>] kunit_try_run_case+0x10c/0x3ac [<0000000026b41f03>] kunit_generic_run_threadfn_adapter+0x80/0xec [<0000000066619fb8>] kthread+0x2e8/0x374 [<00000000a1157f53>] ret_from_fork+0x10/0x20 unreferenced object 0xffffff80c6dd2380 (size 96): comm "kunit_try_catch", pid 483, jiffies 4294896908 hex dump (first 32 bytes): 00 94 7c c8 80 ff ff ff 00 00 00 00 00 00 00 00 ..|............. 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ backtrace (crc 4401212): [<00000000e21852d0>] kmemleak_alloc+0x34/0x40 [<000000009c583f7b>] __kmalloc_cache_noprof+0x26c/0x2f4 [<0000000086e7dd64>] clk_hw_create_clk.part.0.isra.0+0x58/0x2f4 [<00000000dcf1ac31>] clk_hw_get_clk+0x8c/0x114 [<0000000063eb2c90>] clk_test_single_parent_mux_set_range_disjoint_child_last+0x3c/0xa0 [<00000000c97db55a>] kunit_try_run_case+0x13c/0x3ac [<0000000026b41f03>] kunit_generic_run_threadfn_adapter+0x80/0xec [<0000000066619fb8>] kthread+0x2e8/0x374 [<00000000a1157f53>] ret_from_fork+0x10/0x20 ...... Fixes: 02cdeac ("clk: tests: Add tests for single parent mux") Fixes: 2e9cad1 ("clk: tests: Add some tests for orphan with multiple parents") Fixes: 433fb8a ("clk: tests: Add missing test case for ranges") Signed-off-by: Jinjie Ruan <ruanjinjie@huawei.com> Link: https://lore.kernel.org/r/20241016022658.2131826-1-ruanjinjie@huawei.com Reviewed-by: Maxime Ripard <mripard@kernel.org> Signed-off-by: Stephen Boyd <sboyd@kernel.org>
1 parent 23dbbe8 commit 6b5cca7

File tree

1 file changed

+14
-47
lines changed

1 file changed

+14
-47
lines changed

drivers/clk/clk_test.c

Lines changed: 14 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -473,39 +473,29 @@ clk_multiple_parents_mux_test_init(struct kunit *test)
473473
&clk_dummy_rate_ops,
474474
0);
475475
ctx->parents_ctx[0].rate = DUMMY_CLOCK_RATE_1;
476-
ret = clk_hw_register(NULL, &ctx->parents_ctx[0].hw);
476+
ret = clk_hw_register_kunit(test, NULL, &ctx->parents_ctx[0].hw);
477477
if (ret)
478478
return ret;
479479

480480
ctx->parents_ctx[1].hw.init = CLK_HW_INIT_NO_PARENT("parent-1",
481481
&clk_dummy_rate_ops,
482482
0);
483483
ctx->parents_ctx[1].rate = DUMMY_CLOCK_RATE_2;
484-
ret = clk_hw_register(NULL, &ctx->parents_ctx[1].hw);
484+
ret = clk_hw_register_kunit(test, NULL, &ctx->parents_ctx[1].hw);
485485
if (ret)
486486
return ret;
487487

488488
ctx->current_parent = 0;
489489
ctx->hw.init = CLK_HW_INIT_PARENTS("test-mux", parents,
490490
&clk_multiple_parents_mux_ops,
491491
CLK_SET_RATE_PARENT);
492-
ret = clk_hw_register(NULL, &ctx->hw);
492+
ret = clk_hw_register_kunit(test, NULL, &ctx->hw);
493493
if (ret)
494494
return ret;
495495

496496
return 0;
497497
}
498498

499-
static void
500-
clk_multiple_parents_mux_test_exit(struct kunit *test)
501-
{
502-
struct clk_multiple_parent_ctx *ctx = test->priv;
503-
504-
clk_hw_unregister(&ctx->hw);
505-
clk_hw_unregister(&ctx->parents_ctx[0].hw);
506-
clk_hw_unregister(&ctx->parents_ctx[1].hw);
507-
}
508-
509499
/*
510500
* Test that for a clock with multiple parents, clk_get_parent()
511501
* actually returns the current one.
@@ -561,18 +551,18 @@ clk_test_multiple_parents_mux_set_range_set_parent_get_rate(struct kunit *test)
561551
{
562552
struct clk_multiple_parent_ctx *ctx = test->priv;
563553
struct clk_hw *hw = &ctx->hw;
564-
struct clk *clk = clk_hw_get_clk(hw, NULL);
554+
struct clk *clk = clk_hw_get_clk_kunit(test, hw, NULL);
565555
struct clk *parent1, *parent2;
566556
unsigned long rate;
567557
int ret;
568558

569559
kunit_skip(test, "This needs to be fixed in the core.");
570560

571-
parent1 = clk_hw_get_clk(&ctx->parents_ctx[0].hw, NULL);
561+
parent1 = clk_hw_get_clk_kunit(test, &ctx->parents_ctx[0].hw, NULL);
572562
KUNIT_ASSERT_NOT_ERR_OR_NULL(test, parent1);
573563
KUNIT_ASSERT_TRUE(test, clk_is_match(clk_get_parent(clk), parent1));
574564

575-
parent2 = clk_hw_get_clk(&ctx->parents_ctx[1].hw, NULL);
565+
parent2 = clk_hw_get_clk_kunit(test, &ctx->parents_ctx[1].hw, NULL);
576566
KUNIT_ASSERT_NOT_ERR_OR_NULL(test, parent2);
577567

578568
ret = clk_set_rate(parent1, DUMMY_CLOCK_RATE_1);
@@ -593,10 +583,6 @@ clk_test_multiple_parents_mux_set_range_set_parent_get_rate(struct kunit *test)
593583
KUNIT_ASSERT_GT(test, rate, 0);
594584
KUNIT_EXPECT_GE(test, rate, DUMMY_CLOCK_RATE_1 - 1000);
595585
KUNIT_EXPECT_LE(test, rate, DUMMY_CLOCK_RATE_1 + 1000);
596-
597-
clk_put(parent2);
598-
clk_put(parent1);
599-
clk_put(clk);
600586
}
601587

602588
static struct kunit_case clk_multiple_parents_mux_test_cases[] = {
@@ -617,7 +603,6 @@ static struct kunit_suite
617603
clk_multiple_parents_mux_test_suite = {
618604
.name = "clk-multiple-parents-mux-test",
619605
.init = clk_multiple_parents_mux_test_init,
620-
.exit = clk_multiple_parents_mux_test_exit,
621606
.test_cases = clk_multiple_parents_mux_test_cases,
622607
};
623608

@@ -637,29 +622,20 @@ clk_orphan_transparent_multiple_parent_mux_test_init(struct kunit *test)
637622
&clk_dummy_rate_ops,
638623
0);
639624
ctx->parents_ctx[1].rate = DUMMY_CLOCK_INIT_RATE;
640-
ret = clk_hw_register(NULL, &ctx->parents_ctx[1].hw);
625+
ret = clk_hw_register_kunit(test, NULL, &ctx->parents_ctx[1].hw);
641626
if (ret)
642627
return ret;
643628

644629
ctx->hw.init = CLK_HW_INIT_PARENTS("test-orphan-mux", parents,
645630
&clk_multiple_parents_mux_ops,
646631
CLK_SET_RATE_PARENT);
647-
ret = clk_hw_register(NULL, &ctx->hw);
632+
ret = clk_hw_register_kunit(test, NULL, &ctx->hw);
648633
if (ret)
649634
return ret;
650635

651636
return 0;
652637
}
653638

654-
static void
655-
clk_orphan_transparent_multiple_parent_mux_test_exit(struct kunit *test)
656-
{
657-
struct clk_multiple_parent_ctx *ctx = test->priv;
658-
659-
clk_hw_unregister(&ctx->hw);
660-
clk_hw_unregister(&ctx->parents_ctx[1].hw);
661-
}
662-
663639
/*
664640
* Test that, for a mux whose current parent hasn't been registered yet and is
665641
* thus orphan, clk_get_parent() will return NULL.
@@ -912,7 +888,7 @@ clk_test_orphan_transparent_multiple_parent_mux_set_range_set_parent_get_rate(st
912888
{
913889
struct clk_multiple_parent_ctx *ctx = test->priv;
914890
struct clk_hw *hw = &ctx->hw;
915-
struct clk *clk = clk_hw_get_clk(hw, NULL);
891+
struct clk *clk = clk_hw_get_clk_kunit(test, hw, NULL);
916892
struct clk *parent;
917893
unsigned long rate;
918894
int ret;
@@ -921,7 +897,7 @@ clk_test_orphan_transparent_multiple_parent_mux_set_range_set_parent_get_rate(st
921897

922898
clk_hw_set_rate_range(hw, DUMMY_CLOCK_RATE_1, DUMMY_CLOCK_RATE_2);
923899

924-
parent = clk_hw_get_clk(&ctx->parents_ctx[1].hw, NULL);
900+
parent = clk_hw_get_clk_kunit(test, &ctx->parents_ctx[1].hw, NULL);
925901
KUNIT_ASSERT_NOT_ERR_OR_NULL(test, parent);
926902

927903
ret = clk_set_parent(clk, parent);
@@ -931,9 +907,6 @@ clk_test_orphan_transparent_multiple_parent_mux_set_range_set_parent_get_rate(st
931907
KUNIT_ASSERT_GT(test, rate, 0);
932908
KUNIT_EXPECT_GE(test, rate, DUMMY_CLOCK_RATE_1);
933909
KUNIT_EXPECT_LE(test, rate, DUMMY_CLOCK_RATE_2);
934-
935-
clk_put(parent);
936-
clk_put(clk);
937910
}
938911

939912
static struct kunit_case clk_orphan_transparent_multiple_parent_mux_test_cases[] = {
@@ -961,7 +934,6 @@ static struct kunit_case clk_orphan_transparent_multiple_parent_mux_test_cases[]
961934
static struct kunit_suite clk_orphan_transparent_multiple_parent_mux_test_suite = {
962935
.name = "clk-orphan-transparent-multiple-parent-mux-test",
963936
.init = clk_orphan_transparent_multiple_parent_mux_test_init,
964-
.exit = clk_orphan_transparent_multiple_parent_mux_test_exit,
965937
.test_cases = clk_orphan_transparent_multiple_parent_mux_test_cases,
966938
};
967939

@@ -986,15 +958,15 @@ static int clk_single_parent_mux_test_init(struct kunit *test)
986958
&clk_dummy_rate_ops,
987959
0);
988960

989-
ret = clk_hw_register(NULL, &ctx->parent_ctx.hw);
961+
ret = clk_hw_register_kunit(test, NULL, &ctx->parent_ctx.hw);
990962
if (ret)
991963
return ret;
992964

993965
ctx->hw.init = CLK_HW_INIT("test-clk", "parent-clk",
994966
&clk_dummy_single_parent_ops,
995967
CLK_SET_RATE_PARENT);
996968

997-
ret = clk_hw_register(NULL, &ctx->hw);
969+
ret = clk_hw_register_kunit(test, NULL, &ctx->hw);
998970
if (ret)
999971
return ret;
1000972

@@ -1060,7 +1032,7 @@ clk_test_single_parent_mux_set_range_disjoint_child_last(struct kunit *test)
10601032
{
10611033
struct clk_single_parent_ctx *ctx = test->priv;
10621034
struct clk_hw *hw = &ctx->hw;
1063-
struct clk *clk = clk_hw_get_clk(hw, NULL);
1035+
struct clk *clk = clk_hw_get_clk_kunit(test, hw, NULL);
10641036
struct clk *parent;
10651037
int ret;
10661038

@@ -1074,8 +1046,6 @@ clk_test_single_parent_mux_set_range_disjoint_child_last(struct kunit *test)
10741046

10751047
ret = clk_set_rate_range(clk, 3000, 4000);
10761048
KUNIT_EXPECT_LT(test, ret, 0);
1077-
1078-
clk_put(clk);
10791049
}
10801050

10811051
/*
@@ -1092,7 +1062,7 @@ clk_test_single_parent_mux_set_range_disjoint_parent_last(struct kunit *test)
10921062
{
10931063
struct clk_single_parent_ctx *ctx = test->priv;
10941064
struct clk_hw *hw = &ctx->hw;
1095-
struct clk *clk = clk_hw_get_clk(hw, NULL);
1065+
struct clk *clk = clk_hw_get_clk_kunit(test, hw, NULL);
10961066
struct clk *parent;
10971067
int ret;
10981068

@@ -1106,8 +1076,6 @@ clk_test_single_parent_mux_set_range_disjoint_parent_last(struct kunit *test)
11061076

11071077
ret = clk_set_rate_range(parent, 3000, 4000);
11081078
KUNIT_EXPECT_LT(test, ret, 0);
1109-
1110-
clk_put(clk);
11111079
}
11121080

11131081
/*
@@ -1238,7 +1206,6 @@ static struct kunit_suite
12381206
clk_single_parent_mux_test_suite = {
12391207
.name = "clk-single-parent-mux-test",
12401208
.init = clk_single_parent_mux_test_init,
1241-
.exit = clk_single_parent_mux_test_exit,
12421209
.test_cases = clk_single_parent_mux_test_cases,
12431210
};
12441211

0 commit comments

Comments
 (0)