| Seq | Timestamp | Type | Name | Duration |
|---|---|---|---|---|
| 0/0 | 2026/06/23 18:13 | flow | patch-triage |
0mArgs:null Results: {
"EnableConfigs": [
"BPF_JIT",
"BPF_SYSCALL",
"CGROUP_BPF"
],
"FocusSymbols": [
"bpf_tcp_ops_store_hdr_opt",
"bpf_tcp_ops_load_hdr_opt",
"bpf_tcp_ops_reserve_hdr_opt",
"bpf_tcp_ops_get_retval",
"cgroup_bpf_struct_ops_register",
"cgroup_struct_ops_link_detach",
"cgroup_struct_ops_link_update",
"cgroup_bpf_struct_ops_attach",
"bpf_struct_ops_link_update_check",
"bpf_tcp_ops_parse_hdr",
"bpf_tcp_ops_hdr_opt_len"
],
"Reasoning": "The patch introduces a new BPF struct_ops type `bpf_tcp_ops` that can be attached to cgroups. This allows BPF programs to hook into various TCP events (like timeout initialization, RTT measurement, state changes, and header option parsing/writing) on a per-cgroup basis. The changes involve core BPF cgroup attachment logic, struct_ops map management, and TCP stack hooks. It is a functional change that adds new reachable code paths in the kernel, making it worth fuzzing.",
"WorthFuzzing": true
} |
| 1/1 | 2026/06/23 18:13 | action | read-patch-diff |
0mArgs:null Results: {
"PatchDiff": "commit b701d6de975f9ae313f7b18395b5440bcffb92f0\nAuthor: syz-cluster \u003ctriage@syzkaller.com\u003e\nDate: Tue Jun 23 18:13:12 2026 +0000\n\n syz-cluster: applied patch under review\n\ndiff --git a/include/linux/bpf-cgroup-defs.h b/include/linux/bpf-cgroup-defs.h\nindex c9e6b26abab6d7..0147b8bec97368 100644\n--- a/include/linux/bpf-cgroup-defs.h\n+++ b/include/linux/bpf-cgroup-defs.h\n@@ -47,6 +47,7 @@ enum cgroup_bpf_attach_type {\n \tCGROUP_INET6_GETSOCKNAME,\n \tCGROUP_UNIX_GETSOCKNAME,\n \tCGROUP_INET_SOCK_RELEASE,\n+\tCGROUP_TCP_SOCK_OPS,\n \tCGROUP_LSM_START,\n \tCGROUP_LSM_END = CGROUP_LSM_START + CGROUP_LSM_NUM - 1,\n \tMAX_CGROUP_BPF_ATTACH_TYPE\ndiff --git a/include/linux/bpf-cgroup.h b/include/linux/bpf-cgroup.h\nindex 4d0cc65976a14a..8a75a6cd73096c 100644\n--- a/include/linux/bpf-cgroup.h\n+++ b/include/linux/bpf-cgroup.h\n@@ -100,6 +100,8 @@ struct bpf_cgroup_storage {\n struct bpf_cgroup_link {\n \tstruct bpf_link link;\n \tstruct cgroup *cgroup;\n+\tstruct bpf_map *map;\n+\twait_queue_head_t wait_hup;\n };\n \n struct bpf_prog_list {\n@@ -110,6 +112,18 @@ struct bpf_prog_list {\n \tu32 flags;\n };\n \n+#define bpf_cgroup_struct_ops_foreach(var, item, cgrp, atype)\t\t\\\n+\tfor (item = rcu_dereference((cgrp)-\u003ebpf.effective[atype])-\u003eitems;\\\n+\t ((var) = READ_ONCE(item-\u003ekdata));\t\t\t\t\\\n+\t item++)\n+\n+static inline bool cgroup_bpf_is_struct_ops_atype(enum cgroup_bpf_attach_type atype)\n+{\n+\treturn atype == CGROUP_TCP_SOCK_OPS;\n+}\n+void cgroup_bpf_struct_ops_register(int atype, u32 type_id, void *cfi_stubs, bool mult_trace);\n+int cgroup_bpf_struct_ops_attach(struct bpf_map *map, const union bpf_attr *attr);\n+\n void __init cgroup_bpf_lifetime_notifier_init(void);\n \n int __cgroup_bpf_run_filter_skb(struct sock *sk,\n@@ -479,6 +493,20 @@ static inline int bpf_percpu_cgroup_storage_update(struct bpf_map *map,\n \treturn 0;\n }\n \n+static inline bool cgroup_bpf_is_struct_ops_atype(int atype)\n+{\n+\treturn false;\n+}\n+static inline void cgroup_bpf_struct_ops_register(int atype, u32 type_id, void *cfi_stubs,\n+\t\t\t\t\t\t bool mult_trace)\n+{\n+}\n+static inline int cgroup_bpf_struct_ops_attach(struct bpf_map *map,\n+\t\t\t\t\t const union bpf_attr *attr)\n+{\n+\treturn -EOPNOTSUPP;\n+}\n+\n #define cgroup_bpf_enabled(atype) (0)\n #define BPF_CGROUP_RUN_SA_PROG_LOCK(sk, uaddr, uaddrlen, atype, t_ctx) ({ 0; })\n #define BPF_CGROUP_RUN_SA_PROG(sk, uaddr, uaddrlen, atype) ({ 0; })\ndiff --git a/include/linux/bpf.h b/include/linux/bpf.h\nindex 7719f652844560..91024d2da4ea86 100644\n--- a/include/linux/bpf.h\n+++ b/include/linux/bpf.h\n@@ -90,6 +90,7 @@ struct bpf_map_ops {\n \tstruct bpf_map *(*map_alloc)(union bpf_attr *attr);\n \tvoid (*map_release)(struct bpf_map *map, struct file *map_file);\n \tvoid (*map_free)(struct bpf_map *map);\n+\tvoid (*map_free_pre_rcu)(struct bpf_map *map);\n \tint (*map_get_next_key)(struct bpf_map *map, void *key, void *next_key);\n \tvoid (*map_release_uref)(struct bpf_map *map);\n \tvoid *(*map_lookup_elem_sys_only)(struct bpf_map *map, void *key);\n@@ -2099,6 +2100,18 @@ struct btf_member;\n *\t unloaded while in use.\n * @name: The name of the struct bpf_struct_ops object.\n * @func_models: Func models\n+ * @cgroup_atype: A value in enum cgroup_bpf_attach_type for cgroup attachment.\n+ *\t\t 0 means the struct_ops type does not support cgroup attachment.\n+ *\t\t If cgroup_atype is non-zero, the @reg and @unreg must be NULL\n+ *\t\t because the attachment/detachment will be handled by the bpf core.\n+ * @free_after_tasks_rcu_gp: Set to true if it needs the bpf core to wait for\n+ * a tasks_rcu gp before freeing the struct_ops map\n+ * and its progs. It is unnecessary if the @unreg\n+ * has waited for the correct rcu gp or the @unreg\n+ * has ensured all struct_ops prog has finished running.\n+ * @free_after_mult_rcu_gp: Same as @free_after_tasks_rcu_gp but waiting for\n+ * both tasks_trace_rcu and regular rcu grace period.\n+ * It is usually needed if the struct_ops has sleepable prog.\n */\n struct bpf_struct_ops {\n \tconst struct bpf_verifier_ops *verifier_ops;\n@@ -2117,6 +2130,9 @@ struct bpf_struct_ops {\n \tstruct module *owner;\n \tconst char *name;\n \tstruct btf_func_model func_models[BPF_STRUCT_OPS_MAX_NR_MEMBERS];\n+\tint cgroup_atype;\n+\tbool free_after_tasks_rcu_gp;\n+\tbool free_after_mult_rcu_gp;\n };\n \n /* Every member of a struct_ops type has an instance even a member is not\n@@ -2245,6 +2261,13 @@ u32 bpf_struct_ops_id(const void *kdata);\n int bpf_struct_ops_for_each_prog(const void *kdata,\n \t\t\t\t int (*cb)(struct bpf_prog *prog, void *data),\n \t\t\t\t void *data);\n+void *bpf_struct_ops_map_kdata(struct bpf_map *map);\n+u32 bpf_struct_ops_kdata_map_id(void *kdata);\n+void *bpf_struct_ops_map_cfi_stubs(struct bpf_map *map);\n+bool bpf_struct_ops_valid_to_reg(struct bpf_map *map);\n+int bpf_struct_ops_link_update_check(struct bpf_map *new_map, struct bpf_map *old_map,\n+\t\t\t\t struct bpf_map *expected_old_map);\n+int bpf_struct_ops_map_cgroup_atype(struct bpf_map *map);\n \n #ifdef CONFIG_NET\n /* Define it here to avoid the use of forward declaration */\n@@ -2309,6 +2332,32 @@ static inline void bpf_map_struct_ops_info_fill(struct bpf_map_info *info, struc\n static inline void bpf_struct_ops_desc_release(struct bpf_struct_ops_desc *st_ops_desc)\n {\n }\n+static inline void *bpf_struct_ops_map_kdata(struct bpf_map *map)\n+{\n+\treturn NULL;\n+}\n+static inline u32 bpf_struct_ops_kdata_map_id(void *kdata)\n+{\n+\treturn 0;\n+}\n+static inline int bpf_struct_ops_map_cgroup_atype(struct bpf_map *map)\n+{\n+\treturn 0;\n+}\n+static inline void *bpf_struct_ops_map_cfi_stubs(struct bpf_map *map)\n+{\n+\treturn NULL;\n+}\n+static inline bool bpf_struct_ops_valid_to_reg(struct bpf_map *map)\n+{\n+\treturn false;\n+}\n+static inline int bpf_struct_ops_link_update_check(struct bpf_map *new_map,\n+\t\t\t\t\t\t struct bpf_map *old_map,\n+\t\t\t\t\t\t struct bpf_map *expected_old_map)\n+{\n+\treturn -EOPNOTSUPP;\n+}\n \n #endif\n \n@@ -2484,7 +2533,10 @@ u64 bpf_event_output(struct bpf_map *map, u64 flags, void *meta, u64 meta_size,\n * since other cpus are walking the array of pointers in parallel.\n */\n struct bpf_prog_array_item {\n-\tstruct bpf_prog *prog;\n+\tunion {\n+\t\tstruct bpf_prog *prog;\n+\t\tvoid *kdata;\n+\t};\n \tunion {\n \t\tstruct bpf_cgroup_storage *cgroup_storage[MAX_BPF_CGROUP_STORAGE_TYPE];\n \t\tu64 bpf_cookie;\n@@ -2526,6 +2578,7 @@ int bpf_prog_array_copy(struct bpf_prog_array *old_array,\n \t\t\tstruct bpf_prog *include_prog,\n \t\t\tu64 bpf_cookie,\n \t\t\tstruct bpf_prog_array **new_array);\n+struct bpf_prog *bpf_prog_dummy(void);\n \n struct bpf_run_ctx {};\n \n@@ -2544,6 +2597,7 @@ struct bpf_trace_run_ctx {\n struct bpf_tramp_run_ctx {\n \tstruct bpf_run_ctx run_ctx;\n \tu64 bpf_cookie;\n+\tint retval;\n \tstruct bpf_run_ctx *saved_run_ctx;\n };\n \ndiff --git a/include/linux/filter.h b/include/linux/filter.h\nindex 67d337ede91b8e..fe28db65fb6a1c 100644\n--- a/include/linux/filter.h\n+++ b/include/linux/filter.h\n@@ -1843,6 +1843,11 @@ static __always_inline long __bpf_xdp_redirect_map(struct bpf_map *map, u64 inde\n \treturn XDP_REDIRECT;\n }\n \n+int __bpf_sock_ops_load_hdr_opt(struct bpf_sock_ops_kern *bpf_sock,\n+\t\t\t\tvoid *search_res, u32 len, u64 flags);\n+int __bpf_sock_ops_store_hdr_opt(struct bpf_sock_ops_kern *bpf_sock,\n+\t\t\t\t const void *from, u32 len, u64 flags);\n+\n #ifdef CONFIG_NET\n int __bpf_skb_load_bytes(const struct sk_buff *skb, u32 offset, void *to, u32 len);\n int __bpf_skb_store_bytes(struct sk_buff *skb, u32 offset, const void *from,\ndiff --git a/include/net/tcp.h b/include/net/tcp.h\nindex 6d376ea4d1c01d..7bf702117602da 100644\n--- a/include/net/tcp.h\n+++ b/include/net/tcp.h\n@@ -2953,12 +2953,160 @@ static inline void tcp_clear_sock_ops_cb_flags(struct sock *sk)\n \n #endif\n \n+#if defined(CONFIG_BPF_JIT) \u0026\u0026 defined(CONFIG_CGROUP_BPF)\n+\n+struct bpf_tcp_ops {\n+\t/* Should return the initial SYN (active open) or SYN-ACK (passive open)\n+\t * retransmission timeout. Return the timeout in jiffies, or \u003c= 0 for\n+\t * the kernel default.\n+\t *\n+\t * @req: request_sock on the passive (synack) path; NULL otherwise.\n+\t */\n+\tint (*timeout_init)(struct sock *sk, struct request_sock *req);\n+\n+\t/* Should return the initial advertised receive window, in packets,\n+\t * or \u003c 0 for the kernel default. @req as in timeout_init().\n+\t */\n+\tint (*rwnd_init)(struct sock *sk, struct request_sock *req);\n+\n+\t/* Called when an active connection becomes established.\n+\t * @skb is the SYNACK that completed the 3WHS, or NULL for a\n+\t * TCP_REPAIR socket (tcp_finish_connect() with no skb).\n+\t */\n+\tvoid (*active_established)(struct sock *sk, struct sk_buff *skb__nullable);\n+\n+\t/* Called when a passive connection becomes established.\n+\t * @skb is the ACK that completed the 3WHS.\n+\t */\n+\tvoid (*passive_established)(struct sock *sk, struct sk_buff *skb);\n+\n+\t/* Called when the retransmission timer fires. */\n+\tvoid (*rto)(struct sock *sk);\n+\n+\t/* Called on every RTT sample.\n+\t * @mrtt: the measured RTT, in microseconds.\n+\t * @srtt: the updated smoothed RTT.\n+\t */\n+\tvoid (*rtt)(struct sock *sk, long mrtt, u32 srtt);\n+\n+\t/* Called when the connection changes TCP state.\n+\t * @state: the new state (one of the TCP_* states).\n+\t */\n+\tvoid (*set_state)(struct sock *sk, int state);\n+\n+\t/* Called when an skb is retransmitted.\n+\t * @skb: the retransmitted skb.\n+\t * @err: tcp_transmit_skb() return value (0 on success).\n+\t */\n+\tvoid (*retrans)(struct sock *sk, struct sk_buff *skb, int err);\n+\n+\t/* Called right before an active connection is initialized. */\n+\tvoid (*connect)(struct sock *sk);\n+\n+\t/* Called on listen(2), right after the socket enters TCP_LISTEN. */\n+\tvoid (*listen)(struct sock *sk);\n+\n+\t/* Parse the TCP header options of an incoming skb received on an\n+\t * established connection. Use bpf_dynptr_from_skb()/bpf_skb_load_bytes()\n+\t * to access the options.\n+\t */\n+\tvoid (*parse_hdr)(struct sock *sk, struct sk_buff *skb);\n+\n+\t/* Reserve space in the outgoing TCP header for options to be written\n+\t * later by write_hdr_opt(). Call bpf_reserve_hdr_opt() to reserve bytes.\n+\t *\n+\t * @skb: outgoing packet. NULL when called from tcp_current_mss()\n+\t * (MSS sizing).\n+\t * @req: request_sock on the synack path; NULL otherwise.\n+\t * @syn_skb: incoming SYN on the synack path; NULL otherwise.\n+\t * @synack_type: TCP_SYNACK_COOKIE indicates a stateless syncookie.\n+\t * @remaining: pointer to the size of space still available; cast it\n+\t * using bpf_rdonly_cast() before dereferencing.\n+\t */\n+\tvoid (*hdr_opt_len)(struct sock *sk, struct sk_buff *skb,\n+\t\t\t struct request_sock *req, struct sk_buff *syn_skb,\n+\t\t\t enum tcp_synack_type synack_type,\n+\t\t\t unsigned int *remaining);\n+\n+\t/* Write header options into the space reserved earlier by hdr_opt_len().\n+\t * Use bpf_store_hdr_opt() to write; it appends within the reserved window\n+\t * shared with legacy SOCKOPS.\n+\t *\n+\t * @skb: outgoing packet.\n+\t * @req: request_sock on the synack path; NULL otherwise.\n+\t * @syn_skb: incoming SYN on the synack path; NULL otherwise.\n+\t * @synack_type: TCP_SYNACK_COOKIE indicates a stateless syncookie.\n+\t * @opt_off: offset in the outgoing @skb's TCP header where the\n+\t *\t bpf_tcp_ops portion of the reserved window begins, i.e. after\n+\t *\t the kernel and legacy options.\n+\t */\n+\tvoid (*write_hdr_opt)(struct sock *sk, struct sk_buff *skb,\n+\t\t\t struct request_sock *req, struct sk_buff *syn_skb,\n+\t\t\t enum tcp_synack_type synack_type,\n+\t\t\t u32 opt_off);\n+};\n+\n+#define bpf_tcp_ops_call(op, sk, ...)\t\t\t\t\t\\\n+do {\t\t\t\t\t\t\t\t\t\\\n+\tif (cgroup_bpf_enabled(CGROUP_TCP_SOCK_OPS)) {\t\t\t\\\n+\t\tconst struct bpf_prog_array_item *item;\t\t\t\\\n+\t\tconst struct bpf_tcp_ops *tcp_ops;\t\t\t\\\n+\t\tstruct cgroup *cgrp;\t\t\t\t\t\\\n+\t\t\t\t\t\t\t\t\t\\\n+\t\tcgrp = sock_cgroup_ptr(\u0026sk-\u003esk_cgrp_data);\t\t\\\n+\t\trcu_read_lock_dont_migrate();\t\t\t\t\\\n+\t\tbpf_cgroup_struct_ops_foreach(tcp_ops, item, cgrp,\t\\\n+\t\t\t\t\t CGROUP_TCP_SOCK_OPS) {\t\\\n+\t\t\tif (tcp_ops-\u003eop)\t\t\t\t\\\n+\t\t\t\ttcp_ops-\u003eop(sk, ##__VA_ARGS__);\t\t\\\n+\t\t}\t\t\t\t\t\t\t\\\n+\t\trcu_read_unlock_migrate();\t\t\t\t\\\n+\t}\t\t\t\t\t\t\t\t\\\n+} while (0)\n+\n+#define bpf_tcp_ops_call_int(op, init_retval, sk, ...)\t\t\t\\\n+({\t\t\t\t\t\t\t\t\t\\\n+\tint __retval = (init_retval);\t\t\t\t\t\\\n+\tif (cgroup_bpf_enabled(CGROUP_TCP_SOCK_OPS)) {\t\t\t\\\n+\t\tconst struct bpf_prog_array_item *item;\t\t\t\\\n+\t\tconst struct bpf_tcp_ops *tcp_ops;\t\t\t\\\n+\t\tstruct bpf_tramp_run_ctx run_ctx;\t\t\t\\\n+\t\tstruct bpf_run_ctx *old_run_ctx;\t\t\t\\\n+\t\tstruct sock *__sk = sk_to_full_sk(sk); \\\n+\t\tstruct request_sock *req = NULL;\t\t\t\\\n+\t\tstruct cgroup *cgrp;\t\t\t\t\t\\\n+\t\t\t\t\t\t\t\t\t\\\n+\t\tif (__sk) {\t\t\t\t\t\t\\\n+\t\t\trun_ctx.retval = (init_retval);\t\t\t\\\n+\t\t\tcgrp = sock_cgroup_ptr(\u0026__sk-\u003esk_cgrp_data);\t\\\n+\t\t\tif (!sk_fullsock(sk))\t\t\t\t\\\n+\t\t\t\treq = (struct request_sock *)sk;\t\\\n+\t\t\trcu_read_lock_dont_migrate();\t\t\t\\\n+\t\t\told_run_ctx = bpf_set_run_ctx(\u0026run_ctx.run_ctx);\\\n+\t\t\tbpf_cgroup_struct_ops_foreach(tcp_ops, item, cgrp, \\\n+\t\t\t\t\t\t CGROUP_TCP_SOCK_OPS) { \\\n+\t\t\t\tif (tcp_ops-\u003eop)\t\t\t\\\n+\t\t\t\t\trun_ctx.retval = tcp_ops-\u003eop(__sk, req, ##__VA_ARGS__); \\\n+\t\t\t}\t\t\t\t\t\t\\\n+\t\t\tbpf_reset_run_ctx(old_run_ctx);\t\t\t\\\n+\t\t\trcu_read_unlock_migrate();\t\t\t\\\n+\t\t\t__retval = run_ctx.retval;\t\t\t\\\n+\t\t}\t\t\t\t\t\t\t\\\n+\t}\t\t\t\t\t\t\t\t\\\n+\t__retval;\t\t\t\t\t\t\t\\\n+})\n+\n+#else\n+#define bpf_tcp_ops_call(op, sk, ...)\t\tdo { } while (0)\n+#define bpf_tcp_ops_call_int(op, init_retval, sk, ...)\t(init_retval)\n+#endif\n+\n static inline u32 tcp_timeout_init(struct sock *sk)\n {\n \tint timeout;\n \n \ttimeout = tcp_call_bpf(sk, BPF_SOCK_OPS_TIMEOUT_INIT, 0, NULL);\n-\n+\ttimeout = bpf_tcp_ops_call_int(timeout_init, timeout, sk);\n \tif (timeout \u003c= 0)\n \t\ttimeout = TCP_TIMEOUT_INIT;\n \treturn min_t(int, timeout, TCP_RTO_MAX);\n@@ -2969,7 +3117,7 @@ static inline u32 tcp_rwnd_init_bpf(struct sock *sk)\n \tint rwnd;\n \n \trwnd = tcp_call_bpf(sk, BPF_SOCK_OPS_RWND_INIT, 0, NULL);\n-\n+\trwnd = bpf_tcp_ops_call_int(rwnd_init, rwnd, sk);\n \tif (rwnd \u003c 0)\n \t\trwnd = 0;\n \treturn rwnd;\n@@ -2984,6 +3132,7 @@ static inline void tcp_bpf_rtt(struct sock *sk, long mrtt, u32 srtt)\n {\n \tif (BPF_SOCK_OPS_TEST_FLAG(tcp_sk(sk), BPF_SOCK_OPS_RTT_CB_FLAG))\n \t\ttcp_call_bpf_2arg(sk, BPF_SOCK_OPS_RTT_CB, mrtt, srtt);\n+\tbpf_tcp_ops_call(rtt, sk, mrtt, srtt);\n }\n \n #if IS_ENABLED(CONFIG_SMC)\ndiff --git a/include/uapi/linux/bpf.h b/include/uapi/linux/bpf.h\nindex 89b36de5fdbb6d..45b9ee29e46174 100644\n--- a/include/uapi/linux/bpf.h\n+++ b/include/uapi/linux/bpf.h\n@@ -1756,7 +1756,7 @@ union bpf_attr {\n \t\t\t__u32\tprog_cnt;\n \t\t\t__u32\tcount;\n \t\t};\n-\t\t__u32\t\t:32;\n+\t\t__u32\t\ttype_id;\n \t\t/* output: per-program attach_flags.\n \t\t * not allowed to be set during effective query.\n \t\t */\n@@ -4799,15 +4799,18 @@ union bpf_attr {\n * \t\tThe non-negative copied *buf* length equal to or less than\n * \t\t*size* on success, or a negative error in case of failure.\n *\n- * long bpf_load_hdr_opt(struct bpf_sock_ops *skops, void *searchby_res, u32 len, u64 flags)\n+ * long bpf_load_hdr_opt(void *ctx, void *searchby_res, u32 len, u64 flags)\n *\tDescription\n *\t\tLoad header option. Support reading a particular TCP header\n- *\t\toption for bpf program (**BPF_PROG_TYPE_SOCK_OPS**).\n+ *\t\toption for bpf program (**BPF_PROG_TYPE_SOCK_OPS**). For the\n+ *\t\t**bpf_tcp_ops** struct_ops, this helper can be called from the\n+ *\t\t**parse_hdr**\\ () and **write_hdr_opt**\\ () operators.\n *\n- *\t\tIf *flags* is 0, it will search the option from the\n- *\t\t*skops*\\ **-\u003eskb_data**. The comment in **struct bpf_sock_ops**\n- *\t\thas details on what skb_data contains under different\n- *\t\t*skops*\\ **-\u003eop**.\n+ *\t\tIf *flags* is 0, it will search the option from the packet\n+ *\t\tassociated with the current operation. For\n+ *\t\t**BPF_PROG_TYPE_SOCK_OPS**, the comment in\n+ *\t\t**struct bpf_sock_ops** has details on what skb_data\n+ *\t\tcontains under different *op*.\n *\n *\t\tThe first byte of the *searchby_res* specifies the\n *\t\tkind that it wants to search.\n@@ -4840,6 +4843,8 @@ union bpf_attr {\n *\n *\t\t* **BPF_LOAD_HDR_OPT_TCP_SYN** to search from the\n *\t\t saved_syn packet or the just-received syn packet.\n+ *\t\t Not supported by the **bpf_tcp_ops** struct_ops, which\n+ *\t\t rejects all flags.\n *\n *\tReturn\n *\t\t\u003e 0 when found, the header option is copied to *searchby_res*.\n@@ -4860,9 +4865,9 @@ union bpf_attr {\n *\t\tpacket.\n *\n *\t\t**-EPERM** if the helper cannot be used under the current\n- *\t\t*skops*\\ **-\u003eop**.\n+ *\t\toperation.\n *\n- * long bpf_store_hdr_opt(struct bpf_sock_ops *skops, const void *from, u32 len, u64 flags)\n+ * long bpf_store_hdr_opt(void *ctx, const void *from, u32 len, u64 flags)\n *\tDescription\n *\t\tStore header option. The data will be copied\n *\t\tfrom buffer *from* with length *len* to the TCP header.\n@@ -4878,7 +4883,9 @@ union bpf_attr {\n *\t\tby searching the same option in the outgoing skb.\n *\n *\t\tThis helper can only be called during\n- *\t\t**BPF_SOCK_OPS_WRITE_HDR_OPT_CB**.\n+ *\t\t**BPF_SOCK_OPS_WRITE_HDR_OPT_CB**, or from the\n+ *\t\t**write_hdr_opt**\\ () operator of the **bpf_tcp_ops**\n+ *\t\tstruct_ops.\n *\n *\tReturn\n *\t\t0 on success, or negative error in case of failure:\n@@ -4893,9 +4900,9 @@ union bpf_attr {\n *\t\t**-EFAULT** on failure to parse the existing header options.\n *\n *\t\t**-EPERM** if the helper cannot be used under the current\n- *\t\t*skops*\\ **-\u003eop**.\n+ *\t\toperation.\n *\n- * long bpf_reserve_hdr_opt(struct bpf_sock_ops *skops, u32 len, u64 flags)\n+ * long bpf_reserve_hdr_opt(void *ctx, u32 len, u64 flags)\n *\tDescription\n *\t\tReserve *len* bytes for the bpf header option. The\n *\t\tspace will be used by **bpf_store_hdr_opt**\\ () later in\n@@ -4905,7 +4912,9 @@ union bpf_attr {\n *\t\tthe total number of bytes will be reserved.\n *\n *\t\tThis helper can only be called during\n- *\t\t**BPF_SOCK_OPS_HDR_OPT_LEN_CB**.\n+ *\t\t**BPF_SOCK_OPS_HDR_OPT_LEN_CB**, or from the\n+ *\t\t**hdr_opt_len**\\ () operator of the **bpf_tcp_ops**\n+ *\t\tstruct_ops.\n *\n *\tReturn\n *\t\t0 on success, or negative error in case of failure:\n@@ -4915,7 +4924,7 @@ union bpf_attr {\n *\t\t**-ENOSPC** if there is not enough space in the header.\n *\n *\t\t**-EPERM** if the helper cannot be used under the current\n- *\t\t*skops*\\ **-\u003eop**.\n+ *\t\toperation.\n *\n * void *bpf_inode_storage_get(struct bpf_map *map, void *inode, void *value, u64 flags)\n *\tDescription\n@@ -6815,6 +6824,8 @@ struct bpf_link_info {\n \t\t} xdp;\n \t\tstruct {\n \t\t\t__u32 map_id;\n+\t\t\t__u32 :32;\n+\t\t\t__u64 cgroup_id;\n \t\t} struct_ops;\n \t\tstruct {\n \t\t\t__u32 pf;\ndiff --git a/kernel/bpf/bpf_struct_ops.c b/kernel/bpf/bpf_struct_ops.c\nindex 51b16e5f5534e0..85cfc7e88d3fd6 100644\n--- a/kernel/bpf/bpf_struct_ops.c\n+++ b/kernel/bpf/bpf_struct_ops.c\n@@ -13,6 +13,7 @@\n #include \u003clinux/btf_ids.h\u003e\n #include \u003clinux/rcupdate_wait.h\u003e\n #include \u003clinux/poll.h\u003e\n+#include \u003clinux/bpf-cgroup.h\u003e\n \n struct bpf_struct_ops_value {\n \tstruct bpf_struct_ops_common_value common;\n@@ -57,7 +58,7 @@ struct bpf_struct_ops_map {\n \n struct bpf_struct_ops_link {\n \tstruct bpf_link link;\n-\tstruct bpf_map __rcu *map;\n+\tstruct bpf_map *map;\n \twait_queue_head_t wait_hup;\n };\n \n@@ -984,9 +985,18 @@ static void __bpf_struct_ops_map_free(struct bpf_map *map)\n \tbpf_map_area_free(st_map);\n }\n \n+static void bpf_struct_ops_map_free_pre_rcu(struct bpf_map *map)\n+{\n+\tstruct bpf_struct_ops_map *st_map = (struct bpf_struct_ops_map *)map;\n+\n+\tbpf_struct_ops_map_del_ksyms(st_map);\n+}\n+\n static void bpf_struct_ops_map_free(struct bpf_map *map)\n {\n \tstruct bpf_struct_ops_map *st_map = (struct bpf_struct_ops_map *)map;\n+\tstruct bpf_struct_ops *st_ops = st_map-\u003est_ops_desc-\u003est_ops;\n+\tbool tasks_rcu = st_ops-\u003efree_after_tasks_rcu_gp;\n \n \t/* st_ops-\u003eowner was acquired during map_alloc to implicitly holds\n \t * the btf's refcnt. The acquire was only done when btf_is_module()\n@@ -997,24 +1007,8 @@ static void bpf_struct_ops_map_free(struct bpf_map *map)\n \n \tbpf_struct_ops_map_dissoc_progs(st_map);\n \n-\tbpf_struct_ops_map_del_ksyms(st_map);\n-\n-\t/* The struct_ops's function may switch to another struct_ops.\n-\t *\n-\t * For example, bpf_tcp_cc_x-\u003einit() may switch to\n-\t * another tcp_cc_y by calling\n-\t * setsockopt(TCP_CONGESTION, \"tcp_cc_y\").\n-\t * During the switch, bpf_struct_ops_put(tcp_cc_x) is called\n-\t * and its refcount may reach 0 which then free its\n-\t * trampoline image while tcp_cc_x is still running.\n-\t *\n-\t * A vanilla rcu gp is to wait for all bpf-tcp-cc prog\n-\t * to finish. bpf-tcp-cc prog is non sleepable.\n-\t * A rcu_tasks gp is to wait for the last few insn\n-\t * in the tramopline image to finish before releasing\n-\t * the trampoline image.\n-\t */\n-\tsynchronize_rcu_mult(call_rcu, call_rcu_tasks);\n+\tif (tasks_rcu \u0026\u0026 IS_ENABLED(CONFIG_TASKS_RCU))\n+\t\tsynchronize_rcu_tasks();\n \n \t__bpf_struct_ops_map_free(map);\n }\n@@ -1083,6 +1077,11 @@ static struct bpf_map *bpf_struct_ops_map_alloc(union bpf_attr *attr)\n \t\tgoto errout;\n \t}\n \n+\tif (st_ops_desc-\u003est_ops-\u003ecgroup_atype \u0026\u0026 !(attr-\u003emap_flags \u0026 BPF_F_LINK)) {\n+\t\tret = -EOPNOTSUPP;\n+\t\tgoto errout;\n+\t}\n+\n \tvt = st_ops_desc-\u003evalue_type;\n \tif (attr-\u003evalue_size != vt-\u003esize) {\n \t\tret = -EINVAL;\n@@ -1123,6 +1122,8 @@ static struct bpf_map *bpf_struct_ops_map_alloc(union bpf_attr *attr)\n \n \tmutex_init(\u0026st_map-\u003elock);\n \tbpf_map_init_from_attr(map, attr);\n+\tmap-\u003efree_after_mult_rcu_gp = st_ops_desc-\u003est_ops-\u003efree_after_mult_rcu_gp;\n+\tmap-\u003efree_after_rcu_gp = true;\n \n \treturn map;\n \n@@ -1155,6 +1156,7 @@ const struct bpf_map_ops bpf_struct_ops_map_ops = {\n \t.map_alloc_check = bpf_struct_ops_map_alloc_check,\n \t.map_alloc = bpf_struct_ops_map_alloc,\n \t.map_free = bpf_struct_ops_map_free,\n+\t.map_free_pre_rcu = bpf_struct_ops_map_free_pre_rcu,\n \t.map_get_next_key = bpf_struct_ops_map_get_next_key,\n \t.map_lookup_elem = bpf_struct_ops_map_lookup_elem,\n \t.map_delete_elem = bpf_struct_ops_map_delete_elem,\n@@ -1241,7 +1243,41 @@ int bpf_struct_ops_for_each_prog(const void *kdata,\n }\n EXPORT_SYMBOL_GPL(bpf_struct_ops_for_each_prog);\n \n-static bool bpf_struct_ops_valid_to_reg(struct bpf_map *map)\n+void *bpf_struct_ops_map_kdata(struct bpf_map *map)\n+{\n+\tstruct bpf_struct_ops_map *st_map;\n+\n+\tst_map = container_of(map, struct bpf_struct_ops_map, map);\n+\treturn st_map-\u003ekvalue.data;\n+}\n+\n+u32 bpf_struct_ops_kdata_map_id(void *kdata)\n+{\n+\tstruct bpf_struct_ops_value *kvalue =\n+\t\tcontainer_of(kdata, struct bpf_struct_ops_value, data);\n+\tstruct bpf_struct_ops_map *st_map =\n+\t\tcontainer_of(kvalue, struct bpf_struct_ops_map, kvalue);\n+\n+\treturn st_map-\u003emap.id;\n+}\n+\n+int bpf_struct_ops_map_cgroup_atype(struct bpf_map *map)\n+{\n+\tstruct bpf_struct_ops_map *st_map;\n+\n+\tst_map = container_of(map, struct bpf_struct_ops_map, map);\n+\treturn st_map-\u003est_ops_desc-\u003est_ops-\u003ecgroup_atype;\n+}\n+\n+void *bpf_struct_ops_map_cfi_stubs(struct bpf_map *map)\n+{\n+\tstruct bpf_struct_ops_map *st_map;\n+\n+\tst_map = container_of(map, struct bpf_struct_ops_map, map);\n+\treturn st_map-\u003est_ops_desc-\u003est_ops-\u003ecfi_stubs;\n+}\n+\n+bool bpf_struct_ops_valid_to_reg(struct bpf_map *map)\n {\n \tstruct bpf_struct_ops_map *st_map = (struct bpf_struct_ops_map *)map;\n \n@@ -1257,8 +1293,7 @@ static void bpf_struct_ops_map_link_dealloc(struct bpf_link *link)\n \tstruct bpf_struct_ops_map *st_map;\n \n \tst_link = container_of(link, struct bpf_struct_ops_link, link);\n-\tst_map = (struct bpf_struct_ops_map *)\n-\t\trcu_dereference_protected(st_link-\u003emap, true);\n+\tst_map = (struct bpf_struct_ops_map *)st_link-\u003emap;\n \tif (st_map) {\n \t\tst_map-\u003est_ops_desc-\u003est_ops-\u003eunreg(\u0026st_map-\u003ekvalue.data, link);\n \t\tbpf_map_put(\u0026st_map-\u003emap);\n@@ -1273,11 +1308,11 @@ static void bpf_struct_ops_map_link_show_fdinfo(const struct bpf_link *link,\n \tstruct bpf_map *map;\n \n \tst_link = container_of(link, struct bpf_struct_ops_link, link);\n-\trcu_read_lock();\n-\tmap = rcu_dereference(st_link-\u003emap);\n+\tmutex_lock(\u0026update_mutex);\n+\tmap = st_link-\u003emap;\n \tif (map)\n \t\tseq_printf(seq, \"map_id:\\t%d\\n\", map-\u003eid);\n-\trcu_read_unlock();\n+\tmutex_unlock(\u0026update_mutex);\n }\n \n static int bpf_struct_ops_map_link_fill_link_info(const struct bpf_link *link,\n@@ -1287,11 +1322,31 @@ static int bpf_struct_ops_map_link_fill_link_info(const struct bpf_link *link,\n \tstruct bpf_map *map;\n \n \tst_link = container_of(link, struct bpf_struct_ops_link, link);\n-\trcu_read_lock();\n-\tmap = rcu_dereference(st_link-\u003emap);\n+\tmutex_lock(\u0026update_mutex);\n+\tmap = st_link-\u003emap;\n \tif (map)\n \t\tinfo-\u003estruct_ops.map_id = map-\u003eid;\n-\trcu_read_unlock();\n+\tmutex_unlock(\u0026update_mutex);\n+\treturn 0;\n+}\n+\n+int bpf_struct_ops_link_update_check(struct bpf_map *new_map,\n+\t\t\t\t struct bpf_map *old_map,\n+\t\t\t\t struct bpf_map *expected_old_map)\n+{\n+\tstruct bpf_struct_ops_map *st_map, *old_st_map;\n+\n+\tif (!old_map)\n+\t\treturn -ENOLINK;\n+\tif (expected_old_map \u0026\u0026 old_map != expected_old_map)\n+\t\treturn -EPERM;\n+\n+\tst_map = container_of(new_map, struct bpf_struct_ops_map, map);\n+\told_st_map = container_of(old_map, struct bpf_struct_ops_map, map);\n+\t/* The new and old struct_ops must be the same type. */\n+\tif (st_map-\u003est_ops_desc != old_st_map-\u003est_ops_desc)\n+\t\treturn -EINVAL;\n+\n \treturn 0;\n }\n \n@@ -1313,30 +1368,19 @@ static int bpf_struct_ops_map_link_update(struct bpf_link *link, struct bpf_map\n \t\treturn -EOPNOTSUPP;\n \n \tmutex_lock(\u0026update_mutex);\n-\n-\told_map = rcu_dereference_protected(st_link-\u003emap, lockdep_is_held(\u0026update_mutex));\n-\tif (!old_map) {\n-\t\terr = -ENOLINK;\n-\t\tgoto err_out;\n-\t}\n-\tif (expected_old_map \u0026\u0026 old_map != expected_old_map) {\n-\t\terr = -EPERM;\n+\told_map = st_link-\u003emap;\n+\terr = bpf_struct_ops_link_update_check(new_map, old_map, expected_old_map);\n+\tif (err)\n \t\tgoto err_out;\n-\t}\n \n \told_st_map = container_of(old_map, struct bpf_struct_ops_map, map);\n-\t/* The new and old struct_ops must be the same type. */\n-\tif (st_map-\u003est_ops_desc != old_st_map-\u003est_ops_desc) {\n-\t\terr = -EINVAL;\n-\t\tgoto err_out;\n-\t}\n \n \terr = st_map-\u003est_ops_desc-\u003est_ops-\u003eupdate(st_map-\u003ekvalue.data, old_st_map-\u003ekvalue.data, link);\n \tif (err)\n \t\tgoto err_out;\n \n \tbpf_map_inc(new_map);\n-\trcu_assign_pointer(st_link-\u003emap, new_map);\n+\tWRITE_ONCE(st_link-\u003emap, new_map);\n \tbpf_map_put(old_map);\n \n err_out:\n@@ -1353,7 +1397,7 @@ static int bpf_struct_ops_map_link_detach(struct bpf_link *link)\n \n \tmutex_lock(\u0026update_mutex);\n \n-\tmap = rcu_dereference_protected(st_link-\u003emap, lockdep_is_held(\u0026update_mutex));\n+\tmap = st_link-\u003emap;\n \tif (!map) {\n \t\tmutex_unlock(\u0026update_mutex);\n \t\treturn 0;\n@@ -1362,7 +1406,7 @@ static int bpf_struct_ops_map_link_detach(struct bpf_link *link)\n \n \tst_map-\u003est_ops_desc-\u003est_ops-\u003eunreg(\u0026st_map-\u003ekvalue.data, link);\n \n-\tRCU_INIT_POINTER(st_link-\u003emap, NULL);\n+\tWRITE_ONCE(st_link-\u003emap, NULL);\n \t/* Pair with bpf_map_get() in bpf_struct_ops_link_create() or\n \t * bpf_map_inc() in bpf_struct_ops_map_link_update().\n \t */\n@@ -1382,7 +1426,7 @@ static __poll_t bpf_struct_ops_map_link_poll(struct file *file,\n \n \tpoll_wait(file, \u0026st_link-\u003ewait_hup, pts);\n \n-\treturn rcu_access_pointer(st_link-\u003emap) ? 0 : EPOLLHUP;\n+\treturn READ_ONCE(st_link-\u003emap) ? 0 : EPOLLHUP;\n }\n \n static const struct bpf_link_ops bpf_struct_ops_map_lops = {\n@@ -1400,6 +1444,7 @@ int bpf_struct_ops_link_create(union bpf_attr *attr)\n \tstruct bpf_link_primer link_primer;\n \tstruct bpf_struct_ops_map *st_map;\n \tstruct bpf_map *map;\n+\tint cgroup_atype;\n \tint err;\n \n \tmap = bpf_map_get(attr-\u003elink_create.map_fd);\n@@ -1413,6 +1458,19 @@ int bpf_struct_ops_link_create(union bpf_attr *attr)\n \t\tgoto err_out;\n \t}\n \n+\tcgroup_atype = st_map-\u003est_ops_desc-\u003est_ops-\u003ecgroup_atype;\n+\tif (cgroup_atype) {\n+\t\terr = cgroup_bpf_struct_ops_attach(map, attr);\n+\t\tbpf_map_put(map);\n+\t\treturn err;\n+\t}\n+\n+\tif (memchr_inv(\u0026attr-\u003elink_create.cgroup, 0, sizeof(attr-\u003elink_create.cgroup)) ||\n+\t attr-\u003elink_create.target_fd) {\n+\t\terr = -EINVAL;\n+\t\tgoto err_out;\n+\t}\n+\n \tlink = kzalloc_obj(*link, GFP_USER);\n \tif (!link) {\n \t\terr = -ENOMEM;\n@@ -1438,7 +1496,7 @@ int bpf_struct_ops_link_create(union bpf_attr *attr)\n \t\tlink = NULL;\n \t\tgoto err_out;\n \t}\n-\tRCU_INIT_POINTER(link-\u003emap, map);\n+\tlink-\u003emap = map;\n \tmutex_unlock(\u0026update_mutex);\n \n \treturn bpf_link_settle(\u0026link_primer);\ndiff --git a/kernel/bpf/btf.c b/kernel/bpf/btf.c\nindex 64572f85edc832..d591f306ace526 100644\n--- a/kernel/bpf/btf.c\n+++ b/kernel/bpf/btf.c\n@@ -20,6 +20,7 @@\n #include \u003clinux/btf.h\u003e\n #include \u003clinux/btf_ids.h\u003e\n #include \u003clinux/bpf.h\u003e\n+#include \u003clinux/bpf-cgroup.h\u003e\n #include \u003clinux/bpf_lsm.h\u003e\n #include \u003clinux/skmsg.h\u003e\n #include \u003clinux/perf_event.h\u003e\n@@ -9836,6 +9837,7 @@ btf_add_struct_ops(struct btf *btf, struct bpf_struct_ops *st_ops,\n \t\t struct bpf_verifier_log *log)\n {\n \tstruct btf_struct_ops_tab *tab, *new_tab;\n+\tint cgroup_atype;\n \tint i, err;\n \n \ttab = btf-\u003estruct_ops_tab;\n@@ -9847,8 +9849,10 @@ btf_add_struct_ops(struct btf *btf, struct bpf_struct_ops *st_ops,\n \t\tbtf-\u003estruct_ops_tab = tab;\n \t}\n \n+\tcgroup_atype = st_ops-\u003ecgroup_atype;\n \tfor (i = 0; i \u003c tab-\u003ecnt; i++)\n-\t\tif (tab-\u003eops[i].st_ops == st_ops)\n+\t\tif (tab-\u003eops[i].st_ops == st_ops ||\n+\t\t (cgroup_atype \u0026\u0026 cgroup_atype == tab-\u003eops[i].st_ops-\u003ecgroup_atype))\n \t\t\treturn -EEXIST;\n \n \tif (tab-\u003ecnt == tab-\u003ecapacity) {\n@@ -9868,6 +9872,23 @@ btf_add_struct_ops(struct btf *btf, struct bpf_struct_ops *st_ops,\n \tif (err)\n \t\treturn err;\n \n+\tif (cgroup_atype) {\n+\t\tif (!cgroup_bpf_is_struct_ops_atype(cgroup_atype) ||\n+\t\t st_ops-\u003ereg || st_ops-\u003eunreg || st_ops-\u003efree_after_tasks_rcu_gp) {\n+\t\t\tbpf_struct_ops_desc_release(\u0026tab-\u003eops[btf-\u003estruct_ops_tab-\u003ecnt]);\n+\t\t\treturn -EINVAL;\n+\t\t}\n+\n+\t\t/* There is no need to unregister from cgroup when the\n+\t\t * btf_free(). No struct_ops map and its cgroup link\n+\t\t * can be created once its btf is gone.\n+\t\t */\n+\t\tcgroup_bpf_struct_ops_register(cgroup_atype,\n+\t\t\t\t\t tab-\u003eops[btf-\u003estruct_ops_tab-\u003ecnt].type_id,\n+\t\t\t\t\t st_ops-\u003ecfi_stubs,\n+\t\t\t\t\t st_ops-\u003efree_after_mult_rcu_gp);\n+\t}\n+\n \tbtf-\u003estruct_ops_tab-\u003ecnt++;\n \n \treturn 0;\ndiff --git a/kernel/bpf/cgroup.c b/kernel/bpf/cgroup.c\nindex 4355ccb78a9c67..a808d2a31f11dd 100644\n--- a/kernel/bpf/cgroup.c\n+++ b/kernel/bpf/cgroup.c\n@@ -24,6 +24,29 @@\n DEFINE_STATIC_KEY_ARRAY_FALSE(cgroup_bpf_enabled_key, MAX_CGROUP_BPF_ATTACH_TYPE);\n EXPORT_SYMBOL(cgroup_bpf_enabled_key);\n \n+static u32 struct_ops_type_id[MAX_CGROUP_BPF_ATTACH_TYPE];\n+static void *struct_ops_cfi_stubs[MAX_CGROUP_BPF_ATTACH_TYPE];\n+static bool struct_ops_mult_rcu[MAX_CGROUP_BPF_ATTACH_TYPE];\n+\n+void cgroup_bpf_struct_ops_register(int atype, u32 type_id, void *cfi_stubs, bool mult_rcu)\n+{\n+\tstruct_ops_type_id[atype] = type_id;\n+\tstruct_ops_cfi_stubs[atype] = cfi_stubs;\n+\tstruct_ops_mult_rcu[atype] = mult_rcu;\n+}\n+\n+static enum cgroup_bpf_attach_type find_atype_by_struct_ops_id(u32 type_id)\n+{\n+\tenum cgroup_bpf_attach_type atype;\n+\n+\tfor (atype = 0; atype \u003c MAX_CGROUP_BPF_ATTACH_TYPE; atype++) {\n+\t\tif (cgroup_bpf_is_struct_ops_atype(atype) \u0026\u0026\n+\t\t struct_ops_type_id[atype] == type_id)\n+\t\t\treturn atype;\n+\t}\n+\treturn CGROUP_BPF_ATTACH_TYPE_INVALID;\n+}\n+\n /*\n * cgroup bpf destruction makes heavy use of work items and there can be a lot\n * of concurrent destructions. Use a separate workqueue so that cgroup bpf\n@@ -306,6 +329,19 @@ static void bpf_cgroup_storages_link(struct bpf_cgroup_storage *storages[],\n \t\tbpf_cgroup_storage_link(storages[stype], cgrp, attach_type);\n }\n \n+static void cgroup_struct_ops_link_detach_wake(struct bpf_cgroup_link *link, bool wake_poll)\n+{\n+\tcgroup_put(link-\u003ecgroup);\n+\tlink-\u003ecgroup = NULL;\n+\n+\tbpf_map_put(link-\u003emap);\n+\t/* READ_ONCE in cgroup_struct_ops_link_poll */\n+\tWRITE_ONCE(link-\u003emap, NULL);\n+\n+\tif (wake_poll)\n+\t\twake_up_interruptible_poll(\u0026link-\u003ewait_hup, EPOLLHUP);\n+}\n+\n /* Called when bpf_cgroup_link is auto-detached from dying cgroup.\n * It drops cgroup and bpf_prog refcounts, and marks bpf_link as defunct. It\n * doesn't free link memory, which will eventually be done by bpf_link's\n@@ -313,8 +349,92 @@ static void bpf_cgroup_storages_link(struct bpf_cgroup_storage *storages[],\n */\n static void bpf_cgroup_link_auto_detach(struct bpf_cgroup_link *link)\n {\n-\tcgroup_put(link-\u003ecgroup);\n-\tlink-\u003ecgroup = NULL;\n+\tif (link-\u003emap) {\n+\t\tcgroup_struct_ops_link_detach_wake(link, true);\n+\t} else {\n+\t\tif (link-\u003elink.prog-\u003eexpected_attach_type == BPF_LSM_CGROUP)\n+\t\t\tbpf_trampoline_unlink_cgroup_shim(link-\u003elink.prog);\n+\t\tcgroup_put(link-\u003ecgroup);\n+\t\tlink-\u003ecgroup = NULL;\n+\t}\n+}\n+\n+static void bpf_cgroup_array_free_rcu(struct rcu_head *rcu)\n+{\n+\tkfree(container_of(rcu, struct bpf_prog_array, rcu));\n+}\n+\n+static void bpf_cgroup_array_free(struct bpf_prog_array *array,\n+\t\t\t\t enum cgroup_bpf_attach_type atype)\n+{\n+\tif (!array || array == \u0026bpf_empty_prog_array)\n+\t\treturn;\n+\tif (struct_ops_mult_rcu[atype])\n+\t\t/* RCU tasks trace grace period implies RCU grace period. */\n+\t\tcall_rcu_tasks_trace(\u0026array-\u003ercu, bpf_cgroup_array_free_rcu);\n+\telse\n+\t\tkfree_rcu(array, rcu);\n+}\n+\n+static void *bpf_cgroup_array_dummy(enum cgroup_bpf_attach_type atype)\n+{\n+\tif (cgroup_bpf_is_struct_ops_atype(atype))\n+\t\treturn struct_ops_cfi_stubs[atype];\n+\treturn bpf_prog_dummy();\n+}\n+\n+static int bpf_cgroup_array_length(struct bpf_prog_array *array,\n+\t\t\t\t enum cgroup_bpf_attach_type atype)\n+{\n+\tstruct bpf_prog_array_item *item;\n+\tint cnt = 0;\n+\n+\tfor (item = array-\u003eitems; item-\u003eprog; item++)\n+\t\tif (item-\u003eprog != bpf_cgroup_array_dummy(atype))\n+\t\t\tcnt++;\n+\n+\treturn cnt;\n+}\n+\n+static int bpf_cgroup_array_copy_to_user(struct bpf_prog_array *array,\n+\t\t\t\t\t __u32 __user *prog_ids, int cnt,\n+\t\t\t\t\t enum cgroup_bpf_attach_type atype)\n+{\n+\tstruct bpf_prog_array_item *item;\n+\tint i = 0;\n+\tu32 id;\n+\n+\tfor (item = array-\u003eitems; item-\u003eprog \u0026\u0026 i \u003c cnt; item++) {\n+\t\tif (item-\u003eprog == bpf_cgroup_array_dummy(atype))\n+\t\t\tcontinue;\n+\n+\t\tif (cgroup_bpf_is_struct_ops_atype(atype))\n+\t\t\tid = bpf_struct_ops_kdata_map_id(item-\u003ekdata);\n+\t\telse\n+\t\t\tid = item-\u003eprog-\u003eaux-\u003eid;\n+\n+\t\tif (copy_to_user(prog_ids + i, \u0026id, sizeof(id)))\n+\t\t\treturn -EFAULT;\n+\t\ti++;\n+\t}\n+\treturn item-\u003eprog ? -ENOSPC : 0;\n+}\n+\n+static int bpf_cgroup_array_delete_safe_at(struct bpf_prog_array *array,\n+\t\t\t\t\t int index, enum cgroup_bpf_attach_type atype)\n+{\n+\tstruct bpf_prog_array_item *item;\n+\n+\tfor (item = array-\u003eitems; item-\u003eprog; item++) {\n+\t\tif (item-\u003eprog == bpf_cgroup_array_dummy(atype))\n+\t\t\tcontinue;\n+\t\tif (!index) {\n+\t\t\tWRITE_ONCE(item-\u003eprog, bpf_cgroup_array_dummy(atype));\n+\t\t\treturn 0;\n+\t\t}\n+\t\tindex--;\n+\t}\n+\treturn -ENOENT;\n }\n \n /**\n@@ -346,18 +466,15 @@ static void cgroup_bpf_release(struct work_struct *work)\n \t\t\t\t\tbpf_trampoline_unlink_cgroup_shim(pl-\u003eprog);\n \t\t\t\tbpf_prog_put(pl-\u003eprog);\n \t\t\t}\n-\t\t\tif (pl-\u003elink) {\n-\t\t\t\tif (pl-\u003elink-\u003elink.prog-\u003eexpected_attach_type == BPF_LSM_CGROUP)\n-\t\t\t\t\tbpf_trampoline_unlink_cgroup_shim(pl-\u003elink-\u003elink.prog);\n+\t\t\tif (pl-\u003elink)\n \t\t\t\tbpf_cgroup_link_auto_detach(pl-\u003elink);\n-\t\t\t}\n \t\t\tkfree(pl);\n \t\t\tstatic_branch_dec(\u0026cgroup_bpf_enabled_key[atype]);\n \t\t}\n \t\told_array = rcu_dereference_protected(\n \t\t\t\tcgrp-\u003ebpf.effective[atype],\n \t\t\t\tlockdep_is_held(\u0026cgroup_mutex));\n-\t\tbpf_prog_array_free(old_array);\n+\t\tbpf_cgroup_array_free(old_array, atype);\n \t}\n \n \tlist_for_each_entry_safe(storage, stmp, storages, list_cg) {\n@@ -399,6 +516,31 @@ static struct bpf_prog *prog_list_prog(struct bpf_prog_list *pl)\n \treturn NULL;\n }\n \n+static void prog_list_init_item(struct bpf_prog_list *pl, struct bpf_prog_array_item *item)\n+{\n+\tif (pl-\u003elink \u0026\u0026 pl-\u003elink-\u003emap) {\n+\t\titem-\u003ekdata = bpf_struct_ops_map_kdata(pl-\u003elink-\u003emap);\n+\t} else {\n+\t\titem-\u003eprog = prog_list_prog(pl);\n+\t\tbpf_cgroup_storages_assign(item-\u003ecgroup_storage, pl-\u003estorage);\n+\t}\n+}\n+\n+static void prog_list_replace_item(struct bpf_prog_list *pl, struct bpf_prog_array_item *item)\n+{\n+\tif (pl-\u003elink \u0026\u0026 pl-\u003elink-\u003emap)\n+\t\tWRITE_ONCE(item-\u003ekdata, bpf_struct_ops_map_kdata(pl-\u003elink-\u003emap));\n+\telse\n+\t\tWRITE_ONCE(item-\u003eprog, pl-\u003elink-\u003elink.prog);\n+}\n+\n+static u32 prog_list_id(struct bpf_prog_list *pl)\n+{\n+\tif (pl-\u003elink \u0026\u0026 pl-\u003elink-\u003emap)\n+\t\treturn pl-\u003elink-\u003emap-\u003eid;\n+\treturn prog_list_prog(pl)-\u003eaux-\u003eid;\n+}\n+\n /* count number of elements in the list.\n * it's slow but the list cannot be long\n */\n@@ -408,7 +550,7 @@ static u32 prog_list_length(struct hlist_head *head, int *preorder_cnt)\n \tu32 cnt = 0;\n \n \thlist_for_each_entry(pl, head, node) {\n-\t\tif (!prog_list_prog(pl))\n+\t\tif (!pl-\u003eprog \u0026\u0026 !pl-\u003elink)\n \t\t\tcontinue;\n \t\tif (preorder_cnt \u0026\u0026 (pl-\u003eflags \u0026 BPF_F_PREORDER))\n \t\t\t(*preorder_cnt)++;\n@@ -482,7 +624,7 @@ static int compute_effective_progs(struct cgroup *cgrp,\n \n \t\tinit_bstart = bstart;\n \t\thlist_for_each_entry(pl, \u0026p-\u003ebpf.progs[atype], node) {\n-\t\t\tif (!prog_list_prog(pl))\n+\t\t\tif (!pl-\u003eprog \u0026\u0026 !pl-\u003elink)\n \t\t\t\tcontinue;\n \n \t\t\tif (pl-\u003eflags \u0026 BPF_F_PREORDER) {\n@@ -492,9 +634,7 @@ static int compute_effective_progs(struct cgroup *cgrp,\n \t\t\t\titem = \u0026progs-\u003eitems[fstart];\n \t\t\t\tfstart++;\n \t\t\t}\n-\t\t\titem-\u003eprog = prog_list_prog(pl);\n-\t\t\tbpf_cgroup_storages_assign(item-\u003ecgroup_storage,\n-\t\t\t\t\t\t pl-\u003estorage);\n+\t\t\tprog_list_init_item(pl, item);\n \t\t\tcnt++;\n \t\t}\n \n@@ -517,7 +657,7 @@ static void activate_effective_progs(struct cgroup *cgrp,\n \t/* free prog array after grace period, since __cgroup_bpf_run_*()\n \t * might be still walking the array\n \t */\n-\tbpf_prog_array_free(old_array);\n+\tbpf_cgroup_array_free(old_array, atype);\n }\n \n /**\n@@ -557,7 +697,7 @@ static int cgroup_bpf_inherit(struct cgroup *cgrp)\n \treturn 0;\n cleanup:\n \tfor (i = 0; i \u003c NR; i++)\n-\t\tbpf_prog_array_free(arrays[i]);\n+\t\tbpf_cgroup_array_free(arrays[i], i);\n \n \tfor (p = cgroup_parent(cgrp); p; p = cgroup_parent(p))\n \t\tcgroup_bpf_put(p);\n@@ -612,7 +752,7 @@ static int update_effective_progs(struct cgroup *cgrp,\n \n \t\tif (percpu_ref_is_zero(\u0026desc-\u003ebpf.refcnt)) {\n \t\t\tif (unlikely(desc-\u003ebpf.inactive)) {\n-\t\t\t\tbpf_prog_array_free(desc-\u003ebpf.inactive);\n+\t\t\t\tbpf_cgroup_array_free(desc-\u003ebpf.inactive, atype);\n \t\t\t\tdesc-\u003ebpf.inactive = NULL;\n \t\t\t}\n \t\t\tcontinue;\n@@ -631,7 +771,7 @@ static int update_effective_progs(struct cgroup *cgrp,\n \tcss_for_each_descendant_pre(css, \u0026cgrp-\u003eself) {\n \t\tstruct cgroup *desc = container_of(css, struct cgroup, self);\n \n-\t\tbpf_prog_array_free(desc-\u003ebpf.inactive);\n+\t\tbpf_cgroup_array_free(desc-\u003ebpf.inactive, atype);\n \t\tdesc-\u003ebpf.inactive = NULL;\n \t}\n \n@@ -866,7 +1006,7 @@ static int __cgroup_bpf_attach(struct cgroup *cgrp,\n \tif (pl) {\n \t\told_prog = pl-\u003eprog;\n \t} else {\n-\t\tpl = kmalloc_obj(*pl);\n+\t\tpl = kzalloc_obj(*pl);\n \t\tif (!pl) {\n \t\t\tbpf_cgroup_storages_free(new_storage);\n \t\t\treturn -ENOMEM;\n@@ -965,9 +1105,10 @@ static int effective_prog_pos(struct cgroup *cgrp,\n \n \t\tinit_bstart = bstart;\n \t\thlist_for_each_entry(pl, \u0026p-\u003ebpf.progs[atype], node) {\n-\t\t\tif (!prog_list_prog(pl))\n-\t\t\t\tcontinue;\n-\n+\t\t\t/*\n+\t\t\t * No detaching pl (NULL prog and link) is visible to the callers,\n+\t\t\t * so skip the check compute_effective_progs() needs.\n+\t\t\t */\n \t\t\tif (pl-\u003eflags \u0026 BPF_F_PREORDER) {\n \t\t\t\tif (pl == target_pl)\n \t\t\t\t\tpos = bstart;\n@@ -1014,7 +1155,7 @@ static void replace_effective_prog(struct cgroup *cgrp,\n \t\t\t\tdesc-\u003ebpf.effective[atype],\n \t\t\t\tlockdep_is_held(\u0026cgroup_mutex));\n \t\titem = \u0026progs-\u003eitems[pos];\n-\t\tWRITE_ONCE(item-\u003eprog, pl-\u003elink-\u003elink.prog);\n+\t\tprog_list_replace_item(pl, item);\n \t}\n }\n \n@@ -1152,7 +1293,7 @@ static void purge_effective_progs(struct cgroup *cgrp, struct bpf_prog_list *pl,\n \t\t\t\tlockdep_is_held(\u0026cgroup_mutex));\n \n \t\t/* Remove the program from the array */\n-\t\tWARN_ONCE(bpf_prog_array_delete_safe_at(progs, pos),\n+\t\tWARN_ONCE(bpf_cgroup_array_delete_safe_at(progs, pos, atype),\n \t\t\t \"Failed to purge a prog from array at index %d\", pos);\n \t}\n }\n@@ -1262,7 +1403,17 @@ static int __cgroup_bpf_query(struct cgroup *cgrp, const union bpf_attr *attr,\n \tif (effective_query \u0026\u0026 prog_attach_flags)\n \t\treturn -EINVAL;\n \n-\tif (type == BPF_LSM_CGROUP) {\n+\tif (type == BPF_STRUCT_OPS) {\n+\t\tu32 type_id = attr-\u003equery.type_id;\n+\n+\t\tatype = find_atype_by_struct_ops_id(type_id);\n+\t\tif (atype == CGROUP_BPF_ATTACH_TYPE_INVALID)\n+\t\t\treturn -ENOENT;\n+\t\tfrom_atype = to_atype = atype;\n+\t\tflags = 0;\n+\t\tif (!cgroup_bpf_enabled(atype))\n+\t\t\tgoto skip_count;\n+\t} else if (type == BPF_LSM_CGROUP) {\n \t\tif (!effective_query \u0026\u0026 attr-\u003equery.prog_cnt \u0026\u0026\n \t\t prog_ids \u0026\u0026 !prog_attach_flags)\n \t\t\treturn -EINVAL;\n@@ -1282,12 +1433,13 @@ static int __cgroup_bpf_query(struct cgroup *cgrp, const union bpf_attr *attr,\n \t\tif (effective_query) {\n \t\t\teffective = rcu_dereference_protected(cgrp-\u003ebpf.effective[atype],\n \t\t\t\t\t\t\t lockdep_is_held(\u0026cgroup_mutex));\n-\t\t\ttotal_cnt += bpf_prog_array_length(effective);\n+\t\t\ttotal_cnt += bpf_cgroup_array_length(effective, atype);\n \t\t} else {\n \t\t\ttotal_cnt += prog_list_length(\u0026cgrp-\u003ebpf.progs[atype], NULL);\n \t\t}\n \t}\n \n+skip_count:\n \t/* always output uattr-\u003equery.attach_flags as 0 during effective query */\n \tflags = effective_query ? 0 : flags;\n \tif (copy_to_user(\u0026uattr-\u003equery.attach_flags, \u0026flags, sizeof(flags)))\n@@ -1312,20 +1464,18 @@ static int __cgroup_bpf_query(struct cgroup *cgrp, const union bpf_attr *attr,\n \t\tif (effective_query) {\n \t\t\teffective = rcu_dereference_protected(cgrp-\u003ebpf.effective[atype],\n \t\t\t\t\t\t\t lockdep_is_held(\u0026cgroup_mutex));\n-\t\t\tcnt = min_t(int, bpf_prog_array_length(effective), total_cnt);\n-\t\t\tret = bpf_prog_array_copy_to_user(effective, prog_ids, cnt);\n+\t\t\tcnt = min_t(int, bpf_cgroup_array_length(effective, atype), total_cnt);\n+\t\t\tret = bpf_cgroup_array_copy_to_user(effective, prog_ids, cnt, atype);\n \t\t} else {\n \t\t\tstruct hlist_head *progs;\n \t\t\tstruct bpf_prog_list *pl;\n-\t\t\tstruct bpf_prog *prog;\n \t\t\tu32 id;\n \n \t\t\tprogs = \u0026cgrp-\u003ebpf.progs[atype];\n \t\t\tcnt = min_t(int, prog_list_length(progs, NULL), total_cnt);\n \t\t\ti = 0;\n \t\t\thlist_for_each_entry(pl, progs, node) {\n-\t\t\t\tprog = prog_list_prog(pl);\n-\t\t\t\tid = prog-\u003eaux-\u003eid;\n+\t\t\t\tid = prog_list_id(pl);\n \t\t\t\tif (copy_to_user(prog_ids + i, \u0026id, sizeof(id)))\n \t\t\t\t\treturn -EFAULT;\n \t\t\t\tif (++i == cnt)\n@@ -2747,6 +2897,270 @@ const struct bpf_verifier_ops cg_sockopt_verifier_ops = {\n const struct bpf_prog_ops cg_sockopt_prog_ops = {\n };\n \n+static int __cgroup_struct_ops_link_detach(struct bpf_link *link, bool wake_poll)\n+{\n+\tstruct bpf_cgroup_link *cg_link = container_of(link, struct bpf_cgroup_link, link);\n+\tenum cgroup_bpf_attach_type atype;\n+\tstruct bpf_prog_list *pl;\n+\tstruct bpf_map *map;\n+\tstruct cgroup *cgrp;\n+\n+\tcgroup_lock();\n+\n+\tcgrp = cg_link-\u003ecgroup;\n+\tif (!cgrp) {\n+\t\tcgroup_unlock();\n+\t\treturn 0;\n+\t}\n+\n+\tmap = cg_link-\u003emap;\n+\tatype = bpf_struct_ops_map_cgroup_atype(map);\n+\n+\thlist_for_each_entry(pl, \u0026cgrp-\u003ebpf.progs[atype], node) {\n+\t\tif (pl-\u003elink == cg_link)\n+\t\t\tbreak;\n+\t}\n+\n+\t/* mark deleted so compute_effective_progs() skips it */\n+\tpl-\u003elink = NULL;\n+\tif (update_effective_progs(cgrp, atype)) {\n+\t\tpl-\u003elink = cg_link;\n+\t\tpurge_effective_progs(cgrp, pl, atype);\n+\t}\n+\n+\thlist_del(\u0026pl-\u003enode);\n+\tcgroup_struct_ops_link_detach_wake(cg_link, wake_poll);\n+\tcgrp-\u003ebpf.revisions[atype]++;\n+\n+\tkfree(pl);\n+\tstatic_branch_dec(\u0026cgroup_bpf_enabled_key[atype]);\n+\n+\tcgroup_unlock();\n+\n+\treturn 0;\n+}\n+\n+static int cgroup_struct_ops_link_detach(struct bpf_link *link)\n+{\n+\treturn __cgroup_struct_ops_link_detach(link, true);\n+}\n+\n+static void cgroup_struct_ops_link_dealloc(struct bpf_link *link)\n+{\n+\tstruct bpf_cgroup_link *cg_link = container_of(link, struct bpf_cgroup_link, link);\n+\n+\t__cgroup_struct_ops_link_detach(link, false);\n+\tkfree(cg_link);\n+}\n+\n+static void cgroup_struct_ops_link_show_fdinfo(const struct bpf_link *link, struct seq_file *seq)\n+{\n+\tstruct bpf_cgroup_link *cg_link =\n+\t\tcontainer_of(link, struct bpf_cgroup_link, link);\n+\n+\tcgroup_lock();\n+\tif (!cg_link-\u003ecgroup) {\n+\t\tcgroup_unlock();\n+\t\treturn;\n+\t}\n+\n+\tseq_printf(seq, \"map_id:\\t%u\\n\", cg_link-\u003emap-\u003eid);\n+\tseq_printf(seq, \"cgroup_id:\\t%llu\\n\", cgroup_id(cg_link-\u003ecgroup));\n+\tcgroup_unlock();\n+}\n+\n+static int cgroup_struct_ops_link_fill_link_info(const struct bpf_link *link,\n+\t\t\t\t\t\t struct bpf_link_info *info)\n+{\n+\tstruct bpf_cgroup_link *cg_link = container_of(link, struct bpf_cgroup_link, link);\n+\n+\tcgroup_lock();\n+\tif (!cg_link-\u003ecgroup) {\n+\t\tcgroup_unlock();\n+\t\treturn 0;\n+\t}\n+\n+\tinfo-\u003estruct_ops.map_id = cg_link-\u003emap-\u003eid;\n+\tinfo-\u003estruct_ops.cgroup_id = cgroup_id(cg_link-\u003ecgroup);\n+\tcgroup_unlock();\n+\treturn 0;\n+}\n+\n+static int cgroup_struct_ops_link_update(struct bpf_link *link, struct bpf_map *new_map,\n+\t\t\t\t\t struct bpf_map *expected_old_map)\n+{\n+\tstruct bpf_cgroup_link *cg_link = container_of(link, struct bpf_cgroup_link, link);\n+\tenum cgroup_bpf_attach_type atype;\n+\tstruct bpf_prog_list *pl;\n+\tstruct bpf_map *old_map;\n+\tstruct cgroup *cgrp;\n+\tbool found = false;\n+\tint err;\n+\n+\tif (!bpf_struct_ops_valid_to_reg(new_map))\n+\t\treturn -EINVAL;\n+\n+\tcgroup_lock();\n+\n+\tcgrp = cg_link-\u003ecgroup;\n+\tif (!cgrp) {\n+\t\terr = -ENOLINK;\n+\t\tgoto out;\n+\t}\n+\n+\told_map = cg_link-\u003emap;\n+\terr = bpf_struct_ops_link_update_check(new_map, old_map, expected_old_map);\n+\tif (err)\n+\t\tgoto out;\n+\n+\tatype = bpf_struct_ops_map_cgroup_atype(new_map);\n+\n+\thlist_for_each_entry(pl, \u0026cgrp-\u003ebpf.progs[atype], node) {\n+\t\tif (pl-\u003elink == cg_link) {\n+\t\t\tfound = true;\n+\t\t\tbreak;\n+\t\t}\n+\t}\n+\tif (!found) {\n+\t\terr = -ENOENT;\n+\t\tgoto out;\n+\t}\n+\n+\tbpf_map_inc(new_map);\n+\tWRITE_ONCE(cg_link-\u003emap, new_map);\n+\treplace_effective_prog(cgrp, atype, pl);\n+\tbpf_map_put(old_map);\n+\tcgrp-\u003ebpf.revisions[atype]++;\n+\n+out:\n+\tcgroup_unlock();\n+\treturn err;\n+}\n+\n+static __poll_t cgroup_struct_ops_link_poll(struct file *file, struct poll_table_struct *pts)\n+{\n+\tstruct bpf_cgroup_link *link = file-\u003eprivate_data;\n+\n+\tpoll_wait(file, \u0026link-\u003ewait_hup, pts);\n+\n+\treturn READ_ONCE(link-\u003emap) ? 0 : EPOLLHUP;\n+}\n+\n+static const struct bpf_link_ops cgroup_struct_ops_link_ops = {\n+\t.dealloc = cgroup_struct_ops_link_dealloc,\n+\t.detach = cgroup_struct_ops_link_detach,\n+\t.show_fdinfo = cgroup_struct_ops_link_show_fdinfo,\n+\t.fill_link_info = cgroup_struct_ops_link_fill_link_info,\n+\t.update_map = cgroup_struct_ops_link_update,\n+\t.poll = cgroup_struct_ops_link_poll,\n+};\n+\n+int cgroup_bpf_struct_ops_attach(struct bpf_map *map, const union bpf_attr *attr)\n+{\n+\tu32 flags = attr-\u003elink_create.flags;\n+\tu32 pl_flags = (flags \u0026 BPF_F_PREORDER) | BPF_F_ALLOW_MULTI;\n+\tenum cgroup_bpf_attach_type atype;\n+\tstruct bpf_link_primer link_primer;\n+\tstruct bpf_cgroup_link *link;\n+\tstruct bpf_prog_list *pl = NULL;\n+\tstruct hlist_head *progs;\n+\tstruct cgroup *cgrp;\n+\tint err;\n+\n+\tif (flags \u0026 ~BPF_F_LINK_ATTACH_MASK)\n+\t\treturn -EINVAL;\n+\n+\t/*\n+\t * Attaching struct_ops to cgroup is through link only. All relative\n+\t * position must be corresponding to a link id or fd.\n+\t */\n+\tif (attr-\u003elink_create.cgroup.relative_fd \u0026\u0026 !(flags \u0026 BPF_F_LINK))\n+\t\treturn -EINVAL;\n+\n+\tlink = kzalloc_obj(*link, GFP_USER);\n+\tif (!link)\n+\t\treturn -ENOMEM;\n+\n+\tbpf_link_init(\u0026link-\u003elink, BPF_LINK_TYPE_STRUCT_OPS,\n+\t\t \u0026cgroup_struct_ops_link_ops, NULL,\n+\t\t attr-\u003elink_create.attach_type);\n+\n+\terr = bpf_link_prime(\u0026link-\u003elink, \u0026link_primer);\n+\tif (err) {\n+\t\tkfree(link);\n+\t\treturn err;\n+\t}\n+\n+\tcgrp = cgroup_get_from_fd(attr-\u003elink_create.target_fd);\n+\tif (IS_ERR(cgrp)) {\n+\t\terr = PTR_ERR(cgrp);\n+\t\tgoto cleanup;\n+\t}\n+\n+\tbpf_map_inc(map);\n+\tlink-\u003emap = map;\n+\tlink-\u003ecgroup = cgrp;\n+\tinit_waitqueue_head(\u0026link-\u003ewait_hup);\n+\n+\tatype = bpf_struct_ops_map_cgroup_atype(map);\n+\tprogs = \u0026cgrp-\u003ebpf.progs[atype];\n+\n+\tcgroup_lock();\n+\n+\tif (attr-\u003elink_create.cgroup.expected_revision \u0026\u0026\n+\t attr-\u003elink_create.cgroup.expected_revision != cgrp-\u003ebpf.revisions[atype]) {\n+\t\terr = -ESTALE;\n+\t\tgoto unlock;\n+\t}\n+\n+\tif (prog_list_length(progs, NULL) \u003e= BPF_CGROUP_MAX_PROGS) {\n+\t\terr = -E2BIG;\n+\t\tgoto unlock;\n+\t}\n+\n+\tpl = kzalloc_obj(*pl);\n+\tif (!pl) {\n+\t\terr = -ENOMEM;\n+\t\tgoto unlock;\n+\t}\n+\n+\tpl-\u003elink = link;\n+\tpl-\u003eflags = pl_flags;\n+\tcgrp-\u003ebpf.flags[atype] = BPF_F_ALLOW_MULTI;\n+\n+\terr = insert_pl_to_hlist(pl, progs, NULL, link,\n+\t\t\t\t flags | BPF_F_ALLOW_MULTI, attr-\u003elink_create.cgroup.relative_fd);\n+\tif (err)\n+\t\tgoto unlock;\n+\n+\terr = update_effective_progs(cgrp, atype);\n+\tif (err) {\n+\t\thlist_del(\u0026pl-\u003enode);\n+\t\tgoto unlock;\n+\t}\n+\n+\tcgrp-\u003ebpf.revisions[atype]++;\n+\tstatic_branch_inc(\u0026cgroup_bpf_enabled_key[atype]);\n+\n+\tcgroup_unlock();\n+\n+\treturn bpf_link_settle(\u0026link_primer);\n+\n+unlock:\n+\tcgroup_unlock();\n+\n+cleanup:\n+\tkfree(pl);\n+\tif (link-\u003ecgroup) {\n+\t\tcgroup_put(link-\u003ecgroup);\n+\t\tlink-\u003ecgroup = NULL;\n+\t\tbpf_map_put(link-\u003emap);\n+\t\tlink-\u003emap = NULL;\n+\t}\n+\tbpf_link_cleanup(\u0026link_primer);\n+\treturn err;\n+}\n+\n /* Common helpers for cgroup hooks. */\n const struct bpf_func_proto *\n cgroup_common_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)\ndiff --git a/kernel/bpf/core.c b/kernel/bpf/core.c\nindex 649cce41e13f3b..1837bb7bb4e9d6 100644\n--- a/kernel/bpf/core.c\n+++ b/kernel/bpf/core.c\n@@ -2740,6 +2740,11 @@ void bpf_prog_array_free_sleepable(struct bpf_prog_array *progs)\n \tcall_rcu_tasks_trace(\u0026progs-\u003ercu, __bpf_prog_array_free_sleepable_cb);\n }\n \n+struct bpf_prog *bpf_prog_dummy(void)\n+{\n+\treturn \u0026dummy_bpf_prog.prog;\n+}\n+\n int bpf_prog_array_length(struct bpf_prog_array *array)\n {\n \tstruct bpf_prog_array_item *item;\ndiff --git a/kernel/bpf/syscall.c b/kernel/bpf/syscall.c\nindex 6db306d23b479f..b439c0b0eadde7 100644\n--- a/kernel/bpf/syscall.c\n+++ b/kernel/bpf/syscall.c\n@@ -956,6 +956,9 @@ void bpf_map_put(struct bpf_map *map)\n \t\t/* bpf_map_free_id() must be called first */\n \t\tbpf_map_free_id(map);\n \n+\t\tif (map-\u003eops-\u003emap_free_pre_rcu)\n+\t\t\tmap-\u003eops-\u003emap_free_pre_rcu(map);\n+\n \t\tWARN_ON_ONCE(atomic64_read(\u0026map-\u003esleepable_refcnt));\n \t\t/* RCU tasks trace grace period implies RCU grace period. */\n \t\tif (READ_ONCE(map-\u003efree_after_mult_rcu_gp))\n@@ -4810,6 +4813,7 @@ static int bpf_prog_query(const union bpf_attr *attr,\n \tcase BPF_CGROUP_GETSOCKOPT:\n \tcase BPF_CGROUP_SETSOCKOPT:\n \tcase BPF_LSM_CGROUP:\n+\tcase BPF_STRUCT_OPS:\n \t\treturn cgroup_bpf_prog_query(attr, uattr, uattr_size);\n \tcase BPF_LIRC_MODE2:\n \t\treturn lirc_prog_query(attr, uattr);\ndiff --git a/net/core/filter.c b/net/core/filter.c\nindex 1dd5e37ae13027..dc44ffb7a380b9 100644\n--- a/net/core/filter.c\n+++ b/net/core/filter.c\n@@ -7885,17 +7885,14 @@ static const u8 *bpf_search_tcp_opt(const u8 *op, const u8 *opend,\n \treturn ERR_PTR(-ENOMSG);\n }\n \n-BPF_CALL_4(bpf_sock_ops_load_hdr_opt, struct bpf_sock_ops_kern *, bpf_sock,\n-\t void *, search_res, u32, len, u64, flags)\n+int __bpf_sock_ops_load_hdr_opt(struct bpf_sock_ops_kern *bpf_sock,\n+\t\t\t\tvoid *search_res, u32 len, u64 flags)\n {\n \tbool eol, load_syn = flags \u0026 BPF_LOAD_HDR_OPT_TCP_SYN;\n \tconst u8 *op, *opend, *magic, *search = search_res;\n \tu8 search_kind, search_len, copy_len, magic_len;\n \tint ret;\n \n-\tif (!is_locked_tcp_sock_ops(bpf_sock))\n-\t\treturn -EOPNOTSUPP;\n-\n \t/* 2 byte is the minimal option len except TCPOPT_NOP and\n \t * TCPOPT_EOL which are useless for the bpf prog to learn\n \t * and this helper disallow loading them also.\n@@ -7956,6 +7953,15 @@ BPF_CALL_4(bpf_sock_ops_load_hdr_opt, struct bpf_sock_ops_kern *, bpf_sock,\n \treturn ret;\n }\n \n+BPF_CALL_4(bpf_sock_ops_load_hdr_opt, struct bpf_sock_ops_kern *, bpf_sock,\n+\t void *, search_res, u32, len, u64, flags)\n+{\n+\tif (!is_locked_tcp_sock_ops(bpf_sock))\n+\t\treturn -EOPNOTSUPP;\n+\n+\treturn __bpf_sock_ops_load_hdr_opt(bpf_sock, search_res, len, flags);\n+}\n+\n static const struct bpf_func_proto bpf_sock_ops_load_hdr_opt_proto = {\n \t.func\t\t= bpf_sock_ops_load_hdr_opt,\n \t.gpl_only\t= false,\n@@ -7966,17 +7972,14 @@ static const struct bpf_func_proto bpf_sock_ops_load_hdr_opt_proto = {\n \t.arg4_type\t= ARG_ANYTHING,\n };\n \n-BPF_CALL_4(bpf_sock_ops_store_hdr_opt, struct bpf_sock_ops_kern *, bpf_sock,\n-\t const void *, from, u32, len, u64, flags)\n+int __bpf_sock_ops_store_hdr_opt(struct bpf_sock_ops_kern *bpf_sock,\n+\t\t\t\t const void *from, u32 len, u64 flags)\n {\n \tu8 new_kind, new_kind_len, magic_len = 0, *opend;\n \tconst u8 *op, *new_op, *magic = NULL;\n \tstruct sk_buff *skb;\n \tbool eol;\n \n-\tif (bpf_sock-\u003eop != BPF_SOCK_OPS_WRITE_HDR_OPT_CB)\n-\t\treturn -EPERM;\n-\n \tif (len \u003c 2 || flags)\n \t\treturn -EINVAL;\n \n@@ -8034,6 +8037,15 @@ BPF_CALL_4(bpf_sock_ops_store_hdr_opt, struct bpf_sock_ops_kern *, bpf_sock,\n \treturn 0;\n }\n \n+BPF_CALL_4(bpf_sock_ops_store_hdr_opt, struct bpf_sock_ops_kern *, bpf_sock,\n+\t const void *, from, u32, len, u64, flags)\n+{\n+\tif (bpf_sock-\u003eop != BPF_SOCK_OPS_WRITE_HDR_OPT_CB)\n+\t\treturn -EPERM;\n+\n+\treturn __bpf_sock_ops_store_hdr_opt(bpf_sock, from, len, flags);\n+}\n+\n static const struct bpf_func_proto bpf_sock_ops_store_hdr_opt_proto = {\n \t.func\t\t= bpf_sock_ops_store_hdr_opt,\n \t.gpl_only\t= false,\n@@ -12633,6 +12645,7 @@ static int __init bpf_kfunc_init(void)\n \tret = ret ?: register_btf_kfunc_id_set(BPF_PROG_TYPE_LWT_SEG6LOCAL, \u0026bpf_kfunc_set_skb);\n \tret = ret ?: register_btf_kfunc_id_set(BPF_PROG_TYPE_NETFILTER, \u0026bpf_kfunc_set_skb);\n \tret = ret ?: register_btf_kfunc_id_set(BPF_PROG_TYPE_TRACING, \u0026bpf_kfunc_set_skb);\n+\tret = ret ?: register_btf_kfunc_id_set(BPF_PROG_TYPE_STRUCT_OPS, \u0026bpf_kfunc_set_skb);\n \tret = ret ?: register_btf_kfunc_id_set(BPF_PROG_TYPE_SCHED_CLS, \u0026bpf_kfunc_set_skb_meta);\n \tret = ret ?: register_btf_kfunc_id_set(BPF_PROG_TYPE_SCHED_ACT, \u0026bpf_kfunc_set_skb_meta);\n \tret = ret ?: register_btf_kfunc_id_set(BPF_PROG_TYPE_XDP, \u0026bpf_kfunc_set_xdp);\ndiff --git a/net/ipv4/Makefile b/net/ipv4/Makefile\nindex 06e21c26b76f2e..afbac63d1cb437 100644\n--- a/net/ipv4/Makefile\n+++ b/net/ipv4/Makefile\n@@ -70,6 +70,7 @@ obj-$(CONFIG_TCP_AO) += tcp_ao.o\n \n ifeq ($(CONFIG_BPF_JIT),y)\n obj-$(CONFIG_BPF_SYSCALL) += bpf_tcp_ca.o\n+obj-$(CONFIG_CGROUP_BPF) += bpf_tcp_ops.o\n endif\n \n ifdef CONFIG_GCOV_PROFILE_NETFILTER\ndiff --git a/net/ipv4/af_inet.c b/net/ipv4/af_inet.c\nindex 32d006c1a8eeda..ac8431da67f4dc 100644\n--- a/net/ipv4/af_inet.c\n+++ b/net/ipv4/af_inet.c\n@@ -227,6 +227,7 @@ int __inet_listen_sk(struct sock *sk, int backlog)\n \t\t\treturn err;\n \n \t\ttcp_call_bpf(sk, BPF_SOCK_OPS_TCP_LISTEN_CB, 0, NULL);\n+\t\tbpf_tcp_ops_call(listen, sk);\n \t}\n \treturn 0;\n }\ndiff --git a/net/ipv4/bpf_tcp_ca.c b/net/ipv4/bpf_tcp_ca.c\nindex 791e15063237c9..e224ecafbd6992 100644\n--- a/net/ipv4/bpf_tcp_ca.c\n+++ b/net/ipv4/bpf_tcp_ca.c\n@@ -339,6 +339,22 @@ static struct bpf_struct_ops bpf_tcp_congestion_ops = {\n \t.validate = bpf_tcp_ca_validate,\n \t.name = \"tcp_congestion_ops\",\n \t.cfi_stubs = \u0026__bpf_ops_tcp_congestion_ops,\n+\t/* The struct_ops's function may switch to another struct_ops.\n+\t *\n+\t * For example, bpf_tcp_cc_x-\u003einit() may switch to\n+\t * another tcp_cc_y by calling\n+\t * setsockopt(TCP_CONGESTION, \"tcp_cc_y\").\n+\t * During the switch, bpf_struct_ops_put(tcp_cc_x) is called\n+\t * and its refcount may reach 0 which then free its\n+\t * trampoline image while tcp_cc_x is still running.\n+\t *\n+\t * A vanilla rcu gp is to wait for all bpf-tcp-cc prog\n+\t * to finish. bpf-tcp-cc prog is non sleepable.\n+\t * A rcu_tasks gp is to wait for the last few insn\n+\t * in the tramopline image to finish before releasing\n+\t * the trampoline image.\n+\t */\n+\t.free_after_tasks_rcu_gp = true,\n \t.owner = THIS_MODULE,\n };\n \ndiff --git a/net/ipv4/bpf_tcp_ops.c b/net/ipv4/bpf_tcp_ops.c\nnew file mode 100644\nindex 00000000000000..0c7352517ac317\n--- /dev/null\n+++ b/net/ipv4/bpf_tcp_ops.c\n@@ -0,0 +1,310 @@\n+// SPDX-License-Identifier: GPL-2.0\n+/* Copyright (c) 2026 Meta Platforms, Inc. and affiliates. */\n+\n+#include \u003clinux/bpf.h\u003e\n+#include \u003clinux/btf_ids.h\u003e\n+#include \u003clinux/bpf_verifier.h\u003e\n+#include \u003clinux/filter.h\u003e\n+#include \u003cnet/bpf_sk_storage.h\u003e\n+#include \u003cnet/tcp.h\u003e\n+\n+static int timeout_init_stub(struct sock *sk, struct request_sock *req__nullable)\n+{\n+\tstruct bpf_tramp_run_ctx *ctx =\n+\t\tcontainer_of(current-\u003ebpf_ctx, struct bpf_tramp_run_ctx, run_ctx);\n+\n+\treturn ctx-\u003eretval;\n+}\n+\n+static int rwnd_init_stub(struct sock *sk, struct request_sock *req__nullable)\n+{\n+\tstruct bpf_tramp_run_ctx *ctx =\n+\t\tcontainer_of(current-\u003ebpf_ctx, struct bpf_tramp_run_ctx, run_ctx);\n+\n+\treturn ctx-\u003eretval;\n+}\n+\n+static void active_established_stub(struct sock *sk, struct sk_buff *skb__nullable)\n+{\n+}\n+\n+static void passive_established_stub(struct sock *sk, struct sk_buff *skb)\n+{\n+}\n+\n+static void rto_stub(struct sock *sk)\n+{\n+}\n+\n+static void rtt_stub(struct sock *sk, long mrtt, u32 srtt)\n+{\n+}\n+\n+static void set_state_stub(struct sock *sk, int state)\n+{\n+}\n+\n+static void retrans_stub(struct sock *sk, struct sk_buff *skb, int err)\n+{\n+}\n+\n+static void connect_stub(struct sock *sk)\n+{\n+}\n+\n+static void listen_stub(struct sock *sk)\n+{\n+}\n+\n+static void parse_hdr_stub(struct sock *sk, struct sk_buff *skb)\n+{\n+}\n+\n+static void hdr_opt_len_stub(struct sock *sk, struct sk_buff *skb__nullable,\n+\t\t\t struct request_sock *req__nullable,\n+\t\t\t struct sk_buff *syn_skb__nullable,\n+\t\t\t enum tcp_synack_type synack_type,\n+\t\t\t unsigned int *remaining)\n+{\n+}\n+\n+static void write_hdr_opt_stub(struct sock *sk, struct sk_buff *skb,\n+\t\t\t struct request_sock *req__nullable,\n+\t\t\t struct sk_buff *syn_skb__nullable,\n+\t\t\t enum tcp_synack_type synack_type,\n+\t\t\t u32 opt_off)\n+{\n+}\n+\n+static struct bpf_tcp_ops __bpf_tcp_ops = {\n+\t.timeout_init = timeout_init_stub,\n+\t.rwnd_init = rwnd_init_stub,\n+\t.active_established = active_established_stub,\n+\t.passive_established = passive_established_stub,\n+\t.rto = rto_stub,\n+\t.rtt = rtt_stub,\n+\t.set_state = set_state_stub,\n+\t.retrans = retrans_stub,\n+\t.connect = connect_stub,\n+\t.listen = listen_stub,\n+\t.parse_hdr = parse_hdr_stub,\n+\t.hdr_opt_len = hdr_opt_len_stub,\n+\t.write_hdr_opt = write_hdr_opt_stub,\n+};\n+\n+BPF_CALL_4(bpf_tcp_ops_store_hdr_opt, void *, ctx, const void *, from,\n+\t u32, len, u64, flags)\n+{\n+\tstruct sk_buff *skb = ((struct sk_buff **)ctx)[1];\n+\tstruct bpf_sock_ops_kern sock_ops = {};\n+\tu32 opt_off = ((u64 *)ctx)[5];\n+\tu8 *op, *opend;\n+\n+\t/* bpf_tcp_ops does not keep track of the end of the written TCP header\n+\t * options, so search for it every time the helper is called. The free\n+\t * space is NOP-filled, so a TCPOPT_NOP ends the search rather than being\n+\t * skipped as in a normal option walk in sockops.\n+\t */\n+\top = skb-\u003edata + opt_off;\n+\topend = skb-\u003edata + tcp_hdrlen(skb);\n+\twhile (op \u003c opend \u0026\u0026 *op != TCPOPT_NOP) {\n+\t\tif (*op == TCPOPT_EOL || op + 1 \u003e= opend || op[1] \u003c 2)\n+\t\t\tbreak;\n+\t\top += op[1];\n+\t}\n+\n+\tsock_ops.skb = skb;\n+\tsock_ops.skb_data_end = op;\n+\tsock_ops.remaining_opt_len = opend - op;\n+\n+\treturn __bpf_sock_ops_store_hdr_opt(\u0026sock_ops, from, len, flags);\n+}\n+\n+static const struct bpf_func_proto bpf_tcp_ops_store_hdr_opt_proto = {\n+\t.func\t\t= bpf_tcp_ops_store_hdr_opt,\n+\t.gpl_only\t= false,\n+\t.ret_type\t= RET_INTEGER,\n+\t.arg1_type\t= ARG_PTR_TO_CTX,\n+\t.arg2_type\t= ARG_PTR_TO_MEM | MEM_RDONLY,\n+\t.arg3_type\t= ARG_CONST_SIZE,\n+\t.arg4_type\t= ARG_ANYTHING,\n+};\n+\n+BPF_CALL_4(bpf_tcp_ops_load_hdr_opt, void *, ctx, void *, search_res,\n+\t u32, len, u64, flags)\n+{\n+\tstruct sk_buff *skb = ((struct sk_buff **)ctx)[1];\n+\tstruct bpf_sock_ops_kern sock_ops = {};\n+\n+\t/* No flags supported. In particular BPF_LOAD_HDR_OPT_TCP_SYN, which\n+\t * loads from the saved SYN, is not available because bpf_tcp_ops has no\n+\t * carrier to track the SYN source across the hooks.\n+\t */\n+\tif (flags)\n+\t\treturn -EINVAL;\n+\n+\tsock_ops.skb = skb;\n+\tsock_ops.skb_data_end = skb-\u003edata + tcp_hdrlen(skb);\n+\n+\treturn __bpf_sock_ops_load_hdr_opt(\u0026sock_ops, search_res, len, flags);\n+}\n+\n+static const struct bpf_func_proto bpf_tcp_ops_load_hdr_opt_proto = {\n+\t.func\t\t= bpf_tcp_ops_load_hdr_opt,\n+\t.gpl_only\t= false,\n+\t.ret_type\t= RET_INTEGER,\n+\t.arg1_type\t= ARG_PTR_TO_CTX,\n+\t.arg2_type\t= ARG_PTR_TO_MEM | MEM_WRITE,\n+\t.arg3_type\t= ARG_CONST_SIZE,\n+\t.arg4_type\t= ARG_ANYTHING,\n+};\n+\n+BPF_CALL_3(bpf_tcp_ops_reserve_hdr_opt, void *, ctx, u32, len, u64, flags)\n+{\n+\tunsigned int *remaining = ((unsigned int **)ctx)[5];\n+\n+\tif (flags || len \u003c 2)\n+\t\treturn -EINVAL;\n+\n+\tif (len \u003e *remaining)\n+\t\treturn -ENOSPC;\n+\n+\t*remaining -= len;\n+\treturn 0;\n+}\n+\n+static const struct bpf_func_proto bpf_tcp_ops_reserve_hdr_opt_proto = {\n+\t.func\t\t= bpf_tcp_ops_reserve_hdr_opt,\n+\t.gpl_only\t= false,\n+\t.ret_type\t= RET_INTEGER,\n+\t.arg1_type\t= ARG_PTR_TO_CTX,\n+\t.arg2_type\t= ARG_ANYTHING,\n+\t.arg3_type\t= ARG_ANYTHING,\n+};\n+\n+BPF_CALL_0(bpf_tcp_ops_get_retval)\n+{\n+\tstruct bpf_tramp_run_ctx *ctx =\n+\t\tcontainer_of(current-\u003ebpf_ctx, struct bpf_tramp_run_ctx, run_ctx);\n+\n+\t/* bpf_get_retval() is only exposed to timeout_init/rwnd_init, which\n+\t * always run via bpf_tcp_ops_call_int(). Its run_ctx carries the int\n+\t * return value chained across the bpf_tcp_ops attached to the cgroup\n+\t * and is this program's saved_run_ctx.\n+\t */\n+\tif (WARN_ON_ONCE(!ctx-\u003esaved_run_ctx))\n+\t\treturn 0;\n+\n+\treturn container_of(ctx-\u003esaved_run_ctx, struct bpf_tramp_run_ctx,\n+\t\t\t run_ctx)-\u003eretval;\n+}\n+\n+const struct bpf_func_proto bpf_tcp_ops_get_retval_proto = {\n+\t.func\t\t= bpf_tcp_ops_get_retval,\n+\t.gpl_only\t= false,\n+\t.ret_type\t= RET_INTEGER,\n+};\n+\n+static const struct bpf_func_proto *\n+get_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)\n+{\n+\tu32 moff = prog-\u003eaux-\u003eattach_st_ops_member_off;\n+\n+\tswitch (func_id) {\n+\tcase BPF_FUNC_sk_storage_get:\n+\t\treturn \u0026bpf_sk_storage_get_proto;\n+\tcase BPF_FUNC_sk_storage_delete:\n+\t\treturn \u0026bpf_sk_storage_delete_proto;\n+\tcase BPF_FUNC_setsockopt:\n+\t\t/* The sk may be an unlocked listener (synack path) or NULL\n+\t\t * fullsock; disable for members that can run unlocked.\n+\t\t */\n+\t\tif (moff == offsetof(struct bpf_tcp_ops, rwnd_init) ||\n+\t\t moff == offsetof(struct bpf_tcp_ops, timeout_init) ||\n+\t\t moff == offsetof(struct bpf_tcp_ops, hdr_opt_len) ||\n+\t\t moff == offsetof(struct bpf_tcp_ops, write_hdr_opt))\n+\t\t\treturn NULL;\n+\t\treturn \u0026bpf_sk_setsockopt_proto;\n+\tcase BPF_FUNC_getsockopt:\n+\t\tif (moff == offsetof(struct bpf_tcp_ops, rwnd_init) ||\n+\t\t moff == offsetof(struct bpf_tcp_ops, timeout_init) ||\n+\t\t moff == offsetof(struct bpf_tcp_ops, hdr_opt_len) ||\n+\t\t moff == offsetof(struct bpf_tcp_ops, write_hdr_opt))\n+\t\t\treturn NULL;\n+\t\treturn \u0026bpf_sk_getsockopt_proto;\n+\tcase BPF_FUNC_get_retval:\n+\t\tif (moff == offsetof(struct bpf_tcp_ops, timeout_init) ||\n+\t\t moff == offsetof(struct bpf_tcp_ops, rwnd_init))\n+\t\t\treturn \u0026bpf_tcp_ops_get_retval_proto;\n+\t\treturn NULL;\n+\tcase BPF_FUNC_reserve_hdr_opt:\n+\t\tif (moff == offsetof(struct bpf_tcp_ops, hdr_opt_len))\n+\t\t\treturn \u0026bpf_tcp_ops_reserve_hdr_opt_proto;\n+\t\treturn NULL;\n+\tcase BPF_FUNC_load_hdr_opt:\n+\t\tif (moff == offsetof(struct bpf_tcp_ops, parse_hdr) ||\n+\t\t moff == offsetof(struct bpf_tcp_ops, write_hdr_opt))\n+\t\t\treturn \u0026bpf_tcp_ops_load_hdr_opt_proto;\n+\t\treturn NULL;\n+\tcase BPF_FUNC_store_hdr_opt:\n+\t\tif (moff == offsetof(struct bpf_tcp_ops, write_hdr_opt))\n+\t\t\treturn \u0026bpf_tcp_ops_store_hdr_opt_proto;\n+\t\treturn NULL;\n+\tdefault:\n+\t\treturn bpf_base_func_proto(func_id, prog);\n+\t}\n+}\n+\n+static bool is_valid_access(int off, int size, enum bpf_access_type type,\n+\t\t\t const struct bpf_prog *prog, struct bpf_insn_access_aux *info)\n+{\n+\tif (!bpf_tracing_btf_ctx_access(off, size, type, prog, info))\n+\t\treturn false;\n+\n+\tif (base_type(info-\u003ereg_type) == PTR_TO_BTF_ID \u0026\u0026\n+\t !bpf_type_has_unsafe_modifiers(info-\u003ereg_type) \u0026\u0026\n+\t info-\u003ebtf_id == btf_sock_ids[BTF_SOCK_TYPE_SOCK])\n+\t\t/* promote it to tcp_sock */\n+\t\tinfo-\u003ebtf_id = btf_sock_ids[BTF_SOCK_TYPE_TCP];\n+\n+\treturn true;\n+}\n+\n+static int bpf_tcp_ops_init_member(const struct btf_type *t,\n+\t\t\t\t const struct btf_member *member,\n+\t\t\t\t void *kdata, const void *udata)\n+{\n+\treturn 0;\n+}\n+\n+static int bpf_tcp_ops_init(struct btf *btf)\n+{\n+\treturn 0;\n+}\n+\n+static int bpf_tcp_ops_validate(void *kdata)\n+{\n+\treturn 0;\n+}\n+\n+static const struct bpf_verifier_ops bpf_tcp_ops_verifier = {\n+\t.get_func_proto\t\t= get_func_proto,\n+\t.is_valid_access\t= is_valid_access,\n+};\n+\n+static struct bpf_struct_ops bpf_tcp_ops = {\n+\t.verifier_ops = \u0026bpf_tcp_ops_verifier,\n+\t.init_member = bpf_tcp_ops_init_member,\n+\t.init = bpf_tcp_ops_init,\n+\t.validate = bpf_tcp_ops_validate,\n+\t.name = \"bpf_tcp_ops\",\n+\t.cgroup_atype = CGROUP_TCP_SOCK_OPS,\n+\t.cfi_stubs = \u0026__bpf_tcp_ops,\n+\t.owner = THIS_MODULE,\n+};\n+\n+static int __init __bpf_tcp_ops_init(void)\n+{\n+\treturn register_bpf_struct_ops(\u0026bpf_tcp_ops, bpf_tcp_ops);\n+}\n+late_initcall(__bpf_tcp_ops_init);\ndiff --git a/net/ipv4/tcp.c b/net/ipv4/tcp.c\nindex 455441f1b69490..94ed1ac2abc1bc 100644\n--- a/net/ipv4/tcp.c\n+++ b/net/ipv4/tcp.c\n@@ -2996,6 +2996,7 @@ void tcp_set_state(struct sock *sk, int state)\n \n \tif (BPF_SOCK_OPS_TEST_FLAG(tcp_sk(sk), BPF_SOCK_OPS_STATE_CB_FLAG))\n \t\ttcp_call_bpf_2arg(sk, BPF_SOCK_OPS_STATE_CB, oldstate, state);\n+\tbpf_tcp_ops_call(set_state, sk, state);\n \n \tswitch (state) {\n \tcase TCP_ESTABLISHED:\ndiff --git a/net/ipv4/tcp_input.c b/net/ipv4/tcp_input.c\nindex 61045a8886e485..a3614678913810 100644\n--- a/net/ipv4/tcp_input.c\n+++ b/net/ipv4/tcp_input.c\n@@ -208,6 +208,18 @@ static void bpf_skops_established(struct sock *sk, int bpf_op,\n }\n #endif\n \n+static void bpf_tcp_ops_parse_hdr(struct sock *sk, struct sk_buff *skb)\n+{\n+\tswitch (sk-\u003esk_state) {\n+\tcase TCP_SYN_RECV:\n+\tcase TCP_SYN_SENT:\n+\tcase TCP_LISTEN:\n+\t\treturn;\n+\t}\n+\n+\tbpf_tcp_ops_call(parse_hdr, sk, skb);\n+}\n+\n static __cold void tcp_gro_dev_warn(const struct sock *sk, const struct sk_buff *skb,\n \t\t\t\t unsigned int len)\n {\n@@ -6431,6 +6443,7 @@ static bool tcp_validate_incoming(struct sock *sk, struct sk_buff *skb,\n \n pass:\n \tbpf_skops_parse_hdr(sk, skb);\n+\tbpf_tcp_ops_parse_hdr(sk, skb);\n \n \treturn true;\n \n@@ -6694,6 +6707,10 @@ void tcp_init_transfer(struct sock *sk, int bpf_op, struct sk_buff *skb)\n \ttp-\u003esnd_cwnd_stamp = tcp_jiffies32;\n \n \tbpf_skops_established(sk, bpf_op, skb);\n+\tif (bpf_op == BPF_SOCK_OPS_ACTIVE_ESTABLISHED_CB)\n+\t\tbpf_tcp_ops_call(active_established, sk, skb);\n+\telse\n+\t\tbpf_tcp_ops_call(passive_established, sk, skb);\n \t/* Initialize congestion control unless BPF initialized it already: */\n \tif (!icsk-\u003eicsk_ca_initialized)\n \t\ttcp_init_congestion_control(sk);\ndiff --git a/net/ipv4/tcp_output.c b/net/ipv4/tcp_output.c\nindex 26dd751ec72abf..580652d0a135d1 100644\n--- a/net/ipv4/tcp_output.c\n+++ b/net/ipv4/tcp_output.c\n@@ -573,6 +573,13 @@ static void bpf_skops_write_hdr_opt(struct sock *sk, struct sk_buff *skb,\n \tif (nr_written \u003c max_opt_len)\n \t\tmemset(skb-\u003edata + first_opt_off + nr_written, TCPOPT_NOP,\n \t\t max_opt_len - nr_written);\n+\n+\t/* bpf_tcp_ops portion is NOP-filled (everything past the sockops\n+\t * writer's bytes). The writer find the append point by scanning from\n+\t * first_opt_off + nr_written to the first NOP.\n+\t */\n+\tbpf_tcp_ops_call(write_hdr_opt, sk, skb, req, syn_skb, synack_type,\n+\t\t\t first_opt_off + nr_written);\n }\n #else\n static u32 bpf_skops_hdr_opt_len(struct sock *sk, struct sk_buff *skb,\n@@ -594,6 +601,32 @@ static void bpf_skops_write_hdr_opt(struct sock *sk, struct sk_buff *skb,\n }\n #endif\n \n+static u32 bpf_tcp_ops_hdr_opt_len(struct sock *sk, struct sk_buff *skb,\n+\t\t\t\t struct request_sock *req,\n+\t\t\t\t struct sk_buff *syn_skb,\n+\t\t\t\t enum tcp_synack_type synack_type,\n+\t\t\t\t struct tcp_out_options *opts,\n+\t\t\t\t u32 remaining)\n+{\n+\tunsigned int remaining_out = remaining, reserved;\n+\n+\tif (!remaining)\n+\t\treturn 0;\n+\n+\t/* bpf_tcp_ops_reserve_hdr_opt() reserves space via remaining_out */\n+\tbpf_tcp_ops_call(hdr_opt_len, sk, skb, req, syn_skb, synack_type, \u0026remaining_out);\n+\n+\treserved = remaining - remaining_out;\n+\tif (!reserved)\n+\t\treturn remaining;\n+\n+\t/* round up to 4 bytes */\n+\treserved = (reserved + 3) \u0026 ~3;\n+\n+\topts-\u003ebpf_opt_len += reserved;\n+\treturn remaining - reserved;\n+}\n+\n static __be32 *process_tcp_ao_options(struct tcp_sock *tp,\n \t\t\t\t const struct tcp_request_sock *tcprsk,\n \t\t\t\t struct tcp_out_options *opts,\n@@ -1053,6 +1086,8 @@ static unsigned int tcp_syn_options(struct sock *sk, struct sk_buff *skb,\n \n \tremaining = bpf_skops_hdr_opt_len(sk, skb, NULL, NULL, 0, opts,\n \t\t\t\t\t remaining);\n+\tremaining = bpf_tcp_ops_hdr_opt_len(sk, skb, NULL, NULL, 0, opts,\n+\t\t\t\t\t remaining);\n \n \treturn MAX_TCP_OPTION_SPACE - remaining;\n }\n@@ -1141,6 +1176,8 @@ static unsigned int tcp_synack_options(const struct sock *sk,\n \n \tremaining = bpf_skops_hdr_opt_len((struct sock *)sk, skb, req, syn_skb,\n \t\t\t\t\t synack_type, opts, remaining);\n+\tremaining = bpf_tcp_ops_hdr_opt_len((struct sock *)sk, skb, req, syn_skb,\n+\t\t\t\t\t synack_type, opts, remaining);\n \n \treturn MAX_TCP_OPTION_SPACE - remaining;\n }\n@@ -1244,6 +1281,15 @@ static unsigned int tcp_established_options(struct sock *sk, struct sk_buff *skb\n \t\tsize = MAX_TCP_OPTION_SPACE - remaining;\n \t}\n \n+\tif (cgroup_bpf_enabled(CGROUP_TCP_SOCK_OPS)) {\n+\t\tunsigned int remaining = MAX_TCP_OPTION_SPACE - size;\n+\n+\t\tremaining = bpf_tcp_ops_hdr_opt_len(sk, skb, NULL, NULL, 0, opts,\n+\t\t\t\t\t\t remaining);\n+\n+\t\tsize = MAX_TCP_OPTION_SPACE - remaining;\n+\t}\n+\n \treturn size;\n }\n \n@@ -3678,6 +3724,7 @@ int __tcp_retransmit_skb(struct sock *sk, struct sk_buff *skb, int segs)\n \tif (BPF_SOCK_OPS_TEST_FLAG(tp, BPF_SOCK_OPS_RETRANS_CB_FLAG))\n \t\ttcp_call_bpf_3arg(sk, BPF_SOCK_OPS_RETRANS_CB,\n \t\t\t\t TCP_SKB_CB(skb)-\u003eseq, segs, err);\n+\tbpf_tcp_ops_call(retrans, sk, skb, err);\n \n \tif (unlikely(err) \u0026\u0026 err != -EBUSY)\n \t\tNET_ADD_STATS(sock_net(sk), LINUX_MIB_TCPRETRANSFAIL, segs);\n@@ -4298,6 +4345,7 @@ int tcp_connect(struct sock *sk)\n \tint err;\n \n \ttcp_call_bpf(sk, BPF_SOCK_OPS_TCP_CONNECT_CB, 0, NULL);\n+\tbpf_tcp_ops_call(connect, sk);\n \n #if defined(CONFIG_TCP_MD5SIG) \u0026\u0026 defined(CONFIG_TCP_AO)\n \t/* Has to be checked late, after setting daddr/saddr/ops.\ndiff --git a/net/ipv4/tcp_timer.c b/net/ipv4/tcp_timer.c\nindex bf171b5e1eb306..4337627ee0ea25 100644\n--- a/net/ipv4/tcp_timer.c\n+++ b/net/ipv4/tcp_timer.c\n@@ -290,6 +290,7 @@ static int tcp_write_timeout(struct sock *sk)\n \t\ttcp_call_bpf_3arg(sk, BPF_SOCK_OPS_RTO_CB,\n \t\t\t\t icsk-\u003eicsk_retransmits,\n \t\t\t\t icsk-\u003eicsk_rto, (int)expired);\n+\tbpf_tcp_ops_call(rto, sk);\n \n \tif (expired) {\n \t\t/* Has it gone just too far? */\ndiff --git a/net/sched/bpf_qdisc.c b/net/sched/bpf_qdisc.c\nindex 098ca02aed89c2..5691c13781a8b0 100644\n--- a/net/sched/bpf_qdisc.c\n+++ b/net/sched/bpf_qdisc.c\n@@ -280,7 +280,6 @@ BTF_KFUNCS_START(qdisc_kfunc_ids)\n BTF_ID_FLAGS(func, bpf_skb_get_hash)\n BTF_ID_FLAGS(func, bpf_kfree_skb, KF_RELEASE)\n BTF_ID_FLAGS(func, bpf_qdisc_skb_drop, KF_RELEASE)\n-BTF_ID_FLAGS(func, bpf_dynptr_from_skb)\n BTF_ID_FLAGS(func, bpf_qdisc_watchdog_schedule)\n BTF_ID_FLAGS(func, bpf_qdisc_init_prologue)\n BTF_ID_FLAGS(func, bpf_qdisc_reset_destroy_epilogue)\n@@ -290,7 +289,6 @@ BTF_KFUNCS_END(qdisc_kfunc_ids)\n BTF_SET_START(qdisc_common_kfunc_set)\n BTF_ID(func, bpf_skb_get_hash)\n BTF_ID(func, bpf_kfree_skb)\n-BTF_ID(func, bpf_dynptr_from_skb)\n BTF_SET_END(qdisc_common_kfunc_set)\n \n BTF_SET_START(qdisc_enqueue_kfunc_set)\ndiff --git a/tools/include/uapi/linux/bpf.h b/tools/include/uapi/linux/bpf.h\nindex 89b36de5fdbb6d..45b9ee29e46174 100644\n--- a/tools/include/uapi/linux/bpf.h\n+++ b/tools/include/uapi/linux/bpf.h\n@@ -1756,7 +1756,7 @@ union bpf_attr {\n \t\t\t__u32\tprog_cnt;\n \t\t\t__u32\tcount;\n \t\t};\n-\t\t__u32\t\t:32;\n+\t\t__u32\t\ttype_id;\n \t\t/* output: per-program attach_flags.\n \t\t * not allowed to be set during effective query.\n \t\t */\n@@ -4799,15 +4799,18 @@ union bpf_attr {\n * \t\tThe non-negative copied *buf* length equal to or less than\n * \t\t*size* on success, or a negative error in case of failure.\n *\n- * long bpf_load_hdr_opt(struct bpf_sock_ops *skops, void *searchby_res, u32 len, u64 flags)\n+ * long bpf_load_hdr_opt(void *ctx, void *searchby_res, u32 len, u64 flags)\n *\tDescription\n *\t\tLoad header option. Support reading a particular TCP header\n- *\t\toption for bpf program (**BPF_PROG_TYPE_SOCK_OPS**).\n+ *\t\toption for bpf program (**BPF_PROG_TYPE_SOCK_OPS**). For the\n+ *\t\t**bpf_tcp_ops** struct_ops, this helper can be called from the\n+ *\t\t**parse_hdr**\\ () and **write_hdr_opt**\\ () operators.\n *\n- *\t\tIf *flags* is 0, it will search the option from the\n- *\t\t*skops*\\ **-\u003eskb_data**. The comment in **struct bpf_sock_ops**\n- *\t\thas details on what skb_data contains under different\n- *\t\t*skops*\\ **-\u003eop**.\n+ *\t\tIf *flags* is 0, it will search the option from the packet\n+ *\t\tassociated with the current operation. For\n+ *\t\t**BPF_PROG_TYPE_SOCK_OPS**, the comment in\n+ *\t\t**struct bpf_sock_ops** has details on what skb_data\n+ *\t\tcontains under different *op*.\n *\n *\t\tThe first byte of the *searchby_res* specifies the\n *\t\tkind that it wants to search.\n@@ -4840,6 +4843,8 @@ union bpf_attr {\n *\n *\t\t* **BPF_LOAD_HDR_OPT_TCP_SYN** to search from the\n *\t\t saved_syn packet or the just-received syn packet.\n+ *\t\t Not supported by the **bpf_tcp_ops** struct_ops, which\n+ *\t\t rejects all flags.\n *\n *\tReturn\n *\t\t\u003e 0 when found, the header option is copied to *searchby_res*.\n@@ -4860,9 +4865,9 @@ union bpf_attr {\n *\t\tpacket.\n *\n *\t\t**-EPERM** if the helper cannot be used under the current\n- *\t\t*skops*\\ **-\u003eop**.\n+ *\t\toperation.\n *\n- * long bpf_store_hdr_opt(struct bpf_sock_ops *skops, const void *from, u32 len, u64 flags)\n+ * long bpf_store_hdr_opt(void *ctx, const void *from, u32 len, u64 flags)\n *\tDescription\n *\t\tStore header option. The data will be copied\n *\t\tfrom buffer *from* with length *len* to the TCP header.\n@@ -4878,7 +4883,9 @@ union bpf_attr {\n *\t\tby searching the same option in the outgoing skb.\n *\n *\t\tThis helper can only be called during\n- *\t\t**BPF_SOCK_OPS_WRITE_HDR_OPT_CB**.\n+ *\t\t**BPF_SOCK_OPS_WRITE_HDR_OPT_CB**, or from the\n+ *\t\t**write_hdr_opt**\\ () operator of the **bpf_tcp_ops**\n+ *\t\tstruct_ops.\n *\n *\tReturn\n *\t\t0 on success, or negative error in case of failure:\n@@ -4893,9 +4900,9 @@ union bpf_attr {\n *\t\t**-EFAULT** on failure to parse the existing header options.\n *\n *\t\t**-EPERM** if the helper cannot be used under the current\n- *\t\t*skops*\\ **-\u003eop**.\n+ *\t\toperation.\n *\n- * long bpf_reserve_hdr_opt(struct bpf_sock_ops *skops, u32 len, u64 flags)\n+ * long bpf_reserve_hdr_opt(void *ctx, u32 len, u64 flags)\n *\tDescription\n *\t\tReserve *len* bytes for the bpf header option. The\n *\t\tspace will be used by **bpf_store_hdr_opt**\\ () later in\n@@ -4905,7 +4912,9 @@ union bpf_attr {\n *\t\tthe total number of bytes will be reserved.\n *\n *\t\tThis helper can only be called during\n- *\t\t**BPF_SOCK_OPS_HDR_OPT_LEN_CB**.\n+ *\t\t**BPF_SOCK_OPS_HDR_OPT_LEN_CB**, or from the\n+ *\t\t**hdr_opt_len**\\ () operator of the **bpf_tcp_ops**\n+ *\t\tstruct_ops.\n *\n *\tReturn\n *\t\t0 on success, or negative error in case of failure:\n@@ -4915,7 +4924,7 @@ union bpf_attr {\n *\t\t**-ENOSPC** if there is not enough space in the header.\n *\n *\t\t**-EPERM** if the helper cannot be used under the current\n- *\t\t*skops*\\ **-\u003eop**.\n+ *\t\toperation.\n *\n * void *bpf_inode_storage_get(struct bpf_map *map, void *inode, void *value, u64 flags)\n *\tDescription\n@@ -6815,6 +6824,8 @@ struct bpf_link_info {\n \t\t} xdp;\n \t\tstruct {\n \t\t\t__u32 map_id;\n+\t\t\t__u32 :32;\n+\t\t\t__u64 cgroup_id;\n \t\t} struct_ops;\n \t\tstruct {\n \t\t\t__u32 pf;\ndiff --git a/tools/lib/bpf/bpf.c b/tools/lib/bpf/bpf.c\nindex 96819c082c772d..a9de7f107cf7bb 100644\n--- a/tools/lib/bpf/bpf.c\n+++ b/tools/lib/bpf/bpf.c\n@@ -934,6 +934,7 @@ int bpf_link_create(int prog_fd, int target_fd,\n \tcase BPF_CGROUP_GETSOCKOPT:\n \tcase BPF_CGROUP_SETSOCKOPT:\n \tcase BPF_LSM_CGROUP:\n+\tcase BPF_STRUCT_OPS:\n \t\trelative_fd = OPTS_GET(opts, cgroup.relative_fd, 0);\n \t\trelative_id = OPTS_GET(opts, cgroup.relative_id, 0);\n \t\tif (relative_fd \u0026\u0026 relative_id)\n@@ -1056,6 +1057,7 @@ int bpf_prog_query_opts(int target, enum bpf_attach_type type,\n \tattr.query.attach_type\t\t= type;\n \tattr.query.query_flags\t\t= OPTS_GET(opts, query_flags, 0);\n \tattr.query.count\t\t= OPTS_GET(opts, count, 0);\n+\tattr.query.type_id\t\t= OPTS_GET(opts, type_id, 0);\n \tattr.query.prog_ids\t\t= ptr_to_u64(OPTS_GET(opts, prog_ids, NULL));\n \tattr.query.link_ids\t\t= ptr_to_u64(OPTS_GET(opts, link_ids, NULL));\n \tattr.query.prog_attach_flags\t= ptr_to_u64(OPTS_GET(opts, prog_attach_flags, NULL));\ndiff --git a/tools/lib/bpf/bpf.h b/tools/lib/bpf/bpf.h\nindex 7534a593edae21..490e8cb4ba537d 100644\n--- a/tools/lib/bpf/bpf.h\n+++ b/tools/lib/bpf/bpf.h\n@@ -637,9 +637,10 @@ struct bpf_prog_query_opts {\n \t__u32 *link_ids;\n \t__u32 *link_attach_flags;\n \t__u64 revision;\n+\t__u32 type_id;\n \tsize_t :0;\n };\n-#define bpf_prog_query_opts__last_field revision\n+#define bpf_prog_query_opts__last_field type_id\n \n /**\n * @brief **bpf_prog_query_opts()** queries the BPF programs and BPF links\ndiff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c\nindex 1368752aa13c39..17f8466e33fa37 100644\n--- a/tools/lib/bpf/libbpf.c\n+++ b/tools/lib/bpf/libbpf.c\n@@ -14104,6 +14104,65 @@ struct bpf_link *bpf_map__attach_struct_ops(const struct bpf_map *map)\n \treturn \u0026link-\u003elink;\n }\n \n+struct bpf_link *bpf_map__attach_cgroup_opts(const struct bpf_map *map, int cgroup_fd,\n+\t\t\t\t\t const struct bpf_cgroup_opts *opts)\n+{\n+\tLIBBPF_OPTS(bpf_link_create_opts, link_create_opts);\n+\tstruct bpf_link_struct_ops *link;\n+\t__u32 relative_id, zero = 0;\n+\tint err, fd, relative_fd;\n+\n+\tif (!OPTS_VALID(opts, bpf_cgroup_opts))\n+\t\treturn libbpf_err_ptr(-EINVAL);\n+\n+\tif (!bpf_map__is_struct_ops(map)) {\n+\t\tpr_warn(\"map '%s': can't attach non-struct_ops map\\n\", map-\u003ename);\n+\t\treturn libbpf_err_ptr(-EINVAL);\n+\t}\n+\n+\tif (map-\u003efd \u003c 0) {\n+\t\tpr_warn(\"map '%s': can't attach BPF map without FD (was it created?)\\n\", map-\u003ename);\n+\t\treturn libbpf_err_ptr(-EINVAL);\n+\t}\n+\n+\trelative_id = OPTS_GET(opts, relative_id, 0);\n+\trelative_fd = OPTS_GET(opts, relative_fd, 0);\n+\n+\tif (relative_fd \u0026\u0026 relative_id) {\n+\t\tpr_warn(\"map '%s': relative_fd and relative_id cannot be set at the same time\\n\",\n+\t\t\tmap-\u003ename);\n+\t\treturn libbpf_err_ptr(-EINVAL);\n+\t}\n+\n+\tlink_create_opts.cgroup.expected_revision = OPTS_GET(opts, expected_revision, 0);\n+\tlink_create_opts.cgroup.relative_fd = relative_fd;\n+\tlink_create_opts.cgroup.relative_id = relative_id;\n+\tlink_create_opts.flags = OPTS_GET(opts, flags, 0);\n+\n+\tlink = calloc(1, sizeof(*link));\n+\tif (!link)\n+\t\treturn libbpf_err_ptr(-ENOMEM);\n+\n+\terr = bpf_map_update_elem(map-\u003efd, \u0026zero, map-\u003est_ops-\u003ekern_vdata, 0);\n+\tif (err \u0026\u0026 err != -EBUSY) {\n+\t\tfree(link);\n+\t\treturn libbpf_err_ptr(err);\n+\t}\n+\n+\tlink-\u003elink.detach = bpf_link__detach_struct_ops;\n+\n+\tfd = bpf_link_create(map-\u003efd, cgroup_fd, BPF_STRUCT_OPS, \u0026link_create_opts);\n+\tif (fd \u003c 0) {\n+\t\tfree(link);\n+\t\treturn libbpf_err_ptr(fd);\n+\t}\n+\n+\tlink-\u003elink.fd = fd;\n+\tlink-\u003emap_fd = map-\u003efd;\n+\n+\treturn \u0026link-\u003elink;\n+}\n+\n /*\n * Swap the back struct_ops of a link with a new struct_ops map.\n */\ndiff --git a/tools/lib/bpf/libbpf.h b/tools/lib/bpf/libbpf.h\nindex b965ad57154036..0e5f4e9bba41aa 100644\n--- a/tools/lib/bpf/libbpf.h\n+++ b/tools/lib/bpf/libbpf.h\n@@ -960,6 +960,9 @@ bpf_program__attach_cgroup_opts(const struct bpf_program *prog, int cgroup_fd,\n struct bpf_map;\n \n LIBBPF_API struct bpf_link *bpf_map__attach_struct_ops(const struct bpf_map *map);\n+LIBBPF_API struct bpf_link *bpf_map__attach_cgroup_opts(const struct bpf_map *map,\n+\t\t\t\t\t\t\tint cgroup_fd,\n+\t\t\t\t\t\t\tconst struct bpf_cgroup_opts *opts);\n LIBBPF_API int bpf_link__update_map(struct bpf_link *link, const struct bpf_map *map);\n \n struct bpf_iter_attach_opts {\ndiff --git a/tools/lib/bpf/libbpf.map b/tools/lib/bpf/libbpf.map\nindex b731df19ae6926..1b01d49e58eb13 100644\n--- a/tools/lib/bpf/libbpf.map\n+++ b/tools/lib/bpf/libbpf.map\n@@ -462,3 +462,8 @@ LIBBPF_1.8.0 {\n \t\tbpf_program__clone;\n \t\tbtf__new_empty_opts;\n } LIBBPF_1.7.0;\n+\n+LIBBPF_1.9.0 {\n+\tglobal:\n+\t\tbpf_map__attach_cgroup_opts;\n+} LIBBPF_1.8.0;\ndiff --git a/tools/lib/bpf/libbpf_version.h b/tools/lib/bpf/libbpf_version.h\nindex c446c0cd8cf92e..57b74ef3618c10 100644\n--- a/tools/lib/bpf/libbpf_version.h\n+++ b/tools/lib/bpf/libbpf_version.h\n@@ -4,6 +4,6 @@\n #define __LIBBPF_VERSION_H\n \n #define LIBBPF_MAJOR_VERSION 1\n-#define LIBBPF_MINOR_VERSION 8\n+#define LIBBPF_MINOR_VERSION 9\n \n #endif /* __LIBBPF_VERSION_H */\ndiff --git a/tools/testing/selftests/bpf/prog_tests/bpf_tcp_ops.c b/tools/testing/selftests/bpf/prog_tests/bpf_tcp_ops.c\nnew file mode 100644\nindex 00000000000000..4d087bdc46135e\n--- /dev/null\n+++ b/tools/testing/selftests/bpf/prog_tests/bpf_tcp_ops.c\n@@ -0,0 +1,554 @@\n+// SPDX-License-Identifier: GPL-2.0\n+/* Copyright (c) 2026 Meta Platforms, Inc. and affiliates. */\n+\n+#include \u003ctest_progs.h\u003e\n+#include \u003cnetwork_helpers.h\u003e\n+#include \u003cbpf/btf.h\u003e\n+#include \"cgroup_helpers.h\"\n+#include \"bpf_tcp_ops.skel.h\"\n+\n+#define CGROUP_PATH\t\"/bpf_tcp_ops\"\n+#define TEST_NETNS\t\"bpf_tcp_ops\"\n+\n+static __s32 get_bpf_tcp_ops_type_id(void)\n+{\n+\tstruct btf *vmlinux_btf;\n+\t__s32 type_id;\n+\n+\tvmlinux_btf = btf__load_vmlinux_btf();\n+\tif (!ASSERT_OK_PTR(vmlinux_btf, \"load_vmlinux_btf\"))\n+\t\treturn -1;\n+\n+\ttype_id = btf__find_by_name_kind(vmlinux_btf, \"bpf_tcp_ops\", BTF_KIND_STRUCT);\n+\tbtf__free(vmlinux_btf);\n+\n+\tASSERT_GT(type_id, 0, \"find_bpf_tcp_ops\");\n+\treturn type_id;\n+}\n+\n+static void reset_order(struct bpf_tcp_ops *skel)\n+{\n+\tmemset(skel-\u003ebss-\u003elisten_order, 0, sizeof(skel-\u003ebss-\u003elisten_order));\n+\tmemset(skel-\u003ebss-\u003econnect_order, 0, sizeof(skel-\u003ebss-\u003econnect_order));\n+\tskel-\u003ebss-\u003elisten_cnt = 0;\n+\tskel-\u003ebss-\u003econnect_cnt = 0;\n+}\n+\n+static void do_listen_connect(int family)\n+{\n+\tconst char *addr = family == AF_INET ? \"127.0.0.1\" : \"::1\";\n+\tint server_fd, client_fd;\n+\n+\tserver_fd = start_server(family, SOCK_STREAM, addr, 0, 0);\n+\tif (!ASSERT_GE(server_fd, 0, \"start_server\"))\n+\t\treturn;\n+\n+\tclient_fd = connect_to_fd(server_fd, 0);\n+\tif (ASSERT_OK_FD(client_fd, \"connect_to_fd\"))\n+\t\tclose(client_fd);\n+\n+\tclose(server_fd);\n+}\n+\n+/*\n+ * Attach ops1 and ops2 normally (in that order), then ops3 with\n+ * BPF_F_PREORDER. Expected execution order: [3, 1, 2] — ops3 runs\n+ * first despite being attached last, ops1 before ops2 by attach order.\n+ */\n+static void test_order(int cgroup_fd, struct bpf_tcp_ops *skel, int family)\n+{\n+\tLIBBPF_OPTS(bpf_cgroup_opts, preorder_opts, .flags = BPF_F_PREORDER);\n+\tstruct bpf_link *link1 = NULL, *link2 = NULL, *link3 = NULL;\n+\n+\tlink1 = bpf_map__attach_cgroup_opts(skel-\u003emaps.tcp_ops1, cgroup_fd, NULL);\n+\tif (!ASSERT_OK_PTR(link1, \"attach_ops1\"))\n+\t\tgoto done;\n+\n+\tlink2 = bpf_map__attach_cgroup_opts(skel-\u003emaps.tcp_ops2, cgroup_fd, NULL);\n+\tif (!ASSERT_OK_PTR(link2, \"attach_ops2\"))\n+\t\tgoto done;\n+\n+\tlink3 = bpf_map__attach_cgroup_opts(skel-\u003emaps.tcp_ops3, cgroup_fd,\n+\t\t\t\t\t \u0026preorder_opts);\n+\tif (!ASSERT_OK_PTR(link3, \"attach_ops3_preorder\"))\n+\t\tgoto done;\n+\n+\treset_order(skel);\n+\tdo_listen_connect(family);\n+\n+\tASSERT_EQ(skel-\u003ebss-\u003elisten_cnt, 3, \"listen_cnt\");\n+\tASSERT_EQ(skel-\u003ebss-\u003elisten_order[0], 3, \"listen_order[0]\");\n+\tASSERT_EQ(skel-\u003ebss-\u003elisten_order[1], 1, \"listen_order[1]\");\n+\tASSERT_EQ(skel-\u003ebss-\u003elisten_order[2], 2, \"listen_order[2]\");\n+\n+\tASSERT_EQ(skel-\u003ebss-\u003econnect_cnt, 3, \"connect_cnt\");\n+\tASSERT_EQ(skel-\u003ebss-\u003econnect_order[0], 3, \"connect_order[0]\");\n+\tASSERT_EQ(skel-\u003ebss-\u003econnect_order[1], 1, \"connect_order[1]\");\n+\tASSERT_EQ(skel-\u003ebss-\u003econnect_order[2], 2, \"connect_order[2]\");\n+\n+done:\n+\tbpf_link__destroy(link3);\n+\tbpf_link__destroy(link2);\n+\tbpf_link__destroy(link1);\n+}\n+\n+static void run_order_subtest(void)\n+{\n+\tstruct bpf_tcp_ops *skel = NULL;\n+\tstruct netns_obj *ns = NULL;\n+\tint cgroup_fd;\n+\n+\tcgroup_fd = test__join_cgroup(CGROUP_PATH);\n+\tif (!ASSERT_GE(cgroup_fd, 0, \"join_cgroup\"))\n+\t\treturn;\n+\n+\tns = netns_new(TEST_NETNS, true);\n+\tif (!ASSERT_OK_PTR(ns, \"netns_new\"))\n+\t\tgoto done;\n+\n+\tskel = bpf_tcp_ops__open_and_load();\n+\tif (!ASSERT_OK_PTR(skel, \"open_and_load\"))\n+\t\tgoto done;\n+\n+\ttest_order(cgroup_fd, skel, AF_INET);\n+\ttest_order(cgroup_fd, skel, AF_INET6);\n+\n+done:\n+\tbpf_tcp_ops__destroy(skel);\n+\tnetns_free(ns);\n+\tclose(cgroup_fd);\n+}\n+\n+/*\n+ * Position a new attachment relative to an existing one. Attach ops1, then\n+ * ops2 with BPF_F_BEFORE ops1, then ops3 with BPF_F_AFTER ops2. Expected\n+ * execution order: [2, 3, 1]. For struct_ops, relative_fd refers to a link\n+ * fd, so BPF_F_LINK must be set.\n+ */\n+static void test_before_after(int cgroup_fd, struct bpf_tcp_ops *skel)\n+{\n+\tstruct bpf_link *link1 = NULL, *link2 = NULL, *link3 = NULL;\n+\tLIBBPF_OPTS(bpf_cgroup_opts, opts);\n+\n+\tlink1 = bpf_map__attach_cgroup_opts(skel-\u003emaps.tcp_ops1, cgroup_fd, NULL);\n+\tif (!ASSERT_OK_PTR(link1, \"attach_ops1\"))\n+\t\tgoto done;\n+\n+\topts.flags = BPF_F_BEFORE | BPF_F_LINK;\n+\topts.relative_fd = bpf_link__fd(link1);\n+\tlink2 = bpf_map__attach_cgroup_opts(skel-\u003emaps.tcp_ops2, cgroup_fd, \u0026opts);\n+\tif (!ASSERT_OK_PTR(link2, \"attach_ops2_before\"))\n+\t\tgoto done;\n+\n+\topts.flags = BPF_F_AFTER | BPF_F_LINK;\n+\topts.relative_fd = bpf_link__fd(link2);\n+\tlink3 = bpf_map__attach_cgroup_opts(skel-\u003emaps.tcp_ops3, cgroup_fd, \u0026opts);\n+\tif (!ASSERT_OK_PTR(link3, \"attach_ops3_after\"))\n+\t\tgoto done;\n+\n+\treset_order(skel);\n+\tdo_listen_connect(AF_INET6);\n+\n+\tASSERT_EQ(skel-\u003ebss-\u003elisten_cnt, 3, \"listen_cnt\");\n+\tASSERT_EQ(skel-\u003ebss-\u003elisten_order[0], 2, \"listen_order[0]\");\n+\tASSERT_EQ(skel-\u003ebss-\u003elisten_order[1], 3, \"listen_order[1]\");\n+\tASSERT_EQ(skel-\u003ebss-\u003elisten_order[2], 1, \"listen_order[2]\");\n+\n+done:\n+\tbpf_link__destroy(link3);\n+\tbpf_link__destroy(link2);\n+\tbpf_link__destroy(link1);\n+}\n+\n+static void run_before_after_subtest(void)\n+{\n+\tstruct bpf_tcp_ops *skel = NULL;\n+\tstruct netns_obj *ns = NULL;\n+\tint cgroup_fd;\n+\n+\tcgroup_fd = test__join_cgroup(CGROUP_PATH);\n+\tif (!ASSERT_GE(cgroup_fd, 0, \"join_cgroup\"))\n+\t\treturn;\n+\n+\tns = netns_new(TEST_NETNS, true);\n+\tif (!ASSERT_OK_PTR(ns, \"netns_new\"))\n+\t\tgoto done;\n+\n+\tskel = bpf_tcp_ops__open_and_load();\n+\tif (!ASSERT_OK_PTR(skel, \"open_and_load\"))\n+\t\tgoto done;\n+\n+\ttest_before_after(cgroup_fd, skel);\n+\n+done:\n+\tbpf_tcp_ops__destroy(skel);\n+\tnetns_free(ns);\n+\tclose(cgroup_fd);\n+}\n+\n+static void test_query(int cgroup_fd, struct bpf_tcp_ops *skel)\n+{\n+\tstruct bpf_map_info info = {};\n+\t__u32 info_len = sizeof(info);\n+\tLIBBPF_OPTS(bpf_prog_query_opts, query_opts);\n+\tstruct bpf_link *link1 = NULL, *link2 = NULL;\n+\t__u32 map1_id, map2_id, map_ids[2] = {};\n+\t__s32 type_id;\n+\n+\ttype_id = get_bpf_tcp_ops_type_id();\n+\tif (type_id \u003c= 0)\n+\t\treturn;\n+\n+\tbpf_map_get_info_by_fd(bpf_map__fd(skel-\u003emaps.tcp_ops1), \u0026info, \u0026info_len);\n+\tmap1_id = info.id;\n+\n+\tbpf_map_get_info_by_fd(bpf_map__fd(skel-\u003emaps.tcp_ops2), \u0026info, \u0026info_len);\n+\tmap2_id = info.id;\n+\n+\tlink1 = bpf_map__attach_cgroup_opts(skel-\u003emaps.tcp_ops1, cgroup_fd, NULL);\n+\tif (!ASSERT_OK_PTR(link1, \"attach_ops1\"))\n+\t\tgoto done;\n+\n+\tlink2 = bpf_map__attach_cgroup_opts(skel-\u003emaps.tcp_ops2, cgroup_fd, NULL);\n+\tif (!ASSERT_OK_PTR(link2, \"attach_ops2\"))\n+\t\tgoto done;\n+\n+\t/* query effective: expect 2 entries in attachment order */\n+\tquery_opts.type_id = type_id;\n+\tquery_opts.prog_ids = map_ids;\n+\tquery_opts.count = ARRAY_SIZE(map_ids);\n+\tquery_opts.query_flags = BPF_F_QUERY_EFFECTIVE;\n+\tASSERT_OK(bpf_prog_query_opts(cgroup_fd, BPF_STRUCT_OPS, \u0026query_opts),\n+\t\t \"query_effective\");\n+\tASSERT_EQ(query_opts.count, 2, \"query_effective_count\");\n+\tASSERT_EQ(map_ids[0], map1_id, \"map_ids[0]\");\n+\tASSERT_EQ(map_ids[1], map2_id, \"map_ids[1]\");\n+\n+\t/* query attached (non-effective): expect 2 entries */\n+\tmemset(map_ids, 0, sizeof(map_ids));\n+\tquery_opts.query_flags = 0;\n+\tquery_opts.count = ARRAY_SIZE(map_ids);\n+\tASSERT_OK(bpf_prog_query_opts(cgroup_fd, BPF_STRUCT_OPS, \u0026query_opts),\n+\t\t \"query_attached\");\n+\tASSERT_EQ(query_opts.count, 2, \"query_attached_count\");\n+\tASSERT_EQ(map_ids[0], map1_id, \"attached_map_ids[0]\");\n+\tASSERT_EQ(map_ids[1], map2_id, \"attached_map_ids[1]\");\n+\n+done:\n+\tbpf_link__destroy(link2);\n+\tbpf_link__destroy(link1);\n+}\n+\n+static void run_query_subtest(void)\n+{\n+\tstruct bpf_tcp_ops *skel = NULL;\n+\tstruct netns_obj *ns = NULL;\n+\tint cgroup_fd;\n+\n+\tcgroup_fd = test__join_cgroup(CGROUP_PATH);\n+\tif (!ASSERT_GE(cgroup_fd, 0, \"join_cgroup\"))\n+\t\treturn;\n+\n+\tns = netns_new(TEST_NETNS, true);\n+\tif (!ASSERT_OK_PTR(ns, \"netns_new\"))\n+\t\tgoto done;\n+\n+\tskel = bpf_tcp_ops__open_and_load();\n+\tif (!ASSERT_OK_PTR(skel, \"open_and_load\"))\n+\t\tgoto done;\n+\n+\ttest_query(cgroup_fd, skel);\n+\n+done:\n+\tbpf_tcp_ops__destroy(skel);\n+\tnetns_free(ns);\n+\tclose(cgroup_fd);\n+}\n+\n+/* Must match progs/bpf_tcp_ops.c */\n+#define OPS_RETVAL1\t11\n+#define OPS_RETVAL2\t22\n+\n+/*\n+ * Attach three struct_ops implementing timeout_init to the same cgroup; they\n+ * run in attach order [retval1, retval2, retval3]. timeout_init's return value\n+ * is chained: the first prog reads the kernel seed via bpf_get_retval() (0,\n+ * since no legacy sockops prog is attached) and returns OPS_RETVAL1; each\n+ * subsequent prog must then observe the previous prog's return value. This\n+ * proves the trampoline inherits the retval across an array of struct_ops.\n+ */\n+static void test_retval(int cgroup_fd, struct bpf_tcp_ops *skel)\n+{\n+\tstruct bpf_link *link1 = NULL, *link2 = NULL, *link3 = NULL;\n+\n+\tskel-\u003ebss-\u003eretval_saw1 = -1;\n+\tskel-\u003ebss-\u003eretval_saw2 = -1;\n+\tskel-\u003ebss-\u003eretval_saw3 = -1;\n+\n+\tlink1 = bpf_map__attach_cgroup_opts(skel-\u003emaps.tcp_ops_retval1, cgroup_fd, NULL);\n+\tif (!ASSERT_OK_PTR(link1, \"attach_retval1\"))\n+\t\tgoto done;\n+\n+\tlink2 = bpf_map__attach_cgroup_opts(skel-\u003emaps.tcp_ops_retval2, cgroup_fd, NULL);\n+\tif (!ASSERT_OK_PTR(link2, \"attach_retval2\"))\n+\t\tgoto done;\n+\n+\tlink3 = bpf_map__attach_cgroup_opts(skel-\u003emaps.tcp_ops_retval3, cgroup_fd, NULL);\n+\tif (!ASSERT_OK_PTR(link3, \"attach_retval3\"))\n+\t\tgoto done;\n+\n+\tdo_listen_connect(AF_INET6);\n+\n+\t/* First prog inherits the kernel seed (no legacy sockops -\u003e 0). */\n+\tASSERT_EQ(skel-\u003ebss-\u003eretval_saw1, 0, \"retval_saw1\");\n+\t/* Each subsequent prog inherits the previous prog's return value. */\n+\tASSERT_EQ(skel-\u003ebss-\u003eretval_saw2, OPS_RETVAL1, \"retval_saw2\");\n+\tASSERT_EQ(skel-\u003ebss-\u003eretval_saw3, OPS_RETVAL2, \"retval_saw3\");\n+\n+done:\n+\tbpf_link__destroy(link3);\n+\tbpf_link__destroy(link2);\n+\tbpf_link__destroy(link1);\n+}\n+\n+static void run_retval_subtest(void)\n+{\n+\tstruct bpf_tcp_ops *skel = NULL;\n+\tstruct netns_obj *ns = NULL;\n+\tint cgroup_fd;\n+\n+\tcgroup_fd = test__join_cgroup(CGROUP_PATH);\n+\tif (!ASSERT_GE(cgroup_fd, 0, \"join_cgroup\"))\n+\t\treturn;\n+\n+\tns = netns_new(TEST_NETNS, true);\n+\tif (!ASSERT_OK_PTR(ns, \"netns_new\"))\n+\t\tgoto done;\n+\n+\tskel = bpf_tcp_ops__open_and_load();\n+\tif (!ASSERT_OK_PTR(skel, \"open_and_load\"))\n+\t\tgoto done;\n+\n+\ttest_retval(cgroup_fd, skel);\n+\n+done:\n+\tbpf_tcp_ops__destroy(skel);\n+\tnetns_free(ns);\n+\tclose(cgroup_fd);\n+}\n+\n+/*\n+ * bpf_link__update_map() swaps the struct_ops map backing an attached link.\n+ * The link keeps its position, including BPF_F_PREORDER, across the update.\n+ * Attach ops1 (normal) and ops2 (preorder): order [2, 1]. Update the normal\n+ * link to ops3 -\u003e [2, 3]; update the preorder link to ops1 -\u003e [1, 3].\n+ */\n+static void test_update(int cgroup_fd, struct bpf_tcp_ops *skel)\n+{\n+\tLIBBPF_OPTS(bpf_cgroup_opts, preorder_opts, .flags = BPF_F_PREORDER);\n+\tstruct bpf_link *link = NULL, *link_pre = NULL;\n+\n+\tlink = bpf_map__attach_cgroup_opts(skel-\u003emaps.tcp_ops1, cgroup_fd, NULL);\n+\tif (!ASSERT_OK_PTR(link, \"attach_ops1\"))\n+\t\tgoto done;\n+\n+\tlink_pre = bpf_map__attach_cgroup_opts(skel-\u003emaps.tcp_ops2, cgroup_fd,\n+\t\t\t\t\t \u0026preorder_opts);\n+\tif (!ASSERT_OK_PTR(link_pre, \"attach_ops2_preorder\"))\n+\t\tgoto done;\n+\n+\treset_order(skel);\n+\tdo_listen_connect(AF_INET6);\n+\tASSERT_EQ(skel-\u003ebss-\u003elisten_cnt, 2, \"cnt_initial\");\n+\tASSERT_EQ(skel-\u003ebss-\u003elisten_order[0], 2, \"order0_initial\");\n+\tASSERT_EQ(skel-\u003ebss-\u003elisten_order[1], 1, \"order1_initial\");\n+\n+\t/* Update the normal link's map (ops1 -\u003e ops3); position is unchanged. */\n+\tif (!ASSERT_OK(bpf_link__update_map(link, skel-\u003emaps.tcp_ops3), \"update_normal\"))\n+\t\tgoto done;\n+\n+\treset_order(skel);\n+\tdo_listen_connect(AF_INET6);\n+\tASSERT_EQ(skel-\u003ebss-\u003elisten_order[0], 2, \"order0_after_normal\");\n+\tASSERT_EQ(skel-\u003ebss-\u003elisten_order[1], 3, \"order1_after_normal\");\n+\n+\t/* Update the preorder link's map (ops2 -\u003e ops1); it stays first. */\n+\tif (!ASSERT_OK(bpf_link__update_map(link_pre, skel-\u003emaps.tcp_ops1), \"update_preorder\"))\n+\t\tgoto done;\n+\n+\treset_order(skel);\n+\tdo_listen_connect(AF_INET6);\n+\tASSERT_EQ(skel-\u003ebss-\u003elisten_order[0], 1, \"order0_after_preorder\");\n+\tASSERT_EQ(skel-\u003ebss-\u003elisten_order[1], 3, \"order1_after_preorder\");\n+\n+done:\n+\tbpf_link__destroy(link_pre);\n+\tbpf_link__destroy(link);\n+}\n+\n+static void run_update_subtest(void)\n+{\n+\tstruct bpf_tcp_ops *skel = NULL;\n+\tstruct netns_obj *ns = NULL;\n+\tint cgroup_fd;\n+\n+\tcgroup_fd = test__join_cgroup(CGROUP_PATH);\n+\tif (!ASSERT_GE(cgroup_fd, 0, \"join_cgroup\"))\n+\t\treturn;\n+\n+\tns = netns_new(TEST_NETNS, true);\n+\tif (!ASSERT_OK_PTR(ns, \"netns_new\"))\n+\t\tgoto done;\n+\n+\tskel = bpf_tcp_ops__open_and_load();\n+\tif (!ASSERT_OK_PTR(skel, \"open_and_load\"))\n+\t\tgoto done;\n+\n+\ttest_update(cgroup_fd, skel);\n+\n+done:\n+\tbpf_tcp_ops__destroy(skel);\n+\tnetns_free(ns);\n+\tclose(cgroup_fd);\n+}\n+\n+/*\n+ * Two-level hierarchy. Attach ops1 to the parent and ops2 to the child, then\n+ * trigger from a socket in the child. Descendant progs run before ancestor\n+ * progs, so the order is [2 (child), 1 (parent)].\n+ */\n+static void test_hierarchy(int parent_fd, int child_fd, struct bpf_tcp_ops *skel)\n+{\n+\tstruct bpf_link *plink = NULL, *clink = NULL;\n+\n+\tplink = bpf_map__attach_cgroup_opts(skel-\u003emaps.tcp_ops1, parent_fd, NULL);\n+\tif (!ASSERT_OK_PTR(plink, \"attach_parent\"))\n+\t\tgoto done;\n+\n+\tclink = bpf_map__attach_cgroup_opts(skel-\u003emaps.tcp_ops2, child_fd, NULL);\n+\tif (!ASSERT_OK_PTR(clink, \"attach_child\"))\n+\t\tgoto done;\n+\n+\treset_order(skel);\n+\tdo_listen_connect(AF_INET6);\n+\n+\tASSERT_EQ(skel-\u003ebss-\u003elisten_cnt, 2, \"listen_cnt\");\n+\tASSERT_EQ(skel-\u003ebss-\u003elisten_order[0], 2, \"listen_order[0]\");\n+\tASSERT_EQ(skel-\u003ebss-\u003elisten_order[1], 1, \"listen_order[1]\");\n+\n+done:\n+\tbpf_link__destroy(clink);\n+\tbpf_link__destroy(plink);\n+}\n+\n+static void run_hierarchy_subtest(void)\n+{\n+\tstruct bpf_tcp_ops *skel = NULL;\n+\tstruct netns_obj *ns = NULL;\n+\tint parent_fd, child_fd = -1;\n+\n+\tparent_fd = test__join_cgroup(CGROUP_PATH);\n+\tif (!ASSERT_GE(parent_fd, 0, \"join_parent_cgroup\"))\n+\t\treturn;\n+\n+\tchild_fd = create_and_get_cgroup(CGROUP_PATH \"/child\");\n+\tif (!ASSERT_GE(child_fd, 0, \"create_child_cgroup\"))\n+\t\tgoto done;\n+\n+\tif (!ASSERT_OK(join_cgroup(CGROUP_PATH \"/child\"), \"join_child_cgroup\"))\n+\t\tgoto done;\n+\n+\tns = netns_new(TEST_NETNS, true);\n+\tif (!ASSERT_OK_PTR(ns, \"netns_new\"))\n+\t\tgoto done;\n+\n+\tskel = bpf_tcp_ops__open_and_load();\n+\tif (!ASSERT_OK_PTR(skel, \"open_and_load\"))\n+\t\tgoto done;\n+\n+\ttest_hierarchy(parent_fd, child_fd, skel);\n+\n+done:\n+\tbpf_tcp_ops__destroy(skel);\n+\tnetns_free(ns);\n+\tif (child_fd \u003e= 0)\n+\t\tclose(child_fd);\n+\tclose(parent_fd);\n+}\n+\n+/*\n+ * Attach ops1 to the parent, then create and join the child cgroup. The child\n+ * is created after the attach, so it must inherit the parent's effective progs\n+ * via cgroup_bpf_inherit(). A socket in the child runs the parent's prog.\n+ */\n+static void test_inherit(int parent_fd, struct bpf_tcp_ops *skel)\n+{\n+\tstruct bpf_link *plink = NULL;\n+\tint child_fd = -1;\n+\n+\tplink = bpf_map__attach_cgroup_opts(skel-\u003emaps.tcp_ops1, parent_fd, NULL);\n+\tif (!ASSERT_OK_PTR(plink, \"attach_parent\"))\n+\t\tgoto done;\n+\n+\tchild_fd = create_and_get_cgroup(CGROUP_PATH \"/child\");\n+\tif (!ASSERT_GE(child_fd, 0, \"create_child_cgroup\"))\n+\t\tgoto done;\n+\n+\tif (!ASSERT_OK(join_cgroup(CGROUP_PATH \"/child\"), \"join_child_cgroup\"))\n+\t\tgoto done;\n+\n+\treset_order(skel);\n+\tdo_listen_connect(AF_INET6);\n+\n+\tASSERT_EQ(skel-\u003ebss-\u003elisten_cnt, 1, \"listen_cnt\");\n+\tASSERT_EQ(skel-\u003ebss-\u003elisten_order[0], 1, \"listen_order[0]\");\n+\n+done:\n+\tif (child_fd \u003e= 0)\n+\t\tclose(child_fd);\n+\tbpf_link__destroy(plink);\n+}\n+\n+static void run_inherit_subtest(void)\n+{\n+\tstruct bpf_tcp_ops *skel = NULL;\n+\tstruct netns_obj *ns = NULL;\n+\tint parent_fd;\n+\n+\tparent_fd = test__join_cgroup(CGROUP_PATH);\n+\tif (!ASSERT_GE(parent_fd, 0, \"join_parent_cgroup\"))\n+\t\treturn;\n+\n+\tns = netns_new(TEST_NETNS, true);\n+\tif (!ASSERT_OK_PTR(ns, \"netns_new\"))\n+\t\tgoto done;\n+\n+\tskel = bpf_tcp_ops__open_and_load();\n+\tif (!ASSERT_OK_PTR(skel, \"open_and_load\"))\n+\t\tgoto done;\n+\n+\ttest_inherit(parent_fd, skel);\n+\n+done:\n+\tbpf_tcp_ops__destroy(skel);\n+\tnetns_free(ns);\n+\tclose(parent_fd);\n+}\n+\n+void test_bpf_tcp_ops(void)\n+{\n+\tif (test__start_subtest(\"order\"))\n+\t\trun_order_subtest();\n+\tif (test__start_subtest(\"before_after\"))\n+\t\trun_before_after_subtest();\n+\tif (test__start_subtest(\"query\"))\n+\t\trun_query_subtest();\n+\tif (test__start_subtest(\"retval\"))\n+\t\trun_retval_subtest();\n+\tif (test__start_subtest(\"update\"))\n+\t\trun_update_subtest();\n+\tif (test__start_subtest(\"hierarchy\"))\n+\t\trun_hierarchy_subtest();\n+\tif (test__start_subtest(\"inherit\"))\n+\t\trun_inherit_subtest();\n+}\ndiff --git a/tools/testing/selftests/bpf/prog_tests/bpf_tcp_ops_hdr.c b/tools/testing/selftests/bpf/prog_tests/bpf_tcp_ops_hdr.c\nnew file mode 100644\nindex 00000000000000..73e34d2be9a456\n--- /dev/null\n+++ b/tools/testing/selftests/bpf/prog_tests/bpf_tcp_ops_hdr.c\n@@ -0,0 +1,97 @@\n+// SPDX-License-Identifier: GPL-2.0\n+/* Copyright (c) 2026 Meta Platforms, Inc. and affiliates. */\n+\n+#include \u003ctest_progs.h\u003e\n+#include \u003cnetwork_helpers.h\u003e\n+#include \"cgroup_helpers.h\"\n+#include \"bpf_tcp_ops_hdr.skel.h\"\n+\n+#define CGROUP_PATH\t\"/bpf_tcp_ops_hdr\"\n+#define TEST_NETNS\t\"bpf_tcp_ops_hdr\"\n+\n+#define TEST_OPT_D0\t0xAB\n+#define TEST_OPT_D1\t0xCD\n+\n+static void send_recv(void)\n+{\n+\tchar buf[64] = {};\n+\tint server_fd, client_fd, accept_fd;\n+\tssize_t n;\n+\n+\tserver_fd = start_server(AF_INET6, SOCK_STREAM, \"::1\", 0, 0);\n+\tif (!ASSERT_GE(server_fd, 0, \"start_server\"))\n+\t\treturn;\n+\n+\tclient_fd = connect_to_fd(server_fd, 0);\n+\tif (!ASSERT_OK_FD(client_fd, \"connect_to_fd\"))\n+\t\tgoto close_server;\n+\n+\taccept_fd = accept(server_fd, NULL, NULL);\n+\tif (!ASSERT_OK_FD(accept_fd, \"accept\"))\n+\t\tgoto close_client;\n+\n+\t/* Exchange data both directions so option-bearing data packets\n+\t * are sent and parsed on each side.\n+\t */\n+\tn = send(client_fd, buf, sizeof(buf), 0);\n+\tASSERT_EQ(n, sizeof(buf), \"client_send\");\n+\tn = recv(accept_fd, buf, sizeof(buf), 0);\n+\tASSERT_EQ(n, sizeof(buf), \"server_recv\");\n+\n+\tn = send(accept_fd, buf, sizeof(buf), 0);\n+\tASSERT_EQ(n, sizeof(buf), \"server_send\");\n+\tn = recv(client_fd, buf, sizeof(buf), 0);\n+\tASSERT_EQ(n, sizeof(buf), \"client_recv\");\n+\n+\tclose(accept_fd);\n+close_client:\n+\tclose(client_fd);\n+close_server:\n+\tclose(server_fd);\n+}\n+\n+static void run_hdr_opt(void)\n+{\n+\tstruct bpf_tcp_ops_hdr *skel = NULL;\n+\tstruct bpf_link *link = NULL;\n+\tstruct netns_obj *ns = NULL;\n+\tint cgroup_fd;\n+\n+\tcgroup_fd = test__join_cgroup(CGROUP_PATH);\n+\tif (!ASSERT_GE(cgroup_fd, 0, \"join_cgroup\"))\n+\t\treturn;\n+\n+\tns = netns_new(TEST_NETNS, true);\n+\tif (!ASSERT_OK_PTR(ns, \"netns_new\"))\n+\t\tgoto done;\n+\n+\tskel = bpf_tcp_ops_hdr__open_and_load();\n+\tif (!ASSERT_OK_PTR(skel, \"open_and_load\"))\n+\t\tgoto done;\n+\n+\tlink = bpf_map__attach_cgroup_opts(skel-\u003emaps.test_hdr_ops, cgroup_fd, NULL);\n+\tif (!ASSERT_OK_PTR(link, \"attach_cgroup\"))\n+\t\tgoto done;\n+\n+\tsend_recv();\n+\n+\t/* Reserve + write hooks ran while sending. */\n+\tASSERT_GT(skel-\u003ebss-\u003ehdr_opt_len_cnt, 0, \"hdr_opt_len_cnt\");\n+\tASSERT_GT(skel-\u003ebss-\u003ewrite_cnt, 0, \"write_cnt\");\n+\t/* Parse hook ran and recovered our option on the receive side. */\n+\tASSERT_GT(skel-\u003ebss-\u003eparse_cnt, 0, \"parse_cnt\");\n+\tASSERT_GT(skel-\u003ebss-\u003efound_cnt, 0, \"found_cnt\");\n+\tASSERT_EQ(skel-\u003ebss-\u003efound_d0, TEST_OPT_D0, \"found_d0\");\n+\tASSERT_EQ(skel-\u003ebss-\u003efound_d1, TEST_OPT_D1, \"found_d1\");\n+\n+done:\n+\tbpf_link__destroy(link);\n+\tbpf_tcp_ops_hdr__destroy(skel);\n+\tnetns_free(ns);\n+\tclose(cgroup_fd);\n+}\n+\n+void test_bpf_tcp_ops_hdr(void)\n+{\n+\trun_hdr_opt();\n+}\ndiff --git a/tools/testing/selftests/bpf/progs/bpf_tcp_ops.c b/tools/testing/selftests/bpf/progs/bpf_tcp_ops.c\nnew file mode 100644\nindex 00000000000000..94a7f52573d517\n--- /dev/null\n+++ b/tools/testing/selftests/bpf/progs/bpf_tcp_ops.c\n@@ -0,0 +1,141 @@\n+// SPDX-License-Identifier: GPL-2.0\n+/* Copyright (c) 2026 Meta Platforms, Inc. and affiliates. */\n+\n+#include \"vmlinux.h\"\n+#include \u003cbpf/bpf_helpers.h\u003e\n+#include \u003cbpf/bpf_tracing.h\u003e\n+\n+#define MAX_CGROUP_OPS 8\n+\n+/* Call order for listen and connect, indexed by call sequence */\n+u32 listen_order[MAX_CGROUP_OPS];\n+u32 listen_cnt;\n+\n+u32 connect_order[MAX_CGROUP_OPS];\n+u32 connect_cnt;\n+\n+static void record_listen(int id)\n+{\n+\tu32 idx = listen_cnt;\n+\n+\tif (idx \u003c MAX_CGROUP_OPS) {\n+\t\tlisten_order[idx] = id;\n+\t\tlisten_cnt = idx + 1;\n+\t}\n+}\n+\n+static void record_connect(int id)\n+{\n+\tu32 idx = connect_cnt;\n+\n+\tif (idx \u003c MAX_CGROUP_OPS) {\n+\t\tconnect_order[idx] = id;\n+\t\tconnect_cnt = idx + 1;\n+\t}\n+}\n+\n+/* struct_ops instance 1 */\n+\n+SEC(\"struct_ops\")\n+void BPF_PROG(tcp_ops1_listen, struct sock *sk)\n+{\n+\trecord_listen(1);\n+}\n+\n+SEC(\"struct_ops\")\n+void BPF_PROG(tcp_ops1_connect, struct sock *sk)\n+{\n+\trecord_connect(1);\n+}\n+\n+SEC(\".struct_ops.link\")\n+struct bpf_tcp_ops tcp_ops1 = {\n+\t.listen = (void *)tcp_ops1_listen,\n+\t.connect = (void *)tcp_ops1_connect,\n+};\n+\n+/* struct_ops instance 2 */\n+\n+SEC(\"struct_ops\")\n+void BPF_PROG(tcp_ops2_listen, struct sock *sk)\n+{\n+\trecord_listen(2);\n+}\n+\n+SEC(\"struct_ops\")\n+void BPF_PROG(tcp_ops2_connect, struct sock *sk)\n+{\n+\trecord_connect(2);\n+}\n+\n+SEC(\".struct_ops.link\")\n+struct bpf_tcp_ops tcp_ops2 = {\n+\t.listen = (void *)tcp_ops2_listen,\n+\t.connect = (void *)tcp_ops2_connect,\n+};\n+\n+/* struct_ops instance 3 */\n+\n+SEC(\"struct_ops\")\n+void BPF_PROG(tcp_ops3_listen, struct sock *sk)\n+{\n+\trecord_listen(3);\n+}\n+\n+SEC(\"struct_ops\")\n+void BPF_PROG(tcp_ops3_connect, struct sock *sk)\n+{\n+\trecord_connect(3);\n+}\n+\n+SEC(\".struct_ops.link\")\n+struct bpf_tcp_ops tcp_ops3 = {\n+\t.listen = (void *)tcp_ops3_listen,\n+\t.connect = (void *)tcp_ops3_connect,\n+};\n+\n+#define OPS_RETVAL1\t11\n+#define OPS_RETVAL2\t22\n+#define OPS_RETVAL3\t33\n+\n+int retval_saw1;\n+int retval_saw2;\n+int retval_saw3;\n+\n+SEC(\"struct_ops\")\n+int BPF_PROG(tcp_ops_retval1_timeout_init, struct sock *sk, struct request_sock *req)\n+{\n+\tretval_saw1 = bpf_get_retval();\n+\treturn OPS_RETVAL1;\n+}\n+\n+SEC(\".struct_ops.link\")\n+struct bpf_tcp_ops tcp_ops_retval1 = {\n+\t.timeout_init = (void *)tcp_ops_retval1_timeout_init,\n+};\n+\n+SEC(\"struct_ops\")\n+int BPF_PROG(tcp_ops_retval2_timeout_init, struct sock *sk, struct request_sock *req)\n+{\n+\tretval_saw2 = bpf_get_retval();\n+\treturn OPS_RETVAL2;\n+}\n+\n+SEC(\".struct_ops.link\")\n+struct bpf_tcp_ops tcp_ops_retval2 = {\n+\t.timeout_init = (void *)tcp_ops_retval2_timeout_init,\n+};\n+\n+SEC(\"struct_ops\")\n+int BPF_PROG(tcp_ops_retval3_timeout_init, struct sock *sk, struct request_sock *req)\n+{\n+\tretval_saw3 = bpf_get_retval();\n+\treturn OPS_RETVAL3;\n+}\n+\n+SEC(\".struct_ops.link\")\n+struct bpf_tcp_ops tcp_ops_retval3 = {\n+\t.timeout_init = (void *)tcp_ops_retval3_timeout_init,\n+};\n+\n+char _license[] SEC(\"license\") = \"GPL\";\ndiff --git a/tools/testing/selftests/bpf/progs/bpf_tcp_ops_hdr.c b/tools/testing/selftests/bpf/progs/bpf_tcp_ops_hdr.c\nnew file mode 100644\nindex 00000000000000..46618a604d965a\n--- /dev/null\n+++ b/tools/testing/selftests/bpf/progs/bpf_tcp_ops_hdr.c\n@@ -0,0 +1,83 @@\n+// SPDX-License-Identifier: GPL-2.0\n+/* Copyright (c) 2026 Meta Platforms, Inc. and affiliates. */\n+\n+#include \"vmlinux.h\"\n+#include \u003cbpf/bpf_helpers.h\u003e\n+#include \u003cbpf/bpf_tracing.h\u003e\n+\n+/* Experimental option kind and payload written/parsed by this test. */\n+#define TEST_OPT_KIND\t0xFD\n+#define TEST_OPT_LEN\t4\n+#define TEST_OPT_D0\t0xAB\n+#define TEST_OPT_D1\t0xCD\n+\n+int hdr_opt_len_cnt;\n+int write_cnt;\n+int parse_cnt;\n+int found_cnt;\n+__u8 found_d0;\n+__u8 found_d1;\n+\n+SEC(\"struct_ops\")\n+void BPF_PROG(test_hdr_opt_len, struct sock *sk, struct sk_buff *skb,\n+\t struct request_sock *req, struct sk_buff *syn_skb,\n+\t enum tcp_synack_type synack_type, unsigned int *remaining)\n+{\n+\thdr_opt_len_cnt++;\n+\n+\t/* Reserve TEST_OPT_LEN bytes; the helper decrements *remaining. Stacks\n+\t * with other progs in the cgroup hierarchy.\n+\t */\n+\tbpf_reserve_hdr_opt(ctx, TEST_OPT_LEN, 0);\n+}\n+\n+SEC(\"struct_ops\")\n+void BPF_PROG(test_write_hdr_opt, struct sock *sk, struct sk_buff *skb,\n+\t struct request_sock *req, struct sk_buff *syn_skb,\n+\t enum tcp_synack_type synack_type, __u32 opt_off)\n+{\n+\t__u8 opt[TEST_OPT_LEN] = {\n+\t\tTEST_OPT_KIND, TEST_OPT_LEN, TEST_OPT_D0, TEST_OPT_D1,\n+\t};\n+\n+\t/* bpf_store_hdr_opt() takes the program ctx (the kernel reads the\n+\t * outgoing skb from it); it appends after any options already written\n+\t * in the reserved window, rejects duplicates, and confines the write to\n+\t * the header option scratch. Stacks across progs in the cgroup hierarchy.\n+\t */\n+\tif (bpf_store_hdr_opt(ctx, opt, sizeof(opt), 0))\n+\t\treturn;\n+\n+\twrite_cnt++;\n+}\n+\n+SEC(\"struct_ops\")\n+void BPF_PROG(test_parse_hdr, struct sock *sk, struct sk_buff *skb)\n+{\n+\t__u8 opt[TEST_OPT_LEN] = {\n+\t\tTEST_OPT_KIND, TEST_OPT_LEN, TEST_OPT_D0, TEST_OPT_D1,\n+\t};\n+\n+\tparse_cnt++;\n+\n+\t/* Look up the experimental option written by test_write_hdr_opt() in\n+\t * the incoming skb. For an experimental kind the search matches on the\n+\t * 2-byte magic in opt[2..3]; on a match the found option is copied back\n+\t * into opt[].\n+\t */\n+\tif (bpf_load_hdr_opt(ctx, opt, sizeof(opt), 0) \u003c 0)\n+\t\treturn;\n+\n+\tfound_d0 = opt[2];\n+\tfound_d1 = opt[3];\n+\tfound_cnt++;\n+}\n+\n+SEC(\".struct_ops.link\")\n+struct bpf_tcp_ops test_hdr_ops = {\n+\t.hdr_opt_len\t= (void *)test_hdr_opt_len,\n+\t.write_hdr_opt\t= (void *)test_write_hdr_opt,\n+\t.parse_hdr\t= (void *)test_parse_hdr,\n+};\n+\n+char _license[] SEC(\"license\") = \"GPL\";\n"
} |
| 2/1 | 2026/06/23 18:13 | agent | patch-evaluator |
0mModel:gemini-3.1-pro-preview Args: null Results: {
"EnableConfigs": [
"BPF_JIT",
"BPF_SYSCALL",
"CGROUP_BPF"
],
"FocusSymbols": [
"bpf_tcp_ops_store_hdr_opt",
"bpf_tcp_ops_load_hdr_opt",
"bpf_tcp_ops_reserve_hdr_opt",
"bpf_tcp_ops_get_retval",
"cgroup_bpf_struct_ops_register",
"cgroup_struct_ops_link_detach",
"cgroup_struct_ops_link_update",
"cgroup_bpf_struct_ops_attach",
"bpf_struct_ops_link_update_check",
"bpf_tcp_ops_parse_hdr",
"bpf_tcp_ops_hdr_opt_len"
],
"Reasoning": "The patch introduces a new BPF struct_ops type `bpf_tcp_ops` that can be attached to cgroups. This allows BPF programs to hook into various TCP events (like timeout initialization, RTT measurement, state changes, and header option parsing/writing) on a per-cgroup basis. The changes involve core BPF cgroup attachment logic, struct_ops map management, and TCP stack hooks. It is a functional change that adds new reachable code paths in the kernel, making it worth fuzzing.",
"WorthFuzzing": true
}Instruction: You are an expert Linux kernel maintainer. Your job is to review a provided patch series and determine if it makes functional changes to the kernel that should be fuzzed. IMPORTANT: The changes have ALREADY been applied and committed as the HEAD commit in your workspace. Do NOT rely on your internal knowledge of the kernel. You must actively use your code access tools to examine the actual source code and confirm any assumptions. Return WorthFuzzing=false if the patch only contains: - Modifications to Documentation/, Kconfig files, or code comments. - Purely decorative changes, such as logging (e.g., pr_err, printk) or tracepoints. - Changes to numeric constants or macros that do not functionally alter execution flow. - Code paths that are impossible to reach in virtualized environments like GCE or QEMU, even when utilizing software-emulated hardware (e.g., usb gadget, mac80211_hwsim). If it modifies reachable core kernel logic, drivers, or architectures, use your code search tools to verify the code can be executed, then return WorthFuzzing=true. When returning WorthFuzzing=true, you MUST ALSO: 1. Extract any specific kernel functions that should be heavily fuzzed into FocusSymbols. Avoid listing generic hot-path functions to prevent skewed test distributions. 2. Identify any specific CONFIG_ options required to properly test this new/modified feature. Go and look into the Kconfig files and check for ifdefs around the code, do not make assumptions. Do not list too generic configs (we already have them enabled). Only list those that specifically cover the modified code. List them in the EnableConfigs output array, and DO NOT add a 'CONFIG_' prefix (e.g., return "NET_IPV4" instead of "CONFIG_NET_IPV4"). Prefer calling several tools at the same time to save round-trips. Use set-results tool to provide results of the analysis. It must be called exactly once before the final reply. Ignore results of this tool. Prompt: For your convenience, here is the diff of the changes:
commit b701d6de975f9ae313f7b18395b5440bcffb92f0
Author: syz-cluster <triage@syzkaller.com>
Date: Tue Jun 23 18:13:12 2026 +0000
syz-cluster: applied patch under review
diff --git a/include/linux/bpf-cgroup-defs.h b/include/linux/bpf-cgroup-defs.h
index c9e6b26abab6d7..0147b8bec97368 100644
--- a/include/linux/bpf-cgroup-defs.h
+++ b/include/linux/bpf-cgroup-defs.h
@@ -47,6 +47,7 @@ enum cgroup_bpf_attach_type {
CGROUP_INET6_GETSOCKNAME,
CGROUP_UNIX_GETSOCKNAME,
CGROUP_INET_SOCK_RELEASE,
+ CGROUP_TCP_SOCK_OPS,
CGROUP_LSM_START,
CGROUP_LSM_END = CGROUP_LSM_START + CGROUP_LSM_NUM - 1,
MAX_CGROUP_BPF_ATTACH_TYPE
diff --git a/include/linux/bpf-cgroup.h b/include/linux/bpf-cgroup.h
index 4d0cc65976a14a..8a75a6cd73096c 100644
--- a/include/linux/bpf-cgroup.h
+++ b/include/linux/bpf-cgroup.h
@@ -100,6 +100,8 @@ struct bpf_cgroup_storage {
struct bpf_cgroup_link {
struct bpf_link link;
struct cgroup *cgroup;
+ struct bpf_map *map;
+ wait_queue_head_t wait_hup;
};
struct bpf_prog_list {
@@ -110,6 +112,18 @@ struct bpf_prog_list {
u32 flags;
};
+#define bpf_cgroup_struct_ops_foreach(var, item, cgrp, atype) \
+ for (item = rcu_dereference((cgrp)->bpf.effective[atype])->items;\
+ ((var) = READ_ONCE(item->kdata)); \
+ item++)
+
+static inline bool cgroup_bpf_is_struct_ops_atype(enum cgroup_bpf_attach_type atype)
+{
+ return atype == CGROUP_TCP_SOCK_OPS;
+}
+void cgroup_bpf_struct_ops_register(int atype, u32 type_id, void *cfi_stubs, bool mult_trace);
+int cgroup_bpf_struct_ops_attach(struct bpf_map *map, const union bpf_attr *attr);
+
void __init cgroup_bpf_lifetime_notifier_init(void);
int __cgroup_bpf_run_filter_skb(struct sock *sk,
@@ -479,6 +493,20 @@ static inline int bpf_percpu_cgroup_storage_update(struct bpf_map *map,
return 0;
}
+static inline bool cgroup_bpf_is_struct_ops_atype(int atype)
+{
+ return false;
+}
+static inline void cgroup_bpf_struct_ops_register(int atype, u32 type_id, void *cfi_stubs,
+ bool mult_trace)
+{
+}
+static inline int cgroup_bpf_struct_ops_attach(struct bpf_map *map,
+ const union bpf_attr *attr)
+{
+ return -EOPNOTSUPP;
+}
+
#define cgroup_bpf_enabled(atype) (0)
#define BPF_CGROUP_RUN_SA_PROG_LOCK(sk, uaddr, uaddrlen, atype, t_ctx) ({ 0; })
#define BPF_CGROUP_RUN_SA_PROG(sk, uaddr, uaddrlen, atype) ({ 0; })
diff --git a/include/linux/bpf.h b/include/linux/bpf.h
index 7719f652844560..91024d2da4ea86 100644
--- a/include/linux/bpf.h
+++ b/include/linux/bpf.h
@@ -90,6 +90,7 @@ struct bpf_map_ops {
struct bpf_map *(*map_alloc)(union bpf_attr *attr);
void (*map_release)(struct bpf_map *map, struct file *map_file);
void (*map_free)(struct bpf_map *map);
+ void (*map_free_pre_rcu)(struct bpf_map *map);
int (*map_get_next_key)(struct bpf_map *map, void *key, void *next_key);
void (*map_release_uref)(struct bpf_map *map);
void *(*map_lookup_elem_sys_only)(struct bpf_map *map, void *key);
@@ -2099,6 +2100,18 @@ struct btf_member;
* unloaded while in use.
* @name: The name of the struct bpf_struct_ops object.
* @func_models: Func models
+ * @cgroup_atype: A value in enum cgroup_bpf_attach_type for cgroup attachment.
+ * 0 means the struct_ops type does not support cgroup attachment.
+ * If cgroup_atype is non-zero, the @reg and @unreg must be NULL
+ * because the attachment/detachment will be handled by the bpf core.
+ * @free_after_tasks_rcu_gp: Set to true if it needs the bpf core to wait for
+ * a tasks_rcu gp before freeing the struct_ops map
+ * and its progs. It is unnecessary if the @unreg
+ * has waited for the correct rcu gp or the @unreg
+ * has ensured all struct_ops prog has finished running.
+ * @free_after_mult_rcu_gp: Same as @free_after_tasks_rcu_gp but waiting for
+ * both tasks_trace_rcu and regular rcu grace period.
+ * It is usually needed if the struct_ops has sleepable prog.
*/
struct bpf_struct_ops {
const struct bpf_verifier_ops *verifier_ops;
@@ -2117,6 +2130,9 @@ struct bpf_struct_ops {
struct module *owner;
const char *name;
struct btf_func_model func_models[BPF_STRUCT_OPS_MAX_NR_MEMBERS];
+ int cgroup_atype;
+ bool free_after_tasks_rcu_gp;
+ bool free_after_mult_rcu_gp;
};
/* Every member of a struct_ops type has an instance even a member is not
@@ -2245,6 +2261,13 @@ u32 bpf_struct_ops_id(const void *kdata);
int bpf_struct_ops_for_each_prog(const void *kdata,
int (*cb)(struct bpf_prog *prog, void *data),
void *data);
+void *bpf_struct_ops_map_kdata(struct bpf_map *map);
+u32 bpf_struct_ops_kdata_map_id(void *kdata);
+void *bpf_struct_ops_map_cfi_stubs(struct bpf_map *map);
+bool bpf_struct_ops_valid_to_reg(struct bpf_map *map);
+int bpf_struct_ops_link_update_check(struct bpf_map *new_map, struct bpf_map *old_map,
+ struct bpf_map *expected_old_map);
+int bpf_struct_ops_map_cgroup_atype(struct bpf_map *map);
#ifdef CONFIG_NET
/* Define it here to avoid the use of forward declaration */
@@ -2309,6 +2332,32 @@ static inline void bpf_map_struct_ops_info_fill(struct bpf_map_info *info, struc
static inline void bpf_struct_ops_desc_release(struct bpf_struct_ops_desc *st_ops_desc)
{
}
+static inline void *bpf_struct_ops_map_kdata(struct bpf_map *map)
+{
+ return NULL;
+}
+static inline u32 bpf_struct_ops_kdata_map_id(void *kdata)
+{
+ return 0;
+}
+static inline int bpf_struct_ops_map_cgroup_atype(struct bpf_map *map)
+{
+ return 0;
+}
+static inline void *bpf_struct_ops_map_cfi_stubs(struct bpf_map *map)
+{
+ return NULL;
+}
+static inline bool bpf_struct_ops_valid_to_reg(struct bpf_map *map)
+{
+ return false;
+}
+static inline int bpf_struct_ops_link_update_check(struct bpf_map *new_map,
+ struct bpf_map *old_map,
+ struct bpf_map *expected_old_map)
+{
+ return -EOPNOTSUPP;
+}
#endif
@@ -2484,7 +2533,10 @@ u64 bpf_event_output(struct bpf_map *map, u64 flags, void *meta, u64 meta_size,
* since other cpus are walking the array of pointers in parallel.
*/
struct bpf_prog_array_item {
- struct bpf_prog *prog;
+ union {
+ struct bpf_prog *prog;
+ void *kdata;
+ };
union {
struct bpf_cgroup_storage *cgroup_storage[MAX_BPF_CGROUP_STORAGE_TYPE];
u64 bpf_cookie;
@@ -2526,6 +2578,7 @@ int bpf_prog_array_copy(struct bpf_prog_array *old_array,
struct bpf_prog *include_prog,
u64 bpf_cookie,
struct bpf_prog_array **new_array);
+struct bpf_prog *bpf_prog_dummy(void);
struct bpf_run_ctx {};
@@ -2544,6 +2597,7 @@ struct bpf_trace_run_ctx {
struct bpf_tramp_run_ctx {
struct bpf_run_ctx run_ctx;
u64 bpf_cookie;
+ int retval;
struct bpf_run_ctx *saved_run_ctx;
};
diff --git a/include/linux/filter.h b/include/linux/filter.h
index 67d337ede91b8e..fe28db65fb6a1c 100644
--- a/include/linux/filter.h
+++ b/include/linux/filter.h
@@ -1843,6 +1843,11 @@ static __always_inline long __bpf_xdp_redirect_map(struct bpf_map *map, u64 inde
return XDP_REDIRECT;
}
+int __bpf_sock_ops_load_hdr_opt(struct bpf_sock_ops_kern *bpf_sock,
+ void *search_res, u32 len, u64 flags);
+int __bpf_sock_ops_store_hdr_opt(struct bpf_sock_ops_kern *bpf_sock,
+ const void *from, u32 len, u64 flags);
+
#ifdef CONFIG_NET
int __bpf_skb_load_bytes(const struct sk_buff *skb, u32 offset, void *to, u32 len);
int __bpf_skb_store_bytes(struct sk_buff *skb, u32 offset, const void *from,
diff --git a/include/net/tcp.h b/include/net/tcp.h
index 6d376ea4d1c01d..7bf702117602da 100644
--- a/include/net/tcp.h
+++ b/include/net/tcp.h
@@ -2953,12 +2953,160 @@ static inline void tcp_clear_sock_ops_cb_flags(struct sock *sk)
#endif
+#if defined(CONFIG_BPF_JIT) && defined(CONFIG_CGROUP_BPF)
+
+struct bpf_tcp_ops {
+ /* Should return the initial SYN (active open) or SYN-ACK (passive open)
+ * retransmission timeout. Return the timeout in jiffies, or <= 0 for
+ * the kernel default.
+ *
+ * @req: request_sock on the passive (synack) path; NULL otherwise.
+ */
+ int (*timeout_init)(struct sock *sk, struct request_sock *req);
+
+ /* Should return the initial advertised receive window, in packets,
+ * or < 0 for the kernel default. @req as in timeout_init().
+ */
+ int (*rwnd_init)(struct sock *sk, struct request_sock *req);
+
+ /* Called when an active connection becomes established.
+ * @skb is the SYNACK that completed the 3WHS, or NULL for a
+ * TCP_REPAIR socket (tcp_finish_connect() with no skb).
+ */
+ void (*active_established)(struct sock *sk, struct sk_buff *skb__nullable);
+
+ /* Called when a passive connection becomes established.
+ * @skb is the ACK that completed the 3WHS.
+ */
+ void (*passive_established)(struct sock *sk, struct sk_buff *skb);
+
+ /* Called when the retransmission timer fires. */
+ void (*rto)(struct sock *sk);
+
+ /* Called on every RTT sample.
+ * @mrtt: the measured RTT, in microseconds.
+ * @srtt: the updated smoothed RTT.
+ */
+ void (*rtt)(struct sock *sk, long mrtt, u32 srtt);
+
+ /* Called when the connection changes TCP state.
+ * @state: the new state (one of the TCP_* states).
+ */
+ void (*set_state)(struct sock *sk, int state);
+
+ /* Called when an skb is retransmitted.
+ * @skb: the retransmitted skb.
+ * @err: tcp_transmit_skb() return value (0 on success).
+ */
+ void (*retrans)(struct sock *sk, struct sk_buff *skb, int err);
+
+ /* Called right before an active connection is initialized. */
+ void (*connect)(struct sock *sk);
+
+ /* Called on listen(2), right after the socket enters TCP_LISTEN. */
+ void (*listen)(struct sock *sk);
+
+ /* Parse the TCP header options of an incoming skb received on an
+ * established connection. Use bpf_dynptr_from_skb()/bpf_skb_load_bytes()
+ * to access the options.
+ */
+ void (*parse_hdr)(struct sock *sk, struct sk_buff *skb);
+
+ /* Reserve space in the outgoing TCP header for options to be written
+ * later by write_hdr_opt(). Call bpf_reserve_hdr_opt() to reserve bytes.
+ *
+ * @skb: outgoing packet. NULL when called from tcp_current_mss()
+ * (MSS sizing).
+ * @req: request_sock on the synack path; NULL otherwise.
+ * @syn_skb: incoming SYN on the synack path; NULL otherwise.
+ * @synack_type: TCP_SYNACK_COOKIE indicates a stateless syncookie.
+ * @remaining: pointer to the size of space still available; cast it
+ * using bpf_rdonly_cast() before dereferencing.
+ */
+ void (*hdr_opt_len)(struct sock *sk, struct sk_buff *skb,
+ struct request_sock *req, struct sk_buff *syn_skb,
+ enum tcp_synack_type synack_type,
+ unsigned int *remaining);
+
+ /* Write header options into the space reserved earlier by hdr_opt_len().
+ * Use bpf_store_hdr_opt() to write; it appends within the reserved window
+ * shared with legacy SOCKOPS.
+ *
+ * @skb: outgoing packet.
+ * @req: request_sock on the synack path; NULL otherwise.
+ * @syn_skb: incoming SYN on the synack path; NULL otherwise.
+ * @synack_type: TCP_SYNACK_COOKIE indicates a stateless syncookie.
+ * @opt_off: offset in the outgoing @skb's TCP header where the
+ * bpf_tcp_ops portion of the reserved window begins, i.e. after
+ * the kernel and legacy options.
+ */
+ void (*write_hdr_opt)(struct sock *sk, struct sk_buff *skb,
+ struct request_sock *req, struct sk_buff *syn_skb,
+ enum tcp_synack_type synack_type,
+ u32 opt_off);
+};
+
+#define bpf_tcp_ops_call(op, sk, ...) \
+do { \
+ if (cgroup_bpf_enabled(CGROUP_TCP_SOCK_OPS)) { \
+ const struct bpf_prog_array_item *item; \
+ const struct bpf_tcp_ops *tcp_ops; \
+ struct cgroup *cgrp; \
+ \
+ cgrp = sock_cgroup_ptr(&sk->sk_cgrp_data); \
+ rcu_read_lock_dont_migrate(); \
+ bpf_cgroup_struct_ops_foreach(tcp_ops, item, cgrp, \
+ CGROUP_TCP_SOCK_OPS) { \
+ if (tcp_ops->op) \
+ tcp_ops->op(sk, ##__VA_ARGS__); \
+ } \
+ rcu_read_unlock_migrate(); \
+ } \
+} while (0)
+
+#define bpf_tcp_ops_call_int(op, init_retval, sk, ...) \
+({ \
+ int __retval = (init_retval); \
+ if (cgroup_bpf_enabled(CGROUP_TCP_SOCK_OPS)) { \
+ const struct bpf_prog_array_item *item; \
+ const struct bpf_tcp_ops *tcp_ops; \
+ struct bpf_tramp_run_ctx run_ctx; \
+ struct bpf_run_ctx *old_run_ctx; \
+ struct sock *__sk = sk_to_full_sk(sk); \
+ struct request_sock *req = NULL; \
+ struct cgroup *cgrp; \
+ \
+ if (__sk) { \
+ run_ctx.retval = (init_retval); \
+ cgrp = sock_cgroup_ptr(&__sk->sk_cgrp_data); \
+ if (!sk_fullsock(sk)) \
+ req = (struct request_sock *)sk; \
+ rcu_read_lock_dont_migrate(); \
+ old_run_ctx = bpf_set_run_ctx(&run_ctx.run_ctx);\
+ bpf_cgroup_struct_ops_foreach(tcp_ops, item, cgrp, \
+ CGROUP_TCP_SOCK_OPS) { \
+ if (tcp_ops->op) \
+ run_ctx.retval = tcp_ops->op(__sk, req, ##__VA_ARGS__); \
+ } \
+ bpf_reset_run_ctx(old_run_ctx); \
+ rcu_read_unlock_migrate(); \
+ __retval = run_ctx.retval; \
+ } \
+ } \
+ __retval; \
+})
+
+#else
+#define bpf_tcp_ops_call(op, sk, ...) do { } while (0)
+#define bpf_tcp_ops_call_int(op, init_retval, sk, ...) (init_retval)
+#endif
+
static inline u32 tcp_timeout_init(struct sock *sk)
{
int timeout;
timeout = tcp_call_bpf(sk, BPF_SOCK_OPS_TIMEOUT_INIT, 0, NULL);
-
+ timeout = bpf_tcp_ops_call_int(timeout_init, timeout, sk);
if (timeout <= 0)
timeout = TCP_TIMEOUT_INIT;
return min_t(int, timeout, TCP_RTO_MAX);
@@ -2969,7 +3117,7 @@ static inline u32 tcp_rwnd_init_bpf(struct sock *sk)
int rwnd;
rwnd = tcp_call_bpf(sk, BPF_SOCK_OPS_RWND_INIT, 0, NULL);
-
+ rwnd = bpf_tcp_ops_call_int(rwnd_init, rwnd, sk);
if (rwnd < 0)
rwnd = 0;
return rwnd;
@@ -2984,6 +3132,7 @@ static inline void tcp_bpf_rtt(struct sock *sk, long mrtt, u32 srtt)
{
if (BPF_SOCK_OPS_TEST_FLAG(tcp_sk(sk), BPF_SOCK_OPS_RTT_CB_FLAG))
tcp_call_bpf_2arg(sk, BPF_SOCK_OPS_RTT_CB, mrtt, srtt);
+ bpf_tcp_ops_call(rtt, sk, mrtt, srtt);
}
#if IS_ENABLED(CONFIG_SMC)
diff --git a/include/uapi/linux/bpf.h b/include/uapi/linux/bpf.h
index 89b36de5fdbb6d..45b9ee29e46174 100644
--- a/include/uapi/linux/bpf.h
+++ b/include/uapi/linux/bpf.h
@@ -1756,7 +1756,7 @@ union bpf_attr {
__u32 prog_cnt;
__u32 count;
};
- __u32 :32;
+ __u32 type_id;
/* output: per-program attach_flags.
* not allowed to be set during effective query.
*/
@@ -4799,15 +4799,18 @@ union bpf_attr {
* The non-negative copied *buf* length equal to or less than
* *size* on success, or a negative error in case of failure.
*
- * long bpf_load_hdr_opt(struct bpf_sock_ops *skops, void *searchby_res, u32 len, u64 flags)
+ * long bpf_load_hdr_opt(void *ctx, void *searchby_res, u32 len, u64 flags)
* Description
* Load header option. Support reading a particular TCP header
- * option for bpf program (**BPF_PROG_TYPE_SOCK_OPS**).
+ * option for bpf program (**BPF_PROG_TYPE_SOCK_OPS**). For the
+ * **bpf_tcp_ops** struct_ops, this helper can be called from the
+ * **parse_hdr**\ () and **write_hdr_opt**\ () operators.
*
- * If *flags* is 0, it will search the option from the
- * *skops*\ **->skb_data**. The comment in **struct bpf_sock_ops**
- * has details on what skb_data contains under different
- * *skops*\ **->op**.
+ * If *flags* is 0, it will search the option from the packet
+ * associated with the current operation. For
+ * **BPF_PROG_TYPE_SOCK_OPS**, the comment in
+ * **struct bpf_sock_ops** has details on what skb_data
+ * contains under different *op*.
*
* The first byte of the *searchby_res* specifies the
* kind that it wants to search.
@@ -4840,6 +4843,8 @@ union bpf_attr {
*
* * **BPF_LOAD_HDR_OPT_TCP_SYN** to search from the
* saved_syn packet or the just-received syn packet.
+ * Not supported by the **bpf_tcp_ops** struct_ops, which
+ * rejects all flags.
*
* Return
* > 0 when found, the header option is copied to *searchby_res*.
@@ -4860,9 +4865,9 @@ union bpf_attr {
* packet.
*
* **-EPERM** if the helper cannot be used under the current
- * *skops*\ **->op**.
+ * operation.
*
- * long bpf_store_hdr_opt(struct bpf_sock_ops *skops, const void *from, u32 len, u64 flags)
+ * long bpf_store_hdr_opt(void *ctx, const void *from, u32 len, u64 flags)
* Description
* Store header option. The data will be copied
* from buffer *from* with length *len* to the TCP header.
@@ -4878,7 +4883,9 @@ union bpf_attr {
* by searching the same option in the outgoing skb.
*
* This helper can only be called during
- * **BPF_SOCK_OPS_WRITE_HDR_OPT_CB**.
+ * **BPF_SOCK_OPS_WRITE_HDR_OPT_CB**, or from the
+ * **write_hdr_opt**\ () operator of the **bpf_tcp_ops**
+ * struct_ops.
*
* Return
* 0 on success, or negative error in case of failure:
@@ -4893,9 +4900,9 @@ union bpf_attr {
* **-EFAULT** on failure to parse the existing header options.
*
* **-EPERM** if the helper cannot be used under the current
- * *skops*\ **->op**.
+ * operation.
*
- * long bpf_reserve_hdr_opt(struct bpf_sock_ops *skops, u32 len, u64 flags)
+ * long bpf_reserve_hdr_opt(void *ctx, u32 len, u64 flags)
* Description
* Reserve *len* bytes for the bpf header option. The
* space will be used by **bpf_store_hdr_opt**\ () later in
@@ -4905,7 +4912,9 @@ union bpf_attr {
* the total number of bytes will be reserved.
*
* This helper can only be called during
- * **BPF_SOCK_OPS_HDR_OPT_LEN_CB**.
+ * **BPF_SOCK_OPS_HDR_OPT_LEN_CB**, or from the
+ * **hdr_opt_len**\ () operator of the **bpf_tcp_ops**
+ * struct_ops.
*
* Return
* 0 on success, or negative error in case of failure:
@@ -4915,7 +4924,7 @@ union bpf_attr {
* **-ENOSPC** if there is not enough space in the header.
*
* **-EPERM** if the helper cannot be used under the current
- * *skops*\ **->op**.
+ * operation.
*
* void *bpf_inode_storage_get(struct bpf_map *map, void *inode, void *value, u64 flags)
* Description
@@ -6815,6 +6824,8 @@ struct bpf_link_info {
} xdp;
struct {
__u32 map_id;
+ __u32 :32;
+ __u64 cgroup_id;
} struct_ops;
struct {
__u32 pf;
diff --git a/kernel/bpf/bpf_struct_ops.c b/kernel/bpf/bpf_struct_ops.c
index 51b16e5f5534e0..85cfc7e88d3fd6 100644
--- a/kernel/bpf/bpf_struct_ops.c
+++ b/kernel/bpf/bpf_struct_ops.c
@@ -13,6 +13,7 @@
#include <linux/btf_ids.h>
#include <linux/rcupdate_wait.h>
#include <linux/poll.h>
+#include <linux/bpf-cgroup.h>
struct bpf_struct_ops_value {
struct bpf_struct_ops_common_value common;
@@ -57,7 +58,7 @@ struct bpf_struct_ops_map {
struct bpf_struct_ops_link {
struct bpf_link link;
- struct bpf_map __rcu *map;
+ struct bpf_map *map;
wait_queue_head_t wait_hup;
};
@@ -984,9 +985,18 @@ static void __bpf_struct_ops_map_free(struct bpf_map *map)
bpf_map_area_free(st_map);
}
+static void bpf_struct_ops_map_free_pre_rcu(struct bpf_map *map)
+{
+ struct bpf_struct_ops_map *st_map = (struct bpf_struct_ops_map *)map;
+
+ bpf_struct_ops_map_del_ksyms(st_map);
+}
+
static void bpf_struct_ops_map_free(struct bpf_map *map)
{
struct bpf_struct_ops_map *st_map = (struct bpf_struct_ops_map *)map;
+ struct bpf_struct_ops *st_ops = st_map->st_ops_desc->st_ops;
+ bool tasks_rcu = st_ops->free_after_tasks_rcu_gp;
/* st_ops->owner was acquired during map_alloc to implicitly holds
* the btf's refcnt. The acquire was only done when btf_is_module()
@@ -997,24 +1007,8 @@ static void bpf_struct_ops_map_free(struct bpf_map *map)
bpf_struct_ops_map_dissoc_progs(st_map);
- bpf_struct_ops_map_del_ksyms(st_map);
-
- /* The struct_ops's function may switch to another struct_ops.
- *
- * For example, bpf_tcp_cc_x->init() may switch to
- * another tcp_cc_y by calling
- * setsockopt(TCP_CONGESTION, "tcp_cc_y").
- * During the switch, bpf_struct_ops_put(tcp_cc_x) is called
- * and its refcount may reach 0 which then free its
- * trampoline image while tcp_cc_x is still running.
- *
- * A vanilla rcu gp is to wait for all bpf-tcp-cc prog
- * to finish. bpf-tcp-cc prog is non sleepable.
- * A rcu_tasks gp is to wait for the last few insn
- * in the tramopline image to finish before releasing
- * the trampoline image.
- */
- synchronize_rcu_mult(call_rcu, call_rcu_tasks);
+ if (tasks_rcu && IS_ENABLED(CONFIG_TASKS_RCU))
+ synchronize_rcu_tasks();
__bpf_struct_ops_map_free(map);
}
@@ -1083,6 +1077,11 @@ static struct bpf_map *bpf_struct_ops_map_alloc(union bpf_attr *attr)
goto errout;
}
+ if (st_ops_desc->st_ops->cgroup_atype && !(attr->map_flags & BPF_F_LINK)) {
+ ret = -EOPNOTSUPP;
+ goto errout;
+ }
+
vt = st_ops_desc->value_type;
if (attr->value_size != vt->size) {
ret = -EINVAL;
@@ -1123,6 +1122,8 @@ static struct bpf_map *bpf_struct_ops_map_alloc(union bpf_attr *attr)
mutex_init(&st_map->lock);
bpf_map_init_from_attr(map, attr);
+ map->free_after_mult_rcu_gp = st_ops_desc->st_ops->free_after_mult_rcu_gp;
+ map->free_after_rcu_gp = true;
return map;
@@ -1155,6 +1156,7 @@ const struct bpf_map_ops bpf_struct_ops_map_ops = {
.map_alloc_check = bpf_struct_ops_map_alloc_check,
.map_alloc = bpf_struct_ops_map_alloc,
.map_free = bpf_struct_ops_map_free,
+ .map_free_pre_rcu = bpf_struct_ops_map_free_pre_rcu,
.map_get_next_key = bpf_struct_ops_map_get_next_key,
.map_lookup_elem = bpf_struct_ops_map_lookup_elem,
.map_delete_elem = bpf_struct_ops_map_delete_elem,
@@ -1241,7 +1243,41 @@ int bpf_struct_ops_for_each_prog(const void *kdata,
}
EXPORT_SYMBOL_GPL(bpf_struct_ops_for_each_prog);
-static bool bpf_struct_ops_valid_to_reg(struct bpf_map *map)
+void *bpf_struct_ops_map_kdata(struct bpf_map *map)
+{
+ struct bpf_struct_ops_map *st_map;
+
+ st_map = container_of(map, struct bpf_struct_ops_map, map);
+ return st_map->kvalue.data;
+}
+
+u32 bpf_struct_ops_kdata_map_id(void *kdata)
+{
+ struct bpf_struct_ops_value *kvalue =
+ container_of(kdata, struct bpf_struct_ops_value, data);
+ struct bpf_struct_ops_map *st_map =
+ container_of(kvalue, struct bpf_struct_ops_map, kvalue);
+
+ return st_map->map.id;
+}
+
+int bpf_struct_ops_map_cgroup_atype(struct bpf_map *map)
+{
+ struct bpf_struct_ops_map *st_map;
+
+ st_map = container_of(map, struct bpf_struct_ops_map, map);
+ return st_map->st_ops_desc->st_ops->cgroup_atype;
+}
+
+void *bpf_struct_ops_map_cfi_stubs(struct bpf_map *map)
+{
+ struct bpf_struct_ops_map *st_map;
+
+ st_map = container_of(map, struct bpf_struct_ops_map, map);
+ return st_map->st_ops_desc->st_ops->cfi_stubs;
+}
+
+bool bpf_struct_ops_valid_to_reg(struct bpf_map *map)
{
struct bpf_struct_ops_map *st_map = (struct bpf_struct_ops_map *)map;
@@ -1257,8 +1293,7 @@ static void bpf_struct_ops_map_link_dealloc(struct bpf_link *link)
struct bpf_struct_ops_map *st_map;
st_link = container_of(link, struct bpf_struct_ops_link, link);
- st_map = (struct bpf_struct_ops_map *)
- rcu_dereference_protected(st_link->map, true);
+ st_map = (struct bpf_struct_ops_map *)st_link->map;
if (st_map) {
st_map->st_ops_desc->st_ops->unreg(&st_map->kvalue.data, link);
bpf_map_put(&st_map->map);
@@ -1273,11 +1308,11 @@ static void bpf_struct_ops_map_link_show_fdinfo(const struct bpf_link *link,
struct bpf_map *map;
st_link = container_of(link, struct bpf_struct_ops_link, link);
- rcu_read_lock();
- map = rcu_dereference(st_link->map);
+ mutex_lock(&update_mutex);
+ map = st_link->map;
if (map)
seq_printf(seq, "map_id:\t%d\n", map->id);
- rcu_read_unlock();
+ mutex_unlock(&update_mutex);
}
static int bpf_struct_ops_map_link_fill_link_info(const struct bpf_link *link,
@@ -1287,11 +1322,31 @@ static int bpf_struct_ops_map_link_fill_link_info(const struct bpf_link *link,
struct bpf_map *map;
st_link = container_of(link, struct bpf_struct_ops_link, link);
- rcu_read_lock();
- map = rcu_dereference(st_link->map);
+ mutex_lock(&update_mutex);
+ map = st_link->map;
if (map)
info->struct_ops.map_id = map->id;
- rcu_read_unlock();
+ mutex_unlock(&update_mutex);
+ return 0;
+}
+
+int bpf_struct_ops_link_update_check(struct bpf_map *new_map,
+ struct bpf_map *old_map,
+ struct bpf_map *expected_old_map)
+{
+ struct bpf_struct_ops_map *st_map, *old_st_map;
+
+ if (!old_map)
+ return -ENOLINK;
+ if (expected_old_map && old_map != expected_old_map)
+ return -EPERM;
+
+ st_map = container_of(new_map, struct bpf_struct_ops_map, map);
+ old_st_map = container_of(old_map, struct bpf_struct_ops_map, map);
+ /* The new and old struct_ops must be the same type. */
+ if (st_map->st_ops_desc != old_st_map->st_ops_desc)
+ return -EINVAL;
+
return 0;
}
@@ -1313,30 +1368,19 @@ static int bpf_struct_ops_map_link_update(struct bpf_link *link, struct bpf_map
return -EOPNOTSUPP;
mutex_lock(&update_mutex);
-
- old_map = rcu_dereference_protected(st_link->map, lockdep_is_held(&update_mutex));
- if (!old_map) {
- err = -ENOLINK;
- goto err_out;
- }
- if (expected_old_map && old_map != expected_old_map) {
- err = -EPERM;
+ old_map = st_link->map;
+ err = bpf_struct_ops_link_update_check(new_map, old_map, expected_old_map);
+ if (err)
goto err_out;
- }
old_st_map = container_of(old_map, struct bpf_struct_ops_map, map);
- /* The new and old struct_ops must be the same type. */
- if (st_map->st_ops_desc != old_st_map->st_ops_desc) {
- err = -EINVAL;
- goto err_out;
- }
err = st_map->st_ops_desc->st_ops->update(st_map->kvalue.data, old_st_map->kvalue.data, link);
if (err)
goto err_out;
bpf_map_inc(new_map);
- rcu_assign_pointer(st_link->map, new_map);
+ WRITE_ONCE(st_link->map, new_map);
bpf_map_put(old_map);
err_out:
@@ -1353,7 +1397,7 @@ static int bpf_struct_ops_map_link_detach(struct bpf_link *link)
mutex_lock(&update_mutex);
- map = rcu_dereference_protected(st_link->map, lockdep_is_held(&update_mutex));
+ map = st_link->map;
if (!map) {
mutex_unlock(&update_mutex);
return 0;
@@ -1362,7 +1406,7 @@ static int bpf_struct_ops_map_link_detach(struct bpf_link *link)
st_map->st_ops_desc->st_ops->unreg(&st_map->kvalue.data, link);
- RCU_INIT_POINTER(st_link->map, NULL);
+ WRITE_ONCE(st_link->map, NULL);
/* Pair with bpf_map_get() in bpf_struct_ops_link_create() or
* bpf_map_inc() in bpf_struct_ops_map_link_update().
*/
@@ -1382,7 +1426,7 @@ static __poll_t bpf_struct_ops_map_link_poll(struct file *file,
poll_wait(file, &st_link->wait_hup, pts);
- return rcu_access_pointer(st_link->map) ? 0 : EPOLLHUP;
+ return READ_ONCE(st_link->map) ? 0 : EPOLLHUP;
}
static const struct bpf_link_ops bpf_struct_ops_map_lops = {
@@ -1400,6 +1444,7 @@ int bpf_struct_ops_link_create(union bpf_attr *attr)
struct bpf_link_primer link_primer;
struct bpf_struct_ops_map *st_map;
struct bpf_map *map;
+ int cgroup_atype;
int err;
map = bpf_map_get(attr->link_create.map_fd);
@@ -1413,6 +1458,19 @@ int bpf_struct_ops_link_create(union bpf_attr *attr)
goto err_out;
}
+ cgroup_atype = st_map->st_ops_desc->st_ops->cgroup_atype;
+ if (cgroup_atype) {
+ err = cgroup_bpf_struct_ops_attach(map, attr);
+ bpf_map_put(map);
+ return err;
+ }
+
+ if (memchr_inv(&attr->link_create.cgroup, 0, sizeof(attr->link_create.cgroup)) ||
+ attr->link_create.target_fd) {
+ err = -EINVAL;
+ goto err_out;
+ }
+
link = kzalloc_obj(*link, GFP_USER);
if (!link) {
err = -ENOMEM;
@@ -1438,7 +1496,7 @@ int bpf_struct_ops_link_create(union bpf_attr *attr)
link = NULL;
goto err_out;
}
- RCU_INIT_POINTER(link->map, map);
+ link->map = map;
mutex_unlock(&update_mutex);
return bpf_link_settle(&link_primer);
diff --git a/kernel/bpf/btf.c b/kernel/bpf/btf.c
index 64572f85edc832..d591f306ace526 100644
--- a/kernel/bpf/btf.c
+++ b/kernel/bpf/btf.c
@@ -20,6 +20,7 @@
#include <linux/btf.h>
#include <linux/btf_ids.h>
#include <linux/bpf.h>
+#include <linux/bpf-cgroup.h>
#include <linux/bpf_lsm.h>
#include <linux/skmsg.h>
#include <linux/perf_event.h>
@@ -9836,6 +9837,7 @@ btf_add_struct_ops(struct btf *btf, struct bpf_struct_ops *st_ops,
struct bpf_verifier_log *log)
{
struct btf_struct_ops_tab *tab, *new_tab;
+ int cgroup_atype;
int i, err;
tab = btf->struct_ops_tab;
@@ -9847,8 +9849,10 @@ btf_add_struct_ops(struct btf *btf, struct bpf_struct_ops *st_ops,
btf->struct_ops_tab = tab;
}
+ cgroup_atype = st_ops->cgroup_atype;
for (i = 0; i < tab->cnt; i++)
- if (tab->ops[i].st_ops == st_ops)
+ if (tab->ops[i].st_ops == st_ops ||
+ (cgroup_atype && cgroup_atype == tab->ops[i].st_ops->cgroup_atype))
return -EEXIST;
if (tab->cnt == tab->capacity) {
@@ -9868,6 +9872,23 @@ btf_add_struct_ops(struct btf *btf, struct bpf_struct_ops *st_ops,
if (err)
return err;
+ if (cgroup_atype) {
+ if (!cgroup_bpf_is_struct_ops_atype(cgroup_atype) ||
+ st_ops->reg || st_ops->unreg || st_ops->free_after_tasks_rcu_gp) {
+ bpf_struct_ops_desc_release(&tab->ops[btf->struct_ops_tab->cnt]);
+ return -EINVAL;
+ }
+
+ /* There is no need to unregister from cgroup when the
+ * btf_free(). No struct_ops map and its cgroup link
+ * can be created once its btf is gone.
+ */
+ cgroup_bpf_struct_ops_register(cgroup_atype,
+ tab->ops[btf->struct_ops_tab->cnt].type_id,
+ st_ops->cfi_stubs,
+ st_ops->free_after_mult_rcu_gp);
+ }
+
btf->struct_ops_tab->cnt++;
return 0;
diff --git a/kernel/bpf/cgroup.c b/kernel/bpf/cgroup.c
index 4355ccb78a9c67..a808d2a31f11dd 100644
--- a/kernel/bpf/cgroup.c
+++ b/kernel/bpf/cgroup.c
@@ -24,6 +24,29 @@
DEFINE_STATIC_KEY_ARRAY_FALSE(cgroup_bpf_enabled_key, MAX_CGROUP_BPF_ATTACH_TYPE);
EXPORT_SYMBOL(cgroup_bpf_enabled_key);
+static u32 struct_ops_type_id[MAX_CGROUP_BPF_ATTACH_TYPE];
+static void *struct_ops_cfi_stubs[MAX_CGROUP_BPF_ATTACH_TYPE];
+static bool struct_ops_mult_rcu[MAX_CGROUP_BPF_ATTACH_TYPE];
+
+void cgroup_bpf_struct_ops_register(int atype, u32 type_id, void *cfi_stubs, bool mult_rcu)
+{
+ struct_ops_type_id[atype] = type_id;
+ struct_ops_cfi_stubs[atype] = cfi_stubs;
+ struct_ops_mult_rcu[atype] = mult_rcu;
+}
+
+static enum cgroup_bpf_attach_type find_atype_by_struct_ops_id(u32 type_id)
+{
+ enum cgroup_bpf_attach_type atype;
+
+ for (atype = 0; atype < MAX_CGROUP_BPF_ATTACH_TYPE; atype++) {
+ if (cgroup_bpf_is_struct_ops_atype(atype) &&
+ struct_ops_type_id[atype] == type_id)
+ return atype;
+ }
+ return CGROUP_BPF_ATTACH_TYPE_INVALID;
+}
+
/*
* cgroup bpf destruction makes heavy use of work items and there can be a lot
* of concurrent destructions. Use a separate workqueue so that cgroup bpf
@@ -306,6 +329,19 @@ static void bpf_cgroup_storages_link(struct bpf_cgroup_storage *storages[],
bpf_cgroup_storage_link(storages[stype], cgrp, attach_type);
}
+static void cgroup_struct_ops_link_detach_wake(struct bpf_cgroup_link *link, bool wake_poll)
+{
+ cgroup_put(link->cgroup);
+ link->cgroup = NULL;
+
+ bpf_map_put(link->map);
+ /* READ_ONCE in cgroup_struct_ops_link_poll */
+ WRITE_ONCE(link->map, NULL);
+
+ if (wake_poll)
+ wake_up_interruptible_poll(&link->wait_hup, EPOLLHUP);
+}
+
/* Called when bpf_cgroup_link is auto-detached from dying cgroup.
* It drops cgroup and bpf_prog refcounts, and marks bpf_link as defunct. It
* doesn't free link memory, which will eventually be done by bpf_link's
@@ -313,8 +349,92 @@ static void bpf_cgroup_storages_link(struct bpf_cgroup_storage *storages[],
*/
static void bpf_cgroup_link_auto_detach(struct bpf_cgroup_link *link)
{
- cgroup_put(link->cgroup);
- link->cgroup = NULL;
+ if (link->map) {
+ cgroup_struct_ops_link_detach_wake(link, true);
+ } else {
+ if (link->link.prog->expected_attach_type == BPF_LSM_CGROUP)
+ bpf_trampoline_unlink_cgroup_shim(link->link.prog);
+ cgroup_put(link->cgroup);
+ link->cgroup = NULL;
+ }
+}
+
+static void bpf_cgroup_array_free_rcu(struct rcu_head *rcu)
+{
+ kfree(container_of(rcu, struct bpf_prog_array, rcu));
+}
+
+static void bpf_cgroup_array_free(struct bpf_prog_array *array,
+ enum cgroup_bpf_attach_type atype)
+{
+ if (!array || array == &bpf_empty_prog_array)
+ return;
+ if (struct_ops_mult_rcu[atype])
+ /* RCU tasks trace grace period implies RCU grace period. */
+ call_rcu_tasks_trace(&array->rcu, bpf_cgroup_array_free_rcu);
+ else
+ kfree_rcu(array, rcu);
+}
+
+static void *bpf_cgroup_array_dummy(enum cgroup_bpf_attach_type atype)
+{
+ if (cgroup_bpf_is_struct_ops_atype(atype))
+ return struct_ops_cfi_stubs[atype];
+ return bpf_prog_dummy();
+}
+
+static int bpf_cgroup_array_length(struct bpf_prog_array *array,
+ enum cgroup_bpf_attach_type atype)
+{
+ struct bpf_prog_array_item *item;
+ int cnt = 0;
+
+ for (item = array->items; item->prog; item++)
+ if (item->prog != bpf_cgroup_array_dummy(atype))
+ cnt++;
+
+ return cnt;
+}
+
+static int bpf_cgroup_array_copy_to_user(struct bpf_prog_array *array,
+ __u32 __user *prog_ids, int cnt,
+ enum cgroup_bpf_attach_type atype)
+{
+ struct bpf_prog_array_item *item;
+ int i = 0;
+ u32 id;
+
+ for (item = array->items; item->prog && i < cnt; item++) {
+ if (item->prog == bpf_cgroup_array_dummy(atype))
+ continue;
+
+ if (cgroup_bpf_is_struct_ops_atype(atype))
+ id = bpf_struct_ops_kdata_map_id(item->kdata);
+ else
+ id = item->prog->aux->id;
+
+ if (copy_to_user(prog_ids + i, &id, sizeof(id)))
+ return -EFAULT;
+ i++;
+ }
+ return item->prog ? -ENOSPC : 0;
+}
+
+static int bpf_cgroup_array_delete_safe_at(struct bpf_prog_array *array,
+ int index, enum cgroup_bpf_attach_type atype)
+{
+ struct bpf_prog_array_item *item;
+
+ for (item = array->items; item->prog; item++) {
+ if (item->prog == bpf_cgroup_array_dummy(atype))
+ continue;
+ if (!index) {
+ WRITE_ONCE(item->prog, bpf_cgroup_array_dummy(atype));
+ return 0;
+ }
+ index--;
+ }
+ return -ENOENT;
}
/**
@@ -346,18 +466,15 @@ static void cgroup_bpf_release(struct work_struct *work)
bpf_trampoline_unlink_cgroup_shim(pl->prog);
bpf_prog_put(pl->prog);
}
- if (pl->link) {
- if (pl->link->link.prog->expected_attach_type == BPF_LSM_CGROUP)
- bpf_trampoline_unlink_cgroup_shim(pl->link->link.prog);
+ if (pl->link)
bpf_cgroup_link_auto_detach(pl->link);
- }
kfree(pl);
static_branch_dec(&cgroup_bpf_enabled_key[atype]);
}
old_array = rcu_dereference_protected(
cgrp->bpf.effective[atype],
lockdep_is_held(&cgroup_mutex));
- bpf_prog_array_free(old_array);
+ bpf_cgroup_array_free(old_array, atype);
}
list_for_each_entry_safe(storage, stmp, storages, list_cg) {
@@ -399,6 +516,31 @@ static struct bpf_prog *prog_list_prog(struct bpf_prog_list *pl)
return NULL;
}
+static void prog_list_init_item(struct bpf_prog_list *pl, struct bpf_prog_array_item *item)
+{
+ if (pl->link && pl->link->map) {
+ item->kdata = bpf_struct_ops_map_kdata(pl->link->map);
+ } else {
+ item->prog = prog_list_prog(pl);
+ bpf_cgroup_storages_assign(item->cgroup_storage, pl->storage);
+ }
+}
+
+static void prog_list_replace_item(struct bpf_prog_list *pl, struct bpf_prog_array_item *item)
+{
+ if (pl->link && pl->link->map)
+ WRITE_ONCE(item->kdata, bpf_struct_ops_map_kdata(pl->link->map));
+ else
+ WRITE_ONCE(item->prog, pl->link->link.prog);
+}
+
+static u32 prog_list_id(struct bpf_prog_list *pl)
+{
+ if (pl->link && pl->link->map)
+ return pl->link->map->id;
+ return prog_list_prog(pl)->aux->id;
+}
+
/* count number of elements in the list.
* it's slow but the list cannot be long
*/
@@ -408,7 +550,7 @@ static u32 prog_list_length(struct hlist_head *head, int *preorder_cnt)
u32 cnt = 0;
hlist_for_each_entry(pl, head, node) {
- if (!prog_list_prog(pl))
+ if (!pl->prog && !pl->link)
continue;
if (preorder_cnt && (pl->flags & BPF_F_PREORDER))
(*preorder_cnt)++;
@@ -482,7 +624,7 @@ static int compute_effective_progs(struct cgroup *cgrp,
init_bstart = bstart;
hlist_for_each_entry(pl, &p->bpf.progs[atype], node) {
- if (!prog_list_prog(pl))
+ if (!pl->prog && !pl->link)
continue;
if (pl->flags & BPF_F_PREORDER) {
@@ -492,9 +634,7 @@ static int compute_effective_progs(struct cgroup *cgrp,
item = &progs->items[fstart];
fstart++;
}
- item->prog = prog_list_prog(pl);
- bpf_cgroup_storages_assign(item->cgroup_storage,
- pl->storage);
+ prog_list_init_item(pl, item);
cnt++;
}
@@ -517,7 +657,7 @@ static void activate_effective_progs(struct cgroup *cgrp,
/* free prog array after grace period, since __cgroup_bpf_run_*()
* might be still walking the array
*/
- bpf_prog_array_free(old_array);
+ bpf_cgroup_array_free(old_array, atype);
}
/**
@@ -557,7 +697,7 @@ static int cgroup_bpf_inherit(struct cgroup *cgrp)
return 0;
cleanup:
for (i = 0; i < NR; i++)
- bpf_prog_array_free(arrays[i]);
+ bpf_cgroup_array_free(arrays[i], i);
for (p = cgroup_parent(cgrp); p; p = cgroup_parent(p))
cgroup_bpf_put(p);
@@ -612,7 +752,7 @@ static int update_effective_progs(struct cgroup *cgrp,
if (percpu_ref_is_zero(&desc->bpf.refcnt)) {
if (unlikely(desc->bpf.inactive)) {
- bpf_prog_array_free(desc->bpf.inactive);
+ bpf_cgroup_array_free(desc->bpf.inactive, atype);
desc->bpf.inactive = NULL;
}
continue;
@@ -631,7 +771,7 @@ static int update_effective_progs(struct cgroup *cgrp,
css_for_each_descendant_pre(css, &cgrp->self) {
struct cgroup *desc = container_of(css, struct cgroup, self);
- bpf_prog_array_free(desc->bpf.inactive);
+ bpf_cgroup_array_free(desc->bpf.inactive, atype);
desc->bpf.inactive = NULL;
}
@@ -866,7 +1006,7 @@ static int __cgroup_bpf_attach(struct cgroup *cgrp,
if (pl) {
old_prog = pl->prog;
} else {
- pl = kmalloc_obj(*pl);
+ pl = kzalloc_obj(*pl);
if (!pl) {
bpf_cgroup_storages_free(new_storage);
return -ENOMEM;
@@ -965,9 +1105,10 @@ static int effective_prog_pos(struct cgroup *cgrp,
init_bstart = bstart;
hlist_for_each_entry(pl, &p->bpf.progs[atype], node) {
- if (!prog_list_prog(pl))
- continue;
-
+ /*
+ * No detaching pl (NULL prog and link) is visible to the callers,
+ * so skip the check compute_effective_progs() needs.
+ */
if (pl->flags & BPF_F_PREORDER) {
if (pl == target_pl)
pos = bstart;
@@ -1014,7 +1155,7 @@ static void replace_effective_prog(struct cgroup *cgrp,
desc->bpf.effective[atype],
lockdep_is_held(&cgroup_mutex));
item = &progs->items[pos];
- WRITE_ONCE(item->prog, pl->link->link.prog);
+ prog_list_replace_item(pl, item);
}
}
@@ -1152,7 +1293,7 @@ static void purge_effective_progs(struct cgroup *cgrp, struct bpf_prog_list *pl,
lockdep_is_held(&cgroup_mutex));
/* Remove the program from the array */
- WARN_ONCE(bpf_prog_array_delete_safe_at(progs, pos),
+ WARN_ONCE(bpf_cgroup_array_delete_safe_at(progs, pos, atype),
"Failed to purge a prog from array at index %d", pos);
}
}
@@ -1262,7 +1403,17 @@ static int __cgroup_bpf_query(struct cgroup *cgrp, const union bpf_attr *attr,
if (effective_query && prog_attach_flags)
return -EINVAL;
- if (type == BPF_LSM_CGROUP) {
+ if (type == BPF_STRUCT_OPS) {
+ u32 type_id = attr->query.type_id;
+
+ atype = find_atype_by_struct_ops_id(type_id);
+ if (atype == CGROUP_BPF_ATTACH_TYPE_INVALID)
+ return -ENOENT;
+ from_atype = to_atype = atype;
+ flags = 0;
+ if (!cgroup_bpf_enabled(atype))
+ goto skip_count;
+ } else if (type == BPF_LSM_CGROUP) {
if (!effective_query && attr->query.prog_cnt &&
prog_ids && !prog_attach_flags)
return -EINVAL;
@@ -1282,12 +1433,13 @@ static int __cgroup_bpf_query(struct cgroup *cgrp, const union bpf_attr *attr,
if (effective_query) {
effective = rcu_dereference_protected(cgrp->bpf.effective[atype],
lockdep_is_held(&cgroup_mutex));
- total_cnt += bpf_prog_array_length(effective);
+ total_cnt += bpf_cgroup_array_length(effective, atype);
} else {
total_cnt += prog_list_length(&cgrp->bpf.progs[atype], NULL);
}
}
+skip_count:
/* always output uattr->query.attach_flags as 0 during effective query */
flags = effective_query ? 0 : flags;
if (copy_to_user(&uattr->query.attach_flags, &flags, sizeof(flags)))
@@ -1312,20 +1464,18 @@ static int __cgroup_bpf_query(struct cgroup *cgrp, const union bpf_attr *attr,
if (effective_query) {
effective = rcu_dereference_protected(cgrp->bpf.effective[atype],
lockdep_is_held(&cgroup_mutex));
- cnt = min_t(int, bpf_prog_array_length(effective), total_cnt);
- ret = bpf_prog_array_copy_to_user(effective, prog_ids, cnt);
+ cnt = min_t(int, bpf_cgroup_array_length(effective, atype), total_cnt);
+ ret = bpf_cgroup_array_copy_to_user(effective, prog_ids, cnt, atype);
} else {
struct hlist_head *progs;
struct bpf_prog_list *pl;
- struct bpf_prog *prog;
u32 id;
progs = &cgrp->bpf.progs[atype];
cnt = min_t(int, prog_list_length(progs, NULL), total_cnt);
i = 0;
hlist_for_each_entry(pl, progs, node) {
- prog = prog_list_prog(pl);
- id = prog->aux->id;
+ id = prog_list_id(pl);
if (copy_to_user(prog_ids + i, &id, sizeof(id)))
return -EFAULT;
if (++i == cnt)
@@ -2747,6 +2897,270 @@ const struct bpf_verifier_ops cg_sockopt_verifier_ops = {
const struct bpf_prog_ops cg_sockopt_prog_ops = {
};
+static int __cgroup_struct_ops_link_detach(struct bpf_link *link, bool wake_poll)
+{
+ struct bpf_cgroup_link *cg_link = container_of(link, struct bpf_cgroup_link, link);
+ enum cgroup_bpf_attach_type atype;
+ struct bpf_prog_list *pl;
+ struct bpf_map *map;
+ struct cgroup *cgrp;
+
+ cgroup_lock();
+
+ cgrp = cg_link->cgroup;
+ if (!cgrp) {
+ cgroup_unlock();
+ return 0;
+ }
+
+ map = cg_link->map;
+ atype = bpf_struct_ops_map_cgroup_atype(map);
+
+ hlist_for_each_entry(pl, &cgrp->bpf.progs[atype], node) {
+ if (pl->link == cg_link)
+ break;
+ }
+
+ /* mark deleted so compute_effective_progs() skips it */
+ pl->link = NULL;
+ if (update_effective_progs(cgrp, atype)) {
+ pl->link = cg_link;
+ purge_effective_progs(cgrp, pl, atype);
+ }
+
+ hlist_del(&pl->node);
+ cgroup_struct_ops_link_detach_wake(cg_link, wake_poll);
+ cgrp->bpf.revisions[atype]++;
+
+ kfree(pl);
+ static_branch_dec(&cgroup_bpf_enabled_key[atype]);
+
+ cgroup_unlock();
+
+ return 0;
+}
+
+static int cgroup_struct_ops_link_detach(struct bpf_link *link)
+{
+ return __cgroup_struct_ops_link_detach(link, true);
+}
+
+static void cgroup_struct_ops_link_dealloc(struct bpf_link *link)
+{
+ struct bpf_cgroup_link *cg_link = container_of(link, struct bpf_cgroup_link, link);
+
+ __cgroup_struct_ops_link_detach(link, false);
+ kfree(cg_link);
+}
+
+static void cgroup_struct_ops_link_show_fdinfo(const struct bpf_link *link, struct seq_file *seq)
+{
+ struct bpf_cgroup_link *cg_link =
+ container_of(link, struct bpf_cgroup_link, link);
+
+ cgroup_lock();
+ if (!cg_link->cgroup) {
+ cgroup_unlock();
+ return;
+ }
+
+ seq_printf(seq, "map_id:\t%u\n", cg_link->map->id);
+ seq_printf(seq, "cgroup_id:\t%llu\n", cgroup_id(cg_link->cgroup));
+ cgroup_unlock();
+}
+
+static int cgroup_struct_ops_link_fill_link_info(const struct bpf_link *link,
+ struct bpf_link_info *info)
+{
+ struct bpf_cgroup_link *cg_link = container_of(link, struct bpf_cgroup_link, link);
+
+ cgroup_lock();
+ if (!cg_link->cgroup) {
+ cgroup_unlock();
+ return 0;
+ }
+
+ info->struct_ops.map_id = cg_link->map->id;
+ info->struct_ops.cgroup_id = cgroup_id(cg_link->cgroup);
+ cgroup_unlock();
+ return 0;
+}
+
+static int cgroup_struct_ops_link_update(struct bpf_link *link, struct bpf_map *new_map,
+ struct bpf_map *expected_old_map)
+{
+ struct bpf_cgroup_link *cg_link = container_of(link, struct bpf_cgroup_link, link);
+ enum cgroup_bpf_attach_type atype;
+ struct bpf_prog_list *pl;
+ struct bpf_map *old_map;
+ struct cgroup *cgrp;
+ bool found = false;
+ int err;
+
+ if (!bpf_struct_ops_valid_to_reg(new_map))
+ return -EINVAL;
+
+ cgroup_lock();
+
+ cgrp = cg_link->cgroup;
+ if (!cgrp) {
+ err = -ENOLINK;
+ goto out;
+ }
+
+ old_map = cg_link->map;
+ err = bpf_struct_ops_link_update_check(new_map, old_map, expected_old_map);
+ if (err)
+ goto out;
+
+ atype = bpf_struct_ops_map_cgroup_atype(new_map);
+
+ hlist_for_each_entry(pl, &cgrp->bpf.progs[atype], node) {
+ if (pl->link == cg_link) {
+ found = true;
+ break;
+ }
+ }
+ if (!found) {
+ err = -ENOENT;
+ goto out;
+ }
+
+ bpf_map_inc(new_map);
+ WRITE_ONCE(cg_link->map, new_map);
+ replace_effective_prog(cgrp, atype, pl);
+ bpf_map_put(old_map);
+ cgrp->bpf.revisions[atype]++;
+
+out:
+ cgroup_unlock();
+ return err;
+}
+
+static __poll_t cgroup_struct_ops_link_poll(struct file *file, struct poll_table_struct *pts)
+{
+ struct bpf_cgroup_link *link = file->private_data;
+
+ poll_wait(file, &link->wait_hup, pts);
+
+ return READ_ONCE(link->map) ? 0 : EPOLLHUP;
+}
+
+static const struct bpf_link_ops cgroup_struct_ops_link_ops = {
+ .dealloc = cgroup_struct_ops_link_dealloc,
+ .detach = cgroup_struct_ops_link_detach,
+ .show_fdinfo = cgroup_struct_ops_link_show_fdinfo,
+ .fill_link_info = cgroup_struct_ops_link_fill_link_info,
+ .update_map = cgroup_struct_ops_link_update,
+ .poll = cgroup_struct_ops_link_poll,
+};
+
+int cgroup_bpf_struct_ops_attach(struct bpf_map *map, const union bpf_attr *attr)
+{
+ u32 flags = attr->link_create.flags;
+ u32 pl_flags = (flags & BPF_F_PREORDER) | BPF_F_ALLOW_MULTI;
+ enum cgroup_bpf_attach_type atype;
+ struct bpf_link_primer link_primer;
+ struct bpf_cgroup_link *link;
+ struct bpf_prog_list *pl = NULL;
+ struct hlist_head *progs;
+ struct cgroup *cgrp;
+ int err;
+
+ if (flags & ~BPF_F_LINK_ATTACH_MASK)
+ return -EINVAL;
+
+ /*
+ * Attaching struct_ops to cgroup is through link only. All relative
+ * position must be corresponding to a link id or fd.
+ */
+ if (attr->link_create.cgroup.relative_fd && !(flags & BPF_F_LINK))
+ return -EINVAL;
+
+ link = kzalloc_obj(*link, GFP_USER);
+ if (!link)
+ return -ENOMEM;
+
+ bpf_link_init(&link->link, BPF_LINK_TYPE_STRUCT_OPS,
+ &cgroup_struct_ops_link_ops, NULL,
+ attr->link_create.attach_type);
+
+ err = bpf_link_prime(&link->link, &link_primer);
+ if (err) {
+ kfree(link);
+ return err;
+ }
+
+ cgrp = cgroup_get_from_fd(attr->link_create.target_fd);
+ if (IS_ERR(cgrp)) {
+ err = PTR_ERR(cgrp);
+ goto cleanup;
+ }
+
+ bpf_map_inc(map);
+ link->map = map;
+ link->cgroup = cgrp;
+ init_waitqueue_head(&link->wait_hup);
+
+ atype = bpf_struct_ops_map_cgroup_atype(map);
+ progs = &cgrp->bpf.progs[atype];
+
+ cgroup_lock();
+
+ if (attr->link_create.cgroup.expected_revision &&
+ attr->link_create.cgroup.expected_revision != cgrp->bpf.revisions[atype]) {
+ err = -ESTALE;
+ goto unlock;
+ }
+
+ if (prog_list_length(progs, NULL) >= BPF_CGROUP_MAX_PROGS) {
+ err = -E2BIG;
+ goto unlock;
+ }
+
+ pl = kzalloc_obj(*pl);
+ if (!pl) {
+ err = -ENOMEM;
+ goto unlock;
+ }
+
+ pl->link = link;
+ pl->flags = pl_flags;
+ cgrp->bpf.flags[atype] = BPF_F_ALLOW_MULTI;
+
+ err = insert_pl_to_hlist(pl, progs, NULL, link,
+ flags | BPF_F_ALLOW_MULTI, attr->link_create.cgroup.relative_fd);
+ if (err)
+ goto unlock;
+
+ err = update_effective_progs(cgrp, atype);
+ if (err) {
+ hlist_del(&pl->node);
+ goto unlock;
+ }
+
+ cgrp->bpf.revisions[atype]++;
+ static_branch_inc(&cgroup_bpf_enabled_key[atype]);
+
+ cgroup_unlock();
+
+ return bpf_link_settle(&link_primer);
+
+unlock:
+ cgroup_unlock();
+
+cleanup:
+ kfree(pl);
+ if (link->cgroup) {
+ cgroup_put(link->cgroup);
+ link->cgroup = NULL;
+ bpf_map_put(link->map);
+ link->map = NULL;
+ }
+ bpf_link_cleanup(&link_primer);
+ return err;
+}
+
/* Common helpers for cgroup hooks. */
const struct bpf_func_proto *
cgroup_common_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
diff --git a/kernel/bpf/core.c b/kernel/bpf/core.c
index 649cce41e13f3b..1837bb7bb4e9d6 100644
--- a/kernel/bpf/core.c
+++ b/kernel/bpf/core.c
@@ -2740,6 +2740,11 @@ void bpf_prog_array_free_sleepable(struct bpf_prog_array *progs)
call_rcu_tasks_trace(&progs->rcu, __bpf_prog_array_free_sleepable_cb);
}
+struct bpf_prog *bpf_prog_dummy(void)
+{
+ return &dummy_bpf_prog.prog;
+}
+
int bpf_prog_array_length(struct bpf_prog_array *array)
{
struct bpf_prog_array_item *item;
diff --git a/kernel/bpf/syscall.c b/kernel/bpf/syscall.c
index 6db306d23b479f..b439c0b0eadde7 100644
--- a/kernel/bpf/syscall.c
+++ b/kernel/bpf/syscall.c
@@ -956,6 +956,9 @@ void bpf_map_put(struct bpf_map *map)
/* bpf_map_free_id() must be called first */
bpf_map_free_id(map);
+ if (map->ops->map_free_pre_rcu)
+ map->ops->map_free_pre_rcu(map);
+
WARN_ON_ONCE(atomic64_read(&map->sleepable_refcnt));
/* RCU tasks trace grace period implies RCU grace period. */
if (READ_ONCE(map->free_after_mult_rcu_gp))
@@ -4810,6 +4813,7 @@ static int bpf_prog_query(const union bpf_attr *attr,
case BPF_CGROUP_GETSOCKOPT:
case BPF_CGROUP_SETSOCKOPT:
case BPF_LSM_CGROUP:
+ case BPF_STRUCT_OPS:
return cgroup_bpf_prog_query(attr, uattr, uattr_size);
case BPF_LIRC_MODE2:
return lirc_prog_query(attr, uattr);
diff --git a/net/core/filter.c b/net/core/filter.c
index 1dd5e37ae13027..dc44ffb7a380b9 100644
--- a/net/core/filter.c
+++ b/net/core/filter.c
@@ -7885,17 +7885,14 @@ static const u8 *bpf_search_tcp_opt(const u8 *op, const u8 *opend,
return ERR_PTR(-ENOMSG);
}
-BPF_CALL_4(bpf_sock_ops_load_hdr_opt, struct bpf_sock_ops_kern *, bpf_sock,
- void *, search_res, u32, len, u64, flags)
+int __bpf_sock_ops_load_hdr_opt(struct bpf_sock_ops_kern *bpf_sock,
+ void *search_res, u32 len, u64 flags)
{
bool eol, load_syn = flags & BPF_LOAD_HDR_OPT_TCP_SYN;
const u8 *op, *opend, *magic, *search = search_res;
u8 search_kind, search_len, copy_len, magic_len;
int ret;
- if (!is_locked_tcp_sock_ops(bpf_sock))
- return -EOPNOTSUPP;
-
/* 2 byte is the minimal option len except TCPOPT_NOP and
* TCPOPT_EOL which are useless for the bpf prog to learn
* and this helper disallow loading them also.
@@ -7956,6 +7953,15 @@ BPF_CALL_4(bpf_sock_ops_load_hdr_opt, struct bpf_sock_ops_kern *, bpf_sock,
return ret;
}
+BPF_CALL_4(bpf_sock_ops_load_hdr_opt, struct bpf_sock_ops_kern *, bpf_sock,
+ void *, search_res, u32, len, u64, flags)
+{
+ if (!is_locked_tcp_sock_ops(bpf_sock))
+ return -EOPNOTSUPP;
+
+ return __bpf_sock_ops_load_hdr_opt(bpf_sock, search_res, len, flags);
+}
+
static const struct bpf_func_proto bpf_sock_ops_load_hdr_opt_proto = {
.func = bpf_sock_ops_load_hdr_opt,
.gpl_only = false,
@@ -7966,17 +7972,14 @@ static const struct bpf_func_proto bpf_sock_ops_load_hdr_opt_proto = {
.arg4_type = ARG_ANYTHING,
};
-BPF_CALL_4(bpf_sock_ops_store_hdr_opt, struct bpf_sock_ops_kern *, bpf_sock,
- const void *, from, u32, len, u64, flags)
+int __bpf_sock_ops_store_hdr_opt(struct bpf_sock_ops_kern *bpf_sock,
+ const void *from, u32 len, u64 flags)
{
u8 new_kind, new_kind_len, magic_len = 0, *opend;
const u8 *op, *new_op, *magic = NULL;
struct sk_buff *skb;
bool eol;
- if (bpf_sock->op != BPF_SOCK_OPS_WRITE_HDR_OPT_CB)
- return -EPERM;
-
if (len < 2 || flags)
return -EINVAL;
@@ -8034,6 +8037,15 @@ BPF_CALL_4(bpf_sock_ops_store_hdr_opt, struct bpf_sock_ops_kern *, bpf_sock,
return 0;
}
+BPF_CALL_4(bpf_sock_ops_store_hdr_opt, struct bpf_sock_ops_kern *, bpf_sock,
+ const void *, from, u32, len, u64, flags)
+{
+ if (bpf_sock->op != BPF_SOCK_OPS_WRITE_HDR_OPT_CB)
+ return -EPERM;
+
+ return __bpf_sock_ops_store_hdr_opt(bpf_sock, from, len, flags);
+}
+
static const struct bpf_func_proto bpf_sock_ops_store_hdr_opt_proto = {
.func = bpf_sock_ops_store_hdr_opt,
.gpl_only = false,
@@ -12633,6 +12645,7 @@ static int __init bpf_kfunc_init(void)
ret = ret ?: register_btf_kfunc_id_set(BPF_PROG_TYPE_LWT_SEG6LOCAL, &bpf_kfunc_set_skb);
ret = ret ?: register_btf_kfunc_id_set(BPF_PROG_TYPE_NETFILTER, &bpf_kfunc_set_skb);
ret = ret ?: register_btf_kfunc_id_set(BPF_PROG_TYPE_TRACING, &bpf_kfunc_set_skb);
+ ret = ret ?: register_btf_kfunc_id_set(BPF_PROG_TYPE_STRUCT_OPS, &bpf_kfunc_set_skb);
ret = ret ?: register_btf_kfunc_id_set(BPF_PROG_TYPE_SCHED_CLS, &bpf_kfunc_set_skb_meta);
ret = ret ?: register_btf_kfunc_id_set(BPF_PROG_TYPE_SCHED_ACT, &bpf_kfunc_set_skb_meta);
ret = ret ?: register_btf_kfunc_id_set(BPF_PROG_TYPE_XDP, &bpf_kfunc_set_xdp);
diff --git a/net/ipv4/Makefile b/net/ipv4/Makefile
index 06e21c26b76f2e..afbac63d1cb437 100644
--- a/net/ipv4/Makefile
+++ b/net/ipv4/Makefile
@@ -70,6 +70,7 @@ obj-$(CONFIG_TCP_AO) += tcp_ao.o
ifeq ($(CONFIG_BPF_JIT),y)
obj-$(CONFIG_BPF_SYSCALL) += bpf_tcp_ca.o
+obj-$(CONFIG_CGROUP_BPF) += bpf_tcp_ops.o
endif
ifdef CONFIG_GCOV_PROFILE_NETFILTER
diff --git a/net/ipv4/af_inet.c b/net/ipv4/af_inet.c
index 32d006c1a8eeda..ac8431da67f4dc 100644
--- a/net/ipv4/af_inet.c
+++ b/net/ipv4/af_inet.c
@@ -227,6 +227,7 @@ int __inet_listen_sk(struct sock *sk, int backlog)
return err;
tcp_call_bpf(sk, BPF_SOCK_OPS_TCP_LISTEN_CB, 0, NULL);
+ bpf_tcp_ops_call(listen, sk);
}
return 0;
}
diff --git a/net/ipv4/bpf_tcp_ca.c b/net/ipv4/bpf_tcp_ca.c
index 791e15063237c9..e224ecafbd6992 100644
--- a/net/ipv4/bpf_tcp_ca.c
+++ b/net/ipv4/bpf_tcp_ca.c
@@ -339,6 +339,22 @@ static struct bpf_struct_ops bpf_tcp_congestion_ops = {
.validate = bpf_tcp_ca_validate,
.name = "tcp_congestion_ops",
.cfi_stubs = &__bpf_ops_tcp_congestion_ops,
+ /* The struct_ops's function may switch to another struct_ops.
+ *
+ * For example, bpf_tcp_cc_x->init() may switch to
+ * another tcp_cc_y by calling
+ * setsockopt(TCP_CONGESTION, "tcp_cc_y").
+ * During the switch, bpf_struct_ops_put(tcp_cc_x) is called
+ * and its refcount may reach 0 which then free its
+ * trampoline image while tcp_cc_x is still running.
+ *
+ * A vanilla rcu gp is to wait for all bpf-tcp-cc prog
+ * to finish. bpf-tcp-cc prog is non sleepable.
+ * A rcu_tasks gp is to wait for the last few insn
+ * in the tramopline image to finish before releasing
+ * the trampoline image.
+ */
+ .free_after_tasks_rcu_gp = true,
.owner = THIS_MODULE,
};
diff --git a/net/ipv4/bpf_tcp_ops.c b/net/ipv4/bpf_tcp_ops.c
new file mode 100644
index 00000000000000..0c7352517ac317
--- /dev/null
+++ b/net/ipv4/bpf_tcp_ops.c
@@ -0,0 +1,310 @@
+// SPDX-License-Identifier: GPL-2.0
+/* Copyright (c) 2026 Meta Platforms, Inc. and affiliates. */
+
+#include <linux/bpf.h>
+#include <linux/btf_ids.h>
+#include <linux/bpf_verifier.h>
+#include <linux/filter.h>
+#include <net/bpf_sk_storage.h>
+#include <net/tcp.h>
+
+static int timeout_init_stub(struct sock *sk, struct request_sock *req__nullable)
+{
+ struct bpf_tramp_run_ctx *ctx =
+ container_of(current->bpf_ctx, struct bpf_tramp_run_ctx, run_ctx);
+
+ return ctx->retval;
+}
+
+static int rwnd_init_stub(struct sock *sk, struct request_sock *req__nullable)
+{
+ struct bpf_tramp_run_ctx *ctx =
+ container_of(current->bpf_ctx, struct bpf_tramp_run_ctx, run_ctx);
+
+ return ctx->retval;
+}
+
+static void active_established_stub(struct sock *sk, struct sk_buff *skb__nullable)
+{
+}
+
+static void passive_established_stub(struct sock *sk, struct sk_buff *skb)
+{
+}
+
+static void rto_stub(struct sock *sk)
+{
+}
+
+static void rtt_stub(struct sock *sk, long mrtt, u32 srtt)
+{
+}
+
+static void set_state_stub(struct sock *sk, int state)
+{
+}
+
+static void retrans_stub(struct sock *sk, struct sk_buff *skb, int err)
+{
+}
+
+static void connect_stub(struct sock *sk)
+{
+}
+
+static void listen_stub(struct sock *sk)
+{
+}
+
+static void parse_hdr_stub(struct sock *sk, struct sk_buff *skb)
+{
+}
+
+static void hdr_opt_len_stub(struct sock *sk, struct sk_buff *skb__nullable,
+ struct request_sock *req__nullable,
+ struct sk_buff *syn_skb__nullable,
+ enum tcp_synack_type synack_type,
+ unsigned int *remaining)
+{
+}
+
+static void write_hdr_opt_stub(struct sock *sk, struct sk_buff *skb,
+ struct request_sock *req__nullable,
+ struct sk_buff *syn_skb__nullable,
+ enum tcp_synack_type synack_type,
+ u32 opt_off)
+{
+}
+
+static struct bpf_tcp_ops __bpf_tcp_ops = {
+ .timeout_init = timeout_init_stub,
+ .rwnd_init = rwnd_init_stub,
+ .active_established = active_established_stub,
+ .passive_established = passive_established_stub,
+ .rto = rto_stub,
+ .rtt = rtt_stub,
+ .set_state = set_state_stub,
+ .retrans = retrans_stub,
+ .connect = connect_stub,
+ .listen = listen_stub,
+ .parse_hdr = parse_hdr_stub,
+ .hdr_opt_len = hdr_opt_len_stub,
+ .write_hdr_opt = write_hdr_opt_stub,
+};
+
+BPF_CALL_4(bpf_tcp_ops_store_hdr_opt, void *, ctx, const void *, from,
+ u32, len, u64, flags)
+{
+ struct sk_buff *skb = ((struct sk_buff **)ctx)[1];
+ struct bpf_sock_ops_kern sock_ops = {};
+ u32 opt_off = ((u64 *)ctx)[5];
+ u8 *op, *opend;
+
+ /* bpf_tcp_ops does not keep track of the end of the written TCP header
+ * options, so search for it every time the helper is called. The free
+ * space is NOP-filled, so a TCPOPT_NOP ends the search rather than being
+ * skipped as in a normal option walk in sockops.
+ */
+ op = skb->data + opt_off;
+ opend = skb->data + tcp_hdrlen(skb);
+ while (op < opend && *op != TCPOPT_NOP) {
+ if (*op == TCPOPT_EOL || op + 1 >= opend || op[1] < 2)
+ break;
+ op += op[1];
+ }
+
+ sock_ops.skb = skb;
+ sock_ops.skb_data_end = op;
+ sock_ops.remaining_opt_len = opend - op;
+
+ return __bpf_sock_ops_store_hdr_opt(&sock_ops, from, len, flags);
+}
+
+static const struct bpf_func_proto bpf_tcp_ops_store_hdr_opt_proto = {
+ .func = bpf_tcp_ops_store_hdr_opt,
+ .gpl_only = false,
+ .ret_type = RET_INTEGER,
+ .arg1_type = ARG_PTR_TO_CTX,
+ .arg2_type = ARG_PTR_TO_MEM | MEM_RDONLY,
+ .arg3_type = ARG_CONST_SIZE,
+ .arg4_type = ARG_ANYTHING,
+};
+
+BPF_CALL_4(bpf_tcp_ops_load_hdr_opt, void *, ctx, void *, search_res,
+ u32, len, u64, flags)
+{
+ struct sk_buff *skb = ((struct sk_buff **)ctx)[1];
+ struct bpf_sock_ops_kern sock_ops = {};
+
+ /* No flags supported. In particular BPF_LOAD_HDR_OPT_TCP_SYN, which
+ * loads from the saved SYN, is not available because bpf_tcp_ops has no
+ * carrier to track the SYN source across the hooks.
+ */
+ if (flags)
+ return -EINVAL;
+
+ sock_ops.skb = skb;
+ sock_ops.skb_data_end = skb->data + tcp_hdrlen(skb);
+
+ return __bpf_sock_ops_load_hdr_opt(&sock_ops, search_res, len, flags);
+}
+
+static const struct bpf_func_proto bpf_tcp_ops_load_hdr_opt_proto = {
+ .func = bpf_tcp_ops_load_hdr_opt,
+ .gpl_only = false,
+ .ret_type = RET_INTEGER,
+ .arg1_type = ARG_PTR_TO_CTX,
+ .arg2_type = ARG_PTR_TO_MEM | MEM_WRITE,
+ .arg3_type = ARG_CONST_SIZE,
+ .arg4_type = ARG_ANYTHING,
+};
+
+BPF_CALL_3(bpf_tcp_ops_reserve_hdr_opt, void *, ctx, u32, len, u64, flags)
+{
+ unsigned int *remaining = ((unsigned int **)ctx)[5];
+
+ if (flags || len < 2)
+ return -EINVAL;
+
+ if (len > *remaining)
+ return -ENOSPC;
+
+ *remaining -= len;
+ return 0;
+}
+
+static const struct bpf_func_proto bpf_tcp_ops_reserve_hdr_opt_proto = {
+ .func = bpf_tcp_ops_reserve_hdr_opt,
+ .gpl_only = false,
+ .ret_type = RET_INTEGER,
+ .arg1_type = ARG_PTR_TO_CTX,
+ .arg2_type = ARG_ANYTHING,
+ .arg3_type = ARG_ANYTHING,
+};
+
+BPF_CALL_0(bpf_tcp_ops_get_retval)
+{
+ struct bpf_tramp_run_ctx *ctx =
+ container_of(current->bpf_ctx, struct bpf_tramp_run_ctx, run_ctx);
+
+ /* bpf_get_retval() is only exposed to timeout_init/rwnd_init, which
+ * always run via bpf_tcp_ops_call_int(). Its run_ctx carries the int
+ * return value chained across the bpf_tcp_ops attached to the cgroup
+ * and is this program's saved_run_ctx.
+ */
+ if (WARN_ON_ONCE(!ctx->saved_run_ctx))
+ return 0;
+
+ return container_of(ctx->saved_run_ctx, struct bpf_tramp_run_ctx,
+ run_ctx)->retval;
+}
+
+const struct bpf_func_proto bpf_tcp_ops_get_retval_proto = {
+ .func = bpf_tcp_ops_get_retval,
+ .gpl_only = false,
+ .ret_type = RET_INTEGER,
+};
+
+static const struct bpf_func_proto *
+get_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
+{
+ u32 moff = prog->aux->attach_st_ops_member_off;
+
+ switch (func_id) {
+ case BPF_FUNC_sk_storage_get:
+ return &bpf_sk_storage_get_proto;
+ case BPF_FUNC_sk_storage_delete:
+ return &bpf_sk_storage_delete_proto;
+ case BPF_FUNC_setsockopt:
+ /* The sk may be an unlocked listener (synack path) or NULL
+ * fullsock; disable for members that can run unlocked.
+ */
+ if (moff == offsetof(struct bpf_tcp_ops, rwnd_init) ||
+ moff == offsetof(struct bpf_tcp_ops, timeout_init) ||
+ moff == offsetof(struct bpf_tcp_ops, hdr_opt_len) ||
+ moff == offsetof(struct bpf_tcp_ops, write_hdr_opt))
+ return NULL;
+ return &bpf_sk_setsockopt_proto;
+ case BPF_FUNC_getsockopt:
+ if (moff == offsetof(struct bpf_tcp_ops, rwnd_init) ||
+ moff == offsetof(struct bpf_tcp_ops, timeout_init) ||
+ moff == offsetof(struct bpf_tcp_ops, hdr_opt_len) ||
+ moff == offsetof(struct bpf_tcp_ops, write_hdr_opt))
+ return NULL;
+ return &bpf_sk_getsockopt_proto;
+ case BPF_FUNC_get_retval:
+ if (moff == offsetof(struct bpf_tcp_ops, timeout_init) ||
+ moff == offsetof(struct bpf_tcp_ops, rwnd_init))
+ return &bpf_tcp_ops_get_retval_proto;
+ return NULL;
+ case BPF_FUNC_reserve_hdr_opt:
+ if (moff == offsetof(struct bpf_tcp_ops, hdr_opt_len))
+ return &bpf_tcp_ops_reserve_hdr_opt_proto;
+ return NULL;
+ case BPF_FUNC_load_hdr_opt:
+ if (moff == offsetof(struct bpf_tcp_ops, parse_hdr) ||
+ moff == offsetof(struct bpf_tcp_ops, write_hdr_opt))
+ return &bpf_tcp_ops_load_hdr_opt_proto;
+ return NULL;
+ case BPF_FUNC_store_hdr_opt:
+ if (moff == offsetof(struct bpf_tcp_ops, write_hdr_opt))
+ return &bpf_tcp_ops_store_hdr_opt_proto;
+ return NULL;
+ default:
+ return bpf_base_func_proto(func_id, prog);
+ }
+}
+
+static bool is_valid_access(int off, int size, enum bpf_access_type type,
+ const struct bpf_prog *prog, struct bpf_insn_access_aux *info)
+{
+ if (!bpf_tracing_btf_ctx_access(off, size, type, prog, info))
+ return false;
+
+ if (base_type(info->reg_type) == PTR_TO_BTF_ID &&
+ !bpf_type_has_unsafe_modifiers(info->reg_type) &&
+ info->btf_id == btf_sock_ids[BTF_SOCK_TYPE_SOCK])
+ /* promote it to tcp_sock */
+ info->btf_id = btf_sock_ids[BTF_SOCK_TYPE_TCP];
+
+ return true;
+}
+
+static int bpf_tcp_ops_init_member(const struct btf_type *t,
+ const struct btf_member *member,
+ void *kdata, const void *udata)
+{
+ return 0;
+}
+
+static int bpf_tcp_ops_init(struct btf *btf)
+{
+ return 0;
+}
+
+static int bpf_tcp_ops_validate(void *kdata)
+{
+ return 0;
+}
+
+static const struct bpf_verifier_ops bpf_tcp_ops_verifier = {
+ .get_func_proto = get_func_proto,
+ .is_valid_access = is_valid_access,
+};
+
+static struct bpf_struct_ops bpf_tcp_ops = {
+ .verifier_ops = &bpf_tcp_ops_verifier,
+ .init_member = bpf_tcp_ops_init_member,
+ .init = bpf_tcp_ops_init,
+ .validate = bpf_tcp_ops_validate,
+ .name = "bpf_tcp_ops",
+ .cgroup_atype = CGROUP_TCP_SOCK_OPS,
+ .cfi_stubs = &__bpf_tcp_ops,
+ .owner = THIS_MODULE,
+};
+
+static int __init __bpf_tcp_ops_init(void)
+{
+ return register_bpf_struct_ops(&bpf_tcp_ops, bpf_tcp_ops);
+}
+late_initcall(__bpf_tcp_ops_init);
diff --git a/net/ipv4/tcp.c b/net/ipv4/tcp.c
index 455441f1b69490..94ed1ac2abc1bc 100644
--- a/net/ipv4/tcp.c
+++ b/net/ipv4/tcp.c
@@ -2996,6 +2996,7 @@ void tcp_set_state(struct sock *sk, int state)
if (BPF_SOCK_OPS_TEST_FLAG(tcp_sk(sk), BPF_SOCK_OPS_STATE_CB_FLAG))
tcp_call_bpf_2arg(sk, BPF_SOCK_OPS_STATE_CB, oldstate, state);
+ bpf_tcp_ops_call(set_state, sk, state);
switch (state) {
case TCP_ESTABLISHED:
diff --git a/net/ipv4/tcp_input.c b/net/ipv4/tcp_input.c
index 61045a8886e485..a3614678913810 100644
--- a/net/ipv4/tcp_input.c
+++ b/net/ipv4/tcp_input.c
@@ -208,6 +208,18 @@ static void bpf_skops_established(struct sock *sk, int bpf_op,
}
#endif
+static void bpf_tcp_ops_parse_hdr(struct sock *sk, struct sk_buff *skb)
+{
+ switch (sk->sk_state) {
+ case TCP_SYN_RECV:
+ case TCP_SYN_SENT:
+ case TCP_LISTEN:
+ return;
+ }
+
+ bpf_tcp_ops_call(parse_hdr, sk, skb);
+}
+
static __cold void tcp_gro_dev_warn(const struct sock *sk, const struct sk_buff *skb,
unsigned int len)
{
@@ -6431,6 +6443,7 @@ static bool tcp_validate_incoming(struct sock *sk, struct sk_buff *skb,
pass:
bpf_skops_parse_hdr(sk, skb);
+ bpf_tcp_ops_parse_hdr(sk, skb);
return true;
@@ -6694,6 +6707,10 @@ void tcp_init_transfer(struct sock *sk, int bpf_op, struct sk_buff *skb)
tp->snd_cwnd_stamp = tcp_jiffies32;
bpf_skops_established(sk, bpf_op, skb);
+ if (bpf_op == BPF_SOCK_OPS_ACTIVE_ESTABLISHED_CB)
+ bpf_tcp_ops_call(active_established, sk, skb);
+ else
+ bpf_tcp_ops_call(passive_established, sk, skb);
/* Initialize congestion control unless BPF initialized it already: */
if (!icsk->icsk_ca_initialized)
tcp_init_congestion_control(sk);
diff --git a/net/ipv4/tcp_output.c b/net/ipv4/tcp_output.c
index 26dd751ec72abf..580652d0a135d1 100644
--- a/net/ipv4/tcp_output.c
+++ b/net/ipv4/tcp_output.c
@@ -573,6 +573,13 @@ static void bpf_skops_write_hdr_opt(struct sock *sk, struct sk_buff *skb,
if (nr_written < max_opt_len)
memset(skb->data + first_opt_off + nr_written, TCPOPT_NOP,
max_opt_len - nr_written);
+
+ /* bpf_tcp_ops portion is NOP-filled (everything past the sockops
+ * writer's bytes). The writer find the append point by scanning from
+ * first_opt_off + nr_written to the first NOP.
+ */
+ bpf_tcp_ops_call(write_hdr_opt, sk, skb, req, syn_skb, synack_type,
+ first_opt_off + nr_written);
}
#else
static u32 bpf_skops_hdr_opt_len(struct sock *sk, struct sk_buff *skb,
@@ -594,6 +601,32 @@ static void bpf_skops_write_hdr_opt(struct sock *sk, struct sk_buff *skb,
}
#endif
+static u32 bpf_tcp_ops_hdr_opt_len(struct sock *sk, struct sk_buff *skb,
+ struct request_sock *req,
+ struct sk_buff *syn_skb,
+ enum tcp_synack_type synack_type,
+ struct tcp_out_options *opts,
+ u32 remaining)
+{
+ unsigned int remaining_out = remaining, reserved;
+
+ if (!remaining)
+ return 0;
+
+ /* bpf_tcp_ops_reserve_hdr_opt() reserves space via remaining_out */
+ bpf_tcp_ops_call(hdr_opt_len, sk, skb, req, syn_skb, synack_type, &remaining_out);
+
+ reserved = remaining - remaining_out;
+ if (!reserved)
+ return remaining;
+
+ /* round up to 4 bytes */
+ reserved = (reserved + 3) & ~3;
+
+ opts->bpf_opt_len += reserved;
+ return remaining - reserved;
+}
+
static __be32 *process_tcp_ao_options(struct tcp_sock *tp,
const struct tcp_request_sock *tcprsk,
struct tcp_out_options *opts,
@@ -1053,6 +1086,8 @@ static unsigned int tcp_syn_options(struct sock *sk, struct sk_buff *skb,
remaining = bpf_skops_hdr_opt_len(sk, skb, NULL, NULL, 0, opts,
remaining);
+ remaining = bpf_tcp_ops_hdr_opt_len(sk, skb, NULL, NULL, 0, opts,
+ remaining);
return MAX_TCP_OPTION_SPACE - remaining;
}
@@ -1141,6 +1176,8 @@ static unsigned int tcp_synack_options(const struct sock *sk,
remaining = bpf_skops_hdr_opt_len((struct sock *)sk, skb, req, syn_skb,
synack_type, opts, remaining);
+ remaining = bpf_tcp_ops_hdr_opt_len((struct sock *)sk, skb, req, syn_skb,
+ synack_type, opts, remaining);
return MAX_TCP_OPTION_SPACE - remaining;
}
@@ -1244,6 +1281,15 @@ static unsigned int tcp_established_options(struct sock *sk, struct sk_buff *skb
size = MAX_TCP_OPTION_SPACE - remaining;
}
+ if (cgroup_bpf_enabled(CGROUP_TCP_SOCK_OPS)) {
+ unsigned int remaining = MAX_TCP_OPTION_SPACE - size;
+
+ remaining = bpf_tcp_ops_hdr_opt_len(sk, skb, NULL, NULL, 0, opts,
+ remaining);
+
+ size = MAX_TCP_OPTION_SPACE - remaining;
+ }
+
return size;
}
@@ -3678,6 +3724,7 @@ int __tcp_retransmit_skb(struct sock *sk, struct sk_buff *skb, int segs)
if (BPF_SOCK_OPS_TEST_FLAG(tp, BPF_SOCK_OPS_RETRANS_CB_FLAG))
tcp_call_bpf_3arg(sk, BPF_SOCK_OPS_RETRANS_CB,
TCP_SKB_CB(skb)->seq, segs, err);
+ bpf_tcp_ops_call(retrans, sk, skb, err);
if (unlikely(err) && err != -EBUSY)
NET_ADD_STATS(sock_net(sk), LINUX_MIB_TCPRETRANSFAIL, segs);
@@ -4298,6 +4345,7 @@ int tcp_connect(struct sock *sk)
int err;
tcp_call_bpf(sk, BPF_SOCK_OPS_TCP_CONNECT_CB, 0, NULL);
+ bpf_tcp_ops_call(connect, sk);
#if defined(CONFIG_TCP_MD5SIG) && defined(CONFIG_TCP_AO)
/* Has to be checked late, after setting daddr/saddr/ops.
diff --git a/net/ipv4/tcp_timer.c b/net/ipv4/tcp_timer.c
index bf171b5e1eb306..4337627ee0ea25 100644
--- a/net/ipv4/tcp_timer.c
+++ b/net/ipv4/tcp_timer.c
@@ -290,6 +290,7 @@ static int tcp_write_timeout(struct sock *sk)
tcp_call_bpf_3arg(sk, BPF_SOCK_OPS_RTO_CB,
icsk->icsk_retransmits,
icsk->icsk_rto, (int)expired);
+ bpf_tcp_ops_call(rto, sk);
if (expired) {
/* Has it gone just too far? */
diff --git a/net/sched/bpf_qdisc.c b/net/sched/bpf_qdisc.c
index 098ca02aed89c2..5691c13781a8b0 100644
--- a/net/sched/bpf_qdisc.c
+++ b/net/sched/bpf_qdisc.c
@@ -280,7 +280,6 @@ BTF_KFUNCS_START(qdisc_kfunc_ids)
BTF_ID_FLAGS(func, bpf_skb_get_hash)
BTF_ID_FLAGS(func, bpf_kfree_skb, KF_RELEASE)
BTF_ID_FLAGS(func, bpf_qdisc_skb_drop, KF_RELEASE)
-BTF_ID_FLAGS(func, bpf_dynptr_from_skb)
BTF_ID_FLAGS(func, bpf_qdisc_watchdog_schedule)
BTF_ID_FLAGS(func, bpf_qdisc_init_prologue)
BTF_ID_FLAGS(func, bpf_qdisc_reset_destroy_epilogue)
@@ -290,7 +289,6 @@ BTF_KFUNCS_END(qdisc_kfunc_ids)
BTF_SET_START(qdisc_common_kfunc_set)
BTF_ID(func, bpf_skb_get_hash)
BTF_ID(func, bpf_kfree_skb)
-BTF_ID(func, bpf_dynptr_from_skb)
BTF_SET_END(qdisc_common_kfunc_set)
BTF_SET_START(qdisc_enqueue_kfunc_set)
diff --git a/tools/include/uapi/linux/bpf.h b/tools/include/uapi/linux/bpf.h
index 89b36de5fdbb6d..45b9ee29e46174 100644
--- a/tools/include/uapi/linux/bpf.h
+++ b/tools/include/uapi/linux/bpf.h
@@ -1756,7 +1756,7 @@ union bpf_attr {
__u32 prog_cnt;
__u32 count;
};
- __u32 :32;
+ __u32 type_id;
/* output: per-program attach_flags.
* not allowed to be set during effective query.
*/
@@ -4799,15 +4799,18 @@ union bpf_attr {
* The non-negative copied *buf* length equal to or less than
* *size* on success, or a negative error in case of failure.
*
- * long bpf_load_hdr_opt(struct bpf_sock_ops *skops, void *searchby_res, u32 len, u64 flags)
+ * long bpf_load_hdr_opt(void *ctx, void *searchby_res, u32 len, u64 flags)
* Description
* Load header option. Support reading a particular TCP header
- * option for bpf program (**BPF_PROG_TYPE_SOCK_OPS**).
+ * option for bpf program (**BPF_PROG_TYPE_SOCK_OPS**). For the
+ * **bpf_tcp_ops** struct_ops, this helper can be called from the
+ * **parse_hdr**\ () and **write_hdr_opt**\ () operators.
*
- * If *flags* is 0, it will search the option from the
- * *skops*\ **->skb_data**. The comment in **struct bpf_sock_ops**
- * has details on what skb_data contains under different
- * *skops*\ **->op**.
+ * If *flags* is 0, it will search the option from the packet
+ * associated with the current operation. For
+ * **BPF_PROG_TYPE_SOCK_OPS**, the comment in
+ * **struct bpf_sock_ops** has details on what skb_data
+ * contains under different *op*.
*
* The first byte of the *searchby_res* specifies the
* kind that it wants to search.
@@ -4840,6 +4843,8 @@ union bpf_attr {
*
* * **BPF_LOAD_HDR_OPT_TCP_SYN** to search from the
* saved_syn packet or the just-received syn packet.
+ * Not supported by the **bpf_tcp_ops** struct_ops, which
+ * rejects all flags.
*
* Return
* > 0 when found, the header option is copied to *searchby_res*.
@@ -4860,9 +4865,9 @@ union bpf_attr {
* packet.
*
* **-EPERM** if the helper cannot be used under the current
- * *skops*\ **->op**.
+ * operation.
*
- * long bpf_store_hdr_opt(struct bpf_sock_ops *skops, const void *from, u32 len, u64 flags)
+ * long bpf_store_hdr_opt(void *ctx, const void *from, u32 len, u64 flags)
* Description
* Store header option. The data will be copied
* from buffer *from* with length *len* to the TCP header.
@@ -4878,7 +4883,9 @@ union bpf_attr {
* by searching the same option in the outgoing skb.
*
* This helper can only be called during
- * **BPF_SOCK_OPS_WRITE_HDR_OPT_CB**.
+ * **BPF_SOCK_OPS_WRITE_HDR_OPT_CB**, or from the
+ * **write_hdr_opt**\ () operator of the **bpf_tcp_ops**
+ * struct_ops.
*
* Return
* 0 on success, or negative error in case of failure:
@@ -4893,9 +4900,9 @@ union bpf_attr {
* **-EFAULT** on failure to parse the existing header options.
*
* **-EPERM** if the helper cannot be used under the current
- * *skops*\ **->op**.
+ * operation.
*
- * long bpf_reserve_hdr_opt(struct bpf_sock_ops *skops, u32 len, u64 flags)
+ * long bpf_reserve_hdr_opt(void *ctx, u32 len, u64 flags)
* Description
* Reserve *len* bytes for the bpf header option. The
* space will be used by **bpf_store_hdr_opt**\ () later in
@@ -4905,7 +4912,9 @@ union bpf_attr {
* the total number of bytes will be reserved.
*
* This helper can only be called during
- * **BPF_SOCK_OPS_HDR_OPT_LEN_CB**.
+ * **BPF_SOCK_OPS_HDR_OPT_LEN_CB**, or from the
+ * **hdr_opt_len**\ () operator of the **bpf_tcp_ops**
+ * struct_ops.
*
* Return
* 0 on success, or negative error in case of failure:
@@ -4915,7 +4924,7 @@ union bpf_attr {
* **-ENOSPC** if there is not enough space in the header.
*
* **-EPERM** if the helper cannot be used under the current
- * *skops*\ **->op**.
+ * operation.
*
* void *bpf_inode_storage_get(struct bpf_map *map, void *inode, void *value, u64 flags)
* Description
@@ -6815,6 +6824,8 @@ struct bpf_link_info {
} xdp;
struct {
__u32 map_id;
+ __u32 :32;
+ __u64 cgroup_id;
} struct_ops;
struct {
__u32 pf;
diff --git a/tools/lib/bpf/bpf.c b/tools/lib/bpf/bpf.c
index 96819c082c772d..a9de7f107cf7bb 100644
--- a/tools/lib/bpf/bpf.c
+++ b/tools/lib/bpf/bpf.c
@@ -934,6 +934,7 @@ int bpf_link_create(int prog_fd, int target_fd,
case BPF_CGROUP_GETSOCKOPT:
case BPF_CGROUP_SETSOCKOPT:
case BPF_LSM_CGROUP:
+ case BPF_STRUCT_OPS:
relative_fd = OPTS_GET(opts, cgroup.relative_fd, 0);
relative_id = OPTS_GET(opts, cgroup.relative_id, 0);
if (relative_fd && relative_id)
@@ -1056,6 +1057,7 @@ int bpf_prog_query_opts(int target, enum bpf_attach_type type,
attr.query.attach_type = type;
attr.query.query_flags = OPTS_GET(opts, query_flags, 0);
attr.query.count = OPTS_GET(opts, count, 0);
+ attr.query.type_id = OPTS_GET(opts, type_id, 0);
attr.query.prog_ids = ptr_to_u64(OPTS_GET(opts, prog_ids, NULL));
attr.query.link_ids = ptr_to_u64(OPTS_GET(opts, link_ids, NULL));
attr.query.prog_attach_flags = ptr_to_u64(OPTS_GET(opts, prog_attach_flags, NULL));
diff --git a/tools/lib/bpf/bpf.h b/tools/lib/bpf/bpf.h
index 7534a593edae21..490e8cb4ba537d 100644
--- a/tools/lib/bpf/bpf.h
+++ b/tools/lib/bpf/bpf.h
@@ -637,9 +637,10 @@ struct bpf_prog_query_opts {
__u32 *link_ids;
__u32 *link_attach_flags;
__u64 revision;
+ __u32 type_id;
size_t :0;
};
-#define bpf_prog_query_opts__last_field revision
+#define bpf_prog_query_opts__last_field type_id
/**
* @brief **bpf_prog_query_opts()** queries the BPF programs and BPF links
diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c
index 1368752aa13c39..17f8466e33fa37 100644
--- a/tools/lib/bpf/libbpf.c
+++ b/tools/lib/bpf/libbpf.c
@@ -14104,6 +14104,65 @@ struct bpf_link *bpf_map__attach_struct_ops(const struct bpf_map *map)
return &link->link;
}
+struct bpf_link *bpf_map__attach_cgroup_opts(const struct bpf_map *map, int cgroup_fd,
+ const struct bpf_cgroup_opts *opts)
+{
+ LIBBPF_OPTS(bpf_link_create_opts, link_create_opts);
+ struct bpf_link_struct_ops *link;
+ __u32 relative_id, zero = 0;
+ int err, fd, relative_fd;
+
+ if (!OPTS_VALID(opts, bpf_cgroup_opts))
+ return libbpf_err_ptr(-EINVAL);
+
+ if (!bpf_map__is_struct_ops(map)) {
+ pr_warn("map '%s': can't attach non-struct_ops map\n", map->name);
+ return libbpf_err_ptr(-EINVAL);
+ }
+
+ if (map->fd < 0) {
+ pr_warn("map '%s': can't attach BPF map without FD (was it created?)\n", map->name);
+ return libbpf_err_ptr(-EINVAL);
+ }
+
+ relative_id = OPTS_GET(opts, relative_id, 0);
+ relative_fd = OPTS_GET(opts, relative_fd, 0);
+
+ if (relative_fd && relative_id) {
+ pr_warn("map '%s': relative_fd and relative_id cannot be set at the same time\n",
+ map->name);
+ return libbpf_err_ptr(-EINVAL);
+ }
+
+ link_create_opts.cgroup.expected_revision = OPTS_GET(opts, expected_revision, 0);
+ link_create_opts.cgroup.relative_fd = relative_fd;
+ link_create_opts.cgroup.relative_id = relative_id;
+ link_create_opts.flags = OPTS_GET(opts, flags, 0);
+
+ link = calloc(1, sizeof(*link));
+ if (!link)
+ return libbpf_err_ptr(-ENOMEM);
+
+ err = bpf_map_update_elem(map->fd, &zero, map->st_ops->kern_vdata, 0);
+ if (err && err != -EBUSY) {
+ free(link);
+ return libbpf_err_ptr(err);
+ }
+
+ link->link.detach = bpf_link__detach_struct_ops;
+
+ fd = bpf_link_create(map->fd, cgroup_fd, BPF_STRUCT_OPS, &link_create_opts);
+ if (fd < 0) {
+ free(link);
+ return libbpf_err_ptr(fd);
+ }
+
+ link->link.fd = fd;
+ link->map_fd = map->fd;
+
+ return &link->link;
+}
+
/*
* Swap the back struct_ops of a link with a new struct_ops map.
*/
diff --git a/tools/lib/bpf/libbpf.h b/tools/lib/bpf/libbpf.h
index b965ad57154036..0e5f4e9bba41aa 100644
--- a/tools/lib/bpf/libbpf.h
+++ b/tools/lib/bpf/libbpf.h
@@ -960,6 +960,9 @@ bpf_program__attach_cgroup_opts(const struct bpf_program *prog, int cgroup_fd,
struct bpf_map;
LIBBPF_API struct bpf_link *bpf_map__attach_struct_ops(const struct bpf_map *map);
+LIBBPF_API struct bpf_link *bpf_map__attach_cgroup_opts(const struct bpf_map *map,
+ int cgroup_fd,
+ const struct bpf_cgroup_opts *opts);
LIBBPF_API int bpf_link__update_map(struct bpf_link *link, const struct bpf_map *map);
struct bpf_iter_attach_opts {
diff --git a/tools/lib/bpf/libbpf.map b/tools/lib/bpf/libbpf.map
index b731df19ae6926..1b01d49e58eb13 100644
--- a/tools/lib/bpf/libbpf.map
+++ b/tools/lib/bpf/libbpf.map
@@ -462,3 +462,8 @@ LIBBPF_1.8.0 {
bpf_program__clone;
btf__new_empty_opts;
} LIBBPF_1.7.0;
+
+LIBBPF_1.9.0 {
+ global:
+ bpf_map__attach_cgroup_opts;
+} LIBBPF_1.8.0;
diff --git a/tools/lib/bpf/libbpf_version.h b/tools/lib/bpf/libbpf_version.h
index c446c0cd8cf92e..57b74ef3618c10 100644
--- a/tools/lib/bpf/libbpf_version.h
+++ b/tools/lib/bpf/libbpf_version.h
@@ -4,6 +4,6 @@
#define __LIBBPF_VERSION_H
#define LIBBPF_MAJOR_VERSION 1
-#define LIBBPF_MINOR_VERSION 8
+#define LIBBPF_MINOR_VERSION 9
#endif /* __LIBBPF_VERSION_H */
diff --git a/tools/testing/selftests/bpf/prog_tests/bpf_tcp_ops.c b/tools/testing/selftests/bpf/prog_tests/bpf_tcp_ops.c
new file mode 100644
index 00000000000000..4d087bdc46135e
--- /dev/null
+++ b/tools/testing/selftests/bpf/prog_tests/bpf_tcp_ops.c
@@ -0,0 +1,554 @@
+// SPDX-License-Identifier: GPL-2.0
+/* Copyright (c) 2026 Meta Platforms, Inc. and affiliates. */
+
+#include <test_progs.h>
+#include <network_helpers.h>
+#include <bpf/btf.h>
+#include "cgroup_helpers.h"
+#include "bpf_tcp_ops.skel.h"
+
+#define CGROUP_PATH "/bpf_tcp_ops"
+#define TEST_NETNS "bpf_tcp_ops"
+
+static __s32 get_bpf_tcp_ops_type_id(void)
+{
+ struct btf *vmlinux_btf;
+ __s32 type_id;
+
+ vmlinux_btf = btf__load_vmlinux_btf();
+ if (!ASSERT_OK_PTR(vmlinux_btf, "load_vmlinux_btf"))
+ return -1;
+
+ type_id = btf__find_by_name_kind(vmlinux_btf, "bpf_tcp_ops", BTF_KIND_STRUCT);
+ btf__free(vmlinux_btf);
+
+ ASSERT_GT(type_id, 0, "find_bpf_tcp_ops");
+ return type_id;
+}
+
+static void reset_order(struct bpf_tcp_ops *skel)
+{
+ memset(skel->bss->listen_order, 0, sizeof(skel->bss->listen_order));
+ memset(skel->bss->connect_order, 0, sizeof(skel->bss->connect_order));
+ skel->bss->listen_cnt = 0;
+ skel->bss->connect_cnt = 0;
+}
+
+static void do_listen_connect(int family)
+{
+ const char *addr = family == AF_INET ? "127.0.0.1" : "::1";
+ int server_fd, client_fd;
+
+ server_fd = start_server(family, SOCK_STREAM, addr, 0, 0);
+ if (!ASSERT_GE(server_fd, 0, "start_server"))
+ return;
+
+ client_fd = connect_to_fd(server_fd, 0);
+ if (ASSERT_OK_FD(client_fd, "connect_to_fd"))
+ close(client_fd);
+
+ close(server_fd);
+}
+
+/*
+ * Attach ops1 and ops2 normally (in that order), then ops3 with
+ * BPF_F_PREORDER. Expected execution order: [3, 1, 2] — ops3 runs
+ * first despite being attached last, ops1 before ops2 by attach order.
+ */
+static void test_order(int cgroup_fd, struct bpf_tcp_ops *skel, int family)
+{
+ LIBBPF_OPTS(bpf_cgroup_opts, preorder_opts, .flags = BPF_F_PREORDER);
+ struct bpf_link *link1 = NULL, *link2 = NULL, *link3 = NULL;
+
+ link1 = bpf_map__attach_cgroup_opts(skel->maps.tcp_ops1, cgroup_fd, NULL);
+ if (!ASSERT_OK_PTR(link1, "attach_ops1"))
+ goto done;
+
+ link2 = bpf_map__attach_cgroup_opts(skel->maps.tcp_ops2, cgroup_fd, NULL);
+ if (!ASSERT_OK_PTR(link2, "attach_ops2"))
+ goto done;
+
+ link3 = bpf_map__attach_cgroup_opts(skel->maps.tcp_ops3, cgroup_fd,
+ &preorder_opts);
+ if (!ASSERT_OK_PTR(link3, "attach_ops3_preorder"))
+ goto done;
+
+ reset_order(skel);
+ do_listen_connect(family);
+
+ ASSERT_EQ(skel->bss->listen_cnt, 3, "listen_cnt");
+ ASSERT_EQ(skel->bss->listen_order[0], 3, "listen_order[0]");
+ ASSERT_EQ(skel->bss->listen_order[1], 1, "listen_order[1]");
+ ASSERT_EQ(skel->bss->listen_order[2], 2, "listen_order[2]");
+
+ ASSERT_EQ(skel->bss->connect_cnt, 3, "connect_cnt");
+ ASSERT_EQ(skel->bss->connect_order[0], 3, "connect_order[0]");
+ ASSERT_EQ(skel->bss->connect_order[1], 1, "connect_order[1]");
+ ASSERT_EQ(skel->bss->connect_order[2], 2, "connect_order[2]");
+
+done:
+ bpf_link__destroy(link3);
+ bpf_link__destroy(link2);
+ bpf_link__destroy(link1);
+}
+
+static void run_order_subtest(void)
+{
+ struct bpf_tcp_ops *skel = NULL;
+ struct netns_obj *ns = NULL;
+ int cgroup_fd;
+
+ cgroup_fd = test__join_cgroup(CGROUP_PATH);
+ if (!ASSERT_GE(cgroup_fd, 0, "join_cgroup"))
+ return;
+
+ ns = netns_new(TEST_NETNS, true);
+ if (!ASSERT_OK_PTR(ns, "netns_new"))
+ goto done;
+
+ skel = bpf_tcp_ops__open_and_load();
+ if (!ASSERT_OK_PTR(skel, "open_and_load"))
+ goto done;
+
+ test_order(cgroup_fd, skel, AF_INET);
+ test_order(cgroup_fd, skel, AF_INET6);
+
+done:
+ bpf_tcp_ops__destroy(skel);
+ netns_free(ns);
+ close(cgroup_fd);
+}
+
+/*
+ * Position a new attachment relative to an existing one. Attach ops1, then
+ * ops2 with BPF_F_BEFORE ops1, then ops3 with BPF_F_AFTER ops2. Expected
+ * execution order: [2, 3, 1]. For struct_ops, relative_fd refers to a link
+ * fd, so BPF_F_LINK must be set.
+ */
+static void test_before_after(int cgroup_fd, struct bpf_tcp_ops *skel)
+{
+ struct bpf_link *link1 = NULL, *link2 = NULL, *link3 = NULL;
+ LIBBPF_OPTS(bpf_cgroup_opts, opts);
+
+ link1 = bpf_map__attach_cgroup_opts(skel->maps.tcp_ops1, cgroup_fd, NULL);
+ if (!ASSERT_OK_PTR(link1, "attach_ops1"))
+ goto done;
+
+ opts.flags = BPF_F_BEFORE | BPF_F_LINK;
+ opts.relative_fd = bpf_link__fd(link1);
+ link2 = bpf_map__attach_cgroup_opts(skel->maps.tcp_ops2, cgroup_fd, &opts);
+ if (!ASSERT_OK_PTR(link2, "attach_ops2_before"))
+ goto done;
+
+ opts.flags = BPF_F_AFTER | BPF_F_LINK;
+ opts.relative_fd = bpf_link__fd(link2);
+ link3 = bpf_map__attach_cgroup_opts(skel->maps.tcp_ops3, cgroup_fd, &opts);
+ if (!ASSERT_OK_PTR(link3, "attach_ops3_after"))
+ goto done;
+
+ reset_order(skel);
+ do_listen_connect(AF_INET6);
+
+ ASSERT_EQ(skel->bss->listen_cnt, 3, "listen_cnt");
+ ASSERT_EQ(skel->bss->listen_order[0], 2, "listen_order[0]");
+ ASSERT_EQ(skel->bss->listen_order[1], 3, "listen_order[1]");
+ ASSERT_EQ(skel->bss->listen_order[2], 1, "listen_order[2]");
+
+done:
+ bpf_link__destroy(link3);
+ bpf_link__destroy(link2);
+ bpf_link__destroy(link1);
+}
+
+static void run_before_after_subtest(void)
+{
+ struct bpf_tcp_ops *skel = NULL;
+ struct netns_obj *ns = NULL;
+ int cgroup_fd;
+
+ cgroup_fd = test__join_cgroup(CGROUP_PATH);
+ if (!ASSERT_GE(cgroup_fd, 0, "join_cgroup"))
+ return;
+
+ ns = netns_new(TEST_NETNS, true);
+ if (!ASSERT_OK_PTR(ns, "netns_new"))
+ goto done;
+
+ skel = bpf_tcp_ops__open_and_load();
+ if (!ASSERT_OK_PTR(skel, "open_and_load"))
+ goto done;
+
+ test_before_after(cgroup_fd, skel);
+
+done:
+ bpf_tcp_ops__destroy(skel);
+ netns_free(ns);
+ close(cgroup_fd);
+}
+
+static void test_query(int cgroup_fd, struct bpf_tcp_ops *skel)
+{
+ struct bpf_map_info info = {};
+ __u32 info_len = sizeof(info);
+ LIBBPF_OPTS(bpf_prog_query_opts, query_opts);
+ struct bpf_link *link1 = NULL, *link2 = NULL;
+ __u32 map1_id, map2_id, map_ids[2] = {};
+ __s32 type_id;
+
+ type_id = get_bpf_tcp_ops_type_id();
+ if (type_id <= 0)
+ return;
+
+ bpf_map_get_info_by_fd(bpf_map__fd(skel->maps.tcp_ops1), &info, &info_len);
+ map1_id = info.id;
+
+ bpf_map_get_info_by_fd(bpf_map__fd(skel->maps.tcp_ops2), &info, &info_len);
+ map2_id = info.id;
+
+ link1 = bpf_map__attach_cgroup_opts(skel->maps.tcp_ops1, cgroup_fd, NULL);
+ if (!ASSERT_OK_PTR(link1, "attach_ops1"))
+ goto done;
+
+ link2 = bpf_map__attach_cgroup_opts(skel->maps.tcp_ops2, cgroup_fd, NULL);
+ if (!ASSERT_OK_PTR(link2, "attach_ops2"))
+ goto done;
+
+ /* query effective: expect 2 entries in attachment order */
+ query_opts.type_id = type_id;
+ query_opts.prog_ids = map_ids;
+ query_opts.count = ARRAY_SIZE(map_ids);
+ query_opts.query_flags = BPF_F_QUERY_EFFECTIVE;
+ ASSERT_OK(bpf_prog_query_opts(cgroup_fd, BPF_STRUCT_OPS, &query_opts),
+ "query_effective");
+ ASSERT_EQ(query_opts.count, 2, "query_effective_count");
+ ASSERT_EQ(map_ids[0], map1_id, "map_ids[0]");
+ ASSERT_EQ(map_ids[1], map2_id, "map_ids[1]");
+
+ /* query attached (non-effective): expect 2 entries */
+ memset(map_ids, 0, sizeof(map_ids));
+ query_opts.query_flags = 0;
+ query_opts.count = ARRAY_SIZE(map_ids);
+ ASSERT_OK(bpf_prog_query_opts(cgroup_fd, BPF_STRUCT_OPS, &query_opts),
+ "query_attached");
+ ASSERT_EQ(query_opts.count, 2, "query_attached_count");
+ ASSERT_EQ(map_ids[0], map1_id, "attached_map_ids[0]");
+ ASSERT_EQ(map_ids[1], map2_id, "attached_map_ids[1]");
+
+done:
+ bpf_link__destroy(link2);
+ bpf_link__destroy(link1);
+}
+
+static void run_query_subtest(void)
+{
+ struct bpf_tcp_ops *skel = NULL;
+ struct netns_obj *ns = NULL;
+ int cgroup_fd;
+
+ cgroup_fd = test__join_cgroup(CGROUP_PATH);
+ if (!ASSERT_GE(cgroup_fd, 0, "join_cgroup"))
+ return;
+
+ ns = netns_new(TEST_NETNS, true);
+ if (!ASSERT_OK_PTR(ns, "netns_new"))
+ goto done;
+
+ skel = bpf_tcp_ops__open_and_load();
+ if (!ASSERT_OK_PTR(skel, "open_and_load"))
+ goto done;
+
+ test_query(cgroup_fd, skel);
+
+done:
+ bpf_tcp_ops__destroy(skel);
+ netns_free(ns);
+ close(cgroup_fd);
+}
+
+/* Must match progs/bpf_tcp_ops.c */
+#define OPS_RETVAL1 11
+#define OPS_RETVAL2 22
+
+/*
+ * Attach three struct_ops implementing timeout_init to the same cgroup; they
+ * run in attach order [retval1, retval2, retval3]. timeout_init's return value
+ * is chained: the first prog reads the kernel seed via bpf_get_retval() (0,
+ * since no legacy sockops prog is attached) and returns OPS_RETVAL1; each
+ * subsequent prog must then observe the previous prog's return value. This
+ * proves the trampoline inherits the retval across an array of struct_ops.
+ */
+static void test_retval(int cgroup_fd, struct bpf_tcp_ops *skel)
+{
+ struct bpf_link *link1 = NULL, *link2 = NULL, *link3 = NULL;
+
+ skel->bss->retval_saw1 = -1;
+ skel->bss->retval_saw2 = -1;
+ skel->bss->retval_saw3 = -1;
+
+ link1 = bpf_map__attach_cgroup_opts(skel->maps.tcp_ops_retval1, cgroup_fd, NULL);
+ if (!ASSERT_OK_PTR(link1, "attach_retval1"))
+ goto done;
+
+ link2 = bpf_map__attach_cgroup_opts(skel->maps.tcp_ops_retval2, cgroup_fd, NULL);
+ if (!ASSERT_OK_PTR(link2, "attach_retval2"))
+ goto done;
+
+ link3 = bpf_map__attach_cgroup_opts(skel->maps.tcp_ops_retval3, cgroup_fd, NULL);
+ if (!ASSERT_OK_PTR(link3, "attach_retval3"))
+ goto done;
+
+ do_listen_connect(AF_INET6);
+
+ /* First prog inherits the kernel seed (no legacy sockops -> 0). */
+ ASSERT_EQ(skel->bss->retval_saw1, 0, "retval_saw1");
+ /* Each subsequent prog inherits the previous prog's return value. */
+ ASSERT_EQ(skel->bss->retval_saw2, OPS_RETVAL1, "retval_saw2");
+ ASSERT_EQ(skel->bss->retval_saw3, OPS_RETVAL2, "retval_saw3");
+
+done:
+ bpf_link__destroy(link3);
+ bpf_link__destroy(link2);
+ bpf_link__destroy(link1);
+}
+
+static void run_retval_subtest(void)
+{
+ struct bpf_tcp_ops *skel = NULL;
+ struct netns_obj *ns = NULL;
+ int cgroup_fd;
+
+ cgroup_fd = test__join_cgroup(CGROUP_PATH);
+ if (!ASSERT_GE(cgroup_fd, 0, "join_cgroup"))
+ return;
+
+ ns = netns_new(TEST_NETNS, true);
+ if (!ASSERT_OK_PTR(ns, "netns_new"))
+ goto done;
+
+ skel = bpf_tcp_ops__open_and_load();
+ if (!ASSERT_OK_PTR(skel, "open_and_load"))
+ goto done;
+
+ test_retval(cgroup_fd, skel);
+
+done:
+ bpf_tcp_ops__destroy(skel);
+ netns_free(ns);
+ close(cgroup_fd);
+}
+
+/*
+ * bpf_link__update_map() swaps the struct_ops map backing an attached link.
+ * The link keeps its position, including BPF_F_PREORDER, across the update.
+ * Attach ops1 (normal) and ops2 (preorder): order [2, 1]. Update the normal
+ * link to ops3 -> [2, 3]; update the preorder link to ops1 -> [1, 3].
+ */
+static void test_update(int cgroup_fd, struct bpf_tcp_ops *skel)
+{
+ LIBBPF_OPTS(bpf_cgroup_opts, preorder_opts, .flags = BPF_F_PREORDER);
+ struct bpf_link *link = NULL, *link_pre = NULL;
+
+ link = bpf_map__attach_cgroup_opts(skel->maps.tcp_ops1, cgroup_fd, NULL);
+ if (!ASSERT_OK_PTR(link, "attach_ops1"))
+ goto done;
+
+ link_pre = bpf_map__attach_cgroup_opts(skel->maps.tcp_ops2, cgroup_fd,
+ &preorder_opts);
+ if (!ASSERT_OK_PTR(link_pre, "attach_ops2_preorder"))
+ goto done;
+
+ reset_order(skel);
+ do_listen_connect(AF_INET6);
+ ASSERT_EQ(skel->bss->listen_cnt, 2, "cnt_initial");
+ ASSERT_EQ(skel->bss->listen_order[0], 2, "order0_initial");
+ ASSERT_EQ(skel->bss->listen_order[1], 1, "order1_initial");
+
+ /* Update the normal link's map (ops1 -> ops3); position is unchanged. */
+ if (!ASSERT_OK(bpf_link__update_map(link, skel->maps.tcp_ops3), "update_normal"))
+ goto done;
+
+ reset_order(skel);
+ do_listen_connect(AF_INET6);
+ ASSERT_EQ(skel->bss->listen_order[0], 2, "order0_after_normal");
+ ASSERT_EQ(skel->bss->listen_order[1], 3, "order1_after_normal");
+
+ /* Update the preorder link's map (ops2 -> ops1); it stays first. */
+ if (!ASSERT_OK(bpf_link__update_map(link_pre, skel->maps.tcp_ops1), "update_preorder"))
+ goto done;
+
+ reset_order(skel);
+ do_listen_connect(AF_INET6);
+ ASSERT_EQ(skel->bss->listen_order[0], 1, "order0_after_preorder");
+ ASSERT_EQ(skel->bss->listen_order[1], 3, "order1_after_preorder");
+
+done:
+ bpf_link__destroy(link_pre);
+ bpf_link__destroy(link);
+}
+
+static void run_update_subtest(void)
+{
+ struct bpf_tcp_ops *skel = NULL;
+ struct netns_obj *ns = NULL;
+ int cgroup_fd;
+
+ cgroup_fd = test__join_cgroup(CGROUP_PATH);
+ if (!ASSERT_GE(cgroup_fd, 0, "join_cgroup"))
+ return;
+
+ ns = netns_new(TEST_NETNS, true);
+ if (!ASSERT_OK_PTR(ns, "netns_new"))
+ goto done;
+
+ skel = bpf_tcp_ops__open_and_load();
+ if (!ASSERT_OK_PTR(skel, "open_and_load"))
+ goto done;
+
+ test_update(cgroup_fd, skel);
+
+done:
+ bpf_tcp_ops__destroy(skel);
+ netns_free(ns);
+ close(cgroup_fd);
+}
+
+/*
+ * Two-level hierarchy. Attach ops1 to the parent and ops2 to the child, then
+ * trigger from a socket in the child. Descendant progs run before ancestor
+ * progs, so the order is [2 (child), 1 (parent)].
+ */
+static void test_hierarchy(int parent_fd, int child_fd, struct bpf_tcp_ops *skel)
+{
+ struct bpf_link *plink = NULL, *clink = NULL;
+
+ plink = bpf_map__attach_cgroup_opts(skel->maps.tcp_ops1, parent_fd, NULL);
+ if (!ASSERT_OK_PTR(plink, "attach_parent"))
+ goto done;
+
+ clink = bpf_map__attach_cgroup_opts(skel->maps.tcp_ops2, child_fd, NULL);
+ if (!ASSERT_OK_PTR(clink, "attach_child"))
+ goto done;
+
+ reset_order(skel);
+ do_listen_connect(AF_INET6);
+
+ ASSERT_EQ(skel->bss->listen_cnt, 2, "listen_cnt");
+ ASSERT_EQ(skel->bss->listen_order[0], 2, "listen_order[0]");
+ ASSERT_EQ(skel->bss->listen_order[1], 1, "listen_order[1]");
+
+done:
+ bpf_link__destroy(clink);
+ bpf_link__destroy(plink);
+}
+
+static void run_hierarchy_subtest(void)
+{
+ struct bpf_tcp_ops *skel = NULL;
+ struct netns_obj *ns = NULL;
+ int parent_fd, child_fd = -1;
+
+ parent_fd = test__join_cgroup(CGROUP_PATH);
+ if (!ASSERT_GE(parent_fd, 0, "join_parent_cgroup"))
+ return;
+
+ child_fd = create_and_get_cgroup(CGROUP_PATH "/child");
+ if (!ASSERT_GE(child_fd, 0, "create_child_cgroup"))
+ goto done;
+
+ if (!ASSERT_OK(join_cgroup(CGROUP_PATH "/child"), "join_child_cgroup"))
+ goto done;
+
+ ns = netns_new(TEST_NETNS, true);
+ if (!ASSERT_OK_PTR(ns, "netns_new"))
+ goto done;
+
+ skel = bpf_tcp_ops__open_and_load();
+ if (!ASSERT_OK_PTR(skel, "open_and_load"))
+ goto done;
+
+ test_hierarchy(parent_fd, child_fd, skel);
+
+done:
+ bpf_tcp_ops__destroy(skel);
+ netns_free(ns);
+ if (child_fd >= 0)
+ close(child_fd);
+ close(parent_fd);
+}
+
+/*
+ * Attach ops1 to the parent, then create and join the child cgroup. The child
+ * is created after the attach, so it must inherit the parent's effective progs
+ * via cgroup_bpf_inherit(). A socket in the child runs the parent's prog.
+ */
+static void test_inherit(int parent_fd, struct bpf_tcp_ops *skel)
+{
+ struct bpf_link *plink = NULL;
+ int child_fd = -1;
+
+ plink = bpf_map__attach_cgroup_opts(skel->maps.tcp_ops1, parent_fd, NULL);
+ if (!ASSERT_OK_PTR(plink, "attach_parent"))
+ goto done;
+
+ child_fd = create_and_get_cgroup(CGROUP_PATH "/child");
+ if (!ASSERT_GE(child_fd, 0, "create_child_cgroup"))
+ goto done;
+
+ if (!ASSERT_OK(join_cgroup(CGROUP_PATH "/child"), "join_child_cgroup"))
+ goto done;
+
+ reset_order(skel);
+ do_listen_connect(AF_INET6);
+
+ ASSERT_EQ(skel->bss->listen_cnt, 1, "listen_cnt");
+ ASSERT_EQ(skel->bss->listen_order[0], 1, "listen_order[0]");
+
+done:
+ if (child_fd >= 0)
+ close(child_fd);
+ bpf_link__destroy(plink);
+}
+
+static void run_inherit_subtest(void)
+{
+ struct bpf_tcp_ops *skel = NULL;
+ struct netns_obj *ns = NULL;
+ int parent_fd;
+
+ parent_fd = test__join_cgroup(CGROUP_PATH);
+ if (!ASSERT_GE(parent_fd, 0, "join_parent_cgroup"))
+ return;
+
+ ns = netns_new(TEST_NETNS, true);
+ if (!ASSERT_OK_PTR(ns, "netns_new"))
+ goto done;
+
+ skel = bpf_tcp_ops__open_and_load();
+ if (!ASSERT_OK_PTR(skel, "open_and_load"))
+ goto done;
+
+ test_inherit(parent_fd, skel);
+
+done:
+ bpf_tcp_ops__destroy(skel);
+ netns_free(ns);
+ close(parent_fd);
+}
+
+void test_bpf_tcp_ops(void)
+{
+ if (test__start_subtest("order"))
+ run_order_subtest();
+ if (test__start_subtest("before_after"))
+ run_before_after_subtest();
+ if (test__start_subtest("query"))
+ run_query_subtest();
+ if (test__start_subtest("retval"))
+ run_retval_subtest();
+ if (test__start_subtest("update"))
+ run_update_subtest();
+ if (test__start_subtest("hierarchy"))
+ run_hierarchy_subtest();
+ if (test__start_subtest("inherit"))
+ run_inherit_subtest();
+}
diff --git a/tools/testing/selftests/bpf/prog_tests/bpf_tcp_ops_hdr.c b/tools/testing/selftests/bpf/prog_tests/bpf_tcp_ops_hdr.c
new file mode 100644
index 00000000000000..73e34d2be9a456
--- /dev/null
+++ b/tools/testing/selftests/bpf/prog_tests/bpf_tcp_ops_hdr.c
@@ -0,0 +1,97 @@
+// SPDX-License-Identifier: GPL-2.0
+/* Copyright (c) 2026 Meta Platforms, Inc. and affiliates. */
+
+#include <test_progs.h>
+#include <network_helpers.h>
+#include "cgroup_helpers.h"
+#include "bpf_tcp_ops_hdr.skel.h"
+
+#define CGROUP_PATH "/bpf_tcp_ops_hdr"
+#define TEST_NETNS "bpf_tcp_ops_hdr"
+
+#define TEST_OPT_D0 0xAB
+#define TEST_OPT_D1 0xCD
+
+static void send_recv(void)
+{
+ char buf[64] = {};
+ int server_fd, client_fd, accept_fd;
+ ssize_t n;
+
+ server_fd = start_server(AF_INET6, SOCK_STREAM, "::1", 0, 0);
+ if (!ASSERT_GE(server_fd, 0, "start_server"))
+ return;
+
+ client_fd = connect_to_fd(server_fd, 0);
+ if (!ASSERT_OK_FD(client_fd, "connect_to_fd"))
+ goto close_server;
+
+ accept_fd = accept(server_fd, NULL, NULL);
+ if (!ASSERT_OK_FD(accept_fd, "accept"))
+ goto close_client;
+
+ /* Exchange data both directions so option-bearing data packets
+ * are sent and parsed on each side.
+ */
+ n = send(client_fd, buf, sizeof(buf), 0);
+ ASSERT_EQ(n, sizeof(buf), "client_send");
+ n = recv(accept_fd, buf, sizeof(buf), 0);
+ ASSERT_EQ(n, sizeof(buf), "server_recv");
+
+ n = send(accept_fd, buf, sizeof(buf), 0);
+ ASSERT_EQ(n, sizeof(buf), "server_send");
+ n = recv(client_fd, buf, sizeof(buf), 0);
+ ASSERT_EQ(n, sizeof(buf), "client_recv");
+
+ close(accept_fd);
+close_client:
+ close(client_fd);
+close_server:
+ close(server_fd);
+}
+
+static void run_hdr_opt(void)
+{
+ struct bpf_tcp_ops_hdr *skel = NULL;
+ struct bpf_link *link = NULL;
+ struct netns_obj *ns = NULL;
+ int cgroup_fd;
+
+ cgroup_fd = test__join_cgroup(CGROUP_PATH);
+ if (!ASSERT_GE(cgroup_fd, 0, "join_cgroup"))
+ return;
+
+ ns = netns_new(TEST_NETNS, true);
+ if (!ASSERT_OK_PTR(ns, "netns_new"))
+ goto done;
+
+ skel = bpf_tcp_ops_hdr__open_and_load();
+ if (!ASSERT_OK_PTR(skel, "open_and_load"))
+ goto done;
+
+ link = bpf_map__attach_cgroup_opts(skel->maps.test_hdr_ops, cgroup_fd, NULL);
+ if (!ASSERT_OK_PTR(link, "attach_cgroup"))
+ goto done;
+
+ send_recv();
+
+ /* Reserve + write hooks ran while sending. */
+ ASSERT_GT(skel->bss->hdr_opt_len_cnt, 0, "hdr_opt_len_cnt");
+ ASSERT_GT(skel->bss->write_cnt, 0, "write_cnt");
+ /* Parse hook ran and recovered our option on the receive side. */
+ ASSERT_GT(skel->bss->parse_cnt, 0, "parse_cnt");
+ ASSERT_GT(skel->bss->found_cnt, 0, "found_cnt");
+ ASSERT_EQ(skel->bss->found_d0, TEST_OPT_D0, "found_d0");
+ ASSERT_EQ(skel->bss->found_d1, TEST_OPT_D1, "found_d1");
+
+done:
+ bpf_link__destroy(link);
+ bpf_tcp_ops_hdr__destroy(skel);
+ netns_free(ns);
+ close(cgroup_fd);
+}
+
+void test_bpf_tcp_ops_hdr(void)
+{
+ run_hdr_opt();
+}
diff --git a/tools/testing/selftests/bpf/progs/bpf_tcp_ops.c b/tools/testing/selftests/bpf/progs/bpf_tcp_ops.c
new file mode 100644
index 00000000000000..94a7f52573d517
--- /dev/null
+++ b/tools/testing/selftests/bpf/progs/bpf_tcp_ops.c
@@ -0,0 +1,141 @@
+// SPDX-License-Identifier: GPL-2.0
+/* Copyright (c) 2026 Meta Platforms, Inc. and affiliates. */
+
+#include "vmlinux.h"
+#include <bpf/bpf_helpers.h>
+#include <bpf/bpf_tracing.h>
+
+#define MAX_CGROUP_OPS 8
+
+/* Call order for listen and connect, indexed by call sequence */
+u32 listen_order[MAX_CGROUP_OPS];
+u32 listen_cnt;
+
+u32 connect_order[MAX_CGROUP_OPS];
+u32 connect_cnt;
+
+static void record_listen(int id)
+{
+ u32 idx = listen_cnt;
+
+ if (idx < MAX_CGROUP_OPS) {
+ listen_order[idx] = id;
+ listen_cnt = idx + 1;
+ }
+}
+
+static void record_connect(int id)
+{
+ u32 idx = connect_cnt;
+
+ if (idx < MAX_CGROUP_OPS) {
+ connect_order[idx] = id;
+ connect_cnt = idx + 1;
+ }
+}
+
+/* struct_ops instance 1 */
+
+SEC("struct_ops")
+void BPF_PROG(tcp_ops1_listen, struct sock *sk)
+{
+ record_listen(1);
+}
+
+SEC("struct_ops")
+void BPF_PROG(tcp_ops1_connect, struct sock *sk)
+{
+ record_connect(1);
+}
+
+SEC(".struct_ops.link")
+struct bpf_tcp_ops tcp_ops1 = {
+ .listen = (void *)tcp_ops1_listen,
+ .connect = (void *)tcp_ops1_connect,
+};
+
+/* struct_ops instance 2 */
+
+SEC("struct_ops")
+void BPF_PROG(tcp_ops2_listen, struct sock *sk)
+{
+ record_listen(2);
+}
+
+SEC("struct_ops")
+void BPF_PROG(tcp_ops2_connect, struct sock *sk)
+{
+ record_connect(2);
+}
+
+SEC(".struct_ops.link")
+struct bpf_tcp_ops tcp_ops2 = {
+ .listen = (void *)tcp_ops2_listen,
+ .connect = (void *)tcp_ops2_connect,
+};
+
+/* struct_ops instance 3 */
+
+SEC("struct_ops")
+void BPF_PROG(tcp_ops3_listen, struct sock *sk)
+{
+ record_listen(3);
+}
+
+SEC("struct_ops")
+void BPF_PROG(tcp_ops3_connect, struct sock *sk)
+{
+ record_connect(3);
+}
+
+SEC(".struct_ops.link")
+struct bpf_tcp_ops tcp_ops3 = {
+ .listen = (void *)tcp_ops3_listen,
+ .connect = (void *)tcp_ops3_connect,
+};
+
+#define OPS_RETVAL1 11
+#define OPS_RETVAL2 22
+#define OPS_RETVAL3 33
+
+int retval_saw1;
+int retval_saw2;
+int retval_saw3;
+
+SEC("struct_ops")
+int BPF_PROG(tcp_ops_retval1_timeout_init, struct sock *sk, struct request_sock *req)
+{
+ retval_saw1 = bpf_get_retval();
+ return OPS_RETVAL1;
+}
+
+SEC(".struct_ops.link")
+struct bpf_tcp_ops tcp_ops_retval1 = {
+ .timeout_init = (void *)tcp_ops_retval1_timeout_init,
+};
+
+SEC("struct_ops")
+int BPF_PROG(tcp_ops_retval2_timeout_init, struct sock *sk, struct request_sock *req)
+{
+ retval_saw2 = bpf_get_retval();
+ return OPS_RETVAL2;
+}
+
+SEC(".struct_ops.link")
+struct bpf_tcp_ops tcp_ops_retval2 = {
+ .timeout_init = (void *)tcp_ops_retval2_timeout_init,
+};
+
+SEC("struct_ops")
+int BPF_PROG(tcp_ops_retval3_timeout_init, struct sock *sk, struct request_sock *req)
+{
+ retval_saw3 = bpf_get_retval();
+ return OPS_RETVAL3;
+}
+
+SEC(".struct_ops.link")
+struct bpf_tcp_ops tcp_ops_retval3 = {
+ .timeout_init = (void *)tcp_ops_retval3_timeout_init,
+};
+
+char _license[] SEC("license") = "GPL";
diff --git a/tools/testing/selftests/bpf/progs/bpf_tcp_ops_hdr.c b/tools/testing/selftests/bpf/progs/bpf_tcp_ops_hdr.c
new file mode 100644
index 00000000000000..46618a604d965a
--- /dev/null
+++ b/tools/testing/selftests/bpf/progs/bpf_tcp_ops_hdr.c
@@ -0,0 +1,83 @@
+// SPDX-License-Identifier: GPL-2.0
+/* Copyright (c) 2026 Meta Platforms, Inc. and affiliates. */
+
+#include "vmlinux.h"
+#include <bpf/bpf_helpers.h>
+#include <bpf/bpf_tracing.h>
+
+/* Experimental option kind and payload written/parsed by this test. */
+#define TEST_OPT_KIND 0xFD
+#define TEST_OPT_LEN 4
+#define TEST_OPT_D0 0xAB
+#define TEST_OPT_D1 0xCD
+
+int hdr_opt_len_cnt;
+int write_cnt;
+int parse_cnt;
+int found_cnt;
+__u8 found_d0;
+__u8 found_d1;
+
+SEC("struct_ops")
+void BPF_PROG(test_hdr_opt_len, struct sock *sk, struct sk_buff *skb,
+ struct request_sock *req, struct sk_buff *syn_skb,
+ enum tcp_synack_type synack_type, unsigned int *remaining)
+{
+ hdr_opt_len_cnt++;
+
+ /* Reserve TEST_OPT_LEN bytes; the helper decrements *remaining. Stacks
+ * with other progs in the cgroup hierarchy.
+ */
+ bpf_reserve_hdr_opt(ctx, TEST_OPT_LEN, 0);
+}
+
+SEC("struct_ops")
+void BPF_PROG(test_write_hdr_opt, struct sock *sk, struct sk_buff *skb,
+ struct request_sock *req, struct sk_buff *syn_skb,
+ enum tcp_synack_type synack_type, __u32 opt_off)
+{
+ __u8 opt[TEST_OPT_LEN] = {
+ TEST_OPT_KIND, TEST_OPT_LEN, TEST_OPT_D0, TEST_OPT_D1,
+ };
+
+ /* bpf_store_hdr_opt() takes the program ctx (the kernel reads the
+ * outgoing skb from it); it appends after any options already written
+ * in the reserved window, rejects duplicates, and confines the write to
+ * the header option scratch. Stacks across progs in the cgroup hierarchy.
+ */
+ if (bpf_store_hdr_opt(ctx, opt, sizeof(opt), 0))
+ return;
+
+ write_cnt++;
+}
+
+SEC("struct_ops")
+void BPF_PROG(test_parse_hdr, struct sock *sk, struct sk_buff *skb)
+{
+ __u8 opt[TEST_OPT_LEN] = {
+ TEST_OPT_KIND, TEST_OPT_LEN, TEST_OPT_D0, TEST_OPT_D1,
+ };
+
+ parse_cnt++;
+
+ /* Look up the experimental option written by test_write_hdr_opt() in
+ * the incoming skb. For an experimental kind the search matches on the
+ * 2-byte magic in opt[2..3]; on a match the found option is copied back
+ * into opt[].
+ */
+ if (bpf_load_hdr_opt(ctx, opt, sizeof(opt), 0) < 0)
+ return;
+
+ found_d0 = opt[2];
+ found_d1 = opt[3];
+ found_cnt++;
+}
+
+SEC(".struct_ops.link")
+struct bpf_tcp_ops test_hdr_ops = {
+ .hdr_opt_len = (void *)test_hdr_opt_len,
+ .write_hdr_opt = (void *)test_write_hdr_opt,
+ .parse_hdr = (void *)test_parse_hdr,
+};
+
+char _license[] SEC("license") = "GPL";
|
| 3/2 | 2026/06/23 18:13 | llm | patch-evaluator |
0mModel:gemini-3.1-pro-preview Args: null Results: null Tokens: input: 46718 output: 22 thoughts: 1145 Thoughts: **Analyzing the BPF TCP Operations Patch Series** Okay, so I'm looking at this patch series that introduces a new `bpf_tcp_ops` type for BPF, and it's designed to be attached to cgroups. This is significant, a functional change that introduces new BPF functionality within the TCP stack – definitely something that warrants a closer look. I've got a fairly extensive list of files modified across the kernel: the core BPF infrastructure, cgroup definitions, TCP-specific code, and, importantly, the user-space tools for interacting with this new functionality. The extent of the modifications tells me this is more than a trivial change; it's touching fundamental parts of the BPF and TCP architecture. The fact that `WorthFuzzing=true` has been declared in the series already gives a hint of the scale of possible issues that might show up with this. The build configurations needed are `CONFIG_BPF_JIT` and `CONFIG_CGROUP_BPF` which are both required for the compilation of `bpf_tcp_ops.c`. Then `CONFIG_BPF_SYSCALL` is also needed because the `net/ipv4/Makefile` has the following logic: ```makefile ifeq ($(CONFIG_BPF_JIT),y) obj-$(CONFIG_BPF_SYSCALL) += bpf_tcp_ca.o obj-$(CONFIG_CGROUP_BPF) += bpf_tcp_ops.o endif ``` I'm making a note that syzbot often enables these, so I'm thinking about how this could be fuzz tested. Now, let's drill down into the key functions. In `net/ipv4/bpf_tcp_ops.c`, I've got: * `bpf_tcp_ops_store_hdr_opt` * `bpf_tcp_ops_load_hdr_opt` * `bpf_tcp_ops_reserve_hdr_opt` * `bpf_tcp_ops_get_retval` These are going to be crucial for understanding how the BPF programs interact with the TCP header and flow. In `kernel/bpf/cgroup.c`, I see a cluster of cgroup-specific functions: * `cgroup_bpf_struct_ops_register` * `cgroup_struct_ops_link_detach` * `cgroup_struct_ops_link_dealloc` * `cgroup_struct_ops_link_show_fdinfo` * `cgroup_struct_ops_link_fill_link_info` * `cgroup_struct_ops_link_update` * `cgroup_struct_ops_link_poll` * `cgroup_bpf_struct_ops_attach` This confirms that the patch is deeply integrated with the cgroup BPF framework, which is exactly as expected. Then, within `kernel/bpf/bpf_struct_ops.c`, I see: * `bpf_struct_ops_map_free_pre_rcu` * `bpf_struct_ops_map_kdata` * `bpf_struct_ops_kdata_map_id` * `bpf_struct_ops_map_cgroup_atype` * `bpf_struct_ops_map_cfi_stubs` * `bpf_struct_ops_valid_to_reg` * `bpf_struct_ops_link_update_check` These seem related to struct operations and map management, again, fundamental for the correct operation of the entire feature. Beyond those, I'll be looking out for how these are actually being *used* in the TCP stack. The `bpf_tcp_ops_call` macro is defined and used in: * `net/ipv4/tcp.c` * `net/ipv4/tcp_input.c` * `net/ipv4/tcp_output.c` * `net/ipv4/tcp_timer.c` * `net/ipv4/af_inet.c` I'll also have a look at: * `bpf_tcp_ops_parse_hdr` * `bpf_tcp_ops_hdr_opt_len` These will be important because the TCP functionality is core to the feature. A quick `grep` will allow me to check if there's anything else interesting that's not listed. |
| 4/2 | 2026/06/23 18:13 | tool | grepper |
0mArgs:{
"Expression": "bpf_tcp_ops_call"
}Results: {
"Output": "include/net/tcp.h=2958=struct bpf_tcp_ops {\n--\ninclude/net/tcp.h-3048-\ninclude/net/tcp.h:3049:#define bpf_tcp_ops_call(op, sk, ...)\t\t\t\t\t\\\ninclude/net/tcp.h-3050-do {\t\t\t\t\t\t\t\t\t\\\n--\ninclude/net/tcp.h-3066-\ninclude/net/tcp.h:3067:#define bpf_tcp_ops_call_int(op, init_retval, sk, ...)\t\t\t\\\ninclude/net/tcp.h-3068-({\t\t\t\t\t\t\t\t\t\\\n--\ninclude/net/tcp.h-3099-#else\ninclude/net/tcp.h:3100:#define bpf_tcp_ops_call(op, sk, ...)\t\tdo { } while (0)\ninclude/net/tcp.h:3101:#define bpf_tcp_ops_call_int(op, init_retval, sk, ...)\t(init_retval)\ninclude/net/tcp.h-3102-#endif\n--\ninclude/net/tcp.h=3104=static inline u32 tcp_timeout_init(struct sock *sk)\n--\ninclude/net/tcp.h-3108-\ttimeout = tcp_call_bpf(sk, BPF_SOCK_OPS_TIMEOUT_INIT, 0, NULL);\ninclude/net/tcp.h:3109:\ttimeout = bpf_tcp_ops_call_int(timeout_init, timeout, sk);\ninclude/net/tcp.h-3110-\tif (timeout \u003c= 0)\n--\ninclude/net/tcp.h=3115=static inline u32 tcp_rwnd_init_bpf(struct sock *sk)\n--\ninclude/net/tcp.h-3119-\trwnd = tcp_call_bpf(sk, BPF_SOCK_OPS_RWND_INIT, 0, NULL);\ninclude/net/tcp.h:3120:\trwnd = bpf_tcp_ops_call_int(rwnd_init, rwnd, sk);\ninclude/net/tcp.h-3121-\tif (rwnd \u003c 0)\n--\ninclude/net/tcp.h=3131=static inline void tcp_bpf_rtt(struct sock *sk, long mrtt, u32 srtt)\n--\ninclude/net/tcp.h-3134-\t\ttcp_call_bpf_2arg(sk, BPF_SOCK_OPS_RTT_CB, mrtt, srtt);\ninclude/net/tcp.h:3135:\tbpf_tcp_ops_call(rtt, sk, mrtt, srtt);\ninclude/net/tcp.h-3136-}\n--\nnet/ipv4/af_inet.c=198=int __inet_listen_sk(struct sock *sk, int backlog)\n--\nnet/ipv4/af_inet.c-229-\t\ttcp_call_bpf(sk, BPF_SOCK_OPS_TCP_LISTEN_CB, 0, NULL);\nnet/ipv4/af_inet.c:230:\t\tbpf_tcp_ops_call(listen, sk);\nnet/ipv4/af_inet.c-231-\t}\n--\nnet/ipv4/bpf_tcp_ops.c=185=BPF_CALL_0(bpf_tcp_ops_get_retval)\n--\nnet/ipv4/bpf_tcp_ops.c-190-\t/* bpf_get_retval() is only exposed to timeout_init/rwnd_init, which\nnet/ipv4/bpf_tcp_ops.c:191:\t * always run via bpf_tcp_ops_call_int(). Its run_ctx carries the int\nnet/ipv4/bpf_tcp_ops.c-192-\t * return value chained across the bpf_tcp_ops attached to the cgroup\n--\nnet/ipv4/tcp.c=2960=void tcp_set_state(struct sock *sk, int state)\n--\nnet/ipv4/tcp.c-2998-\t\ttcp_call_bpf_2arg(sk, BPF_SOCK_OPS_STATE_CB, oldstate, state);\nnet/ipv4/tcp.c:2999:\tbpf_tcp_ops_call(set_state, sk, state);\nnet/ipv4/tcp.c-3000-\n--\nnet/ipv4/tcp_input.c=211=static void bpf_tcp_ops_parse_hdr(struct sock *sk, struct sk_buff *skb)\n--\nnet/ipv4/tcp_input.c-219-\nnet/ipv4/tcp_input.c:220:\tbpf_tcp_ops_call(parse_hdr, sk, skb);\nnet/ipv4/tcp_input.c-221-}\n--\nnet/ipv4/tcp_input.c=6688=void tcp_init_transfer(struct sock *sk, int bpf_op, struct sk_buff *skb)\n--\nnet/ipv4/tcp_input.c-6710-\tif (bpf_op == BPF_SOCK_OPS_ACTIVE_ESTABLISHED_CB)\nnet/ipv4/tcp_input.c:6711:\t\tbpf_tcp_ops_call(active_established, sk, skb);\nnet/ipv4/tcp_input.c-6712-\telse\nnet/ipv4/tcp_input.c:6713:\t\tbpf_tcp_ops_call(passive_established, sk, skb);\nnet/ipv4/tcp_input.c-6714-\t/* Initialize congestion control unless BPF initialized it already: */\n--\nnet/ipv4/tcp_output.c=533=static void bpf_skops_write_hdr_opt(struct sock *sk, struct sk_buff *skb,\n--\nnet/ipv4/tcp_output.c-580-\t */\nnet/ipv4/tcp_output.c:581:\tbpf_tcp_ops_call(write_hdr_opt, sk, skb, req, syn_skb, synack_type,\nnet/ipv4/tcp_output.c-582-\t\t\t first_opt_off + nr_written);\n--\nnet/ipv4/tcp_output.c=604=static u32 bpf_tcp_ops_hdr_opt_len(struct sock *sk, struct sk_buff *skb,\n--\nnet/ipv4/tcp_output.c-616-\t/* bpf_tcp_ops_reserve_hdr_opt() reserves space via remaining_out */\nnet/ipv4/tcp_output.c:617:\tbpf_tcp_ops_call(hdr_opt_len, sk, skb, req, syn_skb, synack_type, \u0026remaining_out);\nnet/ipv4/tcp_output.c-618-\n--\nnet/ipv4/tcp_output.c=3596=int __tcp_retransmit_skb(struct sock *sk, struct sk_buff *skb, int segs)\n--\nnet/ipv4/tcp_output.c-3726-\t\t\t\t TCP_SKB_CB(skb)-\u003eseq, segs, err);\nnet/ipv4/tcp_output.c:3727:\tbpf_tcp_ops_call(retrans, sk, skb, err);\nnet/ipv4/tcp_output.c-3728-\n--\nnet/ipv4/tcp_output.c=4341=int tcp_connect(struct sock *sk)\n--\nnet/ipv4/tcp_output.c-4347-\ttcp_call_bpf(sk, BPF_SOCK_OPS_TCP_CONNECT_CB, 0, NULL);\nnet/ipv4/tcp_output.c:4348:\tbpf_tcp_ops_call(connect, sk);\nnet/ipv4/tcp_output.c-4349-\n--\nnet/ipv4/tcp_timer.c=243=static int tcp_write_timeout(struct sock *sk)\n--\nnet/ipv4/tcp_timer.c-292-\t\t\t\t icsk-\u003eicsk_rto, (int)expired);\nnet/ipv4/tcp_timer.c:293:\tbpf_tcp_ops_call(rto, sk);\nnet/ipv4/tcp_timer.c-294-\n"
} |
| 5/2 | 2026/06/23 18:13 | llm | patch-evaluator |
0mModel:gemini-3.1-pro-preview Args: null Results: null Tokens: input: 51006 output: 24 thoughts: 595 Thoughts: **Identifying Key BPF Focus Functions for Targeted Testing** Okay, so I'm thinking about how to zero in on the relevant functions for BPF program execution or attachment as the core focus points for our analysis. Right off the bat, I know that the macro `bpf_tcp_ops_call` isn't a symbol, so I'll need to look at the functions it invokes or relates to. Initially, I was looking at things like `tcp_timeout_init`, `tcp_rwnd_init_bpf`, and others, but then it hit me. These are generic TCP hot-path functions. If I focus on these I'm going to get a skewed distribution during fuzzing, which the prompt specifically says to avoid. That means I need to concentrate on the *BPF-specific* functions; the ones that have been added or modified as a result of the BPF implementation. So, I'll hone in on the code in `net/ipv4/bpf_tcp_ops.c`. From that, the key candidates seem to be: `bpf_tcp_ops_store_hdr_opt`, `bpf_tcp_ops_load_hdr_opt`, `bpf_tcp_ops_reserve_hdr_opt`, and `bpf_tcp_ops_get_retval`. These are definitely on the radar. Next, I need to look at functions related to cgroup, specifically in `kernel/bpf/cgroup.c`. Here, I'm finding: `cgroup_bpf_struct_ops_register`, `cgroup_struct_ops_link_detach`, `cgroup_struct_ops_link_dealloc`, `cgroup_struct_ops_link_show_fdinfo`, `cgroup_struct_ops_link_fill_link_info`, `cgroup_struct_ops_link_update`, `cgroup_struct_ops_link_poll`, and `cgroup_bpf_struct_ops_attach`. These are all interesting because they're related to cgroup integration with BPF. Finally, within `kernel/bpf/bpf_struct_ops.c` I see: `bpf_struct_ops_map_free_pre_rcu`, `bpf_struct_ops_map_kdata`, `bpf_struct_ops_kdata_map_id`, `bpf_struct_ops_map_cgroup_atype`, `bpf_struct_ops_map_cfi_stubs`, `bpf_struct_ops_valid_to_reg`, and `bpf_struct_ops_link_update_check`. These deal with the struct operations, including the maps. I'm thinking I should take a quick look back at `net/ipv4/bpf_tcp_ops.c` to see if there's anything else I missed, but I think I have a solid starting list. |
| 6/2 | 2026/06/23 18:13 | tool | grepper |
0mArgs:{
"Expression": "^static.*bpf_tcp_ops_"
}Results: {
"Output": "net/ipv4/bpf_tcp_ops.c=95=BPF_CALL_4(bpf_tcp_ops_store_hdr_opt, void *, ctx, const void *, from,\n--\nnet/ipv4/bpf_tcp_ops.c-122-\nnet/ipv4/bpf_tcp_ops.c:123:static const struct bpf_func_proto bpf_tcp_ops_store_hdr_opt_proto = {\nnet/ipv4/bpf_tcp_ops.c-124-\t.func\t\t= bpf_tcp_ops_store_hdr_opt,\n--\nnet/ipv4/bpf_tcp_ops.c=133=BPF_CALL_4(bpf_tcp_ops_load_hdr_opt, void *, ctx, void *, search_res,\n--\nnet/ipv4/bpf_tcp_ops.c-151-\nnet/ipv4/bpf_tcp_ops.c:152:static const struct bpf_func_proto bpf_tcp_ops_load_hdr_opt_proto = {\nnet/ipv4/bpf_tcp_ops.c-153-\t.func\t\t= bpf_tcp_ops_load_hdr_opt,\n--\nnet/ipv4/bpf_tcp_ops.c=162=BPF_CALL_3(bpf_tcp_ops_reserve_hdr_opt, void *, ctx, u32, len, u64, flags)\n--\nnet/ipv4/bpf_tcp_ops.c-175-\nnet/ipv4/bpf_tcp_ops.c:176:static const struct bpf_func_proto bpf_tcp_ops_reserve_hdr_opt_proto = {\nnet/ipv4/bpf_tcp_ops.c-177-\t.func\t\t= bpf_tcp_ops_reserve_hdr_opt,\n--\nnet/ipv4/bpf_tcp_ops.c=258=static bool is_valid_access(int off, int size, enum bpf_access_type type,\n--\nnet/ipv4/bpf_tcp_ops.c-272-\nnet/ipv4/bpf_tcp_ops.c:273:static int bpf_tcp_ops_init_member(const struct btf_type *t,\nnet/ipv4/bpf_tcp_ops.c-274-\t\t\t\t const struct btf_member *member,\n--\nnet/ipv4/bpf_tcp_ops.c-279-\nnet/ipv4/bpf_tcp_ops.c:280:static int bpf_tcp_ops_init(struct btf *btf)\nnet/ipv4/bpf_tcp_ops.c-281-{\n--\nnet/ipv4/bpf_tcp_ops.c-284-\nnet/ipv4/bpf_tcp_ops.c:285:static int bpf_tcp_ops_validate(void *kdata)\nnet/ipv4/bpf_tcp_ops.c-286-{\n--\nnet/ipv4/bpf_tcp_ops.c-289-\nnet/ipv4/bpf_tcp_ops.c:290:static const struct bpf_verifier_ops bpf_tcp_ops_verifier = {\nnet/ipv4/bpf_tcp_ops.c-291-\t.get_func_proto\t\t= get_func_proto,\n--\nnet/ipv4/bpf_tcp_ops.c=295=static struct bpf_struct_ops bpf_tcp_ops = {\n--\nnet/ipv4/bpf_tcp_ops.c-305-\nnet/ipv4/bpf_tcp_ops.c:306:static int __init __bpf_tcp_ops_init(void)\nnet/ipv4/bpf_tcp_ops.c-307-{\n--\nnet/ipv4/tcp_input.c=205=static void bpf_skops_established(struct sock *sk, int bpf_op,\n--\nnet/ipv4/tcp_input.c-210-\nnet/ipv4/tcp_input.c:211:static void bpf_tcp_ops_parse_hdr(struct sock *sk, struct sk_buff *skb)\nnet/ipv4/tcp_input.c-212-{\n--\nnet/ipv4/tcp_output.c=595=static void bpf_skops_write_hdr_opt(struct sock *sk, struct sk_buff *skb,\n--\nnet/ipv4/tcp_output.c-603-\nnet/ipv4/tcp_output.c:604:static u32 bpf_tcp_ops_hdr_opt_len(struct sock *sk, struct sk_buff *skb,\nnet/ipv4/tcp_output.c-605-\t\t\t\t struct request_sock *req,\n--\ntools/testing/selftests/bpf/prog_tests/bpf_tcp_ops.c-12-\ntools/testing/selftests/bpf/prog_tests/bpf_tcp_ops.c:13:static __s32 get_bpf_tcp_ops_type_id(void)\ntools/testing/selftests/bpf/prog_tests/bpf_tcp_ops.c-14-{\n"
} |
| 7/2 | 2026/06/23 18:13 | llm | patch-evaluator |
0mModel:gemini-3.1-pro-preview Args: null Results: null Tokens: input: 53580 output: 318 thoughts: 682 Thoughts: **My Assessment of the BPF TCP Cgroup Integration** Alright, let's break this down. My primary focus here is on the new BPF functionality being introduced and its potential implications. I've identified the key functions involved in this patch, namely those related to TCP header option manipulation (`bpf_tcp_ops_store_hdr_opt`, `bpf_tcp_ops_load_hdr_opt`, `bpf_tcp_ops_reserve_hdr_opt`, `bpf_tcp_ops_parse_hdr`, `bpf_tcp_ops_hdr_opt_len` and retrieval (`bpf_tcp_ops_get_retval`) as well as the cgroup and BPF integration points: `cgroup_bpf_struct_ops_register`, `cgroup_struct_ops_link_detach`, `cgroup_struct_ops_link_update`, `cgroup_bpf_struct_ops_attach`, and `bpf_struct_ops_link_update_check`. This includes aspects of map management, and link checking. I've refined the list to the essential functions I need to analyze for functionality and security. I also noted that the inclusion of BPF TCP operations in cgroups provides granular control over TCP events, which is interesting. The ability to hook into things like header option parsing, writing and reservation, plus TCP state changes on a per-cgroup basis, definitely opens up some interesting use cases – and, critically, increases the attack surface. The core of this is the addition of the `bpf_tcp_ops` struct_ops type, which allows BPF programs to be attached to cgroups. This is how the per-cgroup hooks are implemented. This means looking closely at how the attachment logic works, how the struct_ops map is managed (I'll need to drill down into the map functions), and the specific hooks within the TCP stack. I can see this as a functional change that adds new code paths in the kernel. This immediately flags it as something that *needs* fuzzing. We're dealing with new code, new interaction points between BPF and TCP, and new potential for unexpected behavior or, worse, vulnerabilities. Finally, I checked and confirmed the necessary configurations: `BPF_JIT`, `BPF_SYSCALL`, `CGROUP_BPF`, and obviously `NET`, making sure the build environment is set correctly. Now, I have to set some results, and move on. |
| 8/2 | 2026/06/23 18:13 | tool | set-results |
0mArgs:{
"EnableConfigs": [
"BPF_JIT",
"BPF_SYSCALL",
"CGROUP_BPF"
],
"FocusSymbols": [
"bpf_tcp_ops_store_hdr_opt",
"bpf_tcp_ops_load_hdr_opt",
"bpf_tcp_ops_reserve_hdr_opt",
"bpf_tcp_ops_get_retval",
"cgroup_bpf_struct_ops_register",
"cgroup_struct_ops_link_detach",
"cgroup_struct_ops_link_update",
"cgroup_bpf_struct_ops_attach",
"bpf_struct_ops_link_update_check",
"bpf_tcp_ops_parse_hdr",
"bpf_tcp_ops_hdr_opt_len"
],
"Reasoning": "The patch introduces a new BPF struct_ops type `bpf_tcp_ops` that can be attached to cgroups. This allows BPF programs to hook into various TCP events (like timeout initialization, RTT measurement, state changes, and header option parsing/writing) on a per-cgroup basis. The changes involve core BPF cgroup attachment logic, struct_ops map management, and TCP stack hooks. It is a functional change that adds new reachable code paths in the kernel, making it worth fuzzing.",
"WorthFuzzing": true
}Results: {
"EnableConfigs": [
"BPF_JIT",
"BPF_SYSCALL",
"CGROUP_BPF"
],
"FocusSymbols": [
"bpf_tcp_ops_store_hdr_opt",
"bpf_tcp_ops_load_hdr_opt",
"bpf_tcp_ops_reserve_hdr_opt",
"bpf_tcp_ops_get_retval",
"cgroup_bpf_struct_ops_register",
"cgroup_struct_ops_link_detach",
"cgroup_struct_ops_link_update",
"cgroup_bpf_struct_ops_attach",
"bpf_struct_ops_link_update_check",
"bpf_tcp_ops_parse_hdr",
"bpf_tcp_ops_hdr_opt_len"
],
"Reasoning": "The patch introduces a new BPF struct_ops type `bpf_tcp_ops` that can be attached to cgroups. This allows BPF programs to hook into various TCP events (like timeout initialization, RTT measurement, state changes, and header option parsing/writing) on a per-cgroup basis. The changes involve core BPF cgroup attachment logic, struct_ops map management, and TCP stack hooks. It is a functional change that adds new reachable code paths in the kernel, making it worth fuzzing.",
"WorthFuzzing": true
} |
| Total Calls | Total Tokens | Avg Tokens | Total Duration (Seconds) | Avg Duration (Seconds) |
|---|
| Total Calls | Total Duration (Seconds) | Avg Duration (Seconds) |
|---|