mirror of
https://git.kernel.org/pub/scm/linux/kernel/git/stable/linux.git
synced 2024-10-31 16:38:12 +00:00
0a80cf67f3
Add selftest with few subtests testing proper bpf_cookie usage. Kprobe and uprobe subtests are pretty straightforward and just validate that the same BPF program attached with different bpf_cookie will be triggered with those different bpf_cookie values. Tracepoint subtest is a bit more interesting, as it is the only perf_event-based BPF hook that shares bpf_prog_array between multiple perf_events internally. This means that the same BPF program can't be attached to the same tracepoint multiple times. So we have 3 identical copies. This arrangement allows to test bpf_prog_array_copy()'s handling of bpf_prog_array list manipulation logic when programs are attached and detached. The test validates that bpf_cookie isn't mixed up and isn't lost during such list manipulations. Perf_event subtest validates that two BPF links can be created against the same perf_event (but not at the same time, only one BPF program can be attached to perf_event itself), and that for each we can specify different bpf_cookie value. Signed-off-by: Andrii Nakryiko <andrii@kernel.org> Signed-off-by: Daniel Borkmann <daniel@iogearbox.net> Link: https://lore.kernel.org/bpf/20210815070609.987780-15-andrii@kernel.org
85 lines
1.6 KiB
C
85 lines
1.6 KiB
C
// SPDX-License-Identifier: GPL-2.0
|
|
/* Copyright (c) 2021 Facebook */
|
|
|
|
#include "vmlinux.h"
|
|
#include <bpf/bpf_helpers.h>
|
|
#include <bpf/bpf_tracing.h>
|
|
|
|
int my_tid;
|
|
|
|
int kprobe_res;
|
|
int kprobe_multi_res;
|
|
int kretprobe_res;
|
|
int uprobe_res;
|
|
int uretprobe_res;
|
|
int tp_res;
|
|
int pe_res;
|
|
|
|
static void update(void *ctx, int *res)
|
|
{
|
|
if (my_tid != (u32)bpf_get_current_pid_tgid())
|
|
return;
|
|
|
|
*res |= bpf_get_attach_cookie(ctx);
|
|
}
|
|
|
|
SEC("kprobe/sys_nanosleep")
|
|
int handle_kprobe(struct pt_regs *ctx)
|
|
{
|
|
update(ctx, &kprobe_res);
|
|
return 0;
|
|
}
|
|
|
|
SEC("kretprobe/sys_nanosleep")
|
|
int handle_kretprobe(struct pt_regs *ctx)
|
|
{
|
|
update(ctx, &kretprobe_res);
|
|
return 0;
|
|
}
|
|
|
|
SEC("uprobe/trigger_func")
|
|
int handle_uprobe(struct pt_regs *ctx)
|
|
{
|
|
update(ctx, &uprobe_res);
|
|
return 0;
|
|
}
|
|
|
|
SEC("uretprobe/trigger_func")
|
|
int handle_uretprobe(struct pt_regs *ctx)
|
|
{
|
|
update(ctx, &uretprobe_res);
|
|
return 0;
|
|
}
|
|
|
|
/* bpf_prog_array, used by kernel internally to keep track of attached BPF
|
|
* programs to a given BPF hook (e.g., for tracepoints) doesn't allow the same
|
|
* BPF program to be attached multiple times. So have three identical copies
|
|
* ready to attach to the same tracepoint.
|
|
*/
|
|
SEC("tp/syscalls/sys_enter_nanosleep")
|
|
int handle_tp1(struct pt_regs *ctx)
|
|
{
|
|
update(ctx, &tp_res);
|
|
return 0;
|
|
}
|
|
SEC("tp/syscalls/sys_enter_nanosleep")
|
|
int handle_tp2(struct pt_regs *ctx)
|
|
{
|
|
update(ctx, &tp_res);
|
|
return 0;
|
|
}
|
|
SEC("tp/syscalls/sys_enter_nanosleep")
|
|
int handle_tp3(void *ctx)
|
|
{
|
|
update(ctx, &tp_res);
|
|
return 1;
|
|
}
|
|
|
|
SEC("perf_event")
|
|
int handle_pe(struct pt_regs *ctx)
|
|
{
|
|
update(ctx, &pe_res);
|
|
return 0;
|
|
}
|
|
|
|
char _license[] SEC("license") = "GPL";
|