From nobody Tue Apr 7 14:04:18 2026 Received: from mail-pf1-f169.google.com (mail-pf1-f169.google.com [209.85.210.169]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 17A303D301D for ; Fri, 3 Apr 2026 17:09:34 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.169 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1775236176; cv=none; b=t4zOZx1p6FElCDeCSeHlS6fmcDpsGBCMwGf6vspGVEC4Ldg0mp4isTcL6k2IifCRNY6kb9rL/yiliZylXrdpnQVljwP1IZ8uAmh1+nAZRGvxN52GC19aiJV/mlGFcIyN0eiCarAYRkWmsHyABwJXvmNxdojsU20DdiYmswkYODg= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1775236176; c=relaxed/simple; bh=Lga53tOyWFUFw4YhT3N80GQCFhiyz4oS/A2d/gapydY=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=Tgqj0B1wv6VeZnHrRCtxwYe0rs4inTeE0PaETs7MfwdSs09nSbRMK9LOhx2iBN4xJXBoqQZPLW7LzqlVtoHH7Mf6gz98c64MClv1GLDdHaYhQP+LAVn0D7hERkryit0ENZgjykUum5p+Cs2dJRKljzc1VKyluabFu1xWIRXRwF4= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=kk1iodx4; arc=none smtp.client-ip=209.85.210.169 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="kk1iodx4" Received: by mail-pf1-f169.google.com with SMTP id d2e1a72fcca58-82ce0a9b3f7so937441b3a.0 for ; Fri, 03 Apr 2026 10:09:34 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1775236174; x=1775840974; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=sn6ir+iu8Ky2X/Dj3l/FSV5ByUGa4ggdFPo+/I8j8J4=; b=kk1iodx4+aBGpnWvHOVx1mPOxbU3UfRgx3u2R49of34EY6C11CjU2vbhHTUbUKGFqm THWR1RVln9zuFF34CTJiKEbx5uivrnuiLqh2gBhbkFB+cAwg8heoLZOQuqcYtcqZkjCf u81Q1Z1tkO2zAGSvuoFCD+UJ8TOYyB34ayFCmoqfcApbT0amsGxhZ1KZpER233ALb20Y w290P3tDXC4BQjnsPCgXlRA+wukaIg/CfX5izfUdYAffifxKS7s0ao4lWDHiBa3iT+9G BGu8LJTLaq/7fGGcn1JmYQS2uuqpgeYbO2cZHLcDCNRyGQAFZoxpRwZNJHE0CvAjYeYI TU/A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1775236174; x=1775840974; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to; bh=sn6ir+iu8Ky2X/Dj3l/FSV5ByUGa4ggdFPo+/I8j8J4=; b=EOSBM2SwEdZxzzo6czi9+a088xyCNXQTZwvOg9M8rJ5fp/DiViQNFXuqOwG9EYcAs8 Wk7FpI0R+yRsOCjE8iEB8YNU3J64Z8VQmw77z+AriOexSw455EcQvyc4kW7P+384eTIC tMRTokrN1KFQ2Abd4A2AsI0tHwLfbRKP5eIWgaUHuF4CfhdDCBEAq3+Fc25adc1PYidH 8V3VH1BGrxsY+RFjDwOtN0CIvxTlpJU8d9wyWAMasKR0Vhgd6oniPuwF1Ry1LMi55kdY hM8rau5Rl2uDIGQLlbSIfHtu2mwVzhxRve/BN1L+nGBSJNOKFeYx22i4k6UyUAKmSkK+ bsVQ== X-Gm-Message-State: AOJu0Yw0DU50314qrzOtaXIWRQkCh/NJ4/F8phamvczXzR0B5pK5Icrw NtL0j++feTW0f4Jz9WcLoYbzP6fkBggFe+C9kBFa9W5xYz1TYBGZcVno X-Gm-Gg: AeBDieuL2jW3TQvsm0XCKYrWr53LxWnZMRgCFQp9UIWXLi73JfheuqJMEUxHpmZ1pIB Wq3Nl3dgjovPG6w+rBLlXzlfAPN1LQ2KveV+QNohjtUNaxFePC5XKa4MwWMv/LDCNWDuJvv4B+U fQKMXrWo/o83gQQSRXNHDegWd7xnI3w0YNzqOzuH91qwPJpbK//6StW+stO+QQLwmWtqfvJrjMA frMEZfy+612T/OotAS44BZ8hKrSvU/zjZ5M97eRlVJFWa4omMSmRj2Kq1csx1mvlUNqVqwGTRQx aa3lD7muzBzN3Aoibd/Gm9WfrVjSD7icH1rIrY/E+lqjqc5a9aAIZb9RmA5ry/WippZIj2ugyQb ahEdb5r8MCJz6ZbrNXQyG150CPfINpI4mBGly1dzw+HQaNdB0Z7O9fR1e4Mls9YTgo6z8agjud3 PoM2YOlxBYwjPX+SqcaS8Wke3lGG1R4i4PRdgVeszT5nX1pq2+QbZn4GLJWeMe1MBZ X-Received: by 2002:a05:6a00:2393:b0:82c:dd31:b83d with SMTP id d2e1a72fcca58-82d0dba0775mr3675206b3a.38.1775236174310; Fri, 03 Apr 2026 10:09:34 -0700 (PDT) Received: from localhost.localdomain ([116.128.244.171]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-82cf9b3c930sm6588725b3a.17.2026.04.03.10.09.27 (version=TLS1_3 cipher=TLS_CHACHA20_POLY1305_SHA256 bits=256/256); Fri, 03 Apr 2026 10:09:33 -0700 (PDT) From: Chengkaitao To: arnd@arndb.de, ast@kernel.org, ihor.solodrai@linux.dev, daniel@iogearbox.net, andrii@kernel.org, martin.lau@linux.dev, eddyz87@gmail.com, memxor@gmail.com, song@kernel.org, yonghong.song@linux.dev, jolsa@kernel.org, john.fastabend@gmail.com, pengdonglin@xiaomi.com Cc: linux-kernel@vger.kernel.org, linux-arch@vger.kernel.org, bpf@vger.kernel.org, Kaitao Cheng Subject: [PATCH bpf-next 3/3] bpf: classify rbtree kfuncs with BPF_VERIF_KFUNC_DEF sets Date: Sat, 4 Apr 2026 01:08:57 +0800 Message-ID: <20260403170900.58659-4-pilgrimtao@gmail.com> X-Mailer: git-send-email 2.50.1 In-Reply-To: <20260403170900.58659-1-pilgrimtao@gmail.com> References: <20260403170900.58659-1-pilgrimtao@gmail.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable From: Kaitao Cheng Remove verifier logic that enumerated special_kfunc_list[KF_bpf_rbtree_*] and compared against it. Use BPF_VERIF_KFUNC_DEF so each kfunc's verifier-facing safety classification is declared in one place next to the implementation. When adding new kfuncs later, you no longer need to thread ad-hoc allowlists through scattered verifier branches=E2=80=94tagging the kfunc on= ce with BPF_VERIF_KFUNC_DEF is enough. This patch only migrates the rbtree kfuncs; other kfunc families can be converted the same way in follow-up work. Signed-off-by: Kaitao Cheng --- include/asm-generic/btf_ids.lds.h | 5 +++- kernel/bpf/helpers.c | 7 +++++ kernel/bpf/verifier.c | 44 +++++++++++-------------------- 3 files changed, 26 insertions(+), 30 deletions(-) diff --git a/include/asm-generic/btf_ids.lds.h b/include/asm-generic/btf_id= s.lds.h index 7579ba58f5ff..879391725d97 100644 --- a/include/asm-generic/btf_ids.lds.h +++ b/include/asm-generic/btf_ids.lds.h @@ -16,7 +16,10 @@ KEEP(*(.BTF_ids.##sfx)) \ __BTF_ids_seg_end_##sfx =3D .; =20 -#define BTF_IDS_VERIFIER_SUBSEGS +#define BTF_IDS_VERIFIER_SUBSEGS \ + BTF_IDS_SUBSEG(bpf_verif_kfunc_rbtree_add) \ + BTF_IDS_SUBSEG(bpf_verif_kfunc_rbtree_graph_node) \ + BTF_IDS_SUBSEG(bpf_verif_kfunc_rbtree_api) =20 #endif /* CONFIG_DEBUG_INFO_BTF */ =20 diff --git a/kernel/bpf/helpers.c b/kernel/bpf/helpers.c index 2d8538bf4cfa..d4a8b0b98210 100644 --- a/kernel/bpf/helpers.c +++ b/kernel/bpf/helpers.c @@ -2606,6 +2606,7 @@ __bpf_kfunc struct bpf_rb_node *bpf_rbtree_remove(str= uct bpf_rb_root *root, WRITE_ONCE(node_internal->owner, NULL); return (struct bpf_rb_node *)n; } +BPF_VERIF_KFUNC_DEF(bpf_rbtree_remove, rbtree_api, rbtree_graph_node) =20 /* Need to copy rbtree_add_cached's logic here because our 'less' is a BPF * program @@ -2667,6 +2668,7 @@ __bpf_kfunc int bpf_rbtree_add(struct bpf_rb_root *ro= ot, =20 return __bpf_rbtree_add(root, n, (void *)less, meta ? meta->record : NULL= , off); } +BPF_VERIF_KFUNC_DEF(bpf_rbtree_add, rbtree_api, rbtree_add, rbtree_graph_n= ode) =20 __bpf_kfunc int bpf_rbtree_add_impl(struct bpf_rb_root *root, struct bpf_r= b_node *node, bool (less)(struct bpf_rb_node *a, const struct bpf_rb_node *b), @@ -2674,6 +2676,7 @@ __bpf_kfunc int bpf_rbtree_add_impl(struct bpf_rb_roo= t *root, struct bpf_rb_node { return bpf_rbtree_add(root, node, less, meta__ign, off); } +BPF_VERIF_KFUNC_DEF(bpf_rbtree_add_impl, rbtree_api, rbtree_add, rbtree_gr= aph_node) =20 __bpf_kfunc struct bpf_rb_node *bpf_rbtree_first(struct bpf_rb_root *root) { @@ -2681,6 +2684,7 @@ __bpf_kfunc struct bpf_rb_node *bpf_rbtree_first(stru= ct bpf_rb_root *root) =20 return (struct bpf_rb_node *)rb_first_cached(r); } +BPF_VERIF_KFUNC_DEF(bpf_rbtree_first, rbtree_api) =20 __bpf_kfunc struct bpf_rb_node *bpf_rbtree_root(struct bpf_rb_root *root) { @@ -2688,6 +2692,7 @@ __bpf_kfunc struct bpf_rb_node *bpf_rbtree_root(struc= t bpf_rb_root *root) =20 return (struct bpf_rb_node *)r->rb_root.rb_node; } +BPF_VERIF_KFUNC_DEF(bpf_rbtree_root, rbtree_api) =20 __bpf_kfunc struct bpf_rb_node *bpf_rbtree_left(struct bpf_rb_root *root, = struct bpf_rb_node *node) { @@ -2698,6 +2703,7 @@ __bpf_kfunc struct bpf_rb_node *bpf_rbtree_left(struc= t bpf_rb_root *root, struct =20 return (struct bpf_rb_node *)node_internal->rb_node.rb_left; } +BPF_VERIF_KFUNC_DEF(bpf_rbtree_left, rbtree_api, rbtree_graph_node) =20 __bpf_kfunc struct bpf_rb_node *bpf_rbtree_right(struct bpf_rb_root *root,= struct bpf_rb_node *node) { @@ -2708,6 +2714,7 @@ __bpf_kfunc struct bpf_rb_node *bpf_rbtree_right(stru= ct bpf_rb_root *root, struc =20 return (struct bpf_rb_node *)node_internal->rb_node.rb_right; } +BPF_VERIF_KFUNC_DEF(bpf_rbtree_right, rbtree_api, rbtree_graph_node) =20 /** * bpf_task_acquire - Acquire a reference to a task. A task acquired by th= is diff --git a/kernel/bpf/verifier.c b/kernel/bpf/verifier.c index 8c1cf2eb6cbb..db9ba47903ef 100644 --- a/kernel/bpf/verifier.c +++ b/kernel/bpf/verifier.c @@ -12714,6 +12714,10 @@ BTF_ID(func, bpf_session_is_return) BTF_ID(func, bpf_stream_vprintk) BTF_ID(func, bpf_stream_print_stack) =20 +BTF_SET_SUB(bpf_verif_kfunc_rbtree_add) +BTF_SET_SUB(bpf_verif_kfunc_rbtree_graph_node) +BTF_SET_SUB(bpf_verif_kfunc_rbtree_api) + static bool is_bpf_obj_new_kfunc(u32 func_id) { return func_id =3D=3D special_kfunc_list[KF_bpf_obj_new] || @@ -12752,12 +12756,6 @@ static bool is_bpf_list_push_kfunc(u32 func_id) func_id =3D=3D special_kfunc_list[KF_bpf_list_push_back_impl]; } =20 -static bool is_bpf_rbtree_add_kfunc(u32 func_id) -{ - return func_id =3D=3D special_kfunc_list[KF_bpf_rbtree_add] || - func_id =3D=3D special_kfunc_list[KF_bpf_rbtree_add_impl]; -} - static bool is_task_work_add_kfunc(u32 func_id) { return func_id =3D=3D special_kfunc_list[KF_bpf_task_work_schedule_signal= ] || @@ -13162,16 +13160,6 @@ static bool is_bpf_list_api_kfunc(u32 btf_id) btf_id =3D=3D special_kfunc_list[KF_bpf_list_back]; } =20 -static bool is_bpf_rbtree_api_kfunc(u32 btf_id) -{ - return is_bpf_rbtree_add_kfunc(btf_id) || - btf_id =3D=3D special_kfunc_list[KF_bpf_rbtree_remove] || - btf_id =3D=3D special_kfunc_list[KF_bpf_rbtree_first] || - btf_id =3D=3D special_kfunc_list[KF_bpf_rbtree_root] || - btf_id =3D=3D special_kfunc_list[KF_bpf_rbtree_left] || - btf_id =3D=3D special_kfunc_list[KF_bpf_rbtree_right]; -} - static bool is_bpf_iter_num_api_kfunc(u32 btf_id) { return btf_id =3D=3D special_kfunc_list[KF_bpf_iter_num_new] || @@ -13182,7 +13170,7 @@ static bool is_bpf_iter_num_api_kfunc(u32 btf_id) static bool is_bpf_graph_api_kfunc(u32 btf_id) { return is_bpf_list_api_kfunc(btf_id) || - is_bpf_rbtree_api_kfunc(btf_id) || + btf_id_set_contains(&bpf_verif_kfunc_rbtree_api, btf_id) || is_bpf_refcount_acquire_kfunc(btf_id); } =20 @@ -13216,7 +13204,7 @@ static bool kfunc_spin_allowed(u32 btf_id) =20 static bool is_sync_callback_calling_kfunc(u32 btf_id) { - return is_bpf_rbtree_add_kfunc(btf_id); + return btf_id_set_contains(&bpf_verif_kfunc_rbtree_add, btf_id); } =20 static bool is_async_callback_calling_kfunc(u32 btf_id) @@ -13244,7 +13232,7 @@ static bool is_callback_calling_kfunc(u32 btf_id) =20 static bool is_rbtree_lock_required_kfunc(u32 btf_id) { - return is_bpf_rbtree_api_kfunc(btf_id); + return btf_id_set_contains(&bpf_verif_kfunc_rbtree_api, btf_id); } =20 static bool check_kfunc_is_graph_root_api(struct bpf_verifier_env *env, @@ -13258,7 +13246,7 @@ static bool check_kfunc_is_graph_root_api(struct bp= f_verifier_env *env, ret =3D is_bpf_list_api_kfunc(kfunc_btf_id); break; case BPF_RB_ROOT: - ret =3D is_bpf_rbtree_api_kfunc(kfunc_btf_id); + ret =3D btf_id_set_contains(&bpf_verif_kfunc_rbtree_api, kfunc_btf_id); break; default: verbose(env, "verifier internal error: unexpected graph root argument ty= pe %s\n", @@ -13283,10 +13271,7 @@ static bool check_kfunc_is_graph_node_api(struct b= pf_verifier_env *env, ret =3D is_bpf_list_push_kfunc(kfunc_btf_id); break; case BPF_RB_NODE: - ret =3D (is_bpf_rbtree_add_kfunc(kfunc_btf_id) || - kfunc_btf_id =3D=3D special_kfunc_list[KF_bpf_rbtree_remove] || - kfunc_btf_id =3D=3D special_kfunc_list[KF_bpf_rbtree_left] || - kfunc_btf_id =3D=3D special_kfunc_list[KF_bpf_rbtree_right]); + ret =3D btf_id_set_contains(&bpf_verif_kfunc_rbtree_graph_node, kfunc_bt= f_id); break; default: verbose(env, "verifier internal error: unexpected graph node argument ty= pe %s\n", @@ -13823,7 +13808,7 @@ static int check_kfunc_args(struct bpf_verifier_env= *env, struct bpf_kfunc_call_ return ret; break; case KF_ARG_PTR_TO_RB_NODE: - if (is_bpf_rbtree_add_kfunc(meta->func_id)) { + if (btf_id_set_contains(&bpf_verif_kfunc_rbtree_add, meta->func_id)) { if (reg->type !=3D (PTR_TO_BTF_ID | MEM_ALLOC)) { verbose(env, "arg#%d expected pointer to allocated object\n", i); return -EINVAL; @@ -14305,7 +14290,7 @@ static int check_kfunc_call(struct bpf_verifier_env= *env, struct bpf_insn *insn, if (err < 0) return err; =20 - if (is_bpf_rbtree_add_kfunc(meta.func_id)) { + if (btf_id_set_contains(&bpf_verif_kfunc_rbtree_add, meta.func_id)) { err =3D push_callback_call(env, insn, insn_idx, meta.subprogno, set_rbtree_add_callback_state); if (err) { @@ -14409,7 +14394,8 @@ static int check_kfunc_call(struct bpf_verifier_env= *env, struct bpf_insn *insn, return err; } =20 - if (is_bpf_list_push_kfunc(meta.func_id) || is_bpf_rbtree_add_kfunc(meta.= func_id)) { + if (is_bpf_list_push_kfunc(meta.func_id) || + btf_id_set_contains(&bpf_verif_kfunc_rbtree_add, meta.func_id)) { release_ref_obj_id =3D regs[BPF_REG_2].ref_obj_id; insn_aux->insert_off =3D regs[BPF_REG_2].var_off.value; insn_aux->kptr_struct_meta =3D btf_find_struct_meta(meta.arg_btf, meta.a= rg_btf_id); @@ -23438,13 +23424,13 @@ static int fixup_kfunc_call(struct bpf_verifier_e= nv *env, struct bpf_insn *insn, insn_buf[2] =3D *insn; *cnt =3D 3; } else if (is_bpf_list_push_kfunc(desc->func_id) || - is_bpf_rbtree_add_kfunc(desc->func_id)) { + btf_id_set_contains(&bpf_verif_kfunc_rbtree_add, desc->func_id)) { struct btf_struct_meta *kptr_struct_meta =3D env->insn_aux_data[insn_idx= ].kptr_struct_meta; int struct_meta_reg =3D BPF_REG_3; int node_offset_reg =3D BPF_REG_4; =20 /* rbtree_add has extra 'less' arg, so args-to-fixup are in diff regs */ - if (is_bpf_rbtree_add_kfunc(desc->func_id)) { + if (btf_id_set_contains(&bpf_verif_kfunc_rbtree_add, desc->func_id)) { struct_meta_reg =3D BPF_REG_4; node_offset_reg =3D BPF_REG_5; } --=20 2.43.0