Skip to content

Commit b3eece0

Browse files
author
Alexei Starovoitov
committed
Merge branch 'bpftool-show-pid'
Andrii Nakryiko says: ==================== This patch set implements libbpf support for a second kind of special externs, kernel symbols, in addition to existing Kconfig externs. Right now, only untyped (const void) externs are supported, which, in C language, allow only to take their address. In the future, with kernel BTF getting type info about its own global and per-cpu variables, libbpf will extend this support with BTF type info, which will allow to also directly access variable's contents and follow its internal pointers, similarly to how it's possible today in fentry/fexit programs. As a first practical use of this functionality, bpftool gained ability to show PIDs of processes that have open file descriptors for BPF map/program/link/BTF object. It relies on iter/task_file BPF iterator program to extract this information efficiently. There was a bunch of bpftool refactoring (especially Makefile) necessary to generalize bpftool's internal BPF program use. This includes generalization of BPF skeletons support, addition of a vmlinux.h generation, extracting and building minimal subset of bpftool for bootstrapping. v2->v3: - fix sec_btf_id check (Hao); v1->v2: - docs fixes (Quentin); - dual GPL/BSD license for pid_inter.bpf.c (Quentin); - NULL-init kcfg_data (Hao Luo); rfc->v1: - show pids, if supported by kernel, always (Alexei); - switched iter output to binary to support showing process names; - update man pages; - fix few minor bugs in libbpf w.r.t. extern iteration. ==================== Signed-off-by: Alexei Starovoitov <ast@kernel.org>
2 parents 1bdb6c9 + 075c776 commit b3eece0

27 files changed

Lines changed: 1252 additions & 547 deletions

‎tools/bpf/bpftool/.gitignore‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
# SPDX-License-Identifier: GPL-2.0-only
22
*.d
3-
/_bpftool
3+
/bpftool-bootstrap
44
/bpftool
55
bpftool*.8
66
bpf-helpers.*
77
FEATURE-DUMP.bpftool
88
feature
99
libbpf
10-
profiler.skel.h
10+
/*.skel.h
11+
/vmlinux.h

‎tools/bpf/bpftool/Documentation/bpftool-btf.rst‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,11 @@ DESCRIPTION
3636
otherwise list all BTF objects currently loaded on the
3737
system.
3838

39+
Since Linux 5.8 bpftool is able to discover information about
40+
processes that hold open file descriptors (FDs) against BTF
41+
objects. On such kernels bpftool will automatically emit this
42+
information as well.
43+
3944
**bpftool btf dump** *BTF_SRC*
4045
Dump BTF entries from a given *BTF_SRC*.
4146

‎tools/bpf/bpftool/Documentation/bpftool-link.rst‎

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,11 @@ DESCRIPTION
3737
zero or more named attributes, some of which depend on type
3838
of link.
3939

40+
Since Linux 5.8 bpftool is able to discover information about
41+
processes that hold open file descriptors (FDs) against BPF
42+
links. On such kernels bpftool will automatically emit this
43+
information as well.
44+
4045
**bpftool link pin** *LINK* *FILE*
4146
Pin link *LINK* as *FILE*.
4247

@@ -82,6 +87,7 @@ EXAMPLES
8287

8388
10: cgroup prog 25
8489
cgroup_id 614 attach_type egress
90+
pids test_progs(223)
8591

8692
**# bpftool --json --pretty link show**
8793

@@ -91,7 +97,12 @@ EXAMPLES
9197
"type": "cgroup",
9298
"prog_id": 25,
9399
"cgroup_id": 614,
94-
"attach_type": "egress"
100+
"attach_type": "egress",
101+
"pids": [{
102+
"pid": 223,
103+
"comm": "test_progs"
104+
}
105+
]
95106
}
96107
]
97108

‎tools/bpf/bpftool/Documentation/bpftool-map.rst‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,11 @@ DESCRIPTION
6262
Output will start with map ID followed by map type and
6363
zero or more named attributes (depending on kernel version).
6464

65+
Since Linux 5.8 bpftool is able to discover information about
66+
processes that hold open file descriptors (FDs) against BPF
67+
maps. On such kernels bpftool will automatically emit this
68+
information as well.
69+
6570
**bpftool map create** *FILE* **type** *TYPE* **key** *KEY_SIZE* **value** *VALUE_SIZE* **entries** *MAX_ENTRIES* **name** *NAME* [**flags** *FLAGS*] [**dev** *NAME*]
6671
Create a new map with given parameters and pin it to *bpffs*
6772
as *FILE*.
@@ -180,7 +185,8 @@ EXAMPLES
180185
::
181186

182187
10: hash name some_map flags 0x0
183-
key 4B value 8B max_entries 2048 memlock 167936B
188+
key 4B value 8B max_entries 2048 memlock 167936B
189+
pids systemd(1)
184190

185191
The following three commands are equivalent:
186192

‎tools/bpf/bpftool/Documentation/bpftool-prog.rst‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,11 @@ DESCRIPTION
7575
program run. Activation or deactivation of the feature is
7676
performed via the **kernel.bpf_stats_enabled** sysctl knob.
7777

78+
Since Linux 5.8 bpftool is able to discover information about
79+
processes that hold open file descriptors (FDs) against BPF
80+
programs. On such kernels bpftool will automatically emit this
81+
information as well.
82+
7883
**bpftool prog dump xlated** *PROG* [{ **file** *FILE* | **opcodes** | **visual** | **linum** }]
7984
Dump eBPF instructions of the programs from the kernel. By
8085
default, eBPF will be disassembled and printed to standard
@@ -243,6 +248,7 @@ EXAMPLES
243248
10: xdp name some_prog tag 005a3d2123620c8b gpl run_time_ns 81632 run_cnt 10
244249
loaded_at 2017-09-29T20:11:00+0000 uid 0
245250
xlated 528B jited 370B memlock 4096B map_ids 10
251+
pids systemd(1)
246252

247253
**# bpftool --json --pretty prog show**
248254

@@ -262,6 +268,11 @@ EXAMPLES
262268
"bytes_jited": 370,
263269
"bytes_memlock": 4096,
264270
"map_ids": [10
271+
],
272+
"pids": [{
273+
"pid": 1,
274+
"comm": "systemd"
275+
}
265276
]
266277
}
267278
]

‎tools/bpf/bpftool/Makefile‎

Lines changed: 39 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ CFLAGS += -O2
4242
CFLAGS += -W -Wall -Wextra -Wno-unused-parameter -Wno-missing-field-initializers
4343
CFLAGS += $(filter-out -Wswitch-enum,$(EXTRA_WARNINGS))
4444
CFLAGS += -DPACKAGE='"bpftool"' -D__EXPORTED_HEADERS__ \
45+
-I$(if $(OUTPUT),$(OUTPUT),.) \
4546
-I$(srctree)/kernel/bpf/ \
4647
-I$(srctree)/tools/include \
4748
-I$(srctree)/tools/include/uapi \
@@ -61,9 +62,9 @@ CLANG ?= clang
6162

6263
FEATURE_USER = .bpftool
6364
FEATURE_TESTS = libbfd disassembler-four-args reallocarray zlib libcap \
64-
clang-bpf-global-var
65+
clang-bpf-co-re
6566
FEATURE_DISPLAY = libbfd disassembler-four-args zlib libcap \
66-
clang-bpf-global-var
67+
clang-bpf-co-re
6768

6869
check_feat := 1
6970
NON_CHECK_FEAT_TARGETS := clean uninstall doc doc-clean doc-install doc-uninstall
@@ -116,48 +117,64 @@ CFLAGS += -DHAVE_LIBBFD_SUPPORT
116117
SRCS += $(BFD_SRCS)
117118
endif
118119

120+
BPFTOOL_BOOTSTRAP := $(if $(OUTPUT),$(OUTPUT)bpftool-bootstrap,./bpftool-bootstrap)
121+
122+
BOOTSTRAP_OBJS = $(addprefix $(OUTPUT),main.o common.o json_writer.o gen.o btf.o)
119123
OBJS = $(patsubst %.c,$(OUTPUT)%.o,$(SRCS)) $(OUTPUT)disasm.o
120-
_OBJS = $(filter-out $(OUTPUT)prog.o,$(OBJS)) $(OUTPUT)_prog.o
121124

122-
ifeq ($(feature-clang-bpf-global-var),1)
123-
__OBJS = $(OBJS)
124-
else
125-
__OBJS = $(_OBJS)
126-
endif
125+
VMLINUX_BTF_PATHS := $(if $(O),$(O)/vmlinux) \
126+
$(if $(KBUILD_OUTPUT),$(KBUILD_OUTPUT)/vmlinux) \
127+
../../../vmlinux \
128+
/sys/kernel/btf/vmlinux \
129+
/boot/vmlinux-$(shell uname -r)
130+
VMLINUX_BTF := $(abspath $(firstword $(wildcard $(VMLINUX_BTF_PATHS))))
127131

128-
$(OUTPUT)_prog.o: prog.c
129-
$(QUIET_CC)$(CC) $(CFLAGS) -c -MMD -DBPFTOOL_WITHOUT_SKELETONS -o $@ $<
132+
ifneq ($(VMLINUX_BTF),)
133+
ifeq ($(feature-clang-bpf-co-re),1)
130134

131-
$(OUTPUT)_bpftool: $(_OBJS) $(LIBBPF)
132-
$(QUIET_LINK)$(CC) $(CFLAGS) $(LDFLAGS) -o $@ $(_OBJS) $(LIBS)
135+
BUILD_BPF_SKELS := 1
133136

134-
skeleton/profiler.bpf.o: skeleton/profiler.bpf.c $(LIBBPF)
137+
$(OUTPUT)vmlinux.h: $(VMLINUX_BTF) $(BPFTOOL_BOOTSTRAP)
138+
$(QUIET_GEN)$(BPFTOOL_BOOTSTRAP) btf dump file $< format c > $@
139+
140+
$(OUTPUT)%.bpf.o: skeleton/%.bpf.c $(OUTPUT)vmlinux.h $(LIBBPF)
135141
$(QUIET_CLANG)$(CLANG) \
142+
-I$(if $(OUTPUT),$(OUTPUT),.) \
136143
-I$(srctree)/tools/include/uapi/ \
137-
-I$(LIBBPF_PATH) -I$(srctree)/tools/lib \
144+
-I$(LIBBPF_PATH) \
145+
-I$(srctree)/tools/lib \
138146
-g -O2 -target bpf -c $< -o $@
139147

140-
profiler.skel.h: $(OUTPUT)_bpftool skeleton/profiler.bpf.o
141-
$(QUIET_GEN)$(OUTPUT)./_bpftool gen skeleton skeleton/profiler.bpf.o > $@
148+
$(OUTPUT)%.skel.h: $(OUTPUT)%.bpf.o $(BPFTOOL_BOOTSTRAP)
149+
$(QUIET_GEN)$(BPFTOOL_BOOTSTRAP) gen skeleton $< > $@
142150

143-
$(OUTPUT)prog.o: prog.c profiler.skel.h
144-
$(QUIET_CC)$(CC) $(CFLAGS) -c -MMD -o $@ $<
151+
$(OUTPUT)prog.o: $(OUTPUT)profiler.skel.h
152+
153+
$(OUTPUT)pids.o: $(OUTPUT)pid_iter.skel.h
154+
155+
endif
156+
endif
157+
158+
CFLAGS += $(if BUILD_BPF_SKELS,,-DBPFTOOL_WITHOUT_SKELETONS)
145159

146160
$(OUTPUT)disasm.o: $(srctree)/kernel/bpf/disasm.c
147161
$(QUIET_CC)$(CC) $(CFLAGS) -c -MMD -o $@ $<
148162

149163
$(OUTPUT)feature.o: | zdep
150164

151-
$(OUTPUT)bpftool: $(__OBJS) $(LIBBPF)
152-
$(QUIET_LINK)$(CC) $(CFLAGS) $(LDFLAGS) -o $@ $(__OBJS) $(LIBS)
165+
$(BPFTOOL_BOOTSTRAP): $(BOOTSTRAP_OBJS) $(LIBBPF)
166+
$(QUIET_LINK)$(CC) $(CFLAGS) $(LDFLAGS) -o $@ $(BOOTSTRAP_OBJS) $(LIBS)
167+
168+
$(OUTPUT)bpftool: $(OBJS) $(LIBBPF)
169+
$(QUIET_LINK)$(CC) $(CFLAGS) $(LDFLAGS) -o $@ $(OBJS) $(LIBS)
153170

154171
$(OUTPUT)%.o: %.c
155172
$(QUIET_CC)$(CC) $(CFLAGS) -c -MMD -o $@ $<
156173

157174
clean: $(LIBBPF)-clean
158175
$(call QUIET_CLEAN, bpftool)
159176
$(Q)$(RM) -- $(OUTPUT)bpftool $(OUTPUT)*.o $(OUTPUT)*.d
160-
$(Q)$(RM) -- $(OUTPUT)_bpftool profiler.skel.h skeleton/profiler.bpf.o
177+
$(Q)$(RM) -- $(BPFTOOL_BOOTSTRAP) $(OUTPUT)*.skel.h $(OUTPUT)vmlinux.h
161178
$(Q)$(RM) -r -- $(OUTPUT)libbpf/
162179
$(call QUIET_CLEAN, core-gen)
163180
$(Q)$(RM) -- $(OUTPUT)FEATURE-DUMP.bpftool
@@ -192,6 +209,7 @@ FORCE:
192209
zdep:
193210
@if [ "$(feature-zlib)" != "1" ]; then echo "No zlib found"; exit 1 ; fi
194211

212+
.SECONDARY:
195213
.PHONY: all FORCE clean install uninstall zdep
196214
.PHONY: doc doc-clean doc-install doc-uninstall
197215
.DEFAULT_GOAL := all

‎tools/bpf/bpftool/btf.c‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -809,6 +809,7 @@ show_btf_plain(struct bpf_btf_info *info, int fd,
809809
printf("%s%u", n++ == 0 ? " map_ids " : ",",
810810
obj->obj_id);
811811
}
812+
emit_obj_refs_plain(&refs_table, info->id, "\n\tpids ");
812813

813814
printf("\n");
814815
}
@@ -841,6 +842,9 @@ show_btf_json(struct bpf_btf_info *info, int fd,
841842
jsonw_uint(json_wtr, obj->obj_id);
842843
}
843844
jsonw_end_array(json_wtr); /* map_ids */
845+
846+
emit_obj_refs_json(&refs_table, info->id, json_wtr); /* pids */
847+
844848
jsonw_end_object(json_wtr); /* btf object */
845849
}
846850

@@ -893,6 +897,7 @@ static int do_show(int argc, char **argv)
893897
close(fd);
894898
return err;
895899
}
900+
build_obj_refs_table(&refs_table, BPF_OBJ_BTF);
896901

897902
if (fd >= 0) {
898903
err = show_btf(fd, &btf_prog_table, &btf_map_table);
@@ -939,6 +944,7 @@ static int do_show(int argc, char **argv)
939944
exit_free:
940945
delete_btf_table(&btf_prog_table);
941946
delete_btf_table(&btf_map_table);
947+
delete_obj_refs_table(&refs_table);
942948

943949
return err;
944950
}

0 commit comments

Comments
 (0)