Skip to content

Commit fb57b17

Browse files
authored
Remove Rlimit only when eBPF does not account memory to cgroup (#207)
squashing for signing
1 parent 37fa717 commit fb57b17

File tree

3 files changed

+10
-18
lines changed

3 files changed

+10
-18
lines changed

examples/mapspec_editor/main.go

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,8 @@ import (
55
_ "embed"
66
"fmt"
77
"log"
8-
"math"
98

109
"github.com/cilium/ebpf"
11-
"golang.org/x/sys/unix"
1210

1311
manager "github.com/DataDog/ebpf-manager"
1412
)
@@ -33,10 +31,7 @@ func run() error {
3331
EditorFlag: manager.EditMaxEntries | manager.EditType,
3432
},
3533
},
36-
RLimit: &unix.Rlimit{
37-
Cur: math.MaxUint64,
38-
Max: math.MaxUint64,
39-
},
34+
RemoveRlimit: true,
4035
}
4136

4237
if err := m.InitWithOptions(bytes.NewReader(Probe), options); err != nil {

manager.go

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import (
1414
"github.com/cilium/ebpf/asm"
1515
"github.com/cilium/ebpf/btf"
1616
"github.com/cilium/ebpf/features"
17-
"golang.org/x/sys/unix"
17+
"github.com/cilium/ebpf/rlimit"
1818
)
1919

2020
// FunctionExcluder - An interface for types that can be used for `AdditionalExcludedFunctionCollector`
@@ -104,9 +104,9 @@ type Options struct {
104104
// ProbeRetryDelay - Defines the delay to wait before a probe should retry to attach / detach on error.
105105
DefaultProbeRetryDelay time.Duration
106106

107-
// RLimit - The maps & programs provided to the manager might exceed the maximum allowed memory lock.
107+
// RemoveRlimit - The maps & programs provided to the manager might exceed the maximum allowed memory lock.
108108
// `RLIMIT_MEMLOCK` If a limit is provided here it will be applied when the manager is initialized.
109-
RLimit *unix.Rlimit
109+
RemoveRlimit bool
110110

111111
// KeepKernelBTF - Defines if the kernel types defined in VerifierOptions.Programs.KernelTypes and KernelModuleTypes should be cleaned up
112112
// once the manager is done using them. By default, the manager will clean them up to save up space. DISCLAIMER: if
@@ -487,9 +487,11 @@ func (m *Manager) InitWithOptions(elf io.ReaderAt, options Options) error {
487487
}
488488

489489
// set resource limit if requested
490-
if m.options.RLimit != nil {
491-
err := unix.Setrlimit(unix.RLIMIT_MEMLOCK, m.options.RLimit)
492-
if err != nil {
490+
if m.options.RemoveRlimit {
491+
// rlimit.RemoveMemlock automatically detects if rlimit based accounting
492+
// is used for eBPF maps. Only in this case is the rlimit removed.
493+
// If cgroup based accounting is in effect, then rlimit is not removed.
494+
if err := rlimit.RemoveMemlock(); err != nil {
493495
m.stateLock.Unlock()
494496
return fmt.Errorf("couldn't adjust RLIMIT_MEMLOCK: %w", err)
495497
}

manager_test.go

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,13 @@ import (
44
"bytes"
55
"errors"
66
"io"
7-
"math"
87
"os"
98
"strings"
109
"testing"
1110

1211
"github.com/cilium/ebpf"
1312
"github.com/cilium/ebpf/asm"
1413
"github.com/cilium/ebpf/rlimit"
15-
"golang.org/x/sys/unix"
1614
)
1715

1816
func TestVerifierError(t *testing.T) {
@@ -60,10 +58,7 @@ func TestExclude(t *testing.T) {
6058
},
6159
}
6260
opts := Options{
63-
RLimit: &unix.Rlimit{
64-
Cur: math.MaxUint64,
65-
Max: math.MaxUint64,
66-
},
61+
RemoveRlimit: true,
6762
ExcludedMaps: []string{"map_two"},
6863
}
6964

0 commit comments

Comments
 (0)