Blame SOURCES/bcc-0.24.0-RHEL-libbpf-version-fixes.patch

243a81
From 8f2951559127ffb93c3e36d5fc9d870768826ae9 Mon Sep 17 00:00:00 2001
243a81
From: Jerome Marchand <jmarchan@redhat.com>
243a81
Date: Thu, 24 Mar 2022 16:08:17 +0100
243a81
Subject: [PATCH 2/2] RHEL: libbpf version fixes
243a81
243a81
Revert "bcc: Replace deprecated libbpf APIs" since the libbpf version
243a81
provided in RHEL 8 doesn't provide the new APIs.
243a81
243a81
Remove BPF_MAP_TYPE_BLOOM_FILTER from bps since the libbpf version in
243a81
RHEL 8, doesn't provide bloom filter map.
243a81
243a81
Rename btf__load_vmlinux_btf into libbpf_find_kernel_btf. The function
243a81
has been renamed upstream for naming consistency, but RHEL 8 libbpf
243a81
still uses the old name.
243a81
243a81
Add definition of struct bpf_core_relo.
243a81
---
243a81
 introspection/bps.c   |  1 -
243a81
 libbpf-tools/ksnoop.c |  2 +-
243a81
 src/cc/bcc_btf.cc     | 73 ++++++++++++++++++++++++++++++++++++-
243a81
 src/cc/libbpf.c       | 84 +++++++------------------------------------
243a81
 4 files changed, 85 insertions(+), 75 deletions(-)
243a81
243a81
diff --git a/introspection/bps.c b/introspection/bps.c
243a81
index 232b23d4..6ec02e6c 100644
243a81
--- a/introspection/bps.c
243a81
+++ b/introspection/bps.c
243a81
@@ -80,7 +80,6 @@ static const char * const map_type_strings[] = {
243a81
   [BPF_MAP_TYPE_RINGBUF] = "ringbuf",
243a81
   [BPF_MAP_TYPE_INODE_STORAGE] = "inode_storage",
243a81
   [BPF_MAP_TYPE_TASK_STORAGE] = "task_storage",
243a81
-  [BPF_MAP_TYPE_BLOOM_FILTER] = "bloom_filter",
243a81
 };
243a81
 
243a81
 #define ARRAY_SIZE(x) (sizeof(x) / sizeof(*(x)))
243a81
diff --git a/libbpf-tools/ksnoop.c b/libbpf-tools/ksnoop.c
243a81
index 69c58403..a6ea6107 100644
243a81
--- a/libbpf-tools/ksnoop.c
243a81
+++ b/libbpf-tools/ksnoop.c
243a81
@@ -347,7 +347,7 @@ static struct btf *get_btf(const char *name)
243a81
 		name && strlen(name) > 0 ? name : "vmlinux");
243a81
 
243a81
 	if (!vmlinux_btf) {
243a81
-		vmlinux_btf = btf__load_vmlinux_btf();
243a81
+		vmlinux_btf = libbpf_find_kernel_btf();
243a81
 		if (!vmlinux_btf) {
243a81
 			err = -errno;
243a81
 			p_err("No BTF, cannot determine type info: %s", strerror(-err));
243a81
diff --git a/src/cc/bcc_btf.cc b/src/cc/bcc_btf.cc
243a81
index 7f551ae8..c78ba823 100644
243a81
--- a/src/cc/bcc_btf.cc
243a81
+++ b/src/cc/bcc_btf.cc
243a81
@@ -170,6 +170,77 @@ static int btf_ext_setup_line_info(struct btf_ext *btf_ext)
243a81
         return btf_ext_setup_info(btf_ext, ¶m;;
243a81
 }
243a81
 
243a81
+/* bpf_core_relo_kind encodes which aspect of captured field/type/enum value
243a81
+ * has to be adjusted by relocations.
243a81
+ */
243a81
+enum bpf_core_relo_kind {
243a81
+	BPF_FIELD_BYTE_OFFSET = 0,	/* field byte offset */
243a81
+	BPF_FIELD_BYTE_SIZE = 1,	/* field size in bytes */
243a81
+	BPF_FIELD_EXISTS = 2,		/* field existence in target kernel */
243a81
+	BPF_FIELD_SIGNED = 3,		/* field signedness (0 - unsigned, 1 - signed) */
243a81
+	BPF_FIELD_LSHIFT_U64 = 4,	/* bitfield-specific left bitshift */
243a81
+	BPF_FIELD_RSHIFT_U64 = 5,	/* bitfield-specific right bitshift */
243a81
+	BPF_TYPE_ID_LOCAL = 6,		/* type ID in local BPF object */
243a81
+	BPF_TYPE_ID_TARGET = 7,		/* type ID in target kernel */
243a81
+	BPF_TYPE_EXISTS = 8,		/* type existence in target kernel */
243a81
+	BPF_TYPE_SIZE = 9,		/* type size in bytes */
243a81
+	BPF_ENUMVAL_EXISTS = 10,	/* enum value existence in target kernel */
243a81
+	BPF_ENUMVAL_VALUE = 11,		/* enum value integer value */
243a81
+};
243a81
+
243a81
+/* The minimum bpf_core_relo checked by the loader
243a81
+ *
243a81
+ * CO-RE relocation captures the following data:
243a81
+ * - insn_off - instruction offset (in bytes) within a BPF program that needs
243a81
+ *   its insn->imm field to be relocated with actual field info;
243a81
+ * - type_id - BTF type ID of the "root" (containing) entity of a relocatable
243a81
+ *   type or field;
243a81
+ * - access_str_off - offset into corresponding .BTF string section. String
243a81
+ *   interpretation depends on specific relocation kind:
243a81
+ *     - for field-based relocations, string encodes an accessed field using
243a81
+ *     a sequence of field and array indices, separated by colon (:). It's
243a81
+ *     conceptually very close to LLVM's getelementptr ([0]) instruction's
243a81
+ *     arguments for identifying offset to a field.
243a81
+ *     - for type-based relocations, strings is expected to be just "0";
243a81
+ *     - for enum value-based relocations, string contains an index of enum
243a81
+ *     value within its enum type;
243a81
+ *
243a81
+ * Example to provide a better feel.
243a81
+ *
243a81
+ *   struct sample {
243a81
+ *       int a;
243a81
+ *       struct {
243a81
+ *           int b[10];
243a81
+ *       };
243a81
+ *   };
243a81
+ *
243a81
+ *   struct sample *s = ...;
243a81
+ *   int x = &s->a;     // encoded as "0:0" (a is field #0)
243a81
+ *   int y = &s->b[5];  // encoded as "0:1:0:5" (anon struct is field #1, 
243a81
+ *                      // b is field #0 inside anon struct, accessing elem #5)
243a81
+ *   int z = &s[10]->b; // encoded as "10:1" (ptr is used as an array)
243a81
+ *
243a81
+ * type_id for all relocs in this example  will capture BTF type id of
243a81
+ * `struct sample`.
243a81
+ *
243a81
+ * Such relocation is emitted when using __builtin_preserve_access_index()
243a81
+ * Clang built-in, passing expression that captures field address, e.g.:
243a81
+ *
243a81
+ * bpf_probe_read(&dst, sizeof(dst),
243a81
+ *		  __builtin_preserve_access_index(&src->a.b.c));
243a81
+ *
243a81
+ * In this case Clang will emit field relocation recording necessary data to
243a81
+ * be able to find offset of embedded `a.b.c` field within `src` struct.
243a81
+ *
243a81
+ *   [0] https://llvm.org/docs/LangRef.html#getelementptr-instruction
243a81
+ */
243a81
+struct bpf_core_relo {
243a81
+	__u32   insn_off;
243a81
+	__u32   type_id;
243a81
+	__u32   access_str_off;
243a81
+	enum bpf_core_relo_kind kind;
243a81
+};
243a81
+
243a81
 static int btf_ext_setup_core_relos(struct btf_ext *btf_ext)
243a81
 {
243a81
         struct btf_ext_sec_setup_param param = {
243a81
@@ -597,7 +668,7 @@ int BTF::load(uint8_t *btf_sec, uintptr_t btf_sec_size,
243a81
     return -1;
243a81
   }
243a81
 
243a81
-  if (btf__load_into_kernel(btf)) {
243a81
+  if (btf__load(btf)) {
243a81
     btf__free(btf);
243a81
     warning("Loading .BTF section failed\n");
243a81
     return -1;
243a81
diff --git a/src/cc/libbpf.c b/src/cc/libbpf.c
243a81
index e6403299..7410ae1a 100644
243a81
--- a/src/cc/libbpf.c
243a81
+++ b/src/cc/libbpf.c
243a81
@@ -297,25 +297,6 @@ static uint64_t ptr_to_u64(void *ptr)
243a81
   return (uint64_t) (unsigned long) ptr;
243a81
 }
243a81
 
243a81
-static int libbpf_bpf_map_create(struct bpf_create_map_attr *create_attr)
243a81
-{
243a81
-  LIBBPF_OPTS(bpf_map_create_opts, p);
243a81
-
243a81
-  p.map_flags = create_attr->map_flags;
243a81
-  p.numa_node = create_attr->numa_node;
243a81
-  p.btf_fd = create_attr->btf_fd;
243a81
-  p.btf_key_type_id = create_attr->btf_key_type_id;
243a81
-  p.btf_value_type_id = create_attr->btf_value_type_id;
243a81
-  p.map_ifindex = create_attr->map_ifindex;
243a81
-  if (create_attr->map_type == BPF_MAP_TYPE_STRUCT_OPS)
243a81
-    p.btf_vmlinux_value_type_id = create_attr->btf_vmlinux_value_type_id;
243a81
-  else
243a81
-    p.inner_map_fd = create_attr->inner_map_fd;
243a81
-
243a81
-  return bpf_map_create(create_attr->map_type, create_attr->name, create_attr->key_size,
243a81
-                        create_attr->value_size, create_attr->max_entries, &p);
243a81
-}
243a81
-
243a81
 int bcc_create_map_xattr(struct bpf_create_map_attr *attr, bool allow_rlimit)
243a81
 {
243a81
   unsigned name_len = attr->name ? strlen(attr->name) : 0;
243a81
@@ -323,7 +304,7 @@ int bcc_create_map_xattr(struct bpf_create_map_attr *attr, bool allow_rlimit)
243a81
 
243a81
   memcpy(map_name, attr->name, min(name_len, BPF_OBJ_NAME_LEN - 1));
243a81
   attr->name = map_name;
243a81
-  int ret = libbpf_bpf_map_create(attr);
243a81
+  int ret = bpf_create_map_xattr(attr);
243a81
 
243a81
   if (ret < 0 && errno == EPERM) {
243a81
     if (!allow_rlimit)
243a81
@@ -335,7 +316,7 @@ int bcc_create_map_xattr(struct bpf_create_map_attr *attr, bool allow_rlimit)
243a81
       rl.rlim_max = RLIM_INFINITY;
243a81
       rl.rlim_cur = rl.rlim_max;
243a81
       if (setrlimit(RLIMIT_MEMLOCK, &rl) == 0)
243a81
-        ret = libbpf_bpf_map_create(attr);
243a81
+        ret = bpf_create_map_xattr(attr);
243a81
     }
243a81
   }
243a81
 
243a81
@@ -345,12 +326,12 @@ int bcc_create_map_xattr(struct bpf_create_map_attr *attr, bool allow_rlimit)
243a81
     attr->btf_fd = 0;
243a81
     attr->btf_key_type_id = 0;
243a81
     attr->btf_value_type_id = 0;
243a81
-    ret = libbpf_bpf_map_create(attr);
243a81
+    ret = bpf_create_map_xattr(attr);
243a81
   }
243a81
 
243a81
   if (ret < 0 && name_len && (errno == E2BIG || errno == EINVAL)) {
243a81
     map_name[0] = '\0';
243a81
-    ret = libbpf_bpf_map_create(attr);
243a81
+    ret = bpf_create_map_xattr(attr);
243a81
   }
243a81
 
243a81
   if (ret < 0 && errno == EPERM) {
243a81
@@ -363,7 +344,7 @@ int bcc_create_map_xattr(struct bpf_create_map_attr *attr, bool allow_rlimit)
243a81
       rl.rlim_max = RLIM_INFINITY;
243a81
       rl.rlim_cur = rl.rlim_max;
243a81
       if (setrlimit(RLIMIT_MEMLOCK, &rl) == 0)
243a81
-        ret = libbpf_bpf_map_create(attr);
243a81
+        ret = bpf_create_map_xattr(attr);
243a81
     }
243a81
   }
243a81
   return ret;
243a81
@@ -627,47 +608,6 @@ int bpf_prog_get_tag(int fd, unsigned long long *ptag)
243a81
   return 0;
243a81
 }
243a81
 
243a81
-static int libbpf_bpf_prog_load(const struct bpf_load_program_attr *load_attr,
243a81
-                                char *log_buf, size_t log_buf_sz)
243a81
-{
243a81
-  LIBBPF_OPTS(bpf_prog_load_opts, p);
243a81
-
243a81
-  if (!load_attr || !log_buf != !log_buf_sz) {
243a81
-    errno = EINVAL;
243a81
-    return -EINVAL;
243a81
-  }
243a81
-
243a81
-  p.expected_attach_type = load_attr->expected_attach_type;
243a81
-  switch (load_attr->prog_type) {
243a81
-  case BPF_PROG_TYPE_STRUCT_OPS:
243a81
-  case BPF_PROG_TYPE_LSM:
243a81
-    p.attach_btf_id = load_attr->attach_btf_id;
243a81
-    break;
243a81
-  case BPF_PROG_TYPE_TRACING:
243a81
-  case BPF_PROG_TYPE_EXT:
243a81
-    p.attach_btf_id = load_attr->attach_btf_id;
243a81
-    p.attach_prog_fd = load_attr->attach_prog_fd;
243a81
-    break;
243a81
-  default:
243a81
-    p.prog_ifindex = load_attr->prog_ifindex;
243a81
-    p.kern_version = load_attr->kern_version;
243a81
-  }
243a81
-  p.log_level = load_attr->log_level;
243a81
-  p.log_buf = log_buf;
243a81
-  p.log_size = log_buf_sz;
243a81
-  p.prog_btf_fd = load_attr->prog_btf_fd;
243a81
-  p.func_info_rec_size = load_attr->func_info_rec_size;
243a81
-  p.func_info_cnt = load_attr->func_info_cnt;
243a81
-  p.func_info = load_attr->func_info;
243a81
-  p.line_info_rec_size = load_attr->line_info_rec_size;
243a81
-  p.line_info_cnt = load_attr->line_info_cnt;
243a81
-  p.line_info = load_attr->line_info;
243a81
-  p.prog_flags = load_attr->prog_flags;
243a81
-
243a81
-  return bpf_prog_load(load_attr->prog_type, load_attr->name, load_attr->license,
243a81
-                       load_attr->insns, load_attr->insns_cnt, &p);
243a81
-}
243a81
-
243a81
 int bcc_prog_load_xattr(struct bpf_load_program_attr *attr, int prog_len,
243a81
                         char *log_buf, unsigned log_buf_size, bool allow_rlimit)
243a81
 {
243a81
@@ -750,7 +690,7 @@ int bcc_prog_load_xattr(struct bpf_load_program_attr *attr, int prog_len,
243a81
     attr->name = prog_name;
243a81
   }
243a81
 
243a81
-  ret = libbpf_bpf_prog_load(attr, attr_log_buf, attr_log_buf_size);
243a81
+  ret = bpf_load_program_xattr(attr, attr_log_buf, attr_log_buf_size);
243a81
 
243a81
   // func_info/line_info may not be supported in old kernels.
243a81
   if (ret < 0 && attr->func_info && errno == EINVAL) {
243a81
@@ -761,14 +701,14 @@ int bcc_prog_load_xattr(struct bpf_load_program_attr *attr, int prog_len,
243a81
     attr->line_info = NULL;
243a81
     attr->line_info_cnt = 0;
243a81
     attr->line_info_rec_size = 0;
243a81
-    ret = libbpf_bpf_prog_load(attr, attr_log_buf, attr_log_buf_size);
243a81
+    ret = bpf_load_program_xattr(attr, attr_log_buf, attr_log_buf_size);
243a81
   }
243a81
 
243a81
   // BPF object name is not supported on older Kernels.
243a81
   // If we failed due to this, clear the name and try again.
243a81
   if (ret < 0 && name_len && (errno == E2BIG || errno == EINVAL)) {
243a81
     prog_name[0] = '\0';
243a81
-    ret = libbpf_bpf_prog_load(attr, attr_log_buf, attr_log_buf_size);
243a81
+    ret = bpf_load_program_xattr(attr, attr_log_buf, attr_log_buf_size);
243a81
   }
243a81
 
243a81
   if (ret < 0 && errno == EPERM) {
243a81
@@ -787,7 +727,7 @@ int bcc_prog_load_xattr(struct bpf_load_program_attr *attr, int prog_len,
243a81
       rl.rlim_max = RLIM_INFINITY;
243a81
       rl.rlim_cur = rl.rlim_max;
243a81
       if (setrlimit(RLIMIT_MEMLOCK, &rl) == 0)
243a81
-        ret = libbpf_bpf_prog_load(attr, attr_log_buf, attr_log_buf_size);
243a81
+        ret = bpf_load_program_xattr(attr, attr_log_buf, attr_log_buf_size);
243a81
     }
243a81
   }
243a81
 
243a81
@@ -805,7 +745,7 @@ int bcc_prog_load_xattr(struct bpf_load_program_attr *attr, int prog_len,
243a81
       // If logging is not already enabled, enable it and do the syscall again.
243a81
       if (attr->log_level == 0) {
243a81
         attr->log_level = 1;
243a81
-        ret = libbpf_bpf_prog_load(attr, log_buf, log_buf_size);
243a81
+        ret = bpf_load_program_xattr(attr, log_buf, log_buf_size);
243a81
       }
243a81
       // Print the log message and return.
243a81
       bpf_print_hints(ret, log_buf);
243a81
@@ -829,7 +769,7 @@ int bcc_prog_load_xattr(struct bpf_load_program_attr *attr, int prog_len,
243a81
         goto return_result;
243a81
       }
243a81
       tmp_log_buf[0] = 0;
243a81
-      ret = libbpf_bpf_prog_load(attr, tmp_log_buf, tmp_log_buf_size);
243a81
+      ret = bpf_load_program_xattr(attr, tmp_log_buf, tmp_log_buf_size);
243a81
       if (ret < 0 && errno == ENOSPC) {
243a81
         // Temporary buffer size is not enough. Double it and try again.
243a81
         free(tmp_log_buf);
243a81
@@ -1369,7 +1309,7 @@ int kernel_struct_has_field(const char *struct_name, const char *field_name)
243a81
   struct btf *btf;
243a81
   int i, ret, btf_id;
243a81
 
243a81
-  btf = btf__load_vmlinux_btf();
243a81
+  btf = libbpf_find_kernel_btf();
243a81
   ret = libbpf_get_error(btf);
243a81
   if (ret)
243a81
     return -1;
243a81
-- 
243a81
2.35.3
243a81