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

From: Tao Cui

Date: Fri Dec 04 2026 - 04:27:26 EST




在 2026/12/3 23:44, bot+bpf-ci@xxxxxxxxxx 写道:
>> blk-iocost: add BPF struct_ops cost model support
>
> TL;DR:
> - ioc_bpf_reg() may overwrite shared ops->bdev on re-attach after device
> removal, possibly causing use-after-free of freed map/link
Yes, and it is the same shape as the RFC v7 report. v16 will
reject a second attachment with -EINVAL when ops->bdev is already set.
> - done_bio clamp can under-credit done_vtime, stalling iocg, and affects
> non-BPF users
Confirmed on all three counts. The premise does not hold: a payoff is
recorded against vtime at commit before it is paid into done_vtime,
and every shift moves both counters by the same delta, so done_vtime
cannot pass vtime. v16 will revert ioc_rqos_done_bio() to the
unconditional add and drop the clamp sentence from the commit message.

> - Commit message -EOPNOTSUPP claim, a misleading comment and an __rcu
> annotation are nits
All three will be reworked in v16: the -EOPNOTSUPP sentence will drop,
the detach comment will rewritten to the actual teardown order, and the
prfill goes through ioc_bpf_model() so the #ifdef block, the duplicated
seq_printf() and the __rcu local are gone.

Thanks,
Tao

>
>> blk-iocost: add BPF struct_ops cost model support
>>
>> iocost prices IO with a linear model derived from a handful of
>> device parameters. The real cost of an IO depends on device
>> internals the formula cannot capture and the right formula is
>> device specific. This lets a BPF program take over pricing for
>> a device so that the model can be adapted to it without
>> rebuilding the kernel.
>>
>> Attaching an iocost_model_ops struct_ops binds the model to one
>> device: each device has at most one attached model, and
>> attaching switches the device's pricing to it while io.cost.qos
>> keeps controlling whether the device is throttled at all. The
>> attached model also resets the per-iocg accounting, dropping
>> what was priced under the builtin model. "model=linear" and
>> "model=bpf" select between the attached model and the builtin
>> one across io.cost.model writes, and the model steps down when
>> the struct_ops link is deleted or the device is removed, at
>> which point the device reverts to the builtin model; a device
>> with no attached model rejects removal with -EOPNOTSUPP.
>> Attaching does not enable the controller.
>>
>> Switching between the builtin and a BPF model changes the
>> pricing, and debts, delays and waiters priced by the previous
>> model would keep the cgroup near-stalled while they pay back.
>> The accounting therefore starts fresh on a switch: debts and
>> delays are dropped, waiters are released without being charged,
>> and vtime snaps to the activation target. Leaving a BPF model
>> waits for an RCU grace period first so every bio it priced has
>> finished accounting; the reset runs only when the model is
>> still selected, and in-flight payoffs are clamped to vtime so
>> stale costs cannot push done_vtime past it.
>>
>> The model cannot go away while it is pricing: the bdev
>> reference from .reg() pins the queue until .unreg() drops it, so
>> the queue stays reachable even after del_gendisk() has unhashed
>> the bdev, and the link holds the model until .unreg() has run,
>> so iocost keeps no reference of its own. ioc_rqos_exit() ejects
>> through a work item because it runs under rq_qos_mutex; the
>> eject holds the link against a concurrent close. Models
>> without calc_cost or with zero or out-of-range transfer
>> coefficients are rejected when attached.
>>
>> The kernel keeps no per-cgroup state for the model; models
>> which need per-cgroup data keep it in their own maps, created
>> and freed with the cgroups. Without CONFIG_BLK_CGROUP_IOCOST_BPF
>> the struct_ops is not built and the builtin model is the only
>> choice.
>>
>> Suggested-by: Tejun Heo <tj@xxxxxxxxxx>
>> Signed-off-by: Tao Cui <cuitao@xxxxxxxxxx>
>> Acked-by: Alexei Starovoitov <ast@xxxxxxxxxx>
>>
>
> The commit message says "a device with no attached model rejects removal
> with -EOPNOTSUPP". Which code does this?
>
> I couldn't find a removal or detach request in the patch. io.cost.model
> accepts only ctrl=, model=linear, model=bpf and the coefficient tokens, and
> ioc_bpf_unreg() returns void. The only -EOPNOTSUPP returns added are attach
> failures in ioc_bpf_reg():
>
> if (!link)
> return -EOPNOTSUPP;
> ...
> if (bdev_is_partition(bdev) || !queue_is_mq(q)) {
> blkdev_put_no_open(bdev);
> return -EOPNOTSUPP;
> }
>
> The nearest real case, writing "model=bpf" to a device with no attached
> model, fails with -EINVAL in ioc_cost_model_write():
>
> else if (!strcmp(buf, "bpf") && ioc_bpf_attached(ioc))
> model = ioc_bpf_attached(ioc);
> else
> goto out_parse;
>
> Should this sentence be dropped, or changed to say that model=bpf is
> rejected with -EINVAL while no model is attached?
>
> The commit message also says "in-flight payoffs are clamped to vtime so
> stale costs cannot push done_vtime past it". See the ioc_rqos_done_bio()
> comment below for why that premise looks incorrect.
>
>> diff --git a/block/blk-iocost.c b/block/blk-iocost.c
>
> [ ... ]
>
>> @@ -2923,8 +3139,24 @@ static void ioc_rqos_done_bio(struct rq_qos *rqos, struct bio *bio)
>> {
>> struct ioc_gq *iocg = blkg_to_iocg(bio->bi_blkg);
>>
>> - if (iocg && bio->bi_iocost_cost)
>> - atomic64_add(bio->bi_iocost_cost, &iocg->done_vtime);
>> + if (iocg && bio->bi_iocost_cost) {
>> + /*
>> + * A model switch reset re-anchors vtime and done_vtime
>> + * while bios priced by the old model are still in flight.
>> + * Their completion would pay the stale cost into done_vtime
>> + * and can push it past vtime, breaking the (vtime -
>> + * done_vtime) invariant the accounting relies on until new
>> + * IO advances vtime again. Clamp the payoff to vtime; the
>> + * excess is priced by a model that no longer exists and is
>> + * what the reset drops.
>> + */
>> + u64 vtime = atomic64_read(&iocg->vtime);
>> + u64 vdone = atomic64_read(&iocg->done_vtime);
>> +
>> + atomic64_add(min(bio->bi_iocost_cost,
>> + vtime - min(vtime, vdone)),
>> + &iocg->done_vtime);
>> + }
>> }
>
> Can the premise of this clamp actually occur? iocg_model_switch_reset()
> moves both counters by the same delta:
>
> atomic64_add(vtarget - vtime, &iocg->vtime);
> atomic64_add(vtarget - vtime, &iocg->done_vtime);
>
> iocg_activate(), hweight_after_donation() and the debt payoff in
> iocg_kick_waitq() also move both counters together, so (vtime - done_vtime)
> should still equal the cost of the bios in flight and each completion can
> add its full bi_iocost_cost. Bios released by iocg_release_waiters() or
> charged as debt are never committed, so their bi_iocost_cost stays 0.
>
> The clamp also reads vtime and done_vtime as two separate lockless loads.
> Can it under-credit a bio when a writer lands between them? For example,
> with one bio in flight with cost 10, vtime=1000 and done_vtime=990, the
> completion reads vtime=1000. Then hweight_after_donation() discards an
> excess of 500, so vtime=1500 and done_vtime=1490. The completion reads
> done_vtime=1490 and computes min(10, 1000 - min(1000, 1490)) = 0, leaving
> done_vtime 10 behind vtime permanently.
>
> A similar loss can happen while iocg_model_switch_reset() runs, since it
> lowers vtime before it lowers done_vtime when vtime > vtarget, and a
> completion between the two adds sees vtime < vdone and adds 0.
>
> Plain min() on wrapping u64 vtime values is also not wrap-safe, unlike the
> time_after64()/time_before64() comparisons used elsewhere in the file. A
> bio in flight when vtime wraps gets 0 credit.
>
> Nothing closes the gap afterwards, because later adjustments shift both
> counters equally. iocg_is_idle() checks done_vtime != vtime, so the iocg is
> never deactivated and the period timer never stops. ioc_timer_fn() also
> counts a phantom inflight_us for it and, when rpct/wpct QoS is configured,
> counts it in nr_lagging each period, which prevents vrate from rising.
>
> The clamp is not under CONFIG_BLK_CGROUP_IOCOST_BPF, so it affects iocost
> users who never attach a BPF model. Should the clamp be dropped and the
> unconditional atomic64_add(bio->bi_iocost_cost, &iocg->done_vtime) kept?
> The comment and the commit message sentence would need correcting as well.
>
> [ ... ]
>
>> @@ -2980,10 +3212,105 @@ static void ioc_rqos_queue_depth_changed(struct rq_qos *rqos)
>> spin_unlock_irq(&ioc->lock);
>> }
>>
>> +#ifdef CONFIG_BLK_CGROUP_IOCOST_BPF
>> +/* Called by .unreg and by device removal, whichever is first. */
>> +struct ioc_bpf_detach {
>> + struct work_struct work;
>> + struct bpf_link *link;
>> +};
>
> This comment doesn't seem to match the code. struct ioc_bpf_detach and
> ioc_bpf_detach_workfn() are used only on device removal: ioc_rqos_exit()
> allocates and queues them, and .unreg never does.
>
> If the comment refers to ioc_detach_model(), that function has a single
> caller, ioc_bpf_unreg(). On removal, ioc_rqos_exit() queues the work, which
> calls .unreg, and ioc_bpf_unreg() then finds q_to_ioc() == NULL and skips
> ioc_detach_model(). Could the comment be updated to describe the actual
> teardown ordering?
>
> [ ... ]
>
>> @@ -3535,14 +3862,37 @@ static u64 ioc_cost_model_prfill(struct seq_file *sf,
> [ ... ]
>> +#ifdef CONFIG_BLK_CGROUP_IOCOST_BPF
>> + {
>> + const struct iocost_model_ops __rcu *ops;
>> +
>> + rcu_read_lock();
>> + ops = rcu_dereference(ioc->bpf_model);
>> + if (ops)
>> + seq_printf(sf, "model=bpf name=%.*s\n",
>> + (int)sizeof(ops->name), ops->name);
>
> Is the __rcu annotation on this local correct? rcu_dereference() returns a
> plain kernel pointer, so with CONFIG_BLK_CGROUP_IOCOST_BPF=y sparse should
> report an address-space mismatch on the assignment and a noderef dereference
> at ops->name. The local should probably be
> "const struct iocost_model_ops *ops".
>
> Since ioc->lock is already held here, would
> "const struct iocost_model_ops *ops = ioc_bpf_model(ioc);" work instead?
> ioc_bpf_model() already does rcu_dereference_check(..., lockdep_is_held(&ioc->lock))
> and has a NULL-returning stub when CONFIG_BLK_CGROUP_IOCOST_BPF is off.
> That would drop the extra RCU section and the duplicated seven-coefficient
> seq_printf() in the #ifdef/#else.
>
> [ ... ]
>
>> @@ -3703,6 +4142,205 @@ static struct blkcg_policy blkcg_policy_iocost = {
>> +static int ioc_bpf_reg(void *kdata, struct bpf_link *link)
>> +{
>> + struct iocost_model_ops *ops = kdata;
>
> [ ... ]
>
>> + if (ioc->attached) {
>> + ret = -EBUSY;
>> + goto out;
>> + }
>
> [ ... ]
>
>> + ops->bdev = bdev;
>> + ret = 0;
>> + goto done;
>
> ops->bdev is per-attachment state stored in the struct_ops map value, but
> every link created from that map shares the same kdata pointer. Can
> ioc_bpf_reg() overwrite an already-set ops->bdev?
>
> bpf_struct_ops_valid_to_reg() checks only the map type, BPF_F_LINK and READY
> state, so several links on one BPF_F_LINK map are allowed. The
> ioc->attached -EBUSY check rejects a second link only while the first is
> still attached to the same live ioc.
>
> On device removal there is a window. ioc_rqos_exit() only queues the eject
> work, and skips the eject entirely if kzalloc(GFP_NOIO) fails. Until the
> work runs, link1 is still registered and ops->bdev still holds B1. If the
> same dev_t comes back (for example LOOP_CTL_REMOVE followed by LOOP_CTL_ADD
> of the same index) and the same map is attached again:
>
> link2 reg:
> blkdev_get_no_open(X) -> B2
> q_to_ioc(q2) == NULL -> blk_iocost_init()
> ioc2->attached = ops, ioc2->attached_link = link2
> ops->bdev = B2 /* B1 reference leaked */
>
> eject work:
> ioc_bpf_detach_workfn() -> bpf_struct_ops_map_link_detach(link1)
> -> ioc_bpf_unreg(ops, link1)
> bdev = ops->bdev (B2); ioc2->attached_link != link1, no detach
> ops->bdev = NULL; blkdev_put_no_open(B2) /* drops link2's ref */
>
> link2 close:
> bpf_struct_ops_map_link_dealloc() -> ioc_bpf_unreg(ops, link2)
> bdev == NULL -> return, ioc_detach_model() never runs
> bpf_map_put() -> map freed, kfree(st_link)
>
> After that, ioc2->attached and ioc2->bpf_model still point into the freed
> map, so calc_vtime_cost() calls model->calc_cost() through freed memory.
> ioc2->attached_link points at the freed link, so a later removal of the
> disk does bpf_link_inc_not_zero() on freed memory in ioc_rqos_exit().
>
> hid_bpf_reg() guards against this case:
>
> /* prevent multiple attach of the same struct_ops */
> if (ops->hdev)
> return -EINVAL;
>
> Should ioc_bpf_reg() similarly reject a reg while ops->bdev is still set,
> for example with -EBUSY, or keep the bdev reference in per-link state
> instead of the shared map value?
>
>
> ---
> AI reviewed your patch. Please fix the bug or email reply why it's not a bug.
> See: https://github.com/kernel-patches/vmtest/blob/master/ci/claude/README.md
>
> CI run summary: https://github.com/kernel-patches/bpf/actions/runs/47135984243