Skip to content

Commit 8a0d217

Browse files
committed
all: use runtime.AddCleanup
Convert almost all uses of runtime.SetFinalizer to runtime.AddCleanup. The exception is the unsafe memory code, which doesn't seem that easy to convert. Signed-off-by: Lorenz Bauer <[email protected]>
1 parent f7a61b3 commit 8a0d217

File tree

8 files changed

+45
-29
lines changed

8 files changed

+45
-29
lines changed

btf/kernel.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -151,9 +151,9 @@ func loadKernelSpec() (*Spec, error) {
151151
return nil, fmt.Errorf("load vmlinux: %w", err)
152152
}
153153

154-
runtime.SetFinalizer(spec.decoder.sharedBuf, func(_ *sharedBuf) {
154+
runtime.AddCleanup(spec.decoder.sharedBuf, func(raw []byte) {
155155
_ = unix.Munmap(raw)
156-
})
156+
}, raw)
157157

158158
return spec, nil
159159
}

internal/epoll/poller.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ type Poller struct {
3434
eventMu sync.Mutex
3535
closeEvent *eventFd
3636
flushEvent *eventFd
37+
cleanup runtime.Cleanup
3738
}
3839

3940
func New() (_ *Poller, err error) {
@@ -75,7 +76,10 @@ func New() (_ *Poller, err error) {
7576
return nil, fmt.Errorf("add flush eventfd: %w", err)
7677
}
7778

78-
runtime.SetFinalizer(p, (*Poller).Close)
79+
p.cleanup = runtime.AddCleanup(p, func(epollFd int) {
80+
_ = unix.Close(epollFd)
81+
}, p.epollFd)
82+
7983
return p, nil
8084
}
8185

@@ -84,7 +88,7 @@ func New() (_ *Poller, err error) {
8488
// Interrupts any calls to Wait. Multiple calls to Close are valid, but subsequent
8589
// calls will return os.ErrClosed.
8690
func (p *Poller) Close() error {
87-
runtime.SetFinalizer(p, nil)
91+
p.cleanup.Stop()
8892

8993
// Interrupt Wait() via the closeEvent fd if it's currently blocked.
9094
if err := p.wakeWaitForClose(); err != nil {

internal/sys/fd.go

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -21,23 +21,14 @@ const invalidFd = -1
2121
func newFD(value int) *FD {
2222
testmain.TraceFD(value, 1)
2323

24-
fd := &FD{value}
25-
runtime.SetFinalizer(fd, (*FD).finalize)
24+
fd := &FD{raw: value}
25+
fd.cleanup = runtime.AddCleanup(fd, func(raw int) {
26+
testmain.LeakFD(raw)
27+
_ = unix.Close(raw)
28+
}, fd.raw)
2629
return fd
2730
}
2831

29-
// finalize is set as the FD's runtime finalizer and
30-
// sends a leak trace before calling FD.Close().
31-
func (fd *FD) finalize() {
32-
if fd.raw == invalidFd {
33-
return
34-
}
35-
36-
testmain.LeakFD(fd.raw)
37-
38-
_ = fd.Close()
39-
}
40-
4132
func (fd *FD) String() string {
4233
return strconv.FormatInt(int64(fd.raw), 10)
4334
}
@@ -63,6 +54,6 @@ func (fd *FD) Disown() int {
6354
testmain.ForgetFD(value)
6455
fd.raw = invalidFd
6556

66-
runtime.SetFinalizer(fd, nil)
57+
fd.cleanup.Stop()
6758
return value
6859
}

internal/sys/fd_other.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,14 @@ package sys
55
import (
66
"fmt"
77
"os"
8+
"runtime"
89

910
"github.com/cilium/ebpf/internal/unix"
1011
)
1112

1213
type FD struct {
13-
raw int
14+
raw int
15+
cleanup runtime.Cleanup
1416
}
1517

1618
// NewFD wraps a raw fd with a finalizer.

internal/sys/fd_windows.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package sys
33
import (
44
"fmt"
55
"os"
6+
"runtime"
67

78
"github.com/cilium/ebpf/internal"
89
"github.com/cilium/ebpf/internal/efw"
@@ -12,7 +13,8 @@ import (
1213
//
1314
// It is not equivalent to a real file descriptor or handle.
1415
type FD struct {
15-
raw int
16+
raw int
17+
cleanup runtime.Cleanup
1618
}
1719

1820
// NewFD wraps a raw fd with a finalizer.

internal/tracefs/kprobe.go

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,8 @@ type Event struct {
199199
typ ProbeType
200200
group, name string
201201
// event id allocated by the kernel. 0 if the event has already been removed.
202-
id uint64
202+
id uint64
203+
cleanup runtime.Cleanup
203204
}
204205

205206
// NewEvent creates a new ephemeral trace event.
@@ -316,8 +317,17 @@ func NewEvent(args ProbeArgs) (*Event, error) {
316317
return nil, fmt.Errorf("get trace event id: %w", err)
317318
}
318319

319-
evt := &Event{args.Type, args.Group, eventName, tid}
320-
runtime.SetFinalizer(evt, (*Event).Close)
320+
evt := &Event{typ: args.Type, group: args.Group, name: eventName, id: tid}
321+
322+
type cleanupData struct {
323+
typ ProbeType
324+
pe string
325+
}
326+
327+
evt.cleanup = runtime.AddCleanup(evt, func(cleanupData cleanupData) {
328+
_ = removeEvent(cleanupData.typ, pe)
329+
}, cleanupData{args.Type, fmt.Sprintf("%s/%s", evt.group, evt.name)})
330+
321331
return evt, nil
322332
}
323333

@@ -330,7 +340,7 @@ func (evt *Event) Close() error {
330340
}
331341

332342
evt.id = 0
333-
runtime.SetFinalizer(evt, nil)
343+
evt.cleanup.Stop()
334344
pe := fmt.Sprintf("%s/%s", evt.group, evt.name)
335345
return removeEvent(evt.typ, pe)
336346
}

perf/ring.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ type perfEventRing struct {
2222
cpu int
2323
mmap []byte
2424
ringReader
25+
cleanup runtime.Cleanup
2526
}
2627

2728
func newPerfEventRing(cpu, perCPUBuffer int, opts ReaderOptions) (_ *sys.FD, _ *perfEventRing, err error) {
@@ -73,7 +74,9 @@ func newPerfEventRing(cpu, perCPUBuffer int, opts ReaderOptions) (_ *sys.FD, _ *
7374
mmap: mmap,
7475
ringReader: reader,
7576
}
76-
runtime.SetFinalizer(ring, (*perfEventRing).Close)
77+
ring.cleanup = runtime.AddCleanup(ring, func(mmap []byte) {
78+
_ = unix.Munmap(mmap)
79+
}, ring.mmap)
7780

7881
return fd, ring, nil
7982
}
@@ -95,7 +98,7 @@ func perfBufferSize(perCPUBuffer int) int {
9598
}
9699

97100
func (ring *perfEventRing) Close() error {
98-
runtime.SetFinalizer(ring, nil)
101+
ring.cleanup.Stop()
99102
mmap := ring.mmap
100103
ring.mmap = nil
101104
return unix.Munmap(mmap)

ringbuf/ring.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ type ringbufEventRing struct {
1919
prod []byte
2020
cons []byte
2121
*ringReader
22+
cleanup runtime.Cleanup
2223
}
2324

2425
func newRingBufEventRing(mapFD, size int) (*ringbufEventRing, error) {
@@ -41,13 +42,16 @@ func newRingBufEventRing(mapFD, size int) (*ringbufEventRing, error) {
4142
cons: cons,
4243
ringReader: newRingReader(cons_pos, prod_pos, prod[os.Getpagesize():]),
4344
}
44-
runtime.SetFinalizer(ring, (*ringbufEventRing).Close)
45+
ring.cleanup = runtime.AddCleanup(ring, func(mappings struct{ prod, cons []byte }) {
46+
_ = unix.Munmap(mappings.prod)
47+
_ = unix.Munmap(mappings.cons)
48+
}, struct{ prod, cons []byte }{prod, cons})
4549

4650
return ring, nil
4751
}
4852

4953
func (ring *ringbufEventRing) Close() {
50-
runtime.SetFinalizer(ring, nil)
54+
ring.cleanup.Stop()
5155

5256
_ = unix.Munmap(ring.prod)
5357
_ = unix.Munmap(ring.cons)

0 commit comments

Comments
 (0)