| Seq | Timestamp | Type | Name | Duration |
|---|---|---|---|---|
| 0/0 | 2026/09/14 07:59 | flow | patch-triage |
22mArgs:null Results: {
"EnableConfigs": [
"BLK_CGROUP_IOCOST_BPF",
"BLK_CGROUP_IOCOST",
"BLK_CGROUP",
"CGROUPS",
"BPF_SYSCALL",
"BPF_JIT",
"DEBUG_INFO_BTF"
],
"FocusSymbols": [
"iocost_bpf_model_get",
"iocost_bpf_model_put",
"iocost_notify_blkcg_online",
"iocost_notify_blkcg_offline",
"bpf_iocost_reg",
"bpf_iocost_unreg",
"bpf_iocost_validate",
"ioc_cost_model_write",
"calc_vtime_cost",
"calc_size_vtime_cost"
],
"KMSANReasoning": "The patch introduces BPF struct_ops support for pluggable cost models in the blk-iocost I/O controller (CONFIG_BLK_CGROUP_IOCOST_BPF), along with lifecycle hooks, a new tracepoint (iocost_ioc_tick), and BPF selftests.\n\nAnalysis of memory handling:\n1. Heap allocations: The only new heap allocation is `struct iocost_bpf_model`, which is allocated using `kzalloc()` with all fields (`refs`, `ops`, `list`, `lifecycle`) explicitly initialized.\n2. Stack buffers: `bpf_model` in `ioc_cost_model_write()` is initialized (`bpf_model[0] = '\\0'`) and written via `match_strlcpy()`. The tick tracepoint variables in `ioc_timer_fn()` are all explicitly populated scalar variables. Member name copying in BPF struct_ops uses `bpf_obj_name_cpy()`, which zeroes the target buffer and validates null-termination.\n3. Kernel-to-userspace exposure: No binary kernel structures, unpadded structs, or network packets are sent to user space. The only output is formatted ASCII text printed to `seq_file` in cgroupfs (`io.cost.model`), printing initialized strings and integers.\n4. KASAN vs KMSAN applicability: Any potential issues introduced by this patch involve concurrency, RCU dereferences, BPF struct_ops refcounting, and list management (risking use-after-free, double free, or deadlocks), which are detected by KASAN and LOCKDEP. There are no uninitialized memory reads or information leak hazards that would require KMSAN.",
"NeedsKMSAN": false,
"Reasoning": "The patch series introduces BPF struct_ops support for pluggable cost models in the blk-iocost cgroup IO controller (CONFIG_BLK_CGROUP_IOCOST_BPF). It adds a new struct_ops type 'iocost_model_ops', a model registry with reference counting and lifecycle tracking, integration with cgroup blkcg online/offline notifications, sysfs/cgroupfs parsing to switch models via 'io.cost.model', and RCU-guarded dispatch to the BPF calc_cost callbacks during bio charging. This modifies reachable core block layer, cgroup, and BPF logic, making it worth fuzzing for potential concurrency, refcounting, or type validation bugs.",
"WorthFuzzing": true
} |
| 1/1 | 2026/09/14 07:59 | action | read-patch-diff |
0mArgs:null Results: {
"PatchDiff": "commit 7af58b5c104951da88ad0e3e0a7f79c1b30e46ca\nAuthor: syz-cluster \u003ctriage@syzkaller.com\u003e\nDate: Mon Sep 14 07:59:42 2026 +0000\n\n syz-cluster: applied patch under review\n\ndiff --git a/Documentation/admin-guide/cgroup-v2.rst b/Documentation/admin-guide/cgroup-v2.rst\nindex 8d2603751c51a..0d2d0525255cb 100644\n--- a/Documentation/admin-guide/cgroup-v2.rst\n+++ b/Documentation/admin-guide/cgroup-v2.rst\n@@ -2124,6 +2124,18 @@ IO Interface Files\n \tparameters are written to, \"ctrl\" become \"user\" and the\n \tautomatic changes are disabled.\n \n+\tWhen CONFIG_BLK_CGROUP_IOCOST_BPF is enabled, \"model\" also accepts\n+\tthe name of a registered iocost_model_ops BPF struct_ops model:\n+\t\"model=\u003cname\u003e\" binds the model to the device and the model fully\n+\treplaces the builtin linear pricing, for every operation including\n+\tflushes; \"model=linear\" (or \"ctrl=auto/user\") restores the builtin\n+\tmodel. Writing an unknown name fails with ENOENT and nothing is\n+\tapplied. Unregistering a model removes its name so it can no\n+\tlonger be selected; devices already bound keep using it, and keep\n+\treceiving cgroup lifecycle notifications, until switched back to\n+\tthe builtin model. See include/linux/blk-iocost.h\n+\tfor the model interface.\n+\n \tWhen \"model\" is \"linear\", the following model parameters are\n \tdefined.\n \ndiff --git a/block/Kconfig b/block/Kconfig\nindex 70e4a66d941ff..91e808f86d28d 100644\n--- a/block/Kconfig\n+++ b/block/Kconfig\n@@ -231,4 +231,13 @@ config BLK_ERROR_INJECTION\n \n source \"block/Kconfig.iosched\"\n \n+config BLK_CGROUP_IOCOST_BPF\n+\tbool \"Enable BPF pluggable cost model support for the cost IO controller\"\n+\tdepends on BLK_CGROUP_IOCOST \u0026\u0026 BPF_SYSCALL \u0026\u0026 BPF_JIT \u0026\u0026 DEBUG_INFO_BTF\n+\thelp\n+\t Enabling this option registers the \"iocost_model_ops\" BPF\n+\t struct_ops type, which allows a BPF program to fully replace\n+\t the builtin linear cost model on a device it is bound to\n+\t through io.cost.model.\n+\n endif # BLOCK\ndiff --git a/block/Makefile b/block/Makefile\nindex e7bd320e3d697..ee5cebeea006f 100644\n--- a/block/Makefile\n+++ b/block/Makefile\n@@ -39,3 +39,4 @@ obj-$(CONFIG_BLK_INLINE_ENCRYPTION)\t+= blk-crypto.o blk-crypto-profile.o \\\n \t\t\t\t\t blk-crypto-sysfs.o\n obj-$(CONFIG_BLK_INLINE_ENCRYPTION_FALLBACK)\t+= blk-crypto-fallback.o\n obj-$(CONFIG_BLOCK_HOLDER_DEPRECATED)\t+= holder.o\n+obj-$(CONFIG_BLK_CGROUP_IOCOST_BPF)\t+= blk-iocost-bpf.o\ndiff --git a/block/blk-cgroup.c b/block/blk-cgroup.c\nindex 2b5c29434e426..872871045351c 100644\n--- a/block/blk-cgroup.c\n+++ b/block/blk-cgroup.c\n@@ -32,6 +32,7 @@\n #include \u003clinux/part_stat.h\u003e\n #include \"blk.h\"\n #include \"blk-cgroup.h\"\n+#include \u003clinux/blk-iocost.h\u003e\n #include \"blk-ioprio.h\"\n #include \"blk-throttle.h\"\n \n@@ -1341,6 +1342,7 @@ void blkcg_unpin_online(struct cgroup_subsys_state *blkcg_css)\n */\n static void blkcg_css_offline(struct cgroup_subsys_state *css)\n {\n+\tiocost_notify_blkcg_offline(css_to_blkcg(css));\n \t/* this prevents anyone from attaching or migrating to this blkcg */\n \twb_blkcg_offline(css);\n \n@@ -1445,6 +1447,7 @@ blkcg_css_alloc(struct cgroup_subsys_state *parent_css)\n \n static int blkcg_css_online(struct cgroup_subsys_state *css)\n {\n+\tiocost_notify_blkcg_online(css_to_blkcg(css));\n \tstruct blkcg *parent = blkcg_parent(css_to_blkcg(css));\n \n \t/*\ndiff --git a/block/blk-iocost-bpf.c b/block/blk-iocost-bpf.c\nnew file mode 100644\nindex 0000000000000..7ad88600c203a\n--- /dev/null\n+++ b/block/blk-iocost-bpf.c\n@@ -0,0 +1,307 @@\n+// SPDX-License-Identifier: GPL-2.0\n+/*\n+ * blk-iocost: BPF struct_ops plumbing for pluggable cost models.\n+ *\n+ * Registers the \"iocost_model_ops\" struct_ops type and maintains the\n+ * name registry of registered models. A registered model is bound to\n+ * a device through io.cost.model; see include/linux/blk-iocost.h.\n+ */\n+#include \u003clinux/init.h\u003e\n+#include \u003clinux/kernel.h\u003e\n+#include \u003clinux/module.h\u003e\n+#include \u003clinux/mutex.h\u003e\n+#include \u003clinux/slab.h\u003e\n+#include \u003clinux/bpf.h\u003e\n+#include \u003clinux/bpf_verifier.h\u003e\n+#include \u003clinux/btf.h\u003e\n+#include \u003clinux/blk-iocost.h\u003e\n+\n+static DEFINE_MUTEX(iocost_bpf_reg_lock);\n+static LIST_HEAD(iocost_bpf_models);\n+static LIST_HEAD(iocost_bpf_lifecycle);\n+\n+/*\n+ * The registry holds a bpf_struct_ops_get() reference obtained in .reg;\n+ * .unreg drops it, so the kdata of an unregistered model stays alive\n+ * while any device is still bound to it.\n+ */\n+struct iocost_bpf_model {\n+\tstruct list_head\tlist;\t\t/* name registry */\n+\tstruct list_head\tlifecycle;\t/* lifecycle notify list */\n+\tconst struct iocost_model_ops *ops;\n+\trefcount_t\t\trefs;\n+};\n+\n+/*\n+ * Look up a registered model by name and acquire a reference on it.\n+ * The registry lock is held across lookup and bpf_struct_ops_get() so\n+ * the model cannot be unregistered in between.\n+ */\n+int iocost_bpf_model_get(const char *name,\n+\t\t\t const struct iocost_model_ops **opsp)\n+{\n+\tstruct iocost_bpf_model *m;\n+\tint ret = -ENOENT;\n+\n+\tmutex_lock(\u0026iocost_bpf_reg_lock);\n+\tlist_for_each_entry(m, \u0026iocost_bpf_models, list) {\n+\t\tif (!strcmp(m-\u003eops-\u003ename, name)) {\n+\t\t\tif (bpf_struct_ops_get(m-\u003eops)) {\n+\t\t\t\trefcount_inc(\u0026m-\u003erefs);\n+\t\t\t\t*opsp = m-\u003eops;\n+\t\t\t\tret = 0;\n+\t\t\t}\n+\t\t\tbreak;\n+\t\t}\n+\t}\n+\tmutex_unlock(\u0026iocost_bpf_reg_lock);\n+\treturn ret;\n+}\n+\n+static struct iocost_bpf_model *\n+iocost_bpf_model_lookup(const struct iocost_model_ops *ops)\n+{\n+\tstruct iocost_bpf_model *m;\n+\n+\tlist_for_each_entry(m, \u0026iocost_bpf_models, list) {\n+\t\tif (m-\u003eops == ops)\n+\t\t\treturn m;\n+\t}\n+\treturn NULL;\n+}\n+\n+/*\n+ * Lifecycle notifications walk the lifecycle list, which keeps a node\n+ * for as long as any device has the model bound, so an unregistered\n+ * but still-bound model keeps receiving blkcg online/offline.\n+ */\n+void iocost_notify_blkcg_online(struct blkcg *blkcg)\n+{\n+\tstruct iocost_bpf_model *m;\n+\n+\tguard(mutex)(\u0026iocost_bpf_reg_lock);\n+\tlist_for_each_entry(m, \u0026iocost_bpf_lifecycle, lifecycle) {\n+\t\tif (m-\u003eops-\u003eblkcg_online)\n+\t\t\tm-\u003eops-\u003eblkcg_online(blkcg);\n+\t}\n+}\n+\n+void iocost_notify_blkcg_offline(struct blkcg *blkcg)\n+{\n+\tstruct iocost_bpf_model *m;\n+\n+\tguard(mutex)(\u0026iocost_bpf_reg_lock);\n+\tlist_for_each_entry(m, \u0026iocost_bpf_lifecycle, lifecycle) {\n+\t\tif (m-\u003eops-\u003eblkcg_offline)\n+\t\t\tm-\u003eops-\u003eblkcg_offline(blkcg);\n+\t}\n+}\n+\n+static int bpf_iocost_model_init(struct btf *btf)\n+{\n+\ts32 type_id;\n+\n+\ttype_id = btf_find_by_name_kind(btf, \"iocost_model_ops\", BTF_KIND_STRUCT);\n+\tif (type_id \u003c 0)\n+\t\treturn -EINVAL;\n+\treturn 0;\n+}\n+\n+static bool bpf_iocost_is_valid_access(int off, int size,\n+\t\t\t\t enum bpf_access_type type,\n+\t\t\t\t const struct bpf_prog *prog,\n+\t\t\t\t struct bpf_insn_access_aux *info)\n+{\n+\treturn bpf_tracing_btf_ctx_access(off, size, type, prog, info);\n+}\n+\n+static const struct bpf_func_proto *\n+bpf_iocost_get_func_proto(enum bpf_func_id func_id,\n+\t\t\t const struct bpf_prog *prog)\n+{\n+\tswitch (func_id) {\n+#ifdef CONFIG_CGROUPS\n+\tcase BPF_FUNC_cgrp_storage_get:\n+\t\treturn \u0026bpf_cgrp_storage_get_proto;\n+#endif\n+\tdefault:\n+\t\treturn bpf_base_func_proto(func_id, prog);\n+\t}\n+}\n+\n+static int bpf_iocost_check_member(const struct btf_type *t,\n+\t\t\t\t const struct btf_member *member,\n+\t\t\t\t const struct bpf_prog *prog)\n+{\n+\t/* calc_cost() is called with RCU read lock held */\n+\tif (prog-\u003esleepable)\n+\t\treturn -EINVAL;\n+\treturn 0;\n+}\n+\n+static int bpf_iocost_init_member(const struct btf_type *t,\n+\t\t\t\t const struct btf_member *member,\n+\t\t\t\t void *kdata, const void *udata)\n+{\n+\tstruct iocost_model_ops *ops = kdata;\n+\tconst struct iocost_model_ops *uops = udata;\n+\tu32 moff = __btf_member_bit_offset(t, member) / 8;\n+\n+\tswitch (moff) {\n+\tcase offsetof(struct iocost_model_ops, name):\n+\t\tif (bpf_obj_name_cpy(ops-\u003ename, uops-\u003ename,\n+\t\t\t\t sizeof(ops-\u003ename)) \u003c= 0)\n+\t\t\treturn -EINVAL;\n+\t\treturn 1;\n+\t}\n+\n+\treturn 0;\n+}\n+\n+static u64 bpf_iocost_calc_cost_stub(u64 opf, u64 nbytes, u64 sector,\n+\t\t\t\t struct blkcg *blkcg, u64 model_flags);\n+\n+/*\n+ * kdata is seeded from the CFI stubs, so calc_cost is never NULL; a\n+ * model which did not implement it inherits the stub, which prices\n+ * every IO at 0. Compare against the stub to reject it.\n+ */\n+static int bpf_iocost_validate(void *kdata)\n+{\n+\tstruct iocost_model_ops *ops = kdata;\n+\n+\tif (ops-\u003ecalc_cost == bpf_iocost_calc_cost_stub)\n+\t\treturn -EINVAL;\n+\treturn 0;\n+}\n+\n+static int bpf_iocost_reg(void *kdata, struct bpf_link *link)\n+{\n+\tstruct iocost_model_ops *ops = kdata;\n+\tstruct iocost_bpf_model *m;\n+\tint ret = 0;\n+\n+\tif (!bpf_struct_ops_get(ops))\n+\t\treturn -ENOENT;\n+\n+\tm = kzalloc(sizeof(*m), GFP_KERNEL);\n+\tif (!m) {\n+\t\tbpf_struct_ops_put(ops);\n+\t\treturn -ENOMEM;\n+\t}\n+\trefcount_set(\u0026m-\u003erefs, 1);\n+\n+\tmutex_lock(\u0026iocost_bpf_reg_lock);\n+\t{\n+\t\tstruct iocost_bpf_model *other;\n+\n+\t\tlist_for_each_entry(other, \u0026iocost_bpf_models, list) {\n+\t\t\tif (!strcmp(other-\u003eops-\u003ename, ops-\u003ename)) {\n+\t\t\t\tret = -EEXIST;\n+\t\t\t\tbreak;\n+\t\t\t}\n+\t\t}\n+\t}\n+\tif (!ret) {\n+\t\tm-\u003eops = ops;\n+\t\tlist_add(\u0026m-\u003elist, \u0026iocost_bpf_models);\n+\t\tlist_add(\u0026m-\u003elifecycle, \u0026iocost_bpf_lifecycle);\n+\t}\n+\tmutex_unlock(\u0026iocost_bpf_reg_lock);\n+\n+\tif (ret) {\n+\t\tbpf_struct_ops_put(ops);\n+\t\tkfree(m);\n+\t}\n+\treturn ret;\n+}\n+\n+/*\n+ * Unregistering drops the registration reference. When the last\n+ * reference is gone (no device bound), the node leaves the lifecycle\n+ * list and is freed; otherwise bound devices keep it alive and it\n+ * keeps receiving blkcg online/offline notifications.\n+ */\n+static void bpf_iocost_unreg(void *kdata, struct bpf_link *link)\n+{\n+\tstruct iocost_model_ops *ops = kdata;\n+\tstruct iocost_bpf_model *m;\n+\n+\tmutex_lock(\u0026iocost_bpf_reg_lock);\n+\tm = iocost_bpf_model_lookup(ops);\n+\tif (m) {\n+\t\tlist_del(\u0026m-\u003elist);\n+\t\tif (refcount_dec_and_test(\u0026m-\u003erefs)) {\n+\t\t\tlist_del(\u0026m-\u003elifecycle);\n+\t\t\tkfree(m);\n+\t\t}\n+\t}\n+\tmutex_unlock(\u0026iocost_bpf_reg_lock);\n+\n+\tbpf_struct_ops_put(ops);\n+}\n+\n+void iocost_bpf_model_put(const struct iocost_model_ops *ops)\n+{\n+\tstruct iocost_bpf_model *m;\n+\n+\tmutex_lock(\u0026iocost_bpf_reg_lock);\n+\tlist_for_each_entry(m, \u0026iocost_bpf_lifecycle, lifecycle) {\n+\t\tif (m-\u003eops == ops)\n+\t\t\tbreak;\n+\t}\n+\t/*\n+\t * Exactly one bpf_struct_ops_put() per call, pairing the\n+\t * bpf_struct_ops_get() in iocost_bpf_model_get(); the node is\n+\t * freed when the last reference goes, whichever side drops it.\n+\t */\n+\tif (\u0026m-\u003elifecycle != \u0026iocost_bpf_lifecycle \u0026\u0026\n+\t refcount_dec_and_test(\u0026m-\u003erefs)) {\n+\t\tlist_del(\u0026m-\u003elifecycle);\n+\t\tmutex_unlock(\u0026iocost_bpf_reg_lock);\n+\t\tkfree(m);\n+\t\tbpf_struct_ops_put(ops);\n+\t\treturn;\n+\t}\n+\tmutex_unlock(\u0026iocost_bpf_reg_lock);\n+\tbpf_struct_ops_put(ops);\n+}\n+\n+static const struct bpf_verifier_ops bpf_iocost_verifier_ops = {\n+\t.get_func_proto = bpf_iocost_get_func_proto,\n+\t.is_valid_access = bpf_iocost_is_valid_access,\n+};\n+\n+static u64 bpf_iocost_calc_cost_stub(u64 opf, u64 nbytes, u64 sector,\n+\t\t\t\t struct blkcg *blkcg, u64 flags)\n+{\n+\treturn 0;\n+}\n+\n+static void bpf_iocost_blkcg_online_stub(struct blkcg *blkcg) { }\n+static void bpf_iocost_blkcg_offline_stub(struct blkcg *blkcg) { }\n+\n+static struct iocost_model_ops __bpf_ops_iocost_model_ops = {\n+\t.calc_cost = bpf_iocost_calc_cost_stub,\n+\t.blkcg_online = bpf_iocost_blkcg_online_stub,\n+\t.blkcg_offline = bpf_iocost_blkcg_offline_stub,\n+};\n+\n+static struct bpf_struct_ops bpf_iocost_model_ops = {\n+\t.verifier_ops = \u0026bpf_iocost_verifier_ops,\n+\t.init = bpf_iocost_model_init,\n+\t.check_member = bpf_iocost_check_member,\n+\t.init_member = bpf_iocost_init_member,\n+\t.validate = bpf_iocost_validate,\n+\t.reg = bpf_iocost_reg,\n+\t.unreg = bpf_iocost_unreg,\n+\t.name = \"iocost_model_ops\",\n+\t.cfi_stubs = \u0026__bpf_ops_iocost_model_ops,\n+\t.owner = THIS_MODULE,\n+};\n+\n+static int __init bpf_iocost_init(void)\n+{\n+\treturn register_bpf_struct_ops(\u0026bpf_iocost_model_ops, iocost_model_ops);\n+}\n+late_initcall(bpf_iocost_init);\ndiff --git a/block/blk-iocost.c b/block/blk-iocost.c\nindex 2745bffcd5eef..96126a185f9a6 100644\n--- a/block/blk-iocost.c\n+++ b/block/blk-iocost.c\n@@ -177,6 +177,7 @@\n #include \u003clinux/timer.h\u003e\n #include \u003clinux/time64.h\u003e\n #include \u003clinux/parser.h\u003e\n+#include \u003clinux/blk-iocost.h\u003e\n #include \u003clinux/sched/signal.h\u003e\n #include \u003casm/local.h\u003e\n #include \u003casm/local64.h\u003e\n@@ -445,6 +446,11 @@ struct ioc {\n \tint\t\t\t\tautop_idx;\n \tbool\t\t\t\tuser_qos_params:1;\n \tbool\t\t\t\tuser_cost_model:1;\n+\n+#ifdef CONFIG_BLK_CGROUP_IOCOST_BPF\n+\t/* bound BPF cost model, NULL = builtin linear model */\n+\tconst struct iocost_model_ops\t__rcu *model;\n+#endif\n };\n \n struct iocg_pcpu_stat {\n@@ -2238,6 +2244,7 @@ static void ioc_timer_fn(struct timer_list *timer)\n \tstruct ioc_now now;\n \tLIST_HEAD(surpluses);\n \tint nr_debtors, nr_shortages = 0, nr_lagging = 0;\n+\tint nr_active = 0;\n \tu64 usage_us_sum = 0;\n \tu32 ppm_rthr;\n \tu32 ppm_wthr;\n@@ -2274,6 +2281,8 @@ static void ioc_timer_fn(struct timer_list *timer)\n \t\tu64 vdone, vtime, usage_us;\n \t\tu32 hw_active, hw_inuse;\n \n+\t\tnr_active++;\n+\n \t\t/*\n \t\t * Collect unused and wind vtime closer to vnow to prevent\n \t\t * iocgs from accumulating a large amount of budget.\n@@ -2435,6 +2444,14 @@ static void ioc_timer_fn(struct timer_list *timer)\n \n \tioc-\u003ebusy_level = clamp(ioc-\u003ebusy_level, -1000, 1000);\n \n+\t/*\n+\t * vrate and period_us change right below; snapshot the values\n+\t * this period ran in so the tick below reports the period's own\n+\t * parameters instead of the next period's.\n+\t */\n+\tu32 tick_period_us = ioc-\u003eperiod_us;\n+\tu64 tick_vrate = ioc-\u003evtime_base_rate;\n+\n \tioc_adjust_base_vrate(ioc, rq_wait_pct, nr_lagging, nr_shortages,\n \t\t\t prev_busy_level, missed_ppm);\n \n@@ -2448,16 +2465,38 @@ static void ioc_timer_fn(struct timer_list *timer)\n \t */\n \tatomic64_inc(\u0026ioc-\u003ecur_period);\n \n-\tif (ioc-\u003erunning != IOC_STOP) {\n-\t\tif (!list_empty(\u0026ioc-\u003eactive_iocgs)) {\n-\t\t\tioc_start_period(ioc, \u0026now);\n-\t\t} else {\n-\t\t\tioc-\u003ebusy_level = 0;\n-\t\t\tioc-\u003evtime_err = 0;\n-\t\t\tioc-\u003erunning = IOC_IDLE;\n-\t\t}\n+\t/*\n+\t * Snapshot the state this period ran in before the idle\n+\t * transition wipes it, so the final tick reports the period's\n+\t * own busy level (e.g. the saturation that drove the controller\n+\t * idle) instead of the cleared one. usage is normalized by the\n+\t * measured period length, captured before ioc_start_period()\n+\t * overwrites period_at, the same way the donation loop does.\n+\t */\n+\t{\n+\t\tint tick_busy = ioc-\u003ebusy_level;\n+\t\tint tick_running = ioc-\u003erunning;\n+\t\tu64 tick_dur = now.now - ioc-\u003eperiod_at;\n+\t\t/* cur_period was already advanced to N+1 above; report\n+\t\t * the period that just ended, like the other fields */\n+\t\tu64 tick_period = atomic64_read(\u0026ioc-\u003ecur_period) - 1;\n+\n+\t\ttrace_iocost_ioc_tick(ioc, nr_active, usage_us_sum,\n+\t\t\t\t tick_period, tick_period_us,\n+\t\t\t\t tick_vrate, tick_busy, tick_running,\n+\t\t\t\t tick_dur);\n+\n+\t\tif (ioc-\u003erunning != IOC_STOP) {\n+\t\t\tif (!list_empty(\u0026ioc-\u003eactive_iocgs)) {\n+\t\t\t\tioc_start_period(ioc, \u0026now);\n+\t\t\t} else {\n+\t\t\t\tioc-\u003ebusy_level = 0;\n+\t\t\t\tioc-\u003evtime_err = 0;\n+\t\t\t\tioc-\u003erunning = IOC_IDLE;\n+\t\t\t}\n \n-\t\tioc_refresh_vrate(ioc, \u0026now);\n+\t\t\tioc_refresh_vrate(ioc, \u0026now);\n+\t\t}\n \t}\n \n \tspin_unlock_irq(\u0026ioc-\u003elock);\n@@ -2571,10 +2610,28 @@ static void calc_vtime_cost_builtin(struct bio *bio, struct ioc_gq *iocg,\n \n static u64 calc_vtime_cost(struct bio *bio, struct ioc_gq *iocg, bool is_merge)\n {\n+#ifdef CONFIG_BLK_CGROUP_IOCOST_BPF\n+\tconst struct iocost_model_ops *model;\n \tu64 cost;\n \n-\tcalc_vtime_cost_builtin(bio, iocg, is_merge, \u0026cost);\n-\treturn cost;\n+\trcu_read_lock();\n+\tmodel = rcu_dereference(iocg-\u003eioc-\u003emodel);\n+\tif (model) {\n+\t\tcost = model-\u003ecalc_cost(bio-\u003ebi_opf, bio-\u003ebi_iter.bi_size,\n+\t\t\t\t\tbio-\u003ebi_iter.bi_sector,\n+\t\t\t\t\tiocg_to_blkg(iocg)-\u003eblkcg,\n+\t\t\t\t\tis_merge ? IOCOST_COST_F_MERGE : 0);\n+\t\trcu_read_unlock();\n+\t\treturn min(cost, VTIME_PER_SEC);\n+\t}\n+\trcu_read_unlock();\n+#endif\n+\t{\n+\t\tu64 cost;\n+\n+\t\tcalc_vtime_cost_builtin(bio, iocg, is_merge, \u0026cost);\n+\t\treturn cost;\n+\t}\n }\n \n static void calc_size_vtime_cost_builtin(struct request *rq, struct ioc *ioc,\n@@ -2596,10 +2653,28 @@ static void calc_size_vtime_cost_builtin(struct request *rq, struct ioc *ioc,\n \n static u64 calc_size_vtime_cost(struct request *rq, struct ioc *ioc)\n {\n-\tu64 cost;\n+#ifdef CONFIG_BLK_CGROUP_IOCOST_BPF\n+\tconst struct iocost_model_ops *model;\n \n-\tcalc_size_vtime_cost_builtin(rq, ioc, \u0026cost);\n-\treturn cost;\n+\trcu_read_lock();\n+\tmodel = rcu_dereference(ioc-\u003emodel);\n+\tif (model \u0026\u0026 rq-\u003ebio \u0026\u0026 rq-\u003ebio-\u003ebi_blkg) {\n+\t\tu64 cost;\n+\n+\t\tcost = model-\u003ecalc_cost(rq-\u003ecmd_flags, blk_rq_bytes(rq),\n+\t\t\t\t\tblk_rq_pos(rq),\n+\t\t\t\t\trq-\u003ebio-\u003ebi_blkg-\u003eblkcg, 0);\n+\t\trcu_read_unlock();\n+\t\treturn min(cost, VTIME_PER_SEC);\n+\t}\n+\trcu_read_unlock();\n+#endif\n+\t{\n+\t\tu64 cost;\n+\n+\t\tcalc_size_vtime_cost_builtin(rq, ioc, \u0026cost);\n+\t\treturn cost;\n+\t}\n }\n \n enum over_budget_action {\n@@ -2900,6 +2975,19 @@ static void ioc_rqos_exit(struct rq_qos *rqos)\n \n \ttimer_shutdown_sync(\u0026ioc-\u003etimer);\n \tfree_percpu(ioc-\u003epcpu_stat);\n+#ifdef CONFIG_BLK_CGROUP_IOCOST_BPF\n+\t{\n+\t\tconst struct iocost_model_ops *model;\n+\n+\t\tspin_lock_irq(\u0026ioc-\u003elock);\n+\t\tmodel = rcu_dereference_protected(ioc-\u003emodel,\n+\t\t\t\t\tlockdep_is_held(\u0026ioc-\u003elock));\n+\t\trcu_assign_pointer(ioc-\u003emodel, NULL);\n+\t\tspin_unlock_irq(\u0026ioc-\u003elock);\n+\t\tif (model)\n+\t\t\tiocost_bpf_model_put(model);\n+\t}\n+#endif\n \tkfree(ioc);\n }\n \n@@ -3438,12 +3526,30 @@ static u64 ioc_cost_model_prfill(struct seq_file *sf,\n \t\treturn 0;\n \n \tspin_lock_irq(\u0026ioc-\u003elock);\n+#ifdef CONFIG_BLK_CGROUP_IOCOST_BPF\n+\t{\n+\t\tconst struct iocost_model_ops *model =\n+\t\t\trcu_dereference_protected(ioc-\u003emodel,\n+\t\t\t\t\tlockdep_is_held(\u0026ioc-\u003elock));\n+\n+\t\tseq_printf(sf, \"%s ctrl=%s model=%s \"\n+\t\t\t \"rbps=%llu rseqiops=%llu rrandiops=%llu \"\n+\t\t\t \"wbps=%llu wseqiops=%llu wrandiops=%llu\\n\",\n+\t\t\t dname, model ? \"bpf\" :\n+\t\t\t\t ioc-\u003euser_cost_model ? \"user\" : \"auto\",\n+\t\t\t model ? model-\u003ename : \"linear\",\n+\t\t\t u[I_LCOEF_RBPS], u[I_LCOEF_RSEQIOPS],\n+\t\t\t u[I_LCOEF_RRANDIOPS], u[I_LCOEF_WBPS],\n+\t\t\t u[I_LCOEF_WSEQIOPS], u[I_LCOEF_WRANDIOPS]);\n+\t}\n+#else\n \tseq_printf(sf, \"%s ctrl=%s model=linear \"\n \t\t \"rbps=%llu rseqiops=%llu rrandiops=%llu \"\n \t\t \"wbps=%llu wseqiops=%llu wrandiops=%llu\\n\",\n \t\t dname, ioc-\u003euser_cost_model ? \"user\" : \"auto\",\n \t\t u[I_LCOEF_RBPS], u[I_LCOEF_RSEQIOPS], u[I_LCOEF_RRANDIOPS],\n \t\t u[I_LCOEF_WBPS], u[I_LCOEF_WSEQIOPS], u[I_LCOEF_WRANDIOPS]);\n+#endif\n \tspin_unlock_irq(\u0026ioc-\u003elock);\n \treturn 0;\n }\n@@ -3457,6 +3563,52 @@ static int ioc_cost_model_show(struct seq_file *sf, void *v)\n \treturn 0;\n }\n \n+/*\n+ * Resolve the model name and take a reference on the new model before\n+ * anything is applied, so a bad name rejects the whole write. The\n+ * registry lookup takes the registration mutex and must stay outside\n+ * ioc-\u003elock. The returned model is per-write state passed back into\n+ * ioc_bpf_model_commit(), which performs the pointer swap under\n+ * ioc-\u003elock so concurrent writers cannot interleave a half-applied\n+ * configuration, and the caller drops the old model's reference\n+ * afterwards.\n+ */\n+static const struct iocost_model_ops *\n+ioc_bpf_model_prepare(const char *name)\n+{\n+#ifdef CONFIG_BLK_CGROUP_IOCOST_BPF\n+\tconst struct iocost_model_ops *new = NULL;\n+\tint ret;\n+\n+\tif (!name[0])\n+\t\treturn NULL;\n+\tret = iocost_bpf_model_get(name, \u0026new);\n+\treturn ret ? ERR_PTR(ret) : new;\n+#else\n+\treturn name[0] ? ERR_PTR(-ENOENT) : NULL;\n+#endif\n+}\n+\n+/*\n+ * Swap in the model returned by ioc_bpf_model_prepare(). Called with\n+ * ioc-\u003elock held; dropping the old model's reference may sleep, so\n+ * the caller does it after releasing the lock.\n+ */\n+static const struct iocost_model_ops *\n+ioc_bpf_model_commit(struct ioc *ioc, const struct iocost_model_ops *new)\n+{\n+#ifdef CONFIG_BLK_CGROUP_IOCOST_BPF\n+\tconst struct iocost_model_ops *old;\n+\n+\told = rcu_dereference_protected(ioc-\u003emodel,\n+\t\t\t\t\tlockdep_is_held(\u0026ioc-\u003elock));\n+\trcu_assign_pointer(ioc-\u003emodel, new);\n+\treturn old;\n+#else\n+\treturn NULL;\n+#endif\n+}\n+\n static const match_table_t cost_ctrl_tokens = {\n \t{ COST_CTRL,\t\t\"ctrl=%s\"\t},\n \t{ COST_MODEL,\t\t\"model=%s\"\t},\n@@ -3482,6 +3634,7 @@ static ssize_t ioc_cost_model_write(struct kernfs_open_file *of, char *input,\n \tstruct ioc *ioc;\n \tu64 u[NR_I_LCOEFS];\n \tbool user;\n+\tchar bpf_model[IOCOST_MODEL_NAME_LEN];\n \tchar *body, *p;\n \tint ret;\n \n@@ -3512,6 +3665,7 @@ static ssize_t ioc_cost_model_write(struct kernfs_open_file *of, char *input,\n \tspin_lock_irq(\u0026ioc-\u003elock);\n \tmemcpy(u, ioc-\u003eparams.i_lcoefs, sizeof(u));\n \tuser = ioc-\u003euser_cost_model;\n+\tbpf_model[0] = '\\0';\n \n \tret = -EINVAL;\n \n@@ -3533,11 +3687,16 @@ static ssize_t ioc_cost_model_write(struct kernfs_open_file *of, char *input,\n \t\t\t\tuser = true;\n \t\t\telse\n \t\t\t\tgoto unlock;\n+\t\t\tbpf_model[0] = '\\0';\n \t\t\tcontinue;\n \t\tcase COST_MODEL:\n \t\t\tmatch_strlcpy(buf, \u0026args[0], sizeof(buf));\n-\t\t\tif (strcmp(buf, \"linear\"))\n-\t\t\t\tgoto unlock;\n+\t\t\tif (!strcmp(buf, \"linear\")) {\n+\t\t\t\t/* back to the builtin linear model */\n+\t\t\t\tbpf_model[0] = '\\0';\n+\t\t\t\tcontinue;\n+\t\t\t}\n+\t\t\tmatch_strlcpy(bpf_model, \u0026args[0], sizeof(bpf_model));\n \t\t\tcontinue;\n \t\t}\n \n@@ -3550,19 +3709,41 @@ static ssize_t ioc_cost_model_write(struct kernfs_open_file *of, char *input,\n \t\tuser = true;\n \t}\n \n-\tif (user) {\n-\t\tmemcpy(ioc-\u003eparams.i_lcoefs, u, sizeof(u));\n-\t\tioc-\u003euser_cost_model = true;\n-\t} else {\n-\t\tioc-\u003euser_cost_model = false;\n-\t}\n-\tioc_refresh_params(ioc, true);\n-\n \tret = 0;\n \n unlock:\n \tspin_unlock_irq(\u0026ioc-\u003elock);\n \n+\t/*\n+\t * Resolve the model name outside ioc-\u003elock (the registry lookup\n+\t * takes a mutex), so a bad name rejects the whole write before\n+\t * anything is applied. The resolved model is this write's own\n+\t * state, not per-device state, so concurrent writers cannot\n+\t * clobber each other's staged model. On success, re-take the\n+\t * lock to apply the coefficients and swap the model in one go.\n+\t */\n+\tif (!ret) {\n+\t\tconst struct iocost_model_ops *new, *old;\n+\n+\t\tnew = ioc_bpf_model_prepare(bpf_model);\n+\t\tif (IS_ERR(new)) {\n+\t\t\tret = PTR_ERR(new);\n+\t\t} else {\n+\t\t\tspin_lock_irq(\u0026ioc-\u003elock);\n+\t\t\tif (user) {\n+\t\t\t\tmemcpy(ioc-\u003eparams.i_lcoefs, u, sizeof(u));\n+\t\t\t\tioc-\u003euser_cost_model = true;\n+\t\t\t} else {\n+\t\t\t\tioc-\u003euser_cost_model = false;\n+\t\t\t}\n+\t\t\tioc_refresh_params(ioc, true);\n+\t\t\told = ioc_bpf_model_commit(ioc, new);\n+\t\t\tspin_unlock_irq(\u0026ioc-\u003elock);\n+\t\t\tif (IS_ENABLED(CONFIG_BLK_CGROUP_IOCOST_BPF) \u0026\u0026 old)\n+\t\t\t\tiocost_bpf_model_put(old);\n+\t\t}\n+\t}\n+\n \tblk_mq_unquiesce_queue(q);\n \tblk_mq_unfreeze_queue(q, memflags);\n \ndiff --git a/include/linux/blk-iocost.h b/include/linux/blk-iocost.h\nnew file mode 100644\nindex 0000000000000..d4bd2b0c826b3\n--- /dev/null\n+++ b/include/linux/blk-iocost.h\n@@ -0,0 +1,85 @@\n+/* SPDX-License-Identifier: GPL-2.0 */\n+#ifndef _LINUX_BLK_IOCOST_H\n+#define _LINUX_BLK_IOCOST_H\n+\n+#include \u003clinux/types.h\u003e\n+#include \u003clinux/blk_types.h\u003e\n+\n+#define IOCOST_MODEL_NAME_LEN\t16\n+\n+#ifdef CONFIG_BLK_CGROUP_IOCOST_BPF\n+\n+struct blkcg;\n+\n+/*\n+ * Pluggable cost model interface for blk-iocost.\n+ *\n+ * A BPF struct_ops implementation registered against \"iocost_model_ops\"\n+ * fully replaces the builtin linear model on the devices it is bound to\n+ * through io.cost.model. The model owns pricing for every IO on a bound\n+ * device: it prices all operations, including flushes, and it is called\n+ * from the bio charging path. The completion-time request sizing for\n+ * the latency met/missed accounting still uses the builtin coefficients\n+ * (the request's bio, and with it the issuing cgroup, is gone by then);\n+ * extending the model there is an open question of this interface.\n+ *\n+ * calc_cost() is called from the IO submission path with RCU read lock\n+ * held and must not sleep. It returns the cost of the IO in vtime\n+ * units, where 1 second of device time equals VTIME_PER_SEC (2^37,\n+ * available to BPF programs through vmlinux.h). The returned value is\n+ * clamped to 1 second of device time per IO.\n+ *\n+ * The model is passed the blkcg of the issuing cgroup so it can keep\n+ * per-cgroup state. State keyed by the blkcg alone is shared across\n+ * every device the model is bound to, unlike the builtin cursor which\n+ * is per (cgroup, device). blkcg_online()/blkcg_offline() are optional\n+ * callbacks mirroring the blkcg css lifecycle: state created on online\n+ * (or lazily on first use) must be released on offline.\n+ *\n+ * The registration and binding model follows the TCP congestion\n+ * control framework: registering a struct_ops makes the model available\n+ * by its name, while io.cost.model binds one registered model to a\n+ * device. Unregistering removes the name from the registry; devices\n+ * already bound keep using it until switched back to the builtin\n+ * model.\n+ */\n+\n+/*\n+ * iocost-specific call metadata for calc_cost()'s model_flags\n+ * argument; everything else, including REQ_PREFLUSH/REQ_FUA, is\n+ * already present in the opf argument\n+ */\n+#define IOCOST_COST_F_MERGE\t(1ULL \u003c\u003c 0)\t/* called from merge path */\n+\n+struct iocost_model_ops {\n+\tu64 (*calc_cost)(u64 opf, u64 nbytes, sector_t sector,\n+\t\t\t struct blkcg *blkcg, u64 model_flags);\n+\tvoid (*blkcg_online)(struct blkcg *blkcg);\n+\tvoid (*blkcg_offline)(struct blkcg *blkcg);\n+\n+\t/* model name, used to select the model through io.cost.model */\n+\tchar name[16];\n+};\n+\n+int iocost_bpf_model_get(const char *name,\n+\t\t\t const struct iocost_model_ops **opsp);\n+void iocost_bpf_model_put(const struct iocost_model_ops *ops);\n+void iocost_notify_blkcg_online(struct blkcg *blkcg);\n+void iocost_notify_blkcg_offline(struct blkcg *blkcg);\n+\n+#else\t/* CONFIG_BLK_CGROUP_IOCOST_BPF */\n+\n+struct blkcg;\n+struct iocost_model_ops;\n+\n+static inline int iocost_bpf_model_get(const char *name,\n+\t\t\t\t const struct iocost_model_ops **opsp)\n+{\n+\treturn -EOPNOTSUPP;\n+}\n+static inline void iocost_bpf_model_put(const struct iocost_model_ops *ops) { }\n+static inline void iocost_notify_blkcg_online(struct blkcg *blkcg) { }\n+static inline void iocost_notify_blkcg_offline(struct blkcg *blkcg) { }\n+\n+#endif\t/* CONFIG_BLK_CGROUP_IOCOST_BPF */\n+#endif\t/* _LINUX_BLK_IOCOST_H */\ndiff --git a/include/trace/events/iocost.h b/include/trace/events/iocost.h\nindex e772b1bc60d60..ec5d9c453d55a 100644\n--- a/include/trace/events/iocost.h\n+++ b/include/trace/events/iocost.h\n@@ -178,6 +178,52 @@ TRACE_EVENT(iocost_ioc_vrate_adj,\n \t)\n );\n \n+/*\n+ * Periodic per-device summary, emitted once per period from the tail of\n+ * ioc_timer_fn(). Unlike the state-change events above, this fires every\n+ * period the controller is running, including steady states, and carries\n+ * the overall controller state so basic monitoring doesn't require drgn.\n+ */\n+TRACE_EVENT(iocost_ioc_tick,\n+\n+\tTP_PROTO(struct ioc *ioc, int nr_active, u64 usage_us_sum,\n+\t\t u64 cur_period, u32 tick_period_us, u64 tick_vrate,\n+\t\t int tick_busy, int tick_running, u64 tick_dur),\n+\n+\tTP_ARGS(ioc, nr_active, usage_us_sum, cur_period,\n+\t\ttick_period_us, tick_vrate,\n+\t\ttick_busy, tick_running, tick_dur),\n+\n+\tTP_STRUCT__entry (\n+\t\t__string(devname, ioc_name(ioc))\n+\t\t__field(u64, cur_period)\n+\t\t__field(u32, period_us)\n+\t\t__field(u64, vrate)\n+\t\t__field(int, busy_level)\n+\t\t__field(int, nr_active)\n+\t\t__field(u32, usage_pct)\n+\t\t__field(int, running)\n+\t),\n+\n+\tTP_fast_assign(\n+\t\t__assign_str(devname);\n+\t\t__entry-\u003ecur_period = cur_period;\n+\t\t__entry-\u003eperiod_us = tick_period_us;\n+\t\t__entry-\u003evrate = tick_vrate;\n+\t\t__entry-\u003ebusy_level = tick_busy;\n+\t\t__entry-\u003enr_active = nr_active;\n+\t\t__entry-\u003eusage_pct = tick_dur ?\n+\t\t\tdiv_u64(usage_us_sum * 100, tick_dur) : 0;\n+\t\t__entry-\u003erunning = tick_running;\n+\t),\n+\n+\tTP_printk(\"[%s] period=%llu:%uus vrate=%llu busy=%d active=%d usage=%u%% running=%d\",\n+\t\t__get_str(devname), __entry-\u003ecur_period, __entry-\u003eperiod_us,\n+\t\t__entry-\u003evrate, __entry-\u003ebusy_level, __entry-\u003enr_active,\n+\t\t__entry-\u003eusage_pct, __entry-\u003erunning\n+\t)\n+);\n+\n TRACE_EVENT(iocost_iocg_forgive_debt,\n \n \tTP_PROTO(struct ioc_gq *iocg, const char *path, struct ioc_now *now,\ndiff --git a/tools/testing/selftests/bpf/config b/tools/testing/selftests/bpf/config\nindex 2f79688dcf7ce..67a630cb56148 100644\n--- a/tools/testing/selftests/bpf/config\n+++ b/tools/testing/selftests/bpf/config\n@@ -138,3 +138,5 @@ CONFIG_SMC_HS_CTRL_BPF=y\n CONFIG_DIBS=y\n CONFIG_DIBS_LO=y\n CONFIG_PM_WAKELOCKS=y\n+CONFIG_BLK_CGROUP_IOCOST=y\n+CONFIG_BLK_CGROUP_IOCOST_BPF=y\ndiff --git a/tools/testing/selftests/bpf/prog_tests/iocost_model.c b/tools/testing/selftests/bpf/prog_tests/iocost_model.c\nnew file mode 100644\nindex 0000000000000..db0a74b1885be\n--- /dev/null\n+++ b/tools/testing/selftests/bpf/prog_tests/iocost_model.c\n@@ -0,0 +1,200 @@\n+// SPDX-License-Identifier: GPL-2.0\n+#include \u003ctest_progs.h\u003e\n+#include \u003cctype.h\u003e\n+#include \u003cfcntl.h\u003e\n+#include \u003cunistd.h\u003e\n+#include \"iocost_model.skel.h\"\n+#include \"iocost_ms.skel.h\"\n+\n+/*\n+ * Write a line to io.cost.model with write(2) and return the errno of\n+ * the failed write, or 0 on success. stdio is not used here on\n+ * purpose: the kernel's rejection happens in the write() syscall,\n+ * not in the userspace buffer copy, and every write, including\n+ * the error paths of the callers below, is checked.\n+ */\n+static int write_cost_model(const char *buf)\n+{\n+\tint fd, err = 0;\n+\tssize_t n;\n+\n+\tfd = open(\"/sys/fs/cgroup/io.cost.model\", O_WRONLY);\n+\tif (fd \u003c 0)\n+\t\treturn -errno;\t/* negative: the file is not there */\n+\tn = write(fd, buf, strlen(buf));\n+\tif (n \u003c 0)\n+\t\terr = errno;\n+\tclose(fd);\n+\treturn err;\n+}\n+\n+/*\n+ * Read back the io.cost.model line of dev and copy the model= value\n+ * into @model. Returns 0 on success.\n+ */\n+static int readback_model(const char *dev, char *model, size_t model_sz)\n+{\n+\tchar line[256], word[256], *m, *end;\n+\tFILE *fp;\n+\tint found = 0;\n+\n+\tfp = fopen(\"/sys/fs/cgroup/io.cost.model\", \"r\");\n+\tif (!fp)\n+\t\treturn -1;\n+\twhile (fgets(line, sizeof(line), fp)) {\n+\t\tif (sscanf(line, \"%255s\", word) == 1 \u0026\u0026 !strcmp(word, dev)) {\n+\t\t\tfound = 1;\n+\t\t\tbreak;\n+\t\t}\n+\t}\n+\tfclose(fp);\n+\tif (!found)\n+\t\treturn -1;\n+\n+\tm = strstr(line, \"model=\");\n+\tif (!m)\n+\t\treturn -1;\n+\tm += strlen(\"model=\");\n+\tend = m;\n+\twhile (*end \u0026\u0026 !isspace(*end))\n+\t\tend++;\n+\tsnprintf(model, model_sz, \"%.*s\", (int)(end - m), m);\n+\treturn 0;\n+}\n+\n+/*\n+ * Bind the named model to a device with \"model=\u003cname\u003e\", verify the\n+ * readback and restore the builtin model. Returns 0 on success.\n+ */\n+static int bind_model(const char *dev, const char *name)\n+{\n+\tchar buf[300], got[64];\n+\tint err;\n+\n+\tsnprintf(buf, sizeof(buf), \"%s model=%s\\n\", dev, name);\n+\terr = write_cost_model(buf);\n+\tif (err)\n+\t\treturn err \u003e 0 ? -err : -EIO;\n+\terr = readback_model(dev, got, sizeof(got));\n+\tif (!err \u0026\u0026 !strcmp(got, name)) {\n+\t\t/* restore the builtin linear model on every path so the\n+\t\t * device is not left bound to a model the caller may\n+\t\t * unregister right after; the write is checked too */\n+\t\tsnprintf(buf, sizeof(buf), \"%s model=linear\\n\", dev);\n+\t\terr = write_cost_model(buf);\n+\t\treturn err \u003e 0 ? -err : (err ? -EIO : 0);\n+\t}\n+\n+\t/* bind failed or does not read back: still restore; prefer\n+\t * the original failure, but surface the restore error too */\n+\tsnprintf(buf, sizeof(buf), \"%s model=linear\\n\", dev);\n+\t{\n+\t\tint rerr = write_cost_model(buf);\n+\n+\t\treturn err ? (err \u003e 0 ? -err : -EIO)\n+\t\t\t : (rerr \u003e 0 ? -rerr : -EIO);\n+\t}\n+}\n+\n+/*\n+ * True when the device line exists in io.cost.qos and iocost is\n+ * enabled on it.\n+ */\n+static int dev_has_iocost(const char *dev)\n+{\n+\tchar line[256], word[256];\n+\tFILE *fp;\n+\tint found = 0;\n+\n+\tfp = fopen(\"/sys/fs/cgroup/io.cost.qos\", \"r\");\n+\tif (!fp)\n+\t\treturn 0;\n+\twhile (fgets(line, sizeof(line), fp)) {\n+\t\tif (sscanf(line, \"%255s\", word) == 1 \u0026\u0026 !strcmp(word, dev)) {\n+\t\t\tfound = strstr(line, \"enable=1\") != NULL;\n+\t\t\tbreak;\n+\t\t}\n+\t}\n+\tfclose(fp);\n+\treturn found;\n+}\n+\n+/*\n+ * Bind the 2x example model and verify the io.cost.model readback.\n+ * IO accounting itself is not checked here; it needs a device doing\n+ * real IO under iocost and is covered by the kernel-side validation\n+ * described in the cover letter.\n+ *\n+ * Requires root, cgroup v2 and a device with iocost support. The\n+ * device must be given as major:minor in $IOCOST_TEST_DEV, otherwise\n+ * the test is skipped.\n+ */\n+void serial_test_iocost_model(void)\n+{\n+\tstruct iocost_model *skel;\n+\tchar buf[300], *dev;\n+\tint err;\n+\n+\tdev = getenv(\"IOCOST_TEST_DEV\");\n+\tif (!dev || geteuid() != 0) {\n+\t\ttest__skip();\n+\t\treturn;\n+\t}\n+\tif (!dev_has_iocost(dev)) {\n+\t\tprintf(\"skip: %s has no iocost enabled\\n\", dev);\n+\t\ttest__skip();\n+\t\treturn;\n+\t}\n+\n+\t/*\n+\t * negative: binding an unknown model name must be rejected,\n+\t * so a typo cannot silently disable cost model updates\n+\t */\n+\tsnprintf(buf, sizeof(buf), \"%s model=no_such_model\\n\", dev);\n+\terr = write_cost_model(buf);\n+\tASSERT_EQ(err \u003e 0 ? err : 0, ENOENT, \"unknown_model_rejected\");\n+\n+\tskel = iocost_model__open_and_load();\n+\tif (!ASSERT_OK_PTR(skel, \"skel_open_load\"))\n+\t\treturn;\n+\n+\t/* attaching the struct_ops registers the model by name */\n+\terr = iocost_model__attach(skel);\n+\tif (ASSERT_OK(err, \"attach\"))\n+\t\tASSERT_OK(bind_model(dev, \"iocost_2x\"), \"bind_and_readback\");\n+\n+\tiocost_model__destroy(skel);\n+}\n+\n+/*\n+ * Same check for the multi-stream example model. Only one model can\n+ * be bound to a device at a time; both tests bind and restore, so\n+ * they are serial and independent.\n+ */\n+void serial_test_iocost_model_streams(void)\n+{\n+\tstruct iocost_ms *skel;\n+\tchar *dev;\n+\tint err;\n+\n+\tdev = getenv(\"IOCOST_TEST_DEV\");\n+\tif (!dev || geteuid() != 0) {\n+\t\ttest__skip();\n+\t\treturn;\n+\t}\n+\tif (!dev_has_iocost(dev)) {\n+\t\tprintf(\"skip: %s has no iocost enabled\\n\", dev);\n+\t\ttest__skip();\n+\t\treturn;\n+\t}\n+\n+\tskel = iocost_ms__open_and_load();\n+\tif (!ASSERT_OK_PTR(skel, \"skel_open_load\"))\n+\t\treturn;\n+\n+\terr = iocost_ms__attach(skel);\n+\tif (ASSERT_OK(err, \"attach\"))\n+\t\tASSERT_OK(bind_model(dev, \"iocost_ms\"), \"bind_and_readback\");\n+\n+\tiocost_ms__destroy(skel);\n+}\ndiff --git a/tools/testing/selftests/bpf/progs/iocost_model.c b/tools/testing/selftests/bpf/progs/iocost_model.c\nnew file mode 100644\nindex 0000000000000..81928f897297c\n--- /dev/null\n+++ b/tools/testing/selftests/bpf/progs/iocost_model.c\n@@ -0,0 +1,134 @@\n+// SPDX-License-Identifier: GPL-2.0\n+/*\n+ * Example iocost cost model: the builtin linear HDD formula with all\n+ * costs doubled.\n+ *\n+ * The constants mirror what calc_lcoefs() derives from the AUTOP_HDD\n+ * defaults (rbps=174019176 rseqiops=41708 rrandiops=370, w-side\n+ * analog) in vtime units where 1s == 2^37. On a rotational device\n+ * still on ctrl=auto, a device bound to this model through\n+ * io.cost.model charges twice the builtin model under the same\n+ * workload, which makes it a convenient way to verify that\n+ * accounting goes through the BPF path. On a non-rotational device,\n+ * or one with user-pinned coefficients, the ratio to the builtin\n+ * model is arbitrary.\n+ *\n+ * The cursor handling mirrors the builtin: a zero cursor means \"no\n+ * previous IO\", and the cursor is only advanced for READ and WRITE\n+ * bios the builtin prices, so flushes and discards leave it alone.\n+ *\n+ * The model implements the full linear formula itself, including\n+ * flushes: there is no fallback to the builtin model, a dataless\n+ * WRITE|REQ_PREFLUSH is priced as a one-page write.\n+ */\n+\n+#include \"vmlinux.h\"\n+#include \u003cbpf/bpf_helpers.h\u003e\n+#include \u003cbpf/bpf_tracing.h\u003e\n+\n+/* VTIME_PER_SEC comes from vmlinux.h (a BTF enum constant) */\n+#define IOC_PAGE_SIZE\t\t4096\n+#define IOC_SECT_TO_PAGE_SHIFT\t3\t/* 512B sectors to 4kB pages */\n+#define LCOEF_RANDIO_PAGES\t4096\t/* 16MB seek threshold */\n+#define IOCOST_COST_F_MERGE\t(1ULL \u003c\u003c 0)\t/* not in BTF: a plain macro */\n+#define IOCOST_REQ_OP_MASK\t0xff\t\t/* REQ_OP_MASK, not in BTF */\n+\n+/*\n+ * DIV64_U64_ROUND_UP / DIV_ROUND_UP_ULL equivalents, folded at\n+ * compile time\n+ */\n+#define RU(x, y)\t\t((x) / (y) + (((x) % (y)) ? 1 : 0))\n+\n+#define RBPS\t174019176ULL\n+#define RSEQIOPS\t41708ULL\n+#define RRANDIOPS\t370ULL\n+#define WBPS\t178075866ULL\n+#define WSEQIOPS\t42705ULL\n+#define WRANDIOPS\t378ULL\n+\n+#define RPAGE\t(RU(VTIME_PER_SEC, RU(RBPS, IOC_PAGE_SIZE)))\n+#define RSEQIO\t(RU(VTIME_PER_SEC, RSEQIOPS) - RPAGE)\n+#define RRANDIO\t(RU(VTIME_PER_SEC, RRANDIOPS) - RPAGE)\n+#define WPAGE\t(RU(VTIME_PER_SEC, RU(WBPS, IOC_PAGE_SIZE)))\n+#define WSEQIO\t(RU(VTIME_PER_SEC, WSEQIOPS) - WPAGE)\n+#define WRANDIO\t(RU(VTIME_PER_SEC, WRANDIOPS) - WPAGE)\n+\n+/*\n+ * per-cgroup cursor storage: keyed by the cgroup, freed with it, so\n+ * the model never leaks or reuses stale per-cgroup state\n+ */\n+struct {\n+\t__uint(type, BPF_MAP_TYPE_CGRP_STORAGE);\n+\t__uint(map_flags, BPF_F_NO_PREALLOC);\n+\t__type(key, int);\n+\t__type(value, __u64);\n+} cursor_store SEC(\".maps\");\n+\n+SEC(\"struct_ops\")\n+u64 BPF_PROG(iocost_2x_calc_cost, u64 opf, u64 nbytes, u64 sector,\n+\t struct blkcg *blkcg, u64 model_flags)\n+{\n+\tu64 pages, seek_pages = 0, base, coef_page, randio, cost;\n+\n+\t/* builtin truncates: max(sectors \u003e\u003e IOC_SECT_TO_PAGE_SHIFT, 1) */\n+\tpages = nbytes \u003e\u003e IOC_PAGE_SHIFT;\n+\tif (!pages)\n+\t\tpages = 1;\n+\n+\tif ((opf \u0026 IOCOST_REQ_OP_MASK) == REQ_OP_READ) {\n+\t\tbase = RSEQIO; coef_page = RPAGE; randio = RRANDIO;\n+\t} else if ((opf \u0026 IOCOST_REQ_OP_MASK) == REQ_OP_WRITE) {\n+\t\tbase = WSEQIO; coef_page = WPAGE; randio = WRANDIO;\n+\t} else {\n+\t\t/*\n+\t\t * a fully owning model must price every op; unknown\n+\t\t * ops are priced as a single page write\n+\t\t */\n+\t\tbase = 0; coef_page = WPAGE; randio = 0;\n+\t}\n+\n+\t/*\n+\t * mirror the builtin cursor semantics: seek distance is only\n+\t * computed against a non-zero cursor, and the cursor is only\n+\t * advanced for bios the builtin prices (READ/WRITE with a\n+\t * non-zero size), so flushes and discards leave it alone.\n+\t * Like bio_end_sector(), the advance truncates to whole\n+\t * sectors\n+\t */\n+\t{\n+\t\t__u64 *cursor, cur;\n+\t\tint priced = (opf \u0026 IOCOST_REQ_OP_MASK) == REQ_OP_READ ||\n+\t\t\t (opf \u0026 IOCOST_REQ_OP_MASK) == REQ_OP_WRITE;\n+\n+\t\tcursor = bpf_cgrp_storage_get(\u0026cursor_store,\n+\t\t\t\t\t blkcg-\u003ecss.cgroup, NULL,\n+\t\t\t\t\t BPF_LOCAL_STORAGE_GET_F_CREATE);\n+\t\tif (!cursor)\n+\t\t\treturn 2 * (base + pages * coef_page);\n+\t\tcur = *cursor;\n+\t\tif (cur \u0026\u0026 priced) {\n+\t\t\tseek_pages = sector \u003e cur ? sector - cur\n+\t\t\t\t\t\t : cur - sector;\n+\t\t\tseek_pages \u003e\u003e= IOC_SECT_TO_PAGE_SHIFT;\n+\t\t\tif (seek_pages \u003e LCOEF_RANDIO_PAGES)\n+\t\t\t\tbase = randio;\n+\t\t}\n+\t\tif (priced \u0026\u0026 nbytes \u0026\u0026\n+\t\t !(model_flags \u0026 IOCOST_COST_F_MERGE))\n+\t\t\t*cursor = sector + (nbytes \u003e\u003e 9);\n+\t}\n+\n+\tif (model_flags \u0026 IOCOST_COST_F_MERGE)\n+\t\tbase = 0;\n+\n+\tcost = 2 * (base + pages * coef_page);\n+\treturn cost;\n+}\n+\n+SEC(\".struct_ops\")\n+struct iocost_model_ops iocost_2x = {\n+\t.calc_cost = (void *)iocost_2x_calc_cost,\n+\t.name = \"iocost_2x\",\n+};\n+\n+char LICENSE[] SEC(\"license\") = \"GPL\";\ndiff --git a/tools/testing/selftests/bpf/progs/iocost_ms.c b/tools/testing/selftests/bpf/progs/iocost_ms.c\nnew file mode 100644\nindex 0000000000000..2a3c1d6aae4df\n--- /dev/null\n+++ b/tools/testing/selftests/bpf/progs/iocost_ms.c\n@@ -0,0 +1,156 @@\n+// SPDX-License-Identifier: GPL-2.0\n+/*\n+ * Example multi-stream sequentiality detection cost model.\n+ *\n+ * The builtin model keeps a single cursor per cgroup, so two\n+ * interleaved sequential readers in one cgroup are all priced random\n+ * (measured 89x overcharge, 12.9x throughput collapse), while random\n+ * IO inside a hot window smaller than the 16MB seek threshold is\n+ * priced sequential (measured 107x undercharge). This model replaces\n+ * the single cursor with a per-cgroup table of stream slots: an IO is\n+ * sequential iff its sector matches the expected next sector of any\n+ * tracked stream. Interleaved streams keep their own slots, and\n+ * windowed random IO rarely matches a moving expectation.\n+ *\n+ * Stream state lives in a CGRP_STORAGE map, so it is created and\n+ * freed with the cgroup. The model implements the full builtin\n+ * linear formula itself, including flush pricing.\n+ */\n+#include \"vmlinux.h\"\n+#include \u003cbpf/bpf_helpers.h\u003e\n+#include \u003cbpf/bpf_tracing.h\u003e\n+\n+/* VTIME_PER_SEC comes from vmlinux.h (a BTF enum constant) */\n+#define IOC_PAGE_SIZE\t\t4096\n+#define IOC_SECT_TO_PAGE_SHIFT\t3\t/* 512B sectors to 4kB pages */\n+#define IOCOST_COST_F_MERGE\t(1ULL \u003c\u003c 0)\t/* not in BTF: a plain macro */\n+#define IOCOST_REQ_OP_MASK\t0xff\t\t/* REQ_OP_MASK, not in BTF */\n+\n+/*\n+ * DIV64_U64_ROUND_UP / DIV_ROUND_UP_ULL equivalents, folded at\n+ * compile time\n+ */\n+#define RU(x, y)\t\t((x) / (y) + (((x) % (y)) ? 1 : 0))\n+\n+#define RBPS\t174019176ULL\n+#define RSEQIOPS\t41708ULL\n+#define RRANDIOPS\t370ULL\n+#define WBPS\t178075866ULL\n+#define WSEQIOPS\t42705ULL\n+#define WRANDIOPS\t378ULL\n+\n+#define RPAGE\t(RU(VTIME_PER_SEC, RU(RBPS, IOC_PAGE_SIZE)))\n+#define RSEQIO\t(RU(VTIME_PER_SEC, RSEQIOPS) - RPAGE)\n+#define RRANDIO\t(RU(VTIME_PER_SEC, RRANDIOPS) - RPAGE)\n+#define WPAGE\t(RU(VTIME_PER_SEC, RU(WBPS, IOC_PAGE_SIZE)))\n+#define WSEQIO\t(RU(VTIME_PER_SEC, WSEQIOPS) - WPAGE)\n+#define WRANDIO\t(RU(VTIME_PER_SEC, WRANDIOPS) - WPAGE)\n+\n+#define NSLOTS\t4\n+\n+struct streams {\n+\t__u64 expected[NSLOTS];\t/* next expected sector, per stream */\n+\t__u64 stamp[NSLOTS];\t/* LRU stamp, 0 = empty */\n+};\n+\n+/*\n+ * per-cgroup stream table: keyed by the cgroup, freed with it\n+ */\n+struct {\n+\t__uint(type, BPF_MAP_TYPE_CGRP_STORAGE);\n+\t__uint(map_flags, BPF_F_NO_PREALLOC);\n+\t__type(key, int);\n+\t__type(value, struct streams);\n+} stream_tab SEC(\".maps\");\n+\n+SEC(\"struct_ops\")\n+u64 BPF_PROG(iocost_ms_calc_cost, u64 opf, u64 nbytes, u64 sector,\n+\t struct blkcg *blkcg, u64 model_flags)\n+{\n+\tstruct streams *s;\n+\tu64 pages, base, coef_page, randio, advance, now;\n+\tu32 i, victim = 0, found = 0xFFFFFFFF;\n+\n+\tif ((opf \u0026 IOCOST_REQ_OP_MASK) == REQ_OP_READ) {\n+\t\tbase = RSEQIO; coef_page = RPAGE; randio = RRANDIO;\n+\t} else if ((opf \u0026 IOCOST_REQ_OP_MASK) == REQ_OP_WRITE) {\n+\t\tbase = WSEQIO; coef_page = WPAGE; randio = WRANDIO;\n+\t} else {\n+\t\t/*\n+\t\t * a fully owning model must price every op; unknown\n+\t\t * ops are priced as per-page writes\n+\t\t */\n+\t\tbase = 0; coef_page = WPAGE; randio = 0;\n+\t}\n+\tadvance = nbytes \u003e\u003e 9;\t/* whole sectors, like bio_end_sector() */\n+\n+\t/* only bios the builtin prices participate in stream tracking */\n+\tif (!(((opf \u0026 IOCOST_REQ_OP_MASK) == REQ_OP_READ ||\n+\t (opf \u0026 IOCOST_REQ_OP_MASK) == REQ_OP_WRITE) \u0026\u0026 nbytes)) {\n+\t\tpages = nbytes \u003e\u003e IOC_PAGE_SHIFT;\n+\t\tif (!pages)\n+\t\t\tpages = 1;\n+\t\treturn base + pages * coef_page;\n+\t}\n+\n+\ts = bpf_cgrp_storage_get(\u0026stream_tab, blkcg-\u003ecss.cgroup, NULL,\n+\t\t\t\t BPF_LOCAL_STORAGE_GET_F_CREATE);\n+\tif (!s) {\n+\t\t/* no storage: price per page, truncating like the builtin */\n+\t\tpages = nbytes \u003e\u003e IOC_PAGE_SHIFT;\n+\t\tif (!pages)\n+\t\t\tpages = 1;\n+\t\treturn base + pages * coef_page;\n+\t}\n+\n+\t/*\n+\t * Slot access is lockless, mirroring the builtin single-cursor\n+\t * update in ioc_rqos_throttle(): concurrent CPUs submitting for\n+\t * the same cgroup can race on slot updates; mispricing is\n+\t * bounded and acceptable for an example model.\n+\t */\n+\tnow = bpf_ktime_get_ns();\n+\tfor (i = 0; i \u003c NSLOTS; i++) {\n+\t\tif (s-\u003eexpected[i] == sector \u0026\u0026 s-\u003estamp[i]) {\n+\t\t\tfound = i;\n+\t\t\tbreak;\n+\t\t}\n+\t}\n+\tif (found != 0xFFFFFFFF) {\n+\t\t/* sequential: keep the seq base from the op branch */\n+\t\ts-\u003eexpected[found] = sector + advance;\n+\t\ts-\u003estamp[found] = now;\n+\t} else {\n+\t\tbase = randio;\n+\t\tfor (i = 1; i \u003c NSLOTS; i++) {\n+\t\t\tif (s-\u003estamp[i] \u003c s-\u003estamp[victim])\n+\t\t\t\tvictim = i;\n+\t\t}\n+\t\ts-\u003eexpected[victim] = sector + advance;\n+\t\ts-\u003estamp[victim] = now;\n+\t}\n+\n+\t/* builtin truncates: max(sectors \u003e\u003e IOC_SECT_TO_PAGE_SHIFT, 1) */\n+\tpages = nbytes \u003e\u003e IOC_PAGE_SHIFT;\n+\tif (!pages)\n+\t\tpages = 1;\n+\tif (model_flags \u0026 IOCOST_COST_F_MERGE) {\n+\t\t/*\n+\t\t * merged bios skip the base cost but still advance\n+\t\t * the stream position above, so a merge at the\n+\t\t * expected sector does not make the following new IO\n+\t\t * look random\n+\t\t */\n+\t\tbase = 0;\n+\t}\n+\n+\treturn base + pages * coef_page;\n+}\n+\n+SEC(\".struct_ops\")\n+struct iocost_model_ops iocost_ms = {\n+\t.calc_cost = (void *)iocost_ms_calc_cost,\n+\t.name = \"iocost_ms\",\n+};\n+\n+char LICENSE[] SEC(\"license\") = \"GPL\";\n"
} |
| 2/1 | 2026/09/14 07:59 | agent | patch-evaluator |
17mModel:core Args: null Results: {
"EnableConfigs": [
"BLK_CGROUP_IOCOST_BPF",
"BLK_CGROUP_IOCOST",
"BLK_CGROUP",
"CGROUPS",
"BPF_SYSCALL",
"BPF_JIT",
"DEBUG_INFO_BTF"
],
"FocusSymbols": [
"iocost_bpf_model_get",
"iocost_bpf_model_put",
"iocost_notify_blkcg_online",
"iocost_notify_blkcg_offline",
"bpf_iocost_reg",
"bpf_iocost_unreg",
"bpf_iocost_validate",
"ioc_cost_model_write",
"calc_vtime_cost",
"calc_size_vtime_cost"
],
"Reasoning": "The patch series introduces BPF struct_ops support for pluggable cost models in the blk-iocost cgroup IO controller (CONFIG_BLK_CGROUP_IOCOST_BPF). It adds a new struct_ops type 'iocost_model_ops', a model registry with reference counting and lifecycle tracking, integration with cgroup blkcg online/offline notifications, sysfs/cgroupfs parsing to switch models via 'io.cost.model', and RCU-guarded dispatch to the BPF calc_cost callbacks during bio charging. This modifies reachable core block layer, cgroup, and BPF logic, making it worth fuzzing for potential concurrency, refcounting, or type validation bugs.",
"WorthFuzzing": true
}Instruction: You are an expert Linux kernel maintainer and security engineer.
Your job is to review a provided patch series and evaluate whether it warrants fuzzing with syzkaller.
IMPORTANT: The changes have ALREADY been applied and committed as the HEAD commit in
your workspace. Do NOT rely on internal assumptions. You must actively use your code access
tools to inspect the actual source code, callers, and surrounding context.
================================================================================
1. CORE TRIAGE PHILOSOPHY
================================================================================
The goal of patch fuzzing is to discover crashes, regressions, exposed latent bugs,
and newly triggered assertions introduced by the patch series.
- REACHABILITY IS THE PRIMARY GATE:
Fuzzing can only discover bugs in code that can actually execute in standard virtualized
environments (GCE or QEMU, utilizing software-emulated devices like USB gadgets, netdev, tun/tap).
If the modified code is structurally unreachable (see Section 2), it MUST NOT be fuzzed,
regardless of whether it adds assertions or complex logic.
- DO NOT BLINDLY TRUST "NO FUNCTIONAL CHANGE" (NFCI) OR "REFACTORING" CLAIMS:
Patch authors routinely label changes as "cleanups", "refactorings", or state
"No functional change intended". Do NOT take these claims at face value.
Code refactorings that rearrange logic, introduce helper functions, or alter state management
in core subsystems frequently introduce subtle semantic shifts or uncover latent kernel bugs.
If reachable executable code is modified or refactored, it MUST be fuzzed.
- NEW OR MODIFIED ASSERTIONS IN REACHABLE CODE MUST BE FUZZED:
When a patch introduces or modifies runtime checks or assertions (e.g., WARN_ON*, VM_WARN_ON*,
BUG_ON*, lockdep_assert*) in reachable code paths, it enforces new or stricter invariants.
Even if the author believes the invariant always holds, fuzzing is essential to verify whether
an unusual sequence of operations can violate it.
================================================================================
2. WHEN TO RETURN WorthFuzzing=false (NEGATIVE CRITERIA)
================================================================================
Return WorthFuzzing=false ONLY IF all modified code falls strictly into one or more of these categories:
- Non-kernel and non-executable changes:
* Modifications to Documentation/, comments, or spelling fixes.
* User-space directories, self-tests, samples, or scripts (e.g., tools/, samples/, scripts/, usr/)
that do not affect the compiled kernel image (vmlinux) or kernel modules.
* Purely decorative logging (e.g., message strings in pr_err, printk, dev_info) or tracepoints
that do not alter control flow or data structures.
* Build system or Kconfig changes that do not alter compiled C logic.
- Structurally unreachable hardware:
* Vendor-specific PCIe switches, SmartNICs, or GPU drivers (e.g., mlxsw, pds_core, qed,
ionic, amdgpu) requiring physical ASIC/PCIe cards not emulated in standard QEMU.
- Unreachable execution paths:
* Driver teardown callbacks (.remove, .shutdown, pci_unregister_driver) executed only during
physical PCI hot-unplug or manual sysfs driver unbinding.
* Code paths exclusive to architectures other than the target architecture.
================================================================================
3. WHEN TO RETURN WorthFuzzing=true (POSITIVE CRITERIA)
================================================================================
Return WorthFuzzing=true whenever the patch touches reachable executable code, including:
- Core Subsystems:
* Any logic modifications in memory management (mm/), synchronization/locking (kernel/locking/),
BPF, scheduler, core networking, VFS, or syscall handling.
- Refactorings and Code Cleanups:
* Any restructuring of reachable data structures, helper abstractions, or algorithm flows.
- Runtime Assertions and Defensive Checks:
* Any introduction or alteration of assertions (WARN_ON*, VM_WARN_ON*, BUG_ON*, etc.) in reachable paths.
- Reachable Drivers and Protocols:
* Drivers accessible via virtual buses (virtio, USB gadget, loopback, netlink, binder, sockets, etc.).
================================================================================
4. EXTRACTING FocusSymbols (PREVENTING DILUTION)
================================================================================
When WorthFuzzing=true, you must extract specific kernel functions into FocusSymbols to guide the fuzzer:
- AVOID UBIQUITOUS LIFECYCLE HOT-PATHS:
Do NOT list generic, ubiquitous functions called by almost every program in the corpus
(including, but not limited to: general memory allocators and deallocators, page fault
and trap handlers, or core synchronization primitives; this is not an exhaustive list).
Listing ubiquitous functions causes the fuzzer to classify thousands of unrelated tests as "focused",
which severely dilutes fuzzing effort away from the actual changes.
- TARGET SPECIFIC FEATURE LOGIC AND ENTRYPOINTS:
List functions that specifically implement the logic being added or altered, or direct API entrypoints
for the subsystem feature under review.
- HANDLING STATIC INLINE FUNCTIONS IN HEADERS (.h):
Compiler-inlined static functions (such as static inlines in mm/*.h or include/linux/*.h) lack
distinct symbol addresses in vmlinux and cannot be targeted directly by symbol coverage filters.
If the changes are primarily in static inline helpers, identify non-static, feature-specific caller
functions in .c files that exercise them (avoiding ubiquitous lifecycle wrappers).
================================================================================
5. IDENTIFYING EnableConfigs
================================================================================
Identify any specific CONFIG_ options required to properly compile and reach the modified code:
- Inspect Kconfig files and #ifdef guards; do not make assumptions.
- Check "depends on" lines in Kconfig to include any non-standard parent subsystem configs needed.
- Strip any 'CONFIG_' prefix (e.g., return "NET_IPV4" instead of "CONFIG_NET_IPV4").
Prefer calling several tools at the same time to save round-trips.
Use set-results tool to provide results of the analysis.
It must be called exactly once before the final reply.
Ignore results of this tool.
Prompt: Target architecture: amd64
For your convenience, here is the diff of the changes:
commit 7af58b5c104951da88ad0e3e0a7f79c1b30e46ca
Author: syz-cluster <triage@syzkaller.com>
Date: Mon Sep 14 07:59:42 2026 +0000
syz-cluster: applied patch under review
diff --git a/Documentation/admin-guide/cgroup-v2.rst b/Documentation/admin-guide/cgroup-v2.rst
index 8d2603751c51a..0d2d0525255cb 100644
--- a/Documentation/admin-guide/cgroup-v2.rst
+++ b/Documentation/admin-guide/cgroup-v2.rst
@@ -2124,6 +2124,18 @@ IO Interface Files
parameters are written to, "ctrl" become "user" and the
automatic changes are disabled.
+ When CONFIG_BLK_CGROUP_IOCOST_BPF is enabled, "model" also accepts
+ the name of a registered iocost_model_ops BPF struct_ops model:
+ "model=<name>" binds the model to the device and the model fully
+ replaces the builtin linear pricing, for every operation including
+ flushes; "model=linear" (or "ctrl=auto/user") restores the builtin
+ model. Writing an unknown name fails with ENOENT and nothing is
+ applied. Unregistering a model removes its name so it can no
+ longer be selected; devices already bound keep using it, and keep
+ receiving cgroup lifecycle notifications, until switched back to
+ the builtin model. See include/linux/blk-iocost.h
+ for the model interface.
+
When "model" is "linear", the following model parameters are
defined.
diff --git a/block/Kconfig b/block/Kconfig
index 70e4a66d941ff..91e808f86d28d 100644
--- a/block/Kconfig
+++ b/block/Kconfig
@@ -231,4 +231,13 @@ config BLK_ERROR_INJECTION
source "block/Kconfig.iosched"
+config BLK_CGROUP_IOCOST_BPF
+ bool "Enable BPF pluggable cost model support for the cost IO controller"
+ depends on BLK_CGROUP_IOCOST && BPF_SYSCALL && BPF_JIT && DEBUG_INFO_BTF
+ help
+ Enabling this option registers the "iocost_model_ops" BPF
+ struct_ops type, which allows a BPF program to fully replace
+ the builtin linear cost model on a device it is bound to
+ through io.cost.model.
+
endif # BLOCK
diff --git a/block/Makefile b/block/Makefile
index e7bd320e3d697..ee5cebeea006f 100644
--- a/block/Makefile
+++ b/block/Makefile
@@ -39,3 +39,4 @@ obj-$(CONFIG_BLK_INLINE_ENCRYPTION) += blk-crypto.o blk-crypto-profile.o \
blk-crypto-sysfs.o
obj-$(CONFIG_BLK_INLINE_ENCRYPTION_FALLBACK) += blk-crypto-fallback.o
obj-$(CONFIG_BLOCK_HOLDER_DEPRECATED) += holder.o
+obj-$(CONFIG_BLK_CGROUP_IOCOST_BPF) += blk-iocost-bpf.o
diff --git a/block/blk-cgroup.c b/block/blk-cgroup.c
index 2b5c29434e426..872871045351c 100644
--- a/block/blk-cgroup.c
+++ b/block/blk-cgroup.c
@@ -32,6 +32,7 @@
#include <linux/part_stat.h>
#include "blk.h"
#include "blk-cgroup.h"
+#include <linux/blk-iocost.h>
#include "blk-ioprio.h"
#include "blk-throttle.h"
@@ -1341,6 +1342,7 @@ void blkcg_unpin_online(struct cgroup_subsys_state *blkcg_css)
*/
static void blkcg_css_offline(struct cgroup_subsys_state *css)
{
+ iocost_notify_blkcg_offline(css_to_blkcg(css));
/* this prevents anyone from attaching or migrating to this blkcg */
wb_blkcg_offline(css);
@@ -1445,6 +1447,7 @@ blkcg_css_alloc(struct cgroup_subsys_state *parent_css)
static int blkcg_css_online(struct cgroup_subsys_state *css)
{
+ iocost_notify_blkcg_online(css_to_blkcg(css));
struct blkcg *parent = blkcg_parent(css_to_blkcg(css));
/*
diff --git a/block/blk-iocost-bpf.c b/block/blk-iocost-bpf.c
new file mode 100644
index 0000000000000..7ad88600c203a
--- /dev/null
+++ b/block/blk-iocost-bpf.c
@@ -0,0 +1,307 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * blk-iocost: BPF struct_ops plumbing for pluggable cost models.
+ *
+ * Registers the "iocost_model_ops" struct_ops type and maintains the
+ * name registry of registered models. A registered model is bound to
+ * a device through io.cost.model; see include/linux/blk-iocost.h.
+ */
+#include <linux/init.h>
+#include <linux/kernel.h>
+#include <linux/module.h>
+#include <linux/mutex.h>
+#include <linux/slab.h>
+#include <linux/bpf.h>
+#include <linux/bpf_verifier.h>
+#include <linux/btf.h>
+#include <linux/blk-iocost.h>
+
+static DEFINE_MUTEX(iocost_bpf_reg_lock);
+static LIST_HEAD(iocost_bpf_models);
+static LIST_HEAD(iocost_bpf_lifecycle);
+
+/*
+ * The registry holds a bpf_struct_ops_get() reference obtained in .reg;
+ * .unreg drops it, so the kdata of an unregistered model stays alive
+ * while any device is still bound to it.
+ */
+struct iocost_bpf_model {
+ struct list_head list; /* name registry */
+ struct list_head lifecycle; /* lifecycle notify list */
+ const struct iocost_model_ops *ops;
+ refcount_t refs;
+};
+
+/*
+ * Look up a registered model by name and acquire a reference on it.
+ * The registry lock is held across lookup and bpf_struct_ops_get() so
+ * the model cannot be unregistered in between.
+ */
+int iocost_bpf_model_get(const char *name,
+ const struct iocost_model_ops **opsp)
+{
+ struct iocost_bpf_model *m;
+ int ret = -ENOENT;
+
+ mutex_lock(&iocost_bpf_reg_lock);
+ list_for_each_entry(m, &iocost_bpf_models, list) {
+ if (!strcmp(m->ops->name, name)) {
+ if (bpf_struct_ops_get(m->ops)) {
+ refcount_inc(&m->refs);
+ *opsp = m->ops;
+ ret = 0;
+ }
+ break;
+ }
+ }
+ mutex_unlock(&iocost_bpf_reg_lock);
+ return ret;
+}
+
+static struct iocost_bpf_model *
+iocost_bpf_model_lookup(const struct iocost_model_ops *ops)
+{
+ struct iocost_bpf_model *m;
+
+ list_for_each_entry(m, &iocost_bpf_models, list) {
+ if (m->ops == ops)
+ return m;
+ }
+ return NULL;
+}
+
+/*
+ * Lifecycle notifications walk the lifecycle list, which keeps a node
+ * for as long as any device has the model bound, so an unregistered
+ * but still-bound model keeps receiving blkcg online/offline.
+ */
+void iocost_notify_blkcg_online(struct blkcg *blkcg)
+{
+ struct iocost_bpf_model *m;
+
+ guard(mutex)(&iocost_bpf_reg_lock);
+ list_for_each_entry(m, &iocost_bpf_lifecycle, lifecycle) {
+ if (m->ops->blkcg_online)
+ m->ops->blkcg_online(blkcg);
+ }
+}
+
+void iocost_notify_blkcg_offline(struct blkcg *blkcg)
+{
+ struct iocost_bpf_model *m;
+
+ guard(mutex)(&iocost_bpf_reg_lock);
+ list_for_each_entry(m, &iocost_bpf_lifecycle, lifecycle) {
+ if (m->ops->blkcg_offline)
+ m->ops->blkcg_offline(blkcg);
+ }
+}
+
+static int bpf_iocost_model_init(struct btf *btf)
+{
+ s32 type_id;
+
+ type_id = btf_find_by_name_kind(btf, "iocost_model_ops", BTF_KIND_STRUCT);
+ if (type_id < 0)
+ return -EINVAL;
+ return 0;
+}
+
+static bool bpf_iocost_is_valid_access(int off, int size,
+ enum bpf_access_type type,
+ const struct bpf_prog *prog,
+ struct bpf_insn_access_aux *info)
+{
+ return bpf_tracing_btf_ctx_access(off, size, type, prog, info);
+}
+
+static const struct bpf_func_proto *
+bpf_iocost_get_func_proto(enum bpf_func_id func_id,
+ const struct bpf_prog *prog)
+{
+ switch (func_id) {
+#ifdef CONFIG_CGROUPS
+ case BPF_FUNC_cgrp_storage_get:
+ return &bpf_cgrp_storage_get_proto;
+#endif
+ default:
+ return bpf_base_func_proto(func_id, prog);
+ }
+}
+
+static int bpf_iocost_check_member(const struct btf_type *t,
+ const struct btf_member *member,
+ const struct bpf_prog *prog)
+{
+ /* calc_cost() is called with RCU read lock held */
+ if (prog->sleepable)
+ return -EINVAL;
+ return 0;
+}
+
+static int bpf_iocost_init_member(const struct btf_type *t,
+ const struct btf_member *member,
+ void *kdata, const void *udata)
+{
+ struct iocost_model_ops *ops = kdata;
+ const struct iocost_model_ops *uops = udata;
+ u32 moff = __btf_member_bit_offset(t, member) / 8;
+
+ switch (moff) {
+ case offsetof(struct iocost_model_ops, name):
+ if (bpf_obj_name_cpy(ops->name, uops->name,
+ sizeof(ops->name)) <= 0)
+ return -EINVAL;
+ return 1;
+ }
+
+ return 0;
+}
+
+static u64 bpf_iocost_calc_cost_stub(u64 opf, u64 nbytes, u64 sector,
+ struct blkcg *blkcg, u64 model_flags);
+
+/*
+ * kdata is seeded from the CFI stubs, so calc_cost is never NULL; a
+ * model which did not implement it inherits the stub, which prices
+ * every IO at 0. Compare against the stub to reject it.
+ */
+static int bpf_iocost_validate(void *kdata)
+{
+ struct iocost_model_ops *ops = kdata;
+
+ if (ops->calc_cost == bpf_iocost_calc_cost_stub)
+ return -EINVAL;
+ return 0;
+}
+
+static int bpf_iocost_reg(void *kdata, struct bpf_link *link)
+{
+ struct iocost_model_ops *ops = kdata;
+ struct iocost_bpf_model *m;
+ int ret = 0;
+
+ if (!bpf_struct_ops_get(ops))
+ return -ENOENT;
+
+ m = kzalloc(sizeof(*m), GFP_KERNEL);
+ if (!m) {
+ bpf_struct_ops_put(ops);
+ return -ENOMEM;
+ }
+ refcount_set(&m->refs, 1);
+
+ mutex_lock(&iocost_bpf_reg_lock);
+ {
+ struct iocost_bpf_model *other;
+
+ list_for_each_entry(other, &iocost_bpf_models, list) {
+ if (!strcmp(other->ops->name, ops->name)) {
+ ret = -EEXIST;
+ break;
+ }
+ }
+ }
+ if (!ret) {
+ m->ops = ops;
+ list_add(&m->list, &iocost_bpf_models);
+ list_add(&m->lifecycle, &iocost_bpf_lifecycle);
+ }
+ mutex_unlock(&iocost_bpf_reg_lock);
+
+ if (ret) {
+ bpf_struct_ops_put(ops);
+ kfree(m);
+ }
+ return ret;
+}
+
+/*
+ * Unregistering drops the registration reference. When the last
+ * reference is gone (no device bound), the node leaves the lifecycle
+ * list and is freed; otherwise bound devices keep it alive and it
+ * keeps receiving blkcg online/offline notifications.
+ */
+static void bpf_iocost_unreg(void *kdata, struct bpf_link *link)
+{
+ struct iocost_model_ops *ops = kdata;
+ struct iocost_bpf_model *m;
+
+ mutex_lock(&iocost_bpf_reg_lock);
+ m = iocost_bpf_model_lookup(ops);
+ if (m) {
+ list_del(&m->list);
+ if (refcount_dec_and_test(&m->refs)) {
+ list_del(&m->lifecycle);
+ kfree(m);
+ }
+ }
+ mutex_unlock(&iocost_bpf_reg_lock);
+
+ bpf_struct_ops_put(ops);
+}
+
+void iocost_bpf_model_put(const struct iocost_model_ops *ops)
+{
+ struct iocost_bpf_model *m;
+
+ mutex_lock(&iocost_bpf_reg_lock);
+ list_for_each_entry(m, &iocost_bpf_lifecycle, lifecycle) {
+ if (m->ops == ops)
+ break;
+ }
+ /*
+ * Exactly one bpf_struct_ops_put() per call, pairing the
+ * bpf_struct_ops_get() in iocost_bpf_model_get(); the node is
+ * freed when the last reference goes, whichever side drops it.
+ */
+ if (&m->lifecycle != &iocost_bpf_lifecycle &&
+ refcount_dec_and_test(&m->refs)) {
+ list_del(&m->lifecycle);
+ mutex_unlock(&iocost_bpf_reg_lock);
+ kfree(m);
+ bpf_struct_ops_put(ops);
+ return;
+ }
+ mutex_unlock(&iocost_bpf_reg_lock);
+ bpf_struct_ops_put(ops);
+}
+
+static const struct bpf_verifier_ops bpf_iocost_verifier_ops = {
+ .get_func_proto = bpf_iocost_get_func_proto,
+ .is_valid_access = bpf_iocost_is_valid_access,
+};
+
+static u64 bpf_iocost_calc_cost_stub(u64 opf, u64 nbytes, u64 sector,
+ struct blkcg *blkcg, u64 flags)
+{
+ return 0;
+}
+
+static void bpf_iocost_blkcg_online_stub(struct blkcg *blkcg) { }
+static void bpf_iocost_blkcg_offline_stub(struct blkcg *blkcg) { }
+
+static struct iocost_model_ops __bpf_ops_iocost_model_ops = {
+ .calc_cost = bpf_iocost_calc_cost_stub,
+ .blkcg_online = bpf_iocost_blkcg_online_stub,
+ .blkcg_offline = bpf_iocost_blkcg_offline_stub,
+};
+
+static struct bpf_struct_ops bpf_iocost_model_ops = {
+ .verifier_ops = &bpf_iocost_verifier_ops,
+ .init = bpf_iocost_model_init,
+ .check_member = bpf_iocost_check_member,
+ .init_member = bpf_iocost_init_member,
+ .validate = bpf_iocost_validate,
+ .reg = bpf_iocost_reg,
+ .unreg = bpf_iocost_unreg,
+ .name = "iocost_model_ops",
+ .cfi_stubs = &__bpf_ops_iocost_model_ops,
+ .owner = THIS_MODULE,
+};
+
+static int __init bpf_iocost_init(void)
+{
+ return register_bpf_struct_ops(&bpf_iocost_model_ops, iocost_model_ops);
+}
+late_initcall(bpf_iocost_init);
diff --git a/block/blk-iocost.c b/block/blk-iocost.c
index 2745bffcd5eef..96126a185f9a6 100644
--- a/block/blk-iocost.c
+++ b/block/blk-iocost.c
@@ -177,6 +177,7 @@
#include <linux/timer.h>
#include <linux/time64.h>
#include <linux/parser.h>
+#include <linux/blk-iocost.h>
#include <linux/sched/signal.h>
#include <asm/local.h>
#include <asm/local64.h>
@@ -445,6 +446,11 @@ struct ioc {
int autop_idx;
bool user_qos_params:1;
bool user_cost_model:1;
+
+#ifdef CONFIG_BLK_CGROUP_IOCOST_BPF
+ /* bound BPF cost model, NULL = builtin linear model */
+ const struct iocost_model_ops __rcu *model;
+#endif
};
struct iocg_pcpu_stat {
@@ -2238,6 +2244,7 @@ static void ioc_timer_fn(struct timer_list *timer)
struct ioc_now now;
LIST_HEAD(surpluses);
int nr_debtors, nr_shortages = 0, nr_lagging = 0;
+ int nr_active = 0;
u64 usage_us_sum = 0;
u32 ppm_rthr;
u32 ppm_wthr;
@@ -2274,6 +2281,8 @@ static void ioc_timer_fn(struct timer_list *timer)
u64 vdone, vtime, usage_us;
u32 hw_active, hw_inuse;
+ nr_active++;
+
/*
* Collect unused and wind vtime closer to vnow to prevent
* iocgs from accumulating a large amount of budget.
@@ -2435,6 +2444,14 @@ static void ioc_timer_fn(struct timer_list *timer)
ioc->busy_level = clamp(ioc->busy_level, -1000, 1000);
+ /*
+ * vrate and period_us change right below; snapshot the values
+ * this period ran in so the tick below reports the period's own
+ * parameters instead of the next period's.
+ */
+ u32 tick_period_us = ioc->period_us;
+ u64 tick_vrate = ioc->vtime_base_rate;
+
ioc_adjust_base_vrate(ioc, rq_wait_pct, nr_lagging, nr_shortages,
prev_busy_level, missed_ppm);
@@ -2448,16 +2465,38 @@ static void ioc_timer_fn(struct timer_list *timer)
*/
atomic64_inc(&ioc->cur_period);
- if (ioc->running != IOC_STOP) {
- if (!list_empty(&ioc->active_iocgs)) {
- ioc_start_period(ioc, &now);
- } else {
- ioc->busy_level = 0;
- ioc->vtime_err = 0;
- ioc->running = IOC_IDLE;
- }
+ /*
+ * Snapshot the state this period ran in before the idle
+ * transition wipes it, so the final tick reports the period's
+ * own busy level (e.g. the saturation that drove the controller
+ * idle) instead of the cleared one. usage is normalized by the
+ * measured period length, captured before ioc_start_period()
+ * overwrites period_at, the same way the donation loop does.
+ */
+ {
+ int tick_busy = ioc->busy_level;
+ int tick_running = ioc->running;
+ u64 tick_dur = now.now - ioc->period_at;
+ /* cur_period was already advanced to N+1 above; report
+ * the period that just ended, like the other fields */
+ u64 tick_period = atomic64_read(&ioc->cur_period) - 1;
+
+ trace_iocost_ioc_tick(ioc, nr_active, usage_us_sum,
+ tick_period, tick_period_us,
+ tick_vrate, tick_busy, tick_running,
+ tick_dur);
+
+ if (ioc->running != IOC_STOP) {
+ if (!list_empty(&ioc->active_iocgs)) {
+ ioc_start_period(ioc, &now);
+ } else {
+ ioc->busy_level = 0;
+ ioc->vtime_err = 0;
+ ioc->running = IOC_IDLE;
+ }
- ioc_refresh_vrate(ioc, &now);
+ ioc_refresh_vrate(ioc, &now);
+ }
}
spin_unlock_irq(&ioc->lock);
@@ -2571,10 +2610,28 @@ static void calc_vtime_cost_builtin(struct bio *bio, struct ioc_gq *iocg,
static u64 calc_vtime_cost(struct bio *bio, struct ioc_gq *iocg, bool is_merge)
{
+#ifdef CONFIG_BLK_CGROUP_IOCOST_BPF
+ const struct iocost_model_ops *model;
u64 cost;
- calc_vtime_cost_builtin(bio, iocg, is_merge, &cost);
- return cost;
+ rcu_read_lock();
+ model = rcu_dereference(iocg->ioc->model);
+ if (model) {
+ cost = model->calc_cost(bio->bi_opf, bio->bi_iter.bi_size,
+ bio->bi_iter.bi_sector,
+ iocg_to_blkg(iocg)->blkcg,
+ is_merge ? IOCOST_COST_F_MERGE : 0);
+ rcu_read_unlock();
+ return min(cost, VTIME_PER_SEC);
+ }
+ rcu_read_unlock();
+#endif
+ {
+ u64 cost;
+
+ calc_vtime_cost_builtin(bio, iocg, is_merge, &cost);
+ return cost;
+ }
}
static void calc_size_vtime_cost_builtin(struct request *rq, struct ioc *ioc,
@@ -2596,10 +2653,28 @@ static void calc_size_vtime_cost_builtin(struct request *rq, struct ioc *ioc,
static u64 calc_size_vtime_cost(struct request *rq, struct ioc *ioc)
{
- u64 cost;
+#ifdef CONFIG_BLK_CGROUP_IOCOST_BPF
+ const struct iocost_model_ops *model;
- calc_size_vtime_cost_builtin(rq, ioc, &cost);
- return cost;
+ rcu_read_lock();
+ model = rcu_dereference(ioc->model);
+ if (model && rq->bio && rq->bio->bi_blkg) {
+ u64 cost;
+
+ cost = model->calc_cost(rq->cmd_flags, blk_rq_bytes(rq),
+ blk_rq_pos(rq),
+ rq->bio->bi_blkg->blkcg, 0);
+ rcu_read_unlock();
+ return min(cost, VTIME_PER_SEC);
+ }
+ rcu_read_unlock();
+#endif
+ {
+ u64 cost;
+
+ calc_size_vtime_cost_builtin(rq, ioc, &cost);
+ return cost;
+ }
}
enum over_budget_action {
@@ -2900,6 +2975,19 @@ static void ioc_rqos_exit(struct rq_qos *rqos)
timer_shutdown_sync(&ioc->timer);
free_percpu(ioc->pcpu_stat);
+#ifdef CONFIG_BLK_CGROUP_IOCOST_BPF
+ {
+ const struct iocost_model_ops *model;
+
+ spin_lock_irq(&ioc->lock);
+ model = rcu_dereference_protected(ioc->model,
+ lockdep_is_held(&ioc->lock));
+ rcu_assign_pointer(ioc->model, NULL);
+ spin_unlock_irq(&ioc->lock);
+ if (model)
+ iocost_bpf_model_put(model);
+ }
+#endif
kfree(ioc);
}
@@ -3438,12 +3526,30 @@ static u64 ioc_cost_model_prfill(struct seq_file *sf,
return 0;
spin_lock_irq(&ioc->lock);
+#ifdef CONFIG_BLK_CGROUP_IOCOST_BPF
+ {
+ const struct iocost_model_ops *model =
+ rcu_dereference_protected(ioc->model,
+ lockdep_is_held(&ioc->lock));
+
+ seq_printf(sf, "%s ctrl=%s model=%s "
+ "rbps=%llu rseqiops=%llu rrandiops=%llu "
+ "wbps=%llu wseqiops=%llu wrandiops=%llu\n",
+ dname, model ? "bpf" :
+ ioc->user_cost_model ? "user" : "auto",
+ model ? model->name : "linear",
+ u[I_LCOEF_RBPS], u[I_LCOEF_RSEQIOPS],
+ u[I_LCOEF_RRANDIOPS], u[I_LCOEF_WBPS],
+ u[I_LCOEF_WSEQIOPS], u[I_LCOEF_WRANDIOPS]);
+ }
+#else
seq_printf(sf, "%s ctrl=%s model=linear "
"rbps=%llu rseqiops=%llu rrandiops=%llu "
"wbps=%llu wseqiops=%llu wrandiops=%llu\n",
dname, ioc->user_cost_model ? "user" : "auto",
u[I_LCOEF_RBPS], u[I_LCOEF_RSEQIOPS], u[I_LCOEF_RRANDIOPS],
u[I_LCOEF_WBPS], u[I_LCOEF_WSEQIOPS], u[I_LCOEF_WRANDIOPS]);
+#endif
spin_unlock_irq(&ioc->lock);
return 0;
}
@@ -3457,6 +3563,52 @@ static int ioc_cost_model_show(struct seq_file *sf, void *v)
return 0;
}
+/*
+ * Resolve the model name and take a reference on the new model before
+ * anything is applied, so a bad name rejects the whole write. The
+ * registry lookup takes the registration mutex and must stay outside
+ * ioc->lock. The returned model is per-write state passed back into
+ * ioc_bpf_model_commit(), which performs the pointer swap under
+ * ioc->lock so concurrent writers cannot interleave a half-applied
+ * configuration, and the caller drops the old model's reference
+ * afterwards.
+ */
+static const struct iocost_model_ops *
+ioc_bpf_model_prepare(const char *name)
+{
+#ifdef CONFIG_BLK_CGROUP_IOCOST_BPF
+ const struct iocost_model_ops *new = NULL;
+ int ret;
+
+ if (!name[0])
+ return NULL;
+ ret = iocost_bpf_model_get(name, &new);
+ return ret ? ERR_PTR(ret) : new;
+#else
+ return name[0] ? ERR_PTR(-ENOENT) : NULL;
+#endif
+}
+
+/*
+ * Swap in the model returned by ioc_bpf_model_prepare(). Called with
+ * ioc->lock held; dropping the old model's reference may sleep, so
+ * the caller does it after releasing the lock.
+ */
+static const struct iocost_model_ops *
+ioc_bpf_model_commit(struct ioc *ioc, const struct iocost_model_ops *new)
+{
+#ifdef CONFIG_BLK_CGROUP_IOCOST_BPF
+ const struct iocost_model_ops *old;
+
+ old = rcu_dereference_protected(ioc->model,
+ lockdep_is_held(&ioc->lock));
+ rcu_assign_pointer(ioc->model, new);
+ return old;
+#else
+ return NULL;
+#endif
+}
+
static const match_table_t cost_ctrl_tokens = {
{ COST_CTRL, "ctrl=%s" },
{ COST_MODEL, "model=%s" },
@@ -3482,6 +3634,7 @@ static ssize_t ioc_cost_model_write(struct kernfs_open_file *of, char *input,
struct ioc *ioc;
u64 u[NR_I_LCOEFS];
bool user;
+ char bpf_model[IOCOST_MODEL_NAME_LEN];
char *body, *p;
int ret;
@@ -3512,6 +3665,7 @@ static ssize_t ioc_cost_model_write(struct kernfs_open_file *of, char *input,
spin_lock_irq(&ioc->lock);
memcpy(u, ioc->params.i_lcoefs, sizeof(u));
user = ioc->user_cost_model;
+ bpf_model[0] = '\0';
ret = -EINVAL;
@@ -3533,11 +3687,16 @@ static ssize_t ioc_cost_model_write(struct kernfs_open_file *of, char *input,
user = true;
else
goto unlock;
+ bpf_model[0] = '\0';
continue;
case COST_MODEL:
match_strlcpy(buf, &args[0], sizeof(buf));
- if (strcmp(buf, "linear"))
- goto unlock;
+ if (!strcmp(buf, "linear")) {
+ /* back to the builtin linear model */
+ bpf_model[0] = '\0';
+ continue;
+ }
+ match_strlcpy(bpf_model, &args[0], sizeof(bpf_model));
continue;
}
@@ -3550,19 +3709,41 @@ static ssize_t ioc_cost_model_write(struct kernfs_open_file *of, char *input,
user = true;
}
- if (user) {
- memcpy(ioc->params.i_lcoefs, u, sizeof(u));
- ioc->user_cost_model = true;
- } else {
- ioc->user_cost_model = false;
- }
- ioc_refresh_params(ioc, true);
-
ret = 0;
unlock:
spin_unlock_irq(&ioc->lock);
+ /*
+ * Resolve the model name outside ioc->lock (the registry lookup
+ * takes a mutex), so a bad name rejects the whole write before
+ * anything is applied. The resolved model is this write's own
+ * state, not per-device state, so concurrent writers cannot
+ * clobber each other's staged model. On success, re-take the
+ * lock to apply the coefficients and swap the model in one go.
+ */
+ if (!ret) {
+ const struct iocost_model_ops *new, *old;
+
+ new = ioc_bpf_model_prepare(bpf_model);
+ if (IS_ERR(new)) {
+ ret = PTR_ERR(new);
+ } else {
+ spin_lock_irq(&ioc->lock);
+ if (user) {
+ memcpy(ioc->params.i_lcoefs, u, sizeof(u));
+ ioc->user_cost_model = true;
+ } else {
+ ioc->user_cost_model = false;
+ }
+ ioc_refresh_params(ioc, true);
+ old = ioc_bpf_model_commit(ioc, new);
+ spin_unlock_irq(&ioc->lock);
+ if (IS_ENABLED(CONFIG_BLK_CGROUP_IOCOST_BPF) && old)
+ iocost_bpf_model_put(old);
+ }
+ }
+
blk_mq_unquiesce_queue(q);
blk_mq_unfreeze_queue(q, memflags);
diff --git a/include/linux/blk-iocost.h b/include/linux/blk-iocost.h
new file mode 100644
index 0000000000000..d4bd2b0c826b3
--- /dev/null
+++ b/include/linux/blk-iocost.h
@@ -0,0 +1,85 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+#ifndef _LINUX_BLK_IOCOST_H
+#define _LINUX_BLK_IOCOST_H
+
+#include <linux/types.h>
+#include <linux/blk_types.h>
+
+#define IOCOST_MODEL_NAME_LEN 16
+
+#ifdef CONFIG_BLK_CGROUP_IOCOST_BPF
+
+struct blkcg;
+
+/*
+ * Pluggable cost model interface for blk-iocost.
+ *
+ * A BPF struct_ops implementation registered against "iocost_model_ops"
+ * fully replaces the builtin linear model on the devices it is bound to
+ * through io.cost.model. The model owns pricing for every IO on a bound
+ * device: it prices all operations, including flushes, and it is called
+ * from the bio charging path. The completion-time request sizing for
+ * the latency met/missed accounting still uses the builtin coefficients
+ * (the request's bio, and with it the issuing cgroup, is gone by then);
+ * extending the model there is an open question of this interface.
+ *
+ * calc_cost() is called from the IO submission path with RCU read lock
+ * held and must not sleep. It returns the cost of the IO in vtime
+ * units, where 1 second of device time equals VTIME_PER_SEC (2^37,
+ * available to BPF programs through vmlinux.h). The returned value is
+ * clamped to 1 second of device time per IO.
+ *
+ * The model is passed the blkcg of the issuing cgroup so it can keep
+ * per-cgroup state. State keyed by the blkcg alone is shared across
+ * every device the model is bound to, unlike the builtin cursor which
+ * is per (cgroup, device). blkcg_online()/blkcg_offline() are optional
+ * callbacks mirroring the blkcg css lifecycle: state created on online
+ * (or lazily on first use) must be released on offline.
+ *
+ * The registration and binding model follows the TCP congestion
+ * control framework: registering a struct_ops makes the model available
+ * by its name, while io.cost.model binds one registered model to a
+ * device. Unregistering removes the name from the registry; devices
+ * already bound keep using it until switched back to the builtin
+ * model.
+ */
+
+/*
+ * iocost-specific call metadata for calc_cost()'s model_flags
+ * argument; everything else, including REQ_PREFLUSH/REQ_FUA, is
+ * already present in the opf argument
+ */
+#define IOCOST_COST_F_MERGE (1ULL << 0) /* called from merge path */
+
+struct iocost_model_ops {
+ u64 (*calc_cost)(u64 opf, u64 nbytes, sector_t sector,
+ struct blkcg *blkcg, u64 model_flags);
+ void (*blkcg_online)(struct blkcg *blkcg);
+ void (*blkcg_offline)(struct blkcg *blkcg);
+
+ /* model name, used to select the model through io.cost.model */
+ char name[16];
+};
+
+int iocost_bpf_model_get(const char *name,
+ const struct iocost_model_ops **opsp);
+void iocost_bpf_model_put(const struct iocost_model_ops *ops);
+void iocost_notify_blkcg_online(struct blkcg *blkcg);
+void iocost_notify_blkcg_offline(struct blkcg *blkcg);
+
+#else /* CONFIG_BLK_CGROUP_IOCOST_BPF */
+
+struct blkcg;
+struct iocost_model_ops;
+
+static inline int iocost_bpf_model_get(const char *name,
+ const struct iocost_model_ops **opsp)
+{
+ return -EOPNOTSUPP;
+}
+static inline void iocost_bpf_model_put(const struct iocost_model_ops *ops) { }
+static inline void iocost_notify_blkcg_online(struct blkcg *blkcg) { }
+static inline void iocost_notify_blkcg_offline(struct blkcg *blkcg) { }
+
+#endif /* CONFIG_BLK_CGROUP_IOCOST_BPF */
+#endif /* _LINUX_BLK_IOCOST_H */
diff --git a/include/trace/events/iocost.h b/include/trace/events/iocost.h
index e772b1bc60d60..ec5d9c453d55a 100644
--- a/include/trace/events/iocost.h
+++ b/include/trace/events/iocost.h
@@ -178,6 +178,52 @@ TRACE_EVENT(iocost_ioc_vrate_adj,
)
);
+/*
+ * Periodic per-device summary, emitted once per period from the tail of
+ * ioc_timer_fn(). Unlike the state-change events above, this fires every
+ * period the controller is running, including steady states, and carries
+ * the overall controller state so basic monitoring doesn't require drgn.
+ */
+TRACE_EVENT(iocost_ioc_tick,
+
+ TP_PROTO(struct ioc *ioc, int nr_active, u64 usage_us_sum,
+ u64 cur_period, u32 tick_period_us, u64 tick_vrate,
+ int tick_busy, int tick_running, u64 tick_dur),
+
+ TP_ARGS(ioc, nr_active, usage_us_sum, cur_period,
+ tick_period_us, tick_vrate,
+ tick_busy, tick_running, tick_dur),
+
+ TP_STRUCT__entry (
+ __string(devname, ioc_name(ioc))
+ __field(u64, cur_period)
+ __field(u32, period_us)
+ __field(u64, vrate)
+ __field(int, busy_level)
+ __field(int, nr_active)
+ __field(u32, usage_pct)
+ __field(int, running)
+ ),
+
+ TP_fast_assign(
+ __assign_str(devname);
+ __entry->cur_period = cur_period;
+ __entry->period_us = tick_period_us;
+ __entry->vrate = tick_vrate;
+ __entry->busy_level = tick_busy;
+ __entry->nr_active = nr_active;
+ __entry->usage_pct = tick_dur ?
+ div_u64(usage_us_sum * 100, tick_dur) : 0;
+ __entry->running = tick_running;
+ ),
+
+ TP_printk("[%s] period=%llu:%uus vrate=%llu busy=%d active=%d usage=%u%% running=%d",
+ __get_str(devname), __entry->cur_period, __entry->period_us,
+ __entry->vrate, __entry->busy_level, __entry->nr_active,
+ __entry->usage_pct, __entry->running
+ )
+);
+
TRACE_EVENT(iocost_iocg_forgive_debt,
TP_PROTO(struct ioc_gq *iocg, const char *path, struct ioc_now *now,
diff --git a/tools/testing/selftests/bpf/config b/tools/testing/selftests/bpf/config
index 2f79688dcf7ce..67a630cb56148 100644
--- a/tools/testing/selftests/bpf/config
+++ b/tools/testing/selftests/bpf/config
@@ -138,3 +138,5 @@ CONFIG_SMC_HS_CTRL_BPF=y
CONFIG_DIBS=y
CONFIG_DIBS_LO=y
CONFIG_PM_WAKELOCKS=y
+CONFIG_BLK_CGROUP_IOCOST=y
+CONFIG_BLK_CGROUP_IOCOST_BPF=y
diff --git a/tools/testing/selftests/bpf/prog_tests/iocost_model.c b/tools/testing/selftests/bpf/prog_tests/iocost_model.c
new file mode 100644
index 0000000000000..db0a74b1885be
--- /dev/null
+++ b/tools/testing/selftests/bpf/prog_tests/iocost_model.c
@@ -0,0 +1,200 @@
+// SPDX-License-Identifier: GPL-2.0
+#include <test_progs.h>
+#include <ctype.h>
+#include <fcntl.h>
+#include <unistd.h>
+#include "iocost_model.skel.h"
+#include "iocost_ms.skel.h"
+
+/*
+ * Write a line to io.cost.model with write(2) and return the errno of
+ * the failed write, or 0 on success. stdio is not used here on
+ * purpose: the kernel's rejection happens in the write() syscall,
+ * not in the userspace buffer copy, and every write, including
+ * the error paths of the callers below, is checked.
+ */
+static int write_cost_model(const char *buf)
+{
+ int fd, err = 0;
+ ssize_t n;
+
+ fd = open("/sys/fs/cgroup/io.cost.model", O_WRONLY);
+ if (fd < 0)
+ return -errno; /* negative: the file is not there */
+ n = write(fd, buf, strlen(buf));
+ if (n < 0)
+ err = errno;
+ close(fd);
+ return err;
+}
+
+/*
+ * Read back the io.cost.model line of dev and copy the model= value
+ * into @model. Returns 0 on success.
+ */
+static int readback_model(const char *dev, char *model, size_t model_sz)
+{
+ char line[256], word[256], *m, *end;
+ FILE *fp;
+ int found = 0;
+
+ fp = fopen("/sys/fs/cgroup/io.cost.model", "r");
+ if (!fp)
+ return -1;
+ while (fgets(line, sizeof(line), fp)) {
+ if (sscanf(line, "%255s", word) == 1 && !strcmp(word, dev)) {
+ found = 1;
+ break;
+ }
+ }
+ fclose(fp);
+ if (!found)
+ return -1;
+
+ m = strstr(line, "model=");
+ if (!m)
+ return -1;
+ m += strlen("model=");
+ end = m;
+ while (*end && !isspace(*end))
+ end++;
+ snprintf(model, model_sz, "%.*s", (int)(end - m), m);
+ return 0;
+}
+
+/*
+ * Bind the named model to a device with "model=<name>", verify the
+ * readback and restore the builtin model. Returns 0 on success.
+ */
+static int bind_model(const char *dev, const char *name)
+{
+ char buf[300], got[64];
+ int err;
+
+ snprintf(buf, sizeof(buf), "%s model=%s\n", dev, name);
+ err = write_cost_model(buf);
+ if (err)
+ return err > 0 ? -err : -EIO;
+ err = readback_model(dev, got, sizeof(got));
+ if (!err && !strcmp(got, name)) {
+ /* restore the builtin linear model on every path so the
+ * device is not left bound to a model the caller may
+ * unregister right after; the write is checked too */
+ snprintf(buf, sizeof(buf), "%s model=linear\n", dev);
+ err = write_cost_model(buf);
+ return err > 0 ? -err : (err ? -EIO : 0);
+ }
+
+ /* bind failed or does not read back: still restore; prefer
+ * the original failure, but surface the restore error too */
+ snprintf(buf, sizeof(buf), "%s model=linear\n", dev);
+ {
+ int rerr = write_cost_model(buf);
+
+ return err ? (err > 0 ? -err : -EIO)
+ : (rerr > 0 ? -rerr : -EIO);
+ }
+}
+
+/*
+ * True when the device line exists in io.cost.qos and iocost is
+ * enabled on it.
+ */
+static int dev_has_iocost(const char *dev)
+{
+ char line[256], word[256];
+ FILE *fp;
+ int found = 0;
+
+ fp = fopen("/sys/fs/cgroup/io.cost.qos", "r");
+ if (!fp)
+ return 0;
+ while (fgets(line, sizeof(line), fp)) {
+ if (sscanf(line, "%255s", word) == 1 && !strcmp(word, dev)) {
+ found = strstr(line, "enable=1") != NULL;
+ break;
+ }
+ }
+ fclose(fp);
+ return found;
+}
+
+/*
+ * Bind the 2x example model and verify the io.cost.model readback.
+ * IO accounting itself is not checked here; it needs a device doing
+ * real IO under iocost and is covered by the kernel-side validation
+ * described in the cover letter.
+ *
+ * Requires root, cgroup v2 and a device with iocost support. The
+ * device must be given as major:minor in $IOCOST_TEST_DEV, otherwise
+ * the test is skipped.
+ */
+void serial_test_iocost_model(void)
+{
+ struct iocost_model *skel;
+ char buf[300], *dev;
+ int err;
+
+ dev = getenv("IOCOST_TEST_DEV");
+ if (!dev || geteuid() != 0) {
+ test__skip();
+ return;
+ }
+ if (!dev_has_iocost(dev)) {
+ printf("skip: %s has no iocost enabled\n", dev);
+ test__skip();
+ return;
+ }
+
+ /*
+ * negative: binding an unknown model name must be rejected,
+ * so a typo cannot silently disable cost model updates
+ */
+ snprintf(buf, sizeof(buf), "%s model=no_such_model\n", dev);
+ err = write_cost_model(buf);
+ ASSERT_EQ(err > 0 ? err : 0, ENOENT, "unknown_model_rejected");
+
+ skel = iocost_model__open_and_load();
+ if (!ASSERT_OK_PTR(skel, "skel_open_load"))
+ return;
+
+ /* attaching the struct_ops registers the model by name */
+ err = iocost_model__attach(skel);
+ if (ASSERT_OK(err, "attach"))
+ ASSERT_OK(bind_model(dev, "iocost_2x"), "bind_and_readback");
+
+ iocost_model__destroy(skel);
+}
+
+/*
+ * Same check for the multi-stream example model. Only one model can
+ * be bound to a device at a time; both tests bind and restore, so
+ * they are serial and independent.
+ */
+void serial_test_iocost_model_streams(void)
+{
+ struct iocost_ms *skel;
+ char *dev;
+ int err;
+
+ dev = getenv("IOCOST_TEST_DEV");
+ if (!dev || geteuid() != 0) {
+ test__skip();
+ return;
+ }
+ if (!dev_has_iocost(dev)) {
+ printf("skip: %s has no iocost enabled\n", dev);
+ test__skip();
+ return;
+ }
+
+ skel = iocost_ms__open_and_load();
+ if (!ASSERT_OK_PTR(skel, "skel_open_load"))
+ return;
+
+ err = iocost_ms__attach(skel);
+ if (ASSERT_OK(err, "attach"))
+ ASSERT_OK(bind_model(dev, "iocost_ms"), "bind_and_readback");
+
+ iocost_ms__destroy(skel);
+}
diff --git a/tools/testing/selftests/bpf/progs/iocost_model.c b/tools/testing/selftests/bpf/progs/iocost_model.c
new file mode 100644
index 0000000000000..81928f897297c
--- /dev/null
+++ b/tools/testing/selftests/bpf/progs/iocost_model.c
@@ -0,0 +1,134 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * Example iocost cost model: the builtin linear HDD formula with all
+ * costs doubled.
+ *
+ * The constants mirror what calc_lcoefs() derives from the AUTOP_HDD
+ * defaults (rbps=174019176 rseqiops=41708 rrandiops=370, w-side
+ * analog) in vtime units where 1s == 2^37. On a rotational device
+ * still on ctrl=auto, a device bound to this model through
+ * io.cost.model charges twice the builtin model under the same
+ * workload, which makes it a convenient way to verify that
+ * accounting goes through the BPF path. On a non-rotational device,
+ * or one with user-pinned coefficients, the ratio to the builtin
+ * model is arbitrary.
+ *
+ * The cursor handling mirrors the builtin: a zero cursor means "no
+ * previous IO", and the cursor is only advanced for READ and WRITE
+ * bios the builtin prices, so flushes and discards leave it alone.
+ *
+ * The model implements the full linear formula itself, including
+ * flushes: there is no fallback to the builtin model, a dataless
+ * WRITE|REQ_PREFLUSH is priced as a one-page write.
+ */
+
+#include "vmlinux.h"
+#include <bpf/bpf_helpers.h>
+#include <bpf/bpf_tracing.h>
+
+/* VTIME_PER_SEC comes from vmlinux.h (a BTF enum constant) */
+#define IOC_PAGE_SIZE 4096
+#define IOC_SECT_TO_PAGE_SHIFT 3 /* 512B sectors to 4kB pages */
+#define LCOEF_RANDIO_PAGES 4096 /* 16MB seek threshold */
+#define IOCOST_COST_F_MERGE (1ULL << 0) /* not in BTF: a plain macro */
+#define IOCOST_REQ_OP_MASK 0xff /* REQ_OP_MASK, not in BTF */
+
+/*
+ * DIV64_U64_ROUND_UP / DIV_ROUND_UP_ULL equivalents, folded at
+ * compile time
+ */
+#define RU(x, y) ((x) / (y) + (((x) % (y)) ? 1 : 0))
+
+#define RBPS 174019176ULL
+#define RSEQIOPS 41708ULL
+#define RRANDIOPS 370ULL
+#define WBPS 178075866ULL
+#define WSEQIOPS 42705ULL
+#define WRANDIOPS 378ULL
+
+#define RPAGE (RU(VTIME_PER_SEC, RU(RBPS, IOC_PAGE_SIZE)))
+#define RSEQIO (RU(VTIME_PER_SEC, RSEQIOPS) - RPAGE)
+#define RRANDIO (RU(VTIME_PER_SEC, RRANDIOPS) - RPAGE)
+#define WPAGE (RU(VTIME_PER_SEC, RU(WBPS, IOC_PAGE_SIZE)))
+#define WSEQIO (RU(VTIME_PER_SEC, WSEQIOPS) - WPAGE)
+#define WRANDIO (RU(VTIME_PER_SEC, WRANDIOPS) - WPAGE)
+
+/*
+ * per-cgroup cursor storage: keyed by the cgroup, freed with it, so
+ * the model never leaks or reuses stale per-cgroup state
+ */
+struct {
+ __uint(type, BPF_MAP_TYPE_CGRP_STORAGE);
+ __uint(map_flags, BPF_F_NO_PREALLOC);
+ __type(key, int);
+ __type(value, __u64);
+} cursor_store SEC(".maps");
+
+SEC("struct_ops")
+u64 BPF_PROG(iocost_2x_calc_cost, u64 opf, u64 nbytes, u64 sector,
+ struct blkcg *blkcg, u64 model_flags)
+{
+ u64 pages, seek_pages = 0, base, coef_page, randio, cost;
+
+ /* builtin truncates: max(sectors >> IOC_SECT_TO_PAGE_SHIFT, 1) */
+ pages = nbytes >> IOC_PAGE_SHIFT;
+ if (!pages)
+ pages = 1;
+
+ if ((opf & IOCOST_REQ_OP_MASK) == REQ_OP_READ) {
+ base = RSEQIO; coef_page = RPAGE; randio = RRANDIO;
+ } else if ((opf & IOCOST_REQ_OP_MASK) == REQ_OP_WRITE) {
+ base = WSEQIO; coef_page = WPAGE; randio = WRANDIO;
+ } else {
+ /*
+ * a fully owning model must price every op; unknown
+ * ops are priced as a single page write
+ */
+ base = 0; coef_page = WPAGE; randio = 0;
+ }
+
+ /*
+ * mirror the builtin cursor semantics: seek distance is only
+ * computed against a non-zero cursor, and the cursor is only
+ * advanced for bios the builtin prices (READ/WRITE with a
+ * non-zero size), so flushes and discards leave it alone.
+ * Like bio_end_sector(), the advance truncates to whole
+ * sectors
+ */
+ {
+ __u64 *cursor, cur;
+ int priced = (opf & IOCOST_REQ_OP_MASK) == REQ_OP_READ ||
+ (opf & IOCOST_REQ_OP_MASK) == REQ_OP_WRITE;
+
+ cursor = bpf_cgrp_storage_get(&cursor_store,
+ blkcg->css.cgroup, NULL,
+ BPF_LOCAL_STORAGE_GET_F_CREATE);
+ if (!cursor)
+ return 2 * (base + pages * coef_page);
+ cur = *cursor;
+ if (cur && priced) {
+ seek_pages = sector > cur ? sector - cur
+ : cur - sector;
+ seek_pages >>= IOC_SECT_TO_PAGE_SHIFT;
+ if (seek_pages > LCOEF_RANDIO_PAGES)
+ base = randio;
+ }
+ if (priced && nbytes &&
+ !(model_flags & IOCOST_COST_F_MERGE))
+ *cursor = sector + (nbytes >> 9);
+ }
+
+ if (model_flags & IOCOST_COST_F_MERGE)
+ base = 0;
+
+ cost = 2 * (base + pages * coef_page);
+ return cost;
+}
+
+SEC(".struct_ops")
+struct iocost_model_ops iocost_2x = {
+ .calc_cost = (void *)iocost_2x_calc_cost,
+ .name = "iocost_2x",
+};
+
+char LICENSE[] SEC("license") = "GPL";
diff --git a/tools/testing/selftests/bpf/progs/iocost_ms.c b/tools/testing/selftests/bpf/progs/iocost_ms.c
new file mode 100644
index 0000000000000..2a3c1d6aae4df
--- /dev/null
+++ b/tools/testing/selftests/bpf/progs/iocost_ms.c
@@ -0,0 +1,156 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * Example multi-stream sequentiality detection cost model.
+ *
+ * The builtin model keeps a single cursor per cgroup, so two
+ * interleaved sequential readers in one cgroup are all priced random
+ * (measured 89x overcharge, 12.9x throughput collapse), while random
+ * IO inside a hot window smaller than the 16MB seek threshold is
+ * priced sequential (measured 107x undercharge). This model replaces
+ * the single cursor with a per-cgroup table of stream slots: an IO is
+ * sequential iff its sector matches the expected next sector of any
+ * tracked stream. Interleaved streams keep their own slots, and
+ * windowed random IO rarely matches a moving expectation.
+ *
+ * Stream state lives in a CGRP_STORAGE map, so it is created and
+ * freed with the cgroup. The model implements the full builtin
+ * linear formula itself, including flush pricing.
+ */
+#include "vmlinux.h"
+#include <bpf/bpf_helpers.h>
+#include <bpf/bpf_tracing.h>
+
+/* VTIME_PER_SEC comes from vmlinux.h (a BTF enum constant) */
+#define IOC_PAGE_SIZE 4096
+#define IOC_SECT_TO_PAGE_SHIFT 3 /* 512B sectors to 4kB pages */
+#define IOCOST_COST_F_MERGE (1ULL << 0) /* not in BTF: a plain macro */
+#define IOCOST_REQ_OP_MASK 0xff /* REQ_OP_MASK, not in BTF */
+
+/*
+ * DIV64_U64_ROUND_UP / DIV_ROUND_UP_ULL equivalents, folded at
+ * compile time
+ */
+#define RU(x, y) ((x) / (y) + (((x) % (y)) ? 1 : 0))
+
+#define RBPS 174019176ULL
+#define RSEQIOPS 41708ULL
+#define RRANDIOPS 370ULL
+#define WBPS 178075866ULL
+#define WSEQIOPS 42705ULL
+#define WRANDIOPS 378ULL
+
+#define RPAGE (RU(VTIME_PER_SEC, RU(RBPS, IOC_PAGE_SIZE)))
+#define RSEQIO (RU(VTIME_PER_SEC, RSEQIOPS) - RPAGE)
+#define RRANDIO (RU(VTIME_PER_SEC, RRANDIOPS) - RPAGE)
+#define WPAGE (RU(VTIME_PER_SEC, RU(WBPS, IOC_PAGE_SIZE)))
+#define WSEQIO (RU(VTIME_PER_SEC, WSEQIOPS) - WPAGE)
+#define WRANDIO (RU(VTIME_PER_SEC, WRANDIOPS) - WPAGE)
+
+#define NSLOTS 4
+
+struct streams {
+ __u64 expected[NSLOTS]; /* next expected sector, per stream */
+ __u64 stamp[NSLOTS]; /* LRU stamp, 0 = empty */
+};
+
+/*
+ * per-cgroup stream table: keyed by the cgroup, freed with it
+ */
+struct {
+ __uint(type, BPF_MAP_TYPE_CGRP_STORAGE);
+ __uint(map_flags, BPF_F_NO_PREALLOC);
+ __type(key, int);
+ __type(value, struct streams);
+} stream_tab SEC(".maps");
+
+SEC("struct_ops")
+u64 BPF_PROG(iocost_ms_calc_cost, u64 opf, u64 nbytes, u64 sector,
+ struct blkcg *blkcg, u64 model_flags)
+{
+ struct streams *s;
+ u64 pages, base, coef_page, randio, advance, now;
+ u32 i, victim = 0, found = 0xFFFFFFFF;
+
+ if ((opf & IOCOST_REQ_OP_MASK) == REQ_OP_READ) {
+ base = RSEQIO; coef_page = RPAGE; randio = RRANDIO;
+ } else if ((opf & IOCOST_REQ_OP_MASK) == REQ_OP_WRITE) {
+ base = WSEQIO; coef_page = WPAGE; randio = WRANDIO;
+ } else {
+ /*
+ * a fully owning model must price every op; unknown
+ * ops are priced as per-page writes
+ */
+ base = 0; coef_page = WPAGE; randio = 0;
+ }
+ advance = nbytes >> 9; /* whole sectors, like bio_end_sector() */
+
+ /* only bios the builtin prices participate in stream tracking */
+ if (!(((opf & IOCOST_REQ_OP_MASK) == REQ_OP_READ ||
+ (opf & IOCOST_REQ_OP_MASK) == REQ_OP_WRITE) && nbytes)) {
+ pages = nbytes >> IOC_PAGE_SHIFT;
+ if (!pages)
+ pages = 1;
+ return base + pages * coef_page;
+ }
+
+ s = bpf_cgrp_storage_get(&stream_tab, blkcg->css.cgroup, NULL,
+ BPF_LOCAL_STORAGE_GET_F_CREATE);
+ if (!s) {
+ /* no storage: price per page, truncating like the builtin */
+ pages = nbytes >> IOC_PAGE_SHIFT;
+ if (!pages)
+ pages = 1;
+ return base + pages * coef_page;
+ }
+
+ /*
+ * Slot access is lockless, mirroring the builtin single-cursor
+ * update in ioc_rqos_throttle(): concurrent CPUs submitting for
+ * the same cgroup can race on slot updates; mispricing is
+ * bounded and acceptable for an example model.
+ */
+ now = bpf_ktime_get_ns();
+ for (i = 0; i < NSLOTS; i++) {
+ if (s->expected[i] == sector && s->stamp[i]) {
+ found = i;
+ break;
+ }
+ }
+ if (found != 0xFFFFFFFF) {
+ /* sequential: keep the seq base from the op branch */
+ s->expected[found] = sector + advance;
+ s->stamp[found] = now;
+ } else {
+ base = randio;
+ for (i = 1; i < NSLOTS; i++) {
+ if (s->stamp[i] < s->stamp[victim])
+ victim = i;
+ }
+ s->expected[victim] = sector + advance;
+ s->stamp[victim] = now;
+ }
+
+ /* builtin truncates: max(sectors >> IOC_SECT_TO_PAGE_SHIFT, 1) */
+ pages = nbytes >> IOC_PAGE_SHIFT;
+ if (!pages)
+ pages = 1;
+ if (model_flags & IOCOST_COST_F_MERGE) {
+ /*
+ * merged bios skip the base cost but still advance
+ * the stream position above, so a merge at the
+ * expected sector does not make the following new IO
+ * look random
+ */
+ base = 0;
+ }
+
+ return base + pages * coef_page;
+}
+
+SEC(".struct_ops")
+struct iocost_model_ops iocost_ms = {
+ .calc_cost = (void *)iocost_ms_calc_cost,
+ .name = "iocost_ms",
+};
+
+char LICENSE[] SEC("license") = "GPL";
|
| 3/2 | 2026/09/14 07:59 | llm | patch-evaluator |
1mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 20248 output: 34 thoughts: 608 Thoughts: **My Initial Assessment of the Patch** Okay, let's break this down. This patch is targeting the amd64 architecture, and it's a significant one. It's all about adding BPF-based cost model support to the block layer's `io.cost` controller for cgroups. From what I can see, it touches a decent chunk of the code. The changes involve: updating the cgroup-v2 documentation, introducing a new Kconfig option (`BLK_CGROUP_IOCOST_BPF`), adding a new object file (`blk-iocost-bpf.o`) to the Makefile, modifying `blk-cgroup.c` to handle online/offline notifications for the BPF model, and, most importantly, a completely new file (`blk-iocost-bpf.c`) that defines the BPF-related structures and lifecycle management. It also adds BPF cost model support and trace events to `blk-iocost.c`. There are also include file updates and additions to the selftests directory. Now, considering the triage philosophy, the first question is always reachability. Can this code path be executed in a standard virtualized environment? Absolutely. This is dealing with the `io.cost` controller, so userspace can manipulate cgroups, attach BPF programs, write to `/sys/fs/cgroup/io.cost.model`, trigger block device I/O, blkcg online/offline, timer events, etc. The good thing is that this can be tested with standard block devices like virtio-blk, loop, null_blk or nvme/scsi. Given the scope of the change, the next question is if it's worth fuzzing? Yes, this seems like prime fuzzing territory. This patch introduces a lot of new elements: BPF struct_ops, memory management with refcounting, lists, mutexes, RCU reads in the I/O path, and lifecycle hooks with user configuration through sysfs/cgroupfs writes. This is a complex area with potential for edge cases, so fuzzing is almost a must. The next thing to investigate is the configuration options. The patch introduces a new Kconfig option `BLK_CGROUP_IOCOST_BPF`. This option depends on `BLK_CGROUP_IOCOST`, and requires `BPF_SYSCALL`, `BPF_JIT` and `DEBUG_INFO_BTF`. I need to figure out what `BLK_CGROUP_IOCOST` depends on to complete the set up. It's time to fire up `grepper` or `read-file` to explore `block/Kconfig`. |
| 4/2 | 2026/09/14 08:01 | tool | grepper |
0mArgs:{
"Expression": "config BLK_CGROUP_IOCOST",
"PathPrefix": "block/Kconfig"
}Results: {
"Output": "block/Kconfig=145=config BLK_CGROUP_FC_APPID\n--\nblock/Kconfig-153-\nblock/Kconfig:154:config BLK_CGROUP_IOCOST\nblock/Kconfig-155-\tbool \"Enable support for cost model based cgroup IO controller\"\n--\nblock/Kconfig=232=source \"block/Kconfig.iosched\"\nblock/Kconfig-233-\nblock/Kconfig:234:config BLK_CGROUP_IOCOST_BPF\nblock/Kconfig-235-\tbool \"Enable BPF pluggable cost model support for the cost IO controller\"\n"
} |
| 5/2 | 2026/09/14 08:01 | llm | patch-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 21627 output: 32 thoughts: 20 Thoughts: Here's my summary, as though I'm processing those lines in `block/Kconfig`: **Analyzing Kconfig: Delving into Block Device Configuration** Alright, time to dive into lines 150-165 of `block/Kconfig`. I'm expecting this section to define some crucial configuration options related to block devices, given the file context. I'll be looking for dependencies, default values, and the general structure of how these settings are presented. My radar is up for anything that deals with performance, device drivers, or specific block device features. Perhaps we're setting up defaults for cache behavior, error handling, or specific hardware compatibility – the possibilities are extensive within this part of the kernel. I need to understand what's being offered to the user in this configuration section. I'll pay careful attention to any help text associated with these options, as that's often the key to what the developers intended. The goal is a clear overview of any new options or changes introduced in this region of the configuration system. |
| 6/2 | 2026/09/14 08:01 | tool | read-file |
0mArgs:{
"File": "block/Kconfig",
"FirstLine": 150,
"LineCount": 20
}Results: {
"Contents": " 150:\t\t cgroup applications. It enables the Fabric and the storage targets to\n 151:\t\t identify, monitor, and handle FC traffic based on VM tags by inserting\n 152:\t\t application specific identification into the FC frame.\n 153:\t\n 154:\tconfig BLK_CGROUP_IOCOST\n 155:\t\tbool \"Enable support for cost model based cgroup IO controller\"\n 156:\t\tdepends on BLK_CGROUP\n 157:\t\tselect BLK_RQ_ALLOC_TIME\n 158:\t\thelp\n 159:\t\tEnabling this option enables the .weight interface for cost\n 160:\t\tmodel based proportional IO control. The IO controller\n 161:\t\tdistributes IO capacity between different groups based on\n 162:\t\ttheir share of the overall weight distribution.\n 163:\t\n 164:\tconfig BLK_CGROUP_IOPRIO\n 165:\t\tbool \"Cgroup I/O controller for assigning an I/O priority class\"\n 166:\t\tdepends on BLK_CGROUP\n 167:\t\thelp\n 168:\t\tEnable the .prio interface for assigning an I/O priority class to\n 169:\t\trequests. The I/O priority class affects the order in which an I/O\n"
} |
| 7/2 | 2026/09/14 08:01 | llm | patch-evaluator |
3mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 22236 output: 34 thoughts: 223 Thoughts: **My Analysis of Block I/O Cost BPF Dependencies** Okay, let's break this down. It looks like the `BLK_CGROUP_IOCOST_BPF` feature, which I'm assuming is the block I/O cost accounting implemented with BPF, relies on a cascade of dependencies. First, it directly needs `BLK_CGROUP_IOCOST`, and it also explicitly requires `BPF_SYSCALL`, `BPF_JIT`, and `DEBUG_INFO_BTF`. This is pretty standard for any BPF-related functionality; the JIT and BTF are there for performance and debugging, while the syscall hook enables BPF program execution. I'm also betting the I/O cost feature depends on cgroups, since it needs `BLK_CGROUP_IOCOST` which relies on `BLK_CGROUP`. Now, digging a bit deeper into the code, specifically `block/blk-iocost-bpf.c`, I see a function `bpf_iocost_get_func_proto` that handles BPF function prototypes. It uses a `switch` statement and, crucially, conditionally includes the `BPF_FUNC_cgrp_storage_get` prototype based on the `CONFIG_CGROUPS` macro. This makes perfect sense; the BPF program needs to interact with cgroup storage to do its job. Finally, I'm a bit curious about `BPF_STRUCT_OPS`. I'm pretty sure I saw something about it depending on `BPF_SYSCALL`, but I need to double-check that dependency to be absolutely sure. |
| 8/2 | 2026/09/14 08:05 | tool | grepper |
0mError:no matches Args: {
"Expression": "config BPF_STRUCT_OPS",
"PathPrefix": "kernel/bpf/Kconfig"
}Results: {
"error": "no matches"
} |
| 9/2 | 2026/09/14 08:05 | llm | patch-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 22855 output: 28 thoughts: 19 Thoughts: Here's my attempt at summarizing the thought process, tailored for an expert audience: **Hunting for `BPF_STRUCT_OPS` in Kconfig** Okay, so I need to track down the usage of `BPF_STRUCT_OPS` within the kernel's Kconfig infrastructure. This probably means I'm looking for the features or modules that depend on or are *configured* by that option. It's a pretty targeted search, so I can probably script something to comb through the Kconfig files. I'll need to make sure I capture all the configurations, including any conditional dependencies like `if` statements. I should probably start with a recursive `grep` across the `Kconfig` directory, that'll be the fastest way to get a list of the Kconfig files that reference it. I'll need to figure out how to filter that output and parse it to get a list of the locations. From there, I can start to understand the *why* - which subsystems are enabling/disabling this, and how it's being used within the build system. |
| 10/2 | 2026/09/14 08:05 | tool | grepper |
0mArgs:{
"Expression": "BPF_STRUCT_OPS",
"PathPrefix": "kernel/"
}Results: {
"Output": "kernel/bpf/bpf_struct_ops.c=358=int bpf_struct_ops_desc_init(struct bpf_struct_ops_desc *st_ops_desc,\n--\nkernel/bpf/bpf_struct_ops.c-391-\tt = btf_type_by_id(btf, type_id);\nkernel/bpf/bpf_struct_ops.c:392:\tif (btf_type_vlen(t) \u003e BPF_STRUCT_OPS_MAX_NR_MEMBERS) {\nkernel/bpf/bpf_struct_ops.c-393-\t\tpr_warn(\"Cannot support #%u members in struct %s\\n\",\n--\nkernel/bpf/bpf_struct_ops.c=521=int bpf_struct_ops_map_sys_lookup_elem(struct bpf_map *map, void *key,\n--\nkernel/bpf/bpf_struct_ops.c-534-\tstate = smp_load_acquire(\u0026kvalue-\u003ecommon.state);\nkernel/bpf/bpf_struct_ops.c:535:\tif (state == BPF_STRUCT_OPS_STATE_INIT) {\nkernel/bpf/bpf_struct_ops.c-536-\t\tmemset(value, 0, map-\u003evalue_size);\n--\nkernel/bpf/bpf_struct_ops.c=726=static long bpf_struct_ops_map_update_elem(struct bpf_map *map, void *key,\n--\nkernel/bpf/bpf_struct_ops.c-771-\nkernel/bpf/bpf_struct_ops.c:772:\tif (kvalue-\u003ecommon.state != BPF_STRUCT_OPS_STATE_INIT) {\nkernel/bpf/bpf_struct_ops.c-773-\t\terr = -EBUSY;\n--\nkernel/bpf/bpf_struct_ops.c-919-\t\t */\nkernel/bpf/bpf_struct_ops.c:920:\t\tsmp_store_release(\u0026kvalue-\u003ecommon.state, BPF_STRUCT_OPS_STATE_READY);\nkernel/bpf/bpf_struct_ops.c-921-\t\tgoto unlock;\n--\nkernel/bpf/bpf_struct_ops.c-934-\t\t * It ensures the above udata updates (e.g. prog-\u003eaux-\u003eid)\nkernel/bpf/bpf_struct_ops.c:935:\t\t * can be seen once BPF_STRUCT_OPS_STATE_INUSE is set.\nkernel/bpf/bpf_struct_ops.c-936-\t\t */\nkernel/bpf/bpf_struct_ops.c:937:\t\tsmp_store_release(\u0026kvalue-\u003ecommon.state, BPF_STRUCT_OPS_STATE_INUSE);\nkernel/bpf/bpf_struct_ops.c-938-\t\tgoto unlock;\n--\nkernel/bpf/bpf_struct_ops.c=962=static long bpf_struct_ops_map_delete_elem(struct bpf_map *map, void *key)\n--\nkernel/bpf/bpf_struct_ops.c-971-\tprev_state = cmpxchg(\u0026st_map-\u003ekvalue.common.state,\nkernel/bpf/bpf_struct_ops.c:972:\t\t\t BPF_STRUCT_OPS_STATE_INUSE,\nkernel/bpf/bpf_struct_ops.c:973:\t\t\t BPF_STRUCT_OPS_STATE_TOBEFREE);\nkernel/bpf/bpf_struct_ops.c-974-\tswitch (prev_state) {\nkernel/bpf/bpf_struct_ops.c:975:\tcase BPF_STRUCT_OPS_STATE_INUSE:\nkernel/bpf/bpf_struct_ops.c-976-\t\tst_map-\u003est_ops_desc-\u003est_ops-\u003eunreg(\u0026st_map-\u003ekvalue.data, NULL);\n--\nkernel/bpf/bpf_struct_ops.c-978-\t\treturn 0;\nkernel/bpf/bpf_struct_ops.c:979:\tcase BPF_STRUCT_OPS_STATE_TOBEFREE:\nkernel/bpf/bpf_struct_ops.c-980-\t\treturn -EINPROGRESS;\nkernel/bpf/bpf_struct_ops.c:981:\tcase BPF_STRUCT_OPS_STATE_INIT:\nkernel/bpf/bpf_struct_ops.c-982-\t\treturn -ENOENT;\n--\nkernel/bpf/bpf_struct_ops.c=1284=static bool bpf_struct_ops_valid_to_reg(struct bpf_map *map)\n--\nkernel/bpf/bpf_struct_ops.c-1290-\t\t/* Pair with smp_store_release() during map_update */\nkernel/bpf/bpf_struct_ops.c:1291:\t\tsmp_load_acquire(\u0026st_map-\u003ekvalue.common.state) == BPF_STRUCT_OPS_STATE_READY;\nkernel/bpf/bpf_struct_ops.c-1292-}\n--\nkernel/bpf/syscall.c=5793=static int link_create(union bpf_attr *attr, bpfptr_t uattr)\n--\nkernel/bpf/syscall.c-5800-\nkernel/bpf/syscall.c:5801:\tif (attr-\u003elink_create.attach_type == BPF_STRUCT_OPS)\nkernel/bpf/syscall.c-5802-\t\treturn bpf_struct_ops_link_create(attr);\n"
} |
| 11/2 | 2026/09/14 08:05 | llm | patch-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 24549 output: 22 thoughts: 18 Thoughts: Here's my attempt at summarizing the thought process, tailored for an expert audience: **Tracing the BPF Structure Operations Registration** Okay, so I need to understand how these BPF structure operations are being registered. The starting point seems to be `register_bpf_struct_ops`. Let's dig into that function definition. I'm assuming it's probably handling the registration of a new struct's helper functions, which will need to be made available to BPF programs. I'm expecting this to involve setting up some kind of data structure, likely a global registry or table, to link the struct's name (or a unique ID) with pointers to the relevant BPF helper functions (like `load` and `store`). The implementation may also involve some type checking to make sure the passed functions adhere to the expected signatures. I'm curious if this registration also handles things like potential reference counting or locking to ensure thread-safety. I'm guessing that it may interact with the BPF subsystem's internal representation of the kernel's memory map for accessing structures from BPF programs. I'm interested in how it deals with potential conflicts during registration, especially if multiple modules are trying to register operations on the same structure. Time to dive into the source and see what's really happening! |
| 12/2 | 2026/09/14 08:05 | tool | grepper |
0mArgs:{
"Expression": "register_bpf_struct_ops"
}Results: {
"Output": "block/blk-iocost-bpf.c=303=static int __init bpf_iocost_init(void)\nblock/blk-iocost-bpf.c-304-{\nblock/blk-iocost-bpf.c:305:\treturn register_bpf_struct_ops(\u0026bpf_iocost_model_ops, iocost_model_ops);\nblock/blk-iocost-bpf.c-306-}\n--\ndrivers/hid/bpf/hid_bpf_struct_ops.c=337=static int __init hid_bpf_struct_ops_init(void)\ndrivers/hid/bpf/hid_bpf_struct_ops.c-338-{\ndrivers/hid/bpf/hid_bpf_struct_ops.c:339:\treturn register_bpf_struct_ops(\u0026bpf_hid_bpf_ops, hid_bpf_ops);\ndrivers/hid/bpf/hid_bpf_struct_ops.c-340-}\n--\nfs/binfmt_misc_bpf.c=430=static int __init bm_bpf_struct_ops_init(void)\nfs/binfmt_misc_bpf.c-431-{\nfs/binfmt_misc_bpf.c:432:\treturn register_bpf_struct_ops(\u0026bpf_binfmt_misc_ops, binfmt_misc_ops);\nfs/binfmt_misc_bpf.c-433-}\n--\ninclude/linux/bpf.h=2200=static inline bool is_struct_ops_tramp(const struct bpf_tramp_nodes *fentry_nodes)\n--\ninclude/linux/bpf.h-2208- * type information correctly. Developers should use this macro to register\ninclude/linux/bpf.h:2209: * a struct_ops type instead of calling __register_bpf_struct_ops() directly.\ninclude/linux/bpf.h-2210- */\ninclude/linux/bpf.h:2211:#define register_bpf_struct_ops(st_ops, type)\t\t\t\t\\\ninclude/linux/bpf.h-2212-\t({\t\t\t\t\t\t\t\t\\\n--\ninclude/linux/bpf.h-2217-\t\tBTF_TYPE_EMIT(struct bpf_struct_ops_##type);\t\t\\\ninclude/linux/bpf.h:2218:\t\t__register_bpf_struct_ops(st_ops);\t\t\t\\\ninclude/linux/bpf.h-2219-\t})\n--\ninclude/linux/bpf.h=2276=void bpf_struct_ops_desc_release(struct bpf_struct_ops_desc *st_ops_desc);\ninclude/linux/bpf.h-2277-#else\ninclude/linux/bpf.h:2278:#define register_bpf_struct_ops(st_ops, type) ({ (void *)(st_ops); 0; })\ninclude/linux/bpf.h-2279-static inline bool bpf_try_module_get(const void *data, struct module *owner)\n--\ninclude/linux/btf.h=541=struct bpf_struct_ops;\ninclude/linux/btf.h:542:int __register_bpf_struct_ops(struct bpf_struct_ops *st_ops);\ninclude/linux/btf.h-543-const struct bpf_struct_ops_desc *bpf_struct_ops_find_value(struct btf *btf, u32 value_id);\n--\nio_uring/bpf-ops.c=263=static int __init io_uring_bpf_init(void)\n--\nio_uring/bpf-ops.c-266-\nio_uring/bpf-ops.c:267:\tret = register_bpf_struct_ops(\u0026bpf_ring_ops, io_uring_bpf_ops);\nio_uring/bpf-ops.c-268-\tif (ret) {\n--\nkernel/bpf/btf.c=9995=bpf_struct_ops_find(struct btf *btf, u32 type_id)\n--\nkernel/bpf/btf.c-10015-\nkernel/bpf/btf.c:10016:int __register_bpf_struct_ops(struct bpf_struct_ops *st_ops)\nkernel/bpf/btf.c-10017-{\n--\nkernel/bpf/btf.c-10043-}\nkernel/bpf/btf.c:10044:EXPORT_SYMBOL_GPL(__register_bpf_struct_ops);\nkernel/bpf/btf.c-10045-#endif\n--\nkernel/sched/ext/ext.c=10934=static int __init scx_init(void)\n--\nkernel/sched/ext/ext.c-11054-\nkernel/sched/ext/ext.c:11055:\tret = register_bpf_struct_ops(\u0026bpf_sched_ext_ops, sched_ext_ops);\nkernel/sched/ext/ext.c-11056-\tif (ret) {\n--\nkernel/sched/ext/ext.c-11060-\nkernel/sched/ext/ext.c:11061:\tret = register_bpf_struct_ops(\u0026bpf_sched_ext_ops_cid, sched_ext_ops_cid);\nkernel/sched/ext/ext.c-11062-\tif (ret) {\n--\nnet/bpf/bpf_dummy_struct_ops.c=319=static int __init bpf_dummy_struct_ops_init(void)\nnet/bpf/bpf_dummy_struct_ops.c-320-{\nnet/bpf/bpf_dummy_struct_ops.c:321:\treturn register_bpf_struct_ops(\u0026bpf_bpf_dummy_ops, bpf_dummy_ops);\nnet/bpf/bpf_dummy_struct_ops.c-322-}\n--\nnet/ipv4/bpf_tcp_ca.c=346=static int __init bpf_tcp_ca_kfunc_init(void)\n--\nnet/ipv4/bpf_tcp_ca.c-350-\tret = register_btf_kfunc_id_set(BPF_PROG_TYPE_STRUCT_OPS, \u0026bpf_tcp_ca_kfunc_set);\nnet/ipv4/bpf_tcp_ca.c:351:\tret = ret ?: register_bpf_struct_ops(\u0026bpf_tcp_congestion_ops, tcp_congestion_ops);\nnet/ipv4/bpf_tcp_ca.c-352-\n--\nnet/sched/bpf_qdisc.c=460=static int __init bpf_qdisc_kfunc_init(void)\n--\nnet/sched/bpf_qdisc.c-473-\t\t\t\t\t\t THIS_MODULE);\nnet/sched/bpf_qdisc.c:474:\tret = ret ?: register_bpf_struct_ops(\u0026bpf_Qdisc_ops, Qdisc_ops);\nnet/sched/bpf_qdisc.c-475-\n--\nnet/smc/smc_hs_bpf.c=137=int bpf_smc_hs_ctrl_init(void)\nnet/smc/smc_hs_bpf.c-138-{\nnet/smc/smc_hs_bpf.c:139:\treturn register_bpf_struct_ops(\u0026bpf_smc_hs_ctrl_ops, smc_hs_ctrl);\nnet/smc/smc_hs_bpf.c-140-}\n--\ntools/testing/selftests/bpf/test_kmods/bpf_test_no_cfi.c=59=static int bpf_test_no_cfi_init(void)\n--\ntools/testing/selftests/bpf/test_kmods/bpf_test_no_cfi.c-62-\ntools/testing/selftests/bpf/test_kmods/bpf_test_no_cfi.c:63:\tret = register_bpf_struct_ops(\u0026test_no_cif_ops,\ntools/testing/selftests/bpf/test_kmods/bpf_test_no_cfi.c-64-\t\t\t\t bpf_test_no_cfi_ops);\n--\ntools/testing/selftests/bpf/test_kmods/bpf_test_no_cfi.c-68-\ttest_no_cif_ops.cfi_stubs = \u0026__test_no_cif_ops;\ntools/testing/selftests/bpf/test_kmods/bpf_test_no_cfi.c:69:\tret = register_bpf_struct_ops(\u0026test_no_cif_ops,\ntools/testing/selftests/bpf/test_kmods/bpf_test_no_cfi.c-70-\t\t\t\t bpf_test_no_cfi_ops);\n--\ntools/testing/selftests/bpf/test_kmods/bpf_testmod.c=2312=static int bpf_testmod_init(void)\n--\ntools/testing/selftests/bpf/test_kmods/bpf_testmod.c-2328-\tret = ret ?: register_btf_fmodret_id_set(\u0026bpf_testmod_trampoline_count_fmodret_set);\ntools/testing/selftests/bpf/test_kmods/bpf_testmod.c:2329:\tret = ret ?: register_bpf_struct_ops(\u0026bpf_bpf_testmod_ops, bpf_testmod_ops);\ntools/testing/selftests/bpf/test_kmods/bpf_testmod.c:2330:\tret = ret ?: register_bpf_struct_ops(\u0026bpf_testmod_ops2, bpf_testmod_ops2);\ntools/testing/selftests/bpf/test_kmods/bpf_testmod.c:2331:\tret = ret ?: register_bpf_struct_ops(\u0026bpf_testmod_ops3, bpf_testmod_ops3);\ntools/testing/selftests/bpf/test_kmods/bpf_testmod.c:2332:\tret = ret ?: register_bpf_struct_ops(\u0026testmod_st_ops, bpf_testmod_st_ops);\ntools/testing/selftests/bpf/test_kmods/bpf_testmod.c:2333:\tret = ret ?: register_bpf_struct_ops(\u0026testmod_multi_st_ops, bpf_testmod_multi_st_ops);\ntools/testing/selftests/bpf/test_kmods/bpf_testmod.c-2334-\tret = ret ?: register_btf_id_dtor_kfuncs(bpf_testmod_dtors,\n"
} |
| 13/2 | 2026/09/14 08:05 | llm | patch-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 27635 output: 39 thoughts: 59 Thoughts: **Investigating BPF Structure Operations** Okay, so I'm diving into this `BPF_STRUCT_OPS` thing in the kernel. My initial thought is to confirm my suspicion that its use is contingent on `CONFIG_BPF_JIT` being enabled. Seems logical, given the JIT's role in optimizing BPF bytecode and the likely integration of structure operations within that context. To confirm this, I need to check the definition of `__register_bpf_struct_ops`. I'll jump into `kernel/bpf/btf.c` and see what the guard conditions are. This will tell me definitively whether JIT is a hard requirement or if there's some other path, or conditional compilation involved in the activation of the relevant functionality. Gotta keep digging to be sure. |
| 14/2 | 2026/09/14 08:05 | tool | read-file |
0mArgs:{
"File": "kernel/bpf/btf.c",
"FirstLine": 10010,
"LineCount": 20
}Results: {
"Contents": "10010:\t\t\t\treturn \u0026st_ops_list[i];\n10011:\t\t}\n10012:\t\n10013:\t\treturn NULL;\n10014:\t}\n10015:\t\n10016:\tint __register_bpf_struct_ops(struct bpf_struct_ops *st_ops)\n10017:\t{\n10018:\t\tstruct bpf_verifier_log *log;\n10019:\t\tstruct btf *btf;\n10020:\t\tint err = 0;\n10021:\t\n10022:\t\tbtf = btf_get_module_btf(st_ops-\u003eowner);\n10023:\t\tif (!btf)\n10024:\t\t\treturn check_btf_kconfigs(st_ops-\u003eowner, \"struct_ops\");\n10025:\t\tif (IS_ERR(btf))\n10026:\t\t\treturn PTR_ERR(btf);\n10027:\t\n10028:\t\tlog = kzalloc_obj(*log, GFP_KERNEL | __GFP_NOWARN);\n10029:\t\tif (!log) {\n"
} |
| 15/2 | 2026/09/14 08:05 | llm | patch-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 28223 output: 38 thoughts: 43 Thoughts: Okay, let's dive into this. Here's how I'd approach that snippet of code and what I'd be thinking: **Examining Conditional Compilation in BPF BTF Registration** Alright, my first instinct is to zero in on those `#ifdef` directives surrounding `__register_bpf_struct_ops` in both `kernel/bpf/btf.c` and `include/linux/bpf.h`. Clearly, this function, or at least its declaration and definition, is conditionally compiled. This immediately screams "feature flag" or "platform-specific implementation" to me. I want to know *why* it's guarded. What triggers the preprocessor to include or exclude this function? What are the likely conditions? My immediate questions are: Is it tied to a specific kernel version? Perhaps a particular hardware architecture? Or maybe a dependency on another kernel module or feature being enabled? I'd probably go straight to examining where that macro, whatever it is, is defined and how it's used elsewhere in the build system. What are the relevant Kconfig options? I'd also consider the implications of *not* defining the macro. What are the fallback behaviors? Does the code gracefully handle the missing function, or does it lead to a compilation error? How does the lack of this function affect the BTF (BPF Type Format) metadata registration process? I'd bet it has to do with some optimization or feature for handling BTF data structures. Perhaps it's an optimization for a certain BTF use case, or for a specific feature set of the BPF verifier. Perhaps it is a compatibility issue with older BTF code. My next step would be to search for uses of this macro in the kernel code. Where else is it conditional? This would probably give me a lot more context quickly. This could be a crucial part of my current research on BPF BTF data handling. |
| 16/2 | 2026/09/14 08:05 | tool | grepper |
0mArgs:{
"Expression": "bpf_struct_ops_desc_release",
"PathPrefix": "include/linux/bpf.h"
}Results: {
"Output": "include/linux/bpf.h=2275=void bpf_map_struct_ops_info_fill(struct bpf_map_info *info, struct bpf_map *map);\ninclude/linux/bpf.h:2276:void bpf_struct_ops_desc_release(struct bpf_struct_ops_desc *st_ops_desc);\ninclude/linux/bpf.h-2277-#else\n--\ninclude/linux/bpf.h=2312=static inline void bpf_map_struct_ops_info_fill(struct bpf_map_info *info, struct bpf_map *map)\n--\ninclude/linux/bpf.h-2315-\ninclude/linux/bpf.h:2316:static inline void bpf_struct_ops_desc_release(struct bpf_struct_ops_desc *st_ops_desc)\ninclude/linux/bpf.h-2317-{\n"
} |
| 17/2 | 2026/09/14 08:05 | llm | patch-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 28954 output: 37 thoughts: 26 Thoughts: Here's my take on those lines from `include/linux/bpf.h`. **Diving into BPF Header Definitions** Alright, let's pull up lines 2200-2225 from `include/linux/bpf.h`. This section is likely defining core structures or constants that are fundamental to the BPF (Berkeley Packet Filter) architecture within the Linux kernel. Given the location within the `include` directory, it's safe to assume these are meant for both kernel internal use and for userspace BPF program development. I'd expect to see definitions related to instruction encoding, error codes, helper function signatures, or perhaps some of the more complex data structures used to represent BPF programs. Things like register definitions, flag bits for various program states, or macros that simplify writing BPF instructions are all likely candidates. The sheer volume of BPF functionality in recent kernel releases necessitates a well-defined and comprehensive header file. So, I anticipate these lines cover fairly low-level elements, potentially the building blocks used in the BPF verifier or the JIT (Just-In-Time) compiler. I'm keen to see if there are any recent additions or modifications in these lines, as BPF is rapidly evolving. Let's see if there's anything surprising. |
| 18/2 | 2026/09/14 08:05 | tool | read-file |
0mArgs:{
"File": "include/linux/bpf.h",
"FirstLine": 2200,
"LineCount": 25
}Results: {
"Contents": "2200:\tstatic inline bool is_struct_ops_tramp(const struct bpf_tramp_nodes *fentry_nodes)\n2201:\t{\n2202:\t\treturn fentry_nodes-\u003enr_nodes == 1 \u0026\u0026\n2203:\t\t fentry_nodes-\u003enodes[0]-\u003elink-\u003etype == BPF_LINK_TYPE_STRUCT_OPS;\n2204:\t}\n2205:\t\n2206:\t#if defined(CONFIG_BPF_JIT) \u0026\u0026 defined(CONFIG_BPF_SYSCALL)\n2207:\t/* This macro helps developer to register a struct_ops type and generate\n2208:\t * type information correctly. Developers should use this macro to register\n2209:\t * a struct_ops type instead of calling __register_bpf_struct_ops() directly.\n2210:\t */\n2211:\t#define register_bpf_struct_ops(st_ops, type)\t\t\t\t\\\n2212:\t\t({\t\t\t\t\t\t\t\t\\\n2213:\t\t\tstruct bpf_struct_ops_##type {\t\t\t\t\\\n2214:\t\t\t\tstruct bpf_struct_ops_common_value common;\t\\\n2215:\t\t\t\tstruct type data ____cacheline_aligned_in_smp;\t\\\n2216:\t\t\t};\t\t\t\t\t\t\t\\\n2217:\t\t\tBTF_TYPE_EMIT(struct bpf_struct_ops_##type);\t\t\\\n2218:\t\t\t__register_bpf_struct_ops(st_ops);\t\t\t\\\n2219:\t\t})\n2220:\t#define BPF_MODULE_OWNER ((void *)((0xeB9FUL \u003c\u003c 2) + POISON_POINTER_DELTA))\n2221:\tbool bpf_struct_ops_get(const void *kdata);\n2222:\tvoid bpf_struct_ops_put(const void *kdata);\n2223:\tint bpf_struct_ops_supported(const struct bpf_struct_ops *st_ops, u32 moff);\n2224:\tint bpf_struct_ops_map_sys_lookup_elem(struct bpf_map *map, void *key,\n"
} |
| 19/2 | 2026/09/14 08:05 | llm | patch-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 29847 output: 25 thoughts: 173 Thoughts: **Examining the BPF IO Cost Controller Integration** Okay, so I'm looking at this kernel configuration, specifically `BLK_CGROUP_IOCOST_BPF`. It looks like it enables BPF (Berkeley Packet Filter) integration for the I/O cost controller within CGroups. The dependency chain is pretty clear: I need `BLK_CGROUP_IOCOST` and several other BPF-related features, like `BPF_SYSCALL`, `BPF_JIT`, and `DEBUG_INFO_BTF`. And, naturally, `BLK_CGROUP_IOCOST` itself relies on `BLK_CGROUP`. Standard stuff, keeping the dependencies clean. Now, I'm thinking about the actual implementation. I'm focusing on the `block/blk-iocost-bpf.c` file. The use of `#ifdef CONFIG_CGROUPS` around `bpf_cgrp_storage_get_proto` is interesting. It indicates that the functionality within this file relies on the core cgroup infrastructure being available. That's a good sanity check; you wouldn't want to use cgroup-related function calls without cgroups enabled. Finally, I need to check `BPF_FUNC_cgrp_storage_get`. This is probably the critical BPF function that's allowing the integration. I'll need to dig into `kernel/bpf/` to understand exactly how that function works and how it interfaces with the cgroup data structures. I want to know how the cost calculations are being done using BPF, how the BPF programs get attached to cgroups, and how the data is being made available to the BPF program, if I'm going to work on this code. |
| 20/2 | 2026/09/14 08:05 | tool | grepper |
0mArgs:{
"Expression": "bpf_cgrp_storage_get_proto"
}Results: {
"Output": "block/blk-iocost-bpf.c=119=bpf_iocost_get_func_proto(enum bpf_func_id func_id,\n--\nblock/blk-iocost-bpf.c-124-\tcase BPF_FUNC_cgrp_storage_get:\nblock/blk-iocost-bpf.c:125:\t\treturn \u0026bpf_cgrp_storage_get_proto;\nblock/blk-iocost-bpf.c-126-#endif\n--\ninclude/linux/bpf.h=3957=extern const struct bpf_func_proto bpf_user_ringbuf_drain_proto;\ninclude/linux/bpf.h:3958:extern const struct bpf_func_proto bpf_cgrp_storage_get_proto;\ninclude/linux/bpf.h-3959-extern const struct bpf_func_proto bpf_cgrp_storage_delete_proto;\n--\nkernel/bpf/bpf_cgrp_storage.c=160=const struct bpf_map_ops cgrp_storage_map_ops = {\n--\nkernel/bpf/bpf_cgrp_storage.c-174-\nkernel/bpf/bpf_cgrp_storage.c:175:const struct bpf_func_proto bpf_cgrp_storage_get_proto = {\nkernel/bpf/bpf_cgrp_storage.c-176-\t.func\t\t= bpf_cgrp_storage_get,\n--\nkernel/bpf/helpers.c=2067=bpf_base_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)\n--\nkernel/bpf/helpers.c-2169-\tcase BPF_FUNC_cgrp_storage_get:\nkernel/bpf/helpers.c:2170:\t\treturn \u0026bpf_cgrp_storage_get_proto;\nkernel/bpf/helpers.c-2171-\tcase BPF_FUNC_cgrp_storage_delete:\n"
} |
| 21/2 | 2026/09/14 08:05 | llm | patch-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 30938 output: 23 thoughts: 20 Thoughts: Here's my summary, as if I were thinking these thoughts: **Analyzing the BPF cgroup Storage Compilation** Okay, time to dig in. I need to understand the build environment for `bpf_cgrp_storage.c`. It's crucial for understanding how this code will behave at runtime, especially with all the nuances of BPF compilation. I need to determine what flags, what kernel version, and what specific compilers are being used. This information is going to be critical for debugging, porting, or even just properly assessing its security posture. I'll need to know whether any specialized BPF helpers are being enabled, or perhaps if there are certain optimizations or features being exploited during compilation. This is the first, essential step. Let's see what we're dealing with. |
| 22/2 | 2026/09/14 08:06 | tool | grepper |
0mArgs:{
"Expression": "bpf_cgrp_storage.o"
}Results: {
"Output": "kernel/bpf/Makefile=36=ifeq ($(CONFIG_CGROUPS),y)\nkernel/bpf/Makefile:37:obj-$(CONFIG_BPF_SYSCALL) += cgroup_iter.o bpf_cgrp_storage.o\nkernel/bpf/Makefile-38-endif\n"
} |
| 23/2 | 2026/09/14 08:06 | llm | patch-evaluator |
1mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 31233 output: 31 thoughts: 60 Thoughts: **Examining the Dependencies for cgroup Storage Functions** Alright, let's see, I need `CONFIG_CGROUPS` to be enabled if I'm going to use `BPF_FUNC_cgrp_storage_get`. And as if it wasn't obvious, it looks like `BLK_CGROUP`, which is related to block device cgroup functionality, is also dependent on `CGROUPS`. I think it's a good idea to confirm this relationship. The place to check should be `block/Kconfig` in the kernel source. |
| 24/2 | 2026/09/14 08:07 | tool | grepper |
0mArgs:{
"Expression": "config BLK_CGROUP",
"PathPrefix": "block/Kconfig"
}Results: {
"Output": "block/Kconfig=39=config BLK_RQ_ALLOC_TIME\n--\nblock/Kconfig-41-\nblock/Kconfig:42:config BLK_CGROUP_RWSTAT\nblock/Kconfig-43-\tbool\nblock/Kconfig-44-\nblock/Kconfig:45:config BLK_CGROUP_PUNT_BIO\nblock/Kconfig-46-\tbool\n--\nblock/Kconfig=127=config BLK_WBT_MQ\n--\nblock/Kconfig-133-\nblock/Kconfig:134:config BLK_CGROUP_IOLATENCY\nblock/Kconfig-135-\tbool \"Enable support for latency based cgroup IO protection\"\n--\nblock/Kconfig-144-\nblock/Kconfig:145:config BLK_CGROUP_FC_APPID\nblock/Kconfig-146-\tbool \"Enable support to track FC I/O Traffic across cgroup applications\"\n--\nblock/Kconfig-153-\nblock/Kconfig:154:config BLK_CGROUP_IOCOST\nblock/Kconfig-155-\tbool \"Enable support for cost model based cgroup IO controller\"\n--\nblock/Kconfig-163-\nblock/Kconfig:164:config BLK_CGROUP_IOPRIO\nblock/Kconfig-165-\tbool \"Cgroup I/O controller for assigning an I/O priority class\"\n--\nblock/Kconfig=232=source \"block/Kconfig.iosched\"\nblock/Kconfig-233-\nblock/Kconfig:234:config BLK_CGROUP_IOCOST_BPF\nblock/Kconfig-235-\tbool \"Enable BPF pluggable cost model support for the cost IO controller\"\n"
} |
| 25/2 | 2026/09/14 08:07 | llm | patch-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 31893 output: 22 thoughts: 18 Thoughts: Here's my thought process, as if I were considering this myself: **Tracing Down `config BLK_CGROUP`** Okay, so I need to figure out where `config BLK_CGROUP` is defined within the kernel configuration. It's a fundamental step for understanding its implications and potential interactions with other subsystems. I need to trace this back to the source. Let's start with a systematic search; I'll likely need to navigate the `Kconfig` files... |
| 26/2 | 2026/09/14 08:07 | tool | grepper |
0mArgs:{
"Expression": "config BLK_CGROUP$"
}Results: {
"Output": "init/Kconfig=1109=config MEMCG_V1\n--\ninit/Kconfig-1125-\ninit/Kconfig:1126:config BLK_CGROUP\ninit/Kconfig-1127-\tbool \"IO controller\"\n"
} |
| 27/2 | 2026/09/14 08:07 | llm | patch-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 32116 output: 33 thoughts: 18 Thoughts: Here's my take on the `init/Kconfig` thought process, from my perspective: **Diving into init/Kconfig** Okay, time to dig into the `init/Kconfig` file. Specifically, I'm going to jump to around line 1126. I'm guessing that's a likely spot to find a key configuration option related to the initialization process. Let's see what's being defined there and what parameters are at play. Hopefully I'll be able to quickly understand the relationships between different settings by skimming through the relevant surrounding lines. This is probably where I'll find the core logic I'm looking for... let's get to it! |
| 28/2 | 2026/09/14 08:07 | tool | read-file |
0mArgs:{
"File": "init/Kconfig",
"FirstLine": 1125,
"LineCount": 15
}Results: {
"Contents": "1125:\t\n1126:\tconfig BLK_CGROUP\n1127:\t\tbool \"IO controller\"\n1128:\t\tdepends on BLOCK\n1129:\t\tdefault n\n1130:\t\thelp\n1131:\t\tGeneric block IO controller cgroup interface. This is the common\n1132:\t\tcgroup interface which should be used by various IO controlling\n1133:\t\tpolicies.\n1134:\t\n1135:\t\tCurrently, CFQ IO scheduler uses it to recognize task groups and\n1136:\t\tcontrol disk bandwidth allocation (proportional time slice allocation)\n1137:\t\tto such task groups. It is also used by bio throttling logic in\n1138:\t\tblock layer to implement upper limit in IO rates on a device.\n1139:\t\n"
} |
| 29/2 | 2026/09/14 08:07 | llm | patch-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 32528 output: 38 thoughts: 153 Thoughts: **Dependency Analysis and Code Inspection** Okay, so I'm seeing a clear dependency chain here. `BLK_CGROUP` is the foundation, built upon by `BLOCK`, naturally. Then `BLK_CGROUP_IOCOST` relies on `BLK_CGROUP`, which makes sense for any I/O cost accounting within cgroups. Finally, `BLK_CGROUP_IOCOST_BPF` is the most involved, requiring both the base I/O cost feature (`BLK_CGROUP_IOCOST`) *and* a BPF-related trifecta: `BPF_SYSCALL`, `BPF_JIT`, and `DEBUG_INFO_BTF`. That's not surprising; leveraging BPF for I/O cost measurement means we're going to need the BPF infrastructure and debugging support. Now, let's dive into the details. I need to understand the specifics of this implementation. I'll start by thoroughly examining the commit's code changes. I'll focus particularly on the core files: `block/blk-iocost-bpf.c`, which is probably where the magic happens, `block/blk-iocost.c`, which likely defines the I/O cost accounting framework, and `block/blk-cgroup.c` for cgroup integration. First up, let's get into `block/blk-iocost-bpf.c` and understand how the BPF integration is implemented. |
| 30/2 | 2026/09/14 08:08 | tool | read-file |
0mArgs:{
"File": "block/blk-iocost-bpf.c",
"FirstLine": 1,
"LineCount": 100
}Results: {
"Contents": " 1:\t// SPDX-License-Identifier: GPL-2.0\n 2:\t/*\n 3:\t * blk-iocost: BPF struct_ops plumbing for pluggable cost models.\n 4:\t *\n 5:\t * Registers the \"iocost_model_ops\" struct_ops type and maintains the\n 6:\t * name registry of registered models. A registered model is bound to\n 7:\t * a device through io.cost.model; see include/linux/blk-iocost.h.\n 8:\t */\n 9:\t#include \u003clinux/init.h\u003e\n 10:\t#include \u003clinux/kernel.h\u003e\n 11:\t#include \u003clinux/module.h\u003e\n 12:\t#include \u003clinux/mutex.h\u003e\n 13:\t#include \u003clinux/slab.h\u003e\n 14:\t#include \u003clinux/bpf.h\u003e\n 15:\t#include \u003clinux/bpf_verifier.h\u003e\n 16:\t#include \u003clinux/btf.h\u003e\n 17:\t#include \u003clinux/blk-iocost.h\u003e\n 18:\t\n 19:\tstatic DEFINE_MUTEX(iocost_bpf_reg_lock);\n 20:\tstatic LIST_HEAD(iocost_bpf_models);\n 21:\tstatic LIST_HEAD(iocost_bpf_lifecycle);\n 22:\t\n 23:\t/*\n 24:\t * The registry holds a bpf_struct_ops_get() reference obtained in .reg;\n 25:\t * .unreg drops it, so the kdata of an unregistered model stays alive\n 26:\t * while any device is still bound to it.\n 27:\t */\n 28:\tstruct iocost_bpf_model {\n 29:\t\tstruct list_head\tlist;\t\t/* name registry */\n 30:\t\tstruct list_head\tlifecycle;\t/* lifecycle notify list */\n 31:\t\tconst struct iocost_model_ops *ops;\n 32:\t\trefcount_t\t\trefs;\n 33:\t};\n 34:\t\n 35:\t/*\n 36:\t * Look up a registered model by name and acquire a reference on it.\n 37:\t * The registry lock is held across lookup and bpf_struct_ops_get() so\n 38:\t * the model cannot be unregistered in between.\n 39:\t */\n 40:\tint iocost_bpf_model_get(const char *name,\n 41:\t\t\t\t const struct iocost_model_ops **opsp)\n 42:\t{\n 43:\t\tstruct iocost_bpf_model *m;\n 44:\t\tint ret = -ENOENT;\n 45:\t\n 46:\t\tmutex_lock(\u0026iocost_bpf_reg_lock);\n 47:\t\tlist_for_each_entry(m, \u0026iocost_bpf_models, list) {\n 48:\t\t\tif (!strcmp(m-\u003eops-\u003ename, name)) {\n 49:\t\t\t\tif (bpf_struct_ops_get(m-\u003eops)) {\n 50:\t\t\t\t\trefcount_inc(\u0026m-\u003erefs);\n 51:\t\t\t\t\t*opsp = m-\u003eops;\n 52:\t\t\t\t\tret = 0;\n 53:\t\t\t\t}\n 54:\t\t\t\tbreak;\n 55:\t\t\t}\n 56:\t\t}\n 57:\t\tmutex_unlock(\u0026iocost_bpf_reg_lock);\n 58:\t\treturn ret;\n 59:\t}\n 60:\t\n 61:\tstatic struct iocost_bpf_model *\n 62:\tiocost_bpf_model_lookup(const struct iocost_model_ops *ops)\n 63:\t{\n 64:\t\tstruct iocost_bpf_model *m;\n 65:\t\n 66:\t\tlist_for_each_entry(m, \u0026iocost_bpf_models, list) {\n 67:\t\t\tif (m-\u003eops == ops)\n 68:\t\t\t\treturn m;\n 69:\t\t}\n 70:\t\treturn NULL;\n 71:\t}\n 72:\t\n 73:\t/*\n 74:\t * Lifecycle notifications walk the lifecycle list, which keeps a node\n 75:\t * for as long as any device has the model bound, so an unregistered\n 76:\t * but still-bound model keeps receiving blkcg online/offline.\n 77:\t */\n 78:\tvoid iocost_notify_blkcg_online(struct blkcg *blkcg)\n 79:\t{\n 80:\t\tstruct iocost_bpf_model *m;\n 81:\t\n 82:\t\tguard(mutex)(\u0026iocost_bpf_reg_lock);\n 83:\t\tlist_for_each_entry(m, \u0026iocost_bpf_lifecycle, lifecycle) {\n 84:\t\t\tif (m-\u003eops-\u003eblkcg_online)\n 85:\t\t\t\tm-\u003eops-\u003eblkcg_online(blkcg);\n 86:\t\t}\n 87:\t}\n 88:\t\n 89:\tvoid iocost_notify_blkcg_offline(struct blkcg *blkcg)\n 90:\t{\n 91:\t\tstruct iocost_bpf_model *m;\n 92:\t\n 93:\t\tguard(mutex)(\u0026iocost_bpf_reg_lock);\n 94:\t\tlist_for_each_entry(m, \u0026iocost_bpf_lifecycle, lifecycle) {\n 95:\t\t\tif (m-\u003eops-\u003eblkcg_offline)\n 96:\t\t\t\tm-\u003eops-\u003eblkcg_offline(blkcg);\n 97:\t\t}\n 98:\t}\n 99:\t\n 100:\tstatic int bpf_iocost_model_init(struct btf *btf)\n"
} |
| 31/2 | 2026/09/14 08:08 | llm | patch-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 34502 output: 40 thoughts: 20 Thoughts: Okay, let's dive into the rest of `block/blk-iocost-bpf.c`. I need to get a clear picture of how this I/O cost accounting is being implemented using BPF. I'm going to be looking for the key data structures, the crucial BPF programs, and how they interact. Specifically, I'll want to understand how the cost metrics are being sampled, aggregated, and potentially exported. Given my familiarity with block I/O internals, I expect to see integration with request queues and potentially mechanisms to trace the lifecycle of I/O requests. Let's see if it's utilizing tracepoints, kprobes, or some other BPF hook to capture the necessary data. I'm hoping it's efficient, as I know how critical performance can be in the block layer, so I'll be keeping an eye out for any potential bottlenecks or inefficiencies. My goal is a full comprehension of the entire cost-accounting methodology. |
| 32/2 | 2026/09/14 08:08 | tool | read-file |
0mArgs:{
"File": "block/blk-iocost-bpf.c",
"FirstLine": 101,
"LineCount": 100
}Results: {
"Contents": " 101:\t{\n 102:\t\ts32 type_id;\n 103:\t\n 104:\t\ttype_id = btf_find_by_name_kind(btf, \"iocost_model_ops\", BTF_KIND_STRUCT);\n 105:\t\tif (type_id \u003c 0)\n 106:\t\t\treturn -EINVAL;\n 107:\t\treturn 0;\n 108:\t}\n 109:\t\n 110:\tstatic bool bpf_iocost_is_valid_access(int off, int size,\n 111:\t\t\t\t\t enum bpf_access_type type,\n 112:\t\t\t\t\t const struct bpf_prog *prog,\n 113:\t\t\t\t\t struct bpf_insn_access_aux *info)\n 114:\t{\n 115:\t\treturn bpf_tracing_btf_ctx_access(off, size, type, prog, info);\n 116:\t}\n 117:\t\n 118:\tstatic const struct bpf_func_proto *\n 119:\tbpf_iocost_get_func_proto(enum bpf_func_id func_id,\n 120:\t\t\t\t const struct bpf_prog *prog)\n 121:\t{\n 122:\t\tswitch (func_id) {\n 123:\t#ifdef CONFIG_CGROUPS\n 124:\t\tcase BPF_FUNC_cgrp_storage_get:\n 125:\t\t\treturn \u0026bpf_cgrp_storage_get_proto;\n 126:\t#endif\n 127:\t\tdefault:\n 128:\t\t\treturn bpf_base_func_proto(func_id, prog);\n 129:\t\t}\n 130:\t}\n 131:\t\n 132:\tstatic int bpf_iocost_check_member(const struct btf_type *t,\n 133:\t\t\t\t\t const struct btf_member *member,\n 134:\t\t\t\t\t const struct bpf_prog *prog)\n 135:\t{\n 136:\t\t/* calc_cost() is called with RCU read lock held */\n 137:\t\tif (prog-\u003esleepable)\n 138:\t\t\treturn -EINVAL;\n 139:\t\treturn 0;\n 140:\t}\n 141:\t\n 142:\tstatic int bpf_iocost_init_member(const struct btf_type *t,\n 143:\t\t\t\t\t const struct btf_member *member,\n 144:\t\t\t\t\t void *kdata, const void *udata)\n 145:\t{\n 146:\t\tstruct iocost_model_ops *ops = kdata;\n 147:\t\tconst struct iocost_model_ops *uops = udata;\n 148:\t\tu32 moff = __btf_member_bit_offset(t, member) / 8;\n 149:\t\n 150:\t\tswitch (moff) {\n 151:\t\tcase offsetof(struct iocost_model_ops, name):\n 152:\t\t\tif (bpf_obj_name_cpy(ops-\u003ename, uops-\u003ename,\n 153:\t\t\t\t\t sizeof(ops-\u003ename)) \u003c= 0)\n 154:\t\t\t\treturn -EINVAL;\n 155:\t\t\treturn 1;\n 156:\t\t}\n 157:\t\n 158:\t\treturn 0;\n 159:\t}\n 160:\t\n 161:\tstatic u64 bpf_iocost_calc_cost_stub(u64 opf, u64 nbytes, u64 sector,\n 162:\t\t\t\t\t struct blkcg *blkcg, u64 model_flags);\n 163:\t\n 164:\t/*\n 165:\t * kdata is seeded from the CFI stubs, so calc_cost is never NULL; a\n 166:\t * model which did not implement it inherits the stub, which prices\n 167:\t * every IO at 0. Compare against the stub to reject it.\n 168:\t */\n 169:\tstatic int bpf_iocost_validate(void *kdata)\n 170:\t{\n 171:\t\tstruct iocost_model_ops *ops = kdata;\n 172:\t\n 173:\t\tif (ops-\u003ecalc_cost == bpf_iocost_calc_cost_stub)\n 174:\t\t\treturn -EINVAL;\n 175:\t\treturn 0;\n 176:\t}\n 177:\t\n 178:\tstatic int bpf_iocost_reg(void *kdata, struct bpf_link *link)\n 179:\t{\n 180:\t\tstruct iocost_model_ops *ops = kdata;\n 181:\t\tstruct iocost_bpf_model *m;\n 182:\t\tint ret = 0;\n 183:\t\n 184:\t\tif (!bpf_struct_ops_get(ops))\n 185:\t\t\treturn -ENOENT;\n 186:\t\n 187:\t\tm = kzalloc(sizeof(*m), GFP_KERNEL);\n 188:\t\tif (!m) {\n 189:\t\t\tbpf_struct_ops_put(ops);\n 190:\t\t\treturn -ENOMEM;\n 191:\t\t}\n 192:\t\trefcount_set(\u0026m-\u003erefs, 1);\n 193:\t\n 194:\t\tmutex_lock(\u0026iocost_bpf_reg_lock);\n 195:\t\t{\n 196:\t\t\tstruct iocost_bpf_model *other;\n 197:\t\n 198:\t\t\tlist_for_each_entry(other, \u0026iocost_bpf_models, list) {\n 199:\t\t\t\tif (!strcmp(other-\u003eops-\u003ename, ops-\u003ename)) {\n 200:\t\t\t\t\tret = -EEXIST;\n"
} |
| 33/2 | 2026/09/14 08:08 | llm | patch-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 36315 output: 40 thoughts: 27 Thoughts: Here's my take on the specified section of `block/blk-iocost-bpf.c`: **Diving into the IOCost BPF Code** Alright, let's zero in on lines 201 to 307 of `block/blk-iocost-bpf.c`. I know this area deals with the BPF (Berkeley Packet Filter) side of the I/O cost accounting framework. My focus here is likely around data collection and potentially initial processing related to block I/O performance. I anticipate seeing BPF program hooks and related structure definitions that the system attaches to the block layer I/O path. I'll be looking for where metrics like I/O latency, queue depth, and service time are captured, perhaps leveraging kprobes or tracepoints. Also, how the collected data is initially structured and potentially how it's handed off. I'm also interested in how this interacts with the overall `iocost` framework. Does it filter and pass certain key events? Let's get reading! |
| 34/2 | 2026/09/14 08:09 | tool | read-file |
0mArgs:{
"File": "block/blk-iocost-bpf.c",
"FirstLine": 201,
"LineCount": 107
}Results: {
"Contents": " 201:\t\t\t\t\tbreak;\n 202:\t\t\t\t}\n 203:\t\t\t}\n 204:\t\t}\n 205:\t\tif (!ret) {\n 206:\t\t\tm-\u003eops = ops;\n 207:\t\t\tlist_add(\u0026m-\u003elist, \u0026iocost_bpf_models);\n 208:\t\t\tlist_add(\u0026m-\u003elifecycle, \u0026iocost_bpf_lifecycle);\n 209:\t\t}\n 210:\t\tmutex_unlock(\u0026iocost_bpf_reg_lock);\n 211:\t\n 212:\t\tif (ret) {\n 213:\t\t\tbpf_struct_ops_put(ops);\n 214:\t\t\tkfree(m);\n 215:\t\t}\n 216:\t\treturn ret;\n 217:\t}\n 218:\t\n 219:\t/*\n 220:\t * Unregistering drops the registration reference. When the last\n 221:\t * reference is gone (no device bound), the node leaves the lifecycle\n 222:\t * list and is freed; otherwise bound devices keep it alive and it\n 223:\t * keeps receiving blkcg online/offline notifications.\n 224:\t */\n 225:\tstatic void bpf_iocost_unreg(void *kdata, struct bpf_link *link)\n 226:\t{\n 227:\t\tstruct iocost_model_ops *ops = kdata;\n 228:\t\tstruct iocost_bpf_model *m;\n 229:\t\n 230:\t\tmutex_lock(\u0026iocost_bpf_reg_lock);\n 231:\t\tm = iocost_bpf_model_lookup(ops);\n 232:\t\tif (m) {\n 233:\t\t\tlist_del(\u0026m-\u003elist);\n 234:\t\t\tif (refcount_dec_and_test(\u0026m-\u003erefs)) {\n 235:\t\t\t\tlist_del(\u0026m-\u003elifecycle);\n 236:\t\t\t\tkfree(m);\n 237:\t\t\t}\n 238:\t\t}\n 239:\t\tmutex_unlock(\u0026iocost_bpf_reg_lock);\n 240:\t\n 241:\t\tbpf_struct_ops_put(ops);\n 242:\t}\n 243:\t\n 244:\tvoid iocost_bpf_model_put(const struct iocost_model_ops *ops)\n 245:\t{\n 246:\t\tstruct iocost_bpf_model *m;\n 247:\t\n 248:\t\tmutex_lock(\u0026iocost_bpf_reg_lock);\n 249:\t\tlist_for_each_entry(m, \u0026iocost_bpf_lifecycle, lifecycle) {\n 250:\t\t\tif (m-\u003eops == ops)\n 251:\t\t\t\tbreak;\n 252:\t\t}\n 253:\t\t/*\n 254:\t\t * Exactly one bpf_struct_ops_put() per call, pairing the\n 255:\t\t * bpf_struct_ops_get() in iocost_bpf_model_get(); the node is\n 256:\t\t * freed when the last reference goes, whichever side drops it.\n 257:\t\t */\n 258:\t\tif (\u0026m-\u003elifecycle != \u0026iocost_bpf_lifecycle \u0026\u0026\n 259:\t\t refcount_dec_and_test(\u0026m-\u003erefs)) {\n 260:\t\t\tlist_del(\u0026m-\u003elifecycle);\n 261:\t\t\tmutex_unlock(\u0026iocost_bpf_reg_lock);\n 262:\t\t\tkfree(m);\n 263:\t\t\tbpf_struct_ops_put(ops);\n 264:\t\t\treturn;\n 265:\t\t}\n 266:\t\tmutex_unlock(\u0026iocost_bpf_reg_lock);\n 267:\t\tbpf_struct_ops_put(ops);\n 268:\t}\n 269:\t\n 270:\tstatic const struct bpf_verifier_ops bpf_iocost_verifier_ops = {\n 271:\t\t.get_func_proto = bpf_iocost_get_func_proto,\n 272:\t\t.is_valid_access = bpf_iocost_is_valid_access,\n 273:\t};\n 274:\t\n 275:\tstatic u64 bpf_iocost_calc_cost_stub(u64 opf, u64 nbytes, u64 sector,\n 276:\t\t\t\t\t struct blkcg *blkcg, u64 flags)\n 277:\t{\n 278:\t\treturn 0;\n 279:\t}\n 280:\t\n 281:\tstatic void bpf_iocost_blkcg_online_stub(struct blkcg *blkcg) { }\n 282:\tstatic void bpf_iocost_blkcg_offline_stub(struct blkcg *blkcg) { }\n 283:\t\n 284:\tstatic struct iocost_model_ops __bpf_ops_iocost_model_ops = {\n 285:\t\t.calc_cost = bpf_iocost_calc_cost_stub,\n 286:\t\t.blkcg_online = bpf_iocost_blkcg_online_stub,\n 287:\t\t.blkcg_offline = bpf_iocost_blkcg_offline_stub,\n 288:\t};\n 289:\t\n 290:\tstatic struct bpf_struct_ops bpf_iocost_model_ops = {\n 291:\t\t.verifier_ops = \u0026bpf_iocost_verifier_ops,\n 292:\t\t.init = bpf_iocost_model_init,\n 293:\t\t.check_member = bpf_iocost_check_member,\n 294:\t\t.init_member = bpf_iocost_init_member,\n 295:\t\t.validate = bpf_iocost_validate,\n 296:\t\t.reg = bpf_iocost_reg,\n 297:\t\t.unreg = bpf_iocost_unreg,\n 298:\t\t.name = \"iocost_model_ops\",\n 299:\t\t.cfi_stubs = \u0026__bpf_ops_iocost_model_ops,\n 300:\t\t.owner = THIS_MODULE,\n"
} |
| 35/2 | 2026/09/14 08:09 | llm | patch-evaluator |
3mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 38240 output: 31 thoughts: 3223 Thoughts: **Fuzzing Analysis of `block/blk-iocost.c` and Related Files** Okay, let's break down this code. My initial thought is to check for potential race conditions, incorrect refcounting, and unexpected behavior when interacting with the BPF-based I/O cost model. The primary focus is on `iocost_bpf_model_put` and the associated refcounting mechanisms, specifically in relation to `bpf_iocost_unreg`. I see several areas that look interesting from a fuzzing perspective. First, I observe that the `ioc_timer_fn` function is calling `trace_iocost_ioc_tick`, which suggests an instrumentation point. Good for debugging, but not a fuzzing concern. The cost calculation functions (`calc_vtime_cost`, `calc_size_vtime_cost`) are straightforward but should be fuzzed as part of overall I/O flow. The key area starts with the interactions around the BPF model registration and unregistration. The `ioc_rqos_exit` function shows how the model is unregistered. I noticed the refcounting in `iocost_bpf_model_put`, specifically, the double `bpf_struct_ops_put` calls in some scenarios. I am concerned that there is a potential race condition and that a reference count issue could lead to Use-After-Free or double free vulnerability. The refcount logic and the `iocost_bpf_reg_lock` mutex are the critical elements to focus on. If `bpf_iocost_unreg` runs at the same time and the model is not deleted from `iocost_bpf_lifecycle`, there might be a problem. This is especially true if multiple devices unbind simultaneously. Next, I found some potential race in `ioc_cost_model_write`. The code drops the `ioc->lock` between `ioc_bpf_model_prepare` and re-taking it, which could lead to an inconsistent state if two threads are updating the cost model concurrently. I'm wondering if one thread could set coefficients and then be clobbered by a concurrent write, resulting in stale data being applied. The fact that the code handles an empty model string by setting it to NULL also looks like it needs some scrutiny: It would be interesting to see how the system behaves if the "model" parameter is left out. The situation can be caused by updating other values and inadvertently clearing the BPF model. Then, there are those `rcu_read_lock` and `rcu_read_unlock` calls. Is `rq->bio` guaranteed to stay alive? Is `rq->bio->bi_blkg` NULL-safe? It can lead to a NULL pointer dereference. My initial assessment is that these look like they could be reachable through syzkaller, which means we have a viable fuzzing target. Enabling syzkaller and testing the different edge cases would be a good step here. I will want to focus on the following: 1. **Concurrency Testing:** Simultaneous writes to `io.cost.model` from multiple threads/processes, especially when the file contents are being updated. 2. **Model Transitions:** Transitions between different model types (linear, BPF, auto) and empty model configurations. This includes testing the behavior when setting other configuration parameters such as `rbps` *without* specifying the `model` parameter, as it might unexpectedly reset the model to `NULL`. 3. **Refcount and Lifecycle:** Focus on scenarios where BPF programs are registered and unregistered concurrently, or when cgroups are created and destroyed frequently while the BPF models are active. 4. **Error Handling:** Testing for error cases during BPF program load, unload, and execution. 5. **NULL Pointer Dereference:** Testing edge cases where `rq->bio` can become NULL when calculating the cost. 6. **Edge-Case Values:** Extremely large/small values, negative values, and zero values for parameters like `rbps` and `latency_target_us`. In terms of enabling this code path in the kernel, I'll need to check the dependencies. The key configuration options will be `BLK_CGROUP_IOCOST_BPF`, `BLK_CGROUP_IOCOST`, `BLK_CGROUP`, `BPF_SYSCALL`, `BPF_JIT`, and `DEBUG_INFO_BTF`. All of those have to be enabled to allow this path in the kernel. |
| 36/2 | 2026/09/14 08:13 | tool | grepper |
0mError:no matches Args: {
"Expression": "config BPF_SYSCALL",
"PathPrefix": "init/Kconfig"
}Results: {
"error": "no matches"
} |
| 37/2 | 2026/09/14 08:13 | llm | patch-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 42467 output: 23 thoughts: 18 Thoughts: Here's my attempt at summarizing the thought process, tailored for an expert audience: **Hunting Down BPF_SYSCALL in Kconfig** Okay, so I need to track down the configuration option `BPF_SYSCALL`. My immediate thought is, "Where is this defined within the kernel's Kconfig system?" This could be spread across multiple files depending on the subsystem and the overall architecture of the feature. I need a methodical approach to this. My initial action will be to perform a recursive search across the entire kernel source tree's Kconfig files. This is straightforward; a simple grep or `find` command combined with `grep` should quickly yield results, hopefully pinpointing the exact file(s) that define `config BPF_SYSCALL` and, more importantly, *where* it's used. Once I've got that initial list, I'll need to analyze the surrounding context. What other configuration options are related? What dependencies does `BPF_SYSCALL` have? Does it depend on specific architecture features, kernel versions, or other subsystem configurations? Understanding this relationship is critical to fully grasp its impact. This goes beyond a simple search; I need to look at the `depends on`, `select`, and `default` statements that condition its inclusion. My goal isn't just to *find* it; it's to understand *how* it fits into the broader kernel configuration landscape. This will provide insight on where this specific sysctl can be enabled, and what dependencies it requires. |
| 38/2 | 2026/09/14 08:13 | tool | grepper |
0mArgs:{
"Expression": "config BPF_SYSCALL\\b"
}Results: {
"Output": "kernel/bpf/Kconfig=29=menu \"BPF subsystem\"\nkernel/bpf/Kconfig-30-\nkernel/bpf/Kconfig:31:config BPF_SYSCALL\nkernel/bpf/Kconfig-32-\tbool \"Enable bpf() system call\"\n"
} |
| 39/2 | 2026/09/14 08:13 | llm | patch-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 42913 output: 34 thoughts: 19 Thoughts: Here's my take on the thought process: **Diving into the BPF Kernel Configuration** Okay, so I need to understand something about the BPF configuration within the kernel. My instinct tells me to start digging into the `kernel/bpf/Kconfig` file. I'm aiming for around line 31. That's usually where the meat of the initial setup, or maybe some key options, are defined. Time to see what's what and get a handle on the landscape there. Let's see what goodies await. |
| 40/2 | 2026/09/14 08:13 | tool | read-file |
0mArgs:{
"File": "kernel/bpf/Kconfig",
"FirstLine": 25,
"LineCount": 25
}Results: {
"Contents": " 25:\t# default for kernels that were compiled with BPF JIT support.\n 26:\tconfig ARCH_WANT_DEFAULT_BPF_JIT\n 27:\t\tbool\n 28:\t\n 29:\tmenu \"BPF subsystem\"\n 30:\t\n 31:\tconfig BPF_SYSCALL\n 32:\t\tbool \"Enable bpf() system call\"\n 33:\t\tselect BPF\n 34:\t\tselect IRQ_WORK\n 35:\t\tselect NEED_TASKS_RCU\n 36:\t\tselect TASKS_TRACE_RCU\n 37:\t\tselect BINARY_PRINTF\n 38:\t\tselect NET_SOCK_MSG if NET\n 39:\t\tselect NET_XGRESS if NET\n 40:\t\tselect PAGE_POOL if NET\n 41:\t\tdefault n\n 42:\t\thelp\n 43:\t\t Enable the bpf() system call that allows to manipulate BPF programs\n 44:\t\t and maps via file descriptors.\n 45:\t\n 46:\tconfig BPF_JIT\n 47:\t\tbool \"Enable BPF Just In Time compiler\"\n 48:\t\tdepends on BPF\n 49:\t\tdepends on HAVE_CBPF_JIT || HAVE_EBPF_JIT\n"
} |
| 41/2 | 2026/09/14 08:13 | llm | patch-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 43410 output: 23 thoughts: 12 Thoughts: Okay, here's my interpretation of that thought process, tailored for an expert audience, and written in the first person: **Analyzing the Debug Information** Alright, time to dive in. My immediate thought is to zero in on the `DEBUG_INFO_BTF` section. This isn't just a casual observation; it's a strategic entry point. With my understanding of kernel internals, I know this is where the *really* interesting stuff often resides – the debugging information represented in BTF format. I'm thinking about the possibilities: the type information, the line number data, the function metadata... this is where I get a fine-grained view of the kernel's inner workings, its data structures, and the relationships between various components. I'm expecting this information to be crucial for reverse engineering, performance analysis, or potentially debugging tricky kernel issues. My objective is to extract any valuable insights, so I can continue with the next stage of my work. |
| 42/2 | 2026/09/14 08:13 | tool | grepper |
0mArgs:{
"Expression": "config DEBUG_INFO_BTF\\b"
}Results: {
"Output": "lib/Kconfig.debug=378=config DEBUG_INFO_SPLIT\n--\nlib/Kconfig.debug-397-\nlib/Kconfig.debug:398:config DEBUG_INFO_BTF\nlib/Kconfig.debug-399-\tbool \"Generate BTF type information\"\n"
} |
| 43/2 | 2026/09/14 08:13 | llm | patch-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 43731 output: 34 thoughts: 19 Thoughts: Here's my summary, as if I were thinking these thoughts: **Examining Debug Configuration in the Kernel** Okay, let's dive into this. Based on what I'm looking at, I need to get a deeper understanding of how the debug configurations are handled. Specifically, I'm going to pull up `lib/Kconfig.debug`. I know that file contains a lot of critical configuration options for debugging kernel code and related debugging features, and I have a feeling the information I need is likely around line 398. That's usually where things like tracepoints, dynamic debug, or specific debugging levels for core subsystems are defined, given the structure of the Kconfig files. I'm going to check what settings are there. |
| 44/2 | 2026/09/14 08:14 | tool | read-file |
0mArgs:{
"File": "lib/Kconfig.debug",
"FirstLine": 395,
"LineCount": 20
}Results: {
"Contents": " 395:\t\t to know about the .dwo files and include them.\n 396:\t\t Incompatible with older versions of ccache.\n 397:\t\n 398:\tconfig DEBUG_INFO_BTF\n 399:\t\tbool \"Generate BTF type information\"\n 400:\t\tdepends on !DEBUG_INFO_SPLIT \u0026\u0026 !DEBUG_INFO_REDUCED\n 401:\t\tdepends on !GCC_PLUGIN_RANDSTRUCT || COMPILE_TEST\n 402:\t\tdepends on BPF_SYSCALL\n 403:\t\tdepends on PAHOLE_VERSION \u003e= 122\n 404:\t\t# pahole uses elfutils, which does not have support for Hexagon relocations\n 405:\t\tdepends on !HEXAGON\n 406:\t\thelp\n 407:\t\t Generate deduplicated BTF type information from DWARF debug info.\n 408:\t\t Turning this on requires pahole v1.22 or later, which will convert\n 409:\t\t DWARF type info into equivalent deduplicated BTF type info.\n 410:\t\n 411:\tconfig PAHOLE_HAS_BTF_TAG\n 412:\t\tdef_bool PAHOLE_VERSION \u003e= 123\n 413:\t\tdepends on CC_IS_CLANG\n 414:\t\thelp\n"
} |
| 45/2 | 2026/09/14 08:14 | llm | patch-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 44284 output: 34 thoughts: 844 Thoughts: **Focusing the Symbols** Okay, so I need to nail down the `FocusSymbols` for this. First, the required configs are straightforward: `BLK_CGROUP_IOCOST_BPF`, `BLK_CGROUP_IOCOST`, `BLK_CGROUP`, `BPF_SYSCALL`, `BPF_JIT`, and `DEBUG_INFO_BTF`. That’s the easy part. Now, let's get into the *real* work. The guidelines are pretty clear: avoid anything that's a general, ubiquitous hot path like memory allocators or trap handlers. We need to target the *specific* feature logic being added or modified, or the entrypoints for this subsystem. And then there's the static inline issue. Since these don't have distinct symbols, look for callers that exercise them. Alright, time to go function by function. In `block/blk-iocost-bpf.c`, I've got: `iocost_bpf_model_get`, `iocost_bpf_model_lookup`, `iocost_notify_blkcg_online`, `iocost_notify_blkcg_offline`, `bpf_iocost_model_init`, `bpf_iocost_is_valid_access`, `bpf_iocost_get_func_proto`, `bpf_iocost_check_member`, `bpf_iocost_init_member`, `bpf_iocost_validate`, `bpf_iocost_reg`, `bpf_iocost_unreg`, `iocost_bpf_model_put`, `bpf_iocost_calc_cost_stub`, `bpf_iocost_blkcg_online_stub`, `bpf_iocost_blkcg_offline_stub`, and `bpf_iocost_init`. Next, `block/blk-iocost.c` gives us: `ioc_timer_fn`, `calc_vtime_cost`, `calc_size_vtime_cost`, `ioc_rqos_exit`, `ioc_cost_model_prfill`, `ioc_bpf_model_prepare`, `ioc_bpf_model_commit`, and `ioc_cost_model_write`. And finally, in `block/blk-cgroup.c` I have `blkcg_css_offline` and `blkcg_css_online`. Okay, what about the static functions? Can I use those? Wait, yeah, I can, as long as they aren't inlined or ubiquitous. So the non-static functions are definitely symbols, and some key static functions will do. Let's see if those `calc_vtime_cost`, `calc_size_vtime_cost`, and `ioc_cost_model_write` functions are actually inlined or have symbols. I should check how they're called in `block/blk-iocost.c` too. That seems like a good place to start. |
| 46/2 | 2026/09/14 08:14 | tool | grepper |
0mArgs:{
"Expression": "calc_vtime_cost",
"PathPrefix": "block/blk-iocost.c"
}Results: {
"Output": "block/blk-iocost.c=2505=static u64 adjust_inuse_and_calc_cost(struct ioc_gq *iocg, u64 vtime,\n--\nblock/blk-iocost.c-2565-\nblock/blk-iocost.c:2566:static void calc_vtime_cost_builtin(struct bio *bio, struct ioc_gq *iocg,\nblock/blk-iocost.c-2567-\t\t\t\t bool is_merge, u64 *costp)\n--\nblock/blk-iocost.c-2610-\nblock/blk-iocost.c:2611:static u64 calc_vtime_cost(struct bio *bio, struct ioc_gq *iocg, bool is_merge)\nblock/blk-iocost.c-2612-{\n--\nblock/blk-iocost.c-2631-\nblock/blk-iocost.c:2632:\t\tcalc_vtime_cost_builtin(bio, iocg, is_merge, \u0026cost);\nblock/blk-iocost.c-2633-\t\treturn cost;\n--\nblock/blk-iocost.c=2762=static void ioc_rqos_throttle(struct rq_qos *rqos, struct bio *bio)\n--\nblock/blk-iocost.c-2778-\t/* calculate the absolute vtime cost */\nblock/blk-iocost.c:2779:\tabs_cost = calc_vtime_cost(bio, iocg, false);\nblock/blk-iocost.c-2780-\tif (!abs_cost)\n--\nblock/blk-iocost.c=2850=static void ioc_rqos_merge(struct rq_qos *rqos, struct request *rq,\n--\nblock/blk-iocost.c-2863-\nblock/blk-iocost.c:2864:\tabs_cost = calc_vtime_cost(bio, iocg, true);\nblock/blk-iocost.c-2865-\tif (!abs_cost)\n"
} |
| 47/2 | 2026/09/14 08:14 | llm | patch-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 46345 output: 36 thoughts: 64 Thoughts: **Analyzing I/O QoS Throttling and Cost Calculation** Okay, so I'm diving back into this I/O QoS stuff. I need to refresh my memory on the critical components and how they interact. My focus is on the `ioc_rqos_*` functions. Specifically, I'm thinking about `ioc_rqos_throttle`, `ioc_rqos_merge`, and `ioc_rqos_done`. I need to ensure I fully grasp the role of each within the larger framework. I know that `calc_size_vtime_cost` is called within the `ioc_rqos_done` function. I will check the interactions between these. My next step is to pinpoint everywhere `calc_size_vtime_cost` is called. I need to map out every location. This will give me a comprehensive view of how the cost calculation is being used and integrated into the overall I/O QoS process. This might reveal some optimization opportunities or areas where potential bottlenecks exist. |
| 48/2 | 2026/09/14 08:14 | tool | grepper |
0mArgs:{
"Expression": "calc_size_vtime_cost",
"PathPrefix": "block/blk-iocost.c"
}Results: {
"Output": "block/blk-iocost.c=2611=static u64 calc_vtime_cost(struct bio *bio, struct ioc_gq *iocg, bool is_merge)\n--\nblock/blk-iocost.c-2636-\nblock/blk-iocost.c:2637:static void calc_size_vtime_cost_builtin(struct request *rq, struct ioc *ioc,\nblock/blk-iocost.c-2638-\t\t\t\t\t u64 *costp)\n--\nblock/blk-iocost.c-2653-\nblock/blk-iocost.c:2654:static u64 calc_size_vtime_cost(struct request *rq, struct ioc *ioc)\nblock/blk-iocost.c-2655-{\n--\nblock/blk-iocost.c-2674-\nblock/blk-iocost.c:2675:\t\tcalc_size_vtime_cost_builtin(rq, ioc, \u0026cost);\nblock/blk-iocost.c-2676-\t\treturn cost;\n--\nblock/blk-iocost.c=2917=static void ioc_rqos_done(struct rq_qos *rqos, struct request *rq)\n--\nblock/blk-iocost.c-2941-\trq_wait_ns = rq-\u003estart_time_ns - rq-\u003ealloc_time_ns;\nblock/blk-iocost.c:2942:\tsize_nsec = div64_u64(calc_size_vtime_cost(rq, ioc), VTIME_PER_NSEC);\nblock/blk-iocost.c-2943-\n"
} |
| 49/2 | 2026/09/14 08:14 | llm | patch-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 47082 output: 35 thoughts: 24 Thoughts: Here's my take on those thoughts, tailored for an expert audience: **Diving into blk-iocost.c: Let's Inspect `ioc_cost_model_write`** Alright, time to dig in. I'm going to head straight for `block/blk-iocost.c`. My focus? The `ioc_cost_model_write` function. I need to understand exactly how the write I/O cost model is being implemented. What are the key calculations? What parameters are driving the cost determination for writes? I want to see how it interacts with the underlying block layer and resource accounting. Are there any potential bottlenecks or areas for optimization that jump out immediately? I'm particularly interested in how they're handling different storage media (e.g., SSD, HDD, NVMe) within the model and whether or not there are any notable differences. Time to pull up the code and get started. |
| 50/2 | 2026/09/14 08:14 | tool | grepper |
0mArgs:{
"Expression": "ioc_cost_model_write",
"PathPrefix": "block/blk-iocost.c"
}Results: {
"Output": "block/blk-iocost.c=3618=static const match_table_t i_lcoef_tokens = {\n--\nblock/blk-iocost.c-3627-\nblock/blk-iocost.c:3628:static ssize_t ioc_cost_model_write(struct kernfs_open_file *of, char *input,\nblock/blk-iocost.c-3629-\t\t\t\t size_t nbytes, loff_t off)\n--\nblock/blk-iocost.c=3755=static struct cftype ioc_files[] = {\n--\nblock/blk-iocost.c-3771-\t\t.seq_show = ioc_cost_model_show,\nblock/blk-iocost.c:3772:\t\t.write = ioc_cost_model_write,\nblock/blk-iocost.c-3773-\t},\n"
} |
| 51/2 | 2026/09/14 08:14 | llm | patch-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 47573 output: 33 thoughts: 1032 Thoughts: **My Analysis of blk-iocost BPF Feature Implementation** Okay, let's break this down. My goal is to identify the critical functions and configurations related to the new BPF-based cost model in `blk-iocost`, focusing specifically on the entry points and the logic I'm adding. I need to avoid listing generic, ubiquitous functions, and instead target feature-specific logic. I'll also need to consider static inline functions, and how they relate to the implementation. First, I see that `ioc_cost_model_write` handles the write operation for `io.cost.model`. That's a key entry point for user configuration. In `block/blk-iocost-bpf.c`, I have functions like `iocost_bpf_model_get`, `iocost_bpf_model_put`, `iocost_notify_blkcg_online`, `iocost_notify_blkcg_offline`, `bpf_iocost_reg`, `bpf_iocost_unreg`, `bpf_iocost_validate`, `bpf_iocost_init_member`, `bpf_iocost_check_member`, which are all clearly specific to the BPF integration. Then, within `block/blk-iocost.c`, I have `ioc_cost_model_write` (again, user input!), `calc_vtime_cost`, `calc_size_vtime_cost`, `ioc_bpf_model_prepare`, and `ioc_bpf_model_commit`. Now, let's assess the function list against the instructions. None of these functions seem ubiquitous – they're all directly involved in this BPF cost model feature. Specifically: * `iocost_bpf_model_get` and `iocost_bpf_model_put` are about model binding/unbinding during model binding/unbinding and queue teardown. * `iocost_notify_blkcg_online` and `iocost_notify_blkcg_offline` are triggered when blkcg are added and removed. * `bpf_iocost_reg` and `bpf_iocost_unreg` are used when the BPF struct\_ops map/link is registered or unregistered. * `bpf_iocost_validate` confirms the struct\_ops when loading. * `ioc_cost_model_write` takes user input from the `io.cost.model` cgroup file. * `calc_vtime_cost` and `calc_size_vtime_cost` are critical: they are the entry points for cost calculation within iocost, and now dispatch to the BPF model. So it looks like I've covered the main entrypoints and important functions. Next, the configurations. I see `BLK_CGROUP_IOCOST_BPF`, `BLK_CGROUP_IOCOST`, `BLK_CGROUP`, `BPF_SYSCALL`, `BPF_JIT`, and `DEBUG_INFO_BTF` in play. I need to make sure I get the config dependencies right. Let's see how `block/Kconfig` defines them. * `BLK_CGROUP_IOCOST_BPF` depends on `BLK_CGROUP_IOCOST`, `BPF_SYSCALL`, `BPF_JIT`, and `DEBUG_INFO_BTF`. * `BLK_CGROUP_IOCOST` depends on `BLK_CGROUP`. * `BLK_CGROUP` depends on `BLOCK`. That's standard. * `BPF_SYSCALL` selects `BPF`. * `BPF_JIT` depends on `BPF`. * `DEBUG_INFO_BTF` depends on `BPF_SYSCALL`. Good. Everything seems connected, though I should make sure I check that the `CGROUPS` is correctly pulled in by `BLK_CGROUP`. It looks like I need to double-check `BLK_CGROUP` in `init/Kconfig` again to make sure all the dependencies are correct. This looks solid, now. |
| 52/2 | 2026/09/14 08:15 | tool | grepper |
0mArgs:{
"Expression": "config BLK_CGROUP\\b",
"PathPrefix": "init/Kconfig"
}Results: {
"Output": "init/Kconfig=1109=config MEMCG_V1\n--\ninit/Kconfig-1125-\ninit/Kconfig:1126:config BLK_CGROUP\ninit/Kconfig-1127-\tbool \"IO controller\"\n"
} |
| 53/2 | 2026/09/14 08:15 | llm | patch-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 49629 output: 33 thoughts: 26 Thoughts: Here's my thought process, as requested: **Checking Conditional Logic in Kconfig** Okay, so I'm looking at `init/Kconfig` and need to verify something specific. I need to see if line 1126 falls under the `if CGROUPS` conditional. This is a common debugging step and something I do all the time. Knowing the precise location of a configuration option within a conditional block is crucial for understanding its behavior and potential dependencies, especially in a system as complex as the Linux kernel's configuration. This will influence how this feature is enabled, disabled or controlled. |
| 54/2 | 2026/09/14 08:15 | tool | read-file |
0mArgs:{
"File": "init/Kconfig",
"FirstLine": 1000,
"LineCount": 50
}Results: {
"Contents": "1000:\t\tdef_bool y\n1001:\t\n1002:\tconfig CC_NO_STRINGOP_OVERFLOW\n1003:\t\tbool\n1004:\t\tdefault y if CC_IS_GCC \u0026\u0026 GCC_NO_STRINGOP_OVERFLOW\n1005:\t\n1006:\tconfig CC_STRINGOP_OVERFLOW\n1007:\t\tbool\n1008:\t\tdefault y if CC_IS_GCC \u0026\u0026 !CC_NO_STRINGOP_OVERFLOW\n1009:\t\n1010:\t#\n1011:\t# For architectures that know their GCC __int128 support is sound\n1012:\t#\n1013:\tconfig ARCH_SUPPORTS_INT128\n1014:\t\tbool\n1015:\t\n1016:\t# For architectures that (ab)use NUMA to represent different memory regions\n1017:\t# all cpu-local but of different latencies, such as SuperH.\n1018:\t#\n1019:\tconfig ARCH_WANT_NUMA_VARIABLE_LOCALITY\n1020:\t\tbool\n1021:\t\n1022:\tconfig NUMA_BALANCING\n1023:\t\tbool \"Memory placement aware NUMA scheduler\"\n1024:\t\tdepends on ARCH_SUPPORTS_NUMA_BALANCING\n1025:\t\tdepends on ARCH_HAS_PTE_PROTNONE\n1026:\t\tdepends on !ARCH_WANT_NUMA_VARIABLE_LOCALITY\n1027:\t\tdepends on SMP \u0026\u0026 NUMA_MIGRATION \u0026\u0026 !PREEMPT_RT\n1028:\t\thelp\n1029:\t\t This option adds support for automatic NUMA aware memory/task placement.\n1030:\t\t The mechanism is quite primitive and is based on migrating memory when\n1031:\t\t it has references to the node the task is running on.\n1032:\t\n1033:\t\t This system will be inactive on UMA systems.\n1034:\t\n1035:\tconfig SCHED_CACHE\n1036:\t\tbool \"Cache aware load balance\"\n1037:\t\tdefault y\n1038:\t\tdepends on SMP\n1039:\t\thelp\n1040:\t\t When enabled, the scheduler will attempt to aggregate tasks from\n1041:\t\t the same process onto a single Last Level Cache (LLC) domain when\n1042:\t\t possible. This improves cache locality by keeping tasks that share\n1043:\t\t resources within the same cache domain, reducing cache misses and\n1044:\t\t lowering data access latency.\n1045:\t\n1046:\tconfig NUMA_BALANCING_DEFAULT_ENABLED\n1047:\t\tbool \"Automatically enable NUMA aware memory/task placement\"\n1048:\t\tdefault y\n1049:\t\tdepends on NUMA_BALANCING\n"
} |
| 55/2 | 2026/09/14 08:15 | llm | patch-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 50548 output: 33 thoughts: 23 Thoughts: Here's my summary, as if I were the one thinking these thoughts, geared towards an expert: **Diving into `init/Kconfig` for Configuration Insights** Okay, I need to get a handle on how this system initializes its configuration options. I'm going to jump straight into `init/Kconfig` and focus on lines 1080 through 1130. That range is usually where initialization-related options are defined. I'm looking for clues about the boot process, possibly defaults for critical parameters, and how these configurations influence the initial environment. I'll need to sift through this section carefully; I'm expecting some macros or `config` definitions that will shed light on the initial states and what kind of choices are possible at build time. Given the architecture of this system, I'm expecting an influence from the bootloader or early kernel stages. I should also be mindful of dependencies on other configuration parameters, as these lines may link to other parts of the Kconfig tree, perhaps related to security or hardware initialization. Time to dig in and see what's being set up and where the levers are. |
| 56/2 | 2026/09/14 08:15 | tool | read-file |
0mArgs:{
"File": "init/Kconfig",
"FirstLine": 1080,
"LineCount": 50
}Results: {
"Contents": "1080:\t\t which reduces the latencies of dynamic cgroup modifications such\n1081:\t\t as task migrations and controller on/offs at the cost of making\n1082:\t\t hot path operations such as forks and exits more expensive.\n1083:\t\n1084:\t\t Say N if unsure.\n1085:\t\n1086:\tconfig MEMCG\n1087:\t\tbool \"Memory controller\"\n1088:\t\tselect PAGE_COUNTER\n1089:\t\tselect EVENTFD\n1090:\t\tselect SLAB_OBJ_EXT\n1091:\t\tselect VM_EVENT_COUNTERS\n1092:\t\thelp\n1093:\t\t Provides control over the memory footprint of tasks in a cgroup.\n1094:\t\n1095:\tconfig MEMCG_NMI_UNSAFE\n1096:\t\tbool\n1097:\t\tdepends on MEMCG\n1098:\t\tdepends on HAVE_NMI\n1099:\t\tdepends on !ARCH_HAS_NMI_SAFE_THIS_CPU_OPS \u0026\u0026 !ARCH_HAVE_NMI_SAFE_CMPXCHG\n1100:\t\tdefault y\n1101:\t\n1102:\tconfig MEMCG_NMI_SAFETY_REQUIRES_ATOMIC\n1103:\t\tbool\n1104:\t\tdepends on MEMCG\n1105:\t\tdepends on HAVE_NMI\n1106:\t\tdepends on !ARCH_HAS_NMI_SAFE_THIS_CPU_OPS \u0026\u0026 ARCH_HAVE_NMI_SAFE_CMPXCHG\n1107:\t\tdefault y\n1108:\t\n1109:\tconfig MEMCG_V1\n1110:\t\tbool \"Legacy cgroup v1 memory controller\"\n1111:\t\tdepends on MEMCG\n1112:\t\tdefault n\n1113:\t\thelp\n1114:\t\t Legacy cgroup v1 memory controller which has been deprecated by\n1115:\t\t cgroup v2 implementation. The v1 is there for legacy applications\n1116:\t\t which haven't migrated to the new cgroup v2 interface yet. If you\n1117:\t\t do not have any such application then you are completely fine leaving\n1118:\t\t this option disabled.\n1119:\t\n1120:\t\t Please note that feature set of the legacy memory controller is likely\n1121:\t\t going to shrink due to deprecation process. New deployments with v1\n1122:\t\t controller are highly discouraged.\n1123:\t\n1124:\t\t Say N if unsure.\n1125:\t\n1126:\tconfig BLK_CGROUP\n1127:\t\tbool \"IO controller\"\n1128:\t\tdepends on BLOCK\n1129:\t\tdefault n\n"
} |
| 57/2 | 2026/09/14 08:15 | llm | patch-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 51551 output: 29 thoughts: 18 Thoughts: Here's my thought process, as requested: **Navigating the Linux Kernel Configuration Landscape** Okay, so I need to find the configuration options related to `CGROUPS`. My first instinct is to dive into the kernel configuration files, specifically `init/Kconfig`. That's the typical starting point for kernel-level features and their respective Kconfig definitions. I'm assuming that the feature would be defined there. Now I need to know where it is precisely within that file, but I should be able to get there. I'll need to know whether it has a dependent that would make it easy to find. |
| 58/2 | 2026/09/14 08:16 | tool | grepper |
0mArgs:{
"Expression": "menuconfig CGROUPS",
"PathPrefix": "init/Kconfig"
}Results: {
"Output": "init/Kconfig=1054=config SLAB_OBJ_EXT\n--\ninit/Kconfig-1056-\ninit/Kconfig:1057:menuconfig CGROUPS\ninit/Kconfig-1058-\tbool \"Control Group support\"\n"
} |
| 59/2 | 2026/09/14 08:16 | llm | patch-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 51805 output: 29 thoughts: 47 Thoughts: **Navigating the Kconfig Labyrinth** Okay, so I'm digging through this Kconfig file, and I've hit a specific section centered around `CGROUPS`. Line 1057 clearly shows `menuconfig CGROUPS`, which means everything that follows, right up until the `endif # CGROUPS` directive, is conditionally compiled based on whether `CGROUPS` is enabled. That's straightforward, but now I need to quickly pinpoint the end of that conditional block. Knowing the exact scope is crucial for understanding the overall configuration logic, and I need to make sure I don't miss any dependencies or configurations linked to the `CGROUPS` settings. Let's find that `endif`. |
| 60/2 | 2026/09/14 08:17 | tool | grepper |
0mArgs:{
"Expression": "endif.*CGROUPS",
"PathPrefix": "init/Kconfig"
}Results: {
"Output": "init/Kconfig=1404=config SOCK_CGROUP_DATA\n--\ninit/Kconfig-1407-\ninit/Kconfig:1408:endif # CGROUPS\ninit/Kconfig-1409-\n"
} |
| 61/2 | 2026/09/14 08:17 | llm | patch-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 52104 output: 351 thoughts: 639 Thoughts: **Enabling BPF-Based I/O Cost Accounting** Okay, so `BLK_CGROUP` is indeed nested within `if CGROUPS`, which is exactly what I expected. The necessary configurations to enable this BPF-based I/O cost accounting look to be: `CGROUPS`, `BLK_CGROUP`, `BLK_CGROUP_IOCOST`, `BPF_SYSCALL`, `BPF_JIT`, `DEBUG_INFO_BTF`, and `BLK_CGROUP_IOCOST_BPF`. This aligns perfectly. I've also checked the diff for `tools/testing/selftests/bpf/config`, and it confirms that `CONFIG_BLK_CGROUP_IOCOST=y` and `CONFIG_BLK_CGROUP_IOCOST_BPF=y` are present, which is good. Furthermore, the selftests BPF config already includes the required `CGROUPS`, `BLK_CGROUP`, `BPF_SYSCALL`, `BPF_JIT`, and `DEBUG_INFO_BTF` options. So, the testing environment appears ready. Now, let's look at the implementation details. Based on the "FocusSymbols" list, we'll need to make sure we've captured the specific kernel functions responsible for the BPF-based I/O cost logic. These include: `iocost_bpf_model_get`, `iocost_bpf_model_put`, `iocost_notify_blkcg_online`, `iocost_notify_blkcg_offline`, `bpf_iocost_reg`, `bpf_iocost_unreg`, `bpf_iocost_validate`, `ioc_cost_model_write`, `calc_vtime_cost`, and `calc_size_vtime_cost`. I've confirmed that these functions are implemented as straightforward C functions and are not macros or static inlined. Specifically, they're located within the `block/blk-iocost-bpf.c` and `block/blk-iocost.c` files. This is exactly as expected – no surprises here. Given the solid and clean results from this analysis, it looks like we have all the information necessary. It's time to call `set-results` with the relevant configuration and function details to finalize this enabling process. |
| 62/2 | 2026/09/14 08:17 | tool | set-results |
0mArgs:{
"EnableConfigs": [
"BLK_CGROUP_IOCOST_BPF",
"BLK_CGROUP_IOCOST",
"BLK_CGROUP",
"CGROUPS",
"BPF_SYSCALL",
"BPF_JIT",
"DEBUG_INFO_BTF"
],
"FocusSymbols": [
"iocost_bpf_model_get",
"iocost_bpf_model_put",
"iocost_notify_blkcg_online",
"iocost_notify_blkcg_offline",
"bpf_iocost_reg",
"bpf_iocost_unreg",
"bpf_iocost_validate",
"ioc_cost_model_write",
"calc_vtime_cost",
"calc_size_vtime_cost"
],
"Reasoning": "The patch series introduces BPF struct_ops support for pluggable cost models in the blk-iocost cgroup IO controller (CONFIG_BLK_CGROUP_IOCOST_BPF). It adds a new struct_ops type 'iocost_model_ops', a model registry with reference counting and lifecycle tracking, integration with cgroup blkcg online/offline notifications, sysfs/cgroupfs parsing to switch models via 'io.cost.model', and RCU-guarded dispatch to the BPF calc_cost callbacks during bio charging. This modifies reachable core block layer, cgroup, and BPF logic, making it worth fuzzing for potential concurrency, refcounting, or type validation bugs.",
"WorthFuzzing": true
}Results: {
"EnableConfigs": [
"BLK_CGROUP_IOCOST_BPF",
"BLK_CGROUP_IOCOST",
"BLK_CGROUP",
"CGROUPS",
"BPF_SYSCALL",
"BPF_JIT",
"DEBUG_INFO_BTF"
],
"FocusSymbols": [
"iocost_bpf_model_get",
"iocost_bpf_model_put",
"iocost_notify_blkcg_online",
"iocost_notify_blkcg_offline",
"bpf_iocost_reg",
"bpf_iocost_unreg",
"bpf_iocost_validate",
"ioc_cost_model_write",
"calc_vtime_cost",
"calc_size_vtime_cost"
],
"Reasoning": "The patch series introduces BPF struct_ops support for pluggable cost models in the blk-iocost cgroup IO controller (CONFIG_BLK_CGROUP_IOCOST_BPF). It adds a new struct_ops type 'iocost_model_ops', a model registry with reference counting and lifecycle tracking, integration with cgroup blkcg online/offline notifications, sysfs/cgroupfs parsing to switch models via 'io.cost.model', and RCU-guarded dispatch to the BPF calc_cost callbacks during bio charging. This modifies reachable core block layer, cgroup, and BPF logic, making it worth fuzzing for potential concurrency, refcounting, or type validation bugs.",
"WorthFuzzing": true
} |
| 63/1 | 2026/09/14 08:17 | action | If |
4mArgs:{
"WorthFuzzing": true
}Results: null |
| 64/2 | 2026/09/14 08:17 | agent | kmsan-evaluator |
4mModel:core Args: null Results: {
"KMSANReasoning": "The patch introduces BPF struct_ops support for pluggable cost models in the blk-iocost I/O controller (CONFIG_BLK_CGROUP_IOCOST_BPF), along with lifecycle hooks, a new tracepoint (iocost_ioc_tick), and BPF selftests.\n\nAnalysis of memory handling:\n1. Heap allocations: The only new heap allocation is `struct iocost_bpf_model`, which is allocated using `kzalloc()` with all fields (`refs`, `ops`, `list`, `lifecycle`) explicitly initialized.\n2. Stack buffers: `bpf_model` in `ioc_cost_model_write()` is initialized (`bpf_model[0] = '\\0'`) and written via `match_strlcpy()`. The tick tracepoint variables in `ioc_timer_fn()` are all explicitly populated scalar variables. Member name copying in BPF struct_ops uses `bpf_obj_name_cpy()`, which zeroes the target buffer and validates null-termination.\n3. Kernel-to-userspace exposure: No binary kernel structures, unpadded structs, or network packets are sent to user space. The only output is formatted ASCII text printed to `seq_file` in cgroupfs (`io.cost.model`), printing initialized strings and integers.\n4. KASAN vs KMSAN applicability: Any potential issues introduced by this patch involve concurrency, RCU dereferences, BPF struct_ops refcounting, and list management (risking use-after-free, double free, or deadlocks), which are detected by KASAN and LOCKDEP. There are no uninitialized memory reads or information leak hazards that would require KMSAN.",
"NeedsKMSAN": false
}Instruction: You are an expert Linux kernel security engineer specializing in kernel memory
error detectors (KASAN and KMSAN). Your job is to review the provided patch series and
determine if the code changes justify spawning a dedicated KMSAN (KernelMemorySanitizer)
fuzzing session in addition to standard KASAN fuzzing.
CRITICAL DISTINCTION BETWEEN KASAN AND KMSAN:
- Standard KASAN kernel builds (upstream-apparmor-kasan.config) already enable
a comprehensive suite of debugging tools and sanitizers, including KASAN
(out-of-bounds accesses, use-after-free, double free, invalid free), LOCKDEP
(locking bugs and deadlocks), UB-sanitizers, and memory corruption checks.
- KMSAN (KernelMemorySanitizer) detects reads of UNINITIALIZED memory (stack, heap,
or page allocations) and kernel-to-user memory info-leaks.
Rule: THERE IS NO SENSE IN RUNNING A KMSAN SESSION IF A BUG CAN BE CAUGHT BY KASAN,
LOCKDEP, OR OTHER STANDARD BUG DETECTORS.
A dedicated KMSAN fuzzing session incurs significant resource costs. You must ONLY
set NeedsKMSAN=true if the code changes introduce or expose UNINITIALIZED MEMORY risks
that are detected ONLY by KMSAN.
Look holistically at the patch series and surrounding code. Even if no direct
uninitialized field accesses or new buffer allocations are added in the diff itself,
a patch may alter control flow, bounds checking, or data length calculations in ways
that change how the rest of the code operates on existing buffers (e.g. allowing
uninitialized stack/heap memory to be read, copied to user space, or used in control
flow). Do not hesitate to use your code access tools to inspect the surrounding code,
called functions, and callers.
Set NeedsKMSAN=true ONLY IF the patch introduces or modifies:
1. Kernel structures sent to user space (via copy_to_user, put_user, netlink skb
attributes, ioctl output arguments, socket options, or BPF buffers) where fields
or structure padding might not be fully initialized/zeroed.
2. Conditional logic or branching that depends on potentially uninitialized variables
or struct fields.
3. Allocation or initialization of complex data structures where uninitialized fields
could be read later in reachable code paths.
4. Bounds checks, lengths, or logic in a way that allows surrounding code to access
uninitialized bytes of existing buffers.
Set NeedsKMSAN=false IF:
- The code changes primarily risk out-of-bounds access, array overflows, NULL pointer
dereferences, locking deadlocks, or use-after-free bugs (these are already caught
by KASAN, LOCKDEP, or standard bug detectors).
- All stack/heap structures touched or introduced by the patch are fully zeroed
or initialized (e.g. using = {0}, memset, kzalloc) before being read or copied.
- The patch does not introduce any risk of uninitialized memory usage or info-leaks.
Use your code access tools to inspect the surrounding code if necessary, then provide
detailed KMSANReasoning contrasting KASAN vs KMSAN applicability for this patch.
Prefer calling several tools at the same time to save round-trips.
Use set-results tool to provide results of the analysis.
It must be called exactly once before the final reply.
Ignore results of this tool.
Prompt: Target architecture: amd64
For your convenience, here is the diff of the changes:
commit 7af58b5c104951da88ad0e3e0a7f79c1b30e46ca
Author: syz-cluster <triage@syzkaller.com>
Date: Mon Sep 14 07:59:42 2026 +0000
syz-cluster: applied patch under review
diff --git a/Documentation/admin-guide/cgroup-v2.rst b/Documentation/admin-guide/cgroup-v2.rst
index 8d2603751c51a..0d2d0525255cb 100644
--- a/Documentation/admin-guide/cgroup-v2.rst
+++ b/Documentation/admin-guide/cgroup-v2.rst
@@ -2124,6 +2124,18 @@ IO Interface Files
parameters are written to, "ctrl" become "user" and the
automatic changes are disabled.
+ When CONFIG_BLK_CGROUP_IOCOST_BPF is enabled, "model" also accepts
+ the name of a registered iocost_model_ops BPF struct_ops model:
+ "model=<name>" binds the model to the device and the model fully
+ replaces the builtin linear pricing, for every operation including
+ flushes; "model=linear" (or "ctrl=auto/user") restores the builtin
+ model. Writing an unknown name fails with ENOENT and nothing is
+ applied. Unregistering a model removes its name so it can no
+ longer be selected; devices already bound keep using it, and keep
+ receiving cgroup lifecycle notifications, until switched back to
+ the builtin model. See include/linux/blk-iocost.h
+ for the model interface.
+
When "model" is "linear", the following model parameters are
defined.
diff --git a/block/Kconfig b/block/Kconfig
index 70e4a66d941ff..91e808f86d28d 100644
--- a/block/Kconfig
+++ b/block/Kconfig
@@ -231,4 +231,13 @@ config BLK_ERROR_INJECTION
source "block/Kconfig.iosched"
+config BLK_CGROUP_IOCOST_BPF
+ bool "Enable BPF pluggable cost model support for the cost IO controller"
+ depends on BLK_CGROUP_IOCOST && BPF_SYSCALL && BPF_JIT && DEBUG_INFO_BTF
+ help
+ Enabling this option registers the "iocost_model_ops" BPF
+ struct_ops type, which allows a BPF program to fully replace
+ the builtin linear cost model on a device it is bound to
+ through io.cost.model.
+
endif # BLOCK
diff --git a/block/Makefile b/block/Makefile
index e7bd320e3d697..ee5cebeea006f 100644
--- a/block/Makefile
+++ b/block/Makefile
@@ -39,3 +39,4 @@ obj-$(CONFIG_BLK_INLINE_ENCRYPTION) += blk-crypto.o blk-crypto-profile.o \
blk-crypto-sysfs.o
obj-$(CONFIG_BLK_INLINE_ENCRYPTION_FALLBACK) += blk-crypto-fallback.o
obj-$(CONFIG_BLOCK_HOLDER_DEPRECATED) += holder.o
+obj-$(CONFIG_BLK_CGROUP_IOCOST_BPF) += blk-iocost-bpf.o
diff --git a/block/blk-cgroup.c b/block/blk-cgroup.c
index 2b5c29434e426..872871045351c 100644
--- a/block/blk-cgroup.c
+++ b/block/blk-cgroup.c
@@ -32,6 +32,7 @@
#include <linux/part_stat.h>
#include "blk.h"
#include "blk-cgroup.h"
+#include <linux/blk-iocost.h>
#include "blk-ioprio.h"
#include "blk-throttle.h"
@@ -1341,6 +1342,7 @@ void blkcg_unpin_online(struct cgroup_subsys_state *blkcg_css)
*/
static void blkcg_css_offline(struct cgroup_subsys_state *css)
{
+ iocost_notify_blkcg_offline(css_to_blkcg(css));
/* this prevents anyone from attaching or migrating to this blkcg */
wb_blkcg_offline(css);
@@ -1445,6 +1447,7 @@ blkcg_css_alloc(struct cgroup_subsys_state *parent_css)
static int blkcg_css_online(struct cgroup_subsys_state *css)
{
+ iocost_notify_blkcg_online(css_to_blkcg(css));
struct blkcg *parent = blkcg_parent(css_to_blkcg(css));
/*
diff --git a/block/blk-iocost-bpf.c b/block/blk-iocost-bpf.c
new file mode 100644
index 0000000000000..7ad88600c203a
--- /dev/null
+++ b/block/blk-iocost-bpf.c
@@ -0,0 +1,307 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * blk-iocost: BPF struct_ops plumbing for pluggable cost models.
+ *
+ * Registers the "iocost_model_ops" struct_ops type and maintains the
+ * name registry of registered models. A registered model is bound to
+ * a device through io.cost.model; see include/linux/blk-iocost.h.
+ */
+#include <linux/init.h>
+#include <linux/kernel.h>
+#include <linux/module.h>
+#include <linux/mutex.h>
+#include <linux/slab.h>
+#include <linux/bpf.h>
+#include <linux/bpf_verifier.h>
+#include <linux/btf.h>
+#include <linux/blk-iocost.h>
+
+static DEFINE_MUTEX(iocost_bpf_reg_lock);
+static LIST_HEAD(iocost_bpf_models);
+static LIST_HEAD(iocost_bpf_lifecycle);
+
+/*
+ * The registry holds a bpf_struct_ops_get() reference obtained in .reg;
+ * .unreg drops it, so the kdata of an unregistered model stays alive
+ * while any device is still bound to it.
+ */
+struct iocost_bpf_model {
+ struct list_head list; /* name registry */
+ struct list_head lifecycle; /* lifecycle notify list */
+ const struct iocost_model_ops *ops;
+ refcount_t refs;
+};
+
+/*
+ * Look up a registered model by name and acquire a reference on it.
+ * The registry lock is held across lookup and bpf_struct_ops_get() so
+ * the model cannot be unregistered in between.
+ */
+int iocost_bpf_model_get(const char *name,
+ const struct iocost_model_ops **opsp)
+{
+ struct iocost_bpf_model *m;
+ int ret = -ENOENT;
+
+ mutex_lock(&iocost_bpf_reg_lock);
+ list_for_each_entry(m, &iocost_bpf_models, list) {
+ if (!strcmp(m->ops->name, name)) {
+ if (bpf_struct_ops_get(m->ops)) {
+ refcount_inc(&m->refs);
+ *opsp = m->ops;
+ ret = 0;
+ }
+ break;
+ }
+ }
+ mutex_unlock(&iocost_bpf_reg_lock);
+ return ret;
+}
+
+static struct iocost_bpf_model *
+iocost_bpf_model_lookup(const struct iocost_model_ops *ops)
+{
+ struct iocost_bpf_model *m;
+
+ list_for_each_entry(m, &iocost_bpf_models, list) {
+ if (m->ops == ops)
+ return m;
+ }
+ return NULL;
+}
+
+/*
+ * Lifecycle notifications walk the lifecycle list, which keeps a node
+ * for as long as any device has the model bound, so an unregistered
+ * but still-bound model keeps receiving blkcg online/offline.
+ */
+void iocost_notify_blkcg_online(struct blkcg *blkcg)
+{
+ struct iocost_bpf_model *m;
+
+ guard(mutex)(&iocost_bpf_reg_lock);
+ list_for_each_entry(m, &iocost_bpf_lifecycle, lifecycle) {
+ if (m->ops->blkcg_online)
+ m->ops->blkcg_online(blkcg);
+ }
+}
+
+void iocost_notify_blkcg_offline(struct blkcg *blkcg)
+{
+ struct iocost_bpf_model *m;
+
+ guard(mutex)(&iocost_bpf_reg_lock);
+ list_for_each_entry(m, &iocost_bpf_lifecycle, lifecycle) {
+ if (m->ops->blkcg_offline)
+ m->ops->blkcg_offline(blkcg);
+ }
+}
+
+static int bpf_iocost_model_init(struct btf *btf)
+{
+ s32 type_id;
+
+ type_id = btf_find_by_name_kind(btf, "iocost_model_ops", BTF_KIND_STRUCT);
+ if (type_id < 0)
+ return -EINVAL;
+ return 0;
+}
+
+static bool bpf_iocost_is_valid_access(int off, int size,
+ enum bpf_access_type type,
+ const struct bpf_prog *prog,
+ struct bpf_insn_access_aux *info)
+{
+ return bpf_tracing_btf_ctx_access(off, size, type, prog, info);
+}
+
+static const struct bpf_func_proto *
+bpf_iocost_get_func_proto(enum bpf_func_id func_id,
+ const struct bpf_prog *prog)
+{
+ switch (func_id) {
+#ifdef CONFIG_CGROUPS
+ case BPF_FUNC_cgrp_storage_get:
+ return &bpf_cgrp_storage_get_proto;
+#endif
+ default:
+ return bpf_base_func_proto(func_id, prog);
+ }
+}
+
+static int bpf_iocost_check_member(const struct btf_type *t,
+ const struct btf_member *member,
+ const struct bpf_prog *prog)
+{
+ /* calc_cost() is called with RCU read lock held */
+ if (prog->sleepable)
+ return -EINVAL;
+ return 0;
+}
+
+static int bpf_iocost_init_member(const struct btf_type *t,
+ const struct btf_member *member,
+ void *kdata, const void *udata)
+{
+ struct iocost_model_ops *ops = kdata;
+ const struct iocost_model_ops *uops = udata;
+ u32 moff = __btf_member_bit_offset(t, member) / 8;
+
+ switch (moff) {
+ case offsetof(struct iocost_model_ops, name):
+ if (bpf_obj_name_cpy(ops->name, uops->name,
+ sizeof(ops->name)) <= 0)
+ return -EINVAL;
+ return 1;
+ }
+
+ return 0;
+}
+
+static u64 bpf_iocost_calc_cost_stub(u64 opf, u64 nbytes, u64 sector,
+ struct blkcg *blkcg, u64 model_flags);
+
+/*
+ * kdata is seeded from the CFI stubs, so calc_cost is never NULL; a
+ * model which did not implement it inherits the stub, which prices
+ * every IO at 0. Compare against the stub to reject it.
+ */
+static int bpf_iocost_validate(void *kdata)
+{
+ struct iocost_model_ops *ops = kdata;
+
+ if (ops->calc_cost == bpf_iocost_calc_cost_stub)
+ return -EINVAL;
+ return 0;
+}
+
+static int bpf_iocost_reg(void *kdata, struct bpf_link *link)
+{
+ struct iocost_model_ops *ops = kdata;
+ struct iocost_bpf_model *m;
+ int ret = 0;
+
+ if (!bpf_struct_ops_get(ops))
+ return -ENOENT;
+
+ m = kzalloc(sizeof(*m), GFP_KERNEL);
+ if (!m) {
+ bpf_struct_ops_put(ops);
+ return -ENOMEM;
+ }
+ refcount_set(&m->refs, 1);
+
+ mutex_lock(&iocost_bpf_reg_lock);
+ {
+ struct iocost_bpf_model *other;
+
+ list_for_each_entry(other, &iocost_bpf_models, list) {
+ if (!strcmp(other->ops->name, ops->name)) {
+ ret = -EEXIST;
+ break;
+ }
+ }
+ }
+ if (!ret) {
+ m->ops = ops;
+ list_add(&m->list, &iocost_bpf_models);
+ list_add(&m->lifecycle, &iocost_bpf_lifecycle);
+ }
+ mutex_unlock(&iocost_bpf_reg_lock);
+
+ if (ret) {
+ bpf_struct_ops_put(ops);
+ kfree(m);
+ }
+ return ret;
+}
+
+/*
+ * Unregistering drops the registration reference. When the last
+ * reference is gone (no device bound), the node leaves the lifecycle
+ * list and is freed; otherwise bound devices keep it alive and it
+ * keeps receiving blkcg online/offline notifications.
+ */
+static void bpf_iocost_unreg(void *kdata, struct bpf_link *link)
+{
+ struct iocost_model_ops *ops = kdata;
+ struct iocost_bpf_model *m;
+
+ mutex_lock(&iocost_bpf_reg_lock);
+ m = iocost_bpf_model_lookup(ops);
+ if (m) {
+ list_del(&m->list);
+ if (refcount_dec_and_test(&m->refs)) {
+ list_del(&m->lifecycle);
+ kfree(m);
+ }
+ }
+ mutex_unlock(&iocost_bpf_reg_lock);
+
+ bpf_struct_ops_put(ops);
+}
+
+void iocost_bpf_model_put(const struct iocost_model_ops *ops)
+{
+ struct iocost_bpf_model *m;
+
+ mutex_lock(&iocost_bpf_reg_lock);
+ list_for_each_entry(m, &iocost_bpf_lifecycle, lifecycle) {
+ if (m->ops == ops)
+ break;
+ }
+ /*
+ * Exactly one bpf_struct_ops_put() per call, pairing the
+ * bpf_struct_ops_get() in iocost_bpf_model_get(); the node is
+ * freed when the last reference goes, whichever side drops it.
+ */
+ if (&m->lifecycle != &iocost_bpf_lifecycle &&
+ refcount_dec_and_test(&m->refs)) {
+ list_del(&m->lifecycle);
+ mutex_unlock(&iocost_bpf_reg_lock);
+ kfree(m);
+ bpf_struct_ops_put(ops);
+ return;
+ }
+ mutex_unlock(&iocost_bpf_reg_lock);
+ bpf_struct_ops_put(ops);
+}
+
+static const struct bpf_verifier_ops bpf_iocost_verifier_ops = {
+ .get_func_proto = bpf_iocost_get_func_proto,
+ .is_valid_access = bpf_iocost_is_valid_access,
+};
+
+static u64 bpf_iocost_calc_cost_stub(u64 opf, u64 nbytes, u64 sector,
+ struct blkcg *blkcg, u64 flags)
+{
+ return 0;
+}
+
+static void bpf_iocost_blkcg_online_stub(struct blkcg *blkcg) { }
+static void bpf_iocost_blkcg_offline_stub(struct blkcg *blkcg) { }
+
+static struct iocost_model_ops __bpf_ops_iocost_model_ops = {
+ .calc_cost = bpf_iocost_calc_cost_stub,
+ .blkcg_online = bpf_iocost_blkcg_online_stub,
+ .blkcg_offline = bpf_iocost_blkcg_offline_stub,
+};
+
+static struct bpf_struct_ops bpf_iocost_model_ops = {
+ .verifier_ops = &bpf_iocost_verifier_ops,
+ .init = bpf_iocost_model_init,
+ .check_member = bpf_iocost_check_member,
+ .init_member = bpf_iocost_init_member,
+ .validate = bpf_iocost_validate,
+ .reg = bpf_iocost_reg,
+ .unreg = bpf_iocost_unreg,
+ .name = "iocost_model_ops",
+ .cfi_stubs = &__bpf_ops_iocost_model_ops,
+ .owner = THIS_MODULE,
+};
+
+static int __init bpf_iocost_init(void)
+{
+ return register_bpf_struct_ops(&bpf_iocost_model_ops, iocost_model_ops);
+}
+late_initcall(bpf_iocost_init);
diff --git a/block/blk-iocost.c b/block/blk-iocost.c
index 2745bffcd5eef..96126a185f9a6 100644
--- a/block/blk-iocost.c
+++ b/block/blk-iocost.c
@@ -177,6 +177,7 @@
#include <linux/timer.h>
#include <linux/time64.h>
#include <linux/parser.h>
+#include <linux/blk-iocost.h>
#include <linux/sched/signal.h>
#include <asm/local.h>
#include <asm/local64.h>
@@ -445,6 +446,11 @@ struct ioc {
int autop_idx;
bool user_qos_params:1;
bool user_cost_model:1;
+
+#ifdef CONFIG_BLK_CGROUP_IOCOST_BPF
+ /* bound BPF cost model, NULL = builtin linear model */
+ const struct iocost_model_ops __rcu *model;
+#endif
};
struct iocg_pcpu_stat {
@@ -2238,6 +2244,7 @@ static void ioc_timer_fn(struct timer_list *timer)
struct ioc_now now;
LIST_HEAD(surpluses);
int nr_debtors, nr_shortages = 0, nr_lagging = 0;
+ int nr_active = 0;
u64 usage_us_sum = 0;
u32 ppm_rthr;
u32 ppm_wthr;
@@ -2274,6 +2281,8 @@ static void ioc_timer_fn(struct timer_list *timer)
u64 vdone, vtime, usage_us;
u32 hw_active, hw_inuse;
+ nr_active++;
+
/*
* Collect unused and wind vtime closer to vnow to prevent
* iocgs from accumulating a large amount of budget.
@@ -2435,6 +2444,14 @@ static void ioc_timer_fn(struct timer_list *timer)
ioc->busy_level = clamp(ioc->busy_level, -1000, 1000);
+ /*
+ * vrate and period_us change right below; snapshot the values
+ * this period ran in so the tick below reports the period's own
+ * parameters instead of the next period's.
+ */
+ u32 tick_period_us = ioc->period_us;
+ u64 tick_vrate = ioc->vtime_base_rate;
+
ioc_adjust_base_vrate(ioc, rq_wait_pct, nr_lagging, nr_shortages,
prev_busy_level, missed_ppm);
@@ -2448,16 +2465,38 @@ static void ioc_timer_fn(struct timer_list *timer)
*/
atomic64_inc(&ioc->cur_period);
- if (ioc->running != IOC_STOP) {
- if (!list_empty(&ioc->active_iocgs)) {
- ioc_start_period(ioc, &now);
- } else {
- ioc->busy_level = 0;
- ioc->vtime_err = 0;
- ioc->running = IOC_IDLE;
- }
+ /*
+ * Snapshot the state this period ran in before the idle
+ * transition wipes it, so the final tick reports the period's
+ * own busy level (e.g. the saturation that drove the controller
+ * idle) instead of the cleared one. usage is normalized by the
+ * measured period length, captured before ioc_start_period()
+ * overwrites period_at, the same way the donation loop does.
+ */
+ {
+ int tick_busy = ioc->busy_level;
+ int tick_running = ioc->running;
+ u64 tick_dur = now.now - ioc->period_at;
+ /* cur_period was already advanced to N+1 above; report
+ * the period that just ended, like the other fields */
+ u64 tick_period = atomic64_read(&ioc->cur_period) - 1;
+
+ trace_iocost_ioc_tick(ioc, nr_active, usage_us_sum,
+ tick_period, tick_period_us,
+ tick_vrate, tick_busy, tick_running,
+ tick_dur);
+
+ if (ioc->running != IOC_STOP) {
+ if (!list_empty(&ioc->active_iocgs)) {
+ ioc_start_period(ioc, &now);
+ } else {
+ ioc->busy_level = 0;
+ ioc->vtime_err = 0;
+ ioc->running = IOC_IDLE;
+ }
- ioc_refresh_vrate(ioc, &now);
+ ioc_refresh_vrate(ioc, &now);
+ }
}
spin_unlock_irq(&ioc->lock);
@@ -2571,10 +2610,28 @@ static void calc_vtime_cost_builtin(struct bio *bio, struct ioc_gq *iocg,
static u64 calc_vtime_cost(struct bio *bio, struct ioc_gq *iocg, bool is_merge)
{
+#ifdef CONFIG_BLK_CGROUP_IOCOST_BPF
+ const struct iocost_model_ops *model;
u64 cost;
- calc_vtime_cost_builtin(bio, iocg, is_merge, &cost);
- return cost;
+ rcu_read_lock();
+ model = rcu_dereference(iocg->ioc->model);
+ if (model) {
+ cost = model->calc_cost(bio->bi_opf, bio->bi_iter.bi_size,
+ bio->bi_iter.bi_sector,
+ iocg_to_blkg(iocg)->blkcg,
+ is_merge ? IOCOST_COST_F_MERGE : 0);
+ rcu_read_unlock();
+ return min(cost, VTIME_PER_SEC);
+ }
+ rcu_read_unlock();
+#endif
+ {
+ u64 cost;
+
+ calc_vtime_cost_builtin(bio, iocg, is_merge, &cost);
+ return cost;
+ }
}
static void calc_size_vtime_cost_builtin(struct request *rq, struct ioc *ioc,
@@ -2596,10 +2653,28 @@ static void calc_size_vtime_cost_builtin(struct request *rq, struct ioc *ioc,
static u64 calc_size_vtime_cost(struct request *rq, struct ioc *ioc)
{
- u64 cost;
+#ifdef CONFIG_BLK_CGROUP_IOCOST_BPF
+ const struct iocost_model_ops *model;
- calc_size_vtime_cost_builtin(rq, ioc, &cost);
- return cost;
+ rcu_read_lock();
+ model = rcu_dereference(ioc->model);
+ if (model && rq->bio && rq->bio->bi_blkg) {
+ u64 cost;
+
+ cost = model->calc_cost(rq->cmd_flags, blk_rq_bytes(rq),
+ blk_rq_pos(rq),
+ rq->bio->bi_blkg->blkcg, 0);
+ rcu_read_unlock();
+ return min(cost, VTIME_PER_SEC);
+ }
+ rcu_read_unlock();
+#endif
+ {
+ u64 cost;
+
+ calc_size_vtime_cost_builtin(rq, ioc, &cost);
+ return cost;
+ }
}
enum over_budget_action {
@@ -2900,6 +2975,19 @@ static void ioc_rqos_exit(struct rq_qos *rqos)
timer_shutdown_sync(&ioc->timer);
free_percpu(ioc->pcpu_stat);
+#ifdef CONFIG_BLK_CGROUP_IOCOST_BPF
+ {
+ const struct iocost_model_ops *model;
+
+ spin_lock_irq(&ioc->lock);
+ model = rcu_dereference_protected(ioc->model,
+ lockdep_is_held(&ioc->lock));
+ rcu_assign_pointer(ioc->model, NULL);
+ spin_unlock_irq(&ioc->lock);
+ if (model)
+ iocost_bpf_model_put(model);
+ }
+#endif
kfree(ioc);
}
@@ -3438,12 +3526,30 @@ static u64 ioc_cost_model_prfill(struct seq_file *sf,
return 0;
spin_lock_irq(&ioc->lock);
+#ifdef CONFIG_BLK_CGROUP_IOCOST_BPF
+ {
+ const struct iocost_model_ops *model =
+ rcu_dereference_protected(ioc->model,
+ lockdep_is_held(&ioc->lock));
+
+ seq_printf(sf, "%s ctrl=%s model=%s "
+ "rbps=%llu rseqiops=%llu rrandiops=%llu "
+ "wbps=%llu wseqiops=%llu wrandiops=%llu\n",
+ dname, model ? "bpf" :
+ ioc->user_cost_model ? "user" : "auto",
+ model ? model->name : "linear",
+ u[I_LCOEF_RBPS], u[I_LCOEF_RSEQIOPS],
+ u[I_LCOEF_RRANDIOPS], u[I_LCOEF_WBPS],
+ u[I_LCOEF_WSEQIOPS], u[I_LCOEF_WRANDIOPS]);
+ }
+#else
seq_printf(sf, "%s ctrl=%s model=linear "
"rbps=%llu rseqiops=%llu rrandiops=%llu "
"wbps=%llu wseqiops=%llu wrandiops=%llu\n",
dname, ioc->user_cost_model ? "user" : "auto",
u[I_LCOEF_RBPS], u[I_LCOEF_RSEQIOPS], u[I_LCOEF_RRANDIOPS],
u[I_LCOEF_WBPS], u[I_LCOEF_WSEQIOPS], u[I_LCOEF_WRANDIOPS]);
+#endif
spin_unlock_irq(&ioc->lock);
return 0;
}
@@ -3457,6 +3563,52 @@ static int ioc_cost_model_show(struct seq_file *sf, void *v)
return 0;
}
+/*
+ * Resolve the model name and take a reference on the new model before
+ * anything is applied, so a bad name rejects the whole write. The
+ * registry lookup takes the registration mutex and must stay outside
+ * ioc->lock. The returned model is per-write state passed back into
+ * ioc_bpf_model_commit(), which performs the pointer swap under
+ * ioc->lock so concurrent writers cannot interleave a half-applied
+ * configuration, and the caller drops the old model's reference
+ * afterwards.
+ */
+static const struct iocost_model_ops *
+ioc_bpf_model_prepare(const char *name)
+{
+#ifdef CONFIG_BLK_CGROUP_IOCOST_BPF
+ const struct iocost_model_ops *new = NULL;
+ int ret;
+
+ if (!name[0])
+ return NULL;
+ ret = iocost_bpf_model_get(name, &new);
+ return ret ? ERR_PTR(ret) : new;
+#else
+ return name[0] ? ERR_PTR(-ENOENT) : NULL;
+#endif
+}
+
+/*
+ * Swap in the model returned by ioc_bpf_model_prepare(). Called with
+ * ioc->lock held; dropping the old model's reference may sleep, so
+ * the caller does it after releasing the lock.
+ */
+static const struct iocost_model_ops *
+ioc_bpf_model_commit(struct ioc *ioc, const struct iocost_model_ops *new)
+{
+#ifdef CONFIG_BLK_CGROUP_IOCOST_BPF
+ const struct iocost_model_ops *old;
+
+ old = rcu_dereference_protected(ioc->model,
+ lockdep_is_held(&ioc->lock));
+ rcu_assign_pointer(ioc->model, new);
+ return old;
+#else
+ return NULL;
+#endif
+}
+
static const match_table_t cost_ctrl_tokens = {
{ COST_CTRL, "ctrl=%s" },
{ COST_MODEL, "model=%s" },
@@ -3482,6 +3634,7 @@ static ssize_t ioc_cost_model_write(struct kernfs_open_file *of, char *input,
struct ioc *ioc;
u64 u[NR_I_LCOEFS];
bool user;
+ char bpf_model[IOCOST_MODEL_NAME_LEN];
char *body, *p;
int ret;
@@ -3512,6 +3665,7 @@ static ssize_t ioc_cost_model_write(struct kernfs_open_file *of, char *input,
spin_lock_irq(&ioc->lock);
memcpy(u, ioc->params.i_lcoefs, sizeof(u));
user = ioc->user_cost_model;
+ bpf_model[0] = '\0';
ret = -EINVAL;
@@ -3533,11 +3687,16 @@ static ssize_t ioc_cost_model_write(struct kernfs_open_file *of, char *input,
user = true;
else
goto unlock;
+ bpf_model[0] = '\0';
continue;
case COST_MODEL:
match_strlcpy(buf, &args[0], sizeof(buf));
- if (strcmp(buf, "linear"))
- goto unlock;
+ if (!strcmp(buf, "linear")) {
+ /* back to the builtin linear model */
+ bpf_model[0] = '\0';
+ continue;
+ }
+ match_strlcpy(bpf_model, &args[0], sizeof(bpf_model));
continue;
}
@@ -3550,19 +3709,41 @@ static ssize_t ioc_cost_model_write(struct kernfs_open_file *of, char *input,
user = true;
}
- if (user) {
- memcpy(ioc->params.i_lcoefs, u, sizeof(u));
- ioc->user_cost_model = true;
- } else {
- ioc->user_cost_model = false;
- }
- ioc_refresh_params(ioc, true);
-
ret = 0;
unlock:
spin_unlock_irq(&ioc->lock);
+ /*
+ * Resolve the model name outside ioc->lock (the registry lookup
+ * takes a mutex), so a bad name rejects the whole write before
+ * anything is applied. The resolved model is this write's own
+ * state, not per-device state, so concurrent writers cannot
+ * clobber each other's staged model. On success, re-take the
+ * lock to apply the coefficients and swap the model in one go.
+ */
+ if (!ret) {
+ const struct iocost_model_ops *new, *old;
+
+ new = ioc_bpf_model_prepare(bpf_model);
+ if (IS_ERR(new)) {
+ ret = PTR_ERR(new);
+ } else {
+ spin_lock_irq(&ioc->lock);
+ if (user) {
+ memcpy(ioc->params.i_lcoefs, u, sizeof(u));
+ ioc->user_cost_model = true;
+ } else {
+ ioc->user_cost_model = false;
+ }
+ ioc_refresh_params(ioc, true);
+ old = ioc_bpf_model_commit(ioc, new);
+ spin_unlock_irq(&ioc->lock);
+ if (IS_ENABLED(CONFIG_BLK_CGROUP_IOCOST_BPF) && old)
+ iocost_bpf_model_put(old);
+ }
+ }
+
blk_mq_unquiesce_queue(q);
blk_mq_unfreeze_queue(q, memflags);
diff --git a/include/linux/blk-iocost.h b/include/linux/blk-iocost.h
new file mode 100644
index 0000000000000..d4bd2b0c826b3
--- /dev/null
+++ b/include/linux/blk-iocost.h
@@ -0,0 +1,85 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+#ifndef _LINUX_BLK_IOCOST_H
+#define _LINUX_BLK_IOCOST_H
+
+#include <linux/types.h>
+#include <linux/blk_types.h>
+
+#define IOCOST_MODEL_NAME_LEN 16
+
+#ifdef CONFIG_BLK_CGROUP_IOCOST_BPF
+
+struct blkcg;
+
+/*
+ * Pluggable cost model interface for blk-iocost.
+ *
+ * A BPF struct_ops implementation registered against "iocost_model_ops"
+ * fully replaces the builtin linear model on the devices it is bound to
+ * through io.cost.model. The model owns pricing for every IO on a bound
+ * device: it prices all operations, including flushes, and it is called
+ * from the bio charging path. The completion-time request sizing for
+ * the latency met/missed accounting still uses the builtin coefficients
+ * (the request's bio, and with it the issuing cgroup, is gone by then);
+ * extending the model there is an open question of this interface.
+ *
+ * calc_cost() is called from the IO submission path with RCU read lock
+ * held and must not sleep. It returns the cost of the IO in vtime
+ * units, where 1 second of device time equals VTIME_PER_SEC (2^37,
+ * available to BPF programs through vmlinux.h). The returned value is
+ * clamped to 1 second of device time per IO.
+ *
+ * The model is passed the blkcg of the issuing cgroup so it can keep
+ * per-cgroup state. State keyed by the blkcg alone is shared across
+ * every device the model is bound to, unlike the builtin cursor which
+ * is per (cgroup, device). blkcg_online()/blkcg_offline() are optional
+ * callbacks mirroring the blkcg css lifecycle: state created on online
+ * (or lazily on first use) must be released on offline.
+ *
+ * The registration and binding model follows the TCP congestion
+ * control framework: registering a struct_ops makes the model available
+ * by its name, while io.cost.model binds one registered model to a
+ * device. Unregistering removes the name from the registry; devices
+ * already bound keep using it until switched back to the builtin
+ * model.
+ */
+
+/*
+ * iocost-specific call metadata for calc_cost()'s model_flags
+ * argument; everything else, including REQ_PREFLUSH/REQ_FUA, is
+ * already present in the opf argument
+ */
+#define IOCOST_COST_F_MERGE (1ULL << 0) /* called from merge path */
+
+struct iocost_model_ops {
+ u64 (*calc_cost)(u64 opf, u64 nbytes, sector_t sector,
+ struct blkcg *blkcg, u64 model_flags);
+ void (*blkcg_online)(struct blkcg *blkcg);
+ void (*blkcg_offline)(struct blkcg *blkcg);
+
+ /* model name, used to select the model through io.cost.model */
+ char name[16];
+};
+
+int iocost_bpf_model_get(const char *name,
+ const struct iocost_model_ops **opsp);
+void iocost_bpf_model_put(const struct iocost_model_ops *ops);
+void iocost_notify_blkcg_online(struct blkcg *blkcg);
+void iocost_notify_blkcg_offline(struct blkcg *blkcg);
+
+#else /* CONFIG_BLK_CGROUP_IOCOST_BPF */
+
+struct blkcg;
+struct iocost_model_ops;
+
+static inline int iocost_bpf_model_get(const char *name,
+ const struct iocost_model_ops **opsp)
+{
+ return -EOPNOTSUPP;
+}
+static inline void iocost_bpf_model_put(const struct iocost_model_ops *ops) { }
+static inline void iocost_notify_blkcg_online(struct blkcg *blkcg) { }
+static inline void iocost_notify_blkcg_offline(struct blkcg *blkcg) { }
+
+#endif /* CONFIG_BLK_CGROUP_IOCOST_BPF */
+#endif /* _LINUX_BLK_IOCOST_H */
diff --git a/include/trace/events/iocost.h b/include/trace/events/iocost.h
index e772b1bc60d60..ec5d9c453d55a 100644
--- a/include/trace/events/iocost.h
+++ b/include/trace/events/iocost.h
@@ -178,6 +178,52 @@ TRACE_EVENT(iocost_ioc_vrate_adj,
)
);
+/*
+ * Periodic per-device summary, emitted once per period from the tail of
+ * ioc_timer_fn(). Unlike the state-change events above, this fires every
+ * period the controller is running, including steady states, and carries
+ * the overall controller state so basic monitoring doesn't require drgn.
+ */
+TRACE_EVENT(iocost_ioc_tick,
+
+ TP_PROTO(struct ioc *ioc, int nr_active, u64 usage_us_sum,
+ u64 cur_period, u32 tick_period_us, u64 tick_vrate,
+ int tick_busy, int tick_running, u64 tick_dur),
+
+ TP_ARGS(ioc, nr_active, usage_us_sum, cur_period,
+ tick_period_us, tick_vrate,
+ tick_busy, tick_running, tick_dur),
+
+ TP_STRUCT__entry (
+ __string(devname, ioc_name(ioc))
+ __field(u64, cur_period)
+ __field(u32, period_us)
+ __field(u64, vrate)
+ __field(int, busy_level)
+ __field(int, nr_active)
+ __field(u32, usage_pct)
+ __field(int, running)
+ ),
+
+ TP_fast_assign(
+ __assign_str(devname);
+ __entry->cur_period = cur_period;
+ __entry->period_us = tick_period_us;
+ __entry->vrate = tick_vrate;
+ __entry->busy_level = tick_busy;
+ __entry->nr_active = nr_active;
+ __entry->usage_pct = tick_dur ?
+ div_u64(usage_us_sum * 100, tick_dur) : 0;
+ __entry->running = tick_running;
+ ),
+
+ TP_printk("[%s] period=%llu:%uus vrate=%llu busy=%d active=%d usage=%u%% running=%d",
+ __get_str(devname), __entry->cur_period, __entry->period_us,
+ __entry->vrate, __entry->busy_level, __entry->nr_active,
+ __entry->usage_pct, __entry->running
+ )
+);
+
TRACE_EVENT(iocost_iocg_forgive_debt,
TP_PROTO(struct ioc_gq *iocg, const char *path, struct ioc_now *now,
diff --git a/tools/testing/selftests/bpf/config b/tools/testing/selftests/bpf/config
index 2f79688dcf7ce..67a630cb56148 100644
--- a/tools/testing/selftests/bpf/config
+++ b/tools/testing/selftests/bpf/config
@@ -138,3 +138,5 @@ CONFIG_SMC_HS_CTRL_BPF=y
CONFIG_DIBS=y
CONFIG_DIBS_LO=y
CONFIG_PM_WAKELOCKS=y
+CONFIG_BLK_CGROUP_IOCOST=y
+CONFIG_BLK_CGROUP_IOCOST_BPF=y
diff --git a/tools/testing/selftests/bpf/prog_tests/iocost_model.c b/tools/testing/selftests/bpf/prog_tests/iocost_model.c
new file mode 100644
index 0000000000000..db0a74b1885be
--- /dev/null
+++ b/tools/testing/selftests/bpf/prog_tests/iocost_model.c
@@ -0,0 +1,200 @@
+// SPDX-License-Identifier: GPL-2.0
+#include <test_progs.h>
+#include <ctype.h>
+#include <fcntl.h>
+#include <unistd.h>
+#include "iocost_model.skel.h"
+#include "iocost_ms.skel.h"
+
+/*
+ * Write a line to io.cost.model with write(2) and return the errno of
+ * the failed write, or 0 on success. stdio is not used here on
+ * purpose: the kernel's rejection happens in the write() syscall,
+ * not in the userspace buffer copy, and every write, including
+ * the error paths of the callers below, is checked.
+ */
+static int write_cost_model(const char *buf)
+{
+ int fd, err = 0;
+ ssize_t n;
+
+ fd = open("/sys/fs/cgroup/io.cost.model", O_WRONLY);
+ if (fd < 0)
+ return -errno; /* negative: the file is not there */
+ n = write(fd, buf, strlen(buf));
+ if (n < 0)
+ err = errno;
+ close(fd);
+ return err;
+}
+
+/*
+ * Read back the io.cost.model line of dev and copy the model= value
+ * into @model. Returns 0 on success.
+ */
+static int readback_model(const char *dev, char *model, size_t model_sz)
+{
+ char line[256], word[256], *m, *end;
+ FILE *fp;
+ int found = 0;
+
+ fp = fopen("/sys/fs/cgroup/io.cost.model", "r");
+ if (!fp)
+ return -1;
+ while (fgets(line, sizeof(line), fp)) {
+ if (sscanf(line, "%255s", word) == 1 && !strcmp(word, dev)) {
+ found = 1;
+ break;
+ }
+ }
+ fclose(fp);
+ if (!found)
+ return -1;
+
+ m = strstr(line, "model=");
+ if (!m)
+ return -1;
+ m += strlen("model=");
+ end = m;
+ while (*end && !isspace(*end))
+ end++;
+ snprintf(model, model_sz, "%.*s", (int)(end - m), m);
+ return 0;
+}
+
+/*
+ * Bind the named model to a device with "model=<name>", verify the
+ * readback and restore the builtin model. Returns 0 on success.
+ */
+static int bind_model(const char *dev, const char *name)
+{
+ char buf[300], got[64];
+ int err;
+
+ snprintf(buf, sizeof(buf), "%s model=%s\n", dev, name);
+ err = write_cost_model(buf);
+ if (err)
+ return err > 0 ? -err : -EIO;
+ err = readback_model(dev, got, sizeof(got));
+ if (!err && !strcmp(got, name)) {
+ /* restore the builtin linear model on every path so the
+ * device is not left bound to a model the caller may
+ * unregister right after; the write is checked too */
+ snprintf(buf, sizeof(buf), "%s model=linear\n", dev);
+ err = write_cost_model(buf);
+ return err > 0 ? -err : (err ? -EIO : 0);
+ }
+
+ /* bind failed or does not read back: still restore; prefer
+ * the original failure, but surface the restore error too */
+ snprintf(buf, sizeof(buf), "%s model=linear\n", dev);
+ {
+ int rerr = write_cost_model(buf);
+
+ return err ? (err > 0 ? -err : -EIO)
+ : (rerr > 0 ? -rerr : -EIO);
+ }
+}
+
+/*
+ * True when the device line exists in io.cost.qos and iocost is
+ * enabled on it.
+ */
+static int dev_has_iocost(const char *dev)
+{
+ char line[256], word[256];
+ FILE *fp;
+ int found = 0;
+
+ fp = fopen("/sys/fs/cgroup/io.cost.qos", "r");
+ if (!fp)
+ return 0;
+ while (fgets(line, sizeof(line), fp)) {
+ if (sscanf(line, "%255s", word) == 1 && !strcmp(word, dev)) {
+ found = strstr(line, "enable=1") != NULL;
+ break;
+ }
+ }
+ fclose(fp);
+ return found;
+}
+
+/*
+ * Bind the 2x example model and verify the io.cost.model readback.
+ * IO accounting itself is not checked here; it needs a device doing
+ * real IO under iocost and is covered by the kernel-side validation
+ * described in the cover letter.
+ *
+ * Requires root, cgroup v2 and a device with iocost support. The
+ * device must be given as major:minor in $IOCOST_TEST_DEV, otherwise
+ * the test is skipped.
+ */
+void serial_test_iocost_model(void)
+{
+ struct iocost_model *skel;
+ char buf[300], *dev;
+ int err;
+
+ dev = getenv("IOCOST_TEST_DEV");
+ if (!dev || geteuid() != 0) {
+ test__skip();
+ return;
+ }
+ if (!dev_has_iocost(dev)) {
+ printf("skip: %s has no iocost enabled\n", dev);
+ test__skip();
+ return;
+ }
+
+ /*
+ * negative: binding an unknown model name must be rejected,
+ * so a typo cannot silently disable cost model updates
+ */
+ snprintf(buf, sizeof(buf), "%s model=no_such_model\n", dev);
+ err = write_cost_model(buf);
+ ASSERT_EQ(err > 0 ? err : 0, ENOENT, "unknown_model_rejected");
+
+ skel = iocost_model__open_and_load();
+ if (!ASSERT_OK_PTR(skel, "skel_open_load"))
+ return;
+
+ /* attaching the struct_ops registers the model by name */
+ err = iocost_model__attach(skel);
+ if (ASSERT_OK(err, "attach"))
+ ASSERT_OK(bind_model(dev, "iocost_2x"), "bind_and_readback");
+
+ iocost_model__destroy(skel);
+}
+
+/*
+ * Same check for the multi-stream example model. Only one model can
+ * be bound to a device at a time; both tests bind and restore, so
+ * they are serial and independent.
+ */
+void serial_test_iocost_model_streams(void)
+{
+ struct iocost_ms *skel;
+ char *dev;
+ int err;
+
+ dev = getenv("IOCOST_TEST_DEV");
+ if (!dev || geteuid() != 0) {
+ test__skip();
+ return;
+ }
+ if (!dev_has_iocost(dev)) {
+ printf("skip: %s has no iocost enabled\n", dev);
+ test__skip();
+ return;
+ }
+
+ skel = iocost_ms__open_and_load();
+ if (!ASSERT_OK_PTR(skel, "skel_open_load"))
+ return;
+
+ err = iocost_ms__attach(skel);
+ if (ASSERT_OK(err, "attach"))
+ ASSERT_OK(bind_model(dev, "iocost_ms"), "bind_and_readback");
+
+ iocost_ms__destroy(skel);
+}
diff --git a/tools/testing/selftests/bpf/progs/iocost_model.c b/tools/testing/selftests/bpf/progs/iocost_model.c
new file mode 100644
index 0000000000000..81928f897297c
--- /dev/null
+++ b/tools/testing/selftests/bpf/progs/iocost_model.c
@@ -0,0 +1,134 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * Example iocost cost model: the builtin linear HDD formula with all
+ * costs doubled.
+ *
+ * The constants mirror what calc_lcoefs() derives from the AUTOP_HDD
+ * defaults (rbps=174019176 rseqiops=41708 rrandiops=370, w-side
+ * analog) in vtime units where 1s == 2^37. On a rotational device
+ * still on ctrl=auto, a device bound to this model through
+ * io.cost.model charges twice the builtin model under the same
+ * workload, which makes it a convenient way to verify that
+ * accounting goes through the BPF path. On a non-rotational device,
+ * or one with user-pinned coefficients, the ratio to the builtin
+ * model is arbitrary.
+ *
+ * The cursor handling mirrors the builtin: a zero cursor means "no
+ * previous IO", and the cursor is only advanced for READ and WRITE
+ * bios the builtin prices, so flushes and discards leave it alone.
+ *
+ * The model implements the full linear formula itself, including
+ * flushes: there is no fallback to the builtin model, a dataless
+ * WRITE|REQ_PREFLUSH is priced as a one-page write.
+ */
+
+#include "vmlinux.h"
+#include <bpf/bpf_helpers.h>
+#include <bpf/bpf_tracing.h>
+
+/* VTIME_PER_SEC comes from vmlinux.h (a BTF enum constant) */
+#define IOC_PAGE_SIZE 4096
+#define IOC_SECT_TO_PAGE_SHIFT 3 /* 512B sectors to 4kB pages */
+#define LCOEF_RANDIO_PAGES 4096 /* 16MB seek threshold */
+#define IOCOST_COST_F_MERGE (1ULL << 0) /* not in BTF: a plain macro */
+#define IOCOST_REQ_OP_MASK 0xff /* REQ_OP_MASK, not in BTF */
+
+/*
+ * DIV64_U64_ROUND_UP / DIV_ROUND_UP_ULL equivalents, folded at
+ * compile time
+ */
+#define RU(x, y) ((x) / (y) + (((x) % (y)) ? 1 : 0))
+
+#define RBPS 174019176ULL
+#define RSEQIOPS 41708ULL
+#define RRANDIOPS 370ULL
+#define WBPS 178075866ULL
+#define WSEQIOPS 42705ULL
+#define WRANDIOPS 378ULL
+
+#define RPAGE (RU(VTIME_PER_SEC, RU(RBPS, IOC_PAGE_SIZE)))
+#define RSEQIO (RU(VTIME_PER_SEC, RSEQIOPS) - RPAGE)
+#define RRANDIO (RU(VTIME_PER_SEC, RRANDIOPS) - RPAGE)
+#define WPAGE (RU(VTIME_PER_SEC, RU(WBPS, IOC_PAGE_SIZE)))
+#define WSEQIO (RU(VTIME_PER_SEC, WSEQIOPS) - WPAGE)
+#define WRANDIO (RU(VTIME_PER_SEC, WRANDIOPS) - WPAGE)
+
+/*
+ * per-cgroup cursor storage: keyed by the cgroup, freed with it, so
+ * the model never leaks or reuses stale per-cgroup state
+ */
+struct {
+ __uint(type, BPF_MAP_TYPE_CGRP_STORAGE);
+ __uint(map_flags, BPF_F_NO_PREALLOC);
+ __type(key, int);
+ __type(value, __u64);
+} cursor_store SEC(".maps");
+
+SEC("struct_ops")
+u64 BPF_PROG(iocost_2x_calc_cost, u64 opf, u64 nbytes, u64 sector,
+ struct blkcg *blkcg, u64 model_flags)
+{
+ u64 pages, seek_pages = 0, base, coef_page, randio, cost;
+
+ /* builtin truncates: max(sectors >> IOC_SECT_TO_PAGE_SHIFT, 1) */
+ pages = nbytes >> IOC_PAGE_SHIFT;
+ if (!pages)
+ pages = 1;
+
+ if ((opf & IOCOST_REQ_OP_MASK) == REQ_OP_READ) {
+ base = RSEQIO; coef_page = RPAGE; randio = RRANDIO;
+ } else if ((opf & IOCOST_REQ_OP_MASK) == REQ_OP_WRITE) {
+ base = WSEQIO; coef_page = WPAGE; randio = WRANDIO;
+ } else {
+ /*
+ * a fully owning model must price every op; unknown
+ * ops are priced as a single page write
+ */
+ base = 0; coef_page = WPAGE; randio = 0;
+ }
+
+ /*
+ * mirror the builtin cursor semantics: seek distance is only
+ * computed against a non-zero cursor, and the cursor is only
+ * advanced for bios the builtin prices (READ/WRITE with a
+ * non-zero size), so flushes and discards leave it alone.
+ * Like bio_end_sector(), the advance truncates to whole
+ * sectors
+ */
+ {
+ __u64 *cursor, cur;
+ int priced = (opf & IOCOST_REQ_OP_MASK) == REQ_OP_READ ||
+ (opf & IOCOST_REQ_OP_MASK) == REQ_OP_WRITE;
+
+ cursor = bpf_cgrp_storage_get(&cursor_store,
+ blkcg->css.cgroup, NULL,
+ BPF_LOCAL_STORAGE_GET_F_CREATE);
+ if (!cursor)
+ return 2 * (base + pages * coef_page);
+ cur = *cursor;
+ if (cur && priced) {
+ seek_pages = sector > cur ? sector - cur
+ : cur - sector;
+ seek_pages >>= IOC_SECT_TO_PAGE_SHIFT;
+ if (seek_pages > LCOEF_RANDIO_PAGES)
+ base = randio;
+ }
+ if (priced && nbytes &&
+ !(model_flags & IOCOST_COST_F_MERGE))
+ *cursor = sector + (nbytes >> 9);
+ }
+
+ if (model_flags & IOCOST_COST_F_MERGE)
+ base = 0;
+
+ cost = 2 * (base + pages * coef_page);
+ return cost;
+}
+
+SEC(".struct_ops")
+struct iocost_model_ops iocost_2x = {
+ .calc_cost = (void *)iocost_2x_calc_cost,
+ .name = "iocost_2x",
+};
+
+char LICENSE[] SEC("license") = "GPL";
diff --git a/tools/testing/selftests/bpf/progs/iocost_ms.c b/tools/testing/selftests/bpf/progs/iocost_ms.c
new file mode 100644
index 0000000000000..2a3c1d6aae4df
--- /dev/null
+++ b/tools/testing/selftests/bpf/progs/iocost_ms.c
@@ -0,0 +1,156 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * Example multi-stream sequentiality detection cost model.
+ *
+ * The builtin model keeps a single cursor per cgroup, so two
+ * interleaved sequential readers in one cgroup are all priced random
+ * (measured 89x overcharge, 12.9x throughput collapse), while random
+ * IO inside a hot window smaller than the 16MB seek threshold is
+ * priced sequential (measured 107x undercharge). This model replaces
+ * the single cursor with a per-cgroup table of stream slots: an IO is
+ * sequential iff its sector matches the expected next sector of any
+ * tracked stream. Interleaved streams keep their own slots, and
+ * windowed random IO rarely matches a moving expectation.
+ *
+ * Stream state lives in a CGRP_STORAGE map, so it is created and
+ * freed with the cgroup. The model implements the full builtin
+ * linear formula itself, including flush pricing.
+ */
+#include "vmlinux.h"
+#include <bpf/bpf_helpers.h>
+#include <bpf/bpf_tracing.h>
+
+/* VTIME_PER_SEC comes from vmlinux.h (a BTF enum constant) */
+#define IOC_PAGE_SIZE 4096
+#define IOC_SECT_TO_PAGE_SHIFT 3 /* 512B sectors to 4kB pages */
+#define IOCOST_COST_F_MERGE (1ULL << 0) /* not in BTF: a plain macro */
+#define IOCOST_REQ_OP_MASK 0xff /* REQ_OP_MASK, not in BTF */
+
+/*
+ * DIV64_U64_ROUND_UP / DIV_ROUND_UP_ULL equivalents, folded at
+ * compile time
+ */
+#define RU(x, y) ((x) / (y) + (((x) % (y)) ? 1 : 0))
+
+#define RBPS 174019176ULL
+#define RSEQIOPS 41708ULL
+#define RRANDIOPS 370ULL
+#define WBPS 178075866ULL
+#define WSEQIOPS 42705ULL
+#define WRANDIOPS 378ULL
+
+#define RPAGE (RU(VTIME_PER_SEC, RU(RBPS, IOC_PAGE_SIZE)))
+#define RSEQIO (RU(VTIME_PER_SEC, RSEQIOPS) - RPAGE)
+#define RRANDIO (RU(VTIME_PER_SEC, RRANDIOPS) - RPAGE)
+#define WPAGE (RU(VTIME_PER_SEC, RU(WBPS, IOC_PAGE_SIZE)))
+#define WSEQIO (RU(VTIME_PER_SEC, WSEQIOPS) - WPAGE)
+#define WRANDIO (RU(VTIME_PER_SEC, WRANDIOPS) - WPAGE)
+
+#define NSLOTS 4
+
+struct streams {
+ __u64 expected[NSLOTS]; /* next expected sector, per stream */
+ __u64 stamp[NSLOTS]; /* LRU stamp, 0 = empty */
+};
+
+/*
+ * per-cgroup stream table: keyed by the cgroup, freed with it
+ */
+struct {
+ __uint(type, BPF_MAP_TYPE_CGRP_STORAGE);
+ __uint(map_flags, BPF_F_NO_PREALLOC);
+ __type(key, int);
+ __type(value, struct streams);
+} stream_tab SEC(".maps");
+
+SEC("struct_ops")
+u64 BPF_PROG(iocost_ms_calc_cost, u64 opf, u64 nbytes, u64 sector,
+ struct blkcg *blkcg, u64 model_flags)
+{
+ struct streams *s;
+ u64 pages, base, coef_page, randio, advance, now;
+ u32 i, victim = 0, found = 0xFFFFFFFF;
+
+ if ((opf & IOCOST_REQ_OP_MASK) == REQ_OP_READ) {
+ base = RSEQIO; coef_page = RPAGE; randio = RRANDIO;
+ } else if ((opf & IOCOST_REQ_OP_MASK) == REQ_OP_WRITE) {
+ base = WSEQIO; coef_page = WPAGE; randio = WRANDIO;
+ } else {
+ /*
+ * a fully owning model must price every op; unknown
+ * ops are priced as per-page writes
+ */
+ base = 0; coef_page = WPAGE; randio = 0;
+ }
+ advance = nbytes >> 9; /* whole sectors, like bio_end_sector() */
+
+ /* only bios the builtin prices participate in stream tracking */
+ if (!(((opf & IOCOST_REQ_OP_MASK) == REQ_OP_READ ||
+ (opf & IOCOST_REQ_OP_MASK) == REQ_OP_WRITE) && nbytes)) {
+ pages = nbytes >> IOC_PAGE_SHIFT;
+ if (!pages)
+ pages = 1;
+ return base + pages * coef_page;
+ }
+
+ s = bpf_cgrp_storage_get(&stream_tab, blkcg->css.cgroup, NULL,
+ BPF_LOCAL_STORAGE_GET_F_CREATE);
+ if (!s) {
+ /* no storage: price per page, truncating like the builtin */
+ pages = nbytes >> IOC_PAGE_SHIFT;
+ if (!pages)
+ pages = 1;
+ return base + pages * coef_page;
+ }
+
+ /*
+ * Slot access is lockless, mirroring the builtin single-cursor
+ * update in ioc_rqos_throttle(): concurrent CPUs submitting for
+ * the same cgroup can race on slot updates; mispricing is
+ * bounded and acceptable for an example model.
+ */
+ now = bpf_ktime_get_ns();
+ for (i = 0; i < NSLOTS; i++) {
+ if (s->expected[i] == sector && s->stamp[i]) {
+ found = i;
+ break;
+ }
+ }
+ if (found != 0xFFFFFFFF) {
+ /* sequential: keep the seq base from the op branch */
+ s->expected[found] = sector + advance;
+ s->stamp[found] = now;
+ } else {
+ base = randio;
+ for (i = 1; i < NSLOTS; i++) {
+ if (s->stamp[i] < s->stamp[victim])
+ victim = i;
+ }
+ s->expected[victim] = sector + advance;
+ s->stamp[victim] = now;
+ }
+
+ /* builtin truncates: max(sectors >> IOC_SECT_TO_PAGE_SHIFT, 1) */
+ pages = nbytes >> IOC_PAGE_SHIFT;
+ if (!pages)
+ pages = 1;
+ if (model_flags & IOCOST_COST_F_MERGE) {
+ /*
+ * merged bios skip the base cost but still advance
+ * the stream position above, so a merge at the
+ * expected sector does not make the following new IO
+ * look random
+ */
+ base = 0;
+ }
+
+ return base + pages * coef_page;
+}
+
+SEC(".struct_ops")
+struct iocost_model_ops iocost_ms = {
+ .calc_cost = (void *)iocost_ms_calc_cost,
+ .name = "iocost_ms",
+};
+
+char LICENSE[] SEC("license") = "GPL";
|
| 65/3 | 2026/09/14 08:17 | llm | kmsan-evaluator |
2mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 19460 output: 22 thoughts: 1805 Thoughts: Okay, let's break this down. My primary task here is to determine if this patch series necessitates a dedicated KMSAN fuzzing session. The key is distinguishing when KMSAN is *actually* needed versus when standard KASAN or other existing checks will suffice. We *really* don't want to spin up a KMSAN session unless absolutely necessary, given the resource overhead. First, I need to remind myself that standard KASAN already covers a wide array of memory safety issues: out-of-bounds access, use-after-free, double frees, invalid frees, lockdep issues, and various other memory corruption scenarios. The niche KMSAN fills is detecting reads from *uninitialized* memory, specifically on the stack, the heap, and during page allocations, and also kernel-to-user memory info-leaks. So, the rule is clear: If a bug introduced by this code change is likely to be caught by existing sanitizers, *no KMSAN*. KMSAN is for the cases they *miss*. Now, the patch itself. I see a bunch of changes related to BPF integration for iocost, specifically within block I/O accounting. This seems to be adding BPF struct_ops, and the ability to define iocost models using BPF programs. The file changes are: * Documentation/admin-guide/cgroup-v2.rst: Documentation update. Not relevant. * block/Kconfig and block/Makefile: Configuration and build system changes to enable the new BPF functionality. Not relevant. * block/blk-cgroup.c: Calls to online/offline iocost BPF, which is normal for hotplug behavior. * block/blk-iocost-bpf.c: This is the meat of the patch: a new file with the BPF struct_ops implementation. It handles BPF model registration, initialization, validation, and de-registration. This looks like the place to focus my attention. It contains kzalloc calls for `struct iocost_bpf_model` which implies memory allocation. * block/blk-iocost.c: Changes in this file incorporate the BPF model into the iocost framework, including the addition of a `const struct iocost_model_ops __rcu *model;` member to the `struct ioc`. This is where the iocost model is now selected/switched. The BPF dispatch happens here in `calc_vtime_cost()` and `calc_size_vtime_cost()`. There are also changes to incorporate tracing. * include/linux/blk-iocost.h and include/trace/events/iocost.h: Header file and trace event. Not relevant. * tools/testing/selftests/bpf/...: Selftests are good but not critical for this analysis. My concern centers on the newly added BPF integration (`block/blk-iocost-bpf.c`). Specifically, I am interested in how the BPF programs interact with kernel memory and how the `struct iocost_model_ops` is handled. **Uninitialized memory is my primary concern, so I must examine the details of the initialization and data flow within the BPF structure** I'm now zeroing in on `bpf_iocost_init_member` function within `block/blk-iocost-bpf.c`. The code uses `offsetof()` with `struct iocost_model_ops`. It looks like it copies the `name` field from the user-space struct to the kernel struct. But, how is the struct actually initialized? What happens if `name` is not correctly initialized by the user? Also, the struct seems to be filled from udata to kdata and there are some checks done here. Is there potential for leaking uninitialized kernel memory to user space through these BPF programs? I need to understand how the `bpf_struct_ops` is generally used across the kernel. I need to look at other examples to get a better understanding of how the struct members are handled, and how memory is allocated and copied in the initialization flow. This requires me to understand the *full* path from user-provided BPF code, through the verifier, and into the kernel data structures. Is there any way for a malformed BPF program to read or write uninitialized memory or expose kernel memory to user-space? **Based on these initial observations, I'm leaning toward needing KMSAN. The BPF integration introduces a new attack surface, and the initialization of the `iocost_model_ops` struct *could* lead to uninitialized memory being read. However, I need to do further investigation into existing `bpf_struct_ops` usage in the kernel to ascertain how the members are initialized before a final decision is |
| 66/3 | 2026/09/14 08:20 | tool | grepper |
0mArgs:{
"Expression": "bpf_obj_name_cpy"
}Results: {
"Output": "block/blk-iocost-bpf.c=142=static int bpf_iocost_init_member(const struct btf_type *t,\n--\nblock/blk-iocost-bpf.c-151-\tcase offsetof(struct iocost_model_ops, name):\nblock/blk-iocost-bpf.c:152:\t\tif (bpf_obj_name_cpy(ops-\u003ename, uops-\u003ename,\nblock/blk-iocost-bpf.c-153-\t\t\t\t sizeof(ops-\u003ename)) \u003c= 0)\n--\nfs/binfmt_misc_bpf.c=344=static int bm_bpf_init_member(const struct btf_type *t,\n--\nfs/binfmt_misc_bpf.c-353-\tcase offsetof(struct binfmt_misc_ops, name):\nfs/binfmt_misc_bpf.c:354:\t\tif (bpf_obj_name_cpy(ops-\u003ename, uops-\u003ename,\nfs/binfmt_misc_bpf.c-355-\t\t\t\t sizeof(ops-\u003ename)) \u003c= 0)\n--\ninclude/linux/bpf.h=655=struct bpf_map *bpf_prog_arena(struct bpf_prog *prog);\ninclude/linux/bpf.h:656:int bpf_obj_name_cpy(char *dst, const char *src, unsigned int size);\ninclude/linux/bpf.h-657-\n--\nkernel/bpf/syscall.c=1203=int bpf_get_file_flag(int flags)\n--\nkernel/bpf/syscall.c-1224- */\nkernel/bpf/syscall.c:1225:int bpf_obj_name_cpy(char *dst, const char *src, unsigned int size)\nkernel/bpf/syscall.c-1226-{\n--\nkernel/bpf/syscall.c-1244-}\nkernel/bpf/syscall.c:1245:EXPORT_SYMBOL_GPL(bpf_obj_name_cpy);\nkernel/bpf/syscall.c-1246-\n--\nkernel/bpf/syscall.c=1382=static int map_create_alloc(union bpf_attr *attr, bpfptr_t uattr, struct bpf_verifier_log *log,\n--\nkernel/bpf/syscall.c-1541-\nkernel/bpf/syscall.c:1542:\terr = bpf_obj_name_cpy(map-\u003ename, attr-\u003emap_name,\nkernel/bpf/syscall.c-1543-\t\t\t sizeof(attr-\u003emap_name));\n--\nkernel/bpf/syscall.c=2917=static int bpf_prog_load(union bpf_attr *attr, bpfptr_t uattr, struct bpf_log_attr *attr_log)\n--\nkernel/bpf/syscall.c-3126-\tprog-\u003eaux-\u003eload_time = ktime_get_boottime_ns();\nkernel/bpf/syscall.c:3127:\terr = bpf_obj_name_cpy(prog-\u003eaux-\u003ename, attr-\u003eprog_name,\nkernel/bpf/syscall.c-3128-\t\t\t sizeof(attr-\u003eprog_name));\n--\nkernel/sched/ext/ext.c=8015=static int bpf_scx_init_member(const struct btf_type *t,\n--\nkernel/sched/ext/ext.c-8035-\tcase offsetof(struct sched_ext_ops, name):\nkernel/sched/ext/ext.c:8036:\t\tret = bpf_obj_name_cpy(ops-\u003ename, uops-\u003ename,\nkernel/sched/ext/ext.c-8037-\t\t\t\t sizeof(ops-\u003ename));\n--\nnet/ipv4/bpf_tcp_ca.c=211=static int bpf_tcp_ca_init_member(const struct btf_type *t,\n--\nnet/ipv4/bpf_tcp_ca.c-229-\tcase offsetof(struct tcp_congestion_ops, name):\nnet/ipv4/bpf_tcp_ca.c:230:\t\tif (bpf_obj_name_cpy(tcp_ca-\u003ename, utcp_ca-\u003ename,\nnet/ipv4/bpf_tcp_ca.c-231-\t\t\t\t sizeof(tcp_ca-\u003ename)) \u003c= 0)\n--\nnet/sched/bpf_qdisc.c=361=static int bpf_qdisc_init_member(const struct btf_type *t,\n--\nnet/sched/bpf_qdisc.c-382-\tcase offsetof(struct Qdisc_ops, id):\nnet/sched/bpf_qdisc.c:383:\t\tif (bpf_obj_name_cpy(qdisc_ops-\u003eid, uqdisc_ops-\u003eid,\nnet/sched/bpf_qdisc.c-384-\t\t\t\t sizeof(qdisc_ops-\u003eid)) \u003c= 0)\n--\nnet/smc/smc_hs_bpf.c=85=static int smc_bpf_hs_ctrl_init_member(const struct btf_type *t,\n--\nnet/smc/smc_hs_bpf.c-98-\tcase offsetof(struct smc_hs_ctrl, name):\nnet/smc/smc_hs_bpf.c:99:\t\tif (bpf_obj_name_cpy(k_ctrl-\u003ename, u_ctrl-\u003ename,\nnet/smc/smc_hs_bpf.c-100-\t\t\t\t sizeof(u_ctrl-\u003ename)) \u003c= 0)\n"
} |
| 67/3 | 2026/09/14 08:20 | llm | kmsan-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 23736 output: 37 thoughts: 37 Thoughts: Okay, here's my take on those thoughts, framed as my own: **Investigating `bpf_obj_name_cpy`** Right, let's dive into this. The initial thought is to examine the `bpf_obj_name_cpy` function. My instinct is to head straight to the source, so I'm thinking I should pull up `kernel/bpf/syscall.c` and zero in on around line 1225. That's usually where the relevant system call handling code lives, and it's a good starting point to understand how this copy operation works within the broader context of BPF object management. I'm assuming that function handles the copy operation of the name of a BPF object. Reading this will reveal the underlying implementation details, like memory handling, error checking, and interaction with other BPF data structures. This is where the magic happens, or perhaps where subtle bugs hide. |
| 68/3 | 2026/09/14 08:20 | tool | read-file |
0mArgs:{
"File": "kernel/bpf/syscall.c",
"FirstLine": 1224,
"LineCount": 25
}Results: {
"Contents": "1224:\t */\n1225:\tint bpf_obj_name_cpy(char *dst, const char *src, unsigned int size)\n1226:\t{\n1227:\t\tconst char *end = src + size;\n1228:\t\tconst char *orig_src = src;\n1229:\t\n1230:\t\tmemset(dst, 0, size);\n1231:\t\t/* Copy all isalnum(), '_' and '.' chars. */\n1232:\t\twhile (src \u003c end \u0026\u0026 *src) {\n1233:\t\t\tif (!isalnum(*src) \u0026\u0026\n1234:\t\t\t *src != '_' \u0026\u0026 *src != '.')\n1235:\t\t\t\treturn -EINVAL;\n1236:\t\t\t*dst++ = *src++;\n1237:\t\t}\n1238:\t\n1239:\t\t/* No '\\0' found in \"size\" number of bytes */\n1240:\t\tif (src == end)\n1241:\t\t\treturn -EINVAL;\n1242:\t\n1243:\t\treturn src - orig_src;\n1244:\t}\n1245:\tEXPORT_SYMBOL_GPL(bpf_obj_name_cpy);\n1246:\t\n1247:\tint map_check_no_btf(struct bpf_map *map,\n1248:\t\t\t const struct btf *btf,\n"
} |
| 69/3 | 2026/09/14 08:20 | llm | kmsan-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 24378 output: 36 thoughts: 384 Thoughts: **Analyzing `iocost_cost_model_prfill` for Potential Issues** Okay, so I'm looking at this `bpf_obj_name_cpy` function and it's doing a `memset` and then ensuring null termination, which is standard practice for memory safety when copying names. Good. Now, let's dive into this `iocost_cost_model_prfill` function. I see a `seq_printf` call in there, and I want to understand what's happening. I see that `ioc_cost_model_prfill` is conditionally compiled based on `CONFIG_BLK_CGROUP_IOCOST_BPF`. When the BPF feature is enabled, there is this block of code that uses some variables such as `dname`, `model`, and `u`, to print some data. `u` is an array of 64-bit unsigned ints. I need to understand how these values are populated in the BPF enabled scenario. My next step will be to explore how this `ioc_cost_model_prfill` was implemented before. This will help me understand the purpose of the BPF implementation and identify potential areas to look into. |
| 70/3 | 2026/09/14 08:20 | tool | grepper |
0mArgs:{
"Expression": "ioc_cost_model_prfill",
"PathPrefix": "block/blk-iocost.c"
}Results: {
"Output": "block/blk-iocost.c=3361=static ssize_t ioc_qos_write(struct kernfs_open_file *of, char *input,\n--\nblock/blk-iocost.c-3517-\nblock/blk-iocost.c:3518:static u64 ioc_cost_model_prfill(struct seq_file *sf,\nblock/blk-iocost.c-3519-\t\t\t\t struct blkg_policy_data *pd, int off)\n--\nblock/blk-iocost.c=3557=static int ioc_cost_model_show(struct seq_file *sf, void *v)\n--\nblock/blk-iocost.c-3560-\nblock/blk-iocost.c:3561:\tblkcg_print_blkgs(sf, blkcg, ioc_cost_model_prfill,\nblock/blk-iocost.c-3562-\t\t\t \u0026blkcg_policy_iocost, seq_cft(sf)-\u003eprivate, false);\n"
} |
| 71/3 | 2026/09/14 08:20 | llm | kmsan-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 25318 output: 37 thoughts: 37 Thoughts: Here's a summary of my thought process, as requested: **Diving into the IOCost Model Prfill Function** Okay, time to get my hands dirty with `block/blk-iocost.c`. Let's zero in on the `ioc_cost_model_prfill` function – lines 3518 through 3545, specifically. This is where the magic (or the potential bottleneck) of the I/O cost model related to prefilling is handled. I need to refresh my understanding of how prefill contributes to the overall cost calculation, especially in the context of the newer developments. I'm assuming there's been some recent refactoring around this area, so this targeted examination should illuminate any recent changes in the logic and how they affect the model. I'm expecting to see how the prefill cost is weighted and how it interacts with the other components in the model such as read and write costs. I’ll be looking for any clues regarding optimizations or adjustments made to better capture real-world I/O behavior during prefill operations. Is the data sampling done correctly? What are the potential areas for improvement? I'll probably be making notes on any potential assumptions made by the model, as I tend to find holes in there quite quickly. |
| 72/3 | 2026/09/14 08:20 | tool | read-file |
0mArgs:{
"File": "block/blk-iocost.c",
"FirstLine": 3515,
"LineCount": 40
}Results: {
"Contents": "3515:\t\tgoto close_bdev;\n3516:\t}\n3517:\t\n3518:\tstatic u64 ioc_cost_model_prfill(struct seq_file *sf,\n3519:\t\t\t\t\t struct blkg_policy_data *pd, int off)\n3520:\t{\n3521:\t\tconst char *dname = blkg_dev_name(pd-\u003eblkg);\n3522:\t\tstruct ioc *ioc = pd_to_iocg(pd)-\u003eioc;\n3523:\t\tu64 *u = ioc-\u003eparams.i_lcoefs;\n3524:\t\n3525:\t\tif (!dname)\n3526:\t\t\treturn 0;\n3527:\t\n3528:\t\tspin_lock_irq(\u0026ioc-\u003elock);\n3529:\t#ifdef CONFIG_BLK_CGROUP_IOCOST_BPF\n3530:\t\t{\n3531:\t\t\tconst struct iocost_model_ops *model =\n3532:\t\t\t\trcu_dereference_protected(ioc-\u003emodel,\n3533:\t\t\t\t\t\tlockdep_is_held(\u0026ioc-\u003elock));\n3534:\t\n3535:\t\t\tseq_printf(sf, \"%s ctrl=%s model=%s \"\n3536:\t\t\t\t \"rbps=%llu rseqiops=%llu rrandiops=%llu \"\n3537:\t\t\t\t \"wbps=%llu wseqiops=%llu wrandiops=%llu\\n\",\n3538:\t\t\t\t dname, model ? \"bpf\" :\n3539:\t\t\t\t\t ioc-\u003euser_cost_model ? \"user\" : \"auto\",\n3540:\t\t\t\t model ? model-\u003ename : \"linear\",\n3541:\t\t\t\t u[I_LCOEF_RBPS], u[I_LCOEF_RSEQIOPS],\n3542:\t\t\t\t u[I_LCOEF_RRANDIOPS], u[I_LCOEF_WBPS],\n3543:\t\t\t\t u[I_LCOEF_WSEQIOPS], u[I_LCOEF_WRANDIOPS]);\n3544:\t\t}\n3545:\t#else\n3546:\t\tseq_printf(sf, \"%s ctrl=%s model=linear \"\n3547:\t\t\t \"rbps=%llu rseqiops=%llu rrandiops=%llu \"\n3548:\t\t\t \"wbps=%llu wseqiops=%llu wrandiops=%llu\\n\",\n3549:\t\t\t dname, ioc-\u003euser_cost_model ? \"user\" : \"auto\",\n3550:\t\t\t u[I_LCOEF_RBPS], u[I_LCOEF_RSEQIOPS], u[I_LCOEF_RRANDIOPS],\n3551:\t\t\t u[I_LCOEF_WBPS], u[I_LCOEF_WSEQIOPS], u[I_LCOEF_WRANDIOPS]);\n3552:\t#endif\n3553:\t\tspin_unlock_irq(\u0026ioc-\u003elock);\n3554:\t\treturn 0;\n"
} |
| 73/3 | 2026/09/14 08:20 | llm | kmsan-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 26468 output: 38 thoughts: 40 Thoughts: Okay, let's dive into this. Here's how I'd approach this task, as if these were my internal thoughts: **Analyzing the `ioc_cost_model_write` Function** Right, let's take a look at `ioc_cost_model_write`. That sounds like it's where the core logic resides for writing, or at least setting up the cost model. I'll need to understand how the I/O cost modeling interacts with write operations. I wonder if it's applying a specific function for writes, or maybe just using a more generic approach to cost calculation that also applies to writes. Let's see... and let's go straight to the heart of the matter. I'm going to jump into lines 3630-3750 in `block/blk-iocost.c` – that's usually where the meat of a function like this lives, the critical calculations and assignments. I'll be looking for how the write specifics are handled, any conditional branches that distinguish write operations, and the inputs and outputs of this cost model. My focus is on the algorithmic mechanics, and how the model incorporates write operations into the overall performance characterization. |
| 74/3 | 2026/09/14 08:21 | tool | read-file |
0mArgs:{
"File": "block/blk-iocost.c",
"FirstLine": 3630,
"LineCount": 100
}Results: {
"Contents": "3630:\t{\n3631:\t\tstruct blkg_conf_ctx ctx;\n3632:\t\tstruct request_queue *q;\n3633:\t\tunsigned int memflags;\n3634:\t\tstruct ioc *ioc;\n3635:\t\tu64 u[NR_I_LCOEFS];\n3636:\t\tbool user;\n3637:\t\tchar bpf_model[IOCOST_MODEL_NAME_LEN];\n3638:\t\tchar *body, *p;\n3639:\t\tint ret;\n3640:\t\n3641:\t\tblkg_conf_init(\u0026ctx, input);\n3642:\t\n3643:\t\tret = blkg_conf_open_bdev(\u0026ctx);\n3644:\t\tif (ret)\n3645:\t\t\treturn ret;\n3646:\t\n3647:\t\tbody = ctx.body;\n3648:\t\tq = bdev_get_queue(ctx.bdev);\n3649:\t\tif (!queue_is_mq(q)) {\n3650:\t\t\tret = -EOPNOTSUPP;\n3651:\t\t\tgoto close_bdev;\n3652:\t\t}\n3653:\t\n3654:\t\tioc = q_to_ioc(q);\n3655:\t\tif (!ioc) {\n3656:\t\t\tret = blk_iocost_init(ctx.bdev-\u003ebd_disk);\n3657:\t\t\tif (ret)\n3658:\t\t\t\tgoto close_bdev;\n3659:\t\t\tioc = q_to_ioc(q);\n3660:\t\t}\n3661:\t\n3662:\t\tmemflags = blk_mq_freeze_queue(q);\n3663:\t\tblk_mq_quiesce_queue(q);\n3664:\t\n3665:\t\tspin_lock_irq(\u0026ioc-\u003elock);\n3666:\t\tmemcpy(u, ioc-\u003eparams.i_lcoefs, sizeof(u));\n3667:\t\tuser = ioc-\u003euser_cost_model;\n3668:\t\tbpf_model[0] = '\\0';\n3669:\t\n3670:\t\tret = -EINVAL;\n3671:\t\n3672:\t\twhile ((p = strsep(\u0026body, \" \\t\\n\"))) {\n3673:\t\t\tsubstring_t args[MAX_OPT_ARGS];\n3674:\t\t\tchar buf[32];\n3675:\t\t\tint tok;\n3676:\t\t\tu64 v;\n3677:\t\n3678:\t\t\tif (!*p)\n3679:\t\t\t\tcontinue;\n3680:\t\n3681:\t\t\tswitch (match_token(p, cost_ctrl_tokens, args)) {\n3682:\t\t\tcase COST_CTRL:\n3683:\t\t\t\tmatch_strlcpy(buf, \u0026args[0], sizeof(buf));\n3684:\t\t\t\tif (!strcmp(buf, \"auto\"))\n3685:\t\t\t\t\tuser = false;\n3686:\t\t\t\telse if (!strcmp(buf, \"user\"))\n3687:\t\t\t\t\tuser = true;\n3688:\t\t\t\telse\n3689:\t\t\t\t\tgoto unlock;\n3690:\t\t\t\tbpf_model[0] = '\\0';\n3691:\t\t\t\tcontinue;\n3692:\t\t\tcase COST_MODEL:\n3693:\t\t\t\tmatch_strlcpy(buf, \u0026args[0], sizeof(buf));\n3694:\t\t\t\tif (!strcmp(buf, \"linear\")) {\n3695:\t\t\t\t\t/* back to the builtin linear model */\n3696:\t\t\t\t\tbpf_model[0] = '\\0';\n3697:\t\t\t\t\tcontinue;\n3698:\t\t\t\t}\n3699:\t\t\t\tmatch_strlcpy(bpf_model, \u0026args[0], sizeof(bpf_model));\n3700:\t\t\t\tcontinue;\n3701:\t\t\t}\n3702:\t\n3703:\t\t\ttok = match_token(p, i_lcoef_tokens, args);\n3704:\t\t\tif (tok == NR_I_LCOEFS)\n3705:\t\t\t\tgoto unlock;\n3706:\t\t\tif (match_u64(\u0026args[0], \u0026v))\n3707:\t\t\t\tgoto unlock;\n3708:\t\t\tu[tok] = v;\n3709:\t\t\tuser = true;\n3710:\t\t}\n3711:\t\n3712:\t\tret = 0;\n3713:\t\n3714:\tunlock:\n3715:\t\tspin_unlock_irq(\u0026ioc-\u003elock);\n3716:\t\n3717:\t\t/*\n3718:\t\t * Resolve the model name outside ioc-\u003elock (the registry lookup\n3719:\t\t * takes a mutex), so a bad name rejects the whole write before\n3720:\t\t * anything is applied. The resolved model is this write's own\n3721:\t\t * state, not per-device state, so concurrent writers cannot\n3722:\t\t * clobber each other's staged model. On success, re-take the\n3723:\t\t * lock to apply the coefficients and swap the model in one go.\n3724:\t\t */\n3725:\t\tif (!ret) {\n3726:\t\t\tconst struct iocost_model_ops *new, *old;\n3727:\t\n3728:\t\t\tnew = ioc_bpf_model_prepare(bpf_model);\n3729:\t\t\tif (IS_ERR(new)) {\n"
} |
| 75/3 | 2026/09/14 08:21 | llm | kmsan-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 28234 output: 37 thoughts: 27 Thoughts: Okay, here's my attempt at summarizing those lines as if they were my thoughts, aimed at a fellow expert: **Diving Back into `blk-iocost.c`** Alright, time to get back to it. Let's see... picking up at lines 3730-3770 in `block/blk-iocost.c`. Right, I remember where we left off. I'm focusing on this particular section because... *[Here, I'd likely tailor the rest based on my actual expertise and current focus. Examples might include:]* ...we were trying to nail down how the costing model handles [specific workload type or resource], or perhaps... ...I'm trying to understand the interaction between [specific data structure or function] and the overall cost calculation. It's crucial for understanding the performance implications of [a specific feature or optimization]. This section is particularly interesting because [reason related to the code's complexity or relevance]. I need to refresh my memory on... oh, here's where it's all defined. |
| 76/3 | 2026/09/14 08:21 | tool | read-file |
0mArgs:{
"File": "block/blk-iocost.c",
"FirstLine": 3730,
"LineCount": 40
}Results: {
"Contents": "3730:\t\t\t\tret = PTR_ERR(new);\n3731:\t\t\t} else {\n3732:\t\t\t\tspin_lock_irq(\u0026ioc-\u003elock);\n3733:\t\t\t\tif (user) {\n3734:\t\t\t\t\tmemcpy(ioc-\u003eparams.i_lcoefs, u, sizeof(u));\n3735:\t\t\t\t\tioc-\u003euser_cost_model = true;\n3736:\t\t\t\t} else {\n3737:\t\t\t\t\tioc-\u003euser_cost_model = false;\n3738:\t\t\t\t}\n3739:\t\t\t\tioc_refresh_params(ioc, true);\n3740:\t\t\t\told = ioc_bpf_model_commit(ioc, new);\n3741:\t\t\t\tspin_unlock_irq(\u0026ioc-\u003elock);\n3742:\t\t\t\tif (IS_ENABLED(CONFIG_BLK_CGROUP_IOCOST_BPF) \u0026\u0026 old)\n3743:\t\t\t\t\tiocost_bpf_model_put(old);\n3744:\t\t\t}\n3745:\t\t}\n3746:\t\n3747:\t\tblk_mq_unquiesce_queue(q);\n3748:\t\tblk_mq_unfreeze_queue(q, memflags);\n3749:\t\n3750:\tclose_bdev:\n3751:\t\tblkg_conf_close_bdev(\u0026ctx);\n3752:\t\treturn ret ?: nbytes;\n3753:\t}\n3754:\t\n3755:\tstatic struct cftype ioc_files[] = {\n3756:\t\t{\n3757:\t\t\t.name = \"weight\",\n3758:\t\t\t.flags = CFTYPE_NOT_ON_ROOT,\n3759:\t\t\t.seq_show = ioc_weight_show,\n3760:\t\t\t.write = ioc_weight_write,\n3761:\t\t},\n3762:\t\t{\n3763:\t\t\t.name = \"cost.qos\",\n3764:\t\t\t.flags = CFTYPE_ONLY_ON_ROOT,\n3765:\t\t\t.seq_show = ioc_qos_show,\n3766:\t\t\t.write = ioc_qos_write,\n3767:\t\t},\n3768:\t\t{\n3769:\t\t\t.name = \"cost.model\",\n"
} |
| 77/3 | 2026/09/14 08:21 | llm | kmsan-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 29116 output: 37 thoughts: 227 Thoughts: **Analyzing BPF Model Buffer Handling and Timer Function** Okay, let's dive into this code. First, I want to confirm the safety of the `bpf_model` buffer handling. I see the declaration: `char bpf_model[IOCOST_MODEL_NAME_LEN];` and the initialization `bpf_model[0] = '\0';` – good, ensuring a clean slate. Then, within `match_strlcpy`, the code copies the input string into `bpf_model`: `match_strlcpy(bpf_model, &args[0], sizeof(bpf_model));`. A quick mental check confirms that `match_strlcpy` is specifically designed to guarantee NUL termination after the copy. Excellent. Now, the `ioc_bpf_model_prepare(bpf_model)` function is called. The check `if (!name[0]) return NULL;` handles the case of an empty string. The following `iocost_bpf_model_get(name, &new);` looks safe so far. The key string comparison happens inside `iocost_bpf_model_get`: `!strcmp(m->ops->name, name)`. Given that the source string is from the BPF model and the destination is the `bpf_model` copy, and all operations in between are null-terminated, the string operations look to be safely handled. No obvious vulnerabilities jump out at this point. Next, I need to shift my focus to `trace_iocost_ioc_tick` within `ioc_timer_fn`. I'll want to check the code from lines 2440 to 2500 in `block/blk-iocost.c`. Time to see what's happening there with the timer function. |
| 78/3 | 2026/09/14 08:21 | tool | read-file |
0mArgs:{
"File": "block/blk-iocost.c",
"FirstLine": 2440,
"LineCount": 70
}Results: {
"Contents": "2440:\t\t} else {\n2441:\t\t\t/* inside the hysterisis margin, we're good */\n2442:\t\t\tioc-\u003ebusy_level = 0;\n2443:\t\t}\n2444:\t\n2445:\t\tioc-\u003ebusy_level = clamp(ioc-\u003ebusy_level, -1000, 1000);\n2446:\t\n2447:\t\t/*\n2448:\t\t * vrate and period_us change right below; snapshot the values\n2449:\t\t * this period ran in so the tick below reports the period's own\n2450:\t\t * parameters instead of the next period's.\n2451:\t\t */\n2452:\t\tu32 tick_period_us = ioc-\u003eperiod_us;\n2453:\t\tu64 tick_vrate = ioc-\u003evtime_base_rate;\n2454:\t\n2455:\t\tioc_adjust_base_vrate(ioc, rq_wait_pct, nr_lagging, nr_shortages,\n2456:\t\t\t\t prev_busy_level, missed_ppm);\n2457:\t\n2458:\t\tioc_refresh_params(ioc, false);\n2459:\t\n2460:\t\tioc_forgive_debts(ioc, usage_us_sum, nr_debtors, \u0026now);\n2461:\t\n2462:\t\t/*\n2463:\t\t * This period is done. Move onto the next one. If nothing's\n2464:\t\t * going on with the device, stop the timer.\n2465:\t\t */\n2466:\t\tatomic64_inc(\u0026ioc-\u003ecur_period);\n2467:\t\n2468:\t\t/*\n2469:\t\t * Snapshot the state this period ran in before the idle\n2470:\t\t * transition wipes it, so the final tick reports the period's\n2471:\t\t * own busy level (e.g. the saturation that drove the controller\n2472:\t\t * idle) instead of the cleared one. usage is normalized by the\n2473:\t\t * measured period length, captured before ioc_start_period()\n2474:\t\t * overwrites period_at, the same way the donation loop does.\n2475:\t\t */\n2476:\t\t{\n2477:\t\t\tint tick_busy = ioc-\u003ebusy_level;\n2478:\t\t\tint tick_running = ioc-\u003erunning;\n2479:\t\t\tu64 tick_dur = now.now - ioc-\u003eperiod_at;\n2480:\t\t\t/* cur_period was already advanced to N+1 above; report\n2481:\t\t\t * the period that just ended, like the other fields */\n2482:\t\t\tu64 tick_period = atomic64_read(\u0026ioc-\u003ecur_period) - 1;\n2483:\t\n2484:\t\t\ttrace_iocost_ioc_tick(ioc, nr_active, usage_us_sum,\n2485:\t\t\t\t\t tick_period, tick_period_us,\n2486:\t\t\t\t\t tick_vrate, tick_busy, tick_running,\n2487:\t\t\t\t\t tick_dur);\n2488:\t\n2489:\t\t\tif (ioc-\u003erunning != IOC_STOP) {\n2490:\t\t\t\tif (!list_empty(\u0026ioc-\u003eactive_iocgs)) {\n2491:\t\t\t\t\tioc_start_period(ioc, \u0026now);\n2492:\t\t\t\t} else {\n2493:\t\t\t\t\tioc-\u003ebusy_level = 0;\n2494:\t\t\t\t\tioc-\u003evtime_err = 0;\n2495:\t\t\t\t\tioc-\u003erunning = IOC_IDLE;\n2496:\t\t\t\t}\n2497:\t\n2498:\t\t\t\tioc_refresh_vrate(ioc, \u0026now);\n2499:\t\t\t}\n2500:\t\t}\n2501:\t\n2502:\t\tspin_unlock_irq(\u0026ioc-\u003elock);\n2503:\t}\n2504:\t\n2505:\tstatic u64 adjust_inuse_and_calc_cost(struct ioc_gq *iocg, u64 vtime,\n2506:\t\t\t\t\t u64 abs_cost, struct ioc_now *now)\n2507:\t{\n2508:\t\tstruct ioc *ioc = iocg-\u003eioc;\n2509:\t\tstruct ioc_margins *margins = \u0026ioc-\u003emargins;\n"
} |
| 79/3 | 2026/09/14 08:21 | llm | kmsan-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 30906 output: 28 thoughts: 790 Thoughts: **Analyzing the `iocost_ioc_tick` Tracepoint and BPF Model Registration** Okay, let's dive into this code. First, I see we're looking at the definition of the `iocost_ioc_tick` tracepoint. The structure looks pretty straightforward, explicitly initializing every single field. I especially note the `__assign_str(devname)` macro, which is using `ioc_name(ioc)`. This is good, ensuring we have all the data we need for this tracepoint. Now, I'm switching gears to the `blk-iocost-bpf.c` file. I'm seeing the `bpf_iocost_reg` function, which seems to handle the registration of a BPF-based iocost model. I see that a `struct iocost_bpf_model *m` is allocated using `kzalloc`. Then, the code increments a reference counter, assigns operations, and adds it to two linked lists, `iocost_bpf_models` and `iocost_bpf_lifecycle`. It's a typical model initialization, really. But here's where my expert instincts kick in. How exactly are these `struct_ops` types being created and registered within the BPF framework? That's what I want to understand. I'll need to know more about how BPF handles allocation and preparation of `kdata` and `udata` for `struct_ops`. Time to check the `kernel/bpf/bpf_struct_ops.c` file and see what's what. Let's grep for `bpf_struct_ops`. |
| 80/3 | 2026/09/14 08:21 | tool | grepper |
0mArgs:{
"Expression": "init_member",
"PathPrefix": "kernel/bpf/"
}Results: {
"Output": "kernel/bpf/bpf_struct_ops.c=726=static long bpf_struct_ops_map_update_elem(struct bpf_map *map, void *key,\n--\nkernel/bpf/bpf_struct_ops.c-802-\nkernel/bpf/bpf_struct_ops.c:803:\t\terr = st_ops-\u003einit_member(t, member, kdata, udata);\nkernel/bpf/bpf_struct_ops.c-804-\t\tif (err \u003c 0)\n--\nkernel/bpf/bpf_struct_ops.c-806-\nkernel/bpf/bpf_struct_ops.c:807:\t\t/* The -\u003einit_member() has handled this member */\nkernel/bpf/bpf_struct_ops.c-808-\t\tif (err \u003e 0)\n--\nkernel/bpf/bpf_struct_ops.c-810-\nkernel/bpf/bpf_struct_ops.c:811:\t\t/* If st_ops-\u003einit_member does not handle it,\nkernel/bpf/bpf_struct_ops.c-812-\t\t * we will only handle func ptrs and zero-ed members\n--\nkernel/bpf/bpf_struct_ops.c-941-\t/* Error during st_ops-\u003ereg(). Can happen if this struct_ops needs to be\nkernel/bpf/bpf_struct_ops.c:942:\t * verified as a whole, after all init_member() calls. Can also happen if\nkernel/bpf/bpf_struct_ops.c-943-\t * there was a race in registering the struct_ops (under the same name) to\n"
} |
| 81/3 | 2026/09/14 08:21 | llm | kmsan-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 32506 output: 41 thoughts: 34 Thoughts: Here's my take on the thought process, as if it were my own: **Diving into the BPF Struct Ops Update** Okay, time to get my hands dirty. Let's dig into the details of how we're handling structure operations within the BPF framework. Specifically, I need to understand `bpf_struct_ops_map_update_elem`. I'm going to navigate to `kernel/bpf/bpf_struct_ops.c` and start poring over the code there. This function is critical for how we update elements within the struct ops map, and understanding the nuances of that process is key to debugging, optimization, and maintaining the overall integrity of the BPF system. I anticipate needing to review the data structures involved, the locking mechanisms used, and any validation checks performed to ensure that updates are done securely and correctly. This is where the rubber meets the road when it comes to struct ops, so I'll need to go slow, be methodical, and trace the execution path thoroughly. |
| 82/3 | 2026/09/14 08:21 | tool | read-file |
0mArgs:{
"File": "kernel/bpf/bpf_struct_ops.c",
"FirstLine": 790,
"LineCount": 40
}Results: {
"Contents": " 790:\t\t\tstruct bpf_ksym *ksym;\n 791:\t\t\tu32 moff;\n 792:\t\n 793:\t\t\tmoff = __btf_member_bit_offset(t, member) / 8;\n 794:\t\t\tmname = btf_name_by_offset(st_map-\u003ebtf, member-\u003ename_off);\n 795:\t\t\tptype = btf_type_resolve_ptr(st_map-\u003ebtf, member-\u003etype, NULL);\n 796:\t\t\tif (ptype == module_type) {\n 797:\t\t\t\tif (*(void **)(udata + moff))\n 798:\t\t\t\t\tgoto reset_unlock;\n 799:\t\t\t\t*(void **)(kdata + moff) = BPF_MODULE_OWNER;\n 800:\t\t\t\tcontinue;\n 801:\t\t\t}\n 802:\t\n 803:\t\t\terr = st_ops-\u003einit_member(t, member, kdata, udata);\n 804:\t\t\tif (err \u003c 0)\n 805:\t\t\t\tgoto reset_unlock;\n 806:\t\n 807:\t\t\t/* The -\u003einit_member() has handled this member */\n 808:\t\t\tif (err \u003e 0)\n 809:\t\t\t\tcontinue;\n 810:\t\n 811:\t\t\t/* If st_ops-\u003einit_member does not handle it,\n 812:\t\t\t * we will only handle func ptrs and zero-ed members\n 813:\t\t\t * here. Reject everything else.\n 814:\t\t\t */\n 815:\t\n 816:\t\t\t/* All non func ptr member must be 0 */\n 817:\t\t\tif (!ptype || !btf_type_is_func_proto(ptype)) {\n 818:\t\t\t\tu32 msize;\n 819:\t\n 820:\t\t\t\tmtype = btf_type_by_id(st_map-\u003ebtf, member-\u003etype);\n 821:\t\t\t\tmtype = btf_resolve_size(st_map-\u003ebtf, mtype, \u0026msize);\n 822:\t\t\t\tif (IS_ERR(mtype)) {\n 823:\t\t\t\t\terr = PTR_ERR(mtype);\n 824:\t\t\t\t\tgoto reset_unlock;\n 825:\t\t\t\t}\n 826:\t\n 827:\t\t\t\tif (memchr_inv(udata + moff, 0, msize)) {\n 828:\t\t\t\t\terr = -EINVAL;\n 829:\t\t\t\t\tgoto reset_unlock;\n"
} |
| 83/3 | 2026/09/14 08:21 | llm | kmsan-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 33456 output: 386 thoughts: 2705 Thoughts: **KMSAN Assessment: Blk-iocost BPF Model Integration** Alright, let's break this down systematically. I'm focusing on potential memory safety issues, particularly those related to uninitialized memory, which is the domain of KMSAN. My analysis starts by carefully examining how `struct iocost_bpf_model` is handled, given its central role in this patch. I see `kzalloc` is used for allocation, initializing all fields to zero – that's a good start. The critical `ops` pointer, the `list_head` and `refcount_t` are then also properly initialized. Static structures like `__bpf_ops_iocost_model_ops` and `bpf_iocost_model_ops` are similarly well-defined. The core functions, `calc_vtime_cost` and `calc_size_vtime_cost`, involve RCU-protected access to the `model` pointer. The `cost` variable's initialization is dependent on the result of `model->calc_cost` - which is well defined. I'm checking to make sure there are no uninitialized reads here – everything's fine. Now, looking at `ioc_rqos_exit`, it cleans up the `ioc->model` and then at `ioc_cost_model_prfill`, reads that model for formatting. The important aspect is ensuring no data is leaked to user space via these functions. Examining `ioc_cost_model_write`, and `iocost_bpf_model_get`, I confirm that the input string is properly validated, and the resulting structure is handled safely. The `bpf_obj_name_cpy` is used which zeroes before copying, which eliminates risks. The `iocost_bpf_model_get` gets a model and a reference count, no issues there. `ioc_bpf_model_prepare` checks to see if the name is an empty string. Even if the return `ret` value is used, the data is initialized by default. A critical point is to ensure no uninitialized data gets copied to user space. After reviewing how this code interacts with the userspace, it outputs text to the `seq_file` in the cgroup v2 io.cost.model interface. I can see from printing the strings that all data passed is properly initialized. Therefore, there's no risk of information leaks here. I've meticulously reviewed the KMSAN criteria. Nothing here involves kernel structures being sent to user space with potential uninitialized fields or padding, no conditional logic or branching based on uninitialized variables, no allocation of complex data structures with uninitialized reads. And no bounds checks or logic that could expose uninitialized data. The primary failure modes I can identify relate to concurrency and locking (reference counting, use-after-free), NULL pointer dereferences, and BPF verifier or dispatch issues. These are already well-covered by KASAN, LOCKDEP, and standard kernel bug detectors. KMSAN isn't the appropriate tool here. In conclusion, given the nature of the changes, the initialization patterns employed, and the careful handling of user-space interactions, `NeedsKMSAN` should be set to `false`. KASAN and LOCKDEP are more than sufficient to detect any memory safety issues in this patch. |
| 84/3 | 2026/09/14 08:21 | tool | set-results |
0mArgs:{
"KMSANReasoning": "The patch introduces BPF struct_ops support for pluggable cost models in the blk-iocost I/O controller (CONFIG_BLK_CGROUP_IOCOST_BPF), along with lifecycle hooks, a new tracepoint (iocost_ioc_tick), and BPF selftests.\n\nAnalysis of memory handling:\n1. Heap allocations: The only new heap allocation is `struct iocost_bpf_model`, which is allocated using `kzalloc()` with all fields (`refs`, `ops`, `list`, `lifecycle`) explicitly initialized.\n2. Stack buffers: `bpf_model` in `ioc_cost_model_write()` is initialized (`bpf_model[0] = '\\0'`) and written via `match_strlcpy()`. The tick tracepoint variables in `ioc_timer_fn()` are all explicitly populated scalar variables. Member name copying in BPF struct_ops uses `bpf_obj_name_cpy()`, which zeroes the target buffer and validates null-termination.\n3. Kernel-to-userspace exposure: No binary kernel structures, unpadded structs, or network packets are sent to user space. The only output is formatted ASCII text printed to `seq_file` in cgroupfs (`io.cost.model`), printing initialized strings and integers.\n4. KASAN vs KMSAN applicability: Any potential issues introduced by this patch involve concurrency, RCU dereferences, BPF struct_ops refcounting, and list management (risking use-after-free, double free, or deadlocks), which are detected by KASAN and LOCKDEP. There are no uninitialized memory reads or information leak hazards that would require KMSAN.",
"NeedsKMSAN": false
}Results: {
"KMSANReasoning": "The patch introduces BPF struct_ops support for pluggable cost models in the blk-iocost I/O controller (CONFIG_BLK_CGROUP_IOCOST_BPF), along with lifecycle hooks, a new tracepoint (iocost_ioc_tick), and BPF selftests.\n\nAnalysis of memory handling:\n1. Heap allocations: The only new heap allocation is `struct iocost_bpf_model`, which is allocated using `kzalloc()` with all fields (`refs`, `ops`, `list`, `lifecycle`) explicitly initialized.\n2. Stack buffers: `bpf_model` in `ioc_cost_model_write()` is initialized (`bpf_model[0] = '\\0'`) and written via `match_strlcpy()`. The tick tracepoint variables in `ioc_timer_fn()` are all explicitly populated scalar variables. Member name copying in BPF struct_ops uses `bpf_obj_name_cpy()`, which zeroes the target buffer and validates null-termination.\n3. Kernel-to-userspace exposure: No binary kernel structures, unpadded structs, or network packets are sent to user space. The only output is formatted ASCII text printed to `seq_file` in cgroupfs (`io.cost.model`), printing initialized strings and integers.\n4. KASAN vs KMSAN applicability: Any potential issues introduced by this patch involve concurrency, RCU dereferences, BPF struct_ops refcounting, and list management (risking use-after-free, double free, or deadlocks), which are detected by KASAN and LOCKDEP. There are no uninitialized memory reads or information leak hazards that would require KMSAN.",
"NeedsKMSAN": false
} |
| Total Calls | Total Tokens | Avg Tokens | Total Duration (Seconds) | Avg Duration (Seconds) |
|---|
| Total Calls | Total Duration (Seconds) | Avg Duration (Seconds) |
|---|