[PATCH v2 11/15] dyndbg: don't close trace instance when in use

From: Łukasz Bartosik
Date: Thu Nov 30 2023 - 18:42:34 EST


Don't allow trace instance to be closed when it
is still being used by at least one callsite.

Signed-off-by: Łukasz Bartosik <lb@xxxxxxxxxxxx>
---
include/linux/dynamic_debug.h | 3 ++-
lib/dynamic_debug.c | 31 +++++++++++++++++++++++++++++++
2 files changed, 33 insertions(+), 1 deletion(-)

diff --git a/include/linux/dynamic_debug.h b/include/linux/dynamic_debug.h
index 56f152e75604..5555857d9ba5 100644
--- a/include/linux/dynamic_debug.h
+++ b/include/linux/dynamic_debug.h
@@ -186,7 +186,8 @@ void __dynamic_ibdev_dbg(struct _ddebug *descriptor,
.filename = __FILE__, \
.format = (fmt), \
.lineno = __LINE__, \
- .ctrl = { .flags = _DPRINTK_FLAGS_DEFAULT }, \
+ .ctrl = { .flags = _DPRINTK_FLAGS_DEFAULT, \
+ .trace_dst = TRACE_DST_MAX }, \
.class_id = cls, \
_DPRINTK_KEY_INIT \
}; \
diff --git a/lib/dynamic_debug.c b/lib/dynamic_debug.c
index 43e94023a4eb..73b6818e5fab 100644
--- a/lib/dynamic_debug.c
+++ b/lib/dynamic_debug.c
@@ -81,6 +81,7 @@ struct flag_settings {
struct ddebug_trace_inst {
const char *name;
struct trace_array *arr;
+ int use_cnt;
};

/*
@@ -274,6 +275,7 @@ static int handle_tr_opend_cmd(const char *arg)
goto end;
}

+ inst->use_cnt = 0;
set_bit(idx, tr.bmap);
v3pr_info("opened trace instance idx=%d, name=%s\n", idx, arg);
end:
@@ -296,6 +298,14 @@ static int handle_tr_close_cmd(const char *arg)

inst = &tr.inst[idx];

+ WARN_ON(inst->use_cnt < 0);
+ if (inst->use_cnt) {
+ pr_err("trace instance is being used name=%s, use_cnt=%d\n",
+ inst->name, inst->use_cnt);
+ ret = -EBUSY;
+ goto end;
+ }
+
trace_array_put(inst->arr);
/*
* don't destroy trace instance but let user do it manually
@@ -316,6 +326,26 @@ static int handle_tr_close_cmd(const char *arg)
return ret;
}

+static
+void update_tr_dst(const struct _ddebug *desc, const struct dd_ctrl *nctrl)
+{
+ int oflags = get_flags(desc), odst = get_trace_dst(desc);
+ int nflags = nctrl->flags, ndst = nctrl->trace_dst;
+
+ if (oflags & _DPRINTK_FLAGS_TRACE &&
+ nflags & _DPRINTK_FLAGS_TRACE &&
+ odst == ndst)
+ return;
+
+ if (oflags & _DPRINTK_FLAGS_TRACE &&
+ odst != TRACE_DST_MAX)
+ tr.inst[odst].use_cnt--;
+
+ if (nflags & _DPRINTK_FLAGS_TRACE &&
+ ndst != TRACE_DST_MAX)
+ tr.inst[ndst].use_cnt++;
+}
+
static int ddebug_parse_cmd(char *words[], int nwords)
{
int ret;
@@ -447,6 +477,7 @@ static int ddebug_change(const struct ddebug_query *query,
dt->mod_name, dp->function,
ddebug_describe_flags(get_flags(dp), &fbuf),
ddebug_describe_flags(nctrl.flags, &nbuf));
+ update_tr_dst(dp, &nctrl);
set_ctrl(dp, &nctrl);
}
}
--
2.43.0.rc2.451.g8631bc7472-goog