Skip to content

Commit

Permalink
chore: invoke rlimit.RemoveMemLock only once in main thread (#686)
Browse files Browse the repository at this point in the history
# Description

We only need to invoke `rlimit.RemoveMemLock` once, no need to do it in
every plugin
## Related Issue

If this pull request is related to any issue, please mention it here.
Additionally, make sure that the issue is assigned to you before
submitting this pull request.

## Checklist

- [ ] I have read the [contributing
documentation](https://retina.sh/docs/contributing).
- [ ] I signed and signed-off the commits (`git commit -S -s ...`). See
[this
documentation](https://docs.github.com/en/authentication/managing-commit-signature-verification/about-commit-signature-verification)
on signing commits.
- [ ] I have correctly attributed the author(s) of the code.
- [ ] I have tested the changes locally.
- [ ] I have followed the project's style guidelines.
- [ ] I have updated the documentation, if necessary.
- [ ] I have added tests, if applicable.

## Screenshots (if applicable) or Testing Completed

All plugins are still able to initialize and run normally.
## Additional Notes

Add any additional notes or context about the pull request here.

---

Please refer to the [CONTRIBUTING.md](../CONTRIBUTING.md) file for more
information on how to contribute to this project.
  • Loading branch information
nddq authored Aug 30, 2024
1 parent 3abb947 commit 3438ceb
Show file tree
Hide file tree
Showing 8 changed files with 12 additions and 39 deletions.
6 changes: 6 additions & 0 deletions cmd/hubble/daemon_main_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import (
"github.com/cilium/cilium/pkg/option"
"github.com/cilium/cilium/pkg/promise"
"github.com/cilium/cilium/pkg/time"
"github.com/cilium/ebpf/rlimit"
"github.com/cilium/proxy/pkg/logging"
"github.com/microsoft/retina/internal/buildinfo"
"github.com/microsoft/retina/pkg/config"
Expand Down Expand Up @@ -300,6 +301,11 @@ func Execute(cobraCmd *cobra.Command, h *hive.Hive) {
initDaemonConfig(h.Viper())
initLogging()

// Allow the current process to lock memory for eBPF resources.
if err := rlimit.RemoveMemlock(); err != nil {
logger.Fatal("failed to remove memlock", zap.Error(err))
}

//nolint:gocritic // without granular commits this commented-out code may be lost
// initEnv(h.Viper())

Expand Down
6 changes: 6 additions & 0 deletions cmd/legacy/daemon.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
crmgr "sigs.k8s.io/controller-runtime/pkg/manager"
metricsserver "sigs.k8s.io/controller-runtime/pkg/metrics/server"

"github.com/cilium/ebpf/rlimit"
"github.com/go-logr/zapr"
retinav1alpha1 "github.com/microsoft/retina/crd/api/v1alpha1"
"github.com/microsoft/retina/internal/buildinfo"
Expand Down Expand Up @@ -132,6 +133,11 @@ func (d *Daemon) Start() error {
defer zl.Close()
mainLogger := zl.Named("main").Sugar()

// Allow the current process to lock memory for eBPF resources.
if err = rlimit.RemoveMemlock(); err != nil {
mainLogger.Fatal("failed to remove memlock", zap.Error(err))
}

metrics.InitializeMetrics()

mainLogger.Info(zap.String("data aggregation level", daemonConfig.DataAggregationLevel.String()))
Expand Down
6 changes: 0 additions & 6 deletions pkg/plugin/conntrack/conntrack_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"time"

"github.com/cilium/ebpf"
"github.com/cilium/ebpf/rlimit"
"github.com/microsoft/retina/internal/ktime"
"github.com/microsoft/retina/pkg/config"
"github.com/microsoft/retina/pkg/log"
Expand Down Expand Up @@ -37,11 +36,6 @@ func (ct *Conntrack) Run(ctx context.Context) error {
ct.l.Info("conntrack is disabled in low data aggregation level")
return nil
}
// Allow the current process to lock memory for eBPF resources.
if err := rlimit.RemoveMemlock(); err != nil {
ct.l.Error("RemoveMemlock failed", zap.Error(err))
return errors.Wrapf(err, "failed to remove memlock limit")
}

objs := &conntrackObjects{}
err := loadConntrackObjects(objs, &ebpf.CollectionOptions{
Expand Down
6 changes: 0 additions & 6 deletions pkg/plugin/dns/dns_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import (
"strings"

v1 "github.com/cilium/cilium/pkg/hubble/api/v1"
"github.com/cilium/ebpf/rlimit"
"github.com/inspektor-gadget/inspektor-gadget/pkg/gadgets/trace/dns/tracer"
"github.com/inspektor-gadget/inspektor-gadget/pkg/gadgets/trace/dns/types"
"github.com/inspektor-gadget/inspektor-gadget/pkg/utils/host"
Expand Down Expand Up @@ -46,11 +45,6 @@ func (d *dns) Compile(ctx context.Context) error {
}

func (d *dns) Init() error {
if err := rlimit.RemoveMemlock(); err != nil {
d.l.Error("RemoveMemLock failed:%w", zap.Error(err))
return err
}

// Create tracer. In this case no parameters are passed.
err := host.Init(host.Config{})
tracer, err := tracer.NewTracer()
Expand Down
7 changes: 0 additions & 7 deletions pkg/plugin/dropreason/dropreason_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import (
"github.com/cilium/ebpf"
"github.com/cilium/ebpf/link"
"github.com/cilium/ebpf/perf"
"github.com/cilium/ebpf/rlimit"
"github.com/microsoft/retina/internal/ktime"
kcfg "github.com/microsoft/retina/pkg/config"
"github.com/microsoft/retina/pkg/enricher"
Expand Down Expand Up @@ -118,12 +117,6 @@ func (dr *dropReason) Compile(ctx context.Context) error {

func (dr *dropReason) Init() error {
var err error

if err = rlimit.RemoveMemlock(); err != nil {
dr.l.Error("RemoveMemLock failed:%w", zap.Error(err))
return err
}

// Get the absolute path to this file during runtime.
dir, err := absPath()
if err != nil {
Expand Down
6 changes: 0 additions & 6 deletions pkg/plugin/packetforward/packetforward_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import (

hubblev1 "github.com/cilium/cilium/pkg/hubble/api/v1"
"github.com/cilium/ebpf"
"github.com/cilium/ebpf/rlimit"
"github.com/microsoft/retina/pkg/loader"
"github.com/microsoft/retina/pkg/log"
"github.com/microsoft/retina/pkg/metrics"
Expand Down Expand Up @@ -132,11 +131,6 @@ func (p *packetForward) Compile(ctx context.Context) error {
}

func (p *packetForward) Init() error {
if err := rlimit.RemoveMemlock(); err != nil {
p.l.Error("RemoveMemLock failed:%w", zap.Error(err))
return err
}

// Get the absolute path to this file during runtime.
dir, err := absPath()
if err != nil {
Expand Down
7 changes: 0 additions & 7 deletions pkg/plugin/packetparser/packetparser_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import (
v1 "github.com/cilium/cilium/pkg/hubble/api/v1"
"github.com/cilium/ebpf"
"github.com/cilium/ebpf/perf"
"github.com/cilium/ebpf/rlimit"
"github.com/florianl/go-tc"
helper "github.com/florianl/go-tc/core"
"github.com/microsoft/retina/internal/ktime"
Expand Down Expand Up @@ -118,12 +117,6 @@ func (p *packetParser) Init() error {
p.l.Warn("packet parser and latency plugin will not init because pod level is disabled")
return nil
}

if err := rlimit.RemoveMemlock(); err != nil {
p.l.Error("RemoveMemLock failed:%w", zap.Error(err))
return err
}

// Get the absolute path to this file during runtime.
dir, err := absPath()
if err != nil {
Expand Down
7 changes: 0 additions & 7 deletions pkg/plugin/tcpretrans/tcpretrans_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (
"strings"

v1 "github.com/cilium/cilium/pkg/hubble/api/v1"
"github.com/cilium/ebpf/rlimit"
gadgetcontext "github.com/inspektor-gadget/inspektor-gadget/pkg/gadget-context"
"github.com/inspektor-gadget/inspektor-gadget/pkg/gadgets/trace/tcpretrans/tracer"
"github.com/inspektor-gadget/inspektor-gadget/pkg/gadgets/trace/tcpretrans/types"
Expand Down Expand Up @@ -50,12 +49,6 @@ func (t *tcpretrans) Init() error {
t.l.Warn("tcpretrans will not init because pod level is disabled")
return nil
}

if err := rlimit.RemoveMemlock(); err != nil {
t.l.Error("RemoveMemLock failed", zap.Error(err))
return err
}

// Create tracer. In this case no parameters are passed.
if err := host.Init(host.Config{}); err != nil {
t.l.Error("failed to init host", zap.Error(err))
Expand Down

0 comments on commit 3438ceb

Please sign in to comment.