.. | .. |
---|
| 1 | +// SPDX-License-Identifier: GPL-2.0-or-later |
---|
1 | 2 | /* |
---|
2 | 3 | * kernel/stop_machine.c |
---|
3 | 4 | * |
---|
.. | .. |
---|
5 | 6 | * Copyright (C) 2008, 2005 Rusty Russell rusty@rustcorp.com.au |
---|
6 | 7 | * Copyright (C) 2010 SUSE Linux Products GmbH |
---|
7 | 8 | * Copyright (C) 2010 Tejun Heo <tj@kernel.org> |
---|
8 | | - * |
---|
9 | | - * This file is released under the GPLv2 and any later version. |
---|
10 | 9 | */ |
---|
| 10 | +#include <linux/compiler.h> |
---|
11 | 11 | #include <linux/completion.h> |
---|
12 | 12 | #include <linux/cpu.h> |
---|
13 | 13 | #include <linux/init.h> |
---|
.. | .. |
---|
22 | 22 | #include <linux/atomic.h> |
---|
23 | 23 | #include <linux/nmi.h> |
---|
24 | 24 | #include <linux/sched/wake_q.h> |
---|
25 | | - |
---|
26 | | -/* |
---|
27 | | - * Structure to determine completion condition and record errors. May |
---|
28 | | - * be shared by works on different cpus. |
---|
29 | | - */ |
---|
30 | | -struct cpu_stop_done { |
---|
31 | | - atomic_t nr_todo; /* nr left to execute */ |
---|
32 | | - int ret; /* collected return value */ |
---|
33 | | - struct completion completion; /* fired if nr_todo reaches 0 */ |
---|
34 | | -}; |
---|
| 25 | +#include <linux/slab.h> |
---|
35 | 26 | |
---|
36 | 27 | /* the actual stopper, one per every possible cpu, enabled on online cpus */ |
---|
37 | 28 | struct cpu_stopper { |
---|
.. | .. |
---|
42 | 33 | struct list_head works; /* list of pending works */ |
---|
43 | 34 | |
---|
44 | 35 | struct cpu_stop_work stop_work; /* for stop_cpus */ |
---|
| 36 | + unsigned long caller; |
---|
| 37 | + cpu_stop_fn_t fn; |
---|
45 | 38 | }; |
---|
46 | 39 | |
---|
47 | 40 | static DEFINE_PER_CPU(struct cpu_stopper, cpu_stopper); |
---|
48 | 41 | static bool stop_machine_initialized = false; |
---|
| 42 | + |
---|
| 43 | +void print_stop_info(const char *log_lvl, struct task_struct *task) |
---|
| 44 | +{ |
---|
| 45 | + /* |
---|
| 46 | + * If @task is a stopper task, it cannot migrate and task_cpu() is |
---|
| 47 | + * stable. |
---|
| 48 | + */ |
---|
| 49 | + struct cpu_stopper *stopper = per_cpu_ptr(&cpu_stopper, task_cpu(task)); |
---|
| 50 | + |
---|
| 51 | + if (task != stopper->thread) |
---|
| 52 | + return; |
---|
| 53 | + |
---|
| 54 | + printk("%sStopper: %pS <- %pS\n", log_lvl, stopper->fn, (void *)stopper->caller); |
---|
| 55 | +} |
---|
49 | 56 | |
---|
50 | 57 | /* static data for stop_cpus */ |
---|
51 | 58 | static DEFINE_MUTEX(stop_cpus_mutex); |
---|
.. | .. |
---|
86 | 93 | enabled = stopper->enabled; |
---|
87 | 94 | if (enabled) |
---|
88 | 95 | __cpu_stop_queue_work(stopper, work, &wakeq); |
---|
89 | | - else { |
---|
90 | | - work->disabled = true; |
---|
91 | | - if (work->done) |
---|
92 | | - cpu_stop_signal_done(work->done); |
---|
93 | | - } |
---|
| 96 | + else if (work->done) |
---|
| 97 | + cpu_stop_signal_done(work->done); |
---|
94 | 98 | raw_spin_unlock_irqrestore(&stopper->lock, flags); |
---|
95 | 99 | |
---|
96 | 100 | wake_up_q(&wakeq); |
---|
.. | .. |
---|
126 | 130 | int stop_one_cpu(unsigned int cpu, cpu_stop_fn_t fn, void *arg) |
---|
127 | 131 | { |
---|
128 | 132 | struct cpu_stop_done done; |
---|
129 | | - struct cpu_stop_work work = { .fn = fn, .arg = arg, .done = &done }; |
---|
| 133 | + struct cpu_stop_work work = { .fn = fn, .arg = arg, .done = &done, .caller = _RET_IP_ }; |
---|
130 | 134 | |
---|
131 | 135 | cpu_stop_init_done(&done, 1); |
---|
132 | 136 | if (!cpu_stop_queue_work(cpu, &work)) |
---|
.. | .. |
---|
171 | 175 | /* Reset ack counter. */ |
---|
172 | 176 | atomic_set(&msdata->thread_ack, msdata->num_threads); |
---|
173 | 177 | smp_wmb(); |
---|
174 | | - msdata->state = newstate; |
---|
| 178 | + WRITE_ONCE(msdata->state, newstate); |
---|
175 | 179 | } |
---|
176 | 180 | |
---|
177 | 181 | /* Last one to ack a state moves to the next state. */ |
---|
.. | .. |
---|
181 | 185 | set_state(msdata, msdata->state + 1); |
---|
182 | 186 | } |
---|
183 | 187 | |
---|
| 188 | +notrace void __weak stop_machine_yield(const struct cpumask *cpumask) |
---|
| 189 | +{ |
---|
| 190 | + cpu_relax(); |
---|
| 191 | +} |
---|
| 192 | + |
---|
184 | 193 | /* This is the cpu_stop function which stops the CPU. */ |
---|
185 | 194 | static int multi_cpu_stop(void *data) |
---|
186 | 195 | { |
---|
187 | 196 | struct multi_stop_data *msdata = data; |
---|
188 | | - enum multi_stop_state curstate = MULTI_STOP_NONE; |
---|
| 197 | + enum multi_stop_state newstate, curstate = MULTI_STOP_NONE; |
---|
189 | 198 | int cpu = smp_processor_id(), err = 0; |
---|
| 199 | + const struct cpumask *cpumask; |
---|
190 | 200 | unsigned long flags; |
---|
191 | 201 | bool is_active; |
---|
192 | 202 | |
---|
.. | .. |
---|
196 | 206 | */ |
---|
197 | 207 | local_save_flags(flags); |
---|
198 | 208 | |
---|
199 | | - if (!msdata->active_cpus) |
---|
200 | | - is_active = cpu == cpumask_first(cpu_online_mask); |
---|
201 | | - else |
---|
202 | | - is_active = cpumask_test_cpu(cpu, msdata->active_cpus); |
---|
| 209 | + if (!msdata->active_cpus) { |
---|
| 210 | + cpumask = cpu_online_mask; |
---|
| 211 | + is_active = cpu == cpumask_first(cpumask); |
---|
| 212 | + } else { |
---|
| 213 | + cpumask = msdata->active_cpus; |
---|
| 214 | + is_active = cpumask_test_cpu(cpu, cpumask); |
---|
| 215 | + } |
---|
203 | 216 | |
---|
204 | 217 | /* Simple state machine */ |
---|
205 | 218 | do { |
---|
206 | 219 | /* Chill out and ensure we re-read multi_stop_state. */ |
---|
207 | | - cpu_relax_yield(); |
---|
208 | | - if (msdata->state != curstate) { |
---|
209 | | - curstate = msdata->state; |
---|
| 220 | + stop_machine_yield(cpumask); |
---|
| 221 | + newstate = READ_ONCE(msdata->state); |
---|
| 222 | + if (newstate != curstate) { |
---|
| 223 | + curstate = newstate; |
---|
210 | 224 | switch (curstate) { |
---|
211 | 225 | case MULTI_STOP_DISABLE_IRQ: |
---|
212 | 226 | local_irq_disable(); |
---|
.. | .. |
---|
228 | 242 | */ |
---|
229 | 243 | touch_nmi_watchdog(); |
---|
230 | 244 | } |
---|
| 245 | + rcu_momentary_dyntick_idle(); |
---|
231 | 246 | } while (curstate != MULTI_STOP_EXIT); |
---|
232 | 247 | |
---|
233 | 248 | local_irq_restore(flags); |
---|
.. | .. |
---|
323 | 338 | work1 = work2 = (struct cpu_stop_work){ |
---|
324 | 339 | .fn = multi_cpu_stop, |
---|
325 | 340 | .arg = &msdata, |
---|
326 | | - .done = &done |
---|
| 341 | + .done = &done, |
---|
| 342 | + .caller = _RET_IP_, |
---|
327 | 343 | }; |
---|
328 | 344 | |
---|
329 | 345 | cpu_stop_init_done(&done, 2); |
---|
.. | .. |
---|
359 | 375 | bool stop_one_cpu_nowait(unsigned int cpu, cpu_stop_fn_t fn, void *arg, |
---|
360 | 376 | struct cpu_stop_work *work_buf) |
---|
361 | 377 | { |
---|
362 | | - *work_buf = (struct cpu_stop_work){ .fn = fn, .arg = arg, }; |
---|
| 378 | + *work_buf = (struct cpu_stop_work){ .fn = fn, .arg = arg, .caller = _RET_IP_, }; |
---|
363 | 379 | return cpu_stop_queue_work(cpu, work_buf); |
---|
| 380 | +} |
---|
| 381 | +EXPORT_SYMBOL_GPL(stop_one_cpu_nowait); |
---|
| 382 | + |
---|
| 383 | +/** |
---|
| 384 | + * stop_one_cpu_async - stop a cpu and wait for completion in a separated |
---|
| 385 | + * function: stop_wait_work() |
---|
| 386 | + * @cpu: cpu to stop |
---|
| 387 | + * @fn: function to execute |
---|
| 388 | + * @arg: argument to @fn |
---|
| 389 | + * @work_buf: pointer to cpu_stop_work structure |
---|
| 390 | + * |
---|
| 391 | + * CONTEXT: |
---|
| 392 | + * Might sleep. |
---|
| 393 | + * |
---|
| 394 | + * RETURNS: |
---|
| 395 | + * 0 if cpu_stop_work was queued successfully and @fn will be called. |
---|
| 396 | + * ENOENT if @fn(@arg) was not executed because @cpu was offline. |
---|
| 397 | + */ |
---|
| 398 | +int stop_one_cpu_async(unsigned int cpu, cpu_stop_fn_t fn, void *arg, |
---|
| 399 | + struct cpu_stop_work *work_buf, |
---|
| 400 | + struct cpu_stop_done *done) |
---|
| 401 | +{ |
---|
| 402 | + cpu_stop_init_done(done, 1); |
---|
| 403 | + |
---|
| 404 | + work_buf->done = done; |
---|
| 405 | + work_buf->fn = fn; |
---|
| 406 | + work_buf->arg = arg; |
---|
| 407 | + |
---|
| 408 | + if (cpu_stop_queue_work(cpu, work_buf)) |
---|
| 409 | + return 0; |
---|
| 410 | + |
---|
| 411 | + work_buf->done = NULL; |
---|
| 412 | + |
---|
| 413 | + return -ENOENT; |
---|
| 414 | +} |
---|
| 415 | + |
---|
| 416 | +/** |
---|
| 417 | + * cpu_stop_work_wait - wait for a stop initiated by stop_one_cpu_async(). |
---|
| 418 | + * @work_buf: pointer to cpu_stop_work structure |
---|
| 419 | + * |
---|
| 420 | + * CONTEXT: |
---|
| 421 | + * Might sleep. |
---|
| 422 | + */ |
---|
| 423 | +void cpu_stop_work_wait(struct cpu_stop_work *work_buf) |
---|
| 424 | +{ |
---|
| 425 | + struct cpu_stop_done *done = work_buf->done; |
---|
| 426 | + |
---|
| 427 | + wait_for_completion(&done->completion); |
---|
| 428 | + work_buf->done = NULL; |
---|
364 | 429 | } |
---|
365 | 430 | |
---|
366 | 431 | static bool queue_stop_cpus_work(const struct cpumask *cpumask, |
---|
.. | .. |
---|
378 | 443 | */ |
---|
379 | 444 | preempt_disable(); |
---|
380 | 445 | stop_cpus_in_progress = true; |
---|
| 446 | + barrier(); |
---|
381 | 447 | for_each_cpu(cpu, cpumask) { |
---|
382 | 448 | work = &per_cpu(cpu_stopper.stop_work, cpu); |
---|
383 | 449 | work->fn = fn; |
---|
.. | .. |
---|
386 | 452 | if (cpu_stop_queue_work(cpu, work)) |
---|
387 | 453 | queued = true; |
---|
388 | 454 | } |
---|
| 455 | + barrier(); |
---|
389 | 456 | stop_cpus_in_progress = false; |
---|
390 | 457 | preempt_enable(); |
---|
391 | 458 | |
---|
.. | .. |
---|
432 | 499 | * @cpumask were offline; otherwise, 0 if all executions of @fn |
---|
433 | 500 | * returned 0, any non zero return value if any returned non zero. |
---|
434 | 501 | */ |
---|
435 | | -int stop_cpus(const struct cpumask *cpumask, cpu_stop_fn_t fn, void *arg) |
---|
| 502 | +static int stop_cpus(const struct cpumask *cpumask, cpu_stop_fn_t fn, void *arg) |
---|
436 | 503 | { |
---|
437 | 504 | int ret; |
---|
438 | 505 | |
---|
439 | 506 | /* static works are used, process one request at a time */ |
---|
440 | 507 | mutex_lock(&stop_cpus_mutex); |
---|
441 | | - ret = __stop_cpus(cpumask, fn, arg); |
---|
442 | | - mutex_unlock(&stop_cpus_mutex); |
---|
443 | | - return ret; |
---|
444 | | -} |
---|
445 | | - |
---|
446 | | -/** |
---|
447 | | - * try_stop_cpus - try to stop multiple cpus |
---|
448 | | - * @cpumask: cpus to stop |
---|
449 | | - * @fn: function to execute |
---|
450 | | - * @arg: argument to @fn |
---|
451 | | - * |
---|
452 | | - * Identical to stop_cpus() except that it fails with -EAGAIN if |
---|
453 | | - * someone else is already using the facility. |
---|
454 | | - * |
---|
455 | | - * CONTEXT: |
---|
456 | | - * Might sleep. |
---|
457 | | - * |
---|
458 | | - * RETURNS: |
---|
459 | | - * -EAGAIN if someone else is already stopping cpus, -ENOENT if |
---|
460 | | - * @fn(@arg) was not executed at all because all cpus in @cpumask were |
---|
461 | | - * offline; otherwise, 0 if all executions of @fn returned 0, any non |
---|
462 | | - * zero return value if any returned non zero. |
---|
463 | | - */ |
---|
464 | | -int try_stop_cpus(const struct cpumask *cpumask, cpu_stop_fn_t fn, void *arg) |
---|
465 | | -{ |
---|
466 | | - int ret; |
---|
467 | | - |
---|
468 | | - /* static works are used, process one request at a time */ |
---|
469 | | - if (!mutex_trylock(&stop_cpus_mutex)) |
---|
470 | | - return -EAGAIN; |
---|
471 | 508 | ret = __stop_cpus(cpumask, fn, arg); |
---|
472 | 509 | mutex_unlock(&stop_cpus_mutex); |
---|
473 | 510 | return ret; |
---|
.. | .. |
---|
507 | 544 | int ret; |
---|
508 | 545 | |
---|
509 | 546 | /* cpu stop callbacks must not sleep, make in_atomic() == T */ |
---|
| 547 | + stopper->caller = work->caller; |
---|
| 548 | + stopper->fn = fn; |
---|
510 | 549 | preempt_count_inc(); |
---|
511 | 550 | ret = fn(arg); |
---|
512 | 551 | if (done) { |
---|
.. | .. |
---|
515 | 554 | cpu_stop_signal_done(done); |
---|
516 | 555 | } |
---|
517 | 556 | preempt_count_dec(); |
---|
| 557 | + stopper->fn = NULL; |
---|
| 558 | + stopper->caller = 0; |
---|
518 | 559 | WARN_ONCE(preempt_count(), |
---|
519 | | - "cpu_stop: %pf(%p) leaked preempt count\n", fn, arg); |
---|
| 560 | + "cpu_stop: %ps(%p) leaked preempt count\n", fn, arg); |
---|
520 | 561 | goto repeat; |
---|
521 | 562 | } |
---|
522 | 563 | } |
---|