From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=91.218.175.219 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796376439; b=aeM31MaR5i9sGgfZHT4VgzSPYYrMdklQryt2BUHGupVJ9Vc7T07qCa0Q8OHnueHg2lI/ /fVHUHRuKboOkwTH0Yuj2VFTcT3qhWECo26WfHNrA8eAQq0e9M4V8irZtTzTDOFg/1m5G /hoqO5HZPQgl3yCOldAUFW6Scihe0461Iiba7j9egZDWKBKTYiVwbhTJS5ynfDuAsnGdL rrQta88ExegE9Wjrs2SGhunMSgak9DjQgEemsWAqfOmo/Ji4ts8N3shjWINuuPY7V4r4D eh539M6vT0ZCPvU2X+oT8aORyP+D6aTounJgYpSaD+F6hzBFYYfZ1auJWpWKwnqlVXA== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796376439; h=DMARC-Filter:X-Envelope-To:DKIM-Signature:X-Envelope-To:Received: X-Mizu-Trace-ID:X-Migadu-Flow:Message-ID:Date:MIME-Version:User-Agent: Cc:Subject:To:References:From:In-Reply-To:Content-Type: Content-Transfer-Encoding; bh=cVv2gM7Gk57zlfNlpIBOY2vsQOVSi9mp2vBnN5Je7GQ=; b=EdQfztDdShx/C4Mulh19p8NItjey7z/N8aXzjDlUSkxFPdzuR/gvGIR1sS7apjBi9nC0 cvXuzUhO2DLlDNqmdKSXRxnppqo1izE9+16BBK4RgLRC7jxLBqpCsQyboUSwdimnkm8Bl +BiDSr5vV/t/nSuKjVWHF3YtjIwALKrp6CCFXRxxEr4JfVLQw0mLonQ9vAKlLjQKik1R4 erjyIVU+kHexQEoF3tX2tBQgCdEssZHgQIbbEsXdryPiDDoAltn8ffDReslEhRLdVV9PN ReSLkGei3l6HICiJ5cbuIRdeJhffxWlS3F8/7Q9d8khVQ9Gh07KGyxp2V+k20h/hgkg== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=linux.dev; spf=pass smtp.mailfrom=linux.dev; dkim=pass header.d=linux.dev header.i=@linux.dev header.a=rsa-sha256 header.s=key1 header.b=VI+pSySu; arc=none smtp.remote-ip=91.218.175.219 Received: from mta0.migadu.com (out-219.mta0.migadu.com [91.218.175.219]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id B3676606A330 for ; Fri, 4 Dec 2026 09:27:18 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org B3676606A330 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linux.dev Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key, unprotected) header.d=linux.dev header.i=@linux.dev header.a=rsa-sha256 header.s=key1 header.b=VI+pSySu X-Envelope-To: linux-kselftest@vger.kernel.org DKIM-Signature: a=rsa-sha256; bh=ztTQyIFu0cEe08RYP3ClYCzvInNRGMrV+ZQ/trsr+ok=; c=simple/simple; d=linux.dev; h=from:to:subject:date:message-id:mime-version:content-type; s=key1; t=1796376436; v=1; x=1796981236; b=VI+pSySuLN4xWjuCMZe/tvWDr+Te81ubdshQVSnO48gC0SxNhlWTb+6Ly+3QhPso5FSEnAhM MGbbP4hQHuwBbzcguXZOVse3EjNJrh0Dce0xjXZTJS9Pz484a7qOM0+IP640mPPmO9BQCmjiLcE o0kZ48JuqBYAuzXTRzHjMZ60= X-Envelope-To: linux-kselftest@vger.kernel.org Received: by smtp.migadu.com with ESMTPS id 503ada0125a3ec81; Fri, 04 Dec 2026 09:27:16 +0000 X-Mizu-Trace-ID: 503ada0125a3ec81 X-Migadu-Flow: FLOW_OUT Message-ID: <4e451855-720f-4236-8f62-2d1a01e02288@linux.dev> Date: Fri, 4 Dec 2026 17:27:05 +0800 X-Mailing-List: linux-kselftest@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Cc: cui.tao@linux.dev, 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, cuitao@kylinos.cn, ameryhung@gmail.com, martin.lau@kernel.org, yonghong.song@linux.dev, mason@kernel.org, ihor.solodrai@linux.dev Subject: Re: [PATCH v15 1/4] blk-iocost: add BPF struct_ops cost model support To: bot+bpf-ci@kernel.org, tj@kernel.org, josef@toxicopanda.com, axboe@kernel.dk, ast@kernel.org, alexei.starovoitov@gmail.com References: <20261203150338.2625598-2-cui.tao@linux.dev> <76ee7f0cd787e3afcce95e7529d42ba30d4cd71071eb02dce7c43ea7018d955b@mail.kernel.org> From: Tao Cui In-Reply-To: <76ee7f0cd787e3afcce95e7529d42ba30d4cd71071eb02dce7c43ea7018d955b@mail.kernel.org> Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 在 2026/12/3 23:44, bot+bpf-ci@kernel.org 写道: >> 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 >> 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