Back to home page

LXR

 
 

    


0001 /*
0002  * Functions related to softirq rq completions
0003  */
0004 #include <linux/kernel.h>
0005 #include <linux/module.h>
0006 #include <linux/init.h>
0007 #include <linux/bio.h>
0008 #include <linux/blkdev.h>
0009 #include <linux/interrupt.h>
0010 #include <linux/cpu.h>
0011 #include <linux/sched.h>
0012 
0013 #include "blk.h"
0014 
0015 static DEFINE_PER_CPU(struct list_head, blk_cpu_done);
0016 
0017 /*
0018  * Softirq action handler - move entries to local list and loop over them
0019  * while passing them to the queue registered handler.
0020  */
0021 static __latent_entropy void blk_done_softirq(struct softirq_action *h)
0022 {
0023     struct list_head *cpu_list, local_list;
0024 
0025     local_irq_disable();
0026     cpu_list = this_cpu_ptr(&blk_cpu_done);
0027     list_replace_init(cpu_list, &local_list);
0028     local_irq_enable();
0029 
0030     while (!list_empty(&local_list)) {
0031         struct request *rq;
0032 
0033         rq = list_entry(local_list.next, struct request, ipi_list);
0034         list_del_init(&rq->ipi_list);
0035         rq->q->softirq_done_fn(rq);
0036     }
0037 }
0038 
0039 #ifdef CONFIG_SMP
0040 static void trigger_softirq(void *data)
0041 {
0042     struct request *rq = data;
0043     unsigned long flags;
0044     struct list_head *list;
0045 
0046     local_irq_save(flags);
0047     list = this_cpu_ptr(&blk_cpu_done);
0048     list_add_tail(&rq->ipi_list, list);
0049 
0050     if (list->next == &rq->ipi_list)
0051         raise_softirq_irqoff(BLOCK_SOFTIRQ);
0052 
0053     local_irq_restore(flags);
0054 }
0055 
0056 /*
0057  * Setup and invoke a run of 'trigger_softirq' on the given cpu.
0058  */
0059 static int raise_blk_irq(int cpu, struct request *rq)
0060 {
0061     if (cpu_online(cpu)) {
0062         struct call_single_data *data = &rq->csd;
0063 
0064         data->func = trigger_softirq;
0065         data->info = rq;
0066         data->flags = 0;
0067 
0068         smp_call_function_single_async(cpu, data);
0069         return 0;
0070     }
0071 
0072     return 1;
0073 }
0074 #else /* CONFIG_SMP */
0075 static int raise_blk_irq(int cpu, struct request *rq)
0076 {
0077     return 1;
0078 }
0079 #endif
0080 
0081 static int blk_softirq_cpu_dead(unsigned int cpu)
0082 {
0083     /*
0084      * If a CPU goes away, splice its entries to the current CPU
0085      * and trigger a run of the softirq
0086      */
0087     local_irq_disable();
0088     list_splice_init(&per_cpu(blk_cpu_done, cpu),
0089              this_cpu_ptr(&blk_cpu_done));
0090     raise_softirq_irqoff(BLOCK_SOFTIRQ);
0091     local_irq_enable();
0092 
0093     return 0;
0094 }
0095 
0096 void __blk_complete_request(struct request *req)
0097 {
0098     int ccpu, cpu;
0099     struct request_queue *q = req->q;
0100     unsigned long flags;
0101     bool shared = false;
0102 
0103     BUG_ON(!q->softirq_done_fn);
0104 
0105     local_irq_save(flags);
0106     cpu = smp_processor_id();
0107 
0108     /*
0109      * Select completion CPU
0110      */
0111     if (req->cpu != -1) {
0112         ccpu = req->cpu;
0113         if (!test_bit(QUEUE_FLAG_SAME_FORCE, &q->queue_flags))
0114             shared = cpus_share_cache(cpu, ccpu);
0115     } else
0116         ccpu = cpu;
0117 
0118     /*
0119      * If current CPU and requested CPU share a cache, run the softirq on
0120      * the current CPU. One might concern this is just like
0121      * QUEUE_FLAG_SAME_FORCE, but actually not. blk_complete_request() is
0122      * running in interrupt handler, and currently I/O controller doesn't
0123      * support multiple interrupts, so current CPU is unique actually. This
0124      * avoids IPI sending from current CPU to the first CPU of a group.
0125      */
0126     if (ccpu == cpu || shared) {
0127         struct list_head *list;
0128 do_local:
0129         list = this_cpu_ptr(&blk_cpu_done);
0130         list_add_tail(&req->ipi_list, list);
0131 
0132         /*
0133          * if the list only contains our just added request,
0134          * signal a raise of the softirq. If there are already
0135          * entries there, someone already raised the irq but it
0136          * hasn't run yet.
0137          */
0138         if (list->next == &req->ipi_list)
0139             raise_softirq_irqoff(BLOCK_SOFTIRQ);
0140     } else if (raise_blk_irq(ccpu, req))
0141         goto do_local;
0142 
0143     local_irq_restore(flags);
0144 }
0145 
0146 /**
0147  * blk_complete_request - end I/O on a request
0148  * @req:      the request being processed
0149  *
0150  * Description:
0151  *     Ends all I/O on a request. It does not handle partial completions,
0152  *     unless the driver actually implements this in its completion callback
0153  *     through requeueing. The actual completion happens out-of-order,
0154  *     through a softirq handler. The user must have registered a completion
0155  *     callback through blk_queue_softirq_done().
0156  **/
0157 void blk_complete_request(struct request *req)
0158 {
0159     if (unlikely(blk_should_fake_timeout(req->q)))
0160         return;
0161     if (!blk_mark_rq_complete(req))
0162         __blk_complete_request(req);
0163 }
0164 EXPORT_SYMBOL(blk_complete_request);
0165 
0166 static __init int blk_softirq_init(void)
0167 {
0168     int i;
0169 
0170     for_each_possible_cpu(i)
0171         INIT_LIST_HEAD(&per_cpu(blk_cpu_done, i));
0172 
0173     open_softirq(BLOCK_SOFTIRQ, blk_done_softirq);
0174     cpuhp_setup_state_nocalls(CPUHP_BLOCK_SOFTIRQ_DEAD,
0175                   "block/softirq:dead", NULL,
0176                   blk_softirq_cpu_dead);
0177     return 0;
0178 }
0179 subsys_initcall(blk_softirq_init);