Re: [PATCH v2 2/5] bpf: Add a bpf_sock_from_file helper

From: KP Singh
Date: Thu Nov 19 2020 - 16:52:04 EST


On Thu, Nov 19, 2020 at 5:27 PM Florent Revest <revest@xxxxxxxxxxxx> wrote:
>
> From: Florent Revest <revest@xxxxxxxxxx>
>
> While eBPF programs can check whether a file is a socket by file->f_op
> == &socket_file_ops, they cannot convert the void private_data pointer
> to a struct socket BTF pointer. In order to do this a new helper
> wrapping sock_from_file is added.
>
> This is useful to tracing programs but also other program types
> inheriting this set of helpers such as iterators or LSM programs.
>
> Signed-off-by: Florent Revest <revest@xxxxxxxxxx>

Acked-by: KP Singh <kpsingh@xxxxxxxxxx>

Some minor comments.

> ---
> include/uapi/linux/bpf.h | 7 +++++++
> kernel/trace/bpf_trace.c | 20 ++++++++++++++++++++
> scripts/bpf_helpers_doc.py | 4 ++++
> tools/include/uapi/linux/bpf.h | 7 +++++++
> 4 files changed, 38 insertions(+)
>
> diff --git a/include/uapi/linux/bpf.h b/include/uapi/linux/bpf.h
> index 162999b12790..7d598f161dc0 100644
> --- a/include/uapi/linux/bpf.h
> +++ b/include/uapi/linux/bpf.h
> @@ -3787,6 +3787,12 @@ union bpf_attr {
> * *ARG_PTR_TO_BTF_ID* of type *task_struct*.
> * Return
> * Pointer to the current task.
> + *
> + * struct socket *bpf_sock_from_file(struct file *file)
> + * Description
> + * If the given file contains a socket, returns the associated socket.

"If the given file is a socket" or "represents a socket" would fit better here.

> + * Return
> + * A pointer to a struct socket on success or NULL on failure.

NULL if the file is not a socket.