Skip to content

Commit a46c270

Browse files
Ming Leiaxboe
authored andcommitted
blk-mq: don't schedule block kworker on isolated CPUs
Kernel parameter of `isolcpus=` or 'nohz_full=' are used to isolate CPUs for specific task, and it isn't expected to let block IO disturb these CPUs. blk-mq kworker shouldn't be scheduled on isolated CPUs. Also if isolated CPUs is run for blk-mq kworker, long block IO latency can be caused. Kernel workqueue only respects CPU isolation for WQ_UNBOUND, for bound WQ, the responsibility is on user because CPU is specified as WQ API parameter, such as mod_delayed_work_on(cpu), queue_delayed_work_on(cpu) and queue_work_on(cpu). So not run blk-mq kworker on isolated CPUs by removing isolated CPUs from hctx->cpumask. Meantime use queue map to check if all CPUs in this hw queue are offline instead of hctx->cpumask, this way can avoid any cost in fast IO code path, and is safe since hctx->cpumask are only used in the two cases. Cc: Tim Chen <[email protected]> Cc: Juri Lelli <[email protected]> Cc: Andrew Theurer <[email protected]> Cc: Joe Mario <[email protected]> Cc: Sebastian Jug <[email protected]> Cc: Frederic Weisbecker <[email protected]> Cc: Bart Van Assche <[email protected]> Cc: Tejun Heo <[email protected]> Tesed-by: Joe Mario <[email protected]> Signed-off-by: Ming Lei <[email protected]> Reviewed-by: Ewan D. Milne <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Jens Axboe <[email protected]>
1 parent 7d8d357 commit a46c270

File tree

1 file changed

+47
-10
lines changed

1 file changed

+47
-10
lines changed

block/blk-mq.c

Lines changed: 47 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
#include <linux/prefetch.h>
2929
#include <linux/blk-crypto.h>
3030
#include <linux/part_stat.h>
31+
#include <linux/sched/isolation.h>
3132

3233
#include <trace/events/block.h>
3334

@@ -2163,6 +2164,15 @@ static inline int blk_mq_first_mapped_cpu(struct blk_mq_hw_ctx *hctx)
21632164
return cpu;
21642165
}
21652166

2167+
/*
2168+
* ->next_cpu is always calculated from hctx->cpumask, so simply use
2169+
* it for speeding up the check
2170+
*/
2171+
static bool blk_mq_hctx_empty_cpumask(struct blk_mq_hw_ctx *hctx)
2172+
{
2173+
return hctx->next_cpu >= nr_cpu_ids;
2174+
}
2175+
21662176
/*
21672177
* It'd be great if the workqueue API had a way to pass
21682178
* in a mask and had some smarts for more clever placement.
@@ -2174,7 +2184,8 @@ static int blk_mq_hctx_next_cpu(struct blk_mq_hw_ctx *hctx)
21742184
bool tried = false;
21752185
int next_cpu = hctx->next_cpu;
21762186

2177-
if (hctx->queue->nr_hw_queues == 1)
2187+
/* Switch to unbound if no allowable CPUs in this hctx */
2188+
if (hctx->queue->nr_hw_queues == 1 || blk_mq_hctx_empty_cpumask(hctx))
21782189
return WORK_CPU_UNBOUND;
21792190

21802191
if (--hctx->next_cpu_batch <= 0) {
@@ -3483,23 +3494,38 @@ static bool blk_mq_hctx_has_requests(struct blk_mq_hw_ctx *hctx)
34833494
return data.has_rq;
34843495
}
34853496

3486-
static inline bool blk_mq_last_cpu_in_hctx(unsigned int cpu,
3487-
struct blk_mq_hw_ctx *hctx)
3497+
static bool blk_mq_hctx_has_online_cpu(struct blk_mq_hw_ctx *hctx,
3498+
unsigned int this_cpu)
34883499
{
3489-
if (cpumask_first_and(hctx->cpumask, cpu_online_mask) != cpu)
3490-
return false;
3491-
if (cpumask_next_and(cpu, hctx->cpumask, cpu_online_mask) < nr_cpu_ids)
3492-
return false;
3493-
return true;
3500+
enum hctx_type type = hctx->type;
3501+
int cpu;
3502+
3503+
/*
3504+
* hctx->cpumask has to rule out isolated CPUs, but userspace still
3505+
* might submit IOs on these isolated CPUs, so use the queue map to
3506+
* check if all CPUs mapped to this hctx are offline
3507+
*/
3508+
for_each_online_cpu(cpu) {
3509+
struct blk_mq_hw_ctx *h = blk_mq_map_queue_type(hctx->queue,
3510+
type, cpu);
3511+
3512+
if (h != hctx)
3513+
continue;
3514+
3515+
/* this hctx has at least one online CPU */
3516+
if (this_cpu != cpu)
3517+
return true;
3518+
}
3519+
3520+
return false;
34943521
}
34953522

34963523
static int blk_mq_hctx_notify_offline(unsigned int cpu, struct hlist_node *node)
34973524
{
34983525
struct blk_mq_hw_ctx *hctx = hlist_entry_safe(node,
34993526
struct blk_mq_hw_ctx, cpuhp_online);
35003527

3501-
if (!cpumask_test_cpu(cpu, hctx->cpumask) ||
3502-
!blk_mq_last_cpu_in_hctx(cpu, hctx))
3528+
if (blk_mq_hctx_has_online_cpu(hctx, cpu))
35033529
return 0;
35043530

35053531
/*
@@ -3907,6 +3933,8 @@ static void blk_mq_map_swqueue(struct request_queue *q)
39073933
}
39083934

39093935
queue_for_each_hw_ctx(q, hctx, i) {
3936+
int cpu;
3937+
39103938
/*
39113939
* If no software queues are mapped to this hardware queue,
39123940
* disable it and free the request entries.
@@ -3933,6 +3961,15 @@ static void blk_mq_map_swqueue(struct request_queue *q)
39333961
*/
39343962
sbitmap_resize(&hctx->ctx_map, hctx->nr_ctx);
39353963

3964+
/*
3965+
* Rule out isolated CPUs from hctx->cpumask to avoid
3966+
* running block kworker on isolated CPUs
3967+
*/
3968+
for_each_cpu(cpu, hctx->cpumask) {
3969+
if (cpu_is_isolated(cpu))
3970+
cpumask_clear_cpu(cpu, hctx->cpumask);
3971+
}
3972+
39363973
/*
39373974
* Initialize batch roundrobin counts
39383975
*/

0 commit comments

Comments
 (0)