bpf: Rename __kptr_ref -> __kptr and __kptr -> __kptr_untrusted.
authorAlexei Starovoitov <ast@kernel.org>
Fri, 3 Mar 2023 04:14:41 +0000 (20:14 -0800)
committerDaniel Borkmann <daniel@iogearbox.net>
Fri, 3 Mar 2023 16:42:20 +0000 (17:42 +0100)
__kptr meant to store PTR_UNTRUSTED kernel pointers inside bpf maps.
The concept felt useful, but didn't get much traction,
since bpf_rdonly_cast() was added soon after and bpf programs received
a simpler way to access PTR_UNTRUSTED kernel pointers
without going through restrictive __kptr usage.

Rename __kptr_ref -> __kptr and __kptr -> __kptr_untrusted to indicate
its intended usage.
The main goal of __kptr_untrusted was to read/write such pointers
directly while bpf_kptr_xchg was a mechanism to access refcnted
kernel pointers. The next patch will allow RCU protected __kptr access
with direct read. At that point __kptr_untrusted will be deprecated.

Signed-off-by: Alexei Starovoitov <ast@kernel.org>
Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
Acked-by: David Vernet <void@manifault.com>
Link: https://lore.kernel.org/bpf/20230303041446.3630-2-alexei.starovoitov@gmail.com
14 files changed:
Documentation/bpf/bpf_design_QA.rst
Documentation/bpf/cpumasks.rst
Documentation/bpf/kfuncs.rst
kernel/bpf/btf.c
tools/lib/bpf/bpf_helpers.h
tools/testing/selftests/bpf/progs/cb_refs.c
tools/testing/selftests/bpf/progs/cgrp_kfunc_common.h
tools/testing/selftests/bpf/progs/cpumask_common.h
tools/testing/selftests/bpf/progs/jit_probe_mem.c
tools/testing/selftests/bpf/progs/lru_bug.c
tools/testing/selftests/bpf/progs/map_kptr.c
tools/testing/selftests/bpf/progs/map_kptr_fail.c
tools/testing/selftests/bpf/progs/task_kfunc_common.h
tools/testing/selftests/bpf/test_verifier.c

index bfff0e7e37c2f1b004661b66f1d0a2c11883e29f..38372a956d6515f8131eff6f9a41a1a0cfb5e9ff 100644 (file)
@@ -314,7 +314,7 @@ Q: What is the compatibility story for special BPF types in map values?
 Q: Users are allowed to embed bpf_spin_lock, bpf_timer fields in their BPF map
 values (when using BTF support for BPF maps). This allows to use helpers for
 such objects on these fields inside map values. Users are also allowed to embed
-pointers to some kernel types (with __kptr and __kptr_ref BTF tags). Will the
+pointers to some kernel types (with __kptr_untrusted and __kptr BTF tags). Will the
 kernel preserve backwards compatibility for these features?
 
 A: It depends. For bpf_spin_lock, bpf_timer: YES, for kptr and everything else:
@@ -324,7 +324,7 @@ For struct types that have been added already, like bpf_spin_lock and bpf_timer,
 the kernel will preserve backwards compatibility, as they are part of UAPI.
 
 For kptrs, they are also part of UAPI, but only with respect to the kptr
-mechanism. The types that you can use with a __kptr and __kptr_ref tagged
+mechanism. The types that you can use with a __kptr_untrusted and __kptr tagged
 pointer in your struct are NOT part of the UAPI contract. The supported types can
 and will change across kernel releases. However, operations like accessing kptr
 fields and bpf_kptr_xchg() helper will continue to be supported across kernel
index 24bef9cbbeeeaf1b2ff3349191d82a13b4016526..75344cd230e557ab737fbd2d941d747f2344a1b3 100644 (file)
@@ -51,7 +51,7 @@ For example:
 .. code-block:: c
 
         struct cpumask_map_value {
-                struct bpf_cpumask __kptr_ref * cpumask;
+                struct bpf_cpumask __kptr * cpumask;
         };
 
         struct array_map {
@@ -128,7 +128,7 @@ Here is an example of a ``struct bpf_cpumask *`` being retrieved from a map:
 
        /* struct containing the struct bpf_cpumask kptr which is stored in the map. */
        struct cpumasks_kfunc_map_value {
-               struct bpf_cpumask __kptr_ref * bpf_cpumask;
+               struct bpf_cpumask __kptr * bpf_cpumask;
        };
 
        /* The map containing struct cpumasks_kfunc_map_value entries. */
index 9d85bbc3b77137c55564fe2f449b88b65e99a651..b5d9b0d446bcef81a01cfd4bc15fcf583edd4e8b 100644 (file)
@@ -544,7 +544,7 @@ Here's an example of how it can be used:
 
        /* struct containing the struct task_struct kptr which is actually stored in the map. */
        struct __cgroups_kfunc_map_value {
-               struct cgroup __kptr_ref * cgroup;
+               struct cgroup __kptr * cgroup;
        };
 
        /* The map containing struct __cgroups_kfunc_map_value entries. */
index ef2d8969ed1f749431bd3e6b8346c9a068b53887..c5e1d6955491ac8d9a0f04c520ebfaf13985e97c 100644 (file)
@@ -3288,9 +3288,9 @@ static int btf_find_kptr(const struct btf *btf, const struct btf_type *t,
        /* Reject extra tags */
        if (btf_type_is_type_tag(btf_type_by_id(btf, t->type)))
                return -EINVAL;
-       if (!strcmp("kptr", __btf_name_by_offset(btf, t->name_off)))
+       if (!strcmp("kptr_untrusted", __btf_name_by_offset(btf, t->name_off)))
                type = BPF_KPTR_UNREF;
-       else if (!strcmp("kptr_ref", __btf_name_by_offset(btf, t->name_off)))
+       else if (!strcmp("kptr", __btf_name_by_offset(btf, t->name_off)))
                type = BPF_KPTR_REF;
        else
                return -EINVAL;
index 5ec1871acb2fcee02f471299d4ed8cc9000bd264..7d12d3e620cc8d635460756468ca24cfac0eeaf8 100644 (file)
@@ -174,8 +174,8 @@ enum libbpf_tristate {
 
 #define __kconfig __attribute__((section(".kconfig")))
 #define __ksym __attribute__((section(".ksyms")))
+#define __kptr_untrusted __attribute__((btf_type_tag("kptr_untrusted")))
 #define __kptr __attribute__((btf_type_tag("kptr")))
-#define __kptr_ref __attribute__((btf_type_tag("kptr_ref")))
 
 #ifndef ___bpf_concat
 #define ___bpf_concat(a, b) a ## b
index 7653df1bc7871bb069aaec98795f852a1ad3789f..ce96b33e38d668567c49c2113880a107154cb19d 100644 (file)
@@ -4,7 +4,7 @@
 #include <bpf/bpf_helpers.h>
 
 struct map_value {
-       struct prog_test_ref_kfunc __kptr_ref *ptr;
+       struct prog_test_ref_kfunc __kptr *ptr;
 };
 
 struct {
index 2f8de933b957dc11c1687cdb091bf4913059a858..d0b7cd0d09d773bfaedfdebf6f162958e558ce74 100644 (file)
@@ -10,7 +10,7 @@
 #include <bpf/bpf_tracing.h>
 
 struct __cgrps_kfunc_map_value {
-       struct cgroup __kptr_ref * cgrp;
+       struct cgroup __kptr * cgrp;
 };
 
 struct hash_map {
index ad34f3b602bee2f42496136b6c1fdca5e6b7386a..65e5496ca1b22e047c49f2f1b3ab2c40aff3a6ac 100644 (file)
@@ -10,7 +10,7 @@
 int err;
 
 struct __cpumask_map_value {
-       struct bpf_cpumask __kptr_ref * cpumask;
+       struct bpf_cpumask __kptr * cpumask;
 };
 
 struct array_map {
index 2d2e61470794b8d2c1f8c0baec2555fc502789c3..13f00ca2ed0a03f7dd2c1f2db623afcbe68c5f72 100644 (file)
@@ -4,7 +4,7 @@
 #include <bpf/bpf_tracing.h>
 #include <bpf/bpf_helpers.h>
 
-static struct prog_test_ref_kfunc __kptr_ref *v;
+static struct prog_test_ref_kfunc __kptr *v;
 long total_sum = -1;
 
 extern struct prog_test_ref_kfunc *bpf_kfunc_call_test_acquire(unsigned long *sp) __ksym;
index 687081a724b33325316e765b0a027508b15a0e1c..ad73029cb1e30e2b4ad3b883571deca9818048e0 100644 (file)
@@ -4,7 +4,7 @@
 #include <bpf/bpf_helpers.h>
 
 struct map_value {
-       struct task_struct __kptr *ptr;
+       struct task_struct __kptr_untrusted *ptr;
 };
 
 struct {
index a24d17bc17eb75dd586a404a77eb22c77d3815eb..3fe7cde4cbfd3071b8cadc4200c8761fc7142642 100644 (file)
@@ -4,8 +4,8 @@
 #include <bpf/bpf_helpers.h>
 
 struct map_value {
-       struct prog_test_ref_kfunc __kptr *unref_ptr;
-       struct prog_test_ref_kfunc __kptr_ref *ref_ptr;
+       struct prog_test_ref_kfunc __kptr_untrusted *unref_ptr;
+       struct prog_test_ref_kfunc __kptr *ref_ptr;
 };
 
 struct array_map {
index 760e41e1a63262ab7f7ce2c06793674cad8cf1ea..e19e2a5f38cfb3cab9b9ce6cdc775c21bc01c72c 100644 (file)
@@ -7,9 +7,9 @@
 
 struct map_value {
        char buf[8];
-       struct prog_test_ref_kfunc __kptr *unref_ptr;
-       struct prog_test_ref_kfunc __kptr_ref *ref_ptr;
-       struct prog_test_member __kptr_ref *ref_memb_ptr;
+       struct prog_test_ref_kfunc __kptr_untrusted *unref_ptr;
+       struct prog_test_ref_kfunc __kptr *ref_ptr;
+       struct prog_test_member __kptr *ref_memb_ptr;
 };
 
 struct array_map {
index c0ffd171743edb251cde593f250d8faf92a41c82..4c2a4b0e3a251f13cf74100122417e7897e4cd81 100644 (file)
@@ -10,7 +10,7 @@
 #include <bpf/bpf_tracing.h>
 
 struct __tasks_kfunc_map_value {
-       struct task_struct __kptr_ref * task;
+       struct task_struct __kptr * task;
 };
 
 struct hash_map {
index 8b9949bb833d74a1382e245571287dee4fcc17e3..49a70d9beb0bc5d664bf5c5f9db9396276031401 100644 (file)
@@ -699,13 +699,13 @@ static int create_cgroup_storage(bool percpu)
  *   struct bpf_timer t;
  * };
  * struct btf_ptr {
+ *   struct prog_test_ref_kfunc __kptr_untrusted *ptr;
  *   struct prog_test_ref_kfunc __kptr *ptr;
- *   struct prog_test_ref_kfunc __kptr_ref *ptr;
- *   struct prog_test_member __kptr_ref *ptr;
+ *   struct prog_test_member __kptr *ptr;
  * }
  */
 static const char btf_str_sec[] = "\0bpf_spin_lock\0val\0cnt\0l\0bpf_timer\0timer\0t"
-                                 "\0btf_ptr\0prog_test_ref_kfunc\0ptr\0kptr\0kptr_ref"
+                                 "\0btf_ptr\0prog_test_ref_kfunc\0ptr\0kptr\0kptr_untrusted"
                                  "\0prog_test_member";
 static __u32 btf_raw_types[] = {
        /* int */
@@ -724,20 +724,20 @@ static __u32 btf_raw_types[] = {
        BTF_MEMBER_ENC(41, 4, 0), /* struct bpf_timer t; */
        /* struct prog_test_ref_kfunc */                /* [6] */
        BTF_STRUCT_ENC(51, 0, 0),
-       BTF_STRUCT_ENC(89, 0, 0),                       /* [7] */
+       BTF_STRUCT_ENC(95, 0, 0),                       /* [7] */
+       /* type tag "kptr_untrusted" */
+       BTF_TYPE_TAG_ENC(80, 6),                        /* [8] */
        /* type tag "kptr" */
-       BTF_TYPE_TAG_ENC(75, 6),                        /* [8] */
-       /* type tag "kptr_ref" */
-       BTF_TYPE_TAG_ENC(80, 6),                        /* [9] */
-       BTF_TYPE_TAG_ENC(80, 7),                        /* [10] */
+       BTF_TYPE_TAG_ENC(75, 6),                        /* [9] */
+       BTF_TYPE_TAG_ENC(75, 7),                        /* [10] */
        BTF_PTR_ENC(8),                                 /* [11] */
        BTF_PTR_ENC(9),                                 /* [12] */
        BTF_PTR_ENC(10),                                /* [13] */
        /* struct btf_ptr */                            /* [14] */
        BTF_STRUCT_ENC(43, 3, 24),
-       BTF_MEMBER_ENC(71, 11, 0), /* struct prog_test_ref_kfunc __kptr *ptr; */
-       BTF_MEMBER_ENC(71, 12, 64), /* struct prog_test_ref_kfunc __kptr_ref *ptr; */
-       BTF_MEMBER_ENC(71, 13, 128), /* struct prog_test_member __kptr_ref *ptr; */
+       BTF_MEMBER_ENC(71, 11, 0), /* struct prog_test_ref_kfunc __kptr_untrusted *ptr; */
+       BTF_MEMBER_ENC(71, 12, 64), /* struct prog_test_ref_kfunc __kptr *ptr; */
+       BTF_MEMBER_ENC(71, 13, 128), /* struct prog_test_member __kptr *ptr; */
 };
 
 static char bpf_vlog[UINT_MAX >> 8];