Skip to contentSkip to conversation

kvm: Fix crash due to access uninitialized kvm_state

PATCH Applied Gavin Shan 4 participants 4 messages Jul 31, 2023

lore
Share & tools
lore

Gavin Shan

author
#0 lore raw

PATCH v2 1/1 kvm: Fix crash due to access uninitialized kvm_state

Runs into core dump on arm64 and the backtrace extracted from the core dump is shown as below. It's caused by accessing uninitialized @kvm_state in kvm_flush_coalesced_mmio_buffer() due to commit 176d073029 ("hw/arm/virt: Use machine_memory_devices_init()"), where the machine's memory region is added earlier than before.

main
qemu_init
configure_accelerators
qemu_opts_foreach
do_configure_accelerator
accel_init_machine
kvm_init
virt_kvm_type
virt_set_memmap
machine_memory_devices_init
memory_region_add_subregion
memory_region_add_subregion_common
memory_region_update_container_subregions
memory_region_transaction_begin
qemu_flush_coalesced_mmio_buffer
kvm_flush_coalesced_mmio_buffer

Fix it by bailing early in kvm_flush_coalesced_mmio_buffer() on the uninitialized @kvm_state. With this applied, no crash is observed on arm64.

Fixes 176d073029 ("hw/arm/virt: Use machine_memory_devices_init()")

---
v2: Bail early in kvm_flush_coalesced_mmio_buffer() on the uninitialized
@kvm_state and improved changelog (David/Peter)

---
 accel/kvm/kvm-all.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
accel/kvm/kvm-all.c +1 -1
index 373d876c05..7b3da8dc3a 100644
@@ -2812,7 +2812,7 @@ void kvm_flush_coalesced_mmio_buffer(void)
 {
     KVMState *s = kvm_state;
 
-    if (s->coalesced_flush_in_progress) {
+    if (!s || s->coalesced_flush_in_progress) {
         return;
     }
 
Signature · 1 line
2.41.0
Reviewed-by on v2 1/1
#1 lore raw
Quoted 41 lines · Gavin Shan

Runs into core dump on arm64 and the backtrace extracted from the core dump is shown as below. It's caused by accessing uninitialized @kvm_state in kvm_flush_coalesced_mmio_buffer() due to commit 176d073029 ("hw/arm/virt: Use machine_memory_devices_init()"), where the machine's memory region is added earlier than before.

main
qemu_init
configure_accelerators
qemu_opts_foreach
do_configure_accelerator
accel_init_machine
kvm_init
virt_kvm_type
virt_set_memmap
machine_memory_devices_init
memory_region_add_subregion
memory_region_add_subregion_common
memory_region_update_container_subregions
memory_region_transaction_begin
qemu_flush_coalesced_mmio_buffer
kvm_flush_coalesced_mmio_buffer

Fix it by bailing early in kvm_flush_coalesced_mmio_buffer() on the uninitialized @kvm_state. With this applied, no crash is observed on arm64.

Fixes 176d073029 ("hw/arm/virt: Use machine_memory_devices_init()")

---
v2: Bail early in kvm_flush_coalesced_mmio_buffer() on the uninitialized
@kvm_state and improved changelog (David/Peter)

---
  accel/kvm/kvm-all.c | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
accel/kvm/kvm-all.c +0 -0
index 373d876c05..7b3da8dc3a 100644

accel/kvm/kvm-all.c void kvm_flush_coalesced_mmio_buffer(void) · line 2818

Quoted 8 lines
accel/kvm/kvm-all.c +1 -1
@@ -2812,7 +2812,7 @@ void kvm_flush_coalesced_mmio_buffer(void)
  {
      KVMState *s = kvm_state;
 
-    if (s->coalesced_flush_in_progress) {
+    if (!s || s->coalesced_flush_in_progress) {
          return;
      }
 

Thanks Gavin!

Signature · 3 lines
Cheers,

David / dhildenb
#2 lore raw
Quoted 54 lines · David Hildenbrand
Quoted 41 lines · Gavin Shan

Runs into core dump on arm64 and the backtrace extracted from the core dump is shown as below. It's caused by accessing uninitialized @kvm_state in kvm_flush_coalesced_mmio_buffer() due to commit 176d073029 ("hw/arm/virt: Use machine_memory_devices_init()"), where the machine's memory region is added earlier than before.

main
qemu_init
configure_accelerators
qemu_opts_foreach
do_configure_accelerator
accel_init_machine
kvm_init
virt_kvm_type
virt_set_memmap
machine_memory_devices_init
memory_region_add_subregion
memory_region_add_subregion_common
memory_region_update_container_subregions
memory_region_transaction_begin
qemu_flush_coalesced_mmio_buffer
kvm_flush_coalesced_mmio_buffer

Fix it by bailing early in kvm_flush_coalesced_mmio_buffer() on the uninitialized @kvm_state. With this applied, no crash is observed on arm64.

Fixes 176d073029 ("hw/arm/virt: Use machine_memory_devices_init()")

---
v2: Bail early in kvm_flush_coalesced_mmio_buffer() on the uninitialized
@kvm_state and improved changelog (David/Peter)

---
  accel/kvm/kvm-all.c | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
accel/kvm/kvm-all.c +0 -0
index 373d876c05..7b3da8dc3a 100644

accel/kvm/kvm-all.c void kvm_flush_coalesced_mmio_buffer(void) · line 2818

Quoted 8 lines
accel/kvm/kvm-all.c +1 -1
@@ -2812,7 +2812,7 @@ void kvm_flush_coalesced_mmio_buffer(void)
  {
      KVMState *s = kvm_state;
 
-    if (s->coalesced_flush_in_progress) {
+    if (!s || s->coalesced_flush_in_progress) {
          return;
      }
 

Thanks Gavin!

Quoted 1 line

Applied to target-arm.next, thanks.

-- PMM

Reviewed-by on v2 1/1
#3 lore raw
In reply to Gavin Shan
Quoted 27 lines · Gavin Shan

Runs into core dump on arm64 and the backtrace extracted from the core dump is shown as below. It's caused by accessing uninitialized @kvm_state in kvm_flush_coalesced_mmio_buffer() due to commit 176d073029 ("hw/arm/virt: Use machine_memory_devices_init()"), where the machine's memory region is added earlier than before.

main
qemu_init
configure_accelerators
qemu_opts_foreach
do_configure_accelerator
accel_init_machine
kvm_init
virt_kvm_type
virt_set_memmap
machine_memory_devices_init
memory_region_add_subregion
memory_region_add_subregion_common
memory_region_update_container_subregions
memory_region_transaction_begin
qemu_flush_coalesced_mmio_buffer
kvm_flush_coalesced_mmio_buffer

Fix it by bailing early in kvm_flush_coalesced_mmio_buffer() on the uninitialized @kvm_state. With this applied, no crash is observed on arm64.

Quoted 8 lines
Fixes 176d073029 ("hw/arm/virt: Use machine_memory_devices_init()")

---
v2: Bail early in kvm_flush_coalesced_mmio_buffer() on the uninitialized
@kvm_state and improved changelog (David/Peter)

---
  accel/kvm/kvm-all.c | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)