]> git.kernelconcepts.de Git - karo-tx-linux.git/commitdiff
drm/i915: Close race between processing unpin task and queueing the flip
authorChris Wilson <chris@chris-wilson.co.uk>
Mon, 3 Dec 2012 11:36:30 +0000 (11:36 +0000)
committerBen Hutchings <ben@decadent.org.uk>
Thu, 3 Jan 2013 03:33:29 +0000 (03:33 +0000)
commit e7d841ca03b7ab668620045cd7b428eda9f41601 upstream.

Before queuing the flip but crucially after attaching the unpin-work to
the crtc, we continue to setup the unpin-work. However, should the
hardware fire early, we see the connected unpin-work and queue the task.
The task then promptly runs and unpins the fb before we finish taking
the required references or even pinning it... Havoc.

To close the race, we use the flip-pending atomic to indicate when the
flip is finally setup and enqueued. So during the flip-done processing,
we can check more accurately whether the flip was expected.

v2: Add the appropriate mb() to ensure that the writes to the page-flip
worker are complete prior to marking it active and emitting the MI_FLIP.
On the read side, the mb should be enforced by the spinlocks.

Signed-off-by: Chris Wilson <chris@chris-wilson.co.uk>
[danvet: Review the barriers a bit, we need a write barrier both
before and after updating ->pending. Similarly we need a read barrier
in the interrupt handler both before and after reading ->pending. With
well-ordered irqs only one barrier in each place should be required,
but since this patch explicitly sets out to combat spurious interrupts
with is staged activation of the unpin work we need to go full-bore on
the barriers, too. Discussed with Chris Wilson on irc and changes
acked by him.]
Signed-off-by: Daniel Vetter <daniel.vetter@ffwll.ch>
[bwh: Backported to 3.2: adjust context]
Signed-off-by: Ben Hutchings <ben@decadent.org.uk>
drivers/gpu/drm/i915/i915_debugfs.c
drivers/gpu/drm/i915/i915_irq.c
drivers/gpu/drm/i915/intel_display.c
drivers/gpu/drm/i915/intel_drv.h

index b2e3c973e61ab6574c05a912e35fccdc4880e23e..d00f9054ec2b939261a32670493efd04a7eb5c21 100644 (file)
@@ -339,7 +339,7 @@ static int i915_gem_pageflip_info(struct seq_file *m, void *data)
                        seq_printf(m, "No flip due on pipe %c (plane %c)\n",
                                   pipe, plane);
                } else {
-                       if (!work->pending) {
+                       if (atomic_read(&work->pending) < INTEL_FLIP_COMPLETE) {
                                seq_printf(m, "Flip queued on pipe %c (plane %c)\n",
                                           pipe, plane);
                        } else {
@@ -350,7 +350,7 @@ static int i915_gem_pageflip_info(struct seq_file *m, void *data)
                                seq_printf(m, "Stall check enabled, ");
                        else
                                seq_printf(m, "Stall check waiting for page flip ioctl, ");
-                       seq_printf(m, "%d prepares\n", work->pending);
+                       seq_printf(m, "%d prepares\n", atomic_read(&work->pending));
 
                        if (work->old_fb_obj) {
                                struct drm_i915_gem_object *obj = work->old_fb_obj;
index 2812d7bc514067a2066df10d3d73c3eaed5f13c0..93e74fbd3d3359155e733cae5f7265f1b68f81c1 100644 (file)
@@ -1187,7 +1187,9 @@ static void i915_pageflip_stall_check(struct drm_device *dev, int pipe)
        spin_lock_irqsave(&dev->event_lock, flags);
        work = intel_crtc->unpin_work;
 
-       if (work == NULL || work->pending || !work->enable_stall_check) {
+       if (work == NULL ||
+           atomic_read(&work->pending) >= INTEL_FLIP_COMPLETE ||
+           !work->enable_stall_check) {
                /* Either the pending flip IRQ arrived, or we're too early. Don't check */
                spin_unlock_irqrestore(&dev->event_lock, flags);
                return;
index c9676c2a5a13dfde16aa1add89d36c42fd52449e..c75aa4348a9c61b80315c573a24b3176350d9296 100644 (file)
@@ -6949,11 +6949,18 @@ static void do_intel_finish_page_flip(struct drm_device *dev,
 
        spin_lock_irqsave(&dev->event_lock, flags);
        work = intel_crtc->unpin_work;
-       if (work == NULL || !work->pending) {
+
+       /* Ensure we don't miss a work->pending update ... */
+       smp_rmb();
+
+       if (work == NULL || atomic_read(&work->pending) < INTEL_FLIP_COMPLETE) {
                spin_unlock_irqrestore(&dev->event_lock, flags);
                return;
        }
 
+       /* and that the unpin work is consistent wrt ->pending. */
+       smp_rmb();
+
        intel_crtc->unpin_work = NULL;
 
        if (work->event) {
@@ -7025,16 +7032,25 @@ void intel_prepare_page_flip(struct drm_device *dev, int plane)
                to_intel_crtc(dev_priv->plane_to_crtc_mapping[plane]);
        unsigned long flags;
 
+       /* NB: An MMIO update of the plane base pointer will also
+        * generate a page-flip completion irq, i.e. every modeset
+        * is also accompanied by a spurious intel_prepare_page_flip().
+        */
        spin_lock_irqsave(&dev->event_lock, flags);
-       if (intel_crtc->unpin_work) {
-               if ((++intel_crtc->unpin_work->pending) > 1)
-                       DRM_ERROR("Prepared flip multiple times\n");
-       } else {
-               DRM_DEBUG_DRIVER("preparing flip with no unpin work?\n");
-       }
+       if (intel_crtc->unpin_work)
+               atomic_inc_not_zero(&intel_crtc->unpin_work->pending);
        spin_unlock_irqrestore(&dev->event_lock, flags);
 }
 
+inline static void intel_mark_page_flip_active(struct intel_crtc *intel_crtc)
+{
+       /* Ensure that the work item is consistent when activating it ... */
+       smp_wmb();
+       atomic_set(&intel_crtc->unpin_work->pending, INTEL_FLIP_PENDING);
+       /* and that it is marked active as soon as the irq could fire. */
+       smp_wmb();
+}
+
 static int intel_gen2_queue_flip(struct drm_device *dev,
                                 struct drm_crtc *crtc,
                                 struct drm_framebuffer *fb,
@@ -7071,6 +7087,8 @@ static int intel_gen2_queue_flip(struct drm_device *dev,
        OUT_RING(fb->pitch);
        OUT_RING(obj->gtt_offset + offset);
        OUT_RING(MI_NOOP);
+
+       intel_mark_page_flip_active(intel_crtc);
        ADVANCE_LP_RING();
        return 0;
 
@@ -7114,6 +7132,7 @@ static int intel_gen3_queue_flip(struct drm_device *dev,
        OUT_RING(obj->gtt_offset + offset);
        OUT_RING(MI_NOOP);
 
+       intel_mark_page_flip_active(intel_crtc);
        ADVANCE_LP_RING();
        return 0;
 
@@ -7157,6 +7176,10 @@ static int intel_gen4_queue_flip(struct drm_device *dev,
        pf = 0;
        pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
        OUT_RING(pf | pipesrc);
+
+       intel_mark_page_flip_active(intel_crtc);
+
+       intel_mark_page_flip_active(intel_crtc);
        ADVANCE_LP_RING();
        return 0;
 
@@ -7246,6 +7269,8 @@ static int intel_gen7_queue_flip(struct drm_device *dev,
        intel_ring_emit(ring, (fb->pitch | obj->tiling_mode));
        intel_ring_emit(ring, (obj->gtt_offset));
        intel_ring_emit(ring, (MI_NOOP));
+
+       intel_mark_page_flip_active(intel_crtc);
        intel_ring_advance(ring);
        return 0;
 
index bcadf74b418ddabd59ae786aaa598c32b06aaef9..5212284f18d6d3bb411e4b063072e9a1f5beb1e4 100644 (file)
@@ -257,7 +257,10 @@ struct intel_unpin_work {
        struct drm_i915_gem_object *old_fb_obj;
        struct drm_i915_gem_object *pending_flip_obj;
        struct drm_pending_vblank_event *event;
-       int pending;
+       atomic_t pending;
+#define INTEL_FLIP_INACTIVE    0
+#define INTEL_FLIP_PENDING     1
+#define INTEL_FLIP_COMPLETE    2
        bool enable_stall_check;
 };