Skip to content

Commit f9ab491

Browse files
Sebastian Andrzej Siewioraxboe
authored andcommitted
blk-mq: Use llist_head for blk_cpu_done
With llist_head it is possible to avoid the locking (the irq-off region) when items are added. This makes it possible to add items on a remote CPU without additional locking. llist_add() returns true if the list was previously empty. This can be used to invoke the SMP function call / raise sofirq only if the first item was added (otherwise it is already pending). This simplifies the code a little and reduces the IRQ-off regions. blk_mq_raise_softirq() needs a preempt-disable section to ensure the request is enqueued on the same CPU as the softirq is raised. Some callers (USB-storage) invoke this path in preemptible context. Reviewed-by: Christoph Hellwig <[email protected]> Reviewed-by: Daniel Wagner <[email protected]> Signed-off-by: Sebastian Andrzej Siewior <[email protected]> Signed-off-by: Jens Axboe <[email protected]>
1 parent 0a2efaf commit f9ab491

File tree

2 files changed

+42
-57
lines changed

2 files changed

+42
-57
lines changed

block/blk-mq.c

Lines changed: 41 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@
4141
#include "blk-mq-sched.h"
4242
#include "blk-rq-qos.h"
4343

44-
static DEFINE_PER_CPU(struct list_head, blk_cpu_done);
44+
static DEFINE_PER_CPU(struct llist_head, blk_cpu_done);
4545

4646
static void blk_mq_poll_stats_start(struct request_queue *q);
4747
static void blk_mq_poll_stats_fn(struct blk_stat_callback *cb);
@@ -567,68 +567,29 @@ void blk_mq_end_request(struct request *rq, blk_status_t error)
567567
}
568568
EXPORT_SYMBOL(blk_mq_end_request);
569569

570-
/*
571-
* Softirq action handler - move entries to local list and loop over them
572-
* while passing them to the queue registered handler.
573-
*/
574-
static __latent_entropy void blk_done_softirq(struct softirq_action *h)
570+
static void blk_complete_reqs(struct llist_head *list)
575571
{
576-
struct list_head *cpu_list, local_list;
577-
578-
local_irq_disable();
579-
cpu_list = this_cpu_ptr(&blk_cpu_done);
580-
list_replace_init(cpu_list, &local_list);
581-
local_irq_enable();
582-
583-
while (!list_empty(&local_list)) {
584-
struct request *rq;
572+
struct llist_node *entry = llist_reverse_order(llist_del_all(list));
573+
struct request *rq, *next;
585574

586-
rq = list_entry(local_list.next, struct request, ipi_list);
587-
list_del_init(&rq->ipi_list);
575+
llist_for_each_entry_safe(rq, next, entry, ipi_list)
588576
rq->q->mq_ops->complete(rq);
589-
}
590577
}
591578

592-
static void blk_mq_trigger_softirq(struct request *rq)
579+
static __latent_entropy void blk_done_softirq(struct softirq_action *h)
593580
{
594-
struct list_head *list;
595-
unsigned long flags;
596-
597-
local_irq_save(flags);
598-
list = this_cpu_ptr(&blk_cpu_done);
599-
list_add_tail(&rq->ipi_list, list);
600-
601-
/*
602-
* If the list only contains our just added request, signal a raise of
603-
* the softirq. If there are already entries there, someone already
604-
* raised the irq but it hasn't run yet.
605-
*/
606-
if (list->next == &rq->ipi_list)
607-
raise_softirq_irqoff(BLOCK_SOFTIRQ);
608-
local_irq_restore(flags);
581+
blk_complete_reqs(this_cpu_ptr(&blk_cpu_done));
609582
}
610583

611584
static int blk_softirq_cpu_dead(unsigned int cpu)
612585
{
613-
/*
614-
* If a CPU goes away, splice its entries to the current CPU
615-
* and trigger a run of the softirq
616-
*/
617-
local_irq_disable();
618-
list_splice_init(&per_cpu(blk_cpu_done, cpu),
619-
this_cpu_ptr(&blk_cpu_done));
620-
raise_softirq_irqoff(BLOCK_SOFTIRQ);
621-
local_irq_enable();
622-
586+
blk_complete_reqs(&per_cpu(blk_cpu_done, cpu));
623587
return 0;
624588
}
625589

626-
627590
static void __blk_mq_complete_request_remote(void *data)
628591
{
629-
struct request *rq = data;
630-
631-
blk_mq_trigger_softirq(rq);
592+
__raise_softirq_irqoff(BLOCK_SOFTIRQ);
632593
}
633594

634595
static inline bool blk_mq_complete_need_ipi(struct request *rq)
@@ -657,6 +618,30 @@ static inline bool blk_mq_complete_need_ipi(struct request *rq)
657618
return cpu_online(rq->mq_ctx->cpu);
658619
}
659620

621+
static void blk_mq_complete_send_ipi(struct request *rq)
622+
{
623+
struct llist_head *list;
624+
unsigned int cpu;
625+
626+
cpu = rq->mq_ctx->cpu;
627+
list = &per_cpu(blk_cpu_done, cpu);
628+
if (llist_add(&rq->ipi_list, list)) {
629+
INIT_CSD(&rq->csd, __blk_mq_complete_request_remote, rq);
630+
smp_call_function_single_async(cpu, &rq->csd);
631+
}
632+
}
633+
634+
static void blk_mq_raise_softirq(struct request *rq)
635+
{
636+
struct llist_head *list;
637+
638+
preempt_disable();
639+
list = this_cpu_ptr(&blk_cpu_done);
640+
if (llist_add(&rq->ipi_list, list))
641+
raise_softirq(BLOCK_SOFTIRQ);
642+
preempt_enable();
643+
}
644+
660645
bool blk_mq_complete_request_remote(struct request *rq)
661646
{
662647
WRITE_ONCE(rq->state, MQ_RQ_COMPLETE);
@@ -669,15 +654,15 @@ bool blk_mq_complete_request_remote(struct request *rq)
669654
return false;
670655

671656
if (blk_mq_complete_need_ipi(rq)) {
672-
INIT_CSD(&rq->csd, __blk_mq_complete_request_remote, rq);
673-
smp_call_function_single_async(rq->mq_ctx->cpu, &rq->csd);
674-
} else {
675-
if (rq->q->nr_hw_queues > 1)
676-
return false;
677-
blk_mq_trigger_softirq(rq);
657+
blk_mq_complete_send_ipi(rq);
658+
return true;
678659
}
679660

680-
return true;
661+
if (rq->q->nr_hw_queues == 1) {
662+
blk_mq_raise_softirq(rq);
663+
return true;
664+
}
665+
return false;
681666
}
682667
EXPORT_SYMBOL_GPL(blk_mq_complete_request_remote);
683668

@@ -3892,7 +3877,7 @@ static int __init blk_mq_init(void)
38923877
int i;
38933878

38943879
for_each_possible_cpu(i)
3895-
INIT_LIST_HEAD(&per_cpu(blk_cpu_done, i));
3880+
init_llist_head(&per_cpu(blk_cpu_done, i));
38963881
open_softirq(BLOCK_SOFTIRQ, blk_done_softirq);
38973882

38983883
cpuhp_setup_state_nocalls(CPUHP_BLOCK_SOFTIRQ_DEAD,

include/linux/blkdev.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ struct request {
153153
*/
154154
union {
155155
struct hlist_node hash; /* merge hash */
156-
struct list_head ipi_list;
156+
struct llist_node ipi_list;
157157
};
158158

159159
/*

0 commit comments

Comments
 (0)