Skip to contentSkip to conversation

ide: avoid unbounded recursion

PATCH In review Paolo Bonzini 3 participants 6 messages Jan 12, 2017

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 ide: avoid unbounded recursion

The end_transfer_func can call ide_transfer_start immediately, before returning, and unbounded recursion can happen at least for ide_atapi_cmd_reply_end. Use a bottom half to defer the call and limit stack usage.

Cc Peter Lieven <[email protected]>
Cc John Snow <[email protected]>
---
 hw/ide/core.c | 14 +++++++++++++-
 1 file changed, 13 insertions(+), 1 deletion(-)
hw/ide/core.c +13 -1
index 43709e5..7b9831f 100644
@@ -482,6 +482,13 @@ static void ide_clear_retry(IDEState *s)
     s->bus->retry_nsector = 0;
 }
 
+static void ide_start_transfer_bh_cb(void *opaque)
+{
+    IDEDMA *dma = opaque;
+
+    dma->ops->start_transfer(dma);
+}
+
 /* prepare data transfer and tell what to do after */
 void ide_transfer_start(IDEState *s, uint8_t *buf, int size,
                         EndTransferFunc *end_transfer_func)
@@ -494,7 +501,12 @@ void ide_transfer_start(IDEState *s, uint8_t *buf, int size,
         s->status |= DRQ_STAT;
     }
     if (s->bus->dma->ops->start_transfer) {
-        s->bus->dma->ops->start_transfer(s->bus->dma);
+        /* There can be unbounded recursion between ops->start_transfer
+         * and end_transfer_func, so defer to a bottom half.
+         */
+        aio_bh_schedule_oneshot(qemu_get_aio_context(),
+                                ide_start_transfer_bh_cb,
+                                s->bus->dma);
     }
 }
 
Signature · 1 line
2.9.3
#1 lore raw
Quoted 31 lines · Paolo Bonzini

The end_transfer_func can call ide_transfer_start immediately, before returning, and unbounded recursion can happen at least for ide_atapi_cmd_reply_end. Use a bottom half to defer the call and limit stack usage.

Cc Peter Lieven <[email protected]>
Cc John Snow <[email protected]>
---
 hw/ide/core.c | 14 +++++++++++++-
 1 file changed, 13 insertions(+), 1 deletion(-)
hw/ide/core.c +7 -0
index 43709e5..7b9831f 100644
@@ -482,6 +482,13 @@ static void ide_clear_retry(IDEState *s)
     s->bus->retry_nsector = 0;
 }
 
+static void ide_start_transfer_bh_cb(void *opaque)
+{
+    IDEDMA *dma = opaque;
+
+    dma->ops->start_transfer(dma);
+}
+
 /* prepare data transfer and tell what to do after */
 void ide_transfer_start(IDEState *s, uint8_t *buf, int size,
                         EndTransferFunc *end_transfer_func)

hw/ide/core.c void ide_transfer_start(IDEState *s, uint8_t *buf, int size, · line 509

Quoted 10 lines
hw/ide/core.c +6 -1
@@ -494,7 +501,12 @@ void ide_transfer_start(IDEState *s, uint8_t *buf, int size,
         s->status |= DRQ_STAT;
     }
     if (s->bus->dma->ops->start_transfer) {
-        s->bus->dma->ops->start_transfer(s->bus->dma);
+        /* There can be unbounded recursion between ops->start_transfer
+         * and end_transfer_func, so defer to a bottom half.
+         */
+        aio_bh_schedule_oneshot(qemu_get_aio_context(),
+                                ide_start_transfer_bh_cb,
+                                s->bus->dma);

Are you sure this is safe?

I wonder if there are races with device reset, vmsave, or vcpu hw register accesses.

Stefan

#2 lore raw
Quoted 45 lines · Stefan Hajnoczi
Quoted 31 lines · Paolo Bonzini

The end_transfer_func can call ide_transfer_start immediately, before returning, and unbounded recursion can happen at least for ide_atapi_cmd_reply_end. Use a bottom half to defer the call and limit stack usage.

Cc Peter Lieven <[email protected]>
Cc John Snow <[email protected]>
---
 hw/ide/core.c | 14 +++++++++++++-
 1 file changed, 13 insertions(+), 1 deletion(-)
hw/ide/core.c +7 -0
index 43709e5..7b9831f 100644
@@ -482,6 +482,13 @@ static void ide_clear_retry(IDEState *s)
     s->bus->retry_nsector = 0;
 }
 
+static void ide_start_transfer_bh_cb(void *opaque)
+{
+    IDEDMA *dma = opaque;
+
+    dma->ops->start_transfer(dma);
+}
+
 /* prepare data transfer and tell what to do after */
 void ide_transfer_start(IDEState *s, uint8_t *buf, int size,
                         EndTransferFunc *end_transfer_func)

hw/ide/core.c void ide_transfer_start(IDEState *s, uint8_t *buf, int size, · line 509

Quoted 10 lines
hw/ide/core.c +6 -1
@@ -494,7 +501,12 @@ void ide_transfer_start(IDEState *s, uint8_t *buf, int size,
         s->status |= DRQ_STAT;
     }
     if (s->bus->dma->ops->start_transfer) {
-        s->bus->dma->ops->start_transfer(s->bus->dma);
+        /* There can be unbounded recursion between ops->start_transfer
+         * and end_transfer_func, so defer to a bottom half.
+         */
+        aio_bh_schedule_oneshot(qemu_get_aio_context(),
+                                ide_start_transfer_bh_cb,
+                                s->bus->dma);

Are you sure this is safe?

Quoted 5 lines

I wonder if there are races with device reset, vmsave, or vcpu hw register accesses.

Stefan

Oh, right, reset -- we're guarded against normal accesses because the ATA device will be busy, but we're always allowed to reset.

Can we just amend the ide reset functionality to delete the BH if it hasn't triggered yet?

Also, I suppose drain doesn't clear out any scheduled bottom halves associated with the device, does it?

#3 lore raw
Quoted 51 lines · John Snow
Quoted 48 lines · Stefan Hajnoczi
Quoted 31 lines · Paolo Bonzini

The end_transfer_func can call ide_transfer_start immediately, before returning, and unbounded recursion can happen at least for ide_atapi_cmd_reply_end. Use a bottom half to defer the call and limit stack usage.

Cc Peter Lieven <[email protected]>
Cc John Snow <[email protected]>
---
 hw/ide/core.c | 14 +++++++++++++-
 1 file changed, 13 insertions(+), 1 deletion(-)
hw/ide/core.c +7 -0
index 43709e5..7b9831f 100644
@@ -482,6 +482,13 @@ static void ide_clear_retry(IDEState *s)
     s->bus->retry_nsector = 0;
 }
 
+static void ide_start_transfer_bh_cb(void *opaque)
+{
+    IDEDMA *dma = opaque;
+
+    dma->ops->start_transfer(dma);
+}
+
 /* prepare data transfer and tell what to do after */
 void ide_transfer_start(IDEState *s, uint8_t *buf, int size,
                         EndTransferFunc *end_transfer_func)

hw/ide/core.c void ide_transfer_start(IDEState *s, uint8_t *buf, int size, · line 509

Quoted 10 lines
hw/ide/core.c +6 -1
@@ -494,7 +501,12 @@ void ide_transfer_start(IDEState *s, uint8_t *buf, int size,
         s->status |= DRQ_STAT;
     }
     if (s->bus->dma->ops->start_transfer) {
-        s->bus->dma->ops->start_transfer(s->bus->dma);
+        /* There can be unbounded recursion between ops->start_transfer
+         * and end_transfer_func, so defer to a bottom half.
+         */
+        aio_bh_schedule_oneshot(qemu_get_aio_context(),
+                                ide_start_transfer_bh_cb,
+                                s->bus->dma);

Are you sure this is safe?

I wonder if there are races with device reset, vmsave, or vcpu hw register accesses.

Quoted 8 lines
Quoted 2 lines

Stefan

Oh, right, reset -- we're guarded against normal accesses because the ATA device will be busy, but we're always allowed to reset.

Can we just amend the ide reset functionality to delete the BH if it hasn't triggered yet?

Yes, but there's still migration.

Quoted 2 lines

Also, I suppose drain doesn't clear out any scheduled bottom halves associated with the device, does it?

You could use bdrv_inc/dec_in_flight, but I'm not suggesting that seriously.

Paolo

#4 lore raw
In reply to Stefan Hajnoczi
Quoted 45 lines · Stefan Hajnoczi
Quoted 31 lines · Paolo Bonzini

The end_transfer_func can call ide_transfer_start immediately, before returning, and unbounded recursion can happen at least for ide_atapi_cmd_reply_end. Use a bottom half to defer the call and limit stack usage.

Cc Peter Lieven <[email protected]>
Cc John Snow <[email protected]>
---
 hw/ide/core.c | 14 +++++++++++++-
 1 file changed, 13 insertions(+), 1 deletion(-)
hw/ide/core.c +7 -0
index 43709e5..7b9831f 100644
@@ -482,6 +482,13 @@ static void ide_clear_retry(IDEState *s)
     s->bus->retry_nsector = 0;
 }
 
+static void ide_start_transfer_bh_cb(void *opaque)
+{
+    IDEDMA *dma = opaque;
+
+    dma->ops->start_transfer(dma);
+}
+
 /* prepare data transfer and tell what to do after */
 void ide_transfer_start(IDEState *s, uint8_t *buf, int size,
                         EndTransferFunc *end_transfer_func)

hw/ide/core.c void ide_transfer_start(IDEState *s, uint8_t *buf, int size, · line 509

Quoted 10 lines
hw/ide/core.c +6 -1
@@ -494,7 +501,12 @@ void ide_transfer_start(IDEState *s, uint8_t *buf, int size,
         s->status |= DRQ_STAT;
     }
     if (s->bus->dma->ops->start_transfer) {
-        s->bus->dma->ops->start_transfer(s->bus->dma);
+        /* There can be unbounded recursion between ops->start_transfer
+         * and end_transfer_func, so defer to a bottom half.
+         */
+        aio_bh_schedule_oneshot(qemu_get_aio_context(),
+                                ide_start_transfer_bh_cb,
+                                s->bus->dma);

Are you sure this is safe?

Quoted 2 lines

I wonder if there are races with device reset, vmsave, or vcpu hw register accesses.

Register accesses are okay. For device reset and vmsave I had actually thought about that, but I had thought about it wrong.

Device reset and vmsave drain the BlockBackend, but there's nothing to drain for most PIO commands. Device reset could just cancel a (non-oneshot) bottom half, but I'm not sure of how to handle vmsave. Maybe do only part of start_transfer in the bottom half so that the destination has some condition to check (s->data_ptr < s->data_end?) and can reschedule the bottom half on the destination side depending on that condition?

Paolo

Reviewed-by on v1 1/1
#5 lore raw
In reply to Paolo Bonzini
Quoted 35 lines · Paolo Bonzini

The end_transfer_func can call ide_transfer_start immediately, before returning, and unbounded recursion can happen at least for ide_atapi_cmd_reply_end. Use a bottom half to defer the call and limit stack usage.

Cc Peter Lieven <[email protected]>
Cc John Snow <[email protected]>
---
 hw/ide/core.c | 14 +++++++++++++-
 1 file changed, 13 insertions(+), 1 deletion(-)
hw/ide/core.c +7 -0
index 43709e5..7b9831f 100644
@@ -482,6 +482,13 @@ static void ide_clear_retry(IDEState *s)
     s->bus->retry_nsector = 0;
 }
 
+static void ide_start_transfer_bh_cb(void *opaque)
+{
+    IDEDMA *dma = opaque;
+
+    dma->ops->start_transfer(dma);
+}
+
 /* prepare data transfer and tell what to do after */
 void ide_transfer_start(IDEState *s, uint8_t *buf, int size,
                         EndTransferFunc *end_transfer_func)
@@ -494,7 +501,12 @@ void ide_transfer_start(IDEState *s, uint8_t *buf, int size,
         s->status |= DRQ_STAT;
     }
     if (s->bus->dma->ops->start_transfer) {

hw/ide/core.c void ide_transfer_start(IDEState *s, uint8_t *buf, int size, · line 512

Quoted 10 lines
hw/ide/core.c +6 -1
-        s->bus->dma->ops->start_transfer(s->bus->dma);
+        /* There can be unbounded recursion between ops->start_transfer
+         * and end_transfer_func, so defer to a bottom half.
+         */
+        aio_bh_schedule_oneshot(qemu_get_aio_context(),
+                                ide_start_transfer_bh_cb,
+                                s->bus->dma);
     }
 }
 

Oh, is that all it took? Sorry for putting this off.