[PATCH v3 2/7] spi: Allow spi_delay_exec() to be used from atomic contexts

Heiko Stuebner heiko at sntech.de
Tue Nov 17 15:50:16 PST 2026


From: Heiko Stuebner <heiko.stuebner at cherry.de>

When using spi_delay_exec() only the fsleep in the subsequent
_spi_transfer_delay_ns() can actually sleep.

As we might want to use spi_delay_exec() from atomic contexts in the
future, move the might_sleep() up to the actual place that matters
and fall back to the existing ndelay if called from an atomic context.

Signed-off-by: Heiko Stuebner <heiko.stuebner at cherry.de>
---
 drivers/spi/spi.c | 5 ++---
 1 file changed, 2 insertions(+), 3 deletions(-)

diff --git a/drivers/spi/spi.c b/drivers/spi/spi.c
index 3aeee3b11c0c..8a82d7f5b9fa 100644
--- a/drivers/spi/spi.c
+++ b/drivers/spi/spi.c
@@ -1714,11 +1714,12 @@ static void _spi_transfer_delay_ns(u32 ns)
 {
 	if (!ns)
 		return;
-	if (ns <= NSEC_PER_USEC) {
+	if (ns <= NSEC_PER_USEC || spi_in_atomic()) {
 		ndelay(ns);
 	} else {
 		u32 us = DIV_ROUND_UP(ns, NSEC_PER_USEC);
 
+		might_sleep();
 		fsleep(us);
 	}
 }
@@ -1766,8 +1767,6 @@ int spi_delay_exec(struct spi_delay *_delay, struct spi_transfer *xfer)
 {
 	int delay;
 
-	might_sleep();
-
 	if (!_delay)
 		return -EINVAL;
 
-- 
2.53.0




More information about the Linux-rockchip mailing list