linuxppc-dev.lists.ozlabs.org archive mirror
 help / color / mirror / Atom feed
From: Christophe Leroy <christophe.leroy@csgroup.eu>
To: Naveen N Rao <naveen@kernel.org>,
	"linuxppc-dev@lists.ozlabs.org" <linuxppc-dev@lists.ozlabs.org>
Cc: Steven Rostedt <rostedt@goodmis.org>
Subject: Re: [PATCH 15/17] powerpc/ftrace: Implement ftrace_replace_code()
Date: Fri, 23 Jun 2023 05:32:50 +0000	[thread overview]
Message-ID: <d665e56a-4961-2bdb-26c5-8782fd863251@csgroup.eu> (raw)
In-Reply-To: <c28f852225646b0561bbf3c1d22d03f041ace8e0.1687166935.git.naveen@kernel.org>



Le 19/06/2023 à 11:47, Naveen N Rao a écrit :
> Implement ftrace_replace_code() to consolidate logic from the different
> ftrace patching routines: ftrace_make_nop(), ftrace_make_call() and
> ftrace_modify_call(). Note that ftrace_make_call() is still required
> primarily to handle patching modules during their load time. The other
> two routines should no longer be called.
> 
> This lays the groundwork to enable better control in patching ftrace
> locations, including the ability to nop-out preceding profiling
> instructions when ftrace is disabled.
> 
> Signed-off-by: Naveen N Rao <naveen@kernel.org>

Reviewed-by: Christophe Leroy <christophe.leroy@csgroup.eu>


> ---
>   arch/powerpc/kernel/trace/ftrace.c | 173 ++++++++++++++++-------------
>   1 file changed, 96 insertions(+), 77 deletions(-)
> 
> diff --git a/arch/powerpc/kernel/trace/ftrace.c b/arch/powerpc/kernel/trace/ftrace.c
> index 422dd760fbe013..cf9dce77527920 100644
> --- a/arch/powerpc/kernel/trace/ftrace.c
> +++ b/arch/powerpc/kernel/trace/ftrace.c
> @@ -94,104 +94,123 @@ static unsigned long find_ftrace_tramp(unsigned long ip)
>   	return 0;
>   }
>   
> +static int ftrace_get_call_inst(struct dyn_ftrace *rec, unsigned long addr, ppc_inst_t *call_inst)
> +{
> +	unsigned long ip = rec->ip;
> +	unsigned long stub;
> +
> +	if (is_offset_in_branch_range(addr - ip)) {
> +		/* Within range */
> +		stub = addr;
> +#ifdef CONFIG_MODULES
> +	} else if (rec->arch.mod) {
> +		/* Module code would be going to one of the module stubs */
> +		stub = (addr == (unsigned long)ftrace_caller ? rec->arch.mod->arch.tramp :
> +							       rec->arch.mod->arch.tramp_regs);
> +#endif
> +	} else if (core_kernel_text(ip)) {
> +		/* We would be branching to one of our ftrace stubs */
> +		stub = find_ftrace_tramp(ip);
> +		if (!stub) {
> +			pr_err("0x%lx: No ftrace stubs reachable\n", ip);
> +			return -EINVAL;
> +		}
> +	} else {
> +		return -EINVAL;
> +	}
> +
> +	*call_inst = ftrace_create_branch_inst(ip, stub, 1);
> +	return 0;
> +}
> +
>   #ifdef CONFIG_DYNAMIC_FTRACE_WITH_REGS
>   int ftrace_modify_call(struct dyn_ftrace *rec, unsigned long old_addr, unsigned long addr)
>   {
> -	unsigned long tramp, tramp_old, ip = rec->ip;
> -	ppc_inst_t old, new;
> -	struct module *mod;
> -
> -	if (is_offset_in_branch_range(old_addr - ip) && is_offset_in_branch_range(addr - ip)) {
> -		/* Within range */
> -		old = ftrace_create_branch_inst(ip, old_addr, 1);
> -		new = ftrace_create_branch_inst(ip, addr, 1);
> -		return ftrace_modify_code(ip, old, new);
> -	} else if (core_kernel_text(ip)) {
> -		/*
> -		 * We always patch out of range locations to go to the regs
> -		 * variant, so there is nothing to do here
> -		 */
> -		return 0;
> -	} else if (IS_ENABLED(CONFIG_MODULES)) {
> -		/* Module code would be going to one of the module stubs */
> -		mod = rec->arch.mod;
> -		if (addr == (unsigned long)ftrace_caller) {
> -			tramp_old = mod->arch.tramp_regs;
> -			tramp = mod->arch.tramp;
> -		} else {
> -			tramp_old = mod->arch.tramp;
> -			tramp = mod->arch.tramp_regs;
> -		}
> -		old = ftrace_create_branch_inst(ip, tramp_old, 1);
> -		new = ftrace_create_branch_inst(ip, tramp, 1);
> -		return ftrace_modify_code(ip, old, new);
> -	}
> -
> +	/* This should never be called since we override ftrace_replace_code() */
> +	WARN_ON(1);
>   	return -EINVAL;
>   }
>   #endif
>   
>   int ftrace_make_call(struct dyn_ftrace *rec, unsigned long addr)
>   {
> -	unsigned long tramp, ip = rec->ip;
>   	ppc_inst_t old, new;
> -	struct module *mod;
> +	int ret;
> +
> +	/* This can only ever be called during module load */
> +	if (WARN_ON(!IS_ENABLED(CONFIG_MODULES) || core_kernel_text(rec->ip)))
> +		return -EINVAL;
>   
>   	old = ppc_inst(PPC_RAW_NOP());
> -	if (is_offset_in_branch_range(addr - ip)) {
> -		/* Within range */
> -		new = ftrace_create_branch_inst(ip, addr, 1);
> -		return ftrace_modify_code(ip, old, new);
> -	} else if (core_kernel_text(ip)) {
> -		/* We would be branching to one of our ftrace tramps */
> -		tramp = find_ftrace_tramp(ip);
> -		if (!tramp) {
> -			pr_err("0x%lx: No ftrace trampolines reachable\n", ip);
> -			return -EINVAL;
> -		}
> -		new = ftrace_create_branch_inst(ip, tramp, 1);
> -		return ftrace_modify_code(ip, old, new);
> -	} else if (IS_ENABLED(CONFIG_MODULES)) {
> -		/* Module code would be going to one of the module stubs */
> -		mod = rec->arch.mod;
> -		tramp = (addr == (unsigned long)ftrace_caller ? mod->arch.tramp : mod->arch.tramp_regs);
> -		new = ftrace_create_branch_inst(ip, tramp, 1);
> -		return ftrace_modify_code(ip, old, new);
> -	}
> +	ret = ftrace_get_call_inst(rec, addr, &new);
> +	if (ret)
> +		return ret;
>   
> -	return -EINVAL;
> +	return ftrace_modify_code(rec->ip, old, new);
>   }
>   
>   int ftrace_make_nop(struct module *mod, struct dyn_ftrace *rec, unsigned long addr)
>   {
> -	unsigned long tramp, ip = rec->ip;
> -	ppc_inst_t old, new;
> +	/*
> +	 * This should never be called since we override ftrace_replace_code(),
> +	 * as well as ftrace_init_nop()
> +	 */
> +	WARN_ON(1);
> +	return -EINVAL;
> +}
>   
> -	/* Nop-out the ftrace location */
> -	new = ppc_inst(PPC_RAW_NOP());
> -	if (is_offset_in_branch_range(addr - ip)) {
> -		/* Within range */
> -		old = ftrace_create_branch_inst(ip, addr, 1);
> -		return ftrace_modify_code(ip, old, new);
> -	} else if (core_kernel_text(ip)) {
> -		/* We would be branching to one of our ftrace tramps */
> -		tramp = find_ftrace_tramp(ip);
> -		if (!tramp) {
> -			pr_err("0x%lx: No ftrace trampolines reachable\n", ip);
> -			return -EINVAL;
> +void ftrace_replace_code(int enable)
> +{
> +	ppc_inst_t old, new, call_inst, new_call_inst;
> +	ppc_inst_t nop_inst = ppc_inst(PPC_RAW_NOP());
> +	unsigned long ip, new_addr, addr;
> +	struct ftrace_rec_iter *iter;
> +	struct dyn_ftrace *rec;
> +	int ret = 0, update;
> +
> +	for_ftrace_rec_iter(iter) {
> +		rec = ftrace_rec_iter_record(iter);
> +		ip = rec->ip;
> +
> +		if (rec->flags & FTRACE_FL_DISABLED && !(rec->flags & FTRACE_FL_ENABLED))
> +			continue;
> +
> +		addr = ftrace_get_addr_curr(rec);
> +		new_addr = ftrace_get_addr_new(rec);
> +		update = ftrace_update_record(rec, enable);
> +
> +		switch (update) {
> +		case FTRACE_UPDATE_IGNORE:
> +		default:
> +			continue;
> +		case FTRACE_UPDATE_MODIFY_CALL:
> +			ret = ftrace_get_call_inst(rec, new_addr, &new_call_inst);
> +			ret |= ftrace_get_call_inst(rec, addr, &call_inst);
> +			old = call_inst;
> +			new = new_call_inst;
> +			break;
> +		case FTRACE_UPDATE_MAKE_NOP:
> +			ret = ftrace_get_call_inst(rec, addr, &call_inst);
> +			old = call_inst;
> +			new = nop_inst;
> +			break;
> +		case FTRACE_UPDATE_MAKE_CALL:
> +			ret = ftrace_get_call_inst(rec, new_addr, &call_inst);
> +			old = nop_inst;
> +			new = call_inst;
> +			break;
>   		}
> -		old = ftrace_create_branch_inst(ip, tramp, 1);
> -		return ftrace_modify_code(ip, old, new);
> -	} else if (IS_ENABLED(CONFIG_MODULES)) {
> -		/* Module code would be going to one of the module stubs */
> -		if (!mod)
> -			mod = rec->arch.mod;
> -		tramp = (addr == (unsigned long)ftrace_caller ? mod->arch.tramp : mod->arch.tramp_regs);
> -		old = ftrace_create_branch_inst(ip, tramp, 1);
> -		return ftrace_modify_code(ip, old, new);
> +
> +		if (!ret)
> +			ret = ftrace_modify_code(ip, old, new);
> +		if (ret)
> +			goto out;
>   	}
>   
> -	return -EINVAL;
> +out:
> +	if (ret)
> +		ftrace_bug(ret, rec);
> +	return;
>   }
>   
>   int ftrace_init_nop(struct module *mod, struct dyn_ftrace *rec)

  reply	other threads:[~2023-06-23  5:35 UTC|newest]

Thread overview: 39+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-06-19  9:47 [PATCH 00/17] powerpc/ftrace: refactor and add support for -fpatchable-function-entry Naveen N Rao
2023-06-19  9:47 ` [PATCH 01/17] powerpc/ftrace: Fix dropping weak symbols with older toolchains Naveen N Rao
2023-06-23  5:10   ` Christophe Leroy
2023-06-19  9:47 ` [PATCH 02/17] powerpc/module: Remove unused .ftrace.tramp section Naveen N Rao
2023-06-23  5:12   ` Christophe Leroy
2023-06-19  9:47 ` [PATCH 03/17] powerpc64/ftrace: Move ELFv1 and -pg support code into a separate file Naveen N Rao
2023-06-23  5:13   ` Christophe Leroy
2023-06-19  9:47 ` [PATCH 04/17] powerpc/ftrace: Simplify function_graph support in ftrace.c Naveen N Rao
2023-06-23  5:14   ` Christophe Leroy
2023-06-19  9:47 ` [PATCH 05/17] powerpc/ftrace: Use FTRACE_REGS_ADDR to identify the correct ftrace trampoline Naveen N Rao
2023-06-23  5:15   ` Christophe Leroy
2023-06-19  9:47 ` [PATCH 06/17] powerpc/ftrace: Extend ftrace support for large kernels to ppc32 Naveen N Rao
2023-06-23  5:21   ` Christophe Leroy
2023-06-19  9:47 ` [PATCH 07/17] powerpc/ftrace: Consolidate ftrace support into fewer files Naveen N Rao
2023-06-23  5:25   ` Christophe Leroy
2023-06-28  7:32     ` Naveen N Rao
2023-06-19  9:47 ` [PATCH 08/17] powerpc/ftrace: Refactor ftrace_modify_code() Naveen N Rao
2023-06-23  5:27   ` Christophe Leroy
2023-06-19  9:47 ` [PATCH 09/17] powerpc/ftrace: Stop re-purposing linker generated long branches for ftrace Naveen N Rao
2023-06-23  5:28   ` Christophe Leroy
2023-06-19  9:47 ` [PATCH 10/17] powerpc/ftrace: Add separate ftrace_init_nop() with additional validation Naveen N Rao
2023-06-23  5:29   ` Christophe Leroy
2023-06-19  9:47 ` [PATCH 11/17] powerpc/ftrace: Simplify ftrace_make_nop() Naveen N Rao
2023-06-23  5:30   ` Christophe Leroy
2023-06-19  9:47 ` [PATCH 12/17] powerpc/ftrace: Simplify ftrace_make_call() Naveen N Rao
2023-06-23  5:30   ` Christophe Leroy
2023-06-19  9:47 ` [PATCH 13/17] powerpc/ftrace: Simplify ftrace_modify_call() Naveen N Rao
2023-06-23  5:31   ` Christophe Leroy
2023-06-19  9:47 ` [PATCH 14/17] powerpc/ftrace: Replace use of ftrace_call_replace() with ftrace_create_branch_inst() Naveen N Rao
2023-06-23  5:32   ` Christophe Leroy
2023-06-19  9:47 ` [PATCH 15/17] powerpc/ftrace: Implement ftrace_replace_code() Naveen N Rao
2023-06-23  5:32   ` Christophe Leroy [this message]
2023-06-19  9:47 ` [PATCH 16/17] powerpc/ftrace: Add support for -fpatchable-function-entry Naveen N Rao
2023-06-23  5:37   ` Christophe Leroy
2023-06-28  7:40     ` Naveen N Rao
2023-06-19  9:47 ` [PATCH 17/17] powerpc/ftrace: Create a dummy stackframe to fix stack unwind Naveen N Rao
2023-06-23  5:40   ` Christophe Leroy
2023-06-28  7:43     ` Naveen N Rao
2023-08-23 11:55 ` [PATCH 00/17] powerpc/ftrace: refactor and add support for -fpatchable-function-entry Michael Ellerman

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=d665e56a-4961-2bdb-26c5-8782fd863251@csgroup.eu \
    --to=christophe.leroy@csgroup.eu \
    --cc=linuxppc-dev@lists.ozlabs.org \
    --cc=naveen@kernel.org \
    --cc=rostedt@goodmis.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;
as well as URLs for NNTP newsgroup(s).