diff mbox series

[bpf-next,1/2] bpf: include sub program tags in bpf_prog_info

Message ID 20181210190644.2456253-1-songliubraving@fb.com
State Changes Requested, archived
Delegated to: BPF Maintainers
Headers show
Series [bpf-next,1/2] bpf: include sub program tags in bpf_prog_info | expand

Commit Message

Song Liu Dec. 10, 2018, 7:06 p.m. UTC
This patch adds nr_prog_tags and prog_tags to bpf_prog_info. This is a
reliable way for user space to get tags of all sub programs. Before this
patch, user space need to find sub program tags via kallsyms.

This feature will be used in BPF introspection, where user space queries
information about BPF programs via sys_bpf.

Signed-off-by: Song Liu <songliubraving@fb.com>
---
 include/uapi/linux/bpf.h |  2 ++
 kernel/bpf/syscall.c     | 27 +++++++++++++++++++++++++++
 2 files changed, 29 insertions(+)

Comments

Martin KaFai Lau Dec. 10, 2018, 9:43 p.m. UTC | #1
On Mon, Dec 10, 2018 at 11:06:43AM -0800, Song Liu wrote:
> This patch adds nr_prog_tags and prog_tags to bpf_prog_info. This is a
> reliable way for user space to get tags of all sub programs. Before this
> patch, user space need to find sub program tags via kallsyms.
> 
> This feature will be used in BPF introspection, where user space queries
> information about BPF programs via sys_bpf.
> 
> Signed-off-by: Song Liu <songliubraving@fb.com>
> ---
>  include/uapi/linux/bpf.h |  2 ++
>  kernel/bpf/syscall.c     | 27 +++++++++++++++++++++++++++
>  2 files changed, 29 insertions(+)
> 
> diff --git a/include/uapi/linux/bpf.h b/include/uapi/linux/bpf.h
> index 1bee1135866a..368d185aa32f 100644
> --- a/include/uapi/linux/bpf.h
> +++ b/include/uapi/linux/bpf.h
> @@ -2703,6 +2703,8 @@ struct bpf_prog_info {
>  	__u32 jited_line_info_cnt;
>  	__u32 line_info_rec_size;
>  	__u32 jited_line_info_rec_size;
> +	__u32 nr_prog_tags;
> +	__aligned_u64 prog_tags;
>  } __attribute__((aligned(8)));
>  
>  struct bpf_map_info {
> diff --git a/kernel/bpf/syscall.c b/kernel/bpf/syscall.c
> index 19c88cff7880..df7b1822d585 100644
> --- a/kernel/bpf/syscall.c
> +++ b/kernel/bpf/syscall.c
> @@ -2322,6 +2322,33 @@ static int bpf_prog_get_info_by_fd(struct bpf_prog *prog,
>  		}
>  	}
>  
> +	ulen = info.nr_prog_tags;
> +	info.nr_prog_tags = prog->aux->func_cnt ? : 1;
> +	if (ulen) {
> +		if (bpf_dump_raw_ok()) {
> +			__u8 __user (*user_prog_tags)[BPF_TAG_SIZE];
> +			u32 i;
> +
> +			user_prog_tags = u64_to_user_ptr(info.prog_tags);
> +			ulen = min_t(u32, info.nr_prog_tags, ulen);
> +			if (prog->aux->func_cnt) {
> +				for (i = 0; i < ulen; i++) {
> +					if (copy_to_user(
> +						    user_prog_tags[i],
> +						    prog->aux->func[i]->tag,
> +						    BPF_TAG_SIZE))
> +						return -EFAULT;
> +				}
> +			} else {
> +				if (copy_to_user(user_prog_tags[0],
> +						 prog->tag, BPF_TAG_SIZE))
> +					return -EFAULT;
> +			}
> +		} else {
> +			info.nr_prog_tags = 0;
should be
	info.prog_tags = 0;

> +		}
> +	}
> +
>  done:
>  	if (copy_to_user(uinfo, &info, info_len) ||
>  	    put_user(info_len, &uattr->info.info_len))
> -- 
> 2.17.1
>
diff mbox series

Patch

diff --git a/include/uapi/linux/bpf.h b/include/uapi/linux/bpf.h
index 1bee1135866a..368d185aa32f 100644
--- a/include/uapi/linux/bpf.h
+++ b/include/uapi/linux/bpf.h
@@ -2703,6 +2703,8 @@  struct bpf_prog_info {
 	__u32 jited_line_info_cnt;
 	__u32 line_info_rec_size;
 	__u32 jited_line_info_rec_size;
+	__u32 nr_prog_tags;
+	__aligned_u64 prog_tags;
 } __attribute__((aligned(8)));
 
 struct bpf_map_info {
diff --git a/kernel/bpf/syscall.c b/kernel/bpf/syscall.c
index 19c88cff7880..df7b1822d585 100644
--- a/kernel/bpf/syscall.c
+++ b/kernel/bpf/syscall.c
@@ -2322,6 +2322,33 @@  static int bpf_prog_get_info_by_fd(struct bpf_prog *prog,
 		}
 	}
 
+	ulen = info.nr_prog_tags;
+	info.nr_prog_tags = prog->aux->func_cnt ? : 1;
+	if (ulen) {
+		if (bpf_dump_raw_ok()) {
+			__u8 __user (*user_prog_tags)[BPF_TAG_SIZE];
+			u32 i;
+
+			user_prog_tags = u64_to_user_ptr(info.prog_tags);
+			ulen = min_t(u32, info.nr_prog_tags, ulen);
+			if (prog->aux->func_cnt) {
+				for (i = 0; i < ulen; i++) {
+					if (copy_to_user(
+						    user_prog_tags[i],
+						    prog->aux->func[i]->tag,
+						    BPF_TAG_SIZE))
+						return -EFAULT;
+				}
+			} else {
+				if (copy_to_user(user_prog_tags[0],
+						 prog->tag, BPF_TAG_SIZE))
+					return -EFAULT;
+			}
+		} else {
+			info.nr_prog_tags = 0;
+		}
+	}
+
 done:
 	if (copy_to_user(uinfo, &info, info_len) ||
 	    put_user(info_len, &uattr->info.info_len))