Re: [RFC PATCH v14 1/4] blk-iocost: add BPF struct_ops cost model support

From: Tejun Heo

Date: Sun Nov 29 2026 - 14:10:47 EST


Hello, Tao.

On Wed, Nov 18, 2026 at 09:10:26PM +0800, Tao Cui wrote:
> + const struct iocost_model_ops *attached;
> + struct bpf_link *attached_link;
> + /* either NULL (linear) or ->attached */
> + const struct iocost_model_ops __rcu *bpf_model;

Can you put these inside #ifdef CONFIG_BLK_CGROUP_IOCOST_BPF and go through
helpers with stubs where they're used outside the struct_ops code, e.g. in
ioc_cost_model_write()?

> @@ -2657,6 +2723,41 @@ static u64 calc_vtime_cost(struct bio *bio, struct ioc_gq *iocg, bool is_merge)
...
> + cost = model->calc_cost(bio, is_merge ? IOCOST_CALC_MERGE : 0);
> + return min(cost, VTIME_PER_SEC);

Maybe calc_vtime_cost() and calc_vtime_cost_builtin() can take IOCOST_CALC_
flags from the callers instead of the is_merge bool?

Also, what's the justification for capping the cost at VTIME_PER_SEC?

> @@ -3535,11 +3680,12 @@ static u64 ioc_cost_model_prfill(struct seq_file *sf,
...
> - seq_printf(sf, "%s ctrl=%s model=linear "
> + seq_printf(sf, "%s ctrl=%s model=%s "
> "rbps=%llu rseqiops=%llu rrandiops=%llu "
> "wbps=%llu wseqiops=%llu wrandiops=%llu "
> "flushiops=%llu\n",

ctrl and the coefficients describe the linear model and don't mean anything
while a BPF model is selected. Can you add a name[] member to
iocost_model_ops and print that instead when the model is bpf? Something
like:

252:1 model=bpf name=<name>

> +static void ioc_bpf_unreg(void *kdata, struct bpf_link *link)
> +{
...
> + /* del_gendisk() is in progress or done. ioc_rqos_exit() detaches. */
> + bdev = blkdev_get_no_open(new_decode_dev(ops->dev), false);
> + if (!bdev)
> + return;

Looking the device up by dev_t again here is what makes iocost hold its own
reference on the map and handle the window after the bdev is unhashed. I
wonder whether it'd be simpler to make .unreg the only detach path:

- .reg keeps the device reference it already has and stores the pointer in
a private member of iocost_model_ops, like hid_bpf_ops->hdev. .unreg
reaches the queue through it and drops the reference.

- ioc_rqos_exit() runs under rq_qos_mutex and can't detach the link
directly, but it can queue a work item which calls link->ops->detach()
so that the usual .unreg runs. It'd need to hold the link for that,
maybe with bpf_link_inc_not_zero().

The link holds the map until .unreg has run, so iocost wouldn't need its
own reference on it. The device reference would be there only for .unreg
calls which run after rq_qos_exit(), from the work item or from a link
close racing the removal. Can you add a comment explaining that?

Thanks.

--
tejun