cpu.c 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632
  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. int err;
  599. /*
  600. * By now we've cleared cpu_active_mask, wait for all preempt-disabled
  601. * and RCU users of this state to go away such that all new such users
  602. * will observe it.
  603. *
  604. * For CONFIG_PREEMPT we have preemptible RCU and its sync_rcu() might
  605. * not imply sync_sched(), so wait for both.
  606. *
  607. * Do sync before park smpboot threads to take care the rcu boost case.
  608. */
  609. if (IS_ENABLED(CONFIG_PREEMPT))
  610. synchronize_rcu_mult(call_rcu, call_rcu_sched);
  611. else
  612. synchronize_rcu();
  613. /* Park the hotplug thread */
  614. kthread_park(per_cpu_ptr(&cpuhp_state, cpu)->thread);
  615. /*
  616. * Prevent irq alloc/free while the dying cpu reorganizes the
  617. * interrupt affinities.
  618. */
  619. irq_lock_sparse();
  620. /*
  621. * So now all preempt/rcu users must observe !cpu_active().
  622. */
  623. err = stop_machine(take_cpu_down, NULL, cpumask_of(cpu));
  624. if (err) {
  625. /* CPU didn't die: tell everyone. Can't complain. */
  626. cpu_notify_nofail(CPU_DOWN_FAILED, cpu);
  627. irq_unlock_sparse();
  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. while (!per_cpu(cpu_dead_idle, cpu))
  639. cpu_relax();
  640. smp_mb(); /* Read from cpu_dead_idle before __cpu_die(). */
  641. per_cpu(cpu_dead_idle, cpu) = false;
  642. /* Interrupts are moved away from the dying cpu, reenable alloc/free */
  643. irq_unlock_sparse();
  644. hotplug_cpu__broadcast_tick_pull(cpu);
  645. /* This actually kills the CPU. */
  646. __cpu_die(cpu);
  647. tick_cleanup_dead_cpu(cpu);
  648. return 0;
  649. }
  650. static int notify_dead(unsigned int cpu)
  651. {
  652. cpu_notify_nofail(CPU_DEAD, cpu);
  653. check_for_tasks(cpu);
  654. return 0;
  655. }
  656. #else
  657. #define notify_down_prepare NULL
  658. #define takedown_cpu NULL
  659. #define notify_dead NULL
  660. #define notify_dying NULL
  661. #endif
  662. #ifdef CONFIG_HOTPLUG_CPU
  663. /* Requires cpu_add_remove_lock to be held */
  664. static int __ref _cpu_down(unsigned int cpu, int tasks_frozen,
  665. enum cpuhp_state target)
  666. {
  667. struct cpuhp_cpu_state *st = per_cpu_ptr(&cpuhp_state, cpu);
  668. int prev_state, ret = 0;
  669. bool hasdied = false;
  670. if (num_online_cpus() == 1)
  671. return -EBUSY;
  672. if (!cpu_present(cpu))
  673. return -EINVAL;
  674. cpu_hotplug_begin();
  675. cpuhp_tasks_frozen = tasks_frozen;
  676. prev_state = st->state;
  677. st->target = target;
  678. /*
  679. * If the current CPU state is in the range of the AP hotplug thread,
  680. * then we need to kick the thread.
  681. */
  682. if (st->state > CPUHP_TEARDOWN_CPU) {
  683. ret = cpuhp_kick_ap_work(cpu);
  684. /*
  685. * The AP side has done the error rollback already. Just
  686. * return the error code..
  687. */
  688. if (ret)
  689. goto out;
  690. /*
  691. * We might have stopped still in the range of the AP hotplug
  692. * thread. Nothing to do anymore.
  693. */
  694. if (st->state > CPUHP_TEARDOWN_CPU)
  695. goto out;
  696. }
  697. /*
  698. * The AP brought itself down to CPUHP_TEARDOWN_CPU. So we need
  699. * to do the further cleanups.
  700. */
  701. ret = cpuhp_down_callbacks(cpu, st, cpuhp_bp_states, target);
  702. hasdied = prev_state != st->state && st->state == CPUHP_OFFLINE;
  703. out:
  704. cpu_hotplug_done();
  705. /* This post dead nonsense must die */
  706. if (!ret && hasdied)
  707. cpu_notify_nofail(CPU_POST_DEAD, cpu);
  708. return ret;
  709. }
  710. static int do_cpu_down(unsigned int cpu, enum cpuhp_state target)
  711. {
  712. int err;
  713. cpu_maps_update_begin();
  714. if (cpu_hotplug_disabled) {
  715. err = -EBUSY;
  716. goto out;
  717. }
  718. err = _cpu_down(cpu, 0, target);
  719. out:
  720. cpu_maps_update_done();
  721. return err;
  722. }
  723. int cpu_down(unsigned int cpu)
  724. {
  725. return do_cpu_down(cpu, CPUHP_OFFLINE);
  726. }
  727. EXPORT_SYMBOL(cpu_down);
  728. #endif /*CONFIG_HOTPLUG_CPU*/
  729. /**
  730. * notify_cpu_starting(cpu) - call the CPU_STARTING notifiers
  731. * @cpu: cpu that just started
  732. *
  733. * This function calls the cpu_chain notifiers with CPU_STARTING.
  734. * It must be called by the arch code on the new cpu, before the new cpu
  735. * enables interrupts and before the "boot" cpu returns from __cpu_up().
  736. */
  737. void notify_cpu_starting(unsigned int cpu)
  738. {
  739. struct cpuhp_cpu_state *st = per_cpu_ptr(&cpuhp_state, cpu);
  740. enum cpuhp_state target = min((int)st->target, CPUHP_AP_ONLINE);
  741. while (st->state < target) {
  742. struct cpuhp_step *step;
  743. st->state++;
  744. step = cpuhp_ap_states + st->state;
  745. cpuhp_invoke_callback(cpu, st->state, step->startup);
  746. }
  747. }
  748. /*
  749. * Called from the idle task. We need to set active here, so we can kick off
  750. * the stopper thread and unpark the smpboot threads. If the target state is
  751. * beyond CPUHP_AP_ONLINE_IDLE we kick cpuhp thread and let it bring up the
  752. * cpu further.
  753. */
  754. void cpuhp_online_idle(enum cpuhp_state state)
  755. {
  756. struct cpuhp_cpu_state *st = this_cpu_ptr(&cpuhp_state);
  757. unsigned int cpu = smp_processor_id();
  758. /* Happens for the boot cpu */
  759. if (state != CPUHP_AP_ONLINE_IDLE)
  760. return;
  761. st->state = CPUHP_AP_ONLINE_IDLE;
  762. /* The cpu is marked online, set it active now */
  763. set_cpu_active(cpu, true);
  764. /* Unpark the stopper thread and the hotplug thread of this cpu */
  765. stop_machine_unpark(cpu);
  766. kthread_unpark(st->thread);
  767. /* Should we go further up ? */
  768. if (st->target > CPUHP_AP_ONLINE_IDLE)
  769. __cpuhp_kick_ap_work(st);
  770. else
  771. complete(&st->done);
  772. }
  773. /* Requires cpu_add_remove_lock to be held */
  774. static int _cpu_up(unsigned int cpu, int tasks_frozen, enum cpuhp_state target)
  775. {
  776. struct cpuhp_cpu_state *st = per_cpu_ptr(&cpuhp_state, cpu);
  777. struct task_struct *idle;
  778. int ret = 0;
  779. cpu_hotplug_begin();
  780. if (!cpu_present(cpu)) {
  781. ret = -EINVAL;
  782. goto out;
  783. }
  784. /*
  785. * The caller of do_cpu_up might have raced with another
  786. * caller. Ignore it for now.
  787. */
  788. if (st->state >= target)
  789. goto out;
  790. if (st->state == CPUHP_OFFLINE) {
  791. /* Let it fail before we try to bring the cpu up */
  792. idle = idle_thread_get(cpu);
  793. if (IS_ERR(idle)) {
  794. ret = PTR_ERR(idle);
  795. goto out;
  796. }
  797. }
  798. cpuhp_tasks_frozen = tasks_frozen;
  799. st->target = target;
  800. /*
  801. * If the current CPU state is in the range of the AP hotplug thread,
  802. * then we need to kick the thread once more.
  803. */
  804. if (st->state > CPUHP_BRINGUP_CPU) {
  805. ret = cpuhp_kick_ap_work(cpu);
  806. /*
  807. * The AP side has done the error rollback already. Just
  808. * return the error code..
  809. */
  810. if (ret)
  811. goto out;
  812. }
  813. /*
  814. * Try to reach the target state. We max out on the BP at
  815. * CPUHP_BRINGUP_CPU. After that the AP hotplug thread is
  816. * responsible for bringing it up to the target state.
  817. */
  818. target = min((int)target, CPUHP_BRINGUP_CPU);
  819. ret = cpuhp_up_callbacks(cpu, st, cpuhp_bp_states, target);
  820. out:
  821. cpu_hotplug_done();
  822. return ret;
  823. }
  824. static int do_cpu_up(unsigned int cpu, enum cpuhp_state target)
  825. {
  826. int err = 0;
  827. if (!cpu_possible(cpu)) {
  828. pr_err("can't online cpu %d because it is not configured as may-hotadd at boot time\n",
  829. cpu);
  830. #if defined(CONFIG_IA64)
  831. pr_err("please check additional_cpus= boot parameter\n");
  832. #endif
  833. return -EINVAL;
  834. }
  835. err = try_online_node(cpu_to_node(cpu));
  836. if (err)
  837. return err;
  838. cpu_maps_update_begin();
  839. if (cpu_hotplug_disabled) {
  840. err = -EBUSY;
  841. goto out;
  842. }
  843. err = _cpu_up(cpu, 0, target);
  844. out:
  845. cpu_maps_update_done();
  846. return err;
  847. }
  848. int cpu_up(unsigned int cpu)
  849. {
  850. return do_cpu_up(cpu, CPUHP_ONLINE);
  851. }
  852. EXPORT_SYMBOL_GPL(cpu_up);
  853. #ifdef CONFIG_PM_SLEEP_SMP
  854. static cpumask_var_t frozen_cpus;
  855. int disable_nonboot_cpus(void)
  856. {
  857. int cpu, first_cpu, error = 0;
  858. cpu_maps_update_begin();
  859. first_cpu = cpumask_first(cpu_online_mask);
  860. /*
  861. * We take down all of the non-boot CPUs in one shot to avoid races
  862. * with the userspace trying to use the CPU hotplug at the same time
  863. */
  864. cpumask_clear(frozen_cpus);
  865. pr_info("Disabling non-boot CPUs ...\n");
  866. for_each_online_cpu(cpu) {
  867. if (cpu == first_cpu)
  868. continue;
  869. trace_suspend_resume(TPS("CPU_OFF"), cpu, true);
  870. error = _cpu_down(cpu, 1, CPUHP_OFFLINE);
  871. trace_suspend_resume(TPS("CPU_OFF"), cpu, false);
  872. if (!error)
  873. cpumask_set_cpu(cpu, frozen_cpus);
  874. else {
  875. pr_err("Error taking CPU%d down: %d\n", cpu, error);
  876. break;
  877. }
  878. }
  879. if (!error)
  880. BUG_ON(num_online_cpus() > 1);
  881. else
  882. pr_err("Non-boot CPUs are not disabled\n");
  883. /*
  884. * Make sure the CPUs won't be enabled by someone else. We need to do
  885. * this even in case of failure as all disable_nonboot_cpus() users are
  886. * supposed to do enable_nonboot_cpus() on the failure path.
  887. */
  888. cpu_hotplug_disabled++;
  889. cpu_maps_update_done();
  890. return error;
  891. }
  892. void __weak arch_enable_nonboot_cpus_begin(void)
  893. {
  894. }
  895. void __weak arch_enable_nonboot_cpus_end(void)
  896. {
  897. }
  898. void enable_nonboot_cpus(void)
  899. {
  900. int cpu, error;
  901. /* Allow everyone to use the CPU hotplug again */
  902. cpu_maps_update_begin();
  903. WARN_ON(--cpu_hotplug_disabled < 0);
  904. if (cpumask_empty(frozen_cpus))
  905. goto out;
  906. pr_info("Enabling non-boot CPUs ...\n");
  907. arch_enable_nonboot_cpus_begin();
  908. for_each_cpu(cpu, frozen_cpus) {
  909. trace_suspend_resume(TPS("CPU_ON"), cpu, true);
  910. error = _cpu_up(cpu, 1, CPUHP_ONLINE);
  911. trace_suspend_resume(TPS("CPU_ON"), cpu, false);
  912. if (!error) {
  913. pr_info("CPU%d is up\n", cpu);
  914. continue;
  915. }
  916. pr_warn("Error taking CPU%d up: %d\n", cpu, error);
  917. }
  918. arch_enable_nonboot_cpus_end();
  919. cpumask_clear(frozen_cpus);
  920. out:
  921. cpu_maps_update_done();
  922. }
  923. static int __init alloc_frozen_cpus(void)
  924. {
  925. if (!alloc_cpumask_var(&frozen_cpus, GFP_KERNEL|__GFP_ZERO))
  926. return -ENOMEM;
  927. return 0;
  928. }
  929. core_initcall(alloc_frozen_cpus);
  930. /*
  931. * When callbacks for CPU hotplug notifications are being executed, we must
  932. * ensure that the state of the system with respect to the tasks being frozen
  933. * or not, as reported by the notification, remains unchanged *throughout the
  934. * duration* of the execution of the callbacks.
  935. * Hence we need to prevent the freezer from racing with regular CPU hotplug.
  936. *
  937. * This synchronization is implemented by mutually excluding regular CPU
  938. * hotplug and Suspend/Hibernate call paths by hooking onto the Suspend/
  939. * Hibernate notifications.
  940. */
  941. static int
  942. cpu_hotplug_pm_callback(struct notifier_block *nb,
  943. unsigned long action, void *ptr)
  944. {
  945. switch (action) {
  946. case PM_SUSPEND_PREPARE:
  947. case PM_HIBERNATION_PREPARE:
  948. cpu_hotplug_disable();
  949. break;
  950. case PM_POST_SUSPEND:
  951. case PM_POST_HIBERNATION:
  952. cpu_hotplug_enable();
  953. break;
  954. default:
  955. return NOTIFY_DONE;
  956. }
  957. return NOTIFY_OK;
  958. }
  959. static int __init cpu_hotplug_pm_sync_init(void)
  960. {
  961. /*
  962. * cpu_hotplug_pm_callback has higher priority than x86
  963. * bsp_pm_callback which depends on cpu_hotplug_pm_callback
  964. * to disable cpu hotplug to avoid cpu hotplug race.
  965. */
  966. pm_notifier(cpu_hotplug_pm_callback, 0);
  967. return 0;
  968. }
  969. core_initcall(cpu_hotplug_pm_sync_init);
  970. #endif /* CONFIG_PM_SLEEP_SMP */
  971. #endif /* CONFIG_SMP */
  972. /* Boot processor state steps */
  973. static struct cpuhp_step cpuhp_bp_states[] = {
  974. [CPUHP_OFFLINE] = {
  975. .name = "offline",
  976. .startup = NULL,
  977. .teardown = NULL,
  978. },
  979. #ifdef CONFIG_SMP
  980. [CPUHP_CREATE_THREADS]= {
  981. .name = "threads:create",
  982. .startup = smpboot_create_threads,
  983. .teardown = NULL,
  984. .cant_stop = true,
  985. },
  986. [CPUHP_NOTIFY_PREPARE] = {
  987. .name = "notify:prepare",
  988. .startup = notify_prepare,
  989. .teardown = notify_dead,
  990. .skip_onerr = true,
  991. .cant_stop = true,
  992. },
  993. [CPUHP_BRINGUP_CPU] = {
  994. .name = "cpu:bringup",
  995. .startup = bringup_cpu,
  996. .teardown = NULL,
  997. .cant_stop = true,
  998. },
  999. [CPUHP_TEARDOWN_CPU] = {
  1000. .name = "cpu:teardown",
  1001. .startup = NULL,
  1002. .teardown = takedown_cpu,
  1003. .cant_stop = true,
  1004. },
  1005. #endif
  1006. };
  1007. /* Application processor state steps */
  1008. static struct cpuhp_step cpuhp_ap_states[] = {
  1009. #ifdef CONFIG_SMP
  1010. [CPUHP_AP_NOTIFY_STARTING] = {
  1011. .name = "notify:starting",
  1012. .startup = notify_starting,
  1013. .teardown = notify_dying,
  1014. .skip_onerr = true,
  1015. .cant_stop = true,
  1016. },
  1017. [CPUHP_AP_SMPBOOT_THREADS] = {
  1018. .name = "smpboot:threads",
  1019. .startup = smpboot_unpark_threads,
  1020. .teardown = smpboot_park_threads,
  1021. },
  1022. [CPUHP_AP_NOTIFY_ONLINE] = {
  1023. .name = "notify:online",
  1024. .startup = notify_online,
  1025. .teardown = notify_down_prepare,
  1026. },
  1027. #endif
  1028. [CPUHP_ONLINE] = {
  1029. .name = "online",
  1030. .startup = NULL,
  1031. .teardown = NULL,
  1032. },
  1033. };
  1034. /* Sanity check for callbacks */
  1035. static int cpuhp_cb_check(enum cpuhp_state state)
  1036. {
  1037. if (state <= CPUHP_OFFLINE || state >= CPUHP_ONLINE)
  1038. return -EINVAL;
  1039. return 0;
  1040. }
  1041. static bool cpuhp_is_ap_state(enum cpuhp_state state)
  1042. {
  1043. if (state >= CPUHP_AP_OFFLINE && state <= CPUHP_AP_ONLINE)
  1044. return true;
  1045. return state > CPUHP_BRINGUP_CPU;
  1046. }
  1047. static struct cpuhp_step *cpuhp_get_step(enum cpuhp_state state)
  1048. {
  1049. struct cpuhp_step *sp;
  1050. sp = cpuhp_is_ap_state(state) ? cpuhp_ap_states : cpuhp_bp_states;
  1051. return sp + state;
  1052. }
  1053. static void cpuhp_store_callbacks(enum cpuhp_state state,
  1054. const char *name,
  1055. int (*startup)(unsigned int cpu),
  1056. int (*teardown)(unsigned int cpu))
  1057. {
  1058. /* (Un)Install the callbacks for further cpu hotplug operations */
  1059. struct cpuhp_step *sp;
  1060. mutex_lock(&cpuhp_state_mutex);
  1061. sp = cpuhp_get_step(state);
  1062. sp->startup = startup;
  1063. sp->teardown = teardown;
  1064. sp->name = name;
  1065. mutex_unlock(&cpuhp_state_mutex);
  1066. }
  1067. static void *cpuhp_get_teardown_cb(enum cpuhp_state state)
  1068. {
  1069. return cpuhp_get_step(state)->teardown;
  1070. }
  1071. /*
  1072. * Call the startup/teardown function for a step either on the AP or
  1073. * on the current CPU.
  1074. */
  1075. static int cpuhp_issue_call(int cpu, enum cpuhp_state state,
  1076. int (*cb)(unsigned int), bool bringup)
  1077. {
  1078. int ret;
  1079. if (!cb)
  1080. return 0;
  1081. /*
  1082. * The non AP bound callbacks can fail on bringup. On teardown
  1083. * e.g. module removal we crash for now.
  1084. */
  1085. #ifdef CONFIG_SMP
  1086. if (cpuhp_is_ap_state(state))
  1087. ret = cpuhp_invoke_ap_callback(cpu, state, cb);
  1088. else
  1089. ret = cpuhp_invoke_callback(cpu, state, cb);
  1090. #else
  1091. ret = cpuhp_invoke_callback(cpu, state, cb);
  1092. #endif
  1093. BUG_ON(ret && !bringup);
  1094. return ret;
  1095. }
  1096. /*
  1097. * Called from __cpuhp_setup_state on a recoverable failure.
  1098. *
  1099. * Note: The teardown callbacks for rollback are not allowed to fail!
  1100. */
  1101. static void cpuhp_rollback_install(int failedcpu, enum cpuhp_state state,
  1102. int (*teardown)(unsigned int cpu))
  1103. {
  1104. int cpu;
  1105. if (!teardown)
  1106. return;
  1107. /* Roll back the already executed steps on the other cpus */
  1108. for_each_present_cpu(cpu) {
  1109. struct cpuhp_cpu_state *st = per_cpu_ptr(&cpuhp_state, cpu);
  1110. int cpustate = st->state;
  1111. if (cpu >= failedcpu)
  1112. break;
  1113. /* Did we invoke the startup call on that cpu ? */
  1114. if (cpustate >= state)
  1115. cpuhp_issue_call(cpu, state, teardown, false);
  1116. }
  1117. }
  1118. /*
  1119. * Returns a free for dynamic slot assignment of the Online state. The states
  1120. * are protected by the cpuhp_slot_states mutex and an empty slot is identified
  1121. * by having no name assigned.
  1122. */
  1123. static int cpuhp_reserve_state(enum cpuhp_state state)
  1124. {
  1125. enum cpuhp_state i;
  1126. mutex_lock(&cpuhp_state_mutex);
  1127. for (i = CPUHP_AP_ONLINE_DYN; i <= CPUHP_AP_ONLINE_DYN_END; i++) {
  1128. if (cpuhp_ap_states[i].name)
  1129. continue;
  1130. cpuhp_ap_states[i].name = "Reserved";
  1131. mutex_unlock(&cpuhp_state_mutex);
  1132. return i;
  1133. }
  1134. mutex_unlock(&cpuhp_state_mutex);
  1135. WARN(1, "No more dynamic states available for CPU hotplug\n");
  1136. return -ENOSPC;
  1137. }
  1138. /**
  1139. * __cpuhp_setup_state - Setup the callbacks for an hotplug machine state
  1140. * @state: The state to setup
  1141. * @invoke: If true, the startup function is invoked for cpus where
  1142. * cpu state >= @state
  1143. * @startup: startup callback function
  1144. * @teardown: teardown callback function
  1145. *
  1146. * Returns 0 if successful, otherwise a proper error code
  1147. */
  1148. int __cpuhp_setup_state(enum cpuhp_state state,
  1149. const char *name, bool invoke,
  1150. int (*startup)(unsigned int cpu),
  1151. int (*teardown)(unsigned int cpu))
  1152. {
  1153. int cpu, ret = 0;
  1154. int dyn_state = 0;
  1155. if (cpuhp_cb_check(state) || !name)
  1156. return -EINVAL;
  1157. get_online_cpus();
  1158. /* currently assignments for the ONLINE state are possible */
  1159. if (state == CPUHP_AP_ONLINE_DYN) {
  1160. dyn_state = 1;
  1161. ret = cpuhp_reserve_state(state);
  1162. if (ret < 0)
  1163. goto out;
  1164. state = ret;
  1165. }
  1166. cpuhp_store_callbacks(state, name, startup, teardown);
  1167. if (!invoke || !startup)
  1168. goto out;
  1169. /*
  1170. * Try to call the startup callback for each present cpu
  1171. * depending on the hotplug state of the cpu.
  1172. */
  1173. for_each_present_cpu(cpu) {
  1174. struct cpuhp_cpu_state *st = per_cpu_ptr(&cpuhp_state, cpu);
  1175. int cpustate = st->state;
  1176. if (cpustate < state)
  1177. continue;
  1178. ret = cpuhp_issue_call(cpu, state, startup, true);
  1179. if (ret) {
  1180. cpuhp_rollback_install(cpu, state, teardown);
  1181. cpuhp_store_callbacks(state, NULL, NULL, NULL);
  1182. goto out;
  1183. }
  1184. }
  1185. out:
  1186. put_online_cpus();
  1187. if (!ret && dyn_state)
  1188. return state;
  1189. return ret;
  1190. }
  1191. EXPORT_SYMBOL(__cpuhp_setup_state);
  1192. /**
  1193. * __cpuhp_remove_state - Remove the callbacks for an hotplug machine state
  1194. * @state: The state to remove
  1195. * @invoke: If true, the teardown function is invoked for cpus where
  1196. * cpu state >= @state
  1197. *
  1198. * The teardown callback is currently not allowed to fail. Think
  1199. * about module removal!
  1200. */
  1201. void __cpuhp_remove_state(enum cpuhp_state state, bool invoke)
  1202. {
  1203. int (*teardown)(unsigned int cpu) = cpuhp_get_teardown_cb(state);
  1204. int cpu;
  1205. BUG_ON(cpuhp_cb_check(state));
  1206. get_online_cpus();
  1207. if (!invoke || !teardown)
  1208. goto remove;
  1209. /*
  1210. * Call the teardown callback for each present cpu depending
  1211. * on the hotplug state of the cpu. This function is not
  1212. * allowed to fail currently!
  1213. */
  1214. for_each_present_cpu(cpu) {
  1215. struct cpuhp_cpu_state *st = per_cpu_ptr(&cpuhp_state, cpu);
  1216. int cpustate = st->state;
  1217. if (cpustate >= state)
  1218. cpuhp_issue_call(cpu, state, teardown, false);
  1219. }
  1220. remove:
  1221. cpuhp_store_callbacks(state, NULL, NULL, NULL);
  1222. put_online_cpus();
  1223. }
  1224. EXPORT_SYMBOL(__cpuhp_remove_state);
  1225. #if defined(CONFIG_SYSFS) && defined(CONFIG_HOTPLUG_CPU)
  1226. static ssize_t show_cpuhp_state(struct device *dev,
  1227. struct device_attribute *attr, char *buf)
  1228. {
  1229. struct cpuhp_cpu_state *st = per_cpu_ptr(&cpuhp_state, dev->id);
  1230. return sprintf(buf, "%d\n", st->state);
  1231. }
  1232. static DEVICE_ATTR(state, 0444, show_cpuhp_state, NULL);
  1233. static ssize_t write_cpuhp_target(struct device *dev,
  1234. struct device_attribute *attr,
  1235. const char *buf, size_t count)
  1236. {
  1237. struct cpuhp_cpu_state *st = per_cpu_ptr(&cpuhp_state, dev->id);
  1238. struct cpuhp_step *sp;
  1239. int target, ret;
  1240. ret = kstrtoint(buf, 10, &target);
  1241. if (ret)
  1242. return ret;
  1243. #ifdef CONFIG_CPU_HOTPLUG_STATE_CONTROL
  1244. if (target < CPUHP_OFFLINE || target > CPUHP_ONLINE)
  1245. return -EINVAL;
  1246. #else
  1247. if (target != CPUHP_OFFLINE && target != CPUHP_ONLINE)
  1248. return -EINVAL;
  1249. #endif
  1250. ret = lock_device_hotplug_sysfs();
  1251. if (ret)
  1252. return ret;
  1253. mutex_lock(&cpuhp_state_mutex);
  1254. sp = cpuhp_get_step(target);
  1255. ret = !sp->name || sp->cant_stop ? -EINVAL : 0;
  1256. mutex_unlock(&cpuhp_state_mutex);
  1257. if (ret)
  1258. return ret;
  1259. if (st->state < target)
  1260. ret = do_cpu_up(dev->id, target);
  1261. else
  1262. ret = do_cpu_down(dev->id, target);
  1263. unlock_device_hotplug();
  1264. return ret ? ret : count;
  1265. }
  1266. static ssize_t show_cpuhp_target(struct device *dev,
  1267. struct device_attribute *attr, char *buf)
  1268. {
  1269. struct cpuhp_cpu_state *st = per_cpu_ptr(&cpuhp_state, dev->id);
  1270. return sprintf(buf, "%d\n", st->target);
  1271. }
  1272. static DEVICE_ATTR(target, 0644, show_cpuhp_target, write_cpuhp_target);
  1273. static struct attribute *cpuhp_cpu_attrs[] = {
  1274. &dev_attr_state.attr,
  1275. &dev_attr_target.attr,
  1276. NULL
  1277. };
  1278. static struct attribute_group cpuhp_cpu_attr_group = {
  1279. .attrs = cpuhp_cpu_attrs,
  1280. .name = "hotplug",
  1281. NULL
  1282. };
  1283. static ssize_t show_cpuhp_states(struct device *dev,
  1284. struct device_attribute *attr, char *buf)
  1285. {
  1286. ssize_t cur, res = 0;
  1287. int i;
  1288. mutex_lock(&cpuhp_state_mutex);
  1289. for (i = CPUHP_OFFLINE; i <= CPUHP_ONLINE; i++) {
  1290. struct cpuhp_step *sp = cpuhp_get_step(i);
  1291. if (sp->name) {
  1292. cur = sprintf(buf, "%3d: %s\n", i, sp->name);
  1293. buf += cur;
  1294. res += cur;
  1295. }
  1296. }
  1297. mutex_unlock(&cpuhp_state_mutex);
  1298. return res;
  1299. }
  1300. static DEVICE_ATTR(states, 0444, show_cpuhp_states, NULL);
  1301. static struct attribute *cpuhp_cpu_root_attrs[] = {
  1302. &dev_attr_states.attr,
  1303. NULL
  1304. };
  1305. static struct attribute_group cpuhp_cpu_root_attr_group = {
  1306. .attrs = cpuhp_cpu_root_attrs,
  1307. .name = "hotplug",
  1308. NULL
  1309. };
  1310. static int __init cpuhp_sysfs_init(void)
  1311. {
  1312. int cpu, ret;
  1313. ret = sysfs_create_group(&cpu_subsys.dev_root->kobj,
  1314. &cpuhp_cpu_root_attr_group);
  1315. if (ret)
  1316. return ret;
  1317. for_each_possible_cpu(cpu) {
  1318. struct device *dev = get_cpu_device(cpu);
  1319. if (!dev)
  1320. continue;
  1321. ret = sysfs_create_group(&dev->kobj, &cpuhp_cpu_attr_group);
  1322. if (ret)
  1323. return ret;
  1324. }
  1325. return 0;
  1326. }
  1327. device_initcall(cpuhp_sysfs_init);
  1328. #endif
  1329. /*
  1330. * cpu_bit_bitmap[] is a special, "compressed" data structure that
  1331. * represents all NR_CPUS bits binary values of 1<<nr.
  1332. *
  1333. * It is used by cpumask_of() to get a constant address to a CPU
  1334. * mask value that has a single bit set only.
  1335. */
  1336. /* cpu_bit_bitmap[0] is empty - so we can back into it */
  1337. #define MASK_DECLARE_1(x) [x+1][0] = (1UL << (x))
  1338. #define MASK_DECLARE_2(x) MASK_DECLARE_1(x), MASK_DECLARE_1(x+1)
  1339. #define MASK_DECLARE_4(x) MASK_DECLARE_2(x), MASK_DECLARE_2(x+2)
  1340. #define MASK_DECLARE_8(x) MASK_DECLARE_4(x), MASK_DECLARE_4(x+4)
  1341. const unsigned long cpu_bit_bitmap[BITS_PER_LONG+1][BITS_TO_LONGS(NR_CPUS)] = {
  1342. MASK_DECLARE_8(0), MASK_DECLARE_8(8),
  1343. MASK_DECLARE_8(16), MASK_DECLARE_8(24),
  1344. #if BITS_PER_LONG > 32
  1345. MASK_DECLARE_8(32), MASK_DECLARE_8(40),
  1346. MASK_DECLARE_8(48), MASK_DECLARE_8(56),
  1347. #endif
  1348. };
  1349. EXPORT_SYMBOL_GPL(cpu_bit_bitmap);
  1350. const DECLARE_BITMAP(cpu_all_bits, NR_CPUS) = CPU_BITS_ALL;
  1351. EXPORT_SYMBOL(cpu_all_bits);
  1352. #ifdef CONFIG_INIT_ALL_POSSIBLE
  1353. struct cpumask __cpu_possible_mask __read_mostly
  1354. = {CPU_BITS_ALL};
  1355. #else
  1356. struct cpumask __cpu_possible_mask __read_mostly;
  1357. #endif
  1358. EXPORT_SYMBOL(__cpu_possible_mask);
  1359. struct cpumask __cpu_online_mask __read_mostly;
  1360. EXPORT_SYMBOL(__cpu_online_mask);
  1361. struct cpumask __cpu_present_mask __read_mostly;
  1362. EXPORT_SYMBOL(__cpu_present_mask);
  1363. struct cpumask __cpu_active_mask __read_mostly;
  1364. EXPORT_SYMBOL(__cpu_active_mask);
  1365. void init_cpu_present(const struct cpumask *src)
  1366. {
  1367. cpumask_copy(&__cpu_present_mask, src);
  1368. }
  1369. void init_cpu_possible(const struct cpumask *src)
  1370. {
  1371. cpumask_copy(&__cpu_possible_mask, src);
  1372. }
  1373. void init_cpu_online(const struct cpumask *src)
  1374. {
  1375. cpumask_copy(&__cpu_online_mask, src);
  1376. }
  1377. /*
  1378. * Activate the first processor.
  1379. */
  1380. void __init boot_cpu_init(void)
  1381. {
  1382. int cpu = smp_processor_id();
  1383. /* Mark the boot cpu "present", "online" etc for SMP and UP case */
  1384. set_cpu_online(cpu, true);
  1385. set_cpu_active(cpu, true);
  1386. set_cpu_present(cpu, true);
  1387. set_cpu_possible(cpu, true);
  1388. }
  1389. /*
  1390. * Must be called _AFTER_ setting up the per_cpu areas
  1391. */
  1392. void __init boot_cpu_state_init(void)
  1393. {
  1394. per_cpu_ptr(&cpuhp_state, smp_processor_id())->state = CPUHP_ONLINE;
  1395. }