| 1 |
|
| 2 | #ifndef _LINUX_STOP_MACHINE
|
| 3 | #define _LINUX_STOP_MACHINE
|
| 4 |
|
| 5 | #include <linux/bug.h>
|
| 6 | #include <linux/cpu.h>
|
| 7 | #include <linux/cpumask_types.h>
|
| 8 | #include <linux/smp.h>
|
| 9 | #include <linux/list.h>
|
| 10 |
|
| 11 | |
| 12 | * stop_cpu[s]() is simplistic per-cpu maximum priority cpu
|
| 13 | * monopolization mechanism. The caller can specify a non-sleeping
|
| 14 | * function to be executed on a single or multiple cpus preempting all
|
| 15 | * other processes and monopolizing those cpus until it finishes.
|
| 16 | *
|
| 17 | * Resources for this mechanism are preallocated when a cpu is brought
|
| 18 | * up and requests are guaranteed to be served as long as the target
|
| 19 | * cpus are online.
|
| 20 |
|
| 21 | typedef int (*cpu_stop_fn_t)(void *arg);
|
| 22 |
|
| 23 | #ifdef CONFIG_SMP
|
| 24 |
|
| 25 | struct cpu_stop_work {
|
| 26 | struct list_head list;
|
| 27 | cpu_stop_fn_t fn;
|
| 28 | unsigned long caller;
|
| 29 | void *arg;
|
| 30 | struct cpu_stop_done *done;
|
| 31 | };
|
| 32 |
|
| 33 | int stop_one_cpu(unsigned int cpu, cpu_stop_fn_t fn, void *arg);
|
| 34 | int stop_two_cpus(unsigned int cpu1, unsigned int cpu2, cpu_stop_fn_t fn, void *arg);
|
| 35 | void stop_one_cpu_nowait(unsigned int cpu, cpu_stop_fn_t fn, void *arg,
|
| 36 | struct cpu_stop_work *work_buf);
|
| 37 | void stop_machine_park(int cpu);
|
| 38 | void stop_machine_unpark(int cpu);
|
| 39 | void stop_machine_yield(const struct cpumask *cpumask);
|
| 40 |
|
| 41 | extern void print_stop_info(const char *log_lvl, struct task_struct *task);
|
| 42 |
|
| 43 | #else
|
| 44 |
|
| 45 | #include <linux/workqueue.h>
|
| 46 |
|
| 47 | struct cpu_stop_work {
|
| 48 | struct work_struct work;
|
| 49 | cpu_stop_fn_t fn;
|
| 50 | void *arg;
|
| 51 | };
|
| 52 |
|
| 53 | static inline int stop_one_cpu(unsigned int cpu, cpu_stop_fn_t fn, void *arg)
|
| 54 | {
|
| 55 | int ret = -ENOENT;
|
| 56 | preempt_disable();
|
| 57 | if (cpu == smp_processor_id())
|
| 58 | ret = fn(arg);
|
| 59 | preempt_enable();
|
| 60 | return ret;
|
| 61 | }
|
| 62 |
|
| 63 | static void stop_one_cpu_nowait_workfn(struct work_struct *work)
|
| 64 | {
|
| 65 | struct cpu_stop_work *stwork =
|
| 66 | container_of(work, struct cpu_stop_work, work);
|
| 67 | preempt_disable();
|
| 68 | stwork->fn(stwork->arg);
|
| 69 | preempt_enable();
|
| 70 | }
|
| 71 |
|
| 72 | static inline void stop_one_cpu_nowait(unsigned int cpu,
|
| 73 | cpu_stop_fn_t fn, void *arg,
|
| 74 | struct cpu_stop_work *work_buf)
|
| 75 | {
|
| 76 | if (WARN_ON_ONCE(cpu != smp_processor_id()))
|
| 77 | return;
|
| 78 |
|
| 79 | INIT_WORK(&work_buf->work, stop_one_cpu_nowait_workfn);
|
| 80 | work_buf->fn = fn;
|
| 81 | work_buf->arg = arg;
|
| 82 | schedule_work(&work_buf->work);
|
| 83 | }
|
| 84 |
|
| 85 | static inline void print_stop_info(const char *log_lvl, struct task_struct *task) { }
|
| 86 |
|
| 87 | #endif
|
| 88 |
|
| 89 | |
| 90 | * stop_machine "Bogolock": stop the entire machine, disable interrupts.
|
| 91 | * This is a very heavy lock, which is equivalent to grabbing every raw
|
| 92 | * spinlock (and more). So the "read" side to such a lock is anything
|
| 93 | * which disables preemption.
|
| 94 |
|
| 95 | #if defined(CONFIG_SMP) || defined(CONFIG_HOTPLUG_CPU)
|
| 96 |
|
| 97 | |
| 98 | * stop_machine: freeze the machine on all CPUs and run this function
|
| 99 | * @fn: the function to run
|
| 100 | * @data: the data ptr to pass to @fn()
|
| 101 | * @cpus: the cpus to run @fn() on (NULL = one unspecified online CPU)
|
| 102 | *
|
| 103 | * Description: This causes a thread to be scheduled on every CPU, which
|
| 104 | * will run with interrupts disabled. Each CPU specified by @cpus will
|
| 105 | * run @fn. While @fn is executing, there will no other CPUs holding
|
| 106 | * a raw spinlock or running within any other type of preempt-disabled
|
| 107 | * region of code.
|
| 108 | *
|
| 109 | * When @cpus specifies only a single CPU, this can be thought of as
|
| 110 | * a reader-writer lock where readers disable preemption (for example,
|
| 111 | * by holding a raw spinlock) and where the insanely heavy writers run
|
| 112 | * @fn while also preventing any other CPU from doing any useful work.
|
| 113 | * These writers can also be thought of as having implicitly grabbed every
|
| 114 | * raw spinlock in the kernel.
|
| 115 | *
|
| 116 | * When @fn is a no-op, this can be thought of as an RCU implementation
|
| 117 | * where readers again disable preemption and writers use stop_machine()
|
| 118 | * in place of synchronize_rcu(), albeit with orders of magnitude more
|
| 119 | * disruption than even that of synchronize_rcu_expedited().
|
| 120 | *
|
| 121 | * Although only one stop_machine() operation can proceed at a time,
|
| 122 | * the possibility of blocking in cpus_read_lock() means that the caller
|
| 123 | * cannot usefully rely on this serialization.
|
| 124 | *
|
| 125 | * Return: 0 if all invocations of @fn return zero. Otherwise, the
|
| 126 | * value returned by an arbitrarily chosen member of the set of calls to
|
| 127 | * @fn that returned non-zero.
|
| 128 |
|
| 129 | int stop_machine(cpu_stop_fn_t fn, void *data, const struct cpumask *cpus);
|
| 130 |
|
| 131 | |
| 132 | * stop_machine_cpuslocked: freeze the machine on all CPUs and run this function
|
| 133 | * @fn: the function to run
|
| 134 | * @data: the data ptr to pass to @fn()
|
| 135 | * @cpus: the cpus to run @fn() on (NULL = one unspecified online CPU)
|
| 136 | *
|
| 137 | * Same as above. Avoids nested calls to cpus_read_lock().
|
| 138 | *
|
| 139 | * Context: Must be called from within a cpus_read_lock() protected region.
|
| 140 |
|
| 141 | int stop_machine_cpuslocked(cpu_stop_fn_t fn, void *data, const struct cpumask *cpus);
|
| 142 |
|
| 143 | |
| 144 | * stop_core_cpuslocked: - stop all threads on just one core
|
| 145 | * @cpu: any cpu in the targeted core
|
| 146 | * @fn: the function to run on each CPU in the core containing @cpu
|
| 147 | * @data: the data ptr to pass to @fn()
|
| 148 | *
|
| 149 | * Same as above, but instead of every CPU, only the logical CPUs of the
|
| 150 | * single core containing @cpu are affected.
|
| 151 | *
|
| 152 | * Context: Must be called from within a cpus_read_lock() protected region.
|
| 153 | *
|
| 154 | * Return: 0 if all invocations of @fn return zero. Otherwise, the
|
| 155 | * value returned by an arbitrarily chosen member of the set of calls to
|
| 156 | * @fn that returned non-zero.
|
| 157 |
|
| 158 | int stop_core_cpuslocked(unsigned int cpu, cpu_stop_fn_t fn, void *data);
|
| 159 |
|
| 160 | int stop_machine_from_inactive_cpu(cpu_stop_fn_t fn, void *data,
|
| 161 | const struct cpumask *cpus);
|
| 162 | #else
|
| 163 |
|
| 164 | static __always_inline int stop_machine_cpuslocked(cpu_stop_fn_t fn, void *data,
|
| 165 | const struct cpumask *cpus)
|
| 166 | {
|
| 167 | unsigned long flags;
|
| 168 | int ret;
|
| 169 | local_irq_save(flags);
|
| 170 | ret = fn(data);
|
| 171 | local_irq_restore(flags);
|
| 172 | return ret;
|
| 173 | }
|
| 174 |
|
| 175 | static __always_inline int
|
| 176 | stop_machine(cpu_stop_fn_t fn, void *data, const struct cpumask *cpus)
|
| 177 | {
|
| 178 | return stop_machine_cpuslocked(fn, data, cpus);
|
| 179 | }
|
| 180 |
|
| 181 | static __always_inline int
|
| 182 | stop_machine_from_inactive_cpu(cpu_stop_fn_t fn, void *data,
|
| 183 | const struct cpumask *cpus)
|
| 184 | {
|
| 185 | return stop_machine(fn, data, cpus);
|
| 186 | }
|
| 187 |
|
| 188 | #endif
|
| 189 | #endif
|