All of lore.kernel.org
 help / color / mirror / Atom feed
* [Qemu-devel] [PATCH] Moving alarm_timer assignment before atexit()
@ 2013-08-06 23:29 Amos Kong
  2013-08-07  6:39 ` Laszlo Ersek
  0 siblings, 1 reply; 5+ messages in thread
From: Amos Kong @ 2013-08-06 23:29 UTC (permalink / raw)
  To: qemu-devel; +Cc: vyasevic, pbonzini, aliguori, lersek

We register exit clean function by atexit(),
but alarm_timer is NULL here. If exit is caused
between atexit() and alarm_timer assignment,
real timer can't be cleaned. So move alarm_timer
assignment before atexit().

Signed-off-by: Amos Kong <akong@redhat.com>
---
 qemu-timer.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/qemu-timer.c b/qemu-timer.c
index b2d95e2..9490105 100644
--- a/qemu-timer.c
+++ b/qemu-timer.c
@@ -767,11 +767,11 @@ int init_timer_alarm(void)
         goto fail;
     }
 
+    alarm_timer = t;
     atexit(quit_timers);
 #ifdef CONFIG_POSIX
     pthread_atfork(NULL, NULL, reinit_timers);
 #endif
-    alarm_timer = t;
     return 0;
 
 fail:
-- 
1.8.3.1

^ permalink raw reply related	[flat|nested] 5+ messages in thread

* Re: [Qemu-devel] [PATCH] Moving alarm_timer assignment before atexit()
  2013-08-06 23:29 [Qemu-devel] [PATCH] Moving alarm_timer assignment before atexit() Amos Kong
@ 2013-08-07  6:39 ` Laszlo Ersek
  2013-08-07  7:57   ` Stefan Hajnoczi
  0 siblings, 1 reply; 5+ messages in thread
From: Laszlo Ersek @ 2013-08-07  6:39 UTC (permalink / raw)
  To: Amos Kong; +Cc: vyasevic, pbonzini, aliguori, qemu-devel

On 08/07/13 01:29, Amos Kong wrote:
> We register exit clean function by atexit(),
> but alarm_timer is NULL here. If exit is caused
> between atexit() and alarm_timer assignment,
> real timer can't be cleaned.

That's correct in general, but I don't see how it could happen in the
code being patched. pthread_atfork() won't call exit().

Thanks,
Laszlo

> So move alarm_timer
> assignment before atexit().
> 
> Signed-off-by: Amos Kong <akong@redhat.com>
> ---
>  qemu-timer.c | 2 +-
>  1 file changed, 1 insertion(+), 1 deletion(-)
> 
> diff --git a/qemu-timer.c b/qemu-timer.c
> index b2d95e2..9490105 100644
> --- a/qemu-timer.c
> +++ b/qemu-timer.c
> @@ -767,11 +767,11 @@ int init_timer_alarm(void)
>          goto fail;
>      }
>  
> +    alarm_timer = t;
>      atexit(quit_timers);
>  #ifdef CONFIG_POSIX
>      pthread_atfork(NULL, NULL, reinit_timers);
>  #endif
> -    alarm_timer = t;
>      return 0;
>  
>  fail:
> 

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [Qemu-devel] [PATCH] Moving alarm_timer assignment before atexit()
  2013-08-07  6:39 ` Laszlo Ersek
@ 2013-08-07  7:57   ` Stefan Hajnoczi
  2013-08-07  8:17     ` Amos Kong
  0 siblings, 1 reply; 5+ messages in thread
From: Stefan Hajnoczi @ 2013-08-07  7:57 UTC (permalink / raw)
  To: Laszlo Ersek; +Cc: vyasevic, pbonzini, aliguori, Amos Kong, qemu-devel

On Wed, Aug 07, 2013 at 08:39:19AM +0200, Laszlo Ersek wrote:
> On 08/07/13 01:29, Amos Kong wrote:
> > We register exit clean function by atexit(),
> > but alarm_timer is NULL here. If exit is caused
> > between atexit() and alarm_timer assignment,
> > real timer can't be cleaned.
> 
> That's correct in general, but I don't see how it could happen in the
> code being patched. pthread_atfork() won't call exit().

Agreed.  I can remember thinking about this when reading the code and
deciding not to bother changing it.

Since the patch is on the list though, we might as well apply it.

The only thing I suggest changing is to note that this is currently not
a bug, just a clean-up.

Reviewed-by: Stefan Hajnoczi <stefanha@redhat.com>

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [Qemu-devel] [PATCH] Moving alarm_timer assignment before atexit()
  2013-08-07  7:57   ` Stefan Hajnoczi
@ 2013-08-07  8:17     ` Amos Kong
  2013-08-08  8:19       ` Stefan Hajnoczi
  0 siblings, 1 reply; 5+ messages in thread
From: Amos Kong @ 2013-08-07  8:17 UTC (permalink / raw)
  To: Stefan Hajnoczi; +Cc: vyasevic, pbonzini, aliguori, Laszlo Ersek, qemu-devel

On Wed, Aug 07, 2013 at 09:57:19AM +0200, Stefan Hajnoczi wrote:
> On Wed, Aug 07, 2013 at 08:39:19AM +0200, Laszlo Ersek wrote:
> > On 08/07/13 01:29, Amos Kong wrote:
> > > We register exit clean function by atexit(),
> > > but alarm_timer is NULL here. If exit is caused
> > > between atexit() and alarm_timer assignment,
> > > real timer can't be cleaned.
> > 
> > That's correct in general, but I don't see how it could happen in the
> > code being patched. pthread_atfork() won't call exit().

I try to sleep 10 seconds after atexit(), but no crash occurred when I
killed qemu process.

atexit(quit_timer);
sleep(10);            // kill qemu by 'pkill qemu', no crash
pthread_atfork();
alarm_timer = t;

> Agreed.  I can remember thinking about this when reading the code and
> deciding not to bother changing it.
> 
> Since the patch is on the list though, we might as well apply it.
> 
> The only thing I suggest changing is to note that this is currently not
> a bug, just a clean-up.

It seems just a cleanup.
 
> Reviewed-by: Stefan Hajnoczi <stefanha@redhat.com>


BTW, can we add a check in quit_timers() to avoid one kind of crash
(try to quit the uninited timers, or quit timer repeatedly) ?


diff --git a/qemu-timer.c b/qemu-timer.c
index b2d95e2..023e4ae 100644
--- a/qemu-timer.c
+++ b/qemu-timer.c
@@ -728,8 +728,10 @@ static void win32_rearm_timer(struct
qemu_alarm_timer *t,
 static void quit_timers(void)
 {
     struct qemu_alarm_timer *t = alarm_timer;
-    alarm_timer = NULL;
-    t->stop(t);
+    if (t) {
+        alarm_timer = NULL;
+        t->stop(t);
+    }
 }
 
 #ifdef CONFIG_POSIX

-- 
			Amos.

^ permalink raw reply related	[flat|nested] 5+ messages in thread

* Re: [Qemu-devel] [PATCH] Moving alarm_timer assignment before atexit()
  2013-08-07  8:17     ` Amos Kong
@ 2013-08-08  8:19       ` Stefan Hajnoczi
  0 siblings, 0 replies; 5+ messages in thread
From: Stefan Hajnoczi @ 2013-08-08  8:19 UTC (permalink / raw)
  To: Amos Kong; +Cc: vyasevic, pbonzini, aliguori, Laszlo Ersek, qemu-devel

On Wed, Aug 07, 2013 at 04:17:29PM +0800, Amos Kong wrote:
> BTW, can we add a check in quit_timers() to avoid one kind of crash
> (try to quit the uninited timers, or quit timer repeatedly) ?
> 
> 
> diff --git a/qemu-timer.c b/qemu-timer.c
> index b2d95e2..023e4ae 100644
> --- a/qemu-timer.c
> +++ b/qemu-timer.c
> @@ -728,8 +728,10 @@ static void win32_rearm_timer(struct
> qemu_alarm_timer *t,
>  static void quit_timers(void)
>  {
>      struct qemu_alarm_timer *t = alarm_timer;
> -    alarm_timer = NULL;
> -    t->stop(t);
> +    if (t) {
> +        alarm_timer = NULL;
> +        t->stop(t);
> +    }
>  }

This is unnecessary once your other patch has been applied since t will
be initialized before quit_timers() is installed.

If we do ever hit the problem it should be debugged because assumptions
about the timer lifecycle must be broken.  So avoiding the segfault
isn't really useful here, I think.

Stefan

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2013-08-08  8:20 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2013-08-06 23:29 [Qemu-devel] [PATCH] Moving alarm_timer assignment before atexit() Amos Kong
2013-08-07  6:39 ` Laszlo Ersek
2013-08-07  7:57   ` Stefan Hajnoczi
2013-08-07  8:17     ` Amos Kong
2013-08-08  8:19       ` Stefan Hajnoczi

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.