public inbox for linux-trace-kernel@vger.kernel.org
 help / color / mirror / Atom feed
From: Mark Rutland <mark.rutland@arm.com>
To: Florent Revest <revest@chromium.org>
Cc: linux-kernel@vger.kernel.org, linux-trace-kernel@vger.kernel.org,
	rostedt@goodmis.org, mhiramat@kernel.org, ast@kernel.org,
	daniel@iogearbox.net, kpsingh@kernel.org, jolsa@kernel.org
Subject: Re: [PATCH 1/7] ftrace: Let unregister_ftrace_direct_multi() call ftrace_free_filter()
Date: Tue, 21 Mar 2023 09:47:17 +0000	[thread overview]
Message-ID: <ZBl9JahKr+50sBW0@FVFF77S0Q05N> (raw)
In-Reply-To: <20230316173811.1223508-2-revest@chromium.org>

On Thu, Mar 16, 2023 at 06:38:05PM +0100, Florent Revest wrote:
> A common pattern when using the ftrace_direct_multi API is to unregister
> the ops and also immediately free its filter. We've noticed it's very
> easy for users to miss calling ftrace_free_filter().
> 
> This adds a "free_filters" argument to unregister_ftrace_direct_multi()
> to both remind the user they should free filters and also to make their
> life easier.
> 
> Suggested-by: Steven Rostedt <rostedt@goodmis.org>
> Signed-off-by: Florent Revest <revest@chromium.org>
> ---
>  include/linux/ftrace.h                      | 6 ++++--
>  kernel/bpf/trampoline.c                     | 2 +-
>  kernel/trace/ftrace.c                       | 6 +++++-
>  samples/ftrace/ftrace-direct-multi-modify.c | 3 +--
>  samples/ftrace/ftrace-direct-multi.c        | 3 +--
>  5 files changed, 12 insertions(+), 8 deletions(-)

This looks good to me; I see that the BPF code frees the filter in
bpf_trampoline_put(), so it not doing so via unregister_ftrace_direct_multi()
looks fine. FWIW:

Acked-by: Mark Rutland <mark.rutland@arm.com>

Mark.

> 
> diff --git a/include/linux/ftrace.h b/include/linux/ftrace.h
> index 366c730beaa3..5b68ee874bc1 100644
> --- a/include/linux/ftrace.h
> +++ b/include/linux/ftrace.h
> @@ -407,7 +407,8 @@ int ftrace_modify_direct_caller(struct ftrace_func_entry *entry,
>  				unsigned long new_addr);
>  unsigned long ftrace_find_rec_direct(unsigned long ip);
>  int register_ftrace_direct_multi(struct ftrace_ops *ops, unsigned long addr);
> -int unregister_ftrace_direct_multi(struct ftrace_ops *ops, unsigned long addr);
> +int unregister_ftrace_direct_multi(struct ftrace_ops *ops, unsigned long addr,
> +				   bool free_filters);
>  int modify_ftrace_direct_multi(struct ftrace_ops *ops, unsigned long addr);
>  int modify_ftrace_direct_multi_nolock(struct ftrace_ops *ops, unsigned long addr);
>  
> @@ -446,7 +447,8 @@ static inline int register_ftrace_direct_multi(struct ftrace_ops *ops, unsigned
>  {
>  	return -ENODEV;
>  }
> -static inline int unregister_ftrace_direct_multi(struct ftrace_ops *ops, unsigned long addr)
> +static inline int unregister_ftrace_direct_multi(struct ftrace_ops *ops, unsigned long addr,
> +						 bool free_filters)
>  {
>  	return -ENODEV;
>  }
> diff --git a/kernel/bpf/trampoline.c b/kernel/bpf/trampoline.c
> index d0ed7d6f5eec..88bc23f1e10a 100644
> --- a/kernel/bpf/trampoline.c
> +++ b/kernel/bpf/trampoline.c
> @@ -198,7 +198,7 @@ static int unregister_fentry(struct bpf_trampoline *tr, void *old_addr)
>  	int ret;
>  
>  	if (tr->func.ftrace_managed)
> -		ret = unregister_ftrace_direct_multi(tr->fops, (long)old_addr);
> +		ret = unregister_ftrace_direct_multi(tr->fops, (long)old_addr, false);
>  	else
>  		ret = bpf_arch_text_poke(ip, BPF_MOD_CALL, old_addr, NULL);
>  
> diff --git a/kernel/trace/ftrace.c b/kernel/trace/ftrace.c
> index 29baa97d0d53..fa379cf91fdb 100644
> --- a/kernel/trace/ftrace.c
> +++ b/kernel/trace/ftrace.c
> @@ -5804,7 +5804,8 @@ EXPORT_SYMBOL_GPL(register_ftrace_direct_multi);
>   *  0 on success
>   *  -EINVAL - The @ops object was not properly registered.
>   */
> -int unregister_ftrace_direct_multi(struct ftrace_ops *ops, unsigned long addr)
> +int unregister_ftrace_direct_multi(struct ftrace_ops *ops, unsigned long addr,
> +				   bool free_filters)
>  {
>  	struct ftrace_hash *hash = ops->func_hash->filter_hash;
>  	int err;
> @@ -5822,6 +5823,9 @@ int unregister_ftrace_direct_multi(struct ftrace_ops *ops, unsigned long addr)
>  	/* cleanup for possible another register call */
>  	ops->func = NULL;
>  	ops->trampoline = 0;
> +
> +	if (free_filters)
> +		ftrace_free_filter(ops);
>  	return err;
>  }
>  EXPORT_SYMBOL_GPL(unregister_ftrace_direct_multi);
> diff --git a/samples/ftrace/ftrace-direct-multi-modify.c b/samples/ftrace/ftrace-direct-multi-modify.c
> index b58c594efb51..196b43971cb5 100644
> --- a/samples/ftrace/ftrace-direct-multi-modify.c
> +++ b/samples/ftrace/ftrace-direct-multi-modify.c
> @@ -151,8 +151,7 @@ static int __init ftrace_direct_multi_init(void)
>  static void __exit ftrace_direct_multi_exit(void)
>  {
>  	kthread_stop(simple_tsk);
> -	unregister_ftrace_direct_multi(&direct, my_tramp);
> -	ftrace_free_filter(&direct);
> +	unregister_ftrace_direct_multi(&direct, my_tramp, true);
>  }
>  
>  module_init(ftrace_direct_multi_init);
> diff --git a/samples/ftrace/ftrace-direct-multi.c b/samples/ftrace/ftrace-direct-multi.c
> index c27cf130c319..ea0e88ee5e43 100644
> --- a/samples/ftrace/ftrace-direct-multi.c
> +++ b/samples/ftrace/ftrace-direct-multi.c
> @@ -78,8 +78,7 @@ static int __init ftrace_direct_multi_init(void)
>  
>  static void __exit ftrace_direct_multi_exit(void)
>  {
> -	unregister_ftrace_direct_multi(&direct, (unsigned long) my_tramp);
> -	ftrace_free_filter(&direct);
> +	unregister_ftrace_direct_multi(&direct, (unsigned long) my_tramp, true);
>  }
>  
>  module_init(ftrace_direct_multi_init);
> -- 
> 2.40.0.rc2.332.ga46443480c-goog
> 

  reply	other threads:[~2023-03-21  9:47 UTC|newest]

Thread overview: 22+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-03-16 17:38 [PATCH 0/7] Refactor ftrace direct call APIs Florent Revest
2023-03-16 17:38 ` [PATCH 1/7] ftrace: Let unregister_ftrace_direct_multi() call ftrace_free_filter() Florent Revest
2023-03-21  9:47   ` Mark Rutland [this message]
2023-03-21 10:40     ` Jiri Olsa
2023-03-16 17:38 ` [PATCH 2/7] ftrace: Replace uses of _ftrace_direct APIs with _ftrace_direct_multi Florent Revest
2023-03-16 17:38 ` [PATCH 3/7] ftrace: Remove the legacy _ftrace_direct API Florent Revest
2023-03-16 17:38 ` [PATCH 4/7] ftrace: Rename _ftrace_direct_multi APIs to _ftrace_direct APIs Florent Revest
2023-03-19 15:29   ` Jiri Olsa
2023-03-19 17:55     ` Steven Rostedt
2023-03-19 18:22       ` Jiri Olsa
2023-03-20  0:47         ` Steven Rostedt
2023-03-20  8:05           ` Jiri Olsa
2023-03-20 17:41             ` Florent Revest
2023-03-16 17:38 ` [PATCH 5/7] ftrace: Store direct called addresses in their ops Florent Revest
2023-03-19 15:29   ` Jiri Olsa
2023-03-19 17:54     ` Steven Rostedt
2023-03-19 18:54       ` Jiri Olsa
2023-03-20 17:45         ` Florent Revest
2023-03-20 21:31           ` Steven Rostedt
2023-03-21 10:20             ` Mark Rutland
2023-03-16 17:38 ` [PATCH 6/7] ftrace: Make DIRECT_CALLS work WITH_ARGS and !WITH_REGS Florent Revest
2023-03-16 17:38 ` [PATCH 7/7] ftrace: selftest: remove broken trace_direct_tramp Florent Revest

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=ZBl9JahKr+50sBW0@FVFF77S0Q05N \
    --to=mark.rutland@arm.com \
    --cc=ast@kernel.org \
    --cc=daniel@iogearbox.net \
    --cc=jolsa@kernel.org \
    --cc=kpsingh@kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-trace-kernel@vger.kernel.org \
    --cc=mhiramat@kernel.org \
    --cc=revest@chromium.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