Skip to content

Commit

Permalink
FROMGIT: drm: Add DRM_MODESET_LOCK_BEGIN/END helpers
Browse files Browse the repository at this point in the history
This patch adds a couple of helpers to remove the boilerplate involved
in grabbing all of the modeset locks.

I've also converted the obvious cases in drm core to use the helpers.

The only remaining instance of drm_modeset_lock_all_ctx() is in
drm_framebuffer. It's complicated by the state clear that occurs on
deadlock. ATM, there's no way to inject code in the deadlock path with
the helpers, so it's unfit for conversion.

Changes in v2:
- Relocate ret argument to the end of the list (Daniel)
- Incorporate Daniel's doc suggestions (Daniel)

Suggested-by: Daniel Vetter <daniel@ffwll.ch>
Reviewed-by: Daniel Vetter <daniel@ffwll.ch>
Signed-off-by: Sean Paul <seanpaul@chromium.org>
Link: https://patchwork.freedesktop.org/patch/msgid/20181129150423.239081-4-sean@poorly.run
(cherry picked from commit b7ea04d)
(am from git://anongit.freedesktop.org/drm/drm)

BUG=None
TEST=Tested on cheza, boots and display comes up

Signed-off-by: Sean Paul <seanpaul@chromium.org>
Change-Id: I1dc26668c997b1cba922bd665608f420562d61d9
Reviewed-on: https://chromium-review.googlesource.com/1439575
Reviewed-by: Stéphane Marchesin <marcheu@chromium.org>
  • Loading branch information
atseanpaul authored and chrome-bot committed Jan 30, 2019
1 parent 681f9b7 commit 1552942
Show file tree
Hide file tree
Showing 6 changed files with 83 additions and 78 deletions.
51 changes: 10 additions & 41 deletions drivers/gpu/drm/drm_atomic_helper.c
Expand Up @@ -3089,23 +3089,13 @@ void drm_atomic_helper_shutdown(struct drm_device *dev)
struct drm_modeset_acquire_ctx ctx;
int ret;

drm_modeset_acquire_init(&ctx, 0);
while (1) {
ret = drm_modeset_lock_all_ctx(dev, &ctx);
if (!ret)
ret = __drm_atomic_helper_disable_all(dev, &ctx, true);

if (ret != -EDEADLK)
break;

drm_modeset_backoff(&ctx);
}
DRM_MODESET_LOCK_ALL_BEGIN(dev, ctx, 0, ret);

ret = __drm_atomic_helper_disable_all(dev, &ctx, true);
if (ret)
DRM_ERROR("Disabling all crtc's during unload failed with %i\n", ret);

drm_modeset_drop_locks(&ctx);
drm_modeset_acquire_fini(&ctx);
DRM_MODESET_LOCK_ALL_END(ctx, ret);
}
EXPORT_SYMBOL(drm_atomic_helper_shutdown);

Expand Down Expand Up @@ -3140,14 +3130,7 @@ struct drm_atomic_state *drm_atomic_helper_suspend(struct drm_device *dev)
struct drm_atomic_state *state;
int err;

drm_modeset_acquire_init(&ctx, 0);

retry:
err = drm_modeset_lock_all_ctx(dev, &ctx);
if (err < 0) {
state = ERR_PTR(err);
goto unlock;
}
DRM_MODESET_LOCK_ALL_BEGIN(dev, ctx, 0, err);

state = drm_atomic_helper_duplicate_state(dev, &ctx);
if (IS_ERR(state))
Expand All @@ -3161,13 +3144,10 @@ struct drm_atomic_state *drm_atomic_helper_suspend(struct drm_device *dev)
}

unlock:
if (PTR_ERR(state) == -EDEADLK) {
drm_modeset_backoff(&ctx);
goto retry;
}
DRM_MODESET_LOCK_ALL_END(ctx, err);
if (err)
return ERR_PTR(err);

drm_modeset_drop_locks(&ctx);
drm_modeset_acquire_fini(&ctx);
return state;
}
EXPORT_SYMBOL(drm_atomic_helper_suspend);
Expand Down Expand Up @@ -3241,22 +3221,11 @@ int drm_atomic_helper_resume(struct drm_device *dev,

drm_mode_config_reset(dev);

drm_modeset_acquire_init(&ctx, 0);
while (1) {
err = drm_modeset_lock_all_ctx(dev, &ctx);
if (err)
goto out;
DRM_MODESET_LOCK_ALL_BEGIN(dev, ctx, 0, err);

err = drm_atomic_helper_commit_duplicated_state(state, &ctx);
out:
if (err != -EDEADLK)
break;

drm_modeset_backoff(&ctx);
}
err = drm_atomic_helper_commit_duplicated_state(state, &ctx);

drm_modeset_drop_locks(&ctx);
drm_modeset_acquire_fini(&ctx);
DRM_MODESET_LOCK_ALL_END(ctx, err);
drm_atomic_state_put(state);

return err;
Expand Down
14 changes: 2 additions & 12 deletions drivers/gpu/drm/drm_color_mgmt.c
Expand Up @@ -255,11 +255,7 @@ int drm_mode_gamma_set_ioctl(struct drm_device *dev,
if (crtc_lut->gamma_size != crtc->gamma_size)
return -EINVAL;

drm_modeset_acquire_init(&ctx, 0);
retry:
ret = drm_modeset_lock_all_ctx(dev, &ctx);
if (ret)
goto out;
DRM_MODESET_LOCK_ALL_BEGIN(dev, ctx, 0, ret);

size = crtc_lut->gamma_size * (sizeof(uint16_t));
r_base = crtc->gamma_store;
Expand All @@ -284,13 +280,7 @@ int drm_mode_gamma_set_ioctl(struct drm_device *dev,
crtc->gamma_size, &ctx);

out:
if (ret == -EDEADLK) {
drm_modeset_backoff(&ctx);
goto retry;
}
drm_modeset_drop_locks(&ctx);
drm_modeset_acquire_fini(&ctx);

DRM_MODESET_LOCK_ALL_END(ctx, ret);
return ret;

}
Expand Down
15 changes: 3 additions & 12 deletions drivers/gpu/drm/drm_crtc.c
Expand Up @@ -599,11 +599,8 @@ int drm_mode_setcrtc(struct drm_device *dev, void *data,
plane = crtc->primary;

mutex_lock(&crtc->dev->mode_config.mutex);
drm_modeset_acquire_init(&ctx, DRM_MODESET_ACQUIRE_INTERRUPTIBLE);
retry:
ret = drm_modeset_lock_all_ctx(crtc->dev, &ctx);
if (ret)
goto out;
DRM_MODESET_LOCK_ALL_BEGIN(dev, ctx,
DRM_MODESET_ACQUIRE_INTERRUPTIBLE, ret);

if (crtc_req->mode_valid) {
/* If we have a mode we need a framebuffer. */
Expand Down Expand Up @@ -768,13 +765,7 @@ int drm_mode_setcrtc(struct drm_device *dev, void *data,
fb = NULL;
mode = NULL;

if (ret == -EDEADLK) {
ret = drm_modeset_backoff(&ctx);
if (!ret)
goto retry;
}
drm_modeset_drop_locks(&ctx);
drm_modeset_acquire_fini(&ctx);
DRM_MODESET_LOCK_ALL_END(ctx, ret);
mutex_unlock(&crtc->dev->mode_config.mutex);

return ret;
Expand Down
6 changes: 6 additions & 0 deletions drivers/gpu/drm/drm_modeset_lock.c
Expand Up @@ -56,6 +56,10 @@
* drm_modeset_drop_locks(ctx);
* drm_modeset_acquire_fini(ctx);
*
* For convenience this control flow is implemented in
* DRM_MODESET_LOCK_ALL_BEGIN() and DRM_MODESET_LOCK_ALL_END() for the case
* where all modeset locks need to be taken through drm_modeset_lock_all_ctx().
*
* If all that is needed is a single modeset lock, then the &struct
* drm_modeset_acquire_ctx is not needed and the locking can be simplified
* by passing a NULL instead of ctx in the drm_modeset_lock() call or
Expand Down Expand Up @@ -383,6 +387,8 @@ EXPORT_SYMBOL(drm_modeset_unlock);
* Locks acquired with this function should be released by calling the
* drm_modeset_drop_locks() function on @ctx.
*
* See also: DRM_MODESET_LOCK_ALL_BEGIN() and DRM_MODESET_LOCK_ALL_END()
*
* Returns: 0 on success or a negative error-code on failure.
*/
int drm_modeset_lock_all_ctx(struct drm_device *dev,
Expand Down
16 changes: 3 additions & 13 deletions drivers/gpu/drm/drm_plane.c
Expand Up @@ -744,11 +744,8 @@ static int setplane_internal(struct drm_plane *plane,
struct drm_modeset_acquire_ctx ctx;
int ret;

drm_modeset_acquire_init(&ctx, DRM_MODESET_ACQUIRE_INTERRUPTIBLE);
retry:
ret = drm_modeset_lock_all_ctx(plane->dev, &ctx);
if (ret)
goto fail;
DRM_MODESET_LOCK_ALL_BEGIN(plane->dev, ctx,
DRM_MODESET_ACQUIRE_INTERRUPTIBLE, ret);

if (drm_drv_uses_atomic_modeset(plane->dev))
ret = __setplane_atomic(plane, crtc, fb,
Expand All @@ -759,14 +756,7 @@ static int setplane_internal(struct drm_plane *plane,
crtc_x, crtc_y, crtc_w, crtc_h,
src_x, src_y, src_w, src_h, &ctx);

fail:
if (ret == -EDEADLK) {
ret = drm_modeset_backoff(&ctx);
if (!ret)
goto retry;
}
drm_modeset_drop_locks(&ctx);
drm_modeset_acquire_fini(&ctx);
DRM_MODESET_LOCK_ALL_END(ctx, ret);

return ret;
}
Expand Down
59 changes: 59 additions & 0 deletions include/drm/drm_modeset_lock.h
Expand Up @@ -130,4 +130,63 @@ void drm_warn_on_modeset_not_all_locked(struct drm_device *dev);
int drm_modeset_lock_all_ctx(struct drm_device *dev,
struct drm_modeset_acquire_ctx *ctx);

/**
* DRM_MODESET_LOCK_ALL_BEGIN - Helper to acquire modeset locks
* @dev: drm device
* @ctx: local modeset acquire context, will be dereferenced
* @flags: DRM_MODESET_ACQUIRE_* flags to pass to drm_modeset_acquire_init()
* @ret: local ret/err/etc variable to track error status
*
* Use these macros to simplify grabbing all modeset locks using a local
* context. This has the advantage of reducing boilerplate, but also properly
* checking return values where appropriate.
*
* Any code run between BEGIN and END will be holding the modeset locks.
*
* This must be paired with DRM_MODESET_LOCK_ALL_END(). We will jump back and
* forth between the labels on deadlock and error conditions.
*
* Drivers can acquire additional modeset locks. If any lock acquisition
* fails, the control flow needs to jump to DRM_MODESET_LOCK_ALL_END() with
* the @ret parameter containing the return value of drm_modeset_lock().
*
* Returns:
* The only possible value of ret immediately after DRM_MODESET_LOCK_ALL_BEGIN()
* is 0, so no error checking is necessary
*/
#define DRM_MODESET_LOCK_ALL_BEGIN(dev, ctx, flags, ret) \
drm_modeset_acquire_init(&ctx, flags); \
modeset_lock_retry: \
ret = drm_modeset_lock_all_ctx(dev, &ctx); \
if (ret) \
goto modeset_lock_fail;

/**
* DRM_MODESET_LOCK_ALL_END - Helper to release and cleanup modeset locks
* @ctx: local modeset acquire context, will be dereferenced
* @ret: local ret/err/etc variable to track error status
*
* The other side of DRM_MODESET_LOCK_ALL_BEGIN(). It will bounce back to BEGIN
* if ret is -EDEADLK.
*
* It's important that you use the same ret variable for begin and end so
* deadlock conditions are properly handled.
*
* Returns:
* ret will be untouched unless it is -EDEADLK on entry. That means that if you
* successfully acquire the locks, ret will be whatever your code sets it to. If
* there is a deadlock or other failure with acquire or backoff, ret will be set
* to that failure. In both of these cases the code between BEGIN/END will not
* be run, so the failure will reflect the inability to grab the locks.
*/
#define DRM_MODESET_LOCK_ALL_END(ctx, ret) \
modeset_lock_fail: \
if (ret == -EDEADLK) { \
ret = drm_modeset_backoff(&ctx); \
if (!ret) \
goto modeset_lock_retry; \
} \
drm_modeset_drop_locks(&ctx); \
drm_modeset_acquire_fini(&ctx);

#endif /* DRM_MODESET_LOCK_H_ */

0 comments on commit 1552942

Please sign in to comment.