Patchwork qxl: fix surface tracking & locking

login
register
mail settings
Submitter Alon Levy
Date July 6, 2011, 12:19 p.m.
Message ID <1309954766-9728-10-git-send-email-alevy@redhat.com>
Download mbox | patch
Permalink /patch/103472/
State New
Headers show

Comments

Alon Levy - July 6, 2011, 12:19 p.m.
From: Gerd Hoffmann <kraxel@redhat.com>

Surface tracking needs proper locking with some commands affecting
surfaces running in a thread, add it.  Also reset the surface counter
when zapping all surfaces.

[ alon: use track_lock instead of wlock for guest_surfaces ]

Signed-off-by: Gerd Hoffmann <kraxel@redhat.com>
---
 hw/qxl.c |   13 ++++++++++++-
 hw/qxl.h |    2 ++
 2 files changed, 14 insertions(+), 1 deletions(-)

Patch

diff --git a/hw/qxl.c b/hw/qxl.c
index 830ddae..241c27c 100644
--- a/hw/qxl.c
+++ b/hw/qxl.c
@@ -153,7 +153,12 @@  void qxl_spice_update_area(PCIQXLDevice *qxl, uint32_t surface_id,
 void qxl_spice_destroy_surface_wait(PCIQXLDevice *qxl, uint32_t id)
 {
     qemu_mutex_lock(&qxl->ssd.wlock);
+    qemu_mutex_lock(&qxl->track_lock);
+    PANIC_ON(id >= NUM_SURFACES);
     qxl->ssd.worker->destroy_surface_wait(qxl->ssd.worker, id);
+    qxl->guest_surfaces.cmds[id] = 0;
+    qxl->guest_surfaces.count--;
+    qemu_mutex_unlock(&qxl->track_lock);
     qemu_mutex_unlock(&qxl->ssd.wlock);
 }
 
@@ -182,7 +187,11 @@  void qxl_spice_reset_memslots(PCIQXLDevice *qxl)
 void qxl_spice_destroy_surfaces(PCIQXLDevice *qxl)
 {
     qemu_mutex_lock(&qxl->ssd.wlock);
+    qemu_mutex_lock(&qxl->track_lock);
     qxl->ssd.worker->destroy_surfaces(qxl->ssd.worker);
+    memset(&qxl->guest_surfaces.cmds, 0, sizeof(qxl->guest_surfaces.cmds));
+    qxl->guest_surfaces.count = 0;
+    qemu_mutex_unlock(&qxl->track_lock);
     qemu_mutex_unlock(&qxl->ssd.wlock);
 }
 
@@ -346,6 +355,7 @@  static void qxl_track_command(PCIQXLDevice *qxl, struct QXLCommandExt *ext)
         QXLSurfaceCmd *cmd = qxl_phys2virt(qxl, ext->cmd.data, ext->group_id);
         uint32_t id = le32_to_cpu(cmd->surface_id);
         PANIC_ON(id >= NUM_SURFACES);
+        qemu_mutex_lock(&qxl->track_lock);
         if (cmd->type == QXL_SURFACE_CMD_CREATE) {
             qxl->guest_surfaces.cmds[id] = ext->cmd.data;
             qxl->guest_surfaces.count++;
@@ -356,6 +366,7 @@  static void qxl_track_command(PCIQXLDevice *qxl, struct QXLCommandExt *ext)
             qxl->guest_surfaces.cmds[id] = 0;
             qxl->guest_surfaces.count--;
         }
+        qemu_mutex_unlock(&qxl->track_lock);
         break;
     }
     case QXL_CMD_CURSOR:
@@ -897,7 +908,6 @@  static void qxl_reset_surfaces(PCIQXLDevice *d)
     dprint(d, 1, "%s:\n", __FUNCTION__);
     d->mode = QXL_MODE_UNDEFINED;
     qxl_spice_destroy_surfaces(d);
-    memset(&d->guest_surfaces.cmds, 0, sizeof(d->guest_surfaces.cmds));
 }
 
 /* called from spice server thread context only */
@@ -1321,6 +1331,7 @@  static int qxl_init_common(PCIQXLDevice *qxl)
     qxl->generation = 1;
     qxl->num_memslots = NUM_MEMSLOTS;
     qxl->num_surfaces = NUM_SURFACES;
+    qemu_mutex_init(&qxl->track_lock);
 
     switch (qxl->revision) {
     case 1: /* spice 0.4 -- qxl-1 */
diff --git a/hw/qxl.h b/hw/qxl.h
index 489d518..087ef6b 100644
--- a/hw/qxl.h
+++ b/hw/qxl.h
@@ -55,6 +55,8 @@  typedef struct PCIQXLDevice {
     } guest_surfaces;
     QXLPHYSICAL        guest_cursor;
 
+    QemuMutex          track_lock;
+
     /* thread signaling */
     pthread_t          main;
     int                pipe[2];