xref: /OK3568_Linux_fs/kernel/include/linux/stop_machine.h (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef _LINUX_STOP_MACHINE
3 #define _LINUX_STOP_MACHINE
4 
5 #include <linux/cpu.h>
6 #include <linux/cpumask.h>
7 #include <linux/smp.h>
8 #include <linux/list.h>
9 
10 /*
11  * stop_cpu[s]() is simplistic per-cpu maximum priority cpu
12  * monopolization mechanism.  The caller can specify a non-sleeping
13  * function to be executed on a single or multiple cpus preempting all
14  * other processes and monopolizing those cpus until it finishes.
15  *
16  * Resources for this mechanism are preallocated when a cpu is brought
17  * up and requests are guaranteed to be served as long as the target
18  * cpus are online.
19  */
20 typedef int (*cpu_stop_fn_t)(void *arg);
21 
22 #ifdef CONFIG_SMP
23 
24 struct cpu_stop_work {
25 	struct list_head	list;		/* cpu_stopper->works */
26 	cpu_stop_fn_t		fn;
27 	void			*arg;
28 	struct cpu_stop_done	*done;
29 };
30 
31 /*
32  * Structure to determine completion condition and record errors.  May
33  * be shared by works on different cpus.
34  */
35 struct cpu_stop_done {
36 	atomic_t		nr_todo;	/* nr left to execute */
37 	int			ret;		/* collected return value */
38 	struct completion	completion;	/* fired if nr_todo reaches 0 */
39 };
40 
41 int stop_one_cpu(unsigned int cpu, cpu_stop_fn_t fn, void *arg);
42 int stop_two_cpus(unsigned int cpu1, unsigned int cpu2, cpu_stop_fn_t fn, void *arg);
43 bool stop_one_cpu_nowait(unsigned int cpu, cpu_stop_fn_t fn, void *arg,
44 			 struct cpu_stop_work *work_buf);
45 void stop_machine_park(int cpu);
46 void stop_machine_unpark(int cpu);
47 void stop_machine_yield(const struct cpumask *cpumask);
48 int stop_one_cpu_async(unsigned int cpu, cpu_stop_fn_t fn, void *arg,
49 		       struct cpu_stop_work *work_buf,
50 		       struct cpu_stop_done *done);
51 void cpu_stop_work_wait(struct cpu_stop_work *work_buf);
52 
53 #else	/* CONFIG_SMP */
54 
55 #include <linux/workqueue.h>
56 
57 struct cpu_stop_work {
58 	struct work_struct	work;
59 	cpu_stop_fn_t		fn;
60 	void			*arg;
61 };
62 
stop_one_cpu(unsigned int cpu,cpu_stop_fn_t fn,void * arg)63 static inline int stop_one_cpu(unsigned int cpu, cpu_stop_fn_t fn, void *arg)
64 {
65 	int ret = -ENOENT;
66 	preempt_disable();
67 	if (cpu == smp_processor_id())
68 		ret = fn(arg);
69 	preempt_enable();
70 	return ret;
71 }
72 
stop_one_cpu_nowait_workfn(struct work_struct * work)73 static void stop_one_cpu_nowait_workfn(struct work_struct *work)
74 {
75 	struct cpu_stop_work *stwork =
76 		container_of(work, struct cpu_stop_work, work);
77 	preempt_disable();
78 	stwork->fn(stwork->arg);
79 	preempt_enable();
80 }
81 
stop_one_cpu_nowait(unsigned int cpu,cpu_stop_fn_t fn,void * arg,struct cpu_stop_work * work_buf)82 static inline bool stop_one_cpu_nowait(unsigned int cpu,
83 				       cpu_stop_fn_t fn, void *arg,
84 				       struct cpu_stop_work *work_buf)
85 {
86 	if (cpu == smp_processor_id()) {
87 		INIT_WORK(&work_buf->work, stop_one_cpu_nowait_workfn);
88 		work_buf->fn = fn;
89 		work_buf->arg = arg;
90 		schedule_work(&work_buf->work);
91 		return true;
92 	}
93 
94 	return false;
95 }
96 
97 #endif	/* CONFIG_SMP */
98 
99 /*
100  * stop_machine "Bogolock": stop the entire machine, disable
101  * interrupts.  This is a very heavy lock, which is equivalent to
102  * grabbing every spinlock (and more).  So the "read" side to such a
103  * lock is anything which disables preemption.
104  */
105 #if defined(CONFIG_SMP) || defined(CONFIG_HOTPLUG_CPU)
106 
107 /**
108  * stop_machine: freeze the machine on all CPUs and run this function
109  * @fn: the function to run
110  * @data: the data ptr for the @fn()
111  * @cpus: the cpus to run the @fn() on (NULL = any online cpu)
112  *
113  * Description: This causes a thread to be scheduled on every cpu,
114  * each of which disables interrupts.  The result is that no one is
115  * holding a spinlock or inside any other preempt-disabled region when
116  * @fn() runs.
117  *
118  * This can be thought of as a very heavy write lock, equivalent to
119  * grabbing every spinlock in the kernel.
120  *
121  * Protects against CPU hotplug.
122  */
123 int stop_machine(cpu_stop_fn_t fn, void *data, const struct cpumask *cpus);
124 
125 /**
126  * stop_machine_cpuslocked: freeze the machine on all CPUs and run this function
127  * @fn: the function to run
128  * @data: the data ptr for the @fn()
129  * @cpus: the cpus to run the @fn() on (NULL = any online cpu)
130  *
131  * Same as above. Must be called from with in a cpus_read_lock() protected
132  * region. Avoids nested calls to cpus_read_lock().
133  */
134 int stop_machine_cpuslocked(cpu_stop_fn_t fn, void *data, const struct cpumask *cpus);
135 
136 int stop_machine_from_inactive_cpu(cpu_stop_fn_t fn, void *data,
137 				   const struct cpumask *cpus);
138 #else	/* CONFIG_SMP || CONFIG_HOTPLUG_CPU */
139 
stop_machine_cpuslocked(cpu_stop_fn_t fn,void * data,const struct cpumask * cpus)140 static __always_inline int stop_machine_cpuslocked(cpu_stop_fn_t fn, void *data,
141 					  const struct cpumask *cpus)
142 {
143 	unsigned long flags;
144 	int ret;
145 	local_irq_save(flags);
146 	ret = fn(data);
147 	local_irq_restore(flags);
148 	return ret;
149 }
150 
151 static __always_inline int
stop_machine(cpu_stop_fn_t fn,void * data,const struct cpumask * cpus)152 stop_machine(cpu_stop_fn_t fn, void *data, const struct cpumask *cpus)
153 {
154 	return stop_machine_cpuslocked(fn, data, cpus);
155 }
156 
157 static __always_inline int
stop_machine_from_inactive_cpu(cpu_stop_fn_t fn,void * data,const struct cpumask * cpus)158 stop_machine_from_inactive_cpu(cpu_stop_fn_t fn, void *data,
159 			       const struct cpumask *cpus)
160 {
161 	return stop_machine(fn, data, cpus);
162 }
163 
164 #endif	/* CONFIG_SMP || CONFIG_HOTPLUG_CPU */
165 #endif	/* _LINUX_STOP_MACHINE */
166