[RFC] migration/dirtyrate: check malloc() return

jianchunfu posted 1 patch 2 years, 1 month ago
Test checkpatch passed
Patches applied successfully (tree, apply log)
git fetch https://github.com/patchew-project/qemu tags/patchew/20220410035854.97056-1-jianchunfu@cmss.chinamobile.com
Maintainers: Juan Quintela <quintela@redhat.com>, "Dr. David Alan Gilbert" <dgilbert@redhat.com>
migration/dirtyrate.c | 8 ++++++++
1 file changed, 8 insertions(+)
[RFC] migration/dirtyrate: check malloc() return
Posted by jianchunfu 2 years, 1 month ago
Handling potential memory allocation failures in dirtyrate.

Signed-off-by: jianchunfu <jianchunfu@cmss.chinamobile.com>
---
 migration/dirtyrate.c | 8 ++++++++
 1 file changed, 8 insertions(+)

diff --git a/migration/dirtyrate.c b/migration/dirtyrate.c
index aace12a787..5dd40f32c8 100644
--- a/migration/dirtyrate.c
+++ b/migration/dirtyrate.c
@@ -523,9 +523,17 @@ static void calculate_dirtyrate_dirty_ring(struct DirtyRateConfig config)
     }
 
     dirty_pages = malloc(sizeof(*dirty_pages) * nvcpu);
+    if (!dirty_pages) {
+        error_report("malloc dirty pages for vcpus failed.");
+        exit(1);
+    }
 
     DirtyStat.dirty_ring.nvcpu = nvcpu;
     DirtyStat.dirty_ring.rates = malloc(sizeof(DirtyRateVcpu) * nvcpu);
+    if (!DirtyStat.dirty_ring.rates) {
+        error_report("malloc dirty rates for vcpu ring failed.");
+        exit(1);
+    }
 
     dirtyrate_global_dirty_log_start();
 
-- 
2.18.4
Re: [RFC] migration/dirtyrate: check malloc() return
Posted by Richard Henderson 2 years, 1 month ago
On 4/9/22 20:58, jianchunfu wrote:
> Handling potential memory allocation failures in dirtyrate.
> 
> Signed-off-by: jianchunfu <jianchunfu@cmss.chinamobile.com>
> ---
>   migration/dirtyrate.c | 8 ++++++++
>   1 file changed, 8 insertions(+)
> 
> diff --git a/migration/dirtyrate.c b/migration/dirtyrate.c
> index aace12a787..5dd40f32c8 100644
> --- a/migration/dirtyrate.c
> +++ b/migration/dirtyrate.c
> @@ -523,9 +523,17 @@ static void calculate_dirtyrate_dirty_ring(struct DirtyRateConfig config)
>       }
>   
>       dirty_pages = malloc(sizeof(*dirty_pages) * nvcpu);
> +    if (!dirty_pages) {
> +        error_report("malloc dirty pages for vcpus failed.");
> +        exit(1);
> +    }
>   
>       DirtyStat.dirty_ring.nvcpu = nvcpu;
>       DirtyStat.dirty_ring.rates = malloc(sizeof(DirtyRateVcpu) * nvcpu);
> +    if (!DirtyStat.dirty_ring.rates) {
> +        error_report("malloc dirty rates for vcpu ring failed.");
> +        exit(1);
> +    }

You might as well use g_new(), which handles the sizeof and multiplication, and error 
reporting.


r~
Re: [RFC] migration/dirtyrate: check malloc() return
Posted by Alex Bennée 2 years, 1 month ago
Richard Henderson <richard.henderson@linaro.org> writes:

> On 4/9/22 20:58, jianchunfu wrote:
>> Handling potential memory allocation failures in dirtyrate.
>> Signed-off-by: jianchunfu <jianchunfu@cmss.chinamobile.com>
>> ---
>>   migration/dirtyrate.c | 8 ++++++++
>>   1 file changed, 8 insertions(+)
>> diff --git a/migration/dirtyrate.c b/migration/dirtyrate.c
>> index aace12a787..5dd40f32c8 100644
>> --- a/migration/dirtyrate.c
>> +++ b/migration/dirtyrate.c
>> @@ -523,9 +523,17 @@ static void calculate_dirtyrate_dirty_ring(struct DirtyRateConfig config)
>>       }
>>         dirty_pages = malloc(sizeof(*dirty_pages) * nvcpu);
>> +    if (!dirty_pages) {
>> +        error_report("malloc dirty pages for vcpus failed.");
>> +        exit(1);
>> +    }
>>         DirtyStat.dirty_ring.nvcpu = nvcpu;
>>       DirtyStat.dirty_ring.rates = malloc(sizeof(DirtyRateVcpu) * nvcpu);
>> +    if (!DirtyStat.dirty_ring.rates) {
>> +        error_report("malloc dirty rates for vcpu ring failed.");
>> +        exit(1);
>> +    }
>
> You might as well use g_new(), which handles the sizeof and
> multiplication, and error reporting.

It will also assert if the alloc fails. If this is an allocation QEMU
can recover from then you need to use the try_new variants of the
g_malloc/new functions. However here we are exiting so no actual check
is needed as the g_malloc will exit for us.

-- 
Alex Bennée