]> git.ipfire.org Git - thirdparty/linux.git/commitdiff
bpf: allow using bpf_kptr_xchg even if the MEM_RCU flag is set
authorKaitao Cheng <chengkaitao@kylinos.cn>
Sat, 14 Feb 2026 12:40:41 +0000 (20:40 +0800)
committerAlexei Starovoitov <ast@kernel.org>
Tue, 24 Feb 2026 01:37:06 +0000 (17:37 -0800)
For the following scenario:
    struct tree_node {
struct bpf_refcount ref;
struct bpf_rb_node node;
struct node_data __kptr * node_data;
u64 key;
    };
This means node_data would have the type PTR_TO_BTF_ID | MEM_ALLOC |
NON_OWN_REF | MEM_RCU.

When traversing an rbtree using bpf_rbtree_left/right, if we need to
use bpf_kptr_xchg to read the __kptr pointer, we still need to follow
the remove-read-add sequence.

This patch allows us to use bpf_kptr_xchg to directly read the __kptr
pointer without any prior operations.

Signed-off-by: Kaitao Cheng <chengkaitao@kylinos.cn>
Signed-off-by: Feng Yang <yangfeng@kylinos.cn>
Link: https://lore.kernel.org/r/20260214124042.62229-5-pilgrimtao@gmail.com
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
kernel/bpf/verifier.c

index 7426dba5e5d1c15c752ae7aaac9869e03c911d28..1153a828ce8dfd76e2b1a3bf2a86262478fb4ebe 100644 (file)
@@ -9311,6 +9311,7 @@ static const struct bpf_reg_types kptr_xchg_dest_types = {
                PTR_TO_MAP_VALUE,
                PTR_TO_BTF_ID | MEM_ALLOC,
                PTR_TO_BTF_ID | MEM_ALLOC | NON_OWN_REF,
+               PTR_TO_BTF_ID | MEM_ALLOC | NON_OWN_REF | MEM_RCU,
        }
 };
 static const struct bpf_reg_types dynptr_types = {
@@ -9475,6 +9476,7 @@ found:
        case PTR_TO_BTF_ID | MEM_ALLOC:
        case PTR_TO_BTF_ID | MEM_PERCPU | MEM_ALLOC:
        case PTR_TO_BTF_ID | MEM_ALLOC | NON_OWN_REF:
+       case PTR_TO_BTF_ID | MEM_ALLOC | NON_OWN_REF | MEM_RCU:
                if (meta->func_id != BPF_FUNC_spin_lock && meta->func_id != BPF_FUNC_spin_unlock &&
                    meta->func_id != BPF_FUNC_kptr_xchg) {
                        verifier_bug(env, "unimplemented handling of MEM_ALLOC");