From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=100.103.45.18 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796312693; b=vc8SjCIm+ghqeHXB+7H8QLVdk2mE8cPYeT0PykBvoe4mIBZx8jfdzybRgyjZrq2F3FLL sLXa5H7BRRZA0TEzawPJD0AInKxEKhYG7i9h8Fsuvd66mRk/zo2NpBHsPVJyf4VTgD782 ai8YLZRmy/mpDiJOTqiokwtI82dQfEdHcIepfI0BDPdiaXwJi4OWf2DY/rnDppG44qh8r JsVvVzxmZS9ioGC3B5xfw0Otl9KtEK+HjNaLTH46a7ZeHVBhCd84BTg9TaIzrJu0/z2uc JZKp3OtV4Jz5Y1iMJd375MF502JlNIT+bCE4DwLxYag1InhUp2Sf7THtWPVw1vvjdUg== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796312693; h=Received:DKIM-Signature:Content-Type:MIME-Version:Message-Id: In-Reply-To:References:Subject:From:To:Cc:Date; bh=heoNqwTZa2zg1PrKSsOpQS/vs5/mLitgexatxR3Fx18=; b=WVntMEBa85RAS9A255kqK2dXxrA5qW+WpTm4Sj12Aia1hIXxB/ZeJo/mHMWZhopbncQU AfyxCp4ft7iPyLAoWdPUfEAxHmNGSgP/+HScgOlUKrc58IDKqOEvMMpX0JV1iVVRWS1Ne V0NbnJXdAQHzF6C8kM/mKFW+8auBEJC5R5KZlaxbV6AF22p6evsQG6W1mlqj5FFkF9+Kk poMzjrWROmJbfCzRl/la+x7rny0e/tHXhZSmwNozN7ha3kwJqInjWLOkZIiUEdRgCbeiz nYJdxxWaeoT/viWoIAf5iFd1doNkSdhGkvOMQwImjIFi0kxFb5wa7yjm1BUEq/E2D5Q== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=Nha4X9s0; arc=none smtp.remote-ip=100.103.45.18 Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 60FCF606A322; Thu, 3 Dec 2026 15:44:53 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=Nha4X9s0 Received: by smtp.kernel.org (Postfix) with ESMTPSA id 1CC281F00893; Thu, 3 Dec 2026 15:44:51 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1796312693; bh=heoNqwTZa2zg1PrKSsOpQS/vs5/mLitgexatxR3Fx18=; h=In-Reply-To:References:Subject:From:To:Cc:Date; b=Nha4X9s0ON2WdMJdlmEuqDizOOAdTxJfUZyJOta1v8eRSjpGL672swR1BRpqINkAY 5rxYsTHBteQCY79BvcBvn9yP5Wms/f9trpv/6NlBex2R9yGoL+aw+6clPL0Vc/SbJ5 u4jhLqrb0SYyXVS00uq0wMK4sitKpuCJIqmI44jN8zwc4RkZ9TiBsj36K/FGh9ltRV 8J3QkaTXtciNFKWFdzIAMekULJRipHWdhYO9gp0uzfMryZ33s78i9KnQbHfhCQMmW4 wGwyZF6Z9tsWyEculZ5T3DCG/1n87sdnAIDNYnr3qLxBOZDT1GKerudax9F3qbLhgA 9oQ8YMjVuguLw== Content-Type: multipart/mixed; boundary="===============3647615335938575297==" X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Message-Id: <76ee7f0cd787e3afcce95e7529d42ba30d4cd71071eb02dce7c43ea7018d955b@mail.kernel.org> In-Reply-To: <20261203150338.2625598-2-cui.tao@linux.dev> References: <20261203150338.2625598-2-cui.tao@linux.dev> Subject: Re: [PATCH v15 1/4] blk-iocost: add BPF struct_ops cost model support From: bot+bpf-ci@kernel.org To: cui.tao@linux.dev,tj@kernel.org,josef@toxicopanda.com,axboe@kernel.dk,ast@kernel.org,alexei.starovoitov@gmail.com Cc: cgroups@vger.kernel.org,linux-block@vger.kernel.org,linux-kernel@vger.kernel.org,bpf@vger.kernel.org,andrii@kernel.org,eddyz87@gmail.com,daniel@iogearbox.net,linux-kselftest@vger.kernel.org,cui.tao@linux.dev,cuitao@kylinos.cn,ameryhung@gmail.com,ast@kernel.org,andrii@kernel.org,daniel@iogearbox.net,martin.lau@kernel.org,eddyz87@gmail.com,yonghong.song@linux.dev,mason@kernel.org,ihor.solodrai@linux.dev Date: Thu, 3 Dec 2026 15:44:51 +0000 (UTC) --===============3647615335938575297== Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit > 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 - done_bio clamp can under-credit done_vtime, stalling iocg, and affects non-BPF users - Commit message -EOPNOTSUPP claim, a misleading comment and an __rcu annotation are nits > 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 > Signed-off-by: Tao Cui > Acked-by: Alexei Starovoitov > 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 --===============3647615335938575297==--