qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: Peter Xu <peterx@redhat.com>
To: "Dr. David Alan Gilbert" <dgilbert@redhat.com>
Cc: qemu-devel@nongnu.org, "Eric Blake" <eblake@redhat.com>,
	"Marc-André Lureau" <marcandre.lureau@redhat.com>,
	"Markus Armbruster" <armbru@redhat.com>,
	"Stefan Hajnoczi" <stefanha@redhat.com>
Subject: Re: [Qemu-devel] [PATCH v4 2/4] monitor: protect mon->fds with mon_lock
Date: Wed, 2 May 2018 19:28:10 +0800	[thread overview]
Message-ID: <20180502112810.GA2321@xz-mi> (raw)
In-Reply-To: <20180502111506.GC2679@work-vm>

On Wed, May 02, 2018 at 12:15:07PM +0100, Dr. David Alan Gilbert wrote:
> * Peter Xu (peterx@redhat.com) wrote:
> > mon->fds were protected by BQL.  Now protect it by mon_lock so that it
> > can even be used in monitor iothread.
> > 
> > Signed-off-by: Peter Xu <peterx@redhat.com>
> > ---
> >  monitor.c | 11 ++++++++++-
> >  1 file changed, 10 insertions(+), 1 deletion(-)
> > 
> > diff --git a/monitor.c b/monitor.c
> > index 48882d28ae..9f361ec21e 100644
> > --- a/monitor.c
> > +++ b/monitor.c
> > @@ -213,7 +213,6 @@ struct Monitor {
> >      BlockCompletionFunc *password_completion_cb;
> >      void *password_opaque;
> >      mon_cmd_t *cmd_table;
> > -    QLIST_HEAD(,mon_fd_t) fds;
> >      QTAILQ_ENTRY(Monitor) entry;
> >  
> >      /*
> > @@ -225,6 +224,7 @@ struct Monitor {
> >      /*
> >       * Fields that are protected by the per-monitor lock.
> >       */
> > +    QLIST_HEAD(, mon_fd_t) fds;
> >      QString *outbuf;
> >      guint out_watch;
> >      /* Read under either BQL or mon_lock, written with BQL+mon_lock.  */
> > @@ -2207,6 +2207,7 @@ void qmp_getfd(const char *fdname, Error **errp)
> >          return;
> >      }
> >  
> > +    qemu_mutex_lock(&cur_mon->mon_lock);
> >      QLIST_FOREACH(monfd, &cur_mon->fds, next) {
> >          if (strcmp(monfd->name, fdname) != 0) {
> >              continue;
> > @@ -2214,6 +2215,7 @@ void qmp_getfd(const char *fdname, Error **errp)
> >  
> >          close(monfd->fd);
> >          monfd->fd = fd;
> > +        qemu_mutex_unlock(&cur_mon->mon_lock);
> 
> Why is it safe to have a close() in a mon_lock'd region?
> We've got to make sure everything that happens in it is non-blocking.

Hmm indeed.  Let me move that close() out of the critical section. I
think I need to touch up below [1] too since it'll has similar
problem.

I'll wait for some more comments to repost.  Thanks,

> 
> Dave
> 
> >          return;
> >      }
> >  
> > @@ -2222,12 +2224,14 @@ void qmp_getfd(const char *fdname, Error **errp)
> >      monfd->fd = fd;
> >  
> >      QLIST_INSERT_HEAD(&cur_mon->fds, monfd, next);
> > +    qemu_mutex_unlock(&cur_mon->mon_lock);
> >  }
> >  
> >  void qmp_closefd(const char *fdname, Error **errp)
> >  {
> >      mon_fd_t *monfd;
> >  
> > +    qemu_mutex_lock(&cur_mon->mon_lock);
> >      QLIST_FOREACH(monfd, &cur_mon->fds, next) {
> >          if (strcmp(monfd->name, fdname) != 0) {
> >              continue;
> > @@ -2237,9 +2241,11 @@ void qmp_closefd(const char *fdname, Error **errp)
> >          close(monfd->fd);
> >          g_free(monfd->name);
> >          g_free(monfd);
> > +        qemu_mutex_unlock(&cur_mon->mon_lock);

[1]

> >          return;
> >      }
> >  
> > +    qemu_mutex_unlock(&cur_mon->mon_lock);
> >      error_setg(errp, QERR_FD_NOT_FOUND, fdname);
> >  }
> >  
> > @@ -2247,6 +2253,7 @@ int monitor_get_fd(Monitor *mon, const char *fdname, Error **errp)
> >  {
> >      mon_fd_t *monfd;
> >  
> > +    qemu_mutex_lock(&mon->mon_lock);
> >      QLIST_FOREACH(monfd, &mon->fds, next) {
> >          int fd;
> >  
> > @@ -2260,10 +2267,12 @@ int monitor_get_fd(Monitor *mon, const char *fdname, Error **errp)
> >          QLIST_REMOVE(monfd, next);
> >          g_free(monfd->name);
> >          g_free(monfd);
> > +        qemu_mutex_unlock(&mon->mon_lock);
> >  
> >          return fd;
> >      }
> >  
> > +    qemu_mutex_unlock(&mon->mon_lock);
> >      error_setg(errp, "File descriptor named '%s' has not been found", fdname);
> >      return -1;
> >  }
> > -- 
> > 2.14.3
> > 
> --
> Dr. David Alan Gilbert / dgilbert@redhat.com / Manchester, UK

-- 
Peter Xu

  reply	other threads:[~2018-05-02 11:28 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-05-02 10:04 [Qemu-devel] [PATCH v4 0/4] monitor: let Monitor be thread safe Peter Xu
2018-05-02 10:04 ` [Qemu-devel] [PATCH v4 1/4] monitor: rename out_lock to mon_lock Peter Xu
2018-05-08 10:26   ` Stefan Hajnoczi
2018-05-02 10:04 ` [Qemu-devel] [PATCH v4 2/4] monitor: protect mon->fds with mon_lock Peter Xu
2018-05-02 11:15   ` Dr. David Alan Gilbert
2018-05-02 11:28     ` Peter Xu [this message]
2018-05-02 10:04 ` [Qemu-devel] [PATCH v4 3/4] monitor: more comments on lock-free fleids/funcs Peter Xu
2018-05-08 10:23   ` Stefan Hajnoczi
2018-05-08 10:32     ` Peter Xu
2018-05-08 14:09       ` Stefan Hajnoczi
2018-05-02 10:04 ` [Qemu-devel] [PATCH v4 4/4] monitor: add lock to protect mon_fdsets Peter Xu
2018-05-08 10:26   ` Stefan Hajnoczi

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=20180502112810.GA2321@xz-mi \
    --to=peterx@redhat.com \
    --cc=armbru@redhat.com \
    --cc=dgilbert@redhat.com \
    --cc=eblake@redhat.com \
    --cc=marcandre.lureau@redhat.com \
    --cc=qemu-devel@nongnu.org \
    --cc=stefanha@redhat.com \
    /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).