qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: Zheng Huang <hz1624917200@gmail.com>
To: qemu-devel@nongnu.org
Subject: [PATCH] hw/9pfs: add cleanup operation for 9p-synth
Date: Mon, 31 Mar 2025 21:52:31 +0800	[thread overview]
Message-ID: <a4e34adc-a425-4183-bb4f-f1b8197125eb@gmail.com> (raw)

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.

Signed-off-by: Zheng Huang <hz1624917200@gmail.com>


---
 hw/9pfs/9p-synth.c | 28 ++++++++++++++++++++++++++++
 1 file changed, 28 insertions(+)

diff --git a/hw/9pfs/9p-synth.c b/hw/9pfs/9p-synth.c
index 2abaf3a291..ead8b9e3be 100644
--- a/hw/9pfs/9p-synth.c
+++ b/hw/9pfs/9p-synth.c
@@ -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,
-- 
2.34.1



             reply	other threads:[~2025-03-31 13:53 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-03-31 13:52 Zheng Huang [this message]
2025-04-04 13:59 ` [PATCH] hw/9pfs: add cleanup operation for 9p-synth Christian Schoenebeck
2025-04-04 14:38   ` Greg Kurz

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=a4e34adc-a425-4183-bb4f-f1b8197125eb@gmail.com \
    --to=hz1624917200@gmail.com \
    --cc=qemu-devel@nongnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).