Repository: cilium/ebpf
Pull request #957 (2023-03-03 15:36:13)

Link to maintainers file


Patch:
diff --git a/MAINTAINERS.md b/MAINTAINERS.md
index 9c18e7e..a56a03e 100644
--- a/MAINTAINERS.md
+++ b/MAINTAINERS.md
@@ -1,8 +1,3 @@
 # Maintainers
 
- * [Lorenz Bauer] 
- * [Timo Beckers] (Isovalent)
-
-
-[Lorenz Bauer]: https://github.com/lmb
-[Timo Beckers]: https://github.com/ti-mo
+Maintainers can be found in the [Cilium Maintainers file](https://github.com/cilium/community/blob/main/roles/Maintainers.md)
diff --git a/collection.go b/collection.go
index 30cfe34..6a546c4 100644
--- a/collection.go
+++ b/collection.go
@@ -598,15 +598,11 @@ func resolveKconfig(m *MapSpec) error {
 	data := make([]byte, ds.Size)
 	for _, vsi := range ds.Vars {
 		v := vsi.Type.(*btf.Var)
-		s, err := btf.Sizeof(v.Type)
-		if err != nil {
-			return fmt.Errorf("variable %s: getting size: %w", v.Name, err)
-		}
 
 		switch n := v.TypeName(); n {
 		case "LINUX_KERNEL_VERSION":
-			if s != 4 {
-				return fmt.Errorf("variable %s must be u32, got %d", n, s)
+			if integer, ok := v.Type.(*btf.Int); !ok || integer.Size != 4 {
+				return fmt.Errorf("variable %s must be a 32 bits integer, got %s", n, v.Type)
 			}
 
 			kv, err := internal.KernelVersion()
diff --git a/internal/pinning.go b/internal/pinning.go
index c4afb0f..01d892f 100644
--- a/internal/pinning.go
+++ b/internal/pinning.go
@@ -23,7 +23,7 @@ func Pin(currentPath, newPath string, fd *sys.FD) error {
 	if err != nil {
 		return err
 	}
-	if fsType != BpfFSType {
+	if fsType != unix.BPF_FS_MAGIC {
 		return fmt.Errorf("%s is not on a bpf filesystem", newPath)
 	}
 
diff --git a/internal/statfs.go b/internal/statfs.go
index c45f0b3..44c02d6 100644
--- a/internal/statfs.go
+++ b/internal/statfs.go
@@ -6,12 +6,6 @@ import (
 	"github.com/cilium/ebpf/internal/unix"
 )
 
-const (
-	BpfFSType   = 0xcafe4a11
-	TraceFSType = 0x74726163
-	DebugFSType = 0x64626720
-)
-
 func FSType(path string) (int64, error) {
 	var statfs unix.Statfs_t
 	if err := unix.Statfs(path, &statfs); err != nil {
diff --git a/internal/unix/types_linux.go b/internal/unix/types_linux.go
index 924be2a..2f52e0e 100644
--- a/internal/unix/types_linux.go
+++ b/internal/unix/types_linux.go
@@ -80,6 +80,8 @@ const (
 	SIG_UNBLOCK               = linux.SIG_UNBLOCK
 	EM_NONE                   = linux.EM_NONE
 	EM_BPF                    = linux.EM_BPF
+	BPF_FS_MAGIC              = linux.BPF_FS_MAGIC
+	TRACEFS_MAGIC             = linux.TRACEFS_MAGIC
 )
 
 type Statfs_t = linux.Statfs_t
diff --git a/internal/unix/types_other.go b/internal/unix/types_other.go
index 225fcd0..a51d33c 100644
--- a/internal/unix/types_other.go
+++ b/internal/unix/types_other.go
@@ -84,6 +84,8 @@ const (
 	SIG_UNBLOCK
 	EM_NONE
 	EM_BPF
+	BPF_FS_MAGIC
+	TRACEFS_MAGIC
 )
 
 type Statfs_t struct {
diff --git a/link/kprobe.go b/link/kprobe.go
index 781c100..04ba17c 100644
--- a/link/kprobe.go
+++ b/link/kprobe.go
@@ -91,6 +91,10 @@ func (pt probeType) PerfEventType(ret bool) perfEventType {
 // Losing the reference to the resulting Link (kp) will close the Kprobe
 // and prevent further execution of prog. The Link must be Closed during
 // program shutdown to avoid leaking system resources.
+//
+// If attaching to symbol fails, automatically retries with the running
+// platform's syscall prefix (e.g. __x64_) to support attaching to syscalls
+// in a portable fashion.
 func Kprobe(symbol string, prog *ebpf.Program, opts *KprobeOptions) (Link, error) {
 	k, err := kprobe(symbol, prog, opts, false)
 	if err != nil {
@@ -116,6 +120,10 @@ func Kprobe(symbol string, prog *ebpf.Program, opts *KprobeOptions) (Link, error
 // and prevent further execution of prog. The Link must be Closed during
 // program shutdown to avoid leaking system resources.
 //
+// If attaching to symbol fails, automatically retries with the running
+// platform's syscall prefix (e.g. __x64_) to support attaching to syscalls
+// in a portable fashion.
+//
 // On kernels 5.10 and earlier, setting a kretprobe on a nonexistent symbol
 // incorrectly returns unix.EINVAL instead of os.ErrNotExist.
 func Kretprobe(symbol string, prog *ebpf.Program, opts *KprobeOptions) (Link, error) {
@@ -199,7 +207,7 @@ func kprobe(symbol string, prog *ebpf.Program, opts *KprobeOptions, ret bool) (*
 		return tp, nil
 	}
 	if err != nil && !errors.Is(err, ErrNotSupported) {
-		return nil, fmt.Errorf("creating perf_kprobe PMU: %w", err)
+		return nil, fmt.Errorf("creating perf_kprobe PMU (arch-specific fallback for %q): %w", symbol, err)
 	}
 
 	// Use tracefs if kprobe PMU is missing.
@@ -210,7 +218,7 @@ func kprobe(symbol string, prog *ebpf.Program, opts *KprobeOptions, ret bool) (*
 		tp, err = tracefsKprobe(args)
 	}
 	if err != nil {
-		return nil, fmt.Errorf("creating trace event '%s' in tracefs: %w", symbol, err)
+		return nil, fmt.Errorf("creating tracefs event (arch-specific fallback for %q): %w", symbol, err)
 	}
 
 	return tp, nil
diff --git a/link/perf_event.go b/link/perf_event.go
index 28f2dab..9430f26 100644
--- a/link/perf_event.go
+++ b/link/perf_event.go
@@ -448,8 +448,8 @@ var getTracefsPath = internal.Memoize(func() (string, error) {
 		path   string
 		fsType int64
 	}{
-		{"/sys/kernel/tracing", internal.TraceFSType},
-		{"/sys/kernel/debug/tracing", internal.DebugFSType},
+		{"/sys/kernel/tracing", unix.TRACEFS_MAGIC},
+		{"/sys/kernel/debug/tracing", unix.TRACEFS_MAGIC},
 	} {
 		if fsType, err := internal.FSType(p.path); err == nil && fsType == p.fsType {
 			return p.path, nil


Test patch:
diff --git a/internal/statfs_test.go b/internal/statfs_test.go
new file mode 100644
index 0000000..6d795cd
--- /dev/null
+++ b/internal/statfs_test.go
@@ -0,0 +1,23 @@
+package internal
+
+import (
+	"testing"
+
+	"github.com/cilium/ebpf/internal/unix"
+
+	qt "github.com/frankban/quicktest"
+)
+
+func TestFSType(t *testing.T) {
+	for _, fs := range []struct {
+		path  string
+		magic int64
+	}{
+		{"/sys/kernel/tracing", unix.TRACEFS_MAGIC},
+		{"/sys/fs/bpf", unix.BPF_FS_MAGIC},
+	} {
+		fst, err := FSType(fs.path)
+		qt.Assert(t, err, qt.IsNil)
+		qt.Assert(t, fst, qt.Equals, fs.magic)
+	}
+}
diff --git a/link/perf_event_test.go b/link/perf_event_test.go
index 0490388..b16ac78 100644
--- a/link/perf_event_test.go
+++ b/link/perf_event_test.go
@@ -57,6 +57,11 @@ func TestTraceValidID(t *testing.T) {
 	}
 }
 
+func TestGetTracefsPath(t *testing.T) {
+	_, err := getTracefsPath()
+	qt.Assert(t, err, qt.IsNil)
+}
+
 func TestHaveBPFLinkPerfEvent(t *testing.T) {
 	testutils.CheckFeatureTest(t, haveBPFLinkPerfEvent)
 }