Skip to contentSkip to conversation

ppc: get rid of free() (gitlab #1798)

PATCH Applied Daniel Henrique Barboza 3 participants 9 messages Jul 28, 2023 – Sep 7, 2023

lore
Share & tools
lore
#0 lore raw

cover ppc: get rid of free() (gitlab #1798)

Hello,

Here's some trivial changes following Peter's call to arms against free() and friends in gitlab issue #1798 in an attempt to enforce our memory management guidelines [1].

We only have 2 "free()" occurences that needs fixing in the ppc tree. The hard part is to be dilligent to to not introduce new ones.

Michael, feel free to take it via qemu-trivial.

[1] https://www.qemu.org/docs/master/devel/style.html#low-level-memory-management

Daniel Henrique Barboza (2):
hw/ppc: use g_free() in spapr_tce_table_post_load() target/ppc: use g_free() in test_opcode_table()

 hw/ppc/spapr_iommu.c   | 2 +-
 target/ppc/translate.c | 2 +-
 2 files changed, 2 insertions(+), 2 deletions(-)
Signature · 1 line
2.41.0
#1 lore raw

PATCH 1/2 hw/ppc: use g_free() in spapr_tce_table_post_load()

tcet->mig_table is memcpy'ed from tcet->table, which in turn is created via spapr_tce_alloc_table().

Use g_free() instead of free() to deallocate it.

---
 hw/ppc/spapr_iommu.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
hw/ppc/spapr_iommu.c +1 -1
index 63e34d457a..5e3973fc5f 100644
@@ -248,7 +248,7 @@ static int spapr_tce_table_post_load(void *opaque, int version_id)
         memcpy(tcet->table, tcet->mig_table,
                tcet->nb_table * sizeof(tcet->table[0]));
 
-        free(tcet->mig_table);
+        g_free(tcet->mig_table);
         tcet->mig_table = NULL;
     }
 
Signature · 1 line
2.41.0
Reviewed-by on v1 1/2
#2 lore raw

Re: [PATCH for-8.2 1/2] hw/ppc: use g_free() in spapr_tce_table_post_load()

Quoted 2 lines · Daniel Henrique Barboza

tcet->mig_table is memcpy'ed from tcet->table,

The pointer is just copied, not memcpy'd.

Quoted 2 lines

which in turn is created
via spapr_tce_alloc_table().

You could mention that this uses g_new0() for the allocation.

Quoted 12 lines

Use g_free() instead of free() to deallocate it.

---
 hw/ppc/spapr_iommu.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
hw/ppc/spapr_iommu.c +0 -0
index 63e34d457a..5e3973fc5f 100644

hw/ppc/spapr_iommu.c static int spapr_tce_table_post_load(void *opaque, int version_id) · line 253

Quoted 7 lines
hw/ppc/spapr_iommu.c +1 -1
@@ -248,7 +248,7 @@ static int spapr_tce_table_post_load(void *opaque, int version_id)
         memcpy(tcet->table, tcet->mig_table,
                tcet->nb_table * sizeof(tcet->table[0]));
 
-        free(tcet->mig_table);
+        g_free(tcet->mig_table);
         tcet->mig_table = NULL;
     }

thanks
-- PMM

#3 lore raw

PATCH 2/2 target/ppc: use g_free() in test_opcode_table()

In reply to Daniel Henrique Barboza

Use g_free(table[i]) instead of free(table[i]) to comply with QEMU low level memory management guidelines.

---
 target/ppc/translate.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
target/ppc/translate.c +1 -1
index e6a0709066..d90535266e 100644
@@ -7129,7 +7129,7 @@ static int test_opcode_table(opc_handler_t **table, int len)
                 tmp = test_opcode_table(ind_table(table[i]),
                     PPC_CPU_INDIRECT_OPCODES_LEN);
                 if (tmp == 0) {
-                    free(table[i]);
+                    g_free(table[i]);
                     table[i] = &invalid_handler;
                 } else {
                     count++;
Signature · 1 line
2.41.0
#4 lore raw

Re: [PATCH for-8.2 2/2] target/ppc: use g_free() in test_opcode_table()

Quoted 14 lines · Daniel Henrique Barboza

Use g_free(table[i]) instead of free(table[i]) to comply with QEMU low level memory management guidelines.

---
 target/ppc/translate.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
target/ppc/translate.c +0 -0
index e6a0709066..d90535266e 100644

target/ppc/translate.c static int test_opcode_table(opc_handler_t **table, int len) · line 7135

Quoted 8 lines
target/ppc/translate.c +1 -1
@@ -7129,7 +7129,7 @@ static int test_opcode_table(opc_handler_t **table, int len)
                 tmp = test_opcode_table(ind_table(table[i]),
                     PPC_CPU_INDIRECT_OPCODES_LEN);
                 if (tmp == 0) {
-                    free(table[i]);
+                    g_free(table[i]);
                     table[i] = &invalid_handler;
                 } else {
                     count++;

Where is the allocation that this memory is free()ing? I think it is the g_new() in create_new_table(), but the code is a little complicated for me to understand...

thanks
-- PMM

#5 lore raw

Re: [PATCH for-8.2 2/2] target/ppc: use g_free() in test_opcode_table()

Quoted 20 lines · Peter Maydell
Quoted 18 lines · Daniel Henrique Barboza

Use g_free(table[i]) instead of free(table[i]) to comply with QEMU low level memory management guidelines.

---
  target/ppc/translate.c | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
target/ppc/translate.c +0 -1
index e6a0709066..d90535266e 100644
@@ -7129,7 +7129,7 @@ static int test_opcode_table(opc_handler_t **table, int len)
                  tmp = test_opcode_table(ind_table(table[i]),
                      PPC_CPU_INDIRECT_OPCODES_LEN);
                  if (tmp == 0) {
-                    free(table[i]);
Quoted 8 lines

target/ppc/translate.c static int test_opcode_table(opc_handler_t **table, int len) · line 7135

Quoted 4 lines
target/ppc/translate.c +1 -0
+                    g_free(table[i]);
                      table[i] = &invalid_handler;
                  } else {
                      count++;

Where is the allocation that this memory is free()ing? I think it is the g_new() in create_new_table(), but the code is a little complicated for me to understand...

It's on create_new_table() in the same file:

static int create_new_table(opc_handler_t **table, unsigned char idx)

{
    opc_handler_t **tmp;

    tmp = g_new(opc_handler_t *, PPC_CPU_INDIRECT_OPCODES_LEN);
    fill_new_table(tmp, PPC_CPU_INDIRECT_OPCODES_LEN);
    table[idx] = (opc_handler_t *)((uintptr_t)tmp | PPC_INDIRECT);

    return 0;
}

I probably should've mentioned in the commit msg ...

Daniel

Quoted 3 lines

thanks
-- PMM

#6 lore raw
In reply to Daniel Henrique Barboza
Quoted 3 lines · Daniel Henrique Barboza

Here's some trivial changes following Peter's call to arms against free() and friends in gitlab issue #1798 in an attempt to enforce our memory management guidelines [1].

To clarify, this isn't a "call to arms". The issue is marked up as a "bite-sized task", which is to say that it's a potential easy place to start for newcomers to the community who might be making their first contribution to the codebase. The changes it suggests aren't urgent; at most they're a nice-to-have, since glib guarantees that you can mix malloc/free and g_malloc/g_free.

We've had this sitting around as a suggestion on the wiki page for bite-sized-tasks for years, and occasionally people come through and have a go at it. I wanted to clean up and expand on the description of what we had in mind for the change, to give those people a better chance of successfully completing the task.

thanks
-- PMM

#7 lore raw
Quoted 6 lines · Peter Maydell
Quoted 3 lines · Daniel Henrique Barboza

Here's some trivial changes following Peter's call to arms against free() and friends in gitlab issue #1798 in an attempt to enforce our memory management guidelines [1].

Quoted 6 lines

To clarify, this isn't a "call to arms". The issue is marked up as a "bite-sized task", which is to say that it's a potential easy place to start for newcomers to the community who might be making their first contribution to the codebase. The changes it suggests aren't urgent; at most they're a nice-to-have, since glib guarantees that you can mix malloc/free and g_malloc/g_free.

I failed to realized it was a byte sized task :/ and my Coccinelle comment in the bug makes me fell dumb hehe (given that Coccinelle is not newcomer friendly).

Quoted 7 lines

We've had this sitting around as a suggestion on the wiki page for bite-sized-tasks for years, and occasionally people come through and have a go at it. I wanted to clean up and expand on the description of what we had in mind for the change, to give those people a better chance of successfully completing the task.

What we can do then, since I already sent these, is perhaps link these patches as example/template in the gitlab issue later on.

Thanks,

Daniel

Quoted 3 lines

thanks
-- PMM

#8 lore raw
Quoted 28 lines · Daniel Henrique Barboza
Quoted 6 lines · Peter Maydell
Quoted 3 lines · Daniel Henrique Barboza

Here's some trivial changes following Peter's call to arms against free() and friends in gitlab issue #1798 in an attempt to enforce our memory management guidelines [1].

Quoted 6 lines

To clarify, this isn't a "call to arms". The issue is marked up as a "bite-sized task", which is to say that it's a potential easy place to start for newcomers to the community who might be making their first contribution to the codebase. The changes it suggests aren't urgent; at most they're a nice-to-have, since glib guarantees that you can mix malloc/free and g_malloc/g_free.

I failed to realized it was a byte sized task :/ and my Coccinelle comment in the bug makes me fell dumb hehe (given that Coccinelle is not newcomer friendly).

Quoted 7 lines

We've had this sitting around as a suggestion on the wiki page for bite-sized-tasks for years, and occasionally people come through and have a go at it. I wanted to clean up and expand on the description of what we had in mind for the change, to give those people a better chance of successfully completing the task.

Quoted 2 lines

What we can do then, since I already sent these, is perhaps link these patches as example/template in the gitlab issue later on.

Applied to my trivial-patches branch adding suggested commit comment fixes while at it, hopefully there's nothing more to do :)

Thanks,

/mjt