drm/i915: All fixes to make i915 work well with PREEMPT_RT.
Subsystems (2)
v3 cover drm/i915: All fixes to make i915 work well with PREEMPT_RT.
All fixes to make the core i915 module (without display) working correctly on PREEMPT_RT.
Some fixes are still needed, as the selftests still fail. But otherwise the test results were looking good on PREEMPT_RT.
It looks like the patch that added a spinlock caused a regression on !PREEMPT_RT, and may even have caused similar test failures on PREEMPT_RT, so to fix it I removed RCU entirely, and converted to a locked implementation instead.
This time at least, i915 live selftests pass on !PREEMPRT_RT for at least 1 machine with same config as CI.
Changes since v2:
- Remove the extra signaler_active lock, it broke because it didn't protect anything, instead use signalers_lock.
Maarten Lankhorst (3):
drm/i915/gt: Fix selftests on PREEMPT_RT drm/i915/gt: Set stop_timeout() correctly on PREEMPT-RT drm/i915: Use sleeping selftests for igt_atomic on PREEMPT_RT
Sebastian Andrzej Siewior (4):
drm/i915/gt: Use spin_lock_irq() instead of local_irq_disable() +
spin_lock()
drm/i915: Drop the irqs_disabled() check
drm/i915/guc: Consider also RCU depth in busy loop. drm/i915/gt: Use signalers_lock to prevent starvation of irq_work.
drivers/gpu/drm/i915/gt/intel_breadcrumbs.c | 169 +++++++++++------- .../gpu/drm/i915/gt/intel_breadcrumbs_types.h | 1 - drivers/gpu/drm/i915/gt/intel_engine_cs.c | 2 +- .../drm/i915/gt/intel_execlists_submission.c | 17 +- drivers/gpu/drm/i915/gt/selftest_engine_pm.c | 8 +- drivers/gpu/drm/i915/gt/uc/intel_guc.h | 2 +- drivers/gpu/drm/i915/i915_request.c | 2 - drivers/gpu/drm/i915/selftests/igt_atomic.c | 7 + 8 files changed, 127 insertions(+), 81 deletions(-)
Signature · 1 line
2.53.0
PATCH v3 1/7 drm/i915/gt: Use spin_lock_irq() instead of local_irq_disable() + spin_lock()
From: Sebastian Andrzej Siewior <[email protected]>
execlists_dequeue() is invoked from a function which uses local_irq_disable() to disable interrupts so the spin_lock() behaves like spin_lock_irq().
This breaks PREEMPT_RT because local_irq_disable() + spin_lock() is not the same as spin_lock_irq().
execlists_dequeue_irq() and execlists_dequeue() has each one caller only. If intel_engine_cs::active::lock is acquired and released with the _irq suffix then it behaves almost as if execlists_dequeue() would be invoked with disabled interrupts. The difference is the last part of the function which is then invoked with enabled interrupts.
I can't tell if this makes a difference. From looking at it, it might work to move the last unlock at the end of the function as I didn't find anything that would acquire the lock again.
--- .../drm/i915/gt/intel_execlists_submission.c | 17 +++++------------ 1 file changed, 5 insertions(+), 12 deletions(-)
drivers/gpu/drm/i915/gt/intel_execlists_submission.c +5 -12
@@ -1300,7 +1300,7 @@ static void execlists_dequeue(struct intel_engine_cs *engine)* and context switches) submission. */ -spin_lock (&sched_engine->lock); +spin_lock_irq (&sched_engine->lock);/* * If the queue is higher priority than the last
@@ -1400,7 +1400,7 @@ static void execlists_dequeue(struct intel_engine_cs *engine)* Even if ELSP[1] is occupied and not worthy * of timeslices, our queue might be. */ -spin_unlock (&sched_engine->lock); +spin_unlock_irq (&sched_engine->lock);return ; } }
@@ -1426,7 +1426,7 @@ static void execlists_dequeue(struct intel_engine_cs *engine)if (last && !can_merge_rq (last, rq)) {spin_unlock (&ve->base.sched_engine->lock); -spin_unlock (&engine->sched_engine->lock); +spin_unlock_irq (&engine->sched_engine->lock);return ;/* leave this for another sibling */ }
@@ -1588,7 +1588,7 @@ static void execlists_dequeue(struct intel_engine_cs *engine)*/ sched_engine->queue_priority_hint =queue_prio (sched_engine);i915_sched_engine_reset_on_empty (sched_engine); -spin_unlock (&sched_engine->lock); +spin_unlock_irq (&sched_engine->lock);/* * We can skip poking the HW if we ended up with exactly the same set
@@ -1614,13 +1614,6 @@ static void execlists_dequeue(struct intel_engine_cs *engine) } } -static void execlists_dequeue_irq (struct intel_engine_cs *engine) -{ -local_irq_disable ();/* Suspend interrupts across request submission */ -execlists_dequeue (engine); -local_irq_enable ();/* flush irq_work (e.g. breadcrumb enabling) */ -} -static void clear_ports (struct i915_request **ports,int count) {memset_p ((void **)ports,NULL , count);
@@ -2475,7 +2468,7 @@ static void execlists_submission_tasklet(struct tasklet_struct *t) }if (!engine->execlists.pending[0 ]) { -execlists_dequeue_irq (engine); +execlists_dequeue (engine);start_timeslice (engine); }
Signature · 1 line
2.53.0
[email protected]
AI reviewRe: [PATCH v3 1/7] drm/i915/gt: Use spin_lock_irq() instead of local_irq_disable() + spin_lock()
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [High] Removing local_irq_disable() re-enables interrupts before and during ELSP hardware writes, risking…
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [High] Removing local_irq_disable() re-enables interrupts before and during ELSP hardware writes, risking incomplete submissions and GPU hangs. --
commit ff9840660b858aab1bd255c1588d895283bda28f
Author: Sebastian Andrzej Siewior <[email protected]>
drm/i915/gt: Use spin_lock_irq() instead of local_irq_disable() + spin_lock()
This commit replaces local_irq_disable() and spin_lock() with spin_lock_irq() in the execlists submission path to improve PREEMPT_RT compatibility. However, releasing the lock earlier in the function leaves the hardware ELSP MMIO writes exposed to interrupts and preemption.
Quoted 4 lines
drivers/gpu/drm/i915/gt/intel_execlists_submission.c +0
-0
[ ... ]
drivers/gpu/drm/i915/gt/intel_execlists_submission.c static void execlists_dequeue(struct intel_engine_cs *engine) · line 1591
Quoted 6 lines
drivers/gpu/drm/i915/gt/intel_execlists_submission.c +1
-1@@ -1588,7 +1588,7 @@ static void execlists_dequeue(struct intel_engine_cs *engine)*/ sched_engine->queue_priority_hint =queue_prio (sched_engine);i915_sched_engine_reset_on_empty (sched_engine); -spin_unlock (&sched_engine->lock); +spin_unlock_irq (&sched_engine->lock);
[Severity: High]
Does this change leave the hardware ELSP writes exposed to interrupts and preemption?
By removing local_irq_disable() around the entire execlists_dequeue() call and replacing it with spin_lock_irq() internally, interrupts are now re-enabled at this spin_unlock_irq() call.
This means the subsequent hardware writes in execlists_submit_ports() now occur with interrupts enabled:
execlists_dequeue() {
...
spin_unlock_irq(&sched_engine->lock);
/* ... */
if (submit && ...) {
...
execlists_submit_ports(engine);
}
}Since execlists_submit_ports() performs 4-DWord MMIO writes to the ELSP that must remain strictly atomic, could being interrupted or preempted during these writes result in incomplete context descriptors being fetched, potentially causing GPU hangs?
[ ... ]
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
PATCH v3 2/7 drm/i915: Drop the irqs_disabled() check
In reply to Maarten LankhorstFrom: Sebastian Andrzej Siewior <[email protected]>
The !irqs_disabled() check triggers on PREEMPT_RT even with i915_sched_engine::lock acquired. The reason is the lock is transformed into a sleeping lock on PREEMPT_RT and does not disable interrupts.
There is no need to check for disabled interrupts. The lockdep annotation below already check if the lock has been acquired by the caller and will yell if the interrupts are not disabled.
Remove the !irqs_disabled() check.
--- drivers/gpu/drm/i915/i915_request.c | 2 -- 1 file changed, 2 deletions(-)
drivers/gpu/drm/i915/i915_request.c +0 -2
@@ -610,7 +610,6 @@ bool __i915_request_submit(struct i915_request *request)RQ_TRACE (request,"\n" ); -GEM_BUG_ON (!irqs_disabled ());lockdep_assert_held (&engine->sched_engine->lock);/*
@@ -719,7 +718,6 @@ void __i915_request_unsubmit(struct i915_request *request)*/ RQ_TRACE (request,"\n" ); -GEM_BUG_ON (!irqs_disabled ());lockdep_assert_held (&engine->sched_engine->lock);/*
Signature · 1 line
2.53.0
PATCH v3 3/7 drm/i915/guc: Consider also RCU depth in busy loop.
In reply to Maarten LankhorstFrom: Sebastian Andrzej Siewior <[email protected]>
intel_guc_send_busy_loop() looks at in_atomic() and irqs_disabled() to decide if it should busy-spin while waiting or if it may sleep. Both checks will report false on PREEMPT_RT if sleeping spinlocks are acquired leading to RCU splats while the function sleeps.
Check also if RCU has been disabled.
--- drivers/gpu/drm/i915/gt/uc/intel_guc.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/gpu/drm/i915/gt/uc/intel_guc.h +1 -1
@@ -362,7 +362,7 @@ static inline int intel_guc_send_busy_loop(struct intel_guc *guc, {int err;unsigned int sleep_period_ms =1 ; -bool not_atomic = !in_atomic () && !irqs_disabled (); +bool not_atomic = !in_atomic () && !irqs_disabled () && !rcu_preempt_depth ();/* * FIXME: Have caller pass in if we are in an atomic context to avoid
Signature · 1 line
2.53.0
PATCH v3 4/7 drm/i915/gt: Fix selftests on PREEMPT_RT
In reply to Maarten Lankhorst--- drivers/gpu/drm/i915/gt/selftest_engine_pm.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-)
drivers/gpu/drm/i915/gt/selftest_engine_pm.c +4 -4
@@ -277,11 +277,11 @@ static int live_engine_busy_stats(void *arg)st_engine_heartbeat_disable (engine);ENGINE_TRACE (engine,"measuring idle time\n" ); -preempt_disable (); +migrate_disable (); de =intel_engine_get_busy_time (engine, &t[0 ]);udelay (100 ); de =ktime_sub (intel_engine_get_busy_time (engine, &t[1 ]), de); -preempt_enable (); +migrate_enable (); dt =ktime_sub (t[1 ], t[0 ]);if (de <0 || de >10 ) { pr_err("%s: reported %lldns [%d%%] busyness while sleeping [for %lldns]\n",
@@ -316,11 +316,11 @@ static int live_engine_busy_stats(void *arg) }ENGINE_TRACE (engine,"measuring busy time\n" ); -preempt_disable (); +migrate_disable (); de =intel_engine_get_busy_time (engine, &t[0 ]);mdelay (100 ); de =ktime_sub (intel_engine_get_busy_time (engine, &t[1 ]), de); -preempt_enable (); +migrate_enable (); dt =ktime_sub (t[1 ], t[0 ]);if (100 * de <95 * dt ||95 * de >100 * dt) { pr_err("%s: reported %lldns [%d%%] busyness while spinning [for %lldns]\n",
Signature · 1 line
2.53.0
PATCH v3 5/7 drm/i915/gt: Set stop_timeout() correctly on PREEMPT-RT
In reply to Maarten LankhorstAlso check if RCU is disabled for PREEMPT-RT, which is the case when local_bh_disable() is called.
--- drivers/gpu/drm/i915/gt/intel_engine_cs.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/gpu/drm/i915/gt/intel_engine_cs.c +1 -1
@@ -1607,7 +1607,7 @@ u64 intel_engine_get_last_batch_head(const struct intel_engine_cs *engine)static unsigned long stop_timeout (const struct intel_engine_cs *engine) { -if (in_atomic () ||irqs_disabled ())/* inside atomic preempt-reset? */ +if (in_atomic () ||irqs_disabled () ||rcu_preempt_depth ())/* inside atomic preempt-reset? */ return 0 ;/*
Signature · 1 line
2.53.0
PATCH v3 6/7 drm/i915: Use sleeping selftests for igt_atomic on PREEMPT_RT
In reply to Maarten LankhorstThis makes the i915 selftests slightly happier, especially related to GPU reset.
I believe this may be a better approach than trying to convert uncore->lock to raw_spinlock
--- drivers/gpu/drm/i915/selftests/igt_atomic.c | 7 +++++++ 1 file changed, 7 insertions(+)
drivers/gpu/drm/i915/selftests/igt_atomic.c +7 -0
@@ -39,7 +39,14 @@ static void __hardirq_end(void)local_irq_enable (); } +static void __maybe_unused __nop (void ) +{} +const struct igt_atomic_section igt_atomic_phases[] = { +#if IS_ENABLED(CONFIG_PREEMPT_RT) + {"sleeping" , __nop, __nop }, + { }, +#endif {"preempt" , __preempt_begin, __preempt_end }, {"softirq" , __softirq_begin, __softirq_end }, {"hardirq" , __hardirq_begin, __hardirq_end },
Signature · 1 line
2.53.0
PATCH v3 7/7 drm/i915/gt: Use signalers_lock to prevent starvation of irq_work.
In reply to Maarten LankhorstFrom: Sebastian Andrzej Siewior <[email protected]>
IRQ-Work (FIFO-1) will be preempted by the threaded-interrupt (FIFO-50) and the interrupt will poll on signaler_active while the irq-work can't make progress.
Solve this by adding a global spinlock to prevent starvation and force completion.
The existing RCU handling gets in the way on PREEMPT_RT, and would likely require conversion to raw spinlock to take them inside a rcu_read_lock(), so remove RCU as well.
--- drivers/gpu/drm/i915/gt/intel_breadcrumbs.c | 169 +++++++++++------- .../gpu/drm/i915/gt/intel_breadcrumbs_types.h | 1 - 2 files changed, 109 insertions(+), 61 deletions(-)
drivers/gpu/drm/i915/gt/intel_breadcrumbs.c +109 -60
@@ -88,24 +88,25 @@ static void add_signaling_context(struct intel_breadcrumbs *b, struct intel_context *ce) {lockdep_assert_held (&ce->signal_lock); +lockdep_assert_held (&b->signalers_lock); -spin_lock (&b->signalers_lock); -list_add_rcu (&ce->signal_link, &b->signalers); -spin_unlock (&b->signalers_lock); +if (list_empty (&ce->signals)) +return ; + +intel_context_get (ce); +list_add (&ce->signal_link, &b->signalers); }static bool remove_signaling_context (struct intel_breadcrumbs *b,struct intel_context *ce) {lockdep_assert_held (&ce->signal_lock); +lockdep_assert_held (&b->signalers_lock); -if (!list_empty (&ce->signals)) +if (!list_empty (&ce->signals) ||list_empty (&ce->signal_link))return false; -spin_lock (&b->signalers_lock); -list_del_rcu (&ce->signal_link); -spin_unlock (&b->signalers_lock); - +list_del_init (&ce->signal_link);return true; }
@@ -174,7 +175,7 @@ static void signal_irq_work(struct irq_work *work)struct intel_breadcrumbs *b =container_of (work,typeof (*b), irq_work);const ktime_t timestamp =ktime_get ();struct llist_node *signal, *sn; -struct intel_context *ce; +struct intel_context *ce, *next; signal =NULL ;if (unlikely (!llist_empty (&b->signaled_requests)))
@@ -208,13 +209,13 @@ static void signal_irq_work(struct irq_work *work)if (!signal &&READ_ONCE (b->irq_armed) &&list_empty (&b->signalers))intel_breadcrumbs_disarm_irq (b); -rcu_read_lock (); -atomic_inc (&b->signaler_active); -list_for_each_entry_rcu (ce, &b->signalers, signal_link) { +spin_lock (&b->signalers_lock); +list_for_each_entry_safe (ce, next, &b->signalers, signal_link) {struct i915_request *rq; +bool release; -list_for_each_entry_rcu (rq, &ce->signals, signal_link) { -bool release; +spin_lock (&ce->signal_lock); +while ((rq =list_first_entry_or_null (&ce->signals,typeof (*rq), signal_link))) {if (!__i915_request_is_complete (rq))break ;
@@ -228,15 +229,9 @@ static void signal_irq_work(struct irq_work *work)* spinlock as the callback chain may end up adding * more signalers to the same context or engine. */ -spin_lock (&ce->signal_lock); -list_del_rcu (&rq->signal_link); - release =remove_signaling_context (b, ce); -spin_unlock (&ce->signal_lock); -if (release) { -if (intel_timeline_is_last (ce->timeline, rq)) -add_retire (b, ce->timeline); -intel_context_put (ce); - } +list_del (&rq->signal_link); +if (list_empty (&ce->signals) &&intel_timeline_is_last (ce->timeline, rq)) +add_retire (b, ce->timeline);if (__dma_fence_signal (&rq->fence))/* We own signal_node now, xfer to local list */
@@ -244,9 +239,13 @@ static void signal_irq_work(struct irq_work *work)else i915_request_put (rq); } + + release =remove_signaling_context (b, ce); +spin_unlock (&ce->signal_lock); +if (release) +intel_context_put (ce); } -atomic_dec (&b->signaler_active); -rcu_read_unlock (); +spin_unlock (&b->signalers_lock);llist_for_each_safe (signal, sn, signal) { struct i915_request *rq =
@@ -347,14 +346,15 @@ static void irq_signal_request(struct i915_request *rq,irq_work_queue (&b->irq_work); } -static void insert_breadcrumb (struct i915_request *rq) +static bool insert_breadcrumb (struct i915_request *rq, +struct intel_breadcrumbs *b) { -struct intel_breadcrumbs *b =READ_ONCE (rq->engine)->breadcrumbs;struct intel_context *ce = rq->context;struct list_head *pos; +bool ret;if (test_bit (I915_FENCE_FLAG_SIGNAL , &rq->fence.flags)) -return ; +return false;/* * If the request is already completed, we can transfer it
@@ -363,14 +363,15 @@ static void insert_breadcrumb(struct i915_request *rq)*/ if (__i915_request_is_complete (rq)) {irq_signal_request (rq, b); -return ; +return false; }if (list_empty (&ce->signals)) { -intel_context_get (ce); -add_signaling_context (b, ce); + ret = true; pos = &ce->signals; }else { + ret = false; +/* * We keep the seqno in retirement order, so we can break * inside intel_engine_signal_breadcrumbs as soon as we've
@@ -395,23 +396,19 @@ static void insert_breadcrumb(struct i915_request *rq) }i915_request_get (rq); -list_add_rcu (&rq->signal_link, pos); +list_add (&rq->signal_link, pos);GEM_BUG_ON (!check_signal_order (ce, rq));GEM_BUG_ON (test_bit (DMA_FENCE_FLAG_SIGNALED_BIT , &rq->fence.flags));set_bit (I915_FENCE_FLAG_SIGNAL , &rq->fence.flags); -/* -* Defer enabling the interrupt to after HW submission and recheck -* the request as it may have completed and raised the interrupt as -* we were attaching it into the lists. -*/ -if (!READ_ONCE (b->irq_armed) ||__i915_request_is_complete (rq)) -irq_work_queue (&b->irq_work); +return ret; }bool i915_request_enable_breadcrumb (struct i915_request *rq) {struct intel_context *ce = rq->context; +struct intel_breadcrumbs *b; +bool add_context;/* Serialises with i915_request_retire() using rq->lock */ if (test_bit (DMA_FENCE_FLAG_SIGNALED_BIT , &rq->fence.flags))
@@ -427,30 +424,88 @@ bool i915_request_enable_breadcrumb(struct i915_request *rq)return true;spin_lock (&ce->signal_lock); + b =READ_ONCE (rq->engine)->breadcrumbs; +if (test_bit (I915_FENCE_FLAG_ACTIVE , &rq->fence.flags)) -insert_breadcrumb (rq); + add_context =insert_breadcrumb (rq, b); + +if (add_context &&spin_trylock (&b->signalers_lock)) { +add_signaling_context (b, ce); +spin_unlock (&b->signalers_lock); + add_context = false; + } + +/* +* Defer enabling the interrupt to after HW submission and recheck +* the request as it may have completed and raised the interrupt as +* we were attaching it into the lists. +*/ +if (!READ_ONCE (b->irq_armed) ||__i915_request_is_complete (rq)) +irq_work_queue (&b->irq_work); +spin_unlock (&ce->signal_lock); +if (add_context) { +/* +* Fast trylock didn't work, use slow locking. +* +* Dropping the lock to solve the inversion is safe, since +* no race is possible against remove_signaling_context() +* without being added as signaling context. +*/ +spin_lock (&b->signalers_lock); +spin_lock (&ce->signal_lock); +add_signaling_context (b, ce); +spin_unlock (&ce->signal_lock); +spin_unlock (&b->signalers_lock); + } +return true; } + +static void unlock_context_remove_signaling (struct intel_context *ce, +struct intel_breadcrumbs *b, +unsigned long flags) +{ +bool release = false, retry = false; + +if (list_empty (&ce->signals)) { +if (spin_trylock (&b->signalers_lock)) { + release =remove_signaling_context (b, ce); +spin_unlock (&b->signalers_lock); + }else { + retry = true; + } + } +spin_unlock_irqrestore (&ce->signal_lock, flags); + +if (retry) { +spin_lock_irqsave (&b->signalers_lock, flags); +spin_lock (&ce->signal_lock); + release =remove_signaling_context (b, ce); +spin_unlock (&ce->signal_lock); +spin_unlock_irqrestore (&b->signalers_lock, flags); + } + +if (release) +intel_context_put (ce); +} +void i915_request_cancel_breadcrumb (struct i915_request *rq) {struct intel_breadcrumbs *b =READ_ONCE (rq->engine)->breadcrumbs;struct intel_context *ce = rq->context; -bool release; +unsigned long flags; -spin_lock (&ce->signal_lock); +spin_lock_irqsave (&ce->signal_lock, flags);if (!test_and_clear_bit (I915_FENCE_FLAG_SIGNAL , &rq->fence.flags)) {spin_unlock (&ce->signal_lock);return ; } -list_del_rcu (&rq->signal_link); - release =remove_signaling_context (b, ce); -spin_unlock (&ce->signal_lock); -if (release) -intel_context_put (ce); +list_del (&rq->signal_link); +unlock_context_remove_signaling (ce, b, flags);if (__i915_request_is_complete (rq))irq_signal_request (rq, b);
@@ -462,7 +517,6 @@ void intel_context_remove_breadcrumbs(struct intel_context *ce, struct intel_breadcrumbs *b) {struct i915_request *rq, *rn; -bool release = false;unsigned long flags;spin_lock_irqsave (&ce->signal_lock, flags);
@@ -476,31 +530,26 @@ void intel_context_remove_breadcrumbs(struct intel_context *ce, &rq->fence.flags))continue ; -list_del_rcu (&rq->signal_link); +list_del (&rq->signal_link);irq_signal_request (rq, b);i915_request_put (rq); } - release =remove_signaling_context (b, ce); unlock: -spin_unlock_irqrestore (&ce->signal_lock, flags); -if (release) -intel_context_put (ce); - -while (atomic_read (&b->signaler_active)) -cpu_relax (); +unlock_context_remove_signaling (ce, b, flags); }static void print_signals (struct intel_breadcrumbs *b,struct drm_printer *p) {struct intel_context *ce;struct i915_request *rq; +unsigned long flags;drm_printf (p,"Signals:\n" ); -rcu_read_lock (); -list_for_each_entry_rcu (ce, &b->signalers, signal_link) { -list_for_each_entry_rcu (rq, &ce->signals, signal_link) +spin_lock_irqsave (&b->signalers_lock, flags); +list_for_each_entry (ce, &b->signalers, signal_link) { +list_for_each_entry (rq, &ce->signals, signal_link) drm_printf(p, "\t[%llx:%llx%s] @ %dms\n", rq->fence.context, rq->fence.seqno, __i915_request_is_complete(rq) ? "!" :
@@ -508,7 +557,7 @@ static void print_signals(struct intel_breadcrumbs *b, struct drm_printer *p)"" , jiffies_to_msecs(jiffies - rq->emitted_jiffies)); } -rcu_read_unlock (); +spin_unlock_irqrestore (&b->signalers_lock, flags); } void intel_engine_print_breadcrumbs(struct intel_engine_cs *engine,
drivers/gpu/drm/i915/gt/intel_breadcrumbs_types.h +0 -1
@@ -39,7 +39,6 @@ struct intel_breadcrumbs {spinlock_t signalers_lock;/* protects the list of signalers */ struct list_head signalers;struct llist_head signaled_requests; -atomic_t signaler_active;spinlock_t irq_lock;/* protects the interrupt from hardirq context */ struct irq_work irq_work;/* for use from inside irq_lock */
Signature · 1 line
2.53.0
[email protected]
AI reviewRe: [PATCH v3 7/7] drm/i915/gt: Use signalers_lock to prevent starvation of irq_work.
Thank you for your contribution! Sashiko AI review found 6 potential issue(s) to consider: - [Critical] Missing `spin_unlock_irqrestore` in the early return path leaves local interrupts disabled.…
Thank you for your contribution! Sashiko AI review found 6 potential issue(s) to consider:
- [Critical] Missing spin_unlock_irqrestore in the early return path leaves local interrupts disabled.
- [High] Uninitialized variable add_context may be evaluated and lead to undefined behavior.
- [High] Data race / list corruption when iterating ce->signals without holding ce->signal_lock.
- [High] Double list_add in add_signaling_context causes list corruption due to race condition.
- [High] Queueing irq_work before adding the context to the signalers list causes missed wakeups.
- [High] Unconditional call to list_empty and list_del_init on a potentially uninitialized list node causes NULL pointer dereference. --
commit 84930541bab063f0b3f4b8114a287062a6526adc
Author: Sebastian Andrzej Siewior <[email protected]>
drm/i915/gt: Use signalers_lock to prevent starvation of irq_work.
This commit replaces RCU with a global spinlock to manage the signalers list in order to prevent IRQ-Work starvation and support PREEMPT_RT.
Quoted 8 lines
drivers/gpu/drm/i915/gt/intel_breadcrumbs.c +0
-0@@ -88,24 +88,25 @@ static void add_signaling_context(struct intel_breadcrumbs *b, struct intel_context *ce) {lockdep_assert_held (&ce->signal_lock);
drivers/gpu/drm/i915/gt/intel_breadcrumbs.c static void add_signaling_context(struct intel_breadcrumbs *b, · line 97
Quoted 10 lines
drivers/gpu/drm/i915/gt/intel_breadcrumbs.c +6
-3+lockdep_assert_held (&b->signalers_lock); -spin_lock (&b->signalers_lock); -list_add_rcu (&ce->signal_link, &b->signalers); -spin_unlock (&b->signalers_lock); +if (list_empty (&ce->signals)) +return ; + +intel_context_get (ce); +list_add (&ce->signal_link, &b->signalers);
[Severity: High]
Could this unconditionally add a node that is already linked?
In unlock_context_remove_signaling(), if spin_trylock() fails, ce->signal_lock is dropped while the context remains linked in b->signalers:
if (list_empty(&ce->signals)) {
if (spin_trylock(&b->signalers_lock)) {
// ...
} else {
retry = true;
}
}
spin_unlock_irqrestore(&ce->signal_lock, flags);If another thread concurrently submits a new request to this context via i915_request_enable_breadcrumb(), it sees an empty ce->signals list, inserts the request, and calls add_signaling_context().
Because ce->signals is now non-empty, add_signaling_context() unconditionally executes list_add(&ce->signal_link, &b->signalers). Since the node is already linked, this would corrupt the doubly-linked list.
Quoted 7 lines
drivers/gpu/drm/i915/gt/intel_breadcrumbs.c +1
-0}static bool remove_signaling_context (struct intel_breadcrumbs *b,struct intel_context *ce) {lockdep_assert_held (&ce->signal_lock); +lockdep_assert_held (&b->signalers_lock);
drivers/gpu/drm/i915/gt/intel_breadcrumbs.c static void add_signaling_context(struct intel_breadcrumbs *b, · line 109
Quoted 10 lines
drivers/gpu/drm/i915/gt/intel_breadcrumbs.c +2
-5-if (!list_empty (&ce->signals)) +if (!list_empty (&ce->signals) ||list_empty (&ce->signal_link))return false; -spin_lock (&b->signalers_lock); -list_del_rcu (&ce->signal_link); -spin_unlock (&b->signalers_lock); - +list_del_init (&ce->signal_link);
[Severity: High]
Can remove_signaling_context() dereference a NULL pointer if the list node is zero-initialized?
The patch introduces logic in unlock_context_remove_signaling() that calls remove_signaling_context() even if ce->signals was empty and the context was never added to b->signalers.
If ce->signal_link is zero-initialized rather than initialized via INIT_LIST_HEAD, list_empty(&ce->signal_link) evaluates to
NULL == &ce->signal_link (returning false). It then proceeds to call list_del_init(&ce->signal_link), which dereferences the NULL pointers inside the zeroed list head.
Quoted 2 lines
drivers/gpu/drm/i915/gt/intel_breadcrumbs.c +0
-0return true; }
[ ... ]
drivers/gpu/drm/i915/gt/intel_breadcrumbs.c bool i915_request_enable_breadcrumb(struct i915_request *rq) · line 432
Quoted 11 lines
drivers/gpu/drm/i915/gt/intel_breadcrumbs.c +5
-1@@ -427,30 +424,88 @@ bool i915_request_enable_breadcrumb(struct i915_request *rq)return true;spin_lock (&ce->signal_lock); + b =READ_ONCE (rq->engine)->breadcrumbs; +if (test_bit (I915_FENCE_FLAG_ACTIVE , &rq->fence.flags)) -insert_breadcrumb (rq); + add_context =insert_breadcrumb (rq, b); + +if (add_context &&spin_trylock (&b->signalers_lock)) {
[Severity: High]
Can add_context be used uninitialized here?
If test_bit(I915_FENCE_FLAG_ACTIVE, &rq->fence.flags) evaluates to false (e.g., if the bit was cleared concurrently after the lockless check above), add_context is never assigned.
It is then evaluated in the condition:
if (add_context && spin_trylock(&b->signalers_lock)) {If the uninitialized stack variable evaluates to true, it could lead to incorrectly calling add_signaling_context() and list corruption.
Quoted 6 lines
drivers/gpu/drm/i915/gt/intel_breadcrumbs.c +6
-0+add_signaling_context (b, ce); +spin_unlock (&b->signalers_lock); + add_context = false; + } + +/*
drivers/gpu/drm/i915/gt/intel_breadcrumbs.c bool i915_request_enable_breadcrumb(struct i915_request *rq) · line 448
Quoted 10 lines
drivers/gpu/drm/i915/gt/intel_breadcrumbs.c +8
-0+* Defer enabling the interrupt to after HW submission and recheck +* the request as it may have completed and raised the interrupt as +* we were attaching it into the lists. +*/ +if (!READ_ONCE (b->irq_armed) ||__i915_request_is_complete (rq)) +irq_work_queue (&b->irq_work); +spin_unlock (&ce->signal_lock); +if (add_context) {
[Severity: High]
Does queuing irq_work before the context is added to the signalers list create a TOCTOU race?
If the fast path spin_trylock() fails in i915_request_enable_breadcrumb(), adding the context to b->signalers is deferred to the slow path here. However, irq_work_queue() is executed immediately before the slow path block.
If the irq_work executes before the slow path adds the context, it sees an empty list and finishes without arming the hardware interrupt. Once the context is subsequently added, no further IRQ work is queued, and the interrupt remains disarmed forever, causing system hangs.
Quoted 16 lines
drivers/gpu/drm/i915/gt/intel_breadcrumbs.c +14
-0+/* +* Fast trylock didn't work, use slow locking. +* +* Dropping the lock to solve the inversion is safe, since +* no race is possible against remove_signaling_context() +* without being added as signaling context. +*/ +spin_lock (&b->signalers_lock); +spin_lock (&ce->signal_lock); +add_signaling_context (b, ce); +spin_unlock (&ce->signal_lock); +spin_unlock (&b->signalers_lock); + } +return true; }
[ ... ]
drivers/gpu/drm/i915/gt/intel_breadcrumbs.c bool i915_request_enable_breadcrumb(struct i915_request *rq) · line 503
Quoted 11 lines
void
i915_request_cancel_breadcrumb(struct i915_request *rq){drivers/gpu/drm/i915/gt/intel_breadcrumbs.c +2
-2struct intel_breadcrumbs *b =READ_ONCE (rq->engine)->breadcrumbs;struct intel_context *ce = rq->context; -bool release; +unsigned long flags; -spin_lock (&ce->signal_lock); +spin_lock_irqsave (&ce->signal_lock, flags);if (!test_and_clear_bit (I915_FENCE_FLAG_SIGNAL , &rq->fence.flags)) {spin_unlock (&ce->signal_lock);
[Severity: Critical]
Are local interrupts left disabled in this early return path?
The lock acquisition was changed to spin_lock_irqsave(&ce->signal_lock, flags), but the early return still uses spin_unlock(&ce->signal_lock), which fails to restore the CPU's interrupt state.
Quoted 2 lines
drivers/gpu/drm/i915/gt/intel_breadcrumbs.c +0
-0return ; }
[ ... ]
Quoted 4 lines
drivers/gpu/drm/i915/gt/intel_breadcrumbs.c +0
-0@@ -508,7 +557,7 @@ static void print_signals(struct intel_breadcrumbs *b, struct drm_printer *p) {struct intel_context *ce;struct i915_request *rq;
drivers/gpu/drm/i915/gt/intel_breadcrumbs.c static void print_signals(struct intel_breadcrumbs *b, struct drm_printer *p) · line 552
Quoted 10 lines
drivers/gpu/drm/i915/gt/intel_breadcrumbs.c +4
-3+unsigned long flags;drm_printf (p,"Signals:\n" ); -rcu_read_lock (); -list_for_each_entry_rcu (ce, &b->signalers, signal_link) { -list_for_each_entry_rcu (rq, &ce->signals, signal_link) +spin_lock_irqsave (&b->signalers_lock, flags); +list_for_each_entry (ce, &b->signalers, signal_link) { +list_for_each_entry (rq, &ce->signals, signal_link)
[Severity: High]
Is it safe to iterate over ce->signals without holding ce->signal_lock?
Since RCU is removed, ce->signals is a standard doubly-linked list. Writers (e.g., i915_request_cancel_breadcrumb) modify ce->signals holding only ce->signal_lock.
Iterating over it here while only holding b->signalers_lock exposes a data race where concurrent modifications could cause the iteration to dereference LIST_POISON1.
Quoted 1 line
drm_printf(p, "\t[%llx:%llx%s] @ %dms\n",
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=7