* [PATCH] hwspinlock: improve locking safety by using raw_spinlock_t
@ 2024-08-24 14:38 Wen Yang
2024-08-26 16:37 ` Bjorn Andersson
0 siblings, 1 reply; 3+ messages in thread
From: Wen Yang @ 2024-08-24 14:38 UTC (permalink / raw)
To: Bjorn Andersson, Baolin Wang
Cc: Wen Yang, Dave Young, linux-remoteproc, linux-kernel
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.
Signed-off-by: Wen Yang <wen.yang@linux.dev>
Cc: Bjorn Andersson <andersson@kernel.org>
Cc: Baolin Wang <baolin.wang@linux.alibaba.com>
Cc: Dave Young <dyoung@redhat.com>
Cc: linux-remoteproc@vger.kernel.org
Cc: linux-kernel@vger.kernel.org
---
drivers/hwspinlock/hwspinlock_core.c | 20 ++++++++++----------
drivers/hwspinlock/hwspinlock_internal.h | 2 +-
2 files changed, 11 insertions(+), 11 deletions(-)
diff --git a/drivers/hwspinlock/hwspinlock_core.c b/drivers/hwspinlock/hwspinlock_core.c
index 6505261e6068..76e5a6c645b1 100644
--- a/drivers/hwspinlock/hwspinlock_core.c
+++ b/drivers/hwspinlock/hwspinlock_core.c
@@ -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);
diff --git a/drivers/hwspinlock/hwspinlock_internal.h b/drivers/hwspinlock/hwspinlock_internal.h
index f298fc0ee5ad..9fbd66e8a82f 100644
--- a/drivers/hwspinlock/hwspinlock_internal.h
+++ b/drivers/hwspinlock/hwspinlock_internal.h
@@ -42,7 +42,7 @@ struct hwspinlock_ops {
*/
struct hwspinlock {
struct hwspinlock_device *bank;
- spinlock_t lock;
+ raw_spinlock_t lock;
void *priv;
};
--
2.25.1
^ permalink raw reply related [flat|nested] 3+ messages in thread
* Re: [PATCH] hwspinlock: improve locking safety by using raw_spinlock_t
2024-08-24 14:38 [PATCH] hwspinlock: improve locking safety by using raw_spinlock_t Wen Yang
@ 2024-08-26 16:37 ` Bjorn Andersson
2024-09-01 13:34 ` Wen Yang
0 siblings, 1 reply; 3+ messages in thread
From: Bjorn Andersson @ 2024-08-26 16:37 UTC (permalink / raw)
To: Wen Yang; +Cc: Baolin Wang, Dave Young, linux-remoteproc, linux-kernel
On Sat, Aug 24, 2024 at 10:38:47PM GMT, Wen Yang wrote:
> 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.
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
>
> Signed-off-by: Wen Yang <wen.yang@linux.dev>
> Cc: Bjorn Andersson <andersson@kernel.org>
> Cc: Baolin Wang <baolin.wang@linux.alibaba.com>
> Cc: Dave Young <dyoung@redhat.com>
> Cc: linux-remoteproc@vger.kernel.org
> Cc: linux-kernel@vger.kernel.org
> ---
> drivers/hwspinlock/hwspinlock_core.c | 20 ++++++++++----------
> drivers/hwspinlock/hwspinlock_internal.h | 2 +-
> 2 files changed, 11 insertions(+), 11 deletions(-)
>
> diff --git a/drivers/hwspinlock/hwspinlock_core.c b/drivers/hwspinlock/hwspinlock_core.c
> index 6505261e6068..76e5a6c645b1 100644
> --- a/drivers/hwspinlock/hwspinlock_core.c
> +++ b/drivers/hwspinlock/hwspinlock_core.c
> @@ -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);
> diff --git a/drivers/hwspinlock/hwspinlock_internal.h b/drivers/hwspinlock/hwspinlock_internal.h
> index f298fc0ee5ad..9fbd66e8a82f 100644
> --- a/drivers/hwspinlock/hwspinlock_internal.h
> +++ b/drivers/hwspinlock/hwspinlock_internal.h
> @@ -42,7 +42,7 @@ struct hwspinlock_ops {
> */
> struct hwspinlock {
> struct hwspinlock_device *bank;
> - spinlock_t lock;
> + raw_spinlock_t lock;
> void *priv;
> };
>
> --
> 2.25.1
>
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: [PATCH] hwspinlock: improve locking safety by using raw_spinlock_t
2024-08-26 16:37 ` Bjorn Andersson
@ 2024-09-01 13:34 ` Wen Yang
0 siblings, 0 replies; 3+ messages in thread
From: Wen Yang @ 2024-09-01 13:34 UTC (permalink / raw)
To: Bjorn Andersson; +Cc: Baolin Wang, Dave Young, linux-remoteproc, linux-kernel
On 2024/8/27 00:37, Bjorn Andersson wrote:
> On Sat, Aug 24, 2024 at 10:38:47PM GMT, Wen Yang wrote:
>> 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.
>
> 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.rst
drivers/hwspinlock/hwspinlock_core.c
include/linux/hwspinlock.h
--
Best wishes,
Wen
>
>>
>> Signed-off-by: Wen Yang <wen.yang@linux.dev>
>> Cc: Bjorn Andersson <andersson@kernel.org>
>> Cc: Baolin Wang <baolin.wang@linux.alibaba.com>
>> Cc: Dave Young <dyoung@redhat.com>
>> Cc: linux-remoteproc@vger.kernel.org
>> Cc: linux-kernel@vger.kernel.org
>> ---
>> drivers/hwspinlock/hwspinlock_core.c | 20 ++++++++++----------
>> drivers/hwspinlock/hwspinlock_internal.h | 2 +-
>> 2 files changed, 11 insertions(+), 11 deletions(-)
>>
>> diff --git a/drivers/hwspinlock/hwspinlock_core.c b/drivers/hwspinlock/hwspinlock_core.c
>> index 6505261e6068..76e5a6c645b1 100644
>> --- a/drivers/hwspinlock/hwspinlock_core.c
>> +++ b/drivers/hwspinlock/hwspinlock_core.c
>> @@ -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);
>> diff --git a/drivers/hwspinlock/hwspinlock_internal.h b/drivers/hwspinlock/hwspinlock_internal.h
>> index f298fc0ee5ad..9fbd66e8a82f 100644
>> --- a/drivers/hwspinlock/hwspinlock_internal.h
>> +++ b/drivers/hwspinlock/hwspinlock_internal.h
>> @@ -42,7 +42,7 @@ struct hwspinlock_ops {
>> */
>> struct hwspinlock {
>> struct hwspinlock_device *bank;
>> - spinlock_t lock;
>> + raw_spinlock_t lock;
>> void *priv;
>> };
>>
>> --
>> 2.25.1
>>
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2024-09-01 13:35 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2024-08-24 14:38 [PATCH] hwspinlock: improve locking safety by using raw_spinlock_t Wen Yang
2024-08-26 16:37 ` Bjorn Andersson
2024-09-01 13:34 ` Wen Yang
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).