Re: [Qemu-devel] [PATCH v3] qemu-char: don't issue CHR_EVENT_OPEN in a BH

2013-06-04 Thread Hans de Goede

Hi,

On 06/03/2013 10:25 PM, Michael Roth wrote:

snip


To fix these, let's just drop the BH.

Since the initial reasoning for using it still applies to an extent,
work around that by deferring the delivery of CHR_EVENT_OPENED until
after the chardevs have been fully initialized, toward the end of
qmp_chardev_add() (or some cases, qemu_chr_new_from_opts()). This
defers delivery long enough that we can be assured a CharDriverState
is fully initialized before CHR_EVENT_OPENED is sent.

Also, rather than requiring each chardev to do an explicit open, do it
automatically, and allow the small few who don't desire such behavior to
suppress the OPENED-on-init behavior by setting a
'suppress_be_open_on_init' flag.

We additionally add missing OPENED events for stdio backends on w32,
which were previously not being issued, causing us to not recieve the
banner and initial prompts for qmp/hmp.


2 remarks:

1) We already have something very similar to suppress_be_open_on_init
on the frontend side, it is called explicit_fe_open, I suggest calling
the backend variant explicit_be_open for consistency

2) It would be good to do a full audit to check if you've caught all
backends which need suppress_be_open_on_init:
a) check all callers of register_char_driver*
b) check if their open method calls qemu_chr_be_generic_open
c) if it does not they need suppress_be_open_on_init

You've missed at least the spicevmc  spiceport backends,
which both need suppress_be_open_on_init

Regards,

Hans






Reported-by: Stefan Priebe s.pri...@profihost.ag
Cc: qemu-sta...@nongnu.org
Signed-off-by: Michael Roth mdr...@linux.vnet.ibm.com
---
v2-v3:
  * removed artifact in from v1 in backends/baum.c, test build with
BRLAPI=y (Anthony)
  * rebased on latest origin/master

v1-v2:
  * default to sending OPENED on backend init, add flag to suppress
it (Anthony)
  * fix missing OPENED for stdio backends on w32
  * fix missing OPENED when qemu_chr_new_from_opts() doesn't use
qmp_chardev_add()
  * clean up/update commit message

  backends/baum.c   |2 --
  include/sysemu/char.h |2 +-
  qemu-char.c   |   38 +-
  ui/console.c  |1 -
  ui/gtk.c  |1 -
  5 files changed, 18 insertions(+), 26 deletions(-)

diff --git a/backends/baum.c b/backends/baum.c
index 4cba79f..62aa784 100644
--- a/backends/baum.c
+++ b/backends/baum.c
@@ -611,8 +611,6 @@ CharDriverState *chr_baum_init(void)

  qemu_set_fd_handler(baum-brlapi_fd, baum_chr_read, NULL, baum);

-qemu_chr_be_generic_open(chr);
-
  return chr;

  fail:
diff --git a/include/sysemu/char.h b/include/sysemu/char.h
index 5e42c90..b0ae749 100644
--- a/include/sysemu/char.h
+++ b/include/sysemu/char.h
@@ -70,13 +70,13 @@ struct CharDriverState {
  void (*chr_set_echo)(struct CharDriverState *chr, bool echo);
  void (*chr_set_fe_open)(struct CharDriverState *chr, int fe_open);
  void *opaque;
-int idle_tag;
  char *label;
  char *filename;
  int be_open;
  int fe_open;
  int explicit_fe_open;
  int avail_connections;
+bool suppress_be_open_on_init;
  QemuOpts *opts;
  QTAILQ_ENTRY(CharDriverState) next;
  };
diff --git a/qemu-char.c b/qemu-char.c
index d04b429..8043f86 100644
--- a/qemu-char.c
+++ b/qemu-char.c
@@ -110,19 +110,9 @@ void qemu_chr_be_event(CharDriverState *s, int event)
  s-chr_event(s-handler_opaque, event);
  }

-static gboolean qemu_chr_be_generic_open_bh(gpointer opaque)
-{
-CharDriverState *s = opaque;
-qemu_chr_be_event(s, CHR_EVENT_OPENED);
-s-idle_tag = 0;
-return FALSE;
-}
-
  void qemu_chr_be_generic_open(CharDriverState *s)
  {
-if (s-idle_tag == 0) {
-s-idle_tag = g_idle_add(qemu_chr_be_generic_open_bh, s);
-}
+qemu_chr_be_event(s, CHR_EVENT_OPENED);
  }

  int qemu_chr_fe_write(CharDriverState *s, const uint8_t *buf, int len)
@@ -247,6 +237,7 @@ static CharDriverState *qemu_chr_open_null(void)

  chr = g_malloc0(sizeof(CharDriverState));
  chr-chr_write = null_chr_write;
+chr-suppress_be_open_on_init = true;
  return chr;
  }

@@ -504,9 +495,6 @@ static CharDriverState *qemu_chr_open_mux(CharDriverState 
*drv)
  /* Frontend guest-open / -close notification is not support with muxes */
  chr-chr_set_fe_open = NULL;

-/* Muxes are always open on creation */
-qemu_chr_be_generic_open(chr);
-
  return chr;
  }

@@ -883,8 +871,6 @@ static CharDriverState *qemu_chr_open_fd(int fd_in, int 
fd_out)
  chr-chr_update_read_handler = fd_chr_update_read_handler;
  chr-chr_close = fd_chr_close;

-qemu_chr_be_generic_open(chr);
-
  return chr;
  }

@@ -1243,6 +1229,7 @@ static CharDriverState *qemu_chr_open_pty(const char *id,
  chr-chr_update_read_handler = pty_chr_update_read_handler;
  chr-chr_close = pty_chr_close;
  chr-chr_add_watch = pty_chr_add_watch;
+chr-suppress_be_open_on_init = true;

  s-fd = 

Re: [Qemu-devel] [PATCH v3] qemu-char: don't issue CHR_EVENT_OPEN in a BH

2013-06-04 Thread mdroth
On Tue, Jun 04, 2013 at 11:58:12AM +0200, Hans de Goede wrote:
 Hi,
 
 On 06/03/2013 10:25 PM, Michael Roth wrote:
 
 snip
 
 To fix these, let's just drop the BH.
 
 Since the initial reasoning for using it still applies to an extent,
 work around that by deferring the delivery of CHR_EVENT_OPENED until
 after the chardevs have been fully initialized, toward the end of
 qmp_chardev_add() (or some cases, qemu_chr_new_from_opts()). This
 defers delivery long enough that we can be assured a CharDriverState
 is fully initialized before CHR_EVENT_OPENED is sent.
 
 Also, rather than requiring each chardev to do an explicit open, do it
 automatically, and allow the small few who don't desire such behavior to
 suppress the OPENED-on-init behavior by setting a
 'suppress_be_open_on_init' flag.
 
 We additionally add missing OPENED events for stdio backends on w32,
 which were previously not being issued, causing us to not recieve the
 banner and initial prompts for qmp/hmp.
 
 2 remarks:
 
 1) We already have something very similar to suppress_be_open_on_init
 on the frontend side, it is called explicit_fe_open, I suggest calling
 the backend variant explicit_be_open for consistency

I think the naming is a bit confusing at first glance, but the behavior
does seem consistent with what we're trying to do for backends, so I'll
go ahead and make the change for v4.

 
 2) It would be good to do a full audit to check if you've caught all
 backends which need suppress_be_open_on_init:
 a) check all callers of register_char_driver*
 b) check if their open method calls qemu_chr_be_generic_open
 c) if it does not they need suppress_be_open_on_init
 

Agreed; that was the intention here, but it looks like I missed some.

 You've missed at least the spicevmc  spiceport backends,
 which both need suppress_be_open_on_init

Looks like we need one for qemu_chr_open_msmouse() as well. Will fix
these in v4.

Thanks!

 
 Regards,
 
 Hans
 
 
 
 
 
 Reported-by: Stefan Priebe s.pri...@profihost.ag
 Cc: qemu-sta...@nongnu.org
 Signed-off-by: Michael Roth mdr...@linux.vnet.ibm.com
 ---
 v2-v3:
   * removed artifact in from v1 in backends/baum.c, test build with
 BRLAPI=y (Anthony)
   * rebased on latest origin/master
 
 v1-v2:
   * default to sending OPENED on backend init, add flag to suppress
 it (Anthony)
   * fix missing OPENED for stdio backends on w32
   * fix missing OPENED when qemu_chr_new_from_opts() doesn't use
 qmp_chardev_add()
   * clean up/update commit message
 
   backends/baum.c   |2 --
   include/sysemu/char.h |2 +-
   qemu-char.c   |   38 +-
   ui/console.c  |1 -
   ui/gtk.c  |1 -
   5 files changed, 18 insertions(+), 26 deletions(-)
 
 diff --git a/backends/baum.c b/backends/baum.c
 index 4cba79f..62aa784 100644
 --- a/backends/baum.c
 +++ b/backends/baum.c
 @@ -611,8 +611,6 @@ CharDriverState *chr_baum_init(void)
 
   qemu_set_fd_handler(baum-brlapi_fd, baum_chr_read, NULL, baum);
 
 -qemu_chr_be_generic_open(chr);
 -
   return chr;
 
   fail:
 diff --git a/include/sysemu/char.h b/include/sysemu/char.h
 index 5e42c90..b0ae749 100644
 --- a/include/sysemu/char.h
 +++ b/include/sysemu/char.h
 @@ -70,13 +70,13 @@ struct CharDriverState {
   void (*chr_set_echo)(struct CharDriverState *chr, bool echo);
   void (*chr_set_fe_open)(struct CharDriverState *chr, int fe_open);
   void *opaque;
 -int idle_tag;
   char *label;
   char *filename;
   int be_open;
   int fe_open;
   int explicit_fe_open;
   int avail_connections;
 +bool suppress_be_open_on_init;
   QemuOpts *opts;
   QTAILQ_ENTRY(CharDriverState) next;
   };
 diff --git a/qemu-char.c b/qemu-char.c
 index d04b429..8043f86 100644
 --- a/qemu-char.c
 +++ b/qemu-char.c
 @@ -110,19 +110,9 @@ void qemu_chr_be_event(CharDriverState *s, int event)
   s-chr_event(s-handler_opaque, event);
   }
 
 -static gboolean qemu_chr_be_generic_open_bh(gpointer opaque)
 -{
 -CharDriverState *s = opaque;
 -qemu_chr_be_event(s, CHR_EVENT_OPENED);
 -s-idle_tag = 0;
 -return FALSE;
 -}
 -
   void qemu_chr_be_generic_open(CharDriverState *s)
   {
 -if (s-idle_tag == 0) {
 -s-idle_tag = g_idle_add(qemu_chr_be_generic_open_bh, s);
 -}
 +qemu_chr_be_event(s, CHR_EVENT_OPENED);
   }
 
   int qemu_chr_fe_write(CharDriverState *s, const uint8_t *buf, int len)
 @@ -247,6 +237,7 @@ static CharDriverState *qemu_chr_open_null(void)
 
   chr = g_malloc0(sizeof(CharDriverState));
   chr-chr_write = null_chr_write;
 +chr-suppress_be_open_on_init = true;
   return chr;
   }
 
 @@ -504,9 +495,6 @@ static CharDriverState 
 *qemu_chr_open_mux(CharDriverState *drv)
   /* Frontend guest-open / -close notification is not support with muxes 
  */
   chr-chr_set_fe_open = NULL;
 
 -/* Muxes are always open on creation */
 -qemu_chr_be_generic_open(chr);
 -
   return chr;
   

[Qemu-devel] [PATCH v3] qemu-char: don't issue CHR_EVENT_OPEN in a BH

2013-06-03 Thread Michael Roth
When CHR_EVENT_OPENED was initially added, it was CHR_EVENT_RESET,
and it was issued as a bottom-half:

86e94dea5b740dad65446c857f6959eae43e0ba6

Which we basically used to print out a greeting/prompt for the
monitor.

AFAICT the only reason this was ever done in a BH was because in
some cases we'd modify the chr_write handler for a new chardev
backend *after* the site where we issued the reset (see:
86e94d:qemu_chr_open_stdio())

At some point this event was renamed to CHR_EVENT_OPENED, and we've
maintained the use of this BH ever since.

However, due to 9f939df955a4152aad69a19a77e0898631bb2c18, we schedule
the BH via g_idle_add(), which is causing events to sometimes be
delivered after we've already begun processing data from backends,
leading to:

 known bugs:

  QMP:
session negotation resets with OPENED event, in some cases this
is causing new sessions to get sporadically reset

 potential bugs:

  hw/usb/redirect.c:
can_read handler checks for dev-parser != NULL, which may be
true if CLOSED BH has not been executed yet. In the past, OPENED
quiesced outstanding CLOSED events prior to us reading client
data. If it's delayed, our check may allow reads to occur even
though we haven't processed the OPENED event yet, and when we
do finally get the OPENED event, our state may get reset.

  qtest.c:
can begin session before OPENED event is processed, leading to
a spurious reset of the system and irq_levels

  gdbstub.c:
may start a gdb session prior to the machine being paused

To fix these, let's just drop the BH.

Since the initial reasoning for using it still applies to an extent,
work around that by deferring the delivery of CHR_EVENT_OPENED until
after the chardevs have been fully initialized, toward the end of
qmp_chardev_add() (or some cases, qemu_chr_new_from_opts()). This
defers delivery long enough that we can be assured a CharDriverState
is fully initialized before CHR_EVENT_OPENED is sent.

Also, rather than requiring each chardev to do an explicit open, do it
automatically, and allow the small few who don't desire such behavior to
suppress the OPENED-on-init behavior by setting a
'suppress_be_open_on_init' flag.

We additionally add missing OPENED events for stdio backends on w32,
which were previously not being issued, causing us to not recieve the
banner and initial prompts for qmp/hmp.

Reported-by: Stefan Priebe s.pri...@profihost.ag
Cc: qemu-sta...@nongnu.org
Signed-off-by: Michael Roth mdr...@linux.vnet.ibm.com
---
v2-v3:
 * removed artifact in from v1 in backends/baum.c, test build with
   BRLAPI=y (Anthony)
 * rebased on latest origin/master

v1-v2:
 * default to sending OPENED on backend init, add flag to suppress
   it (Anthony)
 * fix missing OPENED for stdio backends on w32
 * fix missing OPENED when qemu_chr_new_from_opts() doesn't use
   qmp_chardev_add()
 * clean up/update commit message

 backends/baum.c   |2 --
 include/sysemu/char.h |2 +-
 qemu-char.c   |   38 +-
 ui/console.c  |1 -
 ui/gtk.c  |1 -
 5 files changed, 18 insertions(+), 26 deletions(-)

diff --git a/backends/baum.c b/backends/baum.c
index 4cba79f..62aa784 100644
--- a/backends/baum.c
+++ b/backends/baum.c
@@ -611,8 +611,6 @@ CharDriverState *chr_baum_init(void)
 
 qemu_set_fd_handler(baum-brlapi_fd, baum_chr_read, NULL, baum);
 
-qemu_chr_be_generic_open(chr);
-
 return chr;
 
 fail:
diff --git a/include/sysemu/char.h b/include/sysemu/char.h
index 5e42c90..b0ae749 100644
--- a/include/sysemu/char.h
+++ b/include/sysemu/char.h
@@ -70,13 +70,13 @@ struct CharDriverState {
 void (*chr_set_echo)(struct CharDriverState *chr, bool echo);
 void (*chr_set_fe_open)(struct CharDriverState *chr, int fe_open);
 void *opaque;
-int idle_tag;
 char *label;
 char *filename;
 int be_open;
 int fe_open;
 int explicit_fe_open;
 int avail_connections;
+bool suppress_be_open_on_init;
 QemuOpts *opts;
 QTAILQ_ENTRY(CharDriverState) next;
 };
diff --git a/qemu-char.c b/qemu-char.c
index d04b429..8043f86 100644
--- a/qemu-char.c
+++ b/qemu-char.c
@@ -110,19 +110,9 @@ void qemu_chr_be_event(CharDriverState *s, int event)
 s-chr_event(s-handler_opaque, event);
 }
 
-static gboolean qemu_chr_be_generic_open_bh(gpointer opaque)
-{
-CharDriverState *s = opaque;
-qemu_chr_be_event(s, CHR_EVENT_OPENED);
-s-idle_tag = 0;
-return FALSE;
-}
-
 void qemu_chr_be_generic_open(CharDriverState *s)
 {
-if (s-idle_tag == 0) {
-s-idle_tag = g_idle_add(qemu_chr_be_generic_open_bh, s);
-}
+qemu_chr_be_event(s, CHR_EVENT_OPENED);
 }
 
 int qemu_chr_fe_write(CharDriverState *s, const uint8_t *buf, int len)
@@ -247,6 +237,7 @@ static CharDriverState *qemu_chr_open_null(void)
 
 chr = g_malloc0(sizeof(CharDriverState));
 chr-chr_write = null_chr_write;
+chr-suppress_be_open_on_init = true;
 return chr;
 }
 
@@ -504,9 

Re: [Qemu-devel] [PATCH v3] qemu-char: don't issue CHR_EVENT_OPEN in a BH

2013-06-03 Thread Anthony Liguori
Michael Roth mdr...@linux.vnet.ibm.com writes:

 When CHR_EVENT_OPENED was initially added, it was CHR_EVENT_RESET,
 and it was issued as a bottom-half:

 86e94dea5b740dad65446c857f6959eae43e0ba6

 Which we basically used to print out a greeting/prompt for the
 monitor.

 AFAICT the only reason this was ever done in a BH was because in
 some cases we'd modify the chr_write handler for a new chardev
 backend *after* the site where we issued the reset (see:
 86e94d:qemu_chr_open_stdio())

 At some point this event was renamed to CHR_EVENT_OPENED, and we've
 maintained the use of this BH ever since.

 However, due to 9f939df955a4152aad69a19a77e0898631bb2c18, we schedule
 the BH via g_idle_add(), which is causing events to sometimes be
 delivered after we've already begun processing data from backends,
 leading to:

  known bugs:

   QMP:
 session negotation resets with OPENED event, in some cases this
 is causing new sessions to get sporadically reset

  potential bugs:

   hw/usb/redirect.c:
 can_read handler checks for dev-parser != NULL, which may be
 true if CLOSED BH has not been executed yet. In the past, OPENED
 quiesced outstanding CLOSED events prior to us reading client
 data. If it's delayed, our check may allow reads to occur even
 though we haven't processed the OPENED event yet, and when we
 do finally get the OPENED event, our state may get reset.

   qtest.c:
 can begin session before OPENED event is processed, leading to
 a spurious reset of the system and irq_levels

   gdbstub.c:
 may start a gdb session prior to the machine being paused

 To fix these, let's just drop the BH.

 Since the initial reasoning for using it still applies to an extent,
 work around that by deferring the delivery of CHR_EVENT_OPENED until
 after the chardevs have been fully initialized, toward the end of
 qmp_chardev_add() (or some cases, qemu_chr_new_from_opts()). This
 defers delivery long enough that we can be assured a CharDriverState
 is fully initialized before CHR_EVENT_OPENED is sent.

 Also, rather than requiring each chardev to do an explicit open, do it
 automatically, and allow the small few who don't desire such behavior to
 suppress the OPENED-on-init behavior by setting a
 'suppress_be_open_on_init' flag.

 We additionally add missing OPENED events for stdio backends on w32,
 which were previously not being issued, causing us to not recieve the
 banner and initial prompts for qmp/hmp.

 Reported-by: Stefan Priebe s.pri...@profihost.ag
 Cc: qemu-sta...@nongnu.org
 Signed-off-by: Michael Roth mdr...@linux.vnet.ibm.com

Reviewed-by: Anthony Liguori aligu...@us.ibm.com

Thanks for digging into this Mike!

Regards,

Anthony Liguori

 ---
 v2-v3:
  * removed artifact in from v1 in backends/baum.c, test build with
BRLAPI=y (Anthony)
  * rebased on latest origin/master

 v1-v2:
  * default to sending OPENED on backend init, add flag to suppress
it (Anthony)
  * fix missing OPENED for stdio backends on w32
  * fix missing OPENED when qemu_chr_new_from_opts() doesn't use
qmp_chardev_add()
  * clean up/update commit message

  backends/baum.c   |2 --
  include/sysemu/char.h |2 +-
  qemu-char.c   |   38 +-
  ui/console.c  |1 -
  ui/gtk.c  |1 -
  5 files changed, 18 insertions(+), 26 deletions(-)

 diff --git a/backends/baum.c b/backends/baum.c
 index 4cba79f..62aa784 100644
 --- a/backends/baum.c
 +++ b/backends/baum.c
 @@ -611,8 +611,6 @@ CharDriverState *chr_baum_init(void)
  
  qemu_set_fd_handler(baum-brlapi_fd, baum_chr_read, NULL, baum);
  
 -qemu_chr_be_generic_open(chr);
 -
  return chr;
  
  fail:
 diff --git a/include/sysemu/char.h b/include/sysemu/char.h
 index 5e42c90..b0ae749 100644
 --- a/include/sysemu/char.h
 +++ b/include/sysemu/char.h
 @@ -70,13 +70,13 @@ struct CharDriverState {
  void (*chr_set_echo)(struct CharDriverState *chr, bool echo);
  void (*chr_set_fe_open)(struct CharDriverState *chr, int fe_open);
  void *opaque;
 -int idle_tag;
  char *label;
  char *filename;
  int be_open;
  int fe_open;
  int explicit_fe_open;
  int avail_connections;
 +bool suppress_be_open_on_init;
  QemuOpts *opts;
  QTAILQ_ENTRY(CharDriverState) next;
  };
 diff --git a/qemu-char.c b/qemu-char.c
 index d04b429..8043f86 100644
 --- a/qemu-char.c
 +++ b/qemu-char.c
 @@ -110,19 +110,9 @@ void qemu_chr_be_event(CharDriverState *s, int event)
  s-chr_event(s-handler_opaque, event);
  }
  
 -static gboolean qemu_chr_be_generic_open_bh(gpointer opaque)
 -{
 -CharDriverState *s = opaque;
 -qemu_chr_be_event(s, CHR_EVENT_OPENED);
 -s-idle_tag = 0;
 -return FALSE;
 -}
 -
  void qemu_chr_be_generic_open(CharDriverState *s)
  {
 -if (s-idle_tag == 0) {
 -s-idle_tag = g_idle_add(qemu_chr_be_generic_open_bh, s);
 -}
 +qemu_chr_be_event(s, CHR_EVENT_OPENED);
  }
  
  int