From: Andrii Nakryiko <andrii.nakryiko@gmail.com>
To: Lin Yikai <yikai.lin@vivo.com>
Cc: Alexei Starovoitov <ast@kernel.org>,
Daniel Borkmann <daniel@iogearbox.net>,
Andrii Nakryiko <andrii@kernel.org>,
Martin KaFai Lau <martin.lau@linux.dev>,
Eduard Zingerman <eddyz87@gmail.com>, Song Liu <song@kernel.org>,
Yonghong Song <yonghong.song@linux.dev>,
John Fastabend <john.fastabend@gmail.com>,
KP Singh <kpsingh@kernel.org>,
Stanislav Fomichev <sdf@fomichev.me>, Hao Luo <haoluo@google.com>,
Jiri Olsa <jolsa@kernel.org>,
Matt Bobrowski <mattbobrowski@google.com>,
Steven Rostedt <rostedt@goodmis.org>,
Masami Hiramatsu <mhiramat@kernel.org>,
Mathieu Desnoyers <mathieu.desnoyers@efficios.com>,
Mykola Lysenko <mykolal@fb.com>, Shuah Khan <shuah@kernel.org>,
linux-kernel@vger.kernel.org, bpf@vger.kernel.org,
linux-trace-kernel@vger.kernel.org,
linux-kselftest@vger.kernel.org, opensource.kernel@vivo.com
Subject: Re: [PATCH bpf-next v1 1/3] bpf: Add bpf_file_d_path helper
Date: Fri, 19 Jul 2024 11:55:20 -0700 [thread overview]
Message-ID: <CAEf4BzZsqcnn8cWSig6Yu-RHvpsFVmJfeNT8CmHuHpN_rTrCwg@mail.gmail.com> (raw)
In-Reply-To: <20240718115153.1967859-2-yikai.lin@vivo.com>
On Thu, Jul 18, 2024 at 4:53 AM Lin Yikai <yikai.lin@vivo.com> wrote:
>
> Add the "bpf_file_d_path" helper function
> to retrieve the path from a struct file object.
> But there is no need to include vmlinux.h
> or reference the definition of struct file.
>
> Additionally, update the bpf.h tools uapi header.
>
> Signed-off-by: Lin Yikai <yikai.lin@vivo.com>
> ---
> include/uapi/linux/bpf.h | 20 ++++++++++++++++++++
> kernel/trace/bpf_trace.c | 34 ++++++++++++++++++++++++++++++++++
> tools/include/uapi/linux/bpf.h | 20 ++++++++++++++++++++
> 3 files changed, 74 insertions(+)
>
> diff --git a/include/uapi/linux/bpf.h b/include/uapi/linux/bpf.h
> index 35bcf52dbc65..7e5cec61a877 100644
> --- a/include/uapi/linux/bpf.h
> +++ b/include/uapi/linux/bpf.h
> @@ -5792,6 +5792,25 @@ union bpf_attr {
> * 0 on success.
> *
> * **-ENOENT** if the bpf_local_storage cannot be found.
> + *
> + * long bpf_file_d_path(void *file, char *dst, u32 size)
> + * Description
> + * Return full path for the given *file* object.
> + *
> + * In order to solve issues where certain eBPF programs can not include
> + * the definition of struct file or vmlinux.h
> + * due to their complexity and conflicts on some operating system,
> + * the variable *file* here is declared as type void*
> + * instead of the traditional struct file*.
> + * It will be forcibly converted into type struct file* later.
> + *
> + * If the path is larger than *size*, then only *size*
> + * bytes will be copied to *dst*
> + *
> + * Return
> + * On success, the strictly positive length of the string,
> + * including the trailing NULL character. On error, a negative
> + * value.
> */
> #define ___BPF_FUNC_MAPPER(FN, ctx...) \
> FN(unspec, 0, ##ctx) \
> @@ -6006,6 +6025,7 @@ union bpf_attr {
> FN(user_ringbuf_drain, 209, ##ctx) \
> FN(cgrp_storage_get, 210, ##ctx) \
> FN(cgrp_storage_delete, 211, ##ctx) \
> + FN(file_d_path, 212, ##ctx) \
> /* */
>
> /* backwards-compatibility macros for users of __BPF_FUNC_MAPPER that don't
> diff --git a/kernel/trace/bpf_trace.c b/kernel/trace/bpf_trace.c
> index cd098846e251..70fde7f20e97 100644
> --- a/kernel/trace/bpf_trace.c
> +++ b/kernel/trace/bpf_trace.c
> @@ -1257,6 +1257,38 @@ static const struct bpf_func_proto bpf_get_func_arg_cnt_proto = {
> .arg1_type = ARG_PTR_TO_CTX,
> };
>
> +BPF_CALL_3(bpf_file_d_path, void *, file, char*, dst, u32, size)
> +{
> + long len;
> + struct file copy;
> + char *ptr;
> +
> + if (!size)
> + return 0;
> +
> + len = copy_from_kernel_nofault(©, (struct file *)file, sizeof(struct file));
> + if (len < 0)
> + return len;
> +
> + ptr = d_path(&(copy.f_path), dst, size);
> + if (IS_ERR(ptr)) {
> + len = PTR_ERR(ptr);
> + } else {
> + len = dst + size - ptr;
> + memmove(dst, ptr, len);
> + }
> + return len;
> +}
> +
> +const struct bpf_func_proto bpf_file_d_path_proto = {
> + .func = bpf_file_d_path,
> + .gpl_only = false,
> + .ret_type = RET_INTEGER,
> + .arg1_type = ARG_ANYTHING,
you can't just accept any random value as `struct file *`, this is a
complete no-go. It will have to accept some sort of PTR_TRUSTED
argument, be added as kfunc, etc, etc. We had earlier discussion
around this, I don't remember all the details, but this is definitely
not the way forward.
> + .arg2_type = ARG_PTR_TO_MEM,
> + .arg3_type = ARG_CONST_SIZE_OR_ZERO,
> +};
> +
> #ifdef CONFIG_KEYS
> __bpf_kfunc_start_defs();
>
> @@ -1629,6 +1661,8 @@ bpf_tracing_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
> return &bpf_find_vma_proto;
> case BPF_FUNC_trace_vprintk:
> return bpf_get_trace_vprintk_proto();
> + case BPF_FUNC_file_d_path:
> + return &bpf_file_d_path_proto;
> default:
> return bpf_base_func_proto(func_id, prog);
> }
> diff --git a/tools/include/uapi/linux/bpf.h b/tools/include/uapi/linux/bpf.h
> index 35bcf52dbc65..7e5cec61a877 100644
> --- a/tools/include/uapi/linux/bpf.h
> +++ b/tools/include/uapi/linux/bpf.h
> @@ -5792,6 +5792,25 @@ union bpf_attr {
> * 0 on success.
> *
> * **-ENOENT** if the bpf_local_storage cannot be found.
> + *
> + * long bpf_file_d_path(void *file, char *dst, u32 size)
> + * Description
> + * Return full path for the given *file* object.
> + *
> + * In order to solve issues where certain eBPF programs can not include
> + * the definition of struct file or vmlinux.h
> + * due to their complexity and conflicts on some operating system,
> + * the variable *file* here is declared as type void*
> + * instead of the traditional struct file*.
> + * It will be forcibly converted into type struct file* later.
> + *
> + * If the path is larger than *size*, then only *size*
> + * bytes will be copied to *dst*
> + *
> + * Return
> + * On success, the strictly positive length of the string,
> + * including the trailing NULL character. On error, a negative
> + * value.
> */
> #define ___BPF_FUNC_MAPPER(FN, ctx...) \
> FN(unspec, 0, ##ctx) \
> @@ -6006,6 +6025,7 @@ union bpf_attr {
> FN(user_ringbuf_drain, 209, ##ctx) \
> FN(cgrp_storage_get, 210, ##ctx) \
> FN(cgrp_storage_delete, 211, ##ctx) \
> + FN(file_d_path, 212, ##ctx) \
> /* */
>
> /* backwards-compatibility macros for users of __BPF_FUNC_MAPPER that don't
> --
> 2.34.1
>
next prev parent reply other threads:[~2024-07-19 18:55 UTC|newest]
Thread overview: 5+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-07-18 11:51 [PATCH bpf-next v1 0/3] add bpf_file_d_path helper and selftests Lin Yikai
2024-07-18 11:51 ` [PATCH bpf-next v1 1/3] bpf: Add bpf_file_d_path helper Lin Yikai
2024-07-19 18:55 ` Andrii Nakryiko [this message]
2024-07-18 11:51 ` [PATCH bpf-next v1 2/3] selftests/bpf:Adding test for " Lin Yikai
2024-07-18 17:16 ` [PATCH bpf-next v1 0/3] add bpf_file_d_path helper and selftests Martin KaFai Lau
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=CAEf4BzZsqcnn8cWSig6Yu-RHvpsFVmJfeNT8CmHuHpN_rTrCwg@mail.gmail.com \
--to=andrii.nakryiko@gmail.com \
--cc=andrii@kernel.org \
--cc=ast@kernel.org \
--cc=bpf@vger.kernel.org \
--cc=daniel@iogearbox.net \
--cc=eddyz87@gmail.com \
--cc=haoluo@google.com \
--cc=john.fastabend@gmail.com \
--cc=jolsa@kernel.org \
--cc=kpsingh@kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-kselftest@vger.kernel.org \
--cc=linux-trace-kernel@vger.kernel.org \
--cc=martin.lau@linux.dev \
--cc=mathieu.desnoyers@efficios.com \
--cc=mattbobrowski@google.com \
--cc=mhiramat@kernel.org \
--cc=mykolal@fb.com \
--cc=opensource.kernel@vivo.com \
--cc=rostedt@goodmis.org \
--cc=sdf@fomichev.me \
--cc=shuah@kernel.org \
--cc=song@kernel.org \
--cc=yikai.lin@vivo.com \
--cc=yonghong.song@linux.dev \
/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).