Skip to content

Commit 66f0919

Browse files
Shang XiaoJingmhiramat
authored andcommitted
tracing: kprobe: Fix memory leak in test_gen_kprobe/kretprobe_cmd()
test_gen_kprobe_cmd() only free buf in fail path, hence buf will leak when there is no failure. Move kfree(buf) from fail path to common path to prevent the memleak. The same reason and solution in test_gen_kretprobe_cmd(). unreferenced object 0xffff888143b14000 (size 2048): comm "insmod", pid 52490, jiffies 4301890980 (age 40.553s) hex dump (first 32 bytes): 70 3a 6b 70 72 6f 62 65 73 2f 67 65 6e 5f 6b 70 p:kprobes/gen_kp 72 6f 62 65 5f 74 65 73 74 20 64 6f 5f 73 79 73 robe_test do_sys backtrace: [<000000006d7b836b>] kmalloc_trace+0x27/0xa0 [<0000000009528b5b>] 0xffffffffa059006f [<000000008408b580>] do_one_initcall+0x87/0x2a0 [<00000000c4980a7e>] do_init_module+0xdf/0x320 [<00000000d775aad0>] load_module+0x3006/0x3390 [<00000000e9a74b80>] __do_sys_finit_module+0x113/0x1b0 [<000000003726480d>] do_syscall_64+0x35/0x80 [<000000003441e93b>] entry_SYSCALL_64_after_hwframe+0x46/0xb0 Link: https://lore.kernel.org/all/[email protected]/ Fixes: 6483624 ("tracing: Add kprobe event command generation test module") Cc: [email protected] Signed-off-by: Shang XiaoJing <[email protected]> Acked-by: Masami Hiramatsu (Google) <[email protected]> Signed-off-by: Masami Hiramatsu (Google) <[email protected]>
1 parent 61b304b commit 66f0919

File tree

1 file changed

+7
-11
lines changed

1 file changed

+7
-11
lines changed

kernel/trace/kprobe_event_gen_test.c

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -100,20 +100,20 @@ static int __init test_gen_kprobe_cmd(void)
100100
KPROBE_GEN_TEST_FUNC,
101101
KPROBE_GEN_TEST_ARG0, KPROBE_GEN_TEST_ARG1);
102102
if (ret)
103-
goto free;
103+
goto out;
104104

105105
/* Use kprobe_event_add_fields to add the rest of the fields */
106106

107107
ret = kprobe_event_add_fields(&cmd, KPROBE_GEN_TEST_ARG2, KPROBE_GEN_TEST_ARG3);
108108
if (ret)
109-
goto free;
109+
goto out;
110110

111111
/*
112112
* This actually creates the event.
113113
*/
114114
ret = kprobe_event_gen_cmd_end(&cmd);
115115
if (ret)
116-
goto free;
116+
goto out;
117117

118118
/*
119119
* Now get the gen_kprobe_test event file. We need to prevent
@@ -136,13 +136,11 @@ static int __init test_gen_kprobe_cmd(void)
136136
goto delete;
137137
}
138138
out:
139+
kfree(buf);
139140
return ret;
140141
delete:
141142
/* We got an error after creating the event, delete it */
142143
ret = kprobe_event_delete("gen_kprobe_test");
143-
free:
144-
kfree(buf);
145-
146144
goto out;
147145
}
148146

@@ -170,14 +168,14 @@ static int __init test_gen_kretprobe_cmd(void)
170168
KPROBE_GEN_TEST_FUNC,
171169
"$retval");
172170
if (ret)
173-
goto free;
171+
goto out;
174172

175173
/*
176174
* This actually creates the event.
177175
*/
178176
ret = kretprobe_event_gen_cmd_end(&cmd);
179177
if (ret)
180-
goto free;
178+
goto out;
181179

182180
/*
183181
* Now get the gen_kretprobe_test event file. We need to
@@ -201,13 +199,11 @@ static int __init test_gen_kretprobe_cmd(void)
201199
goto delete;
202200
}
203201
out:
202+
kfree(buf);
204203
return ret;
205204
delete:
206205
/* We got an error after creating the event, delete it */
207206
ret = kprobe_event_delete("gen_kretprobe_test");
208-
free:
209-
kfree(buf);
210-
211207
goto out;
212208
}
213209

0 commit comments

Comments
 (0)