hw/ide/core: Fix possible crash via NULL pointer in ide_cancel_dma_sync()
PATCH v2 1/1 hw/ide/core: Fix possible crash via NULL pointer in ide_cancel_dma_sync()
From: Thomas Huth <[email protected]>
ide_cancel_dma_sync() is called with a "IDEState *s" for one of the two IDE drives on a bus (primary or secondary drive) to cancel all pending DMA transfers on the drive. The code then checks s->bus->dma->aiocb to see whether there is any IO in flight on the *bus* and then calls blk_drain(s->blk) to wait for its completion. However, s->bus->dma->aiocb might belong to the other drive on the bus, and if there is no disk attached to the current drive, s->blk is NULL. Since blk_drain() does not check its parameter for a NULL pointer, QEMU can crash in such a case.
To fix the problem, we have to check that "blk" is not NULL before calling blk_drain(). And we have to call blk_drain() for both drives, otherwise the assert(s->bus->dma->aiocb == NULL) statement after the blk_drain() might trigger if the IO in flight belongs to the the other drive.
Resolves: https://gitlab.com/qemu-project/qemu/-/work_items/905
Resolves: https://gitlab.com/qemu-project/qemu/-/work_items/4052
---
v2: Drain both drives
hw/ide/core.c | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-)
hw/ide/core.c +8 -1
@@ -741,10 +741,17 @@ void ide_cancel_dma_sync(IDEState *s)* In the future we'll be able to safely cancel the I/O if the * whole DMA operation will be submitted to disk with a single * aio operation with preadv/pwritev. +* +* Note: s->bus->dma->aiocb might belong to the adjacent IDEState, +* so we have to drain both drives to get it cleared. */ if (s->bus->dma->aiocb) {trace_ide_cancel_dma_sync_remaining (); -blk_drain (s->blk); +for (int i =0 ; i <2 ; i++) { +if (s->bus->ifs[i].blk) { +blk_drain (s->bus->ifs[i].blk); + } + }assert (s->bus->dma->aiocb ==NULL ); } }
Signature · 1 line
2.55.0
Quoted 42 lines · Thomas Huth
From: Thomas Huth <[email protected]>
ide_cancel_dma_sync()is called with a "IDEState *s" for one of the two IDE drives on a bus (primary or secondary drive) to cancel all pending DMA transfers on the drive. The code then checkss->bus->dma->aiocbto see whether there is any IO in flight on the *bus* and then callsblk_drain(s->blk)to wait for its completion. However,s->bus->dma->aiocbmight belong to the other drive on the bus, and if there is no disk attached to the current drive,s->blkis NULL. Sinceblk_drain()does not check its parameter for a NULL pointer, QEMU can crash in such a case.To fix the problem, we have to check that "blk" is not NULL before calling
blk_drain(). And we have to callblk_drain()for both drives, otherwise theassert(s->bus->dma->aiocb == NULL)statement after theblk_drain()might trigger if the IO in flight belongs to the the other drive.Reported-by Alexander Bulekov <[email protected]>Reported-by dong lingSigned-off-by Thomas Huth <[email protected]>---
v2: Drain both driveshw/ide/core.c | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-)hw/ide/core.c +3
-0@@ -741,10 +741,17 @@ void ide_cancel_dma_sync(IDEState *s)* In the future we'll be able to safely cancel the I/O if the * whole DMA operation will be submitted to disk with a single * aio operation with preadv/pwritev. +* +* Note: s->bus->dma->aiocb might belong to the adjacent IDEState, +* so we have to drain both drives to get it cleared. */ if (s->bus->dma->aiocb) {
hw/ide/core.c void ide_cancel_dma_sync(IDEState *s) · line 757
Quoted 10 lines
hw/ide/core.c +5
-1trace_ide_cancel_dma_sync_remaining (); -blk_drain (s->blk); +for (int i =0 ; i <2 ; i++) { +if (s->bus->ifs[i].blk) { +blk_drain (s->bus->ifs[i].blk); + } + }assert (s->bus->dma->aiocb ==NULL ); } }
Doesn't ide_bus_active_if() do the right thing here for in-flight requests? I'd expect to be able to do something like:
if (s->bus->dma->aiocb) {
trace_ide_cancel_dma_sync_remaining();
blk_drain(ide_bus_active_if(s->bus)->blk);
}ATB,
Mark.
Quoted 57 lines · Mark Cave-Ayland
Quoted 42 lines · Thomas Huth
From: Thomas Huth <[email protected]>
ide_cancel_dma_sync()is called with a "IDEState *s" for one of the two IDE drives on a bus (primary or secondary drive) to cancel all pending DMA transfers on the drive. The code then checkss->bus->dma->aiocbto see whether there is any IO in flight on the *bus* and then callsblk_drain(s->blk)to wait for its completion. However,s->bus->dma->aiocbmight belong to the other drive on the bus, and if there is no disk attached to the current drive,s->blkis NULL. Sinceblk_drain()does not check its parameter for a NULL pointer, QEMU can crash in such a case.To fix the problem, we have to check that "blk" is not NULL before calling
blk_drain(). And we have to callblk_drain()for both drives, otherwise theassert(s->bus->dma->aiocb == NULL)statement after theblk_drain()might trigger if the IO in flight belongs to the the other drive.Resolves: https://urldefense.proofpoint.com/v2/url? u=https-3A__gitlab.com_qemu-2Dproject_qemu_-2D_work-5Fitems_905&d=DwIDAg&c=s883GpUCOChKOHiocYtGcg&r=c23RpsaH4D2MKyD3EPJTDa0BAxz6tV8aUJqVSoytEiY&m=BbZFP82dEZMyWzN5GnJ9XaxwmyrmYGbqWbEBNSUM_afB5JRRF3InIaZAr5UpUUh2&s=rCgSz13wVfk11Z-Uw5WTQViAQKkSvwpQqf_dJX1tiro&e=
Reported-by Alexander Bulekov <[email protected]>Resolves: https://urldefense.proofpoint.com/v2/url? u=https-3A__gitlab.com_qemu-2Dproject_qemu_-2D_work-5Fitems_4052&d=DwIDAg&c=s883GpUCOChKOHiocYtGcg&r=c23RpsaH4D2MKyD3EPJTDa0BAxz6tV8aUJqVSoytEiY&m=BbZFP82dEZMyWzN5GnJ9XaxwmyrmYGbqWbEBNSUM_afB5JRRF3InIaZAr5UpUUh2&s=B-AkqMAsQwA0LzU5ptwPJ3ZIBAA8ncf5ZbQg8GTwaME&e=
Reported-by dong lingSigned-off-by Thomas Huth <[email protected]>---
v2: Drain both drives
hw/ide/core.c| 9 ++++++++-1 file changed, 8 insertions(+), 1 deletion(-)hw/ide/core.c +3
-0@@ -741,10 +741,17 @@ void ide_cancel_dma_sync(IDEState *s)* In the future we'll be able to safely cancel the I/O if the * whole DMA operation will be submitted to disk with a single * aio operation with preadv/pwritev. +* +* Note: s->bus->dma->aiocb might belong to the adjacent IDEState, +* so we have to drain both drives to get it cleared. */ if (s->bus->dma->aiocb) {hw/ide/core.c void ide_cancel_dma_sync(IDEState *s) · line 757
Quoted 10 lines
hw/ide/core.c +5
-1trace_ide_cancel_dma_sync_remaining(); - blk_drain(s->blk); + for (int i = 0; i < 2; i++) { + if (s->bus->ifs[i].blk) { + blk_drain(s->bus->ifs[i].blk); + } + } assert(s->bus->dma->aiocb == NULL); } }Doesn't
ide_bus_active_if()do the right thing here for in-flight requests? I'd expect to be able to do something like:
Quoted 4 lines
if (
s->bus->dma->aiocb) {
trace_ide_cancel_dma_sync_remaining();
blk_drain(ide_bus_active_if(s->bus)->blk);
}
Sounds appealing, so I gave it a try, but it does not seem to work: I can still trigger the problem with this code (both, the NULL pointer dereference and the assert() after the drain). Looks like it is possible to trigger a DMA transfer for the other drive, and then while the transfer is still in flight, switch the current drive and trigger the code path that calls ide_cancel_dma_sync(). So we need indeed to drain both drives, i.e. use this current patch here.
Thomas
Quoted 48 lines · Mark Cave-Ayland
Quoted 46 lines · Thomas Huth
From: Thomas Huth <[email protected]>
ide_cancel_dma_sync()is called with a "IDEState *s" for one of the two IDE drives on a bus (primary or secondary drive) to cancel all pending DMA transfers on the drive. The code then checkss->bus->dma->aiocbto see whether there is any IO in flight on the *bus* and then callsblk_drain(s->blk)to wait for its completion. However,s->bus->dma->aiocbmight belong to the other drive on the bus, and if there is no disk attached to the current drive,s->blkis NULL. Sinceblk_drain()does not check its parameter for a NULL pointer, QEMU can crash in such a case.To fix the problem, we have to check that "blk" is not NULL before calling
blk_drain(). And we have to callblk_drain()for both drives, otherwise theassert(s->bus->dma->aiocb == NULL)statement after theblk_drain()might trigger if the IO in flight belongs to the the other drive.Resolves: https://urldefense.proofpoint.com/v2/url? u=https-3A__gitlab.com_qemu-2Dproject_qemu_-2D_work-5Fitems_905&d=DwIDAg&c=s883GpUCOChKOHiocYtGcg&r=c23RpsaH4D2MKyD3EPJTDa0BAxz6tV8aUJqVSoytEiY&m=BbZFP82dEZMyWzN5GnJ9XaxwmyrmYGbqWbEBNSUM_afB5JRRF3InIaZAr5UpUUh2&s=rCgSz13wVfk11Z-Uw5WTQViAQKkSvwpQqf_dJX1tiro&e=
Reported-by Alexander Bulekov <[email protected]>Resolves: https://urldefense.proofpoint.com/v2/url? u=https-3A__gitlab.com_qemu-2Dproject_qemu_-2D_work-5Fitems_4052&d=DwIDAg&c=s883GpUCOChKOHiocYtGcg&r=c23RpsaH4D2MKyD3EPJTDa0BAxz6tV8aUJqVSoytEiY&m=BbZFP82dEZMyWzN5GnJ9XaxwmyrmYGbqWbEBNSUM_afB5JRRF3InIaZAr5UpUUh2&s=B-AkqMAsQwA0LzU5ptwPJ3ZIBAA8ncf5ZbQg8GTwaME&e=
Reported-by dong lingSigned-off-by Thomas Huth <[email protected]>---
v2: Drain both drives
hw/ide/core.c| 9 ++++++++-1 file changed, 8 insertions(+), 1 deletion(-)hw/ide/core.c +5
-1@@ -741,10 +741,17 @@ void ide_cancel_dma_sync(IDEState *s)* In the future we'll be able to safely cancel the I/O if the * whole DMA operation will be submitted to disk with a single * aio operation with preadv/pwritev. +* +* Note: s->bus->dma->aiocb might belong to the adjacent IDEState, +* so we have to drain both drives to get it cleared. */ if (s->bus->dma->aiocb) { trace_ide_cancel_dma_sync_remaining(); - blk_drain(s->blk); + for (int i = 0; i < 2; i++) { + if (s->bus->ifs[i].blk) {
Quoted 8 lines
hw/ide/core.c void ide_cancel_dma_sync(IDEState *s) · line 757
Quoted 6 lines
hw/ide/core.c +3
-0+ blk_drain(s->bus->ifs[i].blk); + } + } assert(s->bus->dma->aiocb == NULL); } }Doesn't
ide_bus_active_if()do the right thing here for in-flight requests?
bmdma_cmd_writeb() already calls ide_bus_active_if().
Is the issue in the caller, bmdma_cmd_writeb(), instead of here in ide_cancel_dma_sync()?
Quoted 13 lines
I'd expect to be able to do something like:
if (
s->bus->dma->aiocb) {
trace_ide_cancel_dma_sync_remaining();
blk_drain(ide_bus_active_if(s->bus)->blk);
}ATB,
Mark.
Quoted 58 lines · Philippe Mathieu-Daudé
Quoted 48 lines · Mark Cave-Ayland
Quoted 46 lines · Thomas Huth
From: Thomas Huth <[email protected]>
ide_cancel_dma_sync()is called with a "IDEState *s" for one of the two IDE drives on a bus (primary or secondary drive) to cancel all pending DMA transfers on the drive. The code then checkss->bus->dma->aiocbto see whether there is any IO in flight on the *bus* and then callsblk_drain(s->blk)to wait for its completion. However,s->bus->dma->aiocbmight belong to the other drive on the bus, and if there is no disk attached to the current drive,s->blkis NULL. Sinceblk_drain()does not check its parameter for a NULL pointer, QEMU can crash in such a case.To fix the problem, we have to check that "blk" is not NULL before calling
blk_drain(). And we have to callblk_drain()for both drives, otherwise theassert(s->bus->dma->aiocb == NULL)statement after theblk_drain()might trigger if the IO in flight belongs to the the other drive.Resolves: https://urldefense.proofpoint.com/v2/url? u=https-3A__gitlab.com_qemu-2Dproject_qemu_-2D_work-5Fitems_905&d=DwIDAg&c=s883GpUCOChKOHiocYtGcg&r=c23RpsaH4D2MKyD3EPJTDa0BAxz6tV8aUJqVSoytEiY&m=BbZFP82dEZMyWzN5GnJ9XaxwmyrmYGbqWbEBNSUM_afB5JRRF3InIaZAr5UpUUh2&s=rCgSz13wVfk11Z-Uw5WTQViAQKkSvwpQqf_dJX1tiro&e=
Reported-by Alexander Bulekov <[email protected]>Resolves: https://urldefense.proofpoint.com/v2/url? u=https-3A__gitlab.com_qemu-2Dproject_qemu_-2D_work-5Fitems_4052&d=DwIDAg&c=s883GpUCOChKOHiocYtGcg&r=c23RpsaH4D2MKyD3EPJTDa0BAxz6tV8aUJqVSoytEiY&m=BbZFP82dEZMyWzN5GnJ9XaxwmyrmYGbqWbEBNSUM_afB5JRRF3InIaZAr5UpUUh2&s=B-AkqMAsQwA0LzU5ptwPJ3ZIBAA8ncf5ZbQg8GTwaME&e=
Reported-by dong lingSigned-off-by Thomas Huth <[email protected]>---
v2: Drain both drives
hw/ide/core.c| 9 ++++++++-1 file changed, 8 insertions(+), 1 deletion(-)hw/ide/core.c +5
-1@@ -741,10 +741,17 @@ void ide_cancel_dma_sync(IDEState *s)* In the future we'll be able to safely cancel the I/O if the * whole DMA operation will be submitted to disk with a single * aio operation with preadv/pwritev. +* +* Note: s->bus->dma->aiocb might belong to the adjacent IDEState, +* so we have to drain both drives to get it cleared. */ if (s->bus->dma->aiocb) { trace_ide_cancel_dma_sync_remaining(); - blk_drain(s->blk); + for (int i = 0; i < 2; i++) { + if (s->bus->ifs[i].blk) {Quoted 8 lines
hw/ide/core.c void ide_cancel_dma_sync(IDEState *s) · line 757
Quoted 6 lines
hw/ide/core.c +3
-0+ blk_drain(s->bus->ifs[i].blk); + } + } assert(s->bus->dma->aiocb == NULL); } }Doesn't
ide_bus_active_if()do the right thing here for in-flight requests?
Quoted 4 lines
bmdma_cmd_writeb()already callside_bus_active_if().Is the issue in the caller,
bmdma_cmd_writeb(), instead of here inide_cancel_dma_sync()?
i.e.:
-- >8 --
hw/ide/pci.c +1 -1
@@ -384,7 +384,7 @@ void bmdma_cmd_writeb(BMDMAState *bm, uint32_t val)/* Ignore writes to SSBM if it keeps the old value */ if ((val &BM_CMD_START ) != (bm->cmd &BM_CMD_START )) {if (!(val &BM_CMD_START )) { -ide_cancel_dma_sync (ide_bus_active_if (bm->bus)); +ide_cancel_dma_sync (bmdma_active_if (bm)); bm->status &= ~BM_STATUS_DMAING ; }else { bm->cur_addr = bm->addr;
---
Quoted 16 lines
Quoted 14 lines
I'd expect to be able to do something like:
if (
s->bus->dma->aiocb) {
trace_ide_cancel_dma_sync_remaining();
blk_drain(ide_bus_active_if(s->bus)->blk);
}ATB,
Mark.
Quoted 72 lines · Philippe Mathieu-Daudé
Quoted 58 lines · Philippe Mathieu-Daudé
Quoted 48 lines · Mark Cave-Ayland
Quoted 46 lines · Thomas Huth
From: Thomas Huth <[email protected]>
ide_cancel_dma_sync()is called with a "IDEState *s" for one of the two IDE drives on a bus (primary or secondary drive) to cancel all pending DMA transfers on the drive. The code then checkss->bus->dma->aiocbto see whether there is any IO in flight on the *bus* and then callsblk_drain(s->blk)to wait for its completion. However,s->bus->dma->aiocbmight belong to the other drive on the bus, and if there is no disk attached to the current drive,s->blkis NULL. Sinceblk_drain()does not check its parameter for a NULL pointer, QEMU can crash in such a case.To fix the problem, we have to check that "blk" is not NULL before calling
blk_drain(). And we have to callblk_drain()for both drives, otherwise theassert(s->bus->dma->aiocb == NULL)statement after theblk_drain()might trigger if the IO in flight belongs to the the other drive.Resolves: https://urldefense.proofpoint.com/v2/url? u=https-3A__gitlab.com_qemu-2Dproject_qemu_-2D_work-5Fitems_905&d=DwIDAg&c=s883GpUCOChKOHiocYtGcg&r=c23RpsaH4D2MKyD3EPJTDa0BAxz6tV8aUJqVSoytEiY&m=BbZFP82dEZMyWzN5GnJ9XaxwmyrmYGbqWbEBNSUM_afB5JRRF3InIaZAr5UpUUh2&s=rCgSz13wVfk11Z-Uw5WTQViAQKkSvwpQqf_dJX1tiro&e=
Reported-by Alexander Bulekov <[email protected]>Resolves: https://urldefense.proofpoint.com/v2/url? u=https-3A__gitlab.com_qemu-2Dproject_qemu_-2D_work-5Fitems_4052&d=DwIDAg&c=s883GpUCOChKOHiocYtGcg&r=c23RpsaH4D2MKyD3EPJTDa0BAxz6tV8aUJqVSoytEiY&m=BbZFP82dEZMyWzN5GnJ9XaxwmyrmYGbqWbEBNSUM_afB5JRRF3InIaZAr5UpUUh2&s=B-AkqMAsQwA0LzU5ptwPJ3ZIBAA8ncf5ZbQg8GTwaME&e=
Reported-by dong lingSigned-off-by Thomas Huth <[email protected]>---
v2: Drain both drives
hw/ide/core.c| 9 ++++++++-1 file changed, 8 insertions(+), 1 deletion(-)hw/ide/core.c +5
-1@@ -741,10 +741,17 @@ void ide_cancel_dma_sync(IDEState *s)* In the future we'll be able to safely cancel the I/O if the * whole DMA operation will be submitted to disk with a single * aio operation with preadv/pwritev. +* +* Note: s->bus->dma->aiocb might belong to the adjacent IDEState, +* so we have to drain both drives to get it cleared. */ if (s->bus->dma->aiocb) { trace_ide_cancel_dma_sync_remaining(); - blk_drain(s->blk); + for (int i = 0; i < 2; i++) { + if (s->bus->ifs[i].blk) {Quoted 8 lines
hw/ide/core.c void ide_cancel_dma_sync(IDEState *s) · line 757
Quoted 6 lines
hw/ide/core.c +3
-0+ blk_drain(s->bus->ifs[i].blk); + } + } assert(s->bus->dma->aiocb == NULL); } }Doesn't
ide_bus_active_if()do the right thing here for in-flight requests?Quoted 4 lines
bmdma_cmd_writeb()already callside_bus_active_if().Is the issue in the caller,
bmdma_cmd_writeb(), instead of here inide_cancel_dma_sync()?i.e.:
-- >8 --
hw/ide/pci.c +0
-0
hw/ide/pci.c void bmdma_cmd_writeb(BMDMAState *bm, uint32_t val) · line 386
Quoted 3 lines
hw/ide/pci.c +0
-0@@ -384,7 +384,7 @@ void bmdma_cmd_writeb(BMDMAState *bm, uint32_t val) /* Ignore writes to SSBM if it keeps the old value */ if ((val & BM_CMD_START) != (bm->cmd & BM_CMD_START)) { if (!(val & BM_CMD_START)) {
Also, shouldn't be calling ide_transfer_halt() first?
(Without IDE expert it seems late to get a fix for the release...)
Quoted 25 lines
hw/ide/pci.c +1
-2- ide_cancel_dma_sync(ide_bus_active_if(bm->bus)); + ide_cancel_dma_sync(bmdma_active_if(bm)); bm->status &= ~BM_STATUS_DMAING; } else { bm->cur_addr = bm->addr; ---Quoted 17 lines
Quoted 15 lines
I'd expect to be able to do something like:
if (
s->bus->dma->aiocb) {
trace_ide_cancel_dma_sync_remaining();
blk_drain(ide_bus_active_if(s->bus)->blk);
}ATB,
Mark.
Quoted 72 lines · Philippe Mathieu-Daudé
Quoted 58 lines · Philippe Mathieu-Daudé
Quoted 48 lines · Mark Cave-Ayland
Quoted 46 lines · Thomas Huth
From: Thomas Huth <[email protected]>
ide_cancel_dma_sync()is called with a "IDEState *s" for one of the two IDE drives on a bus (primary or secondary drive) to cancel all pending DMA transfers on the drive. The code then checkss->bus->dma->aiocbto see whether there is any IO in flight on the *bus* and then callsblk_drain(s->blk)to wait for its completion. However,s->bus->dma->aiocbmight belong to the other drive on the bus, and if there is no disk attached to the current drive,s->blkis NULL. Sinceblk_drain()does not check its parameter for a NULL pointer, QEMU can crash in such a case.To fix the problem, we have to check that "blk" is not NULL before calling
blk_drain(). And we have to callblk_drain()for both drives, otherwise theassert(s->bus->dma->aiocb == NULL)statement after theblk_drain()might trigger if the IO in flight belongs to the the other drive.Resolves: https://urldefense.proofpoint.com/v2/url? u=https-3A__gitlab.com_qemu-2Dproject_qemu_-2D_work-5Fitems_905&d=DwIDAg&c=s883GpUCOChKOHiocYtGcg&r=c23RpsaH4D2MKyD3EPJTDa0BAxz6tV8aUJqVSoytEiY&m=BbZFP82dEZMyWzN5GnJ9XaxwmyrmYGbqWbEBNSUM_afB5JRRF3InIaZAr5UpUUh2&s=rCgSz13wVfk11Z-Uw5WTQViAQKkSvwpQqf_dJX1tiro&e=
Reported-by Alexander Bulekov <[email protected]>Resolves: https://urldefense.proofpoint.com/v2/url? u=https-3A__gitlab.com_qemu-2Dproject_qemu_-2D_work-5Fitems_4052&d=DwIDAg&c=s883GpUCOChKOHiocYtGcg&r=c23RpsaH4D2MKyD3EPJTDa0BAxz6tV8aUJqVSoytEiY&m=BbZFP82dEZMyWzN5GnJ9XaxwmyrmYGbqWbEBNSUM_afB5JRRF3InIaZAr5UpUUh2&s=B-AkqMAsQwA0LzU5ptwPJ3ZIBAA8ncf5ZbQg8GTwaME&e=
Reported-by dong lingSigned-off-by Thomas Huth <[email protected]>---
v2: Drain both drives
hw/ide/core.c| 9 ++++++++-1 file changed, 8 insertions(+), 1 deletion(-)hw/ide/core.c +5
-1@@ -741,10 +741,17 @@ void ide_cancel_dma_sync(IDEState *s)* In the future we'll be able to safely cancel the I/O if the * whole DMA operation will be submitted to disk with a single * aio operation with preadv/pwritev. +* +* Note: s->bus->dma->aiocb might belong to the adjacent IDEState, +* so we have to drain both drives to get it cleared. */ if (s->bus->dma->aiocb) { trace_ide_cancel_dma_sync_remaining(); - blk_drain(s->blk); + for (int i = 0; i < 2; i++) { + if (s->bus->ifs[i].blk) {Quoted 8 lines
hw/ide/core.c void ide_cancel_dma_sync(IDEState *s) · line 757
Quoted 6 lines
hw/ide/core.c +3
-0+ blk_drain(s->bus->ifs[i].blk); + } + } assert(s->bus->dma->aiocb == NULL); } }Doesn't
ide_bus_active_if()do the right thing here for in-flight requests?Quoted 4 lines
bmdma_cmd_writeb()already callside_bus_active_if().Is the issue in the caller,
bmdma_cmd_writeb(), instead of here inide_cancel_dma_sync()?i.e.:
-- >8 --
hw/ide/pci.c +0
-0
hw/ide/pci.c void bmdma_cmd_writeb(BMDMAState *bm, uint32_t val) · line 390
Quoted 8 lines
hw/ide/pci.c +1
-1@@ -384,7 +384,7 @@ void bmdma_cmd_writeb(BMDMAState *bm, uint32_t val) /* Ignore writes to SSBM if it keeps the old value */ if ((val & BM_CMD_START) != (bm->cmd & BM_CMD_START)) { if (!(val & BM_CMD_START)) { - ide_cancel_dma_sync(ide_bus_active_if(bm->bus)); + ide_cancel_dma_sync(bmdma_active_if(bm)); bm->status &= ~BM_STATUS_DMAING; } else { bm->cur_addr = bm->addr;
No, I don't think this will fix all cases. While https://gitlab.com/qemu-project/qemu/-/work_items/905 has a call trace with bmdma_cmd_writeb() in it, https://gitlab.com/qemu-project/qemu/-/work_items/4052 is slightly different: The backtrace shows ide_bus_perform_srst() -> ide_perform_srst() -> ide_cancel_dma_sync() here instead. I'd say let's play safe and always drain both interfaces. WDYT?
Thomas
Quoted 68 lines · Thomas Huth
Quoted 67 lines · Philippe Mathieu-Daudé
Quoted 59 lines · Philippe Mathieu-Daudé
Quoted 49 lines · Mark Cave-Ayland
Quoted 47 lines · Thomas Huth
From: Thomas Huth <[email protected]>
ide_cancel_dma_sync()is called with a "IDEState *s" for one of the two IDE drives on a bus (primary or secondary drive) to cancel all pending DMA transfers on the drive. The code then checkss->bus->dma->aiocbto see whether there is any IO in flight on the *bus* and then callsblk_drain(s->blk)to wait for its completion. However,s->bus->dma->aiocbmight belong to the other drive on the bus, and if there is no disk attached to the current drive,s->blkis NULL. Sinceblk_drain()does not check its parameter for a NULL pointer, QEMU can crash in such a case.To fix the problem, we have to check that "blk" is not NULL before calling
blk_drain(). And we have to callblk_drain()for both drives, otherwise theassert(s->bus->dma->aiocb == NULL)statement after theblk_drain()might trigger if the IO in flight belongs to the the other drive.Resolves: https://urldefense.proofpoint.com/v2/url? u=https-3A__gitlab.com_qemu-2Dproject_qemu_-2D_work-5Fitems_905&d=DwIDAg&c=s883GpUCOChKOHiocYtGcg&r=c23RpsaH4D2MKyD3EPJTDa0BAxz6tV8aUJqVSoytEiY&m=BbZFP82dEZMyWzN5GnJ9XaxwmyrmYGbqWbEBNSUM_afB5JRRF3InIaZAr5UpUUh2&s=rCgSz13wVfk11Z-Uw5WTQViAQKkSvwpQqf_dJX1tiro&e=
Reported-by Alexander Bulekov <[email protected]>Resolves: https://urldefense.proofpoint.com/v2/url? u=https-3A__gitlab.com_qemu-2Dproject_qemu_-2D_work-5Fitems_4052&d=DwIDAg&c=s883GpUCOChKOHiocYtGcg&r=c23RpsaH4D2MKyD3EPJTDa0BAxz6tV8aUJqVSoytEiY&m=BbZFP82dEZMyWzN5GnJ9XaxwmyrmYGbqWbEBNSUM_afB5JRRF3InIaZAr5UpUUh2&s=B-AkqMAsQwA0LzU5ptwPJ3ZIBAA8ncf5ZbQg8GTwaME&e=
Reported-by dong lingSigned-off-by Thomas Huth <[email protected]>---
v2: Drain both drives
hw/ide/core.c| 9 ++++++++-1 file changed, 8 insertions(+), 1 deletion(-)hw/ide/core.c +5
-1@@ -741,10 +741,17 @@ void ide_cancel_dma_sync(IDEState *s)* In the future we'll be able to safely cancel the I/O if the * whole DMA operation will be submitted to disk with a single * aio operation with preadv/pwritev. +* +* Note: s->bus->dma->aiocb might belong to the adjacent IDEState, +* so we have to drain both drives to get it cleared. */ if (s->bus->dma->aiocb) { trace_ide_cancel_dma_sync_remaining(); - blk_drain(s->blk); + for (int i = 0; i < 2; i++) { + if (s->bus->ifs[i].blk) {Quoted 8 lines
Quoted 6 lines
hw/ide/core.c +3
-0+ blk_drain(s->bus->ifs[i].blk); + } + } assert(s->bus->dma->aiocb == NULL); }}
Doesn't
ide_bus_active_if()do the right thing here for in-flight requests?Quoted 4 lines
bmdma_cmd_writeb()already callside_bus_active_if().Is the issue in the caller,
bmdma_cmd_writeb(), instead of here inide_cancel_dma_sync()?i.e.:
Quoted 15 lines
Quoted 6 lines
-- >8 --
hw/ide/pci.c +0
-0hw/ide/pci.c void bmdma_cmd_writeb(BMDMAState *bm, uint32_t val) · line 390
Quoted 8 lines
hw/ide/pci.c +1
-1@@ -384,7 +384,7 @@ void bmdma_cmd_writeb(BMDMAState *bm, uint32_t val) /* Ignore writes to SSBM if it keeps the old value */ if ((val & BM_CMD_START) != (bm->cmd & BM_CMD_START)) { if (!(val & BM_CMD_START)) { - ide_cancel_dma_sync(ide_bus_active_if(bm->bus)); + ide_cancel_dma_sync(bmdma_active_if(bm)); bm->status &= ~BM_STATUS_DMAING; } else { bm->cur_addr = bm->addr;No, I don't think this will fix all cases. While https://gitlab.com/ qemu-project/qemu/-/work_items/905 has a call trace with
bmdma_cmd_writeb()in it, https://gitlab.com/qemu-project/qemu/-/ work_items/4052 is slightly different: The backtrace showside_bus_perform_srst()->ide_perform_srst()->ide_cancel_dma_sync()here instead. I'd say let's play safe and always drain both interfaces. WDYT?
I dunno I'm kind of lost. Can't blk_bs() handle NULL blk?
-- >8 --
block/block-backend.c +1 -2
@@ -760,7 +760,7 @@ BlockBackend *blk_by_name(const char *name)BlockDriverState *blk_bs (BlockBackend *blk) {IO_CODE (); -return blk->root ? blk->root->bs :NULL ; +return (blk && blk->root) ? blk->root->bs :NULL ; } ---
Quoted 92 lines · Philippe Mathieu-Daudé
Quoted 67 lines · Thomas Huth
Quoted 66 lines · Philippe Mathieu-Daudé
Quoted 58 lines · Philippe Mathieu-Daudé
Quoted 48 lines · Mark Cave-Ayland
Quoted 46 lines · Thomas Huth
From: Thomas Huth <[email protected]>
ide_cancel_dma_sync()is called with a "IDEState *s" for one of the two IDE drives on a bus (primary or secondary drive) to cancel all pending DMA transfers on the drive. The code then checkss->bus->dma->aiocbto see whether there is any IO in flight on the *bus* and then callsblk_drain(s->blk)to wait for its completion. However,s->bus->dma->aiocbmight belong to the other drive on the bus, and if there is no disk attached to the current drive,s->blkis NULL. Sinceblk_drain()does not check its parameter for a NULL pointer, QEMU can crash in such a case.To fix the problem, we have to check that "blk" is not NULL before calling
blk_drain(). And we have to callblk_drain()for both drives, otherwise theassert(s->bus->dma->aiocb == NULL)statement after theblk_drain()might trigger if the IO in flight belongs to the the other drive.Resolves: https://urldefense.proofpoint.com/v2/url? u=https-3A__gitlab.com_qemu-2Dproject_qemu_-2D_work-5Fitems_905&d=DwIDAg&c=s883GpUCOChKOHiocYtGcg&r=c23RpsaH4D2MKyD3EPJTDa0BAxz6tV8aUJqVSoytEiY&m=BbZFP82dEZMyWzN5GnJ9XaxwmyrmYGbqWbEBNSUM_afB5JRRF3InIaZAr5UpUUh2&s=rCgSz13wVfk11Z-Uw5WTQViAQKkSvwpQqf_dJX1tiro&e=
Reported-by Alexander Bulekov <[email protected]>Resolves: https://urldefense.proofpoint.com/v2/url? u=https-3A__gitlab.com_qemu-2Dproject_qemu_-2D_work-5Fitems_4052&d=DwIDAg&c=s883GpUCOChKOHiocYtGcg&r=c23RpsaH4D2MKyD3EPJTDa0BAxz6tV8aUJqVSoytEiY&m=BbZFP82dEZMyWzN5GnJ9XaxwmyrmYGbqWbEBNSUM_afB5JRRF3InIaZAr5UpUUh2&s=B-AkqMAsQwA0LzU5ptwPJ3ZIBAA8ncf5ZbQg8GTwaME&e=
Reported-by dong lingSigned-off-by Thomas Huth <[email protected]>---
v2: Drain both drives
hw/ide/core.c| 9 ++++++++-1 file changed, 8 insertions(+), 1 deletion(-)hw/ide/core.c +5
-1@@ -741,10 +741,17 @@ void ide_cancel_dma_sync(IDEState *s)* In the future we'll be able to safely cancel the I/O if the * whole DMA operation will be submitted to disk with a single * aio operation with preadv/pwritev. +* +* Note: s->bus->dma->aiocb might belong to the adjacent IDEState, +* so we have to drain both drives to get it cleared. */ if (s->bus->dma->aiocb) { trace_ide_cancel_dma_sync_remaining(); - blk_drain(s->blk); + for (int i = 0; i < 2; i++) { + if (s->bus->ifs[i].blk) {Quoted 8 lines
hw/ide/core.c void ide_cancel_dma_sync(IDEState *s) · line 757
Quoted 6 lines
hw/ide/core.c +3
-0+ blk_drain(s->bus->ifs[i].blk); + } + } assert(s->bus->dma->aiocb == NULL); } }Doesn't
ide_bus_active_if()do the right thing here for in-flight requests?Quoted 4 lines
bmdma_cmd_writeb()already callside_bus_active_if().Is the issue in the caller,
bmdma_cmd_writeb(), instead of here inide_cancel_dma_sync()?i.e.:
Quoted 15 lines
Quoted 6 lines
-- >8 --
hw/ide/pci.c +0
-0hw/ide/pci.c void bmdma_cmd_writeb(BMDMAState *bm, uint32_t val) · line 390
Quoted 8 lines
hw/ide/pci.c +1
-1@@ -384,7 +384,7 @@ void bmdma_cmd_writeb(BMDMAState *bm, uint32_t val) /* Ignore writes to SSBM if it keeps the old value */ if ((val & BM_CMD_START) != (bm->cmd & BM_CMD_START)) { if (!(val & BM_CMD_START)) { - ide_cancel_dma_sync(ide_bus_active_if(bm->bus)); + ide_cancel_dma_sync(bmdma_active_if(bm)); bm->status &= ~BM_STATUS_DMAING; } else { bm->cur_addr = bm->addr;No, I don't think this will fix all cases. While https://gitlab.com/ qemu- project/qemu/-/work_items/905 has a call trace with
bmdma_cmd_writeb()in it, https://gitlab.com/qemu-project/qemu/-/ work_items/4052 is slightly different: The backtrace showside_bus_perform_srst()->ide_perform_srst()->ide_cancel_dma_sync()here instead. I'd say let's play safe and always drain both interfaces. WDYT?I dunno I'm kind of lost. Can't
blk_bs()handle NULL blk?-- >8 --
block/block-backend.c +0
-0
block/block-backend.c BlockBackend *blk_by_name(const char *name) · line 764
Quoted 6 lines
block/block-backend.c +1
-1@@ -760,7 +760,7 @@ BlockBackend *blk_by_name(const char *name) BlockDriverState *blk_bs(BlockBackend *blk) { IO_CODE(); - return blk->root ? blk->root->bs : NULL; + return (blk && blk->root) ? blk->root->bs : NULL; }
That would maybe help with the NULL pointer crash, but it would not prevent the assert(s->bus->dma->aiocb == NULL) from triggering.
Simply draining both drives seems to be the only solution that I see right now, unless we want to remove the assert() statement?
Thomas
Quoted 102 lines · Thomas Huth
Quoted 93 lines · Philippe Mathieu-Daudé
Quoted 68 lines · Thomas Huth
Quoted 67 lines · Philippe Mathieu-Daudé
Quoted 59 lines · Philippe Mathieu-Daudé
Quoted 49 lines · Mark Cave-Ayland
Quoted 47 lines · Thomas Huth
From: Thomas Huth <[email protected]>
ide_cancel_dma_sync()is called with a "IDEState *s" for one of the two IDE drives on a bus (primary or secondary drive) to cancel all pending DMA transfers on the drive. The code then checkss->bus->dma->aiocbto see whether there is any IO in flight on the *bus* and then callsblk_drain(s->blk)to wait for its completion. However,s->bus->dma->aiocbmight belong to the other drive on the bus, and if there is no disk attached to the current drive,s->blkis NULL. Sinceblk_drain()does not check its parameter for a NULL pointer, QEMU can crash in such a case.To fix the problem, we have to check that "blk" is not NULL before calling
blk_drain(). And we have to callblk_drain()for both drives, otherwise theassert(s->bus->dma->aiocb == NULL)statement after theblk_drain()might trigger if the IO in flight belongs to the the other drive.Resolves: https://urldefense.proofpoint.com/v2/url? u=https-3A__gitlab.com_qemu-2Dproject_qemu_-2D_work-5Fitems_905&d=DwIDAg&c=s883GpUCOChKOHiocYtGcg&r=c23RpsaH4D2MKyD3EPJTDa0BAxz6tV8aUJqVSoytEiY&m=BbZFP82dEZMyWzN5GnJ9XaxwmyrmYGbqWbEBNSUM_afB5JRRF3InIaZAr5UpUUh2&s=rCgSz13wVfk11Z-Uw5WTQViAQKkSvwpQqf_dJX1tiro&e=
Reported-by Alexander Bulekov <[email protected]>Resolves: https://urldefense.proofpoint.com/v2/url? u=https-3A__gitlab.com_qemu-2Dproject_qemu_-2D_work-5Fitems_4052&d=DwIDAg&c=s883GpUCOChKOHiocYtGcg&r=c23RpsaH4D2MKyD3EPJTDa0BAxz6tV8aUJqVSoytEiY&m=BbZFP82dEZMyWzN5GnJ9XaxwmyrmYGbqWbEBNSUM_afB5JRRF3InIaZAr5UpUUh2&s=B-AkqMAsQwA0LzU5ptwPJ3ZIBAA8ncf5ZbQg8GTwaME&e=
Reported-by dong lingSigned-off-by Thomas Huth <[email protected]>---
v2: Drain both drives
hw/ide/core.c| 9 ++++++++-1 file changed, 8 insertions(+), 1 deletion(-)hw/ide/core.c +5
-1@@ -741,10 +741,17 @@ void ide_cancel_dma_sync(IDEState *s)* In the future we'll be able to safely cancel the I/O if the * whole DMA operation will be submitted to disk with a single * aio operation with preadv/pwritev. +* +* Note: s->bus->dma->aiocb might belong to the adjacent IDEState, +* so we have to drain both drives to get it cleared. */ if (s->bus->dma->aiocb) { trace_ide_cancel_dma_sync_remaining(); - blk_drain(s->blk); + for (int i = 0; i < 2; i++) { + if (s->bus->ifs[i].blk) {Quoted 8 lines
Quoted 6 lines
hw/ide/core.c +3
-0+ blk_drain(s->bus->ifs[i].blk); + } + } assert(s->bus->dma->aiocb == NULL); }}
Doesn't
ide_bus_active_if()do the right thing here for in-flight requests?Quoted 4 lines
bmdma_cmd_writeb()already callside_bus_active_if().Is the issue in the caller,
bmdma_cmd_writeb(), instead of here inide_cancel_dma_sync()?i.e.:
Quoted 15 lines
Quoted 6 lines
-- >8 --
hw/ide/pci.c +0
-0hw/ide/pci.c void bmdma_cmd_writeb(BMDMAState *bm, uint32_t val) · line 390
Quoted 8 lines
hw/ide/pci.c +1
-1@@ -384,7 +384,7 @@ void bmdma_cmd_writeb(BMDMAState *bm, uint32_t val) /* Ignore writes to SSBM if it keeps the old value */ if ((val & BM_CMD_START) != (bm->cmd & BM_CMD_START)) { if (!(val & BM_CMD_START)) { - ide_cancel_dma_sync(ide_bus_active_if(bm->bus)); + ide_cancel_dma_sync(bmdma_active_if(bm)); bm->status &= ~BM_STATUS_DMAING; } else { bm->cur_addr = bm->addr;No, I don't think this will fix all cases. While https://gitlab.com/ qemu- project/qemu/-/work_items/905 has a call trace with
bmdma_cmd_writeb()in it, https://gitlab.com/qemu-project/qemu/-/ work_items/4052 is slightly different: The backtrace showside_bus_perform_srst()->ide_perform_srst()->ide_cancel_dma_sync()here instead. I'd say let's play safe and always drain both interfaces. WDYT?I dunno I'm kind of lost. Can't
blk_bs()handle NULL blk?-- >8 --
block/block-backend.c +0
-0block/block-backend.c BlockBackend *blk_by_name(const char *name) · line 764
Quoted 6 lines
block/block-backend.c +1
-1@@ -760,7 +760,7 @@ BlockBackend *blk_by_name(const char *name) BlockDriverState *blk_bs(BlockBackend *blk) { IO_CODE(); - return blk->root ? blk->root->bs : NULL; + return (blk && blk->root) ? blk->root->bs : NULL; }That would maybe help with the NULL pointer crash, but it would not prevent the
assert(s->bus->dma->aiocb == NULL)from triggering.
Quoted 1 line
Simply draining both drives seems to be the only solution that I see right now, unless we want to remove the
assert()statement?
OK... Maybe move the comment before the for() for clarity. Still some review from qemu-block@ team would help...
Quoted 4 lines
Thomas