cpu.c 44 KB

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