[PATCH 3/4] mtd: spi-nor: Keep RWW wait conditions lockless
From: Miquel Raynal
Date: Thu Nov 19 2026 - 17:14:40 EST
The RWW start helpers are evaluated as wait_event_killable() conditions.
The wait_event sets the task to TASK_KILLABLE before the condition
runs. In those conditions, a mutex that cannot be acquired incorrectly
overwrites the state to TASK_UNINTERRUPTIBLE for the time of the sleep,
and then the TASK_RUNNING state set back, which makes the next
wait_event() schedule() a no-op before retrying the loop.
The issue is also visible when enabling CONFIG_DEBUG_ATOMIC_SLEEP, since
the mutex acquisition calls __might_sleep() which triggers a "do not
call blocking ops when !TASK_RUNNING" warning in this case.
Since only the state of the banks must be sampled in the conditions,
rework the wait sections by creating a loop waiting on non-sleeping
conditions.
Fixes: 74df43b3f626 ("mtd: spi-nor: Enhance locking to support reads while writes")
Signed-off-by: Miquel Raynal <miquel.raynal@xxxxxxxxxxx>
---
drivers/mtd/spi-nor/core.c | 80 ++++++++++++++++++++++++++++++++++++++++++----
1 file changed, 73 insertions(+), 7 deletions(-)
diff --git a/drivers/mtd/spi-nor/core.c b/drivers/mtd/spi-nor/core.c
index 7394c9a17334..d46c18e4401c 100644
--- a/drivers/mtd/spi-nor/core.c
+++ b/drivers/mtd/spi-nor/core.c
@@ -1166,6 +1166,45 @@ static unsigned int spi_nor_banks_mask(struct spi_nor *nor, loff_t start,
return mask;
}
+static bool spi_nor_rww_no_ongoing(struct spi_nor *nor)
+{
+ struct spi_nor_rww *rww = &nor->rww;
+
+ return !READ_ONCE(rww->ongoing_io) &&
+ !READ_ONCE(rww->ongoing_rd) &&
+ !READ_ONCE(rww->ongoing_pe);
+}
+
+static bool spi_nor_rww_can_start_io(struct spi_nor *nor)
+{
+ return !READ_ONCE(nor->rww.ongoing_io);
+}
+
+static bool spi_nor_rww_can_start_exclusive(struct spi_nor *nor)
+{
+ return spi_nor_rww_no_ongoing(nor);
+}
+
+static bool spi_nor_rww_can_start_pe(struct spi_nor *nor, loff_t start, size_t len)
+{
+ struct spi_nor_rww *rww = &nor->rww;
+
+ return spi_nor_rww_no_ongoing(nor) &&
+ !(READ_ONCE(rww->used_banks) &
+ spi_nor_banks_mask(nor, start, len));
+}
+
+static bool spi_nor_rww_can_start_rd(struct spi_nor *nor, loff_t start, size_t len)
+{
+ struct spi_nor_rww *rww = &nor->rww;
+
+ if (READ_ONCE(rww->ongoing_io) || READ_ONCE(rww->ongoing_rd))
+ return false;
+
+ return !(READ_ONCE(rww->used_banks) &
+ spi_nor_banks_mask(nor, start, len));
+}
+
/* Generic helpers for internal locking and serialization */
static bool spi_nor_rww_start_io(struct spi_nor *nor)
{
@@ -1189,10 +1228,19 @@ static void spi_nor_rww_end_io(struct spi_nor *nor)
static int spi_nor_lock_device(struct spi_nor *nor)
{
+ int ret;
+
if (!spi_nor_use_parallel_locking(nor))
return 0;
- return wait_event_killable(nor->rww.wait, spi_nor_rww_start_io(nor));
+ for (;;) {
+ ret = wait_event_killable(nor->rww.wait,
+ spi_nor_rww_can_start_io(nor));
+ if (ret)
+ return ret;
+ if (spi_nor_rww_start_io(nor))
+ return 0;
+ }
}
static void spi_nor_unlock_device(struct spi_nor *nor)
@@ -1243,8 +1291,14 @@ int spi_nor_prep_and_lock(struct spi_nor *nor)
return 0;
}
- ret = wait_event_killable(nor->rww.wait,
- spi_nor_rww_start_exclusive(nor));
+ for (;;) {
+ ret = wait_event_killable(nor->rww.wait,
+ spi_nor_rww_can_start_exclusive(nor));
+ if (ret)
+ break;
+ if (spi_nor_rww_start_exclusive(nor))
+ break;
+ }
if (ret)
spi_nor_unprep(nor);
@@ -1308,8 +1362,14 @@ static int spi_nor_prep_and_lock_pe(struct spi_nor *nor, loff_t start, size_t le
return 0;
}
- ret = wait_event_killable(nor->rww.wait,
- spi_nor_rww_start_pe(nor, start, len));
+ for (;;) {
+ ret = wait_event_killable(nor->rww.wait,
+ spi_nor_rww_can_start_pe(nor, start, len));
+ if (ret)
+ break;
+ if (spi_nor_rww_start_pe(nor, start, len))
+ break;
+ }
if (ret)
spi_nor_unprep(nor);
@@ -1375,8 +1435,14 @@ static int spi_nor_prep_and_lock_rd(struct spi_nor *nor, loff_t start, size_t le
return 0;
}
- ret = wait_event_killable(nor->rww.wait,
- spi_nor_rww_start_rd(nor, start, len));
+ for (;;) {
+ ret = wait_event_killable(nor->rww.wait,
+ spi_nor_rww_can_start_rd(nor, start, len));
+ if (ret)
+ break;
+ if (spi_nor_rww_start_rd(nor, start, len))
+ break;
+ }
if (ret)
spi_nor_unprep(nor);
--
2.56.0