xref: /linux/tools/testing/selftests/sched_ext/non_scx_kfunc_deny.bpf.c (revision 06bc7ff0a1e0f2b0102e1314e3527a7ec0997851)
1*5897ca15SCheng-Yang Chou /* SPDX-License-Identifier: GPL-2.0 */
2*5897ca15SCheng-Yang Chou /*
3*5897ca15SCheng-Yang Chou  * Verify that context-sensitive SCX kfuncs (even "unlocked" ones) are
4*5897ca15SCheng-Yang Chou  * restricted to only SCX struct_ops programs. Non-SCX struct_ops programs,
5*5897ca15SCheng-Yang Chou  * such as TCP congestion control programs, should be rejected by the BPF
6*5897ca15SCheng-Yang Chou  * verifier when attempting to call these kfuncs.
7*5897ca15SCheng-Yang Chou  *
8*5897ca15SCheng-Yang Chou  * Copyright (C) 2026 Ching-Chun (Jim) Huang <jserv@ccns.ncku.edu.tw>
9*5897ca15SCheng-Yang Chou  * Copyright (C) 2026 Cheng-Yang Chou <yphbchou0911@gmail.com>
10*5897ca15SCheng-Yang Chou  */
11*5897ca15SCheng-Yang Chou 
12*5897ca15SCheng-Yang Chou #include <vmlinux.h>
13*5897ca15SCheng-Yang Chou #include <bpf/bpf_helpers.h>
14*5897ca15SCheng-Yang Chou #include <bpf/bpf_tracing.h>
15*5897ca15SCheng-Yang Chou 
16*5897ca15SCheng-Yang Chou /* SCX kfunc from scx_kfunc_ids_any set */
17*5897ca15SCheng-Yang Chou void scx_bpf_kick_cpu(s32 cpu, u64 flags) __ksym;
18*5897ca15SCheng-Yang Chou 
19*5897ca15SCheng-Yang Chou SEC("struct_ops/ssthresh")
BPF_PROG(tcp_ca_ssthresh,struct sock * sk)20*5897ca15SCheng-Yang Chou __u32 BPF_PROG(tcp_ca_ssthresh, struct sock *sk)
21*5897ca15SCheng-Yang Chou {
22*5897ca15SCheng-Yang Chou 	/*
23*5897ca15SCheng-Yang Chou 	 * This call should be rejected by the verifier because this is a
24*5897ca15SCheng-Yang Chou 	 * TCP congestion control program (non-SCX struct_ops).
25*5897ca15SCheng-Yang Chou 	 */
26*5897ca15SCheng-Yang Chou 	scx_bpf_kick_cpu(0, 0);
27*5897ca15SCheng-Yang Chou 	return 2;
28*5897ca15SCheng-Yang Chou }
29*5897ca15SCheng-Yang Chou 
30*5897ca15SCheng-Yang Chou SEC("struct_ops/cong_avoid")
BPF_PROG(tcp_ca_cong_avoid,struct sock * sk,__u32 ack,__u32 acked)31*5897ca15SCheng-Yang Chou void BPF_PROG(tcp_ca_cong_avoid, struct sock *sk, __u32 ack, __u32 acked) {}
32*5897ca15SCheng-Yang Chou 
33*5897ca15SCheng-Yang Chou SEC("struct_ops/undo_cwnd")
BPF_PROG(tcp_ca_undo_cwnd,struct sock * sk)34*5897ca15SCheng-Yang Chou __u32 BPF_PROG(tcp_ca_undo_cwnd, struct sock *sk) { return 2; }
35*5897ca15SCheng-Yang Chou 
36*5897ca15SCheng-Yang Chou SEC(".struct_ops")
37*5897ca15SCheng-Yang Chou struct tcp_congestion_ops tcp_non_scx_ca = {
38*5897ca15SCheng-Yang Chou 	.ssthresh   = (void *)tcp_ca_ssthresh,
39*5897ca15SCheng-Yang Chou 	.cong_avoid = (void *)tcp_ca_cong_avoid,
40*5897ca15SCheng-Yang Chou 	.undo_cwnd  = (void *)tcp_ca_undo_cwnd,
41*5897ca15SCheng-Yang Chou 	.name       = "tcp_kfunc_deny",
42*5897ca15SCheng-Yang Chou };
43*5897ca15SCheng-Yang Chou 
44*5897ca15SCheng-Yang Chou char _license[] SEC("license") = "GPL";
45