Trivial cleanups around g_malloc()
cover Trivial cleanups around g_malloc()
I'm routing these patches through qemu-trivial, even though some of them touch actively maintained code, and could go through the respective tree instead:
* PATCH 1 block (Kevin, Stefan)
* PATCH 3 KVM (Paolo)
* PATCH 4 migration (Juan, Amit)
* PATCH 5 VNC (Gerd)
If you want me to reroute any of them, let me know.
Markus Armbruster (6):
onenand: g_malloc() can't fail, bury dead error handling
rtl8139: g_malloc() can't fail, bury dead error handling
kvm: g_malloc() can't fail, bury dead error handling
rdma: g_malloc0() can't fail, bury dead error handling
vnc: g_realloc() can't fail, bury dead error handling
translate-all: Use g_try_malloc() for dynamic translator buffer
hw/block/onenand.c | 8 +------- hw/net/rtl8139.c | 14 -------------- kvm-all.c | 4 ---- migration/rdma.c | 3 --- translate-all.c | 2 +- ui/vnc.c | 4 ---- 6 files changed, 2 insertions(+), 33 deletions(-)
Signature · 1 line
1.9.3
PATCH 1/6 onenand: g_malloc() can't fail, bury dead error handling
--- hw/block/onenand.c | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-)
hw/block/onenand.c +1 -7
@@ -346,15 +346,9 @@ static inline int onenand_prog_spare(OneNANDState *s, int sec, int secn,static inline int onenand_erase (OneNANDState *s,int sec,int num) {uint8_t *blankbuf, *tmpbuf; + blankbuf =g_malloc (512 ); -if (!blankbuf) { -return 1 ; - } tmpbuf =g_malloc (512 ); -if (!tmpbuf) { -g_free (blankbuf); -return 1 ; - }memset (blankbuf,0xff ,512 );for (; num >0 ; num--, sec++) {if (s->blk_cur) {
Signature · 1 line
1.9.3
PATCH 2/6 rtl8139: g_malloc() can't fail, bury dead error handling
In reply to Markus Armbruster--- hw/net/rtl8139.c | 14 -------------- 1 file changed, 14 deletions(-)
hw/net/rtl8139.c +0 -14
@@ -2075,20 +2075,6 @@ static int rtl8139_cplus_transmit_one(RTL8139State *s) "length to %d\n", txsize); } -if (!s->cplus_txbuffer) - { -/* out of memory */ - -DPRINTF ("+++ C+ mode transmiter failed to reallocate %d bytes\n" , - s->cplus_txbuffer_len); - -/* update tally counter */ - ++s->tally_counters.TxERR; - ++s->tally_counters.TxAbt; - -return 0 ; - } -/* append more data to the packet */ DPRINTF("+++ C+ mode transmit reading %d bytes from host memory at "
Signature · 1 line
1.9.3
Re: [Qemu-devel] [PATCH 2/6] rtl8139: g_malloc() can't fail, bury dead error handling
Quoted 20 lines · On Tue, 27 Jan 2015 17:38:27 +0100 Markus Armbruster <[email protected]> wrote:
Signed-off-by Markus Armbruster <[email protected]>--- hw/net/rtl8139.c | 14 -------------- 1 file changed, 14 deletions(-)hw/net/rtl8139.c +0
-7@@ -2075,20 +2075,6 @@ static int rtl8139_cplus_transmit_one(RTL8139State *s) "length to %d\n", txsize); } -if (!s->cplus_txbuffer) - { -/* out of memory */ - -DPRINTF ("+++ C+ mode transmiter failed to reallocate %d bytes\n" , - s->cplus_txbuffer_len); -
hw/net/rtl8139.c static int rtl8139_cplus_transmit_one(RTL8139State *s) · line 2080
Quoted 10 lines
hw/net/rtl8139.c +0
-7-/* update tally counter */ - ++s->tally_counters.TxERR; - ++s->tally_counters.TxAbt; - -return 0 ; - } -/* append more data to the packet */ DPRINTF("+++ C+ mode transmit reading %d bytes from host memory at "
Wouldn't it be better to use g_try_malloc() here instead? If the code can handle OOM conditions, I think it's better to continue with a lost packet here than to shut down QEMU the hard way.
(Also looking at the history of that file, the code originally used qemu_malloc() which could fail - but instead of being replaced by g_try_malloc(), it got replaced with g_malloc() instead which was maybe the wrong decision).
Thomas
Re: [Qemu-devel] [PATCH 2/6] rtl8139: g_malloc() can't fail, bury dead error handling
Quoted 32 lines · Thomas Huth
Quoted 29 lines · On Tue, 27 Jan 2015 17:38:27 +0100 Markus Armbruster <[email protected]> wrote:
Signed-off-by Markus Armbruster <[email protected]>--- hw/net/rtl8139.c | 14 -------------- 1 file changed, 14 deletions(-)hw/net/rtl8139.c +0
-14@@ -2075,20 +2075,6 @@ static int rtl8139_cplus_transmit_one(RTL8139State *s) "length to %d\n", txsize); } -if (!s->cplus_txbuffer) - { -/* out of memory */ - -DPRINTF ("+++ C+ mode transmiter failed to reallocate %d bytes\n" , - s->cplus_txbuffer_len); - -/* update tally counter */ - ++s->tally_counters.TxERR; - ++s->tally_counters.TxAbt; - -return 0 ; - } -/* append more data to the packet */
Quoted 9 lines
hw/net/rtl8139.c static int rtl8139_cplus_transmit_one(RTL8139State *s) · line 2080
Quoted 1 line
hw/net/rtl8139.c +0
-0DPRINTF("+++ C+ mode transmit reading %d bytes from host memory at "Wouldn't it be better to use
g_try_malloc()here instead? If the code can handle OOM conditions, I think it's better to continue with a lost packet here than to shut down QEMU the hard way.
(Also looking at the history of that file, the code originally usedqemu_malloc()which could fail - but instead of being replaced byg_try_malloc(), it got replaced withg_malloc()instead which was maybe the wrong decision).
When allocating 64KiB[*] fails, then you're a dead process walking.
By checking for and recovering from such allocation failures, you can try to limp on for a bit. If your programmers have been perfectly diligent, you'll limp on until you die in a non-recoverable allocation failure. More likely, your programmers have been human, and you'll die an ugly death after some unchecked allocation or in some untested recovery that doesn't actually work.
Our strategy is to use allocations that need checking only for large sizes and where recovery is possible without crippling loss of functionality. Those are rare. Gives us a chance to actually test their recovery.
There's ample prededence for ditching untested allocation error recovery in favour of simply terminating the process.
If you think recovery is worthwhile here, post a patch. Ideally extending tests/rtl8139-test.c to cover the error recovery.
[*] That's the value of s->cplus_txbuffer_len (apparent once you peel off the obfuscation).
PATCH 3/6 kvm: g_malloc() can't fail, bury dead error handling
In reply to Markus Armbruster--- kvm-all.c | 4 ---- 1 file changed, 4 deletions(-)
kvm-all.c +0 -4
@@ -2070,10 +2070,6 @@ int kvm_insert_breakpoint(CPUState *cpu, target_ulong addr, } bp =g_malloc (sizeof (struct kvm_sw_breakpoint )); -if (!bp) { -return -ENOMEM ; - } - bp->pc = addr; bp->use_count =1 ; err =kvm_arch_insert_sw_breakpoint (cpu, bp);
Signature · 1 line
1.9.3
Re: [Qemu-devel] [PATCH 3/6] kvm: g_malloc() can't fail, bury dead error handling
Quoted 10 lines · On Tue, 27 Jan 2015 17:38:28 +0100 Markus Armbruster <[email protected]> wrote:
Signed-off-by Markus Armbruster <[email protected]>--- kvm-all.c | 4 ---- 1 file changed, 4 deletions(-)kvm-all.c +0
-0
kvm-all.c int kvm_insert_breakpoint(CPUState *cpu, target_ulong addr, · line 2075
Quoted 10 lines
kvm-all.c +0
-4@@ -2070,10 +2070,6 @@ int kvm_insert_breakpoint(CPUState *cpu, target_ulong addr, } bp =g_malloc (sizeof (struct kvm_sw_breakpoint )); -if (!bp) { -return -ENOMEM ; - } - bp->pc = addr; bp->use_count =1 ; err =kvm_arch_insert_sw_breakpoint (cpu, bp);
I think I'd also use g_try_malloc() here instead. Looks like an error gets reported to GDB when this function returns with an error code, so returning -ENOMEM should be ok here, shouldn't it?
Thomas
Re: [Qemu-devel] [PATCH 3/6] kvm: g_malloc() can't fail, bury dead error handling
Quoted 15 lines · Thomas Huth
Quoted 12 lines · On Tue, 27 Jan 2015 17:38:28 +0100 Markus Armbruster <[email protected]> wrote:
Signed-off-by Markus Armbruster <[email protected]>--- kvm-all.c | 4 ---- 1 file changed, 4 deletions(-)kvm-all.c +0
-0@@ -2070,10 +2070,6 @@ int kvm_insert_breakpoint(CPUState *cpu, target_ulong addr, }
Quoted 12 lines
kvm-all.c int kvm_insert_breakpoint(CPUState *cpu, target_ulong addr, · line 2075
Quoted 8 lines
kvm-all.c +0
-4bp =g_malloc (sizeof (struct kvm_sw_breakpoint )); -if (!bp) { -return -ENOMEM ; - } - bp->pc = addr; bp->use_count =1 ; err =kvm_arch_insert_sw_breakpoint (cpu, bp);I think I'd also use
g_try_malloc()here instead. Looks like an error gets reported to GDB when this function returns with an error code, so returning-ENOMEMshould be ok here, shouldn't it?
Same rationale as for PATCH 2. sizeof(struct kvm_sw_breakpoint) is 40 bytes on my system.
PATCH 4/6 rdma: g_malloc0() can't fail, bury dead error handling
In reply to Markus Armbruster--- migration/rdma.c | 3 --- 1 file changed, 3 deletions(-)
migration/rdma.c +0 -3
@@ -1147,9 +1147,6 @@ static int qemu_rdma_register_and_get_keys(RDMAContext *rdma,/* allocate memory to store chunk MRs */ if (!block->pmr) { block->pmr =g_malloc0 (block->nb_chunks *sizeof (struct ibv_mr *)); -if (!block->pmr) { -return -1 ; - } }/*
Signature · 1 line
1.9.3
PATCH 5/6 vnc: g_realloc() can't fail, bury dead error handling
In reply to Markus Armbruster--- ui/vnc.c | 4 ---- 1 file changed, 4 deletions(-)
ui/vnc.c +0 -4
@@ -659,10 +659,6 @@ void buffer_reserve(Buffer *buffer, size_t len)if ((buffer->capacity - buffer->offset) < len) { buffer->capacity += (len +1024 ); buffer->buffer =g_realloc (buffer->buffer, buffer->capacity); -if (buffer->buffer ==NULL ) { -fprintf (stderr,"vnc: out of memory\n" ); -exit (1 ); - } } }
Signature · 1 line
1.9.3
Re: [Qemu-devel] [PATCH 5/6] vnc: g_realloc() can't fail, bury dead error handling
Quoted 10 lines · On Tue, 27 Jan 2015 17:38:30 +0100 Markus Armbruster <[email protected]> wrote:
Signed-off-by Markus Armbruster <[email protected]>--- ui/vnc.c | 4 ---- 1 file changed, 4 deletions(-)ui/vnc.c +0
-0
ui/vnc.c void buffer_reserve(Buffer *buffer, size_t len) · line 664
Quoted 10 lines
ui/vnc.c +0
-4@@ -659,10 +659,6 @@ void buffer_reserve(Buffer *buffer, size_t len)if ((buffer->capacity - buffer->offset) < len) { buffer->capacity += (len +1024 ); buffer->buffer =g_realloc (buffer->buffer, buffer->capacity); -if (buffer->buffer ==NULL ) { -fprintf (stderr,"vnc: out of memory\n" ); -exit (1 ); - } } }
PATCH 6/6 translate-all: Use g_try_malloc() for dynamic translator buffer
In reply to Markus ArmbrusterThe USE_MMAP code can fail, and the caller handles the failure already. Let the !USE_MMAP code fail as well, for consistency.
--- translate-all.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
translate-all.c +1 -1
@@ -631,7 +631,7 @@ static inline void *alloc_code_gen_buffer(void)#else static inline void *alloc_code_gen_buffer (void ) { -void *buf =g_malloc (tcg_ctx.code_gen_buffer_size); +void *buf =g_try_malloc (tcg_ctx.code_gen_buffer_size);if (buf ==NULL ) {return NULL ;
Signature · 1 line
1.9.3
[your mail used the wrong address for qemu-trivial; I'm redirecting my reply]
Quoted 6 lines · Markus Armbruster
I'm routing these patches through qemu-trivial, even though some of them touch actively maintained code, and could go through the respective tree instead:
* PATCH 1 block (Kevin, Stefan)
Quoted 7 lines
* PATCH 3 KVM (Paolo)
* PATCH 4 migration (Juan, Amit)
* PATCH 5 VNC (Gerd)
If you want me to reroute any of them, let me know.
For all 6:
Patch 2 was the hardest to review (the allocation occurs in context not shown in the patch).
Signature · 2 lines
Eric Blake eblake redhat com +1-919-301-3266 Libvirt virtualization library http://libvirt.org
I screwed up cc: qemu-trivial, my apologies.
Quoted 29 lines · Markus Armbruster
I'm routing these patches through qemu-trivial, even though some of them touch actively maintained code, and could go through the respective tree instead:
* PATCH 1 block (Kevin, Stefan)
* PATCH 3 KVM (Paolo)
* PATCH 4 migration (Juan, Amit)
* PATCH 5 VNC (Gerd)
If you want me to reroute any of them, let me know.
Markus Armbruster (6):
onenand:g_malloc()can't fail, bury dead error handling
rtl8139:g_malloc()can't fail, bury dead error handling
kvm:g_malloc()can't fail, bury dead error handling
rdma:g_malloc0()can't fail, bury dead error handling
vnc:g_realloc()can't fail, bury dead error handling
translate-all: Useg_try_malloc()for dynamic translator bufferhw/block/onenand.c | 8 +------- hw/net/rtl8139.c | 14 -------------- kvm-all.c | 4 ---- migration/rdma.c | 3 --- translate-all.c | 2 +- ui/vnc.c | 4 ---- 6 files changed, 2 insertions(+), 33 deletions(-)
Gonglei
Reviewed-byQuoted 34 lines · Markus Armbruster
I screwed up cc: qemu-trivial, my apologies.
Quoted 29 lines · Markus Armbruster
I'm routing these patches through qemu-trivial, even though some of them touch actively maintained code, and could go through the respective tree instead:
* PATCH 1 block (Kevin, Stefan)
* PATCH 3 KVM (Paolo)
* PATCH 4 migration (Juan, Amit)
* PATCH 5 VNC (Gerd)
If you want me to reroute any of them, let me know.
Markus Armbruster (6):
onenand:g_malloc()can't fail, bury dead error handling
rtl8139:g_malloc()can't fail, bury dead error handling
kvm:g_malloc()can't fail, bury dead error handling
rdma:g_malloc0()can't fail, bury dead error handling
vnc:g_realloc()can't fail, bury dead error handling
translate-all: Useg_try_malloc()for dynamic translator bufferhw/block/onenand.c | 8 +------- hw/net/rtl8139.c | 14 -------------- kvm-all.c | 4 ---- migration/rdma.c | 3 --- translate-all.c | 2 +- ui/vnc.c | 4 ---- 6 files changed, 2 insertions(+), 33 deletions(-)
For serials:
Regards,
-Gonglei
Quoted 16 lines · Gonglei
Quoted 13 lines · Markus Armbruster
I screwed up cc: qemu-trivial, my apologies.
Quoted 8 lines
Markus Armbruster (6):
onenand:g_malloc()can't fail, bury dead error handling
rtl8139:g_malloc()can't fail, bury dead error handling
kvm:g_malloc()can't fail, bury dead error handling
rdma:g_malloc0()can't fail, bury dead error handling
vnc:g_realloc()can't fail, bury dead error handling
translate-all: Useg_try_malloc()for dynamic translator buffer
Quoted 1 line
For serials:
s/serials/series/
Quoted 9 lines
Reviewed-by Gonglei <[email protected]>Regards,
-Gonglei
Signature · 2 lines
Eric Blake eblake redhat com +1-919-301-3266 Libvirt virtualization library http://libvirt.org
Quoted 17 lines · Eric Blake
Quoted 16 lines · Gonglei
Quoted 13 lines · Markus Armbruster
I screwed up cc: qemu-trivial, my apologies.
Quoted 8 lines
Markus Armbruster (6):
onenand:g_malloc()can't fail, bury dead error handling
rtl8139:g_malloc()can't fail, bury dead error handling
kvm:g_malloc()can't fail, bury dead error handling
rdma:g_malloc0()can't fail, bury dead error handling
vnc:g_realloc()can't fail, bury dead error handling
translate-all: Useg_try_malloc()for dynamic translator buffer
Quoted 3 lines
Quoted 1 line
For serials:
s/serials/series/
Yes, thanks :)
Quoted 11 lines
Quoted 10 lines
Reviewed-by Gonglei <[email protected]>Regards,
-Gonglei