mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Steven Rostedt <rostedt@goodmis.org>
To: Petr Mladek <pmladek@suse.cz>
Cc: Frederic Weisbecker <fweisbec@gmail.com>,
	Masami Hiramatsu <masami.hiramatsu.pt@hitachi.com>,
	"Paul E. McKenney" <paulmck@linux.vnet.ibm.com>,
	Jiri Kosina <jkosina@suse.cz>,
	linux-kernel@vger.kernel.org, x86@kernel.org
Subject: Re: [PATCH v6 6/8] x86: modify ftrace function using the new int3-based framework
Date: Tue, 14 Jan 2014 20:04:45 -0500	[thread overview]
Message-ID: <20140114200445.6c1ce0ea@gandalf.local.home> (raw)
In-Reply-To: <1386690140-19941-7-git-send-email-pmladek@suse.cz>

On Tue, 10 Dec 2013 16:42:18 +0100
Petr Mladek <pmladek@suse.cz> wrote:

> The commit fd4363fff3d9 (x86: Introduce int3 (breakpoint)-based instruction
> patching) uses the same technique that has been used in ftrace since 08d636b
> ("ftrace/x86: Have arch x86_64 use breakpoints instead of stop machine")
> 
> It would be great to use the new generic implementation and clean up the x86
> ftrace code a bit.
> 
> Let's start with ftrace_modify_code. It does basically the same as text_poke_bp.
> In addition, it checks whether the replaced code is the expected one to avoid
> problems with modules. The checking code can be split from
> ftrace_modify_code_direct.
> 
> Note that we do not longer need to set modifying_ftrace_code in
> ftrace_update_ftrace_func. The int3 interrupt will be handled by
> poke_int3_handler.
> 
> Signed-off-by: Petr Mladek <pmladek@suse.cz>
> ---
>  arch/x86/kernel/ftrace.c | 120 +++++++++++++++++++----------------------------
>  1 file changed, 47 insertions(+), 73 deletions(-)
> 
> diff --git a/arch/x86/kernel/ftrace.c b/arch/x86/kernel/ftrace.c
> index d4bdd253fea7..5ade40e4a175 100644
> --- a/arch/x86/kernel/ftrace.c
> +++ b/arch/x86/kernel/ftrace.c
> @@ -52,6 +52,33 @@ union ftrace_code_union {
>  	} __attribute__((packed));
>  };
>  
> +/*
> + * Note: Due to modules and __init, code can
> + *  disappear and change, we need to protect against faulting
> + *  as well as code changing. We do this by using the
> + *  probe_kernel_* functions.

Actually the above comment is no longer accurate. I know that you just
copied it from my code, and this is my fault. But instead of keeping an
inaccurate comment around, lets fix it.

/*
 * Note: Modifying kernel code is extremely critical to get right.
 *  We must try to detect any error, and we should only change code
 *  with code that we expect to be changed. Always test that the old
 *  text is what we expect it to be before we modify it, and fail
 *  if it is not.
 */

> + */
> +static int
> +ftrace_check_code(unsigned long ip, unsigned const char *expected)
> +{
> +	unsigned char actual[MCOUNT_INSN_SIZE];
> +
> +	if (probe_kernel_read(actual, (void *)ip, MCOUNT_INSN_SIZE))
> +		return -EFAULT;
> +
> +	if (memcmp(actual, expected, MCOUNT_INSN_SIZE) != 0) {
> +		WARN_ONCE(1,
> +			  "ftrace check failed: %x %x%x%x%x vs. %x %x%x%x%x\n",
> +			  actual[0],
> +			  actual[1], actual[2], actual[3], actual[4],
> +			  expected[0],
> +			  expected[1], expected[2], expected[3], expected[4]);
> +		return -EINVAL;

Hmm, I guess I'm fine with warning here like this, but it is also
covered in kernel/ftrace.c too, with the return code o -EINVAL.

But duplicate warnings may not be that bad.

> +	}
> +
> +	return 0;
> +}
> +
>  static int ftrace_calc_offset(long ip, long addr)
>  {
>  	return (int)(addr - ip);
> @@ -103,28 +130,12 @@ static int
>  ftrace_modify_code_direct(unsigned long ip, unsigned const char *old_code,
>  		   unsigned const char *new_code)
>  {
> -	unsigned char replaced[MCOUNT_INSN_SIZE];
> -
> -	/*
> -	 * Note: Due to modules and __init, code can
> -	 *  disappear and change, we need to protect against faulting
> -	 *  as well as code changing. We do this by using the
> -	 *  probe_kernel_* functions.
> -	 *
> -	 * No real locking needed, this code is run through
> -	 * kstop_machine, or before SMP starts.
> -	 */
> +	int ret;
>  
> -	/* read the text we want to modify */
> -	if (probe_kernel_read(replaced, (void *)ip, MCOUNT_INSN_SIZE))
> -		return -EFAULT;
> -
> -	/* Make sure it is what we expect it to be */
> -	if (memcmp(replaced, old_code, MCOUNT_INSN_SIZE) != 0)
> -		return -EINVAL;
> +	ret = ftrace_check_code(ip, old_code);

Wait? If this fails, why not return with ret? Otherwise, we just
returned with success!

-- Steve

>  
>  	/* replace the text with the new text */
> -	if (do_ftrace_mod_code(ip, new_code))
> +	if (!ret && do_ftrace_mod_code(ip, new_code))
>  		return -EPERM;
>  
>  	sync_core();
> @@ -132,6 +143,22 @@ ftrace_modify_code_direct(unsigned long ip, unsigned const char *old_code,
>  	return 0;
>  }
>  
> +static int
> +ftrace_modify_code(unsigned long ip, unsigned const char *old_code,
> +		   unsigned const char *new_code)
> +{
> +	int ret;
> +
> +	ret = ftrace_check_code(ip, old_code);
> +
> +	/* replace the text with the new text */
> +	if (!ret)
> +		ret = text_poke_bp((void *)ip, new_code, MCOUNT_INSN_SIZE,
> +				   (void *)ip + MCOUNT_INSN_SIZE);
> +
> +	return ret;
> +}
> +

  reply	other threads:[~2014-01-15  1:04 UTC|newest]

Thread overview: 31+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2013-12-10 15:42 [PATCH v6 0/8] x86: use new text_poke_bp in ftrace Petr Mladek
2013-12-10 15:42 ` [PATCH v6 1/8] x86: allow to handle errors in text_poke function family Petr Mladek
2013-12-11  2:52   ` Masami Hiramatsu
2014-01-14 23:20   ` Steven Rostedt
2014-01-21 13:00     ` Petr Mladek
2014-01-21 14:02       ` Steven Rostedt
2014-01-22  0:52         ` Masami Hiramatsu
2014-01-22  1:18           ` Steven Rostedt
2013-12-10 15:42 ` [PATCH v6 2/8] x86: allow to call text_poke_bp during boot Petr Mladek
2013-12-10 15:46   ` Borislav Petkov
2013-12-10 16:01     ` Steven Rostedt
2013-12-10 16:08       ` Borislav Petkov
2013-12-10 16:44         ` Petr Mladek
2013-12-10 15:42 ` [PATCH v6 3/8] x86: add generic function to modify more calls using int3 framework Petr Mladek
2014-01-15  0:33   ` Steven Rostedt
2014-01-15  8:18     ` Masami Hiramatsu
2014-01-15 14:11       ` Steven Rostedt
2014-01-21 13:50     ` Petr Mladek
2014-01-21 14:07       ` Steven Rostedt
2013-12-10 15:42 ` [PATCH v6 4/8] x86: speed up int3-based patching using direct write Petr Mladek
2014-01-15  0:45   ` Steven Rostedt
2013-12-10 15:42 ` [PATCH v6 5/8] x86: do not trace __probe_kernel_read Petr Mladek
2014-01-15  0:51   ` Steven Rostedt
2013-12-10 15:42 ` [PATCH v6 6/8] x86: modify ftrace function using the new int3-based framework Petr Mladek
2014-01-15  1:04   ` Steven Rostedt [this message]
2013-12-10 15:42 ` [PATCH v6 7/8] x86: patch all traced function calls using the " Petr Mladek
2014-01-15 15:47   ` Steven Rostedt
2014-01-22 13:20     ` Petr Mladek
2014-01-23 14:21       ` Petr Mladek
2014-01-23 16:10         ` Steven Rostedt
2013-12-10 15:42 ` [PATCH v6 8/8] x86: enable/disable ftrace graph call using new " Petr Mladek

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20140114200445.6c1ce0ea@gandalf.local.home \
    --to=rostedt@goodmis.org \
    --cc=fweisbec@gmail.com \
    --cc=jkosina@suse.cz \
    --cc=linux-kernel@vger.kernel.org \
    --cc=masami.hiramatsu.pt@hitachi.com \
    --cc=paulmck@linux.vnet.ibm.com \
    --cc=pmladek@suse.cz \
    --cc=x86@kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®