Skip to contentSkip to conversation

hw/9pfs: add cleanup operation for 9p-synth

PATCH In review Zheng Huang 3 participants 3 messages Mar 31, 2025 – Apr 4, 2025

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 hw/9pfs: add cleanup operation for 9p-synth

Hi,
This patch adds a cleanup operation for 9p-synth, which fixes a memory leak bug in synth_init() and other related operations. All child nodes of synth_root need to be freed before the entire filesystem exits.

If you have any better ideas for the implementation, please feel free to share them.

---
 hw/9pfs/9p-synth.c | 28 ++++++++++++++++++++++++++++
 1 file changed, 28 insertions(+)
hw/9pfs/9p-synth.c +28 -0
index 2abaf3a291..ead8b9e3be 100644
@@ -24,6 +24,7 @@
 #include "qemu/rcu.h"
 #include "qemu/rcu_queue.h"
 #include "qemu/cutils.h"
+#include "qobject/qlist.h"
 #include "system/qtest.h"
 
 /* Root node for synth file system */
@@ -136,6 +137,19 @@ int qemu_v9fs_synth_add_file(V9fsSynthNode *parent, int mode,
     return 0;
 }
 
+// Must call after get synth_mutex
+static void v9fs_recursive_free_node(V9fsSynthNode *node)
+{
+    V9fsSynthNode *entry;
+
+    for (entry = QLIST_FIRST(&node->child); entry;) {
+        V9fsSynthNode *next = QLIST_NEXT(entry, sibling);
+        v9fs_recursive_free_node(entry);
+        g_free(entry);
+        entry = next;
+    }
+}
+
 static void synth_fill_statbuf(V9fsSynthNode *node, struct stat *stbuf)
 {
     stbuf->st_dev = 0;
@@ -615,8 +629,22 @@ static int synth_init(FsContext *ctx, Error **errp)
     return 0;
 }
 
+
+static void synth_cleanup(FsContext *ctx)
+{
+    // recursively free all child nodes of synth_root
+    // V9fsSynthNode *tmp;
+    QEMU_LOCK_GUARD(&synth_mutex);
+    v9fs_recursive_free_node(&synth_root);
+    // QLIST_FOREACH(tmp, &synth_root.child, sibling) {
+    //     v9fs_recursive_free_node(tmp);
+    // }
+    QLIST_INIT(&synth_root.child);
+}
+
 FileOperations synth_ops = {
     .init         = synth_init,
+    .cleanup      = synth_cleanup,
     .lstat        = synth_lstat,
     .readlink     = synth_readlink,
     .close        = synth_close,
Signature · 1 line
2.34.1
#1 lore raw
Quoted 1 line · CEST Zheng Huang

Hi,

Hi!

Quoted 2 lines

This patch adds a cleanup operation for 9p-synth, which fixes a memory leak bug in synth_init() and other related operations.

Which other operations?

Quoted 2 lines

All child nodes
of synth_root need to be freed before the entire filesystem exits.

I assume this is a theoretical fix, because I currently don't see how this could result in memory being leaked in practice. The synth fs driver is just used for 9pfs's automated test cases. Shortly after cleanup handler would be called, the entire process is torn down anyway, and with that all memory being freed automatically.

Quoted 2 lines

If you have any better ideas for the implementation, please feel free to share them.

By using two nested loops in synth_cleanup()? One loop for walking vertically (child) and one loop for walking horizontally (sibling). Then you could just open code everything within synth_cleanup() instead.

However I don't see a real reason for this patch in the first place.

Quoted 43 lines
---
 hw/9pfs/9p-synth.c | 28 ++++++++++++++++++++++++++++
 1 file changed, 28 insertions(+)
hw/9pfs/9p-synth.c +14 -0
index 2abaf3a291..ead8b9e3be 100644
@@ -24,6 +24,7 @@
 #include "qemu/rcu.h"
 #include "qemu/rcu_queue.h"
 #include "qemu/cutils.h"
+#include "qobject/qlist.h"
 #include "system/qtest.h"
 
 /* Root node for synth file system */
@@ -136,6 +137,19 @@ int qemu_v9fs_synth_add_file(V9fsSynthNode *parent, int mode,
     return 0;
 }
 
+// Must call after get synth_mutex
+static void v9fs_recursive_free_node(V9fsSynthNode *node)
+{
+    V9fsSynthNode *entry;
+
+    for (entry = QLIST_FIRST(&node->child); entry;) {
+        V9fsSynthNode *next = QLIST_NEXT(entry, sibling);
+        v9fs_recursive_free_node(entry);
+        g_free(entry);
+        entry = next;
+    }
+}
+
 static void synth_fill_statbuf(V9fsSynthNode *node, struct stat *stbuf)
 {
     stbuf->st_dev = 0;
@@ -615,8 +629,22 @@ static int synth_init(FsContext *ctx, Error **errp)
     return 0;
 }
 

hw/9pfs/9p-synth.c static int synth_init(FsContext *ctx, Error **errp) · line 641

Quoted 10 lines
hw/9pfs/9p-synth.c +10 -0
+
+static void synth_cleanup(FsContext *ctx)
+{
+    // recursively free all child nodes of synth_root
+    // V9fsSynthNode *tmp;
+    QEMU_LOCK_GUARD(&synth_mutex);
+    v9fs_recursive_free_node(&synth_root);
+    // QLIST_FOREACH(tmp, &synth_root.child, sibling) {
+    //     v9fs_recursive_free_node(tmp);
+    // }

No commented code in patch submissions, please.

/Christian

Quoted 10 lines
hw/9pfs/9p-synth.c +4 -0
+    QLIST_INIT(&synth_root.child);
+}
+
 FileOperations synth_ops = {
     .init         = synth_init,
+    .cleanup      = synth_cleanup,
     .lstat        = synth_lstat,
     .readlink     = synth_readlink,
     .close        = synth_close,
#2 lore raw
Quoted 5 lines · On Fri, 04 Apr 2025 15:59:54 +0200 Christian Schoenebeck…
Quoted 1 line · CEST Zheng Huang

Hi,

Hi!

Hi Zheng !

We certainly appreciate politeness here but we only want relevant details in what will become the commit message. Anything else you want to share that doesn't carry useful information about the patch itself should go below the '---' after your Signed-off-by.

Quoted 5 lines
Quoted 2 lines

This patch adds a cleanup operation for 9p-synth, which fixes a memory leak bug in synth_init() and other related operations.

Which other operations?

Quoted 9 lines
Quoted 2 lines

All child nodes
of synth_root need to be freed before the entire filesystem exits.

I assume this is a theoretical fix, because I currently don't see how this could result in memory being leaked in practice. The synth fs driver is just used for 9pfs's automated test cases. Shortly after cleanup handler would be called, the entire process is torn down anyway, and with that all memory being freed automatically.

FWIW it can be considered a good general practice to match every g_new and friends with a g_free. This would silent any suspicion of memory leak ;-)

Quoted 9 lines
Quoted 2 lines

If you have any better ideas for the implementation, please feel free to share them.

By using two nested loops in synth_cleanup()? One loop for walking vertically (child) and one loop for walking horizontally (sibling). Then you could just open code everything within synth_cleanup() instead.

However I don't see a real reason for this patch in the first place.

We can imagine this could be required if we decide to support hot unplug of 9p backends but I don't believe this is something we need anytime soon.

Quoted 58 lines
Quoted 43 lines
---
 hw/9pfs/9p-synth.c | 28 ++++++++++++++++++++++++++++
 1 file changed, 28 insertions(+)
hw/9pfs/9p-synth.c +14 -0
index 2abaf3a291..ead8b9e3be 100644
@@ -24,6 +24,7 @@
 #include "qemu/rcu.h"
 #include "qemu/rcu_queue.h"
 #include "qemu/cutils.h"
+#include "qobject/qlist.h"
 #include "system/qtest.h"
 
 /* Root node for synth file system */
@@ -136,6 +137,19 @@ int qemu_v9fs_synth_add_file(V9fsSynthNode *parent, int mode,
     return 0;
 }
 
+// Must call after get synth_mutex
+static void v9fs_recursive_free_node(V9fsSynthNode *node)
+{
+    V9fsSynthNode *entry;
+
+    for (entry = QLIST_FIRST(&node->child); entry;) {
+        V9fsSynthNode *next = QLIST_NEXT(entry, sibling);
+        v9fs_recursive_free_node(entry);
+        g_free(entry);
+        entry = next;
+    }
+}
+
 static void synth_fill_statbuf(V9fsSynthNode *node, struct stat *stbuf)
 {
     stbuf->st_dev = 0;
@@ -615,8 +629,22 @@ static int synth_init(FsContext *ctx, Error **errp)
     return 0;
 }
 

hw/9pfs/9p-synth.c static int synth_init(FsContext *ctx, Error **errp) · line 641

Quoted 10 lines
hw/9pfs/9p-synth.c +10 -0
+
+static void synth_cleanup(FsContext *ctx)
+{
+    // recursively free all child nodes of synth_root
+    // V9fsSynthNode *tmp;
+    QEMU_LOCK_GUARD(&synth_mutex);
+    v9fs_recursive_free_node(&synth_root);
+    // QLIST_FOREACH(tmp, &synth_root.child, sibling) {
+    //     v9fs_recursive_free_node(tmp);
+    // }

No commented code in patch submissions, please.

/Christian

Quoted 12 lines

hw/9pfs/9p-synth.c static int synth_init(FsContext *ctx, Error **errp) · line 650

Quoted 10 lines
hw/9pfs/9p-synth.c +4 -0
+    QLIST_INIT(&synth_root.child);
+}
+
 FileOperations synth_ops = {
     .init         = synth_init,
+    .cleanup      = synth_cleanup,
     .lstat        = synth_lstat,
     .readlink     = synth_readlink,
     .close        = synth_close,

Cheers,

Signature · 1 line
Greg