| 1 | package rlimit |
| 2 | |
| 3 | import ( |
| 4 | "errors" |
| 5 | "fmt" |
| 6 | "sync" |
| 7 | |
| 8 | "github.com/cilium/ebpf/internal" |
| 9 | "github.com/cilium/ebpf/internal/sys" |
| 10 | "github.com/cilium/ebpf/internal/unix" |
| 11 | ) |
| 12 | |
| 13 | var ( |
| 14 | unsupportedMemcgAccounting = &internal.UnsupportedFeatureError{ |
| 15 | MinimumVersion: internal.Version{5, 11, 0}, |
| 16 | Name: "memcg-based accounting for BPF memory", |
| 17 | } |
| 18 | haveMemcgAccounting error |
| 19 | |
| 20 | rlimitMu sync.Mutex |
| 21 | ) |
| 22 | |
| 23 | func init() { |
| 24 | // We have to run this feature test at init, since it relies on changing |
| 25 | // RLIMIT_MEMLOCK. Doing so is not safe in a concurrent program. Instead, |
| 26 | // we rely on the initialization order guaranteed by the Go runtime to |
| 27 | // execute the test in a safe environment: |
| 28 | // |
| 29 | // the invocation of init functions happens in a single goroutine, |
| 30 | // sequentially, one package at a time. |
| 31 | // |
| 32 | // This is also the reason why RemoveMemlock is in its own package: |
| 33 | // we only want to run the initializer if RemoveMemlock is called |
| 34 | // from somewhere. |
| 35 | haveMemcgAccounting = detectMemcgAccounting() |
| 36 | } |
| 37 | |
| 38 | func detectMemcgAccounting() error { |
| 39 | // Retrieve the original limit to prevent lowering Max, since |
| 40 | // doing so is a permanent operation when running unprivileged. |
| 41 | var oldLimit unix.Rlimit |
| 42 | if err := unix.Prlimit(0, unix.RLIMIT_MEMLOCK, nil, &oldLimit); err != nil { |
| 43 | return fmt.Errorf("getting original memlock rlimit: %s", err) |
| 44 | } |
| 45 | |
| 46 | // Drop the current limit to zero, maintaining the old Max value. |
| 47 | // This is always permitted by the kernel for unprivileged users. |
| 48 | // Retrieve a new copy of the old limit tuple to minimize the chances |
| 49 | // of failing the restore operation below. |
| 50 | zeroLimit := unix.Rlimit{Cur: 0, Max: oldLimit.Max} |
| 51 | if err := unix.Prlimit(0, unix.RLIMIT_MEMLOCK, &zeroLimit, &oldLimit); err != nil { |
| 52 | return fmt.Errorf("lowering memlock rlimit: %s", err) |
| 53 | } |
| 54 | |
| 55 | attr := sys.MapCreateAttr{ |
| 56 | MapType: 2, /* Array */ |
| 57 | KeySize: 4, |
| 58 | ValueSize: 4, |
| 59 | MaxEntries: 1, |
| 60 | } |
| 61 | |
| 62 | // Creating a map allocates shared (and locked) memory that counts against |
| 63 | // the rlimit on pre-5.11 kernels, but against the memory cgroup budget on |
| 64 | // kernels 5.11 and over. If this call succeeds with the process' memlock |
| 65 | // rlimit set to 0, we can reasonably assume memcg accounting is supported. |
| 66 | fd, mapErr := sys.MapCreate(&attr) |
| 67 | |
| 68 | // Restore old limits regardless of what happened. |
| 69 | if err := unix.Prlimit(0, unix.RLIMIT_MEMLOCK, &oldLimit, nil); err != nil { |
| 70 | return fmt.Errorf("restoring old memlock rlimit: %s", err) |
| 71 | } |
| 72 | |
| 73 | // Map creation successful, memcg accounting supported. |
| 74 | if mapErr == nil { |
| 75 | fd.Close() |
| 76 | return nil |
| 77 | } |
| 78 | |
| 79 | if errors.Is(mapErr, sys.ErrTokenCapabilities) { |
| 80 | // If BPF tokens are in use and array maps are not permitted, assume memcg |
| 81 | // accounting is supported since tokens are a newer feature. |
| 82 | return nil |
| 83 | } |
| 84 | |
| 85 | // EPERM shows up when map creation would exceed the memory budget. |
| 86 | if errors.Is(mapErr, unix.EPERM) { |
| 87 | return unsupportedMemcgAccounting |
| 88 | } |
| 89 | |
| 90 | // This shouldn't happen really. |
| 91 | return fmt.Errorf("unexpected error detecting memory cgroup accounting: %s", mapErr) |
| 92 | } |
| 93 | |
| 94 | // RemoveMemlock removes the limit on the amount of memory the current |
| 95 | // process can lock into RAM, if necessary. |
| 96 | // |
| 97 | // This is not required to load eBPF resources on kernel versions 5.11+ |
| 98 | // due to the introduction of cgroup-based memory accounting. On such kernels |
| 99 | // the function is a no-op. |
| 100 | // |
| 101 | // Since the function may change global per-process limits it should be invoked |
| 102 | // at program start up, in main() or init(). |
| 103 | // |
| 104 | // This function exists as a convenience and should only be used when |
| 105 | // permanently raising RLIMIT_MEMLOCK to infinite is appropriate. Consider |
| 106 | // invoking prlimit(2) directly with a more reasonable limit if desired. |
| 107 | // |
| 108 | // Requires CAP_SYS_RESOURCE on kernels < 5.11. |
| 109 | func RemoveMemlock() error { |
| 110 | if haveMemcgAccounting == nil { |
| 111 | return nil |
| 112 | } |
| 113 | |
| 114 | if !errors.Is(haveMemcgAccounting, unsupportedMemcgAccounting) { |
| 115 | return haveMemcgAccounting |
| 116 | } |
| 117 | |
| 118 | rlimitMu.Lock() |
| 119 | defer rlimitMu.Unlock() |
| 120 | |
| 121 | // pid 0 affects the current process. Requires CAP_SYS_RESOURCE. |
| 122 | newLimit := unix.Rlimit{Cur: unix.RLIM_INFINITY, Max: unix.RLIM_INFINITY} |
| 123 | if err := unix.Prlimit(0, unix.RLIMIT_MEMLOCK, &newLimit, nil); err != nil { |
| 124 | return fmt.Errorf("failed to set memlock rlimit: %w", err) |
| 125 | } |
| 126 | |
| 127 | return nil |
| 128 | } |