Re: [PATCH] RISC-V: Don't check text_mutex during stop_machine

From: Steven Rostedt
Date: Thu May 06 2021 - 09:20:08 EST


On Thu, 6 May 2021 00:10:41 -0700
Palmer Dabbelt <palmer@xxxxxxxxxxx> wrote:

> diff --git a/arch/riscv/kernel/ftrace.c b/arch/riscv/kernel/ftrace.c
> index 7f1e5203de88..da2405652f1d 100644
> --- a/arch/riscv/kernel/ftrace.c
> +++ b/arch/riscv/kernel/ftrace.c
> @@ -11,6 +11,8 @@
> #include <asm/cacheflush.h>
> #include <asm/patch.h>
>
> +int riscv_ftrace_in_stop_machine;
> +
> #ifdef CONFIG_DYNAMIC_FTRACE
> int ftrace_arch_code_modify_prepare(void) __acquires(&text_mutex)
> {
> @@ -232,3 +234,16 @@ int ftrace_disable_ftrace_graph_caller(void)
> }
> #endif /* CONFIG_DYNAMIC_FTRACE */
> #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
> +
> +void arch_ftrace_update_code(int command)
> +{
> + /*
> + * The code sequences we use for ftrace can't be patched while the
> + * kernel is running, so we need to use stop_machine() to modify them
> + * for now. This doesn't play nice with text_mutex, we use this flag
> + * to elide the check.
> + */
> + riscv_ftrace_in_stop_machine = true;
> + ftrace_run_stop_machine(command);
> + riscv_ftrace_in_stop_machine = false;
> +}
> diff --git a/arch/riscv/kernel/patch.c b/arch/riscv/kernel/patch.c
> index 0b552873a577..7983dba477f0 100644

This would work, but my suggestion was to do it without having to add this
arch function. Because the caller of this is:

static void ftrace_run_update_code(int command)
{
int ret;

ret = ftrace_arch_code_modify_prepare();
FTRACE_WARN_ON(ret);
if (ret)
return;

/*
* By default we use stop_machine() to modify the code.
* But archs can do what ever they want as long as it
* is safe. The stop_machine() is the safest, but also
* produces the most overhead.
*/
arch_ftrace_update_code(command);

ret = ftrace_arch_code_modify_post_process();
FTRACE_WARN_ON(ret);
}


Where you already have two hooks that you use to take the text_mutex before
calling arch_ftrace_update_code().

In RISC-V those are:

int ftrace_arch_code_modify_prepare(void) __acquires(&text_mutex)
{
mutex_lock(&text_mutex);
return 0;
}

int ftrace_arch_code_modify_post_process(void) __releases(&text_mutex)
{
mutex_unlock(&text_mutex);
return 0;
}

Where all you have to do is change them to:

int ftrace_arch_code_modify_prepare(void) __acquires(&text_mutex)
{
mutex_lock(&text_mutex);
riscv_ftrace_in_stop_machine = true;
return 0;
}

int ftrace_arch_code_modify_post_process(void) __releases(&text_mutex)
{
riscv_ftrace_in_stop_machine = false;
mutex_unlock(&text_mutex);
return 0;
}

And you have the exact same affect. Those functions are only used before
calling the stop machine code you have.

-- Steve