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

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