Skip to contentSkip to conversation

mm/damon: fix memory leak in memcg_path sysfs file

PATCH Applied SeongJae Park 1 participant 3 messages Jun 19, 2025

Subsystems (4)
lore
Share & tools
lore

SeongJae Park

author maintainer
#0 lore raw

cover mm/damon: fix memory leak in memcg_path sysfs file

Users can leak memory by repeatedly writing a string to DAMOS sysfs memcg_path file. Fix it (patch 1) and add a selftest (patch 2) to avoid reoccurrance of the bug.

SeongJae Park (2):
mm/damon/sysfs-schemes: free old damon_sysfs_scheme_filter->memcg_path
on write
selftets/damon: add a test for memcg_path leak

 mm/damon/sysfs-schemes.c                      |  1 +
 tools/testing/selftests/damon/Makefile        |  1 +
 .../selftests/damon/sysfs_memcg_path_leak.sh  | 43 +++++++++++++++++++
 3 files changed, 45 insertions(+)
 create mode 100755 tools/testing/selftests/damon/sysfs_memcg_path_leak.sh

base-commit: 05b89e828eb4f791f721cbdc65f36e1a8287a9d3

Signature · 1 line
2.39.5
author maintainer
#1 lore raw

PATCH 1/2 mm/damon/sysfs-schemes: free old damon_sysfs_scheme_filter->memcg_path on write

memcg_path_store() assigns a newly allocated memory buffer to filter->memcg_path, without deallocating the previously allocated and assigned memory buffer. As a result, users can leak kernel memory by continuously writing a data to memcg_path DAMOS sysfs file. Fix the leak by deallocating the previously set memory buffer.

Fixes 7ee161f18b5d ("mm/damon/sysfs-schemes: implement filter directory")
---
 mm/damon/sysfs-schemes.c | 1 +
 1 file changed, 1 insertion(+)
mm/damon/sysfs-schemes.c +1 -0
index 0f6c9e1fec0b..30ae7518ffbf 100644
@@ -472,6 +472,7 @@ static ssize_t memcg_path_store(struct kobject *kobj,
 		return -ENOMEM;
 
 	strscpy(path, buf, count + 1);
+	kfree(filter->memcg_path);
 	filter->memcg_path = path;
 	return count;
 }
Signature · 1 line
2.39.5
author maintainer
#2 lore raw

PATCH 2/2 selftets/damon: add a test for memcg_path leak

In reply to SeongJae Park

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

---
 tools/testing/selftests/damon/Makefile        |  1 +
 .../selftests/damon/sysfs_memcg_path_leak.sh  | 43 +++++++++++++++++++
 2 files changed, 44 insertions(+)
 create mode 100755 tools/testing/selftests/damon/sysfs_memcg_path_leak.sh
tools/testing/selftests/damon/Makefile +1 -0
index ff21524be458..e888455e3cf8 100644
@@ -15,6 +15,7 @@ TEST_PROGS += reclaim.sh lru_sort.sh
 # regression tests (reproducers of previously found bugs)
 TEST_PROGS += sysfs_update_removed_scheme_dir.sh
 TEST_PROGS += sysfs_update_schemes_tried_regions_hang.py
+TEST_PROGS += sysfs_memcg_path_leak.sh
 
 EXTRA_CLEAN = __pycache__
 
tools/testing/selftests/damon/sysfs_memcg_path_leak.sh new file +43 -0

43 lines not on this page · raw message

Signature · 1 line
2.39.5