Re: [re: PATCH v2 00/15 - 10/11] dyndbg: move lock,unlock into ddebug_change, drop goto

From: Łukasz Bartosik
Date: Fri Dec 08 2023 - 19:32:54 EST


pt., 8 gru 2023 o 01:15 Jim Cromie <jim.cromie@xxxxxxxxx> napisał(a):
>
> Signed-off-by: Jim Cromie <jim.cromie@xxxxxxxxx>
> ---
> lib/dynamic_debug.c | 11 ++++-------
> 1 file changed, 4 insertions(+), 7 deletions(-)
>
> diff --git a/lib/dynamic_debug.c b/lib/dynamic_debug.c
> index fc903e90ea0d..b63429462d69 100644
> --- a/lib/dynamic_debug.c
> +++ b/lib/dynamic_debug.c
> @@ -537,6 +537,8 @@ static int ddebug_change(const struct ddebug_query *query,
> struct ddebug_class_map *map = NULL;
> int __outvar valid_class;
>
> + mutex_lock(&ddebug_lock);
> +
> /* search for matching ddebugs */
> list_for_each_entry(dt, &ddebug_tables, link) {
>
> @@ -625,6 +627,7 @@ static int ddebug_change(const struct ddebug_query *query,
> if (nfound)
> update_tr_default_dst(modifiers);
>
> + mutex_unlock(&ddebug_lock);
> return nfound;
> }
>
> @@ -932,23 +935,17 @@ static int ddebug_exec_query(char *query_string, const char *modname)
> return -EINVAL;
> }
>
> - mutex_lock(&ddebug_lock);
> -

I deliberately moved locking here from ddebug_change because
ddebug_parse_flags calls read_T_args (when T flag is present) and this
in turn calls find_tr_instance which uses global bitmap (tr.bmap).
If we add that ddebug_proc_show can be triggered concurrently then it
can lead to trouble.

For example
echo "open usb" > /proc/dynamic_debug/control
echo "module usbcore =T:usb" > /proc/dynamic_debug/control # from one console
echo "close usb" > / /proc/dynamic_debug/control
# from another console

and we can end up with an attempt to use closed trace instance"usb"



> /* check flags 1st (last arg) so query is pairs of spec,val */
> if (ddebug_parse_flags(words[nwords-1], &modifiers)) {
> pr_err("flags parse failed\n");
> - goto err;
> + return -EINVAL;
> }
>
> /* actually go and implement the change */
> nfound = ddebug_change(&query, &modifiers);
>
> - mutex_unlock(&ddebug_lock);
> vpr_info_dq(&query, nfound ? "applied" : "no-match");
> return nfound;
> -err:
> - mutex_unlock(&ddebug_lock);
> - return -EINVAL;
> }
>
> /* handle multiple queries in query string, continue on error, return
> --
> 2.43.0
>