2015-12-04 02:18:11

by Li Bin

[permalink] [raw]
Subject: [PATCH v2 0/2] arm64: stop using kstop_machine for ftrace

v2:
Based on the comments from Will and Steve,
1. Modify the commit message
2. Fix the misleading comments for ftrace_modify_code

Link: https://lkml.org/lkml/2015/12/3/422

Li Bin (2):
arm64: ftrace: stop using kstop_machine to enable/disable tracing
arm64: ftrace: fix the comments for ftrace_modify_code

arch/arm64/kernel/ftrace.c | 10 ++++++----
1 files changed, 6 insertions(+), 4 deletions(-)


2015-12-04 02:18:12

by Li Bin

[permalink] [raw]
Subject: [PATCH v2 1/2] arm64: ftrace: stop using kstop_machine to enable/disable tracing

For ftrace on arm64, kstop_machine which is hugely disruptive
to a running system is not needed to convert nops to ftrace calls
or back, because that to be modified instrucions, that NOP, B or BL,
are all safe instructions which called "concurrent modification
and execution of instructions", that can be executed by one
thread of execution as they are being modified by another thread
of execution without requiring explicit synchronization.

Signed-off-by: Li Bin <[email protected]>
---
arch/arm64/kernel/ftrace.c | 5 +++++
1 files changed, 5 insertions(+), 0 deletions(-)

diff --git a/arch/arm64/kernel/ftrace.c b/arch/arm64/kernel/ftrace.c
index c851be7..9669b33 100644
--- a/arch/arm64/kernel/ftrace.c
+++ b/arch/arm64/kernel/ftrace.c
@@ -93,6 +93,11 @@ int ftrace_make_nop(struct module *mod, struct dyn_ftrace *rec,
return ftrace_modify_code(pc, old, new, true);
}

+void arch_ftrace_update_code(int command)
+{
+ ftrace_modify_all_code(command);
+}
+
int __init ftrace_dyn_arch_init(void)
{
return 0;
--
1.7.1

2015-12-04 02:18:09

by Li Bin

[permalink] [raw]
Subject: [PATCH v2 2/2] arm64: ftrace: fix the comments for ftrace_modify_code

There is no need to worry about module text disappearing case,
because that ftrace has a module notifier that is called when
a module is being unloaded and before the text goes away, and this
code grabs the ftrace_lock mutex and removes the module functions
from the ftrace list, such that it will no longer do any
modifications to that module's text.
The update to make functions be traced or not is done under the
ftrace_lock mutex as well.

Signed-off-by: Li Bin <[email protected]>
---
arch/arm64/kernel/ftrace.c | 5 +----
1 files changed, 1 insertions(+), 4 deletions(-)

diff --git a/arch/arm64/kernel/ftrace.c b/arch/arm64/kernel/ftrace.c
index 9669b33..ee91c0c 100644
--- a/arch/arm64/kernel/ftrace.c
+++ b/arch/arm64/kernel/ftrace.c
@@ -29,12 +29,9 @@ static int ftrace_modify_code(unsigned long pc, u32 old, u32 new,

/*
* Note:
- * Due to modules and __init, code can disappear and change,
+ * Due to __init, code can disappear and change,
* we need to protect against faulting as well as code changing.
* We do this by aarch64_insn_*() which use the probe_kernel_*().
- *
- * No lock is held here because all the modifications are run
- * through stop_machine().
*/
if (validate) {
if (aarch64_insn_read((void *)pc, &replaced))
--
1.7.1

2015-12-04 02:50:09

by Steven Rostedt

[permalink] [raw]
Subject: Re: [PATCH v2 2/2] arm64: ftrace: fix the comments for ftrace_modify_code

On Fri, 4 Dec 2015 10:18:39 +0800
Li Bin <[email protected]> wrote:

> There is no need to worry about module text disappearing case,
> because that ftrace has a module notifier that is called when
> a module is being unloaded and before the text goes away, and this
> code grabs the ftrace_lock mutex and removes the module functions
> from the ftrace list, such that it will no longer do any
> modifications to that module's text.
> The update to make functions be traced or not is done under the
> ftrace_lock mutex as well.
>
> Signed-off-by: Li Bin <[email protected]>
> ---
> arch/arm64/kernel/ftrace.c | 5 +----
> 1 files changed, 1 insertions(+), 4 deletions(-)
>
> diff --git a/arch/arm64/kernel/ftrace.c b/arch/arm64/kernel/ftrace.c
> index 9669b33..ee91c0c 100644
> --- a/arch/arm64/kernel/ftrace.c
> +++ b/arch/arm64/kernel/ftrace.c
> @@ -29,12 +29,9 @@ static int ftrace_modify_code(unsigned long pc, u32 old, u32 new,
>
> /*
> * Note:
> - * Due to modules and __init, code can disappear and change,
> + * Due to __init, code can disappear and change,

Init code should not be modified either because it is black listed in
recordmcount.c.

I say just change the comment to be something like:

We are paranoid about modifying text, as if a bug were to happen, it
could cause us to read or write to someplace that could cause harm.
Carefully read and modify the code with aarch64_insn_*() which uses
probe_kernel_*(), and make sure what we read is what we expected it to
be before modifying it.

-- Steve


> * we need to protect against faulting as well as code changing.
> * We do this by aarch64_insn_*() which use the probe_kernel_*().
> - *
> - * No lock is held here because all the modifications are run
> - * through stop_machine().
> */
> if (validate) {
> if (aarch64_insn_read((void *)pc, &replaced))

2015-12-04 03:12:46

by Steven Rostedt

[permalink] [raw]
Subject: Re: [PATCH v2 1/2] arm64: ftrace: stop using kstop_machine to enable/disable tracing

On Fri, 4 Dec 2015 10:18:38 +0800
Li Bin <[email protected]> wrote:

> For ftrace on arm64, kstop_machine which is hugely disruptive
> to a running system is not needed to convert nops to ftrace calls
> or back, because that to be modified instrucions, that NOP, B or BL,
> are all safe instructions which called "concurrent modification
> and execution of instructions", that can be executed by one
> thread of execution as they are being modified by another thread
> of execution without requiring explicit synchronization.
>
> Signed-off-by: Li Bin <[email protected]>
> ---
> arch/arm64/kernel/ftrace.c | 5 +++++
> 1 files changed, 5 insertions(+), 0 deletions(-)
>
> diff --git a/arch/arm64/kernel/ftrace.c b/arch/arm64/kernel/ftrace.c
> index c851be7..9669b33 100644
> --- a/arch/arm64/kernel/ftrace.c
> +++ b/arch/arm64/kernel/ftrace.c
> @@ -93,6 +93,11 @@ int ftrace_make_nop(struct module *mod, struct dyn_ftrace *rec,
> return ftrace_modify_code(pc, old, new, true);
> }
>
> +void arch_ftrace_update_code(int command)
> +{
> + ftrace_modify_all_code(command);

Hmm, I wonder why I haven't done this for powerpc. I probably should.

Anyway,

Reviewed-by: Steven Rostedt <[email protected]>

If it is indeed safe not to do any special handling.

-- Steve

> +}
> +
> int __init ftrace_dyn_arch_init(void)
> {
> return 0;

2015-12-04 03:24:28

by Li Bin

[permalink] [raw]
Subject: Re: [PATCH v2 2/2] arm64: ftrace: fix the comments for ftrace_modify_code



on 2015/12/4 10:50, Steven Rostedt wrote:
> On Fri, 4 Dec 2015 10:18:39 +0800
> Li Bin <[email protected]> wrote:
>
>> There is no need to worry about module text disappearing case,
>> because that ftrace has a module notifier that is called when
>> a module is being unloaded and before the text goes away, and this
>> code grabs the ftrace_lock mutex and removes the module functions
>> from the ftrace list, such that it will no longer do any
>> modifications to that module's text.
>> The update to make functions be traced or not is done under the
>> ftrace_lock mutex as well.
>>
>> Signed-off-by: Li Bin <[email protected]>
>> ---
>> arch/arm64/kernel/ftrace.c | 5 +----
>> 1 files changed, 1 insertions(+), 4 deletions(-)
>>
>> diff --git a/arch/arm64/kernel/ftrace.c b/arch/arm64/kernel/ftrace.c
>> index 9669b33..ee91c0c 100644
>> --- a/arch/arm64/kernel/ftrace.c
>> +++ b/arch/arm64/kernel/ftrace.c
>> @@ -29,12 +29,9 @@ static int ftrace_modify_code(unsigned long pc, u32 old, u32 new,
>>
>> /*
>> * Note:
>> - * Due to modules and __init, code can disappear and change,
>> + * Due to __init, code can disappear and change,
> Init code should not be modified either because it is black listed in
> recordmcount.c.
>
> I say just change the comment to be something like:
>
> We are paranoid about modifying text, as if a bug were to happen, it
> could cause us to read or write to someplace that could cause harm.
> Carefully read and modify the code with aarch64_insn_*() which uses
> probe_kernel_*(), and make sure what we read is what we expected it to
> be before modifying it.

Ok, I will modify it.

Thanks,
Li Bin

> -- Steve
>
>
>> * we need to protect against faulting as well as code changing.
>> * We do this by aarch64_insn_*() which use the probe_kernel_*().
>> - *
>> - * No lock is held here because all the modifications are run
>> - * through stop_machine().
>> */
>> if (validate) {
>> if (aarch64_insn_read((void *)pc, &replaced))
>
> .
>

2015-12-04 06:38:45

by Li Bin

[permalink] [raw]
Subject: Re: [PATCH v2 2/2] arm64: ftrace: fix the comments for ftrace_modify_code

I will also update the comment for the other arch that using the similar
description, such as ia64/metag/powerpc/sh/x86.

Thanks,
Li Bin

on 2015/12/4 10:50, Steven Rostedt wrote:
> On Fri, 4 Dec 2015 10:18:39 +0800
> Li Bin <[email protected]> wrote:
>
>> There is no need to worry about module text disappearing case,
>> because that ftrace has a module notifier that is called when
>> a module is being unloaded and before the text goes away, and this
>> code grabs the ftrace_lock mutex and removes the module functions
>> from the ftrace list, such that it will no longer do any
>> modifications to that module's text.
>> The update to make functions be traced or not is done under the
>> ftrace_lock mutex as well.
>>
>> Signed-off-by: Li Bin <[email protected]>
>> ---
>> arch/arm64/kernel/ftrace.c | 5 +----
>> 1 files changed, 1 insertions(+), 4 deletions(-)
>>
>> diff --git a/arch/arm64/kernel/ftrace.c b/arch/arm64/kernel/ftrace.c
>> index 9669b33..ee91c0c 100644
>> --- a/arch/arm64/kernel/ftrace.c
>> +++ b/arch/arm64/kernel/ftrace.c
>> @@ -29,12 +29,9 @@ static int ftrace_modify_code(unsigned long pc, u32 old, u32 new,
>>
>> /*
>> * Note:
>> - * Due to modules and __init, code can disappear and change,
>> + * Due to __init, code can disappear and change,
> Init code should not be modified either because it is black listed in
> recordmcount.c.
>
> I say just change the comment to be something like:
>
> We are paranoid about modifying text, as if a bug were to happen, it
> could cause us to read or write to someplace that could cause harm.
> Carefully read and modify the code with aarch64_insn_*() which uses
> probe_kernel_*(), and make sure what we read is what we expected it to
> be before modifying it.
>
> -- Steve
>
>
>> * we need to protect against faulting as well as code changing.
>> * We do this by aarch64_insn_*() which use the probe_kernel_*().
>> - *
>> - * No lock is held here because all the modifications are run
>> - * through stop_machine().
>> */
>> if (validate) {
>> if (aarch64_insn_read((void *)pc, &replaced))
>
> .
>