hwspinlock: improve locking safety by using raw_spinlock_t
PATCH 1/1 hwspinlock: improve locking safety by using raw_spinlock_t
Both __hwspin_trylock and __hwspin_unlock use hwlock->lock, and require running in atomic context, with a special annotation:
function will never sleep.
However, this requirement is not fulfilled on PREEMPT_RT.
To address it, use raw_spinlock_t instead of spin_lock_t.
--- drivers/hwspinlock/hwspinlock_core.c | 20 ++++++++++---------- drivers/hwspinlock/hwspinlock_internal.h | 2 +- 2 files changed, 11 insertions(+), 11 deletions(-)
drivers/hwspinlock/hwspinlock_core.c +10 -10
@@ -111,17 +111,17 @@ int __hwspin_trylock(struct hwspinlock *hwlock, int mode, unsigned long *flags)*/ switch (mode) {case HWLOCK_IRQSTATE : - ret =spin_trylock_irqsave (&hwlock->lock, *flags); + ret =raw_spin_trylock_irqsave (&hwlock->lock, *flags);break ;case HWLOCK_IRQ : - ret =spin_trylock_irq (&hwlock->lock); + ret =raw_spin_trylock_irq (&hwlock->lock);break ;case HWLOCK_RAW :case HWLOCK_IN_ATOMIC : ret =1 ;break ;default : - ret =spin_trylock (&hwlock->lock); + ret =raw_spin_trylock (&hwlock->lock);break ; }
@@ -136,17 +136,17 @@ int __hwspin_trylock(struct hwspinlock *hwlock, int mode, unsigned long *flags)if (!ret) {switch (mode) {case HWLOCK_IRQSTATE : -spin_unlock_irqrestore (&hwlock->lock, *flags); +raw_spin_unlock_irqrestore (&hwlock->lock, *flags);break ;case HWLOCK_IRQ : -spin_unlock_irq (&hwlock->lock); +raw_spin_unlock_irq (&hwlock->lock);break ;case HWLOCK_RAW :case HWLOCK_IN_ATOMIC :/* Nothing to do */ break ;default : -spin_unlock (&hwlock->lock); +raw_spin_unlock (&hwlock->lock);break ; }
@@ -289,17 +289,17 @@ void __hwspin_unlock(struct hwspinlock *hwlock, int mode, unsigned long *flags)/* Undo the spin_trylock{_irq, _irqsave} called while locking */ switch (mode) {case HWLOCK_IRQSTATE : -spin_unlock_irqrestore (&hwlock->lock, *flags); +raw_spin_unlock_irqrestore (&hwlock->lock, *flags);break ;case HWLOCK_IRQ : -spin_unlock_irq (&hwlock->lock); +raw_spin_unlock_irq (&hwlock->lock);break ;case HWLOCK_RAW :case HWLOCK_IN_ATOMIC :/* Nothing to do */ break ;default : -spin_unlock (&hwlock->lock); +raw_spin_unlock (&hwlock->lock);break ; } }
@@ -535,7 +535,7 @@ int hwspin_lock_register(struct hwspinlock_device *bank, struct device *dev,for (i =0 ; i < num_locks; i++) { hwlock = &bank->lock[i]; -spin_lock_init (&hwlock->lock); +raw_spin_lock_init (&hwlock->lock); hwlock->bank = bank; ret =hwspin_lock_register_single (hwlock, base_id + i);
drivers/hwspinlock/hwspinlock_internal.h +1 -1
@@ -42,7 +42,7 @@ struct hwspinlock_ops {*/ struct hwspinlock {struct hwspinlock_device *bank; -spinlock_t lock; +raw_spinlock_t lock;void *priv; };
Signature · 1 line
2.25.1
Bjorn Andersson
maintainerQuoted 6 lines · Wen Yang
Both
__hwspin_trylockand__hwspin_unlockusehwlock->lock, and require running in atomic context, with a special annotation:
function will never sleep.
However, this requirement is not fulfilled onPREEMPT_RT.To address it, use
raw_spinlock_tinstead ofspin_lock_t.
I think the "will never sleep" comment expresses that the function can be called in atomic or irq context, not necessarily that it must not sleep.
If this is the case, would it be better to fix the comment or the code?
Regards,
Bjorn
Quoted 104 lines
Signed-off-by Wen Yang <[email protected]>Cc Bjorn Andersson <[email protected]>Cc Baolin Wang <[email protected]>Cc Dave Young <[email protected]>--- drivers/hwspinlock/hwspinlock_core.c | 20 ++++++++++---------- drivers/hwspinlock/hwspinlock_internal.h | 2 +- 2 files changed, 11 insertions(+), 11 deletions(-)drivers/hwspinlock/hwspinlock_core.c +10
-10@@ -111,17 +111,17 @@ int __hwspin_trylock(struct hwspinlock *hwlock, int mode, unsigned long *flags)*/ switch (mode) {case HWLOCK_IRQSTATE : - ret =spin_trylock_irqsave (&hwlock->lock, *flags); + ret =raw_spin_trylock_irqsave (&hwlock->lock, *flags);break ;case HWLOCK_IRQ : - ret =spin_trylock_irq (&hwlock->lock); + ret =raw_spin_trylock_irq (&hwlock->lock);break ;case HWLOCK_RAW :case HWLOCK_IN_ATOMIC : ret =1 ;break ;default : - ret =spin_trylock (&hwlock->lock); + ret =raw_spin_trylock (&hwlock->lock);break ; }@@ -136,17 +136,17 @@ int __hwspin_trylock(struct hwspinlock *hwlock, int mode, unsigned long *flags)if (!ret) {switch (mode) {case HWLOCK_IRQSTATE : -spin_unlock_irqrestore (&hwlock->lock, *flags); +raw_spin_unlock_irqrestore (&hwlock->lock, *flags);break ;case HWLOCK_IRQ : -spin_unlock_irq (&hwlock->lock); +raw_spin_unlock_irq (&hwlock->lock);break ;case HWLOCK_RAW :case HWLOCK_IN_ATOMIC :/* Nothing to do */ break ;default : -spin_unlock (&hwlock->lock); +raw_spin_unlock (&hwlock->lock);break ; }@@ -289,17 +289,17 @@ void __hwspin_unlock(struct hwspinlock *hwlock, int mode, unsigned long *flags)/* Undo the spin_trylock{_irq, _irqsave} called while locking */ switch (mode) {case HWLOCK_IRQSTATE : -spin_unlock_irqrestore (&hwlock->lock, *flags); +raw_spin_unlock_irqrestore (&hwlock->lock, *flags);break ;case HWLOCK_IRQ : -spin_unlock_irq (&hwlock->lock); +raw_spin_unlock_irq (&hwlock->lock);break ;case HWLOCK_RAW :case HWLOCK_IN_ATOMIC :/* Nothing to do */ break ;default : -spin_unlock (&hwlock->lock); +raw_spin_unlock (&hwlock->lock);break ; } }@@ -535,7 +535,7 @@ int hwspin_lock_register(struct hwspinlock_device *bank, struct device *dev,for (i =0 ; i < num_locks; i++) { hwlock = &bank->lock[i]; -spin_lock_init (&hwlock->lock); +raw_spin_lock_init (&hwlock->lock); hwlock->bank = bank; ret =hwspin_lock_register_single (hwlock, base_id + i);drivers/hwspinlock/hwspinlock_internal.h +1
-1@@ -42,7 +42,7 @@ struct hwspinlock_ops {*/ struct hwspinlock {struct hwspinlock_device *bank; -spinlock_t lock; +raw_spinlock_t lock;void *priv; };Signature · 1 line
2.25.1
Quoted 6 lines · Bjorn Andersson
Quoted 5 lines · Wen Yang
Both
__hwspin_trylockand__hwspin_unlockusehwlock->lock, and require running in atomic context, with a special annotation:
function will never sleep.
However, this requirement is not fulfilled onPREEMPT_RT.
Quoted 8 lines
Quoted 1 line
To address it, use
raw_spinlock_tinstead ofspin_lock_t.I think the "will never sleep" comment expresses that the function can be called in atomic or irq context, not necessarily that it must not sleep.
If this is the case, would it be better to fix the comment or the code?
Thank you for your commens.
Let's try updating the following documents:Documentation/locking/hwspinlock.rstdrivers/hwspinlock/hwspinlock_core.cinclude/linux/hwspinlock.h
--
Best wishes,
Wen
Quoted 105 lines
Quoted 105 lines
Signed-off-by Wen Yang <[email protected]>Cc Bjorn Andersson <[email protected]>Cc Baolin Wang <[email protected]>Cc Dave Young <[email protected]>--- drivers/hwspinlock/hwspinlock_core.c | 20 ++++++++++---------- drivers/hwspinlock/hwspinlock_internal.h | 2 +- 2 files changed, 11 insertions(+), 11 deletions(-)drivers/hwspinlock/hwspinlock_core.c +10
-10@@ -111,17 +111,17 @@ int __hwspin_trylock(struct hwspinlock *hwlock, int mode, unsigned long *flags)*/ switch (mode) {case HWLOCK_IRQSTATE : - ret =spin_trylock_irqsave (&hwlock->lock, *flags); + ret =raw_spin_trylock_irqsave (&hwlock->lock, *flags);break ;case HWLOCK_IRQ : - ret =spin_trylock_irq (&hwlock->lock); + ret =raw_spin_trylock_irq (&hwlock->lock);break ;case HWLOCK_RAW :case HWLOCK_IN_ATOMIC : ret =1 ;break ;default : - ret =spin_trylock (&hwlock->lock); + ret =raw_spin_trylock (&hwlock->lock);break ; }@@ -136,17 +136,17 @@ int __hwspin_trylock(struct hwspinlock *hwlock, int mode, unsigned long *flags)if (!ret) {switch (mode) {case HWLOCK_IRQSTATE : -spin_unlock_irqrestore (&hwlock->lock, *flags); +raw_spin_unlock_irqrestore (&hwlock->lock, *flags);break ;case HWLOCK_IRQ : -spin_unlock_irq (&hwlock->lock); +raw_spin_unlock_irq (&hwlock->lock);break ;case HWLOCK_RAW :case HWLOCK_IN_ATOMIC :/* Nothing to do */ break ;default : -spin_unlock (&hwlock->lock); +raw_spin_unlock (&hwlock->lock);break ; }@@ -289,17 +289,17 @@ void __hwspin_unlock(struct hwspinlock *hwlock, int mode, unsigned long *flags)/* Undo the spin_trylock{_irq, _irqsave} called while locking */ switch (mode) {case HWLOCK_IRQSTATE : -spin_unlock_irqrestore (&hwlock->lock, *flags); +raw_spin_unlock_irqrestore (&hwlock->lock, *flags);break ;case HWLOCK_IRQ : -spin_unlock_irq (&hwlock->lock); +raw_spin_unlock_irq (&hwlock->lock);break ;case HWLOCK_RAW :case HWLOCK_IN_ATOMIC :/* Nothing to do */ break ;default : -spin_unlock (&hwlock->lock); +raw_spin_unlock (&hwlock->lock);break ; } }@@ -535,7 +535,7 @@ int hwspin_lock_register(struct hwspinlock_device *bank, struct device *dev,for (i =0 ; i < num_locks; i++) { hwlock = &bank->lock[i]; -spin_lock_init (&hwlock->lock); +raw_spin_lock_init (&hwlock->lock); hwlock->bank = bank; ret =hwspin_lock_register_single (hwlock, base_id + i);drivers/hwspinlock/hwspinlock_internal.h +1
-1@@ -42,7 +42,7 @@ struct hwspinlock_ops {*/ struct hwspinlock {struct hwspinlock_device *bank; -spinlock_t lock; +raw_spinlock_t lock;void *priv; };Signature · 1 line
2.25.1