[PATCH] chardev: delete QemuOpts when a chardev is removed

Paolo Bonzini posted 1 patch 2 years, 4 months ago
Test checkpatch passed
Patches applied successfully (tree, apply log)
git fetch https://github.com/patchew-project/qemu tags/patchew/20211123130915.390848-1-pbonzini@redhat.com
Maintainers: Paolo Bonzini <pbonzini@redhat.com>, "Marc-André Lureau" <marcandre.lureau@redhat.com>
chardev/char.c | 15 +++++++++++++++
1 file changed, 15 insertions(+)
[PATCH] chardev: delete QemuOpts when a chardev is removed
Posted by Paolo Bonzini 2 years, 4 months ago
QEMU cannot add a chardev with the same id as a previously unplugged device.
The root cause of this issue is that the QemuOpts is still registered,
causing qemu_opts_create to fail.  Remove the QemuOpts when a character
device is removed.

Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
---
 chardev/char.c | 15 +++++++++++++++
 1 file changed, 15 insertions(+)

diff --git a/chardev/char.c b/chardev/char.c
index 0169d8dde4..d46b307830 100644
--- a/chardev/char.c
+++ b/chardev/char.c
@@ -277,6 +277,20 @@ static void char_init(Object *obj)
 
 }
 
+static void chr_unparent(Object *obj)
+{
+    const char *id;
+    QemuOpts *opts;
+
+    if (obj->parent == get_chardevs_root()) {
+        id = object_get_canonical_path_component(obj);
+        opts = qemu_opts_find(qemu_find_opts("chardev"), id);
+        if (opts) {
+            qemu_opts_del(opts);
+        }
+    }
+}
+
 static int null_chr_write(Chardev *chr, const uint8_t *buf, int len)
 {
     return len;
@@ -286,6 +300,7 @@ static void char_class_init(ObjectClass *oc, void *data)
 {
     ChardevClass *cc = CHARDEV_CLASS(oc);
 
+    oc->unparent = chr_unparent;
     cc->chr_write = null_chr_write;
     cc->chr_be_event = chr_be_event;
 }
-- 
2.33.1


Re: [PATCH] chardev: delete QemuOpts when a chardev is removed
Posted by Marc-André Lureau 2 years, 4 months ago
On Tue, Nov 23, 2021 at 5:11 PM Paolo Bonzini <pbonzini@redhat.com> wrote:

> QEMU cannot add a chardev with the same id as a previously unplugged
> device.
> The root cause of this issue is that the QemuOpts is still registered,
> causing qemu_opts_create to fail.  Remove the QemuOpts when a character
> device is removed.
>
> Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
>

Reviewed-by: Marc-André Lureau <marcandre.lureau@redhat.com>

---
>  chardev/char.c | 15 +++++++++++++++
>  1 file changed, 15 insertions(+)
>
> diff --git a/chardev/char.c b/chardev/char.c
> index 0169d8dde4..d46b307830 100644
> --- a/chardev/char.c
> +++ b/chardev/char.c
> @@ -277,6 +277,20 @@ static void char_init(Object *obj)
>
>  }
>
> +static void chr_unparent(Object *obj)
> +{
> +    const char *id;
> +    QemuOpts *opts;
> +
> +    if (obj->parent == get_chardevs_root()) {
> +        id = object_get_canonical_path_component(obj);
> +        opts = qemu_opts_find(qemu_find_opts("chardev"), id);
> +        if (opts) {
> +            qemu_opts_del(opts);
> +        }
> +    }
> +}
> +
>  static int null_chr_write(Chardev *chr, const uint8_t *buf, int len)
>  {
>      return len;
> @@ -286,6 +300,7 @@ static void char_class_init(ObjectClass *oc, void
> *data)
>  {
>      ChardevClass *cc = CHARDEV_CLASS(oc);
>
> +    oc->unparent = chr_unparent;
>      cc->chr_write = null_chr_write;
>      cc->chr_be_event = chr_be_event;
>  }
> --
> 2.33.1
>
>
>

-- 
Marc-André Lureau