Skip to content

Commit

Permalink
samples/bpf: Use kyscall instead of kprobe in syscall tracing program
Browse files Browse the repository at this point in the history
Syscall tracing using kprobe is quite unstable. Since it uses the exact
name of the kernel function, the program might broke due to the rename
of a function. The problem can also be caused by a changes in the
arguments of the function to which the kprobe connects.

In this commit, ksyscall is used instead of kprobe. By using ksyscall,
libbpf will detect the appropriate kernel function name.
(e.g. sys_write -> __s390_sys_write). This eliminates the need to worry
about which wrapper function to attach in order to parse arguments.

In addition, ksyscall provides more fine method with attaching system
call, the coarse SYSCALL helper at trace_common.h can be removed.

Signed-off-by: Daniel T. Lee <[email protected]>
Signed-off-by: Andrii Nakryiko <[email protected]>
Link: https://lore.kernel.org/bpf/[email protected]
  • Loading branch information
DanielTimLee authored and anakryiko committed Dec 29, 2022
1 parent 3046500 commit 1d0c5f6
Show file tree
Hide file tree
Showing 7 changed files with 12 additions and 31 deletions.
17 changes: 8 additions & 9 deletions samples/bpf/map_perf_test_kern.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
#include <bpf/bpf_helpers.h>
#include <bpf/bpf_tracing.h>
#include <bpf/bpf_core_read.h>
#include "trace_common.h"

#define MAX_ENTRIES 1000
#define MAX_NR_CPUS 1024
Expand Down Expand Up @@ -102,7 +101,7 @@ struct {
__uint(max_entries, MAX_ENTRIES);
} lru_hash_lookup_map SEC(".maps");

SEC("kprobe/" SYSCALL(sys_getuid))
SEC("ksyscall/getuid")
int stress_hmap(struct pt_regs *ctx)
{
u32 key = bpf_get_current_pid_tgid();
Expand All @@ -120,7 +119,7 @@ int stress_hmap(struct pt_regs *ctx)
return 0;
}

SEC("kprobe/" SYSCALL(sys_geteuid))
SEC("ksyscall/geteuid")
int stress_percpu_hmap(struct pt_regs *ctx)
{
u32 key = bpf_get_current_pid_tgid();
Expand All @@ -137,7 +136,7 @@ int stress_percpu_hmap(struct pt_regs *ctx)
return 0;
}

SEC("kprobe/" SYSCALL(sys_getgid))
SEC("ksyscall/getgid")
int stress_hmap_alloc(struct pt_regs *ctx)
{
u32 key = bpf_get_current_pid_tgid();
Expand All @@ -154,7 +153,7 @@ int stress_hmap_alloc(struct pt_regs *ctx)
return 0;
}

SEC("kprobe/" SYSCALL(sys_getegid))
SEC("ksyscall/getegid")
int stress_percpu_hmap_alloc(struct pt_regs *ctx)
{
u32 key = bpf_get_current_pid_tgid();
Expand All @@ -171,7 +170,7 @@ int stress_percpu_hmap_alloc(struct pt_regs *ctx)
return 0;
}

SEC("kprobe/" SYSCALL(sys_connect))
SEC("ksyscall/connect")
int stress_lru_hmap_alloc(struct pt_regs *ctx)
{
struct pt_regs *real_regs = (struct pt_regs *)PT_REGS_PARM1_CORE(ctx);
Expand Down Expand Up @@ -251,7 +250,7 @@ int stress_lru_hmap_alloc(struct pt_regs *ctx)
return 0;
}

SEC("kprobe/" SYSCALL(sys_gettid))
SEC("ksyscall/gettid")
int stress_lpm_trie_map_alloc(struct pt_regs *ctx)
{
union {
Expand All @@ -273,7 +272,7 @@ int stress_lpm_trie_map_alloc(struct pt_regs *ctx)
return 0;
}

SEC("kprobe/" SYSCALL(sys_getpgid))
SEC("ksyscall/getpgid")
int stress_hash_map_lookup(struct pt_regs *ctx)
{
u32 key = 1, i;
Expand All @@ -286,7 +285,7 @@ int stress_hash_map_lookup(struct pt_regs *ctx)
return 0;
}

SEC("kprobe/" SYSCALL(sys_getppid))
SEC("ksyscall/getppid")
int stress_array_map_lookup(struct pt_regs *ctx)
{
u32 key = 1, i;
Expand Down
3 changes: 1 addition & 2 deletions samples/bpf/test_current_task_under_cgroup_kern.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
#include <linux/version.h>
#include <bpf/bpf_helpers.h>
#include <uapi/linux/utsname.h>
#include "trace_common.h"

struct {
__uint(type, BPF_MAP_TYPE_CGROUP_ARRAY);
Expand All @@ -27,7 +26,7 @@ struct {
} perf_map SEC(".maps");

/* Writes the last PID that called sync to a map at index 0 */
SEC("kprobe/" SYSCALL(sys_sync))
SEC("ksyscall/sync")
int bpf_prog1(struct pt_regs *ctx)
{
u64 pid = bpf_get_current_pid_tgid();
Expand Down
1 change: 0 additions & 1 deletion samples/bpf/test_map_in_map_kern.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
#include <bpf/bpf_helpers.h>
#include <bpf/bpf_tracing.h>
#include <bpf/bpf_core_read.h>
#include "trace_common.h"

#define MAX_NR_PORTS 65536

Expand Down
3 changes: 1 addition & 2 deletions samples/bpf/test_probe_write_user_kern.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
#include <bpf/bpf_helpers.h>
#include <bpf/bpf_tracing.h>
#include <bpf/bpf_core_read.h>
#include "trace_common.h"

struct {
__uint(type, BPF_MAP_TYPE_HASH);
Expand All @@ -28,7 +27,7 @@ struct {
* This example sits on a syscall, and the syscall ABI is relatively stable
* of course, across platforms, and over time, the ABI may change.
*/
SEC("kprobe/" SYSCALL(sys_connect))
SEC("ksyscall/connect")
int bpf_prog1(struct pt_regs *ctx)
{
struct pt_regs *real_regs = (struct pt_regs *)PT_REGS_PARM1_CORE(ctx);
Expand Down
13 changes: 0 additions & 13 deletions samples/bpf/trace_common.h

This file was deleted.

3 changes: 1 addition & 2 deletions samples/bpf/trace_output_kern.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
#include <linux/version.h>
#include <uapi/linux/bpf.h>
#include <bpf/bpf_helpers.h>
#include "trace_common.h"

struct {
__uint(type, BPF_MAP_TYPE_PERF_EVENT_ARRAY);
Expand All @@ -11,7 +10,7 @@ struct {
__uint(max_entries, 2);
} my_map SEC(".maps");

SEC("kprobe/" SYSCALL(sys_write))
SEC("ksyscall/write")
int bpf_prog1(struct pt_regs *ctx)
{
struct S {
Expand Down
3 changes: 1 addition & 2 deletions samples/bpf/tracex2_kern.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
#include <uapi/linux/bpf.h>
#include <bpf/bpf_helpers.h>
#include <bpf/bpf_tracing.h>
#include "trace_common.h"

struct {
__uint(type, BPF_MAP_TYPE_HASH);
Expand Down Expand Up @@ -78,7 +77,7 @@ struct {
__uint(max_entries, 1024);
} my_hist_map SEC(".maps");

SEC("kprobe/" SYSCALL(sys_write))
SEC("ksyscall/write")
int bpf_prog3(struct pt_regs *ctx)
{
long write_size = PT_REGS_PARM3(ctx);
Expand Down

0 comments on commit 1d0c5f6

Please sign in to comment.