From: Daniel Wagner When isolcpus=io_queue is enabled and the last housekeeping CPU for a given hctx goes offline, no CPU would be left to handle I/O. To prevent I/O stalls, disallow offlining housekeeping CPUs that are still serving isolated CPUs. Signed-off-by: Daniel Wagner Reviewed-by: Hannes Reinecke [atomlin: - Removed duplicate paragraph from commit message - Allow offlining of non-housekeeping CPUs - Fix logic flaw that prematurely rejected valid offline requests - Iterated over cpu_online_mask and manually reverse-mapped CPUs to correctly detect isolated CPUs, as blk_mq_map_swqueue() intentionally prunes them from hctx->cpumask - Prevented a TOCTOU NULL pointer dereference race against concurrent device teardown by using READ_ONCE() to fetch the disk pointer] Signed-off-by: Aaron Tomlin --- block/blk-mq.c | 56 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) diff --git a/block/blk-mq.c b/block/blk-mq.c index 4c5c16cce4f8..afe0c0bf7e8a 100644 --- a/block/blk-mq.c +++ b/block/blk-mq.c @@ -3720,6 +3720,57 @@ static bool blk_mq_hctx_has_requests(struct blk_mq_hw_ctx *hctx) return data.has_rq; } +static bool blk_mq_hctx_can_offline_hk_cpu(struct blk_mq_hw_ctx *hctx, + unsigned int this_cpu) +{ + const struct cpumask *hk_mask = housekeeping_cpumask(HK_TYPE_IO_QUEUE); + struct gendisk *disk; + int cpu, fallback_isolated_cpu = -1; + + /* + * If the CPU being offlined is not a housekeeping CPU, + * offlining it will not strand isolated CPUs. Allow it. + */ + if (!cpumask_test_cpu(this_cpu, hk_mask)) + return true; + /* + * Iterate over all online CPUs and manually check their mapping. + * We cannot use hctx->cpumask here because blk_mq_map_swqueue() + * intentionally strips isolated CPUs from it to prevent kworker + * routing. + */ + for_each_online_cpu(cpu) { + struct blk_mq_hw_ctx *h; + + if (cpu == this_cpu) + continue; + + h = blk_mq_map_queue_type(hctx->queue, hctx->type, cpu); + if (h != hctx) + continue; + + if (cpumask_test_cpu(cpu, hk_mask)) + return true; + + if (fallback_isolated_cpu == -1) + fallback_isolated_cpu = cpu; + } + + if (fallback_isolated_cpu != -1) { + /* + * Use READ_ONCE() to prevent compiler double-fetch TOCTOU + * issues if the disk is removed concurrently. + */ + disk = READ_ONCE(hctx->queue->disk); + pr_warn("%s: trying to offline hctx%d but online isolated CPU %d is still mapped to it\n", + disk ? disk->disk_name : "?", hctx->queue_num, + fallback_isolated_cpu); + return false; + } + + return true; +} + static bool blk_mq_hctx_has_online_cpu(struct blk_mq_hw_ctx *hctx, unsigned int this_cpu) { @@ -3752,6 +3803,11 @@ static int blk_mq_hctx_notify_offline(unsigned int cpu, struct hlist_node *node) struct blk_mq_hw_ctx, cpuhp_online); int ret = 0; + if (housekeeping_enabled(HK_TYPE_IO_QUEUE)) { + if (!blk_mq_hctx_can_offline_hk_cpu(hctx, cpu)) + return -EINVAL; + } + if (!hctx->nr_ctx || blk_mq_hctx_has_online_cpu(hctx, cpu)) return 0; -- 2.51.0