cpu.c 43 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782
  1. /* CPU control.
  2. * (C) 2001, 2002, 2003, 2004 Rusty Russell
  3. *
  4. * This code is licenced under the GPL.
  5. */
  6. #include <linux/proc_fs.h>
  7. #include <linux/smp.h>
  8. #include <linux/init.h>
  9. #include <linux/notifier.h>
  10. #include <linux/sched.h>
  11. #include <linux/unistd.h>
  12. #include <linux/cpu.h>
  13. #include <linux/oom.h>
  14. #include <linux/rcupdate.h>
  15. #include <linux/export.h>
  16. #include <linux/bug.h>
  17. #include <linux/kthread.h>
  18. #include <linux/stop_machine.h>
  19. #include <linux/mutex.h>
  20. #include <linux/gfp.h>
  21. #include <linux/suspend.h>
  22. #include <linux/lockdep.h>
  23. #include <linux/tick.h>
  24. #include <linux/irq.h>
  25. #include <linux/smpboot.h>
  26. #include <trace/events/power.h>
  27. #define CREATE_TRACE_POINTS
  28. #include <trace/events/cpuhp.h>
  29. #include "smpboot.h"
  30. /**
  31. * cpuhp_cpu_state - Per cpu hotplug state storage
  32. * @state: The current cpu state
  33. * @target: The target state
  34. * @thread: Pointer to the hotplug thread
  35. * @should_run: Thread should execute
  36. * @rollback: Perform a rollback
  37. * @cb_stat: The state for a single callback (install/uninstall)
  38. * @cb: Single callback function (install/uninstall)
  39. * @result: Result of the operation
  40. * @done: Signal completion to the issuer of the task
  41. */
  42. struct cpuhp_cpu_state {
  43. enum cpuhp_state state;
  44. enum cpuhp_state target;
  45. #ifdef CONFIG_SMP
  46. struct task_struct *thread;
  47. bool should_run;
  48. bool rollback;
  49. enum cpuhp_state cb_state;
  50. int (*cb)(unsigned int cpu);
  51. int result;
  52. struct completion done;
  53. #endif
  54. };
  55. static DEFINE_PER_CPU(struct cpuhp_cpu_state, cpuhp_state);
  56. /**
  57. * cpuhp_step - Hotplug state machine step
  58. * @name: Name of the step
  59. * @startup: Startup function of the step
  60. * @teardown: Teardown function of the step
  61. * @skip_onerr: Do not invoke the functions on error rollback
  62. * Will go away once the notifiers are gone
  63. * @cant_stop: Bringup/teardown can't be stopped at this step
  64. */
  65. struct cpuhp_step {
  66. const char *name;
  67. int (*startup)(unsigned int cpu);
  68. int (*teardown)(unsigned int cpu);
  69. bool skip_onerr;
  70. bool cant_stop;
  71. };
  72. static DEFINE_MUTEX(cpuhp_state_mutex);
  73. static struct cpuhp_step cpuhp_bp_states[];
  74. static struct cpuhp_step cpuhp_ap_states[];
  75. /**
  76. * cpuhp_invoke_callback _ Invoke the callbacks for a given state
  77. * @cpu: The cpu for which the callback should be invoked
  78. * @step: The step in the state machine
  79. * @cb: The callback function to invoke
  80. *
  81. * Called from cpu hotplug and from the state register machinery
  82. */
  83. static int cpuhp_invoke_callback(unsigned int cpu, enum cpuhp_state step,
  84. int (*cb)(unsigned int))
  85. {
  86. struct cpuhp_cpu_state *st = per_cpu_ptr(&cpuhp_state, cpu);
  87. int ret = 0;
  88. if (cb) {
  89. trace_cpuhp_enter(cpu, st->target, step, cb);
  90. ret = cb(cpu);
  91. trace_cpuhp_exit(cpu, st->state, step, ret);
  92. }
  93. return ret;
  94. }
  95. #ifdef CONFIG_SMP
  96. /* Serializes the updates to cpu_online_mask, cpu_present_mask */
  97. static DEFINE_MUTEX(cpu_add_remove_lock);
  98. bool cpuhp_tasks_frozen;
  99. EXPORT_SYMBOL_GPL(cpuhp_tasks_frozen);
  100. /*
  101. * The following two APIs (cpu_maps_update_begin/done) must be used when
  102. * attempting to serialize the updates to cpu_online_mask & cpu_present_mask.
  103. * The APIs cpu_notifier_register_begin/done() must be used to protect CPU
  104. * hotplug callback (un)registration performed using __register_cpu_notifier()
  105. * or __unregister_cpu_notifier().
  106. */
  107. void cpu_maps_update_begin(void)
  108. {
  109. mutex_lock(&cpu_add_remove_lock);
  110. }
  111. EXPORT_SYMBOL(cpu_notifier_register_begin);
  112. void cpu_maps_update_done(void)
  113. {
  114. mutex_unlock(&cpu_add_remove_lock);
  115. }
  116. EXPORT_SYMBOL(cpu_notifier_register_done);
  117. static RAW_NOTIFIER_HEAD(cpu_chain);
  118. /* If set, cpu_up and cpu_down will return -EBUSY and do nothing.
  119. * Should always be manipulated under cpu_add_remove_lock
  120. */
  121. static int cpu_hotplug_disabled;
  122. #ifdef CONFIG_HOTPLUG_CPU
  123. static struct {
  124. struct task_struct *active_writer;
  125. /* wait queue to wake up the active_writer */
  126. wait_queue_head_t wq;
  127. /* verifies that no writer will get active while readers are active */
  128. struct mutex lock;
  129. /*
  130. * Also blocks the new readers during
  131. * an ongoing cpu hotplug operation.
  132. */
  133. atomic_t refcount;
  134. #ifdef CONFIG_DEBUG_LOCK_ALLOC
  135. struct lockdep_map dep_map;
  136. #endif
  137. } cpu_hotplug = {
  138. .active_writer = NULL,
  139. .wq = __WAIT_QUEUE_HEAD_INITIALIZER(cpu_hotplug.wq),
  140. .lock = __MUTEX_INITIALIZER(cpu_hotplug.lock),
  141. #ifdef CONFIG_DEBUG_LOCK_ALLOC
  142. .dep_map = {.name = "cpu_hotplug.lock" },
  143. #endif
  144. };
  145. /* Lockdep annotations for get/put_online_cpus() and cpu_hotplug_begin/end() */
  146. #define cpuhp_lock_acquire_read() lock_map_acquire_read(&cpu_hotplug.dep_map)
  147. #define cpuhp_lock_acquire_tryread() \
  148. lock_map_acquire_tryread(&cpu_hotplug.dep_map)
  149. #define cpuhp_lock_acquire() lock_map_acquire(&cpu_hotplug.dep_map)
  150. #define cpuhp_lock_release() lock_map_release(&cpu_hotplug.dep_map)
  151. void get_online_cpus(void)
  152. {
  153. might_sleep();
  154. if (cpu_hotplug.active_writer == current)
  155. return;
  156. cpuhp_lock_acquire_read();
  157. mutex_lock(&cpu_hotplug.lock);
  158. atomic_inc(&cpu_hotplug.refcount);
  159. mutex_unlock(&cpu_hotplug.lock);
  160. }
  161. EXPORT_SYMBOL_GPL(get_online_cpus);
  162. void put_online_cpus(void)
  163. {
  164. int refcount;
  165. if (cpu_hotplug.active_writer == current)
  166. return;
  167. refcount = atomic_dec_return(&cpu_hotplug.refcount);
  168. if (WARN_ON(refcount < 0)) /* try to fix things up */
  169. atomic_inc(&cpu_hotplug.refcount);
  170. if (refcount <= 0 && waitqueue_active(&cpu_hotplug.wq))
  171. wake_up(&cpu_hotplug.wq);
  172. cpuhp_lock_release();
  173. }
  174. EXPORT_SYMBOL_GPL(put_online_cpus);
  175. /*
  176. * This ensures that the hotplug operation can begin only when the
  177. * refcount goes to zero.
  178. *
  179. * Note that during a cpu-hotplug operation, the new readers, if any,
  180. * will be blocked by the cpu_hotplug.lock
  181. *
  182. * Since cpu_hotplug_begin() is always called after invoking
  183. * cpu_maps_update_begin(), we can be sure that only one writer is active.
  184. *
  185. * Note that theoretically, there is a possibility of a livelock:
  186. * - Refcount goes to zero, last reader wakes up the sleeping
  187. * writer.
  188. * - Last reader unlocks the cpu_hotplug.lock.
  189. * - A new reader arrives at this moment, bumps up the refcount.
  190. * - The writer acquires the cpu_hotplug.lock finds the refcount
  191. * non zero and goes to sleep again.
  192. *
  193. * However, this is very difficult to achieve in practice since
  194. * get_online_cpus() not an api which is called all that often.
  195. *
  196. */
  197. void cpu_hotplug_begin(void)
  198. {
  199. DEFINE_WAIT(wait);
  200. cpu_hotplug.active_writer = current;
  201. cpuhp_lock_acquire();
  202. for (;;) {
  203. mutex_lock(&cpu_hotplug.lock);
  204. prepare_to_wait(&cpu_hotplug.wq, &wait, TASK_UNINTERRUPTIBLE);
  205. if (likely(!atomic_read(&cpu_hotplug.refcount)))
  206. break;
  207. mutex_unlock(&cpu_hotplug.lock);
  208. schedule();
  209. }
  210. finish_wait(&cpu_hotplug.wq, &wait);
  211. }
  212. void cpu_hotplug_done(void)
  213. {
  214. cpu_hotplug.active_writer = NULL;
  215. mutex_unlock(&cpu_hotplug.lock);
  216. cpuhp_lock_release();
  217. }
  218. /*
  219. * Wait for currently running CPU hotplug operations to complete (if any) and
  220. * disable future CPU hotplug (from sysfs). The 'cpu_add_remove_lock' protects
  221. * the 'cpu_hotplug_disabled' flag. The same lock is also acquired by the
  222. * hotplug path before performing hotplug operations. So acquiring that lock
  223. * guarantees mutual exclusion from any currently running hotplug operations.
  224. */
  225. void cpu_hotplug_disable(void)
  226. {
  227. cpu_maps_update_begin();
  228. cpu_hotplug_disabled++;
  229. cpu_maps_update_done();
  230. }
  231. EXPORT_SYMBOL_GPL(cpu_hotplug_disable);
  232. void cpu_hotplug_enable(void)
  233. {
  234. cpu_maps_update_begin();
  235. WARN_ON(--cpu_hotplug_disabled < 0);
  236. cpu_maps_update_done();
  237. }
  238. EXPORT_SYMBOL_GPL(cpu_hotplug_enable);
  239. #endif /* CONFIG_HOTPLUG_CPU */
  240. /* Need to know about CPUs going up/down? */
  241. int register_cpu_notifier(struct notifier_block *nb)
  242. {
  243. int ret;
  244. cpu_maps_update_begin();
  245. ret = raw_notifier_chain_register(&cpu_chain, nb);
  246. cpu_maps_update_done();
  247. return ret;
  248. }
  249. int __register_cpu_notifier(struct notifier_block *nb)
  250. {
  251. return raw_notifier_chain_register(&cpu_chain, nb);
  252. }
  253. static int __cpu_notify(unsigned long val, unsigned int cpu, int nr_to_call,
  254. int *nr_calls)
  255. {
  256. unsigned long mod = cpuhp_tasks_frozen ? CPU_TASKS_FROZEN : 0;
  257. void *hcpu = (void *)(long)cpu;
  258. int ret;
  259. ret = __raw_notifier_call_chain(&cpu_chain, val | mod, hcpu, nr_to_call,
  260. nr_calls);
  261. return notifier_to_errno(ret);
  262. }
  263. static int cpu_notify(unsigned long val, unsigned int cpu)
  264. {
  265. return __cpu_notify(val, cpu, -1, NULL);
  266. }
  267. static void cpu_notify_nofail(unsigned long val, unsigned int cpu)
  268. {
  269. BUG_ON(cpu_notify(val, cpu));
  270. }
  271. /* Notifier wrappers for transitioning to state machine */
  272. static int notify_prepare(unsigned int cpu)
  273. {
  274. int nr_calls = 0;
  275. int ret;
  276. ret = __cpu_notify(CPU_UP_PREPARE, cpu, -1, &nr_calls);
  277. if (ret) {
  278. nr_calls--;
  279. printk(KERN_WARNING "%s: attempt to bring up CPU %u failed\n",
  280. __func__, cpu);
  281. __cpu_notify(CPU_UP_CANCELED, cpu, nr_calls, NULL);
  282. }
  283. return ret;
  284. }
  285. static int notify_online(unsigned int cpu)
  286. {
  287. cpu_notify(CPU_ONLINE, cpu);
  288. return 0;
  289. }
  290. static int notify_starting(unsigned int cpu)
  291. {
  292. cpu_notify(CPU_STARTING, cpu);
  293. return 0;
  294. }
  295. static int bringup_wait_for_ap(unsigned int cpu)
  296. {
  297. struct cpuhp_cpu_state *st = per_cpu_ptr(&cpuhp_state, cpu);
  298. wait_for_completion(&st->done);
  299. return st->result;
  300. }
  301. static int bringup_cpu(unsigned int cpu)
  302. {
  303. struct task_struct *idle = idle_thread_get(cpu);
  304. int ret;
  305. /* Arch-specific enabling code. */
  306. ret = __cpu_up(cpu, idle);
  307. if (ret) {
  308. cpu_notify(CPU_UP_CANCELED, cpu);
  309. return ret;
  310. }
  311. ret = bringup_wait_for_ap(cpu);
  312. BUG_ON(!cpu_online(cpu));
  313. return ret;
  314. }
  315. /*
  316. * Hotplug state machine related functions
  317. */
  318. static void undo_cpu_down(unsigned int cpu, struct cpuhp_cpu_state *st,
  319. struct cpuhp_step *steps)
  320. {
  321. for (st->state++; st->state < st->target; st->state++) {
  322. struct cpuhp_step *step = steps + st->state;
  323. if (!step->skip_onerr)
  324. cpuhp_invoke_callback(cpu, st->state, step->startup);
  325. }
  326. }
  327. static int cpuhp_down_callbacks(unsigned int cpu, struct cpuhp_cpu_state *st,
  328. struct cpuhp_step *steps, enum cpuhp_state target)
  329. {
  330. enum cpuhp_state prev_state = st->state;
  331. int ret = 0;
  332. for (; st->state > target; st->state--) {
  333. struct cpuhp_step *step = steps + st->state;
  334. ret = cpuhp_invoke_callback(cpu, st->state, step->teardown);
  335. if (ret) {
  336. st->target = prev_state;
  337. undo_cpu_down(cpu, st, steps);
  338. break;
  339. }
  340. }
  341. return ret;
  342. }
  343. static void undo_cpu_up(unsigned int cpu, struct cpuhp_cpu_state *st,
  344. struct cpuhp_step *steps)
  345. {
  346. for (st->state--; st->state > st->target; st->state--) {
  347. struct cpuhp_step *step = steps + st->state;
  348. if (!step->skip_onerr)
  349. cpuhp_invoke_callback(cpu, st->state, step->teardown);
  350. }
  351. }
  352. static int cpuhp_up_callbacks(unsigned int cpu, struct cpuhp_cpu_state *st,
  353. struct cpuhp_step *steps, enum cpuhp_state target)
  354. {
  355. enum cpuhp_state prev_state = st->state;
  356. int ret = 0;
  357. while (st->state < target) {
  358. struct cpuhp_step *step;
  359. st->state++;
  360. step = steps + st->state;
  361. ret = cpuhp_invoke_callback(cpu, st->state, step->startup);
  362. if (ret) {
  363. st->target = prev_state;
  364. undo_cpu_up(cpu, st, steps);
  365. break;
  366. }
  367. }
  368. return ret;
  369. }
  370. /*
  371. * The cpu hotplug threads manage the bringup and teardown of the cpus
  372. */
  373. static void cpuhp_create(unsigned int cpu)
  374. {
  375. struct cpuhp_cpu_state *st = per_cpu_ptr(&cpuhp_state, cpu);
  376. init_completion(&st->done);
  377. }
  378. static int cpuhp_should_run(unsigned int cpu)
  379. {
  380. struct cpuhp_cpu_state *st = this_cpu_ptr(&cpuhp_state);
  381. return st->should_run;
  382. }
  383. /* Execute the teardown callbacks. Used to be CPU_DOWN_PREPARE */
  384. static int cpuhp_ap_offline(unsigned int cpu, struct cpuhp_cpu_state *st)
  385. {
  386. enum cpuhp_state target = max((int)st->target, CPUHP_TEARDOWN_CPU);
  387. return cpuhp_down_callbacks(cpu, st, cpuhp_ap_states, target);
  388. }
  389. /* Execute the online startup callbacks. Used to be CPU_ONLINE */
  390. static int cpuhp_ap_online(unsigned int cpu, struct cpuhp_cpu_state *st)
  391. {
  392. return cpuhp_up_callbacks(cpu, st, cpuhp_ap_states, st->target);
  393. }
  394. /*
  395. * Execute teardown/startup callbacks on the plugged cpu. Also used to invoke
  396. * callbacks when a state gets [un]installed at runtime.
  397. */
  398. static void cpuhp_thread_fun(unsigned int cpu)
  399. {
  400. struct cpuhp_cpu_state *st = this_cpu_ptr(&cpuhp_state);
  401. int ret = 0;
  402. /*
  403. * Paired with the mb() in cpuhp_kick_ap_work and
  404. * cpuhp_invoke_ap_callback, so the work set is consistent visible.
  405. */
  406. smp_mb();
  407. if (!st->should_run)
  408. return;
  409. st->should_run = false;
  410. /* Single callback invocation for [un]install ? */
  411. if (st->cb) {
  412. if (st->cb_state < CPUHP_AP_ONLINE) {
  413. local_irq_disable();
  414. ret = cpuhp_invoke_callback(cpu, st->cb_state, st->cb);
  415. local_irq_enable();
  416. } else {
  417. ret = cpuhp_invoke_callback(cpu, st->cb_state, st->cb);
  418. }
  419. } else if (st->rollback) {
  420. BUG_ON(st->state < CPUHP_AP_ONLINE_IDLE);
  421. undo_cpu_down(cpu, st, cpuhp_ap_states);
  422. /*
  423. * This is a momentary workaround to keep the notifier users
  424. * happy. Will go away once we got rid of the notifiers.
  425. */
  426. cpu_notify_nofail(CPU_DOWN_FAILED, cpu);
  427. st->rollback = false;
  428. } else {
  429. /* Cannot happen .... */
  430. BUG_ON(st->state < CPUHP_AP_ONLINE_IDLE);
  431. /* Regular hotplug work */
  432. if (st->state < st->target)
  433. ret = cpuhp_ap_online(cpu, st);
  434. else if (st->state > st->target)
  435. ret = cpuhp_ap_offline(cpu, st);
  436. }
  437. st->result = ret;
  438. complete(&st->done);
  439. }
  440. /* Invoke a single callback on a remote cpu */
  441. static int cpuhp_invoke_ap_callback(int cpu, enum cpuhp_state state,
  442. int (*cb)(unsigned int))
  443. {
  444. struct cpuhp_cpu_state *st = per_cpu_ptr(&cpuhp_state, cpu);
  445. if (!cpu_online(cpu))
  446. return 0;
  447. /*
  448. * If we are up and running, use the hotplug thread. For early calls
  449. * we invoke the thread function directly.
  450. */
  451. if (!st->thread)
  452. return cpuhp_invoke_callback(cpu, state, cb);
  453. st->cb_state = state;
  454. st->cb = cb;
  455. /*
  456. * Make sure the above stores are visible before should_run becomes
  457. * true. Paired with the mb() above in cpuhp_thread_fun()
  458. */
  459. smp_mb();
  460. st->should_run = true;
  461. wake_up_process(st->thread);
  462. wait_for_completion(&st->done);
  463. return st->result;
  464. }
  465. /* Regular hotplug invocation of the AP hotplug thread */
  466. static void __cpuhp_kick_ap_work(struct cpuhp_cpu_state *st)
  467. {
  468. st->result = 0;
  469. st->cb = NULL;
  470. /*
  471. * Make sure the above stores are visible before should_run becomes
  472. * true. Paired with the mb() above in cpuhp_thread_fun()
  473. */
  474. smp_mb();
  475. st->should_run = true;
  476. wake_up_process(st->thread);
  477. }
  478. static int cpuhp_kick_ap_work(unsigned int cpu)
  479. {
  480. struct cpuhp_cpu_state *st = per_cpu_ptr(&cpuhp_state, cpu);
  481. enum cpuhp_state state = st->state;
  482. trace_cpuhp_enter(cpu, st->target, state, cpuhp_kick_ap_work);
  483. __cpuhp_kick_ap_work(st);
  484. wait_for_completion(&st->done);
  485. trace_cpuhp_exit(cpu, st->state, state, st->result);
  486. return st->result;
  487. }
  488. static struct smp_hotplug_thread cpuhp_threads = {
  489. .store = &cpuhp_state.thread,
  490. .create = &cpuhp_create,
  491. .thread_should_run = cpuhp_should_run,
  492. .thread_fn = cpuhp_thread_fun,
  493. .thread_comm = "cpuhp/%u",
  494. .selfparking = true,
  495. };
  496. void __init cpuhp_threads_init(void)
  497. {
  498. BUG_ON(smpboot_register_percpu_thread(&cpuhp_threads));
  499. kthread_unpark(this_cpu_read(cpuhp_state.thread));
  500. }
  501. #ifdef CONFIG_HOTPLUG_CPU
  502. EXPORT_SYMBOL(register_cpu_notifier);
  503. EXPORT_SYMBOL(__register_cpu_notifier);
  504. void unregister_cpu_notifier(struct notifier_block *nb)
  505. {
  506. cpu_maps_update_begin();
  507. raw_notifier_chain_unregister(&cpu_chain, nb);
  508. cpu_maps_update_done();
  509. }
  510. EXPORT_SYMBOL(unregister_cpu_notifier);
  511. void __unregister_cpu_notifier(struct notifier_block *nb)
  512. {
  513. raw_notifier_chain_unregister(&cpu_chain, nb);
  514. }
  515. EXPORT_SYMBOL(__unregister_cpu_notifier);
  516. /**
  517. * clear_tasks_mm_cpumask - Safely clear tasks' mm_cpumask for a CPU
  518. * @cpu: a CPU id
  519. *
  520. * This function walks all processes, finds a valid mm struct for each one and
  521. * then clears a corresponding bit in mm's cpumask. While this all sounds
  522. * trivial, there are various non-obvious corner cases, which this function
  523. * tries to solve in a safe manner.
  524. *
  525. * Also note that the function uses a somewhat relaxed locking scheme, so it may
  526. * be called only for an already offlined CPU.
  527. */
  528. void clear_tasks_mm_cpumask(int cpu)
  529. {
  530. struct task_struct *p;
  531. /*
  532. * This function is called after the cpu is taken down and marked
  533. * offline, so its not like new tasks will ever get this cpu set in
  534. * their mm mask. -- Peter Zijlstra
  535. * Thus, we may use rcu_read_lock() here, instead of grabbing
  536. * full-fledged tasklist_lock.
  537. */
  538. WARN_ON(cpu_online(cpu));
  539. rcu_read_lock();
  540. for_each_process(p) {
  541. struct task_struct *t;
  542. /*
  543. * Main thread might exit, but other threads may still have
  544. * a valid mm. Find one.
  545. */
  546. t = find_lock_task_mm(p);
  547. if (!t)
  548. continue;
  549. cpumask_clear_cpu(cpu, mm_cpumask(t->mm));
  550. task_unlock(t);
  551. }
  552. rcu_read_unlock();
  553. }
  554. static inline void check_for_tasks(int dead_cpu)
  555. {
  556. struct task_struct *g, *p;
  557. read_lock(&tasklist_lock);
  558. for_each_process_thread(g, p) {
  559. if (!p->on_rq)
  560. continue;
  561. /*
  562. * We do the check with unlocked task_rq(p)->lock.
  563. * Order the reading to do not warn about a task,
  564. * which was running on this cpu in the past, and
  565. * it's just been woken on another cpu.
  566. */
  567. rmb();
  568. if (task_cpu(p) != dead_cpu)
  569. continue;
  570. pr_warn("Task %s (pid=%d) is on cpu %d (state=%ld, flags=%x)\n",
  571. p->comm, task_pid_nr(p), dead_cpu, p->state, p->flags);
  572. }
  573. read_unlock(&tasklist_lock);
  574. }
  575. static int notify_down_prepare(unsigned int cpu)
  576. {
  577. int err, nr_calls = 0;
  578. err = __cpu_notify(CPU_DOWN_PREPARE, cpu, -1, &nr_calls);
  579. if (err) {
  580. nr_calls--;
  581. __cpu_notify(CPU_DOWN_FAILED, cpu, nr_calls, NULL);
  582. pr_warn("%s: attempt to take down CPU %u failed\n",
  583. __func__, cpu);
  584. }
  585. return err;
  586. }
  587. static int notify_dying(unsigned int cpu)
  588. {
  589. cpu_notify(CPU_DYING, cpu);
  590. return 0;
  591. }
  592. /* Take this CPU down. */
  593. static int take_cpu_down(void *_param)
  594. {
  595. struct cpuhp_cpu_state *st = this_cpu_ptr(&cpuhp_state);
  596. enum cpuhp_state target = max((int)st->target, CPUHP_AP_OFFLINE);
  597. int err, cpu = smp_processor_id();
  598. /* Ensure this CPU doesn't handle any more interrupts. */
  599. err = __cpu_disable();
  600. if (err < 0)
  601. return err;
  602. /* Invoke the former CPU_DYING callbacks */
  603. for (; st->state > target; st->state--) {
  604. struct cpuhp_step *step = cpuhp_ap_states + st->state;
  605. cpuhp_invoke_callback(cpu, st->state, step->teardown);
  606. }
  607. /* Give up timekeeping duties */
  608. tick_handover_do_timer();
  609. /* Park the stopper thread */
  610. stop_machine_park(cpu);
  611. return 0;
  612. }
  613. static int takedown_cpu(unsigned int cpu)
  614. {
  615. struct cpuhp_cpu_state *st = per_cpu_ptr(&cpuhp_state, cpu);
  616. int err;
  617. /* Park the smpboot threads */
  618. kthread_park(per_cpu_ptr(&cpuhp_state, cpu)->thread);
  619. smpboot_park_threads(cpu);
  620. /*
  621. * Prevent irq alloc/free while the dying cpu reorganizes the
  622. * interrupt affinities.
  623. */
  624. irq_lock_sparse();
  625. /*
  626. * So now all preempt/rcu users must observe !cpu_active().
  627. */
  628. err = stop_machine(take_cpu_down, NULL, cpumask_of(cpu));
  629. if (err) {
  630. /* CPU refused to die */
  631. irq_unlock_sparse();
  632. /* Unpark the hotplug thread so we can rollback there */
  633. kthread_unpark(per_cpu_ptr(&cpuhp_state, cpu)->thread);
  634. return err;
  635. }
  636. BUG_ON(cpu_online(cpu));
  637. /*
  638. * The migration_call() CPU_DYING callback will have removed all
  639. * runnable tasks from the cpu, there's only the idle task left now
  640. * that the migration thread is done doing the stop_machine thing.
  641. *
  642. * Wait for the stop thread to go away.
  643. */
  644. wait_for_completion(&st->done);
  645. BUG_ON(st->state != CPUHP_AP_IDLE_DEAD);
  646. /* Interrupts are moved away from the dying cpu, reenable alloc/free */
  647. irq_unlock_sparse();
  648. hotplug_cpu__broadcast_tick_pull(cpu);
  649. /* This actually kills the CPU. */
  650. __cpu_die(cpu);
  651. tick_cleanup_dead_cpu(cpu);
  652. return 0;
  653. }
  654. static int notify_dead(unsigned int cpu)
  655. {
  656. cpu_notify_nofail(CPU_DEAD, cpu);
  657. check_for_tasks(cpu);
  658. return 0;
  659. }
  660. static void cpuhp_complete_idle_dead(void *arg)
  661. {
  662. struct cpuhp_cpu_state *st = arg;
  663. complete(&st->done);
  664. }
  665. void cpuhp_report_idle_dead(void)
  666. {
  667. struct cpuhp_cpu_state *st = this_cpu_ptr(&cpuhp_state);
  668. BUG_ON(st->state != CPUHP_AP_OFFLINE);
  669. rcu_report_dead(smp_processor_id());
  670. st->state = CPUHP_AP_IDLE_DEAD;
  671. /*
  672. * We cannot call complete after rcu_report_dead() so we delegate it
  673. * to an online cpu.
  674. */
  675. smp_call_function_single(cpumask_first(cpu_online_mask),
  676. cpuhp_complete_idle_dead, st, 0);
  677. }
  678. #else
  679. #define notify_down_prepare NULL
  680. #define takedown_cpu NULL
  681. #define notify_dead NULL
  682. #define notify_dying NULL
  683. #endif
  684. #ifdef CONFIG_HOTPLUG_CPU
  685. /* Requires cpu_add_remove_lock to be held */
  686. static int __ref _cpu_down(unsigned int cpu, int tasks_frozen,
  687. enum cpuhp_state target)
  688. {
  689. struct cpuhp_cpu_state *st = per_cpu_ptr(&cpuhp_state, cpu);
  690. int prev_state, ret = 0;
  691. bool hasdied = false;
  692. if (num_online_cpus() == 1)
  693. return -EBUSY;
  694. if (!cpu_present(cpu))
  695. return -EINVAL;
  696. cpu_hotplug_begin();
  697. cpuhp_tasks_frozen = tasks_frozen;
  698. prev_state = st->state;
  699. st->target = target;
  700. /*
  701. * If the current CPU state is in the range of the AP hotplug thread,
  702. * then we need to kick the thread.
  703. */
  704. if (st->state > CPUHP_TEARDOWN_CPU) {
  705. ret = cpuhp_kick_ap_work(cpu);
  706. /*
  707. * The AP side has done the error rollback already. Just
  708. * return the error code..
  709. */
  710. if (ret)
  711. goto out;
  712. /*
  713. * We might have stopped still in the range of the AP hotplug
  714. * thread. Nothing to do anymore.
  715. */
  716. if (st->state > CPUHP_TEARDOWN_CPU)
  717. goto out;
  718. }
  719. /*
  720. * The AP brought itself down to CPUHP_TEARDOWN_CPU. So we need
  721. * to do the further cleanups.
  722. */
  723. ret = cpuhp_down_callbacks(cpu, st, cpuhp_bp_states, target);
  724. if (ret && st->state > CPUHP_TEARDOWN_CPU && st->state < prev_state) {
  725. st->target = prev_state;
  726. st->rollback = true;
  727. cpuhp_kick_ap_work(cpu);
  728. }
  729. hasdied = prev_state != st->state && st->state == CPUHP_OFFLINE;
  730. out:
  731. cpu_hotplug_done();
  732. /* This post dead nonsense must die */
  733. if (!ret && hasdied)
  734. cpu_notify_nofail(CPU_POST_DEAD, cpu);
  735. return ret;
  736. }
  737. static int do_cpu_down(unsigned int cpu, enum cpuhp_state target)
  738. {
  739. int err;
  740. cpu_maps_update_begin();
  741. if (cpu_hotplug_disabled) {
  742. err = -EBUSY;
  743. goto out;
  744. }
  745. err = _cpu_down(cpu, 0, target);
  746. out:
  747. cpu_maps_update_done();
  748. return err;
  749. }
  750. int cpu_down(unsigned int cpu)
  751. {
  752. return do_cpu_down(cpu, CPUHP_OFFLINE);
  753. }
  754. EXPORT_SYMBOL(cpu_down);
  755. #endif /*CONFIG_HOTPLUG_CPU*/
  756. /**
  757. * notify_cpu_starting(cpu) - call the CPU_STARTING notifiers
  758. * @cpu: cpu that just started
  759. *
  760. * This function calls the cpu_chain notifiers with CPU_STARTING.
  761. * It must be called by the arch code on the new cpu, before the new cpu
  762. * enables interrupts and before the "boot" cpu returns from __cpu_up().
  763. */
  764. void notify_cpu_starting(unsigned int cpu)
  765. {
  766. struct cpuhp_cpu_state *st = per_cpu_ptr(&cpuhp_state, cpu);
  767. enum cpuhp_state target = min((int)st->target, CPUHP_AP_ONLINE);
  768. while (st->state < target) {
  769. struct cpuhp_step *step;
  770. st->state++;
  771. step = cpuhp_ap_states + st->state;
  772. cpuhp_invoke_callback(cpu, st->state, step->startup);
  773. }
  774. }
  775. /*
  776. * Called from the idle task. We need to set active here, so we can kick off
  777. * the stopper thread and unpark the smpboot threads. If the target state is
  778. * beyond CPUHP_AP_ONLINE_IDLE we kick cpuhp thread and let it bring up the
  779. * cpu further.
  780. */
  781. void cpuhp_online_idle(enum cpuhp_state state)
  782. {
  783. struct cpuhp_cpu_state *st = this_cpu_ptr(&cpuhp_state);
  784. unsigned int cpu = smp_processor_id();
  785. /* Happens for the boot cpu */
  786. if (state != CPUHP_AP_ONLINE_IDLE)
  787. return;
  788. st->state = CPUHP_AP_ONLINE_IDLE;
  789. /* Unpark the stopper thread and the hotplug thread of this cpu */
  790. stop_machine_unpark(cpu);
  791. kthread_unpark(st->thread);
  792. /* Should we go further up ? */
  793. if (st->target > CPUHP_AP_ONLINE_IDLE)
  794. __cpuhp_kick_ap_work(st);
  795. else
  796. complete(&st->done);
  797. }
  798. /* Requires cpu_add_remove_lock to be held */
  799. static int _cpu_up(unsigned int cpu, int tasks_frozen, enum cpuhp_state target)
  800. {
  801. struct cpuhp_cpu_state *st = per_cpu_ptr(&cpuhp_state, cpu);
  802. struct task_struct *idle;
  803. int ret = 0;
  804. cpu_hotplug_begin();
  805. if (!cpu_present(cpu)) {
  806. ret = -EINVAL;
  807. goto out;
  808. }
  809. /*
  810. * The caller of do_cpu_up might have raced with another
  811. * caller. Ignore it for now.
  812. */
  813. if (st->state >= target)
  814. goto out;
  815. if (st->state == CPUHP_OFFLINE) {
  816. /* Let it fail before we try to bring the cpu up */
  817. idle = idle_thread_get(cpu);
  818. if (IS_ERR(idle)) {
  819. ret = PTR_ERR(idle);
  820. goto out;
  821. }
  822. }
  823. cpuhp_tasks_frozen = tasks_frozen;
  824. st->target = target;
  825. /*
  826. * If the current CPU state is in the range of the AP hotplug thread,
  827. * then we need to kick the thread once more.
  828. */
  829. if (st->state > CPUHP_BRINGUP_CPU) {
  830. ret = cpuhp_kick_ap_work(cpu);
  831. /*
  832. * The AP side has done the error rollback already. Just
  833. * return the error code..
  834. */
  835. if (ret)
  836. goto out;
  837. }
  838. /*
  839. * Try to reach the target state. We max out on the BP at
  840. * CPUHP_BRINGUP_CPU. After that the AP hotplug thread is
  841. * responsible for bringing it up to the target state.
  842. */
  843. target = min((int)target, CPUHP_BRINGUP_CPU);
  844. ret = cpuhp_up_callbacks(cpu, st, cpuhp_bp_states, target);
  845. out:
  846. cpu_hotplug_done();
  847. return ret;
  848. }
  849. static int do_cpu_up(unsigned int cpu, enum cpuhp_state target)
  850. {
  851. int err = 0;
  852. if (!cpu_possible(cpu)) {
  853. pr_err("can't online cpu %d because it is not configured as may-hotadd at boot time\n",
  854. cpu);
  855. #if defined(CONFIG_IA64)
  856. pr_err("please check additional_cpus= boot parameter\n");
  857. #endif
  858. return -EINVAL;
  859. }
  860. err = try_online_node(cpu_to_node(cpu));
  861. if (err)
  862. return err;
  863. cpu_maps_update_begin();
  864. if (cpu_hotplug_disabled) {
  865. err = -EBUSY;
  866. goto out;
  867. }
  868. err = _cpu_up(cpu, 0, target);
  869. out:
  870. cpu_maps_update_done();
  871. return err;
  872. }
  873. int cpu_up(unsigned int cpu)
  874. {
  875. return do_cpu_up(cpu, CPUHP_ONLINE);
  876. }
  877. EXPORT_SYMBOL_GPL(cpu_up);
  878. #ifdef CONFIG_PM_SLEEP_SMP
  879. static cpumask_var_t frozen_cpus;
  880. int disable_nonboot_cpus(void)
  881. {
  882. int cpu, first_cpu, error = 0;
  883. cpu_maps_update_begin();
  884. first_cpu = cpumask_first(cpu_online_mask);
  885. /*
  886. * We take down all of the non-boot CPUs in one shot to avoid races
  887. * with the userspace trying to use the CPU hotplug at the same time
  888. */
  889. cpumask_clear(frozen_cpus);
  890. pr_info("Disabling non-boot CPUs ...\n");
  891. for_each_online_cpu(cpu) {
  892. if (cpu == first_cpu)
  893. continue;
  894. trace_suspend_resume(TPS("CPU_OFF"), cpu, true);
  895. error = _cpu_down(cpu, 1, CPUHP_OFFLINE);
  896. trace_suspend_resume(TPS("CPU_OFF"), cpu, false);
  897. if (!error)
  898. cpumask_set_cpu(cpu, frozen_cpus);
  899. else {
  900. pr_err("Error taking CPU%d down: %d\n", cpu, error);
  901. break;
  902. }
  903. }
  904. if (!error)
  905. BUG_ON(num_online_cpus() > 1);
  906. else
  907. pr_err("Non-boot CPUs are not disabled\n");
  908. /*
  909. * Make sure the CPUs won't be enabled by someone else. We need to do
  910. * this even in case of failure as all disable_nonboot_cpus() users are
  911. * supposed to do enable_nonboot_cpus() on the failure path.
  912. */
  913. cpu_hotplug_disabled++;
  914. cpu_maps_update_done();
  915. return error;
  916. }
  917. void __weak arch_enable_nonboot_cpus_begin(void)
  918. {
  919. }
  920. void __weak arch_enable_nonboot_cpus_end(void)
  921. {
  922. }
  923. void enable_nonboot_cpus(void)
  924. {
  925. int cpu, error;
  926. /* Allow everyone to use the CPU hotplug again */
  927. cpu_maps_update_begin();
  928. WARN_ON(--cpu_hotplug_disabled < 0);
  929. if (cpumask_empty(frozen_cpus))
  930. goto out;
  931. pr_info("Enabling non-boot CPUs ...\n");
  932. arch_enable_nonboot_cpus_begin();
  933. for_each_cpu(cpu, frozen_cpus) {
  934. trace_suspend_resume(TPS("CPU_ON"), cpu, true);
  935. error = _cpu_up(cpu, 1, CPUHP_ONLINE);
  936. trace_suspend_resume(TPS("CPU_ON"), cpu, false);
  937. if (!error) {
  938. pr_info("CPU%d is up\n", cpu);
  939. continue;
  940. }
  941. pr_warn("Error taking CPU%d up: %d\n", cpu, error);
  942. }
  943. arch_enable_nonboot_cpus_end();
  944. cpumask_clear(frozen_cpus);
  945. out:
  946. cpu_maps_update_done();
  947. }
  948. static int __init alloc_frozen_cpus(void)
  949. {
  950. if (!alloc_cpumask_var(&frozen_cpus, GFP_KERNEL|__GFP_ZERO))
  951. return -ENOMEM;
  952. return 0;
  953. }
  954. core_initcall(alloc_frozen_cpus);
  955. /*
  956. * When callbacks for CPU hotplug notifications are being executed, we must
  957. * ensure that the state of the system with respect to the tasks being frozen
  958. * or not, as reported by the notification, remains unchanged *throughout the
  959. * duration* of the execution of the callbacks.
  960. * Hence we need to prevent the freezer from racing with regular CPU hotplug.
  961. *
  962. * This synchronization is implemented by mutually excluding regular CPU
  963. * hotplug and Suspend/Hibernate call paths by hooking onto the Suspend/
  964. * Hibernate notifications.
  965. */
  966. static int
  967. cpu_hotplug_pm_callback(struct notifier_block *nb,
  968. unsigned long action, void *ptr)
  969. {
  970. switch (action) {
  971. case PM_SUSPEND_PREPARE:
  972. case PM_HIBERNATION_PREPARE:
  973. cpu_hotplug_disable();
  974. break;
  975. case PM_POST_SUSPEND:
  976. case PM_POST_HIBERNATION:
  977. cpu_hotplug_enable();
  978. break;
  979. default:
  980. return NOTIFY_DONE;
  981. }
  982. return NOTIFY_OK;
  983. }
  984. static int __init cpu_hotplug_pm_sync_init(void)
  985. {
  986. /*
  987. * cpu_hotplug_pm_callback has higher priority than x86
  988. * bsp_pm_callback which depends on cpu_hotplug_pm_callback
  989. * to disable cpu hotplug to avoid cpu hotplug race.
  990. */
  991. pm_notifier(cpu_hotplug_pm_callback, 0);
  992. return 0;
  993. }
  994. core_initcall(cpu_hotplug_pm_sync_init);
  995. #endif /* CONFIG_PM_SLEEP_SMP */
  996. #endif /* CONFIG_SMP */
  997. /* Boot processor state steps */
  998. static struct cpuhp_step cpuhp_bp_states[] = {
  999. [CPUHP_OFFLINE] = {
  1000. .name = "offline",
  1001. .startup = NULL,
  1002. .teardown = NULL,
  1003. },
  1004. #ifdef CONFIG_SMP
  1005. [CPUHP_CREATE_THREADS]= {
  1006. .name = "threads:create",
  1007. .startup = smpboot_create_threads,
  1008. .teardown = NULL,
  1009. .cant_stop = true,
  1010. },
  1011. [CPUHP_PERF_PREPARE] = {
  1012. .name = "perf prepare",
  1013. .startup = perf_event_init_cpu,
  1014. .teardown = perf_event_exit_cpu,
  1015. },
  1016. [CPUHP_WORKQUEUE_PREP] = {
  1017. .name = "workqueue prepare",
  1018. .startup = workqueue_prepare_cpu,
  1019. .teardown = NULL,
  1020. },
  1021. [CPUHP_HRTIMERS_PREPARE] = {
  1022. .name = "hrtimers prepare",
  1023. .startup = hrtimers_prepare_cpu,
  1024. .teardown = hrtimers_dead_cpu,
  1025. },
  1026. [CPUHP_SMPCFD_PREPARE] = {
  1027. .name = "SMPCFD prepare",
  1028. .startup = smpcfd_prepare_cpu,
  1029. .teardown = smpcfd_dead_cpu,
  1030. },
  1031. [CPUHP_RCUTREE_PREP] = {
  1032. .name = "RCU-tree prepare",
  1033. .startup = rcutree_prepare_cpu,
  1034. .teardown = rcutree_dead_cpu,
  1035. },
  1036. /*
  1037. * Preparatory and dead notifiers. Will be replaced once the notifiers
  1038. * are converted to states.
  1039. */
  1040. [CPUHP_NOTIFY_PREPARE] = {
  1041. .name = "notify:prepare",
  1042. .startup = notify_prepare,
  1043. .teardown = notify_dead,
  1044. .skip_onerr = true,
  1045. .cant_stop = true,
  1046. },
  1047. /*
  1048. * On the tear-down path, timers_dead_cpu() must be invoked
  1049. * before blk_mq_queue_reinit_notify() from notify_dead(),
  1050. * otherwise a RCU stall occurs.
  1051. */
  1052. [CPUHP_TIMERS_DEAD] = {
  1053. .name = "timers dead",
  1054. .startup = NULL,
  1055. .teardown = timers_dead_cpu,
  1056. },
  1057. /* Kicks the plugged cpu into life */
  1058. [CPUHP_BRINGUP_CPU] = {
  1059. .name = "cpu:bringup",
  1060. .startup = bringup_cpu,
  1061. .teardown = NULL,
  1062. .cant_stop = true,
  1063. },
  1064. [CPUHP_AP_SMPCFD_DYING] = {
  1065. .startup = NULL,
  1066. .teardown = smpcfd_dying_cpu,
  1067. },
  1068. /*
  1069. * Handled on controll processor until the plugged processor manages
  1070. * this itself.
  1071. */
  1072. [CPUHP_TEARDOWN_CPU] = {
  1073. .name = "cpu:teardown",
  1074. .startup = NULL,
  1075. .teardown = takedown_cpu,
  1076. .cant_stop = true,
  1077. },
  1078. #else
  1079. [CPUHP_BRINGUP_CPU] = { },
  1080. #endif
  1081. };
  1082. /* Application processor state steps */
  1083. static struct cpuhp_step cpuhp_ap_states[] = {
  1084. #ifdef CONFIG_SMP
  1085. /* Final state before CPU kills itself */
  1086. [CPUHP_AP_IDLE_DEAD] = {
  1087. .name = "idle:dead",
  1088. },
  1089. /*
  1090. * Last state before CPU enters the idle loop to die. Transient state
  1091. * for synchronization.
  1092. */
  1093. [CPUHP_AP_OFFLINE] = {
  1094. .name = "ap:offline",
  1095. .cant_stop = true,
  1096. },
  1097. /* First state is scheduler control. Interrupts are disabled */
  1098. [CPUHP_AP_SCHED_STARTING] = {
  1099. .name = "sched:starting",
  1100. .startup = sched_cpu_starting,
  1101. .teardown = sched_cpu_dying,
  1102. },
  1103. [CPUHP_AP_RCUTREE_DYING] = {
  1104. .startup = NULL,
  1105. .teardown = rcutree_dying_cpu,
  1106. },
  1107. /*
  1108. * Low level startup/teardown notifiers. Run with interrupts
  1109. * disabled. Will be removed once the notifiers are converted to
  1110. * states.
  1111. */
  1112. [CPUHP_AP_NOTIFY_STARTING] = {
  1113. .name = "notify:starting",
  1114. .startup = notify_starting,
  1115. .teardown = notify_dying,
  1116. .skip_onerr = true,
  1117. .cant_stop = true,
  1118. },
  1119. /* Entry state on starting. Interrupts enabled from here on. Transient
  1120. * state for synchronsization */
  1121. [CPUHP_AP_ONLINE] = {
  1122. .name = "ap:online",
  1123. },
  1124. /* Handle smpboot threads park/unpark */
  1125. [CPUHP_AP_SMPBOOT_THREADS] = {
  1126. .name = "smpboot:threads",
  1127. .startup = smpboot_unpark_threads,
  1128. .teardown = NULL,
  1129. },
  1130. [CPUHP_AP_PERF_ONLINE] = {
  1131. .name = "perf online",
  1132. .startup = perf_event_init_cpu,
  1133. .teardown = perf_event_exit_cpu,
  1134. },
  1135. [CPUHP_AP_WORKQUEUE_ONLINE] = {
  1136. .name = "workqueue online",
  1137. .startup = workqueue_online_cpu,
  1138. .teardown = workqueue_offline_cpu,
  1139. },
  1140. [CPUHP_AP_RCUTREE_ONLINE] = {
  1141. .name = "RCU-tree online",
  1142. .startup = rcutree_online_cpu,
  1143. .teardown = rcutree_offline_cpu,
  1144. },
  1145. /*
  1146. * Online/down_prepare notifiers. Will be removed once the notifiers
  1147. * are converted to states.
  1148. */
  1149. [CPUHP_AP_NOTIFY_ONLINE] = {
  1150. .name = "notify:online",
  1151. .startup = notify_online,
  1152. .teardown = notify_down_prepare,
  1153. .skip_onerr = true,
  1154. },
  1155. #endif
  1156. /*
  1157. * The dynamically registered state space is here
  1158. */
  1159. #ifdef CONFIG_SMP
  1160. /* Last state is scheduler control setting the cpu active */
  1161. [CPUHP_AP_ACTIVE] = {
  1162. .name = "sched:active",
  1163. .startup = sched_cpu_activate,
  1164. .teardown = sched_cpu_deactivate,
  1165. },
  1166. #endif
  1167. /* CPU is fully up and running. */
  1168. [CPUHP_ONLINE] = {
  1169. .name = "online",
  1170. .startup = NULL,
  1171. .teardown = NULL,
  1172. },
  1173. };
  1174. /* Sanity check for callbacks */
  1175. static int cpuhp_cb_check(enum cpuhp_state state)
  1176. {
  1177. if (state <= CPUHP_OFFLINE || state >= CPUHP_ONLINE)
  1178. return -EINVAL;
  1179. return 0;
  1180. }
  1181. static bool cpuhp_is_ap_state(enum cpuhp_state state)
  1182. {
  1183. /*
  1184. * The extra check for CPUHP_TEARDOWN_CPU is only for documentation
  1185. * purposes as that state is handled explicitely in cpu_down.
  1186. */
  1187. return state > CPUHP_BRINGUP_CPU && state != CPUHP_TEARDOWN_CPU;
  1188. }
  1189. static struct cpuhp_step *cpuhp_get_step(enum cpuhp_state state)
  1190. {
  1191. struct cpuhp_step *sp;
  1192. sp = cpuhp_is_ap_state(state) ? cpuhp_ap_states : cpuhp_bp_states;
  1193. return sp + state;
  1194. }
  1195. static void cpuhp_store_callbacks(enum cpuhp_state state,
  1196. const char *name,
  1197. int (*startup)(unsigned int cpu),
  1198. int (*teardown)(unsigned int cpu))
  1199. {
  1200. /* (Un)Install the callbacks for further cpu hotplug operations */
  1201. struct cpuhp_step *sp;
  1202. mutex_lock(&cpuhp_state_mutex);
  1203. sp = cpuhp_get_step(state);
  1204. sp->startup = startup;
  1205. sp->teardown = teardown;
  1206. sp->name = name;
  1207. mutex_unlock(&cpuhp_state_mutex);
  1208. }
  1209. static void *cpuhp_get_teardown_cb(enum cpuhp_state state)
  1210. {
  1211. return cpuhp_get_step(state)->teardown;
  1212. }
  1213. /*
  1214. * Call the startup/teardown function for a step either on the AP or
  1215. * on the current CPU.
  1216. */
  1217. static int cpuhp_issue_call(int cpu, enum cpuhp_state state,
  1218. int (*cb)(unsigned int), bool bringup)
  1219. {
  1220. int ret;
  1221. if (!cb)
  1222. return 0;
  1223. /*
  1224. * The non AP bound callbacks can fail on bringup. On teardown
  1225. * e.g. module removal we crash for now.
  1226. */
  1227. #ifdef CONFIG_SMP
  1228. if (cpuhp_is_ap_state(state))
  1229. ret = cpuhp_invoke_ap_callback(cpu, state, cb);
  1230. else
  1231. ret = cpuhp_invoke_callback(cpu, state, cb);
  1232. #else
  1233. ret = cpuhp_invoke_callback(cpu, state, cb);
  1234. #endif
  1235. BUG_ON(ret && !bringup);
  1236. return ret;
  1237. }
  1238. /*
  1239. * Called from __cpuhp_setup_state on a recoverable failure.
  1240. *
  1241. * Note: The teardown callbacks for rollback are not allowed to fail!
  1242. */
  1243. static void cpuhp_rollback_install(int failedcpu, enum cpuhp_state state,
  1244. int (*teardown)(unsigned int cpu))
  1245. {
  1246. int cpu;
  1247. if (!teardown)
  1248. return;
  1249. /* Roll back the already executed steps on the other cpus */
  1250. for_each_present_cpu(cpu) {
  1251. struct cpuhp_cpu_state *st = per_cpu_ptr(&cpuhp_state, cpu);
  1252. int cpustate = st->state;
  1253. if (cpu >= failedcpu)
  1254. break;
  1255. /* Did we invoke the startup call on that cpu ? */
  1256. if (cpustate >= state)
  1257. cpuhp_issue_call(cpu, state, teardown, false);
  1258. }
  1259. }
  1260. /*
  1261. * Returns a free for dynamic slot assignment of the Online state. The states
  1262. * are protected by the cpuhp_slot_states mutex and an empty slot is identified
  1263. * by having no name assigned.
  1264. */
  1265. static int cpuhp_reserve_state(enum cpuhp_state state)
  1266. {
  1267. enum cpuhp_state i;
  1268. mutex_lock(&cpuhp_state_mutex);
  1269. for (i = CPUHP_AP_ONLINE_DYN; i <= CPUHP_AP_ONLINE_DYN_END; i++) {
  1270. if (cpuhp_ap_states[i].name)
  1271. continue;
  1272. cpuhp_ap_states[i].name = "Reserved";
  1273. mutex_unlock(&cpuhp_state_mutex);
  1274. return i;
  1275. }
  1276. mutex_unlock(&cpuhp_state_mutex);
  1277. WARN(1, "No more dynamic states available for CPU hotplug\n");
  1278. return -ENOSPC;
  1279. }
  1280. /**
  1281. * __cpuhp_setup_state - Setup the callbacks for an hotplug machine state
  1282. * @state: The state to setup
  1283. * @invoke: If true, the startup function is invoked for cpus where
  1284. * cpu state >= @state
  1285. * @startup: startup callback function
  1286. * @teardown: teardown callback function
  1287. *
  1288. * Returns 0 if successful, otherwise a proper error code
  1289. */
  1290. int __cpuhp_setup_state(enum cpuhp_state state,
  1291. const char *name, bool invoke,
  1292. int (*startup)(unsigned int cpu),
  1293. int (*teardown)(unsigned int cpu))
  1294. {
  1295. int cpu, ret = 0;
  1296. int dyn_state = 0;
  1297. if (cpuhp_cb_check(state) || !name)
  1298. return -EINVAL;
  1299. get_online_cpus();
  1300. /* currently assignments for the ONLINE state are possible */
  1301. if (state == CPUHP_AP_ONLINE_DYN) {
  1302. dyn_state = 1;
  1303. ret = cpuhp_reserve_state(state);
  1304. if (ret < 0)
  1305. goto out;
  1306. state = ret;
  1307. }
  1308. cpuhp_store_callbacks(state, name, startup, teardown);
  1309. if (!invoke || !startup)
  1310. goto out;
  1311. /*
  1312. * Try to call the startup callback for each present cpu
  1313. * depending on the hotplug state of the cpu.
  1314. */
  1315. for_each_present_cpu(cpu) {
  1316. struct cpuhp_cpu_state *st = per_cpu_ptr(&cpuhp_state, cpu);
  1317. int cpustate = st->state;
  1318. if (cpustate < state)
  1319. continue;
  1320. ret = cpuhp_issue_call(cpu, state, startup, true);
  1321. if (ret) {
  1322. cpuhp_rollback_install(cpu, state, teardown);
  1323. cpuhp_store_callbacks(state, NULL, NULL, NULL);
  1324. goto out;
  1325. }
  1326. }
  1327. out:
  1328. put_online_cpus();
  1329. if (!ret && dyn_state)
  1330. return state;
  1331. return ret;
  1332. }
  1333. EXPORT_SYMBOL(__cpuhp_setup_state);
  1334. /**
  1335. * __cpuhp_remove_state - Remove the callbacks for an hotplug machine state
  1336. * @state: The state to remove
  1337. * @invoke: If true, the teardown function is invoked for cpus where
  1338. * cpu state >= @state
  1339. *
  1340. * The teardown callback is currently not allowed to fail. Think
  1341. * about module removal!
  1342. */
  1343. void __cpuhp_remove_state(enum cpuhp_state state, bool invoke)
  1344. {
  1345. int (*teardown)(unsigned int cpu) = cpuhp_get_teardown_cb(state);
  1346. int cpu;
  1347. BUG_ON(cpuhp_cb_check(state));
  1348. get_online_cpus();
  1349. if (!invoke || !teardown)
  1350. goto remove;
  1351. /*
  1352. * Call the teardown callback for each present cpu depending
  1353. * on the hotplug state of the cpu. This function is not
  1354. * allowed to fail currently!
  1355. */
  1356. for_each_present_cpu(cpu) {
  1357. struct cpuhp_cpu_state *st = per_cpu_ptr(&cpuhp_state, cpu);
  1358. int cpustate = st->state;
  1359. if (cpustate >= state)
  1360. cpuhp_issue_call(cpu, state, teardown, false);
  1361. }
  1362. remove:
  1363. cpuhp_store_callbacks(state, NULL, NULL, NULL);
  1364. put_online_cpus();
  1365. }
  1366. EXPORT_SYMBOL(__cpuhp_remove_state);
  1367. #if defined(CONFIG_SYSFS) && defined(CONFIG_HOTPLUG_CPU)
  1368. static ssize_t show_cpuhp_state(struct device *dev,
  1369. struct device_attribute *attr, char *buf)
  1370. {
  1371. struct cpuhp_cpu_state *st = per_cpu_ptr(&cpuhp_state, dev->id);
  1372. return sprintf(buf, "%d\n", st->state);
  1373. }
  1374. static DEVICE_ATTR(state, 0444, show_cpuhp_state, NULL);
  1375. static ssize_t write_cpuhp_target(struct device *dev,
  1376. struct device_attribute *attr,
  1377. const char *buf, size_t count)
  1378. {
  1379. struct cpuhp_cpu_state *st = per_cpu_ptr(&cpuhp_state, dev->id);
  1380. struct cpuhp_step *sp;
  1381. int target, ret;
  1382. ret = kstrtoint(buf, 10, &target);
  1383. if (ret)
  1384. return ret;
  1385. #ifdef CONFIG_CPU_HOTPLUG_STATE_CONTROL
  1386. if (target < CPUHP_OFFLINE || target > CPUHP_ONLINE)
  1387. return -EINVAL;
  1388. #else
  1389. if (target != CPUHP_OFFLINE && target != CPUHP_ONLINE)
  1390. return -EINVAL;
  1391. #endif
  1392. ret = lock_device_hotplug_sysfs();
  1393. if (ret)
  1394. return ret;
  1395. mutex_lock(&cpuhp_state_mutex);
  1396. sp = cpuhp_get_step(target);
  1397. ret = !sp->name || sp->cant_stop ? -EINVAL : 0;
  1398. mutex_unlock(&cpuhp_state_mutex);
  1399. if (ret)
  1400. return ret;
  1401. if (st->state < target)
  1402. ret = do_cpu_up(dev->id, target);
  1403. else
  1404. ret = do_cpu_down(dev->id, target);
  1405. unlock_device_hotplug();
  1406. return ret ? ret : count;
  1407. }
  1408. static ssize_t show_cpuhp_target(struct device *dev,
  1409. struct device_attribute *attr, char *buf)
  1410. {
  1411. struct cpuhp_cpu_state *st = per_cpu_ptr(&cpuhp_state, dev->id);
  1412. return sprintf(buf, "%d\n", st->target);
  1413. }
  1414. static DEVICE_ATTR(target, 0644, show_cpuhp_target, write_cpuhp_target);
  1415. static struct attribute *cpuhp_cpu_attrs[] = {
  1416. &dev_attr_state.attr,
  1417. &dev_attr_target.attr,
  1418. NULL
  1419. };
  1420. static struct attribute_group cpuhp_cpu_attr_group = {
  1421. .attrs = cpuhp_cpu_attrs,
  1422. .name = "hotplug",
  1423. NULL
  1424. };
  1425. static ssize_t show_cpuhp_states(struct device *dev,
  1426. struct device_attribute *attr, char *buf)
  1427. {
  1428. ssize_t cur, res = 0;
  1429. int i;
  1430. mutex_lock(&cpuhp_state_mutex);
  1431. for (i = CPUHP_OFFLINE; i <= CPUHP_ONLINE; i++) {
  1432. struct cpuhp_step *sp = cpuhp_get_step(i);
  1433. if (sp->name) {
  1434. cur = sprintf(buf, "%3d: %s\n", i, sp->name);
  1435. buf += cur;
  1436. res += cur;
  1437. }
  1438. }
  1439. mutex_unlock(&cpuhp_state_mutex);
  1440. return res;
  1441. }
  1442. static DEVICE_ATTR(states, 0444, show_cpuhp_states, NULL);
  1443. static struct attribute *cpuhp_cpu_root_attrs[] = {
  1444. &dev_attr_states.attr,
  1445. NULL
  1446. };
  1447. static struct attribute_group cpuhp_cpu_root_attr_group = {
  1448. .attrs = cpuhp_cpu_root_attrs,
  1449. .name = "hotplug",
  1450. NULL
  1451. };
  1452. static int __init cpuhp_sysfs_init(void)
  1453. {
  1454. int cpu, ret;
  1455. ret = sysfs_create_group(&cpu_subsys.dev_root->kobj,
  1456. &cpuhp_cpu_root_attr_group);
  1457. if (ret)
  1458. return ret;
  1459. for_each_possible_cpu(cpu) {
  1460. struct device *dev = get_cpu_device(cpu);
  1461. if (!dev)
  1462. continue;
  1463. ret = sysfs_create_group(&dev->kobj, &cpuhp_cpu_attr_group);
  1464. if (ret)
  1465. return ret;
  1466. }
  1467. return 0;
  1468. }
  1469. device_initcall(cpuhp_sysfs_init);
  1470. #endif
  1471. /*
  1472. * cpu_bit_bitmap[] is a special, "compressed" data structure that
  1473. * represents all NR_CPUS bits binary values of 1<<nr.
  1474. *
  1475. * It is used by cpumask_of() to get a constant address to a CPU
  1476. * mask value that has a single bit set only.
  1477. */
  1478. /* cpu_bit_bitmap[0] is empty - so we can back into it */
  1479. #define MASK_DECLARE_1(x) [x+1][0] = (1UL << (x))
  1480. #define MASK_DECLARE_2(x) MASK_DECLARE_1(x), MASK_DECLARE_1(x+1)
  1481. #define MASK_DECLARE_4(x) MASK_DECLARE_2(x), MASK_DECLARE_2(x+2)
  1482. #define MASK_DECLARE_8(x) MASK_DECLARE_4(x), MASK_DECLARE_4(x+4)
  1483. const unsigned long cpu_bit_bitmap[BITS_PER_LONG+1][BITS_TO_LONGS(NR_CPUS)] = {
  1484. MASK_DECLARE_8(0), MASK_DECLARE_8(8),
  1485. MASK_DECLARE_8(16), MASK_DECLARE_8(24),
  1486. #if BITS_PER_LONG > 32
  1487. MASK_DECLARE_8(32), MASK_DECLARE_8(40),
  1488. MASK_DECLARE_8(48), MASK_DECLARE_8(56),
  1489. #endif
  1490. };
  1491. EXPORT_SYMBOL_GPL(cpu_bit_bitmap);
  1492. const DECLARE_BITMAP(cpu_all_bits, NR_CPUS) = CPU_BITS_ALL;
  1493. EXPORT_SYMBOL(cpu_all_bits);
  1494. #ifdef CONFIG_INIT_ALL_POSSIBLE
  1495. struct cpumask __cpu_possible_mask __read_mostly
  1496. = {CPU_BITS_ALL};
  1497. #else
  1498. struct cpumask __cpu_possible_mask __read_mostly;
  1499. #endif
  1500. EXPORT_SYMBOL(__cpu_possible_mask);
  1501. struct cpumask __cpu_online_mask __read_mostly;
  1502. EXPORT_SYMBOL(__cpu_online_mask);
  1503. struct cpumask __cpu_present_mask __read_mostly;
  1504. EXPORT_SYMBOL(__cpu_present_mask);
  1505. struct cpumask __cpu_active_mask __read_mostly;
  1506. EXPORT_SYMBOL(__cpu_active_mask);
  1507. void init_cpu_present(const struct cpumask *src)
  1508. {
  1509. cpumask_copy(&__cpu_present_mask, src);
  1510. }
  1511. void init_cpu_possible(const struct cpumask *src)
  1512. {
  1513. cpumask_copy(&__cpu_possible_mask, src);
  1514. }
  1515. void init_cpu_online(const struct cpumask *src)
  1516. {
  1517. cpumask_copy(&__cpu_online_mask, src);
  1518. }
  1519. /*
  1520. * Activate the first processor.
  1521. */
  1522. void __init boot_cpu_init(void)
  1523. {
  1524. int cpu = smp_processor_id();
  1525. /* Mark the boot cpu "present", "online" etc for SMP and UP case */
  1526. set_cpu_online(cpu, true);
  1527. set_cpu_active(cpu, true);
  1528. set_cpu_present(cpu, true);
  1529. set_cpu_possible(cpu, true);
  1530. }
  1531. /*
  1532. * Must be called _AFTER_ setting up the per_cpu areas
  1533. */
  1534. void __init boot_cpu_state_init(void)
  1535. {
  1536. per_cpu_ptr(&cpuhp_state, smp_processor_id())->state = CPUHP_ONLINE;
  1537. }