Skip to content

Commit 7442bec

Browse files
ebpf: performance improvement for opensnitch-procs
We were sending to userspace unnecessary exit events, consuming unnecessary CPU cycles. We only intercept execve and execveat, but sched_process_exit is invoked by more functions (sched_process_exit, clone, ...), so we were receiving on the daemon events that we did nothing with them, apart from consuming CPU cycles. On some scenarios like on servers running saltstack (as salt-master), this caused to consume more CPU than needed. cherry picked from 15fcf67
1 parent 81dd625 commit 7442bec

File tree

1 file changed

+16
-11
lines changed

1 file changed

+16
-11
lines changed

ebpf_prog/opensnitch-procs.c

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,10 @@ struct bpf_map_def SEC("maps/proc-events") events = {
1111
};
1212

1313
struct bpf_map_def SEC("maps/execMap") execMap = {
14-
.type = BPF_MAP_TYPE_HASH,
15-
.key_size = sizeof(u32),
16-
.value_size = sizeof(struct data_t),
17-
.max_entries = 256,
14+
.type = BPF_MAP_TYPE_HASH,
15+
.key_size = sizeof(u32),
16+
.value_size = sizeof(struct data_t),
17+
.max_entries = 256,
1818
};
1919

2020

@@ -46,14 +46,12 @@ static __always_inline void __handle_exit_execve(struct trace_sys_exit_execve *c
4646
{
4747
u64 pid_tgid = bpf_get_current_pid_tgid();
4848
struct data_t *proc = bpf_map_lookup_elem(&execMap, &pid_tgid);
49+
// don't delete the pid from execMap here, delegate it to sched_process_exit
4950
if (proc == NULL) { return; }
50-
if (ctx->ret != 0) { goto out; }
51+
if (ctx->ret != 0) { return; }
5152
proc->ret_code = ctx->ret;
5253

5354
bpf_perf_event_output(ctx, &events, BPF_F_CURRENT_CPU, proc, sizeof(*proc));
54-
55-
out:
56-
bpf_map_delete_elem(&execMap, &pid_tgid);
5755
}
5856

5957
// https://0xax.gitbooks.io/linux-insides/content/SysCall/linux-syscall-4.html
@@ -63,6 +61,14 @@ static __always_inline void __handle_exit_execve(struct trace_sys_exit_execve *c
6361
SEC("tracepoint/sched/sched_process_exit")
6462
int tracepoint__sched_sched_process_exit(struct pt_regs *ctx)
6563
{
64+
u64 pid_tgid = bpf_get_current_pid_tgid();
65+
struct data_t *proc = bpf_map_lookup_elem(&execMap, &pid_tgid);
66+
// if the pid is not in execMap cache (because it's not of a pid we've
67+
// previously intercepted), do not send the event to userspace, because
68+
// we won't do anything with it and it consumes CPU cycles (too much in some
69+
// scenarios).
70+
if (proc == NULL) { return 0; }
71+
6672
int zero = 0;
6773
struct data_t *data = bpf_map_lookup_elem(&heapstore, &zero);
6874
if (!data){ return 0; }
@@ -71,7 +77,6 @@ int tracepoint__sched_sched_process_exit(struct pt_regs *ctx)
7177
data->type = EVENT_SCHED_EXIT;
7278
bpf_perf_event_output(ctx, &events, BPF_F_CURRENT_CPU, data, sizeof(*data));
7379

74-
u64 pid_tgid = bpf_get_current_pid_tgid();
7580
bpf_map_delete_elem(&execMap, &pid_tgid);
7681
return 0;
7782
};
@@ -129,7 +134,7 @@ int tracepoint__syscalls_sys_enter_execve(struct trace_sys_enter_execve* ctx)
129134
#else
130135
// in case of failure adding the item to the map, send it directly
131136
u64 pid_tgid = bpf_get_current_pid_tgid();
132-
if (bpf_map_update_elem(&execMap, &pid_tgid, data, BPF_ANY) != 0) {
137+
if (bpf_map_update_elem(&execMap, &pid_tgid, data, BPF_ANY) != 0) {
133138

134139
// With some commands, this helper fails with error -28 (ENOSPC). Misleading error? cmd failed maybe?
135140
// BUG: after coming back from suspend state, this helper fails with error -95 (EOPNOTSUPP)
@@ -180,7 +185,7 @@ int tracepoint__syscalls_sys_enter_execveat(struct trace_sys_enter_execveat* ctx
180185
#else
181186
// in case of failure adding the item to the map, send it directly
182187
u64 pid_tgid = bpf_get_current_pid_tgid();
183-
if (bpf_map_update_elem(&execMap, &pid_tgid, data, BPF_ANY) != 0) {
188+
if (bpf_map_update_elem(&execMap, &pid_tgid, data, BPF_ANY) != 0) {
184189

185190
// With some commands, this helper fails with error -28 (ENOSPC). Misleading error? cmd failed maybe?
186191
// BUG: after coming back from suspend state, this helper fails with error -95 (EOPNOTSUPP)

0 commit comments

Comments
 (0)