hw/ide/ahci.c | 17 ++++++++++++++--- hw/ide/ich.c | 8 ++++++++ include/hw/ide/ahci.h | 1 + 3 files changed, 23 insertions(+), 3 deletions(-)
When a Windows guest puts an idle SATA drive into standby, it disables
PCI bus mastering on the AHCI controller but fails to clear the port
engine bits in the port command register. During post-migration
handling, QEMU’s ahci_state_post_load function checks only these port
engine bits to determine whether to start the engines and map the FIS
receive buffer (PxFB) and Command List buffer (PxCLB). Because PCI bus
mastering is disabled, DMA access to guest memory is blocked.
Consequently, the memory mapping requests fail or incorrectly route
through bounce buffers instead of direct guest memory, causing the
destination QEMU process to abort the migration.
Starting DMA/FIS engines when bus mastering is disabled might lead to
buffer mapping failures or improper bounce buffer allocations. Do not
start the engines in the aforementioned scenario. When the guest
re-enables bus mastering to access an idle drive, it should reset the
port engine, safely allocating the buffers and starting the engines.
Reported-by: andychiu <andychiu@synology.com>
Suggested-by: Michael S. Tsirkin <mst@redhat.com>
Suggested-by: John Snow <jsnow@redhat.com>
Link: https://lists.gnu.org/archive/html/qemu-devel/2019-09/msg01544.html
Signed-off-by: Abhisek Panda <abhisek.panda1@nutanix.com>
---
hw/ide/ahci.c | 17 ++++++++++++++---
hw/ide/ich.c | 8 ++++++++
include/hw/ide/ahci.h | 1 +
3 files changed, 23 insertions(+), 3 deletions(-)
diff --git a/hw/ide/ahci.c b/hw/ide/ahci.c
index 86f6297dc9..5f842eff13 100644
--- a/hw/ide/ahci.c
+++ b/hw/ide/ahci.c
@@ -172,6 +172,11 @@ static uint32_t ahci_port_read(AHCIState *s, int port, int offset)
return val;
}
+static inline int ahci_bus_master_enabled(AHCIState *s)
+{
+ return !s->bus_master_enabled || s->bus_master_enabled(s);
+}
+
static void ahci_check_irq(AHCIState *s)
{
int i;
@@ -1625,6 +1630,8 @@ static const IDEDMAOps ahci_dma_ops = {
void ahci_init(AHCIState *s, DeviceState *qdev)
{
+ s->bus_master_enabled = NULL;
+
/* XXX BAR size should be 1k, but that breaks, so bump it to 4k for now */
memory_region_init_io(&s->mem, OBJECT(qdev), &ahci_mem_ops, s,
"ahci", AHCI_MEM_BAR_SIZE);
@@ -1792,10 +1799,14 @@ static int ahci_state_post_load(void *opaque, int version_id)
return -1;
}
- /* After a migrate, the DMA/FIS engines are "off" and
- * need to be conditionally restarted */
+ /*
+ * After a migrate, the DMA/FIS engines are "off" and need to be
+ * conditionally restarted. If PCI bus mastering is disabled, leave
+ * the engines stopped because DMA is inactive.
+ */
pr->cmd &= ~(PORT_CMD_LIST_ON | PORT_CMD_FIS_ON);
- if (ahci_cond_start_engines(ad) != 0) {
+ if (ahci_bus_master_enabled(s) &&
+ ahci_cond_start_engines(ad) != 0) {
return -1;
}
diff --git a/hw/ide/ich.c b/hw/ide/ich.c
index b00987f08d..8e8c2e108d 100644
--- a/hw/ide/ich.c
+++ b/hw/ide/ich.c
@@ -110,6 +110,13 @@ static void pci_ich9_reset(DeviceState *dev)
ahci_reset(&d->ahci);
}
+static bool pci_ich9_ahci_bus_master_enabled(AHCIState *s)
+{
+ AHCIPCIState *d = container_of(s, AHCIPCIState, ahci);
+
+ return d->parent_obj.is_master;
+}
+
static void pci_ich9_ahci_init(Object *obj)
{
AHCIPCIState *d = ICH9_AHCI(obj);
@@ -117,6 +124,7 @@ static void pci_ich9_ahci_init(Object *obj)
qemu_init_irq_child(obj, "update-irq", &d->irq,
pci_ich9_ahci_update_irq, d, 0);
ahci_init(&d->ahci, DEVICE(obj));
+ d->ahci.bus_master_enabled = pci_ich9_ahci_bus_master_enabled;
d->ahci.irq = &d->irq;
}
diff --git a/include/hw/ide/ahci.h b/include/hw/ide/ahci.h
index cd07b87811..434a6457b5 100644
--- a/include/hw/ide/ahci.h
+++ b/include/hw/ide/ahci.h
@@ -46,6 +46,7 @@ typedef struct AHCIState {
uint32_t ports;
qemu_irq irq;
AddressSpace *as;
+ bool (*bus_master_enabled)(struct AHCIState *s);
} AHCIState;
--
2.43.7
On 11/2/26 14:41, Abhisek Panda wrote:
> When a Windows guest puts an idle SATA drive into standby, it disables
> PCI bus mastering on the AHCI controller but fails to clear the port
> engine bits in the port command register. During post-migration
> handling, QEMU’s ahci_state_post_load function checks only these port
> engine bits to determine whether to start the engines and map the FIS
> receive buffer (PxFB) and Command List buffer (PxCLB). Because PCI bus
> mastering is disabled, DMA access to guest memory is blocked.
> Consequently, the memory mapping requests fail or incorrectly route
> through bounce buffers instead of direct guest memory, causing the
> destination QEMU process to abort the migration.
The fix is right, but on current master the abort is the less common
outcome. Since 637b0aa139 ("softmmu: Support concurrent bounce
buffers") each PCI device has its own 4k bounce budget, and a started
port takes 1024 bytes for the command list plus 256 for the FIS area.
While the started ports fit into that budget, post_load succeeds with
lst and res_fis pointing at private zero-filled bounce buffers, and the
migration completes. When the guest later stops the port, the unmap
writes those buffers back, i.e. zeros land on PxCLB/PxFB in guest
memory. Only when the budget is exceeded does post_load fail.
I have checked both on q35 with a qtest: clear bus mastering, migrate,
re-enable it, stop and restart port 0, issue IDENTIFY. With one disk
master migrates fine and stopping the port zeroes the whole command
list and FIS area. With three disks the destination fails with "bad
FIS receive buffer address". With your patch both cases migrate,
guest memory stays intact, and the restarted port works.
Please describe both outcomes in the commit message, the silent memory
corruption is the more important one.
> Starting DMA/FIS engines when bus mastering is disabled might lead to
> buffer mapping failures or improper bounce buffer allocations. Do not
> start the engines in the aforementioned scenario. When the guest
> re-enables bus mastering to access an idle drive, it should reset the
> port engine, safely allocating the buffers and starting the engines.
"it should" is what the patch relies on, as nothing restarts the
engines on a bus master 0->1 transition. Andy's trace in the 2019
thread shows that Windows 10 does exactly that: it re-enables bus
mastering, rewrites PxCLB/PxFB, clears and restarts PxCMD and issues a
COMRESET. Windows 7 does not clear bus mastering at all. Please state
this instead of "should".
> Reported-by: andychiu <andychiu@synology.com>
> Suggested-by: Michael S. Tsirkin <mst@redhat.com>
> Suggested-by: John Snow <jsnow@redhat.com>
> Link: https://lists.gnu.org/archive/html/qemu-devel/2019-09/msg01544.html
> Signed-off-by: Abhisek Panda <abhisek.panda1@nutanix.com>
With the commit message updated:
Tested-by: Denis V. Lunev <den@openvz.org>
Reviewed-by: Denis V. Lunev <den@openvz.org>
Den
P.S. John - will you take this?
On Mon, Nov 2, 2026 at 10:42 AM Denis V. Lunev <den@virtuozzo.com> wrote:
>
> On 11/2/26 14:41, Abhisek Panda wrote:
> > When a Windows guest puts an idle SATA drive into standby, it disables
> > PCI bus mastering on the AHCI controller but fails to clear the port
> > engine bits in the port command register. During post-migration
> > handling, QEMU’s ahci_state_post_load function checks only these port
> > engine bits to determine whether to start the engines and map the FIS
> > receive buffer (PxFB) and Command List buffer (PxCLB). Because PCI bus
> > mastering is disabled, DMA access to guest memory is blocked.
> > Consequently, the memory mapping requests fail or incorrectly route
> > through bounce buffers instead of direct guest memory, causing the
> > destination QEMU process to abort the migration.
> The fix is right, but on current master the abort is the less common
> outcome. Since 637b0aa139 ("softmmu: Support concurrent bounce
> buffers") each PCI device has its own 4k bounce budget, and a started
> port takes 1024 bytes for the command list plus 256 for the FIS area.
> While the started ports fit into that budget, post_load succeeds with
> lst and res_fis pointing at private zero-filled bounce buffers, and the
> migration completes. When the guest later stops the port, the unmap
> writes those buffers back, i.e. zeros land on PxCLB/PxFB in guest
> memory. Only when the budget is exceeded does post_load fail.
>
> I have checked both on q35 with a qtest: clear bus mastering, migrate,
> re-enable it, stop and restart port 0, issue IDENTIFY. With one disk
> master migrates fine and stopping the port zeroes the whole command
> list and FIS area. With three disks the destination fails with "bad
> FIS receive buffer address". With your patch both cases migrate,
> guest memory stays intact, and the restarted port works.
>
> Please describe both outcomes in the commit message, the silent memory
> corruption is the more important one.
>
> > Starting DMA/FIS engines when bus mastering is disabled might lead to
> > buffer mapping failures or improper bounce buffer allocations. Do not
> > start the engines in the aforementioned scenario. When the guest
> > re-enables bus mastering to access an idle drive, it should reset the
> > port engine, safely allocating the buffers and starting the engines.
> "it should" is what the patch relies on, as nothing restarts the
> engines on a bus master 0->1 transition. Andy's trace in the 2019
> thread shows that Windows 10 does exactly that: it re-enables bus
> mastering, rewrites PxCLB/PxFB, clears and restarts PxCMD and issues a
> COMRESET. Windows 7 does not clear bus mastering at all. Please state
> this instead of "should".
>
> > Reported-by: andychiu <andychiu@synology.com>
> > Suggested-by: Michael S. Tsirkin <mst@redhat.com>
> > Suggested-by: John Snow <jsnow@redhat.com>
> > Link: https://lists.gnu.org/archive/html/qemu-devel/2019-09/msg01544.html
> > Signed-off-by: Abhisek Panda <abhisek.panda1@nutanix.com>
> With the commit message updated:
>
> Tested-by: Denis V. Lunev <den@openvz.org>
> Reviewed-by: Denis V. Lunev <den@openvz.org>
>
> Den
>
> P.S. John - will you take this?
My attention is currently pulled very strongly to an in-house project;
If you believe it should be merged I will trust your judgement and I
wouldn't be upset if you merged it.
--js
© 2016 - 2026 Red Hat, Inc.