cpu.c 54 KB

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