sync with latest libbpf repo
authorYonghong Song <yhs@fb.com>
Mon, 20 Dec 2021 01:41:48 +0000 (17:41 -0800)
committeryonghong-song <ys114321@gmail.com>
Mon, 20 Dec 2021 03:15:20 +0000 (19:15 -0800)
sync upto the following commit:
  96268bf0c2b7 sync: latest libbpf changes from kernel

Signed-off-by: Yonghong Song <yhs@fb.com>
docs/kernel-versions.md
src/cc/compat/linux/virtual_bpf.h
src/cc/export/helpers.h
src/cc/libbpf
src/cc/libbpf.c

index 1b4334569331feec9056106997e120bae70dde1b..2c6422712e72c7837518b59bb81be14f4db44b5e 100644 (file)
@@ -230,7 +230,10 @@ Helper | Kernel version | License | Commit |
 `BPF_FUNC_get_current_task()` | 4.8 | GPL | [`606274c5abd8`](https://git.kernel.org/cgit/linux/kernel/git/torvalds/linux.git/commit/?id=606274c5abd8e245add01bc7145a8cbb92b69ba8)
 `BPF_FUNC_get_current_task_btf()` | 5.11 | GPL | [`3ca1032ab7ab`](https://github.com/torvalds/linux/commit/3ca1032ab7ab010eccb107aa515598788f7d93bb)
 `BPF_FUNC_get_current_uid_gid()` | 4.2 |  | [`ffeedafbf023`](https://git.kernel.org/cgit/linux/kernel/git/torvalds/linux.git/commit/?id=ffeedafbf0236f03aeb2e8db273b3e5ae5f5bc89)
+`BPF_FUNC_get_func_arg()` | 5.17 |  | [`f92c1e183604`](https://git.kernel.org/pub/scm/linux/kernel/git/bpf/bpf-next.git/commit?id=f92c1e183604c20ce00eb889315fdaa8f2d9e509)
+`BPF_FUNC_get_func_arg_cnt()` | 5.17 |  | [`f92c1e183604`](https://git.kernel.org/pub/scm/linux/kernel/git/bpf/bpf-next.git/commit?id=f92c1e183604c20ce00eb889315fdaa8f2d9e509)
 `BPF_FUNC_get_func_ip()` | 5.15 |  | [`5d8b583d04ae`](https://git.kernel.org/pub/scm/linux/kernel/git/bpf/bpf-next.git/commit/?id=5d8b583d04aedb3bd5f6d227a334c210c7d735f9)
+`BPF_FUNC_get_func_ret()` | 5.17 |  | [`f92c1e183604`](https://git.kernel.org/pub/scm/linux/kernel/git/bpf/bpf-next.git/commit?id=f92c1e183604c20ce00eb889315fdaa8f2d9e509)
 `BPF_FUNC_get_hash_recalc()` | 4.8 |  | [`13c5c240f789`](https://git.kernel.org/cgit/linux/kernel/git/torvalds/linux.git/commit/?id=13c5c240f789bbd2bcacb14a23771491485ae61f)
 `BPF_FUNC_get_listener_sock()` | 5.1 |  | [`dbafd7ddd623`](https://kernel.googlesource.com/pub/scm/linux/kernel/git/davem/net-next/+/dbafd7ddd62369b2f3926ab847cbf8fc40e800b7)
 `BPF_FUNC_get_local_storage()` | 4.19 |  | [`cd3394317653`](https://github.com/torvalds/linux/commit/cd3394317653837e2eb5c5d0904a8996102af9fc)
@@ -257,6 +260,7 @@ Helper | Kernel version | License | Commit |
 `BPF_FUNC_l3_csum_replace()` | 4.1 |  | [`91bc4822c3d6`](https://git.kernel.org/cgit/linux/kernel/git/torvalds/linux.git/commit/?id=91bc4822c3d61b9bb7ef66d3b77948a4f9177954)
 `BPF_FUNC_l4_csum_replace()` | 4.1 |  | [`91bc4822c3d6`](https://git.kernel.org/cgit/linux/kernel/git/torvalds/linux.git/commit/?id=91bc4822c3d61b9bb7ef66d3b77948a4f9177954)
 `BPF_FUNC_load_hdr_opt()` | 5.10 |  | [`0813a841566f`](https://git.kernel.org/pub/scm/linux/kernel/git/netdev/net-next.git/commit?id=0813a841566f0962a5551be7749b43c45f0022a0)
+`BPF_FUNC_loop()` | 5.17 |  | [`e6f2dd0f8067`](https://git.kernel.org/pub/scm/linux/kernel/git/bpf/bpf-next.git/commit?id=e6f2dd0f80674e9d5960337b3e9c2a242441b326)
 `BPF_FUNC_lwt_push_encap()` | 4.18 |  | [`fe94cc290f53`](https://git.kernel.org/cgit/linux/kernel/git/davem/net-next.git/commit/?id=fe94cc290f535709d3c5ebd1e472dfd0aec7ee79)
 `BPF_FUNC_lwt_seg6_action()` | 4.18 |  | [`fe94cc290f53`](https://git.kernel.org/cgit/linux/kernel/git/davem/net-next.git/commit/?id=fe94cc290f535709d3c5ebd1e472dfd0aec7ee79)
 `BPF_FUNC_lwt_seg6_adjust_srh()` | 4.18 |  | [`fe94cc290f53`](https://git.kernel.org/cgit/linux/kernel/git/davem/net-next.git/commit/?id=fe94cc290f535709d3c5ebd1e472dfd0aec7ee79)
@@ -357,6 +361,7 @@ Helper | Kernel version | License | Commit |
 `BPF_FUNC_spin_lock()` | 5.1 |  | [`d83525ca62cf`](https://git.kernel.org/pub/scm/linux/kernel/git/davem/net-next.git/commit/?id=d83525ca62cf8ebe3271d14c36fb900c294274a2)
 `BPF_FUNC_spin_unlock()` | 5.1 |  | [`d83525ca62cf`](https://git.kernel.org/pub/scm/linux/kernel/git/davem/net-next.git/commit/?id=d83525ca62cf8ebe3271d14c36fb900c294274a2)
 `BPF_FUNC_store_hdr_opt()` | 5.10 |  | [`0813a841566f`](https://git.kernel.org/pub/scm/linux/kernel/git/netdev/net-next.git/commit?id=0813a841566f0962a5551be7749b43c45f0022a0)
+`BPF_FUNC_strncmp()` | 5.17 |  | [`c5fb19937455`](https://git.kernel.org/pub/scm/linux/kernel/git/bpf/bpf-next.git/commit?id=c5fb19937455095573a19ddcbff32e993ed10e35)
 `BPF_FUNC_strtol()` | 5.2 |  | [`d7a4cb9b6705`](https://kernel.googlesource.com/pub/scm/linux/kernel/git/davem/net-next/+/d7a4cb9b6705a89937d12c8158a35a3145dc967a)
 `BPF_FUNC_strtoul()` | 5.2 |  | [`d7a4cb9b6705`](https://kernel.googlesource.com/pub/scm/linux/kernel/git/davem/net-next/+/d7a4cb9b6705a89937d12c8158a35a3145dc967a)
 `BPF_FUNC_sys_bpf()` | 5.14 |  | [`79a7f8bdb159`](https://github.com/torvalds/linux/commit/79a7f8bdb159d9914b58740f3d31d602a6e4aca8)
index 92f8da5a462a986d65a8cb057434ecb49d49853d..0f3a54732bb7e68214f1991a20a966cd1aaf67f7 100644 (file)
@@ -1343,8 +1343,10 @@ union bpf_attr {
                        /* or valid module BTF object fd or 0 to attach to vmlinux */
                        __u32           attach_btf_obj_fd;
                };
-               __u32           :32;            /* pad */
+               __u32           core_relo_cnt;  /* number of bpf_core_relo */
                __aligned_u64   fd_array;       /* array of FDs */
+               __aligned_u64   core_relos;
+               __u32           core_relo_rec_size; /* sizeof(struct bpf_core_relo) */
        };
 
        struct { /* anonymous struct used by BPF_OBJ_* commands */
@@ -4958,6 +4960,65 @@ union bpf_attr {
  *             **-ENOENT** if *task->mm* is NULL, or no vma contains *addr*.
  *             **-EBUSY** if failed to try lock mmap_lock.
  *             **-EINVAL** for invalid **flags**.
+ *
+ * long bpf_loop(u32 nr_loops, void *callback_fn, void *callback_ctx, u64 flags)
+ *     Description
+ *             For **nr_loops**, call **callback_fn** function
+ *             with **callback_ctx** as the context parameter.
+ *             The **callback_fn** should be a static function and
+ *             the **callback_ctx** should be a pointer to the stack.
+ *             The **flags** is used to control certain aspects of the helper.
+ *             Currently, the **flags** must be 0. Currently, nr_loops is
+ *             limited to 1 << 23 (~8 million) loops.
+ *
+ *             long (\*callback_fn)(u32 index, void \*ctx);
+ *
+ *             where **index** is the current index in the loop. The index
+ *             is zero-indexed.
+ *
+ *             If **callback_fn** returns 0, the helper will continue to the next
+ *             loop. If return value is 1, the helper will skip the rest of
+ *             the loops and return. Other return values are not used now,
+ *             and will be rejected by the verifier.
+ *
+ *     Return
+ *             The number of loops performed, **-EINVAL** for invalid **flags**,
+ *             **-E2BIG** if **nr_loops** exceeds the maximum number of loops.
+ *
+ * long bpf_strncmp(const char *s1, u32 s1_sz, const char *s2)
+ *     Description
+ *             Do strncmp() between **s1** and **s2**. **s1** doesn't need
+ *             to be null-terminated and **s1_sz** is the maximum storage
+ *             size of **s1**. **s2** must be a read-only string.
+ *     Return
+ *             An integer less than, equal to, or greater than zero
+ *             if the first **s1_sz** bytes of **s1** is found to be
+ *             less than, to match, or be greater than **s2**.
+ *
+ * long bpf_get_func_arg(void *ctx, u32 n, u64 *value)
+ *     Description
+ *             Get **n**-th argument (zero based) of the traced function (for tracing programs)
+ *             returned in **value**.
+ *
+ *     Return
+ *             0 on success.
+ *             **-EINVAL** if n >= arguments count of traced function.
+ *
+ * long bpf_get_func_ret(void *ctx, u64 *value)
+ *     Description
+ *             Get return value of the traced function (for tracing programs)
+ *             in **value**.
+ *
+ *     Return
+ *             0 on success.
+ *             **-EOPNOTSUPP** for tracing programs other than BPF_TRACE_FEXIT or BPF_MODIFY_RETURN.
+ *
+ * long bpf_get_func_arg_cnt(void *ctx)
+ *     Description
+ *             Get number of arguments of the traced function (for tracing programs).
+ *
+ *     Return
+ *             The number of arguments of the traced function.
  */
 #define __BPF_FUNC_MAPPER(FN)          \
        FN(unspec),                     \
@@ -5141,6 +5202,11 @@ union bpf_attr {
        FN(skc_to_unix_sock),           \
        FN(kallsyms_lookup_name),       \
        FN(find_vma),                   \
+       FN(loop),                       \
+       FN(strncmp),                    \
+       FN(get_func_arg),               \
+       FN(get_func_ret),               \
+       FN(get_func_arg_cnt),           \
        /* */
 
 /* integer value in 'imm' field of BPF_CALL instruction selects which helper
@@ -6350,5 +6416,79 @@ enum {
        BTF_F_ZERO      =       (1ULL << 3),
 };
 
+/* bpf_core_relo_kind encodes which aspect of captured field/type/enum value
+ * has to be adjusted by relocations. It is emitted by llvm and passed to
+ * libbpf and later to the kernel.
+ */
+enum bpf_core_relo_kind {
+       BPF_CORE_FIELD_BYTE_OFFSET = 0,      /* field byte offset */
+       BPF_CORE_FIELD_BYTE_SIZE = 1,        /* field size in bytes */
+       BPF_CORE_FIELD_EXISTS = 2,           /* field existence in target kernel */
+       BPF_CORE_FIELD_SIGNED = 3,           /* field signedness (0 - unsigned, 1 - signed) */
+       BPF_CORE_FIELD_LSHIFT_U64 = 4,       /* bitfield-specific left bitshift */
+       BPF_CORE_FIELD_RSHIFT_U64 = 5,       /* bitfield-specific right bitshift */
+       BPF_CORE_TYPE_ID_LOCAL = 6,          /* type ID in local BPF object */
+       BPF_CORE_TYPE_ID_TARGET = 7,         /* type ID in target kernel */
+       BPF_CORE_TYPE_EXISTS = 8,            /* type existence in target kernel */
+       BPF_CORE_TYPE_SIZE = 9,              /* type size in bytes */
+       BPF_CORE_ENUMVAL_EXISTS = 10,        /* enum value existence in target kernel */
+       BPF_CORE_ENUMVAL_VALUE = 11,         /* enum value integer value */
+};
+
+/*
+ * "struct bpf_core_relo" is used to pass relocation data form LLVM to libbpf
+ * and from libbpf to the kernel.
+ *
+ * CO-RE relocation captures the following data:
+ * - insn_off - instruction offset (in bytes) within a BPF program that needs
+ *   its insn->imm field to be relocated with actual field info;
+ * - type_id - BTF type ID of the "root" (containing) entity of a relocatable
+ *   type or field;
+ * - access_str_off - offset into corresponding .BTF string section. String
+ *   interpretation depends on specific relocation kind:
+ *     - for field-based relocations, string encodes an accessed field using
+ *       a sequence of field and array indices, separated by colon (:). It's
+ *       conceptually very close to LLVM's getelementptr ([0]) instruction's
+ *       arguments for identifying offset to a field.
+ *     - for type-based relocations, strings is expected to be just "0";
+ *     - for enum value-based relocations, string contains an index of enum
+ *       value within its enum type;
+ * - kind - one of enum bpf_core_relo_kind;
+ *
+ * Example:
+ *   struct sample {
+ *       int a;
+ *       struct {
+ *           int b[10];
+ *       };
+ *   };
+ *
+ *   struct sample *s = ...;
+ *   int *x = &s->a;     // encoded as "0:0" (a is field #0)
+ *   int *y = &s->b[5];  // encoded as "0:1:0:5" (anon struct is field #1,
+ *                       // b is field #0 inside anon struct, accessing elem #5)
+ *   int *z = &s[10]->b; // encoded as "10:1" (ptr is used as an array)
+ *
+ * type_id for all relocs in this example will capture BTF type id of
+ * `struct sample`.
+ *
+ * Such relocation is emitted when using __builtin_preserve_access_index()
+ * Clang built-in, passing expression that captures field address, e.g.:
+ *
+ * bpf_probe_read(&dst, sizeof(dst),
+ *               __builtin_preserve_access_index(&src->a.b.c));
+ *
+ * In this case Clang will emit field relocation recording necessary data to
+ * be able to find offset of embedded `a.b.c` field within `src` struct.
+ *
+ * [0] https://llvm.org/docs/LangRef.html#getelementptr-instruction
+ */
+struct bpf_core_relo {
+       __u32 insn_off;
+       __u32 type_id;
+       __u32 access_str_off;
+       enum bpf_core_relo_kind kind;
+};
+
 #endif /* _UAPI__LINUX_BPF_H__ */
 )********"
index 79c00b6144ec864e92be2b9a6be8e0dcf6eec2c6..91c2d35f8a9b1b09df12ee710587a1fbc7d91444 100644 (file)
@@ -920,6 +920,14 @@ static long (*bpf_kallsyms_lookup_name)(const char *name, int name_sz, int flags
 static long (*bpf_find_vma)(struct task_struct *task, __u64 addr, void *callback_fn,
                            void *callback_ctx, __u64 flags) =
   (void *)BPF_FUNC_find_vma;
+static long (*bpf_loop)(__u32 nr_loops, void *callback_fn, void *callback_ctx, __u64 flags) =
+  (void *)BPF_FUNC_loop;
+static long (*bpf_strncmp)(const char *s1, __u32 s1_sz, const char *s2) =
+  (void *)BPF_FUNC_strncmp;
+static long (*bpf_get_func_arg)(void *ctx, __u32 n, __u64 *value) =
+  (void *)BPF_FUNC_get_func_arg;
+static long (*bpf_get_func_ret)(void *ctx, __u64 *value) = (void *)BPF_FUNC_get_func_ret;
+static long (*bpf_get_func_arg_cnt)(void *ctx) = (void *)BPF_FUNC_get_func_arg_cnt;
 
 /* llvm builtin functions that eBPF C program may use to
  * emit BPF_LD_ABS and BPF_LD_IND instructions
index 93e89b34740c509406e948c78a404dd2fba67b8b..96268bf0c2b73b3ba91172a74179c2272870b8f8 160000 (submodule)
@@ -1 +1 @@
-Subproject commit 93e89b34740c509406e948c78a404dd2fba67b8b
+Subproject commit 96268bf0c2b73b3ba91172a74179c2272870b8f8
index 6434ec486e0f7f7f024dab7a0bd5f4a21893cbf8..d704cf120ca27da7db3106c3a7d227d22317ff0a 100644 (file)
@@ -285,6 +285,11 @@ static struct bpf_helper helpers[] = {
   {"skc_to_unix_sock", "5.16"},
   {"kallsyms_lookup_name", "5.16"},
   {"find_vma", "5.17"},
+  {"loop", "5.17"},
+  {"strncmp", "5.17"},
+  {"get_func_arg", "5.17"},
+  {"get_func_ret", "5.17"},
+  {"get_func_arg_cnt", "5.17"},
 };
 
 static uint64_t ptr_to_u64(void *ptr)