Skip to content

Commit 0f5225b

Browse files
Peter Zijlstraingomolnar
authored andcommitted
locking/mutex, drm: Introduce mutex_trylock_recursive()
By popular DRM demand, introduce mutex_trylock_recursive() to fix up the two GEM users. Without this it is very easy for these drivers to get stuck in low-memory situations and trigger OOM. Work is in progress to remove the need for this in at least i915. Signed-off-by: Peter Zijlstra (Intel) <peterz@infradead.org> Cc: Chris Wilson <chris@chris-wilson.co.uk> Cc: Daniel Vetter <daniel.vetter@ffwll.ch> Cc: David Airlie <airlied@linux.ie> Cc: Davidlohr Bueso <dave@stgolabs.net> Cc: Ding Tianhong <dingtianhong@huawei.com> Cc: Imre Deak <imre.deak@intel.com> Cc: Jason Low <jason.low2@hpe.com> Cc: Linus Torvalds <torvalds@linux-foundation.org> Cc: Paul E. McKenney <paulmck@us.ibm.com> Cc: Peter Zijlstra <peterz@infradead.org> Cc: Rob Clark <robdclark@gmail.com> Cc: Terry Rudd <terry.rudd@hpe.com> Cc: Thomas Gleixner <tglx@linutronix.de> Cc: Tim Chen <tim.c.chen@linux.intel.com> Cc: Will Deacon <Will.Deacon@arm.com> Cc: dri-devel@lists.freedesktop.org Cc: linux-kernel@vger.kernel.org Signed-off-by: Ingo Molnar <mingo@kernel.org>
1 parent 83f0616 commit 0f5225b

4 files changed

Lines changed: 61 additions & 7 deletions

File tree

drivers/gpu/drm/i915/i915_gem_shrinker.c

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -227,11 +227,20 @@ unsigned long i915_gem_shrink_all(struct drm_i915_private *dev_priv)
227227

228228
static bool i915_gem_shrinker_lock(struct drm_device *dev, bool *unlock)
229229
{
230-
if (!mutex_trylock(&dev->struct_mutex))
230+
switch (mutex_trylock_recursive(&dev->struct_mutex)) {
231+
case MUTEX_TRYLOCK_FAILED:
231232
return false;
232233

233-
*unlock = true;
234-
return true;
234+
case MUTEX_TRYLOCK_SUCCESS:
235+
*unlock = true;
236+
return true;
237+
238+
case MUTEX_TRYLOCK_RECURSIVE:
239+
*unlock = false;
240+
return true;
241+
}
242+
243+
BUG();
235244
}
236245

237246
static unsigned long

drivers/gpu/drm/msm/msm_gem_shrinker.c

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,21 @@
2020

2121
static bool msm_gem_shrinker_lock(struct drm_device *dev, bool *unlock)
2222
{
23-
if (!mutex_trylock(&dev->struct_mutex))
23+
switch (mutex_trylock_recursive(&dev->struct_mutex)) {
24+
case MUTEX_TRYLOCK_FAILED:
2425
return false;
2526

26-
*unlock = true;
27-
return true;
28-
}
27+
case MUTEX_TRYLOCK_SUCCESS:
28+
*unlock = true;
29+
return true;
30+
31+
case MUTEX_TRYLOCK_RECURSIVE:
32+
*unlock = false;
33+
return true;
34+
}
2935

36+
BUG();
37+
}
3038

3139
static unsigned long
3240
msm_gem_shrinker_count(struct shrinker *shrinker, struct shrink_control *sc)

include/linux/mutex.h

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,4 +189,35 @@ extern void mutex_unlock(struct mutex *lock);
189189

190190
extern int atomic_dec_and_mutex_lock(atomic_t *cnt, struct mutex *lock);
191191

192+
/*
193+
* These values are chosen such that FAIL and SUCCESS match the
194+
* values of the regular mutex_trylock().
195+
*/
196+
enum mutex_trylock_recursive_enum {
197+
MUTEX_TRYLOCK_FAILED = 0,
198+
MUTEX_TRYLOCK_SUCCESS = 1,
199+
MUTEX_TRYLOCK_RECURSIVE,
200+
};
201+
202+
/**
203+
* mutex_trylock_recursive - trylock variant that allows recursive locking
204+
* @lock: mutex to be locked
205+
*
206+
* This function should not be used, _ever_. It is purely for hysterical GEM
207+
* raisins, and once those are gone this will be removed.
208+
*
209+
* Returns:
210+
* MUTEX_TRYLOCK_FAILED - trylock failed,
211+
* MUTEX_TRYLOCK_SUCCESS - lock acquired,
212+
* MUTEX_TRYLOCK_RECURSIVE - we already owned the lock.
213+
*/
214+
static inline __deprecated __must_check enum mutex_trylock_recursive_enum
215+
mutex_trylock_recursive(struct mutex *lock)
216+
{
217+
if (unlikely(__mutex_owner(lock) == current))
218+
return MUTEX_TRYLOCK_RECURSIVE;
219+
220+
return mutex_trylock(lock);
221+
}
222+
192223
#endif /* __LINUX_MUTEX_H */

scripts/checkpatch.pl

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6076,6 +6076,12 @@ sub process {
60766076
}
60776077
}
60786078

6079+
# check for mutex_trylock_recursive usage
6080+
if ($line =~ /mutex_trylock_recursive/) {
6081+
ERROR("LOCKING",
6082+
"recursive locking is bad, do not use this ever.\n" . $herecurr);
6083+
}
6084+
60796085
# check for lockdep_set_novalidate_class
60806086
if ($line =~ /^.\s*lockdep_set_novalidate_class\s*\(/ ||
60816087
$line =~ /__lockdep_no_validate__\s*\)/ ) {

0 commit comments

Comments
 (0)