cpu.c 40 KB

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