md/raid5-ppl: Fix invalid context sleep in ppl_io_unit_finished() on PREEMPT_RT
Subsystems (2)
PATCH 1/1 md/raid5-ppl: Fix invalid context sleep in ppl_io_unit_finished() on PREEMPT_RT
The function ppl_io_unit_finished() uses a local_irq_save()/spin_lock() sequence. On a PREEMPT_RT enabled kernel, spin_lock() can sleep. Calling it with interrupts disabled creates an atomic context where sleeping is forbidden.
Ensuring that the interrupt state is managed atomically with the lock itself. The change is applied to both the 'log->io_list_lock' and 'ppl_conf->no_mem_stripes_lock' critical sections within the function.
--- drivers/md/raid5-ppl.c | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-)
drivers/md/raid5-ppl.c +4 -8
@@ -553,15 +553,13 @@ static void ppl_io_unit_finished(struct ppl_io_unit *io)pr_debug ("%s: seq: %llu\n" , __func__, io->seq); -local_irq_save (flags); - -spin_lock (&log->io_list_lock); +spin_lock_irqsave (&log->io_list_lock, flags);list_del (&io->log_sibling); -spin_unlock (&log->io_list_lock); +spin_unlock_irqrestore (&log->io_list_lock, flags);mempool_free (io, &ppl_conf->io_pool); -spin_lock (&ppl_conf->no_mem_stripes_lock); +spin_lock_irqsave (&ppl_conf->no_mem_stripes_lock, flags);if (!list_empty (&ppl_conf->no_mem_stripes)) {struct stripe_head *sh;
@@ -571,9 +569,7 @@ static void ppl_io_unit_finished(struct ppl_io_unit *io)set_bit (STRIPE_HANDLE , &sh->state);raid5_release_stripe (sh); } -spin_unlock (&ppl_conf->no_mem_stripes_lock); - -local_irq_restore (flags); +spin_unlock_irqrestore (&ppl_conf->no_mem_stripes_lock, flags);wake_up (&conf->wait_for_quiescent); }
Signature · 1 line
2.50.0
Hi,
Quoted 5 lines · Yunseong Kim
The function
ppl_io_unit_finished()uses a local_irq_save()/spin_lock() sequence. On aPREEMPT_RTenabled kernel,spin_lock()can sleep. Calling it with interrupts disabled creates an atomic context where sleeping is forbidden.
What? I believe spin_lock can never sleep.
Quoted 19 lines
Ensuring that the interrupt state is managed atomically with the lock itself. The change is applied to both the '
log->io_list_lock' and 'ppl_conf->no_mem_stripes_lock' critical sections within the function.Signed-off-by Yunseong Kim <[email protected]>--- drivers/md/raid5-ppl.c | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-)drivers/md/raid5-ppl.c +0
-2@@ -553,15 +553,13 @@ static void ppl_io_unit_finished(struct ppl_io_unit *io)pr_debug ("%s: seq: %llu\n" , __func__, io->seq); -local_irq_save (flags); -
drivers/md/raid5-ppl.c static void ppl_io_unit_finished(struct ppl_io_unit *io) · line 562
Quoted 10 lines
drivers/md/raid5-ppl.c +3
-3-spin_lock (&log->io_list_lock); +spin_lock_irqsave (&log->io_list_lock, flags);list_del (&io->log_sibling); -spin_unlock (&log->io_list_lock); +spin_unlock_irqrestore (&log->io_list_lock, flags);mempool_free (io, &ppl_conf->io_pool); -spin_lock (&ppl_conf->no_mem_stripes_lock); +spin_lock_irqsave (&ppl_conf->no_mem_stripes_lock, flags);
Please notice, local_irq_save + spin_lock is the same as spin_lock_irqsave, I don't think your changes have any functonal chagnes.
Thanks,
Kuai
Quoted 15 lines
drivers/md/raid5-ppl.c +1
-3if (!list_empty (&ppl_conf->no_mem_stripes)) {struct stripe_head *sh;@@ -571,9 +569,7 @@ static void ppl_io_unit_finished(struct ppl_io_unit *io)set_bit (STRIPE_HANDLE , &sh->state);raid5_release_stripe (sh); } -spin_unlock (&ppl_conf->no_mem_stripes_lock); - -local_irq_restore (flags); +spin_unlock_irqrestore (&ppl_conf->no_mem_stripes_lock, flags);wake_up (&conf->wait_for_quiescent); }
Hi Yu,
Quoted 9 lines · Yu Kuai
Hi,
Quoted 5 lines · Yunseong Kim
The function
ppl_io_unit_finished()uses a local_irq_save()/spin_lock() sequence. On aPREEMPT_RTenabled kernel,spin_lock()can sleep. Calling it with interrupts disabled creates an atomic context where sleeping is forbidden.What? I believe
spin_lockcan never sleep.
I think you might have been a bit surprised by me sending a patch out of the blue. It would be helpful to refer to the references below:
On PREEMPT_RT kernels, these lock types are converted to sleeping locks:
local_lock
spinlock_t
rwlock_t
Quoted 9 lines
Quoted 9 lines
Ensuring that the interrupt state is managed atomically with the lock itself. The change is applied to both the '
log->io_list_lock' and 'ppl_conf->no_mem_stripes_lock' critical sections within the function.Signed-off-by Yunseong Kim <[email protected]>---
drivers/md/raid5-ppl.c| 12 ++++--------1 file changed, 4 insertions(+), 8 deletions(-)
Quoted 10 lines
drivers/md/raid5-ppl.c static void ppl_io_unit_finished(struct ppl_io_unit *io) · line 555
Quoted 10 lines
drivers/md/raid5-ppl.c +1
-2@@ -553,15 +553,13 @@ static void ppl_io_unit_finished(struct ppl_io_unit *io) pr_debug("%s: seq: %llu\n", __func__, io->seq); - local_irq_save(flags); - - spin_lock(&log->io_list_lock); + spin_lock_irqsave(&log->io_list_lock, flags);
The changes in spinlock_t and rwlock_t semantics on PREEMPT_RT kernels have a few implications. For example, on a non-PREEMPT_RT kernel the following code sequence works as expected:
local_irq_disable();
spin_lock(&lock);
and is fully equivalent to:
spin_lock_irq(&lock);
Same applies to rwlock_t and the _irqsave() suffix variants.
Quoted 10 lines
drivers/md/raid5-ppl.c static void ppl_io_unit_finished(struct ppl_io_unit *io) · line 562
Quoted 6 lines
drivers/md/raid5-ppl.c +2
-1list_del(&io->log_sibling); - spin_unlock(&log->io_list_lock); + spin_unlock_irqrestore(&log->io_list_lock, flags); mempool_free(io, &ppl_conf->io_pool); - spin_lock(&ppl_conf->no_mem_stripes_lock); + spin_lock_irqsave(&ppl_conf->no_mem_stripes_lock, flags);Please notice,
local_irq_save+spin_lockis the same asspin_lock_irqsave, I don't think your changes have any functonal chagnes.
This issue has also been a problem in other subsystems, such as USB:
[BUG] usb: gadget: dummy_hcd: Sleeping function called from invalid context in dummy_dequeue on PREEMPT_RT
I am currently contributing to the kernel to address the areas that need to adapt to this paradigm shift so that the PREEMPT_RT Linux kernel can be well supported. I have CC’d other RT people so they can also review this part.
Quoted 2 lines
Thanks,
Kuai
Thank you!
Yunseong
Hi,
Quoted 48 lines · Yunseong Kim
Hi Yu,
Quoted 9 lines · Yu Kuai
Hi,
Quoted 5 lines · Yunseong Kim
The function
ppl_io_unit_finished()uses a local_irq_save()/spin_lock() sequence. On aPREEMPT_RTenabled kernel,spin_lock()can sleep. Calling it with interrupts disabled creates an atomic context where sleeping is forbidden.What? I believe
spin_lockcan never sleep.I think you might have been a bit surprised by me sending a patch out of the blue. It would be helpful to refer to the references below:
On
PREEMPT_RTkernels, these lock types are converted to sleeping locks:
local_lock
spinlock_t
rwlock_tQuoted 9 lines
Quoted 9 lines
Ensuring that the interrupt state is managed atomically with the lock itself. The change is applied to both the '
log->io_list_lock' and 'ppl_conf->no_mem_stripes_lock' critical sections within the function.Signed-off-by Yunseong Kim <[email protected]>--- drivers/md/raid5-ppl.c | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-)Quoted 10 lines
drivers/md/raid5-ppl.c static void ppl_io_unit_finished(struct ppl_io_unit *io) · line 555
Quoted 10 lines
drivers/md/raid5-ppl.c +1
-2@@ -553,15 +553,13 @@ static void ppl_io_unit_finished(struct ppl_io_unit *io) pr_debug("%s: seq: %llu\n", __func__, io->seq); - local_irq_save(flags); - - spin_lock(&log->io_list_lock); + spin_lock_irqsave(&log->io_list_lock, flags);The changes in
spinlock_tandrwlock_tsemantics onPREEMPT_RTkernels have a few implications. For example, on a non-PREEMPT_RT kernel the following code sequence works as expected:local_irq_disable(); spin_lock(&lock);
Quoted 7 lines
and is fully equivalent to:
spin_lock_irq(&lock);Same applies to
rwlock_tand the_irqsave()suffix variants.
Yes, lessons are learned. Perhaps add a link tag in the commit message just in case someone else will be confused?
Thanks,
Kuai
Quoted 28 lines
Quoted 10 lines
drivers/md/raid5-ppl.c static void ppl_io_unit_finished(struct ppl_io_unit *io) · line 562
Quoted 7 lines
drivers/md/raid5-ppl.c +2
-1list_del(&io->log_sibling); - spin_unlock(&log->io_list_lock); + spin_unlock_irqrestore(&log->io_list_lock, flags); mempool_free(io, &ppl_conf->io_pool); - spin_lock(&ppl_conf->no_mem_stripes_lock); + spin_lock_irqsave(&ppl_conf->no_mem_stripes_lock, flags);Please notice,
local_irq_save+spin_lockis the same asspin_lock_irqsave, I don't think your changes have any functonal chagnes.This issue has also been a problem in other subsystems, such as USB:
[BUG] usb: gadget:
dummy_hcd: Sleeping function called from invalid context indummy_dequeueonPREEMPT_RTI am currently contributing to the kernel to address the areas that need to adapt to this paradigm shift so that the
PREEMPT_RTLinux kernel can be well supported. I have CC’d other RT people so they can also review this part.Quoted 2 lines
Thanks,
KuaiThank you!
Yunseong