Skip to contentSkip to conversation

virtio: vdpa: omit check return of g_malloc

PATCH Applied Li Qiang 7 participants 8 messages Aug 19, 2020 – Sep 23, 2020

lore
Share & tools
lore

Li Qiang

author
#0 lore raw

PATCH 1/1 virtio: vdpa: omit check return of g_malloc

If g_malloc fails, the application will be terminated. No need to check the return value of g_malloc.

---
 hw/virtio/vhost-vdpa.c | 7 +------
 1 file changed, 1 insertion(+), 6 deletions(-)
hw/virtio/vhost-vdpa.c +1 -6
index 4580f3efd8..403ae3ae07 100644
@@ -320,10 +320,8 @@ static int vhost_vdpa_set_config(struct vhost_dev *dev, const uint8_t *data,
     struct vhost_vdpa_config *config;
     int ret;
     unsigned long config_size = offsetof(struct vhost_vdpa_config, buf);
+
     config = g_malloc(size + config_size);
-    if (config == NULL) {
-        return -1;
-    }
     config->off = offset;
     config->len = size;
     memcpy(config->buf, data, size);
@@ -340,9 +338,6 @@ static int vhost_vdpa_get_config(struct vhost_dev *dev, uint8_t *config,
     int ret;
 
     v_config = g_malloc(config_len + config_size);
-    if (v_config == NULL) {
-        return -1;
-    }
     v_config->len = config_len;
     v_config->off = 0;
     ret = vhost_vdpa_call(dev, VHOST_VDPA_GET_CONFIG, v_config);
Signature · 1 line
2.17.1
#1 lore raw
Quoted 1 line · Li Qiang

If g_malloc fails, the application will be terminated.

Which we don't want... better to use g_try_malloc() instead?

Quoted 34 lines

No need to check the return value of g_malloc.

---
 hw/virtio/vhost-vdpa.c | 7 +------
 1 file changed, 1 insertion(+), 6 deletions(-)
hw/virtio/vhost-vdpa.c +1 -6
index 4580f3efd8..403ae3ae07 100644
@@ -320,10 +320,8 @@ static int vhost_vdpa_set_config(struct vhost_dev *dev, const uint8_t *data,
     struct vhost_vdpa_config *config;
     int ret;
     unsigned long config_size = offsetof(struct vhost_vdpa_config, buf);
+
     config = g_malloc(size + config_size);
-    if (config == NULL) {
-        return -1;
-    }
     config->off = offset;
     config->len = size;
     memcpy(config->buf, data, size);
@@ -340,9 +338,6 @@ static int vhost_vdpa_get_config(struct vhost_dev *dev, uint8_t *config,
     int ret;
 
     v_config = g_malloc(config_len + config_size);
-    if (v_config == NULL) {
-        return -1;
-    }
     v_config->len = config_len;
     v_config->off = 0;
     ret = vhost_vdpa_call(dev, VHOST_VDPA_GET_CONFIG, v_config);
#2 lore raw
Quoted 5 lines · Philippe Mathieu-Daudé <[email protected]> 于2020年8月19日周三 下午11:07写道:
Quoted 1 line · Li Qiang

If g_malloc fails, the application will be terminated.

Which we don't want... better to use g_try_malloc() instead?

I don't think so. If g_malloc return NULL it means a critical situation I think terminate the application
is OK. Though I don't find any rule/practices the qemu code base uses g_malloc far more than
g_try_malloc.

Thanks,
Li Qiang

Quoted 36 lines
Quoted 35 lines

No need to check the return value of g_malloc.

---
 hw/virtio/vhost-vdpa.c | 7 +------
 1 file changed, 1 insertion(+), 6 deletions(-)
hw/virtio/vhost-vdpa.c +1 -6
index 4580f3efd8..403ae3ae07 100644
@@ -320,10 +320,8 @@ static int vhost_vdpa_set_config(struct vhost_dev *dev, const uint8_t *data,
     struct vhost_vdpa_config *config;
     int ret;
     unsigned long config_size = offsetof(struct vhost_vdpa_config, buf);
+
     config = g_malloc(size + config_size);
-    if (config == NULL) {
-        return -1;
-    }
     config->off = offset;
     config->len = size;
     memcpy(config->buf, data, size);
@@ -340,9 +338,6 @@ static int vhost_vdpa_get_config(struct vhost_dev *dev, uint8_t *config,
     int ret;
 
     v_config = g_malloc(config_len + config_size);
-    if (v_config == NULL) {
-        return -1;
-    }
     v_config->len = config_len;
     v_config->off = 0;
     ret = vhost_vdpa_call(dev, VHOST_VDPA_GET_CONFIG, v_config);
#3 lore raw
Quoted 5 lines · Li Qiang
Quoted 4 lines · Philippe Mathieu-Daudé <[email protected]> 于2020年8月19日周三 下午11:07写道:
Quoted 1 line · Li Qiang

If g_malloc fails, the application will be terminated.

Quoted 7 lines
Quoted 1 line

Which we don't want... better to use g_try_malloc() instead?

I don't think so. If g_malloc return NULL it means a critical situation I think terminate the application
is OK. Though I don't find any rule/practices the qemu code base uses g_malloc far more than
g_try_malloc.

g_try_malloc is only for cases you could recover from, by either deferring or doing something else. A straight out of memory failure is fatal.

Arguably a bunch of the try_malloc's in the code base should be straight mallocs. The ELF loaders load_symbols does it because I guess having the symbols is a bonus and you could still run the program if a) there was enough memory to run and b) your symbol table was very large.

Quoted 40 lines

Thanks,
Li Qiang

Quoted 36 lines
Quoted 35 lines

No need to check the return value of g_malloc.

---
 hw/virtio/vhost-vdpa.c | 7 +------
 1 file changed, 1 insertion(+), 6 deletions(-)
hw/virtio/vhost-vdpa.c +1 -6
index 4580f3efd8..403ae3ae07 100644
@@ -320,10 +320,8 @@ static int vhost_vdpa_set_config(struct vhost_dev *dev, const uint8_t *data,
     struct vhost_vdpa_config *config;
     int ret;
     unsigned long config_size = offsetof(struct vhost_vdpa_config, buf);
+
     config = g_malloc(size + config_size);
-    if (config == NULL) {
-        return -1;
-    }
     config->off = offset;
     config->len = size;
     memcpy(config->buf, data, size);
@@ -340,9 +338,6 @@ static int vhost_vdpa_get_config(struct vhost_dev *dev, uint8_t *config,
     int ret;
 
     v_config = g_malloc(config_len + config_size);
-    if (v_config == NULL) {
-        return -1;
-    }
     v_config->len = config_len;
     v_config->off = 0;
     ret = vhost_vdpa_call(dev, VHOST_VDPA_GET_CONFIG, v_config);
Signature · 1 line
Alex Bennée
#4 lore raw
Quoted 16 lines · Alex Bennée
Quoted 5 lines · Li Qiang
Quoted 4 lines · Philippe Mathieu-Daudé <[email protected]> 于2020年8月19日周三 下午11:07写道:
Quoted 1 line · Li Qiang

If g_malloc fails, the application will be terminated.

Quoted 7 lines
Quoted 1 line

Which we don't want... better to use g_try_malloc() instead?

I don't think so. If g_malloc return NULL it means a critical situation I think terminate the application
is OK. Though I don't find any rule/practices the qemu code base uses g_malloc far more than
g_try_malloc.

Quoted 8 lines

g_try_malloc is only for cases you could recover from, by either deferring or doing something else. A straight out of memory failure is fatal.

Arguably a bunch of the try_malloc's in the code base should be straight mallocs. The ELF loaders load_symbols does it because I guess having the symbols is a bonus and you could still run the program if a) there was enough memory to run and b) your symbol table was very large.

If the amount of memory being allocated is under user control (such as from an elf header or qcow2 image), you want g_try_malloc() to prevent against malicious users requesting outrageous amounts. But if it is solely under programmer control, where the maximum amount requested is not going to be a problem unless you are already truly out of memory for other reasons, g_malloc() is appropriate. A potential rule of thumb might be that if you know it is less than 1M of memory, it's not worth trying to recover from.

Signature · 3 lines
Eric Blake, Principal Software Engineer
Red Hat, Inc.           +1-919-301-3226
Virtualization:  qemu.org | libvirt.org
Reviewed-by on v1 1/1
#5 lore raw
In reply to Li Qiang
Quoted 26 lines · Li Qiang

If g_malloc fails, the application will be terminated. No need to check the return value of g_malloc.

---
 hw/virtio/vhost-vdpa.c | 7 +------
 1 file changed, 1 insertion(+), 6 deletions(-)
hw/virtio/vhost-vdpa.c +1 -3
index 4580f3efd8..403ae3ae07 100644
@@ -320,10 +320,8 @@ static int vhost_vdpa_set_config(struct vhost_dev *dev, const uint8_t *data,
     struct vhost_vdpa_config *config;
     int ret;
     unsigned long config_size = offsetof(struct vhost_vdpa_config, buf);
+
     config = g_malloc(size + config_size);
-    if (config == NULL) {
-        return -1;
-    }
     config->off = offset;
     config->len = size;
     memcpy(config->buf, data, size);

hw/virtio/vhost-vdpa.c static int vhost_vdpa_get_config(struct vhost_dev *dev, uint8_t *config, · line 343

Quoted 9 lines
hw/virtio/vhost-vdpa.c +0 -3
@@ -340,9 +338,6 @@ static int vhost_vdpa_get_config(struct vhost_dev *dev, uint8_t *config,
     int ret;
 
     v_config = g_malloc(config_len + config_size);
-    if (v_config == NULL) {
-        return -1;
-    }
     v_config->len = config_len;
     v_config->off = 0;
     ret = vhost_vdpa_call(dev, VHOST_VDPA_GET_CONFIG, v_config);
#6 lore raw

Le 18/09/2020 à 14:53, Laurent Vivier a écrit :

Quoted 28 lines · Li Qiang
Quoted 27 lines

If g_malloc fails, the application will be terminated. No need to check the return value of g_malloc.

---
 hw/virtio/vhost-vdpa.c | 7 +------
 1 file changed, 1 insertion(+), 6 deletions(-)
hw/virtio/vhost-vdpa.c +1 -3
index 4580f3efd8..403ae3ae07 100644
@@ -320,10 +320,8 @@ static int vhost_vdpa_set_config(struct vhost_dev *dev, const uint8_t *data,
     struct vhost_vdpa_config *config;
     int ret;
     unsigned long config_size = offsetof(struct vhost_vdpa_config, buf);
+
     config = g_malloc(size + config_size);
-    if (config == NULL) {
-        return -1;
-    }
     config->off = offset;
     config->len = size;
     memcpy(config->buf, data, size);
@@ -340,9 +338,6 @@ static int vhost_vdpa_get_config(struct vhost_dev *dev, uint8_t *config,
     int ret;
 
Quoted 12 lines

hw/virtio/vhost-vdpa.c static int vhost_vdpa_get_config(struct vhost_dev *dev, uint8_t *config, · line 343

Quoted 8 lines
hw/virtio/vhost-vdpa.c +0 -3
     v_config = g_malloc(config_len + config_size);
-    if (v_config == NULL) {
-        return -1;
-    }
     v_config->len = config_len;
     v_config->off = 0;
     ret = vhost_vdpa_call(dev, VHOST_VDPA_GET_CONFIG, v_config);

Applied to my trivial-patches branch.

Thanks,
Laurent

Reviewed-by on v1 1/1
#7 lore raw
In reply to Li Qiang
Quoted 4 lines · Li Qiang

If g_malloc fails, the application will be terminated. No need to check the return value of g_malloc.

I think this is fine because a failure to config the dev would just cause an error_report later and it's extremely unlikely you don't have enough memory to get that far anyway.

Signature · 1 line
Alex Bennée