@@ -11,10 +11,10 @@ struct bpf_map_def SEC("maps/proc-events") events = {
11
11
};
12
12
13
13
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 ,
18
18
};
19
19
20
20
@@ -46,14 +46,12 @@ static __always_inline void __handle_exit_execve(struct trace_sys_exit_execve *c
46
46
{
47
47
u64 pid_tgid = bpf_get_current_pid_tgid ();
48
48
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
49
50
if (proc == NULL ) { return ; }
50
- if (ctx -> ret != 0 ) { goto out ; }
51
+ if (ctx -> ret != 0 ) { return ; }
51
52
proc -> ret_code = ctx -> ret ;
52
53
53
54
bpf_perf_event_output (ctx , & events , BPF_F_CURRENT_CPU , proc , sizeof (* proc ));
54
-
55
- out :
56
- bpf_map_delete_elem (& execMap , & pid_tgid );
57
55
}
58
56
59
57
// 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
63
61
SEC ("tracepoint/sched/sched_process_exit" )
64
62
int tracepoint__sched_sched_process_exit (struct pt_regs * ctx )
65
63
{
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
+
66
72
int zero = 0 ;
67
73
struct data_t * data = bpf_map_lookup_elem (& heapstore , & zero );
68
74
if (!data ){ return 0 ; }
@@ -71,7 +77,6 @@ int tracepoint__sched_sched_process_exit(struct pt_regs *ctx)
71
77
data -> type = EVENT_SCHED_EXIT ;
72
78
bpf_perf_event_output (ctx , & events , BPF_F_CURRENT_CPU , data , sizeof (* data ));
73
79
74
- u64 pid_tgid = bpf_get_current_pid_tgid ();
75
80
bpf_map_delete_elem (& execMap , & pid_tgid );
76
81
return 0 ;
77
82
};
@@ -129,7 +134,7 @@ int tracepoint__syscalls_sys_enter_execve(struct trace_sys_enter_execve* ctx)
129
134
#else
130
135
// in case of failure adding the item to the map, send it directly
131
136
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 ) {
133
138
134
139
// With some commands, this helper fails with error -28 (ENOSPC). Misleading error? cmd failed maybe?
135
140
// 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
180
185
#else
181
186
// in case of failure adding the item to the map, send it directly
182
187
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 ) {
184
189
185
190
// With some commands, this helper fails with error -28 (ENOSPC). Misleading error? cmd failed maybe?
186
191
// BUG: after coming back from suspend state, this helper fails with error -95 (EOPNOTSUPP)
0 commit comments