qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: Fam Zheng <famz@redhat.com>
To: Eric Blake <eblake@redhat.com>
Cc: qemu-devel@nongnu.org, pbonzini@redhat.com
Subject: Re: [Qemu-devel] [PATCH] qemu-thread: Assert locks are initialized before using
Date: Thu, 6 Jul 2017 20:36:27 +0800	[thread overview]
Message-ID: <20170706123627.GC13548@lemon.lan> (raw)
In-Reply-To: <93dea10b-83f9-db95-107b-9da8cff3baa9@redhat.com>

On Thu, 07/06 07:16, Eric Blake wrote:
> On 07/04/2017 07:23 AM, Fam Zheng wrote:
> > Not all platforms check whether a lock is initialized before used.  In
> > particular Linux seems to be more permissive than OSX.
> > 
> > Check initialization state explicitly in our code to catch such bugs
> > earlier.
> > 
> > Signed-off-by: Fam Zheng <famz@redhat.com>
> > ---
> >  include/qemu/thread-posix.h |  4 ++++
> >  include/qemu/thread-win32.h |  5 +++++
> >  util/qemu-thread-posix.c    | 27 +++++++++++++++++++++++++++
> >  util/qemu-thread-win32.c    | 34 +++++++++++++++++++++++++++++++++-
> >  4 files changed, 69 insertions(+), 1 deletion(-)
> > 
> > diff --git a/include/qemu/thread-posix.h b/include/qemu/thread-posix.h
> > index 09d1e15..e5e3a0f 100644
> > --- a/include/qemu/thread-posix.h
> > +++ b/include/qemu/thread-posix.h
> > @@ -12,10 +12,12 @@ typedef QemuMutex QemuRecMutex;
> >  
> >  struct QemuMutex {
> >      pthread_mutex_t lock;
> > +    bool initialized;
> >  };
> 
> Are we worried about an object living on the stack and inheriting bit
> values that make the object already appear initialized?  Would a magic
> number a little less likely than '1' reduce the risk of inherited stack
> garbage throwing us off?
> 
> Then again, several years ago, the Cygwin project quit using a magic
> number cookie to track if synchronization objects were initialized, as
> it ran into issues where repeated calls to a function that allocates an
> object would cause the second allocation to fail because it saw leftover
> stack contents from the first time through, so even with it's use of
> something a little less likely than a bool '1', it still became a problem.

I don't know the answer to your question about magic number problem, but more
often than not a lock is heap allocated, so I'm not worried.

> 
> 
> > @@ -58,6 +61,7 @@ void qemu_mutex_lock(QemuMutex *mutex)
> >  {
> >      int err;
> >  
> > +    assert(mutex->initialized);
> >      err = pthread_mutex_lock(&mutex->lock);
> >      if (err)
> >          error_exit(err, __func__);
> 
> Are we sure this isn't going to penalize our code speed, by adding a
> conditional on every lock/unlock?

We already have assertions everywhere, and I've never worried about its
computation cost.  Hot paths should try not to contend on locks anyway.

(This patch is already in master, if there is a problem, it will need a follow
up patch.)

Fam

  reply	other threads:[~2017-07-06 12:36 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-07-04 12:23 [Qemu-devel] [PATCH] qemu-thread: Assert locks are initialized before using Fam Zheng
2017-07-04 13:17 ` Philippe Mathieu-Daudé
2017-07-06 10:08 ` Stefan Hajnoczi
2017-07-06 12:16 ` Eric Blake
2017-07-06 12:36   ` Fam Zheng [this message]
2017-07-06 12:37   ` Paolo Bonzini

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=20170706123627.GC13548@lemon.lan \
    --to=famz@redhat.com \
    --cc=eblake@redhat.com \
    --cc=pbonzini@redhat.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).