cpu.c 44 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812
  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. bool hasdied = false;
  669. if (num_online_cpus() == 1)
  670. return -EBUSY;
  671. if (!cpu_present(cpu))
  672. return -EINVAL;
  673. cpu_hotplug_begin();
  674. cpuhp_tasks_frozen = tasks_frozen;
  675. prev_state = st->state;
  676. st->target = target;
  677. /*
  678. * If the current CPU state is in the range of the AP hotplug thread,
  679. * then we need to kick the thread.
  680. */
  681. if (st->state > CPUHP_TEARDOWN_CPU) {
  682. ret = cpuhp_kick_ap_work(cpu);
  683. /*
  684. * The AP side has done the error rollback already. Just
  685. * return the error code..
  686. */
  687. if (ret)
  688. goto out;
  689. /*
  690. * We might have stopped still in the range of the AP hotplug
  691. * thread. Nothing to do anymore.
  692. */
  693. if (st->state > CPUHP_TEARDOWN_CPU)
  694. goto out;
  695. }
  696. /*
  697. * The AP brought itself down to CPUHP_TEARDOWN_CPU. So we need
  698. * to do the further cleanups.
  699. */
  700. ret = cpuhp_down_callbacks(cpu, st, target);
  701. if (ret && st->state > CPUHP_TEARDOWN_CPU && st->state < prev_state) {
  702. st->target = prev_state;
  703. st->rollback = true;
  704. cpuhp_kick_ap_work(cpu);
  705. }
  706. hasdied = prev_state != st->state && st->state == CPUHP_OFFLINE;
  707. out:
  708. cpu_hotplug_done();
  709. return ret;
  710. }
  711. static int do_cpu_down(unsigned int cpu, enum cpuhp_state target)
  712. {
  713. int err;
  714. cpu_maps_update_begin();
  715. if (cpu_hotplug_disabled) {
  716. err = -EBUSY;
  717. goto out;
  718. }
  719. err = _cpu_down(cpu, 0, target);
  720. out:
  721. cpu_maps_update_done();
  722. return err;
  723. }
  724. int cpu_down(unsigned int cpu)
  725. {
  726. return do_cpu_down(cpu, CPUHP_OFFLINE);
  727. }
  728. EXPORT_SYMBOL(cpu_down);
  729. #endif /*CONFIG_HOTPLUG_CPU*/
  730. /**
  731. * notify_cpu_starting(cpu) - Invoke the callbacks on the starting CPU
  732. * @cpu: cpu that just started
  733. *
  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. rcu_cpu_starting(cpu); /* Enables RCU usage on this CPU. */
  742. while (st->state < target) {
  743. st->state++;
  744. cpuhp_invoke_callback(cpu, st->state, true, NULL);
  745. }
  746. }
  747. /*
  748. * Called from the idle task. We need to set active here, so we can kick off
  749. * the stopper thread and unpark the smpboot threads. If the target state is
  750. * beyond CPUHP_AP_ONLINE_IDLE we kick cpuhp thread and let it bring up the
  751. * cpu further.
  752. */
  753. void cpuhp_online_idle(enum cpuhp_state state)
  754. {
  755. struct cpuhp_cpu_state *st = this_cpu_ptr(&cpuhp_state);
  756. unsigned int cpu = smp_processor_id();
  757. /* Happens for the boot cpu */
  758. if (state != CPUHP_AP_ONLINE_IDLE)
  759. return;
  760. st->state = CPUHP_AP_ONLINE_IDLE;
  761. /* Unpark the stopper thread and the hotplug thread of this cpu */
  762. stop_machine_unpark(cpu);
  763. kthread_unpark(st->thread);
  764. /* Should we go further up ? */
  765. if (st->target > CPUHP_AP_ONLINE_IDLE)
  766. __cpuhp_kick_ap_work(st);
  767. else
  768. complete(&st->done);
  769. }
  770. /* Requires cpu_add_remove_lock to be held */
  771. static int _cpu_up(unsigned int cpu, int tasks_frozen, enum cpuhp_state target)
  772. {
  773. struct cpuhp_cpu_state *st = per_cpu_ptr(&cpuhp_state, cpu);
  774. struct task_struct *idle;
  775. int ret = 0;
  776. cpu_hotplug_begin();
  777. if (!cpu_present(cpu)) {
  778. ret = -EINVAL;
  779. goto out;
  780. }
  781. /*
  782. * The caller of do_cpu_up might have raced with another
  783. * caller. Ignore it for now.
  784. */
  785. if (st->state >= target)
  786. goto out;
  787. if (st->state == CPUHP_OFFLINE) {
  788. /* Let it fail before we try to bring the cpu up */
  789. idle = idle_thread_get(cpu);
  790. if (IS_ERR(idle)) {
  791. ret = PTR_ERR(idle);
  792. goto out;
  793. }
  794. }
  795. cpuhp_tasks_frozen = tasks_frozen;
  796. st->target = target;
  797. /*
  798. * If the current CPU state is in the range of the AP hotplug thread,
  799. * then we need to kick the thread once more.
  800. */
  801. if (st->state > CPUHP_BRINGUP_CPU) {
  802. ret = cpuhp_kick_ap_work(cpu);
  803. /*
  804. * The AP side has done the error rollback already. Just
  805. * return the error code..
  806. */
  807. if (ret)
  808. goto out;
  809. }
  810. /*
  811. * Try to reach the target state. We max out on the BP at
  812. * CPUHP_BRINGUP_CPU. After that the AP hotplug thread is
  813. * responsible for bringing it up to the target state.
  814. */
  815. target = min((int)target, CPUHP_BRINGUP_CPU);
  816. ret = cpuhp_up_callbacks(cpu, st, target);
  817. out:
  818. cpu_hotplug_done();
  819. return ret;
  820. }
  821. static int do_cpu_up(unsigned int cpu, enum cpuhp_state target)
  822. {
  823. int err = 0;
  824. if (!cpu_possible(cpu)) {
  825. pr_err("can't online cpu %d because it is not configured as may-hotadd at boot time\n",
  826. cpu);
  827. #if defined(CONFIG_IA64)
  828. pr_err("please check additional_cpus= boot parameter\n");
  829. #endif
  830. return -EINVAL;
  831. }
  832. err = try_online_node(cpu_to_node(cpu));
  833. if (err)
  834. return err;
  835. cpu_maps_update_begin();
  836. if (cpu_hotplug_disabled) {
  837. err = -EBUSY;
  838. goto out;
  839. }
  840. err = _cpu_up(cpu, 0, target);
  841. out:
  842. cpu_maps_update_done();
  843. return err;
  844. }
  845. int cpu_up(unsigned int cpu)
  846. {
  847. return do_cpu_up(cpu, CPUHP_ONLINE);
  848. }
  849. EXPORT_SYMBOL_GPL(cpu_up);
  850. #ifdef CONFIG_PM_SLEEP_SMP
  851. static cpumask_var_t frozen_cpus;
  852. int freeze_secondary_cpus(int primary)
  853. {
  854. int cpu, error = 0;
  855. cpu_maps_update_begin();
  856. if (!cpu_online(primary))
  857. primary = cpumask_first(cpu_online_mask);
  858. /*
  859. * We take down all of the non-boot CPUs in one shot to avoid races
  860. * with the userspace trying to use the CPU hotplug at the same time
  861. */
  862. cpumask_clear(frozen_cpus);
  863. pr_info("Disabling non-boot CPUs ...\n");
  864. for_each_online_cpu(cpu) {
  865. if (cpu == primary)
  866. continue;
  867. trace_suspend_resume(TPS("CPU_OFF"), cpu, true);
  868. error = _cpu_down(cpu, 1, CPUHP_OFFLINE);
  869. trace_suspend_resume(TPS("CPU_OFF"), cpu, false);
  870. if (!error)
  871. cpumask_set_cpu(cpu, frozen_cpus);
  872. else {
  873. pr_err("Error taking CPU%d down: %d\n", cpu, error);
  874. break;
  875. }
  876. }
  877. if (!error)
  878. BUG_ON(num_online_cpus() > 1);
  879. else
  880. pr_err("Non-boot CPUs are not disabled\n");
  881. /*
  882. * Make sure the CPUs won't be enabled by someone else. We need to do
  883. * this even in case of failure as all disable_nonboot_cpus() users are
  884. * supposed to do enable_nonboot_cpus() on the failure path.
  885. */
  886. cpu_hotplug_disabled++;
  887. cpu_maps_update_done();
  888. return error;
  889. }
  890. void __weak arch_enable_nonboot_cpus_begin(void)
  891. {
  892. }
  893. void __weak arch_enable_nonboot_cpus_end(void)
  894. {
  895. }
  896. void enable_nonboot_cpus(void)
  897. {
  898. int cpu, error;
  899. /* Allow everyone to use the CPU hotplug again */
  900. cpu_maps_update_begin();
  901. __cpu_hotplug_enable();
  902. if (cpumask_empty(frozen_cpus))
  903. goto out;
  904. pr_info("Enabling non-boot CPUs ...\n");
  905. arch_enable_nonboot_cpus_begin();
  906. for_each_cpu(cpu, frozen_cpus) {
  907. trace_suspend_resume(TPS("CPU_ON"), cpu, true);
  908. error = _cpu_up(cpu, 1, CPUHP_ONLINE);
  909. trace_suspend_resume(TPS("CPU_ON"), cpu, false);
  910. if (!error) {
  911. pr_info("CPU%d is up\n", cpu);
  912. continue;
  913. }
  914. pr_warn("Error taking CPU%d up: %d\n", cpu, error);
  915. }
  916. arch_enable_nonboot_cpus_end();
  917. cpumask_clear(frozen_cpus);
  918. out:
  919. cpu_maps_update_done();
  920. }
  921. static int __init alloc_frozen_cpus(void)
  922. {
  923. if (!alloc_cpumask_var(&frozen_cpus, GFP_KERNEL|__GFP_ZERO))
  924. return -ENOMEM;
  925. return 0;
  926. }
  927. core_initcall(alloc_frozen_cpus);
  928. /*
  929. * When callbacks for CPU hotplug notifications are being executed, we must
  930. * ensure that the state of the system with respect to the tasks being frozen
  931. * or not, as reported by the notification, remains unchanged *throughout the
  932. * duration* of the execution of the callbacks.
  933. * Hence we need to prevent the freezer from racing with regular CPU hotplug.
  934. *
  935. * This synchronization is implemented by mutually excluding regular CPU
  936. * hotplug and Suspend/Hibernate call paths by hooking onto the Suspend/
  937. * Hibernate notifications.
  938. */
  939. static int
  940. cpu_hotplug_pm_callback(struct notifier_block *nb,
  941. unsigned long action, void *ptr)
  942. {
  943. switch (action) {
  944. case PM_SUSPEND_PREPARE:
  945. case PM_HIBERNATION_PREPARE:
  946. cpu_hotplug_disable();
  947. break;
  948. case PM_POST_SUSPEND:
  949. case PM_POST_HIBERNATION:
  950. cpu_hotplug_enable();
  951. break;
  952. default:
  953. return NOTIFY_DONE;
  954. }
  955. return NOTIFY_OK;
  956. }
  957. static int __init cpu_hotplug_pm_sync_init(void)
  958. {
  959. /*
  960. * cpu_hotplug_pm_callback has higher priority than x86
  961. * bsp_pm_callback which depends on cpu_hotplug_pm_callback
  962. * to disable cpu hotplug to avoid cpu hotplug race.
  963. */
  964. pm_notifier(cpu_hotplug_pm_callback, 0);
  965. return 0;
  966. }
  967. core_initcall(cpu_hotplug_pm_sync_init);
  968. #endif /* CONFIG_PM_SLEEP_SMP */
  969. #endif /* CONFIG_SMP */
  970. /* Boot processor state steps */
  971. static struct cpuhp_step cpuhp_bp_states[] = {
  972. [CPUHP_OFFLINE] = {
  973. .name = "offline",
  974. .startup.single = NULL,
  975. .teardown.single = NULL,
  976. },
  977. #ifdef CONFIG_SMP
  978. [CPUHP_CREATE_THREADS]= {
  979. .name = "threads:prepare",
  980. .startup.single = smpboot_create_threads,
  981. .teardown.single = NULL,
  982. .cant_stop = true,
  983. },
  984. [CPUHP_PERF_PREPARE] = {
  985. .name = "perf:prepare",
  986. .startup.single = perf_event_init_cpu,
  987. .teardown.single = perf_event_exit_cpu,
  988. },
  989. [CPUHP_WORKQUEUE_PREP] = {
  990. .name = "workqueue:prepare",
  991. .startup.single = workqueue_prepare_cpu,
  992. .teardown.single = NULL,
  993. },
  994. [CPUHP_HRTIMERS_PREPARE] = {
  995. .name = "hrtimers:prepare",
  996. .startup.single = hrtimers_prepare_cpu,
  997. .teardown.single = hrtimers_dead_cpu,
  998. },
  999. [CPUHP_SMPCFD_PREPARE] = {
  1000. .name = "smpcfd:prepare",
  1001. .startup.single = smpcfd_prepare_cpu,
  1002. .teardown.single = smpcfd_dead_cpu,
  1003. },
  1004. [CPUHP_RELAY_PREPARE] = {
  1005. .name = "relay:prepare",
  1006. .startup.single = relay_prepare_cpu,
  1007. .teardown.single = NULL,
  1008. },
  1009. [CPUHP_SLAB_PREPARE] = {
  1010. .name = "slab:prepare",
  1011. .startup.single = slab_prepare_cpu,
  1012. .teardown.single = slab_dead_cpu,
  1013. },
  1014. [CPUHP_RCUTREE_PREP] = {
  1015. .name = "RCU/tree:prepare",
  1016. .startup.single = rcutree_prepare_cpu,
  1017. .teardown.single = rcutree_dead_cpu,
  1018. },
  1019. /*
  1020. * On the tear-down path, timers_dead_cpu() must be invoked
  1021. * before blk_mq_queue_reinit_notify() from notify_dead(),
  1022. * otherwise a RCU stall occurs.
  1023. */
  1024. [CPUHP_TIMERS_DEAD] = {
  1025. .name = "timers:dead",
  1026. .startup.single = NULL,
  1027. .teardown.single = timers_dead_cpu,
  1028. },
  1029. /* Kicks the plugged cpu into life */
  1030. [CPUHP_BRINGUP_CPU] = {
  1031. .name = "cpu:bringup",
  1032. .startup.single = bringup_cpu,
  1033. .teardown.single = NULL,
  1034. .cant_stop = true,
  1035. },
  1036. [CPUHP_AP_SMPCFD_DYING] = {
  1037. .name = "smpcfd:dying",
  1038. .startup.single = NULL,
  1039. .teardown.single = smpcfd_dying_cpu,
  1040. },
  1041. /*
  1042. * Handled on controll processor until the plugged processor manages
  1043. * this itself.
  1044. */
  1045. [CPUHP_TEARDOWN_CPU] = {
  1046. .name = "cpu:teardown",
  1047. .startup.single = NULL,
  1048. .teardown.single = takedown_cpu,
  1049. .cant_stop = true,
  1050. },
  1051. #else
  1052. [CPUHP_BRINGUP_CPU] = { },
  1053. #endif
  1054. };
  1055. /* Application processor state steps */
  1056. static struct cpuhp_step cpuhp_ap_states[] = {
  1057. #ifdef CONFIG_SMP
  1058. /* Final state before CPU kills itself */
  1059. [CPUHP_AP_IDLE_DEAD] = {
  1060. .name = "idle:dead",
  1061. },
  1062. /*
  1063. * Last state before CPU enters the idle loop to die. Transient state
  1064. * for synchronization.
  1065. */
  1066. [CPUHP_AP_OFFLINE] = {
  1067. .name = "ap:offline",
  1068. .cant_stop = true,
  1069. },
  1070. /* First state is scheduler control. Interrupts are disabled */
  1071. [CPUHP_AP_SCHED_STARTING] = {
  1072. .name = "sched:starting",
  1073. .startup.single = sched_cpu_starting,
  1074. .teardown.single = sched_cpu_dying,
  1075. },
  1076. [CPUHP_AP_RCUTREE_DYING] = {
  1077. .name = "RCU/tree:dying",
  1078. .startup.single = NULL,
  1079. .teardown.single = rcutree_dying_cpu,
  1080. },
  1081. /* Entry state on starting. Interrupts enabled from here on. Transient
  1082. * state for synchronsization */
  1083. [CPUHP_AP_ONLINE] = {
  1084. .name = "ap:online",
  1085. },
  1086. /* Handle smpboot threads park/unpark */
  1087. [CPUHP_AP_SMPBOOT_THREADS] = {
  1088. .name = "smpboot/threads:online",
  1089. .startup.single = smpboot_unpark_threads,
  1090. .teardown.single = NULL,
  1091. },
  1092. [CPUHP_AP_PERF_ONLINE] = {
  1093. .name = "perf:online",
  1094. .startup.single = perf_event_init_cpu,
  1095. .teardown.single = perf_event_exit_cpu,
  1096. },
  1097. [CPUHP_AP_WORKQUEUE_ONLINE] = {
  1098. .name = "workqueue:online",
  1099. .startup.single = workqueue_online_cpu,
  1100. .teardown.single = workqueue_offline_cpu,
  1101. },
  1102. [CPUHP_AP_RCUTREE_ONLINE] = {
  1103. .name = "RCU/tree:online",
  1104. .startup.single = rcutree_online_cpu,
  1105. .teardown.single = rcutree_offline_cpu,
  1106. },
  1107. #endif
  1108. /*
  1109. * The dynamically registered state space is here
  1110. */
  1111. #ifdef CONFIG_SMP
  1112. /* Last state is scheduler control setting the cpu active */
  1113. [CPUHP_AP_ACTIVE] = {
  1114. .name = "sched:active",
  1115. .startup.single = sched_cpu_activate,
  1116. .teardown.single = sched_cpu_deactivate,
  1117. },
  1118. #endif
  1119. /* CPU is fully up and running. */
  1120. [CPUHP_ONLINE] = {
  1121. .name = "online",
  1122. .startup.single = NULL,
  1123. .teardown.single = NULL,
  1124. },
  1125. };
  1126. /* Sanity check for callbacks */
  1127. static int cpuhp_cb_check(enum cpuhp_state state)
  1128. {
  1129. if (state <= CPUHP_OFFLINE || state >= CPUHP_ONLINE)
  1130. return -EINVAL;
  1131. return 0;
  1132. }
  1133. /*
  1134. * Returns a free for dynamic slot assignment of the Online state. The states
  1135. * are protected by the cpuhp_slot_states mutex and an empty slot is identified
  1136. * by having no name assigned.
  1137. */
  1138. static int cpuhp_reserve_state(enum cpuhp_state state)
  1139. {
  1140. enum cpuhp_state i;
  1141. for (i = CPUHP_AP_ONLINE_DYN; i <= CPUHP_AP_ONLINE_DYN_END; i++) {
  1142. if (!cpuhp_ap_states[i].name)
  1143. return i;
  1144. }
  1145. WARN(1, "No more dynamic states available for CPU hotplug\n");
  1146. return -ENOSPC;
  1147. }
  1148. static int cpuhp_store_callbacks(enum cpuhp_state state, const char *name,
  1149. int (*startup)(unsigned int cpu),
  1150. int (*teardown)(unsigned int cpu),
  1151. bool multi_instance)
  1152. {
  1153. /* (Un)Install the callbacks for further cpu hotplug operations */
  1154. struct cpuhp_step *sp;
  1155. int ret = 0;
  1156. mutex_lock(&cpuhp_state_mutex);
  1157. if (state == CPUHP_AP_ONLINE_DYN) {
  1158. ret = cpuhp_reserve_state(state);
  1159. if (ret < 0)
  1160. goto out;
  1161. state = ret;
  1162. }
  1163. sp = cpuhp_get_step(state);
  1164. if (name && sp->name) {
  1165. ret = -EBUSY;
  1166. goto out;
  1167. }
  1168. sp->startup.single = startup;
  1169. sp->teardown.single = teardown;
  1170. sp->name = name;
  1171. sp->multi_instance = multi_instance;
  1172. INIT_HLIST_HEAD(&sp->list);
  1173. out:
  1174. mutex_unlock(&cpuhp_state_mutex);
  1175. return ret;
  1176. }
  1177. static void *cpuhp_get_teardown_cb(enum cpuhp_state state)
  1178. {
  1179. return cpuhp_get_step(state)->teardown.single;
  1180. }
  1181. /*
  1182. * Call the startup/teardown function for a step either on the AP or
  1183. * on the current CPU.
  1184. */
  1185. static int cpuhp_issue_call(int cpu, enum cpuhp_state state, bool bringup,
  1186. struct hlist_node *node)
  1187. {
  1188. struct cpuhp_step *sp = cpuhp_get_step(state);
  1189. int ret;
  1190. if ((bringup && !sp->startup.single) ||
  1191. (!bringup && !sp->teardown.single))
  1192. return 0;
  1193. /*
  1194. * The non AP bound callbacks can fail on bringup. On teardown
  1195. * e.g. module removal we crash for now.
  1196. */
  1197. #ifdef CONFIG_SMP
  1198. if (cpuhp_is_ap_state(state))
  1199. ret = cpuhp_invoke_ap_callback(cpu, state, bringup, node);
  1200. else
  1201. ret = cpuhp_invoke_callback(cpu, state, bringup, node);
  1202. #else
  1203. ret = cpuhp_invoke_callback(cpu, state, bringup, node);
  1204. #endif
  1205. BUG_ON(ret && !bringup);
  1206. return ret;
  1207. }
  1208. /*
  1209. * Called from __cpuhp_setup_state on a recoverable failure.
  1210. *
  1211. * Note: The teardown callbacks for rollback are not allowed to fail!
  1212. */
  1213. static void cpuhp_rollback_install(int failedcpu, enum cpuhp_state state,
  1214. struct hlist_node *node)
  1215. {
  1216. int cpu;
  1217. /* Roll back the already executed steps on the other cpus */
  1218. for_each_present_cpu(cpu) {
  1219. struct cpuhp_cpu_state *st = per_cpu_ptr(&cpuhp_state, cpu);
  1220. int cpustate = st->state;
  1221. if (cpu >= failedcpu)
  1222. break;
  1223. /* Did we invoke the startup call on that cpu ? */
  1224. if (cpustate >= state)
  1225. cpuhp_issue_call(cpu, state, false, node);
  1226. }
  1227. }
  1228. int __cpuhp_state_add_instance(enum cpuhp_state state, struct hlist_node *node,
  1229. bool invoke)
  1230. {
  1231. struct cpuhp_step *sp;
  1232. int cpu;
  1233. int ret;
  1234. sp = cpuhp_get_step(state);
  1235. if (sp->multi_instance == false)
  1236. return -EINVAL;
  1237. get_online_cpus();
  1238. if (!invoke || !sp->startup.multi)
  1239. goto add_node;
  1240. /*
  1241. * Try to call the startup callback for each present cpu
  1242. * depending on the hotplug state of the cpu.
  1243. */
  1244. for_each_present_cpu(cpu) {
  1245. struct cpuhp_cpu_state *st = per_cpu_ptr(&cpuhp_state, cpu);
  1246. int cpustate = st->state;
  1247. if (cpustate < state)
  1248. continue;
  1249. ret = cpuhp_issue_call(cpu, state, true, node);
  1250. if (ret) {
  1251. if (sp->teardown.multi)
  1252. cpuhp_rollback_install(cpu, state, node);
  1253. goto err;
  1254. }
  1255. }
  1256. add_node:
  1257. ret = 0;
  1258. mutex_lock(&cpuhp_state_mutex);
  1259. hlist_add_head(node, &sp->list);
  1260. mutex_unlock(&cpuhp_state_mutex);
  1261. err:
  1262. put_online_cpus();
  1263. return ret;
  1264. }
  1265. EXPORT_SYMBOL_GPL(__cpuhp_state_add_instance);
  1266. /**
  1267. * __cpuhp_setup_state - Setup the callbacks for an hotplug machine state
  1268. * @state: The state to setup
  1269. * @invoke: If true, the startup function is invoked for cpus where
  1270. * cpu state >= @state
  1271. * @startup: startup callback function
  1272. * @teardown: teardown callback function
  1273. * @multi_instance: State is set up for multiple instances which get
  1274. * added afterwards.
  1275. *
  1276. * Returns:
  1277. * On success:
  1278. * Positive state number if @state is CPUHP_AP_ONLINE_DYN
  1279. * 0 for all other states
  1280. * On failure: proper (negative) error code
  1281. */
  1282. int __cpuhp_setup_state(enum cpuhp_state state,
  1283. const char *name, bool invoke,
  1284. int (*startup)(unsigned int cpu),
  1285. int (*teardown)(unsigned int cpu),
  1286. bool multi_instance)
  1287. {
  1288. int cpu, ret = 0;
  1289. bool dynstate;
  1290. if (cpuhp_cb_check(state) || !name)
  1291. return -EINVAL;
  1292. get_online_cpus();
  1293. ret = cpuhp_store_callbacks(state, name, startup, teardown,
  1294. multi_instance);
  1295. dynstate = state == CPUHP_AP_ONLINE_DYN;
  1296. if (ret > 0 && dynstate) {
  1297. state = ret;
  1298. ret = 0;
  1299. }
  1300. if (ret || !invoke || !startup)
  1301. goto out;
  1302. /*
  1303. * Try to call the startup callback for each present cpu
  1304. * depending on the hotplug state of the cpu.
  1305. */
  1306. for_each_present_cpu(cpu) {
  1307. struct cpuhp_cpu_state *st = per_cpu_ptr(&cpuhp_state, cpu);
  1308. int cpustate = st->state;
  1309. if (cpustate < state)
  1310. continue;
  1311. ret = cpuhp_issue_call(cpu, state, true, NULL);
  1312. if (ret) {
  1313. if (teardown)
  1314. cpuhp_rollback_install(cpu, state, NULL);
  1315. cpuhp_store_callbacks(state, NULL, NULL, NULL, false);
  1316. goto out;
  1317. }
  1318. }
  1319. out:
  1320. put_online_cpus();
  1321. /*
  1322. * If the requested state is CPUHP_AP_ONLINE_DYN, return the
  1323. * dynamically allocated state in case of success.
  1324. */
  1325. if (!ret && dynstate)
  1326. return state;
  1327. return ret;
  1328. }
  1329. EXPORT_SYMBOL(__cpuhp_setup_state);
  1330. int __cpuhp_state_remove_instance(enum cpuhp_state state,
  1331. struct hlist_node *node, bool invoke)
  1332. {
  1333. struct cpuhp_step *sp = cpuhp_get_step(state);
  1334. int cpu;
  1335. BUG_ON(cpuhp_cb_check(state));
  1336. if (!sp->multi_instance)
  1337. return -EINVAL;
  1338. get_online_cpus();
  1339. if (!invoke || !cpuhp_get_teardown_cb(state))
  1340. goto remove;
  1341. /*
  1342. * Call the teardown callback for each present cpu depending
  1343. * on the hotplug state of the cpu. This function is not
  1344. * allowed to fail currently!
  1345. */
  1346. for_each_present_cpu(cpu) {
  1347. struct cpuhp_cpu_state *st = per_cpu_ptr(&cpuhp_state, cpu);
  1348. int cpustate = st->state;
  1349. if (cpustate >= state)
  1350. cpuhp_issue_call(cpu, state, false, node);
  1351. }
  1352. remove:
  1353. mutex_lock(&cpuhp_state_mutex);
  1354. hlist_del(node);
  1355. mutex_unlock(&cpuhp_state_mutex);
  1356. put_online_cpus();
  1357. return 0;
  1358. }
  1359. EXPORT_SYMBOL_GPL(__cpuhp_state_remove_instance);
  1360. /**
  1361. * __cpuhp_remove_state - Remove the callbacks for an hotplug machine state
  1362. * @state: The state to remove
  1363. * @invoke: If true, the teardown function is invoked for cpus where
  1364. * cpu state >= @state
  1365. *
  1366. * The teardown callback is currently not allowed to fail. Think
  1367. * about module removal!
  1368. */
  1369. void __cpuhp_remove_state(enum cpuhp_state state, bool invoke)
  1370. {
  1371. struct cpuhp_step *sp = cpuhp_get_step(state);
  1372. int cpu;
  1373. BUG_ON(cpuhp_cb_check(state));
  1374. get_online_cpus();
  1375. if (sp->multi_instance) {
  1376. WARN(!hlist_empty(&sp->list),
  1377. "Error: Removing state %d which has instances left.\n",
  1378. state);
  1379. goto remove;
  1380. }
  1381. if (!invoke || !cpuhp_get_teardown_cb(state))
  1382. goto remove;
  1383. /*
  1384. * Call the teardown callback for each present cpu depending
  1385. * on the hotplug state of the cpu. This function is not
  1386. * allowed to fail currently!
  1387. */
  1388. for_each_present_cpu(cpu) {
  1389. struct cpuhp_cpu_state *st = per_cpu_ptr(&cpuhp_state, cpu);
  1390. int cpustate = st->state;
  1391. if (cpustate >= state)
  1392. cpuhp_issue_call(cpu, state, false, NULL);
  1393. }
  1394. remove:
  1395. cpuhp_store_callbacks(state, NULL, NULL, NULL, false);
  1396. put_online_cpus();
  1397. }
  1398. EXPORT_SYMBOL(__cpuhp_remove_state);
  1399. #if defined(CONFIG_SYSFS) && defined(CONFIG_HOTPLUG_CPU)
  1400. static ssize_t show_cpuhp_state(struct device *dev,
  1401. struct device_attribute *attr, char *buf)
  1402. {
  1403. struct cpuhp_cpu_state *st = per_cpu_ptr(&cpuhp_state, dev->id);
  1404. return sprintf(buf, "%d\n", st->state);
  1405. }
  1406. static DEVICE_ATTR(state, 0444, show_cpuhp_state, NULL);
  1407. static ssize_t write_cpuhp_target(struct device *dev,
  1408. struct device_attribute *attr,
  1409. const char *buf, size_t count)
  1410. {
  1411. struct cpuhp_cpu_state *st = per_cpu_ptr(&cpuhp_state, dev->id);
  1412. struct cpuhp_step *sp;
  1413. int target, ret;
  1414. ret = kstrtoint(buf, 10, &target);
  1415. if (ret)
  1416. return ret;
  1417. #ifdef CONFIG_CPU_HOTPLUG_STATE_CONTROL
  1418. if (target < CPUHP_OFFLINE || target > CPUHP_ONLINE)
  1419. return -EINVAL;
  1420. #else
  1421. if (target != CPUHP_OFFLINE && target != CPUHP_ONLINE)
  1422. return -EINVAL;
  1423. #endif
  1424. ret = lock_device_hotplug_sysfs();
  1425. if (ret)
  1426. return ret;
  1427. mutex_lock(&cpuhp_state_mutex);
  1428. sp = cpuhp_get_step(target);
  1429. ret = !sp->name || sp->cant_stop ? -EINVAL : 0;
  1430. mutex_unlock(&cpuhp_state_mutex);
  1431. if (ret)
  1432. return ret;
  1433. if (st->state < target)
  1434. ret = do_cpu_up(dev->id, target);
  1435. else
  1436. ret = do_cpu_down(dev->id, target);
  1437. unlock_device_hotplug();
  1438. return ret ? ret : count;
  1439. }
  1440. static ssize_t show_cpuhp_target(struct device *dev,
  1441. struct device_attribute *attr, char *buf)
  1442. {
  1443. struct cpuhp_cpu_state *st = per_cpu_ptr(&cpuhp_state, dev->id);
  1444. return sprintf(buf, "%d\n", st->target);
  1445. }
  1446. static DEVICE_ATTR(target, 0644, show_cpuhp_target, write_cpuhp_target);
  1447. static struct attribute *cpuhp_cpu_attrs[] = {
  1448. &dev_attr_state.attr,
  1449. &dev_attr_target.attr,
  1450. NULL
  1451. };
  1452. static struct attribute_group cpuhp_cpu_attr_group = {
  1453. .attrs = cpuhp_cpu_attrs,
  1454. .name = "hotplug",
  1455. NULL
  1456. };
  1457. static ssize_t show_cpuhp_states(struct device *dev,
  1458. struct device_attribute *attr, char *buf)
  1459. {
  1460. ssize_t cur, res = 0;
  1461. int i;
  1462. mutex_lock(&cpuhp_state_mutex);
  1463. for (i = CPUHP_OFFLINE; i <= CPUHP_ONLINE; i++) {
  1464. struct cpuhp_step *sp = cpuhp_get_step(i);
  1465. if (sp->name) {
  1466. cur = sprintf(buf, "%3d: %s\n", i, sp->name);
  1467. buf += cur;
  1468. res += cur;
  1469. }
  1470. }
  1471. mutex_unlock(&cpuhp_state_mutex);
  1472. return res;
  1473. }
  1474. static DEVICE_ATTR(states, 0444, show_cpuhp_states, NULL);
  1475. static struct attribute *cpuhp_cpu_root_attrs[] = {
  1476. &dev_attr_states.attr,
  1477. NULL
  1478. };
  1479. static struct attribute_group cpuhp_cpu_root_attr_group = {
  1480. .attrs = cpuhp_cpu_root_attrs,
  1481. .name = "hotplug",
  1482. NULL
  1483. };
  1484. static int __init cpuhp_sysfs_init(void)
  1485. {
  1486. int cpu, ret;
  1487. ret = sysfs_create_group(&cpu_subsys.dev_root->kobj,
  1488. &cpuhp_cpu_root_attr_group);
  1489. if (ret)
  1490. return ret;
  1491. for_each_possible_cpu(cpu) {
  1492. struct device *dev = get_cpu_device(cpu);
  1493. if (!dev)
  1494. continue;
  1495. ret = sysfs_create_group(&dev->kobj, &cpuhp_cpu_attr_group);
  1496. if (ret)
  1497. return ret;
  1498. }
  1499. return 0;
  1500. }
  1501. device_initcall(cpuhp_sysfs_init);
  1502. #endif
  1503. /*
  1504. * cpu_bit_bitmap[] is a special, "compressed" data structure that
  1505. * represents all NR_CPUS bits binary values of 1<<nr.
  1506. *
  1507. * It is used by cpumask_of() to get a constant address to a CPU
  1508. * mask value that has a single bit set only.
  1509. */
  1510. /* cpu_bit_bitmap[0] is empty - so we can back into it */
  1511. #define MASK_DECLARE_1(x) [x+1][0] = (1UL << (x))
  1512. #define MASK_DECLARE_2(x) MASK_DECLARE_1(x), MASK_DECLARE_1(x+1)
  1513. #define MASK_DECLARE_4(x) MASK_DECLARE_2(x), MASK_DECLARE_2(x+2)
  1514. #define MASK_DECLARE_8(x) MASK_DECLARE_4(x), MASK_DECLARE_4(x+4)
  1515. const unsigned long cpu_bit_bitmap[BITS_PER_LONG+1][BITS_TO_LONGS(NR_CPUS)] = {
  1516. MASK_DECLARE_8(0), MASK_DECLARE_8(8),
  1517. MASK_DECLARE_8(16), MASK_DECLARE_8(24),
  1518. #if BITS_PER_LONG > 32
  1519. MASK_DECLARE_8(32), MASK_DECLARE_8(40),
  1520. MASK_DECLARE_8(48), MASK_DECLARE_8(56),
  1521. #endif
  1522. };
  1523. EXPORT_SYMBOL_GPL(cpu_bit_bitmap);
  1524. const DECLARE_BITMAP(cpu_all_bits, NR_CPUS) = CPU_BITS_ALL;
  1525. EXPORT_SYMBOL(cpu_all_bits);
  1526. #ifdef CONFIG_INIT_ALL_POSSIBLE
  1527. struct cpumask __cpu_possible_mask __read_mostly
  1528. = {CPU_BITS_ALL};
  1529. #else
  1530. struct cpumask __cpu_possible_mask __read_mostly;
  1531. #endif
  1532. EXPORT_SYMBOL(__cpu_possible_mask);
  1533. struct cpumask __cpu_online_mask __read_mostly;
  1534. EXPORT_SYMBOL(__cpu_online_mask);
  1535. struct cpumask __cpu_present_mask __read_mostly;
  1536. EXPORT_SYMBOL(__cpu_present_mask);
  1537. struct cpumask __cpu_active_mask __read_mostly;
  1538. EXPORT_SYMBOL(__cpu_active_mask);
  1539. void init_cpu_present(const struct cpumask *src)
  1540. {
  1541. cpumask_copy(&__cpu_present_mask, src);
  1542. }
  1543. void init_cpu_possible(const struct cpumask *src)
  1544. {
  1545. cpumask_copy(&__cpu_possible_mask, src);
  1546. }
  1547. void init_cpu_online(const struct cpumask *src)
  1548. {
  1549. cpumask_copy(&__cpu_online_mask, src);
  1550. }
  1551. /*
  1552. * Activate the first processor.
  1553. */
  1554. void __init boot_cpu_init(void)
  1555. {
  1556. int cpu = smp_processor_id();
  1557. /* Mark the boot cpu "present", "online" etc for SMP and UP case */
  1558. set_cpu_online(cpu, true);
  1559. set_cpu_active(cpu, true);
  1560. set_cpu_present(cpu, true);
  1561. set_cpu_possible(cpu, true);
  1562. }
  1563. /*
  1564. * Must be called _AFTER_ setting up the per_cpu areas
  1565. */
  1566. void __init boot_cpu_state_init(void)
  1567. {
  1568. per_cpu_ptr(&cpuhp_state, smp_processor_id())->state = CPUHP_ONLINE;
  1569. }