diff mbox

[03/17] drm: convert crtc and mode_config to ww_mutex

Message ID 1400956226-28053-4-git-send-email-robdclark@gmail.com (mailing list archive)
State New, archived
Headers show

Commit Message

Rob Clark May 24, 2014, 6:30 p.m. UTC
For atomic, it will be quite convenient to not have to care so much
about locking order.  And 'state' gives us a convenient place to stash a
ww_ctx for any sort of update that needs to grab multiple crtc locks.

Because we will want to eventually make locking even more fine grained
(giving locks to planes, connectors, etc), split out drm_modeset_lock
so that the atomic state won't eventually have to keep separate lists of
locked planes/crtcs/etc.

The state keeps track of which locks it has aquired, and for the benefit
of NONBLOCK operations, supports transfering "locked" resources to
driver worker/thread to complete the asynchronous state change.

Signed-off-by: Rob Clark <robdclark@gmail.com>
---
 drivers/gpu/drm/Makefile                  |   2 +-
 drivers/gpu/drm/drm_atomic.c              | 140 ++++++++++++++++++++++++--
 drivers/gpu/drm/drm_crtc.c                |  88 ++++++++++++-----
 drivers/gpu/drm/drm_crtc_helper.c         |   4 +-
 drivers/gpu/drm/drm_fb_cma_helper.c       |   6 +-
 drivers/gpu/drm/drm_fb_helper.c           |  18 ++--
 drivers/gpu/drm/drm_modes.c               |   4 +-
 drivers/gpu/drm/drm_modeset_lock.c        | 159 ++++++++++++++++++++++++++++++
 drivers/gpu/drm/drm_probe_helper.c        |  10 +-
 drivers/gpu/drm/drm_sysfs.c               |   4 +-
 drivers/gpu/drm/exynos/exynos_drm_fbdev.c |   4 +-
 drivers/gpu/drm/gma500/cdv_intel_lvds.c   |   6 +-
 drivers/gpu/drm/gma500/oaktrail_lvds.c    |   6 +-
 drivers/gpu/drm/gma500/psb_intel_lvds.c   |   6 +-
 drivers/gpu/drm/i915/i915_debugfs.c       |  12 +--
 drivers/gpu/drm/i915/i915_drv.c           |   8 +-
 drivers/gpu/drm/i915/i915_irq.c           |   6 +-
 drivers/gpu/drm/i915/intel_display.c      |  20 ++--
 drivers/gpu/drm/i915/intel_dp.c           |  18 ++--
 drivers/gpu/drm/i915/intel_lvds.c         |   6 +-
 drivers/gpu/drm/i915/intel_opregion.c     |   4 +-
 drivers/gpu/drm/i915/intel_overlay.c      |   4 +-
 drivers/gpu/drm/i915/intel_panel.c        |   8 +-
 drivers/gpu/drm/i915/intel_sprite.c       |   2 +-
 drivers/gpu/drm/msm/mdp/mdp4/mdp4_crtc.c  |   4 +-
 drivers/gpu/drm/msm/mdp/mdp5/mdp5_crtc.c  |   4 +-
 drivers/gpu/drm/omapdrm/omap_crtc.c       |  10 +-
 drivers/gpu/drm/omapdrm/omap_plane.c      |   4 +-
 drivers/gpu/drm/tilcdc/tilcdc_crtc.c      |   4 +-
 drivers/gpu/drm/vmwgfx/vmwgfx_kms.c       |  22 ++---
 include/drm/drmP.h                        |   5 -
 include/drm/drm_atomic.h                  |   7 ++
 include/drm/drm_crtc.h                    |  19 ++--
 include/drm/drm_modeset_lock.h            | 130 ++++++++++++++++++++++++
 include/uapi/drm/drm_mode.h               |   3 +
 35 files changed, 614 insertions(+), 143 deletions(-)
 create mode 100644 drivers/gpu/drm/drm_modeset_lock.c
 create mode 100644 include/drm/drm_modeset_lock.h

Comments

Daniel Vetter May 25, 2014, 10:10 p.m. UTC | #1
On Sat, May 24, 2014 at 8:30 PM, Rob Clark <robdclark@gmail.com> wrote:
> @@ -8002,7 +8002,7 @@ bool intel_get_load_detect_pipe(struct drm_connector *connector,
>         if (encoder->crtc) {
>                 crtc = encoder->crtc;
>
> -               mutex_lock(&crtc->mutex);
> +               drm_modeset_lock(&crtc->mutex, NULL);


This is pretty much the reason why I think switching the
mode_config.mutex to a ww_mutex is a bad idea: This call here nests
within the mode_config.mutex and so must be acquired. Wiring the
acquire context through everything is going to be fairly horrible,
especially since you must be able to bail out when trying to lock with
an axquire context.

My original design behind the crtc->mutex and mode_config.mutex split
was that as long as the connector->crtc links didn't change you can
get away with the crtc lock. setplane made a bit a mess out of this,
but strictly speaking as long as you acquire all crtc locks involved
in a potential plane switch (which ww_mtuxes can do) it'll be fine.
Since noticing whether any connector properties change should be
doable upfront I think we should try _really_ hard to keep the
mode_config.mutex a plain mutex which wraps all the more fine-grained
locks and is a catch-all for everything else but crtcs/planes.
-Daniel
Rob Clark May 25, 2014, 11:16 p.m. UTC | #2
On Sun, May 25, 2014 at 6:10 PM, Daniel Vetter <daniel@ffwll.ch> wrote:
> On Sat, May 24, 2014 at 8:30 PM, Rob Clark <robdclark@gmail.com> wrote:
>> @@ -8002,7 +8002,7 @@ bool intel_get_load_detect_pipe(struct drm_connector *connector,
>>         if (encoder->crtc) {
>>                 crtc = encoder->crtc;
>>
>> -               mutex_lock(&crtc->mutex);
>> +               drm_modeset_lock(&crtc->mutex, NULL);
>
>
> This is pretty much the reason why I think switching the
> mode_config.mutex to a ww_mutex is a bad idea: This call here nests
> within the mode_config.mutex and so must be acquired. Wiring the
> acquire context through everything is going to be fairly horrible,
> especially since you must be able to bail out when trying to lock with
> an axquire context.

which is the call-path to here from mode_config.mutex?  Is it possible
to just move the locking to a higher level for a
drm_modeset_lock_all()?

> My original design behind the crtc->mutex and mode_config.mutex split
> was that as long as the connector->crtc links didn't change you can
> get away with the crtc lock. setplane made a bit a mess out of this,
> but strictly speaking as long as you acquire all crtc locks involved
> in a potential plane switch (which ww_mtuxes can do) it'll be fine.
> Since noticing whether any connector properties change should be
> doable upfront I think we should try _really_ hard to keep the
> mode_config.mutex a plain mutex which wraps all the more fine-grained
> locks and is a catch-all for everything else but crtcs/planes.

That is basically how it was in one of the earlier iterations of
atomic.. but didn't hold mode_config.mutex in a lot of places where it
was previously held.. and while I do want to make locking more fine
grained I didn't want to try and do it at the same time as landing all
these other changes.

BR,
-R

> -Daniel
> --
> Daniel Vetter
> Software Engineer, Intel Corporation
> +41 (0) 79 365 57 48 - http://blog.ffwll.ch
Daniel Vetter May 26, 2014, 8:23 a.m. UTC | #3
On Sun, May 25, 2014 at 07:16:43PM -0400, Rob Clark wrote:
> On Sun, May 25, 2014 at 6:10 PM, Daniel Vetter <daniel@ffwll.ch> wrote:
> > On Sat, May 24, 2014 at 8:30 PM, Rob Clark <robdclark@gmail.com> wrote:
> >> @@ -8002,7 +8002,7 @@ bool intel_get_load_detect_pipe(struct drm_connector *connector,
> >>         if (encoder->crtc) {
> >>                 crtc = encoder->crtc;
> >>
> >> -               mutex_lock(&crtc->mutex);
> >> +               drm_modeset_lock(&crtc->mutex, NULL);
> >
> >
> > This is pretty much the reason why I think switching the
> > mode_config.mutex to a ww_mutex is a bad idea: This call here nests
> > within the mode_config.mutex and so must be acquired. Wiring the
> > acquire context through everything is going to be fairly horrible,
> > especially since you must be able to bail out when trying to lock with
> > an axquire context.
> 
> which is the call-path to here from mode_config.mutex?  Is it possible
> to just move the locking to a higher level for a
> drm_modeset_lock_all()?

Connector probing. And the entire point of crtc locks was to _not_ block
all screen updates while we poke for a new edid or do load balancing. If
you want to test this you need a gen3/4 with tv-out (native, not through
sdvo) or a gen2 or i915g/gm with vga.

> > My original design behind the crtc->mutex and mode_config.mutex split
> > was that as long as the connector->crtc links didn't change you can
> > get away with the crtc lock. setplane made a bit a mess out of this,
> > but strictly speaking as long as you acquire all crtc locks involved
> > in a potential plane switch (which ww_mtuxes can do) it'll be fine.
> > Since noticing whether any connector properties change should be
> > doable upfront I think we should try _really_ hard to keep the
> > mode_config.mutex a plain mutex which wraps all the more fine-grained
> > locks and is a catch-all for everything else but crtcs/planes.
> 
> That is basically how it was in one of the earlier iterations of
> atomic.. but didn't hold mode_config.mutex in a lot of places where it
> was previously held.. and while I do want to make locking more fine
> grained I didn't want to try and do it at the same time as landing all
> these other changes.

Hm, maybe we should land the locking stuff first? I.e. just convert
crtc->mutex into a ww_mutex, and then add more fine-grained locking to
e.g. setplane by only grabbing the locks of the involved crtcs with the
w/w logic. We might need an additional plane mutex to make it work. Iirc
Ville had some patches for just this.

I'm arguing this since locking at the current interface I have a really
hard time seeing how we're going to implement this in i915. Still reading
around though.
-Daniel
Rob Clark May 26, 2014, 11:56 a.m. UTC | #4
On Mon, May 26, 2014 at 4:23 AM, Daniel Vetter <daniel@ffwll.ch> wrote:
> On Sun, May 25, 2014 at 07:16:43PM -0400, Rob Clark wrote:
>> On Sun, May 25, 2014 at 6:10 PM, Daniel Vetter <daniel@ffwll.ch> wrote:
>> > On Sat, May 24, 2014 at 8:30 PM, Rob Clark <robdclark@gmail.com> wrote:
>> >> @@ -8002,7 +8002,7 @@ bool intel_get_load_detect_pipe(struct drm_connector *connector,
>> >>         if (encoder->crtc) {
>> >>                 crtc = encoder->crtc;
>> >>
>> >> -               mutex_lock(&crtc->mutex);
>> >> +               drm_modeset_lock(&crtc->mutex, NULL);
>> >
>> >
>> > This is pretty much the reason why I think switching the
>> > mode_config.mutex to a ww_mutex is a bad idea: This call here nests
>> > within the mode_config.mutex and so must be acquired. Wiring the
>> > acquire context through everything is going to be fairly horrible,
>> > especially since you must be able to bail out when trying to lock with
>> > an axquire context.
>>
>> which is the call-path to here from mode_config.mutex?  Is it possible
>> to just move the locking to a higher level for a
>> drm_modeset_lock_all()?
>
> Connector probing. And the entire point of crtc locks was to _not_ block
> all screen updates while we poke for a new edid or do load balancing. If
> you want to test this you need a gen3/4 with tv-out (native, not through
> sdvo) or a gen2 or i915g/gm with vga.


hmm, I guess I'm still not quite seeing the issue.  For non-atomic
paths, we are grabbing mode_config and/or crtc mutex as bare mutexes
in same spots as we did before.  So if it worked before without
nested_lock stuff it should still work now.

BR,
-R
Daniel Vetter May 26, 2014, 2:35 p.m. UTC | #5
On Mon, May 26, 2014 at 07:56:50AM -0400, Rob Clark wrote:
> On Mon, May 26, 2014 at 4:23 AM, Daniel Vetter <daniel@ffwll.ch> wrote:
> > On Sun, May 25, 2014 at 07:16:43PM -0400, Rob Clark wrote:
> >> On Sun, May 25, 2014 at 6:10 PM, Daniel Vetter <daniel@ffwll.ch> wrote:
> >> > On Sat, May 24, 2014 at 8:30 PM, Rob Clark <robdclark@gmail.com> wrote:
> >> >> @@ -8002,7 +8002,7 @@ bool intel_get_load_detect_pipe(struct drm_connector *connector,
> >> >>         if (encoder->crtc) {
> >> >>                 crtc = encoder->crtc;
> >> >>
> >> >> -               mutex_lock(&crtc->mutex);
> >> >> +               drm_modeset_lock(&crtc->mutex, NULL);
> >> >
> >> >
> >> > This is pretty much the reason why I think switching the
> >> > mode_config.mutex to a ww_mutex is a bad idea: This call here nests
> >> > within the mode_config.mutex and so must be acquired. Wiring the
> >> > acquire context through everything is going to be fairly horrible,
> >> > especially since you must be able to bail out when trying to lock with
> >> > an axquire context.
> >>
> >> which is the call-path to here from mode_config.mutex?  Is it possible
> >> to just move the locking to a higher level for a
> >> drm_modeset_lock_all()?
> >
> > Connector probing. And the entire point of crtc locks was to _not_ block
> > all screen updates while we poke for a new edid or do load balancing. If
> > you want to test this you need a gen3/4 with tv-out (native, not through
> > sdvo) or a gen2 or i915g/gm with vga.
> 
> 
> hmm, I guess I'm still not quite seeing the issue.  For non-atomic
> paths, we are grabbing mode_config and/or crtc mutex as bare mutexes
> in same spots as we did before.  So if it worked before without
> nested_lock stuff it should still work now.

Thread A is doing output probing.

				Thread B is doing atomic modeset

Grabs mode_config.mutex

				Grabs crtc_A->ww_mutex

Tries to grab crtc_A->ww_mutex,
blocks since normal ww_mutex_lock

				Tries to grab mode_config.mutex with ww
				acuiquire context, blocks since current
				holder hasn't acquired the mutex with a ww
				ticket

-> Deadlock.

You really can't mix lock nesting with w/w and lock nesting with a static
hierarchy. It's all or nothing.
-Daniel
Daniel Vetter May 26, 2014, 2:36 p.m. UTC | #6
On Mon, May 26, 2014 at 04:35:04PM +0200, Daniel Vetter wrote:
> On Mon, May 26, 2014 at 07:56:50AM -0400, Rob Clark wrote:
> > On Mon, May 26, 2014 at 4:23 AM, Daniel Vetter <daniel@ffwll.ch> wrote:
> > > On Sun, May 25, 2014 at 07:16:43PM -0400, Rob Clark wrote:
> > >> On Sun, May 25, 2014 at 6:10 PM, Daniel Vetter <daniel@ffwll.ch> wrote:
> > >> > On Sat, May 24, 2014 at 8:30 PM, Rob Clark <robdclark@gmail.com> wrote:
> > >> >> @@ -8002,7 +8002,7 @@ bool intel_get_load_detect_pipe(struct drm_connector *connector,
> > >> >>         if (encoder->crtc) {
> > >> >>                 crtc = encoder->crtc;
> > >> >>
> > >> >> -               mutex_lock(&crtc->mutex);
> > >> >> +               drm_modeset_lock(&crtc->mutex, NULL);
> > >> >
> > >> >
> > >> > This is pretty much the reason why I think switching the
> > >> > mode_config.mutex to a ww_mutex is a bad idea: This call here nests
> > >> > within the mode_config.mutex and so must be acquired. Wiring the
> > >> > acquire context through everything is going to be fairly horrible,
> > >> > especially since you must be able to bail out when trying to lock with
> > >> > an axquire context.
> > >>
> > >> which is the call-path to here from mode_config.mutex?  Is it possible
> > >> to just move the locking to a higher level for a
> > >> drm_modeset_lock_all()?
> > >
> > > Connector probing. And the entire point of crtc locks was to _not_ block
> > > all screen updates while we poke for a new edid or do load balancing. If
> > > you want to test this you need a gen3/4 with tv-out (native, not through
> > > sdvo) or a gen2 or i915g/gm with vga.
> > 
> > 
> > hmm, I guess I'm still not quite seeing the issue.  For non-atomic
> > paths, we are grabbing mode_config and/or crtc mutex as bare mutexes
> > in same spots as we did before.  So if it worked before without
> > nested_lock stuff it should still work now.
> 
> Thread A is doing output probing.
> 
> 				Thread B is doing atomic modeset
> 
> Grabs mode_config.mutex
> 
> 				Grabs crtc_A->ww_mutex
> 
> Tries to grab crtc_A->ww_mutex,
> blocks since normal ww_mutex_lock
.. blocks since normal ww_mutex_lock without acquire ticket.

Otherwise it's a bit unclear.
-Daniel


> 
> 				Tries to grab mode_config.mutex with ww
> 				acuiquire context, blocks since current
> 				holder hasn't acquired the mutex with a ww
> 				ticket
> 
> -> Deadlock.
> 
> You really can't mix lock nesting with w/w and lock nesting with a static
> hierarchy. It's all or nothing.
> -Daniel
> -- 
> Daniel Vetter
> Software Engineer, Intel Corporation
> +41 (0) 79 365 57 48 - http://blog.ffwll.ch
Rob Clark May 26, 2014, 3:04 p.m. UTC | #7
On Mon, May 26, 2014 at 10:35 AM, Daniel Vetter <daniel@ffwll.ch> wrote:
> On Mon, May 26, 2014 at 07:56:50AM -0400, Rob Clark wrote:
>> On Mon, May 26, 2014 at 4:23 AM, Daniel Vetter <daniel@ffwll.ch> wrote:
>> > On Sun, May 25, 2014 at 07:16:43PM -0400, Rob Clark wrote:
>> >> On Sun, May 25, 2014 at 6:10 PM, Daniel Vetter <daniel@ffwll.ch> wrote:
>> >> > On Sat, May 24, 2014 at 8:30 PM, Rob Clark <robdclark@gmail.com> wrote:
>> >> >> @@ -8002,7 +8002,7 @@ bool intel_get_load_detect_pipe(struct drm_connector *connector,
>> >> >>         if (encoder->crtc) {
>> >> >>                 crtc = encoder->crtc;
>> >> >>
>> >> >> -               mutex_lock(&crtc->mutex);
>> >> >> +               drm_modeset_lock(&crtc->mutex, NULL);
>> >> >
>> >> >
>> >> > This is pretty much the reason why I think switching the
>> >> > mode_config.mutex to a ww_mutex is a bad idea: This call here nests
>> >> > within the mode_config.mutex and so must be acquired. Wiring the
>> >> > acquire context through everything is going to be fairly horrible,
>> >> > especially since you must be able to bail out when trying to lock with
>> >> > an axquire context.
>> >>
>> >> which is the call-path to here from mode_config.mutex?  Is it possible
>> >> to just move the locking to a higher level for a
>> >> drm_modeset_lock_all()?
>> >
>> > Connector probing. And the entire point of crtc locks was to _not_ block
>> > all screen updates while we poke for a new edid or do load balancing. If
>> > you want to test this you need a gen3/4 with tv-out (native, not through
>> > sdvo) or a gen2 or i915g/gm with vga.
>>
>>
>> hmm, I guess I'm still not quite seeing the issue.  For non-atomic
>> paths, we are grabbing mode_config and/or crtc mutex as bare mutexes
>> in same spots as we did before.  So if it worked before without
>> nested_lock stuff it should still work now.
>
> Thread A is doing output probing.
>
>                                 Thread B is doing atomic modeset
>
> Grabs mode_config.mutex
>
>                                 Grabs crtc_A->ww_mutex
>
> Tries to grab crtc_A->ww_mutex,
> blocks since normal ww_mutex_lock
>
>                                 Tries to grab mode_config.mutex with ww
>                                 acuiquire context, blocks since current
>                                 holder hasn't acquired the mutex with a ww
>                                 ticket


hmm, ok, I had thought this case, thread B would get -EDEADLK because
lock was held, and not his acquire ctx.  If that is not the case, then
I would propose this:

All places doing things the old way, must grab mode_config.mutex first
currently.  And we use mode_config.mutex to protect
mode_config.acquire_ctx.  So all the lower spots grabbing individual
crtc mutexes can safely use mode_config.acquire_ctx.

Then the only headache is propagating -EDEADLK up the call stack.  If
we are lucky, the all already propagate -EINTR, etc.

BR,
-R

>
> -> Deadlock.
>
> You really can't mix lock nesting with w/w and lock nesting with a static
> hierarchy. It's all or nothing.
> -Daniel
> --
> Daniel Vetter
> Software Engineer, Intel Corporation
> +41 (0) 79 365 57 48 - http://blog.ffwll.ch
Daniel Vetter May 26, 2014, 3:07 p.m. UTC | #8
On Mon, May 26, 2014 at 5:04 PM, Rob Clark <robdclark@gmail.com> wrote:
> hmm, ok, I had thought this case, thread B would get -EDEADLK because
> lock was held, and not his acquire ctx.  If that is not the case, then
> I would propose this:
>
> All places doing things the old way, must grab mode_config.mutex first
> currently.  And we use mode_config.mutex to protect
> mode_config.acquire_ctx.  So all the lower spots grabbing individual
> crtc mutexes can safely use mode_config.acquire_ctx.
>
> Then the only headache is propagating -EDEADLK up the call stack.  If
> we are lucky, the all already propagate -EINTR, etc.

The output poll work most definitely doesn't propagate -EINTR. Like
I've said, this will be painful. And imo doing this also makes the kms
locking into quite a mess overall.
-Daniel
Rob Clark May 26, 2014, 3:20 p.m. UTC | #9
On Mon, May 26, 2014 at 11:07 AM, Daniel Vetter <daniel@ffwll.ch> wrote:
> On Mon, May 26, 2014 at 5:04 PM, Rob Clark <robdclark@gmail.com> wrote:
>> hmm, ok, I had thought this case, thread B would get -EDEADLK because
>> lock was held, and not his acquire ctx.  If that is not the case, then
>> I would propose this:
>>
>> All places doing things the old way, must grab mode_config.mutex first
>> currently.  And we use mode_config.mutex to protect
>> mode_config.acquire_ctx.  So all the lower spots grabbing individual
>> crtc mutexes can safely use mode_config.acquire_ctx.
>>
>> Then the only headache is propagating -EDEADLK up the call stack.  If
>> we are lucky, the all already propagate -EINTR, etc.
>
> The output poll work most definitely doesn't propagate -EINTR. Like
> I've said, this will be painful. And imo doing this also makes the kms
> locking into quite a mess overall.

Well, we could hold mode_config.mutex as a traditional mutex around
atomic operations.  What you loose out would be now _NONBLOCK
operations could conceivable call into driver paths without
mode_config.mutex held.  This was the advantage of converting
mode_config.mutex as well.  Granted, it is slightly theoretical
because until we expose atomic ioctl it would only apply to page_flip
(which was not holding mode_config.mutex).  And we also want to get
rid of mode_config.mutex in these paths too.

Otoh, if we want to make locking more fine grained, more use of
ww_mutex seems like the best way.  And if that means adding a return
value to a fxn here/there and propagating errors properly, maybe we
should just go ahead and do that.  It sounds like the right long term
solution anyways.

BR,
-R

> -Daniel
> --
> Daniel Vetter
> Software Engineer, Intel Corporation
> +41 (0) 79 365 57 48 - http://blog.ffwll.ch
Daniel Vetter May 26, 2014, 3:35 p.m. UTC | #10
On Mon, May 26, 2014 at 11:20:49AM -0400, Rob Clark wrote:
> On Mon, May 26, 2014 at 11:07 AM, Daniel Vetter <daniel@ffwll.ch> wrote:
> > On Mon, May 26, 2014 at 5:04 PM, Rob Clark <robdclark@gmail.com> wrote:
> >> hmm, ok, I had thought this case, thread B would get -EDEADLK because
> >> lock was held, and not his acquire ctx.  If that is not the case, then
> >> I would propose this:
> >>
> >> All places doing things the old way, must grab mode_config.mutex first
> >> currently.  And we use mode_config.mutex to protect
> >> mode_config.acquire_ctx.  So all the lower spots grabbing individual
> >> crtc mutexes can safely use mode_config.acquire_ctx.
> >>
> >> Then the only headache is propagating -EDEADLK up the call stack.  If
> >> we are lucky, the all already propagate -EINTR, etc.
> >
> > The output poll work most definitely doesn't propagate -EINTR. Like
> > I've said, this will be painful. And imo doing this also makes the kms
> > locking into quite a mess overall.
> 
> Well, we could hold mode_config.mutex as a traditional mutex around
> atomic operations.  What you loose out would be now _NONBLOCK
> operations could conceivable call into driver paths without
> mode_config.mutex held.  This was the advantage of converting
> mode_config.mutex as well.  Granted, it is slightly theoretical
> because until we expose atomic ioctl it would only apply to page_flip
> (which was not holding mode_config.mutex).  And we also want to get
> rid of mode_config.mutex in these paths too.
> 
> Otoh, if we want to make locking more fine grained, more use of
> ww_mutex seems like the best way.  And if that means adding a return
> value to a fxn here/there and propagating errors properly, maybe we
> should just go ahead and do that.  It sounds like the right long term
> solution anyways.

Yeah, I'm starting to lean towards trying to elide mode_config.mutex
completely from the atomic paths (and modesets in general). I think the
only bits we really need is adding ww mutexes to planes _and_ to
connectors. The atomic would _only_ ever acquire ww mutexes, and we would
be able to guarante that most of them are only held short times so that we
don't need to bother with trylocking them for NONBLOCK. That should simply
the atomic logic a bit I hope.

So that, and a full subsystem audit unfortunately :(
-Daniel
Rob Clark May 26, 2014, 3:49 p.m. UTC | #11
On Mon, May 26, 2014 at 11:35 AM, Daniel Vetter <daniel@ffwll.ch> wrote:
> On Mon, May 26, 2014 at 11:20:49AM -0400, Rob Clark wrote:
>> On Mon, May 26, 2014 at 11:07 AM, Daniel Vetter <daniel@ffwll.ch> wrote:
>> > On Mon, May 26, 2014 at 5:04 PM, Rob Clark <robdclark@gmail.com> wrote:
>> >> hmm, ok, I had thought this case, thread B would get -EDEADLK because
>> >> lock was held, and not his acquire ctx.  If that is not the case, then
>> >> I would propose this:
>> >>
>> >> All places doing things the old way, must grab mode_config.mutex first
>> >> currently.  And we use mode_config.mutex to protect
>> >> mode_config.acquire_ctx.  So all the lower spots grabbing individual
>> >> crtc mutexes can safely use mode_config.acquire_ctx.
>> >>
>> >> Then the only headache is propagating -EDEADLK up the call stack.  If
>> >> we are lucky, the all already propagate -EINTR, etc.
>> >
>> > The output poll work most definitely doesn't propagate -EINTR. Like
>> > I've said, this will be painful. And imo doing this also makes the kms
>> > locking into quite a mess overall.
>>
>> Well, we could hold mode_config.mutex as a traditional mutex around
>> atomic operations.  What you loose out would be now _NONBLOCK
>> operations could conceivable call into driver paths without
>> mode_config.mutex held.  This was the advantage of converting
>> mode_config.mutex as well.  Granted, it is slightly theoretical
>> because until we expose atomic ioctl it would only apply to page_flip
>> (which was not holding mode_config.mutex).  And we also want to get
>> rid of mode_config.mutex in these paths too.
>>
>> Otoh, if we want to make locking more fine grained, more use of
>> ww_mutex seems like the best way.  And if that means adding a return
>> value to a fxn here/there and propagating errors properly, maybe we
>> should just go ahead and do that.  It sounds like the right long term
>> solution anyways.
>
> Yeah, I'm starting to lean towards trying to elide mode_config.mutex
> completely from the atomic paths (and modesets in general). I think the
> only bits we really need is adding ww mutexes to planes _and_ to
> connectors. The atomic would _only_ ever acquire ww mutexes, and we would
> be able to guarante that most of them are only held short times so that we
> don't need to bother with trylocking them for NONBLOCK. That should simply
> the atomic logic a bit I hope.
>
> So that, and a full subsystem audit unfortunately :(

well, getting rid of any global lock from atomic path is, I hope, the
eventual goal.  But we are *really* late already with atomic, and too
many other things backing up on top of this.  I suppose I should have
a closer look at the detect paths, to just see how hairy the
alternative of adding error propagation would be.  Although maybe not
today (it is supposed to be a holiday here.. and I've already spent
enough of the weekend on atomic ;-))

Just to get *something* merged to unblock driver work and maybe get
people starting to think about plumbing this through userspace (xrandr
atomic modeset, for example), maybe the way to go is just revert
mode_config.mutex to a traditional mutex for now.  The weird cases
only start to come up with real atomic ioctls, and we could use a
driver flag to opt-in to that on a per driver basis.

BR,
-R

> -Daniel
> --
> Daniel Vetter
> Software Engineer, Intel Corporation
> +41 (0) 79 365 57 48 - http://blog.ffwll.ch
Daniel Vetter May 26, 2014, 4:09 p.m. UTC | #12
On Mon, May 26, 2014 at 11:49:59AM -0400, Rob Clark wrote:
> On Mon, May 26, 2014 at 11:35 AM, Daniel Vetter <daniel@ffwll.ch> wrote:
> > On Mon, May 26, 2014 at 11:20:49AM -0400, Rob Clark wrote:
> >> On Mon, May 26, 2014 at 11:07 AM, Daniel Vetter <daniel@ffwll.ch> wrote:
> >> > On Mon, May 26, 2014 at 5:04 PM, Rob Clark <robdclark@gmail.com> wrote:
> >> >> hmm, ok, I had thought this case, thread B would get -EDEADLK because
> >> >> lock was held, and not his acquire ctx.  If that is not the case, then
> >> >> I would propose this:
> >> >>
> >> >> All places doing things the old way, must grab mode_config.mutex first
> >> >> currently.  And we use mode_config.mutex to protect
> >> >> mode_config.acquire_ctx.  So all the lower spots grabbing individual
> >> >> crtc mutexes can safely use mode_config.acquire_ctx.
> >> >>
> >> >> Then the only headache is propagating -EDEADLK up the call stack.  If
> >> >> we are lucky, the all already propagate -EINTR, etc.
> >> >
> >> > The output poll work most definitely doesn't propagate -EINTR. Like
> >> > I've said, this will be painful. And imo doing this also makes the kms
> >> > locking into quite a mess overall.
> >>
> >> Well, we could hold mode_config.mutex as a traditional mutex around
> >> atomic operations.  What you loose out would be now _NONBLOCK
> >> operations could conceivable call into driver paths without
> >> mode_config.mutex held.  This was the advantage of converting
> >> mode_config.mutex as well.  Granted, it is slightly theoretical
> >> because until we expose atomic ioctl it would only apply to page_flip
> >> (which was not holding mode_config.mutex).  And we also want to get
> >> rid of mode_config.mutex in these paths too.
> >>
> >> Otoh, if we want to make locking more fine grained, more use of
> >> ww_mutex seems like the best way.  And if that means adding a return
> >> value to a fxn here/there and propagating errors properly, maybe we
> >> should just go ahead and do that.  It sounds like the right long term
> >> solution anyways.
> >
> > Yeah, I'm starting to lean towards trying to elide mode_config.mutex
> > completely from the atomic paths (and modesets in general). I think the
> > only bits we really need is adding ww mutexes to planes _and_ to
> > connectors. The atomic would _only_ ever acquire ww mutexes, and we would
> > be able to guarante that most of them are only held short times so that we
> > don't need to bother with trylocking them for NONBLOCK. That should simply
> > the atomic logic a bit I hope.
> >
> > So that, and a full subsystem audit unfortunately :(
> 
> well, getting rid of any global lock from atomic path is, I hope, the
> eventual goal.  But we are *really* late already with atomic, and too
> many other things backing up on top of this.  I suppose I should have
> a closer look at the detect paths, to just see how hairy the
> alternative of adding error propagation would be.  Although maybe not
> today (it is supposed to be a holiday here.. and I've already spent
> enough of the weekend on atomic ;-))
> 
> Just to get *something* merged to unblock driver work and maybe get
> people starting to think about plumbing this through userspace (xrandr
> atomic modeset, for example), maybe the way to go is just revert
> mode_config.mutex to a traditional mutex for now.  The weird cases
> only start to come up with real atomic ioctls, and we could use a
> driver flag to opt-in to that on a per driver basis.

This will result in people raving about regressions, which from a
maintainer pov I'll reject. If we want to chicken out of this question I'd
vote for an drm_modeset_lock_all for the atomic paths, which also isn't
really helpful. I don't really think there's an easy answer.

Wrt unblocking driver work: In i915 we keep on plunging ahead without the
interface stuff thus far. And wrt unblocking everything else: I hear a lot
of people screaming about the lack of atomic updates, but at least within
Intel not many people throwing resources at it. So my impression is that
it can't be that bad really.
-Daniel
diff mbox

Patch

diff --git a/drivers/gpu/drm/Makefile b/drivers/gpu/drm/Makefile
index ba2ed83..7a6a9f5 100644
--- a/drivers/gpu/drm/Makefile
+++ b/drivers/gpu/drm/Makefile
@@ -14,7 +14,7 @@  drm-y       :=	drm_auth.o drm_buffer.o drm_bufs.o drm_cache.o \
 		drm_info.o drm_debugfs.o drm_encoder_slave.o \
 		drm_trace_points.o drm_global.o drm_prime.o \
 		drm_rect.o drm_vma_manager.o drm_flip_work.o \
-		drm_plane_helper.o drm_atomic.o
+		drm_plane_helper.o drm_atomic.o drm_modeset_lock.o
 
 drm-$(CONFIG_COMPAT) += drm_ioc32.o
 drm-$(CONFIG_DRM_GEM_CMA_HELPER) += drm_gem_cma_helper.o
diff --git a/drivers/gpu/drm/drm_atomic.c b/drivers/gpu/drm/drm_atomic.c
index 12fcbc0..45df5e5 100644
--- a/drivers/gpu/drm/drm_atomic.c
+++ b/drivers/gpu/drm/drm_atomic.c
@@ -57,8 +57,14 @@  struct drm_atomic_state *drm_atomic_begin(struct drm_device *dev,
 	ptr = &state[1];
 
 	kref_init(&state->refcount);
+
+	drm_modeset_acquire_init(&state->acquire_ctx,
+			!!(flags & DRM_MODE_ATOMIC_NOLOCK),
+			!!(flags & DRM_MODE_ATOMIC_NONBLOCK));
+
 	state->dev = dev;
 	state->flags = flags;
+
 	return state;
 }
 EXPORT_SYMBOL(drm_atomic_begin);
@@ -94,10 +100,102 @@  EXPORT_SYMBOL(drm_atomic_set_event);
  */
 int drm_atomic_check(struct drm_device *dev, struct drm_atomic_state *state)
 {
+	struct drm_atomic_state *a = state;
+	a->acquire_ctx.frozen = true;
 	return 0;  /* for now */
 }
 EXPORT_SYMBOL(drm_atomic_check);
 
+/* Note that we drop and re-acquire the locks w/ ww_mutex directly,
+ * since we keep the crtc in our list with in_atomic == true.
+ */
+
+static void drop_locks(struct drm_atomic_state *a,
+		struct ww_acquire_ctx *ww_ctx)
+{
+	struct drm_modeset_acquire_ctx *ctx = &a->acquire_ctx;
+	struct drm_modeset_lock *lock;
+
+	mutex_lock(&ctx->mutex);
+	list_for_each_entry(lock, &ctx->locked, head)
+		ww_mutex_unlock(&lock->mutex);
+	mutex_unlock(&ctx->mutex);
+
+	ww_acquire_fini(ww_ctx);
+}
+
+static void grab_locks(struct drm_atomic_state *a,
+		struct ww_acquire_ctx *ww_ctx)
+{
+	struct drm_modeset_acquire_ctx *ctx = &a->acquire_ctx;
+	struct drm_modeset_lock *lock, *slow_locked, *contended;
+	int ret;
+
+	lock = slow_locked = contended = NULL;
+
+
+	ww_acquire_init(ww_ctx, &crtc_ww_class);
+
+	/*
+	 * We need to do proper rain^Hww dance.. another context
+	 * could sneak in a grab the lock in order to check
+	 * crtc->in_atomic, and we get -EDEADLK.  But the winner
+	 * will realize the mistake when it sees crtc->in_atomic
+	 * already set, and then drop lock and return -EBUSY.
+	 * So we just need to keep dancing until we win.
+	 */
+retry:
+	ret = 0;
+	list_for_each_entry(lock, &ctx->locked, head) {
+		if (lock == slow_locked) {
+			slow_locked = NULL;
+			continue;
+		}
+		contended = lock;
+		ret = ww_mutex_lock(&lock->mutex, ww_ctx);
+		if (ret)
+			goto fail;
+	}
+
+fail:
+	if (ret == -EDEADLK) {
+		/* we lost out in a seqno race, backoff, lock and retry.. */
+
+		list_for_each_entry(lock, &ctx->locked, head) {
+			if (lock == contended)
+				break;
+			ww_mutex_unlock(&lock->mutex);
+		}
+
+		if (slow_locked)
+			ww_mutex_unlock(&slow_locked->mutex);
+
+		ww_mutex_lock_slow(&contended->mutex, ww_ctx);
+		slow_locked = contended;
+		goto retry;
+	}
+	WARN_ON(ret);   /* if we get EALREADY then something is fubar */
+}
+
+static void commit_locks(struct drm_atomic_state *a,
+		struct ww_acquire_ctx *ww_ctx)
+{
+	/* and properly release them (clear in_atomic, remove from list): */
+	drm_modeset_drop_locks(&a->acquire_ctx);
+	ww_acquire_fini(ww_ctx);
+	a->committed = true;
+}
+
+static int atomic_commit(struct drm_atomic_state *a,
+		struct ww_acquire_ctx *ww_ctx)
+{
+	int ret = 0;
+
+	commit_locks(a, ww_ctx);
+
+	return ret;
+}
+
 /**
  * drm_atomic_commit - commit state
  * @dev: DRM device
@@ -109,30 +207,60 @@  EXPORT_SYMBOL(drm_atomic_check);
  * RETURNS
  * Zero for success or -errno
  */
-int drm_atomic_commit(struct drm_device *dev, struct drm_atomic_state *state)
+int drm_atomic_commit(struct drm_device *dev, struct drm_atomic_state *a)
 {
-	return 0;  /* for now */
+	return atomic_commit(a, &a->acquire_ctx.ww_ctx);
 }
 EXPORT_SYMBOL(drm_atomic_commit);
 
 /**
+ * drm_atomic_commit_unlocked - like drm_atomic_commit
+ * but can be called back by driver in other thread.  Manages the lock
+ * transfer from initiating thread.
+ */
+int drm_atomic_commit_unlocked(struct drm_device *dev,
+		struct drm_atomic_state *a)
+{
+	struct ww_acquire_ctx ww_ctx;
+	grab_locks(a, &ww_ctx);
+	return atomic_commit(a, &ww_ctx);
+}
+EXPORT_SYMBOL(drm_atomic_commit_unlocked);
+
+/**
  * drm_atomic_end - conclude the atomic update
  * @dev: DRM device
  * @state: the driver state object
  *
  * Release resources associated with the state object.
  */
-void drm_atomic_end(struct drm_device *dev, struct drm_atomic_state *state)
+void drm_atomic_end(struct drm_device *dev, struct drm_atomic_state *a)
 {
-	drm_atomic_state_unreference(state);
+	/* if commit is happening from another thread, it will
+	 * block grabbing locks until we drop (and not set
+	 * a->committed until after), so this is not a race:
+	 */
+	if (!a->committed)
+		drop_locks(a, &a->acquire_ctx.ww_ctx);
+
+	drm_atomic_state_unreference(a);
 }
 EXPORT_SYMBOL(drm_atomic_end);
 
 void _drm_atomic_state_free(struct kref *kref)
 {
-	struct drm_atomic_state *state =
+	struct drm_atomic_state *a =
 		container_of(kref, struct drm_atomic_state, refcount);
-	kfree(state);
+
+	/* in case we haven't already: */
+	if (!a->committed) {
+		grab_locks(a, &a->acquire_ctx.ww_ctx);
+		commit_locks(a, &a->acquire_ctx.ww_ctx);
+	}
+
+	drm_modeset_acquire_fini(&a->acquire_ctx);
+
+	kfree(a);
 }
 EXPORT_SYMBOL(_drm_atomic_state_free);
 
diff --git a/drivers/gpu/drm/drm_crtc.c b/drivers/gpu/drm/drm_crtc.c
index 1876abb..dd10e4c 100644
--- a/drivers/gpu/drm/drm_crtc.c
+++ b/drivers/gpu/drm/drm_crtc.c
@@ -37,8 +37,8 @@ 
 #include <drm/drm_crtc.h>
 #include <drm/drm_edid.h>
 #include <drm/drm_fourcc.h>
+#include <drm/drm_atomic.h>
 
-#include "drm_crtc_internal.h"
 
 /**
  * drm_modeset_lock_all - take all modeset locks
@@ -50,12 +50,32 @@ 
  */
 void drm_modeset_lock_all(struct drm_device *dev)
 {
-	struct drm_crtc *crtc;
+	struct drm_mode_config *config = &dev->mode_config;
+	struct drm_modeset_acquire_ctx *ctx;
+	int ret;
 
-	mutex_lock(&dev->mode_config.mutex);
+	ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
+	if (WARN_ON(!ctx))
+		return;
 
-	list_for_each_entry(crtc, &dev->mode_config.crtc_list, head)
-		mutex_lock_nest_lock(&crtc->mutex, &dev->mode_config.mutex);
+retry:
+	drm_modeset_acquire_init(ctx, false, false);
+
+	ret = drm_modeset_lock(&config->mutex, ctx) ||
+			drm_modeset_lock_all_crtcs(dev, ctx);
+	if (ret == -EDEADLK) {
+		drm_modeset_drop_locks(ctx);
+		ww_acquire_fini(&ctx->ww_ctx);
+		drm_modeset_acquire_fini(ctx);
+		goto retry;
+	}
+
+	WARN_ON(config->acquire_ctx);
+
+	/* now we hold the locks, so now that it is safe, stash the
+	 * ctx for drm_modeset_unlock_all():
+	 */
+	config->acquire_ctx = ctx;
 }
 EXPORT_SYMBOL(drm_modeset_lock_all);
 
@@ -67,12 +87,18 @@  EXPORT_SYMBOL(drm_modeset_lock_all);
  */
 void drm_modeset_unlock_all(struct drm_device *dev)
 {
-	struct drm_crtc *crtc;
+	struct drm_mode_config *config = &dev->mode_config;
+	struct drm_modeset_acquire_ctx *ctx = config->acquire_ctx;
 
-	list_for_each_entry(crtc, &dev->mode_config.crtc_list, head)
-		mutex_unlock(&crtc->mutex);
+	if (WARN_ON(!ctx))
+		return;
+
+	config->acquire_ctx = NULL;
+	drm_modeset_drop_locks(ctx);
+	ww_acquire_fini(&ctx->ww_ctx);
+	drm_modeset_acquire_fini(ctx);
 
-	mutex_unlock(&dev->mode_config.mutex);
+	kfree(ctx);
 }
 EXPORT_SYMBOL(drm_modeset_unlock_all);
 
@@ -91,9 +117,9 @@  void drm_warn_on_modeset_not_all_locked(struct drm_device *dev)
 		return;
 
 	list_for_each_entry(crtc, &dev->mode_config.crtc_list, head)
-		WARN_ON(!mutex_is_locked(&crtc->mutex));
+		WARN_ON(!drm_modeset_is_locked(&crtc->mutex));
 
-	WARN_ON(!mutex_is_locked(&dev->mode_config.mutex));
+	WARN_ON(!drm_modeset_is_locked(&dev->mode_config.mutex));
 }
 EXPORT_SYMBOL(drm_warn_on_modeset_not_all_locked);
 
@@ -691,6 +717,8 @@  void drm_framebuffer_remove(struct drm_framebuffer *fb)
 }
 EXPORT_SYMBOL(drm_framebuffer_remove);
 
+DEFINE_WW_CLASS(crtc_ww_class);
+
 /**
  * drm_crtc_init_with_planes - Initialise a new CRTC object with
  *    specified primary and cursor planes.
@@ -710,6 +738,7 @@  int drm_crtc_init_with_planes(struct drm_device *dev, struct drm_crtc *crtc,
 			      void *cursor,
 			      const struct drm_crtc_funcs *funcs)
 {
+	struct drm_mode_config *config = &dev->mode_config;
 	int ret;
 
 	crtc->dev = dev;
@@ -717,8 +746,9 @@  int drm_crtc_init_with_planes(struct drm_device *dev, struct drm_crtc *crtc,
 	crtc->invert_dimensions = false;
 
 	drm_modeset_lock_all(dev);
-	mutex_init(&crtc->mutex);
-	mutex_lock_nest_lock(&crtc->mutex, &dev->mode_config.mutex);
+	drm_modeset_lock_init(&crtc->mutex);
+	/* dropped by _unlock_all(): */
+	drm_modeset_lock(&crtc->mutex, config->acquire_ctx);
 
 	ret = drm_mode_object_get(dev, &crtc->base, DRM_MODE_OBJECT_CRTC);
 	if (ret)
@@ -755,6 +785,8 @@  void drm_crtc_cleanup(struct drm_crtc *crtc)
 	kfree(crtc->gamma_store);
 	crtc->gamma_store = NULL;
 
+	drm_modeset_lock_fini(&crtc->mutex);
+
 	drm_mode_object_put(dev, &crtc->base);
 	list_del(&crtc->head);
 	dev->mode_config.num_crtc--;
@@ -1794,7 +1826,7 @@  int drm_mode_getconnector(struct drm_device *dev, void *data,
 
 	DRM_DEBUG_KMS("[CONNECTOR:%d:?]\n", out_resp->connector_id);
 
-	mutex_lock(&dev->mode_config.mutex);
+	drm_modeset_lock(&dev->mode_config.mutex, NULL);
 
 	obj = drm_mode_object_find(dev, out_resp->connector_id,
 				   DRM_MODE_OBJECT_CONNECTOR);
@@ -1895,7 +1927,7 @@  int drm_mode_getconnector(struct drm_device *dev, void *data,
 	out_resp->count_encoders = encoders_count;
 
 out:
-	mutex_unlock(&dev->mode_config.mutex);
+	drm_modeset_unlock(&dev->mode_config.mutex);
 
 	return ret;
 }
@@ -2496,7 +2528,7 @@  static int drm_mode_cursor_common(struct drm_device *dev,
 	}
 	crtc = obj_to_crtc(obj);
 
-	mutex_lock(&crtc->mutex);
+	drm_modeset_lock(&crtc->mutex, NULL);
 	if (req->flags & DRM_MODE_CURSOR_BO) {
 		if (!crtc->funcs->cursor_set && !crtc->funcs->cursor_set2) {
 			ret = -ENXIO;
@@ -2520,7 +2552,7 @@  static int drm_mode_cursor_common(struct drm_device *dev,
 		}
 	}
 out:
-	mutex_unlock(&crtc->mutex);
+	drm_modeset_unlock(&crtc->mutex);
 
 	return ret;
 
@@ -3929,20 +3961,25 @@  int drm_mode_obj_set_property_ioctl(struct drm_device *dev, void *data,
 				    struct drm_file *file_priv)
 {
 	struct drm_mode_obj_set_property *arg = data;
+	struct drm_mode_config *config = &dev->mode_config;
 	struct drm_atomic_state *state;
 	int ret = -EINVAL;
 
 	if (!drm_core_check_feature(dev, DRIVER_MODESET))
 		return -EINVAL;
 
-	drm_modeset_lock_all(dev);
-
+retry:
 	state = dev->driver->atomic_begin(dev, 0);
 	if (IS_ERR(state)) {
 		ret = PTR_ERR(state);
-		goto out_unlock;
+		goto out;
 	}
 
+	ret = drm_modeset_lock(&config->mutex, &state->acquire_ctx) ||
+			drm_modeset_lock_all_crtcs(dev, &state->acquire_ctx);
+	if (ret)
+		goto out;
+
 	ret = drm_mode_set_obj_prop_id(dev, state,
 			arg->obj_id, arg->obj_type,
 			arg->prop_id, arg->value, NULL);
@@ -3957,8 +3994,8 @@  int drm_mode_obj_set_property_ioctl(struct drm_device *dev, void *data,
 
 out:
 	dev->driver->atomic_end(dev, state);
-out_unlock:
-	drm_modeset_unlock_all(dev);
+	if (ret == -EDEADLK)
+		goto retry;
 	return ret;
 }
 
@@ -4195,7 +4232,7 @@  int drm_mode_page_flip_ioctl(struct drm_device *dev,
 		return -ENOENT;
 	crtc = obj_to_crtc(obj);
 
-	mutex_lock(&crtc->mutex);
+	drm_modeset_lock(&crtc->mutex, NULL);
 	if (crtc->primary->fb == NULL) {
 		/* The framebuffer is currently unbound, presumably
 		 * due to a hotplug event, that userspace has not
@@ -4279,7 +4316,7 @@  out:
 		drm_framebuffer_unreference(fb);
 	if (old_fb)
 		drm_framebuffer_unreference(old_fb);
-	mutex_unlock(&crtc->mutex);
+	drm_modeset_unlock(&crtc->mutex);
 
 	return ret;
 }
@@ -4643,7 +4680,7 @@  EXPORT_SYMBOL(drm_format_vert_chroma_subsampling);
  */
 void drm_mode_config_init(struct drm_device *dev)
 {
-	mutex_init(&dev->mode_config.mutex);
+	drm_modeset_lock_init(&dev->mode_config.mutex);
 	mutex_init(&dev->mode_config.idr_mutex);
 	mutex_init(&dev->mode_config.fb_lock);
 	INIT_LIST_HEAD(&dev->mode_config.fb_list);
@@ -4743,5 +4780,6 @@  void drm_mode_config_cleanup(struct drm_device *dev)
 	}
 
 	idr_destroy(&dev->mode_config.crtc_idr);
+	drm_modeset_lock_fini(&dev->mode_config.mutex);
 }
 EXPORT_SYMBOL(drm_mode_config_cleanup);
diff --git a/drivers/gpu/drm/drm_crtc_helper.c b/drivers/gpu/drm/drm_crtc_helper.c
index 54e8fdb..8c30259 100644
--- a/drivers/gpu/drm/drm_crtc_helper.c
+++ b/drivers/gpu/drm/drm_crtc_helper.c
@@ -88,7 +88,7 @@  bool drm_helper_encoder_in_use(struct drm_encoder *encoder)
 	struct drm_connector *connector;
 	struct drm_device *dev = encoder->dev;
 
-	WARN_ON(!mutex_is_locked(&dev->mode_config.mutex));
+	WARN_ON(!drm_modeset_is_locked(&dev->mode_config.mutex));
 	list_for_each_entry(connector, &dev->mode_config.connector_list, head)
 		if (connector->encoder == encoder)
 			return true;
@@ -112,7 +112,7 @@  bool drm_helper_crtc_in_use(struct drm_crtc *crtc)
 	struct drm_encoder *encoder;
 	struct drm_device *dev = crtc->dev;
 
-	WARN_ON(!mutex_is_locked(&dev->mode_config.mutex));
+	WARN_ON(!drm_modeset_is_locked(&dev->mode_config.mutex));
 	list_for_each_entry(encoder, &dev->mode_config.encoder_list, head)
 		if (encoder->crtc == crtc && drm_helper_encoder_in_use(encoder))
 			return true;
diff --git a/drivers/gpu/drm/drm_fb_cma_helper.c b/drivers/gpu/drm/drm_fb_cma_helper.c
index 61b5a47..65540b7 100644
--- a/drivers/gpu/drm/drm_fb_cma_helper.c
+++ b/drivers/gpu/drm/drm_fb_cma_helper.c
@@ -211,13 +211,13 @@  int drm_fb_cma_debugfs_show(struct seq_file *m, void *arg)
 	struct drm_framebuffer *fb;
 	int ret;
 
-	ret = mutex_lock_interruptible(&dev->mode_config.mutex);
+	ret = drm_modeset_lock_interruptible(&dev->mode_config.mutex, NULL);
 	if (ret)
 		return ret;
 
 	ret = mutex_lock_interruptible(&dev->struct_mutex);
 	if (ret) {
-		mutex_unlock(&dev->mode_config.mutex);
+		drm_modeset_unlock(&dev->mode_config.mutex);
 		return ret;
 	}
 
@@ -225,7 +225,7 @@  int drm_fb_cma_debugfs_show(struct seq_file *m, void *arg)
 		drm_fb_cma_describe(fb, m);
 
 	mutex_unlock(&dev->struct_mutex);
-	mutex_unlock(&dev->mode_config.mutex);
+	drm_modeset_unlock(&dev->mode_config.mutex);
 
 	return 0;
 }
diff --git a/drivers/gpu/drm/drm_fb_helper.c b/drivers/gpu/drm/drm_fb_helper.c
index e95ed58..142f6bd 100644
--- a/drivers/gpu/drm/drm_fb_helper.c
+++ b/drivers/gpu/drm/drm_fb_helper.c
@@ -331,7 +331,11 @@  static bool drm_fb_helper_force_kernel_mode(void)
 		if (dev->switch_power_state == DRM_SWITCH_POWER_OFF)
 			continue;
 
-		if (!mutex_trylock(&dev->mode_config.mutex)) {
+		/* NOTE: we use lockless flag below to avoid grabbing other
+		 * modeset locks.  So just trylock the underlying mutex
+		 * directly:
+		 */
+		if (!mutex_trylock(&dev->mode_config.mutex.mutex.base)) {
 			error = true;
 			continue;
 		}
@@ -340,7 +344,7 @@  static bool drm_fb_helper_force_kernel_mode(void)
 		if (ret)
 			error = true;
 
-		mutex_unlock(&dev->mode_config.mutex);
+		mutex_unlock(&dev->mode_config.mutex.mutex.base);
 	}
 	return error;
 }
@@ -1557,11 +1561,11 @@  bool drm_fb_helper_initial_config(struct drm_fb_helper *fb_helper, int bpp_sel)
 
 	drm_fb_helper_parse_command_line(fb_helper);
 
-	mutex_lock(&dev->mode_config.mutex);
+	drm_modeset_lock(&dev->mode_config.mutex, NULL);
 	count = drm_fb_helper_probe_connector_modes(fb_helper,
 						    dev->mode_config.max_width,
 						    dev->mode_config.max_height);
-	mutex_unlock(&dev->mode_config.mutex);
+	drm_modeset_unlock(&dev->mode_config.mutex);
 	/*
 	 * we shouldn't end up with no modes here.
 	 */
@@ -1601,10 +1605,10 @@  int drm_fb_helper_hotplug_event(struct drm_fb_helper *fb_helper)
 	if (!fb_helper->fb)
 		return 0;
 
-	mutex_lock(&fb_helper->dev->mode_config.mutex);
+	drm_modeset_lock(&fb_helper->dev->mode_config.mutex, NULL);
 	if (!drm_fb_helper_is_bound(fb_helper)) {
 		fb_helper->delayed_hotplug = true;
-		mutex_unlock(&fb_helper->dev->mode_config.mutex);
+		drm_modeset_unlock(&fb_helper->dev->mode_config.mutex);
 		return 0;
 	}
 	DRM_DEBUG_KMS("\n");
@@ -1613,7 +1617,7 @@  int drm_fb_helper_hotplug_event(struct drm_fb_helper *fb_helper)
 	max_height = fb_helper->fb->height;
 
 	drm_fb_helper_probe_connector_modes(fb_helper, max_width, max_height);
-	mutex_unlock(&fb_helper->dev->mode_config.mutex);
+	drm_modeset_unlock(&fb_helper->dev->mode_config.mutex);
 
 	drm_modeset_lock_all(dev);
 	drm_setup_crtcs(fb_helper);
diff --git a/drivers/gpu/drm/drm_modes.c b/drivers/gpu/drm/drm_modes.c
index bedf189..d8a6b16 100644
--- a/drivers/gpu/drm/drm_modes.c
+++ b/drivers/gpu/drm/drm_modes.c
@@ -116,7 +116,7 @@  EXPORT_SYMBOL(drm_mode_destroy);
 void drm_mode_probed_add(struct drm_connector *connector,
 			 struct drm_display_mode *mode)
 {
-	WARN_ON(!mutex_is_locked(&connector->dev->mode_config.mutex));
+	WARN_ON(!drm_modeset_is_locked(&connector->dev->mode_config.mutex));
 
 	list_add_tail(&mode->head, &connector->probed_modes);
 }
@@ -1029,7 +1029,7 @@  void drm_mode_connector_list_update(struct drm_connector *connector,
 	struct drm_display_mode *pmode, *pt;
 	int found_it;
 
-	WARN_ON(!mutex_is_locked(&connector->dev->mode_config.mutex));
+	WARN_ON(!drm_modeset_is_locked(&connector->dev->mode_config.mutex));
 
 	list_for_each_entry_safe(pmode, pt, &connector->probed_modes,
 				 head) {
diff --git a/drivers/gpu/drm/drm_modeset_lock.c b/drivers/gpu/drm/drm_modeset_lock.c
new file mode 100644
index 0000000..49f1afc
--- /dev/null
+++ b/drivers/gpu/drm/drm_modeset_lock.c
@@ -0,0 +1,159 @@ 
+/*
+ * Copyright (C) 2014 Red Hat
+ * Author: Rob Clark <robdclark@gmail.com>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the "Software"),
+ * to deal in the Software without restriction, including without limitation
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
+ * and/or sell copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
+ * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
+ * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
+ * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
+ * OTHER DEALINGS IN THE SOFTWARE.
+ */
+
+#include <drm/drmP.h>
+#include <drm/drm_crtc.h>
+#include <drm/drm_modeset_lock.h>
+
+
+void drm_modeset_acquire_init(struct drm_modeset_acquire_ctx *ctx,
+		bool nolock, bool nonblock)
+{
+	ww_acquire_init(&ctx->ww_ctx, &crtc_ww_class);
+	INIT_LIST_HEAD(&ctx->locked);
+	mutex_init(&ctx->mutex);
+	ctx->nolock = nolock;
+	ctx->nonblock = nonblock;
+}
+
+void drm_modeset_acquire_fini(struct drm_modeset_acquire_ctx *ctx)
+{
+	/*
+	 * NOTE: it is intentional that ww_acquire_fini() is not called
+	 * here.. due to the way lock handover works in drm_atomic
+	 */
+	mutex_destroy(&ctx->mutex);
+}
+
+void drm_modeset_drop_locks(struct drm_modeset_acquire_ctx *ctx)
+{
+	mutex_lock(&ctx->mutex);
+	while (!list_empty(&ctx->locked)) {
+		struct drm_modeset_lock *lock;
+
+		lock = list_first_entry(&ctx->locked,
+				struct drm_modeset_lock, head);
+
+		drm_modeset_unlock(lock);
+	}
+	mutex_unlock(&ctx->mutex);
+}
+
+static int modeset_lock(struct drm_modeset_lock *lock,
+		struct drm_modeset_acquire_ctx *ctx, bool interruptible)
+{
+	int ret;
+
+	if (ctx->nolock)
+		return 0;
+
+	WARN_ON(ctx->frozen);    /* all locks should be held by now! */
+
+retry:
+	if (interruptible)
+		ret = ww_mutex_lock_interruptible(&lock->mutex, &ctx->ww_ctx);
+	else
+		ret = ww_mutex_lock(&lock->mutex, &ctx->ww_ctx);
+	if (!ret) {
+		if (lock->atomic_pending) {
+			/* some other pending update with dropped locks */
+			ww_mutex_unlock(&lock->mutex);
+			if (ctx->nonblock)
+				return -EBUSY;
+			wait_event(lock->event, !lock->atomic_pending);
+			goto retry;
+		}
+		lock->atomic_pending = true;
+		WARN_ON(!list_empty(&lock->head));
+		list_add(&lock->head, &ctx->locked);
+	} else if (ret == -EALREADY) {
+		/* we already hold the lock.. this is fine */
+		ret = 0;
+	}
+
+	return ret;
+}
+
+/**
+ * drm_modeset_lock - take modeset lock
+ * @lock: lock to take
+ * @ctx: acquire ctx
+ *
+ * If ctx is not NULL, then then it's acquire context is used
+ * and the lock does not need to be explicitly unlocked, it
+ * will be automatically unlocked when the atomic update is
+ * complete
+ */
+int drm_modeset_lock(struct drm_modeset_lock *lock,
+		struct drm_modeset_acquire_ctx *ctx)
+{
+	if (ctx)
+		return modeset_lock(lock, ctx, false);
+
+	ww_mutex_lock(&lock->mutex, NULL);
+	return 0;
+}
+EXPORT_SYMBOL(drm_modeset_lock);
+
+int drm_modeset_lock_interruptible(struct drm_modeset_lock *lock,
+		struct drm_modeset_acquire_ctx *ctx)
+{
+	if (ctx)
+		return modeset_lock(lock, ctx, true);
+
+	return ww_mutex_lock_interruptible(&lock->mutex, NULL);
+}
+EXPORT_SYMBOL(drm_modeset_lock_interruptible);
+
+/**
+ * drm_modeset_unlock - drop modeset lock
+ * @lock: lock to release
+ */
+void drm_modeset_unlock(struct drm_modeset_lock *lock)
+{
+	list_del_init(&lock->head);
+	lock->atomic_pending = false;
+	ww_mutex_unlock(&lock->mutex);
+	wake_up_all(&lock->event);
+}
+EXPORT_SYMBOL(drm_modeset_unlock);
+
+/**
+ * drm_modeset_lock_all_crtcs - helper to drm_modeset_lock() all CRTCs
+ */
+int drm_modeset_lock_all_crtcs(struct drm_device *dev,
+		struct drm_modeset_acquire_ctx *ctx)
+{
+	struct drm_mode_config *config = &dev->mode_config;
+	struct drm_crtc *crtc;
+	int ret = 0;
+
+	list_for_each_entry(crtc, &config->crtc_list, head) {
+		ret = drm_modeset_lock(&crtc->mutex, ctx);
+		if (ret)
+			return ret;
+	}
+
+	return 0;
+}
+EXPORT_SYMBOL(drm_modeset_lock_all_crtcs);
diff --git a/drivers/gpu/drm/drm_probe_helper.c b/drivers/gpu/drm/drm_probe_helper.c
index 79f07f2..4552fe5 100644
--- a/drivers/gpu/drm/drm_probe_helper.c
+++ b/drivers/gpu/drm/drm_probe_helper.c
@@ -93,7 +93,7 @@  static int drm_helper_probe_single_connector_modes_merge_bits(struct drm_connect
 	int mode_flags = 0;
 	bool verbose_prune = true;
 
-	WARN_ON(!mutex_is_locked(&dev->mode_config.mutex));
+	WARN_ON(!drm_modeset_is_locked(&dev->mode_config.mutex));
 
 	DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n", connector->base.id,
 			drm_get_connector_name(connector));
@@ -255,7 +255,7 @@  static void output_poll_execute(struct work_struct *work)
 	if (!drm_kms_helper_poll)
 		return;
 
-	mutex_lock(&dev->mode_config.mutex);
+	drm_modeset_lock(&dev->mode_config.mutex, NULL);
 	list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
 
 		/* Ignore forced connectors. */
@@ -293,7 +293,7 @@  static void output_poll_execute(struct work_struct *work)
 		}
 	}
 
-	mutex_unlock(&dev->mode_config.mutex);
+	drm_modeset_unlock(&dev->mode_config.mutex);
 
 	if (changed)
 		drm_kms_helper_hotplug_event(dev);
@@ -419,7 +419,7 @@  bool drm_helper_hpd_irq_event(struct drm_device *dev)
 	if (!dev->mode_config.poll_enabled)
 		return false;
 
-	mutex_lock(&dev->mode_config.mutex);
+	drm_modeset_lock(&dev->mode_config.mutex, NULL);
 	list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
 
 		/* Only handle HPD capable connectors. */
@@ -438,7 +438,7 @@  bool drm_helper_hpd_irq_event(struct drm_device *dev)
 			changed = true;
 	}
 
-	mutex_unlock(&dev->mode_config.mutex);
+	drm_modeset_unlock(&dev->mode_config.mutex);
 
 	if (changed)
 		drm_kms_helper_hotplug_event(dev);
diff --git a/drivers/gpu/drm/drm_sysfs.c b/drivers/gpu/drm/drm_sysfs.c
index c22c309..981e570 100644
--- a/drivers/gpu/drm/drm_sysfs.c
+++ b/drivers/gpu/drm/drm_sysfs.c
@@ -173,12 +173,12 @@  static ssize_t status_show(struct device *device,
 	enum drm_connector_status status;
 	int ret;
 
-	ret = mutex_lock_interruptible(&connector->dev->mode_config.mutex);
+	ret = drm_modeset_lock_interruptible(&connector->dev->mode_config.mutex, NULL);
 	if (ret)
 		return ret;
 
 	status = connector->funcs->detect(connector, true);
-	mutex_unlock(&connector->dev->mode_config.mutex);
+	drm_modeset_unlock(&connector->dev->mode_config.mutex);
 
 	return snprintf(buf, PAGE_SIZE, "%s\n",
 			drm_get_connector_status_name(status));
diff --git a/drivers/gpu/drm/exynos/exynos_drm_fbdev.c b/drivers/gpu/drm/exynos/exynos_drm_fbdev.c
index addbf75..2371716 100644
--- a/drivers/gpu/drm/exynos/exynos_drm_fbdev.c
+++ b/drivers/gpu/drm/exynos/exynos_drm_fbdev.c
@@ -242,7 +242,7 @@  bool exynos_drm_fbdev_is_anything_connected(struct drm_device *dev)
 	struct drm_connector *connector;
 	bool ret = false;
 
-	mutex_lock(&dev->mode_config.mutex);
+	drm_modeset_lock(&dev->mode_config.mutex, NULL);
 	list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
 		if (connector->status != connector_status_connected)
 			continue;
@@ -250,7 +250,7 @@  bool exynos_drm_fbdev_is_anything_connected(struct drm_device *dev)
 		ret = true;
 		break;
 	}
-	mutex_unlock(&dev->mode_config.mutex);
+	drm_modeset_unlock(&dev->mode_config.mutex);
 
 	return ret;
 }
diff --git a/drivers/gpu/drm/gma500/cdv_intel_lvds.c b/drivers/gpu/drm/gma500/cdv_intel_lvds.c
index 7ffaed4..f9ee300 100644
--- a/drivers/gpu/drm/gma500/cdv_intel_lvds.c
+++ b/drivers/gpu/drm/gma500/cdv_intel_lvds.c
@@ -714,7 +714,7 @@  void cdv_intel_lvds_init(struct drm_device *dev,
 	 * Attempt to get the fixed panel mode from DDC.  Assume that the
 	 * preferred mode is the right one.
 	 */
-	mutex_lock(&dev->mode_config.mutex);
+	drm_modeset_lock(&dev->mode_config.mutex, NULL);
 	psb_intel_ddc_get_modes(connector,
 				&gma_encoder->ddc_bus->adapter);
 	list_for_each_entry(scan, &connector->probed_modes, head) {
@@ -775,12 +775,12 @@  void cdv_intel_lvds_init(struct drm_device *dev,
 	}
 
 out:
-	mutex_unlock(&dev->mode_config.mutex);
+	drm_modeset_unlock(&dev->mode_config.mutex);
 	drm_sysfs_connector_add(connector);
 	return;
 
 failed_find:
-	mutex_unlock(&dev->mode_config.mutex);
+	drm_modeset_unlock(&dev->mode_config.mutex);
 	printk(KERN_ERR "Failed find\n");
 	if (gma_encoder->ddc_bus)
 		psb_intel_i2c_destroy(gma_encoder->ddc_bus);
diff --git a/drivers/gpu/drm/gma500/oaktrail_lvds.c b/drivers/gpu/drm/gma500/oaktrail_lvds.c
index 9b09946..b8c1c39 100644
--- a/drivers/gpu/drm/gma500/oaktrail_lvds.c
+++ b/drivers/gpu/drm/gma500/oaktrail_lvds.c
@@ -359,7 +359,7 @@  void oaktrail_lvds_init(struct drm_device *dev,
 	 *    if closed, act like it's not there for now
 	 */
 
-	mutex_lock(&dev->mode_config.mutex);
+	drm_modeset_lock(&dev->mode_config.mutex, NULL);
 	i2c_adap = i2c_get_adapter(dev_priv->ops->i2c_bus);
 	if (i2c_adap == NULL)
 		dev_err(dev->dev, "No ddc adapter available!\n");
@@ -402,13 +402,13 @@  void oaktrail_lvds_init(struct drm_device *dev,
 	}
 
 out:
-	mutex_unlock(&dev->mode_config.mutex);
+	drm_modeset_unlock(&dev->mode_config.mutex);
 
 	drm_sysfs_connector_add(connector);
 	return;
 
 failed_find:
-	mutex_unlock(&dev->mode_config.mutex);
+	drm_modeset_unlock(&dev->mode_config.mutex);
 
 	dev_dbg(dev->dev, "No LVDS modes found, disabling.\n");
 	if (gma_encoder->ddc_bus)
diff --git a/drivers/gpu/drm/gma500/psb_intel_lvds.c b/drivers/gpu/drm/gma500/psb_intel_lvds.c
index 6b64d9d..3442e44 100644
--- a/drivers/gpu/drm/gma500/psb_intel_lvds.c
+++ b/drivers/gpu/drm/gma500/psb_intel_lvds.c
@@ -779,7 +779,7 @@  void psb_intel_lvds_init(struct drm_device *dev,
 	 * Attempt to get the fixed panel mode from DDC.  Assume that the
 	 * preferred mode is the right one.
 	 */
-	mutex_lock(&dev->mode_config.mutex);
+	drm_modeset_lock(&dev->mode_config.mutex, NULL);
 	psb_intel_ddc_get_modes(connector, &lvds_priv->ddc_bus->adapter);
 	list_for_each_entry(scan, &connector->probed_modes, head) {
 		if (scan->type & DRM_MODE_TYPE_PREFERRED) {
@@ -830,12 +830,12 @@  void psb_intel_lvds_init(struct drm_device *dev,
 	 * actually having one.
 	 */
 out:
-	mutex_unlock(&dev->mode_config.mutex);
+	drm_modeset_unlock(&dev->mode_config.mutex);
 	drm_sysfs_connector_add(connector);
 	return;
 
 failed_find:
-	mutex_unlock(&dev->mode_config.mutex);
+	drm_modeset_unlock(&dev->mode_config.mutex);
 	if (lvds_priv->ddc_bus)
 		psb_intel_i2c_destroy(lvds_priv->ddc_bus);
 failed_ddc:
diff --git a/drivers/gpu/drm/i915/i915_debugfs.c b/drivers/gpu/drm/i915/i915_debugfs.c
index 18b3565..693c13b 100644
--- a/drivers/gpu/drm/i915/i915_debugfs.c
+++ b/drivers/gpu/drm/i915/i915_debugfs.c
@@ -1635,7 +1635,7 @@  static int i915_gem_framebuffer_info(struct seq_file *m, void *data)
 
 #ifdef CONFIG_DRM_I915_FBDEV
 	struct drm_i915_private *dev_priv = dev->dev_private;
-	int ret = mutex_lock_interruptible(&dev->mode_config.mutex);
+	int ret = drm_modeset_lock_interruptible(&dev->mode_config.mutex, NULL);
 	if (ret)
 		return ret;
 
@@ -1650,7 +1650,7 @@  static int i915_gem_framebuffer_info(struct seq_file *m, void *data)
 		   atomic_read(&fb->base.refcount.refcount));
 	describe_obj(m, fb->obj);
 	seq_putc(m, '\n');
-	mutex_unlock(&dev->mode_config.mutex);
+	drm_modeset_unlock(&dev->mode_config.mutex);
 #endif
 
 	mutex_lock(&dev->mode_config.fb_lock);
@@ -1681,7 +1681,7 @@  static int i915_context_status(struct seq_file *m, void *unused)
 	struct i915_hw_context *ctx;
 	int ret, i;
 
-	ret = mutex_lock_interruptible(&dev->mode_config.mutex);
+	ret = drm_modeset_lock_interruptible(&dev->mode_config.mutex, NULL);
 	if (ret)
 		return ret;
 
@@ -1711,7 +1711,7 @@  static int i915_context_status(struct seq_file *m, void *unused)
 		seq_putc(m, '\n');
 	}
 
-	mutex_unlock(&dev->mode_config.mutex);
+	drm_modeset_unlock(&dev->mode_config.mutex);
 
 	return 0;
 }
@@ -2573,7 +2573,7 @@  static int i9xx_pipe_crc_auto_source(struct drm_device *dev, enum pipe pipe,
 
 	*source = INTEL_PIPE_CRC_SOURCE_PIPE;
 
-	mutex_lock(&dev->mode_config.mutex);
+	drm_modeset_lock(&dev->mode_config.mutex, NULL);
 	list_for_each_entry(encoder, &dev->mode_config.encoder_list,
 			    base.head) {
 		if (!encoder->base.crtc)
@@ -2609,7 +2609,7 @@  static int i9xx_pipe_crc_auto_source(struct drm_device *dev, enum pipe pipe,
 			break;
 		}
 	}
-	mutex_unlock(&dev->mode_config.mutex);
+	drm_modeset_unlock(&dev->mode_config.mutex);
 
 	return ret;
 }
diff --git a/drivers/gpu/drm/i915/i915_drv.c b/drivers/gpu/drm/i915/i915_drv.c
index e3a2482..8f2741a 100644
--- a/drivers/gpu/drm/i915/i915_drv.c
+++ b/drivers/gpu/drm/i915/i915_drv.c
@@ -475,10 +475,10 @@  static int i915_drm_freeze(struct drm_device *dev)
 		 * Disable CRTCs directly since we want to preserve sw state
 		 * for _thaw.
 		 */
-		mutex_lock(&dev->mode_config.mutex);
+		drm_modeset_lock(&dev->mode_config.mutex, NULL);
 		list_for_each_entry(crtc, &dev->mode_config.crtc_list, head)
 			dev_priv->display.crtc_disable(crtc);
-		mutex_unlock(&dev->mode_config.mutex);
+		drm_modeset_unlock(&dev->mode_config.mutex);
 
 		intel_modeset_suspend_hw(dev);
 	}
@@ -546,14 +546,14 @@  static void intel_resume_hotplug(struct drm_device *dev)
 	struct drm_mode_config *mode_config = &dev->mode_config;
 	struct intel_encoder *encoder;
 
-	mutex_lock(&mode_config->mutex);
+	drm_modeset_lock(&mode_config->mutex, NULL);
 	DRM_DEBUG_KMS("running encoder hotplug functions\n");
 
 	list_for_each_entry(encoder, &mode_config->encoder_list, base.head)
 		if (encoder->hot_plug)
 			encoder->hot_plug(encoder);
 
-	mutex_unlock(&mode_config->mutex);
+	drm_modeset_unlock(&mode_config->mutex);
 
 	/* Just fire off a uevent and let userspace tell us what to do */
 	drm_helper_hpd_irq_event(dev);
diff --git a/drivers/gpu/drm/i915/i915_irq.c b/drivers/gpu/drm/i915/i915_irq.c
index 2d76183..e8e931f 100644
--- a/drivers/gpu/drm/i915/i915_irq.c
+++ b/drivers/gpu/drm/i915/i915_irq.c
@@ -940,7 +940,7 @@  static bool intel_hpd_irq_event(struct drm_device *dev,
 {
 	enum drm_connector_status old_status;
 
-	WARN_ON(!mutex_is_locked(&dev->mode_config.mutex));
+	WARN_ON(!drm_modeset_is_locked(&dev->mode_config.mutex));
 	old_status = connector->status;
 
 	connector->status = connector->funcs->detect(connector, false);
@@ -979,7 +979,7 @@  static void i915_hotplug_work_func(struct work_struct *work)
 	if (!dev_priv->enable_hotplug_processing)
 		return;
 
-	mutex_lock(&mode_config->mutex);
+	drm_modeset_lock(&mode_config->mutex, NULL);
 	DRM_DEBUG_KMS("running encoder hotplug functions\n");
 
 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
@@ -1026,7 +1026,7 @@  static void i915_hotplug_work_func(struct work_struct *work)
 				changed = true;
 		}
 	}
-	mutex_unlock(&mode_config->mutex);
+	drm_modeset_unlock(&mode_config->mutex);
 
 	if (changed)
 		drm_kms_helper_hotplug_event(dev);
diff --git a/drivers/gpu/drm/i915/intel_display.c b/drivers/gpu/drm/i915/intel_display.c
index 72b5c34..562e575 100644
--- a/drivers/gpu/drm/i915/intel_display.c
+++ b/drivers/gpu/drm/i915/intel_display.c
@@ -2363,7 +2363,7 @@  void intel_display_handle_reset(struct drm_device *dev)
 	list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
 		struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
 
-		mutex_lock(&crtc->mutex);
+		drm_modeset_lock(&crtc->mutex, NULL);
 		/*
 		 * FIXME: Once we have proper support for primary planes (and
 		 * disabling them without disabling the entire crtc) allow again
@@ -2374,7 +2374,7 @@  void intel_display_handle_reset(struct drm_device *dev)
 							       crtc->primary->fb,
 							       crtc->x,
 							       crtc->y);
-		mutex_unlock(&crtc->mutex);
+		drm_modeset_unlock(&crtc->mutex);
 	}
 }
 
@@ -8002,7 +8002,7 @@  bool intel_get_load_detect_pipe(struct drm_connector *connector,
 	if (encoder->crtc) {
 		crtc = encoder->crtc;
 
-		mutex_lock(&crtc->mutex);
+		drm_modeset_lock(&crtc->mutex, NULL);
 
 		old->dpms_mode = connector->dpms;
 		old->load_detect_temp = false;
@@ -8033,7 +8033,7 @@  bool intel_get_load_detect_pipe(struct drm_connector *connector,
 		return false;
 	}
 
-	mutex_lock(&crtc->mutex);
+	drm_modeset_lock(&crtc->mutex, NULL);
 	intel_encoder->new_crtc = to_intel_crtc(crtc);
 	to_intel_connector(connector)->new_encoder = intel_encoder;
 
@@ -8083,7 +8083,7 @@  bool intel_get_load_detect_pipe(struct drm_connector *connector,
 		intel_crtc->new_config = &intel_crtc->config;
 	else
 		intel_crtc->new_config = NULL;
-	mutex_unlock(&crtc->mutex);
+	drm_modeset_unlock(&crtc->mutex);
 	return false;
 }
 
@@ -8112,7 +8112,7 @@  void intel_release_load_detect_pipe(struct drm_connector *connector,
 			drm_framebuffer_unreference(old->release_fb);
 		}
 
-		mutex_unlock(&crtc->mutex);
+		drm_modeset_unlock(&crtc->mutex);
 		return;
 	}
 
@@ -8120,7 +8120,7 @@  void intel_release_load_detect_pipe(struct drm_connector *connector,
 	if (old->dpms_mode != DRM_MODE_DPMS_ON)
 		connector->funcs->dpms(connector, old->dpms_mode);
 
-	mutex_unlock(&crtc->mutex);
+	drm_modeset_unlock(&crtc->mutex);
 }
 
 static int i9xx_pll_refclk(struct drm_device *dev,
@@ -10578,7 +10578,7 @@  enum pipe intel_get_pipe_from_connector(struct intel_connector *connector)
 {
 	struct drm_encoder *encoder = connector->base.encoder;
 
-	WARN_ON(!mutex_is_locked(&connector->base.dev->mode_config.mutex));
+	WARN_ON(!drm_modeset_is_locked(&connector->base.dev->mode_config.mutex));
 
 	if (!encoder)
 		return INVALID_PIPE;
@@ -11336,9 +11336,9 @@  void intel_modeset_init(struct drm_device *dev)
 	/* Just in case the BIOS is doing something questionable. */
 	intel_disable_fbc(dev);
 
-	mutex_lock(&dev->mode_config.mutex);
+	drm_modeset_lock(&dev->mode_config.mutex, NULL);
 	intel_modeset_setup_hw_state(dev, false);
-	mutex_unlock(&dev->mode_config.mutex);
+	drm_modeset_unlock(&dev->mode_config.mutex);
 
 	list_for_each_entry(crtc, &dev->mode_config.crtc_list,
 			    base.head) {
diff --git a/drivers/gpu/drm/i915/intel_dp.c b/drivers/gpu/drm/i915/intel_dp.c
index c63d210..e4c0b10 100644
--- a/drivers/gpu/drm/i915/intel_dp.c
+++ b/drivers/gpu/drm/i915/intel_dp.c
@@ -1131,7 +1131,7 @@  static void edp_panel_vdd_off_sync(struct intel_dp *intel_dp)
 	u32 pp;
 	u32 pp_stat_reg, pp_ctrl_reg;
 
-	WARN_ON(!mutex_is_locked(&dev->mode_config.mutex));
+	WARN_ON(!drm_modeset_is_locked(&dev->mode_config.mutex));
 
 	if (!intel_dp->want_panel_vdd && edp_have_panel_vdd(intel_dp)) {
 		struct intel_digital_port *intel_dig_port =
@@ -1168,9 +1168,9 @@  static void edp_panel_vdd_work(struct work_struct *__work)
 						 struct intel_dp, panel_vdd_work);
 	struct drm_device *dev = intel_dp_to_dev(intel_dp);
 
-	mutex_lock(&dev->mode_config.mutex);
+	drm_modeset_lock(&dev->mode_config.mutex, NULL);
 	edp_panel_vdd_off_sync(intel_dp);
-	mutex_unlock(&dev->mode_config.mutex);
+	drm_modeset_unlock(&dev->mode_config.mutex);
 }
 
 static void edp_panel_vdd_off(struct intel_dp *intel_dp, bool sync)
@@ -3385,9 +3385,9 @@  void intel_dp_encoder_destroy(struct drm_encoder *encoder)
 	drm_encoder_cleanup(encoder);
 	if (is_edp(intel_dp)) {
 		cancel_delayed_work_sync(&intel_dp->panel_vdd_work);
-		mutex_lock(&dev->mode_config.mutex);
+		drm_modeset_lock(&dev->mode_config.mutex, NULL);
 		edp_panel_vdd_off_sync(intel_dp);
-		mutex_unlock(&dev->mode_config.mutex);
+		drm_modeset_unlock(&dev->mode_config.mutex);
 	}
 	kfree(intel_dig_port);
 }
@@ -3829,7 +3829,7 @@  static bool intel_edp_init_connector(struct intel_dp *intel_dp,
 	/* We now know it's not a ghost, init power sequence regs. */
 	intel_dp_init_panel_power_sequencer_registers(dev, intel_dp, power_seq);
 
-	mutex_lock(&dev->mode_config.mutex);
+	drm_modeset_lock(&dev->mode_config.mutex, NULL);
 	edid = drm_get_edid(connector, &intel_dp->aux.ddc);
 	if (edid) {
 		if (drm_add_edid_modes(connector, edid)) {
@@ -3863,7 +3863,7 @@  static bool intel_edp_init_connector(struct intel_dp *intel_dp,
 		if (fixed_mode)
 			fixed_mode->type |= DRM_MODE_TYPE_PREFERRED;
 	}
-	mutex_unlock(&dev->mode_config.mutex);
+	drm_modeset_unlock(&dev->mode_config.mutex);
 
 	intel_panel_init(&intel_connector->panel, fixed_mode, downclock_mode);
 	intel_panel_setup_backlight(connector);
@@ -3966,9 +3966,9 @@  intel_dp_init_connector(struct intel_digital_port *intel_dig_port,
 		drm_dp_aux_unregister_i2c_bus(&intel_dp->aux);
 		if (is_edp(intel_dp)) {
 			cancel_delayed_work_sync(&intel_dp->panel_vdd_work);
-			mutex_lock(&dev->mode_config.mutex);
+			drm_modeset_lock(&dev->mode_config.mutex, NULL);
 			edp_panel_vdd_off_sync(intel_dp);
-			mutex_unlock(&dev->mode_config.mutex);
+			drm_modeset_unlock(&dev->mode_config.mutex);
 		}
 		drm_sysfs_connector_remove(connector);
 		drm_connector_cleanup(connector);
diff --git a/drivers/gpu/drm/i915/intel_lvds.c b/drivers/gpu/drm/i915/intel_lvds.c
index 4028237..bdee5d8 100644
--- a/drivers/gpu/drm/i915/intel_lvds.c
+++ b/drivers/gpu/drm/i915/intel_lvds.c
@@ -997,7 +997,7 @@  void intel_lvds_init(struct drm_device *dev)
 	 * Attempt to get the fixed panel mode from DDC.  Assume that the
 	 * preferred mode is the right one.
 	 */
-	mutex_lock(&dev->mode_config.mutex);
+	drm_modeset_lock(&dev->mode_config.mutex, NULL);
 	edid = drm_get_edid(connector, intel_gmbus_get_adapter(dev_priv, pin));
 	if (edid) {
 		if (drm_add_edid_modes(connector, edid)) {
@@ -1091,7 +1091,7 @@  void intel_lvds_init(struct drm_device *dev)
 		goto failed;
 
 out:
-	mutex_unlock(&dev->mode_config.mutex);
+	drm_modeset_unlock(&dev->mode_config.mutex);
 
 	lvds_encoder->is_dual_link = compute_is_dual_link_lvds(lvds_encoder);
 	DRM_DEBUG_KMS("detected %s-link lvds configuration\n",
@@ -1121,7 +1121,7 @@  out:
 	return;
 
 failed:
-	mutex_unlock(&dev->mode_config.mutex);
+	drm_modeset_unlock(&dev->mode_config.mutex);
 
 	DRM_DEBUG_KMS("No LVDS modes found, disabling.\n");
 	drm_connector_cleanup(connector);
diff --git a/drivers/gpu/drm/i915/intel_opregion.c b/drivers/gpu/drm/i915/intel_opregion.c
index acde294..81d2998 100644
--- a/drivers/gpu/drm/i915/intel_opregion.c
+++ b/drivers/gpu/drm/i915/intel_opregion.c
@@ -410,7 +410,7 @@  static u32 asle_set_backlight(struct drm_device *dev, u32 bclp)
 	if (bclp > 255)
 		return ASLC_BACKLIGHT_FAILED;
 
-	mutex_lock(&dev->mode_config.mutex);
+	drm_modeset_lock(&dev->mode_config.mutex, NULL);
 
 	/*
 	 * Update backlight on all connectors that support backlight (usually
@@ -421,7 +421,7 @@  static u32 asle_set_backlight(struct drm_device *dev, u32 bclp)
 		intel_panel_set_backlight(intel_connector, bclp, 255);
 	iowrite32(DIV_ROUND_UP(bclp * 100, 255) | ASLE_CBLV_VALID, &asle->cblv);
 
-	mutex_unlock(&dev->mode_config.mutex);
+	drm_modeset_unlock(&dev->mode_config.mutex);
 
 
 	return 0;
diff --git a/drivers/gpu/drm/i915/intel_overlay.c b/drivers/gpu/drm/i915/intel_overlay.c
index d8adc91..38f8234 100644
--- a/drivers/gpu/drm/i915/intel_overlay.c
+++ b/drivers/gpu/drm/i915/intel_overlay.c
@@ -688,7 +688,7 @@  static int intel_overlay_do_put_image(struct intel_overlay *overlay,
 	u32 swidth, swidthsw, sheight, ostride;
 
 	BUG_ON(!mutex_is_locked(&dev->struct_mutex));
-	BUG_ON(!mutex_is_locked(&dev->mode_config.mutex));
+	BUG_ON(!drm_modeset_is_locked(&dev->mode_config.mutex));
 	BUG_ON(!overlay);
 
 	ret = intel_overlay_release_old_vid(overlay);
@@ -793,7 +793,7 @@  int intel_overlay_switch_off(struct intel_overlay *overlay)
 	int ret;
 
 	BUG_ON(!mutex_is_locked(&dev->struct_mutex));
-	BUG_ON(!mutex_is_locked(&dev->mode_config.mutex));
+	BUG_ON(!drm_modeset_is_locked(&dev->mode_config.mutex));
 
 	ret = intel_overlay_recover_from_interrupt(overlay);
 	if (ret != 0)
diff --git a/drivers/gpu/drm/i915/intel_panel.c b/drivers/gpu/drm/i915/intel_panel.c
index 44ad415..4121b48 100644
--- a/drivers/gpu/drm/i915/intel_panel.c
+++ b/drivers/gpu/drm/i915/intel_panel.c
@@ -823,12 +823,12 @@  static int intel_backlight_device_update_status(struct backlight_device *bd)
 	struct intel_connector *connector = bl_get_data(bd);
 	struct drm_device *dev = connector->base.dev;
 
-	mutex_lock(&dev->mode_config.mutex);
+	drm_modeset_lock(&dev->mode_config.mutex, NULL);
 	DRM_DEBUG_KMS("updating intel_backlight, brightness=%d/%d\n",
 		      bd->props.brightness, bd->props.max_brightness);
 	intel_panel_set_backlight(connector, bd->props.brightness,
 				  bd->props.max_brightness);
-	mutex_unlock(&dev->mode_config.mutex);
+	drm_modeset_unlock(&dev->mode_config.mutex);
 	return 0;
 }
 
@@ -840,9 +840,9 @@  static int intel_backlight_device_get_brightness(struct backlight_device *bd)
 	int ret;
 
 	intel_runtime_pm_get(dev_priv);
-	mutex_lock(&dev->mode_config.mutex);
+	drm_modeset_lock(&dev->mode_config.mutex, NULL);
 	ret = intel_panel_get_backlight(connector);
-	mutex_unlock(&dev->mode_config.mutex);
+	drm_modeset_unlock(&dev->mode_config.mutex);
 	intel_runtime_pm_put(dev_priv);
 
 	return ret;
diff --git a/drivers/gpu/drm/i915/intel_sprite.c b/drivers/gpu/drm/i915/intel_sprite.c
index 213cd58..c235546 100644
--- a/drivers/gpu/drm/i915/intel_sprite.c
+++ b/drivers/gpu/drm/i915/intel_sprite.c
@@ -55,7 +55,7 @@  static bool intel_pipe_update_start(struct intel_crtc *crtc, uint32_t *start_vbl
 	int scanline, min, max, vblank_start;
 	DEFINE_WAIT(wait);
 
-	WARN_ON(!mutex_is_locked(&crtc->base.mutex));
+	WARN_ON(!drm_modeset_is_locked(&crtc->base.mutex));
 
 	vblank_start = mode->crtc_vblank_start;
 	if (mode->flags & DRM_MODE_FLAG_INTERLACE)
diff --git a/drivers/gpu/drm/msm/mdp/mdp4/mdp4_crtc.c b/drivers/gpu/drm/msm/mdp/mdp4/mdp4_crtc.c
index efa19c8..7cf0f78 100644
--- a/drivers/gpu/drm/msm/mdp/mdp4/mdp4_crtc.c
+++ b/drivers/gpu/drm/msm/mdp/mdp4/mdp4_crtc.c
@@ -198,9 +198,9 @@  static void unref_fb_worker(struct drm_flip_work *work, void *val)
 		container_of(work, struct mdp4_crtc, unref_fb_work);
 	struct drm_device *dev = mdp4_crtc->base.dev;
 
-	mutex_lock(&dev->mode_config.mutex);
+	drm_modeset_lock(&dev->mode_config.mutex, NULL);
 	drm_framebuffer_unreference(val);
-	mutex_unlock(&dev->mode_config.mutex);
+	drm_modeset_unlock(&dev->mode_config.mutex);
 }
 
 static void unref_cursor_worker(struct drm_flip_work *work, void *val)
diff --git a/drivers/gpu/drm/msm/mdp/mdp5/mdp5_crtc.c b/drivers/gpu/drm/msm/mdp/mdp5/mdp5_crtc.c
index ff48944..771390b 100644
--- a/drivers/gpu/drm/msm/mdp/mdp5/mdp5_crtc.c
+++ b/drivers/gpu/drm/msm/mdp/mdp5/mdp5_crtc.c
@@ -186,9 +186,9 @@  static void unref_fb_worker(struct drm_flip_work *work, void *val)
 		container_of(work, struct mdp5_crtc, unref_fb_work);
 	struct drm_device *dev = mdp5_crtc->base.dev;
 
-	mutex_lock(&dev->mode_config.mutex);
+	drm_modeset_lock(&dev->mode_config.mutex, NULL);
 	drm_framebuffer_unreference(val);
-	mutex_unlock(&dev->mode_config.mutex);
+	drm_modeset_unlock(&dev->mode_config.mutex);
 }
 
 static void mdp5_crtc_destroy(struct drm_crtc *crtc)
diff --git a/drivers/gpu/drm/omapdrm/omap_crtc.c b/drivers/gpu/drm/omapdrm/omap_crtc.c
index a7c0010..a75934d 100644
--- a/drivers/gpu/drm/omapdrm/omap_crtc.c
+++ b/drivers/gpu/drm/omapdrm/omap_crtc.c
@@ -319,13 +319,13 @@  static void page_flip_worker(struct work_struct *work)
 	struct drm_display_mode *mode = &crtc->mode;
 	struct drm_gem_object *bo;
 
-	mutex_lock(&crtc->mutex);
+	drm_modeset_lock(&crtc->mutex, NULL);
 	omap_plane_mode_set(omap_crtc->plane, crtc, crtc->primary->fb,
 			0, 0, mode->hdisplay, mode->vdisplay,
 			crtc->x << 16, crtc->y << 16,
 			mode->hdisplay << 16, mode->vdisplay << 16,
 			vblank_cb, crtc);
-	mutex_unlock(&crtc->mutex);
+	drm_modeset_unlock(&crtc->mutex);
 
 	bo = omap_framebuffer_bo(crtc->primary->fb, 0);
 	drm_gem_object_unreference_unlocked(bo);
@@ -467,7 +467,7 @@  static void apply_worker(struct work_struct *work)
 	 * the callbacks and list modification all serialized
 	 * with respect to modesetting ioctls from userspace.
 	 */
-	mutex_lock(&crtc->mutex);
+	drm_modeset_lock(&crtc->mutex, NULL);
 	dispc_runtime_get();
 
 	/*
@@ -512,7 +512,7 @@  static void apply_worker(struct work_struct *work)
 
 out:
 	dispc_runtime_put();
-	mutex_unlock(&crtc->mutex);
+	drm_modeset_unlock(&crtc->mutex);
 }
 
 int omap_crtc_apply(struct drm_crtc *crtc,
@@ -520,7 +520,7 @@  int omap_crtc_apply(struct drm_crtc *crtc,
 {
 	struct omap_crtc *omap_crtc = to_omap_crtc(crtc);
 
-	WARN_ON(!mutex_is_locked(&crtc->mutex));
+	WARN_ON(!drm_modeset_is_locked(&crtc->mutex));
 
 	/* no need to queue it again if it is already queued: */
 	if (apply->queued)
diff --git a/drivers/gpu/drm/omapdrm/omap_plane.c b/drivers/gpu/drm/omapdrm/omap_plane.c
index 8ae5c49..2d3c975 100644
--- a/drivers/gpu/drm/omapdrm/omap_plane.c
+++ b/drivers/gpu/drm/omapdrm/omap_plane.c
@@ -72,9 +72,9 @@  static void unpin_worker(struct drm_flip_work *work, void *val)
 	struct drm_device *dev = omap_plane->base.dev;
 
 	omap_framebuffer_unpin(val);
-	mutex_lock(&dev->mode_config.mutex);
+	drm_modeset_lock(&dev->mode_config.mutex, NULL);
 	drm_framebuffer_unreference(val);
-	mutex_unlock(&dev->mode_config.mutex);
+	drm_modeset_unlock(&dev->mode_config.mutex);
 }
 
 /* update which fb (if any) is pinned for scanout */
diff --git a/drivers/gpu/drm/tilcdc/tilcdc_crtc.c b/drivers/gpu/drm/tilcdc/tilcdc_crtc.c
index d642d4a0..92839ba 100644
--- a/drivers/gpu/drm/tilcdc/tilcdc_crtc.c
+++ b/drivers/gpu/drm/tilcdc/tilcdc_crtc.c
@@ -45,9 +45,9 @@  static void unref_worker(struct drm_flip_work *work, void *val)
 		container_of(work, struct tilcdc_crtc, unref_work);
 	struct drm_device *dev = tilcdc_crtc->base.dev;
 
-	mutex_lock(&dev->mode_config.mutex);
+	drm_modeset_lock(&dev->mode_config.mutex, NULL);
 	drm_framebuffer_unreference(val);
-	mutex_unlock(&dev->mode_config.mutex);
+	drm_modeset_unlock(&dev->mode_config.mutex);
 }
 
 static void set_scanout(struct drm_crtc *crtc, int n)
diff --git a/drivers/gpu/drm/vmwgfx/vmwgfx_kms.c b/drivers/gpu/drm/vmwgfx/vmwgfx_kms.c
index 6b9db5e..f338e3e 100644
--- a/drivers/gpu/drm/vmwgfx/vmwgfx_kms.c
+++ b/drivers/gpu/drm/vmwgfx/vmwgfx_kms.c
@@ -187,7 +187,7 @@  int vmw_du_crtc_cursor_set(struct drm_crtc *crtc, struct drm_file *file_priv,
 	 * can do this since the caller in the drm core doesn't check anything
 	 * which is protected by any looks.
 	 */
-	mutex_unlock(&crtc->mutex);
+	drm_modeset_unlock(&crtc->mutex);
 	drm_modeset_lock_all(dev_priv->dev);
 
 	/* A lot of the code assumes this */
@@ -252,7 +252,7 @@  int vmw_du_crtc_cursor_set(struct drm_crtc *crtc, struct drm_file *file_priv,
 	ret = 0;
 out:
 	drm_modeset_unlock_all(dev_priv->dev);
-	mutex_lock(&crtc->mutex);
+	drm_modeset_lock(&crtc->mutex, NULL);
 
 	return ret;
 }
@@ -273,7 +273,7 @@  int vmw_du_crtc_cursor_move(struct drm_crtc *crtc, int x, int y)
 	 * can do this since the caller in the drm core doesn't check anything
 	 * which is protected by any looks.
 	 */
-	mutex_unlock(&crtc->mutex);
+	drm_modeset_unlock(&crtc->mutex);
 	drm_modeset_lock_all(dev_priv->dev);
 
 	vmw_cursor_update_position(dev_priv, shown,
@@ -281,7 +281,7 @@  int vmw_du_crtc_cursor_move(struct drm_crtc *crtc, int x, int y)
 				   du->cursor_y + du->hotspot_y);
 
 	drm_modeset_unlock_all(dev_priv->dev);
-	mutex_lock(&crtc->mutex);
+	drm_modeset_lock(&crtc->mutex, NULL);
 
 	return 0;
 }
@@ -387,7 +387,7 @@  void vmw_kms_cursor_post_execbuf(struct vmw_private *dev_priv)
 	struct vmw_display_unit *du;
 	struct drm_crtc *crtc;
 
-	mutex_lock(&dev->mode_config.mutex);
+	drm_modeset_lock(&dev->mode_config.mutex, NULL);
 
 	list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
 		du = vmw_crtc_to_du(crtc);
@@ -401,7 +401,7 @@  void vmw_kms_cursor_post_execbuf(struct vmw_private *dev_priv)
 					64, 64, du->hotspot_x, du->hotspot_y);
 	}
 
-	mutex_unlock(&dev->mode_config.mutex);
+	drm_modeset_unlock(&dev->mode_config.mutex);
 }
 
 /*
@@ -1506,7 +1506,7 @@  int vmw_kms_cursor_bypass_ioctl(struct drm_device *dev, void *data,
 	int ret = 0;
 
 
-	mutex_lock(&dev->mode_config.mutex);
+	drm_modeset_lock(&dev->mode_config.mutex, NULL);
 	if (arg->flags & DRM_VMW_CURSOR_BYPASS_ALL) {
 
 		list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
@@ -1515,7 +1515,7 @@  int vmw_kms_cursor_bypass_ioctl(struct drm_device *dev, void *data,
 			du->hotspot_y = arg->yhot;
 		}
 
-		mutex_unlock(&dev->mode_config.mutex);
+		drm_modeset_unlock(&dev->mode_config.mutex);
 		return 0;
 	}
 
@@ -1532,7 +1532,7 @@  int vmw_kms_cursor_bypass_ioctl(struct drm_device *dev, void *data,
 	du->hotspot_y = arg->yhot;
 
 out:
-	mutex_unlock(&dev->mode_config.mutex);
+	drm_modeset_unlock(&dev->mode_config.mutex);
 
 	return ret;
 }
@@ -1682,7 +1682,7 @@  static int vmw_du_update_layout(struct vmw_private *dev_priv, unsigned num,
 	struct vmw_display_unit *du;
 	struct drm_connector *con;
 
-	mutex_lock(&dev->mode_config.mutex);
+	drm_modeset_lock(&dev->mode_config.mutex, NULL);
 
 #if 0
 	{
@@ -1712,7 +1712,7 @@  static int vmw_du_update_layout(struct vmw_private *dev_priv, unsigned num,
 		con->status = vmw_du_connector_detect(con, true);
 	}
 
-	mutex_unlock(&dev->mode_config.mutex);
+	drm_modeset_unlock(&dev->mode_config.mutex);
 
 	return 0;
 }
diff --git a/include/drm/drmP.h b/include/drm/drmP.h
index e8d0d17..5ac3587 100644
--- a/include/drm/drmP.h
+++ b/include/drm/drmP.h
@@ -1264,11 +1264,6 @@  static inline int drm_device_is_unplugged(struct drm_device *dev)
 	return ret;
 }
 
-static inline bool drm_modeset_is_locked(struct drm_device *dev)
-{
-	return mutex_is_locked(&dev->mode_config.mutex);
-}
-
 static inline bool drm_is_render_client(const struct drm_file *file_priv)
 {
 	return file_priv->minor->type == DRM_MINOR_RENDER;
diff --git a/include/drm/drm_atomic.h b/include/drm/drm_atomic.h
index 5274212..ff72b81 100644
--- a/include/drm/drm_atomic.h
+++ b/include/drm/drm_atomic.h
@@ -80,6 +80,8 @@  int drm_atomic_set_event(struct drm_device *dev,
 		struct drm_pending_vblank_event *event);
 int drm_atomic_check(struct drm_device *dev, struct drm_atomic_state *state);
 int drm_atomic_commit(struct drm_device *dev, struct drm_atomic_state *state);
+int drm_atomic_commit_unlocked(struct drm_device *dev,
+		struct drm_atomic_state *state);
 void drm_atomic_end(struct drm_device *dev, struct drm_atomic_state *state);
 
 /**
@@ -89,6 +91,11 @@  struct drm_atomic_state {
 	struct kref refcount;
 	struct drm_device *dev;
 	uint32_t flags;
+
+	bool committed;
+	bool checked;       /* just for debugging */
+
+	struct drm_modeset_acquire_ctx acquire_ctx;
 };
 
 static inline void
diff --git a/include/drm/drm_crtc.h b/include/drm/drm_crtc.h
index 4790cad..83e0f88 100644
--- a/include/drm/drm_crtc.h
+++ b/include/drm/drm_crtc.h
@@ -33,6 +33,7 @@ 
 #include <linux/hdmi.h>
 #include <drm/drm_mode.h>
 #include <drm/drm_fourcc.h>
+#include <drm/drm_modeset_lock.h>
 
 struct drm_device;
 struct drm_mode_set;
@@ -194,6 +195,10 @@  struct drm_property {
 	struct list_head enum_blob_list;
 };
 
+void drm_modeset_lock_all(struct drm_device *dev);
+void drm_modeset_unlock_all(struct drm_device *dev);
+void drm_warn_on_modeset_not_all_locked(struct drm_device *dev);
+
 struct drm_crtc;
 struct drm_connector;
 struct drm_encoder;
@@ -273,6 +278,7 @@  struct drm_crtc_funcs {
  * drm_crtc - central CRTC control structure
  * @dev: parent DRM device
  * @head: list management
+ * @mutex: per-CRTC locking
  * @base: base KMS object for ID tracking etc.
  * @primary: primary plane for this CRTC
  * @cursor: cursor plane for this CRTC
@@ -307,7 +313,7 @@  struct drm_crtc {
 	 * state, ...) and a write lock for everything which can be update
 	 * without a full modeset (fb, cursor data, ...)
 	 */
-	struct mutex mutex;
+	struct drm_modeset_lock mutex;
 
 	struct drm_mode_object base;
 
@@ -729,7 +735,12 @@  struct drm_mode_group {
  * global restrictions are also here, e.g. dimension restrictions.
  */
 struct drm_mode_config {
-	struct mutex mutex; /* protects configuration (mode lists etc.) */
+
+	/* protects configuration (mode lists etc.): */
+	struct drm_modeset_lock mutex;
+	/* the current acquire ctx (for drm_modeset_lock_all()) */
+	struct drm_modeset_acquire_ctx *acquire_ctx;
+
 	struct mutex idr_mutex; /* for IDR management */
 	struct idr crtc_idr; /* use this idr for all IDs, fb, crtc, connector, modes - just makes life easier */
 	/* this is limited to one for now */
@@ -830,10 +841,6 @@  struct drm_prop_enum_list {
 	char *name;
 };
 
-extern void drm_modeset_lock_all(struct drm_device *dev);
-extern void drm_modeset_unlock_all(struct drm_device *dev);
-extern void drm_warn_on_modeset_not_all_locked(struct drm_device *dev);
-
 extern int drm_crtc_init_with_planes(struct drm_device *dev,
 				     struct drm_crtc *crtc,
 				     struct drm_plane *primary,
diff --git a/include/drm/drm_modeset_lock.h b/include/drm/drm_modeset_lock.h
new file mode 100644
index 0000000..bc83a10
--- /dev/null
+++ b/include/drm/drm_modeset_lock.h
@@ -0,0 +1,130 @@ 
+/*
+ * Copyright (C) 2014 Red Hat
+ * Author: Rob Clark <robdclark@gmail.com>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the "Software"),
+ * to deal in the Software without restriction, including without limitation
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
+ * and/or sell copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
+ * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
+ * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
+ * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
+ * OTHER DEALINGS IN THE SOFTWARE.
+ */
+
+#ifndef DRM_MODESET_LOCK_H_
+#define DRM_MODESET_LOCK_H_
+
+#include <linux/ww_mutex.h>
+
+struct drm_modeset_acquire_ctx {
+
+	struct ww_acquire_ctx ww_ctx;
+
+	bool nolock : 1;
+	bool nonblock : 1;
+
+	/* just for debugging, the context is 'frozen' in drm_atomic_check()
+	 * to catch anyone who might be trying to acquire a lock after it is
+	 * too late.
+	 */
+	bool frozen : 1;
+
+	/* list of 'struct drm_modeset_lock': */
+	struct list_head locked;
+
+	/* currently simply for protecting against 'locked' list manipulation
+	 * between original thread calling atomic->end() and driver thread
+	 * calling back drm_atomic_commit_unlocked().
+	 *
+	 * Other spots are sufficiently synchronized by virtue of holding
+	 * the lock's ww_mutex.  But during the lock/resource hand-over to the
+	 * driver thread (drop_locks()/grab_locks()), we cannot rely on this.
+	 */
+	struct mutex mutex;
+};
+
+/**
+ * drm_modeset_lock - used for locking modeset resources.
+ * @mutex: resource locking
+ * @atomic_pending: is this resource part of a still-pending
+ *    atomic update
+ * @head: used to hold it's place on state->locked list when
+ *    part of an atomic update
+ *
+ * Used for locking CRTCs and other modeset resources.
+ */
+struct drm_modeset_lock {
+	/**
+	 * modeset lock
+	 */
+	struct ww_mutex mutex;
+
+	/**
+	 * Are we busy (pending asynchronous/NONBLOCK update)?  Any further
+	 * asynchronous update will return -EBUSY if it also needs to acquire
+	 * this lock.  While a synchronous update will block until the pending
+	 * async update completes.
+	 *
+	 * Drivers must ensure the update is completed before sending vblank
+	 * event to userspace.  Typically this just means don't send event
+	 * before drm_atomic_commit_unlocked() returns.
+	 */
+	bool atomic_pending;
+
+	/**
+	 * Resources that are locked as part of an atomic update are added
+	 * to a list (so we know what to unlock at the end).
+	 */
+	struct list_head head;
+
+	/**
+	 * For waiting on atomic_pending locks, if not a NONBLOCK operation.
+	 */
+	wait_queue_head_t event;
+};
+
+extern struct ww_class crtc_ww_class;
+
+void drm_modeset_acquire_init(struct drm_modeset_acquire_ctx *ctx,
+		bool nolock, bool nonblock);
+void drm_modeset_acquire_fini(struct drm_modeset_acquire_ctx *ctx);
+void drm_modeset_drop_locks(struct drm_modeset_acquire_ctx *ctx);
+
+static inline void drm_modeset_lock_init(struct drm_modeset_lock *lock)
+{
+	ww_mutex_init(&lock->mutex, &crtc_ww_class);
+	INIT_LIST_HEAD(&lock->head);
+	init_waitqueue_head(&lock->event);
+}
+
+static inline void drm_modeset_lock_fini(struct drm_modeset_lock *lock)
+{
+	WARN_ON(!list_empty(&lock->head));
+}
+
+static inline bool drm_modeset_is_locked(struct drm_modeset_lock *lock)
+{
+	return ww_mutex_is_locked(&lock->mutex);
+}
+
+int drm_modeset_lock(struct drm_modeset_lock *lock,
+		struct drm_modeset_acquire_ctx *ctx);
+int drm_modeset_lock_interruptible(struct drm_modeset_lock *lock,
+		struct drm_modeset_acquire_ctx *ctx);
+void drm_modeset_unlock(struct drm_modeset_lock *lock);
+
+struct drm_device;
+int drm_modeset_lock_all_crtcs(struct drm_device *dev,
+		struct drm_modeset_acquire_ctx *ctx);
+
+#endif /* DRM_MODESET_LOCK_H_ */
diff --git a/include/uapi/drm/drm_mode.h b/include/uapi/drm/drm_mode.h
index f104c26..12e2139 100644
--- a/include/uapi/drm/drm_mode.h
+++ b/include/uapi/drm/drm_mode.h
@@ -496,4 +496,7 @@  struct drm_mode_destroy_dumb {
 	uint32_t handle;
 };
 
+#define DRM_MODE_ATOMIC_NONBLOCK  0x0200
+#define DRM_MODE_ATOMIC_NOLOCK    0x8000  /* only used internally */
+
 #endif