bpf qdisc
Subsystems (10)
v3 cover bpf qdisc
Hi all,
This patchset aims to support implementing qdisc using bpf struct_ops. This version takes a step back and only implements the minimum support for bpf qdisc. 1) support of adding skb to bpf_list and bpf_rbtree directly and 2) classful qdisc are deferred to future patchsets. In addition, we only allow attaching bpf qdisc to root or mq for now. This is to prevent accidentally breaking exisiting classful qdiscs that rely on data in a child qdisc. This limit may be lifted in the future after careful inspection.
* Overview *
This series supports implementing qdisc using bpf struct_ops. bpf qdisc aims to be a flexible and easy-to-use infrastructure that allows users to quickly experiment with different scheduling algorithms/policies. It only requires users to implement core qdisc logic using bpf and implements the mundane part for them. In addition, the ability to easily communicate between qdisc and other components will also bring new opportunities for new applications and optimizations.
* struct_ops changes *
To make struct_ops works better with bpf qdisc, two new changes are introduced to bpf specifically for struct_ops programs. Frist, we introduce "__ref" postfix for arguments in stub functions in patch 1-2. It allows Qdisc_ops->enqueue to acquire an unique referenced kptr to the skb argument. Through the reference object tracking mechanism in the verifier, we can make sure that the acquired skb will be either enqueued or dropped. Besides, no duplicate references can be acquired. Then, we allow a referenced kptr to be returned from struct_ops programs so that we can return an skb naturally. This is done and tested in patch 3 and 4.
* Performance of bpf qdisc *
This patchset includes two qdisc examples, bpf_fifo and bpf_fq, for __testing__ purposes. For performance test, we compare selftests and their kernel counterparts to give you a sense of the performance of qdisc implemented in bpf.
The implementation of bpf_fq is fairly complex and slightly different from fq so later we only compare the two fifo qdiscs. bpf_fq implements a scheduling algorithm similar to fq before commit 29f834aa326e ("net_sched:sch_fq: add 3 bands and WRR scheduling") was introduced. bpf_fifo uses a single bpf_list as a queue instead of three queues for different priorities in pfifo_fast. The time complexity of fifo however should be similar since the queue selection time is negligible.
Test setup:
client -> qdisc -------------> server
~~~~~~~~~~~~~~~ ~~~~~~
nested VM1 @ DC1 VM2 @ DC2Throghput: iperf3 -t 600, 5 times
Qdisc Average (GBits/sec)
---------- -------------------
pfifo_fast 12.52 ± 0.26
bpf_fifo 11.72 ± 0.32
fq 10.24 ± 0.13
bpf_fq 11.92 ± 0.64 Latency: sockperf pp --tcp -t 600, 5 times
Qdisc Average (usec)
---------- --------------
pfifo_fast 244.58 ± 7.93
bpf_fifo 244.92 ± 15.22
fq 234.30 ± 19.25
bpf_fq 221.34 ± 10.76Looking at the two fifo qdiscs, the 6.4% drop in throughput in the bpf implementatioin is consistent with previous observation (v8 throughput test on a loopback device). This should be able to be mitigated by supporting adding skb to bpf_list or bpf_rbtree directly in the future.
* Clean up skb in bpf qdisc during reset *
The current implementation relies on bpf qdisc implementors to correctly release skbs in queues (bpf graphs or maps) in .reset, which might not be a safe thing to do. The solution as Martin has suggested would be supporting private data in struct_ops. This can also help simplifying implementation of qdisc that works with mq. For examples, qdiscs in the selftest mostly use global data. Therefore, even if user add multiple qdisc instances under mq, they would still share the same queue.
* Misc *
This patchset has grown a bit in this iteration. I can split the kptr acquire/release mechanism for struct_ops into another set if that makes sense and helps people review.
The performance numbers will be updated again when the set gets closer to landing.
---
v3:
* Rebase to bpf-next/master
* Remove the requirement in the verifier that "__ref arguments must acquire ref_obj_id first" by making each prog keeping a copy of arg_ctx_info and saving ref_obj_id in it.
* Generalize prog_ops_moff() to work with any struct_op (now called
bpf_struct_ops_prog_moff())
* Use bpf_struct_ops_prog_moff(prog) instead of
prog->aux->attach_func_name to infer the ops of a program * Limit attach to root and mq for now and add corresponding selftests
* Simplify qdisc selftests with network_helper
* Fix fq_remove_flow() not deleting the stashed flow
v2: Rebase to bpf-next/master
Patch 1-4
Remove the use of ctx_arg_info->ref_obj_id when acquiring referenced kptr from struct_ops arg Improve type comparison when checking kptr return from struct_ops
Simplify selftests with test_loader and nomerge attribute
Patch 5
Remove redundant checks in qdisc_init
Disallow tail_call
Patch 6
Improve kfunc ops availabilty filter by
i) Checking struct_ops->type
ii) Defining op-specific kfunc set
Patch 7
Search and add bpf_kfunc_desc after gen_prologue/epilogue
Patch 8
Use gen_prologue/epilogue to init/cancel watchdog timer
Patch 12
Mark read-only func arg and struct member const in libbpf
v1:
Fix struct_ops referenced kptr acquire/return mechanisms Allow creating dynptr from skb
Add bpf qdisc kfunc filter
Support updating bstats and qstats
Update qdiscs in selftest to update stats
Add gc, handle hash collision and fix bugs in fq_bpf
past RFCs
v9: Drop classful qdisc operations and kfuncs
Drop support of enqueuing skb directly to bpf_rbtree/list
v8: Implement support of bpf qdisc using struct_ops
Allow struct_ops to acquire referenced kptr via argument Allow struct_ops to release and return referenced kptr Support enqueuing sk_buff to bpf_rbtree/list Move examples from samples to selftests
Add a classful qdisc selftest
v7: Reference skb using kptr to sk_buff instead of __sk_buff
Use the new bpf rbtree/link to for skb queues Add reset and init programs
Add a bpf fq qdisc sample
Add a bpf netem qdisc sample
v6: switch to kptr based approach
v5: mv kernel/bpf/skb_map.c net/core/skb_map.c
implement flow map as map-in-map
rename bpf_skb_tc_classify() and move it to net/sched/cls_api.c clean up eBPF qdisc program context
v4: get rid of PIFO, use rbtree directly
v3: move priority queue from sch_bpf to skb map
introduce skb map and its helpers
introduce bpf_skb_classify()
use netdevice notifier to reset skb's
Rebase on latest bpf-next
v2: Rebase on latest net-next
Make the code more complete (but still incomplete)
Amery Hung (18):
bpf: Make every prog keep a copy of ctx_arg_info
bpf: Support getting referenced kptr from struct_ops argument selftests/bpf: Test referenced kptr arguments of struct_ops programs
bpf: Allow struct_ops prog to return referenced kptr selftests/bpf: Test returning referenced kptr from struct_ops programs
bpf: Prepare to reuse get_ctx_arg_idx
bpf: Generalize finding member offset of struct_ops prog
bpf: net_sched: Support implementation of Qdisc_ops in bpf
bpf: net_sched: Add basic bpf qdisc kfuncs
bpf: Search and add kfuncs in struct_ops prologue and epilogue
bpf: net_sched: Add a qdisc watchdog timer
bpf: net_sched: Support updating bstats
bpf: net_sched: Support updating qstats
bpf: net_sched: Allow writing to more Qdisc members
libbpf: Support creating and destroying qdisc selftests/bpf: Add a basic fifo qdisc test
selftests/bpf: Add a bpf fq qdisc to selftest selftests/bpf: Test attaching bpf qdisc to mq and non root
include/linux/bpf.h | 12 +- include/linux/btf.h | 1 + include/linux/filter.h | 10 + kernel/bpf/bpf_iter.c | 13 +- kernel/bpf/bpf_struct_ops.c | 53 +- kernel/bpf/btf.c | 7 +- kernel/bpf/verifier.c | 121 ++- net/ipv4/bpf_tcp_ca.c | 23 +- net/sched/Kconfig | 12 + net/sched/Makefile | 1 + net/sched/bpf_qdisc.c | 447 +++++++++++ net/sched/sch_api.c | 14 +- net/sched/sch_generic.c | 3 +- tools/lib/bpf/libbpf.h | 5 +- tools/lib/bpf/netlink.c | 20 +- tools/testing/selftests/bpf/config | 2 + .../selftests/bpf/prog_tests/bpf_qdisc.c | 210 +++++ .../prog_tests/test_struct_ops_kptr_return.c | 16 + .../prog_tests/test_struct_ops_refcounted.c | 12 + .../selftests/bpf/progs/bpf_qdisc_common.h | 27 + .../selftests/bpf/progs/bpf_qdisc_fifo.c | 117 +++ .../selftests/bpf/progs/bpf_qdisc_fq.c | 720 ++++++++++++++++++ .../bpf/progs/struct_ops_kptr_return.c | 30 + ...uct_ops_kptr_return_fail__invalid_scalar.c | 26 + .../struct_ops_kptr_return_fail__local_kptr.c | 34 + ...uct_ops_kptr_return_fail__nonzero_offset.c | 25 + .../struct_ops_kptr_return_fail__wrong_type.c | 30 + .../bpf/progs/struct_ops_refcounted.c | 31 + ...ruct_ops_refcounted_fail__global_subprog.c | 39 + .../struct_ops_refcounted_fail__ref_leak.c | 22 + .../selftests/bpf/test_kmods/bpf_testmod.c | 15 + .../selftests/bpf/test_kmods/bpf_testmod.h | 6 + 32 files changed, 2038 insertions(+), 66 deletions(-) create mode 100644 net/sched/bpf_qdisc.c create mode 100644 tools/testing/selftests/bpf/prog_tests/bpf_qdisc.c create mode 100644 tools/testing/selftests/bpf/prog_tests/test_struct_ops_kptr_return.c create mode 100644 tools/testing/selftests/bpf/prog_tests/test_struct_ops_refcounted.c create mode 100644 tools/testing/selftests/bpf/progs/bpf_qdisc_common.h create mode 100644 tools/testing/selftests/bpf/progs/bpf_qdisc_fifo.c create mode 100644 tools/testing/selftests/bpf/progs/bpf_qdisc_fq.c create mode 100644 tools/testing/selftests/bpf/progs/struct_ops_kptr_return.c create mode 100644 tools/testing/selftests/bpf/progs/struct_ops_kptr_return_fail__invalid_scalar.c create mode 100644 tools/testing/selftests/bpf/progs/struct_ops_kptr_return_fail__local_kptr.c create mode 100644 tools/testing/selftests/bpf/progs/struct_ops_kptr_return_fail__nonzero_offset.c create mode 100644 tools/testing/selftests/bpf/progs/struct_ops_kptr_return_fail__wrong_type.c create mode 100644 tools/testing/selftests/bpf/progs/struct_ops_refcounted.c create mode 100644 tools/testing/selftests/bpf/progs/struct_ops_refcounted_fail__global_subprog.c create mode 100644 tools/testing/selftests/bpf/progs/struct_ops_refcounted_fail__ref_leak.c
Signature · 1 line
2.47.1
PATCH v3 1/18 bpf: Make every prog keep a copy of ctx_arg_info
Currently, ctx_arg_info is read-only in the view of the verifier since it is shared among programs of the same attach type. Make each program have their own copy of ctx_arg_info so that we can use it to store program specific information.
In the next patch where we support acquiring a referenced kptr through a struct_ops argument tagged with "__ref", ctx_arg_info->ref_obj_id will be used to store the unique reference object id of the argument. This avoids creating a requirement in the verifier that "__ref" tagged arguments must be the first set of references acquired [0].
[0] https://lore.kernel.org/bpf/[email protected]/
--- include/linux/bpf.h | 7 +++++-- kernel/bpf/bpf_iter.c | 13 ++++++------- kernel/bpf/verifier.c | 25 +++++++++++++++---------- 3 files changed, 26 insertions(+), 19 deletions(-)
include/linux/bpf.h +5 -2
@@ -1507,7 +1507,7 @@ struct bpf_prog_aux {u32 max_rdonly_access;u32 max_rdwr_access;struct btf *attach_btf; -const struct bpf_ctx_arg_aux *ctx_arg_info; +struct bpf_ctx_arg_aux *ctx_arg_info;void __percpu *priv_stack_ptr;struct mutex dst_mutex;/* protects dst_* pointers below, *after* prog becomes visible */ struct bpf_prog *dst_prog;
@@ -1945,6 +1945,9 @@ static inline void bpf_struct_ops_desc_release(struct bpf_struct_ops_desc *st_op#endif +int bpf_prog_ctx_arg_info_init (struct bpf_prog *prog, +const struct bpf_ctx_arg_aux *info,u32 cnt); +#if defined(CONFIG_CGROUP_BPF ) && defined(CONFIG_BPF_LSM )int bpf_trampoline_link_cgroup_shim (struct bpf_prog *prog,int cgroup_atype);
@@ -2546,7 +2549,7 @@ struct bpf_iter__bpf_map_elem {int bpf_iter_reg_target (const struct bpf_iter_reg *reg_info);void bpf_iter_unreg_target (const struct bpf_iter_reg *reg_info); -bool bpf_iter_prog_supported (struct bpf_prog *prog); +int bpf_iter_prog_supported (struct bpf_prog *prog);const struct bpf_func_proto *bpf_iter_get_func_proto (enum bpf_func_id func_id,const struct bpf_prog *prog);int bpf_iter_link_attach (const union bpf_attr *attr,bpfptr_t uattr,struct bpf_prog *prog);
kernel/bpf/bpf_iter.c +6 -7
@@ -335,7 +335,7 @@ static void cache_btf_id(struct bpf_iter_target_info *tinfo, tinfo->btf_id = prog->aux->attach_btf_id; } -bool bpf_iter_prog_supported (struct bpf_prog *prog) +int bpf_iter_prog_supported (struct bpf_prog *prog) {const char *attach_fname = prog->aux->attach_func_name;struct bpf_iter_target_info *tinfo =NULL , *iter;
@@ -344,7 +344,7 @@ bool bpf_iter_prog_supported(struct bpf_prog *prog)int prefix_len =strlen (prefix);if (strncmp (attach_fname, prefix, prefix_len)) -return false; +return -EINVAL ;mutex_lock (&targets_mutex);list_for_each_entry (iter, &targets, list) {
@@ -360,12 +360,11 @@ bool bpf_iter_prog_supported(struct bpf_prog *prog) }mutex_unlock (&targets_mutex); -if (tinfo) { - prog->aux->ctx_arg_info_size = tinfo->reg_info->ctx_arg_info_size; - prog->aux->ctx_arg_info = tinfo->reg_info->ctx_arg_info; - } +if (!tinfo) +return -EINVAL ; -return tinfo !=NULL ; +return bpf_prog_ctx_arg_info_init (prog, tinfo->reg_info->ctx_arg_info, + tinfo->reg_info->ctx_arg_info_size); } const struct bpf_func_proto *
kernel/bpf/verifier.c +15 -10
@@ -22377,6 +22377,18 @@ static void print_verification_stats(struct bpf_verifier_env *env) env->peak_states, env->longest_mark_read_walk); } +int bpf_prog_ctx_arg_info_init (struct bpf_prog *prog, +const struct bpf_ctx_arg_aux *info,u32 cnt) +{ + prog->aux->ctx_arg_info =kcalloc (cnt,sizeof (*info),GFP_KERNEL ); +if (!prog->aux->ctx_arg_info) +return -ENOMEM ; + +memcpy (prog->aux->ctx_arg_info, info,sizeof (*info) * cnt); + prog->aux->ctx_arg_info_size = cnt; +return 0 ; +} +static int check_struct_ops_btf_id (struct bpf_verifier_env *env) {const struct btf_type *t, *func_proto;
@@ -22457,17 +22469,12 @@ static int check_struct_ops_btf_id(struct bpf_verifier_env *env)return -EACCES ; } -/* btf_ctx_access() used this to provide argument type info */ - prog->aux->ctx_arg_info = - st_ops_desc->arg_info[member_idx].info; - prog->aux->ctx_arg_info_size = - st_ops_desc->arg_info[member_idx].cnt; - prog->aux->attach_func_proto = func_proto; prog->aux->attach_func_name = mname; env->ops = st_ops->verifier_ops; -return 0 ; +return bpf_prog_ctx_arg_info_init (prog, st_ops_desc->arg_info[member_idx].info, + st_ops_desc->arg_info[member_idx].cnt); }#define SECURITY_PREFIX "security_"
@@ -22917,9 +22924,7 @@ static int check_attach_btf_id(struct bpf_verifier_env *env) prog->aux->attach_btf_trace = true;return 0 ; }else if (prog->expected_attach_type ==BPF_TRACE_ITER ) { -if (!bpf_iter_prog_supported (prog)) -return -EINVAL ; -return 0 ; +return bpf_iter_prog_supported (prog); }if (prog->type ==BPF_PROG_TYPE_LSM ) {
Signature · 1 line
2.47.1
Re: [PATCH bpf-next v3 01/18] bpf: Make every prog keep a copy of ctx_arg_info
Quoted 11 lines · Amery Hung
Currently,
ctx_arg_infois read-only in the view of the verifier since it is shared among programs of the same attach type. Make each program have their own copy ofctx_arg_infoso that we can use it to store program specific information.In the next patch where we support acquiring a referenced kptr through a
struct_opsargument tagged with "__ref",ctx_arg_info->ref_obj_idwill be used to store the unique reference object id of the argument. This avoids creating a requirement in the verifier that "__ref" tagged arguments must be the first set of references acquired [0].
Quoted 4 lines
[0] https://lore.kernel.org/bpf/[email protected]/
Signed-off-by Amery Hung <[email protected]>---
[...]
PATCH v3 2/18 bpf: Support getting referenced kptr from struct_ops argument
In reply to Amery HungFrom: Amery Hung <[email protected]>
Allows struct_ops programs to acqurie referenced kptrs from arguments by directly reading the argument.
The verifier will acquire a reference for struct_ops a argument tagged with "__ref" in the stub function in the beginning of the main program. The user will be able to access the referenced kptr directly by reading the context as long as it has not been released by the program.
This new mechanism to acquire referenced kptr (compared to the existing "kfunc with KF_ACQUIRE") is introduced for ergonomic and semantic reasons. In the first use case, Qdisc_ops, an skb is passed to .enqueue in the first argument. This mechanism provides a natural way for users to get a referenced kptr in the .enqueue struct_ops programs and makes sure that a qdisc will always enqueue or drop the skb.
--- include/linux/bpf.h | 3 +++ kernel/bpf/bpf_struct_ops.c | 26 ++++++++++++++++++++------ kernel/bpf/btf.c | 1 + kernel/bpf/verifier.c | 35 ++++++++++++++++++++++++++++++++--- 4 files changed, 56 insertions(+), 9 deletions(-)
include/linux/bpf.h +3 -0
@@ -968,6 +968,7 @@ struct bpf_insn_access_aux {struct {struct btf *btf;u32 btf_id; +u32 ref_obj_id; }; };struct bpf_verifier_log *log;/* for verbose logs */
@@ -1481,6 +1482,8 @@ struct bpf_ctx_arg_aux {enum bpf_reg_type reg_type;struct btf *btf;u32 btf_id; +u32 ref_obj_id; +bool refcounted; };struct btf_mod_pair {
kernel/bpf/bpf_struct_ops.c +20 -6
@@ -146,6 +146,7 @@ void bpf_struct_ops_image_free(void *image) }#define MAYBE_NULL_SUFFIX "__nullable" +#define REFCOUNTED_SUFFIX "__ref"/* Prepare argument info for every nullable argument of a member of a * struct_ops type.
@@ -174,11 +175,13 @@ static int prepare_arg_info(struct btf *btf, struct bpf_struct_ops_arg_info *arg_info) {const struct btf_type *stub_func_proto, *pointed_type; +bool is_nullable = false, is_refcounted = false;const struct btf_param *stub_args, *args;struct bpf_ctx_arg_aux *info, *info_buf;u32 nargs, arg_no, info_cnt =0 ;char ksym[KSYM_SYMBOL_LEN ];const char *stub_fname; +const char *suffix;s32 stub_func_id;u32 arg_btf_id;int offset;
@@ -223,12 +226,19 @@ static int prepare_arg_info(struct btf *btf, info = info_buf;for (arg_no =0 ; arg_no < nargs; arg_no++) {/* Skip arguments that is not suffixed with -* "__nullable". +* "__nullable or __ref". */ -if (!btf_param_match_suffix (btf, &stub_args[arg_no], -MAYBE_NULL_SUFFIX )) + is_nullable =btf_param_match_suffix (btf, &stub_args[arg_no], +MAYBE_NULL_SUFFIX ); + is_refcounted =btf_param_match_suffix (btf, &stub_args[arg_no], +REFCOUNTED_SUFFIX ); +if (!is_nullable && !is_refcounted)continue ; +if (is_nullable) + suffix =MAYBE_NULL_SUFFIX ; +else if (is_refcounted) + suffix =REFCOUNTED_SUFFIX ;/* Should be a pointer to struct */ pointed_type = btf_type_resolve_ptr(btf, args[arg_no].type,
@@ -236,7 +246,7 @@ static int prepare_arg_info(struct btf *btf,if (!pointed_type || !btf_type_is_struct (pointed_type)) {pr_warn ("stub function %s has %s tagging to an unsupported type\n" , - stub_fname,MAYBE_NULL_SUFFIX ); + stub_fname, suffix); goto err_out; }
@@ -254,11 +264,15 @@ static int prepare_arg_info(struct btf *btf, }/* Fill the information of the new argument */ - info->reg_type = -PTR_TRUSTED |PTR_TO_BTF_ID |PTR_MAYBE_NULL ; info->btf_id = arg_btf_id; info->btf = btf; info->offset = offset; +if (is_nullable) { + info->reg_type =PTR_TRUSTED |PTR_TO_BTF_ID |PTR_MAYBE_NULL ; + }else if (is_refcounted) { + info->reg_type =PTR_TRUSTED |PTR_TO_BTF_ID ; + info->refcounted = true; + } info++; info_cnt++;
kernel/bpf/btf.c +1 -0
@@ -6677,6 +6677,7 @@ bool btf_ctx_access(int off, int size, enum bpf_access_type type, info->reg_type = ctx_arg_info->reg_type; info->btf = ctx_arg_info->btf ? : btf_vmlinux; info->btf_id = ctx_arg_info->btf_id; + info->ref_obj_id = ctx_arg_info->refcounted ? ctx_arg_info->ref_obj_id :0 ;return true; } }
kernel/bpf/verifier.c +32 -3
@@ -1543,6 +1543,17 @@ static void release_reference_state(struct bpf_verifier_state *state, int idx)return ; } +static bool find_reference_state (struct bpf_verifier_state *state,int ptr_id) +{ +int i; + +for (i =0 ; i < state->acquired_refs; i++) +if (state->refs[i].id == ptr_id) +return true; + +return false; +} +static int release_lock_state (struct bpf_verifier_state *state,int type,int id,void *ptr) {int i;
@@ -5981,7 +5992,8 @@ static int check_packet_access(struct bpf_verifier_env *env, u32 regno, int off,/* check access to 'struct bpf_context' fields. Supports fixed offsets only */ static int check_ctx_access (struct bpf_verifier_env *env,int insn_idx,int off,int size,enum bpf_access_type t,enum bpf_reg_type *reg_type, -struct btf **btf,u32 *btf_id,bool *is_retval,bool is_ldsx) +struct btf **btf,u32 *btf_id,bool *is_retval,bool is_ldsx, +u32 *ref_obj_id) {struct bpf_insn_access_aux info = { .reg_type = *reg_type,
@@ -6003,8 +6015,16 @@ static int check_ctx_access(struct bpf_verifier_env *env, int insn_idx, int off, *is_retval = info.is_retval;if (base_type (*reg_type) ==PTR_TO_BTF_ID ) { +if (info.ref_obj_id && + !find_reference_state (env->cur_state, info.ref_obj_id)) { +verbose (env,"invalid bpf_context access off=%d. Reference may already be released\n" , + off); +return -EACCES ; + } + *btf = info.btf; *btf_id = info.btf_id; + *ref_obj_id = info.ref_obj_id; }else { env->insn_aux_data[insn_idx].ctx_field_size = info.ctx_field_size; }
@@ -7367,7 +7387,7 @@ static int check_mem_access(struct bpf_verifier_env *env, int insn_idx, u32 regnstruct bpf_retval_range range;enum bpf_reg_type reg_type =SCALAR_VALUE ;struct btf *btf =NULL ; -u32 btf_id =0 ; +u32 btf_id =0 , ref_obj_id =0 ;if (t ==BPF_WRITE && value_regno >=0 &&is_pointer_value (env, value_regno)) {
@@ -7380,7 +7400,7 @@ static int check_mem_access(struct bpf_verifier_env *env, int insn_idx, u32 regnreturn err; err =check_ctx_access (env, insn_idx, off, size, t, ®_type, &btf, - &btf_id, &is_retval, is_ldsx); + &btf_id, &is_retval, is_ldsx, &ref_obj_id);if (err)verbose_linfo (env, insn_idx,"; " );if (!err && t ==BPF_READ && value_regno >=0 ) {
@@ -7411,6 +7431,7 @@ static int check_mem_access(struct bpf_verifier_env *env, int insn_idx, u32 regnif (base_type (reg_type) ==PTR_TO_BTF_ID ) { regs[value_regno].btf = btf; regs[value_regno].btf_id = btf_id; + regs[value_regno].ref_obj_id = ref_obj_id; } } regs[value_regno].type = reg_type;
@@ -22148,6 +22169,7 @@ static int do_check_common(struct bpf_verifier_env *env, int subprog) {bool pop_log = !(env->log.level &BPF_LOG_LEVEL2 );struct bpf_subprog_info *sub =subprog_info (env, subprog); +struct bpf_prog_aux *aux = env->prog->aux;struct bpf_verifier_state *state;struct bpf_reg_state *regs;int ret, i;
@@ -22255,6 +22277,13 @@ static int do_check_common(struct bpf_verifier_env *env, int subprog)mark_reg_known_zero (env, regs,BPF_REG_1 ); } +/* Acquire references for struct_ops program arguments tagged with "__ref" */ +if (!subprog && env->prog->type ==BPF_PROG_TYPE_STRUCT_OPS ) { +for (i =0 ; i < aux->ctx_arg_info_size; i++) + aux->ctx_arg_info[i].ref_obj_id = aux->ctx_arg_info[i].refcounted ? +acquire_reference (env,0 ) :0 ; + } + ret =do_check (env); out:/* check for NULL is necessary, since cur_state can be freed inside
Signature · 1 line
2.47.1
Re: [PATCH bpf-next v3 02/18] bpf: Support getting referenced kptr from struct_ops argument
Quoted 17 lines · Amery Hung
From: Amery Hung <[email protected]>
Allows
struct_opsprograms to acqurie referenced kptrs from arguments by directly reading the argument.The verifier will acquire a reference for
struct_opsa argument tagged with "__ref" in the stub function in the beginning of the main program. The user will be able to access the referenced kptr directly by reading the context as long as it has not been released by the program.This new mechanism to acquire referenced kptr (compared to the existing "kfunc with
KF_ACQUIRE") is introduced for ergonomic and semantic reasons. In the first use case,Qdisc_ops, an skb is passed to .enqueue in the first argument. This mechanism provides a natural way for users to get a referenced kptr in the .enqueuestruct_opsprograms and makes sure that a qdisc will always enqueue or drop the skb.
Quoted 2 lines
Signed-off-by Amery Hung <[email protected]>---
[...]
PATCH v3 3/18 selftests/bpf: Test referenced kptr arguments of struct_ops programs
In reply to Amery HungFrom: Amery Hung <[email protected]>
Test referenced kptr acquired through struct_ops argument tagged with "__ref". The success case checks whether 1) a reference to the correct type is acquired, and 2) the referenced kptr argument can be accessed in multiple paths as long as it hasn't been released. In the fail cases, we first confirm that a referenced kptr acquried through a struct_ops argument is not allowed to be leaked. Then, we make sure this new referenced kptr acquiring mechanism does not accidentally allow referenced kptrs to flow into global subprograms through their arguments.
--- .../prog_tests/test_struct_ops_refcounted.c | 12 ++++++ .../bpf/progs/struct_ops_refcounted.c | 31 +++++++++++++++ ...ruct_ops_refcounted_fail__global_subprog.c | 39 +++++++++++++++++++ .../struct_ops_refcounted_fail__ref_leak.c | 22 +++++++++++ .../selftests/bpf/test_kmods/bpf_testmod.c | 7 ++++ .../selftests/bpf/test_kmods/bpf_testmod.h | 2 + 6 files changed, 113 insertions(+) create mode 100644 tools/testing/selftests/bpf/prog_tests/test_struct_ops_refcounted.c create mode 100644 tools/testing/selftests/bpf/progs/struct_ops_refcounted.c create mode 100644 tools/testing/selftests/bpf/progs/struct_ops_refcounted_fail__global_subprog.c create mode 100644 tools/testing/selftests/bpf/progs/struct_ops_refcounted_fail__ref_leak.c
tools/testing/selftests/bpf/prog_tests/test_struct_ops_refcounted.c new file +12 -0
@@ -0,0 +1,12 @@ +#include <test_progs.h> + +#include "struct_ops_refcounted.skel.h" +#include "struct_ops_refcounted_fail__ref_leak.skel.h" +#include "struct_ops_refcounted_fail__global_subprog.skel.h" + +void test_struct_ops_refcounted (void ) +{ +RUN_TESTS (struct_ops_refcounted); +RUN_TESTS (struct_ops_refcounted_fail__ref_leak); +RUN_TESTS (struct_ops_refcounted_fail__global_subprog); +}
tools/testing/selftests/bpf/progs/struct_ops_refcounted.c new file +31 -0
@@ -0,0 +1,31 @@ +#include <vmlinux.h> +#include <bpf/bpf_tracing.h> +#include "../test_kmods/bpf_testmod.h" +#include "bpf_misc.h" + +char _license[] SEC("license") = "GPL"; + +__attribute__((nomerge)) extern void bpf_task_release(struct task_struct *p) __ksym; + +/* This is a test BPF program that uses struct_ops to access a referenced +* kptr argument. This is a test for the verifier to ensure that it +* 1) recongnizes the task as a referenced object (i.e., ref_obj_id > 0), and +* 2) the same reference can be acquired from multiple paths as long as it +* has not been released. +*/ +SEC("struct_ops/test_refcounted") +int BPF_PROG (refcounted ,int dummy,struct task_struct *task) +{ +if (dummy ==1 ) +bpf_task_release (task); +else +bpf_task_release (task); +return 0 ; +} + +SEC(".struct_ops.link") +struct bpf_testmod_ops testmod_refcounted = { + .test_refcounted = (void *)refcounted, +}; + +
tools/testing/selftests/bpf/progs/struct_ops_refcounted_fail__global_subprog.c new file +39 -0
@@ -0,0 +1,39 @@ +#include <vmlinux.h> +#include <bpf/bpf_tracing.h> +#include "../test_kmods/bpf_testmod.h" +#include "bpf_misc.h" + +char _license[] SEC("license") = "GPL"; + +extern void bpf_task_release(struct task_struct *p) __ksym; + +__noinline int subprog_release(__u64 *ctx __arg_ctx) +{ +struct task_struct *task = (struct task_struct *)ctx[1 ]; +int dummy = (int )ctx[0 ]; + +bpf_task_release (task); + +return dummy +1 ; +} + +/* Test that the verifier rejects a program that contains a global +* subprogram with referenced kptr arguments +*/ +SEC("struct_ops/test_refcounted") +__failure __log_level(2) +__msg("Validating subprog_release() func#1...") +__msg("invalid bpf_context access off=8. Reference may already be released") +int refcounted_fail__global_subprog (unsigned long long *ctx) +{ +struct task_struct *task = (struct task_struct *)ctx[1 ]; + +bpf_task_release (task); + +return subprog_release (ctx); +} + +SEC(".struct_ops.link") +struct bpf_testmod_ops testmod_ref_acquire = { + .test_refcounted = (void *)refcounted_fail__global_subprog, +};
tools/testing/selftests/bpf/progs/struct_ops_refcounted_fail__ref_leak.c new file +22 -0
@@ -0,0 +1,22 @@ +#include <vmlinux.h> +#include <bpf/bpf_tracing.h> +#include "../test_kmods/bpf_testmod.h" +#include "bpf_misc.h" + +char _license[] SEC("license") = "GPL"; + +/* Test that the verifier rejects a program that acquires a referenced +* kptr through context without releasing the reference +*/ +SEC("struct_ops/test_refcounted") +__failure __msg("Unreleased reference id=1 alloc_insn=0") +int BPF_PROG(refcounted_fail__ref_leak, int dummy, + struct task_struct *task) +{ +return 0 ; +} + +SEC(".struct_ops.link") +struct bpf_testmod_ops testmod_ref_acquire = { + .test_refcounted = (void *)refcounted_fail__ref_leak, +};
tools/testing/selftests/bpf/test_kmods/bpf_testmod.c +7 -0
@@ -1176,10 +1176,17 @@ static int bpf_testmod_ops__test_maybe_null(int dummy,return 0 ; } +static int bpf_testmod_ops__test_refcounted (int dummy, +struct task_struct *task__ref) +{ +return 0 ; +} +static struct bpf_testmod_ops __bpf_testmod_ops = { .test_1 = bpf_testmod_test_1, .test_2 = bpf_testmod_test_2, .test_maybe_null = bpf_testmod_ops__test_maybe_null, + .test_refcounted = bpf_testmod_ops__test_refcounted, };struct bpf_struct_ops bpf_bpf_testmod_ops = {
tools/testing/selftests/bpf/test_kmods/bpf_testmod.h +2 -0
@@ -36,6 +36,8 @@ struct bpf_testmod_ops {/* Used to test nullable arguments. */ int (*test_maybe_null)(int dummy,struct task_struct *task);int (*unsupported_ops)(void ); +/* Used to test ref_acquired arguments. */ +int (*test_refcounted)(int dummy,struct task_struct *task);/* The following fields are used to test shadow copies. */ char onebyte;
Signature · 1 line
2.47.1
Re: [PATCH bpf-next v3 03/18] selftests/bpf: Test referenced kptr arguments of struct_ops programs
Quoted 11 lines · Amery Hung
From: Amery Hung <[email protected]>
Test referenced kptr acquired through
struct_opsargument tagged with "__ref". The success case checks whether 1) a reference to the correct type is acquired, and 2) the referenced kptr argument can be accessed in multiple paths as long as it hasn't been released. In the fail cases, we first confirm that a referenced kptr acquried through astruct_opsargument is not allowed to be leaked. Then, we make sure this new referenced kptr acquiring mechanism does not accidentally allow referenced kptrs to flow into global subprograms through their arguments.
Quoted 2 lines
Signed-off-by Amery Hung <[email protected]>---
[...]
PATCH v3 4/18 bpf: Allow struct_ops prog to return referenced kptr
In reply to Amery HungFrom: Amery Hung <[email protected]>
Allow a struct_ops program to return a referenced kptr if the struct_ops operator's return type is a struct pointer. To make sure the returned pointer continues to be valid in the kernel, several constraints are required:
1) The type of the pointer must matches the return type
2) The pointer originally comes from the kernel (not locally allocated)
3) The pointer is in its unmodified form
Implementation wise, a referenced kptr first needs to be allowed to _leak_ in check_reference_leak() if it is in the return register. Then, in check_return_code(), constraints 1-3 are checked. During struct_ops registration, a check is also added to warn about operators with non-struct pointer return.
In addition, since the first user, Qdisc_ops::dequeue, allows a NULL pointer to be returned when there is no skb to be dequeued, we will allow a scalar value with value equals to NULL to be returned.
In the future when there is a struct_ops user that always expects a valid pointer to be returned from an operator, we may extend tagging to the return value. We can tell the verifier to only allow NULL pointer return if the return value is tagged with MAY_BE_NULL.
--- kernel/bpf/bpf_struct_ops.c | 12 +++++++++++- kernel/bpf/verifier.c | 36 ++++++++++++++++++++++++++++++++---- 2 files changed, 43 insertions(+), 5 deletions(-)
kernel/bpf/bpf_struct_ops.c +11 -1
@@ -389,7 +389,7 @@ int bpf_struct_ops_desc_init(struct bpf_struct_ops_desc *st_ops_desc, st_ops_desc->value_type =btf_type_by_id (btf, value_id);for_each_member (i, t, member) { -const struct btf_type *func_proto; +const struct btf_type *func_proto, *ret_type;void **stub_func_addr;u32 moff;
@@ -426,6 +426,16 @@ int bpf_struct_ops_desc_init(struct bpf_struct_ops_desc *st_ops_desc,if (!func_proto ||bpf_struct_ops_supported (st_ops, moff))continue ; +if (func_proto->type) { + ret_type =btf_type_resolve_ptr (btf, func_proto->type,NULL ); +if (ret_type && !__btf_type_is_struct (ret_type)) { +pr_warn ("func ptr %s in struct %s returns non-struct pointer, which is not supported\n" , + mname, st_ops->name); + err = -EOPNOTSUPP ; + goto errout; + } + } +if (btf_distill_func_proto (log, btf, func_proto, mname, &st_ops->func_models[i])) {
kernel/bpf/verifier.c +32 -4
@@ -10758,6 +10758,8 @@ record_func_key(struct bpf_verifier_env *env, struct bpf_call_arg_meta *meta,static int check_reference_leak (struct bpf_verifier_env *env,bool exception_exit) {struct bpf_verifier_state *state = env->cur_state; +enum bpf_prog_type type =resolve_prog_type (env->prog); +struct bpf_reg_state *reg =reg_state (env,BPF_REG_0 );bool refs_lingering = false;int i;
@@ -10767,6 +10769,12 @@ static int check_reference_leak(struct bpf_verifier_env *env, bool exception_exifor (i =0 ; i < state->acquired_refs; i++) {if (state->refs[i].type !=REF_TYPE_PTR )continue ; +/* Allow struct_ops programs to return a referenced kptr back to +* kernel. Type checks are performed later in check_return_code. +*/ +if (type ==BPF_PROG_TYPE_STRUCT_OPS && !exception_exit && + reg->ref_obj_id == state->refs[i].id) +continue ;verbose (env,"Unreleased reference id=%d alloc_insn=%d\n" , state->refs[i].id, state->refs[i].insn_idx); refs_lingering = true;
@@ -16405,13 +16413,14 @@ static int check_return_code(struct bpf_verifier_env *env, int regno, const charconst char *exit_ctx ="At program exit" ;struct tnum enforce_attach_type_range = tnum_unknown;const struct bpf_prog *prog = env->prog; -struct bpf_reg_state *reg; +struct bpf_reg_state *reg =reg_state (env, regno);struct bpf_retval_range range =retval_range (0 ,1 );enum bpf_prog_type prog_type =resolve_prog_type (env->prog);int err;struct bpf_func_state *frame = env->cur_state->frame[0 ];const bool is_subprog = frame->subprogno;bool return_32bit = false; +const struct btf_type *reg_type, *ret_type =NULL ;/* LSM and struct_ops func-ptr's return type could be "void" */ if (!is_subprog || frame->in_exception_callback_fn) {
@@ -16420,10 +16429,26 @@ static int check_return_code(struct bpf_verifier_env *env, int regno, const charif (prog->expected_attach_type ==BPF_LSM_CGROUP )/* See below, can be 0 or 0-1 depending on hook. */ break ; - fallthrough; +if (!prog->aux->attach_func_proto->type) +return 0 ; +break ;case BPF_PROG_TYPE_STRUCT_OPS :if (!prog->aux->attach_func_proto->type)return 0 ; + +if (frame->in_exception_callback_fn) +break ; + +/* Allow a struct_ops program to return a referenced kptr if it +* matches the operator's return type and is in its unmodified +* form. A scalar zero (i.e., a null pointer) is also allowed. +*/ + reg_type = reg->btf ?btf_type_by_id (reg->btf, reg->btf_id) :NULL ; + ret_type =btf_type_resolve_ptr (prog->aux->attach_btf, + prog->aux->attach_func_proto->type, +NULL ); +if (ret_type && ret_type == reg_type && reg->ref_obj_id) +return __check_ptr_off_reg (env, reg, regno, false);break ;default :break ;
@@ -16445,8 +16470,6 @@ static int check_return_code(struct bpf_verifier_env *env, int regno, const charreturn -EACCES ; } - reg =cur_regs (env) + regno; -if (frame->in_async_callback_fn) {/* enforce return zero from async callbacks like timer */ exit_ctx ="At async callback return" ;
@@ -16545,6 +16568,11 @@ static int check_return_code(struct bpf_verifier_env *env, int regno, const charcase BPF_PROG_TYPE_NETFILTER : range =retval_range (NF_DROP ,NF_ACCEPT );break ; +case BPF_PROG_TYPE_STRUCT_OPS : +if (!ret_type) +return 0 ; + range =retval_range (0 ,0 ); +break ;case BPF_PROG_TYPE_EXT :/* freplace program can return anything as its return value * depends on the to-be-replaced kernel func or bpf program.
Signature · 1 line
2.47.1
Re: [PATCH bpf-next v3 04/18] bpf: Allow struct_ops prog to return referenced kptr
Quoted 21 lines · Amery Hung
From: Amery Hung <[email protected]>
Allow a
struct_opsprogram to return a referenced kptr if thestruct_opsoperator's return type is a struct pointer. To make sure the returned pointer continues to be valid in the kernel, several constraints are required:1) The type of the pointer must matches the return type
2) The pointer originally comes from the kernel (not locally allocated)
3) The pointer is in its unmodified formImplementation wise, a referenced kptr first needs to be allowed to _leak_ in
check_reference_leak()if it is in the return register. Then, incheck_return_code(), constraints 1-3 are checked. Duringstruct_opsregistration, a check is also added to warn about operators with non-struct pointer return.In addition, since the first user,
Qdisc_ops::dequeue, allows a NULL pointer to be returned when there is no skb to be dequeued, we will allow a scalar value with value equals to NULL to be returned.
Quoted 7 lines
In the future when there is a
struct_opsuser that always expects a valid pointer to be returned from an operator, we may extend tagging to the return value. We can tell the verifier to only allow NULL pointer return if the return value is tagged withMAY_BE_NULL.Signed-off-by Amery Hung <[email protected]>---
[...]
PATCH v3 5/18 selftests/bpf: Test returning referenced kptr from struct_ops programs
In reply to Amery HungFrom: Amery Hung <[email protected]>
Test struct_ops programs returning referenced kptr. When the return type of a struct_ops operator is pointer to struct, the verifier should only allow programs that return a scalar NULL or a non-local kptr with the correct type in its unmodified form.
--- .../prog_tests/test_struct_ops_kptr_return.c | 16 +++++++++ .../bpf/progs/struct_ops_kptr_return.c | 30 ++++++++++++++++ ...uct_ops_kptr_return_fail__invalid_scalar.c | 26 ++++++++++++++ .../struct_ops_kptr_return_fail__local_kptr.c | 34 +++++++++++++++++++ ...uct_ops_kptr_return_fail__nonzero_offset.c | 25 ++++++++++++++ .../struct_ops_kptr_return_fail__wrong_type.c | 30 ++++++++++++++++ .../selftests/bpf/test_kmods/bpf_testmod.c | 8 +++++ .../selftests/bpf/test_kmods/bpf_testmod.h | 4 +++ 8 files changed, 173 insertions(+) create mode 100644 tools/testing/selftests/bpf/prog_tests/test_struct_ops_kptr_return.c create mode 100644 tools/testing/selftests/bpf/progs/struct_ops_kptr_return.c create mode 100644 tools/testing/selftests/bpf/progs/struct_ops_kptr_return_fail__invalid_scalar.c create mode 100644 tools/testing/selftests/bpf/progs/struct_ops_kptr_return_fail__local_kptr.c create mode 100644 tools/testing/selftests/bpf/progs/struct_ops_kptr_return_fail__nonzero_offset.c create mode 100644 tools/testing/selftests/bpf/progs/struct_ops_kptr_return_fail__wrong_type.c
tools/testing/selftests/bpf/prog_tests/test_struct_ops_kptr_return.c new file +16 -0
@@ -0,0 +1,16 @@ +#include <test_progs.h> + +#include "struct_ops_kptr_return.skel.h" +#include "struct_ops_kptr_return_fail__wrong_type.skel.h" +#include "struct_ops_kptr_return_fail__invalid_scalar.skel.h" +#include "struct_ops_kptr_return_fail__nonzero_offset.skel.h" +#include "struct_ops_kptr_return_fail__local_kptr.skel.h" + +void test_struct_ops_kptr_return (void ) +{ +RUN_TESTS (struct_ops_kptr_return); +RUN_TESTS (struct_ops_kptr_return_fail__wrong_type); +RUN_TESTS (struct_ops_kptr_return_fail__invalid_scalar); +RUN_TESTS (struct_ops_kptr_return_fail__nonzero_offset); +RUN_TESTS (struct_ops_kptr_return_fail__local_kptr); +}
tools/testing/selftests/bpf/progs/struct_ops_kptr_return.c new file +30 -0
@@ -0,0 +1,30 @@ +#include <vmlinux.h> +#include <bpf/bpf_tracing.h> +#include "../test_kmods/bpf_testmod.h" +#include "bpf_misc.h" + +char _license[] SEC("license") = "GPL"; + +void bpf_task_release(struct task_struct *p) __ksym; + +/* This test struct_ops BPF programs returning referenced kptr. The verifier should +* allow a referenced kptr or a NULL pointer to be returned. A referenced kptr to task +* here is acquried automatically as the task argument is tagged with "__ref". +*/ +SEC("struct_ops/test_return_ref_kptr") +struct task_struct *BPF_PROG (kptr_return ,int dummy, +struct task_struct *task,struct cgroup *cgrp) +{ +if (dummy %2 ) { +bpf_task_release (task); +return NULL ; + } +return task; +} + +SEC(".struct_ops.link") +struct bpf_testmod_ops testmod_kptr_return = { + .test_return_ref_kptr = (void *)kptr_return, +}; + +
tools/testing/selftests/bpf/progs/struct_ops_kptr_return_fail__invalid_scalar.c new file +26 -0
@@ -0,0 +1,26 @@ +#include <vmlinux.h> +#include <bpf/bpf_tracing.h> +#include "../test_kmods/bpf_testmod.h" +#include "bpf_misc.h" + +char _license[] SEC("license") = "GPL"; + +struct cgroup *bpf_cgroup_acquire (struct cgroup *p) __ksym; +void bpf_task_release(struct task_struct *p) __ksym; + +/* This test struct_ops BPF programs returning referenced kptr. The verifier should +* reject programs returning a non-zero scalar value. +*/ +SEC("struct_ops/test_return_ref_kptr") +__failure __msg("At program exit the register R0 has smin=1 smax=1 should have been in [0, 0]") +struct task_struct *BPF_PROG(kptr_return_fail__invalid_scalar, int dummy, +struct task_struct *task,struct cgroup *cgrp) +{ +bpf_task_release (task); +return (struct task_struct *)1 ; +} + +SEC(".struct_ops.link") +struct bpf_testmod_ops testmod_kptr_return = { + .test_return_ref_kptr = (void *)kptr_return_fail__invalid_scalar, +};
tools/testing/selftests/bpf/progs/struct_ops_kptr_return_fail__local_kptr.c new file +34 -0
@@ -0,0 +1,34 @@ +#include <vmlinux.h> +#include <bpf/bpf_tracing.h> +#include "../test_kmods/bpf_testmod.h" +#include "bpf_experimental.h" +#include "bpf_misc.h" + +char _license[] SEC("license") = "GPL"; + +struct cgroup *bpf_cgroup_acquire (struct cgroup *p) __ksym; +void bpf_task_release(struct task_struct *p) __ksym; + +/* This test struct_ops BPF programs returning referenced kptr. The verifier should +* reject programs returning a local kptr. +*/ +SEC("struct_ops/test_return_ref_kptr") +__failure __msg("At program exit the register R0 is not a known value (ptr_or_null_)") +struct task_struct *BPF_PROG(kptr_return_fail__local_kptr, int dummy, +struct task_struct *task,struct cgroup *cgrp) +{ +struct task_struct *t; + +bpf_task_release (task); + + t =bpf_obj_new (typeof (*task)); +if (!t) +return NULL ; + +return t; +} + +SEC(".struct_ops.link") +struct bpf_testmod_ops testmod_kptr_return = { + .test_return_ref_kptr = (void *)kptr_return_fail__local_kptr, +};
tools/testing/selftests/bpf/progs/struct_ops_kptr_return_fail__nonzero_offset.c new file +25 -0
@@ -0,0 +1,25 @@ +#include <vmlinux.h> +#include <bpf/bpf_tracing.h> +#include "../test_kmods/bpf_testmod.h" +#include "bpf_misc.h" + +char _license[] SEC("license") = "GPL"; + +struct cgroup *bpf_cgroup_acquire (struct cgroup *p) __ksym; +void bpf_task_release(struct task_struct *p) __ksym; + +/* This test struct_ops BPF programs returning referenced kptr. The verifier should +* reject programs returning a modified referenced kptr. +*/ +SEC("struct_ops/test_return_ref_kptr") +__failure __msg("dereference of modified trusted_ptr_ ptr R0 off={{[0-9]+}} disallowed") +struct task_struct *BPF_PROG(kptr_return_fail__nonzero_offset, int dummy, +struct task_struct *task,struct cgroup *cgrp) +{ +return (struct task_struct *)&task->jobctl; +} + +SEC(".struct_ops.link") +struct bpf_testmod_ops testmod_kptr_return = { + .test_return_ref_kptr = (void *)kptr_return_fail__nonzero_offset, +};
tools/testing/selftests/bpf/progs/struct_ops_kptr_return_fail__wrong_type.c new file +30 -0
@@ -0,0 +1,30 @@ +#include <vmlinux.h> +#include <bpf/bpf_tracing.h> +#include "../test_kmods/bpf_testmod.h" +#include "bpf_misc.h" + +char _license[] SEC("license") = "GPL"; + +struct cgroup *bpf_cgroup_acquire (struct cgroup *p) __ksym; +void bpf_task_release(struct task_struct *p) __ksym; + +/* This test struct_ops BPF programs returning referenced kptr. The verifier should +* reject programs returning a referenced kptr of the wrong type. +*/ +SEC("struct_ops/test_return_ref_kptr") +__failure __msg("At program exit the register R0 is not a known value (ptr_or_null_)") +struct task_struct *BPF_PROG(kptr_return_fail__wrong_type, int dummy, +struct task_struct *task,struct cgroup *cgrp) +{ +struct task_struct *ret; + + ret = (struct task_struct *)bpf_cgroup_acquire (cgrp); +bpf_task_release (task); + +return ret; +} + +SEC(".struct_ops.link") +struct bpf_testmod_ops testmod_kptr_return = { + .test_return_ref_kptr = (void *)kptr_return_fail__wrong_type, +};
tools/testing/selftests/bpf/test_kmods/bpf_testmod.c +8 -0
@@ -1182,11 +1182,19 @@ static int bpf_testmod_ops__test_refcounted(int dummy,return 0 ; } +static struct task_struct * +bpf_testmod_ops__test_return_ref_kptr (int dummy,struct task_struct *task__ref, +struct cgroup *cgrp) +{ +return NULL ; +} +static struct bpf_testmod_ops __bpf_testmod_ops = { .test_1 = bpf_testmod_test_1, .test_2 = bpf_testmod_test_2, .test_maybe_null = bpf_testmod_ops__test_maybe_null, .test_refcounted = bpf_testmod_ops__test_refcounted, + .test_return_ref_kptr = bpf_testmod_ops__test_return_ref_kptr, };struct bpf_struct_ops bpf_bpf_testmod_ops = {
tools/testing/selftests/bpf/test_kmods/bpf_testmod.h +4 -0
@@ -6,6 +6,7 @@#include <linux/types.h> struct task_struct ; +struct cgroup ;struct bpf_testmod_test_read_ctx {char *buf;
@@ -38,6 +39,9 @@ struct bpf_testmod_ops {int (*unsupported_ops)(void );/* Used to test ref_acquired arguments. */ int (*test_refcounted)(int dummy,struct task_struct *task); +/* Used to test returning referenced kptr. */ +struct task_struct *(*test_return_ref_kptr)(int dummy,struct task_struct *task, +struct cgroup *cgrp);/* The following fields are used to test shadow copies. */ char onebyte;
Signature · 1 line
2.47.1
Re: [PATCH bpf-next v3 05/18] selftests/bpf: Test returning referenced kptr from struct_ops programs
Quoted 9 lines · Amery Hung
From: Amery Hung <[email protected]>
Test
struct_opsprograms returning referenced kptr. When the return type of astruct_opsoperator is pointer to struct, the verifier should only allow programs that return a scalar NULL or a non-local kptr with the correct type in its unmodified form.Signed-off-by Amery Hung <[email protected]>---
[...]
PATCH v3 6/18 bpf: Prepare to reuse get_ctx_arg_idx
In reply to Amery HungRename get_ctx_arg_idx to bpf_ctx_arg_idx, and allow others to call it. No functional change.
--- include/linux/btf.h | 1 + kernel/bpf/btf.c | 6 +++--- 2 files changed, 4 insertions(+), 3 deletions(-)
include/linux/btf.h +1 -0
@@ -519,6 +519,7 @@ bool btf_param_match_suffix(const struct btf *btf, const char *suffix);int btf_ctx_arg_offset (const struct btf *btf,const struct btf_type *func_proto,u32 arg_no); +u32 btf_ctx_arg_idx (struct btf *btf,const struct btf_type *func_proto,int off);struct bpf_verifier_log ;
kernel/bpf/btf.c +3 -3
@@ -6370,8 +6370,8 @@ static bool is_int_ptr(struct btf *btf, const struct btf_type *t)return btf_type_is_int (t); } -static u32 get_ctx_arg_idx (struct btf *btf,const struct btf_type *func_proto, -int off) +u32 btf_ctx_arg_idx (struct btf *btf,const struct btf_type *func_proto, +int off) {const struct btf_param *args;const struct btf_type *t;
@@ -6549,7 +6549,7 @@ bool btf_ctx_access(int off, int size, enum bpf_access_type type, tname, off);return false; } - arg =get_ctx_arg_idx (btf, t, off); + arg =btf_ctx_arg_idx (btf, t, off); args = (const struct btf_param *)(t +1 );/* if (t == NULL) Fall back to default BPF prog with * MAX_BPF_FUNC_REG_ARGS u64 arguments.
Signature · 1 line
2.47.1
PATCH v3 7/18 bpf: Generalize finding member offset of struct_ops prog
In reply to Amery HungGeneralize prog_ops_moff() so that we can use it to retrieve a struct_ops program's offset for different ops.
--- include/linux/bpf.h | 1 + kernel/bpf/bpf_struct_ops.c | 13 +++++++++++++ net/ipv4/bpf_tcp_ca.c | 23 ++--------------------- 3 files changed, 16 insertions(+), 21 deletions(-)
include/linux/bpf.h +1 -0
@@ -1892,6 +1892,7 @@ static inline void bpf_module_put(const void *data, struct module *owner)module_put (owner); }int bpf_struct_ops_link_create (union bpf_attr *attr); +u32 bpf_struct_ops_prog_moff (const struct bpf_prog *prog);#ifdef CONFIG_NET /* Define it here to avoid the use of forward declaration */
kernel/bpf/bpf_struct_ops.c +13 -0
@@ -1386,3 +1386,16 @@ void bpf_map_struct_ops_info_fill(struct bpf_map_info *info, struct bpf_map *map info->btf_vmlinux_id =btf_obj_id (st_map->btf); } + +u32 bpf_struct_ops_prog_moff (const struct bpf_prog *prog) +{ +const struct btf_member *m; +const struct btf_type *t; +u32 midx; + + t =btf_type_by_id (prog->aux->attach_btf, prog->aux->attach_btf_id); + midx = prog->expected_attach_type; + m = &btf_type_member (t)[midx]; + +return __btf_member_bit_offset (t, m) /8 ; +}
net/ipv4/bpf_tcp_ca.c +2 -21
@@ -16,7 +16,6 @@ static struct bpf_struct_ops bpf_tcp_congestion_ops;static const struct btf_type *tcp_sock_type;static u32 tcp_sock_id, sock_id; -static const struct btf_type *tcp_congestion_ops_type;static int bpf_tcp_ca_init (struct btf *btf) {
@@ -33,11 +32,6 @@ static int bpf_tcp_ca_init(struct btf *btf) tcp_sock_id = type_id; tcp_sock_type =btf_type_by_id (btf, tcp_sock_id); - type_id =btf_find_by_name_kind (btf,"tcp_congestion_ops" ,BTF_KIND_STRUCT ); -if (type_id <0 ) -return -EINVAL ; - tcp_congestion_ops_type =btf_type_by_id (btf, type_id); -return 0 ; }
@@ -135,19 +129,6 @@ static const struct bpf_func_proto bpf_tcp_send_ack_proto = { .arg2_type = ARG_ANYTHING, }; -static u32 prog_ops_moff (const struct bpf_prog *prog) -{ -const struct btf_member *m; -const struct btf_type *t; -u32 midx; - - midx = prog->expected_attach_type; - t = tcp_congestion_ops_type; - m = &btf_type_member (t)[midx]; - -return __btf_member_bit_offset (t, m) /8 ; -} -static const struct bpf_func_proto *bpf_tcp_ca_get_func_proto (enum bpf_func_id func_id,const struct bpf_prog *prog)
@@ -166,7 +147,7 @@ bpf_tcp_ca_get_func_proto(enum bpf_func_id func_id,* setsockopt() to make further changes which * may potentially allocate new resources. */ -if (prog_ops_moff (prog) != +if (bpf_struct_ops_prog_moff (prog) != offsetof(struct tcp_congestion_ops , release))return &bpf_sk_setsockopt_proto;return NULL ;
@@ -177,7 +158,7 @@ bpf_tcp_ca_get_func_proto(enum bpf_func_id func_id,* The bpf-tcp-cc already has a more powerful way * to read tcp_sock from the PTR_TO_BTF_ID. */ -if (prog_ops_moff (prog) != +if (bpf_struct_ops_prog_moff (prog) != offsetof(struct tcp_congestion_ops , release))return &bpf_sk_getsockopt_proto;return NULL ;
Signature · 1 line
2.47.1
Re: [PATCH bpf-next v3 07/18] bpf: Generalize finding member offset of struct_ops prog
Quoted 5 lines · Amery Hung
Generalize
prog_ops_moff()so that we can use it to retrieve astruct_opsprogram's offset for different ops.Signed-off-by Amery Hung <[email protected]>---
[...]
PATCH v3 8/18 bpf: net_sched: Support implementation of Qdisc_ops in bpf
In reply to Amery HungFrom: Amery Hung <[email protected]>
Enable users to implement a classless qdisc using bpf. The last few patches in this series has prepared struct_ops to support core operators in Qdisc_ops. The recent advancement in bpf such as allocated objects, bpf list and bpf rbtree has also provided powerful and flexible building blocks to realize sophisticated scheduling algorithms. Therefore, in this patch, we start allowing qdisc to be implemented using bpf struct_ops. Users can implement Qdisc_ops.{enqueue, dequeue, init, reset, and .destroy in Qdisc_ops in bpf and register the qdisc dynamically into the kernel.
We do not allow users to attach bpf qdiscs to classful qdiscs. This is to prevent accidentally breaking existings clasful qdiscs if they rely on some data in the child qdisc. This restrication can potentially be lifted in the future. Note that, we still allow bpf qdisc to be attached to mq.
--- net/sched/Kconfig | 12 +++ net/sched/Makefile | 1 + net/sched/bpf_qdisc.c | 210 ++++++++++++++++++++++++++++++++++++++++ net/sched/sch_api.c | 14 ++- net/sched/sch_generic.c | 3 +- 5 files changed, 236 insertions(+), 4 deletions(-) create mode 100644 net/sched/bpf_qdisc.c
net/sched/Kconfig +12 -0
@@ -403,6 +403,18 @@ config NET_SCH_ETS If unsure, say N. +config NET_SCH_BPF + bool "BPF-based Qdisc" + depends on BPF_SYSCALL && BPF_JIT && DEBUG_INFO_BTF + help + This option allows BPF-based queueing disiplines. With BPF struct_ops, + users can implement supported operators in Qdisc_ops using BPF programs. + The queue holding skb can be built with BPF maps or graphs. + + Say Y here if you want to use BPF-based Qdisc. + + If unsure, say N. + menuconfig NET_SCH_DEFAULT bool "Allow override default queue discipline" help
net/sched/Makefile +1 -0
@@ -62,6 +62,7 @@ obj-$(CONFIG_NET_SCH_FQ_PIE) += sch_fq_pie.o obj-$(CONFIG_NET_SCH_CBS) += sch_cbs.o obj-$(CONFIG_NET_SCH_ETF) += sch_etf.o obj-$(CONFIG_NET_SCH_TAPRIO) += sch_taprio.o +obj-$(CONFIG_NET_SCH_BPF) += bpf_qdisc.o obj-$(CONFIG_NET_CLS_U32) += cls_u32.o obj-$(CONFIG_NET_CLS_ROUTE4) += cls_route.o
net/sched/bpf_qdisc.c new file +210 -0
210 lines not on this page · raw message
net/sched/sch_api.c +11 -3
@@ -25,6 +25,7 @@#include <linux/hrtimer.h> #include <linux/slab.h> #include <linux/hashtable.h> +#include <linux/bpf.h> #include <net/net_namespace.h> #include <net/sock.h>
@@ -358,7 +359,7 @@ static struct Qdisc_ops *qdisc_lookup_ops(struct nlattr *kind)read_lock (&qdisc_mod_lock);for (q = qdisc_base; q; q = q->next) {if (nla_strcmp (kind, q->id) ==0 ) { -if (!try_module_get (q->owner)) +if (!bpf_try_module_get (q, q->owner)) q =NULL ;break ; }
@@ -1200,6 +1201,13 @@ static int qdisc_graft(struct net_device *dev, struct Qdisc *parent,return -EINVAL ; } +if (new && + !(parent->flags &TCQ_F_MQROOT ) && + new->ops->owner ==BPF_MODULE_OWNER ) { +NL_SET_ERR_MSG (extack,"BPF qdisc not supported on a non root" ); +return -EINVAL ; + } +if (new && !(parent->flags &TCQ_F_MQROOT ) &&rcu_access_pointer (new->stab)) {
@@ -1287,7 +1295,7 @@ static struct Qdisc *qdisc_create(struct net_device *dev,/* We will try again qdisc_lookup_ops, * so don't keep a reference. */ -module_put (ops->owner); +bpf_module_put (ops, ops->owner); err = -EAGAIN ; goto err_out; }
@@ -1398,7 +1406,7 @@ static struct Qdisc *qdisc_create(struct net_device *dev,netdev_put (dev, &sch->dev_tracker);qdisc_free (sch); err_out2: -module_put (ops->owner); +bpf_module_put (ops, ops->owner); err_out: *errp = err;return NULL ;
net/sched/sch_generic.c +2 -1
@@ -24,6 +24,7 @@#include <linux/if_vlan.h> #include <linux/skb_array.h> #include <linux/if_macvlan.h> +#include <linux/bpf.h> #include <net/sch_generic.h> #include <net/pkt_sched.h> #include <net/dst.h>
@@ -1078,7 +1079,7 @@ static void __qdisc_destroy(struct Qdisc *qdisc) ops->destroy (qdisc);lockdep_unregister_key (&qdisc->root_lock_key); -module_put (ops->owner); +bpf_module_put (ops, ops->owner);netdev_put (dev, &qdisc->dev_tracker);trace_qdisc_destroy (qdisc);
Signature · 1 line
2.47.1
Jakub Kicinski
maintainerRe: [PATCH bpf-next v3 08/18] bpf: net_sched: Support implementation of Qdisc_ops in bpf
net/sched/sch_api.c static int qdisc_graft(struct net_device *dev, struct Qdisc *parent, · line 1209
Quoted 6 lines · On Fri, 31 Jan 2025 11:28:47 -0800 Amery Hung wrote:
net/sched/sch_api.c +6
-0+if (new && + !(parent->flags &TCQ_F_MQROOT ) && + new->ops->owner ==BPF_MODULE_OWNER ) { +NL_SET_ERR_MSG (extack,"BPF qdisc not supported on a non root" ); +return -EINVAL ; + }
This check should live in bpf_qdisc.c
Re: [PATCH bpf-next v3 08/18] bpf: net_sched: Support implementation of Qdisc_ops in bpf
Quoted 10 lines · Jakub Kicinski
net/sched/sch_api.c static int qdisc_graft(struct net_device *dev, struct Qdisc *parent, · line 1209
Quoted 6 lines · On Fri, 31 Jan 2025 11:28:47 -0800 Amery Hung wrote:
net/sched/sch_api.c +6
-0+if (new && + !(parent->flags &TCQ_F_MQROOT ) && + new->ops->owner ==BPF_MODULE_OWNER ) { +NL_SET_ERR_MSG (extack,"BPF qdisc not supported on a non root" ); +return -EINVAL ; + }This check should live in
bpf_qdisc.c
Might be a dumb question, but could you explain why this is preferred?
I can certainly do the check in Qdisc_ops::init instead though.
Thanks,
Amery
Jakub Kicinski
maintainerRe: [PATCH bpf-next v3 08/18] bpf: net_sched: Support implementation of Qdisc_ops in bpf
Quoted 9 lines · On Tue, 4 Feb 2025 15:21:27 -0800 Amery Hung wrote:
Quoted 8 lines · Jakub Kicinski
Quoted 6 lines · On Fri, 31 Jan 2025 11:28:47 -0800 Amery Hung wrote:
net/sched/sch_api.c +6
-0+if (new && + !(parent->flags &TCQ_F_MQROOT ) && + new->ops->owner ==BPF_MODULE_OWNER ) { +NL_SET_ERR_MSG (extack,"BPF qdisc not supported on a non root" ); +return -EINVAL ; + }
Quoted 5 lines
Quoted 1 line
This check should live in
bpf_qdisc.cMight be a dumb question, but could you explain why this is preferred?
I can certainly do the check in
Qdisc_ops::initinstead though.
Basic SW abstractions, this is the generic layer, bpf_qdisc is just one implementation that plugs into it.
Re: [PATCH bpf-next v3 08/18] bpf: net_sched: Support implementation of Qdisc_ops in bpf
Quoted 11 lines · Jakub Kicinski
Quoted 9 lines · On Tue, 4 Feb 2025 15:21:27 -0800 Amery Hung wrote:
Quoted 8 lines · Jakub Kicinski
Quoted 6 lines · On Fri, 31 Jan 2025 11:28:47 -0800 Amery Hung wrote:
net/sched/sch_api.c +6
-0+if (new && + !(parent->flags &TCQ_F_MQROOT ) && + new->ops->owner ==BPF_MODULE_OWNER ) { +NL_SET_ERR_MSG (extack,"BPF qdisc not supported on a non root" ); +return -EINVAL ; + }
Quoted 8 lines
Quoted 5 lines
Quoted 1 line
This check should live in
bpf_qdisc.cMight be a dumb question, but could you explain why this is preferred?
I can certainly do the check in
Qdisc_ops::initinstead though.Basic SW abstractions, this is the generic layer,
bpf_qdiscis just one implementation that plugs into it.
Got it. Thanks for the clarification.
PATCH v3 9/18 bpf: net_sched: Add basic bpf qdisc kfuncs
In reply to Amery HungFrom: Amery Hung <[email protected]>
Add basic kfuncs for working on skb in qdisc.
Both bpf_qdisc_skb_drop() and bpf_kfree_skb() can be used to release a reference to an skb. However, bpf_qdisc_skb_drop() can only be called in .enqueue where a to_free skb list is available from kernel to defer the release. bpf_kfree_skb() should be used elsewhere. It is also used in bpf_obj_free_fields() when cleaning up skb in maps and collections.
bpf_skb_get_hash() returns the flow hash of an skb, which can be used to build flow-based queueing algorithms.
Finally, allow users to create read-only dynptr via bpf_dynptr_from_skb().
--- include/linux/bpf.h | 1 + kernel/bpf/bpf_struct_ops.c | 2 + net/sched/bpf_qdisc.c | 93 ++++++++++++++++++++++++++++++++++++- 3 files changed, 95 insertions(+), 1 deletion(-)
include/linux/bpf.h +1 -0
@@ -1810,6 +1810,7 @@ struct bpf_struct_ops {void *cfi_stubs;struct module *owner;const char *name; +const struct btf_type *type;struct btf_func_model func_models[BPF_STRUCT_OPS_MAX_NR_MEMBERS ]; };
kernel/bpf/bpf_struct_ops.c +2 -0
@@ -460,6 +460,8 @@ int bpf_struct_ops_desc_init(struct bpf_struct_ops_desc *st_ops_desc, goto errout; } + st_ops->type = t; +return 0 ; errout:
net/sched/bpf_qdisc.c +92 -1
106 lines not on this page · raw message
Signature · 1 line
2.47.1
PATCH v3 10/18 bpf: Search and add kfuncs in struct_ops prologue and epilogue
In reply to Amery HungFrom: Amery Hung <[email protected]>
Currently, add_kfunc_call() is only invoked once before the main verification loop. Therefore, the verifier could not find the bpf_kfunc_btf_tab of a new kfunc call which is not seen in user defined struct_ops operators but introduced in gen_prologue or gen_epilogue during do_misc_fixup(). Fix this by searching kfuncs in the patching instruction buffer and add them to prog->aux->kfunc_tab.
--- kernel/bpf/verifier.c | 25 ++++++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-)
kernel/bpf/verifier.c +24 -1
@@ -3215,6 +3215,21 @@ bpf_jit_find_kfunc_model(const struct bpf_prog *prog,return res ? &res->func_model :NULL ; } +static int add_kfunc_in_insns (struct bpf_verifier_env *env, +struct bpf_insn *insn,int cnt) +{ +int i, ret; + +for (i =0 ; i < cnt; i++, insn++) { +if (bpf_pseudo_kfunc_call (insn)) { + ret =add_kfunc_call (env, insn->imm, insn->off); +if (ret <0 ) +return ret; + } + } +return 0 ; +} +static int add_subprog_and_kfunc (struct bpf_verifier_env *env) {struct bpf_subprog_info *subprog = env->subprog_info;
@@ -20368,7 +20383,7 @@ static int convert_ctx_accesses(struct bpf_verifier_env *env) {struct bpf_subprog_info *subprogs = env->subprog_info;const struct bpf_verifier_ops *ops = env->ops; -int i, cnt, size, ctx_field_size, delta =0 , epilogue_cnt =0 ; +int i, cnt, size, ctx_field_size, ret, delta =0 , epilogue_cnt =0 ;const int insn_cnt = env->prog->len;struct bpf_insn *epilogue_buf = env->epilogue_buf;struct bpf_insn *insn_buf = env->insn_buf;
@@ -20397,6 +20412,10 @@ static int convert_ctx_accesses(struct bpf_verifier_env *env)return -ENOMEM ; env->prog = new_prog; delta += cnt -1 ; + + ret =add_kfunc_in_insns (env, epilogue_buf, epilogue_cnt -1 ); +if (ret <0 ) +return ret; } }
@@ -20417,6 +20436,10 @@ static int convert_ctx_accesses(struct bpf_verifier_env *env) env->prog = new_prog; delta += cnt -1 ; + + ret =add_kfunc_in_insns (env, insn_buf, cnt -1 ); +if (ret <0 ) +return ret; } }
Signature · 1 line
2.47.1
Re: [PATCH bpf-next v3 10/18] bpf: Search and add kfuncs in struct_ops prologue and epilogue
Quoted 11 lines · Amery Hung
From: Amery Hung <[email protected]>
Currently,
add_kfunc_call()is only invoked once before the main verification loop. Therefore, the verifier could not find thebpf_kfunc_btf_tabof a new kfunc call which is not seen in user definedstruct_opsoperators but introduced ingen_prologueorgen_epilogueduringdo_misc_fixup(). Fix this by searching kfuncs in the patching instruction buffer and add them toprog->aux->kfunc_tab.Signed-off-by Amery Hung <[email protected]>---
[...]
kernel/bpf/verifier.c static int convert_ctx_accesses(struct bpf_verifier_env *env) · line 20416
Quoted 6 lines
kernel/bpf/verifier.c +2
-0@@ -20397,6 +20412,10 @@ static int convert_ctx_accesses(struct bpf_verifier_env *env)return -ENOMEM ; env->prog = new_prog; delta += cnt -1 ; + + ret =add_kfunc_in_insns (env, epilogue_buf, epilogue_cnt -1 );
Rant: the -1 here is a bit confusing, it is second time I forget that
last instruction of the epilogue has to be some kind of control flow, and last instruction of the prologue has to be first instruction of the program.
Quoted 5 lines
kernel/bpf/verifier.c +2
-0+if (ret <0 ) +return ret; } }
[...]
PATCH v3 11/18 bpf: net_sched: Add a qdisc watchdog timer
In reply to Amery HungFrom: Amery Hung <[email protected]>
Add a watchdog timer to bpf qdisc. The watchdog can be used to schedule the execution of qdisc through kfunc, bpf_qdisc_schedule(). It can be useful for building traffic shaping scheduling algorithm, where the time the next packet will be dequeued is known.
--- include/linux/filter.h | 10 +++++ net/sched/bpf_qdisc.c | 92 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 102 insertions(+)
include/linux/filter.h +10 -0
@@ -469,6 +469,16 @@ static inline bool insn_is_cast_user(const struct bpf_insn *insn) .off = 0, \ .imm = BPF_CALL_IMM(FUNC) }) +/* Kfunc call */ + +#define BPF_CALL_KFUNC (OFF ,IMM ) \ + ((struct bpf_insn) { \ + .code = BPF_JMP | BPF_CALL, \ + .dst_reg = 0, \ + .src_reg = BPF_PSEUDO_KFUNC_CALL, \ + .off = OFF, \ + .imm = IMM }) +/* Raw code statement block */ #define BPF_RAW_INSN (CODE ,DST ,SRC ,OFF ,IMM ) \
net/sched/bpf_qdisc.c +92 -0
142 lines not on this page · raw message
Signature · 1 line
2.47.1
PATCH v3 12/18 bpf: net_sched: Support updating bstats
In reply to Amery HungFrom: Amery Hung <[email protected]>
Add a kfunc to update Qdisc bstats when an skb is dequeued. The kfunc is only available in .dequeue programs.
--- net/sched/bpf_qdisc.c | 11 +++++++++++ 1 file changed, 11 insertions(+)
net/sched/bpf_qdisc.c +11 -0
@@ -213,6 +213,15 @@ __bpf_kfunc void bpf_qdisc_reset_destroy_epilogue(struct Qdisc *sch)qdisc_watchdog_cancel (&q->watchdog); } +/* bpf_qdisc_bstats_update - Update Qdisc basic statistics +* @sch: The qdisc from which an skb is dequeued. +* @skb: The skb to be dequeued. +*/ +__bpf_kfunc void bpf_qdisc_bstats_update(struct Qdisc *sch, const struct sk_buff *skb) +{ +bstats_update (&sch->bstats, skb); +} +__bpf_kfunc_end_defs ();BTF_KFUNCS_START (qdisc_kfunc_ids)
@@ -223,6 +232,7 @@ BTF_ID_FLAGS(func, bpf_dynptr_from_skb, KF_TRUSTED_ARGS) BTF_ID_FLAGS(func, bpf_qdisc_watchdog_schedule, KF_TRUSTED_ARGS) BTF_ID_FLAGS(func, bpf_qdisc_init_prologue, KF_TRUSTED_ARGS) BTF_ID_FLAGS(func, bpf_qdisc_reset_destroy_epilogue, KF_TRUSTED_ARGS) +BTF_ID_FLAGS(func, bpf_qdisc_bstats_update, KF_TRUSTED_ARGS)BTF_KFUNCS_END (qdisc_kfunc_ids)BTF_SET_START (qdisc_common_kfunc_set)
@@ -238,6 +248,7 @@ BTF_SET_END(qdisc_enqueue_kfunc_set) BTF_SET_START(qdisc_dequeue_kfunc_set) BTF_ID(func, bpf_qdisc_watchdog_schedule) +BTF_ID(func, bpf_qdisc_bstats_update) BTF_SET_END(qdisc_dequeue_kfunc_set) static int bpf_qdisc_kfunc_filter(const struct bpf_prog *prog, u32 kfunc_id)
Signature · 1 line
2.47.1
PATCH v3 13/18 bpf: net_sched: Support updating qstats
In reply to Amery HungFrom: Amery Hung <[email protected]>
Allow bpf qdisc programs to update Qdisc qstats directly with btf struct access.
--- net/sched/bpf_qdisc.c | 53 ++++++++++++++++++++++++++++++++++++------- 1 file changed, 45 insertions(+), 8 deletions(-)
net/sched/bpf_qdisc.c +45 -8
@@ -36,6 +36,7 @@ bpf_qdisc_get_func_proto(enum bpf_func_id func_id, } } +BTF_ID_LIST_SINGLE(bpf_qdisc_ids, struct, Qdisc) BTF_ID_LIST_SINGLE(bpf_sk_buff_ids, struct, sk_buff)BTF_ID_LIST_SINGLE (bpf_sk_buff_ptr_ids, struct, bpf_sk_buff_ptr)
@@ -60,20 +61,37 @@ static bool bpf_qdisc_is_valid_access(int off, int size,return bpf_tracing_btf_ctx_access (off, size, type, prog, info); } -static int bpf_qdisc_btf_struct_access (struct bpf_verifier_log *log, -const struct bpf_reg_state *reg, -int off,int size) +static int bpf_qdisc_qdisc_access (struct bpf_verifier_log *log, +const struct bpf_reg_state *reg, +int off,int size) { -const struct btf_type *t, *skbt;size_t end; - skbt =btf_type_by_id (reg->btf, bpf_sk_buff_ids[0 ]); - t =btf_type_by_id (reg->btf, reg->btf_id); -if (t != skbt) { -bpf_log (log,"only read is supported\n" ); +switch (off) { + case offsetof(struct Qdisc, qstats) ... offsetofend(struct Qdisc, qstats) - 1: + end = offsetofend(struct Qdisc, qstats); +break ; +default : +bpf_log (log,"no write support to Qdisc at off %d\n" , off); +return -EACCES ; + } + +if (off + size > end) { +bpf_log (log, +"write access at off %d with size %d beyond the member of Qdisc ended at %zu\n" , + off, size, end);return -EACCES ; } +return 0 ; +} + +static int bpf_qdisc_sk_buff_access (struct bpf_verifier_log *log, +const struct bpf_reg_state *reg, +int off,int size) +{ +size_t end; +switch (off) {case offsetof(struct sk_buff , tstamp): end = offsetofend(struct sk_buff, tstamp);
@@ -115,6 +133,25 @@ static int bpf_qdisc_btf_struct_access(struct bpf_verifier_log *log,return 0 ; } +static int bpf_qdisc_btf_struct_access (struct bpf_verifier_log *log, +const struct bpf_reg_state *reg, +int off,int size) +{ +const struct btf_type *t, *skbt, *qdisct; + + skbt =btf_type_by_id (reg->btf, bpf_sk_buff_ids[0 ]); + qdisct =btf_type_by_id (reg->btf, bpf_qdisc_ids[0 ]); + t =btf_type_by_id (reg->btf, reg->btf_id); + +if (t == skbt) +return bpf_qdisc_sk_buff_access (log, reg, off, size); +else if (t == qdisct) +return bpf_qdisc_qdisc_access (log, reg, off, size); + +bpf_log (log,"only read is supported\n" ); +return -EACCES ; +} +BTF_ID_LIST (bpf_qdisc_init_prologue_ids)BTF_ID (func, bpf_qdisc_init_prologue)
Signature · 1 line
2.47.1
PATCH v3 14/18 bpf: net_sched: Allow writing to more Qdisc members
In reply to Amery HungFrom: Amery Hung <[email protected]>
Allow bpf qdisc to write to Qdisc->limit and Qdisc->q.qlen.
--- net/sched/bpf_qdisc.c | 6 ++++++ 1 file changed, 6 insertions(+)
net/sched/bpf_qdisc.c +6 -0
@@ -68,6 +68,12 @@ static int bpf_qdisc_qdisc_access(struct bpf_verifier_log *log,size_t end;switch (off) { +case offsetof(struct Qdisc , limit): + end = offsetofend(struct Qdisc, limit); +break ; +case offsetof(struct Qdisc , q) + offsetof(struct qdisc_skb_head , qlen): + end = offsetof(struct Qdisc, q) + offsetofend(struct qdisc_skb_head, qlen); +break ; case offsetof(struct Qdisc, qstats) ... offsetofend(struct Qdisc, qstats) - 1: end = offsetofend(struct Qdisc, qstats);break ;
Signature · 1 line
2.47.1
PATCH v3 15/18 libbpf: Support creating and destroying qdisc
In reply to Amery HungFrom: Amery Hung <[email protected]>
Extend struct bpf_tc_hook with handle, qdisc name and a new attach type, BPF_TC_QDISC, to allow users to add or remove any qdisc specified in addition to clsact.
--- tools/lib/bpf/libbpf.h | 5 ++++- tools/lib/bpf/netlink.c | 20 +++++++++++++++++--- 2 files changed, 21 insertions(+), 4 deletions(-)
tools/lib/bpf/libbpf.h +4 -1
@@ -1270,6 +1270,7 @@ enum bpf_tc_attach_point {BPF_TC_INGRESS =1 <<0 ,BPF_TC_EGRESS =1 <<1 ,BPF_TC_CUSTOM =1 <<2 , + BPF_TC_QDISC = 1 << 3, };#define BPF_TC_PARENT (a, b) \
@@ -1284,9 +1285,11 @@ struct bpf_tc_hook {int ifindex;enum bpf_tc_attach_point attach_point;__u32 parent; +__u32 handle; +const char *qdisc; size_t :0; }; -#define bpf_tc_hook__last_field parent +#define bpf_tc_hook__last_field qdiscstruct bpf_tc_opts {size_t sz;
tools/lib/bpf/netlink.c +17 -3
@@ -529,9 +529,9 @@ int bpf_xdp_query_id(int ifindex, int flags, __u32 *prog_id) } -typedef int (*qdisc_config_t )(struct libbpf_nla_req *req); +typedef int (*qdisc_config_t )(struct libbpf_nla_req *req,const struct bpf_tc_hook *hook); -static int clsact_config (struct libbpf_nla_req *req) +static int clsact_config (struct libbpf_nla_req *req,const struct bpf_tc_hook *hook) { req->tc.tcm_parent =TC_H_CLSACT ; req->tc.tcm_handle =TC_H_MAKE (TC_H_CLSACT ,0 );
@@ -539,6 +539,16 @@ static int clsact_config(struct libbpf_nla_req *req)return nlattr_add (req,TCA_KIND ,"clsact" ,sizeof ("clsact" )); } +static int qdisc_config (struct libbpf_nla_req *req,const struct bpf_tc_hook *hook) +{ +const char *qdisc =OPTS_GET (hook, qdisc,NULL ); + + req->tc.tcm_parent =OPTS_GET (hook, parent,TC_H_ROOT ); + req->tc.tcm_handle =OPTS_GET (hook, handle,0 ); + +return nlattr_add (req,TCA_KIND , qdisc,strlen (qdisc) +1 ); +} +static int attach_point_to_config (struct bpf_tc_hook *hook,qdisc_config_t *config) {
@@ -552,6 +562,9 @@ static int attach_point_to_config(struct bpf_tc_hook *hook,return 0 ;case BPF_TC_CUSTOM :return -EOPNOTSUPP ; +case BPF_TC_QDISC : + *config = &qdisc_config; +return 0 ;default :return -EINVAL ; }
@@ -596,7 +609,7 @@ static int tc_qdisc_modify(struct bpf_tc_hook *hook, int cmd, int flags) req.tc.tcm_family =AF_UNSPEC ; req.tc.tcm_ifindex =OPTS_GET (hook, ifindex,0 ); - ret =config (&req); + ret =config (&req, hook);if (ret <0 )return ret;
@@ -639,6 +652,7 @@ int bpf_tc_hook_destroy(struct bpf_tc_hook *hook)case BPF_TC_INGRESS :case BPF_TC_EGRESS :return libbpf_err (__bpf_tc_detach (hook,NULL , true)); +case BPF_TC_QDISC :case BPF_TC_INGRESS |BPF_TC_EGRESS :return libbpf_err (tc_qdisc_delete (hook));case BPF_TC_CUSTOM :
Signature · 1 line
2.47.1
PATCH v3 16/18 selftests/bpf: Add a basic fifo qdisc test
In reply to Amery HungFrom: Amery Hung <[email protected]>
This selftest includes a bare minimum fifo qdisc, which simply enqueues sk_buffs into the back of a bpf list and dequeues from the front of the list.
--- tools/testing/selftests/bpf/config | 1 + .../selftests/bpf/prog_tests/bpf_qdisc.c | 79 ++++++++++++ .../selftests/bpf/progs/bpf_qdisc_common.h | 27 ++++ .../selftests/bpf/progs/bpf_qdisc_fifo.c | 117 ++++++++++++++++++ 4 files changed, 224 insertions(+) create mode 100644 tools/testing/selftests/bpf/prog_tests/bpf_qdisc.c create mode 100644 tools/testing/selftests/bpf/progs/bpf_qdisc_common.h create mode 100644 tools/testing/selftests/bpf/progs/bpf_qdisc_fifo.c
tools/testing/selftests/bpf/config +1 -0
@@ -71,6 +71,7 @@ CONFIG_NET_IPGRE=y CONFIG_NET_IPGRE_DEMUX=y CONFIG_NET_IPIP=y CONFIG_NET_MPLS_GSO=y +CONFIG_NET_SCH_BPF=y CONFIG_NET_SCH_FQ=y CONFIG_NET_SCH_INGRESS=y CONFIG_NET_SCHED=y
tools/testing/selftests/bpf/prog_tests/bpf_qdisc.c new file +79 -0
79 lines not on this page · raw message
tools/testing/selftests/bpf/progs/bpf_qdisc_common.h new file +27 -0
@@ -0,0 +1,27 @@ +#ifndef _BPF_QDISC_COMMON_H +#define _BPF_QDISC_COMMON_H + +#define NET_XMIT_SUCCESS 0x00 +#define NET_XMIT_DROP 0x01/* skb dropped */ +#define NET_XMIT_CN 0x02/* congestion notification */ + +#define TC_PRIO_CONTROL 7 +#define TC_PRIO_MAX 15 + +u32 bpf_skb_get_hash(struct sk_buff *p) __ksym; +void bpf_kfree_skb(struct sk_buff *p) __ksym; +void bpf_qdisc_skb_drop(struct sk_buff *p, struct bpf_sk_buff_ptr *to_free) __ksym; +void bpf_qdisc_watchdog_schedule(struct Qdisc *sch, u64 expire, u64 delta_ns) __ksym; +void bpf_qdisc_bstats_update(struct Qdisc *sch, const struct sk_buff *skb) __ksym; + +static struct qdisc_skb_cb *qdisc_skb_cb (const struct sk_buff *skb) +{ +return (struct qdisc_skb_cb *)skb->cb; +} + +static inline unsigned int qdisc_pkt_len (const struct sk_buff *skb) +{ +return qdisc_skb_cb (skb)->pkt_len; +} + +#endif
tools/testing/selftests/bpf/progs/bpf_qdisc_fifo.c new file +117 -0
117 lines not on this page · raw message
Signature · 1 line
2.47.1
PATCH v3 17/18 selftests/bpf: Add a bpf fq qdisc to selftest
In reply to Amery HungFrom: Amery Hung <[email protected]>
This test implements a more sophisticated qdisc using bpf. The bpf fair- queueing (fq) qdisc gives each flow an equal chance to transmit data. It also respects the timestamp of skb for rate limiting.
--- .../selftests/bpf/prog_tests/bpf_qdisc.c | 24 + .../selftests/bpf/progs/bpf_qdisc_fq.c | 720 ++++++++++++++++++ 2 files changed, 744 insertions(+) create mode 100644 tools/testing/selftests/bpf/progs/bpf_qdisc_fq.c
tools/testing/selftests/bpf/prog_tests/bpf_qdisc.c +24 -0
@@ -4,6 +4,7 @@#include "network_helpers.h" #include "bpf_qdisc_fifo.skel.h" +#include "bpf_qdisc_fq.skel.h" #define LO_IFINDEX 1
@@ -64,6 +65,27 @@ static void test_fifo(void)bpf_qdisc_fifo__destroy (fifo_skel); } +static void test_fq (void ) +{ +struct bpf_qdisc_fq *fq_skel; +struct bpf_link *link; + + fq_skel =bpf_qdisc_fq__open_and_load (); +if (!ASSERT_OK_PTR (fq_skel,"bpf_qdisc_fq__open_and_load" )) +return ; + + link =bpf_map__attach_struct_ops (fq_skel->maps.fq); +if (!ASSERT_OK_PTR (link,"bpf_map__attach_struct_ops" )) { +bpf_qdisc_fq__destroy (fq_skel); +return ; + } + +do_test ("bpf_fq" ); + +bpf_link__destroy (link); +bpf_qdisc_fq__destroy (fq_skel); +} +void test_bpf_qdisc (void ) {struct netns_obj *netns;
@@ -74,6 +96,8 @@ void test_bpf_qdisc(void)if (test__start_subtest ("fifo" ))test_fifo (); +if (test__start_subtest ("fq" )) +test_fq ();netns_free (netns); }
tools/testing/selftests/bpf/progs/bpf_qdisc_fq.c new file +720 -0
720 lines not on this page · raw message
Signature · 1 line
2.47.1
PATCH v3 18/18 selftests/bpf: Test attaching bpf qdisc to mq and non root
In reply to Amery HungUntil we are certain that existing classful qdiscs work with bpf qdisc, make sure we don't allow attaching a bpf qdisc to non root. Meanwhile, attaching to mq is allowed.
--- tools/testing/selftests/bpf/config | 1 + .../selftests/bpf/prog_tests/bpf_qdisc.c | 111 +++++++++++++++++- 2 files changed, 110 insertions(+), 2 deletions(-)
tools/testing/selftests/bpf/config +1 -0
@@ -74,6 +74,7 @@ CONFIG_NET_MPLS_GSO=y CONFIG_NET_SCH_BPF=y CONFIG_NET_SCH_FQ=y CONFIG_NET_SCH_INGRESS=y +CONFIG_NET_SCH_HTB=y CONFIG_NET_SCHED=y CONFIG_NETDEVSIM=y CONFIG_NETFILTER=y
tools/testing/selftests/bpf/prog_tests/bpf_qdisc.c +109 -2
127 lines not on this page · raw message
Signature · 1 line
2.47.1
Re: [PATCH bpf-next v3 18/18] selftests/bpf: Test attaching bpf qdisc to mq and non root
Quoted 34 lines · Amery Hung
Until we are certain that existing classful qdiscs work with bpf qdisc, make sure we don't allow attaching a bpf qdisc to non root. Meanwhile, attaching to mq is allowed.
Signed-off-by Amery Hung <[email protected]>--- tools/testing/selftests/bpf/config | 1 + .../selftests/bpf/prog_tests/bpf_qdisc.c | 111 +++++++++++++++++- 2 files changed, 110 insertions(+), 2 deletions(-)tools/testing/selftests/bpf/config +1
-0@@ -74,6 +74,7 @@ CONFIG_NET_MPLS_GSO=y CONFIG_NET_SCH_BPF=y CONFIG_NET_SCH_FQ=y CONFIG_NET_SCH_INGRESS=y +CONFIG_NET_SCH_HTB=y CONFIG_NET_SCHED=y CONFIG_NETDEVSIM=y CONFIG_NETFILTER=ytools/testing/selftests/bpf/prog_tests/bpf_qdisc.c +3
-0@@ -86,18 +86,125 @@ static void test_fq(void)bpf_qdisc_fq__destroy (fq_skel); } +static int netdevsim_write_cmd (const char *path,const char *cmd) +{ +FILE *fp;
tools/testing/selftests/bpf/prog_tests/bpf_qdisc.c static void test_fq(void) · line 101
Quoted 10 lines
tools/testing/selftests/bpf/prog_tests/bpf_qdisc.c +10
-0+ + fp =fopen (path,"w" ); +if (!ASSERT_OK_PTR (fp,"write_netdevsim_cmd" )) +return -errno; + +fprintf (fp, cmd); +fclose (fp); +return 0 ; +} +
I will replace netdevsim with veth for attaching mq. The function above that failed to compile in CI will also go.
Quoted 114 lines
tools/testing/selftests/bpf/prog_tests/bpf_qdisc.c +96
-3+static void test_qdisc_attach_to_mq (void ) +{ +DECLARE_LIBBPF_OPTS (bpf_tc_hook, hook, + .attach_point = BPF_TC_QDISC, + .parent = 0x00010001, + .handle = 0x8000000, + .qdisc = "bpf_fifo"); +struct bpf_qdisc_fifo *fifo_skel; +struct bpf_link *link; +int err; + + hook.ifindex =if_nametoindex ("eni1np1" ); +if (!ASSERT_NEQ (hook.ifindex,0 ,"if_nametoindex" )) +return ; + + fifo_skel =bpf_qdisc_fifo__open_and_load (); +if (!ASSERT_OK_PTR (fifo_skel,"bpf_qdisc_fifo__open_and_load" )) +return ; + + link =bpf_map__attach_struct_ops (fifo_skel->maps.fifo); +if (!ASSERT_OK_PTR (link,"bpf_map__attach_struct_ops" )) { +bpf_qdisc_fifo__destroy (fifo_skel); +return ; + } + +ASSERT_OK (system ("tc qdisc add dev eni1np1 root handle 1: mq" ),"create mq" ); + + err =bpf_tc_hook_create (&hook); +ASSERT_OK (err,"attach qdisc" ); + +bpf_tc_hook_destroy (&hook); + +ASSERT_OK (system ("tc qdisc delete dev eni1np1 root mq" ),"delete mq" ); + +bpf_link__destroy (link); +bpf_qdisc_fifo__destroy (fifo_skel); +} + +static void test_qdisc_attach_to_non_root (void ) +{ + DECLARE_LIBBPF_OPTS(bpf_tc_hook, hook, .ifindex = LO_IFINDEX, + .attach_point = BPF_TC_QDISC, + .parent = 0x00010001, + .handle = 0x8000000, + .qdisc = "bpf_fifo"); +struct bpf_qdisc_fifo *fifo_skel; +struct bpf_link *link; +int err; + + fifo_skel =bpf_qdisc_fifo__open_and_load (); +if (!ASSERT_OK_PTR (fifo_skel,"bpf_qdisc_fifo__open_and_load" )) +return ; + + link =bpf_map__attach_struct_ops (fifo_skel->maps.fifo); +if (!ASSERT_OK_PTR (link,"bpf_map__attach_struct_ops" )) { +bpf_qdisc_fifo__destroy (fifo_skel); +return ; + } + +ASSERT_OK (system ("tc qdisc add dev lo root handle 1: htb" ),"create htb" ); +ASSERT_OK (system ("tc class add dev lo parent 1: classid 1:1 htb rate 75Kbit" ),"create htb class" ); + + err =bpf_tc_hook_create (&hook); +ASSERT_ERR (err,"attach qdisc" ); + +bpf_tc_hook_destroy (&hook); + +ASSERT_OK (system ("tc qdisc delete dev lo root htb" ),"delete htb" ); + +bpf_link__destroy (link); +bpf_qdisc_fifo__destroy (fifo_skel); +} +void test_bpf_qdisc (void ) { +struct nstoken *nstoken =NULL ;struct netns_obj *netns; +int err; - netns =netns_new ("bpf_qdisc_ns" , true); + netns =netns_new ("bpf_qdisc_ns" , false);if (!ASSERT_OK_PTR (netns,"netns_new" ))return ; + err =netdevsim_write_cmd ("/sys/bus/netdevsim/new_device" ,"1 1 4" ); +if (!ASSERT_OK (err,"create netdevsim" )) { +netns_free (netns); +return ; + } + +ASSERT_OK (system ("ip link set eni1np1 netns bpf_qdisc_ns" ),"ip link set netdevsim" ); + + nstoken =open_netns ("bpf_qdisc_ns" ); +if (!ASSERT_OK_PTR (nstoken,"open_netns" )) + goto out; +if (test__start_subtest ("fifo" ))test_fifo ();if (test__start_subtest ("fq" ))test_fq (); - +if (test__start_subtest ("attach to mq" )) +test_qdisc_attach_to_mq (); +if (test__start_subtest ("attach to non root" )) +test_qdisc_attach_to_non_root (); + +out: + err =netdevsim_write_cmd ("/sys/bus/netdevsim/del_device" ,"1" ); +ASSERT_OK (err,"delete netdevsim" );netns_free (netns); } --2.47.1
Eduard Zingerman
maintainerRe: [PATCH bpf-next v3 18/18] selftests/bpf: Test attaching bpf qdisc to mq and non root
In reply to Amery HungOn Fri, 2025-01-31 at 11:28 -0800, Amery Hung wrote:
[...]
Quoted 7 lines
tools/testing/selftests/bpf/prog_tests/bpf_qdisc.c +0
-0@@ -86,18 +86,125 @@ static void test_fq(void)bpf_qdisc_fq__destroy (fq_skel); }
tools/testing/selftests/bpf/prog_tests/bpf_qdisc.c static void test_fq(void) · line 97
Quoted 10 lines
tools/testing/selftests/bpf/prog_tests/bpf_qdisc.c +9
-0+static int netdevsim_write_cmd (const char *path,const char *cmd) +{ +FILE *fp; + + fp =fopen (path,"w" ); +if (!ASSERT_OK_PTR (fp,"write_netdevsim_cmd" )) +return -errno; + +fprintf (fp, cmd);
I get the following error message when compiling these tests using clang 19.1.7:
<kernel>/tools/testing/selftests/bpf/prog_tests/bpf_qdisc.c:97:14: error: format string is not a string literal (potentially insecure) [-Werror,-Wformat-security]
97 | fprintf(fp, cmd);
| ^~~
<kernel>/tools/testing/selftests/bpf/prog_tests/bpf_qdisc.c:97:14: note: treat the string as an argument to avoid this
97 | fprintf(fp, cmd);
| ^
| Quoted 4 lines
tools/testing/selftests/bpf/prog_tests/bpf_qdisc.c +4
-0+fclose (fp); +return 0 ; +} +
[...]
Re: [PATCH bpf-next v3 18/18] selftests/bpf: Test attaching bpf qdisc to mq and non root
Quoted 26 lines · Eduard Zingerman
On Fri, 2025-01-31 at 11:28 -0800, Amery Hung wrote:
[...]
Quoted 7 lines
tools/testing/selftests/bpf/prog_tests/bpf_qdisc.c +0
-0@@ -86,18 +86,125 @@ static void test_fq(void)bpf_qdisc_fq__destroy (fq_skel); }tools/testing/selftests/bpf/prog_tests/bpf_qdisc.c static void test_fq(void) · line 97
Quoted 10 lines
tools/testing/selftests/bpf/prog_tests/bpf_qdisc.c +9
-0+static int netdevsim_write_cmd (const char *path,const char *cmd) +{ +FILE *fp; + + fp =fopen (path,"w" ); +if (!ASSERT_OK_PTR (fp,"write_netdevsim_cmd" )) +return -errno; + +fprintf (fp, cmd);I get the following error message when compiling these tests using clang 19.1.7:
Quoted 8 lines
<kernel>/tools/testing/selftests/bpf/prog_tests/bpf_qdisc.c:97:14: error: format string is not a string literal (potentially insecure) [-Werror,-Wformat-security]
97 |fprintf(fp, cmd);
| ^~~
<kernel>/tools/testing/selftests/bpf/prog_tests/bpf_qdisc.c:97:14: note: treat the string as an argument to avoid this97 | fprintf(fp, cmd); | ^ |
I am removing the use of netdevsim and along with this function in the next version. The warning should also disappear. Thanks for the review!
Quoted 7 lines
Quoted 4 lines
tools/testing/selftests/bpf/prog_tests/bpf_qdisc.c +4
-0+fclose (fp); +return 0 ; +} +[...]