mirror-linux/mm/damon
SeongJae Park 441f92f7d3 mm/damon/sysfs-schemes: delete tried region in regions_rmdirs()
DAMON sysfs maintains the DAMOS tried region directory objects via a
linked list.  When the user requests refresh of the directories, DAMON
sysfs removes all the region directories first, and then generate updated
regions directory on the empty space.  The removal function
(damon_sysfs_scheme_regions_rm_dirs()) only puts the kobj objects. 
Deletion of the container region object from the linked list is done
inside the kobj release callback function.

If somehow the callback invocation is delayed, the list will contain
regions list that gonna be freed.  If the updated region directories
creation is started in this situation, the list can be corrupted and
use-after-free can happen.

Because the kobj objects are managed by only DAMON sysfs, the issue cannot
happen in normal situation.  But, such delays can be made on kernels that
built with CONFIG_DEBUG_KOBJECT_RELEASE.  On the kernel, the issue can
indeed be reproduced like below.

    # damo start --damos_action stat
    # cd /sys/kernel/mm/damon/admin/kdamonds/0/
    # for i in {1..10}; do echo update_schemes_tried_regions > state; done
    # dmesg | grep underflow
    [   89.296152] refcount_t: underflow; use-after-free.

Fix the issue by removing the region object from the list when
decrementing the reference count.

Also update damos_sysfs_populate_region_dir() to add the region object to
the list only after the kobject_init_and_add() is success, so that fail of
kobject_init_and_add() is not leaving the deallocated object on the list.

The issue was discovered [1] by Sashiko.

Link: https://lore.kernel.org/20260518152559.93038-1-sj@kernel.org
Link: https://lore.kernel.org/20260513011920.119183-1-sj@kernel.org [1]
Fixes: 9277d0367b ("mm/damon/sysfs-schemes: implement scheme region directory")
Signed-off-by: SeongJae Park <sj@kernel.org>
Cc: <stable@vger.kernel.org> # 6.2.x
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
2026-05-21 19:06:12 -07:00
..
tests mm/damon/tests/core-kunit: test goal_tuner commit 2026-04-05 13:53:26 -07:00
Kconfig mm/damon: add CONFIG_DAMON_DEBUG_SANITY 2026-04-05 13:53:20 -07:00
Makefile mm/damon: introduce DAMON_STAT module 2025-07-09 22:41:55 -07:00
core.c 7 hotfixes. 6 are cc:stable and all are for MM. Please see the 2026-04-19 14:45:37 -07:00
lru_sort.c mm/damon/lru_sort: detect and use fresh enabled and kdamond_pid values 2026-04-27 05:54:27 -07:00
modules-common.c mm/damon: s/primitives/code/ on comments 2025-06-05 21:55:41 -07:00
modules-common.h mm/damon: s/primitives/code/ on comments 2025-06-05 21:55:41 -07:00
ops-common.c mm/damon/ops-common: remove redudnant mmu notifier call in pmdp mkold 2026-03-24 14:38:25 -07:00
ops-common.h mm/damon/paddr: move filters existence check function to ops-common 2025-09-13 16:54:47 -07:00
paddr.c mm/damon: remove unused target param of get_scheme_score() 2026-04-05 13:53:00 -07:00
reclaim.c mm/damon/reclaim: detect and use fresh enabled and kdamond_pid values 2026-04-27 05:54:26 -07:00
stat.c mm/damon/stat: detect and use fresh enabled value 2026-04-27 05:54:27 -07:00
sysfs-common.c Convert 'alloc_obj' family to use the new default GFP_KERNEL argument 2026-02-21 17:09:51 -08:00
sysfs-common.h mm/damon: s/primitives/code/ on comments 2025-06-05 21:55:41 -07:00
sysfs-schemes.c mm/damon/sysfs-schemes: delete tried region in regions_rmdirs() 2026-05-21 19:06:12 -07:00
sysfs.c mm/damon/sysfs: dealloc repeat_call_control if damon_call() fails 2026-04-06 11:13:42 -07:00
vaddr.c mm/damon/vaddr: do not split regions for min_nr_regions 2026-04-05 13:53:11 -07:00