[PATCH 4/4] mtd: spi-nor: Prevent KCSAN warnings in the RWW path

From: Miquel Raynal

Date: Thu Nov 19 2026 - 17:14:44 EST


The RWW wait predicates expectedly sample the "ongoing" state of the
chips without nor->lock. Mark the state updates with WRITE_ONCE() to
pair with the existing READ_ONCE() to avoid possible KCSAN warnings.

Signed-off-by: Miquel Raynal <miquel.raynal@xxxxxxxxxxx>
---
drivers/mtd/spi-nor/core.c | 46 ++++++++++++++++++++++++----------------------
1 file changed, 24 insertions(+), 22 deletions(-)

diff --git a/drivers/mtd/spi-nor/core.c b/drivers/mtd/spi-nor/core.c
index d46c18e4401c..623275637495 100644
--- a/drivers/mtd/spi-nor/core.c
+++ b/drivers/mtd/spi-nor/core.c
@@ -579,8 +579,8 @@ static int spi_nor_rww_start_rdst(struct spi_nor *nor)
if (rww->ongoing_io || rww->ongoing_rd)
return -EAGAIN;

- rww->ongoing_io = true;
- rww->ongoing_rd = true;
+ WRITE_ONCE(rww->ongoing_io, true);
+ WRITE_ONCE(rww->ongoing_rd, true);

return 0;
}
@@ -591,8 +591,8 @@ static void spi_nor_rww_end_rdst(struct spi_nor *nor)

guard(mutex)(&nor->lock);

- rww->ongoing_io = false;
- rww->ongoing_rd = false;
+ WRITE_ONCE(rww->ongoing_io, false);
+ WRITE_ONCE(rww->ongoing_rd, false);
}

static int spi_nor_lock_rdst(struct spi_nor *nor)
@@ -1215,7 +1215,7 @@ static bool spi_nor_rww_start_io(struct spi_nor *nor)
if (rww->ongoing_io)
return false;

- rww->ongoing_io = true;
+ WRITE_ONCE(rww->ongoing_io, true);

return true;
}
@@ -1223,7 +1223,7 @@ static bool spi_nor_rww_start_io(struct spi_nor *nor)
static void spi_nor_rww_end_io(struct spi_nor *nor)
{
guard(mutex)(&nor->lock);
- nor->rww.ongoing_io = false;
+ WRITE_ONCE(nor->rww.ongoing_io, false);
}

static int spi_nor_lock_device(struct spi_nor *nor)
@@ -1261,9 +1261,9 @@ static bool spi_nor_rww_start_exclusive(struct spi_nor *nor)
if (rww->ongoing_io || rww->ongoing_rd || rww->ongoing_pe)
return false;

- rww->ongoing_io = true;
- rww->ongoing_rd = true;
- rww->ongoing_pe = true;
+ WRITE_ONCE(rww->ongoing_io, true);
+ WRITE_ONCE(rww->ongoing_rd, true);
+ WRITE_ONCE(rww->ongoing_pe, true);

return true;
}
@@ -1273,9 +1273,9 @@ static void spi_nor_rww_end_exclusive(struct spi_nor *nor)
struct spi_nor_rww *rww = &nor->rww;

guard(mutex)(&nor->lock);
- rww->ongoing_io = false;
- rww->ongoing_rd = false;
- rww->ongoing_pe = false;
+ WRITE_ONCE(rww->ongoing_io, false);
+ WRITE_ONCE(rww->ongoing_rd, false);
+ WRITE_ONCE(rww->ongoing_pe, false);
}

int spi_nor_prep_and_lock(struct spi_nor *nor)
@@ -1333,8 +1333,8 @@ static bool spi_nor_rww_start_pe(struct spi_nor *nor, loff_t start, size_t len)
if (rww->used_banks & banks)
return false;

- rww->used_banks |= banks;
- rww->ongoing_pe = true;
+ WRITE_ONCE(rww->used_banks, rww->used_banks | banks);
+ WRITE_ONCE(rww->ongoing_pe, true);

return true;
}
@@ -1345,8 +1345,9 @@ static void spi_nor_rww_end_pe(struct spi_nor *nor, loff_t start, size_t len)

guard(mutex)(&nor->lock);

- rww->used_banks &= ~spi_nor_banks_mask(nor, start, len);
- rww->ongoing_pe = false;
+ WRITE_ONCE(rww->used_banks,
+ rww->used_banks & ~spi_nor_banks_mask(nor, start, len));
+ WRITE_ONCE(rww->ongoing_pe, false);
}

static int spi_nor_prep_and_lock_pe(struct spi_nor *nor, loff_t start, size_t len)
@@ -1404,9 +1405,9 @@ static bool spi_nor_rww_start_rd(struct spi_nor *nor, loff_t start, size_t len)
if (rww->used_banks & banks)
return false;

- rww->used_banks |= banks;
- rww->ongoing_io = true;
- rww->ongoing_rd = true;
+ WRITE_ONCE(rww->used_banks, rww->used_banks | banks);
+ WRITE_ONCE(rww->ongoing_io, true);
+ WRITE_ONCE(rww->ongoing_rd, true);

return true;
}
@@ -1417,9 +1418,10 @@ static void spi_nor_rww_end_rd(struct spi_nor *nor, loff_t start, size_t len)

guard(mutex)(&nor->lock);

- rww->used_banks &= ~spi_nor_banks_mask(nor, start, len);
- rww->ongoing_io = false;
- rww->ongoing_rd = false;
+ WRITE_ONCE(rww->used_banks,
+ rww->used_banks & ~spi_nor_banks_mask(nor, start, len));
+ WRITE_ONCE(rww->ongoing_io, false);
+ WRITE_ONCE(rww->ongoing_rd, false);
}

static int spi_nor_prep_and_lock_rd(struct spi_nor *nor, loff_t start, size_t len)

--
2.56.0