diff --git a/processmanager/ebpf/ebpf.go b/processmanager/ebpf/ebpf.go index 5bbfbfaab..6b8459b7e 100644 --- a/processmanager/ebpf/ebpf.go +++ b/processmanager/ebpf/ebpf.go @@ -640,7 +640,7 @@ func (impl *ebpfMapsImpl) UpdatePidPageMappingInfo(pid libpf.PID, prefix lpm.Pre // DeletePidPageMappingInfo removes the elements specified by prefixes from eBPF map // pid_page_to_mapping_info and returns the number of elements removed. -func (impl *ebpfMapsImpl) DeletePidPageMappingInfo(pid libpf.PID, prefixes []lpm.Prefix) (int, +func (impl *ebpfMapsImpl) DeletePidPageMappingInfo(pid libpf.PID, prefixes []lpm.Prefix) (uint64, error, ) { if impl.hasLPMTrieBatchOperations { @@ -656,11 +656,11 @@ func (impl *ebpfMapsImpl) DeletePidPageMappingInfo(pid libpf.PID, prefixes []lpm return impl.DeletePidPageMappingInfoSingle(pid, prefixes) } -func (impl *ebpfMapsImpl) DeletePidPageMappingInfoSingle(pid libpf.PID, prefixes []lpm.Prefix) (int, +func (impl *ebpfMapsImpl) DeletePidPageMappingInfoSingle(pid libpf.PID, prefixes []lpm.Prefix) (uint64, error, ) { cKey := &support.PIDPage{} - var deleted int + var deleted uint64 var combinedErrors error for _, prefix := range prefixes { *cKey = getPIDPageFromPrefix(pid, prefix) @@ -674,7 +674,7 @@ func (impl *ebpfMapsImpl) DeletePidPageMappingInfoSingle(pid libpf.PID, prefixes return deleted, combinedErrors } -func (impl *ebpfMapsImpl) DeletePidPageMappingInfoBatch(pid libpf.PID, prefixes []lpm.Prefix) (int, +func (impl *ebpfMapsImpl) DeletePidPageMappingInfoBatch(pid libpf.PID, prefixes []lpm.Prefix) (uint64, error, ) { // Prepare all keys based on the given prefixes. @@ -685,7 +685,13 @@ func (impl *ebpfMapsImpl) DeletePidPageMappingInfoBatch(pid libpf.PID, prefixes deleted, err := impl.PidPageToMappingInfo.BatchDelete( ptrCastMarshaler[support.PIDPage](cKeys), nil) - return deleted, impl.trackMapError(metrics.IDPidPageToMappingInfoBatchDelete, err) + + // BatchDelete returns a count of deleted entries, so this should never happen. + if deleted < 0 { + err = errors.Join(err, fmt.Errorf("negative batch delete count: %d", deleted)) + deleted = 0 + } + return uint64(deleted), impl.trackMapError(metrics.IDPidPageToMappingInfoBatchDelete, err) } // LookupPidPageInformation returns the fileID and bias for a given pid and page combination from diff --git a/processmanager/ebpfapi/ebpf.go b/processmanager/ebpfapi/ebpf.go index 0f8398138..aa244f728 100644 --- a/processmanager/ebpfapi/ebpf.go +++ b/processmanager/ebpfapi/ebpf.go @@ -51,7 +51,7 @@ type EbpfHandler interface { // DeletePidPageMappingInfo removes the elements specified by prefixes from eBPF map // pid_page_to_mapping_info and returns the number of elements removed. - DeletePidPageMappingInfo(pid libpf.PID, prefixes []lpm.Prefix) (int, error) + DeletePidPageMappingInfo(pid libpf.PID, prefixes []lpm.Prefix) (uint64, error) // CollectMetrics returns gathered errors for changes to eBPF maps. CollectMetrics() []metrics.Metric diff --git a/processmanager/processinfo.go b/processmanager/processinfo.go index f0d5966c9..855717e86 100644 --- a/processmanager/processinfo.go +++ b/processmanager/processinfo.go @@ -318,7 +318,7 @@ func (pm *ProcessManager) processRemovedMapping(pid libpf.PID, m *Mapping) uint6 fileID := host.FileIDFromLibpf(mf.File.Value().FileID) pm.eim.DecRef(fileID) - return uint64(deleted) + return deleted } // Caller is responsible to hold pm.mu write lock to avoid race conditions. @@ -456,11 +456,11 @@ func (pm *ProcessManager) processPIDExit(pid libpf.PID) { err = errors.Join(err, fmt.Errorf("failed to delete dummy prefix for PID %d: %v", pid, err2)) } - pm.pidPageToMappingInfoSize -= uint64(deleted) for idx := range info.mappings { - pm.processRemovedMapping(pid, &info.mappings[idx]) + deleted += pm.processRemovedMapping(pid, &info.mappings[idx]) } + pm.pidPageToMappingInfoSize -= min(pm.pidPageToMappingInfoSize, deleted) pm.processRemovedInterpreters(pid, libpf.Set[util.OnDiskFileIdentifier]{}) } @@ -651,7 +651,7 @@ func (pm *ProcessManager) SynchronizeProcess(pr process.Process) { for _, m := range mpRemove { numChanges += pm.processRemovedMapping(pid, m) } - pm.pidPageToMappingInfoSize -= numChanges + pm.pidPageToMappingInfoSize -= min(pm.pidPageToMappingInfoSize, numChanges) pm.mu.Lock() pm.processRemovedInterpreters(pid, interpretersValid) pm.mu.Unlock() diff --git a/tools/coredump/ebpfmaps.go b/tools/coredump/ebpfmaps.go index db9b83709..db5070665 100644 --- a/tools/coredump/ebpfmaps.go +++ b/tools/coredump/ebpfmaps.go @@ -234,9 +234,9 @@ func (emc *ebpfMapsCoredump) UpdatePidPageMappingInfo(pid libpf.PID, prefix lpm. host.FileID(fileID), bias) } -func (emc *ebpfMapsCoredump) DeletePidPageMappingInfo(pid libpf.PID, prefixes []lpm.Prefix) (int, +func (emc *ebpfMapsCoredump) DeletePidPageMappingInfo(pid libpf.PID, prefixes []lpm.Prefix) (uint64, error) { - var deleted int + var deleted uint64 for _, prefix := range prefixes { if err := emc.DeletePidInterpreterMapping(pid, prefix); err != nil { return deleted, err