diff options
Diffstat (limited to 'kernel/smp.c')
| -rw-r--r-- | kernel/smp.c | 432 |
1 files changed, 236 insertions, 196 deletions
diff --git a/kernel/smp.c b/kernel/smp.c index bbedbb7efe3..858baac568e 100644 --- a/kernel/smp.c +++ b/kernel/smp.c | |||
| @@ -2,40 +2,82 @@ | |||
| 2 | * Generic helpers for smp ipi calls | 2 | * Generic helpers for smp ipi calls |
| 3 | * | 3 | * |
| 4 | * (C) Jens Axboe <jens.axboe@oracle.com> 2008 | 4 | * (C) Jens Axboe <jens.axboe@oracle.com> 2008 |
| 5 | * | ||
| 6 | */ | 5 | */ |
| 7 | #include <linux/init.h> | ||
| 8 | #include <linux/module.h> | ||
| 9 | #include <linux/percpu.h> | ||
| 10 | #include <linux/rcupdate.h> | 6 | #include <linux/rcupdate.h> |
| 11 | #include <linux/rculist.h> | 7 | #include <linux/rculist.h> |
| 8 | #include <linux/kernel.h> | ||
| 9 | #include <linux/module.h> | ||
| 10 | #include <linux/percpu.h> | ||
| 11 | #include <linux/init.h> | ||
| 12 | #include <linux/smp.h> | 12 | #include <linux/smp.h> |
| 13 | #include <linux/cpu.h> | ||
| 13 | 14 | ||
| 14 | static DEFINE_PER_CPU(struct call_single_queue, call_single_queue); | 15 | static DEFINE_PER_CPU(struct call_single_queue, call_single_queue); |
| 15 | static LIST_HEAD(call_function_queue); | 16 | |
| 16 | __cacheline_aligned_in_smp DEFINE_SPINLOCK(call_function_lock); | 17 | static struct { |
| 18 | struct list_head queue; | ||
| 19 | spinlock_t lock; | ||
| 20 | } call_function __cacheline_aligned_in_smp = | ||
| 21 | { | ||
| 22 | .queue = LIST_HEAD_INIT(call_function.queue), | ||
| 23 | .lock = __SPIN_LOCK_UNLOCKED(call_function.lock), | ||
| 24 | }; | ||
| 17 | 25 | ||
| 18 | enum { | 26 | enum { |
| 19 | CSD_FLAG_WAIT = 0x01, | 27 | CSD_FLAG_LOCK = 0x01, |
| 20 | CSD_FLAG_ALLOC = 0x02, | ||
| 21 | CSD_FLAG_LOCK = 0x04, | ||
| 22 | }; | 28 | }; |
| 23 | 29 | ||
| 24 | struct call_function_data { | 30 | struct call_function_data { |
| 25 | struct call_single_data csd; | 31 | struct call_single_data csd; |
| 26 | spinlock_t lock; | 32 | spinlock_t lock; |
| 27 | unsigned int refs; | 33 | unsigned int refs; |
| 28 | struct rcu_head rcu_head; | 34 | cpumask_var_t cpumask; |
| 29 | unsigned long cpumask_bits[]; | ||
| 30 | }; | 35 | }; |
| 31 | 36 | ||
| 32 | struct call_single_queue { | 37 | struct call_single_queue { |
| 33 | struct list_head list; | 38 | struct list_head list; |
| 34 | spinlock_t lock; | 39 | spinlock_t lock; |
| 40 | }; | ||
| 41 | |||
| 42 | static DEFINE_PER_CPU(struct call_function_data, cfd_data) = { | ||
| 43 | .lock = __SPIN_LOCK_UNLOCKED(cfd_data.lock), | ||
| 44 | }; | ||
| 45 | |||
| 46 | static int | ||
| 47 | hotplug_cfd(struct notifier_block *nfb, unsigned long action, void *hcpu) | ||
| 48 | { | ||
| 49 | long cpu = (long)hcpu; | ||
| 50 | struct call_function_data *cfd = &per_cpu(cfd_data, cpu); | ||
| 51 | |||
| 52 | switch (action) { | ||
| 53 | case CPU_UP_PREPARE: | ||
| 54 | case CPU_UP_PREPARE_FROZEN: | ||
| 55 | if (!alloc_cpumask_var_node(&cfd->cpumask, GFP_KERNEL, | ||
| 56 | cpu_to_node(cpu))) | ||
| 57 | return NOTIFY_BAD; | ||
| 58 | break; | ||
| 59 | |||
| 60 | #ifdef CONFIG_CPU_HOTPLUG | ||
| 61 | case CPU_UP_CANCELED: | ||
| 62 | case CPU_UP_CANCELED_FROZEN: | ||
| 63 | |||
| 64 | case CPU_DEAD: | ||
| 65 | case CPU_DEAD_FROZEN: | ||
| 66 | free_cpumask_var(cfd->cpumask); | ||
| 67 | break; | ||
| 68 | #endif | ||
| 69 | }; | ||
| 70 | |||
| 71 | return NOTIFY_OK; | ||
| 72 | } | ||
| 73 | |||
| 74 | static struct notifier_block __cpuinitdata hotplug_cfd_notifier = { | ||
| 75 | .notifier_call = hotplug_cfd, | ||
| 35 | }; | 76 | }; |
| 36 | 77 | ||
| 37 | static int __cpuinit init_call_single_data(void) | 78 | static int __cpuinit init_call_single_data(void) |
| 38 | { | 79 | { |
| 80 | void *cpu = (void *)(long)smp_processor_id(); | ||
| 39 | int i; | 81 | int i; |
| 40 | 82 | ||
| 41 | for_each_possible_cpu(i) { | 83 | for_each_possible_cpu(i) { |
| @@ -44,29 +86,63 @@ static int __cpuinit init_call_single_data(void) | |||
| 44 | spin_lock_init(&q->lock); | 86 | spin_lock_init(&q->lock); |
| 45 | INIT_LIST_HEAD(&q->list); | 87 | INIT_LIST_HEAD(&q->list); |
| 46 | } | 88 | } |
| 89 | |||
| 90 | hotplug_cfd(&hotplug_cfd_notifier, CPU_UP_PREPARE, cpu); | ||
| 91 | register_cpu_notifier(&hotplug_cfd_notifier); | ||
| 92 | |||
| 47 | return 0; | 93 | return 0; |
| 48 | } | 94 | } |
| 49 | early_initcall(init_call_single_data); | 95 | early_initcall(init_call_single_data); |
| 50 | 96 | ||
| 51 | static void csd_flag_wait(struct call_single_data *data) | 97 | /* |
| 98 | * csd_lock/csd_unlock used to serialize access to per-cpu csd resources | ||
| 99 | * | ||
| 100 | * For non-synchronous ipi calls the csd can still be in use by the | ||
| 101 | * previous function call. For multi-cpu calls its even more interesting | ||
| 102 | * as we'll have to ensure no other cpu is observing our csd. | ||
| 103 | */ | ||
| 104 | static void csd_lock_wait(struct call_single_data *data) | ||
| 52 | { | 105 | { |
| 53 | /* Wait for response */ | 106 | while (data->flags & CSD_FLAG_LOCK) |
| 54 | do { | ||
| 55 | if (!(data->flags & CSD_FLAG_WAIT)) | ||
| 56 | break; | ||
| 57 | cpu_relax(); | 107 | cpu_relax(); |
| 58 | } while (1); | 108 | } |
| 109 | |||
| 110 | static void csd_lock(struct call_single_data *data) | ||
| 111 | { | ||
| 112 | csd_lock_wait(data); | ||
| 113 | data->flags = CSD_FLAG_LOCK; | ||
| 114 | |||
| 115 | /* | ||
| 116 | * prevent CPU from reordering the above assignment | ||
| 117 | * to ->flags with any subsequent assignments to other | ||
| 118 | * fields of the specified call_single_data structure: | ||
| 119 | */ | ||
| 120 | smp_mb(); | ||
| 121 | } | ||
| 122 | |||
| 123 | static void csd_unlock(struct call_single_data *data) | ||
| 124 | { | ||
| 125 | WARN_ON(!(data->flags & CSD_FLAG_LOCK)); | ||
| 126 | |||
| 127 | /* | ||
| 128 | * ensure we're all done before releasing data: | ||
| 129 | */ | ||
| 130 | smp_mb(); | ||
| 131 | |||
| 132 | data->flags &= ~CSD_FLAG_LOCK; | ||
| 59 | } | 133 | } |
| 60 | 134 | ||
| 61 | /* | 135 | /* |
| 62 | * Insert a previously allocated call_single_data element for execution | 136 | * Insert a previously allocated call_single_data element |
| 63 | * on the given CPU. data must already have ->func, ->info, and ->flags set. | 137 | * for execution on the given CPU. data must already have |
| 138 | * ->func, ->info, and ->flags set. | ||
| 64 | */ | 139 | */ |
| 65 | static void generic_exec_single(int cpu, struct call_single_data *data) | 140 | static |
| 141 | void generic_exec_single(int cpu, struct call_single_data *data, int wait) | ||
| 66 | { | 142 | { |
| 67 | struct call_single_queue *dst = &per_cpu(call_single_queue, cpu); | 143 | struct call_single_queue *dst = &per_cpu(call_single_queue, cpu); |
| 68 | int wait = data->flags & CSD_FLAG_WAIT, ipi; | ||
| 69 | unsigned long flags; | 144 | unsigned long flags; |
| 145 | int ipi; | ||
| 70 | 146 | ||
| 71 | spin_lock_irqsave(&dst->lock, flags); | 147 | spin_lock_irqsave(&dst->lock, flags); |
| 72 | ipi = list_empty(&dst->list); | 148 | ipi = list_empty(&dst->list); |
| @@ -74,24 +150,21 @@ static void generic_exec_single(int cpu, struct call_single_data *data) | |||
| 74 | spin_unlock_irqrestore(&dst->lock, flags); | 150 | spin_unlock_irqrestore(&dst->lock, flags); |
| 75 | 151 | ||
| 76 | /* | 152 | /* |
| 77 | * Make the list addition visible before sending the ipi. | 153 | * The list addition should be visible before sending the IPI |
| 154 | * handler locks the list to pull the entry off it because of | ||
| 155 | * normal cache coherency rules implied by spinlocks. | ||
| 156 | * | ||
| 157 | * If IPIs can go out of order to the cache coherency protocol | ||
| 158 | * in an architecture, sufficient synchronisation should be added | ||
| 159 | * to arch code to make it appear to obey cache coherency WRT | ||
| 160 | * locking and barrier primitives. Generic code isn't really | ||
| 161 | * equipped to do the right thing... | ||
| 78 | */ | 162 | */ |
| 79 | smp_mb(); | ||
| 80 | |||
| 81 | if (ipi) | 163 | if (ipi) |
| 82 | arch_send_call_function_single_ipi(cpu); | 164 | arch_send_call_function_single_ipi(cpu); |
| 83 | 165 | ||
| 84 | if (wait) | 166 | if (wait) |
| 85 | csd_flag_wait(data); | 167 | csd_lock_wait(data); |
| 86 | } | ||
| 87 | |||
| 88 | static void rcu_free_call_data(struct rcu_head *head) | ||
| 89 | { | ||
| 90 | struct call_function_data *data; | ||
| 91 | |||
| 92 | data = container_of(head, struct call_function_data, rcu_head); | ||
| 93 | |||
| 94 | kfree(data); | ||
| 95 | } | 168 | } |
| 96 | 169 | ||
| 97 | /* | ||
