Skip to contentSkip to conversation

block: Introduce a BPF-based I/O scheduler

RFC In review Chengkaitao 4 participants 7 messages Mar 27 – Mar 28

Subsystems (4)
lore
Share & tools
lore
#0 lore raw

cover block: Introduce a BPF-based I/O scheduler

From: Kaitao Cheng <[email protected]>

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 these two threads:

https://lore.kernel.org/all/[email protected]/
https://lore.kernel.org/all/[email protected]/

To try it, you need to apply the patches from those series first.

Note: This is still somewhat experimental. I have only done basic testing, there may be bugs or security issues, which I plan to address in follow-up work. I am also looking for community feedback on whether this direction and the implementation approach make sense, and what else we should consider.

Kaitao Cheng (2):
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                             |  49 +-
 block/blk-mq-sched.h                          |   4 +
 block/blk-mq.c                                |   8 +-
 block/blk-mq.h                                |   2 +-
 block/blk.h                                   |   2 +
 block/ufq-bpfops.c                            | 213 +++++++
 block/ufq-iosched.c                           | 526 ++++++++++++++++++
 block/ufq-iosched.h                           |  38 ++
 block/ufq-kfunc.c                             |  91 +++
 tools/ufq_iosched/.gitignore                  |   2 +
 tools/ufq_iosched/Makefile                    | 262 +++++++++
 tools/ufq_iosched/README.md                   | 136 +++++
 .../include/bpf-compat/gnu/stubs.h            |  12 +
 tools/ufq_iosched/include/ufq/common.bpf.h    |  73 +++
 tools/ufq_iosched/include/ufq/common.h        |  91 +++
 tools/ufq_iosched/include/ufq/simple_stat.h   |  21 +
 tools/ufq_iosched/ufq_simple.bpf.c            | 445 +++++++++++++++
 tools/ufq_iosched/ufq_simple.c                | 118 ++++
 20 files changed, 2094 insertions(+), 8 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.43.0
#1 lore raw

RFC 1/2 block: Introduce the UFQ I/O scheduler

From: 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     |  49 +++-
 block/blk-mq-sched.h  |   4 +
 block/blk-mq.c        |   8 +-
 block/blk-mq.h        |   2 +-
 block/blk.h           |   2 +
 block/ufq-bpfops.c    | 213 +++++++++++++++++
 block/ufq-iosched.c   | 526 ++++++++++++++++++++++++++++++++++++++++++
 block/ufq-iosched.h   |  38 +++
 block/ufq-kfunc.c     |  91 ++++++++
 11 files changed, 934 insertions(+), 8 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
index 27f11320b8d1..56afc425cc52 100644
@@ -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
index c65f4da93702..9bb9144079aa 100644
@@ -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
block/blk-merge.c +43 -6

86 lines not on this page · raw message

block/blk-mq-sched.h +4 -0
index 5678e15bd33c..e5f7187044c4 100644
@@ -7,6 +7,10 @@
 
 #define MAX_SCHED_RQ (16 * BLKDEV_DEFAULT_RQ)
 
+bool blk_mq_sched_merge_fn(struct request_queue *q, struct bio *bio,
+		unsigned int nr_segs, struct request **merged_request,
+		struct request *rq, enum elv_merge type, void (*fn)
+		(struct request_queue *, struct request *, enum elv_merge));
 bool blk_mq_sched_try_merge(struct request_queue *q, struct bio *bio,
 		unsigned int nr_segs, struct request **merged_request);
 bool blk_mq_sched_bio_merge(struct request_queue *q, struct bio *bio,
block/blk-mq.c +7 -1
index 3da2215b2912..b8282f9a534b 100644
@@ -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
index aa15d31aaae9..3f85cae7bf57 100644
@@ -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 +2 -0
index f6053e9dd2aa..2da3958ec27b 100644
@@ -449,6 +449,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 +213 -0

213 lines not on this page · raw message

block/ufq-iosched.c new file +526 -0

526 lines not on this page · raw message

block/ufq-iosched.h new file +38 -0
new file mode 100644
index 000000000000..c717362eab31
@@ -0,0 +1,38 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+/*
+ * Copyright (c) 2026 KylinSoft Corporation.
+ * Copyright (c) 2026 Kaitao Cheng <[email protected]>
+ */
+#ifndef _BLOCK_UFQ_IOSCHED_H
+#define _BLOCK_UFQ_IOSCHED_H
+
+#include "elevator.h"
+#include "blk-mq.h"
+
+#ifndef BPF_IOSCHED_NAME_MAX
+#define BPF_IOSCHED_NAME_MAX	16
+#endif
+
+struct ufq_iosched_ops {
+	int (*init_sched)(struct request_queue *q);
+	int (*insert_req)(struct request_queue *q, struct request *rq,
+			blk_insert_t flags);
+	int (*exit_sched)(struct request_queue *q);
+	bool (*has_req)(struct request_queue *q, int rqs_count);
+	void (*req_merged)(struct request_queue *q, struct request *rq, int type);
+	void (*finish_req)(struct request *rq);
+	struct request *(*merge_req)(struct request_queue *q, struct request *rq,
+			int *type);
+	struct request *(*find_req_from_sector)(struct request_queue *q,
+			sector_t start, sector_t end);
+	struct request *(*former_req)(struct request_queue *q, struct request *rq);
+	struct request *(*next_req)(struct request_queue *q, struct request *rq);
+	struct request *(*dispatch_req)(struct request_queue *q);
+	char name[BPF_IOSCHED_NAME_MAX];
+};
+extern struct ufq_iosched_ops ufq_ops;
+
+int bpf_ufq_ops_init(void);
+int bpf_ufq_kfunc_init(void);
+
+#endif /* _BLOCK_UFQ_IOSCHED_H */
block/ufq-kfunc.c new file +91 -0

91 lines not on this page · raw message

Signature · 1 line
2.43.0
#2 lore raw

Re: [RFC 1/2] block: Introduce the UFQ I/O scheduler

Quoted 9 lines · Chengkaitao
block/ufq-kfunc.c new file +8 -0
+
+__bpf_kfunc bool bpf_request_put(struct request *rq)
+{
+       if (req_ref_put_and_test(rq))
+               return false;
+
+       return true;
+}

...

Quoted 1 line

+BTF_ID_FLAGS(func, bpf_request_put)

I bet you knew this is not safe and despite that you still decided to hack it this way.
This corner cutting is a red flag for this patchset and your link list patches.
Since you got caught doing this you need to prove much harder that you know what you're doing and there are no other safety issues.

#3 lore raw

RFC 2/2 tools/ufq_iosched: add BPF example scheduler and build scaffolding

In reply to Chengkaitao

From: 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                   | 136 ++++++
 .../include/bpf-compat/gnu/stubs.h            |  12 +
 tools/ufq_iosched/include/ufq/common.bpf.h    |  73 +++
 tools/ufq_iosched/include/ufq/common.h        |  91 ++++
 tools/ufq_iosched/include/ufq/simple_stat.h   |  21 +
 tools/ufq_iosched/ufq_simple.bpf.c            | 445 ++++++++++++++++++
 tools/ufq_iosched/ufq_simple.c                | 118 +++++
 9 files changed, 1160 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
new file mode 100644
index 000000000000..d6264fe1c8cd
@@ -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 +136 -0

136 lines not on this page · raw message

tools/ufq_iosched/include/bpf-compat/gnu/stubs.h new file +12 -0
new file mode 100644
index 000000000000..f200ac0f6ccd
@@ -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 +73 -0

73 lines not on this page · raw message

tools/ufq_iosched/include/ufq/common.h new file +91 -0

91 lines not on this page · raw message

tools/ufq_iosched/include/ufq/simple_stat.h new file +21 -0
new file mode 100644
index 000000000000..286d5999a457
@@ -0,0 +1,21 @@
+/* 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_MERGE_CNT,
+	UFQ_SIMP_MERGE_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 +445 -0

445 lines not on this page · raw message

tools/ufq_iosched/ufq_simple.c new file +118 -0

118 lines not on this page · raw message

Signature · 1 line
2.43.0
#4 lore raw

Re: [RFC 2/2] tools/ufq_iosched: add BPF example scheduler and build scaffolding

tools/ufq_iosched/README.md line 31

Quoted 6 lines · Chengkaitao

+3. rust >= 1.70.0

tools/ufq_iosched/README.md new file +4 -0
+
+Rust schedulers uses features present in the rust toolchain >= 1.70.0. You
+should be able to use the stable build from rustup, but if that doesn't
+work, try using the rustup nightly build.

[ I have no context on this patch series, but since the filter Cc the Rust for Linux mailing list due to the Rust keyword (I imagine), I guess I will take the chance to ask. ]

Is there a reason to have a different minimum than the Rust global one?

Currently it is Rust 1.78.0, and soon (this cycle) will be Rust 1.85.0, so that should be fine for you. The plan is to follow Debian Stable's Rust toolchain.

In addition, you mention nightly -- why someone would need nightly? i.e. you offer a Rust 1.70.0 minimum, which is 3 years old, but then right after you mention Rust nightly, which is essentially 1 day old. So it is a huge contrast, not to mention the availability of unstable features in nightly. Do you mean sometimes you may need unstable features?

Thanks!

Cheers,
Miguel

#5 lore raw
In reply to Chengkaitao
Quoted 2 lines · Chengkaitao

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,

Does "ctx" perhaps refer to struct blk_mq_hw_ctx? If so, please use the abbreviation "hctx" to prevent confusion with struct blk_mq_ctx (block layer software queue).

For what type of block devices is this new type of I/O scheduler intended? This new type of I/O scheduler is not appropriate for hard disks. To schedule I/O effectively for harddisks, an I/O scheduler must
be aware of all pending I/O requests. This is why the mq-deadline I/O
scheduler maintains a single list of requests across all hardware
queues.

Additionally, this new I/O scheduler is not appropriate for the fastest block devices. For very fast block devices, any I/O scheduler incurs a measurable overhead.

Quoted 3 lines

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,

What does "into user space" mean in this context? As you know BPF code runs in kernel context.

Thanks,

Bart.

#6 lore raw
Quoted 8 lines · Bart Van Assche
Quoted 2 lines · Chengkaitao

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,

Does "ctx" perhaps refer to struct blk_mq_hw_ctx? If so, please use the abbreviation "hctx" to prevent confusion with struct blk_mq_ctx (block layer software queue).

Here, ctx refers to struct blk_mq_ctx. The intent is: when no eBPF policy is attached, the new I/O scheduler behaves like the none scheduler; when an eBPF policy is attached, the blk_mq_ctx queues maintained by the new I/O scheduler serve as backup and fallback for the eBPF program.

Quoted 6 lines

For what type of block devices is this new type of I/O scheduler intended? This new type of I/O scheduler is not appropriate for hard disks. To schedule I/O effectively for harddisks, an I/O scheduler must be aware of all pending I/O requests. This is why the mq-deadline I/O scheduler maintains a single list of requests across all hardware queues.

This new I/O scheduler targets mechanical hard disks. The scheduler can be aware of all pending I/O requests, and users can maintain a single list of requests in an eBPF program.

Quoted 3 lines

Additionally, this new I/O scheduler is not appropriate for the fastest block devices. For very fast block devices, any I/O scheduler incurs a measurable overhead.

For the fastest block devices, avoiding extra scheduling policy is often the best policy. In some customized workloads, however, extra policy may be needed, for example priority scheduling, cgroup-aware differentiation, or fine-grained metrics. Those scenarios have not been validated with real demos yet, but the approach seems viable.

Quoted 6 lines
Quoted 3 lines

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,

What does "into user space" mean in this context? As you know BPF code runs in kernel context.

Sorry, my wording was inaccurate. It would be more appropriate to phrase it as "into user-defined BPF programs".

Signature · 2 lines
Yours,
Chengkaitao