Skip to content

Commit b2dc1ec

Browse files
shroffnikawasaki
authored andcommitted
check: add kmemleak support to blktests
Running blktests can also help uncover kernel memory leaks when the kernel is built with CONFIG_DEBUG_KMEMLEAK. However, until now the blktests framework had no way to automatically detect or report such leaks. Users typically had to manually setup kmemleak and trigger scans after running tests[1][2]. This change integrates kmemleak support directly into the blktests framework. Before running each test, the framework checks for the presence of /sys/kernel/debug/kmemleak to determine whether kmemleak is enabled for the running kernel. If available, before running a test, any existing kmemleak reports are cleared to avoid false positives from previous tests. After the test completes, the framework explicitly triggers a kmemleak scan. If memory leaks are detected, they are written to a per-test file at, "results/.../.../<test>.kmemleak" and the corresponding test is marked as FAIL. Users can then inspect the <test>.kmemleak file to analyze the reported leaks. With this enhancement, blktests can automatically detect kernel memory leaks (if kerel is configured with CONFIG_DEBUG_KMEMLEAK support) on a per-test basis, removing the need for manual kmemleak setup and scans. This should make it easier and faster to identify memory leaks introduced by individual tests. [1] https://lore.kernel.org/all/CAHj4cs8oJFvz=daCvjHM5dYCNQH4UXwSySPPU4v-WHce_kZXZA@mail.gmail.com/ [2] https://lore.kernel.org/all/CAHj4cs9wv3SdPo+N01Fw2SHBYDs9tj2M_e1-GdQOkRy=DsBB1w@mail.gmail.com/ Signed-off-by: Nilay Shroff <[email protected]> Reviewed-by: Daniel Wagner <[email protected]> Reviewed-by: Yi Zhang <[email protected]> Reviewed-by: Johannes Thumshirn <[email protected]> [Shin'ichiro: replaced indent spaces with tabs] Signed-off-by: Shin'ichiro Kawasaki <[email protected]>
1 parent 7c3ad92 commit b2dc1ec

1 file changed

Lines changed: 47 additions & 0 deletions

File tree

check

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,36 @@ _check_dmesg() {
183183
fi
184184
}
185185

186+
_setup_kmemleak() {
187+
local f="/sys/kernel/debug/kmemleak"
188+
189+
if [[ ! -e $f || ! -r $f ]]; then
190+
return 0
191+
fi
192+
193+
echo clear > "$f"
194+
}
195+
196+
_check_kmemleak() {
197+
local kmemleak
198+
local f="/sys/kernel/debug/kmemleak"
199+
200+
if [[ ! -e $f || ! -r $f ]]; then
201+
return 0
202+
fi
203+
204+
echo scan > "$f"
205+
sleep 1
206+
kmemleak=$(cat "$f")
207+
208+
if [[ -z $kmemleak ]]; then
209+
return 0
210+
fi
211+
212+
printf '%s\n' "$kmemleak" > "${seqres}.kmemleak"
213+
return 1
214+
}
215+
186216
_read_last_test_run() {
187217
local seqres="${RESULTS_DIR}/${TEST_NAME}"
188218

@@ -377,6 +407,8 @@ _call_test() {
377407
if [[ -v SKIP_REASONS ]]; then
378408
TEST_RUN["status"]="not run"
379409
else
410+
_setup_kmemleak
411+
380412
if [[ -w /dev/kmsg ]]; then
381413
local dmesg_marker="run blktests $TEST_NAME at ${TEST_RUN["date"]}"
382414
echo "$dmesg_marker" >> /dev/kmsg
@@ -414,6 +446,9 @@ _call_test() {
414446
elif ! _check_dmesg "$dmesg_marker"; then
415447
TEST_RUN["status"]=fail
416448
TEST_RUN["reason"]=dmesg
449+
elif ! _check_kmemleak; then
450+
TEST_RUN["status"]=fail
451+
TEST_RUN["reason"]=kmemleak
417452
else
418453
TEST_RUN["status"]=pass
419454
fi
@@ -451,6 +486,18 @@ _call_test() {
451486
print \" \" \$0
452487
}" "${seqres}.dmesg"
453488
;;
489+
kmemleak)
490+
echo " kmemleak detected:"
491+
awk "
492+
{
493+
if (NR > 10) {
494+
print \" ...\"
495+
print \" (See '${seqres}.kmemleak' for the entire message)\"
496+
exit
497+
}
498+
print \" \" \$0
499+
}" "${seqres}.kmemleak"
500+
;;
454501
esac
455502
return 1
456503
else

0 commit comments

Comments
 (0)