cpu.c 59 KB

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