Received: from [140.186.70.92] (port=59761 helo=eggs.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1OK7we-0005b3-Ln for qemu-devel@nongnu.org; Thu, 03 Jun 2010 06:46:45 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.69) (envelope-from ) id 1OK7wa-0003Fj-1Z for qemu-devel@nongnu.org; Thu, 03 Jun 2010 06:46:40 -0400 Received: from mx3-phx2.redhat.com ([209.132.183.24]:49602 helo=mx01.colomx.prod.int.phx2.redhat.com) by eggs.gnu.org with esmtp (Exim 4.69) (envelope-from ) id 1OK7wZ-0003FP-R9 for qemu-devel@nongnu.org; Thu, 03 Jun 2010 06:46:36 -0400 Date: Thu, 3 Jun 2010 06:46:27 -0400 (EDT) From: Paolo Bonzini Message-ID: <1229492684.2331001275561987330.JavaMail.root@zmail07.collab.prod.int.phx2.redhat.com> In-Reply-To: <1152486922.2330781275561618453.JavaMail.root@zmail07.collab.prod.int.phx2.redhat.com> Subject: Re: [Qemu-devel] Re: [PATCH 3/3] vnc: threaded VNC server MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable List-Id: qemu-devel.nongnu.org List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: Corentin Chary Cc: Anthony Liguori , Adam Litke , Gautham R Shenoy , qemu-devel@nongnu.org, Alexander Graf > >> +void vnc_job_push(VncJob *job) > >> +{ > >> + =C2=A0 =C2=A0vnc_lock_queue(queue); > >> + =C2=A0 =C2=A0if (QLIST_EMPTY(&job->rectangles)) { > >> + =C2=A0 =C2=A0 =C2=A0 =C2=A0qemu_free(job); > > > > No need to lock if you get into the "then" block. >=20 > 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. >>> + =C2=A0 =C2=A0qemu_mutex_unlock(&job->vs->output_mutex); >>> + >>> + =C2=A0 =C2=A0if (job->vs->csock !=3D -1 && job->vs->abording !=3D tru= e) { >>> + =C2=A0 =C2=A0 =C2=A0 =C2=A0vnc_flush(job->vs); >>> + =C2=A0 =C2=A0} >>> + >> >> You're accessing the abort flag outside the mutex here. =C2=A0Also, 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 n= othing. 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 =3D=3D -1) { >>> + goto disconnected; >>> + } The "goto" is jumping over the unlocking of &job->vs->mutex. You only want a "break;" I think. >> =C2=A0static void vnc_disconnect_finish(VncState *vs) >> =C2=A0{ >> + =C2=A0 =C2=A0vnc_jobs_join(vs); /* Wait encoding jobs */ >> + =C2=A0 =C2=A0vnc_lock(vs); > > Possibly racy? =C2=A0Maybe 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 i= n >> vnc_disconnect_finish, as soon as you unlock they'll have a dangling >> pointer. =C2=A0(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). =C2=A0Probably= it's >> better to use a single lock on vd->clients instead of one lock per VncSt= ate. Same here. No race because everything happens in the main thread. Paolo