staging: fbtft: make dirty_lock IRQ-safe
Subsystems (3)
PATCH v2 1/1 staging: fbtft: make dirty_lock IRQ-safe
fbtft_mkdirty() can be reached from the fbcon rendering path while processing printk() in hardirq context. Meanwhile, dirty_lock is also taken by fbtft_deferred_io() in workqueue context with local interrupts enabled.
Lockdep reports a possible IRQ lock inversion involving dirty_lock and console_owner. A hardirq can interrupt a CPU holding dirty_lock and enter the console rendering path, which can attempt to acquire dirty_lock again.
The following lockdep report was observed on an RK3566 system with CONFIG_PROVE_LOCKING enabled:
WARNING: possible irq lock inversion dependency detected swapper/2/0 just changed the state of lock:
(console_owner){-...}-{0:0}
but this lock took another, HARDIRQ-unsafe lock in the past:
(&par->dirty_lock){+.+.}-{2:2}
CPU0 CPU1
---- ----
lock(&par->dirty_lock);
local_irq_disable();
lock(console_owner);
lock(&par->dirty_lock);
<Interrupt>
lock(console_owner);*** DEADLOCK ***
Use spin_lock_irqsave() for fbtft_mkdirty() and spin_lock_irq() for fbtft_deferred_io(). They only access the dirty line range, so the IRQ-off regions remain short.
---
Changes in v2:
- Drop the comment explaining the lock ordering in fbtft_mkdirty().
- Use spin_lock_irq() in fbtft_deferred_io(), since it runs in workqueue context with interrupts enabled.
v1: https://lore.kernel.org/lkml/[email protected]/
drivers/staging/fbtft/fbtft-core.c | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-)
drivers/staging/fbtft/fbtft-core.c +5 -4
@@ -298,14 +298,15 @@ static void fbtft_mkdirty(struct fb_info *info, int y, int height) {struct fbtft_par *par = info->par;struct fb_deferred_io *fbdefio = info->fbdefio; +unsigned long flags;/* Mark display lines/area as dirty */ -spin_lock (&par->dirty_lock); +spin_lock_irqsave (&par->dirty_lock, flags);if (y < par->dirty_lines_start) par->dirty_lines_start = y;if (y + height -1 > par->dirty_lines_end) par->dirty_lines_end = y + height -1 ; -spin_unlock (&par->dirty_lock); +spin_unlock_irqrestore (&par->dirty_lock, flags);/* Schedule deferred_io to update display (no-op if already on queue)*/ schedule_delayed_work (&info->deferred_work, fbdefio->delay);
@@ -318,13 +319,13 @@ static void fbtft_deferred_io(struct fb_info *info, struct list_head *pagereflisstruct fb_deferred_io_pageref *pageref;unsigned int y_low =0 , y_high =0 ; -spin_lock (&par->dirty_lock); +spin_lock_irq (&par->dirty_lock); dirty_lines_start = par->dirty_lines_start; dirty_lines_end = par->dirty_lines_end;/* set display line markers as clean */ par->dirty_lines_start = par->info->var.yres -1 ; par->dirty_lines_end =0 ; -spin_unlock (&par->dirty_lock); +spin_unlock_irq (&par->dirty_lock);/* Mark display lines as dirty */ list_for_each_entry (pageref, pagereflist, list) {
Signature · 1 line
2.43.0
[email protected]
AI reviewThank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
Pre-existing issues:
- [High] dirty_lock must be converted to raw_spinlock_t to prevent fatal kernel panics on PREEMPT_RT. --
--- Patch [1]: [PATCH v2] staging: fbtft: make dirty_lock IRQ-safe --- commit 2781cf2c7f0e0cfa744e4747b9f7004477fdff7b
Author: Hui Su <[email protected]>
staging: fbtft: make dirty_lock IRQ-safe
This commit updates the dirty_lock acquisitions in fbtft_mkdirty() and fbtft_deferred_io() to use IRQ-safe variants. This prevents a possible IRQ lock inversion between dirty_lock and console_owner.
drivers/staging/fbtft/fbtft-core.c static void fbtft_mkdirty(struct fb_info *info, int y, int height) · line 304
Quoted 13 lines
drivers/staging/fbtft/fbtft-core.c +2
-1@@ -298,14 +298,15 @@ static void fbtft_mkdirty(struct fb_info *info, int y, int height) {struct fbtft_par *par = info->par;struct fb_deferred_io *fbdefio = info->fbdefio; +unsigned long flags;/* Mark display lines/area as dirty */ -spin_lock (&par->dirty_lock); +spin_lock_irqsave (&par->dirty_lock, flags);
[Severity: High]
This isn't a bug introduced by this patch, but will this cause a kernel panic on PREEMPT_RT systems?
The commit message notes that fbtft_mkdirty() can be reached from the fbcon rendering path while processing printk() in hardirq context. On PREEMPT_RT, spinlock_t is mapped to a sleepable rt_mutex. Acquiring a sleepable lock in true hardirq context will trigger a sleep-in-atomic bug.
Since dirty_lock is accessed in true hardirq context, should the declaration of dirty_lock in struct fbtft_par be converted to a raw_spinlock_t and acquired via raw_spin_lock_irqsave() to safely synchronize under PREEMPT_RT?
Quoted 2 lines
drivers/staging/fbtft/fbtft-core.c +0
-0if (y < par->dirty_lines_start) par->dirty_lines_start = y;
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
Quoted 4 lines · Hui Su
fbtft_mkdirty()can be reached from the fbcon rendering path while processingprintk()in hardirq context. Meanwhile,dirty_lockis also taken byfbtft_deferred_io()in workqueue context with local interrupts enabled.
...
Quoted 6 lines
Use
spin_lock_irqsave()forfbtft_mkdirty()andspin_lock_irq()forfbtft_deferred_io(). They only access the dirty line range, so the IRQ-off regions remain short.Fixes c296d5f9957c ("staging: fbtft: core support")Signed-off-by Hui Su <[email protected]>