On Fri, Aug 1, 2025 at 4:16 AM Steven Rostedt <rost...@goodmis.org> wrote:
>
> From: Steven Rostedt <rost...@goodmis.org>
>
> Several functions need to call btf_put() on the btf pointer before it
> returns leading to using "goto" branches to jump to the end to call
> btf_put(btf). This can be simplified by introducing DEFINE_FREE() to allow
> functions to define the btf descriptor with:
>
>    struct btf *btf __free(btf_put) = NULL;
>
> Then the btf descriptor will always have btf_put() called on it if it
> isn't NULL or ERR before exiting the function.
>
> Where needed, no_free_ptr(btf) is used to assign the btf descriptor to a
> pointer that will be used outside the function.
>
> Signed-off-by: Steven Rostedt (Google) <rost...@goodmis.org>
> ---
>  include/linux/btf.h         |  4 +++
>  kernel/bpf/btf.c            | 56 +++++++++++--------------------------
>  kernel/trace/trace_btf.c    | 15 +++++-----
>  kernel/trace/trace_output.c |  8 ++----
>  4 files changed, 31 insertions(+), 52 deletions(-)
>
> diff --git a/include/linux/btf.h b/include/linux/btf.h
> index b2983706292f..e118c69c4996 100644
> --- a/include/linux/btf.h
> +++ b/include/linux/btf.h
> @@ -8,6 +8,7 @@
>  #include <linux/bpfptr.h>
>  #include <linux/bsearch.h>
>  #include <linux/btf_ids.h>
> +#include <linux/cleanup.h>
>  #include <uapi/linux/btf.h>
>  #include <uapi/linux/bpf.h>
>
> @@ -150,6 +151,9 @@ struct btf *btf_get_by_fd(int fd);
>  int btf_get_info_by_fd(const struct btf *btf,
>                        const union bpf_attr *attr,
>                        union bpf_attr __user *uattr);
> +
> +DEFINE_FREE(btf_put, struct btf *, if (!IS_ERR_OR_NULL(_T)) btf_put(_T))
> +
>  /* Figure out the size of a type_id.  If type_id is a modifier
>   * (e.g. const), it will be resolved to find out the type with size.
>   *
> diff --git a/kernel/bpf/btf.c b/kernel/bpf/btf.c
> index 1d2cf898e21e..480657912c96 100644
> --- a/kernel/bpf/btf.c
> +++ b/kernel/bpf/btf.c
> @@ -3788,7 +3788,7 @@ static int btf_parse_kptr(const struct btf *btf, struct 
> btf_field *field,
>         /* If a matching btf type is found in kernel or module BTFs, kptr_ref
>          * is that BTF, otherwise it's program BTF
>          */
> -       struct btf *kptr_btf;
> +       struct btf *kptr_btf __free(btf_put) = NULL;

Sorry I hate this __free() style.
It's not a simplification, but an obfuscation of code and logic.

--
pw-bot: cr

Reply via email to