Skip to content

Commit 07fff6e

Browse files
committed
collection: document garbage collection behaviour of *Map and *Program
Since this behaviour can be surprising, especially when handling prog arrays, document the recommended usage on functions returning Collection/Map/Program. Signed-off-by: Timo Beckers <[email protected]>
1 parent 4785297 commit 07fff6e

File tree

2 files changed

+27
-3
lines changed

2 files changed

+27
-3
lines changed

collection.go

+18-3
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,9 @@ func (cs *CollectionSpec) Assign(to interface{}) error {
191191
// LoadAndAssign loads Maps and Programs into the kernel and assigns them
192192
// to a struct.
193193
//
194+
// Omitting Map/Program.Close() during application shutdown is an error.
195+
// See the package documentation for details around Map and Program lifecycle.
196+
//
194197
// This function is a shortcut to manually checking the presence
195198
// of maps and programs in a CollectionSpec. Consider using bpf2go
196199
// if this sounds useful.
@@ -273,12 +276,20 @@ type Collection struct {
273276
Maps map[string]*Map
274277
}
275278

276-
// NewCollection creates a Collection from a specification.
279+
// NewCollection creates a Collection from the given spec, creating and
280+
// loading its declared resources into the kernel.
281+
//
282+
// Omitting Collection.Close() during application shutdown is an error.
283+
// See the package documentation for details around Map and Program lifecycle.
277284
func NewCollection(spec *CollectionSpec) (*Collection, error) {
278285
return NewCollectionWithOptions(spec, CollectionOptions{})
279286
}
280287

281-
// NewCollectionWithOptions creates a Collection from a specification.
288+
// NewCollectionWithOptions creates a Collection from the given spec using
289+
// options, creating and loading its declared resources into the kernel.
290+
//
291+
// Omitting Collection.Close() during application shutdown is an error.
292+
// See the package documentation for details around Map and Program lifecycle.
282293
func NewCollectionWithOptions(spec *CollectionSpec, opts CollectionOptions) (*Collection, error) {
283294
loader := newCollectionLoader(spec, &opts)
284295
defer loader.cleanup()
@@ -531,7 +542,11 @@ func (cl *collectionLoader) populateMaps() error {
531542
return nil
532543
}
533544

534-
// LoadCollection parses an object file and converts it to a collection.
545+
// LoadCollection reads an object file and creates and loads its declared
546+
// resources into the kernel.
547+
//
548+
// Omitting Collection.Close() during application shutdown is an error.
549+
// See the package documentation for details around Map and Program lifecycle.
535550
func LoadCollection(file string) (*Collection, error) {
536551
spec, err := LoadCollectionSpec(file)
537552
if err != nil {

doc.go

+9
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,13 @@
1313
// your application as any other resource.
1414
//
1515
// Use the link subpackage to attach a loaded program to a hook in the kernel.
16+
//
17+
// Note that losing all references to Map and Program resources will cause
18+
// their underlying file descriptors to be closed, potentially removing those
19+
// objects from the kernel. Always retain a reference by e.g. deferring a
20+
// Close() of a Collection or LoadAndAssign object until application exit.
21+
//
22+
// Special care needs to be taken when handling maps of type ProgramArray,
23+
// as the kernel erases its contents when the last userspace or bpffs
24+
// reference disappears, regardless of the map being in active use.
1625
package ebpf

0 commit comments

Comments
 (0)