blk-iocost: charge flushes and zone appends
Subsystems (3)
v2 cover blk-iocost: charge flushes and zone appends
From: Tao Cui <[email protected]>
While testing iocost's weight-based throttling under concurrent IO, we observed that a cgroup limited to 1% weight could issue an unbounded number of flushes without being throttled: an fsync loop produced ~510k flushes in 12s with cost.usage staying at zero the entire time. The device was monopolized while iocost reported no activity. On ext4, a write+fsync workload showed the same gap: the write component was charged but the flush component was not.
The cause is that the builtin linear cost model defines coefficients only for READ and WRITE. Standalone flushes (the dataless REQ_OP_WRITE | REQ_PREFLUSH bios from blkdev_issue_flush()), zone append requests on zoned devices, and the flush component of data-bearing REQ_OP_WRITE | REQ_PREFLUSH bios (e.g. journal commits) fall through to a cost of zero. Zone append completions are also excluded from the latency statistics, so the vrate feedback loop cannot respond to latency induced by zone append operations.
1/4: charge the flush component of any REQ_PREFLUSH bio (standalone
and pre-flush) as pageless random writes with one-page floor, and skip the cursor update for dataless bios
2/4: charge zone appends as sequential writes; skip cursor update
(ZA bi_sector is zone start, not the actual write position)
3/4: count ZA completions in latency stats (vrate feedback) 4/4: fix stale comment referring to nonexistent aux_iocg
After this series, on the same 1%-weight cgroup:
- the fsync loop is limited to 24 flushes per 12s (matching the expected budget for the hdd profile)
- 16000 zone appends on a zoned null_blk are charged 533264 usec
- on ext4, the write+fsync workload is correctly accounted through the journal layer (~2.2us per flush, matching the ssd_fast profile's page-cost floor)
- sequential read throughput is unchanged
Both v1 review findings (cursor corruption by dataless flushes, missing charge for the PREFLUSH component of data-bearing writes) are addressed in 1/4; see its changelog for details. Proper flush-cost modeling with a dedicated coefficient will follow as a separate RFC.
Tao Cui (4):
blk-iocost: charge flushes as pageless random writes
blk-iocost: charge zone appends as page-counted sequential writes
blk-iocost: account zone append completions in latency stats
blk-iocost: fix stale comment in ioc_rqos_throttle()
block/blk-iocost.c | 42 ++++++++++++++++++++++++++++++++------- 1 file changed, 35 insertions(+), 7 deletions(-)
--
2.43.0
PATCH v2 1/4 blk-iocost: charge flushes as pageless random writes
From: Tao Cui <[email protected]>
Standalone flushes issued by blkdev_issue_flush() are represented as dataless REQ_OP_WRITE | REQ_PREFLUSH bios, which calc_vtime_cost_builtin() prices at zero. The flush component of flush-heavy workloads such as database commits, journal flushes, and metadata sync is thus neither charged nor throttled: a cgroup at 1% weight can issue ~510k flushes per 12s, monopolizing the device while iocost reports zero usage.
The same is true for the flush component of data-bearing REQ_OP_WRITE | REQ_PREFLUSH bios, e.g. journal commit writes: they are charged for their data only, and the cache flush the flush machine runs ahead of it is free.
Charge the flush component of any REQ_PREFLUSH bio on top of its data cost, priced as a pageless random write (LCOEF_WRANDIO), which provides an approximation of the device time consumed by a flush. For profiles where WRANDIO clamps to zero (ssd_dfl / ssd_fast), use a one-page floor (LCOEF_WPAGE). A dataless flush bio falls out of the switch with zero data cost and picks up the same surcharge, so standalone and pre-flush forms are priced the same way. After this patch, the same 1%-weight cgroup is limited to 24 flushes per 12s; on ext4, write+fsync workloads are correctly accounted through the journal layer (~2.2us per flush on the ssd_fast profile).
A standalone flush must also not update iocg->cursor: its bi_sector (usually 0) is not a data position, so setting the cursor from it would misclassify the following READ/WRITE bios, and a zero cursor defeats the !iocg->cursor sentinel in calc_vtime_cost_builtin(). Skip the cursor update for dataless bios.
---
Changes in v2:
- Skip the iocg->cursor update for dataless flush bios, which would otherwise corrupt the seq/rand classification of the following IOs (reported in review of v1).
- Charge the flush component of data-bearing REQ_PREFLUSH bios too; v1 only priced standalone flushes (reported in review of v1).
--- block/blk-iocost.c | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-)
block/blk-iocost.c +17 -3
@@ -2532,8 +2532,20 @@ static void calc_vtime_cost_builtin(struct bio *bio, struct ioc_gq *iocg,u64 pages =max_t (u64,bio_sectors (bio) >>IOC_SECT_TO_PAGE_SHIFT ,1 );u64 seek_pages =0 ;u64 cost =0 ; +u64 flush_cost =0 ; -/* Can't calculate cost for empty bio */ +/* +* A WRITE|REQ_PREFLUSH bio carries a flush component: the flush +* machine runs a cache flush for it, either standalone (dataless) +* or ahead of the data. Charge the flush on top of the data cost, +* priced as a pageless random write with a one-page floor so fast +* profiles still charge something. Flush bios are never merged. +*/ +if (!is_merge && (bio->bi_opf &REQ_PREFLUSH )) + flush_cost =max (ioc->params.lcoefs[LCOEF_WRANDIO ], + ioc->params.lcoefs[LCOEF_WPAGE ]); + +/* Can't calculate data cost for empty bio */ if (!bio->bi_iter.bi_size) goto out;
@@ -2566,7 +2578,7 @@ static void calc_vtime_cost_builtin(struct bio *bio, struct ioc_gq *iocg, } cost += pages * coef_page; out: - *costp = cost; + *costp = cost + flush_cost; }static u64 calc_vtime_cost (struct bio *bio,struct ioc_gq *iocg,bool is_merge)
@@ -2708,7 +2720,9 @@ static void ioc_rqos_throttle(struct rq_qos *rqos, struct bio *bio)if (!iocg_activate (iocg, &now))return ; - iocg->cursor =bio_end_sector (bio); +/* dataless bios have no meaningful position for seq/rand detection */ +if (bio->bi_iter.bi_size) + iocg->cursor =bio_end_sector (bio); vtime =atomic64_read (&iocg->vtime); cost =adjust_inuse_and_calc_cost (iocg, vtime, abs_cost, &now);
Signature · 1 line
2.43.0
Re: [PATCH v2 1/4] blk-iocost: charge flushes as pageless random writes
Looks good:
Tejun Heo
maintainerRe: [PATCH v2 1/4] blk-iocost: charge flushes as pageless random writes
In reply to Tao CuiHello, Tao.
Quoted 5 lines · Tao Cui
Charge the flush component of any
REQ_PREFLUSHbio on top of its data cost, priced as a pageless random write (LCOEF_WRANDIO), which provides an approximation of the device time consumed by a flush. For profiles where WRANDIO clamps to zero (ssd_dfl/ssd_fast), use a one-page floor (LCOEF_WPAGE).
Pricing a flush as a random write, or as a page where that comes out to zero, is as arbitrary as pricing it at zero, and it changes what existing setups get charged. 013adcbef165 ("blk-iocost: skip empty flush bio in iocost") zeroed them because the flush machinery coalesces flushes and the write coefficients don't say anything about what a flush costs.
Can you instead add the flushiops parameter you mentioned in the v1 thread to the linear model? A flush costs VTIME_PER_SEC / flushiops, and zero when the parameter isn't set, including in the builtin profiles, so nothing changes until it's configured or the profiles are regenerated with a flush measurement.
The same charge should cover REQ_FUA the way the flush machinery does: one flush for PREFLUSH, and one for FUA on a device without FUA support, which is when a post-flush gets issued.
Please also drop the Fixes tag on this patch. With the parameter, this is an extension rather than a fix.
Thanks.
Signature · 1 line
tejun
Re: [PATCH v2 1/4] blk-iocost: charge flushes as pageless random writes
Hello Tejun,
Quoted 9 lines · Tejun Heo
Hello, Tao.
Quoted 5 lines · Tao Cui
Charge the flush component of any
REQ_PREFLUSHbio on top of its data cost, priced as a pageless random write (LCOEF_WRANDIO), which provides an approximation of the device time consumed by a flush. For profiles where WRANDIO clamps to zero (ssd_dfl/ssd_fast), use a one-page floor (LCOEF_WPAGE).
Quoted 6 lines
Pricing a flush as a random write, or as a page where that comes out to zero, is as arbitrary as pricing it at zero, and it changes what existing setups get charged.
013adcbef165("blk-iocost: skip empty flush bio in iocost") zeroed them because the flush machinery coalesces flushes and the write coefficients don't say anything about what a flush costs.
Understood. The write-coefficient pricing was only an approximation and would change the accounting behavior for existing setups, so I'll drop it and rework the series around a dedicated flush cost.
Quoted 6 lines
Can you instead add the flushiops parameter you mentioned in the v1 thread to the linear model? A flush costs
VTIME_PER_SEC/ flushiops, and zero when the parameter isn't set, including in the builtin profiles, so nothing changes until it's configured or the profiles are regenerated with a flush measurement.
Will do. I'll add flushiops to the linear model using the same conversion as the other iops coefficients:
- add a flushiops=%u token for io.cost.model and translate it to LCOEF_FLUSH = VTIME_PER_SEC / flushiops
- keep flushiops zero in builtin profiles, with no fallback, so existing profiles keep their current behavior
- account REQ_PREFLUSH and REQ_FUA consistently with the flush machinery:
PREFLUSH charges one flush, and FUA adds another flush when the device does not support native FUA
The rest of the series (zone append charging, latency accounting, and the comment fix) remains unchanged.
Quoted 7 lines
The same charge should cover
REQ_FUAthe way the flush machinery does: one flush for PREFLUSH, and one for FUA on a device without FUA support, which is when a post-flush gets issued.Please also drop the Fixes tag on this patch. With the parameter, this is an extension rather than a fix.
Done. v3 will position this as a cost model extension.
One remaining question is whether builtin profiles should eventually carry measured flushiops values, or whether flushiops is expected to remain user-configured. This series leaves it at zero because I don't have reliable measurements for the existing profiles.
Thanks,
Tao
Quoted 2 lines
Thanks.
Tejun Heo
maintainerRe: [PATCH v2 1/4] blk-iocost: charge flushes as pageless random writes
Hello,
Quoted 4 lines · Tao Cui
One remaining question is whether builtin profiles should eventually carry measured flushiops values, or whether flushiops is expected to remain user-configured. This series leaves it at zero because I don't have reliable measurements for the existing profiles.
The cost of a flush is more a function of what came before the flush how long ago than anything else. The linear model is too dumb to estimate that. You can tune this to suit a particular use case and maybe one can argue that charging a small cost to penalize unnecessarily frequent flushes would be a reasonable default policy, but, I don't know, what the default model can do is pretty limited, so I'm not sure it matters that much one way or the other.
If getting this right is actually useful, stateful BPF model is probably the right way.
Thanks.
Signature · 1 line
tejun
Re: [PATCH v2 1/4] blk-iocost: charge flushes as pageless random writes
In reply to Tejun HeoQuoted 3 lines · Tejun Heo
Hello, Tao.
Pricing a flush as a random write, or as a page where that comes out to zero, is as arbitrary as pricing it at zero,
No, it absolutely is not. For any flush that actually makes here it is extremely expensive, and 0 is the wrongest possible value. Even batching does not change that, as batching is not actually effective in any normal file system workload.
Tejun Heo
maintainerRe: [PATCH v2 1/4] blk-iocost: charge flushes as pageless random writes
Quoted 9 lines · Christoph Hellwig
Quoted 3 lines · Tejun Heo
Hello, Tao.
Pricing a flush as a random write, or as a page where that comes out to zero, is as arbitrary as pricing it at zero,No, it absolutely is not. For any flush that actually makes here it is extremely expensive, and 0 is the wrongest possible value. Even batching does not change that, as batching is not actually effective in any normal file system workload.
I see. I don't think the default value matters all that much given how crude a tool the default model is. We can set it to a page. I don't think we should impose it on existing users that are configuring custom models.
Thanks.
Signature · 1 line
tejun
PATCH v2 2/4 blk-iocost: charge zone appends as page-counted sequential writes
In reply to Tao CuiFrom: Tao Cui <[email protected]>
Zone append is a primary write operation for zoned devices; zoned btrfs and f2fs use it for data writes. It is priced at zero, so the zone append portion of zoned workloads runs outside the controller:
a 1%-weight cgroup issued 16000 appends at zero cost on a zoned null_blk.
A zone append advances the zone write pointer and is therefore sequential from the device's perspective; the actual sector is only returned after completion, so the cursor-based seq/rand classification doesn't apply. Price it as a page-counted sequential write. After this patch, 16000 appends from the same cgroup are charged 533264 usec (33us per append).
Also skip the cursor update for ZA bios: bi_sector is the zone start, not the actual write position (which is only returned after completion). Setting the cursor from ZA would misclassify subsequent READ/WRITE bios.
--- block/blk-iocost.c | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-)
block/blk-iocost.c +13 -2
@@ -2560,6 +2560,17 @@ static void calc_vtime_cost_builtin(struct bio *bio, struct ioc_gq *iocg, coef_randio = ioc->params.lcoefs[LCOEF_WRANDIO ]; coef_page = ioc->params.lcoefs[LCOEF_WPAGE ];break ; +case REQ_OP_ZONE_APPEND : +/* +* A zone append advances the zone write pointer and is +* therefore sequential from the device's perspective, so +* the cursor-based classification below doesn't apply. +* Compute the full cost here. +*/ +if (!is_merge) + cost += ioc->params.lcoefs[LCOEF_WSEQIO ]; + cost += pages * ioc->params.lcoefs[LCOEF_WPAGE ]; + goto out;default : goto out; }
@@ -2720,8 +2731,8 @@ static void ioc_rqos_throttle(struct rq_qos *rqos, struct bio *bio)if (!iocg_activate (iocg, &now))return ; -/* dataless bios have no meaningful position for seq/rand detection */ -if (bio->bi_iter.bi_size) +/* ZA bi_sector is zone start, dataless bios have no write position */ +if (bio->bi_iter.bi_size &&bio_op (bio) !=REQ_OP_ZONE_APPEND ) iocg->cursor =bio_end_sector (bio); vtime =atomic64_read (&iocg->vtime); cost =adjust_inuse_and_calc_cost (iocg, vtime, abs_cost, &now);
Signature · 1 line
2.43.0
Re: [PATCH v2 2/4] blk-iocost: charge zone appends as page-counted sequential writes
Quoted 4 lines · Tao Cui
From: Tao Cui <[email protected]>
Zone append is a primary write operation for zoned devices; zoned btrfs and f2fs use it for data writes.
f2fs doesn't. xfs and btrfs do.
Otherwise looks good:
Re: [PATCH v2 2/4] blk-iocost: charge zone appends as page-counted sequential writes
Hi Christoph,
Quoted 8 lines · Christoph Hellwig
Quoted 4 lines · Tao Cui
From: Tao Cui <[email protected]>
Zone append is a primary write operation for zoned devices; zoned btrfs and f2fs use it for data writes.
f2fs doesn't. xfs and btrfs do.
Thanks for the review. I'll fix the commit message to remove f2fs and mention xfs and btrfs instead.
Thanks,
Tao
Quoted 3 lines
Otherwise looks good:
Reviewed-by Christoph Hellwig <[email protected]>
Tejun Heo
maintainerRe: [PATCH v2 2/4] blk-iocost: charge zone appends as page-counted sequential writes
In reply to Tao CuiHello, Tao.
Quoted 4 lines · Tao Cui
A zone append advances the zone write pointer and is therefore sequential from the device's perspective; the actual sector is only returned after completion, so the cursor-based seq/rand classification doesn't apply. Price it as a page-counted sequential write.
Whether an append is cheap depends on where the previous IO went, the same as for a regular write, and the cursor already tracks that. The sector is the zone start rather than the write pointer, but the cursor gets set from the same sector, so back-to-back appends to the same zone are a bio length apart and classify sequential, appends to different zones are a zone apart and classify random, and emulated appends already arrive here as regular writes at the write pointer.
It isn't exact. A read or regular write into a zone being appended to is measured from the zone start rather than the write pointer, so it drifts toward random as the zone fills, and the last append in one zone and the first in the next are contiguous but compute as a zone apart. Both are bounded by the difference between the seq and rand base costs of one IO, which is small on the flash devices that do native appends. Mostly right is fine here. Can you just add REQ_OP_ZONE_APPEND to the REQ_OP_WRITE case and leave the cursor update alone?
Also, the Fixes tags on this patch and 3/4 should point at 0512a75b98f8 ("block: Introduce REQ_OP_ZONE_APPEND"), which added the op without pricing it, rather than the iocost commit that predates it.
Thanks.
Signature · 1 line
tejun
Re: [PATCH v2 2/4] blk-iocost: charge zone appends as page-counted sequential writes
Hello Tejun,
Quoted 16 lines · Tejun Heo
Hello, Tao.
Quoted 4 lines · Tao Cui
A zone append advances the zone write pointer and is therefore sequential from the device's perspective; the actual sector is only returned after completion, so the cursor-based seq/rand classification doesn't apply. Price it as a page-counted sequential write.
Whether an append is cheap depends on where the previous IO went, the same as for a regular write, and the cursor already tracks that. The sector is the zone start rather than the write pointer, but the cursor gets set from the same sector, so back-to-back appends to the same zone are a bio length apart and classify sequential, appends to different zones are a zone apart and classify random, and emulated appends already arrive here as regular writes at the write pointer.
Quoted 9 lines
It isn't exact. A read or regular write into a zone being appended to is measured from the zone start rather than the write pointer, so it drifts toward random as the zone fills, and the last append in one zone and the first in the next are contiguous but compute as a zone apart. Both are bounded by the difference between the seq and rand base costs of one IO, which is small on the flash devices that do native appends. Mostly right is fine here. Can you just add
REQ_OP_ZONE_APPENDto theREQ_OP_WRITEcase and leave the cursor update alone?
That fits the existing model better than the special handling I introduced. I had missed that the cursor is set from the same zone-start sector on every append, so back-to-back appends classify sequential as expected, and that emulated appends already take the regular WRITE path.
I'll drop the special case and the cursor skip in v3 and just add REQ_OP_ZONE_APPEND to the REQ_OP_WRITE case.
Quoted 4 lines
Also, the Fixes tags on this patch and 3/4 should point at
0512a75b98f8("block: IntroduceREQ_OP_ZONE_APPEND"), which added the op without pricing it, rather than the iocost commit that predates it.
Agreed, that's the right anchor - the gap appeared when the op was added. Both Fixes tags will be updated.
Thanks.
Quoted 2 lines
Thanks.
PATCH v2 3/4 blk-iocost: account zone append completions in latency stats
In reply to Tao CuiFrom: Tao Cui <[email protected]>
ioc_rqos_done() only accounts READ and WRITE completions, so zone append completions are excluded from the latency window: the vrate feedback loop cannot see ZA-induced latency, leaving it unable to respond to device saturation caused by zone appends. Similarly, calc_size_vtime_cost_builtin() does not classify zone append as a write operation.
This is not covered by the patch that prices zone appends: charging goes through the cost model, while the latency window is the feedback path the vrate controller watches.
Treat zone append as WRITE for both latency accounting and cost classification.
--- block/blk-iocost.c | 2 ++ 1 file changed, 2 insertions(+)
block/blk-iocost.c +2 -0
@@ -2609,6 +2609,7 @@ static void calc_size_vtime_cost_builtin(struct request *rq, struct ioc *ioc,case REQ_OP_READ : *costp = pages * ioc->params.lcoefs[LCOEF_RPAGE ];break ; +case REQ_OP_ZONE_APPEND :case REQ_OP_WRITE : *costp = pages * ioc->params.lcoefs[LCOEF_WPAGE ];break ;
@@ -2879,6 +2880,7 @@ static void ioc_rqos_done(struct rq_qos *rqos, struct request *rq) pidx =QOS_RLAT ; rw =READ ;break ; +case REQ_OP_ZONE_APPEND :case REQ_OP_WRITE : pidx =QOS_WLAT ; rw =WRITE ;
Signature · 1 line
2.43.0
Re: [PATCH v2 3/4] blk-iocost: account zone append completions in latency stats
Looks good:
PATCH v2 4/4 blk-iocost: fix stale comment in ioc_rqos_throttle()
In reply to Tao CuiFrom: Tao Cui <[email protected]>
The comment says that priority-inversion IOs are "punted to @ioc->aux_iocg", but no aux_iocg field ever existed in struct ioc. The comment was introduced already stale by commit da437b95db83 ("blk-iocost: grab ioc->lock for debt handling"). Update it to describe the current use_debt / iocg->abs_vdebt mechanism.
--- block/blk-iocost.c | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-)
block/blk-iocost.c +5 -4
@@ -2751,10 +2751,11 @@ static void ioc_rqos_throttle(struct rq_qos *rqos, struct bio *bio)/* * We're over budget. This can be handled in two ways. IOs which may -* cause priority inversions are punted to @ioc->aux_iocg and charged as -* debt. Otherwise, the issuer is blocked on @iocg->waitq. Debt handling -* requires @ioc->lock, waitq handling @iocg->waitq.lock. Determine -* whether debt handling is needed and acquire locks accordingly. +* cause priority inversions are issued regardless and charged against +* @iocg->abs_vdebt as debt. Otherwise, the issuer is blocked on +* @iocg->waitq. Debt handling requires @ioc->lock, waitq handling +* @iocg->waitq.lock. Determine whether debt handling is needed and +* acquire locks accordingly. */ use_debt =bio_issue_as_root_blkg (bio) ||fatal_signal_pending (current); ioc_locked = use_debt ||READ_ONCE (iocg->abs_vdebt);
Signature · 1 line
2.43.0
Re: [PATCH v2 4/4] blk-iocost: fix stale comment in ioc_rqos_throttle()
Looks good: