qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: Paolo Bonzini <pbonzini@redhat.com>
To: Corentin Chary <corentin.chary@gmail.com>
Cc: Anthony Liguori <aliguori@linux.vnet.ibm.com>,
	Adam Litke <agl@us.ibm.com>, Gautham R Shenoy <ego@in.ibm.com>,
	qemu-devel@nongnu.org, Alexander Graf <agraf@suse.de>
Subject: Re: [Qemu-devel] Re: [PATCH 3/3] vnc: threaded VNC server
Date: Thu, 3 Jun 2010 06:46:27 -0400 (EDT)	[thread overview]
Message-ID: <1229492684.2331001275561987330.JavaMail.root@zmail07.collab.prod.int.phx2.redhat.com> (raw)
In-Reply-To: <1152486922.2330781275561618453.JavaMail.root@zmail07.collab.prod.int.phx2.redhat.com>

> >> +void vnc_job_push(VncJob *job)
> >> +{
> >> +    vnc_lock_queue(queue);
> >> +    if (QLIST_EMPTY(&job->rectangles)) {
> >> +        qemu_free(job);
> >
> > No need to lock if you get into the "then" block.
> 
> I locked it because the main thread can try to push a job while a
> consumer is removing one, so I can't call QLIST_EMPTY() without
> locking the queue.

You're obviously right.

>>> +    qemu_mutex_unlock(&job->vs->output_mutex);
>>> +
>>> +    if (job->vs->csock != -1 && job->vs->abording != true) {
>>> +        vnc_flush(job->vs);
>>> +    }
>>> +
>>
>> You're accessing the abort flag outside the mutex here.  Also, you are not
>> using vnc_{,un}lock_output.
>
> I assumed that bool (int) where atomic .. but you're right I should lock that.

They are, however: 1) if you access them outside a mutex you have to think about
whether you need memory barriers and whether there are races; 2) since you already
own the mutex and you're just keeping it a bit longer, it costs basically nothing.
BTW, with the same reasoning you could avoid taking the mutex altogether in
vnc_abort_display_jobs (but I think it's better to keep it).

Also, I took a look at the code again and I noticed this:

>>> +        if (job->vs->csock == -1) {
>>> +            goto disconnected;
>>> +        }

The "goto" is jumping over the unlocking of &job->vs->mutex.  You only want
a "break;" I think.

>>  static void vnc_disconnect_finish(VncState *vs)
>>  {
>> +    vnc_jobs_join(vs); /* Wait encoding jobs */
>> +    vnc_lock(vs);
>
> Possibly racy?  Maybe you have to set the aforementioned new flag
> queue->exit at the beginning of vnc_jobs_join, and refuse new jobs if it is
> set.
>
> vnc_disconnect_finish can only be called by the main thread, I don't
> see how this could be racy, any hint ?

I was thinking of someone queuing jobs between the end of vnc_jobs_join
and the time the vnc_lock is taken.  But indeed jobs cannot be queued
at this time because vnc_refresh can only be called from the same
thread.  So this is correct.

>> Also, if anything waits on the same vs in vnc_refresh while you own it in
>> vnc_disconnect_finish, as soon as you unlock they'll have a dangling
>> pointer.  (After you unlock the mutex the OS wakes the thread, but then
>> pthread_mutex_lock has to check again that no one got the lock in the
>> meanwhile; so QTAILQ_FOREACH_SAFE is not protecting you).  Probably it's
>> better to use a single lock on vd->clients instead of one lock per VncState.

Same here.  No race because everything happens in the main thread.

Paolo

       reply	other threads:[~2010-06-03 10:46 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <1152486922.2330781275561618453.JavaMail.root@zmail07.collab.prod.int.phx2.redhat.com>
2010-06-03 10:46 ` Paolo Bonzini [this message]
2010-05-29  7:38 [Qemu-devel] [PATCH 0/3] [RFC] Threaded vnc server Corentin Chary
2010-05-29  7:38 ` [Qemu-devel] [PATCH 3/3] vnc: threaded VNC server Corentin Chary
2010-06-03  7:55   ` [Qemu-devel] " Paolo Bonzini
2010-06-03  8:26     ` Corentin Chary

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=1229492684.2331001275561987330.JavaMail.root@zmail07.collab.prod.int.phx2.redhat.com \
    --to=pbonzini@redhat.com \
    --cc=agl@us.ibm.com \
    --cc=agraf@suse.de \
    --cc=aliguori@linux.vnet.ibm.com \
    --cc=corentin.chary@gmail.com \
    --cc=ego@in.ibm.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).