hw/virtio: fix crash in virtio-balloon and test it
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
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
@@ -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
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
@@ -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
@@ -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
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
@@ -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
Re: [PATCH v2 1/3 for 9.2] hw/virtio: fix crash in processing balloon stats
In reply to Daniel P. BerrangéQuoted 67 lines · Daniel P. Berrangé
balloon_stats_get_allwill iterate over guest stats upto the maxVIRTIO_BALLOON_S_NRvalue, callingvisit_type_uint64to populate the QObject dict. The dict keys are obtained from the static arrayballoon_stat_nameswhich isVIRTIO_BALLOON_S_NRin size.Unfortunately the way that array is declared results in any unassigned stats getting a NULL name, which will then cause
visit_type_uint64to trigger an assert inqobject_output_add_obj.The
balloon_stat_namesarray was fortunately fully populated with names until recently:commit
0d2eeef77a33315187df8519491a900bde4a3d83
Author: Bibo Mao <[email protected]>
Date: Mon Oct 28 10:38:09 2024 +0800linux-headers: Update to Linux v6.12-rc5
pulled a change to
include/standard-headers/linux/virtio_balloon.hwhich increasedVIRTIO_BALLOON_S_NRby 6, and failed to add the new names toballoon_stat_names.This commit fills in the missing names, and uses a static assert to guarantee that any future changes to
VIRTIO_BALLOON_S_NRwill cause a build failure untilballoon_stat_namesis updated.This problem was detected by the Cockpit Project's automated integration tests on QEMU 9.2.0-rc1.
Fixes 0d2eeef77a33315187df8519491a900bde4a3d83Reported-by Martin Pitt <[email protected]>Reviewed-by Richard W.M. Jones <[email protected]>Signed-off-by Daniel P. Berrangé <[email protected]>--- hw/virtio/virtio-balloon.c | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-)hw/virtio/virtio-balloon.c +9
-1@@ -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
Re: [PATCH v2 2/3 for 9.2] tests/qtest: drop 'fuzz-' prefix from virtio-balloon test
In reply to Daniel P. BerrangéQuoted 4 lines · Daniel P. Berrangé
This test file is expected to be extended for arbitrary virtio-balloon related tests, not merely those discovered by fuzzing.
Signed-off-by Daniel P. Berrangé <[email protected]>
Re: [PATCH v2 3/3 for 9.2] tests/qtest: add test for querying balloon guest stats
In reply to Daniel P. BerrangéQuoted 41 lines · Daniel P. Berrangé
This test would have identified the crash caused by the addition of new balloon stats fields.
Signed-off-by Daniel P. Berrangé <[email protected]>--- tests/qtest/virtio-balloon-test.c | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+)tests/qtest/virtio-balloon-test.c +18
-0@@ -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 (); }
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.
Signed-off-by Daniel P. Berrangé <[email protected]>--- 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%)
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.ccreate mode 100644tests/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
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_allwill iterate over guest stats upto the maxVIRTIO_BALLOON_S_NRvalue, callingvisit_type_uint64to populate the QObject dict. The dict keys are obtained from the static arrayballoon_stat_nameswhich isVIRTIO_BALLOON_S_NRin size.Unfortunately the way that array is declared results in any unassigned stats getting a NULL name, which will then cause
visit_type_uint64to trigger an assert inqobject_output_add_obj.The
balloon_stat_namesarray was fortunately fully populated with names until recently:commit
0d2eeef77a33315187df8519491a900bde4a3d83
Author: Bibo Mao <[email protected]>
Date: Mon Oct 28 10:38:09 2024 +0800linux-headers: Update to Linux v6.12-rc5
pulled a change to
include/standard-headers/linux/virtio_balloon.hwhich increasedVIRTIO_BALLOON_S_NRby 6, and failed to add the new names toballoon_stat_names.This commit fills in the missing names, and uses a static assert to guarantee that any future changes to
VIRTIO_BALLOON_S_NRwill cause a build failure untilballoon_stat_namesis updated.
Quoted 8 lines
This problem was detected by the Cockpit Project's automated integration tests on QEMU 9.2.0-rc1.
Fixes 0d2eeef77a33315187df8519491a900bde4a3d83Reported-by Martin Pitt <[email protected]>Reviewed-by Richard W.M. Jones <[email protected]>Signed-off-by Daniel P. Berrangé <[email protected]>
/mjt
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 statsQuoted 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.ccreate mode 100644tests/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
@@ -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.
Michael S. Tsirkin
Acked-byQuoted 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 statsQuoted 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.ccreate mode 100644tests/qtest/virtio-balloon-test.cQuoted 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@@ -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:
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 statsSeries:> Acked-by: Michael S. Tsirkin <[email protected]>
Series queued, thanks.