aboutsummaryrefslogtreecommitdiffstats
path: root/kernel/smp.c
diff options
context:
space:
mode:
Diffstat (limited to 'kernel/smp.c')
-rw-r--r--kernel/smp.c80
1 files changed, 49 insertions, 31 deletions
diff --git a/kernel/smp.c b/kernel/smp.c
index f38a1e692259..07854477c164 100644
--- a/kernel/smp.c
+++ b/kernel/smp.c
@@ -19,7 +19,7 @@
19 19
20enum { 20enum {
21 CSD_FLAG_LOCK = 0x01, 21 CSD_FLAG_LOCK = 0x01,
22 CSD_FLAG_WAIT = 0x02, 22 CSD_FLAG_SYNCHRONOUS = 0x02,
23}; 23};
24 24
25struct call_function_data { 25struct call_function_data {
@@ -107,7 +107,7 @@ void __init call_function_init(void)
107 */ 107 */
108static void csd_lock_wait(struct call_single_data *csd) 108static void csd_lock_wait(struct call_single_data *csd)
109{ 109{
110 while (csd->flags & CSD_FLAG_LOCK) 110 while (smp_load_acquire(&csd->flags) & CSD_FLAG_LOCK)
111 cpu_relax(); 111 cpu_relax();
112} 112}
113 113
@@ -121,19 +121,17 @@ static void csd_lock(struct call_single_data *csd)
121 * to ->flags with any subsequent assignments to other 121 * to ->flags with any subsequent assignments to other
122 * fields of the specified call_single_data structure: 122 * fields of the specified call_single_data structure:
123 */ 123 */
124 smp_mb(); 124 smp_wmb();
125} 125}
126 126
127static void csd_unlock(struct call_single_data *csd) 127static void csd_unlock(struct call_single_data *csd)
128{ 128{
129 WARN_ON((csd->flags & CSD_FLAG_WAIT) && !(csd->flags & CSD_FLAG_LOCK)); 129 WARN_ON(!(csd->flags & CSD_FLAG_LOCK));
130 130
131 /* 131 /*
132 * ensure we're all done before releasing data: 132 * ensure we're all done before releasing data:
133 */ 133 */
134 smp_mb(); 134 smp_store_release(&csd->flags, 0);
135
136 csd->flags &= ~CSD_FLAG_LOCK;
137} 135}
138 136
139static DEFINE_PER_CPU_SHARED_ALIGNED(struct call_single_data, csd_data); 137static DEFINE_PER_CPU_SHARED_ALIGNED(struct call_single_data, csd_data);
@@ -144,13 +142,16 @@ static DEFINE_PER_CPU_SHARED_ALIGNED(struct call_single_data, csd_data);
144 * ->func, ->info, and ->flags set. 142 * ->func, ->info, and ->flags set.
145 */ 143 */
146static int generic_exec_single(int cpu, struct call_single_data *csd, 144static int generic_exec_single(int cpu, struct call_single_data *csd,
147 smp_call_func_t func, void *info, int wait) 145 smp_call_func_t func, void *info)
148{ 146{
149 struct call_single_data csd_stack = { .flags = 0 };
150 unsigned long flags;
151
152
153 if (cpu == smp_processor_id()) { 147 if (cpu == smp_processor_id()) {
148 unsigned long flags;
149
150 /*
151 * We can unlock early even for the synchronous on-stack case,
152 * since we're doing this from the same CPU..
153 */
154 csd_unlock(csd);
154 local_irq_save(flags); 155 local_irq_save(flags);
155 func(info); 156 func(info);
156 local_irq_restore(flags); 157 local_irq_restore(flags);
@@ -158,24 +159,14 @@ static int generic_exec_single(int cpu, struct call_single_data *csd,
158 } 159 }
159 160
160 161
161 if ((unsigned)cpu >= nr_cpu_ids || !cpu_online(cpu)) 162 if ((unsigned)cpu >= nr_cpu_ids || !cpu_online(cpu)) {
163 csd_unlock(csd);
162 return -ENXIO; 164 return -ENXIO;
163
164
165 if (!csd) {
166 csd = &csd_stack;
167 if (!wait)
168 csd = this_cpu_ptr(&csd_data);
169 } 165 }
170 166
171 csd_lock(csd);
172
173 csd->func = func; 167 csd->func = func;
174 csd->info = info; 168 csd->info = info;
175 169
176 if (wait)
177 csd->flags |= CSD_FLAG_WAIT;
178
179 /* 170 /*
180 * The list addition should be visible before sending the IPI 171 * The list addition should be visible before sending the IPI
181 * handler locks the list to pull the entry off it because of 172 * handler locks the list to pull the entry off it because of
@@ -190,9 +181,6 @@ static int generic_exec_single(int cpu, struct call_single_data *csd,
190 if (llist_add(&csd->llist, &per_cpu(call_single_queue, cpu))) 181 if (llist_add(&csd->llist, &per_cpu(call_single_queue, cpu)))
191 arch_send_call_function_single_ipi(cpu); 182 arch_send_call_function_single_ipi(cpu);
192 183
193 if (wait)
194 csd_lock_wait(csd);
195
196 return 0; 184 return 0;
197} 185}
198 186
@@ -250,8 +238,17 @@ static void flush_smp_call_function_queue(bool warn_cpu_offline)
250 } 238 }
251 239
252 llist_for_each_entry_safe(csd, csd_next, entry, llist) { 240 llist_for_each_entry_safe(csd, csd_next, entry, llist) {
253 csd->func(csd->info); 241 smp_call_func_t func = csd->func;
254 csd_unlock(csd); 242 void *info = csd->info;
243
244 /* Do we wait until *after* callback? */
245 if (csd->flags & CSD_FLAG_SYNCHRONOUS) {
246 func(info);
247 csd_unlock(csd);
248 } else {
249 csd_unlock(csd);
250 func(info);
251 }
255 } 252 }
256 253
257 /* 254 /*
@@ -274,6 +271,8 @@ static void flush_smp_call_function_queue(bool warn_cpu_offline)
274int smp_call_function_single(int cpu, smp_call_func_t func, void *info, 271int smp_call_function_single(int cpu, smp_call_func_t func, void *info,
275 int wait) 272 int wait)
276{ 273{
274 struct call_single_data *csd;
275 struct call_single_data csd_stack = { .flags = CSD_FLAG_LOCK | CSD_FLAG_SYNCHRONOUS };
277 int this_cpu; 276 int this_cpu;
278 int err; 277 int err;
279 278
@@ -292,7 +291,16 @@ int smp_call_function_single(int cpu, smp_call_func_t func, void *info,
292 WARN_ON_ONCE(cpu_online(this_cpu) && irqs_disabled() 291 WARN_ON_ONCE(cpu_online(this_cpu) && irqs_disabled()
293 && !oops_in_progress); 292 && !oops_in_progress);
294 293
295 err = generic_exec_single(cpu, NULL, func, info, wait); 294 csd = &csd_stack;
295 if (!wait) {
296 csd = this_cpu_ptr(&csd_data);
297 csd_lock(csd);
298 }
299
300 err = generic_exec_single(cpu, csd, func, info);
301
302 if (wait)
303 csd_lock_wait(csd);
296 304
297 put_cpu(); 305 put_cpu();
298 306
@@ -321,7 +329,15 @@ int smp_call_function_single_async(int cpu, struct call_single_data *csd)
321 int err = 0; 329 int err = 0;
322 330
323 preempt_disable(); 331 preempt_disable();
324 err = generic_exec_single(cpu, csd, csd->func, csd->info, 0); 332
333 /* We could deadlock if we have to wait here with interrupts disabled! */
334 if (WARN_ON_ONCE(csd->flags & CSD_FLAG_LOCK))
335 csd_lock_wait(csd);
336
337 csd->flags = CSD_FLAG_LOCK;
338 smp_wmb();
339
340 err = generic_exec_single(cpu, csd, csd->func, csd->info);
325 preempt_enable(); 341 preempt_enable();
326 342
327 return err; 343 return err;
@@ -433,6 +449,8 @@ void smp_call_function_many(const struct cpumask *mask,
433 struct call_single_data *csd = per_cpu_ptr(cfd->csd, cpu); 449 struct call_single_data *csd = per_cpu_ptr(cfd->csd, cpu);
434 450
435 csd_lock(csd); 451 csd_lock(csd);
452 if (wait)
453 csd->flags |= CSD_FLAG_SYNCHRONOUS;
436 csd->func = func; 454 csd->func = func;
437 csd->info = info; 455 csd->info = info;
438 llist_add(&csd->llist, &per_cpu(call_single_queue, cpu)); 456 llist_add(&csd->llist, &per_cpu(call_single_queue, cpu));