From: Yonghong Song <yhs@fb.com>
To: Andrii Nakryiko <andriin@fb.com>, <bpf@vger.kernel.org>,
Martin KaFai Lau <kafai@fb.com>, <netdev@vger.kernel.org>
Cc: Alexei Starovoitov <ast@fb.com>,
Daniel Borkmann <daniel@iogearbox.net>, <kernel-team@fb.com>
Subject: [RFC PATCH bpf-next v2 12/17] bpf: implement query for target_proto and file dumper prog_id
Date: Wed, 15 Apr 2020 12:27:54 -0700 [thread overview]
Message-ID: <20200415192754.4083756-1-yhs@fb.com> (raw)
In-Reply-To: <20200415192740.4082659-1-yhs@fb.com>
Given a fd representing a bpfdump target, user
can retrieve the target_proto name which represents
the bpf program prototype.
Given a fd representing a file dumper, user can
retrieve the bpf_prog id associated with that dumper.
Signed-off-by: Yonghong Song <yhs@fb.com>
---
include/linux/bpf.h | 2 +
include/uapi/linux/bpf.h | 11 +++++-
kernel/bpf/dump.c | 72 ++++++++++++++++++++++++++++++++++
kernel/bpf/syscall.c | 2 +-
tools/include/uapi/linux/bpf.h | 11 +++++-
5 files changed, 95 insertions(+), 3 deletions(-)
diff --git a/include/linux/bpf.h b/include/linux/bpf.h
index 401e5bf921a2..a1ae8509e735 100644
--- a/include/linux/bpf.h
+++ b/include/linux/bpf.h
@@ -1138,6 +1138,8 @@ int bpf_prog_dump_create(struct bpf_prog *prog);
struct bpf_prog *bpf_dump_get_prog(struct seq_file *seq, u32 priv_data_size,
u64 *session_id, u64 *seq_num, bool is_last);
int bpf_dump_run_prog(struct bpf_prog *prog, void *ctx);
+int bpf_dump_query(const union bpf_attr *attr, union bpf_attr __user *uattr);
+
int bpf_percpu_hash_copy(struct bpf_map *map, void *key, void *value);
int bpf_percpu_array_copy(struct bpf_map *map, void *key, void *value);
int bpf_percpu_hash_update(struct bpf_map *map, void *key, void *value,
diff --git a/include/uapi/linux/bpf.h b/include/uapi/linux/bpf.h
index 75f3657d526c..856e3f8a63b8 100644
--- a/include/uapi/linux/bpf.h
+++ b/include/uapi/linux/bpf.h
@@ -533,7 +533,10 @@ union bpf_attr {
};
struct { /* anonymous struct used by BPF_OBJ_GET_INFO_BY_FD */
- __u32 bpf_fd;
+ union {
+ __u32 bpf_fd;
+ __u32 dump_query_fd;
+ };
__u32 info_len;
__aligned_u64 info;
} info;
@@ -3618,6 +3621,12 @@ struct bpf_btf_info {
__u32 id;
} __attribute__((aligned(8)));
+struct bpf_dump_info {
+ __aligned_u64 prog_ctx_type_name;
+ __u32 type_name_buf_len;
+ __u32 prog_id;
+} __attribute__((aligned(8)));
+
/* User bpf_sock_addr struct to access socket fields and sockaddr struct passed
* by user and intended to be used by socket (e.g. to bind to, depends on
* attach attach type).
diff --git a/kernel/bpf/dump.c b/kernel/bpf/dump.c
index 789b35772a81..643591bf5aea 100644
--- a/kernel/bpf/dump.c
+++ b/kernel/bpf/dump.c
@@ -93,6 +93,78 @@ static void *get_extra_priv_dptr(void *old_ptr, u32 old_size)
return old_ptr + roundup(old_size, 8);
}
+int bpf_dump_query(const union bpf_attr *attr, union bpf_attr __user *uattr)
+{
+ struct bpf_dump_info __user *ubpf_dinfo;
+ struct bpfdump_target_info *tinfo;
+ struct dumper_inode_info *i_info;
+ struct bpf_dump_info bpf_dinfo;
+ const char *prog_ctx_type_name;
+ void * __user tname_buf;
+ u32 tname_len, info_len;
+ struct file *filp;
+ struct fd qfd;
+ int err = 0;
+
+ qfd = fdget(attr->info.dump_query_fd);
+ filp = qfd.file;
+ if (!filp)
+ return -EBADF;
+
+ if (filp->f_op != &bpf_dumper_ops &&
+ filp->f_inode->i_op != &bpfdump_dir_iops) {
+ err = -EINVAL;
+ goto done;
+ }
+
+ info_len = attr->info.info_len;
+ ubpf_dinfo = u64_to_user_ptr(attr->info.info);
+ err = bpf_check_uarg_tail_zero(ubpf_dinfo, sizeof(bpf_dinfo),
+ info_len);
+ if (err)
+ goto done;
+ info_len = min_t(u32, sizeof(bpf_dinfo), info_len);
+
+ memset(&bpf_dinfo, 0, sizeof(bpf_dinfo));
+ if (copy_from_user(&bpf_dinfo, ubpf_dinfo, info_len)) {
+ err = -EFAULT;
+ goto done;
+ }
+
+ /* copy prog_id for dumpers */
+ if (filp->f_op == &bpf_dumper_ops) {
+ i_info = filp->f_inode->i_private;
+ bpf_dinfo.prog_id = i_info->prog->aux->id;
+ tinfo = i_info->tinfo;
+ } else {
+ tinfo = filp->f_inode->i_private;
+ }
+
+ prog_ctx_type_name = tinfo->prog_ctx_type_name;
+
+ tname_len = strlen(prog_ctx_type_name) + 1;
+ if (bpf_dinfo.type_name_buf_len < tname_len) {
+ err = -ENOSPC;
+ goto done;
+ }
+
+ /* copy prog_ctx_type_name */
+ tname_buf = u64_to_user_ptr(bpf_dinfo.prog_ctx_type_name);
+ if (copy_to_user(tname_buf, prog_ctx_type_name, tname_len)) {
+ err = -EFAULT;
+ goto done;
+ }
+
+ /* copy potentially updated bpf_dinfo and info_len */
+ if (copy_to_user(ubpf_dinfo, &bpf_dinfo, info_len) ||
+ put_user(info_len, &uattr->info.info_len))
+ return -EFAULT;
+
+done:
+ fdput(qfd);
+ return err;
+}
+
#ifdef CONFIG_PROC_FS
static void dumper_show_fdinfo(struct seq_file *m, struct file *filp)
{
diff --git a/kernel/bpf/syscall.c b/kernel/bpf/syscall.c
index e6a4514435c4..1cde78e53a17 100644
--- a/kernel/bpf/syscall.c
+++ b/kernel/bpf/syscall.c
@@ -3358,7 +3358,7 @@ static int bpf_obj_get_info_by_fd(const union bpf_attr *attr,
else if (f.file->f_op == &btf_fops)
err = bpf_btf_get_info_by_fd(f.file->private_data, attr, uattr);
else
- err = -EINVAL;
+ err = bpf_dump_query(attr, uattr);
fdput(f);
return err;
diff --git a/tools/include/uapi/linux/bpf.h b/tools/include/uapi/linux/bpf.h
index 75f3657d526c..856e3f8a63b8 100644
--- a/tools/include/uapi/linux/bpf.h
+++ b/tools/include/uapi/linux/bpf.h
@@ -533,7 +533,10 @@ union bpf_attr {
};
struct { /* anonymous struct used by BPF_OBJ_GET_INFO_BY_FD */
- __u32 bpf_fd;
+ union {
+ __u32 bpf_fd;
+ __u32 dump_query_fd;
+ };
__u32 info_len;
__aligned_u64 info;
} info;
@@ -3618,6 +3621,12 @@ struct bpf_btf_info {
__u32 id;
} __attribute__((aligned(8)));
+struct bpf_dump_info {
+ __aligned_u64 prog_ctx_type_name;
+ __u32 type_name_buf_len;
+ __u32 prog_id;
+} __attribute__((aligned(8)));
+
/* User bpf_sock_addr struct to access socket fields and sockaddr struct passed
* by user and intended to be used by socket (e.g. to bind to, depends on
* attach attach type).
--
2.24.1
next prev parent reply other threads:[~2020-04-15 19:32 UTC|newest]
Thread overview: 24+ messages / expand[flat|nested] mbox.gz Atom feed top
2020-04-15 19:27 [RFC PATCH bpf-next v2 00/17] bpf: implement bpf based dumping of kernel data structures Yonghong Song
2020-04-15 19:27 ` [RFC PATCH bpf-next v2 01/17] net: refactor net assignment for seq_net_private structure Yonghong Song
2020-04-15 19:27 ` [RFC PATCH bpf-next v2 02/17] bpf: create /sys/kernel/bpfdump mount file system Yonghong Song
2020-04-15 19:27 ` [RFC PATCH bpf-next v2 03/17] bpf: provide a way for targets to register themselves Yonghong Song
2020-04-15 19:27 ` [RFC PATCH bpf-next v2 04/17] bpf: allow loading of a dumper program Yonghong Song
2020-04-15 19:27 ` [RFC PATCH bpf-next v2 05/17] bpf: create file or anonymous dumpers Yonghong Song
2020-04-15 19:27 ` [RFC PATCH bpf-next v2 06/17] bpf: add PTR_TO_BTF_ID_OR_NULL support Yonghong Song
2020-04-15 19:27 ` [RFC PATCH bpf-next v2 07/17] bpf: add netlink and ipv6_route targets Yonghong Song
2020-04-15 19:27 ` [RFC PATCH bpf-next v2 08/17] bpf: add bpf_map target Yonghong Song
2020-04-15 19:27 ` [RFC PATCH bpf-next v2 09/17] bpf: add task and task/file targets Yonghong Song
2020-04-15 19:27 ` [RFC PATCH bpf-next v2 10/17] bpf: add bpf_seq_printf and bpf_seq_write helpers Yonghong Song
2020-04-15 19:27 ` [RFC PATCH bpf-next v2 11/17] bpf: support variable length array in tracing programs Yonghong Song
2020-04-15 19:27 ` Yonghong Song [this message]
2020-04-15 19:27 ` [RFC PATCH bpf-next v2 13/17] tools/libbpf: libbpf support for bpfdump Yonghong Song
2020-04-15 19:27 ` [RFC PATCH bpf-next v2 14/17] tools/bpftool: add bpf dumper support Yonghong Song
2020-04-15 19:27 ` [RFC PATCH bpf-next v2 15/17] tools/bpf: selftests: add dumper programs for ipv6_route and netlink Yonghong Song
2020-04-15 19:27 ` [RFC PATCH bpf-next v2 16/17] tools/bpf: selftests: add dumper progs for bpf_map/task/task_file Yonghong Song
2020-04-15 19:28 ` [RFC PATCH bpf-next v2 17/17] tools/bpf: selftests: add a selftest for anonymous dumper Yonghong Song
2020-04-16 2:23 ` [RFC PATCH bpf-next v2 00/17] bpf: implement bpf based dumping of kernel data structures David Ahern
2020-04-16 6:41 ` Yonghong Song
2020-04-17 15:02 ` Alan Maguire
2020-04-19 5:34 ` Yonghong Song
2020-04-17 10:54 ` Alan Maguire
2020-04-19 5:30 ` Yonghong Song
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=20200415192754.4083756-1-yhs@fb.com \
--to=yhs@fb.com \
--cc=andriin@fb.com \
--cc=ast@fb.com \
--cc=bpf@vger.kernel.org \
--cc=daniel@iogearbox.net \
--cc=kafai@fb.com \
--cc=kernel-team@fb.com \
--cc=netdev@vger.kernel.org \
/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