Skip to contentSkip to conversation

bpf: Store/dump license string for loaded program

PATCH Superseded Jiri Olsa 3 participants 8 messages Apr 23, 2018 – Apr 25, 2018

Subsystems (5)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Jiri Olsa

author reviewer
#0 lore raw

cover bpf: Store/dump license string for loaded program

hi,
sending the change to store and dump the license
info for loaded BPF programs. It's important for
us get the license info, when investigating on
screwed up machine.

Adding change to bpftool to dump the license via:

  # bpftool prog list
  1: kprobe  name func_begin  tag 59bef35a42fda602 license GPL
          loaded_at Apr 22/15:46  uid 0
          xlated 272B  not jited  memlock 4096B  map_ids 1
  # bpftool prog show --json
  [{"id":1,"type":"kprobe","name":"fun ... ,"license":"GPL", ... ]

Also available at:
https://git.kernel.org/pub/scm/linux/kernel/git/jolsa/perf.git
bpf/license

thanks,
jirka

---
Jiri Olsa (3):
bpf: Store license string for loaded program tools bpf: Sync bpf.h uapi header
tools bpftool: Display license in prog show/list

 include/linux/bpf.h            | 1 +
 include/uapi/linux/bpf.h       | 3 +++
 kernel/bpf/core.c              | 1 +
 kernel/bpf/syscall.c           | 7 ++++++-
 tools/bpf/bpftool/prog.c       | 4 ++++
 tools/include/uapi/linux/bpf.h | 4 ++++
 6 files changed, 19 insertions(+), 1 deletion(-)
author reviewer
#1 lore raw

PATCH 1/3 bpf: Store license string for loaded program

Storing the license string provided loaded program, so it can be provided back when dumping the loaded programs info (added in following change).

---
 include/linux/bpf.h      | 1 +
 include/uapi/linux/bpf.h | 3 +++
 kernel/bpf/core.c        | 1 +
 kernel/bpf/syscall.c     | 7 ++++++-
 4 files changed, 11 insertions(+), 1 deletion(-)
include/linux/bpf.h +1 -0
index ee5275e7d4df..8530b791c1b0 100644
@@ -274,6 +274,7 @@ struct bpf_prog_aux {
 	struct user_struct *user;
 	u64 load_time; /* ns since boottime */
 	char name[BPF_OBJ_NAME_LEN];
+	char *license;
 #ifdef CONFIG_SECURITY
 	void *security;
 #endif
include/uapi/linux/bpf.h +3 -0
index c8383a289f7b..b7298ee177e7 100644
@@ -239,6 +239,8 @@ enum bpf_attach_type {
 
 #define BPF_OBJ_NAME_LEN 16U
 
+#define BPF_OBJ_LICENSE_LEN 128U
+
 /* Flags for accessing BPF object */
 #define BPF_F_RDONLY		(1U << 3)
 #define BPF_F_WRONLY		(1U << 4)
@@ -1039,6 +1041,7 @@ struct bpf_prog_info {
 	__u32 ifindex;
 	__u64 netns_dev;
 	__u64 netns_ino;
+	char license[BPF_OBJ_LICENSE_LEN];
 } __attribute__((aligned(8)));
 
 struct bpf_map_info {
kernel/bpf/core.c +1 -0
index d315b393abdd..05352a928917 100644
@@ -142,6 +142,7 @@ struct bpf_prog *bpf_prog_realloc(struct bpf_prog *fp_old, unsigned int size,
 
 void __bpf_prog_free(struct bpf_prog *fp)
 {
+	kfree(fp->aux->license);
 	kfree(fp->aux);
 	vfree(fp);
 }
kernel/bpf/syscall.c +6 -1
index fe23dc5a3ec4..a876af93147f 100644
@@ -1265,7 +1265,7 @@ static int bpf_prog_load(union bpf_attr *attr)
 	enum bpf_prog_type type = attr->prog_type;
 	struct bpf_prog *prog;
 	int err;
-	char license[128];
+	char license[BPF_OBJ_LICENSE_LEN];
 	bool is_gpl;
 
 	if (CHECK_ATTR(BPF_PROG_LOAD))
@@ -1345,6 +1345,10 @@ static int bpf_prog_load(union bpf_attr *attr)
 	if (err)
 		goto free_prog;
 
+	prog->aux->license = kstrdup(license, GFP_KERNEL);
+	if (!prog->aux->license)
+		goto free_prog;
+
 	/* run eBPF verifier */
 	err = bpf_check(&prog, attr);
 	if (err < 0)
@@ -1917,6 +1921,7 @@ static int bpf_prog_get_info_by_fd(struct bpf_prog *prog,
 
 	memcpy(info.tag, prog->tag, sizeof(prog->tag));
 	memcpy(info.name, prog->aux->name, sizeof(prog->aux->name));
+	strncpy(info.license, prog->aux->license, BPF_OBJ_LICENSE_LEN);
 
 	ulen = info.nr_map_ids;
 	info.nr_map_ids = prog->aux->used_map_cnt;
Signature · 1 line
2.13.6
author reviewer
#2 lore raw

PATCH 2/3 tools bpf: Sync bpf.h uapi header

In reply to Jiri Olsa

Syncing the bpf.h uapi header with tools.

---
 tools/include/uapi/linux/bpf.h | 4 ++++
 1 file changed, 4 insertions(+)
tools/include/uapi/linux/bpf.h +4 -0
index 7f7fbb9d0253..b7298ee177e7 100644
@@ -239,6 +239,8 @@ enum bpf_attach_type {
 
 #define BPF_OBJ_NAME_LEN 16U
 
+#define BPF_OBJ_LICENSE_LEN 128U
+
 /* Flags for accessing BPF object */
 #define BPF_F_RDONLY		(1U << 3)
 #define BPF_F_WRONLY		(1U << 4)
@@ -884,6 +886,7 @@ enum bpf_func_id {
 /* BPF_FUNC_skb_set_tunnel_key flags. */
 #define BPF_F_ZERO_CSUM_TX		(1ULL << 1)
 #define BPF_F_DONT_FRAGMENT		(1ULL << 2)
+#define BPF_F_SEQ_NUMBER		(1ULL << 3)
 
 /* BPF_FUNC_perf_event_output, BPF_FUNC_perf_event_read and
  * BPF_FUNC_perf_event_read_value flags.
@@ -1038,6 +1041,7 @@ struct bpf_prog_info {
 	__u32 ifindex;
 	__u64 netns_dev;
 	__u64 netns_ino;
+	char license[BPF_OBJ_LICENSE_LEN];
 } __attribute__((aligned(8)));
 
 struct bpf_map_info {
Signature · 1 line
2.13.6
author reviewer
#3 lore raw

PATCH 3/3 tools bpftool: Display license in prog show/list

In reply to Jiri Olsa

Display the license string in bpftool prog command, like:

  # bpftool prog list
  1: kprobe  name func_begin  tag 59bef35a42fda602 license GPL
          loaded_at Apr 22/15:46  uid 0
          xlated 272B  not jited  memlock 4096B  map_ids 1
  # bpftool prog show --json
  [{"id":1,"type":"kprobe","name":"fun ... ,"license":"GPL", ... ]
---
 tools/bpf/bpftool/prog.c | 4 ++++
 1 file changed, 4 insertions(+)
tools/bpf/bpftool/prog.c +4 -0
index 548adb9b7317..ea5ede899cf7 100644
@@ -235,6 +235,8 @@ static void print_prog_json(struct bpf_prog_info *info, int fd)
 		     info->tag[0], info->tag[1], info->tag[2], info->tag[3],
 		     info->tag[4], info->tag[5], info->tag[6], info->tag[7]);
 
+	jsonw_string_field(json_wtr, "license", info->license);
+
 	print_dev_json(info->ifindex, info->netns_dev, info->netns_ino);
 
 	if (info->load_time) {
@@ -295,8 +297,10 @@ static void print_prog_plain(struct bpf_prog_info *info, int fd)
 	printf("tag ");
 	fprint_hex(stdout, info->tag, BPF_TAG_SIZE, "");
 	print_dev_plain(info->ifindex, info->netns_dev, info->netns_ino);
+	printf(" license %s", info->license);
 	printf("\n");
 
+
 	if (info->load_time) {
 		char buf[32];
 
Signature · 1 line
2.13.6
#4 lore raw
In reply to Jiri Olsa
Quoted 5 lines · Jiri Olsa

hi,
sending the change to store and dump the license
info for loaded BPF programs. It's important for
us get the license info, when investigating on
screwed up machine.

hmm. boolean flag whether bpf prog is gpl or not is already exposed via bpf_prog_info.
I see no point of wasting extra 128 bytes of kernel memory.

#5 lore raw
Quoted 9 lines · Alexei Starovoitov
Quoted 5 lines · Jiri Olsa

hi,
sending the change to store and dump the license
info for loaded BPF programs. It's important for
us get the license info, when investigating on
screwed up machine.

hmm. boolean flag whether bpf prog is gpl or not
is already exposed via bpf_prog_info.

hum, I can't see that (on bpf-next/master)
would the attached change be ok with you?

jirka

---

include/uapi/linux/bpf.h +1 -0
index e6679393b687..2ce9c9d41c2b 100644
@@ -1062,6 +1062,7 @@ struct bpf_prog_info {
 	__u32 ifindex;
 	__u64 netns_dev;
 	__u64 netns_ino;
+	__u16 gpl_compatible:1;
 } __attribute__((aligned(8)));
 
 struct bpf_map_info {
kernel/bpf/syscall.c +1 -0
index fe23dc5a3ec4..7bb4ff1c770a 100644
@@ -1914,6 +1914,7 @@ static int bpf_prog_get_info_by_fd(struct bpf_prog *prog,
 	info.load_time = prog->aux->load_time;
 	info.created_by_uid = from_kuid_munged(current_user_ns(),
 					       prog->aux->user->uid);
+	info.gpl_compatible = prog->gpl_compatible;
 
 	memcpy(info.tag, prog->tag, sizeof(prog->tag));
 	memcpy(info.name, prog->aux->name, sizeof(prog->aux->name));
#6 lore raw
Quoted 23 lines · Jiri Olsa
Quoted 9 lines · Alexei Starovoitov
Quoted 5 lines · Jiri Olsa

hi,
sending the change to store and dump the license
info for loaded BPF programs. It's important for
us get the license info, when investigating on
screwed up machine.

hmm. boolean flag whether bpf prog is gpl or not
is already exposed via bpf_prog_info.

hum, I can't see that (on bpf-next/master)
would the attached change be ok with you?

jirka

---

include/uapi/linux/bpf.h +0 -0
index e6679393b687..2ce9c9d41c2b 100644

include/uapi/linux/bpf.h struct bpf_prog_info { · line 1066

Quoted 5 lines
include/uapi/linux/bpf.h +1 -0
@@ -1062,6 +1062,7 @@ struct bpf_prog_info {
 	__u32 ifindex;
 	__u64 netns_dev;
 	__u64 netns_ino;
+	__u16 gpl_compatible:1;
 } __attribute__((aligned(8)));

ahh. I swear there were patches to add it and I thought we accepted them. Also just noticed that commit 675fc275a3a2d added 4-byte hole in there. So I'm thinking we can fill the hole with
__u32 ifindex;
+ __u32 gpl_compatible:1;
__u64 netns_dev;
__u64 netns_ino;

and keep adding bit fields in there without breaking user space. Such patch would need to go to bpf tree.

#7 lore raw
Quoted 38 lines · Alexei Starovoitov
Quoted 23 lines · Jiri Olsa
Quoted 9 lines · Alexei Starovoitov
Quoted 5 lines · Jiri Olsa

hi,
sending the change to store and dump the license
info for loaded BPF programs. It's important for
us get the license info, when investigating on
screwed up machine.

hmm. boolean flag whether bpf prog is gpl or not
is already exposed via bpf_prog_info.

hum, I can't see that (on bpf-next/master)
would the attached change be ok with you?

jirka

---

include/uapi/linux/bpf.h +0 -0
index e6679393b687..2ce9c9d41c2b 100644

include/uapi/linux/bpf.h struct bpf_prog_info { · line 1066

Quoted 5 lines
include/uapi/linux/bpf.h +1 -0
@@ -1062,6 +1062,7 @@ struct bpf_prog_info {
 	__u32 ifindex;
 	__u64 netns_dev;
 	__u64 netns_ino;
+	__u16 gpl_compatible:1;
 } __attribute__((aligned(8)));

ahh. I swear there were patches to add it and I thought we accepted them. Also just noticed that commit 675fc275a3a2d added 4-byte hole in there. So I'm thinking we can fill the hole with

include/uapi/linux/bpf.h +1 -0
 	__u32 ifindex;
+	__u32 gpl_compatible:1;
 	__u64 netns_dev;
 	__u64 netns_ino;
Quoted 3 lines

and keep adding bit fields in there without breaking user space. Such patch would need to go to bpf tree.

ok, will post v2 with that

thanks,
jirka