Skip to contentSkip to conversation

hw/virtio: fix crash in virtio-balloon and test it

PATCH In review Daniel P. Berrangé 6 participants 13 messages Nov 29, 2024 – Dec 3, 2024 Arm/Amlogic Meson SoC support

lore
Share & tools
lore
#0 lore raw

v2 cover hw/virtio: fix crash in virtio-balloon and test it

See patch 1 for the background info on the problem

Changed in v2:

* Add qtest coverage for the crash scenario

Daniel P. Berrangé (3):
hw/virtio: fix crash in processing balloon stats tests/qtest: drop 'fuzz-' prefix from virtio-balloon test tests/qtest: add test for querying balloon guest stats

 hw/virtio/virtio-balloon.c             | 16 +++++++-
 tests/qtest/fuzz-virtio-balloon-test.c | 37 -----------------
 tests/qtest/meson.build                |  2 +-
 tests/qtest/virtio-balloon-test.c      | 57 ++++++++++++++++++++++++++
 4 files changed, 73 insertions(+), 39 deletions(-)
 delete mode 100644 tests/qtest/fuzz-virtio-balloon-test.c
 create mode 100644 tests/qtest/virtio-balloon-test.c
Signature · 1 line
2.46.0
#1 lore raw

PATCH v2 1/3 hw/virtio: fix crash in processing balloon stats

balloon_stats_get_all will iterate over guest stats upto the max VIRTIO_BALLOON_S_NR value, calling visit_type_uint64 to populate the QObject dict. The dict keys are obtained from the static array balloon_stat_names which is VIRTIO_BALLOON_S_NR in size.

Unfortunately the way that array is declared results in any unassigned stats getting a NULL name, which will then cause visit_type_uint64 to trigger an assert in qobject_output_add_obj.

The balloon_stat_names array was fortunately fully populated with names until recently:

commit 0d2eeef77a33315187df8519491a900bde4a3d83
Author: Bibo Mao <[email protected]>
Date: Mon Oct 28 10:38:09 2024 +0800

linux-headers: Update to Linux v6.12-rc5

pulled a change to include/standard-headers/linux/virtio_balloon.h which increased VIRTIO_BALLOON_S_NR by 6, and failed to add the new names to balloon_stat_names.

This commit fills in the missing names, and uses a static assert to guarantee that any future changes to VIRTIO_BALLOON_S_NR will cause a build failure until balloon_stat_names is updated.

This problem was detected by the Cockpit Project's automated integration tests on QEMU 9.2.0-rc1.

---
 hw/virtio/virtio-balloon.c | 16 +++++++++++++++-
 1 file changed, 15 insertions(+), 1 deletion(-)
hw/virtio/virtio-balloon.c +15 -1
index 609e39a821..afd2ad6dd6 100644
@@ -167,19 +167,33 @@ static void balloon_deflate_page(VirtIOBalloon *balloon,
     }
 }
 
+/*
+ * All stats upto VIRTIO_BALLOON_S_NR /must/ have a
+ * non-NULL name declared here, since these are used
+ * as keys for populating the QDict with stats
+ */
 static const char *balloon_stat_names[] = {
    [VIRTIO_BALLOON_S_SWAP_IN] = "stat-swap-in",
    [VIRTIO_BALLOON_S_SWAP_OUT] = "stat-swap-out",
    [VIRTIO_BALLOON_S_MAJFLT] = "stat-major-faults",
    [VIRTIO_BALLOON_S_MINFLT] = "stat-minor-faults",
    [VIRTIO_BALLOON_S_MEMFREE] = "stat-free-memory",
+
    [VIRTIO_BALLOON_S_MEMTOT] = "stat-total-memory",
    [VIRTIO_BALLOON_S_AVAIL] = "stat-available-memory",
    [VIRTIO_BALLOON_S_CACHES] = "stat-disk-caches",
    [VIRTIO_BALLOON_S_HTLB_PGALLOC] = "stat-htlb-pgalloc",
    [VIRTIO_BALLOON_S_HTLB_PGFAIL] = "stat-htlb-pgfail",
-   [VIRTIO_BALLOON_S_NR] = NULL
+
+   [VIRTIO_BALLOON_S_OOM_KILL] = "stat-oom-kills",
+   [VIRTIO_BALLOON_S_ALLOC_STALL] = "stat-alloc-stalls",
+   [VIRTIO_BALLOON_S_ASYNC_SCAN] = "stat-async-scans",
+   [VIRTIO_BALLOON_S_DIRECT_SCAN] = "stat-direct-scans",
+   [VIRTIO_BALLOON_S_ASYNC_RECLAIM] = "stat-async-reclaims",
+
+   [VIRTIO_BALLOON_S_DIRECT_RECLAIM] = "stat-direct-reclaims",
 };
+G_STATIC_ASSERT(G_N_ELEMENTS(balloon_stat_names) == VIRTIO_BALLOON_S_NR);
 
 /*
  * reset_stats - Mark all items in the stats array as unset
Signature · 1 line
2.46.0
Reviewed-by on v2 1/3
#2 lore raw

Re: [PATCH v2 1/3 for 9.2] hw/virtio: fix crash in processing balloon stats

Quoted 67 lines · Daniel P. Berrangé

balloon_stats_get_all will iterate over guest stats upto the max VIRTIO_BALLOON_S_NR value, calling visit_type_uint64 to populate the QObject dict. The dict keys are obtained from the static array balloon_stat_names which is VIRTIO_BALLOON_S_NR in size.

Unfortunately the way that array is declared results in any unassigned stats getting a NULL name, which will then cause visit_type_uint64 to trigger an assert in qobject_output_add_obj.

The balloon_stat_names array was fortunately fully populated with names until recently:

commit 0d2eeef77a33315187df8519491a900bde4a3d83
Author: Bibo Mao <[email protected]>
Date: Mon Oct 28 10:38:09 2024 +0800

linux-headers: Update to Linux v6.12-rc5

pulled a change to include/standard-headers/linux/virtio_balloon.h which increased VIRTIO_BALLOON_S_NR by 6, and failed to add the new names to balloon_stat_names.

This commit fills in the missing names, and uses a static assert to guarantee that any future changes to VIRTIO_BALLOON_S_NR will cause a build failure until balloon_stat_names is updated.

This problem was detected by the Cockpit Project's automated integration tests on QEMU 9.2.0-rc1.

---
  hw/virtio/virtio-balloon.c | 16 +++++++++++++++-
  1 file changed, 15 insertions(+), 1 deletion(-)
hw/virtio/virtio-balloon.c +9 -1
index 609e39a821..afd2ad6dd6 100644
@@ -167,19 +167,33 @@ static void balloon_deflate_page(VirtIOBalloon *balloon,
      }
  }
 
+/*
+ * All stats upto VIRTIO_BALLOON_S_NR /must/ have a
+ * non-NULL name declared here, since these are used
+ * as keys for populating the QDict with stats
+ */
  static const char *balloon_stat_names[] = {
     [VIRTIO_BALLOON_S_SWAP_IN] = "stat-swap-in",
     [VIRTIO_BALLOON_S_SWAP_OUT] = "stat-swap-out",
     [VIRTIO_BALLOON_S_MAJFLT] = "stat-major-faults",
     [VIRTIO_BALLOON_S_MINFLT] = "stat-minor-faults",
     [VIRTIO_BALLOON_S_MEMFREE] = "stat-free-memory",
+
     [VIRTIO_BALLOON_S_MEMTOT] = "stat-total-memory",
     [VIRTIO_BALLOON_S_AVAIL] = "stat-available-memory",
     [VIRTIO_BALLOON_S_CACHES] = "stat-disk-caches",
     [VIRTIO_BALLOON_S_HTLB_PGALLOC] = "stat-htlb-pgalloc",
     [VIRTIO_BALLOON_S_HTLB_PGFAIL] = "stat-htlb-pgfail",
-   [VIRTIO_BALLOON_S_NR] = NULL
+
+   [VIRTIO_BALLOON_S_OOM_KILL] = "stat-oom-kills",
+   [VIRTIO_BALLOON_S_ALLOC_STALL] = "stat-alloc-stalls",

hw/virtio/virtio-balloon.c static void balloon_deflate_page(VirtIOBalloon *balloon, · line 199

Quoted 10 lines
hw/virtio/virtio-balloon.c +6 -0
+   [VIRTIO_BALLOON_S_ASYNC_SCAN] = "stat-async-scans",
+   [VIRTIO_BALLOON_S_DIRECT_SCAN] = "stat-direct-scans",
+   [VIRTIO_BALLOON_S_ASYNC_RECLAIM] = "stat-async-reclaims",
+
+   [VIRTIO_BALLOON_S_DIRECT_RECLAIM] = "stat-direct-reclaims",
  };
+G_STATIC_ASSERT(G_N_ELEMENTS(balloon_stat_names) == VIRTIO_BALLOON_S_NR);
 
  /*
   * reset_stats - Mark all items in the stats array as unset
Signature · 3 lines
Cheers,

David / dhildenb
Reviewed-by on v2 1/3
#3 lore raw

Re: [PATCH v2 1/3 for 9.2] hw/virtio: fix crash in processing balloon stats

In reply to Daniel P. Berrangé
Quoted 26 lines · Daniel P. Berrangé

balloon_stats_get_all will iterate over guest stats upto the max VIRTIO_BALLOON_S_NR value, calling visit_type_uint64 to populate the QObject dict. The dict keys are obtained from the static array balloon_stat_names which is VIRTIO_BALLOON_S_NR in size.

Unfortunately the way that array is declared results in any unassigned stats getting a NULL name, which will then cause visit_type_uint64 to trigger an assert in qobject_output_add_obj.

The balloon_stat_names array was fortunately fully populated with names until recently:

commit 0d2eeef77a33315187df8519491a900bde4a3d83
Author: Bibo Mao <[email protected]>
Date: Mon Oct 28 10:38:09 2024 +0800

linux-headers: Update to Linux v6.12-rc5

pulled a change to include/standard-headers/linux/virtio_balloon.h which increased VIRTIO_BALLOON_S_NR by 6, and failed to add the new names to balloon_stat_names.

This commit fills in the missing names, and uses a static assert to guarantee that any future changes to VIRTIO_BALLOON_S_NR will cause a build failure until balloon_stat_names is updated.

Quoted 8 lines

This problem was detected by the Cockpit Project's automated integration tests on QEMU 9.2.0-rc1.

/mjt

#4 lore raw

PATCH v2 2/3 tests/qtest: drop 'fuzz-' prefix from virtio-balloon test

In reply to Daniel P. Berrangé

This test file is expected to be extended for arbitrary virtio-balloon related tests, not merely those discovered by fuzzing.

---
 tests/qtest/meson.build                                       | 2 +-
 .../{fuzz-virtio-balloon-test.c => virtio-balloon-test.c}     | 4 ++--
 2 files changed, 3 insertions(+), 3 deletions(-)
 rename tests/qtest/{fuzz-virtio-balloon-test.c => virtio-balloon-test.c} (84%)
tests/qtest/meson.build +1 -1
index f2f35367ae..bd41c9da5f 100644
@@ -88,7 +88,7 @@ qtests_i386 = \
   (config_all_devices.has_key('CONFIG_MEGASAS_SCSI_PCI') ? ['fuzz-megasas-test'] : []) +    \
   (config_all_devices.has_key('CONFIG_LSI_SCSI_PCI') ? ['fuzz-lsi53c895a-test'] : []) +     \
   (config_all_devices.has_key('CONFIG_VIRTIO_SCSI') ? ['fuzz-virtio-scsi-test'] : []) +     \
-  (config_all_devices.has_key('CONFIG_VIRTIO_BALLOON') ? ['fuzz-virtio-balloon-test'] : []) + \
+  (config_all_devices.has_key('CONFIG_VIRTIO_BALLOON') ? ['virtio-balloon-test'] : []) + \
   (config_all_devices.has_key('CONFIG_Q35') ? ['q35-test'] : []) +                          \
   (config_all_devices.has_key('CONFIG_SB16') ? ['fuzz-sb16-test'] : []) +                   \
   (config_all_devices.has_key('CONFIG_SDHCI_PCI') ? ['fuzz-sdcard-test'] : []) +            \
tests/qtest/virtio-balloon-test.c renamed from tests/qtest/fuzz-virtio-balloon-test.c +2 -2
similarity index 84%
rename from tests/qtest/fuzz-virtio-balloon-test.c
rename to tests/qtest/virtio-balloon-test.c
index ecb597fbee..6bea33b590 100644
@@ -1,5 +1,5 @@
 /*
- * QTest fuzzer-generated testcase for virtio balloon device
+ * QTest test cases for virtio balloon device
  *
  * Copyright (c) 2024 Gao Shiyuan <[email protected]>
  *
@@ -30,7 +30,7 @@ int main(int argc, char **argv)
 {
     g_test_init(&argc, &argv, NULL);
 
-    qtest_add_func("fuzz/virtio/oss_fuzz_71649", oss_fuzz_71649);
+    qtest_add_func("virtio-balloon/oss_fuzz_71649", oss_fuzz_71649);
 
     return g_test_run();
 }
Signature · 1 line
2.46.0
Reviewed-by on v2 2/3
#6 lore raw

Re: [PATCH v2 2/3 for 9.2] tests/qtest: drop 'fuzz-' prefix from virtio-balloon test

In reply to Daniel P. Berrangé
Quoted 9 lines · Daniel P. Berrangé

This test file is expected to be extended for arbitrary virtio-balloon related tests, not merely those discovered by fuzzing.

---
  tests/qtest/meson.build                                       | 2 +-
  .../{fuzz-virtio-balloon-test.c => virtio-balloon-test.c}     | 4 ++--
  2 files changed, 3 insertions(+), 3 deletions(-)

rename tests/qtest/{fuzz-virtio-balloon-test.c => virtio-balloon-test.c} (84%)

#7 lore raw

PATCH v2 3/3 tests/qtest: add test for querying balloon guest stats

In reply to Daniel P. Berrangé

This test would have identified the crash caused by the addition of new balloon stats fields.

---
 tests/qtest/virtio-balloon-test.c | 20 ++++++++++++++++++++
 1 file changed, 20 insertions(+)
tests/qtest/virtio-balloon-test.c +20 -0
index 6bea33b590..ecdd363b06 100644
@@ -8,6 +8,7 @@
 
 #include "qemu/osdep.h"
 #include "libqtest.h"
+#include "standard-headers/linux/virtio_balloon.h"
 
 /*
  * https://gitlab.com/qemu-project/qemu/-/issues/2576
@@ -26,11 +27,30 @@ static void oss_fuzz_71649(void)
     qtest_quit(s);
 }
 
+static void query_stats(void)
+{
+    QTestState *s = qtest_init("-device virtio-balloon,id=balloon"
+                               " -nodefaults");
+    QDict *ret = qtest_qmp_assert_success_ref(
+        s,
+        "{ 'execute': 'qom-get', 'arguments': "     \
+        "{ 'path': '/machine/peripheral/balloon', " \
+        "  'property': 'guest-stats' } }");
+    QDict *stats = qdict_get_qdict(ret, "stats");
+
+    /* We expect 1 entry in the dict for each known kernel stat */
+    assert(qdict_size(stats) == VIRTIO_BALLOON_S_NR);
+
+    qobject_unref(ret);
+    qtest_quit(s);
+}
+
 int main(int argc, char **argv)
 {
     g_test_init(&argc, &argv, NULL);
 
     qtest_add_func("virtio-balloon/oss_fuzz_71649", oss_fuzz_71649);
+    qtest_add_func("virtio-balloon/query-stats", query_stats);
 
     return g_test_run();
 }
Signature · 1 line
2.46.0
Reviewed-by on v2 3/3
#8 lore raw

Re: [PATCH v2 3/3 for 9.2] tests/qtest: add test for querying balloon guest stats

Quoted 41 lines · Daniel P. Berrangé

This test would have identified the crash caused by the addition of new balloon stats fields.

---
 tests/qtest/virtio-balloon-test.c | 20 ++++++++++++++++++++
 1 file changed, 20 insertions(+)
tests/qtest/virtio-balloon-test.c +18 -0
index 6bea33b590..ecdd363b06 100644
@@ -8,6 +8,7 @@
 
 #include "qemu/osdep.h"
 #include "libqtest.h"
+#include "standard-headers/linux/virtio_balloon.h"
 
 /*
  * https://gitlab.com/qemu-project/qemu/-/issues/2576
@@ -26,11 +27,30 @@ static void oss_fuzz_71649(void)
     qtest_quit(s);
 }
 
+static void query_stats(void)
+{
+    QTestState *s = qtest_init("-device virtio-balloon,id=balloon"
+                               " -nodefaults");
+    QDict *ret = qtest_qmp_assert_success_ref(
+        s,
+        "{ 'execute': 'qom-get', 'arguments': "     \
+        "{ 'path': '/machine/peripheral/balloon', " \
+        "  'property': 'guest-stats' } }");
+    QDict *stats = qdict_get_qdict(ret, "stats");
+
+    /* We expect 1 entry in the dict for each known kernel stat */
+    assert(qdict_size(stats) == VIRTIO_BALLOON_S_NR);
+
+    qobject_unref(ret);
+    qtest_quit(s);
+}

tests/qtest/virtio-balloon-test.c static void oss_fuzz_71649(void) · line 56

Quoted 10 lines
tests/qtest/virtio-balloon-test.c +2 -0
+
 int main(int argc, char **argv)
 {
     g_test_init(&argc, &argv, NULL);
 
     qtest_add_func("virtio-balloon/oss_fuzz_71649", oss_fuzz_71649);
+    qtest_add_func("virtio-balloon/query-stats", query_stats);
 
     return g_test_run();
 }
#9 lore raw
In reply to Daniel P. Berrangé
Quoted 11 lines · Daniel P. Berrangé

See patch 1 for the background info on the problem

Changed in v2:

* Add qtest coverage for the crash scenario

Daniel P. Berrangé (3):
hw/virtio: fix crash in processing balloon stats tests/qtest: drop 'fuzz-' prefix from virtio-balloon test tests/qtest: add test for querying balloon guest stats

Quoted 8 lines
  hw/virtio/virtio-balloon.c             | 16 +++++++-
  tests/qtest/fuzz-virtio-balloon-test.c | 37 -----------------
  tests/qtest/meson.build                |  2 +-
  tests/qtest/virtio-balloon-test.c      | 57 ++++++++++++++++++++++++++
  4 files changed, 73 insertions(+), 39 deletions(-)

delete mode 100644 tests/qtest/fuzz-virtio-balloon-test.c create mode 100644 tests/qtest/virtio-balloon-test.c

@MST, do you want to queue this or should I do it? We should get this into 9.2.

Signature · 3 lines
Cheers,

David / dhildenb
#10 lore raw

Hi,

Quoted 21 lines · David Hildenbrand
Quoted 11 lines · Daniel P. Berrangé

See patch 1 for the background info on the problem

Changed in v2:

  * Add qtest coverage for the crash scenario

Daniel P. Berrangé (3):
   hw/virtio: fix crash in processing balloon stats tests/qtest: drop 'fuzz-' prefix from virtio-balloon test tests/qtest: add test for querying balloon guest stats

Quoted 8 lines

  hw/virtio/virtio-balloon.c             | 16 +++++++- tests/qtest/fuzz-virtio-balloon-test.c | 37 ----------------- tests/qtest/meson.build                |  2 +- tests/qtest/virtio-balloon-test.c      | 57 ++++++++++++++++++++++++++

  4 files changed, 73 insertions(+), 39 deletions(-)

  delete mode 100644 tests/qtest/fuzz-virtio-balloon-test.c create mode 100644 tests/qtest/virtio-balloon-test.c

Quoted 2 lines

@MST, do you want to queue this or should I do it? We should get this into 9.2.

I'm preparing a PR for tomorrow; I included this series amending:

-- >8 --

MAINTAINERS +1 -0
index 2b1c4abed65..51e3a79b6bb 100644
@@ -2227,6 +2227,7 @@ F: hw/virtio/virtio-balloon*.c
 F: include/hw/virtio/virtio-balloon.h
 F: system/balloon.c
 F: include/sysemu/balloon.h
+F: tests/qtest/virtio-balloon-test.c
 
 virtio-9p
 M: Greg Kurz <[email protected]>

---

Tell me if it isn't appropriate and I'll drop it.

Regards,

Phil.

#11 lore raw
Quoted 45 lines · Philippe Mathieu-Daudé

Hi,

Quoted 21 lines · David Hildenbrand
Quoted 11 lines · Daniel P. Berrangé

See patch 1 for the background info on the problem

Changed in v2:

  * Add qtest coverage for the crash scenario

Daniel P. Berrangé (3):
   hw/virtio: fix crash in processing balloon stats tests/qtest: drop 'fuzz-' prefix from virtio-balloon test tests/qtest: add test for querying balloon guest stats

Quoted 8 lines

  hw/virtio/virtio-balloon.c             | 16 +++++++- tests/qtest/fuzz-virtio-balloon-test.c | 37 ----------------- tests/qtest/meson.build                |  2 +- tests/qtest/virtio-balloon-test.c      | 57 ++++++++++++++++++++++++++

  4 files changed, 73 insertions(+), 39 deletions(-)

  delete mode 100644 tests/qtest/fuzz-virtio-balloon-test.c create mode 100644 tests/qtest/virtio-balloon-test.c

Quoted 2 lines

@MST, do you want to queue this or should I do it? We should get this into 9.2.

I'm preparing a PR for tomorrow; I included this series amending:

-- >8 --

MAINTAINERS +1 -1
index 2b1c4abed65..51e3a79b6bb 100644
@@ -2227,6 +2227,7 @@ F: hw/virtio/virtio-balloon*.c
 F: include/hw/virtio/virtio-balloon.h
 F: system/balloon.c
 F: include/sysemu/balloon.h
+F: tests/qtest/virtio-balloon-test.c
 
 virtio-9p
 M: Greg Kurz <[email protected]>
---
Quoted 5 lines

Tell me if it isn't appropriate and I'll drop it.

Regards,

Phil.

Go ahead.

Series:

#12 lore raw
Quoted 9 lines · Michael S. Tsirkin
Quoted 8 lines · Philippe Mathieu-Daudé

Hi,

Quoted 5 lines · David Hildenbrand
Quoted 4 lines · Daniel P. Berrangé

See patch 1 for the background info on the problem

Changed in v2:

Quoted 8 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines

  * Add qtest coverage for the crash scenario

Daniel P. Berrangé (3):
   hw/virtio: fix crash in processing balloon stats tests/qtest: drop 'fuzz-' prefix from virtio-balloon test tests/qtest: add test for querying balloon guest stats

Series:> Acked-by: Michael S. Tsirkin <[email protected]>

Series queued, thanks.