]> git.ipfire.org Git - thirdparty/kernel/linux.git/commitdiff
mm/damon/reclaim: handle ctx allocation failure
authorSeongJae Park <sj@kernel.org>
Fri, 29 May 2026 00:01:02 +0000 (17:01 -0700)
committerAndrew Morton <akpm@linux-foundation.org>
Wed, 3 Jun 2026 23:25:50 +0000 (16:25 -0700)
Patch series "mm/damon/{reclaim,lru_sort}: handle ctx allocation failures".

DAMON_RECLAIM and DAMON_LRU_SORT could dereference NULL pointers if their
damon_ctx object allocations fail.  The bugs are expected to happen
infrequently because the allocations are arguably too small to fail on
common setups.  But theoretically they are possible and the consequences
are bad.  Fix those.

The issues were discovered [1] by Sashiko.

This patch (of 2):

DAMON_RECLAIM allocates the damon_ctx object for its kdamond in its init
function.  damon_reclaim_enabled_store() wrongly assumes the allocation
will always succeed once tried.  If the damon_ctx allocation was failed,
therefore, code execution reaches to damon_commit_ctx() while 'ctx' is
NULL.  As a result, it dereferences the NULL 'ctx' pointer.  Avoid the
NULL dereference by returning -ENOMEM if 'ctx' is NULL.

Link: https://lore.kernel.org/20260529000104.7006-2-sj@kernel.org
Link: https://lore.kernel.org/20260419014800.877-1-sj@kernel.org
Fixes: 3f7a914ab9a5 ("mm/damon/reclaim: use damon_initialized()")
Signed-off-by: SeongJae Park <sj@kernel.org>
Cc: <stable@vger.kernel.org> # 6.18.x
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
mm/damon/reclaim.c

index fe7fce26cf6ce3a77b27d0bb2a195a5c065322ac..96f6dfc28eae45a66f0081c1894fb0ed3cf53d34 100644 (file)
@@ -339,6 +339,10 @@ static int damon_reclaim_enabled_store(const char *val,
        if (!damon_initialized())
                return 0;
 
+       /* damon_modules_new_paddr_ctx_target() in the init function failed. */
+       if (!ctx)
+               return -ENOMEM;
+
        return damon_reclaim_turn(enabled);
 }