block: Introduce a BPF-based I/O scheduler
Subsystems (3)
v2 cover block: Introduce a BPF-based I/O scheduler
I have been working on adding a new BPF-based I/O scheduler. It has both kernel and user-space parts. In kernel space, using per-ctx, I implemented a simple elevator that exposes a set of BPF hooks. The goal is to move the policy side of I/O scheduling out of the kernel and into user space, which should greatly increase flexibility and applicability. To verify that the whole stack works end to end, I wrote a simple BPF example program. I am calling this feature the UFQ (User-programmable Flexible Queueing) I/O scheduler.
This patch depends on new BPF functionality that I have already posted to the BPF community but that is not yet in mainline. Details are in the thread:
https://lore.kernel.org/bpf/[email protected]/
To try it, you need to apply the patches from that series first.
I am looking for community feedback on whether this direction and the implementation approach make sense, and what else we should consider.
todo:
1. More thorough testing
2. Split the code into multiple sub-patches
3. Identify concrete use cases
Changes in v2:
- Remove bpf_request_put (Alexei Starovoitov)
- Update the UFQ README (Miguel Ojeda)
- Add bio merge support
- Fix synchronization issues during UFQ scheduler transitions
Link to v1:
https://lore.kernel.org/bpf/[email protected]/
Kaitao Cheng (3):
bpf: Add KF_SPIN_LOCK flag for kfuncs under bpf_spin_lock
block: Introduce the UFQ I/O scheduler tools/ufq_iosched: add BPF example scheduler and build scaffolding
block/Kconfig.iosched | 8 + block/Makefile | 1 + block/blk-merge.c | 28 +- block/blk-mq.c | 8 +- block/blk-mq.h | 2 +- block/blk.h | 5 + block/ufq-bpfops.c | 241 +++++++ block/ufq-iosched.c | 640 ++++++++++++++++++ block/ufq-iosched.h | 64 ++ block/ufq-kfunc.c | 131 ++++ include/linux/btf.h | 1 + kernel/bpf/verifier.c | 20 +- tools/ufq_iosched/.gitignore | 2 + tools/ufq_iosched/Makefile | 262 +++++++ tools/ufq_iosched/README.md | 145 ++++ .../include/bpf-compat/gnu/stubs.h | 12 + tools/ufq_iosched/include/ufq/common.bpf.h | 75 ++ tools/ufq_iosched/include/ufq/common.h | 90 +++ tools/ufq_iosched/include/ufq/simple_stat.h | 23 + tools/ufq_iosched/ufq_simple.bpf.c | 604 +++++++++++++++++ tools/ufq_iosched/ufq_simple.c | 120 ++++ 21 files changed, 2464 insertions(+), 18 deletions(-) create mode 100644 block/ufq-bpfops.c create mode 100644 block/ufq-iosched.c create mode 100644 block/ufq-iosched.h create mode 100644 block/ufq-kfunc.c create mode 100644 tools/ufq_iosched/.gitignore create mode 100644 tools/ufq_iosched/Makefile create mode 100644 tools/ufq_iosched/README.md create mode 100644 tools/ufq_iosched/include/bpf-compat/gnu/stubs.h create mode 100644 tools/ufq_iosched/include/ufq/common.bpf.h create mode 100644 tools/ufq_iosched/include/ufq/common.h create mode 100644 tools/ufq_iosched/include/ufq/simple_stat.h create mode 100644 tools/ufq_iosched/ufq_simple.bpf.c create mode 100644 tools/ufq_iosched/ufq_simple.c
Signature · 1 line
2.50.1 (Apple Git-155)
RFC v2 1/3 bpf: Add KF_SPIN_LOCK flag for kfuncs under bpf_spin_lock
From: Kaitao Cheng <[email protected]>
Introduce the KF_SPIN_LOCK kfunc metadata flag in BTF so kfuncs may be explicitly marked as safe to call while holding bpf_spin_lock.
Allow kfuncs defined in kernel modules to be marked with KF_SPIN_LOCK.
--- include/linux/btf.h | 1 + kernel/bpf/verifier.c | 20 +++++++++++++++----- 2 files changed, 16 insertions(+), 5 deletions(-)
include/linux/btf.h +1 -0
@@ -79,6 +79,7 @@#define KF_ARENA_ARG1 (1 << 14)/* kfunc takes an arena pointer as its first argument */ #define KF_ARENA_ARG2 (1 << 15)/* kfunc takes an arena pointer as its second argument */ #define KF_IMPLICIT_ARGS (1 << 16)/* kfunc has implicit arguments supplied by the verifier */ +#define KF_SPIN_LOCK (1 << 17)/* kfunc is allowed inside bpf_spin_lock-ed region */ /* * Tag marking a kernel function as a kfunc. This is meant to minimize the
kernel/bpf/verifier.c +15 -5
@@ -11403,11 +11403,21 @@ static bool is_bpf_stream_kfunc(u32 btf_id) btf_id == special_kfunc_list[KF_bpf_stream_print_stack]; } -static bool kfunc_spin_allowed (u32 btf_id) +static bool kfunc_spin_allowed (struct bpf_verifier_env *env,s32 func_id,s16 offset) { -return is_bpf_graph_api_kfunc (btf_id) ||is_bpf_iter_num_api_kfunc (btf_id) || -is_bpf_res_spin_lock_kfunc (btf_id) ||is_bpf_arena_kfunc (btf_id) || -is_bpf_stream_kfunc (btf_id); +struct bpf_kfunc_meta kfunc; +int err; + +if (is_bpf_graph_api_kfunc (func_id) ||is_bpf_iter_num_api_kfunc (func_id) || +is_bpf_res_spin_lock_kfunc (func_id) ||is_bpf_arena_kfunc (func_id) || +is_bpf_stream_kfunc (func_id)) +return true; + + err =fetch_kfunc_meta (env, func_id, offset, &kfunc); +if (err || !kfunc.flags) +return false; + +return *kfunc.flags &KF_SPIN_LOCK ; }static bool is_sync_callback_calling_kfunc (u32 btf_id)
@@ -17025,7 +17035,7 @@ static int do_check_insn(struct bpf_verifier_env *env, bool *do_print_state) insn->imm != BPF_FUNC_spin_unlock && insn->imm != BPF_FUNC_kptr_xchg) || (insn->src_reg == BPF_PSEUDO_KFUNC_CALL && - (insn->off != 0 || !kfunc_spin_allowed(insn->imm)))) { + !kfunc_spin_allowed(env, insn->imm, insn->off))) {verbose (env,"function calls are not allowed while holding a lock\n" );return -EINVAL ;
Signature · 1 line
2.50.1 (Apple Git-155)
RFC v2 2/3 block: Introduce the UFQ I/O scheduler
In reply to Kaitao chengFrom: Kaitao Cheng <[email protected]>
Introduce IOSCHED_UFQ, a blk-mq elevator ("ufq: User-programmable Flexible Queueing") whose policy is supplied by an eBPF program via struct_ops (insert, dispatch, merge, finish, etc.).
When no eBPF program is attached, the UFQ I/O scheduler uses a simple, per-ctx queueing policy (similar to none). After an eBPF program is attached, the user-defined scheduling policy replaces UFQ’s built-in queueing policy, while per-ctx queues remain available as a fallback mechanism.
--- block/Kconfig.iosched | 8 + block/Makefile | 1 + block/blk-merge.c | 28 +- block/blk-mq.c | 8 +- block/blk-mq.h | 2 +- block/blk.h | 5 + block/ufq-bpfops.c | 241 ++++++++++++++++ block/ufq-iosched.c | 640 ++++++++++++++++++++++++++++++++++++++++++ block/ufq-iosched.h | 64 +++++ block/ufq-kfunc.c | 131 +++++++++ 10 files changed, 1115 insertions(+), 13 deletions(-) create mode 100644 block/ufq-bpfops.c create mode 100644 block/ufq-iosched.c create mode 100644 block/ufq-iosched.h create mode 100644 block/ufq-kfunc.c
block/Kconfig.iosched +8 -0
@@ -44,4 +44,12 @@ config BFQ_CGROUP_DEBUG Enable some debugging help. Currently it exports additional stat files in a cgroup which can be useful for debugging. +config IOSCHED_UFQ + tristate "UFQ I/O scheduler" + default y + help + The UFQ I/O scheduler is a programmable I/O scheduler. When + enabled, an out-of-kernel I/O scheduler based on eBPF can be + designed to interact with it, leveraging its customizable + hooks to redefine I/O scheduling policies. endmenu
block/Makefile +1 -0
@@ -24,6 +24,7 @@ obj-$(CONFIG_MQ_IOSCHED_DEADLINE) += mq-deadline.o obj-$(CONFIG_MQ_IOSCHED_KYBER) += kyber-iosched.o bfq-y := bfq-iosched.o bfq-wf2q.o bfq-cgroup.o obj-$(CONFIG_IOSCHED_BFQ) += bfq.o +obj-$(CONFIG_IOSCHED_UFQ) += ufq-iosched.o ufq-bpfops.o ufq-kfunc.o obj-$(CONFIG_BLK_DEV_INTEGRITY) += bio-integrity.o blk-integrity.o t10-pi.o \ bio-integrity-auto.o bio-integrity-fs.o
block/blk-merge.c +17 -11
@@ -774,8 +774,8 @@ u8 bio_seg_gap(struct request_queue *q, struct bio *prev, struct bio *next,* For non-mq, this has to be called with the request spinlock acquired. * For mq with scheduling, the appropriate queue wide lock should be held. */ -static struct request *attempt_merge (struct request_queue *q, -struct request *req,struct request *next) +static struct request *attempt_merge (struct request_queue *q,struct request *req, +struct request *next,bool nohash) {if (!rq_mergeable (req) || !rq_mergeable (next))return NULL ;
@@ -842,7 +842,7 @@ static struct request *attempt_merge(struct request_queue *q, req->__data_len +=blk_rq_bytes (next); -if (!blk_discard_mergable (req)) +if (!nohash && !blk_discard_mergable (req))elv_merge_requests (q, req, next);blk_crypto_rq_put_keyslot (next);
@@ -868,7 +868,7 @@ static struct request *attempt_back_merge(struct request_queue *q,struct request *next =elv_latter_request (q, rq);if (next) -return attempt_merge (q, rq, next); +return attempt_merge (q, rq, next, false);return NULL ; }
@@ -879,11 +879,17 @@ static struct request *attempt_front_merge(struct request_queue *q,struct request *prev =elv_former_request (q, rq);if (prev) -return attempt_merge (q, prev, rq); +return attempt_merge (q, prev, rq, false);return NULL ; } +struct request *bpf_attempt_merge (struct request_queue *q,struct request *rq, +struct request *next) +{ +return attempt_merge (q, rq, next, true); +} +/* * Try to merge 'next' into 'rq'. Return true if the merge happened, false * otherwise. The caller is responsible for freeing 'next' if the merge
@@ -892,7 +898,7 @@ static struct request *attempt_front_merge(struct request_queue *q,bool blk_attempt_req_merge (struct request_queue *q,struct request *rq,struct request *next) { -return attempt_merge (q, rq, next); +return attempt_merge (q, rq, next, false); }bool blk_rq_merge_ok (struct request *rq,struct bio *bio)
@@ -1035,11 +1041,11 @@ static enum bio_merge_status bio_attempt_discard_merge(struct request_queue *q,return BIO_MERGE_FAILED ; } -static enum bio_merge_status blk_attempt_bio_merge (struct request_queue *q, -struct request *rq, -struct bio *bio, -unsigned int nr_segs, -bool sched_allow_merge) +enum bio_merge_status blk_attempt_bio_merge (struct request_queue *q, +struct request *rq, +struct bio *bio, +unsigned int nr_segs, +bool sched_allow_merge) {if (!blk_rq_merge_ok (rq, bio))return BIO_MERGE_NONE ;
block/blk-mq.c +7 -1
@@ -796,7 +796,7 @@ static void blk_mq_finish_request(struct request *rq) } } -static void __blk_mq_free_request (struct request *rq) +void __blk_mq_free_request (struct request *rq) {struct request_queue *q = rq->q;struct blk_mq_ctx *ctx = rq->mq_ctx;
@@ -1844,6 +1844,12 @@ static bool dispatch_rq_from_ctx(struct sbitmap *sb, unsigned int bitnr,if (list_empty (&ctx->rq_lists[type]))sbitmap_clear_bit (sb, bitnr); } + +if (dispatch_data->rq) { + dispatch_data->rq->rq_flags |=RQF_STARTED ; +if (hctx->queue->last_merge == dispatch_data->rq) + hctx->queue->last_merge =NULL ; + }spin_unlock (&ctx->lock);return !dispatch_data->rq;
block/blk-mq.h +1 -1
@@ -56,7 +56,7 @@ void blk_mq_flush_busy_ctxs(struct blk_mq_hw_ctx *hctx, struct list_head *list);struct request *blk_mq_dequeue_from_ctx (struct blk_mq_hw_ctx *hctx,struct blk_mq_ctx *start);void blk_mq_put_rq_ref (struct request *rq); - +void __blk_mq_free_request (struct request *rq);/* * Internal helpers for allocating/freeing the request map */
block/blk.h +5 -0
@@ -313,6 +313,9 @@ enum bio_merge_status {enum bio_merge_status bio_attempt_back_merge (struct request *req,struct bio *bio,unsigned int nr_segs); +enum bio_merge_status blk_attempt_bio_merge (struct request_queue *q, +struct request *rq,struct bio *bio,unsigned int nr_segs, +bool sched_allow_merge);bool blk_attempt_plug_merge (struct request_queue *q,struct bio *bio,unsigned int nr_segs); bool blk_bio_list_merge(struct request_queue *q, struct list_head *list,
@@ -444,6 +447,8 @@ static inline unsigned get_max_segment_size(const struct queue_limits *lim,int ll_back_merge_fn (struct request *req,struct bio *bio,unsigned int nr_segs); +struct request *bpf_attempt_merge (struct request_queue *q,struct request *rq, +struct request *next);bool blk_attempt_req_merge (struct request_queue *q,struct request *rq,struct request *next);unsigned int blk_recalc_rq_segments (struct request *rq);
block/ufq-bpfops.c new file +241 -0
241 lines not on this page · raw message
block/ufq-iosched.c new file +640 -0
640 lines not on this page · raw message
block/ufq-iosched.h new file +64 -0
64 lines not on this page · raw message
block/ufq-kfunc.c new file +131 -0
131 lines not on this page · raw message
Signature · 1 line
2.50.1 (Apple Git-155)
[email protected]
AI reviewRe: [RFC v2 2/3] block: Introduce the UFQ I/O scheduler
CONFIG_IOSCHED_UFQ is a tristate option so the UFQ modules can be built with CONFIG_IOSCHED_UFQ=m. However bpf_attempt_merge has no EXPORT_SYMBOL or EXPORT_SYMBOL_GPL declaration. When built as a…
Quoted 55 lines
block/Kconfig.iosched +8
-0@@ -44,4 +44,12 @@ Enable some debugging help. Currently it exports additional stat files in a cgroup which can be useful for debugging. +config IOSCHED_UFQ + tristate "UFQ I/O scheduler" + default y + help + The UFQ I/O scheduler is a programmable I/O scheduler. When + enabled, an out-of-kernel I/O scheduler based on eBPF can be + designed to interact with it, leveraging its customizable + hooks to redefine I/O scheduling policies. endmenublock/Makefile +1
-0@@ -24,6 +24,7 @@ obj-$(CONFIG_MQ_IOSCHED_KYBER) += kyber-iosched.o bfq-y := bfq-iosched.o bfq-wf2q.o bfq-cgroup.o obj-$(CONFIG_IOSCHED_BFQ) += bfq.o +obj-$(CONFIG_IOSCHED_UFQ) += ufq-iosched.o ufq-bpfops.o ufq-kfunc.o obj-$(CONFIG_BLK_DEV_INTEGRITY) += bio-integrity.o blk-integrity.o t10-pi.o \ bio-integrity-fs.oblock/blk-merge.c +3
-4@@ -774,8 +774,8 @@* For non-mq, this has to be called with the request spinlock acquired. * For mq with scheduling, the appropriate queue wide lock should be held. */ -static struct request *attempt_merge (struct request_queue *q, -struct request *req,struct request *next) +static struct request *attempt_merge (struct request_queue *q,struct request *req, +struct request *next,bool nohash) {if (!rq_mergeable (req) || !rq_mergeable (next))return NULL ;@@ -842,7 +842,7 @@ req->__data_len +=blk_rq_bytes (next); -if (!blk_discard_mergable (req)) +if (!nohash && !blk_discard_mergable (req))elv_merge_requests (q, req, next);blk_crypto_rq_put_keyslot (next);@@ -879,11 +879,17 @@struct request *prev =elv_former_request (q, rq);if (prev) -return attempt_merge (q, prev, rq);
block/blk-merge.c line 891
Quoted 10 lines
block/blk-merge.c +6
-0+return attempt_merge (q, prev, rq, false);return NULL ; } +struct request *bpf_attempt_merge (struct request_queue *q,struct request *rq, +struct request *next) +{ +return attempt_merge (q, rq, next, true); +}
CONFIG_IOSCHED_UFQ is a tristate option so the UFQ modules can be built with CONFIG_IOSCHED_UFQ=m. However bpf_attempt_merge has no EXPORT_SYMBOL or EXPORT_SYMBOL_GPL declaration. When built as a module, ufq-iosched.ko calls this function in ufq_try_insert_merge and ufq-kfunc.ko calls it in bpf_request_try_merge, causing a linker failure ("undefined reference to bpf_attempt_merge"). Should EXPORT_SYMBOL_GPL be added here, or should CONFIG_IOSCHED_UFQ be changed to a bool?
Quoted 10 lines
block/blk-merge.c +0
-3/* * Try to merge 'next' into 'rq'. Return true if the merge happened, false @@ -1035,11 +1041,11 @@return BIO_MERGE_FAILED ; } -static enum bio_merge_status blk_attempt_bio_merge(struct request_queue *q, - struct request *rq, - struct bio *bio,
block/blk-merge.c line 1051
Quoted 10 lines
block/blk-merge.c +5
-2- unsigned int nr_segs, - bool sched_allow_merge) +enum bio_merge_status blk_attempt_bio_merge (struct request_queue *q, +struct request *rq, +struct bio *bio, +unsigned int nr_segs, +bool sched_allow_merge) {if (!blk_rq_merge_ok (rq, bio))return BIO_MERGE_NONE ;
blk_attempt_bio_merge is changed from static to externally linked and is called from ufq-iosched.c (ufq_bio_merge) and ufq-kfunc.c (bpf_request_bio_try_merge). Since CONFIG_IOSCHED_UFQ is tristate, these callers can be built as modules, but blk_attempt_bio_merge has no EXPORT_SYMBOL or EXPORT_SYMBOL_GPL. When CONFIG_IOSCHED_UFQ=m, the modules will fail to link with "undefined reference to blk_attempt_bio_merge". Should EXPORT_SYMBOL_GPL be added, or should CONFIG_IOSCHED_UFQ be restricted to bool?
block/blk-mq.c line 802
Quoted 12 lines
block/blk-mq.c +1
-1@@ -796,7 +796,7 @@ } } -static void __blk_mq_free_request (struct request *rq) +void __blk_mq_free_request (struct request *rq) {struct request_queue *q = rq->q;struct blk_mq_ctx *ctx = rq->mq_ctx;
__blk_mq_free_request is changed from static to externally linked and is called from ufq-iosched.c (ufq_dispatch_request, ufq_try_insert_merge) and ufq-kfunc.c (bpf_request_release). CONFIG_IOSCHED_UFQ is tristate so these callers can be modules, but __blk_mq_free_request has no EXPORT_SYMBOL declaration. With CONFIG_IOSCHED_UFQ=m the modules will fail to link ("undefined reference to __blk_mq_free_request"). Should EXPORT_SYMBOL_GPL be added here, or should CONFIG_IOSCHED_UFQ be restricted to bool?
[ ... ]
Quoted 4 lines
block/ufq-bpfops.c new file +0
-0@@ -0,0 +1,241 @@
[ ... ]
block/ufq-bpfops.c line 134
Quoted 13 lines
block/ufq-bpfops.c new file +13
-0+static int bpf_ufq_enable (void *ops) +{ + ufq_ops = *(struct ufq_iosched_ops *)ops; +atomic_set (&ufq_bpfops_enabled,1 ); +return 0 ; +} + +static void bpf_ufq_disable (struct ufq_iosched_ops *ops) +{ +atomic_set (&ufq_bpfops_enabled,0 ); +wait_event (ufq_bpfops_wq, !atomic_read (&ufq_bpfops_users)); +memset (&ufq_ops,0 ,sizeof (ufq_ops)); +}
Is a memory barrier needed between atomic_set(&ufq_bpfops_enabled, 0) and the first atomic_read(&ufq_bpfops_users)?
Looking at ufq_bpfops_tryget:
atomic_inc(&ufq_bpfops_users);
smp_mb__after_atomic();
if (unlikely(!atomic_read(&ufq_bpfops_enabled))) {
if (atomic_dec_and_test(&ufq_bpfops_users))
wake_up_all(&ufq_bpfops_wq);
return NULL;
}
return &ufq_ops;The inc/mb/read sequence on the reader side pairs with a store/mb/read on the disable side. But bpf_ufq_disable only has:
atomic_set(&ufq_bpfops_enabled, 0); wait_event(..., !atomic_read(&ufq_bpfops_users));
The wait_event macro expands to an immediate condition check before acquiring the wait queue lock. Without a barrier between the store to enabled and the first load of users, Store-Load reordering can allow the disable path to observe users == 0 while a concurrent reader observes enabled == 1 and returns &ufq_ops. This creates a window where the disable path proceeds to memset(&ufq_ops, 0, ...) while a reader is returning the pointer to a caller who will immediately dereference it, resulting in a NULL function pointer dereference.
Should smp_mb__after_atomic() be added after the atomic_set in bpf_ufq_disable?
Quoted 17 lines
block/ufq-bpfops.c new file +17
-0+static bool bpf_ufq_is_valid_access (int off,int size, +enum bpf_access_type type, +const struct bpf_prog *prog, +struct bpf_insn_access_aux *info) +{ +if (type !=BPF_READ ) +return false; +if (off <0 || off >=sizeof (__u64) *MAX_BPF_FUNC_ARGS ) +return false; +if (off % size !=0 ) +return false; + +/* +* btf_ctx_access() treats pointers that are not "pointer to struct" +* as scalars (no reg_type), so loading pointers like merge_req()'s +* int *type or merge_bio()'s bool *merged from ctx leaves a SCALAR +* and stores through them fail verification. Model both as writable
block/ufq-bpfops.c line 81
Quoted 10 lines
block/ufq-bpfops.c new file +10
-0+* buffers. +*/ +if (size ==sizeof (__u64) && prog->aux->attach_func_name && + ((!strcmp (prog->aux->attach_func_name,"merge_req" ) && off ==16 ) || + (!strcmp (prog->aux->attach_func_name,"merge_bio" ) && off ==24 ))) { +if (!btf_ctx_access (off, size, type, prog, info)) +return false; + info->reg_type =PTR_TO_BUF ; +return true; + }
The merge_req and merge_bio ctx offsets at 16 and 24 are overridden to PTR_TO_BUF so BPF programs can store through the int *type and bool *merged pointers. However info->reg_type is set without populating any size or range field (such as info->mem_size or a MEM_FIXED_SIZE marker).
The verifier's check_buffer_access only enforces off >= 0 and rejects variable offsets; it does not clamp the access to any declared buffer size. This allows a BPF program to compute ptr + K for large K and write beyond the stack object.
The kernel callers pass real stack variables:
ufq_try_insert_merge: &merge_type (4-byte int)
ufq_bio_merge: &ret (1-byte bool)
A BPF program could issue *(type + 1024) = 0 which would be accepted by the verifier (constant offset, non-negative, PTR_TO_BUF) but write to stack memory beyond the local variable, corrupting saved registers or frame pointers.
Other subsystems mark such buffers with PTR_TO_MEM | MEM_FIXED_SIZE and set info->mem_size, or use kfuncs with explicit size arguments. Should a declared size be added here, or should these be redesigned as kfunc parameters?
[ ... ]
Quoted 4 lines
block/ufq-iosched.c new file +0
-0@@ -0,0 +1,640 @@
[ ... ]
Quoted 15 lines
block/ufq-iosched.c new file +15
-0+static int ufq_init_sched (struct request_queue *q,struct elevator_queue *eq) +{ +const struct ufq_iosched_ops *ops; +struct ufq_data *ufq; + + ufq =kzalloc_node (sizeof (*ufq),GFP_KERNEL , q->node); +if (!ufq) +return -ENOMEM ; + + eq->elevator_data = ufq; + ufq->q = q; +INIT_LIST_HEAD (&ufq->active_node); + +blk_queue_flag_set (QUEUE_FLAG_SQ_SCHED , q); + q->elevator = eq;
block/ufq-iosched.c line 155
Quoted 10 lines
block/ufq-iosched.c new file +10
-0+ + q->async_depth = q->nr_requests; + ufq->async_depth = q->nr_requests; + + ops =ufq_bpfops_tryget (); +if (ops) { +if (ops->init_sched) + ops->init_sched (q); +ufq_bpfops_put (); + }
The ops->init_sched(q) call here discards the return value. The ufq_iosched_ops struct defines init_sched with signature 'int (*init_sched)(struct request_queue *q)', intending it to signal errors.
If a BPF init_sched returns an error (for example -ENOMEM from a failed per-queue allocation), ufq_init_sched continues, returns 0, and registers the queue on ufq_active_queues with the BPF program in an inconsistent state. The BPF program believes initialization failed while the kernel believes the queue is active. Later ops->insert_req / ops->dispatch_req calls will run against uninitialized BPF state.
Other block elevators (blk-mq-sched.c, blk_mq_init_sched) check the return value and roll back on error (teardown tags, free structures, clear q->elevator). Should the return value be checked here and the error propagated?
[ ... ]
Quoted 4 lines
block/ufq-kfunc.c new file +0
-0@@ -0,0 +1,131 @@
[ ... ]
Quoted 32 lines
block/ufq-kfunc.c new file +32
-0+__bpf_kfunc struct request *bpf_request_try_merge(struct request *rq, struct request *next) +{ +struct blk_mq_ctx *ctx; +struct ufq_data *ufq; +struct request *free; + +if (!rq || !next || !rq->q || rq->q != next->q) +return NULL ; + + ufq = rq->q->elevator->elevator_data; +if (!ufq) +return NULL ; + +if (rq->mq_ctx != next->mq_ctx || rq->mq_hctx != next->mq_hctx) +return NULL ; + + ctx = rq->mq_ctx; +if (!ctx) +return NULL ; + +spin_lock (&ctx->lock); + free =bpf_attempt_merge (rq->q, rq, next); +if (free) { +if (rq->q->last_merge == free) + rq->q->last_merge =NULL ; +list_del_init (&free->queuelist); +atomic_dec (&ufq->rqs_count); + } +spin_unlock (&ctx->lock); + +return free; +}
[ ... ]
block/ufq-kfunc.c line 97
Quoted 6 lines
block/ufq-kfunc.c new file +6
-0+BTF_KFUNCS_START(ufq_kfunc_set_ops) +BTF_ID_FLAGS(func, bpf_request_acquire, KF_ACQUIRE | KF_RET_NULL) +BTF_ID_FLAGS(func, bpf_request_release, KF_RELEASE) +BTF_ID_FLAGS(func, bpf_request_bio_try_merge, KF_SPIN_LOCK) +BTF_ID_FLAGS(func, bpf_request_try_merge, KF_SPIN_LOCK) +BTF_KFUNCS_END(ufq_kfunc_set_ops)
bpf_request_try_merge returns 'struct request *' and has multiple NULL return paths (!rq, !next, !ufq, mq_ctx/mq_hctx mismatch, !ctx, merge failure). The kfunc is annotated with KF_SPIN_LOCK but lacks KF_RET_NULL.
Without KF_RET_NULL the verifier treats the return value as a non-NULL pointer and allows BPF programs to dereference it unconditionally. When the kfunc returns NULL at runtime, the BPF program dereferences NULL and causes a kernel crash.
bpf_request_acquire is correctly annotated with 'KF_ACQUIRE | KF_RET_NULL'. Should bpf_request_try_merge also have KF_RET_NULL (or 'KF_SPIN_LOCK | KF_ACQUIRE | KF_RET_NULL')?
---
AI reviewed your patch. Please fix the bug or email reply why it's not a bug.
See: https://github.com/kernel-patches/vmtest/blob/master/ci/claude/README.md
CI run summary: https://github.com/kernel-patches/bpf/actions/runs/25269522212
[email protected]
AI reviewRe: [RFC v2 2/3] block: Introduce the UFQ I/O scheduler
In reply to Kaitao chengThank you for your contribution! Sashiko AI review found 11 potential issue(s) to consider: - [Critical] Arbitrary kernel stack read/write due to unvalidated PTR_TO_BUF context arguments.…
Thank you for your contribution! Sashiko AI review found 11 potential issue(s) to consider:
- [Critical] Arbitrary kernel stack read/write due to unvalidated PTR_TO_BUF context arguments.
- [Critical] dispatch_req accepts requests from foreign block queues, corrupting hardware context maps.
- [High] ufq_bio_merge incorrectly destroys the request when a bio is successfully merged.
- [High] BPF kfuncs and callbacks modify request state without verifying they are idle, causing DMA corruption.
- [High] Incorrect request handling after successful merge_req causes duplicate BPF insertions and ghost requests.
- [High] The bpf_request_try_merge kfunc permanently leaks the merged request and hardware tag.
- [High] Exposing the request to the dispatch list before BPF insertion causes a Write-After-Free.
- [High] Missing memory barrier in bpf_ufq_disable allows ufq_ops to be destroyed while still in use.
- [High] Annotating blocking kfuncs with KF_SPIN_LOCK triggers a PREEMPT_RT panic.
- [High] bpf_request_try_merge blindly casts elevator private data, causing memory corruption across I/O schedulers.
- [High] Failed BPF request insertions still receive a finish_req callback, corrupting BPF state. --
commit 3bcfa14cede6b5991bb234bc371f06d27de520a5
Author: Kaitao Cheng <[email protected]>
block: Introduce the UFQ I/O scheduler
Introduce IOSCHED_UFQ, a blk-mq elevator whose policy is supplied by an eBPF program via struct_ops. When no program is attached, it uses a simple per-ctx queueing policy as a fallback mechanism.
Quoted 3 lines
block/ufq-bpfops.c new file +0
-0
[ ... ]
Quoted 5 lines
block/ufq-bpfops.c new file +5
-0+static bool bpf_ufq_is_valid_access (int off,int size, +enum bpf_access_type type, +const struct bpf_prog *prog, +struct bpf_insn_access_aux *info) +{
[ ... ]
block/ufq-bpfops.c line 84
Quoted 11 lines
block/ufq-bpfops.c new file +11
-0+if (size ==sizeof (__u64) && prog->aux->attach_func_name && + ((!strcmp (prog->aux->attach_func_name,"merge_req" ) && off ==16 ) || + (!strcmp (prog->aux->attach_func_name,"merge_bio" ) && off ==24 ))) { +if (!btf_ctx_access (off, size, type, prog, info)) +return false; + info->reg_type =PTR_TO_BUF ; +return true; + } + +return btf_ctx_access (off, size, type, prog, info); +}
Does this allow arbitrary memory access?
The verifier handles PTR_TO_BUF by recording the maximum accessed offset in env->prog->aux->max_rdwr_access. Since UFQ uses BPF_PROG_TYPE_STRUCT_OPS, max_rdwr_access is ignored during program load.
Can a BPF program use large offsets to read or write arbitrary kernel stack memory?
[ ... ]
block/ufq-bpfops.c line 134
Quoted 6 lines
block/ufq-bpfops.c new file +6
-0+static void bpf_ufq_disable (struct ufq_iosched_ops *ops) +{ +atomic_set (&ufq_bpfops_enabled,0 ); +wait_event (ufq_bpfops_wq, !atomic_read (&ufq_bpfops_users)); +memset (&ufq_ops,0 ,sizeof (ufq_ops)); +}
Since atomic_set() is relaxed and the initial check in wait_event() evaluates locklessly without a full memory barrier, could the read of ufq_bpfops_users be reordered before the store to ufq_bpfops_enabled?
If a concurrent ufq_bpfops_tryget() reads enabled == 1 while the disable path observes users == 0, would it proceed to clear ufq_ops while it is still in use?
Quoted 3 lines
block/ufq-iosched.c new file +0
-0
[ ... ]
block/ufq-iosched.c line 46
Quoted 11 lines
block/ufq-iosched.c new file +11
-0+static struct request *ufq_dispatch_request (struct blk_mq_hw_ctx *hctx) +{ +struct ufq_data *ufq = hctx->queue->elevator->elevator_data; +const struct ufq_iosched_ops *ops; +struct blk_mq_ctx *ctx; +struct request *rq =NULL ; +unsigned short idx; + + ops =ufq_bpfops_tryget (); +if (ops && ops->dispatch_req) { + rq = ops->dispatch_req (hctx->queue);
Is there anything preventing the BPF program from returning a request that belongs to a different block device's queue?
If the BPF program stores a request from Queue A and returns it during the dispatch cycle of Queue B, would this clear the wrong ctx_map bits below and dispatch the request to the wrong driver?
[ ... ]
Quoted 3 lines
block/ufq-iosched.c new file +3
-0+static bool ufq_bio_merge (struct request_queue *q,struct bio *bio, +unsigned int nr_segs) +{
[ ... ]
Quoted 4 lines
block/ufq-iosched.c new file +4
-0+ ops =ufq_bpfops_tryget (); +if (ops) { +if (ops->merge_bio) { + rq = ops->merge_bio (q, bio, nr_segs, &ret);
[ ... ]
block/ufq-iosched.c line 329
Quoted 9 lines
block/ufq-iosched.c new file +9
-0+if (rq) { +ufq_bpfops_put (); +spin_lock (&rq->mq_ctx->lock); +if (!list_empty (&rq->queuelist)) { +list_del_init (&rq->queuelist); +atomic_dec (&ufq->rqs_count); + } +spin_unlock (&rq->mq_ctx->lock); +blk_mq_free_request (rq);
When merge_bio() returns a request, doesn't this indicate the bio was successfully appended to that existing request?
If so, why is blk_mq_free_request() called here? This seems to abort the pending I/O and destroy the request while it is still waiting to be dispatched.
[ ... ]
Quoted 3 lines
block/ufq-iosched.c new file +3
-0+static enum elv_merge ufq_try_insert_merge (struct request_queue *q, +struct request **new) +{
[ ... ]
Quoted 5 lines
block/ufq-iosched.c new file +5
-0+if (type ==ELEVATOR_NO_MERGE || !target) { +if (ops) +ufq_bpfops_put (); +return ELEVATOR_NO_MERGE ; + }else if (type ==ELEVATOR_FRONT_MERGE ) {
[ ... ]
block/ufq-iosched.c line 413
Quoted 9 lines
block/ufq-iosched.c new file +9
-0+ }else if (type ==ELEVATOR_BACK_MERGE ) { +spin_lock (&target->mq_ctx->lock); + free =bpf_attempt_merge (q, target, rq); +if (!free) { +spin_unlock (&target->mq_ctx->lock); +pr_err ("ufq-iosched: back merge failed\n" ); + goto rollback; + } + *new = target;
If ops->merge_req() returns ELEVATOR_BACK_MERGE, *new is set to target. In ufq_insert_requests(), this request is then passed to ops->insert_req().
Doesn't this cause a duplicate insert_req() call for a request the BPF program is already tracking?
Also, if ELEVATOR_FRONT_MERGE is returned, the kernel replaces and frees the target request, but is the BPF program ever notified about the freed request?
[ ... ]
Quoted 4 lines
block/ufq-iosched.c new file +4
-0+static void ufq_insert_requests (struct blk_mq_hw_ctx *hctx, +struct list_head *list, +blk_insert_t flags) +{
[ ... ]
Quoted 14 lines
block/ufq-iosched.c new file +14
-0+ type =ufq_try_insert_merge (q, &rq); +if (type ==ELEVATOR_NO_MERGE ) { + rq->fifo_time = jiffies; + ctx = rq->mq_ctx; + rq->elv.priv[0 ] = (void *)((uintptr_t )rq->elv.priv[0 ] + |UFQ_PRIV_IN_UFQ ); +spin_lock (&ctx->lock); +if (flags &BLK_MQ_INSERT_AT_HEAD ) +list_add (&rq->queuelist, &ctx->rq_lists[hctx->type]); +else +list_add_tail (&rq->queuelist, + &ctx->rq_lists[hctx->type]); + + bit = ctx->index_hw[hctx->type];
block/ufq-iosched.c line 480
Quoted 10 lines
block/ufq-iosched.c new file +10
-0+if (!sbitmap_test_bit (&hctx->ctx_map, bit)) +sbitmap_set_bit (&hctx->ctx_map, bit); + q->last_merge = rq; +spin_unlock (&ctx->lock); +atomic_inc (&ufq->rqs_count); + } + +if (ops && rq && ops->insert_req) { + rq->elv.priv[0 ] = (void *)((uintptr_t )rq->elv.priv[0 ] + |UFQ_PRIV_IN_BPF );
Once ctx->lock is dropped above, the request is globally visible and can be immediately dispatched and freed by a concurrent hardware queue run.
Could accessing rq->elv.priv[0] and passing rq to ops->insert_req() below lead to a write-after-free and a dangling pointer being sent to the BPF program?
block/ufq-iosched.c line 485
Quoted 5 lines
block/ufq-iosched.c new file +5
-0+ ret = ops->insert_req (q, rq, flags); +if (ret) { +atomic_inc (&ufq->ops_stats.insert_err_count); +pr_err ("ufq-iosched: bpf insert_req error (%d)\n" , ret); + }else {
If ops->insert_req() returns an error here, the request remains in the software queue but the UFQ_PRIV_IN_BPF flag is never cleared.
When the request completes, won't ufq_finish_request() see this flag and incorrectly call ops->finish_req() for a request the BPF program explicitly rejected?
Quoted 3 lines
block/ufq-kfunc.c new file +0
-0
[ ... ]
Quoted 3 lines
block/ufq-kfunc.c new file +3
-0+__bpf_kfunc bool bpf_request_bio_try_merge(struct request *rq, struct bio *bio, +unsigned int nr_segs) +{
[ ... ]
block/ufq-kfunc.c line 50
Quoted 6 lines
block/ufq-kfunc.c new file +6
-0+spin_lock (&ctx->lock); + merged =blk_attempt_bio_merge (rq->q, rq, bio, nr_segs, true) ==BIO_MERGE_OK ; +spin_unlock (&ctx->lock); + +return merged; +}
Is it safe to merge bios into this request without checking if it is idle?
If the target request has already been dispatched to the hardware (rq->rq_flags & RQF_STARTED), could modifying the bio lists and segment counts while the hardware is actively executing the I/O cause DMA corruption?
[ ... ]
block/ufq-kfunc.c line 61
Quoted 10 lines
block/ufq-kfunc.c new file +10
-0+__bpf_kfunc struct request *bpf_request_try_merge(struct request *rq, struct request *next) +{ +struct blk_mq_ctx *ctx; +struct ufq_data *ufq; +struct request *free; + +if (!rq || !next || !rq->q || rq->q != next->q) +return NULL ; + + ufq = rq->q->elevator->elevator_data;
Because this kfunc is also registered for BPF_PROG_TYPE_SYSCALL, a BPF program could pass a request from a block device running a completely different I/O scheduler.
Wouldn't assuming rq->q->elevator->elevator_data is a struct ufq_data and decrementing ufq->rqs_count below corrupt the private data of that other scheduler?
[ ... ]
block/ufq-kfunc.c line 83
Quoted 12 lines
block/ufq-kfunc.c new file +12
-0+spin_lock (&ctx->lock); + free =bpf_attempt_merge (rq->q, rq, next); +if (free) { +if (rq->q->last_merge == free) + rq->q->last_merge =NULL ; +list_del_init (&free->queuelist); +atomic_dec (&ufq->rqs_count); + } +spin_unlock (&ctx->lock); + +return free; +}
When bpf_attempt_merge() successfully merges next into rq, it returns the hollowed-out next request.
Since this kfunc returns it directly to the BPF program without the KF_ACQUIRE flag, the verifier treats it as an untracked pointer. Does this prevent the BPF program from calling bpf_request_release() on it, permanently leaking the request and its hardware tag?
[ ... ]
block/ufq-kfunc.c line 97
Quoted 6 lines
block/ufq-kfunc.c new file +6
-0+BTF_KFUNCS_START(ufq_kfunc_set_ops) +BTF_ID_FLAGS(func, bpf_request_acquire, KF_ACQUIRE | KF_RET_NULL) +BTF_ID_FLAGS(func, bpf_request_release, KF_RELEASE) +BTF_ID_FLAGS(func, bpf_request_bio_try_merge, KF_SPIN_LOCK) +BTF_ID_FLAGS(func, bpf_request_try_merge, KF_SPIN_LOCK) +BTF_KFUNCS_END(ufq_kfunc_set_ops)
These kfuncs are registered with KF_SPIN_LOCK, meaning they can be called inside a bpf_spin_lock critical section where preemption and interrupts are disabled.
Since they call spin_lock(&ctx->lock) internally, won't this cause a scheduling while atomic panic on PREEMPT_RT kernels where spinlock_t is a sleeping rt_mutex?
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2
RFC v2 3/3 tools/ufq_iosched: add BPF example scheduler and build scaffolding
In reply to Kaitao chengFrom: Kaitao Cheng <[email protected]>
Add ufq_iosched as a simple example for the UFQ block I/O scheduler, In the ufq_simple example, we implement the eBPF struct_ops hooks the kernel exposes so we can exercise and validate the behavior and stability of the kernel UFQ scheduling framework. The Makefile and directory layout are modeled after sched_ext.
This mirrors the sched_ext examples pattern so developers can experiment with user-defined queueing policies on top of IOSCHED_UFQ.
--- tools/ufq_iosched/.gitignore | 2 + tools/ufq_iosched/Makefile | 262 ++++++++ tools/ufq_iosched/README.md | 145 +++++ .../include/bpf-compat/gnu/stubs.h | 12 + tools/ufq_iosched/include/ufq/common.bpf.h | 75 +++ tools/ufq_iosched/include/ufq/common.h | 90 +++ tools/ufq_iosched/include/ufq/simple_stat.h | 23 + tools/ufq_iosched/ufq_simple.bpf.c | 604 ++++++++++++++++++ tools/ufq_iosched/ufq_simple.c | 120 ++++ 9 files changed, 1333 insertions(+) create mode 100644 tools/ufq_iosched/.gitignore create mode 100644 tools/ufq_iosched/Makefile create mode 100644 tools/ufq_iosched/README.md create mode 100644 tools/ufq_iosched/include/bpf-compat/gnu/stubs.h create mode 100644 tools/ufq_iosched/include/ufq/common.bpf.h create mode 100644 tools/ufq_iosched/include/ufq/common.h create mode 100644 tools/ufq_iosched/include/ufq/simple_stat.h create mode 100644 tools/ufq_iosched/ufq_simple.bpf.c create mode 100644 tools/ufq_iosched/ufq_simple.c
tools/ufq_iosched/.gitignore new file +2 -0
@@ -0,0 +1,2 @@ +tools/ +build/
tools/ufq_iosched/Makefile new file +262 -0
262 lines not on this page · raw message
tools/ufq_iosched/README.md new file +145 -0
145 lines not on this page · raw message
tools/ufq_iosched/include/bpf-compat/gnu/stubs.h new file +12 -0
@@ -0,0 +1,12 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +/* +* Dummy gnu/stubs.h. clang can end up including /usr/include/gnu/stubs.h when +* compiling BPF files although its content doesn't play any role. The file in +* turn includes stubs-64.h or stubs-32.h depending on whether __x86_64__ is +* defined. When compiling a BPF source, __x86_64__ isn't set and thus +* stubs-32.h is selected. However, the file is not there if the system doesn't +* have 32bit glibc devel package installed leading to a build failure. +* +* The problem is worked around by making this file available in the include +* search paths before the system one when building BPF. +*/
tools/ufq_iosched/include/ufq/common.bpf.h new file +75 -0
75 lines not on this page · raw message
tools/ufq_iosched/include/ufq/common.h new file +90 -0
90 lines not on this page · raw message
tools/ufq_iosched/include/ufq/simple_stat.h new file +23 -0
@@ -0,0 +1,23 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +/* +* Copyright (c) 2026 KylinSoft Corporation. +* Copyright (c) 2026 Kaitao Cheng <[email protected]> +*/ +#ifndef __UFQ_SIMPLE_STAT_H +#define __UFQ_SIMPLE_STAT_H + +enum ufq_simp_stat_index { +UFQ_SIMP_INSERT_CNT , +UFQ_SIMP_INSERT_SIZE , +UFQ_SIMP_DISPATCH_CNT , +UFQ_SIMP_DISPATCH_SIZE , +UFQ_SIMP_RQMERGE_CNT , +UFQ_SIMP_RQMERGE_SIZE , +UFQ_SIMP_BIOMERGE_CNT , +UFQ_SIMP_BIOMERGE_SIZE , +UFQ_SIMP_FINISH_CNT , +UFQ_SIMP_FINISH_SIZE , +UFQ_SIMP_STAT_MAX , +}; + +#endif /* __UFQ_SIMPLE_STAT_H */
tools/ufq_iosched/ufq_simple.bpf.c new file +604 -0
604 lines not on this page · raw message
tools/ufq_iosched/ufq_simple.c new file +120 -0
120 lines not on this page · raw message
Signature · 1 line
2.50.1 (Apple Git-155)
[email protected]
AI reviewRe: [RFC v2 3/3] tools/ufq_iosched: add BPF example scheduler and build scaffolding
Does this signature match the kernel's ufq_iosched_ops definition?
Quoted 5 lines
tools/ufq_iosched/ufq_simple.bpf.c new file +0
-0
[ ... ]
tools/ufq_iosched/ufq_simple.bpf.c line 121
Quoted 3 lines
tools/ufq_iosched/ufq_simple.bpf.c new file +3
-0+int BPF_STRUCT_OPS (ufq_simple_insert_req ,struct request_queue *q, +struct request *rq,blk_insert_t flags, +struct list_head *freeq)
^^^^^^^
Does this signature match the kernel's ufq_iosched_ops definition?
Looking at block/ufq-iosched.h, the insert_req member is defined with three parameters:
int (*insert_req)(struct request_queue *q, struct request *rq,
blk_insert_t flags);
The kernel-side callers in block/ufq-iosched.c also pass only three arguments:
ret = ops->insert_req(q, rq, flags);
ops->insert_req(q, target, 0);The BPF struct_ops verifier (check_struct_ops_btf_id) validates that each BPF program matches the declared function-pointer signature in the kernel. A program with four parameters registered against a three-parameter member will be rejected at load time, so ufq_simple will fail to attach via UFQ_OPS_LOAD / UFQ_OPS_ATTACH and the example cannot be used.
Even if the verifier accepted the mismatch, the trampoline would not populate a fourth argument, leaving freeq uninitialized. Should the extra 'struct list_head *freeq' parameter be removed?
Quoted 4 lines
tools/ufq_iosched/ufq_simple.bpf.c new file +4
-0+{ +struct ufq_simple_data *ufq_sd; +struct queue_list_node *qnode; +struct sort_tree_node *snode;
[ ... ]
tools/ufq_iosched/ufq_simple.bpf.c line 604
Quoted 10 lines
tools/ufq_iosched/ufq_simple.bpf.c new file +10
-0+UFQ_OPS_DEFINE(ufq_simple_ops, + .init_sched = (void *)ufq_simple_init_sched, + .exit_sched = (void *)ufq_simple_exit_sched, + .insert_req = (void *)ufq_simple_insert_req, + .dispatch_req = (void *)ufq_simple_dispatch_req, + .has_req = (void *)ufq_simple_has_req, + .finish_req = (void *)ufq_simple_finish_req, + .merge_req = (void *)ufq_simple_merge_req, + .merge_bio = (void *)ufq_simple_merge_bio, + .name ="ufq_simple" );
---
AI reviewed your patch. Please fix the bug or email reply why it's not a bug.
See: https://github.com/kernel-patches/vmtest/blob/master/ci/claude/README.md
CI run summary: https://github.com/kernel-patches/bpf/actions/runs/25269522212
[email protected]
AI reviewRe: [RFC v2 3/3] tools/ufq_iosched: add BPF example scheduler and build scaffolding
In reply to Kaitao chengThank you for your contribution! Sashiko AI review found 9 potential issue(s) to consider: - [High] Hallucinated BPF rbtree traversal functions - [High] Wait-context violation and lock inversion via…
Thank you for your contribution! Sashiko AI review found 9 potential issue(s) to consider:
- [High] Hallucinated BPF rbtree traversal functions
- [High] Wait-context violation and lock inversion via bpf_request_bio_try_merge
- [High] Verifier rejection due to unreleased acquired request references
- [High] Verifier rejection from accessing non-owning reference after tree mutation
- [High] Target request permanently orphaned after a merge operation
- [High] Red-black tree corruption due to mutable traversal keys
- [High] Use-After-Free when accessing free->__data_len
- [Medium] Incorrect queue insertion order for BLK_MQ_INSERT_AT_HEAD
- [Medium] Non-atomic updates to per-CPU array statistics
--
commit 837eafbebf3f48dff3cc402191f742e1f891291d
Author: Kaitao Cheng <[email protected]> tools/ufq_iosched: add BPF example scheduler and build scaffolding
Add ufq_iosched as a simple example for the UFQ block I/O scheduler. It implements the eBPF struct_ops hooks the kernel exposes to exercise the behavior and stability of the kernel UFQ scheduling framework.
Quoted 5 lines
tools/ufq_iosched/ufq_simple.bpf.c new file +0
-0
[ ... ]
tools/ufq_iosched/ufq_simple.bpf.c line 61
Quoted 7 lines
tools/ufq_iosched/ufq_simple.bpf.c new file +7
-0+static void stat_add (u32 idx,u32 val) +{ +u64 *cnt_p =bpf_map_lookup_elem (&stats, &idx); + +if (cnt_p) + (*cnt_p) += val; +}
Since operations like insert_req can run in process context while finish_req might run in softirq on the same CPU, could these non-atomic updates result in lost statistics? Should this use __sync_fetch_and_add() instead?
[ ... ]
Quoted 4 lines
tools/ufq_iosched/ufq_simple.bpf.c new file +4
-0+int BPF_STRUCT_OPS (ufq_simple_insert_req ,struct request_queue *q, +struct request *rq,blk_insert_t flags, +struct list_head *freeq) +{
[ ... ]
Quoted 1 line
tools/ufq_iosched/ufq_simple.bpf.c new file +1
-0+if (flags &BLK_MQ_INSERT_AT_HEAD ) {
[ ... ]
tools/ufq_iosched/ufq_simple.bpf.c line 164
Quoted 5 lines
tools/ufq_iosched/ufq_simple.bpf.c new file +5
-0+/* Add queue_list_node to dispatch list */ +bpf_spin_lock (&ufq_sd->lock); + ret =bpf_list_push_back (&ufq_sd->dispatch, &qnode->node); +bpf_spin_unlock (&ufq_sd->lock); + }else {
Does using bpf_list_push_back() here place the request at the tail of the dispatch list rather than the head? Since dispatch_req pops from the front, this seems to violate the requested BLK_MQ_INSERT_AT_HEAD semantics.
[ ... ]
Quoted 2 lines
tools/ufq_iosched/ufq_simple.bpf.c new file +2
-0+struct request *BPF_STRUCT_OPS (ufq_simple_dispatch_req ,struct request_queue *q) +{
[ ... ]
Quoted 8 lines
tools/ufq_iosched/ufq_simple.bpf.c new file +8
-0+ snode = container_of(rb_node, struct sort_tree_node, rb_node); + +/* Get request from sort_tree_node (this will be returned) */ + rq =bpf_kptr_xchg (&snode->req,NULL ); +bpf_spin_unlock (&ufq_sd->lock); +bpf_obj_drop (snode); + } +if (!rq)
tools/ufq_iosched/ufq_simple.bpf.c line 263
Quoted 10 lines
tools/ufq_iosched/ufq_simple.bpf.c new file +10
-0+bpf_printk ("ufq_simple/dispatch_req: no request to dispatch" ); + +out: +if (rq) { +stat_add (UFQ_SIMP_DISPATCH_CNT ,1 ); +stat_add (UFQ_SIMP_DISPATCH_SIZE , rq->__data_len); + } + +return rq; +}
Will the verifier reject this program because it returns an acquired reference from bpf_kptr_xchg without calling bpf_request_release()? Also, if the verifier did allow this, would the request reference permanently leak since the block layer doesn't consume BPF kptr references?
Quoted 2 lines
tools/ufq_iosched/ufq_simple.bpf.c new file +2
-0+bool BPF_STRUCT_OPS (ufq_simple_has_req ,struct request_queue *q,int rqs_count) +{
[ ... ]
tools/ufq_iosched/ufq_simple.bpf.c line 280
Quoted 4 lines
tools/ufq_iosched/ufq_simple.bpf.c new file +4
-0+bpf_spin_lock (&ufq_sd->lock); + has = !bpf_list_empty (&ufq_sd->dispatch) || +bpf_rbtree_root (&ufq_sd->sort_tree_read) || +bpf_rbtree_root (&ufq_sd->sort_tree_write);
The functions bpf_rbtree_root(), bpf_rbtree_left(), and bpf_rbtree_right() are used throughout this file, but they do not appear to exist in the BPF API. The API only exposes bpf_rbtree_add(), bpf_rbtree_remove(), and bpf_rbtree_first(). Will this code fail to compile and load?
[ ... ]
Quoted 3 lines
tools/ufq_iosched/ufq_simple.bpf.c new file +3
-0+struct request *BPF_STRUCT_OPS (ufq_simple_merge_req ,struct request_queue *q, +struct request *rq,int *type) +{
[ ... ]
Quoted 22 lines
tools/ufq_iosched/ufq_simple.bpf.c new file +22
-0+if (mt) { +if (rq->mq_ctx != targ_mq_ctx || rq->mq_hctx != targ_mq_hctx) { + mt =ELEVATOR_NO_MERGE ; +break ; + } + + rb_node =bpf_rbtree_remove (tree, rb_node); +if (rb_node) { + snode =container_of (rb_node, + struct sort_tree_node, rb_node); + targ =bpf_kptr_xchg (&snode->req,NULL ); +bpf_spin_unlock (&ufq_sd->lock); +if (targ) { + *type = mt; +stat_add (UFQ_SIMP_RQMERGE_CNT ,1 ); +stat_add (UFQ_SIMP_RQMERGE_SIZE , targ->__data_len); +stat_sub (UFQ_SIMP_INSERT_CNT ,1 ); +stat_sub (UFQ_SIMP_INSERT_SIZE , targ->__data_len); + } + +bpf_obj_drop (snode); + }else {
tools/ufq_iosched/ufq_simple.bpf.c line 393
Quoted 10 lines
tools/ufq_iosched/ufq_simple.bpf.c new file +10
-0+bpf_spin_unlock (&ufq_sd->lock); + *type =ELEVATOR_NO_MERGE ; + } +return targ; + } + } +bpf_spin_unlock (&ufq_sd->lock); + +return NULL ; +}
If a candidate request is found for merging, it gets permanently removed from the scheduler's rbtree and its node is dropped. Since dispatch_req will now never see it, does this result in a permanent I/O hang for the merged target request?
Quoted 5 lines
tools/ufq_iosched/ufq_simple.bpf.c new file +5
-0+static struct request *merge_bio_left_unlock (struct ufq_simple_data *ufq_sd, +struct bpf_rb_root *tree, +struct sort_tree_node *snode, +struct request *cand) +{
[ ... ]
tools/ufq_iosched/ufq_simple.bpf.c line 427
Quoted 6 lines
tools/ufq_iosched/ufq_simple.bpf.c new file +6
-0+if (left_end == cand_start) + free =bpf_request_try_merge (left_rq, cand); + +if (free == cand) { + removed =bpf_rbtree_remove (tree, &snode->rb_node); + left_rq =bpf_kptr_xchg (&left_node->req, left_rq);
According to BPF safety rules, removing a node from a collection invalidates all non-owning references to that collection. Will the verifier reject this program because it accesses left_node right after calling bpf_rbtree_remove()?
tools/ufq_iosched/ufq_simple.bpf.c line 435
Quoted 8 lines
tools/ufq_iosched/ufq_simple.bpf.c new file +8
-0+bpf_spin_unlock (&ufq_sd->lock); +if (removed) { +struct sort_tree_node *drop =container_of (removed, + struct sort_tree_node, rb_node); +bpf_obj_drop (drop); + } +stat_add (UFQ_SIMP_RQMERGE_CNT ,1 ); +stat_add (UFQ_SIMP_RQMERGE_SIZE , free->__data_len);
If bpf_request_try_merge() is successful, it merges and frees the candidate request. By passing free->__data_len to stat_add, does this cause a use-after-free read on a request that was just returned to the block layer?
[ ... ]
Quoted 4 lines
tools/ufq_iosched/ufq_simple.bpf.c new file +4
-0+struct request *BPF_STRUCT_OPS (ufq_simple_merge_bio , +struct request_queue *q,struct bio *bio, +unsigned int nr_segs,bool *merged) +{
[ ... ]
Quoted 1 line
tools/ufq_iosched/ufq_simple.bpf.c new file +1
-0+bpf_spin_lock (&ufq_sd->lock);
[ ... ]
Quoted 6 lines
tools/ufq_iosched/ufq_simple.bpf.c new file +6
-0+while (rb_node && count <UFQ_LOOP_MAX ) { + count++; + snode = container_of(rb_node, struct sort_tree_node, rb_node); + cand =bpf_kptr_xchg (&snode->req,NULL ); +if (!cand) +break ;
tools/ufq_iosched/ufq_simple.bpf.c line 562
Quoted 10 lines
tools/ufq_iosched/ufq_simple.bpf.c new file +10
-0+ + cand_start = cand->__sector; + cand_end = cand_start + (cand->__data_len >>SECTOR_SHIFT ); + +if (end < cand_start) { + rb_node =bpf_rbtree_left (tree, rb_node); + }else if (start > cand_end) { + rb_node =bpf_rbtree_right (tree, rb_node); + }else if (cand_start == end) { +if (bpf_request_bio_try_merge (cand, bio, nr_segs)) {
The rbtree traversal here compares against cand->__sector. If a request was previously front-merged, its __sector would be modified by the block layer. Since the node isn't re-inserted to update its sorting key, could subsequent traversals navigate down the wrong tree branches and corrupt the red-black tree?
Additionally, the code holds a bpf_spin_lock while calling the kfunc bpf_request_bio_try_merge().
Looking at bpf_request_bio_try_merge() in block/ufq-kfunc.c, it internally acquires a spin_lock:
block/ufq-kfunc.c:bpf_request_bio_try_merge() {
...
spin_lock(&ctx->lock);
merged = blk_attempt_bio_merge(rq->q, rq, bio, nr_segs, true) == BIO_MERGE_OK;
spin_unlock(&ctx->lock);
...
}Since bpf_spin_lock() maps to a raw_spinlock_t, and spin_lock() is sleepable on PREEMPT_RT kernels, will this create a sleep-in-atomic context that triggers a lockdep BUG and system panic?
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=3