diff --git a/block/blk-mq.c b/block/blk-mq.c index 5ae22d5565404..3241bec90fb2e 100644 --- a/block/blk-mq.c +++ b/block/blk-mq.c @@ -717,6 +717,25 @@ struct request *blk_mq_alloc_request(struct request_queue *q, blk_opf_t opf, } EXPORT_SYMBOL(blk_mq_alloc_request); +/* + * Return an online CPU which is neither isolated from the scheduler domains + * nor from managed interrupts, or nr_cpu_ids if there is none. + */ +static unsigned int blk_mq_housekeeping_cpu(void) +{ + unsigned int cpu; + + rcu_read_lock(); + for_each_online_cpu(cpu) { + if (!cpu_is_isolated(cpu) && + housekeeping_test_cpu(cpu, HK_TYPE_MANAGED_IRQ)) + break; + } + rcu_read_unlock(); + + return cpu; +} + struct request *blk_mq_alloc_request_hctx(struct request_queue *q, blk_opf_t opf, blk_mq_req_flags_t flags, unsigned int hctx_idx) { @@ -767,8 +786,24 @@ struct request *blk_mq_alloc_request_hctx(struct request_queue *q, if (!blk_mq_hw_queue_mapped(data.hctx)) goto out_queue_exit; cpu = cpumask_first_and(data.hctx->cpumask, cpu_online_mask); - if (cpu >= nr_cpu_ids) - goto out_queue_exit; + if (cpu >= nr_cpu_ids) { + /* + * Only isolated or offline CPUs are mapped to this hardware + * context. Isolated CPUs are not part of hctx->cpumask, and + * offline CPUs may come online later and need the hardware + * context then. Passthrough requests are inserted directly + * into hctx->dispatch, so use the software context of a + * housekeeping CPU for them. Inactive hardware contexts lost + * their last online CPU and were drained for the CPU offline, + * don't allocate requests on them. + */ + if (!blk_op_is_passthrough(opf) || + test_bit(BLK_MQ_S_INACTIVE, &data.hctx->state)) + goto out_queue_exit; + cpu = blk_mq_housekeeping_cpu(); + if (cpu >= nr_cpu_ids) + goto out_queue_exit; + } data.ctx = __blk_mq_get_ctx(q, cpu); if (q->elevator)