All of lore.kernel.org
 help / color / mirror / Atom feed
From: Christian Borntraeger <borntraeger@linux.ibm.com>
To: Kevin Wolf <kwolf@redhat.com>
Cc: qemu-block@nongnu.org, qemu-s390x <qemu-s390x@nongnu.org>,
	qemu-devel <qemu-devel@nongnu.org>
Subject: performance regression due to 0bd5a1b3d60c "block: Fix corruption from broken RMW atomicity"
Date: Thu, 26 Nov 2026 16:43:27 +0100	[thread overview]
Message-ID: <5fa22932-3a6c-4b77-af53-a2d187e7603f@linux.ibm.com> (raw)

Kevin,

the fix
- 0bd5a1b3d60c ("block: Fix corruption from broken RMW atomicity")
results in a pretty hefty performance regression for my nvme disk via virtio-blk and
multiple iothreads.

I uses AI to analyze and summarize and the result was seem legit from what I
can tell, so I paste this is verbatim.
I verified that 0bd5a1b3d60c is bad and 0bd5a1b3d60c^ is good.



## Description of problem
Since commit 0bd5a1b3d60c ("block: Fix corruption from broken RMW atomicity")
and the follow-up cf4f672c11d6 ("block: Remove now redundant
bdrv_wait_serialising_requests() calls"), tracked_request_begin()
unconditionally calls bdrv_wait_serialising_requests_locked(). The
`bs->serialising_in_flight` fast path that bdrv_wait_serialising_requests()
used to provide is gone.

As a result, every request, including plain aligned reads on a setup that
never has serialising requests, walks the complete `bs->tracked_requests`
list while holding `bs->reqs_lock`. With a multiqueue virtio-blk device
whose queues are mapped to several iothreads, all iothreads share that
one list and lock. With N requests in flight, each request does an O(N)
pointer chase through list nodes that live on other iothreads' coroutine
stacks, so most nodes are remote cache lines, and it holds the lock
while doing so.

The RMW race the commit fixes only exists with misaligned writes, e.g.
512-byte guest sectors on 4k host storage. The setup here is 512/512
(logical block size on both host and guest), so no request is ever
serialising, but all requests still pay for the walk.

Effect: multiqueue throughput stops scaling. Adding queues and iothreads
barely helps, no iothread reaches 100% CPU, and the host NVMe sees only
about 40 of the 128 requests the guest keeps in flight.

## Steps to reproduce
1. Attach a fast host NVMe namespace as raw virtio-blk with `num-queues=4`
    and an `iothread-vq-mapping` of one queue per iothread (4 iothreads),
    `aio=native`, `cache=none`.
2. In the guest, run 4k random reads with one job per queue, e.g.
    `fio --name=t --filename=/dev/vdb --direct=1 --ioengine=libaio --bs=4k --rw=randread --iodepth=32 --numjobs=4 --cpus_allowed=0,3,6,9 --cpus_allowed_policy=split --size=16G --offset_increment=16G --runtime=20 --time_based --group_reporting`
    (the CPU list puts one job on each blk-mq queue of a 12-vCPU guest).
3. Repeat with iodepth=64. Throughput goes down instead of up.
4. `perf record -g` on the host shows bdrv_find_conflicting_request() as
    one of the top symbols in the iothreads, called via
    blk_aio_read_entry -> bdrv_co_preadv_part -> tracked_request_begin ->
    bdrv_wait_serialising_requests_locked.

## Additional information

### Measurements
4k random I/O, IOPS, same guest and config throughout (two runs where two values are shown):

| test | master 2026-11-05 | master + suggested fix below | distro 11.1.90 snapshot |
|---|---|---|---|
| randread, 4 jobs x 32 deep | 542k / 566k | 693k / 741k | 494k / 505k |
| randwrite, 4 jobs x 32 deep | 533k / 534k | 700k / 707k | 536k / 538k |
| randread, 4 jobs x 64 deep | 448k | 830k | 445k |
| randread, 1 job x 128 deep | 339k | 368k | 333k |
| randread, 1 job x 1 deep | 12.9k | 12.3k | 12.8k |

The same 4 jobs reach about 839k IOPS when run directly on the host
NVMe, so with the fix the guest is close to bare metal at depth 64.

Other observations:
- **Profile:** before the fix, about 10% of iothread cycles are in
   bdrv_find_conflicting_request(). In annotated code, about 88% of those
   samples are on loading the next list node, and about 85% of
   tracked_request_begin()'s samples are on the shared list head.
- **Splitting the list confirms the cause.** Splitting the same NVMe into
   four dm-linear devices, attached as four separate virtio-blk disks (each
   with its own BlockDriverState, list and lock, same 4 iothreads, same 128
   requests in flight), raises throughput from about 440k to 613k IOPS
   without any code change, and bdrv_find_conflicting_request() drops to
   about 1% of iothread cycles.
- **Not the usual suspects:** changing iothread poll-max-ns, host KVM
   halt_poll_ns or aio=io_uring vs native made no difference to throughput.

This was measured on s390x, but nothing in the affected code is
architecture specific. I expect the same behaviour on other hosts with
multiqueue virtio-blk/virtio-scsi and several iothreads.

### Suggested fix
Restore the fast path inside tracked_request_begin(): only call
bdrv_wait_serialising_requests_locked() if `bs->serialising_in_flight` is
non-zero, reading the counter while `bs->reqs_lock` is held (i.e. after
the QLIST_INSERT_HEAD that is already done under the lock).

Why this keeps the race fixed:
- **Marking is atomic with respect to the lock.** A request is marked
   serialising (`req->serialising = true` plus the serialising_in_flight
   increment in tracked_request_set_serialising()) only while holding
   `bs->reqs_lock`.
- **So a zero counter means nothing can conflict.** If the new request
   sees the counter as 0 under the lock, no request currently in the list
   is serialising, and bdrv_find_conflicting_request() would return NULL
   anyway.
- **A later serialising request waits for us.** If a request becomes
   serialising after we dropped the lock, its own
   bdrv_wait_serialising_requests_locked() walks the list, finds our
   request (already inserted, `waiting_for == NULL`) and waits for it.
   That is the ownership semantics the RMW fix relies on.
- **The one remaining difference is harmless.** A serialising request may
   already have decremented the counter in tracked_request_end() but not
   yet left the list. It has finished its I/O, so not waiting for it is
   safe, and this matches the behaviour before 0bd5a1b3d60c.

With a local build containing exactly this conditional, the guest numbers
in the "master + suggested fix" column above were measured.

Remaining limitation, not addressed by this fix: all iothreads still
insert into and remove from one list under one mutex per
BlockDriverState. At higher request rates that will become the next
scaling limit for multiqueue devices.

### Fixes
- 0bd5a1b3d60c ("block: Fix corruption from broken RMW atomicity")
- cf4f672c11d6 ("block: Remove now redundant bdrv_wait_serialising_requests() calls")

Both were backported to stable-11.1 (v11.1.3) and stable-10.0 (v10.0.15),
so these releases are affected as well.

Christian


             reply	other threads:[~2026-11-26 15:43 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-26 15:43 Christian Borntraeger [this message]
2026-11-27 10:36 ` performance regression due to 0bd5a1b3d60c "block: Fix corruption from broken RMW atomicity" Kevin Wolf
2026-11-27 11:33   ` Paolo Bonzini
2026-11-27 13:58     ` Kevin Wolf
2026-11-30  9:32       ` Christian Borntraeger
2026-11-27 11:33   ` Christian Borntraeger

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=5fa22932-3a6c-4b77-af53-a2d187e7603f@linux.ibm.com \
    --to=borntraeger@linux.ibm.com \
    --cc=kwolf@redhat.com \
    --cc=qemu-block@nongnu.org \
    --cc=qemu-devel@nongnu.org \
    --cc=qemu-s390x@nongnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.