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

2020-11-19 Thread Martin KaFai Lau
On Thu, Nov 19, 2020 at 05:26:51PM +0100, Florent Revest wrote:
> From: Florent Revest 
> 
> While eBPF programs can check whether a file is a socket by file->f_op
> == _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.
Acked-by: Martin KaFai Lau 


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

2020-11-19 Thread KP Singh
On Thu, Nov 19, 2020 at 5:27 PM Florent Revest  wrote:
>
> From: Florent Revest 
>
> While eBPF programs can check whether a file is a socket by file->f_op
> == _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 

Acked-by: KP Singh 

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.