Skip to content

Commit 15fcf67

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.
1 parent 9a605d3 commit 15fcf67

File tree

1 file changed

+10
-5
lines changed

1 file changed

+10
-5
lines changed

ebpf_prog/opensnitch-procs.c

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -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
};

0 commit comments

Comments
 (0)