cpu.c 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716
  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. st->cb_state = state;
  448. st->cb = cb;
  449. /*
  450. * Make sure the above stores are visible before should_run becomes
  451. * true. Paired with the mb() above in cpuhp_thread_fun()
  452. */
  453. smp_mb();
  454. st->should_run = true;
  455. wake_up_process(st->thread);
  456. wait_for_completion(&st->done);
  457. return st->result;
  458. }
  459. /* Regular hotplug invocation of the AP hotplug thread */
  460. static void __cpuhp_kick_ap_work(struct cpuhp_cpu_state *st)
  461. {
  462. st->result = 0;
  463. st->cb = NULL;
  464. /*
  465. * Make sure the above stores are visible before should_run becomes
  466. * true. Paired with the mb() above in cpuhp_thread_fun()
  467. */
  468. smp_mb();
  469. st->should_run = true;
  470. wake_up_process(st->thread);
  471. }
  472. static int cpuhp_kick_ap_work(unsigned int cpu)
  473. {
  474. struct cpuhp_cpu_state *st = per_cpu_ptr(&cpuhp_state, cpu);
  475. enum cpuhp_state state = st->state;
  476. trace_cpuhp_enter(cpu, st->target, state, cpuhp_kick_ap_work);
  477. __cpuhp_kick_ap_work(st);
  478. wait_for_completion(&st->done);
  479. trace_cpuhp_exit(cpu, st->state, state, st->result);
  480. return st->result;
  481. }
  482. static struct smp_hotplug_thread cpuhp_threads = {
  483. .store = &cpuhp_state.thread,
  484. .create = &cpuhp_create,
  485. .thread_should_run = cpuhp_should_run,
  486. .thread_fn = cpuhp_thread_fun,
  487. .thread_comm = "cpuhp/%u",
  488. .selfparking = true,
  489. };
  490. void __init cpuhp_threads_init(void)
  491. {
  492. BUG_ON(smpboot_register_percpu_thread(&cpuhp_threads));
  493. kthread_unpark(this_cpu_read(cpuhp_state.thread));
  494. }
  495. #ifdef CONFIG_HOTPLUG_CPU
  496. EXPORT_SYMBOL(register_cpu_notifier);
  497. EXPORT_SYMBOL(__register_cpu_notifier);
  498. void unregister_cpu_notifier(struct notifier_block *nb)
  499. {
  500. cpu_maps_update_begin();
  501. raw_notifier_chain_unregister(&cpu_chain, nb);
  502. cpu_maps_update_done();
  503. }
  504. EXPORT_SYMBOL(unregister_cpu_notifier);
  505. void __unregister_cpu_notifier(struct notifier_block *nb)
  506. {
  507. raw_notifier_chain_unregister(&cpu_chain, nb);
  508. }
  509. EXPORT_SYMBOL(__unregister_cpu_notifier);
  510. /**
  511. * clear_tasks_mm_cpumask - Safely clear tasks' mm_cpumask for a CPU
  512. * @cpu: a CPU id
  513. *
  514. * This function walks all processes, finds a valid mm struct for each one and
  515. * then clears a corresponding bit in mm's cpumask. While this all sounds
  516. * trivial, there are various non-obvious corner cases, which this function
  517. * tries to solve in a safe manner.
  518. *
  519. * Also note that the function uses a somewhat relaxed locking scheme, so it may
  520. * be called only for an already offlined CPU.
  521. */
  522. void clear_tasks_mm_cpumask(int cpu)
  523. {
  524. struct task_struct *p;
  525. /*
  526. * This function is called after the cpu is taken down and marked
  527. * offline, so its not like new tasks will ever get this cpu set in
  528. * their mm mask. -- Peter Zijlstra
  529. * Thus, we may use rcu_read_lock() here, instead of grabbing
  530. * full-fledged tasklist_lock.
  531. */
  532. WARN_ON(cpu_online(cpu));
  533. rcu_read_lock();
  534. for_each_process(p) {
  535. struct task_struct *t;
  536. /*
  537. * Main thread might exit, but other threads may still have
  538. * a valid mm. Find one.
  539. */
  540. t = find_lock_task_mm(p);
  541. if (!t)
  542. continue;
  543. cpumask_clear_cpu(cpu, mm_cpumask(t->mm));
  544. task_unlock(t);
  545. }
  546. rcu_read_unlock();
  547. }
  548. static inline void check_for_tasks(int dead_cpu)
  549. {
  550. struct task_struct *g, *p;
  551. read_lock(&tasklist_lock);
  552. for_each_process_thread(g, p) {
  553. if (!p->on_rq)
  554. continue;
  555. /*
  556. * We do the check with unlocked task_rq(p)->lock.
  557. * Order the reading to do not warn about a task,
  558. * which was running on this cpu in the past, and
  559. * it's just been woken on another cpu.
  560. */
  561. rmb();
  562. if (task_cpu(p) != dead_cpu)
  563. continue;
  564. pr_warn("Task %s (pid=%d) is on cpu %d (state=%ld, flags=%x)\n",
  565. p->comm, task_pid_nr(p), dead_cpu, p->state, p->flags);
  566. }
  567. read_unlock(&tasklist_lock);
  568. }
  569. static int notify_down_prepare(unsigned int cpu)
  570. {
  571. int err, nr_calls = 0;
  572. err = __cpu_notify(CPU_DOWN_PREPARE, cpu, -1, &nr_calls);
  573. if (err) {
  574. nr_calls--;
  575. __cpu_notify(CPU_DOWN_FAILED, cpu, nr_calls, NULL);
  576. pr_warn("%s: attempt to take down CPU %u failed\n",
  577. __func__, cpu);
  578. }
  579. return err;
  580. }
  581. static int notify_dying(unsigned int cpu)
  582. {
  583. cpu_notify(CPU_DYING, cpu);
  584. return 0;
  585. }
  586. /* Take this CPU down. */
  587. static int take_cpu_down(void *_param)
  588. {
  589. struct cpuhp_cpu_state *st = this_cpu_ptr(&cpuhp_state);
  590. enum cpuhp_state target = max((int)st->target, CPUHP_AP_OFFLINE);
  591. int err, cpu = smp_processor_id();
  592. /* Ensure this CPU doesn't handle any more interrupts. */
  593. err = __cpu_disable();
  594. if (err < 0)
  595. return err;
  596. /* Invoke the former CPU_DYING callbacks */
  597. for (; st->state > target; st->state--) {
  598. struct cpuhp_step *step = cpuhp_ap_states + st->state;
  599. cpuhp_invoke_callback(cpu, st->state, step->teardown);
  600. }
  601. /* Give up timekeeping duties */
  602. tick_handover_do_timer();
  603. /* Park the stopper thread */
  604. stop_machine_park(cpu);
  605. return 0;
  606. }
  607. static int takedown_cpu(unsigned int cpu)
  608. {
  609. struct cpuhp_cpu_state *st = per_cpu_ptr(&cpuhp_state, cpu);
  610. int err;
  611. /* Park the smpboot threads */
  612. kthread_park(per_cpu_ptr(&cpuhp_state, cpu)->thread);
  613. smpboot_park_threads(cpu);
  614. /*
  615. * Prevent irq alloc/free while the dying cpu reorganizes the
  616. * interrupt affinities.
  617. */
  618. irq_lock_sparse();
  619. /*
  620. * So now all preempt/rcu users must observe !cpu_active().
  621. */
  622. err = stop_machine(take_cpu_down, NULL, cpumask_of(cpu));
  623. if (err) {
  624. /* CPU refused to die */
  625. irq_unlock_sparse();
  626. /* Unpark the hotplug thread so we can rollback there */
  627. kthread_unpark(per_cpu_ptr(&cpuhp_state, cpu)->thread);
  628. return err;
  629. }
  630. BUG_ON(cpu_online(cpu));
  631. /*
  632. * The migration_call() CPU_DYING callback will have removed all
  633. * runnable tasks from the cpu, there's only the idle task left now
  634. * that the migration thread is done doing the stop_machine thing.
  635. *
  636. * Wait for the stop thread to go away.
  637. */
  638. wait_for_completion(&st->done);
  639. BUG_ON(st->state != CPUHP_AP_IDLE_DEAD);
  640. /* Interrupts are moved away from the dying cpu, reenable alloc/free */
  641. irq_unlock_sparse();
  642. hotplug_cpu__broadcast_tick_pull(cpu);
  643. /* This actually kills the CPU. */
  644. __cpu_die(cpu);
  645. tick_cleanup_dead_cpu(cpu);
  646. return 0;
  647. }
  648. static int notify_dead(unsigned int cpu)
  649. {
  650. cpu_notify_nofail(CPU_DEAD, cpu);
  651. check_for_tasks(cpu);
  652. return 0;
  653. }
  654. static void cpuhp_complete_idle_dead(void *arg)
  655. {
  656. struct cpuhp_cpu_state *st = arg;
  657. complete(&st->done);
  658. }
  659. void cpuhp_report_idle_dead(void)
  660. {
  661. struct cpuhp_cpu_state *st = this_cpu_ptr(&cpuhp_state);
  662. BUG_ON(st->state != CPUHP_AP_OFFLINE);
  663. rcu_report_dead(smp_processor_id());
  664. st->state = CPUHP_AP_IDLE_DEAD;
  665. /*
  666. * We cannot call complete after rcu_report_dead() so we delegate it
  667. * to an online cpu.
  668. */
  669. smp_call_function_single(cpumask_first(cpu_online_mask),
  670. cpuhp_complete_idle_dead, st, 0);
  671. }
  672. #else
  673. #define notify_down_prepare NULL
  674. #define takedown_cpu NULL
  675. #define notify_dead NULL
  676. #define notify_dying NULL
  677. #endif
  678. #ifdef CONFIG_HOTPLUG_CPU
  679. /* Requires cpu_add_remove_lock to be held */
  680. static int __ref _cpu_down(unsigned int cpu, int tasks_frozen,
  681. enum cpuhp_state target)
  682. {
  683. struct cpuhp_cpu_state *st = per_cpu_ptr(&cpuhp_state, cpu);
  684. int prev_state, ret = 0;
  685. bool hasdied = false;
  686. if (num_online_cpus() == 1)
  687. return -EBUSY;
  688. if (!cpu_present(cpu))
  689. return -EINVAL;
  690. cpu_hotplug_begin();
  691. cpuhp_tasks_frozen = tasks_frozen;
  692. prev_state = st->state;
  693. st->target = target;
  694. /*
  695. * If the current CPU state is in the range of the AP hotplug thread,
  696. * then we need to kick the thread.
  697. */
  698. if (st->state > CPUHP_TEARDOWN_CPU) {
  699. ret = cpuhp_kick_ap_work(cpu);
  700. /*
  701. * The AP side has done the error rollback already. Just
  702. * return the error code..
  703. */
  704. if (ret)
  705. goto out;
  706. /*
  707. * We might have stopped still in the range of the AP hotplug
  708. * thread. Nothing to do anymore.
  709. */
  710. if (st->state > CPUHP_TEARDOWN_CPU)
  711. goto out;
  712. }
  713. /*
  714. * The AP brought itself down to CPUHP_TEARDOWN_CPU. So we need
  715. * to do the further cleanups.
  716. */
  717. ret = cpuhp_down_callbacks(cpu, st, cpuhp_bp_states, target);
  718. if (ret && st->state > CPUHP_TEARDOWN_CPU && st->state < prev_state) {
  719. st->target = prev_state;
  720. st->rollback = true;
  721. cpuhp_kick_ap_work(cpu);
  722. }
  723. hasdied = prev_state != st->state && st->state == CPUHP_OFFLINE;
  724. out:
  725. cpu_hotplug_done();
  726. /* This post dead nonsense must die */
  727. if (!ret && hasdied)
  728. cpu_notify_nofail(CPU_POST_DEAD, cpu);
  729. return ret;
  730. }
  731. static int do_cpu_down(unsigned int cpu, enum cpuhp_state target)
  732. {
  733. int err;
  734. cpu_maps_update_begin();
  735. if (cpu_hotplug_disabled) {
  736. err = -EBUSY;
  737. goto out;
  738. }
  739. err = _cpu_down(cpu, 0, target);
  740. out:
  741. cpu_maps_update_done();
  742. return err;
  743. }
  744. int cpu_down(unsigned int cpu)
  745. {
  746. return do_cpu_down(cpu, CPUHP_OFFLINE);
  747. }
  748. EXPORT_SYMBOL(cpu_down);
  749. #endif /*CONFIG_HOTPLUG_CPU*/
  750. /**
  751. * notify_cpu_starting(cpu) - call the CPU_STARTING notifiers
  752. * @cpu: cpu that just started
  753. *
  754. * This function calls the cpu_chain notifiers with CPU_STARTING.
  755. * It must be called by the arch code on the new cpu, before the new cpu
  756. * enables interrupts and before the "boot" cpu returns from __cpu_up().
  757. */
  758. void notify_cpu_starting(unsigned int cpu)
  759. {
  760. struct cpuhp_cpu_state *st = per_cpu_ptr(&cpuhp_state, cpu);
  761. enum cpuhp_state target = min((int)st->target, CPUHP_AP_ONLINE);
  762. while (st->state < target) {
  763. struct cpuhp_step *step;
  764. st->state++;
  765. step = cpuhp_ap_states + st->state;
  766. cpuhp_invoke_callback(cpu, st->state, step->startup);
  767. }
  768. }
  769. /*
  770. * Called from the idle task. We need to set active here, so we can kick off
  771. * the stopper thread and unpark the smpboot threads. If the target state is
  772. * beyond CPUHP_AP_ONLINE_IDLE we kick cpuhp thread and let it bring up the
  773. * cpu further.
  774. */
  775. void cpuhp_online_idle(enum cpuhp_state state)
  776. {
  777. struct cpuhp_cpu_state *st = this_cpu_ptr(&cpuhp_state);
  778. unsigned int cpu = smp_processor_id();
  779. /* Happens for the boot cpu */
  780. if (state != CPUHP_AP_ONLINE_IDLE)
  781. return;
  782. st->state = CPUHP_AP_ONLINE_IDLE;
  783. /* Unpark the stopper thread and the hotplug thread of this cpu */
  784. stop_machine_unpark(cpu);
  785. kthread_unpark(st->thread);
  786. /* Should we go further up ? */
  787. if (st->target > CPUHP_AP_ONLINE_IDLE)
  788. __cpuhp_kick_ap_work(st);
  789. else
  790. complete(&st->done);
  791. }
  792. /* Requires cpu_add_remove_lock to be held */
  793. static int _cpu_up(unsigned int cpu, int tasks_frozen, enum cpuhp_state target)
  794. {
  795. struct cpuhp_cpu_state *st = per_cpu_ptr(&cpuhp_state, cpu);
  796. struct task_struct *idle;
  797. int ret = 0;
  798. cpu_hotplug_begin();
  799. if (!cpu_present(cpu)) {
  800. ret = -EINVAL;
  801. goto out;
  802. }
  803. /*
  804. * The caller of do_cpu_up might have raced with another
  805. * caller. Ignore it for now.
  806. */
  807. if (st->state >= target)
  808. goto out;
  809. if (st->state == CPUHP_OFFLINE) {
  810. /* Let it fail before we try to bring the cpu up */
  811. idle = idle_thread_get(cpu);
  812. if (IS_ERR(idle)) {
  813. ret = PTR_ERR(idle);
  814. goto out;
  815. }
  816. }
  817. cpuhp_tasks_frozen = tasks_frozen;
  818. st->target = target;
  819. /*
  820. * If the current CPU state is in the range of the AP hotplug thread,
  821. * then we need to kick the thread once more.
  822. */
  823. if (st->state > CPUHP_BRINGUP_CPU) {
  824. ret = cpuhp_kick_ap_work(cpu);
  825. /*
  826. * The AP side has done the error rollback already. Just
  827. * return the error code..
  828. */
  829. if (ret)
  830. goto out;
  831. }
  832. /*
  833. * Try to reach the target state. We max out on the BP at
  834. * CPUHP_BRINGUP_CPU. After that the AP hotplug thread is
  835. * responsible for bringing it up to the target state.
  836. */
  837. target = min((int)target, CPUHP_BRINGUP_CPU);
  838. ret = cpuhp_up_callbacks(cpu, st, cpuhp_bp_states, target);
  839. out:
  840. cpu_hotplug_done();
  841. return ret;
  842. }
  843. static int do_cpu_up(unsigned int cpu, enum cpuhp_state target)
  844. {
  845. int err = 0;
  846. if (!cpu_possible(cpu)) {
  847. pr_err("can't online cpu %d because it is not configured as may-hotadd at boot time\n",
  848. cpu);
  849. #if defined(CONFIG_IA64)
  850. pr_err("please check additional_cpus= boot parameter\n");
  851. #endif
  852. return -EINVAL;
  853. }
  854. err = try_online_node(cpu_to_node(cpu));
  855. if (err)
  856. return err;
  857. cpu_maps_update_begin();
  858. if (cpu_hotplug_disabled) {
  859. err = -EBUSY;
  860. goto out;
  861. }
  862. err = _cpu_up(cpu, 0, target);
  863. out:
  864. cpu_maps_update_done();
  865. return err;
  866. }
  867. int cpu_up(unsigned int cpu)
  868. {
  869. return do_cpu_up(cpu, CPUHP_ONLINE);
  870. }
  871. EXPORT_SYMBOL_GPL(cpu_up);
  872. #ifdef CONFIG_PM_SLEEP_SMP
  873. static cpumask_var_t frozen_cpus;
  874. int disable_nonboot_cpus(void)
  875. {
  876. int cpu, first_cpu, error = 0;
  877. cpu_maps_update_begin();
  878. first_cpu = cpumask_first(cpu_online_mask);
  879. /*
  880. * We take down all of the non-boot CPUs in one shot to avoid races
  881. * with the userspace trying to use the CPU hotplug at the same time
  882. */
  883. cpumask_clear(frozen_cpus);
  884. pr_info("Disabling non-boot CPUs ...\n");
  885. for_each_online_cpu(cpu) {
  886. if (cpu == first_cpu)
  887. continue;
  888. trace_suspend_resume(TPS("CPU_OFF"), cpu, true);
  889. error = _cpu_down(cpu, 1, CPUHP_OFFLINE);
  890. trace_suspend_resume(TPS("CPU_OFF"), cpu, false);
  891. if (!error)
  892. cpumask_set_cpu(cpu, frozen_cpus);
  893. else {
  894. pr_err("Error taking CPU%d down: %d\n", cpu, error);
  895. break;
  896. }
  897. }
  898. if (!error)
  899. BUG_ON(num_online_cpus() > 1);
  900. else
  901. pr_err("Non-boot CPUs are not disabled\n");
  902. /*
  903. * Make sure the CPUs won't be enabled by someone else. We need to do
  904. * this even in case of failure as all disable_nonboot_cpus() users are
  905. * supposed to do enable_nonboot_cpus() on the failure path.
  906. */
  907. cpu_hotplug_disabled++;
  908. cpu_maps_update_done();
  909. return error;
  910. }
  911. void __weak arch_enable_nonboot_cpus_begin(void)
  912. {
  913. }
  914. void __weak arch_enable_nonboot_cpus_end(void)
  915. {
  916. }
  917. void enable_nonboot_cpus(void)
  918. {
  919. int cpu, error;
  920. /* Allow everyone to use the CPU hotplug again */
  921. cpu_maps_update_begin();
  922. WARN_ON(--cpu_hotplug_disabled < 0);
  923. if (cpumask_empty(frozen_cpus))
  924. goto out;
  925. pr_info("Enabling non-boot CPUs ...\n");
  926. arch_enable_nonboot_cpus_begin();
  927. for_each_cpu(cpu, frozen_cpus) {
  928. trace_suspend_resume(TPS("CPU_ON"), cpu, true);
  929. error = _cpu_up(cpu, 1, CPUHP_ONLINE);
  930. trace_suspend_resume(TPS("CPU_ON"), cpu, false);
  931. if (!error) {
  932. pr_info("CPU%d is up\n", cpu);
  933. continue;
  934. }
  935. pr_warn("Error taking CPU%d up: %d\n", cpu, error);
  936. }
  937. arch_enable_nonboot_cpus_end();
  938. cpumask_clear(frozen_cpus);
  939. out:
  940. cpu_maps_update_done();
  941. }
  942. static int __init alloc_frozen_cpus(void)
  943. {
  944. if (!alloc_cpumask_var(&frozen_cpus, GFP_KERNEL|__GFP_ZERO))
  945. return -ENOMEM;
  946. return 0;
  947. }
  948. core_initcall(alloc_frozen_cpus);
  949. /*
  950. * When callbacks for CPU hotplug notifications are being executed, we must
  951. * ensure that the state of the system with respect to the tasks being frozen
  952. * or not, as reported by the notification, remains unchanged *throughout the
  953. * duration* of the execution of the callbacks.
  954. * Hence we need to prevent the freezer from racing with regular CPU hotplug.
  955. *
  956. * This synchronization is implemented by mutually excluding regular CPU
  957. * hotplug and Suspend/Hibernate call paths by hooking onto the Suspend/
  958. * Hibernate notifications.
  959. */
  960. static int
  961. cpu_hotplug_pm_callback(struct notifier_block *nb,
  962. unsigned long action, void *ptr)
  963. {
  964. switch (action) {
  965. case PM_SUSPEND_PREPARE:
  966. case PM_HIBERNATION_PREPARE:
  967. cpu_hotplug_disable();
  968. break;
  969. case PM_POST_SUSPEND:
  970. case PM_POST_HIBERNATION:
  971. cpu_hotplug_enable();
  972. break;
  973. default:
  974. return NOTIFY_DONE;
  975. }
  976. return NOTIFY_OK;
  977. }
  978. static int __init cpu_hotplug_pm_sync_init(void)
  979. {
  980. /*
  981. * cpu_hotplug_pm_callback has higher priority than x86
  982. * bsp_pm_callback which depends on cpu_hotplug_pm_callback
  983. * to disable cpu hotplug to avoid cpu hotplug race.
  984. */
  985. pm_notifier(cpu_hotplug_pm_callback, 0);
  986. return 0;
  987. }
  988. core_initcall(cpu_hotplug_pm_sync_init);
  989. #endif /* CONFIG_PM_SLEEP_SMP */
  990. #endif /* CONFIG_SMP */
  991. /* Boot processor state steps */
  992. static struct cpuhp_step cpuhp_bp_states[] = {
  993. [CPUHP_OFFLINE] = {
  994. .name = "offline",
  995. .startup = NULL,
  996. .teardown = NULL,
  997. },
  998. #ifdef CONFIG_SMP
  999. [CPUHP_CREATE_THREADS]= {
  1000. .name = "threads:create",
  1001. .startup = smpboot_create_threads,
  1002. .teardown = NULL,
  1003. .cant_stop = true,
  1004. },
  1005. /*
  1006. * Preparatory and dead notifiers. Will be replaced once the notifiers
  1007. * are converted to states.
  1008. */
  1009. [CPUHP_NOTIFY_PREPARE] = {
  1010. .name = "notify:prepare",
  1011. .startup = notify_prepare,
  1012. .teardown = notify_dead,
  1013. .skip_onerr = true,
  1014. .cant_stop = true,
  1015. },
  1016. /* Kicks the plugged cpu into life */
  1017. [CPUHP_BRINGUP_CPU] = {
  1018. .name = "cpu:bringup",
  1019. .startup = bringup_cpu,
  1020. .teardown = NULL,
  1021. .cant_stop = true,
  1022. },
  1023. /*
  1024. * Handled on controll processor until the plugged processor manages
  1025. * this itself.
  1026. */
  1027. [CPUHP_TEARDOWN_CPU] = {
  1028. .name = "cpu:teardown",
  1029. .startup = NULL,
  1030. .teardown = takedown_cpu,
  1031. .cant_stop = true,
  1032. },
  1033. #else
  1034. [CPUHP_BRINGUP_CPU] = { },
  1035. #endif
  1036. };
  1037. /* Application processor state steps */
  1038. static struct cpuhp_step cpuhp_ap_states[] = {
  1039. #ifdef CONFIG_SMP
  1040. /* Final state before CPU kills itself */
  1041. [CPUHP_AP_IDLE_DEAD] = {
  1042. .name = "idle:dead",
  1043. },
  1044. /*
  1045. * Last state before CPU enters the idle loop to die. Transient state
  1046. * for synchronization.
  1047. */
  1048. [CPUHP_AP_OFFLINE] = {
  1049. .name = "ap:offline",
  1050. .cant_stop = true,
  1051. },
  1052. /* First state is scheduler control. Interrupts are disabled */
  1053. [CPUHP_AP_SCHED_STARTING] = {
  1054. .name = "sched:starting",
  1055. .startup = sched_cpu_starting,
  1056. .teardown = sched_cpu_dying,
  1057. },
  1058. /*
  1059. * Low level startup/teardown notifiers. Run with interrupts
  1060. * disabled. Will be removed once the notifiers are converted to
  1061. * states.
  1062. */
  1063. [CPUHP_AP_NOTIFY_STARTING] = {
  1064. .name = "notify:starting",
  1065. .startup = notify_starting,
  1066. .teardown = notify_dying,
  1067. .skip_onerr = true,
  1068. .cant_stop = true,
  1069. },
  1070. /* Entry state on starting. Interrupts enabled from here on. Transient
  1071. * state for synchronsization */
  1072. [CPUHP_AP_ONLINE] = {
  1073. .name = "ap:online",
  1074. },
  1075. /* Handle smpboot threads park/unpark */
  1076. [CPUHP_AP_SMPBOOT_THREADS] = {
  1077. .name = "smpboot:threads",
  1078. .startup = smpboot_unpark_threads,
  1079. .teardown = NULL,
  1080. },
  1081. /*
  1082. * Online/down_prepare notifiers. Will be removed once the notifiers
  1083. * are converted to states.
  1084. */
  1085. [CPUHP_AP_NOTIFY_ONLINE] = {
  1086. .name = "notify:online",
  1087. .startup = notify_online,
  1088. .teardown = notify_down_prepare,
  1089. .skip_onerr = true,
  1090. },
  1091. #endif
  1092. /*
  1093. * The dynamically registered state space is here
  1094. */
  1095. #ifdef CONFIG_SMP
  1096. /* Last state is scheduler control setting the cpu active */
  1097. [CPUHP_AP_ACTIVE] = {
  1098. .name = "sched:active",
  1099. .startup = sched_cpu_activate,
  1100. .teardown = sched_cpu_deactivate,
  1101. },
  1102. #endif
  1103. /* CPU is fully up and running. */
  1104. [CPUHP_ONLINE] = {
  1105. .name = "online",
  1106. .startup = NULL,
  1107. .teardown = NULL,
  1108. },
  1109. };
  1110. /* Sanity check for callbacks */
  1111. static int cpuhp_cb_check(enum cpuhp_state state)
  1112. {
  1113. if (state <= CPUHP_OFFLINE || state >= CPUHP_ONLINE)
  1114. return -EINVAL;
  1115. return 0;
  1116. }
  1117. static bool cpuhp_is_ap_state(enum cpuhp_state state)
  1118. {
  1119. /*
  1120. * The extra check for CPUHP_TEARDOWN_CPU is only for documentation
  1121. * purposes as that state is handled explicitely in cpu_down.
  1122. */
  1123. return state > CPUHP_BRINGUP_CPU && state != CPUHP_TEARDOWN_CPU;
  1124. }
  1125. static struct cpuhp_step *cpuhp_get_step(enum cpuhp_state state)
  1126. {
  1127. struct cpuhp_step *sp;
  1128. sp = cpuhp_is_ap_state(state) ? cpuhp_ap_states : cpuhp_bp_states;
  1129. return sp + state;
  1130. }
  1131. static void cpuhp_store_callbacks(enum cpuhp_state state,
  1132. const char *name,
  1133. int (*startup)(unsigned int cpu),
  1134. int (*teardown)(unsigned int cpu))
  1135. {
  1136. /* (Un)Install the callbacks for further cpu hotplug operations */
  1137. struct cpuhp_step *sp;
  1138. mutex_lock(&cpuhp_state_mutex);
  1139. sp = cpuhp_get_step(state);
  1140. sp->startup = startup;
  1141. sp->teardown = teardown;
  1142. sp->name = name;
  1143. mutex_unlock(&cpuhp_state_mutex);
  1144. }
  1145. static void *cpuhp_get_teardown_cb(enum cpuhp_state state)
  1146. {
  1147. return cpuhp_get_step(state)->teardown;
  1148. }
  1149. /*
  1150. * Call the startup/teardown function for a step either on the AP or
  1151. * on the current CPU.
  1152. */
  1153. static int cpuhp_issue_call(int cpu, enum cpuhp_state state,
  1154. int (*cb)(unsigned int), bool bringup)
  1155. {
  1156. int ret;
  1157. if (!cb)
  1158. return 0;
  1159. /*
  1160. * The non AP bound callbacks can fail on bringup. On teardown
  1161. * e.g. module removal we crash for now.
  1162. */
  1163. #ifdef CONFIG_SMP
  1164. if (cpuhp_is_ap_state(state))
  1165. ret = cpuhp_invoke_ap_callback(cpu, state, cb);
  1166. else
  1167. ret = cpuhp_invoke_callback(cpu, state, cb);
  1168. #else
  1169. ret = cpuhp_invoke_callback(cpu, state, cb);
  1170. #endif
  1171. BUG_ON(ret && !bringup);
  1172. return ret;
  1173. }
  1174. /*
  1175. * Called from __cpuhp_setup_state on a recoverable failure.
  1176. *
  1177. * Note: The teardown callbacks for rollback are not allowed to fail!
  1178. */
  1179. static void cpuhp_rollback_install(int failedcpu, enum cpuhp_state state,
  1180. int (*teardown)(unsigned int cpu))
  1181. {
  1182. int cpu;
  1183. if (!teardown)
  1184. return;
  1185. /* Roll back the already executed steps on the other cpus */
  1186. for_each_present_cpu(cpu) {
  1187. struct cpuhp_cpu_state *st = per_cpu_ptr(&cpuhp_state, cpu);
  1188. int cpustate = st->state;
  1189. if (cpu >= failedcpu)
  1190. break;
  1191. /* Did we invoke the startup call on that cpu ? */
  1192. if (cpustate >= state)
  1193. cpuhp_issue_call(cpu, state, teardown, false);
  1194. }
  1195. }
  1196. /*
  1197. * Returns a free for dynamic slot assignment of the Online state. The states
  1198. * are protected by the cpuhp_slot_states mutex and an empty slot is identified
  1199. * by having no name assigned.
  1200. */
  1201. static int cpuhp_reserve_state(enum cpuhp_state state)
  1202. {
  1203. enum cpuhp_state i;
  1204. mutex_lock(&cpuhp_state_mutex);
  1205. for (i = CPUHP_AP_ONLINE_DYN; i <= CPUHP_AP_ONLINE_DYN_END; i++) {
  1206. if (cpuhp_ap_states[i].name)
  1207. continue;
  1208. cpuhp_ap_states[i].name = "Reserved";
  1209. mutex_unlock(&cpuhp_state_mutex);
  1210. return i;
  1211. }
  1212. mutex_unlock(&cpuhp_state_mutex);
  1213. WARN(1, "No more dynamic states available for CPU hotplug\n");
  1214. return -ENOSPC;
  1215. }
  1216. /**
  1217. * __cpuhp_setup_state - Setup the callbacks for an hotplug machine state
  1218. * @state: The state to setup
  1219. * @invoke: If true, the startup function is invoked for cpus where
  1220. * cpu state >= @state
  1221. * @startup: startup callback function
  1222. * @teardown: teardown callback function
  1223. *
  1224. * Returns 0 if successful, otherwise a proper error code
  1225. */
  1226. int __cpuhp_setup_state(enum cpuhp_state state,
  1227. const char *name, bool invoke,
  1228. int (*startup)(unsigned int cpu),
  1229. int (*teardown)(unsigned int cpu))
  1230. {
  1231. int cpu, ret = 0;
  1232. int dyn_state = 0;
  1233. if (cpuhp_cb_check(state) || !name)
  1234. return -EINVAL;
  1235. get_online_cpus();
  1236. /* currently assignments for the ONLINE state are possible */
  1237. if (state == CPUHP_AP_ONLINE_DYN) {
  1238. dyn_state = 1;
  1239. ret = cpuhp_reserve_state(state);
  1240. if (ret < 0)
  1241. goto out;
  1242. state = ret;
  1243. }
  1244. cpuhp_store_callbacks(state, name, startup, teardown);
  1245. if (!invoke || !startup)
  1246. goto out;
  1247. /*
  1248. * Try to call the startup callback for each present cpu
  1249. * depending on the hotplug state of the cpu.
  1250. */
  1251. for_each_present_cpu(cpu) {
  1252. struct cpuhp_cpu_state *st = per_cpu_ptr(&cpuhp_state, cpu);
  1253. int cpustate = st->state;
  1254. if (cpustate < state)
  1255. continue;
  1256. ret = cpuhp_issue_call(cpu, state, startup, true);
  1257. if (ret) {
  1258. cpuhp_rollback_install(cpu, state, teardown);
  1259. cpuhp_store_callbacks(state, NULL, NULL, NULL);
  1260. goto out;
  1261. }
  1262. }
  1263. out:
  1264. put_online_cpus();
  1265. if (!ret && dyn_state)
  1266. return state;
  1267. return ret;
  1268. }
  1269. EXPORT_SYMBOL(__cpuhp_setup_state);
  1270. /**
  1271. * __cpuhp_remove_state - Remove the callbacks for an hotplug machine state
  1272. * @state: The state to remove
  1273. * @invoke: If true, the teardown function is invoked for cpus where
  1274. * cpu state >= @state
  1275. *
  1276. * The teardown callback is currently not allowed to fail. Think
  1277. * about module removal!
  1278. */
  1279. void __cpuhp_remove_state(enum cpuhp_state state, bool invoke)
  1280. {
  1281. int (*teardown)(unsigned int cpu) = cpuhp_get_teardown_cb(state);
  1282. int cpu;
  1283. BUG_ON(cpuhp_cb_check(state));
  1284. get_online_cpus();
  1285. if (!invoke || !teardown)
  1286. goto remove;
  1287. /*
  1288. * Call the teardown callback for each present cpu depending
  1289. * on the hotplug state of the cpu. This function is not
  1290. * allowed to fail currently!
  1291. */
  1292. for_each_present_cpu(cpu) {
  1293. struct cpuhp_cpu_state *st = per_cpu_ptr(&cpuhp_state, cpu);
  1294. int cpustate = st->state;
  1295. if (cpustate >= state)
  1296. cpuhp_issue_call(cpu, state, teardown, false);
  1297. }
  1298. remove:
  1299. cpuhp_store_callbacks(state, NULL, NULL, NULL);
  1300. put_online_cpus();
  1301. }
  1302. EXPORT_SYMBOL(__cpuhp_remove_state);
  1303. #if defined(CONFIG_SYSFS) && defined(CONFIG_HOTPLUG_CPU)
  1304. static ssize_t show_cpuhp_state(struct device *dev,
  1305. struct device_attribute *attr, char *buf)
  1306. {
  1307. struct cpuhp_cpu_state *st = per_cpu_ptr(&cpuhp_state, dev->id);
  1308. return sprintf(buf, "%d\n", st->state);
  1309. }
  1310. static DEVICE_ATTR(state, 0444, show_cpuhp_state, NULL);
  1311. static ssize_t write_cpuhp_target(struct device *dev,
  1312. struct device_attribute *attr,
  1313. const char *buf, size_t count)
  1314. {
  1315. struct cpuhp_cpu_state *st = per_cpu_ptr(&cpuhp_state, dev->id);
  1316. struct cpuhp_step *sp;
  1317. int target, ret;
  1318. ret = kstrtoint(buf, 10, &target);
  1319. if (ret)
  1320. return ret;
  1321. #ifdef CONFIG_CPU_HOTPLUG_STATE_CONTROL
  1322. if (target < CPUHP_OFFLINE || target > CPUHP_ONLINE)
  1323. return -EINVAL;
  1324. #else
  1325. if (target != CPUHP_OFFLINE && target != CPUHP_ONLINE)
  1326. return -EINVAL;
  1327. #endif
  1328. ret = lock_device_hotplug_sysfs();
  1329. if (ret)
  1330. return ret;
  1331. mutex_lock(&cpuhp_state_mutex);
  1332. sp = cpuhp_get_step(target);
  1333. ret = !sp->name || sp->cant_stop ? -EINVAL : 0;
  1334. mutex_unlock(&cpuhp_state_mutex);
  1335. if (ret)
  1336. return ret;
  1337. if (st->state < target)
  1338. ret = do_cpu_up(dev->id, target);
  1339. else
  1340. ret = do_cpu_down(dev->id, target);
  1341. unlock_device_hotplug();
  1342. return ret ? ret : count;
  1343. }
  1344. static ssize_t show_cpuhp_target(struct device *dev,
  1345. struct device_attribute *attr, char *buf)
  1346. {
  1347. struct cpuhp_cpu_state *st = per_cpu_ptr(&cpuhp_state, dev->id);
  1348. return sprintf(buf, "%d\n", st->target);
  1349. }
  1350. static DEVICE_ATTR(target, 0644, show_cpuhp_target, write_cpuhp_target);
  1351. static struct attribute *cpuhp_cpu_attrs[] = {
  1352. &dev_attr_state.attr,
  1353. &dev_attr_target.attr,
  1354. NULL
  1355. };
  1356. static struct attribute_group cpuhp_cpu_attr_group = {
  1357. .attrs = cpuhp_cpu_attrs,
  1358. .name = "hotplug",
  1359. NULL
  1360. };
  1361. static ssize_t show_cpuhp_states(struct device *dev,
  1362. struct device_attribute *attr, char *buf)
  1363. {
  1364. ssize_t cur, res = 0;
  1365. int i;
  1366. mutex_lock(&cpuhp_state_mutex);
  1367. for (i = CPUHP_OFFLINE; i <= CPUHP_ONLINE; i++) {
  1368. struct cpuhp_step *sp = cpuhp_get_step(i);
  1369. if (sp->name) {
  1370. cur = sprintf(buf, "%3d: %s\n", i, sp->name);
  1371. buf += cur;
  1372. res += cur;
  1373. }
  1374. }
  1375. mutex_unlock(&cpuhp_state_mutex);
  1376. return res;
  1377. }
  1378. static DEVICE_ATTR(states, 0444, show_cpuhp_states, NULL);
  1379. static struct attribute *cpuhp_cpu_root_attrs[] = {
  1380. &dev_attr_states.attr,
  1381. NULL
  1382. };
  1383. static struct attribute_group cpuhp_cpu_root_attr_group = {
  1384. .attrs = cpuhp_cpu_root_attrs,
  1385. .name = "hotplug",
  1386. NULL
  1387. };
  1388. static int __init cpuhp_sysfs_init(void)
  1389. {
  1390. int cpu, ret;
  1391. ret = sysfs_create_group(&cpu_subsys.dev_root->kobj,
  1392. &cpuhp_cpu_root_attr_group);
  1393. if (ret)
  1394. return ret;
  1395. for_each_possible_cpu(cpu) {
  1396. struct device *dev = get_cpu_device(cpu);
  1397. if (!dev)
  1398. continue;
  1399. ret = sysfs_create_group(&dev->kobj, &cpuhp_cpu_attr_group);
  1400. if (ret)
  1401. return ret;
  1402. }
  1403. return 0;
  1404. }
  1405. device_initcall(cpuhp_sysfs_init);
  1406. #endif
  1407. /*
  1408. * cpu_bit_bitmap[] is a special, "compressed" data structure that
  1409. * represents all NR_CPUS bits binary values of 1<<nr.
  1410. *
  1411. * It is used by cpumask_of() to get a constant address to a CPU
  1412. * mask value that has a single bit set only.
  1413. */
  1414. /* cpu_bit_bitmap[0] is empty - so we can back into it */
  1415. #define MASK_DECLARE_1(x) [x+1][0] = (1UL << (x))
  1416. #define MASK_DECLARE_2(x) MASK_DECLARE_1(x), MASK_DECLARE_1(x+1)
  1417. #define MASK_DECLARE_4(x) MASK_DECLARE_2(x), MASK_DECLARE_2(x+2)
  1418. #define MASK_DECLARE_8(x) MASK_DECLARE_4(x), MASK_DECLARE_4(x+4)
  1419. const unsigned long cpu_bit_bitmap[BITS_PER_LONG+1][BITS_TO_LONGS(NR_CPUS)] = {
  1420. MASK_DECLARE_8(0), MASK_DECLARE_8(8),
  1421. MASK_DECLARE_8(16), MASK_DECLARE_8(24),
  1422. #if BITS_PER_LONG > 32
  1423. MASK_DECLARE_8(32), MASK_DECLARE_8(40),
  1424. MASK_DECLARE_8(48), MASK_DECLARE_8(56),
  1425. #endif
  1426. };
  1427. EXPORT_SYMBOL_GPL(cpu_bit_bitmap);
  1428. const DECLARE_BITMAP(cpu_all_bits, NR_CPUS) = CPU_BITS_ALL;
  1429. EXPORT_SYMBOL(cpu_all_bits);
  1430. #ifdef CONFIG_INIT_ALL_POSSIBLE
  1431. struct cpumask __cpu_possible_mask __read_mostly
  1432. = {CPU_BITS_ALL};
  1433. #else
  1434. struct cpumask __cpu_possible_mask __read_mostly;
  1435. #endif
  1436. EXPORT_SYMBOL(__cpu_possible_mask);
  1437. struct cpumask __cpu_online_mask __read_mostly;
  1438. EXPORT_SYMBOL(__cpu_online_mask);
  1439. struct cpumask __cpu_present_mask __read_mostly;
  1440. EXPORT_SYMBOL(__cpu_present_mask);
  1441. struct cpumask __cpu_active_mask __read_mostly;
  1442. EXPORT_SYMBOL(__cpu_active_mask);
  1443. void init_cpu_present(const struct cpumask *src)
  1444. {
  1445. cpumask_copy(&__cpu_present_mask, src);
  1446. }
  1447. void init_cpu_possible(const struct cpumask *src)
  1448. {
  1449. cpumask_copy(&__cpu_possible_mask, src);
  1450. }
  1451. void init_cpu_online(const struct cpumask *src)
  1452. {
  1453. cpumask_copy(&__cpu_online_mask, src);
  1454. }
  1455. /*
  1456. * Activate the first processor.
  1457. */
  1458. void __init boot_cpu_init(void)
  1459. {
  1460. int cpu = smp_processor_id();
  1461. /* Mark the boot cpu "present", "online" etc for SMP and UP case */
  1462. set_cpu_online(cpu, true);
  1463. set_cpu_active(cpu, true);
  1464. set_cpu_present(cpu, true);
  1465. set_cpu_possible(cpu, true);
  1466. }
  1467. /*
  1468. * Must be called _AFTER_ setting up the per_cpu areas
  1469. */
  1470. void __init boot_cpu_state_init(void)
  1471. {
  1472. per_cpu_ptr(&cpuhp_state, smp_processor_id())->state = CPUHP_ONLINE;
  1473. }