cpu.c 55 KB

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