cpu.c 55 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283
  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/signal.h>
  11. #include <linux/sched/hotplug.h>
  12. #include <linux/sched/task.h>
  13. #include <linux/unistd.h>
  14. #include <linux/cpu.h>
  15. #include <linux/oom.h>
  16. #include <linux/rcupdate.h>
  17. #include <linux/export.h>
  18. #include <linux/bug.h>
  19. #include <linux/kthread.h>
  20. #include <linux/stop_machine.h>
  21. #include <linux/mutex.h>
  22. #include <linux/gfp.h>
  23. #include <linux/suspend.h>
  24. #include <linux/lockdep.h>
  25. #include <linux/tick.h>
  26. #include <linux/irq.h>
  27. #include <linux/nmi.h>
  28. #include <linux/smpboot.h>
  29. #include <linux/relay.h>
  30. #include <linux/slab.h>
  31. #include <linux/percpu-rwsem.h>
  32. #include <trace/events/power.h>
  33. #define CREATE_TRACE_POINTS
  34. #include <trace/events/cpuhp.h>
  35. #include "smpboot.h"
  36. /**
  37. * cpuhp_cpu_state - Per cpu hotplug state storage
  38. * @state: The current cpu state
  39. * @target: The target state
  40. * @thread: Pointer to the hotplug thread
  41. * @should_run: Thread should execute
  42. * @rollback: Perform a rollback
  43. * @single: Single callback invocation
  44. * @bringup: Single callback bringup or teardown selector
  45. * @cb_state: The state for a single callback (install/uninstall)
  46. * @result: Result of the operation
  47. * @done_up: Signal completion to the issuer of the task for cpu-up
  48. * @done_down: Signal completion to the issuer of the task for cpu-down
  49. */
  50. struct cpuhp_cpu_state {
  51. enum cpuhp_state state;
  52. enum cpuhp_state target;
  53. enum cpuhp_state fail;
  54. #ifdef CONFIG_SMP
  55. struct task_struct *thread;
  56. bool should_run;
  57. bool rollback;
  58. bool single;
  59. bool bringup;
  60. bool booted_once;
  61. struct hlist_node *node;
  62. struct hlist_node *last;
  63. enum cpuhp_state cb_state;
  64. int result;
  65. struct completion done_up;
  66. struct completion done_down;
  67. #endif
  68. };
  69. static DEFINE_PER_CPU(struct cpuhp_cpu_state, cpuhp_state) = {
  70. .fail = CPUHP_INVALID,
  71. };
  72. #if defined(CONFIG_LOCKDEP) && defined(CONFIG_SMP)
  73. static struct lockdep_map cpuhp_state_up_map =
  74. STATIC_LOCKDEP_MAP_INIT("cpuhp_state-up", &cpuhp_state_up_map);
  75. static struct lockdep_map cpuhp_state_down_map =
  76. STATIC_LOCKDEP_MAP_INIT("cpuhp_state-down", &cpuhp_state_down_map);
  77. static inline void cpuhp_lock_acquire(bool bringup)
  78. {
  79. lock_map_acquire(bringup ? &cpuhp_state_up_map : &cpuhp_state_down_map);
  80. }
  81. static inline void cpuhp_lock_release(bool bringup)
  82. {
  83. lock_map_release(bringup ? &cpuhp_state_up_map : &cpuhp_state_down_map);
  84. }
  85. #else
  86. static inline void cpuhp_lock_acquire(bool bringup) { }
  87. static inline void cpuhp_lock_release(bool bringup) { }
  88. #endif
  89. /**
  90. * cpuhp_step - Hotplug state machine step
  91. * @name: Name of the step
  92. * @startup: Startup function of the step
  93. * @teardown: Teardown function of the step
  94. * @skip_onerr: Do not invoke the functions on error rollback
  95. * Will go away once the notifiers are gone
  96. * @cant_stop: Bringup/teardown can't be stopped at this step
  97. */
  98. struct cpuhp_step {
  99. const char *name;
  100. union {
  101. int (*single)(unsigned int cpu);
  102. int (*multi)(unsigned int cpu,
  103. struct hlist_node *node);
  104. } startup;
  105. union {
  106. int (*single)(unsigned int cpu);
  107. int (*multi)(unsigned int cpu,
  108. struct hlist_node *node);
  109. } teardown;
  110. struct hlist_head list;
  111. bool skip_onerr;
  112. bool cant_stop;
  113. bool multi_instance;
  114. };
  115. static DEFINE_MUTEX(cpuhp_state_mutex);
  116. static struct cpuhp_step cpuhp_hp_states[];
  117. static struct cpuhp_step *cpuhp_get_step(enum cpuhp_state state)
  118. {
  119. return cpuhp_hp_states + state;
  120. }
  121. /**
  122. * cpuhp_invoke_callback _ Invoke the callbacks for a given state
  123. * @cpu: The cpu for which the callback should be invoked
  124. * @state: The state to do callbacks for
  125. * @bringup: True if the bringup callback should be invoked
  126. * @node: For multi-instance, do a single entry callback for install/remove
  127. * @lastp: For multi-instance rollback, remember how far we got
  128. *
  129. * Called from cpu hotplug and from the state register machinery.
  130. */
  131. static int cpuhp_invoke_callback(unsigned int cpu, enum cpuhp_state state,
  132. bool bringup, struct hlist_node *node,
  133. struct hlist_node **lastp)
  134. {
  135. struct cpuhp_cpu_state *st = per_cpu_ptr(&cpuhp_state, cpu);
  136. struct cpuhp_step *step = cpuhp_get_step(state);
  137. int (*cbm)(unsigned int cpu, struct hlist_node *node);
  138. int (*cb)(unsigned int cpu);
  139. int ret, cnt;
  140. if (st->fail == state) {
  141. st->fail = CPUHP_INVALID;
  142. if (!(bringup ? step->startup.single : step->teardown.single))
  143. return 0;
  144. return -EAGAIN;
  145. }
  146. if (!step->multi_instance) {
  147. WARN_ON_ONCE(lastp && *lastp);
  148. cb = bringup ? step->startup.single : step->teardown.single;
  149. if (!cb)
  150. return 0;
  151. trace_cpuhp_enter(cpu, st->target, state, cb);
  152. ret = cb(cpu);
  153. trace_cpuhp_exit(cpu, st->state, state, ret);
  154. return ret;
  155. }
  156. cbm = bringup ? step->startup.multi : step->teardown.multi;
  157. if (!cbm)
  158. return 0;
  159. /* Single invocation for instance add/remove */
  160. if (node) {
  161. WARN_ON_ONCE(lastp && *lastp);
  162. trace_cpuhp_multi_enter(cpu, st->target, state, cbm, node);
  163. ret = cbm(cpu, node);
  164. trace_cpuhp_exit(cpu, st->state, state, ret);
  165. return ret;
  166. }
  167. /* State transition. Invoke on all instances */
  168. cnt = 0;
  169. hlist_for_each(node, &step->list) {
  170. if (lastp && node == *lastp)
  171. break;
  172. trace_cpuhp_multi_enter(cpu, st->target, state, cbm, node);
  173. ret = cbm(cpu, node);
  174. trace_cpuhp_exit(cpu, st->state, state, ret);
  175. if (ret) {
  176. if (!lastp)
  177. goto err;
  178. *lastp = node;
  179. return ret;
  180. }
  181. cnt++;
  182. }
  183. if (lastp)
  184. *lastp = NULL;
  185. return 0;
  186. err:
  187. /* Rollback the instances if one failed */
  188. cbm = !bringup ? step->startup.multi : step->teardown.multi;
  189. if (!cbm)
  190. return ret;
  191. hlist_for_each(node, &step->list) {
  192. if (!cnt--)
  193. break;
  194. trace_cpuhp_multi_enter(cpu, st->target, state, cbm, node);
  195. ret = cbm(cpu, node);
  196. trace_cpuhp_exit(cpu, st->state, state, ret);
  197. /*
  198. * Rollback must not fail,
  199. */
  200. WARN_ON_ONCE(ret);
  201. }
  202. return ret;
  203. }
  204. #ifdef CONFIG_SMP
  205. static bool cpuhp_is_ap_state(enum cpuhp_state state)
  206. {
  207. /*
  208. * The extra check for CPUHP_TEARDOWN_CPU is only for documentation
  209. * purposes as that state is handled explicitly in cpu_down.
  210. */
  211. return state > CPUHP_BRINGUP_CPU && state != CPUHP_TEARDOWN_CPU;
  212. }
  213. static inline void wait_for_ap_thread(struct cpuhp_cpu_state *st, bool bringup)
  214. {
  215. struct completion *done = bringup ? &st->done_up : &st->done_down;
  216. wait_for_completion(done);
  217. }
  218. static inline void complete_ap_thread(struct cpuhp_cpu_state *st, bool bringup)
  219. {
  220. struct completion *done = bringup ? &st->done_up : &st->done_down;
  221. complete(done);
  222. }
  223. /*
  224. * The former STARTING/DYING states, ran with IRQs disabled and must not fail.
  225. */
  226. static bool cpuhp_is_atomic_state(enum cpuhp_state state)
  227. {
  228. return CPUHP_AP_IDLE_DEAD <= state && state < CPUHP_AP_ONLINE;
  229. }
  230. /* Serializes the updates to cpu_online_mask, cpu_present_mask */
  231. static DEFINE_MUTEX(cpu_add_remove_lock);
  232. bool cpuhp_tasks_frozen;
  233. EXPORT_SYMBOL_GPL(cpuhp_tasks_frozen);
  234. /*
  235. * The following two APIs (cpu_maps_update_begin/done) must be used when
  236. * attempting to serialize the updates to cpu_online_mask & cpu_present_mask.
  237. */
  238. void cpu_maps_update_begin(void)
  239. {
  240. mutex_lock(&cpu_add_remove_lock);
  241. }
  242. void cpu_maps_update_done(void)
  243. {
  244. mutex_unlock(&cpu_add_remove_lock);
  245. }
  246. /*
  247. * If set, cpu_up and cpu_down will return -EBUSY and do nothing.
  248. * Should always be manipulated under cpu_add_remove_lock
  249. */
  250. static int cpu_hotplug_disabled;
  251. #ifdef CONFIG_HOTPLUG_CPU
  252. DEFINE_STATIC_PERCPU_RWSEM(cpu_hotplug_lock);
  253. void cpus_read_lock(void)
  254. {
  255. percpu_down_read(&cpu_hotplug_lock);
  256. }
  257. EXPORT_SYMBOL_GPL(cpus_read_lock);
  258. int cpus_read_trylock(void)
  259. {
  260. return percpu_down_read_trylock(&cpu_hotplug_lock);
  261. }
  262. EXPORT_SYMBOL_GPL(cpus_read_trylock);
  263. void cpus_read_unlock(void)
  264. {
  265. percpu_up_read(&cpu_hotplug_lock);
  266. }
  267. EXPORT_SYMBOL_GPL(cpus_read_unlock);
  268. void cpus_write_lock(void)
  269. {
  270. percpu_down_write(&cpu_hotplug_lock);
  271. }
  272. void cpus_write_unlock(void)
  273. {
  274. percpu_up_write(&cpu_hotplug_lock);
  275. }
  276. void lockdep_assert_cpus_held(void)
  277. {
  278. percpu_rwsem_assert_held(&cpu_hotplug_lock);
  279. }
  280. /*
  281. * Wait for currently running CPU hotplug operations to complete (if any) and
  282. * disable future CPU hotplug (from sysfs). The 'cpu_add_remove_lock' protects
  283. * the 'cpu_hotplug_disabled' flag. The same lock is also acquired by the
  284. * hotplug path before performing hotplug operations. So acquiring that lock
  285. * guarantees mutual exclusion from any currently running hotplug operations.
  286. */
  287. void cpu_hotplug_disable(void)
  288. {
  289. cpu_maps_update_begin();
  290. cpu_hotplug_disabled++;
  291. cpu_maps_update_done();
  292. }
  293. EXPORT_SYMBOL_GPL(cpu_hotplug_disable);
  294. static void __cpu_hotplug_enable(void)
  295. {
  296. if (WARN_ONCE(!cpu_hotplug_disabled, "Unbalanced cpu hotplug enable\n"))
  297. return;
  298. cpu_hotplug_disabled--;
  299. }
  300. void cpu_hotplug_enable(void)
  301. {
  302. cpu_maps_update_begin();
  303. __cpu_hotplug_enable();
  304. cpu_maps_update_done();
  305. }
  306. EXPORT_SYMBOL_GPL(cpu_hotplug_enable);
  307. #endif /* CONFIG_HOTPLUG_CPU */
  308. #ifdef CONFIG_HOTPLUG_SMT
  309. enum cpuhp_smt_control cpu_smt_control __read_mostly = CPU_SMT_ENABLED;
  310. EXPORT_SYMBOL_GPL(cpu_smt_control);
  311. static bool cpu_smt_available __read_mostly;
  312. void __init cpu_smt_disable(bool force)
  313. {
  314. if (cpu_smt_control == CPU_SMT_FORCE_DISABLED ||
  315. cpu_smt_control == CPU_SMT_NOT_SUPPORTED)
  316. return;
  317. if (force) {
  318. pr_info("SMT: Force disabled\n");
  319. cpu_smt_control = CPU_SMT_FORCE_DISABLED;
  320. } else {
  321. cpu_smt_control = CPU_SMT_DISABLED;
  322. }
  323. }
  324. /*
  325. * The decision whether SMT is supported can only be done after the full
  326. * CPU identification. Called from architecture code before non boot CPUs
  327. * are brought up.
  328. */
  329. void __init cpu_smt_check_topology_early(void)
  330. {
  331. if (!topology_smt_supported())
  332. cpu_smt_control = CPU_SMT_NOT_SUPPORTED;
  333. }
  334. /*
  335. * If SMT was disabled by BIOS, detect it here, after the CPUs have been
  336. * brought online. This ensures the smt/l1tf sysfs entries are consistent
  337. * with reality. cpu_smt_available is set to true during the bringup of non
  338. * boot CPUs when a SMT sibling is detected. Note, this may overwrite
  339. * cpu_smt_control's previous setting.
  340. */
  341. void __init cpu_smt_check_topology(void)
  342. {
  343. if (!cpu_smt_available)
  344. cpu_smt_control = CPU_SMT_NOT_SUPPORTED;
  345. }
  346. static int __init smt_cmdline_disable(char *str)
  347. {
  348. cpu_smt_disable(str && !strcmp(str, "force"));
  349. return 0;
  350. }
  351. early_param("nosmt", smt_cmdline_disable);
  352. static inline bool cpu_smt_allowed(unsigned int cpu)
  353. {
  354. if (topology_is_primary_thread(cpu))
  355. return true;
  356. /*
  357. * If the CPU is not a 'primary' thread and the booted_once bit is
  358. * set then the processor has SMT support. Store this information
  359. * for the late check of SMT support in cpu_smt_check_topology().
  360. */
  361. if (per_cpu(cpuhp_state, cpu).booted_once)
  362. cpu_smt_available = true;
  363. if (cpu_smt_control == CPU_SMT_ENABLED)
  364. return true;
  365. /*
  366. * On x86 it's required to boot all logical CPUs at least once so
  367. * that the init code can get a chance to set CR4.MCE on each
  368. * CPU. Otherwise, a broadacasted MCE observing CR4.MCE=0b on any
  369. * core will shutdown the machine.
  370. */
  371. return !per_cpu(cpuhp_state, cpu).booted_once;
  372. }
  373. #else
  374. static inline bool cpu_smt_allowed(unsigned int cpu) { return true; }
  375. #endif
  376. static inline enum cpuhp_state
  377. cpuhp_set_state(struct cpuhp_cpu_state *st, enum cpuhp_state target)
  378. {
  379. enum cpuhp_state prev_state = st->state;
  380. st->rollback = false;
  381. st->last = NULL;
  382. st->target = target;
  383. st->single = false;
  384. st->bringup = st->state < target;
  385. return prev_state;
  386. }
  387. static inline void
  388. cpuhp_reset_state(struct cpuhp_cpu_state *st, enum cpuhp_state prev_state)
  389. {
  390. st->rollback = true;
  391. /*
  392. * If we have st->last we need to undo partial multi_instance of this
  393. * state first. Otherwise start undo at the previous state.
  394. */
  395. if (!st->last) {
  396. if (st->bringup)
  397. st->state--;
  398. else
  399. st->state++;
  400. }
  401. st->target = prev_state;
  402. st->bringup = !st->bringup;
  403. }
  404. /* Regular hotplug invocation of the AP hotplug thread */
  405. static void __cpuhp_kick_ap(struct cpuhp_cpu_state *st)
  406. {
  407. if (!st->single && st->state == st->target)
  408. return;
  409. st->result = 0;
  410. /*
  411. * Make sure the above stores are visible before should_run becomes
  412. * true. Paired with the mb() above in cpuhp_thread_fun()
  413. */
  414. smp_mb();
  415. st->should_run = true;
  416. wake_up_process(st->thread);
  417. wait_for_ap_thread(st, st->bringup);
  418. }
  419. static int cpuhp_kick_ap(struct cpuhp_cpu_state *st, enum cpuhp_state target)
  420. {
  421. enum cpuhp_state prev_state;
  422. int ret;
  423. prev_state = cpuhp_set_state(st, target);
  424. __cpuhp_kick_ap(st);
  425. if ((ret = st->result)) {
  426. cpuhp_reset_state(st, prev_state);
  427. __cpuhp_kick_ap(st);
  428. }
  429. return ret;
  430. }
  431. static int bringup_wait_for_ap(unsigned int cpu)
  432. {
  433. struct cpuhp_cpu_state *st = per_cpu_ptr(&cpuhp_state, cpu);
  434. /* Wait for the CPU to reach CPUHP_AP_ONLINE_IDLE */
  435. wait_for_ap_thread(st, true);
  436. if (WARN_ON_ONCE((!cpu_online(cpu))))
  437. return -ECANCELED;
  438. /* Unpark the stopper thread and the hotplug thread of the target cpu */
  439. stop_machine_unpark(cpu);
  440. kthread_unpark(st->thread);
  441. /*
  442. * SMT soft disabling on X86 requires to bring the CPU out of the
  443. * BIOS 'wait for SIPI' state in order to set the CR4.MCE bit. The
  444. * CPU marked itself as booted_once in cpu_notify_starting() so the
  445. * cpu_smt_allowed() check will now return false if this is not the
  446. * primary sibling.
  447. */
  448. if (!cpu_smt_allowed(cpu))
  449. return -ECANCELED;
  450. if (st->target <= CPUHP_AP_ONLINE_IDLE)
  451. return 0;
  452. return cpuhp_kick_ap(st, st->target);
  453. }
  454. static int bringup_cpu(unsigned int cpu)
  455. {
  456. struct task_struct *idle = idle_thread_get(cpu);
  457. int ret;
  458. /*
  459. * Some architectures have to walk the irq descriptors to
  460. * setup the vector space for the cpu which comes online.
  461. * Prevent irq alloc/free across the bringup.
  462. */
  463. irq_lock_sparse();
  464. /* Arch-specific enabling code. */
  465. ret = __cpu_up(cpu, idle);
  466. irq_unlock_sparse();
  467. if (ret)
  468. return ret;
  469. return bringup_wait_for_ap(cpu);
  470. }
  471. /*
  472. * Hotplug state machine related functions
  473. */
  474. static void undo_cpu_up(unsigned int cpu, struct cpuhp_cpu_state *st)
  475. {
  476. for (st->state--; st->state > st->target; st->state--) {
  477. struct cpuhp_step *step = cpuhp_get_step(st->state);
  478. if (!step->skip_onerr)
  479. cpuhp_invoke_callback(cpu, st->state, false, NULL, NULL);
  480. }
  481. }
  482. static int cpuhp_up_callbacks(unsigned int cpu, struct cpuhp_cpu_state *st,
  483. enum cpuhp_state target)
  484. {
  485. enum cpuhp_state prev_state = st->state;
  486. int ret = 0;
  487. while (st->state < target) {
  488. st->state++;
  489. ret = cpuhp_invoke_callback(cpu, st->state, true, NULL, NULL);
  490. if (ret) {
  491. st->target = prev_state;
  492. undo_cpu_up(cpu, st);
  493. break;
  494. }
  495. }
  496. return ret;
  497. }
  498. /*
  499. * The cpu hotplug threads manage the bringup and teardown of the cpus
  500. */
  501. static void cpuhp_create(unsigned int cpu)
  502. {
  503. struct cpuhp_cpu_state *st = per_cpu_ptr(&cpuhp_state, cpu);
  504. init_completion(&st->done_up);
  505. init_completion(&st->done_down);
  506. }
  507. static int cpuhp_should_run(unsigned int cpu)
  508. {
  509. struct cpuhp_cpu_state *st = this_cpu_ptr(&cpuhp_state);
  510. return st->should_run;
  511. }
  512. /*
  513. * Execute teardown/startup callbacks on the plugged cpu. Also used to invoke
  514. * callbacks when a state gets [un]installed at runtime.
  515. *
  516. * Each invocation of this function by the smpboot thread does a single AP
  517. * state callback.
  518. *
  519. * It has 3 modes of operation:
  520. * - single: runs st->cb_state
  521. * - up: runs ++st->state, while st->state < st->target
  522. * - down: runs st->state--, while st->state > st->target
  523. *
  524. * When complete or on error, should_run is cleared and the completion is fired.
  525. */
  526. static void cpuhp_thread_fun(unsigned int cpu)
  527. {
  528. struct cpuhp_cpu_state *st = this_cpu_ptr(&cpuhp_state);
  529. bool bringup = st->bringup;
  530. enum cpuhp_state state;
  531. /*
  532. * ACQUIRE for the cpuhp_should_run() load of ->should_run. Ensures
  533. * that if we see ->should_run we also see the rest of the state.
  534. */
  535. smp_mb();
  536. if (WARN_ON_ONCE(!st->should_run))
  537. return;
  538. cpuhp_lock_acquire(bringup);
  539. if (st->single) {
  540. state = st->cb_state;
  541. st->should_run = false;
  542. } else {
  543. if (bringup) {
  544. st->state++;
  545. state = st->state;
  546. st->should_run = (st->state < st->target);
  547. WARN_ON_ONCE(st->state > st->target);
  548. } else {
  549. state = st->state;
  550. st->state--;
  551. st->should_run = (st->state > st->target);
  552. WARN_ON_ONCE(st->state < st->target);
  553. }
  554. }
  555. WARN_ON_ONCE(!cpuhp_is_ap_state(state));
  556. if (st->rollback) {
  557. struct cpuhp_step *step = cpuhp_get_step(state);
  558. if (step->skip_onerr)
  559. goto next;
  560. }
  561. if (cpuhp_is_atomic_state(state)) {
  562. local_irq_disable();
  563. st->result = cpuhp_invoke_callback(cpu, state, bringup, st->node, &st->last);
  564. local_irq_enable();
  565. /*
  566. * STARTING/DYING must not fail!
  567. */
  568. WARN_ON_ONCE(st->result);
  569. } else {
  570. st->result = cpuhp_invoke_callback(cpu, state, bringup, st->node, &st->last);
  571. }
  572. if (st->result) {
  573. /*
  574. * If we fail on a rollback, we're up a creek without no
  575. * paddle, no way forward, no way back. We loose, thanks for
  576. * playing.
  577. */
  578. WARN_ON_ONCE(st->rollback);
  579. st->should_run = false;
  580. }
  581. next:
  582. cpuhp_lock_release(bringup);
  583. if (!st->should_run)
  584. complete_ap_thread(st, bringup);
  585. }
  586. /* Invoke a single callback on a remote cpu */
  587. static int
  588. cpuhp_invoke_ap_callback(int cpu, enum cpuhp_state state, bool bringup,
  589. struct hlist_node *node)
  590. {
  591. struct cpuhp_cpu_state *st = per_cpu_ptr(&cpuhp_state, cpu);
  592. int ret;
  593. if (!cpu_online(cpu))
  594. return 0;
  595. cpuhp_lock_acquire(false);
  596. cpuhp_lock_release(false);
  597. cpuhp_lock_acquire(true);
  598. cpuhp_lock_release(true);
  599. /*
  600. * If we are up and running, use the hotplug thread. For early calls
  601. * we invoke the thread function directly.
  602. */
  603. if (!st->thread)
  604. return cpuhp_invoke_callback(cpu, state, bringup, node, NULL);
  605. st->rollback = false;
  606. st->last = NULL;
  607. st->node = node;
  608. st->bringup = bringup;
  609. st->cb_state = state;
  610. st->single = true;
  611. __cpuhp_kick_ap(st);
  612. /*
  613. * If we failed and did a partial, do a rollback.
  614. */
  615. if ((ret = st->result) && st->last) {
  616. st->rollback = true;
  617. st->bringup = !bringup;
  618. __cpuhp_kick_ap(st);
  619. }
  620. /*
  621. * Clean up the leftovers so the next hotplug operation wont use stale
  622. * data.
  623. */
  624. st->node = st->last = NULL;
  625. return ret;
  626. }
  627. static int cpuhp_kick_ap_work(unsigned int cpu)
  628. {
  629. struct cpuhp_cpu_state *st = per_cpu_ptr(&cpuhp_state, cpu);
  630. enum cpuhp_state prev_state = st->state;
  631. int ret;
  632. cpuhp_lock_acquire(false);
  633. cpuhp_lock_release(false);
  634. cpuhp_lock_acquire(true);
  635. cpuhp_lock_release(true);
  636. trace_cpuhp_enter(cpu, st->target, prev_state, cpuhp_kick_ap_work);
  637. ret = cpuhp_kick_ap(st, st->target);
  638. trace_cpuhp_exit(cpu, st->state, prev_state, ret);
  639. return ret;
  640. }
  641. static struct smp_hotplug_thread cpuhp_threads = {
  642. .store = &cpuhp_state.thread,
  643. .create = &cpuhp_create,
  644. .thread_should_run = cpuhp_should_run,
  645. .thread_fn = cpuhp_thread_fun,
  646. .thread_comm = "cpuhp/%u",
  647. .selfparking = true,
  648. };
  649. void __init cpuhp_threads_init(void)
  650. {
  651. BUG_ON(smpboot_register_percpu_thread(&cpuhp_threads));
  652. kthread_unpark(this_cpu_read(cpuhp_state.thread));
  653. }
  654. #ifdef CONFIG_HOTPLUG_CPU
  655. /**
  656. * clear_tasks_mm_cpumask - Safely clear tasks' mm_cpumask for a CPU
  657. * @cpu: a CPU id
  658. *
  659. * This function walks all processes, finds a valid mm struct for each one and
  660. * then clears a corresponding bit in mm's cpumask. While this all sounds
  661. * trivial, there are various non-obvious corner cases, which this function
  662. * tries to solve in a safe manner.
  663. *
  664. * Also note that the function uses a somewhat relaxed locking scheme, so it may
  665. * be called only for an already offlined CPU.
  666. */
  667. void clear_tasks_mm_cpumask(int cpu)
  668. {
  669. struct task_struct *p;
  670. /*
  671. * This function is called after the cpu is taken down and marked
  672. * offline, so its not like new tasks will ever get this cpu set in
  673. * their mm mask. -- Peter Zijlstra
  674. * Thus, we may use rcu_read_lock() here, instead of grabbing
  675. * full-fledged tasklist_lock.
  676. */
  677. WARN_ON(cpu_online(cpu));
  678. rcu_read_lock();
  679. for_each_process(p) {
  680. struct task_struct *t;
  681. /*
  682. * Main thread might exit, but other threads may still have
  683. * a valid mm. Find one.
  684. */
  685. t = find_lock_task_mm(p);
  686. if (!t)
  687. continue;
  688. cpumask_clear_cpu(cpu, mm_cpumask(t->mm));
  689. task_unlock(t);
  690. }
  691. rcu_read_unlock();
  692. }
  693. /* Take this CPU down. */
  694. static int take_cpu_down(void *_param)
  695. {
  696. struct cpuhp_cpu_state *st = this_cpu_ptr(&cpuhp_state);
  697. enum cpuhp_state target = max((int)st->target, CPUHP_AP_OFFLINE);
  698. int err, cpu = smp_processor_id();
  699. int ret;
  700. /* Ensure this CPU doesn't handle any more interrupts. */
  701. err = __cpu_disable();
  702. if (err < 0)
  703. return err;
  704. /*
  705. * We get here while we are in CPUHP_TEARDOWN_CPU state and we must not
  706. * do this step again.
  707. */
  708. WARN_ON(st->state != CPUHP_TEARDOWN_CPU);
  709. st->state--;
  710. /* Invoke the former CPU_DYING callbacks */
  711. for (; st->state > target; st->state--) {
  712. ret = cpuhp_invoke_callback(cpu, st->state, false, NULL, NULL);
  713. /*
  714. * DYING must not fail!
  715. */
  716. WARN_ON_ONCE(ret);
  717. }
  718. /* Give up timekeeping duties */
  719. tick_handover_do_timer();
  720. /* Park the stopper thread */
  721. stop_machine_park(cpu);
  722. return 0;
  723. }
  724. static int takedown_cpu(unsigned int cpu)
  725. {
  726. struct cpuhp_cpu_state *st = per_cpu_ptr(&cpuhp_state, cpu);
  727. int err;
  728. /* Park the smpboot threads */
  729. kthread_park(per_cpu_ptr(&cpuhp_state, cpu)->thread);
  730. /*
  731. * Prevent irq alloc/free while the dying cpu reorganizes the
  732. * interrupt affinities.
  733. */
  734. irq_lock_sparse();
  735. /*
  736. * So now all preempt/rcu users must observe !cpu_active().
  737. */
  738. err = stop_machine_cpuslocked(take_cpu_down, NULL, cpumask_of(cpu));
  739. if (err) {
  740. /* CPU refused to die */
  741. irq_unlock_sparse();
  742. /* Unpark the hotplug thread so we can rollback there */
  743. kthread_unpark(per_cpu_ptr(&cpuhp_state, cpu)->thread);
  744. return err;
  745. }
  746. BUG_ON(cpu_online(cpu));
  747. /*
  748. * The teardown callback for CPUHP_AP_SCHED_STARTING will have removed
  749. * all runnable tasks from the CPU, there's only the idle task left now
  750. * that the migration thread is done doing the stop_machine thing.
  751. *
  752. * Wait for the stop thread to go away.
  753. */
  754. wait_for_ap_thread(st, false);
  755. BUG_ON(st->state != CPUHP_AP_IDLE_DEAD);
  756. /* Interrupts are moved away from the dying cpu, reenable alloc/free */
  757. irq_unlock_sparse();
  758. hotplug_cpu__broadcast_tick_pull(cpu);
  759. /* This actually kills the CPU. */
  760. __cpu_die(cpu);
  761. tick_cleanup_dead_cpu(cpu);
  762. rcutree_migrate_callbacks(cpu);
  763. return 0;
  764. }
  765. static void cpuhp_complete_idle_dead(void *arg)
  766. {
  767. struct cpuhp_cpu_state *st = arg;
  768. complete_ap_thread(st, false);
  769. }
  770. void cpuhp_report_idle_dead(void)
  771. {
  772. struct cpuhp_cpu_state *st = this_cpu_ptr(&cpuhp_state);
  773. BUG_ON(st->state != CPUHP_AP_OFFLINE);
  774. rcu_report_dead(smp_processor_id());
  775. st->state = CPUHP_AP_IDLE_DEAD;
  776. /*
  777. * We cannot call complete after rcu_report_dead() so we delegate it
  778. * to an online cpu.
  779. */
  780. smp_call_function_single(cpumask_first(cpu_online_mask),
  781. cpuhp_complete_idle_dead, st, 0);
  782. }
  783. static void undo_cpu_down(unsigned int cpu, struct cpuhp_cpu_state *st)
  784. {
  785. for (st->state++; st->state < st->target; st->state++) {
  786. struct cpuhp_step *step = cpuhp_get_step(st->state);
  787. if (!step->skip_onerr)
  788. cpuhp_invoke_callback(cpu, st->state, true, NULL, NULL);
  789. }
  790. }
  791. static int cpuhp_down_callbacks(unsigned int cpu, struct cpuhp_cpu_state *st,
  792. enum cpuhp_state target)
  793. {
  794. enum cpuhp_state prev_state = st->state;
  795. int ret = 0;
  796. for (; st->state > target; st->state--) {
  797. ret = cpuhp_invoke_callback(cpu, st->state, false, NULL, NULL);
  798. if (ret) {
  799. st->target = prev_state;
  800. undo_cpu_down(cpu, st);
  801. break;
  802. }
  803. }
  804. return ret;
  805. }
  806. /* Requires cpu_add_remove_lock to be held */
  807. static int __ref _cpu_down(unsigned int cpu, int tasks_frozen,
  808. enum cpuhp_state target)
  809. {
  810. struct cpuhp_cpu_state *st = per_cpu_ptr(&cpuhp_state, cpu);
  811. int prev_state, ret = 0;
  812. if (num_online_cpus() == 1)
  813. return -EBUSY;
  814. if (!cpu_present(cpu))
  815. return -EINVAL;
  816. cpus_write_lock();
  817. cpuhp_tasks_frozen = tasks_frozen;
  818. prev_state = cpuhp_set_state(st, target);
  819. /*
  820. * If the current CPU state is in the range of the AP hotplug thread,
  821. * then we need to kick the thread.
  822. */
  823. if (st->state > CPUHP_TEARDOWN_CPU) {
  824. st->target = max((int)target, CPUHP_TEARDOWN_CPU);
  825. ret = cpuhp_kick_ap_work(cpu);
  826. /*
  827. * The AP side has done the error rollback already. Just
  828. * return the error code..
  829. */
  830. if (ret)
  831. goto out;
  832. /*
  833. * We might have stopped still in the range of the AP hotplug
  834. * thread. Nothing to do anymore.
  835. */
  836. if (st->state > CPUHP_TEARDOWN_CPU)
  837. goto out;
  838. st->target = target;
  839. }
  840. /*
  841. * The AP brought itself down to CPUHP_TEARDOWN_CPU. So we need
  842. * to do the further cleanups.
  843. */
  844. ret = cpuhp_down_callbacks(cpu, st, target);
  845. if (ret && st->state > CPUHP_TEARDOWN_CPU && st->state < prev_state) {
  846. cpuhp_reset_state(st, prev_state);
  847. __cpuhp_kick_ap(st);
  848. }
  849. out:
  850. cpus_write_unlock();
  851. /*
  852. * Do post unplug cleanup. This is still protected against
  853. * concurrent CPU hotplug via cpu_add_remove_lock.
  854. */
  855. lockup_detector_cleanup();
  856. return ret;
  857. }
  858. static int cpu_down_maps_locked(unsigned int cpu, enum cpuhp_state target)
  859. {
  860. if (cpu_hotplug_disabled)
  861. return -EBUSY;
  862. return _cpu_down(cpu, 0, target);
  863. }
  864. static int do_cpu_down(unsigned int cpu, enum cpuhp_state target)
  865. {
  866. int err;
  867. cpu_maps_update_begin();
  868. err = cpu_down_maps_locked(cpu, target);
  869. cpu_maps_update_done();
  870. return err;
  871. }
  872. int cpu_down(unsigned int cpu)
  873. {
  874. return do_cpu_down(cpu, CPUHP_OFFLINE);
  875. }
  876. EXPORT_SYMBOL(cpu_down);
  877. #else
  878. #define takedown_cpu NULL
  879. #endif /*CONFIG_HOTPLUG_CPU*/
  880. /**
  881. * notify_cpu_starting(cpu) - Invoke the callbacks on the starting CPU
  882. * @cpu: cpu that just started
  883. *
  884. * It must be called by the arch code on the new cpu, before the new cpu
  885. * enables interrupts and before the "boot" cpu returns from __cpu_up().
  886. */
  887. void notify_cpu_starting(unsigned int cpu)
  888. {
  889. struct cpuhp_cpu_state *st = per_cpu_ptr(&cpuhp_state, cpu);
  890. enum cpuhp_state target = min((int)st->target, CPUHP_AP_ONLINE);
  891. int ret;
  892. rcu_cpu_starting(cpu); /* Enables RCU usage on this CPU. */
  893. st->booted_once = true;
  894. while (st->state < target) {
  895. st->state++;
  896. ret = cpuhp_invoke_callback(cpu, st->state, true, NULL, NULL);
  897. /*
  898. * STARTING must not fail!
  899. */
  900. WARN_ON_ONCE(ret);
  901. }
  902. }
  903. /*
  904. * Called from the idle task. Wake up the controlling task which brings the
  905. * stopper and the hotplug thread of the upcoming CPU up and then delegates
  906. * the rest of the online bringup to the hotplug thread.
  907. */
  908. void cpuhp_online_idle(enum cpuhp_state state)
  909. {
  910. struct cpuhp_cpu_state *st = this_cpu_ptr(&cpuhp_state);
  911. /* Happens for the boot cpu */
  912. if (state != CPUHP_AP_ONLINE_IDLE)
  913. return;
  914. st->state = CPUHP_AP_ONLINE_IDLE;
  915. complete_ap_thread(st, true);
  916. }
  917. /* Requires cpu_add_remove_lock to be held */
  918. static int _cpu_up(unsigned int cpu, int tasks_frozen, enum cpuhp_state target)
  919. {
  920. struct cpuhp_cpu_state *st = per_cpu_ptr(&cpuhp_state, cpu);
  921. struct task_struct *idle;
  922. int ret = 0;
  923. cpus_write_lock();
  924. if (!cpu_present(cpu)) {
  925. ret = -EINVAL;
  926. goto out;
  927. }
  928. /*
  929. * The caller of do_cpu_up might have raced with another
  930. * caller. Ignore it for now.
  931. */
  932. if (st->state >= target)
  933. goto out;
  934. if (st->state == CPUHP_OFFLINE) {
  935. /* Let it fail before we try to bring the cpu up */
  936. idle = idle_thread_get(cpu);
  937. if (IS_ERR(idle)) {
  938. ret = PTR_ERR(idle);
  939. goto out;
  940. }
  941. }
  942. cpuhp_tasks_frozen = tasks_frozen;
  943. cpuhp_set_state(st, target);
  944. /*
  945. * If the current CPU state is in the range of the AP hotplug thread,
  946. * then we need to kick the thread once more.
  947. */
  948. if (st->state > CPUHP_BRINGUP_CPU) {
  949. ret = cpuhp_kick_ap_work(cpu);
  950. /*
  951. * The AP side has done the error rollback already. Just
  952. * return the error code..
  953. */
  954. if (ret)
  955. goto out;
  956. }
  957. /*
  958. * Try to reach the target state. We max out on the BP at
  959. * CPUHP_BRINGUP_CPU. After that the AP hotplug thread is
  960. * responsible for bringing it up to the target state.
  961. */
  962. target = min((int)target, CPUHP_BRINGUP_CPU);
  963. ret = cpuhp_up_callbacks(cpu, st, target);
  964. out:
  965. cpus_write_unlock();
  966. return ret;
  967. }
  968. static int do_cpu_up(unsigned int cpu, enum cpuhp_state target)
  969. {
  970. int err = 0;
  971. if (!cpu_possible(cpu)) {
  972. pr_err("can't online cpu %d because it is not configured as may-hotadd at boot time\n",
  973. cpu);
  974. #if defined(CONFIG_IA64)
  975. pr_err("please check additional_cpus= boot parameter\n");
  976. #endif
  977. return -EINVAL;
  978. }
  979. err = try_online_node(cpu_to_node(cpu));
  980. if (err)
  981. return err;
  982. cpu_maps_update_begin();
  983. if (cpu_hotplug_disabled) {
  984. err = -EBUSY;
  985. goto out;
  986. }
  987. if (!cpu_smt_allowed(cpu)) {
  988. err = -EPERM;
  989. goto out;
  990. }
  991. err = _cpu_up(cpu, 0, target);
  992. out:
  993. cpu_maps_update_done();
  994. return err;
  995. }
  996. int cpu_up(unsigned int cpu)
  997. {
  998. return do_cpu_up(cpu, CPUHP_ONLINE);
  999. }
  1000. EXPORT_SYMBOL_GPL(cpu_up);
  1001. #ifdef CONFIG_PM_SLEEP_SMP
  1002. static cpumask_var_t frozen_cpus;
  1003. int freeze_secondary_cpus(int primary)
  1004. {
  1005. int cpu, error = 0;
  1006. cpu_maps_update_begin();
  1007. if (!cpu_online(primary))
  1008. primary = cpumask_first(cpu_online_mask);
  1009. /*
  1010. * We take down all of the non-boot CPUs in one shot to avoid races
  1011. * with the userspace trying to use the CPU hotplug at the same time
  1012. */
  1013. cpumask_clear(frozen_cpus);
  1014. pr_info("Disabling non-boot CPUs ...\n");
  1015. for_each_online_cpu(cpu) {
  1016. if (cpu == primary)
  1017. continue;
  1018. trace_suspend_resume(TPS("CPU_OFF"), cpu, true);
  1019. error = _cpu_down(cpu, 1, CPUHP_OFFLINE);
  1020. trace_suspend_resume(TPS("CPU_OFF"), cpu, false);
  1021. if (!error)
  1022. cpumask_set_cpu(cpu, frozen_cpus);
  1023. else {
  1024. pr_err("Error taking CPU%d down: %d\n", cpu, error);
  1025. break;
  1026. }
  1027. }
  1028. if (!error)
  1029. BUG_ON(num_online_cpus() > 1);
  1030. else
  1031. pr_err("Non-boot CPUs are not disabled\n");
  1032. /*
  1033. * Make sure the CPUs won't be enabled by someone else. We need to do
  1034. * this even in case of failure as all disable_nonboot_cpus() users are
  1035. * supposed to do enable_nonboot_cpus() on the failure path.
  1036. */
  1037. cpu_hotplug_disabled++;
  1038. cpu_maps_update_done();
  1039. return error;
  1040. }
  1041. void __weak arch_enable_nonboot_cpus_begin(void)
  1042. {
  1043. }
  1044. void __weak arch_enable_nonboot_cpus_end(void)
  1045. {
  1046. }
  1047. void enable_nonboot_cpus(void)
  1048. {
  1049. int cpu, error;
  1050. /* Allow everyone to use the CPU hotplug again */
  1051. cpu_maps_update_begin();
  1052. __cpu_hotplug_enable();
  1053. if (cpumask_empty(frozen_cpus))
  1054. goto out;
  1055. pr_info("Enabling non-boot CPUs ...\n");
  1056. arch_enable_nonboot_cpus_begin();
  1057. for_each_cpu(cpu, frozen_cpus) {
  1058. trace_suspend_resume(TPS("CPU_ON"), cpu, true);
  1059. error = _cpu_up(cpu, 1, CPUHP_ONLINE);
  1060. trace_suspend_resume(TPS("CPU_ON"), cpu, false);
  1061. if (!error) {
  1062. pr_info("CPU%d is up\n", cpu);
  1063. continue;
  1064. }
  1065. pr_warn("Error taking CPU%d up: %d\n", cpu, error);
  1066. }
  1067. arch_enable_nonboot_cpus_end();
  1068. cpumask_clear(frozen_cpus);
  1069. out:
  1070. cpu_maps_update_done();
  1071. }
  1072. static int __init alloc_frozen_cpus(void)
  1073. {
  1074. if (!alloc_cpumask_var(&frozen_cpus, GFP_KERNEL|__GFP_ZERO))
  1075. return -ENOMEM;
  1076. return 0;
  1077. }
  1078. core_initcall(alloc_frozen_cpus);
  1079. /*
  1080. * When callbacks for CPU hotplug notifications are being executed, we must
  1081. * ensure that the state of the system with respect to the tasks being frozen
  1082. * or not, as reported by the notification, remains unchanged *throughout the
  1083. * duration* of the execution of the callbacks.
  1084. * Hence we need to prevent the freezer from racing with regular CPU hotplug.
  1085. *
  1086. * This synchronization is implemented by mutually excluding regular CPU
  1087. * hotplug and Suspend/Hibernate call paths by hooking onto the Suspend/
  1088. * Hibernate notifications.
  1089. */
  1090. static int
  1091. cpu_hotplug_pm_callback(struct notifier_block *nb,
  1092. unsigned long action, void *ptr)
  1093. {
  1094. switch (action) {
  1095. case PM_SUSPEND_PREPARE:
  1096. case PM_HIBERNATION_PREPARE:
  1097. cpu_hotplug_disable();
  1098. break;
  1099. case PM_POST_SUSPEND:
  1100. case PM_POST_HIBERNATION:
  1101. cpu_hotplug_enable();
  1102. break;
  1103. default:
  1104. return NOTIFY_DONE;
  1105. }
  1106. return NOTIFY_OK;
  1107. }
  1108. static int __init cpu_hotplug_pm_sync_init(void)
  1109. {
  1110. /*
  1111. * cpu_hotplug_pm_callback has higher priority than x86
  1112. * bsp_pm_callback which depends on cpu_hotplug_pm_callback
  1113. * to disable cpu hotplug to avoid cpu hotplug race.
  1114. */
  1115. pm_notifier(cpu_hotplug_pm_callback, 0);
  1116. return 0;
  1117. }
  1118. core_initcall(cpu_hotplug_pm_sync_init);
  1119. #endif /* CONFIG_PM_SLEEP_SMP */
  1120. int __boot_cpu_id;
  1121. #endif /* CONFIG_SMP */
  1122. /* Boot processor state steps */
  1123. static struct cpuhp_step cpuhp_hp_states[] = {
  1124. [CPUHP_OFFLINE] = {
  1125. .name = "offline",
  1126. .startup.single = NULL,
  1127. .teardown.single = NULL,
  1128. },
  1129. #ifdef CONFIG_SMP
  1130. [CPUHP_CREATE_THREADS]= {
  1131. .name = "threads:prepare",
  1132. .startup.single = smpboot_create_threads,
  1133. .teardown.single = NULL,
  1134. .cant_stop = true,
  1135. },
  1136. [CPUHP_PERF_PREPARE] = {
  1137. .name = "perf:prepare",
  1138. .startup.single = perf_event_init_cpu,
  1139. .teardown.single = perf_event_exit_cpu,
  1140. },
  1141. [CPUHP_WORKQUEUE_PREP] = {
  1142. .name = "workqueue:prepare",
  1143. .startup.single = workqueue_prepare_cpu,
  1144. .teardown.single = NULL,
  1145. },
  1146. [CPUHP_HRTIMERS_PREPARE] = {
  1147. .name = "hrtimers:prepare",
  1148. .startup.single = hrtimers_prepare_cpu,
  1149. .teardown.single = hrtimers_dead_cpu,
  1150. },
  1151. [CPUHP_SMPCFD_PREPARE] = {
  1152. .name = "smpcfd:prepare",
  1153. .startup.single = smpcfd_prepare_cpu,
  1154. .teardown.single = smpcfd_dead_cpu,
  1155. },
  1156. [CPUHP_RELAY_PREPARE] = {
  1157. .name = "relay:prepare",
  1158. .startup.single = relay_prepare_cpu,
  1159. .teardown.single = NULL,
  1160. },
  1161. [CPUHP_SLAB_PREPARE] = {
  1162. .name = "slab:prepare",
  1163. .startup.single = slab_prepare_cpu,
  1164. .teardown.single = slab_dead_cpu,
  1165. },
  1166. [CPUHP_RCUTREE_PREP] = {
  1167. .name = "RCU/tree:prepare",
  1168. .startup.single = rcutree_prepare_cpu,
  1169. .teardown.single = rcutree_dead_cpu,
  1170. },
  1171. /*
  1172. * On the tear-down path, timers_dead_cpu() must be invoked
  1173. * before blk_mq_queue_reinit_notify() from notify_dead(),
  1174. * otherwise a RCU stall occurs.
  1175. */
  1176. [CPUHP_TIMERS_PREPARE] = {
  1177. .name = "timers:prepare",
  1178. .startup.single = timers_prepare_cpu,
  1179. .teardown.single = timers_dead_cpu,
  1180. },
  1181. /* Kicks the plugged cpu into life */
  1182. [CPUHP_BRINGUP_CPU] = {
  1183. .name = "cpu:bringup",
  1184. .startup.single = bringup_cpu,
  1185. .teardown.single = NULL,
  1186. .cant_stop = true,
  1187. },
  1188. /* Final state before CPU kills itself */
  1189. [CPUHP_AP_IDLE_DEAD] = {
  1190. .name = "idle:dead",
  1191. },
  1192. /*
  1193. * Last state before CPU enters the idle loop to die. Transient state
  1194. * for synchronization.
  1195. */
  1196. [CPUHP_AP_OFFLINE] = {
  1197. .name = "ap:offline",
  1198. .cant_stop = true,
  1199. },
  1200. /* First state is scheduler control. Interrupts are disabled */
  1201. [CPUHP_AP_SCHED_STARTING] = {
  1202. .name = "sched:starting",
  1203. .startup.single = sched_cpu_starting,
  1204. .teardown.single = sched_cpu_dying,
  1205. },
  1206. [CPUHP_AP_RCUTREE_DYING] = {
  1207. .name = "RCU/tree:dying",
  1208. .startup.single = NULL,
  1209. .teardown.single = rcutree_dying_cpu,
  1210. },
  1211. [CPUHP_AP_SMPCFD_DYING] = {
  1212. .name = "smpcfd:dying",
  1213. .startup.single = NULL,
  1214. .teardown.single = smpcfd_dying_cpu,
  1215. },
  1216. /* Entry state on starting. Interrupts enabled from here on. Transient
  1217. * state for synchronsization */
  1218. [CPUHP_AP_ONLINE] = {
  1219. .name = "ap:online",
  1220. },
  1221. /*
  1222. * Handled on controll processor until the plugged processor manages
  1223. * this itself.
  1224. */
  1225. [CPUHP_TEARDOWN_CPU] = {
  1226. .name = "cpu:teardown",
  1227. .startup.single = NULL,
  1228. .teardown.single = takedown_cpu,
  1229. .cant_stop = true,
  1230. },
  1231. /* Handle smpboot threads park/unpark */
  1232. [CPUHP_AP_SMPBOOT_THREADS] = {
  1233. .name = "smpboot/threads:online",
  1234. .startup.single = smpboot_unpark_threads,
  1235. .teardown.single = smpboot_park_threads,
  1236. },
  1237. [CPUHP_AP_IRQ_AFFINITY_ONLINE] = {
  1238. .name = "irq/affinity:online",
  1239. .startup.single = irq_affinity_online_cpu,
  1240. .teardown.single = NULL,
  1241. },
  1242. [CPUHP_AP_PERF_ONLINE] = {
  1243. .name = "perf:online",
  1244. .startup.single = perf_event_init_cpu,
  1245. .teardown.single = perf_event_exit_cpu,
  1246. },
  1247. [CPUHP_AP_WATCHDOG_ONLINE] = {
  1248. .name = "lockup_detector:online",
  1249. .startup.single = lockup_detector_online_cpu,
  1250. .teardown.single = lockup_detector_offline_cpu,
  1251. },
  1252. [CPUHP_AP_WORKQUEUE_ONLINE] = {
  1253. .name = "workqueue:online",
  1254. .startup.single = workqueue_online_cpu,
  1255. .teardown.single = workqueue_offline_cpu,
  1256. },
  1257. [CPUHP_AP_RCUTREE_ONLINE] = {
  1258. .name = "RCU/tree:online",
  1259. .startup.single = rcutree_online_cpu,
  1260. .teardown.single = rcutree_offline_cpu,
  1261. },
  1262. #endif
  1263. /*
  1264. * The dynamically registered state space is here
  1265. */
  1266. #ifdef CONFIG_SMP
  1267. /* Last state is scheduler control setting the cpu active */
  1268. [CPUHP_AP_ACTIVE] = {
  1269. .name = "sched:active",
  1270. .startup.single = sched_cpu_activate,
  1271. .teardown.single = sched_cpu_deactivate,
  1272. },
  1273. #endif
  1274. /* CPU is fully up and running. */
  1275. [CPUHP_ONLINE] = {
  1276. .name = "online",
  1277. .startup.single = NULL,
  1278. .teardown.single = NULL,
  1279. },
  1280. };
  1281. /* Sanity check for callbacks */
  1282. static int cpuhp_cb_check(enum cpuhp_state state)
  1283. {
  1284. if (state <= CPUHP_OFFLINE || state >= CPUHP_ONLINE)
  1285. return -EINVAL;
  1286. return 0;
  1287. }
  1288. /*
  1289. * Returns a free for dynamic slot assignment of the Online state. The states
  1290. * are protected by the cpuhp_slot_states mutex and an empty slot is identified
  1291. * by having no name assigned.
  1292. */
  1293. static int cpuhp_reserve_state(enum cpuhp_state state)
  1294. {
  1295. enum cpuhp_state i, end;
  1296. struct cpuhp_step *step;
  1297. switch (state) {
  1298. case CPUHP_AP_ONLINE_DYN:
  1299. step = cpuhp_hp_states + CPUHP_AP_ONLINE_DYN;
  1300. end = CPUHP_AP_ONLINE_DYN_END;
  1301. break;
  1302. case CPUHP_BP_PREPARE_DYN:
  1303. step = cpuhp_hp_states + CPUHP_BP_PREPARE_DYN;
  1304. end = CPUHP_BP_PREPARE_DYN_END;
  1305. break;
  1306. default:
  1307. return -EINVAL;
  1308. }
  1309. for (i = state; i <= end; i++, step++) {
  1310. if (!step->name)
  1311. return i;
  1312. }
  1313. WARN(1, "No more dynamic states available for CPU hotplug\n");
  1314. return -ENOSPC;
  1315. }
  1316. static int cpuhp_store_callbacks(enum cpuhp_state state, const char *name,
  1317. int (*startup)(unsigned int cpu),
  1318. int (*teardown)(unsigned int cpu),
  1319. bool multi_instance)
  1320. {
  1321. /* (Un)Install the callbacks for further cpu hotplug operations */
  1322. struct cpuhp_step *sp;
  1323. int ret = 0;
  1324. /*
  1325. * If name is NULL, then the state gets removed.
  1326. *
  1327. * CPUHP_AP_ONLINE_DYN and CPUHP_BP_PREPARE_DYN are handed out on
  1328. * the first allocation from these dynamic ranges, so the removal
  1329. * would trigger a new allocation and clear the wrong (already
  1330. * empty) state, leaving the callbacks of the to be cleared state
  1331. * dangling, which causes wreckage on the next hotplug operation.
  1332. */
  1333. if (name && (state == CPUHP_AP_ONLINE_DYN ||
  1334. state == CPUHP_BP_PREPARE_DYN)) {
  1335. ret = cpuhp_reserve_state(state);
  1336. if (ret < 0)
  1337. return ret;
  1338. state = ret;
  1339. }
  1340. sp = cpuhp_get_step(state);
  1341. if (name && sp->name)
  1342. return -EBUSY;
  1343. sp->startup.single = startup;
  1344. sp->teardown.single = teardown;
  1345. sp->name = name;
  1346. sp->multi_instance = multi_instance;
  1347. INIT_HLIST_HEAD(&sp->list);
  1348. return ret;
  1349. }
  1350. static void *cpuhp_get_teardown_cb(enum cpuhp_state state)
  1351. {
  1352. return cpuhp_get_step(state)->teardown.single;
  1353. }
  1354. /*
  1355. * Call the startup/teardown function for a step either on the AP or
  1356. * on the current CPU.
  1357. */
  1358. static int cpuhp_issue_call(int cpu, enum cpuhp_state state, bool bringup,
  1359. struct hlist_node *node)
  1360. {
  1361. struct cpuhp_step *sp = cpuhp_get_step(state);
  1362. int ret;
  1363. /*
  1364. * If there's nothing to do, we done.
  1365. * Relies on the union for multi_instance.
  1366. */
  1367. if ((bringup && !sp->startup.single) ||
  1368. (!bringup && !sp->teardown.single))
  1369. return 0;
  1370. /*
  1371. * The non AP bound callbacks can fail on bringup. On teardown
  1372. * e.g. module removal we crash for now.
  1373. */
  1374. #ifdef CONFIG_SMP
  1375. if (cpuhp_is_ap_state(state))
  1376. ret = cpuhp_invoke_ap_callback(cpu, state, bringup, node);
  1377. else
  1378. ret = cpuhp_invoke_callback(cpu, state, bringup, node, NULL);
  1379. #else
  1380. ret = cpuhp_invoke_callback(cpu, state, bringup, node, NULL);
  1381. #endif
  1382. BUG_ON(ret && !bringup);
  1383. return ret;
  1384. }
  1385. /*
  1386. * Called from __cpuhp_setup_state on a recoverable failure.
  1387. *
  1388. * Note: The teardown callbacks for rollback are not allowed to fail!
  1389. */
  1390. static void cpuhp_rollback_install(int failedcpu, enum cpuhp_state state,
  1391. struct hlist_node *node)
  1392. {
  1393. int cpu;
  1394. /* Roll back the already executed steps on the other cpus */
  1395. for_each_present_cpu(cpu) {
  1396. struct cpuhp_cpu_state *st = per_cpu_ptr(&cpuhp_state, cpu);
  1397. int cpustate = st->state;
  1398. if (cpu >= failedcpu)
  1399. break;
  1400. /* Did we invoke the startup call on that cpu ? */
  1401. if (cpustate >= state)
  1402. cpuhp_issue_call(cpu, state, false, node);
  1403. }
  1404. }
  1405. int __cpuhp_state_add_instance_cpuslocked(enum cpuhp_state state,
  1406. struct hlist_node *node,
  1407. bool invoke)
  1408. {
  1409. struct cpuhp_step *sp;
  1410. int cpu;
  1411. int ret;
  1412. lockdep_assert_cpus_held();
  1413. sp = cpuhp_get_step(state);
  1414. if (sp->multi_instance == false)
  1415. return -EINVAL;
  1416. mutex_lock(&cpuhp_state_mutex);
  1417. if (!invoke || !sp->startup.multi)
  1418. goto add_node;
  1419. /*
  1420. * Try to call the startup callback for each present cpu
  1421. * depending on the hotplug state of the cpu.
  1422. */
  1423. for_each_present_cpu(cpu) {
  1424. struct cpuhp_cpu_state *st = per_cpu_ptr(&cpuhp_state, cpu);
  1425. int cpustate = st->state;
  1426. if (cpustate < state)
  1427. continue;
  1428. ret = cpuhp_issue_call(cpu, state, true, node);
  1429. if (ret) {
  1430. if (sp->teardown.multi)
  1431. cpuhp_rollback_install(cpu, state, node);
  1432. goto unlock;
  1433. }
  1434. }
  1435. add_node:
  1436. ret = 0;
  1437. hlist_add_head(node, &sp->list);
  1438. unlock:
  1439. mutex_unlock(&cpuhp_state_mutex);
  1440. return ret;
  1441. }
  1442. int __cpuhp_state_add_instance(enum cpuhp_state state, struct hlist_node *node,
  1443. bool invoke)
  1444. {
  1445. int ret;
  1446. cpus_read_lock();
  1447. ret = __cpuhp_state_add_instance_cpuslocked(state, node, invoke);
  1448. cpus_read_unlock();
  1449. return ret;
  1450. }
  1451. EXPORT_SYMBOL_GPL(__cpuhp_state_add_instance);
  1452. /**
  1453. * __cpuhp_setup_state_cpuslocked - Setup the callbacks for an hotplug machine state
  1454. * @state: The state to setup
  1455. * @invoke: If true, the startup function is invoked for cpus where
  1456. * cpu state >= @state
  1457. * @startup: startup callback function
  1458. * @teardown: teardown callback function
  1459. * @multi_instance: State is set up for multiple instances which get
  1460. * added afterwards.
  1461. *
  1462. * The caller needs to hold cpus read locked while calling this function.
  1463. * Returns:
  1464. * On success:
  1465. * Positive state number if @state is CPUHP_AP_ONLINE_DYN
  1466. * 0 for all other states
  1467. * On failure: proper (negative) error code
  1468. */
  1469. int __cpuhp_setup_state_cpuslocked(enum cpuhp_state state,
  1470. const char *name, bool invoke,
  1471. int (*startup)(unsigned int cpu),
  1472. int (*teardown)(unsigned int cpu),
  1473. bool multi_instance)
  1474. {
  1475. int cpu, ret = 0;
  1476. bool dynstate;
  1477. lockdep_assert_cpus_held();
  1478. if (cpuhp_cb_check(state) || !name)
  1479. return -EINVAL;
  1480. mutex_lock(&cpuhp_state_mutex);
  1481. ret = cpuhp_store_callbacks(state, name, startup, teardown,
  1482. multi_instance);
  1483. dynstate = state == CPUHP_AP_ONLINE_DYN;
  1484. if (ret > 0 && dynstate) {
  1485. state = ret;
  1486. ret = 0;
  1487. }
  1488. if (ret || !invoke || !startup)
  1489. goto out;
  1490. /*
  1491. * Try to call the startup callback for each present cpu
  1492. * depending on the hotplug state of the cpu.
  1493. */
  1494. for_each_present_cpu(cpu) {
  1495. struct cpuhp_cpu_state *st = per_cpu_ptr(&cpuhp_state, cpu);
  1496. int cpustate = st->state;
  1497. if (cpustate < state)
  1498. continue;
  1499. ret = cpuhp_issue_call(cpu, state, true, NULL);
  1500. if (ret) {
  1501. if (teardown)
  1502. cpuhp_rollback_install(cpu, state, NULL);
  1503. cpuhp_store_callbacks(state, NULL, NULL, NULL, false);
  1504. goto out;
  1505. }
  1506. }
  1507. out:
  1508. mutex_unlock(&cpuhp_state_mutex);
  1509. /*
  1510. * If the requested state is CPUHP_AP_ONLINE_DYN, return the
  1511. * dynamically allocated state in case of success.
  1512. */
  1513. if (!ret && dynstate)
  1514. return state;
  1515. return ret;
  1516. }
  1517. EXPORT_SYMBOL(__cpuhp_setup_state_cpuslocked);
  1518. int __cpuhp_setup_state(enum cpuhp_state state,
  1519. const char *name, bool invoke,
  1520. int (*startup)(unsigned int cpu),
  1521. int (*teardown)(unsigned int cpu),
  1522. bool multi_instance)
  1523. {
  1524. int ret;
  1525. cpus_read_lock();
  1526. ret = __cpuhp_setup_state_cpuslocked(state, name, invoke, startup,
  1527. teardown, multi_instance);
  1528. cpus_read_unlock();
  1529. return ret;
  1530. }
  1531. EXPORT_SYMBOL(__cpuhp_setup_state);
  1532. int __cpuhp_state_remove_instance(enum cpuhp_state state,
  1533. struct hlist_node *node, bool invoke)
  1534. {
  1535. struct cpuhp_step *sp = cpuhp_get_step(state);
  1536. int cpu;
  1537. BUG_ON(cpuhp_cb_check(state));
  1538. if (!sp->multi_instance)
  1539. return -EINVAL;
  1540. cpus_read_lock();
  1541. mutex_lock(&cpuhp_state_mutex);
  1542. if (!invoke || !cpuhp_get_teardown_cb(state))
  1543. goto remove;
  1544. /*
  1545. * Call the teardown callback for each present cpu depending
  1546. * on the hotplug state of the cpu. This function is not
  1547. * allowed to fail currently!
  1548. */
  1549. for_each_present_cpu(cpu) {
  1550. struct cpuhp_cpu_state *st = per_cpu_ptr(&cpuhp_state, cpu);
  1551. int cpustate = st->state;
  1552. if (cpustate >= state)
  1553. cpuhp_issue_call(cpu, state, false, node);
  1554. }
  1555. remove:
  1556. hlist_del(node);
  1557. mutex_unlock(&cpuhp_state_mutex);
  1558. cpus_read_unlock();
  1559. return 0;
  1560. }
  1561. EXPORT_SYMBOL_GPL(__cpuhp_state_remove_instance);
  1562. /**
  1563. * __cpuhp_remove_state_cpuslocked - Remove the callbacks for an hotplug machine state
  1564. * @state: The state to remove
  1565. * @invoke: If true, the teardown function is invoked for cpus where
  1566. * cpu state >= @state
  1567. *
  1568. * The caller needs to hold cpus read locked while calling this function.
  1569. * The teardown callback is currently not allowed to fail. Think
  1570. * about module removal!
  1571. */
  1572. void __cpuhp_remove_state_cpuslocked(enum cpuhp_state state, bool invoke)
  1573. {
  1574. struct cpuhp_step *sp = cpuhp_get_step(state);
  1575. int cpu;
  1576. BUG_ON(cpuhp_cb_check(state));
  1577. lockdep_assert_cpus_held();
  1578. mutex_lock(&cpuhp_state_mutex);
  1579. if (sp->multi_instance) {
  1580. WARN(!hlist_empty(&sp->list),
  1581. "Error: Removing state %d which has instances left.\n",
  1582. state);
  1583. goto remove;
  1584. }
  1585. if (!invoke || !cpuhp_get_teardown_cb(state))
  1586. goto remove;
  1587. /*
  1588. * Call the teardown callback for each present cpu depending
  1589. * on the hotplug state of the cpu. This function is not
  1590. * allowed to fail currently!
  1591. */
  1592. for_each_present_cpu(cpu) {
  1593. struct cpuhp_cpu_state *st = per_cpu_ptr(&cpuhp_state, cpu);
  1594. int cpustate = st->state;
  1595. if (cpustate >= state)
  1596. cpuhp_issue_call(cpu, state, false, NULL);
  1597. }
  1598. remove:
  1599. cpuhp_store_callbacks(state, NULL, NULL, NULL, false);
  1600. mutex_unlock(&cpuhp_state_mutex);
  1601. }
  1602. EXPORT_SYMBOL(__cpuhp_remove_state_cpuslocked);
  1603. void __cpuhp_remove_state(enum cpuhp_state state, bool invoke)
  1604. {
  1605. cpus_read_lock();
  1606. __cpuhp_remove_state_cpuslocked(state, invoke);
  1607. cpus_read_unlock();
  1608. }
  1609. EXPORT_SYMBOL(__cpuhp_remove_state);
  1610. #if defined(CONFIG_SYSFS) && defined(CONFIG_HOTPLUG_CPU)
  1611. static ssize_t show_cpuhp_state(struct device *dev,
  1612. struct device_attribute *attr, char *buf)
  1613. {
  1614. struct cpuhp_cpu_state *st = per_cpu_ptr(&cpuhp_state, dev->id);
  1615. return sprintf(buf, "%d\n", st->state);
  1616. }
  1617. static DEVICE_ATTR(state, 0444, show_cpuhp_state, NULL);
  1618. static ssize_t write_cpuhp_target(struct device *dev,
  1619. struct device_attribute *attr,
  1620. const char *buf, size_t count)
  1621. {
  1622. struct cpuhp_cpu_state *st = per_cpu_ptr(&cpuhp_state, dev->id);
  1623. struct cpuhp_step *sp;
  1624. int target, ret;
  1625. ret = kstrtoint(buf, 10, &target);
  1626. if (ret)
  1627. return ret;
  1628. #ifdef CONFIG_CPU_HOTPLUG_STATE_CONTROL
  1629. if (target < CPUHP_OFFLINE || target > CPUHP_ONLINE)
  1630. return -EINVAL;
  1631. #else
  1632. if (target != CPUHP_OFFLINE && target != CPUHP_ONLINE)
  1633. return -EINVAL;
  1634. #endif
  1635. ret = lock_device_hotplug_sysfs();
  1636. if (ret)
  1637. return ret;
  1638. mutex_lock(&cpuhp_state_mutex);
  1639. sp = cpuhp_get_step(target);
  1640. ret = !sp->name || sp->cant_stop ? -EINVAL : 0;
  1641. mutex_unlock(&cpuhp_state_mutex);
  1642. if (ret)
  1643. goto out;
  1644. if (st->state < target)
  1645. ret = do_cpu_up(dev->id, target);
  1646. else
  1647. ret = do_cpu_down(dev->id, target);
  1648. out:
  1649. unlock_device_hotplug();
  1650. return ret ? ret : count;
  1651. }
  1652. static ssize_t show_cpuhp_target(struct device *dev,
  1653. struct device_attribute *attr, char *buf)
  1654. {
  1655. struct cpuhp_cpu_state *st = per_cpu_ptr(&cpuhp_state, dev->id);
  1656. return sprintf(buf, "%d\n", st->target);
  1657. }
  1658. static DEVICE_ATTR(target, 0644, show_cpuhp_target, write_cpuhp_target);
  1659. static ssize_t write_cpuhp_fail(struct device *dev,
  1660. struct device_attribute *attr,
  1661. const char *buf, size_t count)
  1662. {
  1663. struct cpuhp_cpu_state *st = per_cpu_ptr(&cpuhp_state, dev->id);
  1664. struct cpuhp_step *sp;
  1665. int fail, ret;
  1666. ret = kstrtoint(buf, 10, &fail);
  1667. if (ret)
  1668. return ret;
  1669. /*
  1670. * Cannot fail STARTING/DYING callbacks.
  1671. */
  1672. if (cpuhp_is_atomic_state(fail))
  1673. return -EINVAL;
  1674. /*
  1675. * Cannot fail anything that doesn't have callbacks.
  1676. */
  1677. mutex_lock(&cpuhp_state_mutex);
  1678. sp = cpuhp_get_step(fail);
  1679. if (!sp->startup.single && !sp->teardown.single)
  1680. ret = -EINVAL;
  1681. mutex_unlock(&cpuhp_state_mutex);
  1682. if (ret)
  1683. return ret;
  1684. st->fail = fail;
  1685. return count;
  1686. }
  1687. static ssize_t show_cpuhp_fail(struct device *dev,
  1688. struct device_attribute *attr, char *buf)
  1689. {
  1690. struct cpuhp_cpu_state *st = per_cpu_ptr(&cpuhp_state, dev->id);
  1691. return sprintf(buf, "%d\n", st->fail);
  1692. }
  1693. static DEVICE_ATTR(fail, 0644, show_cpuhp_fail, write_cpuhp_fail);
  1694. static struct attribute *cpuhp_cpu_attrs[] = {
  1695. &dev_attr_state.attr,
  1696. &dev_attr_target.attr,
  1697. &dev_attr_fail.attr,
  1698. NULL
  1699. };
  1700. static const struct attribute_group cpuhp_cpu_attr_group = {
  1701. .attrs = cpuhp_cpu_attrs,
  1702. .name = "hotplug",
  1703. NULL
  1704. };
  1705. static ssize_t show_cpuhp_states(struct device *dev,
  1706. struct device_attribute *attr, char *buf)
  1707. {
  1708. ssize_t cur, res = 0;
  1709. int i;
  1710. mutex_lock(&cpuhp_state_mutex);
  1711. for (i = CPUHP_OFFLINE; i <= CPUHP_ONLINE; i++) {
  1712. struct cpuhp_step *sp = cpuhp_get_step(i);
  1713. if (sp->name) {
  1714. cur = sprintf(buf, "%3d: %s\n", i, sp->name);
  1715. buf += cur;
  1716. res += cur;
  1717. }
  1718. }
  1719. mutex_unlock(&cpuhp_state_mutex);
  1720. return res;
  1721. }
  1722. static DEVICE_ATTR(states, 0444, show_cpuhp_states, NULL);
  1723. static struct attribute *cpuhp_cpu_root_attrs[] = {
  1724. &dev_attr_states.attr,
  1725. NULL
  1726. };
  1727. static const struct attribute_group cpuhp_cpu_root_attr_group = {
  1728. .attrs = cpuhp_cpu_root_attrs,
  1729. .name = "hotplug",
  1730. NULL
  1731. };
  1732. #ifdef CONFIG_HOTPLUG_SMT
  1733. static const char *smt_states[] = {
  1734. [CPU_SMT_ENABLED] = "on",
  1735. [CPU_SMT_DISABLED] = "off",
  1736. [CPU_SMT_FORCE_DISABLED] = "forceoff",
  1737. [CPU_SMT_NOT_SUPPORTED] = "notsupported",
  1738. };
  1739. static ssize_t
  1740. show_smt_control(struct device *dev, struct device_attribute *attr, char *buf)
  1741. {
  1742. return snprintf(buf, PAGE_SIZE - 2, "%s\n", smt_states[cpu_smt_control]);
  1743. }
  1744. static void cpuhp_offline_cpu_device(unsigned int cpu)
  1745. {
  1746. struct device *dev = get_cpu_device(cpu);
  1747. dev->offline = true;
  1748. /* Tell user space about the state change */
  1749. kobject_uevent(&dev->kobj, KOBJ_OFFLINE);
  1750. }
  1751. static void cpuhp_online_cpu_device(unsigned int cpu)
  1752. {
  1753. struct device *dev = get_cpu_device(cpu);
  1754. dev->offline = false;
  1755. /* Tell user space about the state change */
  1756. kobject_uevent(&dev->kobj, KOBJ_ONLINE);
  1757. }
  1758. static int cpuhp_smt_disable(enum cpuhp_smt_control ctrlval)
  1759. {
  1760. int cpu, ret = 0;
  1761. cpu_maps_update_begin();
  1762. for_each_online_cpu(cpu) {
  1763. if (topology_is_primary_thread(cpu))
  1764. continue;
  1765. ret = cpu_down_maps_locked(cpu, CPUHP_OFFLINE);
  1766. if (ret)
  1767. break;
  1768. /*
  1769. * As this needs to hold the cpu maps lock it's impossible
  1770. * to call device_offline() because that ends up calling
  1771. * cpu_down() which takes cpu maps lock. cpu maps lock
  1772. * needs to be held as this might race against in kernel
  1773. * abusers of the hotplug machinery (thermal management).
  1774. *
  1775. * So nothing would update device:offline state. That would
  1776. * leave the sysfs entry stale and prevent onlining after
  1777. * smt control has been changed to 'off' again. This is
  1778. * called under the sysfs hotplug lock, so it is properly
  1779. * serialized against the regular offline usage.
  1780. */
  1781. cpuhp_offline_cpu_device(cpu);
  1782. }
  1783. if (!ret)
  1784. cpu_smt_control = ctrlval;
  1785. cpu_maps_update_done();
  1786. return ret;
  1787. }
  1788. static int cpuhp_smt_enable(void)
  1789. {
  1790. int cpu, ret = 0;
  1791. cpu_maps_update_begin();
  1792. cpu_smt_control = CPU_SMT_ENABLED;
  1793. for_each_present_cpu(cpu) {
  1794. /* Skip online CPUs and CPUs on offline nodes */
  1795. if (cpu_online(cpu) || !node_online(cpu_to_node(cpu)))
  1796. continue;
  1797. ret = _cpu_up(cpu, 0, CPUHP_ONLINE);
  1798. if (ret)
  1799. break;
  1800. /* See comment in cpuhp_smt_disable() */
  1801. cpuhp_online_cpu_device(cpu);
  1802. }
  1803. cpu_maps_update_done();
  1804. return ret;
  1805. }
  1806. static ssize_t
  1807. store_smt_control(struct device *dev, struct device_attribute *attr,
  1808. const char *buf, size_t count)
  1809. {
  1810. int ctrlval, ret;
  1811. if (sysfs_streq(buf, "on"))
  1812. ctrlval = CPU_SMT_ENABLED;
  1813. else if (sysfs_streq(buf, "off"))
  1814. ctrlval = CPU_SMT_DISABLED;
  1815. else if (sysfs_streq(buf, "forceoff"))
  1816. ctrlval = CPU_SMT_FORCE_DISABLED;
  1817. else
  1818. return -EINVAL;
  1819. if (cpu_smt_control == CPU_SMT_FORCE_DISABLED)
  1820. return -EPERM;
  1821. if (cpu_smt_control == CPU_SMT_NOT_SUPPORTED)
  1822. return -ENODEV;
  1823. ret = lock_device_hotplug_sysfs();
  1824. if (ret)
  1825. return ret;
  1826. if (ctrlval != cpu_smt_control) {
  1827. switch (ctrlval) {
  1828. case CPU_SMT_ENABLED:
  1829. ret = cpuhp_smt_enable();
  1830. break;
  1831. case CPU_SMT_DISABLED:
  1832. case CPU_SMT_FORCE_DISABLED:
  1833. ret = cpuhp_smt_disable(ctrlval);
  1834. break;
  1835. }
  1836. }
  1837. unlock_device_hotplug();
  1838. return ret ? ret : count;
  1839. }
  1840. static DEVICE_ATTR(control, 0644, show_smt_control, store_smt_control);
  1841. static ssize_t
  1842. show_smt_active(struct device *dev, struct device_attribute *attr, char *buf)
  1843. {
  1844. bool active = topology_max_smt_threads() > 1;
  1845. return snprintf(buf, PAGE_SIZE - 2, "%d\n", active);
  1846. }
  1847. static DEVICE_ATTR(active, 0444, show_smt_active, NULL);
  1848. static struct attribute *cpuhp_smt_attrs[] = {
  1849. &dev_attr_control.attr,
  1850. &dev_attr_active.attr,
  1851. NULL
  1852. };
  1853. static const struct attribute_group cpuhp_smt_attr_group = {
  1854. .attrs = cpuhp_smt_attrs,
  1855. .name = "smt",
  1856. NULL
  1857. };
  1858. static int __init cpu_smt_state_init(void)
  1859. {
  1860. return sysfs_create_group(&cpu_subsys.dev_root->kobj,
  1861. &cpuhp_smt_attr_group);
  1862. }
  1863. #else
  1864. static inline int cpu_smt_state_init(void) { return 0; }
  1865. #endif
  1866. static int __init cpuhp_sysfs_init(void)
  1867. {
  1868. int cpu, ret;
  1869. ret = cpu_smt_state_init();
  1870. if (ret)
  1871. return ret;
  1872. ret = sysfs_create_group(&cpu_subsys.dev_root->kobj,
  1873. &cpuhp_cpu_root_attr_group);
  1874. if (ret)
  1875. return ret;
  1876. for_each_possible_cpu(cpu) {
  1877. struct device *dev = get_cpu_device(cpu);
  1878. if (!dev)
  1879. continue;
  1880. ret = sysfs_create_group(&dev->kobj, &cpuhp_cpu_attr_group);
  1881. if (ret)
  1882. return ret;
  1883. }
  1884. return 0;
  1885. }
  1886. device_initcall(cpuhp_sysfs_init);
  1887. #endif
  1888. /*
  1889. * cpu_bit_bitmap[] is a special, "compressed" data structure that
  1890. * represents all NR_CPUS bits binary values of 1<<nr.
  1891. *
  1892. * It is used by cpumask_of() to get a constant address to a CPU
  1893. * mask value that has a single bit set only.
  1894. */
  1895. /* cpu_bit_bitmap[0] is empty - so we can back into it */
  1896. #define MASK_DECLARE_1(x) [x+1][0] = (1UL << (x))
  1897. #define MASK_DECLARE_2(x) MASK_DECLARE_1(x), MASK_DECLARE_1(x+1)
  1898. #define MASK_DECLARE_4(x) MASK_DECLARE_2(x), MASK_DECLARE_2(x+2)
  1899. #define MASK_DECLARE_8(x) MASK_DECLARE_4(x), MASK_DECLARE_4(x+4)
  1900. const unsigned long cpu_bit_bitmap[BITS_PER_LONG+1][BITS_TO_LONGS(NR_CPUS)] = {
  1901. MASK_DECLARE_8(0), MASK_DECLARE_8(8),
  1902. MASK_DECLARE_8(16), MASK_DECLARE_8(24),
  1903. #if BITS_PER_LONG > 32
  1904. MASK_DECLARE_8(32), MASK_DECLARE_8(40),
  1905. MASK_DECLARE_8(48), MASK_DECLARE_8(56),
  1906. #endif
  1907. };
  1908. EXPORT_SYMBOL_GPL(cpu_bit_bitmap);
  1909. const DECLARE_BITMAP(cpu_all_bits, NR_CPUS) = CPU_BITS_ALL;
  1910. EXPORT_SYMBOL(cpu_all_bits);
  1911. #ifdef CONFIG_INIT_ALL_POSSIBLE
  1912. struct cpumask __cpu_possible_mask __read_mostly
  1913. = {CPU_BITS_ALL};
  1914. #else
  1915. struct cpumask __cpu_possible_mask __read_mostly;
  1916. #endif
  1917. EXPORT_SYMBOL(__cpu_possible_mask);
  1918. struct cpumask __cpu_online_mask __read_mostly;
  1919. EXPORT_SYMBOL(__cpu_online_mask);
  1920. struct cpumask __cpu_present_mask __read_mostly;
  1921. EXPORT_SYMBOL(__cpu_present_mask);
  1922. struct cpumask __cpu_active_mask __read_mostly;
  1923. EXPORT_SYMBOL(__cpu_active_mask);
  1924. void init_cpu_present(const struct cpumask *src)
  1925. {
  1926. cpumask_copy(&__cpu_present_mask, src);
  1927. }
  1928. void init_cpu_possible(const struct cpumask *src)
  1929. {
  1930. cpumask_copy(&__cpu_possible_mask, src);
  1931. }
  1932. void init_cpu_online(const struct cpumask *src)
  1933. {
  1934. cpumask_copy(&__cpu_online_mask, src);
  1935. }
  1936. /*
  1937. * Activate the first processor.
  1938. */
  1939. void __init boot_cpu_init(void)
  1940. {
  1941. int cpu = smp_processor_id();
  1942. /* Mark the boot cpu "present", "online" etc for SMP and UP case */
  1943. set_cpu_online(cpu, true);
  1944. set_cpu_active(cpu, true);
  1945. set_cpu_present(cpu, true);
  1946. set_cpu_possible(cpu, true);
  1947. #ifdef CONFIG_SMP
  1948. __boot_cpu_id = cpu;
  1949. #endif
  1950. }
  1951. /*
  1952. * Must be called _AFTER_ setting up the per_cpu areas
  1953. */
  1954. void __init boot_cpu_hotplug_init(void)
  1955. {
  1956. this_cpu_write(cpuhp_state.booted_once, true);
  1957. this_cpu_write(cpuhp_state.state, CPUHP_ONLINE);
  1958. }