Skip to content

Commit 142c6a6

Browse files
icklegregkh
authored andcommitted
drm/i915: Check for rq->hwsp validity after acquiring RCU lock
commit 45db630 upstream. Since we allow removing the timeline map at runtime, there is a risk that rq->hwsp points into a stale page. To control that risk, we hold the RCU read lock while reading *rq->hwsp, but we missed a couple of important barriers. First, the unpinning / removal of the timeline map must be after all RCU readers into that map are complete, i.e. after an rcu barrier (in this case courtesy of call_rcu()). Secondly, we must make sure that the rq->hwsp we are about to dereference under the RCU lock is valid. In this case, we make the rq->hwsp pointer safe during i915_request_retire() and so we know that rq->hwsp may become invalid only after the request has been signaled. Therefore is the request is not yet signaled when we acquire rq->hwsp under the RCU, we know that rq->hwsp will remain valid for the duration of the RCU read lock. This is a very small window that may lead to either considering the request not completed (causing a delay until the request is checked again, any wait for the request is not affected) or dereferencing an invalid pointer. Fixes: 3adac46 ("drm/i915: Introduce concept of per-timeline (context) HWSP") Signed-off-by: Chris Wilson <chris@chris-wilson.co.uk> Cc: Tvrtko Ursulin <tvrtko.ursulin@intel.com> Cc: <stable@vger.kernel.org> # v5.1+ Reviewed-by: Tvrtko Ursulin <tvrtko.ursulin@intel.com> Link: https://patchwork.freedesktop.org/patch/msgid/20201218122421.18344-1-chris@chris-wilson.co.uk (cherry picked from commit 9bb36cf) Signed-off-by: Jani Nikula <jani.nikula@intel.com> Link: https://patchwork.freedesktop.org/patch/msgid/20210118101755.476744-1-chris@chris-wilson.co.uk Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
1 parent bdab6bd commit 142c6a6

3 files changed

Lines changed: 38 additions & 18 deletions

File tree

‎drivers/gpu/drm/i915/gt/intel_breadcrumbs.c‎

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -134,11 +134,6 @@ static bool remove_signaling_context(struct intel_breadcrumbs *b,
134134
return true;
135135
}
136136

137-
static inline bool __request_completed(const struct i915_request *rq)
138-
{
139-
return i915_seqno_passed(__hwsp_seqno(rq), rq->fence.seqno);
140-
}
141-
142137
__maybe_unused static bool
143138
check_signal_order(struct intel_context *ce, struct i915_request *rq)
144139
{
@@ -257,7 +252,7 @@ static void signal_irq_work(struct irq_work *work)
257252
list_for_each_entry_rcu(rq, &ce->signals, signal_link) {
258253
bool release;
259254

260-
if (!__request_completed(rq))
255+
if (!__i915_request_is_complete(rq))
261256
break;
262257

263258
if (!test_and_clear_bit(I915_FENCE_FLAG_SIGNAL,
@@ -379,7 +374,7 @@ static void insert_breadcrumb(struct i915_request *rq)
379374
* straight onto a signaled list, and queue the irq worker for
380375
* its signal completion.
381376
*/
382-
if (__request_completed(rq)) {
377+
if (__i915_request_is_complete(rq)) {
383378
if (__signal_request(rq) &&
384379
llist_add(&rq->signal_node, &b->signaled_requests))
385380
irq_work_queue(&b->irq_work);

‎drivers/gpu/drm/i915/gt/intel_timeline.c‎

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -126,18 +126,17 @@ static void __rcu_cacheline_free(struct rcu_head *rcu)
126126
struct intel_timeline_cacheline *cl =
127127
container_of(rcu, typeof(*cl), rcu);
128128

129+
/* Must wait until after all *rq->hwsp are complete before removing */
130+
i915_gem_object_unpin_map(cl->hwsp->vma->obj);
131+
__idle_hwsp_free(cl->hwsp, ptr_unmask_bits(cl->vaddr, CACHELINE_BITS));
132+
129133
i915_active_fini(&cl->active);
130134
kfree(cl);
131135
}
132136

133137
static void __idle_cacheline_free(struct intel_timeline_cacheline *cl)
134138
{
135139
GEM_BUG_ON(!i915_active_is_idle(&cl->active));
136-
137-
i915_gem_object_unpin_map(cl->hwsp->vma->obj);
138-
i915_vma_put(cl->hwsp->vma);
139-
__idle_hwsp_free(cl->hwsp, ptr_unmask_bits(cl->vaddr, CACHELINE_BITS));
140-
141140
call_rcu(&cl->rcu, __rcu_cacheline_free);
142141
}
143142

@@ -179,7 +178,6 @@ cacheline_alloc(struct intel_timeline_hwsp *hwsp, unsigned int cacheline)
179178
return ERR_CAST(vaddr);
180179
}
181180

182-
i915_vma_get(hwsp->vma);
183181
cl->hwsp = hwsp;
184182
cl->vaddr = page_pack_bits(vaddr, cacheline);
185183

‎drivers/gpu/drm/i915/i915_request.h‎

Lines changed: 32 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -434,7 +434,7 @@ static inline u32 hwsp_seqno(const struct i915_request *rq)
434434

435435
static inline bool __i915_request_has_started(const struct i915_request *rq)
436436
{
437-
return i915_seqno_passed(hwsp_seqno(rq), rq->fence.seqno - 1);
437+
return i915_seqno_passed(__hwsp_seqno(rq), rq->fence.seqno - 1);
438438
}
439439

440440
/**
@@ -465,11 +465,19 @@ static inline bool __i915_request_has_started(const struct i915_request *rq)
465465
*/
466466
static inline bool i915_request_started(const struct i915_request *rq)
467467
{
468+
bool result;
469+
468470
if (i915_request_signaled(rq))
469471
return true;
470472

471-
/* Remember: started but may have since been preempted! */
472-
return __i915_request_has_started(rq);
473+
result = true;
474+
rcu_read_lock(); /* the HWSP may be freed at runtime */
475+
if (likely(!i915_request_signaled(rq)))
476+
/* Remember: started but may have since been preempted! */
477+
result = __i915_request_has_started(rq);
478+
rcu_read_unlock();
479+
480+
return result;
473481
}
474482

475483
/**
@@ -482,10 +490,16 @@ static inline bool i915_request_started(const struct i915_request *rq)
482490
*/
483491
static inline bool i915_request_is_running(const struct i915_request *rq)
484492
{
493+
bool result;
494+
485495
if (!i915_request_is_active(rq))
486496
return false;
487497

488-
return __i915_request_has_started(rq);
498+
rcu_read_lock();
499+
result = __i915_request_has_started(rq) && i915_request_is_active(rq);
500+
rcu_read_unlock();
501+
502+
return result;
489503
}
490504

491505
/**
@@ -509,12 +523,25 @@ static inline bool i915_request_is_ready(const struct i915_request *rq)
509523
return !list_empty(&rq->sched.link);
510524
}
511525

526+
static inline bool __i915_request_is_complete(const struct i915_request *rq)
527+
{
528+
return i915_seqno_passed(__hwsp_seqno(rq), rq->fence.seqno);
529+
}
530+
512531
static inline bool i915_request_completed(const struct i915_request *rq)
513532
{
533+
bool result;
534+
514535
if (i915_request_signaled(rq))
515536
return true;
516537

517-
return i915_seqno_passed(hwsp_seqno(rq), rq->fence.seqno);
538+
result = true;
539+
rcu_read_lock(); /* the HWSP may be freed at runtime */
540+
if (likely(!i915_request_signaled(rq)))
541+
result = __i915_request_is_complete(rq);
542+
rcu_read_unlock();
543+
544+
return result;
518545
}
519546

520547
static inline void i915_request_mark_complete(struct i915_request *rq)

0 commit comments

Comments
 (0)