Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

selftets/damon: add a test for memcg_path leak

There was a memory leak bug in DAMOS sysfs memcg_path file. Add a
selftest to ensure the bug never comes back.

Link: https://lkml.kernel.org/r/20250619183608.6647-3-sj@kernel.org
Signed-off-by: SeongJae Park <sj@kernel.org>
Cc: Shuah Khan <shuah@kernel.org>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>

authored by

SeongJae Park and committed by
Andrew Morton
dfa3cf0b 5d26b5bd

+44
+1
tools/testing/selftests/damon/Makefile
··· 15 15 # regression tests (reproducers of previously found bugs) 16 16 TEST_PROGS += sysfs_update_removed_scheme_dir.sh 17 17 TEST_PROGS += sysfs_update_schemes_tried_regions_hang.py 18 + TEST_PROGS += sysfs_memcg_path_leak.sh 18 19 19 20 EXTRA_CLEAN = __pycache__ 20 21
+43
tools/testing/selftests/damon/sysfs_memcg_path_leak.sh
··· 1 + #!/bin/bash 2 + # SPDX-License-Identifier: GPL-2.0 3 + 4 + if [ $EUID -ne 0 ] 5 + then 6 + echo "Run as root" 7 + exit $ksft_skip 8 + fi 9 + 10 + damon_sysfs="/sys/kernel/mm/damon/admin" 11 + if [ ! -d "$damon_sysfs" ] 12 + then 13 + echo "damon sysfs not found" 14 + exit $ksft_skip 15 + fi 16 + 17 + # ensure filter directory 18 + echo 1 > "$damon_sysfs/kdamonds/nr_kdamonds" 19 + echo 1 > "$damon_sysfs/kdamonds/0/contexts/nr_contexts" 20 + echo 1 > "$damon_sysfs/kdamonds/0/contexts/0/schemes/nr_schemes" 21 + echo 1 > "$damon_sysfs/kdamonds/0/contexts/0/schemes/0/filters/nr_filters" 22 + 23 + filter_dir="$damon_sysfs/kdamonds/0/contexts/0/schemes/0/filters/0" 24 + 25 + before_kb=$(grep Slab /proc/meminfo | awk '{print $2}') 26 + 27 + # try to leak 3000 KiB 28 + for i in {1..102400}; 29 + do 30 + echo "012345678901234567890123456789" > "$filter_dir/memcg_path" 31 + done 32 + 33 + after_kb=$(grep Slab /proc/meminfo | awk '{print $2}') 34 + # expect up to 1500 KiB free from other tasks memory 35 + expected_after_kb_max=$((before_kb + 1500)) 36 + 37 + if [ "$after_kb" -gt "$expected_after_kb_max" ] 38 + then 39 + echo "maybe memcg_path are leaking: $before_kb -> $after_kb" 40 + exit 1 41 + else 42 + exit 0 43 + fi