Skip to contentSkip to conversation

kvm: minor cleanups in kvm_close and kvm_irqchip_release_virq

PATCH New Bin Guo 1 participant 3 messages May 9 KVM

lore
Share & tools
lore

Bin Guo

author
#0 lore raw

cover kvm: minor cleanups in kvm_close and kvm_irqchip_release_virq

Two small cleanups in kvm-all.c:

- Patch 1 removes a redundant null/fd check in kvm_close() that can never be true once the early-return guard at function entry has already passed.

- Patch 2 fixes an off-by-one in kvm_irqchip_release_virq(): after a swap-remove the newly placed entry at the same index was never re-examined, so a duplicate GSI could be silently left in the routing table.

Bin Guo (2):
kvm: remove redundant check in kvm_close
kvm: fix kvm_irqchip_release_virq loop after swap-remove

 accel/kvm/kvm-all.c | 12 ++++++------
 1 file changed, 6 insertions(+), 6 deletions(-)
Signature · 1 line
2.50.1 (Apple Git-155)
author
#1 lore raw

PATCH 1/2 kvm: remove redundant check in kvm_close

In kvm_close(), the condition !kvm_state || kvm_state->fd == -1 is checked at the function entry, causing an early return if either is true. Later in the function, the same condition is redundantly checked again before closing vmfd and fd.

Remove the redundant second check since it can never be true at that point.

---
 accel/kvm/kvm-all.c | 11 +++++------
 1 file changed, 5 insertions(+), 6 deletions(-)
accel/kvm/kvm-all.c +5 -6
index 92af42503b..70bc2c86a2 100644
@@ -769,12 +769,11 @@ void kvm_close(void)
         cpu->kvm_vcpu_stats_fd = -1;
     }
 
-    if (kvm_state && kvm_state->fd != -1) {
-        close(kvm_state->vmfd);
-        kvm_state->vmfd = -1;
-        close(kvm_state->fd);
-        kvm_state->fd = -1;
-    }
+    close(kvm_state->vmfd);
+    kvm_state->vmfd = -1;
+    close(kvm_state->fd);
+    kvm_state->fd = -1;
+
     kvm_state = NULL;
 }
 
Signature · 1 line
2.50.1 (Apple Git-155)
author
#2 lore raw

PATCH 2/2 kvm: fix kvm_irqchip_release_virq loop after swap-remove

In reply to Bin Guo

In kvm_irqchip_release_virq(), when an entry matching the target GSI is found, the code removes it by swapping in the last entry:

    s->irq_routes->nr--;
    *e = s->irq_routes->entries[s->irq_routes->nr];

However, the loop then increments i unconditionally, which means the newly-swapped entry at position i is never re-checked. If that entry also has gsi == virq, it will be skipped and left in the routing table.

Fix this by decrementing i after the swap, so the same index is re-examined in the next iteration.

---
 accel/kvm/kvm-all.c | 1 +
 1 file changed, 1 insertion(+)
accel/kvm/kvm-all.c +1 -0
index 70bc2c86a2..3c5b62c97e 100644
@@ -2278,6 +2278,7 @@ void kvm_irqchip_release_virq(KVMState *s, int virq)
         if (e->gsi == virq) {
             s->irq_routes->nr--;
             *e = s->irq_routes->entries[s->irq_routes->nr];
+            i--;
         }
     }
     clear_gsi(s, virq);
Signature · 1 line
2.50.1 (Apple Git-155)