Re: [PATCH] mmc: core: use GFP_NOIO for SD cache flush buffers

From: Linus Walleij

Date: Sun Nov 22 2026 - 04:44:14 EST


Hi Xiaolei,

thanks for your patch.

On Wed, Nov 18, 2026 at 4:32 AM Xiaolei Wang <wangxiaolei01@xxxxxxxxxxx> wrote:

> 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>

The analysis appears to have been done by an LLM because it would
take a human weeks to do this, and since you are doing other patches
on parallel I think as much is clear.

That said, the code checks out and the fix seems right, though ideally
I'd like a block maintainers ACK on it as well and I think if you fix
issues like this Jens A need to be CC:ed.

Reviewed-by: Linus Walleij <linusw@xxxxxxxxxx>

Yours,
Linus Walleij