Noticed while reviewing code. I'm not sure whether this might or might not explain some of the missed vblank hilarity we've been seeing. I think those all go through the vblank completion event, which has unconditional barriers - it always takes the spinlock. Therefore no cc stable.
v2: - Barrriers are hard, put them in in the right order (Chris). - Improve the comments a bit.
Cc: Rodrigo Siqueira rodrigosiqueiramelo@gmail.com Cc: Chris Wilson chris@chris-wilson.co.uk Signed-off-by: Daniel Vetter daniel.vetter@intel.com --- drivers/gpu/drm/drm_vblank.c | 38 +++++++++++++++++++++++++++++++++++- include/drm/drm_vblank.h | 13 +++++++++++- 2 files changed, 49 insertions(+), 2 deletions(-)
diff --git a/drivers/gpu/drm/drm_vblank.c b/drivers/gpu/drm/drm_vblank.c index 603ab105125d..eb2a8304536c 100644 --- a/drivers/gpu/drm/drm_vblank.c +++ b/drivers/gpu/drm/drm_vblank.c @@ -295,11 +295,23 @@ static void drm_update_vblank_count(struct drm_device *dev, unsigned int pipe, static u64 drm_vblank_count(struct drm_device *dev, unsigned int pipe) { struct drm_vblank_crtc *vblank = &dev->vblank[pipe]; + u64 count;
if (WARN_ON(pipe >= dev->num_crtcs)) return 0;
- return vblank->count; + count = vblank->count; + + /* + * This read barrier corresponds to the implicit write barrier of the + * write seqlock in store_vblank(). Note that this is the only place + * where we need an explicit barrier, since all other access goes + * through drm_vblank_count_and_time(), which already has the required + * read barrier curtesy of the read seqlock. + */ + smp_rmb(); + + return count; }
/** @@ -764,6 +776,14 @@ drm_get_last_vbltimestamp(struct drm_device *dev, unsigned int pipe, * vblank interrupt (since it only reports the software vblank counter), see * drm_crtc_accurate_vblank_count() for such use-cases. * + * Note that for a given vblank counter value drm_crtc_handle_vblank() + * and drm_crtc_vblank_count() or drm_crtc_vblank_count_and_time() + * provide a barrier: Any writes done before calling + * drm_crtc_handle_vblank() will be visible to callers of the later + * functions, iff the vblank count is the same or a later one. + * + * See also &drm_vblank_crtc.count. + * * Returns: * The software vblank counter. */ @@ -818,6 +838,14 @@ static u64 drm_vblank_count_and_time(struct drm_device *dev, unsigned int pipe, * vblank events since the system was booted, including lost events due to * modesetting activity. Returns corresponding system timestamp of the time * of the vblank interval that corresponds to the current vblank counter value. + * + * Note that for a given vblank counter value drm_crtc_handle_vblank() + * and drm_crtc_vblank_count() or drm_crtc_vblank_count_and_time() + * provide a barrier: Any writes done before calling + * drm_crtc_handle_vblank() will be visible to callers of the later + * functions, iff the vblank count is the same or a later one. + * + * See also &drm_vblank_crtc.count. */ u64 drm_crtc_vblank_count_and_time(struct drm_crtc *crtc, ktime_t *vblanktime) @@ -1791,6 +1819,14 @@ EXPORT_SYMBOL(drm_handle_vblank); * * This is the native KMS version of drm_handle_vblank(). * + * Note that for a given vblank counter value drm_crtc_handle_vblank() + * and drm_crtc_vblank_count() or drm_crtc_vblank_count_and_time() + * provide a barrier: Any writes done before calling + * drm_crtc_handle_vblank() will be visible to callers of the later + * functions, iff the vblank count is the same or a later one. + * + * See also &drm_vblank_crtc.count. + * * Returns: * True if the event was successfully handled, false on failure. */ diff --git a/include/drm/drm_vblank.h b/include/drm/drm_vblank.h index e528bb2f659d..5ec623740158 100644 --- a/include/drm/drm_vblank.h +++ b/include/drm/drm_vblank.h @@ -110,7 +110,18 @@ struct drm_vblank_crtc { seqlock_t seqlock;
/** - * @count: Current software vblank counter. + * @count: + * + * Current software vblank counter. + * + * Note that for a given vblank counter value drm_crtc_handle_vblank() + * and drm_crtc_vblank_count() or drm_crtc_vblank_count_and_time() + * provide a barrier: Any writes done before calling + * drm_crtc_handle_vblank() will be visible to callers of the later + * functions, iff the vblank count is the same or a later one. + * + * IMPORTANT: This guarantee requires barriers, therefor never access + * this field directly. Use drm_crtc_vblank_count() instead. */ u64 count; /**
It's the recommended version, wait_for_vblanks is a bit a hacky interim thing that predates all the flip_done tracking. It's unfortunately still the default ...
Signed-off-by: Daniel Vetter daniel.vetter@intel.com Cc: Rodrigo Siqueira rodrigosiqueiramelo@gmail.com Cc: Haneen Mohammed hamohammed.sa@gmail.com Cc: Daniel Vetter daniel@ffwll.ch --- drivers/gpu/drm/vkms/vkms_drv.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/drivers/gpu/drm/vkms/vkms_drv.c b/drivers/gpu/drm/vkms/vkms_drv.c index 44ab9f8ef8be..80524a22412a 100644 --- a/drivers/gpu/drm/vkms/vkms_drv.c +++ b/drivers/gpu/drm/vkms/vkms_drv.c @@ -83,7 +83,7 @@ static void vkms_atomic_commit_tail(struct drm_atomic_state *old_state)
drm_atomic_helper_commit_hw_done(old_state);
- drm_atomic_helper_wait_for_vblanks(dev, old_state); + drm_atomic_helper_wait_for_flip_done(dev, old_state);
for_each_old_crtc_in_state(old_state, crtc, old_crtc_state, i) { struct vkms_crtc_state *vkms_state =
On 07/19, Daniel Vetter wrote:
It's the recommended version, wait_for_vblanks is a bit a hacky interim thing that predates all the flip_done tracking. It's unfortunately still the default ...
Just one question, is it safe to replace drm_atomic_helper_wait_for_vblanks by drm_atomic_helper_wait_for_flip_done? I noticed that only six drivers use these functions; they are:
* atmel-hlcdc * mediatek * msm * tegra * tilcdc * virtio
If we change these drivers, can we drop the helper drm_atomic_helper_wait_for_vblanks?
Reviewed-by: Rodrigo Siqueira rodrigosiqueiramelo@gmail.com
Thanks
Signed-off-by: Daniel Vetter daniel.vetter@intel.com Cc: Rodrigo Siqueira rodrigosiqueiramelo@gmail.com Cc: Haneen Mohammed hamohammed.sa@gmail.com Cc: Daniel Vetter daniel@ffwll.ch
drivers/gpu/drm/vkms/vkms_drv.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/drivers/gpu/drm/vkms/vkms_drv.c b/drivers/gpu/drm/vkms/vkms_drv.c index 44ab9f8ef8be..80524a22412a 100644 --- a/drivers/gpu/drm/vkms/vkms_drv.c +++ b/drivers/gpu/drm/vkms/vkms_drv.c @@ -83,7 +83,7 @@ static void vkms_atomic_commit_tail(struct drm_atomic_state *old_state)
drm_atomic_helper_commit_hw_done(old_state);
- drm_atomic_helper_wait_for_vblanks(dev, old_state);
drm_atomic_helper_wait_for_flip_done(dev, old_state);
for_each_old_crtc_in_state(old_state, crtc, old_crtc_state, i) { struct vkms_crtc_state *vkms_state =
-- 2.22.0
On Tue, Sep 03, 2019 at 08:49:06AM -0400, Rodrigo Siqueira wrote:
On 07/19, Daniel Vetter wrote:
It's the recommended version, wait_for_vblanks is a bit a hacky interim thing that predates all the flip_done tracking. It's unfortunately still the default ...
Just one question, is it safe to replace drm_atomic_helper_wait_for_vblanks by drm_atomic_helper_wait_for_flip_done? I noticed that only six drivers use these functions; they are:
- atmel-hlcdc
- mediatek
- msm
- tegra
- tilcdc
- virtio
If we change these drivers, can we drop the helper drm_atomic_helper_wait_for_vblanks?
Yes, but there might be a tiny behaviour change, that's why I haven't just made it the default.
Also note that wait_for_vblanks is still the default in the atomic_commit_tail (seee drm_atomic_helper_commit_tail), so there's a pile more drivers using this implicitly.
But yeah would be really great to fix that all up, since I think wait_for_flip_done is the better function. Maybe a todo.rst? Or perhaps we should at least do it for atomic helpers, and just see what breaks? As a start for this conversion.
Reviewed-by: Rodrigo Siqueira rodrigosiqueiramelo@gmail.com
Thanks, Daniel
Thanks
Signed-off-by: Daniel Vetter daniel.vetter@intel.com Cc: Rodrigo Siqueira rodrigosiqueiramelo@gmail.com Cc: Haneen Mohammed hamohammed.sa@gmail.com Cc: Daniel Vetter daniel@ffwll.ch
drivers/gpu/drm/vkms/vkms_drv.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/drivers/gpu/drm/vkms/vkms_drv.c b/drivers/gpu/drm/vkms/vkms_drv.c index 44ab9f8ef8be..80524a22412a 100644 --- a/drivers/gpu/drm/vkms/vkms_drv.c +++ b/drivers/gpu/drm/vkms/vkms_drv.c @@ -83,7 +83,7 @@ static void vkms_atomic_commit_tail(struct drm_atomic_state *old_state)
drm_atomic_helper_commit_hw_done(old_state);
- drm_atomic_helper_wait_for_vblanks(dev, old_state);
drm_atomic_helper_wait_for_flip_done(dev, old_state);
for_each_old_crtc_in_state(old_state, crtc, old_crtc_state, i) { struct vkms_crtc_state *vkms_state =
-- 2.22.0
-- Rodrigo Siqueira Software Engineer, Advanced Micro Devices (AMD) https://siqueira.tech
We can reduce the critical section in vkms_vblank_simulate under output->lock quite a lot:
- hrtimer_forward_now just needs to be ordered correctly wrt drm_crtc_handle_vblank. We already access the hrtimer timestamp without locks. While auditing that I noticed that we don't correctly annotate the read there, so sprinkle a READ_ONCE to make sure the compiler doesn't do anything foolish.
- drm_crtc_handle_vblank must stay under the lock to avoid races with drm_crtc_arm_vblank_event.
- The access to vkms_ouptut->crc_state also must stay under the lock.
- next problem is making sure the output->state structure doesn't get freed too early. First we rely on a given hrtimer being serialized: If we call drm_crtc_handle_vblank, then we are guaranteed that the previous call to vkms_vblank_simulate has completed. The other side of the coin is that the atomic updates waits for the vblank to happen before it releases the old state. Both taken together means that by the time the atomic update releases the old state, the hrtimer won't access it anymore (it might be accessing the new state at the same time, but that's ok).
- state is invariant, except the few fields separate protected by state->crc_lock. So no need to hold the lock for that.
- finally the queue_work. We need to make sure there's no races with the flush_work, i.e. when we call flush_work we need to guarantee that the hrtimer can't requeue the work again. This is guaranteed by the same vblank/hrtimer ordering guarantees like the reasoning above why state won't be freed too early: flush_work on the old state is called after wait_for_flip_done in the atomic commit code.
Therefore we can also move everything after the output->crc_state out of the critical section.
Motivated by suggestions from Rodrigo.
Signed-off-by: Daniel Vetter daniel.vetter@intel.com Cc: Rodrigo Siqueira rodrigosiqueiramelo@gmail.com Cc: Haneen Mohammed hamohammed.sa@gmail.com Cc: Daniel Vetter daniel@ffwll.ch --- drivers/gpu/drm/vkms/vkms_crtc.c | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-)
diff --git a/drivers/gpu/drm/vkms/vkms_crtc.c b/drivers/gpu/drm/vkms/vkms_crtc.c index 927dafaebc76..74f703b8d22a 100644 --- a/drivers/gpu/drm/vkms/vkms_crtc.c +++ b/drivers/gpu/drm/vkms/vkms_crtc.c @@ -16,17 +16,18 @@ static enum hrtimer_restart vkms_vblank_simulate(struct hrtimer *timer) u64 ret_overrun; bool ret;
- spin_lock(&output->lock); - ret_overrun = hrtimer_forward_now(&output->vblank_hrtimer, output->period_ns); WARN_ON(ret_overrun != 1);
+ spin_lock(&output->lock); ret = drm_crtc_handle_vblank(crtc); if (!ret) DRM_ERROR("vkms failure on handling vblank");
state = output->composer_state; + spin_unlock(&output->lock); + if (state && output->composer_enabled) { u64 frame = drm_crtc_accurate_vblank_count(crtc);
@@ -48,8 +49,6 @@ static enum hrtimer_restart vkms_vblank_simulate(struct hrtimer *timer) DRM_DEBUG_DRIVER("Composer worker already queued\n"); }
- spin_unlock(&output->lock); - return HRTIMER_RESTART; }
@@ -85,7 +84,7 @@ bool vkms_get_vblank_timestamp(struct drm_device *dev, unsigned int pipe, struct vkms_output *output = &vkmsdev->output; struct drm_vblank_crtc *vblank = &dev->vblank[pipe];
- *vblank_time = output->vblank_hrtimer.node.expires; + *vblank_time = READ_ONCE(output->vblank_hrtimer.node.expires);
if (WARN_ON(*vblank_time == vblank->time)) return true;
Thanks for this patch! It looks good for me.
Reviewed-by: Rodrigo Siqueira rodrigosiqueiramelo@gmail.com
On 07/19, Daniel Vetter wrote:
We can reduce the critical section in vkms_vblank_simulate under output->lock quite a lot:
hrtimer_forward_now just needs to be ordered correctly wrt drm_crtc_handle_vblank. We already access the hrtimer timestamp without locks. While auditing that I noticed that we don't correctly annotate the read there, so sprinkle a READ_ONCE to make sure the compiler doesn't do anything foolish.
drm_crtc_handle_vblank must stay under the lock to avoid races with drm_crtc_arm_vblank_event.
The access to vkms_ouptut->crc_state also must stay under the lock.
next problem is making sure the output->state structure doesn't get freed too early. First we rely on a given hrtimer being serialized: If we call drm_crtc_handle_vblank, then we are guaranteed that the previous call to vkms_vblank_simulate has completed. The other side of the coin is that the atomic updates waits for the vblank to happen before it releases the old state. Both taken together means that by the time the atomic update releases the old state, the hrtimer won't access it anymore (it might be accessing the new state at the same time, but that's ok).
state is invariant, except the few fields separate protected by state->crc_lock. So no need to hold the lock for that.
finally the queue_work. We need to make sure there's no races with the flush_work, i.e. when we call flush_work we need to guarantee that the hrtimer can't requeue the work again. This is guaranteed by the same vblank/hrtimer ordering guarantees like the reasoning above why state won't be freed too early: flush_work on the old state is called after wait_for_flip_done in the atomic commit code.
Therefore we can also move everything after the output->crc_state out of the critical section.
Motivated by suggestions from Rodrigo.
Signed-off-by: Daniel Vetter daniel.vetter@intel.com Cc: Rodrigo Siqueira rodrigosiqueiramelo@gmail.com Cc: Haneen Mohammed hamohammed.sa@gmail.com Cc: Daniel Vetter daniel@ffwll.ch
drivers/gpu/drm/vkms/vkms_crtc.c | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-)
diff --git a/drivers/gpu/drm/vkms/vkms_crtc.c b/drivers/gpu/drm/vkms/vkms_crtc.c index 927dafaebc76..74f703b8d22a 100644 --- a/drivers/gpu/drm/vkms/vkms_crtc.c +++ b/drivers/gpu/drm/vkms/vkms_crtc.c @@ -16,17 +16,18 @@ static enum hrtimer_restart vkms_vblank_simulate(struct hrtimer *timer) u64 ret_overrun; bool ret;
- spin_lock(&output->lock);
- ret_overrun = hrtimer_forward_now(&output->vblank_hrtimer, output->period_ns); WARN_ON(ret_overrun != 1);
spin_lock(&output->lock); ret = drm_crtc_handle_vblank(crtc); if (!ret) DRM_ERROR("vkms failure on handling vblank");
state = output->composer_state;
spin_unlock(&output->lock);
if (state && output->composer_enabled) { u64 frame = drm_crtc_accurate_vblank_count(crtc);
@@ -48,8 +49,6 @@ static enum hrtimer_restart vkms_vblank_simulate(struct hrtimer *timer) DRM_DEBUG_DRIVER("Composer worker already queued\n"); }
- spin_unlock(&output->lock);
- return HRTIMER_RESTART;
}
@@ -85,7 +84,7 @@ bool vkms_get_vblank_timestamp(struct drm_device *dev, unsigned int pipe, struct vkms_output *output = &vkmsdev->output; struct drm_vblank_crtc *vblank = &dev->vblank[pipe];
- *vblank_time = output->vblank_hrtimer.node.expires;
*vblank_time = READ_ONCE(output->vblank_hrtimer.node.expires);
if (WARN_ON(*vblank_time == vblank->time)) return true;
-- 2.22.0
On Tue, Sep 03, 2019 at 08:50:29AM -0400, Rodrigo Siqueira wrote:
Thanks for this patch! It looks good for me.
Reviewed-by: Rodrigo Siqueira rodrigosiqueiramelo@gmail.com
Thanks for taking a look at all this, entire series merged. With the r-b from Ville on patch 1, but I'm happy to further discuss your questions. Plus I augmented the commit message a bit for patch 1 to explain the missed vblank story better. -Daniel
On 07/19, Daniel Vetter wrote:
We can reduce the critical section in vkms_vblank_simulate under output->lock quite a lot:
hrtimer_forward_now just needs to be ordered correctly wrt drm_crtc_handle_vblank. We already access the hrtimer timestamp without locks. While auditing that I noticed that we don't correctly annotate the read there, so sprinkle a READ_ONCE to make sure the compiler doesn't do anything foolish.
drm_crtc_handle_vblank must stay under the lock to avoid races with drm_crtc_arm_vblank_event.
The access to vkms_ouptut->crc_state also must stay under the lock.
next problem is making sure the output->state structure doesn't get freed too early. First we rely on a given hrtimer being serialized: If we call drm_crtc_handle_vblank, then we are guaranteed that the previous call to vkms_vblank_simulate has completed. The other side of the coin is that the atomic updates waits for the vblank to happen before it releases the old state. Both taken together means that by the time the atomic update releases the old state, the hrtimer won't access it anymore (it might be accessing the new state at the same time, but that's ok).
state is invariant, except the few fields separate protected by state->crc_lock. So no need to hold the lock for that.
finally the queue_work. We need to make sure there's no races with the flush_work, i.e. when we call flush_work we need to guarantee that the hrtimer can't requeue the work again. This is guaranteed by the same vblank/hrtimer ordering guarantees like the reasoning above why state won't be freed too early: flush_work on the old state is called after wait_for_flip_done in the atomic commit code.
Therefore we can also move everything after the output->crc_state out of the critical section.
Motivated by suggestions from Rodrigo.
Signed-off-by: Daniel Vetter daniel.vetter@intel.com Cc: Rodrigo Siqueira rodrigosiqueiramelo@gmail.com Cc: Haneen Mohammed hamohammed.sa@gmail.com Cc: Daniel Vetter daniel@ffwll.ch
drivers/gpu/drm/vkms/vkms_crtc.c | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-)
diff --git a/drivers/gpu/drm/vkms/vkms_crtc.c b/drivers/gpu/drm/vkms/vkms_crtc.c index 927dafaebc76..74f703b8d22a 100644 --- a/drivers/gpu/drm/vkms/vkms_crtc.c +++ b/drivers/gpu/drm/vkms/vkms_crtc.c @@ -16,17 +16,18 @@ static enum hrtimer_restart vkms_vblank_simulate(struct hrtimer *timer) u64 ret_overrun; bool ret;
- spin_lock(&output->lock);
- ret_overrun = hrtimer_forward_now(&output->vblank_hrtimer, output->period_ns); WARN_ON(ret_overrun != 1);
spin_lock(&output->lock); ret = drm_crtc_handle_vblank(crtc); if (!ret) DRM_ERROR("vkms failure on handling vblank");
state = output->composer_state;
spin_unlock(&output->lock);
if (state && output->composer_enabled) { u64 frame = drm_crtc_accurate_vblank_count(crtc);
@@ -48,8 +49,6 @@ static enum hrtimer_restart vkms_vblank_simulate(struct hrtimer *timer) DRM_DEBUG_DRIVER("Composer worker already queued\n"); }
- spin_unlock(&output->lock);
- return HRTIMER_RESTART;
}
@@ -85,7 +84,7 @@ bool vkms_get_vblank_timestamp(struct drm_device *dev, unsigned int pipe, struct vkms_output *output = &vkmsdev->output; struct drm_vblank_crtc *vblank = &dev->vblank[pipe];
- *vblank_time = output->vblank_hrtimer.node.expires;
*vblank_time = READ_ONCE(output->vblank_hrtimer.node.expires);
if (WARN_ON(*vblank_time == vblank->time)) return true;
-- 2.22.0
-- Rodrigo Siqueira Software Engineer, Advanced Micro Devices (AMD) https://siqueira.tech
On Fri, Jul 19, 2019 at 05:23:12PM +0200, Daniel Vetter wrote:
Noticed while reviewing code. I'm not sure whether this might or might not explain some of the missed vblank hilarity we've been seeing. I think those all go through the vblank completion event, which has unconditional barriers - it always takes the spinlock. Therefore no cc stable.
v2:
- Barrriers are hard, put them in in the right order (Chris).
- Improve the comments a bit.
Cc: Rodrigo Siqueira rodrigosiqueiramelo@gmail.com Cc: Chris Wilson chris@chris-wilson.co.uk Signed-off-by: Daniel Vetter daniel.vetter@intel.com
drivers/gpu/drm/drm_vblank.c | 38 +++++++++++++++++++++++++++++++++++- include/drm/drm_vblank.h | 13 +++++++++++- 2 files changed, 49 insertions(+), 2 deletions(-)
diff --git a/drivers/gpu/drm/drm_vblank.c b/drivers/gpu/drm/drm_vblank.c index 603ab105125d..eb2a8304536c 100644 --- a/drivers/gpu/drm/drm_vblank.c +++ b/drivers/gpu/drm/drm_vblank.c @@ -295,11 +295,23 @@ static void drm_update_vblank_count(struct drm_device *dev, unsigned int pipe, static u64 drm_vblank_count(struct drm_device *dev, unsigned int pipe) { struct drm_vblank_crtc *vblank = &dev->vblank[pipe];
u64 count;
if (WARN_ON(pipe >= dev->num_crtcs)) return 0;
- return vblank->count;
- count = vblank->count;
Hmm. This is now a 64bit quantity, which means on 32bit the load/store won't be atomic. That doesn't seem particularly great.
- /*
* This read barrier corresponds to the implicit write barrier of the
* write seqlock in store_vblank(). Note that this is the only place
* where we need an explicit barrier, since all other access goes
* through drm_vblank_count_and_time(), which already has the required
* read barrier curtesy of the read seqlock.
*/
- smp_rmb();
- return count;
}
/**
On Fri, Jul 19, 2019 at 7:06 PM Ville Syrjälä ville.syrjala@linux.intel.com wrote:
On Fri, Jul 19, 2019 at 05:23:12PM +0200, Daniel Vetter wrote:
Noticed while reviewing code. I'm not sure whether this might or might not explain some of the missed vblank hilarity we've been seeing. I think those all go through the vblank completion event, which has unconditional barriers - it always takes the spinlock. Therefore no cc stable.
v2:
- Barrriers are hard, put them in in the right order (Chris).
- Improve the comments a bit.
Cc: Rodrigo Siqueira rodrigosiqueiramelo@gmail.com Cc: Chris Wilson chris@chris-wilson.co.uk Signed-off-by: Daniel Vetter daniel.vetter@intel.com
drivers/gpu/drm/drm_vblank.c | 38 +++++++++++++++++++++++++++++++++++- include/drm/drm_vblank.h | 13 +++++++++++- 2 files changed, 49 insertions(+), 2 deletions(-)
diff --git a/drivers/gpu/drm/drm_vblank.c b/drivers/gpu/drm/drm_vblank.c index 603ab105125d..eb2a8304536c 100644 --- a/drivers/gpu/drm/drm_vblank.c +++ b/drivers/gpu/drm/drm_vblank.c @@ -295,11 +295,23 @@ static void drm_update_vblank_count(struct drm_device *dev, unsigned int pipe, static u64 drm_vblank_count(struct drm_device *dev, unsigned int pipe) { struct drm_vblank_crtc *vblank = &dev->vblank[pipe];
u64 count; if (WARN_ON(pipe >= dev->num_crtcs)) return 0;
return vblank->count;
count = vblank->count;
Hmm. This is now a 64bit quantity, which means on 32bit the load/store won't be atomic. That doesn't seem particularly great.
Hm ... so read-side seqno here? At least for 32bit, but not sure that's worth it, probably simpler to just do it unconditionally. Otoh ... do we care? This matters like once every every year at 120Hz ... -Daniel
/*
* This read barrier corresponds to the implicit write barrier of the
* write seqlock in store_vblank(). Note that this is the only place
* where we need an explicit barrier, since all other access goes
* through drm_vblank_count_and_time(), which already has the required
* read barrier curtesy of the read seqlock.
*/
smp_rmb();
return count;
}
/**
-- Ville Syrjälä Intel
On Fri, Jul 19, 2019 at 08:33:49PM +0200, Daniel Vetter wrote:
On Fri, Jul 19, 2019 at 7:06 PM Ville Syrjälä ville.syrjala@linux.intel.com wrote:
On Fri, Jul 19, 2019 at 05:23:12PM +0200, Daniel Vetter wrote:
Noticed while reviewing code. I'm not sure whether this might or might not explain some of the missed vblank hilarity we've been seeing. I think those all go through the vblank completion event, which has unconditional barriers - it always takes the spinlock. Therefore no cc stable.
v2:
- Barrriers are hard, put them in in the right order (Chris).
- Improve the comments a bit.
Cc: Rodrigo Siqueira rodrigosiqueiramelo@gmail.com Cc: Chris Wilson chris@chris-wilson.co.uk Signed-off-by: Daniel Vetter daniel.vetter@intel.com
drivers/gpu/drm/drm_vblank.c | 38 +++++++++++++++++++++++++++++++++++- include/drm/drm_vblank.h | 13 +++++++++++- 2 files changed, 49 insertions(+), 2 deletions(-)
diff --git a/drivers/gpu/drm/drm_vblank.c b/drivers/gpu/drm/drm_vblank.c index 603ab105125d..eb2a8304536c 100644 --- a/drivers/gpu/drm/drm_vblank.c +++ b/drivers/gpu/drm/drm_vblank.c @@ -295,11 +295,23 @@ static void drm_update_vblank_count(struct drm_device *dev, unsigned int pipe, static u64 drm_vblank_count(struct drm_device *dev, unsigned int pipe) { struct drm_vblank_crtc *vblank = &dev->vblank[pipe];
u64 count; if (WARN_ON(pipe >= dev->num_crtcs)) return 0;
return vblank->count;
count = vblank->count;
Hmm. This is now a 64bit quantity, which means on 32bit the load/store won't be atomic. That doesn't seem particularly great.
Hm ... so read-side seqno here? At least for 32bit, but not sure that's worth it, probably simpler to just do it unconditionally.
Or make it atomic64_t perhaps?
Otoh ... do we care? This matters like once every every year at 120Hz ...
Dunno. Might avoid a few odd bug reports maybe.
Noticed while reviewing code. I'm not sure whether this might or might not explain some of the missed vblank hilarity we've been seeing. I think those all go through the vblank completion event, which has unconditional barriers - it always takes the spinlock. Therefore no cc stable.
v2: - Barrriers are hard, put them in in the right order (Chris). - Improve the comments a bit.
v3:
Ville noticed that on 32bit we might be breaking up the load/stores, now that the vblank counter has been switched over to be 64 bit. Fix that up by switching to atomic64_t. This this happens so rarely in practice I figured no need to cc: stable ...
Cc: Ville Syrjälä ville.syrjala@linux.intel.com Cc: Keith Packard keithp@keithp.com References: 570e86963a51 ("drm: Widen vblank count to 64-bits [v3]") Cc: Rodrigo Siqueira rodrigosiqueiramelo@gmail.com Cc: Chris Wilson chris@chris-wilson.co.uk Signed-off-by: Daniel Vetter daniel.vetter@intel.com --- drivers/gpu/drm/drm_vblank.c | 45 ++++++++++++++++++++++++++++++++---- include/drm/drm_vblank.h | 15 ++++++++++-- 2 files changed, 54 insertions(+), 6 deletions(-)
diff --git a/drivers/gpu/drm/drm_vblank.c b/drivers/gpu/drm/drm_vblank.c index 603ab105125d..03e37bceac9c 100644 --- a/drivers/gpu/drm/drm_vblank.c +++ b/drivers/gpu/drm/drm_vblank.c @@ -107,7 +107,7 @@ static void store_vblank(struct drm_device *dev, unsigned int pipe,
write_seqlock(&vblank->seqlock); vblank->time = t_vblank; - vblank->count += vblank_count_inc; + atomic64_add(vblank_count_inc, &vblank->count); write_sequnlock(&vblank->seqlock); }
@@ -273,7 +273,8 @@ static void drm_update_vblank_count(struct drm_device *dev, unsigned int pipe,
DRM_DEBUG_VBL("updating vblank count on crtc %u:" " current=%llu, diff=%u, hw=%u hw_last=%u\n", - pipe, vblank->count, diff, cur_vblank, vblank->last); + pipe, atomic64_read(&vblank->count), diff, + cur_vblank, vblank->last);
if (diff == 0) { WARN_ON_ONCE(cur_vblank != vblank->last); @@ -295,11 +296,23 @@ static void drm_update_vblank_count(struct drm_device *dev, unsigned int pipe, static u64 drm_vblank_count(struct drm_device *dev, unsigned int pipe) { struct drm_vblank_crtc *vblank = &dev->vblank[pipe]; + u64 count;
if (WARN_ON(pipe >= dev->num_crtcs)) return 0;
- return vblank->count; + count = atomic64_read(&vblank->count); + + /* + * This read barrier corresponds to the implicit write barrier of the + * write seqlock in store_vblank(). Note that this is the only place + * where we need an explicit barrier, since all other access goes + * through drm_vblank_count_and_time(), which already has the required + * read barrier curtesy of the read seqlock. + */ + smp_rmb(); + + return count; }
/** @@ -764,6 +777,14 @@ drm_get_last_vbltimestamp(struct drm_device *dev, unsigned int pipe, * vblank interrupt (since it only reports the software vblank counter), see * drm_crtc_accurate_vblank_count() for such use-cases. * + * Note that for a given vblank counter value drm_crtc_handle_vblank() + * and drm_crtc_vblank_count() or drm_crtc_vblank_count_and_time() + * provide a barrier: Any writes done before calling + * drm_crtc_handle_vblank() will be visible to callers of the later + * functions, iff the vblank count is the same or a later one. + * + * See also &drm_vblank_crtc.count. + * * Returns: * The software vblank counter. */ @@ -801,7 +822,7 @@ static u64 drm_vblank_count_and_time(struct drm_device *dev, unsigned int pipe,
do { seq = read_seqbegin(&vblank->seqlock); - vblank_count = vblank->count; + vblank_count = atomic64_read(&vblank->count); *vblanktime = vblank->time; } while (read_seqretry(&vblank->seqlock, seq));
@@ -818,6 +839,14 @@ static u64 drm_vblank_count_and_time(struct drm_device *dev, unsigned int pipe, * vblank events since the system was booted, including lost events due to * modesetting activity. Returns corresponding system timestamp of the time * of the vblank interval that corresponds to the current vblank counter value. + * + * Note that for a given vblank counter value drm_crtc_handle_vblank() + * and drm_crtc_vblank_count() or drm_crtc_vblank_count_and_time() + * provide a barrier: Any writes done before calling + * drm_crtc_handle_vblank() will be visible to callers of the later + * functions, iff the vblank count is the same or a later one. + * + * See also &drm_vblank_crtc.count. */ u64 drm_crtc_vblank_count_and_time(struct drm_crtc *crtc, ktime_t *vblanktime) @@ -1791,6 +1820,14 @@ EXPORT_SYMBOL(drm_handle_vblank); * * This is the native KMS version of drm_handle_vblank(). * + * Note that for a given vblank counter value drm_crtc_handle_vblank() + * and drm_crtc_vblank_count() or drm_crtc_vblank_count_and_time() + * provide a barrier: Any writes done before calling + * drm_crtc_handle_vblank() will be visible to callers of the later + * functions, iff the vblank count is the same or a later one. + * + * See also &drm_vblank_crtc.count. + * * Returns: * True if the event was successfully handled, false on failure. */ diff --git a/include/drm/drm_vblank.h b/include/drm/drm_vblank.h index 9fe4ba8bc622..c16c44052b3d 100644 --- a/include/drm/drm_vblank.h +++ b/include/drm/drm_vblank.h @@ -109,9 +109,20 @@ struct drm_vblank_crtc { seqlock_t seqlock;
/** - * @count: Current software vblank counter. + * @count: + * + * Current software vblank counter. + * + * Note that for a given vblank counter value drm_crtc_handle_vblank() + * and drm_crtc_vblank_count() or drm_crtc_vblank_count_and_time() + * provide a barrier: Any writes done before calling + * drm_crtc_handle_vblank() will be visible to callers of the later + * functions, iff the vblank count is the same or a later one. + * + * IMPORTANT: This guarantee requires barriers, therefor never access + * this field directly. Use drm_crtc_vblank_count() instead. */ - u64 count; + atomic64_t count; /** * @time: Vblank timestamp corresponding to @count. */
On Tue, Jul 23, 2019 at 03:13:37PM +0200, Daniel Vetter wrote:
Noticed while reviewing code. I'm not sure whether this might or might not explain some of the missed vblank hilarity we've been seeing. I think those all go through the vblank completion event, which has unconditional barriers - it always takes the spinlock. Therefore no cc stable.
v2:
- Barrriers are hard, put them in in the right order (Chris).
- Improve the comments a bit.
v3:
Ville noticed that on 32bit we might be breaking up the load/stores, now that the vblank counter has been switched over to be 64 bit. Fix that up by switching to atomic64_t. This this happens so rarely in practice I figured no need to cc: stable ...
Cc: Ville Syrjälä ville.syrjala@linux.intel.com Cc: Keith Packard keithp@keithp.com References: 570e86963a51 ("drm: Widen vblank count to 64-bits [v3]") Cc: Rodrigo Siqueira rodrigosiqueiramelo@gmail.com Cc: Chris Wilson chris@chris-wilson.co.uk Signed-off-by: Daniel Vetter daniel.vetter@intel.com
Reviewed-by: Ville Syrjälä ville.syrjala@linux.intel.com
drivers/gpu/drm/drm_vblank.c | 45 ++++++++++++++++++++++++++++++++---- include/drm/drm_vblank.h | 15 ++++++++++-- 2 files changed, 54 insertions(+), 6 deletions(-)
diff --git a/drivers/gpu/drm/drm_vblank.c b/drivers/gpu/drm/drm_vblank.c index 603ab105125d..03e37bceac9c 100644 --- a/drivers/gpu/drm/drm_vblank.c +++ b/drivers/gpu/drm/drm_vblank.c @@ -107,7 +107,7 @@ static void store_vblank(struct drm_device *dev, unsigned int pipe,
write_seqlock(&vblank->seqlock); vblank->time = t_vblank;
- vblank->count += vblank_count_inc;
- atomic64_add(vblank_count_inc, &vblank->count); write_sequnlock(&vblank->seqlock);
}
@@ -273,7 +273,8 @@ static void drm_update_vblank_count(struct drm_device *dev, unsigned int pipe,
DRM_DEBUG_VBL("updating vblank count on crtc %u:" " current=%llu, diff=%u, hw=%u hw_last=%u\n",
pipe, vblank->count, diff, cur_vblank, vblank->last);
pipe, atomic64_read(&vblank->count), diff,
cur_vblank, vblank->last);
if (diff == 0) { WARN_ON_ONCE(cur_vblank != vblank->last);
@@ -295,11 +296,23 @@ static void drm_update_vblank_count(struct drm_device *dev, unsigned int pipe, static u64 drm_vblank_count(struct drm_device *dev, unsigned int pipe) { struct drm_vblank_crtc *vblank = &dev->vblank[pipe];
u64 count;
if (WARN_ON(pipe >= dev->num_crtcs)) return 0;
- return vblank->count;
- count = atomic64_read(&vblank->count);
- /*
* This read barrier corresponds to the implicit write barrier of the
* write seqlock in store_vblank(). Note that this is the only place
* where we need an explicit barrier, since all other access goes
* through drm_vblank_count_and_time(), which already has the required
* read barrier curtesy of the read seqlock.
*/
- smp_rmb();
- return count;
}
/** @@ -764,6 +777,14 @@ drm_get_last_vbltimestamp(struct drm_device *dev, unsigned int pipe,
- vblank interrupt (since it only reports the software vblank counter), see
- drm_crtc_accurate_vblank_count() for such use-cases.
- Note that for a given vblank counter value drm_crtc_handle_vblank()
- and drm_crtc_vblank_count() or drm_crtc_vblank_count_and_time()
- provide a barrier: Any writes done before calling
- drm_crtc_handle_vblank() will be visible to callers of the later
- functions, iff the vblank count is the same or a later one.
- See also &drm_vblank_crtc.count.
*/
- Returns:
- The software vblank counter.
@@ -801,7 +822,7 @@ static u64 drm_vblank_count_and_time(struct drm_device *dev, unsigned int pipe,
do { seq = read_seqbegin(&vblank->seqlock);
vblank_count = vblank->count;
*vblanktime = vblank->time; } while (read_seqretry(&vblank->seqlock, seq));vblank_count = atomic64_read(&vblank->count);
@@ -818,6 +839,14 @@ static u64 drm_vblank_count_and_time(struct drm_device *dev, unsigned int pipe,
- vblank events since the system was booted, including lost events due to
- modesetting activity. Returns corresponding system timestamp of the time
- of the vblank interval that corresponds to the current vblank counter value.
- Note that for a given vblank counter value drm_crtc_handle_vblank()
- and drm_crtc_vblank_count() or drm_crtc_vblank_count_and_time()
- provide a barrier: Any writes done before calling
- drm_crtc_handle_vblank() will be visible to callers of the later
- functions, iff the vblank count is the same or a later one.
*/
- See also &drm_vblank_crtc.count.
u64 drm_crtc_vblank_count_and_time(struct drm_crtc *crtc, ktime_t *vblanktime) @@ -1791,6 +1820,14 @@ EXPORT_SYMBOL(drm_handle_vblank);
- This is the native KMS version of drm_handle_vblank().
- Note that for a given vblank counter value drm_crtc_handle_vblank()
- and drm_crtc_vblank_count() or drm_crtc_vblank_count_and_time()
- provide a barrier: Any writes done before calling
- drm_crtc_handle_vblank() will be visible to callers of the later
- functions, iff the vblank count is the same or a later one.
- See also &drm_vblank_crtc.count.
*/
- Returns:
- True if the event was successfully handled, false on failure.
diff --git a/include/drm/drm_vblank.h b/include/drm/drm_vblank.h index 9fe4ba8bc622..c16c44052b3d 100644 --- a/include/drm/drm_vblank.h +++ b/include/drm/drm_vblank.h @@ -109,9 +109,20 @@ struct drm_vblank_crtc { seqlock_t seqlock;
/**
* @count: Current software vblank counter.
* @count:
*
* Current software vblank counter.
*
* Note that for a given vblank counter value drm_crtc_handle_vblank()
* and drm_crtc_vblank_count() or drm_crtc_vblank_count_and_time()
* provide a barrier: Any writes done before calling
* drm_crtc_handle_vblank() will be visible to callers of the later
* functions, iff the vblank count is the same or a later one.
*
* IMPORTANT: This guarantee requires barriers, therefor never access
*/* this field directly. Use drm_crtc_vblank_count() instead.
- u64 count;
- atomic64_t count; /**
*/
- @time: Vblank timestamp corresponding to @count.
-- 2.22.0
Hi Daniel,
All the series look really good for me. I just have some few questions here.
On 07/23, Daniel Vetter wrote:
Noticed while reviewing code. I'm not sure whether this might or might not explain some of the missed vblank hilarity we've been seeing. I
I have to admit that I'm a little bit confused about the "missed vblank hilarity we've been seeing". Could you elaborate a little bit more about this problem in the commit message?
Additionally, how about break this commit in two? One dedicated to the barriers and the atomic64, and the other related to the documentation?
think those all go through the vblank completion event, which has unconditional barriers - it always takes the spinlock. Therefore no cc stable.
v2:
- Barrriers are hard, put them in in the right order (Chris).
- Improve the comments a bit.
v3:
Ville noticed that on 32bit we might be breaking up the load/stores, now that the vblank counter has been switched over to be 64 bit. Fix that up by switching to atomic64_t. This this happens so rarely in practice I figured no need to cc: stable ...
Cc: Ville Syrjälä ville.syrjala@linux.intel.com Cc: Keith Packard keithp@keithp.com References: 570e86963a51 ("drm: Widen vblank count to 64-bits [v3]") Cc: Rodrigo Siqueira rodrigosiqueiramelo@gmail.com Cc: Chris Wilson chris@chris-wilson.co.uk Signed-off-by: Daniel Vetter daniel.vetter@intel.com
drivers/gpu/drm/drm_vblank.c | 45 ++++++++++++++++++++++++++++++++---- include/drm/drm_vblank.h | 15 ++++++++++-- 2 files changed, 54 insertions(+), 6 deletions(-)
diff --git a/drivers/gpu/drm/drm_vblank.c b/drivers/gpu/drm/drm_vblank.c index 603ab105125d..03e37bceac9c 100644 --- a/drivers/gpu/drm/drm_vblank.c +++ b/drivers/gpu/drm/drm_vblank.c @@ -107,7 +107,7 @@ static void store_vblank(struct drm_device *dev, unsigned int pipe,
write_seqlock(&vblank->seqlock); vblank->time = t_vblank;
- vblank->count += vblank_count_inc;
- atomic64_add(vblank_count_inc, &vblank->count); write_sequnlock(&vblank->seqlock);
}
@@ -273,7 +273,8 @@ static void drm_update_vblank_count(struct drm_device *dev, unsigned int pipe,
DRM_DEBUG_VBL("updating vblank count on crtc %u:" " current=%llu, diff=%u, hw=%u hw_last=%u\n",
pipe, vblank->count, diff, cur_vblank, vblank->last);
pipe, atomic64_read(&vblank->count), diff,
cur_vblank, vblank->last);
if (diff == 0) { WARN_ON_ONCE(cur_vblank != vblank->last);
@@ -295,11 +296,23 @@ static void drm_update_vblank_count(struct drm_device *dev, unsigned int pipe, static u64 drm_vblank_count(struct drm_device *dev, unsigned int pipe) { struct drm_vblank_crtc *vblank = &dev->vblank[pipe];
u64 count;
if (WARN_ON(pipe >= dev->num_crtcs)) return 0;
- return vblank->count;
- count = atomic64_read(&vblank->count);
- /*
* This read barrier corresponds to the implicit write barrier of the
* write seqlock in store_vblank(). Note that this is the only place
* where we need an explicit barrier, since all other access goes
* through drm_vblank_count_and_time(), which already has the required
* read barrier curtesy of the read seqlock.
*/
- smp_rmb();
I think I did not get all the idea behind the smp_rmb() in this function. FWIU, smp_xxx are used for preventing race conditions in a multiprocessor system, right? In this sense, I can presume that this change can bring benefits for VKMS or any other virtual driver; on the other hand, this will not bring any advantage on real drivers like i915 and amdgpu since these devices are not related with smp stuff, right?
Thanks
- return count;
}
/** @@ -764,6 +777,14 @@ drm_get_last_vbltimestamp(struct drm_device *dev, unsigned int pipe,
- vblank interrupt (since it only reports the software vblank counter), see
- drm_crtc_accurate_vblank_count() for such use-cases.
- Note that for a given vblank counter value drm_crtc_handle_vblank()
- and drm_crtc_vblank_count() or drm_crtc_vblank_count_and_time()
- provide a barrier: Any writes done before calling
- drm_crtc_handle_vblank() will be visible to callers of the later
- functions, iff the vblank count is the same or a later one.
- See also &drm_vblank_crtc.count.
*/
- Returns:
- The software vblank counter.
@@ -801,7 +822,7 @@ static u64 drm_vblank_count_and_time(struct drm_device *dev, unsigned int pipe,
do { seq = read_seqbegin(&vblank->seqlock);
vblank_count = vblank->count;
*vblanktime = vblank->time; } while (read_seqretry(&vblank->seqlock, seq));vblank_count = atomic64_read(&vblank->count);
@@ -818,6 +839,14 @@ static u64 drm_vblank_count_and_time(struct drm_device *dev, unsigned int pipe,
- vblank events since the system was booted, including lost events due to
- modesetting activity. Returns corresponding system timestamp of the time
- of the vblank interval that corresponds to the current vblank counter value.
- Note that for a given vblank counter value drm_crtc_handle_vblank()
- and drm_crtc_vblank_count() or drm_crtc_vblank_count_and_time()
- provide a barrier: Any writes done before calling
- drm_crtc_handle_vblank() will be visible to callers of the later
- functions, iff the vblank count is the same or a later one.
*/
- See also &drm_vblank_crtc.count.
u64 drm_crtc_vblank_count_and_time(struct drm_crtc *crtc, ktime_t *vblanktime) @@ -1791,6 +1820,14 @@ EXPORT_SYMBOL(drm_handle_vblank);
- This is the native KMS version of drm_handle_vblank().
- Note that for a given vblank counter value drm_crtc_handle_vblank()
- and drm_crtc_vblank_count() or drm_crtc_vblank_count_and_time()
- provide a barrier: Any writes done before calling
- drm_crtc_handle_vblank() will be visible to callers of the later
- functions, iff the vblank count is the same or a later one.
- See also &drm_vblank_crtc.count.
*/
- Returns:
- True if the event was successfully handled, false on failure.
diff --git a/include/drm/drm_vblank.h b/include/drm/drm_vblank.h index 9fe4ba8bc622..c16c44052b3d 100644 --- a/include/drm/drm_vblank.h +++ b/include/drm/drm_vblank.h @@ -109,9 +109,20 @@ struct drm_vblank_crtc { seqlock_t seqlock;
/**
* @count: Current software vblank counter.
* @count:
*
* Current software vblank counter.
*
* Note that for a given vblank counter value drm_crtc_handle_vblank()
* and drm_crtc_vblank_count() or drm_crtc_vblank_count_and_time()
* provide a barrier: Any writes done before calling
* drm_crtc_handle_vblank() will be visible to callers of the later
* functions, iff the vblank count is the same or a later one.
*
* IMPORTANT: This guarantee requires barriers, therefor never access
*/* this field directly. Use drm_crtc_vblank_count() instead.
- u64 count;
- atomic64_t count; /**
*/
- @time: Vblank timestamp corresponding to @count.
-- 2.22.0
On Tue, Sep 03, 2019 at 08:47:03AM -0400, Rodrigo Siqueira wrote:
Hi Daniel,
All the series look really good for me. I just have some few questions here.
On 07/23, Daniel Vetter wrote:
Noticed while reviewing code. I'm not sure whether this might or might not explain some of the missed vblank hilarity we've been seeing. I
I have to admit that I'm a little bit confused about the "missed vblank hilarity we've been seeing". Could you elaborate a little bit more about this problem in the commit message?
We've had various reports on various drivers that hw vblanks seem to get lost and the driver stuck on vblank waits. I think most of those where just driver bugs, but could be also that there's some issues in the vblank core.
Additionally, how about break this commit in two? One dedicated to the barriers and the atomic64, and the other related to the documentation?
think those all go through the vblank completion event, which has unconditional barriers - it always takes the spinlock. Therefore no cc stable.
v2:
- Barrriers are hard, put them in in the right order (Chris).
- Improve the comments a bit.
v3:
Ville noticed that on 32bit we might be breaking up the load/stores, now that the vblank counter has been switched over to be 64 bit. Fix that up by switching to atomic64_t. This this happens so rarely in practice I figured no need to cc: stable ...
Cc: Ville Syrjälä ville.syrjala@linux.intel.com Cc: Keith Packard keithp@keithp.com References: 570e86963a51 ("drm: Widen vblank count to 64-bits [v3]") Cc: Rodrigo Siqueira rodrigosiqueiramelo@gmail.com Cc: Chris Wilson chris@chris-wilson.co.uk Signed-off-by: Daniel Vetter daniel.vetter@intel.com
drivers/gpu/drm/drm_vblank.c | 45 ++++++++++++++++++++++++++++++++---- include/drm/drm_vblank.h | 15 ++++++++++-- 2 files changed, 54 insertions(+), 6 deletions(-)
diff --git a/drivers/gpu/drm/drm_vblank.c b/drivers/gpu/drm/drm_vblank.c index 603ab105125d..03e37bceac9c 100644 --- a/drivers/gpu/drm/drm_vblank.c +++ b/drivers/gpu/drm/drm_vblank.c @@ -107,7 +107,7 @@ static void store_vblank(struct drm_device *dev, unsigned int pipe,
write_seqlock(&vblank->seqlock); vblank->time = t_vblank;
- vblank->count += vblank_count_inc;
- atomic64_add(vblank_count_inc, &vblank->count); write_sequnlock(&vblank->seqlock);
}
@@ -273,7 +273,8 @@ static void drm_update_vblank_count(struct drm_device *dev, unsigned int pipe,
DRM_DEBUG_VBL("updating vblank count on crtc %u:" " current=%llu, diff=%u, hw=%u hw_last=%u\n",
pipe, vblank->count, diff, cur_vblank, vblank->last);
pipe, atomic64_read(&vblank->count), diff,
cur_vblank, vblank->last);
if (diff == 0) { WARN_ON_ONCE(cur_vblank != vblank->last);
@@ -295,11 +296,23 @@ static void drm_update_vblank_count(struct drm_device *dev, unsigned int pipe, static u64 drm_vblank_count(struct drm_device *dev, unsigned int pipe) { struct drm_vblank_crtc *vblank = &dev->vblank[pipe];
u64 count;
if (WARN_ON(pipe >= dev->num_crtcs)) return 0;
- return vblank->count;
- count = atomic64_read(&vblank->count);
- /*
* This read barrier corresponds to the implicit write barrier of the
* write seqlock in store_vblank(). Note that this is the only place
* where we need an explicit barrier, since all other access goes
* through drm_vblank_count_and_time(), which already has the required
* read barrier curtesy of the read seqlock.
*/
- smp_rmb();
I think I did not get all the idea behind the smp_rmb() in this function. FWIU, smp_xxx are used for preventing race conditions in a multiprocessor system, right? In this sense, I can presume that this change can bring benefits for VKMS or any other virtual driver; on the other hand, this will not bring any advantage on real drivers like i915 and amdgpu since these devices are not related with smp stuff, right?
smp or not smp is about the cpu your driver is running on, not anything to do with the device hardware itself. And nowadays there's simply no single-threaded processors anymore, everything has at least 2 cores (even the tiniest soc). So yeah, this matters for everyone.
smp_* functions only get compiled out to nothing if you have CONFIG_UP (which means only 1 cpu core with only 1 SMT thread is supported).
And yeah correctly placing smp barriers is Real Hard Stuff (tm). -Daniel
Thanks
- return count;
}
/** @@ -764,6 +777,14 @@ drm_get_last_vbltimestamp(struct drm_device *dev, unsigned int pipe,
- vblank interrupt (since it only reports the software vblank counter), see
- drm_crtc_accurate_vblank_count() for such use-cases.
- Note that for a given vblank counter value drm_crtc_handle_vblank()
- and drm_crtc_vblank_count() or drm_crtc_vblank_count_and_time()
- provide a barrier: Any writes done before calling
- drm_crtc_handle_vblank() will be visible to callers of the later
- functions, iff the vblank count is the same or a later one.
- See also &drm_vblank_crtc.count.
*/
- Returns:
- The software vblank counter.
@@ -801,7 +822,7 @@ static u64 drm_vblank_count_and_time(struct drm_device *dev, unsigned int pipe,
do { seq = read_seqbegin(&vblank->seqlock);
vblank_count = vblank->count;
*vblanktime = vblank->time; } while (read_seqretry(&vblank->seqlock, seq));vblank_count = atomic64_read(&vblank->count);
@@ -818,6 +839,14 @@ static u64 drm_vblank_count_and_time(struct drm_device *dev, unsigned int pipe,
- vblank events since the system was booted, including lost events due to
- modesetting activity. Returns corresponding system timestamp of the time
- of the vblank interval that corresponds to the current vblank counter value.
- Note that for a given vblank counter value drm_crtc_handle_vblank()
- and drm_crtc_vblank_count() or drm_crtc_vblank_count_and_time()
- provide a barrier: Any writes done before calling
- drm_crtc_handle_vblank() will be visible to callers of the later
- functions, iff the vblank count is the same or a later one.
*/
- See also &drm_vblank_crtc.count.
u64 drm_crtc_vblank_count_and_time(struct drm_crtc *crtc, ktime_t *vblanktime) @@ -1791,6 +1820,14 @@ EXPORT_SYMBOL(drm_handle_vblank);
- This is the native KMS version of drm_handle_vblank().
- Note that for a given vblank counter value drm_crtc_handle_vblank()
- and drm_crtc_vblank_count() or drm_crtc_vblank_count_and_time()
- provide a barrier: Any writes done before calling
- drm_crtc_handle_vblank() will be visible to callers of the later
- functions, iff the vblank count is the same or a later one.
- See also &drm_vblank_crtc.count.
*/
- Returns:
- True if the event was successfully handled, false on failure.
diff --git a/include/drm/drm_vblank.h b/include/drm/drm_vblank.h index 9fe4ba8bc622..c16c44052b3d 100644 --- a/include/drm/drm_vblank.h +++ b/include/drm/drm_vblank.h @@ -109,9 +109,20 @@ struct drm_vblank_crtc { seqlock_t seqlock;
/**
* @count: Current software vblank counter.
* @count:
*
* Current software vblank counter.
*
* Note that for a given vblank counter value drm_crtc_handle_vblank()
* and drm_crtc_vblank_count() or drm_crtc_vblank_count_and_time()
* provide a barrier: Any writes done before calling
* drm_crtc_handle_vblank() will be visible to callers of the later
* functions, iff the vblank count is the same or a later one.
*
* IMPORTANT: This guarantee requires barriers, therefor never access
*/* this field directly. Use drm_crtc_vblank_count() instead.
- u64 count;
- atomic64_t count; /**
*/
- @time: Vblank timestamp corresponding to @count.
-- 2.22.0
-- Rodrigo Siqueira Software Engineer, Advanced Micro Devices (AMD) https://siqueira.tech
Intel-gfx mailing list Intel-gfx@lists.freedesktop.org https://lists.freedesktop.org/mailman/listinfo/intel-gfx
Hi, thanks for the explanation.
I noticed that I forgot to add my r-b.
Reviewed-by: Rodrigo Siqueira rodrigosiqueiramelo@gmail.com
On 09/03, Daniel Vetter wrote:
On Tue, Sep 03, 2019 at 08:47:03AM -0400, Rodrigo Siqueira wrote:
Hi Daniel,
All the series look really good for me. I just have some few questions here.
On 07/23, Daniel Vetter wrote:
Noticed while reviewing code. I'm not sure whether this might or might not explain some of the missed vblank hilarity we've been seeing. I
I have to admit that I'm a little bit confused about the "missed vblank hilarity we've been seeing". Could you elaborate a little bit more about this problem in the commit message?
We've had various reports on various drivers that hw vblanks seem to get lost and the driver stuck on vblank waits. I think most of those where just driver bugs, but could be also that there's some issues in the vblank core.
Additionally, how about break this commit in two? One dedicated to the barriers and the atomic64, and the other related to the documentation?
think those all go through the vblank completion event, which has unconditional barriers - it always takes the spinlock. Therefore no cc stable.
v2:
- Barrriers are hard, put them in in the right order (Chris).
- Improve the comments a bit.
v3:
Ville noticed that on 32bit we might be breaking up the load/stores, now that the vblank counter has been switched over to be 64 bit. Fix that up by switching to atomic64_t. This this happens so rarely in practice I figured no need to cc: stable ...
Cc: Ville Syrjälä ville.syrjala@linux.intel.com Cc: Keith Packard keithp@keithp.com References: 570e86963a51 ("drm: Widen vblank count to 64-bits [v3]") Cc: Rodrigo Siqueira rodrigosiqueiramelo@gmail.com Cc: Chris Wilson chris@chris-wilson.co.uk Signed-off-by: Daniel Vetter daniel.vetter@intel.com
drivers/gpu/drm/drm_vblank.c | 45 ++++++++++++++++++++++++++++++++---- include/drm/drm_vblank.h | 15 ++++++++++-- 2 files changed, 54 insertions(+), 6 deletions(-)
diff --git a/drivers/gpu/drm/drm_vblank.c b/drivers/gpu/drm/drm_vblank.c index 603ab105125d..03e37bceac9c 100644 --- a/drivers/gpu/drm/drm_vblank.c +++ b/drivers/gpu/drm/drm_vblank.c @@ -107,7 +107,7 @@ static void store_vblank(struct drm_device *dev, unsigned int pipe,
write_seqlock(&vblank->seqlock); vblank->time = t_vblank;
- vblank->count += vblank_count_inc;
- atomic64_add(vblank_count_inc, &vblank->count); write_sequnlock(&vblank->seqlock);
}
@@ -273,7 +273,8 @@ static void drm_update_vblank_count(struct drm_device *dev, unsigned int pipe,
DRM_DEBUG_VBL("updating vblank count on crtc %u:" " current=%llu, diff=%u, hw=%u hw_last=%u\n",
pipe, vblank->count, diff, cur_vblank, vblank->last);
pipe, atomic64_read(&vblank->count), diff,
cur_vblank, vblank->last);
if (diff == 0) { WARN_ON_ONCE(cur_vblank != vblank->last);
@@ -295,11 +296,23 @@ static void drm_update_vblank_count(struct drm_device *dev, unsigned int pipe, static u64 drm_vblank_count(struct drm_device *dev, unsigned int pipe) { struct drm_vblank_crtc *vblank = &dev->vblank[pipe];
u64 count;
if (WARN_ON(pipe >= dev->num_crtcs)) return 0;
- return vblank->count;
- count = atomic64_read(&vblank->count);
- /*
* This read barrier corresponds to the implicit write barrier of the
* write seqlock in store_vblank(). Note that this is the only place
* where we need an explicit barrier, since all other access goes
* through drm_vblank_count_and_time(), which already has the required
* read barrier curtesy of the read seqlock.
*/
- smp_rmb();
I think I did not get all the idea behind the smp_rmb() in this function. FWIU, smp_xxx are used for preventing race conditions in a multiprocessor system, right? In this sense, I can presume that this change can bring benefits for VKMS or any other virtual driver; on the other hand, this will not bring any advantage on real drivers like i915 and amdgpu since these devices are not related with smp stuff, right?
smp or not smp is about the cpu your driver is running on, not anything to do with the device hardware itself. And nowadays there's simply no single-threaded processors anymore, everything has at least 2 cores (even the tiniest soc). So yeah, this matters for everyone.
smp_* functions only get compiled out to nothing if you have CONFIG_UP (which means only 1 cpu core with only 1 SMT thread is supported).
And yeah correctly placing smp barriers is Real Hard Stuff (tm). -Daniel
Thanks
- return count;
}
/** @@ -764,6 +777,14 @@ drm_get_last_vbltimestamp(struct drm_device *dev, unsigned int pipe,
- vblank interrupt (since it only reports the software vblank counter), see
- drm_crtc_accurate_vblank_count() for such use-cases.
- Note that for a given vblank counter value drm_crtc_handle_vblank()
- and drm_crtc_vblank_count() or drm_crtc_vblank_count_and_time()
- provide a barrier: Any writes done before calling
- drm_crtc_handle_vblank() will be visible to callers of the later
- functions, iff the vblank count is the same or a later one.
- See also &drm_vblank_crtc.count.
*/
- Returns:
- The software vblank counter.
@@ -801,7 +822,7 @@ static u64 drm_vblank_count_and_time(struct drm_device *dev, unsigned int pipe,
do { seq = read_seqbegin(&vblank->seqlock);
vblank_count = vblank->count;
*vblanktime = vblank->time; } while (read_seqretry(&vblank->seqlock, seq));vblank_count = atomic64_read(&vblank->count);
@@ -818,6 +839,14 @@ static u64 drm_vblank_count_and_time(struct drm_device *dev, unsigned int pipe,
- vblank events since the system was booted, including lost events due to
- modesetting activity. Returns corresponding system timestamp of the time
- of the vblank interval that corresponds to the current vblank counter value.
- Note that for a given vblank counter value drm_crtc_handle_vblank()
- and drm_crtc_vblank_count() or drm_crtc_vblank_count_and_time()
- provide a barrier: Any writes done before calling
- drm_crtc_handle_vblank() will be visible to callers of the later
- functions, iff the vblank count is the same or a later one.
*/
- See also &drm_vblank_crtc.count.
u64 drm_crtc_vblank_count_and_time(struct drm_crtc *crtc, ktime_t *vblanktime) @@ -1791,6 +1820,14 @@ EXPORT_SYMBOL(drm_handle_vblank);
- This is the native KMS version of drm_handle_vblank().
- Note that for a given vblank counter value drm_crtc_handle_vblank()
- and drm_crtc_vblank_count() or drm_crtc_vblank_count_and_time()
- provide a barrier: Any writes done before calling
- drm_crtc_handle_vblank() will be visible to callers of the later
- functions, iff the vblank count is the same or a later one.
- See also &drm_vblank_crtc.count.
*/
- Returns:
- True if the event was successfully handled, false on failure.
diff --git a/include/drm/drm_vblank.h b/include/drm/drm_vblank.h index 9fe4ba8bc622..c16c44052b3d 100644 --- a/include/drm/drm_vblank.h +++ b/include/drm/drm_vblank.h @@ -109,9 +109,20 @@ struct drm_vblank_crtc { seqlock_t seqlock;
/**
* @count: Current software vblank counter.
* @count:
*
* Current software vblank counter.
*
* Note that for a given vblank counter value drm_crtc_handle_vblank()
* and drm_crtc_vblank_count() or drm_crtc_vblank_count_and_time()
* provide a barrier: Any writes done before calling
* drm_crtc_handle_vblank() will be visible to callers of the later
* functions, iff the vblank count is the same or a later one.
*
* IMPORTANT: This guarantee requires barriers, therefor never access
*/* this field directly. Use drm_crtc_vblank_count() instead.
- u64 count;
- atomic64_t count; /**
*/
- @time: Vblank timestamp corresponding to @count.
-- 2.22.0
-- Rodrigo Siqueira Software Engineer, Advanced Micro Devices (AMD) https://siqueira.tech
Intel-gfx mailing list Intel-gfx@lists.freedesktop.org https://lists.freedesktop.org/mailman/listinfo/intel-gfx
-- Daniel Vetter Software Engineer, Intel Corporation http://blog.ffwll.ch
On Tue, Sep 03, 2019 at 11:17:12AM -0400, Rodrigo Siqueira wrote:
Hi, thanks for the explanation.
I noticed that I forgot to add my r-b.
Reviewed-by: Rodrigo Siqueira rodrigosiqueiramelo@gmail.com
Uh I just pushed, so can't add your r-b now :-/
Sry. -Daniel
On 09/03, Daniel Vetter wrote:
On Tue, Sep 03, 2019 at 08:47:03AM -0400, Rodrigo Siqueira wrote:
Hi Daniel,
All the series look really good for me. I just have some few questions here.
On 07/23, Daniel Vetter wrote:
Noticed while reviewing code. I'm not sure whether this might or might not explain some of the missed vblank hilarity we've been seeing. I
I have to admit that I'm a little bit confused about the "missed vblank hilarity we've been seeing". Could you elaborate a little bit more about this problem in the commit message?
We've had various reports on various drivers that hw vblanks seem to get lost and the driver stuck on vblank waits. I think most of those where just driver bugs, but could be also that there's some issues in the vblank core.
Additionally, how about break this commit in two? One dedicated to the barriers and the atomic64, and the other related to the documentation?
think those all go through the vblank completion event, which has unconditional barriers - it always takes the spinlock. Therefore no cc stable.
v2:
- Barrriers are hard, put them in in the right order (Chris).
- Improve the comments a bit.
v3:
Ville noticed that on 32bit we might be breaking up the load/stores, now that the vblank counter has been switched over to be 64 bit. Fix that up by switching to atomic64_t. This this happens so rarely in practice I figured no need to cc: stable ...
Cc: Ville Syrjälä ville.syrjala@linux.intel.com Cc: Keith Packard keithp@keithp.com References: 570e86963a51 ("drm: Widen vblank count to 64-bits [v3]") Cc: Rodrigo Siqueira rodrigosiqueiramelo@gmail.com Cc: Chris Wilson chris@chris-wilson.co.uk Signed-off-by: Daniel Vetter daniel.vetter@intel.com
drivers/gpu/drm/drm_vblank.c | 45 ++++++++++++++++++++++++++++++++---- include/drm/drm_vblank.h | 15 ++++++++++-- 2 files changed, 54 insertions(+), 6 deletions(-)
diff --git a/drivers/gpu/drm/drm_vblank.c b/drivers/gpu/drm/drm_vblank.c index 603ab105125d..03e37bceac9c 100644 --- a/drivers/gpu/drm/drm_vblank.c +++ b/drivers/gpu/drm/drm_vblank.c @@ -107,7 +107,7 @@ static void store_vblank(struct drm_device *dev, unsigned int pipe,
write_seqlock(&vblank->seqlock); vblank->time = t_vblank;
- vblank->count += vblank_count_inc;
- atomic64_add(vblank_count_inc, &vblank->count); write_sequnlock(&vblank->seqlock);
}
@@ -273,7 +273,8 @@ static void drm_update_vblank_count(struct drm_device *dev, unsigned int pipe,
DRM_DEBUG_VBL("updating vblank count on crtc %u:" " current=%llu, diff=%u, hw=%u hw_last=%u\n",
pipe, vblank->count, diff, cur_vblank, vblank->last);
pipe, atomic64_read(&vblank->count), diff,
cur_vblank, vblank->last);
if (diff == 0) { WARN_ON_ONCE(cur_vblank != vblank->last);
@@ -295,11 +296,23 @@ static void drm_update_vblank_count(struct drm_device *dev, unsigned int pipe, static u64 drm_vblank_count(struct drm_device *dev, unsigned int pipe) { struct drm_vblank_crtc *vblank = &dev->vblank[pipe];
u64 count;
if (WARN_ON(pipe >= dev->num_crtcs)) return 0;
- return vblank->count;
- count = atomic64_read(&vblank->count);
- /*
* This read barrier corresponds to the implicit write barrier of the
* write seqlock in store_vblank(). Note that this is the only place
* where we need an explicit barrier, since all other access goes
* through drm_vblank_count_and_time(), which already has the required
* read barrier curtesy of the read seqlock.
*/
- smp_rmb();
I think I did not get all the idea behind the smp_rmb() in this function. FWIU, smp_xxx are used for preventing race conditions in a multiprocessor system, right? In this sense, I can presume that this change can bring benefits for VKMS or any other virtual driver; on the other hand, this will not bring any advantage on real drivers like i915 and amdgpu since these devices are not related with smp stuff, right?
smp or not smp is about the cpu your driver is running on, not anything to do with the device hardware itself. And nowadays there's simply no single-threaded processors anymore, everything has at least 2 cores (even the tiniest soc). So yeah, this matters for everyone.
smp_* functions only get compiled out to nothing if you have CONFIG_UP (which means only 1 cpu core with only 1 SMT thread is supported).
And yeah correctly placing smp barriers is Real Hard Stuff (tm). -Daniel
Thanks
- return count;
}
/** @@ -764,6 +777,14 @@ drm_get_last_vbltimestamp(struct drm_device *dev, unsigned int pipe,
- vblank interrupt (since it only reports the software vblank counter), see
- drm_crtc_accurate_vblank_count() for such use-cases.
- Note that for a given vblank counter value drm_crtc_handle_vblank()
- and drm_crtc_vblank_count() or drm_crtc_vblank_count_and_time()
- provide a barrier: Any writes done before calling
- drm_crtc_handle_vblank() will be visible to callers of the later
- functions, iff the vblank count is the same or a later one.
- See also &drm_vblank_crtc.count.
*/
- Returns:
- The software vblank counter.
@@ -801,7 +822,7 @@ static u64 drm_vblank_count_and_time(struct drm_device *dev, unsigned int pipe,
do { seq = read_seqbegin(&vblank->seqlock);
vblank_count = vblank->count;
*vblanktime = vblank->time; } while (read_seqretry(&vblank->seqlock, seq));vblank_count = atomic64_read(&vblank->count);
@@ -818,6 +839,14 @@ static u64 drm_vblank_count_and_time(struct drm_device *dev, unsigned int pipe,
- vblank events since the system was booted, including lost events due to
- modesetting activity. Returns corresponding system timestamp of the time
- of the vblank interval that corresponds to the current vblank counter value.
- Note that for a given vblank counter value drm_crtc_handle_vblank()
- and drm_crtc_vblank_count() or drm_crtc_vblank_count_and_time()
- provide a barrier: Any writes done before calling
- drm_crtc_handle_vblank() will be visible to callers of the later
- functions, iff the vblank count is the same or a later one.
*/
- See also &drm_vblank_crtc.count.
u64 drm_crtc_vblank_count_and_time(struct drm_crtc *crtc, ktime_t *vblanktime) @@ -1791,6 +1820,14 @@ EXPORT_SYMBOL(drm_handle_vblank);
- This is the native KMS version of drm_handle_vblank().
- Note that for a given vblank counter value drm_crtc_handle_vblank()
- and drm_crtc_vblank_count() or drm_crtc_vblank_count_and_time()
- provide a barrier: Any writes done before calling
- drm_crtc_handle_vblank() will be visible to callers of the later
- functions, iff the vblank count is the same or a later one.
- See also &drm_vblank_crtc.count.
*/
- Returns:
- True if the event was successfully handled, false on failure.
diff --git a/include/drm/drm_vblank.h b/include/drm/drm_vblank.h index 9fe4ba8bc622..c16c44052b3d 100644 --- a/include/drm/drm_vblank.h +++ b/include/drm/drm_vblank.h @@ -109,9 +109,20 @@ struct drm_vblank_crtc { seqlock_t seqlock;
/**
* @count: Current software vblank counter.
* @count:
*
* Current software vblank counter.
*
* Note that for a given vblank counter value drm_crtc_handle_vblank()
* and drm_crtc_vblank_count() or drm_crtc_vblank_count_and_time()
* provide a barrier: Any writes done before calling
* drm_crtc_handle_vblank() will be visible to callers of the later
* functions, iff the vblank count is the same or a later one.
*
* IMPORTANT: This guarantee requires barriers, therefor never access
*/* this field directly. Use drm_crtc_vblank_count() instead.
- u64 count;
- atomic64_t count; /**
*/
- @time: Vblank timestamp corresponding to @count.
-- 2.22.0
-- Rodrigo Siqueira Software Engineer, Advanced Micro Devices (AMD) https://siqueira.tech
Intel-gfx mailing list Intel-gfx@lists.freedesktop.org https://lists.freedesktop.org/mailman/listinfo/intel-gfx
-- Daniel Vetter Software Engineer, Intel Corporation http://blog.ffwll.ch
-- Rodrigo Siqueira Software Engineer, Advanced Micro Devices (AMD) https://siqueira.tech
dri-devel@lists.freedesktop.org