Improve EBPF cleanup (#21501)
Unlink shared memory and semaphore for eBPF cgroup during cleanup. Remove duplicate sem_post
Stelios Fragkakis committed
Jan 4, 2026 at 20:46 UTC
a59c441442a8281be77228efca1deb44310edfd5
2 files changed
+9
-3
src/collectors/cgroups.plugin/cgroup-discovery.c
+9
-1
@@ -1055,6 +1055,12 @@ static void netdata_cgroup_ebpf_set_values(size_t length)
1055
1056
static void netdata_cgroup_ebpf_initialize_shm()
1057
{
1058
+ // Unlink any existing shared memory and semaphore to start fresh.
1059
+ // This prevents truncating memory that another process might be using.
1060
+ // Existing mappings in other processes remain valid until they unmap.
1061
+ (void) shm_unlink(NETDATA_SHARED_MEMORY_EBPF_CGROUP_NAME);
1062
+ (void) sem_unlink(NETDATA_NAMED_SEMAPHORE_EBPF_CGROUP_NAME);
1063
+
1064
shm_fd_cgroup_ebpf = shm_open(NETDATA_SHARED_MEMORY_EBPF_CGROUP_NAME, O_CREAT | O_RDWR, 0660);
1065
if (shm_fd_cgroup_ebpf < 0) {
1066
collector_error("Cannot initialize shared memory used by cgroup and eBPF, integration won't happen.");
@@ -1093,13 +1099,14 @@ static void netdata_cgroup_ebpf_initialize_shm()
1099
end_init_shm:
1100
close(shm_fd_cgroup_ebpf);
1101
shm_fd_cgroup_ebpf = -1;
1096
- shm_unlink(NETDATA_SHARED_MEMORY_EBPF_CGROUP_NAME);
1102
+ (void) shm_unlink(NETDATA_SHARED_MEMORY_EBPF_CGROUP_NAME);
1103
}
1104
1105
static void cgroup_cleanup_ebpf_integration()
1106
{
1107
if (shm_mutex_cgroup_ebpf != SEM_FAILED) {
1108
sem_close(shm_mutex_cgroup_ebpf);
1109
+ (void) sem_unlink(NETDATA_NAMED_SEMAPHORE_EBPF_CGROUP_NAME);
1110
}
1111
1112
if (shm_cgroup_ebpf.header) {
@@ -1110,6 +1117,7 @@ static void cgroup_cleanup_ebpf_integration()
1117
if (shm_fd_cgroup_ebpf > 0) {
1118
close(shm_fd_cgroup_ebpf);
1119
}
1120
+ (void) shm_unlink(NETDATA_SHARED_MEMORY_EBPF_CGROUP_NAME);
1121
}
1122
1123
// ----------------------------------------------------------------------------
src/collectors/ebpf.plugin/ebpf_cgroup.c
-2
@@ -310,8 +310,6 @@ void ebpf_parse_cgroup_shm_data()
310
previous,
311
shm_ebpf_cgroup.header->cgroup_root_count);
312
#endif
313
-
314
- sem_post(shm_sem_ebpf_cgroup);
313
}
314
315
// --------------------------------------------------------------------------------------------------------------------