cpu.c 56 KB

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