Am 21.09.2026 um 22:41 hat Stefan Hajnoczi geschrieben:
> On Wed, Sep 16, 2026 at 10:04:51AM +0200, Hanna Czenczek wrote:
> > On 03.09.26 16:08, Stefan Hajnoczi wrote:
> > > On Mon, Aug 31, 2026 at 03:51:56PM +0200, Hanna Czenczek wrote:
> > > > Based-on:<[email protected]>
> > > >            [PATCH 0/6] hw/[block]: Fix missing accounting
> > > >            Fri, 24 Jul 2026 17:23:09 +0200
> > > > 
> > > > Hi,
> > > > 
> > > > I’m told there are installations where storage is very slow, and some
> > > > would like the VM stack to report this proactively.  To do so, we should
> > > > (via QAPI events) report on extremely slow I/O requests.
> > > > 
> > > > We already have the latency histogram, but this is not deemed sufficient
> > > > because it is not proactively reporting and would require repeated
> > > > querying.  Therefore, this series introduces the event still.
> > > > 
> > > > Now, from a user's perspective, it would be nice if this event could be
> > > > raised exactly when an I/O request crosses the user-defined threshold,
> > > > but this would require keeping all active requests in a list and
> > > > checking it periodically.  Now, if we used latency cookies for this
> > > Did you consider a per-request timer? The QEMUTimerList active_timers
> > > sorted list does not support efficient insertion, but improving it would
> > > benefit all timer API users.
> > 
> > I'm not sure how that would address the problem. Does that not just move the
> > list elsewhere?
> > 
> > I.e. it still has the problem that every request needs to be put into a list
> > (starting a timer), and be removed when the request is done, or the timer
> > will fire and the below problems would occur if the request is done but we
> > failed to remove it (either use-after-free; or spurious events plus memory
> > leaks on top of a heap allocation per request).
> 
> I wasn't thinking about the lifecycle here, but about the current
> limitation that users aren't notified until request completion. Requests
> can be stuck for a very long time or forever. Using a timer solves the
> timeliness problem.

I think we'll need to do this eventually. There was also talk about
stopping the VM if a request hangs for too long, which will definitely
need a timer.

But I think this specific series can work without it for now. Once we do
have the timer anyway, reporting the latency right when the threshold is
crossed can still be done.

We should be careful with the wording in the documentation to allow both
behaviours so we can make this change in the future.

> > > > (which makes sense), then that would require that every cookie set up is
> > > > also finalized when the request is done, because if we don't, results
> > > > could well be catastrophic:
> > > > - Either we use cookies as-is, which are often allocated on the stack or
> > > >    in some other structure managed by the device; then this would result
> > > >    in use-after-free,
> > > > - Or we allocate something specifically for this checking, so lingering
> > > >    requests would at most create spurious latency events and memory
> > > >    leaks, but this would require an additional heap allocation per
> > > >    request that we would probably want to avoid.
> > > > 
> > > > So ideally we could use latency cookies and could statically verify that
> > > > they are always finalized when the request is done, but doing this in C
> > > > may well be impossible.
> > > I think you are saying that the cookie API is unsafe because cookie
> > > lifetime is not bounded by the request lifetime?
> > 
> > Yes, because it is not statically proven to be so.
> > 
> > > Maybe the block_acct_*() API can be integrated into the actual request
> > > so there is no way to leak the cookie. In other words, directly
> > > associate requests with a BlockAcctStats and stop requiring the user to
> > > manually manage a separate BlockAcctCookie.
> > 
> > I don't follow what you mean concretely. If you are suggesting a list of
> > requests in BlockAcctStats, then that is exactly what I had.
> 
> I meant that each I/O request should contain its own cookie and there is
> never a need to create a cookie separately from the request. That way
> the lifetime issue is solved.
> 
> This would require API changes because device emulation code currently
> has some of the logic for cookies. Devices would no longer call
> block_acct_done() once they have called blk_aio_pwritev(), for
> example. I haven't looked in detail and am not sure if it's feasible.
> 
> > The problem is that if the destructor has to be called explicitly, we may
> > forget to do so; and accounting is done on the device emulation level, so
> > there is no central place where the pairing of constructor and destructor
> > would be obvious and trivial to verify.
> 
> This is the part I'm asking about: can accounting be done by the block
> layer? There might be cases that are purely handled in device emulation
> code without a call into the block layer. In that case the accounting
> still needs to be done in device emulation code. But when device
> emulation calls blk_aio_*(), it should not do accounting itself.

Apart from cases where requests are completed entirely within the
device (like for all block_acct_invalid() callers), there are also cases
where a single device-level requests involves multiple backend-level
requests. I was thinking of IDE TRIM initially, but actually I think
splitting can happen for any request that uses the DMA helpers.

Conversely, virtio-blk can merge requests, so you get a single request
in the backend that covers multiple requests in the device.

Kevin

Attachment: signature.asc
Description: PGP signature

Reply via email to