Patchwork [v7,perf,bpf-next,5/8] perf, bpf: emit PERF_RECORD_KSYMBOL for BPF program load/unload

login
register
mail settings
Submitter Song Liu
Date Jan. 11, 2019, 12:19 a.m.
Message ID <20190111001933.4153792-6-songliubraving@fb.com>
Download mbox | patch
Permalink /patch/697383/
State New
Headers show

Comments

Song Liu - Jan. 11, 2019, 12:19 a.m.
This patch enables emitting PERF_RECORD_KSYMBOL for BPF program load and
unload. A PERF_RECORD_KSYMBOL is issued for each BPF sub program.

Signed-off-by: Song Liu <songliubraving@fb.com>
---
 kernel/events/core.c | 34 ++++++++++++++++++++++++++++++++++
 1 file changed, 34 insertions(+)

Patch

diff --git a/kernel/events/core.c b/kernel/events/core.c
index 8a8fa1b066ff..023821605b8d 100644
--- a/kernel/events/core.c
+++ b/kernel/events/core.c
@@ -7798,6 +7798,30 @@  static int perf_event_bpf_get_name(char *name, int len, void *data)
 	return 0;
 }
 
+static void perf_event_bpf_emit_ksymbols(struct bpf_prog *prog,
+					 enum perf_bpf_event_type type)
+{
+	bool unregister = type == PERF_BPF_EVENT_PROG_UNLOAD;
+	int i;
+
+	if (prog->aux->func_cnt == 0) {
+		perf_event_ksymbol(PERF_RECORD_KSYMBOL_TYPE_BPF,
+				   (u64)(unsigned long)prog->bpf_func,
+				   prog->jited_len, unregister,
+				   perf_event_bpf_get_name, prog);
+	} else {
+		for (i = 0; i < prog->aux->func_cnt; i++) {
+			struct bpf_prog *subprog = prog->aux->func[i];
+
+			perf_event_ksymbol(
+				PERF_RECORD_KSYMBOL_TYPE_BPF,
+				(u64)(unsigned long)subprog->bpf_func,
+				subprog->jited_len, unregister,
+				perf_event_bpf_get_name, subprog);
+		}
+	}
+}
+
 void perf_event_bpf_event(struct bpf_prog *prog,
 			  enum perf_bpf_event_type type,
 			  u16 flags)
@@ -7808,6 +7832,16 @@  void perf_event_bpf_event(struct bpf_prog *prog,
 	    type >= PERF_BPF_EVENT_MAX)
 		return;
 
+	switch (type) {
+	case PERF_BPF_EVENT_PROG_LOAD:
+	case PERF_BPF_EVENT_PROG_UNLOAD:
+		if (atomic_read(&nr_ksymbol_events))
+			perf_event_bpf_emit_ksymbols(prog, type);
+		break;
+	default:
+		break;
+	}
+
 	if (!atomic_read(&nr_bpf_events))
 		return;