Skip to content

Commit 04ad505

Browse files
kerneltoastgregkh
authored andcommitted
drm/i915: Fix ref->mutex deadlock in i915_active_wait()
The following deadlock exists in i915_active_wait() due to a double lock on ref->mutex (call chain listed in order from top to bottom): i915_active_wait(); mutex_lock_interruptible(&ref->mutex); <-- ref->mutex first acquired i915_active_request_retire(); node_retire(); active_retire(); mutex_lock_nested(&ref->mutex, SINGLE_DEPTH_NESTING); <-- DEADLOCK Fix the deadlock by skipping the second ref->mutex lock when active_retire() is called through i915_active_request_retire(). Note that this bug only affects 5.4 and has since been fixed in 5.5. Normally, a backport of the fix from 5.5 would be in order, but the patch set that fixes this deadlock involves massive changes that are neither feasible nor desirable for backporting [1][2][3]. Therefore, this small patch was made to address the deadlock specifically for 5.4. [1] 274cbf2 ("drm/i915: Push the i915_active.retire into a worker") [2] 093b922 ("drm/i915: Split i915_active.mutex into an irq-safe spinlock for the rbtree") [3] 750bde2 ("drm/i915: Serialise with remote retirement") Fixes: 12c255b ("drm/i915: Provide an i915_active.acquire callback") Cc: <stable@vger.kernel.org> # 5.4.x Signed-off-by: Sultan Alsawaf <sultan@kerneltoast.com> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
1 parent 047affa commit 04ad505

2 files changed

Lines changed: 21 additions & 12 deletions

File tree

drivers/gpu/drm/i915/i915_active.c

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ static inline void debug_active_assert(struct i915_active *ref) { }
121121
#endif
122122

123123
static void
124-
__active_retire(struct i915_active *ref)
124+
__active_retire(struct i915_active *ref, bool lock)
125125
{
126126
struct active_node *it, *n;
127127
struct rb_root root;
@@ -138,7 +138,8 @@ __active_retire(struct i915_active *ref)
138138
retire = true;
139139
}
140140

141-
mutex_unlock(&ref->mutex);
141+
if (likely(lock))
142+
mutex_unlock(&ref->mutex);
142143
if (!retire)
143144
return;
144145

@@ -153,21 +154,28 @@ __active_retire(struct i915_active *ref)
153154
}
154155

155156
static void
156-
active_retire(struct i915_active *ref)
157+
active_retire(struct i915_active *ref, bool lock)
157158
{
158159
GEM_BUG_ON(!atomic_read(&ref->count));
159160
if (atomic_add_unless(&ref->count, -1, 1))
160161
return;
161162

162163
/* One active may be flushed from inside the acquire of another */
163-
mutex_lock_nested(&ref->mutex, SINGLE_DEPTH_NESTING);
164-
__active_retire(ref);
164+
if (likely(lock))
165+
mutex_lock_nested(&ref->mutex, SINGLE_DEPTH_NESTING);
166+
__active_retire(ref, lock);
165167
}
166168

167169
static void
168170
node_retire(struct i915_active_request *base, struct i915_request *rq)
169171
{
170-
active_retire(node_from_active(base)->ref);
172+
active_retire(node_from_active(base)->ref, true);
173+
}
174+
175+
static void
176+
node_retire_nolock(struct i915_active_request *base, struct i915_request *rq)
177+
{
178+
active_retire(node_from_active(base)->ref, false);
171179
}
172180

173181
static struct i915_active_request *
@@ -364,7 +372,7 @@ int i915_active_acquire(struct i915_active *ref)
364372
void i915_active_release(struct i915_active *ref)
365373
{
366374
debug_active_assert(ref);
367-
active_retire(ref);
375+
active_retire(ref, true);
368376
}
369377

370378
static void __active_ungrab(struct i915_active *ref)
@@ -391,7 +399,7 @@ void i915_active_ungrab(struct i915_active *ref)
391399
{
392400
GEM_BUG_ON(!test_bit(I915_ACTIVE_GRAB_BIT, &ref->flags));
393401

394-
active_retire(ref);
402+
active_retire(ref, true);
395403
__active_ungrab(ref);
396404
}
397405

@@ -421,12 +429,13 @@ int i915_active_wait(struct i915_active *ref)
421429
break;
422430
}
423431

424-
err = i915_active_request_retire(&it->base, BKL(ref));
432+
err = i915_active_request_retire(&it->base, BKL(ref),
433+
node_retire_nolock);
425434
if (err)
426435
break;
427436
}
428437

429-
__active_retire(ref);
438+
__active_retire(ref, true);
430439
if (err)
431440
return err;
432441

drivers/gpu/drm/i915/i915_active.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -309,7 +309,7 @@ i915_active_request_isset(const struct i915_active_request *active)
309309
*/
310310
static inline int __must_check
311311
i915_active_request_retire(struct i915_active_request *active,
312-
struct mutex *mutex)
312+
struct mutex *mutex, i915_active_retire_fn retire)
313313
{
314314
struct i915_request *request;
315315
long ret;
@@ -327,7 +327,7 @@ i915_active_request_retire(struct i915_active_request *active,
327327
list_del_init(&active->link);
328328
RCU_INIT_POINTER(active->request, NULL);
329329

330-
active->retire(active, request);
330+
retire(active, request);
331331

332332
return 0;
333333
}

0 commit comments

Comments
 (0)