Files
linux/samples/damon/wsse.c
SeongJae Park 0ed1165c37 samples/damon/wsse: fix boot time enable handling
Patch series "mm/damon: fix misc bugs in DAMON modules".

From manual code review, I found below bugs in DAMON modules.

DAMON sample modules crash if those are enabled at boot time, via kernel
command line.  A similar issue was found and fixed on DAMON non-sample
modules in the past, but we didn't check that for sample modules.

DAMON non-sample modules are not setting 'enabled' parameters accordingly
when real enabling is failed.  Honggyu found and fixed[1] this type of
bugs in DAMON sample modules, and my inspection was motivated by the great
work.  Kudos to Honggyu.

Finally, DAMON_RECLIAM is mistakenly losing scheme internal status due to
misuse of damon_commit_ctx().  DAMON_LRU_SORT has a similar misuse, but
fortunately it is not causing real status loss.

Fix the bugs.  Since these are similar patterns of bugs that were found in
the past, it would be better to add tests or refactor the code, in future.


This patch (of 6):

If 'enable' parameter of the 'wsse' DAMON sample module is set at boot
time via the kernel command line, memory allocation is tried before the
slab is initialized.  As a result kernel NULL pointer dereference BUG can
happen.  Fix it by checking the initialization status.

Link: https://lkml.kernel.org/r/20250706193207.39810-1-sj@kernel.org
Link: https://lkml.kernel.org/r/20250706193207.39810-2-sj@kernel.org
Link: https://lore.kernel.org/20250702000205.1921-1-honggyu.kim@sk.com [1]
Fixes: b757c6cfc6 ("samples/damon/wsse: start and stop DAMON as the user requests")
Signed-off-by: SeongJae Park <sj@kernel.org>
Cc: <stable@vger.kernel.org>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
2025-07-13 16:38:33 -07:00

131 lines
2.5 KiB
C

// SPDX-License-Identifier: GPL-2.0
/*
* working set size estimation: monitor access pattern of given process and
* print estimated working set size (total size of regions that showing some
* access).
*/
#define pr_fmt(fmt) "damon_sample_wsse: " fmt
#include <linux/damon.h>
#include <linux/init.h>
#include <linux/kernel.h>
#include <linux/module.h>
static int target_pid __read_mostly;
module_param(target_pid, int, 0600);
static int damon_sample_wsse_enable_store(
const char *val, const struct kernel_param *kp);
static const struct kernel_param_ops enable_param_ops = {
.set = damon_sample_wsse_enable_store,
.get = param_get_bool,
};
static bool enable __read_mostly;
module_param_cb(enable, &enable_param_ops, &enable, 0600);
MODULE_PARM_DESC(enable, "Enable or disable DAMON_SAMPLE_WSSE");
static struct damon_ctx *ctx;
static struct pid *target_pidp;
static int damon_sample_wsse_after_aggregate(struct damon_ctx *c)
{
struct damon_target *t;
damon_for_each_target(t, c) {
struct damon_region *r;
unsigned long wss = 0;
damon_for_each_region(r, t) {
if (r->nr_accesses > 0)
wss += r->ar.end - r->ar.start;
}
pr_info("wss: %lu\n", wss);
}
return 0;
}
static int damon_sample_wsse_start(void)
{
struct damon_target *target;
pr_info("start\n");
ctx = damon_new_ctx();
if (!ctx)
return -ENOMEM;
if (damon_select_ops(ctx, DAMON_OPS_VADDR)) {
damon_destroy_ctx(ctx);
return -EINVAL;
}
target = damon_new_target();
if (!target) {
damon_destroy_ctx(ctx);
return -ENOMEM;
}
damon_add_target(ctx, target);
target_pidp = find_get_pid(target_pid);
if (!target_pidp) {
damon_destroy_ctx(ctx);
return -EINVAL;
}
target->pid = target_pidp;
ctx->callback.after_aggregation = damon_sample_wsse_after_aggregate;
return damon_start(&ctx, 1, true);
}
static void damon_sample_wsse_stop(void)
{
pr_info("stop\n");
if (ctx) {
damon_stop(&ctx, 1);
damon_destroy_ctx(ctx);
}
if (target_pidp)
put_pid(target_pidp);
}
static bool init_called;
static int damon_sample_wsse_enable_store(
const char *val, const struct kernel_param *kp)
{
bool enabled = enable;
int err;
err = kstrtobool(val, &enable);
if (err)
return err;
if (enable == enabled)
return 0;
if (enable) {
err = damon_sample_wsse_start();
if (err)
enable = false;
return err;
}
damon_sample_wsse_stop();
return 0;
}
static int __init damon_sample_wsse_init(void)
{
int err = 0;
init_called = true;
if (enable) {
err = damon_sample_wsse_start();
if (err)
enable = false;
}
return err;
}
module_init(damon_sample_wsse_init);