Skip to content

Commit

Permalink
collection: populate kallsyms caches in newCollectionLoader
Browse files Browse the repository at this point in the history
As mentioned in the code comments, scan all programs in the CollectionSpec
to allow batching together kallsyms lookups, avoiding repeated scanning
of /proc/kallsyms.

Signed-off-by: Timo Beckers <timo@isovalent.com>
  • Loading branch information
ti-mo committed Oct 23, 2024
1 parent 7f04cae commit d3c63ab
Showing 1 changed file with 44 additions and 0 deletions.
44 changes: 44 additions & 0 deletions collection.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/cilium/ebpf/asm"
"github.com/cilium/ebpf/btf"
"github.com/cilium/ebpf/internal"
"github.com/cilium/ebpf/internal/kallsyms"
"github.com/cilium/ebpf/internal/kconfig"
"github.com/cilium/ebpf/internal/linux"
)
Expand Down Expand Up @@ -400,6 +401,10 @@ func newCollectionLoader(coll *CollectionSpec, opts *CollectionOptions) (*collec
}
}

if err := populateKallsyms(coll.Programs); err != nil {
return nil, fmt.Errorf("populating kallsyms caches: %w", err)
}

return &collectionLoader{
coll,
opts,
Expand All @@ -408,6 +413,45 @@ func newCollectionLoader(coll *CollectionSpec, opts *CollectionOptions) (*collec
}, nil
}

// populateKallsyms populates kallsyms caches, making lookups cheaper later on
// during individual program loading. Since we have less context available
// at those stages, we batch the lookups here instead to avoid redundant work.
func populateKallsyms(progs map[string]*ProgramSpec) error {
// Look up associated kernel modules for all symbols referenced by
// ProgramSpec.AttachTo for program types that support attaching to kmods.
mods := make(map[string]string)
for _, p := range progs {
if p.AttachTo != "" && p.targetsKernelModule() {
mods[p.AttachTo] = ""
}
}
if len(mods) != 0 {
if err := kallsyms.AssignModules(mods); err != nil {
return fmt.Errorf("getting modules from kallsyms: %w", err)
}
}

// Look up addresses of all kernel symbols referenced by all programs.
addrs := make(map[string]uint64)
for _, p := range progs {
iter := p.Instructions.Iterate()
for iter.Next() {
ins := iter.Ins
meta, _ := ins.Metadata.Get(ksymMetaKey{}).(*ksymMeta)
if meta != nil {
addrs[meta.Name] = 0
}
}
}
if len(addrs) != 0 {
if err := kallsyms.AssignAddresses(addrs); err != nil {
return fmt.Errorf("getting addresses from kallsyms: %w", err)
}
}

return nil
}

// close all resources left over in the collectionLoader.
func (cl *collectionLoader) close() {
for _, m := range cl.maps {
Expand Down

0 comments on commit d3c63ab

Please sign in to comment.