From nobody Wed Apr 15 16:24:07 2026 Received: from mail-pf1-f177.google.com (mail-pf1-f177.google.com [209.85.210.177]) (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 0EF273B5823 for ; Wed, 4 Mar 2026 14:36:06 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.177 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1772634968; cv=none; b=g+Bav1OsNl8aH0HvFzMPK1qOvrZl+PmeZreA87WYOgKJWA7FRB05S+Gt+maJpKf/s7OeWPlomz6KH2FA2hN1VE27foOa/xb7lv5LKdC48UM/Ho/yb+WCcnui5d1eP5IQYBPZ8epV0QkkEx0TMNQkTvwgdBEYDSmUxFymA9920PM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1772634968; c=relaxed/simple; bh=BqZXOAJGIKKd92V/RAYQDxNC6AlkoRpcSBFvvTkyrIw=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=Lo/mi2AtT8StmdpdwjDOMp1n+IyXtFh3B5N+KByU1lzSMS/vStbxNTc9XjHdqnbuS49jlVO72VjDMhTg/2N3Cv0Ta+WpEInEHL+IoYyos4FWj0rS94L3xchtNBKRSyDI/r5zguOwNFuL31ddFru5yIG/dK6mGuhokVaVHrLEs8w= 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=YFT0B1eC; arc=none smtp.client-ip=209.85.210.177 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="YFT0B1eC" Received: by mail-pf1-f177.google.com with SMTP id d2e1a72fcca58-824c9da9928so4182624b3a.3 for ; Wed, 04 Mar 2026 06:36:06 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1772634966; x=1773239766; 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=dc9SHoQTccjHJ9ny8JNU6itOYKrSyJ60RaW6+NKZF+0=; b=YFT0B1eCFTamUd2fHrTmd82K0Hg2jWgof/dUlkj1aGV9skjBGMqkGCuXJHSTngo7z6 a+wrR78YQdIeLBJuK03VNpd3nXYZZwf2pnwyMOLCb/TspsdtphpCh6C6sJhyDlizWuHX XM95FTocNPIQ5J3KPsoKzjTAMLpVbhdNKa4KzvLKi7d77B8nLlxc8O9yrbtQLnyCUgKj 8cGmP4WRTxYYbRNfnkSLCAGyCbsTye827oSsLu4VV4R+1VRn2BdKWefoFfJYVYkYVnJG 72DyQlebzp/Ye9Ds9q7CA2Iizmk1jQNBDCA+eoL41gNzADsaEcpAeDZkiEyZTozG/SUO ooaQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1772634966; x=1773239766; 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=dc9SHoQTccjHJ9ny8JNU6itOYKrSyJ60RaW6+NKZF+0=; b=WyqIBWHfFggnbeKrV8BmzjllEPvfs0ro/MyZUKDg2Qv5xr2WRAl2T3X0tLSPoEMkrI 4pL5bCkOBBGexUTyqLdDuPN0oZAIKavwqkCAsvTIm8MIVEVgWb0/H9D/Wq6ToG/o9Hav aPU4NqaDBbK8YqSeFJkq1jbUk8wBlQvR9PpmbRhbrA6DFwdU7gSujercmT0ZDxC4PPIG 8FCmH7wzYL8NfQ6SzzzeNautoq4oIqqYACUL1hyMTFHMcJe0jvlAbUWj2ahh+7bvdh+J Xe92Yoj4CqQa1OFET9eEi5c+761ILF/aH1ayMnkECxLHEj+VV2iMgu05ZJmER/pptBBA djAg== X-Forwarded-Encrypted: i=1; AJvYcCVKFn7g5UWjSS9fLyuX8MhxSYopF1Uafebq6CZo6WEVvNKQ5wgmKM4qICoDsLQs0jTr59nZxxxmu4R+RnE=@vger.kernel.org X-Gm-Message-State: AOJu0YzRdzlTyJy9JJBP8SLKbyis2bc3KkX8Ng/1V6fmDRQANqMwILT4 7ao356VsTKGyZLmd/LfWIkiLzQ8vjss5DeDPpqNw1XfrlKCwRYESDr2I X-Gm-Gg: ATEYQzwwOswnFKq1DLbIN8zVKEvPfzq/j2wfxOlyKpLq31c7h98dlz8bJxscA3fDn51 14HuiHTRi9YA7EBfSsLmIxJ0DuxtrQcaxvi11FJ8SXz/nGkw4Sfyi4pZ7n9CKE+nsW5BLrLA9DS /9G1lWtMzG/fhnHiS22aBAllYado2Xbg7/H3JJ3gBf/U6mkrLPwT/SmFOKlkEo3u1qHiRkjfXu1 /1wPYmXLy4aFqOYKcOjAuyCOUWk5fqUvm1DLPTADk4mRJYVPi3m+60+jVxoPYhEBQeoe2vnUlXP pWawurkRV3EGkNXBd3P20jNQTswHfw1tyzoSCdrZv6CAKNlIuDlTurKdxc3rngKI3tz2cn0gvyd OaiaKUtwYZQ7g9WIqYS4A/6GTdFnZsfNzkMn10GdQ4x7LtYAHl5ryUr13IsfCiMqFPZyVqpUr3v gwUZU3ZRN6rowPvngTy9EOzO+5dZIsuuPnM9/HUmy5ise2YT5CStYiog== X-Received: by 2002:a05:6a00:218b:b0:824:9451:c1e9 with SMTP id d2e1a72fcca58-82972df1b4amr2144267b3a.62.1772634966159; Wed, 04 Mar 2026 06:36:06 -0800 (PST) Received: from localhost.localdomain ([113.218.252.150]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-82739d87e4csm18470184b3a.15.2026.03.04.06.35.58 (version=TLS1_3 cipher=TLS_CHACHA20_POLY1305_SHA256 bits=256/256); Wed, 04 Mar 2026 06:36:05 -0800 (PST) From: Chengkaitao To: martin.lau@linux.dev, ast@kernel.org, daniel@iogearbox.net, andrii@kernel.org, eddyz87@gmail.com, song@kernel.org, yonghong.song@linux.dev, john.fastabend@gmail.com, kpsingh@kernel.org, sdf@fomichev.me, haoluo@google.com, jolsa@kernel.org, shuah@kernel.org, chengkaitao@kylinos.cn, linux-kselftest@vger.kernel.org Cc: bpf@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH v6 5/5] bpf/verifier: refactor kfunc checks to table-driven approach Date: Wed, 4 Mar 2026 22:34:59 +0800 Message-ID: <20260304143459.78059-6-pilgrimtao@gmail.com> X-Mailer: git-send-email 2.50.1 In-Reply-To: <20260304143459.78059-1-pilgrimtao@gmail.com> References: <20260304143459.78059-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-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: Kaitao Cheng Replace per-kfunc btf_id chains in list/rbtree/res_lock and graph node checks with btf_id_in_kfunc_table() and static kfunc tables for easier maintenance. Signed-off-by: Kaitao Cheng --- kernel/bpf/verifier.c | 93 +++++++++++++++++++++++++++++-------------- 1 file changed, 64 insertions(+), 29 deletions(-) diff --git a/kernel/bpf/verifier.c b/kernel/bpf/verifier.c index d133d18aa0cc..25961cf83a67 100644 --- a/kernel/bpf/verifier.c +++ b/kernel/bpf/verifier.c @@ -12600,6 +12600,60 @@ BTF_ID(func, bpf_session_is_return) BTF_ID(func, bpf_stream_vprintk) BTF_ID(func, bpf_stream_print_stack) =20 +static const enum special_kfunc_type bpf_list_api_kfuncs[] =3D { + KF_bpf_list_push_front_impl, + KF_bpf_list_push_back_impl, + KF_bpf_list_pop_front, + KF_bpf_list_pop_back, + KF_bpf_list_del, + KF_bpf_list_front, + KF_bpf_list_back, + KF_bpf_list_add_impl, + KF_bpf_list_node_is_edge, + KF_bpf_list_empty, +}; + +/* Kfuncs that take a list node argument (bpf_list_node *). */ +static const enum special_kfunc_type bpf_list_node_api_kfuncs[] =3D { + KF_bpf_list_push_front_impl, + KF_bpf_list_push_back_impl, + KF_bpf_list_del, + KF_bpf_list_add_impl, + KF_bpf_list_node_is_edge, +}; + +/* Kfuncs that take an rbtree node argument (bpf_rb_node *). */ +static const enum special_kfunc_type bpf_rbtree_node_api_kfuncs[] =3D { + KF_bpf_rbtree_remove, + KF_bpf_rbtree_add_impl, + KF_bpf_rbtree_left, + KF_bpf_rbtree_right, +}; + +static const enum special_kfunc_type bpf_rbtree_api_kfuncs[] =3D { + KF_bpf_rbtree_add_impl, + KF_bpf_rbtree_remove, + KF_bpf_rbtree_first, + KF_bpf_rbtree_root, + KF_bpf_rbtree_left, + KF_bpf_rbtree_right, +}; + +static const enum special_kfunc_type bpf_res_spin_lock_kfuncs[] =3D { + KF_bpf_res_spin_lock, + KF_bpf_res_spin_unlock, + KF_bpf_res_spin_lock_irqsave, + KF_bpf_res_spin_unlock_irqrestore, +}; + +static bool btf_id_in_kfunc_table(u32 btf_id, const enum special_kfunc_typ= e *kfuncs, int n) +{ + for (int i =3D 0; i < n; i++) + if (btf_id =3D=3D special_kfunc_list[kfuncs[i]]) + return true; + return false; +} + static bool is_task_work_add_kfunc(u32 func_id) { return func_id =3D=3D special_kfunc_list[KF_bpf_task_work_schedule_signal= ] || @@ -13000,26 +13054,14 @@ static int check_reg_allocation_locked(struct bpf= _verifier_env *env, struct bpf_ =20 static bool is_bpf_list_api_kfunc(u32 btf_id) { - return btf_id =3D=3D special_kfunc_list[KF_bpf_list_push_front_impl] || - btf_id =3D=3D special_kfunc_list[KF_bpf_list_push_back_impl] || - btf_id =3D=3D special_kfunc_list[KF_bpf_list_pop_front] || - btf_id =3D=3D special_kfunc_list[KF_bpf_list_pop_back] || - btf_id =3D=3D special_kfunc_list[KF_bpf_list_del] || - btf_id =3D=3D special_kfunc_list[KF_bpf_list_front] || - btf_id =3D=3D special_kfunc_list[KF_bpf_list_back] || - btf_id =3D=3D special_kfunc_list[KF_bpf_list_add_impl] || - btf_id =3D=3D special_kfunc_list[KF_bpf_list_node_is_edge] || - btf_id =3D=3D special_kfunc_list[KF_bpf_list_empty]; + return btf_id_in_kfunc_table(btf_id, bpf_list_api_kfuncs, + ARRAY_SIZE(bpf_list_api_kfuncs)); } =20 static bool is_bpf_rbtree_api_kfunc(u32 btf_id) { - return btf_id =3D=3D special_kfunc_list[KF_bpf_rbtree_add_impl] || - 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]; + return btf_id_in_kfunc_table(btf_id, bpf_rbtree_api_kfuncs, + ARRAY_SIZE(bpf_rbtree_api_kfuncs)); } =20 static bool is_bpf_iter_num_api_kfunc(u32 btf_id) @@ -13037,10 +13079,8 @@ static bool is_bpf_graph_api_kfunc(u32 btf_id) =20 static bool is_bpf_res_spin_lock_kfunc(u32 btf_id) { - return btf_id =3D=3D special_kfunc_list[KF_bpf_res_spin_lock] || - btf_id =3D=3D special_kfunc_list[KF_bpf_res_spin_unlock] || - btf_id =3D=3D special_kfunc_list[KF_bpf_res_spin_lock_irqsave] || - btf_id =3D=3D special_kfunc_list[KF_bpf_res_spin_unlock_irqrestore= ]; + return btf_id_in_kfunc_table(btf_id, bpf_res_spin_lock_kfuncs, + ARRAY_SIZE(bpf_res_spin_lock_kfuncs)); } =20 static bool is_bpf_arena_kfunc(u32 btf_id) @@ -13129,17 +13169,12 @@ static bool check_kfunc_is_graph_node_api(struct = bpf_verifier_env *env, =20 switch (node_field_type) { case BPF_LIST_NODE: - ret =3D (kfunc_btf_id =3D=3D special_kfunc_list[KF_bpf_list_push_front_i= mpl] || - kfunc_btf_id =3D=3D special_kfunc_list[KF_bpf_list_push_back_impl= ] || - kfunc_btf_id =3D=3D special_kfunc_list[KF_bpf_list_del] || - kfunc_btf_id =3D=3D special_kfunc_list[KF_bpf_list_add_impl] || - kfunc_btf_id =3D=3D special_kfunc_list[KF_bpf_list_node_is_edge]); + ret =3D btf_id_in_kfunc_table(kfunc_btf_id, bpf_list_node_api_kfuncs, + ARRAY_SIZE(bpf_list_node_api_kfuncs)); break; case BPF_RB_NODE: - ret =3D (kfunc_btf_id =3D=3D special_kfunc_list[KF_bpf_rbtree_remove] || - kfunc_btf_id =3D=3D special_kfunc_list[KF_bpf_rbtree_add_impl] || - 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_in_kfunc_table(kfunc_btf_id, bpf_rbtree_node_api_kfuncs, + ARRAY_SIZE(bpf_rbtree_node_api_kfuncs)); break; default: verbose(env, "verifier internal error: unexpected graph node argument ty= pe %s\n", --=20 2.50.1 (Apple Git-155)