virtio-rng: Fix memory leaks in virtio_rng_device_realize
cover virtio-rng: Fix memory leaks in virtio_rng_device_realize
My previous commit, 713e8a10, did not address the fact that the error checking pathways do not attempt to call virtio_cleanup and thus can leak memory in hotplug scenarios.
This patchset shuffles around the error checking so it does not need to perform said cleanup, and changes the error functions from error_set to error_setg.
John Snow (2):
virtio-rng: Move error-checking forward to prevent memory leak
virtio-rng: replace error_set calls with error_setg
hw/virtio/virtio-rng.c | 25 ++++++++++++------------- 1 file changed, 12 insertions(+), 13 deletions(-)
Signature · 1 line
1.9.3
PATCH 1/2 virtio-rng: Move error-checking forward to prevent memory leak
This patch pushes the error-checking forward and the virtio initialization backward in the device realization function in order to prevent memory leaks for hot plug scenarios.
--- hw/virtio/virtio-rng.c | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-)
hw/virtio/virtio-rng.c +10 -10
@@ -147,6 +147,14 @@ static void virtio_rng_device_realize(DeviceState *dev, Error **errp)return ; } +/* Workaround: Property parsing does not enforce unsigned integers, +* So this is a hack to reject such numbers. */ +if (vrng->conf.max_bytes >INT64_MAX ) { +error_set (errp,QERR_INVALID_PARAMETER_VALUE ,"max-bytes" , +"a non-negative integer below 2^63" ); +return ; + } +if (vrng->conf.rng ==NULL ) { vrng->conf.default_backend =RNG_RANDOM (object_new (TYPE_RNG_RANDOM ));
@@ -171,23 +179,15 @@ static void virtio_rng_device_realize(DeviceState *dev, Error **errp) "rng", NULL); } -virtio_init (vdev,"virtio-rng" ,VIRTIO_ID_RNG ,0 ); - vrng->rng = vrng->conf.rng;if (vrng->rng ==NULL ) {error_set (errp,QERR_INVALID_PARAMETER_VALUE ,"rng" ,"a valid object" );return ; } - vrng->vq =virtio_add_queue (vdev,8 , handle_input); +virtio_init (vdev,"virtio-rng" ,VIRTIO_ID_RNG ,0 ); -/* Workaround: Property parsing does not enforce unsigned integers, -* So this is a hack to reject such numbers. */ -if (vrng->conf.max_bytes >INT64_MAX ) { -error_set (errp,QERR_INVALID_PARAMETER_VALUE ,"max-bytes" , -"a non-negative integer below 2^63" ); -return ; - } + vrng->vq =virtio_add_queue (vdev,8 , handle_input); vrng->quota_remaining = vrng->conf.max_bytes; vrng->rate_limit_timer = timer_new_ms(QEMU_CLOCK_VIRTUAL,
Signature · 1 line
1.9.3
PATCH 2/2 virtio-rng: replace error_set calls with error_setg
In reply to John SnowUnder recommendation from Luiz Capitulino, we are changing the error_set calls to error_setg while we are fixing up the error handling pathways of virtio-rng.
--- hw/virtio/virtio-rng.c | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-)
hw/virtio/virtio-rng.c +4 -5
@@ -142,16 +142,15 @@ static void virtio_rng_device_realize(DeviceState *dev, Error **errp)Error *local_err =NULL ;if (!vrng->conf.period_ms >0 ) { -error_set (errp,QERR_INVALID_PARAMETER_VALUE ,"period" , -"a positive number" ); +error_setg (errp,"'period' parameter expects a positive integer" );return ; }/* Workaround: Property parsing does not enforce unsigned integers, * So this is a hack to reject such numbers. */ if (vrng->conf.max_bytes >INT64_MAX ) { -error_set (errp,QERR_INVALID_PARAMETER_VALUE ,"max-bytes" , -"a non-negative integer below 2^63" ); +error_setg (errp,"'max-bytes' parameter must be non-negative, " +"and less than 2^63" );return ; }
@@ -181,7 +180,7 @@ static void virtio_rng_device_realize(DeviceState *dev, Error **errp) vrng->rng = vrng->conf.rng;if (vrng->rng ==NULL ) { -error_set (errp,QERR_INVALID_PARAMETER_VALUE ,"rng" ,"a valid object" ); +error_setg (errp,"'rng' parameter expects a valid object" );return ; }
Signature · 1 line
1.9.3
Quoted 8 lines · John Snow
My previous commit,
713e8a10, did not address the fact that the error checking pathways do not attempt to callvirtio_cleanupand thus can leak memory in hotplug scenarios.This patchset shuffles around the error checking so it does not need to perform said cleanup, and changes the error functions from
error_settoerror_setg.
Quoted 6 lines
John Snow (2):
virtio-rng: Move error-checking forward to prevent memory leak
virtio-rng: replaceerror_setcalls witherror_setghw/virtio/virtio-rng.c | 25 ++++++++++++------------- 1 file changed, 12 insertions(+), 13 deletions(-)
Quoted 8 lines · John Snow
My previous commit,
713e8a10, did not address the fact that the error checking pathways do not attempt to callvirtio_cleanupand thus can leak memory in hotplug scenarios.This patchset shuffles around the error checking so it does not need to perform said cleanup, and changes the error functions from
error_settoerror_setg.
Quoted 6 lines
John Snow (2):
virtio-rng: Move error-checking forward to prevent memory leak
virtio-rng: replaceerror_setcalls witherror_setghw/virtio/virtio-rng.c | 25 ++++++++++++------------- 1 file changed, 12 insertions(+), 13 deletions(-)
Quoted 7 lines · John Snow
My previous commit,
713e8a10, did not address the fact that the error checking pathways do not attempt to callvirtio_cleanupand thus can leak memory in hotplug scenarios.This patchset shuffles around the error checking so it does not need to perform said cleanup, and changes the error functions from
error_settoerror_setg.
Thanks, queued up for 2.2.
Amit