Commit e019b94a0b for qemu.org
commit e019b94a0bca322b312b3843dd1a8cd9c475a50e
Author: Markku Ahvenjärvi <mankku@gmail.com>
Date: Wed Sep 23 15:20:02 2026 +0300
hw/nvme: prevent cq_pending underflow
cq_pending counts interrupt-enabled completion queues with unconsumed
entries. nvme_cq_notifier() and nvme_process_db() decrement it whenever
a CQ becomes empty, but they may also process a notification or doorbell
write for a CQ that was already empty.
Decrement cq_pending only when the CQ contained pending completions
before its head update. Otherwise cq_pending may underflow and
prevent legacy INTx from being deasserted.
Cc: qemu-stable@nongnu.org
Fixes: 83d7ed5c570d ("hw/nvme: fix pin-based interrupt behavior (again)")
Fixes: 2e53b0b45024 ("hw/nvme: Use ioeventfd to handle doorbell updates")
Signed-off-by: Markku Ahvenjärvi <mankku@gmail.com>
Reviewed-by: Klaus Jensen <k.jensen@samsung.com>
Signed-off-by: Klaus Jensen <k.jensen@samsung.com>
diff --git a/hw/nvme/ctrl.c b/hw/nvme/ctrl.c
index 10e5d976a8..e591842a4a 100644
--- a/hw/nvme/ctrl.c
+++ b/hw/nvme/ctrl.c
@@ -4783,6 +4783,7 @@ static void nvme_cq_notifier(EventNotifier *e)
{
NvmeCQueue *cq = container_of(e, NvmeCQueue, notifier);
NvmeCtrl *n = cq->ctrl;
+ bool pending = cq->tail != cq->head;
if (!event_notifier_test_and_clear(e)) {
return;
@@ -4791,7 +4792,7 @@ static void nvme_cq_notifier(EventNotifier *e)
nvme_update_cq_head(cq);
if (cq->tail == cq->head) {
- if (cq->irq_enabled) {
+ if (cq->irq_enabled && pending) {
n->cq_pending--;
}
@@ -8565,6 +8566,7 @@ static void nvme_process_db(NvmeCtrl *n, hwaddr addr, int val)
uint16_t new_head = val & 0xffff;
NvmeCQueue *cq;
+ bool pending;
qid = (addr - (0x1000 + (1 << 2))) >> 3;
if (unlikely(nvme_check_cqid(n, qid))) {
@@ -8619,13 +8621,14 @@ static void nvme_process_db(NvmeCtrl *n, hwaddr addr, int val)
qemu_bh_schedule(cq->bh);
}
+ pending = cq->tail != cq->head;
cq->head = new_head;
if (!qid && n->dbbuf_enabled) {
stl_le_pci_dma(pci, cq->db_addr, cq->head, MEMTXATTRS_UNSPECIFIED);
}
if (cq->tail == cq->head) {
- if (cq->irq_enabled) {
+ if (cq->irq_enabled && pending) {
n->cq_pending--;
}