Skip to contentSkip to conversation

pstore: replace spinlock_t by raw_spinlock_t

PATCH Applied Wen Yang 3 participants 5 messages Aug 19, 2024 – Aug 26, 2024 Pstore filesystem

lore
Share & tools
lore

Wen Yang

author
#0 lore raw

PATCH 1/1 pstore: replace spinlock_t by raw_spinlock_t

pstore_dump() is called when both preemption and local IRQ are disabled, and a spinlock is obtained, which is problematic for the RT kernel because in this configuration, spinlocks are sleep locks.

Replace the spinlock_t with raw_spinlock_t to avoid sleeping in atomic context.

---
 fs/pstore/platform.c   | 8 ++++----
 include/linux/pstore.h | 2 +-
 2 files changed, 5 insertions(+), 5 deletions(-)
fs/pstore/platform.c +4 -4
index 3497ede88aa0..84719e2bcbc4 100644
@@ -288,13 +288,13 @@ static void pstore_dump(struct kmsg_dumper *dumper,
 	why = kmsg_dump_reason_str(reason);
 
 	if (pstore_cannot_block_path(reason)) {
-		if (!spin_trylock_irqsave(&psinfo->buf_lock, flags)) {
+		if (!raw_spin_trylock_irqsave(&psinfo->buf_lock, flags)) {
 			pr_err("dump skipped in %s path because of concurrent dump\n",
 					in_nmi() ? "NMI" : why);
 			return;
 		}
 	} else {
-		spin_lock_irqsave(&psinfo->buf_lock, flags);
+		raw_spin_lock_irqsave(&psinfo->buf_lock, flags);
 	}
 
 	kmsg_dump_rewind(&iter);
@@ -364,7 +364,7 @@ static void pstore_dump(struct kmsg_dumper *dumper,
 		total += record.size;
 		part++;
 	}
-	spin_unlock_irqrestore(&psinfo->buf_lock, flags);
+	raw_spin_unlock_irqrestore(&psinfo->buf_lock, flags);
 
 	if (saved_ret) {
 		pr_err_once("backend (%s) writing error (%d)\n", psinfo->name,
@@ -503,7 +503,7 @@ int pstore_register(struct pstore_info *psi)
 		psi->write_user = pstore_write_user_compat;
 	psinfo = psi;
 	mutex_init(&psinfo->read_mutex);
-	spin_lock_init(&psinfo->buf_lock);
+	raw_spin_lock_init(&psinfo->buf_lock);
 
 	if (psi->flags & PSTORE_FLAGS_DMESG)
 		allocate_buf_for_compression();
include/linux/pstore.h +1 -1
index 638507a3c8ff..fed601053c51 100644
@@ -182,7 +182,7 @@ struct pstore_info {
 	struct module	*owner;
 	const char	*name;
 
-	spinlock_t	buf_lock;
+	raw_spinlock_t	buf_lock;
 	char		*buf;
 	size_t		bufsize;
 
Signature · 1 line
2.25.1
#1 lore raw
Quoted 5 lines · Wen Yang

pstore_dump() is called when both preemption and local IRQ are disabled, and a spinlock is obtained, which is problematic for the RT kernel because in this configuration, spinlocks are sleep locks.

Replace the spinlock_t with raw_spinlock_t to avoid sleeping in atomic context.

This feels odd, is it only an out-of-tree RT thing? Or does this affect in-kernel code as well? What prevents any normal spinlock from sleeping in your system configuration as well?

thanks,

greg k-h

author
#2 lore raw
Quoted 11 lines · Greg KH
Quoted 5 lines · Wen Yang

pstore_dump() is called when both preemption and local IRQ are disabled, and a spinlock is obtained, which is problematic for the RT kernel because in this configuration, spinlocks are sleep locks.

Replace the spinlock_t with raw_spinlock_t to avoid sleeping in atomic context.

This feels odd, is it only an out-of-tree RT thing? Or does this affect in-kernel code as well? What prevents any normal spinlock from sleeping in your system configuration as well?

Thank you for your comment.

If we enable PREEMPT_RT, it will also affect in-kernel code, such as in the following scenario:

echo b > /proc/sysrq-trigger
- sysrq_handle_reboot
- emergency_restart
- kmsg_dump
- pstore_dump
Obtained psinfo->buf_lock, if there is a lot of error log in the kernel, it will last for a long time

If the system unexpectedly crashes at this time:
- panic()
- kmsg_dump
- pstore_dump
Attempting to obtain psinfo->buf_lock while disabling interrupts and preemption, but since this lock is already held by the above process, it will result in illegal sleep.

--
Best wishes,
Wen

maintainer
#3 lore raw
Quoted 28 lines · Wen Yang
Quoted 11 lines · Greg KH
Quoted 5 lines · Wen Yang

pstore_dump() is called when both preemption and local IRQ are disabled, and a spinlock is obtained, which is problematic for the RT kernel because in this configuration, spinlocks are sleep locks.

Replace the spinlock_t with raw_spinlock_t to avoid sleeping in atomic context.

This feels odd, is it only an out-of-tree RT thing? Or does this affect in-kernel code as well? What prevents any normal spinlock from sleeping in your system configuration as well?

Thank you for your comment.

If we enable PREEMPT_RT, it will also affect in-kernel code, such as in the following scenario:

echo b > /proc/sysrq-trigger
- sysrq_handle_reboot
- emergency_restart
- kmsg_dump
- pstore_dump
Obtained psinfo->buf_lock, if there is a lot of error log in the kernel, it will last for a long time

Quoted 7 lines

If the system unexpectedly crashes at this time:
- panic()
- kmsg_dump
- pstore_dump
Attempting to obtain psinfo->buf_lock while disabling interrupts and preemption, but since this lock is already held by the above process, it will result in illegal sleep.

Reading Documentation/locking/locktypes.rst seems to suggest pstore does want the raw version. I'm surprised there aren't many more cases where this is a problem. :P

Signature · 1 line
Kees Cook
maintainer Applied
#4 lore raw
In reply to Wen Yang
Quoted 8 lines · Wen Yang

pstore_dump() is called when both preemption and local IRQ are disabled, and a spinlock is obtained, which is problematic for the RT kernel because in this configuration, spinlocks are sleep locks.

Replace the spinlock_t with raw_spinlock_t to avoid sleeping in atomic context.

[...]

Applied to for-next/pstore, thanks!

[1/1] pstore: replace spinlock_t by raw_spinlock_t

      https://git.kernel.org/kees/c/1bf8012fc699

Take care,

Signature · 1 line
Kees Cook