[PATCH] mmc: core: use GFP_NOIO for SD cache flush buffers
From: Xiaolei Wang
Date: Tue Nov 17 2026 - 22:32:46 EST
sd_flush_cache() is called from mmc_blk_mq_issue_rq()'s REQ_OP_FLUSH
branch, which runs in the block layer dispatch path with the queue usage
counter held. Both it and sd_write_ext_reg(), which it calls, allocate
a 512-byte buffer with GFP_KERNEL. Under memory pressure that
allocation enters filesystem reclaim, and reclaim may submit I/O back to
the same request queue, closing a cycle against queue freeze:
CPU0 CPU1
---- ----
lock(&q->q_usage_counter(io));
lock(fs_reclaim);
lock(&q->q_usage_counter(io));
lock(&q->elevator_lock);
lockdep reports it on an arm64 Raspberry Pi 5 that boots from an SD
card, with the flush dispatched by blk_mq_requeue_work and the elevator
switch in another thread:
-> #2 (fs_reclaim):
fs_reclaim_acquire <- __kmalloc_cache_noprof <- sd_flush_cache
<- mmc_blk_mq_issue_rq <- mmc_mq_queue_rq
<- blk_mq_dispatch_rq_list
blk_alloc_queue() declares this ordering explicitly and teaches lockdep
about it:
/* Teach lockdep about lock ordering (reclaim WRT queue freeze lock). */
fs_reclaim_acquire(GFP_KERNEL);
rwsem_acquire_read(&q->io_lockdep_map, 0, 0, _RET_IP_);
rwsem_release(&q->io_lockdep_map, _RET_IP_);
fs_reclaim_release(GFP_KERNEL);
Taking q_usage_counter(io) and then entering reclaim is the reverse of
that contract. GFP_NOIO drops __GFP_FS and __GFP_IO, so neither the
lockdep dependency nor the writeback that could resubmit to this queue
can happen any more.
sd_write_ext_reg() is also called from sd_enable_cache() (probe) and
sd_poweroff_notify() (suspend). NOIO costs nothing on those paths: the
allocation is order-0 and 512 bytes, and reclaim without writeback is
still available. Keeping a single gfp flavour in the helper makes its
semantics independent of the caller.
Fixes: 130206a615a9 ("mmc: core: Add support for cache ctrl for SD cards")
Cc: stable@xxxxxxxxxxxxxxx
Signed-off-by: Xiaolei Wang <wangxiaolei01@xxxxxxxxxxx>
---
drivers/mmc/core/sd.c | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)
diff --git a/drivers/mmc/core/sd.c b/drivers/mmc/core/sd.c
index c763efb10f64..88d0ddfe007a 100644
--- a/drivers/mmc/core/sd.c
+++ b/drivers/mmc/core/sd.c
@@ -1041,7 +1041,7 @@ static int sd_write_ext_reg(struct mmc_card *card, u8 fno, u8 page, u16 offset,
struct scatterlist sg;
u8 *reg_buf;
- reg_buf = kzalloc(512, GFP_KERNEL);
+ reg_buf = kzalloc(512, GFP_NOIO);
if (!reg_buf)
return -ENOMEM;
@@ -1335,7 +1335,7 @@ static int sd_flush_cache(struct mmc_host *host)
if (!sd_cache_enabled(host))
return 0;
- reg_buf = kzalloc(512, GFP_KERNEL);
+ reg_buf = kzalloc(512, GFP_NOIO);
if (!reg_buf)
return -ENOMEM;
--
2.34.1