Skip to contentSkip to conversation

linux-aio: fix unbalanced plugged counter in laio_io_unplug()

PATCH Applied Stefan Hajnoczi 3 participants 7 messages Jun 9, 2022 – Jun 16, 2022 Block layer

lore
Share & tools
lore
#0 lore raw

cover linux-aio: fix unbalanced plugged counter in laio_io_unplug()

An unlucky I/O pattern can result in stalled Linux AIO requests when the plugged counter becomes unbalanced. See Patch 1 for details.

Patch 2 adds a comment to explain why the laio_io_unplug() even checks max batch in the first place.

Stefan Hajnoczi (2):
linux-aio: fix unbalanced plugged counter in laio_io_unplug()
linux-aio: explain why max batch is checked in laio_io_unplug()

 block/linux-aio.c | 10 +++++++++-
 1 file changed, 9 insertions(+), 1 deletion(-)
Signature · 1 line
2.36.1
#1 lore raw

PATCH 1/2 linux-aio: fix unbalanced plugged counter in laio_io_unplug()

Every laio_io_plug() call has a matching laio_io_unplug() call. There is a plugged counter that tracks the number of levels of plugging and allows for nesting.

The plugged counter must reflect the balance between laio_io_plug() and laio_io_unplug() calls accurately. Otherwise I/O stalls occur since io_submit(2) calls are skipped while plugged.

---
 block/linux-aio.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
block/linux-aio.c +3 -1
index 4c423fcccf..6078da7e42 100644
@@ -363,8 +363,10 @@ void laio_io_unplug(BlockDriverState *bs, LinuxAioState *s,
                     uint64_t dev_max_batch)
 {
     assert(s->io_q.plugged);
+    s->io_q.plugged--;
+
     if (s->io_q.in_queue >= laio_max_batch(s, dev_max_batch) ||
-        (--s->io_q.plugged == 0 &&
+        (!s->io_q.plugged &&
          !s->io_q.blocked && !QSIMPLEQ_EMPTY(&s->io_q.pending))) {
         ioq_submit(s);
     }
Signature · 1 line
2.36.1
Reviewed-by on v1 1/2
#2 lore raw
Quoted 8 lines · Stefan Hajnoczi

Every laio_io_plug() call has a matching laio_io_unplug() call. There is a plugged counter that tracks the number of levels of plugging and allows for nesting.

The plugged counter must reflect the balance between laio_io_plug() and laio_io_unplug() calls accurately. Otherwise I/O stalls occur since io_submit(2) calls are skipped while plugged.

We can add a Fixes tag:

Fixes 68d7946648 ("linux-aio: add `dev_max_batch` parameter to laio_io_unplug()")
Quoted 26 lines

---
block/linux-aio.c | 4 +++-

1 file changed, 3 insertions(+), 1 deletion(-)
block/linux-aio.c +3 -1
index 4c423fcccf..6078da7e42 100644
@@ -363,8 +363,10 @@ void laio_io_unplug(BlockDriverState *bs, LinuxAioState *s,
                    uint64_t dev_max_batch)
 {
    assert(s->io_q.plugged);
+    s->io_q.plugged--;
+
    if (s->io_q.in_queue >= laio_max_batch(s, dev_max_batch) ||
-        (--s->io_q.plugged == 0 &&
+        (!s->io_q.plugged &&
         !s->io_q.blocked && !QSIMPLEQ_EMPTY(&s->io_q.pending))) {
        ioq_submit(s);
    }
Signature · 1 line
2.36.1

Thanks for fixing this issue!

#3 lore raw

PATCH 2/2 linux-aio: explain why max batch is checked in laio_io_unplug()

In reply to Stefan Hajnoczi

It may not be obvious why laio_io_unplug() checks max batch. I discussed this with Stefano and have added a comment summarizing the reason.

Cc Stefano Garzarella <[email protected]>
Cc Kevin Wolf <[email protected]>
---
 block/linux-aio.c | 6 ++++++
 1 file changed, 6 insertions(+)
block/linux-aio.c +6 -0
index 6078da7e42..9c2393a2f7 100644
@@ -365,6 +365,12 @@ void laio_io_unplug(BlockDriverState *bs, LinuxAioState *s,
     assert(s->io_q.plugged);
     s->io_q.plugged--;
 
+    /*
+     * Why max batch checking is performed here:
+     * Another BDS may have queued requests with a higher dev_max_batch and
+     * therefore in_queue could now exceed our dev_max_batch. Re-check the max
+     * batch so we can honor our device's dev_max_batch.
+     */
     if (s->io_q.in_queue >= laio_max_batch(s, dev_max_batch) ||
         (!s->io_q.plugged &&
          !s->io_q.blocked && !QSIMPLEQ_EMPTY(&s->io_q.pending))) {
Signature · 1 line
2.36.1
Reviewed-by on v1 2/2
#4 lore raw

Re: [PATCH 2/2] linux-aio: explain why max batch is checked in laio_io_unplug()

Quoted 30 lines · Stefan Hajnoczi

It may not be obvious why laio_io_unplug() checks max batch. I discussed this with Stefano and have added a comment summarizing the reason.

Cc Stefano Garzarella <[email protected]>
Cc Kevin Wolf <[email protected]>

---
block/linux-aio.c | 6 ++++++

1 file changed, 6 insertions(+)
block/linux-aio.c +6 -0
index 6078da7e42..9c2393a2f7 100644
@@ -365,6 +365,12 @@ void laio_io_unplug(BlockDriverState *bs, LinuxAioState *s,
    assert(s->io_q.plugged);
    s->io_q.plugged--;
 
+    /*
+     * Why max batch checking is performed here:
+     * Another BDS may have queued requests with a higher dev_max_batch and
+     * therefore in_queue could now exceed our dev_max_batch. Re-check the max
+     * batch so we can honor our device's dev_max_batch.
+     */
    if (s->io_q.in_queue >= laio_max_batch(s, dev_max_batch) ||
        (!s->io_q.plugged &&
         !s->io_q.blocked && !QSIMPLEQ_EMPTY(&s->io_q.pending))) {
Signature · 1 line
2.36.1

I should have added that...

author Applied
#5 lore raw
In reply to Stefan Hajnoczi
Quoted 6 lines · Stefan Hajnoczi

An unlucky I/O pattern can result in stalled Linux AIO requests when the plugged counter becomes unbalanced. See Patch 1 for details.

Patch 2 adds a comment to explain why the laio_io_unplug() even checks max batch in the first place.

Quoted 10 lines

Stefan Hajnoczi (2):
linux-aio: fix unbalanced plugged counter in laio_io_unplug()
linux-aio: explain why max batch is checked in laio_io_unplug()

 block/linux-aio.c | 10 +++++++++-
 1 file changed, 9 insertions(+), 1 deletion(-)
Signature · 1 line
2.36.1

Thanks, applied to my block tree:
https://gitlab.com/stefanha/qemu/commits/block

Stefan

#6 lore raw

Thank you for finding this and fixing it. This issue has been giving us grief for months, and this patch appears to resolve the problem.

In our case, it seemed to have much greater severity with the RHEL / CentOS 7.x Linux 3.10 kernel when tied to SolidFire iSCSI based storage. This caused it to escape notice in our original soak period, and is likely a contributor to why others didn't encounter the problem. However, I believe this looks like a serious problem that could affect any guest machine that does a large amount of I/O. I believe the SolidFire connection may be that the I/O can queue up more easily than the local NVMe storage we also use, and there could be something related to the SolidFire QoS re-balancing where the iSCSI connection may be re-negotiated from time to time. So, I think this is more like "happens in some environments more than others", and unfortunately it happened a lot in one of our environments. :-(

Quoted 24 lines · Stefan Hajnoczi
Quoted 4 lines · Stefan Hajnoczi

An unlucky I/O pattern can result in stalled Linux AIO requests when the plugged counter becomes unbalanced. See Patch 1 for details.

Patch 2 adds a comment to explain why the laio_io_unplug() even checks

max

Quoted 6 lines

batch in the first place.

Stefan Hajnoczi (2):
linux-aio: fix unbalanced plugged counter in laio_io_unplug()
linux-aio: explain why max batch is checked in laio_io_unplug()

Quoted 6 lines
 block/linux-aio.c | 10 +++++++++-
 1 file changed, 9 insertions(+), 1 deletion(-)

--
2.36.1

Thanks, applied to my block tree:
https://gitlab.com/stefanha/qemu/commits/block

Stefan

Signature · 1 line
Mark Mielke <[email protected]>