@cryptotaxi247 / netdata-1 / commits / 6785b28a3

Fix epbf crash when process exit (#10957)

thiagoftsm committed Apr 14, 2021 at 09:22 UTC 6785b28a30513019ea919a61e05782980f49918b
8 files changed +26 -36
collectors/ebpf.plugin/ebpf.c
+11 -1
@@ -130,7 +130,17 @@ static void ebpf_exit(int sig)
130 return;
131 }
132
133 - freez(global_process_stat);
133 + if (ebpf_modules[EBPF_MODULE_SOCKET_IDX].enabled) {
134 + ebpf_modules[EBPF_MODULE_SOCKET_IDX].enabled = 0;
135 + clean_socket_apps_structures();
136 + freez(socket_bandwidth_curr);
137 + }
138 +
139 + if (ebpf_modules[EBPF_MODULE_CACHESTAT_IDX].enabled) {
140 + ebpf_modules[EBPF_MODULE_CACHESTAT_IDX].enabled = 0;
141 + clean_cachestat_pid_structures();
142 + freez(cachestat_pid);
143 + }
144
145 /*
146 int ret = fork();
collectors/ebpf.plugin/ebpf_apps.c
+2 -1
@@ -943,7 +943,6 @@ void cleanup_exited_pids()
943
944 pid_t r = p->pid;
945 p = p->next;
946 - del_pid_entry(r);
946
947 // Clean process structure
948 freez(global_process_stats[r]);
@@ -953,6 +952,8 @@ void cleanup_exited_pids()
952 current_apps_data[r] = NULL;
953
954 cleanup_variables_from_other_threads(r);
955 +
956 + del_pid_entry(r);
957 } else {
958 if (unlikely(p->keep))
959 p->keeploops++;
collectors/ebpf.plugin/ebpf_apps.h
+2
@@ -430,6 +430,8 @@ extern size_t read_bandwidth_statistic_using_pid_on_target(ebpf_bandwidth_t **ep
430
431 extern void collect_data_for_all_processes(int tbl_pid_stats_fd);
432
433 +extern void clean_global_memory();
434 +
435 extern ebpf_process_stat_t **global_process_stats;
436 extern ebpf_process_publish_apps_t **current_apps_data;
437 extern netdata_publish_cachestat_t **cachestat_pid;
collectors/ebpf.plugin/ebpf_cachestat.c
+4 -8
@@ -43,7 +43,7 @@ struct config cachestat_config = { .first_section = NULL,
43 *
44 * Clean the allocated structures.
45 */
46 -static void clean_pid_structures() {
46 +void clean_cachestat_pid_structures() {
47 struct pid_stat *pids = root_of_pids;
48 while (pids) {
49 freez(cachestat_pid[pids->pid]);
@@ -71,9 +71,6 @@ static void ebpf_cachestat_cleanup(void *ptr)
71 UNUSED(dt);
72 }
73
74 - clean_pid_structures();
75 - freez(cachestat_pid);
76 -
74 ebpf_cleanup_publish_syscall(cachestat_counter_publish_aggregated);
75
76 freez(cachestat_vector);
@@ -360,15 +357,11 @@ void *ebpf_cachestat_read_hash(void *ptr)
357 ebpf_module_t *em = (ebpf_module_t *)ptr;
358
359 usec_t step = NETDATA_LATENCY_CACHESTAT_SLEEP_MS * em->update_time;
363 - int apps = em->apps_charts;
360 while (!close_ebpf_plugin) {
361 usec_t dt = heartbeat_next(&hb, step);
362 (void)dt;
363
364 read_global_table();
369 -
370 - if (apps)
371 - read_apps_table();
365 }
366 read_thread_closed = 1;
367
@@ -512,6 +505,9 @@ static void cachestat_collector(ebpf_module_t *em)
505 pthread_mutex_lock(&collect_data_mutex);
506 pthread_cond_wait(&collect_data_cond_var, &collect_data_mutex);
507
508 + if (apps)
509 + read_apps_table();
510 +
511 pthread_mutex_lock(&lock);
512
513 cachestat_send_global(&publish);
collectors/ebpf.plugin/ebpf_cachestat.h
+1
@@ -60,5 +60,6 @@ typedef struct netdata_publish_cachestat {
60 } netdata_publish_cachestat_t;
61
62 extern void *ebpf_cachestat_thread(void *ptr);
63 +extern void clean_cachestat_pid_structures();
64
65 #endif // NETDATA_EBPF_CACHESTAT_H
collectors/ebpf.plugin/ebpf_process.c
-21
@@ -905,26 +905,6 @@ void clean_global_memory() {
905 }
906 }
907
908 -void clean_pid_on_target(struct pid_on_target *ptr) {
909 - while (ptr) {
910 - struct pid_on_target *next = ptr->next;
911 - freez(ptr);
912 -
913 - ptr = next;
914 - }
915 -}
916 -
917 -void clean_apps_structures(struct target *ptr) {
918 - struct target *agdt = ptr;
919 - while (agdt) {
920 - struct target *next = agdt->next;
921 - clean_pid_on_target(agdt->root_pid);
922 - freez(agdt);
923 -
924 - agdt = next;
925 - }
926 -}
927 -
908 /**
909 * Clean up the main thread.
910 *
@@ -949,7 +929,6 @@ static void ebpf_process_cleanup(void *ptr)
929 freez(global_process_stats);
930 freez(current_apps_data);
931
952 - clean_apps_structures(apps_groups_root_target);
932 freez(process_data.map_fd);
933
934 struct bpf_program *prog;
collectors/ebpf.plugin/ebpf_socket.c
+5 -5
@@ -1325,7 +1325,7 @@ static void read_socket_hash_table(int fd, int family, int network_connection)
1325 return;
1326
1327 netdata_socket_idx_t key = {};
1328 - netdata_socket_idx_t next_key;
1328 + netdata_socket_idx_t next_key = {};
1329 netdata_socket_idx_t removeme;
1330 int removesock = 0;
1331
@@ -1421,7 +1421,7 @@ void update_listen_table(uint16_t value, uint8_t proto)
1421 static void read_listen_table()
1422 {
1423 uint16_t key = 0;
1424 - uint16_t next_key;
1424 + uint16_t next_key = 0;
1425
1426 int fd = map_fd[NETDATA_SOCKET_LISTEN_TABLE];
1427 uint8_t value;
@@ -1769,7 +1769,7 @@ static void clean_hostnames(ebpf_network_viewer_hostname_list_t *hostnames)
1769 }
1770 }
1771
1772 -void clean_thread_structures() {
1772 +void clean_socket_apps_structures() {
1773 struct pid_stat *pids = root_of_pids;
1774 while (pids) {
1775 freez(socket_bandwidth_curr[pids->pid]);
@@ -1853,8 +1853,6 @@ static void ebpf_socket_cleanup(void *ptr)
1853 ebpf_cleanup_publish_syscall(socket_publish_aggregated);
1854 freez(socket_hash_values);
1855
1856 - clean_thread_structures();
1857 - freez(socket_bandwidth_curr);
1856 freez(bandwidth_vector);
1857
1858 freez(socket_values);
@@ -2822,6 +2820,8 @@ void *ebpf_socket_thread(void *ptr)
2820 {
2821 netdata_thread_cleanup_push(ebpf_socket_cleanup, ptr);
2822
2823 + memset(&inbound_vectors.tree, 0, sizeof(avl_tree_lock));
2824 + memset(&outbound_vectors.tree, 0, sizeof(avl_tree_lock));
2825 avl_init_lock(&inbound_vectors.tree, compare_sockets);
2826 avl_init_lock(&outbound_vectors.tree, compare_sockets);
2827
collectors/ebpf.plugin/ebpf_socket.h
+1
@@ -294,6 +294,7 @@ extern void update_listen_table(uint16_t value, uint8_t proto);
294 extern void parse_network_viewer_section(struct config *cfg);
295 extern void fill_ip_list(ebpf_network_viewer_ip_list_t **out, ebpf_network_viewer_ip_list_t *in, char *table);
296 extern void parse_service_name_section(struct config *cfg);
297 +extern void clean_socket_apps_structures();
298
299 extern ebpf_socket_publish_apps_t **socket_bandwidth_curr;
300